xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision abaf3e5b)
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(cpu_env,
95                                     offsetof(CPUARMState, pc),
96                                     "pc");
97     for (i = 0; i < 32; i++) {
98         cpu_X[i] = tcg_global_mem_new_i64(cpu_env,
99                                           offsetof(CPUARMState, xregs[i]),
100                                           regnames[i]);
101     }
102 
103     cpu_exclusive_high = tcg_global_mem_new_i64(cpu_env,
104         offsetof(CPUARMState, exclusive_high), "exclusive_high");
105 }
106 
107 /*
108  * Return the core mmu_idx to use for A64 "unprivileged load/store" insns
109  */
110 static int get_a64_user_mem_index(DisasContext *s)
111 {
112     /*
113      * If AccType_UNPRIV is not used, the insn uses AccType_NORMAL,
114      * which is the usual mmu_idx for this cpu state.
115      */
116     ARMMMUIdx useridx = s->mmu_idx;
117 
118     if (s->unpriv) {
119         /*
120          * We have pre-computed the condition for AccType_UNPRIV.
121          * Therefore we should never get here with a mmu_idx for
122          * which we do not know the corresponding user mmu_idx.
123          */
124         switch (useridx) {
125         case ARMMMUIdx_E10_1:
126         case ARMMMUIdx_E10_1_PAN:
127             useridx = ARMMMUIdx_E10_0;
128             break;
129         case ARMMMUIdx_E20_2:
130         case ARMMMUIdx_E20_2_PAN:
131             useridx = ARMMMUIdx_E20_0;
132             break;
133         default:
134             g_assert_not_reached();
135         }
136     }
137     return arm_to_core_mmu_idx(useridx);
138 }
139 
140 static void set_btype_raw(int val)
141 {
142     tcg_gen_st_i32(tcg_constant_i32(val), cpu_env,
143                    offsetof(CPUARMState, btype));
144 }
145 
146 static void set_btype(DisasContext *s, int val)
147 {
148     /* BTYPE is a 2-bit field, and 0 should be done with reset_btype.  */
149     tcg_debug_assert(val >= 1 && val <= 3);
150     set_btype_raw(val);
151     s->btype = -1;
152 }
153 
154 static void reset_btype(DisasContext *s)
155 {
156     if (s->btype != 0) {
157         set_btype_raw(0);
158         s->btype = 0;
159     }
160 }
161 
162 static void gen_pc_plus_diff(DisasContext *s, TCGv_i64 dest, target_long diff)
163 {
164     assert(s->pc_save != -1);
165     if (tb_cflags(s->base.tb) & CF_PCREL) {
166         tcg_gen_addi_i64(dest, cpu_pc, (s->pc_curr - s->pc_save) + diff);
167     } else {
168         tcg_gen_movi_i64(dest, s->pc_curr + diff);
169     }
170 }
171 
172 void gen_a64_update_pc(DisasContext *s, target_long diff)
173 {
174     gen_pc_plus_diff(s, cpu_pc, diff);
175     s->pc_save = s->pc_curr + diff;
176 }
177 
178 /*
179  * Handle Top Byte Ignore (TBI) bits.
180  *
181  * If address tagging is enabled via the TCR TBI bits:
182  *  + for EL2 and EL3 there is only one TBI bit, and if it is set
183  *    then the address is zero-extended, clearing bits [63:56]
184  *  + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
185  *    and TBI1 controls addresses with bit 55 == 1.
186  *    If the appropriate TBI bit is set for the address then
187  *    the address is sign-extended from bit 55 into bits [63:56]
188  *
189  * Here We have concatenated TBI{1,0} into tbi.
190  */
191 static void gen_top_byte_ignore(DisasContext *s, TCGv_i64 dst,
192                                 TCGv_i64 src, int tbi)
193 {
194     if (tbi == 0) {
195         /* Load unmodified address */
196         tcg_gen_mov_i64(dst, src);
197     } else if (!regime_has_2_ranges(s->mmu_idx)) {
198         /* Force tag byte to all zero */
199         tcg_gen_extract_i64(dst, src, 0, 56);
200     } else {
201         /* Sign-extend from bit 55.  */
202         tcg_gen_sextract_i64(dst, src, 0, 56);
203 
204         switch (tbi) {
205         case 1:
206             /* tbi0 but !tbi1: only use the extension if positive */
207             tcg_gen_and_i64(dst, dst, src);
208             break;
209         case 2:
210             /* !tbi0 but tbi1: only use the extension if negative */
211             tcg_gen_or_i64(dst, dst, src);
212             break;
213         case 3:
214             /* tbi0 and tbi1: always use the extension */
215             break;
216         default:
217             g_assert_not_reached();
218         }
219     }
220 }
221 
222 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
223 {
224     /*
225      * If address tagging is enabled for instructions via the TCR TBI bits,
226      * then loading an address into the PC will clear out any tag.
227      */
228     gen_top_byte_ignore(s, cpu_pc, src, s->tbii);
229     s->pc_save = -1;
230 }
231 
232 /*
233  * Handle MTE and/or TBI.
234  *
235  * For TBI, ideally, we would do nothing.  Proper behaviour on fault is
236  * for the tag to be present in the FAR_ELx register.  But for user-only
237  * mode we do not have a TLB with which to implement this, so we must
238  * remove the top byte now.
239  *
240  * Always return a fresh temporary that we can increment independently
241  * of the write-back address.
242  */
243 
244 TCGv_i64 clean_data_tbi(DisasContext *s, TCGv_i64 addr)
245 {
246     TCGv_i64 clean = tcg_temp_new_i64();
247 #ifdef CONFIG_USER_ONLY
248     gen_top_byte_ignore(s, clean, addr, s->tbid);
249 #else
250     tcg_gen_mov_i64(clean, addr);
251 #endif
252     return clean;
253 }
254 
255 /* Insert a zero tag into src, with the result at dst. */
256 static void gen_address_with_allocation_tag0(TCGv_i64 dst, TCGv_i64 src)
257 {
258     tcg_gen_andi_i64(dst, src, ~MAKE_64BIT_MASK(56, 4));
259 }
260 
261 static void gen_probe_access(DisasContext *s, TCGv_i64 ptr,
262                              MMUAccessType acc, int log2_size)
263 {
264     gen_helper_probe_access(cpu_env, ptr,
265                             tcg_constant_i32(acc),
266                             tcg_constant_i32(get_mem_index(s)),
267                             tcg_constant_i32(1 << log2_size));
268 }
269 
270 /*
271  * For MTE, check a single logical or atomic access.  This probes a single
272  * address, the exact one specified.  The size and alignment of the access
273  * is not relevant to MTE, per se, but watchpoints do require the size,
274  * and we want to recognize those before making any other changes to state.
275  */
276 static TCGv_i64 gen_mte_check1_mmuidx(DisasContext *s, TCGv_i64 addr,
277                                       bool is_write, bool tag_checked,
278                                       MemOp memop, bool is_unpriv,
279                                       int core_idx)
280 {
281     if (tag_checked && s->mte_active[is_unpriv]) {
282         TCGv_i64 ret;
283         int desc = 0;
284 
285         desc = FIELD_DP32(desc, MTEDESC, MIDX, core_idx);
286         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
287         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
288         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
289         desc = FIELD_DP32(desc, MTEDESC, ALIGN, get_alignment_bits(memop));
290         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, memop_size(memop) - 1);
291 
292         ret = tcg_temp_new_i64();
293         gen_helper_mte_check(ret, cpu_env, tcg_constant_i32(desc), addr);
294 
295         return ret;
296     }
297     return clean_data_tbi(s, addr);
298 }
299 
300 TCGv_i64 gen_mte_check1(DisasContext *s, TCGv_i64 addr, bool is_write,
301                         bool tag_checked, MemOp memop)
302 {
303     return gen_mte_check1_mmuidx(s, addr, is_write, tag_checked, memop,
304                                  false, get_mem_index(s));
305 }
306 
307 /*
308  * For MTE, check multiple logical sequential accesses.
309  */
310 TCGv_i64 gen_mte_checkN(DisasContext *s, TCGv_i64 addr, bool is_write,
311                         bool tag_checked, int total_size, MemOp single_mop)
312 {
313     if (tag_checked && s->mte_active[0]) {
314         TCGv_i64 ret;
315         int desc = 0;
316 
317         desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
318         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
319         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
320         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
321         desc = FIELD_DP32(desc, MTEDESC, ALIGN, get_alignment_bits(single_mop));
322         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, total_size - 1);
323 
324         ret = tcg_temp_new_i64();
325         gen_helper_mte_check(ret, cpu_env, tcg_constant_i32(desc), addr);
326 
327         return ret;
328     }
329     return clean_data_tbi(s, addr);
330 }
331 
332 /*
333  * Generate the special alignment check that applies to AccType_ATOMIC
334  * and AccType_ORDERED insns under FEAT_LSE2: the access need not be
335  * naturally aligned, but it must not cross a 16-byte boundary.
336  * See AArch64.CheckAlignment().
337  */
338 static void check_lse2_align(DisasContext *s, int rn, int imm,
339                              bool is_write, MemOp mop)
340 {
341     TCGv_i32 tmp;
342     TCGv_i64 addr;
343     TCGLabel *over_label;
344     MMUAccessType type;
345     int mmu_idx;
346 
347     tmp = tcg_temp_new_i32();
348     tcg_gen_extrl_i64_i32(tmp, cpu_reg_sp(s, rn));
349     tcg_gen_addi_i32(tmp, tmp, imm & 15);
350     tcg_gen_andi_i32(tmp, tmp, 15);
351     tcg_gen_addi_i32(tmp, tmp, memop_size(mop));
352 
353     over_label = gen_new_label();
354     tcg_gen_brcondi_i32(TCG_COND_LEU, tmp, 16, over_label);
355 
356     addr = tcg_temp_new_i64();
357     tcg_gen_addi_i64(addr, cpu_reg_sp(s, rn), imm);
358 
359     type = is_write ? MMU_DATA_STORE : MMU_DATA_LOAD,
360     mmu_idx = get_mem_index(s);
361     gen_helper_unaligned_access(cpu_env, addr, tcg_constant_i32(type),
362                                 tcg_constant_i32(mmu_idx));
363 
364     gen_set_label(over_label);
365 
366 }
367 
368 /* Handle the alignment check for AccType_ATOMIC instructions. */
369 static MemOp check_atomic_align(DisasContext *s, int rn, MemOp mop)
370 {
371     MemOp size = mop & MO_SIZE;
372 
373     if (size == MO_8) {
374         return mop;
375     }
376 
377     /*
378      * If size == MO_128, this is a LDXP, and the operation is single-copy
379      * atomic for each doubleword, not the entire quadword; it still must
380      * be quadword aligned.
381      */
382     if (size == MO_128) {
383         return finalize_memop_atom(s, MO_128 | MO_ALIGN,
384                                    MO_ATOM_IFALIGN_PAIR);
385     }
386     if (dc_isar_feature(aa64_lse2, s)) {
387         check_lse2_align(s, rn, 0, true, mop);
388     } else {
389         mop |= MO_ALIGN;
390     }
391     return finalize_memop(s, mop);
392 }
393 
394 /* Handle the alignment check for AccType_ORDERED instructions. */
395 static MemOp check_ordered_align(DisasContext *s, int rn, int imm,
396                                  bool is_write, MemOp mop)
397 {
398     MemOp size = mop & MO_SIZE;
399 
400     if (size == MO_8) {
401         return mop;
402     }
403     if (size == MO_128) {
404         return finalize_memop_atom(s, MO_128 | MO_ALIGN,
405                                    MO_ATOM_IFALIGN_PAIR);
406     }
407     if (!dc_isar_feature(aa64_lse2, s)) {
408         mop |= MO_ALIGN;
409     } else if (!s->naa) {
410         check_lse2_align(s, rn, imm, is_write, mop);
411     }
412     return finalize_memop(s, mop);
413 }
414 
415 typedef struct DisasCompare64 {
416     TCGCond cond;
417     TCGv_i64 value;
418 } DisasCompare64;
419 
420 static void a64_test_cc(DisasCompare64 *c64, int cc)
421 {
422     DisasCompare c32;
423 
424     arm_test_cc(&c32, cc);
425 
426     /*
427      * Sign-extend the 32-bit value so that the GE/LT comparisons work
428      * properly.  The NE/EQ comparisons are also fine with this choice.
429       */
430     c64->cond = c32.cond;
431     c64->value = tcg_temp_new_i64();
432     tcg_gen_ext_i32_i64(c64->value, c32.value);
433 }
434 
435 static void gen_rebuild_hflags(DisasContext *s)
436 {
437     gen_helper_rebuild_hflags_a64(cpu_env, tcg_constant_i32(s->current_el));
438 }
439 
440 static void gen_exception_internal(int excp)
441 {
442     assert(excp_is_internal(excp));
443     gen_helper_exception_internal(cpu_env, tcg_constant_i32(excp));
444 }
445 
446 static void gen_exception_internal_insn(DisasContext *s, int excp)
447 {
448     gen_a64_update_pc(s, 0);
449     gen_exception_internal(excp);
450     s->base.is_jmp = DISAS_NORETURN;
451 }
452 
453 static void gen_exception_bkpt_insn(DisasContext *s, uint32_t syndrome)
454 {
455     gen_a64_update_pc(s, 0);
456     gen_helper_exception_bkpt_insn(cpu_env, tcg_constant_i32(syndrome));
457     s->base.is_jmp = DISAS_NORETURN;
458 }
459 
460 static void gen_step_complete_exception(DisasContext *s)
461 {
462     /* We just completed step of an insn. Move from Active-not-pending
463      * to Active-pending, and then also take the swstep exception.
464      * This corresponds to making the (IMPDEF) choice to prioritize
465      * swstep exceptions over asynchronous exceptions taken to an exception
466      * level where debug is disabled. This choice has the advantage that
467      * we do not need to maintain internal state corresponding to the
468      * ISV/EX syndrome bits between completion of the step and generation
469      * of the exception, and our syndrome information is always correct.
470      */
471     gen_ss_advance(s);
472     gen_swstep_exception(s, 1, s->is_ldex);
473     s->base.is_jmp = DISAS_NORETURN;
474 }
475 
476 static inline bool use_goto_tb(DisasContext *s, uint64_t dest)
477 {
478     if (s->ss_active) {
479         return false;
480     }
481     return translator_use_goto_tb(&s->base, dest);
482 }
483 
484 static void gen_goto_tb(DisasContext *s, int n, int64_t diff)
485 {
486     if (use_goto_tb(s, s->pc_curr + diff)) {
487         /*
488          * For pcrel, the pc must always be up-to-date on entry to
489          * the linked TB, so that it can use simple additions for all
490          * further adjustments.  For !pcrel, the linked TB is compiled
491          * to know its full virtual address, so we can delay the
492          * update to pc to the unlinked path.  A long chain of links
493          * can thus avoid many updates to the PC.
494          */
495         if (tb_cflags(s->base.tb) & CF_PCREL) {
496             gen_a64_update_pc(s, diff);
497             tcg_gen_goto_tb(n);
498         } else {
499             tcg_gen_goto_tb(n);
500             gen_a64_update_pc(s, diff);
501         }
502         tcg_gen_exit_tb(s->base.tb, n);
503         s->base.is_jmp = DISAS_NORETURN;
504     } else {
505         gen_a64_update_pc(s, diff);
506         if (s->ss_active) {
507             gen_step_complete_exception(s);
508         } else {
509             tcg_gen_lookup_and_goto_ptr();
510             s->base.is_jmp = DISAS_NORETURN;
511         }
512     }
513 }
514 
515 /*
516  * Register access functions
517  *
518  * These functions are used for directly accessing a register in where
519  * changes to the final register value are likely to be made. If you
520  * need to use a register for temporary calculation (e.g. index type
521  * operations) use the read_* form.
522  *
523  * B1.2.1 Register mappings
524  *
525  * In instruction register encoding 31 can refer to ZR (zero register) or
526  * the SP (stack pointer) depending on context. In QEMU's case we map SP
527  * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
528  * This is the point of the _sp forms.
529  */
530 TCGv_i64 cpu_reg(DisasContext *s, int reg)
531 {
532     if (reg == 31) {
533         TCGv_i64 t = tcg_temp_new_i64();
534         tcg_gen_movi_i64(t, 0);
535         return t;
536     } else {
537         return cpu_X[reg];
538     }
539 }
540 
541 /* register access for when 31 == SP */
542 TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
543 {
544     return cpu_X[reg];
545 }
546 
547 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
548  * representing the register contents. This TCGv is an auto-freed
549  * temporary so it need not be explicitly freed, and may be modified.
550  */
551 TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
552 {
553     TCGv_i64 v = tcg_temp_new_i64();
554     if (reg != 31) {
555         if (sf) {
556             tcg_gen_mov_i64(v, cpu_X[reg]);
557         } else {
558             tcg_gen_ext32u_i64(v, cpu_X[reg]);
559         }
560     } else {
561         tcg_gen_movi_i64(v, 0);
562     }
563     return v;
564 }
565 
566 TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
567 {
568     TCGv_i64 v = tcg_temp_new_i64();
569     if (sf) {
570         tcg_gen_mov_i64(v, cpu_X[reg]);
571     } else {
572         tcg_gen_ext32u_i64(v, cpu_X[reg]);
573     }
574     return v;
575 }
576 
577 /* Return the offset into CPUARMState of a slice (from
578  * the least significant end) of FP register Qn (ie
579  * Dn, Sn, Hn or Bn).
580  * (Note that this is not the same mapping as for A32; see cpu.h)
581  */
582 static inline int fp_reg_offset(DisasContext *s, int regno, MemOp size)
583 {
584     return vec_reg_offset(s, regno, 0, size);
585 }
586 
587 /* Offset of the high half of the 128 bit vector Qn */
588 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
589 {
590     return vec_reg_offset(s, regno, 1, MO_64);
591 }
592 
593 /* Convenience accessors for reading and writing single and double
594  * FP registers. Writing clears the upper parts of the associated
595  * 128 bit vector register, as required by the architecture.
596  * Note that unlike the GP register accessors, the values returned
597  * by the read functions must be manually freed.
598  */
599 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
600 {
601     TCGv_i64 v = tcg_temp_new_i64();
602 
603     tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
604     return v;
605 }
606 
607 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
608 {
609     TCGv_i32 v = tcg_temp_new_i32();
610 
611     tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
612     return v;
613 }
614 
615 static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
616 {
617     TCGv_i32 v = tcg_temp_new_i32();
618 
619     tcg_gen_ld16u_i32(v, cpu_env, fp_reg_offset(s, reg, MO_16));
620     return v;
621 }
622 
623 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
624  * If SVE is not enabled, then there are only 128 bits in the vector.
625  */
626 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
627 {
628     unsigned ofs = fp_reg_offset(s, rd, MO_64);
629     unsigned vsz = vec_full_reg_size(s);
630 
631     /* Nop move, with side effect of clearing the tail. */
632     tcg_gen_gvec_mov(MO_64, ofs, ofs, is_q ? 16 : 8, vsz);
633 }
634 
635 void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
636 {
637     unsigned ofs = fp_reg_offset(s, reg, MO_64);
638 
639     tcg_gen_st_i64(v, cpu_env, ofs);
640     clear_vec_high(s, false, reg);
641 }
642 
643 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
644 {
645     TCGv_i64 tmp = tcg_temp_new_i64();
646 
647     tcg_gen_extu_i32_i64(tmp, v);
648     write_fp_dreg(s, reg, tmp);
649 }
650 
651 /* Expand a 2-operand AdvSIMD vector operation using an expander function.  */
652 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
653                          GVecGen2Fn *gvec_fn, int vece)
654 {
655     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
656             is_q ? 16 : 8, vec_full_reg_size(s));
657 }
658 
659 /* Expand a 2-operand + immediate AdvSIMD vector operation using
660  * an expander function.
661  */
662 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
663                           int64_t imm, GVecGen2iFn *gvec_fn, int vece)
664 {
665     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
666             imm, is_q ? 16 : 8, vec_full_reg_size(s));
667 }
668 
669 /* Expand a 3-operand AdvSIMD vector operation using an expander function.  */
670 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
671                          GVecGen3Fn *gvec_fn, int vece)
672 {
673     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
674             vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
675 }
676 
677 /* Expand a 4-operand AdvSIMD vector operation using an expander function.  */
678 static void gen_gvec_fn4(DisasContext *s, bool is_q, int rd, int rn, int rm,
679                          int rx, GVecGen4Fn *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), vec_full_reg_offset(s, rx),
683             is_q ? 16 : 8, vec_full_reg_size(s));
684 }
685 
686 /* Expand a 2-operand operation using an out-of-line helper.  */
687 static void gen_gvec_op2_ool(DisasContext *s, bool is_q, int rd,
688                              int rn, int data, gen_helper_gvec_2 *fn)
689 {
690     tcg_gen_gvec_2_ool(vec_full_reg_offset(s, rd),
691                        vec_full_reg_offset(s, rn),
692                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
693 }
694 
695 /* Expand a 3-operand operation using an out-of-line helper.  */
696 static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
697                              int rn, int rm, int data, gen_helper_gvec_3 *fn)
698 {
699     tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
700                        vec_full_reg_offset(s, rn),
701                        vec_full_reg_offset(s, rm),
702                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
703 }
704 
705 /* Expand a 3-operand + fpstatus pointer + simd data value operation using
706  * an out-of-line helper.
707  */
708 static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
709                               int rm, bool is_fp16, int data,
710                               gen_helper_gvec_3_ptr *fn)
711 {
712     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
713     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
714                        vec_full_reg_offset(s, rn),
715                        vec_full_reg_offset(s, rm), fpst,
716                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
717 }
718 
719 /* Expand a 3-operand + qc + operation using an out-of-line helper.  */
720 static void gen_gvec_op3_qc(DisasContext *s, bool is_q, int rd, int rn,
721                             int rm, gen_helper_gvec_3_ptr *fn)
722 {
723     TCGv_ptr qc_ptr = tcg_temp_new_ptr();
724 
725     tcg_gen_addi_ptr(qc_ptr, cpu_env, offsetof(CPUARMState, vfp.qc));
726     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
727                        vec_full_reg_offset(s, rn),
728                        vec_full_reg_offset(s, rm), qc_ptr,
729                        is_q ? 16 : 8, vec_full_reg_size(s), 0, fn);
730 }
731 
732 /* Expand a 4-operand operation using an out-of-line helper.  */
733 static void gen_gvec_op4_ool(DisasContext *s, bool is_q, int rd, int rn,
734                              int rm, int ra, int data, gen_helper_gvec_4 *fn)
735 {
736     tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
737                        vec_full_reg_offset(s, rn),
738                        vec_full_reg_offset(s, rm),
739                        vec_full_reg_offset(s, ra),
740                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
741 }
742 
743 /*
744  * Expand a 4-operand + fpstatus pointer + simd data value operation using
745  * an out-of-line helper.
746  */
747 static void gen_gvec_op4_fpst(DisasContext *s, bool is_q, int rd, int rn,
748                               int rm, int ra, bool is_fp16, int data,
749                               gen_helper_gvec_4_ptr *fn)
750 {
751     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
752     tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
753                        vec_full_reg_offset(s, rn),
754                        vec_full_reg_offset(s, rm),
755                        vec_full_reg_offset(s, ra), fpst,
756                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
757 }
758 
759 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
760  * than the 32 bit equivalent.
761  */
762 static inline void gen_set_NZ64(TCGv_i64 result)
763 {
764     tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
765     tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
766 }
767 
768 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
769 static inline void gen_logic_CC(int sf, TCGv_i64 result)
770 {
771     if (sf) {
772         gen_set_NZ64(result);
773     } else {
774         tcg_gen_extrl_i64_i32(cpu_ZF, result);
775         tcg_gen_mov_i32(cpu_NF, cpu_ZF);
776     }
777     tcg_gen_movi_i32(cpu_CF, 0);
778     tcg_gen_movi_i32(cpu_VF, 0);
779 }
780 
781 /* dest = T0 + T1; compute C, N, V and Z flags */
782 static void gen_add64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
783 {
784     TCGv_i64 result, flag, tmp;
785     result = tcg_temp_new_i64();
786     flag = tcg_temp_new_i64();
787     tmp = tcg_temp_new_i64();
788 
789     tcg_gen_movi_i64(tmp, 0);
790     tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
791 
792     tcg_gen_extrl_i64_i32(cpu_CF, flag);
793 
794     gen_set_NZ64(result);
795 
796     tcg_gen_xor_i64(flag, result, t0);
797     tcg_gen_xor_i64(tmp, t0, t1);
798     tcg_gen_andc_i64(flag, flag, tmp);
799     tcg_gen_extrh_i64_i32(cpu_VF, flag);
800 
801     tcg_gen_mov_i64(dest, result);
802 }
803 
804 static void gen_add32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
805 {
806     TCGv_i32 t0_32 = tcg_temp_new_i32();
807     TCGv_i32 t1_32 = tcg_temp_new_i32();
808     TCGv_i32 tmp = tcg_temp_new_i32();
809 
810     tcg_gen_movi_i32(tmp, 0);
811     tcg_gen_extrl_i64_i32(t0_32, t0);
812     tcg_gen_extrl_i64_i32(t1_32, t1);
813     tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
814     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
815     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
816     tcg_gen_xor_i32(tmp, t0_32, t1_32);
817     tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
818     tcg_gen_extu_i32_i64(dest, cpu_NF);
819 }
820 
821 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
822 {
823     if (sf) {
824         gen_add64_CC(dest, t0, t1);
825     } else {
826         gen_add32_CC(dest, t0, t1);
827     }
828 }
829 
830 /* dest = T0 - T1; compute C, N, V and Z flags */
831 static void gen_sub64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
832 {
833     /* 64 bit arithmetic */
834     TCGv_i64 result, flag, tmp;
835 
836     result = tcg_temp_new_i64();
837     flag = tcg_temp_new_i64();
838     tcg_gen_sub_i64(result, t0, t1);
839 
840     gen_set_NZ64(result);
841 
842     tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
843     tcg_gen_extrl_i64_i32(cpu_CF, flag);
844 
845     tcg_gen_xor_i64(flag, result, t0);
846     tmp = tcg_temp_new_i64();
847     tcg_gen_xor_i64(tmp, t0, t1);
848     tcg_gen_and_i64(flag, flag, tmp);
849     tcg_gen_extrh_i64_i32(cpu_VF, flag);
850     tcg_gen_mov_i64(dest, result);
851 }
852 
853 static void gen_sub32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
854 {
855     /* 32 bit arithmetic */
856     TCGv_i32 t0_32 = tcg_temp_new_i32();
857     TCGv_i32 t1_32 = tcg_temp_new_i32();
858     TCGv_i32 tmp;
859 
860     tcg_gen_extrl_i64_i32(t0_32, t0);
861     tcg_gen_extrl_i64_i32(t1_32, t1);
862     tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
863     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
864     tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
865     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
866     tmp = tcg_temp_new_i32();
867     tcg_gen_xor_i32(tmp, t0_32, t1_32);
868     tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
869     tcg_gen_extu_i32_i64(dest, cpu_NF);
870 }
871 
872 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
873 {
874     if (sf) {
875         gen_sub64_CC(dest, t0, t1);
876     } else {
877         gen_sub32_CC(dest, t0, t1);
878     }
879 }
880 
881 /* dest = T0 + T1 + CF; do not compute flags. */
882 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
883 {
884     TCGv_i64 flag = tcg_temp_new_i64();
885     tcg_gen_extu_i32_i64(flag, cpu_CF);
886     tcg_gen_add_i64(dest, t0, t1);
887     tcg_gen_add_i64(dest, dest, flag);
888 
889     if (!sf) {
890         tcg_gen_ext32u_i64(dest, dest);
891     }
892 }
893 
894 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
895 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
896 {
897     if (sf) {
898         TCGv_i64 result = tcg_temp_new_i64();
899         TCGv_i64 cf_64 = tcg_temp_new_i64();
900         TCGv_i64 vf_64 = tcg_temp_new_i64();
901         TCGv_i64 tmp = tcg_temp_new_i64();
902         TCGv_i64 zero = tcg_constant_i64(0);
903 
904         tcg_gen_extu_i32_i64(cf_64, cpu_CF);
905         tcg_gen_add2_i64(result, cf_64, t0, zero, cf_64, zero);
906         tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, zero);
907         tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
908         gen_set_NZ64(result);
909 
910         tcg_gen_xor_i64(vf_64, result, t0);
911         tcg_gen_xor_i64(tmp, t0, t1);
912         tcg_gen_andc_i64(vf_64, vf_64, tmp);
913         tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
914 
915         tcg_gen_mov_i64(dest, result);
916     } else {
917         TCGv_i32 t0_32 = tcg_temp_new_i32();
918         TCGv_i32 t1_32 = tcg_temp_new_i32();
919         TCGv_i32 tmp = tcg_temp_new_i32();
920         TCGv_i32 zero = tcg_constant_i32(0);
921 
922         tcg_gen_extrl_i64_i32(t0_32, t0);
923         tcg_gen_extrl_i64_i32(t1_32, t1);
924         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, zero, cpu_CF, zero);
925         tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, zero);
926 
927         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
928         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
929         tcg_gen_xor_i32(tmp, t0_32, t1_32);
930         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
931         tcg_gen_extu_i32_i64(dest, cpu_NF);
932     }
933 }
934 
935 /*
936  * Load/Store generators
937  */
938 
939 /*
940  * Store from GPR register to memory.
941  */
942 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
943                              TCGv_i64 tcg_addr, MemOp memop, int memidx,
944                              bool iss_valid,
945                              unsigned int iss_srt,
946                              bool iss_sf, bool iss_ar)
947 {
948     tcg_gen_qemu_st_i64(source, tcg_addr, memidx, memop);
949 
950     if (iss_valid) {
951         uint32_t syn;
952 
953         syn = syn_data_abort_with_iss(0,
954                                       (memop & MO_SIZE),
955                                       false,
956                                       iss_srt,
957                                       iss_sf,
958                                       iss_ar,
959                                       0, 0, 0, 0, 0, false);
960         disas_set_insn_syndrome(s, syn);
961     }
962 }
963 
964 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
965                       TCGv_i64 tcg_addr, MemOp memop,
966                       bool iss_valid,
967                       unsigned int iss_srt,
968                       bool iss_sf, bool iss_ar)
969 {
970     do_gpr_st_memidx(s, source, tcg_addr, memop, get_mem_index(s),
971                      iss_valid, iss_srt, iss_sf, iss_ar);
972 }
973 
974 /*
975  * Load from memory to GPR register
976  */
977 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
978                              MemOp memop, bool extend, int memidx,
979                              bool iss_valid, unsigned int iss_srt,
980                              bool iss_sf, bool iss_ar)
981 {
982     tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
983 
984     if (extend && (memop & MO_SIGN)) {
985         g_assert((memop & MO_SIZE) <= MO_32);
986         tcg_gen_ext32u_i64(dest, dest);
987     }
988 
989     if (iss_valid) {
990         uint32_t syn;
991 
992         syn = syn_data_abort_with_iss(0,
993                                       (memop & MO_SIZE),
994                                       (memop & MO_SIGN) != 0,
995                                       iss_srt,
996                                       iss_sf,
997                                       iss_ar,
998                                       0, 0, 0, 0, 0, false);
999         disas_set_insn_syndrome(s, syn);
1000     }
1001 }
1002 
1003 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
1004                       MemOp memop, bool extend,
1005                       bool iss_valid, unsigned int iss_srt,
1006                       bool iss_sf, bool iss_ar)
1007 {
1008     do_gpr_ld_memidx(s, dest, tcg_addr, memop, extend, get_mem_index(s),
1009                      iss_valid, iss_srt, iss_sf, iss_ar);
1010 }
1011 
1012 /*
1013  * Store from FP register to memory
1014  */
1015 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, MemOp mop)
1016 {
1017     /* This writes the bottom N bits of a 128 bit wide vector to memory */
1018     TCGv_i64 tmplo = tcg_temp_new_i64();
1019 
1020     tcg_gen_ld_i64(tmplo, cpu_env, fp_reg_offset(s, srcidx, MO_64));
1021 
1022     if ((mop & MO_SIZE) < MO_128) {
1023         tcg_gen_qemu_st_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1024     } else {
1025         TCGv_i64 tmphi = tcg_temp_new_i64();
1026         TCGv_i128 t16 = tcg_temp_new_i128();
1027 
1028         tcg_gen_ld_i64(tmphi, cpu_env, fp_reg_hi_offset(s, srcidx));
1029         tcg_gen_concat_i64_i128(t16, tmplo, tmphi);
1030 
1031         tcg_gen_qemu_st_i128(t16, tcg_addr, get_mem_index(s), mop);
1032     }
1033 }
1034 
1035 /*
1036  * Load from memory to FP register
1037  */
1038 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, MemOp mop)
1039 {
1040     /* This always zero-extends and writes to a full 128 bit wide vector */
1041     TCGv_i64 tmplo = tcg_temp_new_i64();
1042     TCGv_i64 tmphi = NULL;
1043 
1044     if ((mop & MO_SIZE) < MO_128) {
1045         tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1046     } else {
1047         TCGv_i128 t16 = tcg_temp_new_i128();
1048 
1049         tcg_gen_qemu_ld_i128(t16, tcg_addr, get_mem_index(s), mop);
1050 
1051         tmphi = tcg_temp_new_i64();
1052         tcg_gen_extr_i128_i64(tmplo, tmphi, t16);
1053     }
1054 
1055     tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
1056 
1057     if (tmphi) {
1058         tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
1059     }
1060     clear_vec_high(s, tmphi != NULL, destidx);
1061 }
1062 
1063 /*
1064  * Vector load/store helpers.
1065  *
1066  * The principal difference between this and a FP load is that we don't
1067  * zero extend as we are filling a partial chunk of the vector register.
1068  * These functions don't support 128 bit loads/stores, which would be
1069  * normal load/store operations.
1070  *
1071  * The _i32 versions are useful when operating on 32 bit quantities
1072  * (eg for floating point single or using Neon helper functions).
1073  */
1074 
1075 /* Get value of an element within a vector register */
1076 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
1077                              int element, MemOp memop)
1078 {
1079     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1080     switch ((unsigned)memop) {
1081     case MO_8:
1082         tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
1083         break;
1084     case MO_16:
1085         tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
1086         break;
1087     case MO_32:
1088         tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
1089         break;
1090     case MO_8|MO_SIGN:
1091         tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
1092         break;
1093     case MO_16|MO_SIGN:
1094         tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
1095         break;
1096     case MO_32|MO_SIGN:
1097         tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
1098         break;
1099     case MO_64:
1100     case MO_64|MO_SIGN:
1101         tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
1102         break;
1103     default:
1104         g_assert_not_reached();
1105     }
1106 }
1107 
1108 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1109                                  int element, MemOp memop)
1110 {
1111     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1112     switch (memop) {
1113     case MO_8:
1114         tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1115         break;
1116     case MO_16:
1117         tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1118         break;
1119     case MO_8|MO_SIGN:
1120         tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1121         break;
1122     case MO_16|MO_SIGN:
1123         tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1124         break;
1125     case MO_32:
1126     case MO_32|MO_SIGN:
1127         tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1128         break;
1129     default:
1130         g_assert_not_reached();
1131     }
1132 }
1133 
1134 /* Set value of an element within a vector register */
1135 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1136                               int element, MemOp memop)
1137 {
1138     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1139     switch (memop) {
1140     case MO_8:
1141         tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1142         break;
1143     case MO_16:
1144         tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1145         break;
1146     case MO_32:
1147         tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1148         break;
1149     case MO_64:
1150         tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1151         break;
1152     default:
1153         g_assert_not_reached();
1154     }
1155 }
1156 
1157 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1158                                   int destidx, int element, MemOp memop)
1159 {
1160     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1161     switch (memop) {
1162     case MO_8:
1163         tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1164         break;
1165     case MO_16:
1166         tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1167         break;
1168     case MO_32:
1169         tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1170         break;
1171     default:
1172         g_assert_not_reached();
1173     }
1174 }
1175 
1176 /* Store from vector register to memory */
1177 static void do_vec_st(DisasContext *s, int srcidx, int element,
1178                       TCGv_i64 tcg_addr, MemOp mop)
1179 {
1180     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1181 
1182     read_vec_element(s, tcg_tmp, srcidx, element, mop & MO_SIZE);
1183     tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1184 }
1185 
1186 /* Load from memory to vector register */
1187 static void do_vec_ld(DisasContext *s, int destidx, int element,
1188                       TCGv_i64 tcg_addr, MemOp mop)
1189 {
1190     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1191 
1192     tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1193     write_vec_element(s, tcg_tmp, destidx, element, mop & MO_SIZE);
1194 }
1195 
1196 /* Check that FP/Neon access is enabled. If it is, return
1197  * true. If not, emit code to generate an appropriate exception,
1198  * and return false; the caller should not emit any code for
1199  * the instruction. Note that this check must happen after all
1200  * unallocated-encoding checks (otherwise the syndrome information
1201  * for the resulting exception will be incorrect).
1202  */
1203 static bool fp_access_check_only(DisasContext *s)
1204 {
1205     if (s->fp_excp_el) {
1206         assert(!s->fp_access_checked);
1207         s->fp_access_checked = true;
1208 
1209         gen_exception_insn_el(s, 0, EXCP_UDEF,
1210                               syn_fp_access_trap(1, 0xe, false, 0),
1211                               s->fp_excp_el);
1212         return false;
1213     }
1214     s->fp_access_checked = true;
1215     return true;
1216 }
1217 
1218 static bool fp_access_check(DisasContext *s)
1219 {
1220     if (!fp_access_check_only(s)) {
1221         return false;
1222     }
1223     if (s->sme_trap_nonstreaming && s->is_nonstreaming) {
1224         gen_exception_insn(s, 0, EXCP_UDEF,
1225                            syn_smetrap(SME_ET_Streaming, false));
1226         return false;
1227     }
1228     return true;
1229 }
1230 
1231 /*
1232  * Check that SVE access is enabled.  If it is, return true.
1233  * If not, emit code to generate an appropriate exception and return false.
1234  * This function corresponds to CheckSVEEnabled().
1235  */
1236 bool sve_access_check(DisasContext *s)
1237 {
1238     if (s->pstate_sm || !dc_isar_feature(aa64_sve, s)) {
1239         assert(dc_isar_feature(aa64_sme, s));
1240         if (!sme_sm_enabled_check(s)) {
1241             goto fail_exit;
1242         }
1243     } else if (s->sve_excp_el) {
1244         gen_exception_insn_el(s, 0, EXCP_UDEF,
1245                               syn_sve_access_trap(), s->sve_excp_el);
1246         goto fail_exit;
1247     }
1248     s->sve_access_checked = true;
1249     return fp_access_check(s);
1250 
1251  fail_exit:
1252     /* Assert that we only raise one exception per instruction. */
1253     assert(!s->sve_access_checked);
1254     s->sve_access_checked = true;
1255     return false;
1256 }
1257 
1258 /*
1259  * Check that SME access is enabled, raise an exception if not.
1260  * Note that this function corresponds to CheckSMEAccess and is
1261  * only used directly for cpregs.
1262  */
1263 static bool sme_access_check(DisasContext *s)
1264 {
1265     if (s->sme_excp_el) {
1266         gen_exception_insn_el(s, 0, EXCP_UDEF,
1267                               syn_smetrap(SME_ET_AccessTrap, false),
1268                               s->sme_excp_el);
1269         return false;
1270     }
1271     return true;
1272 }
1273 
1274 /* This function corresponds to CheckSMEEnabled. */
1275 bool sme_enabled_check(DisasContext *s)
1276 {
1277     /*
1278      * Note that unlike sve_excp_el, we have not constrained sme_excp_el
1279      * to be zero when fp_excp_el has priority.  This is because we need
1280      * sme_excp_el by itself for cpregs access checks.
1281      */
1282     if (!s->fp_excp_el || s->sme_excp_el < s->fp_excp_el) {
1283         s->fp_access_checked = true;
1284         return sme_access_check(s);
1285     }
1286     return fp_access_check_only(s);
1287 }
1288 
1289 /* Common subroutine for CheckSMEAnd*Enabled. */
1290 bool sme_enabled_check_with_svcr(DisasContext *s, unsigned req)
1291 {
1292     if (!sme_enabled_check(s)) {
1293         return false;
1294     }
1295     if (FIELD_EX64(req, SVCR, SM) && !s->pstate_sm) {
1296         gen_exception_insn(s, 0, EXCP_UDEF,
1297                            syn_smetrap(SME_ET_NotStreaming, false));
1298         return false;
1299     }
1300     if (FIELD_EX64(req, SVCR, ZA) && !s->pstate_za) {
1301         gen_exception_insn(s, 0, EXCP_UDEF,
1302                            syn_smetrap(SME_ET_InactiveZA, false));
1303         return false;
1304     }
1305     return true;
1306 }
1307 
1308 /*
1309  * This utility function is for doing register extension with an
1310  * optional shift. You will likely want to pass a temporary for the
1311  * destination register. See DecodeRegExtend() in the ARM ARM.
1312  */
1313 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1314                               int option, unsigned int shift)
1315 {
1316     int extsize = extract32(option, 0, 2);
1317     bool is_signed = extract32(option, 2, 1);
1318 
1319     if (is_signed) {
1320         switch (extsize) {
1321         case 0:
1322             tcg_gen_ext8s_i64(tcg_out, tcg_in);
1323             break;
1324         case 1:
1325             tcg_gen_ext16s_i64(tcg_out, tcg_in);
1326             break;
1327         case 2:
1328             tcg_gen_ext32s_i64(tcg_out, tcg_in);
1329             break;
1330         case 3:
1331             tcg_gen_mov_i64(tcg_out, tcg_in);
1332             break;
1333         }
1334     } else {
1335         switch (extsize) {
1336         case 0:
1337             tcg_gen_ext8u_i64(tcg_out, tcg_in);
1338             break;
1339         case 1:
1340             tcg_gen_ext16u_i64(tcg_out, tcg_in);
1341             break;
1342         case 2:
1343             tcg_gen_ext32u_i64(tcg_out, tcg_in);
1344             break;
1345         case 3:
1346             tcg_gen_mov_i64(tcg_out, tcg_in);
1347             break;
1348         }
1349     }
1350 
1351     if (shift) {
1352         tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1353     }
1354 }
1355 
1356 static inline void gen_check_sp_alignment(DisasContext *s)
1357 {
1358     /* The AArch64 architecture mandates that (if enabled via PSTATE
1359      * or SCTLR bits) there is a check that SP is 16-aligned on every
1360      * SP-relative load or store (with an exception generated if it is not).
1361      * In line with general QEMU practice regarding misaligned accesses,
1362      * we omit these checks for the sake of guest program performance.
1363      * This function is provided as a hook so we can more easily add these
1364      * checks in future (possibly as a "favour catching guest program bugs
1365      * over speed" user selectable option).
1366      */
1367 }
1368 
1369 /*
1370  * This provides a simple table based table lookup decoder. It is
1371  * intended to be used when the relevant bits for decode are too
1372  * awkwardly placed and switch/if based logic would be confusing and
1373  * deeply nested. Since it's a linear search through the table, tables
1374  * should be kept small.
1375  *
1376  * It returns the first handler where insn & mask == pattern, or
1377  * NULL if there is no match.
1378  * The table is terminated by an empty mask (i.e. 0)
1379  */
1380 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1381                                                uint32_t insn)
1382 {
1383     const AArch64DecodeTable *tptr = table;
1384 
1385     while (tptr->mask) {
1386         if ((insn & tptr->mask) == tptr->pattern) {
1387             return tptr->disas_fn;
1388         }
1389         tptr++;
1390     }
1391     return NULL;
1392 }
1393 
1394 /*
1395  * The instruction disassembly implemented here matches
1396  * the instruction encoding classifications in chapter C4
1397  * of the ARM Architecture Reference Manual (DDI0487B_a);
1398  * classification names and decode diagrams here should generally
1399  * match up with those in the manual.
1400  */
1401 
1402 static bool trans_B(DisasContext *s, arg_i *a)
1403 {
1404     reset_btype(s);
1405     gen_goto_tb(s, 0, a->imm);
1406     return true;
1407 }
1408 
1409 static bool trans_BL(DisasContext *s, arg_i *a)
1410 {
1411     gen_pc_plus_diff(s, cpu_reg(s, 30), curr_insn_len(s));
1412     reset_btype(s);
1413     gen_goto_tb(s, 0, a->imm);
1414     return true;
1415 }
1416 
1417 
1418 static bool trans_CBZ(DisasContext *s, arg_cbz *a)
1419 {
1420     DisasLabel match;
1421     TCGv_i64 tcg_cmp;
1422 
1423     tcg_cmp = read_cpu_reg(s, a->rt, a->sf);
1424     reset_btype(s);
1425 
1426     match = gen_disas_label(s);
1427     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1428                         tcg_cmp, 0, match.label);
1429     gen_goto_tb(s, 0, 4);
1430     set_disas_label(s, match);
1431     gen_goto_tb(s, 1, a->imm);
1432     return true;
1433 }
1434 
1435 static bool trans_TBZ(DisasContext *s, arg_tbz *a)
1436 {
1437     DisasLabel match;
1438     TCGv_i64 tcg_cmp;
1439 
1440     tcg_cmp = tcg_temp_new_i64();
1441     tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, a->rt), 1ULL << a->bitpos);
1442 
1443     reset_btype(s);
1444 
1445     match = gen_disas_label(s);
1446     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1447                         tcg_cmp, 0, match.label);
1448     gen_goto_tb(s, 0, 4);
1449     set_disas_label(s, match);
1450     gen_goto_tb(s, 1, a->imm);
1451     return true;
1452 }
1453 
1454 static bool trans_B_cond(DisasContext *s, arg_B_cond *a)
1455 {
1456     reset_btype(s);
1457     if (a->cond < 0x0e) {
1458         /* genuinely conditional branches */
1459         DisasLabel match = gen_disas_label(s);
1460         arm_gen_test_cc(a->cond, match.label);
1461         gen_goto_tb(s, 0, 4);
1462         set_disas_label(s, match);
1463         gen_goto_tb(s, 1, a->imm);
1464     } else {
1465         /* 0xe and 0xf are both "always" conditions */
1466         gen_goto_tb(s, 0, a->imm);
1467     }
1468     return true;
1469 }
1470 
1471 static void set_btype_for_br(DisasContext *s, int rn)
1472 {
1473     if (dc_isar_feature(aa64_bti, s)) {
1474         /* BR to {x16,x17} or !guard -> 1, else 3.  */
1475         set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
1476     }
1477 }
1478 
1479 static void set_btype_for_blr(DisasContext *s)
1480 {
1481     if (dc_isar_feature(aa64_bti, s)) {
1482         /* BLR sets BTYPE to 2, regardless of source guarded page.  */
1483         set_btype(s, 2);
1484     }
1485 }
1486 
1487 static bool trans_BR(DisasContext *s, arg_r *a)
1488 {
1489     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1490     set_btype_for_br(s, a->rn);
1491     s->base.is_jmp = DISAS_JUMP;
1492     return true;
1493 }
1494 
1495 static bool trans_BLR(DisasContext *s, arg_r *a)
1496 {
1497     TCGv_i64 dst = cpu_reg(s, a->rn);
1498     TCGv_i64 lr = cpu_reg(s, 30);
1499     if (dst == lr) {
1500         TCGv_i64 tmp = tcg_temp_new_i64();
1501         tcg_gen_mov_i64(tmp, dst);
1502         dst = tmp;
1503     }
1504     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1505     gen_a64_set_pc(s, dst);
1506     set_btype_for_blr(s);
1507     s->base.is_jmp = DISAS_JUMP;
1508     return true;
1509 }
1510 
1511 static bool trans_RET(DisasContext *s, arg_r *a)
1512 {
1513     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1514     s->base.is_jmp = DISAS_JUMP;
1515     return true;
1516 }
1517 
1518 static TCGv_i64 auth_branch_target(DisasContext *s, TCGv_i64 dst,
1519                                    TCGv_i64 modifier, bool use_key_a)
1520 {
1521     TCGv_i64 truedst;
1522     /*
1523      * Return the branch target for a BRAA/RETA/etc, which is either
1524      * just the destination dst, or that value with the pauth check
1525      * done and the code removed from the high bits.
1526      */
1527     if (!s->pauth_active) {
1528         return dst;
1529     }
1530 
1531     truedst = tcg_temp_new_i64();
1532     if (use_key_a) {
1533         gen_helper_autia(truedst, cpu_env, dst, modifier);
1534     } else {
1535         gen_helper_autib(truedst, cpu_env, dst, modifier);
1536     }
1537     return truedst;
1538 }
1539 
1540 static bool trans_BRAZ(DisasContext *s, arg_braz *a)
1541 {
1542     TCGv_i64 dst;
1543 
1544     if (!dc_isar_feature(aa64_pauth, s)) {
1545         return false;
1546     }
1547 
1548     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1549     gen_a64_set_pc(s, dst);
1550     set_btype_for_br(s, a->rn);
1551     s->base.is_jmp = DISAS_JUMP;
1552     return true;
1553 }
1554 
1555 static bool trans_BLRAZ(DisasContext *s, arg_braz *a)
1556 {
1557     TCGv_i64 dst, lr;
1558 
1559     if (!dc_isar_feature(aa64_pauth, s)) {
1560         return false;
1561     }
1562 
1563     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1564     lr = cpu_reg(s, 30);
1565     if (dst == lr) {
1566         TCGv_i64 tmp = tcg_temp_new_i64();
1567         tcg_gen_mov_i64(tmp, dst);
1568         dst = tmp;
1569     }
1570     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1571     gen_a64_set_pc(s, dst);
1572     set_btype_for_blr(s);
1573     s->base.is_jmp = DISAS_JUMP;
1574     return true;
1575 }
1576 
1577 static bool trans_RETA(DisasContext *s, arg_reta *a)
1578 {
1579     TCGv_i64 dst;
1580 
1581     dst = auth_branch_target(s, cpu_reg(s, 30), cpu_X[31], !a->m);
1582     gen_a64_set_pc(s, dst);
1583     s->base.is_jmp = DISAS_JUMP;
1584     return true;
1585 }
1586 
1587 static bool trans_BRA(DisasContext *s, arg_bra *a)
1588 {
1589     TCGv_i64 dst;
1590 
1591     if (!dc_isar_feature(aa64_pauth, s)) {
1592         return false;
1593     }
1594     dst = auth_branch_target(s, cpu_reg(s,a->rn), cpu_reg_sp(s, a->rm), !a->m);
1595     gen_a64_set_pc(s, dst);
1596     set_btype_for_br(s, a->rn);
1597     s->base.is_jmp = DISAS_JUMP;
1598     return true;
1599 }
1600 
1601 static bool trans_BLRA(DisasContext *s, arg_bra *a)
1602 {
1603     TCGv_i64 dst, lr;
1604 
1605     if (!dc_isar_feature(aa64_pauth, s)) {
1606         return false;
1607     }
1608     dst = auth_branch_target(s, cpu_reg(s, a->rn), cpu_reg_sp(s, a->rm), !a->m);
1609     lr = cpu_reg(s, 30);
1610     if (dst == lr) {
1611         TCGv_i64 tmp = tcg_temp_new_i64();
1612         tcg_gen_mov_i64(tmp, dst);
1613         dst = tmp;
1614     }
1615     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1616     gen_a64_set_pc(s, dst);
1617     set_btype_for_blr(s);
1618     s->base.is_jmp = DISAS_JUMP;
1619     return true;
1620 }
1621 
1622 static bool trans_ERET(DisasContext *s, arg_ERET *a)
1623 {
1624     TCGv_i64 dst;
1625 
1626     if (s->current_el == 0) {
1627         return false;
1628     }
1629     if (s->fgt_eret) {
1630         gen_exception_insn_el(s, 0, EXCP_UDEF, 0, 2);
1631         return true;
1632     }
1633     dst = tcg_temp_new_i64();
1634     tcg_gen_ld_i64(dst, cpu_env,
1635                    offsetof(CPUARMState, elr_el[s->current_el]));
1636 
1637     translator_io_start(&s->base);
1638 
1639     gen_helper_exception_return(cpu_env, dst);
1640     /* Must exit loop to check un-masked IRQs */
1641     s->base.is_jmp = DISAS_EXIT;
1642     return true;
1643 }
1644 
1645 static bool trans_ERETA(DisasContext *s, arg_reta *a)
1646 {
1647     TCGv_i64 dst;
1648 
1649     if (!dc_isar_feature(aa64_pauth, s)) {
1650         return false;
1651     }
1652     if (s->current_el == 0) {
1653         return false;
1654     }
1655     /* The FGT trap takes precedence over an auth trap. */
1656     if (s->fgt_eret) {
1657         gen_exception_insn_el(s, 0, EXCP_UDEF, a->m ? 3 : 2, 2);
1658         return true;
1659     }
1660     dst = tcg_temp_new_i64();
1661     tcg_gen_ld_i64(dst, cpu_env,
1662                    offsetof(CPUARMState, elr_el[s->current_el]));
1663 
1664     dst = auth_branch_target(s, dst, cpu_X[31], !a->m);
1665 
1666     translator_io_start(&s->base);
1667 
1668     gen_helper_exception_return(cpu_env, dst);
1669     /* Must exit loop to check un-masked IRQs */
1670     s->base.is_jmp = DISAS_EXIT;
1671     return true;
1672 }
1673 
1674 static bool trans_NOP(DisasContext *s, arg_NOP *a)
1675 {
1676     return true;
1677 }
1678 
1679 static bool trans_YIELD(DisasContext *s, arg_YIELD *a)
1680 {
1681     /*
1682      * When running in MTTCG we don't generate jumps to the yield and
1683      * WFE helpers as it won't affect the scheduling of other vCPUs.
1684      * If we wanted to more completely model WFE/SEV so we don't busy
1685      * spin unnecessarily we would need to do something more involved.
1686      */
1687     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1688         s->base.is_jmp = DISAS_YIELD;
1689     }
1690     return true;
1691 }
1692 
1693 static bool trans_WFI(DisasContext *s, arg_WFI *a)
1694 {
1695     s->base.is_jmp = DISAS_WFI;
1696     return true;
1697 }
1698 
1699 static bool trans_WFE(DisasContext *s, arg_WFI *a)
1700 {
1701     /*
1702      * When running in MTTCG we don't generate jumps to the yield and
1703      * WFE helpers as it won't affect the scheduling of other vCPUs.
1704      * If we wanted to more completely model WFE/SEV so we don't busy
1705      * spin unnecessarily we would need to do something more involved.
1706      */
1707     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1708         s->base.is_jmp = DISAS_WFE;
1709     }
1710     return true;
1711 }
1712 
1713 static bool trans_XPACLRI(DisasContext *s, arg_XPACLRI *a)
1714 {
1715     if (s->pauth_active) {
1716         gen_helper_xpaci(cpu_X[30], cpu_env, cpu_X[30]);
1717     }
1718     return true;
1719 }
1720 
1721 static bool trans_PACIA1716(DisasContext *s, arg_PACIA1716 *a)
1722 {
1723     if (s->pauth_active) {
1724         gen_helper_pacia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1725     }
1726     return true;
1727 }
1728 
1729 static bool trans_PACIB1716(DisasContext *s, arg_PACIB1716 *a)
1730 {
1731     if (s->pauth_active) {
1732         gen_helper_pacib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1733     }
1734     return true;
1735 }
1736 
1737 static bool trans_AUTIA1716(DisasContext *s, arg_AUTIA1716 *a)
1738 {
1739     if (s->pauth_active) {
1740         gen_helper_autia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1741     }
1742     return true;
1743 }
1744 
1745 static bool trans_AUTIB1716(DisasContext *s, arg_AUTIB1716 *a)
1746 {
1747     if (s->pauth_active) {
1748         gen_helper_autib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1749     }
1750     return true;
1751 }
1752 
1753 static bool trans_ESB(DisasContext *s, arg_ESB *a)
1754 {
1755     /* Without RAS, we must implement this as NOP. */
1756     if (dc_isar_feature(aa64_ras, s)) {
1757         /*
1758          * QEMU does not have a source of physical SErrors,
1759          * so we are only concerned with virtual SErrors.
1760          * The pseudocode in the ARM for this case is
1761          *   if PSTATE.EL IN {EL0, EL1} && EL2Enabled() then
1762          *      AArch64.vESBOperation();
1763          * Most of the condition can be evaluated at translation time.
1764          * Test for EL2 present, and defer test for SEL2 to runtime.
1765          */
1766         if (s->current_el <= 1 && arm_dc_feature(s, ARM_FEATURE_EL2)) {
1767             gen_helper_vesb(cpu_env);
1768         }
1769     }
1770     return true;
1771 }
1772 
1773 static bool trans_PACIAZ(DisasContext *s, arg_PACIAZ *a)
1774 {
1775     if (s->pauth_active) {
1776         gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], tcg_constant_i64(0));
1777     }
1778     return true;
1779 }
1780 
1781 static bool trans_PACIASP(DisasContext *s, arg_PACIASP *a)
1782 {
1783     if (s->pauth_active) {
1784         gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1785     }
1786     return true;
1787 }
1788 
1789 static bool trans_PACIBZ(DisasContext *s, arg_PACIBZ *a)
1790 {
1791     if (s->pauth_active) {
1792         gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], tcg_constant_i64(0));
1793     }
1794     return true;
1795 }
1796 
1797 static bool trans_PACIBSP(DisasContext *s, arg_PACIBSP *a)
1798 {
1799     if (s->pauth_active) {
1800         gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1801     }
1802     return true;
1803 }
1804 
1805 static bool trans_AUTIAZ(DisasContext *s, arg_AUTIAZ *a)
1806 {
1807     if (s->pauth_active) {
1808         gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], tcg_constant_i64(0));
1809     }
1810     return true;
1811 }
1812 
1813 static bool trans_AUTIASP(DisasContext *s, arg_AUTIASP *a)
1814 {
1815     if (s->pauth_active) {
1816         gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1817     }
1818     return true;
1819 }
1820 
1821 static bool trans_AUTIBZ(DisasContext *s, arg_AUTIBZ *a)
1822 {
1823     if (s->pauth_active) {
1824         gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], tcg_constant_i64(0));
1825     }
1826     return true;
1827 }
1828 
1829 static bool trans_AUTIBSP(DisasContext *s, arg_AUTIBSP *a)
1830 {
1831     if (s->pauth_active) {
1832         gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1833     }
1834     return true;
1835 }
1836 
1837 static bool trans_CLREX(DisasContext *s, arg_CLREX *a)
1838 {
1839     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1840     return true;
1841 }
1842 
1843 static bool trans_DSB_DMB(DisasContext *s, arg_DSB_DMB *a)
1844 {
1845     /* We handle DSB and DMB the same way */
1846     TCGBar bar;
1847 
1848     switch (a->types) {
1849     case 1: /* MBReqTypes_Reads */
1850         bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1851         break;
1852     case 2: /* MBReqTypes_Writes */
1853         bar = TCG_BAR_SC | TCG_MO_ST_ST;
1854         break;
1855     default: /* MBReqTypes_All */
1856         bar = TCG_BAR_SC | TCG_MO_ALL;
1857         break;
1858     }
1859     tcg_gen_mb(bar);
1860     return true;
1861 }
1862 
1863 static bool trans_ISB(DisasContext *s, arg_ISB *a)
1864 {
1865     /*
1866      * We need to break the TB after this insn to execute
1867      * self-modifying code correctly and also to take
1868      * any pending interrupts immediately.
1869      */
1870     reset_btype(s);
1871     gen_goto_tb(s, 0, 4);
1872     return true;
1873 }
1874 
1875 static bool trans_SB(DisasContext *s, arg_SB *a)
1876 {
1877     if (!dc_isar_feature(aa64_sb, s)) {
1878         return false;
1879     }
1880     /*
1881      * TODO: There is no speculation barrier opcode for TCG;
1882      * MB and end the TB instead.
1883      */
1884     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1885     gen_goto_tb(s, 0, 4);
1886     return true;
1887 }
1888 
1889 static bool trans_CFINV(DisasContext *s, arg_CFINV *a)
1890 {
1891     if (!dc_isar_feature(aa64_condm_4, s)) {
1892         return false;
1893     }
1894     tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1895     return true;
1896 }
1897 
1898 static bool trans_XAFLAG(DisasContext *s, arg_XAFLAG *a)
1899 {
1900     TCGv_i32 z;
1901 
1902     if (!dc_isar_feature(aa64_condm_5, s)) {
1903         return false;
1904     }
1905 
1906     z = tcg_temp_new_i32();
1907 
1908     tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1909 
1910     /*
1911      * (!C & !Z) << 31
1912      * (!(C | Z)) << 31
1913      * ~((C | Z) << 31)
1914      * ~-(C | Z)
1915      * (C | Z) - 1
1916      */
1917     tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1918     tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1919 
1920     /* !(Z & C) */
1921     tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1922     tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1923 
1924     /* (!C & Z) << 31 -> -(Z & ~C) */
1925     tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1926     tcg_gen_neg_i32(cpu_VF, cpu_VF);
1927 
1928     /* C | Z */
1929     tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1930 
1931     return true;
1932 }
1933 
1934 static bool trans_AXFLAG(DisasContext *s, arg_AXFLAG *a)
1935 {
1936     if (!dc_isar_feature(aa64_condm_5, s)) {
1937         return false;
1938     }
1939 
1940     tcg_gen_sari_i32(cpu_VF, cpu_VF, 31);         /* V ? -1 : 0 */
1941     tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF);     /* C & !V */
1942 
1943     /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1944     tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1945 
1946     tcg_gen_movi_i32(cpu_NF, 0);
1947     tcg_gen_movi_i32(cpu_VF, 0);
1948 
1949     return true;
1950 }
1951 
1952 static bool trans_MSR_i_UAO(DisasContext *s, arg_i *a)
1953 {
1954     if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1955         return false;
1956     }
1957     if (a->imm & 1) {
1958         set_pstate_bits(PSTATE_UAO);
1959     } else {
1960         clear_pstate_bits(PSTATE_UAO);
1961     }
1962     gen_rebuild_hflags(s);
1963     s->base.is_jmp = DISAS_TOO_MANY;
1964     return true;
1965 }
1966 
1967 static bool trans_MSR_i_PAN(DisasContext *s, arg_i *a)
1968 {
1969     if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1970         return false;
1971     }
1972     if (a->imm & 1) {
1973         set_pstate_bits(PSTATE_PAN);
1974     } else {
1975         clear_pstate_bits(PSTATE_PAN);
1976     }
1977     gen_rebuild_hflags(s);
1978     s->base.is_jmp = DISAS_TOO_MANY;
1979     return true;
1980 }
1981 
1982 static bool trans_MSR_i_SPSEL(DisasContext *s, arg_i *a)
1983 {
1984     if (s->current_el == 0) {
1985         return false;
1986     }
1987     gen_helper_msr_i_spsel(cpu_env, tcg_constant_i32(a->imm & PSTATE_SP));
1988     s->base.is_jmp = DISAS_TOO_MANY;
1989     return true;
1990 }
1991 
1992 static bool trans_MSR_i_SBSS(DisasContext *s, arg_i *a)
1993 {
1994     if (!dc_isar_feature(aa64_ssbs, s)) {
1995         return false;
1996     }
1997     if (a->imm & 1) {
1998         set_pstate_bits(PSTATE_SSBS);
1999     } else {
2000         clear_pstate_bits(PSTATE_SSBS);
2001     }
2002     /* Don't need to rebuild hflags since SSBS is a nop */
2003     s->base.is_jmp = DISAS_TOO_MANY;
2004     return true;
2005 }
2006 
2007 static bool trans_MSR_i_DIT(DisasContext *s, arg_i *a)
2008 {
2009     if (!dc_isar_feature(aa64_dit, s)) {
2010         return false;
2011     }
2012     if (a->imm & 1) {
2013         set_pstate_bits(PSTATE_DIT);
2014     } else {
2015         clear_pstate_bits(PSTATE_DIT);
2016     }
2017     /* There's no need to rebuild hflags because DIT is a nop */
2018     s->base.is_jmp = DISAS_TOO_MANY;
2019     return true;
2020 }
2021 
2022 static bool trans_MSR_i_TCO(DisasContext *s, arg_i *a)
2023 {
2024     if (dc_isar_feature(aa64_mte, s)) {
2025         /* Full MTE is enabled -- set the TCO bit as directed. */
2026         if (a->imm & 1) {
2027             set_pstate_bits(PSTATE_TCO);
2028         } else {
2029             clear_pstate_bits(PSTATE_TCO);
2030         }
2031         gen_rebuild_hflags(s);
2032         /* Many factors, including TCO, go into MTE_ACTIVE. */
2033         s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
2034         return true;
2035     } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
2036         /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI.  */
2037         return true;
2038     } else {
2039         /* Insn not present */
2040         return false;
2041     }
2042 }
2043 
2044 static bool trans_MSR_i_DAIFSET(DisasContext *s, arg_i *a)
2045 {
2046     gen_helper_msr_i_daifset(cpu_env, tcg_constant_i32(a->imm));
2047     s->base.is_jmp = DISAS_TOO_MANY;
2048     return true;
2049 }
2050 
2051 static bool trans_MSR_i_DAIFCLEAR(DisasContext *s, arg_i *a)
2052 {
2053     gen_helper_msr_i_daifclear(cpu_env, tcg_constant_i32(a->imm));
2054     /* Exit the cpu loop to re-evaluate pending IRQs. */
2055     s->base.is_jmp = DISAS_UPDATE_EXIT;
2056     return true;
2057 }
2058 
2059 static bool trans_MSR_i_SVCR(DisasContext *s, arg_MSR_i_SVCR *a)
2060 {
2061     if (!dc_isar_feature(aa64_sme, s) || a->mask == 0) {
2062         return false;
2063     }
2064     if (sme_access_check(s)) {
2065         int old = s->pstate_sm | (s->pstate_za << 1);
2066         int new = a->imm * 3;
2067 
2068         if ((old ^ new) & a->mask) {
2069             /* At least one bit changes. */
2070             gen_helper_set_svcr(cpu_env, tcg_constant_i32(new),
2071                                 tcg_constant_i32(a->mask));
2072             s->base.is_jmp = DISAS_TOO_MANY;
2073         }
2074     }
2075     return true;
2076 }
2077 
2078 static void gen_get_nzcv(TCGv_i64 tcg_rt)
2079 {
2080     TCGv_i32 tmp = tcg_temp_new_i32();
2081     TCGv_i32 nzcv = tcg_temp_new_i32();
2082 
2083     /* build bit 31, N */
2084     tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
2085     /* build bit 30, Z */
2086     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
2087     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
2088     /* build bit 29, C */
2089     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
2090     /* build bit 28, V */
2091     tcg_gen_shri_i32(tmp, cpu_VF, 31);
2092     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
2093     /* generate result */
2094     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
2095 }
2096 
2097 static void gen_set_nzcv(TCGv_i64 tcg_rt)
2098 {
2099     TCGv_i32 nzcv = tcg_temp_new_i32();
2100 
2101     /* take NZCV from R[t] */
2102     tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
2103 
2104     /* bit 31, N */
2105     tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
2106     /* bit 30, Z */
2107     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
2108     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
2109     /* bit 29, C */
2110     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
2111     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
2112     /* bit 28, V */
2113     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
2114     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
2115 }
2116 
2117 static void gen_sysreg_undef(DisasContext *s, bool isread,
2118                              uint8_t op0, uint8_t op1, uint8_t op2,
2119                              uint8_t crn, uint8_t crm, uint8_t rt)
2120 {
2121     /*
2122      * Generate code to emit an UNDEF with correct syndrome
2123      * information for a failed system register access.
2124      * This is EC_UNCATEGORIZED (ie a standard UNDEF) in most cases,
2125      * but if FEAT_IDST is implemented then read accesses to registers
2126      * in the feature ID space are reported with the EC_SYSTEMREGISTERTRAP
2127      * syndrome.
2128      */
2129     uint32_t syndrome;
2130 
2131     if (isread && dc_isar_feature(aa64_ids, s) &&
2132         arm_cpreg_encoding_in_idspace(op0, op1, op2, crn, crm)) {
2133         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2134     } else {
2135         syndrome = syn_uncategorized();
2136     }
2137     gen_exception_insn(s, 0, EXCP_UDEF, syndrome);
2138 }
2139 
2140 /* MRS - move from system register
2141  * MSR (register) - move to system register
2142  * SYS
2143  * SYSL
2144  * These are all essentially the same insn in 'read' and 'write'
2145  * versions, with varying op0 fields.
2146  */
2147 static void handle_sys(DisasContext *s, bool isread,
2148                        unsigned int op0, unsigned int op1, unsigned int op2,
2149                        unsigned int crn, unsigned int crm, unsigned int rt)
2150 {
2151     uint32_t key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
2152                                       crn, crm, op0, op1, op2);
2153     const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
2154     bool need_exit_tb = false;
2155     TCGv_ptr tcg_ri = NULL;
2156     TCGv_i64 tcg_rt;
2157 
2158     if (!ri) {
2159         /* Unknown register; this might be a guest error or a QEMU
2160          * unimplemented feature.
2161          */
2162         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
2163                       "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
2164                       isread ? "read" : "write", op0, op1, crn, crm, op2);
2165         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2166         return;
2167     }
2168 
2169     /* Check access permissions */
2170     if (!cp_access_ok(s->current_el, ri, isread)) {
2171         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2172         return;
2173     }
2174 
2175     if (ri->accessfn || (ri->fgt && s->fgt_active)) {
2176         /* Emit code to perform further access permissions checks at
2177          * runtime; this may result in an exception.
2178          */
2179         uint32_t syndrome;
2180 
2181         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2182         gen_a64_update_pc(s, 0);
2183         tcg_ri = tcg_temp_new_ptr();
2184         gen_helper_access_check_cp_reg(tcg_ri, cpu_env,
2185                                        tcg_constant_i32(key),
2186                                        tcg_constant_i32(syndrome),
2187                                        tcg_constant_i32(isread));
2188     } else if (ri->type & ARM_CP_RAISES_EXC) {
2189         /*
2190          * The readfn or writefn might raise an exception;
2191          * synchronize the CPU state in case it does.
2192          */
2193         gen_a64_update_pc(s, 0);
2194     }
2195 
2196     /* Handle special cases first */
2197     switch (ri->type & ARM_CP_SPECIAL_MASK) {
2198     case 0:
2199         break;
2200     case ARM_CP_NOP:
2201         return;
2202     case ARM_CP_NZCV:
2203         tcg_rt = cpu_reg(s, rt);
2204         if (isread) {
2205             gen_get_nzcv(tcg_rt);
2206         } else {
2207             gen_set_nzcv(tcg_rt);
2208         }
2209         return;
2210     case ARM_CP_CURRENTEL:
2211         /* Reads as current EL value from pstate, which is
2212          * guaranteed to be constant by the tb flags.
2213          */
2214         tcg_rt = cpu_reg(s, rt);
2215         tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
2216         return;
2217     case ARM_CP_DC_ZVA:
2218         /* Writes clear the aligned block of memory which rt points into. */
2219         if (s->mte_active[0]) {
2220             int desc = 0;
2221 
2222             desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
2223             desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
2224             desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
2225 
2226             tcg_rt = tcg_temp_new_i64();
2227             gen_helper_mte_check_zva(tcg_rt, cpu_env,
2228                                      tcg_constant_i32(desc), cpu_reg(s, rt));
2229         } else {
2230             tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
2231         }
2232         gen_helper_dc_zva(cpu_env, tcg_rt);
2233         return;
2234     case ARM_CP_DC_GVA:
2235         {
2236             TCGv_i64 clean_addr, tag;
2237 
2238             /*
2239              * DC_GVA, like DC_ZVA, requires that we supply the original
2240              * pointer for an invalid page.  Probe that address first.
2241              */
2242             tcg_rt = cpu_reg(s, rt);
2243             clean_addr = clean_data_tbi(s, tcg_rt);
2244             gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
2245 
2246             if (s->ata) {
2247                 /* Extract the tag from the register to match STZGM.  */
2248                 tag = tcg_temp_new_i64();
2249                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2250                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2251             }
2252         }
2253         return;
2254     case ARM_CP_DC_GZVA:
2255         {
2256             TCGv_i64 clean_addr, tag;
2257 
2258             /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
2259             tcg_rt = cpu_reg(s, rt);
2260             clean_addr = clean_data_tbi(s, tcg_rt);
2261             gen_helper_dc_zva(cpu_env, clean_addr);
2262 
2263             if (s->ata) {
2264                 /* Extract the tag from the register to match STZGM.  */
2265                 tag = tcg_temp_new_i64();
2266                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2267                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2268             }
2269         }
2270         return;
2271     default:
2272         g_assert_not_reached();
2273     }
2274     if ((ri->type & ARM_CP_FPU) && !fp_access_check_only(s)) {
2275         return;
2276     } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
2277         return;
2278     } else if ((ri->type & ARM_CP_SME) && !sme_access_check(s)) {
2279         return;
2280     }
2281 
2282     if (ri->type & ARM_CP_IO) {
2283         /* I/O operations must end the TB here (whether read or write) */
2284         need_exit_tb = translator_io_start(&s->base);
2285     }
2286 
2287     tcg_rt = cpu_reg(s, rt);
2288 
2289     if (isread) {
2290         if (ri->type & ARM_CP_CONST) {
2291             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
2292         } else if (ri->readfn) {
2293             if (!tcg_ri) {
2294                 tcg_ri = gen_lookup_cp_reg(key);
2295             }
2296             gen_helper_get_cp_reg64(tcg_rt, cpu_env, tcg_ri);
2297         } else {
2298             tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
2299         }
2300     } else {
2301         if (ri->type & ARM_CP_CONST) {
2302             /* If not forbidden by access permissions, treat as WI */
2303             return;
2304         } else if (ri->writefn) {
2305             if (!tcg_ri) {
2306                 tcg_ri = gen_lookup_cp_reg(key);
2307             }
2308             gen_helper_set_cp_reg64(cpu_env, tcg_ri, tcg_rt);
2309         } else {
2310             tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
2311         }
2312     }
2313 
2314     if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
2315         /*
2316          * A write to any coprocessor register that ends a TB
2317          * must rebuild the hflags for the next TB.
2318          */
2319         gen_rebuild_hflags(s);
2320         /*
2321          * We default to ending the TB on a coprocessor register write,
2322          * but allow this to be suppressed by the register definition
2323          * (usually only necessary to work around guest bugs).
2324          */
2325         need_exit_tb = true;
2326     }
2327     if (need_exit_tb) {
2328         s->base.is_jmp = DISAS_UPDATE_EXIT;
2329     }
2330 }
2331 
2332 static bool trans_SYS(DisasContext *s, arg_SYS *a)
2333 {
2334     handle_sys(s, a->l, a->op0, a->op1, a->op2, a->crn, a->crm, a->rt);
2335     return true;
2336 }
2337 
2338 static bool trans_SVC(DisasContext *s, arg_i *a)
2339 {
2340     /*
2341      * For SVC, HVC and SMC we advance the single-step state
2342      * machine before taking the exception. This is architecturally
2343      * mandated, to ensure that single-stepping a system call
2344      * instruction works properly.
2345      */
2346     uint32_t syndrome = syn_aa64_svc(a->imm);
2347     if (s->fgt_svc) {
2348         gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2349         return true;
2350     }
2351     gen_ss_advance(s);
2352     gen_exception_insn(s, 4, EXCP_SWI, syndrome);
2353     return true;
2354 }
2355 
2356 static bool trans_HVC(DisasContext *s, arg_i *a)
2357 {
2358     if (s->current_el == 0) {
2359         unallocated_encoding(s);
2360         return true;
2361     }
2362     /*
2363      * The pre HVC helper handles cases when HVC gets trapped
2364      * as an undefined insn by runtime configuration.
2365      */
2366     gen_a64_update_pc(s, 0);
2367     gen_helper_pre_hvc(cpu_env);
2368     /* Architecture requires ss advance before we do the actual work */
2369     gen_ss_advance(s);
2370     gen_exception_insn_el(s, 4, EXCP_HVC, syn_aa64_hvc(a->imm), 2);
2371     return true;
2372 }
2373 
2374 static bool trans_SMC(DisasContext *s, arg_i *a)
2375 {
2376     if (s->current_el == 0) {
2377         unallocated_encoding(s);
2378         return true;
2379     }
2380     gen_a64_update_pc(s, 0);
2381     gen_helper_pre_smc(cpu_env, tcg_constant_i32(syn_aa64_smc(a->imm)));
2382     /* Architecture requires ss advance before we do the actual work */
2383     gen_ss_advance(s);
2384     gen_exception_insn_el(s, 4, EXCP_SMC, syn_aa64_smc(a->imm), 3);
2385     return true;
2386 }
2387 
2388 static bool trans_BRK(DisasContext *s, arg_i *a)
2389 {
2390     gen_exception_bkpt_insn(s, syn_aa64_bkpt(a->imm));
2391     return true;
2392 }
2393 
2394 static bool trans_HLT(DisasContext *s, arg_i *a)
2395 {
2396     /*
2397      * HLT. This has two purposes.
2398      * Architecturally, it is an external halting debug instruction.
2399      * Since QEMU doesn't implement external debug, we treat this as
2400      * it is required for halting debug disabled: it will UNDEF.
2401      * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2402      */
2403     if (semihosting_enabled(s->current_el == 0) && a->imm == 0xf000) {
2404         gen_exception_internal_insn(s, EXCP_SEMIHOST);
2405     } else {
2406         unallocated_encoding(s);
2407     }
2408     return true;
2409 }
2410 
2411 /*
2412  * Load/Store exclusive instructions are implemented by remembering
2413  * the value/address loaded, and seeing if these are the same
2414  * when the store is performed. This is not actually the architecturally
2415  * mandated semantics, but it works for typical guest code sequences
2416  * and avoids having to monitor regular stores.
2417  *
2418  * The store exclusive uses the atomic cmpxchg primitives to avoid
2419  * races in multi-threaded linux-user and when MTTCG softmmu is
2420  * enabled.
2421  */
2422 static void gen_load_exclusive(DisasContext *s, int rt, int rt2, int rn,
2423                                int size, bool is_pair)
2424 {
2425     int idx = get_mem_index(s);
2426     TCGv_i64 dirty_addr, clean_addr;
2427     MemOp memop = check_atomic_align(s, rn, size + is_pair);
2428 
2429     s->is_ldex = true;
2430     dirty_addr = cpu_reg_sp(s, rn);
2431     clean_addr = gen_mte_check1(s, dirty_addr, false, rn != 31, memop);
2432 
2433     g_assert(size <= 3);
2434     if (is_pair) {
2435         g_assert(size >= 2);
2436         if (size == 2) {
2437             tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2438             if (s->be_data == MO_LE) {
2439                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2440                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2441             } else {
2442                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2443                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2444             }
2445         } else {
2446             TCGv_i128 t16 = tcg_temp_new_i128();
2447 
2448             tcg_gen_qemu_ld_i128(t16, clean_addr, idx, memop);
2449 
2450             if (s->be_data == MO_LE) {
2451                 tcg_gen_extr_i128_i64(cpu_exclusive_val,
2452                                       cpu_exclusive_high, t16);
2453             } else {
2454                 tcg_gen_extr_i128_i64(cpu_exclusive_high,
2455                                       cpu_exclusive_val, t16);
2456             }
2457             tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2458             tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2459         }
2460     } else {
2461         tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2462         tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2463     }
2464     tcg_gen_mov_i64(cpu_exclusive_addr, clean_addr);
2465 }
2466 
2467 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2468                                 int rn, int size, int is_pair)
2469 {
2470     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2471      *     && (!is_pair || env->exclusive_high == [addr + datasize])) {
2472      *     [addr] = {Rt};
2473      *     if (is_pair) {
2474      *         [addr + datasize] = {Rt2};
2475      *     }
2476      *     {Rd} = 0;
2477      * } else {
2478      *     {Rd} = 1;
2479      * }
2480      * env->exclusive_addr = -1;
2481      */
2482     TCGLabel *fail_label = gen_new_label();
2483     TCGLabel *done_label = gen_new_label();
2484     TCGv_i64 tmp, clean_addr;
2485     MemOp memop;
2486 
2487     /*
2488      * FIXME: We are out of spec here.  We have recorded only the address
2489      * from load_exclusive, not the entire range, and we assume that the
2490      * size of the access on both sides match.  The architecture allows the
2491      * store to be smaller than the load, so long as the stored bytes are
2492      * within the range recorded by the load.
2493      */
2494 
2495     /* See AArch64.ExclusiveMonitorsPass() and AArch64.IsExclusiveVA(). */
2496     clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2497     tcg_gen_brcond_i64(TCG_COND_NE, clean_addr, cpu_exclusive_addr, fail_label);
2498 
2499     /*
2500      * The write, and any associated faults, only happen if the virtual
2501      * and physical addresses pass the exclusive monitor check.  These
2502      * faults are exceedingly unlikely, because normally the guest uses
2503      * the exact same address register for the load_exclusive, and we
2504      * would have recognized these faults there.
2505      *
2506      * It is possible to trigger an alignment fault pre-LSE2, e.g. with an
2507      * unaligned 4-byte write within the range of an aligned 8-byte load.
2508      * With LSE2, the store would need to cross a 16-byte boundary when the
2509      * load did not, which would mean the store is outside the range
2510      * recorded for the monitor, which would have failed a corrected monitor
2511      * check above.  For now, we assume no size change and retain the
2512      * MO_ALIGN to let tcg know what we checked in the load_exclusive.
2513      *
2514      * It is possible to trigger an MTE fault, by performing the load with
2515      * a virtual address with a valid tag and performing the store with the
2516      * same virtual address and a different invalid tag.
2517      */
2518     memop = size + is_pair;
2519     if (memop == MO_128 || !dc_isar_feature(aa64_lse2, s)) {
2520         memop |= MO_ALIGN;
2521     }
2522     memop = finalize_memop(s, memop);
2523     gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2524 
2525     tmp = tcg_temp_new_i64();
2526     if (is_pair) {
2527         if (size == 2) {
2528             if (s->be_data == MO_LE) {
2529                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2530             } else {
2531                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2532             }
2533             tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2534                                        cpu_exclusive_val, tmp,
2535                                        get_mem_index(s), memop);
2536             tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2537         } else {
2538             TCGv_i128 t16 = tcg_temp_new_i128();
2539             TCGv_i128 c16 = tcg_temp_new_i128();
2540             TCGv_i64 a, b;
2541 
2542             if (s->be_data == MO_LE) {
2543                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt), cpu_reg(s, rt2));
2544                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_val,
2545                                         cpu_exclusive_high);
2546             } else {
2547                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt2), cpu_reg(s, rt));
2548                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_high,
2549                                         cpu_exclusive_val);
2550             }
2551 
2552             tcg_gen_atomic_cmpxchg_i128(t16, cpu_exclusive_addr, c16, t16,
2553                                         get_mem_index(s), memop);
2554 
2555             a = tcg_temp_new_i64();
2556             b = tcg_temp_new_i64();
2557             if (s->be_data == MO_LE) {
2558                 tcg_gen_extr_i128_i64(a, b, t16);
2559             } else {
2560                 tcg_gen_extr_i128_i64(b, a, t16);
2561             }
2562 
2563             tcg_gen_xor_i64(a, a, cpu_exclusive_val);
2564             tcg_gen_xor_i64(b, b, cpu_exclusive_high);
2565             tcg_gen_or_i64(tmp, a, b);
2566 
2567             tcg_gen_setcondi_i64(TCG_COND_NE, tmp, tmp, 0);
2568         }
2569     } else {
2570         tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2571                                    cpu_reg(s, rt), get_mem_index(s), memop);
2572         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2573     }
2574     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2575     tcg_gen_br(done_label);
2576 
2577     gen_set_label(fail_label);
2578     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2579     gen_set_label(done_label);
2580     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2581 }
2582 
2583 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2584                                  int rn, int size)
2585 {
2586     TCGv_i64 tcg_rs = cpu_reg(s, rs);
2587     TCGv_i64 tcg_rt = cpu_reg(s, rt);
2588     int memidx = get_mem_index(s);
2589     TCGv_i64 clean_addr;
2590     MemOp memop;
2591 
2592     if (rn == 31) {
2593         gen_check_sp_alignment(s);
2594     }
2595     memop = check_atomic_align(s, rn, size);
2596     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2597     tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt,
2598                                memidx, memop);
2599 }
2600 
2601 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2602                                       int rn, int size)
2603 {
2604     TCGv_i64 s1 = cpu_reg(s, rs);
2605     TCGv_i64 s2 = cpu_reg(s, rs + 1);
2606     TCGv_i64 t1 = cpu_reg(s, rt);
2607     TCGv_i64 t2 = cpu_reg(s, rt + 1);
2608     TCGv_i64 clean_addr;
2609     int memidx = get_mem_index(s);
2610     MemOp memop;
2611 
2612     if (rn == 31) {
2613         gen_check_sp_alignment(s);
2614     }
2615 
2616     /* This is a single atomic access, despite the "pair". */
2617     memop = check_atomic_align(s, rn, size + 1);
2618     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2619 
2620     if (size == 2) {
2621         TCGv_i64 cmp = tcg_temp_new_i64();
2622         TCGv_i64 val = tcg_temp_new_i64();
2623 
2624         if (s->be_data == MO_LE) {
2625             tcg_gen_concat32_i64(val, t1, t2);
2626             tcg_gen_concat32_i64(cmp, s1, s2);
2627         } else {
2628             tcg_gen_concat32_i64(val, t2, t1);
2629             tcg_gen_concat32_i64(cmp, s2, s1);
2630         }
2631 
2632         tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx, memop);
2633 
2634         if (s->be_data == MO_LE) {
2635             tcg_gen_extr32_i64(s1, s2, cmp);
2636         } else {
2637             tcg_gen_extr32_i64(s2, s1, cmp);
2638         }
2639     } else {
2640         TCGv_i128 cmp = tcg_temp_new_i128();
2641         TCGv_i128 val = tcg_temp_new_i128();
2642 
2643         if (s->be_data == MO_LE) {
2644             tcg_gen_concat_i64_i128(val, t1, t2);
2645             tcg_gen_concat_i64_i128(cmp, s1, s2);
2646         } else {
2647             tcg_gen_concat_i64_i128(val, t2, t1);
2648             tcg_gen_concat_i64_i128(cmp, s2, s1);
2649         }
2650 
2651         tcg_gen_atomic_cmpxchg_i128(cmp, clean_addr, cmp, val, memidx, memop);
2652 
2653         if (s->be_data == MO_LE) {
2654             tcg_gen_extr_i128_i64(s1, s2, cmp);
2655         } else {
2656             tcg_gen_extr_i128_i64(s2, s1, cmp);
2657         }
2658     }
2659 }
2660 
2661 /*
2662  * Compute the ISS.SF bit for syndrome information if an exception
2663  * is taken on a load or store. This indicates whether the instruction
2664  * is accessing a 32-bit or 64-bit register. This logic is derived
2665  * from the ARMv8 specs for LDR (Shared decode for all encodings).
2666  */
2667 static bool ldst_iss_sf(int size, bool sign, bool ext)
2668 {
2669 
2670     if (sign) {
2671         /*
2672          * Signed loads are 64 bit results if we are not going to
2673          * do a zero-extend from 32 to 64 after the load.
2674          * (For a store, sign and ext are always false.)
2675          */
2676         return !ext;
2677     } else {
2678         /* Unsigned loads/stores work at the specified size */
2679         return size == MO_64;
2680     }
2681 }
2682 
2683 static bool trans_STXR(DisasContext *s, arg_stxr *a)
2684 {
2685     if (a->rn == 31) {
2686         gen_check_sp_alignment(s);
2687     }
2688     if (a->lasr) {
2689         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2690     }
2691     gen_store_exclusive(s, a->rs, a->rt, a->rt2, a->rn, a->sz, false);
2692     return true;
2693 }
2694 
2695 static bool trans_LDXR(DisasContext *s, arg_stxr *a)
2696 {
2697     if (a->rn == 31) {
2698         gen_check_sp_alignment(s);
2699     }
2700     gen_load_exclusive(s, a->rt, a->rt2, a->rn, a->sz, false);
2701     if (a->lasr) {
2702         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2703     }
2704     return true;
2705 }
2706 
2707 static bool trans_STLR(DisasContext *s, arg_stlr *a)
2708 {
2709     TCGv_i64 clean_addr;
2710     MemOp memop;
2711     bool iss_sf = ldst_iss_sf(a->sz, false, false);
2712 
2713     /*
2714      * StoreLORelease is the same as Store-Release for QEMU, but
2715      * needs the feature-test.
2716      */
2717     if (!a->lasr && !dc_isar_feature(aa64_lor, s)) {
2718         return false;
2719     }
2720     /* Generate ISS for non-exclusive accesses including LASR.  */
2721     if (a->rn == 31) {
2722         gen_check_sp_alignment(s);
2723     }
2724     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2725     memop = check_ordered_align(s, a->rn, 0, true, a->sz);
2726     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn),
2727                                 true, a->rn != 31, memop);
2728     do_gpr_st(s, cpu_reg(s, a->rt), clean_addr, memop, true, a->rt,
2729               iss_sf, a->lasr);
2730     return true;
2731 }
2732 
2733 static bool trans_LDAR(DisasContext *s, arg_stlr *a)
2734 {
2735     TCGv_i64 clean_addr;
2736     MemOp memop;
2737     bool iss_sf = ldst_iss_sf(a->sz, false, false);
2738 
2739     /* LoadLOAcquire is the same as Load-Acquire for QEMU.  */
2740     if (!a->lasr && !dc_isar_feature(aa64_lor, s)) {
2741         return false;
2742     }
2743     /* Generate ISS for non-exclusive accesses including LASR.  */
2744     if (a->rn == 31) {
2745         gen_check_sp_alignment(s);
2746     }
2747     memop = check_ordered_align(s, a->rn, 0, false, a->sz);
2748     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn),
2749                                 false, a->rn != 31, memop);
2750     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, memop, false, true,
2751               a->rt, iss_sf, a->lasr);
2752     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2753     return true;
2754 }
2755 
2756 static bool trans_STXP(DisasContext *s, arg_stxr *a)
2757 {
2758     if (a->rn == 31) {
2759         gen_check_sp_alignment(s);
2760     }
2761     if (a->lasr) {
2762         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2763     }
2764     gen_store_exclusive(s, a->rs, a->rt, a->rt2, a->rn, a->sz, true);
2765     return true;
2766 }
2767 
2768 static bool trans_LDXP(DisasContext *s, arg_stxr *a)
2769 {
2770     if (a->rn == 31) {
2771         gen_check_sp_alignment(s);
2772     }
2773     gen_load_exclusive(s, a->rt, a->rt2, a->rn, a->sz, true);
2774     if (a->lasr) {
2775         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2776     }
2777     return true;
2778 }
2779 
2780 static bool trans_CASP(DisasContext *s, arg_CASP *a)
2781 {
2782     if (!dc_isar_feature(aa64_atomics, s)) {
2783         return false;
2784     }
2785     if (((a->rt | a->rs) & 1) != 0) {
2786         return false;
2787     }
2788 
2789     gen_compare_and_swap_pair(s, a->rs, a->rt, a->rn, a->sz);
2790     return true;
2791 }
2792 
2793 static bool trans_CAS(DisasContext *s, arg_CAS *a)
2794 {
2795     if (!dc_isar_feature(aa64_atomics, s)) {
2796         return false;
2797     }
2798     gen_compare_and_swap(s, a->rs, a->rt, a->rn, a->sz);
2799     return true;
2800 }
2801 
2802 static bool trans_LD_lit(DisasContext *s, arg_ldlit *a)
2803 {
2804     bool iss_sf = ldst_iss_sf(a->sz, a->sign, false);
2805     TCGv_i64 tcg_rt = cpu_reg(s, a->rt);
2806     TCGv_i64 clean_addr = tcg_temp_new_i64();
2807     MemOp memop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
2808 
2809     gen_pc_plus_diff(s, clean_addr, a->imm);
2810     do_gpr_ld(s, tcg_rt, clean_addr, memop,
2811               false, true, a->rt, iss_sf, false);
2812     return true;
2813 }
2814 
2815 static bool trans_LD_lit_v(DisasContext *s, arg_ldlit *a)
2816 {
2817     /* Load register (literal), vector version */
2818     TCGv_i64 clean_addr;
2819     MemOp memop;
2820 
2821     if (!fp_access_check(s)) {
2822         return true;
2823     }
2824     memop = finalize_memop_asimd(s, a->sz);
2825     clean_addr = tcg_temp_new_i64();
2826     gen_pc_plus_diff(s, clean_addr, a->imm);
2827     do_fp_ld(s, a->rt, clean_addr, memop);
2828     return true;
2829 }
2830 
2831 static void op_addr_ldstpair_pre(DisasContext *s, arg_ldstpair *a,
2832                                  TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
2833                                  uint64_t offset, bool is_store, MemOp mop)
2834 {
2835     if (a->rn == 31) {
2836         gen_check_sp_alignment(s);
2837     }
2838 
2839     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
2840     if (!a->p) {
2841         tcg_gen_addi_i64(*dirty_addr, *dirty_addr, offset);
2842     }
2843 
2844     *clean_addr = gen_mte_checkN(s, *dirty_addr, is_store,
2845                                  (a->w || a->rn != 31), 2 << a->sz, mop);
2846 }
2847 
2848 static void op_addr_ldstpair_post(DisasContext *s, arg_ldstpair *a,
2849                                   TCGv_i64 dirty_addr, uint64_t offset)
2850 {
2851     if (a->w) {
2852         if (a->p) {
2853             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2854         }
2855         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
2856     }
2857 }
2858 
2859 static bool trans_STP(DisasContext *s, arg_ldstpair *a)
2860 {
2861     uint64_t offset = a->imm << a->sz;
2862     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
2863     MemOp mop = finalize_memop(s, a->sz);
2864 
2865     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, true, mop);
2866     tcg_rt = cpu_reg(s, a->rt);
2867     tcg_rt2 = cpu_reg(s, a->rt2);
2868     /*
2869      * We built mop above for the single logical access -- rebuild it
2870      * now for the paired operation.
2871      *
2872      * With LSE2, non-sign-extending pairs are treated atomically if
2873      * aligned, and if unaligned one of the pair will be completely
2874      * within a 16-byte block and that element will be atomic.
2875      * Otherwise each element is separately atomic.
2876      * In all cases, issue one operation with the correct atomicity.
2877      */
2878     mop = a->sz + 1;
2879     if (s->align_mem) {
2880         mop |= (a->sz == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
2881     }
2882     mop = finalize_memop_pair(s, mop);
2883     if (a->sz == 2) {
2884         TCGv_i64 tmp = tcg_temp_new_i64();
2885 
2886         if (s->be_data == MO_LE) {
2887             tcg_gen_concat32_i64(tmp, tcg_rt, tcg_rt2);
2888         } else {
2889             tcg_gen_concat32_i64(tmp, tcg_rt2, tcg_rt);
2890         }
2891         tcg_gen_qemu_st_i64(tmp, clean_addr, get_mem_index(s), mop);
2892     } else {
2893         TCGv_i128 tmp = tcg_temp_new_i128();
2894 
2895         if (s->be_data == MO_LE) {
2896             tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
2897         } else {
2898             tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
2899         }
2900         tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
2901     }
2902     op_addr_ldstpair_post(s, a, dirty_addr, offset);
2903     return true;
2904 }
2905 
2906 static bool trans_LDP(DisasContext *s, arg_ldstpair *a)
2907 {
2908     uint64_t offset = a->imm << a->sz;
2909     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
2910     MemOp mop = finalize_memop(s, a->sz);
2911 
2912     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, false, mop);
2913     tcg_rt = cpu_reg(s, a->rt);
2914     tcg_rt2 = cpu_reg(s, a->rt2);
2915 
2916     /*
2917      * We built mop above for the single logical access -- rebuild it
2918      * now for the paired operation.
2919      *
2920      * With LSE2, non-sign-extending pairs are treated atomically if
2921      * aligned, and if unaligned one of the pair will be completely
2922      * within a 16-byte block and that element will be atomic.
2923      * Otherwise each element is separately atomic.
2924      * In all cases, issue one operation with the correct atomicity.
2925      *
2926      * This treats sign-extending loads like zero-extending loads,
2927      * since that reuses the most code below.
2928      */
2929     mop = a->sz + 1;
2930     if (s->align_mem) {
2931         mop |= (a->sz == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
2932     }
2933     mop = finalize_memop_pair(s, mop);
2934     if (a->sz == 2) {
2935         int o2 = s->be_data == MO_LE ? 32 : 0;
2936         int o1 = o2 ^ 32;
2937 
2938         tcg_gen_qemu_ld_i64(tcg_rt, clean_addr, get_mem_index(s), mop);
2939         if (a->sign) {
2940             tcg_gen_sextract_i64(tcg_rt2, tcg_rt, o2, 32);
2941             tcg_gen_sextract_i64(tcg_rt, tcg_rt, o1, 32);
2942         } else {
2943             tcg_gen_extract_i64(tcg_rt2, tcg_rt, o2, 32);
2944             tcg_gen_extract_i64(tcg_rt, tcg_rt, o1, 32);
2945         }
2946     } else {
2947         TCGv_i128 tmp = tcg_temp_new_i128();
2948 
2949         tcg_gen_qemu_ld_i128(tmp, clean_addr, get_mem_index(s), mop);
2950         if (s->be_data == MO_LE) {
2951             tcg_gen_extr_i128_i64(tcg_rt, tcg_rt2, tmp);
2952         } else {
2953             tcg_gen_extr_i128_i64(tcg_rt2, tcg_rt, tmp);
2954         }
2955     }
2956     op_addr_ldstpair_post(s, a, dirty_addr, offset);
2957     return true;
2958 }
2959 
2960 static bool trans_STP_v(DisasContext *s, arg_ldstpair *a)
2961 {
2962     uint64_t offset = a->imm << a->sz;
2963     TCGv_i64 clean_addr, dirty_addr;
2964     MemOp mop;
2965 
2966     if (!fp_access_check(s)) {
2967         return true;
2968     }
2969 
2970     /* LSE2 does not merge FP pairs; leave these as separate operations. */
2971     mop = finalize_memop_asimd(s, a->sz);
2972     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, true, mop);
2973     do_fp_st(s, a->rt, clean_addr, mop);
2974     tcg_gen_addi_i64(clean_addr, clean_addr, 1 << a->sz);
2975     do_fp_st(s, a->rt2, clean_addr, mop);
2976     op_addr_ldstpair_post(s, a, dirty_addr, offset);
2977     return true;
2978 }
2979 
2980 static bool trans_LDP_v(DisasContext *s, arg_ldstpair *a)
2981 {
2982     uint64_t offset = a->imm << a->sz;
2983     TCGv_i64 clean_addr, dirty_addr;
2984     MemOp mop;
2985 
2986     if (!fp_access_check(s)) {
2987         return true;
2988     }
2989 
2990     /* LSE2 does not merge FP pairs; leave these as separate operations. */
2991     mop = finalize_memop_asimd(s, a->sz);
2992     op_addr_ldstpair_pre(s, a, &clean_addr, &dirty_addr, offset, false, mop);
2993     do_fp_ld(s, a->rt, clean_addr, mop);
2994     tcg_gen_addi_i64(clean_addr, clean_addr, 1 << a->sz);
2995     do_fp_ld(s, a->rt2, clean_addr, mop);
2996     op_addr_ldstpair_post(s, a, dirty_addr, offset);
2997     return true;
2998 }
2999 
3000 static bool trans_STGP(DisasContext *s, arg_ldstpair *a)
3001 {
3002     TCGv_i64 clean_addr, dirty_addr, tcg_rt, tcg_rt2;
3003     uint64_t offset = a->imm << LOG2_TAG_GRANULE;
3004     MemOp mop;
3005     TCGv_i128 tmp;
3006 
3007     /* STGP only comes in one size. */
3008     tcg_debug_assert(a->sz == MO_64);
3009 
3010     if (!dc_isar_feature(aa64_mte_insn_reg, s)) {
3011         return false;
3012     }
3013 
3014     if (a->rn == 31) {
3015         gen_check_sp_alignment(s);
3016     }
3017 
3018     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3019     if (!a->p) {
3020         tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3021     }
3022 
3023     if (!s->ata) {
3024         /*
3025          * TODO: We could rely on the stores below, at least for
3026          * system mode, if we arrange to add MO_ALIGN_16.
3027          */
3028         gen_helper_stg_stub(cpu_env, dirty_addr);
3029     } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
3030         gen_helper_stg_parallel(cpu_env, dirty_addr, dirty_addr);
3031     } else {
3032         gen_helper_stg(cpu_env, dirty_addr, dirty_addr);
3033     }
3034 
3035     mop = finalize_memop(s, MO_64);
3036     clean_addr = gen_mte_checkN(s, dirty_addr, true, false, 2 << MO_64, mop);
3037 
3038     tcg_rt = cpu_reg(s, a->rt);
3039     tcg_rt2 = cpu_reg(s, a->rt2);
3040 
3041     /*
3042      * STGP is defined as two 8-byte memory operations and one tag operation.
3043      * We implement it as one single 16-byte memory operation for convenience.
3044      * Rebuild mop as for STP.
3045      * TODO: The atomicity with LSE2 is stronger than required.
3046      * Need a form of MO_ATOM_WITHIN16_PAIR that never requires
3047      * 16-byte atomicity.
3048      */
3049     mop = MO_128;
3050     if (s->align_mem) {
3051         mop |= MO_ALIGN_8;
3052     }
3053     mop = finalize_memop_pair(s, mop);
3054 
3055     tmp = tcg_temp_new_i128();
3056     if (s->be_data == MO_LE) {
3057         tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
3058     } else {
3059         tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
3060     }
3061     tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
3062 
3063     op_addr_ldstpair_post(s, a, dirty_addr, offset);
3064     return true;
3065 }
3066 
3067 static void op_addr_ldst_imm_pre(DisasContext *s, arg_ldst_imm *a,
3068                                  TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
3069                                  uint64_t offset, bool is_store, MemOp mop)
3070 {
3071     int memidx;
3072 
3073     if (a->rn == 31) {
3074         gen_check_sp_alignment(s);
3075     }
3076 
3077     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3078     if (!a->p) {
3079         tcg_gen_addi_i64(*dirty_addr, *dirty_addr, offset);
3080     }
3081     memidx = a->unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
3082     *clean_addr = gen_mte_check1_mmuidx(s, *dirty_addr, is_store,
3083                                         a->w || a->rn != 31,
3084                                         mop, a->unpriv, memidx);
3085 }
3086 
3087 static void op_addr_ldst_imm_post(DisasContext *s, arg_ldst_imm *a,
3088                                   TCGv_i64 dirty_addr, uint64_t offset)
3089 {
3090     if (a->w) {
3091         if (a->p) {
3092             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3093         }
3094         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
3095     }
3096 }
3097 
3098 static bool trans_STR_i(DisasContext *s, arg_ldst_imm *a)
3099 {
3100     bool iss_sf, iss_valid = !a->w;
3101     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3102     int memidx = a->unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
3103     MemOp mop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3104 
3105     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, true, mop);
3106 
3107     tcg_rt = cpu_reg(s, a->rt);
3108     iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3109 
3110     do_gpr_st_memidx(s, tcg_rt, clean_addr, mop, memidx,
3111                      iss_valid, a->rt, iss_sf, false);
3112     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3113     return true;
3114 }
3115 
3116 static bool trans_LDR_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 = a->unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
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, false, 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_ld_memidx(s, tcg_rt, clean_addr, mop,
3129                      a->ext, memidx, 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_STR_v_i(DisasContext *s, arg_ldst_imm *a)
3135 {
3136     TCGv_i64 clean_addr, dirty_addr;
3137     MemOp mop;
3138 
3139     if (!fp_access_check(s)) {
3140         return true;
3141     }
3142     mop = finalize_memop_asimd(s, a->sz);
3143     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, true, mop);
3144     do_fp_st(s, a->rt, clean_addr, mop);
3145     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3146     return true;
3147 }
3148 
3149 static bool trans_LDR_v_i(DisasContext *s, arg_ldst_imm *a)
3150 {
3151     TCGv_i64 clean_addr, dirty_addr;
3152     MemOp mop;
3153 
3154     if (!fp_access_check(s)) {
3155         return true;
3156     }
3157     mop = finalize_memop_asimd(s, a->sz);
3158     op_addr_ldst_imm_pre(s, a, &clean_addr, &dirty_addr, a->imm, false, mop);
3159     do_fp_ld(s, a->rt, clean_addr, mop);
3160     op_addr_ldst_imm_post(s, a, dirty_addr, a->imm);
3161     return true;
3162 }
3163 
3164 static void op_addr_ldst_pre(DisasContext *s, arg_ldst *a,
3165                              TCGv_i64 *clean_addr, TCGv_i64 *dirty_addr,
3166                              bool is_store, MemOp memop)
3167 {
3168     TCGv_i64 tcg_rm;
3169 
3170     if (a->rn == 31) {
3171         gen_check_sp_alignment(s);
3172     }
3173     *dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3174 
3175     tcg_rm = read_cpu_reg(s, a->rm, 1);
3176     ext_and_shift_reg(tcg_rm, tcg_rm, a->opt, a->s ? a->sz : 0);
3177 
3178     tcg_gen_add_i64(*dirty_addr, *dirty_addr, tcg_rm);
3179     *clean_addr = gen_mte_check1(s, *dirty_addr, is_store, true, memop);
3180 }
3181 
3182 static bool trans_LDR(DisasContext *s, arg_ldst *a)
3183 {
3184     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3185     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3186     MemOp memop;
3187 
3188     if (extract32(a->opt, 1, 1) == 0) {
3189         return false;
3190     }
3191 
3192     memop = finalize_memop(s, a->sz + a->sign * MO_SIGN);
3193     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, false, memop);
3194     tcg_rt = cpu_reg(s, a->rt);
3195     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3196               a->ext, true, a->rt, iss_sf, false);
3197     return true;
3198 }
3199 
3200 static bool trans_STR(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);
3211     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, true, memop);
3212     tcg_rt = cpu_reg(s, a->rt);
3213     do_gpr_st(s, tcg_rt, clean_addr, memop, true, a->rt, iss_sf, false);
3214     return true;
3215 }
3216 
3217 static bool trans_LDR_v(DisasContext *s, arg_ldst *a)
3218 {
3219     TCGv_i64 clean_addr, dirty_addr;
3220     MemOp memop;
3221 
3222     if (extract32(a->opt, 1, 1) == 0) {
3223         return false;
3224     }
3225 
3226     if (!fp_access_check(s)) {
3227         return true;
3228     }
3229 
3230     memop = finalize_memop_asimd(s, a->sz);
3231     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, false, memop);
3232     do_fp_ld(s, a->rt, clean_addr, memop);
3233     return true;
3234 }
3235 
3236 static bool trans_STR_v(DisasContext *s, arg_ldst *a)
3237 {
3238     TCGv_i64 clean_addr, dirty_addr;
3239     MemOp memop;
3240 
3241     if (extract32(a->opt, 1, 1) == 0) {
3242         return false;
3243     }
3244 
3245     if (!fp_access_check(s)) {
3246         return true;
3247     }
3248 
3249     memop = finalize_memop_asimd(s, a->sz);
3250     op_addr_ldst_pre(s, a, &clean_addr, &dirty_addr, true, memop);
3251     do_fp_st(s, a->rt, clean_addr, memop);
3252     return true;
3253 }
3254 
3255 
3256 static bool do_atomic_ld(DisasContext *s, arg_atomic *a, AtomicThreeOpFn *fn,
3257                          int sign, bool invert)
3258 {
3259     MemOp mop = a->sz | sign;
3260     TCGv_i64 clean_addr, tcg_rs, tcg_rt;
3261 
3262     if (a->rn == 31) {
3263         gen_check_sp_alignment(s);
3264     }
3265     mop = check_atomic_align(s, a->rn, mop);
3266     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn), false,
3267                                 a->rn != 31, mop);
3268     tcg_rs = read_cpu_reg(s, a->rs, true);
3269     tcg_rt = cpu_reg(s, a->rt);
3270     if (invert) {
3271         tcg_gen_not_i64(tcg_rs, tcg_rs);
3272     }
3273     /*
3274      * The tcg atomic primitives are all full barriers.  Therefore we
3275      * can ignore the Acquire and Release bits of this instruction.
3276      */
3277     fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3278 
3279     if (mop & MO_SIGN) {
3280         switch (a->sz) {
3281         case MO_8:
3282             tcg_gen_ext8u_i64(tcg_rt, tcg_rt);
3283             break;
3284         case MO_16:
3285             tcg_gen_ext16u_i64(tcg_rt, tcg_rt);
3286             break;
3287         case MO_32:
3288             tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3289             break;
3290         case MO_64:
3291             break;
3292         default:
3293             g_assert_not_reached();
3294         }
3295     }
3296     return true;
3297 }
3298 
3299 TRANS_FEAT(LDADD, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_add_i64, 0, false)
3300 TRANS_FEAT(LDCLR, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_and_i64, 0, true)
3301 TRANS_FEAT(LDEOR, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_xor_i64, 0, false)
3302 TRANS_FEAT(LDSET, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_or_i64, 0, false)
3303 TRANS_FEAT(LDSMAX, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_smax_i64, MO_SIGN, false)
3304 TRANS_FEAT(LDSMIN, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_smin_i64, MO_SIGN, false)
3305 TRANS_FEAT(LDUMAX, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_umax_i64, 0, false)
3306 TRANS_FEAT(LDUMIN, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_fetch_umin_i64, 0, false)
3307 TRANS_FEAT(SWP, aa64_atomics, do_atomic_ld, a, tcg_gen_atomic_xchg_i64, 0, false)
3308 
3309 static bool trans_LDAPR(DisasContext *s, arg_LDAPR *a)
3310 {
3311     bool iss_sf = ldst_iss_sf(a->sz, false, false);
3312     TCGv_i64 clean_addr;
3313     MemOp mop;
3314 
3315     if (!dc_isar_feature(aa64_atomics, s) ||
3316         !dc_isar_feature(aa64_rcpc_8_3, s)) {
3317         return false;
3318     }
3319     if (a->rn == 31) {
3320         gen_check_sp_alignment(s);
3321     }
3322     mop = check_atomic_align(s, a->rn, a->sz);
3323     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, a->rn), false,
3324                                 a->rn != 31, mop);
3325     /*
3326      * LDAPR* are a special case because they are a simple load, not a
3327      * fetch-and-do-something op.
3328      * The architectural consistency requirements here are weaker than
3329      * full load-acquire (we only need "load-acquire processor consistent"),
3330      * but we choose to implement them as full LDAQ.
3331      */
3332     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, mop, false,
3333               true, a->rt, iss_sf, true);
3334     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3335     return true;
3336 }
3337 
3338 static bool trans_LDRA(DisasContext *s, arg_LDRA *a)
3339 {
3340     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3341     MemOp memop;
3342 
3343     /* Load with pointer authentication */
3344     if (!dc_isar_feature(aa64_pauth, s)) {
3345         return false;
3346     }
3347 
3348     if (a->rn == 31) {
3349         gen_check_sp_alignment(s);
3350     }
3351     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3352 
3353     if (s->pauth_active) {
3354         if (!a->m) {
3355             gen_helper_autda(dirty_addr, cpu_env, dirty_addr,
3356                              tcg_constant_i64(0));
3357         } else {
3358             gen_helper_autdb(dirty_addr, cpu_env, dirty_addr,
3359                              tcg_constant_i64(0));
3360         }
3361     }
3362 
3363     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3364 
3365     memop = finalize_memop(s, MO_64);
3366 
3367     /* Note that "clean" and "dirty" here refer to TBI not PAC.  */
3368     clean_addr = gen_mte_check1(s, dirty_addr, false,
3369                                 a->w || a->rn != 31, memop);
3370 
3371     tcg_rt = cpu_reg(s, a->rt);
3372     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3373               /* extend */ false, /* iss_valid */ !a->w,
3374               /* iss_srt */ a->rt, /* iss_sf */ true, /* iss_ar */ false);
3375 
3376     if (a->w) {
3377         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), dirty_addr);
3378     }
3379     return true;
3380 }
3381 
3382 static bool trans_LDAPR_i(DisasContext *s, arg_ldapr_stlr_i *a)
3383 {
3384     TCGv_i64 clean_addr, dirty_addr;
3385     MemOp mop = a->sz | (a->sign ? MO_SIGN : 0);
3386     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3387 
3388     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3389         return false;
3390     }
3391 
3392     if (a->rn == 31) {
3393         gen_check_sp_alignment(s);
3394     }
3395 
3396     mop = check_ordered_align(s, a->rn, a->imm, false, mop);
3397     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3398     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3399     clean_addr = clean_data_tbi(s, dirty_addr);
3400 
3401     /*
3402      * Load-AcquirePC semantics; we implement as the slightly more
3403      * restrictive Load-Acquire.
3404      */
3405     do_gpr_ld(s, cpu_reg(s, a->rt), clean_addr, mop, a->ext, true,
3406               a->rt, iss_sf, true);
3407     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3408     return true;
3409 }
3410 
3411 static bool trans_STLR_i(DisasContext *s, arg_ldapr_stlr_i *a)
3412 {
3413     TCGv_i64 clean_addr, dirty_addr;
3414     MemOp mop = a->sz;
3415     bool iss_sf = ldst_iss_sf(a->sz, a->sign, a->ext);
3416 
3417     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3418         return false;
3419     }
3420 
3421     /* TODO: ARMv8.4-LSE SCTLR.nAA */
3422 
3423     if (a->rn == 31) {
3424         gen_check_sp_alignment(s);
3425     }
3426 
3427     mop = check_ordered_align(s, a->rn, a->imm, true, mop);
3428     dirty_addr = read_cpu_reg_sp(s, a->rn, 1);
3429     tcg_gen_addi_i64(dirty_addr, dirty_addr, a->imm);
3430     clean_addr = clean_data_tbi(s, dirty_addr);
3431 
3432     /* Store-Release semantics */
3433     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3434     do_gpr_st(s, cpu_reg(s, a->rt), clean_addr, mop, true, a->rt, iss_sf, true);
3435     return true;
3436 }
3437 
3438 static bool trans_LD_mult(DisasContext *s, arg_ldst_mult *a)
3439 {
3440     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3441     MemOp endian, align, mop;
3442 
3443     int total;    /* total bytes */
3444     int elements; /* elements per vector */
3445     int r;
3446     int size = a->sz;
3447 
3448     if (!a->p && a->rm != 0) {
3449         /* For non-postindexed accesses the Rm field must be 0 */
3450         return false;
3451     }
3452     if (size == 3 && !a->q && a->selem != 1) {
3453         return false;
3454     }
3455     if (!fp_access_check(s)) {
3456         return true;
3457     }
3458 
3459     if (a->rn == 31) {
3460         gen_check_sp_alignment(s);
3461     }
3462 
3463     /* For our purposes, bytes are always little-endian.  */
3464     endian = s->be_data;
3465     if (size == 0) {
3466         endian = MO_LE;
3467     }
3468 
3469     total = a->rpt * a->selem * (a->q ? 16 : 8);
3470     tcg_rn = cpu_reg_sp(s, a->rn);
3471 
3472     /*
3473      * Issue the MTE check vs the logical repeat count, before we
3474      * promote consecutive little-endian elements below.
3475      */
3476     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31, total,
3477                                 finalize_memop_asimd(s, size));
3478 
3479     /*
3480      * Consecutive little-endian elements from a single register
3481      * can be promoted to a larger little-endian operation.
3482      */
3483     align = MO_ALIGN;
3484     if (a->selem == 1 && endian == MO_LE) {
3485         align = pow2_align(size);
3486         size = 3;
3487     }
3488     if (!s->align_mem) {
3489         align = 0;
3490     }
3491     mop = endian | size | align;
3492 
3493     elements = (a->q ? 16 : 8) >> size;
3494     tcg_ebytes = tcg_constant_i64(1 << size);
3495     for (r = 0; r < a->rpt; r++) {
3496         int e;
3497         for (e = 0; e < elements; e++) {
3498             int xs;
3499             for (xs = 0; xs < a->selem; xs++) {
3500                 int tt = (a->rt + r + xs) % 32;
3501                 do_vec_ld(s, tt, e, clean_addr, mop);
3502                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3503             }
3504         }
3505     }
3506 
3507     /*
3508      * For non-quad operations, setting a slice of the low 64 bits of
3509      * the register clears the high 64 bits (in the ARM ARM pseudocode
3510      * this is implicit in the fact that 'rval' is a 64 bit wide
3511      * variable).  For quad operations, we might still need to zero
3512      * the high bits of SVE.
3513      */
3514     for (r = 0; r < a->rpt * a->selem; r++) {
3515         int tt = (a->rt + r) % 32;
3516         clear_vec_high(s, a->q, tt);
3517     }
3518 
3519     if (a->p) {
3520         if (a->rm == 31) {
3521             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3522         } else {
3523             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3524         }
3525     }
3526     return true;
3527 }
3528 
3529 static bool trans_ST_mult(DisasContext *s, arg_ldst_mult *a)
3530 {
3531     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3532     MemOp endian, align, mop;
3533 
3534     int total;    /* total bytes */
3535     int elements; /* elements per vector */
3536     int r;
3537     int size = a->sz;
3538 
3539     if (!a->p && a->rm != 0) {
3540         /* For non-postindexed accesses the Rm field must be 0 */
3541         return false;
3542     }
3543     if (size == 3 && !a->q && a->selem != 1) {
3544         return false;
3545     }
3546     if (!fp_access_check(s)) {
3547         return true;
3548     }
3549 
3550     if (a->rn == 31) {
3551         gen_check_sp_alignment(s);
3552     }
3553 
3554     /* For our purposes, bytes are always little-endian.  */
3555     endian = s->be_data;
3556     if (size == 0) {
3557         endian = MO_LE;
3558     }
3559 
3560     total = a->rpt * a->selem * (a->q ? 16 : 8);
3561     tcg_rn = cpu_reg_sp(s, a->rn);
3562 
3563     /*
3564      * Issue the MTE check vs the logical repeat count, before we
3565      * promote consecutive little-endian elements below.
3566      */
3567     clean_addr = gen_mte_checkN(s, tcg_rn, true, a->p || a->rn != 31, total,
3568                                 finalize_memop_asimd(s, size));
3569 
3570     /*
3571      * Consecutive little-endian elements from a single register
3572      * can be promoted to a larger little-endian operation.
3573      */
3574     align = MO_ALIGN;
3575     if (a->selem == 1 && endian == MO_LE) {
3576         align = pow2_align(size);
3577         size = 3;
3578     }
3579     if (!s->align_mem) {
3580         align = 0;
3581     }
3582     mop = endian | size | align;
3583 
3584     elements = (a->q ? 16 : 8) >> size;
3585     tcg_ebytes = tcg_constant_i64(1 << size);
3586     for (r = 0; r < a->rpt; r++) {
3587         int e;
3588         for (e = 0; e < elements; e++) {
3589             int xs;
3590             for (xs = 0; xs < a->selem; xs++) {
3591                 int tt = (a->rt + r + xs) % 32;
3592                 do_vec_st(s, tt, e, clean_addr, mop);
3593                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3594             }
3595         }
3596     }
3597 
3598     if (a->p) {
3599         if (a->rm == 31) {
3600             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3601         } else {
3602             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3603         }
3604     }
3605     return true;
3606 }
3607 
3608 static bool trans_ST_single(DisasContext *s, arg_ldst_single *a)
3609 {
3610     int xs, total, rt;
3611     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3612     MemOp mop;
3613 
3614     if (!a->p && a->rm != 0) {
3615         return false;
3616     }
3617     if (!fp_access_check(s)) {
3618         return true;
3619     }
3620 
3621     if (a->rn == 31) {
3622         gen_check_sp_alignment(s);
3623     }
3624 
3625     total = a->selem << a->scale;
3626     tcg_rn = cpu_reg_sp(s, a->rn);
3627 
3628     mop = finalize_memop_asimd(s, a->scale);
3629     clean_addr = gen_mte_checkN(s, tcg_rn, true, a->p || a->rn != 31,
3630                                 total, mop);
3631 
3632     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3633     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3634         do_vec_st(s, rt, a->index, clean_addr, mop);
3635         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3636     }
3637 
3638     if (a->p) {
3639         if (a->rm == 31) {
3640             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3641         } else {
3642             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3643         }
3644     }
3645     return true;
3646 }
3647 
3648 static bool trans_LD_single(DisasContext *s, arg_ldst_single *a)
3649 {
3650     int xs, total, rt;
3651     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3652     MemOp mop;
3653 
3654     if (!a->p && a->rm != 0) {
3655         return false;
3656     }
3657     if (!fp_access_check(s)) {
3658         return true;
3659     }
3660 
3661     if (a->rn == 31) {
3662         gen_check_sp_alignment(s);
3663     }
3664 
3665     total = a->selem << a->scale;
3666     tcg_rn = cpu_reg_sp(s, a->rn);
3667 
3668     mop = finalize_memop_asimd(s, a->scale);
3669     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31,
3670                                 total, mop);
3671 
3672     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3673     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3674         do_vec_ld(s, rt, a->index, clean_addr, mop);
3675         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3676     }
3677 
3678     if (a->p) {
3679         if (a->rm == 31) {
3680             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3681         } else {
3682             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3683         }
3684     }
3685     return true;
3686 }
3687 
3688 static bool trans_LD_single_repl(DisasContext *s, arg_LD_single_repl *a)
3689 {
3690     int xs, total, rt;
3691     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3692     MemOp mop;
3693 
3694     if (!a->p && a->rm != 0) {
3695         return false;
3696     }
3697     if (!fp_access_check(s)) {
3698         return true;
3699     }
3700 
3701     if (a->rn == 31) {
3702         gen_check_sp_alignment(s);
3703     }
3704 
3705     total = a->selem << a->scale;
3706     tcg_rn = cpu_reg_sp(s, a->rn);
3707 
3708     mop = finalize_memop_asimd(s, a->scale);
3709     clean_addr = gen_mte_checkN(s, tcg_rn, false, a->p || a->rn != 31,
3710                                 total, mop);
3711 
3712     tcg_ebytes = tcg_constant_i64(1 << a->scale);
3713     for (xs = 0, rt = a->rt; xs < a->selem; xs++, rt = (rt + 1) % 32) {
3714         /* Load and replicate to all elements */
3715         TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3716 
3717         tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
3718         tcg_gen_gvec_dup_i64(a->scale, vec_full_reg_offset(s, rt),
3719                              (a->q + 1) * 8, vec_full_reg_size(s), tcg_tmp);
3720         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3721     }
3722 
3723     if (a->p) {
3724         if (a->rm == 31) {
3725             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3726         } else {
3727             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, a->rm));
3728         }
3729     }
3730     return true;
3731 }
3732 
3733 static bool trans_STZGM(DisasContext *s, arg_ldst_tag *a)
3734 {
3735     TCGv_i64 addr, clean_addr, tcg_rt;
3736     int size = 4 << s->dcz_blocksize;
3737 
3738     if (!dc_isar_feature(aa64_mte, s)) {
3739         return false;
3740     }
3741     if (s->current_el == 0) {
3742         return false;
3743     }
3744 
3745     if (a->rn == 31) {
3746         gen_check_sp_alignment(s);
3747     }
3748 
3749     addr = read_cpu_reg_sp(s, a->rn, true);
3750     tcg_gen_addi_i64(addr, addr, a->imm);
3751     tcg_rt = cpu_reg(s, a->rt);
3752 
3753     if (s->ata) {
3754         gen_helper_stzgm_tags(cpu_env, addr, tcg_rt);
3755     }
3756     /*
3757      * The non-tags portion of STZGM is mostly like DC_ZVA,
3758      * except the alignment happens before the access.
3759      */
3760     clean_addr = clean_data_tbi(s, addr);
3761     tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3762     gen_helper_dc_zva(cpu_env, clean_addr);
3763     return true;
3764 }
3765 
3766 static bool trans_STGM(DisasContext *s, arg_ldst_tag *a)
3767 {
3768     TCGv_i64 addr, clean_addr, tcg_rt;
3769 
3770     if (!dc_isar_feature(aa64_mte, s)) {
3771         return false;
3772     }
3773     if (s->current_el == 0) {
3774         return false;
3775     }
3776 
3777     if (a->rn == 31) {
3778         gen_check_sp_alignment(s);
3779     }
3780 
3781     addr = read_cpu_reg_sp(s, a->rn, true);
3782     tcg_gen_addi_i64(addr, addr, a->imm);
3783     tcg_rt = cpu_reg(s, a->rt);
3784 
3785     if (s->ata) {
3786         gen_helper_stgm(cpu_env, addr, tcg_rt);
3787     } else {
3788         MMUAccessType acc = MMU_DATA_STORE;
3789         int size = 4 << s->gm_blocksize;
3790 
3791         clean_addr = clean_data_tbi(s, addr);
3792         tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3793         gen_probe_access(s, clean_addr, acc, size);
3794     }
3795     return true;
3796 }
3797 
3798 static bool trans_LDGM(DisasContext *s, arg_ldst_tag *a)
3799 {
3800     TCGv_i64 addr, clean_addr, tcg_rt;
3801 
3802     if (!dc_isar_feature(aa64_mte, s)) {
3803         return false;
3804     }
3805     if (s->current_el == 0) {
3806         return false;
3807     }
3808 
3809     if (a->rn == 31) {
3810         gen_check_sp_alignment(s);
3811     }
3812 
3813     addr = read_cpu_reg_sp(s, a->rn, true);
3814     tcg_gen_addi_i64(addr, addr, a->imm);
3815     tcg_rt = cpu_reg(s, a->rt);
3816 
3817     if (s->ata) {
3818         gen_helper_ldgm(tcg_rt, cpu_env, addr);
3819     } else {
3820         MMUAccessType acc = MMU_DATA_LOAD;
3821         int size = 4 << s->gm_blocksize;
3822 
3823         clean_addr = clean_data_tbi(s, addr);
3824         tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3825         gen_probe_access(s, clean_addr, acc, size);
3826         /* The result tags are zeros.  */
3827         tcg_gen_movi_i64(tcg_rt, 0);
3828     }
3829     return true;
3830 }
3831 
3832 static bool trans_LDG(DisasContext *s, arg_ldst_tag *a)
3833 {
3834     TCGv_i64 addr, clean_addr, tcg_rt;
3835 
3836     if (!dc_isar_feature(aa64_mte_insn_reg, s)) {
3837         return false;
3838     }
3839 
3840     if (a->rn == 31) {
3841         gen_check_sp_alignment(s);
3842     }
3843 
3844     addr = read_cpu_reg_sp(s, a->rn, true);
3845     if (!a->p) {
3846         /* pre-index or signed offset */
3847         tcg_gen_addi_i64(addr, addr, a->imm);
3848     }
3849 
3850     tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
3851     tcg_rt = cpu_reg(s, a->rt);
3852     if (s->ata) {
3853         gen_helper_ldg(tcg_rt, cpu_env, addr, tcg_rt);
3854     } else {
3855         /*
3856          * Tag access disabled: we must check for aborts on the load
3857          * load from [rn+offset], and then insert a 0 tag into rt.
3858          */
3859         clean_addr = clean_data_tbi(s, addr);
3860         gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
3861         gen_address_with_allocation_tag0(tcg_rt, tcg_rt);
3862     }
3863 
3864     if (a->w) {
3865         /* pre-index or post-index */
3866         if (a->p) {
3867             /* post-index */
3868             tcg_gen_addi_i64(addr, addr, a->imm);
3869         }
3870         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), addr);
3871     }
3872     return true;
3873 }
3874 
3875 static bool do_STG(DisasContext *s, arg_ldst_tag *a, bool is_zero, bool is_pair)
3876 {
3877     TCGv_i64 addr, tcg_rt;
3878 
3879     if (a->rn == 31) {
3880         gen_check_sp_alignment(s);
3881     }
3882 
3883     addr = read_cpu_reg_sp(s, a->rn, true);
3884     if (!a->p) {
3885         /* pre-index or signed offset */
3886         tcg_gen_addi_i64(addr, addr, a->imm);
3887     }
3888     tcg_rt = cpu_reg_sp(s, a->rt);
3889     if (!s->ata) {
3890         /*
3891          * For STG and ST2G, we need to check alignment and probe memory.
3892          * TODO: For STZG and STZ2G, we could rely on the stores below,
3893          * at least for system mode; user-only won't enforce alignment.
3894          */
3895         if (is_pair) {
3896             gen_helper_st2g_stub(cpu_env, addr);
3897         } else {
3898             gen_helper_stg_stub(cpu_env, addr);
3899         }
3900     } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
3901         if (is_pair) {
3902             gen_helper_st2g_parallel(cpu_env, addr, tcg_rt);
3903         } else {
3904             gen_helper_stg_parallel(cpu_env, addr, tcg_rt);
3905         }
3906     } else {
3907         if (is_pair) {
3908             gen_helper_st2g(cpu_env, addr, tcg_rt);
3909         } else {
3910             gen_helper_stg(cpu_env, addr, tcg_rt);
3911         }
3912     }
3913 
3914     if (is_zero) {
3915         TCGv_i64 clean_addr = clean_data_tbi(s, addr);
3916         TCGv_i64 zero64 = tcg_constant_i64(0);
3917         TCGv_i128 zero128 = tcg_temp_new_i128();
3918         int mem_index = get_mem_index(s);
3919         MemOp mop = finalize_memop(s, MO_128 | MO_ALIGN);
3920 
3921         tcg_gen_concat_i64_i128(zero128, zero64, zero64);
3922 
3923         /* This is 1 or 2 atomic 16-byte operations. */
3924         tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
3925         if (is_pair) {
3926             tcg_gen_addi_i64(clean_addr, clean_addr, 16);
3927             tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
3928         }
3929     }
3930 
3931     if (a->w) {
3932         /* pre-index or post-index */
3933         if (a->p) {
3934             /* post-index */
3935             tcg_gen_addi_i64(addr, addr, a->imm);
3936         }
3937         tcg_gen_mov_i64(cpu_reg_sp(s, a->rn), addr);
3938     }
3939     return true;
3940 }
3941 
3942 TRANS_FEAT(STG, aa64_mte_insn_reg, do_STG, a, false, false)
3943 TRANS_FEAT(STZG, aa64_mte_insn_reg, do_STG, a, true, false)
3944 TRANS_FEAT(ST2G, aa64_mte_insn_reg, do_STG, a, false, true)
3945 TRANS_FEAT(STZ2G, aa64_mte_insn_reg, do_STG, a, true, true)
3946 
3947 typedef void ArithTwoOp(TCGv_i64, TCGv_i64, TCGv_i64);
3948 
3949 static bool gen_rri(DisasContext *s, arg_rri_sf *a,
3950                     bool rd_sp, bool rn_sp, ArithTwoOp *fn)
3951 {
3952     TCGv_i64 tcg_rn = rn_sp ? cpu_reg_sp(s, a->rn) : cpu_reg(s, a->rn);
3953     TCGv_i64 tcg_rd = rd_sp ? cpu_reg_sp(s, a->rd) : cpu_reg(s, a->rd);
3954     TCGv_i64 tcg_imm = tcg_constant_i64(a->imm);
3955 
3956     fn(tcg_rd, tcg_rn, tcg_imm);
3957     if (!a->sf) {
3958         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3959     }
3960     return true;
3961 }
3962 
3963 /*
3964  * PC-rel. addressing
3965  */
3966 
3967 static bool trans_ADR(DisasContext *s, arg_ri *a)
3968 {
3969     gen_pc_plus_diff(s, cpu_reg(s, a->rd), a->imm);
3970     return true;
3971 }
3972 
3973 static bool trans_ADRP(DisasContext *s, arg_ri *a)
3974 {
3975     int64_t offset = (int64_t)a->imm << 12;
3976 
3977     /* The page offset is ok for CF_PCREL. */
3978     offset -= s->pc_curr & 0xfff;
3979     gen_pc_plus_diff(s, cpu_reg(s, a->rd), offset);
3980     return true;
3981 }
3982 
3983 /*
3984  * Add/subtract (immediate)
3985  */
3986 TRANS(ADD_i, gen_rri, a, 1, 1, tcg_gen_add_i64)
3987 TRANS(SUB_i, gen_rri, a, 1, 1, tcg_gen_sub_i64)
3988 TRANS(ADDS_i, gen_rri, a, 0, 1, a->sf ? gen_add64_CC : gen_add32_CC)
3989 TRANS(SUBS_i, gen_rri, a, 0, 1, a->sf ? gen_sub64_CC : gen_sub32_CC)
3990 
3991 /*
3992  * Add/subtract (immediate, with tags)
3993  */
3994 
3995 static bool gen_add_sub_imm_with_tags(DisasContext *s, arg_rri_tag *a,
3996                                       bool sub_op)
3997 {
3998     TCGv_i64 tcg_rn, tcg_rd;
3999     int imm;
4000 
4001     imm = a->uimm6 << LOG2_TAG_GRANULE;
4002     if (sub_op) {
4003         imm = -imm;
4004     }
4005 
4006     tcg_rn = cpu_reg_sp(s, a->rn);
4007     tcg_rd = cpu_reg_sp(s, a->rd);
4008 
4009     if (s->ata) {
4010         gen_helper_addsubg(tcg_rd, cpu_env, tcg_rn,
4011                            tcg_constant_i32(imm),
4012                            tcg_constant_i32(a->uimm4));
4013     } else {
4014         tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4015         gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4016     }
4017     return true;
4018 }
4019 
4020 TRANS_FEAT(ADDG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, false)
4021 TRANS_FEAT(SUBG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, true)
4022 
4023 /* The input should be a value in the bottom e bits (with higher
4024  * bits zero); returns that value replicated into every element
4025  * of size e in a 64 bit integer.
4026  */
4027 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4028 {
4029     assert(e != 0);
4030     while (e < 64) {
4031         mask |= mask << e;
4032         e *= 2;
4033     }
4034     return mask;
4035 }
4036 
4037 /*
4038  * Logical (immediate)
4039  */
4040 
4041 /*
4042  * Simplified variant of pseudocode DecodeBitMasks() for the case where we
4043  * only require the wmask. Returns false if the imms/immr/immn are a reserved
4044  * value (ie should cause a guest UNDEF exception), and true if they are
4045  * valid, in which case the decoded bit pattern is written to result.
4046  */
4047 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4048                             unsigned int imms, unsigned int immr)
4049 {
4050     uint64_t mask;
4051     unsigned e, levels, s, r;
4052     int len;
4053 
4054     assert(immn < 2 && imms < 64 && immr < 64);
4055 
4056     /* The bit patterns we create here are 64 bit patterns which
4057      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4058      * 64 bits each. Each element contains the same value: a run
4059      * of between 1 and e-1 non-zero bits, rotated within the
4060      * element by between 0 and e-1 bits.
4061      *
4062      * The element size and run length are encoded into immn (1 bit)
4063      * and imms (6 bits) as follows:
4064      * 64 bit elements: immn = 1, imms = <length of run - 1>
4065      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4066      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4067      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4068      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4069      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4070      * Notice that immn = 0, imms = 11111x is the only combination
4071      * not covered by one of the above options; this is reserved.
4072      * Further, <length of run - 1> all-ones is a reserved pattern.
4073      *
4074      * In all cases the rotation is by immr % e (and immr is 6 bits).
4075      */
4076 
4077     /* First determine the element size */
4078     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4079     if (len < 1) {
4080         /* This is the immn == 0, imms == 0x11111x case */
4081         return false;
4082     }
4083     e = 1 << len;
4084 
4085     levels = e - 1;
4086     s = imms & levels;
4087     r = immr & levels;
4088 
4089     if (s == levels) {
4090         /* <length of run - 1> mustn't be all-ones. */
4091         return false;
4092     }
4093 
4094     /* Create the value of one element: s+1 set bits rotated
4095      * by r within the element (which is e bits wide)...
4096      */
4097     mask = MAKE_64BIT_MASK(0, s + 1);
4098     if (r) {
4099         mask = (mask >> r) | (mask << (e - r));
4100         mask &= MAKE_64BIT_MASK(0, e);
4101     }
4102     /* ...then replicate the element over the whole 64 bit value */
4103     mask = bitfield_replicate(mask, e);
4104     *result = mask;
4105     return true;
4106 }
4107 
4108 static bool gen_rri_log(DisasContext *s, arg_rri_log *a, bool set_cc,
4109                         void (*fn)(TCGv_i64, TCGv_i64, int64_t))
4110 {
4111     TCGv_i64 tcg_rd, tcg_rn;
4112     uint64_t imm;
4113 
4114     /* Some immediate field values are reserved. */
4115     if (!logic_imm_decode_wmask(&imm, extract32(a->dbm, 12, 1),
4116                                 extract32(a->dbm, 0, 6),
4117                                 extract32(a->dbm, 6, 6))) {
4118         return false;
4119     }
4120     if (!a->sf) {
4121         imm &= 0xffffffffull;
4122     }
4123 
4124     tcg_rd = set_cc ? cpu_reg(s, a->rd) : cpu_reg_sp(s, a->rd);
4125     tcg_rn = cpu_reg(s, a->rn);
4126 
4127     fn(tcg_rd, tcg_rn, imm);
4128     if (set_cc) {
4129         gen_logic_CC(a->sf, tcg_rd);
4130     }
4131     if (!a->sf) {
4132         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4133     }
4134     return true;
4135 }
4136 
4137 TRANS(AND_i, gen_rri_log, a, false, tcg_gen_andi_i64)
4138 TRANS(ORR_i, gen_rri_log, a, false, tcg_gen_ori_i64)
4139 TRANS(EOR_i, gen_rri_log, a, false, tcg_gen_xori_i64)
4140 TRANS(ANDS_i, gen_rri_log, a, true, tcg_gen_andi_i64)
4141 
4142 /*
4143  * Move wide (immediate)
4144  */
4145 
4146 static bool trans_MOVZ(DisasContext *s, arg_movw *a)
4147 {
4148     int pos = a->hw << 4;
4149     tcg_gen_movi_i64(cpu_reg(s, a->rd), (uint64_t)a->imm << pos);
4150     return true;
4151 }
4152 
4153 static bool trans_MOVN(DisasContext *s, arg_movw *a)
4154 {
4155     int pos = a->hw << 4;
4156     uint64_t imm = a->imm;
4157 
4158     imm = ~(imm << pos);
4159     if (!a->sf) {
4160         imm = (uint32_t)imm;
4161     }
4162     tcg_gen_movi_i64(cpu_reg(s, a->rd), imm);
4163     return true;
4164 }
4165 
4166 static bool trans_MOVK(DisasContext *s, arg_movw *a)
4167 {
4168     int pos = a->hw << 4;
4169     TCGv_i64 tcg_rd, tcg_im;
4170 
4171     tcg_rd = cpu_reg(s, a->rd);
4172     tcg_im = tcg_constant_i64(a->imm);
4173     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_im, pos, 16);
4174     if (!a->sf) {
4175         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4176     }
4177     return true;
4178 }
4179 
4180 /*
4181  * Bitfield
4182  */
4183 
4184 static bool trans_SBFM(DisasContext *s, arg_SBFM *a)
4185 {
4186     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4187     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4188     unsigned int bitsize = a->sf ? 64 : 32;
4189     unsigned int ri = a->immr;
4190     unsigned int si = a->imms;
4191     unsigned int pos, len;
4192 
4193     if (si >= ri) {
4194         /* Wd<s-r:0> = Wn<s:r> */
4195         len = (si - ri) + 1;
4196         tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4197         if (!a->sf) {
4198             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4199         }
4200     } else {
4201         /* Wd<32+s-r,32-r> = Wn<s:0> */
4202         len = si + 1;
4203         pos = (bitsize - ri) & (bitsize - 1);
4204 
4205         if (len < ri) {
4206             /*
4207              * Sign extend the destination field from len to fill the
4208              * balance of the word.  Let the deposit below insert all
4209              * of those sign bits.
4210              */
4211             tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4212             len = ri;
4213         }
4214 
4215         /*
4216          * We start with zero, and we haven't modified any bits outside
4217          * bitsize, therefore no final zero-extension is unneeded for !sf.
4218          */
4219         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4220     }
4221     return true;
4222 }
4223 
4224 static bool trans_UBFM(DisasContext *s, arg_UBFM *a)
4225 {
4226     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4227     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4228     unsigned int bitsize = a->sf ? 64 : 32;
4229     unsigned int ri = a->immr;
4230     unsigned int si = a->imms;
4231     unsigned int pos, len;
4232 
4233     tcg_rd = cpu_reg(s, a->rd);
4234     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4235 
4236     if (si >= ri) {
4237         /* Wd<s-r:0> = Wn<s:r> */
4238         len = (si - ri) + 1;
4239         tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4240     } else {
4241         /* Wd<32+s-r,32-r> = Wn<s:0> */
4242         len = si + 1;
4243         pos = (bitsize - ri) & (bitsize - 1);
4244         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4245     }
4246     return true;
4247 }
4248 
4249 static bool trans_BFM(DisasContext *s, arg_BFM *a)
4250 {
4251     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4252     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4253     unsigned int bitsize = a->sf ? 64 : 32;
4254     unsigned int ri = a->immr;
4255     unsigned int si = a->imms;
4256     unsigned int pos, len;
4257 
4258     tcg_rd = cpu_reg(s, a->rd);
4259     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4260 
4261     if (si >= ri) {
4262         /* Wd<s-r:0> = Wn<s:r> */
4263         tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4264         len = (si - ri) + 1;
4265         pos = 0;
4266     } else {
4267         /* Wd<32+s-r,32-r> = Wn<s:0> */
4268         len = si + 1;
4269         pos = (bitsize - ri) & (bitsize - 1);
4270     }
4271 
4272     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4273     if (!a->sf) {
4274         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4275     }
4276     return true;
4277 }
4278 
4279 static bool trans_EXTR(DisasContext *s, arg_extract *a)
4280 {
4281     TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4282 
4283     tcg_rd = cpu_reg(s, a->rd);
4284 
4285     if (unlikely(a->imm == 0)) {
4286         /*
4287          * tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4288          * so an extract from bit 0 is a special case.
4289          */
4290         if (a->sf) {
4291             tcg_gen_mov_i64(tcg_rd, cpu_reg(s, a->rm));
4292         } else {
4293             tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, a->rm));
4294         }
4295     } else {
4296         tcg_rm = cpu_reg(s, a->rm);
4297         tcg_rn = cpu_reg(s, a->rn);
4298 
4299         if (a->sf) {
4300             /* Specialization to ROR happens in EXTRACT2.  */
4301             tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, a->imm);
4302         } else {
4303             TCGv_i32 t0 = tcg_temp_new_i32();
4304 
4305             tcg_gen_extrl_i64_i32(t0, tcg_rm);
4306             if (a->rm == a->rn) {
4307                 tcg_gen_rotri_i32(t0, t0, a->imm);
4308             } else {
4309                 TCGv_i32 t1 = tcg_temp_new_i32();
4310                 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4311                 tcg_gen_extract2_i32(t0, t0, t1, a->imm);
4312             }
4313             tcg_gen_extu_i32_i64(tcg_rd, t0);
4314         }
4315     }
4316     return true;
4317 }
4318 
4319 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4320  * Note that it is the caller's responsibility to ensure that the
4321  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4322  * mandated semantics for out of range shifts.
4323  */
4324 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4325                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4326 {
4327     switch (shift_type) {
4328     case A64_SHIFT_TYPE_LSL:
4329         tcg_gen_shl_i64(dst, src, shift_amount);
4330         break;
4331     case A64_SHIFT_TYPE_LSR:
4332         tcg_gen_shr_i64(dst, src, shift_amount);
4333         break;
4334     case A64_SHIFT_TYPE_ASR:
4335         if (!sf) {
4336             tcg_gen_ext32s_i64(dst, src);
4337         }
4338         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4339         break;
4340     case A64_SHIFT_TYPE_ROR:
4341         if (sf) {
4342             tcg_gen_rotr_i64(dst, src, shift_amount);
4343         } else {
4344             TCGv_i32 t0, t1;
4345             t0 = tcg_temp_new_i32();
4346             t1 = tcg_temp_new_i32();
4347             tcg_gen_extrl_i64_i32(t0, src);
4348             tcg_gen_extrl_i64_i32(t1, shift_amount);
4349             tcg_gen_rotr_i32(t0, t0, t1);
4350             tcg_gen_extu_i32_i64(dst, t0);
4351         }
4352         break;
4353     default:
4354         assert(FALSE); /* all shift types should be handled */
4355         break;
4356     }
4357 
4358     if (!sf) { /* zero extend final result */
4359         tcg_gen_ext32u_i64(dst, dst);
4360     }
4361 }
4362 
4363 /* Shift a TCGv src by immediate, put result in dst.
4364  * The shift amount must be in range (this should always be true as the
4365  * relevant instructions will UNDEF on bad shift immediates).
4366  */
4367 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4368                           enum a64_shift_type shift_type, unsigned int shift_i)
4369 {
4370     assert(shift_i < (sf ? 64 : 32));
4371 
4372     if (shift_i == 0) {
4373         tcg_gen_mov_i64(dst, src);
4374     } else {
4375         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
4376     }
4377 }
4378 
4379 /* Logical (shifted register)
4380  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
4381  * +----+-----+-----------+-------+---+------+--------+------+------+
4382  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
4383  * +----+-----+-----------+-------+---+------+--------+------+------+
4384  */
4385 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4386 {
4387     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4388     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4389 
4390     sf = extract32(insn, 31, 1);
4391     opc = extract32(insn, 29, 2);
4392     shift_type = extract32(insn, 22, 2);
4393     invert = extract32(insn, 21, 1);
4394     rm = extract32(insn, 16, 5);
4395     shift_amount = extract32(insn, 10, 6);
4396     rn = extract32(insn, 5, 5);
4397     rd = extract32(insn, 0, 5);
4398 
4399     if (!sf && (shift_amount & (1 << 5))) {
4400         unallocated_encoding(s);
4401         return;
4402     }
4403 
4404     tcg_rd = cpu_reg(s, rd);
4405 
4406     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4407         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4408          * register-register MOV and MVN, so it is worth special casing.
4409          */
4410         tcg_rm = cpu_reg(s, rm);
4411         if (invert) {
4412             tcg_gen_not_i64(tcg_rd, tcg_rm);
4413             if (!sf) {
4414                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4415             }
4416         } else {
4417             if (sf) {
4418                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4419             } else {
4420                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4421             }
4422         }
4423         return;
4424     }
4425 
4426     tcg_rm = read_cpu_reg(s, rm, sf);
4427 
4428     if (shift_amount) {
4429         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4430     }
4431 
4432     tcg_rn = cpu_reg(s, rn);
4433 
4434     switch (opc | (invert << 2)) {
4435     case 0: /* AND */
4436     case 3: /* ANDS */
4437         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4438         break;
4439     case 1: /* ORR */
4440         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4441         break;
4442     case 2: /* EOR */
4443         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4444         break;
4445     case 4: /* BIC */
4446     case 7: /* BICS */
4447         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4448         break;
4449     case 5: /* ORN */
4450         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4451         break;
4452     case 6: /* EON */
4453         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4454         break;
4455     default:
4456         assert(FALSE);
4457         break;
4458     }
4459 
4460     if (!sf) {
4461         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4462     }
4463 
4464     if (opc == 3) {
4465         gen_logic_CC(sf, tcg_rd);
4466     }
4467 }
4468 
4469 /*
4470  * Add/subtract (extended register)
4471  *
4472  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
4473  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4474  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
4475  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4476  *
4477  *  sf: 0 -> 32bit, 1 -> 64bit
4478  *  op: 0 -> add  , 1 -> sub
4479  *   S: 1 -> set flags
4480  * opt: 00
4481  * option: extension type (see DecodeRegExtend)
4482  * imm3: optional shift to Rm
4483  *
4484  * Rd = Rn + LSL(extend(Rm), amount)
4485  */
4486 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4487 {
4488     int rd = extract32(insn, 0, 5);
4489     int rn = extract32(insn, 5, 5);
4490     int imm3 = extract32(insn, 10, 3);
4491     int option = extract32(insn, 13, 3);
4492     int rm = extract32(insn, 16, 5);
4493     int opt = extract32(insn, 22, 2);
4494     bool setflags = extract32(insn, 29, 1);
4495     bool sub_op = extract32(insn, 30, 1);
4496     bool sf = extract32(insn, 31, 1);
4497 
4498     TCGv_i64 tcg_rm, tcg_rn; /* temps */
4499     TCGv_i64 tcg_rd;
4500     TCGv_i64 tcg_result;
4501 
4502     if (imm3 > 4 || opt != 0) {
4503         unallocated_encoding(s);
4504         return;
4505     }
4506 
4507     /* non-flag setting ops may use SP */
4508     if (!setflags) {
4509         tcg_rd = cpu_reg_sp(s, rd);
4510     } else {
4511         tcg_rd = cpu_reg(s, rd);
4512     }
4513     tcg_rn = read_cpu_reg_sp(s, rn, sf);
4514 
4515     tcg_rm = read_cpu_reg(s, rm, sf);
4516     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4517 
4518     tcg_result = tcg_temp_new_i64();
4519 
4520     if (!setflags) {
4521         if (sub_op) {
4522             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4523         } else {
4524             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4525         }
4526     } else {
4527         if (sub_op) {
4528             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4529         } else {
4530             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4531         }
4532     }
4533 
4534     if (sf) {
4535         tcg_gen_mov_i64(tcg_rd, tcg_result);
4536     } else {
4537         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4538     }
4539 }
4540 
4541 /*
4542  * Add/subtract (shifted register)
4543  *
4544  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
4545  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4546  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
4547  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4548  *
4549  *    sf: 0 -> 32bit, 1 -> 64bit
4550  *    op: 0 -> add  , 1 -> sub
4551  *     S: 1 -> set flags
4552  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4553  *  imm6: Shift amount to apply to Rm before the add/sub
4554  */
4555 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4556 {
4557     int rd = extract32(insn, 0, 5);
4558     int rn = extract32(insn, 5, 5);
4559     int imm6 = extract32(insn, 10, 6);
4560     int rm = extract32(insn, 16, 5);
4561     int shift_type = extract32(insn, 22, 2);
4562     bool setflags = extract32(insn, 29, 1);
4563     bool sub_op = extract32(insn, 30, 1);
4564     bool sf = extract32(insn, 31, 1);
4565 
4566     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4567     TCGv_i64 tcg_rn, tcg_rm;
4568     TCGv_i64 tcg_result;
4569 
4570     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4571         unallocated_encoding(s);
4572         return;
4573     }
4574 
4575     tcg_rn = read_cpu_reg(s, rn, sf);
4576     tcg_rm = read_cpu_reg(s, rm, sf);
4577 
4578     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4579 
4580     tcg_result = tcg_temp_new_i64();
4581 
4582     if (!setflags) {
4583         if (sub_op) {
4584             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4585         } else {
4586             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4587         }
4588     } else {
4589         if (sub_op) {
4590             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4591         } else {
4592             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4593         }
4594     }
4595 
4596     if (sf) {
4597         tcg_gen_mov_i64(tcg_rd, tcg_result);
4598     } else {
4599         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4600     }
4601 }
4602 
4603 /* Data-processing (3 source)
4604  *
4605  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
4606  *  +--+------+-----------+------+------+----+------+------+------+
4607  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
4608  *  +--+------+-----------+------+------+----+------+------+------+
4609  */
4610 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4611 {
4612     int rd = extract32(insn, 0, 5);
4613     int rn = extract32(insn, 5, 5);
4614     int ra = extract32(insn, 10, 5);
4615     int rm = extract32(insn, 16, 5);
4616     int op_id = (extract32(insn, 29, 3) << 4) |
4617         (extract32(insn, 21, 3) << 1) |
4618         extract32(insn, 15, 1);
4619     bool sf = extract32(insn, 31, 1);
4620     bool is_sub = extract32(op_id, 0, 1);
4621     bool is_high = extract32(op_id, 2, 1);
4622     bool is_signed = false;
4623     TCGv_i64 tcg_op1;
4624     TCGv_i64 tcg_op2;
4625     TCGv_i64 tcg_tmp;
4626 
4627     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
4628     switch (op_id) {
4629     case 0x42: /* SMADDL */
4630     case 0x43: /* SMSUBL */
4631     case 0x44: /* SMULH */
4632         is_signed = true;
4633         break;
4634     case 0x0: /* MADD (32bit) */
4635     case 0x1: /* MSUB (32bit) */
4636     case 0x40: /* MADD (64bit) */
4637     case 0x41: /* MSUB (64bit) */
4638     case 0x4a: /* UMADDL */
4639     case 0x4b: /* UMSUBL */
4640     case 0x4c: /* UMULH */
4641         break;
4642     default:
4643         unallocated_encoding(s);
4644         return;
4645     }
4646 
4647     if (is_high) {
4648         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
4649         TCGv_i64 tcg_rd = cpu_reg(s, rd);
4650         TCGv_i64 tcg_rn = cpu_reg(s, rn);
4651         TCGv_i64 tcg_rm = cpu_reg(s, rm);
4652 
4653         if (is_signed) {
4654             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4655         } else {
4656             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4657         }
4658         return;
4659     }
4660 
4661     tcg_op1 = tcg_temp_new_i64();
4662     tcg_op2 = tcg_temp_new_i64();
4663     tcg_tmp = tcg_temp_new_i64();
4664 
4665     if (op_id < 0x42) {
4666         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
4667         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
4668     } else {
4669         if (is_signed) {
4670             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
4671             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
4672         } else {
4673             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
4674             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
4675         }
4676     }
4677 
4678     if (ra == 31 && !is_sub) {
4679         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
4680         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
4681     } else {
4682         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
4683         if (is_sub) {
4684             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4685         } else {
4686             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4687         }
4688     }
4689 
4690     if (!sf) {
4691         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
4692     }
4693 }
4694 
4695 /* Add/subtract (with carry)
4696  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
4697  * +--+--+--+------------------------+------+-------------+------+-----+
4698  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
4699  * +--+--+--+------------------------+------+-------------+------+-----+
4700  */
4701 
4702 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
4703 {
4704     unsigned int sf, op, setflags, rm, rn, rd;
4705     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
4706 
4707     sf = extract32(insn, 31, 1);
4708     op = extract32(insn, 30, 1);
4709     setflags = extract32(insn, 29, 1);
4710     rm = extract32(insn, 16, 5);
4711     rn = extract32(insn, 5, 5);
4712     rd = extract32(insn, 0, 5);
4713 
4714     tcg_rd = cpu_reg(s, rd);
4715     tcg_rn = cpu_reg(s, rn);
4716 
4717     if (op) {
4718         tcg_y = tcg_temp_new_i64();
4719         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
4720     } else {
4721         tcg_y = cpu_reg(s, rm);
4722     }
4723 
4724     if (setflags) {
4725         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
4726     } else {
4727         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
4728     }
4729 }
4730 
4731 /*
4732  * Rotate right into flags
4733  *  31 30 29                21       15          10      5  4      0
4734  * +--+--+--+-----------------+--------+-----------+------+--+------+
4735  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
4736  * +--+--+--+-----------------+--------+-----------+------+--+------+
4737  */
4738 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
4739 {
4740     int mask = extract32(insn, 0, 4);
4741     int o2 = extract32(insn, 4, 1);
4742     int rn = extract32(insn, 5, 5);
4743     int imm6 = extract32(insn, 15, 6);
4744     int sf_op_s = extract32(insn, 29, 3);
4745     TCGv_i64 tcg_rn;
4746     TCGv_i32 nzcv;
4747 
4748     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
4749         unallocated_encoding(s);
4750         return;
4751     }
4752 
4753     tcg_rn = read_cpu_reg(s, rn, 1);
4754     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
4755 
4756     nzcv = tcg_temp_new_i32();
4757     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
4758 
4759     if (mask & 8) { /* N */
4760         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
4761     }
4762     if (mask & 4) { /* Z */
4763         tcg_gen_not_i32(cpu_ZF, nzcv);
4764         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
4765     }
4766     if (mask & 2) { /* C */
4767         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
4768     }
4769     if (mask & 1) { /* V */
4770         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
4771     }
4772 }
4773 
4774 /*
4775  * Evaluate into flags
4776  *  31 30 29                21        15   14        10      5  4      0
4777  * +--+--+--+-----------------+---------+----+---------+------+--+------+
4778  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
4779  * +--+--+--+-----------------+---------+----+---------+------+--+------+
4780  */
4781 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
4782 {
4783     int o3_mask = extract32(insn, 0, 5);
4784     int rn = extract32(insn, 5, 5);
4785     int o2 = extract32(insn, 15, 6);
4786     int sz = extract32(insn, 14, 1);
4787     int sf_op_s = extract32(insn, 29, 3);
4788     TCGv_i32 tmp;
4789     int shift;
4790 
4791     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
4792         !dc_isar_feature(aa64_condm_4, s)) {
4793         unallocated_encoding(s);
4794         return;
4795     }
4796     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
4797 
4798     tmp = tcg_temp_new_i32();
4799     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
4800     tcg_gen_shli_i32(cpu_NF, tmp, shift);
4801     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
4802     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
4803     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
4804 }
4805 
4806 /* Conditional compare (immediate / register)
4807  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
4808  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
4809  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
4810  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
4811  *        [1]                             y                [0]       [0]
4812  */
4813 static void disas_cc(DisasContext *s, uint32_t insn)
4814 {
4815     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
4816     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
4817     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
4818     DisasCompare c;
4819 
4820     if (!extract32(insn, 29, 1)) {
4821         unallocated_encoding(s);
4822         return;
4823     }
4824     if (insn & (1 << 10 | 1 << 4)) {
4825         unallocated_encoding(s);
4826         return;
4827     }
4828     sf = extract32(insn, 31, 1);
4829     op = extract32(insn, 30, 1);
4830     is_imm = extract32(insn, 11, 1);
4831     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
4832     cond = extract32(insn, 12, 4);
4833     rn = extract32(insn, 5, 5);
4834     nzcv = extract32(insn, 0, 4);
4835 
4836     /* Set T0 = !COND.  */
4837     tcg_t0 = tcg_temp_new_i32();
4838     arm_test_cc(&c, cond);
4839     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
4840 
4841     /* Load the arguments for the new comparison.  */
4842     if (is_imm) {
4843         tcg_y = tcg_temp_new_i64();
4844         tcg_gen_movi_i64(tcg_y, y);
4845     } else {
4846         tcg_y = cpu_reg(s, y);
4847     }
4848     tcg_rn = cpu_reg(s, rn);
4849 
4850     /* Set the flags for the new comparison.  */
4851     tcg_tmp = tcg_temp_new_i64();
4852     if (op) {
4853         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
4854     } else {
4855         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
4856     }
4857 
4858     /* If COND was false, force the flags to #nzcv.  Compute two masks
4859      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
4860      * For tcg hosts that support ANDC, we can make do with just T1.
4861      * In either case, allow the tcg optimizer to delete any unused mask.
4862      */
4863     tcg_t1 = tcg_temp_new_i32();
4864     tcg_t2 = tcg_temp_new_i32();
4865     tcg_gen_neg_i32(tcg_t1, tcg_t0);
4866     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
4867 
4868     if (nzcv & 8) { /* N */
4869         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
4870     } else {
4871         if (TCG_TARGET_HAS_andc_i32) {
4872             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
4873         } else {
4874             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
4875         }
4876     }
4877     if (nzcv & 4) { /* Z */
4878         if (TCG_TARGET_HAS_andc_i32) {
4879             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
4880         } else {
4881             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
4882         }
4883     } else {
4884         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
4885     }
4886     if (nzcv & 2) { /* C */
4887         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
4888     } else {
4889         if (TCG_TARGET_HAS_andc_i32) {
4890             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
4891         } else {
4892             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
4893         }
4894     }
4895     if (nzcv & 1) { /* V */
4896         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
4897     } else {
4898         if (TCG_TARGET_HAS_andc_i32) {
4899             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
4900         } else {
4901             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
4902         }
4903     }
4904 }
4905 
4906 /* Conditional select
4907  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
4908  * +----+----+---+-----------------+------+------+-----+------+------+
4909  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
4910  * +----+----+---+-----------------+------+------+-----+------+------+
4911  */
4912 static void disas_cond_select(DisasContext *s, uint32_t insn)
4913 {
4914     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
4915     TCGv_i64 tcg_rd, zero;
4916     DisasCompare64 c;
4917 
4918     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
4919         /* S == 1 or op2<1> == 1 */
4920         unallocated_encoding(s);
4921         return;
4922     }
4923     sf = extract32(insn, 31, 1);
4924     else_inv = extract32(insn, 30, 1);
4925     rm = extract32(insn, 16, 5);
4926     cond = extract32(insn, 12, 4);
4927     else_inc = extract32(insn, 10, 1);
4928     rn = extract32(insn, 5, 5);
4929     rd = extract32(insn, 0, 5);
4930 
4931     tcg_rd = cpu_reg(s, rd);
4932 
4933     a64_test_cc(&c, cond);
4934     zero = tcg_constant_i64(0);
4935 
4936     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
4937         /* CSET & CSETM.  */
4938         if (else_inv) {
4939             tcg_gen_negsetcond_i64(tcg_invert_cond(c.cond),
4940                                    tcg_rd, c.value, zero);
4941         } else {
4942             tcg_gen_setcond_i64(tcg_invert_cond(c.cond),
4943                                 tcg_rd, c.value, zero);
4944         }
4945     } else {
4946         TCGv_i64 t_true = cpu_reg(s, rn);
4947         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
4948         if (else_inv && else_inc) {
4949             tcg_gen_neg_i64(t_false, t_false);
4950         } else if (else_inv) {
4951             tcg_gen_not_i64(t_false, t_false);
4952         } else if (else_inc) {
4953             tcg_gen_addi_i64(t_false, t_false, 1);
4954         }
4955         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
4956     }
4957 
4958     if (!sf) {
4959         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4960     }
4961 }
4962 
4963 static void handle_clz(DisasContext *s, unsigned int sf,
4964                        unsigned int rn, unsigned int rd)
4965 {
4966     TCGv_i64 tcg_rd, tcg_rn;
4967     tcg_rd = cpu_reg(s, rd);
4968     tcg_rn = cpu_reg(s, rn);
4969 
4970     if (sf) {
4971         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
4972     } else {
4973         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4974         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4975         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
4976         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4977     }
4978 }
4979 
4980 static void handle_cls(DisasContext *s, unsigned int sf,
4981                        unsigned int rn, unsigned int rd)
4982 {
4983     TCGv_i64 tcg_rd, tcg_rn;
4984     tcg_rd = cpu_reg(s, rd);
4985     tcg_rn = cpu_reg(s, rn);
4986 
4987     if (sf) {
4988         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
4989     } else {
4990         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4991         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4992         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
4993         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4994     }
4995 }
4996 
4997 static void handle_rbit(DisasContext *s, unsigned int sf,
4998                         unsigned int rn, unsigned int rd)
4999 {
5000     TCGv_i64 tcg_rd, tcg_rn;
5001     tcg_rd = cpu_reg(s, rd);
5002     tcg_rn = cpu_reg(s, rn);
5003 
5004     if (sf) {
5005         gen_helper_rbit64(tcg_rd, tcg_rn);
5006     } else {
5007         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5008         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5009         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5010         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5011     }
5012 }
5013 
5014 /* REV with sf==1, opcode==3 ("REV64") */
5015 static void handle_rev64(DisasContext *s, unsigned int sf,
5016                          unsigned int rn, unsigned int rd)
5017 {
5018     if (!sf) {
5019         unallocated_encoding(s);
5020         return;
5021     }
5022     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5023 }
5024 
5025 /* REV with sf==0, opcode==2
5026  * REV32 (sf==1, opcode==2)
5027  */
5028 static void handle_rev32(DisasContext *s, unsigned int sf,
5029                          unsigned int rn, unsigned int rd)
5030 {
5031     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5032     TCGv_i64 tcg_rn = cpu_reg(s, rn);
5033 
5034     if (sf) {
5035         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
5036         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
5037     } else {
5038         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
5039     }
5040 }
5041 
5042 /* REV16 (opcode==1) */
5043 static void handle_rev16(DisasContext *s, unsigned int sf,
5044                          unsigned int rn, unsigned int rd)
5045 {
5046     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5047     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5048     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5049     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5050 
5051     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5052     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5053     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5054     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5055     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5056 }
5057 
5058 /* Data-processing (1 source)
5059  *   31  30  29  28             21 20     16 15    10 9    5 4    0
5060  * +----+---+---+-----------------+---------+--------+------+------+
5061  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
5062  * +----+---+---+-----------------+---------+--------+------+------+
5063  */
5064 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5065 {
5066     unsigned int sf, opcode, opcode2, rn, rd;
5067     TCGv_i64 tcg_rd;
5068 
5069     if (extract32(insn, 29, 1)) {
5070         unallocated_encoding(s);
5071         return;
5072     }
5073 
5074     sf = extract32(insn, 31, 1);
5075     opcode = extract32(insn, 10, 6);
5076     opcode2 = extract32(insn, 16, 5);
5077     rn = extract32(insn, 5, 5);
5078     rd = extract32(insn, 0, 5);
5079 
5080 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5081 
5082     switch (MAP(sf, opcode2, opcode)) {
5083     case MAP(0, 0x00, 0x00): /* RBIT */
5084     case MAP(1, 0x00, 0x00):
5085         handle_rbit(s, sf, rn, rd);
5086         break;
5087     case MAP(0, 0x00, 0x01): /* REV16 */
5088     case MAP(1, 0x00, 0x01):
5089         handle_rev16(s, sf, rn, rd);
5090         break;
5091     case MAP(0, 0x00, 0x02): /* REV/REV32 */
5092     case MAP(1, 0x00, 0x02):
5093         handle_rev32(s, sf, rn, rd);
5094         break;
5095     case MAP(1, 0x00, 0x03): /* REV64 */
5096         handle_rev64(s, sf, rn, rd);
5097         break;
5098     case MAP(0, 0x00, 0x04): /* CLZ */
5099     case MAP(1, 0x00, 0x04):
5100         handle_clz(s, sf, rn, rd);
5101         break;
5102     case MAP(0, 0x00, 0x05): /* CLS */
5103     case MAP(1, 0x00, 0x05):
5104         handle_cls(s, sf, rn, rd);
5105         break;
5106     case MAP(1, 0x01, 0x00): /* PACIA */
5107         if (s->pauth_active) {
5108             tcg_rd = cpu_reg(s, rd);
5109             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5110         } else if (!dc_isar_feature(aa64_pauth, s)) {
5111             goto do_unallocated;
5112         }
5113         break;
5114     case MAP(1, 0x01, 0x01): /* PACIB */
5115         if (s->pauth_active) {
5116             tcg_rd = cpu_reg(s, rd);
5117             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5118         } else if (!dc_isar_feature(aa64_pauth, s)) {
5119             goto do_unallocated;
5120         }
5121         break;
5122     case MAP(1, 0x01, 0x02): /* PACDA */
5123         if (s->pauth_active) {
5124             tcg_rd = cpu_reg(s, rd);
5125             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5126         } else if (!dc_isar_feature(aa64_pauth, s)) {
5127             goto do_unallocated;
5128         }
5129         break;
5130     case MAP(1, 0x01, 0x03): /* PACDB */
5131         if (s->pauth_active) {
5132             tcg_rd = cpu_reg(s, rd);
5133             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5134         } else if (!dc_isar_feature(aa64_pauth, s)) {
5135             goto do_unallocated;
5136         }
5137         break;
5138     case MAP(1, 0x01, 0x04): /* AUTIA */
5139         if (s->pauth_active) {
5140             tcg_rd = cpu_reg(s, rd);
5141             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5142         } else if (!dc_isar_feature(aa64_pauth, s)) {
5143             goto do_unallocated;
5144         }
5145         break;
5146     case MAP(1, 0x01, 0x05): /* AUTIB */
5147         if (s->pauth_active) {
5148             tcg_rd = cpu_reg(s, rd);
5149             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5150         } else if (!dc_isar_feature(aa64_pauth, s)) {
5151             goto do_unallocated;
5152         }
5153         break;
5154     case MAP(1, 0x01, 0x06): /* AUTDA */
5155         if (s->pauth_active) {
5156             tcg_rd = cpu_reg(s, rd);
5157             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5158         } else if (!dc_isar_feature(aa64_pauth, s)) {
5159             goto do_unallocated;
5160         }
5161         break;
5162     case MAP(1, 0x01, 0x07): /* AUTDB */
5163         if (s->pauth_active) {
5164             tcg_rd = cpu_reg(s, rd);
5165             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5166         } else if (!dc_isar_feature(aa64_pauth, s)) {
5167             goto do_unallocated;
5168         }
5169         break;
5170     case MAP(1, 0x01, 0x08): /* PACIZA */
5171         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5172             goto do_unallocated;
5173         } else if (s->pauth_active) {
5174             tcg_rd = cpu_reg(s, rd);
5175             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5176         }
5177         break;
5178     case MAP(1, 0x01, 0x09): /* PACIZB */
5179         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5180             goto do_unallocated;
5181         } else if (s->pauth_active) {
5182             tcg_rd = cpu_reg(s, rd);
5183             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5184         }
5185         break;
5186     case MAP(1, 0x01, 0x0a): /* PACDZA */
5187         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5188             goto do_unallocated;
5189         } else if (s->pauth_active) {
5190             tcg_rd = cpu_reg(s, rd);
5191             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5192         }
5193         break;
5194     case MAP(1, 0x01, 0x0b): /* PACDZB */
5195         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5196             goto do_unallocated;
5197         } else if (s->pauth_active) {
5198             tcg_rd = cpu_reg(s, rd);
5199             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5200         }
5201         break;
5202     case MAP(1, 0x01, 0x0c): /* AUTIZA */
5203         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5204             goto do_unallocated;
5205         } else if (s->pauth_active) {
5206             tcg_rd = cpu_reg(s, rd);
5207             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5208         }
5209         break;
5210     case MAP(1, 0x01, 0x0d): /* AUTIZB */
5211         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5212             goto do_unallocated;
5213         } else if (s->pauth_active) {
5214             tcg_rd = cpu_reg(s, rd);
5215             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5216         }
5217         break;
5218     case MAP(1, 0x01, 0x0e): /* AUTDZA */
5219         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5220             goto do_unallocated;
5221         } else if (s->pauth_active) {
5222             tcg_rd = cpu_reg(s, rd);
5223             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5224         }
5225         break;
5226     case MAP(1, 0x01, 0x0f): /* AUTDZB */
5227         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5228             goto do_unallocated;
5229         } else if (s->pauth_active) {
5230             tcg_rd = cpu_reg(s, rd);
5231             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5232         }
5233         break;
5234     case MAP(1, 0x01, 0x10): /* XPACI */
5235         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5236             goto do_unallocated;
5237         } else if (s->pauth_active) {
5238             tcg_rd = cpu_reg(s, rd);
5239             gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
5240         }
5241         break;
5242     case MAP(1, 0x01, 0x11): /* XPACD */
5243         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5244             goto do_unallocated;
5245         } else if (s->pauth_active) {
5246             tcg_rd = cpu_reg(s, rd);
5247             gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5248         }
5249         break;
5250     default:
5251     do_unallocated:
5252         unallocated_encoding(s);
5253         break;
5254     }
5255 
5256 #undef MAP
5257 }
5258 
5259 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5260                        unsigned int rm, unsigned int rn, unsigned int rd)
5261 {
5262     TCGv_i64 tcg_n, tcg_m, tcg_rd;
5263     tcg_rd = cpu_reg(s, rd);
5264 
5265     if (!sf && is_signed) {
5266         tcg_n = tcg_temp_new_i64();
5267         tcg_m = tcg_temp_new_i64();
5268         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5269         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5270     } else {
5271         tcg_n = read_cpu_reg(s, rn, sf);
5272         tcg_m = read_cpu_reg(s, rm, sf);
5273     }
5274 
5275     if (is_signed) {
5276         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5277     } else {
5278         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5279     }
5280 
5281     if (!sf) { /* zero extend final result */
5282         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5283     }
5284 }
5285 
5286 /* LSLV, LSRV, ASRV, RORV */
5287 static void handle_shift_reg(DisasContext *s,
5288                              enum a64_shift_type shift_type, unsigned int sf,
5289                              unsigned int rm, unsigned int rn, unsigned int rd)
5290 {
5291     TCGv_i64 tcg_shift = tcg_temp_new_i64();
5292     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5293     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5294 
5295     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5296     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5297 }
5298 
5299 /* CRC32[BHWX], CRC32C[BHWX] */
5300 static void handle_crc32(DisasContext *s,
5301                          unsigned int sf, unsigned int sz, bool crc32c,
5302                          unsigned int rm, unsigned int rn, unsigned int rd)
5303 {
5304     TCGv_i64 tcg_acc, tcg_val;
5305     TCGv_i32 tcg_bytes;
5306 
5307     if (!dc_isar_feature(aa64_crc32, s)
5308         || (sf == 1 && sz != 3)
5309         || (sf == 0 && sz == 3)) {
5310         unallocated_encoding(s);
5311         return;
5312     }
5313 
5314     if (sz == 3) {
5315         tcg_val = cpu_reg(s, rm);
5316     } else {
5317         uint64_t mask;
5318         switch (sz) {
5319         case 0:
5320             mask = 0xFF;
5321             break;
5322         case 1:
5323             mask = 0xFFFF;
5324             break;
5325         case 2:
5326             mask = 0xFFFFFFFF;
5327             break;
5328         default:
5329             g_assert_not_reached();
5330         }
5331         tcg_val = tcg_temp_new_i64();
5332         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5333     }
5334 
5335     tcg_acc = cpu_reg(s, rn);
5336     tcg_bytes = tcg_constant_i32(1 << sz);
5337 
5338     if (crc32c) {
5339         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5340     } else {
5341         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5342     }
5343 }
5344 
5345 /* Data-processing (2 source)
5346  *   31   30  29 28             21 20  16 15    10 9    5 4    0
5347  * +----+---+---+-----------------+------+--------+------+------+
5348  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
5349  * +----+---+---+-----------------+------+--------+------+------+
5350  */
5351 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5352 {
5353     unsigned int sf, rm, opcode, rn, rd, setflag;
5354     sf = extract32(insn, 31, 1);
5355     setflag = extract32(insn, 29, 1);
5356     rm = extract32(insn, 16, 5);
5357     opcode = extract32(insn, 10, 6);
5358     rn = extract32(insn, 5, 5);
5359     rd = extract32(insn, 0, 5);
5360 
5361     if (setflag && opcode != 0) {
5362         unallocated_encoding(s);
5363         return;
5364     }
5365 
5366     switch (opcode) {
5367     case 0: /* SUBP(S) */
5368         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5369             goto do_unallocated;
5370         } else {
5371             TCGv_i64 tcg_n, tcg_m, tcg_d;
5372 
5373             tcg_n = read_cpu_reg_sp(s, rn, true);
5374             tcg_m = read_cpu_reg_sp(s, rm, true);
5375             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5376             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5377             tcg_d = cpu_reg(s, rd);
5378 
5379             if (setflag) {
5380                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5381             } else {
5382                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5383             }
5384         }
5385         break;
5386     case 2: /* UDIV */
5387         handle_div(s, false, sf, rm, rn, rd);
5388         break;
5389     case 3: /* SDIV */
5390         handle_div(s, true, sf, rm, rn, rd);
5391         break;
5392     case 4: /* IRG */
5393         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5394             goto do_unallocated;
5395         }
5396         if (s->ata) {
5397             gen_helper_irg(cpu_reg_sp(s, rd), cpu_env,
5398                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
5399         } else {
5400             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5401                                              cpu_reg_sp(s, rn));
5402         }
5403         break;
5404     case 5: /* GMI */
5405         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5406             goto do_unallocated;
5407         } else {
5408             TCGv_i64 t = tcg_temp_new_i64();
5409 
5410             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
5411             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
5412             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
5413         }
5414         break;
5415     case 8: /* LSLV */
5416         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5417         break;
5418     case 9: /* LSRV */
5419         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5420         break;
5421     case 10: /* ASRV */
5422         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5423         break;
5424     case 11: /* RORV */
5425         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5426         break;
5427     case 12: /* PACGA */
5428         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5429             goto do_unallocated;
5430         }
5431         gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5432                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
5433         break;
5434     case 16:
5435     case 17:
5436     case 18:
5437     case 19:
5438     case 20:
5439     case 21:
5440     case 22:
5441     case 23: /* CRC32 */
5442     {
5443         int sz = extract32(opcode, 0, 2);
5444         bool crc32c = extract32(opcode, 2, 1);
5445         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5446         break;
5447     }
5448     default:
5449     do_unallocated:
5450         unallocated_encoding(s);
5451         break;
5452     }
5453 }
5454 
5455 /*
5456  * Data processing - register
5457  *  31  30 29  28      25    21  20  16      10         0
5458  * +--+---+--+---+-------+-----+-------+-------+---------+
5459  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
5460  * +--+---+--+---+-------+-----+-------+-------+---------+
5461  */
5462 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5463 {
5464     int op0 = extract32(insn, 30, 1);
5465     int op1 = extract32(insn, 28, 1);
5466     int op2 = extract32(insn, 21, 4);
5467     int op3 = extract32(insn, 10, 6);
5468 
5469     if (!op1) {
5470         if (op2 & 8) {
5471             if (op2 & 1) {
5472                 /* Add/sub (extended register) */
5473                 disas_add_sub_ext_reg(s, insn);
5474             } else {
5475                 /* Add/sub (shifted register) */
5476                 disas_add_sub_reg(s, insn);
5477             }
5478         } else {
5479             /* Logical (shifted register) */
5480             disas_logic_reg(s, insn);
5481         }
5482         return;
5483     }
5484 
5485     switch (op2) {
5486     case 0x0:
5487         switch (op3) {
5488         case 0x00: /* Add/subtract (with carry) */
5489             disas_adc_sbc(s, insn);
5490             break;
5491 
5492         case 0x01: /* Rotate right into flags */
5493         case 0x21:
5494             disas_rotate_right_into_flags(s, insn);
5495             break;
5496 
5497         case 0x02: /* Evaluate into flags */
5498         case 0x12:
5499         case 0x22:
5500         case 0x32:
5501             disas_evaluate_into_flags(s, insn);
5502             break;
5503 
5504         default:
5505             goto do_unallocated;
5506         }
5507         break;
5508 
5509     case 0x2: /* Conditional compare */
5510         disas_cc(s, insn); /* both imm and reg forms */
5511         break;
5512 
5513     case 0x4: /* Conditional select */
5514         disas_cond_select(s, insn);
5515         break;
5516 
5517     case 0x6: /* Data-processing */
5518         if (op0) {    /* (1 source) */
5519             disas_data_proc_1src(s, insn);
5520         } else {      /* (2 source) */
5521             disas_data_proc_2src(s, insn);
5522         }
5523         break;
5524     case 0x8 ... 0xf: /* (3 source) */
5525         disas_data_proc_3src(s, insn);
5526         break;
5527 
5528     default:
5529     do_unallocated:
5530         unallocated_encoding(s);
5531         break;
5532     }
5533 }
5534 
5535 static void handle_fp_compare(DisasContext *s, int size,
5536                               unsigned int rn, unsigned int rm,
5537                               bool cmp_with_zero, bool signal_all_nans)
5538 {
5539     TCGv_i64 tcg_flags = tcg_temp_new_i64();
5540     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
5541 
5542     if (size == MO_64) {
5543         TCGv_i64 tcg_vn, tcg_vm;
5544 
5545         tcg_vn = read_fp_dreg(s, rn);
5546         if (cmp_with_zero) {
5547             tcg_vm = tcg_constant_i64(0);
5548         } else {
5549             tcg_vm = read_fp_dreg(s, rm);
5550         }
5551         if (signal_all_nans) {
5552             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5553         } else {
5554             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5555         }
5556     } else {
5557         TCGv_i32 tcg_vn = tcg_temp_new_i32();
5558         TCGv_i32 tcg_vm = tcg_temp_new_i32();
5559 
5560         read_vec_element_i32(s, tcg_vn, rn, 0, size);
5561         if (cmp_with_zero) {
5562             tcg_gen_movi_i32(tcg_vm, 0);
5563         } else {
5564             read_vec_element_i32(s, tcg_vm, rm, 0, size);
5565         }
5566 
5567         switch (size) {
5568         case MO_32:
5569             if (signal_all_nans) {
5570                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5571             } else {
5572                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5573             }
5574             break;
5575         case MO_16:
5576             if (signal_all_nans) {
5577                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5578             } else {
5579                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5580             }
5581             break;
5582         default:
5583             g_assert_not_reached();
5584         }
5585     }
5586 
5587     gen_set_nzcv(tcg_flags);
5588 }
5589 
5590 /* Floating point compare
5591  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
5592  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5593  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
5594  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5595  */
5596 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5597 {
5598     unsigned int mos, type, rm, op, rn, opc, op2r;
5599     int size;
5600 
5601     mos = extract32(insn, 29, 3);
5602     type = extract32(insn, 22, 2);
5603     rm = extract32(insn, 16, 5);
5604     op = extract32(insn, 14, 2);
5605     rn = extract32(insn, 5, 5);
5606     opc = extract32(insn, 3, 2);
5607     op2r = extract32(insn, 0, 3);
5608 
5609     if (mos || op || op2r) {
5610         unallocated_encoding(s);
5611         return;
5612     }
5613 
5614     switch (type) {
5615     case 0:
5616         size = MO_32;
5617         break;
5618     case 1:
5619         size = MO_64;
5620         break;
5621     case 3:
5622         size = MO_16;
5623         if (dc_isar_feature(aa64_fp16, s)) {
5624             break;
5625         }
5626         /* fallthru */
5627     default:
5628         unallocated_encoding(s);
5629         return;
5630     }
5631 
5632     if (!fp_access_check(s)) {
5633         return;
5634     }
5635 
5636     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
5637 }
5638 
5639 /* Floating point conditional compare
5640  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
5641  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5642  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
5643  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5644  */
5645 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
5646 {
5647     unsigned int mos, type, rm, cond, rn, op, nzcv;
5648     TCGLabel *label_continue = NULL;
5649     int size;
5650 
5651     mos = extract32(insn, 29, 3);
5652     type = extract32(insn, 22, 2);
5653     rm = extract32(insn, 16, 5);
5654     cond = extract32(insn, 12, 4);
5655     rn = extract32(insn, 5, 5);
5656     op = extract32(insn, 4, 1);
5657     nzcv = extract32(insn, 0, 4);
5658 
5659     if (mos) {
5660         unallocated_encoding(s);
5661         return;
5662     }
5663 
5664     switch (type) {
5665     case 0:
5666         size = MO_32;
5667         break;
5668     case 1:
5669         size = MO_64;
5670         break;
5671     case 3:
5672         size = MO_16;
5673         if (dc_isar_feature(aa64_fp16, s)) {
5674             break;
5675         }
5676         /* fallthru */
5677     default:
5678         unallocated_encoding(s);
5679         return;
5680     }
5681 
5682     if (!fp_access_check(s)) {
5683         return;
5684     }
5685 
5686     if (cond < 0x0e) { /* not always */
5687         TCGLabel *label_match = gen_new_label();
5688         label_continue = gen_new_label();
5689         arm_gen_test_cc(cond, label_match);
5690         /* nomatch: */
5691         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
5692         tcg_gen_br(label_continue);
5693         gen_set_label(label_match);
5694     }
5695 
5696     handle_fp_compare(s, size, rn, rm, false, op);
5697 
5698     if (cond < 0x0e) {
5699         gen_set_label(label_continue);
5700     }
5701 }
5702 
5703 /* Floating point conditional select
5704  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
5705  * +---+---+---+-----------+------+---+------+------+-----+------+------+
5706  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
5707  * +---+---+---+-----------+------+---+------+------+-----+------+------+
5708  */
5709 static void disas_fp_csel(DisasContext *s, uint32_t insn)
5710 {
5711     unsigned int mos, type, rm, cond, rn, rd;
5712     TCGv_i64 t_true, t_false;
5713     DisasCompare64 c;
5714     MemOp sz;
5715 
5716     mos = extract32(insn, 29, 3);
5717     type = extract32(insn, 22, 2);
5718     rm = extract32(insn, 16, 5);
5719     cond = extract32(insn, 12, 4);
5720     rn = extract32(insn, 5, 5);
5721     rd = extract32(insn, 0, 5);
5722 
5723     if (mos) {
5724         unallocated_encoding(s);
5725         return;
5726     }
5727 
5728     switch (type) {
5729     case 0:
5730         sz = MO_32;
5731         break;
5732     case 1:
5733         sz = MO_64;
5734         break;
5735     case 3:
5736         sz = MO_16;
5737         if (dc_isar_feature(aa64_fp16, s)) {
5738             break;
5739         }
5740         /* fallthru */
5741     default:
5742         unallocated_encoding(s);
5743         return;
5744     }
5745 
5746     if (!fp_access_check(s)) {
5747         return;
5748     }
5749 
5750     /* Zero extend sreg & hreg inputs to 64 bits now.  */
5751     t_true = tcg_temp_new_i64();
5752     t_false = tcg_temp_new_i64();
5753     read_vec_element(s, t_true, rn, 0, sz);
5754     read_vec_element(s, t_false, rm, 0, sz);
5755 
5756     a64_test_cc(&c, cond);
5757     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
5758                         t_true, t_false);
5759 
5760     /* Note that sregs & hregs write back zeros to the high bits,
5761        and we've already done the zero-extension.  */
5762     write_fp_dreg(s, rd, t_true);
5763 }
5764 
5765 /* Floating-point data-processing (1 source) - half precision */
5766 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
5767 {
5768     TCGv_ptr fpst = NULL;
5769     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
5770     TCGv_i32 tcg_res = tcg_temp_new_i32();
5771 
5772     switch (opcode) {
5773     case 0x0: /* FMOV */
5774         tcg_gen_mov_i32(tcg_res, tcg_op);
5775         break;
5776     case 0x1: /* FABS */
5777         tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
5778         break;
5779     case 0x2: /* FNEG */
5780         tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
5781         break;
5782     case 0x3: /* FSQRT */
5783         fpst = fpstatus_ptr(FPST_FPCR_F16);
5784         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
5785         break;
5786     case 0x8: /* FRINTN */
5787     case 0x9: /* FRINTP */
5788     case 0xa: /* FRINTM */
5789     case 0xb: /* FRINTZ */
5790     case 0xc: /* FRINTA */
5791     {
5792         TCGv_i32 tcg_rmode;
5793 
5794         fpst = fpstatus_ptr(FPST_FPCR_F16);
5795         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
5796         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
5797         gen_restore_rmode(tcg_rmode, fpst);
5798         break;
5799     }
5800     case 0xe: /* FRINTX */
5801         fpst = fpstatus_ptr(FPST_FPCR_F16);
5802         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
5803         break;
5804     case 0xf: /* FRINTI */
5805         fpst = fpstatus_ptr(FPST_FPCR_F16);
5806         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
5807         break;
5808     default:
5809         g_assert_not_reached();
5810     }
5811 
5812     write_fp_sreg(s, rd, tcg_res);
5813 }
5814 
5815 /* Floating-point data-processing (1 source) - single precision */
5816 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
5817 {
5818     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
5819     TCGv_i32 tcg_op, tcg_res;
5820     TCGv_ptr fpst;
5821     int rmode = -1;
5822 
5823     tcg_op = read_fp_sreg(s, rn);
5824     tcg_res = tcg_temp_new_i32();
5825 
5826     switch (opcode) {
5827     case 0x0: /* FMOV */
5828         tcg_gen_mov_i32(tcg_res, tcg_op);
5829         goto done;
5830     case 0x1: /* FABS */
5831         gen_helper_vfp_abss(tcg_res, tcg_op);
5832         goto done;
5833     case 0x2: /* FNEG */
5834         gen_helper_vfp_negs(tcg_res, tcg_op);
5835         goto done;
5836     case 0x3: /* FSQRT */
5837         gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
5838         goto done;
5839     case 0x6: /* BFCVT */
5840         gen_fpst = gen_helper_bfcvt;
5841         break;
5842     case 0x8: /* FRINTN */
5843     case 0x9: /* FRINTP */
5844     case 0xa: /* FRINTM */
5845     case 0xb: /* FRINTZ */
5846     case 0xc: /* FRINTA */
5847         rmode = opcode & 7;
5848         gen_fpst = gen_helper_rints;
5849         break;
5850     case 0xe: /* FRINTX */
5851         gen_fpst = gen_helper_rints_exact;
5852         break;
5853     case 0xf: /* FRINTI */
5854         gen_fpst = gen_helper_rints;
5855         break;
5856     case 0x10: /* FRINT32Z */
5857         rmode = FPROUNDING_ZERO;
5858         gen_fpst = gen_helper_frint32_s;
5859         break;
5860     case 0x11: /* FRINT32X */
5861         gen_fpst = gen_helper_frint32_s;
5862         break;
5863     case 0x12: /* FRINT64Z */
5864         rmode = FPROUNDING_ZERO;
5865         gen_fpst = gen_helper_frint64_s;
5866         break;
5867     case 0x13: /* FRINT64X */
5868         gen_fpst = gen_helper_frint64_s;
5869         break;
5870     default:
5871         g_assert_not_reached();
5872     }
5873 
5874     fpst = fpstatus_ptr(FPST_FPCR);
5875     if (rmode >= 0) {
5876         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
5877         gen_fpst(tcg_res, tcg_op, fpst);
5878         gen_restore_rmode(tcg_rmode, fpst);
5879     } else {
5880         gen_fpst(tcg_res, tcg_op, fpst);
5881     }
5882 
5883  done:
5884     write_fp_sreg(s, rd, tcg_res);
5885 }
5886 
5887 /* Floating-point data-processing (1 source) - double precision */
5888 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
5889 {
5890     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
5891     TCGv_i64 tcg_op, tcg_res;
5892     TCGv_ptr fpst;
5893     int rmode = -1;
5894 
5895     switch (opcode) {
5896     case 0x0: /* FMOV */
5897         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
5898         return;
5899     }
5900 
5901     tcg_op = read_fp_dreg(s, rn);
5902     tcg_res = tcg_temp_new_i64();
5903 
5904     switch (opcode) {
5905     case 0x1: /* FABS */
5906         gen_helper_vfp_absd(tcg_res, tcg_op);
5907         goto done;
5908     case 0x2: /* FNEG */
5909         gen_helper_vfp_negd(tcg_res, tcg_op);
5910         goto done;
5911     case 0x3: /* FSQRT */
5912         gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
5913         goto done;
5914     case 0x8: /* FRINTN */
5915     case 0x9: /* FRINTP */
5916     case 0xa: /* FRINTM */
5917     case 0xb: /* FRINTZ */
5918     case 0xc: /* FRINTA */
5919         rmode = opcode & 7;
5920         gen_fpst = gen_helper_rintd;
5921         break;
5922     case 0xe: /* FRINTX */
5923         gen_fpst = gen_helper_rintd_exact;
5924         break;
5925     case 0xf: /* FRINTI */
5926         gen_fpst = gen_helper_rintd;
5927         break;
5928     case 0x10: /* FRINT32Z */
5929         rmode = FPROUNDING_ZERO;
5930         gen_fpst = gen_helper_frint32_d;
5931         break;
5932     case 0x11: /* FRINT32X */
5933         gen_fpst = gen_helper_frint32_d;
5934         break;
5935     case 0x12: /* FRINT64Z */
5936         rmode = FPROUNDING_ZERO;
5937         gen_fpst = gen_helper_frint64_d;
5938         break;
5939     case 0x13: /* FRINT64X */
5940         gen_fpst = gen_helper_frint64_d;
5941         break;
5942     default:
5943         g_assert_not_reached();
5944     }
5945 
5946     fpst = fpstatus_ptr(FPST_FPCR);
5947     if (rmode >= 0) {
5948         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
5949         gen_fpst(tcg_res, tcg_op, fpst);
5950         gen_restore_rmode(tcg_rmode, fpst);
5951     } else {
5952         gen_fpst(tcg_res, tcg_op, fpst);
5953     }
5954 
5955  done:
5956     write_fp_dreg(s, rd, tcg_res);
5957 }
5958 
5959 static void handle_fp_fcvt(DisasContext *s, int opcode,
5960                            int rd, int rn, int dtype, int ntype)
5961 {
5962     switch (ntype) {
5963     case 0x0:
5964     {
5965         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
5966         if (dtype == 1) {
5967             /* Single to double */
5968             TCGv_i64 tcg_rd = tcg_temp_new_i64();
5969             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
5970             write_fp_dreg(s, rd, tcg_rd);
5971         } else {
5972             /* Single to half */
5973             TCGv_i32 tcg_rd = tcg_temp_new_i32();
5974             TCGv_i32 ahp = get_ahp_flag();
5975             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
5976 
5977             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
5978             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
5979             write_fp_sreg(s, rd, tcg_rd);
5980         }
5981         break;
5982     }
5983     case 0x1:
5984     {
5985         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
5986         TCGv_i32 tcg_rd = tcg_temp_new_i32();
5987         if (dtype == 0) {
5988             /* Double to single */
5989             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
5990         } else {
5991             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
5992             TCGv_i32 ahp = get_ahp_flag();
5993             /* Double to half */
5994             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
5995             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
5996         }
5997         write_fp_sreg(s, rd, tcg_rd);
5998         break;
5999     }
6000     case 0x3:
6001     {
6002         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6003         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6004         TCGv_i32 tcg_ahp = get_ahp_flag();
6005         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6006         if (dtype == 0) {
6007             /* Half to single */
6008             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6009             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6010             write_fp_sreg(s, rd, tcg_rd);
6011         } else {
6012             /* Half to double */
6013             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6014             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6015             write_fp_dreg(s, rd, tcg_rd);
6016         }
6017         break;
6018     }
6019     default:
6020         g_assert_not_reached();
6021     }
6022 }
6023 
6024 /* Floating point data-processing (1 source)
6025  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
6026  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6027  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
6028  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6029  */
6030 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6031 {
6032     int mos = extract32(insn, 29, 3);
6033     int type = extract32(insn, 22, 2);
6034     int opcode = extract32(insn, 15, 6);
6035     int rn = extract32(insn, 5, 5);
6036     int rd = extract32(insn, 0, 5);
6037 
6038     if (mos) {
6039         goto do_unallocated;
6040     }
6041 
6042     switch (opcode) {
6043     case 0x4: case 0x5: case 0x7:
6044     {
6045         /* FCVT between half, single and double precision */
6046         int dtype = extract32(opcode, 0, 2);
6047         if (type == 2 || dtype == type) {
6048             goto do_unallocated;
6049         }
6050         if (!fp_access_check(s)) {
6051             return;
6052         }
6053 
6054         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6055         break;
6056     }
6057 
6058     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6059         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6060             goto do_unallocated;
6061         }
6062         /* fall through */
6063     case 0x0 ... 0x3:
6064     case 0x8 ... 0xc:
6065     case 0xe ... 0xf:
6066         /* 32-to-32 and 64-to-64 ops */
6067         switch (type) {
6068         case 0:
6069             if (!fp_access_check(s)) {
6070                 return;
6071             }
6072             handle_fp_1src_single(s, opcode, rd, rn);
6073             break;
6074         case 1:
6075             if (!fp_access_check(s)) {
6076                 return;
6077             }
6078             handle_fp_1src_double(s, opcode, rd, rn);
6079             break;
6080         case 3:
6081             if (!dc_isar_feature(aa64_fp16, s)) {
6082                 goto do_unallocated;
6083             }
6084 
6085             if (!fp_access_check(s)) {
6086                 return;
6087             }
6088             handle_fp_1src_half(s, opcode, rd, rn);
6089             break;
6090         default:
6091             goto do_unallocated;
6092         }
6093         break;
6094 
6095     case 0x6:
6096         switch (type) {
6097         case 1: /* BFCVT */
6098             if (!dc_isar_feature(aa64_bf16, s)) {
6099                 goto do_unallocated;
6100             }
6101             if (!fp_access_check(s)) {
6102                 return;
6103             }
6104             handle_fp_1src_single(s, opcode, rd, rn);
6105             break;
6106         default:
6107             goto do_unallocated;
6108         }
6109         break;
6110 
6111     default:
6112     do_unallocated:
6113         unallocated_encoding(s);
6114         break;
6115     }
6116 }
6117 
6118 /* Floating-point data-processing (2 source) - single precision */
6119 static void handle_fp_2src_single(DisasContext *s, int opcode,
6120                                   int rd, int rn, int rm)
6121 {
6122     TCGv_i32 tcg_op1;
6123     TCGv_i32 tcg_op2;
6124     TCGv_i32 tcg_res;
6125     TCGv_ptr fpst;
6126 
6127     tcg_res = tcg_temp_new_i32();
6128     fpst = fpstatus_ptr(FPST_FPCR);
6129     tcg_op1 = read_fp_sreg(s, rn);
6130     tcg_op2 = read_fp_sreg(s, rm);
6131 
6132     switch (opcode) {
6133     case 0x0: /* FMUL */
6134         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6135         break;
6136     case 0x1: /* FDIV */
6137         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6138         break;
6139     case 0x2: /* FADD */
6140         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6141         break;
6142     case 0x3: /* FSUB */
6143         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6144         break;
6145     case 0x4: /* FMAX */
6146         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6147         break;
6148     case 0x5: /* FMIN */
6149         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6150         break;
6151     case 0x6: /* FMAXNM */
6152         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6153         break;
6154     case 0x7: /* FMINNM */
6155         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6156         break;
6157     case 0x8: /* FNMUL */
6158         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6159         gen_helper_vfp_negs(tcg_res, tcg_res);
6160         break;
6161     }
6162 
6163     write_fp_sreg(s, rd, tcg_res);
6164 }
6165 
6166 /* Floating-point data-processing (2 source) - double precision */
6167 static void handle_fp_2src_double(DisasContext *s, int opcode,
6168                                   int rd, int rn, int rm)
6169 {
6170     TCGv_i64 tcg_op1;
6171     TCGv_i64 tcg_op2;
6172     TCGv_i64 tcg_res;
6173     TCGv_ptr fpst;
6174 
6175     tcg_res = tcg_temp_new_i64();
6176     fpst = fpstatus_ptr(FPST_FPCR);
6177     tcg_op1 = read_fp_dreg(s, rn);
6178     tcg_op2 = read_fp_dreg(s, rm);
6179 
6180     switch (opcode) {
6181     case 0x0: /* FMUL */
6182         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6183         break;
6184     case 0x1: /* FDIV */
6185         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6186         break;
6187     case 0x2: /* FADD */
6188         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6189         break;
6190     case 0x3: /* FSUB */
6191         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6192         break;
6193     case 0x4: /* FMAX */
6194         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6195         break;
6196     case 0x5: /* FMIN */
6197         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6198         break;
6199     case 0x6: /* FMAXNM */
6200         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6201         break;
6202     case 0x7: /* FMINNM */
6203         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6204         break;
6205     case 0x8: /* FNMUL */
6206         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6207         gen_helper_vfp_negd(tcg_res, tcg_res);
6208         break;
6209     }
6210 
6211     write_fp_dreg(s, rd, tcg_res);
6212 }
6213 
6214 /* Floating-point data-processing (2 source) - half precision */
6215 static void handle_fp_2src_half(DisasContext *s, int opcode,
6216                                 int rd, int rn, int rm)
6217 {
6218     TCGv_i32 tcg_op1;
6219     TCGv_i32 tcg_op2;
6220     TCGv_i32 tcg_res;
6221     TCGv_ptr fpst;
6222 
6223     tcg_res = tcg_temp_new_i32();
6224     fpst = fpstatus_ptr(FPST_FPCR_F16);
6225     tcg_op1 = read_fp_hreg(s, rn);
6226     tcg_op2 = read_fp_hreg(s, rm);
6227 
6228     switch (opcode) {
6229     case 0x0: /* FMUL */
6230         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6231         break;
6232     case 0x1: /* FDIV */
6233         gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6234         break;
6235     case 0x2: /* FADD */
6236         gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6237         break;
6238     case 0x3: /* FSUB */
6239         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6240         break;
6241     case 0x4: /* FMAX */
6242         gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6243         break;
6244     case 0x5: /* FMIN */
6245         gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6246         break;
6247     case 0x6: /* FMAXNM */
6248         gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6249         break;
6250     case 0x7: /* FMINNM */
6251         gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6252         break;
6253     case 0x8: /* FNMUL */
6254         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6255         tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6256         break;
6257     default:
6258         g_assert_not_reached();
6259     }
6260 
6261     write_fp_sreg(s, rd, tcg_res);
6262 }
6263 
6264 /* Floating point data-processing (2 source)
6265  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
6266  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6267  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
6268  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6269  */
6270 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6271 {
6272     int mos = extract32(insn, 29, 3);
6273     int type = extract32(insn, 22, 2);
6274     int rd = extract32(insn, 0, 5);
6275     int rn = extract32(insn, 5, 5);
6276     int rm = extract32(insn, 16, 5);
6277     int opcode = extract32(insn, 12, 4);
6278 
6279     if (opcode > 8 || mos) {
6280         unallocated_encoding(s);
6281         return;
6282     }
6283 
6284     switch (type) {
6285     case 0:
6286         if (!fp_access_check(s)) {
6287             return;
6288         }
6289         handle_fp_2src_single(s, opcode, rd, rn, rm);
6290         break;
6291     case 1:
6292         if (!fp_access_check(s)) {
6293             return;
6294         }
6295         handle_fp_2src_double(s, opcode, rd, rn, rm);
6296         break;
6297     case 3:
6298         if (!dc_isar_feature(aa64_fp16, s)) {
6299             unallocated_encoding(s);
6300             return;
6301         }
6302         if (!fp_access_check(s)) {
6303             return;
6304         }
6305         handle_fp_2src_half(s, opcode, rd, rn, rm);
6306         break;
6307     default:
6308         unallocated_encoding(s);
6309     }
6310 }
6311 
6312 /* Floating-point data-processing (3 source) - single precision */
6313 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6314                                   int rd, int rn, int rm, int ra)
6315 {
6316     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6317     TCGv_i32 tcg_res = tcg_temp_new_i32();
6318     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6319 
6320     tcg_op1 = read_fp_sreg(s, rn);
6321     tcg_op2 = read_fp_sreg(s, rm);
6322     tcg_op3 = read_fp_sreg(s, ra);
6323 
6324     /* These are fused multiply-add, and must be done as one
6325      * floating point operation with no rounding between the
6326      * multiplication and addition steps.
6327      * NB that doing the negations here as separate steps is
6328      * correct : an input NaN should come out with its sign bit
6329      * flipped if it is a negated-input.
6330      */
6331     if (o1 == true) {
6332         gen_helper_vfp_negs(tcg_op3, tcg_op3);
6333     }
6334 
6335     if (o0 != o1) {
6336         gen_helper_vfp_negs(tcg_op1, tcg_op1);
6337     }
6338 
6339     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6340 
6341     write_fp_sreg(s, rd, tcg_res);
6342 }
6343 
6344 /* Floating-point data-processing (3 source) - double precision */
6345 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6346                                   int rd, int rn, int rm, int ra)
6347 {
6348     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6349     TCGv_i64 tcg_res = tcg_temp_new_i64();
6350     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6351 
6352     tcg_op1 = read_fp_dreg(s, rn);
6353     tcg_op2 = read_fp_dreg(s, rm);
6354     tcg_op3 = read_fp_dreg(s, ra);
6355 
6356     /* These are fused multiply-add, and must be done as one
6357      * floating point operation with no rounding between the
6358      * multiplication and addition steps.
6359      * NB that doing the negations here as separate steps is
6360      * correct : an input NaN should come out with its sign bit
6361      * flipped if it is a negated-input.
6362      */
6363     if (o1 == true) {
6364         gen_helper_vfp_negd(tcg_op3, tcg_op3);
6365     }
6366 
6367     if (o0 != o1) {
6368         gen_helper_vfp_negd(tcg_op1, tcg_op1);
6369     }
6370 
6371     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6372 
6373     write_fp_dreg(s, rd, tcg_res);
6374 }
6375 
6376 /* Floating-point data-processing (3 source) - half precision */
6377 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6378                                 int rd, int rn, int rm, int ra)
6379 {
6380     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6381     TCGv_i32 tcg_res = tcg_temp_new_i32();
6382     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6383 
6384     tcg_op1 = read_fp_hreg(s, rn);
6385     tcg_op2 = read_fp_hreg(s, rm);
6386     tcg_op3 = read_fp_hreg(s, ra);
6387 
6388     /* These are fused multiply-add, and must be done as one
6389      * floating point operation with no rounding between the
6390      * multiplication and addition steps.
6391      * NB that doing the negations here as separate steps is
6392      * correct : an input NaN should come out with its sign bit
6393      * flipped if it is a negated-input.
6394      */
6395     if (o1 == true) {
6396         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6397     }
6398 
6399     if (o0 != o1) {
6400         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6401     }
6402 
6403     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6404 
6405     write_fp_sreg(s, rd, tcg_res);
6406 }
6407 
6408 /* Floating point data-processing (3 source)
6409  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
6410  * +---+---+---+-----------+------+----+------+----+------+------+------+
6411  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6412  * +---+---+---+-----------+------+----+------+----+------+------+------+
6413  */
6414 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6415 {
6416     int mos = extract32(insn, 29, 3);
6417     int type = extract32(insn, 22, 2);
6418     int rd = extract32(insn, 0, 5);
6419     int rn = extract32(insn, 5, 5);
6420     int ra = extract32(insn, 10, 5);
6421     int rm = extract32(insn, 16, 5);
6422     bool o0 = extract32(insn, 15, 1);
6423     bool o1 = extract32(insn, 21, 1);
6424 
6425     if (mos) {
6426         unallocated_encoding(s);
6427         return;
6428     }
6429 
6430     switch (type) {
6431     case 0:
6432         if (!fp_access_check(s)) {
6433             return;
6434         }
6435         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6436         break;
6437     case 1:
6438         if (!fp_access_check(s)) {
6439             return;
6440         }
6441         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6442         break;
6443     case 3:
6444         if (!dc_isar_feature(aa64_fp16, s)) {
6445             unallocated_encoding(s);
6446             return;
6447         }
6448         if (!fp_access_check(s)) {
6449             return;
6450         }
6451         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6452         break;
6453     default:
6454         unallocated_encoding(s);
6455     }
6456 }
6457 
6458 /* Floating point immediate
6459  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
6460  * +---+---+---+-----------+------+---+------------+-------+------+------+
6461  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
6462  * +---+---+---+-----------+------+---+------------+-------+------+------+
6463  */
6464 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6465 {
6466     int rd = extract32(insn, 0, 5);
6467     int imm5 = extract32(insn, 5, 5);
6468     int imm8 = extract32(insn, 13, 8);
6469     int type = extract32(insn, 22, 2);
6470     int mos = extract32(insn, 29, 3);
6471     uint64_t imm;
6472     MemOp sz;
6473 
6474     if (mos || imm5) {
6475         unallocated_encoding(s);
6476         return;
6477     }
6478 
6479     switch (type) {
6480     case 0:
6481         sz = MO_32;
6482         break;
6483     case 1:
6484         sz = MO_64;
6485         break;
6486     case 3:
6487         sz = MO_16;
6488         if (dc_isar_feature(aa64_fp16, s)) {
6489             break;
6490         }
6491         /* fallthru */
6492     default:
6493         unallocated_encoding(s);
6494         return;
6495     }
6496 
6497     if (!fp_access_check(s)) {
6498         return;
6499     }
6500 
6501     imm = vfp_expand_imm(sz, imm8);
6502     write_fp_dreg(s, rd, tcg_constant_i64(imm));
6503 }
6504 
6505 /* Handle floating point <=> fixed point conversions. Note that we can
6506  * also deal with fp <=> integer conversions as a special case (scale == 64)
6507  * OPTME: consider handling that special case specially or at least skipping
6508  * the call to scalbn in the helpers for zero shifts.
6509  */
6510 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6511                            bool itof, int rmode, int scale, int sf, int type)
6512 {
6513     bool is_signed = !(opcode & 1);
6514     TCGv_ptr tcg_fpstatus;
6515     TCGv_i32 tcg_shift, tcg_single;
6516     TCGv_i64 tcg_double;
6517 
6518     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
6519 
6520     tcg_shift = tcg_constant_i32(64 - scale);
6521 
6522     if (itof) {
6523         TCGv_i64 tcg_int = cpu_reg(s, rn);
6524         if (!sf) {
6525             TCGv_i64 tcg_extend = tcg_temp_new_i64();
6526 
6527             if (is_signed) {
6528                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6529             } else {
6530                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6531             }
6532 
6533             tcg_int = tcg_extend;
6534         }
6535 
6536         switch (type) {
6537         case 1: /* float64 */
6538             tcg_double = tcg_temp_new_i64();
6539             if (is_signed) {
6540                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6541                                      tcg_shift, tcg_fpstatus);
6542             } else {
6543                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6544                                      tcg_shift, tcg_fpstatus);
6545             }
6546             write_fp_dreg(s, rd, tcg_double);
6547             break;
6548 
6549         case 0: /* float32 */
6550             tcg_single = tcg_temp_new_i32();
6551             if (is_signed) {
6552                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6553                                      tcg_shift, tcg_fpstatus);
6554             } else {
6555                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6556                                      tcg_shift, tcg_fpstatus);
6557             }
6558             write_fp_sreg(s, rd, tcg_single);
6559             break;
6560 
6561         case 3: /* float16 */
6562             tcg_single = tcg_temp_new_i32();
6563             if (is_signed) {
6564                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6565                                      tcg_shift, tcg_fpstatus);
6566             } else {
6567                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6568                                      tcg_shift, tcg_fpstatus);
6569             }
6570             write_fp_sreg(s, rd, tcg_single);
6571             break;
6572 
6573         default:
6574             g_assert_not_reached();
6575         }
6576     } else {
6577         TCGv_i64 tcg_int = cpu_reg(s, rd);
6578         TCGv_i32 tcg_rmode;
6579 
6580         if (extract32(opcode, 2, 1)) {
6581             /* There are too many rounding modes to all fit into rmode,
6582              * so FCVTA[US] is a special case.
6583              */
6584             rmode = FPROUNDING_TIEAWAY;
6585         }
6586 
6587         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
6588 
6589         switch (type) {
6590         case 1: /* float64 */
6591             tcg_double = read_fp_dreg(s, rn);
6592             if (is_signed) {
6593                 if (!sf) {
6594                     gen_helper_vfp_tosld(tcg_int, tcg_double,
6595                                          tcg_shift, tcg_fpstatus);
6596                 } else {
6597                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
6598                                          tcg_shift, tcg_fpstatus);
6599                 }
6600             } else {
6601                 if (!sf) {
6602                     gen_helper_vfp_tould(tcg_int, tcg_double,
6603                                          tcg_shift, tcg_fpstatus);
6604                 } else {
6605                     gen_helper_vfp_touqd(tcg_int, tcg_double,
6606                                          tcg_shift, tcg_fpstatus);
6607                 }
6608             }
6609             if (!sf) {
6610                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6611             }
6612             break;
6613 
6614         case 0: /* float32 */
6615             tcg_single = read_fp_sreg(s, rn);
6616             if (sf) {
6617                 if (is_signed) {
6618                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
6619                                          tcg_shift, tcg_fpstatus);
6620                 } else {
6621                     gen_helper_vfp_touqs(tcg_int, tcg_single,
6622                                          tcg_shift, tcg_fpstatus);
6623                 }
6624             } else {
6625                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6626                 if (is_signed) {
6627                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
6628                                          tcg_shift, tcg_fpstatus);
6629                 } else {
6630                     gen_helper_vfp_touls(tcg_dest, tcg_single,
6631                                          tcg_shift, tcg_fpstatus);
6632                 }
6633                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6634             }
6635             break;
6636 
6637         case 3: /* float16 */
6638             tcg_single = read_fp_sreg(s, rn);
6639             if (sf) {
6640                 if (is_signed) {
6641                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
6642                                          tcg_shift, tcg_fpstatus);
6643                 } else {
6644                     gen_helper_vfp_touqh(tcg_int, tcg_single,
6645                                          tcg_shift, tcg_fpstatus);
6646                 }
6647             } else {
6648                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6649                 if (is_signed) {
6650                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
6651                                          tcg_shift, tcg_fpstatus);
6652                 } else {
6653                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
6654                                          tcg_shift, tcg_fpstatus);
6655                 }
6656                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6657             }
6658             break;
6659 
6660         default:
6661             g_assert_not_reached();
6662         }
6663 
6664         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
6665     }
6666 }
6667 
6668 /* Floating point <-> fixed point conversions
6669  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
6670  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6671  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
6672  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6673  */
6674 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
6675 {
6676     int rd = extract32(insn, 0, 5);
6677     int rn = extract32(insn, 5, 5);
6678     int scale = extract32(insn, 10, 6);
6679     int opcode = extract32(insn, 16, 3);
6680     int rmode = extract32(insn, 19, 2);
6681     int type = extract32(insn, 22, 2);
6682     bool sbit = extract32(insn, 29, 1);
6683     bool sf = extract32(insn, 31, 1);
6684     bool itof;
6685 
6686     if (sbit || (!sf && scale < 32)) {
6687         unallocated_encoding(s);
6688         return;
6689     }
6690 
6691     switch (type) {
6692     case 0: /* float32 */
6693     case 1: /* float64 */
6694         break;
6695     case 3: /* float16 */
6696         if (dc_isar_feature(aa64_fp16, s)) {
6697             break;
6698         }
6699         /* fallthru */
6700     default:
6701         unallocated_encoding(s);
6702         return;
6703     }
6704 
6705     switch ((rmode << 3) | opcode) {
6706     case 0x2: /* SCVTF */
6707     case 0x3: /* UCVTF */
6708         itof = true;
6709         break;
6710     case 0x18: /* FCVTZS */
6711     case 0x19: /* FCVTZU */
6712         itof = false;
6713         break;
6714     default:
6715         unallocated_encoding(s);
6716         return;
6717     }
6718 
6719     if (!fp_access_check(s)) {
6720         return;
6721     }
6722 
6723     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
6724 }
6725 
6726 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
6727 {
6728     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
6729      * without conversion.
6730      */
6731 
6732     if (itof) {
6733         TCGv_i64 tcg_rn = cpu_reg(s, rn);
6734         TCGv_i64 tmp;
6735 
6736         switch (type) {
6737         case 0:
6738             /* 32 bit */
6739             tmp = tcg_temp_new_i64();
6740             tcg_gen_ext32u_i64(tmp, tcg_rn);
6741             write_fp_dreg(s, rd, tmp);
6742             break;
6743         case 1:
6744             /* 64 bit */
6745             write_fp_dreg(s, rd, tcg_rn);
6746             break;
6747         case 2:
6748             /* 64 bit to top half. */
6749             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
6750             clear_vec_high(s, true, rd);
6751             break;
6752         case 3:
6753             /* 16 bit */
6754             tmp = tcg_temp_new_i64();
6755             tcg_gen_ext16u_i64(tmp, tcg_rn);
6756             write_fp_dreg(s, rd, tmp);
6757             break;
6758         default:
6759             g_assert_not_reached();
6760         }
6761     } else {
6762         TCGv_i64 tcg_rd = cpu_reg(s, rd);
6763 
6764         switch (type) {
6765         case 0:
6766             /* 32 bit */
6767             tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
6768             break;
6769         case 1:
6770             /* 64 bit */
6771             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
6772             break;
6773         case 2:
6774             /* 64 bits from top half */
6775             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
6776             break;
6777         case 3:
6778             /* 16 bit */
6779             tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
6780             break;
6781         default:
6782             g_assert_not_reached();
6783         }
6784     }
6785 }
6786 
6787 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
6788 {
6789     TCGv_i64 t = read_fp_dreg(s, rn);
6790     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
6791 
6792     gen_helper_fjcvtzs(t, t, fpstatus);
6793 
6794     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
6795     tcg_gen_extrh_i64_i32(cpu_ZF, t);
6796     tcg_gen_movi_i32(cpu_CF, 0);
6797     tcg_gen_movi_i32(cpu_NF, 0);
6798     tcg_gen_movi_i32(cpu_VF, 0);
6799 }
6800 
6801 /* Floating point <-> integer conversions
6802  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
6803  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
6804  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
6805  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
6806  */
6807 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
6808 {
6809     int rd = extract32(insn, 0, 5);
6810     int rn = extract32(insn, 5, 5);
6811     int opcode = extract32(insn, 16, 3);
6812     int rmode = extract32(insn, 19, 2);
6813     int type = extract32(insn, 22, 2);
6814     bool sbit = extract32(insn, 29, 1);
6815     bool sf = extract32(insn, 31, 1);
6816     bool itof = false;
6817 
6818     if (sbit) {
6819         goto do_unallocated;
6820     }
6821 
6822     switch (opcode) {
6823     case 2: /* SCVTF */
6824     case 3: /* UCVTF */
6825         itof = true;
6826         /* fallthru */
6827     case 4: /* FCVTAS */
6828     case 5: /* FCVTAU */
6829         if (rmode != 0) {
6830             goto do_unallocated;
6831         }
6832         /* fallthru */
6833     case 0: /* FCVT[NPMZ]S */
6834     case 1: /* FCVT[NPMZ]U */
6835         switch (type) {
6836         case 0: /* float32 */
6837         case 1: /* float64 */
6838             break;
6839         case 3: /* float16 */
6840             if (!dc_isar_feature(aa64_fp16, s)) {
6841                 goto do_unallocated;
6842             }
6843             break;
6844         default:
6845             goto do_unallocated;
6846         }
6847         if (!fp_access_check(s)) {
6848             return;
6849         }
6850         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
6851         break;
6852 
6853     default:
6854         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
6855         case 0b01100110: /* FMOV half <-> 32-bit int */
6856         case 0b01100111:
6857         case 0b11100110: /* FMOV half <-> 64-bit int */
6858         case 0b11100111:
6859             if (!dc_isar_feature(aa64_fp16, s)) {
6860                 goto do_unallocated;
6861             }
6862             /* fallthru */
6863         case 0b00000110: /* FMOV 32-bit */
6864         case 0b00000111:
6865         case 0b10100110: /* FMOV 64-bit */
6866         case 0b10100111:
6867         case 0b11001110: /* FMOV top half of 128-bit */
6868         case 0b11001111:
6869             if (!fp_access_check(s)) {
6870                 return;
6871             }
6872             itof = opcode & 1;
6873             handle_fmov(s, rd, rn, type, itof);
6874             break;
6875 
6876         case 0b00111110: /* FJCVTZS */
6877             if (!dc_isar_feature(aa64_jscvt, s)) {
6878                 goto do_unallocated;
6879             } else if (fp_access_check(s)) {
6880                 handle_fjcvtzs(s, rd, rn);
6881             }
6882             break;
6883 
6884         default:
6885         do_unallocated:
6886             unallocated_encoding(s);
6887             return;
6888         }
6889         break;
6890     }
6891 }
6892 
6893 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
6894  *   31  30  29 28     25 24                          0
6895  * +---+---+---+---------+-----------------------------+
6896  * |   | 0 |   | 1 1 1 1 |                             |
6897  * +---+---+---+---------+-----------------------------+
6898  */
6899 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
6900 {
6901     if (extract32(insn, 24, 1)) {
6902         /* Floating point data-processing (3 source) */
6903         disas_fp_3src(s, insn);
6904     } else if (extract32(insn, 21, 1) == 0) {
6905         /* Floating point to fixed point conversions */
6906         disas_fp_fixed_conv(s, insn);
6907     } else {
6908         switch (extract32(insn, 10, 2)) {
6909         case 1:
6910             /* Floating point conditional compare */
6911             disas_fp_ccomp(s, insn);
6912             break;
6913         case 2:
6914             /* Floating point data-processing (2 source) */
6915             disas_fp_2src(s, insn);
6916             break;
6917         case 3:
6918             /* Floating point conditional select */
6919             disas_fp_csel(s, insn);
6920             break;
6921         case 0:
6922             switch (ctz32(extract32(insn, 12, 4))) {
6923             case 0: /* [15:12] == xxx1 */
6924                 /* Floating point immediate */
6925                 disas_fp_imm(s, insn);
6926                 break;
6927             case 1: /* [15:12] == xx10 */
6928                 /* Floating point compare */
6929                 disas_fp_compare(s, insn);
6930                 break;
6931             case 2: /* [15:12] == x100 */
6932                 /* Floating point data-processing (1 source) */
6933                 disas_fp_1src(s, insn);
6934                 break;
6935             case 3: /* [15:12] == 1000 */
6936                 unallocated_encoding(s);
6937                 break;
6938             default: /* [15:12] == 0000 */
6939                 /* Floating point <-> integer conversions */
6940                 disas_fp_int_conv(s, insn);
6941                 break;
6942             }
6943             break;
6944         }
6945     }
6946 }
6947 
6948 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
6949                      int pos)
6950 {
6951     /* Extract 64 bits from the middle of two concatenated 64 bit
6952      * vector register slices left:right. The extracted bits start
6953      * at 'pos' bits into the right (least significant) side.
6954      * We return the result in tcg_right, and guarantee not to
6955      * trash tcg_left.
6956      */
6957     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
6958     assert(pos > 0 && pos < 64);
6959 
6960     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
6961     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
6962     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
6963 }
6964 
6965 /* EXT
6966  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
6967  * +---+---+-------------+-----+---+------+---+------+---+------+------+
6968  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
6969  * +---+---+-------------+-----+---+------+---+------+---+------+------+
6970  */
6971 static void disas_simd_ext(DisasContext *s, uint32_t insn)
6972 {
6973     int is_q = extract32(insn, 30, 1);
6974     int op2 = extract32(insn, 22, 2);
6975     int imm4 = extract32(insn, 11, 4);
6976     int rm = extract32(insn, 16, 5);
6977     int rn = extract32(insn, 5, 5);
6978     int rd = extract32(insn, 0, 5);
6979     int pos = imm4 << 3;
6980     TCGv_i64 tcg_resl, tcg_resh;
6981 
6982     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
6983         unallocated_encoding(s);
6984         return;
6985     }
6986 
6987     if (!fp_access_check(s)) {
6988         return;
6989     }
6990 
6991     tcg_resh = tcg_temp_new_i64();
6992     tcg_resl = tcg_temp_new_i64();
6993 
6994     /* Vd gets bits starting at pos bits into Vm:Vn. This is
6995      * either extracting 128 bits from a 128:128 concatenation, or
6996      * extracting 64 bits from a 64:64 concatenation.
6997      */
6998     if (!is_q) {
6999         read_vec_element(s, tcg_resl, rn, 0, MO_64);
7000         if (pos != 0) {
7001             read_vec_element(s, tcg_resh, rm, 0, MO_64);
7002             do_ext64(s, tcg_resh, tcg_resl, pos);
7003         }
7004     } else {
7005         TCGv_i64 tcg_hh;
7006         typedef struct {
7007             int reg;
7008             int elt;
7009         } EltPosns;
7010         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7011         EltPosns *elt = eltposns;
7012 
7013         if (pos >= 64) {
7014             elt++;
7015             pos -= 64;
7016         }
7017 
7018         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7019         elt++;
7020         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7021         elt++;
7022         if (pos != 0) {
7023             do_ext64(s, tcg_resh, tcg_resl, pos);
7024             tcg_hh = tcg_temp_new_i64();
7025             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7026             do_ext64(s, tcg_hh, tcg_resh, pos);
7027         }
7028     }
7029 
7030     write_vec_element(s, tcg_resl, rd, 0, MO_64);
7031     if (is_q) {
7032         write_vec_element(s, tcg_resh, rd, 1, MO_64);
7033     }
7034     clear_vec_high(s, is_q, rd);
7035 }
7036 
7037 /* TBL/TBX
7038  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
7039  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7040  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
7041  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7042  */
7043 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7044 {
7045     int op2 = extract32(insn, 22, 2);
7046     int is_q = extract32(insn, 30, 1);
7047     int rm = extract32(insn, 16, 5);
7048     int rn = extract32(insn, 5, 5);
7049     int rd = extract32(insn, 0, 5);
7050     int is_tbx = extract32(insn, 12, 1);
7051     int len = (extract32(insn, 13, 2) + 1) * 16;
7052 
7053     if (op2 != 0) {
7054         unallocated_encoding(s);
7055         return;
7056     }
7057 
7058     if (!fp_access_check(s)) {
7059         return;
7060     }
7061 
7062     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7063                        vec_full_reg_offset(s, rm), cpu_env,
7064                        is_q ? 16 : 8, vec_full_reg_size(s),
7065                        (len << 6) | (is_tbx << 5) | rn,
7066                        gen_helper_simd_tblx);
7067 }
7068 
7069 /* ZIP/UZP/TRN
7070  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
7071  * +---+---+-------------+------+---+------+---+------------------+------+
7072  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
7073  * +---+---+-------------+------+---+------+---+------------------+------+
7074  */
7075 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7076 {
7077     int rd = extract32(insn, 0, 5);
7078     int rn = extract32(insn, 5, 5);
7079     int rm = extract32(insn, 16, 5);
7080     int size = extract32(insn, 22, 2);
7081     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7082      * bit 2 indicates 1 vs 2 variant of the insn.
7083      */
7084     int opcode = extract32(insn, 12, 2);
7085     bool part = extract32(insn, 14, 1);
7086     bool is_q = extract32(insn, 30, 1);
7087     int esize = 8 << size;
7088     int i;
7089     int datasize = is_q ? 128 : 64;
7090     int elements = datasize / esize;
7091     TCGv_i64 tcg_res[2], tcg_ele;
7092 
7093     if (opcode == 0 || (size == 3 && !is_q)) {
7094         unallocated_encoding(s);
7095         return;
7096     }
7097 
7098     if (!fp_access_check(s)) {
7099         return;
7100     }
7101 
7102     tcg_res[0] = tcg_temp_new_i64();
7103     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
7104     tcg_ele = tcg_temp_new_i64();
7105 
7106     for (i = 0; i < elements; i++) {
7107         int o, w;
7108 
7109         switch (opcode) {
7110         case 1: /* UZP1/2 */
7111         {
7112             int midpoint = elements / 2;
7113             if (i < midpoint) {
7114                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
7115             } else {
7116                 read_vec_element(s, tcg_ele, rm,
7117                                  2 * (i - midpoint) + part, size);
7118             }
7119             break;
7120         }
7121         case 2: /* TRN1/2 */
7122             if (i & 1) {
7123                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
7124             } else {
7125                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
7126             }
7127             break;
7128         case 3: /* ZIP1/2 */
7129         {
7130             int base = part * elements / 2;
7131             if (i & 1) {
7132                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
7133             } else {
7134                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
7135             }
7136             break;
7137         }
7138         default:
7139             g_assert_not_reached();
7140         }
7141 
7142         w = (i * esize) / 64;
7143         o = (i * esize) % 64;
7144         if (o == 0) {
7145             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
7146         } else {
7147             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
7148             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
7149         }
7150     }
7151 
7152     for (i = 0; i <= is_q; ++i) {
7153         write_vec_element(s, tcg_res[i], rd, i, MO_64);
7154     }
7155     clear_vec_high(s, is_q, rd);
7156 }
7157 
7158 /*
7159  * do_reduction_op helper
7160  *
7161  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7162  * important for correct NaN propagation that we do these
7163  * operations in exactly the order specified by the pseudocode.
7164  *
7165  * This is a recursive function, TCG temps should be freed by the
7166  * calling function once it is done with the values.
7167  */
7168 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7169                                 int esize, int size, int vmap, TCGv_ptr fpst)
7170 {
7171     if (esize == size) {
7172         int element;
7173         MemOp msize = esize == 16 ? MO_16 : MO_32;
7174         TCGv_i32 tcg_elem;
7175 
7176         /* We should have one register left here */
7177         assert(ctpop8(vmap) == 1);
7178         element = ctz32(vmap);
7179         assert(element < 8);
7180 
7181         tcg_elem = tcg_temp_new_i32();
7182         read_vec_element_i32(s, tcg_elem, rn, element, msize);
7183         return tcg_elem;
7184     } else {
7185         int bits = size / 2;
7186         int shift = ctpop8(vmap) / 2;
7187         int vmap_lo = (vmap >> shift) & vmap;
7188         int vmap_hi = (vmap & ~vmap_lo);
7189         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7190 
7191         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7192         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7193         tcg_res = tcg_temp_new_i32();
7194 
7195         switch (fpopcode) {
7196         case 0x0c: /* fmaxnmv half-precision */
7197             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7198             break;
7199         case 0x0f: /* fmaxv half-precision */
7200             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7201             break;
7202         case 0x1c: /* fminnmv half-precision */
7203             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7204             break;
7205         case 0x1f: /* fminv half-precision */
7206             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7207             break;
7208         case 0x2c: /* fmaxnmv */
7209             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7210             break;
7211         case 0x2f: /* fmaxv */
7212             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7213             break;
7214         case 0x3c: /* fminnmv */
7215             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7216             break;
7217         case 0x3f: /* fminv */
7218             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7219             break;
7220         default:
7221             g_assert_not_reached();
7222         }
7223         return tcg_res;
7224     }
7225 }
7226 
7227 /* AdvSIMD across lanes
7228  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7229  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7230  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7231  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7232  */
7233 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7234 {
7235     int rd = extract32(insn, 0, 5);
7236     int rn = extract32(insn, 5, 5);
7237     int size = extract32(insn, 22, 2);
7238     int opcode = extract32(insn, 12, 5);
7239     bool is_q = extract32(insn, 30, 1);
7240     bool is_u = extract32(insn, 29, 1);
7241     bool is_fp = false;
7242     bool is_min = false;
7243     int esize;
7244     int elements;
7245     int i;
7246     TCGv_i64 tcg_res, tcg_elt;
7247 
7248     switch (opcode) {
7249     case 0x1b: /* ADDV */
7250         if (is_u) {
7251             unallocated_encoding(s);
7252             return;
7253         }
7254         /* fall through */
7255     case 0x3: /* SADDLV, UADDLV */
7256     case 0xa: /* SMAXV, UMAXV */
7257     case 0x1a: /* SMINV, UMINV */
7258         if (size == 3 || (size == 2 && !is_q)) {
7259             unallocated_encoding(s);
7260             return;
7261         }
7262         break;
7263     case 0xc: /* FMAXNMV, FMINNMV */
7264     case 0xf: /* FMAXV, FMINV */
7265         /* Bit 1 of size field encodes min vs max and the actual size
7266          * depends on the encoding of the U bit. If not set (and FP16
7267          * enabled) then we do half-precision float instead of single
7268          * precision.
7269          */
7270         is_min = extract32(size, 1, 1);
7271         is_fp = true;
7272         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7273             size = 1;
7274         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7275             unallocated_encoding(s);
7276             return;
7277         } else {
7278             size = 2;
7279         }
7280         break;
7281     default:
7282         unallocated_encoding(s);
7283         return;
7284     }
7285 
7286     if (!fp_access_check(s)) {
7287         return;
7288     }
7289 
7290     esize = 8 << size;
7291     elements = (is_q ? 128 : 64) / esize;
7292 
7293     tcg_res = tcg_temp_new_i64();
7294     tcg_elt = tcg_temp_new_i64();
7295 
7296     /* These instructions operate across all lanes of a vector
7297      * to produce a single result. We can guarantee that a 64
7298      * bit intermediate is sufficient:
7299      *  + for [US]ADDLV the maximum element size is 32 bits, and
7300      *    the result type is 64 bits
7301      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7302      *    same as the element size, which is 32 bits at most
7303      * For the integer operations we can choose to work at 64
7304      * or 32 bits and truncate at the end; for simplicity
7305      * we use 64 bits always. The floating point
7306      * ops do require 32 bit intermediates, though.
7307      */
7308     if (!is_fp) {
7309         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7310 
7311         for (i = 1; i < elements; i++) {
7312             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7313 
7314             switch (opcode) {
7315             case 0x03: /* SADDLV / UADDLV */
7316             case 0x1b: /* ADDV */
7317                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7318                 break;
7319             case 0x0a: /* SMAXV / UMAXV */
7320                 if (is_u) {
7321                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7322                 } else {
7323                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7324                 }
7325                 break;
7326             case 0x1a: /* SMINV / UMINV */
7327                 if (is_u) {
7328                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7329                 } else {
7330                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7331                 }
7332                 break;
7333             default:
7334                 g_assert_not_reached();
7335             }
7336 
7337         }
7338     } else {
7339         /* Floating point vector reduction ops which work across 32
7340          * bit (single) or 16 bit (half-precision) intermediates.
7341          * Note that correct NaN propagation requires that we do these
7342          * operations in exactly the order specified by the pseudocode.
7343          */
7344         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7345         int fpopcode = opcode | is_min << 4 | is_u << 5;
7346         int vmap = (1 << elements) - 1;
7347         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7348                                              (is_q ? 128 : 64), vmap, fpst);
7349         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7350     }
7351 
7352     /* Now truncate the result to the width required for the final output */
7353     if (opcode == 0x03) {
7354         /* SADDLV, UADDLV: result is 2*esize */
7355         size++;
7356     }
7357 
7358     switch (size) {
7359     case 0:
7360         tcg_gen_ext8u_i64(tcg_res, tcg_res);
7361         break;
7362     case 1:
7363         tcg_gen_ext16u_i64(tcg_res, tcg_res);
7364         break;
7365     case 2:
7366         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7367         break;
7368     case 3:
7369         break;
7370     default:
7371         g_assert_not_reached();
7372     }
7373 
7374     write_fp_dreg(s, rd, tcg_res);
7375 }
7376 
7377 /* DUP (Element, Vector)
7378  *
7379  *  31  30   29              21 20    16 15        10  9    5 4    0
7380  * +---+---+-------------------+--------+-------------+------+------+
7381  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7382  * +---+---+-------------------+--------+-------------+------+------+
7383  *
7384  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7385  */
7386 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7387                              int imm5)
7388 {
7389     int size = ctz32(imm5);
7390     int index;
7391 
7392     if (size > 3 || (size == 3 && !is_q)) {
7393         unallocated_encoding(s);
7394         return;
7395     }
7396 
7397     if (!fp_access_check(s)) {
7398         return;
7399     }
7400 
7401     index = imm5 >> (size + 1);
7402     tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7403                          vec_reg_offset(s, rn, index, size),
7404                          is_q ? 16 : 8, vec_full_reg_size(s));
7405 }
7406 
7407 /* DUP (element, scalar)
7408  *  31                   21 20    16 15        10  9    5 4    0
7409  * +-----------------------+--------+-------------+------+------+
7410  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7411  * +-----------------------+--------+-------------+------+------+
7412  */
7413 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7414                               int imm5)
7415 {
7416     int size = ctz32(imm5);
7417     int index;
7418     TCGv_i64 tmp;
7419 
7420     if (size > 3) {
7421         unallocated_encoding(s);
7422         return;
7423     }
7424 
7425     if (!fp_access_check(s)) {
7426         return;
7427     }
7428 
7429     index = imm5 >> (size + 1);
7430 
7431     /* This instruction just extracts the specified element and
7432      * zero-extends it into the bottom of the destination register.
7433      */
7434     tmp = tcg_temp_new_i64();
7435     read_vec_element(s, tmp, rn, index, size);
7436     write_fp_dreg(s, rd, tmp);
7437 }
7438 
7439 /* DUP (General)
7440  *
7441  *  31  30   29              21 20    16 15        10  9    5 4    0
7442  * +---+---+-------------------+--------+-------------+------+------+
7443  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
7444  * +---+---+-------------------+--------+-------------+------+------+
7445  *
7446  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7447  */
7448 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7449                              int imm5)
7450 {
7451     int size = ctz32(imm5);
7452     uint32_t dofs, oprsz, maxsz;
7453 
7454     if (size > 3 || ((size == 3) && !is_q)) {
7455         unallocated_encoding(s);
7456         return;
7457     }
7458 
7459     if (!fp_access_check(s)) {
7460         return;
7461     }
7462 
7463     dofs = vec_full_reg_offset(s, rd);
7464     oprsz = is_q ? 16 : 8;
7465     maxsz = vec_full_reg_size(s);
7466 
7467     tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7468 }
7469 
7470 /* INS (Element)
7471  *
7472  *  31                   21 20    16 15  14    11  10 9    5 4    0
7473  * +-----------------------+--------+------------+---+------+------+
7474  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
7475  * +-----------------------+--------+------------+---+------+------+
7476  *
7477  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7478  * index: encoded in imm5<4:size+1>
7479  */
7480 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7481                              int imm4, int imm5)
7482 {
7483     int size = ctz32(imm5);
7484     int src_index, dst_index;
7485     TCGv_i64 tmp;
7486 
7487     if (size > 3) {
7488         unallocated_encoding(s);
7489         return;
7490     }
7491 
7492     if (!fp_access_check(s)) {
7493         return;
7494     }
7495 
7496     dst_index = extract32(imm5, 1+size, 5);
7497     src_index = extract32(imm4, size, 4);
7498 
7499     tmp = tcg_temp_new_i64();
7500 
7501     read_vec_element(s, tmp, rn, src_index, size);
7502     write_vec_element(s, tmp, rd, dst_index, size);
7503 
7504     /* INS is considered a 128-bit write for SVE. */
7505     clear_vec_high(s, true, rd);
7506 }
7507 
7508 
7509 /* INS (General)
7510  *
7511  *  31                   21 20    16 15        10  9    5 4    0
7512  * +-----------------------+--------+-------------+------+------+
7513  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
7514  * +-----------------------+--------+-------------+------+------+
7515  *
7516  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7517  * index: encoded in imm5<4:size+1>
7518  */
7519 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7520 {
7521     int size = ctz32(imm5);
7522     int idx;
7523 
7524     if (size > 3) {
7525         unallocated_encoding(s);
7526         return;
7527     }
7528 
7529     if (!fp_access_check(s)) {
7530         return;
7531     }
7532 
7533     idx = extract32(imm5, 1 + size, 4 - size);
7534     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7535 
7536     /* INS is considered a 128-bit write for SVE. */
7537     clear_vec_high(s, true, rd);
7538 }
7539 
7540 /*
7541  * UMOV (General)
7542  * SMOV (General)
7543  *
7544  *  31  30   29              21 20    16 15    12   10 9    5 4    0
7545  * +---+---+-------------------+--------+-------------+------+------+
7546  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
7547  * +---+---+-------------------+--------+-------------+------+------+
7548  *
7549  * U: unsigned when set
7550  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7551  */
7552 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7553                                   int rn, int rd, int imm5)
7554 {
7555     int size = ctz32(imm5);
7556     int element;
7557     TCGv_i64 tcg_rd;
7558 
7559     /* Check for UnallocatedEncodings */
7560     if (is_signed) {
7561         if (size > 2 || (size == 2 && !is_q)) {
7562             unallocated_encoding(s);
7563             return;
7564         }
7565     } else {
7566         if (size > 3
7567             || (size < 3 && is_q)
7568             || (size == 3 && !is_q)) {
7569             unallocated_encoding(s);
7570             return;
7571         }
7572     }
7573 
7574     if (!fp_access_check(s)) {
7575         return;
7576     }
7577 
7578     element = extract32(imm5, 1+size, 4);
7579 
7580     tcg_rd = cpu_reg(s, rd);
7581     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7582     if (is_signed && !is_q) {
7583         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7584     }
7585 }
7586 
7587 /* AdvSIMD copy
7588  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
7589  * +---+---+----+-----------------+------+---+------+---+------+------+
7590  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7591  * +---+---+----+-----------------+------+---+------+---+------+------+
7592  */
7593 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7594 {
7595     int rd = extract32(insn, 0, 5);
7596     int rn = extract32(insn, 5, 5);
7597     int imm4 = extract32(insn, 11, 4);
7598     int op = extract32(insn, 29, 1);
7599     int is_q = extract32(insn, 30, 1);
7600     int imm5 = extract32(insn, 16, 5);
7601 
7602     if (op) {
7603         if (is_q) {
7604             /* INS (element) */
7605             handle_simd_inse(s, rd, rn, imm4, imm5);
7606         } else {
7607             unallocated_encoding(s);
7608         }
7609     } else {
7610         switch (imm4) {
7611         case 0:
7612             /* DUP (element - vector) */
7613             handle_simd_dupe(s, is_q, rd, rn, imm5);
7614             break;
7615         case 1:
7616             /* DUP (general) */
7617             handle_simd_dupg(s, is_q, rd, rn, imm5);
7618             break;
7619         case 3:
7620             if (is_q) {
7621                 /* INS (general) */
7622                 handle_simd_insg(s, rd, rn, imm5);
7623             } else {
7624                 unallocated_encoding(s);
7625             }
7626             break;
7627         case 5:
7628         case 7:
7629             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
7630             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
7631             break;
7632         default:
7633             unallocated_encoding(s);
7634             break;
7635         }
7636     }
7637 }
7638 
7639 /* AdvSIMD modified immediate
7640  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
7641  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7642  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
7643  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7644  *
7645  * There are a number of operations that can be carried out here:
7646  *   MOVI - move (shifted) imm into register
7647  *   MVNI - move inverted (shifted) imm into register
7648  *   ORR  - bitwise OR of (shifted) imm with register
7649  *   BIC  - bitwise clear of (shifted) imm with register
7650  * With ARMv8.2 we also have:
7651  *   FMOV half-precision
7652  */
7653 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
7654 {
7655     int rd = extract32(insn, 0, 5);
7656     int cmode = extract32(insn, 12, 4);
7657     int o2 = extract32(insn, 11, 1);
7658     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
7659     bool is_neg = extract32(insn, 29, 1);
7660     bool is_q = extract32(insn, 30, 1);
7661     uint64_t imm = 0;
7662 
7663     if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
7664         /* Check for FMOV (vector, immediate) - half-precision */
7665         if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
7666             unallocated_encoding(s);
7667             return;
7668         }
7669     }
7670 
7671     if (!fp_access_check(s)) {
7672         return;
7673     }
7674 
7675     if (cmode == 15 && o2 && !is_neg) {
7676         /* FMOV (vector, immediate) - half-precision */
7677         imm = vfp_expand_imm(MO_16, abcdefgh);
7678         /* now duplicate across the lanes */
7679         imm = dup_const(MO_16, imm);
7680     } else {
7681         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
7682     }
7683 
7684     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
7685         /* MOVI or MVNI, with MVNI negation handled above.  */
7686         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
7687                              vec_full_reg_size(s), imm);
7688     } else {
7689         /* ORR or BIC, with BIC negation to AND handled above.  */
7690         if (is_neg) {
7691             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
7692         } else {
7693             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
7694         }
7695     }
7696 }
7697 
7698 /* AdvSIMD scalar copy
7699  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
7700  * +-----+----+-----------------+------+---+------+---+------+------+
7701  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7702  * +-----+----+-----------------+------+---+------+---+------+------+
7703  */
7704 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
7705 {
7706     int rd = extract32(insn, 0, 5);
7707     int rn = extract32(insn, 5, 5);
7708     int imm4 = extract32(insn, 11, 4);
7709     int imm5 = extract32(insn, 16, 5);
7710     int op = extract32(insn, 29, 1);
7711 
7712     if (op != 0 || imm4 != 0) {
7713         unallocated_encoding(s);
7714         return;
7715     }
7716 
7717     /* DUP (element, scalar) */
7718     handle_simd_dupes(s, rd, rn, imm5);
7719 }
7720 
7721 /* AdvSIMD scalar pairwise
7722  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7723  * +-----+---+-----------+------+-----------+--------+-----+------+------+
7724  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7725  * +-----+---+-----------+------+-----------+--------+-----+------+------+
7726  */
7727 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
7728 {
7729     int u = extract32(insn, 29, 1);
7730     int size = extract32(insn, 22, 2);
7731     int opcode = extract32(insn, 12, 5);
7732     int rn = extract32(insn, 5, 5);
7733     int rd = extract32(insn, 0, 5);
7734     TCGv_ptr fpst;
7735 
7736     /* For some ops (the FP ones), size[1] is part of the encoding.
7737      * For ADDP strictly it is not but size[1] is always 1 for valid
7738      * encodings.
7739      */
7740     opcode |= (extract32(size, 1, 1) << 5);
7741 
7742     switch (opcode) {
7743     case 0x3b: /* ADDP */
7744         if (u || size != 3) {
7745             unallocated_encoding(s);
7746             return;
7747         }
7748         if (!fp_access_check(s)) {
7749             return;
7750         }
7751 
7752         fpst = NULL;
7753         break;
7754     case 0xc: /* FMAXNMP */
7755     case 0xd: /* FADDP */
7756     case 0xf: /* FMAXP */
7757     case 0x2c: /* FMINNMP */
7758     case 0x2f: /* FMINP */
7759         /* FP op, size[0] is 32 or 64 bit*/
7760         if (!u) {
7761             if (!dc_isar_feature(aa64_fp16, s)) {
7762                 unallocated_encoding(s);
7763                 return;
7764             } else {
7765                 size = MO_16;
7766             }
7767         } else {
7768             size = extract32(size, 0, 1) ? MO_64 : MO_32;
7769         }
7770 
7771         if (!fp_access_check(s)) {
7772             return;
7773         }
7774 
7775         fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7776         break;
7777     default:
7778         unallocated_encoding(s);
7779         return;
7780     }
7781 
7782     if (size == MO_64) {
7783         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7784         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7785         TCGv_i64 tcg_res = tcg_temp_new_i64();
7786 
7787         read_vec_element(s, tcg_op1, rn, 0, MO_64);
7788         read_vec_element(s, tcg_op2, rn, 1, MO_64);
7789 
7790         switch (opcode) {
7791         case 0x3b: /* ADDP */
7792             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
7793             break;
7794         case 0xc: /* FMAXNMP */
7795             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7796             break;
7797         case 0xd: /* FADDP */
7798             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
7799             break;
7800         case 0xf: /* FMAXP */
7801             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
7802             break;
7803         case 0x2c: /* FMINNMP */
7804             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7805             break;
7806         case 0x2f: /* FMINP */
7807             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
7808             break;
7809         default:
7810             g_assert_not_reached();
7811         }
7812 
7813         write_fp_dreg(s, rd, tcg_res);
7814     } else {
7815         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7816         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7817         TCGv_i32 tcg_res = tcg_temp_new_i32();
7818 
7819         read_vec_element_i32(s, tcg_op1, rn, 0, size);
7820         read_vec_element_i32(s, tcg_op2, rn, 1, size);
7821 
7822         if (size == MO_16) {
7823             switch (opcode) {
7824             case 0xc: /* FMAXNMP */
7825                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
7826                 break;
7827             case 0xd: /* FADDP */
7828                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
7829                 break;
7830             case 0xf: /* FMAXP */
7831                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
7832                 break;
7833             case 0x2c: /* FMINNMP */
7834                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
7835                 break;
7836             case 0x2f: /* FMINP */
7837                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
7838                 break;
7839             default:
7840                 g_assert_not_reached();
7841             }
7842         } else {
7843             switch (opcode) {
7844             case 0xc: /* FMAXNMP */
7845                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
7846                 break;
7847             case 0xd: /* FADDP */
7848                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
7849                 break;
7850             case 0xf: /* FMAXP */
7851                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
7852                 break;
7853             case 0x2c: /* FMINNMP */
7854                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
7855                 break;
7856             case 0x2f: /* FMINP */
7857                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
7858                 break;
7859             default:
7860                 g_assert_not_reached();
7861             }
7862         }
7863 
7864         write_fp_sreg(s, rd, tcg_res);
7865     }
7866 }
7867 
7868 /*
7869  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
7870  *
7871  * This code is handles the common shifting code and is used by both
7872  * the vector and scalar code.
7873  */
7874 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
7875                                     TCGv_i64 tcg_rnd, bool accumulate,
7876                                     bool is_u, int size, int shift)
7877 {
7878     bool extended_result = false;
7879     bool round = tcg_rnd != NULL;
7880     int ext_lshift = 0;
7881     TCGv_i64 tcg_src_hi;
7882 
7883     if (round && size == 3) {
7884         extended_result = true;
7885         ext_lshift = 64 - shift;
7886         tcg_src_hi = tcg_temp_new_i64();
7887     } else if (shift == 64) {
7888         if (!accumulate && is_u) {
7889             /* result is zero */
7890             tcg_gen_movi_i64(tcg_res, 0);
7891             return;
7892         }
7893     }
7894 
7895     /* Deal with the rounding step */
7896     if (round) {
7897         if (extended_result) {
7898             TCGv_i64 tcg_zero = tcg_constant_i64(0);
7899             if (!is_u) {
7900                 /* take care of sign extending tcg_res */
7901                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
7902                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
7903                                  tcg_src, tcg_src_hi,
7904                                  tcg_rnd, tcg_zero);
7905             } else {
7906                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
7907                                  tcg_src, tcg_zero,
7908                                  tcg_rnd, tcg_zero);
7909             }
7910         } else {
7911             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
7912         }
7913     }
7914 
7915     /* Now do the shift right */
7916     if (round && extended_result) {
7917         /* extended case, >64 bit precision required */
7918         if (ext_lshift == 0) {
7919             /* special case, only high bits matter */
7920             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
7921         } else {
7922             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
7923             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
7924             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
7925         }
7926     } else {
7927         if (is_u) {
7928             if (shift == 64) {
7929                 /* essentially shifting in 64 zeros */
7930                 tcg_gen_movi_i64(tcg_src, 0);
7931             } else {
7932                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
7933             }
7934         } else {
7935             if (shift == 64) {
7936                 /* effectively extending the sign-bit */
7937                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
7938             } else {
7939                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
7940             }
7941         }
7942     }
7943 
7944     if (accumulate) {
7945         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
7946     } else {
7947         tcg_gen_mov_i64(tcg_res, tcg_src);
7948     }
7949 }
7950 
7951 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
7952 static void handle_scalar_simd_shri(DisasContext *s,
7953                                     bool is_u, int immh, int immb,
7954                                     int opcode, int rn, int rd)
7955 {
7956     const int size = 3;
7957     int immhb = immh << 3 | immb;
7958     int shift = 2 * (8 << size) - immhb;
7959     bool accumulate = false;
7960     bool round = false;
7961     bool insert = false;
7962     TCGv_i64 tcg_rn;
7963     TCGv_i64 tcg_rd;
7964     TCGv_i64 tcg_round;
7965 
7966     if (!extract32(immh, 3, 1)) {
7967         unallocated_encoding(s);
7968         return;
7969     }
7970 
7971     if (!fp_access_check(s)) {
7972         return;
7973     }
7974 
7975     switch (opcode) {
7976     case 0x02: /* SSRA / USRA (accumulate) */
7977         accumulate = true;
7978         break;
7979     case 0x04: /* SRSHR / URSHR (rounding) */
7980         round = true;
7981         break;
7982     case 0x06: /* SRSRA / URSRA (accum + rounding) */
7983         accumulate = round = true;
7984         break;
7985     case 0x08: /* SRI */
7986         insert = true;
7987         break;
7988     }
7989 
7990     if (round) {
7991         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
7992     } else {
7993         tcg_round = NULL;
7994     }
7995 
7996     tcg_rn = read_fp_dreg(s, rn);
7997     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
7998 
7999     if (insert) {
8000         /* shift count same as element size is valid but does nothing;
8001          * special case to avoid potential shift by 64.
8002          */
8003         int esize = 8 << size;
8004         if (shift != esize) {
8005             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8006             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8007         }
8008     } else {
8009         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8010                                 accumulate, is_u, size, shift);
8011     }
8012 
8013     write_fp_dreg(s, rd, tcg_rd);
8014 }
8015 
8016 /* SHL/SLI - Scalar shift left */
8017 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8018                                     int immh, int immb, int opcode,
8019                                     int rn, int rd)
8020 {
8021     int size = 32 - clz32(immh) - 1;
8022     int immhb = immh << 3 | immb;
8023     int shift = immhb - (8 << size);
8024     TCGv_i64 tcg_rn;
8025     TCGv_i64 tcg_rd;
8026 
8027     if (!extract32(immh, 3, 1)) {
8028         unallocated_encoding(s);
8029         return;
8030     }
8031 
8032     if (!fp_access_check(s)) {
8033         return;
8034     }
8035 
8036     tcg_rn = read_fp_dreg(s, rn);
8037     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8038 
8039     if (insert) {
8040         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8041     } else {
8042         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8043     }
8044 
8045     write_fp_dreg(s, rd, tcg_rd);
8046 }
8047 
8048 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8049  * (signed/unsigned) narrowing */
8050 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8051                                    bool is_u_shift, bool is_u_narrow,
8052                                    int immh, int immb, int opcode,
8053                                    int rn, int rd)
8054 {
8055     int immhb = immh << 3 | immb;
8056     int size = 32 - clz32(immh) - 1;
8057     int esize = 8 << size;
8058     int shift = (2 * esize) - immhb;
8059     int elements = is_scalar ? 1 : (64 / esize);
8060     bool round = extract32(opcode, 0, 1);
8061     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8062     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8063     TCGv_i32 tcg_rd_narrowed;
8064     TCGv_i64 tcg_final;
8065 
8066     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8067         { gen_helper_neon_narrow_sat_s8,
8068           gen_helper_neon_unarrow_sat8 },
8069         { gen_helper_neon_narrow_sat_s16,
8070           gen_helper_neon_unarrow_sat16 },
8071         { gen_helper_neon_narrow_sat_s32,
8072           gen_helper_neon_unarrow_sat32 },
8073         { NULL, NULL },
8074     };
8075     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8076         gen_helper_neon_narrow_sat_u8,
8077         gen_helper_neon_narrow_sat_u16,
8078         gen_helper_neon_narrow_sat_u32,
8079         NULL
8080     };
8081     NeonGenNarrowEnvFn *narrowfn;
8082 
8083     int i;
8084 
8085     assert(size < 4);
8086 
8087     if (extract32(immh, 3, 1)) {
8088         unallocated_encoding(s);
8089         return;
8090     }
8091 
8092     if (!fp_access_check(s)) {
8093         return;
8094     }
8095 
8096     if (is_u_shift) {
8097         narrowfn = unsigned_narrow_fns[size];
8098     } else {
8099         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8100     }
8101 
8102     tcg_rn = tcg_temp_new_i64();
8103     tcg_rd = tcg_temp_new_i64();
8104     tcg_rd_narrowed = tcg_temp_new_i32();
8105     tcg_final = tcg_temp_new_i64();
8106 
8107     if (round) {
8108         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8109     } else {
8110         tcg_round = NULL;
8111     }
8112 
8113     for (i = 0; i < elements; i++) {
8114         read_vec_element(s, tcg_rn, rn, i, ldop);
8115         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8116                                 false, is_u_shift, size+1, shift);
8117         narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8118         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8119         if (i == 0) {
8120             tcg_gen_mov_i64(tcg_final, tcg_rd);
8121         } else {
8122             tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8123         }
8124     }
8125 
8126     if (!is_q) {
8127         write_vec_element(s, tcg_final, rd, 0, MO_64);
8128     } else {
8129         write_vec_element(s, tcg_final, rd, 1, MO_64);
8130     }
8131     clear_vec_high(s, is_q, rd);
8132 }
8133 
8134 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8135 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8136                              bool src_unsigned, bool dst_unsigned,
8137                              int immh, int immb, int rn, int rd)
8138 {
8139     int immhb = immh << 3 | immb;
8140     int size = 32 - clz32(immh) - 1;
8141     int shift = immhb - (8 << size);
8142     int pass;
8143 
8144     assert(immh != 0);
8145     assert(!(scalar && is_q));
8146 
8147     if (!scalar) {
8148         if (!is_q && extract32(immh, 3, 1)) {
8149             unallocated_encoding(s);
8150             return;
8151         }
8152 
8153         /* Since we use the variable-shift helpers we must
8154          * replicate the shift count into each element of
8155          * the tcg_shift value.
8156          */
8157         switch (size) {
8158         case 0:
8159             shift |= shift << 8;
8160             /* fall through */
8161         case 1:
8162             shift |= shift << 16;
8163             break;
8164         case 2:
8165         case 3:
8166             break;
8167         default:
8168             g_assert_not_reached();
8169         }
8170     }
8171 
8172     if (!fp_access_check(s)) {
8173         return;
8174     }
8175 
8176     if (size == 3) {
8177         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8178         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8179             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8180             { NULL, gen_helper_neon_qshl_u64 },
8181         };
8182         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8183         int maxpass = is_q ? 2 : 1;
8184 
8185         for (pass = 0; pass < maxpass; pass++) {
8186             TCGv_i64 tcg_op = tcg_temp_new_i64();
8187 
8188             read_vec_element(s, tcg_op, rn, pass, MO_64);
8189             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8190             write_vec_element(s, tcg_op, rd, pass, MO_64);
8191         }
8192         clear_vec_high(s, is_q, rd);
8193     } else {
8194         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8195         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8196             {
8197                 { gen_helper_neon_qshl_s8,
8198                   gen_helper_neon_qshl_s16,
8199                   gen_helper_neon_qshl_s32 },
8200                 { gen_helper_neon_qshlu_s8,
8201                   gen_helper_neon_qshlu_s16,
8202                   gen_helper_neon_qshlu_s32 }
8203             }, {
8204                 { NULL, NULL, NULL },
8205                 { gen_helper_neon_qshl_u8,
8206                   gen_helper_neon_qshl_u16,
8207                   gen_helper_neon_qshl_u32 }
8208             }
8209         };
8210         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8211         MemOp memop = scalar ? size : MO_32;
8212         int maxpass = scalar ? 1 : is_q ? 4 : 2;
8213 
8214         for (pass = 0; pass < maxpass; pass++) {
8215             TCGv_i32 tcg_op = tcg_temp_new_i32();
8216 
8217             read_vec_element_i32(s, tcg_op, rn, pass, memop);
8218             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8219             if (scalar) {
8220                 switch (size) {
8221                 case 0:
8222                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
8223                     break;
8224                 case 1:
8225                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8226                     break;
8227                 case 2:
8228                     break;
8229                 default:
8230                     g_assert_not_reached();
8231                 }
8232                 write_fp_sreg(s, rd, tcg_op);
8233             } else {
8234                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8235             }
8236         }
8237 
8238         if (!scalar) {
8239             clear_vec_high(s, is_q, rd);
8240         }
8241     }
8242 }
8243 
8244 /* Common vector code for handling integer to FP conversion */
8245 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8246                                    int elements, int is_signed,
8247                                    int fracbits, int size)
8248 {
8249     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8250     TCGv_i32 tcg_shift = NULL;
8251 
8252     MemOp mop = size | (is_signed ? MO_SIGN : 0);
8253     int pass;
8254 
8255     if (fracbits || size == MO_64) {
8256         tcg_shift = tcg_constant_i32(fracbits);
8257     }
8258 
8259     if (size == MO_64) {
8260         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8261         TCGv_i64 tcg_double = tcg_temp_new_i64();
8262 
8263         for (pass = 0; pass < elements; pass++) {
8264             read_vec_element(s, tcg_int64, rn, pass, mop);
8265 
8266             if (is_signed) {
8267                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8268                                      tcg_shift, tcg_fpst);
8269             } else {
8270                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8271                                      tcg_shift, tcg_fpst);
8272             }
8273             if (elements == 1) {
8274                 write_fp_dreg(s, rd, tcg_double);
8275             } else {
8276                 write_vec_element(s, tcg_double, rd, pass, MO_64);
8277             }
8278         }
8279     } else {
8280         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8281         TCGv_i32 tcg_float = tcg_temp_new_i32();
8282 
8283         for (pass = 0; pass < elements; pass++) {
8284             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8285 
8286             switch (size) {
8287             case MO_32:
8288                 if (fracbits) {
8289                     if (is_signed) {
8290                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
8291                                              tcg_shift, tcg_fpst);
8292                     } else {
8293                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
8294                                              tcg_shift, tcg_fpst);
8295                     }
8296                 } else {
8297                     if (is_signed) {
8298                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8299                     } else {
8300                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8301                     }
8302                 }
8303                 break;
8304             case MO_16:
8305                 if (fracbits) {
8306                     if (is_signed) {
8307                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8308                                              tcg_shift, tcg_fpst);
8309                     } else {
8310                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8311                                              tcg_shift, tcg_fpst);
8312                     }
8313                 } else {
8314                     if (is_signed) {
8315                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8316                     } else {
8317                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8318                     }
8319                 }
8320                 break;
8321             default:
8322                 g_assert_not_reached();
8323             }
8324 
8325             if (elements == 1) {
8326                 write_fp_sreg(s, rd, tcg_float);
8327             } else {
8328                 write_vec_element_i32(s, tcg_float, rd, pass, size);
8329             }
8330         }
8331     }
8332 
8333     clear_vec_high(s, elements << size == 16, rd);
8334 }
8335 
8336 /* UCVTF/SCVTF - Integer to FP conversion */
8337 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8338                                          bool is_q, bool is_u,
8339                                          int immh, int immb, int opcode,
8340                                          int rn, int rd)
8341 {
8342     int size, elements, fracbits;
8343     int immhb = immh << 3 | immb;
8344 
8345     if (immh & 8) {
8346         size = MO_64;
8347         if (!is_scalar && !is_q) {
8348             unallocated_encoding(s);
8349             return;
8350         }
8351     } else if (immh & 4) {
8352         size = MO_32;
8353     } else if (immh & 2) {
8354         size = MO_16;
8355         if (!dc_isar_feature(aa64_fp16, s)) {
8356             unallocated_encoding(s);
8357             return;
8358         }
8359     } else {
8360         /* immh == 0 would be a failure of the decode logic */
8361         g_assert(immh == 1);
8362         unallocated_encoding(s);
8363         return;
8364     }
8365 
8366     if (is_scalar) {
8367         elements = 1;
8368     } else {
8369         elements = (8 << is_q) >> size;
8370     }
8371     fracbits = (16 << size) - immhb;
8372 
8373     if (!fp_access_check(s)) {
8374         return;
8375     }
8376 
8377     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8378 }
8379 
8380 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8381 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8382                                          bool is_q, bool is_u,
8383                                          int immh, int immb, int rn, int rd)
8384 {
8385     int immhb = immh << 3 | immb;
8386     int pass, size, fracbits;
8387     TCGv_ptr tcg_fpstatus;
8388     TCGv_i32 tcg_rmode, tcg_shift;
8389 
8390     if (immh & 0x8) {
8391         size = MO_64;
8392         if (!is_scalar && !is_q) {
8393             unallocated_encoding(s);
8394             return;
8395         }
8396     } else if (immh & 0x4) {
8397         size = MO_32;
8398     } else if (immh & 0x2) {
8399         size = MO_16;
8400         if (!dc_isar_feature(aa64_fp16, s)) {
8401             unallocated_encoding(s);
8402             return;
8403         }
8404     } else {
8405         /* Should have split out AdvSIMD modified immediate earlier.  */
8406         assert(immh == 1);
8407         unallocated_encoding(s);
8408         return;
8409     }
8410 
8411     if (!fp_access_check(s)) {
8412         return;
8413     }
8414 
8415     assert(!(is_scalar && is_q));
8416 
8417     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8418     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
8419     fracbits = (16 << size) - immhb;
8420     tcg_shift = tcg_constant_i32(fracbits);
8421 
8422     if (size == MO_64) {
8423         int maxpass = is_scalar ? 1 : 2;
8424 
8425         for (pass = 0; pass < maxpass; pass++) {
8426             TCGv_i64 tcg_op = tcg_temp_new_i64();
8427 
8428             read_vec_element(s, tcg_op, rn, pass, MO_64);
8429             if (is_u) {
8430                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8431             } else {
8432                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8433             }
8434             write_vec_element(s, tcg_op, rd, pass, MO_64);
8435         }
8436         clear_vec_high(s, is_q, rd);
8437     } else {
8438         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8439         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8440 
8441         switch (size) {
8442         case MO_16:
8443             if (is_u) {
8444                 fn = gen_helper_vfp_touhh;
8445             } else {
8446                 fn = gen_helper_vfp_toshh;
8447             }
8448             break;
8449         case MO_32:
8450             if (is_u) {
8451                 fn = gen_helper_vfp_touls;
8452             } else {
8453                 fn = gen_helper_vfp_tosls;
8454             }
8455             break;
8456         default:
8457             g_assert_not_reached();
8458         }
8459 
8460         for (pass = 0; pass < maxpass; pass++) {
8461             TCGv_i32 tcg_op = tcg_temp_new_i32();
8462 
8463             read_vec_element_i32(s, tcg_op, rn, pass, size);
8464             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8465             if (is_scalar) {
8466                 write_fp_sreg(s, rd, tcg_op);
8467             } else {
8468                 write_vec_element_i32(s, tcg_op, rd, pass, size);
8469             }
8470         }
8471         if (!is_scalar) {
8472             clear_vec_high(s, is_q, rd);
8473         }
8474     }
8475 
8476     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
8477 }
8478 
8479 /* AdvSIMD scalar shift by immediate
8480  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
8481  * +-----+---+-------------+------+------+--------+---+------+------+
8482  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8483  * +-----+---+-------------+------+------+--------+---+------+------+
8484  *
8485  * This is the scalar version so it works on a fixed sized registers
8486  */
8487 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8488 {
8489     int rd = extract32(insn, 0, 5);
8490     int rn = extract32(insn, 5, 5);
8491     int opcode = extract32(insn, 11, 5);
8492     int immb = extract32(insn, 16, 3);
8493     int immh = extract32(insn, 19, 4);
8494     bool is_u = extract32(insn, 29, 1);
8495 
8496     if (immh == 0) {
8497         unallocated_encoding(s);
8498         return;
8499     }
8500 
8501     switch (opcode) {
8502     case 0x08: /* SRI */
8503         if (!is_u) {
8504             unallocated_encoding(s);
8505             return;
8506         }
8507         /* fall through */
8508     case 0x00: /* SSHR / USHR */
8509     case 0x02: /* SSRA / USRA */
8510     case 0x04: /* SRSHR / URSHR */
8511     case 0x06: /* SRSRA / URSRA */
8512         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8513         break;
8514     case 0x0a: /* SHL / SLI */
8515         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8516         break;
8517     case 0x1c: /* SCVTF, UCVTF */
8518         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8519                                      opcode, rn, rd);
8520         break;
8521     case 0x10: /* SQSHRUN, SQSHRUN2 */
8522     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8523         if (!is_u) {
8524             unallocated_encoding(s);
8525             return;
8526         }
8527         handle_vec_simd_sqshrn(s, true, false, false, true,
8528                                immh, immb, opcode, rn, rd);
8529         break;
8530     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8531     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8532         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8533                                immh, immb, opcode, rn, rd);
8534         break;
8535     case 0xc: /* SQSHLU */
8536         if (!is_u) {
8537             unallocated_encoding(s);
8538             return;
8539         }
8540         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8541         break;
8542     case 0xe: /* SQSHL, UQSHL */
8543         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8544         break;
8545     case 0x1f: /* FCVTZS, FCVTZU */
8546         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8547         break;
8548     default:
8549         unallocated_encoding(s);
8550         break;
8551     }
8552 }
8553 
8554 /* AdvSIMD scalar three different
8555  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
8556  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8557  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
8558  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8559  */
8560 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8561 {
8562     bool is_u = extract32(insn, 29, 1);
8563     int size = extract32(insn, 22, 2);
8564     int opcode = extract32(insn, 12, 4);
8565     int rm = extract32(insn, 16, 5);
8566     int rn = extract32(insn, 5, 5);
8567     int rd = extract32(insn, 0, 5);
8568 
8569     if (is_u) {
8570         unallocated_encoding(s);
8571         return;
8572     }
8573 
8574     switch (opcode) {
8575     case 0x9: /* SQDMLAL, SQDMLAL2 */
8576     case 0xb: /* SQDMLSL, SQDMLSL2 */
8577     case 0xd: /* SQDMULL, SQDMULL2 */
8578         if (size == 0 || size == 3) {
8579             unallocated_encoding(s);
8580             return;
8581         }
8582         break;
8583     default:
8584         unallocated_encoding(s);
8585         return;
8586     }
8587 
8588     if (!fp_access_check(s)) {
8589         return;
8590     }
8591 
8592     if (size == 2) {
8593         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8594         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8595         TCGv_i64 tcg_res = tcg_temp_new_i64();
8596 
8597         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8598         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8599 
8600         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8601         gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
8602 
8603         switch (opcode) {
8604         case 0xd: /* SQDMULL, SQDMULL2 */
8605             break;
8606         case 0xb: /* SQDMLSL, SQDMLSL2 */
8607             tcg_gen_neg_i64(tcg_res, tcg_res);
8608             /* fall through */
8609         case 0x9: /* SQDMLAL, SQDMLAL2 */
8610             read_vec_element(s, tcg_op1, rd, 0, MO_64);
8611             gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
8612                                               tcg_res, tcg_op1);
8613             break;
8614         default:
8615             g_assert_not_reached();
8616         }
8617 
8618         write_fp_dreg(s, rd, tcg_res);
8619     } else {
8620         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8621         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8622         TCGv_i64 tcg_res = tcg_temp_new_i64();
8623 
8624         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8625         gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
8626 
8627         switch (opcode) {
8628         case 0xd: /* SQDMULL, SQDMULL2 */
8629             break;
8630         case 0xb: /* SQDMLSL, SQDMLSL2 */
8631             gen_helper_neon_negl_u32(tcg_res, tcg_res);
8632             /* fall through */
8633         case 0x9: /* SQDMLAL, SQDMLAL2 */
8634         {
8635             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
8636             read_vec_element(s, tcg_op3, rd, 0, MO_32);
8637             gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
8638                                               tcg_res, tcg_op3);
8639             break;
8640         }
8641         default:
8642             g_assert_not_reached();
8643         }
8644 
8645         tcg_gen_ext32u_i64(tcg_res, tcg_res);
8646         write_fp_dreg(s, rd, tcg_res);
8647     }
8648 }
8649 
8650 static void handle_3same_64(DisasContext *s, int opcode, bool u,
8651                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
8652 {
8653     /* Handle 64x64->64 opcodes which are shared between the scalar
8654      * and vector 3-same groups. We cover every opcode where size == 3
8655      * is valid in either the three-reg-same (integer, not pairwise)
8656      * or scalar-three-reg-same groups.
8657      */
8658     TCGCond cond;
8659 
8660     switch (opcode) {
8661     case 0x1: /* SQADD */
8662         if (u) {
8663             gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8664         } else {
8665             gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8666         }
8667         break;
8668     case 0x5: /* SQSUB */
8669         if (u) {
8670             gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8671         } else {
8672             gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8673         }
8674         break;
8675     case 0x6: /* CMGT, CMHI */
8676         cond = u ? TCG_COND_GTU : TCG_COND_GT;
8677     do_cmop:
8678         /* 64 bit integer comparison, result = test ? -1 : 0. */
8679         tcg_gen_negsetcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
8680         break;
8681     case 0x7: /* CMGE, CMHS */
8682         cond = u ? TCG_COND_GEU : TCG_COND_GE;
8683         goto do_cmop;
8684     case 0x11: /* CMTST, CMEQ */
8685         if (u) {
8686             cond = TCG_COND_EQ;
8687             goto do_cmop;
8688         }
8689         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
8690         break;
8691     case 0x8: /* SSHL, USHL */
8692         if (u) {
8693             gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
8694         } else {
8695             gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
8696         }
8697         break;
8698     case 0x9: /* SQSHL, UQSHL */
8699         if (u) {
8700             gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8701         } else {
8702             gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8703         }
8704         break;
8705     case 0xa: /* SRSHL, URSHL */
8706         if (u) {
8707             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
8708         } else {
8709             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
8710         }
8711         break;
8712     case 0xb: /* SQRSHL, UQRSHL */
8713         if (u) {
8714             gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8715         } else {
8716             gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8717         }
8718         break;
8719     case 0x10: /* ADD, SUB */
8720         if (u) {
8721             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
8722         } else {
8723             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
8724         }
8725         break;
8726     default:
8727         g_assert_not_reached();
8728     }
8729 }
8730 
8731 /* Handle the 3-same-operands float operations; shared by the scalar
8732  * and vector encodings. The caller must filter out any encodings
8733  * not allocated for the encoding it is dealing with.
8734  */
8735 static void handle_3same_float(DisasContext *s, int size, int elements,
8736                                int fpopcode, int rd, int rn, int rm)
8737 {
8738     int pass;
8739     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
8740 
8741     for (pass = 0; pass < elements; pass++) {
8742         if (size) {
8743             /* Double */
8744             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8745             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8746             TCGv_i64 tcg_res = tcg_temp_new_i64();
8747 
8748             read_vec_element(s, tcg_op1, rn, pass, MO_64);
8749             read_vec_element(s, tcg_op2, rm, pass, MO_64);
8750 
8751             switch (fpopcode) {
8752             case 0x39: /* FMLS */
8753                 /* As usual for ARM, separate negation for fused multiply-add */
8754                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
8755                 /* fall through */
8756             case 0x19: /* FMLA */
8757                 read_vec_element(s, tcg_res, rd, pass, MO_64);
8758                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
8759                                        tcg_res, fpst);
8760                 break;
8761             case 0x18: /* FMAXNM */
8762                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8763                 break;
8764             case 0x1a: /* FADD */
8765                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8766                 break;
8767             case 0x1b: /* FMULX */
8768                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
8769                 break;
8770             case 0x1c: /* FCMEQ */
8771                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8772                 break;
8773             case 0x1e: /* FMAX */
8774                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8775                 break;
8776             case 0x1f: /* FRECPS */
8777                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8778                 break;
8779             case 0x38: /* FMINNM */
8780                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8781                 break;
8782             case 0x3a: /* FSUB */
8783                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
8784                 break;
8785             case 0x3e: /* FMIN */
8786                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8787                 break;
8788             case 0x3f: /* FRSQRTS */
8789                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8790                 break;
8791             case 0x5b: /* FMUL */
8792                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
8793                 break;
8794             case 0x5c: /* FCMGE */
8795                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8796                 break;
8797             case 0x5d: /* FACGE */
8798                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8799                 break;
8800             case 0x5f: /* FDIV */
8801                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
8802                 break;
8803             case 0x7a: /* FABD */
8804                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
8805                 gen_helper_vfp_absd(tcg_res, tcg_res);
8806                 break;
8807             case 0x7c: /* FCMGT */
8808                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8809                 break;
8810             case 0x7d: /* FACGT */
8811                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8812                 break;
8813             default:
8814                 g_assert_not_reached();
8815             }
8816 
8817             write_vec_element(s, tcg_res, rd, pass, MO_64);
8818         } else {
8819             /* Single */
8820             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8821             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8822             TCGv_i32 tcg_res = tcg_temp_new_i32();
8823 
8824             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
8825             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
8826 
8827             switch (fpopcode) {
8828             case 0x39: /* FMLS */
8829                 /* As usual for ARM, separate negation for fused multiply-add */
8830                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
8831                 /* fall through */
8832             case 0x19: /* FMLA */
8833                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
8834                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
8835                                        tcg_res, fpst);
8836                 break;
8837             case 0x1a: /* FADD */
8838                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8839                 break;
8840             case 0x1b: /* FMULX */
8841                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
8842                 break;
8843             case 0x1c: /* FCMEQ */
8844                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8845                 break;
8846             case 0x1e: /* FMAX */
8847                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8848                 break;
8849             case 0x1f: /* FRECPS */
8850                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8851                 break;
8852             case 0x18: /* FMAXNM */
8853                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8854                 break;
8855             case 0x38: /* FMINNM */
8856                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8857                 break;
8858             case 0x3a: /* FSUB */
8859                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
8860                 break;
8861             case 0x3e: /* FMIN */
8862                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8863                 break;
8864             case 0x3f: /* FRSQRTS */
8865                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8866                 break;
8867             case 0x5b: /* FMUL */
8868                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
8869                 break;
8870             case 0x5c: /* FCMGE */
8871                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8872                 break;
8873             case 0x5d: /* FACGE */
8874                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8875                 break;
8876             case 0x5f: /* FDIV */
8877                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
8878                 break;
8879             case 0x7a: /* FABD */
8880                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
8881                 gen_helper_vfp_abss(tcg_res, tcg_res);
8882                 break;
8883             case 0x7c: /* FCMGT */
8884                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8885                 break;
8886             case 0x7d: /* FACGT */
8887                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8888                 break;
8889             default:
8890                 g_assert_not_reached();
8891             }
8892 
8893             if (elements == 1) {
8894                 /* scalar single so clear high part */
8895                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
8896 
8897                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
8898                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
8899             } else {
8900                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
8901             }
8902         }
8903     }
8904 
8905     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
8906 }
8907 
8908 /* AdvSIMD scalar three same
8909  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
8910  * +-----+---+-----------+------+---+------+--------+---+------+------+
8911  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
8912  * +-----+---+-----------+------+---+------+--------+---+------+------+
8913  */
8914 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
8915 {
8916     int rd = extract32(insn, 0, 5);
8917     int rn = extract32(insn, 5, 5);
8918     int opcode = extract32(insn, 11, 5);
8919     int rm = extract32(insn, 16, 5);
8920     int size = extract32(insn, 22, 2);
8921     bool u = extract32(insn, 29, 1);
8922     TCGv_i64 tcg_rd;
8923 
8924     if (opcode >= 0x18) {
8925         /* Floating point: U, size[1] and opcode indicate operation */
8926         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
8927         switch (fpopcode) {
8928         case 0x1b: /* FMULX */
8929         case 0x1f: /* FRECPS */
8930         case 0x3f: /* FRSQRTS */
8931         case 0x5d: /* FACGE */
8932         case 0x7d: /* FACGT */
8933         case 0x1c: /* FCMEQ */
8934         case 0x5c: /* FCMGE */
8935         case 0x7c: /* FCMGT */
8936         case 0x7a: /* FABD */
8937             break;
8938         default:
8939             unallocated_encoding(s);
8940             return;
8941         }
8942 
8943         if (!fp_access_check(s)) {
8944             return;
8945         }
8946 
8947         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
8948         return;
8949     }
8950 
8951     switch (opcode) {
8952     case 0x1: /* SQADD, UQADD */
8953     case 0x5: /* SQSUB, UQSUB */
8954     case 0x9: /* SQSHL, UQSHL */
8955     case 0xb: /* SQRSHL, UQRSHL */
8956         break;
8957     case 0x8: /* SSHL, USHL */
8958     case 0xa: /* SRSHL, URSHL */
8959     case 0x6: /* CMGT, CMHI */
8960     case 0x7: /* CMGE, CMHS */
8961     case 0x11: /* CMTST, CMEQ */
8962     case 0x10: /* ADD, SUB (vector) */
8963         if (size != 3) {
8964             unallocated_encoding(s);
8965             return;
8966         }
8967         break;
8968     case 0x16: /* SQDMULH, SQRDMULH (vector) */
8969         if (size != 1 && size != 2) {
8970             unallocated_encoding(s);
8971             return;
8972         }
8973         break;
8974     default:
8975         unallocated_encoding(s);
8976         return;
8977     }
8978 
8979     if (!fp_access_check(s)) {
8980         return;
8981     }
8982 
8983     tcg_rd = tcg_temp_new_i64();
8984 
8985     if (size == 3) {
8986         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
8987         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
8988 
8989         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
8990     } else {
8991         /* Do a single operation on the lowest element in the vector.
8992          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
8993          * no side effects for all these operations.
8994          * OPTME: special-purpose helpers would avoid doing some
8995          * unnecessary work in the helper for the 8 and 16 bit cases.
8996          */
8997         NeonGenTwoOpEnvFn *genenvfn;
8998         TCGv_i32 tcg_rn = tcg_temp_new_i32();
8999         TCGv_i32 tcg_rm = tcg_temp_new_i32();
9000         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9001 
9002         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9003         read_vec_element_i32(s, tcg_rm, rm, 0, size);
9004 
9005         switch (opcode) {
9006         case 0x1: /* SQADD, UQADD */
9007         {
9008             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9009                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9010                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9011                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9012             };
9013             genenvfn = fns[size][u];
9014             break;
9015         }
9016         case 0x5: /* SQSUB, UQSUB */
9017         {
9018             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9019                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9020                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9021                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9022             };
9023             genenvfn = fns[size][u];
9024             break;
9025         }
9026         case 0x9: /* SQSHL, UQSHL */
9027         {
9028             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9029                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9030                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9031                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9032             };
9033             genenvfn = fns[size][u];
9034             break;
9035         }
9036         case 0xb: /* SQRSHL, UQRSHL */
9037         {
9038             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9039                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9040                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9041                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9042             };
9043             genenvfn = fns[size][u];
9044             break;
9045         }
9046         case 0x16: /* SQDMULH, SQRDMULH */
9047         {
9048             static NeonGenTwoOpEnvFn * const fns[2][2] = {
9049                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9050                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9051             };
9052             assert(size == 1 || size == 2);
9053             genenvfn = fns[size - 1][u];
9054             break;
9055         }
9056         default:
9057             g_assert_not_reached();
9058         }
9059 
9060         genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9061         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9062     }
9063 
9064     write_fp_dreg(s, rd, tcg_rd);
9065 }
9066 
9067 /* AdvSIMD scalar three same FP16
9068  *  31 30  29 28       24 23  22 21 20  16 15 14 13    11 10  9  5 4  0
9069  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9070  * | 0 1 | U | 1 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 | Rn | Rd |
9071  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9072  * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9073  * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9074  */
9075 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9076                                                   uint32_t insn)
9077 {
9078     int rd = extract32(insn, 0, 5);
9079     int rn = extract32(insn, 5, 5);
9080     int opcode = extract32(insn, 11, 3);
9081     int rm = extract32(insn, 16, 5);
9082     bool u = extract32(insn, 29, 1);
9083     bool a = extract32(insn, 23, 1);
9084     int fpopcode = opcode | (a << 3) |  (u << 4);
9085     TCGv_ptr fpst;
9086     TCGv_i32 tcg_op1;
9087     TCGv_i32 tcg_op2;
9088     TCGv_i32 tcg_res;
9089 
9090     switch (fpopcode) {
9091     case 0x03: /* FMULX */
9092     case 0x04: /* FCMEQ (reg) */
9093     case 0x07: /* FRECPS */
9094     case 0x0f: /* FRSQRTS */
9095     case 0x14: /* FCMGE (reg) */
9096     case 0x15: /* FACGE */
9097     case 0x1a: /* FABD */
9098     case 0x1c: /* FCMGT (reg) */
9099     case 0x1d: /* FACGT */
9100         break;
9101     default:
9102         unallocated_encoding(s);
9103         return;
9104     }
9105 
9106     if (!dc_isar_feature(aa64_fp16, s)) {
9107         unallocated_encoding(s);
9108     }
9109 
9110     if (!fp_access_check(s)) {
9111         return;
9112     }
9113 
9114     fpst = fpstatus_ptr(FPST_FPCR_F16);
9115 
9116     tcg_op1 = read_fp_hreg(s, rn);
9117     tcg_op2 = read_fp_hreg(s, rm);
9118     tcg_res = tcg_temp_new_i32();
9119 
9120     switch (fpopcode) {
9121     case 0x03: /* FMULX */
9122         gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9123         break;
9124     case 0x04: /* FCMEQ (reg) */
9125         gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9126         break;
9127     case 0x07: /* FRECPS */
9128         gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9129         break;
9130     case 0x0f: /* FRSQRTS */
9131         gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9132         break;
9133     case 0x14: /* FCMGE (reg) */
9134         gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9135         break;
9136     case 0x15: /* FACGE */
9137         gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9138         break;
9139     case 0x1a: /* FABD */
9140         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9141         tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9142         break;
9143     case 0x1c: /* FCMGT (reg) */
9144         gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9145         break;
9146     case 0x1d: /* FACGT */
9147         gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9148         break;
9149     default:
9150         g_assert_not_reached();
9151     }
9152 
9153     write_fp_sreg(s, rd, tcg_res);
9154 }
9155 
9156 /* AdvSIMD scalar three same extra
9157  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9158  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9159  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9160  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9161  */
9162 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9163                                                    uint32_t insn)
9164 {
9165     int rd = extract32(insn, 0, 5);
9166     int rn = extract32(insn, 5, 5);
9167     int opcode = extract32(insn, 11, 4);
9168     int rm = extract32(insn, 16, 5);
9169     int size = extract32(insn, 22, 2);
9170     bool u = extract32(insn, 29, 1);
9171     TCGv_i32 ele1, ele2, ele3;
9172     TCGv_i64 res;
9173     bool feature;
9174 
9175     switch (u * 16 + opcode) {
9176     case 0x10: /* SQRDMLAH (vector) */
9177     case 0x11: /* SQRDMLSH (vector) */
9178         if (size != 1 && size != 2) {
9179             unallocated_encoding(s);
9180             return;
9181         }
9182         feature = dc_isar_feature(aa64_rdm, s);
9183         break;
9184     default:
9185         unallocated_encoding(s);
9186         return;
9187     }
9188     if (!feature) {
9189         unallocated_encoding(s);
9190         return;
9191     }
9192     if (!fp_access_check(s)) {
9193         return;
9194     }
9195 
9196     /* Do a single operation on the lowest element in the vector.
9197      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9198      * with no side effects for all these operations.
9199      * OPTME: special-purpose helpers would avoid doing some
9200      * unnecessary work in the helper for the 16 bit cases.
9201      */
9202     ele1 = tcg_temp_new_i32();
9203     ele2 = tcg_temp_new_i32();
9204     ele3 = tcg_temp_new_i32();
9205 
9206     read_vec_element_i32(s, ele1, rn, 0, size);
9207     read_vec_element_i32(s, ele2, rm, 0, size);
9208     read_vec_element_i32(s, ele3, rd, 0, size);
9209 
9210     switch (opcode) {
9211     case 0x0: /* SQRDMLAH */
9212         if (size == 1) {
9213             gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9214         } else {
9215             gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9216         }
9217         break;
9218     case 0x1: /* SQRDMLSH */
9219         if (size == 1) {
9220             gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9221         } else {
9222             gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9223         }
9224         break;
9225     default:
9226         g_assert_not_reached();
9227     }
9228 
9229     res = tcg_temp_new_i64();
9230     tcg_gen_extu_i32_i64(res, ele3);
9231     write_fp_dreg(s, rd, res);
9232 }
9233 
9234 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9235                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9236                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9237 {
9238     /* Handle 64->64 opcodes which are shared between the scalar and
9239      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9240      * is valid in either group and also the double-precision fp ops.
9241      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9242      * requires them.
9243      */
9244     TCGCond cond;
9245 
9246     switch (opcode) {
9247     case 0x4: /* CLS, CLZ */
9248         if (u) {
9249             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9250         } else {
9251             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9252         }
9253         break;
9254     case 0x5: /* NOT */
9255         /* This opcode is shared with CNT and RBIT but we have earlier
9256          * enforced that size == 3 if and only if this is the NOT insn.
9257          */
9258         tcg_gen_not_i64(tcg_rd, tcg_rn);
9259         break;
9260     case 0x7: /* SQABS, SQNEG */
9261         if (u) {
9262             gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9263         } else {
9264             gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9265         }
9266         break;
9267     case 0xa: /* CMLT */
9268         cond = TCG_COND_LT;
9269     do_cmop:
9270         /* 64 bit integer comparison against zero, result is test ? -1 : 0. */
9271         tcg_gen_negsetcond_i64(cond, tcg_rd, tcg_rn, tcg_constant_i64(0));
9272         break;
9273     case 0x8: /* CMGT, CMGE */
9274         cond = u ? TCG_COND_GE : TCG_COND_GT;
9275         goto do_cmop;
9276     case 0x9: /* CMEQ, CMLE */
9277         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9278         goto do_cmop;
9279     case 0xb: /* ABS, NEG */
9280         if (u) {
9281             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9282         } else {
9283             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9284         }
9285         break;
9286     case 0x2f: /* FABS */
9287         gen_helper_vfp_absd(tcg_rd, tcg_rn);
9288         break;
9289     case 0x6f: /* FNEG */
9290         gen_helper_vfp_negd(tcg_rd, tcg_rn);
9291         break;
9292     case 0x7f: /* FSQRT */
9293         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9294         break;
9295     case 0x1a: /* FCVTNS */
9296     case 0x1b: /* FCVTMS */
9297     case 0x1c: /* FCVTAS */
9298     case 0x3a: /* FCVTPS */
9299     case 0x3b: /* FCVTZS */
9300         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9301         break;
9302     case 0x5a: /* FCVTNU */
9303     case 0x5b: /* FCVTMU */
9304     case 0x5c: /* FCVTAU */
9305     case 0x7a: /* FCVTPU */
9306     case 0x7b: /* FCVTZU */
9307         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9308         break;
9309     case 0x18: /* FRINTN */
9310     case 0x19: /* FRINTM */
9311     case 0x38: /* FRINTP */
9312     case 0x39: /* FRINTZ */
9313     case 0x58: /* FRINTA */
9314     case 0x79: /* FRINTI */
9315         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9316         break;
9317     case 0x59: /* FRINTX */
9318         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9319         break;
9320     case 0x1e: /* FRINT32Z */
9321     case 0x5e: /* FRINT32X */
9322         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9323         break;
9324     case 0x1f: /* FRINT64Z */
9325     case 0x5f: /* FRINT64X */
9326         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9327         break;
9328     default:
9329         g_assert_not_reached();
9330     }
9331 }
9332 
9333 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9334                                    bool is_scalar, bool is_u, bool is_q,
9335                                    int size, int rn, int rd)
9336 {
9337     bool is_double = (size == MO_64);
9338     TCGv_ptr fpst;
9339 
9340     if (!fp_access_check(s)) {
9341         return;
9342     }
9343 
9344     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9345 
9346     if (is_double) {
9347         TCGv_i64 tcg_op = tcg_temp_new_i64();
9348         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9349         TCGv_i64 tcg_res = tcg_temp_new_i64();
9350         NeonGenTwoDoubleOpFn *genfn;
9351         bool swap = false;
9352         int pass;
9353 
9354         switch (opcode) {
9355         case 0x2e: /* FCMLT (zero) */
9356             swap = true;
9357             /* fallthrough */
9358         case 0x2c: /* FCMGT (zero) */
9359             genfn = gen_helper_neon_cgt_f64;
9360             break;
9361         case 0x2d: /* FCMEQ (zero) */
9362             genfn = gen_helper_neon_ceq_f64;
9363             break;
9364         case 0x6d: /* FCMLE (zero) */
9365             swap = true;
9366             /* fall through */
9367         case 0x6c: /* FCMGE (zero) */
9368             genfn = gen_helper_neon_cge_f64;
9369             break;
9370         default:
9371             g_assert_not_reached();
9372         }
9373 
9374         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9375             read_vec_element(s, tcg_op, rn, pass, MO_64);
9376             if (swap) {
9377                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9378             } else {
9379                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9380             }
9381             write_vec_element(s, tcg_res, rd, pass, MO_64);
9382         }
9383 
9384         clear_vec_high(s, !is_scalar, rd);
9385     } else {
9386         TCGv_i32 tcg_op = tcg_temp_new_i32();
9387         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9388         TCGv_i32 tcg_res = tcg_temp_new_i32();
9389         NeonGenTwoSingleOpFn *genfn;
9390         bool swap = false;
9391         int pass, maxpasses;
9392 
9393         if (size == MO_16) {
9394             switch (opcode) {
9395             case 0x2e: /* FCMLT (zero) */
9396                 swap = true;
9397                 /* fall through */
9398             case 0x2c: /* FCMGT (zero) */
9399                 genfn = gen_helper_advsimd_cgt_f16;
9400                 break;
9401             case 0x2d: /* FCMEQ (zero) */
9402                 genfn = gen_helper_advsimd_ceq_f16;
9403                 break;
9404             case 0x6d: /* FCMLE (zero) */
9405                 swap = true;
9406                 /* fall through */
9407             case 0x6c: /* FCMGE (zero) */
9408                 genfn = gen_helper_advsimd_cge_f16;
9409                 break;
9410             default:
9411                 g_assert_not_reached();
9412             }
9413         } else {
9414             switch (opcode) {
9415             case 0x2e: /* FCMLT (zero) */
9416                 swap = true;
9417                 /* fall through */
9418             case 0x2c: /* FCMGT (zero) */
9419                 genfn = gen_helper_neon_cgt_f32;
9420                 break;
9421             case 0x2d: /* FCMEQ (zero) */
9422                 genfn = gen_helper_neon_ceq_f32;
9423                 break;
9424             case 0x6d: /* FCMLE (zero) */
9425                 swap = true;
9426                 /* fall through */
9427             case 0x6c: /* FCMGE (zero) */
9428                 genfn = gen_helper_neon_cge_f32;
9429                 break;
9430             default:
9431                 g_assert_not_reached();
9432             }
9433         }
9434 
9435         if (is_scalar) {
9436             maxpasses = 1;
9437         } else {
9438             int vector_size = 8 << is_q;
9439             maxpasses = vector_size >> size;
9440         }
9441 
9442         for (pass = 0; pass < maxpasses; pass++) {
9443             read_vec_element_i32(s, tcg_op, rn, pass, size);
9444             if (swap) {
9445                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9446             } else {
9447                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9448             }
9449             if (is_scalar) {
9450                 write_fp_sreg(s, rd, tcg_res);
9451             } else {
9452                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9453             }
9454         }
9455 
9456         if (!is_scalar) {
9457             clear_vec_high(s, is_q, rd);
9458         }
9459     }
9460 }
9461 
9462 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9463                                     bool is_scalar, bool is_u, bool is_q,
9464                                     int size, int rn, int rd)
9465 {
9466     bool is_double = (size == 3);
9467     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9468 
9469     if (is_double) {
9470         TCGv_i64 tcg_op = tcg_temp_new_i64();
9471         TCGv_i64 tcg_res = tcg_temp_new_i64();
9472         int pass;
9473 
9474         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9475             read_vec_element(s, tcg_op, rn, pass, MO_64);
9476             switch (opcode) {
9477             case 0x3d: /* FRECPE */
9478                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9479                 break;
9480             case 0x3f: /* FRECPX */
9481                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9482                 break;
9483             case 0x7d: /* FRSQRTE */
9484                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9485                 break;
9486             default:
9487                 g_assert_not_reached();
9488             }
9489             write_vec_element(s, tcg_res, rd, pass, MO_64);
9490         }
9491         clear_vec_high(s, !is_scalar, rd);
9492     } else {
9493         TCGv_i32 tcg_op = tcg_temp_new_i32();
9494         TCGv_i32 tcg_res = tcg_temp_new_i32();
9495         int pass, maxpasses;
9496 
9497         if (is_scalar) {
9498             maxpasses = 1;
9499         } else {
9500             maxpasses = is_q ? 4 : 2;
9501         }
9502 
9503         for (pass = 0; pass < maxpasses; pass++) {
9504             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9505 
9506             switch (opcode) {
9507             case 0x3c: /* URECPE */
9508                 gen_helper_recpe_u32(tcg_res, tcg_op);
9509                 break;
9510             case 0x3d: /* FRECPE */
9511                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9512                 break;
9513             case 0x3f: /* FRECPX */
9514                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9515                 break;
9516             case 0x7d: /* FRSQRTE */
9517                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9518                 break;
9519             default:
9520                 g_assert_not_reached();
9521             }
9522 
9523             if (is_scalar) {
9524                 write_fp_sreg(s, rd, tcg_res);
9525             } else {
9526                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9527             }
9528         }
9529         if (!is_scalar) {
9530             clear_vec_high(s, is_q, rd);
9531         }
9532     }
9533 }
9534 
9535 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9536                                 int opcode, bool u, bool is_q,
9537                                 int size, int rn, int rd)
9538 {
9539     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9540      * in the source becomes a size element in the destination).
9541      */
9542     int pass;
9543     TCGv_i32 tcg_res[2];
9544     int destelt = is_q ? 2 : 0;
9545     int passes = scalar ? 1 : 2;
9546 
9547     if (scalar) {
9548         tcg_res[1] = tcg_constant_i32(0);
9549     }
9550 
9551     for (pass = 0; pass < passes; pass++) {
9552         TCGv_i64 tcg_op = tcg_temp_new_i64();
9553         NeonGenNarrowFn *genfn = NULL;
9554         NeonGenNarrowEnvFn *genenvfn = NULL;
9555 
9556         if (scalar) {
9557             read_vec_element(s, tcg_op, rn, pass, size + 1);
9558         } else {
9559             read_vec_element(s, tcg_op, rn, pass, MO_64);
9560         }
9561         tcg_res[pass] = tcg_temp_new_i32();
9562 
9563         switch (opcode) {
9564         case 0x12: /* XTN, SQXTUN */
9565         {
9566             static NeonGenNarrowFn * const xtnfns[3] = {
9567                 gen_helper_neon_narrow_u8,
9568                 gen_helper_neon_narrow_u16,
9569                 tcg_gen_extrl_i64_i32,
9570             };
9571             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9572                 gen_helper_neon_unarrow_sat8,
9573                 gen_helper_neon_unarrow_sat16,
9574                 gen_helper_neon_unarrow_sat32,
9575             };
9576             if (u) {
9577                 genenvfn = sqxtunfns[size];
9578             } else {
9579                 genfn = xtnfns[size];
9580             }
9581             break;
9582         }
9583         case 0x14: /* SQXTN, UQXTN */
9584         {
9585             static NeonGenNarrowEnvFn * const fns[3][2] = {
9586                 { gen_helper_neon_narrow_sat_s8,
9587                   gen_helper_neon_narrow_sat_u8 },
9588                 { gen_helper_neon_narrow_sat_s16,
9589                   gen_helper_neon_narrow_sat_u16 },
9590                 { gen_helper_neon_narrow_sat_s32,
9591                   gen_helper_neon_narrow_sat_u32 },
9592             };
9593             genenvfn = fns[size][u];
9594             break;
9595         }
9596         case 0x16: /* FCVTN, FCVTN2 */
9597             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9598             if (size == 2) {
9599                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
9600             } else {
9601                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9602                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9603                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9604                 TCGv_i32 ahp = get_ahp_flag();
9605 
9606                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9607                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9608                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9609                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9610             }
9611             break;
9612         case 0x36: /* BFCVTN, BFCVTN2 */
9613             {
9614                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9615                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
9616             }
9617             break;
9618         case 0x56:  /* FCVTXN, FCVTXN2 */
9619             /* 64 bit to 32 bit float conversion
9620              * with von Neumann rounding (round to odd)
9621              */
9622             assert(size == 2);
9623             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
9624             break;
9625         default:
9626             g_assert_not_reached();
9627         }
9628 
9629         if (genfn) {
9630             genfn(tcg_res[pass], tcg_op);
9631         } else if (genenvfn) {
9632             genenvfn(tcg_res[pass], cpu_env, tcg_op);
9633         }
9634     }
9635 
9636     for (pass = 0; pass < 2; pass++) {
9637         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
9638     }
9639     clear_vec_high(s, is_q, rd);
9640 }
9641 
9642 /* Remaining saturating accumulating ops */
9643 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
9644                                 bool is_q, int size, int rn, int rd)
9645 {
9646     bool is_double = (size == 3);
9647 
9648     if (is_double) {
9649         TCGv_i64 tcg_rn = tcg_temp_new_i64();
9650         TCGv_i64 tcg_rd = tcg_temp_new_i64();
9651         int pass;
9652 
9653         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9654             read_vec_element(s, tcg_rn, rn, pass, MO_64);
9655             read_vec_element(s, tcg_rd, rd, pass, MO_64);
9656 
9657             if (is_u) { /* USQADD */
9658                 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9659             } else { /* SUQADD */
9660                 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9661             }
9662             write_vec_element(s, tcg_rd, rd, pass, MO_64);
9663         }
9664         clear_vec_high(s, !is_scalar, rd);
9665     } else {
9666         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9667         TCGv_i32 tcg_rd = tcg_temp_new_i32();
9668         int pass, maxpasses;
9669 
9670         if (is_scalar) {
9671             maxpasses = 1;
9672         } else {
9673             maxpasses = is_q ? 4 : 2;
9674         }
9675 
9676         for (pass = 0; pass < maxpasses; pass++) {
9677             if (is_scalar) {
9678                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
9679                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
9680             } else {
9681                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
9682                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9683             }
9684 
9685             if (is_u) { /* USQADD */
9686                 switch (size) {
9687                 case 0:
9688                     gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9689                     break;
9690                 case 1:
9691                     gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9692                     break;
9693                 case 2:
9694                     gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9695                     break;
9696                 default:
9697                     g_assert_not_reached();
9698                 }
9699             } else { /* SUQADD */
9700                 switch (size) {
9701                 case 0:
9702                     gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9703                     break;
9704                 case 1:
9705                     gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9706                     break;
9707                 case 2:
9708                     gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9709                     break;
9710                 default:
9711                     g_assert_not_reached();
9712                 }
9713             }
9714 
9715             if (is_scalar) {
9716                 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
9717             }
9718             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9719         }
9720         clear_vec_high(s, is_q, rd);
9721     }
9722 }
9723 
9724 /* AdvSIMD scalar two reg misc
9725  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
9726  * +-----+---+-----------+------+-----------+--------+-----+------+------+
9727  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
9728  * +-----+---+-----------+------+-----------+--------+-----+------+------+
9729  */
9730 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
9731 {
9732     int rd = extract32(insn, 0, 5);
9733     int rn = extract32(insn, 5, 5);
9734     int opcode = extract32(insn, 12, 5);
9735     int size = extract32(insn, 22, 2);
9736     bool u = extract32(insn, 29, 1);
9737     bool is_fcvt = false;
9738     int rmode;
9739     TCGv_i32 tcg_rmode;
9740     TCGv_ptr tcg_fpstatus;
9741 
9742     switch (opcode) {
9743     case 0x3: /* USQADD / SUQADD*/
9744         if (!fp_access_check(s)) {
9745             return;
9746         }
9747         handle_2misc_satacc(s, true, u, false, size, rn, rd);
9748         return;
9749     case 0x7: /* SQABS / SQNEG */
9750         break;
9751     case 0xa: /* CMLT */
9752         if (u) {
9753             unallocated_encoding(s);
9754             return;
9755         }
9756         /* fall through */
9757     case 0x8: /* CMGT, CMGE */
9758     case 0x9: /* CMEQ, CMLE */
9759     case 0xb: /* ABS, NEG */
9760         if (size != 3) {
9761             unallocated_encoding(s);
9762             return;
9763         }
9764         break;
9765     case 0x12: /* SQXTUN */
9766         if (!u) {
9767             unallocated_encoding(s);
9768             return;
9769         }
9770         /* fall through */
9771     case 0x14: /* SQXTN, UQXTN */
9772         if (size == 3) {
9773             unallocated_encoding(s);
9774             return;
9775         }
9776         if (!fp_access_check(s)) {
9777             return;
9778         }
9779         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
9780         return;
9781     case 0xc ... 0xf:
9782     case 0x16 ... 0x1d:
9783     case 0x1f:
9784         /* Floating point: U, size[1] and opcode indicate operation;
9785          * size[0] indicates single or double precision.
9786          */
9787         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
9788         size = extract32(size, 0, 1) ? 3 : 2;
9789         switch (opcode) {
9790         case 0x2c: /* FCMGT (zero) */
9791         case 0x2d: /* FCMEQ (zero) */
9792         case 0x2e: /* FCMLT (zero) */
9793         case 0x6c: /* FCMGE (zero) */
9794         case 0x6d: /* FCMLE (zero) */
9795             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
9796             return;
9797         case 0x1d: /* SCVTF */
9798         case 0x5d: /* UCVTF */
9799         {
9800             bool is_signed = (opcode == 0x1d);
9801             if (!fp_access_check(s)) {
9802                 return;
9803             }
9804             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
9805             return;
9806         }
9807         case 0x3d: /* FRECPE */
9808         case 0x3f: /* FRECPX */
9809         case 0x7d: /* FRSQRTE */
9810             if (!fp_access_check(s)) {
9811                 return;
9812             }
9813             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
9814             return;
9815         case 0x1a: /* FCVTNS */
9816         case 0x1b: /* FCVTMS */
9817         case 0x3a: /* FCVTPS */
9818         case 0x3b: /* FCVTZS */
9819         case 0x5a: /* FCVTNU */
9820         case 0x5b: /* FCVTMU */
9821         case 0x7a: /* FCVTPU */
9822         case 0x7b: /* FCVTZU */
9823             is_fcvt = true;
9824             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
9825             break;
9826         case 0x1c: /* FCVTAS */
9827         case 0x5c: /* FCVTAU */
9828             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
9829             is_fcvt = true;
9830             rmode = FPROUNDING_TIEAWAY;
9831             break;
9832         case 0x56: /* FCVTXN, FCVTXN2 */
9833             if (size == 2) {
9834                 unallocated_encoding(s);
9835                 return;
9836             }
9837             if (!fp_access_check(s)) {
9838                 return;
9839             }
9840             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
9841             return;
9842         default:
9843             unallocated_encoding(s);
9844             return;
9845         }
9846         break;
9847     default:
9848         unallocated_encoding(s);
9849         return;
9850     }
9851 
9852     if (!fp_access_check(s)) {
9853         return;
9854     }
9855 
9856     if (is_fcvt) {
9857         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
9858         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
9859     } else {
9860         tcg_fpstatus = NULL;
9861         tcg_rmode = NULL;
9862     }
9863 
9864     if (size == 3) {
9865         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9866         TCGv_i64 tcg_rd = tcg_temp_new_i64();
9867 
9868         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
9869         write_fp_dreg(s, rd, tcg_rd);
9870     } else {
9871         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9872         TCGv_i32 tcg_rd = tcg_temp_new_i32();
9873 
9874         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9875 
9876         switch (opcode) {
9877         case 0x7: /* SQABS, SQNEG */
9878         {
9879             NeonGenOneOpEnvFn *genfn;
9880             static NeonGenOneOpEnvFn * const fns[3][2] = {
9881                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
9882                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
9883                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
9884             };
9885             genfn = fns[size][u];
9886             genfn(tcg_rd, cpu_env, tcg_rn);
9887             break;
9888         }
9889         case 0x1a: /* FCVTNS */
9890         case 0x1b: /* FCVTMS */
9891         case 0x1c: /* FCVTAS */
9892         case 0x3a: /* FCVTPS */
9893         case 0x3b: /* FCVTZS */
9894             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
9895                                  tcg_fpstatus);
9896             break;
9897         case 0x5a: /* FCVTNU */
9898         case 0x5b: /* FCVTMU */
9899         case 0x5c: /* FCVTAU */
9900         case 0x7a: /* FCVTPU */
9901         case 0x7b: /* FCVTZU */
9902             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
9903                                  tcg_fpstatus);
9904             break;
9905         default:
9906             g_assert_not_reached();
9907         }
9908 
9909         write_fp_sreg(s, rd, tcg_rd);
9910     }
9911 
9912     if (is_fcvt) {
9913         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
9914     }
9915 }
9916 
9917 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
9918 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
9919                                  int immh, int immb, int opcode, int rn, int rd)
9920 {
9921     int size = 32 - clz32(immh) - 1;
9922     int immhb = immh << 3 | immb;
9923     int shift = 2 * (8 << size) - immhb;
9924     GVecGen2iFn *gvec_fn;
9925 
9926     if (extract32(immh, 3, 1) && !is_q) {
9927         unallocated_encoding(s);
9928         return;
9929     }
9930     tcg_debug_assert(size <= 3);
9931 
9932     if (!fp_access_check(s)) {
9933         return;
9934     }
9935 
9936     switch (opcode) {
9937     case 0x02: /* SSRA / USRA (accumulate) */
9938         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
9939         break;
9940 
9941     case 0x08: /* SRI */
9942         gvec_fn = gen_gvec_sri;
9943         break;
9944 
9945     case 0x00: /* SSHR / USHR */
9946         if (is_u) {
9947             if (shift == 8 << size) {
9948                 /* Shift count the same size as element size produces zero.  */
9949                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
9950                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
9951                 return;
9952             }
9953             gvec_fn = tcg_gen_gvec_shri;
9954         } else {
9955             /* Shift count the same size as element size produces all sign.  */
9956             if (shift == 8 << size) {
9957                 shift -= 1;
9958             }
9959             gvec_fn = tcg_gen_gvec_sari;
9960         }
9961         break;
9962 
9963     case 0x04: /* SRSHR / URSHR (rounding) */
9964         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
9965         break;
9966 
9967     case 0x06: /* SRSRA / URSRA (accum + rounding) */
9968         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
9969         break;
9970 
9971     default:
9972         g_assert_not_reached();
9973     }
9974 
9975     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
9976 }
9977 
9978 /* SHL/SLI - Vector shift left */
9979 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
9980                                  int immh, int immb, int opcode, int rn, int rd)
9981 {
9982     int size = 32 - clz32(immh) - 1;
9983     int immhb = immh << 3 | immb;
9984     int shift = immhb - (8 << size);
9985 
9986     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
9987     assert(size >= 0 && size <= 3);
9988 
9989     if (extract32(immh, 3, 1) && !is_q) {
9990         unallocated_encoding(s);
9991         return;
9992     }
9993 
9994     if (!fp_access_check(s)) {
9995         return;
9996     }
9997 
9998     if (insert) {
9999         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10000     } else {
10001         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10002     }
10003 }
10004 
10005 /* USHLL/SHLL - Vector shift left with widening */
10006 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10007                                  int immh, int immb, int opcode, int rn, int rd)
10008 {
10009     int size = 32 - clz32(immh) - 1;
10010     int immhb = immh << 3 | immb;
10011     int shift = immhb - (8 << size);
10012     int dsize = 64;
10013     int esize = 8 << size;
10014     int elements = dsize/esize;
10015     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10016     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10017     int i;
10018 
10019     if (size >= 3) {
10020         unallocated_encoding(s);
10021         return;
10022     }
10023 
10024     if (!fp_access_check(s)) {
10025         return;
10026     }
10027 
10028     /* For the LL variants the store is larger than the load,
10029      * so if rd == rn we would overwrite parts of our input.
10030      * So load everything right now and use shifts in the main loop.
10031      */
10032     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10033 
10034     for (i = 0; i < elements; i++) {
10035         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10036         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10037         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10038         write_vec_element(s, tcg_rd, rd, i, size + 1);
10039     }
10040 }
10041 
10042 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10043 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10044                                  int immh, int immb, int opcode, int rn, int rd)
10045 {
10046     int immhb = immh << 3 | immb;
10047     int size = 32 - clz32(immh) - 1;
10048     int dsize = 64;
10049     int esize = 8 << size;
10050     int elements = dsize/esize;
10051     int shift = (2 * esize) - immhb;
10052     bool round = extract32(opcode, 0, 1);
10053     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10054     TCGv_i64 tcg_round;
10055     int i;
10056 
10057     if (extract32(immh, 3, 1)) {
10058         unallocated_encoding(s);
10059         return;
10060     }
10061 
10062     if (!fp_access_check(s)) {
10063         return;
10064     }
10065 
10066     tcg_rn = tcg_temp_new_i64();
10067     tcg_rd = tcg_temp_new_i64();
10068     tcg_final = tcg_temp_new_i64();
10069     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10070 
10071     if (round) {
10072         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10073     } else {
10074         tcg_round = NULL;
10075     }
10076 
10077     for (i = 0; i < elements; i++) {
10078         read_vec_element(s, tcg_rn, rn, i, size+1);
10079         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10080                                 false, true, size+1, shift);
10081 
10082         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10083     }
10084 
10085     if (!is_q) {
10086         write_vec_element(s, tcg_final, rd, 0, MO_64);
10087     } else {
10088         write_vec_element(s, tcg_final, rd, 1, MO_64);
10089     }
10090 
10091     clear_vec_high(s, is_q, rd);
10092 }
10093 
10094 
10095 /* AdvSIMD shift by immediate
10096  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10097  * +---+---+---+-------------+------+------+--------+---+------+------+
10098  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10099  * +---+---+---+-------------+------+------+--------+---+------+------+
10100  */
10101 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10102 {
10103     int rd = extract32(insn, 0, 5);
10104     int rn = extract32(insn, 5, 5);
10105     int opcode = extract32(insn, 11, 5);
10106     int immb = extract32(insn, 16, 3);
10107     int immh = extract32(insn, 19, 4);
10108     bool is_u = extract32(insn, 29, 1);
10109     bool is_q = extract32(insn, 30, 1);
10110 
10111     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10112     assert(immh != 0);
10113 
10114     switch (opcode) {
10115     case 0x08: /* SRI */
10116         if (!is_u) {
10117             unallocated_encoding(s);
10118             return;
10119         }
10120         /* fall through */
10121     case 0x00: /* SSHR / USHR */
10122     case 0x02: /* SSRA / USRA (accumulate) */
10123     case 0x04: /* SRSHR / URSHR (rounding) */
10124     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10125         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10126         break;
10127     case 0x0a: /* SHL / SLI */
10128         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10129         break;
10130     case 0x10: /* SHRN */
10131     case 0x11: /* RSHRN / SQRSHRUN */
10132         if (is_u) {
10133             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10134                                    opcode, rn, rd);
10135         } else {
10136             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10137         }
10138         break;
10139     case 0x12: /* SQSHRN / UQSHRN */
10140     case 0x13: /* SQRSHRN / UQRSHRN */
10141         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10142                                opcode, rn, rd);
10143         break;
10144     case 0x14: /* SSHLL / USHLL */
10145         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10146         break;
10147     case 0x1c: /* SCVTF / UCVTF */
10148         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10149                                      opcode, rn, rd);
10150         break;
10151     case 0xc: /* SQSHLU */
10152         if (!is_u) {
10153             unallocated_encoding(s);
10154             return;
10155         }
10156         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10157         break;
10158     case 0xe: /* SQSHL, UQSHL */
10159         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10160         break;
10161     case 0x1f: /* FCVTZS/ FCVTZU */
10162         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10163         return;
10164     default:
10165         unallocated_encoding(s);
10166         return;
10167     }
10168 }
10169 
10170 /* Generate code to do a "long" addition or subtraction, ie one done in
10171  * TCGv_i64 on vector lanes twice the width specified by size.
10172  */
10173 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10174                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10175 {
10176     static NeonGenTwo64OpFn * const fns[3][2] = {
10177         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10178         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10179         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10180     };
10181     NeonGenTwo64OpFn *genfn;
10182     assert(size < 3);
10183 
10184     genfn = fns[size][is_sub];
10185     genfn(tcg_res, tcg_op1, tcg_op2);
10186 }
10187 
10188 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10189                                 int opcode, int rd, int rn, int rm)
10190 {
10191     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10192     TCGv_i64 tcg_res[2];
10193     int pass, accop;
10194 
10195     tcg_res[0] = tcg_temp_new_i64();
10196     tcg_res[1] = tcg_temp_new_i64();
10197 
10198     /* Does this op do an adding accumulate, a subtracting accumulate,
10199      * or no accumulate at all?
10200      */
10201     switch (opcode) {
10202     case 5:
10203     case 8:
10204     case 9:
10205         accop = 1;
10206         break;
10207     case 10:
10208     case 11:
10209         accop = -1;
10210         break;
10211     default:
10212         accop = 0;
10213         break;
10214     }
10215 
10216     if (accop != 0) {
10217         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10218         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10219     }
10220 
10221     /* size == 2 means two 32x32->64 operations; this is worth special
10222      * casing because we can generally handle it inline.
10223      */
10224     if (size == 2) {
10225         for (pass = 0; pass < 2; pass++) {
10226             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10227             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10228             TCGv_i64 tcg_passres;
10229             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10230 
10231             int elt = pass + is_q * 2;
10232 
10233             read_vec_element(s, tcg_op1, rn, elt, memop);
10234             read_vec_element(s, tcg_op2, rm, elt, memop);
10235 
10236             if (accop == 0) {
10237                 tcg_passres = tcg_res[pass];
10238             } else {
10239                 tcg_passres = tcg_temp_new_i64();
10240             }
10241 
10242             switch (opcode) {
10243             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10244                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10245                 break;
10246             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10247                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10248                 break;
10249             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10250             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10251             {
10252                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10253                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10254 
10255                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10256                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10257                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10258                                     tcg_passres,
10259                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10260                 break;
10261             }
10262             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10263             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10264             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10265                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10266                 break;
10267             case 9: /* SQDMLAL, SQDMLAL2 */
10268             case 11: /* SQDMLSL, SQDMLSL2 */
10269             case 13: /* SQDMULL, SQDMULL2 */
10270                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10271                 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10272                                                   tcg_passres, tcg_passres);
10273                 break;
10274             default:
10275                 g_assert_not_reached();
10276             }
10277 
10278             if (opcode == 9 || opcode == 11) {
10279                 /* saturating accumulate ops */
10280                 if (accop < 0) {
10281                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10282                 }
10283                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10284                                                   tcg_res[pass], tcg_passres);
10285             } else if (accop > 0) {
10286                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10287             } else if (accop < 0) {
10288                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10289             }
10290         }
10291     } else {
10292         /* size 0 or 1, generally helper functions */
10293         for (pass = 0; pass < 2; pass++) {
10294             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10295             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10296             TCGv_i64 tcg_passres;
10297             int elt = pass + is_q * 2;
10298 
10299             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10300             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10301 
10302             if (accop == 0) {
10303                 tcg_passres = tcg_res[pass];
10304             } else {
10305                 tcg_passres = tcg_temp_new_i64();
10306             }
10307 
10308             switch (opcode) {
10309             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10310             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10311             {
10312                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10313                 static NeonGenWidenFn * const widenfns[2][2] = {
10314                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10315                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10316                 };
10317                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10318 
10319                 widenfn(tcg_op2_64, tcg_op2);
10320                 widenfn(tcg_passres, tcg_op1);
10321                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10322                               tcg_passres, tcg_op2_64);
10323                 break;
10324             }
10325             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10326             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10327                 if (size == 0) {
10328                     if (is_u) {
10329                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10330                     } else {
10331                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10332                     }
10333                 } else {
10334                     if (is_u) {
10335                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10336                     } else {
10337                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10338                     }
10339                 }
10340                 break;
10341             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10342             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10343             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10344                 if (size == 0) {
10345                     if (is_u) {
10346                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10347                     } else {
10348                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10349                     }
10350                 } else {
10351                     if (is_u) {
10352                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10353                     } else {
10354                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10355                     }
10356                 }
10357                 break;
10358             case 9: /* SQDMLAL, SQDMLAL2 */
10359             case 11: /* SQDMLSL, SQDMLSL2 */
10360             case 13: /* SQDMULL, SQDMULL2 */
10361                 assert(size == 1);
10362                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10363                 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10364                                                   tcg_passres, tcg_passres);
10365                 break;
10366             default:
10367                 g_assert_not_reached();
10368             }
10369 
10370             if (accop != 0) {
10371                 if (opcode == 9 || opcode == 11) {
10372                     /* saturating accumulate ops */
10373                     if (accop < 0) {
10374                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10375                     }
10376                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10377                                                       tcg_res[pass],
10378                                                       tcg_passres);
10379                 } else {
10380                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10381                                   tcg_res[pass], tcg_passres);
10382                 }
10383             }
10384         }
10385     }
10386 
10387     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10388     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10389 }
10390 
10391 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10392                             int opcode, int rd, int rn, int rm)
10393 {
10394     TCGv_i64 tcg_res[2];
10395     int part = is_q ? 2 : 0;
10396     int pass;
10397 
10398     for (pass = 0; pass < 2; pass++) {
10399         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10400         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10401         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10402         static NeonGenWidenFn * const widenfns[3][2] = {
10403             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10404             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10405             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10406         };
10407         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10408 
10409         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10410         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10411         widenfn(tcg_op2_wide, tcg_op2);
10412         tcg_res[pass] = tcg_temp_new_i64();
10413         gen_neon_addl(size, (opcode == 3),
10414                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10415     }
10416 
10417     for (pass = 0; pass < 2; pass++) {
10418         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10419     }
10420 }
10421 
10422 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10423 {
10424     tcg_gen_addi_i64(in, in, 1U << 31);
10425     tcg_gen_extrh_i64_i32(res, in);
10426 }
10427 
10428 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10429                                  int opcode, int rd, int rn, int rm)
10430 {
10431     TCGv_i32 tcg_res[2];
10432     int part = is_q ? 2 : 0;
10433     int pass;
10434 
10435     for (pass = 0; pass < 2; pass++) {
10436         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10437         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10438         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10439         static NeonGenNarrowFn * const narrowfns[3][2] = {
10440             { gen_helper_neon_narrow_high_u8,
10441               gen_helper_neon_narrow_round_high_u8 },
10442             { gen_helper_neon_narrow_high_u16,
10443               gen_helper_neon_narrow_round_high_u16 },
10444             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10445         };
10446         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10447 
10448         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10449         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10450 
10451         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10452 
10453         tcg_res[pass] = tcg_temp_new_i32();
10454         gennarrow(tcg_res[pass], tcg_wideres);
10455     }
10456 
10457     for (pass = 0; pass < 2; pass++) {
10458         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10459     }
10460     clear_vec_high(s, is_q, rd);
10461 }
10462 
10463 /* AdvSIMD three different
10464  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10465  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10466  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10467  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10468  */
10469 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10470 {
10471     /* Instructions in this group fall into three basic classes
10472      * (in each case with the operation working on each element in
10473      * the input vectors):
10474      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10475      *     128 bit input)
10476      * (2) wide 64 x 128 -> 128
10477      * (3) narrowing 128 x 128 -> 64
10478      * Here we do initial decode, catch unallocated cases and
10479      * dispatch to separate functions for each class.
10480      */
10481     int is_q = extract32(insn, 30, 1);
10482     int is_u = extract32(insn, 29, 1);
10483     int size = extract32(insn, 22, 2);
10484     int opcode = extract32(insn, 12, 4);
10485     int rm = extract32(insn, 16, 5);
10486     int rn = extract32(insn, 5, 5);
10487     int rd = extract32(insn, 0, 5);
10488 
10489     switch (opcode) {
10490     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10491     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10492         /* 64 x 128 -> 128 */
10493         if (size == 3) {
10494             unallocated_encoding(s);
10495             return;
10496         }
10497         if (!fp_access_check(s)) {
10498             return;
10499         }
10500         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10501         break;
10502     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10503     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10504         /* 128 x 128 -> 64 */
10505         if (size == 3) {
10506             unallocated_encoding(s);
10507             return;
10508         }
10509         if (!fp_access_check(s)) {
10510             return;
10511         }
10512         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10513         break;
10514     case 14: /* PMULL, PMULL2 */
10515         if (is_u) {
10516             unallocated_encoding(s);
10517             return;
10518         }
10519         switch (size) {
10520         case 0: /* PMULL.P8 */
10521             if (!fp_access_check(s)) {
10522                 return;
10523             }
10524             /* The Q field specifies lo/hi half input for this insn.  */
10525             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10526                              gen_helper_neon_pmull_h);
10527             break;
10528 
10529         case 3: /* PMULL.P64 */
10530             if (!dc_isar_feature(aa64_pmull, s)) {
10531                 unallocated_encoding(s);
10532                 return;
10533             }
10534             if (!fp_access_check(s)) {
10535                 return;
10536             }
10537             /* The Q field specifies lo/hi half input for this insn.  */
10538             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10539                              gen_helper_gvec_pmull_q);
10540             break;
10541 
10542         default:
10543             unallocated_encoding(s);
10544             break;
10545         }
10546         return;
10547     case 9: /* SQDMLAL, SQDMLAL2 */
10548     case 11: /* SQDMLSL, SQDMLSL2 */
10549     case 13: /* SQDMULL, SQDMULL2 */
10550         if (is_u || size == 0) {
10551             unallocated_encoding(s);
10552             return;
10553         }
10554         /* fall through */
10555     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10556     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10557     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10558     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10559     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10560     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10561     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10562         /* 64 x 64 -> 128 */
10563         if (size == 3) {
10564             unallocated_encoding(s);
10565             return;
10566         }
10567         if (!fp_access_check(s)) {
10568             return;
10569         }
10570 
10571         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10572         break;
10573     default:
10574         /* opcode 15 not allocated */
10575         unallocated_encoding(s);
10576         break;
10577     }
10578 }
10579 
10580 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10581 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10582 {
10583     int rd = extract32(insn, 0, 5);
10584     int rn = extract32(insn, 5, 5);
10585     int rm = extract32(insn, 16, 5);
10586     int size = extract32(insn, 22, 2);
10587     bool is_u = extract32(insn, 29, 1);
10588     bool is_q = extract32(insn, 30, 1);
10589 
10590     if (!fp_access_check(s)) {
10591         return;
10592     }
10593 
10594     switch (size + 4 * is_u) {
10595     case 0: /* AND */
10596         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10597         return;
10598     case 1: /* BIC */
10599         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10600         return;
10601     case 2: /* ORR */
10602         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10603         return;
10604     case 3: /* ORN */
10605         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10606         return;
10607     case 4: /* EOR */
10608         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10609         return;
10610 
10611     case 5: /* BSL bitwise select */
10612         gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
10613         return;
10614     case 6: /* BIT, bitwise insert if true */
10615         gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
10616         return;
10617     case 7: /* BIF, bitwise insert if false */
10618         gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
10619         return;
10620 
10621     default:
10622         g_assert_not_reached();
10623     }
10624 }
10625 
10626 /* Pairwise op subgroup of C3.6.16.
10627  *
10628  * This is called directly or via the handle_3same_float for float pairwise
10629  * operations where the opcode and size are calculated differently.
10630  */
10631 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
10632                                    int size, int rn, int rm, int rd)
10633 {
10634     TCGv_ptr fpst;
10635     int pass;
10636 
10637     /* Floating point operations need fpst */
10638     if (opcode >= 0x58) {
10639         fpst = fpstatus_ptr(FPST_FPCR);
10640     } else {
10641         fpst = NULL;
10642     }
10643 
10644     if (!fp_access_check(s)) {
10645         return;
10646     }
10647 
10648     /* These operations work on the concatenated rm:rn, with each pair of
10649      * adjacent elements being operated on to produce an element in the result.
10650      */
10651     if (size == 3) {
10652         TCGv_i64 tcg_res[2];
10653 
10654         for (pass = 0; pass < 2; pass++) {
10655             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10656             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10657             int passreg = (pass == 0) ? rn : rm;
10658 
10659             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
10660             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
10661             tcg_res[pass] = tcg_temp_new_i64();
10662 
10663             switch (opcode) {
10664             case 0x17: /* ADDP */
10665                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
10666                 break;
10667             case 0x58: /* FMAXNMP */
10668                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10669                 break;
10670             case 0x5a: /* FADDP */
10671                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10672                 break;
10673             case 0x5e: /* FMAXP */
10674                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10675                 break;
10676             case 0x78: /* FMINNMP */
10677                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10678                 break;
10679             case 0x7e: /* FMINP */
10680                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10681                 break;
10682             default:
10683                 g_assert_not_reached();
10684             }
10685         }
10686 
10687         for (pass = 0; pass < 2; pass++) {
10688             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10689         }
10690     } else {
10691         int maxpass = is_q ? 4 : 2;
10692         TCGv_i32 tcg_res[4];
10693 
10694         for (pass = 0; pass < maxpass; pass++) {
10695             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10696             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10697             NeonGenTwoOpFn *genfn = NULL;
10698             int passreg = pass < (maxpass / 2) ? rn : rm;
10699             int passelt = (is_q && (pass & 1)) ? 2 : 0;
10700 
10701             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
10702             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
10703             tcg_res[pass] = tcg_temp_new_i32();
10704 
10705             switch (opcode) {
10706             case 0x17: /* ADDP */
10707             {
10708                 static NeonGenTwoOpFn * const fns[3] = {
10709                     gen_helper_neon_padd_u8,
10710                     gen_helper_neon_padd_u16,
10711                     tcg_gen_add_i32,
10712                 };
10713                 genfn = fns[size];
10714                 break;
10715             }
10716             case 0x14: /* SMAXP, UMAXP */
10717             {
10718                 static NeonGenTwoOpFn * const fns[3][2] = {
10719                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
10720                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
10721                     { tcg_gen_smax_i32, tcg_gen_umax_i32 },
10722                 };
10723                 genfn = fns[size][u];
10724                 break;
10725             }
10726             case 0x15: /* SMINP, UMINP */
10727             {
10728                 static NeonGenTwoOpFn * const fns[3][2] = {
10729                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
10730                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
10731                     { tcg_gen_smin_i32, tcg_gen_umin_i32 },
10732                 };
10733                 genfn = fns[size][u];
10734                 break;
10735             }
10736             /* The FP operations are all on single floats (32 bit) */
10737             case 0x58: /* FMAXNMP */
10738                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10739                 break;
10740             case 0x5a: /* FADDP */
10741                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10742                 break;
10743             case 0x5e: /* FMAXP */
10744                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10745                 break;
10746             case 0x78: /* FMINNMP */
10747                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10748                 break;
10749             case 0x7e: /* FMINP */
10750                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10751                 break;
10752             default:
10753                 g_assert_not_reached();
10754             }
10755 
10756             /* FP ops called directly, otherwise call now */
10757             if (genfn) {
10758                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
10759             }
10760         }
10761 
10762         for (pass = 0; pass < maxpass; pass++) {
10763             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
10764         }
10765         clear_vec_high(s, is_q, rd);
10766     }
10767 }
10768 
10769 /* Floating point op subgroup of C3.6.16. */
10770 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
10771 {
10772     /* For floating point ops, the U, size[1] and opcode bits
10773      * together indicate the operation. size[0] indicates single
10774      * or double.
10775      */
10776     int fpopcode = extract32(insn, 11, 5)
10777         | (extract32(insn, 23, 1) << 5)
10778         | (extract32(insn, 29, 1) << 6);
10779     int is_q = extract32(insn, 30, 1);
10780     int size = extract32(insn, 22, 1);
10781     int rm = extract32(insn, 16, 5);
10782     int rn = extract32(insn, 5, 5);
10783     int rd = extract32(insn, 0, 5);
10784 
10785     int datasize = is_q ? 128 : 64;
10786     int esize = 32 << size;
10787     int elements = datasize / esize;
10788 
10789     if (size == 1 && !is_q) {
10790         unallocated_encoding(s);
10791         return;
10792     }
10793 
10794     switch (fpopcode) {
10795     case 0x58: /* FMAXNMP */
10796     case 0x5a: /* FADDP */
10797     case 0x5e: /* FMAXP */
10798     case 0x78: /* FMINNMP */
10799     case 0x7e: /* FMINP */
10800         if (size && !is_q) {
10801             unallocated_encoding(s);
10802             return;
10803         }
10804         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
10805                                rn, rm, rd);
10806         return;
10807     case 0x1b: /* FMULX */
10808     case 0x1f: /* FRECPS */
10809     case 0x3f: /* FRSQRTS */
10810     case 0x5d: /* FACGE */
10811     case 0x7d: /* FACGT */
10812     case 0x19: /* FMLA */
10813     case 0x39: /* FMLS */
10814     case 0x18: /* FMAXNM */
10815     case 0x1a: /* FADD */
10816     case 0x1c: /* FCMEQ */
10817     case 0x1e: /* FMAX */
10818     case 0x38: /* FMINNM */
10819     case 0x3a: /* FSUB */
10820     case 0x3e: /* FMIN */
10821     case 0x5b: /* FMUL */
10822     case 0x5c: /* FCMGE */
10823     case 0x5f: /* FDIV */
10824     case 0x7a: /* FABD */
10825     case 0x7c: /* FCMGT */
10826         if (!fp_access_check(s)) {
10827             return;
10828         }
10829         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
10830         return;
10831 
10832     case 0x1d: /* FMLAL  */
10833     case 0x3d: /* FMLSL  */
10834     case 0x59: /* FMLAL2 */
10835     case 0x79: /* FMLSL2 */
10836         if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
10837             unallocated_encoding(s);
10838             return;
10839         }
10840         if (fp_access_check(s)) {
10841             int is_s = extract32(insn, 23, 1);
10842             int is_2 = extract32(insn, 29, 1);
10843             int data = (is_2 << 1) | is_s;
10844             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
10845                                vec_full_reg_offset(s, rn),
10846                                vec_full_reg_offset(s, rm), cpu_env,
10847                                is_q ? 16 : 8, vec_full_reg_size(s),
10848                                data, gen_helper_gvec_fmlal_a64);
10849         }
10850         return;
10851 
10852     default:
10853         unallocated_encoding(s);
10854         return;
10855     }
10856 }
10857 
10858 /* Integer op subgroup of C3.6.16. */
10859 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
10860 {
10861     int is_q = extract32(insn, 30, 1);
10862     int u = extract32(insn, 29, 1);
10863     int size = extract32(insn, 22, 2);
10864     int opcode = extract32(insn, 11, 5);
10865     int rm = extract32(insn, 16, 5);
10866     int rn = extract32(insn, 5, 5);
10867     int rd = extract32(insn, 0, 5);
10868     int pass;
10869     TCGCond cond;
10870 
10871     switch (opcode) {
10872     case 0x13: /* MUL, PMUL */
10873         if (u && size != 0) {
10874             unallocated_encoding(s);
10875             return;
10876         }
10877         /* fall through */
10878     case 0x0: /* SHADD, UHADD */
10879     case 0x2: /* SRHADD, URHADD */
10880     case 0x4: /* SHSUB, UHSUB */
10881     case 0xc: /* SMAX, UMAX */
10882     case 0xd: /* SMIN, UMIN */
10883     case 0xe: /* SABD, UABD */
10884     case 0xf: /* SABA, UABA */
10885     case 0x12: /* MLA, MLS */
10886         if (size == 3) {
10887             unallocated_encoding(s);
10888             return;
10889         }
10890         break;
10891     case 0x16: /* SQDMULH, SQRDMULH */
10892         if (size == 0 || size == 3) {
10893             unallocated_encoding(s);
10894             return;
10895         }
10896         break;
10897     default:
10898         if (size == 3 && !is_q) {
10899             unallocated_encoding(s);
10900             return;
10901         }
10902         break;
10903     }
10904 
10905     if (!fp_access_check(s)) {
10906         return;
10907     }
10908 
10909     switch (opcode) {
10910     case 0x01: /* SQADD, UQADD */
10911         if (u) {
10912             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
10913         } else {
10914             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
10915         }
10916         return;
10917     case 0x05: /* SQSUB, UQSUB */
10918         if (u) {
10919             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
10920         } else {
10921             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
10922         }
10923         return;
10924     case 0x08: /* SSHL, USHL */
10925         if (u) {
10926             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
10927         } else {
10928             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
10929         }
10930         return;
10931     case 0x0c: /* SMAX, UMAX */
10932         if (u) {
10933             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
10934         } else {
10935             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
10936         }
10937         return;
10938     case 0x0d: /* SMIN, UMIN */
10939         if (u) {
10940             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
10941         } else {
10942             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
10943         }
10944         return;
10945     case 0xe: /* SABD, UABD */
10946         if (u) {
10947             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
10948         } else {
10949             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
10950         }
10951         return;
10952     case 0xf: /* SABA, UABA */
10953         if (u) {
10954             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
10955         } else {
10956             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
10957         }
10958         return;
10959     case 0x10: /* ADD, SUB */
10960         if (u) {
10961             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
10962         } else {
10963             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
10964         }
10965         return;
10966     case 0x13: /* MUL, PMUL */
10967         if (!u) { /* MUL */
10968             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
10969         } else {  /* PMUL */
10970             gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
10971         }
10972         return;
10973     case 0x12: /* MLA, MLS */
10974         if (u) {
10975             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
10976         } else {
10977             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
10978         }
10979         return;
10980     case 0x16: /* SQDMULH, SQRDMULH */
10981         {
10982             static gen_helper_gvec_3_ptr * const fns[2][2] = {
10983                 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
10984                 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
10985             };
10986             gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
10987         }
10988         return;
10989     case 0x11:
10990         if (!u) { /* CMTST */
10991             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
10992             return;
10993         }
10994         /* else CMEQ */
10995         cond = TCG_COND_EQ;
10996         goto do_gvec_cmp;
10997     case 0x06: /* CMGT, CMHI */
10998         cond = u ? TCG_COND_GTU : TCG_COND_GT;
10999         goto do_gvec_cmp;
11000     case 0x07: /* CMGE, CMHS */
11001         cond = u ? TCG_COND_GEU : TCG_COND_GE;
11002     do_gvec_cmp:
11003         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11004                          vec_full_reg_offset(s, rn),
11005                          vec_full_reg_offset(s, rm),
11006                          is_q ? 16 : 8, vec_full_reg_size(s));
11007         return;
11008     }
11009 
11010     if (size == 3) {
11011         assert(is_q);
11012         for (pass = 0; pass < 2; pass++) {
11013             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11014             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11015             TCGv_i64 tcg_res = tcg_temp_new_i64();
11016 
11017             read_vec_element(s, tcg_op1, rn, pass, MO_64);
11018             read_vec_element(s, tcg_op2, rm, pass, MO_64);
11019 
11020             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11021 
11022             write_vec_element(s, tcg_res, rd, pass, MO_64);
11023         }
11024     } else {
11025         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11026             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11027             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11028             TCGv_i32 tcg_res = tcg_temp_new_i32();
11029             NeonGenTwoOpFn *genfn = NULL;
11030             NeonGenTwoOpEnvFn *genenvfn = NULL;
11031 
11032             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11033             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11034 
11035             switch (opcode) {
11036             case 0x0: /* SHADD, UHADD */
11037             {
11038                 static NeonGenTwoOpFn * const fns[3][2] = {
11039                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11040                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11041                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11042                 };
11043                 genfn = fns[size][u];
11044                 break;
11045             }
11046             case 0x2: /* SRHADD, URHADD */
11047             {
11048                 static NeonGenTwoOpFn * const fns[3][2] = {
11049                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11050                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11051                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11052                 };
11053                 genfn = fns[size][u];
11054                 break;
11055             }
11056             case 0x4: /* SHSUB, UHSUB */
11057             {
11058                 static NeonGenTwoOpFn * const fns[3][2] = {
11059                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11060                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11061                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11062                 };
11063                 genfn = fns[size][u];
11064                 break;
11065             }
11066             case 0x9: /* SQSHL, UQSHL */
11067             {
11068                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11069                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11070                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11071                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11072                 };
11073                 genenvfn = fns[size][u];
11074                 break;
11075             }
11076             case 0xa: /* SRSHL, URSHL */
11077             {
11078                 static NeonGenTwoOpFn * const fns[3][2] = {
11079                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11080                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11081                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11082                 };
11083                 genfn = fns[size][u];
11084                 break;
11085             }
11086             case 0xb: /* SQRSHL, UQRSHL */
11087             {
11088                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11089                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11090                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11091                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11092                 };
11093                 genenvfn = fns[size][u];
11094                 break;
11095             }
11096             default:
11097                 g_assert_not_reached();
11098             }
11099 
11100             if (genenvfn) {
11101                 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11102             } else {
11103                 genfn(tcg_res, tcg_op1, tcg_op2);
11104             }
11105 
11106             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11107         }
11108     }
11109     clear_vec_high(s, is_q, rd);
11110 }
11111 
11112 /* AdvSIMD three same
11113  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
11114  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11115  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
11116  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11117  */
11118 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11119 {
11120     int opcode = extract32(insn, 11, 5);
11121 
11122     switch (opcode) {
11123     case 0x3: /* logic ops */
11124         disas_simd_3same_logic(s, insn);
11125         break;
11126     case 0x17: /* ADDP */
11127     case 0x14: /* SMAXP, UMAXP */
11128     case 0x15: /* SMINP, UMINP */
11129     {
11130         /* Pairwise operations */
11131         int is_q = extract32(insn, 30, 1);
11132         int u = extract32(insn, 29, 1);
11133         int size = extract32(insn, 22, 2);
11134         int rm = extract32(insn, 16, 5);
11135         int rn = extract32(insn, 5, 5);
11136         int rd = extract32(insn, 0, 5);
11137         if (opcode == 0x17) {
11138             if (u || (size == 3 && !is_q)) {
11139                 unallocated_encoding(s);
11140                 return;
11141             }
11142         } else {
11143             if (size == 3) {
11144                 unallocated_encoding(s);
11145                 return;
11146             }
11147         }
11148         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11149         break;
11150     }
11151     case 0x18 ... 0x31:
11152         /* floating point ops, sz[1] and U are part of opcode */
11153         disas_simd_3same_float(s, insn);
11154         break;
11155     default:
11156         disas_simd_3same_int(s, insn);
11157         break;
11158     }
11159 }
11160 
11161 /*
11162  * Advanced SIMD three same (ARMv8.2 FP16 variants)
11163  *
11164  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
11165  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11166  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
11167  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11168  *
11169  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11170  * (register), FACGE, FABD, FCMGT (register) and FACGT.
11171  *
11172  */
11173 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11174 {
11175     int opcode = extract32(insn, 11, 3);
11176     int u = extract32(insn, 29, 1);
11177     int a = extract32(insn, 23, 1);
11178     int is_q = extract32(insn, 30, 1);
11179     int rm = extract32(insn, 16, 5);
11180     int rn = extract32(insn, 5, 5);
11181     int rd = extract32(insn, 0, 5);
11182     /*
11183      * For these floating point ops, the U, a and opcode bits
11184      * together indicate the operation.
11185      */
11186     int fpopcode = opcode | (a << 3) | (u << 4);
11187     int datasize = is_q ? 128 : 64;
11188     int elements = datasize / 16;
11189     bool pairwise;
11190     TCGv_ptr fpst;
11191     int pass;
11192 
11193     switch (fpopcode) {
11194     case 0x0: /* FMAXNM */
11195     case 0x1: /* FMLA */
11196     case 0x2: /* FADD */
11197     case 0x3: /* FMULX */
11198     case 0x4: /* FCMEQ */
11199     case 0x6: /* FMAX */
11200     case 0x7: /* FRECPS */
11201     case 0x8: /* FMINNM */
11202     case 0x9: /* FMLS */
11203     case 0xa: /* FSUB */
11204     case 0xe: /* FMIN */
11205     case 0xf: /* FRSQRTS */
11206     case 0x13: /* FMUL */
11207     case 0x14: /* FCMGE */
11208     case 0x15: /* FACGE */
11209     case 0x17: /* FDIV */
11210     case 0x1a: /* FABD */
11211     case 0x1c: /* FCMGT */
11212     case 0x1d: /* FACGT */
11213         pairwise = false;
11214         break;
11215     case 0x10: /* FMAXNMP */
11216     case 0x12: /* FADDP */
11217     case 0x16: /* FMAXP */
11218     case 0x18: /* FMINNMP */
11219     case 0x1e: /* FMINP */
11220         pairwise = true;
11221         break;
11222     default:
11223         unallocated_encoding(s);
11224         return;
11225     }
11226 
11227     if (!dc_isar_feature(aa64_fp16, s)) {
11228         unallocated_encoding(s);
11229         return;
11230     }
11231 
11232     if (!fp_access_check(s)) {
11233         return;
11234     }
11235 
11236     fpst = fpstatus_ptr(FPST_FPCR_F16);
11237 
11238     if (pairwise) {
11239         int maxpass = is_q ? 8 : 4;
11240         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11241         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11242         TCGv_i32 tcg_res[8];
11243 
11244         for (pass = 0; pass < maxpass; pass++) {
11245             int passreg = pass < (maxpass / 2) ? rn : rm;
11246             int passelt = (pass << 1) & (maxpass - 1);
11247 
11248             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11249             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11250             tcg_res[pass] = tcg_temp_new_i32();
11251 
11252             switch (fpopcode) {
11253             case 0x10: /* FMAXNMP */
11254                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11255                                            fpst);
11256                 break;
11257             case 0x12: /* FADDP */
11258                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11259                 break;
11260             case 0x16: /* FMAXP */
11261                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11262                 break;
11263             case 0x18: /* FMINNMP */
11264                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11265                                            fpst);
11266                 break;
11267             case 0x1e: /* FMINP */
11268                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11269                 break;
11270             default:
11271                 g_assert_not_reached();
11272             }
11273         }
11274 
11275         for (pass = 0; pass < maxpass; pass++) {
11276             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11277         }
11278     } else {
11279         for (pass = 0; pass < elements; pass++) {
11280             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11281             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11282             TCGv_i32 tcg_res = tcg_temp_new_i32();
11283 
11284             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11285             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11286 
11287             switch (fpopcode) {
11288             case 0x0: /* FMAXNM */
11289                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11290                 break;
11291             case 0x1: /* FMLA */
11292                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11293                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11294                                            fpst);
11295                 break;
11296             case 0x2: /* FADD */
11297                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11298                 break;
11299             case 0x3: /* FMULX */
11300                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11301                 break;
11302             case 0x4: /* FCMEQ */
11303                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11304                 break;
11305             case 0x6: /* FMAX */
11306                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11307                 break;
11308             case 0x7: /* FRECPS */
11309                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11310                 break;
11311             case 0x8: /* FMINNM */
11312                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11313                 break;
11314             case 0x9: /* FMLS */
11315                 /* As usual for ARM, separate negation for fused multiply-add */
11316                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11317                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11318                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11319                                            fpst);
11320                 break;
11321             case 0xa: /* FSUB */
11322                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11323                 break;
11324             case 0xe: /* FMIN */
11325                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11326                 break;
11327             case 0xf: /* FRSQRTS */
11328                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11329                 break;
11330             case 0x13: /* FMUL */
11331                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11332                 break;
11333             case 0x14: /* FCMGE */
11334                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11335                 break;
11336             case 0x15: /* FACGE */
11337                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11338                 break;
11339             case 0x17: /* FDIV */
11340                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11341                 break;
11342             case 0x1a: /* FABD */
11343                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11344                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11345                 break;
11346             case 0x1c: /* FCMGT */
11347                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11348                 break;
11349             case 0x1d: /* FACGT */
11350                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11351                 break;
11352             default:
11353                 g_assert_not_reached();
11354             }
11355 
11356             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11357         }
11358     }
11359 
11360     clear_vec_high(s, is_q, rd);
11361 }
11362 
11363 /* AdvSIMD three same extra
11364  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
11365  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11366  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
11367  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11368  */
11369 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11370 {
11371     int rd = extract32(insn, 0, 5);
11372     int rn = extract32(insn, 5, 5);
11373     int opcode = extract32(insn, 11, 4);
11374     int rm = extract32(insn, 16, 5);
11375     int size = extract32(insn, 22, 2);
11376     bool u = extract32(insn, 29, 1);
11377     bool is_q = extract32(insn, 30, 1);
11378     bool feature;
11379     int rot;
11380 
11381     switch (u * 16 + opcode) {
11382     case 0x10: /* SQRDMLAH (vector) */
11383     case 0x11: /* SQRDMLSH (vector) */
11384         if (size != 1 && size != 2) {
11385             unallocated_encoding(s);
11386             return;
11387         }
11388         feature = dc_isar_feature(aa64_rdm, s);
11389         break;
11390     case 0x02: /* SDOT (vector) */
11391     case 0x12: /* UDOT (vector) */
11392         if (size != MO_32) {
11393             unallocated_encoding(s);
11394             return;
11395         }
11396         feature = dc_isar_feature(aa64_dp, s);
11397         break;
11398     case 0x03: /* USDOT */
11399         if (size != MO_32) {
11400             unallocated_encoding(s);
11401             return;
11402         }
11403         feature = dc_isar_feature(aa64_i8mm, s);
11404         break;
11405     case 0x04: /* SMMLA */
11406     case 0x14: /* UMMLA */
11407     case 0x05: /* USMMLA */
11408         if (!is_q || size != MO_32) {
11409             unallocated_encoding(s);
11410             return;
11411         }
11412         feature = dc_isar_feature(aa64_i8mm, s);
11413         break;
11414     case 0x18: /* FCMLA, #0 */
11415     case 0x19: /* FCMLA, #90 */
11416     case 0x1a: /* FCMLA, #180 */
11417     case 0x1b: /* FCMLA, #270 */
11418     case 0x1c: /* FCADD, #90 */
11419     case 0x1e: /* FCADD, #270 */
11420         if (size == 0
11421             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11422             || (size == 3 && !is_q)) {
11423             unallocated_encoding(s);
11424             return;
11425         }
11426         feature = dc_isar_feature(aa64_fcma, s);
11427         break;
11428     case 0x1d: /* BFMMLA */
11429         if (size != MO_16 || !is_q) {
11430             unallocated_encoding(s);
11431             return;
11432         }
11433         feature = dc_isar_feature(aa64_bf16, s);
11434         break;
11435     case 0x1f:
11436         switch (size) {
11437         case 1: /* BFDOT */
11438         case 3: /* BFMLAL{B,T} */
11439             feature = dc_isar_feature(aa64_bf16, s);
11440             break;
11441         default:
11442             unallocated_encoding(s);
11443             return;
11444         }
11445         break;
11446     default:
11447         unallocated_encoding(s);
11448         return;
11449     }
11450     if (!feature) {
11451         unallocated_encoding(s);
11452         return;
11453     }
11454     if (!fp_access_check(s)) {
11455         return;
11456     }
11457 
11458     switch (opcode) {
11459     case 0x0: /* SQRDMLAH (vector) */
11460         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11461         return;
11462 
11463     case 0x1: /* SQRDMLSH (vector) */
11464         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11465         return;
11466 
11467     case 0x2: /* SDOT / UDOT */
11468         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11469                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11470         return;
11471 
11472     case 0x3: /* USDOT */
11473         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11474         return;
11475 
11476     case 0x04: /* SMMLA, UMMLA */
11477         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11478                          u ? gen_helper_gvec_ummla_b
11479                          : gen_helper_gvec_smmla_b);
11480         return;
11481     case 0x05: /* USMMLA */
11482         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11483         return;
11484 
11485     case 0x8: /* FCMLA, #0 */
11486     case 0x9: /* FCMLA, #90 */
11487     case 0xa: /* FCMLA, #180 */
11488     case 0xb: /* FCMLA, #270 */
11489         rot = extract32(opcode, 0, 2);
11490         switch (size) {
11491         case 1:
11492             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11493                               gen_helper_gvec_fcmlah);
11494             break;
11495         case 2:
11496             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11497                               gen_helper_gvec_fcmlas);
11498             break;
11499         case 3:
11500             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11501                               gen_helper_gvec_fcmlad);
11502             break;
11503         default:
11504             g_assert_not_reached();
11505         }
11506         return;
11507 
11508     case 0xc: /* FCADD, #90 */
11509     case 0xe: /* FCADD, #270 */
11510         rot = extract32(opcode, 1, 1);
11511         switch (size) {
11512         case 1:
11513             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11514                               gen_helper_gvec_fcaddh);
11515             break;
11516         case 2:
11517             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11518                               gen_helper_gvec_fcadds);
11519             break;
11520         case 3:
11521             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11522                               gen_helper_gvec_fcaddd);
11523             break;
11524         default:
11525             g_assert_not_reached();
11526         }
11527         return;
11528 
11529     case 0xd: /* BFMMLA */
11530         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11531         return;
11532     case 0xf:
11533         switch (size) {
11534         case 1: /* BFDOT */
11535             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11536             break;
11537         case 3: /* BFMLAL{B,T} */
11538             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11539                               gen_helper_gvec_bfmlal);
11540             break;
11541         default:
11542             g_assert_not_reached();
11543         }
11544         return;
11545 
11546     default:
11547         g_assert_not_reached();
11548     }
11549 }
11550 
11551 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11552                                   int size, int rn, int rd)
11553 {
11554     /* Handle 2-reg-misc ops which are widening (so each size element
11555      * in the source becomes a 2*size element in the destination.
11556      * The only instruction like this is FCVTL.
11557      */
11558     int pass;
11559 
11560     if (size == 3) {
11561         /* 32 -> 64 bit fp conversion */
11562         TCGv_i64 tcg_res[2];
11563         int srcelt = is_q ? 2 : 0;
11564 
11565         for (pass = 0; pass < 2; pass++) {
11566             TCGv_i32 tcg_op = tcg_temp_new_i32();
11567             tcg_res[pass] = tcg_temp_new_i64();
11568 
11569             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11570             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
11571         }
11572         for (pass = 0; pass < 2; pass++) {
11573             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11574         }
11575     } else {
11576         /* 16 -> 32 bit fp conversion */
11577         int srcelt = is_q ? 4 : 0;
11578         TCGv_i32 tcg_res[4];
11579         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11580         TCGv_i32 ahp = get_ahp_flag();
11581 
11582         for (pass = 0; pass < 4; pass++) {
11583             tcg_res[pass] = tcg_temp_new_i32();
11584 
11585             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11586             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11587                                            fpst, ahp);
11588         }
11589         for (pass = 0; pass < 4; pass++) {
11590             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11591         }
11592     }
11593 }
11594 
11595 static void handle_rev(DisasContext *s, int opcode, bool u,
11596                        bool is_q, int size, int rn, int rd)
11597 {
11598     int op = (opcode << 1) | u;
11599     int opsz = op + size;
11600     int grp_size = 3 - opsz;
11601     int dsize = is_q ? 128 : 64;
11602     int i;
11603 
11604     if (opsz >= 3) {
11605         unallocated_encoding(s);
11606         return;
11607     }
11608 
11609     if (!fp_access_check(s)) {
11610         return;
11611     }
11612 
11613     if (size == 0) {
11614         /* Special case bytes, use bswap op on each group of elements */
11615         int groups = dsize / (8 << grp_size);
11616 
11617         for (i = 0; i < groups; i++) {
11618             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11619 
11620             read_vec_element(s, tcg_tmp, rn, i, grp_size);
11621             switch (grp_size) {
11622             case MO_16:
11623                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11624                 break;
11625             case MO_32:
11626                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11627                 break;
11628             case MO_64:
11629                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11630                 break;
11631             default:
11632                 g_assert_not_reached();
11633             }
11634             write_vec_element(s, tcg_tmp, rd, i, grp_size);
11635         }
11636         clear_vec_high(s, is_q, rd);
11637     } else {
11638         int revmask = (1 << grp_size) - 1;
11639         int esize = 8 << size;
11640         int elements = dsize / esize;
11641         TCGv_i64 tcg_rn = tcg_temp_new_i64();
11642         TCGv_i64 tcg_rd[2];
11643 
11644         for (i = 0; i < 2; i++) {
11645             tcg_rd[i] = tcg_temp_new_i64();
11646             tcg_gen_movi_i64(tcg_rd[i], 0);
11647         }
11648 
11649         for (i = 0; i < elements; i++) {
11650             int e_rev = (i & 0xf) ^ revmask;
11651             int w = (e_rev * esize) / 64;
11652             int o = (e_rev * esize) % 64;
11653 
11654             read_vec_element(s, tcg_rn, rn, i, size);
11655             tcg_gen_deposit_i64(tcg_rd[w], tcg_rd[w], tcg_rn, o, esize);
11656         }
11657 
11658         for (i = 0; i < 2; i++) {
11659             write_vec_element(s, tcg_rd[i], rd, i, MO_64);
11660         }
11661         clear_vec_high(s, true, rd);
11662     }
11663 }
11664 
11665 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
11666                                   bool is_q, int size, int rn, int rd)
11667 {
11668     /* Implement the pairwise operations from 2-misc:
11669      * SADDLP, UADDLP, SADALP, UADALP.
11670      * These all add pairs of elements in the input to produce a
11671      * double-width result element in the output (possibly accumulating).
11672      */
11673     bool accum = (opcode == 0x6);
11674     int maxpass = is_q ? 2 : 1;
11675     int pass;
11676     TCGv_i64 tcg_res[2];
11677 
11678     if (size == 2) {
11679         /* 32 + 32 -> 64 op */
11680         MemOp memop = size + (u ? 0 : MO_SIGN);
11681 
11682         for (pass = 0; pass < maxpass; pass++) {
11683             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11684             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11685 
11686             tcg_res[pass] = tcg_temp_new_i64();
11687 
11688             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
11689             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
11690             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11691             if (accum) {
11692                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
11693                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
11694             }
11695         }
11696     } else {
11697         for (pass = 0; pass < maxpass; pass++) {
11698             TCGv_i64 tcg_op = tcg_temp_new_i64();
11699             NeonGenOne64OpFn *genfn;
11700             static NeonGenOne64OpFn * const fns[2][2] = {
11701                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
11702                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
11703             };
11704 
11705             genfn = fns[size][u];
11706 
11707             tcg_res[pass] = tcg_temp_new_i64();
11708 
11709             read_vec_element(s, tcg_op, rn, pass, MO_64);
11710             genfn(tcg_res[pass], tcg_op);
11711 
11712             if (accum) {
11713                 read_vec_element(s, tcg_op, rd, pass, MO_64);
11714                 if (size == 0) {
11715                     gen_helper_neon_addl_u16(tcg_res[pass],
11716                                              tcg_res[pass], tcg_op);
11717                 } else {
11718                     gen_helper_neon_addl_u32(tcg_res[pass],
11719                                              tcg_res[pass], tcg_op);
11720                 }
11721             }
11722         }
11723     }
11724     if (!is_q) {
11725         tcg_res[1] = tcg_constant_i64(0);
11726     }
11727     for (pass = 0; pass < 2; pass++) {
11728         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11729     }
11730 }
11731 
11732 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
11733 {
11734     /* Implement SHLL and SHLL2 */
11735     int pass;
11736     int part = is_q ? 2 : 0;
11737     TCGv_i64 tcg_res[2];
11738 
11739     for (pass = 0; pass < 2; pass++) {
11740         static NeonGenWidenFn * const widenfns[3] = {
11741             gen_helper_neon_widen_u8,
11742             gen_helper_neon_widen_u16,
11743             tcg_gen_extu_i32_i64,
11744         };
11745         NeonGenWidenFn *widenfn = widenfns[size];
11746         TCGv_i32 tcg_op = tcg_temp_new_i32();
11747 
11748         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
11749         tcg_res[pass] = tcg_temp_new_i64();
11750         widenfn(tcg_res[pass], tcg_op);
11751         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
11752     }
11753 
11754     for (pass = 0; pass < 2; pass++) {
11755         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11756     }
11757 }
11758 
11759 /* AdvSIMD two reg misc
11760  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
11761  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11762  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
11763  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11764  */
11765 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
11766 {
11767     int size = extract32(insn, 22, 2);
11768     int opcode = extract32(insn, 12, 5);
11769     bool u = extract32(insn, 29, 1);
11770     bool is_q = extract32(insn, 30, 1);
11771     int rn = extract32(insn, 5, 5);
11772     int rd = extract32(insn, 0, 5);
11773     bool need_fpstatus = false;
11774     int rmode = -1;
11775     TCGv_i32 tcg_rmode;
11776     TCGv_ptr tcg_fpstatus;
11777 
11778     switch (opcode) {
11779     case 0x0: /* REV64, REV32 */
11780     case 0x1: /* REV16 */
11781         handle_rev(s, opcode, u, is_q, size, rn, rd);
11782         return;
11783     case 0x5: /* CNT, NOT, RBIT */
11784         if (u && size == 0) {
11785             /* NOT */
11786             break;
11787         } else if (u && size == 1) {
11788             /* RBIT */
11789             break;
11790         } else if (!u && size == 0) {
11791             /* CNT */
11792             break;
11793         }
11794         unallocated_encoding(s);
11795         return;
11796     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
11797     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
11798         if (size == 3) {
11799             unallocated_encoding(s);
11800             return;
11801         }
11802         if (!fp_access_check(s)) {
11803             return;
11804         }
11805 
11806         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
11807         return;
11808     case 0x4: /* CLS, CLZ */
11809         if (size == 3) {
11810             unallocated_encoding(s);
11811             return;
11812         }
11813         break;
11814     case 0x2: /* SADDLP, UADDLP */
11815     case 0x6: /* SADALP, UADALP */
11816         if (size == 3) {
11817             unallocated_encoding(s);
11818             return;
11819         }
11820         if (!fp_access_check(s)) {
11821             return;
11822         }
11823         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
11824         return;
11825     case 0x13: /* SHLL, SHLL2 */
11826         if (u == 0 || size == 3) {
11827             unallocated_encoding(s);
11828             return;
11829         }
11830         if (!fp_access_check(s)) {
11831             return;
11832         }
11833         handle_shll(s, is_q, size, rn, rd);
11834         return;
11835     case 0xa: /* CMLT */
11836         if (u == 1) {
11837             unallocated_encoding(s);
11838             return;
11839         }
11840         /* fall through */
11841     case 0x8: /* CMGT, CMGE */
11842     case 0x9: /* CMEQ, CMLE */
11843     case 0xb: /* ABS, NEG */
11844         if (size == 3 && !is_q) {
11845             unallocated_encoding(s);
11846             return;
11847         }
11848         break;
11849     case 0x3: /* SUQADD, USQADD */
11850         if (size == 3 && !is_q) {
11851             unallocated_encoding(s);
11852             return;
11853         }
11854         if (!fp_access_check(s)) {
11855             return;
11856         }
11857         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
11858         return;
11859     case 0x7: /* SQABS, SQNEG */
11860         if (size == 3 && !is_q) {
11861             unallocated_encoding(s);
11862             return;
11863         }
11864         break;
11865     case 0xc ... 0xf:
11866     case 0x16 ... 0x1f:
11867     {
11868         /* Floating point: U, size[1] and opcode indicate operation;
11869          * size[0] indicates single or double precision.
11870          */
11871         int is_double = extract32(size, 0, 1);
11872         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
11873         size = is_double ? 3 : 2;
11874         switch (opcode) {
11875         case 0x2f: /* FABS */
11876         case 0x6f: /* FNEG */
11877             if (size == 3 && !is_q) {
11878                 unallocated_encoding(s);
11879                 return;
11880             }
11881             break;
11882         case 0x1d: /* SCVTF */
11883         case 0x5d: /* UCVTF */
11884         {
11885             bool is_signed = (opcode == 0x1d) ? true : false;
11886             int elements = is_double ? 2 : is_q ? 4 : 2;
11887             if (is_double && !is_q) {
11888                 unallocated_encoding(s);
11889                 return;
11890             }
11891             if (!fp_access_check(s)) {
11892                 return;
11893             }
11894             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
11895             return;
11896         }
11897         case 0x2c: /* FCMGT (zero) */
11898         case 0x2d: /* FCMEQ (zero) */
11899         case 0x2e: /* FCMLT (zero) */
11900         case 0x6c: /* FCMGE (zero) */
11901         case 0x6d: /* FCMLE (zero) */
11902             if (size == 3 && !is_q) {
11903                 unallocated_encoding(s);
11904                 return;
11905             }
11906             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
11907             return;
11908         case 0x7f: /* FSQRT */
11909             if (size == 3 && !is_q) {
11910                 unallocated_encoding(s);
11911                 return;
11912             }
11913             break;
11914         case 0x1a: /* FCVTNS */
11915         case 0x1b: /* FCVTMS */
11916         case 0x3a: /* FCVTPS */
11917         case 0x3b: /* FCVTZS */
11918         case 0x5a: /* FCVTNU */
11919         case 0x5b: /* FCVTMU */
11920         case 0x7a: /* FCVTPU */
11921         case 0x7b: /* FCVTZU */
11922             need_fpstatus = true;
11923             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
11924             if (size == 3 && !is_q) {
11925                 unallocated_encoding(s);
11926                 return;
11927             }
11928             break;
11929         case 0x5c: /* FCVTAU */
11930         case 0x1c: /* FCVTAS */
11931             need_fpstatus = true;
11932             rmode = FPROUNDING_TIEAWAY;
11933             if (size == 3 && !is_q) {
11934                 unallocated_encoding(s);
11935                 return;
11936             }
11937             break;
11938         case 0x3c: /* URECPE */
11939             if (size == 3) {
11940                 unallocated_encoding(s);
11941                 return;
11942             }
11943             /* fall through */
11944         case 0x3d: /* FRECPE */
11945         case 0x7d: /* FRSQRTE */
11946             if (size == 3 && !is_q) {
11947                 unallocated_encoding(s);
11948                 return;
11949             }
11950             if (!fp_access_check(s)) {
11951                 return;
11952             }
11953             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
11954             return;
11955         case 0x56: /* FCVTXN, FCVTXN2 */
11956             if (size == 2) {
11957                 unallocated_encoding(s);
11958                 return;
11959             }
11960             /* fall through */
11961         case 0x16: /* FCVTN, FCVTN2 */
11962             /* handle_2misc_narrow does a 2*size -> size operation, but these
11963              * instructions encode the source size rather than dest size.
11964              */
11965             if (!fp_access_check(s)) {
11966                 return;
11967             }
11968             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
11969             return;
11970         case 0x36: /* BFCVTN, BFCVTN2 */
11971             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
11972                 unallocated_encoding(s);
11973                 return;
11974             }
11975             if (!fp_access_check(s)) {
11976                 return;
11977             }
11978             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
11979             return;
11980         case 0x17: /* FCVTL, FCVTL2 */
11981             if (!fp_access_check(s)) {
11982                 return;
11983             }
11984             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
11985             return;
11986         case 0x18: /* FRINTN */
11987         case 0x19: /* FRINTM */
11988         case 0x38: /* FRINTP */
11989         case 0x39: /* FRINTZ */
11990             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
11991             /* fall through */
11992         case 0x59: /* FRINTX */
11993         case 0x79: /* FRINTI */
11994             need_fpstatus = true;
11995             if (size == 3 && !is_q) {
11996                 unallocated_encoding(s);
11997                 return;
11998             }
11999             break;
12000         case 0x58: /* FRINTA */
12001             rmode = FPROUNDING_TIEAWAY;
12002             need_fpstatus = true;
12003             if (size == 3 && !is_q) {
12004                 unallocated_encoding(s);
12005                 return;
12006             }
12007             break;
12008         case 0x7c: /* URSQRTE */
12009             if (size == 3) {
12010                 unallocated_encoding(s);
12011                 return;
12012             }
12013             break;
12014         case 0x1e: /* FRINT32Z */
12015         case 0x1f: /* FRINT64Z */
12016             rmode = FPROUNDING_ZERO;
12017             /* fall through */
12018         case 0x5e: /* FRINT32X */
12019         case 0x5f: /* FRINT64X */
12020             need_fpstatus = true;
12021             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12022                 unallocated_encoding(s);
12023                 return;
12024             }
12025             break;
12026         default:
12027             unallocated_encoding(s);
12028             return;
12029         }
12030         break;
12031     }
12032     default:
12033         unallocated_encoding(s);
12034         return;
12035     }
12036 
12037     if (!fp_access_check(s)) {
12038         return;
12039     }
12040 
12041     if (need_fpstatus || rmode >= 0) {
12042         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12043     } else {
12044         tcg_fpstatus = NULL;
12045     }
12046     if (rmode >= 0) {
12047         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12048     } else {
12049         tcg_rmode = NULL;
12050     }
12051 
12052     switch (opcode) {
12053     case 0x5:
12054         if (u && size == 0) { /* NOT */
12055             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12056             return;
12057         }
12058         break;
12059     case 0x8: /* CMGT, CMGE */
12060         if (u) {
12061             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12062         } else {
12063             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12064         }
12065         return;
12066     case 0x9: /* CMEQ, CMLE */
12067         if (u) {
12068             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12069         } else {
12070             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12071         }
12072         return;
12073     case 0xa: /* CMLT */
12074         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12075         return;
12076     case 0xb:
12077         if (u) { /* ABS, NEG */
12078             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12079         } else {
12080             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12081         }
12082         return;
12083     }
12084 
12085     if (size == 3) {
12086         /* All 64-bit element operations can be shared with scalar 2misc */
12087         int pass;
12088 
12089         /* Coverity claims (size == 3 && !is_q) has been eliminated
12090          * from all paths leading to here.
12091          */
12092         tcg_debug_assert(is_q);
12093         for (pass = 0; pass < 2; pass++) {
12094             TCGv_i64 tcg_op = tcg_temp_new_i64();
12095             TCGv_i64 tcg_res = tcg_temp_new_i64();
12096 
12097             read_vec_element(s, tcg_op, rn, pass, MO_64);
12098 
12099             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12100                             tcg_rmode, tcg_fpstatus);
12101 
12102             write_vec_element(s, tcg_res, rd, pass, MO_64);
12103         }
12104     } else {
12105         int pass;
12106 
12107         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12108             TCGv_i32 tcg_op = tcg_temp_new_i32();
12109             TCGv_i32 tcg_res = tcg_temp_new_i32();
12110 
12111             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12112 
12113             if (size == 2) {
12114                 /* Special cases for 32 bit elements */
12115                 switch (opcode) {
12116                 case 0x4: /* CLS */
12117                     if (u) {
12118                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12119                     } else {
12120                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
12121                     }
12122                     break;
12123                 case 0x7: /* SQABS, SQNEG */
12124                     if (u) {
12125                         gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12126                     } else {
12127                         gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12128                     }
12129                     break;
12130                 case 0x2f: /* FABS */
12131                     gen_helper_vfp_abss(tcg_res, tcg_op);
12132                     break;
12133                 case 0x6f: /* FNEG */
12134                     gen_helper_vfp_negs(tcg_res, tcg_op);
12135                     break;
12136                 case 0x7f: /* FSQRT */
12137                     gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12138                     break;
12139                 case 0x1a: /* FCVTNS */
12140                 case 0x1b: /* FCVTMS */
12141                 case 0x1c: /* FCVTAS */
12142                 case 0x3a: /* FCVTPS */
12143                 case 0x3b: /* FCVTZS */
12144                     gen_helper_vfp_tosls(tcg_res, tcg_op,
12145                                          tcg_constant_i32(0), tcg_fpstatus);
12146                     break;
12147                 case 0x5a: /* FCVTNU */
12148                 case 0x5b: /* FCVTMU */
12149                 case 0x5c: /* FCVTAU */
12150                 case 0x7a: /* FCVTPU */
12151                 case 0x7b: /* FCVTZU */
12152                     gen_helper_vfp_touls(tcg_res, tcg_op,
12153                                          tcg_constant_i32(0), tcg_fpstatus);
12154                     break;
12155                 case 0x18: /* FRINTN */
12156                 case 0x19: /* FRINTM */
12157                 case 0x38: /* FRINTP */
12158                 case 0x39: /* FRINTZ */
12159                 case 0x58: /* FRINTA */
12160                 case 0x79: /* FRINTI */
12161                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12162                     break;
12163                 case 0x59: /* FRINTX */
12164                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12165                     break;
12166                 case 0x7c: /* URSQRTE */
12167                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
12168                     break;
12169                 case 0x1e: /* FRINT32Z */
12170                 case 0x5e: /* FRINT32X */
12171                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12172                     break;
12173                 case 0x1f: /* FRINT64Z */
12174                 case 0x5f: /* FRINT64X */
12175                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12176                     break;
12177                 default:
12178                     g_assert_not_reached();
12179                 }
12180             } else {
12181                 /* Use helpers for 8 and 16 bit elements */
12182                 switch (opcode) {
12183                 case 0x5: /* CNT, RBIT */
12184                     /* For these two insns size is part of the opcode specifier
12185                      * (handled earlier); they always operate on byte elements.
12186                      */
12187                     if (u) {
12188                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12189                     } else {
12190                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12191                     }
12192                     break;
12193                 case 0x7: /* SQABS, SQNEG */
12194                 {
12195                     NeonGenOneOpEnvFn *genfn;
12196                     static NeonGenOneOpEnvFn * const fns[2][2] = {
12197                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12198                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12199                     };
12200                     genfn = fns[size][u];
12201                     genfn(tcg_res, cpu_env, tcg_op);
12202                     break;
12203                 }
12204                 case 0x4: /* CLS, CLZ */
12205                     if (u) {
12206                         if (size == 0) {
12207                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
12208                         } else {
12209                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
12210                         }
12211                     } else {
12212                         if (size == 0) {
12213                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
12214                         } else {
12215                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
12216                         }
12217                     }
12218                     break;
12219                 default:
12220                     g_assert_not_reached();
12221                 }
12222             }
12223 
12224             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12225         }
12226     }
12227     clear_vec_high(s, is_q, rd);
12228 
12229     if (tcg_rmode) {
12230         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12231     }
12232 }
12233 
12234 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12235  *
12236  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
12237  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12238  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12239  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12240  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12241  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12242  *
12243  * This actually covers two groups where scalar access is governed by
12244  * bit 28. A bunch of the instructions (float to integral) only exist
12245  * in the vector form and are un-allocated for the scalar decode. Also
12246  * in the scalar decode Q is always 1.
12247  */
12248 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12249 {
12250     int fpop, opcode, a, u;
12251     int rn, rd;
12252     bool is_q;
12253     bool is_scalar;
12254     bool only_in_vector = false;
12255 
12256     int pass;
12257     TCGv_i32 tcg_rmode = NULL;
12258     TCGv_ptr tcg_fpstatus = NULL;
12259     bool need_fpst = true;
12260     int rmode = -1;
12261 
12262     if (!dc_isar_feature(aa64_fp16, s)) {
12263         unallocated_encoding(s);
12264         return;
12265     }
12266 
12267     rd = extract32(insn, 0, 5);
12268     rn = extract32(insn, 5, 5);
12269 
12270     a = extract32(insn, 23, 1);
12271     u = extract32(insn, 29, 1);
12272     is_scalar = extract32(insn, 28, 1);
12273     is_q = extract32(insn, 30, 1);
12274 
12275     opcode = extract32(insn, 12, 5);
12276     fpop = deposit32(opcode, 5, 1, a);
12277     fpop = deposit32(fpop, 6, 1, u);
12278 
12279     switch (fpop) {
12280     case 0x1d: /* SCVTF */
12281     case 0x5d: /* UCVTF */
12282     {
12283         int elements;
12284 
12285         if (is_scalar) {
12286             elements = 1;
12287         } else {
12288             elements = (is_q ? 8 : 4);
12289         }
12290 
12291         if (!fp_access_check(s)) {
12292             return;
12293         }
12294         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12295         return;
12296     }
12297     break;
12298     case 0x2c: /* FCMGT (zero) */
12299     case 0x2d: /* FCMEQ (zero) */
12300     case 0x2e: /* FCMLT (zero) */
12301     case 0x6c: /* FCMGE (zero) */
12302     case 0x6d: /* FCMLE (zero) */
12303         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12304         return;
12305     case 0x3d: /* FRECPE */
12306     case 0x3f: /* FRECPX */
12307         break;
12308     case 0x18: /* FRINTN */
12309         only_in_vector = true;
12310         rmode = FPROUNDING_TIEEVEN;
12311         break;
12312     case 0x19: /* FRINTM */
12313         only_in_vector = true;
12314         rmode = FPROUNDING_NEGINF;
12315         break;
12316     case 0x38: /* FRINTP */
12317         only_in_vector = true;
12318         rmode = FPROUNDING_POSINF;
12319         break;
12320     case 0x39: /* FRINTZ */
12321         only_in_vector = true;
12322         rmode = FPROUNDING_ZERO;
12323         break;
12324     case 0x58: /* FRINTA */
12325         only_in_vector = true;
12326         rmode = FPROUNDING_TIEAWAY;
12327         break;
12328     case 0x59: /* FRINTX */
12329     case 0x79: /* FRINTI */
12330         only_in_vector = true;
12331         /* current rounding mode */
12332         break;
12333     case 0x1a: /* FCVTNS */
12334         rmode = FPROUNDING_TIEEVEN;
12335         break;
12336     case 0x1b: /* FCVTMS */
12337         rmode = FPROUNDING_NEGINF;
12338         break;
12339     case 0x1c: /* FCVTAS */
12340         rmode = FPROUNDING_TIEAWAY;
12341         break;
12342     case 0x3a: /* FCVTPS */
12343         rmode = FPROUNDING_POSINF;
12344         break;
12345     case 0x3b: /* FCVTZS */
12346         rmode = FPROUNDING_ZERO;
12347         break;
12348     case 0x5a: /* FCVTNU */
12349         rmode = FPROUNDING_TIEEVEN;
12350         break;
12351     case 0x5b: /* FCVTMU */
12352         rmode = FPROUNDING_NEGINF;
12353         break;
12354     case 0x5c: /* FCVTAU */
12355         rmode = FPROUNDING_TIEAWAY;
12356         break;
12357     case 0x7a: /* FCVTPU */
12358         rmode = FPROUNDING_POSINF;
12359         break;
12360     case 0x7b: /* FCVTZU */
12361         rmode = FPROUNDING_ZERO;
12362         break;
12363     case 0x2f: /* FABS */
12364     case 0x6f: /* FNEG */
12365         need_fpst = false;
12366         break;
12367     case 0x7d: /* FRSQRTE */
12368     case 0x7f: /* FSQRT (vector) */
12369         break;
12370     default:
12371         unallocated_encoding(s);
12372         return;
12373     }
12374 
12375 
12376     /* Check additional constraints for the scalar encoding */
12377     if (is_scalar) {
12378         if (!is_q) {
12379             unallocated_encoding(s);
12380             return;
12381         }
12382         /* FRINTxx is only in the vector form */
12383         if (only_in_vector) {
12384             unallocated_encoding(s);
12385             return;
12386         }
12387     }
12388 
12389     if (!fp_access_check(s)) {
12390         return;
12391     }
12392 
12393     if (rmode >= 0 || need_fpst) {
12394         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
12395     }
12396 
12397     if (rmode >= 0) {
12398         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12399     }
12400 
12401     if (is_scalar) {
12402         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12403         TCGv_i32 tcg_res = tcg_temp_new_i32();
12404 
12405         switch (fpop) {
12406         case 0x1a: /* FCVTNS */
12407         case 0x1b: /* FCVTMS */
12408         case 0x1c: /* FCVTAS */
12409         case 0x3a: /* FCVTPS */
12410         case 0x3b: /* FCVTZS */
12411             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12412             break;
12413         case 0x3d: /* FRECPE */
12414             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12415             break;
12416         case 0x3f: /* FRECPX */
12417             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12418             break;
12419         case 0x5a: /* FCVTNU */
12420         case 0x5b: /* FCVTMU */
12421         case 0x5c: /* FCVTAU */
12422         case 0x7a: /* FCVTPU */
12423         case 0x7b: /* FCVTZU */
12424             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12425             break;
12426         case 0x6f: /* FNEG */
12427             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12428             break;
12429         case 0x7d: /* FRSQRTE */
12430             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12431             break;
12432         default:
12433             g_assert_not_reached();
12434         }
12435 
12436         /* limit any sign extension going on */
12437         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12438         write_fp_sreg(s, rd, tcg_res);
12439     } else {
12440         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12441             TCGv_i32 tcg_op = tcg_temp_new_i32();
12442             TCGv_i32 tcg_res = tcg_temp_new_i32();
12443 
12444             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12445 
12446             switch (fpop) {
12447             case 0x1a: /* FCVTNS */
12448             case 0x1b: /* FCVTMS */
12449             case 0x1c: /* FCVTAS */
12450             case 0x3a: /* FCVTPS */
12451             case 0x3b: /* FCVTZS */
12452                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12453                 break;
12454             case 0x3d: /* FRECPE */
12455                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12456                 break;
12457             case 0x5a: /* FCVTNU */
12458             case 0x5b: /* FCVTMU */
12459             case 0x5c: /* FCVTAU */
12460             case 0x7a: /* FCVTPU */
12461             case 0x7b: /* FCVTZU */
12462                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12463                 break;
12464             case 0x18: /* FRINTN */
12465             case 0x19: /* FRINTM */
12466             case 0x38: /* FRINTP */
12467             case 0x39: /* FRINTZ */
12468             case 0x58: /* FRINTA */
12469             case 0x79: /* FRINTI */
12470                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12471                 break;
12472             case 0x59: /* FRINTX */
12473                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12474                 break;
12475             case 0x2f: /* FABS */
12476                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12477                 break;
12478             case 0x6f: /* FNEG */
12479                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12480                 break;
12481             case 0x7d: /* FRSQRTE */
12482                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12483                 break;
12484             case 0x7f: /* FSQRT */
12485                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12486                 break;
12487             default:
12488                 g_assert_not_reached();
12489             }
12490 
12491             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12492         }
12493 
12494         clear_vec_high(s, is_q, rd);
12495     }
12496 
12497     if (tcg_rmode) {
12498         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12499     }
12500 }
12501 
12502 /* AdvSIMD scalar x indexed element
12503  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12504  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12505  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12506  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12507  * AdvSIMD vector x indexed element
12508  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12509  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12510  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12511  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12512  */
12513 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12514 {
12515     /* This encoding has two kinds of instruction:
12516      *  normal, where we perform elt x idxelt => elt for each
12517      *     element in the vector
12518      *  long, where we perform elt x idxelt and generate a result of
12519      *     double the width of the input element
12520      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12521      */
12522     bool is_scalar = extract32(insn, 28, 1);
12523     bool is_q = extract32(insn, 30, 1);
12524     bool u = extract32(insn, 29, 1);
12525     int size = extract32(insn, 22, 2);
12526     int l = extract32(insn, 21, 1);
12527     int m = extract32(insn, 20, 1);
12528     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12529     int rm = extract32(insn, 16, 4);
12530     int opcode = extract32(insn, 12, 4);
12531     int h = extract32(insn, 11, 1);
12532     int rn = extract32(insn, 5, 5);
12533     int rd = extract32(insn, 0, 5);
12534     bool is_long = false;
12535     int is_fp = 0;
12536     bool is_fp16 = false;
12537     int index;
12538     TCGv_ptr fpst;
12539 
12540     switch (16 * u + opcode) {
12541     case 0x08: /* MUL */
12542     case 0x10: /* MLA */
12543     case 0x14: /* MLS */
12544         if (is_scalar) {
12545             unallocated_encoding(s);
12546             return;
12547         }
12548         break;
12549     case 0x02: /* SMLAL, SMLAL2 */
12550     case 0x12: /* UMLAL, UMLAL2 */
12551     case 0x06: /* SMLSL, SMLSL2 */
12552     case 0x16: /* UMLSL, UMLSL2 */
12553     case 0x0a: /* SMULL, SMULL2 */
12554     case 0x1a: /* UMULL, UMULL2 */
12555         if (is_scalar) {
12556             unallocated_encoding(s);
12557             return;
12558         }
12559         is_long = true;
12560         break;
12561     case 0x03: /* SQDMLAL, SQDMLAL2 */
12562     case 0x07: /* SQDMLSL, SQDMLSL2 */
12563     case 0x0b: /* SQDMULL, SQDMULL2 */
12564         is_long = true;
12565         break;
12566     case 0x0c: /* SQDMULH */
12567     case 0x0d: /* SQRDMULH */
12568         break;
12569     case 0x01: /* FMLA */
12570     case 0x05: /* FMLS */
12571     case 0x09: /* FMUL */
12572     case 0x19: /* FMULX */
12573         is_fp = 1;
12574         break;
12575     case 0x1d: /* SQRDMLAH */
12576     case 0x1f: /* SQRDMLSH */
12577         if (!dc_isar_feature(aa64_rdm, s)) {
12578             unallocated_encoding(s);
12579             return;
12580         }
12581         break;
12582     case 0x0e: /* SDOT */
12583     case 0x1e: /* UDOT */
12584         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12585             unallocated_encoding(s);
12586             return;
12587         }
12588         break;
12589     case 0x0f:
12590         switch (size) {
12591         case 0: /* SUDOT */
12592         case 2: /* USDOT */
12593             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12594                 unallocated_encoding(s);
12595                 return;
12596             }
12597             size = MO_32;
12598             break;
12599         case 1: /* BFDOT */
12600             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12601                 unallocated_encoding(s);
12602                 return;
12603             }
12604             size = MO_32;
12605             break;
12606         case 3: /* BFMLAL{B,T} */
12607             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12608                 unallocated_encoding(s);
12609                 return;
12610             }
12611             /* can't set is_fp without other incorrect size checks */
12612             size = MO_16;
12613             break;
12614         default:
12615             unallocated_encoding(s);
12616             return;
12617         }
12618         break;
12619     case 0x11: /* FCMLA #0 */
12620     case 0x13: /* FCMLA #90 */
12621     case 0x15: /* FCMLA #180 */
12622     case 0x17: /* FCMLA #270 */
12623         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
12624             unallocated_encoding(s);
12625             return;
12626         }
12627         is_fp = 2;
12628         break;
12629     case 0x00: /* FMLAL */
12630     case 0x04: /* FMLSL */
12631     case 0x18: /* FMLAL2 */
12632     case 0x1c: /* FMLSL2 */
12633         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
12634             unallocated_encoding(s);
12635             return;
12636         }
12637         size = MO_16;
12638         /* is_fp, but we pass cpu_env not fp_status.  */
12639         break;
12640     default:
12641         unallocated_encoding(s);
12642         return;
12643     }
12644 
12645     switch (is_fp) {
12646     case 1: /* normal fp */
12647         /* convert insn encoded size to MemOp size */
12648         switch (size) {
12649         case 0: /* half-precision */
12650             size = MO_16;
12651             is_fp16 = true;
12652             break;
12653         case MO_32: /* single precision */
12654         case MO_64: /* double precision */
12655             break;
12656         default:
12657             unallocated_encoding(s);
12658             return;
12659         }
12660         break;
12661 
12662     case 2: /* complex fp */
12663         /* Each indexable element is a complex pair.  */
12664         size += 1;
12665         switch (size) {
12666         case MO_32:
12667             if (h && !is_q) {
12668                 unallocated_encoding(s);
12669                 return;
12670             }
12671             is_fp16 = true;
12672             break;
12673         case MO_64:
12674             break;
12675         default:
12676             unallocated_encoding(s);
12677             return;
12678         }
12679         break;
12680 
12681     default: /* integer */
12682         switch (size) {
12683         case MO_8:
12684         case MO_64:
12685             unallocated_encoding(s);
12686             return;
12687         }
12688         break;
12689     }
12690     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
12691         unallocated_encoding(s);
12692         return;
12693     }
12694 
12695     /* Given MemOp size, adjust register and indexing.  */
12696     switch (size) {
12697     case MO_16:
12698         index = h << 2 | l << 1 | m;
12699         break;
12700     case MO_32:
12701         index = h << 1 | l;
12702         rm |= m << 4;
12703         break;
12704     case MO_64:
12705         if (l || !is_q) {
12706             unallocated_encoding(s);
12707             return;
12708         }
12709         index = h;
12710         rm |= m << 4;
12711         break;
12712     default:
12713         g_assert_not_reached();
12714     }
12715 
12716     if (!fp_access_check(s)) {
12717         return;
12718     }
12719 
12720     if (is_fp) {
12721         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
12722     } else {
12723         fpst = NULL;
12724     }
12725 
12726     switch (16 * u + opcode) {
12727     case 0x0e: /* SDOT */
12728     case 0x1e: /* UDOT */
12729         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12730                          u ? gen_helper_gvec_udot_idx_b
12731                          : gen_helper_gvec_sdot_idx_b);
12732         return;
12733     case 0x0f:
12734         switch (extract32(insn, 22, 2)) {
12735         case 0: /* SUDOT */
12736             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12737                              gen_helper_gvec_sudot_idx_b);
12738             return;
12739         case 1: /* BFDOT */
12740             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12741                              gen_helper_gvec_bfdot_idx);
12742             return;
12743         case 2: /* USDOT */
12744             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12745                              gen_helper_gvec_usdot_idx_b);
12746             return;
12747         case 3: /* BFMLAL{B,T} */
12748             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
12749                               gen_helper_gvec_bfmlal_idx);
12750             return;
12751         }
12752         g_assert_not_reached();
12753     case 0x11: /* FCMLA #0 */
12754     case 0x13: /* FCMLA #90 */
12755     case 0x15: /* FCMLA #180 */
12756     case 0x17: /* FCMLA #270 */
12757         {
12758             int rot = extract32(insn, 13, 2);
12759             int data = (index << 2) | rot;
12760             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
12761                                vec_full_reg_offset(s, rn),
12762                                vec_full_reg_offset(s, rm),
12763                                vec_full_reg_offset(s, rd), fpst,
12764                                is_q ? 16 : 8, vec_full_reg_size(s), data,
12765                                size == MO_64
12766                                ? gen_helper_gvec_fcmlas_idx
12767                                : gen_helper_gvec_fcmlah_idx);
12768         }
12769         return;
12770 
12771     case 0x00: /* FMLAL */
12772     case 0x04: /* FMLSL */
12773     case 0x18: /* FMLAL2 */
12774     case 0x1c: /* FMLSL2 */
12775         {
12776             int is_s = extract32(opcode, 2, 1);
12777             int is_2 = u;
12778             int data = (index << 2) | (is_2 << 1) | is_s;
12779             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
12780                                vec_full_reg_offset(s, rn),
12781                                vec_full_reg_offset(s, rm), cpu_env,
12782                                is_q ? 16 : 8, vec_full_reg_size(s),
12783                                data, gen_helper_gvec_fmlal_idx_a64);
12784         }
12785         return;
12786 
12787     case 0x08: /* MUL */
12788         if (!is_long && !is_scalar) {
12789             static gen_helper_gvec_3 * const fns[3] = {
12790                 gen_helper_gvec_mul_idx_h,
12791                 gen_helper_gvec_mul_idx_s,
12792                 gen_helper_gvec_mul_idx_d,
12793             };
12794             tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
12795                                vec_full_reg_offset(s, rn),
12796                                vec_full_reg_offset(s, rm),
12797                                is_q ? 16 : 8, vec_full_reg_size(s),
12798                                index, fns[size - 1]);
12799             return;
12800         }
12801         break;
12802 
12803     case 0x10: /* MLA */
12804         if (!is_long && !is_scalar) {
12805             static gen_helper_gvec_4 * const fns[3] = {
12806                 gen_helper_gvec_mla_idx_h,
12807                 gen_helper_gvec_mla_idx_s,
12808                 gen_helper_gvec_mla_idx_d,
12809             };
12810             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
12811                                vec_full_reg_offset(s, rn),
12812                                vec_full_reg_offset(s, rm),
12813                                vec_full_reg_offset(s, rd),
12814                                is_q ? 16 : 8, vec_full_reg_size(s),
12815                                index, fns[size - 1]);
12816             return;
12817         }
12818         break;
12819 
12820     case 0x14: /* MLS */
12821         if (!is_long && !is_scalar) {
12822             static gen_helper_gvec_4 * const fns[3] = {
12823                 gen_helper_gvec_mls_idx_h,
12824                 gen_helper_gvec_mls_idx_s,
12825                 gen_helper_gvec_mls_idx_d,
12826             };
12827             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
12828                                vec_full_reg_offset(s, rn),
12829                                vec_full_reg_offset(s, rm),
12830                                vec_full_reg_offset(s, rd),
12831                                is_q ? 16 : 8, vec_full_reg_size(s),
12832                                index, fns[size - 1]);
12833             return;
12834         }
12835         break;
12836     }
12837 
12838     if (size == 3) {
12839         TCGv_i64 tcg_idx = tcg_temp_new_i64();
12840         int pass;
12841 
12842         assert(is_fp && is_q && !is_long);
12843 
12844         read_vec_element(s, tcg_idx, rm, index, MO_64);
12845 
12846         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
12847             TCGv_i64 tcg_op = tcg_temp_new_i64();
12848             TCGv_i64 tcg_res = tcg_temp_new_i64();
12849 
12850             read_vec_element(s, tcg_op, rn, pass, MO_64);
12851 
12852             switch (16 * u + opcode) {
12853             case 0x05: /* FMLS */
12854                 /* As usual for ARM, separate negation for fused multiply-add */
12855                 gen_helper_vfp_negd(tcg_op, tcg_op);
12856                 /* fall through */
12857             case 0x01: /* FMLA */
12858                 read_vec_element(s, tcg_res, rd, pass, MO_64);
12859                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
12860                 break;
12861             case 0x09: /* FMUL */
12862                 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
12863                 break;
12864             case 0x19: /* FMULX */
12865                 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
12866                 break;
12867             default:
12868                 g_assert_not_reached();
12869             }
12870 
12871             write_vec_element(s, tcg_res, rd, pass, MO_64);
12872         }
12873 
12874         clear_vec_high(s, !is_scalar, rd);
12875     } else if (!is_long) {
12876         /* 32 bit floating point, or 16 or 32 bit integer.
12877          * For the 16 bit scalar case we use the usual Neon helpers and
12878          * rely on the fact that 0 op 0 == 0 with no side effects.
12879          */
12880         TCGv_i32 tcg_idx = tcg_temp_new_i32();
12881         int pass, maxpasses;
12882 
12883         if (is_scalar) {
12884             maxpasses = 1;
12885         } else {
12886             maxpasses = is_q ? 4 : 2;
12887         }
12888 
12889         read_vec_element_i32(s, tcg_idx, rm, index, size);
12890 
12891         if (size == 1 && !is_scalar) {
12892             /* The simplest way to handle the 16x16 indexed ops is to duplicate
12893              * the index into both halves of the 32 bit tcg_idx and then use
12894              * the usual Neon helpers.
12895              */
12896             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
12897         }
12898 
12899         for (pass = 0; pass < maxpasses; pass++) {
12900             TCGv_i32 tcg_op = tcg_temp_new_i32();
12901             TCGv_i32 tcg_res = tcg_temp_new_i32();
12902 
12903             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
12904 
12905             switch (16 * u + opcode) {
12906             case 0x08: /* MUL */
12907             case 0x10: /* MLA */
12908             case 0x14: /* MLS */
12909             {
12910                 static NeonGenTwoOpFn * const fns[2][2] = {
12911                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
12912                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
12913                 };
12914                 NeonGenTwoOpFn *genfn;
12915                 bool is_sub = opcode == 0x4;
12916 
12917                 if (size == 1) {
12918                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
12919                 } else {
12920                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
12921                 }
12922                 if (opcode == 0x8) {
12923                     break;
12924                 }
12925                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
12926                 genfn = fns[size - 1][is_sub];
12927                 genfn(tcg_res, tcg_op, tcg_res);
12928                 break;
12929             }
12930             case 0x05: /* FMLS */
12931             case 0x01: /* FMLA */
12932                 read_vec_element_i32(s, tcg_res, rd, pass,
12933                                      is_scalar ? size : MO_32);
12934                 switch (size) {
12935                 case 1:
12936                     if (opcode == 0x5) {
12937                         /* As usual for ARM, separate negation for fused
12938                          * multiply-add */
12939                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
12940                     }
12941                     if (is_scalar) {
12942                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
12943                                                    tcg_res, fpst);
12944                     } else {
12945                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
12946                                                     tcg_res, fpst);
12947                     }
12948                     break;
12949                 case 2:
12950                     if (opcode == 0x5) {
12951                         /* As usual for ARM, separate negation for
12952                          * fused multiply-add */
12953                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
12954                     }
12955                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
12956                                            tcg_res, fpst);
12957                     break;
12958                 default:
12959                     g_assert_not_reached();
12960                 }
12961                 break;
12962             case 0x09: /* FMUL */
12963                 switch (size) {
12964                 case 1:
12965                     if (is_scalar) {
12966                         gen_helper_advsimd_mulh(tcg_res, tcg_op,
12967                                                 tcg_idx, fpst);
12968                     } else {
12969                         gen_helper_advsimd_mul2h(tcg_res, tcg_op,
12970                                                  tcg_idx, fpst);
12971                     }
12972                     break;
12973                 case 2:
12974                     gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
12975                     break;
12976                 default:
12977                     g_assert_not_reached();
12978                 }
12979                 break;
12980             case 0x19: /* FMULX */
12981                 switch (size) {
12982                 case 1:
12983                     if (is_scalar) {
12984                         gen_helper_advsimd_mulxh(tcg_res, tcg_op,
12985                                                  tcg_idx, fpst);
12986                     } else {
12987                         gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
12988                                                   tcg_idx, fpst);
12989                     }
12990                     break;
12991                 case 2:
12992                     gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
12993                     break;
12994                 default:
12995                     g_assert_not_reached();
12996                 }
12997                 break;
12998             case 0x0c: /* SQDMULH */
12999                 if (size == 1) {
13000                     gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13001                                                tcg_op, tcg_idx);
13002                 } else {
13003                     gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13004                                                tcg_op, tcg_idx);
13005                 }
13006                 break;
13007             case 0x0d: /* SQRDMULH */
13008                 if (size == 1) {
13009                     gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13010                                                 tcg_op, tcg_idx);
13011                 } else {
13012                     gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13013                                                 tcg_op, tcg_idx);
13014                 }
13015                 break;
13016             case 0x1d: /* SQRDMLAH */
13017                 read_vec_element_i32(s, tcg_res, rd, pass,
13018                                      is_scalar ? size : MO_32);
13019                 if (size == 1) {
13020                     gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13021                                                 tcg_op, tcg_idx, tcg_res);
13022                 } else {
13023                     gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13024                                                 tcg_op, tcg_idx, tcg_res);
13025                 }
13026                 break;
13027             case 0x1f: /* SQRDMLSH */
13028                 read_vec_element_i32(s, tcg_res, rd, pass,
13029                                      is_scalar ? size : MO_32);
13030                 if (size == 1) {
13031                     gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13032                                                 tcg_op, tcg_idx, tcg_res);
13033                 } else {
13034                     gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13035                                                 tcg_op, tcg_idx, tcg_res);
13036                 }
13037                 break;
13038             default:
13039                 g_assert_not_reached();
13040             }
13041 
13042             if (is_scalar) {
13043                 write_fp_sreg(s, rd, tcg_res);
13044             } else {
13045                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13046             }
13047         }
13048 
13049         clear_vec_high(s, is_q, rd);
13050     } else {
13051         /* long ops: 16x16->32 or 32x32->64 */
13052         TCGv_i64 tcg_res[2];
13053         int pass;
13054         bool satop = extract32(opcode, 0, 1);
13055         MemOp memop = MO_32;
13056 
13057         if (satop || !u) {
13058             memop |= MO_SIGN;
13059         }
13060 
13061         if (size == 2) {
13062             TCGv_i64 tcg_idx = tcg_temp_new_i64();
13063 
13064             read_vec_element(s, tcg_idx, rm, index, memop);
13065 
13066             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13067                 TCGv_i64 tcg_op = tcg_temp_new_i64();
13068                 TCGv_i64 tcg_passres;
13069                 int passelt;
13070 
13071                 if (is_scalar) {
13072                     passelt = 0;
13073                 } else {
13074                     passelt = pass + (is_q * 2);
13075                 }
13076 
13077                 read_vec_element(s, tcg_op, rn, passelt, memop);
13078 
13079                 tcg_res[pass] = tcg_temp_new_i64();
13080 
13081                 if (opcode == 0xa || opcode == 0xb) {
13082                     /* Non-accumulating ops */
13083                     tcg_passres = tcg_res[pass];
13084                 } else {
13085                     tcg_passres = tcg_temp_new_i64();
13086                 }
13087 
13088                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13089 
13090                 if (satop) {
13091                     /* saturating, doubling */
13092                     gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13093                                                       tcg_passres, tcg_passres);
13094                 }
13095 
13096                 if (opcode == 0xa || opcode == 0xb) {
13097                     continue;
13098                 }
13099 
13100                 /* Accumulating op: handle accumulate step */
13101                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13102 
13103                 switch (opcode) {
13104                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13105                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13106                     break;
13107                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13108                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13109                     break;
13110                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13111                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
13112                     /* fall through */
13113                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13114                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13115                                                       tcg_res[pass],
13116                                                       tcg_passres);
13117                     break;
13118                 default:
13119                     g_assert_not_reached();
13120                 }
13121             }
13122 
13123             clear_vec_high(s, !is_scalar, rd);
13124         } else {
13125             TCGv_i32 tcg_idx = tcg_temp_new_i32();
13126 
13127             assert(size == 1);
13128             read_vec_element_i32(s, tcg_idx, rm, index, size);
13129 
13130             if (!is_scalar) {
13131                 /* The simplest way to handle the 16x16 indexed ops is to
13132                  * duplicate the index into both halves of the 32 bit tcg_idx
13133                  * and then use the usual Neon helpers.
13134                  */
13135                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13136             }
13137 
13138             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13139                 TCGv_i32 tcg_op = tcg_temp_new_i32();
13140                 TCGv_i64 tcg_passres;
13141 
13142                 if (is_scalar) {
13143                     read_vec_element_i32(s, tcg_op, rn, pass, size);
13144                 } else {
13145                     read_vec_element_i32(s, tcg_op, rn,
13146                                          pass + (is_q * 2), MO_32);
13147                 }
13148 
13149                 tcg_res[pass] = tcg_temp_new_i64();
13150 
13151                 if (opcode == 0xa || opcode == 0xb) {
13152                     /* Non-accumulating ops */
13153                     tcg_passres = tcg_res[pass];
13154                 } else {
13155                     tcg_passres = tcg_temp_new_i64();
13156                 }
13157 
13158                 if (memop & MO_SIGN) {
13159                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13160                 } else {
13161                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13162                 }
13163                 if (satop) {
13164                     gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13165                                                       tcg_passres, tcg_passres);
13166                 }
13167 
13168                 if (opcode == 0xa || opcode == 0xb) {
13169                     continue;
13170                 }
13171 
13172                 /* Accumulating op: handle accumulate step */
13173                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13174 
13175                 switch (opcode) {
13176                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13177                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13178                                              tcg_passres);
13179                     break;
13180                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13181                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13182                                              tcg_passres);
13183                     break;
13184                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13185                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13186                     /* fall through */
13187                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13188                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13189                                                       tcg_res[pass],
13190                                                       tcg_passres);
13191                     break;
13192                 default:
13193                     g_assert_not_reached();
13194                 }
13195             }
13196 
13197             if (is_scalar) {
13198                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13199             }
13200         }
13201 
13202         if (is_scalar) {
13203             tcg_res[1] = tcg_constant_i64(0);
13204         }
13205 
13206         for (pass = 0; pass < 2; pass++) {
13207             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13208         }
13209     }
13210 }
13211 
13212 /* Crypto AES
13213  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13214  * +-----------------+------+-----------+--------+-----+------+------+
13215  * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13216  * +-----------------+------+-----------+--------+-----+------+------+
13217  */
13218 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13219 {
13220     int size = extract32(insn, 22, 2);
13221     int opcode = extract32(insn, 12, 5);
13222     int rn = extract32(insn, 5, 5);
13223     int rd = extract32(insn, 0, 5);
13224     gen_helper_gvec_2 *genfn2 = NULL;
13225     gen_helper_gvec_3 *genfn3 = NULL;
13226 
13227     if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13228         unallocated_encoding(s);
13229         return;
13230     }
13231 
13232     switch (opcode) {
13233     case 0x4: /* AESE */
13234         genfn3 = gen_helper_crypto_aese;
13235         break;
13236     case 0x6: /* AESMC */
13237         genfn2 = gen_helper_crypto_aesmc;
13238         break;
13239     case 0x5: /* AESD */
13240         genfn3 = gen_helper_crypto_aesd;
13241         break;
13242     case 0x7: /* AESIMC */
13243         genfn2 = gen_helper_crypto_aesimc;
13244         break;
13245     default:
13246         unallocated_encoding(s);
13247         return;
13248     }
13249 
13250     if (!fp_access_check(s)) {
13251         return;
13252     }
13253     if (genfn2) {
13254         gen_gvec_op2_ool(s, true, rd, rn, 0, genfn2);
13255     } else {
13256         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, genfn3);
13257     }
13258 }
13259 
13260 /* Crypto three-reg SHA
13261  *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
13262  * +-----------------+------+---+------+---+--------+-----+------+------+
13263  * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
13264  * +-----------------+------+---+------+---+--------+-----+------+------+
13265  */
13266 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13267 {
13268     int size = extract32(insn, 22, 2);
13269     int opcode = extract32(insn, 12, 3);
13270     int rm = extract32(insn, 16, 5);
13271     int rn = extract32(insn, 5, 5);
13272     int rd = extract32(insn, 0, 5);
13273     gen_helper_gvec_3 *genfn;
13274     bool feature;
13275 
13276     if (size != 0) {
13277         unallocated_encoding(s);
13278         return;
13279     }
13280 
13281     switch (opcode) {
13282     case 0: /* SHA1C */
13283         genfn = gen_helper_crypto_sha1c;
13284         feature = dc_isar_feature(aa64_sha1, s);
13285         break;
13286     case 1: /* SHA1P */
13287         genfn = gen_helper_crypto_sha1p;
13288         feature = dc_isar_feature(aa64_sha1, s);
13289         break;
13290     case 2: /* SHA1M */
13291         genfn = gen_helper_crypto_sha1m;
13292         feature = dc_isar_feature(aa64_sha1, s);
13293         break;
13294     case 3: /* SHA1SU0 */
13295         genfn = gen_helper_crypto_sha1su0;
13296         feature = dc_isar_feature(aa64_sha1, s);
13297         break;
13298     case 4: /* SHA256H */
13299         genfn = gen_helper_crypto_sha256h;
13300         feature = dc_isar_feature(aa64_sha256, s);
13301         break;
13302     case 5: /* SHA256H2 */
13303         genfn = gen_helper_crypto_sha256h2;
13304         feature = dc_isar_feature(aa64_sha256, s);
13305         break;
13306     case 6: /* SHA256SU1 */
13307         genfn = gen_helper_crypto_sha256su1;
13308         feature = dc_isar_feature(aa64_sha256, s);
13309         break;
13310     default:
13311         unallocated_encoding(s);
13312         return;
13313     }
13314 
13315     if (!feature) {
13316         unallocated_encoding(s);
13317         return;
13318     }
13319 
13320     if (!fp_access_check(s)) {
13321         return;
13322     }
13323     gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
13324 }
13325 
13326 /* Crypto two-reg SHA
13327  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13328  * +-----------------+------+-----------+--------+-----+------+------+
13329  * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13330  * +-----------------+------+-----------+--------+-----+------+------+
13331  */
13332 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13333 {
13334     int size = extract32(insn, 22, 2);
13335     int opcode = extract32(insn, 12, 5);
13336     int rn = extract32(insn, 5, 5);
13337     int rd = extract32(insn, 0, 5);
13338     gen_helper_gvec_2 *genfn;
13339     bool feature;
13340 
13341     if (size != 0) {
13342         unallocated_encoding(s);
13343         return;
13344     }
13345 
13346     switch (opcode) {
13347     case 0: /* SHA1H */
13348         feature = dc_isar_feature(aa64_sha1, s);
13349         genfn = gen_helper_crypto_sha1h;
13350         break;
13351     case 1: /* SHA1SU1 */
13352         feature = dc_isar_feature(aa64_sha1, s);
13353         genfn = gen_helper_crypto_sha1su1;
13354         break;
13355     case 2: /* SHA256SU0 */
13356         feature = dc_isar_feature(aa64_sha256, s);
13357         genfn = gen_helper_crypto_sha256su0;
13358         break;
13359     default:
13360         unallocated_encoding(s);
13361         return;
13362     }
13363 
13364     if (!feature) {
13365         unallocated_encoding(s);
13366         return;
13367     }
13368 
13369     if (!fp_access_check(s)) {
13370         return;
13371     }
13372     gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
13373 }
13374 
13375 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
13376 {
13377     tcg_gen_rotli_i64(d, m, 1);
13378     tcg_gen_xor_i64(d, d, n);
13379 }
13380 
13381 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
13382 {
13383     tcg_gen_rotli_vec(vece, d, m, 1);
13384     tcg_gen_xor_vec(vece, d, d, n);
13385 }
13386 
13387 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
13388                    uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
13389 {
13390     static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
13391     static const GVecGen3 op = {
13392         .fni8 = gen_rax1_i64,
13393         .fniv = gen_rax1_vec,
13394         .opt_opc = vecop_list,
13395         .fno = gen_helper_crypto_rax1,
13396         .vece = MO_64,
13397     };
13398     tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
13399 }
13400 
13401 /* Crypto three-reg SHA512
13402  *  31                   21 20  16 15  14  13 12  11  10  9    5 4    0
13403  * +-----------------------+------+---+---+-----+--------+------+------+
13404  * | 1 1 0 0 1 1 1 0 0 1 1 |  Rm  | 1 | O | 0 0 | opcode |  Rn  |  Rd  |
13405  * +-----------------------+------+---+---+-----+--------+------+------+
13406  */
13407 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13408 {
13409     int opcode = extract32(insn, 10, 2);
13410     int o =  extract32(insn, 14, 1);
13411     int rm = extract32(insn, 16, 5);
13412     int rn = extract32(insn, 5, 5);
13413     int rd = extract32(insn, 0, 5);
13414     bool feature;
13415     gen_helper_gvec_3 *oolfn = NULL;
13416     GVecGen3Fn *gvecfn = NULL;
13417 
13418     if (o == 0) {
13419         switch (opcode) {
13420         case 0: /* SHA512H */
13421             feature = dc_isar_feature(aa64_sha512, s);
13422             oolfn = gen_helper_crypto_sha512h;
13423             break;
13424         case 1: /* SHA512H2 */
13425             feature = dc_isar_feature(aa64_sha512, s);
13426             oolfn = gen_helper_crypto_sha512h2;
13427             break;
13428         case 2: /* SHA512SU1 */
13429             feature = dc_isar_feature(aa64_sha512, s);
13430             oolfn = gen_helper_crypto_sha512su1;
13431             break;
13432         case 3: /* RAX1 */
13433             feature = dc_isar_feature(aa64_sha3, s);
13434             gvecfn = gen_gvec_rax1;
13435             break;
13436         default:
13437             g_assert_not_reached();
13438         }
13439     } else {
13440         switch (opcode) {
13441         case 0: /* SM3PARTW1 */
13442             feature = dc_isar_feature(aa64_sm3, s);
13443             oolfn = gen_helper_crypto_sm3partw1;
13444             break;
13445         case 1: /* SM3PARTW2 */
13446             feature = dc_isar_feature(aa64_sm3, s);
13447             oolfn = gen_helper_crypto_sm3partw2;
13448             break;
13449         case 2: /* SM4EKEY */
13450             feature = dc_isar_feature(aa64_sm4, s);
13451             oolfn = gen_helper_crypto_sm4ekey;
13452             break;
13453         default:
13454             unallocated_encoding(s);
13455             return;
13456         }
13457     }
13458 
13459     if (!feature) {
13460         unallocated_encoding(s);
13461         return;
13462     }
13463 
13464     if (!fp_access_check(s)) {
13465         return;
13466     }
13467 
13468     if (oolfn) {
13469         gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
13470     } else {
13471         gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
13472     }
13473 }
13474 
13475 /* Crypto two-reg SHA512
13476  *  31                                     12  11  10  9    5 4    0
13477  * +-----------------------------------------+--------+------+------+
13478  * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode |  Rn  |  Rd  |
13479  * +-----------------------------------------+--------+------+------+
13480  */
13481 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13482 {
13483     int opcode = extract32(insn, 10, 2);
13484     int rn = extract32(insn, 5, 5);
13485     int rd = extract32(insn, 0, 5);
13486     bool feature;
13487 
13488     switch (opcode) {
13489     case 0: /* SHA512SU0 */
13490         feature = dc_isar_feature(aa64_sha512, s);
13491         break;
13492     case 1: /* SM4E */
13493         feature = dc_isar_feature(aa64_sm4, s);
13494         break;
13495     default:
13496         unallocated_encoding(s);
13497         return;
13498     }
13499 
13500     if (!feature) {
13501         unallocated_encoding(s);
13502         return;
13503     }
13504 
13505     if (!fp_access_check(s)) {
13506         return;
13507     }
13508 
13509     switch (opcode) {
13510     case 0: /* SHA512SU0 */
13511         gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
13512         break;
13513     case 1: /* SM4E */
13514         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
13515         break;
13516     default:
13517         g_assert_not_reached();
13518     }
13519 }
13520 
13521 /* Crypto four-register
13522  *  31               23 22 21 20  16 15  14  10 9    5 4    0
13523  * +-------------------+-----+------+---+------+------+------+
13524  * | 1 1 0 0 1 1 1 0 0 | Op0 |  Rm  | 0 |  Ra  |  Rn  |  Rd  |
13525  * +-------------------+-----+------+---+------+------+------+
13526  */
13527 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13528 {
13529     int op0 = extract32(insn, 21, 2);
13530     int rm = extract32(insn, 16, 5);
13531     int ra = extract32(insn, 10, 5);
13532     int rn = extract32(insn, 5, 5);
13533     int rd = extract32(insn, 0, 5);
13534     bool feature;
13535 
13536     switch (op0) {
13537     case 0: /* EOR3 */
13538     case 1: /* BCAX */
13539         feature = dc_isar_feature(aa64_sha3, s);
13540         break;
13541     case 2: /* SM3SS1 */
13542         feature = dc_isar_feature(aa64_sm3, s);
13543         break;
13544     default:
13545         unallocated_encoding(s);
13546         return;
13547     }
13548 
13549     if (!feature) {
13550         unallocated_encoding(s);
13551         return;
13552     }
13553 
13554     if (!fp_access_check(s)) {
13555         return;
13556     }
13557 
13558     if (op0 < 2) {
13559         TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13560         int pass;
13561 
13562         tcg_op1 = tcg_temp_new_i64();
13563         tcg_op2 = tcg_temp_new_i64();
13564         tcg_op3 = tcg_temp_new_i64();
13565         tcg_res[0] = tcg_temp_new_i64();
13566         tcg_res[1] = tcg_temp_new_i64();
13567 
13568         for (pass = 0; pass < 2; pass++) {
13569             read_vec_element(s, tcg_op1, rn, pass, MO_64);
13570             read_vec_element(s, tcg_op2, rm, pass, MO_64);
13571             read_vec_element(s, tcg_op3, ra, pass, MO_64);
13572 
13573             if (op0 == 0) {
13574                 /* EOR3 */
13575                 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13576             } else {
13577                 /* BCAX */
13578                 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13579             }
13580             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13581         }
13582         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13583         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13584     } else {
13585         TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13586 
13587         tcg_op1 = tcg_temp_new_i32();
13588         tcg_op2 = tcg_temp_new_i32();
13589         tcg_op3 = tcg_temp_new_i32();
13590         tcg_res = tcg_temp_new_i32();
13591         tcg_zero = tcg_constant_i32(0);
13592 
13593         read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13594         read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13595         read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13596 
13597         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13598         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13599         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13600         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13601 
13602         write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13603         write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13604         write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13605         write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13606     }
13607 }
13608 
13609 /* Crypto XAR
13610  *  31                   21 20  16 15    10 9    5 4    0
13611  * +-----------------------+------+--------+------+------+
13612  * | 1 1 0 0 1 1 1 0 1 0 0 |  Rm  |  imm6  |  Rn  |  Rd  |
13613  * +-----------------------+------+--------+------+------+
13614  */
13615 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
13616 {
13617     int rm = extract32(insn, 16, 5);
13618     int imm6 = extract32(insn, 10, 6);
13619     int rn = extract32(insn, 5, 5);
13620     int rd = extract32(insn, 0, 5);
13621 
13622     if (!dc_isar_feature(aa64_sha3, s)) {
13623         unallocated_encoding(s);
13624         return;
13625     }
13626 
13627     if (!fp_access_check(s)) {
13628         return;
13629     }
13630 
13631     gen_gvec_xar(MO_64, vec_full_reg_offset(s, rd),
13632                  vec_full_reg_offset(s, rn),
13633                  vec_full_reg_offset(s, rm), imm6, 16,
13634                  vec_full_reg_size(s));
13635 }
13636 
13637 /* Crypto three-reg imm2
13638  *  31                   21 20  16 15  14 13 12  11  10  9    5 4    0
13639  * +-----------------------+------+-----+------+--------+------+------+
13640  * | 1 1 0 0 1 1 1 0 0 1 0 |  Rm  | 1 0 | imm2 | opcode |  Rn  |  Rd  |
13641  * +-----------------------+------+-----+------+--------+------+------+
13642  */
13643 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
13644 {
13645     static gen_helper_gvec_3 * const fns[4] = {
13646         gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
13647         gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
13648     };
13649     int opcode = extract32(insn, 10, 2);
13650     int imm2 = extract32(insn, 12, 2);
13651     int rm = extract32(insn, 16, 5);
13652     int rn = extract32(insn, 5, 5);
13653     int rd = extract32(insn, 0, 5);
13654 
13655     if (!dc_isar_feature(aa64_sm3, s)) {
13656         unallocated_encoding(s);
13657         return;
13658     }
13659 
13660     if (!fp_access_check(s)) {
13661         return;
13662     }
13663 
13664     gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
13665 }
13666 
13667 /* C3.6 Data processing - SIMD, inc Crypto
13668  *
13669  * As the decode gets a little complex we are using a table based
13670  * approach for this part of the decode.
13671  */
13672 static const AArch64DecodeTable data_proc_simd[] = {
13673     /* pattern  ,  mask     ,  fn                        */
13674     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
13675     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
13676     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
13677     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
13678     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
13679     { 0x0e000400, 0x9fe08400, disas_simd_copy },
13680     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
13681     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
13682     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
13683     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
13684     { 0x0e000000, 0xbf208c00, disas_simd_tb },
13685     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
13686     { 0x2e000000, 0xbf208400, disas_simd_ext },
13687     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
13688     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
13689     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
13690     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
13691     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
13692     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
13693     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
13694     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
13695     { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
13696     { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
13697     { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
13698     { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
13699     { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
13700     { 0xce000000, 0xff808000, disas_crypto_four_reg },
13701     { 0xce800000, 0xffe00000, disas_crypto_xar },
13702     { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
13703     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
13704     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
13705     { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
13706     { 0x00000000, 0x00000000, NULL }
13707 };
13708 
13709 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
13710 {
13711     /* Note that this is called with all non-FP cases from
13712      * table C3-6 so it must UNDEF for entries not specifically
13713      * allocated to instructions in that table.
13714      */
13715     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
13716     if (fn) {
13717         fn(s, insn);
13718     } else {
13719         unallocated_encoding(s);
13720     }
13721 }
13722 
13723 /* C3.6 Data processing - SIMD and floating point */
13724 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
13725 {
13726     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
13727         disas_data_proc_fp(s, insn);
13728     } else {
13729         /* SIMD, including crypto */
13730         disas_data_proc_simd(s, insn);
13731     }
13732 }
13733 
13734 static bool trans_OK(DisasContext *s, arg_OK *a)
13735 {
13736     return true;
13737 }
13738 
13739 static bool trans_FAIL(DisasContext *s, arg_OK *a)
13740 {
13741     s->is_nonstreaming = true;
13742     return true;
13743 }
13744 
13745 /**
13746  * is_guarded_page:
13747  * @env: The cpu environment
13748  * @s: The DisasContext
13749  *
13750  * Return true if the page is guarded.
13751  */
13752 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
13753 {
13754     uint64_t addr = s->base.pc_first;
13755 #ifdef CONFIG_USER_ONLY
13756     return page_get_flags(addr) & PAGE_BTI;
13757 #else
13758     CPUTLBEntryFull *full;
13759     void *host;
13760     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
13761     int flags;
13762 
13763     /*
13764      * We test this immediately after reading an insn, which means
13765      * that the TLB entry must be present and valid, and thus this
13766      * access will never raise an exception.
13767      */
13768     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
13769                               false, &host, &full, 0);
13770     assert(!(flags & TLB_INVALID_MASK));
13771 
13772     return full->guarded;
13773 #endif
13774 }
13775 
13776 /**
13777  * btype_destination_ok:
13778  * @insn: The instruction at the branch destination
13779  * @bt: SCTLR_ELx.BT
13780  * @btype: PSTATE.BTYPE, and is non-zero
13781  *
13782  * On a guarded page, there are a limited number of insns
13783  * that may be present at the branch target:
13784  *   - branch target identifiers,
13785  *   - paciasp, pacibsp,
13786  *   - BRK insn
13787  *   - HLT insn
13788  * Anything else causes a Branch Target Exception.
13789  *
13790  * Return true if the branch is compatible, false to raise BTITRAP.
13791  */
13792 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
13793 {
13794     if ((insn & 0xfffff01fu) == 0xd503201fu) {
13795         /* HINT space */
13796         switch (extract32(insn, 5, 7)) {
13797         case 0b011001: /* PACIASP */
13798         case 0b011011: /* PACIBSP */
13799             /*
13800              * If SCTLR_ELx.BT, then PACI*SP are not compatible
13801              * with btype == 3.  Otherwise all btype are ok.
13802              */
13803             return !bt || btype != 3;
13804         case 0b100000: /* BTI */
13805             /* Not compatible with any btype.  */
13806             return false;
13807         case 0b100010: /* BTI c */
13808             /* Not compatible with btype == 3 */
13809             return btype != 3;
13810         case 0b100100: /* BTI j */
13811             /* Not compatible with btype == 2 */
13812             return btype != 2;
13813         case 0b100110: /* BTI jc */
13814             /* Compatible with any btype.  */
13815             return true;
13816         }
13817     } else {
13818         switch (insn & 0xffe0001fu) {
13819         case 0xd4200000u: /* BRK */
13820         case 0xd4400000u: /* HLT */
13821             /* Give priority to the breakpoint exception.  */
13822             return true;
13823         }
13824     }
13825     return false;
13826 }
13827 
13828 /* C3.1 A64 instruction index by encoding */
13829 static void disas_a64_legacy(DisasContext *s, uint32_t insn)
13830 {
13831     switch (extract32(insn, 25, 4)) {
13832     case 0x5:
13833     case 0xd:      /* Data processing - register */
13834         disas_data_proc_reg(s, insn);
13835         break;
13836     case 0x7:
13837     case 0xf:      /* Data processing - SIMD and floating point */
13838         disas_data_proc_simd_fp(s, insn);
13839         break;
13840     default:
13841         unallocated_encoding(s);
13842         break;
13843     }
13844 }
13845 
13846 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
13847                                           CPUState *cpu)
13848 {
13849     DisasContext *dc = container_of(dcbase, DisasContext, base);
13850     CPUARMState *env = cpu->env_ptr;
13851     ARMCPU *arm_cpu = env_archcpu(env);
13852     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
13853     int bound, core_mmu_idx;
13854 
13855     dc->isar = &arm_cpu->isar;
13856     dc->condjmp = 0;
13857     dc->pc_save = dc->base.pc_first;
13858     dc->aarch64 = true;
13859     dc->thumb = false;
13860     dc->sctlr_b = 0;
13861     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
13862     dc->condexec_mask = 0;
13863     dc->condexec_cond = 0;
13864     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
13865     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
13866     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
13867     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
13868     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
13869     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
13870 #if !defined(CONFIG_USER_ONLY)
13871     dc->user = (dc->current_el == 0);
13872 #endif
13873     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
13874     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
13875     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
13876     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
13877     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
13878     dc->fgt_eret = EX_TBFLAG_A64(tb_flags, FGT_ERET);
13879     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
13880     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
13881     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
13882     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
13883     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
13884     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
13885     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
13886     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
13887     dc->ata = EX_TBFLAG_A64(tb_flags, ATA);
13888     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
13889     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
13890     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
13891     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
13892     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
13893     dc->naa = EX_TBFLAG_A64(tb_flags, NAA);
13894     dc->vec_len = 0;
13895     dc->vec_stride = 0;
13896     dc->cp_regs = arm_cpu->cp_regs;
13897     dc->features = env->features;
13898     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
13899     dc->gm_blocksize = arm_cpu->gm_blocksize;
13900 
13901 #ifdef CONFIG_USER_ONLY
13902     /* In sve_probe_page, we assume TBI is enabled. */
13903     tcg_debug_assert(dc->tbid & 1);
13904 #endif
13905 
13906     dc->lse2 = dc_isar_feature(aa64_lse2, dc);
13907 
13908     /* Single step state. The code-generation logic here is:
13909      *  SS_ACTIVE == 0:
13910      *   generate code with no special handling for single-stepping (except
13911      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
13912      *   this happens anyway because those changes are all system register or
13913      *   PSTATE writes).
13914      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
13915      *   emit code for one insn
13916      *   emit code to clear PSTATE.SS
13917      *   emit code to generate software step exception for completed step
13918      *   end TB (as usual for having generated an exception)
13919      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
13920      *   emit code to generate a software step exception
13921      *   end the TB
13922      */
13923     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
13924     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
13925     dc->is_ldex = false;
13926 
13927     /* Bound the number of insns to execute to those left on the page.  */
13928     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
13929 
13930     /* If architectural single step active, limit to 1.  */
13931     if (dc->ss_active) {
13932         bound = 1;
13933     }
13934     dc->base.max_insns = MIN(dc->base.max_insns, bound);
13935 }
13936 
13937 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
13938 {
13939 }
13940 
13941 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
13942 {
13943     DisasContext *dc = container_of(dcbase, DisasContext, base);
13944     target_ulong pc_arg = dc->base.pc_next;
13945 
13946     if (tb_cflags(dcbase->tb) & CF_PCREL) {
13947         pc_arg &= ~TARGET_PAGE_MASK;
13948     }
13949     tcg_gen_insn_start(pc_arg, 0, 0);
13950     dc->insn_start = tcg_last_op();
13951 }
13952 
13953 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
13954 {
13955     DisasContext *s = container_of(dcbase, DisasContext, base);
13956     CPUARMState *env = cpu->env_ptr;
13957     uint64_t pc = s->base.pc_next;
13958     uint32_t insn;
13959 
13960     /* Singlestep exceptions have the highest priority. */
13961     if (s->ss_active && !s->pstate_ss) {
13962         /* Singlestep state is Active-pending.
13963          * If we're in this state at the start of a TB then either
13964          *  a) we just took an exception to an EL which is being debugged
13965          *     and this is the first insn in the exception handler
13966          *  b) debug exceptions were masked and we just unmasked them
13967          *     without changing EL (eg by clearing PSTATE.D)
13968          * In either case we're going to take a swstep exception in the
13969          * "did not step an insn" case, and so the syndrome ISV and EX
13970          * bits should be zero.
13971          */
13972         assert(s->base.num_insns == 1);
13973         gen_swstep_exception(s, 0, 0);
13974         s->base.is_jmp = DISAS_NORETURN;
13975         s->base.pc_next = pc + 4;
13976         return;
13977     }
13978 
13979     if (pc & 3) {
13980         /*
13981          * PC alignment fault.  This has priority over the instruction abort
13982          * that we would receive from a translation fault via arm_ldl_code.
13983          * This should only be possible after an indirect branch, at the
13984          * start of the TB.
13985          */
13986         assert(s->base.num_insns == 1);
13987         gen_helper_exception_pc_alignment(cpu_env, tcg_constant_tl(pc));
13988         s->base.is_jmp = DISAS_NORETURN;
13989         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
13990         return;
13991     }
13992 
13993     s->pc_curr = pc;
13994     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
13995     s->insn = insn;
13996     s->base.pc_next = pc + 4;
13997 
13998     s->fp_access_checked = false;
13999     s->sve_access_checked = false;
14000 
14001     if (s->pstate_il) {
14002         /*
14003          * Illegal execution state. This has priority over BTI
14004          * exceptions, but comes after instruction abort exceptions.
14005          */
14006         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
14007         return;
14008     }
14009 
14010     if (dc_isar_feature(aa64_bti, s)) {
14011         if (s->base.num_insns == 1) {
14012             /*
14013              * At the first insn of the TB, compute s->guarded_page.
14014              * We delayed computing this until successfully reading
14015              * the first insn of the TB, above.  This (mostly) ensures
14016              * that the softmmu tlb entry has been populated, and the
14017              * page table GP bit is available.
14018              *
14019              * Note that we need to compute this even if btype == 0,
14020              * because this value is used for BR instructions later
14021              * where ENV is not available.
14022              */
14023             s->guarded_page = is_guarded_page(env, s);
14024 
14025             /* First insn can have btype set to non-zero.  */
14026             tcg_debug_assert(s->btype >= 0);
14027 
14028             /*
14029              * Note that the Branch Target Exception has fairly high
14030              * priority -- below debugging exceptions but above most
14031              * everything else.  This allows us to handle this now
14032              * instead of waiting until the insn is otherwise decoded.
14033              */
14034             if (s->btype != 0
14035                 && s->guarded_page
14036                 && !btype_destination_ok(insn, s->bt, s->btype)) {
14037                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
14038                 return;
14039             }
14040         } else {
14041             /* Not the first insn: btype must be 0.  */
14042             tcg_debug_assert(s->btype == 0);
14043         }
14044     }
14045 
14046     s->is_nonstreaming = false;
14047     if (s->sme_trap_nonstreaming) {
14048         disas_sme_fa64(s, insn);
14049     }
14050 
14051     if (!disas_a64(s, insn) &&
14052         !disas_sme(s, insn) &&
14053         !disas_sve(s, insn)) {
14054         disas_a64_legacy(s, insn);
14055     }
14056 
14057     /*
14058      * After execution of most insns, btype is reset to 0.
14059      * Note that we set btype == -1 when the insn sets btype.
14060      */
14061     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14062         reset_btype(s);
14063     }
14064 }
14065 
14066 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14067 {
14068     DisasContext *dc = container_of(dcbase, DisasContext, base);
14069 
14070     if (unlikely(dc->ss_active)) {
14071         /* Note that this means single stepping WFI doesn't halt the CPU.
14072          * For conditional branch insns this is harmless unreachable code as
14073          * gen_goto_tb() has already handled emitting the debug exception
14074          * (and thus a tb-jump is not possible when singlestepping).
14075          */
14076         switch (dc->base.is_jmp) {
14077         default:
14078             gen_a64_update_pc(dc, 4);
14079             /* fall through */
14080         case DISAS_EXIT:
14081         case DISAS_JUMP:
14082             gen_step_complete_exception(dc);
14083             break;
14084         case DISAS_NORETURN:
14085             break;
14086         }
14087     } else {
14088         switch (dc->base.is_jmp) {
14089         case DISAS_NEXT:
14090         case DISAS_TOO_MANY:
14091             gen_goto_tb(dc, 1, 4);
14092             break;
14093         default:
14094         case DISAS_UPDATE_EXIT:
14095             gen_a64_update_pc(dc, 4);
14096             /* fall through */
14097         case DISAS_EXIT:
14098             tcg_gen_exit_tb(NULL, 0);
14099             break;
14100         case DISAS_UPDATE_NOCHAIN:
14101             gen_a64_update_pc(dc, 4);
14102             /* fall through */
14103         case DISAS_JUMP:
14104             tcg_gen_lookup_and_goto_ptr();
14105             break;
14106         case DISAS_NORETURN:
14107         case DISAS_SWI:
14108             break;
14109         case DISAS_WFE:
14110             gen_a64_update_pc(dc, 4);
14111             gen_helper_wfe(cpu_env);
14112             break;
14113         case DISAS_YIELD:
14114             gen_a64_update_pc(dc, 4);
14115             gen_helper_yield(cpu_env);
14116             break;
14117         case DISAS_WFI:
14118             /*
14119              * This is a special case because we don't want to just halt
14120              * the CPU if trying to debug across a WFI.
14121              */
14122             gen_a64_update_pc(dc, 4);
14123             gen_helper_wfi(cpu_env, tcg_constant_i32(4));
14124             /*
14125              * The helper doesn't necessarily throw an exception, but we
14126              * must go back to the main loop to check for interrupts anyway.
14127              */
14128             tcg_gen_exit_tb(NULL, 0);
14129             break;
14130         }
14131     }
14132 }
14133 
14134 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14135                                  CPUState *cpu, FILE *logfile)
14136 {
14137     DisasContext *dc = container_of(dcbase, DisasContext, base);
14138 
14139     fprintf(logfile, "IN: %s\n", lookup_symbol(dc->base.pc_first));
14140     target_disas(logfile, cpu, dc->base.pc_first, dc->base.tb->size);
14141 }
14142 
14143 const TranslatorOps aarch64_translator_ops = {
14144     .init_disas_context = aarch64_tr_init_disas_context,
14145     .tb_start           = aarch64_tr_tb_start,
14146     .insn_start         = aarch64_tr_insn_start,
14147     .translate_insn     = aarch64_tr_translate_insn,
14148     .tb_stop            = aarch64_tr_tb_stop,
14149     .disas_log          = aarch64_tr_disas_log,
14150 };
14151