xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision c1a1f805)
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 /* HINT instruction group, including various allocated HINTs */
1653 static void handle_hint(DisasContext *s, uint32_t insn,
1654                         unsigned int op1, unsigned int op2, unsigned int crm)
1655 {
1656     unsigned int selector = crm << 3 | op2;
1657 
1658     if (op1 != 3) {
1659         unallocated_encoding(s);
1660         return;
1661     }
1662 
1663     switch (selector) {
1664     case 0b00000: /* NOP */
1665         break;
1666     case 0b00011: /* WFI */
1667         s->base.is_jmp = DISAS_WFI;
1668         break;
1669     case 0b00001: /* YIELD */
1670         /* When running in MTTCG we don't generate jumps to the yield and
1671          * WFE helpers as it won't affect the scheduling of other vCPUs.
1672          * If we wanted to more completely model WFE/SEV so we don't busy
1673          * spin unnecessarily we would need to do something more involved.
1674          */
1675         if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1676             s->base.is_jmp = DISAS_YIELD;
1677         }
1678         break;
1679     case 0b00010: /* WFE */
1680         if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1681             s->base.is_jmp = DISAS_WFE;
1682         }
1683         break;
1684     case 0b00100: /* SEV */
1685     case 0b00101: /* SEVL */
1686     case 0b00110: /* DGH */
1687         /* we treat all as NOP at least for now */
1688         break;
1689     case 0b00111: /* XPACLRI */
1690         if (s->pauth_active) {
1691             gen_helper_xpaci(cpu_X[30], cpu_env, cpu_X[30]);
1692         }
1693         break;
1694     case 0b01000: /* PACIA1716 */
1695         if (s->pauth_active) {
1696             gen_helper_pacia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1697         }
1698         break;
1699     case 0b01010: /* PACIB1716 */
1700         if (s->pauth_active) {
1701             gen_helper_pacib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1702         }
1703         break;
1704     case 0b01100: /* AUTIA1716 */
1705         if (s->pauth_active) {
1706             gen_helper_autia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1707         }
1708         break;
1709     case 0b01110: /* AUTIB1716 */
1710         if (s->pauth_active) {
1711             gen_helper_autib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1712         }
1713         break;
1714     case 0b10000: /* ESB */
1715         /* Without RAS, we must implement this as NOP. */
1716         if (dc_isar_feature(aa64_ras, s)) {
1717             /*
1718              * QEMU does not have a source of physical SErrors,
1719              * so we are only concerned with virtual SErrors.
1720              * The pseudocode in the ARM for this case is
1721              *   if PSTATE.EL IN {EL0, EL1} && EL2Enabled() then
1722              *      AArch64.vESBOperation();
1723              * Most of the condition can be evaluated at translation time.
1724              * Test for EL2 present, and defer test for SEL2 to runtime.
1725              */
1726             if (s->current_el <= 1 && arm_dc_feature(s, ARM_FEATURE_EL2)) {
1727                 gen_helper_vesb(cpu_env);
1728             }
1729         }
1730         break;
1731     case 0b11000: /* PACIAZ */
1732         if (s->pauth_active) {
1733             gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30],
1734                              tcg_constant_i64(0));
1735         }
1736         break;
1737     case 0b11001: /* PACIASP */
1738         if (s->pauth_active) {
1739             gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1740         }
1741         break;
1742     case 0b11010: /* PACIBZ */
1743         if (s->pauth_active) {
1744             gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30],
1745                              tcg_constant_i64(0));
1746         }
1747         break;
1748     case 0b11011: /* PACIBSP */
1749         if (s->pauth_active) {
1750             gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1751         }
1752         break;
1753     case 0b11100: /* AUTIAZ */
1754         if (s->pauth_active) {
1755             gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30],
1756                              tcg_constant_i64(0));
1757         }
1758         break;
1759     case 0b11101: /* AUTIASP */
1760         if (s->pauth_active) {
1761             gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1762         }
1763         break;
1764     case 0b11110: /* AUTIBZ */
1765         if (s->pauth_active) {
1766             gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30],
1767                              tcg_constant_i64(0));
1768         }
1769         break;
1770     case 0b11111: /* AUTIBSP */
1771         if (s->pauth_active) {
1772             gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1773         }
1774         break;
1775     default:
1776         /* default specified as NOP equivalent */
1777         break;
1778     }
1779 }
1780 
1781 static void gen_clrex(DisasContext *s, uint32_t insn)
1782 {
1783     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1784 }
1785 
1786 /* CLREX, DSB, DMB, ISB */
1787 static void handle_sync(DisasContext *s, uint32_t insn,
1788                         unsigned int op1, unsigned int op2, unsigned int crm)
1789 {
1790     TCGBar bar;
1791 
1792     if (op1 != 3) {
1793         unallocated_encoding(s);
1794         return;
1795     }
1796 
1797     switch (op2) {
1798     case 2: /* CLREX */
1799         gen_clrex(s, insn);
1800         return;
1801     case 4: /* DSB */
1802     case 5: /* DMB */
1803         switch (crm & 3) {
1804         case 1: /* MBReqTypes_Reads */
1805             bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1806             break;
1807         case 2: /* MBReqTypes_Writes */
1808             bar = TCG_BAR_SC | TCG_MO_ST_ST;
1809             break;
1810         default: /* MBReqTypes_All */
1811             bar = TCG_BAR_SC | TCG_MO_ALL;
1812             break;
1813         }
1814         tcg_gen_mb(bar);
1815         return;
1816     case 6: /* ISB */
1817         /* We need to break the TB after this insn to execute
1818          * a self-modified code correctly and also to take
1819          * any pending interrupts immediately.
1820          */
1821         reset_btype(s);
1822         gen_goto_tb(s, 0, 4);
1823         return;
1824 
1825     case 7: /* SB */
1826         if (crm != 0 || !dc_isar_feature(aa64_sb, s)) {
1827             goto do_unallocated;
1828         }
1829         /*
1830          * TODO: There is no speculation barrier opcode for TCG;
1831          * MB and end the TB instead.
1832          */
1833         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1834         gen_goto_tb(s, 0, 4);
1835         return;
1836 
1837     default:
1838     do_unallocated:
1839         unallocated_encoding(s);
1840         return;
1841     }
1842 }
1843 
1844 static void gen_xaflag(void)
1845 {
1846     TCGv_i32 z = tcg_temp_new_i32();
1847 
1848     tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1849 
1850     /*
1851      * (!C & !Z) << 31
1852      * (!(C | Z)) << 31
1853      * ~((C | Z) << 31)
1854      * ~-(C | Z)
1855      * (C | Z) - 1
1856      */
1857     tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1858     tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1859 
1860     /* !(Z & C) */
1861     tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1862     tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1863 
1864     /* (!C & Z) << 31 -> -(Z & ~C) */
1865     tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1866     tcg_gen_neg_i32(cpu_VF, cpu_VF);
1867 
1868     /* C | Z */
1869     tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1870 }
1871 
1872 static void gen_axflag(void)
1873 {
1874     tcg_gen_sari_i32(cpu_VF, cpu_VF, 31);         /* V ? -1 : 0 */
1875     tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF);     /* C & !V */
1876 
1877     /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1878     tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1879 
1880     tcg_gen_movi_i32(cpu_NF, 0);
1881     tcg_gen_movi_i32(cpu_VF, 0);
1882 }
1883 
1884 /* MSR (immediate) - move immediate to processor state field */
1885 static void handle_msr_i(DisasContext *s, uint32_t insn,
1886                          unsigned int op1, unsigned int op2, unsigned int crm)
1887 {
1888     int op = op1 << 3 | op2;
1889 
1890     /* End the TB by default, chaining is ok.  */
1891     s->base.is_jmp = DISAS_TOO_MANY;
1892 
1893     switch (op) {
1894     case 0x00: /* CFINV */
1895         if (crm != 0 || !dc_isar_feature(aa64_condm_4, s)) {
1896             goto do_unallocated;
1897         }
1898         tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1899         s->base.is_jmp = DISAS_NEXT;
1900         break;
1901 
1902     case 0x01: /* XAFlag */
1903         if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1904             goto do_unallocated;
1905         }
1906         gen_xaflag();
1907         s->base.is_jmp = DISAS_NEXT;
1908         break;
1909 
1910     case 0x02: /* AXFlag */
1911         if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1912             goto do_unallocated;
1913         }
1914         gen_axflag();
1915         s->base.is_jmp = DISAS_NEXT;
1916         break;
1917 
1918     case 0x03: /* UAO */
1919         if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1920             goto do_unallocated;
1921         }
1922         if (crm & 1) {
1923             set_pstate_bits(PSTATE_UAO);
1924         } else {
1925             clear_pstate_bits(PSTATE_UAO);
1926         }
1927         gen_rebuild_hflags(s);
1928         break;
1929 
1930     case 0x04: /* PAN */
1931         if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1932             goto do_unallocated;
1933         }
1934         if (crm & 1) {
1935             set_pstate_bits(PSTATE_PAN);
1936         } else {
1937             clear_pstate_bits(PSTATE_PAN);
1938         }
1939         gen_rebuild_hflags(s);
1940         break;
1941 
1942     case 0x05: /* SPSel */
1943         if (s->current_el == 0) {
1944             goto do_unallocated;
1945         }
1946         gen_helper_msr_i_spsel(cpu_env, tcg_constant_i32(crm & PSTATE_SP));
1947         break;
1948 
1949     case 0x19: /* SSBS */
1950         if (!dc_isar_feature(aa64_ssbs, s)) {
1951             goto do_unallocated;
1952         }
1953         if (crm & 1) {
1954             set_pstate_bits(PSTATE_SSBS);
1955         } else {
1956             clear_pstate_bits(PSTATE_SSBS);
1957         }
1958         /* Don't need to rebuild hflags since SSBS is a nop */
1959         break;
1960 
1961     case 0x1a: /* DIT */
1962         if (!dc_isar_feature(aa64_dit, s)) {
1963             goto do_unallocated;
1964         }
1965         if (crm & 1) {
1966             set_pstate_bits(PSTATE_DIT);
1967         } else {
1968             clear_pstate_bits(PSTATE_DIT);
1969         }
1970         /* There's no need to rebuild hflags because DIT is a nop */
1971         break;
1972 
1973     case 0x1e: /* DAIFSet */
1974         gen_helper_msr_i_daifset(cpu_env, tcg_constant_i32(crm));
1975         break;
1976 
1977     case 0x1f: /* DAIFClear */
1978         gen_helper_msr_i_daifclear(cpu_env, tcg_constant_i32(crm));
1979         /* For DAIFClear, exit the cpu loop to re-evaluate pending IRQs.  */
1980         s->base.is_jmp = DISAS_UPDATE_EXIT;
1981         break;
1982 
1983     case 0x1c: /* TCO */
1984         if (dc_isar_feature(aa64_mte, s)) {
1985             /* Full MTE is enabled -- set the TCO bit as directed. */
1986             if (crm & 1) {
1987                 set_pstate_bits(PSTATE_TCO);
1988             } else {
1989                 clear_pstate_bits(PSTATE_TCO);
1990             }
1991             gen_rebuild_hflags(s);
1992             /* Many factors, including TCO, go into MTE_ACTIVE. */
1993             s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
1994         } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
1995             /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI.  */
1996             s->base.is_jmp = DISAS_NEXT;
1997         } else {
1998             goto do_unallocated;
1999         }
2000         break;
2001 
2002     case 0x1b: /* SVCR* */
2003         if (!dc_isar_feature(aa64_sme, s) || crm < 2 || crm > 7) {
2004             goto do_unallocated;
2005         }
2006         if (sme_access_check(s)) {
2007             int old = s->pstate_sm | (s->pstate_za << 1);
2008             int new = (crm & 1) * 3;
2009             int msk = (crm >> 1) & 3;
2010 
2011             if ((old ^ new) & msk) {
2012                 /* At least one bit changes. */
2013                 gen_helper_set_svcr(cpu_env, tcg_constant_i32(new),
2014                                     tcg_constant_i32(msk));
2015             } else {
2016                 s->base.is_jmp = DISAS_NEXT;
2017             }
2018         }
2019         break;
2020 
2021     default:
2022     do_unallocated:
2023         unallocated_encoding(s);
2024         return;
2025     }
2026 }
2027 
2028 static void gen_get_nzcv(TCGv_i64 tcg_rt)
2029 {
2030     TCGv_i32 tmp = tcg_temp_new_i32();
2031     TCGv_i32 nzcv = tcg_temp_new_i32();
2032 
2033     /* build bit 31, N */
2034     tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
2035     /* build bit 30, Z */
2036     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
2037     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
2038     /* build bit 29, C */
2039     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
2040     /* build bit 28, V */
2041     tcg_gen_shri_i32(tmp, cpu_VF, 31);
2042     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
2043     /* generate result */
2044     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
2045 }
2046 
2047 static void gen_set_nzcv(TCGv_i64 tcg_rt)
2048 {
2049     TCGv_i32 nzcv = tcg_temp_new_i32();
2050 
2051     /* take NZCV from R[t] */
2052     tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
2053 
2054     /* bit 31, N */
2055     tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
2056     /* bit 30, Z */
2057     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
2058     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
2059     /* bit 29, C */
2060     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
2061     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
2062     /* bit 28, V */
2063     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
2064     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
2065 }
2066 
2067 static void gen_sysreg_undef(DisasContext *s, bool isread,
2068                              uint8_t op0, uint8_t op1, uint8_t op2,
2069                              uint8_t crn, uint8_t crm, uint8_t rt)
2070 {
2071     /*
2072      * Generate code to emit an UNDEF with correct syndrome
2073      * information for a failed system register access.
2074      * This is EC_UNCATEGORIZED (ie a standard UNDEF) in most cases,
2075      * but if FEAT_IDST is implemented then read accesses to registers
2076      * in the feature ID space are reported with the EC_SYSTEMREGISTERTRAP
2077      * syndrome.
2078      */
2079     uint32_t syndrome;
2080 
2081     if (isread && dc_isar_feature(aa64_ids, s) &&
2082         arm_cpreg_encoding_in_idspace(op0, op1, op2, crn, crm)) {
2083         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2084     } else {
2085         syndrome = syn_uncategorized();
2086     }
2087     gen_exception_insn(s, 0, EXCP_UDEF, syndrome);
2088 }
2089 
2090 /* MRS - move from system register
2091  * MSR (register) - move to system register
2092  * SYS
2093  * SYSL
2094  * These are all essentially the same insn in 'read' and 'write'
2095  * versions, with varying op0 fields.
2096  */
2097 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
2098                        unsigned int op0, unsigned int op1, unsigned int op2,
2099                        unsigned int crn, unsigned int crm, unsigned int rt)
2100 {
2101     uint32_t key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
2102                                       crn, crm, op0, op1, op2);
2103     const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
2104     bool need_exit_tb = false;
2105     TCGv_ptr tcg_ri = NULL;
2106     TCGv_i64 tcg_rt;
2107 
2108     if (!ri) {
2109         /* Unknown register; this might be a guest error or a QEMU
2110          * unimplemented feature.
2111          */
2112         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
2113                       "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
2114                       isread ? "read" : "write", op0, op1, crn, crm, op2);
2115         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2116         return;
2117     }
2118 
2119     /* Check access permissions */
2120     if (!cp_access_ok(s->current_el, ri, isread)) {
2121         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2122         return;
2123     }
2124 
2125     if (ri->accessfn || (ri->fgt && s->fgt_active)) {
2126         /* Emit code to perform further access permissions checks at
2127          * runtime; this may result in an exception.
2128          */
2129         uint32_t syndrome;
2130 
2131         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2132         gen_a64_update_pc(s, 0);
2133         tcg_ri = tcg_temp_new_ptr();
2134         gen_helper_access_check_cp_reg(tcg_ri, cpu_env,
2135                                        tcg_constant_i32(key),
2136                                        tcg_constant_i32(syndrome),
2137                                        tcg_constant_i32(isread));
2138     } else if (ri->type & ARM_CP_RAISES_EXC) {
2139         /*
2140          * The readfn or writefn might raise an exception;
2141          * synchronize the CPU state in case it does.
2142          */
2143         gen_a64_update_pc(s, 0);
2144     }
2145 
2146     /* Handle special cases first */
2147     switch (ri->type & ARM_CP_SPECIAL_MASK) {
2148     case 0:
2149         break;
2150     case ARM_CP_NOP:
2151         return;
2152     case ARM_CP_NZCV:
2153         tcg_rt = cpu_reg(s, rt);
2154         if (isread) {
2155             gen_get_nzcv(tcg_rt);
2156         } else {
2157             gen_set_nzcv(tcg_rt);
2158         }
2159         return;
2160     case ARM_CP_CURRENTEL:
2161         /* Reads as current EL value from pstate, which is
2162          * guaranteed to be constant by the tb flags.
2163          */
2164         tcg_rt = cpu_reg(s, rt);
2165         tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
2166         return;
2167     case ARM_CP_DC_ZVA:
2168         /* Writes clear the aligned block of memory which rt points into. */
2169         if (s->mte_active[0]) {
2170             int desc = 0;
2171 
2172             desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
2173             desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
2174             desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
2175 
2176             tcg_rt = tcg_temp_new_i64();
2177             gen_helper_mte_check_zva(tcg_rt, cpu_env,
2178                                      tcg_constant_i32(desc), cpu_reg(s, rt));
2179         } else {
2180             tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
2181         }
2182         gen_helper_dc_zva(cpu_env, tcg_rt);
2183         return;
2184     case ARM_CP_DC_GVA:
2185         {
2186             TCGv_i64 clean_addr, tag;
2187 
2188             /*
2189              * DC_GVA, like DC_ZVA, requires that we supply the original
2190              * pointer for an invalid page.  Probe that address first.
2191              */
2192             tcg_rt = cpu_reg(s, rt);
2193             clean_addr = clean_data_tbi(s, tcg_rt);
2194             gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
2195 
2196             if (s->ata) {
2197                 /* Extract the tag from the register to match STZGM.  */
2198                 tag = tcg_temp_new_i64();
2199                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2200                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2201             }
2202         }
2203         return;
2204     case ARM_CP_DC_GZVA:
2205         {
2206             TCGv_i64 clean_addr, tag;
2207 
2208             /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
2209             tcg_rt = cpu_reg(s, rt);
2210             clean_addr = clean_data_tbi(s, tcg_rt);
2211             gen_helper_dc_zva(cpu_env, clean_addr);
2212 
2213             if (s->ata) {
2214                 /* Extract the tag from the register to match STZGM.  */
2215                 tag = tcg_temp_new_i64();
2216                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2217                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2218             }
2219         }
2220         return;
2221     default:
2222         g_assert_not_reached();
2223     }
2224     if ((ri->type & ARM_CP_FPU) && !fp_access_check_only(s)) {
2225         return;
2226     } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
2227         return;
2228     } else if ((ri->type & ARM_CP_SME) && !sme_access_check(s)) {
2229         return;
2230     }
2231 
2232     if (ri->type & ARM_CP_IO) {
2233         /* I/O operations must end the TB here (whether read or write) */
2234         need_exit_tb = translator_io_start(&s->base);
2235     }
2236 
2237     tcg_rt = cpu_reg(s, rt);
2238 
2239     if (isread) {
2240         if (ri->type & ARM_CP_CONST) {
2241             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
2242         } else if (ri->readfn) {
2243             if (!tcg_ri) {
2244                 tcg_ri = gen_lookup_cp_reg(key);
2245             }
2246             gen_helper_get_cp_reg64(tcg_rt, cpu_env, tcg_ri);
2247         } else {
2248             tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
2249         }
2250     } else {
2251         if (ri->type & ARM_CP_CONST) {
2252             /* If not forbidden by access permissions, treat as WI */
2253             return;
2254         } else if (ri->writefn) {
2255             if (!tcg_ri) {
2256                 tcg_ri = gen_lookup_cp_reg(key);
2257             }
2258             gen_helper_set_cp_reg64(cpu_env, tcg_ri, tcg_rt);
2259         } else {
2260             tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
2261         }
2262     }
2263 
2264     if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
2265         /*
2266          * A write to any coprocessor regiser that ends a TB
2267          * must rebuild the hflags for the next TB.
2268          */
2269         gen_rebuild_hflags(s);
2270         /*
2271          * We default to ending the TB on a coprocessor register write,
2272          * but allow this to be suppressed by the register definition
2273          * (usually only necessary to work around guest bugs).
2274          */
2275         need_exit_tb = true;
2276     }
2277     if (need_exit_tb) {
2278         s->base.is_jmp = DISAS_UPDATE_EXIT;
2279     }
2280 }
2281 
2282 /* System
2283  *  31                 22 21  20 19 18 16 15   12 11    8 7   5 4    0
2284  * +---------------------+---+-----+-----+-------+-------+-----+------+
2285  * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 |  CRn  |  CRm  | op2 |  Rt  |
2286  * +---------------------+---+-----+-----+-------+-------+-----+------+
2287  */
2288 static void disas_system(DisasContext *s, uint32_t insn)
2289 {
2290     unsigned int l, op0, op1, crn, crm, op2, rt;
2291     l = extract32(insn, 21, 1);
2292     op0 = extract32(insn, 19, 2);
2293     op1 = extract32(insn, 16, 3);
2294     crn = extract32(insn, 12, 4);
2295     crm = extract32(insn, 8, 4);
2296     op2 = extract32(insn, 5, 3);
2297     rt = extract32(insn, 0, 5);
2298 
2299     if (op0 == 0) {
2300         if (l || rt != 31) {
2301             unallocated_encoding(s);
2302             return;
2303         }
2304         switch (crn) {
2305         case 2: /* HINT (including allocated hints like NOP, YIELD, etc) */
2306             handle_hint(s, insn, op1, op2, crm);
2307             break;
2308         case 3: /* CLREX, DSB, DMB, ISB */
2309             handle_sync(s, insn, op1, op2, crm);
2310             break;
2311         case 4: /* MSR (immediate) */
2312             handle_msr_i(s, insn, op1, op2, crm);
2313             break;
2314         default:
2315             unallocated_encoding(s);
2316             break;
2317         }
2318         return;
2319     }
2320     handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
2321 }
2322 
2323 /* Exception generation
2324  *
2325  *  31             24 23 21 20                     5 4   2 1  0
2326  * +-----------------+-----+------------------------+-----+----+
2327  * | 1 1 0 1 0 1 0 0 | opc |          imm16         | op2 | LL |
2328  * +-----------------------+------------------------+----------+
2329  */
2330 static void disas_exc(DisasContext *s, uint32_t insn)
2331 {
2332     int opc = extract32(insn, 21, 3);
2333     int op2_ll = extract32(insn, 0, 5);
2334     int imm16 = extract32(insn, 5, 16);
2335     uint32_t syndrome;
2336 
2337     switch (opc) {
2338     case 0:
2339         /* For SVC, HVC and SMC we advance the single-step state
2340          * machine before taking the exception. This is architecturally
2341          * mandated, to ensure that single-stepping a system call
2342          * instruction works properly.
2343          */
2344         switch (op2_ll) {
2345         case 1:                                                     /* SVC */
2346             syndrome = syn_aa64_svc(imm16);
2347             if (s->fgt_svc) {
2348                 gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2349                 break;
2350             }
2351             gen_ss_advance(s);
2352             gen_exception_insn(s, 4, EXCP_SWI, syndrome);
2353             break;
2354         case 2:                                                     /* HVC */
2355             if (s->current_el == 0) {
2356                 unallocated_encoding(s);
2357                 break;
2358             }
2359             /* The pre HVC helper handles cases when HVC gets trapped
2360              * as an undefined insn by runtime configuration.
2361              */
2362             gen_a64_update_pc(s, 0);
2363             gen_helper_pre_hvc(cpu_env);
2364             gen_ss_advance(s);
2365             gen_exception_insn_el(s, 4, EXCP_HVC, syn_aa64_hvc(imm16), 2);
2366             break;
2367         case 3:                                                     /* SMC */
2368             if (s->current_el == 0) {
2369                 unallocated_encoding(s);
2370                 break;
2371             }
2372             gen_a64_update_pc(s, 0);
2373             gen_helper_pre_smc(cpu_env, tcg_constant_i32(syn_aa64_smc(imm16)));
2374             gen_ss_advance(s);
2375             gen_exception_insn_el(s, 4, EXCP_SMC, syn_aa64_smc(imm16), 3);
2376             break;
2377         default:
2378             unallocated_encoding(s);
2379             break;
2380         }
2381         break;
2382     case 1:
2383         if (op2_ll != 0) {
2384             unallocated_encoding(s);
2385             break;
2386         }
2387         /* BRK */
2388         gen_exception_bkpt_insn(s, syn_aa64_bkpt(imm16));
2389         break;
2390     case 2:
2391         if (op2_ll != 0) {
2392             unallocated_encoding(s);
2393             break;
2394         }
2395         /* HLT. This has two purposes.
2396          * Architecturally, it is an external halting debug instruction.
2397          * Since QEMU doesn't implement external debug, we treat this as
2398          * it is required for halting debug disabled: it will UNDEF.
2399          * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2400          */
2401         if (semihosting_enabled(s->current_el == 0) && imm16 == 0xf000) {
2402             gen_exception_internal_insn(s, EXCP_SEMIHOST);
2403         } else {
2404             unallocated_encoding(s);
2405         }
2406         break;
2407     case 5:
2408         if (op2_ll < 1 || op2_ll > 3) {
2409             unallocated_encoding(s);
2410             break;
2411         }
2412         /* DCPS1, DCPS2, DCPS3 */
2413         unallocated_encoding(s);
2414         break;
2415     default:
2416         unallocated_encoding(s);
2417         break;
2418     }
2419 }
2420 
2421 /* Branches, exception generating and system instructions */
2422 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
2423 {
2424     switch (extract32(insn, 25, 7)) {
2425     case 0x6a: /* Exception generation / System */
2426         if (insn & (1 << 24)) {
2427             if (extract32(insn, 22, 2) == 0) {
2428                 disas_system(s, insn);
2429             } else {
2430                 unallocated_encoding(s);
2431             }
2432         } else {
2433             disas_exc(s, insn);
2434         }
2435         break;
2436     default:
2437         unallocated_encoding(s);
2438         break;
2439     }
2440 }
2441 
2442 /*
2443  * Load/Store exclusive instructions are implemented by remembering
2444  * the value/address loaded, and seeing if these are the same
2445  * when the store is performed. This is not actually the architecturally
2446  * mandated semantics, but it works for typical guest code sequences
2447  * and avoids having to monitor regular stores.
2448  *
2449  * The store exclusive uses the atomic cmpxchg primitives to avoid
2450  * races in multi-threaded linux-user and when MTTCG softmmu is
2451  * enabled.
2452  */
2453 static void gen_load_exclusive(DisasContext *s, int rt, int rt2, int rn,
2454                                int size, bool is_pair)
2455 {
2456     int idx = get_mem_index(s);
2457     TCGv_i64 dirty_addr, clean_addr;
2458     MemOp memop = check_atomic_align(s, rn, size + is_pair);
2459 
2460     s->is_ldex = true;
2461     dirty_addr = cpu_reg_sp(s, rn);
2462     clean_addr = gen_mte_check1(s, dirty_addr, false, rn != 31, memop);
2463 
2464     g_assert(size <= 3);
2465     if (is_pair) {
2466         g_assert(size >= 2);
2467         if (size == 2) {
2468             tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2469             if (s->be_data == MO_LE) {
2470                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2471                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2472             } else {
2473                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2474                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2475             }
2476         } else {
2477             TCGv_i128 t16 = tcg_temp_new_i128();
2478 
2479             tcg_gen_qemu_ld_i128(t16, clean_addr, idx, memop);
2480 
2481             if (s->be_data == MO_LE) {
2482                 tcg_gen_extr_i128_i64(cpu_exclusive_val,
2483                                       cpu_exclusive_high, t16);
2484             } else {
2485                 tcg_gen_extr_i128_i64(cpu_exclusive_high,
2486                                       cpu_exclusive_val, t16);
2487             }
2488             tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2489             tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2490         }
2491     } else {
2492         tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2493         tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2494     }
2495     tcg_gen_mov_i64(cpu_exclusive_addr, clean_addr);
2496 }
2497 
2498 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2499                                 int rn, int size, int is_pair)
2500 {
2501     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2502      *     && (!is_pair || env->exclusive_high == [addr + datasize])) {
2503      *     [addr] = {Rt};
2504      *     if (is_pair) {
2505      *         [addr + datasize] = {Rt2};
2506      *     }
2507      *     {Rd} = 0;
2508      * } else {
2509      *     {Rd} = 1;
2510      * }
2511      * env->exclusive_addr = -1;
2512      */
2513     TCGLabel *fail_label = gen_new_label();
2514     TCGLabel *done_label = gen_new_label();
2515     TCGv_i64 tmp, dirty_addr, clean_addr;
2516     MemOp memop;
2517 
2518     memop = (size + is_pair) | MO_ALIGN;
2519     memop = finalize_memop(s, memop);
2520 
2521     dirty_addr = cpu_reg_sp(s, rn);
2522     clean_addr = gen_mte_check1(s, dirty_addr, true, rn != 31, memop);
2523 
2524     tcg_gen_brcond_i64(TCG_COND_NE, clean_addr, cpu_exclusive_addr, fail_label);
2525 
2526     tmp = tcg_temp_new_i64();
2527     if (is_pair) {
2528         if (size == 2) {
2529             if (s->be_data == MO_LE) {
2530                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2531             } else {
2532                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2533             }
2534             tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2535                                        cpu_exclusive_val, tmp,
2536                                        get_mem_index(s), memop);
2537             tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2538         } else {
2539             TCGv_i128 t16 = tcg_temp_new_i128();
2540             TCGv_i128 c16 = tcg_temp_new_i128();
2541             TCGv_i64 a, b;
2542 
2543             if (s->be_data == MO_LE) {
2544                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt), cpu_reg(s, rt2));
2545                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_val,
2546                                         cpu_exclusive_high);
2547             } else {
2548                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt2), cpu_reg(s, rt));
2549                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_high,
2550                                         cpu_exclusive_val);
2551             }
2552 
2553             tcg_gen_atomic_cmpxchg_i128(t16, cpu_exclusive_addr, c16, t16,
2554                                         get_mem_index(s), memop);
2555 
2556             a = tcg_temp_new_i64();
2557             b = tcg_temp_new_i64();
2558             if (s->be_data == MO_LE) {
2559                 tcg_gen_extr_i128_i64(a, b, t16);
2560             } else {
2561                 tcg_gen_extr_i128_i64(b, a, t16);
2562             }
2563 
2564             tcg_gen_xor_i64(a, a, cpu_exclusive_val);
2565             tcg_gen_xor_i64(b, b, cpu_exclusive_high);
2566             tcg_gen_or_i64(tmp, a, b);
2567 
2568             tcg_gen_setcondi_i64(TCG_COND_NE, tmp, tmp, 0);
2569         }
2570     } else {
2571         tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2572                                    cpu_reg(s, rt), get_mem_index(s), memop);
2573         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2574     }
2575     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2576     tcg_gen_br(done_label);
2577 
2578     gen_set_label(fail_label);
2579     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2580     gen_set_label(done_label);
2581     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2582 }
2583 
2584 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2585                                  int rn, int size)
2586 {
2587     TCGv_i64 tcg_rs = cpu_reg(s, rs);
2588     TCGv_i64 tcg_rt = cpu_reg(s, rt);
2589     int memidx = get_mem_index(s);
2590     TCGv_i64 clean_addr;
2591     MemOp memop;
2592 
2593     if (rn == 31) {
2594         gen_check_sp_alignment(s);
2595     }
2596     memop = check_atomic_align(s, rn, size);
2597     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2598     tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt,
2599                                memidx, memop);
2600 }
2601 
2602 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2603                                       int rn, int size)
2604 {
2605     TCGv_i64 s1 = cpu_reg(s, rs);
2606     TCGv_i64 s2 = cpu_reg(s, rs + 1);
2607     TCGv_i64 t1 = cpu_reg(s, rt);
2608     TCGv_i64 t2 = cpu_reg(s, rt + 1);
2609     TCGv_i64 clean_addr;
2610     int memidx = get_mem_index(s);
2611     MemOp memop;
2612 
2613     if (rn == 31) {
2614         gen_check_sp_alignment(s);
2615     }
2616 
2617     /* This is a single atomic access, despite the "pair". */
2618     memop = check_atomic_align(s, rn, size + 1);
2619     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, memop);
2620 
2621     if (size == 2) {
2622         TCGv_i64 cmp = tcg_temp_new_i64();
2623         TCGv_i64 val = tcg_temp_new_i64();
2624 
2625         if (s->be_data == MO_LE) {
2626             tcg_gen_concat32_i64(val, t1, t2);
2627             tcg_gen_concat32_i64(cmp, s1, s2);
2628         } else {
2629             tcg_gen_concat32_i64(val, t2, t1);
2630             tcg_gen_concat32_i64(cmp, s2, s1);
2631         }
2632 
2633         tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx, memop);
2634 
2635         if (s->be_data == MO_LE) {
2636             tcg_gen_extr32_i64(s1, s2, cmp);
2637         } else {
2638             tcg_gen_extr32_i64(s2, s1, cmp);
2639         }
2640     } else {
2641         TCGv_i128 cmp = tcg_temp_new_i128();
2642         TCGv_i128 val = tcg_temp_new_i128();
2643 
2644         if (s->be_data == MO_LE) {
2645             tcg_gen_concat_i64_i128(val, t1, t2);
2646             tcg_gen_concat_i64_i128(cmp, s1, s2);
2647         } else {
2648             tcg_gen_concat_i64_i128(val, t2, t1);
2649             tcg_gen_concat_i64_i128(cmp, s2, s1);
2650         }
2651 
2652         tcg_gen_atomic_cmpxchg_i128(cmp, clean_addr, cmp, val, memidx, memop);
2653 
2654         if (s->be_data == MO_LE) {
2655             tcg_gen_extr_i128_i64(s1, s2, cmp);
2656         } else {
2657             tcg_gen_extr_i128_i64(s2, s1, cmp);
2658         }
2659     }
2660 }
2661 
2662 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
2663  * from the ARMv8 specs for LDR (Shared decode for all encodings).
2664  */
2665 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2666 {
2667     int opc0 = extract32(opc, 0, 1);
2668     int regsize;
2669 
2670     if (is_signed) {
2671         regsize = opc0 ? 32 : 64;
2672     } else {
2673         regsize = size == 3 ? 64 : 32;
2674     }
2675     return regsize == 64;
2676 }
2677 
2678 /* Load/store exclusive
2679  *
2680  *  31 30 29         24  23  22   21  20  16  15  14   10 9    5 4    0
2681  * +-----+-------------+----+---+----+------+----+-------+------+------+
2682  * | sz  | 0 0 1 0 0 0 | o2 | L | o1 |  Rs  | o0 |  Rt2  |  Rn  | Rt   |
2683  * +-----+-------------+----+---+----+------+----+-------+------+------+
2684  *
2685  *  sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2686  *   L: 0 -> store, 1 -> load
2687  *  o2: 0 -> exclusive, 1 -> not
2688  *  o1: 0 -> single register, 1 -> register pair
2689  *  o0: 1 -> load-acquire/store-release, 0 -> not
2690  */
2691 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2692 {
2693     int rt = extract32(insn, 0, 5);
2694     int rn = extract32(insn, 5, 5);
2695     int rt2 = extract32(insn, 10, 5);
2696     int rs = extract32(insn, 16, 5);
2697     int is_lasr = extract32(insn, 15, 1);
2698     int o2_L_o1_o0 = extract32(insn, 21, 3) * 2 | is_lasr;
2699     int size = extract32(insn, 30, 2);
2700     TCGv_i64 clean_addr;
2701     MemOp memop;
2702 
2703     switch (o2_L_o1_o0) {
2704     case 0x0: /* STXR */
2705     case 0x1: /* STLXR */
2706         if (rn == 31) {
2707             gen_check_sp_alignment(s);
2708         }
2709         if (is_lasr) {
2710             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2711         }
2712         gen_store_exclusive(s, rs, rt, rt2, rn, size, false);
2713         return;
2714 
2715     case 0x4: /* LDXR */
2716     case 0x5: /* LDAXR */
2717         if (rn == 31) {
2718             gen_check_sp_alignment(s);
2719         }
2720         gen_load_exclusive(s, rt, rt2, rn, size, false);
2721         if (is_lasr) {
2722             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2723         }
2724         return;
2725 
2726     case 0x8: /* STLLR */
2727         if (!dc_isar_feature(aa64_lor, s)) {
2728             break;
2729         }
2730         /* StoreLORelease is the same as Store-Release for QEMU.  */
2731         /* fall through */
2732     case 0x9: /* STLR */
2733         /* Generate ISS for non-exclusive accesses including LASR.  */
2734         if (rn == 31) {
2735             gen_check_sp_alignment(s);
2736         }
2737         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2738         memop = check_ordered_align(s, rn, 0, true, size);
2739         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2740                                     true, rn != 31, memop);
2741         do_gpr_st(s, cpu_reg(s, rt), clean_addr, memop, true, rt,
2742                   disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2743         return;
2744 
2745     case 0xc: /* LDLAR */
2746         if (!dc_isar_feature(aa64_lor, s)) {
2747             break;
2748         }
2749         /* LoadLOAcquire is the same as Load-Acquire for QEMU.  */
2750         /* fall through */
2751     case 0xd: /* LDAR */
2752         /* Generate ISS for non-exclusive accesses including LASR.  */
2753         if (rn == 31) {
2754             gen_check_sp_alignment(s);
2755         }
2756         memop = check_ordered_align(s, rn, 0, false, size);
2757         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2758                                     false, rn != 31, memop);
2759         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, memop, false, true,
2760                   rt, disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2761         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2762         return;
2763 
2764     case 0x2: case 0x3: /* CASP / STXP */
2765         if (size & 2) { /* STXP / STLXP */
2766             if (rn == 31) {
2767                 gen_check_sp_alignment(s);
2768             }
2769             if (is_lasr) {
2770                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2771             }
2772             gen_store_exclusive(s, rs, rt, rt2, rn, size, true);
2773             return;
2774         }
2775         if (rt2 == 31
2776             && ((rt | rs) & 1) == 0
2777             && dc_isar_feature(aa64_atomics, s)) {
2778             /* CASP / CASPL */
2779             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2780             return;
2781         }
2782         break;
2783 
2784     case 0x6: case 0x7: /* CASPA / LDXP */
2785         if (size & 2) { /* LDXP / LDAXP */
2786             if (rn == 31) {
2787                 gen_check_sp_alignment(s);
2788             }
2789             gen_load_exclusive(s, rt, rt2, rn, size, true);
2790             if (is_lasr) {
2791                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2792             }
2793             return;
2794         }
2795         if (rt2 == 31
2796             && ((rt | rs) & 1) == 0
2797             && dc_isar_feature(aa64_atomics, s)) {
2798             /* CASPA / CASPAL */
2799             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2800             return;
2801         }
2802         break;
2803 
2804     case 0xa: /* CAS */
2805     case 0xb: /* CASL */
2806     case 0xe: /* CASA */
2807     case 0xf: /* CASAL */
2808         if (rt2 == 31 && dc_isar_feature(aa64_atomics, s)) {
2809             gen_compare_and_swap(s, rs, rt, rn, size);
2810             return;
2811         }
2812         break;
2813     }
2814     unallocated_encoding(s);
2815 }
2816 
2817 /*
2818  * Load register (literal)
2819  *
2820  *  31 30 29   27  26 25 24 23                5 4     0
2821  * +-----+-------+---+-----+-------------------+-------+
2822  * | opc | 0 1 1 | V | 0 0 |     imm19         |  Rt   |
2823  * +-----+-------+---+-----+-------------------+-------+
2824  *
2825  * V: 1 -> vector (simd/fp)
2826  * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2827  *                   10-> 32 bit signed, 11 -> prefetch
2828  * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2829  */
2830 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2831 {
2832     int rt = extract32(insn, 0, 5);
2833     int64_t imm = sextract32(insn, 5, 19) << 2;
2834     bool is_vector = extract32(insn, 26, 1);
2835     int opc = extract32(insn, 30, 2);
2836     bool is_signed = false;
2837     int size = 2;
2838     TCGv_i64 tcg_rt, clean_addr;
2839     MemOp memop;
2840 
2841     if (is_vector) {
2842         if (opc == 3) {
2843             unallocated_encoding(s);
2844             return;
2845         }
2846         size = 2 + opc;
2847         if (!fp_access_check(s)) {
2848             return;
2849         }
2850         memop = finalize_memop_asimd(s, size);
2851     } else {
2852         if (opc == 3) {
2853             /* PRFM (literal) : prefetch */
2854             return;
2855         }
2856         size = 2 + extract32(opc, 0, 1);
2857         is_signed = extract32(opc, 1, 1);
2858         memop = finalize_memop(s, size + is_signed * MO_SIGN);
2859     }
2860 
2861     tcg_rt = cpu_reg(s, rt);
2862 
2863     clean_addr = tcg_temp_new_i64();
2864     gen_pc_plus_diff(s, clean_addr, imm);
2865 
2866     if (is_vector) {
2867         do_fp_ld(s, rt, clean_addr, memop);
2868     } else {
2869         /* Only unsigned 32bit loads target 32bit registers.  */
2870         bool iss_sf = opc != 0;
2871         do_gpr_ld(s, tcg_rt, clean_addr, memop, false, true, rt, iss_sf, false);
2872     }
2873 }
2874 
2875 /*
2876  * LDNP (Load Pair - non-temporal hint)
2877  * LDP (Load Pair - non vector)
2878  * LDPSW (Load Pair Signed Word - non vector)
2879  * STNP (Store Pair - non-temporal hint)
2880  * STP (Store Pair - non vector)
2881  * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2882  * LDP (Load Pair of SIMD&FP)
2883  * STNP (Store Pair of SIMD&FP - non-temporal hint)
2884  * STP (Store Pair of SIMD&FP)
2885  *
2886  *  31 30 29   27  26  25 24   23  22 21   15 14   10 9    5 4    0
2887  * +-----+-------+---+---+-------+---+-----------------------------+
2888  * | opc | 1 0 1 | V | 0 | index | L |  imm7 |  Rt2  |  Rn  | Rt   |
2889  * +-----+-------+---+---+-------+---+-------+-------+------+------+
2890  *
2891  * opc: LDP/STP/LDNP/STNP        00 -> 32 bit, 10 -> 64 bit
2892  *      LDPSW/STGP               01
2893  *      LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2894  *   V: 0 -> GPR, 1 -> Vector
2895  * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2896  *      10 -> signed offset, 11 -> pre-index
2897  *   L: 0 -> Store 1 -> Load
2898  *
2899  * Rt, Rt2 = GPR or SIMD registers to be stored
2900  * Rn = general purpose register containing address
2901  * imm7 = signed offset (multiple of 4 or 8 depending on size)
2902  */
2903 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2904 {
2905     int rt = extract32(insn, 0, 5);
2906     int rn = extract32(insn, 5, 5);
2907     int rt2 = extract32(insn, 10, 5);
2908     uint64_t offset = sextract64(insn, 15, 7);
2909     int index = extract32(insn, 23, 2);
2910     bool is_vector = extract32(insn, 26, 1);
2911     bool is_load = extract32(insn, 22, 1);
2912     int opc = extract32(insn, 30, 2);
2913     bool is_signed = false;
2914     bool postindex = false;
2915     bool wback = false;
2916     bool set_tag = false;
2917     TCGv_i64 clean_addr, dirty_addr;
2918     MemOp mop;
2919     int size;
2920 
2921     if (opc == 3) {
2922         unallocated_encoding(s);
2923         return;
2924     }
2925 
2926     if (is_vector) {
2927         size = 2 + opc;
2928     } else if (opc == 1 && !is_load) {
2929         /* STGP */
2930         if (!dc_isar_feature(aa64_mte_insn_reg, s) || index == 0) {
2931             unallocated_encoding(s);
2932             return;
2933         }
2934         size = 3;
2935         set_tag = true;
2936     } else {
2937         size = 2 + extract32(opc, 1, 1);
2938         is_signed = extract32(opc, 0, 1);
2939         if (!is_load && is_signed) {
2940             unallocated_encoding(s);
2941             return;
2942         }
2943     }
2944 
2945     switch (index) {
2946     case 1: /* post-index */
2947         postindex = true;
2948         wback = true;
2949         break;
2950     case 0:
2951         /* signed offset with "non-temporal" hint. Since we don't emulate
2952          * caches we don't care about hints to the cache system about
2953          * data access patterns, and handle this identically to plain
2954          * signed offset.
2955          */
2956         if (is_signed) {
2957             /* There is no non-temporal-hint version of LDPSW */
2958             unallocated_encoding(s);
2959             return;
2960         }
2961         postindex = false;
2962         break;
2963     case 2: /* signed offset, rn not updated */
2964         postindex = false;
2965         break;
2966     case 3: /* pre-index */
2967         postindex = false;
2968         wback = true;
2969         break;
2970     }
2971 
2972     if (is_vector && !fp_access_check(s)) {
2973         return;
2974     }
2975 
2976     offset <<= (set_tag ? LOG2_TAG_GRANULE : size);
2977 
2978     if (rn == 31) {
2979         gen_check_sp_alignment(s);
2980     }
2981 
2982     dirty_addr = read_cpu_reg_sp(s, rn, 1);
2983     if (!postindex) {
2984         tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2985     }
2986 
2987     if (set_tag) {
2988         if (!s->ata) {
2989             /*
2990              * TODO: We could rely on the stores below, at least for
2991              * system mode, if we arrange to add MO_ALIGN_16.
2992              */
2993             gen_helper_stg_stub(cpu_env, dirty_addr);
2994         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2995             gen_helper_stg_parallel(cpu_env, dirty_addr, dirty_addr);
2996         } else {
2997             gen_helper_stg(cpu_env, dirty_addr, dirty_addr);
2998         }
2999     }
3000 
3001     if (is_vector) {
3002         mop = finalize_memop_asimd(s, size);
3003     } else {
3004         mop = finalize_memop(s, size);
3005     }
3006     clean_addr = gen_mte_checkN(s, dirty_addr, !is_load,
3007                                 (wback || rn != 31) && !set_tag,
3008                                 2 << size, mop);
3009 
3010     if (is_vector) {
3011         /* LSE2 does not merge FP pairs; leave these as separate operations. */
3012         if (is_load) {
3013             do_fp_ld(s, rt, clean_addr, mop);
3014         } else {
3015             do_fp_st(s, rt, clean_addr, mop);
3016         }
3017         tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
3018         if (is_load) {
3019             do_fp_ld(s, rt2, clean_addr, mop);
3020         } else {
3021             do_fp_st(s, rt2, clean_addr, mop);
3022         }
3023     } else {
3024         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3025         TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
3026 
3027         /*
3028          * We built mop above for the single logical access -- rebuild it
3029          * now for the paired operation.
3030          *
3031          * With LSE2, non-sign-extending pairs are treated atomically if
3032          * aligned, and if unaligned one of the pair will be completely
3033          * within a 16-byte block and that element will be atomic.
3034          * Otherwise each element is separately atomic.
3035          * In all cases, issue one operation with the correct atomicity.
3036          *
3037          * This treats sign-extending loads like zero-extending loads,
3038          * since that reuses the most code below.
3039          */
3040         mop = size + 1;
3041         if (s->align_mem) {
3042             mop |= (size == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
3043         }
3044         mop = finalize_memop_pair(s, mop);
3045 
3046         if (is_load) {
3047             if (size == 2) {
3048                 int o2 = s->be_data == MO_LE ? 32 : 0;
3049                 int o1 = o2 ^ 32;
3050 
3051                 tcg_gen_qemu_ld_i64(tcg_rt, clean_addr, get_mem_index(s), mop);
3052                 if (is_signed) {
3053                     tcg_gen_sextract_i64(tcg_rt2, tcg_rt, o2, 32);
3054                     tcg_gen_sextract_i64(tcg_rt, tcg_rt, o1, 32);
3055                 } else {
3056                     tcg_gen_extract_i64(tcg_rt2, tcg_rt, o2, 32);
3057                     tcg_gen_extract_i64(tcg_rt, tcg_rt, o1, 32);
3058                 }
3059             } else {
3060                 TCGv_i128 tmp = tcg_temp_new_i128();
3061 
3062                 tcg_gen_qemu_ld_i128(tmp, clean_addr, get_mem_index(s), mop);
3063                 if (s->be_data == MO_LE) {
3064                     tcg_gen_extr_i128_i64(tcg_rt, tcg_rt2, tmp);
3065                 } else {
3066                     tcg_gen_extr_i128_i64(tcg_rt2, tcg_rt, tmp);
3067                 }
3068             }
3069         } else {
3070             if (size == 2) {
3071                 TCGv_i64 tmp = tcg_temp_new_i64();
3072 
3073                 if (s->be_data == MO_LE) {
3074                     tcg_gen_concat32_i64(tmp, tcg_rt, tcg_rt2);
3075                 } else {
3076                     tcg_gen_concat32_i64(tmp, tcg_rt2, tcg_rt);
3077                 }
3078                 tcg_gen_qemu_st_i64(tmp, clean_addr, get_mem_index(s), mop);
3079             } else {
3080                 TCGv_i128 tmp = tcg_temp_new_i128();
3081 
3082                 if (s->be_data == MO_LE) {
3083                     tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
3084                 } else {
3085                     tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
3086                 }
3087                 tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
3088             }
3089         }
3090     }
3091 
3092     if (wback) {
3093         if (postindex) {
3094             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3095         }
3096         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3097     }
3098 }
3099 
3100 /*
3101  * Load/store (immediate post-indexed)
3102  * Load/store (immediate pre-indexed)
3103  * Load/store (unscaled immediate)
3104  *
3105  * 31 30 29   27  26 25 24 23 22 21  20    12 11 10 9    5 4    0
3106  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3107  * |size| 1 1 1 | V | 0 0 | opc | 0 |  imm9  | idx |  Rn  |  Rt  |
3108  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3109  *
3110  * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
3111          10 -> unprivileged
3112  * V = 0 -> non-vector
3113  * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
3114  * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3115  */
3116 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
3117                                 int opc,
3118                                 int size,
3119                                 int rt,
3120                                 bool is_vector)
3121 {
3122     int rn = extract32(insn, 5, 5);
3123     int imm9 = sextract32(insn, 12, 9);
3124     int idx = extract32(insn, 10, 2);
3125     bool is_signed = false;
3126     bool is_store = false;
3127     bool is_extended = false;
3128     bool is_unpriv = (idx == 2);
3129     bool iss_valid;
3130     bool post_index;
3131     bool writeback;
3132     int memidx;
3133     MemOp memop;
3134     TCGv_i64 clean_addr, dirty_addr;
3135 
3136     if (is_vector) {
3137         size |= (opc & 2) << 1;
3138         if (size > 4 || is_unpriv) {
3139             unallocated_encoding(s);
3140             return;
3141         }
3142         is_store = ((opc & 1) == 0);
3143         if (!fp_access_check(s)) {
3144             return;
3145         }
3146         memop = finalize_memop_asimd(s, size);
3147     } else {
3148         if (size == 3 && opc == 2) {
3149             /* PRFM - prefetch */
3150             if (idx != 0) {
3151                 unallocated_encoding(s);
3152                 return;
3153             }
3154             return;
3155         }
3156         if (opc == 3 && size > 1) {
3157             unallocated_encoding(s);
3158             return;
3159         }
3160         is_store = (opc == 0);
3161         is_signed = !is_store && extract32(opc, 1, 1);
3162         is_extended = (size < 3) && extract32(opc, 0, 1);
3163         memop = finalize_memop(s, size + is_signed * MO_SIGN);
3164     }
3165 
3166     switch (idx) {
3167     case 0:
3168     case 2:
3169         post_index = false;
3170         writeback = false;
3171         break;
3172     case 1:
3173         post_index = true;
3174         writeback = true;
3175         break;
3176     case 3:
3177         post_index = false;
3178         writeback = true;
3179         break;
3180     default:
3181         g_assert_not_reached();
3182     }
3183 
3184     iss_valid = !is_vector && !writeback;
3185 
3186     if (rn == 31) {
3187         gen_check_sp_alignment(s);
3188     }
3189 
3190     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3191     if (!post_index) {
3192         tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3193     }
3194 
3195     memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
3196 
3197     clean_addr = gen_mte_check1_mmuidx(s, dirty_addr, is_store,
3198                                        writeback || rn != 31,
3199                                        size, is_unpriv, memidx);
3200 
3201     if (is_vector) {
3202         if (is_store) {
3203             do_fp_st(s, rt, clean_addr, memop);
3204         } else {
3205             do_fp_ld(s, rt, clean_addr, memop);
3206         }
3207     } else {
3208         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3209         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3210 
3211         if (is_store) {
3212             do_gpr_st_memidx(s, tcg_rt, clean_addr, memop, memidx,
3213                              iss_valid, rt, iss_sf, false);
3214         } else {
3215             do_gpr_ld_memidx(s, tcg_rt, clean_addr, memop,
3216                              is_extended, memidx,
3217                              iss_valid, rt, iss_sf, false);
3218         }
3219     }
3220 
3221     if (writeback) {
3222         TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3223         if (post_index) {
3224             tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3225         }
3226         tcg_gen_mov_i64(tcg_rn, dirty_addr);
3227     }
3228 }
3229 
3230 /*
3231  * Load/store (register offset)
3232  *
3233  * 31 30 29   27  26 25 24 23 22 21  20  16 15 13 12 11 10 9  5 4  0
3234  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3235  * |size| 1 1 1 | V | 0 0 | opc | 1 |  Rm  | opt | S| 1 0 | Rn | Rt |
3236  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3237  *
3238  * For non-vector:
3239  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3240  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3241  * For vector:
3242  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3243  *   opc<0>: 0 -> store, 1 -> load
3244  * V: 1 -> vector/simd
3245  * opt: extend encoding (see DecodeRegExtend)
3246  * S: if S=1 then scale (essentially index by sizeof(size))
3247  * Rt: register to transfer into/out of
3248  * Rn: address register or SP for base
3249  * Rm: offset register or ZR for offset
3250  */
3251 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
3252                                    int opc,
3253                                    int size,
3254                                    int rt,
3255                                    bool is_vector)
3256 {
3257     int rn = extract32(insn, 5, 5);
3258     int shift = extract32(insn, 12, 1);
3259     int rm = extract32(insn, 16, 5);
3260     int opt = extract32(insn, 13, 3);
3261     bool is_signed = false;
3262     bool is_store = false;
3263     bool is_extended = false;
3264     TCGv_i64 tcg_rm, clean_addr, dirty_addr;
3265     MemOp memop;
3266 
3267     if (extract32(opt, 1, 1) == 0) {
3268         unallocated_encoding(s);
3269         return;
3270     }
3271 
3272     if (is_vector) {
3273         size |= (opc & 2) << 1;
3274         if (size > 4) {
3275             unallocated_encoding(s);
3276             return;
3277         }
3278         is_store = !extract32(opc, 0, 1);
3279         if (!fp_access_check(s)) {
3280             return;
3281         }
3282     } else {
3283         if (size == 3 && opc == 2) {
3284             /* PRFM - prefetch */
3285             return;
3286         }
3287         if (opc == 3 && size > 1) {
3288             unallocated_encoding(s);
3289             return;
3290         }
3291         is_store = (opc == 0);
3292         is_signed = !is_store && extract32(opc, 1, 1);
3293         is_extended = (size < 3) && extract32(opc, 0, 1);
3294     }
3295 
3296     if (rn == 31) {
3297         gen_check_sp_alignment(s);
3298     }
3299     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3300 
3301     tcg_rm = read_cpu_reg(s, rm, 1);
3302     ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
3303 
3304     tcg_gen_add_i64(dirty_addr, dirty_addr, tcg_rm);
3305 
3306     memop = finalize_memop(s, size + is_signed * MO_SIGN);
3307     clean_addr = gen_mte_check1(s, dirty_addr, is_store, true, memop);
3308 
3309     if (is_vector) {
3310         if (is_store) {
3311             do_fp_st(s, rt, clean_addr, memop);
3312         } else {
3313             do_fp_ld(s, rt, clean_addr, memop);
3314         }
3315     } else {
3316         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3317         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3318 
3319         if (is_store) {
3320             do_gpr_st(s, tcg_rt, clean_addr, memop,
3321                       true, rt, iss_sf, false);
3322         } else {
3323             do_gpr_ld(s, tcg_rt, clean_addr, memop,
3324                       is_extended, true, rt, iss_sf, false);
3325         }
3326     }
3327 }
3328 
3329 /*
3330  * Load/store (unsigned immediate)
3331  *
3332  * 31 30 29   27  26 25 24 23 22 21        10 9     5
3333  * +----+-------+---+-----+-----+------------+-------+------+
3334  * |size| 1 1 1 | V | 0 1 | opc |   imm12    |  Rn   |  Rt  |
3335  * +----+-------+---+-----+-----+------------+-------+------+
3336  *
3337  * For non-vector:
3338  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3339  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3340  * For vector:
3341  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3342  *   opc<0>: 0 -> store, 1 -> load
3343  * Rn: base address register (inc SP)
3344  * Rt: target register
3345  */
3346 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
3347                                         int opc,
3348                                         int size,
3349                                         int rt,
3350                                         bool is_vector)
3351 {
3352     int rn = extract32(insn, 5, 5);
3353     unsigned int imm12 = extract32(insn, 10, 12);
3354     unsigned int offset;
3355     TCGv_i64 clean_addr, dirty_addr;
3356     bool is_store;
3357     bool is_signed = false;
3358     bool is_extended = false;
3359     MemOp memop;
3360 
3361     if (is_vector) {
3362         size |= (opc & 2) << 1;
3363         if (size > 4) {
3364             unallocated_encoding(s);
3365             return;
3366         }
3367         is_store = !extract32(opc, 0, 1);
3368         if (!fp_access_check(s)) {
3369             return;
3370         }
3371     } else {
3372         if (size == 3 && opc == 2) {
3373             /* PRFM - prefetch */
3374             return;
3375         }
3376         if (opc == 3 && size > 1) {
3377             unallocated_encoding(s);
3378             return;
3379         }
3380         is_store = (opc == 0);
3381         is_signed = !is_store && extract32(opc, 1, 1);
3382         is_extended = (size < 3) && extract32(opc, 0, 1);
3383     }
3384 
3385     if (rn == 31) {
3386         gen_check_sp_alignment(s);
3387     }
3388     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3389     offset = imm12 << size;
3390     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3391 
3392     memop = finalize_memop(s, size + is_signed * MO_SIGN);
3393     clean_addr = gen_mte_check1(s, dirty_addr, is_store, rn != 31, memop);
3394 
3395     if (is_vector) {
3396         if (is_store) {
3397             do_fp_st(s, rt, clean_addr, memop);
3398         } else {
3399             do_fp_ld(s, rt, clean_addr, memop);
3400         }
3401     } else {
3402         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3403         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3404         if (is_store) {
3405             do_gpr_st(s, tcg_rt, clean_addr, memop, true, rt, iss_sf, false);
3406         } else {
3407             do_gpr_ld(s, tcg_rt, clean_addr, memop,
3408                       is_extended, true, rt, iss_sf, false);
3409         }
3410     }
3411 }
3412 
3413 /* Atomic memory operations
3414  *
3415  *  31  30      27  26    24    22  21   16   15    12    10    5     0
3416  * +------+-------+---+-----+-----+---+----+----+-----+-----+----+-----+
3417  * | size | 1 1 1 | V | 0 0 | A R | 1 | Rs | o3 | opc | 0 0 | Rn |  Rt |
3418  * +------+-------+---+-----+-----+--------+----+-----+-----+----+-----+
3419  *
3420  * Rt: the result register
3421  * Rn: base address or SP
3422  * Rs: the source register for the operation
3423  * V: vector flag (always 0 as of v8.3)
3424  * A: acquire flag
3425  * R: release flag
3426  */
3427 static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
3428                               int size, int rt, bool is_vector)
3429 {
3430     int rs = extract32(insn, 16, 5);
3431     int rn = extract32(insn, 5, 5);
3432     int o3_opc = extract32(insn, 12, 4);
3433     bool r = extract32(insn, 22, 1);
3434     bool a = extract32(insn, 23, 1);
3435     TCGv_i64 tcg_rs, tcg_rt, clean_addr;
3436     AtomicThreeOpFn *fn = NULL;
3437     MemOp mop = size;
3438 
3439     if (is_vector || !dc_isar_feature(aa64_atomics, s)) {
3440         unallocated_encoding(s);
3441         return;
3442     }
3443     switch (o3_opc) {
3444     case 000: /* LDADD */
3445         fn = tcg_gen_atomic_fetch_add_i64;
3446         break;
3447     case 001: /* LDCLR */
3448         fn = tcg_gen_atomic_fetch_and_i64;
3449         break;
3450     case 002: /* LDEOR */
3451         fn = tcg_gen_atomic_fetch_xor_i64;
3452         break;
3453     case 003: /* LDSET */
3454         fn = tcg_gen_atomic_fetch_or_i64;
3455         break;
3456     case 004: /* LDSMAX */
3457         fn = tcg_gen_atomic_fetch_smax_i64;
3458         mop |= MO_SIGN;
3459         break;
3460     case 005: /* LDSMIN */
3461         fn = tcg_gen_atomic_fetch_smin_i64;
3462         mop |= MO_SIGN;
3463         break;
3464     case 006: /* LDUMAX */
3465         fn = tcg_gen_atomic_fetch_umax_i64;
3466         break;
3467     case 007: /* LDUMIN */
3468         fn = tcg_gen_atomic_fetch_umin_i64;
3469         break;
3470     case 010: /* SWP */
3471         fn = tcg_gen_atomic_xchg_i64;
3472         break;
3473     case 014: /* LDAPR, LDAPRH, LDAPRB */
3474         if (!dc_isar_feature(aa64_rcpc_8_3, s) ||
3475             rs != 31 || a != 1 || r != 0) {
3476             unallocated_encoding(s);
3477             return;
3478         }
3479         break;
3480     default:
3481         unallocated_encoding(s);
3482         return;
3483     }
3484 
3485     if (rn == 31) {
3486         gen_check_sp_alignment(s);
3487     }
3488 
3489     mop = check_atomic_align(s, rn, mop);
3490     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), false, rn != 31, mop);
3491 
3492     if (o3_opc == 014) {
3493         /*
3494          * LDAPR* are a special case because they are a simple load, not a
3495          * fetch-and-do-something op.
3496          * The architectural consistency requirements here are weaker than
3497          * full load-acquire (we only need "load-acquire processor consistent"),
3498          * but we choose to implement them as full LDAQ.
3499          */
3500         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop, false,
3501                   true, rt, disas_ldst_compute_iss_sf(size, false, 0), true);
3502         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3503         return;
3504     }
3505 
3506     tcg_rs = read_cpu_reg(s, rs, true);
3507     tcg_rt = cpu_reg(s, rt);
3508 
3509     if (o3_opc == 1) { /* LDCLR */
3510         tcg_gen_not_i64(tcg_rs, tcg_rs);
3511     }
3512 
3513     /* The tcg atomic primitives are all full barriers.  Therefore we
3514      * can ignore the Acquire and Release bits of this instruction.
3515      */
3516     fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3517 
3518     if ((mop & MO_SIGN) && size != MO_64) {
3519         tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3520     }
3521 }
3522 
3523 /*
3524  * PAC memory operations
3525  *
3526  *  31  30      27  26    24    22  21       12  11  10    5     0
3527  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3528  * | size | 1 1 1 | V | 0 0 | M S | 1 |  imm9  | W | 1 | Rn |  Rt |
3529  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3530  *
3531  * Rt: the result register
3532  * Rn: base address or SP
3533  * V: vector flag (always 0 as of v8.3)
3534  * M: clear for key DA, set for key DB
3535  * W: pre-indexing flag
3536  * S: sign for imm9.
3537  */
3538 static void disas_ldst_pac(DisasContext *s, uint32_t insn,
3539                            int size, int rt, bool is_vector)
3540 {
3541     int rn = extract32(insn, 5, 5);
3542     bool is_wback = extract32(insn, 11, 1);
3543     bool use_key_a = !extract32(insn, 23, 1);
3544     int offset;
3545     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3546     MemOp memop;
3547 
3548     if (size != 3 || is_vector || !dc_isar_feature(aa64_pauth, s)) {
3549         unallocated_encoding(s);
3550         return;
3551     }
3552 
3553     if (rn == 31) {
3554         gen_check_sp_alignment(s);
3555     }
3556     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3557 
3558     if (s->pauth_active) {
3559         if (use_key_a) {
3560             gen_helper_autda(dirty_addr, cpu_env, dirty_addr,
3561                              tcg_constant_i64(0));
3562         } else {
3563             gen_helper_autdb(dirty_addr, cpu_env, dirty_addr,
3564                              tcg_constant_i64(0));
3565         }
3566     }
3567 
3568     /* Form the 10-bit signed, scaled offset.  */
3569     offset = (extract32(insn, 22, 1) << 9) | extract32(insn, 12, 9);
3570     offset = sextract32(offset << size, 0, 10 + size);
3571     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3572 
3573     memop = finalize_memop(s, size);
3574 
3575     /* Note that "clean" and "dirty" here refer to TBI not PAC.  */
3576     clean_addr = gen_mte_check1(s, dirty_addr, false,
3577                                 is_wback || rn != 31, memop);
3578 
3579     tcg_rt = cpu_reg(s, rt);
3580     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3581               /* extend */ false, /* iss_valid */ !is_wback,
3582               /* iss_srt */ rt, /* iss_sf */ true, /* iss_ar */ false);
3583 
3584     if (is_wback) {
3585         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3586     }
3587 }
3588 
3589 /*
3590  * LDAPR/STLR (unscaled immediate)
3591  *
3592  *  31  30            24    22  21       12    10    5     0
3593  * +------+-------------+-----+---+--------+-----+----+-----+
3594  * | size | 0 1 1 0 0 1 | opc | 0 |  imm9  | 0 0 | Rn |  Rt |
3595  * +------+-------------+-----+---+--------+-----+----+-----+
3596  *
3597  * Rt: source or destination register
3598  * Rn: base register
3599  * imm9: unscaled immediate offset
3600  * opc: 00: STLUR*, 01/10/11: various LDAPUR*
3601  * size: size of load/store
3602  */
3603 static void disas_ldst_ldapr_stlr(DisasContext *s, uint32_t insn)
3604 {
3605     int rt = extract32(insn, 0, 5);
3606     int rn = extract32(insn, 5, 5);
3607     int offset = sextract32(insn, 12, 9);
3608     int opc = extract32(insn, 22, 2);
3609     int size = extract32(insn, 30, 2);
3610     TCGv_i64 clean_addr, dirty_addr;
3611     bool is_store = false;
3612     bool extend = false;
3613     bool iss_sf;
3614     MemOp mop = size;
3615 
3616     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3617         unallocated_encoding(s);
3618         return;
3619     }
3620 
3621     switch (opc) {
3622     case 0: /* STLURB */
3623         is_store = true;
3624         break;
3625     case 1: /* LDAPUR* */
3626         break;
3627     case 2: /* LDAPURS* 64-bit variant */
3628         if (size == 3) {
3629             unallocated_encoding(s);
3630             return;
3631         }
3632         mop |= MO_SIGN;
3633         break;
3634     case 3: /* LDAPURS* 32-bit variant */
3635         if (size > 1) {
3636             unallocated_encoding(s);
3637             return;
3638         }
3639         mop |= MO_SIGN;
3640         extend = true; /* zero-extend 32->64 after signed load */
3641         break;
3642     default:
3643         g_assert_not_reached();
3644     }
3645 
3646     iss_sf = disas_ldst_compute_iss_sf(size, (mop & MO_SIGN) != 0, opc);
3647 
3648     if (rn == 31) {
3649         gen_check_sp_alignment(s);
3650     }
3651 
3652     mop = check_ordered_align(s, rn, offset, is_store, mop);
3653 
3654     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3655     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3656     clean_addr = clean_data_tbi(s, dirty_addr);
3657 
3658     if (is_store) {
3659         /* Store-Release semantics */
3660         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3661         do_gpr_st(s, cpu_reg(s, rt), clean_addr, mop, true, rt, iss_sf, true);
3662     } else {
3663         /*
3664          * Load-AcquirePC semantics; we implement as the slightly more
3665          * restrictive Load-Acquire.
3666          */
3667         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop,
3668                   extend, true, rt, iss_sf, true);
3669         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3670     }
3671 }
3672 
3673 /* Load/store register (all forms) */
3674 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
3675 {
3676     int rt = extract32(insn, 0, 5);
3677     int opc = extract32(insn, 22, 2);
3678     bool is_vector = extract32(insn, 26, 1);
3679     int size = extract32(insn, 30, 2);
3680 
3681     switch (extract32(insn, 24, 2)) {
3682     case 0:
3683         if (extract32(insn, 21, 1) == 0) {
3684             /* Load/store register (unscaled immediate)
3685              * Load/store immediate pre/post-indexed
3686              * Load/store register unprivileged
3687              */
3688             disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
3689             return;
3690         }
3691         switch (extract32(insn, 10, 2)) {
3692         case 0:
3693             disas_ldst_atomic(s, insn, size, rt, is_vector);
3694             return;
3695         case 2:
3696             disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
3697             return;
3698         default:
3699             disas_ldst_pac(s, insn, size, rt, is_vector);
3700             return;
3701         }
3702         break;
3703     case 1:
3704         disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
3705         return;
3706     }
3707     unallocated_encoding(s);
3708 }
3709 
3710 /* AdvSIMD load/store multiple structures
3711  *
3712  *  31  30  29           23 22  21         16 15    12 11  10 9    5 4    0
3713  * +---+---+---------------+---+-------------+--------+------+------+------+
3714  * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size |  Rn  |  Rt  |
3715  * +---+---+---------------+---+-------------+--------+------+------+------+
3716  *
3717  * AdvSIMD load/store multiple structures (post-indexed)
3718  *
3719  *  31  30  29           23 22  21  20     16 15    12 11  10 9    5 4    0
3720  * +---+---+---------------+---+---+---------+--------+------+------+------+
3721  * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 |   Rm    | opcode | size |  Rn  |  Rt  |
3722  * +---+---+---------------+---+---+---------+--------+------+------+------+
3723  *
3724  * Rt: first (or only) SIMD&FP register to be transferred
3725  * Rn: base address or SP
3726  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3727  */
3728 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
3729 {
3730     int rt = extract32(insn, 0, 5);
3731     int rn = extract32(insn, 5, 5);
3732     int rm = extract32(insn, 16, 5);
3733     int size = extract32(insn, 10, 2);
3734     int opcode = extract32(insn, 12, 4);
3735     bool is_store = !extract32(insn, 22, 1);
3736     bool is_postidx = extract32(insn, 23, 1);
3737     bool is_q = extract32(insn, 30, 1);
3738     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3739     MemOp endian, align, mop;
3740 
3741     int total;    /* total bytes */
3742     int elements; /* elements per vector */
3743     int rpt;    /* num iterations */
3744     int selem;  /* structure elements */
3745     int r;
3746 
3747     if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
3748         unallocated_encoding(s);
3749         return;
3750     }
3751 
3752     if (!is_postidx && rm != 0) {
3753         unallocated_encoding(s);
3754         return;
3755     }
3756 
3757     /* From the shared decode logic */
3758     switch (opcode) {
3759     case 0x0:
3760         rpt = 1;
3761         selem = 4;
3762         break;
3763     case 0x2:
3764         rpt = 4;
3765         selem = 1;
3766         break;
3767     case 0x4:
3768         rpt = 1;
3769         selem = 3;
3770         break;
3771     case 0x6:
3772         rpt = 3;
3773         selem = 1;
3774         break;
3775     case 0x7:
3776         rpt = 1;
3777         selem = 1;
3778         break;
3779     case 0x8:
3780         rpt = 1;
3781         selem = 2;
3782         break;
3783     case 0xa:
3784         rpt = 2;
3785         selem = 1;
3786         break;
3787     default:
3788         unallocated_encoding(s);
3789         return;
3790     }
3791 
3792     if (size == 3 && !is_q && selem != 1) {
3793         /* reserved */
3794         unallocated_encoding(s);
3795         return;
3796     }
3797 
3798     if (!fp_access_check(s)) {
3799         return;
3800     }
3801 
3802     if (rn == 31) {
3803         gen_check_sp_alignment(s);
3804     }
3805 
3806     /* For our purposes, bytes are always little-endian.  */
3807     endian = s->be_data;
3808     if (size == 0) {
3809         endian = MO_LE;
3810     }
3811 
3812     total = rpt * selem * (is_q ? 16 : 8);
3813     tcg_rn = cpu_reg_sp(s, rn);
3814 
3815     /*
3816      * Issue the MTE check vs the logical repeat count, before we
3817      * promote consecutive little-endian elements below.
3818      */
3819     clean_addr = gen_mte_checkN(s, tcg_rn, is_store, is_postidx || rn != 31,
3820                                 total, finalize_memop(s, size));
3821 
3822     /*
3823      * Consecutive little-endian elements from a single register
3824      * can be promoted to a larger little-endian operation.
3825      */
3826     align = MO_ALIGN;
3827     if (selem == 1 && endian == MO_LE) {
3828         align = pow2_align(size);
3829         size = 3;
3830     }
3831     if (!s->align_mem) {
3832         align = 0;
3833     }
3834     mop = endian | size | align;
3835 
3836     elements = (is_q ? 16 : 8) >> size;
3837     tcg_ebytes = tcg_constant_i64(1 << size);
3838     for (r = 0; r < rpt; r++) {
3839         int e;
3840         for (e = 0; e < elements; e++) {
3841             int xs;
3842             for (xs = 0; xs < selem; xs++) {
3843                 int tt = (rt + r + xs) % 32;
3844                 if (is_store) {
3845                     do_vec_st(s, tt, e, clean_addr, mop);
3846                 } else {
3847                     do_vec_ld(s, tt, e, clean_addr, mop);
3848                 }
3849                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3850             }
3851         }
3852     }
3853 
3854     if (!is_store) {
3855         /* For non-quad operations, setting a slice of the low
3856          * 64 bits of the register clears the high 64 bits (in
3857          * the ARM ARM pseudocode this is implicit in the fact
3858          * that 'rval' is a 64 bit wide variable).
3859          * For quad operations, we might still need to zero the
3860          * high bits of SVE.
3861          */
3862         for (r = 0; r < rpt * selem; r++) {
3863             int tt = (rt + r) % 32;
3864             clear_vec_high(s, is_q, tt);
3865         }
3866     }
3867 
3868     if (is_postidx) {
3869         if (rm == 31) {
3870             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3871         } else {
3872             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3873         }
3874     }
3875 }
3876 
3877 /* AdvSIMD load/store single structure
3878  *
3879  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3880  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3881  * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size |  Rn  |  Rt  |
3882  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3883  *
3884  * AdvSIMD load/store single structure (post-indexed)
3885  *
3886  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3887  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3888  * | 0 | Q | 0 0 1 1 0 1 1 | L R |     Rm    | opc | S | size |  Rn  |  Rt  |
3889  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3890  *
3891  * Rt: first (or only) SIMD&FP register to be transferred
3892  * Rn: base address or SP
3893  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3894  * index = encoded in Q:S:size dependent on size
3895  *
3896  * lane_size = encoded in R, opc
3897  * transfer width = encoded in opc, S, size
3898  */
3899 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
3900 {
3901     int rt = extract32(insn, 0, 5);
3902     int rn = extract32(insn, 5, 5);
3903     int rm = extract32(insn, 16, 5);
3904     int size = extract32(insn, 10, 2);
3905     int S = extract32(insn, 12, 1);
3906     int opc = extract32(insn, 13, 3);
3907     int R = extract32(insn, 21, 1);
3908     int is_load = extract32(insn, 22, 1);
3909     int is_postidx = extract32(insn, 23, 1);
3910     int is_q = extract32(insn, 30, 1);
3911 
3912     int scale = extract32(opc, 1, 2);
3913     int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
3914     bool replicate = false;
3915     int index = is_q << 3 | S << 2 | size;
3916     int xs, total;
3917     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3918     MemOp mop;
3919 
3920     if (extract32(insn, 31, 1)) {
3921         unallocated_encoding(s);
3922         return;
3923     }
3924     if (!is_postidx && rm != 0) {
3925         unallocated_encoding(s);
3926         return;
3927     }
3928 
3929     switch (scale) {
3930     case 3:
3931         if (!is_load || S) {
3932             unallocated_encoding(s);
3933             return;
3934         }
3935         scale = size;
3936         replicate = true;
3937         break;
3938     case 0:
3939         break;
3940     case 1:
3941         if (extract32(size, 0, 1)) {
3942             unallocated_encoding(s);
3943             return;
3944         }
3945         index >>= 1;
3946         break;
3947     case 2:
3948         if (extract32(size, 1, 1)) {
3949             unallocated_encoding(s);
3950             return;
3951         }
3952         if (!extract32(size, 0, 1)) {
3953             index >>= 2;
3954         } else {
3955             if (S) {
3956                 unallocated_encoding(s);
3957                 return;
3958             }
3959             index >>= 3;
3960             scale = 3;
3961         }
3962         break;
3963     default:
3964         g_assert_not_reached();
3965     }
3966 
3967     if (!fp_access_check(s)) {
3968         return;
3969     }
3970 
3971     if (rn == 31) {
3972         gen_check_sp_alignment(s);
3973     }
3974 
3975     total = selem << scale;
3976     tcg_rn = cpu_reg_sp(s, rn);
3977 
3978     mop = finalize_memop(s, scale);
3979 
3980     clean_addr = gen_mte_checkN(s, tcg_rn, !is_load, is_postidx || rn != 31,
3981                                 total, mop);
3982 
3983     tcg_ebytes = tcg_constant_i64(1 << scale);
3984     for (xs = 0; xs < selem; xs++) {
3985         if (replicate) {
3986             /* Load and replicate to all elements */
3987             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3988 
3989             tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
3990             tcg_gen_gvec_dup_i64(scale, vec_full_reg_offset(s, rt),
3991                                  (is_q + 1) * 8, vec_full_reg_size(s),
3992                                  tcg_tmp);
3993         } else {
3994             /* Load/store one element per register */
3995             if (is_load) {
3996                 do_vec_ld(s, rt, index, clean_addr, mop);
3997             } else {
3998                 do_vec_st(s, rt, index, clean_addr, mop);
3999             }
4000         }
4001         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
4002         rt = (rt + 1) % 32;
4003     }
4004 
4005     if (is_postidx) {
4006         if (rm == 31) {
4007             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
4008         } else {
4009             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
4010         }
4011     }
4012 }
4013 
4014 /*
4015  * Load/Store memory tags
4016  *
4017  *  31 30 29         24     22  21     12    10      5      0
4018  * +-----+-------------+-----+---+------+-----+------+------+
4019  * | 1 1 | 0 1 1 0 0 1 | op1 | 1 | imm9 | op2 |  Rn  |  Rt  |
4020  * +-----+-------------+-----+---+------+-----+------+------+
4021  */
4022 static void disas_ldst_tag(DisasContext *s, uint32_t insn)
4023 {
4024     int rt = extract32(insn, 0, 5);
4025     int rn = extract32(insn, 5, 5);
4026     uint64_t offset = sextract64(insn, 12, 9) << LOG2_TAG_GRANULE;
4027     int op2 = extract32(insn, 10, 2);
4028     int op1 = extract32(insn, 22, 2);
4029     bool is_load = false, is_pair = false, is_zero = false, is_mult = false;
4030     int index = 0;
4031     TCGv_i64 addr, clean_addr, tcg_rt;
4032 
4033     /* We checked insn bits [29:24,21] in the caller.  */
4034     if (extract32(insn, 30, 2) != 3) {
4035         goto do_unallocated;
4036     }
4037 
4038     /*
4039      * @index is a tri-state variable which has 3 states:
4040      * < 0 : post-index, writeback
4041      * = 0 : signed offset
4042      * > 0 : pre-index, writeback
4043      */
4044     switch (op1) {
4045     case 0:
4046         if (op2 != 0) {
4047             /* STG */
4048             index = op2 - 2;
4049         } else {
4050             /* STZGM */
4051             if (s->current_el == 0 || offset != 0) {
4052                 goto do_unallocated;
4053             }
4054             is_mult = is_zero = true;
4055         }
4056         break;
4057     case 1:
4058         if (op2 != 0) {
4059             /* STZG */
4060             is_zero = true;
4061             index = op2 - 2;
4062         } else {
4063             /* LDG */
4064             is_load = true;
4065         }
4066         break;
4067     case 2:
4068         if (op2 != 0) {
4069             /* ST2G */
4070             is_pair = true;
4071             index = op2 - 2;
4072         } else {
4073             /* STGM */
4074             if (s->current_el == 0 || offset != 0) {
4075                 goto do_unallocated;
4076             }
4077             is_mult = true;
4078         }
4079         break;
4080     case 3:
4081         if (op2 != 0) {
4082             /* STZ2G */
4083             is_pair = is_zero = true;
4084             index = op2 - 2;
4085         } else {
4086             /* LDGM */
4087             if (s->current_el == 0 || offset != 0) {
4088                 goto do_unallocated;
4089             }
4090             is_mult = is_load = true;
4091         }
4092         break;
4093 
4094     default:
4095     do_unallocated:
4096         unallocated_encoding(s);
4097         return;
4098     }
4099 
4100     if (is_mult
4101         ? !dc_isar_feature(aa64_mte, s)
4102         : !dc_isar_feature(aa64_mte_insn_reg, s)) {
4103         goto do_unallocated;
4104     }
4105 
4106     if (rn == 31) {
4107         gen_check_sp_alignment(s);
4108     }
4109 
4110     addr = read_cpu_reg_sp(s, rn, true);
4111     if (index >= 0) {
4112         /* pre-index or signed offset */
4113         tcg_gen_addi_i64(addr, addr, offset);
4114     }
4115 
4116     if (is_mult) {
4117         tcg_rt = cpu_reg(s, rt);
4118 
4119         if (is_zero) {
4120             int size = 4 << s->dcz_blocksize;
4121 
4122             if (s->ata) {
4123                 gen_helper_stzgm_tags(cpu_env, addr, tcg_rt);
4124             }
4125             /*
4126              * The non-tags portion of STZGM is mostly like DC_ZVA,
4127              * except the alignment happens before the access.
4128              */
4129             clean_addr = clean_data_tbi(s, addr);
4130             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4131             gen_helper_dc_zva(cpu_env, clean_addr);
4132         } else if (s->ata) {
4133             if (is_load) {
4134                 gen_helper_ldgm(tcg_rt, cpu_env, addr);
4135             } else {
4136                 gen_helper_stgm(cpu_env, addr, tcg_rt);
4137             }
4138         } else {
4139             MMUAccessType acc = is_load ? MMU_DATA_LOAD : MMU_DATA_STORE;
4140             int size = 4 << GMID_EL1_BS;
4141 
4142             clean_addr = clean_data_tbi(s, addr);
4143             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4144             gen_probe_access(s, clean_addr, acc, size);
4145 
4146             if (is_load) {
4147                 /* The result tags are zeros.  */
4148                 tcg_gen_movi_i64(tcg_rt, 0);
4149             }
4150         }
4151         return;
4152     }
4153 
4154     if (is_load) {
4155         tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
4156         tcg_rt = cpu_reg(s, rt);
4157         if (s->ata) {
4158             gen_helper_ldg(tcg_rt, cpu_env, addr, tcg_rt);
4159         } else {
4160             clean_addr = clean_data_tbi(s, addr);
4161             gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
4162             gen_address_with_allocation_tag0(tcg_rt, addr);
4163         }
4164     } else {
4165         tcg_rt = cpu_reg_sp(s, rt);
4166         if (!s->ata) {
4167             /*
4168              * For STG and ST2G, we need to check alignment and probe memory.
4169              * TODO: For STZG and STZ2G, we could rely on the stores below,
4170              * at least for system mode; user-only won't enforce alignment.
4171              */
4172             if (is_pair) {
4173                 gen_helper_st2g_stub(cpu_env, addr);
4174             } else {
4175                 gen_helper_stg_stub(cpu_env, addr);
4176             }
4177         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
4178             if (is_pair) {
4179                 gen_helper_st2g_parallel(cpu_env, addr, tcg_rt);
4180             } else {
4181                 gen_helper_stg_parallel(cpu_env, addr, tcg_rt);
4182             }
4183         } else {
4184             if (is_pair) {
4185                 gen_helper_st2g(cpu_env, addr, tcg_rt);
4186             } else {
4187                 gen_helper_stg(cpu_env, addr, tcg_rt);
4188             }
4189         }
4190     }
4191 
4192     if (is_zero) {
4193         TCGv_i64 clean_addr = clean_data_tbi(s, addr);
4194         TCGv_i64 zero64 = tcg_constant_i64(0);
4195         TCGv_i128 zero128 = tcg_temp_new_i128();
4196         int mem_index = get_mem_index(s);
4197         MemOp mop = finalize_memop(s, MO_128 | MO_ALIGN);
4198 
4199         tcg_gen_concat_i64_i128(zero128, zero64, zero64);
4200 
4201         /* This is 1 or 2 atomic 16-byte operations. */
4202         tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4203         if (is_pair) {
4204             tcg_gen_addi_i64(clean_addr, clean_addr, 16);
4205             tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4206         }
4207     }
4208 
4209     if (index != 0) {
4210         /* pre-index or post-index */
4211         if (index < 0) {
4212             /* post-index */
4213             tcg_gen_addi_i64(addr, addr, offset);
4214         }
4215         tcg_gen_mov_i64(cpu_reg_sp(s, rn), addr);
4216     }
4217 }
4218 
4219 /* Loads and stores */
4220 static void disas_ldst(DisasContext *s, uint32_t insn)
4221 {
4222     switch (extract32(insn, 24, 6)) {
4223     case 0x08: /* Load/store exclusive */
4224         disas_ldst_excl(s, insn);
4225         break;
4226     case 0x18: case 0x1c: /* Load register (literal) */
4227         disas_ld_lit(s, insn);
4228         break;
4229     case 0x28: case 0x29:
4230     case 0x2c: case 0x2d: /* Load/store pair (all forms) */
4231         disas_ldst_pair(s, insn);
4232         break;
4233     case 0x38: case 0x39:
4234     case 0x3c: case 0x3d: /* Load/store register (all forms) */
4235         disas_ldst_reg(s, insn);
4236         break;
4237     case 0x0c: /* AdvSIMD load/store multiple structures */
4238         disas_ldst_multiple_struct(s, insn);
4239         break;
4240     case 0x0d: /* AdvSIMD load/store single structure */
4241         disas_ldst_single_struct(s, insn);
4242         break;
4243     case 0x19:
4244         if (extract32(insn, 21, 1) != 0) {
4245             disas_ldst_tag(s, insn);
4246         } else if (extract32(insn, 10, 2) == 0) {
4247             disas_ldst_ldapr_stlr(s, insn);
4248         } else {
4249             unallocated_encoding(s);
4250         }
4251         break;
4252     default:
4253         unallocated_encoding(s);
4254         break;
4255     }
4256 }
4257 
4258 typedef void ArithTwoOp(TCGv_i64, TCGv_i64, TCGv_i64);
4259 
4260 static bool gen_rri(DisasContext *s, arg_rri_sf *a,
4261                     bool rd_sp, bool rn_sp, ArithTwoOp *fn)
4262 {
4263     TCGv_i64 tcg_rn = rn_sp ? cpu_reg_sp(s, a->rn) : cpu_reg(s, a->rn);
4264     TCGv_i64 tcg_rd = rd_sp ? cpu_reg_sp(s, a->rd) : cpu_reg(s, a->rd);
4265     TCGv_i64 tcg_imm = tcg_constant_i64(a->imm);
4266 
4267     fn(tcg_rd, tcg_rn, tcg_imm);
4268     if (!a->sf) {
4269         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4270     }
4271     return true;
4272 }
4273 
4274 /*
4275  * PC-rel. addressing
4276  */
4277 
4278 static bool trans_ADR(DisasContext *s, arg_ri *a)
4279 {
4280     gen_pc_plus_diff(s, cpu_reg(s, a->rd), a->imm);
4281     return true;
4282 }
4283 
4284 static bool trans_ADRP(DisasContext *s, arg_ri *a)
4285 {
4286     int64_t offset = (int64_t)a->imm << 12;
4287 
4288     /* The page offset is ok for CF_PCREL. */
4289     offset -= s->pc_curr & 0xfff;
4290     gen_pc_plus_diff(s, cpu_reg(s, a->rd), offset);
4291     return true;
4292 }
4293 
4294 /*
4295  * Add/subtract (immediate)
4296  */
4297 TRANS(ADD_i, gen_rri, a, 1, 1, tcg_gen_add_i64)
4298 TRANS(SUB_i, gen_rri, a, 1, 1, tcg_gen_sub_i64)
4299 TRANS(ADDS_i, gen_rri, a, 0, 1, a->sf ? gen_add64_CC : gen_add32_CC)
4300 TRANS(SUBS_i, gen_rri, a, 0, 1, a->sf ? gen_sub64_CC : gen_sub32_CC)
4301 
4302 /*
4303  * Add/subtract (immediate, with tags)
4304  */
4305 
4306 static bool gen_add_sub_imm_with_tags(DisasContext *s, arg_rri_tag *a,
4307                                       bool sub_op)
4308 {
4309     TCGv_i64 tcg_rn, tcg_rd;
4310     int imm;
4311 
4312     imm = a->uimm6 << LOG2_TAG_GRANULE;
4313     if (sub_op) {
4314         imm = -imm;
4315     }
4316 
4317     tcg_rn = cpu_reg_sp(s, a->rn);
4318     tcg_rd = cpu_reg_sp(s, a->rd);
4319 
4320     if (s->ata) {
4321         gen_helper_addsubg(tcg_rd, cpu_env, tcg_rn,
4322                            tcg_constant_i32(imm),
4323                            tcg_constant_i32(a->uimm4));
4324     } else {
4325         tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4326         gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4327     }
4328     return true;
4329 }
4330 
4331 TRANS_FEAT(ADDG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, false)
4332 TRANS_FEAT(SUBG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, true)
4333 
4334 /* The input should be a value in the bottom e bits (with higher
4335  * bits zero); returns that value replicated into every element
4336  * of size e in a 64 bit integer.
4337  */
4338 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4339 {
4340     assert(e != 0);
4341     while (e < 64) {
4342         mask |= mask << e;
4343         e *= 2;
4344     }
4345     return mask;
4346 }
4347 
4348 /*
4349  * Logical (immediate)
4350  */
4351 
4352 /*
4353  * Simplified variant of pseudocode DecodeBitMasks() for the case where we
4354  * only require the wmask. Returns false if the imms/immr/immn are a reserved
4355  * value (ie should cause a guest UNDEF exception), and true if they are
4356  * valid, in which case the decoded bit pattern is written to result.
4357  */
4358 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4359                             unsigned int imms, unsigned int immr)
4360 {
4361     uint64_t mask;
4362     unsigned e, levels, s, r;
4363     int len;
4364 
4365     assert(immn < 2 && imms < 64 && immr < 64);
4366 
4367     /* The bit patterns we create here are 64 bit patterns which
4368      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4369      * 64 bits each. Each element contains the same value: a run
4370      * of between 1 and e-1 non-zero bits, rotated within the
4371      * element by between 0 and e-1 bits.
4372      *
4373      * The element size and run length are encoded into immn (1 bit)
4374      * and imms (6 bits) as follows:
4375      * 64 bit elements: immn = 1, imms = <length of run - 1>
4376      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4377      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4378      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4379      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4380      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4381      * Notice that immn = 0, imms = 11111x is the only combination
4382      * not covered by one of the above options; this is reserved.
4383      * Further, <length of run - 1> all-ones is a reserved pattern.
4384      *
4385      * In all cases the rotation is by immr % e (and immr is 6 bits).
4386      */
4387 
4388     /* First determine the element size */
4389     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4390     if (len < 1) {
4391         /* This is the immn == 0, imms == 0x11111x case */
4392         return false;
4393     }
4394     e = 1 << len;
4395 
4396     levels = e - 1;
4397     s = imms & levels;
4398     r = immr & levels;
4399 
4400     if (s == levels) {
4401         /* <length of run - 1> mustn't be all-ones. */
4402         return false;
4403     }
4404 
4405     /* Create the value of one element: s+1 set bits rotated
4406      * by r within the element (which is e bits wide)...
4407      */
4408     mask = MAKE_64BIT_MASK(0, s + 1);
4409     if (r) {
4410         mask = (mask >> r) | (mask << (e - r));
4411         mask &= MAKE_64BIT_MASK(0, e);
4412     }
4413     /* ...then replicate the element over the whole 64 bit value */
4414     mask = bitfield_replicate(mask, e);
4415     *result = mask;
4416     return true;
4417 }
4418 
4419 static bool gen_rri_log(DisasContext *s, arg_rri_log *a, bool set_cc,
4420                         void (*fn)(TCGv_i64, TCGv_i64, int64_t))
4421 {
4422     TCGv_i64 tcg_rd, tcg_rn;
4423     uint64_t imm;
4424 
4425     /* Some immediate field values are reserved. */
4426     if (!logic_imm_decode_wmask(&imm, extract32(a->dbm, 12, 1),
4427                                 extract32(a->dbm, 0, 6),
4428                                 extract32(a->dbm, 6, 6))) {
4429         return false;
4430     }
4431     if (!a->sf) {
4432         imm &= 0xffffffffull;
4433     }
4434 
4435     tcg_rd = set_cc ? cpu_reg(s, a->rd) : cpu_reg_sp(s, a->rd);
4436     tcg_rn = cpu_reg(s, a->rn);
4437 
4438     fn(tcg_rd, tcg_rn, imm);
4439     if (set_cc) {
4440         gen_logic_CC(a->sf, tcg_rd);
4441     }
4442     if (!a->sf) {
4443         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4444     }
4445     return true;
4446 }
4447 
4448 TRANS(AND_i, gen_rri_log, a, false, tcg_gen_andi_i64)
4449 TRANS(ORR_i, gen_rri_log, a, false, tcg_gen_ori_i64)
4450 TRANS(EOR_i, gen_rri_log, a, false, tcg_gen_xori_i64)
4451 TRANS(ANDS_i, gen_rri_log, a, true, tcg_gen_andi_i64)
4452 
4453 /*
4454  * Move wide (immediate)
4455  */
4456 
4457 static bool trans_MOVZ(DisasContext *s, arg_movw *a)
4458 {
4459     int pos = a->hw << 4;
4460     tcg_gen_movi_i64(cpu_reg(s, a->rd), (uint64_t)a->imm << pos);
4461     return true;
4462 }
4463 
4464 static bool trans_MOVN(DisasContext *s, arg_movw *a)
4465 {
4466     int pos = a->hw << 4;
4467     uint64_t imm = a->imm;
4468 
4469     imm = ~(imm << pos);
4470     if (!a->sf) {
4471         imm = (uint32_t)imm;
4472     }
4473     tcg_gen_movi_i64(cpu_reg(s, a->rd), imm);
4474     return true;
4475 }
4476 
4477 static bool trans_MOVK(DisasContext *s, arg_movw *a)
4478 {
4479     int pos = a->hw << 4;
4480     TCGv_i64 tcg_rd, tcg_im;
4481 
4482     tcg_rd = cpu_reg(s, a->rd);
4483     tcg_im = tcg_constant_i64(a->imm);
4484     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_im, pos, 16);
4485     if (!a->sf) {
4486         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4487     }
4488     return true;
4489 }
4490 
4491 /*
4492  * Bitfield
4493  */
4494 
4495 static bool trans_SBFM(DisasContext *s, arg_SBFM *a)
4496 {
4497     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4498     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4499     unsigned int bitsize = a->sf ? 64 : 32;
4500     unsigned int ri = a->immr;
4501     unsigned int si = a->imms;
4502     unsigned int pos, len;
4503 
4504     if (si >= ri) {
4505         /* Wd<s-r:0> = Wn<s:r> */
4506         len = (si - ri) + 1;
4507         tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4508         if (!a->sf) {
4509             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4510         }
4511     } else {
4512         /* Wd<32+s-r,32-r> = Wn<s:0> */
4513         len = si + 1;
4514         pos = (bitsize - ri) & (bitsize - 1);
4515 
4516         if (len < ri) {
4517             /*
4518              * Sign extend the destination field from len to fill the
4519              * balance of the word.  Let the deposit below insert all
4520              * of those sign bits.
4521              */
4522             tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4523             len = ri;
4524         }
4525 
4526         /*
4527          * We start with zero, and we haven't modified any bits outside
4528          * bitsize, therefore no final zero-extension is unneeded for !sf.
4529          */
4530         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4531     }
4532     return true;
4533 }
4534 
4535 static bool trans_UBFM(DisasContext *s, arg_UBFM *a)
4536 {
4537     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4538     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4539     unsigned int bitsize = a->sf ? 64 : 32;
4540     unsigned int ri = a->immr;
4541     unsigned int si = a->imms;
4542     unsigned int pos, len;
4543 
4544     tcg_rd = cpu_reg(s, a->rd);
4545     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4546 
4547     if (si >= ri) {
4548         /* Wd<s-r:0> = Wn<s:r> */
4549         len = (si - ri) + 1;
4550         tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4551     } else {
4552         /* Wd<32+s-r,32-r> = Wn<s:0> */
4553         len = si + 1;
4554         pos = (bitsize - ri) & (bitsize - 1);
4555         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4556     }
4557     return true;
4558 }
4559 
4560 static bool trans_BFM(DisasContext *s, arg_BFM *a)
4561 {
4562     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4563     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4564     unsigned int bitsize = a->sf ? 64 : 32;
4565     unsigned int ri = a->immr;
4566     unsigned int si = a->imms;
4567     unsigned int pos, len;
4568 
4569     tcg_rd = cpu_reg(s, a->rd);
4570     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4571 
4572     if (si >= ri) {
4573         /* Wd<s-r:0> = Wn<s:r> */
4574         tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4575         len = (si - ri) + 1;
4576         pos = 0;
4577     } else {
4578         /* Wd<32+s-r,32-r> = Wn<s:0> */
4579         len = si + 1;
4580         pos = (bitsize - ri) & (bitsize - 1);
4581     }
4582 
4583     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4584     if (!a->sf) {
4585         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4586     }
4587     return true;
4588 }
4589 
4590 static bool trans_EXTR(DisasContext *s, arg_extract *a)
4591 {
4592     TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4593 
4594     tcg_rd = cpu_reg(s, a->rd);
4595 
4596     if (unlikely(a->imm == 0)) {
4597         /*
4598          * tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4599          * so an extract from bit 0 is a special case.
4600          */
4601         if (a->sf) {
4602             tcg_gen_mov_i64(tcg_rd, cpu_reg(s, a->rm));
4603         } else {
4604             tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, a->rm));
4605         }
4606     } else {
4607         tcg_rm = cpu_reg(s, a->rm);
4608         tcg_rn = cpu_reg(s, a->rn);
4609 
4610         if (a->sf) {
4611             /* Specialization to ROR happens in EXTRACT2.  */
4612             tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, a->imm);
4613         } else {
4614             TCGv_i32 t0 = tcg_temp_new_i32();
4615 
4616             tcg_gen_extrl_i64_i32(t0, tcg_rm);
4617             if (a->rm == a->rn) {
4618                 tcg_gen_rotri_i32(t0, t0, a->imm);
4619             } else {
4620                 TCGv_i32 t1 = tcg_temp_new_i32();
4621                 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4622                 tcg_gen_extract2_i32(t0, t0, t1, a->imm);
4623             }
4624             tcg_gen_extu_i32_i64(tcg_rd, t0);
4625         }
4626     }
4627     return true;
4628 }
4629 
4630 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4631  * Note that it is the caller's responsibility to ensure that the
4632  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4633  * mandated semantics for out of range shifts.
4634  */
4635 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4636                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4637 {
4638     switch (shift_type) {
4639     case A64_SHIFT_TYPE_LSL:
4640         tcg_gen_shl_i64(dst, src, shift_amount);
4641         break;
4642     case A64_SHIFT_TYPE_LSR:
4643         tcg_gen_shr_i64(dst, src, shift_amount);
4644         break;
4645     case A64_SHIFT_TYPE_ASR:
4646         if (!sf) {
4647             tcg_gen_ext32s_i64(dst, src);
4648         }
4649         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4650         break;
4651     case A64_SHIFT_TYPE_ROR:
4652         if (sf) {
4653             tcg_gen_rotr_i64(dst, src, shift_amount);
4654         } else {
4655             TCGv_i32 t0, t1;
4656             t0 = tcg_temp_new_i32();
4657             t1 = tcg_temp_new_i32();
4658             tcg_gen_extrl_i64_i32(t0, src);
4659             tcg_gen_extrl_i64_i32(t1, shift_amount);
4660             tcg_gen_rotr_i32(t0, t0, t1);
4661             tcg_gen_extu_i32_i64(dst, t0);
4662         }
4663         break;
4664     default:
4665         assert(FALSE); /* all shift types should be handled */
4666         break;
4667     }
4668 
4669     if (!sf) { /* zero extend final result */
4670         tcg_gen_ext32u_i64(dst, dst);
4671     }
4672 }
4673 
4674 /* Shift a TCGv src by immediate, put result in dst.
4675  * The shift amount must be in range (this should always be true as the
4676  * relevant instructions will UNDEF on bad shift immediates).
4677  */
4678 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4679                           enum a64_shift_type shift_type, unsigned int shift_i)
4680 {
4681     assert(shift_i < (sf ? 64 : 32));
4682 
4683     if (shift_i == 0) {
4684         tcg_gen_mov_i64(dst, src);
4685     } else {
4686         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
4687     }
4688 }
4689 
4690 /* Logical (shifted register)
4691  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
4692  * +----+-----+-----------+-------+---+------+--------+------+------+
4693  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
4694  * +----+-----+-----------+-------+---+------+--------+------+------+
4695  */
4696 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4697 {
4698     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4699     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4700 
4701     sf = extract32(insn, 31, 1);
4702     opc = extract32(insn, 29, 2);
4703     shift_type = extract32(insn, 22, 2);
4704     invert = extract32(insn, 21, 1);
4705     rm = extract32(insn, 16, 5);
4706     shift_amount = extract32(insn, 10, 6);
4707     rn = extract32(insn, 5, 5);
4708     rd = extract32(insn, 0, 5);
4709 
4710     if (!sf && (shift_amount & (1 << 5))) {
4711         unallocated_encoding(s);
4712         return;
4713     }
4714 
4715     tcg_rd = cpu_reg(s, rd);
4716 
4717     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4718         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4719          * register-register MOV and MVN, so it is worth special casing.
4720          */
4721         tcg_rm = cpu_reg(s, rm);
4722         if (invert) {
4723             tcg_gen_not_i64(tcg_rd, tcg_rm);
4724             if (!sf) {
4725                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4726             }
4727         } else {
4728             if (sf) {
4729                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4730             } else {
4731                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4732             }
4733         }
4734         return;
4735     }
4736 
4737     tcg_rm = read_cpu_reg(s, rm, sf);
4738 
4739     if (shift_amount) {
4740         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4741     }
4742 
4743     tcg_rn = cpu_reg(s, rn);
4744 
4745     switch (opc | (invert << 2)) {
4746     case 0: /* AND */
4747     case 3: /* ANDS */
4748         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4749         break;
4750     case 1: /* ORR */
4751         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4752         break;
4753     case 2: /* EOR */
4754         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4755         break;
4756     case 4: /* BIC */
4757     case 7: /* BICS */
4758         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4759         break;
4760     case 5: /* ORN */
4761         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4762         break;
4763     case 6: /* EON */
4764         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4765         break;
4766     default:
4767         assert(FALSE);
4768         break;
4769     }
4770 
4771     if (!sf) {
4772         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4773     }
4774 
4775     if (opc == 3) {
4776         gen_logic_CC(sf, tcg_rd);
4777     }
4778 }
4779 
4780 /*
4781  * Add/subtract (extended register)
4782  *
4783  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
4784  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4785  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
4786  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4787  *
4788  *  sf: 0 -> 32bit, 1 -> 64bit
4789  *  op: 0 -> add  , 1 -> sub
4790  *   S: 1 -> set flags
4791  * opt: 00
4792  * option: extension type (see DecodeRegExtend)
4793  * imm3: optional shift to Rm
4794  *
4795  * Rd = Rn + LSL(extend(Rm), amount)
4796  */
4797 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4798 {
4799     int rd = extract32(insn, 0, 5);
4800     int rn = extract32(insn, 5, 5);
4801     int imm3 = extract32(insn, 10, 3);
4802     int option = extract32(insn, 13, 3);
4803     int rm = extract32(insn, 16, 5);
4804     int opt = extract32(insn, 22, 2);
4805     bool setflags = extract32(insn, 29, 1);
4806     bool sub_op = extract32(insn, 30, 1);
4807     bool sf = extract32(insn, 31, 1);
4808 
4809     TCGv_i64 tcg_rm, tcg_rn; /* temps */
4810     TCGv_i64 tcg_rd;
4811     TCGv_i64 tcg_result;
4812 
4813     if (imm3 > 4 || opt != 0) {
4814         unallocated_encoding(s);
4815         return;
4816     }
4817 
4818     /* non-flag setting ops may use SP */
4819     if (!setflags) {
4820         tcg_rd = cpu_reg_sp(s, rd);
4821     } else {
4822         tcg_rd = cpu_reg(s, rd);
4823     }
4824     tcg_rn = read_cpu_reg_sp(s, rn, sf);
4825 
4826     tcg_rm = read_cpu_reg(s, rm, sf);
4827     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4828 
4829     tcg_result = tcg_temp_new_i64();
4830 
4831     if (!setflags) {
4832         if (sub_op) {
4833             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4834         } else {
4835             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4836         }
4837     } else {
4838         if (sub_op) {
4839             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4840         } else {
4841             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4842         }
4843     }
4844 
4845     if (sf) {
4846         tcg_gen_mov_i64(tcg_rd, tcg_result);
4847     } else {
4848         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4849     }
4850 }
4851 
4852 /*
4853  * Add/subtract (shifted register)
4854  *
4855  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
4856  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4857  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
4858  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4859  *
4860  *    sf: 0 -> 32bit, 1 -> 64bit
4861  *    op: 0 -> add  , 1 -> sub
4862  *     S: 1 -> set flags
4863  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4864  *  imm6: Shift amount to apply to Rm before the add/sub
4865  */
4866 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4867 {
4868     int rd = extract32(insn, 0, 5);
4869     int rn = extract32(insn, 5, 5);
4870     int imm6 = extract32(insn, 10, 6);
4871     int rm = extract32(insn, 16, 5);
4872     int shift_type = extract32(insn, 22, 2);
4873     bool setflags = extract32(insn, 29, 1);
4874     bool sub_op = extract32(insn, 30, 1);
4875     bool sf = extract32(insn, 31, 1);
4876 
4877     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4878     TCGv_i64 tcg_rn, tcg_rm;
4879     TCGv_i64 tcg_result;
4880 
4881     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4882         unallocated_encoding(s);
4883         return;
4884     }
4885 
4886     tcg_rn = read_cpu_reg(s, rn, sf);
4887     tcg_rm = read_cpu_reg(s, rm, sf);
4888 
4889     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4890 
4891     tcg_result = tcg_temp_new_i64();
4892 
4893     if (!setflags) {
4894         if (sub_op) {
4895             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4896         } else {
4897             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4898         }
4899     } else {
4900         if (sub_op) {
4901             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4902         } else {
4903             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4904         }
4905     }
4906 
4907     if (sf) {
4908         tcg_gen_mov_i64(tcg_rd, tcg_result);
4909     } else {
4910         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4911     }
4912 }
4913 
4914 /* Data-processing (3 source)
4915  *
4916  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
4917  *  +--+------+-----------+------+------+----+------+------+------+
4918  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
4919  *  +--+------+-----------+------+------+----+------+------+------+
4920  */
4921 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4922 {
4923     int rd = extract32(insn, 0, 5);
4924     int rn = extract32(insn, 5, 5);
4925     int ra = extract32(insn, 10, 5);
4926     int rm = extract32(insn, 16, 5);
4927     int op_id = (extract32(insn, 29, 3) << 4) |
4928         (extract32(insn, 21, 3) << 1) |
4929         extract32(insn, 15, 1);
4930     bool sf = extract32(insn, 31, 1);
4931     bool is_sub = extract32(op_id, 0, 1);
4932     bool is_high = extract32(op_id, 2, 1);
4933     bool is_signed = false;
4934     TCGv_i64 tcg_op1;
4935     TCGv_i64 tcg_op2;
4936     TCGv_i64 tcg_tmp;
4937 
4938     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
4939     switch (op_id) {
4940     case 0x42: /* SMADDL */
4941     case 0x43: /* SMSUBL */
4942     case 0x44: /* SMULH */
4943         is_signed = true;
4944         break;
4945     case 0x0: /* MADD (32bit) */
4946     case 0x1: /* MSUB (32bit) */
4947     case 0x40: /* MADD (64bit) */
4948     case 0x41: /* MSUB (64bit) */
4949     case 0x4a: /* UMADDL */
4950     case 0x4b: /* UMSUBL */
4951     case 0x4c: /* UMULH */
4952         break;
4953     default:
4954         unallocated_encoding(s);
4955         return;
4956     }
4957 
4958     if (is_high) {
4959         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
4960         TCGv_i64 tcg_rd = cpu_reg(s, rd);
4961         TCGv_i64 tcg_rn = cpu_reg(s, rn);
4962         TCGv_i64 tcg_rm = cpu_reg(s, rm);
4963 
4964         if (is_signed) {
4965             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4966         } else {
4967             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4968         }
4969         return;
4970     }
4971 
4972     tcg_op1 = tcg_temp_new_i64();
4973     tcg_op2 = tcg_temp_new_i64();
4974     tcg_tmp = tcg_temp_new_i64();
4975 
4976     if (op_id < 0x42) {
4977         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
4978         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
4979     } else {
4980         if (is_signed) {
4981             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
4982             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
4983         } else {
4984             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
4985             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
4986         }
4987     }
4988 
4989     if (ra == 31 && !is_sub) {
4990         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
4991         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
4992     } else {
4993         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
4994         if (is_sub) {
4995             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4996         } else {
4997             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4998         }
4999     }
5000 
5001     if (!sf) {
5002         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
5003     }
5004 }
5005 
5006 /* Add/subtract (with carry)
5007  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
5008  * +--+--+--+------------------------+------+-------------+------+-----+
5009  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
5010  * +--+--+--+------------------------+------+-------------+------+-----+
5011  */
5012 
5013 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
5014 {
5015     unsigned int sf, op, setflags, rm, rn, rd;
5016     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
5017 
5018     sf = extract32(insn, 31, 1);
5019     op = extract32(insn, 30, 1);
5020     setflags = extract32(insn, 29, 1);
5021     rm = extract32(insn, 16, 5);
5022     rn = extract32(insn, 5, 5);
5023     rd = extract32(insn, 0, 5);
5024 
5025     tcg_rd = cpu_reg(s, rd);
5026     tcg_rn = cpu_reg(s, rn);
5027 
5028     if (op) {
5029         tcg_y = tcg_temp_new_i64();
5030         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
5031     } else {
5032         tcg_y = cpu_reg(s, rm);
5033     }
5034 
5035     if (setflags) {
5036         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
5037     } else {
5038         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
5039     }
5040 }
5041 
5042 /*
5043  * Rotate right into flags
5044  *  31 30 29                21       15          10      5  4      0
5045  * +--+--+--+-----------------+--------+-----------+------+--+------+
5046  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
5047  * +--+--+--+-----------------+--------+-----------+------+--+------+
5048  */
5049 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
5050 {
5051     int mask = extract32(insn, 0, 4);
5052     int o2 = extract32(insn, 4, 1);
5053     int rn = extract32(insn, 5, 5);
5054     int imm6 = extract32(insn, 15, 6);
5055     int sf_op_s = extract32(insn, 29, 3);
5056     TCGv_i64 tcg_rn;
5057     TCGv_i32 nzcv;
5058 
5059     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
5060         unallocated_encoding(s);
5061         return;
5062     }
5063 
5064     tcg_rn = read_cpu_reg(s, rn, 1);
5065     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
5066 
5067     nzcv = tcg_temp_new_i32();
5068     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
5069 
5070     if (mask & 8) { /* N */
5071         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
5072     }
5073     if (mask & 4) { /* Z */
5074         tcg_gen_not_i32(cpu_ZF, nzcv);
5075         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
5076     }
5077     if (mask & 2) { /* C */
5078         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
5079     }
5080     if (mask & 1) { /* V */
5081         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
5082     }
5083 }
5084 
5085 /*
5086  * Evaluate into flags
5087  *  31 30 29                21        15   14        10      5  4      0
5088  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5089  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
5090  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5091  */
5092 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
5093 {
5094     int o3_mask = extract32(insn, 0, 5);
5095     int rn = extract32(insn, 5, 5);
5096     int o2 = extract32(insn, 15, 6);
5097     int sz = extract32(insn, 14, 1);
5098     int sf_op_s = extract32(insn, 29, 3);
5099     TCGv_i32 tmp;
5100     int shift;
5101 
5102     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
5103         !dc_isar_feature(aa64_condm_4, s)) {
5104         unallocated_encoding(s);
5105         return;
5106     }
5107     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
5108 
5109     tmp = tcg_temp_new_i32();
5110     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
5111     tcg_gen_shli_i32(cpu_NF, tmp, shift);
5112     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
5113     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
5114     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
5115 }
5116 
5117 /* Conditional compare (immediate / register)
5118  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
5119  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5120  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
5121  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5122  *        [1]                             y                [0]       [0]
5123  */
5124 static void disas_cc(DisasContext *s, uint32_t insn)
5125 {
5126     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
5127     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
5128     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
5129     DisasCompare c;
5130 
5131     if (!extract32(insn, 29, 1)) {
5132         unallocated_encoding(s);
5133         return;
5134     }
5135     if (insn & (1 << 10 | 1 << 4)) {
5136         unallocated_encoding(s);
5137         return;
5138     }
5139     sf = extract32(insn, 31, 1);
5140     op = extract32(insn, 30, 1);
5141     is_imm = extract32(insn, 11, 1);
5142     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
5143     cond = extract32(insn, 12, 4);
5144     rn = extract32(insn, 5, 5);
5145     nzcv = extract32(insn, 0, 4);
5146 
5147     /* Set T0 = !COND.  */
5148     tcg_t0 = tcg_temp_new_i32();
5149     arm_test_cc(&c, cond);
5150     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
5151 
5152     /* Load the arguments for the new comparison.  */
5153     if (is_imm) {
5154         tcg_y = tcg_temp_new_i64();
5155         tcg_gen_movi_i64(tcg_y, y);
5156     } else {
5157         tcg_y = cpu_reg(s, y);
5158     }
5159     tcg_rn = cpu_reg(s, rn);
5160 
5161     /* Set the flags for the new comparison.  */
5162     tcg_tmp = tcg_temp_new_i64();
5163     if (op) {
5164         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5165     } else {
5166         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5167     }
5168 
5169     /* If COND was false, force the flags to #nzcv.  Compute two masks
5170      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
5171      * For tcg hosts that support ANDC, we can make do with just T1.
5172      * In either case, allow the tcg optimizer to delete any unused mask.
5173      */
5174     tcg_t1 = tcg_temp_new_i32();
5175     tcg_t2 = tcg_temp_new_i32();
5176     tcg_gen_neg_i32(tcg_t1, tcg_t0);
5177     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
5178 
5179     if (nzcv & 8) { /* N */
5180         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
5181     } else {
5182         if (TCG_TARGET_HAS_andc_i32) {
5183             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
5184         } else {
5185             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
5186         }
5187     }
5188     if (nzcv & 4) { /* Z */
5189         if (TCG_TARGET_HAS_andc_i32) {
5190             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
5191         } else {
5192             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
5193         }
5194     } else {
5195         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
5196     }
5197     if (nzcv & 2) { /* C */
5198         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
5199     } else {
5200         if (TCG_TARGET_HAS_andc_i32) {
5201             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
5202         } else {
5203             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
5204         }
5205     }
5206     if (nzcv & 1) { /* V */
5207         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
5208     } else {
5209         if (TCG_TARGET_HAS_andc_i32) {
5210             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
5211         } else {
5212             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
5213         }
5214     }
5215 }
5216 
5217 /* Conditional select
5218  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
5219  * +----+----+---+-----------------+------+------+-----+------+------+
5220  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
5221  * +----+----+---+-----------------+------+------+-----+------+------+
5222  */
5223 static void disas_cond_select(DisasContext *s, uint32_t insn)
5224 {
5225     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
5226     TCGv_i64 tcg_rd, zero;
5227     DisasCompare64 c;
5228 
5229     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
5230         /* S == 1 or op2<1> == 1 */
5231         unallocated_encoding(s);
5232         return;
5233     }
5234     sf = extract32(insn, 31, 1);
5235     else_inv = extract32(insn, 30, 1);
5236     rm = extract32(insn, 16, 5);
5237     cond = extract32(insn, 12, 4);
5238     else_inc = extract32(insn, 10, 1);
5239     rn = extract32(insn, 5, 5);
5240     rd = extract32(insn, 0, 5);
5241 
5242     tcg_rd = cpu_reg(s, rd);
5243 
5244     a64_test_cc(&c, cond);
5245     zero = tcg_constant_i64(0);
5246 
5247     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
5248         /* CSET & CSETM.  */
5249         tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
5250         if (else_inv) {
5251             tcg_gen_neg_i64(tcg_rd, tcg_rd);
5252         }
5253     } else {
5254         TCGv_i64 t_true = cpu_reg(s, rn);
5255         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
5256         if (else_inv && else_inc) {
5257             tcg_gen_neg_i64(t_false, t_false);
5258         } else if (else_inv) {
5259             tcg_gen_not_i64(t_false, t_false);
5260         } else if (else_inc) {
5261             tcg_gen_addi_i64(t_false, t_false, 1);
5262         }
5263         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
5264     }
5265 
5266     if (!sf) {
5267         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5268     }
5269 }
5270 
5271 static void handle_clz(DisasContext *s, unsigned int sf,
5272                        unsigned int rn, unsigned int rd)
5273 {
5274     TCGv_i64 tcg_rd, tcg_rn;
5275     tcg_rd = cpu_reg(s, rd);
5276     tcg_rn = cpu_reg(s, rn);
5277 
5278     if (sf) {
5279         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
5280     } else {
5281         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5282         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5283         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
5284         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5285     }
5286 }
5287 
5288 static void handle_cls(DisasContext *s, unsigned int sf,
5289                        unsigned int rn, unsigned int rd)
5290 {
5291     TCGv_i64 tcg_rd, tcg_rn;
5292     tcg_rd = cpu_reg(s, rd);
5293     tcg_rn = cpu_reg(s, rn);
5294 
5295     if (sf) {
5296         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
5297     } else {
5298         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5299         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5300         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
5301         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5302     }
5303 }
5304 
5305 static void handle_rbit(DisasContext *s, unsigned int sf,
5306                         unsigned int rn, unsigned int rd)
5307 {
5308     TCGv_i64 tcg_rd, tcg_rn;
5309     tcg_rd = cpu_reg(s, rd);
5310     tcg_rn = cpu_reg(s, rn);
5311 
5312     if (sf) {
5313         gen_helper_rbit64(tcg_rd, tcg_rn);
5314     } else {
5315         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5316         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5317         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5318         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5319     }
5320 }
5321 
5322 /* REV with sf==1, opcode==3 ("REV64") */
5323 static void handle_rev64(DisasContext *s, unsigned int sf,
5324                          unsigned int rn, unsigned int rd)
5325 {
5326     if (!sf) {
5327         unallocated_encoding(s);
5328         return;
5329     }
5330     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5331 }
5332 
5333 /* REV with sf==0, opcode==2
5334  * REV32 (sf==1, opcode==2)
5335  */
5336 static void handle_rev32(DisasContext *s, unsigned int sf,
5337                          unsigned int rn, unsigned int rd)
5338 {
5339     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5340     TCGv_i64 tcg_rn = cpu_reg(s, rn);
5341 
5342     if (sf) {
5343         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
5344         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
5345     } else {
5346         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
5347     }
5348 }
5349 
5350 /* REV16 (opcode==1) */
5351 static void handle_rev16(DisasContext *s, unsigned int sf,
5352                          unsigned int rn, unsigned int rd)
5353 {
5354     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5355     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5356     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5357     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5358 
5359     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5360     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5361     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5362     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5363     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5364 }
5365 
5366 /* Data-processing (1 source)
5367  *   31  30  29  28             21 20     16 15    10 9    5 4    0
5368  * +----+---+---+-----------------+---------+--------+------+------+
5369  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
5370  * +----+---+---+-----------------+---------+--------+------+------+
5371  */
5372 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5373 {
5374     unsigned int sf, opcode, opcode2, rn, rd;
5375     TCGv_i64 tcg_rd;
5376 
5377     if (extract32(insn, 29, 1)) {
5378         unallocated_encoding(s);
5379         return;
5380     }
5381 
5382     sf = extract32(insn, 31, 1);
5383     opcode = extract32(insn, 10, 6);
5384     opcode2 = extract32(insn, 16, 5);
5385     rn = extract32(insn, 5, 5);
5386     rd = extract32(insn, 0, 5);
5387 
5388 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5389 
5390     switch (MAP(sf, opcode2, opcode)) {
5391     case MAP(0, 0x00, 0x00): /* RBIT */
5392     case MAP(1, 0x00, 0x00):
5393         handle_rbit(s, sf, rn, rd);
5394         break;
5395     case MAP(0, 0x00, 0x01): /* REV16 */
5396     case MAP(1, 0x00, 0x01):
5397         handle_rev16(s, sf, rn, rd);
5398         break;
5399     case MAP(0, 0x00, 0x02): /* REV/REV32 */
5400     case MAP(1, 0x00, 0x02):
5401         handle_rev32(s, sf, rn, rd);
5402         break;
5403     case MAP(1, 0x00, 0x03): /* REV64 */
5404         handle_rev64(s, sf, rn, rd);
5405         break;
5406     case MAP(0, 0x00, 0x04): /* CLZ */
5407     case MAP(1, 0x00, 0x04):
5408         handle_clz(s, sf, rn, rd);
5409         break;
5410     case MAP(0, 0x00, 0x05): /* CLS */
5411     case MAP(1, 0x00, 0x05):
5412         handle_cls(s, sf, rn, rd);
5413         break;
5414     case MAP(1, 0x01, 0x00): /* PACIA */
5415         if (s->pauth_active) {
5416             tcg_rd = cpu_reg(s, rd);
5417             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5418         } else if (!dc_isar_feature(aa64_pauth, s)) {
5419             goto do_unallocated;
5420         }
5421         break;
5422     case MAP(1, 0x01, 0x01): /* PACIB */
5423         if (s->pauth_active) {
5424             tcg_rd = cpu_reg(s, rd);
5425             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5426         } else if (!dc_isar_feature(aa64_pauth, s)) {
5427             goto do_unallocated;
5428         }
5429         break;
5430     case MAP(1, 0x01, 0x02): /* PACDA */
5431         if (s->pauth_active) {
5432             tcg_rd = cpu_reg(s, rd);
5433             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5434         } else if (!dc_isar_feature(aa64_pauth, s)) {
5435             goto do_unallocated;
5436         }
5437         break;
5438     case MAP(1, 0x01, 0x03): /* PACDB */
5439         if (s->pauth_active) {
5440             tcg_rd = cpu_reg(s, rd);
5441             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5442         } else if (!dc_isar_feature(aa64_pauth, s)) {
5443             goto do_unallocated;
5444         }
5445         break;
5446     case MAP(1, 0x01, 0x04): /* AUTIA */
5447         if (s->pauth_active) {
5448             tcg_rd = cpu_reg(s, rd);
5449             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5450         } else if (!dc_isar_feature(aa64_pauth, s)) {
5451             goto do_unallocated;
5452         }
5453         break;
5454     case MAP(1, 0x01, 0x05): /* AUTIB */
5455         if (s->pauth_active) {
5456             tcg_rd = cpu_reg(s, rd);
5457             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5458         } else if (!dc_isar_feature(aa64_pauth, s)) {
5459             goto do_unallocated;
5460         }
5461         break;
5462     case MAP(1, 0x01, 0x06): /* AUTDA */
5463         if (s->pauth_active) {
5464             tcg_rd = cpu_reg(s, rd);
5465             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5466         } else if (!dc_isar_feature(aa64_pauth, s)) {
5467             goto do_unallocated;
5468         }
5469         break;
5470     case MAP(1, 0x01, 0x07): /* AUTDB */
5471         if (s->pauth_active) {
5472             tcg_rd = cpu_reg(s, rd);
5473             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5474         } else if (!dc_isar_feature(aa64_pauth, s)) {
5475             goto do_unallocated;
5476         }
5477         break;
5478     case MAP(1, 0x01, 0x08): /* PACIZA */
5479         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5480             goto do_unallocated;
5481         } else if (s->pauth_active) {
5482             tcg_rd = cpu_reg(s, rd);
5483             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5484         }
5485         break;
5486     case MAP(1, 0x01, 0x09): /* PACIZB */
5487         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5488             goto do_unallocated;
5489         } else if (s->pauth_active) {
5490             tcg_rd = cpu_reg(s, rd);
5491             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5492         }
5493         break;
5494     case MAP(1, 0x01, 0x0a): /* PACDZA */
5495         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5496             goto do_unallocated;
5497         } else if (s->pauth_active) {
5498             tcg_rd = cpu_reg(s, rd);
5499             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5500         }
5501         break;
5502     case MAP(1, 0x01, 0x0b): /* PACDZB */
5503         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5504             goto do_unallocated;
5505         } else if (s->pauth_active) {
5506             tcg_rd = cpu_reg(s, rd);
5507             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5508         }
5509         break;
5510     case MAP(1, 0x01, 0x0c): /* AUTIZA */
5511         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5512             goto do_unallocated;
5513         } else if (s->pauth_active) {
5514             tcg_rd = cpu_reg(s, rd);
5515             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5516         }
5517         break;
5518     case MAP(1, 0x01, 0x0d): /* AUTIZB */
5519         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5520             goto do_unallocated;
5521         } else if (s->pauth_active) {
5522             tcg_rd = cpu_reg(s, rd);
5523             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5524         }
5525         break;
5526     case MAP(1, 0x01, 0x0e): /* AUTDZA */
5527         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5528             goto do_unallocated;
5529         } else if (s->pauth_active) {
5530             tcg_rd = cpu_reg(s, rd);
5531             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5532         }
5533         break;
5534     case MAP(1, 0x01, 0x0f): /* AUTDZB */
5535         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5536             goto do_unallocated;
5537         } else if (s->pauth_active) {
5538             tcg_rd = cpu_reg(s, rd);
5539             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5540         }
5541         break;
5542     case MAP(1, 0x01, 0x10): /* XPACI */
5543         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5544             goto do_unallocated;
5545         } else if (s->pauth_active) {
5546             tcg_rd = cpu_reg(s, rd);
5547             gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
5548         }
5549         break;
5550     case MAP(1, 0x01, 0x11): /* XPACD */
5551         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5552             goto do_unallocated;
5553         } else if (s->pauth_active) {
5554             tcg_rd = cpu_reg(s, rd);
5555             gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5556         }
5557         break;
5558     default:
5559     do_unallocated:
5560         unallocated_encoding(s);
5561         break;
5562     }
5563 
5564 #undef MAP
5565 }
5566 
5567 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5568                        unsigned int rm, unsigned int rn, unsigned int rd)
5569 {
5570     TCGv_i64 tcg_n, tcg_m, tcg_rd;
5571     tcg_rd = cpu_reg(s, rd);
5572 
5573     if (!sf && is_signed) {
5574         tcg_n = tcg_temp_new_i64();
5575         tcg_m = tcg_temp_new_i64();
5576         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5577         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5578     } else {
5579         tcg_n = read_cpu_reg(s, rn, sf);
5580         tcg_m = read_cpu_reg(s, rm, sf);
5581     }
5582 
5583     if (is_signed) {
5584         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5585     } else {
5586         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5587     }
5588 
5589     if (!sf) { /* zero extend final result */
5590         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5591     }
5592 }
5593 
5594 /* LSLV, LSRV, ASRV, RORV */
5595 static void handle_shift_reg(DisasContext *s,
5596                              enum a64_shift_type shift_type, unsigned int sf,
5597                              unsigned int rm, unsigned int rn, unsigned int rd)
5598 {
5599     TCGv_i64 tcg_shift = tcg_temp_new_i64();
5600     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5601     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5602 
5603     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5604     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5605 }
5606 
5607 /* CRC32[BHWX], CRC32C[BHWX] */
5608 static void handle_crc32(DisasContext *s,
5609                          unsigned int sf, unsigned int sz, bool crc32c,
5610                          unsigned int rm, unsigned int rn, unsigned int rd)
5611 {
5612     TCGv_i64 tcg_acc, tcg_val;
5613     TCGv_i32 tcg_bytes;
5614 
5615     if (!dc_isar_feature(aa64_crc32, s)
5616         || (sf == 1 && sz != 3)
5617         || (sf == 0 && sz == 3)) {
5618         unallocated_encoding(s);
5619         return;
5620     }
5621 
5622     if (sz == 3) {
5623         tcg_val = cpu_reg(s, rm);
5624     } else {
5625         uint64_t mask;
5626         switch (sz) {
5627         case 0:
5628             mask = 0xFF;
5629             break;
5630         case 1:
5631             mask = 0xFFFF;
5632             break;
5633         case 2:
5634             mask = 0xFFFFFFFF;
5635             break;
5636         default:
5637             g_assert_not_reached();
5638         }
5639         tcg_val = tcg_temp_new_i64();
5640         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5641     }
5642 
5643     tcg_acc = cpu_reg(s, rn);
5644     tcg_bytes = tcg_constant_i32(1 << sz);
5645 
5646     if (crc32c) {
5647         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5648     } else {
5649         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5650     }
5651 }
5652 
5653 /* Data-processing (2 source)
5654  *   31   30  29 28             21 20  16 15    10 9    5 4    0
5655  * +----+---+---+-----------------+------+--------+------+------+
5656  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
5657  * +----+---+---+-----------------+------+--------+------+------+
5658  */
5659 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5660 {
5661     unsigned int sf, rm, opcode, rn, rd, setflag;
5662     sf = extract32(insn, 31, 1);
5663     setflag = extract32(insn, 29, 1);
5664     rm = extract32(insn, 16, 5);
5665     opcode = extract32(insn, 10, 6);
5666     rn = extract32(insn, 5, 5);
5667     rd = extract32(insn, 0, 5);
5668 
5669     if (setflag && opcode != 0) {
5670         unallocated_encoding(s);
5671         return;
5672     }
5673 
5674     switch (opcode) {
5675     case 0: /* SUBP(S) */
5676         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5677             goto do_unallocated;
5678         } else {
5679             TCGv_i64 tcg_n, tcg_m, tcg_d;
5680 
5681             tcg_n = read_cpu_reg_sp(s, rn, true);
5682             tcg_m = read_cpu_reg_sp(s, rm, true);
5683             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5684             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5685             tcg_d = cpu_reg(s, rd);
5686 
5687             if (setflag) {
5688                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5689             } else {
5690                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5691             }
5692         }
5693         break;
5694     case 2: /* UDIV */
5695         handle_div(s, false, sf, rm, rn, rd);
5696         break;
5697     case 3: /* SDIV */
5698         handle_div(s, true, sf, rm, rn, rd);
5699         break;
5700     case 4: /* IRG */
5701         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5702             goto do_unallocated;
5703         }
5704         if (s->ata) {
5705             gen_helper_irg(cpu_reg_sp(s, rd), cpu_env,
5706                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
5707         } else {
5708             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5709                                              cpu_reg_sp(s, rn));
5710         }
5711         break;
5712     case 5: /* GMI */
5713         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5714             goto do_unallocated;
5715         } else {
5716             TCGv_i64 t = tcg_temp_new_i64();
5717 
5718             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
5719             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
5720             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
5721         }
5722         break;
5723     case 8: /* LSLV */
5724         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5725         break;
5726     case 9: /* LSRV */
5727         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5728         break;
5729     case 10: /* ASRV */
5730         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5731         break;
5732     case 11: /* RORV */
5733         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5734         break;
5735     case 12: /* PACGA */
5736         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5737             goto do_unallocated;
5738         }
5739         gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5740                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
5741         break;
5742     case 16:
5743     case 17:
5744     case 18:
5745     case 19:
5746     case 20:
5747     case 21:
5748     case 22:
5749     case 23: /* CRC32 */
5750     {
5751         int sz = extract32(opcode, 0, 2);
5752         bool crc32c = extract32(opcode, 2, 1);
5753         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5754         break;
5755     }
5756     default:
5757     do_unallocated:
5758         unallocated_encoding(s);
5759         break;
5760     }
5761 }
5762 
5763 /*
5764  * Data processing - register
5765  *  31  30 29  28      25    21  20  16      10         0
5766  * +--+---+--+---+-------+-----+-------+-------+---------+
5767  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
5768  * +--+---+--+---+-------+-----+-------+-------+---------+
5769  */
5770 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5771 {
5772     int op0 = extract32(insn, 30, 1);
5773     int op1 = extract32(insn, 28, 1);
5774     int op2 = extract32(insn, 21, 4);
5775     int op3 = extract32(insn, 10, 6);
5776 
5777     if (!op1) {
5778         if (op2 & 8) {
5779             if (op2 & 1) {
5780                 /* Add/sub (extended register) */
5781                 disas_add_sub_ext_reg(s, insn);
5782             } else {
5783                 /* Add/sub (shifted register) */
5784                 disas_add_sub_reg(s, insn);
5785             }
5786         } else {
5787             /* Logical (shifted register) */
5788             disas_logic_reg(s, insn);
5789         }
5790         return;
5791     }
5792 
5793     switch (op2) {
5794     case 0x0:
5795         switch (op3) {
5796         case 0x00: /* Add/subtract (with carry) */
5797             disas_adc_sbc(s, insn);
5798             break;
5799 
5800         case 0x01: /* Rotate right into flags */
5801         case 0x21:
5802             disas_rotate_right_into_flags(s, insn);
5803             break;
5804 
5805         case 0x02: /* Evaluate into flags */
5806         case 0x12:
5807         case 0x22:
5808         case 0x32:
5809             disas_evaluate_into_flags(s, insn);
5810             break;
5811 
5812         default:
5813             goto do_unallocated;
5814         }
5815         break;
5816 
5817     case 0x2: /* Conditional compare */
5818         disas_cc(s, insn); /* both imm and reg forms */
5819         break;
5820 
5821     case 0x4: /* Conditional select */
5822         disas_cond_select(s, insn);
5823         break;
5824 
5825     case 0x6: /* Data-processing */
5826         if (op0) {    /* (1 source) */
5827             disas_data_proc_1src(s, insn);
5828         } else {      /* (2 source) */
5829             disas_data_proc_2src(s, insn);
5830         }
5831         break;
5832     case 0x8 ... 0xf: /* (3 source) */
5833         disas_data_proc_3src(s, insn);
5834         break;
5835 
5836     default:
5837     do_unallocated:
5838         unallocated_encoding(s);
5839         break;
5840     }
5841 }
5842 
5843 static void handle_fp_compare(DisasContext *s, int size,
5844                               unsigned int rn, unsigned int rm,
5845                               bool cmp_with_zero, bool signal_all_nans)
5846 {
5847     TCGv_i64 tcg_flags = tcg_temp_new_i64();
5848     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
5849 
5850     if (size == MO_64) {
5851         TCGv_i64 tcg_vn, tcg_vm;
5852 
5853         tcg_vn = read_fp_dreg(s, rn);
5854         if (cmp_with_zero) {
5855             tcg_vm = tcg_constant_i64(0);
5856         } else {
5857             tcg_vm = read_fp_dreg(s, rm);
5858         }
5859         if (signal_all_nans) {
5860             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5861         } else {
5862             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5863         }
5864     } else {
5865         TCGv_i32 tcg_vn = tcg_temp_new_i32();
5866         TCGv_i32 tcg_vm = tcg_temp_new_i32();
5867 
5868         read_vec_element_i32(s, tcg_vn, rn, 0, size);
5869         if (cmp_with_zero) {
5870             tcg_gen_movi_i32(tcg_vm, 0);
5871         } else {
5872             read_vec_element_i32(s, tcg_vm, rm, 0, size);
5873         }
5874 
5875         switch (size) {
5876         case MO_32:
5877             if (signal_all_nans) {
5878                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5879             } else {
5880                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5881             }
5882             break;
5883         case MO_16:
5884             if (signal_all_nans) {
5885                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5886             } else {
5887                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5888             }
5889             break;
5890         default:
5891             g_assert_not_reached();
5892         }
5893     }
5894 
5895     gen_set_nzcv(tcg_flags);
5896 }
5897 
5898 /* Floating point compare
5899  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
5900  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5901  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
5902  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5903  */
5904 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5905 {
5906     unsigned int mos, type, rm, op, rn, opc, op2r;
5907     int size;
5908 
5909     mos = extract32(insn, 29, 3);
5910     type = extract32(insn, 22, 2);
5911     rm = extract32(insn, 16, 5);
5912     op = extract32(insn, 14, 2);
5913     rn = extract32(insn, 5, 5);
5914     opc = extract32(insn, 3, 2);
5915     op2r = extract32(insn, 0, 3);
5916 
5917     if (mos || op || op2r) {
5918         unallocated_encoding(s);
5919         return;
5920     }
5921 
5922     switch (type) {
5923     case 0:
5924         size = MO_32;
5925         break;
5926     case 1:
5927         size = MO_64;
5928         break;
5929     case 3:
5930         size = MO_16;
5931         if (dc_isar_feature(aa64_fp16, s)) {
5932             break;
5933         }
5934         /* fallthru */
5935     default:
5936         unallocated_encoding(s);
5937         return;
5938     }
5939 
5940     if (!fp_access_check(s)) {
5941         return;
5942     }
5943 
5944     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
5945 }
5946 
5947 /* Floating point conditional compare
5948  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
5949  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5950  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
5951  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5952  */
5953 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
5954 {
5955     unsigned int mos, type, rm, cond, rn, op, nzcv;
5956     TCGLabel *label_continue = NULL;
5957     int size;
5958 
5959     mos = extract32(insn, 29, 3);
5960     type = extract32(insn, 22, 2);
5961     rm = extract32(insn, 16, 5);
5962     cond = extract32(insn, 12, 4);
5963     rn = extract32(insn, 5, 5);
5964     op = extract32(insn, 4, 1);
5965     nzcv = extract32(insn, 0, 4);
5966 
5967     if (mos) {
5968         unallocated_encoding(s);
5969         return;
5970     }
5971 
5972     switch (type) {
5973     case 0:
5974         size = MO_32;
5975         break;
5976     case 1:
5977         size = MO_64;
5978         break;
5979     case 3:
5980         size = MO_16;
5981         if (dc_isar_feature(aa64_fp16, s)) {
5982             break;
5983         }
5984         /* fallthru */
5985     default:
5986         unallocated_encoding(s);
5987         return;
5988     }
5989 
5990     if (!fp_access_check(s)) {
5991         return;
5992     }
5993 
5994     if (cond < 0x0e) { /* not always */
5995         TCGLabel *label_match = gen_new_label();
5996         label_continue = gen_new_label();
5997         arm_gen_test_cc(cond, label_match);
5998         /* nomatch: */
5999         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
6000         tcg_gen_br(label_continue);
6001         gen_set_label(label_match);
6002     }
6003 
6004     handle_fp_compare(s, size, rn, rm, false, op);
6005 
6006     if (cond < 0x0e) {
6007         gen_set_label(label_continue);
6008     }
6009 }
6010 
6011 /* Floating point conditional select
6012  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
6013  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6014  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
6015  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6016  */
6017 static void disas_fp_csel(DisasContext *s, uint32_t insn)
6018 {
6019     unsigned int mos, type, rm, cond, rn, rd;
6020     TCGv_i64 t_true, t_false;
6021     DisasCompare64 c;
6022     MemOp sz;
6023 
6024     mos = extract32(insn, 29, 3);
6025     type = extract32(insn, 22, 2);
6026     rm = extract32(insn, 16, 5);
6027     cond = extract32(insn, 12, 4);
6028     rn = extract32(insn, 5, 5);
6029     rd = extract32(insn, 0, 5);
6030 
6031     if (mos) {
6032         unallocated_encoding(s);
6033         return;
6034     }
6035 
6036     switch (type) {
6037     case 0:
6038         sz = MO_32;
6039         break;
6040     case 1:
6041         sz = MO_64;
6042         break;
6043     case 3:
6044         sz = MO_16;
6045         if (dc_isar_feature(aa64_fp16, s)) {
6046             break;
6047         }
6048         /* fallthru */
6049     default:
6050         unallocated_encoding(s);
6051         return;
6052     }
6053 
6054     if (!fp_access_check(s)) {
6055         return;
6056     }
6057 
6058     /* Zero extend sreg & hreg inputs to 64 bits now.  */
6059     t_true = tcg_temp_new_i64();
6060     t_false = tcg_temp_new_i64();
6061     read_vec_element(s, t_true, rn, 0, sz);
6062     read_vec_element(s, t_false, rm, 0, sz);
6063 
6064     a64_test_cc(&c, cond);
6065     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
6066                         t_true, t_false);
6067 
6068     /* Note that sregs & hregs write back zeros to the high bits,
6069        and we've already done the zero-extension.  */
6070     write_fp_dreg(s, rd, t_true);
6071 }
6072 
6073 /* Floating-point data-processing (1 source) - half precision */
6074 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
6075 {
6076     TCGv_ptr fpst = NULL;
6077     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
6078     TCGv_i32 tcg_res = tcg_temp_new_i32();
6079 
6080     switch (opcode) {
6081     case 0x0: /* FMOV */
6082         tcg_gen_mov_i32(tcg_res, tcg_op);
6083         break;
6084     case 0x1: /* FABS */
6085         tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
6086         break;
6087     case 0x2: /* FNEG */
6088         tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
6089         break;
6090     case 0x3: /* FSQRT */
6091         fpst = fpstatus_ptr(FPST_FPCR_F16);
6092         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
6093         break;
6094     case 0x8: /* FRINTN */
6095     case 0x9: /* FRINTP */
6096     case 0xa: /* FRINTM */
6097     case 0xb: /* FRINTZ */
6098     case 0xc: /* FRINTA */
6099     {
6100         TCGv_i32 tcg_rmode;
6101 
6102         fpst = fpstatus_ptr(FPST_FPCR_F16);
6103         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
6104         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6105         gen_restore_rmode(tcg_rmode, fpst);
6106         break;
6107     }
6108     case 0xe: /* FRINTX */
6109         fpst = fpstatus_ptr(FPST_FPCR_F16);
6110         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
6111         break;
6112     case 0xf: /* FRINTI */
6113         fpst = fpstatus_ptr(FPST_FPCR_F16);
6114         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6115         break;
6116     default:
6117         g_assert_not_reached();
6118     }
6119 
6120     write_fp_sreg(s, rd, tcg_res);
6121 }
6122 
6123 /* Floating-point data-processing (1 source) - single precision */
6124 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
6125 {
6126     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
6127     TCGv_i32 tcg_op, tcg_res;
6128     TCGv_ptr fpst;
6129     int rmode = -1;
6130 
6131     tcg_op = read_fp_sreg(s, rn);
6132     tcg_res = tcg_temp_new_i32();
6133 
6134     switch (opcode) {
6135     case 0x0: /* FMOV */
6136         tcg_gen_mov_i32(tcg_res, tcg_op);
6137         goto done;
6138     case 0x1: /* FABS */
6139         gen_helper_vfp_abss(tcg_res, tcg_op);
6140         goto done;
6141     case 0x2: /* FNEG */
6142         gen_helper_vfp_negs(tcg_res, tcg_op);
6143         goto done;
6144     case 0x3: /* FSQRT */
6145         gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
6146         goto done;
6147     case 0x6: /* BFCVT */
6148         gen_fpst = gen_helper_bfcvt;
6149         break;
6150     case 0x8: /* FRINTN */
6151     case 0x9: /* FRINTP */
6152     case 0xa: /* FRINTM */
6153     case 0xb: /* FRINTZ */
6154     case 0xc: /* FRINTA */
6155         rmode = opcode & 7;
6156         gen_fpst = gen_helper_rints;
6157         break;
6158     case 0xe: /* FRINTX */
6159         gen_fpst = gen_helper_rints_exact;
6160         break;
6161     case 0xf: /* FRINTI */
6162         gen_fpst = gen_helper_rints;
6163         break;
6164     case 0x10: /* FRINT32Z */
6165         rmode = FPROUNDING_ZERO;
6166         gen_fpst = gen_helper_frint32_s;
6167         break;
6168     case 0x11: /* FRINT32X */
6169         gen_fpst = gen_helper_frint32_s;
6170         break;
6171     case 0x12: /* FRINT64Z */
6172         rmode = FPROUNDING_ZERO;
6173         gen_fpst = gen_helper_frint64_s;
6174         break;
6175     case 0x13: /* FRINT64X */
6176         gen_fpst = gen_helper_frint64_s;
6177         break;
6178     default:
6179         g_assert_not_reached();
6180     }
6181 
6182     fpst = fpstatus_ptr(FPST_FPCR);
6183     if (rmode >= 0) {
6184         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6185         gen_fpst(tcg_res, tcg_op, fpst);
6186         gen_restore_rmode(tcg_rmode, fpst);
6187     } else {
6188         gen_fpst(tcg_res, tcg_op, fpst);
6189     }
6190 
6191  done:
6192     write_fp_sreg(s, rd, tcg_res);
6193 }
6194 
6195 /* Floating-point data-processing (1 source) - double precision */
6196 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6197 {
6198     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6199     TCGv_i64 tcg_op, tcg_res;
6200     TCGv_ptr fpst;
6201     int rmode = -1;
6202 
6203     switch (opcode) {
6204     case 0x0: /* FMOV */
6205         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6206         return;
6207     }
6208 
6209     tcg_op = read_fp_dreg(s, rn);
6210     tcg_res = tcg_temp_new_i64();
6211 
6212     switch (opcode) {
6213     case 0x1: /* FABS */
6214         gen_helper_vfp_absd(tcg_res, tcg_op);
6215         goto done;
6216     case 0x2: /* FNEG */
6217         gen_helper_vfp_negd(tcg_res, tcg_op);
6218         goto done;
6219     case 0x3: /* FSQRT */
6220         gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
6221         goto done;
6222     case 0x8: /* FRINTN */
6223     case 0x9: /* FRINTP */
6224     case 0xa: /* FRINTM */
6225     case 0xb: /* FRINTZ */
6226     case 0xc: /* FRINTA */
6227         rmode = opcode & 7;
6228         gen_fpst = gen_helper_rintd;
6229         break;
6230     case 0xe: /* FRINTX */
6231         gen_fpst = gen_helper_rintd_exact;
6232         break;
6233     case 0xf: /* FRINTI */
6234         gen_fpst = gen_helper_rintd;
6235         break;
6236     case 0x10: /* FRINT32Z */
6237         rmode = FPROUNDING_ZERO;
6238         gen_fpst = gen_helper_frint32_d;
6239         break;
6240     case 0x11: /* FRINT32X */
6241         gen_fpst = gen_helper_frint32_d;
6242         break;
6243     case 0x12: /* FRINT64Z */
6244         rmode = FPROUNDING_ZERO;
6245         gen_fpst = gen_helper_frint64_d;
6246         break;
6247     case 0x13: /* FRINT64X */
6248         gen_fpst = gen_helper_frint64_d;
6249         break;
6250     default:
6251         g_assert_not_reached();
6252     }
6253 
6254     fpst = fpstatus_ptr(FPST_FPCR);
6255     if (rmode >= 0) {
6256         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6257         gen_fpst(tcg_res, tcg_op, fpst);
6258         gen_restore_rmode(tcg_rmode, fpst);
6259     } else {
6260         gen_fpst(tcg_res, tcg_op, fpst);
6261     }
6262 
6263  done:
6264     write_fp_dreg(s, rd, tcg_res);
6265 }
6266 
6267 static void handle_fp_fcvt(DisasContext *s, int opcode,
6268                            int rd, int rn, int dtype, int ntype)
6269 {
6270     switch (ntype) {
6271     case 0x0:
6272     {
6273         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6274         if (dtype == 1) {
6275             /* Single to double */
6276             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6277             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
6278             write_fp_dreg(s, rd, tcg_rd);
6279         } else {
6280             /* Single to half */
6281             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6282             TCGv_i32 ahp = get_ahp_flag();
6283             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6284 
6285             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6286             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6287             write_fp_sreg(s, rd, tcg_rd);
6288         }
6289         break;
6290     }
6291     case 0x1:
6292     {
6293         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6294         TCGv_i32 tcg_rd = tcg_temp_new_i32();
6295         if (dtype == 0) {
6296             /* Double to single */
6297             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
6298         } else {
6299             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6300             TCGv_i32 ahp = get_ahp_flag();
6301             /* Double to half */
6302             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6303             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6304         }
6305         write_fp_sreg(s, rd, tcg_rd);
6306         break;
6307     }
6308     case 0x3:
6309     {
6310         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6311         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6312         TCGv_i32 tcg_ahp = get_ahp_flag();
6313         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6314         if (dtype == 0) {
6315             /* Half to single */
6316             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6317             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6318             write_fp_sreg(s, rd, tcg_rd);
6319         } else {
6320             /* Half to double */
6321             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6322             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6323             write_fp_dreg(s, rd, tcg_rd);
6324         }
6325         break;
6326     }
6327     default:
6328         g_assert_not_reached();
6329     }
6330 }
6331 
6332 /* Floating point data-processing (1 source)
6333  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
6334  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6335  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
6336  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6337  */
6338 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6339 {
6340     int mos = extract32(insn, 29, 3);
6341     int type = extract32(insn, 22, 2);
6342     int opcode = extract32(insn, 15, 6);
6343     int rn = extract32(insn, 5, 5);
6344     int rd = extract32(insn, 0, 5);
6345 
6346     if (mos) {
6347         goto do_unallocated;
6348     }
6349 
6350     switch (opcode) {
6351     case 0x4: case 0x5: case 0x7:
6352     {
6353         /* FCVT between half, single and double precision */
6354         int dtype = extract32(opcode, 0, 2);
6355         if (type == 2 || dtype == type) {
6356             goto do_unallocated;
6357         }
6358         if (!fp_access_check(s)) {
6359             return;
6360         }
6361 
6362         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6363         break;
6364     }
6365 
6366     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6367         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6368             goto do_unallocated;
6369         }
6370         /* fall through */
6371     case 0x0 ... 0x3:
6372     case 0x8 ... 0xc:
6373     case 0xe ... 0xf:
6374         /* 32-to-32 and 64-to-64 ops */
6375         switch (type) {
6376         case 0:
6377             if (!fp_access_check(s)) {
6378                 return;
6379             }
6380             handle_fp_1src_single(s, opcode, rd, rn);
6381             break;
6382         case 1:
6383             if (!fp_access_check(s)) {
6384                 return;
6385             }
6386             handle_fp_1src_double(s, opcode, rd, rn);
6387             break;
6388         case 3:
6389             if (!dc_isar_feature(aa64_fp16, s)) {
6390                 goto do_unallocated;
6391             }
6392 
6393             if (!fp_access_check(s)) {
6394                 return;
6395             }
6396             handle_fp_1src_half(s, opcode, rd, rn);
6397             break;
6398         default:
6399             goto do_unallocated;
6400         }
6401         break;
6402 
6403     case 0x6:
6404         switch (type) {
6405         case 1: /* BFCVT */
6406             if (!dc_isar_feature(aa64_bf16, s)) {
6407                 goto do_unallocated;
6408             }
6409             if (!fp_access_check(s)) {
6410                 return;
6411             }
6412             handle_fp_1src_single(s, opcode, rd, rn);
6413             break;
6414         default:
6415             goto do_unallocated;
6416         }
6417         break;
6418 
6419     default:
6420     do_unallocated:
6421         unallocated_encoding(s);
6422         break;
6423     }
6424 }
6425 
6426 /* Floating-point data-processing (2 source) - single precision */
6427 static void handle_fp_2src_single(DisasContext *s, int opcode,
6428                                   int rd, int rn, int rm)
6429 {
6430     TCGv_i32 tcg_op1;
6431     TCGv_i32 tcg_op2;
6432     TCGv_i32 tcg_res;
6433     TCGv_ptr fpst;
6434 
6435     tcg_res = tcg_temp_new_i32();
6436     fpst = fpstatus_ptr(FPST_FPCR);
6437     tcg_op1 = read_fp_sreg(s, rn);
6438     tcg_op2 = read_fp_sreg(s, rm);
6439 
6440     switch (opcode) {
6441     case 0x0: /* FMUL */
6442         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6443         break;
6444     case 0x1: /* FDIV */
6445         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6446         break;
6447     case 0x2: /* FADD */
6448         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6449         break;
6450     case 0x3: /* FSUB */
6451         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6452         break;
6453     case 0x4: /* FMAX */
6454         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6455         break;
6456     case 0x5: /* FMIN */
6457         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6458         break;
6459     case 0x6: /* FMAXNM */
6460         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6461         break;
6462     case 0x7: /* FMINNM */
6463         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6464         break;
6465     case 0x8: /* FNMUL */
6466         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6467         gen_helper_vfp_negs(tcg_res, tcg_res);
6468         break;
6469     }
6470 
6471     write_fp_sreg(s, rd, tcg_res);
6472 }
6473 
6474 /* Floating-point data-processing (2 source) - double precision */
6475 static void handle_fp_2src_double(DisasContext *s, int opcode,
6476                                   int rd, int rn, int rm)
6477 {
6478     TCGv_i64 tcg_op1;
6479     TCGv_i64 tcg_op2;
6480     TCGv_i64 tcg_res;
6481     TCGv_ptr fpst;
6482 
6483     tcg_res = tcg_temp_new_i64();
6484     fpst = fpstatus_ptr(FPST_FPCR);
6485     tcg_op1 = read_fp_dreg(s, rn);
6486     tcg_op2 = read_fp_dreg(s, rm);
6487 
6488     switch (opcode) {
6489     case 0x0: /* FMUL */
6490         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6491         break;
6492     case 0x1: /* FDIV */
6493         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6494         break;
6495     case 0x2: /* FADD */
6496         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6497         break;
6498     case 0x3: /* FSUB */
6499         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6500         break;
6501     case 0x4: /* FMAX */
6502         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6503         break;
6504     case 0x5: /* FMIN */
6505         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6506         break;
6507     case 0x6: /* FMAXNM */
6508         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6509         break;
6510     case 0x7: /* FMINNM */
6511         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6512         break;
6513     case 0x8: /* FNMUL */
6514         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6515         gen_helper_vfp_negd(tcg_res, tcg_res);
6516         break;
6517     }
6518 
6519     write_fp_dreg(s, rd, tcg_res);
6520 }
6521 
6522 /* Floating-point data-processing (2 source) - half precision */
6523 static void handle_fp_2src_half(DisasContext *s, int opcode,
6524                                 int rd, int rn, int rm)
6525 {
6526     TCGv_i32 tcg_op1;
6527     TCGv_i32 tcg_op2;
6528     TCGv_i32 tcg_res;
6529     TCGv_ptr fpst;
6530 
6531     tcg_res = tcg_temp_new_i32();
6532     fpst = fpstatus_ptr(FPST_FPCR_F16);
6533     tcg_op1 = read_fp_hreg(s, rn);
6534     tcg_op2 = read_fp_hreg(s, rm);
6535 
6536     switch (opcode) {
6537     case 0x0: /* FMUL */
6538         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6539         break;
6540     case 0x1: /* FDIV */
6541         gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6542         break;
6543     case 0x2: /* FADD */
6544         gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6545         break;
6546     case 0x3: /* FSUB */
6547         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6548         break;
6549     case 0x4: /* FMAX */
6550         gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6551         break;
6552     case 0x5: /* FMIN */
6553         gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6554         break;
6555     case 0x6: /* FMAXNM */
6556         gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6557         break;
6558     case 0x7: /* FMINNM */
6559         gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6560         break;
6561     case 0x8: /* FNMUL */
6562         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6563         tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6564         break;
6565     default:
6566         g_assert_not_reached();
6567     }
6568 
6569     write_fp_sreg(s, rd, tcg_res);
6570 }
6571 
6572 /* Floating point data-processing (2 source)
6573  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
6574  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6575  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
6576  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6577  */
6578 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6579 {
6580     int mos = extract32(insn, 29, 3);
6581     int type = extract32(insn, 22, 2);
6582     int rd = extract32(insn, 0, 5);
6583     int rn = extract32(insn, 5, 5);
6584     int rm = extract32(insn, 16, 5);
6585     int opcode = extract32(insn, 12, 4);
6586 
6587     if (opcode > 8 || mos) {
6588         unallocated_encoding(s);
6589         return;
6590     }
6591 
6592     switch (type) {
6593     case 0:
6594         if (!fp_access_check(s)) {
6595             return;
6596         }
6597         handle_fp_2src_single(s, opcode, rd, rn, rm);
6598         break;
6599     case 1:
6600         if (!fp_access_check(s)) {
6601             return;
6602         }
6603         handle_fp_2src_double(s, opcode, rd, rn, rm);
6604         break;
6605     case 3:
6606         if (!dc_isar_feature(aa64_fp16, s)) {
6607             unallocated_encoding(s);
6608             return;
6609         }
6610         if (!fp_access_check(s)) {
6611             return;
6612         }
6613         handle_fp_2src_half(s, opcode, rd, rn, rm);
6614         break;
6615     default:
6616         unallocated_encoding(s);
6617     }
6618 }
6619 
6620 /* Floating-point data-processing (3 source) - single precision */
6621 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6622                                   int rd, int rn, int rm, int ra)
6623 {
6624     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6625     TCGv_i32 tcg_res = tcg_temp_new_i32();
6626     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6627 
6628     tcg_op1 = read_fp_sreg(s, rn);
6629     tcg_op2 = read_fp_sreg(s, rm);
6630     tcg_op3 = read_fp_sreg(s, ra);
6631 
6632     /* These are fused multiply-add, and must be done as one
6633      * floating point operation with no rounding between the
6634      * multiplication and addition steps.
6635      * NB that doing the negations here as separate steps is
6636      * correct : an input NaN should come out with its sign bit
6637      * flipped if it is a negated-input.
6638      */
6639     if (o1 == true) {
6640         gen_helper_vfp_negs(tcg_op3, tcg_op3);
6641     }
6642 
6643     if (o0 != o1) {
6644         gen_helper_vfp_negs(tcg_op1, tcg_op1);
6645     }
6646 
6647     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6648 
6649     write_fp_sreg(s, rd, tcg_res);
6650 }
6651 
6652 /* Floating-point data-processing (3 source) - double precision */
6653 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6654                                   int rd, int rn, int rm, int ra)
6655 {
6656     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6657     TCGv_i64 tcg_res = tcg_temp_new_i64();
6658     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6659 
6660     tcg_op1 = read_fp_dreg(s, rn);
6661     tcg_op2 = read_fp_dreg(s, rm);
6662     tcg_op3 = read_fp_dreg(s, ra);
6663 
6664     /* These are fused multiply-add, and must be done as one
6665      * floating point operation with no rounding between the
6666      * multiplication and addition steps.
6667      * NB that doing the negations here as separate steps is
6668      * correct : an input NaN should come out with its sign bit
6669      * flipped if it is a negated-input.
6670      */
6671     if (o1 == true) {
6672         gen_helper_vfp_negd(tcg_op3, tcg_op3);
6673     }
6674 
6675     if (o0 != o1) {
6676         gen_helper_vfp_negd(tcg_op1, tcg_op1);
6677     }
6678 
6679     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6680 
6681     write_fp_dreg(s, rd, tcg_res);
6682 }
6683 
6684 /* Floating-point data-processing (3 source) - half precision */
6685 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6686                                 int rd, int rn, int rm, int ra)
6687 {
6688     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6689     TCGv_i32 tcg_res = tcg_temp_new_i32();
6690     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6691 
6692     tcg_op1 = read_fp_hreg(s, rn);
6693     tcg_op2 = read_fp_hreg(s, rm);
6694     tcg_op3 = read_fp_hreg(s, ra);
6695 
6696     /* These are fused multiply-add, and must be done as one
6697      * floating point operation with no rounding between the
6698      * multiplication and addition steps.
6699      * NB that doing the negations here as separate steps is
6700      * correct : an input NaN should come out with its sign bit
6701      * flipped if it is a negated-input.
6702      */
6703     if (o1 == true) {
6704         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6705     }
6706 
6707     if (o0 != o1) {
6708         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6709     }
6710 
6711     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6712 
6713     write_fp_sreg(s, rd, tcg_res);
6714 }
6715 
6716 /* Floating point data-processing (3 source)
6717  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
6718  * +---+---+---+-----------+------+----+------+----+------+------+------+
6719  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6720  * +---+---+---+-----------+------+----+------+----+------+------+------+
6721  */
6722 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6723 {
6724     int mos = extract32(insn, 29, 3);
6725     int type = extract32(insn, 22, 2);
6726     int rd = extract32(insn, 0, 5);
6727     int rn = extract32(insn, 5, 5);
6728     int ra = extract32(insn, 10, 5);
6729     int rm = extract32(insn, 16, 5);
6730     bool o0 = extract32(insn, 15, 1);
6731     bool o1 = extract32(insn, 21, 1);
6732 
6733     if (mos) {
6734         unallocated_encoding(s);
6735         return;
6736     }
6737 
6738     switch (type) {
6739     case 0:
6740         if (!fp_access_check(s)) {
6741             return;
6742         }
6743         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6744         break;
6745     case 1:
6746         if (!fp_access_check(s)) {
6747             return;
6748         }
6749         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6750         break;
6751     case 3:
6752         if (!dc_isar_feature(aa64_fp16, s)) {
6753             unallocated_encoding(s);
6754             return;
6755         }
6756         if (!fp_access_check(s)) {
6757             return;
6758         }
6759         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6760         break;
6761     default:
6762         unallocated_encoding(s);
6763     }
6764 }
6765 
6766 /* Floating point immediate
6767  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
6768  * +---+---+---+-----------+------+---+------------+-------+------+------+
6769  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
6770  * +---+---+---+-----------+------+---+------------+-------+------+------+
6771  */
6772 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6773 {
6774     int rd = extract32(insn, 0, 5);
6775     int imm5 = extract32(insn, 5, 5);
6776     int imm8 = extract32(insn, 13, 8);
6777     int type = extract32(insn, 22, 2);
6778     int mos = extract32(insn, 29, 3);
6779     uint64_t imm;
6780     MemOp sz;
6781 
6782     if (mos || imm5) {
6783         unallocated_encoding(s);
6784         return;
6785     }
6786 
6787     switch (type) {
6788     case 0:
6789         sz = MO_32;
6790         break;
6791     case 1:
6792         sz = MO_64;
6793         break;
6794     case 3:
6795         sz = MO_16;
6796         if (dc_isar_feature(aa64_fp16, s)) {
6797             break;
6798         }
6799         /* fallthru */
6800     default:
6801         unallocated_encoding(s);
6802         return;
6803     }
6804 
6805     if (!fp_access_check(s)) {
6806         return;
6807     }
6808 
6809     imm = vfp_expand_imm(sz, imm8);
6810     write_fp_dreg(s, rd, tcg_constant_i64(imm));
6811 }
6812 
6813 /* Handle floating point <=> fixed point conversions. Note that we can
6814  * also deal with fp <=> integer conversions as a special case (scale == 64)
6815  * OPTME: consider handling that special case specially or at least skipping
6816  * the call to scalbn in the helpers for zero shifts.
6817  */
6818 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6819                            bool itof, int rmode, int scale, int sf, int type)
6820 {
6821     bool is_signed = !(opcode & 1);
6822     TCGv_ptr tcg_fpstatus;
6823     TCGv_i32 tcg_shift, tcg_single;
6824     TCGv_i64 tcg_double;
6825 
6826     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
6827 
6828     tcg_shift = tcg_constant_i32(64 - scale);
6829 
6830     if (itof) {
6831         TCGv_i64 tcg_int = cpu_reg(s, rn);
6832         if (!sf) {
6833             TCGv_i64 tcg_extend = tcg_temp_new_i64();
6834 
6835             if (is_signed) {
6836                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6837             } else {
6838                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6839             }
6840 
6841             tcg_int = tcg_extend;
6842         }
6843 
6844         switch (type) {
6845         case 1: /* float64 */
6846             tcg_double = tcg_temp_new_i64();
6847             if (is_signed) {
6848                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6849                                      tcg_shift, tcg_fpstatus);
6850             } else {
6851                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6852                                      tcg_shift, tcg_fpstatus);
6853             }
6854             write_fp_dreg(s, rd, tcg_double);
6855             break;
6856 
6857         case 0: /* float32 */
6858             tcg_single = tcg_temp_new_i32();
6859             if (is_signed) {
6860                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6861                                      tcg_shift, tcg_fpstatus);
6862             } else {
6863                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6864                                      tcg_shift, tcg_fpstatus);
6865             }
6866             write_fp_sreg(s, rd, tcg_single);
6867             break;
6868 
6869         case 3: /* float16 */
6870             tcg_single = tcg_temp_new_i32();
6871             if (is_signed) {
6872                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6873                                      tcg_shift, tcg_fpstatus);
6874             } else {
6875                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6876                                      tcg_shift, tcg_fpstatus);
6877             }
6878             write_fp_sreg(s, rd, tcg_single);
6879             break;
6880 
6881         default:
6882             g_assert_not_reached();
6883         }
6884     } else {
6885         TCGv_i64 tcg_int = cpu_reg(s, rd);
6886         TCGv_i32 tcg_rmode;
6887 
6888         if (extract32(opcode, 2, 1)) {
6889             /* There are too many rounding modes to all fit into rmode,
6890              * so FCVTA[US] is a special case.
6891              */
6892             rmode = FPROUNDING_TIEAWAY;
6893         }
6894 
6895         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
6896 
6897         switch (type) {
6898         case 1: /* float64 */
6899             tcg_double = read_fp_dreg(s, rn);
6900             if (is_signed) {
6901                 if (!sf) {
6902                     gen_helper_vfp_tosld(tcg_int, tcg_double,
6903                                          tcg_shift, tcg_fpstatus);
6904                 } else {
6905                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
6906                                          tcg_shift, tcg_fpstatus);
6907                 }
6908             } else {
6909                 if (!sf) {
6910                     gen_helper_vfp_tould(tcg_int, tcg_double,
6911                                          tcg_shift, tcg_fpstatus);
6912                 } else {
6913                     gen_helper_vfp_touqd(tcg_int, tcg_double,
6914                                          tcg_shift, tcg_fpstatus);
6915                 }
6916             }
6917             if (!sf) {
6918                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6919             }
6920             break;
6921 
6922         case 0: /* float32 */
6923             tcg_single = read_fp_sreg(s, rn);
6924             if (sf) {
6925                 if (is_signed) {
6926                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
6927                                          tcg_shift, tcg_fpstatus);
6928                 } else {
6929                     gen_helper_vfp_touqs(tcg_int, tcg_single,
6930                                          tcg_shift, tcg_fpstatus);
6931                 }
6932             } else {
6933                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6934                 if (is_signed) {
6935                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
6936                                          tcg_shift, tcg_fpstatus);
6937                 } else {
6938                     gen_helper_vfp_touls(tcg_dest, tcg_single,
6939                                          tcg_shift, tcg_fpstatus);
6940                 }
6941                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6942             }
6943             break;
6944 
6945         case 3: /* float16 */
6946             tcg_single = read_fp_sreg(s, rn);
6947             if (sf) {
6948                 if (is_signed) {
6949                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
6950                                          tcg_shift, tcg_fpstatus);
6951                 } else {
6952                     gen_helper_vfp_touqh(tcg_int, tcg_single,
6953                                          tcg_shift, tcg_fpstatus);
6954                 }
6955             } else {
6956                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6957                 if (is_signed) {
6958                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
6959                                          tcg_shift, tcg_fpstatus);
6960                 } else {
6961                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
6962                                          tcg_shift, tcg_fpstatus);
6963                 }
6964                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6965             }
6966             break;
6967 
6968         default:
6969             g_assert_not_reached();
6970         }
6971 
6972         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
6973     }
6974 }
6975 
6976 /* Floating point <-> fixed point conversions
6977  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
6978  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6979  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
6980  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6981  */
6982 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
6983 {
6984     int rd = extract32(insn, 0, 5);
6985     int rn = extract32(insn, 5, 5);
6986     int scale = extract32(insn, 10, 6);
6987     int opcode = extract32(insn, 16, 3);
6988     int rmode = extract32(insn, 19, 2);
6989     int type = extract32(insn, 22, 2);
6990     bool sbit = extract32(insn, 29, 1);
6991     bool sf = extract32(insn, 31, 1);
6992     bool itof;
6993 
6994     if (sbit || (!sf && scale < 32)) {
6995         unallocated_encoding(s);
6996         return;
6997     }
6998 
6999     switch (type) {
7000     case 0: /* float32 */
7001     case 1: /* float64 */
7002         break;
7003     case 3: /* float16 */
7004         if (dc_isar_feature(aa64_fp16, s)) {
7005             break;
7006         }
7007         /* fallthru */
7008     default:
7009         unallocated_encoding(s);
7010         return;
7011     }
7012 
7013     switch ((rmode << 3) | opcode) {
7014     case 0x2: /* SCVTF */
7015     case 0x3: /* UCVTF */
7016         itof = true;
7017         break;
7018     case 0x18: /* FCVTZS */
7019     case 0x19: /* FCVTZU */
7020         itof = false;
7021         break;
7022     default:
7023         unallocated_encoding(s);
7024         return;
7025     }
7026 
7027     if (!fp_access_check(s)) {
7028         return;
7029     }
7030 
7031     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
7032 }
7033 
7034 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
7035 {
7036     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
7037      * without conversion.
7038      */
7039 
7040     if (itof) {
7041         TCGv_i64 tcg_rn = cpu_reg(s, rn);
7042         TCGv_i64 tmp;
7043 
7044         switch (type) {
7045         case 0:
7046             /* 32 bit */
7047             tmp = tcg_temp_new_i64();
7048             tcg_gen_ext32u_i64(tmp, tcg_rn);
7049             write_fp_dreg(s, rd, tmp);
7050             break;
7051         case 1:
7052             /* 64 bit */
7053             write_fp_dreg(s, rd, tcg_rn);
7054             break;
7055         case 2:
7056             /* 64 bit to top half. */
7057             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
7058             clear_vec_high(s, true, rd);
7059             break;
7060         case 3:
7061             /* 16 bit */
7062             tmp = tcg_temp_new_i64();
7063             tcg_gen_ext16u_i64(tmp, tcg_rn);
7064             write_fp_dreg(s, rd, tmp);
7065             break;
7066         default:
7067             g_assert_not_reached();
7068         }
7069     } else {
7070         TCGv_i64 tcg_rd = cpu_reg(s, rd);
7071 
7072         switch (type) {
7073         case 0:
7074             /* 32 bit */
7075             tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
7076             break;
7077         case 1:
7078             /* 64 bit */
7079             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
7080             break;
7081         case 2:
7082             /* 64 bits from top half */
7083             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
7084             break;
7085         case 3:
7086             /* 16 bit */
7087             tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
7088             break;
7089         default:
7090             g_assert_not_reached();
7091         }
7092     }
7093 }
7094 
7095 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
7096 {
7097     TCGv_i64 t = read_fp_dreg(s, rn);
7098     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
7099 
7100     gen_helper_fjcvtzs(t, t, fpstatus);
7101 
7102     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
7103     tcg_gen_extrh_i64_i32(cpu_ZF, t);
7104     tcg_gen_movi_i32(cpu_CF, 0);
7105     tcg_gen_movi_i32(cpu_NF, 0);
7106     tcg_gen_movi_i32(cpu_VF, 0);
7107 }
7108 
7109 /* Floating point <-> integer conversions
7110  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
7111  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7112  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
7113  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7114  */
7115 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
7116 {
7117     int rd = extract32(insn, 0, 5);
7118     int rn = extract32(insn, 5, 5);
7119     int opcode = extract32(insn, 16, 3);
7120     int rmode = extract32(insn, 19, 2);
7121     int type = extract32(insn, 22, 2);
7122     bool sbit = extract32(insn, 29, 1);
7123     bool sf = extract32(insn, 31, 1);
7124     bool itof = false;
7125 
7126     if (sbit) {
7127         goto do_unallocated;
7128     }
7129 
7130     switch (opcode) {
7131     case 2: /* SCVTF */
7132     case 3: /* UCVTF */
7133         itof = true;
7134         /* fallthru */
7135     case 4: /* FCVTAS */
7136     case 5: /* FCVTAU */
7137         if (rmode != 0) {
7138             goto do_unallocated;
7139         }
7140         /* fallthru */
7141     case 0: /* FCVT[NPMZ]S */
7142     case 1: /* FCVT[NPMZ]U */
7143         switch (type) {
7144         case 0: /* float32 */
7145         case 1: /* float64 */
7146             break;
7147         case 3: /* float16 */
7148             if (!dc_isar_feature(aa64_fp16, s)) {
7149                 goto do_unallocated;
7150             }
7151             break;
7152         default:
7153             goto do_unallocated;
7154         }
7155         if (!fp_access_check(s)) {
7156             return;
7157         }
7158         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
7159         break;
7160 
7161     default:
7162         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
7163         case 0b01100110: /* FMOV half <-> 32-bit int */
7164         case 0b01100111:
7165         case 0b11100110: /* FMOV half <-> 64-bit int */
7166         case 0b11100111:
7167             if (!dc_isar_feature(aa64_fp16, s)) {
7168                 goto do_unallocated;
7169             }
7170             /* fallthru */
7171         case 0b00000110: /* FMOV 32-bit */
7172         case 0b00000111:
7173         case 0b10100110: /* FMOV 64-bit */
7174         case 0b10100111:
7175         case 0b11001110: /* FMOV top half of 128-bit */
7176         case 0b11001111:
7177             if (!fp_access_check(s)) {
7178                 return;
7179             }
7180             itof = opcode & 1;
7181             handle_fmov(s, rd, rn, type, itof);
7182             break;
7183 
7184         case 0b00111110: /* FJCVTZS */
7185             if (!dc_isar_feature(aa64_jscvt, s)) {
7186                 goto do_unallocated;
7187             } else if (fp_access_check(s)) {
7188                 handle_fjcvtzs(s, rd, rn);
7189             }
7190             break;
7191 
7192         default:
7193         do_unallocated:
7194             unallocated_encoding(s);
7195             return;
7196         }
7197         break;
7198     }
7199 }
7200 
7201 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7202  *   31  30  29 28     25 24                          0
7203  * +---+---+---+---------+-----------------------------+
7204  * |   | 0 |   | 1 1 1 1 |                             |
7205  * +---+---+---+---------+-----------------------------+
7206  */
7207 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7208 {
7209     if (extract32(insn, 24, 1)) {
7210         /* Floating point data-processing (3 source) */
7211         disas_fp_3src(s, insn);
7212     } else if (extract32(insn, 21, 1) == 0) {
7213         /* Floating point to fixed point conversions */
7214         disas_fp_fixed_conv(s, insn);
7215     } else {
7216         switch (extract32(insn, 10, 2)) {
7217         case 1:
7218             /* Floating point conditional compare */
7219             disas_fp_ccomp(s, insn);
7220             break;
7221         case 2:
7222             /* Floating point data-processing (2 source) */
7223             disas_fp_2src(s, insn);
7224             break;
7225         case 3:
7226             /* Floating point conditional select */
7227             disas_fp_csel(s, insn);
7228             break;
7229         case 0:
7230             switch (ctz32(extract32(insn, 12, 4))) {
7231             case 0: /* [15:12] == xxx1 */
7232                 /* Floating point immediate */
7233                 disas_fp_imm(s, insn);
7234                 break;
7235             case 1: /* [15:12] == xx10 */
7236                 /* Floating point compare */
7237                 disas_fp_compare(s, insn);
7238                 break;
7239             case 2: /* [15:12] == x100 */
7240                 /* Floating point data-processing (1 source) */
7241                 disas_fp_1src(s, insn);
7242                 break;
7243             case 3: /* [15:12] == 1000 */
7244                 unallocated_encoding(s);
7245                 break;
7246             default: /* [15:12] == 0000 */
7247                 /* Floating point <-> integer conversions */
7248                 disas_fp_int_conv(s, insn);
7249                 break;
7250             }
7251             break;
7252         }
7253     }
7254 }
7255 
7256 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7257                      int pos)
7258 {
7259     /* Extract 64 bits from the middle of two concatenated 64 bit
7260      * vector register slices left:right. The extracted bits start
7261      * at 'pos' bits into the right (least significant) side.
7262      * We return the result in tcg_right, and guarantee not to
7263      * trash tcg_left.
7264      */
7265     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7266     assert(pos > 0 && pos < 64);
7267 
7268     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7269     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7270     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7271 }
7272 
7273 /* EXT
7274  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
7275  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7276  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
7277  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7278  */
7279 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7280 {
7281     int is_q = extract32(insn, 30, 1);
7282     int op2 = extract32(insn, 22, 2);
7283     int imm4 = extract32(insn, 11, 4);
7284     int rm = extract32(insn, 16, 5);
7285     int rn = extract32(insn, 5, 5);
7286     int rd = extract32(insn, 0, 5);
7287     int pos = imm4 << 3;
7288     TCGv_i64 tcg_resl, tcg_resh;
7289 
7290     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7291         unallocated_encoding(s);
7292         return;
7293     }
7294 
7295     if (!fp_access_check(s)) {
7296         return;
7297     }
7298 
7299     tcg_resh = tcg_temp_new_i64();
7300     tcg_resl = tcg_temp_new_i64();
7301 
7302     /* Vd gets bits starting at pos bits into Vm:Vn. This is
7303      * either extracting 128 bits from a 128:128 concatenation, or
7304      * extracting 64 bits from a 64:64 concatenation.
7305      */
7306     if (!is_q) {
7307         read_vec_element(s, tcg_resl, rn, 0, MO_64);
7308         if (pos != 0) {
7309             read_vec_element(s, tcg_resh, rm, 0, MO_64);
7310             do_ext64(s, tcg_resh, tcg_resl, pos);
7311         }
7312     } else {
7313         TCGv_i64 tcg_hh;
7314         typedef struct {
7315             int reg;
7316             int elt;
7317         } EltPosns;
7318         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7319         EltPosns *elt = eltposns;
7320 
7321         if (pos >= 64) {
7322             elt++;
7323             pos -= 64;
7324         }
7325 
7326         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7327         elt++;
7328         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7329         elt++;
7330         if (pos != 0) {
7331             do_ext64(s, tcg_resh, tcg_resl, pos);
7332             tcg_hh = tcg_temp_new_i64();
7333             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7334             do_ext64(s, tcg_hh, tcg_resh, pos);
7335         }
7336     }
7337 
7338     write_vec_element(s, tcg_resl, rd, 0, MO_64);
7339     if (is_q) {
7340         write_vec_element(s, tcg_resh, rd, 1, MO_64);
7341     }
7342     clear_vec_high(s, is_q, rd);
7343 }
7344 
7345 /* TBL/TBX
7346  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
7347  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7348  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
7349  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7350  */
7351 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7352 {
7353     int op2 = extract32(insn, 22, 2);
7354     int is_q = extract32(insn, 30, 1);
7355     int rm = extract32(insn, 16, 5);
7356     int rn = extract32(insn, 5, 5);
7357     int rd = extract32(insn, 0, 5);
7358     int is_tbx = extract32(insn, 12, 1);
7359     int len = (extract32(insn, 13, 2) + 1) * 16;
7360 
7361     if (op2 != 0) {
7362         unallocated_encoding(s);
7363         return;
7364     }
7365 
7366     if (!fp_access_check(s)) {
7367         return;
7368     }
7369 
7370     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7371                        vec_full_reg_offset(s, rm), cpu_env,
7372                        is_q ? 16 : 8, vec_full_reg_size(s),
7373                        (len << 6) | (is_tbx << 5) | rn,
7374                        gen_helper_simd_tblx);
7375 }
7376 
7377 /* ZIP/UZP/TRN
7378  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
7379  * +---+---+-------------+------+---+------+---+------------------+------+
7380  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
7381  * +---+---+-------------+------+---+------+---+------------------+------+
7382  */
7383 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7384 {
7385     int rd = extract32(insn, 0, 5);
7386     int rn = extract32(insn, 5, 5);
7387     int rm = extract32(insn, 16, 5);
7388     int size = extract32(insn, 22, 2);
7389     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7390      * bit 2 indicates 1 vs 2 variant of the insn.
7391      */
7392     int opcode = extract32(insn, 12, 2);
7393     bool part = extract32(insn, 14, 1);
7394     bool is_q = extract32(insn, 30, 1);
7395     int esize = 8 << size;
7396     int i;
7397     int datasize = is_q ? 128 : 64;
7398     int elements = datasize / esize;
7399     TCGv_i64 tcg_res[2], tcg_ele;
7400 
7401     if (opcode == 0 || (size == 3 && !is_q)) {
7402         unallocated_encoding(s);
7403         return;
7404     }
7405 
7406     if (!fp_access_check(s)) {
7407         return;
7408     }
7409 
7410     tcg_res[0] = tcg_temp_new_i64();
7411     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
7412     tcg_ele = tcg_temp_new_i64();
7413 
7414     for (i = 0; i < elements; i++) {
7415         int o, w;
7416 
7417         switch (opcode) {
7418         case 1: /* UZP1/2 */
7419         {
7420             int midpoint = elements / 2;
7421             if (i < midpoint) {
7422                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
7423             } else {
7424                 read_vec_element(s, tcg_ele, rm,
7425                                  2 * (i - midpoint) + part, size);
7426             }
7427             break;
7428         }
7429         case 2: /* TRN1/2 */
7430             if (i & 1) {
7431                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
7432             } else {
7433                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
7434             }
7435             break;
7436         case 3: /* ZIP1/2 */
7437         {
7438             int base = part * elements / 2;
7439             if (i & 1) {
7440                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
7441             } else {
7442                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
7443             }
7444             break;
7445         }
7446         default:
7447             g_assert_not_reached();
7448         }
7449 
7450         w = (i * esize) / 64;
7451         o = (i * esize) % 64;
7452         if (o == 0) {
7453             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
7454         } else {
7455             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
7456             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
7457         }
7458     }
7459 
7460     for (i = 0; i <= is_q; ++i) {
7461         write_vec_element(s, tcg_res[i], rd, i, MO_64);
7462     }
7463     clear_vec_high(s, is_q, rd);
7464 }
7465 
7466 /*
7467  * do_reduction_op helper
7468  *
7469  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7470  * important for correct NaN propagation that we do these
7471  * operations in exactly the order specified by the pseudocode.
7472  *
7473  * This is a recursive function, TCG temps should be freed by the
7474  * calling function once it is done with the values.
7475  */
7476 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7477                                 int esize, int size, int vmap, TCGv_ptr fpst)
7478 {
7479     if (esize == size) {
7480         int element;
7481         MemOp msize = esize == 16 ? MO_16 : MO_32;
7482         TCGv_i32 tcg_elem;
7483 
7484         /* We should have one register left here */
7485         assert(ctpop8(vmap) == 1);
7486         element = ctz32(vmap);
7487         assert(element < 8);
7488 
7489         tcg_elem = tcg_temp_new_i32();
7490         read_vec_element_i32(s, tcg_elem, rn, element, msize);
7491         return tcg_elem;
7492     } else {
7493         int bits = size / 2;
7494         int shift = ctpop8(vmap) / 2;
7495         int vmap_lo = (vmap >> shift) & vmap;
7496         int vmap_hi = (vmap & ~vmap_lo);
7497         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7498 
7499         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7500         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7501         tcg_res = tcg_temp_new_i32();
7502 
7503         switch (fpopcode) {
7504         case 0x0c: /* fmaxnmv half-precision */
7505             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7506             break;
7507         case 0x0f: /* fmaxv half-precision */
7508             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7509             break;
7510         case 0x1c: /* fminnmv half-precision */
7511             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7512             break;
7513         case 0x1f: /* fminv half-precision */
7514             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7515             break;
7516         case 0x2c: /* fmaxnmv */
7517             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7518             break;
7519         case 0x2f: /* fmaxv */
7520             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7521             break;
7522         case 0x3c: /* fminnmv */
7523             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7524             break;
7525         case 0x3f: /* fminv */
7526             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7527             break;
7528         default:
7529             g_assert_not_reached();
7530         }
7531         return tcg_res;
7532     }
7533 }
7534 
7535 /* AdvSIMD across lanes
7536  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7537  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7538  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7539  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7540  */
7541 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7542 {
7543     int rd = extract32(insn, 0, 5);
7544     int rn = extract32(insn, 5, 5);
7545     int size = extract32(insn, 22, 2);
7546     int opcode = extract32(insn, 12, 5);
7547     bool is_q = extract32(insn, 30, 1);
7548     bool is_u = extract32(insn, 29, 1);
7549     bool is_fp = false;
7550     bool is_min = false;
7551     int esize;
7552     int elements;
7553     int i;
7554     TCGv_i64 tcg_res, tcg_elt;
7555 
7556     switch (opcode) {
7557     case 0x1b: /* ADDV */
7558         if (is_u) {
7559             unallocated_encoding(s);
7560             return;
7561         }
7562         /* fall through */
7563     case 0x3: /* SADDLV, UADDLV */
7564     case 0xa: /* SMAXV, UMAXV */
7565     case 0x1a: /* SMINV, UMINV */
7566         if (size == 3 || (size == 2 && !is_q)) {
7567             unallocated_encoding(s);
7568             return;
7569         }
7570         break;
7571     case 0xc: /* FMAXNMV, FMINNMV */
7572     case 0xf: /* FMAXV, FMINV */
7573         /* Bit 1 of size field encodes min vs max and the actual size
7574          * depends on the encoding of the U bit. If not set (and FP16
7575          * enabled) then we do half-precision float instead of single
7576          * precision.
7577          */
7578         is_min = extract32(size, 1, 1);
7579         is_fp = true;
7580         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7581             size = 1;
7582         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7583             unallocated_encoding(s);
7584             return;
7585         } else {
7586             size = 2;
7587         }
7588         break;
7589     default:
7590         unallocated_encoding(s);
7591         return;
7592     }
7593 
7594     if (!fp_access_check(s)) {
7595         return;
7596     }
7597 
7598     esize = 8 << size;
7599     elements = (is_q ? 128 : 64) / esize;
7600 
7601     tcg_res = tcg_temp_new_i64();
7602     tcg_elt = tcg_temp_new_i64();
7603 
7604     /* These instructions operate across all lanes of a vector
7605      * to produce a single result. We can guarantee that a 64
7606      * bit intermediate is sufficient:
7607      *  + for [US]ADDLV the maximum element size is 32 bits, and
7608      *    the result type is 64 bits
7609      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7610      *    same as the element size, which is 32 bits at most
7611      * For the integer operations we can choose to work at 64
7612      * or 32 bits and truncate at the end; for simplicity
7613      * we use 64 bits always. The floating point
7614      * ops do require 32 bit intermediates, though.
7615      */
7616     if (!is_fp) {
7617         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7618 
7619         for (i = 1; i < elements; i++) {
7620             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7621 
7622             switch (opcode) {
7623             case 0x03: /* SADDLV / UADDLV */
7624             case 0x1b: /* ADDV */
7625                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7626                 break;
7627             case 0x0a: /* SMAXV / UMAXV */
7628                 if (is_u) {
7629                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7630                 } else {
7631                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7632                 }
7633                 break;
7634             case 0x1a: /* SMINV / UMINV */
7635                 if (is_u) {
7636                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7637                 } else {
7638                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7639                 }
7640                 break;
7641             default:
7642                 g_assert_not_reached();
7643             }
7644 
7645         }
7646     } else {
7647         /* Floating point vector reduction ops which work across 32
7648          * bit (single) or 16 bit (half-precision) intermediates.
7649          * Note that correct NaN propagation requires that we do these
7650          * operations in exactly the order specified by the pseudocode.
7651          */
7652         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7653         int fpopcode = opcode | is_min << 4 | is_u << 5;
7654         int vmap = (1 << elements) - 1;
7655         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7656                                              (is_q ? 128 : 64), vmap, fpst);
7657         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7658     }
7659 
7660     /* Now truncate the result to the width required for the final output */
7661     if (opcode == 0x03) {
7662         /* SADDLV, UADDLV: result is 2*esize */
7663         size++;
7664     }
7665 
7666     switch (size) {
7667     case 0:
7668         tcg_gen_ext8u_i64(tcg_res, tcg_res);
7669         break;
7670     case 1:
7671         tcg_gen_ext16u_i64(tcg_res, tcg_res);
7672         break;
7673     case 2:
7674         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7675         break;
7676     case 3:
7677         break;
7678     default:
7679         g_assert_not_reached();
7680     }
7681 
7682     write_fp_dreg(s, rd, tcg_res);
7683 }
7684 
7685 /* DUP (Element, Vector)
7686  *
7687  *  31  30   29              21 20    16 15        10  9    5 4    0
7688  * +---+---+-------------------+--------+-------------+------+------+
7689  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7690  * +---+---+-------------------+--------+-------------+------+------+
7691  *
7692  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7693  */
7694 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7695                              int imm5)
7696 {
7697     int size = ctz32(imm5);
7698     int index;
7699 
7700     if (size > 3 || (size == 3 && !is_q)) {
7701         unallocated_encoding(s);
7702         return;
7703     }
7704 
7705     if (!fp_access_check(s)) {
7706         return;
7707     }
7708 
7709     index = imm5 >> (size + 1);
7710     tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7711                          vec_reg_offset(s, rn, index, size),
7712                          is_q ? 16 : 8, vec_full_reg_size(s));
7713 }
7714 
7715 /* DUP (element, scalar)
7716  *  31                   21 20    16 15        10  9    5 4    0
7717  * +-----------------------+--------+-------------+------+------+
7718  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7719  * +-----------------------+--------+-------------+------+------+
7720  */
7721 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7722                               int imm5)
7723 {
7724     int size = ctz32(imm5);
7725     int index;
7726     TCGv_i64 tmp;
7727 
7728     if (size > 3) {
7729         unallocated_encoding(s);
7730         return;
7731     }
7732 
7733     if (!fp_access_check(s)) {
7734         return;
7735     }
7736 
7737     index = imm5 >> (size + 1);
7738 
7739     /* This instruction just extracts the specified element and
7740      * zero-extends it into the bottom of the destination register.
7741      */
7742     tmp = tcg_temp_new_i64();
7743     read_vec_element(s, tmp, rn, index, size);
7744     write_fp_dreg(s, rd, tmp);
7745 }
7746 
7747 /* DUP (General)
7748  *
7749  *  31  30   29              21 20    16 15        10  9    5 4    0
7750  * +---+---+-------------------+--------+-------------+------+------+
7751  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
7752  * +---+---+-------------------+--------+-------------+------+------+
7753  *
7754  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7755  */
7756 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7757                              int imm5)
7758 {
7759     int size = ctz32(imm5);
7760     uint32_t dofs, oprsz, maxsz;
7761 
7762     if (size > 3 || ((size == 3) && !is_q)) {
7763         unallocated_encoding(s);
7764         return;
7765     }
7766 
7767     if (!fp_access_check(s)) {
7768         return;
7769     }
7770 
7771     dofs = vec_full_reg_offset(s, rd);
7772     oprsz = is_q ? 16 : 8;
7773     maxsz = vec_full_reg_size(s);
7774 
7775     tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7776 }
7777 
7778 /* INS (Element)
7779  *
7780  *  31                   21 20    16 15  14    11  10 9    5 4    0
7781  * +-----------------------+--------+------------+---+------+------+
7782  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
7783  * +-----------------------+--------+------------+---+------+------+
7784  *
7785  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7786  * index: encoded in imm5<4:size+1>
7787  */
7788 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7789                              int imm4, int imm5)
7790 {
7791     int size = ctz32(imm5);
7792     int src_index, dst_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     dst_index = extract32(imm5, 1+size, 5);
7805     src_index = extract32(imm4, size, 4);
7806 
7807     tmp = tcg_temp_new_i64();
7808 
7809     read_vec_element(s, tmp, rn, src_index, size);
7810     write_vec_element(s, tmp, rd, dst_index, size);
7811 
7812     /* INS is considered a 128-bit write for SVE. */
7813     clear_vec_high(s, true, rd);
7814 }
7815 
7816 
7817 /* INS (General)
7818  *
7819  *  31                   21 20    16 15        10  9    5 4    0
7820  * +-----------------------+--------+-------------+------+------+
7821  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
7822  * +-----------------------+--------+-------------+------+------+
7823  *
7824  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7825  * index: encoded in imm5<4:size+1>
7826  */
7827 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7828 {
7829     int size = ctz32(imm5);
7830     int idx;
7831 
7832     if (size > 3) {
7833         unallocated_encoding(s);
7834         return;
7835     }
7836 
7837     if (!fp_access_check(s)) {
7838         return;
7839     }
7840 
7841     idx = extract32(imm5, 1 + size, 4 - size);
7842     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7843 
7844     /* INS is considered a 128-bit write for SVE. */
7845     clear_vec_high(s, true, rd);
7846 }
7847 
7848 /*
7849  * UMOV (General)
7850  * SMOV (General)
7851  *
7852  *  31  30   29              21 20    16 15    12   10 9    5 4    0
7853  * +---+---+-------------------+--------+-------------+------+------+
7854  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
7855  * +---+---+-------------------+--------+-------------+------+------+
7856  *
7857  * U: unsigned when set
7858  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7859  */
7860 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7861                                   int rn, int rd, int imm5)
7862 {
7863     int size = ctz32(imm5);
7864     int element;
7865     TCGv_i64 tcg_rd;
7866 
7867     /* Check for UnallocatedEncodings */
7868     if (is_signed) {
7869         if (size > 2 || (size == 2 && !is_q)) {
7870             unallocated_encoding(s);
7871             return;
7872         }
7873     } else {
7874         if (size > 3
7875             || (size < 3 && is_q)
7876             || (size == 3 && !is_q)) {
7877             unallocated_encoding(s);
7878             return;
7879         }
7880     }
7881 
7882     if (!fp_access_check(s)) {
7883         return;
7884     }
7885 
7886     element = extract32(imm5, 1+size, 4);
7887 
7888     tcg_rd = cpu_reg(s, rd);
7889     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7890     if (is_signed && !is_q) {
7891         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7892     }
7893 }
7894 
7895 /* AdvSIMD copy
7896  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
7897  * +---+---+----+-----------------+------+---+------+---+------+------+
7898  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7899  * +---+---+----+-----------------+------+---+------+---+------+------+
7900  */
7901 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7902 {
7903     int rd = extract32(insn, 0, 5);
7904     int rn = extract32(insn, 5, 5);
7905     int imm4 = extract32(insn, 11, 4);
7906     int op = extract32(insn, 29, 1);
7907     int is_q = extract32(insn, 30, 1);
7908     int imm5 = extract32(insn, 16, 5);
7909 
7910     if (op) {
7911         if (is_q) {
7912             /* INS (element) */
7913             handle_simd_inse(s, rd, rn, imm4, imm5);
7914         } else {
7915             unallocated_encoding(s);
7916         }
7917     } else {
7918         switch (imm4) {
7919         case 0:
7920             /* DUP (element - vector) */
7921             handle_simd_dupe(s, is_q, rd, rn, imm5);
7922             break;
7923         case 1:
7924             /* DUP (general) */
7925             handle_simd_dupg(s, is_q, rd, rn, imm5);
7926             break;
7927         case 3:
7928             if (is_q) {
7929                 /* INS (general) */
7930                 handle_simd_insg(s, rd, rn, imm5);
7931             } else {
7932                 unallocated_encoding(s);
7933             }
7934             break;
7935         case 5:
7936         case 7:
7937             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
7938             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
7939             break;
7940         default:
7941             unallocated_encoding(s);
7942             break;
7943         }
7944     }
7945 }
7946 
7947 /* AdvSIMD modified immediate
7948  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
7949  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7950  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
7951  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7952  *
7953  * There are a number of operations that can be carried out here:
7954  *   MOVI - move (shifted) imm into register
7955  *   MVNI - move inverted (shifted) imm into register
7956  *   ORR  - bitwise OR of (shifted) imm with register
7957  *   BIC  - bitwise clear of (shifted) imm with register
7958  * With ARMv8.2 we also have:
7959  *   FMOV half-precision
7960  */
7961 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
7962 {
7963     int rd = extract32(insn, 0, 5);
7964     int cmode = extract32(insn, 12, 4);
7965     int o2 = extract32(insn, 11, 1);
7966     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
7967     bool is_neg = extract32(insn, 29, 1);
7968     bool is_q = extract32(insn, 30, 1);
7969     uint64_t imm = 0;
7970 
7971     if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
7972         /* Check for FMOV (vector, immediate) - half-precision */
7973         if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
7974             unallocated_encoding(s);
7975             return;
7976         }
7977     }
7978 
7979     if (!fp_access_check(s)) {
7980         return;
7981     }
7982 
7983     if (cmode == 15 && o2 && !is_neg) {
7984         /* FMOV (vector, immediate) - half-precision */
7985         imm = vfp_expand_imm(MO_16, abcdefgh);
7986         /* now duplicate across the lanes */
7987         imm = dup_const(MO_16, imm);
7988     } else {
7989         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
7990     }
7991 
7992     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
7993         /* MOVI or MVNI, with MVNI negation handled above.  */
7994         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
7995                              vec_full_reg_size(s), imm);
7996     } else {
7997         /* ORR or BIC, with BIC negation to AND handled above.  */
7998         if (is_neg) {
7999             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
8000         } else {
8001             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
8002         }
8003     }
8004 }
8005 
8006 /* AdvSIMD scalar copy
8007  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
8008  * +-----+----+-----------------+------+---+------+---+------+------+
8009  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
8010  * +-----+----+-----------------+------+---+------+---+------+------+
8011  */
8012 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
8013 {
8014     int rd = extract32(insn, 0, 5);
8015     int rn = extract32(insn, 5, 5);
8016     int imm4 = extract32(insn, 11, 4);
8017     int imm5 = extract32(insn, 16, 5);
8018     int op = extract32(insn, 29, 1);
8019 
8020     if (op != 0 || imm4 != 0) {
8021         unallocated_encoding(s);
8022         return;
8023     }
8024 
8025     /* DUP (element, scalar) */
8026     handle_simd_dupes(s, rd, rn, imm5);
8027 }
8028 
8029 /* AdvSIMD scalar pairwise
8030  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
8031  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8032  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
8033  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8034  */
8035 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
8036 {
8037     int u = extract32(insn, 29, 1);
8038     int size = extract32(insn, 22, 2);
8039     int opcode = extract32(insn, 12, 5);
8040     int rn = extract32(insn, 5, 5);
8041     int rd = extract32(insn, 0, 5);
8042     TCGv_ptr fpst;
8043 
8044     /* For some ops (the FP ones), size[1] is part of the encoding.
8045      * For ADDP strictly it is not but size[1] is always 1 for valid
8046      * encodings.
8047      */
8048     opcode |= (extract32(size, 1, 1) << 5);
8049 
8050     switch (opcode) {
8051     case 0x3b: /* ADDP */
8052         if (u || size != 3) {
8053             unallocated_encoding(s);
8054             return;
8055         }
8056         if (!fp_access_check(s)) {
8057             return;
8058         }
8059 
8060         fpst = NULL;
8061         break;
8062     case 0xc: /* FMAXNMP */
8063     case 0xd: /* FADDP */
8064     case 0xf: /* FMAXP */
8065     case 0x2c: /* FMINNMP */
8066     case 0x2f: /* FMINP */
8067         /* FP op, size[0] is 32 or 64 bit*/
8068         if (!u) {
8069             if (!dc_isar_feature(aa64_fp16, s)) {
8070                 unallocated_encoding(s);
8071                 return;
8072             } else {
8073                 size = MO_16;
8074             }
8075         } else {
8076             size = extract32(size, 0, 1) ? MO_64 : MO_32;
8077         }
8078 
8079         if (!fp_access_check(s)) {
8080             return;
8081         }
8082 
8083         fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8084         break;
8085     default:
8086         unallocated_encoding(s);
8087         return;
8088     }
8089 
8090     if (size == MO_64) {
8091         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8092         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8093         TCGv_i64 tcg_res = tcg_temp_new_i64();
8094 
8095         read_vec_element(s, tcg_op1, rn, 0, MO_64);
8096         read_vec_element(s, tcg_op2, rn, 1, MO_64);
8097 
8098         switch (opcode) {
8099         case 0x3b: /* ADDP */
8100             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
8101             break;
8102         case 0xc: /* FMAXNMP */
8103             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8104             break;
8105         case 0xd: /* FADDP */
8106             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8107             break;
8108         case 0xf: /* FMAXP */
8109             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8110             break;
8111         case 0x2c: /* FMINNMP */
8112             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8113             break;
8114         case 0x2f: /* FMINP */
8115             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8116             break;
8117         default:
8118             g_assert_not_reached();
8119         }
8120 
8121         write_fp_dreg(s, rd, tcg_res);
8122     } else {
8123         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8124         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8125         TCGv_i32 tcg_res = tcg_temp_new_i32();
8126 
8127         read_vec_element_i32(s, tcg_op1, rn, 0, size);
8128         read_vec_element_i32(s, tcg_op2, rn, 1, size);
8129 
8130         if (size == MO_16) {
8131             switch (opcode) {
8132             case 0xc: /* FMAXNMP */
8133                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8134                 break;
8135             case 0xd: /* FADDP */
8136                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
8137                 break;
8138             case 0xf: /* FMAXP */
8139                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
8140                 break;
8141             case 0x2c: /* FMINNMP */
8142                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8143                 break;
8144             case 0x2f: /* FMINP */
8145                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
8146                 break;
8147             default:
8148                 g_assert_not_reached();
8149             }
8150         } else {
8151             switch (opcode) {
8152             case 0xc: /* FMAXNMP */
8153                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8154                 break;
8155             case 0xd: /* FADDP */
8156                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8157                 break;
8158             case 0xf: /* FMAXP */
8159                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8160                 break;
8161             case 0x2c: /* FMINNMP */
8162                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8163                 break;
8164             case 0x2f: /* FMINP */
8165                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8166                 break;
8167             default:
8168                 g_assert_not_reached();
8169             }
8170         }
8171 
8172         write_fp_sreg(s, rd, tcg_res);
8173     }
8174 }
8175 
8176 /*
8177  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8178  *
8179  * This code is handles the common shifting code and is used by both
8180  * the vector and scalar code.
8181  */
8182 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8183                                     TCGv_i64 tcg_rnd, bool accumulate,
8184                                     bool is_u, int size, int shift)
8185 {
8186     bool extended_result = false;
8187     bool round = tcg_rnd != NULL;
8188     int ext_lshift = 0;
8189     TCGv_i64 tcg_src_hi;
8190 
8191     if (round && size == 3) {
8192         extended_result = true;
8193         ext_lshift = 64 - shift;
8194         tcg_src_hi = tcg_temp_new_i64();
8195     } else if (shift == 64) {
8196         if (!accumulate && is_u) {
8197             /* result is zero */
8198             tcg_gen_movi_i64(tcg_res, 0);
8199             return;
8200         }
8201     }
8202 
8203     /* Deal with the rounding step */
8204     if (round) {
8205         if (extended_result) {
8206             TCGv_i64 tcg_zero = tcg_constant_i64(0);
8207             if (!is_u) {
8208                 /* take care of sign extending tcg_res */
8209                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8210                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8211                                  tcg_src, tcg_src_hi,
8212                                  tcg_rnd, tcg_zero);
8213             } else {
8214                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8215                                  tcg_src, tcg_zero,
8216                                  tcg_rnd, tcg_zero);
8217             }
8218         } else {
8219             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8220         }
8221     }
8222 
8223     /* Now do the shift right */
8224     if (round && extended_result) {
8225         /* extended case, >64 bit precision required */
8226         if (ext_lshift == 0) {
8227             /* special case, only high bits matter */
8228             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8229         } else {
8230             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8231             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8232             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8233         }
8234     } else {
8235         if (is_u) {
8236             if (shift == 64) {
8237                 /* essentially shifting in 64 zeros */
8238                 tcg_gen_movi_i64(tcg_src, 0);
8239             } else {
8240                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8241             }
8242         } else {
8243             if (shift == 64) {
8244                 /* effectively extending the sign-bit */
8245                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8246             } else {
8247                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8248             }
8249         }
8250     }
8251 
8252     if (accumulate) {
8253         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8254     } else {
8255         tcg_gen_mov_i64(tcg_res, tcg_src);
8256     }
8257 }
8258 
8259 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8260 static void handle_scalar_simd_shri(DisasContext *s,
8261                                     bool is_u, int immh, int immb,
8262                                     int opcode, int rn, int rd)
8263 {
8264     const int size = 3;
8265     int immhb = immh << 3 | immb;
8266     int shift = 2 * (8 << size) - immhb;
8267     bool accumulate = false;
8268     bool round = false;
8269     bool insert = false;
8270     TCGv_i64 tcg_rn;
8271     TCGv_i64 tcg_rd;
8272     TCGv_i64 tcg_round;
8273 
8274     if (!extract32(immh, 3, 1)) {
8275         unallocated_encoding(s);
8276         return;
8277     }
8278 
8279     if (!fp_access_check(s)) {
8280         return;
8281     }
8282 
8283     switch (opcode) {
8284     case 0x02: /* SSRA / USRA (accumulate) */
8285         accumulate = true;
8286         break;
8287     case 0x04: /* SRSHR / URSHR (rounding) */
8288         round = true;
8289         break;
8290     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8291         accumulate = round = true;
8292         break;
8293     case 0x08: /* SRI */
8294         insert = true;
8295         break;
8296     }
8297 
8298     if (round) {
8299         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8300     } else {
8301         tcg_round = NULL;
8302     }
8303 
8304     tcg_rn = read_fp_dreg(s, rn);
8305     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8306 
8307     if (insert) {
8308         /* shift count same as element size is valid but does nothing;
8309          * special case to avoid potential shift by 64.
8310          */
8311         int esize = 8 << size;
8312         if (shift != esize) {
8313             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8314             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8315         }
8316     } else {
8317         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8318                                 accumulate, is_u, size, shift);
8319     }
8320 
8321     write_fp_dreg(s, rd, tcg_rd);
8322 }
8323 
8324 /* SHL/SLI - Scalar shift left */
8325 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8326                                     int immh, int immb, int opcode,
8327                                     int rn, int rd)
8328 {
8329     int size = 32 - clz32(immh) - 1;
8330     int immhb = immh << 3 | immb;
8331     int shift = immhb - (8 << size);
8332     TCGv_i64 tcg_rn;
8333     TCGv_i64 tcg_rd;
8334 
8335     if (!extract32(immh, 3, 1)) {
8336         unallocated_encoding(s);
8337         return;
8338     }
8339 
8340     if (!fp_access_check(s)) {
8341         return;
8342     }
8343 
8344     tcg_rn = read_fp_dreg(s, rn);
8345     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8346 
8347     if (insert) {
8348         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8349     } else {
8350         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8351     }
8352 
8353     write_fp_dreg(s, rd, tcg_rd);
8354 }
8355 
8356 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8357  * (signed/unsigned) narrowing */
8358 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8359                                    bool is_u_shift, bool is_u_narrow,
8360                                    int immh, int immb, int opcode,
8361                                    int rn, int rd)
8362 {
8363     int immhb = immh << 3 | immb;
8364     int size = 32 - clz32(immh) - 1;
8365     int esize = 8 << size;
8366     int shift = (2 * esize) - immhb;
8367     int elements = is_scalar ? 1 : (64 / esize);
8368     bool round = extract32(opcode, 0, 1);
8369     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8370     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8371     TCGv_i32 tcg_rd_narrowed;
8372     TCGv_i64 tcg_final;
8373 
8374     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8375         { gen_helper_neon_narrow_sat_s8,
8376           gen_helper_neon_unarrow_sat8 },
8377         { gen_helper_neon_narrow_sat_s16,
8378           gen_helper_neon_unarrow_sat16 },
8379         { gen_helper_neon_narrow_sat_s32,
8380           gen_helper_neon_unarrow_sat32 },
8381         { NULL, NULL },
8382     };
8383     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8384         gen_helper_neon_narrow_sat_u8,
8385         gen_helper_neon_narrow_sat_u16,
8386         gen_helper_neon_narrow_sat_u32,
8387         NULL
8388     };
8389     NeonGenNarrowEnvFn *narrowfn;
8390 
8391     int i;
8392 
8393     assert(size < 4);
8394 
8395     if (extract32(immh, 3, 1)) {
8396         unallocated_encoding(s);
8397         return;
8398     }
8399 
8400     if (!fp_access_check(s)) {
8401         return;
8402     }
8403 
8404     if (is_u_shift) {
8405         narrowfn = unsigned_narrow_fns[size];
8406     } else {
8407         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8408     }
8409 
8410     tcg_rn = tcg_temp_new_i64();
8411     tcg_rd = tcg_temp_new_i64();
8412     tcg_rd_narrowed = tcg_temp_new_i32();
8413     tcg_final = tcg_temp_new_i64();
8414 
8415     if (round) {
8416         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8417     } else {
8418         tcg_round = NULL;
8419     }
8420 
8421     for (i = 0; i < elements; i++) {
8422         read_vec_element(s, tcg_rn, rn, i, ldop);
8423         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8424                                 false, is_u_shift, size+1, shift);
8425         narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8426         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8427         if (i == 0) {
8428             tcg_gen_mov_i64(tcg_final, tcg_rd);
8429         } else {
8430             tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8431         }
8432     }
8433 
8434     if (!is_q) {
8435         write_vec_element(s, tcg_final, rd, 0, MO_64);
8436     } else {
8437         write_vec_element(s, tcg_final, rd, 1, MO_64);
8438     }
8439     clear_vec_high(s, is_q, rd);
8440 }
8441 
8442 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8443 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8444                              bool src_unsigned, bool dst_unsigned,
8445                              int immh, int immb, int rn, int rd)
8446 {
8447     int immhb = immh << 3 | immb;
8448     int size = 32 - clz32(immh) - 1;
8449     int shift = immhb - (8 << size);
8450     int pass;
8451 
8452     assert(immh != 0);
8453     assert(!(scalar && is_q));
8454 
8455     if (!scalar) {
8456         if (!is_q && extract32(immh, 3, 1)) {
8457             unallocated_encoding(s);
8458             return;
8459         }
8460 
8461         /* Since we use the variable-shift helpers we must
8462          * replicate the shift count into each element of
8463          * the tcg_shift value.
8464          */
8465         switch (size) {
8466         case 0:
8467             shift |= shift << 8;
8468             /* fall through */
8469         case 1:
8470             shift |= shift << 16;
8471             break;
8472         case 2:
8473         case 3:
8474             break;
8475         default:
8476             g_assert_not_reached();
8477         }
8478     }
8479 
8480     if (!fp_access_check(s)) {
8481         return;
8482     }
8483 
8484     if (size == 3) {
8485         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8486         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8487             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8488             { NULL, gen_helper_neon_qshl_u64 },
8489         };
8490         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8491         int maxpass = is_q ? 2 : 1;
8492 
8493         for (pass = 0; pass < maxpass; pass++) {
8494             TCGv_i64 tcg_op = tcg_temp_new_i64();
8495 
8496             read_vec_element(s, tcg_op, rn, pass, MO_64);
8497             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8498             write_vec_element(s, tcg_op, rd, pass, MO_64);
8499         }
8500         clear_vec_high(s, is_q, rd);
8501     } else {
8502         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8503         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8504             {
8505                 { gen_helper_neon_qshl_s8,
8506                   gen_helper_neon_qshl_s16,
8507                   gen_helper_neon_qshl_s32 },
8508                 { gen_helper_neon_qshlu_s8,
8509                   gen_helper_neon_qshlu_s16,
8510                   gen_helper_neon_qshlu_s32 }
8511             }, {
8512                 { NULL, NULL, NULL },
8513                 { gen_helper_neon_qshl_u8,
8514                   gen_helper_neon_qshl_u16,
8515                   gen_helper_neon_qshl_u32 }
8516             }
8517         };
8518         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8519         MemOp memop = scalar ? size : MO_32;
8520         int maxpass = scalar ? 1 : is_q ? 4 : 2;
8521 
8522         for (pass = 0; pass < maxpass; pass++) {
8523             TCGv_i32 tcg_op = tcg_temp_new_i32();
8524 
8525             read_vec_element_i32(s, tcg_op, rn, pass, memop);
8526             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8527             if (scalar) {
8528                 switch (size) {
8529                 case 0:
8530                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
8531                     break;
8532                 case 1:
8533                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8534                     break;
8535                 case 2:
8536                     break;
8537                 default:
8538                     g_assert_not_reached();
8539                 }
8540                 write_fp_sreg(s, rd, tcg_op);
8541             } else {
8542                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8543             }
8544         }
8545 
8546         if (!scalar) {
8547             clear_vec_high(s, is_q, rd);
8548         }
8549     }
8550 }
8551 
8552 /* Common vector code for handling integer to FP conversion */
8553 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8554                                    int elements, int is_signed,
8555                                    int fracbits, int size)
8556 {
8557     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8558     TCGv_i32 tcg_shift = NULL;
8559 
8560     MemOp mop = size | (is_signed ? MO_SIGN : 0);
8561     int pass;
8562 
8563     if (fracbits || size == MO_64) {
8564         tcg_shift = tcg_constant_i32(fracbits);
8565     }
8566 
8567     if (size == MO_64) {
8568         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8569         TCGv_i64 tcg_double = tcg_temp_new_i64();
8570 
8571         for (pass = 0; pass < elements; pass++) {
8572             read_vec_element(s, tcg_int64, rn, pass, mop);
8573 
8574             if (is_signed) {
8575                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8576                                      tcg_shift, tcg_fpst);
8577             } else {
8578                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8579                                      tcg_shift, tcg_fpst);
8580             }
8581             if (elements == 1) {
8582                 write_fp_dreg(s, rd, tcg_double);
8583             } else {
8584                 write_vec_element(s, tcg_double, rd, pass, MO_64);
8585             }
8586         }
8587     } else {
8588         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8589         TCGv_i32 tcg_float = tcg_temp_new_i32();
8590 
8591         for (pass = 0; pass < elements; pass++) {
8592             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8593 
8594             switch (size) {
8595             case MO_32:
8596                 if (fracbits) {
8597                     if (is_signed) {
8598                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
8599                                              tcg_shift, tcg_fpst);
8600                     } else {
8601                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
8602                                              tcg_shift, tcg_fpst);
8603                     }
8604                 } else {
8605                     if (is_signed) {
8606                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8607                     } else {
8608                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8609                     }
8610                 }
8611                 break;
8612             case MO_16:
8613                 if (fracbits) {
8614                     if (is_signed) {
8615                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8616                                              tcg_shift, tcg_fpst);
8617                     } else {
8618                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8619                                              tcg_shift, tcg_fpst);
8620                     }
8621                 } else {
8622                     if (is_signed) {
8623                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8624                     } else {
8625                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8626                     }
8627                 }
8628                 break;
8629             default:
8630                 g_assert_not_reached();
8631             }
8632 
8633             if (elements == 1) {
8634                 write_fp_sreg(s, rd, tcg_float);
8635             } else {
8636                 write_vec_element_i32(s, tcg_float, rd, pass, size);
8637             }
8638         }
8639     }
8640 
8641     clear_vec_high(s, elements << size == 16, rd);
8642 }
8643 
8644 /* UCVTF/SCVTF - Integer to FP conversion */
8645 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8646                                          bool is_q, bool is_u,
8647                                          int immh, int immb, int opcode,
8648                                          int rn, int rd)
8649 {
8650     int size, elements, fracbits;
8651     int immhb = immh << 3 | immb;
8652 
8653     if (immh & 8) {
8654         size = MO_64;
8655         if (!is_scalar && !is_q) {
8656             unallocated_encoding(s);
8657             return;
8658         }
8659     } else if (immh & 4) {
8660         size = MO_32;
8661     } else if (immh & 2) {
8662         size = MO_16;
8663         if (!dc_isar_feature(aa64_fp16, s)) {
8664             unallocated_encoding(s);
8665             return;
8666         }
8667     } else {
8668         /* immh == 0 would be a failure of the decode logic */
8669         g_assert(immh == 1);
8670         unallocated_encoding(s);
8671         return;
8672     }
8673 
8674     if (is_scalar) {
8675         elements = 1;
8676     } else {
8677         elements = (8 << is_q) >> size;
8678     }
8679     fracbits = (16 << size) - immhb;
8680 
8681     if (!fp_access_check(s)) {
8682         return;
8683     }
8684 
8685     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8686 }
8687 
8688 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8689 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8690                                          bool is_q, bool is_u,
8691                                          int immh, int immb, int rn, int rd)
8692 {
8693     int immhb = immh << 3 | immb;
8694     int pass, size, fracbits;
8695     TCGv_ptr tcg_fpstatus;
8696     TCGv_i32 tcg_rmode, tcg_shift;
8697 
8698     if (immh & 0x8) {
8699         size = MO_64;
8700         if (!is_scalar && !is_q) {
8701             unallocated_encoding(s);
8702             return;
8703         }
8704     } else if (immh & 0x4) {
8705         size = MO_32;
8706     } else if (immh & 0x2) {
8707         size = MO_16;
8708         if (!dc_isar_feature(aa64_fp16, s)) {
8709             unallocated_encoding(s);
8710             return;
8711         }
8712     } else {
8713         /* Should have split out AdvSIMD modified immediate earlier.  */
8714         assert(immh == 1);
8715         unallocated_encoding(s);
8716         return;
8717     }
8718 
8719     if (!fp_access_check(s)) {
8720         return;
8721     }
8722 
8723     assert(!(is_scalar && is_q));
8724 
8725     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8726     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
8727     fracbits = (16 << size) - immhb;
8728     tcg_shift = tcg_constant_i32(fracbits);
8729 
8730     if (size == MO_64) {
8731         int maxpass = is_scalar ? 1 : 2;
8732 
8733         for (pass = 0; pass < maxpass; pass++) {
8734             TCGv_i64 tcg_op = tcg_temp_new_i64();
8735 
8736             read_vec_element(s, tcg_op, rn, pass, MO_64);
8737             if (is_u) {
8738                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8739             } else {
8740                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8741             }
8742             write_vec_element(s, tcg_op, rd, pass, MO_64);
8743         }
8744         clear_vec_high(s, is_q, rd);
8745     } else {
8746         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8747         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8748 
8749         switch (size) {
8750         case MO_16:
8751             if (is_u) {
8752                 fn = gen_helper_vfp_touhh;
8753             } else {
8754                 fn = gen_helper_vfp_toshh;
8755             }
8756             break;
8757         case MO_32:
8758             if (is_u) {
8759                 fn = gen_helper_vfp_touls;
8760             } else {
8761                 fn = gen_helper_vfp_tosls;
8762             }
8763             break;
8764         default:
8765             g_assert_not_reached();
8766         }
8767 
8768         for (pass = 0; pass < maxpass; pass++) {
8769             TCGv_i32 tcg_op = tcg_temp_new_i32();
8770 
8771             read_vec_element_i32(s, tcg_op, rn, pass, size);
8772             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8773             if (is_scalar) {
8774                 write_fp_sreg(s, rd, tcg_op);
8775             } else {
8776                 write_vec_element_i32(s, tcg_op, rd, pass, size);
8777             }
8778         }
8779         if (!is_scalar) {
8780             clear_vec_high(s, is_q, rd);
8781         }
8782     }
8783 
8784     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
8785 }
8786 
8787 /* AdvSIMD scalar shift by immediate
8788  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
8789  * +-----+---+-------------+------+------+--------+---+------+------+
8790  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8791  * +-----+---+-------------+------+------+--------+---+------+------+
8792  *
8793  * This is the scalar version so it works on a fixed sized registers
8794  */
8795 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8796 {
8797     int rd = extract32(insn, 0, 5);
8798     int rn = extract32(insn, 5, 5);
8799     int opcode = extract32(insn, 11, 5);
8800     int immb = extract32(insn, 16, 3);
8801     int immh = extract32(insn, 19, 4);
8802     bool is_u = extract32(insn, 29, 1);
8803 
8804     if (immh == 0) {
8805         unallocated_encoding(s);
8806         return;
8807     }
8808 
8809     switch (opcode) {
8810     case 0x08: /* SRI */
8811         if (!is_u) {
8812             unallocated_encoding(s);
8813             return;
8814         }
8815         /* fall through */
8816     case 0x00: /* SSHR / USHR */
8817     case 0x02: /* SSRA / USRA */
8818     case 0x04: /* SRSHR / URSHR */
8819     case 0x06: /* SRSRA / URSRA */
8820         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8821         break;
8822     case 0x0a: /* SHL / SLI */
8823         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8824         break;
8825     case 0x1c: /* SCVTF, UCVTF */
8826         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8827                                      opcode, rn, rd);
8828         break;
8829     case 0x10: /* SQSHRUN, SQSHRUN2 */
8830     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8831         if (!is_u) {
8832             unallocated_encoding(s);
8833             return;
8834         }
8835         handle_vec_simd_sqshrn(s, true, false, false, true,
8836                                immh, immb, opcode, rn, rd);
8837         break;
8838     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8839     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8840         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8841                                immh, immb, opcode, rn, rd);
8842         break;
8843     case 0xc: /* SQSHLU */
8844         if (!is_u) {
8845             unallocated_encoding(s);
8846             return;
8847         }
8848         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8849         break;
8850     case 0xe: /* SQSHL, UQSHL */
8851         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8852         break;
8853     case 0x1f: /* FCVTZS, FCVTZU */
8854         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8855         break;
8856     default:
8857         unallocated_encoding(s);
8858         break;
8859     }
8860 }
8861 
8862 /* AdvSIMD scalar three different
8863  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
8864  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8865  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
8866  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8867  */
8868 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8869 {
8870     bool is_u = extract32(insn, 29, 1);
8871     int size = extract32(insn, 22, 2);
8872     int opcode = extract32(insn, 12, 4);
8873     int rm = extract32(insn, 16, 5);
8874     int rn = extract32(insn, 5, 5);
8875     int rd = extract32(insn, 0, 5);
8876 
8877     if (is_u) {
8878         unallocated_encoding(s);
8879         return;
8880     }
8881 
8882     switch (opcode) {
8883     case 0x9: /* SQDMLAL, SQDMLAL2 */
8884     case 0xb: /* SQDMLSL, SQDMLSL2 */
8885     case 0xd: /* SQDMULL, SQDMULL2 */
8886         if (size == 0 || size == 3) {
8887             unallocated_encoding(s);
8888             return;
8889         }
8890         break;
8891     default:
8892         unallocated_encoding(s);
8893         return;
8894     }
8895 
8896     if (!fp_access_check(s)) {
8897         return;
8898     }
8899 
8900     if (size == 2) {
8901         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8902         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8903         TCGv_i64 tcg_res = tcg_temp_new_i64();
8904 
8905         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8906         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8907 
8908         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8909         gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
8910 
8911         switch (opcode) {
8912         case 0xd: /* SQDMULL, SQDMULL2 */
8913             break;
8914         case 0xb: /* SQDMLSL, SQDMLSL2 */
8915             tcg_gen_neg_i64(tcg_res, tcg_res);
8916             /* fall through */
8917         case 0x9: /* SQDMLAL, SQDMLAL2 */
8918             read_vec_element(s, tcg_op1, rd, 0, MO_64);
8919             gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
8920                                               tcg_res, tcg_op1);
8921             break;
8922         default:
8923             g_assert_not_reached();
8924         }
8925 
8926         write_fp_dreg(s, rd, tcg_res);
8927     } else {
8928         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8929         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8930         TCGv_i64 tcg_res = tcg_temp_new_i64();
8931 
8932         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8933         gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
8934 
8935         switch (opcode) {
8936         case 0xd: /* SQDMULL, SQDMULL2 */
8937             break;
8938         case 0xb: /* SQDMLSL, SQDMLSL2 */
8939             gen_helper_neon_negl_u32(tcg_res, tcg_res);
8940             /* fall through */
8941         case 0x9: /* SQDMLAL, SQDMLAL2 */
8942         {
8943             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
8944             read_vec_element(s, tcg_op3, rd, 0, MO_32);
8945             gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
8946                                               tcg_res, tcg_op3);
8947             break;
8948         }
8949         default:
8950             g_assert_not_reached();
8951         }
8952 
8953         tcg_gen_ext32u_i64(tcg_res, tcg_res);
8954         write_fp_dreg(s, rd, tcg_res);
8955     }
8956 }
8957 
8958 static void handle_3same_64(DisasContext *s, int opcode, bool u,
8959                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
8960 {
8961     /* Handle 64x64->64 opcodes which are shared between the scalar
8962      * and vector 3-same groups. We cover every opcode where size == 3
8963      * is valid in either the three-reg-same (integer, not pairwise)
8964      * or scalar-three-reg-same groups.
8965      */
8966     TCGCond cond;
8967 
8968     switch (opcode) {
8969     case 0x1: /* SQADD */
8970         if (u) {
8971             gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8972         } else {
8973             gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8974         }
8975         break;
8976     case 0x5: /* SQSUB */
8977         if (u) {
8978             gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8979         } else {
8980             gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8981         }
8982         break;
8983     case 0x6: /* CMGT, CMHI */
8984         /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
8985          * We implement this using setcond (test) and then negating.
8986          */
8987         cond = u ? TCG_COND_GTU : TCG_COND_GT;
8988     do_cmop:
8989         tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
8990         tcg_gen_neg_i64(tcg_rd, tcg_rd);
8991         break;
8992     case 0x7: /* CMGE, CMHS */
8993         cond = u ? TCG_COND_GEU : TCG_COND_GE;
8994         goto do_cmop;
8995     case 0x11: /* CMTST, CMEQ */
8996         if (u) {
8997             cond = TCG_COND_EQ;
8998             goto do_cmop;
8999         }
9000         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
9001         break;
9002     case 0x8: /* SSHL, USHL */
9003         if (u) {
9004             gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
9005         } else {
9006             gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
9007         }
9008         break;
9009     case 0x9: /* SQSHL, UQSHL */
9010         if (u) {
9011             gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9012         } else {
9013             gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9014         }
9015         break;
9016     case 0xa: /* SRSHL, URSHL */
9017         if (u) {
9018             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
9019         } else {
9020             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
9021         }
9022         break;
9023     case 0xb: /* SQRSHL, UQRSHL */
9024         if (u) {
9025             gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9026         } else {
9027             gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9028         }
9029         break;
9030     case 0x10: /* ADD, SUB */
9031         if (u) {
9032             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
9033         } else {
9034             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
9035         }
9036         break;
9037     default:
9038         g_assert_not_reached();
9039     }
9040 }
9041 
9042 /* Handle the 3-same-operands float operations; shared by the scalar
9043  * and vector encodings. The caller must filter out any encodings
9044  * not allocated for the encoding it is dealing with.
9045  */
9046 static void handle_3same_float(DisasContext *s, int size, int elements,
9047                                int fpopcode, int rd, int rn, int rm)
9048 {
9049     int pass;
9050     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9051 
9052     for (pass = 0; pass < elements; pass++) {
9053         if (size) {
9054             /* Double */
9055             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9056             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9057             TCGv_i64 tcg_res = tcg_temp_new_i64();
9058 
9059             read_vec_element(s, tcg_op1, rn, pass, MO_64);
9060             read_vec_element(s, tcg_op2, rm, pass, MO_64);
9061 
9062             switch (fpopcode) {
9063             case 0x39: /* FMLS */
9064                 /* As usual for ARM, separate negation for fused multiply-add */
9065                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
9066                 /* fall through */
9067             case 0x19: /* FMLA */
9068                 read_vec_element(s, tcg_res, rd, pass, MO_64);
9069                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
9070                                        tcg_res, fpst);
9071                 break;
9072             case 0x18: /* FMAXNM */
9073                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9074                 break;
9075             case 0x1a: /* FADD */
9076                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
9077                 break;
9078             case 0x1b: /* FMULX */
9079                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
9080                 break;
9081             case 0x1c: /* FCMEQ */
9082                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9083                 break;
9084             case 0x1e: /* FMAX */
9085                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
9086                 break;
9087             case 0x1f: /* FRECPS */
9088                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9089                 break;
9090             case 0x38: /* FMINNM */
9091                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9092                 break;
9093             case 0x3a: /* FSUB */
9094                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9095                 break;
9096             case 0x3e: /* FMIN */
9097                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
9098                 break;
9099             case 0x3f: /* FRSQRTS */
9100                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9101                 break;
9102             case 0x5b: /* FMUL */
9103                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
9104                 break;
9105             case 0x5c: /* FCMGE */
9106                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9107                 break;
9108             case 0x5d: /* FACGE */
9109                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9110                 break;
9111             case 0x5f: /* FDIV */
9112                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
9113                 break;
9114             case 0x7a: /* FABD */
9115                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9116                 gen_helper_vfp_absd(tcg_res, tcg_res);
9117                 break;
9118             case 0x7c: /* FCMGT */
9119                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9120                 break;
9121             case 0x7d: /* FACGT */
9122                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9123                 break;
9124             default:
9125                 g_assert_not_reached();
9126             }
9127 
9128             write_vec_element(s, tcg_res, rd, pass, MO_64);
9129         } else {
9130             /* Single */
9131             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9132             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9133             TCGv_i32 tcg_res = tcg_temp_new_i32();
9134 
9135             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9136             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9137 
9138             switch (fpopcode) {
9139             case 0x39: /* FMLS */
9140                 /* As usual for ARM, separate negation for fused multiply-add */
9141                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
9142                 /* fall through */
9143             case 0x19: /* FMLA */
9144                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9145                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
9146                                        tcg_res, fpst);
9147                 break;
9148             case 0x1a: /* FADD */
9149                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
9150                 break;
9151             case 0x1b: /* FMULX */
9152                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
9153                 break;
9154             case 0x1c: /* FCMEQ */
9155                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9156                 break;
9157             case 0x1e: /* FMAX */
9158                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
9159                 break;
9160             case 0x1f: /* FRECPS */
9161                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9162                 break;
9163             case 0x18: /* FMAXNM */
9164                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
9165                 break;
9166             case 0x38: /* FMINNM */
9167                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
9168                 break;
9169             case 0x3a: /* FSUB */
9170                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9171                 break;
9172             case 0x3e: /* FMIN */
9173                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9174                 break;
9175             case 0x3f: /* FRSQRTS */
9176                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9177                 break;
9178             case 0x5b: /* FMUL */
9179                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9180                 break;
9181             case 0x5c: /* FCMGE */
9182                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9183                 break;
9184             case 0x5d: /* FACGE */
9185                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9186                 break;
9187             case 0x5f: /* FDIV */
9188                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9189                 break;
9190             case 0x7a: /* FABD */
9191                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9192                 gen_helper_vfp_abss(tcg_res, tcg_res);
9193                 break;
9194             case 0x7c: /* FCMGT */
9195                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9196                 break;
9197             case 0x7d: /* FACGT */
9198                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9199                 break;
9200             default:
9201                 g_assert_not_reached();
9202             }
9203 
9204             if (elements == 1) {
9205                 /* scalar single so clear high part */
9206                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9207 
9208                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9209                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9210             } else {
9211                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9212             }
9213         }
9214     }
9215 
9216     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9217 }
9218 
9219 /* AdvSIMD scalar three same
9220  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
9221  * +-----+---+-----------+------+---+------+--------+---+------+------+
9222  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
9223  * +-----+---+-----------+------+---+------+--------+---+------+------+
9224  */
9225 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9226 {
9227     int rd = extract32(insn, 0, 5);
9228     int rn = extract32(insn, 5, 5);
9229     int opcode = extract32(insn, 11, 5);
9230     int rm = extract32(insn, 16, 5);
9231     int size = extract32(insn, 22, 2);
9232     bool u = extract32(insn, 29, 1);
9233     TCGv_i64 tcg_rd;
9234 
9235     if (opcode >= 0x18) {
9236         /* Floating point: U, size[1] and opcode indicate operation */
9237         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9238         switch (fpopcode) {
9239         case 0x1b: /* FMULX */
9240         case 0x1f: /* FRECPS */
9241         case 0x3f: /* FRSQRTS */
9242         case 0x5d: /* FACGE */
9243         case 0x7d: /* FACGT */
9244         case 0x1c: /* FCMEQ */
9245         case 0x5c: /* FCMGE */
9246         case 0x7c: /* FCMGT */
9247         case 0x7a: /* FABD */
9248             break;
9249         default:
9250             unallocated_encoding(s);
9251             return;
9252         }
9253 
9254         if (!fp_access_check(s)) {
9255             return;
9256         }
9257 
9258         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9259         return;
9260     }
9261 
9262     switch (opcode) {
9263     case 0x1: /* SQADD, UQADD */
9264     case 0x5: /* SQSUB, UQSUB */
9265     case 0x9: /* SQSHL, UQSHL */
9266     case 0xb: /* SQRSHL, UQRSHL */
9267         break;
9268     case 0x8: /* SSHL, USHL */
9269     case 0xa: /* SRSHL, URSHL */
9270     case 0x6: /* CMGT, CMHI */
9271     case 0x7: /* CMGE, CMHS */
9272     case 0x11: /* CMTST, CMEQ */
9273     case 0x10: /* ADD, SUB (vector) */
9274         if (size != 3) {
9275             unallocated_encoding(s);
9276             return;
9277         }
9278         break;
9279     case 0x16: /* SQDMULH, SQRDMULH (vector) */
9280         if (size != 1 && size != 2) {
9281             unallocated_encoding(s);
9282             return;
9283         }
9284         break;
9285     default:
9286         unallocated_encoding(s);
9287         return;
9288     }
9289 
9290     if (!fp_access_check(s)) {
9291         return;
9292     }
9293 
9294     tcg_rd = tcg_temp_new_i64();
9295 
9296     if (size == 3) {
9297         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9298         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9299 
9300         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9301     } else {
9302         /* Do a single operation on the lowest element in the vector.
9303          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9304          * no side effects for all these operations.
9305          * OPTME: special-purpose helpers would avoid doing some
9306          * unnecessary work in the helper for the 8 and 16 bit cases.
9307          */
9308         NeonGenTwoOpEnvFn *genenvfn;
9309         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9310         TCGv_i32 tcg_rm = tcg_temp_new_i32();
9311         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9312 
9313         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9314         read_vec_element_i32(s, tcg_rm, rm, 0, size);
9315 
9316         switch (opcode) {
9317         case 0x1: /* SQADD, UQADD */
9318         {
9319             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9320                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9321                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9322                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9323             };
9324             genenvfn = fns[size][u];
9325             break;
9326         }
9327         case 0x5: /* SQSUB, UQSUB */
9328         {
9329             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9330                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9331                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9332                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9333             };
9334             genenvfn = fns[size][u];
9335             break;
9336         }
9337         case 0x9: /* SQSHL, UQSHL */
9338         {
9339             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9340                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9341                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9342                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9343             };
9344             genenvfn = fns[size][u];
9345             break;
9346         }
9347         case 0xb: /* SQRSHL, UQRSHL */
9348         {
9349             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9350                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9351                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9352                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9353             };
9354             genenvfn = fns[size][u];
9355             break;
9356         }
9357         case 0x16: /* SQDMULH, SQRDMULH */
9358         {
9359             static NeonGenTwoOpEnvFn * const fns[2][2] = {
9360                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9361                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9362             };
9363             assert(size == 1 || size == 2);
9364             genenvfn = fns[size - 1][u];
9365             break;
9366         }
9367         default:
9368             g_assert_not_reached();
9369         }
9370 
9371         genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9372         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9373     }
9374 
9375     write_fp_dreg(s, rd, tcg_rd);
9376 }
9377 
9378 /* AdvSIMD scalar three same FP16
9379  *  31 30  29 28       24 23  22 21 20  16 15 14 13    11 10  9  5 4  0
9380  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9381  * | 0 1 | U | 1 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 | Rn | Rd |
9382  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9383  * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9384  * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9385  */
9386 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9387                                                   uint32_t insn)
9388 {
9389     int rd = extract32(insn, 0, 5);
9390     int rn = extract32(insn, 5, 5);
9391     int opcode = extract32(insn, 11, 3);
9392     int rm = extract32(insn, 16, 5);
9393     bool u = extract32(insn, 29, 1);
9394     bool a = extract32(insn, 23, 1);
9395     int fpopcode = opcode | (a << 3) |  (u << 4);
9396     TCGv_ptr fpst;
9397     TCGv_i32 tcg_op1;
9398     TCGv_i32 tcg_op2;
9399     TCGv_i32 tcg_res;
9400 
9401     switch (fpopcode) {
9402     case 0x03: /* FMULX */
9403     case 0x04: /* FCMEQ (reg) */
9404     case 0x07: /* FRECPS */
9405     case 0x0f: /* FRSQRTS */
9406     case 0x14: /* FCMGE (reg) */
9407     case 0x15: /* FACGE */
9408     case 0x1a: /* FABD */
9409     case 0x1c: /* FCMGT (reg) */
9410     case 0x1d: /* FACGT */
9411         break;
9412     default:
9413         unallocated_encoding(s);
9414         return;
9415     }
9416 
9417     if (!dc_isar_feature(aa64_fp16, s)) {
9418         unallocated_encoding(s);
9419     }
9420 
9421     if (!fp_access_check(s)) {
9422         return;
9423     }
9424 
9425     fpst = fpstatus_ptr(FPST_FPCR_F16);
9426 
9427     tcg_op1 = read_fp_hreg(s, rn);
9428     tcg_op2 = read_fp_hreg(s, rm);
9429     tcg_res = tcg_temp_new_i32();
9430 
9431     switch (fpopcode) {
9432     case 0x03: /* FMULX */
9433         gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9434         break;
9435     case 0x04: /* FCMEQ (reg) */
9436         gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9437         break;
9438     case 0x07: /* FRECPS */
9439         gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9440         break;
9441     case 0x0f: /* FRSQRTS */
9442         gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9443         break;
9444     case 0x14: /* FCMGE (reg) */
9445         gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9446         break;
9447     case 0x15: /* FACGE */
9448         gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9449         break;
9450     case 0x1a: /* FABD */
9451         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9452         tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9453         break;
9454     case 0x1c: /* FCMGT (reg) */
9455         gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9456         break;
9457     case 0x1d: /* FACGT */
9458         gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9459         break;
9460     default:
9461         g_assert_not_reached();
9462     }
9463 
9464     write_fp_sreg(s, rd, tcg_res);
9465 }
9466 
9467 /* AdvSIMD scalar three same extra
9468  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9469  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9470  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9471  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9472  */
9473 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9474                                                    uint32_t insn)
9475 {
9476     int rd = extract32(insn, 0, 5);
9477     int rn = extract32(insn, 5, 5);
9478     int opcode = extract32(insn, 11, 4);
9479     int rm = extract32(insn, 16, 5);
9480     int size = extract32(insn, 22, 2);
9481     bool u = extract32(insn, 29, 1);
9482     TCGv_i32 ele1, ele2, ele3;
9483     TCGv_i64 res;
9484     bool feature;
9485 
9486     switch (u * 16 + opcode) {
9487     case 0x10: /* SQRDMLAH (vector) */
9488     case 0x11: /* SQRDMLSH (vector) */
9489         if (size != 1 && size != 2) {
9490             unallocated_encoding(s);
9491             return;
9492         }
9493         feature = dc_isar_feature(aa64_rdm, s);
9494         break;
9495     default:
9496         unallocated_encoding(s);
9497         return;
9498     }
9499     if (!feature) {
9500         unallocated_encoding(s);
9501         return;
9502     }
9503     if (!fp_access_check(s)) {
9504         return;
9505     }
9506 
9507     /* Do a single operation on the lowest element in the vector.
9508      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9509      * with no side effects for all these operations.
9510      * OPTME: special-purpose helpers would avoid doing some
9511      * unnecessary work in the helper for the 16 bit cases.
9512      */
9513     ele1 = tcg_temp_new_i32();
9514     ele2 = tcg_temp_new_i32();
9515     ele3 = tcg_temp_new_i32();
9516 
9517     read_vec_element_i32(s, ele1, rn, 0, size);
9518     read_vec_element_i32(s, ele2, rm, 0, size);
9519     read_vec_element_i32(s, ele3, rd, 0, size);
9520 
9521     switch (opcode) {
9522     case 0x0: /* SQRDMLAH */
9523         if (size == 1) {
9524             gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9525         } else {
9526             gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9527         }
9528         break;
9529     case 0x1: /* SQRDMLSH */
9530         if (size == 1) {
9531             gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9532         } else {
9533             gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9534         }
9535         break;
9536     default:
9537         g_assert_not_reached();
9538     }
9539 
9540     res = tcg_temp_new_i64();
9541     tcg_gen_extu_i32_i64(res, ele3);
9542     write_fp_dreg(s, rd, res);
9543 }
9544 
9545 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9546                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9547                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9548 {
9549     /* Handle 64->64 opcodes which are shared between the scalar and
9550      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9551      * is valid in either group and also the double-precision fp ops.
9552      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9553      * requires them.
9554      */
9555     TCGCond cond;
9556 
9557     switch (opcode) {
9558     case 0x4: /* CLS, CLZ */
9559         if (u) {
9560             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9561         } else {
9562             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9563         }
9564         break;
9565     case 0x5: /* NOT */
9566         /* This opcode is shared with CNT and RBIT but we have earlier
9567          * enforced that size == 3 if and only if this is the NOT insn.
9568          */
9569         tcg_gen_not_i64(tcg_rd, tcg_rn);
9570         break;
9571     case 0x7: /* SQABS, SQNEG */
9572         if (u) {
9573             gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9574         } else {
9575             gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9576         }
9577         break;
9578     case 0xa: /* CMLT */
9579         /* 64 bit integer comparison against zero, result is
9580          * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9581          * subtracting 1.
9582          */
9583         cond = TCG_COND_LT;
9584     do_cmop:
9585         tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9586         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9587         break;
9588     case 0x8: /* CMGT, CMGE */
9589         cond = u ? TCG_COND_GE : TCG_COND_GT;
9590         goto do_cmop;
9591     case 0x9: /* CMEQ, CMLE */
9592         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9593         goto do_cmop;
9594     case 0xb: /* ABS, NEG */
9595         if (u) {
9596             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9597         } else {
9598             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9599         }
9600         break;
9601     case 0x2f: /* FABS */
9602         gen_helper_vfp_absd(tcg_rd, tcg_rn);
9603         break;
9604     case 0x6f: /* FNEG */
9605         gen_helper_vfp_negd(tcg_rd, tcg_rn);
9606         break;
9607     case 0x7f: /* FSQRT */
9608         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9609         break;
9610     case 0x1a: /* FCVTNS */
9611     case 0x1b: /* FCVTMS */
9612     case 0x1c: /* FCVTAS */
9613     case 0x3a: /* FCVTPS */
9614     case 0x3b: /* FCVTZS */
9615         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9616         break;
9617     case 0x5a: /* FCVTNU */
9618     case 0x5b: /* FCVTMU */
9619     case 0x5c: /* FCVTAU */
9620     case 0x7a: /* FCVTPU */
9621     case 0x7b: /* FCVTZU */
9622         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9623         break;
9624     case 0x18: /* FRINTN */
9625     case 0x19: /* FRINTM */
9626     case 0x38: /* FRINTP */
9627     case 0x39: /* FRINTZ */
9628     case 0x58: /* FRINTA */
9629     case 0x79: /* FRINTI */
9630         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9631         break;
9632     case 0x59: /* FRINTX */
9633         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9634         break;
9635     case 0x1e: /* FRINT32Z */
9636     case 0x5e: /* FRINT32X */
9637         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9638         break;
9639     case 0x1f: /* FRINT64Z */
9640     case 0x5f: /* FRINT64X */
9641         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9642         break;
9643     default:
9644         g_assert_not_reached();
9645     }
9646 }
9647 
9648 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9649                                    bool is_scalar, bool is_u, bool is_q,
9650                                    int size, int rn, int rd)
9651 {
9652     bool is_double = (size == MO_64);
9653     TCGv_ptr fpst;
9654 
9655     if (!fp_access_check(s)) {
9656         return;
9657     }
9658 
9659     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9660 
9661     if (is_double) {
9662         TCGv_i64 tcg_op = tcg_temp_new_i64();
9663         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9664         TCGv_i64 tcg_res = tcg_temp_new_i64();
9665         NeonGenTwoDoubleOpFn *genfn;
9666         bool swap = false;
9667         int pass;
9668 
9669         switch (opcode) {
9670         case 0x2e: /* FCMLT (zero) */
9671             swap = true;
9672             /* fallthrough */
9673         case 0x2c: /* FCMGT (zero) */
9674             genfn = gen_helper_neon_cgt_f64;
9675             break;
9676         case 0x2d: /* FCMEQ (zero) */
9677             genfn = gen_helper_neon_ceq_f64;
9678             break;
9679         case 0x6d: /* FCMLE (zero) */
9680             swap = true;
9681             /* fall through */
9682         case 0x6c: /* FCMGE (zero) */
9683             genfn = gen_helper_neon_cge_f64;
9684             break;
9685         default:
9686             g_assert_not_reached();
9687         }
9688 
9689         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9690             read_vec_element(s, tcg_op, rn, pass, MO_64);
9691             if (swap) {
9692                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9693             } else {
9694                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9695             }
9696             write_vec_element(s, tcg_res, rd, pass, MO_64);
9697         }
9698 
9699         clear_vec_high(s, !is_scalar, rd);
9700     } else {
9701         TCGv_i32 tcg_op = tcg_temp_new_i32();
9702         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9703         TCGv_i32 tcg_res = tcg_temp_new_i32();
9704         NeonGenTwoSingleOpFn *genfn;
9705         bool swap = false;
9706         int pass, maxpasses;
9707 
9708         if (size == MO_16) {
9709             switch (opcode) {
9710             case 0x2e: /* FCMLT (zero) */
9711                 swap = true;
9712                 /* fall through */
9713             case 0x2c: /* FCMGT (zero) */
9714                 genfn = gen_helper_advsimd_cgt_f16;
9715                 break;
9716             case 0x2d: /* FCMEQ (zero) */
9717                 genfn = gen_helper_advsimd_ceq_f16;
9718                 break;
9719             case 0x6d: /* FCMLE (zero) */
9720                 swap = true;
9721                 /* fall through */
9722             case 0x6c: /* FCMGE (zero) */
9723                 genfn = gen_helper_advsimd_cge_f16;
9724                 break;
9725             default:
9726                 g_assert_not_reached();
9727             }
9728         } else {
9729             switch (opcode) {
9730             case 0x2e: /* FCMLT (zero) */
9731                 swap = true;
9732                 /* fall through */
9733             case 0x2c: /* FCMGT (zero) */
9734                 genfn = gen_helper_neon_cgt_f32;
9735                 break;
9736             case 0x2d: /* FCMEQ (zero) */
9737                 genfn = gen_helper_neon_ceq_f32;
9738                 break;
9739             case 0x6d: /* FCMLE (zero) */
9740                 swap = true;
9741                 /* fall through */
9742             case 0x6c: /* FCMGE (zero) */
9743                 genfn = gen_helper_neon_cge_f32;
9744                 break;
9745             default:
9746                 g_assert_not_reached();
9747             }
9748         }
9749 
9750         if (is_scalar) {
9751             maxpasses = 1;
9752         } else {
9753             int vector_size = 8 << is_q;
9754             maxpasses = vector_size >> size;
9755         }
9756 
9757         for (pass = 0; pass < maxpasses; pass++) {
9758             read_vec_element_i32(s, tcg_op, rn, pass, size);
9759             if (swap) {
9760                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9761             } else {
9762                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9763             }
9764             if (is_scalar) {
9765                 write_fp_sreg(s, rd, tcg_res);
9766             } else {
9767                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9768             }
9769         }
9770 
9771         if (!is_scalar) {
9772             clear_vec_high(s, is_q, rd);
9773         }
9774     }
9775 }
9776 
9777 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9778                                     bool is_scalar, bool is_u, bool is_q,
9779                                     int size, int rn, int rd)
9780 {
9781     bool is_double = (size == 3);
9782     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9783 
9784     if (is_double) {
9785         TCGv_i64 tcg_op = tcg_temp_new_i64();
9786         TCGv_i64 tcg_res = tcg_temp_new_i64();
9787         int pass;
9788 
9789         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9790             read_vec_element(s, tcg_op, rn, pass, MO_64);
9791             switch (opcode) {
9792             case 0x3d: /* FRECPE */
9793                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9794                 break;
9795             case 0x3f: /* FRECPX */
9796                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9797                 break;
9798             case 0x7d: /* FRSQRTE */
9799                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9800                 break;
9801             default:
9802                 g_assert_not_reached();
9803             }
9804             write_vec_element(s, tcg_res, rd, pass, MO_64);
9805         }
9806         clear_vec_high(s, !is_scalar, rd);
9807     } else {
9808         TCGv_i32 tcg_op = tcg_temp_new_i32();
9809         TCGv_i32 tcg_res = tcg_temp_new_i32();
9810         int pass, maxpasses;
9811 
9812         if (is_scalar) {
9813             maxpasses = 1;
9814         } else {
9815             maxpasses = is_q ? 4 : 2;
9816         }
9817 
9818         for (pass = 0; pass < maxpasses; pass++) {
9819             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9820 
9821             switch (opcode) {
9822             case 0x3c: /* URECPE */
9823                 gen_helper_recpe_u32(tcg_res, tcg_op);
9824                 break;
9825             case 0x3d: /* FRECPE */
9826                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9827                 break;
9828             case 0x3f: /* FRECPX */
9829                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9830                 break;
9831             case 0x7d: /* FRSQRTE */
9832                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9833                 break;
9834             default:
9835                 g_assert_not_reached();
9836             }
9837 
9838             if (is_scalar) {
9839                 write_fp_sreg(s, rd, tcg_res);
9840             } else {
9841                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9842             }
9843         }
9844         if (!is_scalar) {
9845             clear_vec_high(s, is_q, rd);
9846         }
9847     }
9848 }
9849 
9850 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9851                                 int opcode, bool u, bool is_q,
9852                                 int size, int rn, int rd)
9853 {
9854     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9855      * in the source becomes a size element in the destination).
9856      */
9857     int pass;
9858     TCGv_i32 tcg_res[2];
9859     int destelt = is_q ? 2 : 0;
9860     int passes = scalar ? 1 : 2;
9861 
9862     if (scalar) {
9863         tcg_res[1] = tcg_constant_i32(0);
9864     }
9865 
9866     for (pass = 0; pass < passes; pass++) {
9867         TCGv_i64 tcg_op = tcg_temp_new_i64();
9868         NeonGenNarrowFn *genfn = NULL;
9869         NeonGenNarrowEnvFn *genenvfn = NULL;
9870 
9871         if (scalar) {
9872             read_vec_element(s, tcg_op, rn, pass, size + 1);
9873         } else {
9874             read_vec_element(s, tcg_op, rn, pass, MO_64);
9875         }
9876         tcg_res[pass] = tcg_temp_new_i32();
9877 
9878         switch (opcode) {
9879         case 0x12: /* XTN, SQXTUN */
9880         {
9881             static NeonGenNarrowFn * const xtnfns[3] = {
9882                 gen_helper_neon_narrow_u8,
9883                 gen_helper_neon_narrow_u16,
9884                 tcg_gen_extrl_i64_i32,
9885             };
9886             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9887                 gen_helper_neon_unarrow_sat8,
9888                 gen_helper_neon_unarrow_sat16,
9889                 gen_helper_neon_unarrow_sat32,
9890             };
9891             if (u) {
9892                 genenvfn = sqxtunfns[size];
9893             } else {
9894                 genfn = xtnfns[size];
9895             }
9896             break;
9897         }
9898         case 0x14: /* SQXTN, UQXTN */
9899         {
9900             static NeonGenNarrowEnvFn * const fns[3][2] = {
9901                 { gen_helper_neon_narrow_sat_s8,
9902                   gen_helper_neon_narrow_sat_u8 },
9903                 { gen_helper_neon_narrow_sat_s16,
9904                   gen_helper_neon_narrow_sat_u16 },
9905                 { gen_helper_neon_narrow_sat_s32,
9906                   gen_helper_neon_narrow_sat_u32 },
9907             };
9908             genenvfn = fns[size][u];
9909             break;
9910         }
9911         case 0x16: /* FCVTN, FCVTN2 */
9912             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9913             if (size == 2) {
9914                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
9915             } else {
9916                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9917                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9918                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9919                 TCGv_i32 ahp = get_ahp_flag();
9920 
9921                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9922                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9923                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9924                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9925             }
9926             break;
9927         case 0x36: /* BFCVTN, BFCVTN2 */
9928             {
9929                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9930                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
9931             }
9932             break;
9933         case 0x56:  /* FCVTXN, FCVTXN2 */
9934             /* 64 bit to 32 bit float conversion
9935              * with von Neumann rounding (round to odd)
9936              */
9937             assert(size == 2);
9938             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
9939             break;
9940         default:
9941             g_assert_not_reached();
9942         }
9943 
9944         if (genfn) {
9945             genfn(tcg_res[pass], tcg_op);
9946         } else if (genenvfn) {
9947             genenvfn(tcg_res[pass], cpu_env, tcg_op);
9948         }
9949     }
9950 
9951     for (pass = 0; pass < 2; pass++) {
9952         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
9953     }
9954     clear_vec_high(s, is_q, rd);
9955 }
9956 
9957 /* Remaining saturating accumulating ops */
9958 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
9959                                 bool is_q, int size, int rn, int rd)
9960 {
9961     bool is_double = (size == 3);
9962 
9963     if (is_double) {
9964         TCGv_i64 tcg_rn = tcg_temp_new_i64();
9965         TCGv_i64 tcg_rd = tcg_temp_new_i64();
9966         int pass;
9967 
9968         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9969             read_vec_element(s, tcg_rn, rn, pass, MO_64);
9970             read_vec_element(s, tcg_rd, rd, pass, MO_64);
9971 
9972             if (is_u) { /* USQADD */
9973                 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9974             } else { /* SUQADD */
9975                 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9976             }
9977             write_vec_element(s, tcg_rd, rd, pass, MO_64);
9978         }
9979         clear_vec_high(s, !is_scalar, rd);
9980     } else {
9981         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9982         TCGv_i32 tcg_rd = tcg_temp_new_i32();
9983         int pass, maxpasses;
9984 
9985         if (is_scalar) {
9986             maxpasses = 1;
9987         } else {
9988             maxpasses = is_q ? 4 : 2;
9989         }
9990 
9991         for (pass = 0; pass < maxpasses; pass++) {
9992             if (is_scalar) {
9993                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
9994                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
9995             } else {
9996                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
9997                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9998             }
9999 
10000             if (is_u) { /* USQADD */
10001                 switch (size) {
10002                 case 0:
10003                     gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10004                     break;
10005                 case 1:
10006                     gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10007                     break;
10008                 case 2:
10009                     gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10010                     break;
10011                 default:
10012                     g_assert_not_reached();
10013                 }
10014             } else { /* SUQADD */
10015                 switch (size) {
10016                 case 0:
10017                     gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10018                     break;
10019                 case 1:
10020                     gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10021                     break;
10022                 case 2:
10023                     gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10024                     break;
10025                 default:
10026                     g_assert_not_reached();
10027                 }
10028             }
10029 
10030             if (is_scalar) {
10031                 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
10032             }
10033             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10034         }
10035         clear_vec_high(s, is_q, rd);
10036     }
10037 }
10038 
10039 /* AdvSIMD scalar two reg misc
10040  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
10041  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10042  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
10043  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10044  */
10045 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
10046 {
10047     int rd = extract32(insn, 0, 5);
10048     int rn = extract32(insn, 5, 5);
10049     int opcode = extract32(insn, 12, 5);
10050     int size = extract32(insn, 22, 2);
10051     bool u = extract32(insn, 29, 1);
10052     bool is_fcvt = false;
10053     int rmode;
10054     TCGv_i32 tcg_rmode;
10055     TCGv_ptr tcg_fpstatus;
10056 
10057     switch (opcode) {
10058     case 0x3: /* USQADD / SUQADD*/
10059         if (!fp_access_check(s)) {
10060             return;
10061         }
10062         handle_2misc_satacc(s, true, u, false, size, rn, rd);
10063         return;
10064     case 0x7: /* SQABS / SQNEG */
10065         break;
10066     case 0xa: /* CMLT */
10067         if (u) {
10068             unallocated_encoding(s);
10069             return;
10070         }
10071         /* fall through */
10072     case 0x8: /* CMGT, CMGE */
10073     case 0x9: /* CMEQ, CMLE */
10074     case 0xb: /* ABS, NEG */
10075         if (size != 3) {
10076             unallocated_encoding(s);
10077             return;
10078         }
10079         break;
10080     case 0x12: /* SQXTUN */
10081         if (!u) {
10082             unallocated_encoding(s);
10083             return;
10084         }
10085         /* fall through */
10086     case 0x14: /* SQXTN, UQXTN */
10087         if (size == 3) {
10088             unallocated_encoding(s);
10089             return;
10090         }
10091         if (!fp_access_check(s)) {
10092             return;
10093         }
10094         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10095         return;
10096     case 0xc ... 0xf:
10097     case 0x16 ... 0x1d:
10098     case 0x1f:
10099         /* Floating point: U, size[1] and opcode indicate operation;
10100          * size[0] indicates single or double precision.
10101          */
10102         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10103         size = extract32(size, 0, 1) ? 3 : 2;
10104         switch (opcode) {
10105         case 0x2c: /* FCMGT (zero) */
10106         case 0x2d: /* FCMEQ (zero) */
10107         case 0x2e: /* FCMLT (zero) */
10108         case 0x6c: /* FCMGE (zero) */
10109         case 0x6d: /* FCMLE (zero) */
10110             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10111             return;
10112         case 0x1d: /* SCVTF */
10113         case 0x5d: /* UCVTF */
10114         {
10115             bool is_signed = (opcode == 0x1d);
10116             if (!fp_access_check(s)) {
10117                 return;
10118             }
10119             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10120             return;
10121         }
10122         case 0x3d: /* FRECPE */
10123         case 0x3f: /* FRECPX */
10124         case 0x7d: /* FRSQRTE */
10125             if (!fp_access_check(s)) {
10126                 return;
10127             }
10128             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10129             return;
10130         case 0x1a: /* FCVTNS */
10131         case 0x1b: /* FCVTMS */
10132         case 0x3a: /* FCVTPS */
10133         case 0x3b: /* FCVTZS */
10134         case 0x5a: /* FCVTNU */
10135         case 0x5b: /* FCVTMU */
10136         case 0x7a: /* FCVTPU */
10137         case 0x7b: /* FCVTZU */
10138             is_fcvt = true;
10139             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10140             break;
10141         case 0x1c: /* FCVTAS */
10142         case 0x5c: /* FCVTAU */
10143             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10144             is_fcvt = true;
10145             rmode = FPROUNDING_TIEAWAY;
10146             break;
10147         case 0x56: /* FCVTXN, FCVTXN2 */
10148             if (size == 2) {
10149                 unallocated_encoding(s);
10150                 return;
10151             }
10152             if (!fp_access_check(s)) {
10153                 return;
10154             }
10155             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10156             return;
10157         default:
10158             unallocated_encoding(s);
10159             return;
10160         }
10161         break;
10162     default:
10163         unallocated_encoding(s);
10164         return;
10165     }
10166 
10167     if (!fp_access_check(s)) {
10168         return;
10169     }
10170 
10171     if (is_fcvt) {
10172         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
10173         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
10174     } else {
10175         tcg_fpstatus = NULL;
10176         tcg_rmode = NULL;
10177     }
10178 
10179     if (size == 3) {
10180         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10181         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10182 
10183         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10184         write_fp_dreg(s, rd, tcg_rd);
10185     } else {
10186         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10187         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10188 
10189         read_vec_element_i32(s, tcg_rn, rn, 0, size);
10190 
10191         switch (opcode) {
10192         case 0x7: /* SQABS, SQNEG */
10193         {
10194             NeonGenOneOpEnvFn *genfn;
10195             static NeonGenOneOpEnvFn * const fns[3][2] = {
10196                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10197                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10198                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10199             };
10200             genfn = fns[size][u];
10201             genfn(tcg_rd, cpu_env, tcg_rn);
10202             break;
10203         }
10204         case 0x1a: /* FCVTNS */
10205         case 0x1b: /* FCVTMS */
10206         case 0x1c: /* FCVTAS */
10207         case 0x3a: /* FCVTPS */
10208         case 0x3b: /* FCVTZS */
10209             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10210                                  tcg_fpstatus);
10211             break;
10212         case 0x5a: /* FCVTNU */
10213         case 0x5b: /* FCVTMU */
10214         case 0x5c: /* FCVTAU */
10215         case 0x7a: /* FCVTPU */
10216         case 0x7b: /* FCVTZU */
10217             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10218                                  tcg_fpstatus);
10219             break;
10220         default:
10221             g_assert_not_reached();
10222         }
10223 
10224         write_fp_sreg(s, rd, tcg_rd);
10225     }
10226 
10227     if (is_fcvt) {
10228         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
10229     }
10230 }
10231 
10232 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10233 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10234                                  int immh, int immb, int opcode, int rn, int rd)
10235 {
10236     int size = 32 - clz32(immh) - 1;
10237     int immhb = immh << 3 | immb;
10238     int shift = 2 * (8 << size) - immhb;
10239     GVecGen2iFn *gvec_fn;
10240 
10241     if (extract32(immh, 3, 1) && !is_q) {
10242         unallocated_encoding(s);
10243         return;
10244     }
10245     tcg_debug_assert(size <= 3);
10246 
10247     if (!fp_access_check(s)) {
10248         return;
10249     }
10250 
10251     switch (opcode) {
10252     case 0x02: /* SSRA / USRA (accumulate) */
10253         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10254         break;
10255 
10256     case 0x08: /* SRI */
10257         gvec_fn = gen_gvec_sri;
10258         break;
10259 
10260     case 0x00: /* SSHR / USHR */
10261         if (is_u) {
10262             if (shift == 8 << size) {
10263                 /* Shift count the same size as element size produces zero.  */
10264                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10265                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
10266                 return;
10267             }
10268             gvec_fn = tcg_gen_gvec_shri;
10269         } else {
10270             /* Shift count the same size as element size produces all sign.  */
10271             if (shift == 8 << size) {
10272                 shift -= 1;
10273             }
10274             gvec_fn = tcg_gen_gvec_sari;
10275         }
10276         break;
10277 
10278     case 0x04: /* SRSHR / URSHR (rounding) */
10279         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10280         break;
10281 
10282     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10283         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10284         break;
10285 
10286     default:
10287         g_assert_not_reached();
10288     }
10289 
10290     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10291 }
10292 
10293 /* SHL/SLI - Vector shift left */
10294 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10295                                  int immh, int immb, int opcode, int rn, int rd)
10296 {
10297     int size = 32 - clz32(immh) - 1;
10298     int immhb = immh << 3 | immb;
10299     int shift = immhb - (8 << size);
10300 
10301     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10302     assert(size >= 0 && size <= 3);
10303 
10304     if (extract32(immh, 3, 1) && !is_q) {
10305         unallocated_encoding(s);
10306         return;
10307     }
10308 
10309     if (!fp_access_check(s)) {
10310         return;
10311     }
10312 
10313     if (insert) {
10314         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10315     } else {
10316         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10317     }
10318 }
10319 
10320 /* USHLL/SHLL - Vector shift left with widening */
10321 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10322                                  int immh, int immb, int opcode, int rn, int rd)
10323 {
10324     int size = 32 - clz32(immh) - 1;
10325     int immhb = immh << 3 | immb;
10326     int shift = immhb - (8 << size);
10327     int dsize = 64;
10328     int esize = 8 << size;
10329     int elements = dsize/esize;
10330     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10331     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10332     int i;
10333 
10334     if (size >= 3) {
10335         unallocated_encoding(s);
10336         return;
10337     }
10338 
10339     if (!fp_access_check(s)) {
10340         return;
10341     }
10342 
10343     /* For the LL variants the store is larger than the load,
10344      * so if rd == rn we would overwrite parts of our input.
10345      * So load everything right now and use shifts in the main loop.
10346      */
10347     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10348 
10349     for (i = 0; i < elements; i++) {
10350         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10351         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10352         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10353         write_vec_element(s, tcg_rd, rd, i, size + 1);
10354     }
10355 }
10356 
10357 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10358 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10359                                  int immh, int immb, int opcode, int rn, int rd)
10360 {
10361     int immhb = immh << 3 | immb;
10362     int size = 32 - clz32(immh) - 1;
10363     int dsize = 64;
10364     int esize = 8 << size;
10365     int elements = dsize/esize;
10366     int shift = (2 * esize) - immhb;
10367     bool round = extract32(opcode, 0, 1);
10368     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10369     TCGv_i64 tcg_round;
10370     int i;
10371 
10372     if (extract32(immh, 3, 1)) {
10373         unallocated_encoding(s);
10374         return;
10375     }
10376 
10377     if (!fp_access_check(s)) {
10378         return;
10379     }
10380 
10381     tcg_rn = tcg_temp_new_i64();
10382     tcg_rd = tcg_temp_new_i64();
10383     tcg_final = tcg_temp_new_i64();
10384     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10385 
10386     if (round) {
10387         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10388     } else {
10389         tcg_round = NULL;
10390     }
10391 
10392     for (i = 0; i < elements; i++) {
10393         read_vec_element(s, tcg_rn, rn, i, size+1);
10394         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10395                                 false, true, size+1, shift);
10396 
10397         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10398     }
10399 
10400     if (!is_q) {
10401         write_vec_element(s, tcg_final, rd, 0, MO_64);
10402     } else {
10403         write_vec_element(s, tcg_final, rd, 1, MO_64);
10404     }
10405 
10406     clear_vec_high(s, is_q, rd);
10407 }
10408 
10409 
10410 /* AdvSIMD shift by immediate
10411  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10412  * +---+---+---+-------------+------+------+--------+---+------+------+
10413  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10414  * +---+---+---+-------------+------+------+--------+---+------+------+
10415  */
10416 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10417 {
10418     int rd = extract32(insn, 0, 5);
10419     int rn = extract32(insn, 5, 5);
10420     int opcode = extract32(insn, 11, 5);
10421     int immb = extract32(insn, 16, 3);
10422     int immh = extract32(insn, 19, 4);
10423     bool is_u = extract32(insn, 29, 1);
10424     bool is_q = extract32(insn, 30, 1);
10425 
10426     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10427     assert(immh != 0);
10428 
10429     switch (opcode) {
10430     case 0x08: /* SRI */
10431         if (!is_u) {
10432             unallocated_encoding(s);
10433             return;
10434         }
10435         /* fall through */
10436     case 0x00: /* SSHR / USHR */
10437     case 0x02: /* SSRA / USRA (accumulate) */
10438     case 0x04: /* SRSHR / URSHR (rounding) */
10439     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10440         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10441         break;
10442     case 0x0a: /* SHL / SLI */
10443         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10444         break;
10445     case 0x10: /* SHRN */
10446     case 0x11: /* RSHRN / SQRSHRUN */
10447         if (is_u) {
10448             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10449                                    opcode, rn, rd);
10450         } else {
10451             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10452         }
10453         break;
10454     case 0x12: /* SQSHRN / UQSHRN */
10455     case 0x13: /* SQRSHRN / UQRSHRN */
10456         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10457                                opcode, rn, rd);
10458         break;
10459     case 0x14: /* SSHLL / USHLL */
10460         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10461         break;
10462     case 0x1c: /* SCVTF / UCVTF */
10463         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10464                                      opcode, rn, rd);
10465         break;
10466     case 0xc: /* SQSHLU */
10467         if (!is_u) {
10468             unallocated_encoding(s);
10469             return;
10470         }
10471         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10472         break;
10473     case 0xe: /* SQSHL, UQSHL */
10474         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10475         break;
10476     case 0x1f: /* FCVTZS/ FCVTZU */
10477         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10478         return;
10479     default:
10480         unallocated_encoding(s);
10481         return;
10482     }
10483 }
10484 
10485 /* Generate code to do a "long" addition or subtraction, ie one done in
10486  * TCGv_i64 on vector lanes twice the width specified by size.
10487  */
10488 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10489                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10490 {
10491     static NeonGenTwo64OpFn * const fns[3][2] = {
10492         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10493         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10494         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10495     };
10496     NeonGenTwo64OpFn *genfn;
10497     assert(size < 3);
10498 
10499     genfn = fns[size][is_sub];
10500     genfn(tcg_res, tcg_op1, tcg_op2);
10501 }
10502 
10503 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10504                                 int opcode, int rd, int rn, int rm)
10505 {
10506     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10507     TCGv_i64 tcg_res[2];
10508     int pass, accop;
10509 
10510     tcg_res[0] = tcg_temp_new_i64();
10511     tcg_res[1] = tcg_temp_new_i64();
10512 
10513     /* Does this op do an adding accumulate, a subtracting accumulate,
10514      * or no accumulate at all?
10515      */
10516     switch (opcode) {
10517     case 5:
10518     case 8:
10519     case 9:
10520         accop = 1;
10521         break;
10522     case 10:
10523     case 11:
10524         accop = -1;
10525         break;
10526     default:
10527         accop = 0;
10528         break;
10529     }
10530 
10531     if (accop != 0) {
10532         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10533         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10534     }
10535 
10536     /* size == 2 means two 32x32->64 operations; this is worth special
10537      * casing because we can generally handle it inline.
10538      */
10539     if (size == 2) {
10540         for (pass = 0; pass < 2; pass++) {
10541             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10542             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10543             TCGv_i64 tcg_passres;
10544             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10545 
10546             int elt = pass + is_q * 2;
10547 
10548             read_vec_element(s, tcg_op1, rn, elt, memop);
10549             read_vec_element(s, tcg_op2, rm, elt, memop);
10550 
10551             if (accop == 0) {
10552                 tcg_passres = tcg_res[pass];
10553             } else {
10554                 tcg_passres = tcg_temp_new_i64();
10555             }
10556 
10557             switch (opcode) {
10558             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10559                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10560                 break;
10561             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10562                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10563                 break;
10564             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10565             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10566             {
10567                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10568                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10569 
10570                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10571                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10572                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10573                                     tcg_passres,
10574                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10575                 break;
10576             }
10577             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10578             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10579             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10580                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10581                 break;
10582             case 9: /* SQDMLAL, SQDMLAL2 */
10583             case 11: /* SQDMLSL, SQDMLSL2 */
10584             case 13: /* SQDMULL, SQDMULL2 */
10585                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10586                 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10587                                                   tcg_passres, tcg_passres);
10588                 break;
10589             default:
10590                 g_assert_not_reached();
10591             }
10592 
10593             if (opcode == 9 || opcode == 11) {
10594                 /* saturating accumulate ops */
10595                 if (accop < 0) {
10596                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10597                 }
10598                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10599                                                   tcg_res[pass], tcg_passres);
10600             } else if (accop > 0) {
10601                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10602             } else if (accop < 0) {
10603                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10604             }
10605         }
10606     } else {
10607         /* size 0 or 1, generally helper functions */
10608         for (pass = 0; pass < 2; pass++) {
10609             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10610             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10611             TCGv_i64 tcg_passres;
10612             int elt = pass + is_q * 2;
10613 
10614             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10615             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10616 
10617             if (accop == 0) {
10618                 tcg_passres = tcg_res[pass];
10619             } else {
10620                 tcg_passres = tcg_temp_new_i64();
10621             }
10622 
10623             switch (opcode) {
10624             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10625             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10626             {
10627                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10628                 static NeonGenWidenFn * const widenfns[2][2] = {
10629                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10630                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10631                 };
10632                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10633 
10634                 widenfn(tcg_op2_64, tcg_op2);
10635                 widenfn(tcg_passres, tcg_op1);
10636                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10637                               tcg_passres, tcg_op2_64);
10638                 break;
10639             }
10640             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10641             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10642                 if (size == 0) {
10643                     if (is_u) {
10644                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10645                     } else {
10646                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10647                     }
10648                 } else {
10649                     if (is_u) {
10650                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10651                     } else {
10652                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10653                     }
10654                 }
10655                 break;
10656             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10657             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10658             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10659                 if (size == 0) {
10660                     if (is_u) {
10661                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10662                     } else {
10663                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10664                     }
10665                 } else {
10666                     if (is_u) {
10667                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10668                     } else {
10669                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10670                     }
10671                 }
10672                 break;
10673             case 9: /* SQDMLAL, SQDMLAL2 */
10674             case 11: /* SQDMLSL, SQDMLSL2 */
10675             case 13: /* SQDMULL, SQDMULL2 */
10676                 assert(size == 1);
10677                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10678                 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10679                                                   tcg_passres, tcg_passres);
10680                 break;
10681             default:
10682                 g_assert_not_reached();
10683             }
10684 
10685             if (accop != 0) {
10686                 if (opcode == 9 || opcode == 11) {
10687                     /* saturating accumulate ops */
10688                     if (accop < 0) {
10689                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10690                     }
10691                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10692                                                       tcg_res[pass],
10693                                                       tcg_passres);
10694                 } else {
10695                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10696                                   tcg_res[pass], tcg_passres);
10697                 }
10698             }
10699         }
10700     }
10701 
10702     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10703     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10704 }
10705 
10706 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10707                             int opcode, int rd, int rn, int rm)
10708 {
10709     TCGv_i64 tcg_res[2];
10710     int part = is_q ? 2 : 0;
10711     int pass;
10712 
10713     for (pass = 0; pass < 2; pass++) {
10714         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10715         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10716         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10717         static NeonGenWidenFn * const widenfns[3][2] = {
10718             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10719             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10720             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10721         };
10722         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10723 
10724         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10725         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10726         widenfn(tcg_op2_wide, tcg_op2);
10727         tcg_res[pass] = tcg_temp_new_i64();
10728         gen_neon_addl(size, (opcode == 3),
10729                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10730     }
10731 
10732     for (pass = 0; pass < 2; pass++) {
10733         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10734     }
10735 }
10736 
10737 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10738 {
10739     tcg_gen_addi_i64(in, in, 1U << 31);
10740     tcg_gen_extrh_i64_i32(res, in);
10741 }
10742 
10743 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10744                                  int opcode, int rd, int rn, int rm)
10745 {
10746     TCGv_i32 tcg_res[2];
10747     int part = is_q ? 2 : 0;
10748     int pass;
10749 
10750     for (pass = 0; pass < 2; pass++) {
10751         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10752         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10753         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10754         static NeonGenNarrowFn * const narrowfns[3][2] = {
10755             { gen_helper_neon_narrow_high_u8,
10756               gen_helper_neon_narrow_round_high_u8 },
10757             { gen_helper_neon_narrow_high_u16,
10758               gen_helper_neon_narrow_round_high_u16 },
10759             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10760         };
10761         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10762 
10763         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10764         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10765 
10766         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10767 
10768         tcg_res[pass] = tcg_temp_new_i32();
10769         gennarrow(tcg_res[pass], tcg_wideres);
10770     }
10771 
10772     for (pass = 0; pass < 2; pass++) {
10773         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10774     }
10775     clear_vec_high(s, is_q, rd);
10776 }
10777 
10778 /* AdvSIMD three different
10779  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10780  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10781  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10782  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10783  */
10784 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10785 {
10786     /* Instructions in this group fall into three basic classes
10787      * (in each case with the operation working on each element in
10788      * the input vectors):
10789      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10790      *     128 bit input)
10791      * (2) wide 64 x 128 -> 128
10792      * (3) narrowing 128 x 128 -> 64
10793      * Here we do initial decode, catch unallocated cases and
10794      * dispatch to separate functions for each class.
10795      */
10796     int is_q = extract32(insn, 30, 1);
10797     int is_u = extract32(insn, 29, 1);
10798     int size = extract32(insn, 22, 2);
10799     int opcode = extract32(insn, 12, 4);
10800     int rm = extract32(insn, 16, 5);
10801     int rn = extract32(insn, 5, 5);
10802     int rd = extract32(insn, 0, 5);
10803 
10804     switch (opcode) {
10805     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10806     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10807         /* 64 x 128 -> 128 */
10808         if (size == 3) {
10809             unallocated_encoding(s);
10810             return;
10811         }
10812         if (!fp_access_check(s)) {
10813             return;
10814         }
10815         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10816         break;
10817     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10818     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10819         /* 128 x 128 -> 64 */
10820         if (size == 3) {
10821             unallocated_encoding(s);
10822             return;
10823         }
10824         if (!fp_access_check(s)) {
10825             return;
10826         }
10827         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10828         break;
10829     case 14: /* PMULL, PMULL2 */
10830         if (is_u) {
10831             unallocated_encoding(s);
10832             return;
10833         }
10834         switch (size) {
10835         case 0: /* PMULL.P8 */
10836             if (!fp_access_check(s)) {
10837                 return;
10838             }
10839             /* The Q field specifies lo/hi half input for this insn.  */
10840             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10841                              gen_helper_neon_pmull_h);
10842             break;
10843 
10844         case 3: /* PMULL.P64 */
10845             if (!dc_isar_feature(aa64_pmull, s)) {
10846                 unallocated_encoding(s);
10847                 return;
10848             }
10849             if (!fp_access_check(s)) {
10850                 return;
10851             }
10852             /* The Q field specifies lo/hi half input for this insn.  */
10853             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10854                              gen_helper_gvec_pmull_q);
10855             break;
10856 
10857         default:
10858             unallocated_encoding(s);
10859             break;
10860         }
10861         return;
10862     case 9: /* SQDMLAL, SQDMLAL2 */
10863     case 11: /* SQDMLSL, SQDMLSL2 */
10864     case 13: /* SQDMULL, SQDMULL2 */
10865         if (is_u || size == 0) {
10866             unallocated_encoding(s);
10867             return;
10868         }
10869         /* fall through */
10870     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10871     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10872     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10873     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10874     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10875     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10876     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10877         /* 64 x 64 -> 128 */
10878         if (size == 3) {
10879             unallocated_encoding(s);
10880             return;
10881         }
10882         if (!fp_access_check(s)) {
10883             return;
10884         }
10885 
10886         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10887         break;
10888     default:
10889         /* opcode 15 not allocated */
10890         unallocated_encoding(s);
10891         break;
10892     }
10893 }
10894 
10895 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10896 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10897 {
10898     int rd = extract32(insn, 0, 5);
10899     int rn = extract32(insn, 5, 5);
10900     int rm = extract32(insn, 16, 5);
10901     int size = extract32(insn, 22, 2);
10902     bool is_u = extract32(insn, 29, 1);
10903     bool is_q = extract32(insn, 30, 1);
10904 
10905     if (!fp_access_check(s)) {
10906         return;
10907     }
10908 
10909     switch (size + 4 * is_u) {
10910     case 0: /* AND */
10911         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10912         return;
10913     case 1: /* BIC */
10914         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10915         return;
10916     case 2: /* ORR */
10917         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10918         return;
10919     case 3: /* ORN */
10920         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10921         return;
10922     case 4: /* EOR */
10923         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10924         return;
10925 
10926     case 5: /* BSL bitwise select */
10927         gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
10928         return;
10929     case 6: /* BIT, bitwise insert if true */
10930         gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
10931         return;
10932     case 7: /* BIF, bitwise insert if false */
10933         gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
10934         return;
10935 
10936     default:
10937         g_assert_not_reached();
10938     }
10939 }
10940 
10941 /* Pairwise op subgroup of C3.6.16.
10942  *
10943  * This is called directly or via the handle_3same_float for float pairwise
10944  * operations where the opcode and size are calculated differently.
10945  */
10946 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
10947                                    int size, int rn, int rm, int rd)
10948 {
10949     TCGv_ptr fpst;
10950     int pass;
10951 
10952     /* Floating point operations need fpst */
10953     if (opcode >= 0x58) {
10954         fpst = fpstatus_ptr(FPST_FPCR);
10955     } else {
10956         fpst = NULL;
10957     }
10958 
10959     if (!fp_access_check(s)) {
10960         return;
10961     }
10962 
10963     /* These operations work on the concatenated rm:rn, with each pair of
10964      * adjacent elements being operated on to produce an element in the result.
10965      */
10966     if (size == 3) {
10967         TCGv_i64 tcg_res[2];
10968 
10969         for (pass = 0; pass < 2; pass++) {
10970             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10971             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10972             int passreg = (pass == 0) ? rn : rm;
10973 
10974             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
10975             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
10976             tcg_res[pass] = tcg_temp_new_i64();
10977 
10978             switch (opcode) {
10979             case 0x17: /* ADDP */
10980                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
10981                 break;
10982             case 0x58: /* FMAXNMP */
10983                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10984                 break;
10985             case 0x5a: /* FADDP */
10986                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10987                 break;
10988             case 0x5e: /* FMAXP */
10989                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10990                 break;
10991             case 0x78: /* FMINNMP */
10992                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10993                 break;
10994             case 0x7e: /* FMINP */
10995                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10996                 break;
10997             default:
10998                 g_assert_not_reached();
10999             }
11000         }
11001 
11002         for (pass = 0; pass < 2; pass++) {
11003             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11004         }
11005     } else {
11006         int maxpass = is_q ? 4 : 2;
11007         TCGv_i32 tcg_res[4];
11008 
11009         for (pass = 0; pass < maxpass; pass++) {
11010             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11011             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11012             NeonGenTwoOpFn *genfn = NULL;
11013             int passreg = pass < (maxpass / 2) ? rn : rm;
11014             int passelt = (is_q && (pass & 1)) ? 2 : 0;
11015 
11016             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
11017             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
11018             tcg_res[pass] = tcg_temp_new_i32();
11019 
11020             switch (opcode) {
11021             case 0x17: /* ADDP */
11022             {
11023                 static NeonGenTwoOpFn * const fns[3] = {
11024                     gen_helper_neon_padd_u8,
11025                     gen_helper_neon_padd_u16,
11026                     tcg_gen_add_i32,
11027                 };
11028                 genfn = fns[size];
11029                 break;
11030             }
11031             case 0x14: /* SMAXP, UMAXP */
11032             {
11033                 static NeonGenTwoOpFn * const fns[3][2] = {
11034                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
11035                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
11036                     { tcg_gen_smax_i32, tcg_gen_umax_i32 },
11037                 };
11038                 genfn = fns[size][u];
11039                 break;
11040             }
11041             case 0x15: /* SMINP, UMINP */
11042             {
11043                 static NeonGenTwoOpFn * const fns[3][2] = {
11044                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
11045                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
11046                     { tcg_gen_smin_i32, tcg_gen_umin_i32 },
11047                 };
11048                 genfn = fns[size][u];
11049                 break;
11050             }
11051             /* The FP operations are all on single floats (32 bit) */
11052             case 0x58: /* FMAXNMP */
11053                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11054                 break;
11055             case 0x5a: /* FADDP */
11056                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11057                 break;
11058             case 0x5e: /* FMAXP */
11059                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11060                 break;
11061             case 0x78: /* FMINNMP */
11062                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11063                 break;
11064             case 0x7e: /* FMINP */
11065                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11066                 break;
11067             default:
11068                 g_assert_not_reached();
11069             }
11070 
11071             /* FP ops called directly, otherwise call now */
11072             if (genfn) {
11073                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
11074             }
11075         }
11076 
11077         for (pass = 0; pass < maxpass; pass++) {
11078             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11079         }
11080         clear_vec_high(s, is_q, rd);
11081     }
11082 }
11083 
11084 /* Floating point op subgroup of C3.6.16. */
11085 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
11086 {
11087     /* For floating point ops, the U, size[1] and opcode bits
11088      * together indicate the operation. size[0] indicates single
11089      * or double.
11090      */
11091     int fpopcode = extract32(insn, 11, 5)
11092         | (extract32(insn, 23, 1) << 5)
11093         | (extract32(insn, 29, 1) << 6);
11094     int is_q = extract32(insn, 30, 1);
11095     int size = extract32(insn, 22, 1);
11096     int rm = extract32(insn, 16, 5);
11097     int rn = extract32(insn, 5, 5);
11098     int rd = extract32(insn, 0, 5);
11099 
11100     int datasize = is_q ? 128 : 64;
11101     int esize = 32 << size;
11102     int elements = datasize / esize;
11103 
11104     if (size == 1 && !is_q) {
11105         unallocated_encoding(s);
11106         return;
11107     }
11108 
11109     switch (fpopcode) {
11110     case 0x58: /* FMAXNMP */
11111     case 0x5a: /* FADDP */
11112     case 0x5e: /* FMAXP */
11113     case 0x78: /* FMINNMP */
11114     case 0x7e: /* FMINP */
11115         if (size && !is_q) {
11116             unallocated_encoding(s);
11117             return;
11118         }
11119         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11120                                rn, rm, rd);
11121         return;
11122     case 0x1b: /* FMULX */
11123     case 0x1f: /* FRECPS */
11124     case 0x3f: /* FRSQRTS */
11125     case 0x5d: /* FACGE */
11126     case 0x7d: /* FACGT */
11127     case 0x19: /* FMLA */
11128     case 0x39: /* FMLS */
11129     case 0x18: /* FMAXNM */
11130     case 0x1a: /* FADD */
11131     case 0x1c: /* FCMEQ */
11132     case 0x1e: /* FMAX */
11133     case 0x38: /* FMINNM */
11134     case 0x3a: /* FSUB */
11135     case 0x3e: /* FMIN */
11136     case 0x5b: /* FMUL */
11137     case 0x5c: /* FCMGE */
11138     case 0x5f: /* FDIV */
11139     case 0x7a: /* FABD */
11140     case 0x7c: /* FCMGT */
11141         if (!fp_access_check(s)) {
11142             return;
11143         }
11144         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11145         return;
11146 
11147     case 0x1d: /* FMLAL  */
11148     case 0x3d: /* FMLSL  */
11149     case 0x59: /* FMLAL2 */
11150     case 0x79: /* FMLSL2 */
11151         if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11152             unallocated_encoding(s);
11153             return;
11154         }
11155         if (fp_access_check(s)) {
11156             int is_s = extract32(insn, 23, 1);
11157             int is_2 = extract32(insn, 29, 1);
11158             int data = (is_2 << 1) | is_s;
11159             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11160                                vec_full_reg_offset(s, rn),
11161                                vec_full_reg_offset(s, rm), cpu_env,
11162                                is_q ? 16 : 8, vec_full_reg_size(s),
11163                                data, gen_helper_gvec_fmlal_a64);
11164         }
11165         return;
11166 
11167     default:
11168         unallocated_encoding(s);
11169         return;
11170     }
11171 }
11172 
11173 /* Integer op subgroup of C3.6.16. */
11174 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11175 {
11176     int is_q = extract32(insn, 30, 1);
11177     int u = extract32(insn, 29, 1);
11178     int size = extract32(insn, 22, 2);
11179     int opcode = extract32(insn, 11, 5);
11180     int rm = extract32(insn, 16, 5);
11181     int rn = extract32(insn, 5, 5);
11182     int rd = extract32(insn, 0, 5);
11183     int pass;
11184     TCGCond cond;
11185 
11186     switch (opcode) {
11187     case 0x13: /* MUL, PMUL */
11188         if (u && size != 0) {
11189             unallocated_encoding(s);
11190             return;
11191         }
11192         /* fall through */
11193     case 0x0: /* SHADD, UHADD */
11194     case 0x2: /* SRHADD, URHADD */
11195     case 0x4: /* SHSUB, UHSUB */
11196     case 0xc: /* SMAX, UMAX */
11197     case 0xd: /* SMIN, UMIN */
11198     case 0xe: /* SABD, UABD */
11199     case 0xf: /* SABA, UABA */
11200     case 0x12: /* MLA, MLS */
11201         if (size == 3) {
11202             unallocated_encoding(s);
11203             return;
11204         }
11205         break;
11206     case 0x16: /* SQDMULH, SQRDMULH */
11207         if (size == 0 || size == 3) {
11208             unallocated_encoding(s);
11209             return;
11210         }
11211         break;
11212     default:
11213         if (size == 3 && !is_q) {
11214             unallocated_encoding(s);
11215             return;
11216         }
11217         break;
11218     }
11219 
11220     if (!fp_access_check(s)) {
11221         return;
11222     }
11223 
11224     switch (opcode) {
11225     case 0x01: /* SQADD, UQADD */
11226         if (u) {
11227             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11228         } else {
11229             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11230         }
11231         return;
11232     case 0x05: /* SQSUB, UQSUB */
11233         if (u) {
11234             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11235         } else {
11236             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11237         }
11238         return;
11239     case 0x08: /* SSHL, USHL */
11240         if (u) {
11241             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11242         } else {
11243             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11244         }
11245         return;
11246     case 0x0c: /* SMAX, UMAX */
11247         if (u) {
11248             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11249         } else {
11250             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11251         }
11252         return;
11253     case 0x0d: /* SMIN, UMIN */
11254         if (u) {
11255             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11256         } else {
11257             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11258         }
11259         return;
11260     case 0xe: /* SABD, UABD */
11261         if (u) {
11262             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11263         } else {
11264             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11265         }
11266         return;
11267     case 0xf: /* SABA, UABA */
11268         if (u) {
11269             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11270         } else {
11271             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11272         }
11273         return;
11274     case 0x10: /* ADD, SUB */
11275         if (u) {
11276             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11277         } else {
11278             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11279         }
11280         return;
11281     case 0x13: /* MUL, PMUL */
11282         if (!u) { /* MUL */
11283             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11284         } else {  /* PMUL */
11285             gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11286         }
11287         return;
11288     case 0x12: /* MLA, MLS */
11289         if (u) {
11290             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11291         } else {
11292             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11293         }
11294         return;
11295     case 0x16: /* SQDMULH, SQRDMULH */
11296         {
11297             static gen_helper_gvec_3_ptr * const fns[2][2] = {
11298                 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11299                 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11300             };
11301             gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11302         }
11303         return;
11304     case 0x11:
11305         if (!u) { /* CMTST */
11306             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11307             return;
11308         }
11309         /* else CMEQ */
11310         cond = TCG_COND_EQ;
11311         goto do_gvec_cmp;
11312     case 0x06: /* CMGT, CMHI */
11313         cond = u ? TCG_COND_GTU : TCG_COND_GT;
11314         goto do_gvec_cmp;
11315     case 0x07: /* CMGE, CMHS */
11316         cond = u ? TCG_COND_GEU : TCG_COND_GE;
11317     do_gvec_cmp:
11318         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11319                          vec_full_reg_offset(s, rn),
11320                          vec_full_reg_offset(s, rm),
11321                          is_q ? 16 : 8, vec_full_reg_size(s));
11322         return;
11323     }
11324 
11325     if (size == 3) {
11326         assert(is_q);
11327         for (pass = 0; pass < 2; pass++) {
11328             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11329             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11330             TCGv_i64 tcg_res = tcg_temp_new_i64();
11331 
11332             read_vec_element(s, tcg_op1, rn, pass, MO_64);
11333             read_vec_element(s, tcg_op2, rm, pass, MO_64);
11334 
11335             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11336 
11337             write_vec_element(s, tcg_res, rd, pass, MO_64);
11338         }
11339     } else {
11340         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11341             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11342             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11343             TCGv_i32 tcg_res = tcg_temp_new_i32();
11344             NeonGenTwoOpFn *genfn = NULL;
11345             NeonGenTwoOpEnvFn *genenvfn = NULL;
11346 
11347             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11348             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11349 
11350             switch (opcode) {
11351             case 0x0: /* SHADD, UHADD */
11352             {
11353                 static NeonGenTwoOpFn * const fns[3][2] = {
11354                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11355                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11356                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11357                 };
11358                 genfn = fns[size][u];
11359                 break;
11360             }
11361             case 0x2: /* SRHADD, URHADD */
11362             {
11363                 static NeonGenTwoOpFn * const fns[3][2] = {
11364                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11365                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11366                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11367                 };
11368                 genfn = fns[size][u];
11369                 break;
11370             }
11371             case 0x4: /* SHSUB, UHSUB */
11372             {
11373                 static NeonGenTwoOpFn * const fns[3][2] = {
11374                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11375                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11376                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11377                 };
11378                 genfn = fns[size][u];
11379                 break;
11380             }
11381             case 0x9: /* SQSHL, UQSHL */
11382             {
11383                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11384                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11385                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11386                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11387                 };
11388                 genenvfn = fns[size][u];
11389                 break;
11390             }
11391             case 0xa: /* SRSHL, URSHL */
11392             {
11393                 static NeonGenTwoOpFn * const fns[3][2] = {
11394                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11395                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11396                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11397                 };
11398                 genfn = fns[size][u];
11399                 break;
11400             }
11401             case 0xb: /* SQRSHL, UQRSHL */
11402             {
11403                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11404                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11405                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11406                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11407                 };
11408                 genenvfn = fns[size][u];
11409                 break;
11410             }
11411             default:
11412                 g_assert_not_reached();
11413             }
11414 
11415             if (genenvfn) {
11416                 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11417             } else {
11418                 genfn(tcg_res, tcg_op1, tcg_op2);
11419             }
11420 
11421             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11422         }
11423     }
11424     clear_vec_high(s, is_q, rd);
11425 }
11426 
11427 /* AdvSIMD three same
11428  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
11429  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11430  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
11431  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11432  */
11433 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11434 {
11435     int opcode = extract32(insn, 11, 5);
11436 
11437     switch (opcode) {
11438     case 0x3: /* logic ops */
11439         disas_simd_3same_logic(s, insn);
11440         break;
11441     case 0x17: /* ADDP */
11442     case 0x14: /* SMAXP, UMAXP */
11443     case 0x15: /* SMINP, UMINP */
11444     {
11445         /* Pairwise operations */
11446         int is_q = extract32(insn, 30, 1);
11447         int u = extract32(insn, 29, 1);
11448         int size = extract32(insn, 22, 2);
11449         int rm = extract32(insn, 16, 5);
11450         int rn = extract32(insn, 5, 5);
11451         int rd = extract32(insn, 0, 5);
11452         if (opcode == 0x17) {
11453             if (u || (size == 3 && !is_q)) {
11454                 unallocated_encoding(s);
11455                 return;
11456             }
11457         } else {
11458             if (size == 3) {
11459                 unallocated_encoding(s);
11460                 return;
11461             }
11462         }
11463         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11464         break;
11465     }
11466     case 0x18 ... 0x31:
11467         /* floating point ops, sz[1] and U are part of opcode */
11468         disas_simd_3same_float(s, insn);
11469         break;
11470     default:
11471         disas_simd_3same_int(s, insn);
11472         break;
11473     }
11474 }
11475 
11476 /*
11477  * Advanced SIMD three same (ARMv8.2 FP16 variants)
11478  *
11479  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
11480  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11481  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
11482  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11483  *
11484  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11485  * (register), FACGE, FABD, FCMGT (register) and FACGT.
11486  *
11487  */
11488 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11489 {
11490     int opcode = extract32(insn, 11, 3);
11491     int u = extract32(insn, 29, 1);
11492     int a = extract32(insn, 23, 1);
11493     int is_q = extract32(insn, 30, 1);
11494     int rm = extract32(insn, 16, 5);
11495     int rn = extract32(insn, 5, 5);
11496     int rd = extract32(insn, 0, 5);
11497     /*
11498      * For these floating point ops, the U, a and opcode bits
11499      * together indicate the operation.
11500      */
11501     int fpopcode = opcode | (a << 3) | (u << 4);
11502     int datasize = is_q ? 128 : 64;
11503     int elements = datasize / 16;
11504     bool pairwise;
11505     TCGv_ptr fpst;
11506     int pass;
11507 
11508     switch (fpopcode) {
11509     case 0x0: /* FMAXNM */
11510     case 0x1: /* FMLA */
11511     case 0x2: /* FADD */
11512     case 0x3: /* FMULX */
11513     case 0x4: /* FCMEQ */
11514     case 0x6: /* FMAX */
11515     case 0x7: /* FRECPS */
11516     case 0x8: /* FMINNM */
11517     case 0x9: /* FMLS */
11518     case 0xa: /* FSUB */
11519     case 0xe: /* FMIN */
11520     case 0xf: /* FRSQRTS */
11521     case 0x13: /* FMUL */
11522     case 0x14: /* FCMGE */
11523     case 0x15: /* FACGE */
11524     case 0x17: /* FDIV */
11525     case 0x1a: /* FABD */
11526     case 0x1c: /* FCMGT */
11527     case 0x1d: /* FACGT */
11528         pairwise = false;
11529         break;
11530     case 0x10: /* FMAXNMP */
11531     case 0x12: /* FADDP */
11532     case 0x16: /* FMAXP */
11533     case 0x18: /* FMINNMP */
11534     case 0x1e: /* FMINP */
11535         pairwise = true;
11536         break;
11537     default:
11538         unallocated_encoding(s);
11539         return;
11540     }
11541 
11542     if (!dc_isar_feature(aa64_fp16, s)) {
11543         unallocated_encoding(s);
11544         return;
11545     }
11546 
11547     if (!fp_access_check(s)) {
11548         return;
11549     }
11550 
11551     fpst = fpstatus_ptr(FPST_FPCR_F16);
11552 
11553     if (pairwise) {
11554         int maxpass = is_q ? 8 : 4;
11555         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11556         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11557         TCGv_i32 tcg_res[8];
11558 
11559         for (pass = 0; pass < maxpass; pass++) {
11560             int passreg = pass < (maxpass / 2) ? rn : rm;
11561             int passelt = (pass << 1) & (maxpass - 1);
11562 
11563             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11564             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11565             tcg_res[pass] = tcg_temp_new_i32();
11566 
11567             switch (fpopcode) {
11568             case 0x10: /* FMAXNMP */
11569                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11570                                            fpst);
11571                 break;
11572             case 0x12: /* FADDP */
11573                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11574                 break;
11575             case 0x16: /* FMAXP */
11576                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11577                 break;
11578             case 0x18: /* FMINNMP */
11579                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11580                                            fpst);
11581                 break;
11582             case 0x1e: /* FMINP */
11583                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11584                 break;
11585             default:
11586                 g_assert_not_reached();
11587             }
11588         }
11589 
11590         for (pass = 0; pass < maxpass; pass++) {
11591             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11592         }
11593     } else {
11594         for (pass = 0; pass < elements; pass++) {
11595             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11596             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11597             TCGv_i32 tcg_res = tcg_temp_new_i32();
11598 
11599             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11600             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11601 
11602             switch (fpopcode) {
11603             case 0x0: /* FMAXNM */
11604                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11605                 break;
11606             case 0x1: /* FMLA */
11607                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11608                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11609                                            fpst);
11610                 break;
11611             case 0x2: /* FADD */
11612                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11613                 break;
11614             case 0x3: /* FMULX */
11615                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11616                 break;
11617             case 0x4: /* FCMEQ */
11618                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11619                 break;
11620             case 0x6: /* FMAX */
11621                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11622                 break;
11623             case 0x7: /* FRECPS */
11624                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11625                 break;
11626             case 0x8: /* FMINNM */
11627                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11628                 break;
11629             case 0x9: /* FMLS */
11630                 /* As usual for ARM, separate negation for fused multiply-add */
11631                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11632                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11633                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11634                                            fpst);
11635                 break;
11636             case 0xa: /* FSUB */
11637                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11638                 break;
11639             case 0xe: /* FMIN */
11640                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11641                 break;
11642             case 0xf: /* FRSQRTS */
11643                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11644                 break;
11645             case 0x13: /* FMUL */
11646                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11647                 break;
11648             case 0x14: /* FCMGE */
11649                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11650                 break;
11651             case 0x15: /* FACGE */
11652                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11653                 break;
11654             case 0x17: /* FDIV */
11655                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11656                 break;
11657             case 0x1a: /* FABD */
11658                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11659                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11660                 break;
11661             case 0x1c: /* FCMGT */
11662                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11663                 break;
11664             case 0x1d: /* FACGT */
11665                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11666                 break;
11667             default:
11668                 g_assert_not_reached();
11669             }
11670 
11671             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11672         }
11673     }
11674 
11675     clear_vec_high(s, is_q, rd);
11676 }
11677 
11678 /* AdvSIMD three same extra
11679  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
11680  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11681  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
11682  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11683  */
11684 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11685 {
11686     int rd = extract32(insn, 0, 5);
11687     int rn = extract32(insn, 5, 5);
11688     int opcode = extract32(insn, 11, 4);
11689     int rm = extract32(insn, 16, 5);
11690     int size = extract32(insn, 22, 2);
11691     bool u = extract32(insn, 29, 1);
11692     bool is_q = extract32(insn, 30, 1);
11693     bool feature;
11694     int rot;
11695 
11696     switch (u * 16 + opcode) {
11697     case 0x10: /* SQRDMLAH (vector) */
11698     case 0x11: /* SQRDMLSH (vector) */
11699         if (size != 1 && size != 2) {
11700             unallocated_encoding(s);
11701             return;
11702         }
11703         feature = dc_isar_feature(aa64_rdm, s);
11704         break;
11705     case 0x02: /* SDOT (vector) */
11706     case 0x12: /* UDOT (vector) */
11707         if (size != MO_32) {
11708             unallocated_encoding(s);
11709             return;
11710         }
11711         feature = dc_isar_feature(aa64_dp, s);
11712         break;
11713     case 0x03: /* USDOT */
11714         if (size != MO_32) {
11715             unallocated_encoding(s);
11716             return;
11717         }
11718         feature = dc_isar_feature(aa64_i8mm, s);
11719         break;
11720     case 0x04: /* SMMLA */
11721     case 0x14: /* UMMLA */
11722     case 0x05: /* USMMLA */
11723         if (!is_q || size != MO_32) {
11724             unallocated_encoding(s);
11725             return;
11726         }
11727         feature = dc_isar_feature(aa64_i8mm, s);
11728         break;
11729     case 0x18: /* FCMLA, #0 */
11730     case 0x19: /* FCMLA, #90 */
11731     case 0x1a: /* FCMLA, #180 */
11732     case 0x1b: /* FCMLA, #270 */
11733     case 0x1c: /* FCADD, #90 */
11734     case 0x1e: /* FCADD, #270 */
11735         if (size == 0
11736             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11737             || (size == 3 && !is_q)) {
11738             unallocated_encoding(s);
11739             return;
11740         }
11741         feature = dc_isar_feature(aa64_fcma, s);
11742         break;
11743     case 0x1d: /* BFMMLA */
11744         if (size != MO_16 || !is_q) {
11745             unallocated_encoding(s);
11746             return;
11747         }
11748         feature = dc_isar_feature(aa64_bf16, s);
11749         break;
11750     case 0x1f:
11751         switch (size) {
11752         case 1: /* BFDOT */
11753         case 3: /* BFMLAL{B,T} */
11754             feature = dc_isar_feature(aa64_bf16, s);
11755             break;
11756         default:
11757             unallocated_encoding(s);
11758             return;
11759         }
11760         break;
11761     default:
11762         unallocated_encoding(s);
11763         return;
11764     }
11765     if (!feature) {
11766         unallocated_encoding(s);
11767         return;
11768     }
11769     if (!fp_access_check(s)) {
11770         return;
11771     }
11772 
11773     switch (opcode) {
11774     case 0x0: /* SQRDMLAH (vector) */
11775         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11776         return;
11777 
11778     case 0x1: /* SQRDMLSH (vector) */
11779         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11780         return;
11781 
11782     case 0x2: /* SDOT / UDOT */
11783         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11784                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11785         return;
11786 
11787     case 0x3: /* USDOT */
11788         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11789         return;
11790 
11791     case 0x04: /* SMMLA, UMMLA */
11792         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11793                          u ? gen_helper_gvec_ummla_b
11794                          : gen_helper_gvec_smmla_b);
11795         return;
11796     case 0x05: /* USMMLA */
11797         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11798         return;
11799 
11800     case 0x8: /* FCMLA, #0 */
11801     case 0x9: /* FCMLA, #90 */
11802     case 0xa: /* FCMLA, #180 */
11803     case 0xb: /* FCMLA, #270 */
11804         rot = extract32(opcode, 0, 2);
11805         switch (size) {
11806         case 1:
11807             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11808                               gen_helper_gvec_fcmlah);
11809             break;
11810         case 2:
11811             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11812                               gen_helper_gvec_fcmlas);
11813             break;
11814         case 3:
11815             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11816                               gen_helper_gvec_fcmlad);
11817             break;
11818         default:
11819             g_assert_not_reached();
11820         }
11821         return;
11822 
11823     case 0xc: /* FCADD, #90 */
11824     case 0xe: /* FCADD, #270 */
11825         rot = extract32(opcode, 1, 1);
11826         switch (size) {
11827         case 1:
11828             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11829                               gen_helper_gvec_fcaddh);
11830             break;
11831         case 2:
11832             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11833                               gen_helper_gvec_fcadds);
11834             break;
11835         case 3:
11836             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11837                               gen_helper_gvec_fcaddd);
11838             break;
11839         default:
11840             g_assert_not_reached();
11841         }
11842         return;
11843 
11844     case 0xd: /* BFMMLA */
11845         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11846         return;
11847     case 0xf:
11848         switch (size) {
11849         case 1: /* BFDOT */
11850             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11851             break;
11852         case 3: /* BFMLAL{B,T} */
11853             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11854                               gen_helper_gvec_bfmlal);
11855             break;
11856         default:
11857             g_assert_not_reached();
11858         }
11859         return;
11860 
11861     default:
11862         g_assert_not_reached();
11863     }
11864 }
11865 
11866 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11867                                   int size, int rn, int rd)
11868 {
11869     /* Handle 2-reg-misc ops which are widening (so each size element
11870      * in the source becomes a 2*size element in the destination.
11871      * The only instruction like this is FCVTL.
11872      */
11873     int pass;
11874 
11875     if (size == 3) {
11876         /* 32 -> 64 bit fp conversion */
11877         TCGv_i64 tcg_res[2];
11878         int srcelt = is_q ? 2 : 0;
11879 
11880         for (pass = 0; pass < 2; pass++) {
11881             TCGv_i32 tcg_op = tcg_temp_new_i32();
11882             tcg_res[pass] = tcg_temp_new_i64();
11883 
11884             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11885             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
11886         }
11887         for (pass = 0; pass < 2; pass++) {
11888             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11889         }
11890     } else {
11891         /* 16 -> 32 bit fp conversion */
11892         int srcelt = is_q ? 4 : 0;
11893         TCGv_i32 tcg_res[4];
11894         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11895         TCGv_i32 ahp = get_ahp_flag();
11896 
11897         for (pass = 0; pass < 4; pass++) {
11898             tcg_res[pass] = tcg_temp_new_i32();
11899 
11900             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11901             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11902                                            fpst, ahp);
11903         }
11904         for (pass = 0; pass < 4; pass++) {
11905             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11906         }
11907     }
11908 }
11909 
11910 static void handle_rev(DisasContext *s, int opcode, bool u,
11911                        bool is_q, int size, int rn, int rd)
11912 {
11913     int op = (opcode << 1) | u;
11914     int opsz = op + size;
11915     int grp_size = 3 - opsz;
11916     int dsize = is_q ? 128 : 64;
11917     int i;
11918 
11919     if (opsz >= 3) {
11920         unallocated_encoding(s);
11921         return;
11922     }
11923 
11924     if (!fp_access_check(s)) {
11925         return;
11926     }
11927 
11928     if (size == 0) {
11929         /* Special case bytes, use bswap op on each group of elements */
11930         int groups = dsize / (8 << grp_size);
11931 
11932         for (i = 0; i < groups; i++) {
11933             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11934 
11935             read_vec_element(s, tcg_tmp, rn, i, grp_size);
11936             switch (grp_size) {
11937             case MO_16:
11938                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11939                 break;
11940             case MO_32:
11941                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11942                 break;
11943             case MO_64:
11944                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11945                 break;
11946             default:
11947                 g_assert_not_reached();
11948             }
11949             write_vec_element(s, tcg_tmp, rd, i, grp_size);
11950         }
11951         clear_vec_high(s, is_q, rd);
11952     } else {
11953         int revmask = (1 << grp_size) - 1;
11954         int esize = 8 << size;
11955         int elements = dsize / esize;
11956         TCGv_i64 tcg_rn = tcg_temp_new_i64();
11957         TCGv_i64 tcg_rd[2];
11958 
11959         for (i = 0; i < 2; i++) {
11960             tcg_rd[i] = tcg_temp_new_i64();
11961             tcg_gen_movi_i64(tcg_rd[i], 0);
11962         }
11963 
11964         for (i = 0; i < elements; i++) {
11965             int e_rev = (i & 0xf) ^ revmask;
11966             int w = (e_rev * esize) / 64;
11967             int o = (e_rev * esize) % 64;
11968 
11969             read_vec_element(s, tcg_rn, rn, i, size);
11970             tcg_gen_deposit_i64(tcg_rd[w], tcg_rd[w], tcg_rn, o, esize);
11971         }
11972 
11973         for (i = 0; i < 2; i++) {
11974             write_vec_element(s, tcg_rd[i], rd, i, MO_64);
11975         }
11976         clear_vec_high(s, true, rd);
11977     }
11978 }
11979 
11980 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
11981                                   bool is_q, int size, int rn, int rd)
11982 {
11983     /* Implement the pairwise operations from 2-misc:
11984      * SADDLP, UADDLP, SADALP, UADALP.
11985      * These all add pairs of elements in the input to produce a
11986      * double-width result element in the output (possibly accumulating).
11987      */
11988     bool accum = (opcode == 0x6);
11989     int maxpass = is_q ? 2 : 1;
11990     int pass;
11991     TCGv_i64 tcg_res[2];
11992 
11993     if (size == 2) {
11994         /* 32 + 32 -> 64 op */
11995         MemOp memop = size + (u ? 0 : MO_SIGN);
11996 
11997         for (pass = 0; pass < maxpass; pass++) {
11998             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11999             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
12000 
12001             tcg_res[pass] = tcg_temp_new_i64();
12002 
12003             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
12004             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
12005             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
12006             if (accum) {
12007                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
12008                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12009             }
12010         }
12011     } else {
12012         for (pass = 0; pass < maxpass; pass++) {
12013             TCGv_i64 tcg_op = tcg_temp_new_i64();
12014             NeonGenOne64OpFn *genfn;
12015             static NeonGenOne64OpFn * const fns[2][2] = {
12016                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
12017                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
12018             };
12019 
12020             genfn = fns[size][u];
12021 
12022             tcg_res[pass] = tcg_temp_new_i64();
12023 
12024             read_vec_element(s, tcg_op, rn, pass, MO_64);
12025             genfn(tcg_res[pass], tcg_op);
12026 
12027             if (accum) {
12028                 read_vec_element(s, tcg_op, rd, pass, MO_64);
12029                 if (size == 0) {
12030                     gen_helper_neon_addl_u16(tcg_res[pass],
12031                                              tcg_res[pass], tcg_op);
12032                 } else {
12033                     gen_helper_neon_addl_u32(tcg_res[pass],
12034                                              tcg_res[pass], tcg_op);
12035                 }
12036             }
12037         }
12038     }
12039     if (!is_q) {
12040         tcg_res[1] = tcg_constant_i64(0);
12041     }
12042     for (pass = 0; pass < 2; pass++) {
12043         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12044     }
12045 }
12046 
12047 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
12048 {
12049     /* Implement SHLL and SHLL2 */
12050     int pass;
12051     int part = is_q ? 2 : 0;
12052     TCGv_i64 tcg_res[2];
12053 
12054     for (pass = 0; pass < 2; pass++) {
12055         static NeonGenWidenFn * const widenfns[3] = {
12056             gen_helper_neon_widen_u8,
12057             gen_helper_neon_widen_u16,
12058             tcg_gen_extu_i32_i64,
12059         };
12060         NeonGenWidenFn *widenfn = widenfns[size];
12061         TCGv_i32 tcg_op = tcg_temp_new_i32();
12062 
12063         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
12064         tcg_res[pass] = tcg_temp_new_i64();
12065         widenfn(tcg_res[pass], tcg_op);
12066         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
12067     }
12068 
12069     for (pass = 0; pass < 2; pass++) {
12070         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12071     }
12072 }
12073 
12074 /* AdvSIMD two reg misc
12075  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
12076  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12077  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12078  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12079  */
12080 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
12081 {
12082     int size = extract32(insn, 22, 2);
12083     int opcode = extract32(insn, 12, 5);
12084     bool u = extract32(insn, 29, 1);
12085     bool is_q = extract32(insn, 30, 1);
12086     int rn = extract32(insn, 5, 5);
12087     int rd = extract32(insn, 0, 5);
12088     bool need_fpstatus = false;
12089     int rmode = -1;
12090     TCGv_i32 tcg_rmode;
12091     TCGv_ptr tcg_fpstatus;
12092 
12093     switch (opcode) {
12094     case 0x0: /* REV64, REV32 */
12095     case 0x1: /* REV16 */
12096         handle_rev(s, opcode, u, is_q, size, rn, rd);
12097         return;
12098     case 0x5: /* CNT, NOT, RBIT */
12099         if (u && size == 0) {
12100             /* NOT */
12101             break;
12102         } else if (u && size == 1) {
12103             /* RBIT */
12104             break;
12105         } else if (!u && size == 0) {
12106             /* CNT */
12107             break;
12108         }
12109         unallocated_encoding(s);
12110         return;
12111     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12112     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12113         if (size == 3) {
12114             unallocated_encoding(s);
12115             return;
12116         }
12117         if (!fp_access_check(s)) {
12118             return;
12119         }
12120 
12121         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
12122         return;
12123     case 0x4: /* CLS, CLZ */
12124         if (size == 3) {
12125             unallocated_encoding(s);
12126             return;
12127         }
12128         break;
12129     case 0x2: /* SADDLP, UADDLP */
12130     case 0x6: /* SADALP, UADALP */
12131         if (size == 3) {
12132             unallocated_encoding(s);
12133             return;
12134         }
12135         if (!fp_access_check(s)) {
12136             return;
12137         }
12138         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
12139         return;
12140     case 0x13: /* SHLL, SHLL2 */
12141         if (u == 0 || size == 3) {
12142             unallocated_encoding(s);
12143             return;
12144         }
12145         if (!fp_access_check(s)) {
12146             return;
12147         }
12148         handle_shll(s, is_q, size, rn, rd);
12149         return;
12150     case 0xa: /* CMLT */
12151         if (u == 1) {
12152             unallocated_encoding(s);
12153             return;
12154         }
12155         /* fall through */
12156     case 0x8: /* CMGT, CMGE */
12157     case 0x9: /* CMEQ, CMLE */
12158     case 0xb: /* ABS, NEG */
12159         if (size == 3 && !is_q) {
12160             unallocated_encoding(s);
12161             return;
12162         }
12163         break;
12164     case 0x3: /* SUQADD, USQADD */
12165         if (size == 3 && !is_q) {
12166             unallocated_encoding(s);
12167             return;
12168         }
12169         if (!fp_access_check(s)) {
12170             return;
12171         }
12172         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12173         return;
12174     case 0x7: /* SQABS, SQNEG */
12175         if (size == 3 && !is_q) {
12176             unallocated_encoding(s);
12177             return;
12178         }
12179         break;
12180     case 0xc ... 0xf:
12181     case 0x16 ... 0x1f:
12182     {
12183         /* Floating point: U, size[1] and opcode indicate operation;
12184          * size[0] indicates single or double precision.
12185          */
12186         int is_double = extract32(size, 0, 1);
12187         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12188         size = is_double ? 3 : 2;
12189         switch (opcode) {
12190         case 0x2f: /* FABS */
12191         case 0x6f: /* FNEG */
12192             if (size == 3 && !is_q) {
12193                 unallocated_encoding(s);
12194                 return;
12195             }
12196             break;
12197         case 0x1d: /* SCVTF */
12198         case 0x5d: /* UCVTF */
12199         {
12200             bool is_signed = (opcode == 0x1d) ? true : false;
12201             int elements = is_double ? 2 : is_q ? 4 : 2;
12202             if (is_double && !is_q) {
12203                 unallocated_encoding(s);
12204                 return;
12205             }
12206             if (!fp_access_check(s)) {
12207                 return;
12208             }
12209             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12210             return;
12211         }
12212         case 0x2c: /* FCMGT (zero) */
12213         case 0x2d: /* FCMEQ (zero) */
12214         case 0x2e: /* FCMLT (zero) */
12215         case 0x6c: /* FCMGE (zero) */
12216         case 0x6d: /* FCMLE (zero) */
12217             if (size == 3 && !is_q) {
12218                 unallocated_encoding(s);
12219                 return;
12220             }
12221             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12222             return;
12223         case 0x7f: /* FSQRT */
12224             if (size == 3 && !is_q) {
12225                 unallocated_encoding(s);
12226                 return;
12227             }
12228             break;
12229         case 0x1a: /* FCVTNS */
12230         case 0x1b: /* FCVTMS */
12231         case 0x3a: /* FCVTPS */
12232         case 0x3b: /* FCVTZS */
12233         case 0x5a: /* FCVTNU */
12234         case 0x5b: /* FCVTMU */
12235         case 0x7a: /* FCVTPU */
12236         case 0x7b: /* FCVTZU */
12237             need_fpstatus = true;
12238             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12239             if (size == 3 && !is_q) {
12240                 unallocated_encoding(s);
12241                 return;
12242             }
12243             break;
12244         case 0x5c: /* FCVTAU */
12245         case 0x1c: /* FCVTAS */
12246             need_fpstatus = true;
12247             rmode = FPROUNDING_TIEAWAY;
12248             if (size == 3 && !is_q) {
12249                 unallocated_encoding(s);
12250                 return;
12251             }
12252             break;
12253         case 0x3c: /* URECPE */
12254             if (size == 3) {
12255                 unallocated_encoding(s);
12256                 return;
12257             }
12258             /* fall through */
12259         case 0x3d: /* FRECPE */
12260         case 0x7d: /* FRSQRTE */
12261             if (size == 3 && !is_q) {
12262                 unallocated_encoding(s);
12263                 return;
12264             }
12265             if (!fp_access_check(s)) {
12266                 return;
12267             }
12268             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12269             return;
12270         case 0x56: /* FCVTXN, FCVTXN2 */
12271             if (size == 2) {
12272                 unallocated_encoding(s);
12273                 return;
12274             }
12275             /* fall through */
12276         case 0x16: /* FCVTN, FCVTN2 */
12277             /* handle_2misc_narrow does a 2*size -> size operation, but these
12278              * instructions encode the source size rather than dest size.
12279              */
12280             if (!fp_access_check(s)) {
12281                 return;
12282             }
12283             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12284             return;
12285         case 0x36: /* BFCVTN, BFCVTN2 */
12286             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
12287                 unallocated_encoding(s);
12288                 return;
12289             }
12290             if (!fp_access_check(s)) {
12291                 return;
12292             }
12293             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12294             return;
12295         case 0x17: /* FCVTL, FCVTL2 */
12296             if (!fp_access_check(s)) {
12297                 return;
12298             }
12299             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12300             return;
12301         case 0x18: /* FRINTN */
12302         case 0x19: /* FRINTM */
12303         case 0x38: /* FRINTP */
12304         case 0x39: /* FRINTZ */
12305             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12306             /* fall through */
12307         case 0x59: /* FRINTX */
12308         case 0x79: /* FRINTI */
12309             need_fpstatus = true;
12310             if (size == 3 && !is_q) {
12311                 unallocated_encoding(s);
12312                 return;
12313             }
12314             break;
12315         case 0x58: /* FRINTA */
12316             rmode = FPROUNDING_TIEAWAY;
12317             need_fpstatus = true;
12318             if (size == 3 && !is_q) {
12319                 unallocated_encoding(s);
12320                 return;
12321             }
12322             break;
12323         case 0x7c: /* URSQRTE */
12324             if (size == 3) {
12325                 unallocated_encoding(s);
12326                 return;
12327             }
12328             break;
12329         case 0x1e: /* FRINT32Z */
12330         case 0x1f: /* FRINT64Z */
12331             rmode = FPROUNDING_ZERO;
12332             /* fall through */
12333         case 0x5e: /* FRINT32X */
12334         case 0x5f: /* FRINT64X */
12335             need_fpstatus = true;
12336             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12337                 unallocated_encoding(s);
12338                 return;
12339             }
12340             break;
12341         default:
12342             unallocated_encoding(s);
12343             return;
12344         }
12345         break;
12346     }
12347     default:
12348         unallocated_encoding(s);
12349         return;
12350     }
12351 
12352     if (!fp_access_check(s)) {
12353         return;
12354     }
12355 
12356     if (need_fpstatus || rmode >= 0) {
12357         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12358     } else {
12359         tcg_fpstatus = NULL;
12360     }
12361     if (rmode >= 0) {
12362         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12363     } else {
12364         tcg_rmode = NULL;
12365     }
12366 
12367     switch (opcode) {
12368     case 0x5:
12369         if (u && size == 0) { /* NOT */
12370             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12371             return;
12372         }
12373         break;
12374     case 0x8: /* CMGT, CMGE */
12375         if (u) {
12376             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12377         } else {
12378             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12379         }
12380         return;
12381     case 0x9: /* CMEQ, CMLE */
12382         if (u) {
12383             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12384         } else {
12385             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12386         }
12387         return;
12388     case 0xa: /* CMLT */
12389         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12390         return;
12391     case 0xb:
12392         if (u) { /* ABS, NEG */
12393             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12394         } else {
12395             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12396         }
12397         return;
12398     }
12399 
12400     if (size == 3) {
12401         /* All 64-bit element operations can be shared with scalar 2misc */
12402         int pass;
12403 
12404         /* Coverity claims (size == 3 && !is_q) has been eliminated
12405          * from all paths leading to here.
12406          */
12407         tcg_debug_assert(is_q);
12408         for (pass = 0; pass < 2; pass++) {
12409             TCGv_i64 tcg_op = tcg_temp_new_i64();
12410             TCGv_i64 tcg_res = tcg_temp_new_i64();
12411 
12412             read_vec_element(s, tcg_op, rn, pass, MO_64);
12413 
12414             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12415                             tcg_rmode, tcg_fpstatus);
12416 
12417             write_vec_element(s, tcg_res, rd, pass, MO_64);
12418         }
12419     } else {
12420         int pass;
12421 
12422         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12423             TCGv_i32 tcg_op = tcg_temp_new_i32();
12424             TCGv_i32 tcg_res = tcg_temp_new_i32();
12425 
12426             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12427 
12428             if (size == 2) {
12429                 /* Special cases for 32 bit elements */
12430                 switch (opcode) {
12431                 case 0x4: /* CLS */
12432                     if (u) {
12433                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12434                     } else {
12435                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
12436                     }
12437                     break;
12438                 case 0x7: /* SQABS, SQNEG */
12439                     if (u) {
12440                         gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12441                     } else {
12442                         gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12443                     }
12444                     break;
12445                 case 0x2f: /* FABS */
12446                     gen_helper_vfp_abss(tcg_res, tcg_op);
12447                     break;
12448                 case 0x6f: /* FNEG */
12449                     gen_helper_vfp_negs(tcg_res, tcg_op);
12450                     break;
12451                 case 0x7f: /* FSQRT */
12452                     gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12453                     break;
12454                 case 0x1a: /* FCVTNS */
12455                 case 0x1b: /* FCVTMS */
12456                 case 0x1c: /* FCVTAS */
12457                 case 0x3a: /* FCVTPS */
12458                 case 0x3b: /* FCVTZS */
12459                     gen_helper_vfp_tosls(tcg_res, tcg_op,
12460                                          tcg_constant_i32(0), tcg_fpstatus);
12461                     break;
12462                 case 0x5a: /* FCVTNU */
12463                 case 0x5b: /* FCVTMU */
12464                 case 0x5c: /* FCVTAU */
12465                 case 0x7a: /* FCVTPU */
12466                 case 0x7b: /* FCVTZU */
12467                     gen_helper_vfp_touls(tcg_res, tcg_op,
12468                                          tcg_constant_i32(0), tcg_fpstatus);
12469                     break;
12470                 case 0x18: /* FRINTN */
12471                 case 0x19: /* FRINTM */
12472                 case 0x38: /* FRINTP */
12473                 case 0x39: /* FRINTZ */
12474                 case 0x58: /* FRINTA */
12475                 case 0x79: /* FRINTI */
12476                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12477                     break;
12478                 case 0x59: /* FRINTX */
12479                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12480                     break;
12481                 case 0x7c: /* URSQRTE */
12482                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
12483                     break;
12484                 case 0x1e: /* FRINT32Z */
12485                 case 0x5e: /* FRINT32X */
12486                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12487                     break;
12488                 case 0x1f: /* FRINT64Z */
12489                 case 0x5f: /* FRINT64X */
12490                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12491                     break;
12492                 default:
12493                     g_assert_not_reached();
12494                 }
12495             } else {
12496                 /* Use helpers for 8 and 16 bit elements */
12497                 switch (opcode) {
12498                 case 0x5: /* CNT, RBIT */
12499                     /* For these two insns size is part of the opcode specifier
12500                      * (handled earlier); they always operate on byte elements.
12501                      */
12502                     if (u) {
12503                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12504                     } else {
12505                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12506                     }
12507                     break;
12508                 case 0x7: /* SQABS, SQNEG */
12509                 {
12510                     NeonGenOneOpEnvFn *genfn;
12511                     static NeonGenOneOpEnvFn * const fns[2][2] = {
12512                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12513                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12514                     };
12515                     genfn = fns[size][u];
12516                     genfn(tcg_res, cpu_env, tcg_op);
12517                     break;
12518                 }
12519                 case 0x4: /* CLS, CLZ */
12520                     if (u) {
12521                         if (size == 0) {
12522                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
12523                         } else {
12524                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
12525                         }
12526                     } else {
12527                         if (size == 0) {
12528                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
12529                         } else {
12530                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
12531                         }
12532                     }
12533                     break;
12534                 default:
12535                     g_assert_not_reached();
12536                 }
12537             }
12538 
12539             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12540         }
12541     }
12542     clear_vec_high(s, is_q, rd);
12543 
12544     if (tcg_rmode) {
12545         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12546     }
12547 }
12548 
12549 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12550  *
12551  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
12552  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12553  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12554  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12555  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12556  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12557  *
12558  * This actually covers two groups where scalar access is governed by
12559  * bit 28. A bunch of the instructions (float to integral) only exist
12560  * in the vector form and are un-allocated for the scalar decode. Also
12561  * in the scalar decode Q is always 1.
12562  */
12563 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12564 {
12565     int fpop, opcode, a, u;
12566     int rn, rd;
12567     bool is_q;
12568     bool is_scalar;
12569     bool only_in_vector = false;
12570 
12571     int pass;
12572     TCGv_i32 tcg_rmode = NULL;
12573     TCGv_ptr tcg_fpstatus = NULL;
12574     bool need_fpst = true;
12575     int rmode = -1;
12576 
12577     if (!dc_isar_feature(aa64_fp16, s)) {
12578         unallocated_encoding(s);
12579         return;
12580     }
12581 
12582     rd = extract32(insn, 0, 5);
12583     rn = extract32(insn, 5, 5);
12584 
12585     a = extract32(insn, 23, 1);
12586     u = extract32(insn, 29, 1);
12587     is_scalar = extract32(insn, 28, 1);
12588     is_q = extract32(insn, 30, 1);
12589 
12590     opcode = extract32(insn, 12, 5);
12591     fpop = deposit32(opcode, 5, 1, a);
12592     fpop = deposit32(fpop, 6, 1, u);
12593 
12594     switch (fpop) {
12595     case 0x1d: /* SCVTF */
12596     case 0x5d: /* UCVTF */
12597     {
12598         int elements;
12599 
12600         if (is_scalar) {
12601             elements = 1;
12602         } else {
12603             elements = (is_q ? 8 : 4);
12604         }
12605 
12606         if (!fp_access_check(s)) {
12607             return;
12608         }
12609         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12610         return;
12611     }
12612     break;
12613     case 0x2c: /* FCMGT (zero) */
12614     case 0x2d: /* FCMEQ (zero) */
12615     case 0x2e: /* FCMLT (zero) */
12616     case 0x6c: /* FCMGE (zero) */
12617     case 0x6d: /* FCMLE (zero) */
12618         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12619         return;
12620     case 0x3d: /* FRECPE */
12621     case 0x3f: /* FRECPX */
12622         break;
12623     case 0x18: /* FRINTN */
12624         only_in_vector = true;
12625         rmode = FPROUNDING_TIEEVEN;
12626         break;
12627     case 0x19: /* FRINTM */
12628         only_in_vector = true;
12629         rmode = FPROUNDING_NEGINF;
12630         break;
12631     case 0x38: /* FRINTP */
12632         only_in_vector = true;
12633         rmode = FPROUNDING_POSINF;
12634         break;
12635     case 0x39: /* FRINTZ */
12636         only_in_vector = true;
12637         rmode = FPROUNDING_ZERO;
12638         break;
12639     case 0x58: /* FRINTA */
12640         only_in_vector = true;
12641         rmode = FPROUNDING_TIEAWAY;
12642         break;
12643     case 0x59: /* FRINTX */
12644     case 0x79: /* FRINTI */
12645         only_in_vector = true;
12646         /* current rounding mode */
12647         break;
12648     case 0x1a: /* FCVTNS */
12649         rmode = FPROUNDING_TIEEVEN;
12650         break;
12651     case 0x1b: /* FCVTMS */
12652         rmode = FPROUNDING_NEGINF;
12653         break;
12654     case 0x1c: /* FCVTAS */
12655         rmode = FPROUNDING_TIEAWAY;
12656         break;
12657     case 0x3a: /* FCVTPS */
12658         rmode = FPROUNDING_POSINF;
12659         break;
12660     case 0x3b: /* FCVTZS */
12661         rmode = FPROUNDING_ZERO;
12662         break;
12663     case 0x5a: /* FCVTNU */
12664         rmode = FPROUNDING_TIEEVEN;
12665         break;
12666     case 0x5b: /* FCVTMU */
12667         rmode = FPROUNDING_NEGINF;
12668         break;
12669     case 0x5c: /* FCVTAU */
12670         rmode = FPROUNDING_TIEAWAY;
12671         break;
12672     case 0x7a: /* FCVTPU */
12673         rmode = FPROUNDING_POSINF;
12674         break;
12675     case 0x7b: /* FCVTZU */
12676         rmode = FPROUNDING_ZERO;
12677         break;
12678     case 0x2f: /* FABS */
12679     case 0x6f: /* FNEG */
12680         need_fpst = false;
12681         break;
12682     case 0x7d: /* FRSQRTE */
12683     case 0x7f: /* FSQRT (vector) */
12684         break;
12685     default:
12686         unallocated_encoding(s);
12687         return;
12688     }
12689 
12690 
12691     /* Check additional constraints for the scalar encoding */
12692     if (is_scalar) {
12693         if (!is_q) {
12694             unallocated_encoding(s);
12695             return;
12696         }
12697         /* FRINTxx is only in the vector form */
12698         if (only_in_vector) {
12699             unallocated_encoding(s);
12700             return;
12701         }
12702     }
12703 
12704     if (!fp_access_check(s)) {
12705         return;
12706     }
12707 
12708     if (rmode >= 0 || need_fpst) {
12709         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
12710     }
12711 
12712     if (rmode >= 0) {
12713         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12714     }
12715 
12716     if (is_scalar) {
12717         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12718         TCGv_i32 tcg_res = tcg_temp_new_i32();
12719 
12720         switch (fpop) {
12721         case 0x1a: /* FCVTNS */
12722         case 0x1b: /* FCVTMS */
12723         case 0x1c: /* FCVTAS */
12724         case 0x3a: /* FCVTPS */
12725         case 0x3b: /* FCVTZS */
12726             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12727             break;
12728         case 0x3d: /* FRECPE */
12729             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12730             break;
12731         case 0x3f: /* FRECPX */
12732             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12733             break;
12734         case 0x5a: /* FCVTNU */
12735         case 0x5b: /* FCVTMU */
12736         case 0x5c: /* FCVTAU */
12737         case 0x7a: /* FCVTPU */
12738         case 0x7b: /* FCVTZU */
12739             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12740             break;
12741         case 0x6f: /* FNEG */
12742             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12743             break;
12744         case 0x7d: /* FRSQRTE */
12745             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12746             break;
12747         default:
12748             g_assert_not_reached();
12749         }
12750 
12751         /* limit any sign extension going on */
12752         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12753         write_fp_sreg(s, rd, tcg_res);
12754     } else {
12755         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12756             TCGv_i32 tcg_op = tcg_temp_new_i32();
12757             TCGv_i32 tcg_res = tcg_temp_new_i32();
12758 
12759             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12760 
12761             switch (fpop) {
12762             case 0x1a: /* FCVTNS */
12763             case 0x1b: /* FCVTMS */
12764             case 0x1c: /* FCVTAS */
12765             case 0x3a: /* FCVTPS */
12766             case 0x3b: /* FCVTZS */
12767                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12768                 break;
12769             case 0x3d: /* FRECPE */
12770                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12771                 break;
12772             case 0x5a: /* FCVTNU */
12773             case 0x5b: /* FCVTMU */
12774             case 0x5c: /* FCVTAU */
12775             case 0x7a: /* FCVTPU */
12776             case 0x7b: /* FCVTZU */
12777                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12778                 break;
12779             case 0x18: /* FRINTN */
12780             case 0x19: /* FRINTM */
12781             case 0x38: /* FRINTP */
12782             case 0x39: /* FRINTZ */
12783             case 0x58: /* FRINTA */
12784             case 0x79: /* FRINTI */
12785                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12786                 break;
12787             case 0x59: /* FRINTX */
12788                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12789                 break;
12790             case 0x2f: /* FABS */
12791                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12792                 break;
12793             case 0x6f: /* FNEG */
12794                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12795                 break;
12796             case 0x7d: /* FRSQRTE */
12797                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12798                 break;
12799             case 0x7f: /* FSQRT */
12800                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12801                 break;
12802             default:
12803                 g_assert_not_reached();
12804             }
12805 
12806             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12807         }
12808 
12809         clear_vec_high(s, is_q, rd);
12810     }
12811 
12812     if (tcg_rmode) {
12813         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12814     }
12815 }
12816 
12817 /* AdvSIMD scalar x indexed element
12818  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12819  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12820  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12821  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12822  * AdvSIMD vector x indexed element
12823  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12824  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12825  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12826  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12827  */
12828 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12829 {
12830     /* This encoding has two kinds of instruction:
12831      *  normal, where we perform elt x idxelt => elt for each
12832      *     element in the vector
12833      *  long, where we perform elt x idxelt and generate a result of
12834      *     double the width of the input element
12835      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12836      */
12837     bool is_scalar = extract32(insn, 28, 1);
12838     bool is_q = extract32(insn, 30, 1);
12839     bool u = extract32(insn, 29, 1);
12840     int size = extract32(insn, 22, 2);
12841     int l = extract32(insn, 21, 1);
12842     int m = extract32(insn, 20, 1);
12843     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12844     int rm = extract32(insn, 16, 4);
12845     int opcode = extract32(insn, 12, 4);
12846     int h = extract32(insn, 11, 1);
12847     int rn = extract32(insn, 5, 5);
12848     int rd = extract32(insn, 0, 5);
12849     bool is_long = false;
12850     int is_fp = 0;
12851     bool is_fp16 = false;
12852     int index;
12853     TCGv_ptr fpst;
12854 
12855     switch (16 * u + opcode) {
12856     case 0x08: /* MUL */
12857     case 0x10: /* MLA */
12858     case 0x14: /* MLS */
12859         if (is_scalar) {
12860             unallocated_encoding(s);
12861             return;
12862         }
12863         break;
12864     case 0x02: /* SMLAL, SMLAL2 */
12865     case 0x12: /* UMLAL, UMLAL2 */
12866     case 0x06: /* SMLSL, SMLSL2 */
12867     case 0x16: /* UMLSL, UMLSL2 */
12868     case 0x0a: /* SMULL, SMULL2 */
12869     case 0x1a: /* UMULL, UMULL2 */
12870         if (is_scalar) {
12871             unallocated_encoding(s);
12872             return;
12873         }
12874         is_long = true;
12875         break;
12876     case 0x03: /* SQDMLAL, SQDMLAL2 */
12877     case 0x07: /* SQDMLSL, SQDMLSL2 */
12878     case 0x0b: /* SQDMULL, SQDMULL2 */
12879         is_long = true;
12880         break;
12881     case 0x0c: /* SQDMULH */
12882     case 0x0d: /* SQRDMULH */
12883         break;
12884     case 0x01: /* FMLA */
12885     case 0x05: /* FMLS */
12886     case 0x09: /* FMUL */
12887     case 0x19: /* FMULX */
12888         is_fp = 1;
12889         break;
12890     case 0x1d: /* SQRDMLAH */
12891     case 0x1f: /* SQRDMLSH */
12892         if (!dc_isar_feature(aa64_rdm, s)) {
12893             unallocated_encoding(s);
12894             return;
12895         }
12896         break;
12897     case 0x0e: /* SDOT */
12898     case 0x1e: /* UDOT */
12899         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12900             unallocated_encoding(s);
12901             return;
12902         }
12903         break;
12904     case 0x0f:
12905         switch (size) {
12906         case 0: /* SUDOT */
12907         case 2: /* USDOT */
12908             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12909                 unallocated_encoding(s);
12910                 return;
12911             }
12912             size = MO_32;
12913             break;
12914         case 1: /* BFDOT */
12915             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12916                 unallocated_encoding(s);
12917                 return;
12918             }
12919             size = MO_32;
12920             break;
12921         case 3: /* BFMLAL{B,T} */
12922             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12923                 unallocated_encoding(s);
12924                 return;
12925             }
12926             /* can't set is_fp without other incorrect size checks */
12927             size = MO_16;
12928             break;
12929         default:
12930             unallocated_encoding(s);
12931             return;
12932         }
12933         break;
12934     case 0x11: /* FCMLA #0 */
12935     case 0x13: /* FCMLA #90 */
12936     case 0x15: /* FCMLA #180 */
12937     case 0x17: /* FCMLA #270 */
12938         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
12939             unallocated_encoding(s);
12940             return;
12941         }
12942         is_fp = 2;
12943         break;
12944     case 0x00: /* FMLAL */
12945     case 0x04: /* FMLSL */
12946     case 0x18: /* FMLAL2 */
12947     case 0x1c: /* FMLSL2 */
12948         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
12949             unallocated_encoding(s);
12950             return;
12951         }
12952         size = MO_16;
12953         /* is_fp, but we pass cpu_env not fp_status.  */
12954         break;
12955     default:
12956         unallocated_encoding(s);
12957         return;
12958     }
12959 
12960     switch (is_fp) {
12961     case 1: /* normal fp */
12962         /* convert insn encoded size to MemOp size */
12963         switch (size) {
12964         case 0: /* half-precision */
12965             size = MO_16;
12966             is_fp16 = true;
12967             break;
12968         case MO_32: /* single precision */
12969         case MO_64: /* double precision */
12970             break;
12971         default:
12972             unallocated_encoding(s);
12973             return;
12974         }
12975         break;
12976 
12977     case 2: /* complex fp */
12978         /* Each indexable element is a complex pair.  */
12979         size += 1;
12980         switch (size) {
12981         case MO_32:
12982             if (h && !is_q) {
12983                 unallocated_encoding(s);
12984                 return;
12985             }
12986             is_fp16 = true;
12987             break;
12988         case MO_64:
12989             break;
12990         default:
12991             unallocated_encoding(s);
12992             return;
12993         }
12994         break;
12995 
12996     default: /* integer */
12997         switch (size) {
12998         case MO_8:
12999         case MO_64:
13000             unallocated_encoding(s);
13001             return;
13002         }
13003         break;
13004     }
13005     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
13006         unallocated_encoding(s);
13007         return;
13008     }
13009 
13010     /* Given MemOp size, adjust register and indexing.  */
13011     switch (size) {
13012     case MO_16:
13013         index = h << 2 | l << 1 | m;
13014         break;
13015     case MO_32:
13016         index = h << 1 | l;
13017         rm |= m << 4;
13018         break;
13019     case MO_64:
13020         if (l || !is_q) {
13021             unallocated_encoding(s);
13022             return;
13023         }
13024         index = h;
13025         rm |= m << 4;
13026         break;
13027     default:
13028         g_assert_not_reached();
13029     }
13030 
13031     if (!fp_access_check(s)) {
13032         return;
13033     }
13034 
13035     if (is_fp) {
13036         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
13037     } else {
13038         fpst = NULL;
13039     }
13040 
13041     switch (16 * u + opcode) {
13042     case 0x0e: /* SDOT */
13043     case 0x1e: /* UDOT */
13044         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13045                          u ? gen_helper_gvec_udot_idx_b
13046                          : gen_helper_gvec_sdot_idx_b);
13047         return;
13048     case 0x0f:
13049         switch (extract32(insn, 22, 2)) {
13050         case 0: /* SUDOT */
13051             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13052                              gen_helper_gvec_sudot_idx_b);
13053             return;
13054         case 1: /* BFDOT */
13055             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13056                              gen_helper_gvec_bfdot_idx);
13057             return;
13058         case 2: /* USDOT */
13059             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13060                              gen_helper_gvec_usdot_idx_b);
13061             return;
13062         case 3: /* BFMLAL{B,T} */
13063             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
13064                               gen_helper_gvec_bfmlal_idx);
13065             return;
13066         }
13067         g_assert_not_reached();
13068     case 0x11: /* FCMLA #0 */
13069     case 0x13: /* FCMLA #90 */
13070     case 0x15: /* FCMLA #180 */
13071     case 0x17: /* FCMLA #270 */
13072         {
13073             int rot = extract32(insn, 13, 2);
13074             int data = (index << 2) | rot;
13075             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
13076                                vec_full_reg_offset(s, rn),
13077                                vec_full_reg_offset(s, rm),
13078                                vec_full_reg_offset(s, rd), fpst,
13079                                is_q ? 16 : 8, vec_full_reg_size(s), data,
13080                                size == MO_64
13081                                ? gen_helper_gvec_fcmlas_idx
13082                                : gen_helper_gvec_fcmlah_idx);
13083         }
13084         return;
13085 
13086     case 0x00: /* FMLAL */
13087     case 0x04: /* FMLSL */
13088     case 0x18: /* FMLAL2 */
13089     case 0x1c: /* FMLSL2 */
13090         {
13091             int is_s = extract32(opcode, 2, 1);
13092             int is_2 = u;
13093             int data = (index << 2) | (is_2 << 1) | is_s;
13094             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13095                                vec_full_reg_offset(s, rn),
13096                                vec_full_reg_offset(s, rm), cpu_env,
13097                                is_q ? 16 : 8, vec_full_reg_size(s),
13098                                data, gen_helper_gvec_fmlal_idx_a64);
13099         }
13100         return;
13101 
13102     case 0x08: /* MUL */
13103         if (!is_long && !is_scalar) {
13104             static gen_helper_gvec_3 * const fns[3] = {
13105                 gen_helper_gvec_mul_idx_h,
13106                 gen_helper_gvec_mul_idx_s,
13107                 gen_helper_gvec_mul_idx_d,
13108             };
13109             tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
13110                                vec_full_reg_offset(s, rn),
13111                                vec_full_reg_offset(s, rm),
13112                                is_q ? 16 : 8, vec_full_reg_size(s),
13113                                index, fns[size - 1]);
13114             return;
13115         }
13116         break;
13117 
13118     case 0x10: /* MLA */
13119         if (!is_long && !is_scalar) {
13120             static gen_helper_gvec_4 * const fns[3] = {
13121                 gen_helper_gvec_mla_idx_h,
13122                 gen_helper_gvec_mla_idx_s,
13123                 gen_helper_gvec_mla_idx_d,
13124             };
13125             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13126                                vec_full_reg_offset(s, rn),
13127                                vec_full_reg_offset(s, rm),
13128                                vec_full_reg_offset(s, rd),
13129                                is_q ? 16 : 8, vec_full_reg_size(s),
13130                                index, fns[size - 1]);
13131             return;
13132         }
13133         break;
13134 
13135     case 0x14: /* MLS */
13136         if (!is_long && !is_scalar) {
13137             static gen_helper_gvec_4 * const fns[3] = {
13138                 gen_helper_gvec_mls_idx_h,
13139                 gen_helper_gvec_mls_idx_s,
13140                 gen_helper_gvec_mls_idx_d,
13141             };
13142             tcg_gen_gvec_4_ool(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),
13146                                is_q ? 16 : 8, vec_full_reg_size(s),
13147                                index, fns[size - 1]);
13148             return;
13149         }
13150         break;
13151     }
13152 
13153     if (size == 3) {
13154         TCGv_i64 tcg_idx = tcg_temp_new_i64();
13155         int pass;
13156 
13157         assert(is_fp && is_q && !is_long);
13158 
13159         read_vec_element(s, tcg_idx, rm, index, MO_64);
13160 
13161         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13162             TCGv_i64 tcg_op = tcg_temp_new_i64();
13163             TCGv_i64 tcg_res = tcg_temp_new_i64();
13164 
13165             read_vec_element(s, tcg_op, rn, pass, MO_64);
13166 
13167             switch (16 * u + opcode) {
13168             case 0x05: /* FMLS */
13169                 /* As usual for ARM, separate negation for fused multiply-add */
13170                 gen_helper_vfp_negd(tcg_op, tcg_op);
13171                 /* fall through */
13172             case 0x01: /* FMLA */
13173                 read_vec_element(s, tcg_res, rd, pass, MO_64);
13174                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13175                 break;
13176             case 0x09: /* FMUL */
13177                 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13178                 break;
13179             case 0x19: /* FMULX */
13180                 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13181                 break;
13182             default:
13183                 g_assert_not_reached();
13184             }
13185 
13186             write_vec_element(s, tcg_res, rd, pass, MO_64);
13187         }
13188 
13189         clear_vec_high(s, !is_scalar, rd);
13190     } else if (!is_long) {
13191         /* 32 bit floating point, or 16 or 32 bit integer.
13192          * For the 16 bit scalar case we use the usual Neon helpers and
13193          * rely on the fact that 0 op 0 == 0 with no side effects.
13194          */
13195         TCGv_i32 tcg_idx = tcg_temp_new_i32();
13196         int pass, maxpasses;
13197 
13198         if (is_scalar) {
13199             maxpasses = 1;
13200         } else {
13201             maxpasses = is_q ? 4 : 2;
13202         }
13203 
13204         read_vec_element_i32(s, tcg_idx, rm, index, size);
13205 
13206         if (size == 1 && !is_scalar) {
13207             /* The simplest way to handle the 16x16 indexed ops is to duplicate
13208              * the index into both halves of the 32 bit tcg_idx and then use
13209              * the usual Neon helpers.
13210              */
13211             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13212         }
13213 
13214         for (pass = 0; pass < maxpasses; pass++) {
13215             TCGv_i32 tcg_op = tcg_temp_new_i32();
13216             TCGv_i32 tcg_res = tcg_temp_new_i32();
13217 
13218             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13219 
13220             switch (16 * u + opcode) {
13221             case 0x08: /* MUL */
13222             case 0x10: /* MLA */
13223             case 0x14: /* MLS */
13224             {
13225                 static NeonGenTwoOpFn * const fns[2][2] = {
13226                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13227                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
13228                 };
13229                 NeonGenTwoOpFn *genfn;
13230                 bool is_sub = opcode == 0x4;
13231 
13232                 if (size == 1) {
13233                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13234                 } else {
13235                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13236                 }
13237                 if (opcode == 0x8) {
13238                     break;
13239                 }
13240                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13241                 genfn = fns[size - 1][is_sub];
13242                 genfn(tcg_res, tcg_op, tcg_res);
13243                 break;
13244             }
13245             case 0x05: /* FMLS */
13246             case 0x01: /* FMLA */
13247                 read_vec_element_i32(s, tcg_res, rd, pass,
13248                                      is_scalar ? size : MO_32);
13249                 switch (size) {
13250                 case 1:
13251                     if (opcode == 0x5) {
13252                         /* As usual for ARM, separate negation for fused
13253                          * multiply-add */
13254                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13255                     }
13256                     if (is_scalar) {
13257                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13258                                                    tcg_res, fpst);
13259                     } else {
13260                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13261                                                     tcg_res, fpst);
13262                     }
13263                     break;
13264                 case 2:
13265                     if (opcode == 0x5) {
13266                         /* As usual for ARM, separate negation for
13267                          * fused multiply-add */
13268                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13269                     }
13270                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13271                                            tcg_res, fpst);
13272                     break;
13273                 default:
13274                     g_assert_not_reached();
13275                 }
13276                 break;
13277             case 0x09: /* FMUL */
13278                 switch (size) {
13279                 case 1:
13280                     if (is_scalar) {
13281                         gen_helper_advsimd_mulh(tcg_res, tcg_op,
13282                                                 tcg_idx, fpst);
13283                     } else {
13284                         gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13285                                                  tcg_idx, fpst);
13286                     }
13287                     break;
13288                 case 2:
13289                     gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13290                     break;
13291                 default:
13292                     g_assert_not_reached();
13293                 }
13294                 break;
13295             case 0x19: /* FMULX */
13296                 switch (size) {
13297                 case 1:
13298                     if (is_scalar) {
13299                         gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13300                                                  tcg_idx, fpst);
13301                     } else {
13302                         gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13303                                                   tcg_idx, fpst);
13304                     }
13305                     break;
13306                 case 2:
13307                     gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13308                     break;
13309                 default:
13310                     g_assert_not_reached();
13311                 }
13312                 break;
13313             case 0x0c: /* SQDMULH */
13314                 if (size == 1) {
13315                     gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13316                                                tcg_op, tcg_idx);
13317                 } else {
13318                     gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13319                                                tcg_op, tcg_idx);
13320                 }
13321                 break;
13322             case 0x0d: /* SQRDMULH */
13323                 if (size == 1) {
13324                     gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13325                                                 tcg_op, tcg_idx);
13326                 } else {
13327                     gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13328                                                 tcg_op, tcg_idx);
13329                 }
13330                 break;
13331             case 0x1d: /* SQRDMLAH */
13332                 read_vec_element_i32(s, tcg_res, rd, pass,
13333                                      is_scalar ? size : MO_32);
13334                 if (size == 1) {
13335                     gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13336                                                 tcg_op, tcg_idx, tcg_res);
13337                 } else {
13338                     gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13339                                                 tcg_op, tcg_idx, tcg_res);
13340                 }
13341                 break;
13342             case 0x1f: /* SQRDMLSH */
13343                 read_vec_element_i32(s, tcg_res, rd, pass,
13344                                      is_scalar ? size : MO_32);
13345                 if (size == 1) {
13346                     gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13347                                                 tcg_op, tcg_idx, tcg_res);
13348                 } else {
13349                     gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13350                                                 tcg_op, tcg_idx, tcg_res);
13351                 }
13352                 break;
13353             default:
13354                 g_assert_not_reached();
13355             }
13356 
13357             if (is_scalar) {
13358                 write_fp_sreg(s, rd, tcg_res);
13359             } else {
13360                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13361             }
13362         }
13363 
13364         clear_vec_high(s, is_q, rd);
13365     } else {
13366         /* long ops: 16x16->32 or 32x32->64 */
13367         TCGv_i64 tcg_res[2];
13368         int pass;
13369         bool satop = extract32(opcode, 0, 1);
13370         MemOp memop = MO_32;
13371 
13372         if (satop || !u) {
13373             memop |= MO_SIGN;
13374         }
13375 
13376         if (size == 2) {
13377             TCGv_i64 tcg_idx = tcg_temp_new_i64();
13378 
13379             read_vec_element(s, tcg_idx, rm, index, memop);
13380 
13381             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13382                 TCGv_i64 tcg_op = tcg_temp_new_i64();
13383                 TCGv_i64 tcg_passres;
13384                 int passelt;
13385 
13386                 if (is_scalar) {
13387                     passelt = 0;
13388                 } else {
13389                     passelt = pass + (is_q * 2);
13390                 }
13391 
13392                 read_vec_element(s, tcg_op, rn, passelt, memop);
13393 
13394                 tcg_res[pass] = tcg_temp_new_i64();
13395 
13396                 if (opcode == 0xa || opcode == 0xb) {
13397                     /* Non-accumulating ops */
13398                     tcg_passres = tcg_res[pass];
13399                 } else {
13400                     tcg_passres = tcg_temp_new_i64();
13401                 }
13402 
13403                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13404 
13405                 if (satop) {
13406                     /* saturating, doubling */
13407                     gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13408                                                       tcg_passres, tcg_passres);
13409                 }
13410 
13411                 if (opcode == 0xa || opcode == 0xb) {
13412                     continue;
13413                 }
13414 
13415                 /* Accumulating op: handle accumulate step */
13416                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13417 
13418                 switch (opcode) {
13419                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13420                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13421                     break;
13422                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13423                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13424                     break;
13425                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13426                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
13427                     /* fall through */
13428                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13429                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13430                                                       tcg_res[pass],
13431                                                       tcg_passres);
13432                     break;
13433                 default:
13434                     g_assert_not_reached();
13435                 }
13436             }
13437 
13438             clear_vec_high(s, !is_scalar, rd);
13439         } else {
13440             TCGv_i32 tcg_idx = tcg_temp_new_i32();
13441 
13442             assert(size == 1);
13443             read_vec_element_i32(s, tcg_idx, rm, index, size);
13444 
13445             if (!is_scalar) {
13446                 /* The simplest way to handle the 16x16 indexed ops is to
13447                  * duplicate the index into both halves of the 32 bit tcg_idx
13448                  * and then use the usual Neon helpers.
13449                  */
13450                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13451             }
13452 
13453             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13454                 TCGv_i32 tcg_op = tcg_temp_new_i32();
13455                 TCGv_i64 tcg_passres;
13456 
13457                 if (is_scalar) {
13458                     read_vec_element_i32(s, tcg_op, rn, pass, size);
13459                 } else {
13460                     read_vec_element_i32(s, tcg_op, rn,
13461                                          pass + (is_q * 2), MO_32);
13462                 }
13463 
13464                 tcg_res[pass] = tcg_temp_new_i64();
13465 
13466                 if (opcode == 0xa || opcode == 0xb) {
13467                     /* Non-accumulating ops */
13468                     tcg_passres = tcg_res[pass];
13469                 } else {
13470                     tcg_passres = tcg_temp_new_i64();
13471                 }
13472 
13473                 if (memop & MO_SIGN) {
13474                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13475                 } else {
13476                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13477                 }
13478                 if (satop) {
13479                     gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13480                                                       tcg_passres, tcg_passres);
13481                 }
13482 
13483                 if (opcode == 0xa || opcode == 0xb) {
13484                     continue;
13485                 }
13486 
13487                 /* Accumulating op: handle accumulate step */
13488                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13489 
13490                 switch (opcode) {
13491                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13492                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13493                                              tcg_passres);
13494                     break;
13495                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13496                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13497                                              tcg_passres);
13498                     break;
13499                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13500                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13501                     /* fall through */
13502                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13503                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13504                                                       tcg_res[pass],
13505                                                       tcg_passres);
13506                     break;
13507                 default:
13508                     g_assert_not_reached();
13509                 }
13510             }
13511 
13512             if (is_scalar) {
13513                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13514             }
13515         }
13516 
13517         if (is_scalar) {
13518             tcg_res[1] = tcg_constant_i64(0);
13519         }
13520 
13521         for (pass = 0; pass < 2; pass++) {
13522             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13523         }
13524     }
13525 }
13526 
13527 /* Crypto AES
13528  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13529  * +-----------------+------+-----------+--------+-----+------+------+
13530  * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13531  * +-----------------+------+-----------+--------+-----+------+------+
13532  */
13533 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13534 {
13535     int size = extract32(insn, 22, 2);
13536     int opcode = extract32(insn, 12, 5);
13537     int rn = extract32(insn, 5, 5);
13538     int rd = extract32(insn, 0, 5);
13539     int decrypt;
13540     gen_helper_gvec_2 *genfn2 = NULL;
13541     gen_helper_gvec_3 *genfn3 = NULL;
13542 
13543     if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13544         unallocated_encoding(s);
13545         return;
13546     }
13547 
13548     switch (opcode) {
13549     case 0x4: /* AESE */
13550         decrypt = 0;
13551         genfn3 = gen_helper_crypto_aese;
13552         break;
13553     case 0x6: /* AESMC */
13554         decrypt = 0;
13555         genfn2 = gen_helper_crypto_aesmc;
13556         break;
13557     case 0x5: /* AESD */
13558         decrypt = 1;
13559         genfn3 = gen_helper_crypto_aese;
13560         break;
13561     case 0x7: /* AESIMC */
13562         decrypt = 1;
13563         genfn2 = gen_helper_crypto_aesmc;
13564         break;
13565     default:
13566         unallocated_encoding(s);
13567         return;
13568     }
13569 
13570     if (!fp_access_check(s)) {
13571         return;
13572     }
13573     if (genfn2) {
13574         gen_gvec_op2_ool(s, true, rd, rn, decrypt, genfn2);
13575     } else {
13576         gen_gvec_op3_ool(s, true, rd, rd, rn, decrypt, genfn3);
13577     }
13578 }
13579 
13580 /* Crypto three-reg SHA
13581  *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
13582  * +-----------------+------+---+------+---+--------+-----+------+------+
13583  * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
13584  * +-----------------+------+---+------+---+--------+-----+------+------+
13585  */
13586 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13587 {
13588     int size = extract32(insn, 22, 2);
13589     int opcode = extract32(insn, 12, 3);
13590     int rm = extract32(insn, 16, 5);
13591     int rn = extract32(insn, 5, 5);
13592     int rd = extract32(insn, 0, 5);
13593     gen_helper_gvec_3 *genfn;
13594     bool feature;
13595 
13596     if (size != 0) {
13597         unallocated_encoding(s);
13598         return;
13599     }
13600 
13601     switch (opcode) {
13602     case 0: /* SHA1C */
13603         genfn = gen_helper_crypto_sha1c;
13604         feature = dc_isar_feature(aa64_sha1, s);
13605         break;
13606     case 1: /* SHA1P */
13607         genfn = gen_helper_crypto_sha1p;
13608         feature = dc_isar_feature(aa64_sha1, s);
13609         break;
13610     case 2: /* SHA1M */
13611         genfn = gen_helper_crypto_sha1m;
13612         feature = dc_isar_feature(aa64_sha1, s);
13613         break;
13614     case 3: /* SHA1SU0 */
13615         genfn = gen_helper_crypto_sha1su0;
13616         feature = dc_isar_feature(aa64_sha1, s);
13617         break;
13618     case 4: /* SHA256H */
13619         genfn = gen_helper_crypto_sha256h;
13620         feature = dc_isar_feature(aa64_sha256, s);
13621         break;
13622     case 5: /* SHA256H2 */
13623         genfn = gen_helper_crypto_sha256h2;
13624         feature = dc_isar_feature(aa64_sha256, s);
13625         break;
13626     case 6: /* SHA256SU1 */
13627         genfn = gen_helper_crypto_sha256su1;
13628         feature = dc_isar_feature(aa64_sha256, s);
13629         break;
13630     default:
13631         unallocated_encoding(s);
13632         return;
13633     }
13634 
13635     if (!feature) {
13636         unallocated_encoding(s);
13637         return;
13638     }
13639 
13640     if (!fp_access_check(s)) {
13641         return;
13642     }
13643     gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
13644 }
13645 
13646 /* Crypto two-reg SHA
13647  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13648  * +-----------------+------+-----------+--------+-----+------+------+
13649  * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13650  * +-----------------+------+-----------+--------+-----+------+------+
13651  */
13652 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13653 {
13654     int size = extract32(insn, 22, 2);
13655     int opcode = extract32(insn, 12, 5);
13656     int rn = extract32(insn, 5, 5);
13657     int rd = extract32(insn, 0, 5);
13658     gen_helper_gvec_2 *genfn;
13659     bool feature;
13660 
13661     if (size != 0) {
13662         unallocated_encoding(s);
13663         return;
13664     }
13665 
13666     switch (opcode) {
13667     case 0: /* SHA1H */
13668         feature = dc_isar_feature(aa64_sha1, s);
13669         genfn = gen_helper_crypto_sha1h;
13670         break;
13671     case 1: /* SHA1SU1 */
13672         feature = dc_isar_feature(aa64_sha1, s);
13673         genfn = gen_helper_crypto_sha1su1;
13674         break;
13675     case 2: /* SHA256SU0 */
13676         feature = dc_isar_feature(aa64_sha256, s);
13677         genfn = gen_helper_crypto_sha256su0;
13678         break;
13679     default:
13680         unallocated_encoding(s);
13681         return;
13682     }
13683 
13684     if (!feature) {
13685         unallocated_encoding(s);
13686         return;
13687     }
13688 
13689     if (!fp_access_check(s)) {
13690         return;
13691     }
13692     gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
13693 }
13694 
13695 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
13696 {
13697     tcg_gen_rotli_i64(d, m, 1);
13698     tcg_gen_xor_i64(d, d, n);
13699 }
13700 
13701 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
13702 {
13703     tcg_gen_rotli_vec(vece, d, m, 1);
13704     tcg_gen_xor_vec(vece, d, d, n);
13705 }
13706 
13707 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
13708                    uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
13709 {
13710     static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
13711     static const GVecGen3 op = {
13712         .fni8 = gen_rax1_i64,
13713         .fniv = gen_rax1_vec,
13714         .opt_opc = vecop_list,
13715         .fno = gen_helper_crypto_rax1,
13716         .vece = MO_64,
13717     };
13718     tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
13719 }
13720 
13721 /* Crypto three-reg SHA512
13722  *  31                   21 20  16 15  14  13 12  11  10  9    5 4    0
13723  * +-----------------------+------+---+---+-----+--------+------+------+
13724  * | 1 1 0 0 1 1 1 0 0 1 1 |  Rm  | 1 | O | 0 0 | opcode |  Rn  |  Rd  |
13725  * +-----------------------+------+---+---+-----+--------+------+------+
13726  */
13727 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13728 {
13729     int opcode = extract32(insn, 10, 2);
13730     int o =  extract32(insn, 14, 1);
13731     int rm = extract32(insn, 16, 5);
13732     int rn = extract32(insn, 5, 5);
13733     int rd = extract32(insn, 0, 5);
13734     bool feature;
13735     gen_helper_gvec_3 *oolfn = NULL;
13736     GVecGen3Fn *gvecfn = NULL;
13737 
13738     if (o == 0) {
13739         switch (opcode) {
13740         case 0: /* SHA512H */
13741             feature = dc_isar_feature(aa64_sha512, s);
13742             oolfn = gen_helper_crypto_sha512h;
13743             break;
13744         case 1: /* SHA512H2 */
13745             feature = dc_isar_feature(aa64_sha512, s);
13746             oolfn = gen_helper_crypto_sha512h2;
13747             break;
13748         case 2: /* SHA512SU1 */
13749             feature = dc_isar_feature(aa64_sha512, s);
13750             oolfn = gen_helper_crypto_sha512su1;
13751             break;
13752         case 3: /* RAX1 */
13753             feature = dc_isar_feature(aa64_sha3, s);
13754             gvecfn = gen_gvec_rax1;
13755             break;
13756         default:
13757             g_assert_not_reached();
13758         }
13759     } else {
13760         switch (opcode) {
13761         case 0: /* SM3PARTW1 */
13762             feature = dc_isar_feature(aa64_sm3, s);
13763             oolfn = gen_helper_crypto_sm3partw1;
13764             break;
13765         case 1: /* SM3PARTW2 */
13766             feature = dc_isar_feature(aa64_sm3, s);
13767             oolfn = gen_helper_crypto_sm3partw2;
13768             break;
13769         case 2: /* SM4EKEY */
13770             feature = dc_isar_feature(aa64_sm4, s);
13771             oolfn = gen_helper_crypto_sm4ekey;
13772             break;
13773         default:
13774             unallocated_encoding(s);
13775             return;
13776         }
13777     }
13778 
13779     if (!feature) {
13780         unallocated_encoding(s);
13781         return;
13782     }
13783 
13784     if (!fp_access_check(s)) {
13785         return;
13786     }
13787 
13788     if (oolfn) {
13789         gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
13790     } else {
13791         gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
13792     }
13793 }
13794 
13795 /* Crypto two-reg SHA512
13796  *  31                                     12  11  10  9    5 4    0
13797  * +-----------------------------------------+--------+------+------+
13798  * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode |  Rn  |  Rd  |
13799  * +-----------------------------------------+--------+------+------+
13800  */
13801 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13802 {
13803     int opcode = extract32(insn, 10, 2);
13804     int rn = extract32(insn, 5, 5);
13805     int rd = extract32(insn, 0, 5);
13806     bool feature;
13807 
13808     switch (opcode) {
13809     case 0: /* SHA512SU0 */
13810         feature = dc_isar_feature(aa64_sha512, s);
13811         break;
13812     case 1: /* SM4E */
13813         feature = dc_isar_feature(aa64_sm4, s);
13814         break;
13815     default:
13816         unallocated_encoding(s);
13817         return;
13818     }
13819 
13820     if (!feature) {
13821         unallocated_encoding(s);
13822         return;
13823     }
13824 
13825     if (!fp_access_check(s)) {
13826         return;
13827     }
13828 
13829     switch (opcode) {
13830     case 0: /* SHA512SU0 */
13831         gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
13832         break;
13833     case 1: /* SM4E */
13834         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
13835         break;
13836     default:
13837         g_assert_not_reached();
13838     }
13839 }
13840 
13841 /* Crypto four-register
13842  *  31               23 22 21 20  16 15  14  10 9    5 4    0
13843  * +-------------------+-----+------+---+------+------+------+
13844  * | 1 1 0 0 1 1 1 0 0 | Op0 |  Rm  | 0 |  Ra  |  Rn  |  Rd  |
13845  * +-------------------+-----+------+---+------+------+------+
13846  */
13847 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13848 {
13849     int op0 = extract32(insn, 21, 2);
13850     int rm = extract32(insn, 16, 5);
13851     int ra = extract32(insn, 10, 5);
13852     int rn = extract32(insn, 5, 5);
13853     int rd = extract32(insn, 0, 5);
13854     bool feature;
13855 
13856     switch (op0) {
13857     case 0: /* EOR3 */
13858     case 1: /* BCAX */
13859         feature = dc_isar_feature(aa64_sha3, s);
13860         break;
13861     case 2: /* SM3SS1 */
13862         feature = dc_isar_feature(aa64_sm3, s);
13863         break;
13864     default:
13865         unallocated_encoding(s);
13866         return;
13867     }
13868 
13869     if (!feature) {
13870         unallocated_encoding(s);
13871         return;
13872     }
13873 
13874     if (!fp_access_check(s)) {
13875         return;
13876     }
13877 
13878     if (op0 < 2) {
13879         TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13880         int pass;
13881 
13882         tcg_op1 = tcg_temp_new_i64();
13883         tcg_op2 = tcg_temp_new_i64();
13884         tcg_op3 = tcg_temp_new_i64();
13885         tcg_res[0] = tcg_temp_new_i64();
13886         tcg_res[1] = tcg_temp_new_i64();
13887 
13888         for (pass = 0; pass < 2; pass++) {
13889             read_vec_element(s, tcg_op1, rn, pass, MO_64);
13890             read_vec_element(s, tcg_op2, rm, pass, MO_64);
13891             read_vec_element(s, tcg_op3, ra, pass, MO_64);
13892 
13893             if (op0 == 0) {
13894                 /* EOR3 */
13895                 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13896             } else {
13897                 /* BCAX */
13898                 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13899             }
13900             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13901         }
13902         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13903         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13904     } else {
13905         TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13906 
13907         tcg_op1 = tcg_temp_new_i32();
13908         tcg_op2 = tcg_temp_new_i32();
13909         tcg_op3 = tcg_temp_new_i32();
13910         tcg_res = tcg_temp_new_i32();
13911         tcg_zero = tcg_constant_i32(0);
13912 
13913         read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13914         read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13915         read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13916 
13917         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13918         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13919         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13920         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13921 
13922         write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13923         write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13924         write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13925         write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13926     }
13927 }
13928 
13929 /* Crypto XAR
13930  *  31                   21 20  16 15    10 9    5 4    0
13931  * +-----------------------+------+--------+------+------+
13932  * | 1 1 0 0 1 1 1 0 1 0 0 |  Rm  |  imm6  |  Rn  |  Rd  |
13933  * +-----------------------+------+--------+------+------+
13934  */
13935 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
13936 {
13937     int rm = extract32(insn, 16, 5);
13938     int imm6 = extract32(insn, 10, 6);
13939     int rn = extract32(insn, 5, 5);
13940     int rd = extract32(insn, 0, 5);
13941 
13942     if (!dc_isar_feature(aa64_sha3, s)) {
13943         unallocated_encoding(s);
13944         return;
13945     }
13946 
13947     if (!fp_access_check(s)) {
13948         return;
13949     }
13950 
13951     gen_gvec_xar(MO_64, vec_full_reg_offset(s, rd),
13952                  vec_full_reg_offset(s, rn),
13953                  vec_full_reg_offset(s, rm), imm6, 16,
13954                  vec_full_reg_size(s));
13955 }
13956 
13957 /* Crypto three-reg imm2
13958  *  31                   21 20  16 15  14 13 12  11  10  9    5 4    0
13959  * +-----------------------+------+-----+------+--------+------+------+
13960  * | 1 1 0 0 1 1 1 0 0 1 0 |  Rm  | 1 0 | imm2 | opcode |  Rn  |  Rd  |
13961  * +-----------------------+------+-----+------+--------+------+------+
13962  */
13963 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
13964 {
13965     static gen_helper_gvec_3 * const fns[4] = {
13966         gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
13967         gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
13968     };
13969     int opcode = extract32(insn, 10, 2);
13970     int imm2 = extract32(insn, 12, 2);
13971     int rm = extract32(insn, 16, 5);
13972     int rn = extract32(insn, 5, 5);
13973     int rd = extract32(insn, 0, 5);
13974 
13975     if (!dc_isar_feature(aa64_sm3, s)) {
13976         unallocated_encoding(s);
13977         return;
13978     }
13979 
13980     if (!fp_access_check(s)) {
13981         return;
13982     }
13983 
13984     gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
13985 }
13986 
13987 /* C3.6 Data processing - SIMD, inc Crypto
13988  *
13989  * As the decode gets a little complex we are using a table based
13990  * approach for this part of the decode.
13991  */
13992 static const AArch64DecodeTable data_proc_simd[] = {
13993     /* pattern  ,  mask     ,  fn                        */
13994     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
13995     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
13996     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
13997     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
13998     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
13999     { 0x0e000400, 0x9fe08400, disas_simd_copy },
14000     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
14001     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
14002     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
14003     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
14004     { 0x0e000000, 0xbf208c00, disas_simd_tb },
14005     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
14006     { 0x2e000000, 0xbf208400, disas_simd_ext },
14007     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
14008     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
14009     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
14010     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
14011     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
14012     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
14013     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
14014     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
14015     { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
14016     { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
14017     { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
14018     { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
14019     { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
14020     { 0xce000000, 0xff808000, disas_crypto_four_reg },
14021     { 0xce800000, 0xffe00000, disas_crypto_xar },
14022     { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
14023     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
14024     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
14025     { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
14026     { 0x00000000, 0x00000000, NULL }
14027 };
14028 
14029 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
14030 {
14031     /* Note that this is called with all non-FP cases from
14032      * table C3-6 so it must UNDEF for entries not specifically
14033      * allocated to instructions in that table.
14034      */
14035     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
14036     if (fn) {
14037         fn(s, insn);
14038     } else {
14039         unallocated_encoding(s);
14040     }
14041 }
14042 
14043 /* C3.6 Data processing - SIMD and floating point */
14044 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
14045 {
14046     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
14047         disas_data_proc_fp(s, insn);
14048     } else {
14049         /* SIMD, including crypto */
14050         disas_data_proc_simd(s, insn);
14051     }
14052 }
14053 
14054 static bool trans_OK(DisasContext *s, arg_OK *a)
14055 {
14056     return true;
14057 }
14058 
14059 static bool trans_FAIL(DisasContext *s, arg_OK *a)
14060 {
14061     s->is_nonstreaming = true;
14062     return true;
14063 }
14064 
14065 /**
14066  * is_guarded_page:
14067  * @env: The cpu environment
14068  * @s: The DisasContext
14069  *
14070  * Return true if the page is guarded.
14071  */
14072 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
14073 {
14074     uint64_t addr = s->base.pc_first;
14075 #ifdef CONFIG_USER_ONLY
14076     return page_get_flags(addr) & PAGE_BTI;
14077 #else
14078     CPUTLBEntryFull *full;
14079     void *host;
14080     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
14081     int flags;
14082 
14083     /*
14084      * We test this immediately after reading an insn, which means
14085      * that the TLB entry must be present and valid, and thus this
14086      * access will never raise an exception.
14087      */
14088     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
14089                               false, &host, &full, 0);
14090     assert(!(flags & TLB_INVALID_MASK));
14091 
14092     return full->guarded;
14093 #endif
14094 }
14095 
14096 /**
14097  * btype_destination_ok:
14098  * @insn: The instruction at the branch destination
14099  * @bt: SCTLR_ELx.BT
14100  * @btype: PSTATE.BTYPE, and is non-zero
14101  *
14102  * On a guarded page, there are a limited number of insns
14103  * that may be present at the branch target:
14104  *   - branch target identifiers,
14105  *   - paciasp, pacibsp,
14106  *   - BRK insn
14107  *   - HLT insn
14108  * Anything else causes a Branch Target Exception.
14109  *
14110  * Return true if the branch is compatible, false to raise BTITRAP.
14111  */
14112 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
14113 {
14114     if ((insn & 0xfffff01fu) == 0xd503201fu) {
14115         /* HINT space */
14116         switch (extract32(insn, 5, 7)) {
14117         case 0b011001: /* PACIASP */
14118         case 0b011011: /* PACIBSP */
14119             /*
14120              * If SCTLR_ELx.BT, then PACI*SP are not compatible
14121              * with btype == 3.  Otherwise all btype are ok.
14122              */
14123             return !bt || btype != 3;
14124         case 0b100000: /* BTI */
14125             /* Not compatible with any btype.  */
14126             return false;
14127         case 0b100010: /* BTI c */
14128             /* Not compatible with btype == 3 */
14129             return btype != 3;
14130         case 0b100100: /* BTI j */
14131             /* Not compatible with btype == 2 */
14132             return btype != 2;
14133         case 0b100110: /* BTI jc */
14134             /* Compatible with any btype.  */
14135             return true;
14136         }
14137     } else {
14138         switch (insn & 0xffe0001fu) {
14139         case 0xd4200000u: /* BRK */
14140         case 0xd4400000u: /* HLT */
14141             /* Give priority to the breakpoint exception.  */
14142             return true;
14143         }
14144     }
14145     return false;
14146 }
14147 
14148 /* C3.1 A64 instruction index by encoding */
14149 static void disas_a64_legacy(DisasContext *s, uint32_t insn)
14150 {
14151     switch (extract32(insn, 25, 4)) {
14152     case 0xa: case 0xb: /* Branch, exception generation and system insns */
14153         disas_b_exc_sys(s, insn);
14154         break;
14155     case 0x4:
14156     case 0x6:
14157     case 0xc:
14158     case 0xe:      /* Loads and stores */
14159         disas_ldst(s, insn);
14160         break;
14161     case 0x5:
14162     case 0xd:      /* Data processing - register */
14163         disas_data_proc_reg(s, insn);
14164         break;
14165     case 0x7:
14166     case 0xf:      /* Data processing - SIMD and floating point */
14167         disas_data_proc_simd_fp(s, insn);
14168         break;
14169     default:
14170         unallocated_encoding(s);
14171         break;
14172     }
14173 }
14174 
14175 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14176                                           CPUState *cpu)
14177 {
14178     DisasContext *dc = container_of(dcbase, DisasContext, base);
14179     CPUARMState *env = cpu->env_ptr;
14180     ARMCPU *arm_cpu = env_archcpu(env);
14181     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
14182     int bound, core_mmu_idx;
14183 
14184     dc->isar = &arm_cpu->isar;
14185     dc->condjmp = 0;
14186     dc->pc_save = dc->base.pc_first;
14187     dc->aarch64 = true;
14188     dc->thumb = false;
14189     dc->sctlr_b = 0;
14190     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
14191     dc->condexec_mask = 0;
14192     dc->condexec_cond = 0;
14193     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
14194     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
14195     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
14196     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
14197     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
14198     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14199 #if !defined(CONFIG_USER_ONLY)
14200     dc->user = (dc->current_el == 0);
14201 #endif
14202     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
14203     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
14204     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
14205     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
14206     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
14207     dc->fgt_eret = EX_TBFLAG_A64(tb_flags, FGT_ERET);
14208     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
14209     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
14210     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
14211     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
14212     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
14213     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
14214     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
14215     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
14216     dc->ata = EX_TBFLAG_A64(tb_flags, ATA);
14217     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
14218     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
14219     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
14220     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
14221     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
14222     dc->naa = EX_TBFLAG_A64(tb_flags, NAA);
14223     dc->vec_len = 0;
14224     dc->vec_stride = 0;
14225     dc->cp_regs = arm_cpu->cp_regs;
14226     dc->features = env->features;
14227     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
14228 
14229 #ifdef CONFIG_USER_ONLY
14230     /* In sve_probe_page, we assume TBI is enabled. */
14231     tcg_debug_assert(dc->tbid & 1);
14232 #endif
14233 
14234     dc->lse2 = dc_isar_feature(aa64_lse2, dc);
14235 
14236     /* Single step state. The code-generation logic here is:
14237      *  SS_ACTIVE == 0:
14238      *   generate code with no special handling for single-stepping (except
14239      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14240      *   this happens anyway because those changes are all system register or
14241      *   PSTATE writes).
14242      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14243      *   emit code for one insn
14244      *   emit code to clear PSTATE.SS
14245      *   emit code to generate software step exception for completed step
14246      *   end TB (as usual for having generated an exception)
14247      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14248      *   emit code to generate a software step exception
14249      *   end the TB
14250      */
14251     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
14252     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
14253     dc->is_ldex = false;
14254 
14255     /* Bound the number of insns to execute to those left on the page.  */
14256     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14257 
14258     /* If architectural single step active, limit to 1.  */
14259     if (dc->ss_active) {
14260         bound = 1;
14261     }
14262     dc->base.max_insns = MIN(dc->base.max_insns, bound);
14263 }
14264 
14265 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14266 {
14267 }
14268 
14269 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14270 {
14271     DisasContext *dc = container_of(dcbase, DisasContext, base);
14272     target_ulong pc_arg = dc->base.pc_next;
14273 
14274     if (tb_cflags(dcbase->tb) & CF_PCREL) {
14275         pc_arg &= ~TARGET_PAGE_MASK;
14276     }
14277     tcg_gen_insn_start(pc_arg, 0, 0);
14278     dc->insn_start = tcg_last_op();
14279 }
14280 
14281 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14282 {
14283     DisasContext *s = container_of(dcbase, DisasContext, base);
14284     CPUARMState *env = cpu->env_ptr;
14285     uint64_t pc = s->base.pc_next;
14286     uint32_t insn;
14287 
14288     /* Singlestep exceptions have the highest priority. */
14289     if (s->ss_active && !s->pstate_ss) {
14290         /* Singlestep state is Active-pending.
14291          * If we're in this state at the start of a TB then either
14292          *  a) we just took an exception to an EL which is being debugged
14293          *     and this is the first insn in the exception handler
14294          *  b) debug exceptions were masked and we just unmasked them
14295          *     without changing EL (eg by clearing PSTATE.D)
14296          * In either case we're going to take a swstep exception in the
14297          * "did not step an insn" case, and so the syndrome ISV and EX
14298          * bits should be zero.
14299          */
14300         assert(s->base.num_insns == 1);
14301         gen_swstep_exception(s, 0, 0);
14302         s->base.is_jmp = DISAS_NORETURN;
14303         s->base.pc_next = pc + 4;
14304         return;
14305     }
14306 
14307     if (pc & 3) {
14308         /*
14309          * PC alignment fault.  This has priority over the instruction abort
14310          * that we would receive from a translation fault via arm_ldl_code.
14311          * This should only be possible after an indirect branch, at the
14312          * start of the TB.
14313          */
14314         assert(s->base.num_insns == 1);
14315         gen_helper_exception_pc_alignment(cpu_env, tcg_constant_tl(pc));
14316         s->base.is_jmp = DISAS_NORETURN;
14317         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
14318         return;
14319     }
14320 
14321     s->pc_curr = pc;
14322     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
14323     s->insn = insn;
14324     s->base.pc_next = pc + 4;
14325 
14326     s->fp_access_checked = false;
14327     s->sve_access_checked = false;
14328 
14329     if (s->pstate_il) {
14330         /*
14331          * Illegal execution state. This has priority over BTI
14332          * exceptions, but comes after instruction abort exceptions.
14333          */
14334         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
14335         return;
14336     }
14337 
14338     if (dc_isar_feature(aa64_bti, s)) {
14339         if (s->base.num_insns == 1) {
14340             /*
14341              * At the first insn of the TB, compute s->guarded_page.
14342              * We delayed computing this until successfully reading
14343              * the first insn of the TB, above.  This (mostly) ensures
14344              * that the softmmu tlb entry has been populated, and the
14345              * page table GP bit is available.
14346              *
14347              * Note that we need to compute this even if btype == 0,
14348              * because this value is used for BR instructions later
14349              * where ENV is not available.
14350              */
14351             s->guarded_page = is_guarded_page(env, s);
14352 
14353             /* First insn can have btype set to non-zero.  */
14354             tcg_debug_assert(s->btype >= 0);
14355 
14356             /*
14357              * Note that the Branch Target Exception has fairly high
14358              * priority -- below debugging exceptions but above most
14359              * everything else.  This allows us to handle this now
14360              * instead of waiting until the insn is otherwise decoded.
14361              */
14362             if (s->btype != 0
14363                 && s->guarded_page
14364                 && !btype_destination_ok(insn, s->bt, s->btype)) {
14365                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
14366                 return;
14367             }
14368         } else {
14369             /* Not the first insn: btype must be 0.  */
14370             tcg_debug_assert(s->btype == 0);
14371         }
14372     }
14373 
14374     s->is_nonstreaming = false;
14375     if (s->sme_trap_nonstreaming) {
14376         disas_sme_fa64(s, insn);
14377     }
14378 
14379     if (!disas_a64(s, insn) &&
14380         !disas_sme(s, insn) &&
14381         !disas_sve(s, insn)) {
14382         disas_a64_legacy(s, insn);
14383     }
14384 
14385     /*
14386      * After execution of most insns, btype is reset to 0.
14387      * Note that we set btype == -1 when the insn sets btype.
14388      */
14389     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14390         reset_btype(s);
14391     }
14392 }
14393 
14394 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14395 {
14396     DisasContext *dc = container_of(dcbase, DisasContext, base);
14397 
14398     if (unlikely(dc->ss_active)) {
14399         /* Note that this means single stepping WFI doesn't halt the CPU.
14400          * For conditional branch insns this is harmless unreachable code as
14401          * gen_goto_tb() has already handled emitting the debug exception
14402          * (and thus a tb-jump is not possible when singlestepping).
14403          */
14404         switch (dc->base.is_jmp) {
14405         default:
14406             gen_a64_update_pc(dc, 4);
14407             /* fall through */
14408         case DISAS_EXIT:
14409         case DISAS_JUMP:
14410             gen_step_complete_exception(dc);
14411             break;
14412         case DISAS_NORETURN:
14413             break;
14414         }
14415     } else {
14416         switch (dc->base.is_jmp) {
14417         case DISAS_NEXT:
14418         case DISAS_TOO_MANY:
14419             gen_goto_tb(dc, 1, 4);
14420             break;
14421         default:
14422         case DISAS_UPDATE_EXIT:
14423             gen_a64_update_pc(dc, 4);
14424             /* fall through */
14425         case DISAS_EXIT:
14426             tcg_gen_exit_tb(NULL, 0);
14427             break;
14428         case DISAS_UPDATE_NOCHAIN:
14429             gen_a64_update_pc(dc, 4);
14430             /* fall through */
14431         case DISAS_JUMP:
14432             tcg_gen_lookup_and_goto_ptr();
14433             break;
14434         case DISAS_NORETURN:
14435         case DISAS_SWI:
14436             break;
14437         case DISAS_WFE:
14438             gen_a64_update_pc(dc, 4);
14439             gen_helper_wfe(cpu_env);
14440             break;
14441         case DISAS_YIELD:
14442             gen_a64_update_pc(dc, 4);
14443             gen_helper_yield(cpu_env);
14444             break;
14445         case DISAS_WFI:
14446             /*
14447              * This is a special case because we don't want to just halt
14448              * the CPU if trying to debug across a WFI.
14449              */
14450             gen_a64_update_pc(dc, 4);
14451             gen_helper_wfi(cpu_env, tcg_constant_i32(4));
14452             /*
14453              * The helper doesn't necessarily throw an exception, but we
14454              * must go back to the main loop to check for interrupts anyway.
14455              */
14456             tcg_gen_exit_tb(NULL, 0);
14457             break;
14458         }
14459     }
14460 }
14461 
14462 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14463                                  CPUState *cpu, FILE *logfile)
14464 {
14465     DisasContext *dc = container_of(dcbase, DisasContext, base);
14466 
14467     fprintf(logfile, "IN: %s\n", lookup_symbol(dc->base.pc_first));
14468     target_disas(logfile, cpu, dc->base.pc_first, dc->base.tb->size);
14469 }
14470 
14471 const TranslatorOps aarch64_translator_ops = {
14472     .init_disas_context = aarch64_tr_init_disas_context,
14473     .tb_start           = aarch64_tr_tb_start,
14474     .insn_start         = aarch64_tr_insn_start,
14475     .translate_insn     = aarch64_tr_translate_insn,
14476     .tb_stop            = aarch64_tr_tb_stop,
14477     .disas_log          = aarch64_tr_disas_log,
14478 };
14479