xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision f14eced5)
1 /*
2  *  AArch64 translation
3  *
4  *  Copyright (c) 2013 Alexander Graf <agraf@suse.de>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "qemu/osdep.h"
20 
21 #include "translate.h"
22 #include "translate-a64.h"
23 #include "qemu/log.h"
24 #include "disas/disas.h"
25 #include "arm_ldst.h"
26 #include "semihosting/semihost.h"
27 #include "cpregs.h"
28 
29 static TCGv_i64 cpu_X[32];
30 static TCGv_i64 cpu_pc;
31 
32 /* Load/store exclusive handling */
33 static TCGv_i64 cpu_exclusive_high;
34 
35 static const char *regnames[] = {
36     "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
37     "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
38     "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
39     "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
40 };
41 
42 enum a64_shift_type {
43     A64_SHIFT_TYPE_LSL = 0,
44     A64_SHIFT_TYPE_LSR = 1,
45     A64_SHIFT_TYPE_ASR = 2,
46     A64_SHIFT_TYPE_ROR = 3
47 };
48 
49 /*
50  * Helpers for extracting complex instruction fields
51  */
52 
53 /*
54  * For load/store with an unsigned 12 bit immediate scaled by the element
55  * size. The input has the immediate field in bits [14:3] and the element
56  * size in [2:0].
57  */
58 static int uimm_scaled(DisasContext *s, int x)
59 {
60     unsigned imm = x >> 3;
61     unsigned scale = extract32(x, 0, 3);
62     return imm << scale;
63 }
64 
65 /* For load/store memory tags: scale offset by LOG2_TAG_GRANULE */
66 static int scale_by_log2_tag_granule(DisasContext *s, int x)
67 {
68     return x << LOG2_TAG_GRANULE;
69 }
70 
71 /*
72  * Include the generated decoders.
73  */
74 
75 #include "decode-sme-fa64.c.inc"
76 #include "decode-a64.c.inc"
77 
78 /* Table based decoder typedefs - used when the relevant bits for decode
79  * are too awkwardly scattered across the instruction (eg SIMD).
80  */
81 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
82 
83 typedef struct AArch64DecodeTable {
84     uint32_t pattern;
85     uint32_t mask;
86     AArch64DecodeFn *disas_fn;
87 } AArch64DecodeTable;
88 
89 /* initialize TCG globals.  */
90 void a64_translate_init(void)
91 {
92     int i;
93 
94     cpu_pc = tcg_global_mem_new_i64(tcg_env,
95                                     offsetof(CPUARMState, pc),
96                                     "pc");
97     for (i = 0; i < 32; i++) {
98         cpu_X[i] = tcg_global_mem_new_i64(tcg_env,
99                                           offsetof(CPUARMState, xregs[i]),
100                                           regnames[i]);
101     }
102 
103     cpu_exclusive_high = tcg_global_mem_new_i64(tcg_env,
104         offsetof(CPUARMState, exclusive_high), "exclusive_high");
105 }
106 
107 /*
108  * Return the core mmu_idx to use for A64 load/store insns which
109  * have a "unprivileged load/store" variant. Those insns access
110  * EL0 if executed from an EL which has control over EL0 (usually
111  * EL1) but behave like normal loads and stores if executed from
112  * elsewhere (eg EL3).
113  *
114  * @unpriv : true for the unprivileged encoding; false for the
115  *           normal encoding (in which case we will return the same
116  *           thing as get_mem_index().
117  */
118 static int get_a64_user_mem_index(DisasContext *s, bool unpriv)
119 {
120     /*
121      * If AccType_UNPRIV is not used, the insn uses AccType_NORMAL,
122      * which is the usual mmu_idx for this cpu state.
123      */
124     ARMMMUIdx useridx = s->mmu_idx;
125 
126     if (unpriv && s->unpriv) {
127         /*
128          * We have pre-computed the condition for AccType_UNPRIV.
129          * Therefore we should never get here with a mmu_idx for
130          * which we do not know the corresponding user mmu_idx.
131          */
132         switch (useridx) {
133         case ARMMMUIdx_E10_1:
134         case ARMMMUIdx_E10_1_PAN:
135             useridx = ARMMMUIdx_E10_0;
136             break;
137         case ARMMMUIdx_E20_2:
138         case ARMMMUIdx_E20_2_PAN:
139             useridx = ARMMMUIdx_E20_0;
140             break;
141         default:
142             g_assert_not_reached();
143         }
144     }
145     return arm_to_core_mmu_idx(useridx);
146 }
147 
148 static void set_btype_raw(int val)
149 {
150     tcg_gen_st_i32(tcg_constant_i32(val), tcg_env,
151                    offsetof(CPUARMState, btype));
152 }
153 
154 static void set_btype(DisasContext *s, int val)
155 {
156     /* BTYPE is a 2-bit field, and 0 should be done with reset_btype.  */
157     tcg_debug_assert(val >= 1 && val <= 3);
158     set_btype_raw(val);
159     s->btype = -1;
160 }
161 
162 static void reset_btype(DisasContext *s)
163 {
164     if (s->btype != 0) {
165         set_btype_raw(0);
166         s->btype = 0;
167     }
168 }
169 
170 static void gen_pc_plus_diff(DisasContext *s, TCGv_i64 dest, target_long diff)
171 {
172     assert(s->pc_save != -1);
173     if (tb_cflags(s->base.tb) & CF_PCREL) {
174         tcg_gen_addi_i64(dest, cpu_pc, (s->pc_curr - s->pc_save) + diff);
175     } else {
176         tcg_gen_movi_i64(dest, s->pc_curr + diff);
177     }
178 }
179 
180 void gen_a64_update_pc(DisasContext *s, target_long diff)
181 {
182     gen_pc_plus_diff(s, cpu_pc, diff);
183     s->pc_save = s->pc_curr + diff;
184 }
185 
186 /*
187  * Handle Top Byte Ignore (TBI) bits.
188  *
189  * If address tagging is enabled via the TCR TBI bits:
190  *  + for EL2 and EL3 there is only one TBI bit, and if it is set
191  *    then the address is zero-extended, clearing bits [63:56]
192  *  + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
193  *    and TBI1 controls addresses with bit 55 == 1.
194  *    If the appropriate TBI bit is set for the address then
195  *    the address is sign-extended from bit 55 into bits [63:56]
196  *
197  * Here We have concatenated TBI{1,0} into tbi.
198  */
199 static void gen_top_byte_ignore(DisasContext *s, TCGv_i64 dst,
200                                 TCGv_i64 src, int tbi)
201 {
202     if (tbi == 0) {
203         /* Load unmodified address */
204         tcg_gen_mov_i64(dst, src);
205     } else if (!regime_has_2_ranges(s->mmu_idx)) {
206         /* Force tag byte to all zero */
207         tcg_gen_extract_i64(dst, src, 0, 56);
208     } else {
209         /* Sign-extend from bit 55.  */
210         tcg_gen_sextract_i64(dst, src, 0, 56);
211 
212         switch (tbi) {
213         case 1:
214             /* tbi0 but !tbi1: only use the extension if positive */
215             tcg_gen_and_i64(dst, dst, src);
216             break;
217         case 2:
218             /* !tbi0 but tbi1: only use the extension if negative */
219             tcg_gen_or_i64(dst, dst, src);
220             break;
221         case 3:
222             /* tbi0 and tbi1: always use the extension */
223             break;
224         default:
225             g_assert_not_reached();
226         }
227     }
228 }
229 
230 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
231 {
232     /*
233      * If address tagging is enabled for instructions via the TCR TBI bits,
234      * then loading an address into the PC will clear out any tag.
235      */
236     gen_top_byte_ignore(s, cpu_pc, src, s->tbii);
237     s->pc_save = -1;
238 }
239 
240 /*
241  * Handle MTE and/or TBI.
242  *
243  * For TBI, ideally, we would do nothing.  Proper behaviour on fault is
244  * for the tag to be present in the FAR_ELx register.  But for user-only
245  * mode we do not have a TLB with which to implement this, so we must
246  * remove the top byte now.
247  *
248  * Always return a fresh temporary that we can increment independently
249  * of the write-back address.
250  */
251 
252 TCGv_i64 clean_data_tbi(DisasContext *s, TCGv_i64 addr)
253 {
254     TCGv_i64 clean = tcg_temp_new_i64();
255 #ifdef CONFIG_USER_ONLY
256     gen_top_byte_ignore(s, clean, addr, s->tbid);
257 #else
258     tcg_gen_mov_i64(clean, addr);
259 #endif
260     return clean;
261 }
262 
263 /* Insert a zero tag into src, with the result at dst. */
264 static void gen_address_with_allocation_tag0(TCGv_i64 dst, TCGv_i64 src)
265 {
266     tcg_gen_andi_i64(dst, src, ~MAKE_64BIT_MASK(56, 4));
267 }
268 
269 static void gen_probe_access(DisasContext *s, TCGv_i64 ptr,
270                              MMUAccessType acc, int log2_size)
271 {
272     gen_helper_probe_access(tcg_env, ptr,
273                             tcg_constant_i32(acc),
274                             tcg_constant_i32(get_mem_index(s)),
275                             tcg_constant_i32(1 << log2_size));
276 }
277 
278 /*
279  * For MTE, check a single logical or atomic access.  This probes a single
280  * address, the exact one specified.  The size and alignment of the access
281  * is not relevant to MTE, per se, but watchpoints do require the size,
282  * and we want to recognize those before making any other changes to state.
283  */
284 static TCGv_i64 gen_mte_check1_mmuidx(DisasContext *s, TCGv_i64 addr,
285                                       bool is_write, bool tag_checked,
286                                       MemOp memop, bool is_unpriv,
287                                       int core_idx)
288 {
289     if (tag_checked && s->mte_active[is_unpriv]) {
290         TCGv_i64 ret;
291         int desc = 0;
292 
293         desc = FIELD_DP32(desc, MTEDESC, MIDX, core_idx);
294         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
295         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
296         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
297         desc = FIELD_DP32(desc, MTEDESC, ALIGN, get_alignment_bits(memop));
298         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, memop_size(memop) - 1);
299 
300         ret = tcg_temp_new_i64();
301         gen_helper_mte_check(ret, tcg_env, tcg_constant_i32(desc), addr);
302 
303         return ret;
304     }
305     return clean_data_tbi(s, addr);
306 }
307 
308 TCGv_i64 gen_mte_check1(DisasContext *s, TCGv_i64 addr, bool is_write,
309                         bool tag_checked, MemOp memop)
310 {
311     return gen_mte_check1_mmuidx(s, addr, is_write, tag_checked, memop,
312                                  false, get_mem_index(s));
313 }
314 
315 /*
316  * For MTE, check multiple logical sequential accesses.
317  */
318 TCGv_i64 gen_mte_checkN(DisasContext *s, TCGv_i64 addr, bool is_write,
319                         bool tag_checked, int total_size, MemOp single_mop)
320 {
321     if (tag_checked && s->mte_active[0]) {
322         TCGv_i64 ret;
323         int desc = 0;
324 
325         desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
326         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
327         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
328         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
329         desc = FIELD_DP32(desc, MTEDESC, ALIGN, get_alignment_bits(single_mop));
330         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, total_size - 1);
331 
332         ret = tcg_temp_new_i64();
333         gen_helper_mte_check(ret, tcg_env, tcg_constant_i32(desc), addr);
334 
335         return ret;
336     }
337     return clean_data_tbi(s, addr);
338 }
339 
340 /*
341  * Generate the special alignment check that applies to AccType_ATOMIC
342  * and AccType_ORDERED insns under FEAT_LSE2: the access need not be
343  * naturally aligned, but it must not cross a 16-byte boundary.
344  * See AArch64.CheckAlignment().
345  */
346 static void check_lse2_align(DisasContext *s, int rn, int imm,
347                              bool is_write, MemOp mop)
348 {
349     TCGv_i32 tmp;
350     TCGv_i64 addr;
351     TCGLabel *over_label;
352     MMUAccessType type;
353     int mmu_idx;
354 
355     tmp = tcg_temp_new_i32();
356     tcg_gen_extrl_i64_i32(tmp, cpu_reg_sp(s, rn));
357     tcg_gen_addi_i32(tmp, tmp, imm & 15);
358     tcg_gen_andi_i32(tmp, tmp, 15);
359     tcg_gen_addi_i32(tmp, tmp, memop_size(mop));
360 
361     over_label = gen_new_label();
362     tcg_gen_brcondi_i32(TCG_COND_LEU, tmp, 16, over_label);
363 
364     addr = tcg_temp_new_i64();
365     tcg_gen_addi_i64(addr, cpu_reg_sp(s, rn), imm);
366 
367     type = is_write ? MMU_DATA_STORE : MMU_DATA_LOAD,
368     mmu_idx = get_mem_index(s);
369     gen_helper_unaligned_access(tcg_env, addr, tcg_constant_i32(type),
370                                 tcg_constant_i32(mmu_idx));
371 
372     gen_set_label(over_label);
373 
374 }
375 
376 /* Handle the alignment check for AccType_ATOMIC instructions. */
377 static MemOp check_atomic_align(DisasContext *s, int rn, MemOp mop)
378 {
379     MemOp size = mop & MO_SIZE;
380 
381     if (size == MO_8) {
382         return mop;
383     }
384 
385     /*
386      * If size == MO_128, this is a LDXP, and the operation is single-copy
387      * atomic for each doubleword, not the entire quadword; it still must
388      * be quadword aligned.
389      */
390     if (size == MO_128) {
391         return finalize_memop_atom(s, MO_128 | MO_ALIGN,
392                                    MO_ATOM_IFALIGN_PAIR);
393     }
394     if (dc_isar_feature(aa64_lse2, s)) {
395         check_lse2_align(s, rn, 0, true, mop);
396     } else {
397         mop |= MO_ALIGN;
398     }
399     return finalize_memop(s, mop);
400 }
401 
402 /* Handle the alignment check for AccType_ORDERED instructions. */
403 static MemOp check_ordered_align(DisasContext *s, int rn, int imm,
404                                  bool is_write, MemOp mop)
405 {
406     MemOp size = mop & MO_SIZE;
407 
408     if (size == MO_8) {
409         return mop;
410     }
411     if (size == MO_128) {
412         return finalize_memop_atom(s, MO_128 | MO_ALIGN,
413                                    MO_ATOM_IFALIGN_PAIR);
414     }
415     if (!dc_isar_feature(aa64_lse2, s)) {
416         mop |= MO_ALIGN;
417     } else if (!s->naa) {
418         check_lse2_align(s, rn, imm, is_write, mop);
419     }
420     return finalize_memop(s, mop);
421 }
422 
423 typedef struct DisasCompare64 {
424     TCGCond cond;
425     TCGv_i64 value;
426 } DisasCompare64;
427 
428 static void a64_test_cc(DisasCompare64 *c64, int cc)
429 {
430     DisasCompare c32;
431 
432     arm_test_cc(&c32, cc);
433 
434     /*
435      * Sign-extend the 32-bit value so that the GE/LT comparisons work
436      * properly.  The NE/EQ comparisons are also fine with this choice.
437       */
438     c64->cond = c32.cond;
439     c64->value = tcg_temp_new_i64();
440     tcg_gen_ext_i32_i64(c64->value, c32.value);
441 }
442 
443 static void gen_rebuild_hflags(DisasContext *s)
444 {
445     gen_helper_rebuild_hflags_a64(tcg_env, tcg_constant_i32(s->current_el));
446 }
447 
448 static void gen_exception_internal(int excp)
449 {
450     assert(excp_is_internal(excp));
451     gen_helper_exception_internal(tcg_env, tcg_constant_i32(excp));
452 }
453 
454 static void gen_exception_internal_insn(DisasContext *s, int excp)
455 {
456     gen_a64_update_pc(s, 0);
457     gen_exception_internal(excp);
458     s->base.is_jmp = DISAS_NORETURN;
459 }
460 
461 static void gen_exception_bkpt_insn(DisasContext *s, uint32_t syndrome)
462 {
463     gen_a64_update_pc(s, 0);
464     gen_helper_exception_bkpt_insn(tcg_env, tcg_constant_i32(syndrome));
465     s->base.is_jmp = DISAS_NORETURN;
466 }
467 
468 static void gen_step_complete_exception(DisasContext *s)
469 {
470     /* We just completed step of an insn. Move from Active-not-pending
471      * to Active-pending, and then also take the swstep exception.
472      * This corresponds to making the (IMPDEF) choice to prioritize
473      * swstep exceptions over asynchronous exceptions taken to an exception
474      * level where debug is disabled. This choice has the advantage that
475      * we do not need to maintain internal state corresponding to the
476      * ISV/EX syndrome bits between completion of the step and generation
477      * of the exception, and our syndrome information is always correct.
478      */
479     gen_ss_advance(s);
480     gen_swstep_exception(s, 1, s->is_ldex);
481     s->base.is_jmp = DISAS_NORETURN;
482 }
483 
484 static inline bool use_goto_tb(DisasContext *s, uint64_t dest)
485 {
486     if (s->ss_active) {
487         return false;
488     }
489     return translator_use_goto_tb(&s->base, dest);
490 }
491 
492 static void gen_goto_tb(DisasContext *s, int n, int64_t diff)
493 {
494     if (use_goto_tb(s, s->pc_curr + diff)) {
495         /*
496          * For pcrel, the pc must always be up-to-date on entry to
497          * the linked TB, so that it can use simple additions for all
498          * further adjustments.  For !pcrel, the linked TB is compiled
499          * to know its full virtual address, so we can delay the
500          * update to pc to the unlinked path.  A long chain of links
501          * can thus avoid many updates to the PC.
502          */
503         if (tb_cflags(s->base.tb) & CF_PCREL) {
504             gen_a64_update_pc(s, diff);
505             tcg_gen_goto_tb(n);
506         } else {
507             tcg_gen_goto_tb(n);
508             gen_a64_update_pc(s, diff);
509         }
510         tcg_gen_exit_tb(s->base.tb, n);
511         s->base.is_jmp = DISAS_NORETURN;
512     } else {
513         gen_a64_update_pc(s, diff);
514         if (s->ss_active) {
515             gen_step_complete_exception(s);
516         } else {
517             tcg_gen_lookup_and_goto_ptr();
518             s->base.is_jmp = DISAS_NORETURN;
519         }
520     }
521 }
522 
523 /*
524  * Register access functions
525  *
526  * These functions are used for directly accessing a register in where
527  * changes to the final register value are likely to be made. If you
528  * need to use a register for temporary calculation (e.g. index type
529  * operations) use the read_* form.
530  *
531  * B1.2.1 Register mappings
532  *
533  * In instruction register encoding 31 can refer to ZR (zero register) or
534  * the SP (stack pointer) depending on context. In QEMU's case we map SP
535  * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
536  * This is the point of the _sp forms.
537  */
538 TCGv_i64 cpu_reg(DisasContext *s, int reg)
539 {
540     if (reg == 31) {
541         TCGv_i64 t = tcg_temp_new_i64();
542         tcg_gen_movi_i64(t, 0);
543         return t;
544     } else {
545         return cpu_X[reg];
546     }
547 }
548 
549 /* register access for when 31 == SP */
550 TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
551 {
552     return cpu_X[reg];
553 }
554 
555 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
556  * representing the register contents. This TCGv is an auto-freed
557  * temporary so it need not be explicitly freed, and may be modified.
558  */
559 TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
560 {
561     TCGv_i64 v = tcg_temp_new_i64();
562     if (reg != 31) {
563         if (sf) {
564             tcg_gen_mov_i64(v, cpu_X[reg]);
565         } else {
566             tcg_gen_ext32u_i64(v, cpu_X[reg]);
567         }
568     } else {
569         tcg_gen_movi_i64(v, 0);
570     }
571     return v;
572 }
573 
574 TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
575 {
576     TCGv_i64 v = tcg_temp_new_i64();
577     if (sf) {
578         tcg_gen_mov_i64(v, cpu_X[reg]);
579     } else {
580         tcg_gen_ext32u_i64(v, cpu_X[reg]);
581     }
582     return v;
583 }
584 
585 /* Return the offset into CPUARMState of a slice (from
586  * the least significant end) of FP register Qn (ie
587  * Dn, Sn, Hn or Bn).
588  * (Note that this is not the same mapping as for A32; see cpu.h)
589  */
590 static inline int fp_reg_offset(DisasContext *s, int regno, MemOp size)
591 {
592     return vec_reg_offset(s, regno, 0, size);
593 }
594 
595 /* Offset of the high half of the 128 bit vector Qn */
596 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
597 {
598     return vec_reg_offset(s, regno, 1, MO_64);
599 }
600 
601 /* Convenience accessors for reading and writing single and double
602  * FP registers. Writing clears the upper parts of the associated
603  * 128 bit vector register, as required by the architecture.
604  * Note that unlike the GP register accessors, the values returned
605  * by the read functions must be manually freed.
606  */
607 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
608 {
609     TCGv_i64 v = tcg_temp_new_i64();
610 
611     tcg_gen_ld_i64(v, tcg_env, fp_reg_offset(s, reg, MO_64));
612     return v;
613 }
614 
615 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
616 {
617     TCGv_i32 v = tcg_temp_new_i32();
618 
619     tcg_gen_ld_i32(v, tcg_env, fp_reg_offset(s, reg, MO_32));
620     return v;
621 }
622 
623 static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
624 {
625     TCGv_i32 v = tcg_temp_new_i32();
626 
627     tcg_gen_ld16u_i32(v, tcg_env, fp_reg_offset(s, reg, MO_16));
628     return v;
629 }
630 
631 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
632  * If SVE is not enabled, then there are only 128 bits in the vector.
633  */
634 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
635 {
636     unsigned ofs = fp_reg_offset(s, rd, MO_64);
637     unsigned vsz = vec_full_reg_size(s);
638 
639     /* Nop move, with side effect of clearing the tail. */
640     tcg_gen_gvec_mov(MO_64, ofs, ofs, is_q ? 16 : 8, vsz);
641 }
642 
643 void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
644 {
645     unsigned ofs = fp_reg_offset(s, reg, MO_64);
646 
647     tcg_gen_st_i64(v, tcg_env, ofs);
648     clear_vec_high(s, false, reg);
649 }
650 
651 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
652 {
653     TCGv_i64 tmp = tcg_temp_new_i64();
654 
655     tcg_gen_extu_i32_i64(tmp, v);
656     write_fp_dreg(s, reg, tmp);
657 }
658 
659 /* Expand a 2-operand AdvSIMD vector operation using an expander function.  */
660 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
661                          GVecGen2Fn *gvec_fn, int vece)
662 {
663     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
664             is_q ? 16 : 8, vec_full_reg_size(s));
665 }
666 
667 /* Expand a 2-operand + immediate AdvSIMD vector operation using
668  * an expander function.
669  */
670 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
671                           int64_t imm, GVecGen2iFn *gvec_fn, int vece)
672 {
673     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
674             imm, is_q ? 16 : 8, vec_full_reg_size(s));
675 }
676 
677 /* Expand a 3-operand AdvSIMD vector operation using an expander function.  */
678 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
679                          GVecGen3Fn *gvec_fn, int vece)
680 {
681     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
682             vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
683 }
684 
685 /* Expand a 4-operand AdvSIMD vector operation using an expander function.  */
686 static void gen_gvec_fn4(DisasContext *s, bool is_q, int rd, int rn, int rm,
687                          int rx, GVecGen4Fn *gvec_fn, int vece)
688 {
689     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
690             vec_full_reg_offset(s, rm), vec_full_reg_offset(s, rx),
691             is_q ? 16 : 8, vec_full_reg_size(s));
692 }
693 
694 /* Expand a 2-operand operation using an out-of-line helper.  */
695 static void gen_gvec_op2_ool(DisasContext *s, bool is_q, int rd,
696                              int rn, int data, gen_helper_gvec_2 *fn)
697 {
698     tcg_gen_gvec_2_ool(vec_full_reg_offset(s, rd),
699                        vec_full_reg_offset(s, rn),
700                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
701 }
702 
703 /* Expand a 3-operand operation using an out-of-line helper.  */
704 static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
705                              int rn, int rm, int data, gen_helper_gvec_3 *fn)
706 {
707     tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
708                        vec_full_reg_offset(s, rn),
709                        vec_full_reg_offset(s, rm),
710                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
711 }
712 
713 /* Expand a 3-operand + fpstatus pointer + simd data value operation using
714  * an out-of-line helper.
715  */
716 static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
717                               int rm, bool is_fp16, int data,
718                               gen_helper_gvec_3_ptr *fn)
719 {
720     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
721     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
722                        vec_full_reg_offset(s, rn),
723                        vec_full_reg_offset(s, rm), fpst,
724                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
725 }
726 
727 /* Expand a 3-operand + qc + operation using an out-of-line helper.  */
728 static void gen_gvec_op3_qc(DisasContext *s, bool is_q, int rd, int rn,
729                             int rm, gen_helper_gvec_3_ptr *fn)
730 {
731     TCGv_ptr qc_ptr = tcg_temp_new_ptr();
732 
733     tcg_gen_addi_ptr(qc_ptr, tcg_env, offsetof(CPUARMState, vfp.qc));
734     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
735                        vec_full_reg_offset(s, rn),
736                        vec_full_reg_offset(s, rm), qc_ptr,
737                        is_q ? 16 : 8, vec_full_reg_size(s), 0, fn);
738 }
739 
740 /* Expand a 4-operand operation using an out-of-line helper.  */
741 static void gen_gvec_op4_ool(DisasContext *s, bool is_q, int rd, int rn,
742                              int rm, int ra, int data, gen_helper_gvec_4 *fn)
743 {
744     tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
745                        vec_full_reg_offset(s, rn),
746                        vec_full_reg_offset(s, rm),
747                        vec_full_reg_offset(s, ra),
748                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
749 }
750 
751 /*
752  * Expand a 4-operand + fpstatus pointer + simd data value operation using
753  * an out-of-line helper.
754  */
755 static void gen_gvec_op4_fpst(DisasContext *s, bool is_q, int rd, int rn,
756                               int rm, int ra, bool is_fp16, int data,
757                               gen_helper_gvec_4_ptr *fn)
758 {
759     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
760     tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
761                        vec_full_reg_offset(s, rn),
762                        vec_full_reg_offset(s, rm),
763                        vec_full_reg_offset(s, ra), fpst,
764                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
765 }
766 
767 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
768  * than the 32 bit equivalent.
769  */
770 static inline void gen_set_NZ64(TCGv_i64 result)
771 {
772     tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
773     tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
774 }
775 
776 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
777 static inline void gen_logic_CC(int sf, TCGv_i64 result)
778 {
779     if (sf) {
780         gen_set_NZ64(result);
781     } else {
782         tcg_gen_extrl_i64_i32(cpu_ZF, result);
783         tcg_gen_mov_i32(cpu_NF, cpu_ZF);
784     }
785     tcg_gen_movi_i32(cpu_CF, 0);
786     tcg_gen_movi_i32(cpu_VF, 0);
787 }
788 
789 /* dest = T0 + T1; compute C, N, V and Z flags */
790 static void gen_add64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
791 {
792     TCGv_i64 result, flag, tmp;
793     result = tcg_temp_new_i64();
794     flag = tcg_temp_new_i64();
795     tmp = tcg_temp_new_i64();
796 
797     tcg_gen_movi_i64(tmp, 0);
798     tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
799 
800     tcg_gen_extrl_i64_i32(cpu_CF, flag);
801 
802     gen_set_NZ64(result);
803 
804     tcg_gen_xor_i64(flag, result, t0);
805     tcg_gen_xor_i64(tmp, t0, t1);
806     tcg_gen_andc_i64(flag, flag, tmp);
807     tcg_gen_extrh_i64_i32(cpu_VF, flag);
808 
809     tcg_gen_mov_i64(dest, result);
810 }
811 
812 static void gen_add32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
813 {
814     TCGv_i32 t0_32 = tcg_temp_new_i32();
815     TCGv_i32 t1_32 = tcg_temp_new_i32();
816     TCGv_i32 tmp = tcg_temp_new_i32();
817 
818     tcg_gen_movi_i32(tmp, 0);
819     tcg_gen_extrl_i64_i32(t0_32, t0);
820     tcg_gen_extrl_i64_i32(t1_32, t1);
821     tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
822     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
823     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
824     tcg_gen_xor_i32(tmp, t0_32, t1_32);
825     tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
826     tcg_gen_extu_i32_i64(dest, cpu_NF);
827 }
828 
829 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
830 {
831     if (sf) {
832         gen_add64_CC(dest, t0, t1);
833     } else {
834         gen_add32_CC(dest, t0, t1);
835     }
836 }
837 
838 /* dest = T0 - T1; compute C, N, V and Z flags */
839 static void gen_sub64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
840 {
841     /* 64 bit arithmetic */
842     TCGv_i64 result, flag, tmp;
843 
844     result = tcg_temp_new_i64();
845     flag = tcg_temp_new_i64();
846     tcg_gen_sub_i64(result, t0, t1);
847 
848     gen_set_NZ64(result);
849 
850     tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
851     tcg_gen_extrl_i64_i32(cpu_CF, flag);
852 
853     tcg_gen_xor_i64(flag, result, t0);
854     tmp = tcg_temp_new_i64();
855     tcg_gen_xor_i64(tmp, t0, t1);
856     tcg_gen_and_i64(flag, flag, tmp);
857     tcg_gen_extrh_i64_i32(cpu_VF, flag);
858     tcg_gen_mov_i64(dest, result);
859 }
860 
861 static void gen_sub32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
862 {
863     /* 32 bit arithmetic */
864     TCGv_i32 t0_32 = tcg_temp_new_i32();
865     TCGv_i32 t1_32 = tcg_temp_new_i32();
866     TCGv_i32 tmp;
867 
868     tcg_gen_extrl_i64_i32(t0_32, t0);
869     tcg_gen_extrl_i64_i32(t1_32, t1);
870     tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
871     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
872     tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
873     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
874     tmp = tcg_temp_new_i32();
875     tcg_gen_xor_i32(tmp, t0_32, t1_32);
876     tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
877     tcg_gen_extu_i32_i64(dest, cpu_NF);
878 }
879 
880 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
881 {
882     if (sf) {
883         gen_sub64_CC(dest, t0, t1);
884     } else {
885         gen_sub32_CC(dest, t0, t1);
886     }
887 }
888 
889 /* dest = T0 + T1 + CF; do not compute flags. */
890 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
891 {
892     TCGv_i64 flag = tcg_temp_new_i64();
893     tcg_gen_extu_i32_i64(flag, cpu_CF);
894     tcg_gen_add_i64(dest, t0, t1);
895     tcg_gen_add_i64(dest, dest, flag);
896 
897     if (!sf) {
898         tcg_gen_ext32u_i64(dest, dest);
899     }
900 }
901 
902 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
903 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
904 {
905     if (sf) {
906         TCGv_i64 result = tcg_temp_new_i64();
907         TCGv_i64 cf_64 = tcg_temp_new_i64();
908         TCGv_i64 vf_64 = tcg_temp_new_i64();
909         TCGv_i64 tmp = tcg_temp_new_i64();
910         TCGv_i64 zero = tcg_constant_i64(0);
911 
912         tcg_gen_extu_i32_i64(cf_64, cpu_CF);
913         tcg_gen_add2_i64(result, cf_64, t0, zero, cf_64, zero);
914         tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, zero);
915         tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
916         gen_set_NZ64(result);
917 
918         tcg_gen_xor_i64(vf_64, result, t0);
919         tcg_gen_xor_i64(tmp, t0, t1);
920         tcg_gen_andc_i64(vf_64, vf_64, tmp);
921         tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
922 
923         tcg_gen_mov_i64(dest, result);
924     } else {
925         TCGv_i32 t0_32 = tcg_temp_new_i32();
926         TCGv_i32 t1_32 = tcg_temp_new_i32();
927         TCGv_i32 tmp = tcg_temp_new_i32();
928         TCGv_i32 zero = tcg_constant_i32(0);
929 
930         tcg_gen_extrl_i64_i32(t0_32, t0);
931         tcg_gen_extrl_i64_i32(t1_32, t1);
932         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, zero, cpu_CF, zero);
933         tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, zero);
934 
935         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
936         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
937         tcg_gen_xor_i32(tmp, t0_32, t1_32);
938         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
939         tcg_gen_extu_i32_i64(dest, cpu_NF);
940     }
941 }
942 
943 /*
944  * Load/Store generators
945  */
946 
947 /*
948  * Store from GPR register to memory.
949  */
950 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
951                              TCGv_i64 tcg_addr, MemOp memop, int memidx,
952                              bool iss_valid,
953                              unsigned int iss_srt,
954                              bool iss_sf, bool iss_ar)
955 {
956     tcg_gen_qemu_st_i64(source, tcg_addr, memidx, memop);
957 
958     if (iss_valid) {
959         uint32_t syn;
960 
961         syn = syn_data_abort_with_iss(0,
962                                       (memop & MO_SIZE),
963                                       false,
964                                       iss_srt,
965                                       iss_sf,
966                                       iss_ar,
967                                       0, 0, 0, 0, 0, false);
968         disas_set_insn_syndrome(s, syn);
969     }
970 }
971 
972 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
973                       TCGv_i64 tcg_addr, MemOp memop,
974                       bool iss_valid,
975                       unsigned int iss_srt,
976                       bool iss_sf, bool iss_ar)
977 {
978     do_gpr_st_memidx(s, source, tcg_addr, memop, get_mem_index(s),
979                      iss_valid, iss_srt, iss_sf, iss_ar);
980 }
981 
982 /*
983  * Load from memory to GPR register
984  */
985 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
986                              MemOp memop, bool extend, int memidx,
987                              bool iss_valid, unsigned int iss_srt,
988                              bool iss_sf, bool iss_ar)
989 {
990     tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
991 
992     if (extend && (memop & MO_SIGN)) {
993         g_assert((memop & MO_SIZE) <= MO_32);
994         tcg_gen_ext32u_i64(dest, dest);
995     }
996 
997     if (iss_valid) {
998         uint32_t syn;
999 
1000         syn = syn_data_abort_with_iss(0,
1001                                       (memop & MO_SIZE),
1002                                       (memop & MO_SIGN) != 0,
1003                                       iss_srt,
1004                                       iss_sf,
1005                                       iss_ar,
1006                                       0, 0, 0, 0, 0, false);
1007         disas_set_insn_syndrome(s, syn);
1008     }
1009 }
1010 
1011 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
1012                       MemOp memop, bool extend,
1013                       bool iss_valid, unsigned int iss_srt,
1014                       bool iss_sf, bool iss_ar)
1015 {
1016     do_gpr_ld_memidx(s, dest, tcg_addr, memop, extend, get_mem_index(s),
1017                      iss_valid, iss_srt, iss_sf, iss_ar);
1018 }
1019 
1020 /*
1021  * Store from FP register to memory
1022  */
1023 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, MemOp mop)
1024 {
1025     /* This writes the bottom N bits of a 128 bit wide vector to memory */
1026     TCGv_i64 tmplo = tcg_temp_new_i64();
1027 
1028     tcg_gen_ld_i64(tmplo, tcg_env, fp_reg_offset(s, srcidx, MO_64));
1029 
1030     if ((mop & MO_SIZE) < MO_128) {
1031         tcg_gen_qemu_st_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1032     } else {
1033         TCGv_i64 tmphi = tcg_temp_new_i64();
1034         TCGv_i128 t16 = tcg_temp_new_i128();
1035 
1036         tcg_gen_ld_i64(tmphi, tcg_env, fp_reg_hi_offset(s, srcidx));
1037         tcg_gen_concat_i64_i128(t16, tmplo, tmphi);
1038 
1039         tcg_gen_qemu_st_i128(t16, tcg_addr, get_mem_index(s), mop);
1040     }
1041 }
1042 
1043 /*
1044  * Load from memory to FP register
1045  */
1046 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, MemOp mop)
1047 {
1048     /* This always zero-extends and writes to a full 128 bit wide vector */
1049     TCGv_i64 tmplo = tcg_temp_new_i64();
1050     TCGv_i64 tmphi = NULL;
1051 
1052     if ((mop & MO_SIZE) < MO_128) {
1053         tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1054     } else {
1055         TCGv_i128 t16 = tcg_temp_new_i128();
1056 
1057         tcg_gen_qemu_ld_i128(t16, tcg_addr, get_mem_index(s), mop);
1058 
1059         tmphi = tcg_temp_new_i64();
1060         tcg_gen_extr_i128_i64(tmplo, tmphi, t16);
1061     }
1062 
1063     tcg_gen_st_i64(tmplo, tcg_env, fp_reg_offset(s, destidx, MO_64));
1064 
1065     if (tmphi) {
1066         tcg_gen_st_i64(tmphi, tcg_env, fp_reg_hi_offset(s, destidx));
1067     }
1068     clear_vec_high(s, tmphi != NULL, destidx);
1069 }
1070 
1071 /*
1072  * Vector load/store helpers.
1073  *
1074  * The principal difference between this and a FP load is that we don't
1075  * zero extend as we are filling a partial chunk of the vector register.
1076  * These functions don't support 128 bit loads/stores, which would be
1077  * normal load/store operations.
1078  *
1079  * The _i32 versions are useful when operating on 32 bit quantities
1080  * (eg for floating point single or using Neon helper functions).
1081  */
1082 
1083 /* Get value of an element within a vector register */
1084 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
1085                              int element, MemOp memop)
1086 {
1087     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1088     switch ((unsigned)memop) {
1089     case MO_8:
1090         tcg_gen_ld8u_i64(tcg_dest, tcg_env, vect_off);
1091         break;
1092     case MO_16:
1093         tcg_gen_ld16u_i64(tcg_dest, tcg_env, vect_off);
1094         break;
1095     case MO_32:
1096         tcg_gen_ld32u_i64(tcg_dest, tcg_env, vect_off);
1097         break;
1098     case MO_8|MO_SIGN:
1099         tcg_gen_ld8s_i64(tcg_dest, tcg_env, vect_off);
1100         break;
1101     case MO_16|MO_SIGN:
1102         tcg_gen_ld16s_i64(tcg_dest, tcg_env, vect_off);
1103         break;
1104     case MO_32|MO_SIGN:
1105         tcg_gen_ld32s_i64(tcg_dest, tcg_env, vect_off);
1106         break;
1107     case MO_64:
1108     case MO_64|MO_SIGN:
1109         tcg_gen_ld_i64(tcg_dest, tcg_env, vect_off);
1110         break;
1111     default:
1112         g_assert_not_reached();
1113     }
1114 }
1115 
1116 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1117                                  int element, MemOp memop)
1118 {
1119     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1120     switch (memop) {
1121     case MO_8:
1122         tcg_gen_ld8u_i32(tcg_dest, tcg_env, vect_off);
1123         break;
1124     case MO_16:
1125         tcg_gen_ld16u_i32(tcg_dest, tcg_env, vect_off);
1126         break;
1127     case MO_8|MO_SIGN:
1128         tcg_gen_ld8s_i32(tcg_dest, tcg_env, vect_off);
1129         break;
1130     case MO_16|MO_SIGN:
1131         tcg_gen_ld16s_i32(tcg_dest, tcg_env, vect_off);
1132         break;
1133     case MO_32:
1134     case MO_32|MO_SIGN:
1135         tcg_gen_ld_i32(tcg_dest, tcg_env, vect_off);
1136         break;
1137     default:
1138         g_assert_not_reached();
1139     }
1140 }
1141 
1142 /* Set value of an element within a vector register */
1143 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1144                               int element, MemOp memop)
1145 {
1146     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1147     switch (memop) {
1148     case MO_8:
1149         tcg_gen_st8_i64(tcg_src, tcg_env, vect_off);
1150         break;
1151     case MO_16:
1152         tcg_gen_st16_i64(tcg_src, tcg_env, vect_off);
1153         break;
1154     case MO_32:
1155         tcg_gen_st32_i64(tcg_src, tcg_env, vect_off);
1156         break;
1157     case MO_64:
1158         tcg_gen_st_i64(tcg_src, tcg_env, vect_off);
1159         break;
1160     default:
1161         g_assert_not_reached();
1162     }
1163 }
1164 
1165 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1166                                   int destidx, int element, MemOp memop)
1167 {
1168     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1169     switch (memop) {
1170     case MO_8:
1171         tcg_gen_st8_i32(tcg_src, tcg_env, vect_off);
1172         break;
1173     case MO_16:
1174         tcg_gen_st16_i32(tcg_src, tcg_env, vect_off);
1175         break;
1176     case MO_32:
1177         tcg_gen_st_i32(tcg_src, tcg_env, vect_off);
1178         break;
1179     default:
1180         g_assert_not_reached();
1181     }
1182 }
1183 
1184 /* Store from vector register to memory */
1185 static void do_vec_st(DisasContext *s, int srcidx, int element,
1186                       TCGv_i64 tcg_addr, MemOp mop)
1187 {
1188     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1189 
1190     read_vec_element(s, tcg_tmp, srcidx, element, mop & MO_SIZE);
1191     tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1192 }
1193 
1194 /* Load from memory to vector register */
1195 static void do_vec_ld(DisasContext *s, int destidx, int element,
1196                       TCGv_i64 tcg_addr, MemOp mop)
1197 {
1198     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1199 
1200     tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1201     write_vec_element(s, tcg_tmp, destidx, element, mop & MO_SIZE);
1202 }
1203 
1204 /* Check that FP/Neon access is enabled. If it is, return
1205  * true. If not, emit code to generate an appropriate exception,
1206  * and return false; the caller should not emit any code for
1207  * the instruction. Note that this check must happen after all
1208  * unallocated-encoding checks (otherwise the syndrome information
1209  * for the resulting exception will be incorrect).
1210  */
1211 static bool fp_access_check_only(DisasContext *s)
1212 {
1213     if (s->fp_excp_el) {
1214         assert(!s->fp_access_checked);
1215         s->fp_access_checked = true;
1216 
1217         gen_exception_insn_el(s, 0, EXCP_UDEF,
1218                               syn_fp_access_trap(1, 0xe, false, 0),
1219                               s->fp_excp_el);
1220         return false;
1221     }
1222     s->fp_access_checked = true;
1223     return true;
1224 }
1225 
1226 static bool fp_access_check(DisasContext *s)
1227 {
1228     if (!fp_access_check_only(s)) {
1229         return false;
1230     }
1231     if (s->sme_trap_nonstreaming && s->is_nonstreaming) {
1232         gen_exception_insn(s, 0, EXCP_UDEF,
1233                            syn_smetrap(SME_ET_Streaming, false));
1234         return false;
1235     }
1236     return true;
1237 }
1238 
1239 /*
1240  * Check that SVE access is enabled.  If it is, return true.
1241  * If not, emit code to generate an appropriate exception and return false.
1242  * This function corresponds to CheckSVEEnabled().
1243  */
1244 bool sve_access_check(DisasContext *s)
1245 {
1246     if (s->pstate_sm || !dc_isar_feature(aa64_sve, s)) {
1247         assert(dc_isar_feature(aa64_sme, s));
1248         if (!sme_sm_enabled_check(s)) {
1249             goto fail_exit;
1250         }
1251     } else if (s->sve_excp_el) {
1252         gen_exception_insn_el(s, 0, EXCP_UDEF,
1253                               syn_sve_access_trap(), s->sve_excp_el);
1254         goto fail_exit;
1255     }
1256     s->sve_access_checked = true;
1257     return fp_access_check(s);
1258 
1259  fail_exit:
1260     /* Assert that we only raise one exception per instruction. */
1261     assert(!s->sve_access_checked);
1262     s->sve_access_checked = true;
1263     return false;
1264 }
1265 
1266 /*
1267  * Check that SME access is enabled, raise an exception if not.
1268  * Note that this function corresponds to CheckSMEAccess and is
1269  * only used directly for cpregs.
1270  */
1271 static bool sme_access_check(DisasContext *s)
1272 {
1273     if (s->sme_excp_el) {
1274         gen_exception_insn_el(s, 0, EXCP_UDEF,
1275                               syn_smetrap(SME_ET_AccessTrap, false),
1276                               s->sme_excp_el);
1277         return false;
1278     }
1279     return true;
1280 }
1281 
1282 /* This function corresponds to CheckSMEEnabled. */
1283 bool sme_enabled_check(DisasContext *s)
1284 {
1285     /*
1286      * Note that unlike sve_excp_el, we have not constrained sme_excp_el
1287      * to be zero when fp_excp_el has priority.  This is because we need
1288      * sme_excp_el by itself for cpregs access checks.
1289      */
1290     if (!s->fp_excp_el || s->sme_excp_el < s->fp_excp_el) {
1291         s->fp_access_checked = true;
1292         return sme_access_check(s);
1293     }
1294     return fp_access_check_only(s);
1295 }
1296 
1297 /* Common subroutine for CheckSMEAnd*Enabled. */
1298 bool sme_enabled_check_with_svcr(DisasContext *s, unsigned req)
1299 {
1300     if (!sme_enabled_check(s)) {
1301         return false;
1302     }
1303     if (FIELD_EX64(req, SVCR, SM) && !s->pstate_sm) {
1304         gen_exception_insn(s, 0, EXCP_UDEF,
1305                            syn_smetrap(SME_ET_NotStreaming, false));
1306         return false;
1307     }
1308     if (FIELD_EX64(req, SVCR, ZA) && !s->pstate_za) {
1309         gen_exception_insn(s, 0, EXCP_UDEF,
1310                            syn_smetrap(SME_ET_InactiveZA, false));
1311         return false;
1312     }
1313     return true;
1314 }
1315 
1316 /*
1317  * This utility function is for doing register extension with an
1318  * optional shift. You will likely want to pass a temporary for the
1319  * destination register. See DecodeRegExtend() in the ARM ARM.
1320  */
1321 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1322                               int option, unsigned int shift)
1323 {
1324     int extsize = extract32(option, 0, 2);
1325     bool is_signed = extract32(option, 2, 1);
1326 
1327     if (is_signed) {
1328         switch (extsize) {
1329         case 0:
1330             tcg_gen_ext8s_i64(tcg_out, tcg_in);
1331             break;
1332         case 1:
1333             tcg_gen_ext16s_i64(tcg_out, tcg_in);
1334             break;
1335         case 2:
1336             tcg_gen_ext32s_i64(tcg_out, tcg_in);
1337             break;
1338         case 3:
1339             tcg_gen_mov_i64(tcg_out, tcg_in);
1340             break;
1341         }
1342     } else {
1343         switch (extsize) {
1344         case 0:
1345             tcg_gen_ext8u_i64(tcg_out, tcg_in);
1346             break;
1347         case 1:
1348             tcg_gen_ext16u_i64(tcg_out, tcg_in);
1349             break;
1350         case 2:
1351             tcg_gen_ext32u_i64(tcg_out, tcg_in);
1352             break;
1353         case 3:
1354             tcg_gen_mov_i64(tcg_out, tcg_in);
1355             break;
1356         }
1357     }
1358 
1359     if (shift) {
1360         tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1361     }
1362 }
1363 
1364 static inline void gen_check_sp_alignment(DisasContext *s)
1365 {
1366     /* The AArch64 architecture mandates that (if enabled via PSTATE
1367      * or SCTLR bits) there is a check that SP is 16-aligned on every
1368      * SP-relative load or store (with an exception generated if it is not).
1369      * In line with general QEMU practice regarding misaligned accesses,
1370      * we omit these checks for the sake of guest program performance.
1371      * This function is provided as a hook so we can more easily add these
1372      * checks in future (possibly as a "favour catching guest program bugs
1373      * over speed" user selectable option).
1374      */
1375 }
1376 
1377 /*
1378  * This provides a simple table based table lookup decoder. It is
1379  * intended to be used when the relevant bits for decode are too
1380  * awkwardly placed and switch/if based logic would be confusing and
1381  * deeply nested. Since it's a linear search through the table, tables
1382  * should be kept small.
1383  *
1384  * It returns the first handler where insn & mask == pattern, or
1385  * NULL if there is no match.
1386  * The table is terminated by an empty mask (i.e. 0)
1387  */
1388 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1389                                                uint32_t insn)
1390 {
1391     const AArch64DecodeTable *tptr = table;
1392 
1393     while (tptr->mask) {
1394         if ((insn & tptr->mask) == tptr->pattern) {
1395             return tptr->disas_fn;
1396         }
1397         tptr++;
1398     }
1399     return NULL;
1400 }
1401 
1402 /*
1403  * The instruction disassembly implemented here matches
1404  * the instruction encoding classifications in chapter C4
1405  * of the ARM Architecture Reference Manual (DDI0487B_a);
1406  * classification names and decode diagrams here should generally
1407  * match up with those in the manual.
1408  */
1409 
1410 static bool trans_B(DisasContext *s, arg_i *a)
1411 {
1412     reset_btype(s);
1413     gen_goto_tb(s, 0, a->imm);
1414     return true;
1415 }
1416 
1417 static bool trans_BL(DisasContext *s, arg_i *a)
1418 {
1419     gen_pc_plus_diff(s, cpu_reg(s, 30), curr_insn_len(s));
1420     reset_btype(s);
1421     gen_goto_tb(s, 0, a->imm);
1422     return true;
1423 }
1424 
1425 
1426 static bool trans_CBZ(DisasContext *s, arg_cbz *a)
1427 {
1428     DisasLabel match;
1429     TCGv_i64 tcg_cmp;
1430 
1431     tcg_cmp = read_cpu_reg(s, a->rt, a->sf);
1432     reset_btype(s);
1433 
1434     match = gen_disas_label(s);
1435     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1436                         tcg_cmp, 0, match.label);
1437     gen_goto_tb(s, 0, 4);
1438     set_disas_label(s, match);
1439     gen_goto_tb(s, 1, a->imm);
1440     return true;
1441 }
1442 
1443 static bool trans_TBZ(DisasContext *s, arg_tbz *a)
1444 {
1445     DisasLabel match;
1446     TCGv_i64 tcg_cmp;
1447 
1448     tcg_cmp = tcg_temp_new_i64();
1449     tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, a->rt), 1ULL << a->bitpos);
1450 
1451     reset_btype(s);
1452 
1453     match = gen_disas_label(s);
1454     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1455                         tcg_cmp, 0, match.label);
1456     gen_goto_tb(s, 0, 4);
1457     set_disas_label(s, match);
1458     gen_goto_tb(s, 1, a->imm);
1459     return true;
1460 }
1461 
1462 static bool trans_B_cond(DisasContext *s, arg_B_cond *a)
1463 {
1464     /* BC.cond is only present with FEAT_HBC */
1465     if (a->c && !dc_isar_feature(aa64_hbc, s)) {
1466         return false;
1467     }
1468     reset_btype(s);
1469     if (a->cond < 0x0e) {
1470         /* genuinely conditional branches */
1471         DisasLabel match = gen_disas_label(s);
1472         arm_gen_test_cc(a->cond, match.label);
1473         gen_goto_tb(s, 0, 4);
1474         set_disas_label(s, match);
1475         gen_goto_tb(s, 1, a->imm);
1476     } else {
1477         /* 0xe and 0xf are both "always" conditions */
1478         gen_goto_tb(s, 0, a->imm);
1479     }
1480     return true;
1481 }
1482 
1483 static void set_btype_for_br(DisasContext *s, int rn)
1484 {
1485     if (dc_isar_feature(aa64_bti, s)) {
1486         /* BR to {x16,x17} or !guard -> 1, else 3.  */
1487         set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
1488     }
1489 }
1490 
1491 static void set_btype_for_blr(DisasContext *s)
1492 {
1493     if (dc_isar_feature(aa64_bti, s)) {
1494         /* BLR sets BTYPE to 2, regardless of source guarded page.  */
1495         set_btype(s, 2);
1496     }
1497 }
1498 
1499 static bool trans_BR(DisasContext *s, arg_r *a)
1500 {
1501     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1502     set_btype_for_br(s, a->rn);
1503     s->base.is_jmp = DISAS_JUMP;
1504     return true;
1505 }
1506 
1507 static bool trans_BLR(DisasContext *s, arg_r *a)
1508 {
1509     TCGv_i64 dst = cpu_reg(s, a->rn);
1510     TCGv_i64 lr = cpu_reg(s, 30);
1511     if (dst == lr) {
1512         TCGv_i64 tmp = tcg_temp_new_i64();
1513         tcg_gen_mov_i64(tmp, dst);
1514         dst = tmp;
1515     }
1516     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1517     gen_a64_set_pc(s, dst);
1518     set_btype_for_blr(s);
1519     s->base.is_jmp = DISAS_JUMP;
1520     return true;
1521 }
1522 
1523 static bool trans_RET(DisasContext *s, arg_r *a)
1524 {
1525     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1526     s->base.is_jmp = DISAS_JUMP;
1527     return true;
1528 }
1529 
1530 static TCGv_i64 auth_branch_target(DisasContext *s, TCGv_i64 dst,
1531                                    TCGv_i64 modifier, bool use_key_a)
1532 {
1533     TCGv_i64 truedst;
1534     /*
1535      * Return the branch target for a BRAA/RETA/etc, which is either
1536      * just the destination dst, or that value with the pauth check
1537      * done and the code removed from the high bits.
1538      */
1539     if (!s->pauth_active) {
1540         return dst;
1541     }
1542 
1543     truedst = tcg_temp_new_i64();
1544     if (use_key_a) {
1545         gen_helper_autia_combined(truedst, tcg_env, dst, modifier);
1546     } else {
1547         gen_helper_autib_combined(truedst, tcg_env, dst, modifier);
1548     }
1549     return truedst;
1550 }
1551 
1552 static bool trans_BRAZ(DisasContext *s, arg_braz *a)
1553 {
1554     TCGv_i64 dst;
1555 
1556     if (!dc_isar_feature(aa64_pauth, s)) {
1557         return false;
1558     }
1559 
1560     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1561     gen_a64_set_pc(s, dst);
1562     set_btype_for_br(s, a->rn);
1563     s->base.is_jmp = DISAS_JUMP;
1564     return true;
1565 }
1566 
1567 static bool trans_BLRAZ(DisasContext *s, arg_braz *a)
1568 {
1569     TCGv_i64 dst, lr;
1570 
1571     if (!dc_isar_feature(aa64_pauth, s)) {
1572         return false;
1573     }
1574 
1575     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1576     lr = cpu_reg(s, 30);
1577     if (dst == lr) {
1578         TCGv_i64 tmp = tcg_temp_new_i64();
1579         tcg_gen_mov_i64(tmp, dst);
1580         dst = tmp;
1581     }
1582     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1583     gen_a64_set_pc(s, dst);
1584     set_btype_for_blr(s);
1585     s->base.is_jmp = DISAS_JUMP;
1586     return true;
1587 }
1588 
1589 static bool trans_RETA(DisasContext *s, arg_reta *a)
1590 {
1591     TCGv_i64 dst;
1592 
1593     dst = auth_branch_target(s, cpu_reg(s, 30), cpu_X[31], !a->m);
1594     gen_a64_set_pc(s, dst);
1595     s->base.is_jmp = DISAS_JUMP;
1596     return true;
1597 }
1598 
1599 static bool trans_BRA(DisasContext *s, arg_bra *a)
1600 {
1601     TCGv_i64 dst;
1602 
1603     if (!dc_isar_feature(aa64_pauth, s)) {
1604         return false;
1605     }
1606     dst = auth_branch_target(s, cpu_reg(s,a->rn), cpu_reg_sp(s, a->rm), !a->m);
1607     gen_a64_set_pc(s, dst);
1608     set_btype_for_br(s, a->rn);
1609     s->base.is_jmp = DISAS_JUMP;
1610     return true;
1611 }
1612 
1613 static bool trans_BLRA(DisasContext *s, arg_bra *a)
1614 {
1615     TCGv_i64 dst, lr;
1616 
1617     if (!dc_isar_feature(aa64_pauth, s)) {
1618         return false;
1619     }
1620     dst = auth_branch_target(s, cpu_reg(s, a->rn), cpu_reg_sp(s, a->rm), !a->m);
1621     lr = cpu_reg(s, 30);
1622     if (dst == lr) {
1623         TCGv_i64 tmp = tcg_temp_new_i64();
1624         tcg_gen_mov_i64(tmp, dst);
1625         dst = tmp;
1626     }
1627     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1628     gen_a64_set_pc(s, dst);
1629     set_btype_for_blr(s);
1630     s->base.is_jmp = DISAS_JUMP;
1631     return true;
1632 }
1633 
1634 static bool trans_ERET(DisasContext *s, arg_ERET *a)
1635 {
1636     TCGv_i64 dst;
1637 
1638     if (s->current_el == 0) {
1639         return false;
1640     }
1641     if (s->fgt_eret) {
1642         gen_exception_insn_el(s, 0, EXCP_UDEF, 0, 2);
1643         return true;
1644     }
1645     dst = tcg_temp_new_i64();
1646     tcg_gen_ld_i64(dst, tcg_env,
1647                    offsetof(CPUARMState, elr_el[s->current_el]));
1648 
1649     translator_io_start(&s->base);
1650 
1651     gen_helper_exception_return(tcg_env, dst);
1652     /* Must exit loop to check un-masked IRQs */
1653     s->base.is_jmp = DISAS_EXIT;
1654     return true;
1655 }
1656 
1657 static bool trans_ERETA(DisasContext *s, arg_reta *a)
1658 {
1659     TCGv_i64 dst;
1660 
1661     if (!dc_isar_feature(aa64_pauth, s)) {
1662         return false;
1663     }
1664     if (s->current_el == 0) {
1665         return false;
1666     }
1667     /* The FGT trap takes precedence over an auth trap. */
1668     if (s->fgt_eret) {
1669         gen_exception_insn_el(s, 0, EXCP_UDEF, a->m ? 3 : 2, 2);
1670         return true;
1671     }
1672     dst = tcg_temp_new_i64();
1673     tcg_gen_ld_i64(dst, tcg_env,
1674                    offsetof(CPUARMState, elr_el[s->current_el]));
1675 
1676     dst = auth_branch_target(s, dst, cpu_X[31], !a->m);
1677 
1678     translator_io_start(&s->base);
1679 
1680     gen_helper_exception_return(tcg_env, dst);
1681     /* Must exit loop to check un-masked IRQs */
1682     s->base.is_jmp = DISAS_EXIT;
1683     return true;
1684 }
1685 
1686 static bool trans_NOP(DisasContext *s, arg_NOP *a)
1687 {
1688     return true;
1689 }
1690 
1691 static bool trans_YIELD(DisasContext *s, arg_YIELD *a)
1692 {
1693     /*
1694      * When running in MTTCG we don't generate jumps to the yield and
1695      * WFE helpers as it won't affect the scheduling of other vCPUs.
1696      * If we wanted to more completely model WFE/SEV so we don't busy
1697      * spin unnecessarily we would need to do something more involved.
1698      */
1699     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1700         s->base.is_jmp = DISAS_YIELD;
1701     }
1702     return true;
1703 }
1704 
1705 static bool trans_WFI(DisasContext *s, arg_WFI *a)
1706 {
1707     s->base.is_jmp = DISAS_WFI;
1708     return true;
1709 }
1710 
1711 static bool trans_WFE(DisasContext *s, arg_WFI *a)
1712 {
1713     /*
1714      * When running in MTTCG we don't generate jumps to the yield and
1715      * WFE helpers as it won't affect the scheduling of other vCPUs.
1716      * If we wanted to more completely model WFE/SEV so we don't busy
1717      * spin unnecessarily we would need to do something more involved.
1718      */
1719     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1720         s->base.is_jmp = DISAS_WFE;
1721     }
1722     return true;
1723 }
1724 
1725 static bool trans_XPACLRI(DisasContext *s, arg_XPACLRI *a)
1726 {
1727     if (s->pauth_active) {
1728         gen_helper_xpaci(cpu_X[30], tcg_env, cpu_X[30]);
1729     }
1730     return true;
1731 }
1732 
1733 static bool trans_PACIA1716(DisasContext *s, arg_PACIA1716 *a)
1734 {
1735     if (s->pauth_active) {
1736         gen_helper_pacia(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1737     }
1738     return true;
1739 }
1740 
1741 static bool trans_PACIB1716(DisasContext *s, arg_PACIB1716 *a)
1742 {
1743     if (s->pauth_active) {
1744         gen_helper_pacib(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1745     }
1746     return true;
1747 }
1748 
1749 static bool trans_AUTIA1716(DisasContext *s, arg_AUTIA1716 *a)
1750 {
1751     if (s->pauth_active) {
1752         gen_helper_autia(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1753     }
1754     return true;
1755 }
1756 
1757 static bool trans_AUTIB1716(DisasContext *s, arg_AUTIB1716 *a)
1758 {
1759     if (s->pauth_active) {
1760         gen_helper_autib(cpu_X[17], tcg_env, cpu_X[17], cpu_X[16]);
1761     }
1762     return true;
1763 }
1764 
1765 static bool trans_ESB(DisasContext *s, arg_ESB *a)
1766 {
1767     /* Without RAS, we must implement this as NOP. */
1768     if (dc_isar_feature(aa64_ras, s)) {
1769         /*
1770          * QEMU does not have a source of physical SErrors,
1771          * so we are only concerned with virtual SErrors.
1772          * The pseudocode in the ARM for this case is
1773          *   if PSTATE.EL IN {EL0, EL1} && EL2Enabled() then
1774          *      AArch64.vESBOperation();
1775          * Most of the condition can be evaluated at translation time.
1776          * Test for EL2 present, and defer test for SEL2 to runtime.
1777          */
1778         if (s->current_el <= 1 && arm_dc_feature(s, ARM_FEATURE_EL2)) {
1779             gen_helper_vesb(tcg_env);
1780         }
1781     }
1782     return true;
1783 }
1784 
1785 static bool trans_PACIAZ(DisasContext *s, arg_PACIAZ *a)
1786 {
1787     if (s->pauth_active) {
1788         gen_helper_pacia(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1789     }
1790     return true;
1791 }
1792 
1793 static bool trans_PACIASP(DisasContext *s, arg_PACIASP *a)
1794 {
1795     if (s->pauth_active) {
1796         gen_helper_pacia(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1797     }
1798     return true;
1799 }
1800 
1801 static bool trans_PACIBZ(DisasContext *s, arg_PACIBZ *a)
1802 {
1803     if (s->pauth_active) {
1804         gen_helper_pacib(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1805     }
1806     return true;
1807 }
1808 
1809 static bool trans_PACIBSP(DisasContext *s, arg_PACIBSP *a)
1810 {
1811     if (s->pauth_active) {
1812         gen_helper_pacib(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1813     }
1814     return true;
1815 }
1816 
1817 static bool trans_AUTIAZ(DisasContext *s, arg_AUTIAZ *a)
1818 {
1819     if (s->pauth_active) {
1820         gen_helper_autia(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1821     }
1822     return true;
1823 }
1824 
1825 static bool trans_AUTIASP(DisasContext *s, arg_AUTIASP *a)
1826 {
1827     if (s->pauth_active) {
1828         gen_helper_autia(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1829     }
1830     return true;
1831 }
1832 
1833 static bool trans_AUTIBZ(DisasContext *s, arg_AUTIBZ *a)
1834 {
1835     if (s->pauth_active) {
1836         gen_helper_autib(cpu_X[30], tcg_env, cpu_X[30], tcg_constant_i64(0));
1837     }
1838     return true;
1839 }
1840 
1841 static bool trans_AUTIBSP(DisasContext *s, arg_AUTIBSP *a)
1842 {
1843     if (s->pauth_active) {
1844         gen_helper_autib(cpu_X[30], tcg_env, cpu_X[30], cpu_X[31]);
1845     }
1846     return true;
1847 }
1848 
1849 static bool trans_CLREX(DisasContext *s, arg_CLREX *a)
1850 {
1851     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1852     return true;
1853 }
1854 
1855 static bool trans_DSB_DMB(DisasContext *s, arg_DSB_DMB *a)
1856 {
1857     /* We handle DSB and DMB the same way */
1858     TCGBar bar;
1859 
1860     switch (a->types) {
1861     case 1: /* MBReqTypes_Reads */
1862         bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1863         break;
1864     case 2: /* MBReqTypes_Writes */
1865         bar = TCG_BAR_SC | TCG_MO_ST_ST;
1866         break;
1867     default: /* MBReqTypes_All */
1868         bar = TCG_BAR_SC | TCG_MO_ALL;
1869         break;
1870     }
1871     tcg_gen_mb(bar);
1872     return true;
1873 }
1874 
1875 static bool trans_ISB(DisasContext *s, arg_ISB *a)
1876 {
1877     /*
1878      * We need to break the TB after this insn to execute
1879      * self-modifying code correctly and also to take
1880      * any pending interrupts immediately.
1881      */
1882     reset_btype(s);
1883     gen_goto_tb(s, 0, 4);
1884     return true;
1885 }
1886 
1887 static bool trans_SB(DisasContext *s, arg_SB *a)
1888 {
1889     if (!dc_isar_feature(aa64_sb, s)) {
1890         return false;
1891     }
1892     /*
1893      * TODO: There is no speculation barrier opcode for TCG;
1894      * MB and end the TB instead.
1895      */
1896     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1897     gen_goto_tb(s, 0, 4);
1898     return true;
1899 }
1900 
1901 static bool trans_CFINV(DisasContext *s, arg_CFINV *a)
1902 {
1903     if (!dc_isar_feature(aa64_condm_4, s)) {
1904         return false;
1905     }
1906     tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1907     return true;
1908 }
1909 
1910 static bool trans_XAFLAG(DisasContext *s, arg_XAFLAG *a)
1911 {
1912     TCGv_i32 z;
1913 
1914     if (!dc_isar_feature(aa64_condm_5, s)) {
1915         return false;
1916     }
1917 
1918     z = tcg_temp_new_i32();
1919 
1920     tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1921 
1922     /*
1923      * (!C & !Z) << 31
1924      * (!(C | Z)) << 31
1925      * ~((C | Z) << 31)
1926      * ~-(C | Z)
1927      * (C | Z) - 1
1928      */
1929     tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1930     tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1931 
1932     /* !(Z & C) */
1933     tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1934     tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1935 
1936     /* (!C & Z) << 31 -> -(Z & ~C) */
1937     tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1938     tcg_gen_neg_i32(cpu_VF, cpu_VF);
1939 
1940     /* C | Z */
1941     tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1942 
1943     return true;
1944 }
1945 
1946 static bool trans_AXFLAG(DisasContext *s, arg_AXFLAG *a)
1947 {
1948     if (!dc_isar_feature(aa64_condm_5, s)) {
1949         return false;
1950     }
1951 
1952     tcg_gen_sari_i32(cpu_VF, cpu_VF, 31);         /* V ? -1 : 0 */
1953     tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF);     /* C & !V */
1954 
1955     /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1956     tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1957 
1958     tcg_gen_movi_i32(cpu_NF, 0);
1959     tcg_gen_movi_i32(cpu_VF, 0);
1960 
1961     return true;
1962 }
1963 
1964 static bool trans_MSR_i_UAO(DisasContext *s, arg_i *a)
1965 {
1966     if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1967         return false;
1968     }
1969     if (a->imm & 1) {
1970         set_pstate_bits(PSTATE_UAO);
1971     } else {
1972         clear_pstate_bits(PSTATE_UAO);
1973     }
1974     gen_rebuild_hflags(s);
1975     s->base.is_jmp = DISAS_TOO_MANY;
1976     return true;
1977 }
1978 
1979 static bool trans_MSR_i_PAN(DisasContext *s, arg_i *a)
1980 {
1981     if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1982         return false;
1983     }
1984     if (a->imm & 1) {
1985         set_pstate_bits(PSTATE_PAN);
1986     } else {
1987         clear_pstate_bits(PSTATE_PAN);
1988     }
1989     gen_rebuild_hflags(s);
1990     s->base.is_jmp = DISAS_TOO_MANY;
1991     return true;
1992 }
1993 
1994 static bool trans_MSR_i_SPSEL(DisasContext *s, arg_i *a)
1995 {
1996     if (s->current_el == 0) {
1997         return false;
1998     }
1999     gen_helper_msr_i_spsel(tcg_env, tcg_constant_i32(a->imm & PSTATE_SP));
2000     s->base.is_jmp = DISAS_TOO_MANY;
2001     return true;
2002 }
2003 
2004 static bool trans_MSR_i_SBSS(DisasContext *s, arg_i *a)
2005 {
2006     if (!dc_isar_feature(aa64_ssbs, s)) {
2007         return false;
2008     }
2009     if (a->imm & 1) {
2010         set_pstate_bits(PSTATE_SSBS);
2011     } else {
2012         clear_pstate_bits(PSTATE_SSBS);
2013     }
2014     /* Don't need to rebuild hflags since SSBS is a nop */
2015     s->base.is_jmp = DISAS_TOO_MANY;
2016     return true;
2017 }
2018 
2019 static bool trans_MSR_i_DIT(DisasContext *s, arg_i *a)
2020 {
2021     if (!dc_isar_feature(aa64_dit, s)) {
2022         return false;
2023     }
2024     if (a->imm & 1) {
2025         set_pstate_bits(PSTATE_DIT);
2026     } else {
2027         clear_pstate_bits(PSTATE_DIT);
2028     }
2029     /* There's no need to rebuild hflags because DIT is a nop */
2030     s->base.is_jmp = DISAS_TOO_MANY;
2031     return true;
2032 }
2033 
2034 static bool trans_MSR_i_TCO(DisasContext *s, arg_i *a)
2035 {
2036     if (dc_isar_feature(aa64_mte, s)) {
2037         /* Full MTE is enabled -- set the TCO bit as directed. */
2038         if (a->imm & 1) {
2039             set_pstate_bits(PSTATE_TCO);
2040         } else {
2041             clear_pstate_bits(PSTATE_TCO);
2042         }
2043         gen_rebuild_hflags(s);
2044         /* Many factors, including TCO, go into MTE_ACTIVE. */
2045         s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
2046         return true;
2047     } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
2048         /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI.  */
2049         return true;
2050     } else {
2051         /* Insn not present */
2052         return false;
2053     }
2054 }
2055 
2056 static bool trans_MSR_i_DAIFSET(DisasContext *s, arg_i *a)
2057 {
2058     gen_helper_msr_i_daifset(tcg_env, tcg_constant_i32(a->imm));
2059     s->base.is_jmp = DISAS_TOO_MANY;
2060     return true;
2061 }
2062 
2063 static bool trans_MSR_i_DAIFCLEAR(DisasContext *s, arg_i *a)
2064 {
2065     gen_helper_msr_i_daifclear(tcg_env, tcg_constant_i32(a->imm));
2066     /* Exit the cpu loop to re-evaluate pending IRQs. */
2067     s->base.is_jmp = DISAS_UPDATE_EXIT;
2068     return true;
2069 }
2070 
2071 static bool trans_MSR_i_SVCR(DisasContext *s, arg_MSR_i_SVCR *a)
2072 {
2073     if (!dc_isar_feature(aa64_sme, s) || a->mask == 0) {
2074         return false;
2075     }
2076     if (sme_access_check(s)) {
2077         int old = s->pstate_sm | (s->pstate_za << 1);
2078         int new = a->imm * 3;
2079 
2080         if ((old ^ new) & a->mask) {
2081             /* At least one bit changes. */
2082             gen_helper_set_svcr(tcg_env, tcg_constant_i32(new),
2083                                 tcg_constant_i32(a->mask));
2084             s->base.is_jmp = DISAS_TOO_MANY;
2085         }
2086     }
2087     return true;
2088 }
2089 
2090 static void gen_get_nzcv(TCGv_i64 tcg_rt)
2091 {
2092     TCGv_i32 tmp = tcg_temp_new_i32();
2093     TCGv_i32 nzcv = tcg_temp_new_i32();
2094 
2095     /* build bit 31, N */
2096     tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
2097     /* build bit 30, Z */
2098     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
2099     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
2100     /* build bit 29, C */
2101     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
2102     /* build bit 28, V */
2103     tcg_gen_shri_i32(tmp, cpu_VF, 31);
2104     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
2105     /* generate result */
2106     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
2107 }
2108 
2109 static void gen_set_nzcv(TCGv_i64 tcg_rt)
2110 {
2111     TCGv_i32 nzcv = tcg_temp_new_i32();
2112 
2113     /* take NZCV from R[t] */
2114     tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
2115 
2116     /* bit 31, N */
2117     tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
2118     /* bit 30, Z */
2119     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
2120     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
2121     /* bit 29, C */
2122     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
2123     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
2124     /* bit 28, V */
2125     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
2126     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
2127 }
2128 
2129 static void gen_sysreg_undef(DisasContext *s, bool isread,
2130                              uint8_t op0, uint8_t op1, uint8_t op2,
2131                              uint8_t crn, uint8_t crm, uint8_t rt)
2132 {
2133     /*
2134      * Generate code to emit an UNDEF with correct syndrome
2135      * information for a failed system register access.
2136      * This is EC_UNCATEGORIZED (ie a standard UNDEF) in most cases,
2137      * but if FEAT_IDST is implemented then read accesses to registers
2138      * in the feature ID space are reported with the EC_SYSTEMREGISTERTRAP
2139      * syndrome.
2140      */
2141     uint32_t syndrome;
2142 
2143     if (isread && dc_isar_feature(aa64_ids, s) &&
2144         arm_cpreg_encoding_in_idspace(op0, op1, op2, crn, crm)) {
2145         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2146     } else {
2147         syndrome = syn_uncategorized();
2148     }
2149     gen_exception_insn(s, 0, EXCP_UDEF, syndrome);
2150 }
2151 
2152 /* MRS - move from system register
2153  * MSR (register) - move to system register
2154  * SYS
2155  * SYSL
2156  * These are all essentially the same insn in 'read' and 'write'
2157  * versions, with varying op0 fields.
2158  */
2159 static void handle_sys(DisasContext *s, bool isread,
2160                        unsigned int op0, unsigned int op1, unsigned int op2,
2161                        unsigned int crn, unsigned int crm, unsigned int rt)
2162 {
2163     uint32_t key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
2164                                       crn, crm, op0, op1, op2);
2165     const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
2166     bool need_exit_tb = false;
2167     TCGv_ptr tcg_ri = NULL;
2168     TCGv_i64 tcg_rt;
2169     uint32_t syndrome;
2170 
2171     if (crn == 11 || crn == 15) {
2172         /*
2173          * Check for TIDCP trap, which must take precedence over
2174          * the UNDEF for "no such register" etc.
2175          */
2176         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2177         switch (s->current_el) {
2178         case 0:
2179             if (dc_isar_feature(aa64_tidcp1, s)) {
2180                 gen_helper_tidcp_el0(tcg_env, tcg_constant_i32(syndrome));
2181             }
2182             break;
2183         case 1:
2184             gen_helper_tidcp_el1(tcg_env, tcg_constant_i32(syndrome));
2185             break;
2186         }
2187     }
2188 
2189     if (!ri) {
2190         /* Unknown register; this might be a guest error or a QEMU
2191          * unimplemented feature.
2192          */
2193         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
2194                       "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
2195                       isread ? "read" : "write", op0, op1, crn, crm, op2);
2196         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2197         return;
2198     }
2199 
2200     /* Check access permissions */
2201     if (!cp_access_ok(s->current_el, ri, isread)) {
2202         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2203         return;
2204     }
2205 
2206     if (ri->accessfn || (ri->fgt && s->fgt_active)) {
2207         /* Emit code to perform further access permissions checks at
2208          * runtime; this may result in an exception.
2209          */
2210         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2211         gen_a64_update_pc(s, 0);
2212         tcg_ri = tcg_temp_new_ptr();
2213         gen_helper_access_check_cp_reg(tcg_ri, tcg_env,
2214                                        tcg_constant_i32(key),
2215                                        tcg_constant_i32(syndrome),
2216                                        tcg_constant_i32(isread));
2217     } else if (ri->type & ARM_CP_RAISES_EXC) {
2218         /*
2219          * The readfn or writefn might raise an exception;
2220          * synchronize the CPU state in case it does.
2221          */
2222         gen_a64_update_pc(s, 0);
2223     }
2224 
2225     /* Handle special cases first */
2226     switch (ri->type & ARM_CP_SPECIAL_MASK) {
2227     case 0:
2228         break;
2229     case ARM_CP_NOP:
2230         return;
2231     case ARM_CP_NZCV:
2232         tcg_rt = cpu_reg(s, rt);
2233         if (isread) {
2234             gen_get_nzcv(tcg_rt);
2235         } else {
2236             gen_set_nzcv(tcg_rt);
2237         }
2238         return;
2239     case ARM_CP_CURRENTEL:
2240         /* Reads as current EL value from pstate, which is
2241          * guaranteed to be constant by the tb flags.
2242          */
2243         tcg_rt = cpu_reg(s, rt);
2244         tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
2245         return;
2246     case ARM_CP_DC_ZVA:
2247         /* Writes clear the aligned block of memory which rt points into. */
2248         if (s->mte_active[0]) {
2249             int desc = 0;
2250 
2251             desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
2252             desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
2253             desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
2254 
2255             tcg_rt = tcg_temp_new_i64();
2256             gen_helper_mte_check_zva(tcg_rt, tcg_env,
2257                                      tcg_constant_i32(desc), cpu_reg(s, rt));
2258         } else {
2259             tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
2260         }
2261         gen_helper_dc_zva(tcg_env, tcg_rt);
2262         return;
2263     case ARM_CP_DC_GVA:
2264         {
2265             TCGv_i64 clean_addr, tag;
2266 
2267             /*
2268              * DC_GVA, like DC_ZVA, requires that we supply the original
2269              * pointer for an invalid page.  Probe that address first.
2270              */
2271             tcg_rt = cpu_reg(s, rt);
2272             clean_addr = clean_data_tbi(s, tcg_rt);
2273             gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
2274 
2275             if (s->ata[0]) {
2276                 /* Extract the tag from the register to match STZGM.  */
2277                 tag = tcg_temp_new_i64();
2278                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2279                 gen_helper_stzgm_tags(tcg_env, clean_addr, tag);
2280             }
2281         }
2282         return;
2283     case ARM_CP_DC_GZVA:
2284         {
2285             TCGv_i64 clean_addr, tag;
2286 
2287             /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
2288             tcg_rt = cpu_reg(s, rt);
2289             clean_addr = clean_data_tbi(s, tcg_rt);
2290             gen_helper_dc_zva(tcg_env, clean_addr);
2291 
2292             if (s->ata[0]) {
2293                 /* Extract the tag from the register to match STZGM.  */
2294                 tag = tcg_temp_new_i64();
2295                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2296                 gen_helper_stzgm_tags(tcg_env, clean_addr, tag);
2297             }
2298         }
2299         return;
2300     default:
2301         g_assert_not_reached();
2302     }
2303     if ((ri->type & ARM_CP_FPU) && !fp_access_check_only(s)) {
2304         return;
2305     } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
2306         return;
2307     } else if ((ri->type & ARM_CP_SME) && !sme_access_check(s)) {
2308         return;
2309     }
2310 
2311     if (ri->type & ARM_CP_IO) {
2312         /* I/O operations must end the TB here (whether read or write) */
2313         need_exit_tb = translator_io_start(&s->base);
2314     }
2315 
2316     tcg_rt = cpu_reg(s, rt);
2317 
2318     if (isread) {
2319         if (ri->type & ARM_CP_CONST) {
2320             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
2321         } else if (ri->readfn) {
2322             if (!tcg_ri) {
2323                 tcg_ri = gen_lookup_cp_reg(key);
2324             }
2325             gen_helper_get_cp_reg64(tcg_rt, tcg_env, tcg_ri);
2326         } else {
2327             tcg_gen_ld_i64(tcg_rt, tcg_env, ri->fieldoffset);
2328         }
2329     } else {
2330         if (ri->type & ARM_CP_CONST) {
2331             /* If not forbidden by access permissions, treat as WI */
2332             return;
2333         } else if (ri->writefn) {
2334             if (!tcg_ri) {
2335                 tcg_ri = gen_lookup_cp_reg(key);
2336             }
2337             gen_helper_set_cp_reg64(tcg_env, tcg_ri, tcg_rt);
2338         } else {
2339             tcg_gen_st_i64(tcg_rt, tcg_env, ri->fieldoffset);
2340         }
2341     }
2342 
2343     if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
2344         /*
2345          * A write to any coprocessor register that ends a TB
2346          * must rebuild the hflags for the next TB.
2347          */
2348         gen_rebuild_hflags(s);
2349         /*
2350          * We default to ending the TB on a coprocessor register write,
2351          * but allow this to be suppressed by the register definition
2352          * (usually only necessary to work around guest bugs).
2353          */
2354         need_exit_tb = true;
2355     }
2356     if (need_exit_tb) {
2357         s->base.is_jmp = DISAS_UPDATE_EXIT;
2358     }
2359 }
2360 
2361 static bool trans_SYS(DisasContext *s, arg_SYS *a)
2362 {
2363     handle_sys(s, a->l, a->op0, a->op1, a->op2, a->crn, a->crm, a->rt);
2364     return true;
2365 }
2366 
2367 static bool trans_SVC(DisasContext *s, arg_i *a)
2368 {
2369     /*
2370      * For SVC, HVC and SMC we advance the single-step state
2371      * machine before taking the exception. This is architecturally
2372      * mandated, to ensure that single-stepping a system call
2373      * instruction works properly.
2374      */
2375     uint32_t syndrome = syn_aa64_svc(a->imm);
2376     if (s->fgt_svc) {
2377         gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2378         return true;
2379     }
2380     gen_ss_advance(s);
2381     gen_exception_insn(s, 4, EXCP_SWI, syndrome);
2382     return true;
2383 }
2384 
2385 static bool trans_HVC(DisasContext *s, arg_i *a)
2386 {
2387     if (s->current_el == 0) {
2388         unallocated_encoding(s);
2389         return true;
2390     }
2391     /*
2392      * The pre HVC helper handles cases when HVC gets trapped
2393      * as an undefined insn by runtime configuration.
2394      */
2395     gen_a64_update_pc(s, 0);
2396     gen_helper_pre_hvc(tcg_env);
2397     /* Architecture requires ss advance before we do the actual work */
2398     gen_ss_advance(s);
2399     gen_exception_insn_el(s, 4, EXCP_HVC, syn_aa64_hvc(a->imm), 2);
2400     return true;
2401 }
2402 
2403 static bool trans_SMC(DisasContext *s, arg_i *a)
2404 {
2405     if (s->current_el == 0) {
2406         unallocated_encoding(s);
2407         return true;
2408     }
2409     gen_a64_update_pc(s, 0);
2410     gen_helper_pre_smc(tcg_env, tcg_constant_i32(syn_aa64_smc(a->imm)));
2411     /* Architecture requires ss advance before we do the actual work */
2412     gen_ss_advance(s);
2413     gen_exception_insn_el(s, 4, EXCP_SMC, syn_aa64_smc(a->imm), 3);
2414     return true;
2415 }
2416 
2417 static bool trans_BRK(DisasContext *s, arg_i *a)
2418 {
2419     gen_exception_bkpt_insn(s, syn_aa64_bkpt(a->imm));
2420     return true;
2421 }
2422 
2423 static bool trans_HLT(DisasContext *s, arg_i *a)
2424 {
2425     /*
2426      * HLT. This has two purposes.
2427      * Architecturally, it is an external halting debug instruction.
2428      * Since QEMU doesn't implement external debug, we treat this as
2429      * it is required for halting debug disabled: it will UNDEF.
2430      * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2431      */
2432     if (semihosting_enabled(s->current_el == 0) && a->imm == 0xf000) {
2433         gen_exception_internal_insn(s, EXCP_SEMIHOST);
2434     } else {
2435         unallocated_encoding(s);
2436     }
2437     return true;
2438 }
2439 
2440 /*
2441  * Load/Store exclusive instructions are implemented by remembering
2442  * the value/address loaded, and seeing if these are the same
2443  * when the store is performed. This is not actually the architecturally
2444  * mandated semantics, but it works for typical guest code sequences
2445  * and avoids having to monitor regular stores.
2446  *
2447  * The store exclusive uses the atomic cmpxchg primitives to avoid
2448  * races in multi-threaded linux-user and when MTTCG softmmu is
2449  * enabled.
2450  */
2451 static void gen_load_exclusive(DisasContext *s, int rt, int rt2, int rn,
2452                                int size, bool is_pair)
2453 {
2454     int idx = get_mem_index(s);
2455     TCGv_i64 dirty_addr, clean_addr;
2456     MemOp memop = check_atomic_align(s, rn, size + is_pair);
2457 
2458     s->is_ldex = true;
2459     dirty_addr = cpu_reg_sp(s, rn);
2460     clean_addr = gen_mte_check1(s, dirty_addr, false, rn != 31, memop);
2461 
2462     g_assert(size <= 3);
2463     if (is_pair) {
2464         g_assert(size >= 2);
2465         if (size == 2) {
2466             tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2467             if (s->be_data == MO_LE) {
2468                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2469                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2470             } else {
2471                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2472                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2473             }
2474         } else {
2475             TCGv_i128 t16 = tcg_temp_new_i128();
2476 
2477             tcg_gen_qemu_ld_i128(t16, clean_addr, idx, memop);
2478 
2479             if (s->be_data == MO_LE) {
2480                 tcg_gen_extr_i128_i64(cpu_exclusive_val,
2481                                       cpu_exclusive_high, t16);
2482             } else {
2483                 tcg_gen_extr_i128_i64(cpu_exclusive_high,
2484                                       cpu_exclusive_val, t16);
2485             }
2486             tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2487             tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2488         }
2489     } else {
2490         tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2491         tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2492     }
2493     tcg_gen_mov_i64(cpu_exclusive_addr, clean_addr);
2494 }
2495 
2496 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2497                                 int rn, int size, int is_pair)
2498 {
2499     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2500      *     && (!is_pair || env->exclusive_high == [addr + datasize])) {
2501      *     [addr] = {Rt};
2502      *     if (is_pair) {
2503      *         [addr + datasize] = {Rt2};
2504      *     }
2505      *     {Rd} = 0;
2506      * } else {
2507      *     {Rd} = 1;
2508      * }
2509      * env->exclusive_addr = -1;
2510      */
2511     TCGLabel *fail_label = gen_new_label();
2512     TCGLabel *done_label = gen_new_label();
2513     TCGv_i64 tmp, clean_addr;
2514     MemOp memop;
2515 
2516     /*
2517      * FIXME: We are out of spec here.  We have recorded only the address
2518      * from load_exclusive, not the entire range, and we assume that the
2519      * size of the access on both sides match.  The architecture allows the
2520      * store to be smaller than the load, so long as the stored bytes are
2521      * within the range recorded by the load.
2522      */
2523 
2524     /* See AArch64.ExclusiveMonitorsPass() and AArch64.IsExclusiveVA(). */
2525     clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2526     tcg_gen_brcond_i64(TCG_COND_NE, clean_addr, cpu_exclusive_addr, fail_label);
2527 
2528     /*
2529      * The write, and any associated faults, only happen if the virtual
2530      * and physical addresses pass the exclusive monitor check.  These
2531      * faults are exceedingly unlikely, because normally the guest uses
2532      * the exact same address register for the load_exclusive, and we
2533      * would have recognized these faults there.
2534      *
2535      * It is possible to trigger an alignment fault pre-LSE2, e.g. with an
2536      * unaligned 4-byte write within the range of an aligned 8-byte load.
2537      * With LSE2, the store would need to cross a 16-byte boundary when the
2538      * load did not, which would mean the store is outside the range
2539      * recorded for the monitor, which would have failed a corrected monitor
2540      * check above.  For now, we assume no size change and retain the
2541      * MO_ALIGN to let tcg know what we checked in the load_exclusive.
2542      *
2543      * It is possible to trigger an MTE fault, by performing the load with
2544      * a virtual address with a valid tag and performing the store with the
2545      * same virtual address and a different invalid tag.
2546      */
2547     memop = size + is_pair;
2548     if (memop == MO_128 || !dc_isar_feature(aa64_lse2, s)) {
2549         memop |= MO_ALIGN;
2550     }
2551     memop = finalize_memop(s, memop);
2552     gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2553 
2554     tmp = tcg_temp_new_i64();
2555     if (is_pair) {
2556         if (size == 2) {
2557             if (s->be_data == MO_LE) {
2558                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2559             } else {
2560                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2561             }
2562             tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2563                                        cpu_exclusive_val, tmp,
2564                                        get_mem_index(s), memop);
2565             tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2566         } else {
2567             TCGv_i128 t16 = tcg_temp_new_i128();
2568             TCGv_i128 c16 = tcg_temp_new_i128();
2569             TCGv_i64 a, b;
2570 
2571             if (s->be_data == MO_LE) {
2572                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt), cpu_reg(s, rt2));
2573                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_val,
2574                                         cpu_exclusive_high);
2575             } else {
2576                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt2), cpu_reg(s, rt));
2577                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_high,
2578                                         cpu_exclusive_val);
2579             }
2580 
2581             tcg_gen_atomic_cmpxchg_i128(t16, cpu_exclusive_addr, c16, t16,
2582                                         get_mem_index(s), memop);
2583 
2584             a = tcg_temp_new_i64();
2585             b = tcg_temp_new_i64();
2586             if (s->be_data == MO_LE) {
2587                 tcg_gen_extr_i128_i64(a, b, t16);
2588             } else {
2589                 tcg_gen_extr_i128_i64(b, a, t16);
2590             }
2591 
2592             tcg_gen_xor_i64(a, a, cpu_exclusive_val);
2593             tcg_gen_xor_i64(b, b, cpu_exclusive_high);
2594             tcg_gen_or_i64(tmp, a, b);
2595 
2596             tcg_gen_setcondi_i64(TCG_COND_NE, tmp, tmp, 0);
2597         }
2598     } else {
2599         tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2600                                    cpu_reg(s, rt), get_mem_index(s), memop);
2601         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2602     }
2603     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2604     tcg_gen_br(done_label);
2605 
2606     gen_set_label(fail_label);
2607     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2608     gen_set_label(done_label);
2609     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2610 }
2611 
2612 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2613                                  int rn, int size)
2614 {
2615     TCGv_i64 tcg_rs = cpu_reg(s, rs);
2616     TCGv_i64 tcg_rt = cpu_reg(s, rt);
2617     int memidx = get_mem_index(s);
2618     TCGv_i64 clean_addr;
2619     MemOp memop;
2620 
2621     if (rn == 31) {
2622         gen_check_sp_alignment(s);
2623     }
2624     memop = check_atomic_align(s, rn, size);
2625     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2626     tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt,
2627                                memidx, memop);
2628 }
2629 
2630 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2631                                       int rn, int size)
2632 {
2633     TCGv_i64 s1 = cpu_reg(s, rs);
2634     TCGv_i64 s2 = cpu_reg(s, rs + 1);
2635     TCGv_i64 t1 = cpu_reg(s, rt);
2636     TCGv_i64 t2 = cpu_reg(s, rt + 1);
2637     TCGv_i64 clean_addr;
2638     int memidx = get_mem_index(s);
2639     MemOp memop;
2640 
2641     if (rn == 31) {
2642         gen_check_sp_alignment(s);
2643     }
2644 
2645     /* This is a single atomic access, despite the "pair". */
2646     memop = check_atomic_align(s, rn, size + 1);
2647     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2648 
2649     if (size == 2) {
2650         TCGv_i64 cmp = tcg_temp_new_i64();
2651         TCGv_i64 val = tcg_temp_new_i64();
2652 
2653         if (s->be_data == MO_LE) {
2654             tcg_gen_concat32_i64(val, t1, t2);
2655             tcg_gen_concat32_i64(cmp, s1, s2);
2656         } else {
2657             tcg_gen_concat32_i64(val, t2, t1);
2658             tcg_gen_concat32_i64(cmp, s2, s1);
2659         }
2660 
2661         tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx, memop);
2662 
2663         if (s->be_data == MO_LE) {
2664             tcg_gen_extr32_i64(s1, s2, cmp);
2665         } else {
2666             tcg_gen_extr32_i64(s2, s1, cmp);
2667         }
2668     } else {
2669         TCGv_i128 cmp = tcg_temp_new_i128();
2670         TCGv_i128 val = tcg_temp_new_i128();
2671 
2672         if (s->be_data == MO_LE) {
2673             tcg_gen_concat_i64_i128(val, t1, t2);
2674             tcg_gen_concat_i64_i128(cmp, s1, s2);
2675         } else {
2676             tcg_gen_concat_i64_i128(val, t2, t1);
2677             tcg_gen_concat_i64_i128(cmp, s2, s1);
2678         }
2679 
2680         tcg_gen_atomic_cmpxchg_i128(cmp, clean_addr, cmp, val, memidx, memop);
2681 
2682         if (s->be_data == MO_LE) {
2683             tcg_gen_extr_i128_i64(s1, s2, cmp);
2684         } else {
2685             tcg_gen_extr_i128_i64(s2, s1, cmp);
2686         }
2687     }
2688 }
2689 
2690 /*
2691  * Compute the ISS.SF bit for syndrome information if an exception
2692  * is taken on a load or store. This indicates whether the instruction
2693  * is accessing a 32-bit or 64-bit register. This logic is derived
2694  * from the ARMv8 specs for LDR (Shared decode for all encodings).
2695  */
2696 static bool ldst_iss_sf(int size, bool sign, bool ext)
2697 {
2698 
2699     if (sign) {
2700         /*
2701          * Signed loads are 64 bit results if we are not going to
2702          * do a zero-extend from 32 to 64 after the load.
2703          * (For a store, sign and ext are always false.)
2704          */
2705         return !ext;
2706     } else {
2707         /* Unsigned loads/stores work at the specified size */
2708         return size == MO_64;
2709     }
2710 }
2711 
2712 static bool trans_STXR(DisasContext *s, arg_stxr *a)
2713 {
2714     if (a->rn == 31) {
2715         gen_check_sp_alignment(s);
2716     }
2717     if (a->lasr) {
2718         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2719     }
2720     gen_store_exclusive(s, a->rs, a->rt, a->rt2, a->rn, a->sz, false);
2721     return true;
2722 }
2723 
2724 static bool trans_LDXR(DisasContext *s, arg_stxr *a)
2725 {
2726     if (a->rn == 31) {
2727         gen_check_sp_alignment(s);
2728     }
2729     gen_load_exclusive(s, a->rt, a->rt2, a->rn, a->sz, false);
2730     if (a->lasr) {
2731         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2732     }
2733     return true;
2734 }
2735 
2736 static bool trans_STLR(DisasContext *s, arg_stlr *a)
2737 {
2738     TCGv_i64 clean_addr;
2739     MemOp memop;
2740     bool iss_sf = ldst_iss_sf(a->sz, false, false);
2741 
2742     /*
2743      * StoreLORelease is the same as Store-Release for QEMU, but
2744      * needs the feature-test.
2745      */
2746     if (!a->lasr && !dc_isar_feature(aa64_lor, s)) {
2747         return false;
2748     }
2749     /* Generate ISS for non-exclusive accesses including LASR.  */
2750     if (a->rn == 31) {
2751         gen_check_sp_alignment(s);
2752     }
2753     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2754     memop = check_ordered_align(s, a->rn, 0, true, a->sz);
2755     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn),
2756                                 true, a->rn != 31, memop);
2757     do_gpr_st(s, cpu_reg(s, a->rt), clean_addr, memop, true, a->rt,
2758               iss_sf, a->lasr);
2759     return true;
2760 }
2761 
2762 static bool trans_LDAR(DisasContext *s, arg_stlr *a)
2763 {
2764     TCGv_i64 clean_addr;
2765     MemOp memop;
2766     bool iss_sf = ldst_iss_sf(a->sz, false, false);
2767 
2768     /* LoadLOAcquire is the same as Load-Acquire for QEMU.  */
2769     if (!a->lasr && !dc_isar_feature(aa64_lor, s)) {
2770         return false;
2771     }
2772     /* Generate ISS for non-exclusive accesses including LASR.  */
2773     if (a->rn == 31) {
2774         gen_check_sp_alignment(s);
2775     }
2776     memop = check_ordered_align(s, a->rn, 0, false, a->sz);
2777     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn),
2778                                 false, a->rn != 31, memop);
2779     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, memop, false, true,
2780               a->rt, iss_sf, a->lasr);
2781     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2782     return true;
2783 }
2784 
2785 static bool trans_STXP(DisasContext *s, arg_stxr *a)
2786 {
2787     if (a->rn == 31) {
2788         gen_check_sp_alignment(s);
2789     }
2790     if (a->lasr) {
2791         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2792     }
2793     gen_store_exclusive(s, a->rs, a->rt, a->rt2, a->rn, a->sz, true);
2794     return true;
2795 }
2796 
2797 static bool trans_LDXP(DisasContext *s, arg_stxr *a)
2798 {
2799     if (a->rn == 31) {
2800         gen_check_sp_alignment(s);
2801     }
2802     gen_load_exclusive(s, a->rt, a->rt2, a->rn, a->sz, true);
2803     if (a->lasr) {
2804         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2805     }
2806     return true;
2807 }
2808 
2809 static bool trans_CASP(DisasContext *s, arg_CASP *a)
2810 {
2811     if (!dc_isar_feature(aa64_atomics, s)) {
2812         return false;
2813     }
2814     if (((a->rt | a->rs) & 1) != 0) {
2815         return false;
2816     }
2817 
2818     gen_compare_and_swap_pair(s, a->rs, a->rt, a->rn, a->sz);
2819     return true;
2820 }
2821 
2822 static bool trans_CAS(DisasContext *s, arg_CAS *a)
2823 {
2824     if (!dc_isar_feature(aa64_atomics, s)) {
2825         return false;
2826     }
2827     gen_compare_and_swap(s, a->rs, a->rt, a->rn, a->sz);
2828     return true;
2829 }
2830 
2831 static bool trans_LD_lit(DisasContext *s, arg_ldlit *a)
2832 {
2833     bool iss_sf = ldst_iss_sf(a->sz, a->sign, false);
2834     TCGv_i64 tcg_rt = cpu_reg(s, a->rt);
2835     TCGv_i64 clean_addr = tcg_temp_new_i64();
2836     MemOp memop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
2837 
2838     gen_pc_plus_diff(s, clean_addr, a->imm);
2839     do_gpr_ld(s, tcg_rt, clean_addr, memop,
2840               false, true, a->rt, iss_sf, false);
2841     return true;
2842 }
2843 
2844 static bool trans_LD_lit_v(DisasContext *s, arg_ldlit *a)
2845 {
2846     /* Load register (literal), vector version */
2847     TCGv_i64 clean_addr;
2848     MemOp memop;
2849 
2850     if (!fp_access_check(s)) {
2851         return true;
2852     }
2853     memop = finalize_memop_asimd(s, a->sz);
2854     clean_addr = tcg_temp_new_i64();
2855     gen_pc_plus_diff(s, clean_addr, a->imm);
2856     do_fp_ld(s, a->rt, clean_addr, memop);
2857     return true;
2858 }
2859 
2860 static void op_addr_ldstpair_pre(DisasContext *s, arg_ldstpair *a,
2861                                  TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
2862                                  uint64_t offset, bool is_store, MemOp mop)
2863 {
2864     if (a->rn == 31) {
2865         gen_check_sp_alignment(s);
2866     }
2867 
2868     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
2869     if (!a->p) {
2870         tcg_gen_addi_i64(*dirty_addr, *dirty_addr, offset);
2871     }
2872 
2873     *clean_addr = gen_mte_checkN(s, *dirty_addr, is_store,
2874                                  (a->w || a->rn != 31), 2 << a->sz, mop);
2875 }
2876 
2877 static void op_addr_ldstpair_post(DisasContext *s, arg_ldstpair *a,
2878                                   TCGv_i64 dirty_addr, uint64_t offset)
2879 {
2880     if (a->w) {
2881         if (a->p) {
2882             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2883         }
2884         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
2885     }
2886 }
2887 
2888 static bool trans_STP(DisasContext *s, arg_ldstpair *a)
2889 {
2890     uint64_t offset = a->imm << a->sz;
2891     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
2892     MemOp mop = finalize_memop(s, a->sz);
2893 
2894     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, true, mop);
2895     tcg_rt = cpu_reg(s, a->rt);
2896     tcg_rt2 = cpu_reg(s, a->rt2);
2897     /*
2898      * We built mop above for the single logical access -- rebuild it
2899      * now for the paired operation.
2900      *
2901      * With LSE2, non-sign-extending pairs are treated atomically if
2902      * aligned, and if unaligned one of the pair will be completely
2903      * within a 16-byte block and that element will be atomic.
2904      * Otherwise each element is separately atomic.
2905      * In all cases, issue one operation with the correct atomicity.
2906      */
2907     mop = a->sz + 1;
2908     if (s->align_mem) {
2909         mop |= (a->sz == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
2910     }
2911     mop = finalize_memop_pair(s, mop);
2912     if (a->sz == 2) {
2913         TCGv_i64 tmp = tcg_temp_new_i64();
2914 
2915         if (s->be_data == MO_LE) {
2916             tcg_gen_concat32_i64(tmp, tcg_rt, tcg_rt2);
2917         } else {
2918             tcg_gen_concat32_i64(tmp, tcg_rt2, tcg_rt);
2919         }
2920         tcg_gen_qemu_st_i64(tmp, clean_addr, get_mem_index(s), mop);
2921     } else {
2922         TCGv_i128 tmp = tcg_temp_new_i128();
2923 
2924         if (s->be_data == MO_LE) {
2925             tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
2926         } else {
2927             tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
2928         }
2929         tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
2930     }
2931     op_addr_ldstpair_post(s, a, dirty_addr, offset);
2932     return true;
2933 }
2934 
2935 static bool trans_LDP(DisasContext *s, arg_ldstpair *a)
2936 {
2937     uint64_t offset = a->imm << a->sz;
2938     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
2939     MemOp mop = finalize_memop(s, a->sz);
2940 
2941     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, false, mop);
2942     tcg_rt = cpu_reg(s, a->rt);
2943     tcg_rt2 = cpu_reg(s, a->rt2);
2944 
2945     /*
2946      * We built mop above for the single logical access -- rebuild it
2947      * now for the paired operation.
2948      *
2949      * With LSE2, non-sign-extending pairs are treated atomically if
2950      * aligned, and if unaligned one of the pair will be completely
2951      * within a 16-byte block and that element will be atomic.
2952      * Otherwise each element is separately atomic.
2953      * In all cases, issue one operation with the correct atomicity.
2954      *
2955      * This treats sign-extending loads like zero-extending loads,
2956      * since that reuses the most code below.
2957      */
2958     mop = a->sz + 1;
2959     if (s->align_mem) {
2960         mop |= (a->sz == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
2961     }
2962     mop = finalize_memop_pair(s, mop);
2963     if (a->sz == 2) {
2964         int o2 = s->be_data == MO_LE ? 32 : 0;
2965         int o1 = o2 ^ 32;
2966 
2967         tcg_gen_qemu_ld_i64(tcg_rt, clean_addr, get_mem_index(s), mop);
2968         if (a->sign) {
2969             tcg_gen_sextract_i64(tcg_rt2, tcg_rt, o2, 32);
2970             tcg_gen_sextract_i64(tcg_rt, tcg_rt, o1, 32);
2971         } else {
2972             tcg_gen_extract_i64(tcg_rt2, tcg_rt, o2, 32);
2973             tcg_gen_extract_i64(tcg_rt, tcg_rt, o1, 32);
2974         }
2975     } else {
2976         TCGv_i128 tmp = tcg_temp_new_i128();
2977 
2978         tcg_gen_qemu_ld_i128(tmp, clean_addr, get_mem_index(s), mop);
2979         if (s->be_data == MO_LE) {
2980             tcg_gen_extr_i128_i64(tcg_rt, tcg_rt2, tmp);
2981         } else {
2982             tcg_gen_extr_i128_i64(tcg_rt2, tcg_rt, tmp);
2983         }
2984     }
2985     op_addr_ldstpair_post(s, a, dirty_addr, offset);
2986     return true;
2987 }
2988 
2989 static bool trans_STP_v(DisasContext *s, arg_ldstpair *a)
2990 {
2991     uint64_t offset = a->imm << a->sz;
2992     TCGv_i64 clean_addr, dirty_addr;
2993     MemOp mop;
2994 
2995     if (!fp_access_check(s)) {
2996         return true;
2997     }
2998 
2999     /* LSE2 does not merge FP pairs; leave these as separate operations. */
3000     mop = finalize_memop_asimd(s, a->sz);
3001     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, true, mop);
3002     do_fp_st(s, a->rt, clean_addr, mop);
3003     tcg_gen_addi_i64(clean_addr, clean_addr, 1 << a->sz);
3004     do_fp_st(s, a->rt2, clean_addr, mop);
3005     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3006     return true;
3007 }
3008 
3009 static bool trans_LDP_v(DisasContext *s, arg_ldstpair *a)
3010 {
3011     uint64_t offset = a->imm << a->sz;
3012     TCGv_i64 clean_addr, dirty_addr;
3013     MemOp mop;
3014 
3015     if (!fp_access_check(s)) {
3016         return true;
3017     }
3018 
3019     /* LSE2 does not merge FP pairs; leave these as separate operations. */
3020     mop = finalize_memop_asimd(s, a->sz);
3021     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, false, mop);
3022     do_fp_ld(s, a->rt, clean_addr, mop);
3023     tcg_gen_addi_i64(clean_addr, clean_addr, 1 << a->sz);
3024     do_fp_ld(s, a->rt2, clean_addr, mop);
3025     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3026     return true;
3027 }
3028 
3029 static bool trans_STGP(DisasContext *s, arg_ldstpair *a)
3030 {
3031     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
3032     uint64_t offset = a->imm << LOG2_TAG_GRANULE;
3033     MemOp mop;
3034     TCGv_i128 tmp;
3035 
3036     /* STGP only comes in one size. */
3037     tcg_debug_assert(a->sz == MO_64);
3038 
3039     if (!dc_isar_feature(aa64_mte_insn_reg, s)) {
3040         return false;
3041     }
3042 
3043     if (a->rn == 31) {
3044         gen_check_sp_alignment(s);
3045     }
3046 
3047     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3048     if (!a->p) {
3049         tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3050     }
3051 
3052     clean_addr = clean_data_tbi(s, dirty_addr);
3053     tcg_rt = cpu_reg(s, a->rt);
3054     tcg_rt2 = cpu_reg(s, a->rt2);
3055 
3056     /*
3057      * STGP is defined as two 8-byte memory operations, aligned to TAG_GRANULE,
3058      * and one tag operation.  We implement it as one single aligned 16-byte
3059      * memory operation for convenience.  Note that the alignment ensures
3060      * MO_ATOM_IFALIGN_PAIR produces 8-byte atomicity for the memory store.
3061      */
3062     mop = finalize_memop_atom(s, MO_128 | MO_ALIGN, MO_ATOM_IFALIGN_PAIR);
3063 
3064     tmp = tcg_temp_new_i128();
3065     if (s->be_data == MO_LE) {
3066         tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
3067     } else {
3068         tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
3069     }
3070     tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
3071 
3072     /* Perform the tag store, if tag access enabled. */
3073     if (s->ata[0]) {
3074         if (tb_cflags(s->base.tb) & CF_PARALLEL) {
3075             gen_helper_stg_parallel(tcg_env, dirty_addr, dirty_addr);
3076         } else {
3077             gen_helper_stg(tcg_env, dirty_addr, dirty_addr);
3078         }
3079     }
3080 
3081     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3082     return true;
3083 }
3084 
3085 static void op_addr_ldst_imm_pre(DisasContext *s, arg_ldst_imm *a,
3086                                  TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
3087                                  uint64_t offset, bool is_store, MemOp mop)
3088 {
3089     int memidx;
3090 
3091     if (a->rn == 31) {
3092         gen_check_sp_alignment(s);
3093     }
3094 
3095     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3096     if (!a->p) {
3097         tcg_gen_addi_i64(*dirty_addr, *dirty_addr, offset);
3098     }
3099     memidx = get_a64_user_mem_index(s, a->unpriv);
3100     *clean_addr = gen_mte_check1_mmuidx(s, *dirty_addr, is_store,
3101                                         a->w || a->rn != 31,
3102                                         mop, a->unpriv, memidx);
3103 }
3104 
3105 static void op_addr_ldst_imm_post(DisasContext *s, arg_ldst_imm *a,
3106                                   TCGv_i64 dirty_addr, uint64_t offset)
3107 {
3108     if (a->w) {
3109         if (a->p) {
3110             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3111         }
3112         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
3113     }
3114 }
3115 
3116 static bool trans_STR_i(DisasContext *s, arg_ldst_imm *a)
3117 {
3118     bool iss_sf, iss_valid = !a->w;
3119     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3120     int memidx = get_a64_user_mem_index(s, a->unpriv);
3121     MemOp mop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3122 
3123     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, true, mop);
3124 
3125     tcg_rt = cpu_reg(s, a->rt);
3126     iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3127 
3128     do_gpr_st_memidx(s, tcg_rt, clean_addr, mop, memidx,
3129                      iss_valid, a->rt, iss_sf, false);
3130     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3131     return true;
3132 }
3133 
3134 static bool trans_LDR_i(DisasContext *s, arg_ldst_imm *a)
3135 {
3136     bool iss_sf, iss_valid = !a->w;
3137     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3138     int memidx = get_a64_user_mem_index(s, a->unpriv);
3139     MemOp mop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3140 
3141     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, false, mop);
3142 
3143     tcg_rt = cpu_reg(s, a->rt);
3144     iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3145 
3146     do_gpr_ld_memidx(s, tcg_rt, clean_addr, mop,
3147                      a->ext, memidx, iss_valid, a->rt, iss_sf, false);
3148     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3149     return true;
3150 }
3151 
3152 static bool trans_STR_v_i(DisasContext *s, arg_ldst_imm *a)
3153 {
3154     TCGv_i64 clean_addr, dirty_addr;
3155     MemOp mop;
3156 
3157     if (!fp_access_check(s)) {
3158         return true;
3159     }
3160     mop = finalize_memop_asimd(s, a->sz);
3161     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, true, mop);
3162     do_fp_st(s, a->rt, clean_addr, mop);
3163     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3164     return true;
3165 }
3166 
3167 static bool trans_LDR_v_i(DisasContext *s, arg_ldst_imm *a)
3168 {
3169     TCGv_i64 clean_addr, dirty_addr;
3170     MemOp mop;
3171 
3172     if (!fp_access_check(s)) {
3173         return true;
3174     }
3175     mop = finalize_memop_asimd(s, a->sz);
3176     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, false, mop);
3177     do_fp_ld(s, a->rt, clean_addr, mop);
3178     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3179     return true;
3180 }
3181 
3182 static void op_addr_ldst_pre(DisasContext *s, arg_ldst *a,
3183                              TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
3184                              bool is_store, MemOp memop)
3185 {
3186     TCGv_i64 tcg_rm;
3187 
3188     if (a->rn == 31) {
3189         gen_check_sp_alignment(s);
3190     }
3191     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3192 
3193     tcg_rm = read_cpu_reg(s, a->rm, 1);
3194     ext_and_shift_reg(tcg_rm, tcg_rm, a->opt, a->s ? a->sz : 0);
3195 
3196     tcg_gen_add_i64(*dirty_addr, *dirty_addr, tcg_rm);
3197     *clean_addr = gen_mte_check1(s, *dirty_addr, is_store, true, memop);
3198 }
3199 
3200 static bool trans_LDR(DisasContext *s, arg_ldst *a)
3201 {
3202     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3203     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3204     MemOp memop;
3205 
3206     if (extract32(a->opt, 1, 1) == 0) {
3207         return false;
3208     }
3209 
3210     memop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3211     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, false, memop);
3212     tcg_rt = cpu_reg(s, a->rt);
3213     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3214               a->ext, true, a->rt, iss_sf, false);
3215     return true;
3216 }
3217 
3218 static bool trans_STR(DisasContext *s, arg_ldst *a)
3219 {
3220     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3221     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3222     MemOp memop;
3223 
3224     if (extract32(a->opt, 1, 1) == 0) {
3225         return false;
3226     }
3227 
3228     memop = finalize_memop(s, a->sz);
3229     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, true, memop);
3230     tcg_rt = cpu_reg(s, a->rt);
3231     do_gpr_st(s, tcg_rt, clean_addr, memop, true, a->rt, iss_sf, false);
3232     return true;
3233 }
3234 
3235 static bool trans_LDR_v(DisasContext *s, arg_ldst *a)
3236 {
3237     TCGv_i64 clean_addr, dirty_addr;
3238     MemOp memop;
3239 
3240     if (extract32(a->opt, 1, 1) == 0) {
3241         return false;
3242     }
3243 
3244     if (!fp_access_check(s)) {
3245         return true;
3246     }
3247 
3248     memop = finalize_memop_asimd(s, a->sz);
3249     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, false, memop);
3250     do_fp_ld(s, a->rt, clean_addr, memop);
3251     return true;
3252 }
3253 
3254 static bool trans_STR_v(DisasContext *s, arg_ldst *a)
3255 {
3256     TCGv_i64 clean_addr, dirty_addr;
3257     MemOp memop;
3258 
3259     if (extract32(a->opt, 1, 1) == 0) {
3260         return false;
3261     }
3262 
3263     if (!fp_access_check(s)) {
3264         return true;
3265     }
3266 
3267     memop = finalize_memop_asimd(s, a->sz);
3268     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, true, memop);
3269     do_fp_st(s, a->rt, clean_addr, memop);
3270     return true;
3271 }
3272 
3273 
3274 static bool do_atomic_ld(DisasContext *s, arg_atomic *a, AtomicThreeOpFn *fn,
3275                          int sign, bool invert)
3276 {
3277     MemOp mop = a->sz | sign;
3278     TCGv_i64 clean_addr, tcg_rs, tcg_rt;
3279 
3280     if (a->rn == 31) {
3281         gen_check_sp_alignment(s);
3282     }
3283     mop = check_atomic_align(s, a->rn, mop);
3284     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn), false,
3285                                 a->rn != 31, mop);
3286     tcg_rs = read_cpu_reg(s, a->rs, true);
3287     tcg_rt = cpu_reg(s, a->rt);
3288     if (invert) {
3289         tcg_gen_not_i64(tcg_rs, tcg_rs);
3290     }
3291     /*
3292      * The tcg atomic primitives are all full barriers.  Therefore we
3293      * can ignore the Acquire and Release bits of this instruction.
3294      */
3295     fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3296 
3297     if (mop & MO_SIGN) {
3298         switch (a->sz) {
3299         case MO_8:
3300             tcg_gen_ext8u_i64(tcg_rt, tcg_rt);
3301             break;
3302         case MO_16:
3303             tcg_gen_ext16u_i64(tcg_rt, tcg_rt);
3304             break;
3305         case MO_32:
3306             tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3307             break;
3308         case MO_64:
3309             break;
3310         default:
3311             g_assert_not_reached();
3312         }
3313     }
3314     return true;
3315 }
3316 
3317 TRANS_FEAT(LDADD, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_add_i64, 0, false)
3318 TRANS_FEAT(LDCLR, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_and_i64, 0, true)
3319 TRANS_FEAT(LDEOR, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_xor_i64, 0, false)
3320 TRANS_FEAT(LDSET, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_or_i64, 0, false)
3321 TRANS_FEAT(LDSMAX, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_smax_i64, MO_SIGN, false)
3322 TRANS_FEAT(LDSMIN, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_smin_i64, MO_SIGN, false)
3323 TRANS_FEAT(LDUMAX, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_umax_i64, 0, false)
3324 TRANS_FEAT(LDUMIN, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_umin_i64, 0, false)
3325 TRANS_FEAT(SWP, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_xchg_i64, 0, false)
3326 
3327 static bool trans_LDAPR(DisasContext *s, arg_LDAPR *a)
3328 {
3329     bool iss_sf = ldst_iss_sf(a->sz, false, false);
3330     TCGv_i64 clean_addr;
3331     MemOp mop;
3332 
3333     if (!dc_isar_feature(aa64_atomics, s) ||
3334         !dc_isar_feature(aa64_rcpc_8_3, s)) {
3335         return false;
3336     }
3337     if (a->rn == 31) {
3338         gen_check_sp_alignment(s);
3339     }
3340     mop = check_atomic_align(s, a->rn, a->sz);
3341     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn), false,
3342                                 a->rn != 31, mop);
3343     /*
3344      * LDAPR* are a special case because they are a simple load, not a
3345      * fetch-and-do-something op.
3346      * The architectural consistency requirements here are weaker than
3347      * full load-acquire (we only need "load-acquire processor consistent"),
3348      * but we choose to implement them as full LDAQ.
3349      */
3350     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, mop, false,
3351               true, a->rt, iss_sf, true);
3352     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3353     return true;
3354 }
3355 
3356 static bool trans_LDRA(DisasContext *s, arg_LDRA *a)
3357 {
3358     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3359     MemOp memop;
3360 
3361     /* Load with pointer authentication */
3362     if (!dc_isar_feature(aa64_pauth, s)) {
3363         return false;
3364     }
3365 
3366     if (a->rn == 31) {
3367         gen_check_sp_alignment(s);
3368     }
3369     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3370 
3371     if (s->pauth_active) {
3372         if (!a->m) {
3373             gen_helper_autda_combined(dirty_addr, tcg_env, dirty_addr,
3374                                       tcg_constant_i64(0));
3375         } else {
3376             gen_helper_autdb_combined(dirty_addr, tcg_env, dirty_addr,
3377                                       tcg_constant_i64(0));
3378         }
3379     }
3380 
3381     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3382 
3383     memop = finalize_memop(s, MO_64);
3384 
3385     /* Note that "clean" and "dirty" here refer to TBI not PAC.  */
3386     clean_addr = gen_mte_check1(s, dirty_addr, false,
3387                                 a->w || a->rn != 31, memop);
3388 
3389     tcg_rt = cpu_reg(s, a->rt);
3390     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3391               /* extend */ false, /* iss_valid */ !a->w,
3392               /* iss_srt */ a->rt, /* iss_sf */ true, /* iss_ar */ false);
3393 
3394     if (a->w) {
3395         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
3396     }
3397     return true;
3398 }
3399 
3400 static bool trans_LDAPR_i(DisasContext *s, arg_ldapr_stlr_i *a)
3401 {
3402     TCGv_i64 clean_addr, dirty_addr;
3403     MemOp mop = a->sz | (a->sign ? MO_SIGN : 0);
3404     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3405 
3406     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3407         return false;
3408     }
3409 
3410     if (a->rn == 31) {
3411         gen_check_sp_alignment(s);
3412     }
3413 
3414     mop = check_ordered_align(s, a->rn, a->imm, false, mop);
3415     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3416     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3417     clean_addr = clean_data_tbi(s, dirty_addr);
3418 
3419     /*
3420      * Load-AcquirePC semantics; we implement as the slightly more
3421      * restrictive Load-Acquire.
3422      */
3423     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, mop, a->ext, true,
3424               a->rt, iss_sf, true);
3425     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3426     return true;
3427 }
3428 
3429 static bool trans_STLR_i(DisasContext *s, arg_ldapr_stlr_i *a)
3430 {
3431     TCGv_i64 clean_addr, dirty_addr;
3432     MemOp mop = a->sz;
3433     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3434 
3435     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3436         return false;
3437     }
3438 
3439     /* TODO: ARMv8.4-LSE SCTLR.nAA */
3440 
3441     if (a->rn == 31) {
3442         gen_check_sp_alignment(s);
3443     }
3444 
3445     mop = check_ordered_align(s, a->rn, a->imm, true, mop);
3446     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3447     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3448     clean_addr = clean_data_tbi(s, dirty_addr);
3449 
3450     /* Store-Release semantics */
3451     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3452     do_gpr_st(s, cpu_reg(s, a->rt), clean_addr, mop, true, a->rt, iss_sf, true);
3453     return true;
3454 }
3455 
3456 static bool trans_LD_mult(DisasContext *s, arg_ldst_mult *a)
3457 {
3458     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3459     MemOp endian, align, mop;
3460 
3461     int total;    /* total bytes */
3462     int elements; /* elements per vector */
3463     int r;
3464     int size = a->sz;
3465 
3466     if (!a->p && a->rm != 0) {
3467         /* For non-postindexed accesses the Rm field must be 0 */
3468         return false;
3469     }
3470     if (size == 3 && !a->q && a->selem != 1) {
3471         return false;
3472     }
3473     if (!fp_access_check(s)) {
3474         return true;
3475     }
3476 
3477     if (a->rn == 31) {
3478         gen_check_sp_alignment(s);
3479     }
3480 
3481     /* For our purposes, bytes are always little-endian.  */
3482     endian = s->be_data;
3483     if (size == 0) {
3484         endian = MO_LE;
3485     }
3486 
3487     total = a->rpt * a->selem * (a->q ? 16 : 8);
3488     tcg_rn = cpu_reg_sp(s, a->rn);
3489 
3490     /*
3491      * Issue the MTE check vs the logical repeat count, before we
3492      * promote consecutive little-endian elements below.
3493      */
3494     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31, total,
3495                                 finalize_memop_asimd(s, size));
3496 
3497     /*
3498      * Consecutive little-endian elements from a single register
3499      * can be promoted to a larger little-endian operation.
3500      */
3501     align = MO_ALIGN;
3502     if (a->selem == 1 && endian == MO_LE) {
3503         align = pow2_align(size);
3504         size = 3;
3505     }
3506     if (!s->align_mem) {
3507         align = 0;
3508     }
3509     mop = endian | size | align;
3510 
3511     elements = (a->q ? 16 : 8) >> size;
3512     tcg_ebytes = tcg_constant_i64(1 << size);
3513     for (r = 0; r < a->rpt; r++) {
3514         int e;
3515         for (e = 0; e < elements; e++) {
3516             int xs;
3517             for (xs = 0; xs < a->selem; xs++) {
3518                 int tt = (a->rt + r + xs) % 32;
3519                 do_vec_ld(s, tt, e, clean_addr, mop);
3520                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3521             }
3522         }
3523     }
3524 
3525     /*
3526      * For non-quad operations, setting a slice of the low 64 bits of
3527      * the register clears the high 64 bits (in the ARM ARM pseudocode
3528      * this is implicit in the fact that 'rval' is a 64 bit wide
3529      * variable).  For quad operations, we might still need to zero
3530      * the high bits of SVE.
3531      */
3532     for (r = 0; r < a->rpt * a->selem; r++) {
3533         int tt = (a->rt + r) % 32;
3534         clear_vec_high(s, a->q, tt);
3535     }
3536 
3537     if (a->p) {
3538         if (a->rm == 31) {
3539             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3540         } else {
3541             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3542         }
3543     }
3544     return true;
3545 }
3546 
3547 static bool trans_ST_mult(DisasContext *s, arg_ldst_mult *a)
3548 {
3549     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3550     MemOp endian, align, mop;
3551 
3552     int total;    /* total bytes */
3553     int elements; /* elements per vector */
3554     int r;
3555     int size = a->sz;
3556 
3557     if (!a->p && a->rm != 0) {
3558         /* For non-postindexed accesses the Rm field must be 0 */
3559         return false;
3560     }
3561     if (size == 3 && !a->q && a->selem != 1) {
3562         return false;
3563     }
3564     if (!fp_access_check(s)) {
3565         return true;
3566     }
3567 
3568     if (a->rn == 31) {
3569         gen_check_sp_alignment(s);
3570     }
3571 
3572     /* For our purposes, bytes are always little-endian.  */
3573     endian = s->be_data;
3574     if (size == 0) {
3575         endian = MO_LE;
3576     }
3577 
3578     total = a->rpt * a->selem * (a->q ? 16 : 8);
3579     tcg_rn = cpu_reg_sp(s, a->rn);
3580 
3581     /*
3582      * Issue the MTE check vs the logical repeat count, before we
3583      * promote consecutive little-endian elements below.
3584      */
3585     clean_addr = gen_mte_checkN(s, tcg_rn, true, a->p || a->rn != 31, total,
3586                                 finalize_memop_asimd(s, size));
3587 
3588     /*
3589      * Consecutive little-endian elements from a single register
3590      * can be promoted to a larger little-endian operation.
3591      */
3592     align = MO_ALIGN;
3593     if (a->selem == 1 && endian == MO_LE) {
3594         align = pow2_align(size);
3595         size = 3;
3596     }
3597     if (!s->align_mem) {
3598         align = 0;
3599     }
3600     mop = endian | size | align;
3601 
3602     elements = (a->q ? 16 : 8) >> size;
3603     tcg_ebytes = tcg_constant_i64(1 << size);
3604     for (r = 0; r < a->rpt; r++) {
3605         int e;
3606         for (e = 0; e < elements; e++) {
3607             int xs;
3608             for (xs = 0; xs < a->selem; xs++) {
3609                 int tt = (a->rt + r + xs) % 32;
3610                 do_vec_st(s, tt, e, clean_addr, mop);
3611                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3612             }
3613         }
3614     }
3615 
3616     if (a->p) {
3617         if (a->rm == 31) {
3618             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3619         } else {
3620             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3621         }
3622     }
3623     return true;
3624 }
3625 
3626 static bool trans_ST_single(DisasContext *s, arg_ldst_single *a)
3627 {
3628     int xs, total, rt;
3629     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3630     MemOp mop;
3631 
3632     if (!a->p && a->rm != 0) {
3633         return false;
3634     }
3635     if (!fp_access_check(s)) {
3636         return true;
3637     }
3638 
3639     if (a->rn == 31) {
3640         gen_check_sp_alignment(s);
3641     }
3642 
3643     total = a->selem << a->scale;
3644     tcg_rn = cpu_reg_sp(s, a->rn);
3645 
3646     mop = finalize_memop_asimd(s, a->scale);
3647     clean_addr = gen_mte_checkN(s, tcg_rn, true, a->p || a->rn != 31,
3648                                 total, mop);
3649 
3650     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3651     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3652         do_vec_st(s, rt, a->index, clean_addr, mop);
3653         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3654     }
3655 
3656     if (a->p) {
3657         if (a->rm == 31) {
3658             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3659         } else {
3660             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3661         }
3662     }
3663     return true;
3664 }
3665 
3666 static bool trans_LD_single(DisasContext *s, arg_ldst_single *a)
3667 {
3668     int xs, total, rt;
3669     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3670     MemOp mop;
3671 
3672     if (!a->p && a->rm != 0) {
3673         return false;
3674     }
3675     if (!fp_access_check(s)) {
3676         return true;
3677     }
3678 
3679     if (a->rn == 31) {
3680         gen_check_sp_alignment(s);
3681     }
3682 
3683     total = a->selem << a->scale;
3684     tcg_rn = cpu_reg_sp(s, a->rn);
3685 
3686     mop = finalize_memop_asimd(s, a->scale);
3687     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31,
3688                                 total, mop);
3689 
3690     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3691     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3692         do_vec_ld(s, rt, a->index, clean_addr, mop);
3693         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3694     }
3695 
3696     if (a->p) {
3697         if (a->rm == 31) {
3698             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3699         } else {
3700             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3701         }
3702     }
3703     return true;
3704 }
3705 
3706 static bool trans_LD_single_repl(DisasContext *s, arg_LD_single_repl *a)
3707 {
3708     int xs, total, rt;
3709     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3710     MemOp mop;
3711 
3712     if (!a->p && a->rm != 0) {
3713         return false;
3714     }
3715     if (!fp_access_check(s)) {
3716         return true;
3717     }
3718 
3719     if (a->rn == 31) {
3720         gen_check_sp_alignment(s);
3721     }
3722 
3723     total = a->selem << a->scale;
3724     tcg_rn = cpu_reg_sp(s, a->rn);
3725 
3726     mop = finalize_memop_asimd(s, a->scale);
3727     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31,
3728                                 total, mop);
3729 
3730     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3731     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3732         /* Load and replicate to all elements */
3733         TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3734 
3735         tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
3736         tcg_gen_gvec_dup_i64(a->scale, vec_full_reg_offset(s, rt),
3737                              (a->q + 1) * 8, vec_full_reg_size(s), tcg_tmp);
3738         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3739     }
3740 
3741     if (a->p) {
3742         if (a->rm == 31) {
3743             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3744         } else {
3745             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3746         }
3747     }
3748     return true;
3749 }
3750 
3751 static bool trans_STZGM(DisasContext *s, arg_ldst_tag *a)
3752 {
3753     TCGv_i64 addr, clean_addr, tcg_rt;
3754     int size = 4 << s->dcz_blocksize;
3755 
3756     if (!dc_isar_feature(aa64_mte, s)) {
3757         return false;
3758     }
3759     if (s->current_el == 0) {
3760         return false;
3761     }
3762 
3763     if (a->rn == 31) {
3764         gen_check_sp_alignment(s);
3765     }
3766 
3767     addr = read_cpu_reg_sp(s, a->rn, true);
3768     tcg_gen_addi_i64(addr, addr, a->imm);
3769     tcg_rt = cpu_reg(s, a->rt);
3770 
3771     if (s->ata[0]) {
3772         gen_helper_stzgm_tags(tcg_env, addr, tcg_rt);
3773     }
3774     /*
3775      * The non-tags portion of STZGM is mostly like DC_ZVA,
3776      * except the alignment happens before the access.
3777      */
3778     clean_addr = clean_data_tbi(s, addr);
3779     tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3780     gen_helper_dc_zva(tcg_env, clean_addr);
3781     return true;
3782 }
3783 
3784 static bool trans_STGM(DisasContext *s, arg_ldst_tag *a)
3785 {
3786     TCGv_i64 addr, clean_addr, tcg_rt;
3787 
3788     if (!dc_isar_feature(aa64_mte, s)) {
3789         return false;
3790     }
3791     if (s->current_el == 0) {
3792         return false;
3793     }
3794 
3795     if (a->rn == 31) {
3796         gen_check_sp_alignment(s);
3797     }
3798 
3799     addr = read_cpu_reg_sp(s, a->rn, true);
3800     tcg_gen_addi_i64(addr, addr, a->imm);
3801     tcg_rt = cpu_reg(s, a->rt);
3802 
3803     if (s->ata[0]) {
3804         gen_helper_stgm(tcg_env, addr, tcg_rt);
3805     } else {
3806         MMUAccessType acc = MMU_DATA_STORE;
3807         int size = 4 << s->gm_blocksize;
3808 
3809         clean_addr = clean_data_tbi(s, addr);
3810         tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3811         gen_probe_access(s, clean_addr, acc, size);
3812     }
3813     return true;
3814 }
3815 
3816 static bool trans_LDGM(DisasContext *s, arg_ldst_tag *a)
3817 {
3818     TCGv_i64 addr, clean_addr, tcg_rt;
3819 
3820     if (!dc_isar_feature(aa64_mte, s)) {
3821         return false;
3822     }
3823     if (s->current_el == 0) {
3824         return false;
3825     }
3826 
3827     if (a->rn == 31) {
3828         gen_check_sp_alignment(s);
3829     }
3830 
3831     addr = read_cpu_reg_sp(s, a->rn, true);
3832     tcg_gen_addi_i64(addr, addr, a->imm);
3833     tcg_rt = cpu_reg(s, a->rt);
3834 
3835     if (s->ata[0]) {
3836         gen_helper_ldgm(tcg_rt, tcg_env, addr);
3837     } else {
3838         MMUAccessType acc = MMU_DATA_LOAD;
3839         int size = 4 << s->gm_blocksize;
3840 
3841         clean_addr = clean_data_tbi(s, addr);
3842         tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3843         gen_probe_access(s, clean_addr, acc, size);
3844         /* The result tags are zeros.  */
3845         tcg_gen_movi_i64(tcg_rt, 0);
3846     }
3847     return true;
3848 }
3849 
3850 static bool trans_LDG(DisasContext *s, arg_ldst_tag *a)
3851 {
3852     TCGv_i64 addr, clean_addr, tcg_rt;
3853 
3854     if (!dc_isar_feature(aa64_mte_insn_reg, s)) {
3855         return false;
3856     }
3857 
3858     if (a->rn == 31) {
3859         gen_check_sp_alignment(s);
3860     }
3861 
3862     addr = read_cpu_reg_sp(s, a->rn, true);
3863     if (!a->p) {
3864         /* pre-index or signed offset */
3865         tcg_gen_addi_i64(addr, addr, a->imm);
3866     }
3867 
3868     tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
3869     tcg_rt = cpu_reg(s, a->rt);
3870     if (s->ata[0]) {
3871         gen_helper_ldg(tcg_rt, tcg_env, addr, tcg_rt);
3872     } else {
3873         /*
3874          * Tag access disabled: we must check for aborts on the load
3875          * load from [rn+offset], and then insert a 0 tag into rt.
3876          */
3877         clean_addr = clean_data_tbi(s, addr);
3878         gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
3879         gen_address_with_allocation_tag0(tcg_rt, tcg_rt);
3880     }
3881 
3882     if (a->w) {
3883         /* pre-index or post-index */
3884         if (a->p) {
3885             /* post-index */
3886             tcg_gen_addi_i64(addr, addr, a->imm);
3887         }
3888         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), addr);
3889     }
3890     return true;
3891 }
3892 
3893 static bool do_STG(DisasContext *s, arg_ldst_tag *a, bool is_zero, bool is_pair)
3894 {
3895     TCGv_i64 addr, tcg_rt;
3896 
3897     if (a->rn == 31) {
3898         gen_check_sp_alignment(s);
3899     }
3900 
3901     addr = read_cpu_reg_sp(s, a->rn, true);
3902     if (!a->p) {
3903         /* pre-index or signed offset */
3904         tcg_gen_addi_i64(addr, addr, a->imm);
3905     }
3906     tcg_rt = cpu_reg_sp(s, a->rt);
3907     if (!s->ata[0]) {
3908         /*
3909          * For STG and ST2G, we need to check alignment and probe memory.
3910          * TODO: For STZG and STZ2G, we could rely on the stores below,
3911          * at least for system mode; user-only won't enforce alignment.
3912          */
3913         if (is_pair) {
3914             gen_helper_st2g_stub(tcg_env, addr);
3915         } else {
3916             gen_helper_stg_stub(tcg_env, addr);
3917         }
3918     } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
3919         if (is_pair) {
3920             gen_helper_st2g_parallel(tcg_env, addr, tcg_rt);
3921         } else {
3922             gen_helper_stg_parallel(tcg_env, addr, tcg_rt);
3923         }
3924     } else {
3925         if (is_pair) {
3926             gen_helper_st2g(tcg_env, addr, tcg_rt);
3927         } else {
3928             gen_helper_stg(tcg_env, addr, tcg_rt);
3929         }
3930     }
3931 
3932     if (is_zero) {
3933         TCGv_i64 clean_addr = clean_data_tbi(s, addr);
3934         TCGv_i64 zero64 = tcg_constant_i64(0);
3935         TCGv_i128 zero128 = tcg_temp_new_i128();
3936         int mem_index = get_mem_index(s);
3937         MemOp mop = finalize_memop(s, MO_128 | MO_ALIGN);
3938 
3939         tcg_gen_concat_i64_i128(zero128, zero64, zero64);
3940 
3941         /* This is 1 or 2 atomic 16-byte operations. */
3942         tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
3943         if (is_pair) {
3944             tcg_gen_addi_i64(clean_addr, clean_addr, 16);
3945             tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
3946         }
3947     }
3948 
3949     if (a->w) {
3950         /* pre-index or post-index */
3951         if (a->p) {
3952             /* post-index */
3953             tcg_gen_addi_i64(addr, addr, a->imm);
3954         }
3955         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), addr);
3956     }
3957     return true;
3958 }
3959 
3960 TRANS_FEAT(STG, aa64_mte_insn_reg, do_STG, a, false, false)
3961 TRANS_FEAT(STZG, aa64_mte_insn_reg, do_STG, a, true, false)
3962 TRANS_FEAT(ST2G, aa64_mte_insn_reg, do_STG, a, false, true)
3963 TRANS_FEAT(STZ2G, aa64_mte_insn_reg, do_STG, a, true, true)
3964 
3965 typedef void SetFn(TCGv_env, TCGv_i32, TCGv_i32);
3966 
3967 static bool do_SET(DisasContext *s, arg_set *a, bool is_epilogue,
3968                    bool is_setg, SetFn fn)
3969 {
3970     int memidx;
3971     uint32_t syndrome, desc = 0;
3972 
3973     if (is_setg && !dc_isar_feature(aa64_mte, s)) {
3974         return false;
3975     }
3976 
3977     /*
3978      * UNPREDICTABLE cases: we choose to UNDEF, which allows
3979      * us to pull this check before the CheckMOPSEnabled() test
3980      * (which we do in the helper function)
3981      */
3982     if (a->rs == a->rn || a->rs == a->rd || a->rn == a->rd ||
3983         a->rd == 31 || a->rn == 31) {
3984         return false;
3985     }
3986 
3987     memidx = get_a64_user_mem_index(s, a->unpriv);
3988 
3989     /*
3990      * We pass option_a == true, matching our implementation;
3991      * we pass wrong_option == false: helper function may set that bit.
3992      */
3993     syndrome = syn_mop(true, is_setg, (a->nontemp << 1) | a->unpriv,
3994                        is_epilogue, false, true, a->rd, a->rs, a->rn);
3995 
3996     if (is_setg ? s->ata[a->unpriv] : s->mte_active[a->unpriv]) {
3997         /* We may need to do MTE tag checking, so assemble the descriptor */
3998         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
3999         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
4000         desc = FIELD_DP32(desc, MTEDESC, WRITE, true);
4001         /* SIZEM1 and ALIGN we leave 0 (byte write) */
4002     }
4003     /* The helper function always needs the memidx even with MTE disabled */
4004     desc = FIELD_DP32(desc, MTEDESC, MIDX, memidx);
4005 
4006     /*
4007      * The helper needs the register numbers, but since they're in
4008      * the syndrome anyway, we let it extract them from there rather
4009      * than passing in an extra three integer arguments.
4010      */
4011     fn(tcg_env, tcg_constant_i32(syndrome), tcg_constant_i32(desc));
4012     return true;
4013 }
4014 
4015 TRANS_FEAT(SETP, aa64_mops, do_SET, a, false, false, gen_helper_setp)
4016 TRANS_FEAT(SETM, aa64_mops, do_SET, a, false, false, gen_helper_setm)
4017 TRANS_FEAT(SETE, aa64_mops, do_SET, a, true, false, gen_helper_sete)
4018 TRANS_FEAT(SETGP, aa64_mops, do_SET, a, false, true, gen_helper_setgp)
4019 TRANS_FEAT(SETGM, aa64_mops, do_SET, a, false, true, gen_helper_setgm)
4020 TRANS_FEAT(SETGE, aa64_mops, do_SET, a, true, true, gen_helper_setge)
4021 
4022 typedef void CpyFn(TCGv_env, TCGv_i32, TCGv_i32, TCGv_i32);
4023 
4024 static bool do_CPY(DisasContext *s, arg_cpy *a, bool is_epilogue, CpyFn fn)
4025 {
4026     int rmemidx, wmemidx;
4027     uint32_t syndrome, rdesc = 0, wdesc = 0;
4028     bool wunpriv = extract32(a->options, 0, 1);
4029     bool runpriv = extract32(a->options, 1, 1);
4030 
4031     /*
4032      * UNPREDICTABLE cases: we choose to UNDEF, which allows
4033      * us to pull this check before the CheckMOPSEnabled() test
4034      * (which we do in the helper function)
4035      */
4036     if (a->rs == a->rn || a->rs == a->rd || a->rn == a->rd ||
4037         a->rd == 31 || a->rs == 31 || a->rn == 31) {
4038         return false;
4039     }
4040 
4041     rmemidx = get_a64_user_mem_index(s, runpriv);
4042     wmemidx = get_a64_user_mem_index(s, wunpriv);
4043 
4044     /*
4045      * We pass option_a == true, matching our implementation;
4046      * we pass wrong_option == false: helper function may set that bit.
4047      */
4048     syndrome = syn_mop(false, false, a->options, is_epilogue,
4049                        false, true, a->rd, a->rs, a->rn);
4050 
4051     /* If we need to do MTE tag checking, assemble the descriptors */
4052     if (s->mte_active[runpriv]) {
4053         rdesc = FIELD_DP32(rdesc, MTEDESC, TBI, s->tbid);
4054         rdesc = FIELD_DP32(rdesc, MTEDESC, TCMA, s->tcma);
4055     }
4056     if (s->mte_active[wunpriv]) {
4057         wdesc = FIELD_DP32(wdesc, MTEDESC, TBI, s->tbid);
4058         wdesc = FIELD_DP32(wdesc, MTEDESC, TCMA, s->tcma);
4059         wdesc = FIELD_DP32(wdesc, MTEDESC, WRITE, true);
4060     }
4061     /* The helper function needs these parts of the descriptor regardless */
4062     rdesc = FIELD_DP32(rdesc, MTEDESC, MIDX, rmemidx);
4063     wdesc = FIELD_DP32(wdesc, MTEDESC, MIDX, wmemidx);
4064 
4065     /*
4066      * The helper needs the register numbers, but since they're in
4067      * the syndrome anyway, we let it extract them from there rather
4068      * than passing in an extra three integer arguments.
4069      */
4070     fn(tcg_env, tcg_constant_i32(syndrome), tcg_constant_i32(wdesc),
4071        tcg_constant_i32(rdesc));
4072     return true;
4073 }
4074 
4075 TRANS_FEAT(CPYP, aa64_mops, do_CPY, a, false, gen_helper_cpyp)
4076 TRANS_FEAT(CPYM, aa64_mops, do_CPY, a, false, gen_helper_cpym)
4077 TRANS_FEAT(CPYE, aa64_mops, do_CPY, a, true, gen_helper_cpye)
4078 TRANS_FEAT(CPYFP, aa64_mops, do_CPY, a, false, gen_helper_cpyfp)
4079 TRANS_FEAT(CPYFM, aa64_mops, do_CPY, a, false, gen_helper_cpyfm)
4080 TRANS_FEAT(CPYFE, aa64_mops, do_CPY, a, true, gen_helper_cpyfe)
4081 
4082 typedef void ArithTwoOp(TCGv_i64, TCGv_i64, TCGv_i64);
4083 
4084 static bool gen_rri(DisasContext *s, arg_rri_sf *a,
4085                     bool rd_sp, bool rn_sp, ArithTwoOp *fn)
4086 {
4087     TCGv_i64 tcg_rn = rn_sp ? cpu_reg_sp(s, a->rn) : cpu_reg(s, a->rn);
4088     TCGv_i64 tcg_rd = rd_sp ? cpu_reg_sp(s, a->rd) : cpu_reg(s, a->rd);
4089     TCGv_i64 tcg_imm = tcg_constant_i64(a->imm);
4090 
4091     fn(tcg_rd, tcg_rn, tcg_imm);
4092     if (!a->sf) {
4093         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4094     }
4095     return true;
4096 }
4097 
4098 /*
4099  * PC-rel. addressing
4100  */
4101 
4102 static bool trans_ADR(DisasContext *s, arg_ri *a)
4103 {
4104     gen_pc_plus_diff(s, cpu_reg(s, a->rd), a->imm);
4105     return true;
4106 }
4107 
4108 static bool trans_ADRP(DisasContext *s, arg_ri *a)
4109 {
4110     int64_t offset = (int64_t)a->imm << 12;
4111 
4112     /* The page offset is ok for CF_PCREL. */
4113     offset -= s->pc_curr & 0xfff;
4114     gen_pc_plus_diff(s, cpu_reg(s, a->rd), offset);
4115     return true;
4116 }
4117 
4118 /*
4119  * Add/subtract (immediate)
4120  */
4121 TRANS(ADD_i, gen_rri, a, 1, 1, tcg_gen_add_i64)
4122 TRANS(SUB_i, gen_rri, a, 1, 1, tcg_gen_sub_i64)
4123 TRANS(ADDS_i, gen_rri, a, 0, 1, a->sf ? gen_add64_CC : gen_add32_CC)
4124 TRANS(SUBS_i, gen_rri, a, 0, 1, a->sf ? gen_sub64_CC : gen_sub32_CC)
4125 
4126 /*
4127  * Add/subtract (immediate, with tags)
4128  */
4129 
4130 static bool gen_add_sub_imm_with_tags(DisasContext *s, arg_rri_tag *a,
4131                                       bool sub_op)
4132 {
4133     TCGv_i64 tcg_rn, tcg_rd;
4134     int imm;
4135 
4136     imm = a->uimm6 << LOG2_TAG_GRANULE;
4137     if (sub_op) {
4138         imm = -imm;
4139     }
4140 
4141     tcg_rn = cpu_reg_sp(s, a->rn);
4142     tcg_rd = cpu_reg_sp(s, a->rd);
4143 
4144     if (s->ata[0]) {
4145         gen_helper_addsubg(tcg_rd, tcg_env, tcg_rn,
4146                            tcg_constant_i32(imm),
4147                            tcg_constant_i32(a->uimm4));
4148     } else {
4149         tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4150         gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4151     }
4152     return true;
4153 }
4154 
4155 TRANS_FEAT(ADDG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, false)
4156 TRANS_FEAT(SUBG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, true)
4157 
4158 /* The input should be a value in the bottom e bits (with higher
4159  * bits zero); returns that value replicated into every element
4160  * of size e in a 64 bit integer.
4161  */
4162 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4163 {
4164     assert(e != 0);
4165     while (e < 64) {
4166         mask |= mask << e;
4167         e *= 2;
4168     }
4169     return mask;
4170 }
4171 
4172 /*
4173  * Logical (immediate)
4174  */
4175 
4176 /*
4177  * Simplified variant of pseudocode DecodeBitMasks() for the case where we
4178  * only require the wmask. Returns false if the imms/immr/immn are a reserved
4179  * value (ie should cause a guest UNDEF exception), and true if they are
4180  * valid, in which case the decoded bit pattern is written to result.
4181  */
4182 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4183                             unsigned int imms, unsigned int immr)
4184 {
4185     uint64_t mask;
4186     unsigned e, levels, s, r;
4187     int len;
4188 
4189     assert(immn < 2 && imms < 64 && immr < 64);
4190 
4191     /* The bit patterns we create here are 64 bit patterns which
4192      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4193      * 64 bits each. Each element contains the same value: a run
4194      * of between 1 and e-1 non-zero bits, rotated within the
4195      * element by between 0 and e-1 bits.
4196      *
4197      * The element size and run length are encoded into immn (1 bit)
4198      * and imms (6 bits) as follows:
4199      * 64 bit elements: immn = 1, imms = <length of run - 1>
4200      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4201      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4202      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4203      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4204      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4205      * Notice that immn = 0, imms = 11111x is the only combination
4206      * not covered by one of the above options; this is reserved.
4207      * Further, <length of run - 1> all-ones is a reserved pattern.
4208      *
4209      * In all cases the rotation is by immr % e (and immr is 6 bits).
4210      */
4211 
4212     /* First determine the element size */
4213     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4214     if (len < 1) {
4215         /* This is the immn == 0, imms == 0x11111x case */
4216         return false;
4217     }
4218     e = 1 << len;
4219 
4220     levels = e - 1;
4221     s = imms & levels;
4222     r = immr & levels;
4223 
4224     if (s == levels) {
4225         /* <length of run - 1> mustn't be all-ones. */
4226         return false;
4227     }
4228 
4229     /* Create the value of one element: s+1 set bits rotated
4230      * by r within the element (which is e bits wide)...
4231      */
4232     mask = MAKE_64BIT_MASK(0, s + 1);
4233     if (r) {
4234         mask = (mask >> r) | (mask << (e - r));
4235         mask &= MAKE_64BIT_MASK(0, e);
4236     }
4237     /* ...then replicate the element over the whole 64 bit value */
4238     mask = bitfield_replicate(mask, e);
4239     *result = mask;
4240     return true;
4241 }
4242 
4243 static bool gen_rri_log(DisasContext *s, arg_rri_log *a, bool set_cc,
4244                         void (*fn)(TCGv_i64, TCGv_i64, int64_t))
4245 {
4246     TCGv_i64 tcg_rd, tcg_rn;
4247     uint64_t imm;
4248 
4249     /* Some immediate field values are reserved. */
4250     if (!logic_imm_decode_wmask(&imm, extract32(a->dbm, 12, 1),
4251                                 extract32(a->dbm, 0, 6),
4252                                 extract32(a->dbm, 6, 6))) {
4253         return false;
4254     }
4255     if (!a->sf) {
4256         imm &= 0xffffffffull;
4257     }
4258 
4259     tcg_rd = set_cc ? cpu_reg(s, a->rd) : cpu_reg_sp(s, a->rd);
4260     tcg_rn = cpu_reg(s, a->rn);
4261 
4262     fn(tcg_rd, tcg_rn, imm);
4263     if (set_cc) {
4264         gen_logic_CC(a->sf, tcg_rd);
4265     }
4266     if (!a->sf) {
4267         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4268     }
4269     return true;
4270 }
4271 
4272 TRANS(AND_i, gen_rri_log, a, false, tcg_gen_andi_i64)
4273 TRANS(ORR_i, gen_rri_log, a, false, tcg_gen_ori_i64)
4274 TRANS(EOR_i, gen_rri_log, a, false, tcg_gen_xori_i64)
4275 TRANS(ANDS_i, gen_rri_log, a, true, tcg_gen_andi_i64)
4276 
4277 /*
4278  * Move wide (immediate)
4279  */
4280 
4281 static bool trans_MOVZ(DisasContext *s, arg_movw *a)
4282 {
4283     int pos = a->hw << 4;
4284     tcg_gen_movi_i64(cpu_reg(s, a->rd), (uint64_t)a->imm << pos);
4285     return true;
4286 }
4287 
4288 static bool trans_MOVN(DisasContext *s, arg_movw *a)
4289 {
4290     int pos = a->hw << 4;
4291     uint64_t imm = a->imm;
4292 
4293     imm = ~(imm << pos);
4294     if (!a->sf) {
4295         imm = (uint32_t)imm;
4296     }
4297     tcg_gen_movi_i64(cpu_reg(s, a->rd), imm);
4298     return true;
4299 }
4300 
4301 static bool trans_MOVK(DisasContext *s, arg_movw *a)
4302 {
4303     int pos = a->hw << 4;
4304     TCGv_i64 tcg_rd, tcg_im;
4305 
4306     tcg_rd = cpu_reg(s, a->rd);
4307     tcg_im = tcg_constant_i64(a->imm);
4308     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_im, pos, 16);
4309     if (!a->sf) {
4310         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4311     }
4312     return true;
4313 }
4314 
4315 /*
4316  * Bitfield
4317  */
4318 
4319 static bool trans_SBFM(DisasContext *s, arg_SBFM *a)
4320 {
4321     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4322     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4323     unsigned int bitsize = a->sf ? 64 : 32;
4324     unsigned int ri = a->immr;
4325     unsigned int si = a->imms;
4326     unsigned int pos, len;
4327 
4328     if (si >= ri) {
4329         /* Wd<s-r:0> = Wn<s:r> */
4330         len = (si - ri) + 1;
4331         tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4332         if (!a->sf) {
4333             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4334         }
4335     } else {
4336         /* Wd<32+s-r,32-r> = Wn<s:0> */
4337         len = si + 1;
4338         pos = (bitsize - ri) & (bitsize - 1);
4339 
4340         if (len < ri) {
4341             /*
4342              * Sign extend the destination field from len to fill the
4343              * balance of the word.  Let the deposit below insert all
4344              * of those sign bits.
4345              */
4346             tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4347             len = ri;
4348         }
4349 
4350         /*
4351          * We start with zero, and we haven't modified any bits outside
4352          * bitsize, therefore no final zero-extension is unneeded for !sf.
4353          */
4354         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4355     }
4356     return true;
4357 }
4358 
4359 static bool trans_UBFM(DisasContext *s, arg_UBFM *a)
4360 {
4361     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4362     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4363     unsigned int bitsize = a->sf ? 64 : 32;
4364     unsigned int ri = a->immr;
4365     unsigned int si = a->imms;
4366     unsigned int pos, len;
4367 
4368     tcg_rd = cpu_reg(s, a->rd);
4369     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4370 
4371     if (si >= ri) {
4372         /* Wd<s-r:0> = Wn<s:r> */
4373         len = (si - ri) + 1;
4374         tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4375     } else {
4376         /* Wd<32+s-r,32-r> = Wn<s:0> */
4377         len = si + 1;
4378         pos = (bitsize - ri) & (bitsize - 1);
4379         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4380     }
4381     return true;
4382 }
4383 
4384 static bool trans_BFM(DisasContext *s, arg_BFM *a)
4385 {
4386     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4387     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4388     unsigned int bitsize = a->sf ? 64 : 32;
4389     unsigned int ri = a->immr;
4390     unsigned int si = a->imms;
4391     unsigned int pos, len;
4392 
4393     tcg_rd = cpu_reg(s, a->rd);
4394     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4395 
4396     if (si >= ri) {
4397         /* Wd<s-r:0> = Wn<s:r> */
4398         tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4399         len = (si - ri) + 1;
4400         pos = 0;
4401     } else {
4402         /* Wd<32+s-r,32-r> = Wn<s:0> */
4403         len = si + 1;
4404         pos = (bitsize - ri) & (bitsize - 1);
4405     }
4406 
4407     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4408     if (!a->sf) {
4409         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4410     }
4411     return true;
4412 }
4413 
4414 static bool trans_EXTR(DisasContext *s, arg_extract *a)
4415 {
4416     TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4417 
4418     tcg_rd = cpu_reg(s, a->rd);
4419 
4420     if (unlikely(a->imm == 0)) {
4421         /*
4422          * tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4423          * so an extract from bit 0 is a special case.
4424          */
4425         if (a->sf) {
4426             tcg_gen_mov_i64(tcg_rd, cpu_reg(s, a->rm));
4427         } else {
4428             tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, a->rm));
4429         }
4430     } else {
4431         tcg_rm = cpu_reg(s, a->rm);
4432         tcg_rn = cpu_reg(s, a->rn);
4433 
4434         if (a->sf) {
4435             /* Specialization to ROR happens in EXTRACT2.  */
4436             tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, a->imm);
4437         } else {
4438             TCGv_i32 t0 = tcg_temp_new_i32();
4439 
4440             tcg_gen_extrl_i64_i32(t0, tcg_rm);
4441             if (a->rm == a->rn) {
4442                 tcg_gen_rotri_i32(t0, t0, a->imm);
4443             } else {
4444                 TCGv_i32 t1 = tcg_temp_new_i32();
4445                 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4446                 tcg_gen_extract2_i32(t0, t0, t1, a->imm);
4447             }
4448             tcg_gen_extu_i32_i64(tcg_rd, t0);
4449         }
4450     }
4451     return true;
4452 }
4453 
4454 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4455  * Note that it is the caller's responsibility to ensure that the
4456  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4457  * mandated semantics for out of range shifts.
4458  */
4459 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4460                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4461 {
4462     switch (shift_type) {
4463     case A64_SHIFT_TYPE_LSL:
4464         tcg_gen_shl_i64(dst, src, shift_amount);
4465         break;
4466     case A64_SHIFT_TYPE_LSR:
4467         tcg_gen_shr_i64(dst, src, shift_amount);
4468         break;
4469     case A64_SHIFT_TYPE_ASR:
4470         if (!sf) {
4471             tcg_gen_ext32s_i64(dst, src);
4472         }
4473         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4474         break;
4475     case A64_SHIFT_TYPE_ROR:
4476         if (sf) {
4477             tcg_gen_rotr_i64(dst, src, shift_amount);
4478         } else {
4479             TCGv_i32 t0, t1;
4480             t0 = tcg_temp_new_i32();
4481             t1 = tcg_temp_new_i32();
4482             tcg_gen_extrl_i64_i32(t0, src);
4483             tcg_gen_extrl_i64_i32(t1, shift_amount);
4484             tcg_gen_rotr_i32(t0, t0, t1);
4485             tcg_gen_extu_i32_i64(dst, t0);
4486         }
4487         break;
4488     default:
4489         assert(FALSE); /* all shift types should be handled */
4490         break;
4491     }
4492 
4493     if (!sf) { /* zero extend final result */
4494         tcg_gen_ext32u_i64(dst, dst);
4495     }
4496 }
4497 
4498 /* Shift a TCGv src by immediate, put result in dst.
4499  * The shift amount must be in range (this should always be true as the
4500  * relevant instructions will UNDEF on bad shift immediates).
4501  */
4502 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4503                           enum a64_shift_type shift_type, unsigned int shift_i)
4504 {
4505     assert(shift_i < (sf ? 64 : 32));
4506 
4507     if (shift_i == 0) {
4508         tcg_gen_mov_i64(dst, src);
4509     } else {
4510         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
4511     }
4512 }
4513 
4514 /* Logical (shifted register)
4515  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
4516  * +----+-----+-----------+-------+---+------+--------+------+------+
4517  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
4518  * +----+-----+-----------+-------+---+------+--------+------+------+
4519  */
4520 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4521 {
4522     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4523     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4524 
4525     sf = extract32(insn, 31, 1);
4526     opc = extract32(insn, 29, 2);
4527     shift_type = extract32(insn, 22, 2);
4528     invert = extract32(insn, 21, 1);
4529     rm = extract32(insn, 16, 5);
4530     shift_amount = extract32(insn, 10, 6);
4531     rn = extract32(insn, 5, 5);
4532     rd = extract32(insn, 0, 5);
4533 
4534     if (!sf && (shift_amount & (1 << 5))) {
4535         unallocated_encoding(s);
4536         return;
4537     }
4538 
4539     tcg_rd = cpu_reg(s, rd);
4540 
4541     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4542         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4543          * register-register MOV and MVN, so it is worth special casing.
4544          */
4545         tcg_rm = cpu_reg(s, rm);
4546         if (invert) {
4547             tcg_gen_not_i64(tcg_rd, tcg_rm);
4548             if (!sf) {
4549                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4550             }
4551         } else {
4552             if (sf) {
4553                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4554             } else {
4555                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4556             }
4557         }
4558         return;
4559     }
4560 
4561     tcg_rm = read_cpu_reg(s, rm, sf);
4562 
4563     if (shift_amount) {
4564         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4565     }
4566 
4567     tcg_rn = cpu_reg(s, rn);
4568 
4569     switch (opc | (invert << 2)) {
4570     case 0: /* AND */
4571     case 3: /* ANDS */
4572         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4573         break;
4574     case 1: /* ORR */
4575         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4576         break;
4577     case 2: /* EOR */
4578         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4579         break;
4580     case 4: /* BIC */
4581     case 7: /* BICS */
4582         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4583         break;
4584     case 5: /* ORN */
4585         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4586         break;
4587     case 6: /* EON */
4588         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4589         break;
4590     default:
4591         assert(FALSE);
4592         break;
4593     }
4594 
4595     if (!sf) {
4596         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4597     }
4598 
4599     if (opc == 3) {
4600         gen_logic_CC(sf, tcg_rd);
4601     }
4602 }
4603 
4604 /*
4605  * Add/subtract (extended register)
4606  *
4607  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
4608  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4609  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
4610  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4611  *
4612  *  sf: 0 -> 32bit, 1 -> 64bit
4613  *  op: 0 -> add  , 1 -> sub
4614  *   S: 1 -> set flags
4615  * opt: 00
4616  * option: extension type (see DecodeRegExtend)
4617  * imm3: optional shift to Rm
4618  *
4619  * Rd = Rn + LSL(extend(Rm), amount)
4620  */
4621 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4622 {
4623     int rd = extract32(insn, 0, 5);
4624     int rn = extract32(insn, 5, 5);
4625     int imm3 = extract32(insn, 10, 3);
4626     int option = extract32(insn, 13, 3);
4627     int rm = extract32(insn, 16, 5);
4628     int opt = extract32(insn, 22, 2);
4629     bool setflags = extract32(insn, 29, 1);
4630     bool sub_op = extract32(insn, 30, 1);
4631     bool sf = extract32(insn, 31, 1);
4632 
4633     TCGv_i64 tcg_rm, tcg_rn; /* temps */
4634     TCGv_i64 tcg_rd;
4635     TCGv_i64 tcg_result;
4636 
4637     if (imm3 > 4 || opt != 0) {
4638         unallocated_encoding(s);
4639         return;
4640     }
4641 
4642     /* non-flag setting ops may use SP */
4643     if (!setflags) {
4644         tcg_rd = cpu_reg_sp(s, rd);
4645     } else {
4646         tcg_rd = cpu_reg(s, rd);
4647     }
4648     tcg_rn = read_cpu_reg_sp(s, rn, sf);
4649 
4650     tcg_rm = read_cpu_reg(s, rm, sf);
4651     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4652 
4653     tcg_result = tcg_temp_new_i64();
4654 
4655     if (!setflags) {
4656         if (sub_op) {
4657             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4658         } else {
4659             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4660         }
4661     } else {
4662         if (sub_op) {
4663             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4664         } else {
4665             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4666         }
4667     }
4668 
4669     if (sf) {
4670         tcg_gen_mov_i64(tcg_rd, tcg_result);
4671     } else {
4672         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4673     }
4674 }
4675 
4676 /*
4677  * Add/subtract (shifted register)
4678  *
4679  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
4680  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4681  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
4682  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4683  *
4684  *    sf: 0 -> 32bit, 1 -> 64bit
4685  *    op: 0 -> add  , 1 -> sub
4686  *     S: 1 -> set flags
4687  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4688  *  imm6: Shift amount to apply to Rm before the add/sub
4689  */
4690 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4691 {
4692     int rd = extract32(insn, 0, 5);
4693     int rn = extract32(insn, 5, 5);
4694     int imm6 = extract32(insn, 10, 6);
4695     int rm = extract32(insn, 16, 5);
4696     int shift_type = extract32(insn, 22, 2);
4697     bool setflags = extract32(insn, 29, 1);
4698     bool sub_op = extract32(insn, 30, 1);
4699     bool sf = extract32(insn, 31, 1);
4700 
4701     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4702     TCGv_i64 tcg_rn, tcg_rm;
4703     TCGv_i64 tcg_result;
4704 
4705     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4706         unallocated_encoding(s);
4707         return;
4708     }
4709 
4710     tcg_rn = read_cpu_reg(s, rn, sf);
4711     tcg_rm = read_cpu_reg(s, rm, sf);
4712 
4713     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4714 
4715     tcg_result = tcg_temp_new_i64();
4716 
4717     if (!setflags) {
4718         if (sub_op) {
4719             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4720         } else {
4721             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4722         }
4723     } else {
4724         if (sub_op) {
4725             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4726         } else {
4727             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4728         }
4729     }
4730 
4731     if (sf) {
4732         tcg_gen_mov_i64(tcg_rd, tcg_result);
4733     } else {
4734         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4735     }
4736 }
4737 
4738 /* Data-processing (3 source)
4739  *
4740  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
4741  *  +--+------+-----------+------+------+----+------+------+------+
4742  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
4743  *  +--+------+-----------+------+------+----+------+------+------+
4744  */
4745 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4746 {
4747     int rd = extract32(insn, 0, 5);
4748     int rn = extract32(insn, 5, 5);
4749     int ra = extract32(insn, 10, 5);
4750     int rm = extract32(insn, 16, 5);
4751     int op_id = (extract32(insn, 29, 3) << 4) |
4752         (extract32(insn, 21, 3) << 1) |
4753         extract32(insn, 15, 1);
4754     bool sf = extract32(insn, 31, 1);
4755     bool is_sub = extract32(op_id, 0, 1);
4756     bool is_high = extract32(op_id, 2, 1);
4757     bool is_signed = false;
4758     TCGv_i64 tcg_op1;
4759     TCGv_i64 tcg_op2;
4760     TCGv_i64 tcg_tmp;
4761 
4762     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
4763     switch (op_id) {
4764     case 0x42: /* SMADDL */
4765     case 0x43: /* SMSUBL */
4766     case 0x44: /* SMULH */
4767         is_signed = true;
4768         break;
4769     case 0x0: /* MADD (32bit) */
4770     case 0x1: /* MSUB (32bit) */
4771     case 0x40: /* MADD (64bit) */
4772     case 0x41: /* MSUB (64bit) */
4773     case 0x4a: /* UMADDL */
4774     case 0x4b: /* UMSUBL */
4775     case 0x4c: /* UMULH */
4776         break;
4777     default:
4778         unallocated_encoding(s);
4779         return;
4780     }
4781 
4782     if (is_high) {
4783         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
4784         TCGv_i64 tcg_rd = cpu_reg(s, rd);
4785         TCGv_i64 tcg_rn = cpu_reg(s, rn);
4786         TCGv_i64 tcg_rm = cpu_reg(s, rm);
4787 
4788         if (is_signed) {
4789             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4790         } else {
4791             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4792         }
4793         return;
4794     }
4795 
4796     tcg_op1 = tcg_temp_new_i64();
4797     tcg_op2 = tcg_temp_new_i64();
4798     tcg_tmp = tcg_temp_new_i64();
4799 
4800     if (op_id < 0x42) {
4801         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
4802         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
4803     } else {
4804         if (is_signed) {
4805             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
4806             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
4807         } else {
4808             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
4809             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
4810         }
4811     }
4812 
4813     if (ra == 31 && !is_sub) {
4814         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
4815         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
4816     } else {
4817         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
4818         if (is_sub) {
4819             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4820         } else {
4821             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4822         }
4823     }
4824 
4825     if (!sf) {
4826         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
4827     }
4828 }
4829 
4830 /* Add/subtract (with carry)
4831  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
4832  * +--+--+--+------------------------+------+-------------+------+-----+
4833  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
4834  * +--+--+--+------------------------+------+-------------+------+-----+
4835  */
4836 
4837 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
4838 {
4839     unsigned int sf, op, setflags, rm, rn, rd;
4840     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
4841 
4842     sf = extract32(insn, 31, 1);
4843     op = extract32(insn, 30, 1);
4844     setflags = extract32(insn, 29, 1);
4845     rm = extract32(insn, 16, 5);
4846     rn = extract32(insn, 5, 5);
4847     rd = extract32(insn, 0, 5);
4848 
4849     tcg_rd = cpu_reg(s, rd);
4850     tcg_rn = cpu_reg(s, rn);
4851 
4852     if (op) {
4853         tcg_y = tcg_temp_new_i64();
4854         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
4855     } else {
4856         tcg_y = cpu_reg(s, rm);
4857     }
4858 
4859     if (setflags) {
4860         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
4861     } else {
4862         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
4863     }
4864 }
4865 
4866 /*
4867  * Rotate right into flags
4868  *  31 30 29                21       15          10      5  4      0
4869  * +--+--+--+-----------------+--------+-----------+------+--+------+
4870  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
4871  * +--+--+--+-----------------+--------+-----------+------+--+------+
4872  */
4873 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
4874 {
4875     int mask = extract32(insn, 0, 4);
4876     int o2 = extract32(insn, 4, 1);
4877     int rn = extract32(insn, 5, 5);
4878     int imm6 = extract32(insn, 15, 6);
4879     int sf_op_s = extract32(insn, 29, 3);
4880     TCGv_i64 tcg_rn;
4881     TCGv_i32 nzcv;
4882 
4883     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
4884         unallocated_encoding(s);
4885         return;
4886     }
4887 
4888     tcg_rn = read_cpu_reg(s, rn, 1);
4889     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
4890 
4891     nzcv = tcg_temp_new_i32();
4892     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
4893 
4894     if (mask & 8) { /* N */
4895         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
4896     }
4897     if (mask & 4) { /* Z */
4898         tcg_gen_not_i32(cpu_ZF, nzcv);
4899         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
4900     }
4901     if (mask & 2) { /* C */
4902         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
4903     }
4904     if (mask & 1) { /* V */
4905         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
4906     }
4907 }
4908 
4909 /*
4910  * Evaluate into flags
4911  *  31 30 29                21        15   14        10      5  4      0
4912  * +--+--+--+-----------------+---------+----+---------+------+--+------+
4913  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
4914  * +--+--+--+-----------------+---------+----+---------+------+--+------+
4915  */
4916 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
4917 {
4918     int o3_mask = extract32(insn, 0, 5);
4919     int rn = extract32(insn, 5, 5);
4920     int o2 = extract32(insn, 15, 6);
4921     int sz = extract32(insn, 14, 1);
4922     int sf_op_s = extract32(insn, 29, 3);
4923     TCGv_i32 tmp;
4924     int shift;
4925 
4926     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
4927         !dc_isar_feature(aa64_condm_4, s)) {
4928         unallocated_encoding(s);
4929         return;
4930     }
4931     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
4932 
4933     tmp = tcg_temp_new_i32();
4934     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
4935     tcg_gen_shli_i32(cpu_NF, tmp, shift);
4936     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
4937     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
4938     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
4939 }
4940 
4941 /* Conditional compare (immediate / register)
4942  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
4943  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
4944  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
4945  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
4946  *        [1]                             y                [0]       [0]
4947  */
4948 static void disas_cc(DisasContext *s, uint32_t insn)
4949 {
4950     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
4951     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
4952     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
4953     DisasCompare c;
4954 
4955     if (!extract32(insn, 29, 1)) {
4956         unallocated_encoding(s);
4957         return;
4958     }
4959     if (insn & (1 << 10 | 1 << 4)) {
4960         unallocated_encoding(s);
4961         return;
4962     }
4963     sf = extract32(insn, 31, 1);
4964     op = extract32(insn, 30, 1);
4965     is_imm = extract32(insn, 11, 1);
4966     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
4967     cond = extract32(insn, 12, 4);
4968     rn = extract32(insn, 5, 5);
4969     nzcv = extract32(insn, 0, 4);
4970 
4971     /* Set T0 = !COND.  */
4972     tcg_t0 = tcg_temp_new_i32();
4973     arm_test_cc(&c, cond);
4974     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
4975 
4976     /* Load the arguments for the new comparison.  */
4977     if (is_imm) {
4978         tcg_y = tcg_temp_new_i64();
4979         tcg_gen_movi_i64(tcg_y, y);
4980     } else {
4981         tcg_y = cpu_reg(s, y);
4982     }
4983     tcg_rn = cpu_reg(s, rn);
4984 
4985     /* Set the flags for the new comparison.  */
4986     tcg_tmp = tcg_temp_new_i64();
4987     if (op) {
4988         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
4989     } else {
4990         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
4991     }
4992 
4993     /* If COND was false, force the flags to #nzcv.  Compute two masks
4994      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
4995      * For tcg hosts that support ANDC, we can make do with just T1.
4996      * In either case, allow the tcg optimizer to delete any unused mask.
4997      */
4998     tcg_t1 = tcg_temp_new_i32();
4999     tcg_t2 = tcg_temp_new_i32();
5000     tcg_gen_neg_i32(tcg_t1, tcg_t0);
5001     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
5002 
5003     if (nzcv & 8) { /* N */
5004         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
5005     } else {
5006         if (TCG_TARGET_HAS_andc_i32) {
5007             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
5008         } else {
5009             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
5010         }
5011     }
5012     if (nzcv & 4) { /* Z */
5013         if (TCG_TARGET_HAS_andc_i32) {
5014             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
5015         } else {
5016             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
5017         }
5018     } else {
5019         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
5020     }
5021     if (nzcv & 2) { /* C */
5022         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
5023     } else {
5024         if (TCG_TARGET_HAS_andc_i32) {
5025             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
5026         } else {
5027             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
5028         }
5029     }
5030     if (nzcv & 1) { /* V */
5031         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
5032     } else {
5033         if (TCG_TARGET_HAS_andc_i32) {
5034             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
5035         } else {
5036             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
5037         }
5038     }
5039 }
5040 
5041 /* Conditional select
5042  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
5043  * +----+----+---+-----------------+------+------+-----+------+------+
5044  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
5045  * +----+----+---+-----------------+------+------+-----+------+------+
5046  */
5047 static void disas_cond_select(DisasContext *s, uint32_t insn)
5048 {
5049     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
5050     TCGv_i64 tcg_rd, zero;
5051     DisasCompare64 c;
5052 
5053     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
5054         /* S == 1 or op2<1> == 1 */
5055         unallocated_encoding(s);
5056         return;
5057     }
5058     sf = extract32(insn, 31, 1);
5059     else_inv = extract32(insn, 30, 1);
5060     rm = extract32(insn, 16, 5);
5061     cond = extract32(insn, 12, 4);
5062     else_inc = extract32(insn, 10, 1);
5063     rn = extract32(insn, 5, 5);
5064     rd = extract32(insn, 0, 5);
5065 
5066     tcg_rd = cpu_reg(s, rd);
5067 
5068     a64_test_cc(&c, cond);
5069     zero = tcg_constant_i64(0);
5070 
5071     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
5072         /* CSET & CSETM.  */
5073         if (else_inv) {
5074             tcg_gen_negsetcond_i64(tcg_invert_cond(c.cond),
5075                                    tcg_rd, c.value, zero);
5076         } else {
5077             tcg_gen_setcond_i64(tcg_invert_cond(c.cond),
5078                                 tcg_rd, c.value, zero);
5079         }
5080     } else {
5081         TCGv_i64 t_true = cpu_reg(s, rn);
5082         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
5083         if (else_inv && else_inc) {
5084             tcg_gen_neg_i64(t_false, t_false);
5085         } else if (else_inv) {
5086             tcg_gen_not_i64(t_false, t_false);
5087         } else if (else_inc) {
5088             tcg_gen_addi_i64(t_false, t_false, 1);
5089         }
5090         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
5091     }
5092 
5093     if (!sf) {
5094         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5095     }
5096 }
5097 
5098 static void handle_clz(DisasContext *s, unsigned int sf,
5099                        unsigned int rn, unsigned int rd)
5100 {
5101     TCGv_i64 tcg_rd, tcg_rn;
5102     tcg_rd = cpu_reg(s, rd);
5103     tcg_rn = cpu_reg(s, rn);
5104 
5105     if (sf) {
5106         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
5107     } else {
5108         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5109         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5110         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
5111         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5112     }
5113 }
5114 
5115 static void handle_cls(DisasContext *s, unsigned int sf,
5116                        unsigned int rn, unsigned int rd)
5117 {
5118     TCGv_i64 tcg_rd, tcg_rn;
5119     tcg_rd = cpu_reg(s, rd);
5120     tcg_rn = cpu_reg(s, rn);
5121 
5122     if (sf) {
5123         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
5124     } else {
5125         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5126         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5127         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
5128         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5129     }
5130 }
5131 
5132 static void handle_rbit(DisasContext *s, unsigned int sf,
5133                         unsigned int rn, unsigned int rd)
5134 {
5135     TCGv_i64 tcg_rd, tcg_rn;
5136     tcg_rd = cpu_reg(s, rd);
5137     tcg_rn = cpu_reg(s, rn);
5138 
5139     if (sf) {
5140         gen_helper_rbit64(tcg_rd, tcg_rn);
5141     } else {
5142         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5143         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5144         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5145         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5146     }
5147 }
5148 
5149 /* REV with sf==1, opcode==3 ("REV64") */
5150 static void handle_rev64(DisasContext *s, unsigned int sf,
5151                          unsigned int rn, unsigned int rd)
5152 {
5153     if (!sf) {
5154         unallocated_encoding(s);
5155         return;
5156     }
5157     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5158 }
5159 
5160 /* REV with sf==0, opcode==2
5161  * REV32 (sf==1, opcode==2)
5162  */
5163 static void handle_rev32(DisasContext *s, unsigned int sf,
5164                          unsigned int rn, unsigned int rd)
5165 {
5166     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5167     TCGv_i64 tcg_rn = cpu_reg(s, rn);
5168 
5169     if (sf) {
5170         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
5171         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
5172     } else {
5173         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
5174     }
5175 }
5176 
5177 /* REV16 (opcode==1) */
5178 static void handle_rev16(DisasContext *s, unsigned int sf,
5179                          unsigned int rn, unsigned int rd)
5180 {
5181     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5182     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5183     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5184     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5185 
5186     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5187     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5188     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5189     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5190     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5191 }
5192 
5193 /* Data-processing (1 source)
5194  *   31  30  29  28             21 20     16 15    10 9    5 4    0
5195  * +----+---+---+-----------------+---------+--------+------+------+
5196  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
5197  * +----+---+---+-----------------+---------+--------+------+------+
5198  */
5199 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5200 {
5201     unsigned int sf, opcode, opcode2, rn, rd;
5202     TCGv_i64 tcg_rd;
5203 
5204     if (extract32(insn, 29, 1)) {
5205         unallocated_encoding(s);
5206         return;
5207     }
5208 
5209     sf = extract32(insn, 31, 1);
5210     opcode = extract32(insn, 10, 6);
5211     opcode2 = extract32(insn, 16, 5);
5212     rn = extract32(insn, 5, 5);
5213     rd = extract32(insn, 0, 5);
5214 
5215 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5216 
5217     switch (MAP(sf, opcode2, opcode)) {
5218     case MAP(0, 0x00, 0x00): /* RBIT */
5219     case MAP(1, 0x00, 0x00):
5220         handle_rbit(s, sf, rn, rd);
5221         break;
5222     case MAP(0, 0x00, 0x01): /* REV16 */
5223     case MAP(1, 0x00, 0x01):
5224         handle_rev16(s, sf, rn, rd);
5225         break;
5226     case MAP(0, 0x00, 0x02): /* REV/REV32 */
5227     case MAP(1, 0x00, 0x02):
5228         handle_rev32(s, sf, rn, rd);
5229         break;
5230     case MAP(1, 0x00, 0x03): /* REV64 */
5231         handle_rev64(s, sf, rn, rd);
5232         break;
5233     case MAP(0, 0x00, 0x04): /* CLZ */
5234     case MAP(1, 0x00, 0x04):
5235         handle_clz(s, sf, rn, rd);
5236         break;
5237     case MAP(0, 0x00, 0x05): /* CLS */
5238     case MAP(1, 0x00, 0x05):
5239         handle_cls(s, sf, rn, rd);
5240         break;
5241     case MAP(1, 0x01, 0x00): /* PACIA */
5242         if (s->pauth_active) {
5243             tcg_rd = cpu_reg(s, rd);
5244             gen_helper_pacia(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5245         } else if (!dc_isar_feature(aa64_pauth, s)) {
5246             goto do_unallocated;
5247         }
5248         break;
5249     case MAP(1, 0x01, 0x01): /* PACIB */
5250         if (s->pauth_active) {
5251             tcg_rd = cpu_reg(s, rd);
5252             gen_helper_pacib(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5253         } else if (!dc_isar_feature(aa64_pauth, s)) {
5254             goto do_unallocated;
5255         }
5256         break;
5257     case MAP(1, 0x01, 0x02): /* PACDA */
5258         if (s->pauth_active) {
5259             tcg_rd = cpu_reg(s, rd);
5260             gen_helper_pacda(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5261         } else if (!dc_isar_feature(aa64_pauth, s)) {
5262             goto do_unallocated;
5263         }
5264         break;
5265     case MAP(1, 0x01, 0x03): /* PACDB */
5266         if (s->pauth_active) {
5267             tcg_rd = cpu_reg(s, rd);
5268             gen_helper_pacdb(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5269         } else if (!dc_isar_feature(aa64_pauth, s)) {
5270             goto do_unallocated;
5271         }
5272         break;
5273     case MAP(1, 0x01, 0x04): /* AUTIA */
5274         if (s->pauth_active) {
5275             tcg_rd = cpu_reg(s, rd);
5276             gen_helper_autia(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5277         } else if (!dc_isar_feature(aa64_pauth, s)) {
5278             goto do_unallocated;
5279         }
5280         break;
5281     case MAP(1, 0x01, 0x05): /* AUTIB */
5282         if (s->pauth_active) {
5283             tcg_rd = cpu_reg(s, rd);
5284             gen_helper_autib(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5285         } else if (!dc_isar_feature(aa64_pauth, s)) {
5286             goto do_unallocated;
5287         }
5288         break;
5289     case MAP(1, 0x01, 0x06): /* AUTDA */
5290         if (s->pauth_active) {
5291             tcg_rd = cpu_reg(s, rd);
5292             gen_helper_autda(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5293         } else if (!dc_isar_feature(aa64_pauth, s)) {
5294             goto do_unallocated;
5295         }
5296         break;
5297     case MAP(1, 0x01, 0x07): /* AUTDB */
5298         if (s->pauth_active) {
5299             tcg_rd = cpu_reg(s, rd);
5300             gen_helper_autdb(tcg_rd, tcg_env, tcg_rd, cpu_reg_sp(s, rn));
5301         } else if (!dc_isar_feature(aa64_pauth, s)) {
5302             goto do_unallocated;
5303         }
5304         break;
5305     case MAP(1, 0x01, 0x08): /* PACIZA */
5306         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5307             goto do_unallocated;
5308         } else if (s->pauth_active) {
5309             tcg_rd = cpu_reg(s, rd);
5310             gen_helper_pacia(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5311         }
5312         break;
5313     case MAP(1, 0x01, 0x09): /* PACIZB */
5314         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5315             goto do_unallocated;
5316         } else if (s->pauth_active) {
5317             tcg_rd = cpu_reg(s, rd);
5318             gen_helper_pacib(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5319         }
5320         break;
5321     case MAP(1, 0x01, 0x0a): /* PACDZA */
5322         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5323             goto do_unallocated;
5324         } else if (s->pauth_active) {
5325             tcg_rd = cpu_reg(s, rd);
5326             gen_helper_pacda(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5327         }
5328         break;
5329     case MAP(1, 0x01, 0x0b): /* PACDZB */
5330         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5331             goto do_unallocated;
5332         } else if (s->pauth_active) {
5333             tcg_rd = cpu_reg(s, rd);
5334             gen_helper_pacdb(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5335         }
5336         break;
5337     case MAP(1, 0x01, 0x0c): /* AUTIZA */
5338         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5339             goto do_unallocated;
5340         } else if (s->pauth_active) {
5341             tcg_rd = cpu_reg(s, rd);
5342             gen_helper_autia(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5343         }
5344         break;
5345     case MAP(1, 0x01, 0x0d): /* AUTIZB */
5346         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5347             goto do_unallocated;
5348         } else if (s->pauth_active) {
5349             tcg_rd = cpu_reg(s, rd);
5350             gen_helper_autib(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5351         }
5352         break;
5353     case MAP(1, 0x01, 0x0e): /* AUTDZA */
5354         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5355             goto do_unallocated;
5356         } else if (s->pauth_active) {
5357             tcg_rd = cpu_reg(s, rd);
5358             gen_helper_autda(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5359         }
5360         break;
5361     case MAP(1, 0x01, 0x0f): /* AUTDZB */
5362         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5363             goto do_unallocated;
5364         } else if (s->pauth_active) {
5365             tcg_rd = cpu_reg(s, rd);
5366             gen_helper_autdb(tcg_rd, tcg_env, tcg_rd, tcg_constant_i64(0));
5367         }
5368         break;
5369     case MAP(1, 0x01, 0x10): /* XPACI */
5370         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5371             goto do_unallocated;
5372         } else if (s->pauth_active) {
5373             tcg_rd = cpu_reg(s, rd);
5374             gen_helper_xpaci(tcg_rd, tcg_env, tcg_rd);
5375         }
5376         break;
5377     case MAP(1, 0x01, 0x11): /* XPACD */
5378         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5379             goto do_unallocated;
5380         } else if (s->pauth_active) {
5381             tcg_rd = cpu_reg(s, rd);
5382             gen_helper_xpacd(tcg_rd, tcg_env, tcg_rd);
5383         }
5384         break;
5385     default:
5386     do_unallocated:
5387         unallocated_encoding(s);
5388         break;
5389     }
5390 
5391 #undef MAP
5392 }
5393 
5394 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5395                        unsigned int rm, unsigned int rn, unsigned int rd)
5396 {
5397     TCGv_i64 tcg_n, tcg_m, tcg_rd;
5398     tcg_rd = cpu_reg(s, rd);
5399 
5400     if (!sf && is_signed) {
5401         tcg_n = tcg_temp_new_i64();
5402         tcg_m = tcg_temp_new_i64();
5403         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5404         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5405     } else {
5406         tcg_n = read_cpu_reg(s, rn, sf);
5407         tcg_m = read_cpu_reg(s, rm, sf);
5408     }
5409 
5410     if (is_signed) {
5411         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5412     } else {
5413         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5414     }
5415 
5416     if (!sf) { /* zero extend final result */
5417         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5418     }
5419 }
5420 
5421 /* LSLV, LSRV, ASRV, RORV */
5422 static void handle_shift_reg(DisasContext *s,
5423                              enum a64_shift_type shift_type, unsigned int sf,
5424                              unsigned int rm, unsigned int rn, unsigned int rd)
5425 {
5426     TCGv_i64 tcg_shift = tcg_temp_new_i64();
5427     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5428     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5429 
5430     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5431     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5432 }
5433 
5434 /* CRC32[BHWX], CRC32C[BHWX] */
5435 static void handle_crc32(DisasContext *s,
5436                          unsigned int sf, unsigned int sz, bool crc32c,
5437                          unsigned int rm, unsigned int rn, unsigned int rd)
5438 {
5439     TCGv_i64 tcg_acc, tcg_val;
5440     TCGv_i32 tcg_bytes;
5441 
5442     if (!dc_isar_feature(aa64_crc32, s)
5443         || (sf == 1 && sz != 3)
5444         || (sf == 0 && sz == 3)) {
5445         unallocated_encoding(s);
5446         return;
5447     }
5448 
5449     if (sz == 3) {
5450         tcg_val = cpu_reg(s, rm);
5451     } else {
5452         uint64_t mask;
5453         switch (sz) {
5454         case 0:
5455             mask = 0xFF;
5456             break;
5457         case 1:
5458             mask = 0xFFFF;
5459             break;
5460         case 2:
5461             mask = 0xFFFFFFFF;
5462             break;
5463         default:
5464             g_assert_not_reached();
5465         }
5466         tcg_val = tcg_temp_new_i64();
5467         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5468     }
5469 
5470     tcg_acc = cpu_reg(s, rn);
5471     tcg_bytes = tcg_constant_i32(1 << sz);
5472 
5473     if (crc32c) {
5474         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5475     } else {
5476         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5477     }
5478 }
5479 
5480 /* Data-processing (2 source)
5481  *   31   30  29 28             21 20  16 15    10 9    5 4    0
5482  * +----+---+---+-----------------+------+--------+------+------+
5483  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
5484  * +----+---+---+-----------------+------+--------+------+------+
5485  */
5486 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5487 {
5488     unsigned int sf, rm, opcode, rn, rd, setflag;
5489     sf = extract32(insn, 31, 1);
5490     setflag = extract32(insn, 29, 1);
5491     rm = extract32(insn, 16, 5);
5492     opcode = extract32(insn, 10, 6);
5493     rn = extract32(insn, 5, 5);
5494     rd = extract32(insn, 0, 5);
5495 
5496     if (setflag && opcode != 0) {
5497         unallocated_encoding(s);
5498         return;
5499     }
5500 
5501     switch (opcode) {
5502     case 0: /* SUBP(S) */
5503         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5504             goto do_unallocated;
5505         } else {
5506             TCGv_i64 tcg_n, tcg_m, tcg_d;
5507 
5508             tcg_n = read_cpu_reg_sp(s, rn, true);
5509             tcg_m = read_cpu_reg_sp(s, rm, true);
5510             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5511             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5512             tcg_d = cpu_reg(s, rd);
5513 
5514             if (setflag) {
5515                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5516             } else {
5517                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5518             }
5519         }
5520         break;
5521     case 2: /* UDIV */
5522         handle_div(s, false, sf, rm, rn, rd);
5523         break;
5524     case 3: /* SDIV */
5525         handle_div(s, true, sf, rm, rn, rd);
5526         break;
5527     case 4: /* IRG */
5528         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5529             goto do_unallocated;
5530         }
5531         if (s->ata[0]) {
5532             gen_helper_irg(cpu_reg_sp(s, rd), tcg_env,
5533                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
5534         } else {
5535             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5536                                              cpu_reg_sp(s, rn));
5537         }
5538         break;
5539     case 5: /* GMI */
5540         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5541             goto do_unallocated;
5542         } else {
5543             TCGv_i64 t = tcg_temp_new_i64();
5544 
5545             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
5546             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
5547             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
5548         }
5549         break;
5550     case 8: /* LSLV */
5551         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5552         break;
5553     case 9: /* LSRV */
5554         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5555         break;
5556     case 10: /* ASRV */
5557         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5558         break;
5559     case 11: /* RORV */
5560         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5561         break;
5562     case 12: /* PACGA */
5563         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5564             goto do_unallocated;
5565         }
5566         gen_helper_pacga(cpu_reg(s, rd), tcg_env,
5567                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
5568         break;
5569     case 16:
5570     case 17:
5571     case 18:
5572     case 19:
5573     case 20:
5574     case 21:
5575     case 22:
5576     case 23: /* CRC32 */
5577     {
5578         int sz = extract32(opcode, 0, 2);
5579         bool crc32c = extract32(opcode, 2, 1);
5580         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5581         break;
5582     }
5583     default:
5584     do_unallocated:
5585         unallocated_encoding(s);
5586         break;
5587     }
5588 }
5589 
5590 /*
5591  * Data processing - register
5592  *  31  30 29  28      25    21  20  16      10         0
5593  * +--+---+--+---+-------+-----+-------+-------+---------+
5594  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
5595  * +--+---+--+---+-------+-----+-------+-------+---------+
5596  */
5597 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5598 {
5599     int op0 = extract32(insn, 30, 1);
5600     int op1 = extract32(insn, 28, 1);
5601     int op2 = extract32(insn, 21, 4);
5602     int op3 = extract32(insn, 10, 6);
5603 
5604     if (!op1) {
5605         if (op2 & 8) {
5606             if (op2 & 1) {
5607                 /* Add/sub (extended register) */
5608                 disas_add_sub_ext_reg(s, insn);
5609             } else {
5610                 /* Add/sub (shifted register) */
5611                 disas_add_sub_reg(s, insn);
5612             }
5613         } else {
5614             /* Logical (shifted register) */
5615             disas_logic_reg(s, insn);
5616         }
5617         return;
5618     }
5619 
5620     switch (op2) {
5621     case 0x0:
5622         switch (op3) {
5623         case 0x00: /* Add/subtract (with carry) */
5624             disas_adc_sbc(s, insn);
5625             break;
5626 
5627         case 0x01: /* Rotate right into flags */
5628         case 0x21:
5629             disas_rotate_right_into_flags(s, insn);
5630             break;
5631 
5632         case 0x02: /* Evaluate into flags */
5633         case 0x12:
5634         case 0x22:
5635         case 0x32:
5636             disas_evaluate_into_flags(s, insn);
5637             break;
5638 
5639         default:
5640             goto do_unallocated;
5641         }
5642         break;
5643 
5644     case 0x2: /* Conditional compare */
5645         disas_cc(s, insn); /* both imm and reg forms */
5646         break;
5647 
5648     case 0x4: /* Conditional select */
5649         disas_cond_select(s, insn);
5650         break;
5651 
5652     case 0x6: /* Data-processing */
5653         if (op0) {    /* (1 source) */
5654             disas_data_proc_1src(s, insn);
5655         } else {      /* (2 source) */
5656             disas_data_proc_2src(s, insn);
5657         }
5658         break;
5659     case 0x8 ... 0xf: /* (3 source) */
5660         disas_data_proc_3src(s, insn);
5661         break;
5662 
5663     default:
5664     do_unallocated:
5665         unallocated_encoding(s);
5666         break;
5667     }
5668 }
5669 
5670 static void handle_fp_compare(DisasContext *s, int size,
5671                               unsigned int rn, unsigned int rm,
5672                               bool cmp_with_zero, bool signal_all_nans)
5673 {
5674     TCGv_i64 tcg_flags = tcg_temp_new_i64();
5675     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
5676 
5677     if (size == MO_64) {
5678         TCGv_i64 tcg_vn, tcg_vm;
5679 
5680         tcg_vn = read_fp_dreg(s, rn);
5681         if (cmp_with_zero) {
5682             tcg_vm = tcg_constant_i64(0);
5683         } else {
5684             tcg_vm = read_fp_dreg(s, rm);
5685         }
5686         if (signal_all_nans) {
5687             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5688         } else {
5689             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5690         }
5691     } else {
5692         TCGv_i32 tcg_vn = tcg_temp_new_i32();
5693         TCGv_i32 tcg_vm = tcg_temp_new_i32();
5694 
5695         read_vec_element_i32(s, tcg_vn, rn, 0, size);
5696         if (cmp_with_zero) {
5697             tcg_gen_movi_i32(tcg_vm, 0);
5698         } else {
5699             read_vec_element_i32(s, tcg_vm, rm, 0, size);
5700         }
5701 
5702         switch (size) {
5703         case MO_32:
5704             if (signal_all_nans) {
5705                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5706             } else {
5707                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5708             }
5709             break;
5710         case MO_16:
5711             if (signal_all_nans) {
5712                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5713             } else {
5714                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5715             }
5716             break;
5717         default:
5718             g_assert_not_reached();
5719         }
5720     }
5721 
5722     gen_set_nzcv(tcg_flags);
5723 }
5724 
5725 /* Floating point compare
5726  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
5727  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5728  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
5729  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5730  */
5731 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5732 {
5733     unsigned int mos, type, rm, op, rn, opc, op2r;
5734     int size;
5735 
5736     mos = extract32(insn, 29, 3);
5737     type = extract32(insn, 22, 2);
5738     rm = extract32(insn, 16, 5);
5739     op = extract32(insn, 14, 2);
5740     rn = extract32(insn, 5, 5);
5741     opc = extract32(insn, 3, 2);
5742     op2r = extract32(insn, 0, 3);
5743 
5744     if (mos || op || op2r) {
5745         unallocated_encoding(s);
5746         return;
5747     }
5748 
5749     switch (type) {
5750     case 0:
5751         size = MO_32;
5752         break;
5753     case 1:
5754         size = MO_64;
5755         break;
5756     case 3:
5757         size = MO_16;
5758         if (dc_isar_feature(aa64_fp16, s)) {
5759             break;
5760         }
5761         /* fallthru */
5762     default:
5763         unallocated_encoding(s);
5764         return;
5765     }
5766 
5767     if (!fp_access_check(s)) {
5768         return;
5769     }
5770 
5771     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
5772 }
5773 
5774 /* Floating point conditional compare
5775  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
5776  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5777  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
5778  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5779  */
5780 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
5781 {
5782     unsigned int mos, type, rm, cond, rn, op, nzcv;
5783     TCGLabel *label_continue = NULL;
5784     int size;
5785 
5786     mos = extract32(insn, 29, 3);
5787     type = extract32(insn, 22, 2);
5788     rm = extract32(insn, 16, 5);
5789     cond = extract32(insn, 12, 4);
5790     rn = extract32(insn, 5, 5);
5791     op = extract32(insn, 4, 1);
5792     nzcv = extract32(insn, 0, 4);
5793 
5794     if (mos) {
5795         unallocated_encoding(s);
5796         return;
5797     }
5798 
5799     switch (type) {
5800     case 0:
5801         size = MO_32;
5802         break;
5803     case 1:
5804         size = MO_64;
5805         break;
5806     case 3:
5807         size = MO_16;
5808         if (dc_isar_feature(aa64_fp16, s)) {
5809             break;
5810         }
5811         /* fallthru */
5812     default:
5813         unallocated_encoding(s);
5814         return;
5815     }
5816 
5817     if (!fp_access_check(s)) {
5818         return;
5819     }
5820 
5821     if (cond < 0x0e) { /* not always */
5822         TCGLabel *label_match = gen_new_label();
5823         label_continue = gen_new_label();
5824         arm_gen_test_cc(cond, label_match);
5825         /* nomatch: */
5826         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
5827         tcg_gen_br(label_continue);
5828         gen_set_label(label_match);
5829     }
5830 
5831     handle_fp_compare(s, size, rn, rm, false, op);
5832 
5833     if (cond < 0x0e) {
5834         gen_set_label(label_continue);
5835     }
5836 }
5837 
5838 /* Floating point conditional select
5839  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
5840  * +---+---+---+-----------+------+---+------+------+-----+------+------+
5841  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
5842  * +---+---+---+-----------+------+---+------+------+-----+------+------+
5843  */
5844 static void disas_fp_csel(DisasContext *s, uint32_t insn)
5845 {
5846     unsigned int mos, type, rm, cond, rn, rd;
5847     TCGv_i64 t_true, t_false;
5848     DisasCompare64 c;
5849     MemOp sz;
5850 
5851     mos = extract32(insn, 29, 3);
5852     type = extract32(insn, 22, 2);
5853     rm = extract32(insn, 16, 5);
5854     cond = extract32(insn, 12, 4);
5855     rn = extract32(insn, 5, 5);
5856     rd = extract32(insn, 0, 5);
5857 
5858     if (mos) {
5859         unallocated_encoding(s);
5860         return;
5861     }
5862 
5863     switch (type) {
5864     case 0:
5865         sz = MO_32;
5866         break;
5867     case 1:
5868         sz = MO_64;
5869         break;
5870     case 3:
5871         sz = MO_16;
5872         if (dc_isar_feature(aa64_fp16, s)) {
5873             break;
5874         }
5875         /* fallthru */
5876     default:
5877         unallocated_encoding(s);
5878         return;
5879     }
5880 
5881     if (!fp_access_check(s)) {
5882         return;
5883     }
5884 
5885     /* Zero extend sreg & hreg inputs to 64 bits now.  */
5886     t_true = tcg_temp_new_i64();
5887     t_false = tcg_temp_new_i64();
5888     read_vec_element(s, t_true, rn, 0, sz);
5889     read_vec_element(s, t_false, rm, 0, sz);
5890 
5891     a64_test_cc(&c, cond);
5892     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
5893                         t_true, t_false);
5894 
5895     /* Note that sregs & hregs write back zeros to the high bits,
5896        and we've already done the zero-extension.  */
5897     write_fp_dreg(s, rd, t_true);
5898 }
5899 
5900 /* Floating-point data-processing (1 source) - half precision */
5901 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
5902 {
5903     TCGv_ptr fpst = NULL;
5904     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
5905     TCGv_i32 tcg_res = tcg_temp_new_i32();
5906 
5907     switch (opcode) {
5908     case 0x0: /* FMOV */
5909         tcg_gen_mov_i32(tcg_res, tcg_op);
5910         break;
5911     case 0x1: /* FABS */
5912         tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
5913         break;
5914     case 0x2: /* FNEG */
5915         tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
5916         break;
5917     case 0x3: /* FSQRT */
5918         fpst = fpstatus_ptr(FPST_FPCR_F16);
5919         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
5920         break;
5921     case 0x8: /* FRINTN */
5922     case 0x9: /* FRINTP */
5923     case 0xa: /* FRINTM */
5924     case 0xb: /* FRINTZ */
5925     case 0xc: /* FRINTA */
5926     {
5927         TCGv_i32 tcg_rmode;
5928 
5929         fpst = fpstatus_ptr(FPST_FPCR_F16);
5930         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
5931         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
5932         gen_restore_rmode(tcg_rmode, fpst);
5933         break;
5934     }
5935     case 0xe: /* FRINTX */
5936         fpst = fpstatus_ptr(FPST_FPCR_F16);
5937         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
5938         break;
5939     case 0xf: /* FRINTI */
5940         fpst = fpstatus_ptr(FPST_FPCR_F16);
5941         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
5942         break;
5943     default:
5944         g_assert_not_reached();
5945     }
5946 
5947     write_fp_sreg(s, rd, tcg_res);
5948 }
5949 
5950 /* Floating-point data-processing (1 source) - single precision */
5951 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
5952 {
5953     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
5954     TCGv_i32 tcg_op, tcg_res;
5955     TCGv_ptr fpst;
5956     int rmode = -1;
5957 
5958     tcg_op = read_fp_sreg(s, rn);
5959     tcg_res = tcg_temp_new_i32();
5960 
5961     switch (opcode) {
5962     case 0x0: /* FMOV */
5963         tcg_gen_mov_i32(tcg_res, tcg_op);
5964         goto done;
5965     case 0x1: /* FABS */
5966         gen_helper_vfp_abss(tcg_res, tcg_op);
5967         goto done;
5968     case 0x2: /* FNEG */
5969         gen_helper_vfp_negs(tcg_res, tcg_op);
5970         goto done;
5971     case 0x3: /* FSQRT */
5972         gen_helper_vfp_sqrts(tcg_res, tcg_op, tcg_env);
5973         goto done;
5974     case 0x6: /* BFCVT */
5975         gen_fpst = gen_helper_bfcvt;
5976         break;
5977     case 0x8: /* FRINTN */
5978     case 0x9: /* FRINTP */
5979     case 0xa: /* FRINTM */
5980     case 0xb: /* FRINTZ */
5981     case 0xc: /* FRINTA */
5982         rmode = opcode & 7;
5983         gen_fpst = gen_helper_rints;
5984         break;
5985     case 0xe: /* FRINTX */
5986         gen_fpst = gen_helper_rints_exact;
5987         break;
5988     case 0xf: /* FRINTI */
5989         gen_fpst = gen_helper_rints;
5990         break;
5991     case 0x10: /* FRINT32Z */
5992         rmode = FPROUNDING_ZERO;
5993         gen_fpst = gen_helper_frint32_s;
5994         break;
5995     case 0x11: /* FRINT32X */
5996         gen_fpst = gen_helper_frint32_s;
5997         break;
5998     case 0x12: /* FRINT64Z */
5999         rmode = FPROUNDING_ZERO;
6000         gen_fpst = gen_helper_frint64_s;
6001         break;
6002     case 0x13: /* FRINT64X */
6003         gen_fpst = gen_helper_frint64_s;
6004         break;
6005     default:
6006         g_assert_not_reached();
6007     }
6008 
6009     fpst = fpstatus_ptr(FPST_FPCR);
6010     if (rmode >= 0) {
6011         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6012         gen_fpst(tcg_res, tcg_op, fpst);
6013         gen_restore_rmode(tcg_rmode, fpst);
6014     } else {
6015         gen_fpst(tcg_res, tcg_op, fpst);
6016     }
6017 
6018  done:
6019     write_fp_sreg(s, rd, tcg_res);
6020 }
6021 
6022 /* Floating-point data-processing (1 source) - double precision */
6023 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6024 {
6025     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6026     TCGv_i64 tcg_op, tcg_res;
6027     TCGv_ptr fpst;
6028     int rmode = -1;
6029 
6030     switch (opcode) {
6031     case 0x0: /* FMOV */
6032         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6033         return;
6034     }
6035 
6036     tcg_op = read_fp_dreg(s, rn);
6037     tcg_res = tcg_temp_new_i64();
6038 
6039     switch (opcode) {
6040     case 0x1: /* FABS */
6041         gen_helper_vfp_absd(tcg_res, tcg_op);
6042         goto done;
6043     case 0x2: /* FNEG */
6044         gen_helper_vfp_negd(tcg_res, tcg_op);
6045         goto done;
6046     case 0x3: /* FSQRT */
6047         gen_helper_vfp_sqrtd(tcg_res, tcg_op, tcg_env);
6048         goto done;
6049     case 0x8: /* FRINTN */
6050     case 0x9: /* FRINTP */
6051     case 0xa: /* FRINTM */
6052     case 0xb: /* FRINTZ */
6053     case 0xc: /* FRINTA */
6054         rmode = opcode & 7;
6055         gen_fpst = gen_helper_rintd;
6056         break;
6057     case 0xe: /* FRINTX */
6058         gen_fpst = gen_helper_rintd_exact;
6059         break;
6060     case 0xf: /* FRINTI */
6061         gen_fpst = gen_helper_rintd;
6062         break;
6063     case 0x10: /* FRINT32Z */
6064         rmode = FPROUNDING_ZERO;
6065         gen_fpst = gen_helper_frint32_d;
6066         break;
6067     case 0x11: /* FRINT32X */
6068         gen_fpst = gen_helper_frint32_d;
6069         break;
6070     case 0x12: /* FRINT64Z */
6071         rmode = FPROUNDING_ZERO;
6072         gen_fpst = gen_helper_frint64_d;
6073         break;
6074     case 0x13: /* FRINT64X */
6075         gen_fpst = gen_helper_frint64_d;
6076         break;
6077     default:
6078         g_assert_not_reached();
6079     }
6080 
6081     fpst = fpstatus_ptr(FPST_FPCR);
6082     if (rmode >= 0) {
6083         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6084         gen_fpst(tcg_res, tcg_op, fpst);
6085         gen_restore_rmode(tcg_rmode, fpst);
6086     } else {
6087         gen_fpst(tcg_res, tcg_op, fpst);
6088     }
6089 
6090  done:
6091     write_fp_dreg(s, rd, tcg_res);
6092 }
6093 
6094 static void handle_fp_fcvt(DisasContext *s, int opcode,
6095                            int rd, int rn, int dtype, int ntype)
6096 {
6097     switch (ntype) {
6098     case 0x0:
6099     {
6100         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6101         if (dtype == 1) {
6102             /* Single to double */
6103             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6104             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, tcg_env);
6105             write_fp_dreg(s, rd, tcg_rd);
6106         } else {
6107             /* Single to half */
6108             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6109             TCGv_i32 ahp = get_ahp_flag();
6110             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6111 
6112             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6113             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6114             write_fp_sreg(s, rd, tcg_rd);
6115         }
6116         break;
6117     }
6118     case 0x1:
6119     {
6120         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6121         TCGv_i32 tcg_rd = tcg_temp_new_i32();
6122         if (dtype == 0) {
6123             /* Double to single */
6124             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, tcg_env);
6125         } else {
6126             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6127             TCGv_i32 ahp = get_ahp_flag();
6128             /* Double to half */
6129             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6130             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6131         }
6132         write_fp_sreg(s, rd, tcg_rd);
6133         break;
6134     }
6135     case 0x3:
6136     {
6137         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6138         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6139         TCGv_i32 tcg_ahp = get_ahp_flag();
6140         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6141         if (dtype == 0) {
6142             /* Half to single */
6143             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6144             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6145             write_fp_sreg(s, rd, tcg_rd);
6146         } else {
6147             /* Half to double */
6148             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6149             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6150             write_fp_dreg(s, rd, tcg_rd);
6151         }
6152         break;
6153     }
6154     default:
6155         g_assert_not_reached();
6156     }
6157 }
6158 
6159 /* Floating point data-processing (1 source)
6160  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
6161  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6162  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
6163  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6164  */
6165 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6166 {
6167     int mos = extract32(insn, 29, 3);
6168     int type = extract32(insn, 22, 2);
6169     int opcode = extract32(insn, 15, 6);
6170     int rn = extract32(insn, 5, 5);
6171     int rd = extract32(insn, 0, 5);
6172 
6173     if (mos) {
6174         goto do_unallocated;
6175     }
6176 
6177     switch (opcode) {
6178     case 0x4: case 0x5: case 0x7:
6179     {
6180         /* FCVT between half, single and double precision */
6181         int dtype = extract32(opcode, 0, 2);
6182         if (type == 2 || dtype == type) {
6183             goto do_unallocated;
6184         }
6185         if (!fp_access_check(s)) {
6186             return;
6187         }
6188 
6189         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6190         break;
6191     }
6192 
6193     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6194         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6195             goto do_unallocated;
6196         }
6197         /* fall through */
6198     case 0x0 ... 0x3:
6199     case 0x8 ... 0xc:
6200     case 0xe ... 0xf:
6201         /* 32-to-32 and 64-to-64 ops */
6202         switch (type) {
6203         case 0:
6204             if (!fp_access_check(s)) {
6205                 return;
6206             }
6207             handle_fp_1src_single(s, opcode, rd, rn);
6208             break;
6209         case 1:
6210             if (!fp_access_check(s)) {
6211                 return;
6212             }
6213             handle_fp_1src_double(s, opcode, rd, rn);
6214             break;
6215         case 3:
6216             if (!dc_isar_feature(aa64_fp16, s)) {
6217                 goto do_unallocated;
6218             }
6219 
6220             if (!fp_access_check(s)) {
6221                 return;
6222             }
6223             handle_fp_1src_half(s, opcode, rd, rn);
6224             break;
6225         default:
6226             goto do_unallocated;
6227         }
6228         break;
6229 
6230     case 0x6:
6231         switch (type) {
6232         case 1: /* BFCVT */
6233             if (!dc_isar_feature(aa64_bf16, s)) {
6234                 goto do_unallocated;
6235             }
6236             if (!fp_access_check(s)) {
6237                 return;
6238             }
6239             handle_fp_1src_single(s, opcode, rd, rn);
6240             break;
6241         default:
6242             goto do_unallocated;
6243         }
6244         break;
6245 
6246     default:
6247     do_unallocated:
6248         unallocated_encoding(s);
6249         break;
6250     }
6251 }
6252 
6253 /* Floating-point data-processing (2 source) - single precision */
6254 static void handle_fp_2src_single(DisasContext *s, int opcode,
6255                                   int rd, int rn, int rm)
6256 {
6257     TCGv_i32 tcg_op1;
6258     TCGv_i32 tcg_op2;
6259     TCGv_i32 tcg_res;
6260     TCGv_ptr fpst;
6261 
6262     tcg_res = tcg_temp_new_i32();
6263     fpst = fpstatus_ptr(FPST_FPCR);
6264     tcg_op1 = read_fp_sreg(s, rn);
6265     tcg_op2 = read_fp_sreg(s, rm);
6266 
6267     switch (opcode) {
6268     case 0x0: /* FMUL */
6269         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6270         break;
6271     case 0x1: /* FDIV */
6272         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6273         break;
6274     case 0x2: /* FADD */
6275         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6276         break;
6277     case 0x3: /* FSUB */
6278         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6279         break;
6280     case 0x4: /* FMAX */
6281         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6282         break;
6283     case 0x5: /* FMIN */
6284         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6285         break;
6286     case 0x6: /* FMAXNM */
6287         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6288         break;
6289     case 0x7: /* FMINNM */
6290         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6291         break;
6292     case 0x8: /* FNMUL */
6293         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6294         gen_helper_vfp_negs(tcg_res, tcg_res);
6295         break;
6296     }
6297 
6298     write_fp_sreg(s, rd, tcg_res);
6299 }
6300 
6301 /* Floating-point data-processing (2 source) - double precision */
6302 static void handle_fp_2src_double(DisasContext *s, int opcode,
6303                                   int rd, int rn, int rm)
6304 {
6305     TCGv_i64 tcg_op1;
6306     TCGv_i64 tcg_op2;
6307     TCGv_i64 tcg_res;
6308     TCGv_ptr fpst;
6309 
6310     tcg_res = tcg_temp_new_i64();
6311     fpst = fpstatus_ptr(FPST_FPCR);
6312     tcg_op1 = read_fp_dreg(s, rn);
6313     tcg_op2 = read_fp_dreg(s, rm);
6314 
6315     switch (opcode) {
6316     case 0x0: /* FMUL */
6317         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6318         break;
6319     case 0x1: /* FDIV */
6320         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6321         break;
6322     case 0x2: /* FADD */
6323         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6324         break;
6325     case 0x3: /* FSUB */
6326         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6327         break;
6328     case 0x4: /* FMAX */
6329         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6330         break;
6331     case 0x5: /* FMIN */
6332         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6333         break;
6334     case 0x6: /* FMAXNM */
6335         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6336         break;
6337     case 0x7: /* FMINNM */
6338         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6339         break;
6340     case 0x8: /* FNMUL */
6341         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6342         gen_helper_vfp_negd(tcg_res, tcg_res);
6343         break;
6344     }
6345 
6346     write_fp_dreg(s, rd, tcg_res);
6347 }
6348 
6349 /* Floating-point data-processing (2 source) - half precision */
6350 static void handle_fp_2src_half(DisasContext *s, int opcode,
6351                                 int rd, int rn, int rm)
6352 {
6353     TCGv_i32 tcg_op1;
6354     TCGv_i32 tcg_op2;
6355     TCGv_i32 tcg_res;
6356     TCGv_ptr fpst;
6357 
6358     tcg_res = tcg_temp_new_i32();
6359     fpst = fpstatus_ptr(FPST_FPCR_F16);
6360     tcg_op1 = read_fp_hreg(s, rn);
6361     tcg_op2 = read_fp_hreg(s, rm);
6362 
6363     switch (opcode) {
6364     case 0x0: /* FMUL */
6365         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6366         break;
6367     case 0x1: /* FDIV */
6368         gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6369         break;
6370     case 0x2: /* FADD */
6371         gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6372         break;
6373     case 0x3: /* FSUB */
6374         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6375         break;
6376     case 0x4: /* FMAX */
6377         gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6378         break;
6379     case 0x5: /* FMIN */
6380         gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6381         break;
6382     case 0x6: /* FMAXNM */
6383         gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6384         break;
6385     case 0x7: /* FMINNM */
6386         gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6387         break;
6388     case 0x8: /* FNMUL */
6389         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6390         tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6391         break;
6392     default:
6393         g_assert_not_reached();
6394     }
6395 
6396     write_fp_sreg(s, rd, tcg_res);
6397 }
6398 
6399 /* Floating point data-processing (2 source)
6400  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
6401  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6402  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
6403  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6404  */
6405 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6406 {
6407     int mos = extract32(insn, 29, 3);
6408     int type = extract32(insn, 22, 2);
6409     int rd = extract32(insn, 0, 5);
6410     int rn = extract32(insn, 5, 5);
6411     int rm = extract32(insn, 16, 5);
6412     int opcode = extract32(insn, 12, 4);
6413 
6414     if (opcode > 8 || mos) {
6415         unallocated_encoding(s);
6416         return;
6417     }
6418 
6419     switch (type) {
6420     case 0:
6421         if (!fp_access_check(s)) {
6422             return;
6423         }
6424         handle_fp_2src_single(s, opcode, rd, rn, rm);
6425         break;
6426     case 1:
6427         if (!fp_access_check(s)) {
6428             return;
6429         }
6430         handle_fp_2src_double(s, opcode, rd, rn, rm);
6431         break;
6432     case 3:
6433         if (!dc_isar_feature(aa64_fp16, s)) {
6434             unallocated_encoding(s);
6435             return;
6436         }
6437         if (!fp_access_check(s)) {
6438             return;
6439         }
6440         handle_fp_2src_half(s, opcode, rd, rn, rm);
6441         break;
6442     default:
6443         unallocated_encoding(s);
6444     }
6445 }
6446 
6447 /* Floating-point data-processing (3 source) - single precision */
6448 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6449                                   int rd, int rn, int rm, int ra)
6450 {
6451     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6452     TCGv_i32 tcg_res = tcg_temp_new_i32();
6453     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6454 
6455     tcg_op1 = read_fp_sreg(s, rn);
6456     tcg_op2 = read_fp_sreg(s, rm);
6457     tcg_op3 = read_fp_sreg(s, ra);
6458 
6459     /* These are fused multiply-add, and must be done as one
6460      * floating point operation with no rounding between the
6461      * multiplication and addition steps.
6462      * NB that doing the negations here as separate steps is
6463      * correct : an input NaN should come out with its sign bit
6464      * flipped if it is a negated-input.
6465      */
6466     if (o1 == true) {
6467         gen_helper_vfp_negs(tcg_op3, tcg_op3);
6468     }
6469 
6470     if (o0 != o1) {
6471         gen_helper_vfp_negs(tcg_op1, tcg_op1);
6472     }
6473 
6474     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6475 
6476     write_fp_sreg(s, rd, tcg_res);
6477 }
6478 
6479 /* Floating-point data-processing (3 source) - double precision */
6480 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6481                                   int rd, int rn, int rm, int ra)
6482 {
6483     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6484     TCGv_i64 tcg_res = tcg_temp_new_i64();
6485     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6486 
6487     tcg_op1 = read_fp_dreg(s, rn);
6488     tcg_op2 = read_fp_dreg(s, rm);
6489     tcg_op3 = read_fp_dreg(s, ra);
6490 
6491     /* These are fused multiply-add, and must be done as one
6492      * floating point operation with no rounding between the
6493      * multiplication and addition steps.
6494      * NB that doing the negations here as separate steps is
6495      * correct : an input NaN should come out with its sign bit
6496      * flipped if it is a negated-input.
6497      */
6498     if (o1 == true) {
6499         gen_helper_vfp_negd(tcg_op3, tcg_op3);
6500     }
6501 
6502     if (o0 != o1) {
6503         gen_helper_vfp_negd(tcg_op1, tcg_op1);
6504     }
6505 
6506     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6507 
6508     write_fp_dreg(s, rd, tcg_res);
6509 }
6510 
6511 /* Floating-point data-processing (3 source) - half precision */
6512 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6513                                 int rd, int rn, int rm, int ra)
6514 {
6515     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6516     TCGv_i32 tcg_res = tcg_temp_new_i32();
6517     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6518 
6519     tcg_op1 = read_fp_hreg(s, rn);
6520     tcg_op2 = read_fp_hreg(s, rm);
6521     tcg_op3 = read_fp_hreg(s, ra);
6522 
6523     /* These are fused multiply-add, and must be done as one
6524      * floating point operation with no rounding between the
6525      * multiplication and addition steps.
6526      * NB that doing the negations here as separate steps is
6527      * correct : an input NaN should come out with its sign bit
6528      * flipped if it is a negated-input.
6529      */
6530     if (o1 == true) {
6531         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6532     }
6533 
6534     if (o0 != o1) {
6535         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6536     }
6537 
6538     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6539 
6540     write_fp_sreg(s, rd, tcg_res);
6541 }
6542 
6543 /* Floating point data-processing (3 source)
6544  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
6545  * +---+---+---+-----------+------+----+------+----+------+------+------+
6546  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6547  * +---+---+---+-----------+------+----+------+----+------+------+------+
6548  */
6549 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6550 {
6551     int mos = extract32(insn, 29, 3);
6552     int type = extract32(insn, 22, 2);
6553     int rd = extract32(insn, 0, 5);
6554     int rn = extract32(insn, 5, 5);
6555     int ra = extract32(insn, 10, 5);
6556     int rm = extract32(insn, 16, 5);
6557     bool o0 = extract32(insn, 15, 1);
6558     bool o1 = extract32(insn, 21, 1);
6559 
6560     if (mos) {
6561         unallocated_encoding(s);
6562         return;
6563     }
6564 
6565     switch (type) {
6566     case 0:
6567         if (!fp_access_check(s)) {
6568             return;
6569         }
6570         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6571         break;
6572     case 1:
6573         if (!fp_access_check(s)) {
6574             return;
6575         }
6576         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6577         break;
6578     case 3:
6579         if (!dc_isar_feature(aa64_fp16, s)) {
6580             unallocated_encoding(s);
6581             return;
6582         }
6583         if (!fp_access_check(s)) {
6584             return;
6585         }
6586         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6587         break;
6588     default:
6589         unallocated_encoding(s);
6590     }
6591 }
6592 
6593 /* Floating point immediate
6594  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
6595  * +---+---+---+-----------+------+---+------------+-------+------+------+
6596  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
6597  * +---+---+---+-----------+------+---+------------+-------+------+------+
6598  */
6599 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6600 {
6601     int rd = extract32(insn, 0, 5);
6602     int imm5 = extract32(insn, 5, 5);
6603     int imm8 = extract32(insn, 13, 8);
6604     int type = extract32(insn, 22, 2);
6605     int mos = extract32(insn, 29, 3);
6606     uint64_t imm;
6607     MemOp sz;
6608 
6609     if (mos || imm5) {
6610         unallocated_encoding(s);
6611         return;
6612     }
6613 
6614     switch (type) {
6615     case 0:
6616         sz = MO_32;
6617         break;
6618     case 1:
6619         sz = MO_64;
6620         break;
6621     case 3:
6622         sz = MO_16;
6623         if (dc_isar_feature(aa64_fp16, s)) {
6624             break;
6625         }
6626         /* fallthru */
6627     default:
6628         unallocated_encoding(s);
6629         return;
6630     }
6631 
6632     if (!fp_access_check(s)) {
6633         return;
6634     }
6635 
6636     imm = vfp_expand_imm(sz, imm8);
6637     write_fp_dreg(s, rd, tcg_constant_i64(imm));
6638 }
6639 
6640 /* Handle floating point <=> fixed point conversions. Note that we can
6641  * also deal with fp <=> integer conversions as a special case (scale == 64)
6642  * OPTME: consider handling that special case specially or at least skipping
6643  * the call to scalbn in the helpers for zero shifts.
6644  */
6645 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6646                            bool itof, int rmode, int scale, int sf, int type)
6647 {
6648     bool is_signed = !(opcode & 1);
6649     TCGv_ptr tcg_fpstatus;
6650     TCGv_i32 tcg_shift, tcg_single;
6651     TCGv_i64 tcg_double;
6652 
6653     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
6654 
6655     tcg_shift = tcg_constant_i32(64 - scale);
6656 
6657     if (itof) {
6658         TCGv_i64 tcg_int = cpu_reg(s, rn);
6659         if (!sf) {
6660             TCGv_i64 tcg_extend = tcg_temp_new_i64();
6661 
6662             if (is_signed) {
6663                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6664             } else {
6665                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6666             }
6667 
6668             tcg_int = tcg_extend;
6669         }
6670 
6671         switch (type) {
6672         case 1: /* float64 */
6673             tcg_double = tcg_temp_new_i64();
6674             if (is_signed) {
6675                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6676                                      tcg_shift, tcg_fpstatus);
6677             } else {
6678                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6679                                      tcg_shift, tcg_fpstatus);
6680             }
6681             write_fp_dreg(s, rd, tcg_double);
6682             break;
6683 
6684         case 0: /* float32 */
6685             tcg_single = tcg_temp_new_i32();
6686             if (is_signed) {
6687                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6688                                      tcg_shift, tcg_fpstatus);
6689             } else {
6690                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6691                                      tcg_shift, tcg_fpstatus);
6692             }
6693             write_fp_sreg(s, rd, tcg_single);
6694             break;
6695 
6696         case 3: /* float16 */
6697             tcg_single = tcg_temp_new_i32();
6698             if (is_signed) {
6699                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6700                                      tcg_shift, tcg_fpstatus);
6701             } else {
6702                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6703                                      tcg_shift, tcg_fpstatus);
6704             }
6705             write_fp_sreg(s, rd, tcg_single);
6706             break;
6707 
6708         default:
6709             g_assert_not_reached();
6710         }
6711     } else {
6712         TCGv_i64 tcg_int = cpu_reg(s, rd);
6713         TCGv_i32 tcg_rmode;
6714 
6715         if (extract32(opcode, 2, 1)) {
6716             /* There are too many rounding modes to all fit into rmode,
6717              * so FCVTA[US] is a special case.
6718              */
6719             rmode = FPROUNDING_TIEAWAY;
6720         }
6721 
6722         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
6723 
6724         switch (type) {
6725         case 1: /* float64 */
6726             tcg_double = read_fp_dreg(s, rn);
6727             if (is_signed) {
6728                 if (!sf) {
6729                     gen_helper_vfp_tosld(tcg_int, tcg_double,
6730                                          tcg_shift, tcg_fpstatus);
6731                 } else {
6732                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
6733                                          tcg_shift, tcg_fpstatus);
6734                 }
6735             } else {
6736                 if (!sf) {
6737                     gen_helper_vfp_tould(tcg_int, tcg_double,
6738                                          tcg_shift, tcg_fpstatus);
6739                 } else {
6740                     gen_helper_vfp_touqd(tcg_int, tcg_double,
6741                                          tcg_shift, tcg_fpstatus);
6742                 }
6743             }
6744             if (!sf) {
6745                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6746             }
6747             break;
6748 
6749         case 0: /* float32 */
6750             tcg_single = read_fp_sreg(s, rn);
6751             if (sf) {
6752                 if (is_signed) {
6753                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
6754                                          tcg_shift, tcg_fpstatus);
6755                 } else {
6756                     gen_helper_vfp_touqs(tcg_int, tcg_single,
6757                                          tcg_shift, tcg_fpstatus);
6758                 }
6759             } else {
6760                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6761                 if (is_signed) {
6762                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
6763                                          tcg_shift, tcg_fpstatus);
6764                 } else {
6765                     gen_helper_vfp_touls(tcg_dest, tcg_single,
6766                                          tcg_shift, tcg_fpstatus);
6767                 }
6768                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6769             }
6770             break;
6771 
6772         case 3: /* float16 */
6773             tcg_single = read_fp_sreg(s, rn);
6774             if (sf) {
6775                 if (is_signed) {
6776                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
6777                                          tcg_shift, tcg_fpstatus);
6778                 } else {
6779                     gen_helper_vfp_touqh(tcg_int, tcg_single,
6780                                          tcg_shift, tcg_fpstatus);
6781                 }
6782             } else {
6783                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6784                 if (is_signed) {
6785                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
6786                                          tcg_shift, tcg_fpstatus);
6787                 } else {
6788                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
6789                                          tcg_shift, tcg_fpstatus);
6790                 }
6791                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6792             }
6793             break;
6794 
6795         default:
6796             g_assert_not_reached();
6797         }
6798 
6799         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
6800     }
6801 }
6802 
6803 /* Floating point <-> fixed point conversions
6804  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
6805  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6806  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
6807  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6808  */
6809 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
6810 {
6811     int rd = extract32(insn, 0, 5);
6812     int rn = extract32(insn, 5, 5);
6813     int scale = extract32(insn, 10, 6);
6814     int opcode = extract32(insn, 16, 3);
6815     int rmode = extract32(insn, 19, 2);
6816     int type = extract32(insn, 22, 2);
6817     bool sbit = extract32(insn, 29, 1);
6818     bool sf = extract32(insn, 31, 1);
6819     bool itof;
6820 
6821     if (sbit || (!sf && scale < 32)) {
6822         unallocated_encoding(s);
6823         return;
6824     }
6825 
6826     switch (type) {
6827     case 0: /* float32 */
6828     case 1: /* float64 */
6829         break;
6830     case 3: /* float16 */
6831         if (dc_isar_feature(aa64_fp16, s)) {
6832             break;
6833         }
6834         /* fallthru */
6835     default:
6836         unallocated_encoding(s);
6837         return;
6838     }
6839 
6840     switch ((rmode << 3) | opcode) {
6841     case 0x2: /* SCVTF */
6842     case 0x3: /* UCVTF */
6843         itof = true;
6844         break;
6845     case 0x18: /* FCVTZS */
6846     case 0x19: /* FCVTZU */
6847         itof = false;
6848         break;
6849     default:
6850         unallocated_encoding(s);
6851         return;
6852     }
6853 
6854     if (!fp_access_check(s)) {
6855         return;
6856     }
6857 
6858     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
6859 }
6860 
6861 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
6862 {
6863     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
6864      * without conversion.
6865      */
6866 
6867     if (itof) {
6868         TCGv_i64 tcg_rn = cpu_reg(s, rn);
6869         TCGv_i64 tmp;
6870 
6871         switch (type) {
6872         case 0:
6873             /* 32 bit */
6874             tmp = tcg_temp_new_i64();
6875             tcg_gen_ext32u_i64(tmp, tcg_rn);
6876             write_fp_dreg(s, rd, tmp);
6877             break;
6878         case 1:
6879             /* 64 bit */
6880             write_fp_dreg(s, rd, tcg_rn);
6881             break;
6882         case 2:
6883             /* 64 bit to top half. */
6884             tcg_gen_st_i64(tcg_rn, tcg_env, fp_reg_hi_offset(s, rd));
6885             clear_vec_high(s, true, rd);
6886             break;
6887         case 3:
6888             /* 16 bit */
6889             tmp = tcg_temp_new_i64();
6890             tcg_gen_ext16u_i64(tmp, tcg_rn);
6891             write_fp_dreg(s, rd, tmp);
6892             break;
6893         default:
6894             g_assert_not_reached();
6895         }
6896     } else {
6897         TCGv_i64 tcg_rd = cpu_reg(s, rd);
6898 
6899         switch (type) {
6900         case 0:
6901             /* 32 bit */
6902             tcg_gen_ld32u_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_32));
6903             break;
6904         case 1:
6905             /* 64 bit */
6906             tcg_gen_ld_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_64));
6907             break;
6908         case 2:
6909             /* 64 bits from top half */
6910             tcg_gen_ld_i64(tcg_rd, tcg_env, fp_reg_hi_offset(s, rn));
6911             break;
6912         case 3:
6913             /* 16 bit */
6914             tcg_gen_ld16u_i64(tcg_rd, tcg_env, fp_reg_offset(s, rn, MO_16));
6915             break;
6916         default:
6917             g_assert_not_reached();
6918         }
6919     }
6920 }
6921 
6922 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
6923 {
6924     TCGv_i64 t = read_fp_dreg(s, rn);
6925     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
6926 
6927     gen_helper_fjcvtzs(t, t, fpstatus);
6928 
6929     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
6930     tcg_gen_extrh_i64_i32(cpu_ZF, t);
6931     tcg_gen_movi_i32(cpu_CF, 0);
6932     tcg_gen_movi_i32(cpu_NF, 0);
6933     tcg_gen_movi_i32(cpu_VF, 0);
6934 }
6935 
6936 /* Floating point <-> integer conversions
6937  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
6938  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
6939  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
6940  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
6941  */
6942 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
6943 {
6944     int rd = extract32(insn, 0, 5);
6945     int rn = extract32(insn, 5, 5);
6946     int opcode = extract32(insn, 16, 3);
6947     int rmode = extract32(insn, 19, 2);
6948     int type = extract32(insn, 22, 2);
6949     bool sbit = extract32(insn, 29, 1);
6950     bool sf = extract32(insn, 31, 1);
6951     bool itof = false;
6952 
6953     if (sbit) {
6954         goto do_unallocated;
6955     }
6956 
6957     switch (opcode) {
6958     case 2: /* SCVTF */
6959     case 3: /* UCVTF */
6960         itof = true;
6961         /* fallthru */
6962     case 4: /* FCVTAS */
6963     case 5: /* FCVTAU */
6964         if (rmode != 0) {
6965             goto do_unallocated;
6966         }
6967         /* fallthru */
6968     case 0: /* FCVT[NPMZ]S */
6969     case 1: /* FCVT[NPMZ]U */
6970         switch (type) {
6971         case 0: /* float32 */
6972         case 1: /* float64 */
6973             break;
6974         case 3: /* float16 */
6975             if (!dc_isar_feature(aa64_fp16, s)) {
6976                 goto do_unallocated;
6977             }
6978             break;
6979         default:
6980             goto do_unallocated;
6981         }
6982         if (!fp_access_check(s)) {
6983             return;
6984         }
6985         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
6986         break;
6987 
6988     default:
6989         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
6990         case 0b01100110: /* FMOV half <-> 32-bit int */
6991         case 0b01100111:
6992         case 0b11100110: /* FMOV half <-> 64-bit int */
6993         case 0b11100111:
6994             if (!dc_isar_feature(aa64_fp16, s)) {
6995                 goto do_unallocated;
6996             }
6997             /* fallthru */
6998         case 0b00000110: /* FMOV 32-bit */
6999         case 0b00000111:
7000         case 0b10100110: /* FMOV 64-bit */
7001         case 0b10100111:
7002         case 0b11001110: /* FMOV top half of 128-bit */
7003         case 0b11001111:
7004             if (!fp_access_check(s)) {
7005                 return;
7006             }
7007             itof = opcode & 1;
7008             handle_fmov(s, rd, rn, type, itof);
7009             break;
7010 
7011         case 0b00111110: /* FJCVTZS */
7012             if (!dc_isar_feature(aa64_jscvt, s)) {
7013                 goto do_unallocated;
7014             } else if (fp_access_check(s)) {
7015                 handle_fjcvtzs(s, rd, rn);
7016             }
7017             break;
7018 
7019         default:
7020         do_unallocated:
7021             unallocated_encoding(s);
7022             return;
7023         }
7024         break;
7025     }
7026 }
7027 
7028 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7029  *   31  30  29 28     25 24                          0
7030  * +---+---+---+---------+-----------------------------+
7031  * |   | 0 |   | 1 1 1 1 |                             |
7032  * +---+---+---+---------+-----------------------------+
7033  */
7034 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7035 {
7036     if (extract32(insn, 24, 1)) {
7037         /* Floating point data-processing (3 source) */
7038         disas_fp_3src(s, insn);
7039     } else if (extract32(insn, 21, 1) == 0) {
7040         /* Floating point to fixed point conversions */
7041         disas_fp_fixed_conv(s, insn);
7042     } else {
7043         switch (extract32(insn, 10, 2)) {
7044         case 1:
7045             /* Floating point conditional compare */
7046             disas_fp_ccomp(s, insn);
7047             break;
7048         case 2:
7049             /* Floating point data-processing (2 source) */
7050             disas_fp_2src(s, insn);
7051             break;
7052         case 3:
7053             /* Floating point conditional select */
7054             disas_fp_csel(s, insn);
7055             break;
7056         case 0:
7057             switch (ctz32(extract32(insn, 12, 4))) {
7058             case 0: /* [15:12] == xxx1 */
7059                 /* Floating point immediate */
7060                 disas_fp_imm(s, insn);
7061                 break;
7062             case 1: /* [15:12] == xx10 */
7063                 /* Floating point compare */
7064                 disas_fp_compare(s, insn);
7065                 break;
7066             case 2: /* [15:12] == x100 */
7067                 /* Floating point data-processing (1 source) */
7068                 disas_fp_1src(s, insn);
7069                 break;
7070             case 3: /* [15:12] == 1000 */
7071                 unallocated_encoding(s);
7072                 break;
7073             default: /* [15:12] == 0000 */
7074                 /* Floating point <-> integer conversions */
7075                 disas_fp_int_conv(s, insn);
7076                 break;
7077             }
7078             break;
7079         }
7080     }
7081 }
7082 
7083 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7084                      int pos)
7085 {
7086     /* Extract 64 bits from the middle of two concatenated 64 bit
7087      * vector register slices left:right. The extracted bits start
7088      * at 'pos' bits into the right (least significant) side.
7089      * We return the result in tcg_right, and guarantee not to
7090      * trash tcg_left.
7091      */
7092     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7093     assert(pos > 0 && pos < 64);
7094 
7095     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7096     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7097     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7098 }
7099 
7100 /* EXT
7101  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
7102  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7103  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
7104  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7105  */
7106 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7107 {
7108     int is_q = extract32(insn, 30, 1);
7109     int op2 = extract32(insn, 22, 2);
7110     int imm4 = extract32(insn, 11, 4);
7111     int rm = extract32(insn, 16, 5);
7112     int rn = extract32(insn, 5, 5);
7113     int rd = extract32(insn, 0, 5);
7114     int pos = imm4 << 3;
7115     TCGv_i64 tcg_resl, tcg_resh;
7116 
7117     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7118         unallocated_encoding(s);
7119         return;
7120     }
7121 
7122     if (!fp_access_check(s)) {
7123         return;
7124     }
7125 
7126     tcg_resh = tcg_temp_new_i64();
7127     tcg_resl = tcg_temp_new_i64();
7128 
7129     /* Vd gets bits starting at pos bits into Vm:Vn. This is
7130      * either extracting 128 bits from a 128:128 concatenation, or
7131      * extracting 64 bits from a 64:64 concatenation.
7132      */
7133     if (!is_q) {
7134         read_vec_element(s, tcg_resl, rn, 0, MO_64);
7135         if (pos != 0) {
7136             read_vec_element(s, tcg_resh, rm, 0, MO_64);
7137             do_ext64(s, tcg_resh, tcg_resl, pos);
7138         }
7139     } else {
7140         TCGv_i64 tcg_hh;
7141         typedef struct {
7142             int reg;
7143             int elt;
7144         } EltPosns;
7145         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7146         EltPosns *elt = eltposns;
7147 
7148         if (pos >= 64) {
7149             elt++;
7150             pos -= 64;
7151         }
7152 
7153         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7154         elt++;
7155         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7156         elt++;
7157         if (pos != 0) {
7158             do_ext64(s, tcg_resh, tcg_resl, pos);
7159             tcg_hh = tcg_temp_new_i64();
7160             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7161             do_ext64(s, tcg_hh, tcg_resh, pos);
7162         }
7163     }
7164 
7165     write_vec_element(s, tcg_resl, rd, 0, MO_64);
7166     if (is_q) {
7167         write_vec_element(s, tcg_resh, rd, 1, MO_64);
7168     }
7169     clear_vec_high(s, is_q, rd);
7170 }
7171 
7172 /* TBL/TBX
7173  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
7174  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7175  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
7176  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7177  */
7178 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7179 {
7180     int op2 = extract32(insn, 22, 2);
7181     int is_q = extract32(insn, 30, 1);
7182     int rm = extract32(insn, 16, 5);
7183     int rn = extract32(insn, 5, 5);
7184     int rd = extract32(insn, 0, 5);
7185     int is_tbx = extract32(insn, 12, 1);
7186     int len = (extract32(insn, 13, 2) + 1) * 16;
7187 
7188     if (op2 != 0) {
7189         unallocated_encoding(s);
7190         return;
7191     }
7192 
7193     if (!fp_access_check(s)) {
7194         return;
7195     }
7196 
7197     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7198                        vec_full_reg_offset(s, rm), tcg_env,
7199                        is_q ? 16 : 8, vec_full_reg_size(s),
7200                        (len << 6) | (is_tbx << 5) | rn,
7201                        gen_helper_simd_tblx);
7202 }
7203 
7204 /* ZIP/UZP/TRN
7205  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
7206  * +---+---+-------------+------+---+------+---+------------------+------+
7207  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
7208  * +---+---+-------------+------+---+------+---+------------------+------+
7209  */
7210 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7211 {
7212     int rd = extract32(insn, 0, 5);
7213     int rn = extract32(insn, 5, 5);
7214     int rm = extract32(insn, 16, 5);
7215     int size = extract32(insn, 22, 2);
7216     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7217      * bit 2 indicates 1 vs 2 variant of the insn.
7218      */
7219     int opcode = extract32(insn, 12, 2);
7220     bool part = extract32(insn, 14, 1);
7221     bool is_q = extract32(insn, 30, 1);
7222     int esize = 8 << size;
7223     int i;
7224     int datasize = is_q ? 128 : 64;
7225     int elements = datasize / esize;
7226     TCGv_i64 tcg_res[2], tcg_ele;
7227 
7228     if (opcode == 0 || (size == 3 && !is_q)) {
7229         unallocated_encoding(s);
7230         return;
7231     }
7232 
7233     if (!fp_access_check(s)) {
7234         return;
7235     }
7236 
7237     tcg_res[0] = tcg_temp_new_i64();
7238     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
7239     tcg_ele = tcg_temp_new_i64();
7240 
7241     for (i = 0; i < elements; i++) {
7242         int o, w;
7243 
7244         switch (opcode) {
7245         case 1: /* UZP1/2 */
7246         {
7247             int midpoint = elements / 2;
7248             if (i < midpoint) {
7249                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
7250             } else {
7251                 read_vec_element(s, tcg_ele, rm,
7252                                  2 * (i - midpoint) + part, size);
7253             }
7254             break;
7255         }
7256         case 2: /* TRN1/2 */
7257             if (i & 1) {
7258                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
7259             } else {
7260                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
7261             }
7262             break;
7263         case 3: /* ZIP1/2 */
7264         {
7265             int base = part * elements / 2;
7266             if (i & 1) {
7267                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
7268             } else {
7269                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
7270             }
7271             break;
7272         }
7273         default:
7274             g_assert_not_reached();
7275         }
7276 
7277         w = (i * esize) / 64;
7278         o = (i * esize) % 64;
7279         if (o == 0) {
7280             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
7281         } else {
7282             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
7283             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
7284         }
7285     }
7286 
7287     for (i = 0; i <= is_q; ++i) {
7288         write_vec_element(s, tcg_res[i], rd, i, MO_64);
7289     }
7290     clear_vec_high(s, is_q, rd);
7291 }
7292 
7293 /*
7294  * do_reduction_op helper
7295  *
7296  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7297  * important for correct NaN propagation that we do these
7298  * operations in exactly the order specified by the pseudocode.
7299  *
7300  * This is a recursive function, TCG temps should be freed by the
7301  * calling function once it is done with the values.
7302  */
7303 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7304                                 int esize, int size, int vmap, TCGv_ptr fpst)
7305 {
7306     if (esize == size) {
7307         int element;
7308         MemOp msize = esize == 16 ? MO_16 : MO_32;
7309         TCGv_i32 tcg_elem;
7310 
7311         /* We should have one register left here */
7312         assert(ctpop8(vmap) == 1);
7313         element = ctz32(vmap);
7314         assert(element < 8);
7315 
7316         tcg_elem = tcg_temp_new_i32();
7317         read_vec_element_i32(s, tcg_elem, rn, element, msize);
7318         return tcg_elem;
7319     } else {
7320         int bits = size / 2;
7321         int shift = ctpop8(vmap) / 2;
7322         int vmap_lo = (vmap >> shift) & vmap;
7323         int vmap_hi = (vmap & ~vmap_lo);
7324         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7325 
7326         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7327         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7328         tcg_res = tcg_temp_new_i32();
7329 
7330         switch (fpopcode) {
7331         case 0x0c: /* fmaxnmv half-precision */
7332             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7333             break;
7334         case 0x0f: /* fmaxv half-precision */
7335             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7336             break;
7337         case 0x1c: /* fminnmv half-precision */
7338             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7339             break;
7340         case 0x1f: /* fminv half-precision */
7341             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7342             break;
7343         case 0x2c: /* fmaxnmv */
7344             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7345             break;
7346         case 0x2f: /* fmaxv */
7347             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7348             break;
7349         case 0x3c: /* fminnmv */
7350             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7351             break;
7352         case 0x3f: /* fminv */
7353             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7354             break;
7355         default:
7356             g_assert_not_reached();
7357         }
7358         return tcg_res;
7359     }
7360 }
7361 
7362 /* AdvSIMD across lanes
7363  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7364  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7365  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7366  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7367  */
7368 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7369 {
7370     int rd = extract32(insn, 0, 5);
7371     int rn = extract32(insn, 5, 5);
7372     int size = extract32(insn, 22, 2);
7373     int opcode = extract32(insn, 12, 5);
7374     bool is_q = extract32(insn, 30, 1);
7375     bool is_u = extract32(insn, 29, 1);
7376     bool is_fp = false;
7377     bool is_min = false;
7378     int esize;
7379     int elements;
7380     int i;
7381     TCGv_i64 tcg_res, tcg_elt;
7382 
7383     switch (opcode) {
7384     case 0x1b: /* ADDV */
7385         if (is_u) {
7386             unallocated_encoding(s);
7387             return;
7388         }
7389         /* fall through */
7390     case 0x3: /* SADDLV, UADDLV */
7391     case 0xa: /* SMAXV, UMAXV */
7392     case 0x1a: /* SMINV, UMINV */
7393         if (size == 3 || (size == 2 && !is_q)) {
7394             unallocated_encoding(s);
7395             return;
7396         }
7397         break;
7398     case 0xc: /* FMAXNMV, FMINNMV */
7399     case 0xf: /* FMAXV, FMINV */
7400         /* Bit 1 of size field encodes min vs max and the actual size
7401          * depends on the encoding of the U bit. If not set (and FP16
7402          * enabled) then we do half-precision float instead of single
7403          * precision.
7404          */
7405         is_min = extract32(size, 1, 1);
7406         is_fp = true;
7407         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7408             size = 1;
7409         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7410             unallocated_encoding(s);
7411             return;
7412         } else {
7413             size = 2;
7414         }
7415         break;
7416     default:
7417         unallocated_encoding(s);
7418         return;
7419     }
7420 
7421     if (!fp_access_check(s)) {
7422         return;
7423     }
7424 
7425     esize = 8 << size;
7426     elements = (is_q ? 128 : 64) / esize;
7427 
7428     tcg_res = tcg_temp_new_i64();
7429     tcg_elt = tcg_temp_new_i64();
7430 
7431     /* These instructions operate across all lanes of a vector
7432      * to produce a single result. We can guarantee that a 64
7433      * bit intermediate is sufficient:
7434      *  + for [US]ADDLV the maximum element size is 32 bits, and
7435      *    the result type is 64 bits
7436      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7437      *    same as the element size, which is 32 bits at most
7438      * For the integer operations we can choose to work at 64
7439      * or 32 bits and truncate at the end; for simplicity
7440      * we use 64 bits always. The floating point
7441      * ops do require 32 bit intermediates, though.
7442      */
7443     if (!is_fp) {
7444         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7445 
7446         for (i = 1; i < elements; i++) {
7447             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7448 
7449             switch (opcode) {
7450             case 0x03: /* SADDLV / UADDLV */
7451             case 0x1b: /* ADDV */
7452                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7453                 break;
7454             case 0x0a: /* SMAXV / UMAXV */
7455                 if (is_u) {
7456                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7457                 } else {
7458                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7459                 }
7460                 break;
7461             case 0x1a: /* SMINV / UMINV */
7462                 if (is_u) {
7463                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7464                 } else {
7465                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7466                 }
7467                 break;
7468             default:
7469                 g_assert_not_reached();
7470             }
7471 
7472         }
7473     } else {
7474         /* Floating point vector reduction ops which work across 32
7475          * bit (single) or 16 bit (half-precision) intermediates.
7476          * Note that correct NaN propagation requires that we do these
7477          * operations in exactly the order specified by the pseudocode.
7478          */
7479         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7480         int fpopcode = opcode | is_min << 4 | is_u << 5;
7481         int vmap = (1 << elements) - 1;
7482         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7483                                              (is_q ? 128 : 64), vmap, fpst);
7484         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7485     }
7486 
7487     /* Now truncate the result to the width required for the final output */
7488     if (opcode == 0x03) {
7489         /* SADDLV, UADDLV: result is 2*esize */
7490         size++;
7491     }
7492 
7493     switch (size) {
7494     case 0:
7495         tcg_gen_ext8u_i64(tcg_res, tcg_res);
7496         break;
7497     case 1:
7498         tcg_gen_ext16u_i64(tcg_res, tcg_res);
7499         break;
7500     case 2:
7501         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7502         break;
7503     case 3:
7504         break;
7505     default:
7506         g_assert_not_reached();
7507     }
7508 
7509     write_fp_dreg(s, rd, tcg_res);
7510 }
7511 
7512 /* DUP (Element, Vector)
7513  *
7514  *  31  30   29              21 20    16 15        10  9    5 4    0
7515  * +---+---+-------------------+--------+-------------+------+------+
7516  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7517  * +---+---+-------------------+--------+-------------+------+------+
7518  *
7519  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7520  */
7521 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7522                              int imm5)
7523 {
7524     int size = ctz32(imm5);
7525     int index;
7526 
7527     if (size > 3 || (size == 3 && !is_q)) {
7528         unallocated_encoding(s);
7529         return;
7530     }
7531 
7532     if (!fp_access_check(s)) {
7533         return;
7534     }
7535 
7536     index = imm5 >> (size + 1);
7537     tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7538                          vec_reg_offset(s, rn, index, size),
7539                          is_q ? 16 : 8, vec_full_reg_size(s));
7540 }
7541 
7542 /* DUP (element, scalar)
7543  *  31                   21 20    16 15        10  9    5 4    0
7544  * +-----------------------+--------+-------------+------+------+
7545  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7546  * +-----------------------+--------+-------------+------+------+
7547  */
7548 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7549                               int imm5)
7550 {
7551     int size = ctz32(imm5);
7552     int index;
7553     TCGv_i64 tmp;
7554 
7555     if (size > 3) {
7556         unallocated_encoding(s);
7557         return;
7558     }
7559 
7560     if (!fp_access_check(s)) {
7561         return;
7562     }
7563 
7564     index = imm5 >> (size + 1);
7565 
7566     /* This instruction just extracts the specified element and
7567      * zero-extends it into the bottom of the destination register.
7568      */
7569     tmp = tcg_temp_new_i64();
7570     read_vec_element(s, tmp, rn, index, size);
7571     write_fp_dreg(s, rd, tmp);
7572 }
7573 
7574 /* DUP (General)
7575  *
7576  *  31  30   29              21 20    16 15        10  9    5 4    0
7577  * +---+---+-------------------+--------+-------------+------+------+
7578  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
7579  * +---+---+-------------------+--------+-------------+------+------+
7580  *
7581  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7582  */
7583 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7584                              int imm5)
7585 {
7586     int size = ctz32(imm5);
7587     uint32_t dofs, oprsz, maxsz;
7588 
7589     if (size > 3 || ((size == 3) && !is_q)) {
7590         unallocated_encoding(s);
7591         return;
7592     }
7593 
7594     if (!fp_access_check(s)) {
7595         return;
7596     }
7597 
7598     dofs = vec_full_reg_offset(s, rd);
7599     oprsz = is_q ? 16 : 8;
7600     maxsz = vec_full_reg_size(s);
7601 
7602     tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7603 }
7604 
7605 /* INS (Element)
7606  *
7607  *  31                   21 20    16 15  14    11  10 9    5 4    0
7608  * +-----------------------+--------+------------+---+------+------+
7609  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
7610  * +-----------------------+--------+------------+---+------+------+
7611  *
7612  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7613  * index: encoded in imm5<4:size+1>
7614  */
7615 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7616                              int imm4, int imm5)
7617 {
7618     int size = ctz32(imm5);
7619     int src_index, dst_index;
7620     TCGv_i64 tmp;
7621 
7622     if (size > 3) {
7623         unallocated_encoding(s);
7624         return;
7625     }
7626 
7627     if (!fp_access_check(s)) {
7628         return;
7629     }
7630 
7631     dst_index = extract32(imm5, 1+size, 5);
7632     src_index = extract32(imm4, size, 4);
7633 
7634     tmp = tcg_temp_new_i64();
7635 
7636     read_vec_element(s, tmp, rn, src_index, size);
7637     write_vec_element(s, tmp, rd, dst_index, size);
7638 
7639     /* INS is considered a 128-bit write for SVE. */
7640     clear_vec_high(s, true, rd);
7641 }
7642 
7643 
7644 /* INS (General)
7645  *
7646  *  31                   21 20    16 15        10  9    5 4    0
7647  * +-----------------------+--------+-------------+------+------+
7648  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
7649  * +-----------------------+--------+-------------+------+------+
7650  *
7651  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7652  * index: encoded in imm5<4:size+1>
7653  */
7654 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7655 {
7656     int size = ctz32(imm5);
7657     int idx;
7658 
7659     if (size > 3) {
7660         unallocated_encoding(s);
7661         return;
7662     }
7663 
7664     if (!fp_access_check(s)) {
7665         return;
7666     }
7667 
7668     idx = extract32(imm5, 1 + size, 4 - size);
7669     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7670 
7671     /* INS is considered a 128-bit write for SVE. */
7672     clear_vec_high(s, true, rd);
7673 }
7674 
7675 /*
7676  * UMOV (General)
7677  * SMOV (General)
7678  *
7679  *  31  30   29              21 20    16 15    12   10 9    5 4    0
7680  * +---+---+-------------------+--------+-------------+------+------+
7681  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
7682  * +---+---+-------------------+--------+-------------+------+------+
7683  *
7684  * U: unsigned when set
7685  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7686  */
7687 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7688                                   int rn, int rd, int imm5)
7689 {
7690     int size = ctz32(imm5);
7691     int element;
7692     TCGv_i64 tcg_rd;
7693 
7694     /* Check for UnallocatedEncodings */
7695     if (is_signed) {
7696         if (size > 2 || (size == 2 && !is_q)) {
7697             unallocated_encoding(s);
7698             return;
7699         }
7700     } else {
7701         if (size > 3
7702             || (size < 3 && is_q)
7703             || (size == 3 && !is_q)) {
7704             unallocated_encoding(s);
7705             return;
7706         }
7707     }
7708 
7709     if (!fp_access_check(s)) {
7710         return;
7711     }
7712 
7713     element = extract32(imm5, 1+size, 4);
7714 
7715     tcg_rd = cpu_reg(s, rd);
7716     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7717     if (is_signed && !is_q) {
7718         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7719     }
7720 }
7721 
7722 /* AdvSIMD copy
7723  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
7724  * +---+---+----+-----------------+------+---+------+---+------+------+
7725  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7726  * +---+---+----+-----------------+------+---+------+---+------+------+
7727  */
7728 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7729 {
7730     int rd = extract32(insn, 0, 5);
7731     int rn = extract32(insn, 5, 5);
7732     int imm4 = extract32(insn, 11, 4);
7733     int op = extract32(insn, 29, 1);
7734     int is_q = extract32(insn, 30, 1);
7735     int imm5 = extract32(insn, 16, 5);
7736 
7737     if (op) {
7738         if (is_q) {
7739             /* INS (element) */
7740             handle_simd_inse(s, rd, rn, imm4, imm5);
7741         } else {
7742             unallocated_encoding(s);
7743         }
7744     } else {
7745         switch (imm4) {
7746         case 0:
7747             /* DUP (element - vector) */
7748             handle_simd_dupe(s, is_q, rd, rn, imm5);
7749             break;
7750         case 1:
7751             /* DUP (general) */
7752             handle_simd_dupg(s, is_q, rd, rn, imm5);
7753             break;
7754         case 3:
7755             if (is_q) {
7756                 /* INS (general) */
7757                 handle_simd_insg(s, rd, rn, imm5);
7758             } else {
7759                 unallocated_encoding(s);
7760             }
7761             break;
7762         case 5:
7763         case 7:
7764             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
7765             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
7766             break;
7767         default:
7768             unallocated_encoding(s);
7769             break;
7770         }
7771     }
7772 }
7773 
7774 /* AdvSIMD modified immediate
7775  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
7776  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7777  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
7778  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7779  *
7780  * There are a number of operations that can be carried out here:
7781  *   MOVI - move (shifted) imm into register
7782  *   MVNI - move inverted (shifted) imm into register
7783  *   ORR  - bitwise OR of (shifted) imm with register
7784  *   BIC  - bitwise clear of (shifted) imm with register
7785  * With ARMv8.2 we also have:
7786  *   FMOV half-precision
7787  */
7788 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
7789 {
7790     int rd = extract32(insn, 0, 5);
7791     int cmode = extract32(insn, 12, 4);
7792     int o2 = extract32(insn, 11, 1);
7793     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
7794     bool is_neg = extract32(insn, 29, 1);
7795     bool is_q = extract32(insn, 30, 1);
7796     uint64_t imm = 0;
7797 
7798     if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
7799         /* Check for FMOV (vector, immediate) - half-precision */
7800         if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
7801             unallocated_encoding(s);
7802             return;
7803         }
7804     }
7805 
7806     if (!fp_access_check(s)) {
7807         return;
7808     }
7809 
7810     if (cmode == 15 && o2 && !is_neg) {
7811         /* FMOV (vector, immediate) - half-precision */
7812         imm = vfp_expand_imm(MO_16, abcdefgh);
7813         /* now duplicate across the lanes */
7814         imm = dup_const(MO_16, imm);
7815     } else {
7816         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
7817     }
7818 
7819     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
7820         /* MOVI or MVNI, with MVNI negation handled above.  */
7821         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
7822                              vec_full_reg_size(s), imm);
7823     } else {
7824         /* ORR or BIC, with BIC negation to AND handled above.  */
7825         if (is_neg) {
7826             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
7827         } else {
7828             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
7829         }
7830     }
7831 }
7832 
7833 /* AdvSIMD scalar copy
7834  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
7835  * +-----+----+-----------------+------+---+------+---+------+------+
7836  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7837  * +-----+----+-----------------+------+---+------+---+------+------+
7838  */
7839 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
7840 {
7841     int rd = extract32(insn, 0, 5);
7842     int rn = extract32(insn, 5, 5);
7843     int imm4 = extract32(insn, 11, 4);
7844     int imm5 = extract32(insn, 16, 5);
7845     int op = extract32(insn, 29, 1);
7846 
7847     if (op != 0 || imm4 != 0) {
7848         unallocated_encoding(s);
7849         return;
7850     }
7851 
7852     /* DUP (element, scalar) */
7853     handle_simd_dupes(s, rd, rn, imm5);
7854 }
7855 
7856 /* AdvSIMD scalar pairwise
7857  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7858  * +-----+---+-----------+------+-----------+--------+-----+------+------+
7859  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7860  * +-----+---+-----------+------+-----------+--------+-----+------+------+
7861  */
7862 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
7863 {
7864     int u = extract32(insn, 29, 1);
7865     int size = extract32(insn, 22, 2);
7866     int opcode = extract32(insn, 12, 5);
7867     int rn = extract32(insn, 5, 5);
7868     int rd = extract32(insn, 0, 5);
7869     TCGv_ptr fpst;
7870 
7871     /* For some ops (the FP ones), size[1] is part of the encoding.
7872      * For ADDP strictly it is not but size[1] is always 1 for valid
7873      * encodings.
7874      */
7875     opcode |= (extract32(size, 1, 1) << 5);
7876 
7877     switch (opcode) {
7878     case 0x3b: /* ADDP */
7879         if (u || size != 3) {
7880             unallocated_encoding(s);
7881             return;
7882         }
7883         if (!fp_access_check(s)) {
7884             return;
7885         }
7886 
7887         fpst = NULL;
7888         break;
7889     case 0xc: /* FMAXNMP */
7890     case 0xd: /* FADDP */
7891     case 0xf: /* FMAXP */
7892     case 0x2c: /* FMINNMP */
7893     case 0x2f: /* FMINP */
7894         /* FP op, size[0] is 32 or 64 bit*/
7895         if (!u) {
7896             if (!dc_isar_feature(aa64_fp16, s)) {
7897                 unallocated_encoding(s);
7898                 return;
7899             } else {
7900                 size = MO_16;
7901             }
7902         } else {
7903             size = extract32(size, 0, 1) ? MO_64 : MO_32;
7904         }
7905 
7906         if (!fp_access_check(s)) {
7907             return;
7908         }
7909 
7910         fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7911         break;
7912     default:
7913         unallocated_encoding(s);
7914         return;
7915     }
7916 
7917     if (size == MO_64) {
7918         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7919         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7920         TCGv_i64 tcg_res = tcg_temp_new_i64();
7921 
7922         read_vec_element(s, tcg_op1, rn, 0, MO_64);
7923         read_vec_element(s, tcg_op2, rn, 1, MO_64);
7924 
7925         switch (opcode) {
7926         case 0x3b: /* ADDP */
7927             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
7928             break;
7929         case 0xc: /* FMAXNMP */
7930             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7931             break;
7932         case 0xd: /* FADDP */
7933             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
7934             break;
7935         case 0xf: /* FMAXP */
7936             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
7937             break;
7938         case 0x2c: /* FMINNMP */
7939             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7940             break;
7941         case 0x2f: /* FMINP */
7942             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
7943             break;
7944         default:
7945             g_assert_not_reached();
7946         }
7947 
7948         write_fp_dreg(s, rd, tcg_res);
7949     } else {
7950         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7951         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7952         TCGv_i32 tcg_res = tcg_temp_new_i32();
7953 
7954         read_vec_element_i32(s, tcg_op1, rn, 0, size);
7955         read_vec_element_i32(s, tcg_op2, rn, 1, size);
7956 
7957         if (size == MO_16) {
7958             switch (opcode) {
7959             case 0xc: /* FMAXNMP */
7960                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
7961                 break;
7962             case 0xd: /* FADDP */
7963                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
7964                 break;
7965             case 0xf: /* FMAXP */
7966                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
7967                 break;
7968             case 0x2c: /* FMINNMP */
7969                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
7970                 break;
7971             case 0x2f: /* FMINP */
7972                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
7973                 break;
7974             default:
7975                 g_assert_not_reached();
7976             }
7977         } else {
7978             switch (opcode) {
7979             case 0xc: /* FMAXNMP */
7980                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
7981                 break;
7982             case 0xd: /* FADDP */
7983                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
7984                 break;
7985             case 0xf: /* FMAXP */
7986                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
7987                 break;
7988             case 0x2c: /* FMINNMP */
7989                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
7990                 break;
7991             case 0x2f: /* FMINP */
7992                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
7993                 break;
7994             default:
7995                 g_assert_not_reached();
7996             }
7997         }
7998 
7999         write_fp_sreg(s, rd, tcg_res);
8000     }
8001 }
8002 
8003 /*
8004  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8005  *
8006  * This code is handles the common shifting code and is used by both
8007  * the vector and scalar code.
8008  */
8009 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8010                                     TCGv_i64 tcg_rnd, bool accumulate,
8011                                     bool is_u, int size, int shift)
8012 {
8013     bool extended_result = false;
8014     bool round = tcg_rnd != NULL;
8015     int ext_lshift = 0;
8016     TCGv_i64 tcg_src_hi;
8017 
8018     if (round && size == 3) {
8019         extended_result = true;
8020         ext_lshift = 64 - shift;
8021         tcg_src_hi = tcg_temp_new_i64();
8022     } else if (shift == 64) {
8023         if (!accumulate && is_u) {
8024             /* result is zero */
8025             tcg_gen_movi_i64(tcg_res, 0);
8026             return;
8027         }
8028     }
8029 
8030     /* Deal with the rounding step */
8031     if (round) {
8032         if (extended_result) {
8033             TCGv_i64 tcg_zero = tcg_constant_i64(0);
8034             if (!is_u) {
8035                 /* take care of sign extending tcg_res */
8036                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8037                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8038                                  tcg_src, tcg_src_hi,
8039                                  tcg_rnd, tcg_zero);
8040             } else {
8041                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8042                                  tcg_src, tcg_zero,
8043                                  tcg_rnd, tcg_zero);
8044             }
8045         } else {
8046             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8047         }
8048     }
8049 
8050     /* Now do the shift right */
8051     if (round && extended_result) {
8052         /* extended case, >64 bit precision required */
8053         if (ext_lshift == 0) {
8054             /* special case, only high bits matter */
8055             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8056         } else {
8057             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8058             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8059             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8060         }
8061     } else {
8062         if (is_u) {
8063             if (shift == 64) {
8064                 /* essentially shifting in 64 zeros */
8065                 tcg_gen_movi_i64(tcg_src, 0);
8066             } else {
8067                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8068             }
8069         } else {
8070             if (shift == 64) {
8071                 /* effectively extending the sign-bit */
8072                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8073             } else {
8074                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8075             }
8076         }
8077     }
8078 
8079     if (accumulate) {
8080         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8081     } else {
8082         tcg_gen_mov_i64(tcg_res, tcg_src);
8083     }
8084 }
8085 
8086 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8087 static void handle_scalar_simd_shri(DisasContext *s,
8088                                     bool is_u, int immh, int immb,
8089                                     int opcode, int rn, int rd)
8090 {
8091     const int size = 3;
8092     int immhb = immh << 3 | immb;
8093     int shift = 2 * (8 << size) - immhb;
8094     bool accumulate = false;
8095     bool round = false;
8096     bool insert = false;
8097     TCGv_i64 tcg_rn;
8098     TCGv_i64 tcg_rd;
8099     TCGv_i64 tcg_round;
8100 
8101     if (!extract32(immh, 3, 1)) {
8102         unallocated_encoding(s);
8103         return;
8104     }
8105 
8106     if (!fp_access_check(s)) {
8107         return;
8108     }
8109 
8110     switch (opcode) {
8111     case 0x02: /* SSRA / USRA (accumulate) */
8112         accumulate = true;
8113         break;
8114     case 0x04: /* SRSHR / URSHR (rounding) */
8115         round = true;
8116         break;
8117     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8118         accumulate = round = true;
8119         break;
8120     case 0x08: /* SRI */
8121         insert = true;
8122         break;
8123     }
8124 
8125     if (round) {
8126         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8127     } else {
8128         tcg_round = NULL;
8129     }
8130 
8131     tcg_rn = read_fp_dreg(s, rn);
8132     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8133 
8134     if (insert) {
8135         /* shift count same as element size is valid but does nothing;
8136          * special case to avoid potential shift by 64.
8137          */
8138         int esize = 8 << size;
8139         if (shift != esize) {
8140             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8141             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8142         }
8143     } else {
8144         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8145                                 accumulate, is_u, size, shift);
8146     }
8147 
8148     write_fp_dreg(s, rd, tcg_rd);
8149 }
8150 
8151 /* SHL/SLI - Scalar shift left */
8152 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8153                                     int immh, int immb, int opcode,
8154                                     int rn, int rd)
8155 {
8156     int size = 32 - clz32(immh) - 1;
8157     int immhb = immh << 3 | immb;
8158     int shift = immhb - (8 << size);
8159     TCGv_i64 tcg_rn;
8160     TCGv_i64 tcg_rd;
8161 
8162     if (!extract32(immh, 3, 1)) {
8163         unallocated_encoding(s);
8164         return;
8165     }
8166 
8167     if (!fp_access_check(s)) {
8168         return;
8169     }
8170 
8171     tcg_rn = read_fp_dreg(s, rn);
8172     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8173 
8174     if (insert) {
8175         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8176     } else {
8177         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8178     }
8179 
8180     write_fp_dreg(s, rd, tcg_rd);
8181 }
8182 
8183 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8184  * (signed/unsigned) narrowing */
8185 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8186                                    bool is_u_shift, bool is_u_narrow,
8187                                    int immh, int immb, int opcode,
8188                                    int rn, int rd)
8189 {
8190     int immhb = immh << 3 | immb;
8191     int size = 32 - clz32(immh) - 1;
8192     int esize = 8 << size;
8193     int shift = (2 * esize) - immhb;
8194     int elements = is_scalar ? 1 : (64 / esize);
8195     bool round = extract32(opcode, 0, 1);
8196     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8197     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8198     TCGv_i32 tcg_rd_narrowed;
8199     TCGv_i64 tcg_final;
8200 
8201     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8202         { gen_helper_neon_narrow_sat_s8,
8203           gen_helper_neon_unarrow_sat8 },
8204         { gen_helper_neon_narrow_sat_s16,
8205           gen_helper_neon_unarrow_sat16 },
8206         { gen_helper_neon_narrow_sat_s32,
8207           gen_helper_neon_unarrow_sat32 },
8208         { NULL, NULL },
8209     };
8210     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8211         gen_helper_neon_narrow_sat_u8,
8212         gen_helper_neon_narrow_sat_u16,
8213         gen_helper_neon_narrow_sat_u32,
8214         NULL
8215     };
8216     NeonGenNarrowEnvFn *narrowfn;
8217 
8218     int i;
8219 
8220     assert(size < 4);
8221 
8222     if (extract32(immh, 3, 1)) {
8223         unallocated_encoding(s);
8224         return;
8225     }
8226 
8227     if (!fp_access_check(s)) {
8228         return;
8229     }
8230 
8231     if (is_u_shift) {
8232         narrowfn = unsigned_narrow_fns[size];
8233     } else {
8234         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8235     }
8236 
8237     tcg_rn = tcg_temp_new_i64();
8238     tcg_rd = tcg_temp_new_i64();
8239     tcg_rd_narrowed = tcg_temp_new_i32();
8240     tcg_final = tcg_temp_new_i64();
8241 
8242     if (round) {
8243         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8244     } else {
8245         tcg_round = NULL;
8246     }
8247 
8248     for (i = 0; i < elements; i++) {
8249         read_vec_element(s, tcg_rn, rn, i, ldop);
8250         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8251                                 false, is_u_shift, size+1, shift);
8252         narrowfn(tcg_rd_narrowed, tcg_env, tcg_rd);
8253         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8254         if (i == 0) {
8255             tcg_gen_mov_i64(tcg_final, tcg_rd);
8256         } else {
8257             tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8258         }
8259     }
8260 
8261     if (!is_q) {
8262         write_vec_element(s, tcg_final, rd, 0, MO_64);
8263     } else {
8264         write_vec_element(s, tcg_final, rd, 1, MO_64);
8265     }
8266     clear_vec_high(s, is_q, rd);
8267 }
8268 
8269 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8270 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8271                              bool src_unsigned, bool dst_unsigned,
8272                              int immh, int immb, int rn, int rd)
8273 {
8274     int immhb = immh << 3 | immb;
8275     int size = 32 - clz32(immh) - 1;
8276     int shift = immhb - (8 << size);
8277     int pass;
8278 
8279     assert(immh != 0);
8280     assert(!(scalar && is_q));
8281 
8282     if (!scalar) {
8283         if (!is_q && extract32(immh, 3, 1)) {
8284             unallocated_encoding(s);
8285             return;
8286         }
8287 
8288         /* Since we use the variable-shift helpers we must
8289          * replicate the shift count into each element of
8290          * the tcg_shift value.
8291          */
8292         switch (size) {
8293         case 0:
8294             shift |= shift << 8;
8295             /* fall through */
8296         case 1:
8297             shift |= shift << 16;
8298             break;
8299         case 2:
8300         case 3:
8301             break;
8302         default:
8303             g_assert_not_reached();
8304         }
8305     }
8306 
8307     if (!fp_access_check(s)) {
8308         return;
8309     }
8310 
8311     if (size == 3) {
8312         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8313         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8314             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8315             { NULL, gen_helper_neon_qshl_u64 },
8316         };
8317         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8318         int maxpass = is_q ? 2 : 1;
8319 
8320         for (pass = 0; pass < maxpass; pass++) {
8321             TCGv_i64 tcg_op = tcg_temp_new_i64();
8322 
8323             read_vec_element(s, tcg_op, rn, pass, MO_64);
8324             genfn(tcg_op, tcg_env, tcg_op, tcg_shift);
8325             write_vec_element(s, tcg_op, rd, pass, MO_64);
8326         }
8327         clear_vec_high(s, is_q, rd);
8328     } else {
8329         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8330         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8331             {
8332                 { gen_helper_neon_qshl_s8,
8333                   gen_helper_neon_qshl_s16,
8334                   gen_helper_neon_qshl_s32 },
8335                 { gen_helper_neon_qshlu_s8,
8336                   gen_helper_neon_qshlu_s16,
8337                   gen_helper_neon_qshlu_s32 }
8338             }, {
8339                 { NULL, NULL, NULL },
8340                 { gen_helper_neon_qshl_u8,
8341                   gen_helper_neon_qshl_u16,
8342                   gen_helper_neon_qshl_u32 }
8343             }
8344         };
8345         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8346         MemOp memop = scalar ? size : MO_32;
8347         int maxpass = scalar ? 1 : is_q ? 4 : 2;
8348 
8349         for (pass = 0; pass < maxpass; pass++) {
8350             TCGv_i32 tcg_op = tcg_temp_new_i32();
8351 
8352             read_vec_element_i32(s, tcg_op, rn, pass, memop);
8353             genfn(tcg_op, tcg_env, tcg_op, tcg_shift);
8354             if (scalar) {
8355                 switch (size) {
8356                 case 0:
8357                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
8358                     break;
8359                 case 1:
8360                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8361                     break;
8362                 case 2:
8363                     break;
8364                 default:
8365                     g_assert_not_reached();
8366                 }
8367                 write_fp_sreg(s, rd, tcg_op);
8368             } else {
8369                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8370             }
8371         }
8372 
8373         if (!scalar) {
8374             clear_vec_high(s, is_q, rd);
8375         }
8376     }
8377 }
8378 
8379 /* Common vector code for handling integer to FP conversion */
8380 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8381                                    int elements, int is_signed,
8382                                    int fracbits, int size)
8383 {
8384     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8385     TCGv_i32 tcg_shift = NULL;
8386 
8387     MemOp mop = size | (is_signed ? MO_SIGN : 0);
8388     int pass;
8389 
8390     if (fracbits || size == MO_64) {
8391         tcg_shift = tcg_constant_i32(fracbits);
8392     }
8393 
8394     if (size == MO_64) {
8395         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8396         TCGv_i64 tcg_double = tcg_temp_new_i64();
8397 
8398         for (pass = 0; pass < elements; pass++) {
8399             read_vec_element(s, tcg_int64, rn, pass, mop);
8400 
8401             if (is_signed) {
8402                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8403                                      tcg_shift, tcg_fpst);
8404             } else {
8405                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8406                                      tcg_shift, tcg_fpst);
8407             }
8408             if (elements == 1) {
8409                 write_fp_dreg(s, rd, tcg_double);
8410             } else {
8411                 write_vec_element(s, tcg_double, rd, pass, MO_64);
8412             }
8413         }
8414     } else {
8415         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8416         TCGv_i32 tcg_float = tcg_temp_new_i32();
8417 
8418         for (pass = 0; pass < elements; pass++) {
8419             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8420 
8421             switch (size) {
8422             case MO_32:
8423                 if (fracbits) {
8424                     if (is_signed) {
8425                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
8426                                              tcg_shift, tcg_fpst);
8427                     } else {
8428                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
8429                                              tcg_shift, tcg_fpst);
8430                     }
8431                 } else {
8432                     if (is_signed) {
8433                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8434                     } else {
8435                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8436                     }
8437                 }
8438                 break;
8439             case MO_16:
8440                 if (fracbits) {
8441                     if (is_signed) {
8442                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8443                                              tcg_shift, tcg_fpst);
8444                     } else {
8445                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8446                                              tcg_shift, tcg_fpst);
8447                     }
8448                 } else {
8449                     if (is_signed) {
8450                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8451                     } else {
8452                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8453                     }
8454                 }
8455                 break;
8456             default:
8457                 g_assert_not_reached();
8458             }
8459 
8460             if (elements == 1) {
8461                 write_fp_sreg(s, rd, tcg_float);
8462             } else {
8463                 write_vec_element_i32(s, tcg_float, rd, pass, size);
8464             }
8465         }
8466     }
8467 
8468     clear_vec_high(s, elements << size == 16, rd);
8469 }
8470 
8471 /* UCVTF/SCVTF - Integer to FP conversion */
8472 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8473                                          bool is_q, bool is_u,
8474                                          int immh, int immb, int opcode,
8475                                          int rn, int rd)
8476 {
8477     int size, elements, fracbits;
8478     int immhb = immh << 3 | immb;
8479 
8480     if (immh & 8) {
8481         size = MO_64;
8482         if (!is_scalar && !is_q) {
8483             unallocated_encoding(s);
8484             return;
8485         }
8486     } else if (immh & 4) {
8487         size = MO_32;
8488     } else if (immh & 2) {
8489         size = MO_16;
8490         if (!dc_isar_feature(aa64_fp16, s)) {
8491             unallocated_encoding(s);
8492             return;
8493         }
8494     } else {
8495         /* immh == 0 would be a failure of the decode logic */
8496         g_assert(immh == 1);
8497         unallocated_encoding(s);
8498         return;
8499     }
8500 
8501     if (is_scalar) {
8502         elements = 1;
8503     } else {
8504         elements = (8 << is_q) >> size;
8505     }
8506     fracbits = (16 << size) - immhb;
8507 
8508     if (!fp_access_check(s)) {
8509         return;
8510     }
8511 
8512     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8513 }
8514 
8515 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8516 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8517                                          bool is_q, bool is_u,
8518                                          int immh, int immb, int rn, int rd)
8519 {
8520     int immhb = immh << 3 | immb;
8521     int pass, size, fracbits;
8522     TCGv_ptr tcg_fpstatus;
8523     TCGv_i32 tcg_rmode, tcg_shift;
8524 
8525     if (immh & 0x8) {
8526         size = MO_64;
8527         if (!is_scalar && !is_q) {
8528             unallocated_encoding(s);
8529             return;
8530         }
8531     } else if (immh & 0x4) {
8532         size = MO_32;
8533     } else if (immh & 0x2) {
8534         size = MO_16;
8535         if (!dc_isar_feature(aa64_fp16, s)) {
8536             unallocated_encoding(s);
8537             return;
8538         }
8539     } else {
8540         /* Should have split out AdvSIMD modified immediate earlier.  */
8541         assert(immh == 1);
8542         unallocated_encoding(s);
8543         return;
8544     }
8545 
8546     if (!fp_access_check(s)) {
8547         return;
8548     }
8549 
8550     assert(!(is_scalar && is_q));
8551 
8552     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8553     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
8554     fracbits = (16 << size) - immhb;
8555     tcg_shift = tcg_constant_i32(fracbits);
8556 
8557     if (size == MO_64) {
8558         int maxpass = is_scalar ? 1 : 2;
8559 
8560         for (pass = 0; pass < maxpass; pass++) {
8561             TCGv_i64 tcg_op = tcg_temp_new_i64();
8562 
8563             read_vec_element(s, tcg_op, rn, pass, MO_64);
8564             if (is_u) {
8565                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8566             } else {
8567                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8568             }
8569             write_vec_element(s, tcg_op, rd, pass, MO_64);
8570         }
8571         clear_vec_high(s, is_q, rd);
8572     } else {
8573         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8574         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8575 
8576         switch (size) {
8577         case MO_16:
8578             if (is_u) {
8579                 fn = gen_helper_vfp_touhh;
8580             } else {
8581                 fn = gen_helper_vfp_toshh;
8582             }
8583             break;
8584         case MO_32:
8585             if (is_u) {
8586                 fn = gen_helper_vfp_touls;
8587             } else {
8588                 fn = gen_helper_vfp_tosls;
8589             }
8590             break;
8591         default:
8592             g_assert_not_reached();
8593         }
8594 
8595         for (pass = 0; pass < maxpass; pass++) {
8596             TCGv_i32 tcg_op = tcg_temp_new_i32();
8597 
8598             read_vec_element_i32(s, tcg_op, rn, pass, size);
8599             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8600             if (is_scalar) {
8601                 write_fp_sreg(s, rd, tcg_op);
8602             } else {
8603                 write_vec_element_i32(s, tcg_op, rd, pass, size);
8604             }
8605         }
8606         if (!is_scalar) {
8607             clear_vec_high(s, is_q, rd);
8608         }
8609     }
8610 
8611     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
8612 }
8613 
8614 /* AdvSIMD scalar shift by immediate
8615  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
8616  * +-----+---+-------------+------+------+--------+---+------+------+
8617  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8618  * +-----+---+-------------+------+------+--------+---+------+------+
8619  *
8620  * This is the scalar version so it works on a fixed sized registers
8621  */
8622 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8623 {
8624     int rd = extract32(insn, 0, 5);
8625     int rn = extract32(insn, 5, 5);
8626     int opcode = extract32(insn, 11, 5);
8627     int immb = extract32(insn, 16, 3);
8628     int immh = extract32(insn, 19, 4);
8629     bool is_u = extract32(insn, 29, 1);
8630 
8631     if (immh == 0) {
8632         unallocated_encoding(s);
8633         return;
8634     }
8635 
8636     switch (opcode) {
8637     case 0x08: /* SRI */
8638         if (!is_u) {
8639             unallocated_encoding(s);
8640             return;
8641         }
8642         /* fall through */
8643     case 0x00: /* SSHR / USHR */
8644     case 0x02: /* SSRA / USRA */
8645     case 0x04: /* SRSHR / URSHR */
8646     case 0x06: /* SRSRA / URSRA */
8647         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8648         break;
8649     case 0x0a: /* SHL / SLI */
8650         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8651         break;
8652     case 0x1c: /* SCVTF, UCVTF */
8653         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8654                                      opcode, rn, rd);
8655         break;
8656     case 0x10: /* SQSHRUN, SQSHRUN2 */
8657     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8658         if (!is_u) {
8659             unallocated_encoding(s);
8660             return;
8661         }
8662         handle_vec_simd_sqshrn(s, true, false, false, true,
8663                                immh, immb, opcode, rn, rd);
8664         break;
8665     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8666     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8667         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8668                                immh, immb, opcode, rn, rd);
8669         break;
8670     case 0xc: /* SQSHLU */
8671         if (!is_u) {
8672             unallocated_encoding(s);
8673             return;
8674         }
8675         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8676         break;
8677     case 0xe: /* SQSHL, UQSHL */
8678         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8679         break;
8680     case 0x1f: /* FCVTZS, FCVTZU */
8681         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8682         break;
8683     default:
8684         unallocated_encoding(s);
8685         break;
8686     }
8687 }
8688 
8689 /* AdvSIMD scalar three different
8690  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
8691  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8692  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
8693  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8694  */
8695 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8696 {
8697     bool is_u = extract32(insn, 29, 1);
8698     int size = extract32(insn, 22, 2);
8699     int opcode = extract32(insn, 12, 4);
8700     int rm = extract32(insn, 16, 5);
8701     int rn = extract32(insn, 5, 5);
8702     int rd = extract32(insn, 0, 5);
8703 
8704     if (is_u) {
8705         unallocated_encoding(s);
8706         return;
8707     }
8708 
8709     switch (opcode) {
8710     case 0x9: /* SQDMLAL, SQDMLAL2 */
8711     case 0xb: /* SQDMLSL, SQDMLSL2 */
8712     case 0xd: /* SQDMULL, SQDMULL2 */
8713         if (size == 0 || size == 3) {
8714             unallocated_encoding(s);
8715             return;
8716         }
8717         break;
8718     default:
8719         unallocated_encoding(s);
8720         return;
8721     }
8722 
8723     if (!fp_access_check(s)) {
8724         return;
8725     }
8726 
8727     if (size == 2) {
8728         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8729         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8730         TCGv_i64 tcg_res = tcg_temp_new_i64();
8731 
8732         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8733         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8734 
8735         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8736         gen_helper_neon_addl_saturate_s64(tcg_res, tcg_env, tcg_res, tcg_res);
8737 
8738         switch (opcode) {
8739         case 0xd: /* SQDMULL, SQDMULL2 */
8740             break;
8741         case 0xb: /* SQDMLSL, SQDMLSL2 */
8742             tcg_gen_neg_i64(tcg_res, tcg_res);
8743             /* fall through */
8744         case 0x9: /* SQDMLAL, SQDMLAL2 */
8745             read_vec_element(s, tcg_op1, rd, 0, MO_64);
8746             gen_helper_neon_addl_saturate_s64(tcg_res, tcg_env,
8747                                               tcg_res, tcg_op1);
8748             break;
8749         default:
8750             g_assert_not_reached();
8751         }
8752 
8753         write_fp_dreg(s, rd, tcg_res);
8754     } else {
8755         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8756         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8757         TCGv_i64 tcg_res = tcg_temp_new_i64();
8758 
8759         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8760         gen_helper_neon_addl_saturate_s32(tcg_res, tcg_env, tcg_res, tcg_res);
8761 
8762         switch (opcode) {
8763         case 0xd: /* SQDMULL, SQDMULL2 */
8764             break;
8765         case 0xb: /* SQDMLSL, SQDMLSL2 */
8766             gen_helper_neon_negl_u32(tcg_res, tcg_res);
8767             /* fall through */
8768         case 0x9: /* SQDMLAL, SQDMLAL2 */
8769         {
8770             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
8771             read_vec_element(s, tcg_op3, rd, 0, MO_32);
8772             gen_helper_neon_addl_saturate_s32(tcg_res, tcg_env,
8773                                               tcg_res, tcg_op3);
8774             break;
8775         }
8776         default:
8777             g_assert_not_reached();
8778         }
8779 
8780         tcg_gen_ext32u_i64(tcg_res, tcg_res);
8781         write_fp_dreg(s, rd, tcg_res);
8782     }
8783 }
8784 
8785 static void handle_3same_64(DisasContext *s, int opcode, bool u,
8786                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
8787 {
8788     /* Handle 64x64->64 opcodes which are shared between the scalar
8789      * and vector 3-same groups. We cover every opcode where size == 3
8790      * is valid in either the three-reg-same (integer, not pairwise)
8791      * or scalar-three-reg-same groups.
8792      */
8793     TCGCond cond;
8794 
8795     switch (opcode) {
8796     case 0x1: /* SQADD */
8797         if (u) {
8798             gen_helper_neon_qadd_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8799         } else {
8800             gen_helper_neon_qadd_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8801         }
8802         break;
8803     case 0x5: /* SQSUB */
8804         if (u) {
8805             gen_helper_neon_qsub_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8806         } else {
8807             gen_helper_neon_qsub_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8808         }
8809         break;
8810     case 0x6: /* CMGT, CMHI */
8811         cond = u ? TCG_COND_GTU : TCG_COND_GT;
8812     do_cmop:
8813         /* 64 bit integer comparison, result = test ? -1 : 0. */
8814         tcg_gen_negsetcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
8815         break;
8816     case 0x7: /* CMGE, CMHS */
8817         cond = u ? TCG_COND_GEU : TCG_COND_GE;
8818         goto do_cmop;
8819     case 0x11: /* CMTST, CMEQ */
8820         if (u) {
8821             cond = TCG_COND_EQ;
8822             goto do_cmop;
8823         }
8824         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
8825         break;
8826     case 0x8: /* SSHL, USHL */
8827         if (u) {
8828             gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
8829         } else {
8830             gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
8831         }
8832         break;
8833     case 0x9: /* SQSHL, UQSHL */
8834         if (u) {
8835             gen_helper_neon_qshl_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8836         } else {
8837             gen_helper_neon_qshl_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8838         }
8839         break;
8840     case 0xa: /* SRSHL, URSHL */
8841         if (u) {
8842             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
8843         } else {
8844             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
8845         }
8846         break;
8847     case 0xb: /* SQRSHL, UQRSHL */
8848         if (u) {
8849             gen_helper_neon_qrshl_u64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8850         } else {
8851             gen_helper_neon_qrshl_s64(tcg_rd, tcg_env, tcg_rn, tcg_rm);
8852         }
8853         break;
8854     case 0x10: /* ADD, SUB */
8855         if (u) {
8856             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
8857         } else {
8858             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
8859         }
8860         break;
8861     default:
8862         g_assert_not_reached();
8863     }
8864 }
8865 
8866 /* Handle the 3-same-operands float operations; shared by the scalar
8867  * and vector encodings. The caller must filter out any encodings
8868  * not allocated for the encoding it is dealing with.
8869  */
8870 static void handle_3same_float(DisasContext *s, int size, int elements,
8871                                int fpopcode, int rd, int rn, int rm)
8872 {
8873     int pass;
8874     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
8875 
8876     for (pass = 0; pass < elements; pass++) {
8877         if (size) {
8878             /* Double */
8879             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8880             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8881             TCGv_i64 tcg_res = tcg_temp_new_i64();
8882 
8883             read_vec_element(s, tcg_op1, rn, pass, MO_64);
8884             read_vec_element(s, tcg_op2, rm, pass, MO_64);
8885 
8886             switch (fpopcode) {
8887             case 0x39: /* FMLS */
8888                 /* As usual for ARM, separate negation for fused multiply-add */
8889                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
8890                 /* fall through */
8891             case 0x19: /* FMLA */
8892                 read_vec_element(s, tcg_res, rd, pass, MO_64);
8893                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
8894                                        tcg_res, fpst);
8895                 break;
8896             case 0x18: /* FMAXNM */
8897                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8898                 break;
8899             case 0x1a: /* FADD */
8900                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8901                 break;
8902             case 0x1b: /* FMULX */
8903                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
8904                 break;
8905             case 0x1c: /* FCMEQ */
8906                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8907                 break;
8908             case 0x1e: /* FMAX */
8909                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8910                 break;
8911             case 0x1f: /* FRECPS */
8912                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8913                 break;
8914             case 0x38: /* FMINNM */
8915                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8916                 break;
8917             case 0x3a: /* FSUB */
8918                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
8919                 break;
8920             case 0x3e: /* FMIN */
8921                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8922                 break;
8923             case 0x3f: /* FRSQRTS */
8924                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8925                 break;
8926             case 0x5b: /* FMUL */
8927                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
8928                 break;
8929             case 0x5c: /* FCMGE */
8930                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8931                 break;
8932             case 0x5d: /* FACGE */
8933                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8934                 break;
8935             case 0x5f: /* FDIV */
8936                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
8937                 break;
8938             case 0x7a: /* FABD */
8939                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
8940                 gen_helper_vfp_absd(tcg_res, tcg_res);
8941                 break;
8942             case 0x7c: /* FCMGT */
8943                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8944                 break;
8945             case 0x7d: /* FACGT */
8946                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8947                 break;
8948             default:
8949                 g_assert_not_reached();
8950             }
8951 
8952             write_vec_element(s, tcg_res, rd, pass, MO_64);
8953         } else {
8954             /* Single */
8955             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8956             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8957             TCGv_i32 tcg_res = tcg_temp_new_i32();
8958 
8959             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
8960             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
8961 
8962             switch (fpopcode) {
8963             case 0x39: /* FMLS */
8964                 /* As usual for ARM, separate negation for fused multiply-add */
8965                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
8966                 /* fall through */
8967             case 0x19: /* FMLA */
8968                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
8969                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
8970                                        tcg_res, fpst);
8971                 break;
8972             case 0x1a: /* FADD */
8973                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8974                 break;
8975             case 0x1b: /* FMULX */
8976                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
8977                 break;
8978             case 0x1c: /* FCMEQ */
8979                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8980                 break;
8981             case 0x1e: /* FMAX */
8982                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8983                 break;
8984             case 0x1f: /* FRECPS */
8985                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8986                 break;
8987             case 0x18: /* FMAXNM */
8988                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8989                 break;
8990             case 0x38: /* FMINNM */
8991                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8992                 break;
8993             case 0x3a: /* FSUB */
8994                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
8995                 break;
8996             case 0x3e: /* FMIN */
8997                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8998                 break;
8999             case 0x3f: /* FRSQRTS */
9000                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9001                 break;
9002             case 0x5b: /* FMUL */
9003                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9004                 break;
9005             case 0x5c: /* FCMGE */
9006                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9007                 break;
9008             case 0x5d: /* FACGE */
9009                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9010                 break;
9011             case 0x5f: /* FDIV */
9012                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9013                 break;
9014             case 0x7a: /* FABD */
9015                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9016                 gen_helper_vfp_abss(tcg_res, tcg_res);
9017                 break;
9018             case 0x7c: /* FCMGT */
9019                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9020                 break;
9021             case 0x7d: /* FACGT */
9022                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9023                 break;
9024             default:
9025                 g_assert_not_reached();
9026             }
9027 
9028             if (elements == 1) {
9029                 /* scalar single so clear high part */
9030                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9031 
9032                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9033                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9034             } else {
9035                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9036             }
9037         }
9038     }
9039 
9040     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9041 }
9042 
9043 /* AdvSIMD scalar three same
9044  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
9045  * +-----+---+-----------+------+---+------+--------+---+------+------+
9046  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
9047  * +-----+---+-----------+------+---+------+--------+---+------+------+
9048  */
9049 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9050 {
9051     int rd = extract32(insn, 0, 5);
9052     int rn = extract32(insn, 5, 5);
9053     int opcode = extract32(insn, 11, 5);
9054     int rm = extract32(insn, 16, 5);
9055     int size = extract32(insn, 22, 2);
9056     bool u = extract32(insn, 29, 1);
9057     TCGv_i64 tcg_rd;
9058 
9059     if (opcode >= 0x18) {
9060         /* Floating point: U, size[1] and opcode indicate operation */
9061         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9062         switch (fpopcode) {
9063         case 0x1b: /* FMULX */
9064         case 0x1f: /* FRECPS */
9065         case 0x3f: /* FRSQRTS */
9066         case 0x5d: /* FACGE */
9067         case 0x7d: /* FACGT */
9068         case 0x1c: /* FCMEQ */
9069         case 0x5c: /* FCMGE */
9070         case 0x7c: /* FCMGT */
9071         case 0x7a: /* FABD */
9072             break;
9073         default:
9074             unallocated_encoding(s);
9075             return;
9076         }
9077 
9078         if (!fp_access_check(s)) {
9079             return;
9080         }
9081 
9082         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9083         return;
9084     }
9085 
9086     switch (opcode) {
9087     case 0x1: /* SQADD, UQADD */
9088     case 0x5: /* SQSUB, UQSUB */
9089     case 0x9: /* SQSHL, UQSHL */
9090     case 0xb: /* SQRSHL, UQRSHL */
9091         break;
9092     case 0x8: /* SSHL, USHL */
9093     case 0xa: /* SRSHL, URSHL */
9094     case 0x6: /* CMGT, CMHI */
9095     case 0x7: /* CMGE, CMHS */
9096     case 0x11: /* CMTST, CMEQ */
9097     case 0x10: /* ADD, SUB (vector) */
9098         if (size != 3) {
9099             unallocated_encoding(s);
9100             return;
9101         }
9102         break;
9103     case 0x16: /* SQDMULH, SQRDMULH (vector) */
9104         if (size != 1 && size != 2) {
9105             unallocated_encoding(s);
9106             return;
9107         }
9108         break;
9109     default:
9110         unallocated_encoding(s);
9111         return;
9112     }
9113 
9114     if (!fp_access_check(s)) {
9115         return;
9116     }
9117 
9118     tcg_rd = tcg_temp_new_i64();
9119 
9120     if (size == 3) {
9121         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9122         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9123 
9124         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9125     } else {
9126         /* Do a single operation on the lowest element in the vector.
9127          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9128          * no side effects for all these operations.
9129          * OPTME: special-purpose helpers would avoid doing some
9130          * unnecessary work in the helper for the 8 and 16 bit cases.
9131          */
9132         NeonGenTwoOpEnvFn *genenvfn;
9133         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9134         TCGv_i32 tcg_rm = tcg_temp_new_i32();
9135         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9136 
9137         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9138         read_vec_element_i32(s, tcg_rm, rm, 0, size);
9139 
9140         switch (opcode) {
9141         case 0x1: /* SQADD, UQADD */
9142         {
9143             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9144                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9145                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9146                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9147             };
9148             genenvfn = fns[size][u];
9149             break;
9150         }
9151         case 0x5: /* SQSUB, UQSUB */
9152         {
9153             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9154                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9155                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9156                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9157             };
9158             genenvfn = fns[size][u];
9159             break;
9160         }
9161         case 0x9: /* SQSHL, UQSHL */
9162         {
9163             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9164                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9165                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9166                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9167             };
9168             genenvfn = fns[size][u];
9169             break;
9170         }
9171         case 0xb: /* SQRSHL, UQRSHL */
9172         {
9173             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9174                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9175                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9176                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9177             };
9178             genenvfn = fns[size][u];
9179             break;
9180         }
9181         case 0x16: /* SQDMULH, SQRDMULH */
9182         {
9183             static NeonGenTwoOpEnvFn * const fns[2][2] = {
9184                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9185                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9186             };
9187             assert(size == 1 || size == 2);
9188             genenvfn = fns[size - 1][u];
9189             break;
9190         }
9191         default:
9192             g_assert_not_reached();
9193         }
9194 
9195         genenvfn(tcg_rd32, tcg_env, tcg_rn, tcg_rm);
9196         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9197     }
9198 
9199     write_fp_dreg(s, rd, tcg_rd);
9200 }
9201 
9202 /* AdvSIMD scalar three same FP16
9203  *  31 30  29 28       24 23  22 21 20  16 15 14 13    11 10  9  5 4  0
9204  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9205  * | 0 1 | U | 1 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 | Rn | Rd |
9206  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9207  * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9208  * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9209  */
9210 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9211                                                   uint32_t insn)
9212 {
9213     int rd = extract32(insn, 0, 5);
9214     int rn = extract32(insn, 5, 5);
9215     int opcode = extract32(insn, 11, 3);
9216     int rm = extract32(insn, 16, 5);
9217     bool u = extract32(insn, 29, 1);
9218     bool a = extract32(insn, 23, 1);
9219     int fpopcode = opcode | (a << 3) |  (u << 4);
9220     TCGv_ptr fpst;
9221     TCGv_i32 tcg_op1;
9222     TCGv_i32 tcg_op2;
9223     TCGv_i32 tcg_res;
9224 
9225     switch (fpopcode) {
9226     case 0x03: /* FMULX */
9227     case 0x04: /* FCMEQ (reg) */
9228     case 0x07: /* FRECPS */
9229     case 0x0f: /* FRSQRTS */
9230     case 0x14: /* FCMGE (reg) */
9231     case 0x15: /* FACGE */
9232     case 0x1a: /* FABD */
9233     case 0x1c: /* FCMGT (reg) */
9234     case 0x1d: /* FACGT */
9235         break;
9236     default:
9237         unallocated_encoding(s);
9238         return;
9239     }
9240 
9241     if (!dc_isar_feature(aa64_fp16, s)) {
9242         unallocated_encoding(s);
9243     }
9244 
9245     if (!fp_access_check(s)) {
9246         return;
9247     }
9248 
9249     fpst = fpstatus_ptr(FPST_FPCR_F16);
9250 
9251     tcg_op1 = read_fp_hreg(s, rn);
9252     tcg_op2 = read_fp_hreg(s, rm);
9253     tcg_res = tcg_temp_new_i32();
9254 
9255     switch (fpopcode) {
9256     case 0x03: /* FMULX */
9257         gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9258         break;
9259     case 0x04: /* FCMEQ (reg) */
9260         gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9261         break;
9262     case 0x07: /* FRECPS */
9263         gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9264         break;
9265     case 0x0f: /* FRSQRTS */
9266         gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9267         break;
9268     case 0x14: /* FCMGE (reg) */
9269         gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9270         break;
9271     case 0x15: /* FACGE */
9272         gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9273         break;
9274     case 0x1a: /* FABD */
9275         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9276         tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9277         break;
9278     case 0x1c: /* FCMGT (reg) */
9279         gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9280         break;
9281     case 0x1d: /* FACGT */
9282         gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9283         break;
9284     default:
9285         g_assert_not_reached();
9286     }
9287 
9288     write_fp_sreg(s, rd, tcg_res);
9289 }
9290 
9291 /* AdvSIMD scalar three same extra
9292  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9293  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9294  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9295  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9296  */
9297 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9298                                                    uint32_t insn)
9299 {
9300     int rd = extract32(insn, 0, 5);
9301     int rn = extract32(insn, 5, 5);
9302     int opcode = extract32(insn, 11, 4);
9303     int rm = extract32(insn, 16, 5);
9304     int size = extract32(insn, 22, 2);
9305     bool u = extract32(insn, 29, 1);
9306     TCGv_i32 ele1, ele2, ele3;
9307     TCGv_i64 res;
9308     bool feature;
9309 
9310     switch (u * 16 + opcode) {
9311     case 0x10: /* SQRDMLAH (vector) */
9312     case 0x11: /* SQRDMLSH (vector) */
9313         if (size != 1 && size != 2) {
9314             unallocated_encoding(s);
9315             return;
9316         }
9317         feature = dc_isar_feature(aa64_rdm, s);
9318         break;
9319     default:
9320         unallocated_encoding(s);
9321         return;
9322     }
9323     if (!feature) {
9324         unallocated_encoding(s);
9325         return;
9326     }
9327     if (!fp_access_check(s)) {
9328         return;
9329     }
9330 
9331     /* Do a single operation on the lowest element in the vector.
9332      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9333      * with no side effects for all these operations.
9334      * OPTME: special-purpose helpers would avoid doing some
9335      * unnecessary work in the helper for the 16 bit cases.
9336      */
9337     ele1 = tcg_temp_new_i32();
9338     ele2 = tcg_temp_new_i32();
9339     ele3 = tcg_temp_new_i32();
9340 
9341     read_vec_element_i32(s, ele1, rn, 0, size);
9342     read_vec_element_i32(s, ele2, rm, 0, size);
9343     read_vec_element_i32(s, ele3, rd, 0, size);
9344 
9345     switch (opcode) {
9346     case 0x0: /* SQRDMLAH */
9347         if (size == 1) {
9348             gen_helper_neon_qrdmlah_s16(ele3, tcg_env, ele1, ele2, ele3);
9349         } else {
9350             gen_helper_neon_qrdmlah_s32(ele3, tcg_env, ele1, ele2, ele3);
9351         }
9352         break;
9353     case 0x1: /* SQRDMLSH */
9354         if (size == 1) {
9355             gen_helper_neon_qrdmlsh_s16(ele3, tcg_env, ele1, ele2, ele3);
9356         } else {
9357             gen_helper_neon_qrdmlsh_s32(ele3, tcg_env, ele1, ele2, ele3);
9358         }
9359         break;
9360     default:
9361         g_assert_not_reached();
9362     }
9363 
9364     res = tcg_temp_new_i64();
9365     tcg_gen_extu_i32_i64(res, ele3);
9366     write_fp_dreg(s, rd, res);
9367 }
9368 
9369 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9370                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9371                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9372 {
9373     /* Handle 64->64 opcodes which are shared between the scalar and
9374      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9375      * is valid in either group and also the double-precision fp ops.
9376      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9377      * requires them.
9378      */
9379     TCGCond cond;
9380 
9381     switch (opcode) {
9382     case 0x4: /* CLS, CLZ */
9383         if (u) {
9384             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9385         } else {
9386             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9387         }
9388         break;
9389     case 0x5: /* NOT */
9390         /* This opcode is shared with CNT and RBIT but we have earlier
9391          * enforced that size == 3 if and only if this is the NOT insn.
9392          */
9393         tcg_gen_not_i64(tcg_rd, tcg_rn);
9394         break;
9395     case 0x7: /* SQABS, SQNEG */
9396         if (u) {
9397             gen_helper_neon_qneg_s64(tcg_rd, tcg_env, tcg_rn);
9398         } else {
9399             gen_helper_neon_qabs_s64(tcg_rd, tcg_env, tcg_rn);
9400         }
9401         break;
9402     case 0xa: /* CMLT */
9403         cond = TCG_COND_LT;
9404     do_cmop:
9405         /* 64 bit integer comparison against zero, result is test ? -1 : 0. */
9406         tcg_gen_negsetcond_i64(cond, tcg_rd, tcg_rn, tcg_constant_i64(0));
9407         break;
9408     case 0x8: /* CMGT, CMGE */
9409         cond = u ? TCG_COND_GE : TCG_COND_GT;
9410         goto do_cmop;
9411     case 0x9: /* CMEQ, CMLE */
9412         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9413         goto do_cmop;
9414     case 0xb: /* ABS, NEG */
9415         if (u) {
9416             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9417         } else {
9418             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9419         }
9420         break;
9421     case 0x2f: /* FABS */
9422         gen_helper_vfp_absd(tcg_rd, tcg_rn);
9423         break;
9424     case 0x6f: /* FNEG */
9425         gen_helper_vfp_negd(tcg_rd, tcg_rn);
9426         break;
9427     case 0x7f: /* FSQRT */
9428         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, tcg_env);
9429         break;
9430     case 0x1a: /* FCVTNS */
9431     case 0x1b: /* FCVTMS */
9432     case 0x1c: /* FCVTAS */
9433     case 0x3a: /* FCVTPS */
9434     case 0x3b: /* FCVTZS */
9435         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9436         break;
9437     case 0x5a: /* FCVTNU */
9438     case 0x5b: /* FCVTMU */
9439     case 0x5c: /* FCVTAU */
9440     case 0x7a: /* FCVTPU */
9441     case 0x7b: /* FCVTZU */
9442         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9443         break;
9444     case 0x18: /* FRINTN */
9445     case 0x19: /* FRINTM */
9446     case 0x38: /* FRINTP */
9447     case 0x39: /* FRINTZ */
9448     case 0x58: /* FRINTA */
9449     case 0x79: /* FRINTI */
9450         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9451         break;
9452     case 0x59: /* FRINTX */
9453         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9454         break;
9455     case 0x1e: /* FRINT32Z */
9456     case 0x5e: /* FRINT32X */
9457         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9458         break;
9459     case 0x1f: /* FRINT64Z */
9460     case 0x5f: /* FRINT64X */
9461         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9462         break;
9463     default:
9464         g_assert_not_reached();
9465     }
9466 }
9467 
9468 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9469                                    bool is_scalar, bool is_u, bool is_q,
9470                                    int size, int rn, int rd)
9471 {
9472     bool is_double = (size == MO_64);
9473     TCGv_ptr fpst;
9474 
9475     if (!fp_access_check(s)) {
9476         return;
9477     }
9478 
9479     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9480 
9481     if (is_double) {
9482         TCGv_i64 tcg_op = tcg_temp_new_i64();
9483         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9484         TCGv_i64 tcg_res = tcg_temp_new_i64();
9485         NeonGenTwoDoubleOpFn *genfn;
9486         bool swap = false;
9487         int pass;
9488 
9489         switch (opcode) {
9490         case 0x2e: /* FCMLT (zero) */
9491             swap = true;
9492             /* fallthrough */
9493         case 0x2c: /* FCMGT (zero) */
9494             genfn = gen_helper_neon_cgt_f64;
9495             break;
9496         case 0x2d: /* FCMEQ (zero) */
9497             genfn = gen_helper_neon_ceq_f64;
9498             break;
9499         case 0x6d: /* FCMLE (zero) */
9500             swap = true;
9501             /* fall through */
9502         case 0x6c: /* FCMGE (zero) */
9503             genfn = gen_helper_neon_cge_f64;
9504             break;
9505         default:
9506             g_assert_not_reached();
9507         }
9508 
9509         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9510             read_vec_element(s, tcg_op, rn, pass, MO_64);
9511             if (swap) {
9512                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9513             } else {
9514                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9515             }
9516             write_vec_element(s, tcg_res, rd, pass, MO_64);
9517         }
9518 
9519         clear_vec_high(s, !is_scalar, rd);
9520     } else {
9521         TCGv_i32 tcg_op = tcg_temp_new_i32();
9522         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9523         TCGv_i32 tcg_res = tcg_temp_new_i32();
9524         NeonGenTwoSingleOpFn *genfn;
9525         bool swap = false;
9526         int pass, maxpasses;
9527 
9528         if (size == MO_16) {
9529             switch (opcode) {
9530             case 0x2e: /* FCMLT (zero) */
9531                 swap = true;
9532                 /* fall through */
9533             case 0x2c: /* FCMGT (zero) */
9534                 genfn = gen_helper_advsimd_cgt_f16;
9535                 break;
9536             case 0x2d: /* FCMEQ (zero) */
9537                 genfn = gen_helper_advsimd_ceq_f16;
9538                 break;
9539             case 0x6d: /* FCMLE (zero) */
9540                 swap = true;
9541                 /* fall through */
9542             case 0x6c: /* FCMGE (zero) */
9543                 genfn = gen_helper_advsimd_cge_f16;
9544                 break;
9545             default:
9546                 g_assert_not_reached();
9547             }
9548         } else {
9549             switch (opcode) {
9550             case 0x2e: /* FCMLT (zero) */
9551                 swap = true;
9552                 /* fall through */
9553             case 0x2c: /* FCMGT (zero) */
9554                 genfn = gen_helper_neon_cgt_f32;
9555                 break;
9556             case 0x2d: /* FCMEQ (zero) */
9557                 genfn = gen_helper_neon_ceq_f32;
9558                 break;
9559             case 0x6d: /* FCMLE (zero) */
9560                 swap = true;
9561                 /* fall through */
9562             case 0x6c: /* FCMGE (zero) */
9563                 genfn = gen_helper_neon_cge_f32;
9564                 break;
9565             default:
9566                 g_assert_not_reached();
9567             }
9568         }
9569 
9570         if (is_scalar) {
9571             maxpasses = 1;
9572         } else {
9573             int vector_size = 8 << is_q;
9574             maxpasses = vector_size >> size;
9575         }
9576 
9577         for (pass = 0; pass < maxpasses; pass++) {
9578             read_vec_element_i32(s, tcg_op, rn, pass, size);
9579             if (swap) {
9580                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9581             } else {
9582                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9583             }
9584             if (is_scalar) {
9585                 write_fp_sreg(s, rd, tcg_res);
9586             } else {
9587                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9588             }
9589         }
9590 
9591         if (!is_scalar) {
9592             clear_vec_high(s, is_q, rd);
9593         }
9594     }
9595 }
9596 
9597 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9598                                     bool is_scalar, bool is_u, bool is_q,
9599                                     int size, int rn, int rd)
9600 {
9601     bool is_double = (size == 3);
9602     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9603 
9604     if (is_double) {
9605         TCGv_i64 tcg_op = tcg_temp_new_i64();
9606         TCGv_i64 tcg_res = tcg_temp_new_i64();
9607         int pass;
9608 
9609         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9610             read_vec_element(s, tcg_op, rn, pass, MO_64);
9611             switch (opcode) {
9612             case 0x3d: /* FRECPE */
9613                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9614                 break;
9615             case 0x3f: /* FRECPX */
9616                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9617                 break;
9618             case 0x7d: /* FRSQRTE */
9619                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9620                 break;
9621             default:
9622                 g_assert_not_reached();
9623             }
9624             write_vec_element(s, tcg_res, rd, pass, MO_64);
9625         }
9626         clear_vec_high(s, !is_scalar, rd);
9627     } else {
9628         TCGv_i32 tcg_op = tcg_temp_new_i32();
9629         TCGv_i32 tcg_res = tcg_temp_new_i32();
9630         int pass, maxpasses;
9631 
9632         if (is_scalar) {
9633             maxpasses = 1;
9634         } else {
9635             maxpasses = is_q ? 4 : 2;
9636         }
9637 
9638         for (pass = 0; pass < maxpasses; pass++) {
9639             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9640 
9641             switch (opcode) {
9642             case 0x3c: /* URECPE */
9643                 gen_helper_recpe_u32(tcg_res, tcg_op);
9644                 break;
9645             case 0x3d: /* FRECPE */
9646                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9647                 break;
9648             case 0x3f: /* FRECPX */
9649                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9650                 break;
9651             case 0x7d: /* FRSQRTE */
9652                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9653                 break;
9654             default:
9655                 g_assert_not_reached();
9656             }
9657 
9658             if (is_scalar) {
9659                 write_fp_sreg(s, rd, tcg_res);
9660             } else {
9661                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9662             }
9663         }
9664         if (!is_scalar) {
9665             clear_vec_high(s, is_q, rd);
9666         }
9667     }
9668 }
9669 
9670 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9671                                 int opcode, bool u, bool is_q,
9672                                 int size, int rn, int rd)
9673 {
9674     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9675      * in the source becomes a size element in the destination).
9676      */
9677     int pass;
9678     TCGv_i32 tcg_res[2];
9679     int destelt = is_q ? 2 : 0;
9680     int passes = scalar ? 1 : 2;
9681 
9682     if (scalar) {
9683         tcg_res[1] = tcg_constant_i32(0);
9684     }
9685 
9686     for (pass = 0; pass < passes; pass++) {
9687         TCGv_i64 tcg_op = tcg_temp_new_i64();
9688         NeonGenNarrowFn *genfn = NULL;
9689         NeonGenNarrowEnvFn *genenvfn = NULL;
9690 
9691         if (scalar) {
9692             read_vec_element(s, tcg_op, rn, pass, size + 1);
9693         } else {
9694             read_vec_element(s, tcg_op, rn, pass, MO_64);
9695         }
9696         tcg_res[pass] = tcg_temp_new_i32();
9697 
9698         switch (opcode) {
9699         case 0x12: /* XTN, SQXTUN */
9700         {
9701             static NeonGenNarrowFn * const xtnfns[3] = {
9702                 gen_helper_neon_narrow_u8,
9703                 gen_helper_neon_narrow_u16,
9704                 tcg_gen_extrl_i64_i32,
9705             };
9706             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9707                 gen_helper_neon_unarrow_sat8,
9708                 gen_helper_neon_unarrow_sat16,
9709                 gen_helper_neon_unarrow_sat32,
9710             };
9711             if (u) {
9712                 genenvfn = sqxtunfns[size];
9713             } else {
9714                 genfn = xtnfns[size];
9715             }
9716             break;
9717         }
9718         case 0x14: /* SQXTN, UQXTN */
9719         {
9720             static NeonGenNarrowEnvFn * const fns[3][2] = {
9721                 { gen_helper_neon_narrow_sat_s8,
9722                   gen_helper_neon_narrow_sat_u8 },
9723                 { gen_helper_neon_narrow_sat_s16,
9724                   gen_helper_neon_narrow_sat_u16 },
9725                 { gen_helper_neon_narrow_sat_s32,
9726                   gen_helper_neon_narrow_sat_u32 },
9727             };
9728             genenvfn = fns[size][u];
9729             break;
9730         }
9731         case 0x16: /* FCVTN, FCVTN2 */
9732             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9733             if (size == 2) {
9734                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, tcg_env);
9735             } else {
9736                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9737                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9738                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9739                 TCGv_i32 ahp = get_ahp_flag();
9740 
9741                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9742                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9743                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9744                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9745             }
9746             break;
9747         case 0x36: /* BFCVTN, BFCVTN2 */
9748             {
9749                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9750                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
9751             }
9752             break;
9753         case 0x56:  /* FCVTXN, FCVTXN2 */
9754             /* 64 bit to 32 bit float conversion
9755              * with von Neumann rounding (round to odd)
9756              */
9757             assert(size == 2);
9758             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, tcg_env);
9759             break;
9760         default:
9761             g_assert_not_reached();
9762         }
9763 
9764         if (genfn) {
9765             genfn(tcg_res[pass], tcg_op);
9766         } else if (genenvfn) {
9767             genenvfn(tcg_res[pass], tcg_env, tcg_op);
9768         }
9769     }
9770 
9771     for (pass = 0; pass < 2; pass++) {
9772         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
9773     }
9774     clear_vec_high(s, is_q, rd);
9775 }
9776 
9777 /* Remaining saturating accumulating ops */
9778 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
9779                                 bool is_q, int size, int rn, int rd)
9780 {
9781     bool is_double = (size == 3);
9782 
9783     if (is_double) {
9784         TCGv_i64 tcg_rn = tcg_temp_new_i64();
9785         TCGv_i64 tcg_rd = tcg_temp_new_i64();
9786         int pass;
9787 
9788         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9789             read_vec_element(s, tcg_rn, rn, pass, MO_64);
9790             read_vec_element(s, tcg_rd, rd, pass, MO_64);
9791 
9792             if (is_u) { /* USQADD */
9793                 gen_helper_neon_uqadd_s64(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9794             } else { /* SUQADD */
9795                 gen_helper_neon_sqadd_u64(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9796             }
9797             write_vec_element(s, tcg_rd, rd, pass, MO_64);
9798         }
9799         clear_vec_high(s, !is_scalar, rd);
9800     } else {
9801         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9802         TCGv_i32 tcg_rd = tcg_temp_new_i32();
9803         int pass, maxpasses;
9804 
9805         if (is_scalar) {
9806             maxpasses = 1;
9807         } else {
9808             maxpasses = is_q ? 4 : 2;
9809         }
9810 
9811         for (pass = 0; pass < maxpasses; pass++) {
9812             if (is_scalar) {
9813                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
9814                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
9815             } else {
9816                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
9817                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9818             }
9819 
9820             if (is_u) { /* USQADD */
9821                 switch (size) {
9822                 case 0:
9823                     gen_helper_neon_uqadd_s8(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9824                     break;
9825                 case 1:
9826                     gen_helper_neon_uqadd_s16(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9827                     break;
9828                 case 2:
9829                     gen_helper_neon_uqadd_s32(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9830                     break;
9831                 default:
9832                     g_assert_not_reached();
9833                 }
9834             } else { /* SUQADD */
9835                 switch (size) {
9836                 case 0:
9837                     gen_helper_neon_sqadd_u8(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9838                     break;
9839                 case 1:
9840                     gen_helper_neon_sqadd_u16(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9841                     break;
9842                 case 2:
9843                     gen_helper_neon_sqadd_u32(tcg_rd, tcg_env, tcg_rn, tcg_rd);
9844                     break;
9845                 default:
9846                     g_assert_not_reached();
9847                 }
9848             }
9849 
9850             if (is_scalar) {
9851                 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
9852             }
9853             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9854         }
9855         clear_vec_high(s, is_q, rd);
9856     }
9857 }
9858 
9859 /* AdvSIMD scalar two reg misc
9860  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
9861  * +-----+---+-----------+------+-----------+--------+-----+------+------+
9862  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
9863  * +-----+---+-----------+------+-----------+--------+-----+------+------+
9864  */
9865 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
9866 {
9867     int rd = extract32(insn, 0, 5);
9868     int rn = extract32(insn, 5, 5);
9869     int opcode = extract32(insn, 12, 5);
9870     int size = extract32(insn, 22, 2);
9871     bool u = extract32(insn, 29, 1);
9872     bool is_fcvt = false;
9873     int rmode;
9874     TCGv_i32 tcg_rmode;
9875     TCGv_ptr tcg_fpstatus;
9876 
9877     switch (opcode) {
9878     case 0x3: /* USQADD / SUQADD*/
9879         if (!fp_access_check(s)) {
9880             return;
9881         }
9882         handle_2misc_satacc(s, true, u, false, size, rn, rd);
9883         return;
9884     case 0x7: /* SQABS / SQNEG */
9885         break;
9886     case 0xa: /* CMLT */
9887         if (u) {
9888             unallocated_encoding(s);
9889             return;
9890         }
9891         /* fall through */
9892     case 0x8: /* CMGT, CMGE */
9893     case 0x9: /* CMEQ, CMLE */
9894     case 0xb: /* ABS, NEG */
9895         if (size != 3) {
9896             unallocated_encoding(s);
9897             return;
9898         }
9899         break;
9900     case 0x12: /* SQXTUN */
9901         if (!u) {
9902             unallocated_encoding(s);
9903             return;
9904         }
9905         /* fall through */
9906     case 0x14: /* SQXTN, UQXTN */
9907         if (size == 3) {
9908             unallocated_encoding(s);
9909             return;
9910         }
9911         if (!fp_access_check(s)) {
9912             return;
9913         }
9914         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
9915         return;
9916     case 0xc ... 0xf:
9917     case 0x16 ... 0x1d:
9918     case 0x1f:
9919         /* Floating point: U, size[1] and opcode indicate operation;
9920          * size[0] indicates single or double precision.
9921          */
9922         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
9923         size = extract32(size, 0, 1) ? 3 : 2;
9924         switch (opcode) {
9925         case 0x2c: /* FCMGT (zero) */
9926         case 0x2d: /* FCMEQ (zero) */
9927         case 0x2e: /* FCMLT (zero) */
9928         case 0x6c: /* FCMGE (zero) */
9929         case 0x6d: /* FCMLE (zero) */
9930             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
9931             return;
9932         case 0x1d: /* SCVTF */
9933         case 0x5d: /* UCVTF */
9934         {
9935             bool is_signed = (opcode == 0x1d);
9936             if (!fp_access_check(s)) {
9937                 return;
9938             }
9939             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
9940             return;
9941         }
9942         case 0x3d: /* FRECPE */
9943         case 0x3f: /* FRECPX */
9944         case 0x7d: /* FRSQRTE */
9945             if (!fp_access_check(s)) {
9946                 return;
9947             }
9948             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
9949             return;
9950         case 0x1a: /* FCVTNS */
9951         case 0x1b: /* FCVTMS */
9952         case 0x3a: /* FCVTPS */
9953         case 0x3b: /* FCVTZS */
9954         case 0x5a: /* FCVTNU */
9955         case 0x5b: /* FCVTMU */
9956         case 0x7a: /* FCVTPU */
9957         case 0x7b: /* FCVTZU */
9958             is_fcvt = true;
9959             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
9960             break;
9961         case 0x1c: /* FCVTAS */
9962         case 0x5c: /* FCVTAU */
9963             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
9964             is_fcvt = true;
9965             rmode = FPROUNDING_TIEAWAY;
9966             break;
9967         case 0x56: /* FCVTXN, FCVTXN2 */
9968             if (size == 2) {
9969                 unallocated_encoding(s);
9970                 return;
9971             }
9972             if (!fp_access_check(s)) {
9973                 return;
9974             }
9975             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
9976             return;
9977         default:
9978             unallocated_encoding(s);
9979             return;
9980         }
9981         break;
9982     default:
9983         unallocated_encoding(s);
9984         return;
9985     }
9986 
9987     if (!fp_access_check(s)) {
9988         return;
9989     }
9990 
9991     if (is_fcvt) {
9992         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
9993         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
9994     } else {
9995         tcg_fpstatus = NULL;
9996         tcg_rmode = NULL;
9997     }
9998 
9999     if (size == 3) {
10000         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10001         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10002 
10003         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10004         write_fp_dreg(s, rd, tcg_rd);
10005     } else {
10006         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10007         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10008 
10009         read_vec_element_i32(s, tcg_rn, rn, 0, size);
10010 
10011         switch (opcode) {
10012         case 0x7: /* SQABS, SQNEG */
10013         {
10014             NeonGenOneOpEnvFn *genfn;
10015             static NeonGenOneOpEnvFn * const fns[3][2] = {
10016                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10017                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10018                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10019             };
10020             genfn = fns[size][u];
10021             genfn(tcg_rd, tcg_env, tcg_rn);
10022             break;
10023         }
10024         case 0x1a: /* FCVTNS */
10025         case 0x1b: /* FCVTMS */
10026         case 0x1c: /* FCVTAS */
10027         case 0x3a: /* FCVTPS */
10028         case 0x3b: /* FCVTZS */
10029             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10030                                  tcg_fpstatus);
10031             break;
10032         case 0x5a: /* FCVTNU */
10033         case 0x5b: /* FCVTMU */
10034         case 0x5c: /* FCVTAU */
10035         case 0x7a: /* FCVTPU */
10036         case 0x7b: /* FCVTZU */
10037             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10038                                  tcg_fpstatus);
10039             break;
10040         default:
10041             g_assert_not_reached();
10042         }
10043 
10044         write_fp_sreg(s, rd, tcg_rd);
10045     }
10046 
10047     if (is_fcvt) {
10048         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
10049     }
10050 }
10051 
10052 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10053 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10054                                  int immh, int immb, int opcode, int rn, int rd)
10055 {
10056     int size = 32 - clz32(immh) - 1;
10057     int immhb = immh << 3 | immb;
10058     int shift = 2 * (8 << size) - immhb;
10059     GVecGen2iFn *gvec_fn;
10060 
10061     if (extract32(immh, 3, 1) && !is_q) {
10062         unallocated_encoding(s);
10063         return;
10064     }
10065     tcg_debug_assert(size <= 3);
10066 
10067     if (!fp_access_check(s)) {
10068         return;
10069     }
10070 
10071     switch (opcode) {
10072     case 0x02: /* SSRA / USRA (accumulate) */
10073         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10074         break;
10075 
10076     case 0x08: /* SRI */
10077         gvec_fn = gen_gvec_sri;
10078         break;
10079 
10080     case 0x00: /* SSHR / USHR */
10081         if (is_u) {
10082             if (shift == 8 << size) {
10083                 /* Shift count the same size as element size produces zero.  */
10084                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10085                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
10086                 return;
10087             }
10088             gvec_fn = tcg_gen_gvec_shri;
10089         } else {
10090             /* Shift count the same size as element size produces all sign.  */
10091             if (shift == 8 << size) {
10092                 shift -= 1;
10093             }
10094             gvec_fn = tcg_gen_gvec_sari;
10095         }
10096         break;
10097 
10098     case 0x04: /* SRSHR / URSHR (rounding) */
10099         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10100         break;
10101 
10102     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10103         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10104         break;
10105 
10106     default:
10107         g_assert_not_reached();
10108     }
10109 
10110     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10111 }
10112 
10113 /* SHL/SLI - Vector shift left */
10114 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10115                                  int immh, int immb, int opcode, int rn, int rd)
10116 {
10117     int size = 32 - clz32(immh) - 1;
10118     int immhb = immh << 3 | immb;
10119     int shift = immhb - (8 << size);
10120 
10121     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10122     assert(size >= 0 && size <= 3);
10123 
10124     if (extract32(immh, 3, 1) && !is_q) {
10125         unallocated_encoding(s);
10126         return;
10127     }
10128 
10129     if (!fp_access_check(s)) {
10130         return;
10131     }
10132 
10133     if (insert) {
10134         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10135     } else {
10136         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10137     }
10138 }
10139 
10140 /* USHLL/SHLL - Vector shift left with widening */
10141 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10142                                  int immh, int immb, int opcode, int rn, int rd)
10143 {
10144     int size = 32 - clz32(immh) - 1;
10145     int immhb = immh << 3 | immb;
10146     int shift = immhb - (8 << size);
10147     int dsize = 64;
10148     int esize = 8 << size;
10149     int elements = dsize/esize;
10150     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10151     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10152     int i;
10153 
10154     if (size >= 3) {
10155         unallocated_encoding(s);
10156         return;
10157     }
10158 
10159     if (!fp_access_check(s)) {
10160         return;
10161     }
10162 
10163     /* For the LL variants the store is larger than the load,
10164      * so if rd == rn we would overwrite parts of our input.
10165      * So load everything right now and use shifts in the main loop.
10166      */
10167     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10168 
10169     for (i = 0; i < elements; i++) {
10170         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10171         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10172         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10173         write_vec_element(s, tcg_rd, rd, i, size + 1);
10174     }
10175 }
10176 
10177 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10178 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10179                                  int immh, int immb, int opcode, int rn, int rd)
10180 {
10181     int immhb = immh << 3 | immb;
10182     int size = 32 - clz32(immh) - 1;
10183     int dsize = 64;
10184     int esize = 8 << size;
10185     int elements = dsize/esize;
10186     int shift = (2 * esize) - immhb;
10187     bool round = extract32(opcode, 0, 1);
10188     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10189     TCGv_i64 tcg_round;
10190     int i;
10191 
10192     if (extract32(immh, 3, 1)) {
10193         unallocated_encoding(s);
10194         return;
10195     }
10196 
10197     if (!fp_access_check(s)) {
10198         return;
10199     }
10200 
10201     tcg_rn = tcg_temp_new_i64();
10202     tcg_rd = tcg_temp_new_i64();
10203     tcg_final = tcg_temp_new_i64();
10204     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10205 
10206     if (round) {
10207         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10208     } else {
10209         tcg_round = NULL;
10210     }
10211 
10212     for (i = 0; i < elements; i++) {
10213         read_vec_element(s, tcg_rn, rn, i, size+1);
10214         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10215                                 false, true, size+1, shift);
10216 
10217         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10218     }
10219 
10220     if (!is_q) {
10221         write_vec_element(s, tcg_final, rd, 0, MO_64);
10222     } else {
10223         write_vec_element(s, tcg_final, rd, 1, MO_64);
10224     }
10225 
10226     clear_vec_high(s, is_q, rd);
10227 }
10228 
10229 
10230 /* AdvSIMD shift by immediate
10231  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10232  * +---+---+---+-------------+------+------+--------+---+------+------+
10233  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10234  * +---+---+---+-------------+------+------+--------+---+------+------+
10235  */
10236 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10237 {
10238     int rd = extract32(insn, 0, 5);
10239     int rn = extract32(insn, 5, 5);
10240     int opcode = extract32(insn, 11, 5);
10241     int immb = extract32(insn, 16, 3);
10242     int immh = extract32(insn, 19, 4);
10243     bool is_u = extract32(insn, 29, 1);
10244     bool is_q = extract32(insn, 30, 1);
10245 
10246     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10247     assert(immh != 0);
10248 
10249     switch (opcode) {
10250     case 0x08: /* SRI */
10251         if (!is_u) {
10252             unallocated_encoding(s);
10253             return;
10254         }
10255         /* fall through */
10256     case 0x00: /* SSHR / USHR */
10257     case 0x02: /* SSRA / USRA (accumulate) */
10258     case 0x04: /* SRSHR / URSHR (rounding) */
10259     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10260         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10261         break;
10262     case 0x0a: /* SHL / SLI */
10263         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10264         break;
10265     case 0x10: /* SHRN */
10266     case 0x11: /* RSHRN / SQRSHRUN */
10267         if (is_u) {
10268             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10269                                    opcode, rn, rd);
10270         } else {
10271             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10272         }
10273         break;
10274     case 0x12: /* SQSHRN / UQSHRN */
10275     case 0x13: /* SQRSHRN / UQRSHRN */
10276         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10277                                opcode, rn, rd);
10278         break;
10279     case 0x14: /* SSHLL / USHLL */
10280         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10281         break;
10282     case 0x1c: /* SCVTF / UCVTF */
10283         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10284                                      opcode, rn, rd);
10285         break;
10286     case 0xc: /* SQSHLU */
10287         if (!is_u) {
10288             unallocated_encoding(s);
10289             return;
10290         }
10291         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10292         break;
10293     case 0xe: /* SQSHL, UQSHL */
10294         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10295         break;
10296     case 0x1f: /* FCVTZS/ FCVTZU */
10297         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10298         return;
10299     default:
10300         unallocated_encoding(s);
10301         return;
10302     }
10303 }
10304 
10305 /* Generate code to do a "long" addition or subtraction, ie one done in
10306  * TCGv_i64 on vector lanes twice the width specified by size.
10307  */
10308 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10309                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10310 {
10311     static NeonGenTwo64OpFn * const fns[3][2] = {
10312         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10313         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10314         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10315     };
10316     NeonGenTwo64OpFn *genfn;
10317     assert(size < 3);
10318 
10319     genfn = fns[size][is_sub];
10320     genfn(tcg_res, tcg_op1, tcg_op2);
10321 }
10322 
10323 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10324                                 int opcode, int rd, int rn, int rm)
10325 {
10326     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10327     TCGv_i64 tcg_res[2];
10328     int pass, accop;
10329 
10330     tcg_res[0] = tcg_temp_new_i64();
10331     tcg_res[1] = tcg_temp_new_i64();
10332 
10333     /* Does this op do an adding accumulate, a subtracting accumulate,
10334      * or no accumulate at all?
10335      */
10336     switch (opcode) {
10337     case 5:
10338     case 8:
10339     case 9:
10340         accop = 1;
10341         break;
10342     case 10:
10343     case 11:
10344         accop = -1;
10345         break;
10346     default:
10347         accop = 0;
10348         break;
10349     }
10350 
10351     if (accop != 0) {
10352         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10353         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10354     }
10355 
10356     /* size == 2 means two 32x32->64 operations; this is worth special
10357      * casing because we can generally handle it inline.
10358      */
10359     if (size == 2) {
10360         for (pass = 0; pass < 2; pass++) {
10361             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10362             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10363             TCGv_i64 tcg_passres;
10364             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10365 
10366             int elt = pass + is_q * 2;
10367 
10368             read_vec_element(s, tcg_op1, rn, elt, memop);
10369             read_vec_element(s, tcg_op2, rm, elt, memop);
10370 
10371             if (accop == 0) {
10372                 tcg_passres = tcg_res[pass];
10373             } else {
10374                 tcg_passres = tcg_temp_new_i64();
10375             }
10376 
10377             switch (opcode) {
10378             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10379                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10380                 break;
10381             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10382                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10383                 break;
10384             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10385             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10386             {
10387                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10388                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10389 
10390                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10391                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10392                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10393                                     tcg_passres,
10394                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10395                 break;
10396             }
10397             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10398             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10399             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10400                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10401                 break;
10402             case 9: /* SQDMLAL, SQDMLAL2 */
10403             case 11: /* SQDMLSL, SQDMLSL2 */
10404             case 13: /* SQDMULL, SQDMULL2 */
10405                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10406                 gen_helper_neon_addl_saturate_s64(tcg_passres, tcg_env,
10407                                                   tcg_passres, tcg_passres);
10408                 break;
10409             default:
10410                 g_assert_not_reached();
10411             }
10412 
10413             if (opcode == 9 || opcode == 11) {
10414                 /* saturating accumulate ops */
10415                 if (accop < 0) {
10416                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10417                 }
10418                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], tcg_env,
10419                                                   tcg_res[pass], tcg_passres);
10420             } else if (accop > 0) {
10421                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10422             } else if (accop < 0) {
10423                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10424             }
10425         }
10426     } else {
10427         /* size 0 or 1, generally helper functions */
10428         for (pass = 0; pass < 2; pass++) {
10429             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10430             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10431             TCGv_i64 tcg_passres;
10432             int elt = pass + is_q * 2;
10433 
10434             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10435             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10436 
10437             if (accop == 0) {
10438                 tcg_passres = tcg_res[pass];
10439             } else {
10440                 tcg_passres = tcg_temp_new_i64();
10441             }
10442 
10443             switch (opcode) {
10444             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10445             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10446             {
10447                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10448                 static NeonGenWidenFn * const widenfns[2][2] = {
10449                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10450                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10451                 };
10452                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10453 
10454                 widenfn(tcg_op2_64, tcg_op2);
10455                 widenfn(tcg_passres, tcg_op1);
10456                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10457                               tcg_passres, tcg_op2_64);
10458                 break;
10459             }
10460             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10461             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10462                 if (size == 0) {
10463                     if (is_u) {
10464                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10465                     } else {
10466                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10467                     }
10468                 } else {
10469                     if (is_u) {
10470                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10471                     } else {
10472                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10473                     }
10474                 }
10475                 break;
10476             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10477             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10478             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10479                 if (size == 0) {
10480                     if (is_u) {
10481                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10482                     } else {
10483                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10484                     }
10485                 } else {
10486                     if (is_u) {
10487                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10488                     } else {
10489                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10490                     }
10491                 }
10492                 break;
10493             case 9: /* SQDMLAL, SQDMLAL2 */
10494             case 11: /* SQDMLSL, SQDMLSL2 */
10495             case 13: /* SQDMULL, SQDMULL2 */
10496                 assert(size == 1);
10497                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10498                 gen_helper_neon_addl_saturate_s32(tcg_passres, tcg_env,
10499                                                   tcg_passres, tcg_passres);
10500                 break;
10501             default:
10502                 g_assert_not_reached();
10503             }
10504 
10505             if (accop != 0) {
10506                 if (opcode == 9 || opcode == 11) {
10507                     /* saturating accumulate ops */
10508                     if (accop < 0) {
10509                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10510                     }
10511                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], tcg_env,
10512                                                       tcg_res[pass],
10513                                                       tcg_passres);
10514                 } else {
10515                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10516                                   tcg_res[pass], tcg_passres);
10517                 }
10518             }
10519         }
10520     }
10521 
10522     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10523     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10524 }
10525 
10526 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10527                             int opcode, int rd, int rn, int rm)
10528 {
10529     TCGv_i64 tcg_res[2];
10530     int part = is_q ? 2 : 0;
10531     int pass;
10532 
10533     for (pass = 0; pass < 2; pass++) {
10534         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10535         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10536         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10537         static NeonGenWidenFn * const widenfns[3][2] = {
10538             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10539             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10540             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10541         };
10542         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10543 
10544         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10545         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10546         widenfn(tcg_op2_wide, tcg_op2);
10547         tcg_res[pass] = tcg_temp_new_i64();
10548         gen_neon_addl(size, (opcode == 3),
10549                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10550     }
10551 
10552     for (pass = 0; pass < 2; pass++) {
10553         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10554     }
10555 }
10556 
10557 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10558 {
10559     tcg_gen_addi_i64(in, in, 1U << 31);
10560     tcg_gen_extrh_i64_i32(res, in);
10561 }
10562 
10563 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10564                                  int opcode, int rd, int rn, int rm)
10565 {
10566     TCGv_i32 tcg_res[2];
10567     int part = is_q ? 2 : 0;
10568     int pass;
10569 
10570     for (pass = 0; pass < 2; pass++) {
10571         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10572         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10573         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10574         static NeonGenNarrowFn * const narrowfns[3][2] = {
10575             { gen_helper_neon_narrow_high_u8,
10576               gen_helper_neon_narrow_round_high_u8 },
10577             { gen_helper_neon_narrow_high_u16,
10578               gen_helper_neon_narrow_round_high_u16 },
10579             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10580         };
10581         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10582 
10583         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10584         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10585 
10586         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10587 
10588         tcg_res[pass] = tcg_temp_new_i32();
10589         gennarrow(tcg_res[pass], tcg_wideres);
10590     }
10591 
10592     for (pass = 0; pass < 2; pass++) {
10593         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10594     }
10595     clear_vec_high(s, is_q, rd);
10596 }
10597 
10598 /* AdvSIMD three different
10599  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10600  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10601  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10602  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10603  */
10604 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10605 {
10606     /* Instructions in this group fall into three basic classes
10607      * (in each case with the operation working on each element in
10608      * the input vectors):
10609      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10610      *     128 bit input)
10611      * (2) wide 64 x 128 -> 128
10612      * (3) narrowing 128 x 128 -> 64
10613      * Here we do initial decode, catch unallocated cases and
10614      * dispatch to separate functions for each class.
10615      */
10616     int is_q = extract32(insn, 30, 1);
10617     int is_u = extract32(insn, 29, 1);
10618     int size = extract32(insn, 22, 2);
10619     int opcode = extract32(insn, 12, 4);
10620     int rm = extract32(insn, 16, 5);
10621     int rn = extract32(insn, 5, 5);
10622     int rd = extract32(insn, 0, 5);
10623 
10624     switch (opcode) {
10625     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10626     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10627         /* 64 x 128 -> 128 */
10628         if (size == 3) {
10629             unallocated_encoding(s);
10630             return;
10631         }
10632         if (!fp_access_check(s)) {
10633             return;
10634         }
10635         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10636         break;
10637     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10638     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10639         /* 128 x 128 -> 64 */
10640         if (size == 3) {
10641             unallocated_encoding(s);
10642             return;
10643         }
10644         if (!fp_access_check(s)) {
10645             return;
10646         }
10647         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10648         break;
10649     case 14: /* PMULL, PMULL2 */
10650         if (is_u) {
10651             unallocated_encoding(s);
10652             return;
10653         }
10654         switch (size) {
10655         case 0: /* PMULL.P8 */
10656             if (!fp_access_check(s)) {
10657                 return;
10658             }
10659             /* The Q field specifies lo/hi half input for this insn.  */
10660             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10661                              gen_helper_neon_pmull_h);
10662             break;
10663 
10664         case 3: /* PMULL.P64 */
10665             if (!dc_isar_feature(aa64_pmull, s)) {
10666                 unallocated_encoding(s);
10667                 return;
10668             }
10669             if (!fp_access_check(s)) {
10670                 return;
10671             }
10672             /* The Q field specifies lo/hi half input for this insn.  */
10673             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10674                              gen_helper_gvec_pmull_q);
10675             break;
10676 
10677         default:
10678             unallocated_encoding(s);
10679             break;
10680         }
10681         return;
10682     case 9: /* SQDMLAL, SQDMLAL2 */
10683     case 11: /* SQDMLSL, SQDMLSL2 */
10684     case 13: /* SQDMULL, SQDMULL2 */
10685         if (is_u || size == 0) {
10686             unallocated_encoding(s);
10687             return;
10688         }
10689         /* fall through */
10690     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10691     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10692     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10693     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10694     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10695     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10696     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10697         /* 64 x 64 -> 128 */
10698         if (size == 3) {
10699             unallocated_encoding(s);
10700             return;
10701         }
10702         if (!fp_access_check(s)) {
10703             return;
10704         }
10705 
10706         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10707         break;
10708     default:
10709         /* opcode 15 not allocated */
10710         unallocated_encoding(s);
10711         break;
10712     }
10713 }
10714 
10715 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10716 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10717 {
10718     int rd = extract32(insn, 0, 5);
10719     int rn = extract32(insn, 5, 5);
10720     int rm = extract32(insn, 16, 5);
10721     int size = extract32(insn, 22, 2);
10722     bool is_u = extract32(insn, 29, 1);
10723     bool is_q = extract32(insn, 30, 1);
10724 
10725     if (!fp_access_check(s)) {
10726         return;
10727     }
10728 
10729     switch (size + 4 * is_u) {
10730     case 0: /* AND */
10731         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10732         return;
10733     case 1: /* BIC */
10734         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10735         return;
10736     case 2: /* ORR */
10737         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10738         return;
10739     case 3: /* ORN */
10740         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10741         return;
10742     case 4: /* EOR */
10743         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10744         return;
10745 
10746     case 5: /* BSL bitwise select */
10747         gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
10748         return;
10749     case 6: /* BIT, bitwise insert if true */
10750         gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
10751         return;
10752     case 7: /* BIF, bitwise insert if false */
10753         gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
10754         return;
10755 
10756     default:
10757         g_assert_not_reached();
10758     }
10759 }
10760 
10761 /* Pairwise op subgroup of C3.6.16.
10762  *
10763  * This is called directly or via the handle_3same_float for float pairwise
10764  * operations where the opcode and size are calculated differently.
10765  */
10766 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
10767                                    int size, int rn, int rm, int rd)
10768 {
10769     TCGv_ptr fpst;
10770     int pass;
10771 
10772     /* Floating point operations need fpst */
10773     if (opcode >= 0x58) {
10774         fpst = fpstatus_ptr(FPST_FPCR);
10775     } else {
10776         fpst = NULL;
10777     }
10778 
10779     if (!fp_access_check(s)) {
10780         return;
10781     }
10782 
10783     /* These operations work on the concatenated rm:rn, with each pair of
10784      * adjacent elements being operated on to produce an element in the result.
10785      */
10786     if (size == 3) {
10787         TCGv_i64 tcg_res[2];
10788 
10789         for (pass = 0; pass < 2; pass++) {
10790             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10791             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10792             int passreg = (pass == 0) ? rn : rm;
10793 
10794             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
10795             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
10796             tcg_res[pass] = tcg_temp_new_i64();
10797 
10798             switch (opcode) {
10799             case 0x17: /* ADDP */
10800                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
10801                 break;
10802             case 0x58: /* FMAXNMP */
10803                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10804                 break;
10805             case 0x5a: /* FADDP */
10806                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10807                 break;
10808             case 0x5e: /* FMAXP */
10809                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10810                 break;
10811             case 0x78: /* FMINNMP */
10812                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10813                 break;
10814             case 0x7e: /* FMINP */
10815                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10816                 break;
10817             default:
10818                 g_assert_not_reached();
10819             }
10820         }
10821 
10822         for (pass = 0; pass < 2; pass++) {
10823             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10824         }
10825     } else {
10826         int maxpass = is_q ? 4 : 2;
10827         TCGv_i32 tcg_res[4];
10828 
10829         for (pass = 0; pass < maxpass; pass++) {
10830             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10831             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10832             NeonGenTwoOpFn *genfn = NULL;
10833             int passreg = pass < (maxpass / 2) ? rn : rm;
10834             int passelt = (is_q && (pass & 1)) ? 2 : 0;
10835 
10836             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
10837             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
10838             tcg_res[pass] = tcg_temp_new_i32();
10839 
10840             switch (opcode) {
10841             case 0x17: /* ADDP */
10842             {
10843                 static NeonGenTwoOpFn * const fns[3] = {
10844                     gen_helper_neon_padd_u8,
10845                     gen_helper_neon_padd_u16,
10846                     tcg_gen_add_i32,
10847                 };
10848                 genfn = fns[size];
10849                 break;
10850             }
10851             case 0x14: /* SMAXP, UMAXP */
10852             {
10853                 static NeonGenTwoOpFn * const fns[3][2] = {
10854                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
10855                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
10856                     { tcg_gen_smax_i32, tcg_gen_umax_i32 },
10857                 };
10858                 genfn = fns[size][u];
10859                 break;
10860             }
10861             case 0x15: /* SMINP, UMINP */
10862             {
10863                 static NeonGenTwoOpFn * const fns[3][2] = {
10864                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
10865                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
10866                     { tcg_gen_smin_i32, tcg_gen_umin_i32 },
10867                 };
10868                 genfn = fns[size][u];
10869                 break;
10870             }
10871             /* The FP operations are all on single floats (32 bit) */
10872             case 0x58: /* FMAXNMP */
10873                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10874                 break;
10875             case 0x5a: /* FADDP */
10876                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10877                 break;
10878             case 0x5e: /* FMAXP */
10879                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10880                 break;
10881             case 0x78: /* FMINNMP */
10882                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10883                 break;
10884             case 0x7e: /* FMINP */
10885                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10886                 break;
10887             default:
10888                 g_assert_not_reached();
10889             }
10890 
10891             /* FP ops called directly, otherwise call now */
10892             if (genfn) {
10893                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
10894             }
10895         }
10896 
10897         for (pass = 0; pass < maxpass; pass++) {
10898             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
10899         }
10900         clear_vec_high(s, is_q, rd);
10901     }
10902 }
10903 
10904 /* Floating point op subgroup of C3.6.16. */
10905 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
10906 {
10907     /* For floating point ops, the U, size[1] and opcode bits
10908      * together indicate the operation. size[0] indicates single
10909      * or double.
10910      */
10911     int fpopcode = extract32(insn, 11, 5)
10912         | (extract32(insn, 23, 1) << 5)
10913         | (extract32(insn, 29, 1) << 6);
10914     int is_q = extract32(insn, 30, 1);
10915     int size = extract32(insn, 22, 1);
10916     int rm = extract32(insn, 16, 5);
10917     int rn = extract32(insn, 5, 5);
10918     int rd = extract32(insn, 0, 5);
10919 
10920     int datasize = is_q ? 128 : 64;
10921     int esize = 32 << size;
10922     int elements = datasize / esize;
10923 
10924     if (size == 1 && !is_q) {
10925         unallocated_encoding(s);
10926         return;
10927     }
10928 
10929     switch (fpopcode) {
10930     case 0x58: /* FMAXNMP */
10931     case 0x5a: /* FADDP */
10932     case 0x5e: /* FMAXP */
10933     case 0x78: /* FMINNMP */
10934     case 0x7e: /* FMINP */
10935         if (size && !is_q) {
10936             unallocated_encoding(s);
10937             return;
10938         }
10939         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
10940                                rn, rm, rd);
10941         return;
10942     case 0x1b: /* FMULX */
10943     case 0x1f: /* FRECPS */
10944     case 0x3f: /* FRSQRTS */
10945     case 0x5d: /* FACGE */
10946     case 0x7d: /* FACGT */
10947     case 0x19: /* FMLA */
10948     case 0x39: /* FMLS */
10949     case 0x18: /* FMAXNM */
10950     case 0x1a: /* FADD */
10951     case 0x1c: /* FCMEQ */
10952     case 0x1e: /* FMAX */
10953     case 0x38: /* FMINNM */
10954     case 0x3a: /* FSUB */
10955     case 0x3e: /* FMIN */
10956     case 0x5b: /* FMUL */
10957     case 0x5c: /* FCMGE */
10958     case 0x5f: /* FDIV */
10959     case 0x7a: /* FABD */
10960     case 0x7c: /* FCMGT */
10961         if (!fp_access_check(s)) {
10962             return;
10963         }
10964         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
10965         return;
10966 
10967     case 0x1d: /* FMLAL  */
10968     case 0x3d: /* FMLSL  */
10969     case 0x59: /* FMLAL2 */
10970     case 0x79: /* FMLSL2 */
10971         if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
10972             unallocated_encoding(s);
10973             return;
10974         }
10975         if (fp_access_check(s)) {
10976             int is_s = extract32(insn, 23, 1);
10977             int is_2 = extract32(insn, 29, 1);
10978             int data = (is_2 << 1) | is_s;
10979             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
10980                                vec_full_reg_offset(s, rn),
10981                                vec_full_reg_offset(s, rm), tcg_env,
10982                                is_q ? 16 : 8, vec_full_reg_size(s),
10983                                data, gen_helper_gvec_fmlal_a64);
10984         }
10985         return;
10986 
10987     default:
10988         unallocated_encoding(s);
10989         return;
10990     }
10991 }
10992 
10993 /* Integer op subgroup of C3.6.16. */
10994 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
10995 {
10996     int is_q = extract32(insn, 30, 1);
10997     int u = extract32(insn, 29, 1);
10998     int size = extract32(insn, 22, 2);
10999     int opcode = extract32(insn, 11, 5);
11000     int rm = extract32(insn, 16, 5);
11001     int rn = extract32(insn, 5, 5);
11002     int rd = extract32(insn, 0, 5);
11003     int pass;
11004     TCGCond cond;
11005 
11006     switch (opcode) {
11007     case 0x13: /* MUL, PMUL */
11008         if (u && size != 0) {
11009             unallocated_encoding(s);
11010             return;
11011         }
11012         /* fall through */
11013     case 0x0: /* SHADD, UHADD */
11014     case 0x2: /* SRHADD, URHADD */
11015     case 0x4: /* SHSUB, UHSUB */
11016     case 0xc: /* SMAX, UMAX */
11017     case 0xd: /* SMIN, UMIN */
11018     case 0xe: /* SABD, UABD */
11019     case 0xf: /* SABA, UABA */
11020     case 0x12: /* MLA, MLS */
11021         if (size == 3) {
11022             unallocated_encoding(s);
11023             return;
11024         }
11025         break;
11026     case 0x16: /* SQDMULH, SQRDMULH */
11027         if (size == 0 || size == 3) {
11028             unallocated_encoding(s);
11029             return;
11030         }
11031         break;
11032     default:
11033         if (size == 3 && !is_q) {
11034             unallocated_encoding(s);
11035             return;
11036         }
11037         break;
11038     }
11039 
11040     if (!fp_access_check(s)) {
11041         return;
11042     }
11043 
11044     switch (opcode) {
11045     case 0x01: /* SQADD, UQADD */
11046         if (u) {
11047             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11048         } else {
11049             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11050         }
11051         return;
11052     case 0x05: /* SQSUB, UQSUB */
11053         if (u) {
11054             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11055         } else {
11056             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11057         }
11058         return;
11059     case 0x08: /* SSHL, USHL */
11060         if (u) {
11061             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11062         } else {
11063             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11064         }
11065         return;
11066     case 0x0c: /* SMAX, UMAX */
11067         if (u) {
11068             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11069         } else {
11070             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11071         }
11072         return;
11073     case 0x0d: /* SMIN, UMIN */
11074         if (u) {
11075             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11076         } else {
11077             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11078         }
11079         return;
11080     case 0xe: /* SABD, UABD */
11081         if (u) {
11082             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11083         } else {
11084             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11085         }
11086         return;
11087     case 0xf: /* SABA, UABA */
11088         if (u) {
11089             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11090         } else {
11091             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11092         }
11093         return;
11094     case 0x10: /* ADD, SUB */
11095         if (u) {
11096             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11097         } else {
11098             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11099         }
11100         return;
11101     case 0x13: /* MUL, PMUL */
11102         if (!u) { /* MUL */
11103             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11104         } else {  /* PMUL */
11105             gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11106         }
11107         return;
11108     case 0x12: /* MLA, MLS */
11109         if (u) {
11110             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11111         } else {
11112             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11113         }
11114         return;
11115     case 0x16: /* SQDMULH, SQRDMULH */
11116         {
11117             static gen_helper_gvec_3_ptr * const fns[2][2] = {
11118                 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11119                 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11120             };
11121             gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11122         }
11123         return;
11124     case 0x11:
11125         if (!u) { /* CMTST */
11126             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11127             return;
11128         }
11129         /* else CMEQ */
11130         cond = TCG_COND_EQ;
11131         goto do_gvec_cmp;
11132     case 0x06: /* CMGT, CMHI */
11133         cond = u ? TCG_COND_GTU : TCG_COND_GT;
11134         goto do_gvec_cmp;
11135     case 0x07: /* CMGE, CMHS */
11136         cond = u ? TCG_COND_GEU : TCG_COND_GE;
11137     do_gvec_cmp:
11138         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11139                          vec_full_reg_offset(s, rn),
11140                          vec_full_reg_offset(s, rm),
11141                          is_q ? 16 : 8, vec_full_reg_size(s));
11142         return;
11143     }
11144 
11145     if (size == 3) {
11146         assert(is_q);
11147         for (pass = 0; pass < 2; pass++) {
11148             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11149             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11150             TCGv_i64 tcg_res = tcg_temp_new_i64();
11151 
11152             read_vec_element(s, tcg_op1, rn, pass, MO_64);
11153             read_vec_element(s, tcg_op2, rm, pass, MO_64);
11154 
11155             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11156 
11157             write_vec_element(s, tcg_res, rd, pass, MO_64);
11158         }
11159     } else {
11160         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11161             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11162             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11163             TCGv_i32 tcg_res = tcg_temp_new_i32();
11164             NeonGenTwoOpFn *genfn = NULL;
11165             NeonGenTwoOpEnvFn *genenvfn = NULL;
11166 
11167             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11168             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11169 
11170             switch (opcode) {
11171             case 0x0: /* SHADD, UHADD */
11172             {
11173                 static NeonGenTwoOpFn * const fns[3][2] = {
11174                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11175                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11176                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11177                 };
11178                 genfn = fns[size][u];
11179                 break;
11180             }
11181             case 0x2: /* SRHADD, URHADD */
11182             {
11183                 static NeonGenTwoOpFn * const fns[3][2] = {
11184                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11185                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11186                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11187                 };
11188                 genfn = fns[size][u];
11189                 break;
11190             }
11191             case 0x4: /* SHSUB, UHSUB */
11192             {
11193                 static NeonGenTwoOpFn * const fns[3][2] = {
11194                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11195                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11196                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11197                 };
11198                 genfn = fns[size][u];
11199                 break;
11200             }
11201             case 0x9: /* SQSHL, UQSHL */
11202             {
11203                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11204                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11205                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11206                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11207                 };
11208                 genenvfn = fns[size][u];
11209                 break;
11210             }
11211             case 0xa: /* SRSHL, URSHL */
11212             {
11213                 static NeonGenTwoOpFn * const fns[3][2] = {
11214                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11215                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11216                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11217                 };
11218                 genfn = fns[size][u];
11219                 break;
11220             }
11221             case 0xb: /* SQRSHL, UQRSHL */
11222             {
11223                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11224                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11225                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11226                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11227                 };
11228                 genenvfn = fns[size][u];
11229                 break;
11230             }
11231             default:
11232                 g_assert_not_reached();
11233             }
11234 
11235             if (genenvfn) {
11236                 genenvfn(tcg_res, tcg_env, tcg_op1, tcg_op2);
11237             } else {
11238                 genfn(tcg_res, tcg_op1, tcg_op2);
11239             }
11240 
11241             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11242         }
11243     }
11244     clear_vec_high(s, is_q, rd);
11245 }
11246 
11247 /* AdvSIMD three same
11248  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
11249  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11250  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
11251  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11252  */
11253 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11254 {
11255     int opcode = extract32(insn, 11, 5);
11256 
11257     switch (opcode) {
11258     case 0x3: /* logic ops */
11259         disas_simd_3same_logic(s, insn);
11260         break;
11261     case 0x17: /* ADDP */
11262     case 0x14: /* SMAXP, UMAXP */
11263     case 0x15: /* SMINP, UMINP */
11264     {
11265         /* Pairwise operations */
11266         int is_q = extract32(insn, 30, 1);
11267         int u = extract32(insn, 29, 1);
11268         int size = extract32(insn, 22, 2);
11269         int rm = extract32(insn, 16, 5);
11270         int rn = extract32(insn, 5, 5);
11271         int rd = extract32(insn, 0, 5);
11272         if (opcode == 0x17) {
11273             if (u || (size == 3 && !is_q)) {
11274                 unallocated_encoding(s);
11275                 return;
11276             }
11277         } else {
11278             if (size == 3) {
11279                 unallocated_encoding(s);
11280                 return;
11281             }
11282         }
11283         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11284         break;
11285     }
11286     case 0x18 ... 0x31:
11287         /* floating point ops, sz[1] and U are part of opcode */
11288         disas_simd_3same_float(s, insn);
11289         break;
11290     default:
11291         disas_simd_3same_int(s, insn);
11292         break;
11293     }
11294 }
11295 
11296 /*
11297  * Advanced SIMD three same (ARMv8.2 FP16 variants)
11298  *
11299  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
11300  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11301  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
11302  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11303  *
11304  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11305  * (register), FACGE, FABD, FCMGT (register) and FACGT.
11306  *
11307  */
11308 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11309 {
11310     int opcode = extract32(insn, 11, 3);
11311     int u = extract32(insn, 29, 1);
11312     int a = extract32(insn, 23, 1);
11313     int is_q = extract32(insn, 30, 1);
11314     int rm = extract32(insn, 16, 5);
11315     int rn = extract32(insn, 5, 5);
11316     int rd = extract32(insn, 0, 5);
11317     /*
11318      * For these floating point ops, the U, a and opcode bits
11319      * together indicate the operation.
11320      */
11321     int fpopcode = opcode | (a << 3) | (u << 4);
11322     int datasize = is_q ? 128 : 64;
11323     int elements = datasize / 16;
11324     bool pairwise;
11325     TCGv_ptr fpst;
11326     int pass;
11327 
11328     switch (fpopcode) {
11329     case 0x0: /* FMAXNM */
11330     case 0x1: /* FMLA */
11331     case 0x2: /* FADD */
11332     case 0x3: /* FMULX */
11333     case 0x4: /* FCMEQ */
11334     case 0x6: /* FMAX */
11335     case 0x7: /* FRECPS */
11336     case 0x8: /* FMINNM */
11337     case 0x9: /* FMLS */
11338     case 0xa: /* FSUB */
11339     case 0xe: /* FMIN */
11340     case 0xf: /* FRSQRTS */
11341     case 0x13: /* FMUL */
11342     case 0x14: /* FCMGE */
11343     case 0x15: /* FACGE */
11344     case 0x17: /* FDIV */
11345     case 0x1a: /* FABD */
11346     case 0x1c: /* FCMGT */
11347     case 0x1d: /* FACGT */
11348         pairwise = false;
11349         break;
11350     case 0x10: /* FMAXNMP */
11351     case 0x12: /* FADDP */
11352     case 0x16: /* FMAXP */
11353     case 0x18: /* FMINNMP */
11354     case 0x1e: /* FMINP */
11355         pairwise = true;
11356         break;
11357     default:
11358         unallocated_encoding(s);
11359         return;
11360     }
11361 
11362     if (!dc_isar_feature(aa64_fp16, s)) {
11363         unallocated_encoding(s);
11364         return;
11365     }
11366 
11367     if (!fp_access_check(s)) {
11368         return;
11369     }
11370 
11371     fpst = fpstatus_ptr(FPST_FPCR_F16);
11372 
11373     if (pairwise) {
11374         int maxpass = is_q ? 8 : 4;
11375         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11376         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11377         TCGv_i32 tcg_res[8];
11378 
11379         for (pass = 0; pass < maxpass; pass++) {
11380             int passreg = pass < (maxpass / 2) ? rn : rm;
11381             int passelt = (pass << 1) & (maxpass - 1);
11382 
11383             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11384             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11385             tcg_res[pass] = tcg_temp_new_i32();
11386 
11387             switch (fpopcode) {
11388             case 0x10: /* FMAXNMP */
11389                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11390                                            fpst);
11391                 break;
11392             case 0x12: /* FADDP */
11393                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11394                 break;
11395             case 0x16: /* FMAXP */
11396                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11397                 break;
11398             case 0x18: /* FMINNMP */
11399                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11400                                            fpst);
11401                 break;
11402             case 0x1e: /* FMINP */
11403                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11404                 break;
11405             default:
11406                 g_assert_not_reached();
11407             }
11408         }
11409 
11410         for (pass = 0; pass < maxpass; pass++) {
11411             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11412         }
11413     } else {
11414         for (pass = 0; pass < elements; pass++) {
11415             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11416             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11417             TCGv_i32 tcg_res = tcg_temp_new_i32();
11418 
11419             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11420             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11421 
11422             switch (fpopcode) {
11423             case 0x0: /* FMAXNM */
11424                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11425                 break;
11426             case 0x1: /* FMLA */
11427                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11428                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11429                                            fpst);
11430                 break;
11431             case 0x2: /* FADD */
11432                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11433                 break;
11434             case 0x3: /* FMULX */
11435                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11436                 break;
11437             case 0x4: /* FCMEQ */
11438                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11439                 break;
11440             case 0x6: /* FMAX */
11441                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11442                 break;
11443             case 0x7: /* FRECPS */
11444                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11445                 break;
11446             case 0x8: /* FMINNM */
11447                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11448                 break;
11449             case 0x9: /* FMLS */
11450                 /* As usual for ARM, separate negation for fused multiply-add */
11451                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11452                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11453                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11454                                            fpst);
11455                 break;
11456             case 0xa: /* FSUB */
11457                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11458                 break;
11459             case 0xe: /* FMIN */
11460                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11461                 break;
11462             case 0xf: /* FRSQRTS */
11463                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11464                 break;
11465             case 0x13: /* FMUL */
11466                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11467                 break;
11468             case 0x14: /* FCMGE */
11469                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11470                 break;
11471             case 0x15: /* FACGE */
11472                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11473                 break;
11474             case 0x17: /* FDIV */
11475                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11476                 break;
11477             case 0x1a: /* FABD */
11478                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11479                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11480                 break;
11481             case 0x1c: /* FCMGT */
11482                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11483                 break;
11484             case 0x1d: /* FACGT */
11485                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11486                 break;
11487             default:
11488                 g_assert_not_reached();
11489             }
11490 
11491             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11492         }
11493     }
11494 
11495     clear_vec_high(s, is_q, rd);
11496 }
11497 
11498 /* AdvSIMD three same extra
11499  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
11500  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11501  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
11502  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11503  */
11504 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11505 {
11506     int rd = extract32(insn, 0, 5);
11507     int rn = extract32(insn, 5, 5);
11508     int opcode = extract32(insn, 11, 4);
11509     int rm = extract32(insn, 16, 5);
11510     int size = extract32(insn, 22, 2);
11511     bool u = extract32(insn, 29, 1);
11512     bool is_q = extract32(insn, 30, 1);
11513     bool feature;
11514     int rot;
11515 
11516     switch (u * 16 + opcode) {
11517     case 0x10: /* SQRDMLAH (vector) */
11518     case 0x11: /* SQRDMLSH (vector) */
11519         if (size != 1 && size != 2) {
11520             unallocated_encoding(s);
11521             return;
11522         }
11523         feature = dc_isar_feature(aa64_rdm, s);
11524         break;
11525     case 0x02: /* SDOT (vector) */
11526     case 0x12: /* UDOT (vector) */
11527         if (size != MO_32) {
11528             unallocated_encoding(s);
11529             return;
11530         }
11531         feature = dc_isar_feature(aa64_dp, s);
11532         break;
11533     case 0x03: /* USDOT */
11534         if (size != MO_32) {
11535             unallocated_encoding(s);
11536             return;
11537         }
11538         feature = dc_isar_feature(aa64_i8mm, s);
11539         break;
11540     case 0x04: /* SMMLA */
11541     case 0x14: /* UMMLA */
11542     case 0x05: /* USMMLA */
11543         if (!is_q || size != MO_32) {
11544             unallocated_encoding(s);
11545             return;
11546         }
11547         feature = dc_isar_feature(aa64_i8mm, s);
11548         break;
11549     case 0x18: /* FCMLA, #0 */
11550     case 0x19: /* FCMLA, #90 */
11551     case 0x1a: /* FCMLA, #180 */
11552     case 0x1b: /* FCMLA, #270 */
11553     case 0x1c: /* FCADD, #90 */
11554     case 0x1e: /* FCADD, #270 */
11555         if (size == 0
11556             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11557             || (size == 3 && !is_q)) {
11558             unallocated_encoding(s);
11559             return;
11560         }
11561         feature = dc_isar_feature(aa64_fcma, s);
11562         break;
11563     case 0x1d: /* BFMMLA */
11564         if (size != MO_16 || !is_q) {
11565             unallocated_encoding(s);
11566             return;
11567         }
11568         feature = dc_isar_feature(aa64_bf16, s);
11569         break;
11570     case 0x1f:
11571         switch (size) {
11572         case 1: /* BFDOT */
11573         case 3: /* BFMLAL{B,T} */
11574             feature = dc_isar_feature(aa64_bf16, s);
11575             break;
11576         default:
11577             unallocated_encoding(s);
11578             return;
11579         }
11580         break;
11581     default:
11582         unallocated_encoding(s);
11583         return;
11584     }
11585     if (!feature) {
11586         unallocated_encoding(s);
11587         return;
11588     }
11589     if (!fp_access_check(s)) {
11590         return;
11591     }
11592 
11593     switch (opcode) {
11594     case 0x0: /* SQRDMLAH (vector) */
11595         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11596         return;
11597 
11598     case 0x1: /* SQRDMLSH (vector) */
11599         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11600         return;
11601 
11602     case 0x2: /* SDOT / UDOT */
11603         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11604                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11605         return;
11606 
11607     case 0x3: /* USDOT */
11608         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11609         return;
11610 
11611     case 0x04: /* SMMLA, UMMLA */
11612         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11613                          u ? gen_helper_gvec_ummla_b
11614                          : gen_helper_gvec_smmla_b);
11615         return;
11616     case 0x05: /* USMMLA */
11617         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11618         return;
11619 
11620     case 0x8: /* FCMLA, #0 */
11621     case 0x9: /* FCMLA, #90 */
11622     case 0xa: /* FCMLA, #180 */
11623     case 0xb: /* FCMLA, #270 */
11624         rot = extract32(opcode, 0, 2);
11625         switch (size) {
11626         case 1:
11627             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11628                               gen_helper_gvec_fcmlah);
11629             break;
11630         case 2:
11631             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11632                               gen_helper_gvec_fcmlas);
11633             break;
11634         case 3:
11635             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11636                               gen_helper_gvec_fcmlad);
11637             break;
11638         default:
11639             g_assert_not_reached();
11640         }
11641         return;
11642 
11643     case 0xc: /* FCADD, #90 */
11644     case 0xe: /* FCADD, #270 */
11645         rot = extract32(opcode, 1, 1);
11646         switch (size) {
11647         case 1:
11648             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11649                               gen_helper_gvec_fcaddh);
11650             break;
11651         case 2:
11652             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11653                               gen_helper_gvec_fcadds);
11654             break;
11655         case 3:
11656             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11657                               gen_helper_gvec_fcaddd);
11658             break;
11659         default:
11660             g_assert_not_reached();
11661         }
11662         return;
11663 
11664     case 0xd: /* BFMMLA */
11665         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11666         return;
11667     case 0xf:
11668         switch (size) {
11669         case 1: /* BFDOT */
11670             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11671             break;
11672         case 3: /* BFMLAL{B,T} */
11673             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11674                               gen_helper_gvec_bfmlal);
11675             break;
11676         default:
11677             g_assert_not_reached();
11678         }
11679         return;
11680 
11681     default:
11682         g_assert_not_reached();
11683     }
11684 }
11685 
11686 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11687                                   int size, int rn, int rd)
11688 {
11689     /* Handle 2-reg-misc ops which are widening (so each size element
11690      * in the source becomes a 2*size element in the destination.
11691      * The only instruction like this is FCVTL.
11692      */
11693     int pass;
11694 
11695     if (size == 3) {
11696         /* 32 -> 64 bit fp conversion */
11697         TCGv_i64 tcg_res[2];
11698         int srcelt = is_q ? 2 : 0;
11699 
11700         for (pass = 0; pass < 2; pass++) {
11701             TCGv_i32 tcg_op = tcg_temp_new_i32();
11702             tcg_res[pass] = tcg_temp_new_i64();
11703 
11704             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11705             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, tcg_env);
11706         }
11707         for (pass = 0; pass < 2; pass++) {
11708             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11709         }
11710     } else {
11711         /* 16 -> 32 bit fp conversion */
11712         int srcelt = is_q ? 4 : 0;
11713         TCGv_i32 tcg_res[4];
11714         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11715         TCGv_i32 ahp = get_ahp_flag();
11716 
11717         for (pass = 0; pass < 4; pass++) {
11718             tcg_res[pass] = tcg_temp_new_i32();
11719 
11720             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11721             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11722                                            fpst, ahp);
11723         }
11724         for (pass = 0; pass < 4; pass++) {
11725             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11726         }
11727     }
11728 }
11729 
11730 static void handle_rev(DisasContext *s, int opcode, bool u,
11731                        bool is_q, int size, int rn, int rd)
11732 {
11733     int op = (opcode << 1) | u;
11734     int opsz = op + size;
11735     int grp_size = 3 - opsz;
11736     int dsize = is_q ? 128 : 64;
11737     int i;
11738 
11739     if (opsz >= 3) {
11740         unallocated_encoding(s);
11741         return;
11742     }
11743 
11744     if (!fp_access_check(s)) {
11745         return;
11746     }
11747 
11748     if (size == 0) {
11749         /* Special case bytes, use bswap op on each group of elements */
11750         int groups = dsize / (8 << grp_size);
11751 
11752         for (i = 0; i < groups; i++) {
11753             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11754 
11755             read_vec_element(s, tcg_tmp, rn, i, grp_size);
11756             switch (grp_size) {
11757             case MO_16:
11758                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11759                 break;
11760             case MO_32:
11761                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11762                 break;
11763             case MO_64:
11764                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11765                 break;
11766             default:
11767                 g_assert_not_reached();
11768             }
11769             write_vec_element(s, tcg_tmp, rd, i, grp_size);
11770         }
11771         clear_vec_high(s, is_q, rd);
11772     } else {
11773         int revmask = (1 << grp_size) - 1;
11774         int esize = 8 << size;
11775         int elements = dsize / esize;
11776         TCGv_i64 tcg_rn = tcg_temp_new_i64();
11777         TCGv_i64 tcg_rd[2];
11778 
11779         for (i = 0; i < 2; i++) {
11780             tcg_rd[i] = tcg_temp_new_i64();
11781             tcg_gen_movi_i64(tcg_rd[i], 0);
11782         }
11783 
11784         for (i = 0; i < elements; i++) {
11785             int e_rev = (i & 0xf) ^ revmask;
11786             int w = (e_rev * esize) / 64;
11787             int o = (e_rev * esize) % 64;
11788 
11789             read_vec_element(s, tcg_rn, rn, i, size);
11790             tcg_gen_deposit_i64(tcg_rd[w], tcg_rd[w], tcg_rn, o, esize);
11791         }
11792 
11793         for (i = 0; i < 2; i++) {
11794             write_vec_element(s, tcg_rd[i], rd, i, MO_64);
11795         }
11796         clear_vec_high(s, true, rd);
11797     }
11798 }
11799 
11800 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
11801                                   bool is_q, int size, int rn, int rd)
11802 {
11803     /* Implement the pairwise operations from 2-misc:
11804      * SADDLP, UADDLP, SADALP, UADALP.
11805      * These all add pairs of elements in the input to produce a
11806      * double-width result element in the output (possibly accumulating).
11807      */
11808     bool accum = (opcode == 0x6);
11809     int maxpass = is_q ? 2 : 1;
11810     int pass;
11811     TCGv_i64 tcg_res[2];
11812 
11813     if (size == 2) {
11814         /* 32 + 32 -> 64 op */
11815         MemOp memop = size + (u ? 0 : MO_SIGN);
11816 
11817         for (pass = 0; pass < maxpass; pass++) {
11818             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11819             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11820 
11821             tcg_res[pass] = tcg_temp_new_i64();
11822 
11823             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
11824             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
11825             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11826             if (accum) {
11827                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
11828                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
11829             }
11830         }
11831     } else {
11832         for (pass = 0; pass < maxpass; pass++) {
11833             TCGv_i64 tcg_op = tcg_temp_new_i64();
11834             NeonGenOne64OpFn *genfn;
11835             static NeonGenOne64OpFn * const fns[2][2] = {
11836                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
11837                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
11838             };
11839 
11840             genfn = fns[size][u];
11841 
11842             tcg_res[pass] = tcg_temp_new_i64();
11843 
11844             read_vec_element(s, tcg_op, rn, pass, MO_64);
11845             genfn(tcg_res[pass], tcg_op);
11846 
11847             if (accum) {
11848                 read_vec_element(s, tcg_op, rd, pass, MO_64);
11849                 if (size == 0) {
11850                     gen_helper_neon_addl_u16(tcg_res[pass],
11851                                              tcg_res[pass], tcg_op);
11852                 } else {
11853                     gen_helper_neon_addl_u32(tcg_res[pass],
11854                                              tcg_res[pass], tcg_op);
11855                 }
11856             }
11857         }
11858     }
11859     if (!is_q) {
11860         tcg_res[1] = tcg_constant_i64(0);
11861     }
11862     for (pass = 0; pass < 2; pass++) {
11863         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11864     }
11865 }
11866 
11867 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
11868 {
11869     /* Implement SHLL and SHLL2 */
11870     int pass;
11871     int part = is_q ? 2 : 0;
11872     TCGv_i64 tcg_res[2];
11873 
11874     for (pass = 0; pass < 2; pass++) {
11875         static NeonGenWidenFn * const widenfns[3] = {
11876             gen_helper_neon_widen_u8,
11877             gen_helper_neon_widen_u16,
11878             tcg_gen_extu_i32_i64,
11879         };
11880         NeonGenWidenFn *widenfn = widenfns[size];
11881         TCGv_i32 tcg_op = tcg_temp_new_i32();
11882 
11883         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
11884         tcg_res[pass] = tcg_temp_new_i64();
11885         widenfn(tcg_res[pass], tcg_op);
11886         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
11887     }
11888 
11889     for (pass = 0; pass < 2; pass++) {
11890         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11891     }
11892 }
11893 
11894 /* AdvSIMD two reg misc
11895  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
11896  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11897  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
11898  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11899  */
11900 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
11901 {
11902     int size = extract32(insn, 22, 2);
11903     int opcode = extract32(insn, 12, 5);
11904     bool u = extract32(insn, 29, 1);
11905     bool is_q = extract32(insn, 30, 1);
11906     int rn = extract32(insn, 5, 5);
11907     int rd = extract32(insn, 0, 5);
11908     bool need_fpstatus = false;
11909     int rmode = -1;
11910     TCGv_i32 tcg_rmode;
11911     TCGv_ptr tcg_fpstatus;
11912 
11913     switch (opcode) {
11914     case 0x0: /* REV64, REV32 */
11915     case 0x1: /* REV16 */
11916         handle_rev(s, opcode, u, is_q, size, rn, rd);
11917         return;
11918     case 0x5: /* CNT, NOT, RBIT */
11919         if (u && size == 0) {
11920             /* NOT */
11921             break;
11922         } else if (u && size == 1) {
11923             /* RBIT */
11924             break;
11925         } else if (!u && size == 0) {
11926             /* CNT */
11927             break;
11928         }
11929         unallocated_encoding(s);
11930         return;
11931     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
11932     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
11933         if (size == 3) {
11934             unallocated_encoding(s);
11935             return;
11936         }
11937         if (!fp_access_check(s)) {
11938             return;
11939         }
11940 
11941         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
11942         return;
11943     case 0x4: /* CLS, CLZ */
11944         if (size == 3) {
11945             unallocated_encoding(s);
11946             return;
11947         }
11948         break;
11949     case 0x2: /* SADDLP, UADDLP */
11950     case 0x6: /* SADALP, UADALP */
11951         if (size == 3) {
11952             unallocated_encoding(s);
11953             return;
11954         }
11955         if (!fp_access_check(s)) {
11956             return;
11957         }
11958         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
11959         return;
11960     case 0x13: /* SHLL, SHLL2 */
11961         if (u == 0 || size == 3) {
11962             unallocated_encoding(s);
11963             return;
11964         }
11965         if (!fp_access_check(s)) {
11966             return;
11967         }
11968         handle_shll(s, is_q, size, rn, rd);
11969         return;
11970     case 0xa: /* CMLT */
11971         if (u == 1) {
11972             unallocated_encoding(s);
11973             return;
11974         }
11975         /* fall through */
11976     case 0x8: /* CMGT, CMGE */
11977     case 0x9: /* CMEQ, CMLE */
11978     case 0xb: /* ABS, NEG */
11979         if (size == 3 && !is_q) {
11980             unallocated_encoding(s);
11981             return;
11982         }
11983         break;
11984     case 0x3: /* SUQADD, USQADD */
11985         if (size == 3 && !is_q) {
11986             unallocated_encoding(s);
11987             return;
11988         }
11989         if (!fp_access_check(s)) {
11990             return;
11991         }
11992         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
11993         return;
11994     case 0x7: /* SQABS, SQNEG */
11995         if (size == 3 && !is_q) {
11996             unallocated_encoding(s);
11997             return;
11998         }
11999         break;
12000     case 0xc ... 0xf:
12001     case 0x16 ... 0x1f:
12002     {
12003         /* Floating point: U, size[1] and opcode indicate operation;
12004          * size[0] indicates single or double precision.
12005          */
12006         int is_double = extract32(size, 0, 1);
12007         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12008         size = is_double ? 3 : 2;
12009         switch (opcode) {
12010         case 0x2f: /* FABS */
12011         case 0x6f: /* FNEG */
12012             if (size == 3 && !is_q) {
12013                 unallocated_encoding(s);
12014                 return;
12015             }
12016             break;
12017         case 0x1d: /* SCVTF */
12018         case 0x5d: /* UCVTF */
12019         {
12020             bool is_signed = (opcode == 0x1d) ? true : false;
12021             int elements = is_double ? 2 : is_q ? 4 : 2;
12022             if (is_double && !is_q) {
12023                 unallocated_encoding(s);
12024                 return;
12025             }
12026             if (!fp_access_check(s)) {
12027                 return;
12028             }
12029             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12030             return;
12031         }
12032         case 0x2c: /* FCMGT (zero) */
12033         case 0x2d: /* FCMEQ (zero) */
12034         case 0x2e: /* FCMLT (zero) */
12035         case 0x6c: /* FCMGE (zero) */
12036         case 0x6d: /* FCMLE (zero) */
12037             if (size == 3 && !is_q) {
12038                 unallocated_encoding(s);
12039                 return;
12040             }
12041             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12042             return;
12043         case 0x7f: /* FSQRT */
12044             if (size == 3 && !is_q) {
12045                 unallocated_encoding(s);
12046                 return;
12047             }
12048             break;
12049         case 0x1a: /* FCVTNS */
12050         case 0x1b: /* FCVTMS */
12051         case 0x3a: /* FCVTPS */
12052         case 0x3b: /* FCVTZS */
12053         case 0x5a: /* FCVTNU */
12054         case 0x5b: /* FCVTMU */
12055         case 0x7a: /* FCVTPU */
12056         case 0x7b: /* FCVTZU */
12057             need_fpstatus = true;
12058             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12059             if (size == 3 && !is_q) {
12060                 unallocated_encoding(s);
12061                 return;
12062             }
12063             break;
12064         case 0x5c: /* FCVTAU */
12065         case 0x1c: /* FCVTAS */
12066             need_fpstatus = true;
12067             rmode = FPROUNDING_TIEAWAY;
12068             if (size == 3 && !is_q) {
12069                 unallocated_encoding(s);
12070                 return;
12071             }
12072             break;
12073         case 0x3c: /* URECPE */
12074             if (size == 3) {
12075                 unallocated_encoding(s);
12076                 return;
12077             }
12078             /* fall through */
12079         case 0x3d: /* FRECPE */
12080         case 0x7d: /* FRSQRTE */
12081             if (size == 3 && !is_q) {
12082                 unallocated_encoding(s);
12083                 return;
12084             }
12085             if (!fp_access_check(s)) {
12086                 return;
12087             }
12088             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12089             return;
12090         case 0x56: /* FCVTXN, FCVTXN2 */
12091             if (size == 2) {
12092                 unallocated_encoding(s);
12093                 return;
12094             }
12095             /* fall through */
12096         case 0x16: /* FCVTN, FCVTN2 */
12097             /* handle_2misc_narrow does a 2*size -> size operation, but these
12098              * instructions encode the source size rather than dest size.
12099              */
12100             if (!fp_access_check(s)) {
12101                 return;
12102             }
12103             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12104             return;
12105         case 0x36: /* BFCVTN, BFCVTN2 */
12106             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
12107                 unallocated_encoding(s);
12108                 return;
12109             }
12110             if (!fp_access_check(s)) {
12111                 return;
12112             }
12113             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12114             return;
12115         case 0x17: /* FCVTL, FCVTL2 */
12116             if (!fp_access_check(s)) {
12117                 return;
12118             }
12119             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12120             return;
12121         case 0x18: /* FRINTN */
12122         case 0x19: /* FRINTM */
12123         case 0x38: /* FRINTP */
12124         case 0x39: /* FRINTZ */
12125             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12126             /* fall through */
12127         case 0x59: /* FRINTX */
12128         case 0x79: /* FRINTI */
12129             need_fpstatus = true;
12130             if (size == 3 && !is_q) {
12131                 unallocated_encoding(s);
12132                 return;
12133             }
12134             break;
12135         case 0x58: /* FRINTA */
12136             rmode = FPROUNDING_TIEAWAY;
12137             need_fpstatus = true;
12138             if (size == 3 && !is_q) {
12139                 unallocated_encoding(s);
12140                 return;
12141             }
12142             break;
12143         case 0x7c: /* URSQRTE */
12144             if (size == 3) {
12145                 unallocated_encoding(s);
12146                 return;
12147             }
12148             break;
12149         case 0x1e: /* FRINT32Z */
12150         case 0x1f: /* FRINT64Z */
12151             rmode = FPROUNDING_ZERO;
12152             /* fall through */
12153         case 0x5e: /* FRINT32X */
12154         case 0x5f: /* FRINT64X */
12155             need_fpstatus = true;
12156             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12157                 unallocated_encoding(s);
12158                 return;
12159             }
12160             break;
12161         default:
12162             unallocated_encoding(s);
12163             return;
12164         }
12165         break;
12166     }
12167     default:
12168         unallocated_encoding(s);
12169         return;
12170     }
12171 
12172     if (!fp_access_check(s)) {
12173         return;
12174     }
12175 
12176     if (need_fpstatus || rmode >= 0) {
12177         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12178     } else {
12179         tcg_fpstatus = NULL;
12180     }
12181     if (rmode >= 0) {
12182         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12183     } else {
12184         tcg_rmode = NULL;
12185     }
12186 
12187     switch (opcode) {
12188     case 0x5:
12189         if (u && size == 0) { /* NOT */
12190             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12191             return;
12192         }
12193         break;
12194     case 0x8: /* CMGT, CMGE */
12195         if (u) {
12196             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12197         } else {
12198             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12199         }
12200         return;
12201     case 0x9: /* CMEQ, CMLE */
12202         if (u) {
12203             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12204         } else {
12205             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12206         }
12207         return;
12208     case 0xa: /* CMLT */
12209         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12210         return;
12211     case 0xb:
12212         if (u) { /* ABS, NEG */
12213             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12214         } else {
12215             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12216         }
12217         return;
12218     }
12219 
12220     if (size == 3) {
12221         /* All 64-bit element operations can be shared with scalar 2misc */
12222         int pass;
12223 
12224         /* Coverity claims (size == 3 && !is_q) has been eliminated
12225          * from all paths leading to here.
12226          */
12227         tcg_debug_assert(is_q);
12228         for (pass = 0; pass < 2; pass++) {
12229             TCGv_i64 tcg_op = tcg_temp_new_i64();
12230             TCGv_i64 tcg_res = tcg_temp_new_i64();
12231 
12232             read_vec_element(s, tcg_op, rn, pass, MO_64);
12233 
12234             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12235                             tcg_rmode, tcg_fpstatus);
12236 
12237             write_vec_element(s, tcg_res, rd, pass, MO_64);
12238         }
12239     } else {
12240         int pass;
12241 
12242         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12243             TCGv_i32 tcg_op = tcg_temp_new_i32();
12244             TCGv_i32 tcg_res = tcg_temp_new_i32();
12245 
12246             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12247 
12248             if (size == 2) {
12249                 /* Special cases for 32 bit elements */
12250                 switch (opcode) {
12251                 case 0x4: /* CLS */
12252                     if (u) {
12253                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12254                     } else {
12255                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
12256                     }
12257                     break;
12258                 case 0x7: /* SQABS, SQNEG */
12259                     if (u) {
12260                         gen_helper_neon_qneg_s32(tcg_res, tcg_env, tcg_op);
12261                     } else {
12262                         gen_helper_neon_qabs_s32(tcg_res, tcg_env, tcg_op);
12263                     }
12264                     break;
12265                 case 0x2f: /* FABS */
12266                     gen_helper_vfp_abss(tcg_res, tcg_op);
12267                     break;
12268                 case 0x6f: /* FNEG */
12269                     gen_helper_vfp_negs(tcg_res, tcg_op);
12270                     break;
12271                 case 0x7f: /* FSQRT */
12272                     gen_helper_vfp_sqrts(tcg_res, tcg_op, tcg_env);
12273                     break;
12274                 case 0x1a: /* FCVTNS */
12275                 case 0x1b: /* FCVTMS */
12276                 case 0x1c: /* FCVTAS */
12277                 case 0x3a: /* FCVTPS */
12278                 case 0x3b: /* FCVTZS */
12279                     gen_helper_vfp_tosls(tcg_res, tcg_op,
12280                                          tcg_constant_i32(0), tcg_fpstatus);
12281                     break;
12282                 case 0x5a: /* FCVTNU */
12283                 case 0x5b: /* FCVTMU */
12284                 case 0x5c: /* FCVTAU */
12285                 case 0x7a: /* FCVTPU */
12286                 case 0x7b: /* FCVTZU */
12287                     gen_helper_vfp_touls(tcg_res, tcg_op,
12288                                          tcg_constant_i32(0), tcg_fpstatus);
12289                     break;
12290                 case 0x18: /* FRINTN */
12291                 case 0x19: /* FRINTM */
12292                 case 0x38: /* FRINTP */
12293                 case 0x39: /* FRINTZ */
12294                 case 0x58: /* FRINTA */
12295                 case 0x79: /* FRINTI */
12296                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12297                     break;
12298                 case 0x59: /* FRINTX */
12299                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12300                     break;
12301                 case 0x7c: /* URSQRTE */
12302                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
12303                     break;
12304                 case 0x1e: /* FRINT32Z */
12305                 case 0x5e: /* FRINT32X */
12306                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12307                     break;
12308                 case 0x1f: /* FRINT64Z */
12309                 case 0x5f: /* FRINT64X */
12310                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12311                     break;
12312                 default:
12313                     g_assert_not_reached();
12314                 }
12315             } else {
12316                 /* Use helpers for 8 and 16 bit elements */
12317                 switch (opcode) {
12318                 case 0x5: /* CNT, RBIT */
12319                     /* For these two insns size is part of the opcode specifier
12320                      * (handled earlier); they always operate on byte elements.
12321                      */
12322                     if (u) {
12323                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12324                     } else {
12325                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12326                     }
12327                     break;
12328                 case 0x7: /* SQABS, SQNEG */
12329                 {
12330                     NeonGenOneOpEnvFn *genfn;
12331                     static NeonGenOneOpEnvFn * const fns[2][2] = {
12332                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12333                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12334                     };
12335                     genfn = fns[size][u];
12336                     genfn(tcg_res, tcg_env, tcg_op);
12337                     break;
12338                 }
12339                 case 0x4: /* CLS, CLZ */
12340                     if (u) {
12341                         if (size == 0) {
12342                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
12343                         } else {
12344                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
12345                         }
12346                     } else {
12347                         if (size == 0) {
12348                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
12349                         } else {
12350                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
12351                         }
12352                     }
12353                     break;
12354                 default:
12355                     g_assert_not_reached();
12356                 }
12357             }
12358 
12359             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12360         }
12361     }
12362     clear_vec_high(s, is_q, rd);
12363 
12364     if (tcg_rmode) {
12365         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12366     }
12367 }
12368 
12369 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12370  *
12371  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
12372  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12373  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12374  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12375  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12376  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12377  *
12378  * This actually covers two groups where scalar access is governed by
12379  * bit 28. A bunch of the instructions (float to integral) only exist
12380  * in the vector form and are un-allocated for the scalar decode. Also
12381  * in the scalar decode Q is always 1.
12382  */
12383 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12384 {
12385     int fpop, opcode, a, u;
12386     int rn, rd;
12387     bool is_q;
12388     bool is_scalar;
12389     bool only_in_vector = false;
12390 
12391     int pass;
12392     TCGv_i32 tcg_rmode = NULL;
12393     TCGv_ptr tcg_fpstatus = NULL;
12394     bool need_fpst = true;
12395     int rmode = -1;
12396 
12397     if (!dc_isar_feature(aa64_fp16, s)) {
12398         unallocated_encoding(s);
12399         return;
12400     }
12401 
12402     rd = extract32(insn, 0, 5);
12403     rn = extract32(insn, 5, 5);
12404 
12405     a = extract32(insn, 23, 1);
12406     u = extract32(insn, 29, 1);
12407     is_scalar = extract32(insn, 28, 1);
12408     is_q = extract32(insn, 30, 1);
12409 
12410     opcode = extract32(insn, 12, 5);
12411     fpop = deposit32(opcode, 5, 1, a);
12412     fpop = deposit32(fpop, 6, 1, u);
12413 
12414     switch (fpop) {
12415     case 0x1d: /* SCVTF */
12416     case 0x5d: /* UCVTF */
12417     {
12418         int elements;
12419 
12420         if (is_scalar) {
12421             elements = 1;
12422         } else {
12423             elements = (is_q ? 8 : 4);
12424         }
12425 
12426         if (!fp_access_check(s)) {
12427             return;
12428         }
12429         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12430         return;
12431     }
12432     break;
12433     case 0x2c: /* FCMGT (zero) */
12434     case 0x2d: /* FCMEQ (zero) */
12435     case 0x2e: /* FCMLT (zero) */
12436     case 0x6c: /* FCMGE (zero) */
12437     case 0x6d: /* FCMLE (zero) */
12438         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12439         return;
12440     case 0x3d: /* FRECPE */
12441     case 0x3f: /* FRECPX */
12442         break;
12443     case 0x18: /* FRINTN */
12444         only_in_vector = true;
12445         rmode = FPROUNDING_TIEEVEN;
12446         break;
12447     case 0x19: /* FRINTM */
12448         only_in_vector = true;
12449         rmode = FPROUNDING_NEGINF;
12450         break;
12451     case 0x38: /* FRINTP */
12452         only_in_vector = true;
12453         rmode = FPROUNDING_POSINF;
12454         break;
12455     case 0x39: /* FRINTZ */
12456         only_in_vector = true;
12457         rmode = FPROUNDING_ZERO;
12458         break;
12459     case 0x58: /* FRINTA */
12460         only_in_vector = true;
12461         rmode = FPROUNDING_TIEAWAY;
12462         break;
12463     case 0x59: /* FRINTX */
12464     case 0x79: /* FRINTI */
12465         only_in_vector = true;
12466         /* current rounding mode */
12467         break;
12468     case 0x1a: /* FCVTNS */
12469         rmode = FPROUNDING_TIEEVEN;
12470         break;
12471     case 0x1b: /* FCVTMS */
12472         rmode = FPROUNDING_NEGINF;
12473         break;
12474     case 0x1c: /* FCVTAS */
12475         rmode = FPROUNDING_TIEAWAY;
12476         break;
12477     case 0x3a: /* FCVTPS */
12478         rmode = FPROUNDING_POSINF;
12479         break;
12480     case 0x3b: /* FCVTZS */
12481         rmode = FPROUNDING_ZERO;
12482         break;
12483     case 0x5a: /* FCVTNU */
12484         rmode = FPROUNDING_TIEEVEN;
12485         break;
12486     case 0x5b: /* FCVTMU */
12487         rmode = FPROUNDING_NEGINF;
12488         break;
12489     case 0x5c: /* FCVTAU */
12490         rmode = FPROUNDING_TIEAWAY;
12491         break;
12492     case 0x7a: /* FCVTPU */
12493         rmode = FPROUNDING_POSINF;
12494         break;
12495     case 0x7b: /* FCVTZU */
12496         rmode = FPROUNDING_ZERO;
12497         break;
12498     case 0x2f: /* FABS */
12499     case 0x6f: /* FNEG */
12500         need_fpst = false;
12501         break;
12502     case 0x7d: /* FRSQRTE */
12503     case 0x7f: /* FSQRT (vector) */
12504         break;
12505     default:
12506         unallocated_encoding(s);
12507         return;
12508     }
12509 
12510 
12511     /* Check additional constraints for the scalar encoding */
12512     if (is_scalar) {
12513         if (!is_q) {
12514             unallocated_encoding(s);
12515             return;
12516         }
12517         /* FRINTxx is only in the vector form */
12518         if (only_in_vector) {
12519             unallocated_encoding(s);
12520             return;
12521         }
12522     }
12523 
12524     if (!fp_access_check(s)) {
12525         return;
12526     }
12527 
12528     if (rmode >= 0 || need_fpst) {
12529         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
12530     }
12531 
12532     if (rmode >= 0) {
12533         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12534     }
12535 
12536     if (is_scalar) {
12537         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12538         TCGv_i32 tcg_res = tcg_temp_new_i32();
12539 
12540         switch (fpop) {
12541         case 0x1a: /* FCVTNS */
12542         case 0x1b: /* FCVTMS */
12543         case 0x1c: /* FCVTAS */
12544         case 0x3a: /* FCVTPS */
12545         case 0x3b: /* FCVTZS */
12546             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12547             break;
12548         case 0x3d: /* FRECPE */
12549             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12550             break;
12551         case 0x3f: /* FRECPX */
12552             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12553             break;
12554         case 0x5a: /* FCVTNU */
12555         case 0x5b: /* FCVTMU */
12556         case 0x5c: /* FCVTAU */
12557         case 0x7a: /* FCVTPU */
12558         case 0x7b: /* FCVTZU */
12559             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12560             break;
12561         case 0x6f: /* FNEG */
12562             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12563             break;
12564         case 0x7d: /* FRSQRTE */
12565             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12566             break;
12567         default:
12568             g_assert_not_reached();
12569         }
12570 
12571         /* limit any sign extension going on */
12572         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12573         write_fp_sreg(s, rd, tcg_res);
12574     } else {
12575         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12576             TCGv_i32 tcg_op = tcg_temp_new_i32();
12577             TCGv_i32 tcg_res = tcg_temp_new_i32();
12578 
12579             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12580 
12581             switch (fpop) {
12582             case 0x1a: /* FCVTNS */
12583             case 0x1b: /* FCVTMS */
12584             case 0x1c: /* FCVTAS */
12585             case 0x3a: /* FCVTPS */
12586             case 0x3b: /* FCVTZS */
12587                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12588                 break;
12589             case 0x3d: /* FRECPE */
12590                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12591                 break;
12592             case 0x5a: /* FCVTNU */
12593             case 0x5b: /* FCVTMU */
12594             case 0x5c: /* FCVTAU */
12595             case 0x7a: /* FCVTPU */
12596             case 0x7b: /* FCVTZU */
12597                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12598                 break;
12599             case 0x18: /* FRINTN */
12600             case 0x19: /* FRINTM */
12601             case 0x38: /* FRINTP */
12602             case 0x39: /* FRINTZ */
12603             case 0x58: /* FRINTA */
12604             case 0x79: /* FRINTI */
12605                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12606                 break;
12607             case 0x59: /* FRINTX */
12608                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12609                 break;
12610             case 0x2f: /* FABS */
12611                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12612                 break;
12613             case 0x6f: /* FNEG */
12614                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12615                 break;
12616             case 0x7d: /* FRSQRTE */
12617                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12618                 break;
12619             case 0x7f: /* FSQRT */
12620                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12621                 break;
12622             default:
12623                 g_assert_not_reached();
12624             }
12625 
12626             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12627         }
12628 
12629         clear_vec_high(s, is_q, rd);
12630     }
12631 
12632     if (tcg_rmode) {
12633         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12634     }
12635 }
12636 
12637 /* AdvSIMD scalar x indexed element
12638  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12639  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12640  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12641  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12642  * AdvSIMD vector x indexed element
12643  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12644  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12645  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12646  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12647  */
12648 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12649 {
12650     /* This encoding has two kinds of instruction:
12651      *  normal, where we perform elt x idxelt => elt for each
12652      *     element in the vector
12653      *  long, where we perform elt x idxelt and generate a result of
12654      *     double the width of the input element
12655      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12656      */
12657     bool is_scalar = extract32(insn, 28, 1);
12658     bool is_q = extract32(insn, 30, 1);
12659     bool u = extract32(insn, 29, 1);
12660     int size = extract32(insn, 22, 2);
12661     int l = extract32(insn, 21, 1);
12662     int m = extract32(insn, 20, 1);
12663     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12664     int rm = extract32(insn, 16, 4);
12665     int opcode = extract32(insn, 12, 4);
12666     int h = extract32(insn, 11, 1);
12667     int rn = extract32(insn, 5, 5);
12668     int rd = extract32(insn, 0, 5);
12669     bool is_long = false;
12670     int is_fp = 0;
12671     bool is_fp16 = false;
12672     int index;
12673     TCGv_ptr fpst;
12674 
12675     switch (16 * u + opcode) {
12676     case 0x08: /* MUL */
12677     case 0x10: /* MLA */
12678     case 0x14: /* MLS */
12679         if (is_scalar) {
12680             unallocated_encoding(s);
12681             return;
12682         }
12683         break;
12684     case 0x02: /* SMLAL, SMLAL2 */
12685     case 0x12: /* UMLAL, UMLAL2 */
12686     case 0x06: /* SMLSL, SMLSL2 */
12687     case 0x16: /* UMLSL, UMLSL2 */
12688     case 0x0a: /* SMULL, SMULL2 */
12689     case 0x1a: /* UMULL, UMULL2 */
12690         if (is_scalar) {
12691             unallocated_encoding(s);
12692             return;
12693         }
12694         is_long = true;
12695         break;
12696     case 0x03: /* SQDMLAL, SQDMLAL2 */
12697     case 0x07: /* SQDMLSL, SQDMLSL2 */
12698     case 0x0b: /* SQDMULL, SQDMULL2 */
12699         is_long = true;
12700         break;
12701     case 0x0c: /* SQDMULH */
12702     case 0x0d: /* SQRDMULH */
12703         break;
12704     case 0x01: /* FMLA */
12705     case 0x05: /* FMLS */
12706     case 0x09: /* FMUL */
12707     case 0x19: /* FMULX */
12708         is_fp = 1;
12709         break;
12710     case 0x1d: /* SQRDMLAH */
12711     case 0x1f: /* SQRDMLSH */
12712         if (!dc_isar_feature(aa64_rdm, s)) {
12713             unallocated_encoding(s);
12714             return;
12715         }
12716         break;
12717     case 0x0e: /* SDOT */
12718     case 0x1e: /* UDOT */
12719         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12720             unallocated_encoding(s);
12721             return;
12722         }
12723         break;
12724     case 0x0f:
12725         switch (size) {
12726         case 0: /* SUDOT */
12727         case 2: /* USDOT */
12728             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12729                 unallocated_encoding(s);
12730                 return;
12731             }
12732             size = MO_32;
12733             break;
12734         case 1: /* BFDOT */
12735             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12736                 unallocated_encoding(s);
12737                 return;
12738             }
12739             size = MO_32;
12740             break;
12741         case 3: /* BFMLAL{B,T} */
12742             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12743                 unallocated_encoding(s);
12744                 return;
12745             }
12746             /* can't set is_fp without other incorrect size checks */
12747             size = MO_16;
12748             break;
12749         default:
12750             unallocated_encoding(s);
12751             return;
12752         }
12753         break;
12754     case 0x11: /* FCMLA #0 */
12755     case 0x13: /* FCMLA #90 */
12756     case 0x15: /* FCMLA #180 */
12757     case 0x17: /* FCMLA #270 */
12758         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
12759             unallocated_encoding(s);
12760             return;
12761         }
12762         is_fp = 2;
12763         break;
12764     case 0x00: /* FMLAL */
12765     case 0x04: /* FMLSL */
12766     case 0x18: /* FMLAL2 */
12767     case 0x1c: /* FMLSL2 */
12768         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
12769             unallocated_encoding(s);
12770             return;
12771         }
12772         size = MO_16;
12773         /* is_fp, but we pass tcg_env not fp_status.  */
12774         break;
12775     default:
12776         unallocated_encoding(s);
12777         return;
12778     }
12779 
12780     switch (is_fp) {
12781     case 1: /* normal fp */
12782         /* convert insn encoded size to MemOp size */
12783         switch (size) {
12784         case 0: /* half-precision */
12785             size = MO_16;
12786             is_fp16 = true;
12787             break;
12788         case MO_32: /* single precision */
12789         case MO_64: /* double precision */
12790             break;
12791         default:
12792             unallocated_encoding(s);
12793             return;
12794         }
12795         break;
12796 
12797     case 2: /* complex fp */
12798         /* Each indexable element is a complex pair.  */
12799         size += 1;
12800         switch (size) {
12801         case MO_32:
12802             if (h && !is_q) {
12803                 unallocated_encoding(s);
12804                 return;
12805             }
12806             is_fp16 = true;
12807             break;
12808         case MO_64:
12809             break;
12810         default:
12811             unallocated_encoding(s);
12812             return;
12813         }
12814         break;
12815 
12816     default: /* integer */
12817         switch (size) {
12818         case MO_8:
12819         case MO_64:
12820             unallocated_encoding(s);
12821             return;
12822         }
12823         break;
12824     }
12825     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
12826         unallocated_encoding(s);
12827         return;
12828     }
12829 
12830     /* Given MemOp size, adjust register and indexing.  */
12831     switch (size) {
12832     case MO_16:
12833         index = h << 2 | l << 1 | m;
12834         break;
12835     case MO_32:
12836         index = h << 1 | l;
12837         rm |= m << 4;
12838         break;
12839     case MO_64:
12840         if (l || !is_q) {
12841             unallocated_encoding(s);
12842             return;
12843         }
12844         index = h;
12845         rm |= m << 4;
12846         break;
12847     default:
12848         g_assert_not_reached();
12849     }
12850 
12851     if (!fp_access_check(s)) {
12852         return;
12853     }
12854 
12855     if (is_fp) {
12856         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
12857     } else {
12858         fpst = NULL;
12859     }
12860 
12861     switch (16 * u + opcode) {
12862     case 0x0e: /* SDOT */
12863     case 0x1e: /* UDOT */
12864         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12865                          u ? gen_helper_gvec_udot_idx_b
12866                          : gen_helper_gvec_sdot_idx_b);
12867         return;
12868     case 0x0f:
12869         switch (extract32(insn, 22, 2)) {
12870         case 0: /* SUDOT */
12871             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12872                              gen_helper_gvec_sudot_idx_b);
12873             return;
12874         case 1: /* BFDOT */
12875             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12876                              gen_helper_gvec_bfdot_idx);
12877             return;
12878         case 2: /* USDOT */
12879             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12880                              gen_helper_gvec_usdot_idx_b);
12881             return;
12882         case 3: /* BFMLAL{B,T} */
12883             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
12884                               gen_helper_gvec_bfmlal_idx);
12885             return;
12886         }
12887         g_assert_not_reached();
12888     case 0x11: /* FCMLA #0 */
12889     case 0x13: /* FCMLA #90 */
12890     case 0x15: /* FCMLA #180 */
12891     case 0x17: /* FCMLA #270 */
12892         {
12893             int rot = extract32(insn, 13, 2);
12894             int data = (index << 2) | rot;
12895             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
12896                                vec_full_reg_offset(s, rn),
12897                                vec_full_reg_offset(s, rm),
12898                                vec_full_reg_offset(s, rd), fpst,
12899                                is_q ? 16 : 8, vec_full_reg_size(s), data,
12900                                size == MO_64
12901                                ? gen_helper_gvec_fcmlas_idx
12902                                : gen_helper_gvec_fcmlah_idx);
12903         }
12904         return;
12905 
12906     case 0x00: /* FMLAL */
12907     case 0x04: /* FMLSL */
12908     case 0x18: /* FMLAL2 */
12909     case 0x1c: /* FMLSL2 */
12910         {
12911             int is_s = extract32(opcode, 2, 1);
12912             int is_2 = u;
12913             int data = (index << 2) | (is_2 << 1) | is_s;
12914             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
12915                                vec_full_reg_offset(s, rn),
12916                                vec_full_reg_offset(s, rm), tcg_env,
12917                                is_q ? 16 : 8, vec_full_reg_size(s),
12918                                data, gen_helper_gvec_fmlal_idx_a64);
12919         }
12920         return;
12921 
12922     case 0x08: /* MUL */
12923         if (!is_long && !is_scalar) {
12924             static gen_helper_gvec_3 * const fns[3] = {
12925                 gen_helper_gvec_mul_idx_h,
12926                 gen_helper_gvec_mul_idx_s,
12927                 gen_helper_gvec_mul_idx_d,
12928             };
12929             tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
12930                                vec_full_reg_offset(s, rn),
12931                                vec_full_reg_offset(s, rm),
12932                                is_q ? 16 : 8, vec_full_reg_size(s),
12933                                index, fns[size - 1]);
12934             return;
12935         }
12936         break;
12937 
12938     case 0x10: /* MLA */
12939         if (!is_long && !is_scalar) {
12940             static gen_helper_gvec_4 * const fns[3] = {
12941                 gen_helper_gvec_mla_idx_h,
12942                 gen_helper_gvec_mla_idx_s,
12943                 gen_helper_gvec_mla_idx_d,
12944             };
12945             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
12946                                vec_full_reg_offset(s, rn),
12947                                vec_full_reg_offset(s, rm),
12948                                vec_full_reg_offset(s, rd),
12949                                is_q ? 16 : 8, vec_full_reg_size(s),
12950                                index, fns[size - 1]);
12951             return;
12952         }
12953         break;
12954 
12955     case 0x14: /* MLS */
12956         if (!is_long && !is_scalar) {
12957             static gen_helper_gvec_4 * const fns[3] = {
12958                 gen_helper_gvec_mls_idx_h,
12959                 gen_helper_gvec_mls_idx_s,
12960                 gen_helper_gvec_mls_idx_d,
12961             };
12962             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
12963                                vec_full_reg_offset(s, rn),
12964                                vec_full_reg_offset(s, rm),
12965                                vec_full_reg_offset(s, rd),
12966                                is_q ? 16 : 8, vec_full_reg_size(s),
12967                                index, fns[size - 1]);
12968             return;
12969         }
12970         break;
12971     }
12972 
12973     if (size == 3) {
12974         TCGv_i64 tcg_idx = tcg_temp_new_i64();
12975         int pass;
12976 
12977         assert(is_fp && is_q && !is_long);
12978 
12979         read_vec_element(s, tcg_idx, rm, index, MO_64);
12980 
12981         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
12982             TCGv_i64 tcg_op = tcg_temp_new_i64();
12983             TCGv_i64 tcg_res = tcg_temp_new_i64();
12984 
12985             read_vec_element(s, tcg_op, rn, pass, MO_64);
12986 
12987             switch (16 * u + opcode) {
12988             case 0x05: /* FMLS */
12989                 /* As usual for ARM, separate negation for fused multiply-add */
12990                 gen_helper_vfp_negd(tcg_op, tcg_op);
12991                 /* fall through */
12992             case 0x01: /* FMLA */
12993                 read_vec_element(s, tcg_res, rd, pass, MO_64);
12994                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
12995                 break;
12996             case 0x09: /* FMUL */
12997                 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
12998                 break;
12999             case 0x19: /* FMULX */
13000                 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13001                 break;
13002             default:
13003                 g_assert_not_reached();
13004             }
13005 
13006             write_vec_element(s, tcg_res, rd, pass, MO_64);
13007         }
13008 
13009         clear_vec_high(s, !is_scalar, rd);
13010     } else if (!is_long) {
13011         /* 32 bit floating point, or 16 or 32 bit integer.
13012          * For the 16 bit scalar case we use the usual Neon helpers and
13013          * rely on the fact that 0 op 0 == 0 with no side effects.
13014          */
13015         TCGv_i32 tcg_idx = tcg_temp_new_i32();
13016         int pass, maxpasses;
13017 
13018         if (is_scalar) {
13019             maxpasses = 1;
13020         } else {
13021             maxpasses = is_q ? 4 : 2;
13022         }
13023 
13024         read_vec_element_i32(s, tcg_idx, rm, index, size);
13025 
13026         if (size == 1 && !is_scalar) {
13027             /* The simplest way to handle the 16x16 indexed ops is to duplicate
13028              * the index into both halves of the 32 bit tcg_idx and then use
13029              * the usual Neon helpers.
13030              */
13031             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13032         }
13033 
13034         for (pass = 0; pass < maxpasses; pass++) {
13035             TCGv_i32 tcg_op = tcg_temp_new_i32();
13036             TCGv_i32 tcg_res = tcg_temp_new_i32();
13037 
13038             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13039 
13040             switch (16 * u + opcode) {
13041             case 0x08: /* MUL */
13042             case 0x10: /* MLA */
13043             case 0x14: /* MLS */
13044             {
13045                 static NeonGenTwoOpFn * const fns[2][2] = {
13046                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13047                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
13048                 };
13049                 NeonGenTwoOpFn *genfn;
13050                 bool is_sub = opcode == 0x4;
13051 
13052                 if (size == 1) {
13053                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13054                 } else {
13055                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13056                 }
13057                 if (opcode == 0x8) {
13058                     break;
13059                 }
13060                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13061                 genfn = fns[size - 1][is_sub];
13062                 genfn(tcg_res, tcg_op, tcg_res);
13063                 break;
13064             }
13065             case 0x05: /* FMLS */
13066             case 0x01: /* FMLA */
13067                 read_vec_element_i32(s, tcg_res, rd, pass,
13068                                      is_scalar ? size : MO_32);
13069                 switch (size) {
13070                 case 1:
13071                     if (opcode == 0x5) {
13072                         /* As usual for ARM, separate negation for fused
13073                          * multiply-add */
13074                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13075                     }
13076                     if (is_scalar) {
13077                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13078                                                    tcg_res, fpst);
13079                     } else {
13080                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13081                                                     tcg_res, fpst);
13082                     }
13083                     break;
13084                 case 2:
13085                     if (opcode == 0x5) {
13086                         /* As usual for ARM, separate negation for
13087                          * fused multiply-add */
13088                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13089                     }
13090                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13091                                            tcg_res, fpst);
13092                     break;
13093                 default:
13094                     g_assert_not_reached();
13095                 }
13096                 break;
13097             case 0x09: /* FMUL */
13098                 switch (size) {
13099                 case 1:
13100                     if (is_scalar) {
13101                         gen_helper_advsimd_mulh(tcg_res, tcg_op,
13102                                                 tcg_idx, fpst);
13103                     } else {
13104                         gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13105                                                  tcg_idx, fpst);
13106                     }
13107                     break;
13108                 case 2:
13109                     gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13110                     break;
13111                 default:
13112                     g_assert_not_reached();
13113                 }
13114                 break;
13115             case 0x19: /* FMULX */
13116                 switch (size) {
13117                 case 1:
13118                     if (is_scalar) {
13119                         gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13120                                                  tcg_idx, fpst);
13121                     } else {
13122                         gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13123                                                   tcg_idx, fpst);
13124                     }
13125                     break;
13126                 case 2:
13127                     gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13128                     break;
13129                 default:
13130                     g_assert_not_reached();
13131                 }
13132                 break;
13133             case 0x0c: /* SQDMULH */
13134                 if (size == 1) {
13135                     gen_helper_neon_qdmulh_s16(tcg_res, tcg_env,
13136                                                tcg_op, tcg_idx);
13137                 } else {
13138                     gen_helper_neon_qdmulh_s32(tcg_res, tcg_env,
13139                                                tcg_op, tcg_idx);
13140                 }
13141                 break;
13142             case 0x0d: /* SQRDMULH */
13143                 if (size == 1) {
13144                     gen_helper_neon_qrdmulh_s16(tcg_res, tcg_env,
13145                                                 tcg_op, tcg_idx);
13146                 } else {
13147                     gen_helper_neon_qrdmulh_s32(tcg_res, tcg_env,
13148                                                 tcg_op, tcg_idx);
13149                 }
13150                 break;
13151             case 0x1d: /* SQRDMLAH */
13152                 read_vec_element_i32(s, tcg_res, rd, pass,
13153                                      is_scalar ? size : MO_32);
13154                 if (size == 1) {
13155                     gen_helper_neon_qrdmlah_s16(tcg_res, tcg_env,
13156                                                 tcg_op, tcg_idx, tcg_res);
13157                 } else {
13158                     gen_helper_neon_qrdmlah_s32(tcg_res, tcg_env,
13159                                                 tcg_op, tcg_idx, tcg_res);
13160                 }
13161                 break;
13162             case 0x1f: /* SQRDMLSH */
13163                 read_vec_element_i32(s, tcg_res, rd, pass,
13164                                      is_scalar ? size : MO_32);
13165                 if (size == 1) {
13166                     gen_helper_neon_qrdmlsh_s16(tcg_res, tcg_env,
13167                                                 tcg_op, tcg_idx, tcg_res);
13168                 } else {
13169                     gen_helper_neon_qrdmlsh_s32(tcg_res, tcg_env,
13170                                                 tcg_op, tcg_idx, tcg_res);
13171                 }
13172                 break;
13173             default:
13174                 g_assert_not_reached();
13175             }
13176 
13177             if (is_scalar) {
13178                 write_fp_sreg(s, rd, tcg_res);
13179             } else {
13180                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13181             }
13182         }
13183 
13184         clear_vec_high(s, is_q, rd);
13185     } else {
13186         /* long ops: 16x16->32 or 32x32->64 */
13187         TCGv_i64 tcg_res[2];
13188         int pass;
13189         bool satop = extract32(opcode, 0, 1);
13190         MemOp memop = MO_32;
13191 
13192         if (satop || !u) {
13193             memop |= MO_SIGN;
13194         }
13195 
13196         if (size == 2) {
13197             TCGv_i64 tcg_idx = tcg_temp_new_i64();
13198 
13199             read_vec_element(s, tcg_idx, rm, index, memop);
13200 
13201             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13202                 TCGv_i64 tcg_op = tcg_temp_new_i64();
13203                 TCGv_i64 tcg_passres;
13204                 int passelt;
13205 
13206                 if (is_scalar) {
13207                     passelt = 0;
13208                 } else {
13209                     passelt = pass + (is_q * 2);
13210                 }
13211 
13212                 read_vec_element(s, tcg_op, rn, passelt, memop);
13213 
13214                 tcg_res[pass] = tcg_temp_new_i64();
13215 
13216                 if (opcode == 0xa || opcode == 0xb) {
13217                     /* Non-accumulating ops */
13218                     tcg_passres = tcg_res[pass];
13219                 } else {
13220                     tcg_passres = tcg_temp_new_i64();
13221                 }
13222 
13223                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13224 
13225                 if (satop) {
13226                     /* saturating, doubling */
13227                     gen_helper_neon_addl_saturate_s64(tcg_passres, tcg_env,
13228                                                       tcg_passres, tcg_passres);
13229                 }
13230 
13231                 if (opcode == 0xa || opcode == 0xb) {
13232                     continue;
13233                 }
13234 
13235                 /* Accumulating op: handle accumulate step */
13236                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13237 
13238                 switch (opcode) {
13239                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13240                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13241                     break;
13242                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13243                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13244                     break;
13245                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13246                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
13247                     /* fall through */
13248                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13249                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], tcg_env,
13250                                                       tcg_res[pass],
13251                                                       tcg_passres);
13252                     break;
13253                 default:
13254                     g_assert_not_reached();
13255                 }
13256             }
13257 
13258             clear_vec_high(s, !is_scalar, rd);
13259         } else {
13260             TCGv_i32 tcg_idx = tcg_temp_new_i32();
13261 
13262             assert(size == 1);
13263             read_vec_element_i32(s, tcg_idx, rm, index, size);
13264 
13265             if (!is_scalar) {
13266                 /* The simplest way to handle the 16x16 indexed ops is to
13267                  * duplicate the index into both halves of the 32 bit tcg_idx
13268                  * and then use the usual Neon helpers.
13269                  */
13270                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13271             }
13272 
13273             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13274                 TCGv_i32 tcg_op = tcg_temp_new_i32();
13275                 TCGv_i64 tcg_passres;
13276 
13277                 if (is_scalar) {
13278                     read_vec_element_i32(s, tcg_op, rn, pass, size);
13279                 } else {
13280                     read_vec_element_i32(s, tcg_op, rn,
13281                                          pass + (is_q * 2), MO_32);
13282                 }
13283 
13284                 tcg_res[pass] = tcg_temp_new_i64();
13285 
13286                 if (opcode == 0xa || opcode == 0xb) {
13287                     /* Non-accumulating ops */
13288                     tcg_passres = tcg_res[pass];
13289                 } else {
13290                     tcg_passres = tcg_temp_new_i64();
13291                 }
13292 
13293                 if (memop & MO_SIGN) {
13294                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13295                 } else {
13296                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13297                 }
13298                 if (satop) {
13299                     gen_helper_neon_addl_saturate_s32(tcg_passres, tcg_env,
13300                                                       tcg_passres, tcg_passres);
13301                 }
13302 
13303                 if (opcode == 0xa || opcode == 0xb) {
13304                     continue;
13305                 }
13306 
13307                 /* Accumulating op: handle accumulate step */
13308                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13309 
13310                 switch (opcode) {
13311                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13312                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13313                                              tcg_passres);
13314                     break;
13315                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13316                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13317                                              tcg_passres);
13318                     break;
13319                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13320                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13321                     /* fall through */
13322                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13323                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], tcg_env,
13324                                                       tcg_res[pass],
13325                                                       tcg_passres);
13326                     break;
13327                 default:
13328                     g_assert_not_reached();
13329                 }
13330             }
13331 
13332             if (is_scalar) {
13333                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13334             }
13335         }
13336 
13337         if (is_scalar) {
13338             tcg_res[1] = tcg_constant_i64(0);
13339         }
13340 
13341         for (pass = 0; pass < 2; pass++) {
13342             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13343         }
13344     }
13345 }
13346 
13347 /* Crypto AES
13348  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13349  * +-----------------+------+-----------+--------+-----+------+------+
13350  * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13351  * +-----------------+------+-----------+--------+-----+------+------+
13352  */
13353 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13354 {
13355     int size = extract32(insn, 22, 2);
13356     int opcode = extract32(insn, 12, 5);
13357     int rn = extract32(insn, 5, 5);
13358     int rd = extract32(insn, 0, 5);
13359     gen_helper_gvec_2 *genfn2 = NULL;
13360     gen_helper_gvec_3 *genfn3 = NULL;
13361 
13362     if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13363         unallocated_encoding(s);
13364         return;
13365     }
13366 
13367     switch (opcode) {
13368     case 0x4: /* AESE */
13369         genfn3 = gen_helper_crypto_aese;
13370         break;
13371     case 0x6: /* AESMC */
13372         genfn2 = gen_helper_crypto_aesmc;
13373         break;
13374     case 0x5: /* AESD */
13375         genfn3 = gen_helper_crypto_aesd;
13376         break;
13377     case 0x7: /* AESIMC */
13378         genfn2 = gen_helper_crypto_aesimc;
13379         break;
13380     default:
13381         unallocated_encoding(s);
13382         return;
13383     }
13384 
13385     if (!fp_access_check(s)) {
13386         return;
13387     }
13388     if (genfn2) {
13389         gen_gvec_op2_ool(s, true, rd, rn, 0, genfn2);
13390     } else {
13391         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, genfn3);
13392     }
13393 }
13394 
13395 /* Crypto three-reg SHA
13396  *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
13397  * +-----------------+------+---+------+---+--------+-----+------+------+
13398  * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
13399  * +-----------------+------+---+------+---+--------+-----+------+------+
13400  */
13401 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13402 {
13403     int size = extract32(insn, 22, 2);
13404     int opcode = extract32(insn, 12, 3);
13405     int rm = extract32(insn, 16, 5);
13406     int rn = extract32(insn, 5, 5);
13407     int rd = extract32(insn, 0, 5);
13408     gen_helper_gvec_3 *genfn;
13409     bool feature;
13410 
13411     if (size != 0) {
13412         unallocated_encoding(s);
13413         return;
13414     }
13415 
13416     switch (opcode) {
13417     case 0: /* SHA1C */
13418         genfn = gen_helper_crypto_sha1c;
13419         feature = dc_isar_feature(aa64_sha1, s);
13420         break;
13421     case 1: /* SHA1P */
13422         genfn = gen_helper_crypto_sha1p;
13423         feature = dc_isar_feature(aa64_sha1, s);
13424         break;
13425     case 2: /* SHA1M */
13426         genfn = gen_helper_crypto_sha1m;
13427         feature = dc_isar_feature(aa64_sha1, s);
13428         break;
13429     case 3: /* SHA1SU0 */
13430         genfn = gen_helper_crypto_sha1su0;
13431         feature = dc_isar_feature(aa64_sha1, s);
13432         break;
13433     case 4: /* SHA256H */
13434         genfn = gen_helper_crypto_sha256h;
13435         feature = dc_isar_feature(aa64_sha256, s);
13436         break;
13437     case 5: /* SHA256H2 */
13438         genfn = gen_helper_crypto_sha256h2;
13439         feature = dc_isar_feature(aa64_sha256, s);
13440         break;
13441     case 6: /* SHA256SU1 */
13442         genfn = gen_helper_crypto_sha256su1;
13443         feature = dc_isar_feature(aa64_sha256, s);
13444         break;
13445     default:
13446         unallocated_encoding(s);
13447         return;
13448     }
13449 
13450     if (!feature) {
13451         unallocated_encoding(s);
13452         return;
13453     }
13454 
13455     if (!fp_access_check(s)) {
13456         return;
13457     }
13458     gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
13459 }
13460 
13461 /* Crypto two-reg SHA
13462  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13463  * +-----------------+------+-----------+--------+-----+------+------+
13464  * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13465  * +-----------------+------+-----------+--------+-----+------+------+
13466  */
13467 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13468 {
13469     int size = extract32(insn, 22, 2);
13470     int opcode = extract32(insn, 12, 5);
13471     int rn = extract32(insn, 5, 5);
13472     int rd = extract32(insn, 0, 5);
13473     gen_helper_gvec_2 *genfn;
13474     bool feature;
13475 
13476     if (size != 0) {
13477         unallocated_encoding(s);
13478         return;
13479     }
13480 
13481     switch (opcode) {
13482     case 0: /* SHA1H */
13483         feature = dc_isar_feature(aa64_sha1, s);
13484         genfn = gen_helper_crypto_sha1h;
13485         break;
13486     case 1: /* SHA1SU1 */
13487         feature = dc_isar_feature(aa64_sha1, s);
13488         genfn = gen_helper_crypto_sha1su1;
13489         break;
13490     case 2: /* SHA256SU0 */
13491         feature = dc_isar_feature(aa64_sha256, s);
13492         genfn = gen_helper_crypto_sha256su0;
13493         break;
13494     default:
13495         unallocated_encoding(s);
13496         return;
13497     }
13498 
13499     if (!feature) {
13500         unallocated_encoding(s);
13501         return;
13502     }
13503 
13504     if (!fp_access_check(s)) {
13505         return;
13506     }
13507     gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
13508 }
13509 
13510 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
13511 {
13512     tcg_gen_rotli_i64(d, m, 1);
13513     tcg_gen_xor_i64(d, d, n);
13514 }
13515 
13516 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
13517 {
13518     tcg_gen_rotli_vec(vece, d, m, 1);
13519     tcg_gen_xor_vec(vece, d, d, n);
13520 }
13521 
13522 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
13523                    uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
13524 {
13525     static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
13526     static const GVecGen3 op = {
13527         .fni8 = gen_rax1_i64,
13528         .fniv = gen_rax1_vec,
13529         .opt_opc = vecop_list,
13530         .fno = gen_helper_crypto_rax1,
13531         .vece = MO_64,
13532     };
13533     tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
13534 }
13535 
13536 /* Crypto three-reg SHA512
13537  *  31                   21 20  16 15  14  13 12  11  10  9    5 4    0
13538  * +-----------------------+------+---+---+-----+--------+------+------+
13539  * | 1 1 0 0 1 1 1 0 0 1 1 |  Rm  | 1 | O | 0 0 | opcode |  Rn  |  Rd  |
13540  * +-----------------------+------+---+---+-----+--------+------+------+
13541  */
13542 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13543 {
13544     int opcode = extract32(insn, 10, 2);
13545     int o =  extract32(insn, 14, 1);
13546     int rm = extract32(insn, 16, 5);
13547     int rn = extract32(insn, 5, 5);
13548     int rd = extract32(insn, 0, 5);
13549     bool feature;
13550     gen_helper_gvec_3 *oolfn = NULL;
13551     GVecGen3Fn *gvecfn = NULL;
13552 
13553     if (o == 0) {
13554         switch (opcode) {
13555         case 0: /* SHA512H */
13556             feature = dc_isar_feature(aa64_sha512, s);
13557             oolfn = gen_helper_crypto_sha512h;
13558             break;
13559         case 1: /* SHA512H2 */
13560             feature = dc_isar_feature(aa64_sha512, s);
13561             oolfn = gen_helper_crypto_sha512h2;
13562             break;
13563         case 2: /* SHA512SU1 */
13564             feature = dc_isar_feature(aa64_sha512, s);
13565             oolfn = gen_helper_crypto_sha512su1;
13566             break;
13567         case 3: /* RAX1 */
13568             feature = dc_isar_feature(aa64_sha3, s);
13569             gvecfn = gen_gvec_rax1;
13570             break;
13571         default:
13572             g_assert_not_reached();
13573         }
13574     } else {
13575         switch (opcode) {
13576         case 0: /* SM3PARTW1 */
13577             feature = dc_isar_feature(aa64_sm3, s);
13578             oolfn = gen_helper_crypto_sm3partw1;
13579             break;
13580         case 1: /* SM3PARTW2 */
13581             feature = dc_isar_feature(aa64_sm3, s);
13582             oolfn = gen_helper_crypto_sm3partw2;
13583             break;
13584         case 2: /* SM4EKEY */
13585             feature = dc_isar_feature(aa64_sm4, s);
13586             oolfn = gen_helper_crypto_sm4ekey;
13587             break;
13588         default:
13589             unallocated_encoding(s);
13590             return;
13591         }
13592     }
13593 
13594     if (!feature) {
13595         unallocated_encoding(s);
13596         return;
13597     }
13598 
13599     if (!fp_access_check(s)) {
13600         return;
13601     }
13602 
13603     if (oolfn) {
13604         gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
13605     } else {
13606         gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
13607     }
13608 }
13609 
13610 /* Crypto two-reg SHA512
13611  *  31                                     12  11  10  9    5 4    0
13612  * +-----------------------------------------+--------+------+------+
13613  * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode |  Rn  |  Rd  |
13614  * +-----------------------------------------+--------+------+------+
13615  */
13616 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13617 {
13618     int opcode = extract32(insn, 10, 2);
13619     int rn = extract32(insn, 5, 5);
13620     int rd = extract32(insn, 0, 5);
13621     bool feature;
13622 
13623     switch (opcode) {
13624     case 0: /* SHA512SU0 */
13625         feature = dc_isar_feature(aa64_sha512, s);
13626         break;
13627     case 1: /* SM4E */
13628         feature = dc_isar_feature(aa64_sm4, s);
13629         break;
13630     default:
13631         unallocated_encoding(s);
13632         return;
13633     }
13634 
13635     if (!feature) {
13636         unallocated_encoding(s);
13637         return;
13638     }
13639 
13640     if (!fp_access_check(s)) {
13641         return;
13642     }
13643 
13644     switch (opcode) {
13645     case 0: /* SHA512SU0 */
13646         gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
13647         break;
13648     case 1: /* SM4E */
13649         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
13650         break;
13651     default:
13652         g_assert_not_reached();
13653     }
13654 }
13655 
13656 /* Crypto four-register
13657  *  31               23 22 21 20  16 15  14  10 9    5 4    0
13658  * +-------------------+-----+------+---+------+------+------+
13659  * | 1 1 0 0 1 1 1 0 0 | Op0 |  Rm  | 0 |  Ra  |  Rn  |  Rd  |
13660  * +-------------------+-----+------+---+------+------+------+
13661  */
13662 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13663 {
13664     int op0 = extract32(insn, 21, 2);
13665     int rm = extract32(insn, 16, 5);
13666     int ra = extract32(insn, 10, 5);
13667     int rn = extract32(insn, 5, 5);
13668     int rd = extract32(insn, 0, 5);
13669     bool feature;
13670 
13671     switch (op0) {
13672     case 0: /* EOR3 */
13673     case 1: /* BCAX */
13674         feature = dc_isar_feature(aa64_sha3, s);
13675         break;
13676     case 2: /* SM3SS1 */
13677         feature = dc_isar_feature(aa64_sm3, s);
13678         break;
13679     default:
13680         unallocated_encoding(s);
13681         return;
13682     }
13683 
13684     if (!feature) {
13685         unallocated_encoding(s);
13686         return;
13687     }
13688 
13689     if (!fp_access_check(s)) {
13690         return;
13691     }
13692 
13693     if (op0 < 2) {
13694         TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13695         int pass;
13696 
13697         tcg_op1 = tcg_temp_new_i64();
13698         tcg_op2 = tcg_temp_new_i64();
13699         tcg_op3 = tcg_temp_new_i64();
13700         tcg_res[0] = tcg_temp_new_i64();
13701         tcg_res[1] = tcg_temp_new_i64();
13702 
13703         for (pass = 0; pass < 2; pass++) {
13704             read_vec_element(s, tcg_op1, rn, pass, MO_64);
13705             read_vec_element(s, tcg_op2, rm, pass, MO_64);
13706             read_vec_element(s, tcg_op3, ra, pass, MO_64);
13707 
13708             if (op0 == 0) {
13709                 /* EOR3 */
13710                 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13711             } else {
13712                 /* BCAX */
13713                 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13714             }
13715             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13716         }
13717         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13718         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13719     } else {
13720         TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13721 
13722         tcg_op1 = tcg_temp_new_i32();
13723         tcg_op2 = tcg_temp_new_i32();
13724         tcg_op3 = tcg_temp_new_i32();
13725         tcg_res = tcg_temp_new_i32();
13726         tcg_zero = tcg_constant_i32(0);
13727 
13728         read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13729         read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13730         read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13731 
13732         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13733         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13734         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13735         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13736 
13737         write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13738         write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13739         write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13740         write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13741     }
13742 }
13743 
13744 /* Crypto XAR
13745  *  31                   21 20  16 15    10 9    5 4    0
13746  * +-----------------------+------+--------+------+------+
13747  * | 1 1 0 0 1 1 1 0 1 0 0 |  Rm  |  imm6  |  Rn  |  Rd  |
13748  * +-----------------------+------+--------+------+------+
13749  */
13750 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
13751 {
13752     int rm = extract32(insn, 16, 5);
13753     int imm6 = extract32(insn, 10, 6);
13754     int rn = extract32(insn, 5, 5);
13755     int rd = extract32(insn, 0, 5);
13756 
13757     if (!dc_isar_feature(aa64_sha3, s)) {
13758         unallocated_encoding(s);
13759         return;
13760     }
13761 
13762     if (!fp_access_check(s)) {
13763         return;
13764     }
13765 
13766     gen_gvec_xar(MO_64, vec_full_reg_offset(s, rd),
13767                  vec_full_reg_offset(s, rn),
13768                  vec_full_reg_offset(s, rm), imm6, 16,
13769                  vec_full_reg_size(s));
13770 }
13771 
13772 /* Crypto three-reg imm2
13773  *  31                   21 20  16 15  14 13 12  11  10  9    5 4    0
13774  * +-----------------------+------+-----+------+--------+------+------+
13775  * | 1 1 0 0 1 1 1 0 0 1 0 |  Rm  | 1 0 | imm2 | opcode |  Rn  |  Rd  |
13776  * +-----------------------+------+-----+------+--------+------+------+
13777  */
13778 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
13779 {
13780     static gen_helper_gvec_3 * const fns[4] = {
13781         gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
13782         gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
13783     };
13784     int opcode = extract32(insn, 10, 2);
13785     int imm2 = extract32(insn, 12, 2);
13786     int rm = extract32(insn, 16, 5);
13787     int rn = extract32(insn, 5, 5);
13788     int rd = extract32(insn, 0, 5);
13789 
13790     if (!dc_isar_feature(aa64_sm3, s)) {
13791         unallocated_encoding(s);
13792         return;
13793     }
13794 
13795     if (!fp_access_check(s)) {
13796         return;
13797     }
13798 
13799     gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
13800 }
13801 
13802 /* C3.6 Data processing - SIMD, inc Crypto
13803  *
13804  * As the decode gets a little complex we are using a table based
13805  * approach for this part of the decode.
13806  */
13807 static const AArch64DecodeTable data_proc_simd[] = {
13808     /* pattern  ,  mask     ,  fn                        */
13809     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
13810     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
13811     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
13812     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
13813     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
13814     { 0x0e000400, 0x9fe08400, disas_simd_copy },
13815     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
13816     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
13817     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
13818     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
13819     { 0x0e000000, 0xbf208c00, disas_simd_tb },
13820     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
13821     { 0x2e000000, 0xbf208400, disas_simd_ext },
13822     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
13823     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
13824     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
13825     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
13826     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
13827     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
13828     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
13829     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
13830     { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
13831     { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
13832     { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
13833     { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
13834     { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
13835     { 0xce000000, 0xff808000, disas_crypto_four_reg },
13836     { 0xce800000, 0xffe00000, disas_crypto_xar },
13837     { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
13838     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
13839     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
13840     { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
13841     { 0x00000000, 0x00000000, NULL }
13842 };
13843 
13844 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
13845 {
13846     /* Note that this is called with all non-FP cases from
13847      * table C3-6 so it must UNDEF for entries not specifically
13848      * allocated to instructions in that table.
13849      */
13850     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
13851     if (fn) {
13852         fn(s, insn);
13853     } else {
13854         unallocated_encoding(s);
13855     }
13856 }
13857 
13858 /* C3.6 Data processing - SIMD and floating point */
13859 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
13860 {
13861     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
13862         disas_data_proc_fp(s, insn);
13863     } else {
13864         /* SIMD, including crypto */
13865         disas_data_proc_simd(s, insn);
13866     }
13867 }
13868 
13869 static bool trans_OK(DisasContext *s, arg_OK *a)
13870 {
13871     return true;
13872 }
13873 
13874 static bool trans_FAIL(DisasContext *s, arg_OK *a)
13875 {
13876     s->is_nonstreaming = true;
13877     return true;
13878 }
13879 
13880 /**
13881  * is_guarded_page:
13882  * @env: The cpu environment
13883  * @s: The DisasContext
13884  *
13885  * Return true if the page is guarded.
13886  */
13887 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
13888 {
13889     uint64_t addr = s->base.pc_first;
13890 #ifdef CONFIG_USER_ONLY
13891     return page_get_flags(addr) & PAGE_BTI;
13892 #else
13893     CPUTLBEntryFull *full;
13894     void *host;
13895     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
13896     int flags;
13897 
13898     /*
13899      * We test this immediately after reading an insn, which means
13900      * that the TLB entry must be present and valid, and thus this
13901      * access will never raise an exception.
13902      */
13903     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
13904                               false, &host, &full, 0);
13905     assert(!(flags & TLB_INVALID_MASK));
13906 
13907     return full->extra.arm.guarded;
13908 #endif
13909 }
13910 
13911 /**
13912  * btype_destination_ok:
13913  * @insn: The instruction at the branch destination
13914  * @bt: SCTLR_ELx.BT
13915  * @btype: PSTATE.BTYPE, and is non-zero
13916  *
13917  * On a guarded page, there are a limited number of insns
13918  * that may be present at the branch target:
13919  *   - branch target identifiers,
13920  *   - paciasp, pacibsp,
13921  *   - BRK insn
13922  *   - HLT insn
13923  * Anything else causes a Branch Target Exception.
13924  *
13925  * Return true if the branch is compatible, false to raise BTITRAP.
13926  */
13927 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
13928 {
13929     if ((insn & 0xfffff01fu) == 0xd503201fu) {
13930         /* HINT space */
13931         switch (extract32(insn, 5, 7)) {
13932         case 0b011001: /* PACIASP */
13933         case 0b011011: /* PACIBSP */
13934             /*
13935              * If SCTLR_ELx.BT, then PACI*SP are not compatible
13936              * with btype == 3.  Otherwise all btype are ok.
13937              */
13938             return !bt || btype != 3;
13939         case 0b100000: /* BTI */
13940             /* Not compatible with any btype.  */
13941             return false;
13942         case 0b100010: /* BTI c */
13943             /* Not compatible with btype == 3 */
13944             return btype != 3;
13945         case 0b100100: /* BTI j */
13946             /* Not compatible with btype == 2 */
13947             return btype != 2;
13948         case 0b100110: /* BTI jc */
13949             /* Compatible with any btype.  */
13950             return true;
13951         }
13952     } else {
13953         switch (insn & 0xffe0001fu) {
13954         case 0xd4200000u: /* BRK */
13955         case 0xd4400000u: /* HLT */
13956             /* Give priority to the breakpoint exception.  */
13957             return true;
13958         }
13959     }
13960     return false;
13961 }
13962 
13963 /* C3.1 A64 instruction index by encoding */
13964 static void disas_a64_legacy(DisasContext *s, uint32_t insn)
13965 {
13966     switch (extract32(insn, 25, 4)) {
13967     case 0x5:
13968     case 0xd:      /* Data processing - register */
13969         disas_data_proc_reg(s, insn);
13970         break;
13971     case 0x7:
13972     case 0xf:      /* Data processing - SIMD and floating point */
13973         disas_data_proc_simd_fp(s, insn);
13974         break;
13975     default:
13976         unallocated_encoding(s);
13977         break;
13978     }
13979 }
13980 
13981 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
13982                                           CPUState *cpu)
13983 {
13984     DisasContext *dc = container_of(dcbase, DisasContext, base);
13985     CPUARMState *env = cpu_env(cpu);
13986     ARMCPU *arm_cpu = env_archcpu(env);
13987     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
13988     int bound, core_mmu_idx;
13989 
13990     dc->isar = &arm_cpu->isar;
13991     dc->condjmp = 0;
13992     dc->pc_save = dc->base.pc_first;
13993     dc->aarch64 = true;
13994     dc->thumb = false;
13995     dc->sctlr_b = 0;
13996     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
13997     dc->condexec_mask = 0;
13998     dc->condexec_cond = 0;
13999     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
14000     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
14001     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
14002     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
14003     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
14004     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14005 #if !defined(CONFIG_USER_ONLY)
14006     dc->user = (dc->current_el == 0);
14007 #endif
14008     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
14009     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
14010     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
14011     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
14012     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
14013     dc->fgt_eret = EX_TBFLAG_A64(tb_flags, FGT_ERET);
14014     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
14015     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
14016     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
14017     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
14018     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
14019     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
14020     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
14021     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
14022     dc->ata[0] = EX_TBFLAG_A64(tb_flags, ATA);
14023     dc->ata[1] = EX_TBFLAG_A64(tb_flags, ATA0);
14024     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
14025     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
14026     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
14027     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
14028     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
14029     dc->naa = EX_TBFLAG_A64(tb_flags, NAA);
14030     dc->vec_len = 0;
14031     dc->vec_stride = 0;
14032     dc->cp_regs = arm_cpu->cp_regs;
14033     dc->features = env->features;
14034     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
14035     dc->gm_blocksize = arm_cpu->gm_blocksize;
14036 
14037 #ifdef CONFIG_USER_ONLY
14038     /* In sve_probe_page, we assume TBI is enabled. */
14039     tcg_debug_assert(dc->tbid & 1);
14040 #endif
14041 
14042     dc->lse2 = dc_isar_feature(aa64_lse2, dc);
14043 
14044     /* Single step state. The code-generation logic here is:
14045      *  SS_ACTIVE == 0:
14046      *   generate code with no special handling for single-stepping (except
14047      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14048      *   this happens anyway because those changes are all system register or
14049      *   PSTATE writes).
14050      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14051      *   emit code for one insn
14052      *   emit code to clear PSTATE.SS
14053      *   emit code to generate software step exception for completed step
14054      *   end TB (as usual for having generated an exception)
14055      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14056      *   emit code to generate a software step exception
14057      *   end the TB
14058      */
14059     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
14060     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
14061     dc->is_ldex = false;
14062 
14063     /* Bound the number of insns to execute to those left on the page.  */
14064     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14065 
14066     /* If architectural single step active, limit to 1.  */
14067     if (dc->ss_active) {
14068         bound = 1;
14069     }
14070     dc->base.max_insns = MIN(dc->base.max_insns, bound);
14071 }
14072 
14073 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14074 {
14075 }
14076 
14077 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14078 {
14079     DisasContext *dc = container_of(dcbase, DisasContext, base);
14080     target_ulong pc_arg = dc->base.pc_next;
14081 
14082     if (tb_cflags(dcbase->tb) & CF_PCREL) {
14083         pc_arg &= ~TARGET_PAGE_MASK;
14084     }
14085     tcg_gen_insn_start(pc_arg, 0, 0);
14086     dc->insn_start = tcg_last_op();
14087 }
14088 
14089 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14090 {
14091     DisasContext *s = container_of(dcbase, DisasContext, base);
14092     CPUARMState *env = cpu_env(cpu);
14093     uint64_t pc = s->base.pc_next;
14094     uint32_t insn;
14095 
14096     /* Singlestep exceptions have the highest priority. */
14097     if (s->ss_active && !s->pstate_ss) {
14098         /* Singlestep state is Active-pending.
14099          * If we're in this state at the start of a TB then either
14100          *  a) we just took an exception to an EL which is being debugged
14101          *     and this is the first insn in the exception handler
14102          *  b) debug exceptions were masked and we just unmasked them
14103          *     without changing EL (eg by clearing PSTATE.D)
14104          * In either case we're going to take a swstep exception in the
14105          * "did not step an insn" case, and so the syndrome ISV and EX
14106          * bits should be zero.
14107          */
14108         assert(s->base.num_insns == 1);
14109         gen_swstep_exception(s, 0, 0);
14110         s->base.is_jmp = DISAS_NORETURN;
14111         s->base.pc_next = pc + 4;
14112         return;
14113     }
14114 
14115     if (pc & 3) {
14116         /*
14117          * PC alignment fault.  This has priority over the instruction abort
14118          * that we would receive from a translation fault via arm_ldl_code.
14119          * This should only be possible after an indirect branch, at the
14120          * start of the TB.
14121          */
14122         assert(s->base.num_insns == 1);
14123         gen_helper_exception_pc_alignment(tcg_env, tcg_constant_tl(pc));
14124         s->base.is_jmp = DISAS_NORETURN;
14125         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
14126         return;
14127     }
14128 
14129     s->pc_curr = pc;
14130     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
14131     s->insn = insn;
14132     s->base.pc_next = pc + 4;
14133 
14134     s->fp_access_checked = false;
14135     s->sve_access_checked = false;
14136 
14137     if (s->pstate_il) {
14138         /*
14139          * Illegal execution state. This has priority over BTI
14140          * exceptions, but comes after instruction abort exceptions.
14141          */
14142         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
14143         return;
14144     }
14145 
14146     if (dc_isar_feature(aa64_bti, s)) {
14147         if (s->base.num_insns == 1) {
14148             /*
14149              * At the first insn of the TB, compute s->guarded_page.
14150              * We delayed computing this until successfully reading
14151              * the first insn of the TB, above.  This (mostly) ensures
14152              * that the softmmu tlb entry has been populated, and the
14153              * page table GP bit is available.
14154              *
14155              * Note that we need to compute this even if btype == 0,
14156              * because this value is used for BR instructions later
14157              * where ENV is not available.
14158              */
14159             s->guarded_page = is_guarded_page(env, s);
14160 
14161             /* First insn can have btype set to non-zero.  */
14162             tcg_debug_assert(s->btype >= 0);
14163 
14164             /*
14165              * Note that the Branch Target Exception has fairly high
14166              * priority -- below debugging exceptions but above most
14167              * everything else.  This allows us to handle this now
14168              * instead of waiting until the insn is otherwise decoded.
14169              */
14170             if (s->btype != 0
14171                 && s->guarded_page
14172                 && !btype_destination_ok(insn, s->bt, s->btype)) {
14173                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
14174                 return;
14175             }
14176         } else {
14177             /* Not the first insn: btype must be 0.  */
14178             tcg_debug_assert(s->btype == 0);
14179         }
14180     }
14181 
14182     s->is_nonstreaming = false;
14183     if (s->sme_trap_nonstreaming) {
14184         disas_sme_fa64(s, insn);
14185     }
14186 
14187     if (!disas_a64(s, insn) &&
14188         !disas_sme(s, insn) &&
14189         !disas_sve(s, insn)) {
14190         disas_a64_legacy(s, insn);
14191     }
14192 
14193     /*
14194      * After execution of most insns, btype is reset to 0.
14195      * Note that we set btype == -1 when the insn sets btype.
14196      */
14197     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14198         reset_btype(s);
14199     }
14200 }
14201 
14202 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14203 {
14204     DisasContext *dc = container_of(dcbase, DisasContext, base);
14205 
14206     if (unlikely(dc->ss_active)) {
14207         /* Note that this means single stepping WFI doesn't halt the CPU.
14208          * For conditional branch insns this is harmless unreachable code as
14209          * gen_goto_tb() has already handled emitting the debug exception
14210          * (and thus a tb-jump is not possible when singlestepping).
14211          */
14212         switch (dc->base.is_jmp) {
14213         default:
14214             gen_a64_update_pc(dc, 4);
14215             /* fall through */
14216         case DISAS_EXIT:
14217         case DISAS_JUMP:
14218             gen_step_complete_exception(dc);
14219             break;
14220         case DISAS_NORETURN:
14221             break;
14222         }
14223     } else {
14224         switch (dc->base.is_jmp) {
14225         case DISAS_NEXT:
14226         case DISAS_TOO_MANY:
14227             gen_goto_tb(dc, 1, 4);
14228             break;
14229         default:
14230         case DISAS_UPDATE_EXIT:
14231             gen_a64_update_pc(dc, 4);
14232             /* fall through */
14233         case DISAS_EXIT:
14234             tcg_gen_exit_tb(NULL, 0);
14235             break;
14236         case DISAS_UPDATE_NOCHAIN:
14237             gen_a64_update_pc(dc, 4);
14238             /* fall through */
14239         case DISAS_JUMP:
14240             tcg_gen_lookup_and_goto_ptr();
14241             break;
14242         case DISAS_NORETURN:
14243         case DISAS_SWI:
14244             break;
14245         case DISAS_WFE:
14246             gen_a64_update_pc(dc, 4);
14247             gen_helper_wfe(tcg_env);
14248             break;
14249         case DISAS_YIELD:
14250             gen_a64_update_pc(dc, 4);
14251             gen_helper_yield(tcg_env);
14252             break;
14253         case DISAS_WFI:
14254             /*
14255              * This is a special case because we don't want to just halt
14256              * the CPU if trying to debug across a WFI.
14257              */
14258             gen_a64_update_pc(dc, 4);
14259             gen_helper_wfi(tcg_env, tcg_constant_i32(4));
14260             /*
14261              * The helper doesn't necessarily throw an exception, but we
14262              * must go back to the main loop to check for interrupts anyway.
14263              */
14264             tcg_gen_exit_tb(NULL, 0);
14265             break;
14266         }
14267     }
14268 }
14269 
14270 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14271                                  CPUState *cpu, FILE *logfile)
14272 {
14273     DisasContext *dc = container_of(dcbase, DisasContext, base);
14274 
14275     fprintf(logfile, "IN: %s\n", lookup_symbol(dc->base.pc_first));
14276     target_disas(logfile, cpu, dc->base.pc_first, dc->base.tb->size);
14277 }
14278 
14279 const TranslatorOps aarch64_translator_ops = {
14280     .init_disas_context = aarch64_tr_init_disas_context,
14281     .tb_start           = aarch64_tr_tb_start,
14282     .insn_start         = aarch64_tr_insn_start,
14283     .translate_insn     = aarch64_tr_translate_insn,
14284     .tb_stop            = aarch64_tr_tb_stop,
14285     .disas_log          = aarch64_tr_disas_log,
14286 };
14287