xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision afcd5df5)
1 /*
2  *  AArch64 translation
3  *
4  *  Copyright (c) 2013 Alexander Graf <agraf@suse.de>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "qemu/osdep.h"
20 
21 #include "translate.h"
22 #include "translate-a64.h"
23 #include "qemu/log.h"
24 #include "disas/disas.h"
25 #include "arm_ldst.h"
26 #include "semihosting/semihost.h"
27 #include "cpregs.h"
28 
29 static TCGv_i64 cpu_X[32];
30 static TCGv_i64 cpu_pc;
31 
32 /* Load/store exclusive handling */
33 static TCGv_i64 cpu_exclusive_high;
34 
35 static const char *regnames[] = {
36     "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
37     "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
38     "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
39     "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
40 };
41 
42 enum a64_shift_type {
43     A64_SHIFT_TYPE_LSL = 0,
44     A64_SHIFT_TYPE_LSR = 1,
45     A64_SHIFT_TYPE_ASR = 2,
46     A64_SHIFT_TYPE_ROR = 3
47 };
48 
49 /*
50  * Include the generated decoders.
51  */
52 
53 #include "decode-sme-fa64.c.inc"
54 #include "decode-a64.c.inc"
55 
56 /* Table based decoder typedefs - used when the relevant bits for decode
57  * are too awkwardly scattered across the instruction (eg SIMD).
58  */
59 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
60 
61 typedef struct AArch64DecodeTable {
62     uint32_t pattern;
63     uint32_t mask;
64     AArch64DecodeFn *disas_fn;
65 } AArch64DecodeTable;
66 
67 /* initialize TCG globals.  */
68 void a64_translate_init(void)
69 {
70     int i;
71 
72     cpu_pc = tcg_global_mem_new_i64(cpu_env,
73                                     offsetof(CPUARMState, pc),
74                                     "pc");
75     for (i = 0; i < 32; i++) {
76         cpu_X[i] = tcg_global_mem_new_i64(cpu_env,
77                                           offsetof(CPUARMState, xregs[i]),
78                                           regnames[i]);
79     }
80 
81     cpu_exclusive_high = tcg_global_mem_new_i64(cpu_env,
82         offsetof(CPUARMState, exclusive_high), "exclusive_high");
83 }
84 
85 /*
86  * Return the core mmu_idx to use for A64 "unprivileged load/store" insns
87  */
88 static int get_a64_user_mem_index(DisasContext *s)
89 {
90     /*
91      * If AccType_UNPRIV is not used, the insn uses AccType_NORMAL,
92      * which is the usual mmu_idx for this cpu state.
93      */
94     ARMMMUIdx useridx = s->mmu_idx;
95 
96     if (s->unpriv) {
97         /*
98          * We have pre-computed the condition for AccType_UNPRIV.
99          * Therefore we should never get here with a mmu_idx for
100          * which we do not know the corresponding user mmu_idx.
101          */
102         switch (useridx) {
103         case ARMMMUIdx_E10_1:
104         case ARMMMUIdx_E10_1_PAN:
105             useridx = ARMMMUIdx_E10_0;
106             break;
107         case ARMMMUIdx_E20_2:
108         case ARMMMUIdx_E20_2_PAN:
109             useridx = ARMMMUIdx_E20_0;
110             break;
111         default:
112             g_assert_not_reached();
113         }
114     }
115     return arm_to_core_mmu_idx(useridx);
116 }
117 
118 static void set_btype_raw(int val)
119 {
120     tcg_gen_st_i32(tcg_constant_i32(val), cpu_env,
121                    offsetof(CPUARMState, btype));
122 }
123 
124 static void set_btype(DisasContext *s, int val)
125 {
126     /* BTYPE is a 2-bit field, and 0 should be done with reset_btype.  */
127     tcg_debug_assert(val >= 1 && val <= 3);
128     set_btype_raw(val);
129     s->btype = -1;
130 }
131 
132 static void reset_btype(DisasContext *s)
133 {
134     if (s->btype != 0) {
135         set_btype_raw(0);
136         s->btype = 0;
137     }
138 }
139 
140 static void gen_pc_plus_diff(DisasContext *s, TCGv_i64 dest, target_long diff)
141 {
142     assert(s->pc_save != -1);
143     if (tb_cflags(s->base.tb) & CF_PCREL) {
144         tcg_gen_addi_i64(dest, cpu_pc, (s->pc_curr - s->pc_save) + diff);
145     } else {
146         tcg_gen_movi_i64(dest, s->pc_curr + diff);
147     }
148 }
149 
150 void gen_a64_update_pc(DisasContext *s, target_long diff)
151 {
152     gen_pc_plus_diff(s, cpu_pc, diff);
153     s->pc_save = s->pc_curr + diff;
154 }
155 
156 /*
157  * Handle Top Byte Ignore (TBI) bits.
158  *
159  * If address tagging is enabled via the TCR TBI bits:
160  *  + for EL2 and EL3 there is only one TBI bit, and if it is set
161  *    then the address is zero-extended, clearing bits [63:56]
162  *  + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
163  *    and TBI1 controls addressses with bit 55 == 1.
164  *    If the appropriate TBI bit is set for the address then
165  *    the address is sign-extended from bit 55 into bits [63:56]
166  *
167  * Here We have concatenated TBI{1,0} into tbi.
168  */
169 static void gen_top_byte_ignore(DisasContext *s, TCGv_i64 dst,
170                                 TCGv_i64 src, int tbi)
171 {
172     if (tbi == 0) {
173         /* Load unmodified address */
174         tcg_gen_mov_i64(dst, src);
175     } else if (!regime_has_2_ranges(s->mmu_idx)) {
176         /* Force tag byte to all zero */
177         tcg_gen_extract_i64(dst, src, 0, 56);
178     } else {
179         /* Sign-extend from bit 55.  */
180         tcg_gen_sextract_i64(dst, src, 0, 56);
181 
182         switch (tbi) {
183         case 1:
184             /* tbi0 but !tbi1: only use the extension if positive */
185             tcg_gen_and_i64(dst, dst, src);
186             break;
187         case 2:
188             /* !tbi0 but tbi1: only use the extension if negative */
189             tcg_gen_or_i64(dst, dst, src);
190             break;
191         case 3:
192             /* tbi0 and tbi1: always use the extension */
193             break;
194         default:
195             g_assert_not_reached();
196         }
197     }
198 }
199 
200 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
201 {
202     /*
203      * If address tagging is enabled for instructions via the TCR TBI bits,
204      * then loading an address into the PC will clear out any tag.
205      */
206     gen_top_byte_ignore(s, cpu_pc, src, s->tbii);
207     s->pc_save = -1;
208 }
209 
210 /*
211  * Handle MTE and/or TBI.
212  *
213  * For TBI, ideally, we would do nothing.  Proper behaviour on fault is
214  * for the tag to be present in the FAR_ELx register.  But for user-only
215  * mode we do not have a TLB with which to implement this, so we must
216  * remove the top byte now.
217  *
218  * Always return a fresh temporary that we can increment independently
219  * of the write-back address.
220  */
221 
222 TCGv_i64 clean_data_tbi(DisasContext *s, TCGv_i64 addr)
223 {
224     TCGv_i64 clean = tcg_temp_new_i64();
225 #ifdef CONFIG_USER_ONLY
226     gen_top_byte_ignore(s, clean, addr, s->tbid);
227 #else
228     tcg_gen_mov_i64(clean, addr);
229 #endif
230     return clean;
231 }
232 
233 /* Insert a zero tag into src, with the result at dst. */
234 static void gen_address_with_allocation_tag0(TCGv_i64 dst, TCGv_i64 src)
235 {
236     tcg_gen_andi_i64(dst, src, ~MAKE_64BIT_MASK(56, 4));
237 }
238 
239 static void gen_probe_access(DisasContext *s, TCGv_i64 ptr,
240                              MMUAccessType acc, int log2_size)
241 {
242     gen_helper_probe_access(cpu_env, ptr,
243                             tcg_constant_i32(acc),
244                             tcg_constant_i32(get_mem_index(s)),
245                             tcg_constant_i32(1 << log2_size));
246 }
247 
248 /*
249  * For MTE, check a single logical or atomic access.  This probes a single
250  * address, the exact one specified.  The size and alignment of the access
251  * is not relevant to MTE, per se, but watchpoints do require the size,
252  * and we want to recognize those before making any other changes to state.
253  */
254 static TCGv_i64 gen_mte_check1_mmuidx(DisasContext *s, TCGv_i64 addr,
255                                       bool is_write, bool tag_checked,
256                                       MemOp memop, bool is_unpriv,
257                                       int core_idx)
258 {
259     if (tag_checked && s->mte_active[is_unpriv]) {
260         TCGv_i64 ret;
261         int desc = 0;
262 
263         desc = FIELD_DP32(desc, MTEDESC, MIDX, core_idx);
264         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
265         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
266         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
267         desc = FIELD_DP32(desc, MTEDESC, ALIGN, get_alignment_bits(memop));
268         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, memop_size(memop) - 1);
269 
270         ret = tcg_temp_new_i64();
271         gen_helper_mte_check(ret, cpu_env, tcg_constant_i32(desc), addr);
272 
273         return ret;
274     }
275     return clean_data_tbi(s, addr);
276 }
277 
278 TCGv_i64 gen_mte_check1(DisasContext *s, TCGv_i64 addr, bool is_write,
279                         bool tag_checked, MemOp memop)
280 {
281     return gen_mte_check1_mmuidx(s, addr, is_write, tag_checked, memop,
282                                  false, get_mem_index(s));
283 }
284 
285 /*
286  * For MTE, check multiple logical sequential accesses.
287  */
288 TCGv_i64 gen_mte_checkN(DisasContext *s, TCGv_i64 addr, bool is_write,
289                         bool tag_checked, int total_size, MemOp single_mop)
290 {
291     if (tag_checked && s->mte_active[0]) {
292         TCGv_i64 ret;
293         int desc = 0;
294 
295         desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
296         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
297         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
298         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
299         desc = FIELD_DP32(desc, MTEDESC, ALIGN, get_alignment_bits(single_mop));
300         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, total_size - 1);
301 
302         ret = tcg_temp_new_i64();
303         gen_helper_mte_check(ret, cpu_env, tcg_constant_i32(desc), addr);
304 
305         return ret;
306     }
307     return clean_data_tbi(s, addr);
308 }
309 
310 /*
311  * Generate the special alignment check that applies to AccType_ATOMIC
312  * and AccType_ORDERED insns under FEAT_LSE2: the access need not be
313  * naturally aligned, but it must not cross a 16-byte boundary.
314  * See AArch64.CheckAlignment().
315  */
316 static void check_lse2_align(DisasContext *s, int rn, int imm,
317                              bool is_write, MemOp mop)
318 {
319     TCGv_i32 tmp;
320     TCGv_i64 addr;
321     TCGLabel *over_label;
322     MMUAccessType type;
323     int mmu_idx;
324 
325     tmp = tcg_temp_new_i32();
326     tcg_gen_extrl_i64_i32(tmp, cpu_reg_sp(s, rn));
327     tcg_gen_addi_i32(tmp, tmp, imm & 15);
328     tcg_gen_andi_i32(tmp, tmp, 15);
329     tcg_gen_addi_i32(tmp, tmp, memop_size(mop));
330 
331     over_label = gen_new_label();
332     tcg_gen_brcondi_i32(TCG_COND_LEU, tmp, 16, over_label);
333 
334     addr = tcg_temp_new_i64();
335     tcg_gen_addi_i64(addr, cpu_reg_sp(s, rn), imm);
336 
337     type = is_write ? MMU_DATA_STORE : MMU_DATA_LOAD,
338     mmu_idx = get_mem_index(s);
339     gen_helper_unaligned_access(cpu_env, addr, tcg_constant_i32(type),
340                                 tcg_constant_i32(mmu_idx));
341 
342     gen_set_label(over_label);
343 
344 }
345 
346 /* Handle the alignment check for AccType_ATOMIC instructions. */
347 static MemOp check_atomic_align(DisasContext *s, int rn, MemOp mop)
348 {
349     MemOp size = mop & MO_SIZE;
350 
351     if (size == MO_8) {
352         return mop;
353     }
354 
355     /*
356      * If size == MO_128, this is a LDXP, and the operation is single-copy
357      * atomic for each doubleword, not the entire quadword; it still must
358      * be quadword aligned.
359      */
360     if (size == MO_128) {
361         return finalize_memop_atom(s, MO_128 | MO_ALIGN,
362                                    MO_ATOM_IFALIGN_PAIR);
363     }
364     if (dc_isar_feature(aa64_lse2, s)) {
365         check_lse2_align(s, rn, 0, true, mop);
366     } else {
367         mop |= MO_ALIGN;
368     }
369     return finalize_memop(s, mop);
370 }
371 
372 /* Handle the alignment check for AccType_ORDERED instructions. */
373 static MemOp check_ordered_align(DisasContext *s, int rn, int imm,
374                                  bool is_write, MemOp mop)
375 {
376     MemOp size = mop & MO_SIZE;
377 
378     if (size == MO_8) {
379         return mop;
380     }
381     if (size == MO_128) {
382         return finalize_memop_atom(s, MO_128 | MO_ALIGN,
383                                    MO_ATOM_IFALIGN_PAIR);
384     }
385     if (!dc_isar_feature(aa64_lse2, s)) {
386         mop |= MO_ALIGN;
387     } else if (!s->naa) {
388         check_lse2_align(s, rn, imm, is_write, mop);
389     }
390     return finalize_memop(s, mop);
391 }
392 
393 typedef struct DisasCompare64 {
394     TCGCond cond;
395     TCGv_i64 value;
396 } DisasCompare64;
397 
398 static void a64_test_cc(DisasCompare64 *c64, int cc)
399 {
400     DisasCompare c32;
401 
402     arm_test_cc(&c32, cc);
403 
404     /*
405      * Sign-extend the 32-bit value so that the GE/LT comparisons work
406      * properly.  The NE/EQ comparisons are also fine with this choice.
407       */
408     c64->cond = c32.cond;
409     c64->value = tcg_temp_new_i64();
410     tcg_gen_ext_i32_i64(c64->value, c32.value);
411 }
412 
413 static void gen_rebuild_hflags(DisasContext *s)
414 {
415     gen_helper_rebuild_hflags_a64(cpu_env, tcg_constant_i32(s->current_el));
416 }
417 
418 static void gen_exception_internal(int excp)
419 {
420     assert(excp_is_internal(excp));
421     gen_helper_exception_internal(cpu_env, tcg_constant_i32(excp));
422 }
423 
424 static void gen_exception_internal_insn(DisasContext *s, int excp)
425 {
426     gen_a64_update_pc(s, 0);
427     gen_exception_internal(excp);
428     s->base.is_jmp = DISAS_NORETURN;
429 }
430 
431 static void gen_exception_bkpt_insn(DisasContext *s, uint32_t syndrome)
432 {
433     gen_a64_update_pc(s, 0);
434     gen_helper_exception_bkpt_insn(cpu_env, tcg_constant_i32(syndrome));
435     s->base.is_jmp = DISAS_NORETURN;
436 }
437 
438 static void gen_step_complete_exception(DisasContext *s)
439 {
440     /* We just completed step of an insn. Move from Active-not-pending
441      * to Active-pending, and then also take the swstep exception.
442      * This corresponds to making the (IMPDEF) choice to prioritize
443      * swstep exceptions over asynchronous exceptions taken to an exception
444      * level where debug is disabled. This choice has the advantage that
445      * we do not need to maintain internal state corresponding to the
446      * ISV/EX syndrome bits between completion of the step and generation
447      * of the exception, and our syndrome information is always correct.
448      */
449     gen_ss_advance(s);
450     gen_swstep_exception(s, 1, s->is_ldex);
451     s->base.is_jmp = DISAS_NORETURN;
452 }
453 
454 static inline bool use_goto_tb(DisasContext *s, uint64_t dest)
455 {
456     if (s->ss_active) {
457         return false;
458     }
459     return translator_use_goto_tb(&s->base, dest);
460 }
461 
462 static void gen_goto_tb(DisasContext *s, int n, int64_t diff)
463 {
464     if (use_goto_tb(s, s->pc_curr + diff)) {
465         /*
466          * For pcrel, the pc must always be up-to-date on entry to
467          * the linked TB, so that it can use simple additions for all
468          * further adjustments.  For !pcrel, the linked TB is compiled
469          * to know its full virtual address, so we can delay the
470          * update to pc to the unlinked path.  A long chain of links
471          * can thus avoid many updates to the PC.
472          */
473         if (tb_cflags(s->base.tb) & CF_PCREL) {
474             gen_a64_update_pc(s, diff);
475             tcg_gen_goto_tb(n);
476         } else {
477             tcg_gen_goto_tb(n);
478             gen_a64_update_pc(s, diff);
479         }
480         tcg_gen_exit_tb(s->base.tb, n);
481         s->base.is_jmp = DISAS_NORETURN;
482     } else {
483         gen_a64_update_pc(s, diff);
484         if (s->ss_active) {
485             gen_step_complete_exception(s);
486         } else {
487             tcg_gen_lookup_and_goto_ptr();
488             s->base.is_jmp = DISAS_NORETURN;
489         }
490     }
491 }
492 
493 /*
494  * Register access functions
495  *
496  * These functions are used for directly accessing a register in where
497  * changes to the final register value are likely to be made. If you
498  * need to use a register for temporary calculation (e.g. index type
499  * operations) use the read_* form.
500  *
501  * B1.2.1 Register mappings
502  *
503  * In instruction register encoding 31 can refer to ZR (zero register) or
504  * the SP (stack pointer) depending on context. In QEMU's case we map SP
505  * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
506  * This is the point of the _sp forms.
507  */
508 TCGv_i64 cpu_reg(DisasContext *s, int reg)
509 {
510     if (reg == 31) {
511         TCGv_i64 t = tcg_temp_new_i64();
512         tcg_gen_movi_i64(t, 0);
513         return t;
514     } else {
515         return cpu_X[reg];
516     }
517 }
518 
519 /* register access for when 31 == SP */
520 TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
521 {
522     return cpu_X[reg];
523 }
524 
525 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
526  * representing the register contents. This TCGv is an auto-freed
527  * temporary so it need not be explicitly freed, and may be modified.
528  */
529 TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
530 {
531     TCGv_i64 v = tcg_temp_new_i64();
532     if (reg != 31) {
533         if (sf) {
534             tcg_gen_mov_i64(v, cpu_X[reg]);
535         } else {
536             tcg_gen_ext32u_i64(v, cpu_X[reg]);
537         }
538     } else {
539         tcg_gen_movi_i64(v, 0);
540     }
541     return v;
542 }
543 
544 TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
545 {
546     TCGv_i64 v = tcg_temp_new_i64();
547     if (sf) {
548         tcg_gen_mov_i64(v, cpu_X[reg]);
549     } else {
550         tcg_gen_ext32u_i64(v, cpu_X[reg]);
551     }
552     return v;
553 }
554 
555 /* Return the offset into CPUARMState of a slice (from
556  * the least significant end) of FP register Qn (ie
557  * Dn, Sn, Hn or Bn).
558  * (Note that this is not the same mapping as for A32; see cpu.h)
559  */
560 static inline int fp_reg_offset(DisasContext *s, int regno, MemOp size)
561 {
562     return vec_reg_offset(s, regno, 0, size);
563 }
564 
565 /* Offset of the high half of the 128 bit vector Qn */
566 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
567 {
568     return vec_reg_offset(s, regno, 1, MO_64);
569 }
570 
571 /* Convenience accessors for reading and writing single and double
572  * FP registers. Writing clears the upper parts of the associated
573  * 128 bit vector register, as required by the architecture.
574  * Note that unlike the GP register accessors, the values returned
575  * by the read functions must be manually freed.
576  */
577 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
578 {
579     TCGv_i64 v = tcg_temp_new_i64();
580 
581     tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
582     return v;
583 }
584 
585 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
586 {
587     TCGv_i32 v = tcg_temp_new_i32();
588 
589     tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
590     return v;
591 }
592 
593 static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
594 {
595     TCGv_i32 v = tcg_temp_new_i32();
596 
597     tcg_gen_ld16u_i32(v, cpu_env, fp_reg_offset(s, reg, MO_16));
598     return v;
599 }
600 
601 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
602  * If SVE is not enabled, then there are only 128 bits in the vector.
603  */
604 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
605 {
606     unsigned ofs = fp_reg_offset(s, rd, MO_64);
607     unsigned vsz = vec_full_reg_size(s);
608 
609     /* Nop move, with side effect of clearing the tail. */
610     tcg_gen_gvec_mov(MO_64, ofs, ofs, is_q ? 16 : 8, vsz);
611 }
612 
613 void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
614 {
615     unsigned ofs = fp_reg_offset(s, reg, MO_64);
616 
617     tcg_gen_st_i64(v, cpu_env, ofs);
618     clear_vec_high(s, false, reg);
619 }
620 
621 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
622 {
623     TCGv_i64 tmp = tcg_temp_new_i64();
624 
625     tcg_gen_extu_i32_i64(tmp, v);
626     write_fp_dreg(s, reg, tmp);
627 }
628 
629 /* Expand a 2-operand AdvSIMD vector operation using an expander function.  */
630 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
631                          GVecGen2Fn *gvec_fn, int vece)
632 {
633     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
634             is_q ? 16 : 8, vec_full_reg_size(s));
635 }
636 
637 /* Expand a 2-operand + immediate AdvSIMD vector operation using
638  * an expander function.
639  */
640 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
641                           int64_t imm, GVecGen2iFn *gvec_fn, int vece)
642 {
643     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
644             imm, is_q ? 16 : 8, vec_full_reg_size(s));
645 }
646 
647 /* Expand a 3-operand AdvSIMD vector operation using an expander function.  */
648 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
649                          GVecGen3Fn *gvec_fn, int vece)
650 {
651     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
652             vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
653 }
654 
655 /* Expand a 4-operand AdvSIMD vector operation using an expander function.  */
656 static void gen_gvec_fn4(DisasContext *s, bool is_q, int rd, int rn, int rm,
657                          int rx, GVecGen4Fn *gvec_fn, int vece)
658 {
659     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
660             vec_full_reg_offset(s, rm), vec_full_reg_offset(s, rx),
661             is_q ? 16 : 8, vec_full_reg_size(s));
662 }
663 
664 /* Expand a 2-operand operation using an out-of-line helper.  */
665 static void gen_gvec_op2_ool(DisasContext *s, bool is_q, int rd,
666                              int rn, int data, gen_helper_gvec_2 *fn)
667 {
668     tcg_gen_gvec_2_ool(vec_full_reg_offset(s, rd),
669                        vec_full_reg_offset(s, rn),
670                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
671 }
672 
673 /* Expand a 3-operand operation using an out-of-line helper.  */
674 static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
675                              int rn, int rm, int data, gen_helper_gvec_3 *fn)
676 {
677     tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
678                        vec_full_reg_offset(s, rn),
679                        vec_full_reg_offset(s, rm),
680                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
681 }
682 
683 /* Expand a 3-operand + fpstatus pointer + simd data value operation using
684  * an out-of-line helper.
685  */
686 static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
687                               int rm, bool is_fp16, int data,
688                               gen_helper_gvec_3_ptr *fn)
689 {
690     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
691     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
692                        vec_full_reg_offset(s, rn),
693                        vec_full_reg_offset(s, rm), fpst,
694                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
695 }
696 
697 /* Expand a 3-operand + qc + operation using an out-of-line helper.  */
698 static void gen_gvec_op3_qc(DisasContext *s, bool is_q, int rd, int rn,
699                             int rm, gen_helper_gvec_3_ptr *fn)
700 {
701     TCGv_ptr qc_ptr = tcg_temp_new_ptr();
702 
703     tcg_gen_addi_ptr(qc_ptr, cpu_env, offsetof(CPUARMState, vfp.qc));
704     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
705                        vec_full_reg_offset(s, rn),
706                        vec_full_reg_offset(s, rm), qc_ptr,
707                        is_q ? 16 : 8, vec_full_reg_size(s), 0, fn);
708 }
709 
710 /* Expand a 4-operand operation using an out-of-line helper.  */
711 static void gen_gvec_op4_ool(DisasContext *s, bool is_q, int rd, int rn,
712                              int rm, int ra, int data, gen_helper_gvec_4 *fn)
713 {
714     tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
715                        vec_full_reg_offset(s, rn),
716                        vec_full_reg_offset(s, rm),
717                        vec_full_reg_offset(s, ra),
718                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
719 }
720 
721 /*
722  * Expand a 4-operand + fpstatus pointer + simd data value operation using
723  * an out-of-line helper.
724  */
725 static void gen_gvec_op4_fpst(DisasContext *s, bool is_q, int rd, int rn,
726                               int rm, int ra, bool is_fp16, int data,
727                               gen_helper_gvec_4_ptr *fn)
728 {
729     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
730     tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
731                        vec_full_reg_offset(s, rn),
732                        vec_full_reg_offset(s, rm),
733                        vec_full_reg_offset(s, ra), fpst,
734                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
735 }
736 
737 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
738  * than the 32 bit equivalent.
739  */
740 static inline void gen_set_NZ64(TCGv_i64 result)
741 {
742     tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
743     tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
744 }
745 
746 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
747 static inline void gen_logic_CC(int sf, TCGv_i64 result)
748 {
749     if (sf) {
750         gen_set_NZ64(result);
751     } else {
752         tcg_gen_extrl_i64_i32(cpu_ZF, result);
753         tcg_gen_mov_i32(cpu_NF, cpu_ZF);
754     }
755     tcg_gen_movi_i32(cpu_CF, 0);
756     tcg_gen_movi_i32(cpu_VF, 0);
757 }
758 
759 /* dest = T0 + T1; compute C, N, V and Z flags */
760 static void gen_add64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
761 {
762     TCGv_i64 result, flag, tmp;
763     result = tcg_temp_new_i64();
764     flag = tcg_temp_new_i64();
765     tmp = tcg_temp_new_i64();
766 
767     tcg_gen_movi_i64(tmp, 0);
768     tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
769 
770     tcg_gen_extrl_i64_i32(cpu_CF, flag);
771 
772     gen_set_NZ64(result);
773 
774     tcg_gen_xor_i64(flag, result, t0);
775     tcg_gen_xor_i64(tmp, t0, t1);
776     tcg_gen_andc_i64(flag, flag, tmp);
777     tcg_gen_extrh_i64_i32(cpu_VF, flag);
778 
779     tcg_gen_mov_i64(dest, result);
780 }
781 
782 static void gen_add32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
783 {
784     TCGv_i32 t0_32 = tcg_temp_new_i32();
785     TCGv_i32 t1_32 = tcg_temp_new_i32();
786     TCGv_i32 tmp = tcg_temp_new_i32();
787 
788     tcg_gen_movi_i32(tmp, 0);
789     tcg_gen_extrl_i64_i32(t0_32, t0);
790     tcg_gen_extrl_i64_i32(t1_32, t1);
791     tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
792     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
793     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
794     tcg_gen_xor_i32(tmp, t0_32, t1_32);
795     tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
796     tcg_gen_extu_i32_i64(dest, cpu_NF);
797 }
798 
799 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
800 {
801     if (sf) {
802         gen_add64_CC(dest, t0, t1);
803     } else {
804         gen_add32_CC(dest, t0, t1);
805     }
806 }
807 
808 /* dest = T0 - T1; compute C, N, V and Z flags */
809 static void gen_sub64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
810 {
811     /* 64 bit arithmetic */
812     TCGv_i64 result, flag, tmp;
813 
814     result = tcg_temp_new_i64();
815     flag = tcg_temp_new_i64();
816     tcg_gen_sub_i64(result, t0, t1);
817 
818     gen_set_NZ64(result);
819 
820     tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
821     tcg_gen_extrl_i64_i32(cpu_CF, flag);
822 
823     tcg_gen_xor_i64(flag, result, t0);
824     tmp = tcg_temp_new_i64();
825     tcg_gen_xor_i64(tmp, t0, t1);
826     tcg_gen_and_i64(flag, flag, tmp);
827     tcg_gen_extrh_i64_i32(cpu_VF, flag);
828     tcg_gen_mov_i64(dest, result);
829 }
830 
831 static void gen_sub32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
832 {
833     /* 32 bit arithmetic */
834     TCGv_i32 t0_32 = tcg_temp_new_i32();
835     TCGv_i32 t1_32 = tcg_temp_new_i32();
836     TCGv_i32 tmp;
837 
838     tcg_gen_extrl_i64_i32(t0_32, t0);
839     tcg_gen_extrl_i64_i32(t1_32, t1);
840     tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
841     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
842     tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
843     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
844     tmp = tcg_temp_new_i32();
845     tcg_gen_xor_i32(tmp, t0_32, t1_32);
846     tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
847     tcg_gen_extu_i32_i64(dest, cpu_NF);
848 }
849 
850 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
851 {
852     if (sf) {
853         gen_sub64_CC(dest, t0, t1);
854     } else {
855         gen_sub32_CC(dest, t0, t1);
856     }
857 }
858 
859 /* dest = T0 + T1 + CF; do not compute flags. */
860 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
861 {
862     TCGv_i64 flag = tcg_temp_new_i64();
863     tcg_gen_extu_i32_i64(flag, cpu_CF);
864     tcg_gen_add_i64(dest, t0, t1);
865     tcg_gen_add_i64(dest, dest, flag);
866 
867     if (!sf) {
868         tcg_gen_ext32u_i64(dest, dest);
869     }
870 }
871 
872 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
873 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
874 {
875     if (sf) {
876         TCGv_i64 result = tcg_temp_new_i64();
877         TCGv_i64 cf_64 = tcg_temp_new_i64();
878         TCGv_i64 vf_64 = tcg_temp_new_i64();
879         TCGv_i64 tmp = tcg_temp_new_i64();
880         TCGv_i64 zero = tcg_constant_i64(0);
881 
882         tcg_gen_extu_i32_i64(cf_64, cpu_CF);
883         tcg_gen_add2_i64(result, cf_64, t0, zero, cf_64, zero);
884         tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, zero);
885         tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
886         gen_set_NZ64(result);
887 
888         tcg_gen_xor_i64(vf_64, result, t0);
889         tcg_gen_xor_i64(tmp, t0, t1);
890         tcg_gen_andc_i64(vf_64, vf_64, tmp);
891         tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
892 
893         tcg_gen_mov_i64(dest, result);
894     } else {
895         TCGv_i32 t0_32 = tcg_temp_new_i32();
896         TCGv_i32 t1_32 = tcg_temp_new_i32();
897         TCGv_i32 tmp = tcg_temp_new_i32();
898         TCGv_i32 zero = tcg_constant_i32(0);
899 
900         tcg_gen_extrl_i64_i32(t0_32, t0);
901         tcg_gen_extrl_i64_i32(t1_32, t1);
902         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, zero, cpu_CF, zero);
903         tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, zero);
904 
905         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
906         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
907         tcg_gen_xor_i32(tmp, t0_32, t1_32);
908         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
909         tcg_gen_extu_i32_i64(dest, cpu_NF);
910     }
911 }
912 
913 /*
914  * Load/Store generators
915  */
916 
917 /*
918  * Store from GPR register to memory.
919  */
920 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
921                              TCGv_i64 tcg_addr, MemOp memop, int memidx,
922                              bool iss_valid,
923                              unsigned int iss_srt,
924                              bool iss_sf, bool iss_ar)
925 {
926     tcg_gen_qemu_st_i64(source, tcg_addr, memidx, memop);
927 
928     if (iss_valid) {
929         uint32_t syn;
930 
931         syn = syn_data_abort_with_iss(0,
932                                       (memop & MO_SIZE),
933                                       false,
934                                       iss_srt,
935                                       iss_sf,
936                                       iss_ar,
937                                       0, 0, 0, 0, 0, false);
938         disas_set_insn_syndrome(s, syn);
939     }
940 }
941 
942 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
943                       TCGv_i64 tcg_addr, MemOp memop,
944                       bool iss_valid,
945                       unsigned int iss_srt,
946                       bool iss_sf, bool iss_ar)
947 {
948     do_gpr_st_memidx(s, source, tcg_addr, memop, get_mem_index(s),
949                      iss_valid, iss_srt, iss_sf, iss_ar);
950 }
951 
952 /*
953  * Load from memory to GPR register
954  */
955 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
956                              MemOp memop, bool extend, int memidx,
957                              bool iss_valid, unsigned int iss_srt,
958                              bool iss_sf, bool iss_ar)
959 {
960     tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
961 
962     if (extend && (memop & MO_SIGN)) {
963         g_assert((memop & MO_SIZE) <= MO_32);
964         tcg_gen_ext32u_i64(dest, dest);
965     }
966 
967     if (iss_valid) {
968         uint32_t syn;
969 
970         syn = syn_data_abort_with_iss(0,
971                                       (memop & MO_SIZE),
972                                       (memop & MO_SIGN) != 0,
973                                       iss_srt,
974                                       iss_sf,
975                                       iss_ar,
976                                       0, 0, 0, 0, 0, false);
977         disas_set_insn_syndrome(s, syn);
978     }
979 }
980 
981 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
982                       MemOp memop, bool extend,
983                       bool iss_valid, unsigned int iss_srt,
984                       bool iss_sf, bool iss_ar)
985 {
986     do_gpr_ld_memidx(s, dest, tcg_addr, memop, extend, get_mem_index(s),
987                      iss_valid, iss_srt, iss_sf, iss_ar);
988 }
989 
990 /*
991  * Store from FP register to memory
992  */
993 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, MemOp mop)
994 {
995     /* This writes the bottom N bits of a 128 bit wide vector to memory */
996     TCGv_i64 tmplo = tcg_temp_new_i64();
997 
998     tcg_gen_ld_i64(tmplo, cpu_env, fp_reg_offset(s, srcidx, MO_64));
999 
1000     if ((mop & MO_SIZE) < MO_128) {
1001         tcg_gen_qemu_st_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1002     } else {
1003         TCGv_i64 tmphi = tcg_temp_new_i64();
1004         TCGv_i128 t16 = tcg_temp_new_i128();
1005 
1006         tcg_gen_ld_i64(tmphi, cpu_env, fp_reg_hi_offset(s, srcidx));
1007         tcg_gen_concat_i64_i128(t16, tmplo, tmphi);
1008 
1009         tcg_gen_qemu_st_i128(t16, tcg_addr, get_mem_index(s), mop);
1010     }
1011 }
1012 
1013 /*
1014  * Load from memory to FP register
1015  */
1016 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, MemOp mop)
1017 {
1018     /* This always zero-extends and writes to a full 128 bit wide vector */
1019     TCGv_i64 tmplo = tcg_temp_new_i64();
1020     TCGv_i64 tmphi = NULL;
1021 
1022     if ((mop & MO_SIZE) < MO_128) {
1023         tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1024     } else {
1025         TCGv_i128 t16 = tcg_temp_new_i128();
1026 
1027         tcg_gen_qemu_ld_i128(t16, tcg_addr, get_mem_index(s), mop);
1028 
1029         tmphi = tcg_temp_new_i64();
1030         tcg_gen_extr_i128_i64(tmplo, tmphi, t16);
1031     }
1032 
1033     tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
1034 
1035     if (tmphi) {
1036         tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
1037     }
1038     clear_vec_high(s, tmphi != NULL, destidx);
1039 }
1040 
1041 /*
1042  * Vector load/store helpers.
1043  *
1044  * The principal difference between this and a FP load is that we don't
1045  * zero extend as we are filling a partial chunk of the vector register.
1046  * These functions don't support 128 bit loads/stores, which would be
1047  * normal load/store operations.
1048  *
1049  * The _i32 versions are useful when operating on 32 bit quantities
1050  * (eg for floating point single or using Neon helper functions).
1051  */
1052 
1053 /* Get value of an element within a vector register */
1054 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
1055                              int element, MemOp memop)
1056 {
1057     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1058     switch ((unsigned)memop) {
1059     case MO_8:
1060         tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
1061         break;
1062     case MO_16:
1063         tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
1064         break;
1065     case MO_32:
1066         tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
1067         break;
1068     case MO_8|MO_SIGN:
1069         tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
1070         break;
1071     case MO_16|MO_SIGN:
1072         tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
1073         break;
1074     case MO_32|MO_SIGN:
1075         tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
1076         break;
1077     case MO_64:
1078     case MO_64|MO_SIGN:
1079         tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
1080         break;
1081     default:
1082         g_assert_not_reached();
1083     }
1084 }
1085 
1086 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1087                                  int element, MemOp memop)
1088 {
1089     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1090     switch (memop) {
1091     case MO_8:
1092         tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1093         break;
1094     case MO_16:
1095         tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1096         break;
1097     case MO_8|MO_SIGN:
1098         tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1099         break;
1100     case MO_16|MO_SIGN:
1101         tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1102         break;
1103     case MO_32:
1104     case MO_32|MO_SIGN:
1105         tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1106         break;
1107     default:
1108         g_assert_not_reached();
1109     }
1110 }
1111 
1112 /* Set value of an element within a vector register */
1113 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1114                               int element, MemOp memop)
1115 {
1116     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1117     switch (memop) {
1118     case MO_8:
1119         tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1120         break;
1121     case MO_16:
1122         tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1123         break;
1124     case MO_32:
1125         tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1126         break;
1127     case MO_64:
1128         tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1129         break;
1130     default:
1131         g_assert_not_reached();
1132     }
1133 }
1134 
1135 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1136                                   int destidx, int element, MemOp memop)
1137 {
1138     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1139     switch (memop) {
1140     case MO_8:
1141         tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1142         break;
1143     case MO_16:
1144         tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1145         break;
1146     case MO_32:
1147         tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1148         break;
1149     default:
1150         g_assert_not_reached();
1151     }
1152 }
1153 
1154 /* Store from vector register to memory */
1155 static void do_vec_st(DisasContext *s, int srcidx, int element,
1156                       TCGv_i64 tcg_addr, MemOp mop)
1157 {
1158     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1159 
1160     read_vec_element(s, tcg_tmp, srcidx, element, mop & MO_SIZE);
1161     tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1162 }
1163 
1164 /* Load from memory to vector register */
1165 static void do_vec_ld(DisasContext *s, int destidx, int element,
1166                       TCGv_i64 tcg_addr, MemOp mop)
1167 {
1168     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1169 
1170     tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1171     write_vec_element(s, tcg_tmp, destidx, element, mop & MO_SIZE);
1172 }
1173 
1174 /* Check that FP/Neon access is enabled. If it is, return
1175  * true. If not, emit code to generate an appropriate exception,
1176  * and return false; the caller should not emit any code for
1177  * the instruction. Note that this check must happen after all
1178  * unallocated-encoding checks (otherwise the syndrome information
1179  * for the resulting exception will be incorrect).
1180  */
1181 static bool fp_access_check_only(DisasContext *s)
1182 {
1183     if (s->fp_excp_el) {
1184         assert(!s->fp_access_checked);
1185         s->fp_access_checked = true;
1186 
1187         gen_exception_insn_el(s, 0, EXCP_UDEF,
1188                               syn_fp_access_trap(1, 0xe, false, 0),
1189                               s->fp_excp_el);
1190         return false;
1191     }
1192     s->fp_access_checked = true;
1193     return true;
1194 }
1195 
1196 static bool fp_access_check(DisasContext *s)
1197 {
1198     if (!fp_access_check_only(s)) {
1199         return false;
1200     }
1201     if (s->sme_trap_nonstreaming && s->is_nonstreaming) {
1202         gen_exception_insn(s, 0, EXCP_UDEF,
1203                            syn_smetrap(SME_ET_Streaming, false));
1204         return false;
1205     }
1206     return true;
1207 }
1208 
1209 /*
1210  * Check that SVE access is enabled.  If it is, return true.
1211  * If not, emit code to generate an appropriate exception and return false.
1212  * This function corresponds to CheckSVEEnabled().
1213  */
1214 bool sve_access_check(DisasContext *s)
1215 {
1216     if (s->pstate_sm || !dc_isar_feature(aa64_sve, s)) {
1217         assert(dc_isar_feature(aa64_sme, s));
1218         if (!sme_sm_enabled_check(s)) {
1219             goto fail_exit;
1220         }
1221     } else if (s->sve_excp_el) {
1222         gen_exception_insn_el(s, 0, EXCP_UDEF,
1223                               syn_sve_access_trap(), s->sve_excp_el);
1224         goto fail_exit;
1225     }
1226     s->sve_access_checked = true;
1227     return fp_access_check(s);
1228 
1229  fail_exit:
1230     /* Assert that we only raise one exception per instruction. */
1231     assert(!s->sve_access_checked);
1232     s->sve_access_checked = true;
1233     return false;
1234 }
1235 
1236 /*
1237  * Check that SME access is enabled, raise an exception if not.
1238  * Note that this function corresponds to CheckSMEAccess and is
1239  * only used directly for cpregs.
1240  */
1241 static bool sme_access_check(DisasContext *s)
1242 {
1243     if (s->sme_excp_el) {
1244         gen_exception_insn_el(s, 0, EXCP_UDEF,
1245                               syn_smetrap(SME_ET_AccessTrap, false),
1246                               s->sme_excp_el);
1247         return false;
1248     }
1249     return true;
1250 }
1251 
1252 /* This function corresponds to CheckSMEEnabled. */
1253 bool sme_enabled_check(DisasContext *s)
1254 {
1255     /*
1256      * Note that unlike sve_excp_el, we have not constrained sme_excp_el
1257      * to be zero when fp_excp_el has priority.  This is because we need
1258      * sme_excp_el by itself for cpregs access checks.
1259      */
1260     if (!s->fp_excp_el || s->sme_excp_el < s->fp_excp_el) {
1261         s->fp_access_checked = true;
1262         return sme_access_check(s);
1263     }
1264     return fp_access_check_only(s);
1265 }
1266 
1267 /* Common subroutine for CheckSMEAnd*Enabled. */
1268 bool sme_enabled_check_with_svcr(DisasContext *s, unsigned req)
1269 {
1270     if (!sme_enabled_check(s)) {
1271         return false;
1272     }
1273     if (FIELD_EX64(req, SVCR, SM) && !s->pstate_sm) {
1274         gen_exception_insn(s, 0, EXCP_UDEF,
1275                            syn_smetrap(SME_ET_NotStreaming, false));
1276         return false;
1277     }
1278     if (FIELD_EX64(req, SVCR, ZA) && !s->pstate_za) {
1279         gen_exception_insn(s, 0, EXCP_UDEF,
1280                            syn_smetrap(SME_ET_InactiveZA, false));
1281         return false;
1282     }
1283     return true;
1284 }
1285 
1286 /*
1287  * This utility function is for doing register extension with an
1288  * optional shift. You will likely want to pass a temporary for the
1289  * destination register. See DecodeRegExtend() in the ARM ARM.
1290  */
1291 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1292                               int option, unsigned int shift)
1293 {
1294     int extsize = extract32(option, 0, 2);
1295     bool is_signed = extract32(option, 2, 1);
1296 
1297     if (is_signed) {
1298         switch (extsize) {
1299         case 0:
1300             tcg_gen_ext8s_i64(tcg_out, tcg_in);
1301             break;
1302         case 1:
1303             tcg_gen_ext16s_i64(tcg_out, tcg_in);
1304             break;
1305         case 2:
1306             tcg_gen_ext32s_i64(tcg_out, tcg_in);
1307             break;
1308         case 3:
1309             tcg_gen_mov_i64(tcg_out, tcg_in);
1310             break;
1311         }
1312     } else {
1313         switch (extsize) {
1314         case 0:
1315             tcg_gen_ext8u_i64(tcg_out, tcg_in);
1316             break;
1317         case 1:
1318             tcg_gen_ext16u_i64(tcg_out, tcg_in);
1319             break;
1320         case 2:
1321             tcg_gen_ext32u_i64(tcg_out, tcg_in);
1322             break;
1323         case 3:
1324             tcg_gen_mov_i64(tcg_out, tcg_in);
1325             break;
1326         }
1327     }
1328 
1329     if (shift) {
1330         tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1331     }
1332 }
1333 
1334 static inline void gen_check_sp_alignment(DisasContext *s)
1335 {
1336     /* The AArch64 architecture mandates that (if enabled via PSTATE
1337      * or SCTLR bits) there is a check that SP is 16-aligned on every
1338      * SP-relative load or store (with an exception generated if it is not).
1339      * In line with general QEMU practice regarding misaligned accesses,
1340      * we omit these checks for the sake of guest program performance.
1341      * This function is provided as a hook so we can more easily add these
1342      * checks in future (possibly as a "favour catching guest program bugs
1343      * over speed" user selectable option).
1344      */
1345 }
1346 
1347 /*
1348  * This provides a simple table based table lookup decoder. It is
1349  * intended to be used when the relevant bits for decode are too
1350  * awkwardly placed and switch/if based logic would be confusing and
1351  * deeply nested. Since it's a linear search through the table, tables
1352  * should be kept small.
1353  *
1354  * It returns the first handler where insn & mask == pattern, or
1355  * NULL if there is no match.
1356  * The table is terminated by an empty mask (i.e. 0)
1357  */
1358 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1359                                                uint32_t insn)
1360 {
1361     const AArch64DecodeTable *tptr = table;
1362 
1363     while (tptr->mask) {
1364         if ((insn & tptr->mask) == tptr->pattern) {
1365             return tptr->disas_fn;
1366         }
1367         tptr++;
1368     }
1369     return NULL;
1370 }
1371 
1372 /*
1373  * The instruction disassembly implemented here matches
1374  * the instruction encoding classifications in chapter C4
1375  * of the ARM Architecture Reference Manual (DDI0487B_a);
1376  * classification names and decode diagrams here should generally
1377  * match up with those in the manual.
1378  */
1379 
1380 static bool trans_B(DisasContext *s, arg_i *a)
1381 {
1382     reset_btype(s);
1383     gen_goto_tb(s, 0, a->imm);
1384     return true;
1385 }
1386 
1387 static bool trans_BL(DisasContext *s, arg_i *a)
1388 {
1389     gen_pc_plus_diff(s, cpu_reg(s, 30), curr_insn_len(s));
1390     reset_btype(s);
1391     gen_goto_tb(s, 0, a->imm);
1392     return true;
1393 }
1394 
1395 
1396 static bool trans_CBZ(DisasContext *s, arg_cbz *a)
1397 {
1398     DisasLabel match;
1399     TCGv_i64 tcg_cmp;
1400 
1401     tcg_cmp = read_cpu_reg(s, a->rt, a->sf);
1402     reset_btype(s);
1403 
1404     match = gen_disas_label(s);
1405     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1406                         tcg_cmp, 0, match.label);
1407     gen_goto_tb(s, 0, 4);
1408     set_disas_label(s, match);
1409     gen_goto_tb(s, 1, a->imm);
1410     return true;
1411 }
1412 
1413 static bool trans_TBZ(DisasContext *s, arg_tbz *a)
1414 {
1415     DisasLabel match;
1416     TCGv_i64 tcg_cmp;
1417 
1418     tcg_cmp = tcg_temp_new_i64();
1419     tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, a->rt), 1ULL << a->bitpos);
1420 
1421     reset_btype(s);
1422 
1423     match = gen_disas_label(s);
1424     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1425                         tcg_cmp, 0, match.label);
1426     gen_goto_tb(s, 0, 4);
1427     set_disas_label(s, match);
1428     gen_goto_tb(s, 1, a->imm);
1429     return true;
1430 }
1431 
1432 static bool trans_B_cond(DisasContext *s, arg_B_cond *a)
1433 {
1434     reset_btype(s);
1435     if (a->cond < 0x0e) {
1436         /* genuinely conditional branches */
1437         DisasLabel match = gen_disas_label(s);
1438         arm_gen_test_cc(a->cond, match.label);
1439         gen_goto_tb(s, 0, 4);
1440         set_disas_label(s, match);
1441         gen_goto_tb(s, 1, a->imm);
1442     } else {
1443         /* 0xe and 0xf are both "always" conditions */
1444         gen_goto_tb(s, 0, a->imm);
1445     }
1446     return true;
1447 }
1448 
1449 static void set_btype_for_br(DisasContext *s, int rn)
1450 {
1451     if (dc_isar_feature(aa64_bti, s)) {
1452         /* BR to {x16,x17} or !guard -> 1, else 3.  */
1453         set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
1454     }
1455 }
1456 
1457 static void set_btype_for_blr(DisasContext *s)
1458 {
1459     if (dc_isar_feature(aa64_bti, s)) {
1460         /* BLR sets BTYPE to 2, regardless of source guarded page.  */
1461         set_btype(s, 2);
1462     }
1463 }
1464 
1465 static bool trans_BR(DisasContext *s, arg_r *a)
1466 {
1467     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1468     set_btype_for_br(s, a->rn);
1469     s->base.is_jmp = DISAS_JUMP;
1470     return true;
1471 }
1472 
1473 static bool trans_BLR(DisasContext *s, arg_r *a)
1474 {
1475     TCGv_i64 dst = cpu_reg(s, a->rn);
1476     TCGv_i64 lr = cpu_reg(s, 30);
1477     if (dst == lr) {
1478         TCGv_i64 tmp = tcg_temp_new_i64();
1479         tcg_gen_mov_i64(tmp, dst);
1480         dst = tmp;
1481     }
1482     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1483     gen_a64_set_pc(s, dst);
1484     set_btype_for_blr(s);
1485     s->base.is_jmp = DISAS_JUMP;
1486     return true;
1487 }
1488 
1489 static bool trans_RET(DisasContext *s, arg_r *a)
1490 {
1491     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1492     s->base.is_jmp = DISAS_JUMP;
1493     return true;
1494 }
1495 
1496 static TCGv_i64 auth_branch_target(DisasContext *s, TCGv_i64 dst,
1497                                    TCGv_i64 modifier, bool use_key_a)
1498 {
1499     TCGv_i64 truedst;
1500     /*
1501      * Return the branch target for a BRAA/RETA/etc, which is either
1502      * just the destination dst, or that value with the pauth check
1503      * done and the code removed from the high bits.
1504      */
1505     if (!s->pauth_active) {
1506         return dst;
1507     }
1508 
1509     truedst = tcg_temp_new_i64();
1510     if (use_key_a) {
1511         gen_helper_autia(truedst, cpu_env, dst, modifier);
1512     } else {
1513         gen_helper_autib(truedst, cpu_env, dst, modifier);
1514     }
1515     return truedst;
1516 }
1517 
1518 static bool trans_BRAZ(DisasContext *s, arg_braz *a)
1519 {
1520     TCGv_i64 dst;
1521 
1522     if (!dc_isar_feature(aa64_pauth, s)) {
1523         return false;
1524     }
1525 
1526     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1527     gen_a64_set_pc(s, dst);
1528     set_btype_for_br(s, a->rn);
1529     s->base.is_jmp = DISAS_JUMP;
1530     return true;
1531 }
1532 
1533 static bool trans_BLRAZ(DisasContext *s, arg_braz *a)
1534 {
1535     TCGv_i64 dst, lr;
1536 
1537     if (!dc_isar_feature(aa64_pauth, s)) {
1538         return false;
1539     }
1540 
1541     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1542     lr = cpu_reg(s, 30);
1543     if (dst == lr) {
1544         TCGv_i64 tmp = tcg_temp_new_i64();
1545         tcg_gen_mov_i64(tmp, dst);
1546         dst = tmp;
1547     }
1548     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1549     gen_a64_set_pc(s, dst);
1550     set_btype_for_blr(s);
1551     s->base.is_jmp = DISAS_JUMP;
1552     return true;
1553 }
1554 
1555 static bool trans_RETA(DisasContext *s, arg_reta *a)
1556 {
1557     TCGv_i64 dst;
1558 
1559     dst = auth_branch_target(s, cpu_reg(s, 30), cpu_X[31], !a->m);
1560     gen_a64_set_pc(s, dst);
1561     s->base.is_jmp = DISAS_JUMP;
1562     return true;
1563 }
1564 
1565 static bool trans_BRA(DisasContext *s, arg_bra *a)
1566 {
1567     TCGv_i64 dst;
1568 
1569     if (!dc_isar_feature(aa64_pauth, s)) {
1570         return false;
1571     }
1572     dst = auth_branch_target(s, cpu_reg(s,a->rn), cpu_reg_sp(s, a->rm), !a->m);
1573     gen_a64_set_pc(s, dst);
1574     set_btype_for_br(s, a->rn);
1575     s->base.is_jmp = DISAS_JUMP;
1576     return true;
1577 }
1578 
1579 static bool trans_BLRA(DisasContext *s, arg_bra *a)
1580 {
1581     TCGv_i64 dst, lr;
1582 
1583     if (!dc_isar_feature(aa64_pauth, s)) {
1584         return false;
1585     }
1586     dst = auth_branch_target(s, cpu_reg(s, a->rn), cpu_reg_sp(s, a->rm), !a->m);
1587     lr = cpu_reg(s, 30);
1588     if (dst == lr) {
1589         TCGv_i64 tmp = tcg_temp_new_i64();
1590         tcg_gen_mov_i64(tmp, dst);
1591         dst = tmp;
1592     }
1593     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1594     gen_a64_set_pc(s, dst);
1595     set_btype_for_blr(s);
1596     s->base.is_jmp = DISAS_JUMP;
1597     return true;
1598 }
1599 
1600 static bool trans_ERET(DisasContext *s, arg_ERET *a)
1601 {
1602     TCGv_i64 dst;
1603 
1604     if (s->current_el == 0) {
1605         return false;
1606     }
1607     if (s->fgt_eret) {
1608         gen_exception_insn_el(s, 0, EXCP_UDEF, 0, 2);
1609         return true;
1610     }
1611     dst = tcg_temp_new_i64();
1612     tcg_gen_ld_i64(dst, cpu_env,
1613                    offsetof(CPUARMState, elr_el[s->current_el]));
1614 
1615     translator_io_start(&s->base);
1616 
1617     gen_helper_exception_return(cpu_env, dst);
1618     /* Must exit loop to check un-masked IRQs */
1619     s->base.is_jmp = DISAS_EXIT;
1620     return true;
1621 }
1622 
1623 static bool trans_ERETA(DisasContext *s, arg_reta *a)
1624 {
1625     TCGv_i64 dst;
1626 
1627     if (!dc_isar_feature(aa64_pauth, s)) {
1628         return false;
1629     }
1630     if (s->current_el == 0) {
1631         return false;
1632     }
1633     /* The FGT trap takes precedence over an auth trap. */
1634     if (s->fgt_eret) {
1635         gen_exception_insn_el(s, 0, EXCP_UDEF, a->m ? 3 : 2, 2);
1636         return true;
1637     }
1638     dst = tcg_temp_new_i64();
1639     tcg_gen_ld_i64(dst, cpu_env,
1640                    offsetof(CPUARMState, elr_el[s->current_el]));
1641 
1642     dst = auth_branch_target(s, dst, cpu_X[31], !a->m);
1643 
1644     translator_io_start(&s->base);
1645 
1646     gen_helper_exception_return(cpu_env, dst);
1647     /* Must exit loop to check un-masked IRQs */
1648     s->base.is_jmp = DISAS_EXIT;
1649     return true;
1650 }
1651 
1652 static bool trans_NOP(DisasContext *s, arg_NOP *a)
1653 {
1654     return true;
1655 }
1656 
1657 static bool trans_YIELD(DisasContext *s, arg_YIELD *a)
1658 {
1659     /*
1660      * When running in MTTCG we don't generate jumps to the yield and
1661      * WFE helpers as it won't affect the scheduling of other vCPUs.
1662      * If we wanted to more completely model WFE/SEV so we don't busy
1663      * spin unnecessarily we would need to do something more involved.
1664      */
1665     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1666         s->base.is_jmp = DISAS_YIELD;
1667     }
1668     return true;
1669 }
1670 
1671 static bool trans_WFI(DisasContext *s, arg_WFI *a)
1672 {
1673     s->base.is_jmp = DISAS_WFI;
1674     return true;
1675 }
1676 
1677 static bool trans_WFE(DisasContext *s, arg_WFI *a)
1678 {
1679     /*
1680      * When running in MTTCG we don't generate jumps to the yield and
1681      * WFE helpers as it won't affect the scheduling of other vCPUs.
1682      * If we wanted to more completely model WFE/SEV so we don't busy
1683      * spin unnecessarily we would need to do something more involved.
1684      */
1685     if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1686         s->base.is_jmp = DISAS_WFE;
1687     }
1688     return true;
1689 }
1690 
1691 static bool trans_XPACLRI(DisasContext *s, arg_XPACLRI *a)
1692 {
1693     if (s->pauth_active) {
1694         gen_helper_xpaci(cpu_X[30], cpu_env, cpu_X[30]);
1695     }
1696     return true;
1697 }
1698 
1699 static bool trans_PACIA1716(DisasContext *s, arg_PACIA1716 *a)
1700 {
1701     if (s->pauth_active) {
1702         gen_helper_pacia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1703     }
1704     return true;
1705 }
1706 
1707 static bool trans_PACIB1716(DisasContext *s, arg_PACIB1716 *a)
1708 {
1709     if (s->pauth_active) {
1710         gen_helper_pacib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1711     }
1712     return true;
1713 }
1714 
1715 static bool trans_AUTIA1716(DisasContext *s, arg_AUTIA1716 *a)
1716 {
1717     if (s->pauth_active) {
1718         gen_helper_autia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1719     }
1720     return true;
1721 }
1722 
1723 static bool trans_AUTIB1716(DisasContext *s, arg_AUTIB1716 *a)
1724 {
1725     if (s->pauth_active) {
1726         gen_helper_autib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1727     }
1728     return true;
1729 }
1730 
1731 static bool trans_ESB(DisasContext *s, arg_ESB *a)
1732 {
1733     /* Without RAS, we must implement this as NOP. */
1734     if (dc_isar_feature(aa64_ras, s)) {
1735         /*
1736          * QEMU does not have a source of physical SErrors,
1737          * so we are only concerned with virtual SErrors.
1738          * The pseudocode in the ARM for this case is
1739          *   if PSTATE.EL IN {EL0, EL1} && EL2Enabled() then
1740          *      AArch64.vESBOperation();
1741          * Most of the condition can be evaluated at translation time.
1742          * Test for EL2 present, and defer test for SEL2 to runtime.
1743          */
1744         if (s->current_el <= 1 && arm_dc_feature(s, ARM_FEATURE_EL2)) {
1745             gen_helper_vesb(cpu_env);
1746         }
1747     }
1748     return true;
1749 }
1750 
1751 static bool trans_PACIAZ(DisasContext *s, arg_PACIAZ *a)
1752 {
1753     if (s->pauth_active) {
1754         gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], tcg_constant_i64(0));
1755     }
1756     return true;
1757 }
1758 
1759 static bool trans_PACIASP(DisasContext *s, arg_PACIASP *a)
1760 {
1761     if (s->pauth_active) {
1762         gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1763     }
1764     return true;
1765 }
1766 
1767 static bool trans_PACIBZ(DisasContext *s, arg_PACIBZ *a)
1768 {
1769     if (s->pauth_active) {
1770         gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], tcg_constant_i64(0));
1771     }
1772     return true;
1773 }
1774 
1775 static bool trans_PACIBSP(DisasContext *s, arg_PACIBSP *a)
1776 {
1777     if (s->pauth_active) {
1778         gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1779     }
1780     return true;
1781 }
1782 
1783 static bool trans_AUTIAZ(DisasContext *s, arg_AUTIAZ *a)
1784 {
1785     if (s->pauth_active) {
1786         gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], tcg_constant_i64(0));
1787     }
1788     return true;
1789 }
1790 
1791 static bool trans_AUTIASP(DisasContext *s, arg_AUTIASP *a)
1792 {
1793     if (s->pauth_active) {
1794         gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1795     }
1796     return true;
1797 }
1798 
1799 static bool trans_AUTIBZ(DisasContext *s, arg_AUTIBZ *a)
1800 {
1801     if (s->pauth_active) {
1802         gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], tcg_constant_i64(0));
1803     }
1804     return true;
1805 }
1806 
1807 static bool trans_AUTIBSP(DisasContext *s, arg_AUTIBSP *a)
1808 {
1809     if (s->pauth_active) {
1810         gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1811     }
1812     return true;
1813 }
1814 
1815 static bool trans_CLREX(DisasContext *s, arg_CLREX *a)
1816 {
1817     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1818     return true;
1819 }
1820 
1821 static bool trans_DSB_DMB(DisasContext *s, arg_DSB_DMB *a)
1822 {
1823     /* We handle DSB and DMB the same way */
1824     TCGBar bar;
1825 
1826     switch (a->types) {
1827     case 1: /* MBReqTypes_Reads */
1828         bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1829         break;
1830     case 2: /* MBReqTypes_Writes */
1831         bar = TCG_BAR_SC | TCG_MO_ST_ST;
1832         break;
1833     default: /* MBReqTypes_All */
1834         bar = TCG_BAR_SC | TCG_MO_ALL;
1835         break;
1836     }
1837     tcg_gen_mb(bar);
1838     return true;
1839 }
1840 
1841 static bool trans_ISB(DisasContext *s, arg_ISB *a)
1842 {
1843     /*
1844      * We need to break the TB after this insn to execute
1845      * self-modifying code correctly and also to take
1846      * any pending interrupts immediately.
1847      */
1848     reset_btype(s);
1849     gen_goto_tb(s, 0, 4);
1850     return true;
1851 }
1852 
1853 static bool trans_SB(DisasContext *s, arg_SB *a)
1854 {
1855     if (!dc_isar_feature(aa64_sb, s)) {
1856         return false;
1857     }
1858     /*
1859      * TODO: There is no speculation barrier opcode for TCG;
1860      * MB and end the TB instead.
1861      */
1862     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1863     gen_goto_tb(s, 0, 4);
1864     return true;
1865 }
1866 
1867 static void gen_xaflag(void)
1868 {
1869     TCGv_i32 z = tcg_temp_new_i32();
1870 
1871     tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1872 
1873     /*
1874      * (!C & !Z) << 31
1875      * (!(C | Z)) << 31
1876      * ~((C | Z) << 31)
1877      * ~-(C | Z)
1878      * (C | Z) - 1
1879      */
1880     tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1881     tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1882 
1883     /* !(Z & C) */
1884     tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1885     tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1886 
1887     /* (!C & Z) << 31 -> -(Z & ~C) */
1888     tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1889     tcg_gen_neg_i32(cpu_VF, cpu_VF);
1890 
1891     /* C | Z */
1892     tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1893 }
1894 
1895 static void gen_axflag(void)
1896 {
1897     tcg_gen_sari_i32(cpu_VF, cpu_VF, 31);         /* V ? -1 : 0 */
1898     tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF);     /* C & !V */
1899 
1900     /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1901     tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1902 
1903     tcg_gen_movi_i32(cpu_NF, 0);
1904     tcg_gen_movi_i32(cpu_VF, 0);
1905 }
1906 
1907 /* MSR (immediate) - move immediate to processor state field */
1908 static void handle_msr_i(DisasContext *s, uint32_t insn,
1909                          unsigned int op1, unsigned int op2, unsigned int crm)
1910 {
1911     int op = op1 << 3 | op2;
1912 
1913     /* End the TB by default, chaining is ok.  */
1914     s->base.is_jmp = DISAS_TOO_MANY;
1915 
1916     switch (op) {
1917     case 0x00: /* CFINV */
1918         if (crm != 0 || !dc_isar_feature(aa64_condm_4, s)) {
1919             goto do_unallocated;
1920         }
1921         tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1922         s->base.is_jmp = DISAS_NEXT;
1923         break;
1924 
1925     case 0x01: /* XAFlag */
1926         if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1927             goto do_unallocated;
1928         }
1929         gen_xaflag();
1930         s->base.is_jmp = DISAS_NEXT;
1931         break;
1932 
1933     case 0x02: /* AXFlag */
1934         if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1935             goto do_unallocated;
1936         }
1937         gen_axflag();
1938         s->base.is_jmp = DISAS_NEXT;
1939         break;
1940 
1941     case 0x03: /* UAO */
1942         if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1943             goto do_unallocated;
1944         }
1945         if (crm & 1) {
1946             set_pstate_bits(PSTATE_UAO);
1947         } else {
1948             clear_pstate_bits(PSTATE_UAO);
1949         }
1950         gen_rebuild_hflags(s);
1951         break;
1952 
1953     case 0x04: /* PAN */
1954         if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1955             goto do_unallocated;
1956         }
1957         if (crm & 1) {
1958             set_pstate_bits(PSTATE_PAN);
1959         } else {
1960             clear_pstate_bits(PSTATE_PAN);
1961         }
1962         gen_rebuild_hflags(s);
1963         break;
1964 
1965     case 0x05: /* SPSel */
1966         if (s->current_el == 0) {
1967             goto do_unallocated;
1968         }
1969         gen_helper_msr_i_spsel(cpu_env, tcg_constant_i32(crm & PSTATE_SP));
1970         break;
1971 
1972     case 0x19: /* SSBS */
1973         if (!dc_isar_feature(aa64_ssbs, s)) {
1974             goto do_unallocated;
1975         }
1976         if (crm & 1) {
1977             set_pstate_bits(PSTATE_SSBS);
1978         } else {
1979             clear_pstate_bits(PSTATE_SSBS);
1980         }
1981         /* Don't need to rebuild hflags since SSBS is a nop */
1982         break;
1983 
1984     case 0x1a: /* DIT */
1985         if (!dc_isar_feature(aa64_dit, s)) {
1986             goto do_unallocated;
1987         }
1988         if (crm & 1) {
1989             set_pstate_bits(PSTATE_DIT);
1990         } else {
1991             clear_pstate_bits(PSTATE_DIT);
1992         }
1993         /* There's no need to rebuild hflags because DIT is a nop */
1994         break;
1995 
1996     case 0x1e: /* DAIFSet */
1997         gen_helper_msr_i_daifset(cpu_env, tcg_constant_i32(crm));
1998         break;
1999 
2000     case 0x1f: /* DAIFClear */
2001         gen_helper_msr_i_daifclear(cpu_env, tcg_constant_i32(crm));
2002         /* For DAIFClear, exit the cpu loop to re-evaluate pending IRQs.  */
2003         s->base.is_jmp = DISAS_UPDATE_EXIT;
2004         break;
2005 
2006     case 0x1c: /* TCO */
2007         if (dc_isar_feature(aa64_mte, s)) {
2008             /* Full MTE is enabled -- set the TCO bit as directed. */
2009             if (crm & 1) {
2010                 set_pstate_bits(PSTATE_TCO);
2011             } else {
2012                 clear_pstate_bits(PSTATE_TCO);
2013             }
2014             gen_rebuild_hflags(s);
2015             /* Many factors, including TCO, go into MTE_ACTIVE. */
2016             s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
2017         } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
2018             /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI.  */
2019             s->base.is_jmp = DISAS_NEXT;
2020         } else {
2021             goto do_unallocated;
2022         }
2023         break;
2024 
2025     case 0x1b: /* SVCR* */
2026         if (!dc_isar_feature(aa64_sme, s) || crm < 2 || crm > 7) {
2027             goto do_unallocated;
2028         }
2029         if (sme_access_check(s)) {
2030             int old = s->pstate_sm | (s->pstate_za << 1);
2031             int new = (crm & 1) * 3;
2032             int msk = (crm >> 1) & 3;
2033 
2034             if ((old ^ new) & msk) {
2035                 /* At least one bit changes. */
2036                 gen_helper_set_svcr(cpu_env, tcg_constant_i32(new),
2037                                     tcg_constant_i32(msk));
2038             } else {
2039                 s->base.is_jmp = DISAS_NEXT;
2040             }
2041         }
2042         break;
2043 
2044     default:
2045     do_unallocated:
2046         unallocated_encoding(s);
2047         return;
2048     }
2049 }
2050 
2051 static void gen_get_nzcv(TCGv_i64 tcg_rt)
2052 {
2053     TCGv_i32 tmp = tcg_temp_new_i32();
2054     TCGv_i32 nzcv = tcg_temp_new_i32();
2055 
2056     /* build bit 31, N */
2057     tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
2058     /* build bit 30, Z */
2059     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
2060     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
2061     /* build bit 29, C */
2062     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
2063     /* build bit 28, V */
2064     tcg_gen_shri_i32(tmp, cpu_VF, 31);
2065     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
2066     /* generate result */
2067     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
2068 }
2069 
2070 static void gen_set_nzcv(TCGv_i64 tcg_rt)
2071 {
2072     TCGv_i32 nzcv = tcg_temp_new_i32();
2073 
2074     /* take NZCV from R[t] */
2075     tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
2076 
2077     /* bit 31, N */
2078     tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
2079     /* bit 30, Z */
2080     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
2081     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
2082     /* bit 29, C */
2083     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
2084     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
2085     /* bit 28, V */
2086     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
2087     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
2088 }
2089 
2090 static void gen_sysreg_undef(DisasContext *s, bool isread,
2091                              uint8_t op0, uint8_t op1, uint8_t op2,
2092                              uint8_t crn, uint8_t crm, uint8_t rt)
2093 {
2094     /*
2095      * Generate code to emit an UNDEF with correct syndrome
2096      * information for a failed system register access.
2097      * This is EC_UNCATEGORIZED (ie a standard UNDEF) in most cases,
2098      * but if FEAT_IDST is implemented then read accesses to registers
2099      * in the feature ID space are reported with the EC_SYSTEMREGISTERTRAP
2100      * syndrome.
2101      */
2102     uint32_t syndrome;
2103 
2104     if (isread && dc_isar_feature(aa64_ids, s) &&
2105         arm_cpreg_encoding_in_idspace(op0, op1, op2, crn, crm)) {
2106         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2107     } else {
2108         syndrome = syn_uncategorized();
2109     }
2110     gen_exception_insn(s, 0, EXCP_UDEF, syndrome);
2111 }
2112 
2113 /* MRS - move from system register
2114  * MSR (register) - move to system register
2115  * SYS
2116  * SYSL
2117  * These are all essentially the same insn in 'read' and 'write'
2118  * versions, with varying op0 fields.
2119  */
2120 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
2121                        unsigned int op0, unsigned int op1, unsigned int op2,
2122                        unsigned int crn, unsigned int crm, unsigned int rt)
2123 {
2124     uint32_t key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
2125                                       crn, crm, op0, op1, op2);
2126     const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
2127     bool need_exit_tb = false;
2128     TCGv_ptr tcg_ri = NULL;
2129     TCGv_i64 tcg_rt;
2130 
2131     if (!ri) {
2132         /* Unknown register; this might be a guest error or a QEMU
2133          * unimplemented feature.
2134          */
2135         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
2136                       "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
2137                       isread ? "read" : "write", op0, op1, crn, crm, op2);
2138         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2139         return;
2140     }
2141 
2142     /* Check access permissions */
2143     if (!cp_access_ok(s->current_el, ri, isread)) {
2144         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2145         return;
2146     }
2147 
2148     if (ri->accessfn || (ri->fgt && s->fgt_active)) {
2149         /* Emit code to perform further access permissions checks at
2150          * runtime; this may result in an exception.
2151          */
2152         uint32_t syndrome;
2153 
2154         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2155         gen_a64_update_pc(s, 0);
2156         tcg_ri = tcg_temp_new_ptr();
2157         gen_helper_access_check_cp_reg(tcg_ri, cpu_env,
2158                                        tcg_constant_i32(key),
2159                                        tcg_constant_i32(syndrome),
2160                                        tcg_constant_i32(isread));
2161     } else if (ri->type & ARM_CP_RAISES_EXC) {
2162         /*
2163          * The readfn or writefn might raise an exception;
2164          * synchronize the CPU state in case it does.
2165          */
2166         gen_a64_update_pc(s, 0);
2167     }
2168 
2169     /* Handle special cases first */
2170     switch (ri->type & ARM_CP_SPECIAL_MASK) {
2171     case 0:
2172         break;
2173     case ARM_CP_NOP:
2174         return;
2175     case ARM_CP_NZCV:
2176         tcg_rt = cpu_reg(s, rt);
2177         if (isread) {
2178             gen_get_nzcv(tcg_rt);
2179         } else {
2180             gen_set_nzcv(tcg_rt);
2181         }
2182         return;
2183     case ARM_CP_CURRENTEL:
2184         /* Reads as current EL value from pstate, which is
2185          * guaranteed to be constant by the tb flags.
2186          */
2187         tcg_rt = cpu_reg(s, rt);
2188         tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
2189         return;
2190     case ARM_CP_DC_ZVA:
2191         /* Writes clear the aligned block of memory which rt points into. */
2192         if (s->mte_active[0]) {
2193             int desc = 0;
2194 
2195             desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
2196             desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
2197             desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
2198 
2199             tcg_rt = tcg_temp_new_i64();
2200             gen_helper_mte_check_zva(tcg_rt, cpu_env,
2201                                      tcg_constant_i32(desc), cpu_reg(s, rt));
2202         } else {
2203             tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
2204         }
2205         gen_helper_dc_zva(cpu_env, tcg_rt);
2206         return;
2207     case ARM_CP_DC_GVA:
2208         {
2209             TCGv_i64 clean_addr, tag;
2210 
2211             /*
2212              * DC_GVA, like DC_ZVA, requires that we supply the original
2213              * pointer for an invalid page.  Probe that address first.
2214              */
2215             tcg_rt = cpu_reg(s, rt);
2216             clean_addr = clean_data_tbi(s, tcg_rt);
2217             gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
2218 
2219             if (s->ata) {
2220                 /* Extract the tag from the register to match STZGM.  */
2221                 tag = tcg_temp_new_i64();
2222                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2223                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2224             }
2225         }
2226         return;
2227     case ARM_CP_DC_GZVA:
2228         {
2229             TCGv_i64 clean_addr, tag;
2230 
2231             /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
2232             tcg_rt = cpu_reg(s, rt);
2233             clean_addr = clean_data_tbi(s, tcg_rt);
2234             gen_helper_dc_zva(cpu_env, clean_addr);
2235 
2236             if (s->ata) {
2237                 /* Extract the tag from the register to match STZGM.  */
2238                 tag = tcg_temp_new_i64();
2239                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2240                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2241             }
2242         }
2243         return;
2244     default:
2245         g_assert_not_reached();
2246     }
2247     if ((ri->type & ARM_CP_FPU) && !fp_access_check_only(s)) {
2248         return;
2249     } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
2250         return;
2251     } else if ((ri->type & ARM_CP_SME) && !sme_access_check(s)) {
2252         return;
2253     }
2254 
2255     if (ri->type & ARM_CP_IO) {
2256         /* I/O operations must end the TB here (whether read or write) */
2257         need_exit_tb = translator_io_start(&s->base);
2258     }
2259 
2260     tcg_rt = cpu_reg(s, rt);
2261 
2262     if (isread) {
2263         if (ri->type & ARM_CP_CONST) {
2264             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
2265         } else if (ri->readfn) {
2266             if (!tcg_ri) {
2267                 tcg_ri = gen_lookup_cp_reg(key);
2268             }
2269             gen_helper_get_cp_reg64(tcg_rt, cpu_env, tcg_ri);
2270         } else {
2271             tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
2272         }
2273     } else {
2274         if (ri->type & ARM_CP_CONST) {
2275             /* If not forbidden by access permissions, treat as WI */
2276             return;
2277         } else if (ri->writefn) {
2278             if (!tcg_ri) {
2279                 tcg_ri = gen_lookup_cp_reg(key);
2280             }
2281             gen_helper_set_cp_reg64(cpu_env, tcg_ri, tcg_rt);
2282         } else {
2283             tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
2284         }
2285     }
2286 
2287     if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
2288         /*
2289          * A write to any coprocessor regiser that ends a TB
2290          * must rebuild the hflags for the next TB.
2291          */
2292         gen_rebuild_hflags(s);
2293         /*
2294          * We default to ending the TB on a coprocessor register write,
2295          * but allow this to be suppressed by the register definition
2296          * (usually only necessary to work around guest bugs).
2297          */
2298         need_exit_tb = true;
2299     }
2300     if (need_exit_tb) {
2301         s->base.is_jmp = DISAS_UPDATE_EXIT;
2302     }
2303 }
2304 
2305 /* System
2306  *  31                 22 21  20 19 18 16 15   12 11    8 7   5 4    0
2307  * +---------------------+---+-----+-----+-------+-------+-----+------+
2308  * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 |  CRn  |  CRm  | op2 |  Rt  |
2309  * +---------------------+---+-----+-----+-------+-------+-----+------+
2310  */
2311 static void disas_system(DisasContext *s, uint32_t insn)
2312 {
2313     unsigned int l, op0, op1, crn, crm, op2, rt;
2314     l = extract32(insn, 21, 1);
2315     op0 = extract32(insn, 19, 2);
2316     op1 = extract32(insn, 16, 3);
2317     crn = extract32(insn, 12, 4);
2318     crm = extract32(insn, 8, 4);
2319     op2 = extract32(insn, 5, 3);
2320     rt = extract32(insn, 0, 5);
2321 
2322     if (op0 == 0) {
2323         if (l || rt != 31) {
2324             unallocated_encoding(s);
2325             return;
2326         }
2327         switch (crn) {
2328         case 4: /* MSR (immediate) */
2329             handle_msr_i(s, insn, op1, op2, crm);
2330             break;
2331         default:
2332             unallocated_encoding(s);
2333             break;
2334         }
2335         return;
2336     }
2337     handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
2338 }
2339 
2340 /* Exception generation
2341  *
2342  *  31             24 23 21 20                     5 4   2 1  0
2343  * +-----------------+-----+------------------------+-----+----+
2344  * | 1 1 0 1 0 1 0 0 | opc |          imm16         | op2 | LL |
2345  * +-----------------------+------------------------+----------+
2346  */
2347 static void disas_exc(DisasContext *s, uint32_t insn)
2348 {
2349     int opc = extract32(insn, 21, 3);
2350     int op2_ll = extract32(insn, 0, 5);
2351     int imm16 = extract32(insn, 5, 16);
2352     uint32_t syndrome;
2353 
2354     switch (opc) {
2355     case 0:
2356         /* For SVC, HVC and SMC we advance the single-step state
2357          * machine before taking the exception. This is architecturally
2358          * mandated, to ensure that single-stepping a system call
2359          * instruction works properly.
2360          */
2361         switch (op2_ll) {
2362         case 1:                                                     /* SVC */
2363             syndrome = syn_aa64_svc(imm16);
2364             if (s->fgt_svc) {
2365                 gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2366                 break;
2367             }
2368             gen_ss_advance(s);
2369             gen_exception_insn(s, 4, EXCP_SWI, syndrome);
2370             break;
2371         case 2:                                                     /* HVC */
2372             if (s->current_el == 0) {
2373                 unallocated_encoding(s);
2374                 break;
2375             }
2376             /* The pre HVC helper handles cases when HVC gets trapped
2377              * as an undefined insn by runtime configuration.
2378              */
2379             gen_a64_update_pc(s, 0);
2380             gen_helper_pre_hvc(cpu_env);
2381             gen_ss_advance(s);
2382             gen_exception_insn_el(s, 4, EXCP_HVC, syn_aa64_hvc(imm16), 2);
2383             break;
2384         case 3:                                                     /* SMC */
2385             if (s->current_el == 0) {
2386                 unallocated_encoding(s);
2387                 break;
2388             }
2389             gen_a64_update_pc(s, 0);
2390             gen_helper_pre_smc(cpu_env, tcg_constant_i32(syn_aa64_smc(imm16)));
2391             gen_ss_advance(s);
2392             gen_exception_insn_el(s, 4, EXCP_SMC, syn_aa64_smc(imm16), 3);
2393             break;
2394         default:
2395             unallocated_encoding(s);
2396             break;
2397         }
2398         break;
2399     case 1:
2400         if (op2_ll != 0) {
2401             unallocated_encoding(s);
2402             break;
2403         }
2404         /* BRK */
2405         gen_exception_bkpt_insn(s, syn_aa64_bkpt(imm16));
2406         break;
2407     case 2:
2408         if (op2_ll != 0) {
2409             unallocated_encoding(s);
2410             break;
2411         }
2412         /* HLT. This has two purposes.
2413          * Architecturally, it is an external halting debug instruction.
2414          * Since QEMU doesn't implement external debug, we treat this as
2415          * it is required for halting debug disabled: it will UNDEF.
2416          * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2417          */
2418         if (semihosting_enabled(s->current_el == 0) && imm16 == 0xf000) {
2419             gen_exception_internal_insn(s, EXCP_SEMIHOST);
2420         } else {
2421             unallocated_encoding(s);
2422         }
2423         break;
2424     case 5:
2425         if (op2_ll < 1 || op2_ll > 3) {
2426             unallocated_encoding(s);
2427             break;
2428         }
2429         /* DCPS1, DCPS2, DCPS3 */
2430         unallocated_encoding(s);
2431         break;
2432     default:
2433         unallocated_encoding(s);
2434         break;
2435     }
2436 }
2437 
2438 /* Branches, exception generating and system instructions */
2439 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
2440 {
2441     switch (extract32(insn, 25, 7)) {
2442     case 0x6a: /* Exception generation / System */
2443         if (insn & (1 << 24)) {
2444             if (extract32(insn, 22, 2) == 0) {
2445                 disas_system(s, insn);
2446             } else {
2447                 unallocated_encoding(s);
2448             }
2449         } else {
2450             disas_exc(s, insn);
2451         }
2452         break;
2453     default:
2454         unallocated_encoding(s);
2455         break;
2456     }
2457 }
2458 
2459 /*
2460  * Load/Store exclusive instructions are implemented by remembering
2461  * the value/address loaded, and seeing if these are the same
2462  * when the store is performed. This is not actually the architecturally
2463  * mandated semantics, but it works for typical guest code sequences
2464  * and avoids having to monitor regular stores.
2465  *
2466  * The store exclusive uses the atomic cmpxchg primitives to avoid
2467  * races in multi-threaded linux-user and when MTTCG softmmu is
2468  * enabled.
2469  */
2470 static void gen_load_exclusive(DisasContext *s, int rt, int rt2, int rn,
2471                                int size, bool is_pair)
2472 {
2473     int idx = get_mem_index(s);
2474     TCGv_i64 dirty_addr, clean_addr;
2475     MemOp memop = check_atomic_align(s, rn, size + is_pair);
2476 
2477     s->is_ldex = true;
2478     dirty_addr = cpu_reg_sp(s, rn);
2479     clean_addr = gen_mte_check1(s, dirty_addr, false, rn != 31, memop);
2480 
2481     g_assert(size <= 3);
2482     if (is_pair) {
2483         g_assert(size >= 2);
2484         if (size == 2) {
2485             tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2486             if (s->be_data == MO_LE) {
2487                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2488                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2489             } else {
2490                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2491                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2492             }
2493         } else {
2494             TCGv_i128 t16 = tcg_temp_new_i128();
2495 
2496             tcg_gen_qemu_ld_i128(t16, clean_addr, idx, memop);
2497 
2498             if (s->be_data == MO_LE) {
2499                 tcg_gen_extr_i128_i64(cpu_exclusive_val,
2500                                       cpu_exclusive_high, t16);
2501             } else {
2502                 tcg_gen_extr_i128_i64(cpu_exclusive_high,
2503                                       cpu_exclusive_val, t16);
2504             }
2505             tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2506             tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2507         }
2508     } else {
2509         tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2510         tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2511     }
2512     tcg_gen_mov_i64(cpu_exclusive_addr, clean_addr);
2513 }
2514 
2515 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2516                                 int rn, int size, int is_pair)
2517 {
2518     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2519      *     && (!is_pair || env->exclusive_high == [addr + datasize])) {
2520      *     [addr] = {Rt};
2521      *     if (is_pair) {
2522      *         [addr + datasize] = {Rt2};
2523      *     }
2524      *     {Rd} = 0;
2525      * } else {
2526      *     {Rd} = 1;
2527      * }
2528      * env->exclusive_addr = -1;
2529      */
2530     TCGLabel *fail_label = gen_new_label();
2531     TCGLabel *done_label = gen_new_label();
2532     TCGv_i64 tmp, clean_addr;
2533     MemOp memop;
2534 
2535     /*
2536      * FIXME: We are out of spec here.  We have recorded only the address
2537      * from load_exclusive, not the entire range, and we assume that the
2538      * size of the access on both sides match.  The architecture allows the
2539      * store to be smaller than the load, so long as the stored bytes are
2540      * within the range recorded by the load.
2541      */
2542 
2543     /* See AArch64.ExclusiveMonitorsPass() and AArch64.IsExclusiveVA(). */
2544     clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2545     tcg_gen_brcond_i64(TCG_COND_NE, clean_addr, cpu_exclusive_addr, fail_label);
2546 
2547     /*
2548      * The write, and any associated faults, only happen if the virtual
2549      * and physical addresses pass the exclusive monitor check.  These
2550      * faults are exceedingly unlikely, because normally the guest uses
2551      * the exact same address register for the load_exclusive, and we
2552      * would have recognized these faults there.
2553      *
2554      * It is possible to trigger an alignment fault pre-LSE2, e.g. with an
2555      * unaligned 4-byte write within the range of an aligned 8-byte load.
2556      * With LSE2, the store would need to cross a 16-byte boundary when the
2557      * load did not, which would mean the store is outside the range
2558      * recorded for the monitor, which would have failed a corrected monitor
2559      * check above.  For now, we assume no size change and retain the
2560      * MO_ALIGN to let tcg know what we checked in the load_exclusive.
2561      *
2562      * It is possible to trigger an MTE fault, by performing the load with
2563      * a virtual address with a valid tag and performing the store with the
2564      * same virtual address and a different invalid tag.
2565      */
2566     memop = size + is_pair;
2567     if (memop == MO_128 || !dc_isar_feature(aa64_lse2, s)) {
2568         memop |= MO_ALIGN;
2569     }
2570     memop = finalize_memop(s, memop);
2571     gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2572 
2573     tmp = tcg_temp_new_i64();
2574     if (is_pair) {
2575         if (size == 2) {
2576             if (s->be_data == MO_LE) {
2577                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2578             } else {
2579                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2580             }
2581             tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2582                                        cpu_exclusive_val, tmp,
2583                                        get_mem_index(s), memop);
2584             tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2585         } else {
2586             TCGv_i128 t16 = tcg_temp_new_i128();
2587             TCGv_i128 c16 = tcg_temp_new_i128();
2588             TCGv_i64 a, b;
2589 
2590             if (s->be_data == MO_LE) {
2591                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt), cpu_reg(s, rt2));
2592                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_val,
2593                                         cpu_exclusive_high);
2594             } else {
2595                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt2), cpu_reg(s, rt));
2596                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_high,
2597                                         cpu_exclusive_val);
2598             }
2599 
2600             tcg_gen_atomic_cmpxchg_i128(t16, cpu_exclusive_addr, c16, t16,
2601                                         get_mem_index(s), memop);
2602 
2603             a = tcg_temp_new_i64();
2604             b = tcg_temp_new_i64();
2605             if (s->be_data == MO_LE) {
2606                 tcg_gen_extr_i128_i64(a, b, t16);
2607             } else {
2608                 tcg_gen_extr_i128_i64(b, a, t16);
2609             }
2610 
2611             tcg_gen_xor_i64(a, a, cpu_exclusive_val);
2612             tcg_gen_xor_i64(b, b, cpu_exclusive_high);
2613             tcg_gen_or_i64(tmp, a, b);
2614 
2615             tcg_gen_setcondi_i64(TCG_COND_NE, tmp, tmp, 0);
2616         }
2617     } else {
2618         tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2619                                    cpu_reg(s, rt), get_mem_index(s), memop);
2620         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2621     }
2622     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2623     tcg_gen_br(done_label);
2624 
2625     gen_set_label(fail_label);
2626     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2627     gen_set_label(done_label);
2628     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2629 }
2630 
2631 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2632                                  int rn, int size)
2633 {
2634     TCGv_i64 tcg_rs = cpu_reg(s, rs);
2635     TCGv_i64 tcg_rt = cpu_reg(s, rt);
2636     int memidx = get_mem_index(s);
2637     TCGv_i64 clean_addr;
2638     MemOp memop;
2639 
2640     if (rn == 31) {
2641         gen_check_sp_alignment(s);
2642     }
2643     memop = check_atomic_align(s, rn, size);
2644     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2645     tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt,
2646                                memidx, memop);
2647 }
2648 
2649 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2650                                       int rn, int size)
2651 {
2652     TCGv_i64 s1 = cpu_reg(s, rs);
2653     TCGv_i64 s2 = cpu_reg(s, rs + 1);
2654     TCGv_i64 t1 = cpu_reg(s, rt);
2655     TCGv_i64 t2 = cpu_reg(s, rt + 1);
2656     TCGv_i64 clean_addr;
2657     int memidx = get_mem_index(s);
2658     MemOp memop;
2659 
2660     if (rn == 31) {
2661         gen_check_sp_alignment(s);
2662     }
2663 
2664     /* This is a single atomic access, despite the "pair". */
2665     memop = check_atomic_align(s, rn, size + 1);
2666     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2667 
2668     if (size == 2) {
2669         TCGv_i64 cmp = tcg_temp_new_i64();
2670         TCGv_i64 val = tcg_temp_new_i64();
2671 
2672         if (s->be_data == MO_LE) {
2673             tcg_gen_concat32_i64(val, t1, t2);
2674             tcg_gen_concat32_i64(cmp, s1, s2);
2675         } else {
2676             tcg_gen_concat32_i64(val, t2, t1);
2677             tcg_gen_concat32_i64(cmp, s2, s1);
2678         }
2679 
2680         tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx, memop);
2681 
2682         if (s->be_data == MO_LE) {
2683             tcg_gen_extr32_i64(s1, s2, cmp);
2684         } else {
2685             tcg_gen_extr32_i64(s2, s1, cmp);
2686         }
2687     } else {
2688         TCGv_i128 cmp = tcg_temp_new_i128();
2689         TCGv_i128 val = tcg_temp_new_i128();
2690 
2691         if (s->be_data == MO_LE) {
2692             tcg_gen_concat_i64_i128(val, t1, t2);
2693             tcg_gen_concat_i64_i128(cmp, s1, s2);
2694         } else {
2695             tcg_gen_concat_i64_i128(val, t2, t1);
2696             tcg_gen_concat_i64_i128(cmp, s2, s1);
2697         }
2698 
2699         tcg_gen_atomic_cmpxchg_i128(cmp, clean_addr, cmp, val, memidx, memop);
2700 
2701         if (s->be_data == MO_LE) {
2702             tcg_gen_extr_i128_i64(s1, s2, cmp);
2703         } else {
2704             tcg_gen_extr_i128_i64(s2, s1, cmp);
2705         }
2706     }
2707 }
2708 
2709 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
2710  * from the ARMv8 specs for LDR (Shared decode for all encodings).
2711  */
2712 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2713 {
2714     int opc0 = extract32(opc, 0, 1);
2715     int regsize;
2716 
2717     if (is_signed) {
2718         regsize = opc0 ? 32 : 64;
2719     } else {
2720         regsize = size == 3 ? 64 : 32;
2721     }
2722     return regsize == 64;
2723 }
2724 
2725 /* Load/store exclusive
2726  *
2727  *  31 30 29         24  23  22   21  20  16  15  14   10 9    5 4    0
2728  * +-----+-------------+----+---+----+------+----+-------+------+------+
2729  * | sz  | 0 0 1 0 0 0 | o2 | L | o1 |  Rs  | o0 |  Rt2  |  Rn  | Rt   |
2730  * +-----+-------------+----+---+----+------+----+-------+------+------+
2731  *
2732  *  sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2733  *   L: 0 -> store, 1 -> load
2734  *  o2: 0 -> exclusive, 1 -> not
2735  *  o1: 0 -> single register, 1 -> register pair
2736  *  o0: 1 -> load-acquire/store-release, 0 -> not
2737  */
2738 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2739 {
2740     int rt = extract32(insn, 0, 5);
2741     int rn = extract32(insn, 5, 5);
2742     int rt2 = extract32(insn, 10, 5);
2743     int rs = extract32(insn, 16, 5);
2744     int is_lasr = extract32(insn, 15, 1);
2745     int o2_L_o1_o0 = extract32(insn, 21, 3) * 2 | is_lasr;
2746     int size = extract32(insn, 30, 2);
2747     TCGv_i64 clean_addr;
2748     MemOp memop;
2749 
2750     switch (o2_L_o1_o0) {
2751     case 0x0: /* STXR */
2752     case 0x1: /* STLXR */
2753         if (rn == 31) {
2754             gen_check_sp_alignment(s);
2755         }
2756         if (is_lasr) {
2757             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2758         }
2759         gen_store_exclusive(s, rs, rt, rt2, rn, size, false);
2760         return;
2761 
2762     case 0x4: /* LDXR */
2763     case 0x5: /* LDAXR */
2764         if (rn == 31) {
2765             gen_check_sp_alignment(s);
2766         }
2767         gen_load_exclusive(s, rt, rt2, rn, size, false);
2768         if (is_lasr) {
2769             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2770         }
2771         return;
2772 
2773     case 0x8: /* STLLR */
2774         if (!dc_isar_feature(aa64_lor, s)) {
2775             break;
2776         }
2777         /* StoreLORelease is the same as Store-Release for QEMU.  */
2778         /* fall through */
2779     case 0x9: /* STLR */
2780         /* Generate ISS for non-exclusive accesses including LASR.  */
2781         if (rn == 31) {
2782             gen_check_sp_alignment(s);
2783         }
2784         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2785         memop = check_ordered_align(s, rn, 0, true, size);
2786         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2787                                     true, rn != 31, memop);
2788         do_gpr_st(s, cpu_reg(s, rt), clean_addr, memop, true, rt,
2789                   disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2790         return;
2791 
2792     case 0xc: /* LDLAR */
2793         if (!dc_isar_feature(aa64_lor, s)) {
2794             break;
2795         }
2796         /* LoadLOAcquire is the same as Load-Acquire for QEMU.  */
2797         /* fall through */
2798     case 0xd: /* LDAR */
2799         /* Generate ISS for non-exclusive accesses including LASR.  */
2800         if (rn == 31) {
2801             gen_check_sp_alignment(s);
2802         }
2803         memop = check_ordered_align(s, rn, 0, false, size);
2804         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2805                                     false, rn != 31, memop);
2806         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, memop, false, true,
2807                   rt, disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2808         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2809         return;
2810 
2811     case 0x2: case 0x3: /* CASP / STXP */
2812         if (size & 2) { /* STXP / STLXP */
2813             if (rn == 31) {
2814                 gen_check_sp_alignment(s);
2815             }
2816             if (is_lasr) {
2817                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2818             }
2819             gen_store_exclusive(s, rs, rt, rt2, rn, size, true);
2820             return;
2821         }
2822         if (rt2 == 31
2823             && ((rt | rs) & 1) == 0
2824             && dc_isar_feature(aa64_atomics, s)) {
2825             /* CASP / CASPL */
2826             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2827             return;
2828         }
2829         break;
2830 
2831     case 0x6: case 0x7: /* CASPA / LDXP */
2832         if (size & 2) { /* LDXP / LDAXP */
2833             if (rn == 31) {
2834                 gen_check_sp_alignment(s);
2835             }
2836             gen_load_exclusive(s, rt, rt2, rn, size, true);
2837             if (is_lasr) {
2838                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2839             }
2840             return;
2841         }
2842         if (rt2 == 31
2843             && ((rt | rs) & 1) == 0
2844             && dc_isar_feature(aa64_atomics, s)) {
2845             /* CASPA / CASPAL */
2846             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2847             return;
2848         }
2849         break;
2850 
2851     case 0xa: /* CAS */
2852     case 0xb: /* CASL */
2853     case 0xe: /* CASA */
2854     case 0xf: /* CASAL */
2855         if (rt2 == 31 && dc_isar_feature(aa64_atomics, s)) {
2856             gen_compare_and_swap(s, rs, rt, rn, size);
2857             return;
2858         }
2859         break;
2860     }
2861     unallocated_encoding(s);
2862 }
2863 
2864 /*
2865  * Load register (literal)
2866  *
2867  *  31 30 29   27  26 25 24 23                5 4     0
2868  * +-----+-------+---+-----+-------------------+-------+
2869  * | opc | 0 1 1 | V | 0 0 |     imm19         |  Rt   |
2870  * +-----+-------+---+-----+-------------------+-------+
2871  *
2872  * V: 1 -> vector (simd/fp)
2873  * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2874  *                   10-> 32 bit signed, 11 -> prefetch
2875  * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2876  */
2877 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2878 {
2879     int rt = extract32(insn, 0, 5);
2880     int64_t imm = sextract32(insn, 5, 19) << 2;
2881     bool is_vector = extract32(insn, 26, 1);
2882     int opc = extract32(insn, 30, 2);
2883     bool is_signed = false;
2884     int size = 2;
2885     TCGv_i64 tcg_rt, clean_addr;
2886     MemOp memop;
2887 
2888     if (is_vector) {
2889         if (opc == 3) {
2890             unallocated_encoding(s);
2891             return;
2892         }
2893         size = 2 + opc;
2894         if (!fp_access_check(s)) {
2895             return;
2896         }
2897         memop = finalize_memop_asimd(s, size);
2898     } else {
2899         if (opc == 3) {
2900             /* PRFM (literal) : prefetch */
2901             return;
2902         }
2903         size = 2 + extract32(opc, 0, 1);
2904         is_signed = extract32(opc, 1, 1);
2905         memop = finalize_memop(s, size + is_signed * MO_SIGN);
2906     }
2907 
2908     tcg_rt = cpu_reg(s, rt);
2909 
2910     clean_addr = tcg_temp_new_i64();
2911     gen_pc_plus_diff(s, clean_addr, imm);
2912 
2913     if (is_vector) {
2914         do_fp_ld(s, rt, clean_addr, memop);
2915     } else {
2916         /* Only unsigned 32bit loads target 32bit registers.  */
2917         bool iss_sf = opc != 0;
2918         do_gpr_ld(s, tcg_rt, clean_addr, memop, false, true, rt, iss_sf, false);
2919     }
2920 }
2921 
2922 /*
2923  * LDNP (Load Pair - non-temporal hint)
2924  * LDP (Load Pair - non vector)
2925  * LDPSW (Load Pair Signed Word - non vector)
2926  * STNP (Store Pair - non-temporal hint)
2927  * STP (Store Pair - non vector)
2928  * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2929  * LDP (Load Pair of SIMD&FP)
2930  * STNP (Store Pair of SIMD&FP - non-temporal hint)
2931  * STP (Store Pair of SIMD&FP)
2932  *
2933  *  31 30 29   27  26  25 24   23  22 21   15 14   10 9    5 4    0
2934  * +-----+-------+---+---+-------+---+-----------------------------+
2935  * | opc | 1 0 1 | V | 0 | index | L |  imm7 |  Rt2  |  Rn  | Rt   |
2936  * +-----+-------+---+---+-------+---+-------+-------+------+------+
2937  *
2938  * opc: LDP/STP/LDNP/STNP        00 -> 32 bit, 10 -> 64 bit
2939  *      LDPSW/STGP               01
2940  *      LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2941  *   V: 0 -> GPR, 1 -> Vector
2942  * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2943  *      10 -> signed offset, 11 -> pre-index
2944  *   L: 0 -> Store 1 -> Load
2945  *
2946  * Rt, Rt2 = GPR or SIMD registers to be stored
2947  * Rn = general purpose register containing address
2948  * imm7 = signed offset (multiple of 4 or 8 depending on size)
2949  */
2950 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2951 {
2952     int rt = extract32(insn, 0, 5);
2953     int rn = extract32(insn, 5, 5);
2954     int rt2 = extract32(insn, 10, 5);
2955     uint64_t offset = sextract64(insn, 15, 7);
2956     int index = extract32(insn, 23, 2);
2957     bool is_vector = extract32(insn, 26, 1);
2958     bool is_load = extract32(insn, 22, 1);
2959     int opc = extract32(insn, 30, 2);
2960     bool is_signed = false;
2961     bool postindex = false;
2962     bool wback = false;
2963     bool set_tag = false;
2964     TCGv_i64 clean_addr, dirty_addr;
2965     MemOp mop;
2966     int size;
2967 
2968     if (opc == 3) {
2969         unallocated_encoding(s);
2970         return;
2971     }
2972 
2973     if (is_vector) {
2974         size = 2 + opc;
2975     } else if (opc == 1 && !is_load) {
2976         /* STGP */
2977         if (!dc_isar_feature(aa64_mte_insn_reg, s) || index == 0) {
2978             unallocated_encoding(s);
2979             return;
2980         }
2981         size = 3;
2982         set_tag = true;
2983     } else {
2984         size = 2 + extract32(opc, 1, 1);
2985         is_signed = extract32(opc, 0, 1);
2986         if (!is_load && is_signed) {
2987             unallocated_encoding(s);
2988             return;
2989         }
2990     }
2991 
2992     switch (index) {
2993     case 1: /* post-index */
2994         postindex = true;
2995         wback = true;
2996         break;
2997     case 0:
2998         /* signed offset with "non-temporal" hint. Since we don't emulate
2999          * caches we don't care about hints to the cache system about
3000          * data access patterns, and handle this identically to plain
3001          * signed offset.
3002          */
3003         if (is_signed) {
3004             /* There is no non-temporal-hint version of LDPSW */
3005             unallocated_encoding(s);
3006             return;
3007         }
3008         postindex = false;
3009         break;
3010     case 2: /* signed offset, rn not updated */
3011         postindex = false;
3012         break;
3013     case 3: /* pre-index */
3014         postindex = false;
3015         wback = true;
3016         break;
3017     }
3018 
3019     if (is_vector && !fp_access_check(s)) {
3020         return;
3021     }
3022 
3023     offset <<= (set_tag ? LOG2_TAG_GRANULE : size);
3024 
3025     if (rn == 31) {
3026         gen_check_sp_alignment(s);
3027     }
3028 
3029     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3030     if (!postindex) {
3031         tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3032     }
3033 
3034     if (set_tag) {
3035         if (!s->ata) {
3036             /*
3037              * TODO: We could rely on the stores below, at least for
3038              * system mode, if we arrange to add MO_ALIGN_16.
3039              */
3040             gen_helper_stg_stub(cpu_env, dirty_addr);
3041         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
3042             gen_helper_stg_parallel(cpu_env, dirty_addr, dirty_addr);
3043         } else {
3044             gen_helper_stg(cpu_env, dirty_addr, dirty_addr);
3045         }
3046     }
3047 
3048     if (is_vector) {
3049         mop = finalize_memop_asimd(s, size);
3050     } else {
3051         mop = finalize_memop(s, size);
3052     }
3053     clean_addr = gen_mte_checkN(s, dirty_addr, !is_load,
3054                                 (wback || rn != 31) && !set_tag,
3055                                 2 << size, mop);
3056 
3057     if (is_vector) {
3058         /* LSE2 does not merge FP pairs; leave these as separate operations. */
3059         if (is_load) {
3060             do_fp_ld(s, rt, clean_addr, mop);
3061         } else {
3062             do_fp_st(s, rt, clean_addr, mop);
3063         }
3064         tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
3065         if (is_load) {
3066             do_fp_ld(s, rt2, clean_addr, mop);
3067         } else {
3068             do_fp_st(s, rt2, clean_addr, mop);
3069         }
3070     } else {
3071         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3072         TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
3073 
3074         /*
3075          * We built mop above for the single logical access -- rebuild it
3076          * now for the paired operation.
3077          *
3078          * With LSE2, non-sign-extending pairs are treated atomically if
3079          * aligned, and if unaligned one of the pair will be completely
3080          * within a 16-byte block and that element will be atomic.
3081          * Otherwise each element is separately atomic.
3082          * In all cases, issue one operation with the correct atomicity.
3083          *
3084          * This treats sign-extending loads like zero-extending loads,
3085          * since that reuses the most code below.
3086          */
3087         mop = size + 1;
3088         if (s->align_mem) {
3089             mop |= (size == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
3090         }
3091         mop = finalize_memop_pair(s, mop);
3092 
3093         if (is_load) {
3094             if (size == 2) {
3095                 int o2 = s->be_data == MO_LE ? 32 : 0;
3096                 int o1 = o2 ^ 32;
3097 
3098                 tcg_gen_qemu_ld_i64(tcg_rt, clean_addr, get_mem_index(s), mop);
3099                 if (is_signed) {
3100                     tcg_gen_sextract_i64(tcg_rt2, tcg_rt, o2, 32);
3101                     tcg_gen_sextract_i64(tcg_rt, tcg_rt, o1, 32);
3102                 } else {
3103                     tcg_gen_extract_i64(tcg_rt2, tcg_rt, o2, 32);
3104                     tcg_gen_extract_i64(tcg_rt, tcg_rt, o1, 32);
3105                 }
3106             } else {
3107                 TCGv_i128 tmp = tcg_temp_new_i128();
3108 
3109                 tcg_gen_qemu_ld_i128(tmp, clean_addr, get_mem_index(s), mop);
3110                 if (s->be_data == MO_LE) {
3111                     tcg_gen_extr_i128_i64(tcg_rt, tcg_rt2, tmp);
3112                 } else {
3113                     tcg_gen_extr_i128_i64(tcg_rt2, tcg_rt, tmp);
3114                 }
3115             }
3116         } else {
3117             if (size == 2) {
3118                 TCGv_i64 tmp = tcg_temp_new_i64();
3119 
3120                 if (s->be_data == MO_LE) {
3121                     tcg_gen_concat32_i64(tmp, tcg_rt, tcg_rt2);
3122                 } else {
3123                     tcg_gen_concat32_i64(tmp, tcg_rt2, tcg_rt);
3124                 }
3125                 tcg_gen_qemu_st_i64(tmp, clean_addr, get_mem_index(s), mop);
3126             } else {
3127                 TCGv_i128 tmp = tcg_temp_new_i128();
3128 
3129                 if (s->be_data == MO_LE) {
3130                     tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
3131                 } else {
3132                     tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
3133                 }
3134                 tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
3135             }
3136         }
3137     }
3138 
3139     if (wback) {
3140         if (postindex) {
3141             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3142         }
3143         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3144     }
3145 }
3146 
3147 /*
3148  * Load/store (immediate post-indexed)
3149  * Load/store (immediate pre-indexed)
3150  * Load/store (unscaled immediate)
3151  *
3152  * 31 30 29   27  26 25 24 23 22 21  20    12 11 10 9    5 4    0
3153  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3154  * |size| 1 1 1 | V | 0 0 | opc | 0 |  imm9  | idx |  Rn  |  Rt  |
3155  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3156  *
3157  * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
3158          10 -> unprivileged
3159  * V = 0 -> non-vector
3160  * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
3161  * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3162  */
3163 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
3164                                 int opc,
3165                                 int size,
3166                                 int rt,
3167                                 bool is_vector)
3168 {
3169     int rn = extract32(insn, 5, 5);
3170     int imm9 = sextract32(insn, 12, 9);
3171     int idx = extract32(insn, 10, 2);
3172     bool is_signed = false;
3173     bool is_store = false;
3174     bool is_extended = false;
3175     bool is_unpriv = (idx == 2);
3176     bool iss_valid;
3177     bool post_index;
3178     bool writeback;
3179     int memidx;
3180     MemOp memop;
3181     TCGv_i64 clean_addr, dirty_addr;
3182 
3183     if (is_vector) {
3184         size |= (opc & 2) << 1;
3185         if (size > 4 || is_unpriv) {
3186             unallocated_encoding(s);
3187             return;
3188         }
3189         is_store = ((opc & 1) == 0);
3190         if (!fp_access_check(s)) {
3191             return;
3192         }
3193         memop = finalize_memop_asimd(s, size);
3194     } else {
3195         if (size == 3 && opc == 2) {
3196             /* PRFM - prefetch */
3197             if (idx != 0) {
3198                 unallocated_encoding(s);
3199                 return;
3200             }
3201             return;
3202         }
3203         if (opc == 3 && size > 1) {
3204             unallocated_encoding(s);
3205             return;
3206         }
3207         is_store = (opc == 0);
3208         is_signed = !is_store && extract32(opc, 1, 1);
3209         is_extended = (size < 3) && extract32(opc, 0, 1);
3210         memop = finalize_memop(s, size + is_signed * MO_SIGN);
3211     }
3212 
3213     switch (idx) {
3214     case 0:
3215     case 2:
3216         post_index = false;
3217         writeback = false;
3218         break;
3219     case 1:
3220         post_index = true;
3221         writeback = true;
3222         break;
3223     case 3:
3224         post_index = false;
3225         writeback = true;
3226         break;
3227     default:
3228         g_assert_not_reached();
3229     }
3230 
3231     iss_valid = !is_vector && !writeback;
3232 
3233     if (rn == 31) {
3234         gen_check_sp_alignment(s);
3235     }
3236 
3237     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3238     if (!post_index) {
3239         tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3240     }
3241 
3242     memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
3243 
3244     clean_addr = gen_mte_check1_mmuidx(s, dirty_addr, is_store,
3245                                        writeback || rn != 31,
3246                                        memop, is_unpriv, memidx);
3247 
3248     if (is_vector) {
3249         if (is_store) {
3250             do_fp_st(s, rt, clean_addr, memop);
3251         } else {
3252             do_fp_ld(s, rt, clean_addr, memop);
3253         }
3254     } else {
3255         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3256         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3257 
3258         if (is_store) {
3259             do_gpr_st_memidx(s, tcg_rt, clean_addr, memop, memidx,
3260                              iss_valid, rt, iss_sf, false);
3261         } else {
3262             do_gpr_ld_memidx(s, tcg_rt, clean_addr, memop,
3263                              is_extended, memidx,
3264                              iss_valid, rt, iss_sf, false);
3265         }
3266     }
3267 
3268     if (writeback) {
3269         TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3270         if (post_index) {
3271             tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3272         }
3273         tcg_gen_mov_i64(tcg_rn, dirty_addr);
3274     }
3275 }
3276 
3277 /*
3278  * Load/store (register offset)
3279  *
3280  * 31 30 29   27  26 25 24 23 22 21  20  16 15 13 12 11 10 9  5 4  0
3281  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3282  * |size| 1 1 1 | V | 0 0 | opc | 1 |  Rm  | opt | S| 1 0 | Rn | Rt |
3283  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3284  *
3285  * For non-vector:
3286  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3287  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3288  * For vector:
3289  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3290  *   opc<0>: 0 -> store, 1 -> load
3291  * V: 1 -> vector/simd
3292  * opt: extend encoding (see DecodeRegExtend)
3293  * S: if S=1 then scale (essentially index by sizeof(size))
3294  * Rt: register to transfer into/out of
3295  * Rn: address register or SP for base
3296  * Rm: offset register or ZR for offset
3297  */
3298 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
3299                                    int opc,
3300                                    int size,
3301                                    int rt,
3302                                    bool is_vector)
3303 {
3304     int rn = extract32(insn, 5, 5);
3305     int shift = extract32(insn, 12, 1);
3306     int rm = extract32(insn, 16, 5);
3307     int opt = extract32(insn, 13, 3);
3308     bool is_signed = false;
3309     bool is_store = false;
3310     bool is_extended = false;
3311     TCGv_i64 tcg_rm, clean_addr, dirty_addr;
3312     MemOp memop;
3313 
3314     if (extract32(opt, 1, 1) == 0) {
3315         unallocated_encoding(s);
3316         return;
3317     }
3318 
3319     if (is_vector) {
3320         size |= (opc & 2) << 1;
3321         if (size > 4) {
3322             unallocated_encoding(s);
3323             return;
3324         }
3325         is_store = !extract32(opc, 0, 1);
3326         if (!fp_access_check(s)) {
3327             return;
3328         }
3329         memop = finalize_memop_asimd(s, size);
3330     } else {
3331         if (size == 3 && opc == 2) {
3332             /* PRFM - prefetch */
3333             return;
3334         }
3335         if (opc == 3 && size > 1) {
3336             unallocated_encoding(s);
3337             return;
3338         }
3339         is_store = (opc == 0);
3340         is_signed = !is_store && extract32(opc, 1, 1);
3341         is_extended = (size < 3) && extract32(opc, 0, 1);
3342         memop = finalize_memop(s, size + is_signed * MO_SIGN);
3343     }
3344 
3345     if (rn == 31) {
3346         gen_check_sp_alignment(s);
3347     }
3348     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3349 
3350     tcg_rm = read_cpu_reg(s, rm, 1);
3351     ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
3352 
3353     tcg_gen_add_i64(dirty_addr, dirty_addr, tcg_rm);
3354 
3355     clean_addr = gen_mte_check1(s, dirty_addr, is_store, true, memop);
3356 
3357     if (is_vector) {
3358         if (is_store) {
3359             do_fp_st(s, rt, clean_addr, memop);
3360         } else {
3361             do_fp_ld(s, rt, clean_addr, memop);
3362         }
3363     } else {
3364         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3365         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3366 
3367         if (is_store) {
3368             do_gpr_st(s, tcg_rt, clean_addr, memop,
3369                       true, rt, iss_sf, false);
3370         } else {
3371             do_gpr_ld(s, tcg_rt, clean_addr, memop,
3372                       is_extended, true, rt, iss_sf, false);
3373         }
3374     }
3375 }
3376 
3377 /*
3378  * Load/store (unsigned immediate)
3379  *
3380  * 31 30 29   27  26 25 24 23 22 21        10 9     5
3381  * +----+-------+---+-----+-----+------------+-------+------+
3382  * |size| 1 1 1 | V | 0 1 | opc |   imm12    |  Rn   |  Rt  |
3383  * +----+-------+---+-----+-----+------------+-------+------+
3384  *
3385  * For non-vector:
3386  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3387  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3388  * For vector:
3389  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3390  *   opc<0>: 0 -> store, 1 -> load
3391  * Rn: base address register (inc SP)
3392  * Rt: target register
3393  */
3394 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
3395                                         int opc,
3396                                         int size,
3397                                         int rt,
3398                                         bool is_vector)
3399 {
3400     int rn = extract32(insn, 5, 5);
3401     unsigned int imm12 = extract32(insn, 10, 12);
3402     unsigned int offset;
3403     TCGv_i64 clean_addr, dirty_addr;
3404     bool is_store;
3405     bool is_signed = false;
3406     bool is_extended = false;
3407     MemOp memop;
3408 
3409     if (is_vector) {
3410         size |= (opc & 2) << 1;
3411         if (size > 4) {
3412             unallocated_encoding(s);
3413             return;
3414         }
3415         is_store = !extract32(opc, 0, 1);
3416         if (!fp_access_check(s)) {
3417             return;
3418         }
3419         memop = finalize_memop_asimd(s, size);
3420     } else {
3421         if (size == 3 && opc == 2) {
3422             /* PRFM - prefetch */
3423             return;
3424         }
3425         if (opc == 3 && size > 1) {
3426             unallocated_encoding(s);
3427             return;
3428         }
3429         is_store = (opc == 0);
3430         is_signed = !is_store && extract32(opc, 1, 1);
3431         is_extended = (size < 3) && extract32(opc, 0, 1);
3432         memop = finalize_memop(s, size + is_signed * MO_SIGN);
3433     }
3434 
3435     if (rn == 31) {
3436         gen_check_sp_alignment(s);
3437     }
3438     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3439     offset = imm12 << size;
3440     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3441 
3442     clean_addr = gen_mte_check1(s, dirty_addr, is_store, rn != 31, memop);
3443 
3444     if (is_vector) {
3445         if (is_store) {
3446             do_fp_st(s, rt, clean_addr, memop);
3447         } else {
3448             do_fp_ld(s, rt, clean_addr, memop);
3449         }
3450     } else {
3451         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3452         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3453         if (is_store) {
3454             do_gpr_st(s, tcg_rt, clean_addr, memop, true, rt, iss_sf, false);
3455         } else {
3456             do_gpr_ld(s, tcg_rt, clean_addr, memop,
3457                       is_extended, true, rt, iss_sf, false);
3458         }
3459     }
3460 }
3461 
3462 /* Atomic memory operations
3463  *
3464  *  31  30      27  26    24    22  21   16   15    12    10    5     0
3465  * +------+-------+---+-----+-----+---+----+----+-----+-----+----+-----+
3466  * | size | 1 1 1 | V | 0 0 | A R | 1 | Rs | o3 | opc | 0 0 | Rn |  Rt |
3467  * +------+-------+---+-----+-----+--------+----+-----+-----+----+-----+
3468  *
3469  * Rt: the result register
3470  * Rn: base address or SP
3471  * Rs: the source register for the operation
3472  * V: vector flag (always 0 as of v8.3)
3473  * A: acquire flag
3474  * R: release flag
3475  */
3476 static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
3477                               int size, int rt, bool is_vector)
3478 {
3479     int rs = extract32(insn, 16, 5);
3480     int rn = extract32(insn, 5, 5);
3481     int o3_opc = extract32(insn, 12, 4);
3482     bool r = extract32(insn, 22, 1);
3483     bool a = extract32(insn, 23, 1);
3484     TCGv_i64 tcg_rs, tcg_rt, clean_addr;
3485     AtomicThreeOpFn *fn = NULL;
3486     MemOp mop = size;
3487 
3488     if (is_vector || !dc_isar_feature(aa64_atomics, s)) {
3489         unallocated_encoding(s);
3490         return;
3491     }
3492     switch (o3_opc) {
3493     case 000: /* LDADD */
3494         fn = tcg_gen_atomic_fetch_add_i64;
3495         break;
3496     case 001: /* LDCLR */
3497         fn = tcg_gen_atomic_fetch_and_i64;
3498         break;
3499     case 002: /* LDEOR */
3500         fn = tcg_gen_atomic_fetch_xor_i64;
3501         break;
3502     case 003: /* LDSET */
3503         fn = tcg_gen_atomic_fetch_or_i64;
3504         break;
3505     case 004: /* LDSMAX */
3506         fn = tcg_gen_atomic_fetch_smax_i64;
3507         mop |= MO_SIGN;
3508         break;
3509     case 005: /* LDSMIN */
3510         fn = tcg_gen_atomic_fetch_smin_i64;
3511         mop |= MO_SIGN;
3512         break;
3513     case 006: /* LDUMAX */
3514         fn = tcg_gen_atomic_fetch_umax_i64;
3515         break;
3516     case 007: /* LDUMIN */
3517         fn = tcg_gen_atomic_fetch_umin_i64;
3518         break;
3519     case 010: /* SWP */
3520         fn = tcg_gen_atomic_xchg_i64;
3521         break;
3522     case 014: /* LDAPR, LDAPRH, LDAPRB */
3523         if (!dc_isar_feature(aa64_rcpc_8_3, s) ||
3524             rs != 31 || a != 1 || r != 0) {
3525             unallocated_encoding(s);
3526             return;
3527         }
3528         break;
3529     default:
3530         unallocated_encoding(s);
3531         return;
3532     }
3533 
3534     if (rn == 31) {
3535         gen_check_sp_alignment(s);
3536     }
3537 
3538     mop = check_atomic_align(s, rn, mop);
3539     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), false, rn != 31, mop);
3540 
3541     if (o3_opc == 014) {
3542         /*
3543          * LDAPR* are a special case because they are a simple load, not a
3544          * fetch-and-do-something op.
3545          * The architectural consistency requirements here are weaker than
3546          * full load-acquire (we only need "load-acquire processor consistent"),
3547          * but we choose to implement them as full LDAQ.
3548          */
3549         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop, false,
3550                   true, rt, disas_ldst_compute_iss_sf(size, false, 0), true);
3551         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3552         return;
3553     }
3554 
3555     tcg_rs = read_cpu_reg(s, rs, true);
3556     tcg_rt = cpu_reg(s, rt);
3557 
3558     if (o3_opc == 1) { /* LDCLR */
3559         tcg_gen_not_i64(tcg_rs, tcg_rs);
3560     }
3561 
3562     /* The tcg atomic primitives are all full barriers.  Therefore we
3563      * can ignore the Acquire and Release bits of this instruction.
3564      */
3565     fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3566 
3567     if (mop & MO_SIGN) {
3568         switch (size) {
3569         case MO_8:
3570             tcg_gen_ext8u_i64(tcg_rt, tcg_rt);
3571             break;
3572         case MO_16:
3573             tcg_gen_ext16u_i64(tcg_rt, tcg_rt);
3574             break;
3575         case MO_32:
3576             tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3577             break;
3578         case MO_64:
3579             break;
3580         default:
3581             g_assert_not_reached();
3582         }
3583     }
3584 }
3585 
3586 /*
3587  * PAC memory operations
3588  *
3589  *  31  30      27  26    24    22  21       12  11  10    5     0
3590  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3591  * | size | 1 1 1 | V | 0 0 | M S | 1 |  imm9  | W | 1 | Rn |  Rt |
3592  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3593  *
3594  * Rt: the result register
3595  * Rn: base address or SP
3596  * V: vector flag (always 0 as of v8.3)
3597  * M: clear for key DA, set for key DB
3598  * W: pre-indexing flag
3599  * S: sign for imm9.
3600  */
3601 static void disas_ldst_pac(DisasContext *s, uint32_t insn,
3602                            int size, int rt, bool is_vector)
3603 {
3604     int rn = extract32(insn, 5, 5);
3605     bool is_wback = extract32(insn, 11, 1);
3606     bool use_key_a = !extract32(insn, 23, 1);
3607     int offset;
3608     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3609     MemOp memop;
3610 
3611     if (size != 3 || is_vector || !dc_isar_feature(aa64_pauth, s)) {
3612         unallocated_encoding(s);
3613         return;
3614     }
3615 
3616     if (rn == 31) {
3617         gen_check_sp_alignment(s);
3618     }
3619     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3620 
3621     if (s->pauth_active) {
3622         if (use_key_a) {
3623             gen_helper_autda(dirty_addr, cpu_env, dirty_addr,
3624                              tcg_constant_i64(0));
3625         } else {
3626             gen_helper_autdb(dirty_addr, cpu_env, dirty_addr,
3627                              tcg_constant_i64(0));
3628         }
3629     }
3630 
3631     /* Form the 10-bit signed, scaled offset.  */
3632     offset = (extract32(insn, 22, 1) << 9) | extract32(insn, 12, 9);
3633     offset = sextract32(offset << size, 0, 10 + size);
3634     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3635 
3636     memop = finalize_memop(s, size);
3637 
3638     /* Note that "clean" and "dirty" here refer to TBI not PAC.  */
3639     clean_addr = gen_mte_check1(s, dirty_addr, false,
3640                                 is_wback || rn != 31, memop);
3641 
3642     tcg_rt = cpu_reg(s, rt);
3643     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3644               /* extend */ false, /* iss_valid */ !is_wback,
3645               /* iss_srt */ rt, /* iss_sf */ true, /* iss_ar */ false);
3646 
3647     if (is_wback) {
3648         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3649     }
3650 }
3651 
3652 /*
3653  * LDAPR/STLR (unscaled immediate)
3654  *
3655  *  31  30            24    22  21       12    10    5     0
3656  * +------+-------------+-----+---+--------+-----+----+-----+
3657  * | size | 0 1 1 0 0 1 | opc | 0 |  imm9  | 0 0 | Rn |  Rt |
3658  * +------+-------------+-----+---+--------+-----+----+-----+
3659  *
3660  * Rt: source or destination register
3661  * Rn: base register
3662  * imm9: unscaled immediate offset
3663  * opc: 00: STLUR*, 01/10/11: various LDAPUR*
3664  * size: size of load/store
3665  */
3666 static void disas_ldst_ldapr_stlr(DisasContext *s, uint32_t insn)
3667 {
3668     int rt = extract32(insn, 0, 5);
3669     int rn = extract32(insn, 5, 5);
3670     int offset = sextract32(insn, 12, 9);
3671     int opc = extract32(insn, 22, 2);
3672     int size = extract32(insn, 30, 2);
3673     TCGv_i64 clean_addr, dirty_addr;
3674     bool is_store = false;
3675     bool extend = false;
3676     bool iss_sf;
3677     MemOp mop = size;
3678 
3679     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3680         unallocated_encoding(s);
3681         return;
3682     }
3683 
3684     switch (opc) {
3685     case 0: /* STLURB */
3686         is_store = true;
3687         break;
3688     case 1: /* LDAPUR* */
3689         break;
3690     case 2: /* LDAPURS* 64-bit variant */
3691         if (size == 3) {
3692             unallocated_encoding(s);
3693             return;
3694         }
3695         mop |= MO_SIGN;
3696         break;
3697     case 3: /* LDAPURS* 32-bit variant */
3698         if (size > 1) {
3699             unallocated_encoding(s);
3700             return;
3701         }
3702         mop |= MO_SIGN;
3703         extend = true; /* zero-extend 32->64 after signed load */
3704         break;
3705     default:
3706         g_assert_not_reached();
3707     }
3708 
3709     iss_sf = disas_ldst_compute_iss_sf(size, (mop & MO_SIGN) != 0, opc);
3710 
3711     if (rn == 31) {
3712         gen_check_sp_alignment(s);
3713     }
3714 
3715     mop = check_ordered_align(s, rn, offset, is_store, mop);
3716 
3717     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3718     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3719     clean_addr = clean_data_tbi(s, dirty_addr);
3720 
3721     if (is_store) {
3722         /* Store-Release semantics */
3723         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3724         do_gpr_st(s, cpu_reg(s, rt), clean_addr, mop, true, rt, iss_sf, true);
3725     } else {
3726         /*
3727          * Load-AcquirePC semantics; we implement as the slightly more
3728          * restrictive Load-Acquire.
3729          */
3730         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop,
3731                   extend, true, rt, iss_sf, true);
3732         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3733     }
3734 }
3735 
3736 /* Load/store register (all forms) */
3737 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
3738 {
3739     int rt = extract32(insn, 0, 5);
3740     int opc = extract32(insn, 22, 2);
3741     bool is_vector = extract32(insn, 26, 1);
3742     int size = extract32(insn, 30, 2);
3743 
3744     switch (extract32(insn, 24, 2)) {
3745     case 0:
3746         if (extract32(insn, 21, 1) == 0) {
3747             /* Load/store register (unscaled immediate)
3748              * Load/store immediate pre/post-indexed
3749              * Load/store register unprivileged
3750              */
3751             disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
3752             return;
3753         }
3754         switch (extract32(insn, 10, 2)) {
3755         case 0:
3756             disas_ldst_atomic(s, insn, size, rt, is_vector);
3757             return;
3758         case 2:
3759             disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
3760             return;
3761         default:
3762             disas_ldst_pac(s, insn, size, rt, is_vector);
3763             return;
3764         }
3765         break;
3766     case 1:
3767         disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
3768         return;
3769     }
3770     unallocated_encoding(s);
3771 }
3772 
3773 /* AdvSIMD load/store multiple structures
3774  *
3775  *  31  30  29           23 22  21         16 15    12 11  10 9    5 4    0
3776  * +---+---+---------------+---+-------------+--------+------+------+------+
3777  * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size |  Rn  |  Rt  |
3778  * +---+---+---------------+---+-------------+--------+------+------+------+
3779  *
3780  * AdvSIMD load/store multiple structures (post-indexed)
3781  *
3782  *  31  30  29           23 22  21  20     16 15    12 11  10 9    5 4    0
3783  * +---+---+---------------+---+---+---------+--------+------+------+------+
3784  * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 |   Rm    | opcode | size |  Rn  |  Rt  |
3785  * +---+---+---------------+---+---+---------+--------+------+------+------+
3786  *
3787  * Rt: first (or only) SIMD&FP register to be transferred
3788  * Rn: base address or SP
3789  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3790  */
3791 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
3792 {
3793     int rt = extract32(insn, 0, 5);
3794     int rn = extract32(insn, 5, 5);
3795     int rm = extract32(insn, 16, 5);
3796     int size = extract32(insn, 10, 2);
3797     int opcode = extract32(insn, 12, 4);
3798     bool is_store = !extract32(insn, 22, 1);
3799     bool is_postidx = extract32(insn, 23, 1);
3800     bool is_q = extract32(insn, 30, 1);
3801     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3802     MemOp endian, align, mop;
3803 
3804     int total;    /* total bytes */
3805     int elements; /* elements per vector */
3806     int rpt;    /* num iterations */
3807     int selem;  /* structure elements */
3808     int r;
3809 
3810     if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
3811         unallocated_encoding(s);
3812         return;
3813     }
3814 
3815     if (!is_postidx && rm != 0) {
3816         unallocated_encoding(s);
3817         return;
3818     }
3819 
3820     /* From the shared decode logic */
3821     switch (opcode) {
3822     case 0x0:
3823         rpt = 1;
3824         selem = 4;
3825         break;
3826     case 0x2:
3827         rpt = 4;
3828         selem = 1;
3829         break;
3830     case 0x4:
3831         rpt = 1;
3832         selem = 3;
3833         break;
3834     case 0x6:
3835         rpt = 3;
3836         selem = 1;
3837         break;
3838     case 0x7:
3839         rpt = 1;
3840         selem = 1;
3841         break;
3842     case 0x8:
3843         rpt = 1;
3844         selem = 2;
3845         break;
3846     case 0xa:
3847         rpt = 2;
3848         selem = 1;
3849         break;
3850     default:
3851         unallocated_encoding(s);
3852         return;
3853     }
3854 
3855     if (size == 3 && !is_q && selem != 1) {
3856         /* reserved */
3857         unallocated_encoding(s);
3858         return;
3859     }
3860 
3861     if (!fp_access_check(s)) {
3862         return;
3863     }
3864 
3865     if (rn == 31) {
3866         gen_check_sp_alignment(s);
3867     }
3868 
3869     /* For our purposes, bytes are always little-endian.  */
3870     endian = s->be_data;
3871     if (size == 0) {
3872         endian = MO_LE;
3873     }
3874 
3875     total = rpt * selem * (is_q ? 16 : 8);
3876     tcg_rn = cpu_reg_sp(s, rn);
3877 
3878     /*
3879      * Issue the MTE check vs the logical repeat count, before we
3880      * promote consecutive little-endian elements below.
3881      */
3882     clean_addr = gen_mte_checkN(s, tcg_rn, is_store, is_postidx || rn != 31,
3883                                 total, finalize_memop_asimd(s, size));
3884 
3885     /*
3886      * Consecutive little-endian elements from a single register
3887      * can be promoted to a larger little-endian operation.
3888      */
3889     align = MO_ALIGN;
3890     if (selem == 1 && endian == MO_LE) {
3891         align = pow2_align(size);
3892         size = 3;
3893     }
3894     if (!s->align_mem) {
3895         align = 0;
3896     }
3897     mop = endian | size | align;
3898 
3899     elements = (is_q ? 16 : 8) >> size;
3900     tcg_ebytes = tcg_constant_i64(1 << size);
3901     for (r = 0; r < rpt; r++) {
3902         int e;
3903         for (e = 0; e < elements; e++) {
3904             int xs;
3905             for (xs = 0; xs < selem; xs++) {
3906                 int tt = (rt + r + xs) % 32;
3907                 if (is_store) {
3908                     do_vec_st(s, tt, e, clean_addr, mop);
3909                 } else {
3910                     do_vec_ld(s, tt, e, clean_addr, mop);
3911                 }
3912                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3913             }
3914         }
3915     }
3916 
3917     if (!is_store) {
3918         /* For non-quad operations, setting a slice of the low
3919          * 64 bits of the register clears the high 64 bits (in
3920          * the ARM ARM pseudocode this is implicit in the fact
3921          * that 'rval' is a 64 bit wide variable).
3922          * For quad operations, we might still need to zero the
3923          * high bits of SVE.
3924          */
3925         for (r = 0; r < rpt * selem; r++) {
3926             int tt = (rt + r) % 32;
3927             clear_vec_high(s, is_q, tt);
3928         }
3929     }
3930 
3931     if (is_postidx) {
3932         if (rm == 31) {
3933             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3934         } else {
3935             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3936         }
3937     }
3938 }
3939 
3940 /* AdvSIMD load/store single structure
3941  *
3942  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3943  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3944  * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size |  Rn  |  Rt  |
3945  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3946  *
3947  * AdvSIMD load/store single structure (post-indexed)
3948  *
3949  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3950  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3951  * | 0 | Q | 0 0 1 1 0 1 1 | L R |     Rm    | opc | S | size |  Rn  |  Rt  |
3952  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3953  *
3954  * Rt: first (or only) SIMD&FP register to be transferred
3955  * Rn: base address or SP
3956  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3957  * index = encoded in Q:S:size dependent on size
3958  *
3959  * lane_size = encoded in R, opc
3960  * transfer width = encoded in opc, S, size
3961  */
3962 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
3963 {
3964     int rt = extract32(insn, 0, 5);
3965     int rn = extract32(insn, 5, 5);
3966     int rm = extract32(insn, 16, 5);
3967     int size = extract32(insn, 10, 2);
3968     int S = extract32(insn, 12, 1);
3969     int opc = extract32(insn, 13, 3);
3970     int R = extract32(insn, 21, 1);
3971     int is_load = extract32(insn, 22, 1);
3972     int is_postidx = extract32(insn, 23, 1);
3973     int is_q = extract32(insn, 30, 1);
3974 
3975     int scale = extract32(opc, 1, 2);
3976     int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
3977     bool replicate = false;
3978     int index = is_q << 3 | S << 2 | size;
3979     int xs, total;
3980     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3981     MemOp mop;
3982 
3983     if (extract32(insn, 31, 1)) {
3984         unallocated_encoding(s);
3985         return;
3986     }
3987     if (!is_postidx && rm != 0) {
3988         unallocated_encoding(s);
3989         return;
3990     }
3991 
3992     switch (scale) {
3993     case 3:
3994         if (!is_load || S) {
3995             unallocated_encoding(s);
3996             return;
3997         }
3998         scale = size;
3999         replicate = true;
4000         break;
4001     case 0:
4002         break;
4003     case 1:
4004         if (extract32(size, 0, 1)) {
4005             unallocated_encoding(s);
4006             return;
4007         }
4008         index >>= 1;
4009         break;
4010     case 2:
4011         if (extract32(size, 1, 1)) {
4012             unallocated_encoding(s);
4013             return;
4014         }
4015         if (!extract32(size, 0, 1)) {
4016             index >>= 2;
4017         } else {
4018             if (S) {
4019                 unallocated_encoding(s);
4020                 return;
4021             }
4022             index >>= 3;
4023             scale = 3;
4024         }
4025         break;
4026     default:
4027         g_assert_not_reached();
4028     }
4029 
4030     if (!fp_access_check(s)) {
4031         return;
4032     }
4033 
4034     if (rn == 31) {
4035         gen_check_sp_alignment(s);
4036     }
4037 
4038     total = selem << scale;
4039     tcg_rn = cpu_reg_sp(s, rn);
4040 
4041     mop = finalize_memop_asimd(s, scale);
4042 
4043     clean_addr = gen_mte_checkN(s, tcg_rn, !is_load, is_postidx || rn != 31,
4044                                 total, mop);
4045 
4046     tcg_ebytes = tcg_constant_i64(1 << scale);
4047     for (xs = 0; xs < selem; xs++) {
4048         if (replicate) {
4049             /* Load and replicate to all elements */
4050             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4051 
4052             tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
4053             tcg_gen_gvec_dup_i64(scale, vec_full_reg_offset(s, rt),
4054                                  (is_q + 1) * 8, vec_full_reg_size(s),
4055                                  tcg_tmp);
4056         } else {
4057             /* Load/store one element per register */
4058             if (is_load) {
4059                 do_vec_ld(s, rt, index, clean_addr, mop);
4060             } else {
4061                 do_vec_st(s, rt, index, clean_addr, mop);
4062             }
4063         }
4064         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
4065         rt = (rt + 1) % 32;
4066     }
4067 
4068     if (is_postidx) {
4069         if (rm == 31) {
4070             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
4071         } else {
4072             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
4073         }
4074     }
4075 }
4076 
4077 /*
4078  * Load/Store memory tags
4079  *
4080  *  31 30 29         24     22  21     12    10      5      0
4081  * +-----+-------------+-----+---+------+-----+------+------+
4082  * | 1 1 | 0 1 1 0 0 1 | op1 | 1 | imm9 | op2 |  Rn  |  Rt  |
4083  * +-----+-------------+-----+---+------+-----+------+------+
4084  */
4085 static void disas_ldst_tag(DisasContext *s, uint32_t insn)
4086 {
4087     int rt = extract32(insn, 0, 5);
4088     int rn = extract32(insn, 5, 5);
4089     uint64_t offset = sextract64(insn, 12, 9) << LOG2_TAG_GRANULE;
4090     int op2 = extract32(insn, 10, 2);
4091     int op1 = extract32(insn, 22, 2);
4092     bool is_load = false, is_pair = false, is_zero = false, is_mult = false;
4093     int index = 0;
4094     TCGv_i64 addr, clean_addr, tcg_rt;
4095 
4096     /* We checked insn bits [29:24,21] in the caller.  */
4097     if (extract32(insn, 30, 2) != 3) {
4098         goto do_unallocated;
4099     }
4100 
4101     /*
4102      * @index is a tri-state variable which has 3 states:
4103      * < 0 : post-index, writeback
4104      * = 0 : signed offset
4105      * > 0 : pre-index, writeback
4106      */
4107     switch (op1) {
4108     case 0:
4109         if (op2 != 0) {
4110             /* STG */
4111             index = op2 - 2;
4112         } else {
4113             /* STZGM */
4114             if (s->current_el == 0 || offset != 0) {
4115                 goto do_unallocated;
4116             }
4117             is_mult = is_zero = true;
4118         }
4119         break;
4120     case 1:
4121         if (op2 != 0) {
4122             /* STZG */
4123             is_zero = true;
4124             index = op2 - 2;
4125         } else {
4126             /* LDG */
4127             is_load = true;
4128         }
4129         break;
4130     case 2:
4131         if (op2 != 0) {
4132             /* ST2G */
4133             is_pair = true;
4134             index = op2 - 2;
4135         } else {
4136             /* STGM */
4137             if (s->current_el == 0 || offset != 0) {
4138                 goto do_unallocated;
4139             }
4140             is_mult = true;
4141         }
4142         break;
4143     case 3:
4144         if (op2 != 0) {
4145             /* STZ2G */
4146             is_pair = is_zero = true;
4147             index = op2 - 2;
4148         } else {
4149             /* LDGM */
4150             if (s->current_el == 0 || offset != 0) {
4151                 goto do_unallocated;
4152             }
4153             is_mult = is_load = true;
4154         }
4155         break;
4156 
4157     default:
4158     do_unallocated:
4159         unallocated_encoding(s);
4160         return;
4161     }
4162 
4163     if (is_mult
4164         ? !dc_isar_feature(aa64_mte, s)
4165         : !dc_isar_feature(aa64_mte_insn_reg, s)) {
4166         goto do_unallocated;
4167     }
4168 
4169     if (rn == 31) {
4170         gen_check_sp_alignment(s);
4171     }
4172 
4173     addr = read_cpu_reg_sp(s, rn, true);
4174     if (index >= 0) {
4175         /* pre-index or signed offset */
4176         tcg_gen_addi_i64(addr, addr, offset);
4177     }
4178 
4179     if (is_mult) {
4180         tcg_rt = cpu_reg(s, rt);
4181 
4182         if (is_zero) {
4183             int size = 4 << s->dcz_blocksize;
4184 
4185             if (s->ata) {
4186                 gen_helper_stzgm_tags(cpu_env, addr, tcg_rt);
4187             }
4188             /*
4189              * The non-tags portion of STZGM is mostly like DC_ZVA,
4190              * except the alignment happens before the access.
4191              */
4192             clean_addr = clean_data_tbi(s, addr);
4193             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4194             gen_helper_dc_zva(cpu_env, clean_addr);
4195         } else if (s->ata) {
4196             if (is_load) {
4197                 gen_helper_ldgm(tcg_rt, cpu_env, addr);
4198             } else {
4199                 gen_helper_stgm(cpu_env, addr, tcg_rt);
4200             }
4201         } else {
4202             MMUAccessType acc = is_load ? MMU_DATA_LOAD : MMU_DATA_STORE;
4203             int size = 4 << GMID_EL1_BS;
4204 
4205             clean_addr = clean_data_tbi(s, addr);
4206             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4207             gen_probe_access(s, clean_addr, acc, size);
4208 
4209             if (is_load) {
4210                 /* The result tags are zeros.  */
4211                 tcg_gen_movi_i64(tcg_rt, 0);
4212             }
4213         }
4214         return;
4215     }
4216 
4217     if (is_load) {
4218         tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
4219         tcg_rt = cpu_reg(s, rt);
4220         if (s->ata) {
4221             gen_helper_ldg(tcg_rt, cpu_env, addr, tcg_rt);
4222         } else {
4223             /*
4224              * Tag access disabled: we must check for aborts on the load
4225              * load from [rn+offset], and then insert a 0 tag into rt.
4226              */
4227             clean_addr = clean_data_tbi(s, addr);
4228             gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
4229             gen_address_with_allocation_tag0(tcg_rt, tcg_rt);
4230         }
4231     } else {
4232         tcg_rt = cpu_reg_sp(s, rt);
4233         if (!s->ata) {
4234             /*
4235              * For STG and ST2G, we need to check alignment and probe memory.
4236              * TODO: For STZG and STZ2G, we could rely on the stores below,
4237              * at least for system mode; user-only won't enforce alignment.
4238              */
4239             if (is_pair) {
4240                 gen_helper_st2g_stub(cpu_env, addr);
4241             } else {
4242                 gen_helper_stg_stub(cpu_env, addr);
4243             }
4244         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
4245             if (is_pair) {
4246                 gen_helper_st2g_parallel(cpu_env, addr, tcg_rt);
4247             } else {
4248                 gen_helper_stg_parallel(cpu_env, addr, tcg_rt);
4249             }
4250         } else {
4251             if (is_pair) {
4252                 gen_helper_st2g(cpu_env, addr, tcg_rt);
4253             } else {
4254                 gen_helper_stg(cpu_env, addr, tcg_rt);
4255             }
4256         }
4257     }
4258 
4259     if (is_zero) {
4260         TCGv_i64 clean_addr = clean_data_tbi(s, addr);
4261         TCGv_i64 zero64 = tcg_constant_i64(0);
4262         TCGv_i128 zero128 = tcg_temp_new_i128();
4263         int mem_index = get_mem_index(s);
4264         MemOp mop = finalize_memop(s, MO_128 | MO_ALIGN);
4265 
4266         tcg_gen_concat_i64_i128(zero128, zero64, zero64);
4267 
4268         /* This is 1 or 2 atomic 16-byte operations. */
4269         tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4270         if (is_pair) {
4271             tcg_gen_addi_i64(clean_addr, clean_addr, 16);
4272             tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4273         }
4274     }
4275 
4276     if (index != 0) {
4277         /* pre-index or post-index */
4278         if (index < 0) {
4279             /* post-index */
4280             tcg_gen_addi_i64(addr, addr, offset);
4281         }
4282         tcg_gen_mov_i64(cpu_reg_sp(s, rn), addr);
4283     }
4284 }
4285 
4286 /* Loads and stores */
4287 static void disas_ldst(DisasContext *s, uint32_t insn)
4288 {
4289     switch (extract32(insn, 24, 6)) {
4290     case 0x08: /* Load/store exclusive */
4291         disas_ldst_excl(s, insn);
4292         break;
4293     case 0x18: case 0x1c: /* Load register (literal) */
4294         disas_ld_lit(s, insn);
4295         break;
4296     case 0x28: case 0x29:
4297     case 0x2c: case 0x2d: /* Load/store pair (all forms) */
4298         disas_ldst_pair(s, insn);
4299         break;
4300     case 0x38: case 0x39:
4301     case 0x3c: case 0x3d: /* Load/store register (all forms) */
4302         disas_ldst_reg(s, insn);
4303         break;
4304     case 0x0c: /* AdvSIMD load/store multiple structures */
4305         disas_ldst_multiple_struct(s, insn);
4306         break;
4307     case 0x0d: /* AdvSIMD load/store single structure */
4308         disas_ldst_single_struct(s, insn);
4309         break;
4310     case 0x19:
4311         if (extract32(insn, 21, 1) != 0) {
4312             disas_ldst_tag(s, insn);
4313         } else if (extract32(insn, 10, 2) == 0) {
4314             disas_ldst_ldapr_stlr(s, insn);
4315         } else {
4316             unallocated_encoding(s);
4317         }
4318         break;
4319     default:
4320         unallocated_encoding(s);
4321         break;
4322     }
4323 }
4324 
4325 typedef void ArithTwoOp(TCGv_i64, TCGv_i64, TCGv_i64);
4326 
4327 static bool gen_rri(DisasContext *s, arg_rri_sf *a,
4328                     bool rd_sp, bool rn_sp, ArithTwoOp *fn)
4329 {
4330     TCGv_i64 tcg_rn = rn_sp ? cpu_reg_sp(s, a->rn) : cpu_reg(s, a->rn);
4331     TCGv_i64 tcg_rd = rd_sp ? cpu_reg_sp(s, a->rd) : cpu_reg(s, a->rd);
4332     TCGv_i64 tcg_imm = tcg_constant_i64(a->imm);
4333 
4334     fn(tcg_rd, tcg_rn, tcg_imm);
4335     if (!a->sf) {
4336         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4337     }
4338     return true;
4339 }
4340 
4341 /*
4342  * PC-rel. addressing
4343  */
4344 
4345 static bool trans_ADR(DisasContext *s, arg_ri *a)
4346 {
4347     gen_pc_plus_diff(s, cpu_reg(s, a->rd), a->imm);
4348     return true;
4349 }
4350 
4351 static bool trans_ADRP(DisasContext *s, arg_ri *a)
4352 {
4353     int64_t offset = (int64_t)a->imm << 12;
4354 
4355     /* The page offset is ok for CF_PCREL. */
4356     offset -= s->pc_curr & 0xfff;
4357     gen_pc_plus_diff(s, cpu_reg(s, a->rd), offset);
4358     return true;
4359 }
4360 
4361 /*
4362  * Add/subtract (immediate)
4363  */
4364 TRANS(ADD_i, gen_rri, a, 1, 1, tcg_gen_add_i64)
4365 TRANS(SUB_i, gen_rri, a, 1, 1, tcg_gen_sub_i64)
4366 TRANS(ADDS_i, gen_rri, a, 0, 1, a->sf ? gen_add64_CC : gen_add32_CC)
4367 TRANS(SUBS_i, gen_rri, a, 0, 1, a->sf ? gen_sub64_CC : gen_sub32_CC)
4368 
4369 /*
4370  * Add/subtract (immediate, with tags)
4371  */
4372 
4373 static bool gen_add_sub_imm_with_tags(DisasContext *s, arg_rri_tag *a,
4374                                       bool sub_op)
4375 {
4376     TCGv_i64 tcg_rn, tcg_rd;
4377     int imm;
4378 
4379     imm = a->uimm6 << LOG2_TAG_GRANULE;
4380     if (sub_op) {
4381         imm = -imm;
4382     }
4383 
4384     tcg_rn = cpu_reg_sp(s, a->rn);
4385     tcg_rd = cpu_reg_sp(s, a->rd);
4386 
4387     if (s->ata) {
4388         gen_helper_addsubg(tcg_rd, cpu_env, tcg_rn,
4389                            tcg_constant_i32(imm),
4390                            tcg_constant_i32(a->uimm4));
4391     } else {
4392         tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4393         gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4394     }
4395     return true;
4396 }
4397 
4398 TRANS_FEAT(ADDG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, false)
4399 TRANS_FEAT(SUBG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, true)
4400 
4401 /* The input should be a value in the bottom e bits (with higher
4402  * bits zero); returns that value replicated into every element
4403  * of size e in a 64 bit integer.
4404  */
4405 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4406 {
4407     assert(e != 0);
4408     while (e < 64) {
4409         mask |= mask << e;
4410         e *= 2;
4411     }
4412     return mask;
4413 }
4414 
4415 /*
4416  * Logical (immediate)
4417  */
4418 
4419 /*
4420  * Simplified variant of pseudocode DecodeBitMasks() for the case where we
4421  * only require the wmask. Returns false if the imms/immr/immn are a reserved
4422  * value (ie should cause a guest UNDEF exception), and true if they are
4423  * valid, in which case the decoded bit pattern is written to result.
4424  */
4425 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4426                             unsigned int imms, unsigned int immr)
4427 {
4428     uint64_t mask;
4429     unsigned e, levels, s, r;
4430     int len;
4431 
4432     assert(immn < 2 && imms < 64 && immr < 64);
4433 
4434     /* The bit patterns we create here are 64 bit patterns which
4435      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4436      * 64 bits each. Each element contains the same value: a run
4437      * of between 1 and e-1 non-zero bits, rotated within the
4438      * element by between 0 and e-1 bits.
4439      *
4440      * The element size and run length are encoded into immn (1 bit)
4441      * and imms (6 bits) as follows:
4442      * 64 bit elements: immn = 1, imms = <length of run - 1>
4443      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4444      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4445      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4446      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4447      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4448      * Notice that immn = 0, imms = 11111x is the only combination
4449      * not covered by one of the above options; this is reserved.
4450      * Further, <length of run - 1> all-ones is a reserved pattern.
4451      *
4452      * In all cases the rotation is by immr % e (and immr is 6 bits).
4453      */
4454 
4455     /* First determine the element size */
4456     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4457     if (len < 1) {
4458         /* This is the immn == 0, imms == 0x11111x case */
4459         return false;
4460     }
4461     e = 1 << len;
4462 
4463     levels = e - 1;
4464     s = imms & levels;
4465     r = immr & levels;
4466 
4467     if (s == levels) {
4468         /* <length of run - 1> mustn't be all-ones. */
4469         return false;
4470     }
4471 
4472     /* Create the value of one element: s+1 set bits rotated
4473      * by r within the element (which is e bits wide)...
4474      */
4475     mask = MAKE_64BIT_MASK(0, s + 1);
4476     if (r) {
4477         mask = (mask >> r) | (mask << (e - r));
4478         mask &= MAKE_64BIT_MASK(0, e);
4479     }
4480     /* ...then replicate the element over the whole 64 bit value */
4481     mask = bitfield_replicate(mask, e);
4482     *result = mask;
4483     return true;
4484 }
4485 
4486 static bool gen_rri_log(DisasContext *s, arg_rri_log *a, bool set_cc,
4487                         void (*fn)(TCGv_i64, TCGv_i64, int64_t))
4488 {
4489     TCGv_i64 tcg_rd, tcg_rn;
4490     uint64_t imm;
4491 
4492     /* Some immediate field values are reserved. */
4493     if (!logic_imm_decode_wmask(&imm, extract32(a->dbm, 12, 1),
4494                                 extract32(a->dbm, 0, 6),
4495                                 extract32(a->dbm, 6, 6))) {
4496         return false;
4497     }
4498     if (!a->sf) {
4499         imm &= 0xffffffffull;
4500     }
4501 
4502     tcg_rd = set_cc ? cpu_reg(s, a->rd) : cpu_reg_sp(s, a->rd);
4503     tcg_rn = cpu_reg(s, a->rn);
4504 
4505     fn(tcg_rd, tcg_rn, imm);
4506     if (set_cc) {
4507         gen_logic_CC(a->sf, tcg_rd);
4508     }
4509     if (!a->sf) {
4510         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4511     }
4512     return true;
4513 }
4514 
4515 TRANS(AND_i, gen_rri_log, a, false, tcg_gen_andi_i64)
4516 TRANS(ORR_i, gen_rri_log, a, false, tcg_gen_ori_i64)
4517 TRANS(EOR_i, gen_rri_log, a, false, tcg_gen_xori_i64)
4518 TRANS(ANDS_i, gen_rri_log, a, true, tcg_gen_andi_i64)
4519 
4520 /*
4521  * Move wide (immediate)
4522  */
4523 
4524 static bool trans_MOVZ(DisasContext *s, arg_movw *a)
4525 {
4526     int pos = a->hw << 4;
4527     tcg_gen_movi_i64(cpu_reg(s, a->rd), (uint64_t)a->imm << pos);
4528     return true;
4529 }
4530 
4531 static bool trans_MOVN(DisasContext *s, arg_movw *a)
4532 {
4533     int pos = a->hw << 4;
4534     uint64_t imm = a->imm;
4535 
4536     imm = ~(imm << pos);
4537     if (!a->sf) {
4538         imm = (uint32_t)imm;
4539     }
4540     tcg_gen_movi_i64(cpu_reg(s, a->rd), imm);
4541     return true;
4542 }
4543 
4544 static bool trans_MOVK(DisasContext *s, arg_movw *a)
4545 {
4546     int pos = a->hw << 4;
4547     TCGv_i64 tcg_rd, tcg_im;
4548 
4549     tcg_rd = cpu_reg(s, a->rd);
4550     tcg_im = tcg_constant_i64(a->imm);
4551     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_im, pos, 16);
4552     if (!a->sf) {
4553         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4554     }
4555     return true;
4556 }
4557 
4558 /*
4559  * Bitfield
4560  */
4561 
4562 static bool trans_SBFM(DisasContext *s, arg_SBFM *a)
4563 {
4564     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4565     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4566     unsigned int bitsize = a->sf ? 64 : 32;
4567     unsigned int ri = a->immr;
4568     unsigned int si = a->imms;
4569     unsigned int pos, len;
4570 
4571     if (si >= ri) {
4572         /* Wd<s-r:0> = Wn<s:r> */
4573         len = (si - ri) + 1;
4574         tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4575         if (!a->sf) {
4576             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4577         }
4578     } else {
4579         /* Wd<32+s-r,32-r> = Wn<s:0> */
4580         len = si + 1;
4581         pos = (bitsize - ri) & (bitsize - 1);
4582 
4583         if (len < ri) {
4584             /*
4585              * Sign extend the destination field from len to fill the
4586              * balance of the word.  Let the deposit below insert all
4587              * of those sign bits.
4588              */
4589             tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4590             len = ri;
4591         }
4592 
4593         /*
4594          * We start with zero, and we haven't modified any bits outside
4595          * bitsize, therefore no final zero-extension is unneeded for !sf.
4596          */
4597         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4598     }
4599     return true;
4600 }
4601 
4602 static bool trans_UBFM(DisasContext *s, arg_UBFM *a)
4603 {
4604     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4605     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4606     unsigned int bitsize = a->sf ? 64 : 32;
4607     unsigned int ri = a->immr;
4608     unsigned int si = a->imms;
4609     unsigned int pos, len;
4610 
4611     tcg_rd = cpu_reg(s, a->rd);
4612     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4613 
4614     if (si >= ri) {
4615         /* Wd<s-r:0> = Wn<s:r> */
4616         len = (si - ri) + 1;
4617         tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4618     } else {
4619         /* Wd<32+s-r,32-r> = Wn<s:0> */
4620         len = si + 1;
4621         pos = (bitsize - ri) & (bitsize - 1);
4622         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4623     }
4624     return true;
4625 }
4626 
4627 static bool trans_BFM(DisasContext *s, arg_BFM *a)
4628 {
4629     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4630     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4631     unsigned int bitsize = a->sf ? 64 : 32;
4632     unsigned int ri = a->immr;
4633     unsigned int si = a->imms;
4634     unsigned int pos, len;
4635 
4636     tcg_rd = cpu_reg(s, a->rd);
4637     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4638 
4639     if (si >= ri) {
4640         /* Wd<s-r:0> = Wn<s:r> */
4641         tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4642         len = (si - ri) + 1;
4643         pos = 0;
4644     } else {
4645         /* Wd<32+s-r,32-r> = Wn<s:0> */
4646         len = si + 1;
4647         pos = (bitsize - ri) & (bitsize - 1);
4648     }
4649 
4650     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4651     if (!a->sf) {
4652         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4653     }
4654     return true;
4655 }
4656 
4657 static bool trans_EXTR(DisasContext *s, arg_extract *a)
4658 {
4659     TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4660 
4661     tcg_rd = cpu_reg(s, a->rd);
4662 
4663     if (unlikely(a->imm == 0)) {
4664         /*
4665          * tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4666          * so an extract from bit 0 is a special case.
4667          */
4668         if (a->sf) {
4669             tcg_gen_mov_i64(tcg_rd, cpu_reg(s, a->rm));
4670         } else {
4671             tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, a->rm));
4672         }
4673     } else {
4674         tcg_rm = cpu_reg(s, a->rm);
4675         tcg_rn = cpu_reg(s, a->rn);
4676 
4677         if (a->sf) {
4678             /* Specialization to ROR happens in EXTRACT2.  */
4679             tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, a->imm);
4680         } else {
4681             TCGv_i32 t0 = tcg_temp_new_i32();
4682 
4683             tcg_gen_extrl_i64_i32(t0, tcg_rm);
4684             if (a->rm == a->rn) {
4685                 tcg_gen_rotri_i32(t0, t0, a->imm);
4686             } else {
4687                 TCGv_i32 t1 = tcg_temp_new_i32();
4688                 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4689                 tcg_gen_extract2_i32(t0, t0, t1, a->imm);
4690             }
4691             tcg_gen_extu_i32_i64(tcg_rd, t0);
4692         }
4693     }
4694     return true;
4695 }
4696 
4697 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4698  * Note that it is the caller's responsibility to ensure that the
4699  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4700  * mandated semantics for out of range shifts.
4701  */
4702 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4703                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4704 {
4705     switch (shift_type) {
4706     case A64_SHIFT_TYPE_LSL:
4707         tcg_gen_shl_i64(dst, src, shift_amount);
4708         break;
4709     case A64_SHIFT_TYPE_LSR:
4710         tcg_gen_shr_i64(dst, src, shift_amount);
4711         break;
4712     case A64_SHIFT_TYPE_ASR:
4713         if (!sf) {
4714             tcg_gen_ext32s_i64(dst, src);
4715         }
4716         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4717         break;
4718     case A64_SHIFT_TYPE_ROR:
4719         if (sf) {
4720             tcg_gen_rotr_i64(dst, src, shift_amount);
4721         } else {
4722             TCGv_i32 t0, t1;
4723             t0 = tcg_temp_new_i32();
4724             t1 = tcg_temp_new_i32();
4725             tcg_gen_extrl_i64_i32(t0, src);
4726             tcg_gen_extrl_i64_i32(t1, shift_amount);
4727             tcg_gen_rotr_i32(t0, t0, t1);
4728             tcg_gen_extu_i32_i64(dst, t0);
4729         }
4730         break;
4731     default:
4732         assert(FALSE); /* all shift types should be handled */
4733         break;
4734     }
4735 
4736     if (!sf) { /* zero extend final result */
4737         tcg_gen_ext32u_i64(dst, dst);
4738     }
4739 }
4740 
4741 /* Shift a TCGv src by immediate, put result in dst.
4742  * The shift amount must be in range (this should always be true as the
4743  * relevant instructions will UNDEF on bad shift immediates).
4744  */
4745 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4746                           enum a64_shift_type shift_type, unsigned int shift_i)
4747 {
4748     assert(shift_i < (sf ? 64 : 32));
4749 
4750     if (shift_i == 0) {
4751         tcg_gen_mov_i64(dst, src);
4752     } else {
4753         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
4754     }
4755 }
4756 
4757 /* Logical (shifted register)
4758  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
4759  * +----+-----+-----------+-------+---+------+--------+------+------+
4760  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
4761  * +----+-----+-----------+-------+---+------+--------+------+------+
4762  */
4763 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4764 {
4765     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4766     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4767 
4768     sf = extract32(insn, 31, 1);
4769     opc = extract32(insn, 29, 2);
4770     shift_type = extract32(insn, 22, 2);
4771     invert = extract32(insn, 21, 1);
4772     rm = extract32(insn, 16, 5);
4773     shift_amount = extract32(insn, 10, 6);
4774     rn = extract32(insn, 5, 5);
4775     rd = extract32(insn, 0, 5);
4776 
4777     if (!sf && (shift_amount & (1 << 5))) {
4778         unallocated_encoding(s);
4779         return;
4780     }
4781 
4782     tcg_rd = cpu_reg(s, rd);
4783 
4784     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4785         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4786          * register-register MOV and MVN, so it is worth special casing.
4787          */
4788         tcg_rm = cpu_reg(s, rm);
4789         if (invert) {
4790             tcg_gen_not_i64(tcg_rd, tcg_rm);
4791             if (!sf) {
4792                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4793             }
4794         } else {
4795             if (sf) {
4796                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4797             } else {
4798                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4799             }
4800         }
4801         return;
4802     }
4803 
4804     tcg_rm = read_cpu_reg(s, rm, sf);
4805 
4806     if (shift_amount) {
4807         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4808     }
4809 
4810     tcg_rn = cpu_reg(s, rn);
4811 
4812     switch (opc | (invert << 2)) {
4813     case 0: /* AND */
4814     case 3: /* ANDS */
4815         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4816         break;
4817     case 1: /* ORR */
4818         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4819         break;
4820     case 2: /* EOR */
4821         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4822         break;
4823     case 4: /* BIC */
4824     case 7: /* BICS */
4825         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4826         break;
4827     case 5: /* ORN */
4828         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4829         break;
4830     case 6: /* EON */
4831         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4832         break;
4833     default:
4834         assert(FALSE);
4835         break;
4836     }
4837 
4838     if (!sf) {
4839         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4840     }
4841 
4842     if (opc == 3) {
4843         gen_logic_CC(sf, tcg_rd);
4844     }
4845 }
4846 
4847 /*
4848  * Add/subtract (extended register)
4849  *
4850  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
4851  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4852  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
4853  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4854  *
4855  *  sf: 0 -> 32bit, 1 -> 64bit
4856  *  op: 0 -> add  , 1 -> sub
4857  *   S: 1 -> set flags
4858  * opt: 00
4859  * option: extension type (see DecodeRegExtend)
4860  * imm3: optional shift to Rm
4861  *
4862  * Rd = Rn + LSL(extend(Rm), amount)
4863  */
4864 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4865 {
4866     int rd = extract32(insn, 0, 5);
4867     int rn = extract32(insn, 5, 5);
4868     int imm3 = extract32(insn, 10, 3);
4869     int option = extract32(insn, 13, 3);
4870     int rm = extract32(insn, 16, 5);
4871     int opt = extract32(insn, 22, 2);
4872     bool setflags = extract32(insn, 29, 1);
4873     bool sub_op = extract32(insn, 30, 1);
4874     bool sf = extract32(insn, 31, 1);
4875 
4876     TCGv_i64 tcg_rm, tcg_rn; /* temps */
4877     TCGv_i64 tcg_rd;
4878     TCGv_i64 tcg_result;
4879 
4880     if (imm3 > 4 || opt != 0) {
4881         unallocated_encoding(s);
4882         return;
4883     }
4884 
4885     /* non-flag setting ops may use SP */
4886     if (!setflags) {
4887         tcg_rd = cpu_reg_sp(s, rd);
4888     } else {
4889         tcg_rd = cpu_reg(s, rd);
4890     }
4891     tcg_rn = read_cpu_reg_sp(s, rn, sf);
4892 
4893     tcg_rm = read_cpu_reg(s, rm, sf);
4894     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4895 
4896     tcg_result = tcg_temp_new_i64();
4897 
4898     if (!setflags) {
4899         if (sub_op) {
4900             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4901         } else {
4902             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4903         }
4904     } else {
4905         if (sub_op) {
4906             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4907         } else {
4908             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4909         }
4910     }
4911 
4912     if (sf) {
4913         tcg_gen_mov_i64(tcg_rd, tcg_result);
4914     } else {
4915         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4916     }
4917 }
4918 
4919 /*
4920  * Add/subtract (shifted register)
4921  *
4922  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
4923  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4924  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
4925  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4926  *
4927  *    sf: 0 -> 32bit, 1 -> 64bit
4928  *    op: 0 -> add  , 1 -> sub
4929  *     S: 1 -> set flags
4930  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4931  *  imm6: Shift amount to apply to Rm before the add/sub
4932  */
4933 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4934 {
4935     int rd = extract32(insn, 0, 5);
4936     int rn = extract32(insn, 5, 5);
4937     int imm6 = extract32(insn, 10, 6);
4938     int rm = extract32(insn, 16, 5);
4939     int shift_type = extract32(insn, 22, 2);
4940     bool setflags = extract32(insn, 29, 1);
4941     bool sub_op = extract32(insn, 30, 1);
4942     bool sf = extract32(insn, 31, 1);
4943 
4944     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4945     TCGv_i64 tcg_rn, tcg_rm;
4946     TCGv_i64 tcg_result;
4947 
4948     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4949         unallocated_encoding(s);
4950         return;
4951     }
4952 
4953     tcg_rn = read_cpu_reg(s, rn, sf);
4954     tcg_rm = read_cpu_reg(s, rm, sf);
4955 
4956     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4957 
4958     tcg_result = tcg_temp_new_i64();
4959 
4960     if (!setflags) {
4961         if (sub_op) {
4962             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4963         } else {
4964             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4965         }
4966     } else {
4967         if (sub_op) {
4968             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4969         } else {
4970             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4971         }
4972     }
4973 
4974     if (sf) {
4975         tcg_gen_mov_i64(tcg_rd, tcg_result);
4976     } else {
4977         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4978     }
4979 }
4980 
4981 /* Data-processing (3 source)
4982  *
4983  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
4984  *  +--+------+-----------+------+------+----+------+------+------+
4985  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
4986  *  +--+------+-----------+------+------+----+------+------+------+
4987  */
4988 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4989 {
4990     int rd = extract32(insn, 0, 5);
4991     int rn = extract32(insn, 5, 5);
4992     int ra = extract32(insn, 10, 5);
4993     int rm = extract32(insn, 16, 5);
4994     int op_id = (extract32(insn, 29, 3) << 4) |
4995         (extract32(insn, 21, 3) << 1) |
4996         extract32(insn, 15, 1);
4997     bool sf = extract32(insn, 31, 1);
4998     bool is_sub = extract32(op_id, 0, 1);
4999     bool is_high = extract32(op_id, 2, 1);
5000     bool is_signed = false;
5001     TCGv_i64 tcg_op1;
5002     TCGv_i64 tcg_op2;
5003     TCGv_i64 tcg_tmp;
5004 
5005     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
5006     switch (op_id) {
5007     case 0x42: /* SMADDL */
5008     case 0x43: /* SMSUBL */
5009     case 0x44: /* SMULH */
5010         is_signed = true;
5011         break;
5012     case 0x0: /* MADD (32bit) */
5013     case 0x1: /* MSUB (32bit) */
5014     case 0x40: /* MADD (64bit) */
5015     case 0x41: /* MSUB (64bit) */
5016     case 0x4a: /* UMADDL */
5017     case 0x4b: /* UMSUBL */
5018     case 0x4c: /* UMULH */
5019         break;
5020     default:
5021         unallocated_encoding(s);
5022         return;
5023     }
5024 
5025     if (is_high) {
5026         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
5027         TCGv_i64 tcg_rd = cpu_reg(s, rd);
5028         TCGv_i64 tcg_rn = cpu_reg(s, rn);
5029         TCGv_i64 tcg_rm = cpu_reg(s, rm);
5030 
5031         if (is_signed) {
5032             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5033         } else {
5034             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5035         }
5036         return;
5037     }
5038 
5039     tcg_op1 = tcg_temp_new_i64();
5040     tcg_op2 = tcg_temp_new_i64();
5041     tcg_tmp = tcg_temp_new_i64();
5042 
5043     if (op_id < 0x42) {
5044         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
5045         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
5046     } else {
5047         if (is_signed) {
5048             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
5049             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
5050         } else {
5051             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
5052             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
5053         }
5054     }
5055 
5056     if (ra == 31 && !is_sub) {
5057         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
5058         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
5059     } else {
5060         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
5061         if (is_sub) {
5062             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5063         } else {
5064             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5065         }
5066     }
5067 
5068     if (!sf) {
5069         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
5070     }
5071 }
5072 
5073 /* Add/subtract (with carry)
5074  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
5075  * +--+--+--+------------------------+------+-------------+------+-----+
5076  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
5077  * +--+--+--+------------------------+------+-------------+------+-----+
5078  */
5079 
5080 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
5081 {
5082     unsigned int sf, op, setflags, rm, rn, rd;
5083     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
5084 
5085     sf = extract32(insn, 31, 1);
5086     op = extract32(insn, 30, 1);
5087     setflags = extract32(insn, 29, 1);
5088     rm = extract32(insn, 16, 5);
5089     rn = extract32(insn, 5, 5);
5090     rd = extract32(insn, 0, 5);
5091 
5092     tcg_rd = cpu_reg(s, rd);
5093     tcg_rn = cpu_reg(s, rn);
5094 
5095     if (op) {
5096         tcg_y = tcg_temp_new_i64();
5097         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
5098     } else {
5099         tcg_y = cpu_reg(s, rm);
5100     }
5101 
5102     if (setflags) {
5103         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
5104     } else {
5105         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
5106     }
5107 }
5108 
5109 /*
5110  * Rotate right into flags
5111  *  31 30 29                21       15          10      5  4      0
5112  * +--+--+--+-----------------+--------+-----------+------+--+------+
5113  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
5114  * +--+--+--+-----------------+--------+-----------+------+--+------+
5115  */
5116 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
5117 {
5118     int mask = extract32(insn, 0, 4);
5119     int o2 = extract32(insn, 4, 1);
5120     int rn = extract32(insn, 5, 5);
5121     int imm6 = extract32(insn, 15, 6);
5122     int sf_op_s = extract32(insn, 29, 3);
5123     TCGv_i64 tcg_rn;
5124     TCGv_i32 nzcv;
5125 
5126     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
5127         unallocated_encoding(s);
5128         return;
5129     }
5130 
5131     tcg_rn = read_cpu_reg(s, rn, 1);
5132     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
5133 
5134     nzcv = tcg_temp_new_i32();
5135     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
5136 
5137     if (mask & 8) { /* N */
5138         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
5139     }
5140     if (mask & 4) { /* Z */
5141         tcg_gen_not_i32(cpu_ZF, nzcv);
5142         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
5143     }
5144     if (mask & 2) { /* C */
5145         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
5146     }
5147     if (mask & 1) { /* V */
5148         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
5149     }
5150 }
5151 
5152 /*
5153  * Evaluate into flags
5154  *  31 30 29                21        15   14        10      5  4      0
5155  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5156  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
5157  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5158  */
5159 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
5160 {
5161     int o3_mask = extract32(insn, 0, 5);
5162     int rn = extract32(insn, 5, 5);
5163     int o2 = extract32(insn, 15, 6);
5164     int sz = extract32(insn, 14, 1);
5165     int sf_op_s = extract32(insn, 29, 3);
5166     TCGv_i32 tmp;
5167     int shift;
5168 
5169     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
5170         !dc_isar_feature(aa64_condm_4, s)) {
5171         unallocated_encoding(s);
5172         return;
5173     }
5174     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
5175 
5176     tmp = tcg_temp_new_i32();
5177     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
5178     tcg_gen_shli_i32(cpu_NF, tmp, shift);
5179     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
5180     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
5181     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
5182 }
5183 
5184 /* Conditional compare (immediate / register)
5185  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
5186  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5187  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
5188  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5189  *        [1]                             y                [0]       [0]
5190  */
5191 static void disas_cc(DisasContext *s, uint32_t insn)
5192 {
5193     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
5194     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
5195     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
5196     DisasCompare c;
5197 
5198     if (!extract32(insn, 29, 1)) {
5199         unallocated_encoding(s);
5200         return;
5201     }
5202     if (insn & (1 << 10 | 1 << 4)) {
5203         unallocated_encoding(s);
5204         return;
5205     }
5206     sf = extract32(insn, 31, 1);
5207     op = extract32(insn, 30, 1);
5208     is_imm = extract32(insn, 11, 1);
5209     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
5210     cond = extract32(insn, 12, 4);
5211     rn = extract32(insn, 5, 5);
5212     nzcv = extract32(insn, 0, 4);
5213 
5214     /* Set T0 = !COND.  */
5215     tcg_t0 = tcg_temp_new_i32();
5216     arm_test_cc(&c, cond);
5217     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
5218 
5219     /* Load the arguments for the new comparison.  */
5220     if (is_imm) {
5221         tcg_y = tcg_temp_new_i64();
5222         tcg_gen_movi_i64(tcg_y, y);
5223     } else {
5224         tcg_y = cpu_reg(s, y);
5225     }
5226     tcg_rn = cpu_reg(s, rn);
5227 
5228     /* Set the flags for the new comparison.  */
5229     tcg_tmp = tcg_temp_new_i64();
5230     if (op) {
5231         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5232     } else {
5233         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5234     }
5235 
5236     /* If COND was false, force the flags to #nzcv.  Compute two masks
5237      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
5238      * For tcg hosts that support ANDC, we can make do with just T1.
5239      * In either case, allow the tcg optimizer to delete any unused mask.
5240      */
5241     tcg_t1 = tcg_temp_new_i32();
5242     tcg_t2 = tcg_temp_new_i32();
5243     tcg_gen_neg_i32(tcg_t1, tcg_t0);
5244     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
5245 
5246     if (nzcv & 8) { /* N */
5247         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
5248     } else {
5249         if (TCG_TARGET_HAS_andc_i32) {
5250             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
5251         } else {
5252             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
5253         }
5254     }
5255     if (nzcv & 4) { /* Z */
5256         if (TCG_TARGET_HAS_andc_i32) {
5257             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
5258         } else {
5259             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
5260         }
5261     } else {
5262         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
5263     }
5264     if (nzcv & 2) { /* C */
5265         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
5266     } else {
5267         if (TCG_TARGET_HAS_andc_i32) {
5268             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
5269         } else {
5270             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
5271         }
5272     }
5273     if (nzcv & 1) { /* V */
5274         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
5275     } else {
5276         if (TCG_TARGET_HAS_andc_i32) {
5277             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
5278         } else {
5279             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
5280         }
5281     }
5282 }
5283 
5284 /* Conditional select
5285  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
5286  * +----+----+---+-----------------+------+------+-----+------+------+
5287  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
5288  * +----+----+---+-----------------+------+------+-----+------+------+
5289  */
5290 static void disas_cond_select(DisasContext *s, uint32_t insn)
5291 {
5292     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
5293     TCGv_i64 tcg_rd, zero;
5294     DisasCompare64 c;
5295 
5296     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
5297         /* S == 1 or op2<1> == 1 */
5298         unallocated_encoding(s);
5299         return;
5300     }
5301     sf = extract32(insn, 31, 1);
5302     else_inv = extract32(insn, 30, 1);
5303     rm = extract32(insn, 16, 5);
5304     cond = extract32(insn, 12, 4);
5305     else_inc = extract32(insn, 10, 1);
5306     rn = extract32(insn, 5, 5);
5307     rd = extract32(insn, 0, 5);
5308 
5309     tcg_rd = cpu_reg(s, rd);
5310 
5311     a64_test_cc(&c, cond);
5312     zero = tcg_constant_i64(0);
5313 
5314     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
5315         /* CSET & CSETM.  */
5316         tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
5317         if (else_inv) {
5318             tcg_gen_neg_i64(tcg_rd, tcg_rd);
5319         }
5320     } else {
5321         TCGv_i64 t_true = cpu_reg(s, rn);
5322         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
5323         if (else_inv && else_inc) {
5324             tcg_gen_neg_i64(t_false, t_false);
5325         } else if (else_inv) {
5326             tcg_gen_not_i64(t_false, t_false);
5327         } else if (else_inc) {
5328             tcg_gen_addi_i64(t_false, t_false, 1);
5329         }
5330         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
5331     }
5332 
5333     if (!sf) {
5334         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5335     }
5336 }
5337 
5338 static void handle_clz(DisasContext *s, unsigned int sf,
5339                        unsigned int rn, unsigned int rd)
5340 {
5341     TCGv_i64 tcg_rd, tcg_rn;
5342     tcg_rd = cpu_reg(s, rd);
5343     tcg_rn = cpu_reg(s, rn);
5344 
5345     if (sf) {
5346         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
5347     } else {
5348         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5349         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5350         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
5351         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5352     }
5353 }
5354 
5355 static void handle_cls(DisasContext *s, unsigned int sf,
5356                        unsigned int rn, unsigned int rd)
5357 {
5358     TCGv_i64 tcg_rd, tcg_rn;
5359     tcg_rd = cpu_reg(s, rd);
5360     tcg_rn = cpu_reg(s, rn);
5361 
5362     if (sf) {
5363         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
5364     } else {
5365         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5366         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5367         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
5368         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5369     }
5370 }
5371 
5372 static void handle_rbit(DisasContext *s, unsigned int sf,
5373                         unsigned int rn, unsigned int rd)
5374 {
5375     TCGv_i64 tcg_rd, tcg_rn;
5376     tcg_rd = cpu_reg(s, rd);
5377     tcg_rn = cpu_reg(s, rn);
5378 
5379     if (sf) {
5380         gen_helper_rbit64(tcg_rd, tcg_rn);
5381     } else {
5382         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5383         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5384         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5385         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5386     }
5387 }
5388 
5389 /* REV with sf==1, opcode==3 ("REV64") */
5390 static void handle_rev64(DisasContext *s, unsigned int sf,
5391                          unsigned int rn, unsigned int rd)
5392 {
5393     if (!sf) {
5394         unallocated_encoding(s);
5395         return;
5396     }
5397     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5398 }
5399 
5400 /* REV with sf==0, opcode==2
5401  * REV32 (sf==1, opcode==2)
5402  */
5403 static void handle_rev32(DisasContext *s, unsigned int sf,
5404                          unsigned int rn, unsigned int rd)
5405 {
5406     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5407     TCGv_i64 tcg_rn = cpu_reg(s, rn);
5408 
5409     if (sf) {
5410         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
5411         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
5412     } else {
5413         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
5414     }
5415 }
5416 
5417 /* REV16 (opcode==1) */
5418 static void handle_rev16(DisasContext *s, unsigned int sf,
5419                          unsigned int rn, unsigned int rd)
5420 {
5421     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5422     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5423     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5424     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5425 
5426     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5427     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5428     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5429     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5430     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5431 }
5432 
5433 /* Data-processing (1 source)
5434  *   31  30  29  28             21 20     16 15    10 9    5 4    0
5435  * +----+---+---+-----------------+---------+--------+------+------+
5436  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
5437  * +----+---+---+-----------------+---------+--------+------+------+
5438  */
5439 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5440 {
5441     unsigned int sf, opcode, opcode2, rn, rd;
5442     TCGv_i64 tcg_rd;
5443 
5444     if (extract32(insn, 29, 1)) {
5445         unallocated_encoding(s);
5446         return;
5447     }
5448 
5449     sf = extract32(insn, 31, 1);
5450     opcode = extract32(insn, 10, 6);
5451     opcode2 = extract32(insn, 16, 5);
5452     rn = extract32(insn, 5, 5);
5453     rd = extract32(insn, 0, 5);
5454 
5455 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5456 
5457     switch (MAP(sf, opcode2, opcode)) {
5458     case MAP(0, 0x00, 0x00): /* RBIT */
5459     case MAP(1, 0x00, 0x00):
5460         handle_rbit(s, sf, rn, rd);
5461         break;
5462     case MAP(0, 0x00, 0x01): /* REV16 */
5463     case MAP(1, 0x00, 0x01):
5464         handle_rev16(s, sf, rn, rd);
5465         break;
5466     case MAP(0, 0x00, 0x02): /* REV/REV32 */
5467     case MAP(1, 0x00, 0x02):
5468         handle_rev32(s, sf, rn, rd);
5469         break;
5470     case MAP(1, 0x00, 0x03): /* REV64 */
5471         handle_rev64(s, sf, rn, rd);
5472         break;
5473     case MAP(0, 0x00, 0x04): /* CLZ */
5474     case MAP(1, 0x00, 0x04):
5475         handle_clz(s, sf, rn, rd);
5476         break;
5477     case MAP(0, 0x00, 0x05): /* CLS */
5478     case MAP(1, 0x00, 0x05):
5479         handle_cls(s, sf, rn, rd);
5480         break;
5481     case MAP(1, 0x01, 0x00): /* PACIA */
5482         if (s->pauth_active) {
5483             tcg_rd = cpu_reg(s, rd);
5484             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5485         } else if (!dc_isar_feature(aa64_pauth, s)) {
5486             goto do_unallocated;
5487         }
5488         break;
5489     case MAP(1, 0x01, 0x01): /* PACIB */
5490         if (s->pauth_active) {
5491             tcg_rd = cpu_reg(s, rd);
5492             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5493         } else if (!dc_isar_feature(aa64_pauth, s)) {
5494             goto do_unallocated;
5495         }
5496         break;
5497     case MAP(1, 0x01, 0x02): /* PACDA */
5498         if (s->pauth_active) {
5499             tcg_rd = cpu_reg(s, rd);
5500             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5501         } else if (!dc_isar_feature(aa64_pauth, s)) {
5502             goto do_unallocated;
5503         }
5504         break;
5505     case MAP(1, 0x01, 0x03): /* PACDB */
5506         if (s->pauth_active) {
5507             tcg_rd = cpu_reg(s, rd);
5508             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5509         } else if (!dc_isar_feature(aa64_pauth, s)) {
5510             goto do_unallocated;
5511         }
5512         break;
5513     case MAP(1, 0x01, 0x04): /* AUTIA */
5514         if (s->pauth_active) {
5515             tcg_rd = cpu_reg(s, rd);
5516             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5517         } else if (!dc_isar_feature(aa64_pauth, s)) {
5518             goto do_unallocated;
5519         }
5520         break;
5521     case MAP(1, 0x01, 0x05): /* AUTIB */
5522         if (s->pauth_active) {
5523             tcg_rd = cpu_reg(s, rd);
5524             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5525         } else if (!dc_isar_feature(aa64_pauth, s)) {
5526             goto do_unallocated;
5527         }
5528         break;
5529     case MAP(1, 0x01, 0x06): /* AUTDA */
5530         if (s->pauth_active) {
5531             tcg_rd = cpu_reg(s, rd);
5532             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5533         } else if (!dc_isar_feature(aa64_pauth, s)) {
5534             goto do_unallocated;
5535         }
5536         break;
5537     case MAP(1, 0x01, 0x07): /* AUTDB */
5538         if (s->pauth_active) {
5539             tcg_rd = cpu_reg(s, rd);
5540             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5541         } else if (!dc_isar_feature(aa64_pauth, s)) {
5542             goto do_unallocated;
5543         }
5544         break;
5545     case MAP(1, 0x01, 0x08): /* PACIZA */
5546         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5547             goto do_unallocated;
5548         } else if (s->pauth_active) {
5549             tcg_rd = cpu_reg(s, rd);
5550             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5551         }
5552         break;
5553     case MAP(1, 0x01, 0x09): /* PACIZB */
5554         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5555             goto do_unallocated;
5556         } else if (s->pauth_active) {
5557             tcg_rd = cpu_reg(s, rd);
5558             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5559         }
5560         break;
5561     case MAP(1, 0x01, 0x0a): /* PACDZA */
5562         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5563             goto do_unallocated;
5564         } else if (s->pauth_active) {
5565             tcg_rd = cpu_reg(s, rd);
5566             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5567         }
5568         break;
5569     case MAP(1, 0x01, 0x0b): /* PACDZB */
5570         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5571             goto do_unallocated;
5572         } else if (s->pauth_active) {
5573             tcg_rd = cpu_reg(s, rd);
5574             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5575         }
5576         break;
5577     case MAP(1, 0x01, 0x0c): /* AUTIZA */
5578         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5579             goto do_unallocated;
5580         } else if (s->pauth_active) {
5581             tcg_rd = cpu_reg(s, rd);
5582             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5583         }
5584         break;
5585     case MAP(1, 0x01, 0x0d): /* AUTIZB */
5586         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5587             goto do_unallocated;
5588         } else if (s->pauth_active) {
5589             tcg_rd = cpu_reg(s, rd);
5590             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5591         }
5592         break;
5593     case MAP(1, 0x01, 0x0e): /* AUTDZA */
5594         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5595             goto do_unallocated;
5596         } else if (s->pauth_active) {
5597             tcg_rd = cpu_reg(s, rd);
5598             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5599         }
5600         break;
5601     case MAP(1, 0x01, 0x0f): /* AUTDZB */
5602         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5603             goto do_unallocated;
5604         } else if (s->pauth_active) {
5605             tcg_rd = cpu_reg(s, rd);
5606             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5607         }
5608         break;
5609     case MAP(1, 0x01, 0x10): /* XPACI */
5610         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5611             goto do_unallocated;
5612         } else if (s->pauth_active) {
5613             tcg_rd = cpu_reg(s, rd);
5614             gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
5615         }
5616         break;
5617     case MAP(1, 0x01, 0x11): /* XPACD */
5618         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5619             goto do_unallocated;
5620         } else if (s->pauth_active) {
5621             tcg_rd = cpu_reg(s, rd);
5622             gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5623         }
5624         break;
5625     default:
5626     do_unallocated:
5627         unallocated_encoding(s);
5628         break;
5629     }
5630 
5631 #undef MAP
5632 }
5633 
5634 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5635                        unsigned int rm, unsigned int rn, unsigned int rd)
5636 {
5637     TCGv_i64 tcg_n, tcg_m, tcg_rd;
5638     tcg_rd = cpu_reg(s, rd);
5639 
5640     if (!sf && is_signed) {
5641         tcg_n = tcg_temp_new_i64();
5642         tcg_m = tcg_temp_new_i64();
5643         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5644         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5645     } else {
5646         tcg_n = read_cpu_reg(s, rn, sf);
5647         tcg_m = read_cpu_reg(s, rm, sf);
5648     }
5649 
5650     if (is_signed) {
5651         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5652     } else {
5653         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5654     }
5655 
5656     if (!sf) { /* zero extend final result */
5657         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5658     }
5659 }
5660 
5661 /* LSLV, LSRV, ASRV, RORV */
5662 static void handle_shift_reg(DisasContext *s,
5663                              enum a64_shift_type shift_type, unsigned int sf,
5664                              unsigned int rm, unsigned int rn, unsigned int rd)
5665 {
5666     TCGv_i64 tcg_shift = tcg_temp_new_i64();
5667     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5668     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5669 
5670     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5671     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5672 }
5673 
5674 /* CRC32[BHWX], CRC32C[BHWX] */
5675 static void handle_crc32(DisasContext *s,
5676                          unsigned int sf, unsigned int sz, bool crc32c,
5677                          unsigned int rm, unsigned int rn, unsigned int rd)
5678 {
5679     TCGv_i64 tcg_acc, tcg_val;
5680     TCGv_i32 tcg_bytes;
5681 
5682     if (!dc_isar_feature(aa64_crc32, s)
5683         || (sf == 1 && sz != 3)
5684         || (sf == 0 && sz == 3)) {
5685         unallocated_encoding(s);
5686         return;
5687     }
5688 
5689     if (sz == 3) {
5690         tcg_val = cpu_reg(s, rm);
5691     } else {
5692         uint64_t mask;
5693         switch (sz) {
5694         case 0:
5695             mask = 0xFF;
5696             break;
5697         case 1:
5698             mask = 0xFFFF;
5699             break;
5700         case 2:
5701             mask = 0xFFFFFFFF;
5702             break;
5703         default:
5704             g_assert_not_reached();
5705         }
5706         tcg_val = tcg_temp_new_i64();
5707         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5708     }
5709 
5710     tcg_acc = cpu_reg(s, rn);
5711     tcg_bytes = tcg_constant_i32(1 << sz);
5712 
5713     if (crc32c) {
5714         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5715     } else {
5716         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5717     }
5718 }
5719 
5720 /* Data-processing (2 source)
5721  *   31   30  29 28             21 20  16 15    10 9    5 4    0
5722  * +----+---+---+-----------------+------+--------+------+------+
5723  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
5724  * +----+---+---+-----------------+------+--------+------+------+
5725  */
5726 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5727 {
5728     unsigned int sf, rm, opcode, rn, rd, setflag;
5729     sf = extract32(insn, 31, 1);
5730     setflag = extract32(insn, 29, 1);
5731     rm = extract32(insn, 16, 5);
5732     opcode = extract32(insn, 10, 6);
5733     rn = extract32(insn, 5, 5);
5734     rd = extract32(insn, 0, 5);
5735 
5736     if (setflag && opcode != 0) {
5737         unallocated_encoding(s);
5738         return;
5739     }
5740 
5741     switch (opcode) {
5742     case 0: /* SUBP(S) */
5743         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5744             goto do_unallocated;
5745         } else {
5746             TCGv_i64 tcg_n, tcg_m, tcg_d;
5747 
5748             tcg_n = read_cpu_reg_sp(s, rn, true);
5749             tcg_m = read_cpu_reg_sp(s, rm, true);
5750             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5751             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5752             tcg_d = cpu_reg(s, rd);
5753 
5754             if (setflag) {
5755                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5756             } else {
5757                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5758             }
5759         }
5760         break;
5761     case 2: /* UDIV */
5762         handle_div(s, false, sf, rm, rn, rd);
5763         break;
5764     case 3: /* SDIV */
5765         handle_div(s, true, sf, rm, rn, rd);
5766         break;
5767     case 4: /* IRG */
5768         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5769             goto do_unallocated;
5770         }
5771         if (s->ata) {
5772             gen_helper_irg(cpu_reg_sp(s, rd), cpu_env,
5773                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
5774         } else {
5775             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5776                                              cpu_reg_sp(s, rn));
5777         }
5778         break;
5779     case 5: /* GMI */
5780         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5781             goto do_unallocated;
5782         } else {
5783             TCGv_i64 t = tcg_temp_new_i64();
5784 
5785             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
5786             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
5787             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
5788         }
5789         break;
5790     case 8: /* LSLV */
5791         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5792         break;
5793     case 9: /* LSRV */
5794         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5795         break;
5796     case 10: /* ASRV */
5797         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5798         break;
5799     case 11: /* RORV */
5800         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5801         break;
5802     case 12: /* PACGA */
5803         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5804             goto do_unallocated;
5805         }
5806         gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5807                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
5808         break;
5809     case 16:
5810     case 17:
5811     case 18:
5812     case 19:
5813     case 20:
5814     case 21:
5815     case 22:
5816     case 23: /* CRC32 */
5817     {
5818         int sz = extract32(opcode, 0, 2);
5819         bool crc32c = extract32(opcode, 2, 1);
5820         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5821         break;
5822     }
5823     default:
5824     do_unallocated:
5825         unallocated_encoding(s);
5826         break;
5827     }
5828 }
5829 
5830 /*
5831  * Data processing - register
5832  *  31  30 29  28      25    21  20  16      10         0
5833  * +--+---+--+---+-------+-----+-------+-------+---------+
5834  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
5835  * +--+---+--+---+-------+-----+-------+-------+---------+
5836  */
5837 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5838 {
5839     int op0 = extract32(insn, 30, 1);
5840     int op1 = extract32(insn, 28, 1);
5841     int op2 = extract32(insn, 21, 4);
5842     int op3 = extract32(insn, 10, 6);
5843 
5844     if (!op1) {
5845         if (op2 & 8) {
5846             if (op2 & 1) {
5847                 /* Add/sub (extended register) */
5848                 disas_add_sub_ext_reg(s, insn);
5849             } else {
5850                 /* Add/sub (shifted register) */
5851                 disas_add_sub_reg(s, insn);
5852             }
5853         } else {
5854             /* Logical (shifted register) */
5855             disas_logic_reg(s, insn);
5856         }
5857         return;
5858     }
5859 
5860     switch (op2) {
5861     case 0x0:
5862         switch (op3) {
5863         case 0x00: /* Add/subtract (with carry) */
5864             disas_adc_sbc(s, insn);
5865             break;
5866 
5867         case 0x01: /* Rotate right into flags */
5868         case 0x21:
5869             disas_rotate_right_into_flags(s, insn);
5870             break;
5871 
5872         case 0x02: /* Evaluate into flags */
5873         case 0x12:
5874         case 0x22:
5875         case 0x32:
5876             disas_evaluate_into_flags(s, insn);
5877             break;
5878 
5879         default:
5880             goto do_unallocated;
5881         }
5882         break;
5883 
5884     case 0x2: /* Conditional compare */
5885         disas_cc(s, insn); /* both imm and reg forms */
5886         break;
5887 
5888     case 0x4: /* Conditional select */
5889         disas_cond_select(s, insn);
5890         break;
5891 
5892     case 0x6: /* Data-processing */
5893         if (op0) {    /* (1 source) */
5894             disas_data_proc_1src(s, insn);
5895         } else {      /* (2 source) */
5896             disas_data_proc_2src(s, insn);
5897         }
5898         break;
5899     case 0x8 ... 0xf: /* (3 source) */
5900         disas_data_proc_3src(s, insn);
5901         break;
5902 
5903     default:
5904     do_unallocated:
5905         unallocated_encoding(s);
5906         break;
5907     }
5908 }
5909 
5910 static void handle_fp_compare(DisasContext *s, int size,
5911                               unsigned int rn, unsigned int rm,
5912                               bool cmp_with_zero, bool signal_all_nans)
5913 {
5914     TCGv_i64 tcg_flags = tcg_temp_new_i64();
5915     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
5916 
5917     if (size == MO_64) {
5918         TCGv_i64 tcg_vn, tcg_vm;
5919 
5920         tcg_vn = read_fp_dreg(s, rn);
5921         if (cmp_with_zero) {
5922             tcg_vm = tcg_constant_i64(0);
5923         } else {
5924             tcg_vm = read_fp_dreg(s, rm);
5925         }
5926         if (signal_all_nans) {
5927             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5928         } else {
5929             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5930         }
5931     } else {
5932         TCGv_i32 tcg_vn = tcg_temp_new_i32();
5933         TCGv_i32 tcg_vm = tcg_temp_new_i32();
5934 
5935         read_vec_element_i32(s, tcg_vn, rn, 0, size);
5936         if (cmp_with_zero) {
5937             tcg_gen_movi_i32(tcg_vm, 0);
5938         } else {
5939             read_vec_element_i32(s, tcg_vm, rm, 0, size);
5940         }
5941 
5942         switch (size) {
5943         case MO_32:
5944             if (signal_all_nans) {
5945                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5946             } else {
5947                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5948             }
5949             break;
5950         case MO_16:
5951             if (signal_all_nans) {
5952                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5953             } else {
5954                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5955             }
5956             break;
5957         default:
5958             g_assert_not_reached();
5959         }
5960     }
5961 
5962     gen_set_nzcv(tcg_flags);
5963 }
5964 
5965 /* Floating point compare
5966  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
5967  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5968  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
5969  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5970  */
5971 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5972 {
5973     unsigned int mos, type, rm, op, rn, opc, op2r;
5974     int size;
5975 
5976     mos = extract32(insn, 29, 3);
5977     type = extract32(insn, 22, 2);
5978     rm = extract32(insn, 16, 5);
5979     op = extract32(insn, 14, 2);
5980     rn = extract32(insn, 5, 5);
5981     opc = extract32(insn, 3, 2);
5982     op2r = extract32(insn, 0, 3);
5983 
5984     if (mos || op || op2r) {
5985         unallocated_encoding(s);
5986         return;
5987     }
5988 
5989     switch (type) {
5990     case 0:
5991         size = MO_32;
5992         break;
5993     case 1:
5994         size = MO_64;
5995         break;
5996     case 3:
5997         size = MO_16;
5998         if (dc_isar_feature(aa64_fp16, s)) {
5999             break;
6000         }
6001         /* fallthru */
6002     default:
6003         unallocated_encoding(s);
6004         return;
6005     }
6006 
6007     if (!fp_access_check(s)) {
6008         return;
6009     }
6010 
6011     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
6012 }
6013 
6014 /* Floating point conditional compare
6015  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
6016  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6017  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
6018  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6019  */
6020 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
6021 {
6022     unsigned int mos, type, rm, cond, rn, op, nzcv;
6023     TCGLabel *label_continue = NULL;
6024     int size;
6025 
6026     mos = extract32(insn, 29, 3);
6027     type = extract32(insn, 22, 2);
6028     rm = extract32(insn, 16, 5);
6029     cond = extract32(insn, 12, 4);
6030     rn = extract32(insn, 5, 5);
6031     op = extract32(insn, 4, 1);
6032     nzcv = extract32(insn, 0, 4);
6033 
6034     if (mos) {
6035         unallocated_encoding(s);
6036         return;
6037     }
6038 
6039     switch (type) {
6040     case 0:
6041         size = MO_32;
6042         break;
6043     case 1:
6044         size = MO_64;
6045         break;
6046     case 3:
6047         size = MO_16;
6048         if (dc_isar_feature(aa64_fp16, s)) {
6049             break;
6050         }
6051         /* fallthru */
6052     default:
6053         unallocated_encoding(s);
6054         return;
6055     }
6056 
6057     if (!fp_access_check(s)) {
6058         return;
6059     }
6060 
6061     if (cond < 0x0e) { /* not always */
6062         TCGLabel *label_match = gen_new_label();
6063         label_continue = gen_new_label();
6064         arm_gen_test_cc(cond, label_match);
6065         /* nomatch: */
6066         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
6067         tcg_gen_br(label_continue);
6068         gen_set_label(label_match);
6069     }
6070 
6071     handle_fp_compare(s, size, rn, rm, false, op);
6072 
6073     if (cond < 0x0e) {
6074         gen_set_label(label_continue);
6075     }
6076 }
6077 
6078 /* Floating point conditional select
6079  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
6080  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6081  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
6082  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6083  */
6084 static void disas_fp_csel(DisasContext *s, uint32_t insn)
6085 {
6086     unsigned int mos, type, rm, cond, rn, rd;
6087     TCGv_i64 t_true, t_false;
6088     DisasCompare64 c;
6089     MemOp sz;
6090 
6091     mos = extract32(insn, 29, 3);
6092     type = extract32(insn, 22, 2);
6093     rm = extract32(insn, 16, 5);
6094     cond = extract32(insn, 12, 4);
6095     rn = extract32(insn, 5, 5);
6096     rd = extract32(insn, 0, 5);
6097 
6098     if (mos) {
6099         unallocated_encoding(s);
6100         return;
6101     }
6102 
6103     switch (type) {
6104     case 0:
6105         sz = MO_32;
6106         break;
6107     case 1:
6108         sz = MO_64;
6109         break;
6110     case 3:
6111         sz = MO_16;
6112         if (dc_isar_feature(aa64_fp16, s)) {
6113             break;
6114         }
6115         /* fallthru */
6116     default:
6117         unallocated_encoding(s);
6118         return;
6119     }
6120 
6121     if (!fp_access_check(s)) {
6122         return;
6123     }
6124 
6125     /* Zero extend sreg & hreg inputs to 64 bits now.  */
6126     t_true = tcg_temp_new_i64();
6127     t_false = tcg_temp_new_i64();
6128     read_vec_element(s, t_true, rn, 0, sz);
6129     read_vec_element(s, t_false, rm, 0, sz);
6130 
6131     a64_test_cc(&c, cond);
6132     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
6133                         t_true, t_false);
6134 
6135     /* Note that sregs & hregs write back zeros to the high bits,
6136        and we've already done the zero-extension.  */
6137     write_fp_dreg(s, rd, t_true);
6138 }
6139 
6140 /* Floating-point data-processing (1 source) - half precision */
6141 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
6142 {
6143     TCGv_ptr fpst = NULL;
6144     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
6145     TCGv_i32 tcg_res = tcg_temp_new_i32();
6146 
6147     switch (opcode) {
6148     case 0x0: /* FMOV */
6149         tcg_gen_mov_i32(tcg_res, tcg_op);
6150         break;
6151     case 0x1: /* FABS */
6152         tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
6153         break;
6154     case 0x2: /* FNEG */
6155         tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
6156         break;
6157     case 0x3: /* FSQRT */
6158         fpst = fpstatus_ptr(FPST_FPCR_F16);
6159         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
6160         break;
6161     case 0x8: /* FRINTN */
6162     case 0x9: /* FRINTP */
6163     case 0xa: /* FRINTM */
6164     case 0xb: /* FRINTZ */
6165     case 0xc: /* FRINTA */
6166     {
6167         TCGv_i32 tcg_rmode;
6168 
6169         fpst = fpstatus_ptr(FPST_FPCR_F16);
6170         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
6171         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6172         gen_restore_rmode(tcg_rmode, fpst);
6173         break;
6174     }
6175     case 0xe: /* FRINTX */
6176         fpst = fpstatus_ptr(FPST_FPCR_F16);
6177         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
6178         break;
6179     case 0xf: /* FRINTI */
6180         fpst = fpstatus_ptr(FPST_FPCR_F16);
6181         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6182         break;
6183     default:
6184         g_assert_not_reached();
6185     }
6186 
6187     write_fp_sreg(s, rd, tcg_res);
6188 }
6189 
6190 /* Floating-point data-processing (1 source) - single precision */
6191 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
6192 {
6193     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
6194     TCGv_i32 tcg_op, tcg_res;
6195     TCGv_ptr fpst;
6196     int rmode = -1;
6197 
6198     tcg_op = read_fp_sreg(s, rn);
6199     tcg_res = tcg_temp_new_i32();
6200 
6201     switch (opcode) {
6202     case 0x0: /* FMOV */
6203         tcg_gen_mov_i32(tcg_res, tcg_op);
6204         goto done;
6205     case 0x1: /* FABS */
6206         gen_helper_vfp_abss(tcg_res, tcg_op);
6207         goto done;
6208     case 0x2: /* FNEG */
6209         gen_helper_vfp_negs(tcg_res, tcg_op);
6210         goto done;
6211     case 0x3: /* FSQRT */
6212         gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
6213         goto done;
6214     case 0x6: /* BFCVT */
6215         gen_fpst = gen_helper_bfcvt;
6216         break;
6217     case 0x8: /* FRINTN */
6218     case 0x9: /* FRINTP */
6219     case 0xa: /* FRINTM */
6220     case 0xb: /* FRINTZ */
6221     case 0xc: /* FRINTA */
6222         rmode = opcode & 7;
6223         gen_fpst = gen_helper_rints;
6224         break;
6225     case 0xe: /* FRINTX */
6226         gen_fpst = gen_helper_rints_exact;
6227         break;
6228     case 0xf: /* FRINTI */
6229         gen_fpst = gen_helper_rints;
6230         break;
6231     case 0x10: /* FRINT32Z */
6232         rmode = FPROUNDING_ZERO;
6233         gen_fpst = gen_helper_frint32_s;
6234         break;
6235     case 0x11: /* FRINT32X */
6236         gen_fpst = gen_helper_frint32_s;
6237         break;
6238     case 0x12: /* FRINT64Z */
6239         rmode = FPROUNDING_ZERO;
6240         gen_fpst = gen_helper_frint64_s;
6241         break;
6242     case 0x13: /* FRINT64X */
6243         gen_fpst = gen_helper_frint64_s;
6244         break;
6245     default:
6246         g_assert_not_reached();
6247     }
6248 
6249     fpst = fpstatus_ptr(FPST_FPCR);
6250     if (rmode >= 0) {
6251         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6252         gen_fpst(tcg_res, tcg_op, fpst);
6253         gen_restore_rmode(tcg_rmode, fpst);
6254     } else {
6255         gen_fpst(tcg_res, tcg_op, fpst);
6256     }
6257 
6258  done:
6259     write_fp_sreg(s, rd, tcg_res);
6260 }
6261 
6262 /* Floating-point data-processing (1 source) - double precision */
6263 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6264 {
6265     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6266     TCGv_i64 tcg_op, tcg_res;
6267     TCGv_ptr fpst;
6268     int rmode = -1;
6269 
6270     switch (opcode) {
6271     case 0x0: /* FMOV */
6272         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6273         return;
6274     }
6275 
6276     tcg_op = read_fp_dreg(s, rn);
6277     tcg_res = tcg_temp_new_i64();
6278 
6279     switch (opcode) {
6280     case 0x1: /* FABS */
6281         gen_helper_vfp_absd(tcg_res, tcg_op);
6282         goto done;
6283     case 0x2: /* FNEG */
6284         gen_helper_vfp_negd(tcg_res, tcg_op);
6285         goto done;
6286     case 0x3: /* FSQRT */
6287         gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
6288         goto done;
6289     case 0x8: /* FRINTN */
6290     case 0x9: /* FRINTP */
6291     case 0xa: /* FRINTM */
6292     case 0xb: /* FRINTZ */
6293     case 0xc: /* FRINTA */
6294         rmode = opcode & 7;
6295         gen_fpst = gen_helper_rintd;
6296         break;
6297     case 0xe: /* FRINTX */
6298         gen_fpst = gen_helper_rintd_exact;
6299         break;
6300     case 0xf: /* FRINTI */
6301         gen_fpst = gen_helper_rintd;
6302         break;
6303     case 0x10: /* FRINT32Z */
6304         rmode = FPROUNDING_ZERO;
6305         gen_fpst = gen_helper_frint32_d;
6306         break;
6307     case 0x11: /* FRINT32X */
6308         gen_fpst = gen_helper_frint32_d;
6309         break;
6310     case 0x12: /* FRINT64Z */
6311         rmode = FPROUNDING_ZERO;
6312         gen_fpst = gen_helper_frint64_d;
6313         break;
6314     case 0x13: /* FRINT64X */
6315         gen_fpst = gen_helper_frint64_d;
6316         break;
6317     default:
6318         g_assert_not_reached();
6319     }
6320 
6321     fpst = fpstatus_ptr(FPST_FPCR);
6322     if (rmode >= 0) {
6323         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6324         gen_fpst(tcg_res, tcg_op, fpst);
6325         gen_restore_rmode(tcg_rmode, fpst);
6326     } else {
6327         gen_fpst(tcg_res, tcg_op, fpst);
6328     }
6329 
6330  done:
6331     write_fp_dreg(s, rd, tcg_res);
6332 }
6333 
6334 static void handle_fp_fcvt(DisasContext *s, int opcode,
6335                            int rd, int rn, int dtype, int ntype)
6336 {
6337     switch (ntype) {
6338     case 0x0:
6339     {
6340         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6341         if (dtype == 1) {
6342             /* Single to double */
6343             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6344             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
6345             write_fp_dreg(s, rd, tcg_rd);
6346         } else {
6347             /* Single to half */
6348             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6349             TCGv_i32 ahp = get_ahp_flag();
6350             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6351 
6352             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6353             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6354             write_fp_sreg(s, rd, tcg_rd);
6355         }
6356         break;
6357     }
6358     case 0x1:
6359     {
6360         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6361         TCGv_i32 tcg_rd = tcg_temp_new_i32();
6362         if (dtype == 0) {
6363             /* Double to single */
6364             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
6365         } else {
6366             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6367             TCGv_i32 ahp = get_ahp_flag();
6368             /* Double to half */
6369             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6370             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6371         }
6372         write_fp_sreg(s, rd, tcg_rd);
6373         break;
6374     }
6375     case 0x3:
6376     {
6377         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6378         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6379         TCGv_i32 tcg_ahp = get_ahp_flag();
6380         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6381         if (dtype == 0) {
6382             /* Half to single */
6383             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6384             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6385             write_fp_sreg(s, rd, tcg_rd);
6386         } else {
6387             /* Half to double */
6388             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6389             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6390             write_fp_dreg(s, rd, tcg_rd);
6391         }
6392         break;
6393     }
6394     default:
6395         g_assert_not_reached();
6396     }
6397 }
6398 
6399 /* Floating point data-processing (1 source)
6400  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
6401  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6402  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
6403  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6404  */
6405 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6406 {
6407     int mos = extract32(insn, 29, 3);
6408     int type = extract32(insn, 22, 2);
6409     int opcode = extract32(insn, 15, 6);
6410     int rn = extract32(insn, 5, 5);
6411     int rd = extract32(insn, 0, 5);
6412 
6413     if (mos) {
6414         goto do_unallocated;
6415     }
6416 
6417     switch (opcode) {
6418     case 0x4: case 0x5: case 0x7:
6419     {
6420         /* FCVT between half, single and double precision */
6421         int dtype = extract32(opcode, 0, 2);
6422         if (type == 2 || dtype == type) {
6423             goto do_unallocated;
6424         }
6425         if (!fp_access_check(s)) {
6426             return;
6427         }
6428 
6429         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6430         break;
6431     }
6432 
6433     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6434         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6435             goto do_unallocated;
6436         }
6437         /* fall through */
6438     case 0x0 ... 0x3:
6439     case 0x8 ... 0xc:
6440     case 0xe ... 0xf:
6441         /* 32-to-32 and 64-to-64 ops */
6442         switch (type) {
6443         case 0:
6444             if (!fp_access_check(s)) {
6445                 return;
6446             }
6447             handle_fp_1src_single(s, opcode, rd, rn);
6448             break;
6449         case 1:
6450             if (!fp_access_check(s)) {
6451                 return;
6452             }
6453             handle_fp_1src_double(s, opcode, rd, rn);
6454             break;
6455         case 3:
6456             if (!dc_isar_feature(aa64_fp16, s)) {
6457                 goto do_unallocated;
6458             }
6459 
6460             if (!fp_access_check(s)) {
6461                 return;
6462             }
6463             handle_fp_1src_half(s, opcode, rd, rn);
6464             break;
6465         default:
6466             goto do_unallocated;
6467         }
6468         break;
6469 
6470     case 0x6:
6471         switch (type) {
6472         case 1: /* BFCVT */
6473             if (!dc_isar_feature(aa64_bf16, s)) {
6474                 goto do_unallocated;
6475             }
6476             if (!fp_access_check(s)) {
6477                 return;
6478             }
6479             handle_fp_1src_single(s, opcode, rd, rn);
6480             break;
6481         default:
6482             goto do_unallocated;
6483         }
6484         break;
6485 
6486     default:
6487     do_unallocated:
6488         unallocated_encoding(s);
6489         break;
6490     }
6491 }
6492 
6493 /* Floating-point data-processing (2 source) - single precision */
6494 static void handle_fp_2src_single(DisasContext *s, int opcode,
6495                                   int rd, int rn, int rm)
6496 {
6497     TCGv_i32 tcg_op1;
6498     TCGv_i32 tcg_op2;
6499     TCGv_i32 tcg_res;
6500     TCGv_ptr fpst;
6501 
6502     tcg_res = tcg_temp_new_i32();
6503     fpst = fpstatus_ptr(FPST_FPCR);
6504     tcg_op1 = read_fp_sreg(s, rn);
6505     tcg_op2 = read_fp_sreg(s, rm);
6506 
6507     switch (opcode) {
6508     case 0x0: /* FMUL */
6509         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6510         break;
6511     case 0x1: /* FDIV */
6512         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6513         break;
6514     case 0x2: /* FADD */
6515         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6516         break;
6517     case 0x3: /* FSUB */
6518         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6519         break;
6520     case 0x4: /* FMAX */
6521         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6522         break;
6523     case 0x5: /* FMIN */
6524         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6525         break;
6526     case 0x6: /* FMAXNM */
6527         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6528         break;
6529     case 0x7: /* FMINNM */
6530         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6531         break;
6532     case 0x8: /* FNMUL */
6533         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6534         gen_helper_vfp_negs(tcg_res, tcg_res);
6535         break;
6536     }
6537 
6538     write_fp_sreg(s, rd, tcg_res);
6539 }
6540 
6541 /* Floating-point data-processing (2 source) - double precision */
6542 static void handle_fp_2src_double(DisasContext *s, int opcode,
6543                                   int rd, int rn, int rm)
6544 {
6545     TCGv_i64 tcg_op1;
6546     TCGv_i64 tcg_op2;
6547     TCGv_i64 tcg_res;
6548     TCGv_ptr fpst;
6549 
6550     tcg_res = tcg_temp_new_i64();
6551     fpst = fpstatus_ptr(FPST_FPCR);
6552     tcg_op1 = read_fp_dreg(s, rn);
6553     tcg_op2 = read_fp_dreg(s, rm);
6554 
6555     switch (opcode) {
6556     case 0x0: /* FMUL */
6557         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6558         break;
6559     case 0x1: /* FDIV */
6560         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6561         break;
6562     case 0x2: /* FADD */
6563         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6564         break;
6565     case 0x3: /* FSUB */
6566         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6567         break;
6568     case 0x4: /* FMAX */
6569         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6570         break;
6571     case 0x5: /* FMIN */
6572         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6573         break;
6574     case 0x6: /* FMAXNM */
6575         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6576         break;
6577     case 0x7: /* FMINNM */
6578         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6579         break;
6580     case 0x8: /* FNMUL */
6581         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6582         gen_helper_vfp_negd(tcg_res, tcg_res);
6583         break;
6584     }
6585 
6586     write_fp_dreg(s, rd, tcg_res);
6587 }
6588 
6589 /* Floating-point data-processing (2 source) - half precision */
6590 static void handle_fp_2src_half(DisasContext *s, int opcode,
6591                                 int rd, int rn, int rm)
6592 {
6593     TCGv_i32 tcg_op1;
6594     TCGv_i32 tcg_op2;
6595     TCGv_i32 tcg_res;
6596     TCGv_ptr fpst;
6597 
6598     tcg_res = tcg_temp_new_i32();
6599     fpst = fpstatus_ptr(FPST_FPCR_F16);
6600     tcg_op1 = read_fp_hreg(s, rn);
6601     tcg_op2 = read_fp_hreg(s, rm);
6602 
6603     switch (opcode) {
6604     case 0x0: /* FMUL */
6605         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6606         break;
6607     case 0x1: /* FDIV */
6608         gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6609         break;
6610     case 0x2: /* FADD */
6611         gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6612         break;
6613     case 0x3: /* FSUB */
6614         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6615         break;
6616     case 0x4: /* FMAX */
6617         gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6618         break;
6619     case 0x5: /* FMIN */
6620         gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6621         break;
6622     case 0x6: /* FMAXNM */
6623         gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6624         break;
6625     case 0x7: /* FMINNM */
6626         gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6627         break;
6628     case 0x8: /* FNMUL */
6629         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6630         tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6631         break;
6632     default:
6633         g_assert_not_reached();
6634     }
6635 
6636     write_fp_sreg(s, rd, tcg_res);
6637 }
6638 
6639 /* Floating point data-processing (2 source)
6640  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
6641  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6642  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
6643  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6644  */
6645 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6646 {
6647     int mos = extract32(insn, 29, 3);
6648     int type = extract32(insn, 22, 2);
6649     int rd = extract32(insn, 0, 5);
6650     int rn = extract32(insn, 5, 5);
6651     int rm = extract32(insn, 16, 5);
6652     int opcode = extract32(insn, 12, 4);
6653 
6654     if (opcode > 8 || mos) {
6655         unallocated_encoding(s);
6656         return;
6657     }
6658 
6659     switch (type) {
6660     case 0:
6661         if (!fp_access_check(s)) {
6662             return;
6663         }
6664         handle_fp_2src_single(s, opcode, rd, rn, rm);
6665         break;
6666     case 1:
6667         if (!fp_access_check(s)) {
6668             return;
6669         }
6670         handle_fp_2src_double(s, opcode, rd, rn, rm);
6671         break;
6672     case 3:
6673         if (!dc_isar_feature(aa64_fp16, s)) {
6674             unallocated_encoding(s);
6675             return;
6676         }
6677         if (!fp_access_check(s)) {
6678             return;
6679         }
6680         handle_fp_2src_half(s, opcode, rd, rn, rm);
6681         break;
6682     default:
6683         unallocated_encoding(s);
6684     }
6685 }
6686 
6687 /* Floating-point data-processing (3 source) - single precision */
6688 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6689                                   int rd, int rn, int rm, int ra)
6690 {
6691     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6692     TCGv_i32 tcg_res = tcg_temp_new_i32();
6693     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6694 
6695     tcg_op1 = read_fp_sreg(s, rn);
6696     tcg_op2 = read_fp_sreg(s, rm);
6697     tcg_op3 = read_fp_sreg(s, ra);
6698 
6699     /* These are fused multiply-add, and must be done as one
6700      * floating point operation with no rounding between the
6701      * multiplication and addition steps.
6702      * NB that doing the negations here as separate steps is
6703      * correct : an input NaN should come out with its sign bit
6704      * flipped if it is a negated-input.
6705      */
6706     if (o1 == true) {
6707         gen_helper_vfp_negs(tcg_op3, tcg_op3);
6708     }
6709 
6710     if (o0 != o1) {
6711         gen_helper_vfp_negs(tcg_op1, tcg_op1);
6712     }
6713 
6714     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6715 
6716     write_fp_sreg(s, rd, tcg_res);
6717 }
6718 
6719 /* Floating-point data-processing (3 source) - double precision */
6720 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6721                                   int rd, int rn, int rm, int ra)
6722 {
6723     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6724     TCGv_i64 tcg_res = tcg_temp_new_i64();
6725     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6726 
6727     tcg_op1 = read_fp_dreg(s, rn);
6728     tcg_op2 = read_fp_dreg(s, rm);
6729     tcg_op3 = read_fp_dreg(s, ra);
6730 
6731     /* These are fused multiply-add, and must be done as one
6732      * floating point operation with no rounding between the
6733      * multiplication and addition steps.
6734      * NB that doing the negations here as separate steps is
6735      * correct : an input NaN should come out with its sign bit
6736      * flipped if it is a negated-input.
6737      */
6738     if (o1 == true) {
6739         gen_helper_vfp_negd(tcg_op3, tcg_op3);
6740     }
6741 
6742     if (o0 != o1) {
6743         gen_helper_vfp_negd(tcg_op1, tcg_op1);
6744     }
6745 
6746     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6747 
6748     write_fp_dreg(s, rd, tcg_res);
6749 }
6750 
6751 /* Floating-point data-processing (3 source) - half precision */
6752 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6753                                 int rd, int rn, int rm, int ra)
6754 {
6755     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6756     TCGv_i32 tcg_res = tcg_temp_new_i32();
6757     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6758 
6759     tcg_op1 = read_fp_hreg(s, rn);
6760     tcg_op2 = read_fp_hreg(s, rm);
6761     tcg_op3 = read_fp_hreg(s, ra);
6762 
6763     /* These are fused multiply-add, and must be done as one
6764      * floating point operation with no rounding between the
6765      * multiplication and addition steps.
6766      * NB that doing the negations here as separate steps is
6767      * correct : an input NaN should come out with its sign bit
6768      * flipped if it is a negated-input.
6769      */
6770     if (o1 == true) {
6771         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6772     }
6773 
6774     if (o0 != o1) {
6775         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6776     }
6777 
6778     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6779 
6780     write_fp_sreg(s, rd, tcg_res);
6781 }
6782 
6783 /* Floating point data-processing (3 source)
6784  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
6785  * +---+---+---+-----------+------+----+------+----+------+------+------+
6786  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6787  * +---+---+---+-----------+------+----+------+----+------+------+------+
6788  */
6789 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6790 {
6791     int mos = extract32(insn, 29, 3);
6792     int type = extract32(insn, 22, 2);
6793     int rd = extract32(insn, 0, 5);
6794     int rn = extract32(insn, 5, 5);
6795     int ra = extract32(insn, 10, 5);
6796     int rm = extract32(insn, 16, 5);
6797     bool o0 = extract32(insn, 15, 1);
6798     bool o1 = extract32(insn, 21, 1);
6799 
6800     if (mos) {
6801         unallocated_encoding(s);
6802         return;
6803     }
6804 
6805     switch (type) {
6806     case 0:
6807         if (!fp_access_check(s)) {
6808             return;
6809         }
6810         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6811         break;
6812     case 1:
6813         if (!fp_access_check(s)) {
6814             return;
6815         }
6816         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6817         break;
6818     case 3:
6819         if (!dc_isar_feature(aa64_fp16, s)) {
6820             unallocated_encoding(s);
6821             return;
6822         }
6823         if (!fp_access_check(s)) {
6824             return;
6825         }
6826         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6827         break;
6828     default:
6829         unallocated_encoding(s);
6830     }
6831 }
6832 
6833 /* Floating point immediate
6834  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
6835  * +---+---+---+-----------+------+---+------------+-------+------+------+
6836  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
6837  * +---+---+---+-----------+------+---+------------+-------+------+------+
6838  */
6839 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6840 {
6841     int rd = extract32(insn, 0, 5);
6842     int imm5 = extract32(insn, 5, 5);
6843     int imm8 = extract32(insn, 13, 8);
6844     int type = extract32(insn, 22, 2);
6845     int mos = extract32(insn, 29, 3);
6846     uint64_t imm;
6847     MemOp sz;
6848 
6849     if (mos || imm5) {
6850         unallocated_encoding(s);
6851         return;
6852     }
6853 
6854     switch (type) {
6855     case 0:
6856         sz = MO_32;
6857         break;
6858     case 1:
6859         sz = MO_64;
6860         break;
6861     case 3:
6862         sz = MO_16;
6863         if (dc_isar_feature(aa64_fp16, s)) {
6864             break;
6865         }
6866         /* fallthru */
6867     default:
6868         unallocated_encoding(s);
6869         return;
6870     }
6871 
6872     if (!fp_access_check(s)) {
6873         return;
6874     }
6875 
6876     imm = vfp_expand_imm(sz, imm8);
6877     write_fp_dreg(s, rd, tcg_constant_i64(imm));
6878 }
6879 
6880 /* Handle floating point <=> fixed point conversions. Note that we can
6881  * also deal with fp <=> integer conversions as a special case (scale == 64)
6882  * OPTME: consider handling that special case specially or at least skipping
6883  * the call to scalbn in the helpers for zero shifts.
6884  */
6885 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6886                            bool itof, int rmode, int scale, int sf, int type)
6887 {
6888     bool is_signed = !(opcode & 1);
6889     TCGv_ptr tcg_fpstatus;
6890     TCGv_i32 tcg_shift, tcg_single;
6891     TCGv_i64 tcg_double;
6892 
6893     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
6894 
6895     tcg_shift = tcg_constant_i32(64 - scale);
6896 
6897     if (itof) {
6898         TCGv_i64 tcg_int = cpu_reg(s, rn);
6899         if (!sf) {
6900             TCGv_i64 tcg_extend = tcg_temp_new_i64();
6901 
6902             if (is_signed) {
6903                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6904             } else {
6905                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6906             }
6907 
6908             tcg_int = tcg_extend;
6909         }
6910 
6911         switch (type) {
6912         case 1: /* float64 */
6913             tcg_double = tcg_temp_new_i64();
6914             if (is_signed) {
6915                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6916                                      tcg_shift, tcg_fpstatus);
6917             } else {
6918                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6919                                      tcg_shift, tcg_fpstatus);
6920             }
6921             write_fp_dreg(s, rd, tcg_double);
6922             break;
6923 
6924         case 0: /* float32 */
6925             tcg_single = tcg_temp_new_i32();
6926             if (is_signed) {
6927                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6928                                      tcg_shift, tcg_fpstatus);
6929             } else {
6930                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6931                                      tcg_shift, tcg_fpstatus);
6932             }
6933             write_fp_sreg(s, rd, tcg_single);
6934             break;
6935 
6936         case 3: /* float16 */
6937             tcg_single = tcg_temp_new_i32();
6938             if (is_signed) {
6939                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6940                                      tcg_shift, tcg_fpstatus);
6941             } else {
6942                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6943                                      tcg_shift, tcg_fpstatus);
6944             }
6945             write_fp_sreg(s, rd, tcg_single);
6946             break;
6947 
6948         default:
6949             g_assert_not_reached();
6950         }
6951     } else {
6952         TCGv_i64 tcg_int = cpu_reg(s, rd);
6953         TCGv_i32 tcg_rmode;
6954 
6955         if (extract32(opcode, 2, 1)) {
6956             /* There are too many rounding modes to all fit into rmode,
6957              * so FCVTA[US] is a special case.
6958              */
6959             rmode = FPROUNDING_TIEAWAY;
6960         }
6961 
6962         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
6963 
6964         switch (type) {
6965         case 1: /* float64 */
6966             tcg_double = read_fp_dreg(s, rn);
6967             if (is_signed) {
6968                 if (!sf) {
6969                     gen_helper_vfp_tosld(tcg_int, tcg_double,
6970                                          tcg_shift, tcg_fpstatus);
6971                 } else {
6972                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
6973                                          tcg_shift, tcg_fpstatus);
6974                 }
6975             } else {
6976                 if (!sf) {
6977                     gen_helper_vfp_tould(tcg_int, tcg_double,
6978                                          tcg_shift, tcg_fpstatus);
6979                 } else {
6980                     gen_helper_vfp_touqd(tcg_int, tcg_double,
6981                                          tcg_shift, tcg_fpstatus);
6982                 }
6983             }
6984             if (!sf) {
6985                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6986             }
6987             break;
6988 
6989         case 0: /* float32 */
6990             tcg_single = read_fp_sreg(s, rn);
6991             if (sf) {
6992                 if (is_signed) {
6993                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
6994                                          tcg_shift, tcg_fpstatus);
6995                 } else {
6996                     gen_helper_vfp_touqs(tcg_int, tcg_single,
6997                                          tcg_shift, tcg_fpstatus);
6998                 }
6999             } else {
7000                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7001                 if (is_signed) {
7002                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
7003                                          tcg_shift, tcg_fpstatus);
7004                 } else {
7005                     gen_helper_vfp_touls(tcg_dest, tcg_single,
7006                                          tcg_shift, tcg_fpstatus);
7007                 }
7008                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7009             }
7010             break;
7011 
7012         case 3: /* float16 */
7013             tcg_single = read_fp_sreg(s, rn);
7014             if (sf) {
7015                 if (is_signed) {
7016                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
7017                                          tcg_shift, tcg_fpstatus);
7018                 } else {
7019                     gen_helper_vfp_touqh(tcg_int, tcg_single,
7020                                          tcg_shift, tcg_fpstatus);
7021                 }
7022             } else {
7023                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7024                 if (is_signed) {
7025                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
7026                                          tcg_shift, tcg_fpstatus);
7027                 } else {
7028                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
7029                                          tcg_shift, tcg_fpstatus);
7030                 }
7031                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7032             }
7033             break;
7034 
7035         default:
7036             g_assert_not_reached();
7037         }
7038 
7039         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
7040     }
7041 }
7042 
7043 /* Floating point <-> fixed point conversions
7044  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
7045  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7046  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
7047  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7048  */
7049 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
7050 {
7051     int rd = extract32(insn, 0, 5);
7052     int rn = extract32(insn, 5, 5);
7053     int scale = extract32(insn, 10, 6);
7054     int opcode = extract32(insn, 16, 3);
7055     int rmode = extract32(insn, 19, 2);
7056     int type = extract32(insn, 22, 2);
7057     bool sbit = extract32(insn, 29, 1);
7058     bool sf = extract32(insn, 31, 1);
7059     bool itof;
7060 
7061     if (sbit || (!sf && scale < 32)) {
7062         unallocated_encoding(s);
7063         return;
7064     }
7065 
7066     switch (type) {
7067     case 0: /* float32 */
7068     case 1: /* float64 */
7069         break;
7070     case 3: /* float16 */
7071         if (dc_isar_feature(aa64_fp16, s)) {
7072             break;
7073         }
7074         /* fallthru */
7075     default:
7076         unallocated_encoding(s);
7077         return;
7078     }
7079 
7080     switch ((rmode << 3) | opcode) {
7081     case 0x2: /* SCVTF */
7082     case 0x3: /* UCVTF */
7083         itof = true;
7084         break;
7085     case 0x18: /* FCVTZS */
7086     case 0x19: /* FCVTZU */
7087         itof = false;
7088         break;
7089     default:
7090         unallocated_encoding(s);
7091         return;
7092     }
7093 
7094     if (!fp_access_check(s)) {
7095         return;
7096     }
7097 
7098     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
7099 }
7100 
7101 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
7102 {
7103     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
7104      * without conversion.
7105      */
7106 
7107     if (itof) {
7108         TCGv_i64 tcg_rn = cpu_reg(s, rn);
7109         TCGv_i64 tmp;
7110 
7111         switch (type) {
7112         case 0:
7113             /* 32 bit */
7114             tmp = tcg_temp_new_i64();
7115             tcg_gen_ext32u_i64(tmp, tcg_rn);
7116             write_fp_dreg(s, rd, tmp);
7117             break;
7118         case 1:
7119             /* 64 bit */
7120             write_fp_dreg(s, rd, tcg_rn);
7121             break;
7122         case 2:
7123             /* 64 bit to top half. */
7124             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
7125             clear_vec_high(s, true, rd);
7126             break;
7127         case 3:
7128             /* 16 bit */
7129             tmp = tcg_temp_new_i64();
7130             tcg_gen_ext16u_i64(tmp, tcg_rn);
7131             write_fp_dreg(s, rd, tmp);
7132             break;
7133         default:
7134             g_assert_not_reached();
7135         }
7136     } else {
7137         TCGv_i64 tcg_rd = cpu_reg(s, rd);
7138 
7139         switch (type) {
7140         case 0:
7141             /* 32 bit */
7142             tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
7143             break;
7144         case 1:
7145             /* 64 bit */
7146             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
7147             break;
7148         case 2:
7149             /* 64 bits from top half */
7150             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
7151             break;
7152         case 3:
7153             /* 16 bit */
7154             tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
7155             break;
7156         default:
7157             g_assert_not_reached();
7158         }
7159     }
7160 }
7161 
7162 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
7163 {
7164     TCGv_i64 t = read_fp_dreg(s, rn);
7165     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
7166 
7167     gen_helper_fjcvtzs(t, t, fpstatus);
7168 
7169     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
7170     tcg_gen_extrh_i64_i32(cpu_ZF, t);
7171     tcg_gen_movi_i32(cpu_CF, 0);
7172     tcg_gen_movi_i32(cpu_NF, 0);
7173     tcg_gen_movi_i32(cpu_VF, 0);
7174 }
7175 
7176 /* Floating point <-> integer conversions
7177  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
7178  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7179  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
7180  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7181  */
7182 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
7183 {
7184     int rd = extract32(insn, 0, 5);
7185     int rn = extract32(insn, 5, 5);
7186     int opcode = extract32(insn, 16, 3);
7187     int rmode = extract32(insn, 19, 2);
7188     int type = extract32(insn, 22, 2);
7189     bool sbit = extract32(insn, 29, 1);
7190     bool sf = extract32(insn, 31, 1);
7191     bool itof = false;
7192 
7193     if (sbit) {
7194         goto do_unallocated;
7195     }
7196 
7197     switch (opcode) {
7198     case 2: /* SCVTF */
7199     case 3: /* UCVTF */
7200         itof = true;
7201         /* fallthru */
7202     case 4: /* FCVTAS */
7203     case 5: /* FCVTAU */
7204         if (rmode != 0) {
7205             goto do_unallocated;
7206         }
7207         /* fallthru */
7208     case 0: /* FCVT[NPMZ]S */
7209     case 1: /* FCVT[NPMZ]U */
7210         switch (type) {
7211         case 0: /* float32 */
7212         case 1: /* float64 */
7213             break;
7214         case 3: /* float16 */
7215             if (!dc_isar_feature(aa64_fp16, s)) {
7216                 goto do_unallocated;
7217             }
7218             break;
7219         default:
7220             goto do_unallocated;
7221         }
7222         if (!fp_access_check(s)) {
7223             return;
7224         }
7225         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
7226         break;
7227 
7228     default:
7229         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
7230         case 0b01100110: /* FMOV half <-> 32-bit int */
7231         case 0b01100111:
7232         case 0b11100110: /* FMOV half <-> 64-bit int */
7233         case 0b11100111:
7234             if (!dc_isar_feature(aa64_fp16, s)) {
7235                 goto do_unallocated;
7236             }
7237             /* fallthru */
7238         case 0b00000110: /* FMOV 32-bit */
7239         case 0b00000111:
7240         case 0b10100110: /* FMOV 64-bit */
7241         case 0b10100111:
7242         case 0b11001110: /* FMOV top half of 128-bit */
7243         case 0b11001111:
7244             if (!fp_access_check(s)) {
7245                 return;
7246             }
7247             itof = opcode & 1;
7248             handle_fmov(s, rd, rn, type, itof);
7249             break;
7250 
7251         case 0b00111110: /* FJCVTZS */
7252             if (!dc_isar_feature(aa64_jscvt, s)) {
7253                 goto do_unallocated;
7254             } else if (fp_access_check(s)) {
7255                 handle_fjcvtzs(s, rd, rn);
7256             }
7257             break;
7258 
7259         default:
7260         do_unallocated:
7261             unallocated_encoding(s);
7262             return;
7263         }
7264         break;
7265     }
7266 }
7267 
7268 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7269  *   31  30  29 28     25 24                          0
7270  * +---+---+---+---------+-----------------------------+
7271  * |   | 0 |   | 1 1 1 1 |                             |
7272  * +---+---+---+---------+-----------------------------+
7273  */
7274 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7275 {
7276     if (extract32(insn, 24, 1)) {
7277         /* Floating point data-processing (3 source) */
7278         disas_fp_3src(s, insn);
7279     } else if (extract32(insn, 21, 1) == 0) {
7280         /* Floating point to fixed point conversions */
7281         disas_fp_fixed_conv(s, insn);
7282     } else {
7283         switch (extract32(insn, 10, 2)) {
7284         case 1:
7285             /* Floating point conditional compare */
7286             disas_fp_ccomp(s, insn);
7287             break;
7288         case 2:
7289             /* Floating point data-processing (2 source) */
7290             disas_fp_2src(s, insn);
7291             break;
7292         case 3:
7293             /* Floating point conditional select */
7294             disas_fp_csel(s, insn);
7295             break;
7296         case 0:
7297             switch (ctz32(extract32(insn, 12, 4))) {
7298             case 0: /* [15:12] == xxx1 */
7299                 /* Floating point immediate */
7300                 disas_fp_imm(s, insn);
7301                 break;
7302             case 1: /* [15:12] == xx10 */
7303                 /* Floating point compare */
7304                 disas_fp_compare(s, insn);
7305                 break;
7306             case 2: /* [15:12] == x100 */
7307                 /* Floating point data-processing (1 source) */
7308                 disas_fp_1src(s, insn);
7309                 break;
7310             case 3: /* [15:12] == 1000 */
7311                 unallocated_encoding(s);
7312                 break;
7313             default: /* [15:12] == 0000 */
7314                 /* Floating point <-> integer conversions */
7315                 disas_fp_int_conv(s, insn);
7316                 break;
7317             }
7318             break;
7319         }
7320     }
7321 }
7322 
7323 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7324                      int pos)
7325 {
7326     /* Extract 64 bits from the middle of two concatenated 64 bit
7327      * vector register slices left:right. The extracted bits start
7328      * at 'pos' bits into the right (least significant) side.
7329      * We return the result in tcg_right, and guarantee not to
7330      * trash tcg_left.
7331      */
7332     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7333     assert(pos > 0 && pos < 64);
7334 
7335     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7336     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7337     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7338 }
7339 
7340 /* EXT
7341  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
7342  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7343  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
7344  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7345  */
7346 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7347 {
7348     int is_q = extract32(insn, 30, 1);
7349     int op2 = extract32(insn, 22, 2);
7350     int imm4 = extract32(insn, 11, 4);
7351     int rm = extract32(insn, 16, 5);
7352     int rn = extract32(insn, 5, 5);
7353     int rd = extract32(insn, 0, 5);
7354     int pos = imm4 << 3;
7355     TCGv_i64 tcg_resl, tcg_resh;
7356 
7357     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7358         unallocated_encoding(s);
7359         return;
7360     }
7361 
7362     if (!fp_access_check(s)) {
7363         return;
7364     }
7365 
7366     tcg_resh = tcg_temp_new_i64();
7367     tcg_resl = tcg_temp_new_i64();
7368 
7369     /* Vd gets bits starting at pos bits into Vm:Vn. This is
7370      * either extracting 128 bits from a 128:128 concatenation, or
7371      * extracting 64 bits from a 64:64 concatenation.
7372      */
7373     if (!is_q) {
7374         read_vec_element(s, tcg_resl, rn, 0, MO_64);
7375         if (pos != 0) {
7376             read_vec_element(s, tcg_resh, rm, 0, MO_64);
7377             do_ext64(s, tcg_resh, tcg_resl, pos);
7378         }
7379     } else {
7380         TCGv_i64 tcg_hh;
7381         typedef struct {
7382             int reg;
7383             int elt;
7384         } EltPosns;
7385         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7386         EltPosns *elt = eltposns;
7387 
7388         if (pos >= 64) {
7389             elt++;
7390             pos -= 64;
7391         }
7392 
7393         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7394         elt++;
7395         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7396         elt++;
7397         if (pos != 0) {
7398             do_ext64(s, tcg_resh, tcg_resl, pos);
7399             tcg_hh = tcg_temp_new_i64();
7400             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7401             do_ext64(s, tcg_hh, tcg_resh, pos);
7402         }
7403     }
7404 
7405     write_vec_element(s, tcg_resl, rd, 0, MO_64);
7406     if (is_q) {
7407         write_vec_element(s, tcg_resh, rd, 1, MO_64);
7408     }
7409     clear_vec_high(s, is_q, rd);
7410 }
7411 
7412 /* TBL/TBX
7413  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
7414  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7415  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
7416  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7417  */
7418 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7419 {
7420     int op2 = extract32(insn, 22, 2);
7421     int is_q = extract32(insn, 30, 1);
7422     int rm = extract32(insn, 16, 5);
7423     int rn = extract32(insn, 5, 5);
7424     int rd = extract32(insn, 0, 5);
7425     int is_tbx = extract32(insn, 12, 1);
7426     int len = (extract32(insn, 13, 2) + 1) * 16;
7427 
7428     if (op2 != 0) {
7429         unallocated_encoding(s);
7430         return;
7431     }
7432 
7433     if (!fp_access_check(s)) {
7434         return;
7435     }
7436 
7437     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7438                        vec_full_reg_offset(s, rm), cpu_env,
7439                        is_q ? 16 : 8, vec_full_reg_size(s),
7440                        (len << 6) | (is_tbx << 5) | rn,
7441                        gen_helper_simd_tblx);
7442 }
7443 
7444 /* ZIP/UZP/TRN
7445  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
7446  * +---+---+-------------+------+---+------+---+------------------+------+
7447  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
7448  * +---+---+-------------+------+---+------+---+------------------+------+
7449  */
7450 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7451 {
7452     int rd = extract32(insn, 0, 5);
7453     int rn = extract32(insn, 5, 5);
7454     int rm = extract32(insn, 16, 5);
7455     int size = extract32(insn, 22, 2);
7456     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7457      * bit 2 indicates 1 vs 2 variant of the insn.
7458      */
7459     int opcode = extract32(insn, 12, 2);
7460     bool part = extract32(insn, 14, 1);
7461     bool is_q = extract32(insn, 30, 1);
7462     int esize = 8 << size;
7463     int i;
7464     int datasize = is_q ? 128 : 64;
7465     int elements = datasize / esize;
7466     TCGv_i64 tcg_res[2], tcg_ele;
7467 
7468     if (opcode == 0 || (size == 3 && !is_q)) {
7469         unallocated_encoding(s);
7470         return;
7471     }
7472 
7473     if (!fp_access_check(s)) {
7474         return;
7475     }
7476 
7477     tcg_res[0] = tcg_temp_new_i64();
7478     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
7479     tcg_ele = tcg_temp_new_i64();
7480 
7481     for (i = 0; i < elements; i++) {
7482         int o, w;
7483 
7484         switch (opcode) {
7485         case 1: /* UZP1/2 */
7486         {
7487             int midpoint = elements / 2;
7488             if (i < midpoint) {
7489                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
7490             } else {
7491                 read_vec_element(s, tcg_ele, rm,
7492                                  2 * (i - midpoint) + part, size);
7493             }
7494             break;
7495         }
7496         case 2: /* TRN1/2 */
7497             if (i & 1) {
7498                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
7499             } else {
7500                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
7501             }
7502             break;
7503         case 3: /* ZIP1/2 */
7504         {
7505             int base = part * elements / 2;
7506             if (i & 1) {
7507                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
7508             } else {
7509                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
7510             }
7511             break;
7512         }
7513         default:
7514             g_assert_not_reached();
7515         }
7516 
7517         w = (i * esize) / 64;
7518         o = (i * esize) % 64;
7519         if (o == 0) {
7520             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
7521         } else {
7522             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
7523             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
7524         }
7525     }
7526 
7527     for (i = 0; i <= is_q; ++i) {
7528         write_vec_element(s, tcg_res[i], rd, i, MO_64);
7529     }
7530     clear_vec_high(s, is_q, rd);
7531 }
7532 
7533 /*
7534  * do_reduction_op helper
7535  *
7536  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7537  * important for correct NaN propagation that we do these
7538  * operations in exactly the order specified by the pseudocode.
7539  *
7540  * This is a recursive function, TCG temps should be freed by the
7541  * calling function once it is done with the values.
7542  */
7543 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7544                                 int esize, int size, int vmap, TCGv_ptr fpst)
7545 {
7546     if (esize == size) {
7547         int element;
7548         MemOp msize = esize == 16 ? MO_16 : MO_32;
7549         TCGv_i32 tcg_elem;
7550 
7551         /* We should have one register left here */
7552         assert(ctpop8(vmap) == 1);
7553         element = ctz32(vmap);
7554         assert(element < 8);
7555 
7556         tcg_elem = tcg_temp_new_i32();
7557         read_vec_element_i32(s, tcg_elem, rn, element, msize);
7558         return tcg_elem;
7559     } else {
7560         int bits = size / 2;
7561         int shift = ctpop8(vmap) / 2;
7562         int vmap_lo = (vmap >> shift) & vmap;
7563         int vmap_hi = (vmap & ~vmap_lo);
7564         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7565 
7566         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7567         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7568         tcg_res = tcg_temp_new_i32();
7569 
7570         switch (fpopcode) {
7571         case 0x0c: /* fmaxnmv half-precision */
7572             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7573             break;
7574         case 0x0f: /* fmaxv half-precision */
7575             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7576             break;
7577         case 0x1c: /* fminnmv half-precision */
7578             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7579             break;
7580         case 0x1f: /* fminv half-precision */
7581             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7582             break;
7583         case 0x2c: /* fmaxnmv */
7584             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7585             break;
7586         case 0x2f: /* fmaxv */
7587             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7588             break;
7589         case 0x3c: /* fminnmv */
7590             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7591             break;
7592         case 0x3f: /* fminv */
7593             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7594             break;
7595         default:
7596             g_assert_not_reached();
7597         }
7598         return tcg_res;
7599     }
7600 }
7601 
7602 /* AdvSIMD across lanes
7603  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7604  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7605  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7606  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7607  */
7608 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7609 {
7610     int rd = extract32(insn, 0, 5);
7611     int rn = extract32(insn, 5, 5);
7612     int size = extract32(insn, 22, 2);
7613     int opcode = extract32(insn, 12, 5);
7614     bool is_q = extract32(insn, 30, 1);
7615     bool is_u = extract32(insn, 29, 1);
7616     bool is_fp = false;
7617     bool is_min = false;
7618     int esize;
7619     int elements;
7620     int i;
7621     TCGv_i64 tcg_res, tcg_elt;
7622 
7623     switch (opcode) {
7624     case 0x1b: /* ADDV */
7625         if (is_u) {
7626             unallocated_encoding(s);
7627             return;
7628         }
7629         /* fall through */
7630     case 0x3: /* SADDLV, UADDLV */
7631     case 0xa: /* SMAXV, UMAXV */
7632     case 0x1a: /* SMINV, UMINV */
7633         if (size == 3 || (size == 2 && !is_q)) {
7634             unallocated_encoding(s);
7635             return;
7636         }
7637         break;
7638     case 0xc: /* FMAXNMV, FMINNMV */
7639     case 0xf: /* FMAXV, FMINV */
7640         /* Bit 1 of size field encodes min vs max and the actual size
7641          * depends on the encoding of the U bit. If not set (and FP16
7642          * enabled) then we do half-precision float instead of single
7643          * precision.
7644          */
7645         is_min = extract32(size, 1, 1);
7646         is_fp = true;
7647         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7648             size = 1;
7649         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7650             unallocated_encoding(s);
7651             return;
7652         } else {
7653             size = 2;
7654         }
7655         break;
7656     default:
7657         unallocated_encoding(s);
7658         return;
7659     }
7660 
7661     if (!fp_access_check(s)) {
7662         return;
7663     }
7664 
7665     esize = 8 << size;
7666     elements = (is_q ? 128 : 64) / esize;
7667 
7668     tcg_res = tcg_temp_new_i64();
7669     tcg_elt = tcg_temp_new_i64();
7670 
7671     /* These instructions operate across all lanes of a vector
7672      * to produce a single result. We can guarantee that a 64
7673      * bit intermediate is sufficient:
7674      *  + for [US]ADDLV the maximum element size is 32 bits, and
7675      *    the result type is 64 bits
7676      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7677      *    same as the element size, which is 32 bits at most
7678      * For the integer operations we can choose to work at 64
7679      * or 32 bits and truncate at the end; for simplicity
7680      * we use 64 bits always. The floating point
7681      * ops do require 32 bit intermediates, though.
7682      */
7683     if (!is_fp) {
7684         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7685 
7686         for (i = 1; i < elements; i++) {
7687             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7688 
7689             switch (opcode) {
7690             case 0x03: /* SADDLV / UADDLV */
7691             case 0x1b: /* ADDV */
7692                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7693                 break;
7694             case 0x0a: /* SMAXV / UMAXV */
7695                 if (is_u) {
7696                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7697                 } else {
7698                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7699                 }
7700                 break;
7701             case 0x1a: /* SMINV / UMINV */
7702                 if (is_u) {
7703                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7704                 } else {
7705                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7706                 }
7707                 break;
7708             default:
7709                 g_assert_not_reached();
7710             }
7711 
7712         }
7713     } else {
7714         /* Floating point vector reduction ops which work across 32
7715          * bit (single) or 16 bit (half-precision) intermediates.
7716          * Note that correct NaN propagation requires that we do these
7717          * operations in exactly the order specified by the pseudocode.
7718          */
7719         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7720         int fpopcode = opcode | is_min << 4 | is_u << 5;
7721         int vmap = (1 << elements) - 1;
7722         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7723                                              (is_q ? 128 : 64), vmap, fpst);
7724         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7725     }
7726 
7727     /* Now truncate the result to the width required for the final output */
7728     if (opcode == 0x03) {
7729         /* SADDLV, UADDLV: result is 2*esize */
7730         size++;
7731     }
7732 
7733     switch (size) {
7734     case 0:
7735         tcg_gen_ext8u_i64(tcg_res, tcg_res);
7736         break;
7737     case 1:
7738         tcg_gen_ext16u_i64(tcg_res, tcg_res);
7739         break;
7740     case 2:
7741         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7742         break;
7743     case 3:
7744         break;
7745     default:
7746         g_assert_not_reached();
7747     }
7748 
7749     write_fp_dreg(s, rd, tcg_res);
7750 }
7751 
7752 /* DUP (Element, Vector)
7753  *
7754  *  31  30   29              21 20    16 15        10  9    5 4    0
7755  * +---+---+-------------------+--------+-------------+------+------+
7756  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7757  * +---+---+-------------------+--------+-------------+------+------+
7758  *
7759  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7760  */
7761 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7762                              int imm5)
7763 {
7764     int size = ctz32(imm5);
7765     int index;
7766 
7767     if (size > 3 || (size == 3 && !is_q)) {
7768         unallocated_encoding(s);
7769         return;
7770     }
7771 
7772     if (!fp_access_check(s)) {
7773         return;
7774     }
7775 
7776     index = imm5 >> (size + 1);
7777     tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7778                          vec_reg_offset(s, rn, index, size),
7779                          is_q ? 16 : 8, vec_full_reg_size(s));
7780 }
7781 
7782 /* DUP (element, scalar)
7783  *  31                   21 20    16 15        10  9    5 4    0
7784  * +-----------------------+--------+-------------+------+------+
7785  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7786  * +-----------------------+--------+-------------+------+------+
7787  */
7788 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7789                               int imm5)
7790 {
7791     int size = ctz32(imm5);
7792     int index;
7793     TCGv_i64 tmp;
7794 
7795     if (size > 3) {
7796         unallocated_encoding(s);
7797         return;
7798     }
7799 
7800     if (!fp_access_check(s)) {
7801         return;
7802     }
7803 
7804     index = imm5 >> (size + 1);
7805 
7806     /* This instruction just extracts the specified element and
7807      * zero-extends it into the bottom of the destination register.
7808      */
7809     tmp = tcg_temp_new_i64();
7810     read_vec_element(s, tmp, rn, index, size);
7811     write_fp_dreg(s, rd, tmp);
7812 }
7813 
7814 /* DUP (General)
7815  *
7816  *  31  30   29              21 20    16 15        10  9    5 4    0
7817  * +---+---+-------------------+--------+-------------+------+------+
7818  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
7819  * +---+---+-------------------+--------+-------------+------+------+
7820  *
7821  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7822  */
7823 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7824                              int imm5)
7825 {
7826     int size = ctz32(imm5);
7827     uint32_t dofs, oprsz, maxsz;
7828 
7829     if (size > 3 || ((size == 3) && !is_q)) {
7830         unallocated_encoding(s);
7831         return;
7832     }
7833 
7834     if (!fp_access_check(s)) {
7835         return;
7836     }
7837 
7838     dofs = vec_full_reg_offset(s, rd);
7839     oprsz = is_q ? 16 : 8;
7840     maxsz = vec_full_reg_size(s);
7841 
7842     tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7843 }
7844 
7845 /* INS (Element)
7846  *
7847  *  31                   21 20    16 15  14    11  10 9    5 4    0
7848  * +-----------------------+--------+------------+---+------+------+
7849  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
7850  * +-----------------------+--------+------------+---+------+------+
7851  *
7852  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7853  * index: encoded in imm5<4:size+1>
7854  */
7855 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7856                              int imm4, int imm5)
7857 {
7858     int size = ctz32(imm5);
7859     int src_index, dst_index;
7860     TCGv_i64 tmp;
7861 
7862     if (size > 3) {
7863         unallocated_encoding(s);
7864         return;
7865     }
7866 
7867     if (!fp_access_check(s)) {
7868         return;
7869     }
7870 
7871     dst_index = extract32(imm5, 1+size, 5);
7872     src_index = extract32(imm4, size, 4);
7873 
7874     tmp = tcg_temp_new_i64();
7875 
7876     read_vec_element(s, tmp, rn, src_index, size);
7877     write_vec_element(s, tmp, rd, dst_index, size);
7878 
7879     /* INS is considered a 128-bit write for SVE. */
7880     clear_vec_high(s, true, rd);
7881 }
7882 
7883 
7884 /* INS (General)
7885  *
7886  *  31                   21 20    16 15        10  9    5 4    0
7887  * +-----------------------+--------+-------------+------+------+
7888  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
7889  * +-----------------------+--------+-------------+------+------+
7890  *
7891  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7892  * index: encoded in imm5<4:size+1>
7893  */
7894 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7895 {
7896     int size = ctz32(imm5);
7897     int idx;
7898 
7899     if (size > 3) {
7900         unallocated_encoding(s);
7901         return;
7902     }
7903 
7904     if (!fp_access_check(s)) {
7905         return;
7906     }
7907 
7908     idx = extract32(imm5, 1 + size, 4 - size);
7909     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7910 
7911     /* INS is considered a 128-bit write for SVE. */
7912     clear_vec_high(s, true, rd);
7913 }
7914 
7915 /*
7916  * UMOV (General)
7917  * SMOV (General)
7918  *
7919  *  31  30   29              21 20    16 15    12   10 9    5 4    0
7920  * +---+---+-------------------+--------+-------------+------+------+
7921  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
7922  * +---+---+-------------------+--------+-------------+------+------+
7923  *
7924  * U: unsigned when set
7925  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7926  */
7927 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7928                                   int rn, int rd, int imm5)
7929 {
7930     int size = ctz32(imm5);
7931     int element;
7932     TCGv_i64 tcg_rd;
7933 
7934     /* Check for UnallocatedEncodings */
7935     if (is_signed) {
7936         if (size > 2 || (size == 2 && !is_q)) {
7937             unallocated_encoding(s);
7938             return;
7939         }
7940     } else {
7941         if (size > 3
7942             || (size < 3 && is_q)
7943             || (size == 3 && !is_q)) {
7944             unallocated_encoding(s);
7945             return;
7946         }
7947     }
7948 
7949     if (!fp_access_check(s)) {
7950         return;
7951     }
7952 
7953     element = extract32(imm5, 1+size, 4);
7954 
7955     tcg_rd = cpu_reg(s, rd);
7956     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7957     if (is_signed && !is_q) {
7958         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7959     }
7960 }
7961 
7962 /* AdvSIMD copy
7963  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
7964  * +---+---+----+-----------------+------+---+------+---+------+------+
7965  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7966  * +---+---+----+-----------------+------+---+------+---+------+------+
7967  */
7968 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7969 {
7970     int rd = extract32(insn, 0, 5);
7971     int rn = extract32(insn, 5, 5);
7972     int imm4 = extract32(insn, 11, 4);
7973     int op = extract32(insn, 29, 1);
7974     int is_q = extract32(insn, 30, 1);
7975     int imm5 = extract32(insn, 16, 5);
7976 
7977     if (op) {
7978         if (is_q) {
7979             /* INS (element) */
7980             handle_simd_inse(s, rd, rn, imm4, imm5);
7981         } else {
7982             unallocated_encoding(s);
7983         }
7984     } else {
7985         switch (imm4) {
7986         case 0:
7987             /* DUP (element - vector) */
7988             handle_simd_dupe(s, is_q, rd, rn, imm5);
7989             break;
7990         case 1:
7991             /* DUP (general) */
7992             handle_simd_dupg(s, is_q, rd, rn, imm5);
7993             break;
7994         case 3:
7995             if (is_q) {
7996                 /* INS (general) */
7997                 handle_simd_insg(s, rd, rn, imm5);
7998             } else {
7999                 unallocated_encoding(s);
8000             }
8001             break;
8002         case 5:
8003         case 7:
8004             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
8005             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
8006             break;
8007         default:
8008             unallocated_encoding(s);
8009             break;
8010         }
8011     }
8012 }
8013 
8014 /* AdvSIMD modified immediate
8015  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
8016  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8017  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
8018  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8019  *
8020  * There are a number of operations that can be carried out here:
8021  *   MOVI - move (shifted) imm into register
8022  *   MVNI - move inverted (shifted) imm into register
8023  *   ORR  - bitwise OR of (shifted) imm with register
8024  *   BIC  - bitwise clear of (shifted) imm with register
8025  * With ARMv8.2 we also have:
8026  *   FMOV half-precision
8027  */
8028 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
8029 {
8030     int rd = extract32(insn, 0, 5);
8031     int cmode = extract32(insn, 12, 4);
8032     int o2 = extract32(insn, 11, 1);
8033     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
8034     bool is_neg = extract32(insn, 29, 1);
8035     bool is_q = extract32(insn, 30, 1);
8036     uint64_t imm = 0;
8037 
8038     if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
8039         /* Check for FMOV (vector, immediate) - half-precision */
8040         if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
8041             unallocated_encoding(s);
8042             return;
8043         }
8044     }
8045 
8046     if (!fp_access_check(s)) {
8047         return;
8048     }
8049 
8050     if (cmode == 15 && o2 && !is_neg) {
8051         /* FMOV (vector, immediate) - half-precision */
8052         imm = vfp_expand_imm(MO_16, abcdefgh);
8053         /* now duplicate across the lanes */
8054         imm = dup_const(MO_16, imm);
8055     } else {
8056         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
8057     }
8058 
8059     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
8060         /* MOVI or MVNI, with MVNI negation handled above.  */
8061         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
8062                              vec_full_reg_size(s), imm);
8063     } else {
8064         /* ORR or BIC, with BIC negation to AND handled above.  */
8065         if (is_neg) {
8066             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
8067         } else {
8068             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
8069         }
8070     }
8071 }
8072 
8073 /* AdvSIMD scalar copy
8074  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
8075  * +-----+----+-----------------+------+---+------+---+------+------+
8076  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
8077  * +-----+----+-----------------+------+---+------+---+------+------+
8078  */
8079 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
8080 {
8081     int rd = extract32(insn, 0, 5);
8082     int rn = extract32(insn, 5, 5);
8083     int imm4 = extract32(insn, 11, 4);
8084     int imm5 = extract32(insn, 16, 5);
8085     int op = extract32(insn, 29, 1);
8086 
8087     if (op != 0 || imm4 != 0) {
8088         unallocated_encoding(s);
8089         return;
8090     }
8091 
8092     /* DUP (element, scalar) */
8093     handle_simd_dupes(s, rd, rn, imm5);
8094 }
8095 
8096 /* AdvSIMD scalar pairwise
8097  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
8098  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8099  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
8100  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8101  */
8102 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
8103 {
8104     int u = extract32(insn, 29, 1);
8105     int size = extract32(insn, 22, 2);
8106     int opcode = extract32(insn, 12, 5);
8107     int rn = extract32(insn, 5, 5);
8108     int rd = extract32(insn, 0, 5);
8109     TCGv_ptr fpst;
8110 
8111     /* For some ops (the FP ones), size[1] is part of the encoding.
8112      * For ADDP strictly it is not but size[1] is always 1 for valid
8113      * encodings.
8114      */
8115     opcode |= (extract32(size, 1, 1) << 5);
8116 
8117     switch (opcode) {
8118     case 0x3b: /* ADDP */
8119         if (u || size != 3) {
8120             unallocated_encoding(s);
8121             return;
8122         }
8123         if (!fp_access_check(s)) {
8124             return;
8125         }
8126 
8127         fpst = NULL;
8128         break;
8129     case 0xc: /* FMAXNMP */
8130     case 0xd: /* FADDP */
8131     case 0xf: /* FMAXP */
8132     case 0x2c: /* FMINNMP */
8133     case 0x2f: /* FMINP */
8134         /* FP op, size[0] is 32 or 64 bit*/
8135         if (!u) {
8136             if (!dc_isar_feature(aa64_fp16, s)) {
8137                 unallocated_encoding(s);
8138                 return;
8139             } else {
8140                 size = MO_16;
8141             }
8142         } else {
8143             size = extract32(size, 0, 1) ? MO_64 : MO_32;
8144         }
8145 
8146         if (!fp_access_check(s)) {
8147             return;
8148         }
8149 
8150         fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8151         break;
8152     default:
8153         unallocated_encoding(s);
8154         return;
8155     }
8156 
8157     if (size == MO_64) {
8158         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8159         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8160         TCGv_i64 tcg_res = tcg_temp_new_i64();
8161 
8162         read_vec_element(s, tcg_op1, rn, 0, MO_64);
8163         read_vec_element(s, tcg_op2, rn, 1, MO_64);
8164 
8165         switch (opcode) {
8166         case 0x3b: /* ADDP */
8167             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
8168             break;
8169         case 0xc: /* FMAXNMP */
8170             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8171             break;
8172         case 0xd: /* FADDP */
8173             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8174             break;
8175         case 0xf: /* FMAXP */
8176             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8177             break;
8178         case 0x2c: /* FMINNMP */
8179             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8180             break;
8181         case 0x2f: /* FMINP */
8182             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8183             break;
8184         default:
8185             g_assert_not_reached();
8186         }
8187 
8188         write_fp_dreg(s, rd, tcg_res);
8189     } else {
8190         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8191         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8192         TCGv_i32 tcg_res = tcg_temp_new_i32();
8193 
8194         read_vec_element_i32(s, tcg_op1, rn, 0, size);
8195         read_vec_element_i32(s, tcg_op2, rn, 1, size);
8196 
8197         if (size == MO_16) {
8198             switch (opcode) {
8199             case 0xc: /* FMAXNMP */
8200                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8201                 break;
8202             case 0xd: /* FADDP */
8203                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
8204                 break;
8205             case 0xf: /* FMAXP */
8206                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
8207                 break;
8208             case 0x2c: /* FMINNMP */
8209                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8210                 break;
8211             case 0x2f: /* FMINP */
8212                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
8213                 break;
8214             default:
8215                 g_assert_not_reached();
8216             }
8217         } else {
8218             switch (opcode) {
8219             case 0xc: /* FMAXNMP */
8220                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8221                 break;
8222             case 0xd: /* FADDP */
8223                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8224                 break;
8225             case 0xf: /* FMAXP */
8226                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8227                 break;
8228             case 0x2c: /* FMINNMP */
8229                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8230                 break;
8231             case 0x2f: /* FMINP */
8232                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8233                 break;
8234             default:
8235                 g_assert_not_reached();
8236             }
8237         }
8238 
8239         write_fp_sreg(s, rd, tcg_res);
8240     }
8241 }
8242 
8243 /*
8244  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8245  *
8246  * This code is handles the common shifting code and is used by both
8247  * the vector and scalar code.
8248  */
8249 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8250                                     TCGv_i64 tcg_rnd, bool accumulate,
8251                                     bool is_u, int size, int shift)
8252 {
8253     bool extended_result = false;
8254     bool round = tcg_rnd != NULL;
8255     int ext_lshift = 0;
8256     TCGv_i64 tcg_src_hi;
8257 
8258     if (round && size == 3) {
8259         extended_result = true;
8260         ext_lshift = 64 - shift;
8261         tcg_src_hi = tcg_temp_new_i64();
8262     } else if (shift == 64) {
8263         if (!accumulate && is_u) {
8264             /* result is zero */
8265             tcg_gen_movi_i64(tcg_res, 0);
8266             return;
8267         }
8268     }
8269 
8270     /* Deal with the rounding step */
8271     if (round) {
8272         if (extended_result) {
8273             TCGv_i64 tcg_zero = tcg_constant_i64(0);
8274             if (!is_u) {
8275                 /* take care of sign extending tcg_res */
8276                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8277                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8278                                  tcg_src, tcg_src_hi,
8279                                  tcg_rnd, tcg_zero);
8280             } else {
8281                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8282                                  tcg_src, tcg_zero,
8283                                  tcg_rnd, tcg_zero);
8284             }
8285         } else {
8286             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8287         }
8288     }
8289 
8290     /* Now do the shift right */
8291     if (round && extended_result) {
8292         /* extended case, >64 bit precision required */
8293         if (ext_lshift == 0) {
8294             /* special case, only high bits matter */
8295             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8296         } else {
8297             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8298             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8299             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8300         }
8301     } else {
8302         if (is_u) {
8303             if (shift == 64) {
8304                 /* essentially shifting in 64 zeros */
8305                 tcg_gen_movi_i64(tcg_src, 0);
8306             } else {
8307                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8308             }
8309         } else {
8310             if (shift == 64) {
8311                 /* effectively extending the sign-bit */
8312                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8313             } else {
8314                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8315             }
8316         }
8317     }
8318 
8319     if (accumulate) {
8320         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8321     } else {
8322         tcg_gen_mov_i64(tcg_res, tcg_src);
8323     }
8324 }
8325 
8326 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8327 static void handle_scalar_simd_shri(DisasContext *s,
8328                                     bool is_u, int immh, int immb,
8329                                     int opcode, int rn, int rd)
8330 {
8331     const int size = 3;
8332     int immhb = immh << 3 | immb;
8333     int shift = 2 * (8 << size) - immhb;
8334     bool accumulate = false;
8335     bool round = false;
8336     bool insert = false;
8337     TCGv_i64 tcg_rn;
8338     TCGv_i64 tcg_rd;
8339     TCGv_i64 tcg_round;
8340 
8341     if (!extract32(immh, 3, 1)) {
8342         unallocated_encoding(s);
8343         return;
8344     }
8345 
8346     if (!fp_access_check(s)) {
8347         return;
8348     }
8349 
8350     switch (opcode) {
8351     case 0x02: /* SSRA / USRA (accumulate) */
8352         accumulate = true;
8353         break;
8354     case 0x04: /* SRSHR / URSHR (rounding) */
8355         round = true;
8356         break;
8357     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8358         accumulate = round = true;
8359         break;
8360     case 0x08: /* SRI */
8361         insert = true;
8362         break;
8363     }
8364 
8365     if (round) {
8366         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8367     } else {
8368         tcg_round = NULL;
8369     }
8370 
8371     tcg_rn = read_fp_dreg(s, rn);
8372     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8373 
8374     if (insert) {
8375         /* shift count same as element size is valid but does nothing;
8376          * special case to avoid potential shift by 64.
8377          */
8378         int esize = 8 << size;
8379         if (shift != esize) {
8380             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8381             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8382         }
8383     } else {
8384         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8385                                 accumulate, is_u, size, shift);
8386     }
8387 
8388     write_fp_dreg(s, rd, tcg_rd);
8389 }
8390 
8391 /* SHL/SLI - Scalar shift left */
8392 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8393                                     int immh, int immb, int opcode,
8394                                     int rn, int rd)
8395 {
8396     int size = 32 - clz32(immh) - 1;
8397     int immhb = immh << 3 | immb;
8398     int shift = immhb - (8 << size);
8399     TCGv_i64 tcg_rn;
8400     TCGv_i64 tcg_rd;
8401 
8402     if (!extract32(immh, 3, 1)) {
8403         unallocated_encoding(s);
8404         return;
8405     }
8406 
8407     if (!fp_access_check(s)) {
8408         return;
8409     }
8410 
8411     tcg_rn = read_fp_dreg(s, rn);
8412     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8413 
8414     if (insert) {
8415         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8416     } else {
8417         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8418     }
8419 
8420     write_fp_dreg(s, rd, tcg_rd);
8421 }
8422 
8423 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8424  * (signed/unsigned) narrowing */
8425 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8426                                    bool is_u_shift, bool is_u_narrow,
8427                                    int immh, int immb, int opcode,
8428                                    int rn, int rd)
8429 {
8430     int immhb = immh << 3 | immb;
8431     int size = 32 - clz32(immh) - 1;
8432     int esize = 8 << size;
8433     int shift = (2 * esize) - immhb;
8434     int elements = is_scalar ? 1 : (64 / esize);
8435     bool round = extract32(opcode, 0, 1);
8436     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8437     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8438     TCGv_i32 tcg_rd_narrowed;
8439     TCGv_i64 tcg_final;
8440 
8441     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8442         { gen_helper_neon_narrow_sat_s8,
8443           gen_helper_neon_unarrow_sat8 },
8444         { gen_helper_neon_narrow_sat_s16,
8445           gen_helper_neon_unarrow_sat16 },
8446         { gen_helper_neon_narrow_sat_s32,
8447           gen_helper_neon_unarrow_sat32 },
8448         { NULL, NULL },
8449     };
8450     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8451         gen_helper_neon_narrow_sat_u8,
8452         gen_helper_neon_narrow_sat_u16,
8453         gen_helper_neon_narrow_sat_u32,
8454         NULL
8455     };
8456     NeonGenNarrowEnvFn *narrowfn;
8457 
8458     int i;
8459 
8460     assert(size < 4);
8461 
8462     if (extract32(immh, 3, 1)) {
8463         unallocated_encoding(s);
8464         return;
8465     }
8466 
8467     if (!fp_access_check(s)) {
8468         return;
8469     }
8470 
8471     if (is_u_shift) {
8472         narrowfn = unsigned_narrow_fns[size];
8473     } else {
8474         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8475     }
8476 
8477     tcg_rn = tcg_temp_new_i64();
8478     tcg_rd = tcg_temp_new_i64();
8479     tcg_rd_narrowed = tcg_temp_new_i32();
8480     tcg_final = tcg_temp_new_i64();
8481 
8482     if (round) {
8483         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8484     } else {
8485         tcg_round = NULL;
8486     }
8487 
8488     for (i = 0; i < elements; i++) {
8489         read_vec_element(s, tcg_rn, rn, i, ldop);
8490         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8491                                 false, is_u_shift, size+1, shift);
8492         narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8493         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8494         if (i == 0) {
8495             tcg_gen_mov_i64(tcg_final, tcg_rd);
8496         } else {
8497             tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8498         }
8499     }
8500 
8501     if (!is_q) {
8502         write_vec_element(s, tcg_final, rd, 0, MO_64);
8503     } else {
8504         write_vec_element(s, tcg_final, rd, 1, MO_64);
8505     }
8506     clear_vec_high(s, is_q, rd);
8507 }
8508 
8509 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8510 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8511                              bool src_unsigned, bool dst_unsigned,
8512                              int immh, int immb, int rn, int rd)
8513 {
8514     int immhb = immh << 3 | immb;
8515     int size = 32 - clz32(immh) - 1;
8516     int shift = immhb - (8 << size);
8517     int pass;
8518 
8519     assert(immh != 0);
8520     assert(!(scalar && is_q));
8521 
8522     if (!scalar) {
8523         if (!is_q && extract32(immh, 3, 1)) {
8524             unallocated_encoding(s);
8525             return;
8526         }
8527 
8528         /* Since we use the variable-shift helpers we must
8529          * replicate the shift count into each element of
8530          * the tcg_shift value.
8531          */
8532         switch (size) {
8533         case 0:
8534             shift |= shift << 8;
8535             /* fall through */
8536         case 1:
8537             shift |= shift << 16;
8538             break;
8539         case 2:
8540         case 3:
8541             break;
8542         default:
8543             g_assert_not_reached();
8544         }
8545     }
8546 
8547     if (!fp_access_check(s)) {
8548         return;
8549     }
8550 
8551     if (size == 3) {
8552         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8553         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8554             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8555             { NULL, gen_helper_neon_qshl_u64 },
8556         };
8557         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8558         int maxpass = is_q ? 2 : 1;
8559 
8560         for (pass = 0; pass < maxpass; pass++) {
8561             TCGv_i64 tcg_op = tcg_temp_new_i64();
8562 
8563             read_vec_element(s, tcg_op, rn, pass, MO_64);
8564             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8565             write_vec_element(s, tcg_op, rd, pass, MO_64);
8566         }
8567         clear_vec_high(s, is_q, rd);
8568     } else {
8569         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8570         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8571             {
8572                 { gen_helper_neon_qshl_s8,
8573                   gen_helper_neon_qshl_s16,
8574                   gen_helper_neon_qshl_s32 },
8575                 { gen_helper_neon_qshlu_s8,
8576                   gen_helper_neon_qshlu_s16,
8577                   gen_helper_neon_qshlu_s32 }
8578             }, {
8579                 { NULL, NULL, NULL },
8580                 { gen_helper_neon_qshl_u8,
8581                   gen_helper_neon_qshl_u16,
8582                   gen_helper_neon_qshl_u32 }
8583             }
8584         };
8585         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8586         MemOp memop = scalar ? size : MO_32;
8587         int maxpass = scalar ? 1 : is_q ? 4 : 2;
8588 
8589         for (pass = 0; pass < maxpass; pass++) {
8590             TCGv_i32 tcg_op = tcg_temp_new_i32();
8591 
8592             read_vec_element_i32(s, tcg_op, rn, pass, memop);
8593             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8594             if (scalar) {
8595                 switch (size) {
8596                 case 0:
8597                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
8598                     break;
8599                 case 1:
8600                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8601                     break;
8602                 case 2:
8603                     break;
8604                 default:
8605                     g_assert_not_reached();
8606                 }
8607                 write_fp_sreg(s, rd, tcg_op);
8608             } else {
8609                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8610             }
8611         }
8612 
8613         if (!scalar) {
8614             clear_vec_high(s, is_q, rd);
8615         }
8616     }
8617 }
8618 
8619 /* Common vector code for handling integer to FP conversion */
8620 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8621                                    int elements, int is_signed,
8622                                    int fracbits, int size)
8623 {
8624     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8625     TCGv_i32 tcg_shift = NULL;
8626 
8627     MemOp mop = size | (is_signed ? MO_SIGN : 0);
8628     int pass;
8629 
8630     if (fracbits || size == MO_64) {
8631         tcg_shift = tcg_constant_i32(fracbits);
8632     }
8633 
8634     if (size == MO_64) {
8635         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8636         TCGv_i64 tcg_double = tcg_temp_new_i64();
8637 
8638         for (pass = 0; pass < elements; pass++) {
8639             read_vec_element(s, tcg_int64, rn, pass, mop);
8640 
8641             if (is_signed) {
8642                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8643                                      tcg_shift, tcg_fpst);
8644             } else {
8645                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8646                                      tcg_shift, tcg_fpst);
8647             }
8648             if (elements == 1) {
8649                 write_fp_dreg(s, rd, tcg_double);
8650             } else {
8651                 write_vec_element(s, tcg_double, rd, pass, MO_64);
8652             }
8653         }
8654     } else {
8655         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8656         TCGv_i32 tcg_float = tcg_temp_new_i32();
8657 
8658         for (pass = 0; pass < elements; pass++) {
8659             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8660 
8661             switch (size) {
8662             case MO_32:
8663                 if (fracbits) {
8664                     if (is_signed) {
8665                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
8666                                              tcg_shift, tcg_fpst);
8667                     } else {
8668                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
8669                                              tcg_shift, tcg_fpst);
8670                     }
8671                 } else {
8672                     if (is_signed) {
8673                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8674                     } else {
8675                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8676                     }
8677                 }
8678                 break;
8679             case MO_16:
8680                 if (fracbits) {
8681                     if (is_signed) {
8682                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8683                                              tcg_shift, tcg_fpst);
8684                     } else {
8685                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8686                                              tcg_shift, tcg_fpst);
8687                     }
8688                 } else {
8689                     if (is_signed) {
8690                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8691                     } else {
8692                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8693                     }
8694                 }
8695                 break;
8696             default:
8697                 g_assert_not_reached();
8698             }
8699 
8700             if (elements == 1) {
8701                 write_fp_sreg(s, rd, tcg_float);
8702             } else {
8703                 write_vec_element_i32(s, tcg_float, rd, pass, size);
8704             }
8705         }
8706     }
8707 
8708     clear_vec_high(s, elements << size == 16, rd);
8709 }
8710 
8711 /* UCVTF/SCVTF - Integer to FP conversion */
8712 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8713                                          bool is_q, bool is_u,
8714                                          int immh, int immb, int opcode,
8715                                          int rn, int rd)
8716 {
8717     int size, elements, fracbits;
8718     int immhb = immh << 3 | immb;
8719 
8720     if (immh & 8) {
8721         size = MO_64;
8722         if (!is_scalar && !is_q) {
8723             unallocated_encoding(s);
8724             return;
8725         }
8726     } else if (immh & 4) {
8727         size = MO_32;
8728     } else if (immh & 2) {
8729         size = MO_16;
8730         if (!dc_isar_feature(aa64_fp16, s)) {
8731             unallocated_encoding(s);
8732             return;
8733         }
8734     } else {
8735         /* immh == 0 would be a failure of the decode logic */
8736         g_assert(immh == 1);
8737         unallocated_encoding(s);
8738         return;
8739     }
8740 
8741     if (is_scalar) {
8742         elements = 1;
8743     } else {
8744         elements = (8 << is_q) >> size;
8745     }
8746     fracbits = (16 << size) - immhb;
8747 
8748     if (!fp_access_check(s)) {
8749         return;
8750     }
8751 
8752     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8753 }
8754 
8755 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8756 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8757                                          bool is_q, bool is_u,
8758                                          int immh, int immb, int rn, int rd)
8759 {
8760     int immhb = immh << 3 | immb;
8761     int pass, size, fracbits;
8762     TCGv_ptr tcg_fpstatus;
8763     TCGv_i32 tcg_rmode, tcg_shift;
8764 
8765     if (immh & 0x8) {
8766         size = MO_64;
8767         if (!is_scalar && !is_q) {
8768             unallocated_encoding(s);
8769             return;
8770         }
8771     } else if (immh & 0x4) {
8772         size = MO_32;
8773     } else if (immh & 0x2) {
8774         size = MO_16;
8775         if (!dc_isar_feature(aa64_fp16, s)) {
8776             unallocated_encoding(s);
8777             return;
8778         }
8779     } else {
8780         /* Should have split out AdvSIMD modified immediate earlier.  */
8781         assert(immh == 1);
8782         unallocated_encoding(s);
8783         return;
8784     }
8785 
8786     if (!fp_access_check(s)) {
8787         return;
8788     }
8789 
8790     assert(!(is_scalar && is_q));
8791 
8792     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8793     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
8794     fracbits = (16 << size) - immhb;
8795     tcg_shift = tcg_constant_i32(fracbits);
8796 
8797     if (size == MO_64) {
8798         int maxpass = is_scalar ? 1 : 2;
8799 
8800         for (pass = 0; pass < maxpass; pass++) {
8801             TCGv_i64 tcg_op = tcg_temp_new_i64();
8802 
8803             read_vec_element(s, tcg_op, rn, pass, MO_64);
8804             if (is_u) {
8805                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8806             } else {
8807                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8808             }
8809             write_vec_element(s, tcg_op, rd, pass, MO_64);
8810         }
8811         clear_vec_high(s, is_q, rd);
8812     } else {
8813         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8814         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8815 
8816         switch (size) {
8817         case MO_16:
8818             if (is_u) {
8819                 fn = gen_helper_vfp_touhh;
8820             } else {
8821                 fn = gen_helper_vfp_toshh;
8822             }
8823             break;
8824         case MO_32:
8825             if (is_u) {
8826                 fn = gen_helper_vfp_touls;
8827             } else {
8828                 fn = gen_helper_vfp_tosls;
8829             }
8830             break;
8831         default:
8832             g_assert_not_reached();
8833         }
8834 
8835         for (pass = 0; pass < maxpass; pass++) {
8836             TCGv_i32 tcg_op = tcg_temp_new_i32();
8837 
8838             read_vec_element_i32(s, tcg_op, rn, pass, size);
8839             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8840             if (is_scalar) {
8841                 write_fp_sreg(s, rd, tcg_op);
8842             } else {
8843                 write_vec_element_i32(s, tcg_op, rd, pass, size);
8844             }
8845         }
8846         if (!is_scalar) {
8847             clear_vec_high(s, is_q, rd);
8848         }
8849     }
8850 
8851     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
8852 }
8853 
8854 /* AdvSIMD scalar shift by immediate
8855  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
8856  * +-----+---+-------------+------+------+--------+---+------+------+
8857  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8858  * +-----+---+-------------+------+------+--------+---+------+------+
8859  *
8860  * This is the scalar version so it works on a fixed sized registers
8861  */
8862 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8863 {
8864     int rd = extract32(insn, 0, 5);
8865     int rn = extract32(insn, 5, 5);
8866     int opcode = extract32(insn, 11, 5);
8867     int immb = extract32(insn, 16, 3);
8868     int immh = extract32(insn, 19, 4);
8869     bool is_u = extract32(insn, 29, 1);
8870 
8871     if (immh == 0) {
8872         unallocated_encoding(s);
8873         return;
8874     }
8875 
8876     switch (opcode) {
8877     case 0x08: /* SRI */
8878         if (!is_u) {
8879             unallocated_encoding(s);
8880             return;
8881         }
8882         /* fall through */
8883     case 0x00: /* SSHR / USHR */
8884     case 0x02: /* SSRA / USRA */
8885     case 0x04: /* SRSHR / URSHR */
8886     case 0x06: /* SRSRA / URSRA */
8887         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8888         break;
8889     case 0x0a: /* SHL / SLI */
8890         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8891         break;
8892     case 0x1c: /* SCVTF, UCVTF */
8893         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8894                                      opcode, rn, rd);
8895         break;
8896     case 0x10: /* SQSHRUN, SQSHRUN2 */
8897     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8898         if (!is_u) {
8899             unallocated_encoding(s);
8900             return;
8901         }
8902         handle_vec_simd_sqshrn(s, true, false, false, true,
8903                                immh, immb, opcode, rn, rd);
8904         break;
8905     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8906     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8907         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8908                                immh, immb, opcode, rn, rd);
8909         break;
8910     case 0xc: /* SQSHLU */
8911         if (!is_u) {
8912             unallocated_encoding(s);
8913             return;
8914         }
8915         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8916         break;
8917     case 0xe: /* SQSHL, UQSHL */
8918         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8919         break;
8920     case 0x1f: /* FCVTZS, FCVTZU */
8921         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8922         break;
8923     default:
8924         unallocated_encoding(s);
8925         break;
8926     }
8927 }
8928 
8929 /* AdvSIMD scalar three different
8930  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
8931  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8932  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
8933  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8934  */
8935 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8936 {
8937     bool is_u = extract32(insn, 29, 1);
8938     int size = extract32(insn, 22, 2);
8939     int opcode = extract32(insn, 12, 4);
8940     int rm = extract32(insn, 16, 5);
8941     int rn = extract32(insn, 5, 5);
8942     int rd = extract32(insn, 0, 5);
8943 
8944     if (is_u) {
8945         unallocated_encoding(s);
8946         return;
8947     }
8948 
8949     switch (opcode) {
8950     case 0x9: /* SQDMLAL, SQDMLAL2 */
8951     case 0xb: /* SQDMLSL, SQDMLSL2 */
8952     case 0xd: /* SQDMULL, SQDMULL2 */
8953         if (size == 0 || size == 3) {
8954             unallocated_encoding(s);
8955             return;
8956         }
8957         break;
8958     default:
8959         unallocated_encoding(s);
8960         return;
8961     }
8962 
8963     if (!fp_access_check(s)) {
8964         return;
8965     }
8966 
8967     if (size == 2) {
8968         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8969         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8970         TCGv_i64 tcg_res = tcg_temp_new_i64();
8971 
8972         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8973         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8974 
8975         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8976         gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
8977 
8978         switch (opcode) {
8979         case 0xd: /* SQDMULL, SQDMULL2 */
8980             break;
8981         case 0xb: /* SQDMLSL, SQDMLSL2 */
8982             tcg_gen_neg_i64(tcg_res, tcg_res);
8983             /* fall through */
8984         case 0x9: /* SQDMLAL, SQDMLAL2 */
8985             read_vec_element(s, tcg_op1, rd, 0, MO_64);
8986             gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
8987                                               tcg_res, tcg_op1);
8988             break;
8989         default:
8990             g_assert_not_reached();
8991         }
8992 
8993         write_fp_dreg(s, rd, tcg_res);
8994     } else {
8995         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8996         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8997         TCGv_i64 tcg_res = tcg_temp_new_i64();
8998 
8999         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
9000         gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
9001 
9002         switch (opcode) {
9003         case 0xd: /* SQDMULL, SQDMULL2 */
9004             break;
9005         case 0xb: /* SQDMLSL, SQDMLSL2 */
9006             gen_helper_neon_negl_u32(tcg_res, tcg_res);
9007             /* fall through */
9008         case 0x9: /* SQDMLAL, SQDMLAL2 */
9009         {
9010             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
9011             read_vec_element(s, tcg_op3, rd, 0, MO_32);
9012             gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
9013                                               tcg_res, tcg_op3);
9014             break;
9015         }
9016         default:
9017             g_assert_not_reached();
9018         }
9019 
9020         tcg_gen_ext32u_i64(tcg_res, tcg_res);
9021         write_fp_dreg(s, rd, tcg_res);
9022     }
9023 }
9024 
9025 static void handle_3same_64(DisasContext *s, int opcode, bool u,
9026                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
9027 {
9028     /* Handle 64x64->64 opcodes which are shared between the scalar
9029      * and vector 3-same groups. We cover every opcode where size == 3
9030      * is valid in either the three-reg-same (integer, not pairwise)
9031      * or scalar-three-reg-same groups.
9032      */
9033     TCGCond cond;
9034 
9035     switch (opcode) {
9036     case 0x1: /* SQADD */
9037         if (u) {
9038             gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9039         } else {
9040             gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9041         }
9042         break;
9043     case 0x5: /* SQSUB */
9044         if (u) {
9045             gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9046         } else {
9047             gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9048         }
9049         break;
9050     case 0x6: /* CMGT, CMHI */
9051         /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
9052          * We implement this using setcond (test) and then negating.
9053          */
9054         cond = u ? TCG_COND_GTU : TCG_COND_GT;
9055     do_cmop:
9056         tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
9057         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9058         break;
9059     case 0x7: /* CMGE, CMHS */
9060         cond = u ? TCG_COND_GEU : TCG_COND_GE;
9061         goto do_cmop;
9062     case 0x11: /* CMTST, CMEQ */
9063         if (u) {
9064             cond = TCG_COND_EQ;
9065             goto do_cmop;
9066         }
9067         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
9068         break;
9069     case 0x8: /* SSHL, USHL */
9070         if (u) {
9071             gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
9072         } else {
9073             gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
9074         }
9075         break;
9076     case 0x9: /* SQSHL, UQSHL */
9077         if (u) {
9078             gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9079         } else {
9080             gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9081         }
9082         break;
9083     case 0xa: /* SRSHL, URSHL */
9084         if (u) {
9085             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
9086         } else {
9087             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
9088         }
9089         break;
9090     case 0xb: /* SQRSHL, UQRSHL */
9091         if (u) {
9092             gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9093         } else {
9094             gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9095         }
9096         break;
9097     case 0x10: /* ADD, SUB */
9098         if (u) {
9099             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
9100         } else {
9101             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
9102         }
9103         break;
9104     default:
9105         g_assert_not_reached();
9106     }
9107 }
9108 
9109 /* Handle the 3-same-operands float operations; shared by the scalar
9110  * and vector encodings. The caller must filter out any encodings
9111  * not allocated for the encoding it is dealing with.
9112  */
9113 static void handle_3same_float(DisasContext *s, int size, int elements,
9114                                int fpopcode, int rd, int rn, int rm)
9115 {
9116     int pass;
9117     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9118 
9119     for (pass = 0; pass < elements; pass++) {
9120         if (size) {
9121             /* Double */
9122             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9123             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9124             TCGv_i64 tcg_res = tcg_temp_new_i64();
9125 
9126             read_vec_element(s, tcg_op1, rn, pass, MO_64);
9127             read_vec_element(s, tcg_op2, rm, pass, MO_64);
9128 
9129             switch (fpopcode) {
9130             case 0x39: /* FMLS */
9131                 /* As usual for ARM, separate negation for fused multiply-add */
9132                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
9133                 /* fall through */
9134             case 0x19: /* FMLA */
9135                 read_vec_element(s, tcg_res, rd, pass, MO_64);
9136                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
9137                                        tcg_res, fpst);
9138                 break;
9139             case 0x18: /* FMAXNM */
9140                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9141                 break;
9142             case 0x1a: /* FADD */
9143                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
9144                 break;
9145             case 0x1b: /* FMULX */
9146                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
9147                 break;
9148             case 0x1c: /* FCMEQ */
9149                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9150                 break;
9151             case 0x1e: /* FMAX */
9152                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
9153                 break;
9154             case 0x1f: /* FRECPS */
9155                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9156                 break;
9157             case 0x38: /* FMINNM */
9158                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9159                 break;
9160             case 0x3a: /* FSUB */
9161                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9162                 break;
9163             case 0x3e: /* FMIN */
9164                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
9165                 break;
9166             case 0x3f: /* FRSQRTS */
9167                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9168                 break;
9169             case 0x5b: /* FMUL */
9170                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
9171                 break;
9172             case 0x5c: /* FCMGE */
9173                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9174                 break;
9175             case 0x5d: /* FACGE */
9176                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9177                 break;
9178             case 0x5f: /* FDIV */
9179                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
9180                 break;
9181             case 0x7a: /* FABD */
9182                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9183                 gen_helper_vfp_absd(tcg_res, tcg_res);
9184                 break;
9185             case 0x7c: /* FCMGT */
9186                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9187                 break;
9188             case 0x7d: /* FACGT */
9189                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9190                 break;
9191             default:
9192                 g_assert_not_reached();
9193             }
9194 
9195             write_vec_element(s, tcg_res, rd, pass, MO_64);
9196         } else {
9197             /* Single */
9198             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9199             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9200             TCGv_i32 tcg_res = tcg_temp_new_i32();
9201 
9202             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9203             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9204 
9205             switch (fpopcode) {
9206             case 0x39: /* FMLS */
9207                 /* As usual for ARM, separate negation for fused multiply-add */
9208                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
9209                 /* fall through */
9210             case 0x19: /* FMLA */
9211                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9212                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
9213                                        tcg_res, fpst);
9214                 break;
9215             case 0x1a: /* FADD */
9216                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
9217                 break;
9218             case 0x1b: /* FMULX */
9219                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
9220                 break;
9221             case 0x1c: /* FCMEQ */
9222                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9223                 break;
9224             case 0x1e: /* FMAX */
9225                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
9226                 break;
9227             case 0x1f: /* FRECPS */
9228                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9229                 break;
9230             case 0x18: /* FMAXNM */
9231                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
9232                 break;
9233             case 0x38: /* FMINNM */
9234                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
9235                 break;
9236             case 0x3a: /* FSUB */
9237                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9238                 break;
9239             case 0x3e: /* FMIN */
9240                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9241                 break;
9242             case 0x3f: /* FRSQRTS */
9243                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9244                 break;
9245             case 0x5b: /* FMUL */
9246                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9247                 break;
9248             case 0x5c: /* FCMGE */
9249                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9250                 break;
9251             case 0x5d: /* FACGE */
9252                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9253                 break;
9254             case 0x5f: /* FDIV */
9255                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9256                 break;
9257             case 0x7a: /* FABD */
9258                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9259                 gen_helper_vfp_abss(tcg_res, tcg_res);
9260                 break;
9261             case 0x7c: /* FCMGT */
9262                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9263                 break;
9264             case 0x7d: /* FACGT */
9265                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9266                 break;
9267             default:
9268                 g_assert_not_reached();
9269             }
9270 
9271             if (elements == 1) {
9272                 /* scalar single so clear high part */
9273                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9274 
9275                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9276                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9277             } else {
9278                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9279             }
9280         }
9281     }
9282 
9283     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9284 }
9285 
9286 /* AdvSIMD scalar three same
9287  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
9288  * +-----+---+-----------+------+---+------+--------+---+------+------+
9289  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
9290  * +-----+---+-----------+------+---+------+--------+---+------+------+
9291  */
9292 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9293 {
9294     int rd = extract32(insn, 0, 5);
9295     int rn = extract32(insn, 5, 5);
9296     int opcode = extract32(insn, 11, 5);
9297     int rm = extract32(insn, 16, 5);
9298     int size = extract32(insn, 22, 2);
9299     bool u = extract32(insn, 29, 1);
9300     TCGv_i64 tcg_rd;
9301 
9302     if (opcode >= 0x18) {
9303         /* Floating point: U, size[1] and opcode indicate operation */
9304         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9305         switch (fpopcode) {
9306         case 0x1b: /* FMULX */
9307         case 0x1f: /* FRECPS */
9308         case 0x3f: /* FRSQRTS */
9309         case 0x5d: /* FACGE */
9310         case 0x7d: /* FACGT */
9311         case 0x1c: /* FCMEQ */
9312         case 0x5c: /* FCMGE */
9313         case 0x7c: /* FCMGT */
9314         case 0x7a: /* FABD */
9315             break;
9316         default:
9317             unallocated_encoding(s);
9318             return;
9319         }
9320 
9321         if (!fp_access_check(s)) {
9322             return;
9323         }
9324 
9325         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9326         return;
9327     }
9328 
9329     switch (opcode) {
9330     case 0x1: /* SQADD, UQADD */
9331     case 0x5: /* SQSUB, UQSUB */
9332     case 0x9: /* SQSHL, UQSHL */
9333     case 0xb: /* SQRSHL, UQRSHL */
9334         break;
9335     case 0x8: /* SSHL, USHL */
9336     case 0xa: /* SRSHL, URSHL */
9337     case 0x6: /* CMGT, CMHI */
9338     case 0x7: /* CMGE, CMHS */
9339     case 0x11: /* CMTST, CMEQ */
9340     case 0x10: /* ADD, SUB (vector) */
9341         if (size != 3) {
9342             unallocated_encoding(s);
9343             return;
9344         }
9345         break;
9346     case 0x16: /* SQDMULH, SQRDMULH (vector) */
9347         if (size != 1 && size != 2) {
9348             unallocated_encoding(s);
9349             return;
9350         }
9351         break;
9352     default:
9353         unallocated_encoding(s);
9354         return;
9355     }
9356 
9357     if (!fp_access_check(s)) {
9358         return;
9359     }
9360 
9361     tcg_rd = tcg_temp_new_i64();
9362 
9363     if (size == 3) {
9364         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9365         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9366 
9367         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9368     } else {
9369         /* Do a single operation on the lowest element in the vector.
9370          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9371          * no side effects for all these operations.
9372          * OPTME: special-purpose helpers would avoid doing some
9373          * unnecessary work in the helper for the 8 and 16 bit cases.
9374          */
9375         NeonGenTwoOpEnvFn *genenvfn;
9376         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9377         TCGv_i32 tcg_rm = tcg_temp_new_i32();
9378         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9379 
9380         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9381         read_vec_element_i32(s, tcg_rm, rm, 0, size);
9382 
9383         switch (opcode) {
9384         case 0x1: /* SQADD, UQADD */
9385         {
9386             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9387                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9388                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9389                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9390             };
9391             genenvfn = fns[size][u];
9392             break;
9393         }
9394         case 0x5: /* SQSUB, UQSUB */
9395         {
9396             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9397                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9398                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9399                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9400             };
9401             genenvfn = fns[size][u];
9402             break;
9403         }
9404         case 0x9: /* SQSHL, UQSHL */
9405         {
9406             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9407                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9408                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9409                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9410             };
9411             genenvfn = fns[size][u];
9412             break;
9413         }
9414         case 0xb: /* SQRSHL, UQRSHL */
9415         {
9416             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9417                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9418                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9419                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9420             };
9421             genenvfn = fns[size][u];
9422             break;
9423         }
9424         case 0x16: /* SQDMULH, SQRDMULH */
9425         {
9426             static NeonGenTwoOpEnvFn * const fns[2][2] = {
9427                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9428                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9429             };
9430             assert(size == 1 || size == 2);
9431             genenvfn = fns[size - 1][u];
9432             break;
9433         }
9434         default:
9435             g_assert_not_reached();
9436         }
9437 
9438         genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9439         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9440     }
9441 
9442     write_fp_dreg(s, rd, tcg_rd);
9443 }
9444 
9445 /* AdvSIMD scalar three same FP16
9446  *  31 30  29 28       24 23  22 21 20  16 15 14 13    11 10  9  5 4  0
9447  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9448  * | 0 1 | U | 1 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 | Rn | Rd |
9449  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9450  * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9451  * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9452  */
9453 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9454                                                   uint32_t insn)
9455 {
9456     int rd = extract32(insn, 0, 5);
9457     int rn = extract32(insn, 5, 5);
9458     int opcode = extract32(insn, 11, 3);
9459     int rm = extract32(insn, 16, 5);
9460     bool u = extract32(insn, 29, 1);
9461     bool a = extract32(insn, 23, 1);
9462     int fpopcode = opcode | (a << 3) |  (u << 4);
9463     TCGv_ptr fpst;
9464     TCGv_i32 tcg_op1;
9465     TCGv_i32 tcg_op2;
9466     TCGv_i32 tcg_res;
9467 
9468     switch (fpopcode) {
9469     case 0x03: /* FMULX */
9470     case 0x04: /* FCMEQ (reg) */
9471     case 0x07: /* FRECPS */
9472     case 0x0f: /* FRSQRTS */
9473     case 0x14: /* FCMGE (reg) */
9474     case 0x15: /* FACGE */
9475     case 0x1a: /* FABD */
9476     case 0x1c: /* FCMGT (reg) */
9477     case 0x1d: /* FACGT */
9478         break;
9479     default:
9480         unallocated_encoding(s);
9481         return;
9482     }
9483 
9484     if (!dc_isar_feature(aa64_fp16, s)) {
9485         unallocated_encoding(s);
9486     }
9487 
9488     if (!fp_access_check(s)) {
9489         return;
9490     }
9491 
9492     fpst = fpstatus_ptr(FPST_FPCR_F16);
9493 
9494     tcg_op1 = read_fp_hreg(s, rn);
9495     tcg_op2 = read_fp_hreg(s, rm);
9496     tcg_res = tcg_temp_new_i32();
9497 
9498     switch (fpopcode) {
9499     case 0x03: /* FMULX */
9500         gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9501         break;
9502     case 0x04: /* FCMEQ (reg) */
9503         gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9504         break;
9505     case 0x07: /* FRECPS */
9506         gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9507         break;
9508     case 0x0f: /* FRSQRTS */
9509         gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9510         break;
9511     case 0x14: /* FCMGE (reg) */
9512         gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9513         break;
9514     case 0x15: /* FACGE */
9515         gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9516         break;
9517     case 0x1a: /* FABD */
9518         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9519         tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9520         break;
9521     case 0x1c: /* FCMGT (reg) */
9522         gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9523         break;
9524     case 0x1d: /* FACGT */
9525         gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9526         break;
9527     default:
9528         g_assert_not_reached();
9529     }
9530 
9531     write_fp_sreg(s, rd, tcg_res);
9532 }
9533 
9534 /* AdvSIMD scalar three same extra
9535  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9536  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9537  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9538  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9539  */
9540 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9541                                                    uint32_t insn)
9542 {
9543     int rd = extract32(insn, 0, 5);
9544     int rn = extract32(insn, 5, 5);
9545     int opcode = extract32(insn, 11, 4);
9546     int rm = extract32(insn, 16, 5);
9547     int size = extract32(insn, 22, 2);
9548     bool u = extract32(insn, 29, 1);
9549     TCGv_i32 ele1, ele2, ele3;
9550     TCGv_i64 res;
9551     bool feature;
9552 
9553     switch (u * 16 + opcode) {
9554     case 0x10: /* SQRDMLAH (vector) */
9555     case 0x11: /* SQRDMLSH (vector) */
9556         if (size != 1 && size != 2) {
9557             unallocated_encoding(s);
9558             return;
9559         }
9560         feature = dc_isar_feature(aa64_rdm, s);
9561         break;
9562     default:
9563         unallocated_encoding(s);
9564         return;
9565     }
9566     if (!feature) {
9567         unallocated_encoding(s);
9568         return;
9569     }
9570     if (!fp_access_check(s)) {
9571         return;
9572     }
9573 
9574     /* Do a single operation on the lowest element in the vector.
9575      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9576      * with no side effects for all these operations.
9577      * OPTME: special-purpose helpers would avoid doing some
9578      * unnecessary work in the helper for the 16 bit cases.
9579      */
9580     ele1 = tcg_temp_new_i32();
9581     ele2 = tcg_temp_new_i32();
9582     ele3 = tcg_temp_new_i32();
9583 
9584     read_vec_element_i32(s, ele1, rn, 0, size);
9585     read_vec_element_i32(s, ele2, rm, 0, size);
9586     read_vec_element_i32(s, ele3, rd, 0, size);
9587 
9588     switch (opcode) {
9589     case 0x0: /* SQRDMLAH */
9590         if (size == 1) {
9591             gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9592         } else {
9593             gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9594         }
9595         break;
9596     case 0x1: /* SQRDMLSH */
9597         if (size == 1) {
9598             gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9599         } else {
9600             gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9601         }
9602         break;
9603     default:
9604         g_assert_not_reached();
9605     }
9606 
9607     res = tcg_temp_new_i64();
9608     tcg_gen_extu_i32_i64(res, ele3);
9609     write_fp_dreg(s, rd, res);
9610 }
9611 
9612 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9613                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9614                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9615 {
9616     /* Handle 64->64 opcodes which are shared between the scalar and
9617      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9618      * is valid in either group and also the double-precision fp ops.
9619      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9620      * requires them.
9621      */
9622     TCGCond cond;
9623 
9624     switch (opcode) {
9625     case 0x4: /* CLS, CLZ */
9626         if (u) {
9627             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9628         } else {
9629             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9630         }
9631         break;
9632     case 0x5: /* NOT */
9633         /* This opcode is shared with CNT and RBIT but we have earlier
9634          * enforced that size == 3 if and only if this is the NOT insn.
9635          */
9636         tcg_gen_not_i64(tcg_rd, tcg_rn);
9637         break;
9638     case 0x7: /* SQABS, SQNEG */
9639         if (u) {
9640             gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9641         } else {
9642             gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9643         }
9644         break;
9645     case 0xa: /* CMLT */
9646         /* 64 bit integer comparison against zero, result is
9647          * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9648          * subtracting 1.
9649          */
9650         cond = TCG_COND_LT;
9651     do_cmop:
9652         tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9653         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9654         break;
9655     case 0x8: /* CMGT, CMGE */
9656         cond = u ? TCG_COND_GE : TCG_COND_GT;
9657         goto do_cmop;
9658     case 0x9: /* CMEQ, CMLE */
9659         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9660         goto do_cmop;
9661     case 0xb: /* ABS, NEG */
9662         if (u) {
9663             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9664         } else {
9665             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9666         }
9667         break;
9668     case 0x2f: /* FABS */
9669         gen_helper_vfp_absd(tcg_rd, tcg_rn);
9670         break;
9671     case 0x6f: /* FNEG */
9672         gen_helper_vfp_negd(tcg_rd, tcg_rn);
9673         break;
9674     case 0x7f: /* FSQRT */
9675         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9676         break;
9677     case 0x1a: /* FCVTNS */
9678     case 0x1b: /* FCVTMS */
9679     case 0x1c: /* FCVTAS */
9680     case 0x3a: /* FCVTPS */
9681     case 0x3b: /* FCVTZS */
9682         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9683         break;
9684     case 0x5a: /* FCVTNU */
9685     case 0x5b: /* FCVTMU */
9686     case 0x5c: /* FCVTAU */
9687     case 0x7a: /* FCVTPU */
9688     case 0x7b: /* FCVTZU */
9689         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9690         break;
9691     case 0x18: /* FRINTN */
9692     case 0x19: /* FRINTM */
9693     case 0x38: /* FRINTP */
9694     case 0x39: /* FRINTZ */
9695     case 0x58: /* FRINTA */
9696     case 0x79: /* FRINTI */
9697         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9698         break;
9699     case 0x59: /* FRINTX */
9700         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9701         break;
9702     case 0x1e: /* FRINT32Z */
9703     case 0x5e: /* FRINT32X */
9704         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9705         break;
9706     case 0x1f: /* FRINT64Z */
9707     case 0x5f: /* FRINT64X */
9708         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9709         break;
9710     default:
9711         g_assert_not_reached();
9712     }
9713 }
9714 
9715 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9716                                    bool is_scalar, bool is_u, bool is_q,
9717                                    int size, int rn, int rd)
9718 {
9719     bool is_double = (size == MO_64);
9720     TCGv_ptr fpst;
9721 
9722     if (!fp_access_check(s)) {
9723         return;
9724     }
9725 
9726     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9727 
9728     if (is_double) {
9729         TCGv_i64 tcg_op = tcg_temp_new_i64();
9730         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9731         TCGv_i64 tcg_res = tcg_temp_new_i64();
9732         NeonGenTwoDoubleOpFn *genfn;
9733         bool swap = false;
9734         int pass;
9735 
9736         switch (opcode) {
9737         case 0x2e: /* FCMLT (zero) */
9738             swap = true;
9739             /* fallthrough */
9740         case 0x2c: /* FCMGT (zero) */
9741             genfn = gen_helper_neon_cgt_f64;
9742             break;
9743         case 0x2d: /* FCMEQ (zero) */
9744             genfn = gen_helper_neon_ceq_f64;
9745             break;
9746         case 0x6d: /* FCMLE (zero) */
9747             swap = true;
9748             /* fall through */
9749         case 0x6c: /* FCMGE (zero) */
9750             genfn = gen_helper_neon_cge_f64;
9751             break;
9752         default:
9753             g_assert_not_reached();
9754         }
9755 
9756         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9757             read_vec_element(s, tcg_op, rn, pass, MO_64);
9758             if (swap) {
9759                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9760             } else {
9761                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9762             }
9763             write_vec_element(s, tcg_res, rd, pass, MO_64);
9764         }
9765 
9766         clear_vec_high(s, !is_scalar, rd);
9767     } else {
9768         TCGv_i32 tcg_op = tcg_temp_new_i32();
9769         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9770         TCGv_i32 tcg_res = tcg_temp_new_i32();
9771         NeonGenTwoSingleOpFn *genfn;
9772         bool swap = false;
9773         int pass, maxpasses;
9774 
9775         if (size == MO_16) {
9776             switch (opcode) {
9777             case 0x2e: /* FCMLT (zero) */
9778                 swap = true;
9779                 /* fall through */
9780             case 0x2c: /* FCMGT (zero) */
9781                 genfn = gen_helper_advsimd_cgt_f16;
9782                 break;
9783             case 0x2d: /* FCMEQ (zero) */
9784                 genfn = gen_helper_advsimd_ceq_f16;
9785                 break;
9786             case 0x6d: /* FCMLE (zero) */
9787                 swap = true;
9788                 /* fall through */
9789             case 0x6c: /* FCMGE (zero) */
9790                 genfn = gen_helper_advsimd_cge_f16;
9791                 break;
9792             default:
9793                 g_assert_not_reached();
9794             }
9795         } else {
9796             switch (opcode) {
9797             case 0x2e: /* FCMLT (zero) */
9798                 swap = true;
9799                 /* fall through */
9800             case 0x2c: /* FCMGT (zero) */
9801                 genfn = gen_helper_neon_cgt_f32;
9802                 break;
9803             case 0x2d: /* FCMEQ (zero) */
9804                 genfn = gen_helper_neon_ceq_f32;
9805                 break;
9806             case 0x6d: /* FCMLE (zero) */
9807                 swap = true;
9808                 /* fall through */
9809             case 0x6c: /* FCMGE (zero) */
9810                 genfn = gen_helper_neon_cge_f32;
9811                 break;
9812             default:
9813                 g_assert_not_reached();
9814             }
9815         }
9816 
9817         if (is_scalar) {
9818             maxpasses = 1;
9819         } else {
9820             int vector_size = 8 << is_q;
9821             maxpasses = vector_size >> size;
9822         }
9823 
9824         for (pass = 0; pass < maxpasses; pass++) {
9825             read_vec_element_i32(s, tcg_op, rn, pass, size);
9826             if (swap) {
9827                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9828             } else {
9829                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9830             }
9831             if (is_scalar) {
9832                 write_fp_sreg(s, rd, tcg_res);
9833             } else {
9834                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9835             }
9836         }
9837 
9838         if (!is_scalar) {
9839             clear_vec_high(s, is_q, rd);
9840         }
9841     }
9842 }
9843 
9844 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9845                                     bool is_scalar, bool is_u, bool is_q,
9846                                     int size, int rn, int rd)
9847 {
9848     bool is_double = (size == 3);
9849     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9850 
9851     if (is_double) {
9852         TCGv_i64 tcg_op = tcg_temp_new_i64();
9853         TCGv_i64 tcg_res = tcg_temp_new_i64();
9854         int pass;
9855 
9856         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9857             read_vec_element(s, tcg_op, rn, pass, MO_64);
9858             switch (opcode) {
9859             case 0x3d: /* FRECPE */
9860                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9861                 break;
9862             case 0x3f: /* FRECPX */
9863                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9864                 break;
9865             case 0x7d: /* FRSQRTE */
9866                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9867                 break;
9868             default:
9869                 g_assert_not_reached();
9870             }
9871             write_vec_element(s, tcg_res, rd, pass, MO_64);
9872         }
9873         clear_vec_high(s, !is_scalar, rd);
9874     } else {
9875         TCGv_i32 tcg_op = tcg_temp_new_i32();
9876         TCGv_i32 tcg_res = tcg_temp_new_i32();
9877         int pass, maxpasses;
9878 
9879         if (is_scalar) {
9880             maxpasses = 1;
9881         } else {
9882             maxpasses = is_q ? 4 : 2;
9883         }
9884 
9885         for (pass = 0; pass < maxpasses; pass++) {
9886             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9887 
9888             switch (opcode) {
9889             case 0x3c: /* URECPE */
9890                 gen_helper_recpe_u32(tcg_res, tcg_op);
9891                 break;
9892             case 0x3d: /* FRECPE */
9893                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9894                 break;
9895             case 0x3f: /* FRECPX */
9896                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9897                 break;
9898             case 0x7d: /* FRSQRTE */
9899                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9900                 break;
9901             default:
9902                 g_assert_not_reached();
9903             }
9904 
9905             if (is_scalar) {
9906                 write_fp_sreg(s, rd, tcg_res);
9907             } else {
9908                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9909             }
9910         }
9911         if (!is_scalar) {
9912             clear_vec_high(s, is_q, rd);
9913         }
9914     }
9915 }
9916 
9917 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9918                                 int opcode, bool u, bool is_q,
9919                                 int size, int rn, int rd)
9920 {
9921     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9922      * in the source becomes a size element in the destination).
9923      */
9924     int pass;
9925     TCGv_i32 tcg_res[2];
9926     int destelt = is_q ? 2 : 0;
9927     int passes = scalar ? 1 : 2;
9928 
9929     if (scalar) {
9930         tcg_res[1] = tcg_constant_i32(0);
9931     }
9932 
9933     for (pass = 0; pass < passes; pass++) {
9934         TCGv_i64 tcg_op = tcg_temp_new_i64();
9935         NeonGenNarrowFn *genfn = NULL;
9936         NeonGenNarrowEnvFn *genenvfn = NULL;
9937 
9938         if (scalar) {
9939             read_vec_element(s, tcg_op, rn, pass, size + 1);
9940         } else {
9941             read_vec_element(s, tcg_op, rn, pass, MO_64);
9942         }
9943         tcg_res[pass] = tcg_temp_new_i32();
9944 
9945         switch (opcode) {
9946         case 0x12: /* XTN, SQXTUN */
9947         {
9948             static NeonGenNarrowFn * const xtnfns[3] = {
9949                 gen_helper_neon_narrow_u8,
9950                 gen_helper_neon_narrow_u16,
9951                 tcg_gen_extrl_i64_i32,
9952             };
9953             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9954                 gen_helper_neon_unarrow_sat8,
9955                 gen_helper_neon_unarrow_sat16,
9956                 gen_helper_neon_unarrow_sat32,
9957             };
9958             if (u) {
9959                 genenvfn = sqxtunfns[size];
9960             } else {
9961                 genfn = xtnfns[size];
9962             }
9963             break;
9964         }
9965         case 0x14: /* SQXTN, UQXTN */
9966         {
9967             static NeonGenNarrowEnvFn * const fns[3][2] = {
9968                 { gen_helper_neon_narrow_sat_s8,
9969                   gen_helper_neon_narrow_sat_u8 },
9970                 { gen_helper_neon_narrow_sat_s16,
9971                   gen_helper_neon_narrow_sat_u16 },
9972                 { gen_helper_neon_narrow_sat_s32,
9973                   gen_helper_neon_narrow_sat_u32 },
9974             };
9975             genenvfn = fns[size][u];
9976             break;
9977         }
9978         case 0x16: /* FCVTN, FCVTN2 */
9979             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9980             if (size == 2) {
9981                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
9982             } else {
9983                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9984                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9985                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9986                 TCGv_i32 ahp = get_ahp_flag();
9987 
9988                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9989                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9990                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9991                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9992             }
9993             break;
9994         case 0x36: /* BFCVTN, BFCVTN2 */
9995             {
9996                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9997                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
9998             }
9999             break;
10000         case 0x56:  /* FCVTXN, FCVTXN2 */
10001             /* 64 bit to 32 bit float conversion
10002              * with von Neumann rounding (round to odd)
10003              */
10004             assert(size == 2);
10005             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
10006             break;
10007         default:
10008             g_assert_not_reached();
10009         }
10010 
10011         if (genfn) {
10012             genfn(tcg_res[pass], tcg_op);
10013         } else if (genenvfn) {
10014             genenvfn(tcg_res[pass], cpu_env, tcg_op);
10015         }
10016     }
10017 
10018     for (pass = 0; pass < 2; pass++) {
10019         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
10020     }
10021     clear_vec_high(s, is_q, rd);
10022 }
10023 
10024 /* Remaining saturating accumulating ops */
10025 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
10026                                 bool is_q, int size, int rn, int rd)
10027 {
10028     bool is_double = (size == 3);
10029 
10030     if (is_double) {
10031         TCGv_i64 tcg_rn = tcg_temp_new_i64();
10032         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10033         int pass;
10034 
10035         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10036             read_vec_element(s, tcg_rn, rn, pass, MO_64);
10037             read_vec_element(s, tcg_rd, rd, pass, MO_64);
10038 
10039             if (is_u) { /* USQADD */
10040                 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10041             } else { /* SUQADD */
10042                 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10043             }
10044             write_vec_element(s, tcg_rd, rd, pass, MO_64);
10045         }
10046         clear_vec_high(s, !is_scalar, rd);
10047     } else {
10048         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10049         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10050         int pass, maxpasses;
10051 
10052         if (is_scalar) {
10053             maxpasses = 1;
10054         } else {
10055             maxpasses = is_q ? 4 : 2;
10056         }
10057 
10058         for (pass = 0; pass < maxpasses; pass++) {
10059             if (is_scalar) {
10060                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
10061                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
10062             } else {
10063                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
10064                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10065             }
10066 
10067             if (is_u) { /* USQADD */
10068                 switch (size) {
10069                 case 0:
10070                     gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10071                     break;
10072                 case 1:
10073                     gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10074                     break;
10075                 case 2:
10076                     gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10077                     break;
10078                 default:
10079                     g_assert_not_reached();
10080                 }
10081             } else { /* SUQADD */
10082                 switch (size) {
10083                 case 0:
10084                     gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10085                     break;
10086                 case 1:
10087                     gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10088                     break;
10089                 case 2:
10090                     gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10091                     break;
10092                 default:
10093                     g_assert_not_reached();
10094                 }
10095             }
10096 
10097             if (is_scalar) {
10098                 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
10099             }
10100             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10101         }
10102         clear_vec_high(s, is_q, rd);
10103     }
10104 }
10105 
10106 /* AdvSIMD scalar two reg misc
10107  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
10108  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10109  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
10110  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10111  */
10112 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
10113 {
10114     int rd = extract32(insn, 0, 5);
10115     int rn = extract32(insn, 5, 5);
10116     int opcode = extract32(insn, 12, 5);
10117     int size = extract32(insn, 22, 2);
10118     bool u = extract32(insn, 29, 1);
10119     bool is_fcvt = false;
10120     int rmode;
10121     TCGv_i32 tcg_rmode;
10122     TCGv_ptr tcg_fpstatus;
10123 
10124     switch (opcode) {
10125     case 0x3: /* USQADD / SUQADD*/
10126         if (!fp_access_check(s)) {
10127             return;
10128         }
10129         handle_2misc_satacc(s, true, u, false, size, rn, rd);
10130         return;
10131     case 0x7: /* SQABS / SQNEG */
10132         break;
10133     case 0xa: /* CMLT */
10134         if (u) {
10135             unallocated_encoding(s);
10136             return;
10137         }
10138         /* fall through */
10139     case 0x8: /* CMGT, CMGE */
10140     case 0x9: /* CMEQ, CMLE */
10141     case 0xb: /* ABS, NEG */
10142         if (size != 3) {
10143             unallocated_encoding(s);
10144             return;
10145         }
10146         break;
10147     case 0x12: /* SQXTUN */
10148         if (!u) {
10149             unallocated_encoding(s);
10150             return;
10151         }
10152         /* fall through */
10153     case 0x14: /* SQXTN, UQXTN */
10154         if (size == 3) {
10155             unallocated_encoding(s);
10156             return;
10157         }
10158         if (!fp_access_check(s)) {
10159             return;
10160         }
10161         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10162         return;
10163     case 0xc ... 0xf:
10164     case 0x16 ... 0x1d:
10165     case 0x1f:
10166         /* Floating point: U, size[1] and opcode indicate operation;
10167          * size[0] indicates single or double precision.
10168          */
10169         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10170         size = extract32(size, 0, 1) ? 3 : 2;
10171         switch (opcode) {
10172         case 0x2c: /* FCMGT (zero) */
10173         case 0x2d: /* FCMEQ (zero) */
10174         case 0x2e: /* FCMLT (zero) */
10175         case 0x6c: /* FCMGE (zero) */
10176         case 0x6d: /* FCMLE (zero) */
10177             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10178             return;
10179         case 0x1d: /* SCVTF */
10180         case 0x5d: /* UCVTF */
10181         {
10182             bool is_signed = (opcode == 0x1d);
10183             if (!fp_access_check(s)) {
10184                 return;
10185             }
10186             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10187             return;
10188         }
10189         case 0x3d: /* FRECPE */
10190         case 0x3f: /* FRECPX */
10191         case 0x7d: /* FRSQRTE */
10192             if (!fp_access_check(s)) {
10193                 return;
10194             }
10195             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10196             return;
10197         case 0x1a: /* FCVTNS */
10198         case 0x1b: /* FCVTMS */
10199         case 0x3a: /* FCVTPS */
10200         case 0x3b: /* FCVTZS */
10201         case 0x5a: /* FCVTNU */
10202         case 0x5b: /* FCVTMU */
10203         case 0x7a: /* FCVTPU */
10204         case 0x7b: /* FCVTZU */
10205             is_fcvt = true;
10206             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10207             break;
10208         case 0x1c: /* FCVTAS */
10209         case 0x5c: /* FCVTAU */
10210             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10211             is_fcvt = true;
10212             rmode = FPROUNDING_TIEAWAY;
10213             break;
10214         case 0x56: /* FCVTXN, FCVTXN2 */
10215             if (size == 2) {
10216                 unallocated_encoding(s);
10217                 return;
10218             }
10219             if (!fp_access_check(s)) {
10220                 return;
10221             }
10222             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10223             return;
10224         default:
10225             unallocated_encoding(s);
10226             return;
10227         }
10228         break;
10229     default:
10230         unallocated_encoding(s);
10231         return;
10232     }
10233 
10234     if (!fp_access_check(s)) {
10235         return;
10236     }
10237 
10238     if (is_fcvt) {
10239         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
10240         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
10241     } else {
10242         tcg_fpstatus = NULL;
10243         tcg_rmode = NULL;
10244     }
10245 
10246     if (size == 3) {
10247         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10248         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10249 
10250         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10251         write_fp_dreg(s, rd, tcg_rd);
10252     } else {
10253         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10254         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10255 
10256         read_vec_element_i32(s, tcg_rn, rn, 0, size);
10257 
10258         switch (opcode) {
10259         case 0x7: /* SQABS, SQNEG */
10260         {
10261             NeonGenOneOpEnvFn *genfn;
10262             static NeonGenOneOpEnvFn * const fns[3][2] = {
10263                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10264                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10265                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10266             };
10267             genfn = fns[size][u];
10268             genfn(tcg_rd, cpu_env, tcg_rn);
10269             break;
10270         }
10271         case 0x1a: /* FCVTNS */
10272         case 0x1b: /* FCVTMS */
10273         case 0x1c: /* FCVTAS */
10274         case 0x3a: /* FCVTPS */
10275         case 0x3b: /* FCVTZS */
10276             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10277                                  tcg_fpstatus);
10278             break;
10279         case 0x5a: /* FCVTNU */
10280         case 0x5b: /* FCVTMU */
10281         case 0x5c: /* FCVTAU */
10282         case 0x7a: /* FCVTPU */
10283         case 0x7b: /* FCVTZU */
10284             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10285                                  tcg_fpstatus);
10286             break;
10287         default:
10288             g_assert_not_reached();
10289         }
10290 
10291         write_fp_sreg(s, rd, tcg_rd);
10292     }
10293 
10294     if (is_fcvt) {
10295         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
10296     }
10297 }
10298 
10299 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10300 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10301                                  int immh, int immb, int opcode, int rn, int rd)
10302 {
10303     int size = 32 - clz32(immh) - 1;
10304     int immhb = immh << 3 | immb;
10305     int shift = 2 * (8 << size) - immhb;
10306     GVecGen2iFn *gvec_fn;
10307 
10308     if (extract32(immh, 3, 1) && !is_q) {
10309         unallocated_encoding(s);
10310         return;
10311     }
10312     tcg_debug_assert(size <= 3);
10313 
10314     if (!fp_access_check(s)) {
10315         return;
10316     }
10317 
10318     switch (opcode) {
10319     case 0x02: /* SSRA / USRA (accumulate) */
10320         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10321         break;
10322 
10323     case 0x08: /* SRI */
10324         gvec_fn = gen_gvec_sri;
10325         break;
10326 
10327     case 0x00: /* SSHR / USHR */
10328         if (is_u) {
10329             if (shift == 8 << size) {
10330                 /* Shift count the same size as element size produces zero.  */
10331                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10332                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
10333                 return;
10334             }
10335             gvec_fn = tcg_gen_gvec_shri;
10336         } else {
10337             /* Shift count the same size as element size produces all sign.  */
10338             if (shift == 8 << size) {
10339                 shift -= 1;
10340             }
10341             gvec_fn = tcg_gen_gvec_sari;
10342         }
10343         break;
10344 
10345     case 0x04: /* SRSHR / URSHR (rounding) */
10346         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10347         break;
10348 
10349     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10350         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10351         break;
10352 
10353     default:
10354         g_assert_not_reached();
10355     }
10356 
10357     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10358 }
10359 
10360 /* SHL/SLI - Vector shift left */
10361 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10362                                  int immh, int immb, int opcode, int rn, int rd)
10363 {
10364     int size = 32 - clz32(immh) - 1;
10365     int immhb = immh << 3 | immb;
10366     int shift = immhb - (8 << size);
10367 
10368     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10369     assert(size >= 0 && size <= 3);
10370 
10371     if (extract32(immh, 3, 1) && !is_q) {
10372         unallocated_encoding(s);
10373         return;
10374     }
10375 
10376     if (!fp_access_check(s)) {
10377         return;
10378     }
10379 
10380     if (insert) {
10381         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10382     } else {
10383         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10384     }
10385 }
10386 
10387 /* USHLL/SHLL - Vector shift left with widening */
10388 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10389                                  int immh, int immb, int opcode, int rn, int rd)
10390 {
10391     int size = 32 - clz32(immh) - 1;
10392     int immhb = immh << 3 | immb;
10393     int shift = immhb - (8 << size);
10394     int dsize = 64;
10395     int esize = 8 << size;
10396     int elements = dsize/esize;
10397     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10398     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10399     int i;
10400 
10401     if (size >= 3) {
10402         unallocated_encoding(s);
10403         return;
10404     }
10405 
10406     if (!fp_access_check(s)) {
10407         return;
10408     }
10409 
10410     /* For the LL variants the store is larger than the load,
10411      * so if rd == rn we would overwrite parts of our input.
10412      * So load everything right now and use shifts in the main loop.
10413      */
10414     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10415 
10416     for (i = 0; i < elements; i++) {
10417         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10418         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10419         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10420         write_vec_element(s, tcg_rd, rd, i, size + 1);
10421     }
10422 }
10423 
10424 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10425 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10426                                  int immh, int immb, int opcode, int rn, int rd)
10427 {
10428     int immhb = immh << 3 | immb;
10429     int size = 32 - clz32(immh) - 1;
10430     int dsize = 64;
10431     int esize = 8 << size;
10432     int elements = dsize/esize;
10433     int shift = (2 * esize) - immhb;
10434     bool round = extract32(opcode, 0, 1);
10435     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10436     TCGv_i64 tcg_round;
10437     int i;
10438 
10439     if (extract32(immh, 3, 1)) {
10440         unallocated_encoding(s);
10441         return;
10442     }
10443 
10444     if (!fp_access_check(s)) {
10445         return;
10446     }
10447 
10448     tcg_rn = tcg_temp_new_i64();
10449     tcg_rd = tcg_temp_new_i64();
10450     tcg_final = tcg_temp_new_i64();
10451     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10452 
10453     if (round) {
10454         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10455     } else {
10456         tcg_round = NULL;
10457     }
10458 
10459     for (i = 0; i < elements; i++) {
10460         read_vec_element(s, tcg_rn, rn, i, size+1);
10461         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10462                                 false, true, size+1, shift);
10463 
10464         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10465     }
10466 
10467     if (!is_q) {
10468         write_vec_element(s, tcg_final, rd, 0, MO_64);
10469     } else {
10470         write_vec_element(s, tcg_final, rd, 1, MO_64);
10471     }
10472 
10473     clear_vec_high(s, is_q, rd);
10474 }
10475 
10476 
10477 /* AdvSIMD shift by immediate
10478  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10479  * +---+---+---+-------------+------+------+--------+---+------+------+
10480  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10481  * +---+---+---+-------------+------+------+--------+---+------+------+
10482  */
10483 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10484 {
10485     int rd = extract32(insn, 0, 5);
10486     int rn = extract32(insn, 5, 5);
10487     int opcode = extract32(insn, 11, 5);
10488     int immb = extract32(insn, 16, 3);
10489     int immh = extract32(insn, 19, 4);
10490     bool is_u = extract32(insn, 29, 1);
10491     bool is_q = extract32(insn, 30, 1);
10492 
10493     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10494     assert(immh != 0);
10495 
10496     switch (opcode) {
10497     case 0x08: /* SRI */
10498         if (!is_u) {
10499             unallocated_encoding(s);
10500             return;
10501         }
10502         /* fall through */
10503     case 0x00: /* SSHR / USHR */
10504     case 0x02: /* SSRA / USRA (accumulate) */
10505     case 0x04: /* SRSHR / URSHR (rounding) */
10506     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10507         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10508         break;
10509     case 0x0a: /* SHL / SLI */
10510         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10511         break;
10512     case 0x10: /* SHRN */
10513     case 0x11: /* RSHRN / SQRSHRUN */
10514         if (is_u) {
10515             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10516                                    opcode, rn, rd);
10517         } else {
10518             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10519         }
10520         break;
10521     case 0x12: /* SQSHRN / UQSHRN */
10522     case 0x13: /* SQRSHRN / UQRSHRN */
10523         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10524                                opcode, rn, rd);
10525         break;
10526     case 0x14: /* SSHLL / USHLL */
10527         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10528         break;
10529     case 0x1c: /* SCVTF / UCVTF */
10530         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10531                                      opcode, rn, rd);
10532         break;
10533     case 0xc: /* SQSHLU */
10534         if (!is_u) {
10535             unallocated_encoding(s);
10536             return;
10537         }
10538         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10539         break;
10540     case 0xe: /* SQSHL, UQSHL */
10541         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10542         break;
10543     case 0x1f: /* FCVTZS/ FCVTZU */
10544         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10545         return;
10546     default:
10547         unallocated_encoding(s);
10548         return;
10549     }
10550 }
10551 
10552 /* Generate code to do a "long" addition or subtraction, ie one done in
10553  * TCGv_i64 on vector lanes twice the width specified by size.
10554  */
10555 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10556                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10557 {
10558     static NeonGenTwo64OpFn * const fns[3][2] = {
10559         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10560         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10561         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10562     };
10563     NeonGenTwo64OpFn *genfn;
10564     assert(size < 3);
10565 
10566     genfn = fns[size][is_sub];
10567     genfn(tcg_res, tcg_op1, tcg_op2);
10568 }
10569 
10570 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10571                                 int opcode, int rd, int rn, int rm)
10572 {
10573     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10574     TCGv_i64 tcg_res[2];
10575     int pass, accop;
10576 
10577     tcg_res[0] = tcg_temp_new_i64();
10578     tcg_res[1] = tcg_temp_new_i64();
10579 
10580     /* Does this op do an adding accumulate, a subtracting accumulate,
10581      * or no accumulate at all?
10582      */
10583     switch (opcode) {
10584     case 5:
10585     case 8:
10586     case 9:
10587         accop = 1;
10588         break;
10589     case 10:
10590     case 11:
10591         accop = -1;
10592         break;
10593     default:
10594         accop = 0;
10595         break;
10596     }
10597 
10598     if (accop != 0) {
10599         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10600         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10601     }
10602 
10603     /* size == 2 means two 32x32->64 operations; this is worth special
10604      * casing because we can generally handle it inline.
10605      */
10606     if (size == 2) {
10607         for (pass = 0; pass < 2; pass++) {
10608             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10609             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10610             TCGv_i64 tcg_passres;
10611             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10612 
10613             int elt = pass + is_q * 2;
10614 
10615             read_vec_element(s, tcg_op1, rn, elt, memop);
10616             read_vec_element(s, tcg_op2, rm, elt, memop);
10617 
10618             if (accop == 0) {
10619                 tcg_passres = tcg_res[pass];
10620             } else {
10621                 tcg_passres = tcg_temp_new_i64();
10622             }
10623 
10624             switch (opcode) {
10625             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10626                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10627                 break;
10628             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10629                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10630                 break;
10631             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10632             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10633             {
10634                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10635                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10636 
10637                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10638                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10639                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10640                                     tcg_passres,
10641                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10642                 break;
10643             }
10644             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10645             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10646             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10647                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10648                 break;
10649             case 9: /* SQDMLAL, SQDMLAL2 */
10650             case 11: /* SQDMLSL, SQDMLSL2 */
10651             case 13: /* SQDMULL, SQDMULL2 */
10652                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10653                 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10654                                                   tcg_passres, tcg_passres);
10655                 break;
10656             default:
10657                 g_assert_not_reached();
10658             }
10659 
10660             if (opcode == 9 || opcode == 11) {
10661                 /* saturating accumulate ops */
10662                 if (accop < 0) {
10663                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10664                 }
10665                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10666                                                   tcg_res[pass], tcg_passres);
10667             } else if (accop > 0) {
10668                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10669             } else if (accop < 0) {
10670                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10671             }
10672         }
10673     } else {
10674         /* size 0 or 1, generally helper functions */
10675         for (pass = 0; pass < 2; pass++) {
10676             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10677             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10678             TCGv_i64 tcg_passres;
10679             int elt = pass + is_q * 2;
10680 
10681             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10682             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10683 
10684             if (accop == 0) {
10685                 tcg_passres = tcg_res[pass];
10686             } else {
10687                 tcg_passres = tcg_temp_new_i64();
10688             }
10689 
10690             switch (opcode) {
10691             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10692             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10693             {
10694                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10695                 static NeonGenWidenFn * const widenfns[2][2] = {
10696                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10697                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10698                 };
10699                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10700 
10701                 widenfn(tcg_op2_64, tcg_op2);
10702                 widenfn(tcg_passres, tcg_op1);
10703                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10704                               tcg_passres, tcg_op2_64);
10705                 break;
10706             }
10707             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10708             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10709                 if (size == 0) {
10710                     if (is_u) {
10711                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10712                     } else {
10713                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10714                     }
10715                 } else {
10716                     if (is_u) {
10717                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10718                     } else {
10719                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10720                     }
10721                 }
10722                 break;
10723             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10724             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10725             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10726                 if (size == 0) {
10727                     if (is_u) {
10728                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10729                     } else {
10730                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10731                     }
10732                 } else {
10733                     if (is_u) {
10734                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10735                     } else {
10736                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10737                     }
10738                 }
10739                 break;
10740             case 9: /* SQDMLAL, SQDMLAL2 */
10741             case 11: /* SQDMLSL, SQDMLSL2 */
10742             case 13: /* SQDMULL, SQDMULL2 */
10743                 assert(size == 1);
10744                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10745                 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10746                                                   tcg_passres, tcg_passres);
10747                 break;
10748             default:
10749                 g_assert_not_reached();
10750             }
10751 
10752             if (accop != 0) {
10753                 if (opcode == 9 || opcode == 11) {
10754                     /* saturating accumulate ops */
10755                     if (accop < 0) {
10756                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10757                     }
10758                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10759                                                       tcg_res[pass],
10760                                                       tcg_passres);
10761                 } else {
10762                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10763                                   tcg_res[pass], tcg_passres);
10764                 }
10765             }
10766         }
10767     }
10768 
10769     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10770     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10771 }
10772 
10773 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10774                             int opcode, int rd, int rn, int rm)
10775 {
10776     TCGv_i64 tcg_res[2];
10777     int part = is_q ? 2 : 0;
10778     int pass;
10779 
10780     for (pass = 0; pass < 2; pass++) {
10781         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10782         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10783         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10784         static NeonGenWidenFn * const widenfns[3][2] = {
10785             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10786             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10787             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10788         };
10789         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10790 
10791         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10792         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10793         widenfn(tcg_op2_wide, tcg_op2);
10794         tcg_res[pass] = tcg_temp_new_i64();
10795         gen_neon_addl(size, (opcode == 3),
10796                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10797     }
10798 
10799     for (pass = 0; pass < 2; pass++) {
10800         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10801     }
10802 }
10803 
10804 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10805 {
10806     tcg_gen_addi_i64(in, in, 1U << 31);
10807     tcg_gen_extrh_i64_i32(res, in);
10808 }
10809 
10810 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10811                                  int opcode, int rd, int rn, int rm)
10812 {
10813     TCGv_i32 tcg_res[2];
10814     int part = is_q ? 2 : 0;
10815     int pass;
10816 
10817     for (pass = 0; pass < 2; pass++) {
10818         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10819         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10820         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10821         static NeonGenNarrowFn * const narrowfns[3][2] = {
10822             { gen_helper_neon_narrow_high_u8,
10823               gen_helper_neon_narrow_round_high_u8 },
10824             { gen_helper_neon_narrow_high_u16,
10825               gen_helper_neon_narrow_round_high_u16 },
10826             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10827         };
10828         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10829 
10830         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10831         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10832 
10833         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10834 
10835         tcg_res[pass] = tcg_temp_new_i32();
10836         gennarrow(tcg_res[pass], tcg_wideres);
10837     }
10838 
10839     for (pass = 0; pass < 2; pass++) {
10840         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10841     }
10842     clear_vec_high(s, is_q, rd);
10843 }
10844 
10845 /* AdvSIMD three different
10846  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10847  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10848  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10849  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10850  */
10851 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10852 {
10853     /* Instructions in this group fall into three basic classes
10854      * (in each case with the operation working on each element in
10855      * the input vectors):
10856      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10857      *     128 bit input)
10858      * (2) wide 64 x 128 -> 128
10859      * (3) narrowing 128 x 128 -> 64
10860      * Here we do initial decode, catch unallocated cases and
10861      * dispatch to separate functions for each class.
10862      */
10863     int is_q = extract32(insn, 30, 1);
10864     int is_u = extract32(insn, 29, 1);
10865     int size = extract32(insn, 22, 2);
10866     int opcode = extract32(insn, 12, 4);
10867     int rm = extract32(insn, 16, 5);
10868     int rn = extract32(insn, 5, 5);
10869     int rd = extract32(insn, 0, 5);
10870 
10871     switch (opcode) {
10872     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10873     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10874         /* 64 x 128 -> 128 */
10875         if (size == 3) {
10876             unallocated_encoding(s);
10877             return;
10878         }
10879         if (!fp_access_check(s)) {
10880             return;
10881         }
10882         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10883         break;
10884     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10885     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10886         /* 128 x 128 -> 64 */
10887         if (size == 3) {
10888             unallocated_encoding(s);
10889             return;
10890         }
10891         if (!fp_access_check(s)) {
10892             return;
10893         }
10894         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10895         break;
10896     case 14: /* PMULL, PMULL2 */
10897         if (is_u) {
10898             unallocated_encoding(s);
10899             return;
10900         }
10901         switch (size) {
10902         case 0: /* PMULL.P8 */
10903             if (!fp_access_check(s)) {
10904                 return;
10905             }
10906             /* The Q field specifies lo/hi half input for this insn.  */
10907             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10908                              gen_helper_neon_pmull_h);
10909             break;
10910 
10911         case 3: /* PMULL.P64 */
10912             if (!dc_isar_feature(aa64_pmull, s)) {
10913                 unallocated_encoding(s);
10914                 return;
10915             }
10916             if (!fp_access_check(s)) {
10917                 return;
10918             }
10919             /* The Q field specifies lo/hi half input for this insn.  */
10920             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10921                              gen_helper_gvec_pmull_q);
10922             break;
10923 
10924         default:
10925             unallocated_encoding(s);
10926             break;
10927         }
10928         return;
10929     case 9: /* SQDMLAL, SQDMLAL2 */
10930     case 11: /* SQDMLSL, SQDMLSL2 */
10931     case 13: /* SQDMULL, SQDMULL2 */
10932         if (is_u || size == 0) {
10933             unallocated_encoding(s);
10934             return;
10935         }
10936         /* fall through */
10937     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10938     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10939     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10940     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10941     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10942     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10943     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10944         /* 64 x 64 -> 128 */
10945         if (size == 3) {
10946             unallocated_encoding(s);
10947             return;
10948         }
10949         if (!fp_access_check(s)) {
10950             return;
10951         }
10952 
10953         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10954         break;
10955     default:
10956         /* opcode 15 not allocated */
10957         unallocated_encoding(s);
10958         break;
10959     }
10960 }
10961 
10962 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10963 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10964 {
10965     int rd = extract32(insn, 0, 5);
10966     int rn = extract32(insn, 5, 5);
10967     int rm = extract32(insn, 16, 5);
10968     int size = extract32(insn, 22, 2);
10969     bool is_u = extract32(insn, 29, 1);
10970     bool is_q = extract32(insn, 30, 1);
10971 
10972     if (!fp_access_check(s)) {
10973         return;
10974     }
10975 
10976     switch (size + 4 * is_u) {
10977     case 0: /* AND */
10978         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10979         return;
10980     case 1: /* BIC */
10981         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10982         return;
10983     case 2: /* ORR */
10984         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10985         return;
10986     case 3: /* ORN */
10987         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10988         return;
10989     case 4: /* EOR */
10990         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10991         return;
10992 
10993     case 5: /* BSL bitwise select */
10994         gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
10995         return;
10996     case 6: /* BIT, bitwise insert if true */
10997         gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
10998         return;
10999     case 7: /* BIF, bitwise insert if false */
11000         gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
11001         return;
11002 
11003     default:
11004         g_assert_not_reached();
11005     }
11006 }
11007 
11008 /* Pairwise op subgroup of C3.6.16.
11009  *
11010  * This is called directly or via the handle_3same_float for float pairwise
11011  * operations where the opcode and size are calculated differently.
11012  */
11013 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
11014                                    int size, int rn, int rm, int rd)
11015 {
11016     TCGv_ptr fpst;
11017     int pass;
11018 
11019     /* Floating point operations need fpst */
11020     if (opcode >= 0x58) {
11021         fpst = fpstatus_ptr(FPST_FPCR);
11022     } else {
11023         fpst = NULL;
11024     }
11025 
11026     if (!fp_access_check(s)) {
11027         return;
11028     }
11029 
11030     /* These operations work on the concatenated rm:rn, with each pair of
11031      * adjacent elements being operated on to produce an element in the result.
11032      */
11033     if (size == 3) {
11034         TCGv_i64 tcg_res[2];
11035 
11036         for (pass = 0; pass < 2; pass++) {
11037             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11038             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11039             int passreg = (pass == 0) ? rn : rm;
11040 
11041             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
11042             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
11043             tcg_res[pass] = tcg_temp_new_i64();
11044 
11045             switch (opcode) {
11046             case 0x17: /* ADDP */
11047                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11048                 break;
11049             case 0x58: /* FMAXNMP */
11050                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11051                 break;
11052             case 0x5a: /* FADDP */
11053                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11054                 break;
11055             case 0x5e: /* FMAXP */
11056                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11057                 break;
11058             case 0x78: /* FMINNMP */
11059                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11060                 break;
11061             case 0x7e: /* FMINP */
11062                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11063                 break;
11064             default:
11065                 g_assert_not_reached();
11066             }
11067         }
11068 
11069         for (pass = 0; pass < 2; pass++) {
11070             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11071         }
11072     } else {
11073         int maxpass = is_q ? 4 : 2;
11074         TCGv_i32 tcg_res[4];
11075 
11076         for (pass = 0; pass < maxpass; pass++) {
11077             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11078             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11079             NeonGenTwoOpFn *genfn = NULL;
11080             int passreg = pass < (maxpass / 2) ? rn : rm;
11081             int passelt = (is_q && (pass & 1)) ? 2 : 0;
11082 
11083             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
11084             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
11085             tcg_res[pass] = tcg_temp_new_i32();
11086 
11087             switch (opcode) {
11088             case 0x17: /* ADDP */
11089             {
11090                 static NeonGenTwoOpFn * const fns[3] = {
11091                     gen_helper_neon_padd_u8,
11092                     gen_helper_neon_padd_u16,
11093                     tcg_gen_add_i32,
11094                 };
11095                 genfn = fns[size];
11096                 break;
11097             }
11098             case 0x14: /* SMAXP, UMAXP */
11099             {
11100                 static NeonGenTwoOpFn * const fns[3][2] = {
11101                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
11102                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
11103                     { tcg_gen_smax_i32, tcg_gen_umax_i32 },
11104                 };
11105                 genfn = fns[size][u];
11106                 break;
11107             }
11108             case 0x15: /* SMINP, UMINP */
11109             {
11110                 static NeonGenTwoOpFn * const fns[3][2] = {
11111                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
11112                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
11113                     { tcg_gen_smin_i32, tcg_gen_umin_i32 },
11114                 };
11115                 genfn = fns[size][u];
11116                 break;
11117             }
11118             /* The FP operations are all on single floats (32 bit) */
11119             case 0x58: /* FMAXNMP */
11120                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11121                 break;
11122             case 0x5a: /* FADDP */
11123                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11124                 break;
11125             case 0x5e: /* FMAXP */
11126                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11127                 break;
11128             case 0x78: /* FMINNMP */
11129                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11130                 break;
11131             case 0x7e: /* FMINP */
11132                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11133                 break;
11134             default:
11135                 g_assert_not_reached();
11136             }
11137 
11138             /* FP ops called directly, otherwise call now */
11139             if (genfn) {
11140                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
11141             }
11142         }
11143 
11144         for (pass = 0; pass < maxpass; pass++) {
11145             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11146         }
11147         clear_vec_high(s, is_q, rd);
11148     }
11149 }
11150 
11151 /* Floating point op subgroup of C3.6.16. */
11152 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
11153 {
11154     /* For floating point ops, the U, size[1] and opcode bits
11155      * together indicate the operation. size[0] indicates single
11156      * or double.
11157      */
11158     int fpopcode = extract32(insn, 11, 5)
11159         | (extract32(insn, 23, 1) << 5)
11160         | (extract32(insn, 29, 1) << 6);
11161     int is_q = extract32(insn, 30, 1);
11162     int size = extract32(insn, 22, 1);
11163     int rm = extract32(insn, 16, 5);
11164     int rn = extract32(insn, 5, 5);
11165     int rd = extract32(insn, 0, 5);
11166 
11167     int datasize = is_q ? 128 : 64;
11168     int esize = 32 << size;
11169     int elements = datasize / esize;
11170 
11171     if (size == 1 && !is_q) {
11172         unallocated_encoding(s);
11173         return;
11174     }
11175 
11176     switch (fpopcode) {
11177     case 0x58: /* FMAXNMP */
11178     case 0x5a: /* FADDP */
11179     case 0x5e: /* FMAXP */
11180     case 0x78: /* FMINNMP */
11181     case 0x7e: /* FMINP */
11182         if (size && !is_q) {
11183             unallocated_encoding(s);
11184             return;
11185         }
11186         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11187                                rn, rm, rd);
11188         return;
11189     case 0x1b: /* FMULX */
11190     case 0x1f: /* FRECPS */
11191     case 0x3f: /* FRSQRTS */
11192     case 0x5d: /* FACGE */
11193     case 0x7d: /* FACGT */
11194     case 0x19: /* FMLA */
11195     case 0x39: /* FMLS */
11196     case 0x18: /* FMAXNM */
11197     case 0x1a: /* FADD */
11198     case 0x1c: /* FCMEQ */
11199     case 0x1e: /* FMAX */
11200     case 0x38: /* FMINNM */
11201     case 0x3a: /* FSUB */
11202     case 0x3e: /* FMIN */
11203     case 0x5b: /* FMUL */
11204     case 0x5c: /* FCMGE */
11205     case 0x5f: /* FDIV */
11206     case 0x7a: /* FABD */
11207     case 0x7c: /* FCMGT */
11208         if (!fp_access_check(s)) {
11209             return;
11210         }
11211         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11212         return;
11213 
11214     case 0x1d: /* FMLAL  */
11215     case 0x3d: /* FMLSL  */
11216     case 0x59: /* FMLAL2 */
11217     case 0x79: /* FMLSL2 */
11218         if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11219             unallocated_encoding(s);
11220             return;
11221         }
11222         if (fp_access_check(s)) {
11223             int is_s = extract32(insn, 23, 1);
11224             int is_2 = extract32(insn, 29, 1);
11225             int data = (is_2 << 1) | is_s;
11226             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11227                                vec_full_reg_offset(s, rn),
11228                                vec_full_reg_offset(s, rm), cpu_env,
11229                                is_q ? 16 : 8, vec_full_reg_size(s),
11230                                data, gen_helper_gvec_fmlal_a64);
11231         }
11232         return;
11233 
11234     default:
11235         unallocated_encoding(s);
11236         return;
11237     }
11238 }
11239 
11240 /* Integer op subgroup of C3.6.16. */
11241 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11242 {
11243     int is_q = extract32(insn, 30, 1);
11244     int u = extract32(insn, 29, 1);
11245     int size = extract32(insn, 22, 2);
11246     int opcode = extract32(insn, 11, 5);
11247     int rm = extract32(insn, 16, 5);
11248     int rn = extract32(insn, 5, 5);
11249     int rd = extract32(insn, 0, 5);
11250     int pass;
11251     TCGCond cond;
11252 
11253     switch (opcode) {
11254     case 0x13: /* MUL, PMUL */
11255         if (u && size != 0) {
11256             unallocated_encoding(s);
11257             return;
11258         }
11259         /* fall through */
11260     case 0x0: /* SHADD, UHADD */
11261     case 0x2: /* SRHADD, URHADD */
11262     case 0x4: /* SHSUB, UHSUB */
11263     case 0xc: /* SMAX, UMAX */
11264     case 0xd: /* SMIN, UMIN */
11265     case 0xe: /* SABD, UABD */
11266     case 0xf: /* SABA, UABA */
11267     case 0x12: /* MLA, MLS */
11268         if (size == 3) {
11269             unallocated_encoding(s);
11270             return;
11271         }
11272         break;
11273     case 0x16: /* SQDMULH, SQRDMULH */
11274         if (size == 0 || size == 3) {
11275             unallocated_encoding(s);
11276             return;
11277         }
11278         break;
11279     default:
11280         if (size == 3 && !is_q) {
11281             unallocated_encoding(s);
11282             return;
11283         }
11284         break;
11285     }
11286 
11287     if (!fp_access_check(s)) {
11288         return;
11289     }
11290 
11291     switch (opcode) {
11292     case 0x01: /* SQADD, UQADD */
11293         if (u) {
11294             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11295         } else {
11296             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11297         }
11298         return;
11299     case 0x05: /* SQSUB, UQSUB */
11300         if (u) {
11301             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11302         } else {
11303             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11304         }
11305         return;
11306     case 0x08: /* SSHL, USHL */
11307         if (u) {
11308             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11309         } else {
11310             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11311         }
11312         return;
11313     case 0x0c: /* SMAX, UMAX */
11314         if (u) {
11315             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11316         } else {
11317             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11318         }
11319         return;
11320     case 0x0d: /* SMIN, UMIN */
11321         if (u) {
11322             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11323         } else {
11324             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11325         }
11326         return;
11327     case 0xe: /* SABD, UABD */
11328         if (u) {
11329             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11330         } else {
11331             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11332         }
11333         return;
11334     case 0xf: /* SABA, UABA */
11335         if (u) {
11336             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11337         } else {
11338             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11339         }
11340         return;
11341     case 0x10: /* ADD, SUB */
11342         if (u) {
11343             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11344         } else {
11345             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11346         }
11347         return;
11348     case 0x13: /* MUL, PMUL */
11349         if (!u) { /* MUL */
11350             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11351         } else {  /* PMUL */
11352             gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11353         }
11354         return;
11355     case 0x12: /* MLA, MLS */
11356         if (u) {
11357             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11358         } else {
11359             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11360         }
11361         return;
11362     case 0x16: /* SQDMULH, SQRDMULH */
11363         {
11364             static gen_helper_gvec_3_ptr * const fns[2][2] = {
11365                 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11366                 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11367             };
11368             gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11369         }
11370         return;
11371     case 0x11:
11372         if (!u) { /* CMTST */
11373             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11374             return;
11375         }
11376         /* else CMEQ */
11377         cond = TCG_COND_EQ;
11378         goto do_gvec_cmp;
11379     case 0x06: /* CMGT, CMHI */
11380         cond = u ? TCG_COND_GTU : TCG_COND_GT;
11381         goto do_gvec_cmp;
11382     case 0x07: /* CMGE, CMHS */
11383         cond = u ? TCG_COND_GEU : TCG_COND_GE;
11384     do_gvec_cmp:
11385         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11386                          vec_full_reg_offset(s, rn),
11387                          vec_full_reg_offset(s, rm),
11388                          is_q ? 16 : 8, vec_full_reg_size(s));
11389         return;
11390     }
11391 
11392     if (size == 3) {
11393         assert(is_q);
11394         for (pass = 0; pass < 2; pass++) {
11395             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11396             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11397             TCGv_i64 tcg_res = tcg_temp_new_i64();
11398 
11399             read_vec_element(s, tcg_op1, rn, pass, MO_64);
11400             read_vec_element(s, tcg_op2, rm, pass, MO_64);
11401 
11402             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11403 
11404             write_vec_element(s, tcg_res, rd, pass, MO_64);
11405         }
11406     } else {
11407         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11408             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11409             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11410             TCGv_i32 tcg_res = tcg_temp_new_i32();
11411             NeonGenTwoOpFn *genfn = NULL;
11412             NeonGenTwoOpEnvFn *genenvfn = NULL;
11413 
11414             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11415             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11416 
11417             switch (opcode) {
11418             case 0x0: /* SHADD, UHADD */
11419             {
11420                 static NeonGenTwoOpFn * const fns[3][2] = {
11421                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11422                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11423                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11424                 };
11425                 genfn = fns[size][u];
11426                 break;
11427             }
11428             case 0x2: /* SRHADD, URHADD */
11429             {
11430                 static NeonGenTwoOpFn * const fns[3][2] = {
11431                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11432                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11433                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11434                 };
11435                 genfn = fns[size][u];
11436                 break;
11437             }
11438             case 0x4: /* SHSUB, UHSUB */
11439             {
11440                 static NeonGenTwoOpFn * const fns[3][2] = {
11441                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11442                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11443                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11444                 };
11445                 genfn = fns[size][u];
11446                 break;
11447             }
11448             case 0x9: /* SQSHL, UQSHL */
11449             {
11450                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11451                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11452                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11453                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11454                 };
11455                 genenvfn = fns[size][u];
11456                 break;
11457             }
11458             case 0xa: /* SRSHL, URSHL */
11459             {
11460                 static NeonGenTwoOpFn * const fns[3][2] = {
11461                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11462                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11463                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11464                 };
11465                 genfn = fns[size][u];
11466                 break;
11467             }
11468             case 0xb: /* SQRSHL, UQRSHL */
11469             {
11470                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11471                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11472                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11473                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11474                 };
11475                 genenvfn = fns[size][u];
11476                 break;
11477             }
11478             default:
11479                 g_assert_not_reached();
11480             }
11481 
11482             if (genenvfn) {
11483                 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11484             } else {
11485                 genfn(tcg_res, tcg_op1, tcg_op2);
11486             }
11487 
11488             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11489         }
11490     }
11491     clear_vec_high(s, is_q, rd);
11492 }
11493 
11494 /* AdvSIMD three same
11495  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
11496  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11497  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
11498  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11499  */
11500 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11501 {
11502     int opcode = extract32(insn, 11, 5);
11503 
11504     switch (opcode) {
11505     case 0x3: /* logic ops */
11506         disas_simd_3same_logic(s, insn);
11507         break;
11508     case 0x17: /* ADDP */
11509     case 0x14: /* SMAXP, UMAXP */
11510     case 0x15: /* SMINP, UMINP */
11511     {
11512         /* Pairwise operations */
11513         int is_q = extract32(insn, 30, 1);
11514         int u = extract32(insn, 29, 1);
11515         int size = extract32(insn, 22, 2);
11516         int rm = extract32(insn, 16, 5);
11517         int rn = extract32(insn, 5, 5);
11518         int rd = extract32(insn, 0, 5);
11519         if (opcode == 0x17) {
11520             if (u || (size == 3 && !is_q)) {
11521                 unallocated_encoding(s);
11522                 return;
11523             }
11524         } else {
11525             if (size == 3) {
11526                 unallocated_encoding(s);
11527                 return;
11528             }
11529         }
11530         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11531         break;
11532     }
11533     case 0x18 ... 0x31:
11534         /* floating point ops, sz[1] and U are part of opcode */
11535         disas_simd_3same_float(s, insn);
11536         break;
11537     default:
11538         disas_simd_3same_int(s, insn);
11539         break;
11540     }
11541 }
11542 
11543 /*
11544  * Advanced SIMD three same (ARMv8.2 FP16 variants)
11545  *
11546  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
11547  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11548  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
11549  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11550  *
11551  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11552  * (register), FACGE, FABD, FCMGT (register) and FACGT.
11553  *
11554  */
11555 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11556 {
11557     int opcode = extract32(insn, 11, 3);
11558     int u = extract32(insn, 29, 1);
11559     int a = extract32(insn, 23, 1);
11560     int is_q = extract32(insn, 30, 1);
11561     int rm = extract32(insn, 16, 5);
11562     int rn = extract32(insn, 5, 5);
11563     int rd = extract32(insn, 0, 5);
11564     /*
11565      * For these floating point ops, the U, a and opcode bits
11566      * together indicate the operation.
11567      */
11568     int fpopcode = opcode | (a << 3) | (u << 4);
11569     int datasize = is_q ? 128 : 64;
11570     int elements = datasize / 16;
11571     bool pairwise;
11572     TCGv_ptr fpst;
11573     int pass;
11574 
11575     switch (fpopcode) {
11576     case 0x0: /* FMAXNM */
11577     case 0x1: /* FMLA */
11578     case 0x2: /* FADD */
11579     case 0x3: /* FMULX */
11580     case 0x4: /* FCMEQ */
11581     case 0x6: /* FMAX */
11582     case 0x7: /* FRECPS */
11583     case 0x8: /* FMINNM */
11584     case 0x9: /* FMLS */
11585     case 0xa: /* FSUB */
11586     case 0xe: /* FMIN */
11587     case 0xf: /* FRSQRTS */
11588     case 0x13: /* FMUL */
11589     case 0x14: /* FCMGE */
11590     case 0x15: /* FACGE */
11591     case 0x17: /* FDIV */
11592     case 0x1a: /* FABD */
11593     case 0x1c: /* FCMGT */
11594     case 0x1d: /* FACGT */
11595         pairwise = false;
11596         break;
11597     case 0x10: /* FMAXNMP */
11598     case 0x12: /* FADDP */
11599     case 0x16: /* FMAXP */
11600     case 0x18: /* FMINNMP */
11601     case 0x1e: /* FMINP */
11602         pairwise = true;
11603         break;
11604     default:
11605         unallocated_encoding(s);
11606         return;
11607     }
11608 
11609     if (!dc_isar_feature(aa64_fp16, s)) {
11610         unallocated_encoding(s);
11611         return;
11612     }
11613 
11614     if (!fp_access_check(s)) {
11615         return;
11616     }
11617 
11618     fpst = fpstatus_ptr(FPST_FPCR_F16);
11619 
11620     if (pairwise) {
11621         int maxpass = is_q ? 8 : 4;
11622         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11623         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11624         TCGv_i32 tcg_res[8];
11625 
11626         for (pass = 0; pass < maxpass; pass++) {
11627             int passreg = pass < (maxpass / 2) ? rn : rm;
11628             int passelt = (pass << 1) & (maxpass - 1);
11629 
11630             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11631             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11632             tcg_res[pass] = tcg_temp_new_i32();
11633 
11634             switch (fpopcode) {
11635             case 0x10: /* FMAXNMP */
11636                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11637                                            fpst);
11638                 break;
11639             case 0x12: /* FADDP */
11640                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11641                 break;
11642             case 0x16: /* FMAXP */
11643                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11644                 break;
11645             case 0x18: /* FMINNMP */
11646                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11647                                            fpst);
11648                 break;
11649             case 0x1e: /* FMINP */
11650                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11651                 break;
11652             default:
11653                 g_assert_not_reached();
11654             }
11655         }
11656 
11657         for (pass = 0; pass < maxpass; pass++) {
11658             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11659         }
11660     } else {
11661         for (pass = 0; pass < elements; pass++) {
11662             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11663             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11664             TCGv_i32 tcg_res = tcg_temp_new_i32();
11665 
11666             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11667             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11668 
11669             switch (fpopcode) {
11670             case 0x0: /* FMAXNM */
11671                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11672                 break;
11673             case 0x1: /* FMLA */
11674                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11675                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11676                                            fpst);
11677                 break;
11678             case 0x2: /* FADD */
11679                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11680                 break;
11681             case 0x3: /* FMULX */
11682                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11683                 break;
11684             case 0x4: /* FCMEQ */
11685                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11686                 break;
11687             case 0x6: /* FMAX */
11688                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11689                 break;
11690             case 0x7: /* FRECPS */
11691                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11692                 break;
11693             case 0x8: /* FMINNM */
11694                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11695                 break;
11696             case 0x9: /* FMLS */
11697                 /* As usual for ARM, separate negation for fused multiply-add */
11698                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11699                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11700                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11701                                            fpst);
11702                 break;
11703             case 0xa: /* FSUB */
11704                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11705                 break;
11706             case 0xe: /* FMIN */
11707                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11708                 break;
11709             case 0xf: /* FRSQRTS */
11710                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11711                 break;
11712             case 0x13: /* FMUL */
11713                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11714                 break;
11715             case 0x14: /* FCMGE */
11716                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11717                 break;
11718             case 0x15: /* FACGE */
11719                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11720                 break;
11721             case 0x17: /* FDIV */
11722                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11723                 break;
11724             case 0x1a: /* FABD */
11725                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11726                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11727                 break;
11728             case 0x1c: /* FCMGT */
11729                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11730                 break;
11731             case 0x1d: /* FACGT */
11732                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11733                 break;
11734             default:
11735                 g_assert_not_reached();
11736             }
11737 
11738             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11739         }
11740     }
11741 
11742     clear_vec_high(s, is_q, rd);
11743 }
11744 
11745 /* AdvSIMD three same extra
11746  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
11747  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11748  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
11749  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11750  */
11751 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11752 {
11753     int rd = extract32(insn, 0, 5);
11754     int rn = extract32(insn, 5, 5);
11755     int opcode = extract32(insn, 11, 4);
11756     int rm = extract32(insn, 16, 5);
11757     int size = extract32(insn, 22, 2);
11758     bool u = extract32(insn, 29, 1);
11759     bool is_q = extract32(insn, 30, 1);
11760     bool feature;
11761     int rot;
11762 
11763     switch (u * 16 + opcode) {
11764     case 0x10: /* SQRDMLAH (vector) */
11765     case 0x11: /* SQRDMLSH (vector) */
11766         if (size != 1 && size != 2) {
11767             unallocated_encoding(s);
11768             return;
11769         }
11770         feature = dc_isar_feature(aa64_rdm, s);
11771         break;
11772     case 0x02: /* SDOT (vector) */
11773     case 0x12: /* UDOT (vector) */
11774         if (size != MO_32) {
11775             unallocated_encoding(s);
11776             return;
11777         }
11778         feature = dc_isar_feature(aa64_dp, s);
11779         break;
11780     case 0x03: /* USDOT */
11781         if (size != MO_32) {
11782             unallocated_encoding(s);
11783             return;
11784         }
11785         feature = dc_isar_feature(aa64_i8mm, s);
11786         break;
11787     case 0x04: /* SMMLA */
11788     case 0x14: /* UMMLA */
11789     case 0x05: /* USMMLA */
11790         if (!is_q || size != MO_32) {
11791             unallocated_encoding(s);
11792             return;
11793         }
11794         feature = dc_isar_feature(aa64_i8mm, s);
11795         break;
11796     case 0x18: /* FCMLA, #0 */
11797     case 0x19: /* FCMLA, #90 */
11798     case 0x1a: /* FCMLA, #180 */
11799     case 0x1b: /* FCMLA, #270 */
11800     case 0x1c: /* FCADD, #90 */
11801     case 0x1e: /* FCADD, #270 */
11802         if (size == 0
11803             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11804             || (size == 3 && !is_q)) {
11805             unallocated_encoding(s);
11806             return;
11807         }
11808         feature = dc_isar_feature(aa64_fcma, s);
11809         break;
11810     case 0x1d: /* BFMMLA */
11811         if (size != MO_16 || !is_q) {
11812             unallocated_encoding(s);
11813             return;
11814         }
11815         feature = dc_isar_feature(aa64_bf16, s);
11816         break;
11817     case 0x1f:
11818         switch (size) {
11819         case 1: /* BFDOT */
11820         case 3: /* BFMLAL{B,T} */
11821             feature = dc_isar_feature(aa64_bf16, s);
11822             break;
11823         default:
11824             unallocated_encoding(s);
11825             return;
11826         }
11827         break;
11828     default:
11829         unallocated_encoding(s);
11830         return;
11831     }
11832     if (!feature) {
11833         unallocated_encoding(s);
11834         return;
11835     }
11836     if (!fp_access_check(s)) {
11837         return;
11838     }
11839 
11840     switch (opcode) {
11841     case 0x0: /* SQRDMLAH (vector) */
11842         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11843         return;
11844 
11845     case 0x1: /* SQRDMLSH (vector) */
11846         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11847         return;
11848 
11849     case 0x2: /* SDOT / UDOT */
11850         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11851                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11852         return;
11853 
11854     case 0x3: /* USDOT */
11855         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11856         return;
11857 
11858     case 0x04: /* SMMLA, UMMLA */
11859         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11860                          u ? gen_helper_gvec_ummla_b
11861                          : gen_helper_gvec_smmla_b);
11862         return;
11863     case 0x05: /* USMMLA */
11864         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11865         return;
11866 
11867     case 0x8: /* FCMLA, #0 */
11868     case 0x9: /* FCMLA, #90 */
11869     case 0xa: /* FCMLA, #180 */
11870     case 0xb: /* FCMLA, #270 */
11871         rot = extract32(opcode, 0, 2);
11872         switch (size) {
11873         case 1:
11874             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11875                               gen_helper_gvec_fcmlah);
11876             break;
11877         case 2:
11878             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11879                               gen_helper_gvec_fcmlas);
11880             break;
11881         case 3:
11882             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11883                               gen_helper_gvec_fcmlad);
11884             break;
11885         default:
11886             g_assert_not_reached();
11887         }
11888         return;
11889 
11890     case 0xc: /* FCADD, #90 */
11891     case 0xe: /* FCADD, #270 */
11892         rot = extract32(opcode, 1, 1);
11893         switch (size) {
11894         case 1:
11895             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11896                               gen_helper_gvec_fcaddh);
11897             break;
11898         case 2:
11899             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11900                               gen_helper_gvec_fcadds);
11901             break;
11902         case 3:
11903             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11904                               gen_helper_gvec_fcaddd);
11905             break;
11906         default:
11907             g_assert_not_reached();
11908         }
11909         return;
11910 
11911     case 0xd: /* BFMMLA */
11912         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11913         return;
11914     case 0xf:
11915         switch (size) {
11916         case 1: /* BFDOT */
11917             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11918             break;
11919         case 3: /* BFMLAL{B,T} */
11920             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11921                               gen_helper_gvec_bfmlal);
11922             break;
11923         default:
11924             g_assert_not_reached();
11925         }
11926         return;
11927 
11928     default:
11929         g_assert_not_reached();
11930     }
11931 }
11932 
11933 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11934                                   int size, int rn, int rd)
11935 {
11936     /* Handle 2-reg-misc ops which are widening (so each size element
11937      * in the source becomes a 2*size element in the destination.
11938      * The only instruction like this is FCVTL.
11939      */
11940     int pass;
11941 
11942     if (size == 3) {
11943         /* 32 -> 64 bit fp conversion */
11944         TCGv_i64 tcg_res[2];
11945         int srcelt = is_q ? 2 : 0;
11946 
11947         for (pass = 0; pass < 2; pass++) {
11948             TCGv_i32 tcg_op = tcg_temp_new_i32();
11949             tcg_res[pass] = tcg_temp_new_i64();
11950 
11951             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11952             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
11953         }
11954         for (pass = 0; pass < 2; pass++) {
11955             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11956         }
11957     } else {
11958         /* 16 -> 32 bit fp conversion */
11959         int srcelt = is_q ? 4 : 0;
11960         TCGv_i32 tcg_res[4];
11961         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11962         TCGv_i32 ahp = get_ahp_flag();
11963 
11964         for (pass = 0; pass < 4; pass++) {
11965             tcg_res[pass] = tcg_temp_new_i32();
11966 
11967             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11968             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11969                                            fpst, ahp);
11970         }
11971         for (pass = 0; pass < 4; pass++) {
11972             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11973         }
11974     }
11975 }
11976 
11977 static void handle_rev(DisasContext *s, int opcode, bool u,
11978                        bool is_q, int size, int rn, int rd)
11979 {
11980     int op = (opcode << 1) | u;
11981     int opsz = op + size;
11982     int grp_size = 3 - opsz;
11983     int dsize = is_q ? 128 : 64;
11984     int i;
11985 
11986     if (opsz >= 3) {
11987         unallocated_encoding(s);
11988         return;
11989     }
11990 
11991     if (!fp_access_check(s)) {
11992         return;
11993     }
11994 
11995     if (size == 0) {
11996         /* Special case bytes, use bswap op on each group of elements */
11997         int groups = dsize / (8 << grp_size);
11998 
11999         for (i = 0; i < groups; i++) {
12000             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
12001 
12002             read_vec_element(s, tcg_tmp, rn, i, grp_size);
12003             switch (grp_size) {
12004             case MO_16:
12005                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
12006                 break;
12007             case MO_32:
12008                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
12009                 break;
12010             case MO_64:
12011                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
12012                 break;
12013             default:
12014                 g_assert_not_reached();
12015             }
12016             write_vec_element(s, tcg_tmp, rd, i, grp_size);
12017         }
12018         clear_vec_high(s, is_q, rd);
12019     } else {
12020         int revmask = (1 << grp_size) - 1;
12021         int esize = 8 << size;
12022         int elements = dsize / esize;
12023         TCGv_i64 tcg_rn = tcg_temp_new_i64();
12024         TCGv_i64 tcg_rd[2];
12025 
12026         for (i = 0; i < 2; i++) {
12027             tcg_rd[i] = tcg_temp_new_i64();
12028             tcg_gen_movi_i64(tcg_rd[i], 0);
12029         }
12030 
12031         for (i = 0; i < elements; i++) {
12032             int e_rev = (i & 0xf) ^ revmask;
12033             int w = (e_rev * esize) / 64;
12034             int o = (e_rev * esize) % 64;
12035 
12036             read_vec_element(s, tcg_rn, rn, i, size);
12037             tcg_gen_deposit_i64(tcg_rd[w], tcg_rd[w], tcg_rn, o, esize);
12038         }
12039 
12040         for (i = 0; i < 2; i++) {
12041             write_vec_element(s, tcg_rd[i], rd, i, MO_64);
12042         }
12043         clear_vec_high(s, true, rd);
12044     }
12045 }
12046 
12047 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
12048                                   bool is_q, int size, int rn, int rd)
12049 {
12050     /* Implement the pairwise operations from 2-misc:
12051      * SADDLP, UADDLP, SADALP, UADALP.
12052      * These all add pairs of elements in the input to produce a
12053      * double-width result element in the output (possibly accumulating).
12054      */
12055     bool accum = (opcode == 0x6);
12056     int maxpass = is_q ? 2 : 1;
12057     int pass;
12058     TCGv_i64 tcg_res[2];
12059 
12060     if (size == 2) {
12061         /* 32 + 32 -> 64 op */
12062         MemOp memop = size + (u ? 0 : MO_SIGN);
12063 
12064         for (pass = 0; pass < maxpass; pass++) {
12065             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
12066             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
12067 
12068             tcg_res[pass] = tcg_temp_new_i64();
12069 
12070             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
12071             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
12072             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
12073             if (accum) {
12074                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
12075                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12076             }
12077         }
12078     } else {
12079         for (pass = 0; pass < maxpass; pass++) {
12080             TCGv_i64 tcg_op = tcg_temp_new_i64();
12081             NeonGenOne64OpFn *genfn;
12082             static NeonGenOne64OpFn * const fns[2][2] = {
12083                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
12084                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
12085             };
12086 
12087             genfn = fns[size][u];
12088 
12089             tcg_res[pass] = tcg_temp_new_i64();
12090 
12091             read_vec_element(s, tcg_op, rn, pass, MO_64);
12092             genfn(tcg_res[pass], tcg_op);
12093 
12094             if (accum) {
12095                 read_vec_element(s, tcg_op, rd, pass, MO_64);
12096                 if (size == 0) {
12097                     gen_helper_neon_addl_u16(tcg_res[pass],
12098                                              tcg_res[pass], tcg_op);
12099                 } else {
12100                     gen_helper_neon_addl_u32(tcg_res[pass],
12101                                              tcg_res[pass], tcg_op);
12102                 }
12103             }
12104         }
12105     }
12106     if (!is_q) {
12107         tcg_res[1] = tcg_constant_i64(0);
12108     }
12109     for (pass = 0; pass < 2; pass++) {
12110         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12111     }
12112 }
12113 
12114 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
12115 {
12116     /* Implement SHLL and SHLL2 */
12117     int pass;
12118     int part = is_q ? 2 : 0;
12119     TCGv_i64 tcg_res[2];
12120 
12121     for (pass = 0; pass < 2; pass++) {
12122         static NeonGenWidenFn * const widenfns[3] = {
12123             gen_helper_neon_widen_u8,
12124             gen_helper_neon_widen_u16,
12125             tcg_gen_extu_i32_i64,
12126         };
12127         NeonGenWidenFn *widenfn = widenfns[size];
12128         TCGv_i32 tcg_op = tcg_temp_new_i32();
12129 
12130         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
12131         tcg_res[pass] = tcg_temp_new_i64();
12132         widenfn(tcg_res[pass], tcg_op);
12133         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
12134     }
12135 
12136     for (pass = 0; pass < 2; pass++) {
12137         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12138     }
12139 }
12140 
12141 /* AdvSIMD two reg misc
12142  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
12143  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12144  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12145  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12146  */
12147 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
12148 {
12149     int size = extract32(insn, 22, 2);
12150     int opcode = extract32(insn, 12, 5);
12151     bool u = extract32(insn, 29, 1);
12152     bool is_q = extract32(insn, 30, 1);
12153     int rn = extract32(insn, 5, 5);
12154     int rd = extract32(insn, 0, 5);
12155     bool need_fpstatus = false;
12156     int rmode = -1;
12157     TCGv_i32 tcg_rmode;
12158     TCGv_ptr tcg_fpstatus;
12159 
12160     switch (opcode) {
12161     case 0x0: /* REV64, REV32 */
12162     case 0x1: /* REV16 */
12163         handle_rev(s, opcode, u, is_q, size, rn, rd);
12164         return;
12165     case 0x5: /* CNT, NOT, RBIT */
12166         if (u && size == 0) {
12167             /* NOT */
12168             break;
12169         } else if (u && size == 1) {
12170             /* RBIT */
12171             break;
12172         } else if (!u && size == 0) {
12173             /* CNT */
12174             break;
12175         }
12176         unallocated_encoding(s);
12177         return;
12178     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12179     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12180         if (size == 3) {
12181             unallocated_encoding(s);
12182             return;
12183         }
12184         if (!fp_access_check(s)) {
12185             return;
12186         }
12187 
12188         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
12189         return;
12190     case 0x4: /* CLS, CLZ */
12191         if (size == 3) {
12192             unallocated_encoding(s);
12193             return;
12194         }
12195         break;
12196     case 0x2: /* SADDLP, UADDLP */
12197     case 0x6: /* SADALP, UADALP */
12198         if (size == 3) {
12199             unallocated_encoding(s);
12200             return;
12201         }
12202         if (!fp_access_check(s)) {
12203             return;
12204         }
12205         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
12206         return;
12207     case 0x13: /* SHLL, SHLL2 */
12208         if (u == 0 || size == 3) {
12209             unallocated_encoding(s);
12210             return;
12211         }
12212         if (!fp_access_check(s)) {
12213             return;
12214         }
12215         handle_shll(s, is_q, size, rn, rd);
12216         return;
12217     case 0xa: /* CMLT */
12218         if (u == 1) {
12219             unallocated_encoding(s);
12220             return;
12221         }
12222         /* fall through */
12223     case 0x8: /* CMGT, CMGE */
12224     case 0x9: /* CMEQ, CMLE */
12225     case 0xb: /* ABS, NEG */
12226         if (size == 3 && !is_q) {
12227             unallocated_encoding(s);
12228             return;
12229         }
12230         break;
12231     case 0x3: /* SUQADD, USQADD */
12232         if (size == 3 && !is_q) {
12233             unallocated_encoding(s);
12234             return;
12235         }
12236         if (!fp_access_check(s)) {
12237             return;
12238         }
12239         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12240         return;
12241     case 0x7: /* SQABS, SQNEG */
12242         if (size == 3 && !is_q) {
12243             unallocated_encoding(s);
12244             return;
12245         }
12246         break;
12247     case 0xc ... 0xf:
12248     case 0x16 ... 0x1f:
12249     {
12250         /* Floating point: U, size[1] and opcode indicate operation;
12251          * size[0] indicates single or double precision.
12252          */
12253         int is_double = extract32(size, 0, 1);
12254         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12255         size = is_double ? 3 : 2;
12256         switch (opcode) {
12257         case 0x2f: /* FABS */
12258         case 0x6f: /* FNEG */
12259             if (size == 3 && !is_q) {
12260                 unallocated_encoding(s);
12261                 return;
12262             }
12263             break;
12264         case 0x1d: /* SCVTF */
12265         case 0x5d: /* UCVTF */
12266         {
12267             bool is_signed = (opcode == 0x1d) ? true : false;
12268             int elements = is_double ? 2 : is_q ? 4 : 2;
12269             if (is_double && !is_q) {
12270                 unallocated_encoding(s);
12271                 return;
12272             }
12273             if (!fp_access_check(s)) {
12274                 return;
12275             }
12276             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12277             return;
12278         }
12279         case 0x2c: /* FCMGT (zero) */
12280         case 0x2d: /* FCMEQ (zero) */
12281         case 0x2e: /* FCMLT (zero) */
12282         case 0x6c: /* FCMGE (zero) */
12283         case 0x6d: /* FCMLE (zero) */
12284             if (size == 3 && !is_q) {
12285                 unallocated_encoding(s);
12286                 return;
12287             }
12288             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12289             return;
12290         case 0x7f: /* FSQRT */
12291             if (size == 3 && !is_q) {
12292                 unallocated_encoding(s);
12293                 return;
12294             }
12295             break;
12296         case 0x1a: /* FCVTNS */
12297         case 0x1b: /* FCVTMS */
12298         case 0x3a: /* FCVTPS */
12299         case 0x3b: /* FCVTZS */
12300         case 0x5a: /* FCVTNU */
12301         case 0x5b: /* FCVTMU */
12302         case 0x7a: /* FCVTPU */
12303         case 0x7b: /* FCVTZU */
12304             need_fpstatus = true;
12305             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12306             if (size == 3 && !is_q) {
12307                 unallocated_encoding(s);
12308                 return;
12309             }
12310             break;
12311         case 0x5c: /* FCVTAU */
12312         case 0x1c: /* FCVTAS */
12313             need_fpstatus = true;
12314             rmode = FPROUNDING_TIEAWAY;
12315             if (size == 3 && !is_q) {
12316                 unallocated_encoding(s);
12317                 return;
12318             }
12319             break;
12320         case 0x3c: /* URECPE */
12321             if (size == 3) {
12322                 unallocated_encoding(s);
12323                 return;
12324             }
12325             /* fall through */
12326         case 0x3d: /* FRECPE */
12327         case 0x7d: /* FRSQRTE */
12328             if (size == 3 && !is_q) {
12329                 unallocated_encoding(s);
12330                 return;
12331             }
12332             if (!fp_access_check(s)) {
12333                 return;
12334             }
12335             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12336             return;
12337         case 0x56: /* FCVTXN, FCVTXN2 */
12338             if (size == 2) {
12339                 unallocated_encoding(s);
12340                 return;
12341             }
12342             /* fall through */
12343         case 0x16: /* FCVTN, FCVTN2 */
12344             /* handle_2misc_narrow does a 2*size -> size operation, but these
12345              * instructions encode the source size rather than dest size.
12346              */
12347             if (!fp_access_check(s)) {
12348                 return;
12349             }
12350             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12351             return;
12352         case 0x36: /* BFCVTN, BFCVTN2 */
12353             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
12354                 unallocated_encoding(s);
12355                 return;
12356             }
12357             if (!fp_access_check(s)) {
12358                 return;
12359             }
12360             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12361             return;
12362         case 0x17: /* FCVTL, FCVTL2 */
12363             if (!fp_access_check(s)) {
12364                 return;
12365             }
12366             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12367             return;
12368         case 0x18: /* FRINTN */
12369         case 0x19: /* FRINTM */
12370         case 0x38: /* FRINTP */
12371         case 0x39: /* FRINTZ */
12372             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12373             /* fall through */
12374         case 0x59: /* FRINTX */
12375         case 0x79: /* FRINTI */
12376             need_fpstatus = true;
12377             if (size == 3 && !is_q) {
12378                 unallocated_encoding(s);
12379                 return;
12380             }
12381             break;
12382         case 0x58: /* FRINTA */
12383             rmode = FPROUNDING_TIEAWAY;
12384             need_fpstatus = true;
12385             if (size == 3 && !is_q) {
12386                 unallocated_encoding(s);
12387                 return;
12388             }
12389             break;
12390         case 0x7c: /* URSQRTE */
12391             if (size == 3) {
12392                 unallocated_encoding(s);
12393                 return;
12394             }
12395             break;
12396         case 0x1e: /* FRINT32Z */
12397         case 0x1f: /* FRINT64Z */
12398             rmode = FPROUNDING_ZERO;
12399             /* fall through */
12400         case 0x5e: /* FRINT32X */
12401         case 0x5f: /* FRINT64X */
12402             need_fpstatus = true;
12403             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12404                 unallocated_encoding(s);
12405                 return;
12406             }
12407             break;
12408         default:
12409             unallocated_encoding(s);
12410             return;
12411         }
12412         break;
12413     }
12414     default:
12415         unallocated_encoding(s);
12416         return;
12417     }
12418 
12419     if (!fp_access_check(s)) {
12420         return;
12421     }
12422 
12423     if (need_fpstatus || rmode >= 0) {
12424         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12425     } else {
12426         tcg_fpstatus = NULL;
12427     }
12428     if (rmode >= 0) {
12429         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12430     } else {
12431         tcg_rmode = NULL;
12432     }
12433 
12434     switch (opcode) {
12435     case 0x5:
12436         if (u && size == 0) { /* NOT */
12437             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12438             return;
12439         }
12440         break;
12441     case 0x8: /* CMGT, CMGE */
12442         if (u) {
12443             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12444         } else {
12445             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12446         }
12447         return;
12448     case 0x9: /* CMEQ, CMLE */
12449         if (u) {
12450             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12451         } else {
12452             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12453         }
12454         return;
12455     case 0xa: /* CMLT */
12456         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12457         return;
12458     case 0xb:
12459         if (u) { /* ABS, NEG */
12460             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12461         } else {
12462             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12463         }
12464         return;
12465     }
12466 
12467     if (size == 3) {
12468         /* All 64-bit element operations can be shared with scalar 2misc */
12469         int pass;
12470 
12471         /* Coverity claims (size == 3 && !is_q) has been eliminated
12472          * from all paths leading to here.
12473          */
12474         tcg_debug_assert(is_q);
12475         for (pass = 0; pass < 2; pass++) {
12476             TCGv_i64 tcg_op = tcg_temp_new_i64();
12477             TCGv_i64 tcg_res = tcg_temp_new_i64();
12478 
12479             read_vec_element(s, tcg_op, rn, pass, MO_64);
12480 
12481             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12482                             tcg_rmode, tcg_fpstatus);
12483 
12484             write_vec_element(s, tcg_res, rd, pass, MO_64);
12485         }
12486     } else {
12487         int pass;
12488 
12489         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12490             TCGv_i32 tcg_op = tcg_temp_new_i32();
12491             TCGv_i32 tcg_res = tcg_temp_new_i32();
12492 
12493             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12494 
12495             if (size == 2) {
12496                 /* Special cases for 32 bit elements */
12497                 switch (opcode) {
12498                 case 0x4: /* CLS */
12499                     if (u) {
12500                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12501                     } else {
12502                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
12503                     }
12504                     break;
12505                 case 0x7: /* SQABS, SQNEG */
12506                     if (u) {
12507                         gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12508                     } else {
12509                         gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12510                     }
12511                     break;
12512                 case 0x2f: /* FABS */
12513                     gen_helper_vfp_abss(tcg_res, tcg_op);
12514                     break;
12515                 case 0x6f: /* FNEG */
12516                     gen_helper_vfp_negs(tcg_res, tcg_op);
12517                     break;
12518                 case 0x7f: /* FSQRT */
12519                     gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12520                     break;
12521                 case 0x1a: /* FCVTNS */
12522                 case 0x1b: /* FCVTMS */
12523                 case 0x1c: /* FCVTAS */
12524                 case 0x3a: /* FCVTPS */
12525                 case 0x3b: /* FCVTZS */
12526                     gen_helper_vfp_tosls(tcg_res, tcg_op,
12527                                          tcg_constant_i32(0), tcg_fpstatus);
12528                     break;
12529                 case 0x5a: /* FCVTNU */
12530                 case 0x5b: /* FCVTMU */
12531                 case 0x5c: /* FCVTAU */
12532                 case 0x7a: /* FCVTPU */
12533                 case 0x7b: /* FCVTZU */
12534                     gen_helper_vfp_touls(tcg_res, tcg_op,
12535                                          tcg_constant_i32(0), tcg_fpstatus);
12536                     break;
12537                 case 0x18: /* FRINTN */
12538                 case 0x19: /* FRINTM */
12539                 case 0x38: /* FRINTP */
12540                 case 0x39: /* FRINTZ */
12541                 case 0x58: /* FRINTA */
12542                 case 0x79: /* FRINTI */
12543                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12544                     break;
12545                 case 0x59: /* FRINTX */
12546                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12547                     break;
12548                 case 0x7c: /* URSQRTE */
12549                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
12550                     break;
12551                 case 0x1e: /* FRINT32Z */
12552                 case 0x5e: /* FRINT32X */
12553                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12554                     break;
12555                 case 0x1f: /* FRINT64Z */
12556                 case 0x5f: /* FRINT64X */
12557                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12558                     break;
12559                 default:
12560                     g_assert_not_reached();
12561                 }
12562             } else {
12563                 /* Use helpers for 8 and 16 bit elements */
12564                 switch (opcode) {
12565                 case 0x5: /* CNT, RBIT */
12566                     /* For these two insns size is part of the opcode specifier
12567                      * (handled earlier); they always operate on byte elements.
12568                      */
12569                     if (u) {
12570                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12571                     } else {
12572                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12573                     }
12574                     break;
12575                 case 0x7: /* SQABS, SQNEG */
12576                 {
12577                     NeonGenOneOpEnvFn *genfn;
12578                     static NeonGenOneOpEnvFn * const fns[2][2] = {
12579                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12580                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12581                     };
12582                     genfn = fns[size][u];
12583                     genfn(tcg_res, cpu_env, tcg_op);
12584                     break;
12585                 }
12586                 case 0x4: /* CLS, CLZ */
12587                     if (u) {
12588                         if (size == 0) {
12589                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
12590                         } else {
12591                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
12592                         }
12593                     } else {
12594                         if (size == 0) {
12595                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
12596                         } else {
12597                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
12598                         }
12599                     }
12600                     break;
12601                 default:
12602                     g_assert_not_reached();
12603                 }
12604             }
12605 
12606             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12607         }
12608     }
12609     clear_vec_high(s, is_q, rd);
12610 
12611     if (tcg_rmode) {
12612         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12613     }
12614 }
12615 
12616 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12617  *
12618  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
12619  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12620  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12621  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12622  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12623  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12624  *
12625  * This actually covers two groups where scalar access is governed by
12626  * bit 28. A bunch of the instructions (float to integral) only exist
12627  * in the vector form and are un-allocated for the scalar decode. Also
12628  * in the scalar decode Q is always 1.
12629  */
12630 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12631 {
12632     int fpop, opcode, a, u;
12633     int rn, rd;
12634     bool is_q;
12635     bool is_scalar;
12636     bool only_in_vector = false;
12637 
12638     int pass;
12639     TCGv_i32 tcg_rmode = NULL;
12640     TCGv_ptr tcg_fpstatus = NULL;
12641     bool need_fpst = true;
12642     int rmode = -1;
12643 
12644     if (!dc_isar_feature(aa64_fp16, s)) {
12645         unallocated_encoding(s);
12646         return;
12647     }
12648 
12649     rd = extract32(insn, 0, 5);
12650     rn = extract32(insn, 5, 5);
12651 
12652     a = extract32(insn, 23, 1);
12653     u = extract32(insn, 29, 1);
12654     is_scalar = extract32(insn, 28, 1);
12655     is_q = extract32(insn, 30, 1);
12656 
12657     opcode = extract32(insn, 12, 5);
12658     fpop = deposit32(opcode, 5, 1, a);
12659     fpop = deposit32(fpop, 6, 1, u);
12660 
12661     switch (fpop) {
12662     case 0x1d: /* SCVTF */
12663     case 0x5d: /* UCVTF */
12664     {
12665         int elements;
12666 
12667         if (is_scalar) {
12668             elements = 1;
12669         } else {
12670             elements = (is_q ? 8 : 4);
12671         }
12672 
12673         if (!fp_access_check(s)) {
12674             return;
12675         }
12676         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12677         return;
12678     }
12679     break;
12680     case 0x2c: /* FCMGT (zero) */
12681     case 0x2d: /* FCMEQ (zero) */
12682     case 0x2e: /* FCMLT (zero) */
12683     case 0x6c: /* FCMGE (zero) */
12684     case 0x6d: /* FCMLE (zero) */
12685         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12686         return;
12687     case 0x3d: /* FRECPE */
12688     case 0x3f: /* FRECPX */
12689         break;
12690     case 0x18: /* FRINTN */
12691         only_in_vector = true;
12692         rmode = FPROUNDING_TIEEVEN;
12693         break;
12694     case 0x19: /* FRINTM */
12695         only_in_vector = true;
12696         rmode = FPROUNDING_NEGINF;
12697         break;
12698     case 0x38: /* FRINTP */
12699         only_in_vector = true;
12700         rmode = FPROUNDING_POSINF;
12701         break;
12702     case 0x39: /* FRINTZ */
12703         only_in_vector = true;
12704         rmode = FPROUNDING_ZERO;
12705         break;
12706     case 0x58: /* FRINTA */
12707         only_in_vector = true;
12708         rmode = FPROUNDING_TIEAWAY;
12709         break;
12710     case 0x59: /* FRINTX */
12711     case 0x79: /* FRINTI */
12712         only_in_vector = true;
12713         /* current rounding mode */
12714         break;
12715     case 0x1a: /* FCVTNS */
12716         rmode = FPROUNDING_TIEEVEN;
12717         break;
12718     case 0x1b: /* FCVTMS */
12719         rmode = FPROUNDING_NEGINF;
12720         break;
12721     case 0x1c: /* FCVTAS */
12722         rmode = FPROUNDING_TIEAWAY;
12723         break;
12724     case 0x3a: /* FCVTPS */
12725         rmode = FPROUNDING_POSINF;
12726         break;
12727     case 0x3b: /* FCVTZS */
12728         rmode = FPROUNDING_ZERO;
12729         break;
12730     case 0x5a: /* FCVTNU */
12731         rmode = FPROUNDING_TIEEVEN;
12732         break;
12733     case 0x5b: /* FCVTMU */
12734         rmode = FPROUNDING_NEGINF;
12735         break;
12736     case 0x5c: /* FCVTAU */
12737         rmode = FPROUNDING_TIEAWAY;
12738         break;
12739     case 0x7a: /* FCVTPU */
12740         rmode = FPROUNDING_POSINF;
12741         break;
12742     case 0x7b: /* FCVTZU */
12743         rmode = FPROUNDING_ZERO;
12744         break;
12745     case 0x2f: /* FABS */
12746     case 0x6f: /* FNEG */
12747         need_fpst = false;
12748         break;
12749     case 0x7d: /* FRSQRTE */
12750     case 0x7f: /* FSQRT (vector) */
12751         break;
12752     default:
12753         unallocated_encoding(s);
12754         return;
12755     }
12756 
12757 
12758     /* Check additional constraints for the scalar encoding */
12759     if (is_scalar) {
12760         if (!is_q) {
12761             unallocated_encoding(s);
12762             return;
12763         }
12764         /* FRINTxx is only in the vector form */
12765         if (only_in_vector) {
12766             unallocated_encoding(s);
12767             return;
12768         }
12769     }
12770 
12771     if (!fp_access_check(s)) {
12772         return;
12773     }
12774 
12775     if (rmode >= 0 || need_fpst) {
12776         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
12777     }
12778 
12779     if (rmode >= 0) {
12780         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12781     }
12782 
12783     if (is_scalar) {
12784         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12785         TCGv_i32 tcg_res = tcg_temp_new_i32();
12786 
12787         switch (fpop) {
12788         case 0x1a: /* FCVTNS */
12789         case 0x1b: /* FCVTMS */
12790         case 0x1c: /* FCVTAS */
12791         case 0x3a: /* FCVTPS */
12792         case 0x3b: /* FCVTZS */
12793             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12794             break;
12795         case 0x3d: /* FRECPE */
12796             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12797             break;
12798         case 0x3f: /* FRECPX */
12799             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12800             break;
12801         case 0x5a: /* FCVTNU */
12802         case 0x5b: /* FCVTMU */
12803         case 0x5c: /* FCVTAU */
12804         case 0x7a: /* FCVTPU */
12805         case 0x7b: /* FCVTZU */
12806             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12807             break;
12808         case 0x6f: /* FNEG */
12809             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12810             break;
12811         case 0x7d: /* FRSQRTE */
12812             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12813             break;
12814         default:
12815             g_assert_not_reached();
12816         }
12817 
12818         /* limit any sign extension going on */
12819         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12820         write_fp_sreg(s, rd, tcg_res);
12821     } else {
12822         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12823             TCGv_i32 tcg_op = tcg_temp_new_i32();
12824             TCGv_i32 tcg_res = tcg_temp_new_i32();
12825 
12826             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12827 
12828             switch (fpop) {
12829             case 0x1a: /* FCVTNS */
12830             case 0x1b: /* FCVTMS */
12831             case 0x1c: /* FCVTAS */
12832             case 0x3a: /* FCVTPS */
12833             case 0x3b: /* FCVTZS */
12834                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12835                 break;
12836             case 0x3d: /* FRECPE */
12837                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12838                 break;
12839             case 0x5a: /* FCVTNU */
12840             case 0x5b: /* FCVTMU */
12841             case 0x5c: /* FCVTAU */
12842             case 0x7a: /* FCVTPU */
12843             case 0x7b: /* FCVTZU */
12844                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12845                 break;
12846             case 0x18: /* FRINTN */
12847             case 0x19: /* FRINTM */
12848             case 0x38: /* FRINTP */
12849             case 0x39: /* FRINTZ */
12850             case 0x58: /* FRINTA */
12851             case 0x79: /* FRINTI */
12852                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12853                 break;
12854             case 0x59: /* FRINTX */
12855                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12856                 break;
12857             case 0x2f: /* FABS */
12858                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12859                 break;
12860             case 0x6f: /* FNEG */
12861                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12862                 break;
12863             case 0x7d: /* FRSQRTE */
12864                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12865                 break;
12866             case 0x7f: /* FSQRT */
12867                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12868                 break;
12869             default:
12870                 g_assert_not_reached();
12871             }
12872 
12873             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12874         }
12875 
12876         clear_vec_high(s, is_q, rd);
12877     }
12878 
12879     if (tcg_rmode) {
12880         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12881     }
12882 }
12883 
12884 /* AdvSIMD scalar x indexed element
12885  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12886  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12887  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12888  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12889  * AdvSIMD vector x indexed element
12890  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12891  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12892  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12893  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12894  */
12895 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12896 {
12897     /* This encoding has two kinds of instruction:
12898      *  normal, where we perform elt x idxelt => elt for each
12899      *     element in the vector
12900      *  long, where we perform elt x idxelt and generate a result of
12901      *     double the width of the input element
12902      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12903      */
12904     bool is_scalar = extract32(insn, 28, 1);
12905     bool is_q = extract32(insn, 30, 1);
12906     bool u = extract32(insn, 29, 1);
12907     int size = extract32(insn, 22, 2);
12908     int l = extract32(insn, 21, 1);
12909     int m = extract32(insn, 20, 1);
12910     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12911     int rm = extract32(insn, 16, 4);
12912     int opcode = extract32(insn, 12, 4);
12913     int h = extract32(insn, 11, 1);
12914     int rn = extract32(insn, 5, 5);
12915     int rd = extract32(insn, 0, 5);
12916     bool is_long = false;
12917     int is_fp = 0;
12918     bool is_fp16 = false;
12919     int index;
12920     TCGv_ptr fpst;
12921 
12922     switch (16 * u + opcode) {
12923     case 0x08: /* MUL */
12924     case 0x10: /* MLA */
12925     case 0x14: /* MLS */
12926         if (is_scalar) {
12927             unallocated_encoding(s);
12928             return;
12929         }
12930         break;
12931     case 0x02: /* SMLAL, SMLAL2 */
12932     case 0x12: /* UMLAL, UMLAL2 */
12933     case 0x06: /* SMLSL, SMLSL2 */
12934     case 0x16: /* UMLSL, UMLSL2 */
12935     case 0x0a: /* SMULL, SMULL2 */
12936     case 0x1a: /* UMULL, UMULL2 */
12937         if (is_scalar) {
12938             unallocated_encoding(s);
12939             return;
12940         }
12941         is_long = true;
12942         break;
12943     case 0x03: /* SQDMLAL, SQDMLAL2 */
12944     case 0x07: /* SQDMLSL, SQDMLSL2 */
12945     case 0x0b: /* SQDMULL, SQDMULL2 */
12946         is_long = true;
12947         break;
12948     case 0x0c: /* SQDMULH */
12949     case 0x0d: /* SQRDMULH */
12950         break;
12951     case 0x01: /* FMLA */
12952     case 0x05: /* FMLS */
12953     case 0x09: /* FMUL */
12954     case 0x19: /* FMULX */
12955         is_fp = 1;
12956         break;
12957     case 0x1d: /* SQRDMLAH */
12958     case 0x1f: /* SQRDMLSH */
12959         if (!dc_isar_feature(aa64_rdm, s)) {
12960             unallocated_encoding(s);
12961             return;
12962         }
12963         break;
12964     case 0x0e: /* SDOT */
12965     case 0x1e: /* UDOT */
12966         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12967             unallocated_encoding(s);
12968             return;
12969         }
12970         break;
12971     case 0x0f:
12972         switch (size) {
12973         case 0: /* SUDOT */
12974         case 2: /* USDOT */
12975             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12976                 unallocated_encoding(s);
12977                 return;
12978             }
12979             size = MO_32;
12980             break;
12981         case 1: /* BFDOT */
12982             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12983                 unallocated_encoding(s);
12984                 return;
12985             }
12986             size = MO_32;
12987             break;
12988         case 3: /* BFMLAL{B,T} */
12989             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12990                 unallocated_encoding(s);
12991                 return;
12992             }
12993             /* can't set is_fp without other incorrect size checks */
12994             size = MO_16;
12995             break;
12996         default:
12997             unallocated_encoding(s);
12998             return;
12999         }
13000         break;
13001     case 0x11: /* FCMLA #0 */
13002     case 0x13: /* FCMLA #90 */
13003     case 0x15: /* FCMLA #180 */
13004     case 0x17: /* FCMLA #270 */
13005         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
13006             unallocated_encoding(s);
13007             return;
13008         }
13009         is_fp = 2;
13010         break;
13011     case 0x00: /* FMLAL */
13012     case 0x04: /* FMLSL */
13013     case 0x18: /* FMLAL2 */
13014     case 0x1c: /* FMLSL2 */
13015         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
13016             unallocated_encoding(s);
13017             return;
13018         }
13019         size = MO_16;
13020         /* is_fp, but we pass cpu_env not fp_status.  */
13021         break;
13022     default:
13023         unallocated_encoding(s);
13024         return;
13025     }
13026 
13027     switch (is_fp) {
13028     case 1: /* normal fp */
13029         /* convert insn encoded size to MemOp size */
13030         switch (size) {
13031         case 0: /* half-precision */
13032             size = MO_16;
13033             is_fp16 = true;
13034             break;
13035         case MO_32: /* single precision */
13036         case MO_64: /* double precision */
13037             break;
13038         default:
13039             unallocated_encoding(s);
13040             return;
13041         }
13042         break;
13043 
13044     case 2: /* complex fp */
13045         /* Each indexable element is a complex pair.  */
13046         size += 1;
13047         switch (size) {
13048         case MO_32:
13049             if (h && !is_q) {
13050                 unallocated_encoding(s);
13051                 return;
13052             }
13053             is_fp16 = true;
13054             break;
13055         case MO_64:
13056             break;
13057         default:
13058             unallocated_encoding(s);
13059             return;
13060         }
13061         break;
13062 
13063     default: /* integer */
13064         switch (size) {
13065         case MO_8:
13066         case MO_64:
13067             unallocated_encoding(s);
13068             return;
13069         }
13070         break;
13071     }
13072     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
13073         unallocated_encoding(s);
13074         return;
13075     }
13076 
13077     /* Given MemOp size, adjust register and indexing.  */
13078     switch (size) {
13079     case MO_16:
13080         index = h << 2 | l << 1 | m;
13081         break;
13082     case MO_32:
13083         index = h << 1 | l;
13084         rm |= m << 4;
13085         break;
13086     case MO_64:
13087         if (l || !is_q) {
13088             unallocated_encoding(s);
13089             return;
13090         }
13091         index = h;
13092         rm |= m << 4;
13093         break;
13094     default:
13095         g_assert_not_reached();
13096     }
13097 
13098     if (!fp_access_check(s)) {
13099         return;
13100     }
13101 
13102     if (is_fp) {
13103         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
13104     } else {
13105         fpst = NULL;
13106     }
13107 
13108     switch (16 * u + opcode) {
13109     case 0x0e: /* SDOT */
13110     case 0x1e: /* UDOT */
13111         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13112                          u ? gen_helper_gvec_udot_idx_b
13113                          : gen_helper_gvec_sdot_idx_b);
13114         return;
13115     case 0x0f:
13116         switch (extract32(insn, 22, 2)) {
13117         case 0: /* SUDOT */
13118             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13119                              gen_helper_gvec_sudot_idx_b);
13120             return;
13121         case 1: /* BFDOT */
13122             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13123                              gen_helper_gvec_bfdot_idx);
13124             return;
13125         case 2: /* USDOT */
13126             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13127                              gen_helper_gvec_usdot_idx_b);
13128             return;
13129         case 3: /* BFMLAL{B,T} */
13130             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
13131                               gen_helper_gvec_bfmlal_idx);
13132             return;
13133         }
13134         g_assert_not_reached();
13135     case 0x11: /* FCMLA #0 */
13136     case 0x13: /* FCMLA #90 */
13137     case 0x15: /* FCMLA #180 */
13138     case 0x17: /* FCMLA #270 */
13139         {
13140             int rot = extract32(insn, 13, 2);
13141             int data = (index << 2) | rot;
13142             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
13143                                vec_full_reg_offset(s, rn),
13144                                vec_full_reg_offset(s, rm),
13145                                vec_full_reg_offset(s, rd), fpst,
13146                                is_q ? 16 : 8, vec_full_reg_size(s), data,
13147                                size == MO_64
13148                                ? gen_helper_gvec_fcmlas_idx
13149                                : gen_helper_gvec_fcmlah_idx);
13150         }
13151         return;
13152 
13153     case 0x00: /* FMLAL */
13154     case 0x04: /* FMLSL */
13155     case 0x18: /* FMLAL2 */
13156     case 0x1c: /* FMLSL2 */
13157         {
13158             int is_s = extract32(opcode, 2, 1);
13159             int is_2 = u;
13160             int data = (index << 2) | (is_2 << 1) | is_s;
13161             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13162                                vec_full_reg_offset(s, rn),
13163                                vec_full_reg_offset(s, rm), cpu_env,
13164                                is_q ? 16 : 8, vec_full_reg_size(s),
13165                                data, gen_helper_gvec_fmlal_idx_a64);
13166         }
13167         return;
13168 
13169     case 0x08: /* MUL */
13170         if (!is_long && !is_scalar) {
13171             static gen_helper_gvec_3 * const fns[3] = {
13172                 gen_helper_gvec_mul_idx_h,
13173                 gen_helper_gvec_mul_idx_s,
13174                 gen_helper_gvec_mul_idx_d,
13175             };
13176             tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
13177                                vec_full_reg_offset(s, rn),
13178                                vec_full_reg_offset(s, rm),
13179                                is_q ? 16 : 8, vec_full_reg_size(s),
13180                                index, fns[size - 1]);
13181             return;
13182         }
13183         break;
13184 
13185     case 0x10: /* MLA */
13186         if (!is_long && !is_scalar) {
13187             static gen_helper_gvec_4 * const fns[3] = {
13188                 gen_helper_gvec_mla_idx_h,
13189                 gen_helper_gvec_mla_idx_s,
13190                 gen_helper_gvec_mla_idx_d,
13191             };
13192             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13193                                vec_full_reg_offset(s, rn),
13194                                vec_full_reg_offset(s, rm),
13195                                vec_full_reg_offset(s, rd),
13196                                is_q ? 16 : 8, vec_full_reg_size(s),
13197                                index, fns[size - 1]);
13198             return;
13199         }
13200         break;
13201 
13202     case 0x14: /* MLS */
13203         if (!is_long && !is_scalar) {
13204             static gen_helper_gvec_4 * const fns[3] = {
13205                 gen_helper_gvec_mls_idx_h,
13206                 gen_helper_gvec_mls_idx_s,
13207                 gen_helper_gvec_mls_idx_d,
13208             };
13209             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13210                                vec_full_reg_offset(s, rn),
13211                                vec_full_reg_offset(s, rm),
13212                                vec_full_reg_offset(s, rd),
13213                                is_q ? 16 : 8, vec_full_reg_size(s),
13214                                index, fns[size - 1]);
13215             return;
13216         }
13217         break;
13218     }
13219 
13220     if (size == 3) {
13221         TCGv_i64 tcg_idx = tcg_temp_new_i64();
13222         int pass;
13223 
13224         assert(is_fp && is_q && !is_long);
13225 
13226         read_vec_element(s, tcg_idx, rm, index, MO_64);
13227 
13228         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13229             TCGv_i64 tcg_op = tcg_temp_new_i64();
13230             TCGv_i64 tcg_res = tcg_temp_new_i64();
13231 
13232             read_vec_element(s, tcg_op, rn, pass, MO_64);
13233 
13234             switch (16 * u + opcode) {
13235             case 0x05: /* FMLS */
13236                 /* As usual for ARM, separate negation for fused multiply-add */
13237                 gen_helper_vfp_negd(tcg_op, tcg_op);
13238                 /* fall through */
13239             case 0x01: /* FMLA */
13240                 read_vec_element(s, tcg_res, rd, pass, MO_64);
13241                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13242                 break;
13243             case 0x09: /* FMUL */
13244                 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13245                 break;
13246             case 0x19: /* FMULX */
13247                 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13248                 break;
13249             default:
13250                 g_assert_not_reached();
13251             }
13252 
13253             write_vec_element(s, tcg_res, rd, pass, MO_64);
13254         }
13255 
13256         clear_vec_high(s, !is_scalar, rd);
13257     } else if (!is_long) {
13258         /* 32 bit floating point, or 16 or 32 bit integer.
13259          * For the 16 bit scalar case we use the usual Neon helpers and
13260          * rely on the fact that 0 op 0 == 0 with no side effects.
13261          */
13262         TCGv_i32 tcg_idx = tcg_temp_new_i32();
13263         int pass, maxpasses;
13264 
13265         if (is_scalar) {
13266             maxpasses = 1;
13267         } else {
13268             maxpasses = is_q ? 4 : 2;
13269         }
13270 
13271         read_vec_element_i32(s, tcg_idx, rm, index, size);
13272 
13273         if (size == 1 && !is_scalar) {
13274             /* The simplest way to handle the 16x16 indexed ops is to duplicate
13275              * the index into both halves of the 32 bit tcg_idx and then use
13276              * the usual Neon helpers.
13277              */
13278             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13279         }
13280 
13281         for (pass = 0; pass < maxpasses; pass++) {
13282             TCGv_i32 tcg_op = tcg_temp_new_i32();
13283             TCGv_i32 tcg_res = tcg_temp_new_i32();
13284 
13285             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13286 
13287             switch (16 * u + opcode) {
13288             case 0x08: /* MUL */
13289             case 0x10: /* MLA */
13290             case 0x14: /* MLS */
13291             {
13292                 static NeonGenTwoOpFn * const fns[2][2] = {
13293                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13294                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
13295                 };
13296                 NeonGenTwoOpFn *genfn;
13297                 bool is_sub = opcode == 0x4;
13298 
13299                 if (size == 1) {
13300                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13301                 } else {
13302                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13303                 }
13304                 if (opcode == 0x8) {
13305                     break;
13306                 }
13307                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13308                 genfn = fns[size - 1][is_sub];
13309                 genfn(tcg_res, tcg_op, tcg_res);
13310                 break;
13311             }
13312             case 0x05: /* FMLS */
13313             case 0x01: /* FMLA */
13314                 read_vec_element_i32(s, tcg_res, rd, pass,
13315                                      is_scalar ? size : MO_32);
13316                 switch (size) {
13317                 case 1:
13318                     if (opcode == 0x5) {
13319                         /* As usual for ARM, separate negation for fused
13320                          * multiply-add */
13321                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13322                     }
13323                     if (is_scalar) {
13324                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13325                                                    tcg_res, fpst);
13326                     } else {
13327                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13328                                                     tcg_res, fpst);
13329                     }
13330                     break;
13331                 case 2:
13332                     if (opcode == 0x5) {
13333                         /* As usual for ARM, separate negation for
13334                          * fused multiply-add */
13335                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13336                     }
13337                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13338                                            tcg_res, fpst);
13339                     break;
13340                 default:
13341                     g_assert_not_reached();
13342                 }
13343                 break;
13344             case 0x09: /* FMUL */
13345                 switch (size) {
13346                 case 1:
13347                     if (is_scalar) {
13348                         gen_helper_advsimd_mulh(tcg_res, tcg_op,
13349                                                 tcg_idx, fpst);
13350                     } else {
13351                         gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13352                                                  tcg_idx, fpst);
13353                     }
13354                     break;
13355                 case 2:
13356                     gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13357                     break;
13358                 default:
13359                     g_assert_not_reached();
13360                 }
13361                 break;
13362             case 0x19: /* FMULX */
13363                 switch (size) {
13364                 case 1:
13365                     if (is_scalar) {
13366                         gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13367                                                  tcg_idx, fpst);
13368                     } else {
13369                         gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13370                                                   tcg_idx, fpst);
13371                     }
13372                     break;
13373                 case 2:
13374                     gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13375                     break;
13376                 default:
13377                     g_assert_not_reached();
13378                 }
13379                 break;
13380             case 0x0c: /* SQDMULH */
13381                 if (size == 1) {
13382                     gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13383                                                tcg_op, tcg_idx);
13384                 } else {
13385                     gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13386                                                tcg_op, tcg_idx);
13387                 }
13388                 break;
13389             case 0x0d: /* SQRDMULH */
13390                 if (size == 1) {
13391                     gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13392                                                 tcg_op, tcg_idx);
13393                 } else {
13394                     gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13395                                                 tcg_op, tcg_idx);
13396                 }
13397                 break;
13398             case 0x1d: /* SQRDMLAH */
13399                 read_vec_element_i32(s, tcg_res, rd, pass,
13400                                      is_scalar ? size : MO_32);
13401                 if (size == 1) {
13402                     gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13403                                                 tcg_op, tcg_idx, tcg_res);
13404                 } else {
13405                     gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13406                                                 tcg_op, tcg_idx, tcg_res);
13407                 }
13408                 break;
13409             case 0x1f: /* SQRDMLSH */
13410                 read_vec_element_i32(s, tcg_res, rd, pass,
13411                                      is_scalar ? size : MO_32);
13412                 if (size == 1) {
13413                     gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13414                                                 tcg_op, tcg_idx, tcg_res);
13415                 } else {
13416                     gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13417                                                 tcg_op, tcg_idx, tcg_res);
13418                 }
13419                 break;
13420             default:
13421                 g_assert_not_reached();
13422             }
13423 
13424             if (is_scalar) {
13425                 write_fp_sreg(s, rd, tcg_res);
13426             } else {
13427                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13428             }
13429         }
13430 
13431         clear_vec_high(s, is_q, rd);
13432     } else {
13433         /* long ops: 16x16->32 or 32x32->64 */
13434         TCGv_i64 tcg_res[2];
13435         int pass;
13436         bool satop = extract32(opcode, 0, 1);
13437         MemOp memop = MO_32;
13438 
13439         if (satop || !u) {
13440             memop |= MO_SIGN;
13441         }
13442 
13443         if (size == 2) {
13444             TCGv_i64 tcg_idx = tcg_temp_new_i64();
13445 
13446             read_vec_element(s, tcg_idx, rm, index, memop);
13447 
13448             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13449                 TCGv_i64 tcg_op = tcg_temp_new_i64();
13450                 TCGv_i64 tcg_passres;
13451                 int passelt;
13452 
13453                 if (is_scalar) {
13454                     passelt = 0;
13455                 } else {
13456                     passelt = pass + (is_q * 2);
13457                 }
13458 
13459                 read_vec_element(s, tcg_op, rn, passelt, memop);
13460 
13461                 tcg_res[pass] = tcg_temp_new_i64();
13462 
13463                 if (opcode == 0xa || opcode == 0xb) {
13464                     /* Non-accumulating ops */
13465                     tcg_passres = tcg_res[pass];
13466                 } else {
13467                     tcg_passres = tcg_temp_new_i64();
13468                 }
13469 
13470                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13471 
13472                 if (satop) {
13473                     /* saturating, doubling */
13474                     gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13475                                                       tcg_passres, tcg_passres);
13476                 }
13477 
13478                 if (opcode == 0xa || opcode == 0xb) {
13479                     continue;
13480                 }
13481 
13482                 /* Accumulating op: handle accumulate step */
13483                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13484 
13485                 switch (opcode) {
13486                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13487                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13488                     break;
13489                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13490                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13491                     break;
13492                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13493                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
13494                     /* fall through */
13495                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13496                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13497                                                       tcg_res[pass],
13498                                                       tcg_passres);
13499                     break;
13500                 default:
13501                     g_assert_not_reached();
13502                 }
13503             }
13504 
13505             clear_vec_high(s, !is_scalar, rd);
13506         } else {
13507             TCGv_i32 tcg_idx = tcg_temp_new_i32();
13508 
13509             assert(size == 1);
13510             read_vec_element_i32(s, tcg_idx, rm, index, size);
13511 
13512             if (!is_scalar) {
13513                 /* The simplest way to handle the 16x16 indexed ops is to
13514                  * duplicate the index into both halves of the 32 bit tcg_idx
13515                  * and then use the usual Neon helpers.
13516                  */
13517                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13518             }
13519 
13520             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13521                 TCGv_i32 tcg_op = tcg_temp_new_i32();
13522                 TCGv_i64 tcg_passres;
13523 
13524                 if (is_scalar) {
13525                     read_vec_element_i32(s, tcg_op, rn, pass, size);
13526                 } else {
13527                     read_vec_element_i32(s, tcg_op, rn,
13528                                          pass + (is_q * 2), MO_32);
13529                 }
13530 
13531                 tcg_res[pass] = tcg_temp_new_i64();
13532 
13533                 if (opcode == 0xa || opcode == 0xb) {
13534                     /* Non-accumulating ops */
13535                     tcg_passres = tcg_res[pass];
13536                 } else {
13537                     tcg_passres = tcg_temp_new_i64();
13538                 }
13539 
13540                 if (memop & MO_SIGN) {
13541                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13542                 } else {
13543                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13544                 }
13545                 if (satop) {
13546                     gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13547                                                       tcg_passres, tcg_passres);
13548                 }
13549 
13550                 if (opcode == 0xa || opcode == 0xb) {
13551                     continue;
13552                 }
13553 
13554                 /* Accumulating op: handle accumulate step */
13555                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13556 
13557                 switch (opcode) {
13558                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13559                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13560                                              tcg_passres);
13561                     break;
13562                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13563                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13564                                              tcg_passres);
13565                     break;
13566                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13567                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13568                     /* fall through */
13569                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13570                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13571                                                       tcg_res[pass],
13572                                                       tcg_passres);
13573                     break;
13574                 default:
13575                     g_assert_not_reached();
13576                 }
13577             }
13578 
13579             if (is_scalar) {
13580                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13581             }
13582         }
13583 
13584         if (is_scalar) {
13585             tcg_res[1] = tcg_constant_i64(0);
13586         }
13587 
13588         for (pass = 0; pass < 2; pass++) {
13589             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13590         }
13591     }
13592 }
13593 
13594 /* Crypto AES
13595  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13596  * +-----------------+------+-----------+--------+-----+------+------+
13597  * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13598  * +-----------------+------+-----------+--------+-----+------+------+
13599  */
13600 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13601 {
13602     int size = extract32(insn, 22, 2);
13603     int opcode = extract32(insn, 12, 5);
13604     int rn = extract32(insn, 5, 5);
13605     int rd = extract32(insn, 0, 5);
13606     int decrypt;
13607     gen_helper_gvec_2 *genfn2 = NULL;
13608     gen_helper_gvec_3 *genfn3 = NULL;
13609 
13610     if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13611         unallocated_encoding(s);
13612         return;
13613     }
13614 
13615     switch (opcode) {
13616     case 0x4: /* AESE */
13617         decrypt = 0;
13618         genfn3 = gen_helper_crypto_aese;
13619         break;
13620     case 0x6: /* AESMC */
13621         decrypt = 0;
13622         genfn2 = gen_helper_crypto_aesmc;
13623         break;
13624     case 0x5: /* AESD */
13625         decrypt = 1;
13626         genfn3 = gen_helper_crypto_aese;
13627         break;
13628     case 0x7: /* AESIMC */
13629         decrypt = 1;
13630         genfn2 = gen_helper_crypto_aesmc;
13631         break;
13632     default:
13633         unallocated_encoding(s);
13634         return;
13635     }
13636 
13637     if (!fp_access_check(s)) {
13638         return;
13639     }
13640     if (genfn2) {
13641         gen_gvec_op2_ool(s, true, rd, rn, decrypt, genfn2);
13642     } else {
13643         gen_gvec_op3_ool(s, true, rd, rd, rn, decrypt, genfn3);
13644     }
13645 }
13646 
13647 /* Crypto three-reg SHA
13648  *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
13649  * +-----------------+------+---+------+---+--------+-----+------+------+
13650  * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
13651  * +-----------------+------+---+------+---+--------+-----+------+------+
13652  */
13653 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13654 {
13655     int size = extract32(insn, 22, 2);
13656     int opcode = extract32(insn, 12, 3);
13657     int rm = extract32(insn, 16, 5);
13658     int rn = extract32(insn, 5, 5);
13659     int rd = extract32(insn, 0, 5);
13660     gen_helper_gvec_3 *genfn;
13661     bool feature;
13662 
13663     if (size != 0) {
13664         unallocated_encoding(s);
13665         return;
13666     }
13667 
13668     switch (opcode) {
13669     case 0: /* SHA1C */
13670         genfn = gen_helper_crypto_sha1c;
13671         feature = dc_isar_feature(aa64_sha1, s);
13672         break;
13673     case 1: /* SHA1P */
13674         genfn = gen_helper_crypto_sha1p;
13675         feature = dc_isar_feature(aa64_sha1, s);
13676         break;
13677     case 2: /* SHA1M */
13678         genfn = gen_helper_crypto_sha1m;
13679         feature = dc_isar_feature(aa64_sha1, s);
13680         break;
13681     case 3: /* SHA1SU0 */
13682         genfn = gen_helper_crypto_sha1su0;
13683         feature = dc_isar_feature(aa64_sha1, s);
13684         break;
13685     case 4: /* SHA256H */
13686         genfn = gen_helper_crypto_sha256h;
13687         feature = dc_isar_feature(aa64_sha256, s);
13688         break;
13689     case 5: /* SHA256H2 */
13690         genfn = gen_helper_crypto_sha256h2;
13691         feature = dc_isar_feature(aa64_sha256, s);
13692         break;
13693     case 6: /* SHA256SU1 */
13694         genfn = gen_helper_crypto_sha256su1;
13695         feature = dc_isar_feature(aa64_sha256, s);
13696         break;
13697     default:
13698         unallocated_encoding(s);
13699         return;
13700     }
13701 
13702     if (!feature) {
13703         unallocated_encoding(s);
13704         return;
13705     }
13706 
13707     if (!fp_access_check(s)) {
13708         return;
13709     }
13710     gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
13711 }
13712 
13713 /* Crypto two-reg SHA
13714  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13715  * +-----------------+------+-----------+--------+-----+------+------+
13716  * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13717  * +-----------------+------+-----------+--------+-----+------+------+
13718  */
13719 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13720 {
13721     int size = extract32(insn, 22, 2);
13722     int opcode = extract32(insn, 12, 5);
13723     int rn = extract32(insn, 5, 5);
13724     int rd = extract32(insn, 0, 5);
13725     gen_helper_gvec_2 *genfn;
13726     bool feature;
13727 
13728     if (size != 0) {
13729         unallocated_encoding(s);
13730         return;
13731     }
13732 
13733     switch (opcode) {
13734     case 0: /* SHA1H */
13735         feature = dc_isar_feature(aa64_sha1, s);
13736         genfn = gen_helper_crypto_sha1h;
13737         break;
13738     case 1: /* SHA1SU1 */
13739         feature = dc_isar_feature(aa64_sha1, s);
13740         genfn = gen_helper_crypto_sha1su1;
13741         break;
13742     case 2: /* SHA256SU0 */
13743         feature = dc_isar_feature(aa64_sha256, s);
13744         genfn = gen_helper_crypto_sha256su0;
13745         break;
13746     default:
13747         unallocated_encoding(s);
13748         return;
13749     }
13750 
13751     if (!feature) {
13752         unallocated_encoding(s);
13753         return;
13754     }
13755 
13756     if (!fp_access_check(s)) {
13757         return;
13758     }
13759     gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
13760 }
13761 
13762 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
13763 {
13764     tcg_gen_rotli_i64(d, m, 1);
13765     tcg_gen_xor_i64(d, d, n);
13766 }
13767 
13768 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
13769 {
13770     tcg_gen_rotli_vec(vece, d, m, 1);
13771     tcg_gen_xor_vec(vece, d, d, n);
13772 }
13773 
13774 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
13775                    uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
13776 {
13777     static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
13778     static const GVecGen3 op = {
13779         .fni8 = gen_rax1_i64,
13780         .fniv = gen_rax1_vec,
13781         .opt_opc = vecop_list,
13782         .fno = gen_helper_crypto_rax1,
13783         .vece = MO_64,
13784     };
13785     tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
13786 }
13787 
13788 /* Crypto three-reg SHA512
13789  *  31                   21 20  16 15  14  13 12  11  10  9    5 4    0
13790  * +-----------------------+------+---+---+-----+--------+------+------+
13791  * | 1 1 0 0 1 1 1 0 0 1 1 |  Rm  | 1 | O | 0 0 | opcode |  Rn  |  Rd  |
13792  * +-----------------------+------+---+---+-----+--------+------+------+
13793  */
13794 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13795 {
13796     int opcode = extract32(insn, 10, 2);
13797     int o =  extract32(insn, 14, 1);
13798     int rm = extract32(insn, 16, 5);
13799     int rn = extract32(insn, 5, 5);
13800     int rd = extract32(insn, 0, 5);
13801     bool feature;
13802     gen_helper_gvec_3 *oolfn = NULL;
13803     GVecGen3Fn *gvecfn = NULL;
13804 
13805     if (o == 0) {
13806         switch (opcode) {
13807         case 0: /* SHA512H */
13808             feature = dc_isar_feature(aa64_sha512, s);
13809             oolfn = gen_helper_crypto_sha512h;
13810             break;
13811         case 1: /* SHA512H2 */
13812             feature = dc_isar_feature(aa64_sha512, s);
13813             oolfn = gen_helper_crypto_sha512h2;
13814             break;
13815         case 2: /* SHA512SU1 */
13816             feature = dc_isar_feature(aa64_sha512, s);
13817             oolfn = gen_helper_crypto_sha512su1;
13818             break;
13819         case 3: /* RAX1 */
13820             feature = dc_isar_feature(aa64_sha3, s);
13821             gvecfn = gen_gvec_rax1;
13822             break;
13823         default:
13824             g_assert_not_reached();
13825         }
13826     } else {
13827         switch (opcode) {
13828         case 0: /* SM3PARTW1 */
13829             feature = dc_isar_feature(aa64_sm3, s);
13830             oolfn = gen_helper_crypto_sm3partw1;
13831             break;
13832         case 1: /* SM3PARTW2 */
13833             feature = dc_isar_feature(aa64_sm3, s);
13834             oolfn = gen_helper_crypto_sm3partw2;
13835             break;
13836         case 2: /* SM4EKEY */
13837             feature = dc_isar_feature(aa64_sm4, s);
13838             oolfn = gen_helper_crypto_sm4ekey;
13839             break;
13840         default:
13841             unallocated_encoding(s);
13842             return;
13843         }
13844     }
13845 
13846     if (!feature) {
13847         unallocated_encoding(s);
13848         return;
13849     }
13850 
13851     if (!fp_access_check(s)) {
13852         return;
13853     }
13854 
13855     if (oolfn) {
13856         gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
13857     } else {
13858         gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
13859     }
13860 }
13861 
13862 /* Crypto two-reg SHA512
13863  *  31                                     12  11  10  9    5 4    0
13864  * +-----------------------------------------+--------+------+------+
13865  * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode |  Rn  |  Rd  |
13866  * +-----------------------------------------+--------+------+------+
13867  */
13868 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13869 {
13870     int opcode = extract32(insn, 10, 2);
13871     int rn = extract32(insn, 5, 5);
13872     int rd = extract32(insn, 0, 5);
13873     bool feature;
13874 
13875     switch (opcode) {
13876     case 0: /* SHA512SU0 */
13877         feature = dc_isar_feature(aa64_sha512, s);
13878         break;
13879     case 1: /* SM4E */
13880         feature = dc_isar_feature(aa64_sm4, s);
13881         break;
13882     default:
13883         unallocated_encoding(s);
13884         return;
13885     }
13886 
13887     if (!feature) {
13888         unallocated_encoding(s);
13889         return;
13890     }
13891 
13892     if (!fp_access_check(s)) {
13893         return;
13894     }
13895 
13896     switch (opcode) {
13897     case 0: /* SHA512SU0 */
13898         gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
13899         break;
13900     case 1: /* SM4E */
13901         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
13902         break;
13903     default:
13904         g_assert_not_reached();
13905     }
13906 }
13907 
13908 /* Crypto four-register
13909  *  31               23 22 21 20  16 15  14  10 9    5 4    0
13910  * +-------------------+-----+------+---+------+------+------+
13911  * | 1 1 0 0 1 1 1 0 0 | Op0 |  Rm  | 0 |  Ra  |  Rn  |  Rd  |
13912  * +-------------------+-----+------+---+------+------+------+
13913  */
13914 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13915 {
13916     int op0 = extract32(insn, 21, 2);
13917     int rm = extract32(insn, 16, 5);
13918     int ra = extract32(insn, 10, 5);
13919     int rn = extract32(insn, 5, 5);
13920     int rd = extract32(insn, 0, 5);
13921     bool feature;
13922 
13923     switch (op0) {
13924     case 0: /* EOR3 */
13925     case 1: /* BCAX */
13926         feature = dc_isar_feature(aa64_sha3, s);
13927         break;
13928     case 2: /* SM3SS1 */
13929         feature = dc_isar_feature(aa64_sm3, s);
13930         break;
13931     default:
13932         unallocated_encoding(s);
13933         return;
13934     }
13935 
13936     if (!feature) {
13937         unallocated_encoding(s);
13938         return;
13939     }
13940 
13941     if (!fp_access_check(s)) {
13942         return;
13943     }
13944 
13945     if (op0 < 2) {
13946         TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13947         int pass;
13948 
13949         tcg_op1 = tcg_temp_new_i64();
13950         tcg_op2 = tcg_temp_new_i64();
13951         tcg_op3 = tcg_temp_new_i64();
13952         tcg_res[0] = tcg_temp_new_i64();
13953         tcg_res[1] = tcg_temp_new_i64();
13954 
13955         for (pass = 0; pass < 2; pass++) {
13956             read_vec_element(s, tcg_op1, rn, pass, MO_64);
13957             read_vec_element(s, tcg_op2, rm, pass, MO_64);
13958             read_vec_element(s, tcg_op3, ra, pass, MO_64);
13959 
13960             if (op0 == 0) {
13961                 /* EOR3 */
13962                 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13963             } else {
13964                 /* BCAX */
13965                 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13966             }
13967             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13968         }
13969         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13970         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13971     } else {
13972         TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13973 
13974         tcg_op1 = tcg_temp_new_i32();
13975         tcg_op2 = tcg_temp_new_i32();
13976         tcg_op3 = tcg_temp_new_i32();
13977         tcg_res = tcg_temp_new_i32();
13978         tcg_zero = tcg_constant_i32(0);
13979 
13980         read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13981         read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13982         read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13983 
13984         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13985         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13986         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13987         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13988 
13989         write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13990         write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13991         write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13992         write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13993     }
13994 }
13995 
13996 /* Crypto XAR
13997  *  31                   21 20  16 15    10 9    5 4    0
13998  * +-----------------------+------+--------+------+------+
13999  * | 1 1 0 0 1 1 1 0 1 0 0 |  Rm  |  imm6  |  Rn  |  Rd  |
14000  * +-----------------------+------+--------+------+------+
14001  */
14002 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
14003 {
14004     int rm = extract32(insn, 16, 5);
14005     int imm6 = extract32(insn, 10, 6);
14006     int rn = extract32(insn, 5, 5);
14007     int rd = extract32(insn, 0, 5);
14008 
14009     if (!dc_isar_feature(aa64_sha3, s)) {
14010         unallocated_encoding(s);
14011         return;
14012     }
14013 
14014     if (!fp_access_check(s)) {
14015         return;
14016     }
14017 
14018     gen_gvec_xar(MO_64, vec_full_reg_offset(s, rd),
14019                  vec_full_reg_offset(s, rn),
14020                  vec_full_reg_offset(s, rm), imm6, 16,
14021                  vec_full_reg_size(s));
14022 }
14023 
14024 /* Crypto three-reg imm2
14025  *  31                   21 20  16 15  14 13 12  11  10  9    5 4    0
14026  * +-----------------------+------+-----+------+--------+------+------+
14027  * | 1 1 0 0 1 1 1 0 0 1 0 |  Rm  | 1 0 | imm2 | opcode |  Rn  |  Rd  |
14028  * +-----------------------+------+-----+------+--------+------+------+
14029  */
14030 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
14031 {
14032     static gen_helper_gvec_3 * const fns[4] = {
14033         gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
14034         gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
14035     };
14036     int opcode = extract32(insn, 10, 2);
14037     int imm2 = extract32(insn, 12, 2);
14038     int rm = extract32(insn, 16, 5);
14039     int rn = extract32(insn, 5, 5);
14040     int rd = extract32(insn, 0, 5);
14041 
14042     if (!dc_isar_feature(aa64_sm3, s)) {
14043         unallocated_encoding(s);
14044         return;
14045     }
14046 
14047     if (!fp_access_check(s)) {
14048         return;
14049     }
14050 
14051     gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
14052 }
14053 
14054 /* C3.6 Data processing - SIMD, inc Crypto
14055  *
14056  * As the decode gets a little complex we are using a table based
14057  * approach for this part of the decode.
14058  */
14059 static const AArch64DecodeTable data_proc_simd[] = {
14060     /* pattern  ,  mask     ,  fn                        */
14061     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
14062     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
14063     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
14064     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
14065     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
14066     { 0x0e000400, 0x9fe08400, disas_simd_copy },
14067     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
14068     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
14069     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
14070     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
14071     { 0x0e000000, 0xbf208c00, disas_simd_tb },
14072     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
14073     { 0x2e000000, 0xbf208400, disas_simd_ext },
14074     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
14075     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
14076     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
14077     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
14078     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
14079     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
14080     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
14081     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
14082     { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
14083     { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
14084     { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
14085     { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
14086     { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
14087     { 0xce000000, 0xff808000, disas_crypto_four_reg },
14088     { 0xce800000, 0xffe00000, disas_crypto_xar },
14089     { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
14090     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
14091     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
14092     { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
14093     { 0x00000000, 0x00000000, NULL }
14094 };
14095 
14096 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
14097 {
14098     /* Note that this is called with all non-FP cases from
14099      * table C3-6 so it must UNDEF for entries not specifically
14100      * allocated to instructions in that table.
14101      */
14102     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
14103     if (fn) {
14104         fn(s, insn);
14105     } else {
14106         unallocated_encoding(s);
14107     }
14108 }
14109 
14110 /* C3.6 Data processing - SIMD and floating point */
14111 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
14112 {
14113     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
14114         disas_data_proc_fp(s, insn);
14115     } else {
14116         /* SIMD, including crypto */
14117         disas_data_proc_simd(s, insn);
14118     }
14119 }
14120 
14121 static bool trans_OK(DisasContext *s, arg_OK *a)
14122 {
14123     return true;
14124 }
14125 
14126 static bool trans_FAIL(DisasContext *s, arg_OK *a)
14127 {
14128     s->is_nonstreaming = true;
14129     return true;
14130 }
14131 
14132 /**
14133  * is_guarded_page:
14134  * @env: The cpu environment
14135  * @s: The DisasContext
14136  *
14137  * Return true if the page is guarded.
14138  */
14139 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
14140 {
14141     uint64_t addr = s->base.pc_first;
14142 #ifdef CONFIG_USER_ONLY
14143     return page_get_flags(addr) & PAGE_BTI;
14144 #else
14145     CPUTLBEntryFull *full;
14146     void *host;
14147     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
14148     int flags;
14149 
14150     /*
14151      * We test this immediately after reading an insn, which means
14152      * that the TLB entry must be present and valid, and thus this
14153      * access will never raise an exception.
14154      */
14155     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
14156                               false, &host, &full, 0);
14157     assert(!(flags & TLB_INVALID_MASK));
14158 
14159     return full->guarded;
14160 #endif
14161 }
14162 
14163 /**
14164  * btype_destination_ok:
14165  * @insn: The instruction at the branch destination
14166  * @bt: SCTLR_ELx.BT
14167  * @btype: PSTATE.BTYPE, and is non-zero
14168  *
14169  * On a guarded page, there are a limited number of insns
14170  * that may be present at the branch target:
14171  *   - branch target identifiers,
14172  *   - paciasp, pacibsp,
14173  *   - BRK insn
14174  *   - HLT insn
14175  * Anything else causes a Branch Target Exception.
14176  *
14177  * Return true if the branch is compatible, false to raise BTITRAP.
14178  */
14179 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
14180 {
14181     if ((insn & 0xfffff01fu) == 0xd503201fu) {
14182         /* HINT space */
14183         switch (extract32(insn, 5, 7)) {
14184         case 0b011001: /* PACIASP */
14185         case 0b011011: /* PACIBSP */
14186             /*
14187              * If SCTLR_ELx.BT, then PACI*SP are not compatible
14188              * with btype == 3.  Otherwise all btype are ok.
14189              */
14190             return !bt || btype != 3;
14191         case 0b100000: /* BTI */
14192             /* Not compatible with any btype.  */
14193             return false;
14194         case 0b100010: /* BTI c */
14195             /* Not compatible with btype == 3 */
14196             return btype != 3;
14197         case 0b100100: /* BTI j */
14198             /* Not compatible with btype == 2 */
14199             return btype != 2;
14200         case 0b100110: /* BTI jc */
14201             /* Compatible with any btype.  */
14202             return true;
14203         }
14204     } else {
14205         switch (insn & 0xffe0001fu) {
14206         case 0xd4200000u: /* BRK */
14207         case 0xd4400000u: /* HLT */
14208             /* Give priority to the breakpoint exception.  */
14209             return true;
14210         }
14211     }
14212     return false;
14213 }
14214 
14215 /* C3.1 A64 instruction index by encoding */
14216 static void disas_a64_legacy(DisasContext *s, uint32_t insn)
14217 {
14218     switch (extract32(insn, 25, 4)) {
14219     case 0xa: case 0xb: /* Branch, exception generation and system insns */
14220         disas_b_exc_sys(s, insn);
14221         break;
14222     case 0x4:
14223     case 0x6:
14224     case 0xc:
14225     case 0xe:      /* Loads and stores */
14226         disas_ldst(s, insn);
14227         break;
14228     case 0x5:
14229     case 0xd:      /* Data processing - register */
14230         disas_data_proc_reg(s, insn);
14231         break;
14232     case 0x7:
14233     case 0xf:      /* Data processing - SIMD and floating point */
14234         disas_data_proc_simd_fp(s, insn);
14235         break;
14236     default:
14237         unallocated_encoding(s);
14238         break;
14239     }
14240 }
14241 
14242 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14243                                           CPUState *cpu)
14244 {
14245     DisasContext *dc = container_of(dcbase, DisasContext, base);
14246     CPUARMState *env = cpu->env_ptr;
14247     ARMCPU *arm_cpu = env_archcpu(env);
14248     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
14249     int bound, core_mmu_idx;
14250 
14251     dc->isar = &arm_cpu->isar;
14252     dc->condjmp = 0;
14253     dc->pc_save = dc->base.pc_first;
14254     dc->aarch64 = true;
14255     dc->thumb = false;
14256     dc->sctlr_b = 0;
14257     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
14258     dc->condexec_mask = 0;
14259     dc->condexec_cond = 0;
14260     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
14261     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
14262     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
14263     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
14264     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
14265     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14266 #if !defined(CONFIG_USER_ONLY)
14267     dc->user = (dc->current_el == 0);
14268 #endif
14269     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
14270     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
14271     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
14272     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
14273     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
14274     dc->fgt_eret = EX_TBFLAG_A64(tb_flags, FGT_ERET);
14275     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
14276     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
14277     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
14278     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
14279     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
14280     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
14281     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
14282     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
14283     dc->ata = EX_TBFLAG_A64(tb_flags, ATA);
14284     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
14285     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
14286     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
14287     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
14288     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
14289     dc->naa = EX_TBFLAG_A64(tb_flags, NAA);
14290     dc->vec_len = 0;
14291     dc->vec_stride = 0;
14292     dc->cp_regs = arm_cpu->cp_regs;
14293     dc->features = env->features;
14294     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
14295 
14296 #ifdef CONFIG_USER_ONLY
14297     /* In sve_probe_page, we assume TBI is enabled. */
14298     tcg_debug_assert(dc->tbid & 1);
14299 #endif
14300 
14301     dc->lse2 = dc_isar_feature(aa64_lse2, dc);
14302 
14303     /* Single step state. The code-generation logic here is:
14304      *  SS_ACTIVE == 0:
14305      *   generate code with no special handling for single-stepping (except
14306      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14307      *   this happens anyway because those changes are all system register or
14308      *   PSTATE writes).
14309      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14310      *   emit code for one insn
14311      *   emit code to clear PSTATE.SS
14312      *   emit code to generate software step exception for completed step
14313      *   end TB (as usual for having generated an exception)
14314      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14315      *   emit code to generate a software step exception
14316      *   end the TB
14317      */
14318     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
14319     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
14320     dc->is_ldex = false;
14321 
14322     /* Bound the number of insns to execute to those left on the page.  */
14323     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14324 
14325     /* If architectural single step active, limit to 1.  */
14326     if (dc->ss_active) {
14327         bound = 1;
14328     }
14329     dc->base.max_insns = MIN(dc->base.max_insns, bound);
14330 }
14331 
14332 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14333 {
14334 }
14335 
14336 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14337 {
14338     DisasContext *dc = container_of(dcbase, DisasContext, base);
14339     target_ulong pc_arg = dc->base.pc_next;
14340 
14341     if (tb_cflags(dcbase->tb) & CF_PCREL) {
14342         pc_arg &= ~TARGET_PAGE_MASK;
14343     }
14344     tcg_gen_insn_start(pc_arg, 0, 0);
14345     dc->insn_start = tcg_last_op();
14346 }
14347 
14348 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14349 {
14350     DisasContext *s = container_of(dcbase, DisasContext, base);
14351     CPUARMState *env = cpu->env_ptr;
14352     uint64_t pc = s->base.pc_next;
14353     uint32_t insn;
14354 
14355     /* Singlestep exceptions have the highest priority. */
14356     if (s->ss_active && !s->pstate_ss) {
14357         /* Singlestep state is Active-pending.
14358          * If we're in this state at the start of a TB then either
14359          *  a) we just took an exception to an EL which is being debugged
14360          *     and this is the first insn in the exception handler
14361          *  b) debug exceptions were masked and we just unmasked them
14362          *     without changing EL (eg by clearing PSTATE.D)
14363          * In either case we're going to take a swstep exception in the
14364          * "did not step an insn" case, and so the syndrome ISV and EX
14365          * bits should be zero.
14366          */
14367         assert(s->base.num_insns == 1);
14368         gen_swstep_exception(s, 0, 0);
14369         s->base.is_jmp = DISAS_NORETURN;
14370         s->base.pc_next = pc + 4;
14371         return;
14372     }
14373 
14374     if (pc & 3) {
14375         /*
14376          * PC alignment fault.  This has priority over the instruction abort
14377          * that we would receive from a translation fault via arm_ldl_code.
14378          * This should only be possible after an indirect branch, at the
14379          * start of the TB.
14380          */
14381         assert(s->base.num_insns == 1);
14382         gen_helper_exception_pc_alignment(cpu_env, tcg_constant_tl(pc));
14383         s->base.is_jmp = DISAS_NORETURN;
14384         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
14385         return;
14386     }
14387 
14388     s->pc_curr = pc;
14389     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
14390     s->insn = insn;
14391     s->base.pc_next = pc + 4;
14392 
14393     s->fp_access_checked = false;
14394     s->sve_access_checked = false;
14395 
14396     if (s->pstate_il) {
14397         /*
14398          * Illegal execution state. This has priority over BTI
14399          * exceptions, but comes after instruction abort exceptions.
14400          */
14401         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
14402         return;
14403     }
14404 
14405     if (dc_isar_feature(aa64_bti, s)) {
14406         if (s->base.num_insns == 1) {
14407             /*
14408              * At the first insn of the TB, compute s->guarded_page.
14409              * We delayed computing this until successfully reading
14410              * the first insn of the TB, above.  This (mostly) ensures
14411              * that the softmmu tlb entry has been populated, and the
14412              * page table GP bit is available.
14413              *
14414              * Note that we need to compute this even if btype == 0,
14415              * because this value is used for BR instructions later
14416              * where ENV is not available.
14417              */
14418             s->guarded_page = is_guarded_page(env, s);
14419 
14420             /* First insn can have btype set to non-zero.  */
14421             tcg_debug_assert(s->btype >= 0);
14422 
14423             /*
14424              * Note that the Branch Target Exception has fairly high
14425              * priority -- below debugging exceptions but above most
14426              * everything else.  This allows us to handle this now
14427              * instead of waiting until the insn is otherwise decoded.
14428              */
14429             if (s->btype != 0
14430                 && s->guarded_page
14431                 && !btype_destination_ok(insn, s->bt, s->btype)) {
14432                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
14433                 return;
14434             }
14435         } else {
14436             /* Not the first insn: btype must be 0.  */
14437             tcg_debug_assert(s->btype == 0);
14438         }
14439     }
14440 
14441     s->is_nonstreaming = false;
14442     if (s->sme_trap_nonstreaming) {
14443         disas_sme_fa64(s, insn);
14444     }
14445 
14446     if (!disas_a64(s, insn) &&
14447         !disas_sme(s, insn) &&
14448         !disas_sve(s, insn)) {
14449         disas_a64_legacy(s, insn);
14450     }
14451 
14452     /*
14453      * After execution of most insns, btype is reset to 0.
14454      * Note that we set btype == -1 when the insn sets btype.
14455      */
14456     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14457         reset_btype(s);
14458     }
14459 }
14460 
14461 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14462 {
14463     DisasContext *dc = container_of(dcbase, DisasContext, base);
14464 
14465     if (unlikely(dc->ss_active)) {
14466         /* Note that this means single stepping WFI doesn't halt the CPU.
14467          * For conditional branch insns this is harmless unreachable code as
14468          * gen_goto_tb() has already handled emitting the debug exception
14469          * (and thus a tb-jump is not possible when singlestepping).
14470          */
14471         switch (dc->base.is_jmp) {
14472         default:
14473             gen_a64_update_pc(dc, 4);
14474             /* fall through */
14475         case DISAS_EXIT:
14476         case DISAS_JUMP:
14477             gen_step_complete_exception(dc);
14478             break;
14479         case DISAS_NORETURN:
14480             break;
14481         }
14482     } else {
14483         switch (dc->base.is_jmp) {
14484         case DISAS_NEXT:
14485         case DISAS_TOO_MANY:
14486             gen_goto_tb(dc, 1, 4);
14487             break;
14488         default:
14489         case DISAS_UPDATE_EXIT:
14490             gen_a64_update_pc(dc, 4);
14491             /* fall through */
14492         case DISAS_EXIT:
14493             tcg_gen_exit_tb(NULL, 0);
14494             break;
14495         case DISAS_UPDATE_NOCHAIN:
14496             gen_a64_update_pc(dc, 4);
14497             /* fall through */
14498         case DISAS_JUMP:
14499             tcg_gen_lookup_and_goto_ptr();
14500             break;
14501         case DISAS_NORETURN:
14502         case DISAS_SWI:
14503             break;
14504         case DISAS_WFE:
14505             gen_a64_update_pc(dc, 4);
14506             gen_helper_wfe(cpu_env);
14507             break;
14508         case DISAS_YIELD:
14509             gen_a64_update_pc(dc, 4);
14510             gen_helper_yield(cpu_env);
14511             break;
14512         case DISAS_WFI:
14513             /*
14514              * This is a special case because we don't want to just halt
14515              * the CPU if trying to debug across a WFI.
14516              */
14517             gen_a64_update_pc(dc, 4);
14518             gen_helper_wfi(cpu_env, tcg_constant_i32(4));
14519             /*
14520              * The helper doesn't necessarily throw an exception, but we
14521              * must go back to the main loop to check for interrupts anyway.
14522              */
14523             tcg_gen_exit_tb(NULL, 0);
14524             break;
14525         }
14526     }
14527 }
14528 
14529 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14530                                  CPUState *cpu, FILE *logfile)
14531 {
14532     DisasContext *dc = container_of(dcbase, DisasContext, base);
14533 
14534     fprintf(logfile, "IN: %s\n", lookup_symbol(dc->base.pc_first));
14535     target_disas(logfile, cpu, dc->base.pc_first, dc->base.tb->size);
14536 }
14537 
14538 const TranslatorOps aarch64_translator_ops = {
14539     .init_disas_context = aarch64_tr_init_disas_context,
14540     .tb_start           = aarch64_tr_tb_start,
14541     .insn_start         = aarch64_tr_insn_start,
14542     .translate_insn     = aarch64_tr_translate_insn,
14543     .tb_stop            = aarch64_tr_tb_stop,
14544     .disas_log          = aarch64_tr_disas_log,
14545 };
14546