xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision dd84028f)
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 "cpu.h"
22 #include "exec/exec-all.h"
23 #include "tcg/tcg-op.h"
24 #include "tcg/tcg-op-gvec.h"
25 #include "qemu/log.h"
26 #include "arm_ldst.h"
27 #include "translate.h"
28 #include "internals.h"
29 #include "qemu/host-utils.h"
30 #include "semihosting/semihost.h"
31 #include "exec/gen-icount.h"
32 #include "exec/helper-proto.h"
33 #include "exec/helper-gen.h"
34 #include "exec/log.h"
35 #include "cpregs.h"
36 #include "translate-a64.h"
37 #include "qemu/atomic128.h"
38 
39 static TCGv_i64 cpu_X[32];
40 static TCGv_i64 cpu_pc;
41 
42 /* Load/store exclusive handling */
43 static TCGv_i64 cpu_exclusive_high;
44 
45 static const char *regnames[] = {
46     "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
47     "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
48     "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
49     "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
50 };
51 
52 enum a64_shift_type {
53     A64_SHIFT_TYPE_LSL = 0,
54     A64_SHIFT_TYPE_LSR = 1,
55     A64_SHIFT_TYPE_ASR = 2,
56     A64_SHIFT_TYPE_ROR = 3
57 };
58 
59 /* Table based decoder typedefs - used when the relevant bits for decode
60  * are too awkwardly scattered across the instruction (eg SIMD).
61  */
62 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
63 
64 typedef struct AArch64DecodeTable {
65     uint32_t pattern;
66     uint32_t mask;
67     AArch64DecodeFn *disas_fn;
68 } AArch64DecodeTable;
69 
70 /* initialize TCG globals.  */
71 void a64_translate_init(void)
72 {
73     int i;
74 
75     cpu_pc = tcg_global_mem_new_i64(cpu_env,
76                                     offsetof(CPUARMState, pc),
77                                     "pc");
78     for (i = 0; i < 32; i++) {
79         cpu_X[i] = tcg_global_mem_new_i64(cpu_env,
80                                           offsetof(CPUARMState, xregs[i]),
81                                           regnames[i]);
82     }
83 
84     cpu_exclusive_high = tcg_global_mem_new_i64(cpu_env,
85         offsetof(CPUARMState, exclusive_high), "exclusive_high");
86 }
87 
88 /*
89  * Return the core mmu_idx to use for A64 "unprivileged load/store" insns
90  */
91 static int get_a64_user_mem_index(DisasContext *s)
92 {
93     /*
94      * If AccType_UNPRIV is not used, the insn uses AccType_NORMAL,
95      * which is the usual mmu_idx for this cpu state.
96      */
97     ARMMMUIdx useridx = s->mmu_idx;
98 
99     if (s->unpriv) {
100         /*
101          * We have pre-computed the condition for AccType_UNPRIV.
102          * Therefore we should never get here with a mmu_idx for
103          * which we do not know the corresponding user mmu_idx.
104          */
105         switch (useridx) {
106         case ARMMMUIdx_E10_1:
107         case ARMMMUIdx_E10_1_PAN:
108             useridx = ARMMMUIdx_E10_0;
109             break;
110         case ARMMMUIdx_E20_2:
111         case ARMMMUIdx_E20_2_PAN:
112             useridx = ARMMMUIdx_E20_0;
113             break;
114         default:
115             g_assert_not_reached();
116         }
117     }
118     return arm_to_core_mmu_idx(useridx);
119 }
120 
121 static void set_btype_raw(int val)
122 {
123     tcg_gen_st_i32(tcg_constant_i32(val), cpu_env,
124                    offsetof(CPUARMState, btype));
125 }
126 
127 static void set_btype(DisasContext *s, int val)
128 {
129     /* BTYPE is a 2-bit field, and 0 should be done with reset_btype.  */
130     tcg_debug_assert(val >= 1 && val <= 3);
131     set_btype_raw(val);
132     s->btype = -1;
133 }
134 
135 static void reset_btype(DisasContext *s)
136 {
137     if (s->btype != 0) {
138         set_btype_raw(0);
139         s->btype = 0;
140     }
141 }
142 
143 static void gen_pc_plus_diff(DisasContext *s, TCGv_i64 dest, target_long diff)
144 {
145     assert(s->pc_save != -1);
146     if (tb_cflags(s->base.tb) & CF_PCREL) {
147         tcg_gen_addi_i64(dest, cpu_pc, (s->pc_curr - s->pc_save) + diff);
148     } else {
149         tcg_gen_movi_i64(dest, s->pc_curr + diff);
150     }
151 }
152 
153 void gen_a64_update_pc(DisasContext *s, target_long diff)
154 {
155     gen_pc_plus_diff(s, cpu_pc, diff);
156     s->pc_save = s->pc_curr + diff;
157 }
158 
159 /*
160  * Handle Top Byte Ignore (TBI) bits.
161  *
162  * If address tagging is enabled via the TCR TBI bits:
163  *  + for EL2 and EL3 there is only one TBI bit, and if it is set
164  *    then the address is zero-extended, clearing bits [63:56]
165  *  + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
166  *    and TBI1 controls addressses with bit 55 == 1.
167  *    If the appropriate TBI bit is set for the address then
168  *    the address is sign-extended from bit 55 into bits [63:56]
169  *
170  * Here We have concatenated TBI{1,0} into tbi.
171  */
172 static void gen_top_byte_ignore(DisasContext *s, TCGv_i64 dst,
173                                 TCGv_i64 src, int tbi)
174 {
175     if (tbi == 0) {
176         /* Load unmodified address */
177         tcg_gen_mov_i64(dst, src);
178     } else if (!regime_has_2_ranges(s->mmu_idx)) {
179         /* Force tag byte to all zero */
180         tcg_gen_extract_i64(dst, src, 0, 56);
181     } else {
182         /* Sign-extend from bit 55.  */
183         tcg_gen_sextract_i64(dst, src, 0, 56);
184 
185         switch (tbi) {
186         case 1:
187             /* tbi0 but !tbi1: only use the extension if positive */
188             tcg_gen_and_i64(dst, dst, src);
189             break;
190         case 2:
191             /* !tbi0 but tbi1: only use the extension if negative */
192             tcg_gen_or_i64(dst, dst, src);
193             break;
194         case 3:
195             /* tbi0 and tbi1: always use the extension */
196             break;
197         default:
198             g_assert_not_reached();
199         }
200     }
201 }
202 
203 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
204 {
205     /*
206      * If address tagging is enabled for instructions via the TCR TBI bits,
207      * then loading an address into the PC will clear out any tag.
208      */
209     gen_top_byte_ignore(s, cpu_pc, src, s->tbii);
210     s->pc_save = -1;
211 }
212 
213 /*
214  * Handle MTE and/or TBI.
215  *
216  * For TBI, ideally, we would do nothing.  Proper behaviour on fault is
217  * for the tag to be present in the FAR_ELx register.  But for user-only
218  * mode we do not have a TLB with which to implement this, so we must
219  * remove the top byte now.
220  *
221  * Always return a fresh temporary that we can increment independently
222  * of the write-back address.
223  */
224 
225 TCGv_i64 clean_data_tbi(DisasContext *s, TCGv_i64 addr)
226 {
227     TCGv_i64 clean = tcg_temp_new_i64();
228 #ifdef CONFIG_USER_ONLY
229     gen_top_byte_ignore(s, clean, addr, s->tbid);
230 #else
231     tcg_gen_mov_i64(clean, addr);
232 #endif
233     return clean;
234 }
235 
236 /* Insert a zero tag into src, with the result at dst. */
237 static void gen_address_with_allocation_tag0(TCGv_i64 dst, TCGv_i64 src)
238 {
239     tcg_gen_andi_i64(dst, src, ~MAKE_64BIT_MASK(56, 4));
240 }
241 
242 static void gen_probe_access(DisasContext *s, TCGv_i64 ptr,
243                              MMUAccessType acc, int log2_size)
244 {
245     gen_helper_probe_access(cpu_env, ptr,
246                             tcg_constant_i32(acc),
247                             tcg_constant_i32(get_mem_index(s)),
248                             tcg_constant_i32(1 << log2_size));
249 }
250 
251 /*
252  * For MTE, check a single logical or atomic access.  This probes a single
253  * address, the exact one specified.  The size and alignment of the access
254  * is not relevant to MTE, per se, but watchpoints do require the size,
255  * and we want to recognize those before making any other changes to state.
256  */
257 static TCGv_i64 gen_mte_check1_mmuidx(DisasContext *s, TCGv_i64 addr,
258                                       bool is_write, bool tag_checked,
259                                       int log2_size, bool is_unpriv,
260                                       int core_idx)
261 {
262     if (tag_checked && s->mte_active[is_unpriv]) {
263         TCGv_i64 ret;
264         int desc = 0;
265 
266         desc = FIELD_DP32(desc, MTEDESC, MIDX, core_idx);
267         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
268         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
269         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
270         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, (1 << log2_size) - 1);
271 
272         ret = tcg_temp_new_i64();
273         gen_helper_mte_check(ret, cpu_env, tcg_constant_i32(desc), addr);
274 
275         return ret;
276     }
277     return clean_data_tbi(s, addr);
278 }
279 
280 TCGv_i64 gen_mte_check1(DisasContext *s, TCGv_i64 addr, bool is_write,
281                         bool tag_checked, int log2_size)
282 {
283     return gen_mte_check1_mmuidx(s, addr, is_write, tag_checked, log2_size,
284                                  false, get_mem_index(s));
285 }
286 
287 /*
288  * For MTE, check multiple logical sequential accesses.
289  */
290 TCGv_i64 gen_mte_checkN(DisasContext *s, TCGv_i64 addr, bool is_write,
291                         bool tag_checked, int size)
292 {
293     if (tag_checked && s->mte_active[0]) {
294         TCGv_i64 ret;
295         int desc = 0;
296 
297         desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
298         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
299         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
300         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
301         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, size - 1);
302 
303         ret = tcg_temp_new_i64();
304         gen_helper_mte_check(ret, cpu_env, tcg_constant_i32(desc), addr);
305 
306         return ret;
307     }
308     return clean_data_tbi(s, addr);
309 }
310 
311 typedef struct DisasCompare64 {
312     TCGCond cond;
313     TCGv_i64 value;
314 } DisasCompare64;
315 
316 static void a64_test_cc(DisasCompare64 *c64, int cc)
317 {
318     DisasCompare c32;
319 
320     arm_test_cc(&c32, cc);
321 
322     /*
323      * Sign-extend the 32-bit value so that the GE/LT comparisons work
324      * properly.  The NE/EQ comparisons are also fine with this choice.
325       */
326     c64->cond = c32.cond;
327     c64->value = tcg_temp_new_i64();
328     tcg_gen_ext_i32_i64(c64->value, c32.value);
329 }
330 
331 static void gen_rebuild_hflags(DisasContext *s)
332 {
333     gen_helper_rebuild_hflags_a64(cpu_env, tcg_constant_i32(s->current_el));
334 }
335 
336 static void gen_exception_internal(int excp)
337 {
338     assert(excp_is_internal(excp));
339     gen_helper_exception_internal(cpu_env, tcg_constant_i32(excp));
340 }
341 
342 static void gen_exception_internal_insn(DisasContext *s, int excp)
343 {
344     gen_a64_update_pc(s, 0);
345     gen_exception_internal(excp);
346     s->base.is_jmp = DISAS_NORETURN;
347 }
348 
349 static void gen_exception_bkpt_insn(DisasContext *s, uint32_t syndrome)
350 {
351     gen_a64_update_pc(s, 0);
352     gen_helper_exception_bkpt_insn(cpu_env, tcg_constant_i32(syndrome));
353     s->base.is_jmp = DISAS_NORETURN;
354 }
355 
356 static void gen_step_complete_exception(DisasContext *s)
357 {
358     /* We just completed step of an insn. Move from Active-not-pending
359      * to Active-pending, and then also take the swstep exception.
360      * This corresponds to making the (IMPDEF) choice to prioritize
361      * swstep exceptions over asynchronous exceptions taken to an exception
362      * level where debug is disabled. This choice has the advantage that
363      * we do not need to maintain internal state corresponding to the
364      * ISV/EX syndrome bits between completion of the step and generation
365      * of the exception, and our syndrome information is always correct.
366      */
367     gen_ss_advance(s);
368     gen_swstep_exception(s, 1, s->is_ldex);
369     s->base.is_jmp = DISAS_NORETURN;
370 }
371 
372 static inline bool use_goto_tb(DisasContext *s, uint64_t dest)
373 {
374     if (s->ss_active) {
375         return false;
376     }
377     return translator_use_goto_tb(&s->base, dest);
378 }
379 
380 static void gen_goto_tb(DisasContext *s, int n, int64_t diff)
381 {
382     if (use_goto_tb(s, s->pc_curr + diff)) {
383         /*
384          * For pcrel, the pc must always be up-to-date on entry to
385          * the linked TB, so that it can use simple additions for all
386          * further adjustments.  For !pcrel, the linked TB is compiled
387          * to know its full virtual address, so we can delay the
388          * update to pc to the unlinked path.  A long chain of links
389          * can thus avoid many updates to the PC.
390          */
391         if (tb_cflags(s->base.tb) & CF_PCREL) {
392             gen_a64_update_pc(s, diff);
393             tcg_gen_goto_tb(n);
394         } else {
395             tcg_gen_goto_tb(n);
396             gen_a64_update_pc(s, diff);
397         }
398         tcg_gen_exit_tb(s->base.tb, n);
399         s->base.is_jmp = DISAS_NORETURN;
400     } else {
401         gen_a64_update_pc(s, diff);
402         if (s->ss_active) {
403             gen_step_complete_exception(s);
404         } else {
405             tcg_gen_lookup_and_goto_ptr();
406             s->base.is_jmp = DISAS_NORETURN;
407         }
408     }
409 }
410 
411 /*
412  * Register access functions
413  *
414  * These functions are used for directly accessing a register in where
415  * changes to the final register value are likely to be made. If you
416  * need to use a register for temporary calculation (e.g. index type
417  * operations) use the read_* form.
418  *
419  * B1.2.1 Register mappings
420  *
421  * In instruction register encoding 31 can refer to ZR (zero register) or
422  * the SP (stack pointer) depending on context. In QEMU's case we map SP
423  * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
424  * This is the point of the _sp forms.
425  */
426 TCGv_i64 cpu_reg(DisasContext *s, int reg)
427 {
428     if (reg == 31) {
429         TCGv_i64 t = tcg_temp_new_i64();
430         tcg_gen_movi_i64(t, 0);
431         return t;
432     } else {
433         return cpu_X[reg];
434     }
435 }
436 
437 /* register access for when 31 == SP */
438 TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
439 {
440     return cpu_X[reg];
441 }
442 
443 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
444  * representing the register contents. This TCGv is an auto-freed
445  * temporary so it need not be explicitly freed, and may be modified.
446  */
447 TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
448 {
449     TCGv_i64 v = tcg_temp_new_i64();
450     if (reg != 31) {
451         if (sf) {
452             tcg_gen_mov_i64(v, cpu_X[reg]);
453         } else {
454             tcg_gen_ext32u_i64(v, cpu_X[reg]);
455         }
456     } else {
457         tcg_gen_movi_i64(v, 0);
458     }
459     return v;
460 }
461 
462 TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
463 {
464     TCGv_i64 v = tcg_temp_new_i64();
465     if (sf) {
466         tcg_gen_mov_i64(v, cpu_X[reg]);
467     } else {
468         tcg_gen_ext32u_i64(v, cpu_X[reg]);
469     }
470     return v;
471 }
472 
473 /* Return the offset into CPUARMState of a slice (from
474  * the least significant end) of FP register Qn (ie
475  * Dn, Sn, Hn or Bn).
476  * (Note that this is not the same mapping as for A32; see cpu.h)
477  */
478 static inline int fp_reg_offset(DisasContext *s, int regno, MemOp size)
479 {
480     return vec_reg_offset(s, regno, 0, size);
481 }
482 
483 /* Offset of the high half of the 128 bit vector Qn */
484 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
485 {
486     return vec_reg_offset(s, regno, 1, MO_64);
487 }
488 
489 /* Convenience accessors for reading and writing single and double
490  * FP registers. Writing clears the upper parts of the associated
491  * 128 bit vector register, as required by the architecture.
492  * Note that unlike the GP register accessors, the values returned
493  * by the read functions must be manually freed.
494  */
495 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
496 {
497     TCGv_i64 v = tcg_temp_new_i64();
498 
499     tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
500     return v;
501 }
502 
503 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
504 {
505     TCGv_i32 v = tcg_temp_new_i32();
506 
507     tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
508     return v;
509 }
510 
511 static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
512 {
513     TCGv_i32 v = tcg_temp_new_i32();
514 
515     tcg_gen_ld16u_i32(v, cpu_env, fp_reg_offset(s, reg, MO_16));
516     return v;
517 }
518 
519 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
520  * If SVE is not enabled, then there are only 128 bits in the vector.
521  */
522 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
523 {
524     unsigned ofs = fp_reg_offset(s, rd, MO_64);
525     unsigned vsz = vec_full_reg_size(s);
526 
527     /* Nop move, with side effect of clearing the tail. */
528     tcg_gen_gvec_mov(MO_64, ofs, ofs, is_q ? 16 : 8, vsz);
529 }
530 
531 void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
532 {
533     unsigned ofs = fp_reg_offset(s, reg, MO_64);
534 
535     tcg_gen_st_i64(v, cpu_env, ofs);
536     clear_vec_high(s, false, reg);
537 }
538 
539 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
540 {
541     TCGv_i64 tmp = tcg_temp_new_i64();
542 
543     tcg_gen_extu_i32_i64(tmp, v);
544     write_fp_dreg(s, reg, tmp);
545 }
546 
547 /* Expand a 2-operand AdvSIMD vector operation using an expander function.  */
548 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
549                          GVecGen2Fn *gvec_fn, int vece)
550 {
551     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
552             is_q ? 16 : 8, vec_full_reg_size(s));
553 }
554 
555 /* Expand a 2-operand + immediate AdvSIMD vector operation using
556  * an expander function.
557  */
558 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
559                           int64_t imm, GVecGen2iFn *gvec_fn, int vece)
560 {
561     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
562             imm, is_q ? 16 : 8, vec_full_reg_size(s));
563 }
564 
565 /* Expand a 3-operand AdvSIMD vector operation using an expander function.  */
566 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
567                          GVecGen3Fn *gvec_fn, int vece)
568 {
569     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
570             vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
571 }
572 
573 /* Expand a 4-operand AdvSIMD vector operation using an expander function.  */
574 static void gen_gvec_fn4(DisasContext *s, bool is_q, int rd, int rn, int rm,
575                          int rx, GVecGen4Fn *gvec_fn, int vece)
576 {
577     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
578             vec_full_reg_offset(s, rm), vec_full_reg_offset(s, rx),
579             is_q ? 16 : 8, vec_full_reg_size(s));
580 }
581 
582 /* Expand a 2-operand operation using an out-of-line helper.  */
583 static void gen_gvec_op2_ool(DisasContext *s, bool is_q, int rd,
584                              int rn, int data, gen_helper_gvec_2 *fn)
585 {
586     tcg_gen_gvec_2_ool(vec_full_reg_offset(s, rd),
587                        vec_full_reg_offset(s, rn),
588                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
589 }
590 
591 /* Expand a 3-operand operation using an out-of-line helper.  */
592 static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
593                              int rn, int rm, int data, gen_helper_gvec_3 *fn)
594 {
595     tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
596                        vec_full_reg_offset(s, rn),
597                        vec_full_reg_offset(s, rm),
598                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
599 }
600 
601 /* Expand a 3-operand + fpstatus pointer + simd data value operation using
602  * an out-of-line helper.
603  */
604 static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
605                               int rm, bool is_fp16, int data,
606                               gen_helper_gvec_3_ptr *fn)
607 {
608     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
609     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
610                        vec_full_reg_offset(s, rn),
611                        vec_full_reg_offset(s, rm), fpst,
612                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
613 }
614 
615 /* Expand a 3-operand + qc + operation using an out-of-line helper.  */
616 static void gen_gvec_op3_qc(DisasContext *s, bool is_q, int rd, int rn,
617                             int rm, gen_helper_gvec_3_ptr *fn)
618 {
619     TCGv_ptr qc_ptr = tcg_temp_new_ptr();
620 
621     tcg_gen_addi_ptr(qc_ptr, cpu_env, offsetof(CPUARMState, vfp.qc));
622     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
623                        vec_full_reg_offset(s, rn),
624                        vec_full_reg_offset(s, rm), qc_ptr,
625                        is_q ? 16 : 8, vec_full_reg_size(s), 0, fn);
626 }
627 
628 /* Expand a 4-operand operation using an out-of-line helper.  */
629 static void gen_gvec_op4_ool(DisasContext *s, bool is_q, int rd, int rn,
630                              int rm, int ra, int data, gen_helper_gvec_4 *fn)
631 {
632     tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
633                        vec_full_reg_offset(s, rn),
634                        vec_full_reg_offset(s, rm),
635                        vec_full_reg_offset(s, ra),
636                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
637 }
638 
639 /*
640  * Expand a 4-operand + fpstatus pointer + simd data value operation using
641  * an out-of-line helper.
642  */
643 static void gen_gvec_op4_fpst(DisasContext *s, bool is_q, int rd, int rn,
644                               int rm, int ra, bool is_fp16, int data,
645                               gen_helper_gvec_4_ptr *fn)
646 {
647     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
648     tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
649                        vec_full_reg_offset(s, rn),
650                        vec_full_reg_offset(s, rm),
651                        vec_full_reg_offset(s, ra), fpst,
652                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
653 }
654 
655 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
656  * than the 32 bit equivalent.
657  */
658 static inline void gen_set_NZ64(TCGv_i64 result)
659 {
660     tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
661     tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
662 }
663 
664 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
665 static inline void gen_logic_CC(int sf, TCGv_i64 result)
666 {
667     if (sf) {
668         gen_set_NZ64(result);
669     } else {
670         tcg_gen_extrl_i64_i32(cpu_ZF, result);
671         tcg_gen_mov_i32(cpu_NF, cpu_ZF);
672     }
673     tcg_gen_movi_i32(cpu_CF, 0);
674     tcg_gen_movi_i32(cpu_VF, 0);
675 }
676 
677 /* dest = T0 + T1; compute C, N, V and Z flags */
678 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
679 {
680     if (sf) {
681         TCGv_i64 result, flag, tmp;
682         result = tcg_temp_new_i64();
683         flag = tcg_temp_new_i64();
684         tmp = tcg_temp_new_i64();
685 
686         tcg_gen_movi_i64(tmp, 0);
687         tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
688 
689         tcg_gen_extrl_i64_i32(cpu_CF, flag);
690 
691         gen_set_NZ64(result);
692 
693         tcg_gen_xor_i64(flag, result, t0);
694         tcg_gen_xor_i64(tmp, t0, t1);
695         tcg_gen_andc_i64(flag, flag, tmp);
696         tcg_gen_extrh_i64_i32(cpu_VF, flag);
697 
698         tcg_gen_mov_i64(dest, result);
699     } else {
700         /* 32 bit arithmetic */
701         TCGv_i32 t0_32 = tcg_temp_new_i32();
702         TCGv_i32 t1_32 = tcg_temp_new_i32();
703         TCGv_i32 tmp = tcg_temp_new_i32();
704 
705         tcg_gen_movi_i32(tmp, 0);
706         tcg_gen_extrl_i64_i32(t0_32, t0);
707         tcg_gen_extrl_i64_i32(t1_32, t1);
708         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
709         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
710         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
711         tcg_gen_xor_i32(tmp, t0_32, t1_32);
712         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
713         tcg_gen_extu_i32_i64(dest, cpu_NF);
714     }
715 }
716 
717 /* dest = T0 - T1; compute C, N, V and Z flags */
718 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
719 {
720     if (sf) {
721         /* 64 bit arithmetic */
722         TCGv_i64 result, flag, tmp;
723 
724         result = tcg_temp_new_i64();
725         flag = tcg_temp_new_i64();
726         tcg_gen_sub_i64(result, t0, t1);
727 
728         gen_set_NZ64(result);
729 
730         tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
731         tcg_gen_extrl_i64_i32(cpu_CF, flag);
732 
733         tcg_gen_xor_i64(flag, result, t0);
734         tmp = tcg_temp_new_i64();
735         tcg_gen_xor_i64(tmp, t0, t1);
736         tcg_gen_and_i64(flag, flag, tmp);
737         tcg_gen_extrh_i64_i32(cpu_VF, flag);
738         tcg_gen_mov_i64(dest, result);
739     } else {
740         /* 32 bit arithmetic */
741         TCGv_i32 t0_32 = tcg_temp_new_i32();
742         TCGv_i32 t1_32 = tcg_temp_new_i32();
743         TCGv_i32 tmp;
744 
745         tcg_gen_extrl_i64_i32(t0_32, t0);
746         tcg_gen_extrl_i64_i32(t1_32, t1);
747         tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
748         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
749         tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
750         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
751         tmp = tcg_temp_new_i32();
752         tcg_gen_xor_i32(tmp, t0_32, t1_32);
753         tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
754         tcg_gen_extu_i32_i64(dest, cpu_NF);
755     }
756 }
757 
758 /* dest = T0 + T1 + CF; do not compute flags. */
759 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
760 {
761     TCGv_i64 flag = tcg_temp_new_i64();
762     tcg_gen_extu_i32_i64(flag, cpu_CF);
763     tcg_gen_add_i64(dest, t0, t1);
764     tcg_gen_add_i64(dest, dest, flag);
765 
766     if (!sf) {
767         tcg_gen_ext32u_i64(dest, dest);
768     }
769 }
770 
771 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
772 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
773 {
774     if (sf) {
775         TCGv_i64 result = tcg_temp_new_i64();
776         TCGv_i64 cf_64 = tcg_temp_new_i64();
777         TCGv_i64 vf_64 = tcg_temp_new_i64();
778         TCGv_i64 tmp = tcg_temp_new_i64();
779         TCGv_i64 zero = tcg_constant_i64(0);
780 
781         tcg_gen_extu_i32_i64(cf_64, cpu_CF);
782         tcg_gen_add2_i64(result, cf_64, t0, zero, cf_64, zero);
783         tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, zero);
784         tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
785         gen_set_NZ64(result);
786 
787         tcg_gen_xor_i64(vf_64, result, t0);
788         tcg_gen_xor_i64(tmp, t0, t1);
789         tcg_gen_andc_i64(vf_64, vf_64, tmp);
790         tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
791 
792         tcg_gen_mov_i64(dest, result);
793     } else {
794         TCGv_i32 t0_32 = tcg_temp_new_i32();
795         TCGv_i32 t1_32 = tcg_temp_new_i32();
796         TCGv_i32 tmp = tcg_temp_new_i32();
797         TCGv_i32 zero = tcg_constant_i32(0);
798 
799         tcg_gen_extrl_i64_i32(t0_32, t0);
800         tcg_gen_extrl_i64_i32(t1_32, t1);
801         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, zero, cpu_CF, zero);
802         tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, zero);
803 
804         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
805         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
806         tcg_gen_xor_i32(tmp, t0_32, t1_32);
807         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
808         tcg_gen_extu_i32_i64(dest, cpu_NF);
809     }
810 }
811 
812 /*
813  * Load/Store generators
814  */
815 
816 /*
817  * Store from GPR register to memory.
818  */
819 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
820                              TCGv_i64 tcg_addr, MemOp memop, int memidx,
821                              bool iss_valid,
822                              unsigned int iss_srt,
823                              bool iss_sf, bool iss_ar)
824 {
825     memop = finalize_memop(s, memop);
826     tcg_gen_qemu_st_i64(source, tcg_addr, memidx, memop);
827 
828     if (iss_valid) {
829         uint32_t syn;
830 
831         syn = syn_data_abort_with_iss(0,
832                                       (memop & MO_SIZE),
833                                       false,
834                                       iss_srt,
835                                       iss_sf,
836                                       iss_ar,
837                                       0, 0, 0, 0, 0, false);
838         disas_set_insn_syndrome(s, syn);
839     }
840 }
841 
842 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
843                       TCGv_i64 tcg_addr, MemOp memop,
844                       bool iss_valid,
845                       unsigned int iss_srt,
846                       bool iss_sf, bool iss_ar)
847 {
848     do_gpr_st_memidx(s, source, tcg_addr, memop, get_mem_index(s),
849                      iss_valid, iss_srt, iss_sf, iss_ar);
850 }
851 
852 /*
853  * Load from memory to GPR register
854  */
855 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
856                              MemOp memop, bool extend, int memidx,
857                              bool iss_valid, unsigned int iss_srt,
858                              bool iss_sf, bool iss_ar)
859 {
860     memop = finalize_memop(s, memop);
861     tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
862 
863     if (extend && (memop & MO_SIGN)) {
864         g_assert((memop & MO_SIZE) <= MO_32);
865         tcg_gen_ext32u_i64(dest, dest);
866     }
867 
868     if (iss_valid) {
869         uint32_t syn;
870 
871         syn = syn_data_abort_with_iss(0,
872                                       (memop & MO_SIZE),
873                                       (memop & MO_SIGN) != 0,
874                                       iss_srt,
875                                       iss_sf,
876                                       iss_ar,
877                                       0, 0, 0, 0, 0, false);
878         disas_set_insn_syndrome(s, syn);
879     }
880 }
881 
882 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
883                       MemOp memop, bool extend,
884                       bool iss_valid, unsigned int iss_srt,
885                       bool iss_sf, bool iss_ar)
886 {
887     do_gpr_ld_memidx(s, dest, tcg_addr, memop, extend, get_mem_index(s),
888                      iss_valid, iss_srt, iss_sf, iss_ar);
889 }
890 
891 /*
892  * Store from FP register to memory
893  */
894 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
895 {
896     /* This writes the bottom N bits of a 128 bit wide vector to memory */
897     TCGv_i64 tmplo = tcg_temp_new_i64();
898     MemOp mop;
899 
900     tcg_gen_ld_i64(tmplo, cpu_env, fp_reg_offset(s, srcidx, MO_64));
901 
902     if (size < 4) {
903         mop = finalize_memop(s, size);
904         tcg_gen_qemu_st_i64(tmplo, tcg_addr, get_mem_index(s), mop);
905     } else {
906         bool be = s->be_data == MO_BE;
907         TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
908         TCGv_i64 tmphi = tcg_temp_new_i64();
909 
910         tcg_gen_ld_i64(tmphi, cpu_env, fp_reg_hi_offset(s, srcidx));
911 
912         mop = s->be_data | MO_UQ;
913         tcg_gen_qemu_st_i64(be ? tmphi : tmplo, tcg_addr, get_mem_index(s),
914                             mop | (s->align_mem ? MO_ALIGN_16 : 0));
915         tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
916         tcg_gen_qemu_st_i64(be ? tmplo : tmphi, tcg_hiaddr,
917                             get_mem_index(s), mop);
918     }
919 }
920 
921 /*
922  * Load from memory to FP register
923  */
924 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
925 {
926     /* This always zero-extends and writes to a full 128 bit wide vector */
927     TCGv_i64 tmplo = tcg_temp_new_i64();
928     TCGv_i64 tmphi = NULL;
929     MemOp mop;
930 
931     if (size < 4) {
932         mop = finalize_memop(s, size);
933         tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), mop);
934     } else {
935         bool be = s->be_data == MO_BE;
936         TCGv_i64 tcg_hiaddr;
937 
938         tmphi = tcg_temp_new_i64();
939         tcg_hiaddr = tcg_temp_new_i64();
940 
941         mop = s->be_data | MO_UQ;
942         tcg_gen_qemu_ld_i64(be ? tmphi : tmplo, tcg_addr, get_mem_index(s),
943                             mop | (s->align_mem ? MO_ALIGN_16 : 0));
944         tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
945         tcg_gen_qemu_ld_i64(be ? tmplo : tmphi, tcg_hiaddr,
946                             get_mem_index(s), mop);
947     }
948 
949     tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
950 
951     if (tmphi) {
952         tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
953     }
954     clear_vec_high(s, tmphi != NULL, destidx);
955 }
956 
957 /*
958  * Vector load/store helpers.
959  *
960  * The principal difference between this and a FP load is that we don't
961  * zero extend as we are filling a partial chunk of the vector register.
962  * These functions don't support 128 bit loads/stores, which would be
963  * normal load/store operations.
964  *
965  * The _i32 versions are useful when operating on 32 bit quantities
966  * (eg for floating point single or using Neon helper functions).
967  */
968 
969 /* Get value of an element within a vector register */
970 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
971                              int element, MemOp memop)
972 {
973     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
974     switch ((unsigned)memop) {
975     case MO_8:
976         tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
977         break;
978     case MO_16:
979         tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
980         break;
981     case MO_32:
982         tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
983         break;
984     case MO_8|MO_SIGN:
985         tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
986         break;
987     case MO_16|MO_SIGN:
988         tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
989         break;
990     case MO_32|MO_SIGN:
991         tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
992         break;
993     case MO_64:
994     case MO_64|MO_SIGN:
995         tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
996         break;
997     default:
998         g_assert_not_reached();
999     }
1000 }
1001 
1002 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1003                                  int element, MemOp memop)
1004 {
1005     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1006     switch (memop) {
1007     case MO_8:
1008         tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1009         break;
1010     case MO_16:
1011         tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1012         break;
1013     case MO_8|MO_SIGN:
1014         tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1015         break;
1016     case MO_16|MO_SIGN:
1017         tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1018         break;
1019     case MO_32:
1020     case MO_32|MO_SIGN:
1021         tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1022         break;
1023     default:
1024         g_assert_not_reached();
1025     }
1026 }
1027 
1028 /* Set value of an element within a vector register */
1029 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1030                               int element, MemOp memop)
1031 {
1032     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1033     switch (memop) {
1034     case MO_8:
1035         tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1036         break;
1037     case MO_16:
1038         tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1039         break;
1040     case MO_32:
1041         tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1042         break;
1043     case MO_64:
1044         tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1045         break;
1046     default:
1047         g_assert_not_reached();
1048     }
1049 }
1050 
1051 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1052                                   int destidx, int element, MemOp memop)
1053 {
1054     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1055     switch (memop) {
1056     case MO_8:
1057         tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1058         break;
1059     case MO_16:
1060         tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1061         break;
1062     case MO_32:
1063         tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1064         break;
1065     default:
1066         g_assert_not_reached();
1067     }
1068 }
1069 
1070 /* Store from vector register to memory */
1071 static void do_vec_st(DisasContext *s, int srcidx, int element,
1072                       TCGv_i64 tcg_addr, MemOp mop)
1073 {
1074     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1075 
1076     read_vec_element(s, tcg_tmp, srcidx, element, mop & MO_SIZE);
1077     tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1078 }
1079 
1080 /* Load from memory to vector register */
1081 static void do_vec_ld(DisasContext *s, int destidx, int element,
1082                       TCGv_i64 tcg_addr, MemOp mop)
1083 {
1084     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1085 
1086     tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1087     write_vec_element(s, tcg_tmp, destidx, element, mop & MO_SIZE);
1088 }
1089 
1090 /* Check that FP/Neon access is enabled. If it is, return
1091  * true. If not, emit code to generate an appropriate exception,
1092  * and return false; the caller should not emit any code for
1093  * the instruction. Note that this check must happen after all
1094  * unallocated-encoding checks (otherwise the syndrome information
1095  * for the resulting exception will be incorrect).
1096  */
1097 static bool fp_access_check_only(DisasContext *s)
1098 {
1099     if (s->fp_excp_el) {
1100         assert(!s->fp_access_checked);
1101         s->fp_access_checked = true;
1102 
1103         gen_exception_insn_el(s, 0, EXCP_UDEF,
1104                               syn_fp_access_trap(1, 0xe, false, 0),
1105                               s->fp_excp_el);
1106         return false;
1107     }
1108     s->fp_access_checked = true;
1109     return true;
1110 }
1111 
1112 static bool fp_access_check(DisasContext *s)
1113 {
1114     if (!fp_access_check_only(s)) {
1115         return false;
1116     }
1117     if (s->sme_trap_nonstreaming && s->is_nonstreaming) {
1118         gen_exception_insn(s, 0, EXCP_UDEF,
1119                            syn_smetrap(SME_ET_Streaming, false));
1120         return false;
1121     }
1122     return true;
1123 }
1124 
1125 /*
1126  * Check that SVE access is enabled.  If it is, return true.
1127  * If not, emit code to generate an appropriate exception and return false.
1128  * This function corresponds to CheckSVEEnabled().
1129  */
1130 bool sve_access_check(DisasContext *s)
1131 {
1132     if (s->pstate_sm || !dc_isar_feature(aa64_sve, s)) {
1133         assert(dc_isar_feature(aa64_sme, s));
1134         if (!sme_sm_enabled_check(s)) {
1135             goto fail_exit;
1136         }
1137     } else if (s->sve_excp_el) {
1138         gen_exception_insn_el(s, 0, EXCP_UDEF,
1139                               syn_sve_access_trap(), s->sve_excp_el);
1140         goto fail_exit;
1141     }
1142     s->sve_access_checked = true;
1143     return fp_access_check(s);
1144 
1145  fail_exit:
1146     /* Assert that we only raise one exception per instruction. */
1147     assert(!s->sve_access_checked);
1148     s->sve_access_checked = true;
1149     return false;
1150 }
1151 
1152 /*
1153  * Check that SME access is enabled, raise an exception if not.
1154  * Note that this function corresponds to CheckSMEAccess and is
1155  * only used directly for cpregs.
1156  */
1157 static bool sme_access_check(DisasContext *s)
1158 {
1159     if (s->sme_excp_el) {
1160         gen_exception_insn_el(s, 0, EXCP_UDEF,
1161                               syn_smetrap(SME_ET_AccessTrap, false),
1162                               s->sme_excp_el);
1163         return false;
1164     }
1165     return true;
1166 }
1167 
1168 /* This function corresponds to CheckSMEEnabled. */
1169 bool sme_enabled_check(DisasContext *s)
1170 {
1171     /*
1172      * Note that unlike sve_excp_el, we have not constrained sme_excp_el
1173      * to be zero when fp_excp_el has priority.  This is because we need
1174      * sme_excp_el by itself for cpregs access checks.
1175      */
1176     if (!s->fp_excp_el || s->sme_excp_el < s->fp_excp_el) {
1177         s->fp_access_checked = true;
1178         return sme_access_check(s);
1179     }
1180     return fp_access_check_only(s);
1181 }
1182 
1183 /* Common subroutine for CheckSMEAnd*Enabled. */
1184 bool sme_enabled_check_with_svcr(DisasContext *s, unsigned req)
1185 {
1186     if (!sme_enabled_check(s)) {
1187         return false;
1188     }
1189     if (FIELD_EX64(req, SVCR, SM) && !s->pstate_sm) {
1190         gen_exception_insn(s, 0, EXCP_UDEF,
1191                            syn_smetrap(SME_ET_NotStreaming, false));
1192         return false;
1193     }
1194     if (FIELD_EX64(req, SVCR, ZA) && !s->pstate_za) {
1195         gen_exception_insn(s, 0, EXCP_UDEF,
1196                            syn_smetrap(SME_ET_InactiveZA, false));
1197         return false;
1198     }
1199     return true;
1200 }
1201 
1202 /*
1203  * This utility function is for doing register extension with an
1204  * optional shift. You will likely want to pass a temporary for the
1205  * destination register. See DecodeRegExtend() in the ARM ARM.
1206  */
1207 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1208                               int option, unsigned int shift)
1209 {
1210     int extsize = extract32(option, 0, 2);
1211     bool is_signed = extract32(option, 2, 1);
1212 
1213     if (is_signed) {
1214         switch (extsize) {
1215         case 0:
1216             tcg_gen_ext8s_i64(tcg_out, tcg_in);
1217             break;
1218         case 1:
1219             tcg_gen_ext16s_i64(tcg_out, tcg_in);
1220             break;
1221         case 2:
1222             tcg_gen_ext32s_i64(tcg_out, tcg_in);
1223             break;
1224         case 3:
1225             tcg_gen_mov_i64(tcg_out, tcg_in);
1226             break;
1227         }
1228     } else {
1229         switch (extsize) {
1230         case 0:
1231             tcg_gen_ext8u_i64(tcg_out, tcg_in);
1232             break;
1233         case 1:
1234             tcg_gen_ext16u_i64(tcg_out, tcg_in);
1235             break;
1236         case 2:
1237             tcg_gen_ext32u_i64(tcg_out, tcg_in);
1238             break;
1239         case 3:
1240             tcg_gen_mov_i64(tcg_out, tcg_in);
1241             break;
1242         }
1243     }
1244 
1245     if (shift) {
1246         tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1247     }
1248 }
1249 
1250 static inline void gen_check_sp_alignment(DisasContext *s)
1251 {
1252     /* The AArch64 architecture mandates that (if enabled via PSTATE
1253      * or SCTLR bits) there is a check that SP is 16-aligned on every
1254      * SP-relative load or store (with an exception generated if it is not).
1255      * In line with general QEMU practice regarding misaligned accesses,
1256      * we omit these checks for the sake of guest program performance.
1257      * This function is provided as a hook so we can more easily add these
1258      * checks in future (possibly as a "favour catching guest program bugs
1259      * over speed" user selectable option).
1260      */
1261 }
1262 
1263 /*
1264  * This provides a simple table based table lookup decoder. It is
1265  * intended to be used when the relevant bits for decode are too
1266  * awkwardly placed and switch/if based logic would be confusing and
1267  * deeply nested. Since it's a linear search through the table, tables
1268  * should be kept small.
1269  *
1270  * It returns the first handler where insn & mask == pattern, or
1271  * NULL if there is no match.
1272  * The table is terminated by an empty mask (i.e. 0)
1273  */
1274 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1275                                                uint32_t insn)
1276 {
1277     const AArch64DecodeTable *tptr = table;
1278 
1279     while (tptr->mask) {
1280         if ((insn & tptr->mask) == tptr->pattern) {
1281             return tptr->disas_fn;
1282         }
1283         tptr++;
1284     }
1285     return NULL;
1286 }
1287 
1288 /*
1289  * The instruction disassembly implemented here matches
1290  * the instruction encoding classifications in chapter C4
1291  * of the ARM Architecture Reference Manual (DDI0487B_a);
1292  * classification names and decode diagrams here should generally
1293  * match up with those in the manual.
1294  */
1295 
1296 /* Unconditional branch (immediate)
1297  *   31  30       26 25                                  0
1298  * +----+-----------+-------------------------------------+
1299  * | op | 0 0 1 0 1 |                 imm26               |
1300  * +----+-----------+-------------------------------------+
1301  */
1302 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
1303 {
1304     int64_t diff = sextract32(insn, 0, 26) * 4;
1305 
1306     if (insn & (1U << 31)) {
1307         /* BL Branch with link */
1308         gen_pc_plus_diff(s, cpu_reg(s, 30), curr_insn_len(s));
1309     }
1310 
1311     /* B Branch / BL Branch with link */
1312     reset_btype(s);
1313     gen_goto_tb(s, 0, diff);
1314 }
1315 
1316 /* Compare and branch (immediate)
1317  *   31  30         25  24  23                  5 4      0
1318  * +----+-------------+----+---------------------+--------+
1319  * | sf | 0 1 1 0 1 0 | op |         imm19       |   Rt   |
1320  * +----+-------------+----+---------------------+--------+
1321  */
1322 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
1323 {
1324     unsigned int sf, op, rt;
1325     int64_t diff;
1326     DisasLabel match;
1327     TCGv_i64 tcg_cmp;
1328 
1329     sf = extract32(insn, 31, 1);
1330     op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
1331     rt = extract32(insn, 0, 5);
1332     diff = sextract32(insn, 5, 19) * 4;
1333 
1334     tcg_cmp = read_cpu_reg(s, rt, sf);
1335     reset_btype(s);
1336 
1337     match = gen_disas_label(s);
1338     tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1339                         tcg_cmp, 0, match.label);
1340     gen_goto_tb(s, 0, 4);
1341     set_disas_label(s, match);
1342     gen_goto_tb(s, 1, diff);
1343 }
1344 
1345 /* Test and branch (immediate)
1346  *   31  30         25  24  23   19 18          5 4    0
1347  * +----+-------------+----+-------+-------------+------+
1348  * | b5 | 0 1 1 0 1 1 | op |  b40  |    imm14    |  Rt  |
1349  * +----+-------------+----+-------+-------------+------+
1350  */
1351 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1352 {
1353     unsigned int bit_pos, op, rt;
1354     int64_t diff;
1355     DisasLabel match;
1356     TCGv_i64 tcg_cmp;
1357 
1358     bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1359     op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1360     diff = sextract32(insn, 5, 14) * 4;
1361     rt = extract32(insn, 0, 5);
1362 
1363     tcg_cmp = tcg_temp_new_i64();
1364     tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1365 
1366     reset_btype(s);
1367 
1368     match = gen_disas_label(s);
1369     tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1370                         tcg_cmp, 0, match.label);
1371     gen_goto_tb(s, 0, 4);
1372     set_disas_label(s, match);
1373     gen_goto_tb(s, 1, diff);
1374 }
1375 
1376 /* Conditional branch (immediate)
1377  *  31           25  24  23                  5   4  3    0
1378  * +---------------+----+---------------------+----+------+
1379  * | 0 1 0 1 0 1 0 | o1 |         imm19       | o0 | cond |
1380  * +---------------+----+---------------------+----+------+
1381  */
1382 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1383 {
1384     unsigned int cond;
1385     int64_t diff;
1386 
1387     if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1388         unallocated_encoding(s);
1389         return;
1390     }
1391     diff = sextract32(insn, 5, 19) * 4;
1392     cond = extract32(insn, 0, 4);
1393 
1394     reset_btype(s);
1395     if (cond < 0x0e) {
1396         /* genuinely conditional branches */
1397         DisasLabel match = gen_disas_label(s);
1398         arm_gen_test_cc(cond, match.label);
1399         gen_goto_tb(s, 0, 4);
1400         set_disas_label(s, match);
1401         gen_goto_tb(s, 1, diff);
1402     } else {
1403         /* 0xe and 0xf are both "always" conditions */
1404         gen_goto_tb(s, 0, diff);
1405     }
1406 }
1407 
1408 /* HINT instruction group, including various allocated HINTs */
1409 static void handle_hint(DisasContext *s, uint32_t insn,
1410                         unsigned int op1, unsigned int op2, unsigned int crm)
1411 {
1412     unsigned int selector = crm << 3 | op2;
1413 
1414     if (op1 != 3) {
1415         unallocated_encoding(s);
1416         return;
1417     }
1418 
1419     switch (selector) {
1420     case 0b00000: /* NOP */
1421         break;
1422     case 0b00011: /* WFI */
1423         s->base.is_jmp = DISAS_WFI;
1424         break;
1425     case 0b00001: /* YIELD */
1426         /* When running in MTTCG we don't generate jumps to the yield and
1427          * WFE helpers as it won't affect the scheduling of other vCPUs.
1428          * If we wanted to more completely model WFE/SEV so we don't busy
1429          * spin unnecessarily we would need to do something more involved.
1430          */
1431         if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1432             s->base.is_jmp = DISAS_YIELD;
1433         }
1434         break;
1435     case 0b00010: /* WFE */
1436         if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1437             s->base.is_jmp = DISAS_WFE;
1438         }
1439         break;
1440     case 0b00100: /* SEV */
1441     case 0b00101: /* SEVL */
1442     case 0b00110: /* DGH */
1443         /* we treat all as NOP at least for now */
1444         break;
1445     case 0b00111: /* XPACLRI */
1446         if (s->pauth_active) {
1447             gen_helper_xpaci(cpu_X[30], cpu_env, cpu_X[30]);
1448         }
1449         break;
1450     case 0b01000: /* PACIA1716 */
1451         if (s->pauth_active) {
1452             gen_helper_pacia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1453         }
1454         break;
1455     case 0b01010: /* PACIB1716 */
1456         if (s->pauth_active) {
1457             gen_helper_pacib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1458         }
1459         break;
1460     case 0b01100: /* AUTIA1716 */
1461         if (s->pauth_active) {
1462             gen_helper_autia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1463         }
1464         break;
1465     case 0b01110: /* AUTIB1716 */
1466         if (s->pauth_active) {
1467             gen_helper_autib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1468         }
1469         break;
1470     case 0b10000: /* ESB */
1471         /* Without RAS, we must implement this as NOP. */
1472         if (dc_isar_feature(aa64_ras, s)) {
1473             /*
1474              * QEMU does not have a source of physical SErrors,
1475              * so we are only concerned with virtual SErrors.
1476              * The pseudocode in the ARM for this case is
1477              *   if PSTATE.EL IN {EL0, EL1} && EL2Enabled() then
1478              *      AArch64.vESBOperation();
1479              * Most of the condition can be evaluated at translation time.
1480              * Test for EL2 present, and defer test for SEL2 to runtime.
1481              */
1482             if (s->current_el <= 1 && arm_dc_feature(s, ARM_FEATURE_EL2)) {
1483                 gen_helper_vesb(cpu_env);
1484             }
1485         }
1486         break;
1487     case 0b11000: /* PACIAZ */
1488         if (s->pauth_active) {
1489             gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30],
1490                              tcg_constant_i64(0));
1491         }
1492         break;
1493     case 0b11001: /* PACIASP */
1494         if (s->pauth_active) {
1495             gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1496         }
1497         break;
1498     case 0b11010: /* PACIBZ */
1499         if (s->pauth_active) {
1500             gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30],
1501                              tcg_constant_i64(0));
1502         }
1503         break;
1504     case 0b11011: /* PACIBSP */
1505         if (s->pauth_active) {
1506             gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1507         }
1508         break;
1509     case 0b11100: /* AUTIAZ */
1510         if (s->pauth_active) {
1511             gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30],
1512                              tcg_constant_i64(0));
1513         }
1514         break;
1515     case 0b11101: /* AUTIASP */
1516         if (s->pauth_active) {
1517             gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1518         }
1519         break;
1520     case 0b11110: /* AUTIBZ */
1521         if (s->pauth_active) {
1522             gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30],
1523                              tcg_constant_i64(0));
1524         }
1525         break;
1526     case 0b11111: /* AUTIBSP */
1527         if (s->pauth_active) {
1528             gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1529         }
1530         break;
1531     default:
1532         /* default specified as NOP equivalent */
1533         break;
1534     }
1535 }
1536 
1537 static void gen_clrex(DisasContext *s, uint32_t insn)
1538 {
1539     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1540 }
1541 
1542 /* CLREX, DSB, DMB, ISB */
1543 static void handle_sync(DisasContext *s, uint32_t insn,
1544                         unsigned int op1, unsigned int op2, unsigned int crm)
1545 {
1546     TCGBar bar;
1547 
1548     if (op1 != 3) {
1549         unallocated_encoding(s);
1550         return;
1551     }
1552 
1553     switch (op2) {
1554     case 2: /* CLREX */
1555         gen_clrex(s, insn);
1556         return;
1557     case 4: /* DSB */
1558     case 5: /* DMB */
1559         switch (crm & 3) {
1560         case 1: /* MBReqTypes_Reads */
1561             bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1562             break;
1563         case 2: /* MBReqTypes_Writes */
1564             bar = TCG_BAR_SC | TCG_MO_ST_ST;
1565             break;
1566         default: /* MBReqTypes_All */
1567             bar = TCG_BAR_SC | TCG_MO_ALL;
1568             break;
1569         }
1570         tcg_gen_mb(bar);
1571         return;
1572     case 6: /* ISB */
1573         /* We need to break the TB after this insn to execute
1574          * a self-modified code correctly and also to take
1575          * any pending interrupts immediately.
1576          */
1577         reset_btype(s);
1578         gen_goto_tb(s, 0, 4);
1579         return;
1580 
1581     case 7: /* SB */
1582         if (crm != 0 || !dc_isar_feature(aa64_sb, s)) {
1583             goto do_unallocated;
1584         }
1585         /*
1586          * TODO: There is no speculation barrier opcode for TCG;
1587          * MB and end the TB instead.
1588          */
1589         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1590         gen_goto_tb(s, 0, 4);
1591         return;
1592 
1593     default:
1594     do_unallocated:
1595         unallocated_encoding(s);
1596         return;
1597     }
1598 }
1599 
1600 static void gen_xaflag(void)
1601 {
1602     TCGv_i32 z = tcg_temp_new_i32();
1603 
1604     tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1605 
1606     /*
1607      * (!C & !Z) << 31
1608      * (!(C | Z)) << 31
1609      * ~((C | Z) << 31)
1610      * ~-(C | Z)
1611      * (C | Z) - 1
1612      */
1613     tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1614     tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1615 
1616     /* !(Z & C) */
1617     tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1618     tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1619 
1620     /* (!C & Z) << 31 -> -(Z & ~C) */
1621     tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1622     tcg_gen_neg_i32(cpu_VF, cpu_VF);
1623 
1624     /* C | Z */
1625     tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1626 }
1627 
1628 static void gen_axflag(void)
1629 {
1630     tcg_gen_sari_i32(cpu_VF, cpu_VF, 31);         /* V ? -1 : 0 */
1631     tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF);     /* C & !V */
1632 
1633     /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1634     tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1635 
1636     tcg_gen_movi_i32(cpu_NF, 0);
1637     tcg_gen_movi_i32(cpu_VF, 0);
1638 }
1639 
1640 /* MSR (immediate) - move immediate to processor state field */
1641 static void handle_msr_i(DisasContext *s, uint32_t insn,
1642                          unsigned int op1, unsigned int op2, unsigned int crm)
1643 {
1644     int op = op1 << 3 | op2;
1645 
1646     /* End the TB by default, chaining is ok.  */
1647     s->base.is_jmp = DISAS_TOO_MANY;
1648 
1649     switch (op) {
1650     case 0x00: /* CFINV */
1651         if (crm != 0 || !dc_isar_feature(aa64_condm_4, s)) {
1652             goto do_unallocated;
1653         }
1654         tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1655         s->base.is_jmp = DISAS_NEXT;
1656         break;
1657 
1658     case 0x01: /* XAFlag */
1659         if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1660             goto do_unallocated;
1661         }
1662         gen_xaflag();
1663         s->base.is_jmp = DISAS_NEXT;
1664         break;
1665 
1666     case 0x02: /* AXFlag */
1667         if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1668             goto do_unallocated;
1669         }
1670         gen_axflag();
1671         s->base.is_jmp = DISAS_NEXT;
1672         break;
1673 
1674     case 0x03: /* UAO */
1675         if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1676             goto do_unallocated;
1677         }
1678         if (crm & 1) {
1679             set_pstate_bits(PSTATE_UAO);
1680         } else {
1681             clear_pstate_bits(PSTATE_UAO);
1682         }
1683         gen_rebuild_hflags(s);
1684         break;
1685 
1686     case 0x04: /* PAN */
1687         if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1688             goto do_unallocated;
1689         }
1690         if (crm & 1) {
1691             set_pstate_bits(PSTATE_PAN);
1692         } else {
1693             clear_pstate_bits(PSTATE_PAN);
1694         }
1695         gen_rebuild_hflags(s);
1696         break;
1697 
1698     case 0x05: /* SPSel */
1699         if (s->current_el == 0) {
1700             goto do_unallocated;
1701         }
1702         gen_helper_msr_i_spsel(cpu_env, tcg_constant_i32(crm & PSTATE_SP));
1703         break;
1704 
1705     case 0x19: /* SSBS */
1706         if (!dc_isar_feature(aa64_ssbs, s)) {
1707             goto do_unallocated;
1708         }
1709         if (crm & 1) {
1710             set_pstate_bits(PSTATE_SSBS);
1711         } else {
1712             clear_pstate_bits(PSTATE_SSBS);
1713         }
1714         /* Don't need to rebuild hflags since SSBS is a nop */
1715         break;
1716 
1717     case 0x1a: /* DIT */
1718         if (!dc_isar_feature(aa64_dit, s)) {
1719             goto do_unallocated;
1720         }
1721         if (crm & 1) {
1722             set_pstate_bits(PSTATE_DIT);
1723         } else {
1724             clear_pstate_bits(PSTATE_DIT);
1725         }
1726         /* There's no need to rebuild hflags because DIT is a nop */
1727         break;
1728 
1729     case 0x1e: /* DAIFSet */
1730         gen_helper_msr_i_daifset(cpu_env, tcg_constant_i32(crm));
1731         break;
1732 
1733     case 0x1f: /* DAIFClear */
1734         gen_helper_msr_i_daifclear(cpu_env, tcg_constant_i32(crm));
1735         /* For DAIFClear, exit the cpu loop to re-evaluate pending IRQs.  */
1736         s->base.is_jmp = DISAS_UPDATE_EXIT;
1737         break;
1738 
1739     case 0x1c: /* TCO */
1740         if (dc_isar_feature(aa64_mte, s)) {
1741             /* Full MTE is enabled -- set the TCO bit as directed. */
1742             if (crm & 1) {
1743                 set_pstate_bits(PSTATE_TCO);
1744             } else {
1745                 clear_pstate_bits(PSTATE_TCO);
1746             }
1747             gen_rebuild_hflags(s);
1748             /* Many factors, including TCO, go into MTE_ACTIVE. */
1749             s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
1750         } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
1751             /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI.  */
1752             s->base.is_jmp = DISAS_NEXT;
1753         } else {
1754             goto do_unallocated;
1755         }
1756         break;
1757 
1758     case 0x1b: /* SVCR* */
1759         if (!dc_isar_feature(aa64_sme, s) || crm < 2 || crm > 7) {
1760             goto do_unallocated;
1761         }
1762         if (sme_access_check(s)) {
1763             int old = s->pstate_sm | (s->pstate_za << 1);
1764             int new = (crm & 1) * 3;
1765             int msk = (crm >> 1) & 3;
1766 
1767             if ((old ^ new) & msk) {
1768                 /* At least one bit changes. */
1769                 gen_helper_set_svcr(cpu_env, tcg_constant_i32(new),
1770                                     tcg_constant_i32(msk));
1771             } else {
1772                 s->base.is_jmp = DISAS_NEXT;
1773             }
1774         }
1775         break;
1776 
1777     default:
1778     do_unallocated:
1779         unallocated_encoding(s);
1780         return;
1781     }
1782 }
1783 
1784 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1785 {
1786     TCGv_i32 tmp = tcg_temp_new_i32();
1787     TCGv_i32 nzcv = tcg_temp_new_i32();
1788 
1789     /* build bit 31, N */
1790     tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
1791     /* build bit 30, Z */
1792     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1793     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1794     /* build bit 29, C */
1795     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1796     /* build bit 28, V */
1797     tcg_gen_shri_i32(tmp, cpu_VF, 31);
1798     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1799     /* generate result */
1800     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1801 }
1802 
1803 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1804 {
1805     TCGv_i32 nzcv = tcg_temp_new_i32();
1806 
1807     /* take NZCV from R[t] */
1808     tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
1809 
1810     /* bit 31, N */
1811     tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
1812     /* bit 30, Z */
1813     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1814     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1815     /* bit 29, C */
1816     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1817     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1818     /* bit 28, V */
1819     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1820     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1821 }
1822 
1823 static void gen_sysreg_undef(DisasContext *s, bool isread,
1824                              uint8_t op0, uint8_t op1, uint8_t op2,
1825                              uint8_t crn, uint8_t crm, uint8_t rt)
1826 {
1827     /*
1828      * Generate code to emit an UNDEF with correct syndrome
1829      * information for a failed system register access.
1830      * This is EC_UNCATEGORIZED (ie a standard UNDEF) in most cases,
1831      * but if FEAT_IDST is implemented then read accesses to registers
1832      * in the feature ID space are reported with the EC_SYSTEMREGISTERTRAP
1833      * syndrome.
1834      */
1835     uint32_t syndrome;
1836 
1837     if (isread && dc_isar_feature(aa64_ids, s) &&
1838         arm_cpreg_encoding_in_idspace(op0, op1, op2, crn, crm)) {
1839         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1840     } else {
1841         syndrome = syn_uncategorized();
1842     }
1843     gen_exception_insn(s, 0, EXCP_UDEF, syndrome);
1844 }
1845 
1846 /* MRS - move from system register
1847  * MSR (register) - move to system register
1848  * SYS
1849  * SYSL
1850  * These are all essentially the same insn in 'read' and 'write'
1851  * versions, with varying op0 fields.
1852  */
1853 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1854                        unsigned int op0, unsigned int op1, unsigned int op2,
1855                        unsigned int crn, unsigned int crm, unsigned int rt)
1856 {
1857     uint32_t key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1858                                       crn, crm, op0, op1, op2);
1859     const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
1860     TCGv_ptr tcg_ri = NULL;
1861     TCGv_i64 tcg_rt;
1862 
1863     if (!ri) {
1864         /* Unknown register; this might be a guest error or a QEMU
1865          * unimplemented feature.
1866          */
1867         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1868                       "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1869                       isread ? "read" : "write", op0, op1, crn, crm, op2);
1870         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
1871         return;
1872     }
1873 
1874     /* Check access permissions */
1875     if (!cp_access_ok(s->current_el, ri, isread)) {
1876         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
1877         return;
1878     }
1879 
1880     if (ri->accessfn || (ri->fgt && s->fgt_active)) {
1881         /* Emit code to perform further access permissions checks at
1882          * runtime; this may result in an exception.
1883          */
1884         uint32_t syndrome;
1885 
1886         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1887         gen_a64_update_pc(s, 0);
1888         tcg_ri = tcg_temp_new_ptr();
1889         gen_helper_access_check_cp_reg(tcg_ri, cpu_env,
1890                                        tcg_constant_i32(key),
1891                                        tcg_constant_i32(syndrome),
1892                                        tcg_constant_i32(isread));
1893     } else if (ri->type & ARM_CP_RAISES_EXC) {
1894         /*
1895          * The readfn or writefn might raise an exception;
1896          * synchronize the CPU state in case it does.
1897          */
1898         gen_a64_update_pc(s, 0);
1899     }
1900 
1901     /* Handle special cases first */
1902     switch (ri->type & ARM_CP_SPECIAL_MASK) {
1903     case 0:
1904         break;
1905     case ARM_CP_NOP:
1906         return;
1907     case ARM_CP_NZCV:
1908         tcg_rt = cpu_reg(s, rt);
1909         if (isread) {
1910             gen_get_nzcv(tcg_rt);
1911         } else {
1912             gen_set_nzcv(tcg_rt);
1913         }
1914         return;
1915     case ARM_CP_CURRENTEL:
1916         /* Reads as current EL value from pstate, which is
1917          * guaranteed to be constant by the tb flags.
1918          */
1919         tcg_rt = cpu_reg(s, rt);
1920         tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
1921         return;
1922     case ARM_CP_DC_ZVA:
1923         /* Writes clear the aligned block of memory which rt points into. */
1924         if (s->mte_active[0]) {
1925             int desc = 0;
1926 
1927             desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
1928             desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
1929             desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
1930 
1931             tcg_rt = tcg_temp_new_i64();
1932             gen_helper_mte_check_zva(tcg_rt, cpu_env,
1933                                      tcg_constant_i32(desc), cpu_reg(s, rt));
1934         } else {
1935             tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
1936         }
1937         gen_helper_dc_zva(cpu_env, tcg_rt);
1938         return;
1939     case ARM_CP_DC_GVA:
1940         {
1941             TCGv_i64 clean_addr, tag;
1942 
1943             /*
1944              * DC_GVA, like DC_ZVA, requires that we supply the original
1945              * pointer for an invalid page.  Probe that address first.
1946              */
1947             tcg_rt = cpu_reg(s, rt);
1948             clean_addr = clean_data_tbi(s, tcg_rt);
1949             gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
1950 
1951             if (s->ata) {
1952                 /* Extract the tag from the register to match STZGM.  */
1953                 tag = tcg_temp_new_i64();
1954                 tcg_gen_shri_i64(tag, tcg_rt, 56);
1955                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
1956             }
1957         }
1958         return;
1959     case ARM_CP_DC_GZVA:
1960         {
1961             TCGv_i64 clean_addr, tag;
1962 
1963             /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
1964             tcg_rt = cpu_reg(s, rt);
1965             clean_addr = clean_data_tbi(s, tcg_rt);
1966             gen_helper_dc_zva(cpu_env, clean_addr);
1967 
1968             if (s->ata) {
1969                 /* Extract the tag from the register to match STZGM.  */
1970                 tag = tcg_temp_new_i64();
1971                 tcg_gen_shri_i64(tag, tcg_rt, 56);
1972                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
1973             }
1974         }
1975         return;
1976     default:
1977         g_assert_not_reached();
1978     }
1979     if ((ri->type & ARM_CP_FPU) && !fp_access_check_only(s)) {
1980         return;
1981     } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
1982         return;
1983     } else if ((ri->type & ARM_CP_SME) && !sme_access_check(s)) {
1984         return;
1985     }
1986 
1987     if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1988         gen_io_start();
1989     }
1990 
1991     tcg_rt = cpu_reg(s, rt);
1992 
1993     if (isread) {
1994         if (ri->type & ARM_CP_CONST) {
1995             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1996         } else if (ri->readfn) {
1997             if (!tcg_ri) {
1998                 tcg_ri = gen_lookup_cp_reg(key);
1999             }
2000             gen_helper_get_cp_reg64(tcg_rt, cpu_env, tcg_ri);
2001         } else {
2002             tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
2003         }
2004     } else {
2005         if (ri->type & ARM_CP_CONST) {
2006             /* If not forbidden by access permissions, treat as WI */
2007             return;
2008         } else if (ri->writefn) {
2009             if (!tcg_ri) {
2010                 tcg_ri = gen_lookup_cp_reg(key);
2011             }
2012             gen_helper_set_cp_reg64(cpu_env, tcg_ri, tcg_rt);
2013         } else {
2014             tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
2015         }
2016     }
2017 
2018     if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
2019         /* I/O operations must end the TB here (whether read or write) */
2020         s->base.is_jmp = DISAS_UPDATE_EXIT;
2021     }
2022     if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
2023         /*
2024          * A write to any coprocessor regiser that ends a TB
2025          * must rebuild the hflags for the next TB.
2026          */
2027         gen_rebuild_hflags(s);
2028         /*
2029          * We default to ending the TB on a coprocessor register write,
2030          * but allow this to be suppressed by the register definition
2031          * (usually only necessary to work around guest bugs).
2032          */
2033         s->base.is_jmp = DISAS_UPDATE_EXIT;
2034     }
2035 }
2036 
2037 /* System
2038  *  31                 22 21  20 19 18 16 15   12 11    8 7   5 4    0
2039  * +---------------------+---+-----+-----+-------+-------+-----+------+
2040  * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 |  CRn  |  CRm  | op2 |  Rt  |
2041  * +---------------------+---+-----+-----+-------+-------+-----+------+
2042  */
2043 static void disas_system(DisasContext *s, uint32_t insn)
2044 {
2045     unsigned int l, op0, op1, crn, crm, op2, rt;
2046     l = extract32(insn, 21, 1);
2047     op0 = extract32(insn, 19, 2);
2048     op1 = extract32(insn, 16, 3);
2049     crn = extract32(insn, 12, 4);
2050     crm = extract32(insn, 8, 4);
2051     op2 = extract32(insn, 5, 3);
2052     rt = extract32(insn, 0, 5);
2053 
2054     if (op0 == 0) {
2055         if (l || rt != 31) {
2056             unallocated_encoding(s);
2057             return;
2058         }
2059         switch (crn) {
2060         case 2: /* HINT (including allocated hints like NOP, YIELD, etc) */
2061             handle_hint(s, insn, op1, op2, crm);
2062             break;
2063         case 3: /* CLREX, DSB, DMB, ISB */
2064             handle_sync(s, insn, op1, op2, crm);
2065             break;
2066         case 4: /* MSR (immediate) */
2067             handle_msr_i(s, insn, op1, op2, crm);
2068             break;
2069         default:
2070             unallocated_encoding(s);
2071             break;
2072         }
2073         return;
2074     }
2075     handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
2076 }
2077 
2078 /* Exception generation
2079  *
2080  *  31             24 23 21 20                     5 4   2 1  0
2081  * +-----------------+-----+------------------------+-----+----+
2082  * | 1 1 0 1 0 1 0 0 | opc |          imm16         | op2 | LL |
2083  * +-----------------------+------------------------+----------+
2084  */
2085 static void disas_exc(DisasContext *s, uint32_t insn)
2086 {
2087     int opc = extract32(insn, 21, 3);
2088     int op2_ll = extract32(insn, 0, 5);
2089     int imm16 = extract32(insn, 5, 16);
2090     uint32_t syndrome;
2091 
2092     switch (opc) {
2093     case 0:
2094         /* For SVC, HVC and SMC we advance the single-step state
2095          * machine before taking the exception. This is architecturally
2096          * mandated, to ensure that single-stepping a system call
2097          * instruction works properly.
2098          */
2099         switch (op2_ll) {
2100         case 1:                                                     /* SVC */
2101             syndrome = syn_aa64_svc(imm16);
2102             if (s->fgt_svc) {
2103                 gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2104                 break;
2105             }
2106             gen_ss_advance(s);
2107             gen_exception_insn(s, 4, EXCP_SWI, syndrome);
2108             break;
2109         case 2:                                                     /* HVC */
2110             if (s->current_el == 0) {
2111                 unallocated_encoding(s);
2112                 break;
2113             }
2114             /* The pre HVC helper handles cases when HVC gets trapped
2115              * as an undefined insn by runtime configuration.
2116              */
2117             gen_a64_update_pc(s, 0);
2118             gen_helper_pre_hvc(cpu_env);
2119             gen_ss_advance(s);
2120             gen_exception_insn_el(s, 4, EXCP_HVC, syn_aa64_hvc(imm16), 2);
2121             break;
2122         case 3:                                                     /* SMC */
2123             if (s->current_el == 0) {
2124                 unallocated_encoding(s);
2125                 break;
2126             }
2127             gen_a64_update_pc(s, 0);
2128             gen_helper_pre_smc(cpu_env, tcg_constant_i32(syn_aa64_smc(imm16)));
2129             gen_ss_advance(s);
2130             gen_exception_insn_el(s, 4, EXCP_SMC, syn_aa64_smc(imm16), 3);
2131             break;
2132         default:
2133             unallocated_encoding(s);
2134             break;
2135         }
2136         break;
2137     case 1:
2138         if (op2_ll != 0) {
2139             unallocated_encoding(s);
2140             break;
2141         }
2142         /* BRK */
2143         gen_exception_bkpt_insn(s, syn_aa64_bkpt(imm16));
2144         break;
2145     case 2:
2146         if (op2_ll != 0) {
2147             unallocated_encoding(s);
2148             break;
2149         }
2150         /* HLT. This has two purposes.
2151          * Architecturally, it is an external halting debug instruction.
2152          * Since QEMU doesn't implement external debug, we treat this as
2153          * it is required for halting debug disabled: it will UNDEF.
2154          * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2155          */
2156         if (semihosting_enabled(s->current_el == 0) && imm16 == 0xf000) {
2157             gen_exception_internal_insn(s, EXCP_SEMIHOST);
2158         } else {
2159             unallocated_encoding(s);
2160         }
2161         break;
2162     case 5:
2163         if (op2_ll < 1 || op2_ll > 3) {
2164             unallocated_encoding(s);
2165             break;
2166         }
2167         /* DCPS1, DCPS2, DCPS3 */
2168         unallocated_encoding(s);
2169         break;
2170     default:
2171         unallocated_encoding(s);
2172         break;
2173     }
2174 }
2175 
2176 /* Unconditional branch (register)
2177  *  31           25 24   21 20   16 15   10 9    5 4     0
2178  * +---------------+-------+-------+-------+------+-------+
2179  * | 1 1 0 1 0 1 1 |  opc  |  op2  |  op3  |  Rn  |  op4  |
2180  * +---------------+-------+-------+-------+------+-------+
2181  */
2182 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
2183 {
2184     unsigned int opc, op2, op3, rn, op4;
2185     unsigned btype_mod = 2;   /* 0: BR, 1: BLR, 2: other */
2186     TCGv_i64 dst;
2187     TCGv_i64 modifier;
2188 
2189     opc = extract32(insn, 21, 4);
2190     op2 = extract32(insn, 16, 5);
2191     op3 = extract32(insn, 10, 6);
2192     rn = extract32(insn, 5, 5);
2193     op4 = extract32(insn, 0, 5);
2194 
2195     if (op2 != 0x1f) {
2196         goto do_unallocated;
2197     }
2198 
2199     switch (opc) {
2200     case 0: /* BR */
2201     case 1: /* BLR */
2202     case 2: /* RET */
2203         btype_mod = opc;
2204         switch (op3) {
2205         case 0:
2206             /* BR, BLR, RET */
2207             if (op4 != 0) {
2208                 goto do_unallocated;
2209             }
2210             dst = cpu_reg(s, rn);
2211             break;
2212 
2213         case 2:
2214         case 3:
2215             if (!dc_isar_feature(aa64_pauth, s)) {
2216                 goto do_unallocated;
2217             }
2218             if (opc == 2) {
2219                 /* RETAA, RETAB */
2220                 if (rn != 0x1f || op4 != 0x1f) {
2221                     goto do_unallocated;
2222                 }
2223                 rn = 30;
2224                 modifier = cpu_X[31];
2225             } else {
2226                 /* BRAAZ, BRABZ, BLRAAZ, BLRABZ */
2227                 if (op4 != 0x1f) {
2228                     goto do_unallocated;
2229                 }
2230                 modifier = tcg_constant_i64(0);
2231             }
2232             if (s->pauth_active) {
2233                 dst = tcg_temp_new_i64();
2234                 if (op3 == 2) {
2235                     gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
2236                 } else {
2237                     gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2238                 }
2239             } else {
2240                 dst = cpu_reg(s, rn);
2241             }
2242             break;
2243 
2244         default:
2245             goto do_unallocated;
2246         }
2247         /* BLR also needs to load return address */
2248         if (opc == 1) {
2249             TCGv_i64 lr = cpu_reg(s, 30);
2250             if (dst == lr) {
2251                 TCGv_i64 tmp = tcg_temp_new_i64();
2252                 tcg_gen_mov_i64(tmp, dst);
2253                 dst = tmp;
2254             }
2255             gen_pc_plus_diff(s, lr, curr_insn_len(s));
2256         }
2257         gen_a64_set_pc(s, dst);
2258         break;
2259 
2260     case 8: /* BRAA */
2261     case 9: /* BLRAA */
2262         if (!dc_isar_feature(aa64_pauth, s)) {
2263             goto do_unallocated;
2264         }
2265         if ((op3 & ~1) != 2) {
2266             goto do_unallocated;
2267         }
2268         btype_mod = opc & 1;
2269         if (s->pauth_active) {
2270             dst = tcg_temp_new_i64();
2271             modifier = cpu_reg_sp(s, op4);
2272             if (op3 == 2) {
2273                 gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
2274             } else {
2275                 gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2276             }
2277         } else {
2278             dst = cpu_reg(s, rn);
2279         }
2280         /* BLRAA also needs to load return address */
2281         if (opc == 9) {
2282             TCGv_i64 lr = cpu_reg(s, 30);
2283             if (dst == lr) {
2284                 TCGv_i64 tmp = tcg_temp_new_i64();
2285                 tcg_gen_mov_i64(tmp, dst);
2286                 dst = tmp;
2287             }
2288             gen_pc_plus_diff(s, lr, curr_insn_len(s));
2289         }
2290         gen_a64_set_pc(s, dst);
2291         break;
2292 
2293     case 4: /* ERET */
2294         if (s->current_el == 0) {
2295             goto do_unallocated;
2296         }
2297         switch (op3) {
2298         case 0: /* ERET */
2299             if (op4 != 0) {
2300                 goto do_unallocated;
2301             }
2302             if (s->fgt_eret) {
2303                 gen_exception_insn_el(s, 0, EXCP_UDEF, syn_erettrap(op3), 2);
2304                 return;
2305             }
2306             dst = tcg_temp_new_i64();
2307             tcg_gen_ld_i64(dst, cpu_env,
2308                            offsetof(CPUARMState, elr_el[s->current_el]));
2309             break;
2310 
2311         case 2: /* ERETAA */
2312         case 3: /* ERETAB */
2313             if (!dc_isar_feature(aa64_pauth, s)) {
2314                 goto do_unallocated;
2315             }
2316             if (rn != 0x1f || op4 != 0x1f) {
2317                 goto do_unallocated;
2318             }
2319             /* The FGT trap takes precedence over an auth trap. */
2320             if (s->fgt_eret) {
2321                 gen_exception_insn_el(s, 0, EXCP_UDEF, syn_erettrap(op3), 2);
2322                 return;
2323             }
2324             dst = tcg_temp_new_i64();
2325             tcg_gen_ld_i64(dst, cpu_env,
2326                            offsetof(CPUARMState, elr_el[s->current_el]));
2327             if (s->pauth_active) {
2328                 modifier = cpu_X[31];
2329                 if (op3 == 2) {
2330                     gen_helper_autia(dst, cpu_env, dst, modifier);
2331                 } else {
2332                     gen_helper_autib(dst, cpu_env, dst, modifier);
2333                 }
2334             }
2335             break;
2336 
2337         default:
2338             goto do_unallocated;
2339         }
2340         if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2341             gen_io_start();
2342         }
2343 
2344         gen_helper_exception_return(cpu_env, dst);
2345         /* Must exit loop to check un-masked IRQs */
2346         s->base.is_jmp = DISAS_EXIT;
2347         return;
2348 
2349     case 5: /* DRPS */
2350         if (op3 != 0 || op4 != 0 || rn != 0x1f) {
2351             goto do_unallocated;
2352         } else {
2353             unallocated_encoding(s);
2354         }
2355         return;
2356 
2357     default:
2358     do_unallocated:
2359         unallocated_encoding(s);
2360         return;
2361     }
2362 
2363     switch (btype_mod) {
2364     case 0: /* BR */
2365         if (dc_isar_feature(aa64_bti, s)) {
2366             /* BR to {x16,x17} or !guard -> 1, else 3.  */
2367             set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
2368         }
2369         break;
2370 
2371     case 1: /* BLR */
2372         if (dc_isar_feature(aa64_bti, s)) {
2373             /* BLR sets BTYPE to 2, regardless of source guarded page.  */
2374             set_btype(s, 2);
2375         }
2376         break;
2377 
2378     default: /* RET or none of the above.  */
2379         /* BTYPE will be set to 0 by normal end-of-insn processing.  */
2380         break;
2381     }
2382 
2383     s->base.is_jmp = DISAS_JUMP;
2384 }
2385 
2386 /* Branches, exception generating and system instructions */
2387 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
2388 {
2389     switch (extract32(insn, 25, 7)) {
2390     case 0x0a: case 0x0b:
2391     case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
2392         disas_uncond_b_imm(s, insn);
2393         break;
2394     case 0x1a: case 0x5a: /* Compare & branch (immediate) */
2395         disas_comp_b_imm(s, insn);
2396         break;
2397     case 0x1b: case 0x5b: /* Test & branch (immediate) */
2398         disas_test_b_imm(s, insn);
2399         break;
2400     case 0x2a: /* Conditional branch (immediate) */
2401         disas_cond_b_imm(s, insn);
2402         break;
2403     case 0x6a: /* Exception generation / System */
2404         if (insn & (1 << 24)) {
2405             if (extract32(insn, 22, 2) == 0) {
2406                 disas_system(s, insn);
2407             } else {
2408                 unallocated_encoding(s);
2409             }
2410         } else {
2411             disas_exc(s, insn);
2412         }
2413         break;
2414     case 0x6b: /* Unconditional branch (register) */
2415         disas_uncond_b_reg(s, insn);
2416         break;
2417     default:
2418         unallocated_encoding(s);
2419         break;
2420     }
2421 }
2422 
2423 /*
2424  * Load/Store exclusive instructions are implemented by remembering
2425  * the value/address loaded, and seeing if these are the same
2426  * when the store is performed. This is not actually the architecturally
2427  * mandated semantics, but it works for typical guest code sequences
2428  * and avoids having to monitor regular stores.
2429  *
2430  * The store exclusive uses the atomic cmpxchg primitives to avoid
2431  * races in multi-threaded linux-user and when MTTCG softmmu is
2432  * enabled.
2433  */
2434 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
2435                                TCGv_i64 addr, int size, bool is_pair)
2436 {
2437     int idx = get_mem_index(s);
2438     MemOp memop = s->be_data;
2439 
2440     g_assert(size <= 3);
2441     if (is_pair) {
2442         g_assert(size >= 2);
2443         if (size == 2) {
2444             /* The pair must be single-copy atomic for the doubleword.  */
2445             memop |= MO_64 | MO_ALIGN;
2446             tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2447             if (s->be_data == MO_LE) {
2448                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2449                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2450             } else {
2451                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2452                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2453             }
2454         } else {
2455             /* The pair must be single-copy atomic for *each* doubleword, not
2456                the entire quadword, however it must be quadword aligned.  */
2457             memop |= MO_64;
2458             tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx,
2459                                 memop | MO_ALIGN_16);
2460 
2461             TCGv_i64 addr2 = tcg_temp_new_i64();
2462             tcg_gen_addi_i64(addr2, addr, 8);
2463             tcg_gen_qemu_ld_i64(cpu_exclusive_high, addr2, idx, memop);
2464 
2465             tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2466             tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2467         }
2468     } else {
2469         memop |= size | MO_ALIGN;
2470         tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2471         tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2472     }
2473     tcg_gen_mov_i64(cpu_exclusive_addr, addr);
2474 }
2475 
2476 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2477                                 TCGv_i64 addr, int size, int is_pair)
2478 {
2479     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2480      *     && (!is_pair || env->exclusive_high == [addr + datasize])) {
2481      *     [addr] = {Rt};
2482      *     if (is_pair) {
2483      *         [addr + datasize] = {Rt2};
2484      *     }
2485      *     {Rd} = 0;
2486      * } else {
2487      *     {Rd} = 1;
2488      * }
2489      * env->exclusive_addr = -1;
2490      */
2491     TCGLabel *fail_label = gen_new_label();
2492     TCGLabel *done_label = gen_new_label();
2493     TCGv_i64 tmp;
2494 
2495     tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
2496 
2497     tmp = tcg_temp_new_i64();
2498     if (is_pair) {
2499         if (size == 2) {
2500             if (s->be_data == MO_LE) {
2501                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2502             } else {
2503                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2504             }
2505             tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2506                                        cpu_exclusive_val, tmp,
2507                                        get_mem_index(s),
2508                                        MO_64 | MO_ALIGN | s->be_data);
2509             tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2510         } else {
2511             TCGv_i128 t16 = tcg_temp_new_i128();
2512             TCGv_i128 c16 = tcg_temp_new_i128();
2513             TCGv_i64 a, b;
2514 
2515             if (s->be_data == MO_LE) {
2516                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt), cpu_reg(s, rt2));
2517                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_val,
2518                                         cpu_exclusive_high);
2519             } else {
2520                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt2), cpu_reg(s, rt));
2521                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_high,
2522                                         cpu_exclusive_val);
2523             }
2524 
2525             tcg_gen_atomic_cmpxchg_i128(t16, cpu_exclusive_addr, c16, t16,
2526                                         get_mem_index(s),
2527                                         MO_128 | MO_ALIGN | s->be_data);
2528 
2529             a = tcg_temp_new_i64();
2530             b = tcg_temp_new_i64();
2531             if (s->be_data == MO_LE) {
2532                 tcg_gen_extr_i128_i64(a, b, t16);
2533             } else {
2534                 tcg_gen_extr_i128_i64(b, a, t16);
2535             }
2536 
2537             tcg_gen_xor_i64(a, a, cpu_exclusive_val);
2538             tcg_gen_xor_i64(b, b, cpu_exclusive_high);
2539             tcg_gen_or_i64(tmp, a, b);
2540 
2541             tcg_gen_setcondi_i64(TCG_COND_NE, tmp, tmp, 0);
2542         }
2543     } else {
2544         tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2545                                    cpu_reg(s, rt), get_mem_index(s),
2546                                    size | MO_ALIGN | s->be_data);
2547         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2548     }
2549     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2550     tcg_gen_br(done_label);
2551 
2552     gen_set_label(fail_label);
2553     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2554     gen_set_label(done_label);
2555     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2556 }
2557 
2558 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2559                                  int rn, int size)
2560 {
2561     TCGv_i64 tcg_rs = cpu_reg(s, rs);
2562     TCGv_i64 tcg_rt = cpu_reg(s, rt);
2563     int memidx = get_mem_index(s);
2564     TCGv_i64 clean_addr;
2565 
2566     if (rn == 31) {
2567         gen_check_sp_alignment(s);
2568     }
2569     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, size);
2570     tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt, memidx,
2571                                size | MO_ALIGN | s->be_data);
2572 }
2573 
2574 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2575                                       int rn, int size)
2576 {
2577     TCGv_i64 s1 = cpu_reg(s, rs);
2578     TCGv_i64 s2 = cpu_reg(s, rs + 1);
2579     TCGv_i64 t1 = cpu_reg(s, rt);
2580     TCGv_i64 t2 = cpu_reg(s, rt + 1);
2581     TCGv_i64 clean_addr;
2582     int memidx = get_mem_index(s);
2583 
2584     if (rn == 31) {
2585         gen_check_sp_alignment(s);
2586     }
2587 
2588     /* This is a single atomic access, despite the "pair". */
2589     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, size + 1);
2590 
2591     if (size == 2) {
2592         TCGv_i64 cmp = tcg_temp_new_i64();
2593         TCGv_i64 val = tcg_temp_new_i64();
2594 
2595         if (s->be_data == MO_LE) {
2596             tcg_gen_concat32_i64(val, t1, t2);
2597             tcg_gen_concat32_i64(cmp, s1, s2);
2598         } else {
2599             tcg_gen_concat32_i64(val, t2, t1);
2600             tcg_gen_concat32_i64(cmp, s2, s1);
2601         }
2602 
2603         tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx,
2604                                    MO_64 | MO_ALIGN | s->be_data);
2605 
2606         if (s->be_data == MO_LE) {
2607             tcg_gen_extr32_i64(s1, s2, cmp);
2608         } else {
2609             tcg_gen_extr32_i64(s2, s1, cmp);
2610         }
2611     } else {
2612         TCGv_i128 cmp = tcg_temp_new_i128();
2613         TCGv_i128 val = tcg_temp_new_i128();
2614 
2615         if (s->be_data == MO_LE) {
2616             tcg_gen_concat_i64_i128(val, t1, t2);
2617             tcg_gen_concat_i64_i128(cmp, s1, s2);
2618         } else {
2619             tcg_gen_concat_i64_i128(val, t2, t1);
2620             tcg_gen_concat_i64_i128(cmp, s2, s1);
2621         }
2622 
2623         tcg_gen_atomic_cmpxchg_i128(cmp, clean_addr, cmp, val, memidx,
2624                                     MO_128 | MO_ALIGN | s->be_data);
2625 
2626         if (s->be_data == MO_LE) {
2627             tcg_gen_extr_i128_i64(s1, s2, cmp);
2628         } else {
2629             tcg_gen_extr_i128_i64(s2, s1, cmp);
2630         }
2631     }
2632 }
2633 
2634 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
2635  * from the ARMv8 specs for LDR (Shared decode for all encodings).
2636  */
2637 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2638 {
2639     int opc0 = extract32(opc, 0, 1);
2640     int regsize;
2641 
2642     if (is_signed) {
2643         regsize = opc0 ? 32 : 64;
2644     } else {
2645         regsize = size == 3 ? 64 : 32;
2646     }
2647     return regsize == 64;
2648 }
2649 
2650 /* Load/store exclusive
2651  *
2652  *  31 30 29         24  23  22   21  20  16  15  14   10 9    5 4    0
2653  * +-----+-------------+----+---+----+------+----+-------+------+------+
2654  * | sz  | 0 0 1 0 0 0 | o2 | L | o1 |  Rs  | o0 |  Rt2  |  Rn  | Rt   |
2655  * +-----+-------------+----+---+----+------+----+-------+------+------+
2656  *
2657  *  sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2658  *   L: 0 -> store, 1 -> load
2659  *  o2: 0 -> exclusive, 1 -> not
2660  *  o1: 0 -> single register, 1 -> register pair
2661  *  o0: 1 -> load-acquire/store-release, 0 -> not
2662  */
2663 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2664 {
2665     int rt = extract32(insn, 0, 5);
2666     int rn = extract32(insn, 5, 5);
2667     int rt2 = extract32(insn, 10, 5);
2668     int rs = extract32(insn, 16, 5);
2669     int is_lasr = extract32(insn, 15, 1);
2670     int o2_L_o1_o0 = extract32(insn, 21, 3) * 2 | is_lasr;
2671     int size = extract32(insn, 30, 2);
2672     TCGv_i64 clean_addr;
2673 
2674     switch (o2_L_o1_o0) {
2675     case 0x0: /* STXR */
2676     case 0x1: /* STLXR */
2677         if (rn == 31) {
2678             gen_check_sp_alignment(s);
2679         }
2680         if (is_lasr) {
2681             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2682         }
2683         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2684                                     true, rn != 31, size);
2685         gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, false);
2686         return;
2687 
2688     case 0x4: /* LDXR */
2689     case 0x5: /* LDAXR */
2690         if (rn == 31) {
2691             gen_check_sp_alignment(s);
2692         }
2693         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2694                                     false, rn != 31, size);
2695         s->is_ldex = true;
2696         gen_load_exclusive(s, rt, rt2, clean_addr, size, false);
2697         if (is_lasr) {
2698             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2699         }
2700         return;
2701 
2702     case 0x8: /* STLLR */
2703         if (!dc_isar_feature(aa64_lor, s)) {
2704             break;
2705         }
2706         /* StoreLORelease is the same as Store-Release for QEMU.  */
2707         /* fall through */
2708     case 0x9: /* STLR */
2709         /* Generate ISS for non-exclusive accesses including LASR.  */
2710         if (rn == 31) {
2711             gen_check_sp_alignment(s);
2712         }
2713         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2714         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2715                                     true, rn != 31, size);
2716         /* TODO: ARMv8.4-LSE SCTLR.nAA */
2717         do_gpr_st(s, cpu_reg(s, rt), clean_addr, size | MO_ALIGN, true, rt,
2718                   disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2719         return;
2720 
2721     case 0xc: /* LDLAR */
2722         if (!dc_isar_feature(aa64_lor, s)) {
2723             break;
2724         }
2725         /* LoadLOAcquire is the same as Load-Acquire for QEMU.  */
2726         /* fall through */
2727     case 0xd: /* LDAR */
2728         /* Generate ISS for non-exclusive accesses including LASR.  */
2729         if (rn == 31) {
2730             gen_check_sp_alignment(s);
2731         }
2732         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2733                                     false, rn != 31, size);
2734         /* TODO: ARMv8.4-LSE SCTLR.nAA */
2735         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size | MO_ALIGN, false, true,
2736                   rt, disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2737         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2738         return;
2739 
2740     case 0x2: case 0x3: /* CASP / STXP */
2741         if (size & 2) { /* STXP / STLXP */
2742             if (rn == 31) {
2743                 gen_check_sp_alignment(s);
2744             }
2745             if (is_lasr) {
2746                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2747             }
2748             clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2749                                         true, rn != 31, size);
2750             gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, true);
2751             return;
2752         }
2753         if (rt2 == 31
2754             && ((rt | rs) & 1) == 0
2755             && dc_isar_feature(aa64_atomics, s)) {
2756             /* CASP / CASPL */
2757             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2758             return;
2759         }
2760         break;
2761 
2762     case 0x6: case 0x7: /* CASPA / LDXP */
2763         if (size & 2) { /* LDXP / LDAXP */
2764             if (rn == 31) {
2765                 gen_check_sp_alignment(s);
2766             }
2767             clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2768                                         false, rn != 31, size);
2769             s->is_ldex = true;
2770             gen_load_exclusive(s, rt, rt2, clean_addr, size, true);
2771             if (is_lasr) {
2772                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2773             }
2774             return;
2775         }
2776         if (rt2 == 31
2777             && ((rt | rs) & 1) == 0
2778             && dc_isar_feature(aa64_atomics, s)) {
2779             /* CASPA / CASPAL */
2780             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2781             return;
2782         }
2783         break;
2784 
2785     case 0xa: /* CAS */
2786     case 0xb: /* CASL */
2787     case 0xe: /* CASA */
2788     case 0xf: /* CASAL */
2789         if (rt2 == 31 && dc_isar_feature(aa64_atomics, s)) {
2790             gen_compare_and_swap(s, rs, rt, rn, size);
2791             return;
2792         }
2793         break;
2794     }
2795     unallocated_encoding(s);
2796 }
2797 
2798 /*
2799  * Load register (literal)
2800  *
2801  *  31 30 29   27  26 25 24 23                5 4     0
2802  * +-----+-------+---+-----+-------------------+-------+
2803  * | opc | 0 1 1 | V | 0 0 |     imm19         |  Rt   |
2804  * +-----+-------+---+-----+-------------------+-------+
2805  *
2806  * V: 1 -> vector (simd/fp)
2807  * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2808  *                   10-> 32 bit signed, 11 -> prefetch
2809  * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2810  */
2811 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2812 {
2813     int rt = extract32(insn, 0, 5);
2814     int64_t imm = sextract32(insn, 5, 19) << 2;
2815     bool is_vector = extract32(insn, 26, 1);
2816     int opc = extract32(insn, 30, 2);
2817     bool is_signed = false;
2818     int size = 2;
2819     TCGv_i64 tcg_rt, clean_addr;
2820 
2821     if (is_vector) {
2822         if (opc == 3) {
2823             unallocated_encoding(s);
2824             return;
2825         }
2826         size = 2 + opc;
2827         if (!fp_access_check(s)) {
2828             return;
2829         }
2830     } else {
2831         if (opc == 3) {
2832             /* PRFM (literal) : prefetch */
2833             return;
2834         }
2835         size = 2 + extract32(opc, 0, 1);
2836         is_signed = extract32(opc, 1, 1);
2837     }
2838 
2839     tcg_rt = cpu_reg(s, rt);
2840 
2841     clean_addr = tcg_temp_new_i64();
2842     gen_pc_plus_diff(s, clean_addr, imm);
2843     if (is_vector) {
2844         do_fp_ld(s, rt, clean_addr, size);
2845     } else {
2846         /* Only unsigned 32bit loads target 32bit registers.  */
2847         bool iss_sf = opc != 0;
2848 
2849         do_gpr_ld(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
2850                   false, true, rt, iss_sf, false);
2851     }
2852 }
2853 
2854 /*
2855  * LDNP (Load Pair - non-temporal hint)
2856  * LDP (Load Pair - non vector)
2857  * LDPSW (Load Pair Signed Word - non vector)
2858  * STNP (Store Pair - non-temporal hint)
2859  * STP (Store Pair - non vector)
2860  * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2861  * LDP (Load Pair of SIMD&FP)
2862  * STNP (Store Pair of SIMD&FP - non-temporal hint)
2863  * STP (Store Pair of SIMD&FP)
2864  *
2865  *  31 30 29   27  26  25 24   23  22 21   15 14   10 9    5 4    0
2866  * +-----+-------+---+---+-------+---+-----------------------------+
2867  * | opc | 1 0 1 | V | 0 | index | L |  imm7 |  Rt2  |  Rn  | Rt   |
2868  * +-----+-------+---+---+-------+---+-------+-------+------+------+
2869  *
2870  * opc: LDP/STP/LDNP/STNP        00 -> 32 bit, 10 -> 64 bit
2871  *      LDPSW/STGP               01
2872  *      LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2873  *   V: 0 -> GPR, 1 -> Vector
2874  * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2875  *      10 -> signed offset, 11 -> pre-index
2876  *   L: 0 -> Store 1 -> Load
2877  *
2878  * Rt, Rt2 = GPR or SIMD registers to be stored
2879  * Rn = general purpose register containing address
2880  * imm7 = signed offset (multiple of 4 or 8 depending on size)
2881  */
2882 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2883 {
2884     int rt = extract32(insn, 0, 5);
2885     int rn = extract32(insn, 5, 5);
2886     int rt2 = extract32(insn, 10, 5);
2887     uint64_t offset = sextract64(insn, 15, 7);
2888     int index = extract32(insn, 23, 2);
2889     bool is_vector = extract32(insn, 26, 1);
2890     bool is_load = extract32(insn, 22, 1);
2891     int opc = extract32(insn, 30, 2);
2892 
2893     bool is_signed = false;
2894     bool postindex = false;
2895     bool wback = false;
2896     bool set_tag = false;
2897 
2898     TCGv_i64 clean_addr, dirty_addr;
2899 
2900     int size;
2901 
2902     if (opc == 3) {
2903         unallocated_encoding(s);
2904         return;
2905     }
2906 
2907     if (is_vector) {
2908         size = 2 + opc;
2909     } else if (opc == 1 && !is_load) {
2910         /* STGP */
2911         if (!dc_isar_feature(aa64_mte_insn_reg, s) || index == 0) {
2912             unallocated_encoding(s);
2913             return;
2914         }
2915         size = 3;
2916         set_tag = true;
2917     } else {
2918         size = 2 + extract32(opc, 1, 1);
2919         is_signed = extract32(opc, 0, 1);
2920         if (!is_load && is_signed) {
2921             unallocated_encoding(s);
2922             return;
2923         }
2924     }
2925 
2926     switch (index) {
2927     case 1: /* post-index */
2928         postindex = true;
2929         wback = true;
2930         break;
2931     case 0:
2932         /* signed offset with "non-temporal" hint. Since we don't emulate
2933          * caches we don't care about hints to the cache system about
2934          * data access patterns, and handle this identically to plain
2935          * signed offset.
2936          */
2937         if (is_signed) {
2938             /* There is no non-temporal-hint version of LDPSW */
2939             unallocated_encoding(s);
2940             return;
2941         }
2942         postindex = false;
2943         break;
2944     case 2: /* signed offset, rn not updated */
2945         postindex = false;
2946         break;
2947     case 3: /* pre-index */
2948         postindex = false;
2949         wback = true;
2950         break;
2951     }
2952 
2953     if (is_vector && !fp_access_check(s)) {
2954         return;
2955     }
2956 
2957     offset <<= (set_tag ? LOG2_TAG_GRANULE : size);
2958 
2959     if (rn == 31) {
2960         gen_check_sp_alignment(s);
2961     }
2962 
2963     dirty_addr = read_cpu_reg_sp(s, rn, 1);
2964     if (!postindex) {
2965         tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2966     }
2967 
2968     if (set_tag) {
2969         if (!s->ata) {
2970             /*
2971              * TODO: We could rely on the stores below, at least for
2972              * system mode, if we arrange to add MO_ALIGN_16.
2973              */
2974             gen_helper_stg_stub(cpu_env, dirty_addr);
2975         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2976             gen_helper_stg_parallel(cpu_env, dirty_addr, dirty_addr);
2977         } else {
2978             gen_helper_stg(cpu_env, dirty_addr, dirty_addr);
2979         }
2980     }
2981 
2982     clean_addr = gen_mte_checkN(s, dirty_addr, !is_load,
2983                                 (wback || rn != 31) && !set_tag, 2 << size);
2984 
2985     if (is_vector) {
2986         if (is_load) {
2987             do_fp_ld(s, rt, clean_addr, size);
2988         } else {
2989             do_fp_st(s, rt, clean_addr, size);
2990         }
2991         tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2992         if (is_load) {
2993             do_fp_ld(s, rt2, clean_addr, size);
2994         } else {
2995             do_fp_st(s, rt2, clean_addr, size);
2996         }
2997     } else {
2998         TCGv_i64 tcg_rt = cpu_reg(s, rt);
2999         TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
3000 
3001         if (is_load) {
3002             TCGv_i64 tmp = tcg_temp_new_i64();
3003 
3004             /* Do not modify tcg_rt before recognizing any exception
3005              * from the second load.
3006              */
3007             do_gpr_ld(s, tmp, clean_addr, size + is_signed * MO_SIGN,
3008                       false, false, 0, false, false);
3009             tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
3010             do_gpr_ld(s, tcg_rt2, clean_addr, size + is_signed * MO_SIGN,
3011                       false, false, 0, false, false);
3012 
3013             tcg_gen_mov_i64(tcg_rt, tmp);
3014         } else {
3015             do_gpr_st(s, tcg_rt, clean_addr, size,
3016                       false, 0, false, false);
3017             tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
3018             do_gpr_st(s, tcg_rt2, clean_addr, size,
3019                       false, 0, false, false);
3020         }
3021     }
3022 
3023     if (wback) {
3024         if (postindex) {
3025             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3026         }
3027         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3028     }
3029 }
3030 
3031 /*
3032  * Load/store (immediate post-indexed)
3033  * Load/store (immediate pre-indexed)
3034  * Load/store (unscaled immediate)
3035  *
3036  * 31 30 29   27  26 25 24 23 22 21  20    12 11 10 9    5 4    0
3037  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3038  * |size| 1 1 1 | V | 0 0 | opc | 0 |  imm9  | idx |  Rn  |  Rt  |
3039  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3040  *
3041  * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
3042          10 -> unprivileged
3043  * V = 0 -> non-vector
3044  * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
3045  * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3046  */
3047 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
3048                                 int opc,
3049                                 int size,
3050                                 int rt,
3051                                 bool is_vector)
3052 {
3053     int rn = extract32(insn, 5, 5);
3054     int imm9 = sextract32(insn, 12, 9);
3055     int idx = extract32(insn, 10, 2);
3056     bool is_signed = false;
3057     bool is_store = false;
3058     bool is_extended = false;
3059     bool is_unpriv = (idx == 2);
3060     bool iss_valid;
3061     bool post_index;
3062     bool writeback;
3063     int memidx;
3064 
3065     TCGv_i64 clean_addr, dirty_addr;
3066 
3067     if (is_vector) {
3068         size |= (opc & 2) << 1;
3069         if (size > 4 || is_unpriv) {
3070             unallocated_encoding(s);
3071             return;
3072         }
3073         is_store = ((opc & 1) == 0);
3074         if (!fp_access_check(s)) {
3075             return;
3076         }
3077     } else {
3078         if (size == 3 && opc == 2) {
3079             /* PRFM - prefetch */
3080             if (idx != 0) {
3081                 unallocated_encoding(s);
3082                 return;
3083             }
3084             return;
3085         }
3086         if (opc == 3 && size > 1) {
3087             unallocated_encoding(s);
3088             return;
3089         }
3090         is_store = (opc == 0);
3091         is_signed = extract32(opc, 1, 1);
3092         is_extended = (size < 3) && extract32(opc, 0, 1);
3093     }
3094 
3095     switch (idx) {
3096     case 0:
3097     case 2:
3098         post_index = false;
3099         writeback = false;
3100         break;
3101     case 1:
3102         post_index = true;
3103         writeback = true;
3104         break;
3105     case 3:
3106         post_index = false;
3107         writeback = true;
3108         break;
3109     default:
3110         g_assert_not_reached();
3111     }
3112 
3113     iss_valid = !is_vector && !writeback;
3114 
3115     if (rn == 31) {
3116         gen_check_sp_alignment(s);
3117     }
3118 
3119     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3120     if (!post_index) {
3121         tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3122     }
3123 
3124     memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
3125     clean_addr = gen_mte_check1_mmuidx(s, dirty_addr, is_store,
3126                                        writeback || rn != 31,
3127                                        size, is_unpriv, memidx);
3128 
3129     if (is_vector) {
3130         if (is_store) {
3131             do_fp_st(s, rt, clean_addr, size);
3132         } else {
3133             do_fp_ld(s, rt, clean_addr, size);
3134         }
3135     } else {
3136         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3137         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3138 
3139         if (is_store) {
3140             do_gpr_st_memidx(s, tcg_rt, clean_addr, size, memidx,
3141                              iss_valid, rt, iss_sf, false);
3142         } else {
3143             do_gpr_ld_memidx(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
3144                              is_extended, memidx,
3145                              iss_valid, rt, iss_sf, false);
3146         }
3147     }
3148 
3149     if (writeback) {
3150         TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3151         if (post_index) {
3152             tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3153         }
3154         tcg_gen_mov_i64(tcg_rn, dirty_addr);
3155     }
3156 }
3157 
3158 /*
3159  * Load/store (register offset)
3160  *
3161  * 31 30 29   27  26 25 24 23 22 21  20  16 15 13 12 11 10 9  5 4  0
3162  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3163  * |size| 1 1 1 | V | 0 0 | opc | 1 |  Rm  | opt | S| 1 0 | Rn | Rt |
3164  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3165  *
3166  * For non-vector:
3167  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3168  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3169  * For vector:
3170  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3171  *   opc<0>: 0 -> store, 1 -> load
3172  * V: 1 -> vector/simd
3173  * opt: extend encoding (see DecodeRegExtend)
3174  * S: if S=1 then scale (essentially index by sizeof(size))
3175  * Rt: register to transfer into/out of
3176  * Rn: address register or SP for base
3177  * Rm: offset register or ZR for offset
3178  */
3179 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
3180                                    int opc,
3181                                    int size,
3182                                    int rt,
3183                                    bool is_vector)
3184 {
3185     int rn = extract32(insn, 5, 5);
3186     int shift = extract32(insn, 12, 1);
3187     int rm = extract32(insn, 16, 5);
3188     int opt = extract32(insn, 13, 3);
3189     bool is_signed = false;
3190     bool is_store = false;
3191     bool is_extended = false;
3192 
3193     TCGv_i64 tcg_rm, clean_addr, dirty_addr;
3194 
3195     if (extract32(opt, 1, 1) == 0) {
3196         unallocated_encoding(s);
3197         return;
3198     }
3199 
3200     if (is_vector) {
3201         size |= (opc & 2) << 1;
3202         if (size > 4) {
3203             unallocated_encoding(s);
3204             return;
3205         }
3206         is_store = !extract32(opc, 0, 1);
3207         if (!fp_access_check(s)) {
3208             return;
3209         }
3210     } else {
3211         if (size == 3 && opc == 2) {
3212             /* PRFM - prefetch */
3213             return;
3214         }
3215         if (opc == 3 && size > 1) {
3216             unallocated_encoding(s);
3217             return;
3218         }
3219         is_store = (opc == 0);
3220         is_signed = extract32(opc, 1, 1);
3221         is_extended = (size < 3) && extract32(opc, 0, 1);
3222     }
3223 
3224     if (rn == 31) {
3225         gen_check_sp_alignment(s);
3226     }
3227     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3228 
3229     tcg_rm = read_cpu_reg(s, rm, 1);
3230     ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
3231 
3232     tcg_gen_add_i64(dirty_addr, dirty_addr, tcg_rm);
3233     clean_addr = gen_mte_check1(s, dirty_addr, is_store, true, size);
3234 
3235     if (is_vector) {
3236         if (is_store) {
3237             do_fp_st(s, rt, clean_addr, size);
3238         } else {
3239             do_fp_ld(s, rt, clean_addr, size);
3240         }
3241     } else {
3242         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3243         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3244         if (is_store) {
3245             do_gpr_st(s, tcg_rt, clean_addr, size,
3246                       true, rt, iss_sf, false);
3247         } else {
3248             do_gpr_ld(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
3249                       is_extended, true, rt, iss_sf, false);
3250         }
3251     }
3252 }
3253 
3254 /*
3255  * Load/store (unsigned immediate)
3256  *
3257  * 31 30 29   27  26 25 24 23 22 21        10 9     5
3258  * +----+-------+---+-----+-----+------------+-------+------+
3259  * |size| 1 1 1 | V | 0 1 | opc |   imm12    |  Rn   |  Rt  |
3260  * +----+-------+---+-----+-----+------------+-------+------+
3261  *
3262  * For non-vector:
3263  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3264  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3265  * For vector:
3266  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3267  *   opc<0>: 0 -> store, 1 -> load
3268  * Rn: base address register (inc SP)
3269  * Rt: target register
3270  */
3271 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
3272                                         int opc,
3273                                         int size,
3274                                         int rt,
3275                                         bool is_vector)
3276 {
3277     int rn = extract32(insn, 5, 5);
3278     unsigned int imm12 = extract32(insn, 10, 12);
3279     unsigned int offset;
3280 
3281     TCGv_i64 clean_addr, dirty_addr;
3282 
3283     bool is_store;
3284     bool is_signed = false;
3285     bool is_extended = false;
3286 
3287     if (is_vector) {
3288         size |= (opc & 2) << 1;
3289         if (size > 4) {
3290             unallocated_encoding(s);
3291             return;
3292         }
3293         is_store = !extract32(opc, 0, 1);
3294         if (!fp_access_check(s)) {
3295             return;
3296         }
3297     } else {
3298         if (size == 3 && opc == 2) {
3299             /* PRFM - prefetch */
3300             return;
3301         }
3302         if (opc == 3 && size > 1) {
3303             unallocated_encoding(s);
3304             return;
3305         }
3306         is_store = (opc == 0);
3307         is_signed = extract32(opc, 1, 1);
3308         is_extended = (size < 3) && extract32(opc, 0, 1);
3309     }
3310 
3311     if (rn == 31) {
3312         gen_check_sp_alignment(s);
3313     }
3314     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3315     offset = imm12 << size;
3316     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3317     clean_addr = gen_mte_check1(s, dirty_addr, is_store, rn != 31, size);
3318 
3319     if (is_vector) {
3320         if (is_store) {
3321             do_fp_st(s, rt, clean_addr, size);
3322         } else {
3323             do_fp_ld(s, rt, clean_addr, size);
3324         }
3325     } else {
3326         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3327         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3328         if (is_store) {
3329             do_gpr_st(s, tcg_rt, clean_addr, size,
3330                       true, rt, iss_sf, false);
3331         } else {
3332             do_gpr_ld(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
3333                       is_extended, true, rt, iss_sf, false);
3334         }
3335     }
3336 }
3337 
3338 /* Atomic memory operations
3339  *
3340  *  31  30      27  26    24    22  21   16   15    12    10    5     0
3341  * +------+-------+---+-----+-----+---+----+----+-----+-----+----+-----+
3342  * | size | 1 1 1 | V | 0 0 | A R | 1 | Rs | o3 | opc | 0 0 | Rn |  Rt |
3343  * +------+-------+---+-----+-----+--------+----+-----+-----+----+-----+
3344  *
3345  * Rt: the result register
3346  * Rn: base address or SP
3347  * Rs: the source register for the operation
3348  * V: vector flag (always 0 as of v8.3)
3349  * A: acquire flag
3350  * R: release flag
3351  */
3352 static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
3353                               int size, int rt, bool is_vector)
3354 {
3355     int rs = extract32(insn, 16, 5);
3356     int rn = extract32(insn, 5, 5);
3357     int o3_opc = extract32(insn, 12, 4);
3358     bool r = extract32(insn, 22, 1);
3359     bool a = extract32(insn, 23, 1);
3360     TCGv_i64 tcg_rs, tcg_rt, clean_addr;
3361     AtomicThreeOpFn *fn = NULL;
3362     MemOp mop = s->be_data | size | MO_ALIGN;
3363 
3364     if (is_vector || !dc_isar_feature(aa64_atomics, s)) {
3365         unallocated_encoding(s);
3366         return;
3367     }
3368     switch (o3_opc) {
3369     case 000: /* LDADD */
3370         fn = tcg_gen_atomic_fetch_add_i64;
3371         break;
3372     case 001: /* LDCLR */
3373         fn = tcg_gen_atomic_fetch_and_i64;
3374         break;
3375     case 002: /* LDEOR */
3376         fn = tcg_gen_atomic_fetch_xor_i64;
3377         break;
3378     case 003: /* LDSET */
3379         fn = tcg_gen_atomic_fetch_or_i64;
3380         break;
3381     case 004: /* LDSMAX */
3382         fn = tcg_gen_atomic_fetch_smax_i64;
3383         mop |= MO_SIGN;
3384         break;
3385     case 005: /* LDSMIN */
3386         fn = tcg_gen_atomic_fetch_smin_i64;
3387         mop |= MO_SIGN;
3388         break;
3389     case 006: /* LDUMAX */
3390         fn = tcg_gen_atomic_fetch_umax_i64;
3391         break;
3392     case 007: /* LDUMIN */
3393         fn = tcg_gen_atomic_fetch_umin_i64;
3394         break;
3395     case 010: /* SWP */
3396         fn = tcg_gen_atomic_xchg_i64;
3397         break;
3398     case 014: /* LDAPR, LDAPRH, LDAPRB */
3399         if (!dc_isar_feature(aa64_rcpc_8_3, s) ||
3400             rs != 31 || a != 1 || r != 0) {
3401             unallocated_encoding(s);
3402             return;
3403         }
3404         break;
3405     default:
3406         unallocated_encoding(s);
3407         return;
3408     }
3409 
3410     if (rn == 31) {
3411         gen_check_sp_alignment(s);
3412     }
3413     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), false, rn != 31, size);
3414 
3415     if (o3_opc == 014) {
3416         /*
3417          * LDAPR* are a special case because they are a simple load, not a
3418          * fetch-and-do-something op.
3419          * The architectural consistency requirements here are weaker than
3420          * full load-acquire (we only need "load-acquire processor consistent"),
3421          * but we choose to implement them as full LDAQ.
3422          */
3423         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size, false,
3424                   true, rt, disas_ldst_compute_iss_sf(size, false, 0), true);
3425         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3426         return;
3427     }
3428 
3429     tcg_rs = read_cpu_reg(s, rs, true);
3430     tcg_rt = cpu_reg(s, rt);
3431 
3432     if (o3_opc == 1) { /* LDCLR */
3433         tcg_gen_not_i64(tcg_rs, tcg_rs);
3434     }
3435 
3436     /* The tcg atomic primitives are all full barriers.  Therefore we
3437      * can ignore the Acquire and Release bits of this instruction.
3438      */
3439     fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3440 
3441     if ((mop & MO_SIGN) && size != MO_64) {
3442         tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3443     }
3444 }
3445 
3446 /*
3447  * PAC memory operations
3448  *
3449  *  31  30      27  26    24    22  21       12  11  10    5     0
3450  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3451  * | size | 1 1 1 | V | 0 0 | M S | 1 |  imm9  | W | 1 | Rn |  Rt |
3452  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3453  *
3454  * Rt: the result register
3455  * Rn: base address or SP
3456  * V: vector flag (always 0 as of v8.3)
3457  * M: clear for key DA, set for key DB
3458  * W: pre-indexing flag
3459  * S: sign for imm9.
3460  */
3461 static void disas_ldst_pac(DisasContext *s, uint32_t insn,
3462                            int size, int rt, bool is_vector)
3463 {
3464     int rn = extract32(insn, 5, 5);
3465     bool is_wback = extract32(insn, 11, 1);
3466     bool use_key_a = !extract32(insn, 23, 1);
3467     int offset;
3468     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3469 
3470     if (size != 3 || is_vector || !dc_isar_feature(aa64_pauth, s)) {
3471         unallocated_encoding(s);
3472         return;
3473     }
3474 
3475     if (rn == 31) {
3476         gen_check_sp_alignment(s);
3477     }
3478     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3479 
3480     if (s->pauth_active) {
3481         if (use_key_a) {
3482             gen_helper_autda(dirty_addr, cpu_env, dirty_addr,
3483                              tcg_constant_i64(0));
3484         } else {
3485             gen_helper_autdb(dirty_addr, cpu_env, dirty_addr,
3486                              tcg_constant_i64(0));
3487         }
3488     }
3489 
3490     /* Form the 10-bit signed, scaled offset.  */
3491     offset = (extract32(insn, 22, 1) << 9) | extract32(insn, 12, 9);
3492     offset = sextract32(offset << size, 0, 10 + size);
3493     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3494 
3495     /* Note that "clean" and "dirty" here refer to TBI not PAC.  */
3496     clean_addr = gen_mte_check1(s, dirty_addr, false,
3497                                 is_wback || rn != 31, size);
3498 
3499     tcg_rt = cpu_reg(s, rt);
3500     do_gpr_ld(s, tcg_rt, clean_addr, size,
3501               /* extend */ false, /* iss_valid */ !is_wback,
3502               /* iss_srt */ rt, /* iss_sf */ true, /* iss_ar */ false);
3503 
3504     if (is_wback) {
3505         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3506     }
3507 }
3508 
3509 /*
3510  * LDAPR/STLR (unscaled immediate)
3511  *
3512  *  31  30            24    22  21       12    10    5     0
3513  * +------+-------------+-----+---+--------+-----+----+-----+
3514  * | size | 0 1 1 0 0 1 | opc | 0 |  imm9  | 0 0 | Rn |  Rt |
3515  * +------+-------------+-----+---+--------+-----+----+-----+
3516  *
3517  * Rt: source or destination register
3518  * Rn: base register
3519  * imm9: unscaled immediate offset
3520  * opc: 00: STLUR*, 01/10/11: various LDAPUR*
3521  * size: size of load/store
3522  */
3523 static void disas_ldst_ldapr_stlr(DisasContext *s, uint32_t insn)
3524 {
3525     int rt = extract32(insn, 0, 5);
3526     int rn = extract32(insn, 5, 5);
3527     int offset = sextract32(insn, 12, 9);
3528     int opc = extract32(insn, 22, 2);
3529     int size = extract32(insn, 30, 2);
3530     TCGv_i64 clean_addr, dirty_addr;
3531     bool is_store = false;
3532     bool extend = false;
3533     bool iss_sf;
3534     MemOp mop;
3535 
3536     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3537         unallocated_encoding(s);
3538         return;
3539     }
3540 
3541     /* TODO: ARMv8.4-LSE SCTLR.nAA */
3542     mop = size | MO_ALIGN;
3543 
3544     switch (opc) {
3545     case 0: /* STLURB */
3546         is_store = true;
3547         break;
3548     case 1: /* LDAPUR* */
3549         break;
3550     case 2: /* LDAPURS* 64-bit variant */
3551         if (size == 3) {
3552             unallocated_encoding(s);
3553             return;
3554         }
3555         mop |= MO_SIGN;
3556         break;
3557     case 3: /* LDAPURS* 32-bit variant */
3558         if (size > 1) {
3559             unallocated_encoding(s);
3560             return;
3561         }
3562         mop |= MO_SIGN;
3563         extend = true; /* zero-extend 32->64 after signed load */
3564         break;
3565     default:
3566         g_assert_not_reached();
3567     }
3568 
3569     iss_sf = disas_ldst_compute_iss_sf(size, (mop & MO_SIGN) != 0, opc);
3570 
3571     if (rn == 31) {
3572         gen_check_sp_alignment(s);
3573     }
3574 
3575     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3576     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3577     clean_addr = clean_data_tbi(s, dirty_addr);
3578 
3579     if (is_store) {
3580         /* Store-Release semantics */
3581         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3582         do_gpr_st(s, cpu_reg(s, rt), clean_addr, mop, true, rt, iss_sf, true);
3583     } else {
3584         /*
3585          * Load-AcquirePC semantics; we implement as the slightly more
3586          * restrictive Load-Acquire.
3587          */
3588         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop,
3589                   extend, true, rt, iss_sf, true);
3590         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3591     }
3592 }
3593 
3594 /* Load/store register (all forms) */
3595 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
3596 {
3597     int rt = extract32(insn, 0, 5);
3598     int opc = extract32(insn, 22, 2);
3599     bool is_vector = extract32(insn, 26, 1);
3600     int size = extract32(insn, 30, 2);
3601 
3602     switch (extract32(insn, 24, 2)) {
3603     case 0:
3604         if (extract32(insn, 21, 1) == 0) {
3605             /* Load/store register (unscaled immediate)
3606              * Load/store immediate pre/post-indexed
3607              * Load/store register unprivileged
3608              */
3609             disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
3610             return;
3611         }
3612         switch (extract32(insn, 10, 2)) {
3613         case 0:
3614             disas_ldst_atomic(s, insn, size, rt, is_vector);
3615             return;
3616         case 2:
3617             disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
3618             return;
3619         default:
3620             disas_ldst_pac(s, insn, size, rt, is_vector);
3621             return;
3622         }
3623         break;
3624     case 1:
3625         disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
3626         return;
3627     }
3628     unallocated_encoding(s);
3629 }
3630 
3631 /* AdvSIMD load/store multiple structures
3632  *
3633  *  31  30  29           23 22  21         16 15    12 11  10 9    5 4    0
3634  * +---+---+---------------+---+-------------+--------+------+------+------+
3635  * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size |  Rn  |  Rt  |
3636  * +---+---+---------------+---+-------------+--------+------+------+------+
3637  *
3638  * AdvSIMD load/store multiple structures (post-indexed)
3639  *
3640  *  31  30  29           23 22  21  20     16 15    12 11  10 9    5 4    0
3641  * +---+---+---------------+---+---+---------+--------+------+------+------+
3642  * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 |   Rm    | opcode | size |  Rn  |  Rt  |
3643  * +---+---+---------------+---+---+---------+--------+------+------+------+
3644  *
3645  * Rt: first (or only) SIMD&FP register to be transferred
3646  * Rn: base address or SP
3647  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3648  */
3649 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
3650 {
3651     int rt = extract32(insn, 0, 5);
3652     int rn = extract32(insn, 5, 5);
3653     int rm = extract32(insn, 16, 5);
3654     int size = extract32(insn, 10, 2);
3655     int opcode = extract32(insn, 12, 4);
3656     bool is_store = !extract32(insn, 22, 1);
3657     bool is_postidx = extract32(insn, 23, 1);
3658     bool is_q = extract32(insn, 30, 1);
3659     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3660     MemOp endian, align, mop;
3661 
3662     int total;    /* total bytes */
3663     int elements; /* elements per vector */
3664     int rpt;    /* num iterations */
3665     int selem;  /* structure elements */
3666     int r;
3667 
3668     if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
3669         unallocated_encoding(s);
3670         return;
3671     }
3672 
3673     if (!is_postidx && rm != 0) {
3674         unallocated_encoding(s);
3675         return;
3676     }
3677 
3678     /* From the shared decode logic */
3679     switch (opcode) {
3680     case 0x0:
3681         rpt = 1;
3682         selem = 4;
3683         break;
3684     case 0x2:
3685         rpt = 4;
3686         selem = 1;
3687         break;
3688     case 0x4:
3689         rpt = 1;
3690         selem = 3;
3691         break;
3692     case 0x6:
3693         rpt = 3;
3694         selem = 1;
3695         break;
3696     case 0x7:
3697         rpt = 1;
3698         selem = 1;
3699         break;
3700     case 0x8:
3701         rpt = 1;
3702         selem = 2;
3703         break;
3704     case 0xa:
3705         rpt = 2;
3706         selem = 1;
3707         break;
3708     default:
3709         unallocated_encoding(s);
3710         return;
3711     }
3712 
3713     if (size == 3 && !is_q && selem != 1) {
3714         /* reserved */
3715         unallocated_encoding(s);
3716         return;
3717     }
3718 
3719     if (!fp_access_check(s)) {
3720         return;
3721     }
3722 
3723     if (rn == 31) {
3724         gen_check_sp_alignment(s);
3725     }
3726 
3727     /* For our purposes, bytes are always little-endian.  */
3728     endian = s->be_data;
3729     if (size == 0) {
3730         endian = MO_LE;
3731     }
3732 
3733     total = rpt * selem * (is_q ? 16 : 8);
3734     tcg_rn = cpu_reg_sp(s, rn);
3735 
3736     /*
3737      * Issue the MTE check vs the logical repeat count, before we
3738      * promote consecutive little-endian elements below.
3739      */
3740     clean_addr = gen_mte_checkN(s, tcg_rn, is_store, is_postidx || rn != 31,
3741                                 total);
3742 
3743     /*
3744      * Consecutive little-endian elements from a single register
3745      * can be promoted to a larger little-endian operation.
3746      */
3747     align = MO_ALIGN;
3748     if (selem == 1 && endian == MO_LE) {
3749         align = pow2_align(size);
3750         size = 3;
3751     }
3752     if (!s->align_mem) {
3753         align = 0;
3754     }
3755     mop = endian | size | align;
3756 
3757     elements = (is_q ? 16 : 8) >> size;
3758     tcg_ebytes = tcg_constant_i64(1 << size);
3759     for (r = 0; r < rpt; r++) {
3760         int e;
3761         for (e = 0; e < elements; e++) {
3762             int xs;
3763             for (xs = 0; xs < selem; xs++) {
3764                 int tt = (rt + r + xs) % 32;
3765                 if (is_store) {
3766                     do_vec_st(s, tt, e, clean_addr, mop);
3767                 } else {
3768                     do_vec_ld(s, tt, e, clean_addr, mop);
3769                 }
3770                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3771             }
3772         }
3773     }
3774 
3775     if (!is_store) {
3776         /* For non-quad operations, setting a slice of the low
3777          * 64 bits of the register clears the high 64 bits (in
3778          * the ARM ARM pseudocode this is implicit in the fact
3779          * that 'rval' is a 64 bit wide variable).
3780          * For quad operations, we might still need to zero the
3781          * high bits of SVE.
3782          */
3783         for (r = 0; r < rpt * selem; r++) {
3784             int tt = (rt + r) % 32;
3785             clear_vec_high(s, is_q, tt);
3786         }
3787     }
3788 
3789     if (is_postidx) {
3790         if (rm == 31) {
3791             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3792         } else {
3793             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3794         }
3795     }
3796 }
3797 
3798 /* AdvSIMD load/store single structure
3799  *
3800  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3801  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3802  * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size |  Rn  |  Rt  |
3803  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3804  *
3805  * AdvSIMD load/store single structure (post-indexed)
3806  *
3807  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3808  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3809  * | 0 | Q | 0 0 1 1 0 1 1 | L R |     Rm    | opc | S | size |  Rn  |  Rt  |
3810  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3811  *
3812  * Rt: first (or only) SIMD&FP register to be transferred
3813  * Rn: base address or SP
3814  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3815  * index = encoded in Q:S:size dependent on size
3816  *
3817  * lane_size = encoded in R, opc
3818  * transfer width = encoded in opc, S, size
3819  */
3820 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
3821 {
3822     int rt = extract32(insn, 0, 5);
3823     int rn = extract32(insn, 5, 5);
3824     int rm = extract32(insn, 16, 5);
3825     int size = extract32(insn, 10, 2);
3826     int S = extract32(insn, 12, 1);
3827     int opc = extract32(insn, 13, 3);
3828     int R = extract32(insn, 21, 1);
3829     int is_load = extract32(insn, 22, 1);
3830     int is_postidx = extract32(insn, 23, 1);
3831     int is_q = extract32(insn, 30, 1);
3832 
3833     int scale = extract32(opc, 1, 2);
3834     int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
3835     bool replicate = false;
3836     int index = is_q << 3 | S << 2 | size;
3837     int xs, total;
3838     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3839     MemOp mop;
3840 
3841     if (extract32(insn, 31, 1)) {
3842         unallocated_encoding(s);
3843         return;
3844     }
3845     if (!is_postidx && rm != 0) {
3846         unallocated_encoding(s);
3847         return;
3848     }
3849 
3850     switch (scale) {
3851     case 3:
3852         if (!is_load || S) {
3853             unallocated_encoding(s);
3854             return;
3855         }
3856         scale = size;
3857         replicate = true;
3858         break;
3859     case 0:
3860         break;
3861     case 1:
3862         if (extract32(size, 0, 1)) {
3863             unallocated_encoding(s);
3864             return;
3865         }
3866         index >>= 1;
3867         break;
3868     case 2:
3869         if (extract32(size, 1, 1)) {
3870             unallocated_encoding(s);
3871             return;
3872         }
3873         if (!extract32(size, 0, 1)) {
3874             index >>= 2;
3875         } else {
3876             if (S) {
3877                 unallocated_encoding(s);
3878                 return;
3879             }
3880             index >>= 3;
3881             scale = 3;
3882         }
3883         break;
3884     default:
3885         g_assert_not_reached();
3886     }
3887 
3888     if (!fp_access_check(s)) {
3889         return;
3890     }
3891 
3892     if (rn == 31) {
3893         gen_check_sp_alignment(s);
3894     }
3895 
3896     total = selem << scale;
3897     tcg_rn = cpu_reg_sp(s, rn);
3898 
3899     clean_addr = gen_mte_checkN(s, tcg_rn, !is_load, is_postidx || rn != 31,
3900                                 total);
3901     mop = finalize_memop(s, scale);
3902 
3903     tcg_ebytes = tcg_constant_i64(1 << scale);
3904     for (xs = 0; xs < selem; xs++) {
3905         if (replicate) {
3906             /* Load and replicate to all elements */
3907             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3908 
3909             tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
3910             tcg_gen_gvec_dup_i64(scale, vec_full_reg_offset(s, rt),
3911                                  (is_q + 1) * 8, vec_full_reg_size(s),
3912                                  tcg_tmp);
3913         } else {
3914             /* Load/store one element per register */
3915             if (is_load) {
3916                 do_vec_ld(s, rt, index, clean_addr, mop);
3917             } else {
3918                 do_vec_st(s, rt, index, clean_addr, mop);
3919             }
3920         }
3921         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3922         rt = (rt + 1) % 32;
3923     }
3924 
3925     if (is_postidx) {
3926         if (rm == 31) {
3927             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3928         } else {
3929             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3930         }
3931     }
3932 }
3933 
3934 /*
3935  * Load/Store memory tags
3936  *
3937  *  31 30 29         24     22  21     12    10      5      0
3938  * +-----+-------------+-----+---+------+-----+------+------+
3939  * | 1 1 | 0 1 1 0 0 1 | op1 | 1 | imm9 | op2 |  Rn  |  Rt  |
3940  * +-----+-------------+-----+---+------+-----+------+------+
3941  */
3942 static void disas_ldst_tag(DisasContext *s, uint32_t insn)
3943 {
3944     int rt = extract32(insn, 0, 5);
3945     int rn = extract32(insn, 5, 5);
3946     uint64_t offset = sextract64(insn, 12, 9) << LOG2_TAG_GRANULE;
3947     int op2 = extract32(insn, 10, 2);
3948     int op1 = extract32(insn, 22, 2);
3949     bool is_load = false, is_pair = false, is_zero = false, is_mult = false;
3950     int index = 0;
3951     TCGv_i64 addr, clean_addr, tcg_rt;
3952 
3953     /* We checked insn bits [29:24,21] in the caller.  */
3954     if (extract32(insn, 30, 2) != 3) {
3955         goto do_unallocated;
3956     }
3957 
3958     /*
3959      * @index is a tri-state variable which has 3 states:
3960      * < 0 : post-index, writeback
3961      * = 0 : signed offset
3962      * > 0 : pre-index, writeback
3963      */
3964     switch (op1) {
3965     case 0:
3966         if (op2 != 0) {
3967             /* STG */
3968             index = op2 - 2;
3969         } else {
3970             /* STZGM */
3971             if (s->current_el == 0 || offset != 0) {
3972                 goto do_unallocated;
3973             }
3974             is_mult = is_zero = true;
3975         }
3976         break;
3977     case 1:
3978         if (op2 != 0) {
3979             /* STZG */
3980             is_zero = true;
3981             index = op2 - 2;
3982         } else {
3983             /* LDG */
3984             is_load = true;
3985         }
3986         break;
3987     case 2:
3988         if (op2 != 0) {
3989             /* ST2G */
3990             is_pair = true;
3991             index = op2 - 2;
3992         } else {
3993             /* STGM */
3994             if (s->current_el == 0 || offset != 0) {
3995                 goto do_unallocated;
3996             }
3997             is_mult = true;
3998         }
3999         break;
4000     case 3:
4001         if (op2 != 0) {
4002             /* STZ2G */
4003             is_pair = is_zero = true;
4004             index = op2 - 2;
4005         } else {
4006             /* LDGM */
4007             if (s->current_el == 0 || offset != 0) {
4008                 goto do_unallocated;
4009             }
4010             is_mult = is_load = true;
4011         }
4012         break;
4013 
4014     default:
4015     do_unallocated:
4016         unallocated_encoding(s);
4017         return;
4018     }
4019 
4020     if (is_mult
4021         ? !dc_isar_feature(aa64_mte, s)
4022         : !dc_isar_feature(aa64_mte_insn_reg, s)) {
4023         goto do_unallocated;
4024     }
4025 
4026     if (rn == 31) {
4027         gen_check_sp_alignment(s);
4028     }
4029 
4030     addr = read_cpu_reg_sp(s, rn, true);
4031     if (index >= 0) {
4032         /* pre-index or signed offset */
4033         tcg_gen_addi_i64(addr, addr, offset);
4034     }
4035 
4036     if (is_mult) {
4037         tcg_rt = cpu_reg(s, rt);
4038 
4039         if (is_zero) {
4040             int size = 4 << s->dcz_blocksize;
4041 
4042             if (s->ata) {
4043                 gen_helper_stzgm_tags(cpu_env, addr, tcg_rt);
4044             }
4045             /*
4046              * The non-tags portion of STZGM is mostly like DC_ZVA,
4047              * except the alignment happens before the access.
4048              */
4049             clean_addr = clean_data_tbi(s, addr);
4050             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4051             gen_helper_dc_zva(cpu_env, clean_addr);
4052         } else if (s->ata) {
4053             if (is_load) {
4054                 gen_helper_ldgm(tcg_rt, cpu_env, addr);
4055             } else {
4056                 gen_helper_stgm(cpu_env, addr, tcg_rt);
4057             }
4058         } else {
4059             MMUAccessType acc = is_load ? MMU_DATA_LOAD : MMU_DATA_STORE;
4060             int size = 4 << GMID_EL1_BS;
4061 
4062             clean_addr = clean_data_tbi(s, addr);
4063             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4064             gen_probe_access(s, clean_addr, acc, size);
4065 
4066             if (is_load) {
4067                 /* The result tags are zeros.  */
4068                 tcg_gen_movi_i64(tcg_rt, 0);
4069             }
4070         }
4071         return;
4072     }
4073 
4074     if (is_load) {
4075         tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
4076         tcg_rt = cpu_reg(s, rt);
4077         if (s->ata) {
4078             gen_helper_ldg(tcg_rt, cpu_env, addr, tcg_rt);
4079         } else {
4080             clean_addr = clean_data_tbi(s, addr);
4081             gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
4082             gen_address_with_allocation_tag0(tcg_rt, addr);
4083         }
4084     } else {
4085         tcg_rt = cpu_reg_sp(s, rt);
4086         if (!s->ata) {
4087             /*
4088              * For STG and ST2G, we need to check alignment and probe memory.
4089              * TODO: For STZG and STZ2G, we could rely on the stores below,
4090              * at least for system mode; user-only won't enforce alignment.
4091              */
4092             if (is_pair) {
4093                 gen_helper_st2g_stub(cpu_env, addr);
4094             } else {
4095                 gen_helper_stg_stub(cpu_env, addr);
4096             }
4097         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
4098             if (is_pair) {
4099                 gen_helper_st2g_parallel(cpu_env, addr, tcg_rt);
4100             } else {
4101                 gen_helper_stg_parallel(cpu_env, addr, tcg_rt);
4102             }
4103         } else {
4104             if (is_pair) {
4105                 gen_helper_st2g(cpu_env, addr, tcg_rt);
4106             } else {
4107                 gen_helper_stg(cpu_env, addr, tcg_rt);
4108             }
4109         }
4110     }
4111 
4112     if (is_zero) {
4113         TCGv_i64 clean_addr = clean_data_tbi(s, addr);
4114         TCGv_i64 tcg_zero = tcg_constant_i64(0);
4115         int mem_index = get_mem_index(s);
4116         int i, n = (1 + is_pair) << LOG2_TAG_GRANULE;
4117 
4118         tcg_gen_qemu_st_i64(tcg_zero, clean_addr, mem_index,
4119                             MO_UQ | MO_ALIGN_16);
4120         for (i = 8; i < n; i += 8) {
4121             tcg_gen_addi_i64(clean_addr, clean_addr, 8);
4122             tcg_gen_qemu_st_i64(tcg_zero, clean_addr, mem_index, MO_UQ);
4123         }
4124     }
4125 
4126     if (index != 0) {
4127         /* pre-index or post-index */
4128         if (index < 0) {
4129             /* post-index */
4130             tcg_gen_addi_i64(addr, addr, offset);
4131         }
4132         tcg_gen_mov_i64(cpu_reg_sp(s, rn), addr);
4133     }
4134 }
4135 
4136 /* Loads and stores */
4137 static void disas_ldst(DisasContext *s, uint32_t insn)
4138 {
4139     switch (extract32(insn, 24, 6)) {
4140     case 0x08: /* Load/store exclusive */
4141         disas_ldst_excl(s, insn);
4142         break;
4143     case 0x18: case 0x1c: /* Load register (literal) */
4144         disas_ld_lit(s, insn);
4145         break;
4146     case 0x28: case 0x29:
4147     case 0x2c: case 0x2d: /* Load/store pair (all forms) */
4148         disas_ldst_pair(s, insn);
4149         break;
4150     case 0x38: case 0x39:
4151     case 0x3c: case 0x3d: /* Load/store register (all forms) */
4152         disas_ldst_reg(s, insn);
4153         break;
4154     case 0x0c: /* AdvSIMD load/store multiple structures */
4155         disas_ldst_multiple_struct(s, insn);
4156         break;
4157     case 0x0d: /* AdvSIMD load/store single structure */
4158         disas_ldst_single_struct(s, insn);
4159         break;
4160     case 0x19:
4161         if (extract32(insn, 21, 1) != 0) {
4162             disas_ldst_tag(s, insn);
4163         } else if (extract32(insn, 10, 2) == 0) {
4164             disas_ldst_ldapr_stlr(s, insn);
4165         } else {
4166             unallocated_encoding(s);
4167         }
4168         break;
4169     default:
4170         unallocated_encoding(s);
4171         break;
4172     }
4173 }
4174 
4175 /* PC-rel. addressing
4176  *   31  30   29 28       24 23                5 4    0
4177  * +----+-------+-----------+-------------------+------+
4178  * | op | immlo | 1 0 0 0 0 |       immhi       |  Rd  |
4179  * +----+-------+-----------+-------------------+------+
4180  */
4181 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
4182 {
4183     unsigned int page, rd;
4184     int64_t offset;
4185 
4186     page = extract32(insn, 31, 1);
4187     /* SignExtend(immhi:immlo) -> offset */
4188     offset = sextract64(insn, 5, 19);
4189     offset = offset << 2 | extract32(insn, 29, 2);
4190     rd = extract32(insn, 0, 5);
4191 
4192     if (page) {
4193         /* ADRP (page based) */
4194         offset <<= 12;
4195         /* The page offset is ok for CF_PCREL. */
4196         offset -= s->pc_curr & 0xfff;
4197     }
4198 
4199     gen_pc_plus_diff(s, cpu_reg(s, rd), offset);
4200 }
4201 
4202 /*
4203  * Add/subtract (immediate)
4204  *
4205  *  31 30 29 28         23 22 21         10 9   5 4   0
4206  * +--+--+--+-------------+--+-------------+-----+-----+
4207  * |sf|op| S| 1 0 0 0 1 0 |sh|    imm12    |  Rn | Rd  |
4208  * +--+--+--+-------------+--+-------------+-----+-----+
4209  *
4210  *    sf: 0 -> 32bit, 1 -> 64bit
4211  *    op: 0 -> add  , 1 -> sub
4212  *     S: 1 -> set flags
4213  *    sh: 1 -> LSL imm by 12
4214  */
4215 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
4216 {
4217     int rd = extract32(insn, 0, 5);
4218     int rn = extract32(insn, 5, 5);
4219     uint64_t imm = extract32(insn, 10, 12);
4220     bool shift = extract32(insn, 22, 1);
4221     bool setflags = extract32(insn, 29, 1);
4222     bool sub_op = extract32(insn, 30, 1);
4223     bool is_64bit = extract32(insn, 31, 1);
4224 
4225     TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
4226     TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
4227     TCGv_i64 tcg_result;
4228 
4229     if (shift) {
4230         imm <<= 12;
4231     }
4232 
4233     tcg_result = tcg_temp_new_i64();
4234     if (!setflags) {
4235         if (sub_op) {
4236             tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
4237         } else {
4238             tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
4239         }
4240     } else {
4241         TCGv_i64 tcg_imm = tcg_constant_i64(imm);
4242         if (sub_op) {
4243             gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
4244         } else {
4245             gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
4246         }
4247     }
4248 
4249     if (is_64bit) {
4250         tcg_gen_mov_i64(tcg_rd, tcg_result);
4251     } else {
4252         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4253     }
4254 }
4255 
4256 /*
4257  * Add/subtract (immediate, with tags)
4258  *
4259  *  31 30 29 28         23 22 21     16 14      10 9   5 4   0
4260  * +--+--+--+-------------+--+---------+--+-------+-----+-----+
4261  * |sf|op| S| 1 0 0 0 1 1 |o2|  uimm6  |o3| uimm4 |  Rn | Rd  |
4262  * +--+--+--+-------------+--+---------+--+-------+-----+-----+
4263  *
4264  *    op: 0 -> add, 1 -> sub
4265  */
4266 static void disas_add_sub_imm_with_tags(DisasContext *s, uint32_t insn)
4267 {
4268     int rd = extract32(insn, 0, 5);
4269     int rn = extract32(insn, 5, 5);
4270     int uimm4 = extract32(insn, 10, 4);
4271     int uimm6 = extract32(insn, 16, 6);
4272     bool sub_op = extract32(insn, 30, 1);
4273     TCGv_i64 tcg_rn, tcg_rd;
4274     int imm;
4275 
4276     /* Test all of sf=1, S=0, o2=0, o3=0.  */
4277     if ((insn & 0xa040c000u) != 0x80000000u ||
4278         !dc_isar_feature(aa64_mte_insn_reg, s)) {
4279         unallocated_encoding(s);
4280         return;
4281     }
4282 
4283     imm = uimm6 << LOG2_TAG_GRANULE;
4284     if (sub_op) {
4285         imm = -imm;
4286     }
4287 
4288     tcg_rn = cpu_reg_sp(s, rn);
4289     tcg_rd = cpu_reg_sp(s, rd);
4290 
4291     if (s->ata) {
4292         gen_helper_addsubg(tcg_rd, cpu_env, tcg_rn,
4293                            tcg_constant_i32(imm),
4294                            tcg_constant_i32(uimm4));
4295     } else {
4296         tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4297         gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4298     }
4299 }
4300 
4301 /* The input should be a value in the bottom e bits (with higher
4302  * bits zero); returns that value replicated into every element
4303  * of size e in a 64 bit integer.
4304  */
4305 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4306 {
4307     assert(e != 0);
4308     while (e < 64) {
4309         mask |= mask << e;
4310         e *= 2;
4311     }
4312     return mask;
4313 }
4314 
4315 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
4316 static inline uint64_t bitmask64(unsigned int length)
4317 {
4318     assert(length > 0 && length <= 64);
4319     return ~0ULL >> (64 - length);
4320 }
4321 
4322 /* Simplified variant of pseudocode DecodeBitMasks() for the case where we
4323  * only require the wmask. Returns false if the imms/immr/immn are a reserved
4324  * value (ie should cause a guest UNDEF exception), and true if they are
4325  * valid, in which case the decoded bit pattern is written to result.
4326  */
4327 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4328                             unsigned int imms, unsigned int immr)
4329 {
4330     uint64_t mask;
4331     unsigned e, levels, s, r;
4332     int len;
4333 
4334     assert(immn < 2 && imms < 64 && immr < 64);
4335 
4336     /* The bit patterns we create here are 64 bit patterns which
4337      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4338      * 64 bits each. Each element contains the same value: a run
4339      * of between 1 and e-1 non-zero bits, rotated within the
4340      * element by between 0 and e-1 bits.
4341      *
4342      * The element size and run length are encoded into immn (1 bit)
4343      * and imms (6 bits) as follows:
4344      * 64 bit elements: immn = 1, imms = <length of run - 1>
4345      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4346      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4347      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4348      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4349      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4350      * Notice that immn = 0, imms = 11111x is the only combination
4351      * not covered by one of the above options; this is reserved.
4352      * Further, <length of run - 1> all-ones is a reserved pattern.
4353      *
4354      * In all cases the rotation is by immr % e (and immr is 6 bits).
4355      */
4356 
4357     /* First determine the element size */
4358     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4359     if (len < 1) {
4360         /* This is the immn == 0, imms == 0x11111x case */
4361         return false;
4362     }
4363     e = 1 << len;
4364 
4365     levels = e - 1;
4366     s = imms & levels;
4367     r = immr & levels;
4368 
4369     if (s == levels) {
4370         /* <length of run - 1> mustn't be all-ones. */
4371         return false;
4372     }
4373 
4374     /* Create the value of one element: s+1 set bits rotated
4375      * by r within the element (which is e bits wide)...
4376      */
4377     mask = bitmask64(s + 1);
4378     if (r) {
4379         mask = (mask >> r) | (mask << (e - r));
4380         mask &= bitmask64(e);
4381     }
4382     /* ...then replicate the element over the whole 64 bit value */
4383     mask = bitfield_replicate(mask, e);
4384     *result = mask;
4385     return true;
4386 }
4387 
4388 /* Logical (immediate)
4389  *   31  30 29 28         23 22  21  16 15  10 9    5 4    0
4390  * +----+-----+-------------+---+------+------+------+------+
4391  * | sf | opc | 1 0 0 1 0 0 | N | immr | imms |  Rn  |  Rd  |
4392  * +----+-----+-------------+---+------+------+------+------+
4393  */
4394 static void disas_logic_imm(DisasContext *s, uint32_t insn)
4395 {
4396     unsigned int sf, opc, is_n, immr, imms, rn, rd;
4397     TCGv_i64 tcg_rd, tcg_rn;
4398     uint64_t wmask;
4399     bool is_and = false;
4400 
4401     sf = extract32(insn, 31, 1);
4402     opc = extract32(insn, 29, 2);
4403     is_n = extract32(insn, 22, 1);
4404     immr = extract32(insn, 16, 6);
4405     imms = extract32(insn, 10, 6);
4406     rn = extract32(insn, 5, 5);
4407     rd = extract32(insn, 0, 5);
4408 
4409     if (!sf && is_n) {
4410         unallocated_encoding(s);
4411         return;
4412     }
4413 
4414     if (opc == 0x3) { /* ANDS */
4415         tcg_rd = cpu_reg(s, rd);
4416     } else {
4417         tcg_rd = cpu_reg_sp(s, rd);
4418     }
4419     tcg_rn = cpu_reg(s, rn);
4420 
4421     if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
4422         /* some immediate field values are reserved */
4423         unallocated_encoding(s);
4424         return;
4425     }
4426 
4427     if (!sf) {
4428         wmask &= 0xffffffff;
4429     }
4430 
4431     switch (opc) {
4432     case 0x3: /* ANDS */
4433     case 0x0: /* AND */
4434         tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
4435         is_and = true;
4436         break;
4437     case 0x1: /* ORR */
4438         tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
4439         break;
4440     case 0x2: /* EOR */
4441         tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
4442         break;
4443     default:
4444         assert(FALSE); /* must handle all above */
4445         break;
4446     }
4447 
4448     if (!sf && !is_and) {
4449         /* zero extend final result; we know we can skip this for AND
4450          * since the immediate had the high 32 bits clear.
4451          */
4452         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4453     }
4454 
4455     if (opc == 3) { /* ANDS */
4456         gen_logic_CC(sf, tcg_rd);
4457     }
4458 }
4459 
4460 /*
4461  * Move wide (immediate)
4462  *
4463  *  31 30 29 28         23 22 21 20             5 4    0
4464  * +--+-----+-------------+-----+----------------+------+
4465  * |sf| opc | 1 0 0 1 0 1 |  hw |  imm16         |  Rd  |
4466  * +--+-----+-------------+-----+----------------+------+
4467  *
4468  * sf: 0 -> 32 bit, 1 -> 64 bit
4469  * opc: 00 -> N, 10 -> Z, 11 -> K
4470  * hw: shift/16 (0,16, and sf only 32, 48)
4471  */
4472 static void disas_movw_imm(DisasContext *s, uint32_t insn)
4473 {
4474     int rd = extract32(insn, 0, 5);
4475     uint64_t imm = extract32(insn, 5, 16);
4476     int sf = extract32(insn, 31, 1);
4477     int opc = extract32(insn, 29, 2);
4478     int pos = extract32(insn, 21, 2) << 4;
4479     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4480 
4481     if (!sf && (pos >= 32)) {
4482         unallocated_encoding(s);
4483         return;
4484     }
4485 
4486     switch (opc) {
4487     case 0: /* MOVN */
4488     case 2: /* MOVZ */
4489         imm <<= pos;
4490         if (opc == 0) {
4491             imm = ~imm;
4492         }
4493         if (!sf) {
4494             imm &= 0xffffffffu;
4495         }
4496         tcg_gen_movi_i64(tcg_rd, imm);
4497         break;
4498     case 3: /* MOVK */
4499         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_constant_i64(imm), pos, 16);
4500         if (!sf) {
4501             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4502         }
4503         break;
4504     default:
4505         unallocated_encoding(s);
4506         break;
4507     }
4508 }
4509 
4510 /* Bitfield
4511  *   31  30 29 28         23 22  21  16 15  10 9    5 4    0
4512  * +----+-----+-------------+---+------+------+------+------+
4513  * | sf | opc | 1 0 0 1 1 0 | N | immr | imms |  Rn  |  Rd  |
4514  * +----+-----+-------------+---+------+------+------+------+
4515  */
4516 static void disas_bitfield(DisasContext *s, uint32_t insn)
4517 {
4518     unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
4519     TCGv_i64 tcg_rd, tcg_tmp;
4520 
4521     sf = extract32(insn, 31, 1);
4522     opc = extract32(insn, 29, 2);
4523     n = extract32(insn, 22, 1);
4524     ri = extract32(insn, 16, 6);
4525     si = extract32(insn, 10, 6);
4526     rn = extract32(insn, 5, 5);
4527     rd = extract32(insn, 0, 5);
4528     bitsize = sf ? 64 : 32;
4529 
4530     if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
4531         unallocated_encoding(s);
4532         return;
4533     }
4534 
4535     tcg_rd = cpu_reg(s, rd);
4536 
4537     /* Suppress the zero-extend for !sf.  Since RI and SI are constrained
4538        to be smaller than bitsize, we'll never reference data outside the
4539        low 32-bits anyway.  */
4540     tcg_tmp = read_cpu_reg(s, rn, 1);
4541 
4542     /* Recognize simple(r) extractions.  */
4543     if (si >= ri) {
4544         /* Wd<s-r:0> = Wn<s:r> */
4545         len = (si - ri) + 1;
4546         if (opc == 0) { /* SBFM: ASR, SBFX, SXTB, SXTH, SXTW */
4547             tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4548             goto done;
4549         } else if (opc == 2) { /* UBFM: UBFX, LSR, UXTB, UXTH */
4550             tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4551             return;
4552         }
4553         /* opc == 1, BFXIL fall through to deposit */
4554         tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4555         pos = 0;
4556     } else {
4557         /* Handle the ri > si case with a deposit
4558          * Wd<32+s-r,32-r> = Wn<s:0>
4559          */
4560         len = si + 1;
4561         pos = (bitsize - ri) & (bitsize - 1);
4562     }
4563 
4564     if (opc == 0 && len < ri) {
4565         /* SBFM: sign extend the destination field from len to fill
4566            the balance of the word.  Let the deposit below insert all
4567            of those sign bits.  */
4568         tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4569         len = ri;
4570     }
4571 
4572     if (opc == 1) { /* BFM, BFXIL */
4573         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4574     } else {
4575         /* SBFM or UBFM: We start with zero, and we haven't modified
4576            any bits outside bitsize, therefore the zero-extension
4577            below is unneeded.  */
4578         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4579         return;
4580     }
4581 
4582  done:
4583     if (!sf) { /* zero extend final result */
4584         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4585     }
4586 }
4587 
4588 /* Extract
4589  *   31  30  29 28         23 22   21  20  16 15    10 9    5 4    0
4590  * +----+------+-------------+---+----+------+--------+------+------+
4591  * | sf | op21 | 1 0 0 1 1 1 | N | o0 |  Rm  |  imms  |  Rn  |  Rd  |
4592  * +----+------+-------------+---+----+------+--------+------+------+
4593  */
4594 static void disas_extract(DisasContext *s, uint32_t insn)
4595 {
4596     unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
4597 
4598     sf = extract32(insn, 31, 1);
4599     n = extract32(insn, 22, 1);
4600     rm = extract32(insn, 16, 5);
4601     imm = extract32(insn, 10, 6);
4602     rn = extract32(insn, 5, 5);
4603     rd = extract32(insn, 0, 5);
4604     op21 = extract32(insn, 29, 2);
4605     op0 = extract32(insn, 21, 1);
4606     bitsize = sf ? 64 : 32;
4607 
4608     if (sf != n || op21 || op0 || imm >= bitsize) {
4609         unallocated_encoding(s);
4610     } else {
4611         TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4612 
4613         tcg_rd = cpu_reg(s, rd);
4614 
4615         if (unlikely(imm == 0)) {
4616             /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4617              * so an extract from bit 0 is a special case.
4618              */
4619             if (sf) {
4620                 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
4621             } else {
4622                 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
4623             }
4624         } else {
4625             tcg_rm = cpu_reg(s, rm);
4626             tcg_rn = cpu_reg(s, rn);
4627 
4628             if (sf) {
4629                 /* Specialization to ROR happens in EXTRACT2.  */
4630                 tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, imm);
4631             } else {
4632                 TCGv_i32 t0 = tcg_temp_new_i32();
4633 
4634                 tcg_gen_extrl_i64_i32(t0, tcg_rm);
4635                 if (rm == rn) {
4636                     tcg_gen_rotri_i32(t0, t0, imm);
4637                 } else {
4638                     TCGv_i32 t1 = tcg_temp_new_i32();
4639                     tcg_gen_extrl_i64_i32(t1, tcg_rn);
4640                     tcg_gen_extract2_i32(t0, t0, t1, imm);
4641                 }
4642                 tcg_gen_extu_i32_i64(tcg_rd, t0);
4643             }
4644         }
4645     }
4646 }
4647 
4648 /* Data processing - immediate */
4649 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
4650 {
4651     switch (extract32(insn, 23, 6)) {
4652     case 0x20: case 0x21: /* PC-rel. addressing */
4653         disas_pc_rel_adr(s, insn);
4654         break;
4655     case 0x22: /* Add/subtract (immediate) */
4656         disas_add_sub_imm(s, insn);
4657         break;
4658     case 0x23: /* Add/subtract (immediate, with tags) */
4659         disas_add_sub_imm_with_tags(s, insn);
4660         break;
4661     case 0x24: /* Logical (immediate) */
4662         disas_logic_imm(s, insn);
4663         break;
4664     case 0x25: /* Move wide (immediate) */
4665         disas_movw_imm(s, insn);
4666         break;
4667     case 0x26: /* Bitfield */
4668         disas_bitfield(s, insn);
4669         break;
4670     case 0x27: /* Extract */
4671         disas_extract(s, insn);
4672         break;
4673     default:
4674         unallocated_encoding(s);
4675         break;
4676     }
4677 }
4678 
4679 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4680  * Note that it is the caller's responsibility to ensure that the
4681  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4682  * mandated semantics for out of range shifts.
4683  */
4684 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4685                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4686 {
4687     switch (shift_type) {
4688     case A64_SHIFT_TYPE_LSL:
4689         tcg_gen_shl_i64(dst, src, shift_amount);
4690         break;
4691     case A64_SHIFT_TYPE_LSR:
4692         tcg_gen_shr_i64(dst, src, shift_amount);
4693         break;
4694     case A64_SHIFT_TYPE_ASR:
4695         if (!sf) {
4696             tcg_gen_ext32s_i64(dst, src);
4697         }
4698         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4699         break;
4700     case A64_SHIFT_TYPE_ROR:
4701         if (sf) {
4702             tcg_gen_rotr_i64(dst, src, shift_amount);
4703         } else {
4704             TCGv_i32 t0, t1;
4705             t0 = tcg_temp_new_i32();
4706             t1 = tcg_temp_new_i32();
4707             tcg_gen_extrl_i64_i32(t0, src);
4708             tcg_gen_extrl_i64_i32(t1, shift_amount);
4709             tcg_gen_rotr_i32(t0, t0, t1);
4710             tcg_gen_extu_i32_i64(dst, t0);
4711         }
4712         break;
4713     default:
4714         assert(FALSE); /* all shift types should be handled */
4715         break;
4716     }
4717 
4718     if (!sf) { /* zero extend final result */
4719         tcg_gen_ext32u_i64(dst, dst);
4720     }
4721 }
4722 
4723 /* Shift a TCGv src by immediate, put result in dst.
4724  * The shift amount must be in range (this should always be true as the
4725  * relevant instructions will UNDEF on bad shift immediates).
4726  */
4727 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4728                           enum a64_shift_type shift_type, unsigned int shift_i)
4729 {
4730     assert(shift_i < (sf ? 64 : 32));
4731 
4732     if (shift_i == 0) {
4733         tcg_gen_mov_i64(dst, src);
4734     } else {
4735         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
4736     }
4737 }
4738 
4739 /* Logical (shifted register)
4740  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
4741  * +----+-----+-----------+-------+---+------+--------+------+------+
4742  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
4743  * +----+-----+-----------+-------+---+------+--------+------+------+
4744  */
4745 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4746 {
4747     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4748     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4749 
4750     sf = extract32(insn, 31, 1);
4751     opc = extract32(insn, 29, 2);
4752     shift_type = extract32(insn, 22, 2);
4753     invert = extract32(insn, 21, 1);
4754     rm = extract32(insn, 16, 5);
4755     shift_amount = extract32(insn, 10, 6);
4756     rn = extract32(insn, 5, 5);
4757     rd = extract32(insn, 0, 5);
4758 
4759     if (!sf && (shift_amount & (1 << 5))) {
4760         unallocated_encoding(s);
4761         return;
4762     }
4763 
4764     tcg_rd = cpu_reg(s, rd);
4765 
4766     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4767         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4768          * register-register MOV and MVN, so it is worth special casing.
4769          */
4770         tcg_rm = cpu_reg(s, rm);
4771         if (invert) {
4772             tcg_gen_not_i64(tcg_rd, tcg_rm);
4773             if (!sf) {
4774                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4775             }
4776         } else {
4777             if (sf) {
4778                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4779             } else {
4780                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4781             }
4782         }
4783         return;
4784     }
4785 
4786     tcg_rm = read_cpu_reg(s, rm, sf);
4787 
4788     if (shift_amount) {
4789         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4790     }
4791 
4792     tcg_rn = cpu_reg(s, rn);
4793 
4794     switch (opc | (invert << 2)) {
4795     case 0: /* AND */
4796     case 3: /* ANDS */
4797         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4798         break;
4799     case 1: /* ORR */
4800         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4801         break;
4802     case 2: /* EOR */
4803         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4804         break;
4805     case 4: /* BIC */
4806     case 7: /* BICS */
4807         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4808         break;
4809     case 5: /* ORN */
4810         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4811         break;
4812     case 6: /* EON */
4813         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4814         break;
4815     default:
4816         assert(FALSE);
4817         break;
4818     }
4819 
4820     if (!sf) {
4821         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4822     }
4823 
4824     if (opc == 3) {
4825         gen_logic_CC(sf, tcg_rd);
4826     }
4827 }
4828 
4829 /*
4830  * Add/subtract (extended register)
4831  *
4832  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
4833  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4834  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
4835  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4836  *
4837  *  sf: 0 -> 32bit, 1 -> 64bit
4838  *  op: 0 -> add  , 1 -> sub
4839  *   S: 1 -> set flags
4840  * opt: 00
4841  * option: extension type (see DecodeRegExtend)
4842  * imm3: optional shift to Rm
4843  *
4844  * Rd = Rn + LSL(extend(Rm), amount)
4845  */
4846 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4847 {
4848     int rd = extract32(insn, 0, 5);
4849     int rn = extract32(insn, 5, 5);
4850     int imm3 = extract32(insn, 10, 3);
4851     int option = extract32(insn, 13, 3);
4852     int rm = extract32(insn, 16, 5);
4853     int opt = extract32(insn, 22, 2);
4854     bool setflags = extract32(insn, 29, 1);
4855     bool sub_op = extract32(insn, 30, 1);
4856     bool sf = extract32(insn, 31, 1);
4857 
4858     TCGv_i64 tcg_rm, tcg_rn; /* temps */
4859     TCGv_i64 tcg_rd;
4860     TCGv_i64 tcg_result;
4861 
4862     if (imm3 > 4 || opt != 0) {
4863         unallocated_encoding(s);
4864         return;
4865     }
4866 
4867     /* non-flag setting ops may use SP */
4868     if (!setflags) {
4869         tcg_rd = cpu_reg_sp(s, rd);
4870     } else {
4871         tcg_rd = cpu_reg(s, rd);
4872     }
4873     tcg_rn = read_cpu_reg_sp(s, rn, sf);
4874 
4875     tcg_rm = read_cpu_reg(s, rm, sf);
4876     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4877 
4878     tcg_result = tcg_temp_new_i64();
4879 
4880     if (!setflags) {
4881         if (sub_op) {
4882             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4883         } else {
4884             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4885         }
4886     } else {
4887         if (sub_op) {
4888             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4889         } else {
4890             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4891         }
4892     }
4893 
4894     if (sf) {
4895         tcg_gen_mov_i64(tcg_rd, tcg_result);
4896     } else {
4897         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4898     }
4899 }
4900 
4901 /*
4902  * Add/subtract (shifted register)
4903  *
4904  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
4905  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4906  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
4907  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4908  *
4909  *    sf: 0 -> 32bit, 1 -> 64bit
4910  *    op: 0 -> add  , 1 -> sub
4911  *     S: 1 -> set flags
4912  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4913  *  imm6: Shift amount to apply to Rm before the add/sub
4914  */
4915 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4916 {
4917     int rd = extract32(insn, 0, 5);
4918     int rn = extract32(insn, 5, 5);
4919     int imm6 = extract32(insn, 10, 6);
4920     int rm = extract32(insn, 16, 5);
4921     int shift_type = extract32(insn, 22, 2);
4922     bool setflags = extract32(insn, 29, 1);
4923     bool sub_op = extract32(insn, 30, 1);
4924     bool sf = extract32(insn, 31, 1);
4925 
4926     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4927     TCGv_i64 tcg_rn, tcg_rm;
4928     TCGv_i64 tcg_result;
4929 
4930     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4931         unallocated_encoding(s);
4932         return;
4933     }
4934 
4935     tcg_rn = read_cpu_reg(s, rn, sf);
4936     tcg_rm = read_cpu_reg(s, rm, sf);
4937 
4938     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4939 
4940     tcg_result = tcg_temp_new_i64();
4941 
4942     if (!setflags) {
4943         if (sub_op) {
4944             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4945         } else {
4946             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4947         }
4948     } else {
4949         if (sub_op) {
4950             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4951         } else {
4952             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4953         }
4954     }
4955 
4956     if (sf) {
4957         tcg_gen_mov_i64(tcg_rd, tcg_result);
4958     } else {
4959         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4960     }
4961 }
4962 
4963 /* Data-processing (3 source)
4964  *
4965  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
4966  *  +--+------+-----------+------+------+----+------+------+------+
4967  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
4968  *  +--+------+-----------+------+------+----+------+------+------+
4969  */
4970 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4971 {
4972     int rd = extract32(insn, 0, 5);
4973     int rn = extract32(insn, 5, 5);
4974     int ra = extract32(insn, 10, 5);
4975     int rm = extract32(insn, 16, 5);
4976     int op_id = (extract32(insn, 29, 3) << 4) |
4977         (extract32(insn, 21, 3) << 1) |
4978         extract32(insn, 15, 1);
4979     bool sf = extract32(insn, 31, 1);
4980     bool is_sub = extract32(op_id, 0, 1);
4981     bool is_high = extract32(op_id, 2, 1);
4982     bool is_signed = false;
4983     TCGv_i64 tcg_op1;
4984     TCGv_i64 tcg_op2;
4985     TCGv_i64 tcg_tmp;
4986 
4987     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
4988     switch (op_id) {
4989     case 0x42: /* SMADDL */
4990     case 0x43: /* SMSUBL */
4991     case 0x44: /* SMULH */
4992         is_signed = true;
4993         break;
4994     case 0x0: /* MADD (32bit) */
4995     case 0x1: /* MSUB (32bit) */
4996     case 0x40: /* MADD (64bit) */
4997     case 0x41: /* MSUB (64bit) */
4998     case 0x4a: /* UMADDL */
4999     case 0x4b: /* UMSUBL */
5000     case 0x4c: /* UMULH */
5001         break;
5002     default:
5003         unallocated_encoding(s);
5004         return;
5005     }
5006 
5007     if (is_high) {
5008         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
5009         TCGv_i64 tcg_rd = cpu_reg(s, rd);
5010         TCGv_i64 tcg_rn = cpu_reg(s, rn);
5011         TCGv_i64 tcg_rm = cpu_reg(s, rm);
5012 
5013         if (is_signed) {
5014             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5015         } else {
5016             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5017         }
5018         return;
5019     }
5020 
5021     tcg_op1 = tcg_temp_new_i64();
5022     tcg_op2 = tcg_temp_new_i64();
5023     tcg_tmp = tcg_temp_new_i64();
5024 
5025     if (op_id < 0x42) {
5026         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
5027         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
5028     } else {
5029         if (is_signed) {
5030             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
5031             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
5032         } else {
5033             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
5034             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
5035         }
5036     }
5037 
5038     if (ra == 31 && !is_sub) {
5039         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
5040         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
5041     } else {
5042         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
5043         if (is_sub) {
5044             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5045         } else {
5046             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5047         }
5048     }
5049 
5050     if (!sf) {
5051         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
5052     }
5053 }
5054 
5055 /* Add/subtract (with carry)
5056  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
5057  * +--+--+--+------------------------+------+-------------+------+-----+
5058  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
5059  * +--+--+--+------------------------+------+-------------+------+-----+
5060  */
5061 
5062 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
5063 {
5064     unsigned int sf, op, setflags, rm, rn, rd;
5065     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
5066 
5067     sf = extract32(insn, 31, 1);
5068     op = extract32(insn, 30, 1);
5069     setflags = extract32(insn, 29, 1);
5070     rm = extract32(insn, 16, 5);
5071     rn = extract32(insn, 5, 5);
5072     rd = extract32(insn, 0, 5);
5073 
5074     tcg_rd = cpu_reg(s, rd);
5075     tcg_rn = cpu_reg(s, rn);
5076 
5077     if (op) {
5078         tcg_y = tcg_temp_new_i64();
5079         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
5080     } else {
5081         tcg_y = cpu_reg(s, rm);
5082     }
5083 
5084     if (setflags) {
5085         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
5086     } else {
5087         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
5088     }
5089 }
5090 
5091 /*
5092  * Rotate right into flags
5093  *  31 30 29                21       15          10      5  4      0
5094  * +--+--+--+-----------------+--------+-----------+------+--+------+
5095  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
5096  * +--+--+--+-----------------+--------+-----------+------+--+------+
5097  */
5098 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
5099 {
5100     int mask = extract32(insn, 0, 4);
5101     int o2 = extract32(insn, 4, 1);
5102     int rn = extract32(insn, 5, 5);
5103     int imm6 = extract32(insn, 15, 6);
5104     int sf_op_s = extract32(insn, 29, 3);
5105     TCGv_i64 tcg_rn;
5106     TCGv_i32 nzcv;
5107 
5108     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
5109         unallocated_encoding(s);
5110         return;
5111     }
5112 
5113     tcg_rn = read_cpu_reg(s, rn, 1);
5114     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
5115 
5116     nzcv = tcg_temp_new_i32();
5117     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
5118 
5119     if (mask & 8) { /* N */
5120         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
5121     }
5122     if (mask & 4) { /* Z */
5123         tcg_gen_not_i32(cpu_ZF, nzcv);
5124         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
5125     }
5126     if (mask & 2) { /* C */
5127         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
5128     }
5129     if (mask & 1) { /* V */
5130         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
5131     }
5132 }
5133 
5134 /*
5135  * Evaluate into flags
5136  *  31 30 29                21        15   14        10      5  4      0
5137  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5138  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
5139  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5140  */
5141 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
5142 {
5143     int o3_mask = extract32(insn, 0, 5);
5144     int rn = extract32(insn, 5, 5);
5145     int o2 = extract32(insn, 15, 6);
5146     int sz = extract32(insn, 14, 1);
5147     int sf_op_s = extract32(insn, 29, 3);
5148     TCGv_i32 tmp;
5149     int shift;
5150 
5151     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
5152         !dc_isar_feature(aa64_condm_4, s)) {
5153         unallocated_encoding(s);
5154         return;
5155     }
5156     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
5157 
5158     tmp = tcg_temp_new_i32();
5159     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
5160     tcg_gen_shli_i32(cpu_NF, tmp, shift);
5161     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
5162     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
5163     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
5164 }
5165 
5166 /* Conditional compare (immediate / register)
5167  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
5168  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5169  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
5170  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5171  *        [1]                             y                [0]       [0]
5172  */
5173 static void disas_cc(DisasContext *s, uint32_t insn)
5174 {
5175     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
5176     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
5177     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
5178     DisasCompare c;
5179 
5180     if (!extract32(insn, 29, 1)) {
5181         unallocated_encoding(s);
5182         return;
5183     }
5184     if (insn & (1 << 10 | 1 << 4)) {
5185         unallocated_encoding(s);
5186         return;
5187     }
5188     sf = extract32(insn, 31, 1);
5189     op = extract32(insn, 30, 1);
5190     is_imm = extract32(insn, 11, 1);
5191     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
5192     cond = extract32(insn, 12, 4);
5193     rn = extract32(insn, 5, 5);
5194     nzcv = extract32(insn, 0, 4);
5195 
5196     /* Set T0 = !COND.  */
5197     tcg_t0 = tcg_temp_new_i32();
5198     arm_test_cc(&c, cond);
5199     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
5200 
5201     /* Load the arguments for the new comparison.  */
5202     if (is_imm) {
5203         tcg_y = tcg_temp_new_i64();
5204         tcg_gen_movi_i64(tcg_y, y);
5205     } else {
5206         tcg_y = cpu_reg(s, y);
5207     }
5208     tcg_rn = cpu_reg(s, rn);
5209 
5210     /* Set the flags for the new comparison.  */
5211     tcg_tmp = tcg_temp_new_i64();
5212     if (op) {
5213         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5214     } else {
5215         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5216     }
5217 
5218     /* If COND was false, force the flags to #nzcv.  Compute two masks
5219      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
5220      * For tcg hosts that support ANDC, we can make do with just T1.
5221      * In either case, allow the tcg optimizer to delete any unused mask.
5222      */
5223     tcg_t1 = tcg_temp_new_i32();
5224     tcg_t2 = tcg_temp_new_i32();
5225     tcg_gen_neg_i32(tcg_t1, tcg_t0);
5226     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
5227 
5228     if (nzcv & 8) { /* N */
5229         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
5230     } else {
5231         if (TCG_TARGET_HAS_andc_i32) {
5232             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
5233         } else {
5234             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
5235         }
5236     }
5237     if (nzcv & 4) { /* Z */
5238         if (TCG_TARGET_HAS_andc_i32) {
5239             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
5240         } else {
5241             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
5242         }
5243     } else {
5244         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
5245     }
5246     if (nzcv & 2) { /* C */
5247         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
5248     } else {
5249         if (TCG_TARGET_HAS_andc_i32) {
5250             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
5251         } else {
5252             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
5253         }
5254     }
5255     if (nzcv & 1) { /* V */
5256         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
5257     } else {
5258         if (TCG_TARGET_HAS_andc_i32) {
5259             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
5260         } else {
5261             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
5262         }
5263     }
5264 }
5265 
5266 /* Conditional select
5267  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
5268  * +----+----+---+-----------------+------+------+-----+------+------+
5269  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
5270  * +----+----+---+-----------------+------+------+-----+------+------+
5271  */
5272 static void disas_cond_select(DisasContext *s, uint32_t insn)
5273 {
5274     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
5275     TCGv_i64 tcg_rd, zero;
5276     DisasCompare64 c;
5277 
5278     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
5279         /* S == 1 or op2<1> == 1 */
5280         unallocated_encoding(s);
5281         return;
5282     }
5283     sf = extract32(insn, 31, 1);
5284     else_inv = extract32(insn, 30, 1);
5285     rm = extract32(insn, 16, 5);
5286     cond = extract32(insn, 12, 4);
5287     else_inc = extract32(insn, 10, 1);
5288     rn = extract32(insn, 5, 5);
5289     rd = extract32(insn, 0, 5);
5290 
5291     tcg_rd = cpu_reg(s, rd);
5292 
5293     a64_test_cc(&c, cond);
5294     zero = tcg_constant_i64(0);
5295 
5296     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
5297         /* CSET & CSETM.  */
5298         tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
5299         if (else_inv) {
5300             tcg_gen_neg_i64(tcg_rd, tcg_rd);
5301         }
5302     } else {
5303         TCGv_i64 t_true = cpu_reg(s, rn);
5304         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
5305         if (else_inv && else_inc) {
5306             tcg_gen_neg_i64(t_false, t_false);
5307         } else if (else_inv) {
5308             tcg_gen_not_i64(t_false, t_false);
5309         } else if (else_inc) {
5310             tcg_gen_addi_i64(t_false, t_false, 1);
5311         }
5312         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
5313     }
5314 
5315     if (!sf) {
5316         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5317     }
5318 }
5319 
5320 static void handle_clz(DisasContext *s, unsigned int sf,
5321                        unsigned int rn, unsigned int rd)
5322 {
5323     TCGv_i64 tcg_rd, tcg_rn;
5324     tcg_rd = cpu_reg(s, rd);
5325     tcg_rn = cpu_reg(s, rn);
5326 
5327     if (sf) {
5328         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
5329     } else {
5330         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5331         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5332         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
5333         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5334     }
5335 }
5336 
5337 static void handle_cls(DisasContext *s, unsigned int sf,
5338                        unsigned int rn, unsigned int rd)
5339 {
5340     TCGv_i64 tcg_rd, tcg_rn;
5341     tcg_rd = cpu_reg(s, rd);
5342     tcg_rn = cpu_reg(s, rn);
5343 
5344     if (sf) {
5345         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
5346     } else {
5347         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5348         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5349         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
5350         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5351     }
5352 }
5353 
5354 static void handle_rbit(DisasContext *s, unsigned int sf,
5355                         unsigned int rn, unsigned int rd)
5356 {
5357     TCGv_i64 tcg_rd, tcg_rn;
5358     tcg_rd = cpu_reg(s, rd);
5359     tcg_rn = cpu_reg(s, rn);
5360 
5361     if (sf) {
5362         gen_helper_rbit64(tcg_rd, tcg_rn);
5363     } else {
5364         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5365         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5366         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5367         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5368     }
5369 }
5370 
5371 /* REV with sf==1, opcode==3 ("REV64") */
5372 static void handle_rev64(DisasContext *s, unsigned int sf,
5373                          unsigned int rn, unsigned int rd)
5374 {
5375     if (!sf) {
5376         unallocated_encoding(s);
5377         return;
5378     }
5379     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5380 }
5381 
5382 /* REV with sf==0, opcode==2
5383  * REV32 (sf==1, opcode==2)
5384  */
5385 static void handle_rev32(DisasContext *s, unsigned int sf,
5386                          unsigned int rn, unsigned int rd)
5387 {
5388     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5389     TCGv_i64 tcg_rn = cpu_reg(s, rn);
5390 
5391     if (sf) {
5392         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
5393         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
5394     } else {
5395         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
5396     }
5397 }
5398 
5399 /* REV16 (opcode==1) */
5400 static void handle_rev16(DisasContext *s, unsigned int sf,
5401                          unsigned int rn, unsigned int rd)
5402 {
5403     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5404     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5405     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5406     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5407 
5408     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5409     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5410     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5411     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5412     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5413 }
5414 
5415 /* Data-processing (1 source)
5416  *   31  30  29  28             21 20     16 15    10 9    5 4    0
5417  * +----+---+---+-----------------+---------+--------+------+------+
5418  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
5419  * +----+---+---+-----------------+---------+--------+------+------+
5420  */
5421 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5422 {
5423     unsigned int sf, opcode, opcode2, rn, rd;
5424     TCGv_i64 tcg_rd;
5425 
5426     if (extract32(insn, 29, 1)) {
5427         unallocated_encoding(s);
5428         return;
5429     }
5430 
5431     sf = extract32(insn, 31, 1);
5432     opcode = extract32(insn, 10, 6);
5433     opcode2 = extract32(insn, 16, 5);
5434     rn = extract32(insn, 5, 5);
5435     rd = extract32(insn, 0, 5);
5436 
5437 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5438 
5439     switch (MAP(sf, opcode2, opcode)) {
5440     case MAP(0, 0x00, 0x00): /* RBIT */
5441     case MAP(1, 0x00, 0x00):
5442         handle_rbit(s, sf, rn, rd);
5443         break;
5444     case MAP(0, 0x00, 0x01): /* REV16 */
5445     case MAP(1, 0x00, 0x01):
5446         handle_rev16(s, sf, rn, rd);
5447         break;
5448     case MAP(0, 0x00, 0x02): /* REV/REV32 */
5449     case MAP(1, 0x00, 0x02):
5450         handle_rev32(s, sf, rn, rd);
5451         break;
5452     case MAP(1, 0x00, 0x03): /* REV64 */
5453         handle_rev64(s, sf, rn, rd);
5454         break;
5455     case MAP(0, 0x00, 0x04): /* CLZ */
5456     case MAP(1, 0x00, 0x04):
5457         handle_clz(s, sf, rn, rd);
5458         break;
5459     case MAP(0, 0x00, 0x05): /* CLS */
5460     case MAP(1, 0x00, 0x05):
5461         handle_cls(s, sf, rn, rd);
5462         break;
5463     case MAP(1, 0x01, 0x00): /* PACIA */
5464         if (s->pauth_active) {
5465             tcg_rd = cpu_reg(s, rd);
5466             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5467         } else if (!dc_isar_feature(aa64_pauth, s)) {
5468             goto do_unallocated;
5469         }
5470         break;
5471     case MAP(1, 0x01, 0x01): /* PACIB */
5472         if (s->pauth_active) {
5473             tcg_rd = cpu_reg(s, rd);
5474             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5475         } else if (!dc_isar_feature(aa64_pauth, s)) {
5476             goto do_unallocated;
5477         }
5478         break;
5479     case MAP(1, 0x01, 0x02): /* PACDA */
5480         if (s->pauth_active) {
5481             tcg_rd = cpu_reg(s, rd);
5482             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5483         } else if (!dc_isar_feature(aa64_pauth, s)) {
5484             goto do_unallocated;
5485         }
5486         break;
5487     case MAP(1, 0x01, 0x03): /* PACDB */
5488         if (s->pauth_active) {
5489             tcg_rd = cpu_reg(s, rd);
5490             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5491         } else if (!dc_isar_feature(aa64_pauth, s)) {
5492             goto do_unallocated;
5493         }
5494         break;
5495     case MAP(1, 0x01, 0x04): /* AUTIA */
5496         if (s->pauth_active) {
5497             tcg_rd = cpu_reg(s, rd);
5498             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5499         } else if (!dc_isar_feature(aa64_pauth, s)) {
5500             goto do_unallocated;
5501         }
5502         break;
5503     case MAP(1, 0x01, 0x05): /* AUTIB */
5504         if (s->pauth_active) {
5505             tcg_rd = cpu_reg(s, rd);
5506             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5507         } else if (!dc_isar_feature(aa64_pauth, s)) {
5508             goto do_unallocated;
5509         }
5510         break;
5511     case MAP(1, 0x01, 0x06): /* AUTDA */
5512         if (s->pauth_active) {
5513             tcg_rd = cpu_reg(s, rd);
5514             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5515         } else if (!dc_isar_feature(aa64_pauth, s)) {
5516             goto do_unallocated;
5517         }
5518         break;
5519     case MAP(1, 0x01, 0x07): /* AUTDB */
5520         if (s->pauth_active) {
5521             tcg_rd = cpu_reg(s, rd);
5522             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5523         } else if (!dc_isar_feature(aa64_pauth, s)) {
5524             goto do_unallocated;
5525         }
5526         break;
5527     case MAP(1, 0x01, 0x08): /* PACIZA */
5528         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5529             goto do_unallocated;
5530         } else if (s->pauth_active) {
5531             tcg_rd = cpu_reg(s, rd);
5532             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5533         }
5534         break;
5535     case MAP(1, 0x01, 0x09): /* PACIZB */
5536         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5537             goto do_unallocated;
5538         } else if (s->pauth_active) {
5539             tcg_rd = cpu_reg(s, rd);
5540             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5541         }
5542         break;
5543     case MAP(1, 0x01, 0x0a): /* PACDZA */
5544         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5545             goto do_unallocated;
5546         } else if (s->pauth_active) {
5547             tcg_rd = cpu_reg(s, rd);
5548             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5549         }
5550         break;
5551     case MAP(1, 0x01, 0x0b): /* PACDZB */
5552         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5553             goto do_unallocated;
5554         } else if (s->pauth_active) {
5555             tcg_rd = cpu_reg(s, rd);
5556             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5557         }
5558         break;
5559     case MAP(1, 0x01, 0x0c): /* AUTIZA */
5560         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5561             goto do_unallocated;
5562         } else if (s->pauth_active) {
5563             tcg_rd = cpu_reg(s, rd);
5564             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5565         }
5566         break;
5567     case MAP(1, 0x01, 0x0d): /* AUTIZB */
5568         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5569             goto do_unallocated;
5570         } else if (s->pauth_active) {
5571             tcg_rd = cpu_reg(s, rd);
5572             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5573         }
5574         break;
5575     case MAP(1, 0x01, 0x0e): /* AUTDZA */
5576         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5577             goto do_unallocated;
5578         } else if (s->pauth_active) {
5579             tcg_rd = cpu_reg(s, rd);
5580             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5581         }
5582         break;
5583     case MAP(1, 0x01, 0x0f): /* AUTDZB */
5584         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5585             goto do_unallocated;
5586         } else if (s->pauth_active) {
5587             tcg_rd = cpu_reg(s, rd);
5588             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5589         }
5590         break;
5591     case MAP(1, 0x01, 0x10): /* XPACI */
5592         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5593             goto do_unallocated;
5594         } else if (s->pauth_active) {
5595             tcg_rd = cpu_reg(s, rd);
5596             gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
5597         }
5598         break;
5599     case MAP(1, 0x01, 0x11): /* XPACD */
5600         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5601             goto do_unallocated;
5602         } else if (s->pauth_active) {
5603             tcg_rd = cpu_reg(s, rd);
5604             gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5605         }
5606         break;
5607     default:
5608     do_unallocated:
5609         unallocated_encoding(s);
5610         break;
5611     }
5612 
5613 #undef MAP
5614 }
5615 
5616 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5617                        unsigned int rm, unsigned int rn, unsigned int rd)
5618 {
5619     TCGv_i64 tcg_n, tcg_m, tcg_rd;
5620     tcg_rd = cpu_reg(s, rd);
5621 
5622     if (!sf && is_signed) {
5623         tcg_n = tcg_temp_new_i64();
5624         tcg_m = tcg_temp_new_i64();
5625         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5626         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5627     } else {
5628         tcg_n = read_cpu_reg(s, rn, sf);
5629         tcg_m = read_cpu_reg(s, rm, sf);
5630     }
5631 
5632     if (is_signed) {
5633         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5634     } else {
5635         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5636     }
5637 
5638     if (!sf) { /* zero extend final result */
5639         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5640     }
5641 }
5642 
5643 /* LSLV, LSRV, ASRV, RORV */
5644 static void handle_shift_reg(DisasContext *s,
5645                              enum a64_shift_type shift_type, unsigned int sf,
5646                              unsigned int rm, unsigned int rn, unsigned int rd)
5647 {
5648     TCGv_i64 tcg_shift = tcg_temp_new_i64();
5649     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5650     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5651 
5652     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5653     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5654 }
5655 
5656 /* CRC32[BHWX], CRC32C[BHWX] */
5657 static void handle_crc32(DisasContext *s,
5658                          unsigned int sf, unsigned int sz, bool crc32c,
5659                          unsigned int rm, unsigned int rn, unsigned int rd)
5660 {
5661     TCGv_i64 tcg_acc, tcg_val;
5662     TCGv_i32 tcg_bytes;
5663 
5664     if (!dc_isar_feature(aa64_crc32, s)
5665         || (sf == 1 && sz != 3)
5666         || (sf == 0 && sz == 3)) {
5667         unallocated_encoding(s);
5668         return;
5669     }
5670 
5671     if (sz == 3) {
5672         tcg_val = cpu_reg(s, rm);
5673     } else {
5674         uint64_t mask;
5675         switch (sz) {
5676         case 0:
5677             mask = 0xFF;
5678             break;
5679         case 1:
5680             mask = 0xFFFF;
5681             break;
5682         case 2:
5683             mask = 0xFFFFFFFF;
5684             break;
5685         default:
5686             g_assert_not_reached();
5687         }
5688         tcg_val = tcg_temp_new_i64();
5689         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5690     }
5691 
5692     tcg_acc = cpu_reg(s, rn);
5693     tcg_bytes = tcg_constant_i32(1 << sz);
5694 
5695     if (crc32c) {
5696         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5697     } else {
5698         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5699     }
5700 }
5701 
5702 /* Data-processing (2 source)
5703  *   31   30  29 28             21 20  16 15    10 9    5 4    0
5704  * +----+---+---+-----------------+------+--------+------+------+
5705  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
5706  * +----+---+---+-----------------+------+--------+------+------+
5707  */
5708 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5709 {
5710     unsigned int sf, rm, opcode, rn, rd, setflag;
5711     sf = extract32(insn, 31, 1);
5712     setflag = extract32(insn, 29, 1);
5713     rm = extract32(insn, 16, 5);
5714     opcode = extract32(insn, 10, 6);
5715     rn = extract32(insn, 5, 5);
5716     rd = extract32(insn, 0, 5);
5717 
5718     if (setflag && opcode != 0) {
5719         unallocated_encoding(s);
5720         return;
5721     }
5722 
5723     switch (opcode) {
5724     case 0: /* SUBP(S) */
5725         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5726             goto do_unallocated;
5727         } else {
5728             TCGv_i64 tcg_n, tcg_m, tcg_d;
5729 
5730             tcg_n = read_cpu_reg_sp(s, rn, true);
5731             tcg_m = read_cpu_reg_sp(s, rm, true);
5732             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5733             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5734             tcg_d = cpu_reg(s, rd);
5735 
5736             if (setflag) {
5737                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5738             } else {
5739                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5740             }
5741         }
5742         break;
5743     case 2: /* UDIV */
5744         handle_div(s, false, sf, rm, rn, rd);
5745         break;
5746     case 3: /* SDIV */
5747         handle_div(s, true, sf, rm, rn, rd);
5748         break;
5749     case 4: /* IRG */
5750         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5751             goto do_unallocated;
5752         }
5753         if (s->ata) {
5754             gen_helper_irg(cpu_reg_sp(s, rd), cpu_env,
5755                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
5756         } else {
5757             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5758                                              cpu_reg_sp(s, rn));
5759         }
5760         break;
5761     case 5: /* GMI */
5762         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5763             goto do_unallocated;
5764         } else {
5765             TCGv_i64 t = tcg_temp_new_i64();
5766 
5767             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
5768             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
5769             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
5770         }
5771         break;
5772     case 8: /* LSLV */
5773         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5774         break;
5775     case 9: /* LSRV */
5776         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5777         break;
5778     case 10: /* ASRV */
5779         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5780         break;
5781     case 11: /* RORV */
5782         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5783         break;
5784     case 12: /* PACGA */
5785         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5786             goto do_unallocated;
5787         }
5788         gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5789                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
5790         break;
5791     case 16:
5792     case 17:
5793     case 18:
5794     case 19:
5795     case 20:
5796     case 21:
5797     case 22:
5798     case 23: /* CRC32 */
5799     {
5800         int sz = extract32(opcode, 0, 2);
5801         bool crc32c = extract32(opcode, 2, 1);
5802         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5803         break;
5804     }
5805     default:
5806     do_unallocated:
5807         unallocated_encoding(s);
5808         break;
5809     }
5810 }
5811 
5812 /*
5813  * Data processing - register
5814  *  31  30 29  28      25    21  20  16      10         0
5815  * +--+---+--+---+-------+-----+-------+-------+---------+
5816  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
5817  * +--+---+--+---+-------+-----+-------+-------+---------+
5818  */
5819 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5820 {
5821     int op0 = extract32(insn, 30, 1);
5822     int op1 = extract32(insn, 28, 1);
5823     int op2 = extract32(insn, 21, 4);
5824     int op3 = extract32(insn, 10, 6);
5825 
5826     if (!op1) {
5827         if (op2 & 8) {
5828             if (op2 & 1) {
5829                 /* Add/sub (extended register) */
5830                 disas_add_sub_ext_reg(s, insn);
5831             } else {
5832                 /* Add/sub (shifted register) */
5833                 disas_add_sub_reg(s, insn);
5834             }
5835         } else {
5836             /* Logical (shifted register) */
5837             disas_logic_reg(s, insn);
5838         }
5839         return;
5840     }
5841 
5842     switch (op2) {
5843     case 0x0:
5844         switch (op3) {
5845         case 0x00: /* Add/subtract (with carry) */
5846             disas_adc_sbc(s, insn);
5847             break;
5848 
5849         case 0x01: /* Rotate right into flags */
5850         case 0x21:
5851             disas_rotate_right_into_flags(s, insn);
5852             break;
5853 
5854         case 0x02: /* Evaluate into flags */
5855         case 0x12:
5856         case 0x22:
5857         case 0x32:
5858             disas_evaluate_into_flags(s, insn);
5859             break;
5860 
5861         default:
5862             goto do_unallocated;
5863         }
5864         break;
5865 
5866     case 0x2: /* Conditional compare */
5867         disas_cc(s, insn); /* both imm and reg forms */
5868         break;
5869 
5870     case 0x4: /* Conditional select */
5871         disas_cond_select(s, insn);
5872         break;
5873 
5874     case 0x6: /* Data-processing */
5875         if (op0) {    /* (1 source) */
5876             disas_data_proc_1src(s, insn);
5877         } else {      /* (2 source) */
5878             disas_data_proc_2src(s, insn);
5879         }
5880         break;
5881     case 0x8 ... 0xf: /* (3 source) */
5882         disas_data_proc_3src(s, insn);
5883         break;
5884 
5885     default:
5886     do_unallocated:
5887         unallocated_encoding(s);
5888         break;
5889     }
5890 }
5891 
5892 static void handle_fp_compare(DisasContext *s, int size,
5893                               unsigned int rn, unsigned int rm,
5894                               bool cmp_with_zero, bool signal_all_nans)
5895 {
5896     TCGv_i64 tcg_flags = tcg_temp_new_i64();
5897     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
5898 
5899     if (size == MO_64) {
5900         TCGv_i64 tcg_vn, tcg_vm;
5901 
5902         tcg_vn = read_fp_dreg(s, rn);
5903         if (cmp_with_zero) {
5904             tcg_vm = tcg_constant_i64(0);
5905         } else {
5906             tcg_vm = read_fp_dreg(s, rm);
5907         }
5908         if (signal_all_nans) {
5909             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5910         } else {
5911             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5912         }
5913     } else {
5914         TCGv_i32 tcg_vn = tcg_temp_new_i32();
5915         TCGv_i32 tcg_vm = tcg_temp_new_i32();
5916 
5917         read_vec_element_i32(s, tcg_vn, rn, 0, size);
5918         if (cmp_with_zero) {
5919             tcg_gen_movi_i32(tcg_vm, 0);
5920         } else {
5921             read_vec_element_i32(s, tcg_vm, rm, 0, size);
5922         }
5923 
5924         switch (size) {
5925         case MO_32:
5926             if (signal_all_nans) {
5927                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5928             } else {
5929                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5930             }
5931             break;
5932         case MO_16:
5933             if (signal_all_nans) {
5934                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5935             } else {
5936                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5937             }
5938             break;
5939         default:
5940             g_assert_not_reached();
5941         }
5942     }
5943 
5944     gen_set_nzcv(tcg_flags);
5945 }
5946 
5947 /* Floating point compare
5948  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
5949  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5950  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
5951  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5952  */
5953 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5954 {
5955     unsigned int mos, type, rm, op, rn, opc, op2r;
5956     int size;
5957 
5958     mos = extract32(insn, 29, 3);
5959     type = extract32(insn, 22, 2);
5960     rm = extract32(insn, 16, 5);
5961     op = extract32(insn, 14, 2);
5962     rn = extract32(insn, 5, 5);
5963     opc = extract32(insn, 3, 2);
5964     op2r = extract32(insn, 0, 3);
5965 
5966     if (mos || op || op2r) {
5967         unallocated_encoding(s);
5968         return;
5969     }
5970 
5971     switch (type) {
5972     case 0:
5973         size = MO_32;
5974         break;
5975     case 1:
5976         size = MO_64;
5977         break;
5978     case 3:
5979         size = MO_16;
5980         if (dc_isar_feature(aa64_fp16, s)) {
5981             break;
5982         }
5983         /* fallthru */
5984     default:
5985         unallocated_encoding(s);
5986         return;
5987     }
5988 
5989     if (!fp_access_check(s)) {
5990         return;
5991     }
5992 
5993     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
5994 }
5995 
5996 /* Floating point conditional compare
5997  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
5998  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5999  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
6000  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6001  */
6002 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
6003 {
6004     unsigned int mos, type, rm, cond, rn, op, nzcv;
6005     TCGLabel *label_continue = NULL;
6006     int size;
6007 
6008     mos = extract32(insn, 29, 3);
6009     type = extract32(insn, 22, 2);
6010     rm = extract32(insn, 16, 5);
6011     cond = extract32(insn, 12, 4);
6012     rn = extract32(insn, 5, 5);
6013     op = extract32(insn, 4, 1);
6014     nzcv = extract32(insn, 0, 4);
6015 
6016     if (mos) {
6017         unallocated_encoding(s);
6018         return;
6019     }
6020 
6021     switch (type) {
6022     case 0:
6023         size = MO_32;
6024         break;
6025     case 1:
6026         size = MO_64;
6027         break;
6028     case 3:
6029         size = MO_16;
6030         if (dc_isar_feature(aa64_fp16, s)) {
6031             break;
6032         }
6033         /* fallthru */
6034     default:
6035         unallocated_encoding(s);
6036         return;
6037     }
6038 
6039     if (!fp_access_check(s)) {
6040         return;
6041     }
6042 
6043     if (cond < 0x0e) { /* not always */
6044         TCGLabel *label_match = gen_new_label();
6045         label_continue = gen_new_label();
6046         arm_gen_test_cc(cond, label_match);
6047         /* nomatch: */
6048         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
6049         tcg_gen_br(label_continue);
6050         gen_set_label(label_match);
6051     }
6052 
6053     handle_fp_compare(s, size, rn, rm, false, op);
6054 
6055     if (cond < 0x0e) {
6056         gen_set_label(label_continue);
6057     }
6058 }
6059 
6060 /* Floating point conditional select
6061  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
6062  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6063  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
6064  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6065  */
6066 static void disas_fp_csel(DisasContext *s, uint32_t insn)
6067 {
6068     unsigned int mos, type, rm, cond, rn, rd;
6069     TCGv_i64 t_true, t_false;
6070     DisasCompare64 c;
6071     MemOp sz;
6072 
6073     mos = extract32(insn, 29, 3);
6074     type = extract32(insn, 22, 2);
6075     rm = extract32(insn, 16, 5);
6076     cond = extract32(insn, 12, 4);
6077     rn = extract32(insn, 5, 5);
6078     rd = extract32(insn, 0, 5);
6079 
6080     if (mos) {
6081         unallocated_encoding(s);
6082         return;
6083     }
6084 
6085     switch (type) {
6086     case 0:
6087         sz = MO_32;
6088         break;
6089     case 1:
6090         sz = MO_64;
6091         break;
6092     case 3:
6093         sz = MO_16;
6094         if (dc_isar_feature(aa64_fp16, s)) {
6095             break;
6096         }
6097         /* fallthru */
6098     default:
6099         unallocated_encoding(s);
6100         return;
6101     }
6102 
6103     if (!fp_access_check(s)) {
6104         return;
6105     }
6106 
6107     /* Zero extend sreg & hreg inputs to 64 bits now.  */
6108     t_true = tcg_temp_new_i64();
6109     t_false = tcg_temp_new_i64();
6110     read_vec_element(s, t_true, rn, 0, sz);
6111     read_vec_element(s, t_false, rm, 0, sz);
6112 
6113     a64_test_cc(&c, cond);
6114     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
6115                         t_true, t_false);
6116 
6117     /* Note that sregs & hregs write back zeros to the high bits,
6118        and we've already done the zero-extension.  */
6119     write_fp_dreg(s, rd, t_true);
6120 }
6121 
6122 /* Floating-point data-processing (1 source) - half precision */
6123 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
6124 {
6125     TCGv_ptr fpst = NULL;
6126     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
6127     TCGv_i32 tcg_res = tcg_temp_new_i32();
6128 
6129     switch (opcode) {
6130     case 0x0: /* FMOV */
6131         tcg_gen_mov_i32(tcg_res, tcg_op);
6132         break;
6133     case 0x1: /* FABS */
6134         tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
6135         break;
6136     case 0x2: /* FNEG */
6137         tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
6138         break;
6139     case 0x3: /* FSQRT */
6140         fpst = fpstatus_ptr(FPST_FPCR_F16);
6141         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
6142         break;
6143     case 0x8: /* FRINTN */
6144     case 0x9: /* FRINTP */
6145     case 0xa: /* FRINTM */
6146     case 0xb: /* FRINTZ */
6147     case 0xc: /* FRINTA */
6148     {
6149         TCGv_i32 tcg_rmode;
6150 
6151         fpst = fpstatus_ptr(FPST_FPCR_F16);
6152         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
6153         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6154         gen_restore_rmode(tcg_rmode, fpst);
6155         break;
6156     }
6157     case 0xe: /* FRINTX */
6158         fpst = fpstatus_ptr(FPST_FPCR_F16);
6159         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
6160         break;
6161     case 0xf: /* FRINTI */
6162         fpst = fpstatus_ptr(FPST_FPCR_F16);
6163         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6164         break;
6165     default:
6166         g_assert_not_reached();
6167     }
6168 
6169     write_fp_sreg(s, rd, tcg_res);
6170 }
6171 
6172 /* Floating-point data-processing (1 source) - single precision */
6173 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
6174 {
6175     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
6176     TCGv_i32 tcg_op, tcg_res;
6177     TCGv_ptr fpst;
6178     int rmode = -1;
6179 
6180     tcg_op = read_fp_sreg(s, rn);
6181     tcg_res = tcg_temp_new_i32();
6182 
6183     switch (opcode) {
6184     case 0x0: /* FMOV */
6185         tcg_gen_mov_i32(tcg_res, tcg_op);
6186         goto done;
6187     case 0x1: /* FABS */
6188         gen_helper_vfp_abss(tcg_res, tcg_op);
6189         goto done;
6190     case 0x2: /* FNEG */
6191         gen_helper_vfp_negs(tcg_res, tcg_op);
6192         goto done;
6193     case 0x3: /* FSQRT */
6194         gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
6195         goto done;
6196     case 0x6: /* BFCVT */
6197         gen_fpst = gen_helper_bfcvt;
6198         break;
6199     case 0x8: /* FRINTN */
6200     case 0x9: /* FRINTP */
6201     case 0xa: /* FRINTM */
6202     case 0xb: /* FRINTZ */
6203     case 0xc: /* FRINTA */
6204         rmode = opcode & 7;
6205         gen_fpst = gen_helper_rints;
6206         break;
6207     case 0xe: /* FRINTX */
6208         gen_fpst = gen_helper_rints_exact;
6209         break;
6210     case 0xf: /* FRINTI */
6211         gen_fpst = gen_helper_rints;
6212         break;
6213     case 0x10: /* FRINT32Z */
6214         rmode = FPROUNDING_ZERO;
6215         gen_fpst = gen_helper_frint32_s;
6216         break;
6217     case 0x11: /* FRINT32X */
6218         gen_fpst = gen_helper_frint32_s;
6219         break;
6220     case 0x12: /* FRINT64Z */
6221         rmode = FPROUNDING_ZERO;
6222         gen_fpst = gen_helper_frint64_s;
6223         break;
6224     case 0x13: /* FRINT64X */
6225         gen_fpst = gen_helper_frint64_s;
6226         break;
6227     default:
6228         g_assert_not_reached();
6229     }
6230 
6231     fpst = fpstatus_ptr(FPST_FPCR);
6232     if (rmode >= 0) {
6233         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6234         gen_fpst(tcg_res, tcg_op, fpst);
6235         gen_restore_rmode(tcg_rmode, fpst);
6236     } else {
6237         gen_fpst(tcg_res, tcg_op, fpst);
6238     }
6239 
6240  done:
6241     write_fp_sreg(s, rd, tcg_res);
6242 }
6243 
6244 /* Floating-point data-processing (1 source) - double precision */
6245 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6246 {
6247     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6248     TCGv_i64 tcg_op, tcg_res;
6249     TCGv_ptr fpst;
6250     int rmode = -1;
6251 
6252     switch (opcode) {
6253     case 0x0: /* FMOV */
6254         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6255         return;
6256     }
6257 
6258     tcg_op = read_fp_dreg(s, rn);
6259     tcg_res = tcg_temp_new_i64();
6260 
6261     switch (opcode) {
6262     case 0x1: /* FABS */
6263         gen_helper_vfp_absd(tcg_res, tcg_op);
6264         goto done;
6265     case 0x2: /* FNEG */
6266         gen_helper_vfp_negd(tcg_res, tcg_op);
6267         goto done;
6268     case 0x3: /* FSQRT */
6269         gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
6270         goto done;
6271     case 0x8: /* FRINTN */
6272     case 0x9: /* FRINTP */
6273     case 0xa: /* FRINTM */
6274     case 0xb: /* FRINTZ */
6275     case 0xc: /* FRINTA */
6276         rmode = opcode & 7;
6277         gen_fpst = gen_helper_rintd;
6278         break;
6279     case 0xe: /* FRINTX */
6280         gen_fpst = gen_helper_rintd_exact;
6281         break;
6282     case 0xf: /* FRINTI */
6283         gen_fpst = gen_helper_rintd;
6284         break;
6285     case 0x10: /* FRINT32Z */
6286         rmode = FPROUNDING_ZERO;
6287         gen_fpst = gen_helper_frint32_d;
6288         break;
6289     case 0x11: /* FRINT32X */
6290         gen_fpst = gen_helper_frint32_d;
6291         break;
6292     case 0x12: /* FRINT64Z */
6293         rmode = FPROUNDING_ZERO;
6294         gen_fpst = gen_helper_frint64_d;
6295         break;
6296     case 0x13: /* FRINT64X */
6297         gen_fpst = gen_helper_frint64_d;
6298         break;
6299     default:
6300         g_assert_not_reached();
6301     }
6302 
6303     fpst = fpstatus_ptr(FPST_FPCR);
6304     if (rmode >= 0) {
6305         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6306         gen_fpst(tcg_res, tcg_op, fpst);
6307         gen_restore_rmode(tcg_rmode, fpst);
6308     } else {
6309         gen_fpst(tcg_res, tcg_op, fpst);
6310     }
6311 
6312  done:
6313     write_fp_dreg(s, rd, tcg_res);
6314 }
6315 
6316 static void handle_fp_fcvt(DisasContext *s, int opcode,
6317                            int rd, int rn, int dtype, int ntype)
6318 {
6319     switch (ntype) {
6320     case 0x0:
6321     {
6322         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6323         if (dtype == 1) {
6324             /* Single to double */
6325             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6326             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
6327             write_fp_dreg(s, rd, tcg_rd);
6328         } else {
6329             /* Single to half */
6330             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6331             TCGv_i32 ahp = get_ahp_flag();
6332             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6333 
6334             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6335             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6336             write_fp_sreg(s, rd, tcg_rd);
6337         }
6338         break;
6339     }
6340     case 0x1:
6341     {
6342         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6343         TCGv_i32 tcg_rd = tcg_temp_new_i32();
6344         if (dtype == 0) {
6345             /* Double to single */
6346             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
6347         } else {
6348             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6349             TCGv_i32 ahp = get_ahp_flag();
6350             /* Double to half */
6351             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6352             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6353         }
6354         write_fp_sreg(s, rd, tcg_rd);
6355         break;
6356     }
6357     case 0x3:
6358     {
6359         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6360         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6361         TCGv_i32 tcg_ahp = get_ahp_flag();
6362         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6363         if (dtype == 0) {
6364             /* Half to single */
6365             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6366             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6367             write_fp_sreg(s, rd, tcg_rd);
6368         } else {
6369             /* Half to double */
6370             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6371             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6372             write_fp_dreg(s, rd, tcg_rd);
6373         }
6374         break;
6375     }
6376     default:
6377         g_assert_not_reached();
6378     }
6379 }
6380 
6381 /* Floating point data-processing (1 source)
6382  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
6383  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6384  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
6385  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6386  */
6387 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6388 {
6389     int mos = extract32(insn, 29, 3);
6390     int type = extract32(insn, 22, 2);
6391     int opcode = extract32(insn, 15, 6);
6392     int rn = extract32(insn, 5, 5);
6393     int rd = extract32(insn, 0, 5);
6394 
6395     if (mos) {
6396         goto do_unallocated;
6397     }
6398 
6399     switch (opcode) {
6400     case 0x4: case 0x5: case 0x7:
6401     {
6402         /* FCVT between half, single and double precision */
6403         int dtype = extract32(opcode, 0, 2);
6404         if (type == 2 || dtype == type) {
6405             goto do_unallocated;
6406         }
6407         if (!fp_access_check(s)) {
6408             return;
6409         }
6410 
6411         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6412         break;
6413     }
6414 
6415     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6416         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6417             goto do_unallocated;
6418         }
6419         /* fall through */
6420     case 0x0 ... 0x3:
6421     case 0x8 ... 0xc:
6422     case 0xe ... 0xf:
6423         /* 32-to-32 and 64-to-64 ops */
6424         switch (type) {
6425         case 0:
6426             if (!fp_access_check(s)) {
6427                 return;
6428             }
6429             handle_fp_1src_single(s, opcode, rd, rn);
6430             break;
6431         case 1:
6432             if (!fp_access_check(s)) {
6433                 return;
6434             }
6435             handle_fp_1src_double(s, opcode, rd, rn);
6436             break;
6437         case 3:
6438             if (!dc_isar_feature(aa64_fp16, s)) {
6439                 goto do_unallocated;
6440             }
6441 
6442             if (!fp_access_check(s)) {
6443                 return;
6444             }
6445             handle_fp_1src_half(s, opcode, rd, rn);
6446             break;
6447         default:
6448             goto do_unallocated;
6449         }
6450         break;
6451 
6452     case 0x6:
6453         switch (type) {
6454         case 1: /* BFCVT */
6455             if (!dc_isar_feature(aa64_bf16, s)) {
6456                 goto do_unallocated;
6457             }
6458             if (!fp_access_check(s)) {
6459                 return;
6460             }
6461             handle_fp_1src_single(s, opcode, rd, rn);
6462             break;
6463         default:
6464             goto do_unallocated;
6465         }
6466         break;
6467 
6468     default:
6469     do_unallocated:
6470         unallocated_encoding(s);
6471         break;
6472     }
6473 }
6474 
6475 /* Floating-point data-processing (2 source) - single precision */
6476 static void handle_fp_2src_single(DisasContext *s, int opcode,
6477                                   int rd, int rn, int rm)
6478 {
6479     TCGv_i32 tcg_op1;
6480     TCGv_i32 tcg_op2;
6481     TCGv_i32 tcg_res;
6482     TCGv_ptr fpst;
6483 
6484     tcg_res = tcg_temp_new_i32();
6485     fpst = fpstatus_ptr(FPST_FPCR);
6486     tcg_op1 = read_fp_sreg(s, rn);
6487     tcg_op2 = read_fp_sreg(s, rm);
6488 
6489     switch (opcode) {
6490     case 0x0: /* FMUL */
6491         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6492         break;
6493     case 0x1: /* FDIV */
6494         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6495         break;
6496     case 0x2: /* FADD */
6497         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6498         break;
6499     case 0x3: /* FSUB */
6500         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6501         break;
6502     case 0x4: /* FMAX */
6503         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6504         break;
6505     case 0x5: /* FMIN */
6506         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6507         break;
6508     case 0x6: /* FMAXNM */
6509         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6510         break;
6511     case 0x7: /* FMINNM */
6512         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6513         break;
6514     case 0x8: /* FNMUL */
6515         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6516         gen_helper_vfp_negs(tcg_res, tcg_res);
6517         break;
6518     }
6519 
6520     write_fp_sreg(s, rd, tcg_res);
6521 }
6522 
6523 /* Floating-point data-processing (2 source) - double precision */
6524 static void handle_fp_2src_double(DisasContext *s, int opcode,
6525                                   int rd, int rn, int rm)
6526 {
6527     TCGv_i64 tcg_op1;
6528     TCGv_i64 tcg_op2;
6529     TCGv_i64 tcg_res;
6530     TCGv_ptr fpst;
6531 
6532     tcg_res = tcg_temp_new_i64();
6533     fpst = fpstatus_ptr(FPST_FPCR);
6534     tcg_op1 = read_fp_dreg(s, rn);
6535     tcg_op2 = read_fp_dreg(s, rm);
6536 
6537     switch (opcode) {
6538     case 0x0: /* FMUL */
6539         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6540         break;
6541     case 0x1: /* FDIV */
6542         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6543         break;
6544     case 0x2: /* FADD */
6545         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6546         break;
6547     case 0x3: /* FSUB */
6548         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6549         break;
6550     case 0x4: /* FMAX */
6551         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6552         break;
6553     case 0x5: /* FMIN */
6554         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6555         break;
6556     case 0x6: /* FMAXNM */
6557         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6558         break;
6559     case 0x7: /* FMINNM */
6560         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6561         break;
6562     case 0x8: /* FNMUL */
6563         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6564         gen_helper_vfp_negd(tcg_res, tcg_res);
6565         break;
6566     }
6567 
6568     write_fp_dreg(s, rd, tcg_res);
6569 }
6570 
6571 /* Floating-point data-processing (2 source) - half precision */
6572 static void handle_fp_2src_half(DisasContext *s, int opcode,
6573                                 int rd, int rn, int rm)
6574 {
6575     TCGv_i32 tcg_op1;
6576     TCGv_i32 tcg_op2;
6577     TCGv_i32 tcg_res;
6578     TCGv_ptr fpst;
6579 
6580     tcg_res = tcg_temp_new_i32();
6581     fpst = fpstatus_ptr(FPST_FPCR_F16);
6582     tcg_op1 = read_fp_hreg(s, rn);
6583     tcg_op2 = read_fp_hreg(s, rm);
6584 
6585     switch (opcode) {
6586     case 0x0: /* FMUL */
6587         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6588         break;
6589     case 0x1: /* FDIV */
6590         gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6591         break;
6592     case 0x2: /* FADD */
6593         gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6594         break;
6595     case 0x3: /* FSUB */
6596         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6597         break;
6598     case 0x4: /* FMAX */
6599         gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6600         break;
6601     case 0x5: /* FMIN */
6602         gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6603         break;
6604     case 0x6: /* FMAXNM */
6605         gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6606         break;
6607     case 0x7: /* FMINNM */
6608         gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6609         break;
6610     case 0x8: /* FNMUL */
6611         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6612         tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6613         break;
6614     default:
6615         g_assert_not_reached();
6616     }
6617 
6618     write_fp_sreg(s, rd, tcg_res);
6619 }
6620 
6621 /* Floating point data-processing (2 source)
6622  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
6623  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6624  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
6625  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6626  */
6627 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6628 {
6629     int mos = extract32(insn, 29, 3);
6630     int type = extract32(insn, 22, 2);
6631     int rd = extract32(insn, 0, 5);
6632     int rn = extract32(insn, 5, 5);
6633     int rm = extract32(insn, 16, 5);
6634     int opcode = extract32(insn, 12, 4);
6635 
6636     if (opcode > 8 || mos) {
6637         unallocated_encoding(s);
6638         return;
6639     }
6640 
6641     switch (type) {
6642     case 0:
6643         if (!fp_access_check(s)) {
6644             return;
6645         }
6646         handle_fp_2src_single(s, opcode, rd, rn, rm);
6647         break;
6648     case 1:
6649         if (!fp_access_check(s)) {
6650             return;
6651         }
6652         handle_fp_2src_double(s, opcode, rd, rn, rm);
6653         break;
6654     case 3:
6655         if (!dc_isar_feature(aa64_fp16, s)) {
6656             unallocated_encoding(s);
6657             return;
6658         }
6659         if (!fp_access_check(s)) {
6660             return;
6661         }
6662         handle_fp_2src_half(s, opcode, rd, rn, rm);
6663         break;
6664     default:
6665         unallocated_encoding(s);
6666     }
6667 }
6668 
6669 /* Floating-point data-processing (3 source) - single precision */
6670 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6671                                   int rd, int rn, int rm, int ra)
6672 {
6673     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6674     TCGv_i32 tcg_res = tcg_temp_new_i32();
6675     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6676 
6677     tcg_op1 = read_fp_sreg(s, rn);
6678     tcg_op2 = read_fp_sreg(s, rm);
6679     tcg_op3 = read_fp_sreg(s, ra);
6680 
6681     /* These are fused multiply-add, and must be done as one
6682      * floating point operation with no rounding between the
6683      * multiplication and addition steps.
6684      * NB that doing the negations here as separate steps is
6685      * correct : an input NaN should come out with its sign bit
6686      * flipped if it is a negated-input.
6687      */
6688     if (o1 == true) {
6689         gen_helper_vfp_negs(tcg_op3, tcg_op3);
6690     }
6691 
6692     if (o0 != o1) {
6693         gen_helper_vfp_negs(tcg_op1, tcg_op1);
6694     }
6695 
6696     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6697 
6698     write_fp_sreg(s, rd, tcg_res);
6699 }
6700 
6701 /* Floating-point data-processing (3 source) - double precision */
6702 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6703                                   int rd, int rn, int rm, int ra)
6704 {
6705     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6706     TCGv_i64 tcg_res = tcg_temp_new_i64();
6707     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6708 
6709     tcg_op1 = read_fp_dreg(s, rn);
6710     tcg_op2 = read_fp_dreg(s, rm);
6711     tcg_op3 = read_fp_dreg(s, ra);
6712 
6713     /* These are fused multiply-add, and must be done as one
6714      * floating point operation with no rounding between the
6715      * multiplication and addition steps.
6716      * NB that doing the negations here as separate steps is
6717      * correct : an input NaN should come out with its sign bit
6718      * flipped if it is a negated-input.
6719      */
6720     if (o1 == true) {
6721         gen_helper_vfp_negd(tcg_op3, tcg_op3);
6722     }
6723 
6724     if (o0 != o1) {
6725         gen_helper_vfp_negd(tcg_op1, tcg_op1);
6726     }
6727 
6728     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6729 
6730     write_fp_dreg(s, rd, tcg_res);
6731 }
6732 
6733 /* Floating-point data-processing (3 source) - half precision */
6734 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6735                                 int rd, int rn, int rm, int ra)
6736 {
6737     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6738     TCGv_i32 tcg_res = tcg_temp_new_i32();
6739     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6740 
6741     tcg_op1 = read_fp_hreg(s, rn);
6742     tcg_op2 = read_fp_hreg(s, rm);
6743     tcg_op3 = read_fp_hreg(s, ra);
6744 
6745     /* These are fused multiply-add, and must be done as one
6746      * floating point operation with no rounding between the
6747      * multiplication and addition steps.
6748      * NB that doing the negations here as separate steps is
6749      * correct : an input NaN should come out with its sign bit
6750      * flipped if it is a negated-input.
6751      */
6752     if (o1 == true) {
6753         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6754     }
6755 
6756     if (o0 != o1) {
6757         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6758     }
6759 
6760     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6761 
6762     write_fp_sreg(s, rd, tcg_res);
6763 }
6764 
6765 /* Floating point data-processing (3 source)
6766  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
6767  * +---+---+---+-----------+------+----+------+----+------+------+------+
6768  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6769  * +---+---+---+-----------+------+----+------+----+------+------+------+
6770  */
6771 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6772 {
6773     int mos = extract32(insn, 29, 3);
6774     int type = extract32(insn, 22, 2);
6775     int rd = extract32(insn, 0, 5);
6776     int rn = extract32(insn, 5, 5);
6777     int ra = extract32(insn, 10, 5);
6778     int rm = extract32(insn, 16, 5);
6779     bool o0 = extract32(insn, 15, 1);
6780     bool o1 = extract32(insn, 21, 1);
6781 
6782     if (mos) {
6783         unallocated_encoding(s);
6784         return;
6785     }
6786 
6787     switch (type) {
6788     case 0:
6789         if (!fp_access_check(s)) {
6790             return;
6791         }
6792         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6793         break;
6794     case 1:
6795         if (!fp_access_check(s)) {
6796             return;
6797         }
6798         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6799         break;
6800     case 3:
6801         if (!dc_isar_feature(aa64_fp16, s)) {
6802             unallocated_encoding(s);
6803             return;
6804         }
6805         if (!fp_access_check(s)) {
6806             return;
6807         }
6808         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6809         break;
6810     default:
6811         unallocated_encoding(s);
6812     }
6813 }
6814 
6815 /* Floating point immediate
6816  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
6817  * +---+---+---+-----------+------+---+------------+-------+------+------+
6818  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
6819  * +---+---+---+-----------+------+---+------------+-------+------+------+
6820  */
6821 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6822 {
6823     int rd = extract32(insn, 0, 5);
6824     int imm5 = extract32(insn, 5, 5);
6825     int imm8 = extract32(insn, 13, 8);
6826     int type = extract32(insn, 22, 2);
6827     int mos = extract32(insn, 29, 3);
6828     uint64_t imm;
6829     MemOp sz;
6830 
6831     if (mos || imm5) {
6832         unallocated_encoding(s);
6833         return;
6834     }
6835 
6836     switch (type) {
6837     case 0:
6838         sz = MO_32;
6839         break;
6840     case 1:
6841         sz = MO_64;
6842         break;
6843     case 3:
6844         sz = MO_16;
6845         if (dc_isar_feature(aa64_fp16, s)) {
6846             break;
6847         }
6848         /* fallthru */
6849     default:
6850         unallocated_encoding(s);
6851         return;
6852     }
6853 
6854     if (!fp_access_check(s)) {
6855         return;
6856     }
6857 
6858     imm = vfp_expand_imm(sz, imm8);
6859     write_fp_dreg(s, rd, tcg_constant_i64(imm));
6860 }
6861 
6862 /* Handle floating point <=> fixed point conversions. Note that we can
6863  * also deal with fp <=> integer conversions as a special case (scale == 64)
6864  * OPTME: consider handling that special case specially or at least skipping
6865  * the call to scalbn in the helpers for zero shifts.
6866  */
6867 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6868                            bool itof, int rmode, int scale, int sf, int type)
6869 {
6870     bool is_signed = !(opcode & 1);
6871     TCGv_ptr tcg_fpstatus;
6872     TCGv_i32 tcg_shift, tcg_single;
6873     TCGv_i64 tcg_double;
6874 
6875     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
6876 
6877     tcg_shift = tcg_constant_i32(64 - scale);
6878 
6879     if (itof) {
6880         TCGv_i64 tcg_int = cpu_reg(s, rn);
6881         if (!sf) {
6882             TCGv_i64 tcg_extend = tcg_temp_new_i64();
6883 
6884             if (is_signed) {
6885                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6886             } else {
6887                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6888             }
6889 
6890             tcg_int = tcg_extend;
6891         }
6892 
6893         switch (type) {
6894         case 1: /* float64 */
6895             tcg_double = tcg_temp_new_i64();
6896             if (is_signed) {
6897                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6898                                      tcg_shift, tcg_fpstatus);
6899             } else {
6900                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6901                                      tcg_shift, tcg_fpstatus);
6902             }
6903             write_fp_dreg(s, rd, tcg_double);
6904             break;
6905 
6906         case 0: /* float32 */
6907             tcg_single = tcg_temp_new_i32();
6908             if (is_signed) {
6909                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6910                                      tcg_shift, tcg_fpstatus);
6911             } else {
6912                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6913                                      tcg_shift, tcg_fpstatus);
6914             }
6915             write_fp_sreg(s, rd, tcg_single);
6916             break;
6917 
6918         case 3: /* float16 */
6919             tcg_single = tcg_temp_new_i32();
6920             if (is_signed) {
6921                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6922                                      tcg_shift, tcg_fpstatus);
6923             } else {
6924                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6925                                      tcg_shift, tcg_fpstatus);
6926             }
6927             write_fp_sreg(s, rd, tcg_single);
6928             break;
6929 
6930         default:
6931             g_assert_not_reached();
6932         }
6933     } else {
6934         TCGv_i64 tcg_int = cpu_reg(s, rd);
6935         TCGv_i32 tcg_rmode;
6936 
6937         if (extract32(opcode, 2, 1)) {
6938             /* There are too many rounding modes to all fit into rmode,
6939              * so FCVTA[US] is a special case.
6940              */
6941             rmode = FPROUNDING_TIEAWAY;
6942         }
6943 
6944         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
6945 
6946         switch (type) {
6947         case 1: /* float64 */
6948             tcg_double = read_fp_dreg(s, rn);
6949             if (is_signed) {
6950                 if (!sf) {
6951                     gen_helper_vfp_tosld(tcg_int, tcg_double,
6952                                          tcg_shift, tcg_fpstatus);
6953                 } else {
6954                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
6955                                          tcg_shift, tcg_fpstatus);
6956                 }
6957             } else {
6958                 if (!sf) {
6959                     gen_helper_vfp_tould(tcg_int, tcg_double,
6960                                          tcg_shift, tcg_fpstatus);
6961                 } else {
6962                     gen_helper_vfp_touqd(tcg_int, tcg_double,
6963                                          tcg_shift, tcg_fpstatus);
6964                 }
6965             }
6966             if (!sf) {
6967                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6968             }
6969             break;
6970 
6971         case 0: /* float32 */
6972             tcg_single = read_fp_sreg(s, rn);
6973             if (sf) {
6974                 if (is_signed) {
6975                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
6976                                          tcg_shift, tcg_fpstatus);
6977                 } else {
6978                     gen_helper_vfp_touqs(tcg_int, tcg_single,
6979                                          tcg_shift, tcg_fpstatus);
6980                 }
6981             } else {
6982                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6983                 if (is_signed) {
6984                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
6985                                          tcg_shift, tcg_fpstatus);
6986                 } else {
6987                     gen_helper_vfp_touls(tcg_dest, tcg_single,
6988                                          tcg_shift, tcg_fpstatus);
6989                 }
6990                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6991             }
6992             break;
6993 
6994         case 3: /* float16 */
6995             tcg_single = read_fp_sreg(s, rn);
6996             if (sf) {
6997                 if (is_signed) {
6998                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
6999                                          tcg_shift, tcg_fpstatus);
7000                 } else {
7001                     gen_helper_vfp_touqh(tcg_int, tcg_single,
7002                                          tcg_shift, tcg_fpstatus);
7003                 }
7004             } else {
7005                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7006                 if (is_signed) {
7007                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
7008                                          tcg_shift, tcg_fpstatus);
7009                 } else {
7010                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
7011                                          tcg_shift, tcg_fpstatus);
7012                 }
7013                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7014             }
7015             break;
7016 
7017         default:
7018             g_assert_not_reached();
7019         }
7020 
7021         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
7022     }
7023 }
7024 
7025 /* Floating point <-> fixed point conversions
7026  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
7027  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7028  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
7029  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7030  */
7031 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
7032 {
7033     int rd = extract32(insn, 0, 5);
7034     int rn = extract32(insn, 5, 5);
7035     int scale = extract32(insn, 10, 6);
7036     int opcode = extract32(insn, 16, 3);
7037     int rmode = extract32(insn, 19, 2);
7038     int type = extract32(insn, 22, 2);
7039     bool sbit = extract32(insn, 29, 1);
7040     bool sf = extract32(insn, 31, 1);
7041     bool itof;
7042 
7043     if (sbit || (!sf && scale < 32)) {
7044         unallocated_encoding(s);
7045         return;
7046     }
7047 
7048     switch (type) {
7049     case 0: /* float32 */
7050     case 1: /* float64 */
7051         break;
7052     case 3: /* float16 */
7053         if (dc_isar_feature(aa64_fp16, s)) {
7054             break;
7055         }
7056         /* fallthru */
7057     default:
7058         unallocated_encoding(s);
7059         return;
7060     }
7061 
7062     switch ((rmode << 3) | opcode) {
7063     case 0x2: /* SCVTF */
7064     case 0x3: /* UCVTF */
7065         itof = true;
7066         break;
7067     case 0x18: /* FCVTZS */
7068     case 0x19: /* FCVTZU */
7069         itof = false;
7070         break;
7071     default:
7072         unallocated_encoding(s);
7073         return;
7074     }
7075 
7076     if (!fp_access_check(s)) {
7077         return;
7078     }
7079 
7080     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
7081 }
7082 
7083 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
7084 {
7085     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
7086      * without conversion.
7087      */
7088 
7089     if (itof) {
7090         TCGv_i64 tcg_rn = cpu_reg(s, rn);
7091         TCGv_i64 tmp;
7092 
7093         switch (type) {
7094         case 0:
7095             /* 32 bit */
7096             tmp = tcg_temp_new_i64();
7097             tcg_gen_ext32u_i64(tmp, tcg_rn);
7098             write_fp_dreg(s, rd, tmp);
7099             break;
7100         case 1:
7101             /* 64 bit */
7102             write_fp_dreg(s, rd, tcg_rn);
7103             break;
7104         case 2:
7105             /* 64 bit to top half. */
7106             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
7107             clear_vec_high(s, true, rd);
7108             break;
7109         case 3:
7110             /* 16 bit */
7111             tmp = tcg_temp_new_i64();
7112             tcg_gen_ext16u_i64(tmp, tcg_rn);
7113             write_fp_dreg(s, rd, tmp);
7114             break;
7115         default:
7116             g_assert_not_reached();
7117         }
7118     } else {
7119         TCGv_i64 tcg_rd = cpu_reg(s, rd);
7120 
7121         switch (type) {
7122         case 0:
7123             /* 32 bit */
7124             tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
7125             break;
7126         case 1:
7127             /* 64 bit */
7128             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
7129             break;
7130         case 2:
7131             /* 64 bits from top half */
7132             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
7133             break;
7134         case 3:
7135             /* 16 bit */
7136             tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
7137             break;
7138         default:
7139             g_assert_not_reached();
7140         }
7141     }
7142 }
7143 
7144 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
7145 {
7146     TCGv_i64 t = read_fp_dreg(s, rn);
7147     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
7148 
7149     gen_helper_fjcvtzs(t, t, fpstatus);
7150 
7151     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
7152     tcg_gen_extrh_i64_i32(cpu_ZF, t);
7153     tcg_gen_movi_i32(cpu_CF, 0);
7154     tcg_gen_movi_i32(cpu_NF, 0);
7155     tcg_gen_movi_i32(cpu_VF, 0);
7156 }
7157 
7158 /* Floating point <-> integer conversions
7159  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
7160  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7161  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
7162  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7163  */
7164 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
7165 {
7166     int rd = extract32(insn, 0, 5);
7167     int rn = extract32(insn, 5, 5);
7168     int opcode = extract32(insn, 16, 3);
7169     int rmode = extract32(insn, 19, 2);
7170     int type = extract32(insn, 22, 2);
7171     bool sbit = extract32(insn, 29, 1);
7172     bool sf = extract32(insn, 31, 1);
7173     bool itof = false;
7174 
7175     if (sbit) {
7176         goto do_unallocated;
7177     }
7178 
7179     switch (opcode) {
7180     case 2: /* SCVTF */
7181     case 3: /* UCVTF */
7182         itof = true;
7183         /* fallthru */
7184     case 4: /* FCVTAS */
7185     case 5: /* FCVTAU */
7186         if (rmode != 0) {
7187             goto do_unallocated;
7188         }
7189         /* fallthru */
7190     case 0: /* FCVT[NPMZ]S */
7191     case 1: /* FCVT[NPMZ]U */
7192         switch (type) {
7193         case 0: /* float32 */
7194         case 1: /* float64 */
7195             break;
7196         case 3: /* float16 */
7197             if (!dc_isar_feature(aa64_fp16, s)) {
7198                 goto do_unallocated;
7199             }
7200             break;
7201         default:
7202             goto do_unallocated;
7203         }
7204         if (!fp_access_check(s)) {
7205             return;
7206         }
7207         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
7208         break;
7209 
7210     default:
7211         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
7212         case 0b01100110: /* FMOV half <-> 32-bit int */
7213         case 0b01100111:
7214         case 0b11100110: /* FMOV half <-> 64-bit int */
7215         case 0b11100111:
7216             if (!dc_isar_feature(aa64_fp16, s)) {
7217                 goto do_unallocated;
7218             }
7219             /* fallthru */
7220         case 0b00000110: /* FMOV 32-bit */
7221         case 0b00000111:
7222         case 0b10100110: /* FMOV 64-bit */
7223         case 0b10100111:
7224         case 0b11001110: /* FMOV top half of 128-bit */
7225         case 0b11001111:
7226             if (!fp_access_check(s)) {
7227                 return;
7228             }
7229             itof = opcode & 1;
7230             handle_fmov(s, rd, rn, type, itof);
7231             break;
7232 
7233         case 0b00111110: /* FJCVTZS */
7234             if (!dc_isar_feature(aa64_jscvt, s)) {
7235                 goto do_unallocated;
7236             } else if (fp_access_check(s)) {
7237                 handle_fjcvtzs(s, rd, rn);
7238             }
7239             break;
7240 
7241         default:
7242         do_unallocated:
7243             unallocated_encoding(s);
7244             return;
7245         }
7246         break;
7247     }
7248 }
7249 
7250 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7251  *   31  30  29 28     25 24                          0
7252  * +---+---+---+---------+-----------------------------+
7253  * |   | 0 |   | 1 1 1 1 |                             |
7254  * +---+---+---+---------+-----------------------------+
7255  */
7256 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7257 {
7258     if (extract32(insn, 24, 1)) {
7259         /* Floating point data-processing (3 source) */
7260         disas_fp_3src(s, insn);
7261     } else if (extract32(insn, 21, 1) == 0) {
7262         /* Floating point to fixed point conversions */
7263         disas_fp_fixed_conv(s, insn);
7264     } else {
7265         switch (extract32(insn, 10, 2)) {
7266         case 1:
7267             /* Floating point conditional compare */
7268             disas_fp_ccomp(s, insn);
7269             break;
7270         case 2:
7271             /* Floating point data-processing (2 source) */
7272             disas_fp_2src(s, insn);
7273             break;
7274         case 3:
7275             /* Floating point conditional select */
7276             disas_fp_csel(s, insn);
7277             break;
7278         case 0:
7279             switch (ctz32(extract32(insn, 12, 4))) {
7280             case 0: /* [15:12] == xxx1 */
7281                 /* Floating point immediate */
7282                 disas_fp_imm(s, insn);
7283                 break;
7284             case 1: /* [15:12] == xx10 */
7285                 /* Floating point compare */
7286                 disas_fp_compare(s, insn);
7287                 break;
7288             case 2: /* [15:12] == x100 */
7289                 /* Floating point data-processing (1 source) */
7290                 disas_fp_1src(s, insn);
7291                 break;
7292             case 3: /* [15:12] == 1000 */
7293                 unallocated_encoding(s);
7294                 break;
7295             default: /* [15:12] == 0000 */
7296                 /* Floating point <-> integer conversions */
7297                 disas_fp_int_conv(s, insn);
7298                 break;
7299             }
7300             break;
7301         }
7302     }
7303 }
7304 
7305 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7306                      int pos)
7307 {
7308     /* Extract 64 bits from the middle of two concatenated 64 bit
7309      * vector register slices left:right. The extracted bits start
7310      * at 'pos' bits into the right (least significant) side.
7311      * We return the result in tcg_right, and guarantee not to
7312      * trash tcg_left.
7313      */
7314     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7315     assert(pos > 0 && pos < 64);
7316 
7317     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7318     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7319     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7320 }
7321 
7322 /* EXT
7323  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
7324  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7325  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
7326  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7327  */
7328 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7329 {
7330     int is_q = extract32(insn, 30, 1);
7331     int op2 = extract32(insn, 22, 2);
7332     int imm4 = extract32(insn, 11, 4);
7333     int rm = extract32(insn, 16, 5);
7334     int rn = extract32(insn, 5, 5);
7335     int rd = extract32(insn, 0, 5);
7336     int pos = imm4 << 3;
7337     TCGv_i64 tcg_resl, tcg_resh;
7338 
7339     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7340         unallocated_encoding(s);
7341         return;
7342     }
7343 
7344     if (!fp_access_check(s)) {
7345         return;
7346     }
7347 
7348     tcg_resh = tcg_temp_new_i64();
7349     tcg_resl = tcg_temp_new_i64();
7350 
7351     /* Vd gets bits starting at pos bits into Vm:Vn. This is
7352      * either extracting 128 bits from a 128:128 concatenation, or
7353      * extracting 64 bits from a 64:64 concatenation.
7354      */
7355     if (!is_q) {
7356         read_vec_element(s, tcg_resl, rn, 0, MO_64);
7357         if (pos != 0) {
7358             read_vec_element(s, tcg_resh, rm, 0, MO_64);
7359             do_ext64(s, tcg_resh, tcg_resl, pos);
7360         }
7361     } else {
7362         TCGv_i64 tcg_hh;
7363         typedef struct {
7364             int reg;
7365             int elt;
7366         } EltPosns;
7367         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7368         EltPosns *elt = eltposns;
7369 
7370         if (pos >= 64) {
7371             elt++;
7372             pos -= 64;
7373         }
7374 
7375         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7376         elt++;
7377         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7378         elt++;
7379         if (pos != 0) {
7380             do_ext64(s, tcg_resh, tcg_resl, pos);
7381             tcg_hh = tcg_temp_new_i64();
7382             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7383             do_ext64(s, tcg_hh, tcg_resh, pos);
7384         }
7385     }
7386 
7387     write_vec_element(s, tcg_resl, rd, 0, MO_64);
7388     if (is_q) {
7389         write_vec_element(s, tcg_resh, rd, 1, MO_64);
7390     }
7391     clear_vec_high(s, is_q, rd);
7392 }
7393 
7394 /* TBL/TBX
7395  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
7396  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7397  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
7398  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7399  */
7400 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7401 {
7402     int op2 = extract32(insn, 22, 2);
7403     int is_q = extract32(insn, 30, 1);
7404     int rm = extract32(insn, 16, 5);
7405     int rn = extract32(insn, 5, 5);
7406     int rd = extract32(insn, 0, 5);
7407     int is_tbx = extract32(insn, 12, 1);
7408     int len = (extract32(insn, 13, 2) + 1) * 16;
7409 
7410     if (op2 != 0) {
7411         unallocated_encoding(s);
7412         return;
7413     }
7414 
7415     if (!fp_access_check(s)) {
7416         return;
7417     }
7418 
7419     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7420                        vec_full_reg_offset(s, rm), cpu_env,
7421                        is_q ? 16 : 8, vec_full_reg_size(s),
7422                        (len << 6) | (is_tbx << 5) | rn,
7423                        gen_helper_simd_tblx);
7424 }
7425 
7426 /* ZIP/UZP/TRN
7427  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
7428  * +---+---+-------------+------+---+------+---+------------------+------+
7429  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
7430  * +---+---+-------------+------+---+------+---+------------------+------+
7431  */
7432 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7433 {
7434     int rd = extract32(insn, 0, 5);
7435     int rn = extract32(insn, 5, 5);
7436     int rm = extract32(insn, 16, 5);
7437     int size = extract32(insn, 22, 2);
7438     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7439      * bit 2 indicates 1 vs 2 variant of the insn.
7440      */
7441     int opcode = extract32(insn, 12, 2);
7442     bool part = extract32(insn, 14, 1);
7443     bool is_q = extract32(insn, 30, 1);
7444     int esize = 8 << size;
7445     int i;
7446     int datasize = is_q ? 128 : 64;
7447     int elements = datasize / esize;
7448     TCGv_i64 tcg_res[2], tcg_ele;
7449 
7450     if (opcode == 0 || (size == 3 && !is_q)) {
7451         unallocated_encoding(s);
7452         return;
7453     }
7454 
7455     if (!fp_access_check(s)) {
7456         return;
7457     }
7458 
7459     tcg_res[0] = tcg_temp_new_i64();
7460     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
7461     tcg_ele = tcg_temp_new_i64();
7462 
7463     for (i = 0; i < elements; i++) {
7464         int o, w;
7465 
7466         switch (opcode) {
7467         case 1: /* UZP1/2 */
7468         {
7469             int midpoint = elements / 2;
7470             if (i < midpoint) {
7471                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
7472             } else {
7473                 read_vec_element(s, tcg_ele, rm,
7474                                  2 * (i - midpoint) + part, size);
7475             }
7476             break;
7477         }
7478         case 2: /* TRN1/2 */
7479             if (i & 1) {
7480                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
7481             } else {
7482                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
7483             }
7484             break;
7485         case 3: /* ZIP1/2 */
7486         {
7487             int base = part * elements / 2;
7488             if (i & 1) {
7489                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
7490             } else {
7491                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
7492             }
7493             break;
7494         }
7495         default:
7496             g_assert_not_reached();
7497         }
7498 
7499         w = (i * esize) / 64;
7500         o = (i * esize) % 64;
7501         if (o == 0) {
7502             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
7503         } else {
7504             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
7505             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
7506         }
7507     }
7508 
7509     for (i = 0; i <= is_q; ++i) {
7510         write_vec_element(s, tcg_res[i], rd, i, MO_64);
7511     }
7512     clear_vec_high(s, is_q, rd);
7513 }
7514 
7515 /*
7516  * do_reduction_op helper
7517  *
7518  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7519  * important for correct NaN propagation that we do these
7520  * operations in exactly the order specified by the pseudocode.
7521  *
7522  * This is a recursive function, TCG temps should be freed by the
7523  * calling function once it is done with the values.
7524  */
7525 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7526                                 int esize, int size, int vmap, TCGv_ptr fpst)
7527 {
7528     if (esize == size) {
7529         int element;
7530         MemOp msize = esize == 16 ? MO_16 : MO_32;
7531         TCGv_i32 tcg_elem;
7532 
7533         /* We should have one register left here */
7534         assert(ctpop8(vmap) == 1);
7535         element = ctz32(vmap);
7536         assert(element < 8);
7537 
7538         tcg_elem = tcg_temp_new_i32();
7539         read_vec_element_i32(s, tcg_elem, rn, element, msize);
7540         return tcg_elem;
7541     } else {
7542         int bits = size / 2;
7543         int shift = ctpop8(vmap) / 2;
7544         int vmap_lo = (vmap >> shift) & vmap;
7545         int vmap_hi = (vmap & ~vmap_lo);
7546         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7547 
7548         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7549         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7550         tcg_res = tcg_temp_new_i32();
7551 
7552         switch (fpopcode) {
7553         case 0x0c: /* fmaxnmv half-precision */
7554             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7555             break;
7556         case 0x0f: /* fmaxv half-precision */
7557             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7558             break;
7559         case 0x1c: /* fminnmv half-precision */
7560             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7561             break;
7562         case 0x1f: /* fminv half-precision */
7563             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7564             break;
7565         case 0x2c: /* fmaxnmv */
7566             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7567             break;
7568         case 0x2f: /* fmaxv */
7569             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7570             break;
7571         case 0x3c: /* fminnmv */
7572             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7573             break;
7574         case 0x3f: /* fminv */
7575             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7576             break;
7577         default:
7578             g_assert_not_reached();
7579         }
7580         return tcg_res;
7581     }
7582 }
7583 
7584 /* AdvSIMD across lanes
7585  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7586  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7587  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7588  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7589  */
7590 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7591 {
7592     int rd = extract32(insn, 0, 5);
7593     int rn = extract32(insn, 5, 5);
7594     int size = extract32(insn, 22, 2);
7595     int opcode = extract32(insn, 12, 5);
7596     bool is_q = extract32(insn, 30, 1);
7597     bool is_u = extract32(insn, 29, 1);
7598     bool is_fp = false;
7599     bool is_min = false;
7600     int esize;
7601     int elements;
7602     int i;
7603     TCGv_i64 tcg_res, tcg_elt;
7604 
7605     switch (opcode) {
7606     case 0x1b: /* ADDV */
7607         if (is_u) {
7608             unallocated_encoding(s);
7609             return;
7610         }
7611         /* fall through */
7612     case 0x3: /* SADDLV, UADDLV */
7613     case 0xa: /* SMAXV, UMAXV */
7614     case 0x1a: /* SMINV, UMINV */
7615         if (size == 3 || (size == 2 && !is_q)) {
7616             unallocated_encoding(s);
7617             return;
7618         }
7619         break;
7620     case 0xc: /* FMAXNMV, FMINNMV */
7621     case 0xf: /* FMAXV, FMINV */
7622         /* Bit 1 of size field encodes min vs max and the actual size
7623          * depends on the encoding of the U bit. If not set (and FP16
7624          * enabled) then we do half-precision float instead of single
7625          * precision.
7626          */
7627         is_min = extract32(size, 1, 1);
7628         is_fp = true;
7629         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7630             size = 1;
7631         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7632             unallocated_encoding(s);
7633             return;
7634         } else {
7635             size = 2;
7636         }
7637         break;
7638     default:
7639         unallocated_encoding(s);
7640         return;
7641     }
7642 
7643     if (!fp_access_check(s)) {
7644         return;
7645     }
7646 
7647     esize = 8 << size;
7648     elements = (is_q ? 128 : 64) / esize;
7649 
7650     tcg_res = tcg_temp_new_i64();
7651     tcg_elt = tcg_temp_new_i64();
7652 
7653     /* These instructions operate across all lanes of a vector
7654      * to produce a single result. We can guarantee that a 64
7655      * bit intermediate is sufficient:
7656      *  + for [US]ADDLV the maximum element size is 32 bits, and
7657      *    the result type is 64 bits
7658      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7659      *    same as the element size, which is 32 bits at most
7660      * For the integer operations we can choose to work at 64
7661      * or 32 bits and truncate at the end; for simplicity
7662      * we use 64 bits always. The floating point
7663      * ops do require 32 bit intermediates, though.
7664      */
7665     if (!is_fp) {
7666         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7667 
7668         for (i = 1; i < elements; i++) {
7669             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7670 
7671             switch (opcode) {
7672             case 0x03: /* SADDLV / UADDLV */
7673             case 0x1b: /* ADDV */
7674                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7675                 break;
7676             case 0x0a: /* SMAXV / UMAXV */
7677                 if (is_u) {
7678                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7679                 } else {
7680                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7681                 }
7682                 break;
7683             case 0x1a: /* SMINV / UMINV */
7684                 if (is_u) {
7685                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7686                 } else {
7687                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7688                 }
7689                 break;
7690             default:
7691                 g_assert_not_reached();
7692             }
7693 
7694         }
7695     } else {
7696         /* Floating point vector reduction ops which work across 32
7697          * bit (single) or 16 bit (half-precision) intermediates.
7698          * Note that correct NaN propagation requires that we do these
7699          * operations in exactly the order specified by the pseudocode.
7700          */
7701         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7702         int fpopcode = opcode | is_min << 4 | is_u << 5;
7703         int vmap = (1 << elements) - 1;
7704         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7705                                              (is_q ? 128 : 64), vmap, fpst);
7706         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7707     }
7708 
7709     /* Now truncate the result to the width required for the final output */
7710     if (opcode == 0x03) {
7711         /* SADDLV, UADDLV: result is 2*esize */
7712         size++;
7713     }
7714 
7715     switch (size) {
7716     case 0:
7717         tcg_gen_ext8u_i64(tcg_res, tcg_res);
7718         break;
7719     case 1:
7720         tcg_gen_ext16u_i64(tcg_res, tcg_res);
7721         break;
7722     case 2:
7723         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7724         break;
7725     case 3:
7726         break;
7727     default:
7728         g_assert_not_reached();
7729     }
7730 
7731     write_fp_dreg(s, rd, tcg_res);
7732 }
7733 
7734 /* DUP (Element, Vector)
7735  *
7736  *  31  30   29              21 20    16 15        10  9    5 4    0
7737  * +---+---+-------------------+--------+-------------+------+------+
7738  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7739  * +---+---+-------------------+--------+-------------+------+------+
7740  *
7741  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7742  */
7743 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7744                              int imm5)
7745 {
7746     int size = ctz32(imm5);
7747     int index;
7748 
7749     if (size > 3 || (size == 3 && !is_q)) {
7750         unallocated_encoding(s);
7751         return;
7752     }
7753 
7754     if (!fp_access_check(s)) {
7755         return;
7756     }
7757 
7758     index = imm5 >> (size + 1);
7759     tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7760                          vec_reg_offset(s, rn, index, size),
7761                          is_q ? 16 : 8, vec_full_reg_size(s));
7762 }
7763 
7764 /* DUP (element, scalar)
7765  *  31                   21 20    16 15        10  9    5 4    0
7766  * +-----------------------+--------+-------------+------+------+
7767  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7768  * +-----------------------+--------+-------------+------+------+
7769  */
7770 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7771                               int imm5)
7772 {
7773     int size = ctz32(imm5);
7774     int index;
7775     TCGv_i64 tmp;
7776 
7777     if (size > 3) {
7778         unallocated_encoding(s);
7779         return;
7780     }
7781 
7782     if (!fp_access_check(s)) {
7783         return;
7784     }
7785 
7786     index = imm5 >> (size + 1);
7787 
7788     /* This instruction just extracts the specified element and
7789      * zero-extends it into the bottom of the destination register.
7790      */
7791     tmp = tcg_temp_new_i64();
7792     read_vec_element(s, tmp, rn, index, size);
7793     write_fp_dreg(s, rd, tmp);
7794 }
7795 
7796 /* DUP (General)
7797  *
7798  *  31  30   29              21 20    16 15        10  9    5 4    0
7799  * +---+---+-------------------+--------+-------------+------+------+
7800  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
7801  * +---+---+-------------------+--------+-------------+------+------+
7802  *
7803  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7804  */
7805 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7806                              int imm5)
7807 {
7808     int size = ctz32(imm5);
7809     uint32_t dofs, oprsz, maxsz;
7810 
7811     if (size > 3 || ((size == 3) && !is_q)) {
7812         unallocated_encoding(s);
7813         return;
7814     }
7815 
7816     if (!fp_access_check(s)) {
7817         return;
7818     }
7819 
7820     dofs = vec_full_reg_offset(s, rd);
7821     oprsz = is_q ? 16 : 8;
7822     maxsz = vec_full_reg_size(s);
7823 
7824     tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7825 }
7826 
7827 /* INS (Element)
7828  *
7829  *  31                   21 20    16 15  14    11  10 9    5 4    0
7830  * +-----------------------+--------+------------+---+------+------+
7831  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
7832  * +-----------------------+--------+------------+---+------+------+
7833  *
7834  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7835  * index: encoded in imm5<4:size+1>
7836  */
7837 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7838                              int imm4, int imm5)
7839 {
7840     int size = ctz32(imm5);
7841     int src_index, dst_index;
7842     TCGv_i64 tmp;
7843 
7844     if (size > 3) {
7845         unallocated_encoding(s);
7846         return;
7847     }
7848 
7849     if (!fp_access_check(s)) {
7850         return;
7851     }
7852 
7853     dst_index = extract32(imm5, 1+size, 5);
7854     src_index = extract32(imm4, size, 4);
7855 
7856     tmp = tcg_temp_new_i64();
7857 
7858     read_vec_element(s, tmp, rn, src_index, size);
7859     write_vec_element(s, tmp, rd, dst_index, size);
7860 
7861     /* INS is considered a 128-bit write for SVE. */
7862     clear_vec_high(s, true, rd);
7863 }
7864 
7865 
7866 /* INS (General)
7867  *
7868  *  31                   21 20    16 15        10  9    5 4    0
7869  * +-----------------------+--------+-------------+------+------+
7870  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
7871  * +-----------------------+--------+-------------+------+------+
7872  *
7873  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7874  * index: encoded in imm5<4:size+1>
7875  */
7876 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7877 {
7878     int size = ctz32(imm5);
7879     int idx;
7880 
7881     if (size > 3) {
7882         unallocated_encoding(s);
7883         return;
7884     }
7885 
7886     if (!fp_access_check(s)) {
7887         return;
7888     }
7889 
7890     idx = extract32(imm5, 1 + size, 4 - size);
7891     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7892 
7893     /* INS is considered a 128-bit write for SVE. */
7894     clear_vec_high(s, true, rd);
7895 }
7896 
7897 /*
7898  * UMOV (General)
7899  * SMOV (General)
7900  *
7901  *  31  30   29              21 20    16 15    12   10 9    5 4    0
7902  * +---+---+-------------------+--------+-------------+------+------+
7903  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
7904  * +---+---+-------------------+--------+-------------+------+------+
7905  *
7906  * U: unsigned when set
7907  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7908  */
7909 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7910                                   int rn, int rd, int imm5)
7911 {
7912     int size = ctz32(imm5);
7913     int element;
7914     TCGv_i64 tcg_rd;
7915 
7916     /* Check for UnallocatedEncodings */
7917     if (is_signed) {
7918         if (size > 2 || (size == 2 && !is_q)) {
7919             unallocated_encoding(s);
7920             return;
7921         }
7922     } else {
7923         if (size > 3
7924             || (size < 3 && is_q)
7925             || (size == 3 && !is_q)) {
7926             unallocated_encoding(s);
7927             return;
7928         }
7929     }
7930 
7931     if (!fp_access_check(s)) {
7932         return;
7933     }
7934 
7935     element = extract32(imm5, 1+size, 4);
7936 
7937     tcg_rd = cpu_reg(s, rd);
7938     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7939     if (is_signed && !is_q) {
7940         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7941     }
7942 }
7943 
7944 /* AdvSIMD copy
7945  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
7946  * +---+---+----+-----------------+------+---+------+---+------+------+
7947  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7948  * +---+---+----+-----------------+------+---+------+---+------+------+
7949  */
7950 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7951 {
7952     int rd = extract32(insn, 0, 5);
7953     int rn = extract32(insn, 5, 5);
7954     int imm4 = extract32(insn, 11, 4);
7955     int op = extract32(insn, 29, 1);
7956     int is_q = extract32(insn, 30, 1);
7957     int imm5 = extract32(insn, 16, 5);
7958 
7959     if (op) {
7960         if (is_q) {
7961             /* INS (element) */
7962             handle_simd_inse(s, rd, rn, imm4, imm5);
7963         } else {
7964             unallocated_encoding(s);
7965         }
7966     } else {
7967         switch (imm4) {
7968         case 0:
7969             /* DUP (element - vector) */
7970             handle_simd_dupe(s, is_q, rd, rn, imm5);
7971             break;
7972         case 1:
7973             /* DUP (general) */
7974             handle_simd_dupg(s, is_q, rd, rn, imm5);
7975             break;
7976         case 3:
7977             if (is_q) {
7978                 /* INS (general) */
7979                 handle_simd_insg(s, rd, rn, imm5);
7980             } else {
7981                 unallocated_encoding(s);
7982             }
7983             break;
7984         case 5:
7985         case 7:
7986             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
7987             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
7988             break;
7989         default:
7990             unallocated_encoding(s);
7991             break;
7992         }
7993     }
7994 }
7995 
7996 /* AdvSIMD modified immediate
7997  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
7998  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7999  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
8000  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8001  *
8002  * There are a number of operations that can be carried out here:
8003  *   MOVI - move (shifted) imm into register
8004  *   MVNI - move inverted (shifted) imm into register
8005  *   ORR  - bitwise OR of (shifted) imm with register
8006  *   BIC  - bitwise clear of (shifted) imm with register
8007  * With ARMv8.2 we also have:
8008  *   FMOV half-precision
8009  */
8010 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
8011 {
8012     int rd = extract32(insn, 0, 5);
8013     int cmode = extract32(insn, 12, 4);
8014     int o2 = extract32(insn, 11, 1);
8015     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
8016     bool is_neg = extract32(insn, 29, 1);
8017     bool is_q = extract32(insn, 30, 1);
8018     uint64_t imm = 0;
8019 
8020     if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
8021         /* Check for FMOV (vector, immediate) - half-precision */
8022         if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
8023             unallocated_encoding(s);
8024             return;
8025         }
8026     }
8027 
8028     if (!fp_access_check(s)) {
8029         return;
8030     }
8031 
8032     if (cmode == 15 && o2 && !is_neg) {
8033         /* FMOV (vector, immediate) - half-precision */
8034         imm = vfp_expand_imm(MO_16, abcdefgh);
8035         /* now duplicate across the lanes */
8036         imm = dup_const(MO_16, imm);
8037     } else {
8038         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
8039     }
8040 
8041     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
8042         /* MOVI or MVNI, with MVNI negation handled above.  */
8043         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
8044                              vec_full_reg_size(s), imm);
8045     } else {
8046         /* ORR or BIC, with BIC negation to AND handled above.  */
8047         if (is_neg) {
8048             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
8049         } else {
8050             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
8051         }
8052     }
8053 }
8054 
8055 /* AdvSIMD scalar copy
8056  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
8057  * +-----+----+-----------------+------+---+------+---+------+------+
8058  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
8059  * +-----+----+-----------------+------+---+------+---+------+------+
8060  */
8061 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
8062 {
8063     int rd = extract32(insn, 0, 5);
8064     int rn = extract32(insn, 5, 5);
8065     int imm4 = extract32(insn, 11, 4);
8066     int imm5 = extract32(insn, 16, 5);
8067     int op = extract32(insn, 29, 1);
8068 
8069     if (op != 0 || imm4 != 0) {
8070         unallocated_encoding(s);
8071         return;
8072     }
8073 
8074     /* DUP (element, scalar) */
8075     handle_simd_dupes(s, rd, rn, imm5);
8076 }
8077 
8078 /* AdvSIMD scalar pairwise
8079  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
8080  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8081  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
8082  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8083  */
8084 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
8085 {
8086     int u = extract32(insn, 29, 1);
8087     int size = extract32(insn, 22, 2);
8088     int opcode = extract32(insn, 12, 5);
8089     int rn = extract32(insn, 5, 5);
8090     int rd = extract32(insn, 0, 5);
8091     TCGv_ptr fpst;
8092 
8093     /* For some ops (the FP ones), size[1] is part of the encoding.
8094      * For ADDP strictly it is not but size[1] is always 1 for valid
8095      * encodings.
8096      */
8097     opcode |= (extract32(size, 1, 1) << 5);
8098 
8099     switch (opcode) {
8100     case 0x3b: /* ADDP */
8101         if (u || size != 3) {
8102             unallocated_encoding(s);
8103             return;
8104         }
8105         if (!fp_access_check(s)) {
8106             return;
8107         }
8108 
8109         fpst = NULL;
8110         break;
8111     case 0xc: /* FMAXNMP */
8112     case 0xd: /* FADDP */
8113     case 0xf: /* FMAXP */
8114     case 0x2c: /* FMINNMP */
8115     case 0x2f: /* FMINP */
8116         /* FP op, size[0] is 32 or 64 bit*/
8117         if (!u) {
8118             if (!dc_isar_feature(aa64_fp16, s)) {
8119                 unallocated_encoding(s);
8120                 return;
8121             } else {
8122                 size = MO_16;
8123             }
8124         } else {
8125             size = extract32(size, 0, 1) ? MO_64 : MO_32;
8126         }
8127 
8128         if (!fp_access_check(s)) {
8129             return;
8130         }
8131 
8132         fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8133         break;
8134     default:
8135         unallocated_encoding(s);
8136         return;
8137     }
8138 
8139     if (size == MO_64) {
8140         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8141         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8142         TCGv_i64 tcg_res = tcg_temp_new_i64();
8143 
8144         read_vec_element(s, tcg_op1, rn, 0, MO_64);
8145         read_vec_element(s, tcg_op2, rn, 1, MO_64);
8146 
8147         switch (opcode) {
8148         case 0x3b: /* ADDP */
8149             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
8150             break;
8151         case 0xc: /* FMAXNMP */
8152             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8153             break;
8154         case 0xd: /* FADDP */
8155             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8156             break;
8157         case 0xf: /* FMAXP */
8158             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8159             break;
8160         case 0x2c: /* FMINNMP */
8161             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8162             break;
8163         case 0x2f: /* FMINP */
8164             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8165             break;
8166         default:
8167             g_assert_not_reached();
8168         }
8169 
8170         write_fp_dreg(s, rd, tcg_res);
8171     } else {
8172         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8173         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8174         TCGv_i32 tcg_res = tcg_temp_new_i32();
8175 
8176         read_vec_element_i32(s, tcg_op1, rn, 0, size);
8177         read_vec_element_i32(s, tcg_op2, rn, 1, size);
8178 
8179         if (size == MO_16) {
8180             switch (opcode) {
8181             case 0xc: /* FMAXNMP */
8182                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8183                 break;
8184             case 0xd: /* FADDP */
8185                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
8186                 break;
8187             case 0xf: /* FMAXP */
8188                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
8189                 break;
8190             case 0x2c: /* FMINNMP */
8191                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8192                 break;
8193             case 0x2f: /* FMINP */
8194                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
8195                 break;
8196             default:
8197                 g_assert_not_reached();
8198             }
8199         } else {
8200             switch (opcode) {
8201             case 0xc: /* FMAXNMP */
8202                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8203                 break;
8204             case 0xd: /* FADDP */
8205                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8206                 break;
8207             case 0xf: /* FMAXP */
8208                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8209                 break;
8210             case 0x2c: /* FMINNMP */
8211                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8212                 break;
8213             case 0x2f: /* FMINP */
8214                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8215                 break;
8216             default:
8217                 g_assert_not_reached();
8218             }
8219         }
8220 
8221         write_fp_sreg(s, rd, tcg_res);
8222     }
8223 }
8224 
8225 /*
8226  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8227  *
8228  * This code is handles the common shifting code and is used by both
8229  * the vector and scalar code.
8230  */
8231 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8232                                     TCGv_i64 tcg_rnd, bool accumulate,
8233                                     bool is_u, int size, int shift)
8234 {
8235     bool extended_result = false;
8236     bool round = tcg_rnd != NULL;
8237     int ext_lshift = 0;
8238     TCGv_i64 tcg_src_hi;
8239 
8240     if (round && size == 3) {
8241         extended_result = true;
8242         ext_lshift = 64 - shift;
8243         tcg_src_hi = tcg_temp_new_i64();
8244     } else if (shift == 64) {
8245         if (!accumulate && is_u) {
8246             /* result is zero */
8247             tcg_gen_movi_i64(tcg_res, 0);
8248             return;
8249         }
8250     }
8251 
8252     /* Deal with the rounding step */
8253     if (round) {
8254         if (extended_result) {
8255             TCGv_i64 tcg_zero = tcg_constant_i64(0);
8256             if (!is_u) {
8257                 /* take care of sign extending tcg_res */
8258                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8259                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8260                                  tcg_src, tcg_src_hi,
8261                                  tcg_rnd, tcg_zero);
8262             } else {
8263                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8264                                  tcg_src, tcg_zero,
8265                                  tcg_rnd, tcg_zero);
8266             }
8267         } else {
8268             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8269         }
8270     }
8271 
8272     /* Now do the shift right */
8273     if (round && extended_result) {
8274         /* extended case, >64 bit precision required */
8275         if (ext_lshift == 0) {
8276             /* special case, only high bits matter */
8277             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8278         } else {
8279             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8280             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8281             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8282         }
8283     } else {
8284         if (is_u) {
8285             if (shift == 64) {
8286                 /* essentially shifting in 64 zeros */
8287                 tcg_gen_movi_i64(tcg_src, 0);
8288             } else {
8289                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8290             }
8291         } else {
8292             if (shift == 64) {
8293                 /* effectively extending the sign-bit */
8294                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8295             } else {
8296                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8297             }
8298         }
8299     }
8300 
8301     if (accumulate) {
8302         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8303     } else {
8304         tcg_gen_mov_i64(tcg_res, tcg_src);
8305     }
8306 }
8307 
8308 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8309 static void handle_scalar_simd_shri(DisasContext *s,
8310                                     bool is_u, int immh, int immb,
8311                                     int opcode, int rn, int rd)
8312 {
8313     const int size = 3;
8314     int immhb = immh << 3 | immb;
8315     int shift = 2 * (8 << size) - immhb;
8316     bool accumulate = false;
8317     bool round = false;
8318     bool insert = false;
8319     TCGv_i64 tcg_rn;
8320     TCGv_i64 tcg_rd;
8321     TCGv_i64 tcg_round;
8322 
8323     if (!extract32(immh, 3, 1)) {
8324         unallocated_encoding(s);
8325         return;
8326     }
8327 
8328     if (!fp_access_check(s)) {
8329         return;
8330     }
8331 
8332     switch (opcode) {
8333     case 0x02: /* SSRA / USRA (accumulate) */
8334         accumulate = true;
8335         break;
8336     case 0x04: /* SRSHR / URSHR (rounding) */
8337         round = true;
8338         break;
8339     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8340         accumulate = round = true;
8341         break;
8342     case 0x08: /* SRI */
8343         insert = true;
8344         break;
8345     }
8346 
8347     if (round) {
8348         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8349     } else {
8350         tcg_round = NULL;
8351     }
8352 
8353     tcg_rn = read_fp_dreg(s, rn);
8354     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8355 
8356     if (insert) {
8357         /* shift count same as element size is valid but does nothing;
8358          * special case to avoid potential shift by 64.
8359          */
8360         int esize = 8 << size;
8361         if (shift != esize) {
8362             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8363             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8364         }
8365     } else {
8366         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8367                                 accumulate, is_u, size, shift);
8368     }
8369 
8370     write_fp_dreg(s, rd, tcg_rd);
8371 }
8372 
8373 /* SHL/SLI - Scalar shift left */
8374 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8375                                     int immh, int immb, int opcode,
8376                                     int rn, int rd)
8377 {
8378     int size = 32 - clz32(immh) - 1;
8379     int immhb = immh << 3 | immb;
8380     int shift = immhb - (8 << size);
8381     TCGv_i64 tcg_rn;
8382     TCGv_i64 tcg_rd;
8383 
8384     if (!extract32(immh, 3, 1)) {
8385         unallocated_encoding(s);
8386         return;
8387     }
8388 
8389     if (!fp_access_check(s)) {
8390         return;
8391     }
8392 
8393     tcg_rn = read_fp_dreg(s, rn);
8394     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8395 
8396     if (insert) {
8397         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8398     } else {
8399         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8400     }
8401 
8402     write_fp_dreg(s, rd, tcg_rd);
8403 }
8404 
8405 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8406  * (signed/unsigned) narrowing */
8407 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8408                                    bool is_u_shift, bool is_u_narrow,
8409                                    int immh, int immb, int opcode,
8410                                    int rn, int rd)
8411 {
8412     int immhb = immh << 3 | immb;
8413     int size = 32 - clz32(immh) - 1;
8414     int esize = 8 << size;
8415     int shift = (2 * esize) - immhb;
8416     int elements = is_scalar ? 1 : (64 / esize);
8417     bool round = extract32(opcode, 0, 1);
8418     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8419     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8420     TCGv_i32 tcg_rd_narrowed;
8421     TCGv_i64 tcg_final;
8422 
8423     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8424         { gen_helper_neon_narrow_sat_s8,
8425           gen_helper_neon_unarrow_sat8 },
8426         { gen_helper_neon_narrow_sat_s16,
8427           gen_helper_neon_unarrow_sat16 },
8428         { gen_helper_neon_narrow_sat_s32,
8429           gen_helper_neon_unarrow_sat32 },
8430         { NULL, NULL },
8431     };
8432     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8433         gen_helper_neon_narrow_sat_u8,
8434         gen_helper_neon_narrow_sat_u16,
8435         gen_helper_neon_narrow_sat_u32,
8436         NULL
8437     };
8438     NeonGenNarrowEnvFn *narrowfn;
8439 
8440     int i;
8441 
8442     assert(size < 4);
8443 
8444     if (extract32(immh, 3, 1)) {
8445         unallocated_encoding(s);
8446         return;
8447     }
8448 
8449     if (!fp_access_check(s)) {
8450         return;
8451     }
8452 
8453     if (is_u_shift) {
8454         narrowfn = unsigned_narrow_fns[size];
8455     } else {
8456         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8457     }
8458 
8459     tcg_rn = tcg_temp_new_i64();
8460     tcg_rd = tcg_temp_new_i64();
8461     tcg_rd_narrowed = tcg_temp_new_i32();
8462     tcg_final = tcg_temp_new_i64();
8463 
8464     if (round) {
8465         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8466     } else {
8467         tcg_round = NULL;
8468     }
8469 
8470     for (i = 0; i < elements; i++) {
8471         read_vec_element(s, tcg_rn, rn, i, ldop);
8472         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8473                                 false, is_u_shift, size+1, shift);
8474         narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8475         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8476         if (i == 0) {
8477             tcg_gen_mov_i64(tcg_final, tcg_rd);
8478         } else {
8479             tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8480         }
8481     }
8482 
8483     if (!is_q) {
8484         write_vec_element(s, tcg_final, rd, 0, MO_64);
8485     } else {
8486         write_vec_element(s, tcg_final, rd, 1, MO_64);
8487     }
8488     clear_vec_high(s, is_q, rd);
8489 }
8490 
8491 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8492 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8493                              bool src_unsigned, bool dst_unsigned,
8494                              int immh, int immb, int rn, int rd)
8495 {
8496     int immhb = immh << 3 | immb;
8497     int size = 32 - clz32(immh) - 1;
8498     int shift = immhb - (8 << size);
8499     int pass;
8500 
8501     assert(immh != 0);
8502     assert(!(scalar && is_q));
8503 
8504     if (!scalar) {
8505         if (!is_q && extract32(immh, 3, 1)) {
8506             unallocated_encoding(s);
8507             return;
8508         }
8509 
8510         /* Since we use the variable-shift helpers we must
8511          * replicate the shift count into each element of
8512          * the tcg_shift value.
8513          */
8514         switch (size) {
8515         case 0:
8516             shift |= shift << 8;
8517             /* fall through */
8518         case 1:
8519             shift |= shift << 16;
8520             break;
8521         case 2:
8522         case 3:
8523             break;
8524         default:
8525             g_assert_not_reached();
8526         }
8527     }
8528 
8529     if (!fp_access_check(s)) {
8530         return;
8531     }
8532 
8533     if (size == 3) {
8534         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8535         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8536             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8537             { NULL, gen_helper_neon_qshl_u64 },
8538         };
8539         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8540         int maxpass = is_q ? 2 : 1;
8541 
8542         for (pass = 0; pass < maxpass; pass++) {
8543             TCGv_i64 tcg_op = tcg_temp_new_i64();
8544 
8545             read_vec_element(s, tcg_op, rn, pass, MO_64);
8546             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8547             write_vec_element(s, tcg_op, rd, pass, MO_64);
8548         }
8549         clear_vec_high(s, is_q, rd);
8550     } else {
8551         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8552         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8553             {
8554                 { gen_helper_neon_qshl_s8,
8555                   gen_helper_neon_qshl_s16,
8556                   gen_helper_neon_qshl_s32 },
8557                 { gen_helper_neon_qshlu_s8,
8558                   gen_helper_neon_qshlu_s16,
8559                   gen_helper_neon_qshlu_s32 }
8560             }, {
8561                 { NULL, NULL, NULL },
8562                 { gen_helper_neon_qshl_u8,
8563                   gen_helper_neon_qshl_u16,
8564                   gen_helper_neon_qshl_u32 }
8565             }
8566         };
8567         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8568         MemOp memop = scalar ? size : MO_32;
8569         int maxpass = scalar ? 1 : is_q ? 4 : 2;
8570 
8571         for (pass = 0; pass < maxpass; pass++) {
8572             TCGv_i32 tcg_op = tcg_temp_new_i32();
8573 
8574             read_vec_element_i32(s, tcg_op, rn, pass, memop);
8575             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8576             if (scalar) {
8577                 switch (size) {
8578                 case 0:
8579                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
8580                     break;
8581                 case 1:
8582                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8583                     break;
8584                 case 2:
8585                     break;
8586                 default:
8587                     g_assert_not_reached();
8588                 }
8589                 write_fp_sreg(s, rd, tcg_op);
8590             } else {
8591                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8592             }
8593         }
8594 
8595         if (!scalar) {
8596             clear_vec_high(s, is_q, rd);
8597         }
8598     }
8599 }
8600 
8601 /* Common vector code for handling integer to FP conversion */
8602 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8603                                    int elements, int is_signed,
8604                                    int fracbits, int size)
8605 {
8606     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8607     TCGv_i32 tcg_shift = NULL;
8608 
8609     MemOp mop = size | (is_signed ? MO_SIGN : 0);
8610     int pass;
8611 
8612     if (fracbits || size == MO_64) {
8613         tcg_shift = tcg_constant_i32(fracbits);
8614     }
8615 
8616     if (size == MO_64) {
8617         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8618         TCGv_i64 tcg_double = tcg_temp_new_i64();
8619 
8620         for (pass = 0; pass < elements; pass++) {
8621             read_vec_element(s, tcg_int64, rn, pass, mop);
8622 
8623             if (is_signed) {
8624                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8625                                      tcg_shift, tcg_fpst);
8626             } else {
8627                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8628                                      tcg_shift, tcg_fpst);
8629             }
8630             if (elements == 1) {
8631                 write_fp_dreg(s, rd, tcg_double);
8632             } else {
8633                 write_vec_element(s, tcg_double, rd, pass, MO_64);
8634             }
8635         }
8636     } else {
8637         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8638         TCGv_i32 tcg_float = tcg_temp_new_i32();
8639 
8640         for (pass = 0; pass < elements; pass++) {
8641             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8642 
8643             switch (size) {
8644             case MO_32:
8645                 if (fracbits) {
8646                     if (is_signed) {
8647                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
8648                                              tcg_shift, tcg_fpst);
8649                     } else {
8650                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
8651                                              tcg_shift, tcg_fpst);
8652                     }
8653                 } else {
8654                     if (is_signed) {
8655                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8656                     } else {
8657                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8658                     }
8659                 }
8660                 break;
8661             case MO_16:
8662                 if (fracbits) {
8663                     if (is_signed) {
8664                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8665                                              tcg_shift, tcg_fpst);
8666                     } else {
8667                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8668                                              tcg_shift, tcg_fpst);
8669                     }
8670                 } else {
8671                     if (is_signed) {
8672                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8673                     } else {
8674                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8675                     }
8676                 }
8677                 break;
8678             default:
8679                 g_assert_not_reached();
8680             }
8681 
8682             if (elements == 1) {
8683                 write_fp_sreg(s, rd, tcg_float);
8684             } else {
8685                 write_vec_element_i32(s, tcg_float, rd, pass, size);
8686             }
8687         }
8688     }
8689 
8690     clear_vec_high(s, elements << size == 16, rd);
8691 }
8692 
8693 /* UCVTF/SCVTF - Integer to FP conversion */
8694 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8695                                          bool is_q, bool is_u,
8696                                          int immh, int immb, int opcode,
8697                                          int rn, int rd)
8698 {
8699     int size, elements, fracbits;
8700     int immhb = immh << 3 | immb;
8701 
8702     if (immh & 8) {
8703         size = MO_64;
8704         if (!is_scalar && !is_q) {
8705             unallocated_encoding(s);
8706             return;
8707         }
8708     } else if (immh & 4) {
8709         size = MO_32;
8710     } else if (immh & 2) {
8711         size = MO_16;
8712         if (!dc_isar_feature(aa64_fp16, s)) {
8713             unallocated_encoding(s);
8714             return;
8715         }
8716     } else {
8717         /* immh == 0 would be a failure of the decode logic */
8718         g_assert(immh == 1);
8719         unallocated_encoding(s);
8720         return;
8721     }
8722 
8723     if (is_scalar) {
8724         elements = 1;
8725     } else {
8726         elements = (8 << is_q) >> size;
8727     }
8728     fracbits = (16 << size) - immhb;
8729 
8730     if (!fp_access_check(s)) {
8731         return;
8732     }
8733 
8734     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8735 }
8736 
8737 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8738 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8739                                          bool is_q, bool is_u,
8740                                          int immh, int immb, int rn, int rd)
8741 {
8742     int immhb = immh << 3 | immb;
8743     int pass, size, fracbits;
8744     TCGv_ptr tcg_fpstatus;
8745     TCGv_i32 tcg_rmode, tcg_shift;
8746 
8747     if (immh & 0x8) {
8748         size = MO_64;
8749         if (!is_scalar && !is_q) {
8750             unallocated_encoding(s);
8751             return;
8752         }
8753     } else if (immh & 0x4) {
8754         size = MO_32;
8755     } else if (immh & 0x2) {
8756         size = MO_16;
8757         if (!dc_isar_feature(aa64_fp16, s)) {
8758             unallocated_encoding(s);
8759             return;
8760         }
8761     } else {
8762         /* Should have split out AdvSIMD modified immediate earlier.  */
8763         assert(immh == 1);
8764         unallocated_encoding(s);
8765         return;
8766     }
8767 
8768     if (!fp_access_check(s)) {
8769         return;
8770     }
8771 
8772     assert(!(is_scalar && is_q));
8773 
8774     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8775     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
8776     fracbits = (16 << size) - immhb;
8777     tcg_shift = tcg_constant_i32(fracbits);
8778 
8779     if (size == MO_64) {
8780         int maxpass = is_scalar ? 1 : 2;
8781 
8782         for (pass = 0; pass < maxpass; pass++) {
8783             TCGv_i64 tcg_op = tcg_temp_new_i64();
8784 
8785             read_vec_element(s, tcg_op, rn, pass, MO_64);
8786             if (is_u) {
8787                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8788             } else {
8789                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8790             }
8791             write_vec_element(s, tcg_op, rd, pass, MO_64);
8792         }
8793         clear_vec_high(s, is_q, rd);
8794     } else {
8795         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8796         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8797 
8798         switch (size) {
8799         case MO_16:
8800             if (is_u) {
8801                 fn = gen_helper_vfp_touhh;
8802             } else {
8803                 fn = gen_helper_vfp_toshh;
8804             }
8805             break;
8806         case MO_32:
8807             if (is_u) {
8808                 fn = gen_helper_vfp_touls;
8809             } else {
8810                 fn = gen_helper_vfp_tosls;
8811             }
8812             break;
8813         default:
8814             g_assert_not_reached();
8815         }
8816 
8817         for (pass = 0; pass < maxpass; pass++) {
8818             TCGv_i32 tcg_op = tcg_temp_new_i32();
8819 
8820             read_vec_element_i32(s, tcg_op, rn, pass, size);
8821             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8822             if (is_scalar) {
8823                 write_fp_sreg(s, rd, tcg_op);
8824             } else {
8825                 write_vec_element_i32(s, tcg_op, rd, pass, size);
8826             }
8827         }
8828         if (!is_scalar) {
8829             clear_vec_high(s, is_q, rd);
8830         }
8831     }
8832 
8833     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
8834 }
8835 
8836 /* AdvSIMD scalar shift by immediate
8837  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
8838  * +-----+---+-------------+------+------+--------+---+------+------+
8839  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8840  * +-----+---+-------------+------+------+--------+---+------+------+
8841  *
8842  * This is the scalar version so it works on a fixed sized registers
8843  */
8844 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8845 {
8846     int rd = extract32(insn, 0, 5);
8847     int rn = extract32(insn, 5, 5);
8848     int opcode = extract32(insn, 11, 5);
8849     int immb = extract32(insn, 16, 3);
8850     int immh = extract32(insn, 19, 4);
8851     bool is_u = extract32(insn, 29, 1);
8852 
8853     if (immh == 0) {
8854         unallocated_encoding(s);
8855         return;
8856     }
8857 
8858     switch (opcode) {
8859     case 0x08: /* SRI */
8860         if (!is_u) {
8861             unallocated_encoding(s);
8862             return;
8863         }
8864         /* fall through */
8865     case 0x00: /* SSHR / USHR */
8866     case 0x02: /* SSRA / USRA */
8867     case 0x04: /* SRSHR / URSHR */
8868     case 0x06: /* SRSRA / URSRA */
8869         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8870         break;
8871     case 0x0a: /* SHL / SLI */
8872         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8873         break;
8874     case 0x1c: /* SCVTF, UCVTF */
8875         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8876                                      opcode, rn, rd);
8877         break;
8878     case 0x10: /* SQSHRUN, SQSHRUN2 */
8879     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8880         if (!is_u) {
8881             unallocated_encoding(s);
8882             return;
8883         }
8884         handle_vec_simd_sqshrn(s, true, false, false, true,
8885                                immh, immb, opcode, rn, rd);
8886         break;
8887     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8888     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8889         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8890                                immh, immb, opcode, rn, rd);
8891         break;
8892     case 0xc: /* SQSHLU */
8893         if (!is_u) {
8894             unallocated_encoding(s);
8895             return;
8896         }
8897         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8898         break;
8899     case 0xe: /* SQSHL, UQSHL */
8900         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8901         break;
8902     case 0x1f: /* FCVTZS, FCVTZU */
8903         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8904         break;
8905     default:
8906         unallocated_encoding(s);
8907         break;
8908     }
8909 }
8910 
8911 /* AdvSIMD scalar three different
8912  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
8913  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8914  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
8915  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8916  */
8917 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8918 {
8919     bool is_u = extract32(insn, 29, 1);
8920     int size = extract32(insn, 22, 2);
8921     int opcode = extract32(insn, 12, 4);
8922     int rm = extract32(insn, 16, 5);
8923     int rn = extract32(insn, 5, 5);
8924     int rd = extract32(insn, 0, 5);
8925 
8926     if (is_u) {
8927         unallocated_encoding(s);
8928         return;
8929     }
8930 
8931     switch (opcode) {
8932     case 0x9: /* SQDMLAL, SQDMLAL2 */
8933     case 0xb: /* SQDMLSL, SQDMLSL2 */
8934     case 0xd: /* SQDMULL, SQDMULL2 */
8935         if (size == 0 || size == 3) {
8936             unallocated_encoding(s);
8937             return;
8938         }
8939         break;
8940     default:
8941         unallocated_encoding(s);
8942         return;
8943     }
8944 
8945     if (!fp_access_check(s)) {
8946         return;
8947     }
8948 
8949     if (size == 2) {
8950         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8951         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8952         TCGv_i64 tcg_res = tcg_temp_new_i64();
8953 
8954         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8955         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8956 
8957         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8958         gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
8959 
8960         switch (opcode) {
8961         case 0xd: /* SQDMULL, SQDMULL2 */
8962             break;
8963         case 0xb: /* SQDMLSL, SQDMLSL2 */
8964             tcg_gen_neg_i64(tcg_res, tcg_res);
8965             /* fall through */
8966         case 0x9: /* SQDMLAL, SQDMLAL2 */
8967             read_vec_element(s, tcg_op1, rd, 0, MO_64);
8968             gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
8969                                               tcg_res, tcg_op1);
8970             break;
8971         default:
8972             g_assert_not_reached();
8973         }
8974 
8975         write_fp_dreg(s, rd, tcg_res);
8976     } else {
8977         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8978         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8979         TCGv_i64 tcg_res = tcg_temp_new_i64();
8980 
8981         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8982         gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
8983 
8984         switch (opcode) {
8985         case 0xd: /* SQDMULL, SQDMULL2 */
8986             break;
8987         case 0xb: /* SQDMLSL, SQDMLSL2 */
8988             gen_helper_neon_negl_u32(tcg_res, tcg_res);
8989             /* fall through */
8990         case 0x9: /* SQDMLAL, SQDMLAL2 */
8991         {
8992             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
8993             read_vec_element(s, tcg_op3, rd, 0, MO_32);
8994             gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
8995                                               tcg_res, tcg_op3);
8996             break;
8997         }
8998         default:
8999             g_assert_not_reached();
9000         }
9001 
9002         tcg_gen_ext32u_i64(tcg_res, tcg_res);
9003         write_fp_dreg(s, rd, tcg_res);
9004     }
9005 }
9006 
9007 static void handle_3same_64(DisasContext *s, int opcode, bool u,
9008                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
9009 {
9010     /* Handle 64x64->64 opcodes which are shared between the scalar
9011      * and vector 3-same groups. We cover every opcode where size == 3
9012      * is valid in either the three-reg-same (integer, not pairwise)
9013      * or scalar-three-reg-same groups.
9014      */
9015     TCGCond cond;
9016 
9017     switch (opcode) {
9018     case 0x1: /* SQADD */
9019         if (u) {
9020             gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9021         } else {
9022             gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9023         }
9024         break;
9025     case 0x5: /* SQSUB */
9026         if (u) {
9027             gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9028         } else {
9029             gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9030         }
9031         break;
9032     case 0x6: /* CMGT, CMHI */
9033         /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
9034          * We implement this using setcond (test) and then negating.
9035          */
9036         cond = u ? TCG_COND_GTU : TCG_COND_GT;
9037     do_cmop:
9038         tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
9039         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9040         break;
9041     case 0x7: /* CMGE, CMHS */
9042         cond = u ? TCG_COND_GEU : TCG_COND_GE;
9043         goto do_cmop;
9044     case 0x11: /* CMTST, CMEQ */
9045         if (u) {
9046             cond = TCG_COND_EQ;
9047             goto do_cmop;
9048         }
9049         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
9050         break;
9051     case 0x8: /* SSHL, USHL */
9052         if (u) {
9053             gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
9054         } else {
9055             gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
9056         }
9057         break;
9058     case 0x9: /* SQSHL, UQSHL */
9059         if (u) {
9060             gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9061         } else {
9062             gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9063         }
9064         break;
9065     case 0xa: /* SRSHL, URSHL */
9066         if (u) {
9067             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
9068         } else {
9069             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
9070         }
9071         break;
9072     case 0xb: /* SQRSHL, UQRSHL */
9073         if (u) {
9074             gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9075         } else {
9076             gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9077         }
9078         break;
9079     case 0x10: /* ADD, SUB */
9080         if (u) {
9081             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
9082         } else {
9083             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
9084         }
9085         break;
9086     default:
9087         g_assert_not_reached();
9088     }
9089 }
9090 
9091 /* Handle the 3-same-operands float operations; shared by the scalar
9092  * and vector encodings. The caller must filter out any encodings
9093  * not allocated for the encoding it is dealing with.
9094  */
9095 static void handle_3same_float(DisasContext *s, int size, int elements,
9096                                int fpopcode, int rd, int rn, int rm)
9097 {
9098     int pass;
9099     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9100 
9101     for (pass = 0; pass < elements; pass++) {
9102         if (size) {
9103             /* Double */
9104             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9105             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9106             TCGv_i64 tcg_res = tcg_temp_new_i64();
9107 
9108             read_vec_element(s, tcg_op1, rn, pass, MO_64);
9109             read_vec_element(s, tcg_op2, rm, pass, MO_64);
9110 
9111             switch (fpopcode) {
9112             case 0x39: /* FMLS */
9113                 /* As usual for ARM, separate negation for fused multiply-add */
9114                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
9115                 /* fall through */
9116             case 0x19: /* FMLA */
9117                 read_vec_element(s, tcg_res, rd, pass, MO_64);
9118                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
9119                                        tcg_res, fpst);
9120                 break;
9121             case 0x18: /* FMAXNM */
9122                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9123                 break;
9124             case 0x1a: /* FADD */
9125                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
9126                 break;
9127             case 0x1b: /* FMULX */
9128                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
9129                 break;
9130             case 0x1c: /* FCMEQ */
9131                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9132                 break;
9133             case 0x1e: /* FMAX */
9134                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
9135                 break;
9136             case 0x1f: /* FRECPS */
9137                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9138                 break;
9139             case 0x38: /* FMINNM */
9140                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9141                 break;
9142             case 0x3a: /* FSUB */
9143                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9144                 break;
9145             case 0x3e: /* FMIN */
9146                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
9147                 break;
9148             case 0x3f: /* FRSQRTS */
9149                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9150                 break;
9151             case 0x5b: /* FMUL */
9152                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
9153                 break;
9154             case 0x5c: /* FCMGE */
9155                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9156                 break;
9157             case 0x5d: /* FACGE */
9158                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9159                 break;
9160             case 0x5f: /* FDIV */
9161                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
9162                 break;
9163             case 0x7a: /* FABD */
9164                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9165                 gen_helper_vfp_absd(tcg_res, tcg_res);
9166                 break;
9167             case 0x7c: /* FCMGT */
9168                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9169                 break;
9170             case 0x7d: /* FACGT */
9171                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9172                 break;
9173             default:
9174                 g_assert_not_reached();
9175             }
9176 
9177             write_vec_element(s, tcg_res, rd, pass, MO_64);
9178         } else {
9179             /* Single */
9180             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9181             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9182             TCGv_i32 tcg_res = tcg_temp_new_i32();
9183 
9184             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9185             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9186 
9187             switch (fpopcode) {
9188             case 0x39: /* FMLS */
9189                 /* As usual for ARM, separate negation for fused multiply-add */
9190                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
9191                 /* fall through */
9192             case 0x19: /* FMLA */
9193                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9194                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
9195                                        tcg_res, fpst);
9196                 break;
9197             case 0x1a: /* FADD */
9198                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
9199                 break;
9200             case 0x1b: /* FMULX */
9201                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
9202                 break;
9203             case 0x1c: /* FCMEQ */
9204                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9205                 break;
9206             case 0x1e: /* FMAX */
9207                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
9208                 break;
9209             case 0x1f: /* FRECPS */
9210                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9211                 break;
9212             case 0x18: /* FMAXNM */
9213                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
9214                 break;
9215             case 0x38: /* FMINNM */
9216                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
9217                 break;
9218             case 0x3a: /* FSUB */
9219                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9220                 break;
9221             case 0x3e: /* FMIN */
9222                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9223                 break;
9224             case 0x3f: /* FRSQRTS */
9225                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9226                 break;
9227             case 0x5b: /* FMUL */
9228                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9229                 break;
9230             case 0x5c: /* FCMGE */
9231                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9232                 break;
9233             case 0x5d: /* FACGE */
9234                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9235                 break;
9236             case 0x5f: /* FDIV */
9237                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9238                 break;
9239             case 0x7a: /* FABD */
9240                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9241                 gen_helper_vfp_abss(tcg_res, tcg_res);
9242                 break;
9243             case 0x7c: /* FCMGT */
9244                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9245                 break;
9246             case 0x7d: /* FACGT */
9247                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9248                 break;
9249             default:
9250                 g_assert_not_reached();
9251             }
9252 
9253             if (elements == 1) {
9254                 /* scalar single so clear high part */
9255                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9256 
9257                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9258                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9259             } else {
9260                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9261             }
9262         }
9263     }
9264 
9265     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9266 }
9267 
9268 /* AdvSIMD scalar three same
9269  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
9270  * +-----+---+-----------+------+---+------+--------+---+------+------+
9271  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
9272  * +-----+---+-----------+------+---+------+--------+---+------+------+
9273  */
9274 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9275 {
9276     int rd = extract32(insn, 0, 5);
9277     int rn = extract32(insn, 5, 5);
9278     int opcode = extract32(insn, 11, 5);
9279     int rm = extract32(insn, 16, 5);
9280     int size = extract32(insn, 22, 2);
9281     bool u = extract32(insn, 29, 1);
9282     TCGv_i64 tcg_rd;
9283 
9284     if (opcode >= 0x18) {
9285         /* Floating point: U, size[1] and opcode indicate operation */
9286         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9287         switch (fpopcode) {
9288         case 0x1b: /* FMULX */
9289         case 0x1f: /* FRECPS */
9290         case 0x3f: /* FRSQRTS */
9291         case 0x5d: /* FACGE */
9292         case 0x7d: /* FACGT */
9293         case 0x1c: /* FCMEQ */
9294         case 0x5c: /* FCMGE */
9295         case 0x7c: /* FCMGT */
9296         case 0x7a: /* FABD */
9297             break;
9298         default:
9299             unallocated_encoding(s);
9300             return;
9301         }
9302 
9303         if (!fp_access_check(s)) {
9304             return;
9305         }
9306 
9307         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9308         return;
9309     }
9310 
9311     switch (opcode) {
9312     case 0x1: /* SQADD, UQADD */
9313     case 0x5: /* SQSUB, UQSUB */
9314     case 0x9: /* SQSHL, UQSHL */
9315     case 0xb: /* SQRSHL, UQRSHL */
9316         break;
9317     case 0x8: /* SSHL, USHL */
9318     case 0xa: /* SRSHL, URSHL */
9319     case 0x6: /* CMGT, CMHI */
9320     case 0x7: /* CMGE, CMHS */
9321     case 0x11: /* CMTST, CMEQ */
9322     case 0x10: /* ADD, SUB (vector) */
9323         if (size != 3) {
9324             unallocated_encoding(s);
9325             return;
9326         }
9327         break;
9328     case 0x16: /* SQDMULH, SQRDMULH (vector) */
9329         if (size != 1 && size != 2) {
9330             unallocated_encoding(s);
9331             return;
9332         }
9333         break;
9334     default:
9335         unallocated_encoding(s);
9336         return;
9337     }
9338 
9339     if (!fp_access_check(s)) {
9340         return;
9341     }
9342 
9343     tcg_rd = tcg_temp_new_i64();
9344 
9345     if (size == 3) {
9346         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9347         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9348 
9349         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9350     } else {
9351         /* Do a single operation on the lowest element in the vector.
9352          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9353          * no side effects for all these operations.
9354          * OPTME: special-purpose helpers would avoid doing some
9355          * unnecessary work in the helper for the 8 and 16 bit cases.
9356          */
9357         NeonGenTwoOpEnvFn *genenvfn;
9358         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9359         TCGv_i32 tcg_rm = tcg_temp_new_i32();
9360         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9361 
9362         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9363         read_vec_element_i32(s, tcg_rm, rm, 0, size);
9364 
9365         switch (opcode) {
9366         case 0x1: /* SQADD, UQADD */
9367         {
9368             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9369                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9370                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9371                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9372             };
9373             genenvfn = fns[size][u];
9374             break;
9375         }
9376         case 0x5: /* SQSUB, UQSUB */
9377         {
9378             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9379                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9380                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9381                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9382             };
9383             genenvfn = fns[size][u];
9384             break;
9385         }
9386         case 0x9: /* SQSHL, UQSHL */
9387         {
9388             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9389                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9390                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9391                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9392             };
9393             genenvfn = fns[size][u];
9394             break;
9395         }
9396         case 0xb: /* SQRSHL, UQRSHL */
9397         {
9398             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9399                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9400                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9401                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9402             };
9403             genenvfn = fns[size][u];
9404             break;
9405         }
9406         case 0x16: /* SQDMULH, SQRDMULH */
9407         {
9408             static NeonGenTwoOpEnvFn * const fns[2][2] = {
9409                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9410                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9411             };
9412             assert(size == 1 || size == 2);
9413             genenvfn = fns[size - 1][u];
9414             break;
9415         }
9416         default:
9417             g_assert_not_reached();
9418         }
9419 
9420         genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9421         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9422     }
9423 
9424     write_fp_dreg(s, rd, tcg_rd);
9425 }
9426 
9427 /* AdvSIMD scalar three same FP16
9428  *  31 30  29 28       24 23  22 21 20  16 15 14 13    11 10  9  5 4  0
9429  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9430  * | 0 1 | U | 1 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 | Rn | Rd |
9431  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9432  * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9433  * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9434  */
9435 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9436                                                   uint32_t insn)
9437 {
9438     int rd = extract32(insn, 0, 5);
9439     int rn = extract32(insn, 5, 5);
9440     int opcode = extract32(insn, 11, 3);
9441     int rm = extract32(insn, 16, 5);
9442     bool u = extract32(insn, 29, 1);
9443     bool a = extract32(insn, 23, 1);
9444     int fpopcode = opcode | (a << 3) |  (u << 4);
9445     TCGv_ptr fpst;
9446     TCGv_i32 tcg_op1;
9447     TCGv_i32 tcg_op2;
9448     TCGv_i32 tcg_res;
9449 
9450     switch (fpopcode) {
9451     case 0x03: /* FMULX */
9452     case 0x04: /* FCMEQ (reg) */
9453     case 0x07: /* FRECPS */
9454     case 0x0f: /* FRSQRTS */
9455     case 0x14: /* FCMGE (reg) */
9456     case 0x15: /* FACGE */
9457     case 0x1a: /* FABD */
9458     case 0x1c: /* FCMGT (reg) */
9459     case 0x1d: /* FACGT */
9460         break;
9461     default:
9462         unallocated_encoding(s);
9463         return;
9464     }
9465 
9466     if (!dc_isar_feature(aa64_fp16, s)) {
9467         unallocated_encoding(s);
9468     }
9469 
9470     if (!fp_access_check(s)) {
9471         return;
9472     }
9473 
9474     fpst = fpstatus_ptr(FPST_FPCR_F16);
9475 
9476     tcg_op1 = read_fp_hreg(s, rn);
9477     tcg_op2 = read_fp_hreg(s, rm);
9478     tcg_res = tcg_temp_new_i32();
9479 
9480     switch (fpopcode) {
9481     case 0x03: /* FMULX */
9482         gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9483         break;
9484     case 0x04: /* FCMEQ (reg) */
9485         gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9486         break;
9487     case 0x07: /* FRECPS */
9488         gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9489         break;
9490     case 0x0f: /* FRSQRTS */
9491         gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9492         break;
9493     case 0x14: /* FCMGE (reg) */
9494         gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9495         break;
9496     case 0x15: /* FACGE */
9497         gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9498         break;
9499     case 0x1a: /* FABD */
9500         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9501         tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9502         break;
9503     case 0x1c: /* FCMGT (reg) */
9504         gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9505         break;
9506     case 0x1d: /* FACGT */
9507         gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9508         break;
9509     default:
9510         g_assert_not_reached();
9511     }
9512 
9513     write_fp_sreg(s, rd, tcg_res);
9514 }
9515 
9516 /* AdvSIMD scalar three same extra
9517  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9518  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9519  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9520  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9521  */
9522 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9523                                                    uint32_t insn)
9524 {
9525     int rd = extract32(insn, 0, 5);
9526     int rn = extract32(insn, 5, 5);
9527     int opcode = extract32(insn, 11, 4);
9528     int rm = extract32(insn, 16, 5);
9529     int size = extract32(insn, 22, 2);
9530     bool u = extract32(insn, 29, 1);
9531     TCGv_i32 ele1, ele2, ele3;
9532     TCGv_i64 res;
9533     bool feature;
9534 
9535     switch (u * 16 + opcode) {
9536     case 0x10: /* SQRDMLAH (vector) */
9537     case 0x11: /* SQRDMLSH (vector) */
9538         if (size != 1 && size != 2) {
9539             unallocated_encoding(s);
9540             return;
9541         }
9542         feature = dc_isar_feature(aa64_rdm, s);
9543         break;
9544     default:
9545         unallocated_encoding(s);
9546         return;
9547     }
9548     if (!feature) {
9549         unallocated_encoding(s);
9550         return;
9551     }
9552     if (!fp_access_check(s)) {
9553         return;
9554     }
9555 
9556     /* Do a single operation on the lowest element in the vector.
9557      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9558      * with no side effects for all these operations.
9559      * OPTME: special-purpose helpers would avoid doing some
9560      * unnecessary work in the helper for the 16 bit cases.
9561      */
9562     ele1 = tcg_temp_new_i32();
9563     ele2 = tcg_temp_new_i32();
9564     ele3 = tcg_temp_new_i32();
9565 
9566     read_vec_element_i32(s, ele1, rn, 0, size);
9567     read_vec_element_i32(s, ele2, rm, 0, size);
9568     read_vec_element_i32(s, ele3, rd, 0, size);
9569 
9570     switch (opcode) {
9571     case 0x0: /* SQRDMLAH */
9572         if (size == 1) {
9573             gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9574         } else {
9575             gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9576         }
9577         break;
9578     case 0x1: /* SQRDMLSH */
9579         if (size == 1) {
9580             gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9581         } else {
9582             gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9583         }
9584         break;
9585     default:
9586         g_assert_not_reached();
9587     }
9588 
9589     res = tcg_temp_new_i64();
9590     tcg_gen_extu_i32_i64(res, ele3);
9591     write_fp_dreg(s, rd, res);
9592 }
9593 
9594 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9595                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9596                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9597 {
9598     /* Handle 64->64 opcodes which are shared between the scalar and
9599      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9600      * is valid in either group and also the double-precision fp ops.
9601      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9602      * requires them.
9603      */
9604     TCGCond cond;
9605 
9606     switch (opcode) {
9607     case 0x4: /* CLS, CLZ */
9608         if (u) {
9609             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9610         } else {
9611             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9612         }
9613         break;
9614     case 0x5: /* NOT */
9615         /* This opcode is shared with CNT and RBIT but we have earlier
9616          * enforced that size == 3 if and only if this is the NOT insn.
9617          */
9618         tcg_gen_not_i64(tcg_rd, tcg_rn);
9619         break;
9620     case 0x7: /* SQABS, SQNEG */
9621         if (u) {
9622             gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9623         } else {
9624             gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9625         }
9626         break;
9627     case 0xa: /* CMLT */
9628         /* 64 bit integer comparison against zero, result is
9629          * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9630          * subtracting 1.
9631          */
9632         cond = TCG_COND_LT;
9633     do_cmop:
9634         tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9635         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9636         break;
9637     case 0x8: /* CMGT, CMGE */
9638         cond = u ? TCG_COND_GE : TCG_COND_GT;
9639         goto do_cmop;
9640     case 0x9: /* CMEQ, CMLE */
9641         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9642         goto do_cmop;
9643     case 0xb: /* ABS, NEG */
9644         if (u) {
9645             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9646         } else {
9647             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9648         }
9649         break;
9650     case 0x2f: /* FABS */
9651         gen_helper_vfp_absd(tcg_rd, tcg_rn);
9652         break;
9653     case 0x6f: /* FNEG */
9654         gen_helper_vfp_negd(tcg_rd, tcg_rn);
9655         break;
9656     case 0x7f: /* FSQRT */
9657         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9658         break;
9659     case 0x1a: /* FCVTNS */
9660     case 0x1b: /* FCVTMS */
9661     case 0x1c: /* FCVTAS */
9662     case 0x3a: /* FCVTPS */
9663     case 0x3b: /* FCVTZS */
9664         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9665         break;
9666     case 0x5a: /* FCVTNU */
9667     case 0x5b: /* FCVTMU */
9668     case 0x5c: /* FCVTAU */
9669     case 0x7a: /* FCVTPU */
9670     case 0x7b: /* FCVTZU */
9671         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9672         break;
9673     case 0x18: /* FRINTN */
9674     case 0x19: /* FRINTM */
9675     case 0x38: /* FRINTP */
9676     case 0x39: /* FRINTZ */
9677     case 0x58: /* FRINTA */
9678     case 0x79: /* FRINTI */
9679         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9680         break;
9681     case 0x59: /* FRINTX */
9682         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9683         break;
9684     case 0x1e: /* FRINT32Z */
9685     case 0x5e: /* FRINT32X */
9686         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9687         break;
9688     case 0x1f: /* FRINT64Z */
9689     case 0x5f: /* FRINT64X */
9690         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9691         break;
9692     default:
9693         g_assert_not_reached();
9694     }
9695 }
9696 
9697 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9698                                    bool is_scalar, bool is_u, bool is_q,
9699                                    int size, int rn, int rd)
9700 {
9701     bool is_double = (size == MO_64);
9702     TCGv_ptr fpst;
9703 
9704     if (!fp_access_check(s)) {
9705         return;
9706     }
9707 
9708     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9709 
9710     if (is_double) {
9711         TCGv_i64 tcg_op = tcg_temp_new_i64();
9712         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9713         TCGv_i64 tcg_res = tcg_temp_new_i64();
9714         NeonGenTwoDoubleOpFn *genfn;
9715         bool swap = false;
9716         int pass;
9717 
9718         switch (opcode) {
9719         case 0x2e: /* FCMLT (zero) */
9720             swap = true;
9721             /* fallthrough */
9722         case 0x2c: /* FCMGT (zero) */
9723             genfn = gen_helper_neon_cgt_f64;
9724             break;
9725         case 0x2d: /* FCMEQ (zero) */
9726             genfn = gen_helper_neon_ceq_f64;
9727             break;
9728         case 0x6d: /* FCMLE (zero) */
9729             swap = true;
9730             /* fall through */
9731         case 0x6c: /* FCMGE (zero) */
9732             genfn = gen_helper_neon_cge_f64;
9733             break;
9734         default:
9735             g_assert_not_reached();
9736         }
9737 
9738         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9739             read_vec_element(s, tcg_op, rn, pass, MO_64);
9740             if (swap) {
9741                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9742             } else {
9743                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9744             }
9745             write_vec_element(s, tcg_res, rd, pass, MO_64);
9746         }
9747 
9748         clear_vec_high(s, !is_scalar, rd);
9749     } else {
9750         TCGv_i32 tcg_op = tcg_temp_new_i32();
9751         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9752         TCGv_i32 tcg_res = tcg_temp_new_i32();
9753         NeonGenTwoSingleOpFn *genfn;
9754         bool swap = false;
9755         int pass, maxpasses;
9756 
9757         if (size == MO_16) {
9758             switch (opcode) {
9759             case 0x2e: /* FCMLT (zero) */
9760                 swap = true;
9761                 /* fall through */
9762             case 0x2c: /* FCMGT (zero) */
9763                 genfn = gen_helper_advsimd_cgt_f16;
9764                 break;
9765             case 0x2d: /* FCMEQ (zero) */
9766                 genfn = gen_helper_advsimd_ceq_f16;
9767                 break;
9768             case 0x6d: /* FCMLE (zero) */
9769                 swap = true;
9770                 /* fall through */
9771             case 0x6c: /* FCMGE (zero) */
9772                 genfn = gen_helper_advsimd_cge_f16;
9773                 break;
9774             default:
9775                 g_assert_not_reached();
9776             }
9777         } else {
9778             switch (opcode) {
9779             case 0x2e: /* FCMLT (zero) */
9780                 swap = true;
9781                 /* fall through */
9782             case 0x2c: /* FCMGT (zero) */
9783                 genfn = gen_helper_neon_cgt_f32;
9784                 break;
9785             case 0x2d: /* FCMEQ (zero) */
9786                 genfn = gen_helper_neon_ceq_f32;
9787                 break;
9788             case 0x6d: /* FCMLE (zero) */
9789                 swap = true;
9790                 /* fall through */
9791             case 0x6c: /* FCMGE (zero) */
9792                 genfn = gen_helper_neon_cge_f32;
9793                 break;
9794             default:
9795                 g_assert_not_reached();
9796             }
9797         }
9798 
9799         if (is_scalar) {
9800             maxpasses = 1;
9801         } else {
9802             int vector_size = 8 << is_q;
9803             maxpasses = vector_size >> size;
9804         }
9805 
9806         for (pass = 0; pass < maxpasses; pass++) {
9807             read_vec_element_i32(s, tcg_op, rn, pass, size);
9808             if (swap) {
9809                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9810             } else {
9811                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9812             }
9813             if (is_scalar) {
9814                 write_fp_sreg(s, rd, tcg_res);
9815             } else {
9816                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9817             }
9818         }
9819 
9820         if (!is_scalar) {
9821             clear_vec_high(s, is_q, rd);
9822         }
9823     }
9824 }
9825 
9826 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9827                                     bool is_scalar, bool is_u, bool is_q,
9828                                     int size, int rn, int rd)
9829 {
9830     bool is_double = (size == 3);
9831     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9832 
9833     if (is_double) {
9834         TCGv_i64 tcg_op = tcg_temp_new_i64();
9835         TCGv_i64 tcg_res = tcg_temp_new_i64();
9836         int pass;
9837 
9838         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9839             read_vec_element(s, tcg_op, rn, pass, MO_64);
9840             switch (opcode) {
9841             case 0x3d: /* FRECPE */
9842                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9843                 break;
9844             case 0x3f: /* FRECPX */
9845                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9846                 break;
9847             case 0x7d: /* FRSQRTE */
9848                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9849                 break;
9850             default:
9851                 g_assert_not_reached();
9852             }
9853             write_vec_element(s, tcg_res, rd, pass, MO_64);
9854         }
9855         clear_vec_high(s, !is_scalar, rd);
9856     } else {
9857         TCGv_i32 tcg_op = tcg_temp_new_i32();
9858         TCGv_i32 tcg_res = tcg_temp_new_i32();
9859         int pass, maxpasses;
9860 
9861         if (is_scalar) {
9862             maxpasses = 1;
9863         } else {
9864             maxpasses = is_q ? 4 : 2;
9865         }
9866 
9867         for (pass = 0; pass < maxpasses; pass++) {
9868             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9869 
9870             switch (opcode) {
9871             case 0x3c: /* URECPE */
9872                 gen_helper_recpe_u32(tcg_res, tcg_op);
9873                 break;
9874             case 0x3d: /* FRECPE */
9875                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9876                 break;
9877             case 0x3f: /* FRECPX */
9878                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9879                 break;
9880             case 0x7d: /* FRSQRTE */
9881                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9882                 break;
9883             default:
9884                 g_assert_not_reached();
9885             }
9886 
9887             if (is_scalar) {
9888                 write_fp_sreg(s, rd, tcg_res);
9889             } else {
9890                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9891             }
9892         }
9893         if (!is_scalar) {
9894             clear_vec_high(s, is_q, rd);
9895         }
9896     }
9897 }
9898 
9899 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9900                                 int opcode, bool u, bool is_q,
9901                                 int size, int rn, int rd)
9902 {
9903     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9904      * in the source becomes a size element in the destination).
9905      */
9906     int pass;
9907     TCGv_i32 tcg_res[2];
9908     int destelt = is_q ? 2 : 0;
9909     int passes = scalar ? 1 : 2;
9910 
9911     if (scalar) {
9912         tcg_res[1] = tcg_constant_i32(0);
9913     }
9914 
9915     for (pass = 0; pass < passes; pass++) {
9916         TCGv_i64 tcg_op = tcg_temp_new_i64();
9917         NeonGenNarrowFn *genfn = NULL;
9918         NeonGenNarrowEnvFn *genenvfn = NULL;
9919 
9920         if (scalar) {
9921             read_vec_element(s, tcg_op, rn, pass, size + 1);
9922         } else {
9923             read_vec_element(s, tcg_op, rn, pass, MO_64);
9924         }
9925         tcg_res[pass] = tcg_temp_new_i32();
9926 
9927         switch (opcode) {
9928         case 0x12: /* XTN, SQXTUN */
9929         {
9930             static NeonGenNarrowFn * const xtnfns[3] = {
9931                 gen_helper_neon_narrow_u8,
9932                 gen_helper_neon_narrow_u16,
9933                 tcg_gen_extrl_i64_i32,
9934             };
9935             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9936                 gen_helper_neon_unarrow_sat8,
9937                 gen_helper_neon_unarrow_sat16,
9938                 gen_helper_neon_unarrow_sat32,
9939             };
9940             if (u) {
9941                 genenvfn = sqxtunfns[size];
9942             } else {
9943                 genfn = xtnfns[size];
9944             }
9945             break;
9946         }
9947         case 0x14: /* SQXTN, UQXTN */
9948         {
9949             static NeonGenNarrowEnvFn * const fns[3][2] = {
9950                 { gen_helper_neon_narrow_sat_s8,
9951                   gen_helper_neon_narrow_sat_u8 },
9952                 { gen_helper_neon_narrow_sat_s16,
9953                   gen_helper_neon_narrow_sat_u16 },
9954                 { gen_helper_neon_narrow_sat_s32,
9955                   gen_helper_neon_narrow_sat_u32 },
9956             };
9957             genenvfn = fns[size][u];
9958             break;
9959         }
9960         case 0x16: /* FCVTN, FCVTN2 */
9961             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9962             if (size == 2) {
9963                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
9964             } else {
9965                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9966                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9967                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9968                 TCGv_i32 ahp = get_ahp_flag();
9969 
9970                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9971                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9972                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9973                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9974             }
9975             break;
9976         case 0x36: /* BFCVTN, BFCVTN2 */
9977             {
9978                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9979                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
9980             }
9981             break;
9982         case 0x56:  /* FCVTXN, FCVTXN2 */
9983             /* 64 bit to 32 bit float conversion
9984              * with von Neumann rounding (round to odd)
9985              */
9986             assert(size == 2);
9987             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
9988             break;
9989         default:
9990             g_assert_not_reached();
9991         }
9992 
9993         if (genfn) {
9994             genfn(tcg_res[pass], tcg_op);
9995         } else if (genenvfn) {
9996             genenvfn(tcg_res[pass], cpu_env, tcg_op);
9997         }
9998     }
9999 
10000     for (pass = 0; pass < 2; pass++) {
10001         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
10002     }
10003     clear_vec_high(s, is_q, rd);
10004 }
10005 
10006 /* Remaining saturating accumulating ops */
10007 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
10008                                 bool is_q, int size, int rn, int rd)
10009 {
10010     bool is_double = (size == 3);
10011 
10012     if (is_double) {
10013         TCGv_i64 tcg_rn = tcg_temp_new_i64();
10014         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10015         int pass;
10016 
10017         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10018             read_vec_element(s, tcg_rn, rn, pass, MO_64);
10019             read_vec_element(s, tcg_rd, rd, pass, MO_64);
10020 
10021             if (is_u) { /* USQADD */
10022                 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10023             } else { /* SUQADD */
10024                 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10025             }
10026             write_vec_element(s, tcg_rd, rd, pass, MO_64);
10027         }
10028         clear_vec_high(s, !is_scalar, rd);
10029     } else {
10030         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10031         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10032         int pass, maxpasses;
10033 
10034         if (is_scalar) {
10035             maxpasses = 1;
10036         } else {
10037             maxpasses = is_q ? 4 : 2;
10038         }
10039 
10040         for (pass = 0; pass < maxpasses; pass++) {
10041             if (is_scalar) {
10042                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
10043                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
10044             } else {
10045                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
10046                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10047             }
10048 
10049             if (is_u) { /* USQADD */
10050                 switch (size) {
10051                 case 0:
10052                     gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10053                     break;
10054                 case 1:
10055                     gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10056                     break;
10057                 case 2:
10058                     gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10059                     break;
10060                 default:
10061                     g_assert_not_reached();
10062                 }
10063             } else { /* SUQADD */
10064                 switch (size) {
10065                 case 0:
10066                     gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10067                     break;
10068                 case 1:
10069                     gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10070                     break;
10071                 case 2:
10072                     gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10073                     break;
10074                 default:
10075                     g_assert_not_reached();
10076                 }
10077             }
10078 
10079             if (is_scalar) {
10080                 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
10081             }
10082             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10083         }
10084         clear_vec_high(s, is_q, rd);
10085     }
10086 }
10087 
10088 /* AdvSIMD scalar two reg misc
10089  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
10090  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10091  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
10092  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10093  */
10094 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
10095 {
10096     int rd = extract32(insn, 0, 5);
10097     int rn = extract32(insn, 5, 5);
10098     int opcode = extract32(insn, 12, 5);
10099     int size = extract32(insn, 22, 2);
10100     bool u = extract32(insn, 29, 1);
10101     bool is_fcvt = false;
10102     int rmode;
10103     TCGv_i32 tcg_rmode;
10104     TCGv_ptr tcg_fpstatus;
10105 
10106     switch (opcode) {
10107     case 0x3: /* USQADD / SUQADD*/
10108         if (!fp_access_check(s)) {
10109             return;
10110         }
10111         handle_2misc_satacc(s, true, u, false, size, rn, rd);
10112         return;
10113     case 0x7: /* SQABS / SQNEG */
10114         break;
10115     case 0xa: /* CMLT */
10116         if (u) {
10117             unallocated_encoding(s);
10118             return;
10119         }
10120         /* fall through */
10121     case 0x8: /* CMGT, CMGE */
10122     case 0x9: /* CMEQ, CMLE */
10123     case 0xb: /* ABS, NEG */
10124         if (size != 3) {
10125             unallocated_encoding(s);
10126             return;
10127         }
10128         break;
10129     case 0x12: /* SQXTUN */
10130         if (!u) {
10131             unallocated_encoding(s);
10132             return;
10133         }
10134         /* fall through */
10135     case 0x14: /* SQXTN, UQXTN */
10136         if (size == 3) {
10137             unallocated_encoding(s);
10138             return;
10139         }
10140         if (!fp_access_check(s)) {
10141             return;
10142         }
10143         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10144         return;
10145     case 0xc ... 0xf:
10146     case 0x16 ... 0x1d:
10147     case 0x1f:
10148         /* Floating point: U, size[1] and opcode indicate operation;
10149          * size[0] indicates single or double precision.
10150          */
10151         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10152         size = extract32(size, 0, 1) ? 3 : 2;
10153         switch (opcode) {
10154         case 0x2c: /* FCMGT (zero) */
10155         case 0x2d: /* FCMEQ (zero) */
10156         case 0x2e: /* FCMLT (zero) */
10157         case 0x6c: /* FCMGE (zero) */
10158         case 0x6d: /* FCMLE (zero) */
10159             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10160             return;
10161         case 0x1d: /* SCVTF */
10162         case 0x5d: /* UCVTF */
10163         {
10164             bool is_signed = (opcode == 0x1d);
10165             if (!fp_access_check(s)) {
10166                 return;
10167             }
10168             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10169             return;
10170         }
10171         case 0x3d: /* FRECPE */
10172         case 0x3f: /* FRECPX */
10173         case 0x7d: /* FRSQRTE */
10174             if (!fp_access_check(s)) {
10175                 return;
10176             }
10177             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10178             return;
10179         case 0x1a: /* FCVTNS */
10180         case 0x1b: /* FCVTMS */
10181         case 0x3a: /* FCVTPS */
10182         case 0x3b: /* FCVTZS */
10183         case 0x5a: /* FCVTNU */
10184         case 0x5b: /* FCVTMU */
10185         case 0x7a: /* FCVTPU */
10186         case 0x7b: /* FCVTZU */
10187             is_fcvt = true;
10188             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10189             break;
10190         case 0x1c: /* FCVTAS */
10191         case 0x5c: /* FCVTAU */
10192             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10193             is_fcvt = true;
10194             rmode = FPROUNDING_TIEAWAY;
10195             break;
10196         case 0x56: /* FCVTXN, FCVTXN2 */
10197             if (size == 2) {
10198                 unallocated_encoding(s);
10199                 return;
10200             }
10201             if (!fp_access_check(s)) {
10202                 return;
10203             }
10204             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10205             return;
10206         default:
10207             unallocated_encoding(s);
10208             return;
10209         }
10210         break;
10211     default:
10212         unallocated_encoding(s);
10213         return;
10214     }
10215 
10216     if (!fp_access_check(s)) {
10217         return;
10218     }
10219 
10220     if (is_fcvt) {
10221         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
10222         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
10223     } else {
10224         tcg_fpstatus = NULL;
10225         tcg_rmode = NULL;
10226     }
10227 
10228     if (size == 3) {
10229         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10230         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10231 
10232         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10233         write_fp_dreg(s, rd, tcg_rd);
10234     } else {
10235         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10236         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10237 
10238         read_vec_element_i32(s, tcg_rn, rn, 0, size);
10239 
10240         switch (opcode) {
10241         case 0x7: /* SQABS, SQNEG */
10242         {
10243             NeonGenOneOpEnvFn *genfn;
10244             static NeonGenOneOpEnvFn * const fns[3][2] = {
10245                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10246                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10247                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10248             };
10249             genfn = fns[size][u];
10250             genfn(tcg_rd, cpu_env, tcg_rn);
10251             break;
10252         }
10253         case 0x1a: /* FCVTNS */
10254         case 0x1b: /* FCVTMS */
10255         case 0x1c: /* FCVTAS */
10256         case 0x3a: /* FCVTPS */
10257         case 0x3b: /* FCVTZS */
10258             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10259                                  tcg_fpstatus);
10260             break;
10261         case 0x5a: /* FCVTNU */
10262         case 0x5b: /* FCVTMU */
10263         case 0x5c: /* FCVTAU */
10264         case 0x7a: /* FCVTPU */
10265         case 0x7b: /* FCVTZU */
10266             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10267                                  tcg_fpstatus);
10268             break;
10269         default:
10270             g_assert_not_reached();
10271         }
10272 
10273         write_fp_sreg(s, rd, tcg_rd);
10274     }
10275 
10276     if (is_fcvt) {
10277         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
10278     }
10279 }
10280 
10281 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10282 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10283                                  int immh, int immb, int opcode, int rn, int rd)
10284 {
10285     int size = 32 - clz32(immh) - 1;
10286     int immhb = immh << 3 | immb;
10287     int shift = 2 * (8 << size) - immhb;
10288     GVecGen2iFn *gvec_fn;
10289 
10290     if (extract32(immh, 3, 1) && !is_q) {
10291         unallocated_encoding(s);
10292         return;
10293     }
10294     tcg_debug_assert(size <= 3);
10295 
10296     if (!fp_access_check(s)) {
10297         return;
10298     }
10299 
10300     switch (opcode) {
10301     case 0x02: /* SSRA / USRA (accumulate) */
10302         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10303         break;
10304 
10305     case 0x08: /* SRI */
10306         gvec_fn = gen_gvec_sri;
10307         break;
10308 
10309     case 0x00: /* SSHR / USHR */
10310         if (is_u) {
10311             if (shift == 8 << size) {
10312                 /* Shift count the same size as element size produces zero.  */
10313                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10314                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
10315                 return;
10316             }
10317             gvec_fn = tcg_gen_gvec_shri;
10318         } else {
10319             /* Shift count the same size as element size produces all sign.  */
10320             if (shift == 8 << size) {
10321                 shift -= 1;
10322             }
10323             gvec_fn = tcg_gen_gvec_sari;
10324         }
10325         break;
10326 
10327     case 0x04: /* SRSHR / URSHR (rounding) */
10328         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10329         break;
10330 
10331     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10332         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10333         break;
10334 
10335     default:
10336         g_assert_not_reached();
10337     }
10338 
10339     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10340 }
10341 
10342 /* SHL/SLI - Vector shift left */
10343 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10344                                  int immh, int immb, int opcode, int rn, int rd)
10345 {
10346     int size = 32 - clz32(immh) - 1;
10347     int immhb = immh << 3 | immb;
10348     int shift = immhb - (8 << size);
10349 
10350     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10351     assert(size >= 0 && size <= 3);
10352 
10353     if (extract32(immh, 3, 1) && !is_q) {
10354         unallocated_encoding(s);
10355         return;
10356     }
10357 
10358     if (!fp_access_check(s)) {
10359         return;
10360     }
10361 
10362     if (insert) {
10363         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10364     } else {
10365         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10366     }
10367 }
10368 
10369 /* USHLL/SHLL - Vector shift left with widening */
10370 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10371                                  int immh, int immb, int opcode, int rn, int rd)
10372 {
10373     int size = 32 - clz32(immh) - 1;
10374     int immhb = immh << 3 | immb;
10375     int shift = immhb - (8 << size);
10376     int dsize = 64;
10377     int esize = 8 << size;
10378     int elements = dsize/esize;
10379     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10380     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10381     int i;
10382 
10383     if (size >= 3) {
10384         unallocated_encoding(s);
10385         return;
10386     }
10387 
10388     if (!fp_access_check(s)) {
10389         return;
10390     }
10391 
10392     /* For the LL variants the store is larger than the load,
10393      * so if rd == rn we would overwrite parts of our input.
10394      * So load everything right now and use shifts in the main loop.
10395      */
10396     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10397 
10398     for (i = 0; i < elements; i++) {
10399         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10400         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10401         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10402         write_vec_element(s, tcg_rd, rd, i, size + 1);
10403     }
10404 }
10405 
10406 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10407 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10408                                  int immh, int immb, int opcode, int rn, int rd)
10409 {
10410     int immhb = immh << 3 | immb;
10411     int size = 32 - clz32(immh) - 1;
10412     int dsize = 64;
10413     int esize = 8 << size;
10414     int elements = dsize/esize;
10415     int shift = (2 * esize) - immhb;
10416     bool round = extract32(opcode, 0, 1);
10417     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10418     TCGv_i64 tcg_round;
10419     int i;
10420 
10421     if (extract32(immh, 3, 1)) {
10422         unallocated_encoding(s);
10423         return;
10424     }
10425 
10426     if (!fp_access_check(s)) {
10427         return;
10428     }
10429 
10430     tcg_rn = tcg_temp_new_i64();
10431     tcg_rd = tcg_temp_new_i64();
10432     tcg_final = tcg_temp_new_i64();
10433     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10434 
10435     if (round) {
10436         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10437     } else {
10438         tcg_round = NULL;
10439     }
10440 
10441     for (i = 0; i < elements; i++) {
10442         read_vec_element(s, tcg_rn, rn, i, size+1);
10443         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10444                                 false, true, size+1, shift);
10445 
10446         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10447     }
10448 
10449     if (!is_q) {
10450         write_vec_element(s, tcg_final, rd, 0, MO_64);
10451     } else {
10452         write_vec_element(s, tcg_final, rd, 1, MO_64);
10453     }
10454 
10455     clear_vec_high(s, is_q, rd);
10456 }
10457 
10458 
10459 /* AdvSIMD shift by immediate
10460  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10461  * +---+---+---+-------------+------+------+--------+---+------+------+
10462  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10463  * +---+---+---+-------------+------+------+--------+---+------+------+
10464  */
10465 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10466 {
10467     int rd = extract32(insn, 0, 5);
10468     int rn = extract32(insn, 5, 5);
10469     int opcode = extract32(insn, 11, 5);
10470     int immb = extract32(insn, 16, 3);
10471     int immh = extract32(insn, 19, 4);
10472     bool is_u = extract32(insn, 29, 1);
10473     bool is_q = extract32(insn, 30, 1);
10474 
10475     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10476     assert(immh != 0);
10477 
10478     switch (opcode) {
10479     case 0x08: /* SRI */
10480         if (!is_u) {
10481             unallocated_encoding(s);
10482             return;
10483         }
10484         /* fall through */
10485     case 0x00: /* SSHR / USHR */
10486     case 0x02: /* SSRA / USRA (accumulate) */
10487     case 0x04: /* SRSHR / URSHR (rounding) */
10488     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10489         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10490         break;
10491     case 0x0a: /* SHL / SLI */
10492         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10493         break;
10494     case 0x10: /* SHRN */
10495     case 0x11: /* RSHRN / SQRSHRUN */
10496         if (is_u) {
10497             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10498                                    opcode, rn, rd);
10499         } else {
10500             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10501         }
10502         break;
10503     case 0x12: /* SQSHRN / UQSHRN */
10504     case 0x13: /* SQRSHRN / UQRSHRN */
10505         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10506                                opcode, rn, rd);
10507         break;
10508     case 0x14: /* SSHLL / USHLL */
10509         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10510         break;
10511     case 0x1c: /* SCVTF / UCVTF */
10512         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10513                                      opcode, rn, rd);
10514         break;
10515     case 0xc: /* SQSHLU */
10516         if (!is_u) {
10517             unallocated_encoding(s);
10518             return;
10519         }
10520         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10521         break;
10522     case 0xe: /* SQSHL, UQSHL */
10523         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10524         break;
10525     case 0x1f: /* FCVTZS/ FCVTZU */
10526         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10527         return;
10528     default:
10529         unallocated_encoding(s);
10530         return;
10531     }
10532 }
10533 
10534 /* Generate code to do a "long" addition or subtraction, ie one done in
10535  * TCGv_i64 on vector lanes twice the width specified by size.
10536  */
10537 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10538                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10539 {
10540     static NeonGenTwo64OpFn * const fns[3][2] = {
10541         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10542         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10543         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10544     };
10545     NeonGenTwo64OpFn *genfn;
10546     assert(size < 3);
10547 
10548     genfn = fns[size][is_sub];
10549     genfn(tcg_res, tcg_op1, tcg_op2);
10550 }
10551 
10552 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10553                                 int opcode, int rd, int rn, int rm)
10554 {
10555     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10556     TCGv_i64 tcg_res[2];
10557     int pass, accop;
10558 
10559     tcg_res[0] = tcg_temp_new_i64();
10560     tcg_res[1] = tcg_temp_new_i64();
10561 
10562     /* Does this op do an adding accumulate, a subtracting accumulate,
10563      * or no accumulate at all?
10564      */
10565     switch (opcode) {
10566     case 5:
10567     case 8:
10568     case 9:
10569         accop = 1;
10570         break;
10571     case 10:
10572     case 11:
10573         accop = -1;
10574         break;
10575     default:
10576         accop = 0;
10577         break;
10578     }
10579 
10580     if (accop != 0) {
10581         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10582         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10583     }
10584 
10585     /* size == 2 means two 32x32->64 operations; this is worth special
10586      * casing because we can generally handle it inline.
10587      */
10588     if (size == 2) {
10589         for (pass = 0; pass < 2; pass++) {
10590             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10591             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10592             TCGv_i64 tcg_passres;
10593             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10594 
10595             int elt = pass + is_q * 2;
10596 
10597             read_vec_element(s, tcg_op1, rn, elt, memop);
10598             read_vec_element(s, tcg_op2, rm, elt, memop);
10599 
10600             if (accop == 0) {
10601                 tcg_passres = tcg_res[pass];
10602             } else {
10603                 tcg_passres = tcg_temp_new_i64();
10604             }
10605 
10606             switch (opcode) {
10607             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10608                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10609                 break;
10610             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10611                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10612                 break;
10613             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10614             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10615             {
10616                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10617                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10618 
10619                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10620                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10621                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10622                                     tcg_passres,
10623                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10624                 break;
10625             }
10626             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10627             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10628             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10629                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10630                 break;
10631             case 9: /* SQDMLAL, SQDMLAL2 */
10632             case 11: /* SQDMLSL, SQDMLSL2 */
10633             case 13: /* SQDMULL, SQDMULL2 */
10634                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10635                 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10636                                                   tcg_passres, tcg_passres);
10637                 break;
10638             default:
10639                 g_assert_not_reached();
10640             }
10641 
10642             if (opcode == 9 || opcode == 11) {
10643                 /* saturating accumulate ops */
10644                 if (accop < 0) {
10645                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10646                 }
10647                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10648                                                   tcg_res[pass], tcg_passres);
10649             } else if (accop > 0) {
10650                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10651             } else if (accop < 0) {
10652                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10653             }
10654         }
10655     } else {
10656         /* size 0 or 1, generally helper functions */
10657         for (pass = 0; pass < 2; pass++) {
10658             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10659             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10660             TCGv_i64 tcg_passres;
10661             int elt = pass + is_q * 2;
10662 
10663             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10664             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10665 
10666             if (accop == 0) {
10667                 tcg_passres = tcg_res[pass];
10668             } else {
10669                 tcg_passres = tcg_temp_new_i64();
10670             }
10671 
10672             switch (opcode) {
10673             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10674             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10675             {
10676                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10677                 static NeonGenWidenFn * const widenfns[2][2] = {
10678                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10679                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10680                 };
10681                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10682 
10683                 widenfn(tcg_op2_64, tcg_op2);
10684                 widenfn(tcg_passres, tcg_op1);
10685                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10686                               tcg_passres, tcg_op2_64);
10687                 break;
10688             }
10689             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10690             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10691                 if (size == 0) {
10692                     if (is_u) {
10693                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10694                     } else {
10695                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10696                     }
10697                 } else {
10698                     if (is_u) {
10699                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10700                     } else {
10701                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10702                     }
10703                 }
10704                 break;
10705             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10706             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10707             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10708                 if (size == 0) {
10709                     if (is_u) {
10710                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10711                     } else {
10712                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10713                     }
10714                 } else {
10715                     if (is_u) {
10716                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10717                     } else {
10718                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10719                     }
10720                 }
10721                 break;
10722             case 9: /* SQDMLAL, SQDMLAL2 */
10723             case 11: /* SQDMLSL, SQDMLSL2 */
10724             case 13: /* SQDMULL, SQDMULL2 */
10725                 assert(size == 1);
10726                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10727                 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10728                                                   tcg_passres, tcg_passres);
10729                 break;
10730             default:
10731                 g_assert_not_reached();
10732             }
10733 
10734             if (accop != 0) {
10735                 if (opcode == 9 || opcode == 11) {
10736                     /* saturating accumulate ops */
10737                     if (accop < 0) {
10738                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10739                     }
10740                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10741                                                       tcg_res[pass],
10742                                                       tcg_passres);
10743                 } else {
10744                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10745                                   tcg_res[pass], tcg_passres);
10746                 }
10747             }
10748         }
10749     }
10750 
10751     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10752     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10753 }
10754 
10755 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10756                             int opcode, int rd, int rn, int rm)
10757 {
10758     TCGv_i64 tcg_res[2];
10759     int part = is_q ? 2 : 0;
10760     int pass;
10761 
10762     for (pass = 0; pass < 2; pass++) {
10763         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10764         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10765         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10766         static NeonGenWidenFn * const widenfns[3][2] = {
10767             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10768             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10769             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10770         };
10771         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10772 
10773         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10774         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10775         widenfn(tcg_op2_wide, tcg_op2);
10776         tcg_res[pass] = tcg_temp_new_i64();
10777         gen_neon_addl(size, (opcode == 3),
10778                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10779     }
10780 
10781     for (pass = 0; pass < 2; pass++) {
10782         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10783     }
10784 }
10785 
10786 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10787 {
10788     tcg_gen_addi_i64(in, in, 1U << 31);
10789     tcg_gen_extrh_i64_i32(res, in);
10790 }
10791 
10792 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10793                                  int opcode, int rd, int rn, int rm)
10794 {
10795     TCGv_i32 tcg_res[2];
10796     int part = is_q ? 2 : 0;
10797     int pass;
10798 
10799     for (pass = 0; pass < 2; pass++) {
10800         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10801         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10802         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10803         static NeonGenNarrowFn * const narrowfns[3][2] = {
10804             { gen_helper_neon_narrow_high_u8,
10805               gen_helper_neon_narrow_round_high_u8 },
10806             { gen_helper_neon_narrow_high_u16,
10807               gen_helper_neon_narrow_round_high_u16 },
10808             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10809         };
10810         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10811 
10812         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10813         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10814 
10815         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10816 
10817         tcg_res[pass] = tcg_temp_new_i32();
10818         gennarrow(tcg_res[pass], tcg_wideres);
10819     }
10820 
10821     for (pass = 0; pass < 2; pass++) {
10822         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10823     }
10824     clear_vec_high(s, is_q, rd);
10825 }
10826 
10827 /* AdvSIMD three different
10828  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10829  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10830  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10831  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10832  */
10833 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10834 {
10835     /* Instructions in this group fall into three basic classes
10836      * (in each case with the operation working on each element in
10837      * the input vectors):
10838      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10839      *     128 bit input)
10840      * (2) wide 64 x 128 -> 128
10841      * (3) narrowing 128 x 128 -> 64
10842      * Here we do initial decode, catch unallocated cases and
10843      * dispatch to separate functions for each class.
10844      */
10845     int is_q = extract32(insn, 30, 1);
10846     int is_u = extract32(insn, 29, 1);
10847     int size = extract32(insn, 22, 2);
10848     int opcode = extract32(insn, 12, 4);
10849     int rm = extract32(insn, 16, 5);
10850     int rn = extract32(insn, 5, 5);
10851     int rd = extract32(insn, 0, 5);
10852 
10853     switch (opcode) {
10854     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10855     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10856         /* 64 x 128 -> 128 */
10857         if (size == 3) {
10858             unallocated_encoding(s);
10859             return;
10860         }
10861         if (!fp_access_check(s)) {
10862             return;
10863         }
10864         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10865         break;
10866     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10867     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10868         /* 128 x 128 -> 64 */
10869         if (size == 3) {
10870             unallocated_encoding(s);
10871             return;
10872         }
10873         if (!fp_access_check(s)) {
10874             return;
10875         }
10876         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10877         break;
10878     case 14: /* PMULL, PMULL2 */
10879         if (is_u) {
10880             unallocated_encoding(s);
10881             return;
10882         }
10883         switch (size) {
10884         case 0: /* PMULL.P8 */
10885             if (!fp_access_check(s)) {
10886                 return;
10887             }
10888             /* The Q field specifies lo/hi half input for this insn.  */
10889             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10890                              gen_helper_neon_pmull_h);
10891             break;
10892 
10893         case 3: /* PMULL.P64 */
10894             if (!dc_isar_feature(aa64_pmull, s)) {
10895                 unallocated_encoding(s);
10896                 return;
10897             }
10898             if (!fp_access_check(s)) {
10899                 return;
10900             }
10901             /* The Q field specifies lo/hi half input for this insn.  */
10902             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10903                              gen_helper_gvec_pmull_q);
10904             break;
10905 
10906         default:
10907             unallocated_encoding(s);
10908             break;
10909         }
10910         return;
10911     case 9: /* SQDMLAL, SQDMLAL2 */
10912     case 11: /* SQDMLSL, SQDMLSL2 */
10913     case 13: /* SQDMULL, SQDMULL2 */
10914         if (is_u || size == 0) {
10915             unallocated_encoding(s);
10916             return;
10917         }
10918         /* fall through */
10919     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10920     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10921     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10922     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10923     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10924     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10925     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10926         /* 64 x 64 -> 128 */
10927         if (size == 3) {
10928             unallocated_encoding(s);
10929             return;
10930         }
10931         if (!fp_access_check(s)) {
10932             return;
10933         }
10934 
10935         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10936         break;
10937     default:
10938         /* opcode 15 not allocated */
10939         unallocated_encoding(s);
10940         break;
10941     }
10942 }
10943 
10944 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10945 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10946 {
10947     int rd = extract32(insn, 0, 5);
10948     int rn = extract32(insn, 5, 5);
10949     int rm = extract32(insn, 16, 5);
10950     int size = extract32(insn, 22, 2);
10951     bool is_u = extract32(insn, 29, 1);
10952     bool is_q = extract32(insn, 30, 1);
10953 
10954     if (!fp_access_check(s)) {
10955         return;
10956     }
10957 
10958     switch (size + 4 * is_u) {
10959     case 0: /* AND */
10960         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10961         return;
10962     case 1: /* BIC */
10963         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10964         return;
10965     case 2: /* ORR */
10966         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10967         return;
10968     case 3: /* ORN */
10969         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10970         return;
10971     case 4: /* EOR */
10972         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10973         return;
10974 
10975     case 5: /* BSL bitwise select */
10976         gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
10977         return;
10978     case 6: /* BIT, bitwise insert if true */
10979         gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
10980         return;
10981     case 7: /* BIF, bitwise insert if false */
10982         gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
10983         return;
10984 
10985     default:
10986         g_assert_not_reached();
10987     }
10988 }
10989 
10990 /* Pairwise op subgroup of C3.6.16.
10991  *
10992  * This is called directly or via the handle_3same_float for float pairwise
10993  * operations where the opcode and size are calculated differently.
10994  */
10995 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
10996                                    int size, int rn, int rm, int rd)
10997 {
10998     TCGv_ptr fpst;
10999     int pass;
11000 
11001     /* Floating point operations need fpst */
11002     if (opcode >= 0x58) {
11003         fpst = fpstatus_ptr(FPST_FPCR);
11004     } else {
11005         fpst = NULL;
11006     }
11007 
11008     if (!fp_access_check(s)) {
11009         return;
11010     }
11011 
11012     /* These operations work on the concatenated rm:rn, with each pair of
11013      * adjacent elements being operated on to produce an element in the result.
11014      */
11015     if (size == 3) {
11016         TCGv_i64 tcg_res[2];
11017 
11018         for (pass = 0; pass < 2; pass++) {
11019             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11020             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11021             int passreg = (pass == 0) ? rn : rm;
11022 
11023             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
11024             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
11025             tcg_res[pass] = tcg_temp_new_i64();
11026 
11027             switch (opcode) {
11028             case 0x17: /* ADDP */
11029                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11030                 break;
11031             case 0x58: /* FMAXNMP */
11032                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11033                 break;
11034             case 0x5a: /* FADDP */
11035                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11036                 break;
11037             case 0x5e: /* FMAXP */
11038                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11039                 break;
11040             case 0x78: /* FMINNMP */
11041                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11042                 break;
11043             case 0x7e: /* FMINP */
11044                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11045                 break;
11046             default:
11047                 g_assert_not_reached();
11048             }
11049         }
11050 
11051         for (pass = 0; pass < 2; pass++) {
11052             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11053         }
11054     } else {
11055         int maxpass = is_q ? 4 : 2;
11056         TCGv_i32 tcg_res[4];
11057 
11058         for (pass = 0; pass < maxpass; pass++) {
11059             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11060             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11061             NeonGenTwoOpFn *genfn = NULL;
11062             int passreg = pass < (maxpass / 2) ? rn : rm;
11063             int passelt = (is_q && (pass & 1)) ? 2 : 0;
11064 
11065             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
11066             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
11067             tcg_res[pass] = tcg_temp_new_i32();
11068 
11069             switch (opcode) {
11070             case 0x17: /* ADDP */
11071             {
11072                 static NeonGenTwoOpFn * const fns[3] = {
11073                     gen_helper_neon_padd_u8,
11074                     gen_helper_neon_padd_u16,
11075                     tcg_gen_add_i32,
11076                 };
11077                 genfn = fns[size];
11078                 break;
11079             }
11080             case 0x14: /* SMAXP, UMAXP */
11081             {
11082                 static NeonGenTwoOpFn * const fns[3][2] = {
11083                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
11084                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
11085                     { tcg_gen_smax_i32, tcg_gen_umax_i32 },
11086                 };
11087                 genfn = fns[size][u];
11088                 break;
11089             }
11090             case 0x15: /* SMINP, UMINP */
11091             {
11092                 static NeonGenTwoOpFn * const fns[3][2] = {
11093                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
11094                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
11095                     { tcg_gen_smin_i32, tcg_gen_umin_i32 },
11096                 };
11097                 genfn = fns[size][u];
11098                 break;
11099             }
11100             /* The FP operations are all on single floats (32 bit) */
11101             case 0x58: /* FMAXNMP */
11102                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11103                 break;
11104             case 0x5a: /* FADDP */
11105                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11106                 break;
11107             case 0x5e: /* FMAXP */
11108                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11109                 break;
11110             case 0x78: /* FMINNMP */
11111                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11112                 break;
11113             case 0x7e: /* FMINP */
11114                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11115                 break;
11116             default:
11117                 g_assert_not_reached();
11118             }
11119 
11120             /* FP ops called directly, otherwise call now */
11121             if (genfn) {
11122                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
11123             }
11124         }
11125 
11126         for (pass = 0; pass < maxpass; pass++) {
11127             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11128         }
11129         clear_vec_high(s, is_q, rd);
11130     }
11131 }
11132 
11133 /* Floating point op subgroup of C3.6.16. */
11134 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
11135 {
11136     /* For floating point ops, the U, size[1] and opcode bits
11137      * together indicate the operation. size[0] indicates single
11138      * or double.
11139      */
11140     int fpopcode = extract32(insn, 11, 5)
11141         | (extract32(insn, 23, 1) << 5)
11142         | (extract32(insn, 29, 1) << 6);
11143     int is_q = extract32(insn, 30, 1);
11144     int size = extract32(insn, 22, 1);
11145     int rm = extract32(insn, 16, 5);
11146     int rn = extract32(insn, 5, 5);
11147     int rd = extract32(insn, 0, 5);
11148 
11149     int datasize = is_q ? 128 : 64;
11150     int esize = 32 << size;
11151     int elements = datasize / esize;
11152 
11153     if (size == 1 && !is_q) {
11154         unallocated_encoding(s);
11155         return;
11156     }
11157 
11158     switch (fpopcode) {
11159     case 0x58: /* FMAXNMP */
11160     case 0x5a: /* FADDP */
11161     case 0x5e: /* FMAXP */
11162     case 0x78: /* FMINNMP */
11163     case 0x7e: /* FMINP */
11164         if (size && !is_q) {
11165             unallocated_encoding(s);
11166             return;
11167         }
11168         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11169                                rn, rm, rd);
11170         return;
11171     case 0x1b: /* FMULX */
11172     case 0x1f: /* FRECPS */
11173     case 0x3f: /* FRSQRTS */
11174     case 0x5d: /* FACGE */
11175     case 0x7d: /* FACGT */
11176     case 0x19: /* FMLA */
11177     case 0x39: /* FMLS */
11178     case 0x18: /* FMAXNM */
11179     case 0x1a: /* FADD */
11180     case 0x1c: /* FCMEQ */
11181     case 0x1e: /* FMAX */
11182     case 0x38: /* FMINNM */
11183     case 0x3a: /* FSUB */
11184     case 0x3e: /* FMIN */
11185     case 0x5b: /* FMUL */
11186     case 0x5c: /* FCMGE */
11187     case 0x5f: /* FDIV */
11188     case 0x7a: /* FABD */
11189     case 0x7c: /* FCMGT */
11190         if (!fp_access_check(s)) {
11191             return;
11192         }
11193         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11194         return;
11195 
11196     case 0x1d: /* FMLAL  */
11197     case 0x3d: /* FMLSL  */
11198     case 0x59: /* FMLAL2 */
11199     case 0x79: /* FMLSL2 */
11200         if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11201             unallocated_encoding(s);
11202             return;
11203         }
11204         if (fp_access_check(s)) {
11205             int is_s = extract32(insn, 23, 1);
11206             int is_2 = extract32(insn, 29, 1);
11207             int data = (is_2 << 1) | is_s;
11208             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11209                                vec_full_reg_offset(s, rn),
11210                                vec_full_reg_offset(s, rm), cpu_env,
11211                                is_q ? 16 : 8, vec_full_reg_size(s),
11212                                data, gen_helper_gvec_fmlal_a64);
11213         }
11214         return;
11215 
11216     default:
11217         unallocated_encoding(s);
11218         return;
11219     }
11220 }
11221 
11222 /* Integer op subgroup of C3.6.16. */
11223 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11224 {
11225     int is_q = extract32(insn, 30, 1);
11226     int u = extract32(insn, 29, 1);
11227     int size = extract32(insn, 22, 2);
11228     int opcode = extract32(insn, 11, 5);
11229     int rm = extract32(insn, 16, 5);
11230     int rn = extract32(insn, 5, 5);
11231     int rd = extract32(insn, 0, 5);
11232     int pass;
11233     TCGCond cond;
11234 
11235     switch (opcode) {
11236     case 0x13: /* MUL, PMUL */
11237         if (u && size != 0) {
11238             unallocated_encoding(s);
11239             return;
11240         }
11241         /* fall through */
11242     case 0x0: /* SHADD, UHADD */
11243     case 0x2: /* SRHADD, URHADD */
11244     case 0x4: /* SHSUB, UHSUB */
11245     case 0xc: /* SMAX, UMAX */
11246     case 0xd: /* SMIN, UMIN */
11247     case 0xe: /* SABD, UABD */
11248     case 0xf: /* SABA, UABA */
11249     case 0x12: /* MLA, MLS */
11250         if (size == 3) {
11251             unallocated_encoding(s);
11252             return;
11253         }
11254         break;
11255     case 0x16: /* SQDMULH, SQRDMULH */
11256         if (size == 0 || size == 3) {
11257             unallocated_encoding(s);
11258             return;
11259         }
11260         break;
11261     default:
11262         if (size == 3 && !is_q) {
11263             unallocated_encoding(s);
11264             return;
11265         }
11266         break;
11267     }
11268 
11269     if (!fp_access_check(s)) {
11270         return;
11271     }
11272 
11273     switch (opcode) {
11274     case 0x01: /* SQADD, UQADD */
11275         if (u) {
11276             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11277         } else {
11278             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11279         }
11280         return;
11281     case 0x05: /* SQSUB, UQSUB */
11282         if (u) {
11283             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11284         } else {
11285             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11286         }
11287         return;
11288     case 0x08: /* SSHL, USHL */
11289         if (u) {
11290             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11291         } else {
11292             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11293         }
11294         return;
11295     case 0x0c: /* SMAX, UMAX */
11296         if (u) {
11297             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11298         } else {
11299             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11300         }
11301         return;
11302     case 0x0d: /* SMIN, UMIN */
11303         if (u) {
11304             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11305         } else {
11306             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11307         }
11308         return;
11309     case 0xe: /* SABD, UABD */
11310         if (u) {
11311             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11312         } else {
11313             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11314         }
11315         return;
11316     case 0xf: /* SABA, UABA */
11317         if (u) {
11318             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11319         } else {
11320             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11321         }
11322         return;
11323     case 0x10: /* ADD, SUB */
11324         if (u) {
11325             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11326         } else {
11327             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11328         }
11329         return;
11330     case 0x13: /* MUL, PMUL */
11331         if (!u) { /* MUL */
11332             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11333         } else {  /* PMUL */
11334             gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11335         }
11336         return;
11337     case 0x12: /* MLA, MLS */
11338         if (u) {
11339             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11340         } else {
11341             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11342         }
11343         return;
11344     case 0x16: /* SQDMULH, SQRDMULH */
11345         {
11346             static gen_helper_gvec_3_ptr * const fns[2][2] = {
11347                 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11348                 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11349             };
11350             gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11351         }
11352         return;
11353     case 0x11:
11354         if (!u) { /* CMTST */
11355             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11356             return;
11357         }
11358         /* else CMEQ */
11359         cond = TCG_COND_EQ;
11360         goto do_gvec_cmp;
11361     case 0x06: /* CMGT, CMHI */
11362         cond = u ? TCG_COND_GTU : TCG_COND_GT;
11363         goto do_gvec_cmp;
11364     case 0x07: /* CMGE, CMHS */
11365         cond = u ? TCG_COND_GEU : TCG_COND_GE;
11366     do_gvec_cmp:
11367         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11368                          vec_full_reg_offset(s, rn),
11369                          vec_full_reg_offset(s, rm),
11370                          is_q ? 16 : 8, vec_full_reg_size(s));
11371         return;
11372     }
11373 
11374     if (size == 3) {
11375         assert(is_q);
11376         for (pass = 0; pass < 2; pass++) {
11377             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11378             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11379             TCGv_i64 tcg_res = tcg_temp_new_i64();
11380 
11381             read_vec_element(s, tcg_op1, rn, pass, MO_64);
11382             read_vec_element(s, tcg_op2, rm, pass, MO_64);
11383 
11384             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11385 
11386             write_vec_element(s, tcg_res, rd, pass, MO_64);
11387         }
11388     } else {
11389         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11390             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11391             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11392             TCGv_i32 tcg_res = tcg_temp_new_i32();
11393             NeonGenTwoOpFn *genfn = NULL;
11394             NeonGenTwoOpEnvFn *genenvfn = NULL;
11395 
11396             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11397             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11398 
11399             switch (opcode) {
11400             case 0x0: /* SHADD, UHADD */
11401             {
11402                 static NeonGenTwoOpFn * const fns[3][2] = {
11403                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11404                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11405                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11406                 };
11407                 genfn = fns[size][u];
11408                 break;
11409             }
11410             case 0x2: /* SRHADD, URHADD */
11411             {
11412                 static NeonGenTwoOpFn * const fns[3][2] = {
11413                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11414                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11415                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11416                 };
11417                 genfn = fns[size][u];
11418                 break;
11419             }
11420             case 0x4: /* SHSUB, UHSUB */
11421             {
11422                 static NeonGenTwoOpFn * const fns[3][2] = {
11423                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11424                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11425                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11426                 };
11427                 genfn = fns[size][u];
11428                 break;
11429             }
11430             case 0x9: /* SQSHL, UQSHL */
11431             {
11432                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11433                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11434                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11435                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11436                 };
11437                 genenvfn = fns[size][u];
11438                 break;
11439             }
11440             case 0xa: /* SRSHL, URSHL */
11441             {
11442                 static NeonGenTwoOpFn * const fns[3][2] = {
11443                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11444                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11445                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11446                 };
11447                 genfn = fns[size][u];
11448                 break;
11449             }
11450             case 0xb: /* SQRSHL, UQRSHL */
11451             {
11452                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11453                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11454                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11455                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11456                 };
11457                 genenvfn = fns[size][u];
11458                 break;
11459             }
11460             default:
11461                 g_assert_not_reached();
11462             }
11463 
11464             if (genenvfn) {
11465                 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11466             } else {
11467                 genfn(tcg_res, tcg_op1, tcg_op2);
11468             }
11469 
11470             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11471         }
11472     }
11473     clear_vec_high(s, is_q, rd);
11474 }
11475 
11476 /* AdvSIMD three same
11477  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
11478  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11479  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
11480  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11481  */
11482 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11483 {
11484     int opcode = extract32(insn, 11, 5);
11485 
11486     switch (opcode) {
11487     case 0x3: /* logic ops */
11488         disas_simd_3same_logic(s, insn);
11489         break;
11490     case 0x17: /* ADDP */
11491     case 0x14: /* SMAXP, UMAXP */
11492     case 0x15: /* SMINP, UMINP */
11493     {
11494         /* Pairwise operations */
11495         int is_q = extract32(insn, 30, 1);
11496         int u = extract32(insn, 29, 1);
11497         int size = extract32(insn, 22, 2);
11498         int rm = extract32(insn, 16, 5);
11499         int rn = extract32(insn, 5, 5);
11500         int rd = extract32(insn, 0, 5);
11501         if (opcode == 0x17) {
11502             if (u || (size == 3 && !is_q)) {
11503                 unallocated_encoding(s);
11504                 return;
11505             }
11506         } else {
11507             if (size == 3) {
11508                 unallocated_encoding(s);
11509                 return;
11510             }
11511         }
11512         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11513         break;
11514     }
11515     case 0x18 ... 0x31:
11516         /* floating point ops, sz[1] and U are part of opcode */
11517         disas_simd_3same_float(s, insn);
11518         break;
11519     default:
11520         disas_simd_3same_int(s, insn);
11521         break;
11522     }
11523 }
11524 
11525 /*
11526  * Advanced SIMD three same (ARMv8.2 FP16 variants)
11527  *
11528  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
11529  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11530  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
11531  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11532  *
11533  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11534  * (register), FACGE, FABD, FCMGT (register) and FACGT.
11535  *
11536  */
11537 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11538 {
11539     int opcode = extract32(insn, 11, 3);
11540     int u = extract32(insn, 29, 1);
11541     int a = extract32(insn, 23, 1);
11542     int is_q = extract32(insn, 30, 1);
11543     int rm = extract32(insn, 16, 5);
11544     int rn = extract32(insn, 5, 5);
11545     int rd = extract32(insn, 0, 5);
11546     /*
11547      * For these floating point ops, the U, a and opcode bits
11548      * together indicate the operation.
11549      */
11550     int fpopcode = opcode | (a << 3) | (u << 4);
11551     int datasize = is_q ? 128 : 64;
11552     int elements = datasize / 16;
11553     bool pairwise;
11554     TCGv_ptr fpst;
11555     int pass;
11556 
11557     switch (fpopcode) {
11558     case 0x0: /* FMAXNM */
11559     case 0x1: /* FMLA */
11560     case 0x2: /* FADD */
11561     case 0x3: /* FMULX */
11562     case 0x4: /* FCMEQ */
11563     case 0x6: /* FMAX */
11564     case 0x7: /* FRECPS */
11565     case 0x8: /* FMINNM */
11566     case 0x9: /* FMLS */
11567     case 0xa: /* FSUB */
11568     case 0xe: /* FMIN */
11569     case 0xf: /* FRSQRTS */
11570     case 0x13: /* FMUL */
11571     case 0x14: /* FCMGE */
11572     case 0x15: /* FACGE */
11573     case 0x17: /* FDIV */
11574     case 0x1a: /* FABD */
11575     case 0x1c: /* FCMGT */
11576     case 0x1d: /* FACGT */
11577         pairwise = false;
11578         break;
11579     case 0x10: /* FMAXNMP */
11580     case 0x12: /* FADDP */
11581     case 0x16: /* FMAXP */
11582     case 0x18: /* FMINNMP */
11583     case 0x1e: /* FMINP */
11584         pairwise = true;
11585         break;
11586     default:
11587         unallocated_encoding(s);
11588         return;
11589     }
11590 
11591     if (!dc_isar_feature(aa64_fp16, s)) {
11592         unallocated_encoding(s);
11593         return;
11594     }
11595 
11596     if (!fp_access_check(s)) {
11597         return;
11598     }
11599 
11600     fpst = fpstatus_ptr(FPST_FPCR_F16);
11601 
11602     if (pairwise) {
11603         int maxpass = is_q ? 8 : 4;
11604         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11605         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11606         TCGv_i32 tcg_res[8];
11607 
11608         for (pass = 0; pass < maxpass; pass++) {
11609             int passreg = pass < (maxpass / 2) ? rn : rm;
11610             int passelt = (pass << 1) & (maxpass - 1);
11611 
11612             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11613             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11614             tcg_res[pass] = tcg_temp_new_i32();
11615 
11616             switch (fpopcode) {
11617             case 0x10: /* FMAXNMP */
11618                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11619                                            fpst);
11620                 break;
11621             case 0x12: /* FADDP */
11622                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11623                 break;
11624             case 0x16: /* FMAXP */
11625                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11626                 break;
11627             case 0x18: /* FMINNMP */
11628                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11629                                            fpst);
11630                 break;
11631             case 0x1e: /* FMINP */
11632                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11633                 break;
11634             default:
11635                 g_assert_not_reached();
11636             }
11637         }
11638 
11639         for (pass = 0; pass < maxpass; pass++) {
11640             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11641         }
11642     } else {
11643         for (pass = 0; pass < elements; pass++) {
11644             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11645             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11646             TCGv_i32 tcg_res = tcg_temp_new_i32();
11647 
11648             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11649             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11650 
11651             switch (fpopcode) {
11652             case 0x0: /* FMAXNM */
11653                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11654                 break;
11655             case 0x1: /* FMLA */
11656                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11657                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11658                                            fpst);
11659                 break;
11660             case 0x2: /* FADD */
11661                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11662                 break;
11663             case 0x3: /* FMULX */
11664                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11665                 break;
11666             case 0x4: /* FCMEQ */
11667                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11668                 break;
11669             case 0x6: /* FMAX */
11670                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11671                 break;
11672             case 0x7: /* FRECPS */
11673                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11674                 break;
11675             case 0x8: /* FMINNM */
11676                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11677                 break;
11678             case 0x9: /* FMLS */
11679                 /* As usual for ARM, separate negation for fused multiply-add */
11680                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11681                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11682                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11683                                            fpst);
11684                 break;
11685             case 0xa: /* FSUB */
11686                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11687                 break;
11688             case 0xe: /* FMIN */
11689                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11690                 break;
11691             case 0xf: /* FRSQRTS */
11692                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11693                 break;
11694             case 0x13: /* FMUL */
11695                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11696                 break;
11697             case 0x14: /* FCMGE */
11698                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11699                 break;
11700             case 0x15: /* FACGE */
11701                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11702                 break;
11703             case 0x17: /* FDIV */
11704                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11705                 break;
11706             case 0x1a: /* FABD */
11707                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11708                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11709                 break;
11710             case 0x1c: /* FCMGT */
11711                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11712                 break;
11713             case 0x1d: /* FACGT */
11714                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11715                 break;
11716             default:
11717                 g_assert_not_reached();
11718             }
11719 
11720             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11721         }
11722     }
11723 
11724     clear_vec_high(s, is_q, rd);
11725 }
11726 
11727 /* AdvSIMD three same extra
11728  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
11729  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11730  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
11731  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11732  */
11733 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11734 {
11735     int rd = extract32(insn, 0, 5);
11736     int rn = extract32(insn, 5, 5);
11737     int opcode = extract32(insn, 11, 4);
11738     int rm = extract32(insn, 16, 5);
11739     int size = extract32(insn, 22, 2);
11740     bool u = extract32(insn, 29, 1);
11741     bool is_q = extract32(insn, 30, 1);
11742     bool feature;
11743     int rot;
11744 
11745     switch (u * 16 + opcode) {
11746     case 0x10: /* SQRDMLAH (vector) */
11747     case 0x11: /* SQRDMLSH (vector) */
11748         if (size != 1 && size != 2) {
11749             unallocated_encoding(s);
11750             return;
11751         }
11752         feature = dc_isar_feature(aa64_rdm, s);
11753         break;
11754     case 0x02: /* SDOT (vector) */
11755     case 0x12: /* UDOT (vector) */
11756         if (size != MO_32) {
11757             unallocated_encoding(s);
11758             return;
11759         }
11760         feature = dc_isar_feature(aa64_dp, s);
11761         break;
11762     case 0x03: /* USDOT */
11763         if (size != MO_32) {
11764             unallocated_encoding(s);
11765             return;
11766         }
11767         feature = dc_isar_feature(aa64_i8mm, s);
11768         break;
11769     case 0x04: /* SMMLA */
11770     case 0x14: /* UMMLA */
11771     case 0x05: /* USMMLA */
11772         if (!is_q || size != MO_32) {
11773             unallocated_encoding(s);
11774             return;
11775         }
11776         feature = dc_isar_feature(aa64_i8mm, s);
11777         break;
11778     case 0x18: /* FCMLA, #0 */
11779     case 0x19: /* FCMLA, #90 */
11780     case 0x1a: /* FCMLA, #180 */
11781     case 0x1b: /* FCMLA, #270 */
11782     case 0x1c: /* FCADD, #90 */
11783     case 0x1e: /* FCADD, #270 */
11784         if (size == 0
11785             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11786             || (size == 3 && !is_q)) {
11787             unallocated_encoding(s);
11788             return;
11789         }
11790         feature = dc_isar_feature(aa64_fcma, s);
11791         break;
11792     case 0x1d: /* BFMMLA */
11793         if (size != MO_16 || !is_q) {
11794             unallocated_encoding(s);
11795             return;
11796         }
11797         feature = dc_isar_feature(aa64_bf16, s);
11798         break;
11799     case 0x1f:
11800         switch (size) {
11801         case 1: /* BFDOT */
11802         case 3: /* BFMLAL{B,T} */
11803             feature = dc_isar_feature(aa64_bf16, s);
11804             break;
11805         default:
11806             unallocated_encoding(s);
11807             return;
11808         }
11809         break;
11810     default:
11811         unallocated_encoding(s);
11812         return;
11813     }
11814     if (!feature) {
11815         unallocated_encoding(s);
11816         return;
11817     }
11818     if (!fp_access_check(s)) {
11819         return;
11820     }
11821 
11822     switch (opcode) {
11823     case 0x0: /* SQRDMLAH (vector) */
11824         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11825         return;
11826 
11827     case 0x1: /* SQRDMLSH (vector) */
11828         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11829         return;
11830 
11831     case 0x2: /* SDOT / UDOT */
11832         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11833                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11834         return;
11835 
11836     case 0x3: /* USDOT */
11837         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11838         return;
11839 
11840     case 0x04: /* SMMLA, UMMLA */
11841         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11842                          u ? gen_helper_gvec_ummla_b
11843                          : gen_helper_gvec_smmla_b);
11844         return;
11845     case 0x05: /* USMMLA */
11846         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11847         return;
11848 
11849     case 0x8: /* FCMLA, #0 */
11850     case 0x9: /* FCMLA, #90 */
11851     case 0xa: /* FCMLA, #180 */
11852     case 0xb: /* FCMLA, #270 */
11853         rot = extract32(opcode, 0, 2);
11854         switch (size) {
11855         case 1:
11856             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11857                               gen_helper_gvec_fcmlah);
11858             break;
11859         case 2:
11860             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11861                               gen_helper_gvec_fcmlas);
11862             break;
11863         case 3:
11864             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11865                               gen_helper_gvec_fcmlad);
11866             break;
11867         default:
11868             g_assert_not_reached();
11869         }
11870         return;
11871 
11872     case 0xc: /* FCADD, #90 */
11873     case 0xe: /* FCADD, #270 */
11874         rot = extract32(opcode, 1, 1);
11875         switch (size) {
11876         case 1:
11877             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11878                               gen_helper_gvec_fcaddh);
11879             break;
11880         case 2:
11881             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11882                               gen_helper_gvec_fcadds);
11883             break;
11884         case 3:
11885             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11886                               gen_helper_gvec_fcaddd);
11887             break;
11888         default:
11889             g_assert_not_reached();
11890         }
11891         return;
11892 
11893     case 0xd: /* BFMMLA */
11894         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11895         return;
11896     case 0xf:
11897         switch (size) {
11898         case 1: /* BFDOT */
11899             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11900             break;
11901         case 3: /* BFMLAL{B,T} */
11902             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11903                               gen_helper_gvec_bfmlal);
11904             break;
11905         default:
11906             g_assert_not_reached();
11907         }
11908         return;
11909 
11910     default:
11911         g_assert_not_reached();
11912     }
11913 }
11914 
11915 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11916                                   int size, int rn, int rd)
11917 {
11918     /* Handle 2-reg-misc ops which are widening (so each size element
11919      * in the source becomes a 2*size element in the destination.
11920      * The only instruction like this is FCVTL.
11921      */
11922     int pass;
11923 
11924     if (size == 3) {
11925         /* 32 -> 64 bit fp conversion */
11926         TCGv_i64 tcg_res[2];
11927         int srcelt = is_q ? 2 : 0;
11928 
11929         for (pass = 0; pass < 2; pass++) {
11930             TCGv_i32 tcg_op = tcg_temp_new_i32();
11931             tcg_res[pass] = tcg_temp_new_i64();
11932 
11933             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11934             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
11935         }
11936         for (pass = 0; pass < 2; pass++) {
11937             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11938         }
11939     } else {
11940         /* 16 -> 32 bit fp conversion */
11941         int srcelt = is_q ? 4 : 0;
11942         TCGv_i32 tcg_res[4];
11943         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11944         TCGv_i32 ahp = get_ahp_flag();
11945 
11946         for (pass = 0; pass < 4; pass++) {
11947             tcg_res[pass] = tcg_temp_new_i32();
11948 
11949             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11950             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11951                                            fpst, ahp);
11952         }
11953         for (pass = 0; pass < 4; pass++) {
11954             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11955         }
11956     }
11957 }
11958 
11959 static void handle_rev(DisasContext *s, int opcode, bool u,
11960                        bool is_q, int size, int rn, int rd)
11961 {
11962     int op = (opcode << 1) | u;
11963     int opsz = op + size;
11964     int grp_size = 3 - opsz;
11965     int dsize = is_q ? 128 : 64;
11966     int i;
11967 
11968     if (opsz >= 3) {
11969         unallocated_encoding(s);
11970         return;
11971     }
11972 
11973     if (!fp_access_check(s)) {
11974         return;
11975     }
11976 
11977     if (size == 0) {
11978         /* Special case bytes, use bswap op on each group of elements */
11979         int groups = dsize / (8 << grp_size);
11980 
11981         for (i = 0; i < groups; i++) {
11982             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11983 
11984             read_vec_element(s, tcg_tmp, rn, i, grp_size);
11985             switch (grp_size) {
11986             case MO_16:
11987                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11988                 break;
11989             case MO_32:
11990                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11991                 break;
11992             case MO_64:
11993                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11994                 break;
11995             default:
11996                 g_assert_not_reached();
11997             }
11998             write_vec_element(s, tcg_tmp, rd, i, grp_size);
11999         }
12000         clear_vec_high(s, is_q, rd);
12001     } else {
12002         int revmask = (1 << grp_size) - 1;
12003         int esize = 8 << size;
12004         int elements = dsize / esize;
12005         TCGv_i64 tcg_rn = tcg_temp_new_i64();
12006         TCGv_i64 tcg_rd[2];
12007 
12008         for (i = 0; i < 2; i++) {
12009             tcg_rd[i] = tcg_temp_new_i64();
12010             tcg_gen_movi_i64(tcg_rd[i], 0);
12011         }
12012 
12013         for (i = 0; i < elements; i++) {
12014             int e_rev = (i & 0xf) ^ revmask;
12015             int w = (e_rev * esize) / 64;
12016             int o = (e_rev * esize) % 64;
12017 
12018             read_vec_element(s, tcg_rn, rn, i, size);
12019             tcg_gen_deposit_i64(tcg_rd[w], tcg_rd[w], tcg_rn, o, esize);
12020         }
12021 
12022         for (i = 0; i < 2; i++) {
12023             write_vec_element(s, tcg_rd[i], rd, i, MO_64);
12024         }
12025         clear_vec_high(s, true, rd);
12026     }
12027 }
12028 
12029 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
12030                                   bool is_q, int size, int rn, int rd)
12031 {
12032     /* Implement the pairwise operations from 2-misc:
12033      * SADDLP, UADDLP, SADALP, UADALP.
12034      * These all add pairs of elements in the input to produce a
12035      * double-width result element in the output (possibly accumulating).
12036      */
12037     bool accum = (opcode == 0x6);
12038     int maxpass = is_q ? 2 : 1;
12039     int pass;
12040     TCGv_i64 tcg_res[2];
12041 
12042     if (size == 2) {
12043         /* 32 + 32 -> 64 op */
12044         MemOp memop = size + (u ? 0 : MO_SIGN);
12045 
12046         for (pass = 0; pass < maxpass; pass++) {
12047             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
12048             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
12049 
12050             tcg_res[pass] = tcg_temp_new_i64();
12051 
12052             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
12053             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
12054             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
12055             if (accum) {
12056                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
12057                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12058             }
12059         }
12060     } else {
12061         for (pass = 0; pass < maxpass; pass++) {
12062             TCGv_i64 tcg_op = tcg_temp_new_i64();
12063             NeonGenOne64OpFn *genfn;
12064             static NeonGenOne64OpFn * const fns[2][2] = {
12065                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
12066                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
12067             };
12068 
12069             genfn = fns[size][u];
12070 
12071             tcg_res[pass] = tcg_temp_new_i64();
12072 
12073             read_vec_element(s, tcg_op, rn, pass, MO_64);
12074             genfn(tcg_res[pass], tcg_op);
12075 
12076             if (accum) {
12077                 read_vec_element(s, tcg_op, rd, pass, MO_64);
12078                 if (size == 0) {
12079                     gen_helper_neon_addl_u16(tcg_res[pass],
12080                                              tcg_res[pass], tcg_op);
12081                 } else {
12082                     gen_helper_neon_addl_u32(tcg_res[pass],
12083                                              tcg_res[pass], tcg_op);
12084                 }
12085             }
12086         }
12087     }
12088     if (!is_q) {
12089         tcg_res[1] = tcg_constant_i64(0);
12090     }
12091     for (pass = 0; pass < 2; pass++) {
12092         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12093     }
12094 }
12095 
12096 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
12097 {
12098     /* Implement SHLL and SHLL2 */
12099     int pass;
12100     int part = is_q ? 2 : 0;
12101     TCGv_i64 tcg_res[2];
12102 
12103     for (pass = 0; pass < 2; pass++) {
12104         static NeonGenWidenFn * const widenfns[3] = {
12105             gen_helper_neon_widen_u8,
12106             gen_helper_neon_widen_u16,
12107             tcg_gen_extu_i32_i64,
12108         };
12109         NeonGenWidenFn *widenfn = widenfns[size];
12110         TCGv_i32 tcg_op = tcg_temp_new_i32();
12111 
12112         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
12113         tcg_res[pass] = tcg_temp_new_i64();
12114         widenfn(tcg_res[pass], tcg_op);
12115         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
12116     }
12117 
12118     for (pass = 0; pass < 2; pass++) {
12119         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12120     }
12121 }
12122 
12123 /* AdvSIMD two reg misc
12124  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
12125  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12126  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12127  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12128  */
12129 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
12130 {
12131     int size = extract32(insn, 22, 2);
12132     int opcode = extract32(insn, 12, 5);
12133     bool u = extract32(insn, 29, 1);
12134     bool is_q = extract32(insn, 30, 1);
12135     int rn = extract32(insn, 5, 5);
12136     int rd = extract32(insn, 0, 5);
12137     bool need_fpstatus = false;
12138     int rmode = -1;
12139     TCGv_i32 tcg_rmode;
12140     TCGv_ptr tcg_fpstatus;
12141 
12142     switch (opcode) {
12143     case 0x0: /* REV64, REV32 */
12144     case 0x1: /* REV16 */
12145         handle_rev(s, opcode, u, is_q, size, rn, rd);
12146         return;
12147     case 0x5: /* CNT, NOT, RBIT */
12148         if (u && size == 0) {
12149             /* NOT */
12150             break;
12151         } else if (u && size == 1) {
12152             /* RBIT */
12153             break;
12154         } else if (!u && size == 0) {
12155             /* CNT */
12156             break;
12157         }
12158         unallocated_encoding(s);
12159         return;
12160     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12161     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12162         if (size == 3) {
12163             unallocated_encoding(s);
12164             return;
12165         }
12166         if (!fp_access_check(s)) {
12167             return;
12168         }
12169 
12170         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
12171         return;
12172     case 0x4: /* CLS, CLZ */
12173         if (size == 3) {
12174             unallocated_encoding(s);
12175             return;
12176         }
12177         break;
12178     case 0x2: /* SADDLP, UADDLP */
12179     case 0x6: /* SADALP, UADALP */
12180         if (size == 3) {
12181             unallocated_encoding(s);
12182             return;
12183         }
12184         if (!fp_access_check(s)) {
12185             return;
12186         }
12187         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
12188         return;
12189     case 0x13: /* SHLL, SHLL2 */
12190         if (u == 0 || size == 3) {
12191             unallocated_encoding(s);
12192             return;
12193         }
12194         if (!fp_access_check(s)) {
12195             return;
12196         }
12197         handle_shll(s, is_q, size, rn, rd);
12198         return;
12199     case 0xa: /* CMLT */
12200         if (u == 1) {
12201             unallocated_encoding(s);
12202             return;
12203         }
12204         /* fall through */
12205     case 0x8: /* CMGT, CMGE */
12206     case 0x9: /* CMEQ, CMLE */
12207     case 0xb: /* ABS, NEG */
12208         if (size == 3 && !is_q) {
12209             unallocated_encoding(s);
12210             return;
12211         }
12212         break;
12213     case 0x3: /* SUQADD, USQADD */
12214         if (size == 3 && !is_q) {
12215             unallocated_encoding(s);
12216             return;
12217         }
12218         if (!fp_access_check(s)) {
12219             return;
12220         }
12221         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12222         return;
12223     case 0x7: /* SQABS, SQNEG */
12224         if (size == 3 && !is_q) {
12225             unallocated_encoding(s);
12226             return;
12227         }
12228         break;
12229     case 0xc ... 0xf:
12230     case 0x16 ... 0x1f:
12231     {
12232         /* Floating point: U, size[1] and opcode indicate operation;
12233          * size[0] indicates single or double precision.
12234          */
12235         int is_double = extract32(size, 0, 1);
12236         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12237         size = is_double ? 3 : 2;
12238         switch (opcode) {
12239         case 0x2f: /* FABS */
12240         case 0x6f: /* FNEG */
12241             if (size == 3 && !is_q) {
12242                 unallocated_encoding(s);
12243                 return;
12244             }
12245             break;
12246         case 0x1d: /* SCVTF */
12247         case 0x5d: /* UCVTF */
12248         {
12249             bool is_signed = (opcode == 0x1d) ? true : false;
12250             int elements = is_double ? 2 : is_q ? 4 : 2;
12251             if (is_double && !is_q) {
12252                 unallocated_encoding(s);
12253                 return;
12254             }
12255             if (!fp_access_check(s)) {
12256                 return;
12257             }
12258             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12259             return;
12260         }
12261         case 0x2c: /* FCMGT (zero) */
12262         case 0x2d: /* FCMEQ (zero) */
12263         case 0x2e: /* FCMLT (zero) */
12264         case 0x6c: /* FCMGE (zero) */
12265         case 0x6d: /* FCMLE (zero) */
12266             if (size == 3 && !is_q) {
12267                 unallocated_encoding(s);
12268                 return;
12269             }
12270             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12271             return;
12272         case 0x7f: /* FSQRT */
12273             if (size == 3 && !is_q) {
12274                 unallocated_encoding(s);
12275                 return;
12276             }
12277             break;
12278         case 0x1a: /* FCVTNS */
12279         case 0x1b: /* FCVTMS */
12280         case 0x3a: /* FCVTPS */
12281         case 0x3b: /* FCVTZS */
12282         case 0x5a: /* FCVTNU */
12283         case 0x5b: /* FCVTMU */
12284         case 0x7a: /* FCVTPU */
12285         case 0x7b: /* FCVTZU */
12286             need_fpstatus = true;
12287             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12288             if (size == 3 && !is_q) {
12289                 unallocated_encoding(s);
12290                 return;
12291             }
12292             break;
12293         case 0x5c: /* FCVTAU */
12294         case 0x1c: /* FCVTAS */
12295             need_fpstatus = true;
12296             rmode = FPROUNDING_TIEAWAY;
12297             if (size == 3 && !is_q) {
12298                 unallocated_encoding(s);
12299                 return;
12300             }
12301             break;
12302         case 0x3c: /* URECPE */
12303             if (size == 3) {
12304                 unallocated_encoding(s);
12305                 return;
12306             }
12307             /* fall through */
12308         case 0x3d: /* FRECPE */
12309         case 0x7d: /* FRSQRTE */
12310             if (size == 3 && !is_q) {
12311                 unallocated_encoding(s);
12312                 return;
12313             }
12314             if (!fp_access_check(s)) {
12315                 return;
12316             }
12317             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12318             return;
12319         case 0x56: /* FCVTXN, FCVTXN2 */
12320             if (size == 2) {
12321                 unallocated_encoding(s);
12322                 return;
12323             }
12324             /* fall through */
12325         case 0x16: /* FCVTN, FCVTN2 */
12326             /* handle_2misc_narrow does a 2*size -> size operation, but these
12327              * instructions encode the source size rather than dest size.
12328              */
12329             if (!fp_access_check(s)) {
12330                 return;
12331             }
12332             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12333             return;
12334         case 0x36: /* BFCVTN, BFCVTN2 */
12335             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
12336                 unallocated_encoding(s);
12337                 return;
12338             }
12339             if (!fp_access_check(s)) {
12340                 return;
12341             }
12342             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12343             return;
12344         case 0x17: /* FCVTL, FCVTL2 */
12345             if (!fp_access_check(s)) {
12346                 return;
12347             }
12348             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12349             return;
12350         case 0x18: /* FRINTN */
12351         case 0x19: /* FRINTM */
12352         case 0x38: /* FRINTP */
12353         case 0x39: /* FRINTZ */
12354             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12355             /* fall through */
12356         case 0x59: /* FRINTX */
12357         case 0x79: /* FRINTI */
12358             need_fpstatus = true;
12359             if (size == 3 && !is_q) {
12360                 unallocated_encoding(s);
12361                 return;
12362             }
12363             break;
12364         case 0x58: /* FRINTA */
12365             rmode = FPROUNDING_TIEAWAY;
12366             need_fpstatus = true;
12367             if (size == 3 && !is_q) {
12368                 unallocated_encoding(s);
12369                 return;
12370             }
12371             break;
12372         case 0x7c: /* URSQRTE */
12373             if (size == 3) {
12374                 unallocated_encoding(s);
12375                 return;
12376             }
12377             break;
12378         case 0x1e: /* FRINT32Z */
12379         case 0x1f: /* FRINT64Z */
12380             rmode = FPROUNDING_ZERO;
12381             /* fall through */
12382         case 0x5e: /* FRINT32X */
12383         case 0x5f: /* FRINT64X */
12384             need_fpstatus = true;
12385             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12386                 unallocated_encoding(s);
12387                 return;
12388             }
12389             break;
12390         default:
12391             unallocated_encoding(s);
12392             return;
12393         }
12394         break;
12395     }
12396     default:
12397         unallocated_encoding(s);
12398         return;
12399     }
12400 
12401     if (!fp_access_check(s)) {
12402         return;
12403     }
12404 
12405     if (need_fpstatus || rmode >= 0) {
12406         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12407     } else {
12408         tcg_fpstatus = NULL;
12409     }
12410     if (rmode >= 0) {
12411         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12412     } else {
12413         tcg_rmode = NULL;
12414     }
12415 
12416     switch (opcode) {
12417     case 0x5:
12418         if (u && size == 0) { /* NOT */
12419             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12420             return;
12421         }
12422         break;
12423     case 0x8: /* CMGT, CMGE */
12424         if (u) {
12425             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12426         } else {
12427             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12428         }
12429         return;
12430     case 0x9: /* CMEQ, CMLE */
12431         if (u) {
12432             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12433         } else {
12434             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12435         }
12436         return;
12437     case 0xa: /* CMLT */
12438         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12439         return;
12440     case 0xb:
12441         if (u) { /* ABS, NEG */
12442             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12443         } else {
12444             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12445         }
12446         return;
12447     }
12448 
12449     if (size == 3) {
12450         /* All 64-bit element operations can be shared with scalar 2misc */
12451         int pass;
12452 
12453         /* Coverity claims (size == 3 && !is_q) has been eliminated
12454          * from all paths leading to here.
12455          */
12456         tcg_debug_assert(is_q);
12457         for (pass = 0; pass < 2; pass++) {
12458             TCGv_i64 tcg_op = tcg_temp_new_i64();
12459             TCGv_i64 tcg_res = tcg_temp_new_i64();
12460 
12461             read_vec_element(s, tcg_op, rn, pass, MO_64);
12462 
12463             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12464                             tcg_rmode, tcg_fpstatus);
12465 
12466             write_vec_element(s, tcg_res, rd, pass, MO_64);
12467         }
12468     } else {
12469         int pass;
12470 
12471         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12472             TCGv_i32 tcg_op = tcg_temp_new_i32();
12473             TCGv_i32 tcg_res = tcg_temp_new_i32();
12474 
12475             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12476 
12477             if (size == 2) {
12478                 /* Special cases for 32 bit elements */
12479                 switch (opcode) {
12480                 case 0x4: /* CLS */
12481                     if (u) {
12482                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12483                     } else {
12484                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
12485                     }
12486                     break;
12487                 case 0x7: /* SQABS, SQNEG */
12488                     if (u) {
12489                         gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12490                     } else {
12491                         gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12492                     }
12493                     break;
12494                 case 0x2f: /* FABS */
12495                     gen_helper_vfp_abss(tcg_res, tcg_op);
12496                     break;
12497                 case 0x6f: /* FNEG */
12498                     gen_helper_vfp_negs(tcg_res, tcg_op);
12499                     break;
12500                 case 0x7f: /* FSQRT */
12501                     gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12502                     break;
12503                 case 0x1a: /* FCVTNS */
12504                 case 0x1b: /* FCVTMS */
12505                 case 0x1c: /* FCVTAS */
12506                 case 0x3a: /* FCVTPS */
12507                 case 0x3b: /* FCVTZS */
12508                     gen_helper_vfp_tosls(tcg_res, tcg_op,
12509                                          tcg_constant_i32(0), tcg_fpstatus);
12510                     break;
12511                 case 0x5a: /* FCVTNU */
12512                 case 0x5b: /* FCVTMU */
12513                 case 0x5c: /* FCVTAU */
12514                 case 0x7a: /* FCVTPU */
12515                 case 0x7b: /* FCVTZU */
12516                     gen_helper_vfp_touls(tcg_res, tcg_op,
12517                                          tcg_constant_i32(0), tcg_fpstatus);
12518                     break;
12519                 case 0x18: /* FRINTN */
12520                 case 0x19: /* FRINTM */
12521                 case 0x38: /* FRINTP */
12522                 case 0x39: /* FRINTZ */
12523                 case 0x58: /* FRINTA */
12524                 case 0x79: /* FRINTI */
12525                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12526                     break;
12527                 case 0x59: /* FRINTX */
12528                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12529                     break;
12530                 case 0x7c: /* URSQRTE */
12531                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
12532                     break;
12533                 case 0x1e: /* FRINT32Z */
12534                 case 0x5e: /* FRINT32X */
12535                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12536                     break;
12537                 case 0x1f: /* FRINT64Z */
12538                 case 0x5f: /* FRINT64X */
12539                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12540                     break;
12541                 default:
12542                     g_assert_not_reached();
12543                 }
12544             } else {
12545                 /* Use helpers for 8 and 16 bit elements */
12546                 switch (opcode) {
12547                 case 0x5: /* CNT, RBIT */
12548                     /* For these two insns size is part of the opcode specifier
12549                      * (handled earlier); they always operate on byte elements.
12550                      */
12551                     if (u) {
12552                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12553                     } else {
12554                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12555                     }
12556                     break;
12557                 case 0x7: /* SQABS, SQNEG */
12558                 {
12559                     NeonGenOneOpEnvFn *genfn;
12560                     static NeonGenOneOpEnvFn * const fns[2][2] = {
12561                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12562                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12563                     };
12564                     genfn = fns[size][u];
12565                     genfn(tcg_res, cpu_env, tcg_op);
12566                     break;
12567                 }
12568                 case 0x4: /* CLS, CLZ */
12569                     if (u) {
12570                         if (size == 0) {
12571                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
12572                         } else {
12573                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
12574                         }
12575                     } else {
12576                         if (size == 0) {
12577                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
12578                         } else {
12579                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
12580                         }
12581                     }
12582                     break;
12583                 default:
12584                     g_assert_not_reached();
12585                 }
12586             }
12587 
12588             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12589         }
12590     }
12591     clear_vec_high(s, is_q, rd);
12592 
12593     if (tcg_rmode) {
12594         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12595     }
12596 }
12597 
12598 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12599  *
12600  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
12601  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12602  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12603  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12604  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12605  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12606  *
12607  * This actually covers two groups where scalar access is governed by
12608  * bit 28. A bunch of the instructions (float to integral) only exist
12609  * in the vector form and are un-allocated for the scalar decode. Also
12610  * in the scalar decode Q is always 1.
12611  */
12612 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12613 {
12614     int fpop, opcode, a, u;
12615     int rn, rd;
12616     bool is_q;
12617     bool is_scalar;
12618     bool only_in_vector = false;
12619 
12620     int pass;
12621     TCGv_i32 tcg_rmode = NULL;
12622     TCGv_ptr tcg_fpstatus = NULL;
12623     bool need_fpst = true;
12624     int rmode = -1;
12625 
12626     if (!dc_isar_feature(aa64_fp16, s)) {
12627         unallocated_encoding(s);
12628         return;
12629     }
12630 
12631     rd = extract32(insn, 0, 5);
12632     rn = extract32(insn, 5, 5);
12633 
12634     a = extract32(insn, 23, 1);
12635     u = extract32(insn, 29, 1);
12636     is_scalar = extract32(insn, 28, 1);
12637     is_q = extract32(insn, 30, 1);
12638 
12639     opcode = extract32(insn, 12, 5);
12640     fpop = deposit32(opcode, 5, 1, a);
12641     fpop = deposit32(fpop, 6, 1, u);
12642 
12643     switch (fpop) {
12644     case 0x1d: /* SCVTF */
12645     case 0x5d: /* UCVTF */
12646     {
12647         int elements;
12648 
12649         if (is_scalar) {
12650             elements = 1;
12651         } else {
12652             elements = (is_q ? 8 : 4);
12653         }
12654 
12655         if (!fp_access_check(s)) {
12656             return;
12657         }
12658         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12659         return;
12660     }
12661     break;
12662     case 0x2c: /* FCMGT (zero) */
12663     case 0x2d: /* FCMEQ (zero) */
12664     case 0x2e: /* FCMLT (zero) */
12665     case 0x6c: /* FCMGE (zero) */
12666     case 0x6d: /* FCMLE (zero) */
12667         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12668         return;
12669     case 0x3d: /* FRECPE */
12670     case 0x3f: /* FRECPX */
12671         break;
12672     case 0x18: /* FRINTN */
12673         only_in_vector = true;
12674         rmode = FPROUNDING_TIEEVEN;
12675         break;
12676     case 0x19: /* FRINTM */
12677         only_in_vector = true;
12678         rmode = FPROUNDING_NEGINF;
12679         break;
12680     case 0x38: /* FRINTP */
12681         only_in_vector = true;
12682         rmode = FPROUNDING_POSINF;
12683         break;
12684     case 0x39: /* FRINTZ */
12685         only_in_vector = true;
12686         rmode = FPROUNDING_ZERO;
12687         break;
12688     case 0x58: /* FRINTA */
12689         only_in_vector = true;
12690         rmode = FPROUNDING_TIEAWAY;
12691         break;
12692     case 0x59: /* FRINTX */
12693     case 0x79: /* FRINTI */
12694         only_in_vector = true;
12695         /* current rounding mode */
12696         break;
12697     case 0x1a: /* FCVTNS */
12698         rmode = FPROUNDING_TIEEVEN;
12699         break;
12700     case 0x1b: /* FCVTMS */
12701         rmode = FPROUNDING_NEGINF;
12702         break;
12703     case 0x1c: /* FCVTAS */
12704         rmode = FPROUNDING_TIEAWAY;
12705         break;
12706     case 0x3a: /* FCVTPS */
12707         rmode = FPROUNDING_POSINF;
12708         break;
12709     case 0x3b: /* FCVTZS */
12710         rmode = FPROUNDING_ZERO;
12711         break;
12712     case 0x5a: /* FCVTNU */
12713         rmode = FPROUNDING_TIEEVEN;
12714         break;
12715     case 0x5b: /* FCVTMU */
12716         rmode = FPROUNDING_NEGINF;
12717         break;
12718     case 0x5c: /* FCVTAU */
12719         rmode = FPROUNDING_TIEAWAY;
12720         break;
12721     case 0x7a: /* FCVTPU */
12722         rmode = FPROUNDING_POSINF;
12723         break;
12724     case 0x7b: /* FCVTZU */
12725         rmode = FPROUNDING_ZERO;
12726         break;
12727     case 0x2f: /* FABS */
12728     case 0x6f: /* FNEG */
12729         need_fpst = false;
12730         break;
12731     case 0x7d: /* FRSQRTE */
12732     case 0x7f: /* FSQRT (vector) */
12733         break;
12734     default:
12735         unallocated_encoding(s);
12736         return;
12737     }
12738 
12739 
12740     /* Check additional constraints for the scalar encoding */
12741     if (is_scalar) {
12742         if (!is_q) {
12743             unallocated_encoding(s);
12744             return;
12745         }
12746         /* FRINTxx is only in the vector form */
12747         if (only_in_vector) {
12748             unallocated_encoding(s);
12749             return;
12750         }
12751     }
12752 
12753     if (!fp_access_check(s)) {
12754         return;
12755     }
12756 
12757     if (rmode >= 0 || need_fpst) {
12758         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
12759     }
12760 
12761     if (rmode >= 0) {
12762         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12763     }
12764 
12765     if (is_scalar) {
12766         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12767         TCGv_i32 tcg_res = tcg_temp_new_i32();
12768 
12769         switch (fpop) {
12770         case 0x1a: /* FCVTNS */
12771         case 0x1b: /* FCVTMS */
12772         case 0x1c: /* FCVTAS */
12773         case 0x3a: /* FCVTPS */
12774         case 0x3b: /* FCVTZS */
12775             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12776             break;
12777         case 0x3d: /* FRECPE */
12778             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12779             break;
12780         case 0x3f: /* FRECPX */
12781             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12782             break;
12783         case 0x5a: /* FCVTNU */
12784         case 0x5b: /* FCVTMU */
12785         case 0x5c: /* FCVTAU */
12786         case 0x7a: /* FCVTPU */
12787         case 0x7b: /* FCVTZU */
12788             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12789             break;
12790         case 0x6f: /* FNEG */
12791             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12792             break;
12793         case 0x7d: /* FRSQRTE */
12794             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12795             break;
12796         default:
12797             g_assert_not_reached();
12798         }
12799 
12800         /* limit any sign extension going on */
12801         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12802         write_fp_sreg(s, rd, tcg_res);
12803     } else {
12804         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12805             TCGv_i32 tcg_op = tcg_temp_new_i32();
12806             TCGv_i32 tcg_res = tcg_temp_new_i32();
12807 
12808             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12809 
12810             switch (fpop) {
12811             case 0x1a: /* FCVTNS */
12812             case 0x1b: /* FCVTMS */
12813             case 0x1c: /* FCVTAS */
12814             case 0x3a: /* FCVTPS */
12815             case 0x3b: /* FCVTZS */
12816                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12817                 break;
12818             case 0x3d: /* FRECPE */
12819                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12820                 break;
12821             case 0x5a: /* FCVTNU */
12822             case 0x5b: /* FCVTMU */
12823             case 0x5c: /* FCVTAU */
12824             case 0x7a: /* FCVTPU */
12825             case 0x7b: /* FCVTZU */
12826                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12827                 break;
12828             case 0x18: /* FRINTN */
12829             case 0x19: /* FRINTM */
12830             case 0x38: /* FRINTP */
12831             case 0x39: /* FRINTZ */
12832             case 0x58: /* FRINTA */
12833             case 0x79: /* FRINTI */
12834                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12835                 break;
12836             case 0x59: /* FRINTX */
12837                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12838                 break;
12839             case 0x2f: /* FABS */
12840                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12841                 break;
12842             case 0x6f: /* FNEG */
12843                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12844                 break;
12845             case 0x7d: /* FRSQRTE */
12846                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12847                 break;
12848             case 0x7f: /* FSQRT */
12849                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12850                 break;
12851             default:
12852                 g_assert_not_reached();
12853             }
12854 
12855             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12856         }
12857 
12858         clear_vec_high(s, is_q, rd);
12859     }
12860 
12861     if (tcg_rmode) {
12862         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12863     }
12864 }
12865 
12866 /* AdvSIMD scalar x indexed element
12867  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12868  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12869  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12870  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12871  * AdvSIMD vector x indexed element
12872  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12873  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12874  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12875  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12876  */
12877 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12878 {
12879     /* This encoding has two kinds of instruction:
12880      *  normal, where we perform elt x idxelt => elt for each
12881      *     element in the vector
12882      *  long, where we perform elt x idxelt and generate a result of
12883      *     double the width of the input element
12884      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12885      */
12886     bool is_scalar = extract32(insn, 28, 1);
12887     bool is_q = extract32(insn, 30, 1);
12888     bool u = extract32(insn, 29, 1);
12889     int size = extract32(insn, 22, 2);
12890     int l = extract32(insn, 21, 1);
12891     int m = extract32(insn, 20, 1);
12892     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12893     int rm = extract32(insn, 16, 4);
12894     int opcode = extract32(insn, 12, 4);
12895     int h = extract32(insn, 11, 1);
12896     int rn = extract32(insn, 5, 5);
12897     int rd = extract32(insn, 0, 5);
12898     bool is_long = false;
12899     int is_fp = 0;
12900     bool is_fp16 = false;
12901     int index;
12902     TCGv_ptr fpst;
12903 
12904     switch (16 * u + opcode) {
12905     case 0x08: /* MUL */
12906     case 0x10: /* MLA */
12907     case 0x14: /* MLS */
12908         if (is_scalar) {
12909             unallocated_encoding(s);
12910             return;
12911         }
12912         break;
12913     case 0x02: /* SMLAL, SMLAL2 */
12914     case 0x12: /* UMLAL, UMLAL2 */
12915     case 0x06: /* SMLSL, SMLSL2 */
12916     case 0x16: /* UMLSL, UMLSL2 */
12917     case 0x0a: /* SMULL, SMULL2 */
12918     case 0x1a: /* UMULL, UMULL2 */
12919         if (is_scalar) {
12920             unallocated_encoding(s);
12921             return;
12922         }
12923         is_long = true;
12924         break;
12925     case 0x03: /* SQDMLAL, SQDMLAL2 */
12926     case 0x07: /* SQDMLSL, SQDMLSL2 */
12927     case 0x0b: /* SQDMULL, SQDMULL2 */
12928         is_long = true;
12929         break;
12930     case 0x0c: /* SQDMULH */
12931     case 0x0d: /* SQRDMULH */
12932         break;
12933     case 0x01: /* FMLA */
12934     case 0x05: /* FMLS */
12935     case 0x09: /* FMUL */
12936     case 0x19: /* FMULX */
12937         is_fp = 1;
12938         break;
12939     case 0x1d: /* SQRDMLAH */
12940     case 0x1f: /* SQRDMLSH */
12941         if (!dc_isar_feature(aa64_rdm, s)) {
12942             unallocated_encoding(s);
12943             return;
12944         }
12945         break;
12946     case 0x0e: /* SDOT */
12947     case 0x1e: /* UDOT */
12948         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12949             unallocated_encoding(s);
12950             return;
12951         }
12952         break;
12953     case 0x0f:
12954         switch (size) {
12955         case 0: /* SUDOT */
12956         case 2: /* USDOT */
12957             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12958                 unallocated_encoding(s);
12959                 return;
12960             }
12961             size = MO_32;
12962             break;
12963         case 1: /* BFDOT */
12964             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12965                 unallocated_encoding(s);
12966                 return;
12967             }
12968             size = MO_32;
12969             break;
12970         case 3: /* BFMLAL{B,T} */
12971             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12972                 unallocated_encoding(s);
12973                 return;
12974             }
12975             /* can't set is_fp without other incorrect size checks */
12976             size = MO_16;
12977             break;
12978         default:
12979             unallocated_encoding(s);
12980             return;
12981         }
12982         break;
12983     case 0x11: /* FCMLA #0 */
12984     case 0x13: /* FCMLA #90 */
12985     case 0x15: /* FCMLA #180 */
12986     case 0x17: /* FCMLA #270 */
12987         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
12988             unallocated_encoding(s);
12989             return;
12990         }
12991         is_fp = 2;
12992         break;
12993     case 0x00: /* FMLAL */
12994     case 0x04: /* FMLSL */
12995     case 0x18: /* FMLAL2 */
12996     case 0x1c: /* FMLSL2 */
12997         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
12998             unallocated_encoding(s);
12999             return;
13000         }
13001         size = MO_16;
13002         /* is_fp, but we pass cpu_env not fp_status.  */
13003         break;
13004     default:
13005         unallocated_encoding(s);
13006         return;
13007     }
13008 
13009     switch (is_fp) {
13010     case 1: /* normal fp */
13011         /* convert insn encoded size to MemOp size */
13012         switch (size) {
13013         case 0: /* half-precision */
13014             size = MO_16;
13015             is_fp16 = true;
13016             break;
13017         case MO_32: /* single precision */
13018         case MO_64: /* double precision */
13019             break;
13020         default:
13021             unallocated_encoding(s);
13022             return;
13023         }
13024         break;
13025 
13026     case 2: /* complex fp */
13027         /* Each indexable element is a complex pair.  */
13028         size += 1;
13029         switch (size) {
13030         case MO_32:
13031             if (h && !is_q) {
13032                 unallocated_encoding(s);
13033                 return;
13034             }
13035             is_fp16 = true;
13036             break;
13037         case MO_64:
13038             break;
13039         default:
13040             unallocated_encoding(s);
13041             return;
13042         }
13043         break;
13044 
13045     default: /* integer */
13046         switch (size) {
13047         case MO_8:
13048         case MO_64:
13049             unallocated_encoding(s);
13050             return;
13051         }
13052         break;
13053     }
13054     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
13055         unallocated_encoding(s);
13056         return;
13057     }
13058 
13059     /* Given MemOp size, adjust register and indexing.  */
13060     switch (size) {
13061     case MO_16:
13062         index = h << 2 | l << 1 | m;
13063         break;
13064     case MO_32:
13065         index = h << 1 | l;
13066         rm |= m << 4;
13067         break;
13068     case MO_64:
13069         if (l || !is_q) {
13070             unallocated_encoding(s);
13071             return;
13072         }
13073         index = h;
13074         rm |= m << 4;
13075         break;
13076     default:
13077         g_assert_not_reached();
13078     }
13079 
13080     if (!fp_access_check(s)) {
13081         return;
13082     }
13083 
13084     if (is_fp) {
13085         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
13086     } else {
13087         fpst = NULL;
13088     }
13089 
13090     switch (16 * u + opcode) {
13091     case 0x0e: /* SDOT */
13092     case 0x1e: /* UDOT */
13093         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13094                          u ? gen_helper_gvec_udot_idx_b
13095                          : gen_helper_gvec_sdot_idx_b);
13096         return;
13097     case 0x0f:
13098         switch (extract32(insn, 22, 2)) {
13099         case 0: /* SUDOT */
13100             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13101                              gen_helper_gvec_sudot_idx_b);
13102             return;
13103         case 1: /* BFDOT */
13104             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13105                              gen_helper_gvec_bfdot_idx);
13106             return;
13107         case 2: /* USDOT */
13108             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13109                              gen_helper_gvec_usdot_idx_b);
13110             return;
13111         case 3: /* BFMLAL{B,T} */
13112             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
13113                               gen_helper_gvec_bfmlal_idx);
13114             return;
13115         }
13116         g_assert_not_reached();
13117     case 0x11: /* FCMLA #0 */
13118     case 0x13: /* FCMLA #90 */
13119     case 0x15: /* FCMLA #180 */
13120     case 0x17: /* FCMLA #270 */
13121         {
13122             int rot = extract32(insn, 13, 2);
13123             int data = (index << 2) | rot;
13124             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
13125                                vec_full_reg_offset(s, rn),
13126                                vec_full_reg_offset(s, rm),
13127                                vec_full_reg_offset(s, rd), fpst,
13128                                is_q ? 16 : 8, vec_full_reg_size(s), data,
13129                                size == MO_64
13130                                ? gen_helper_gvec_fcmlas_idx
13131                                : gen_helper_gvec_fcmlah_idx);
13132         }
13133         return;
13134 
13135     case 0x00: /* FMLAL */
13136     case 0x04: /* FMLSL */
13137     case 0x18: /* FMLAL2 */
13138     case 0x1c: /* FMLSL2 */
13139         {
13140             int is_s = extract32(opcode, 2, 1);
13141             int is_2 = u;
13142             int data = (index << 2) | (is_2 << 1) | is_s;
13143             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13144                                vec_full_reg_offset(s, rn),
13145                                vec_full_reg_offset(s, rm), cpu_env,
13146                                is_q ? 16 : 8, vec_full_reg_size(s),
13147                                data, gen_helper_gvec_fmlal_idx_a64);
13148         }
13149         return;
13150 
13151     case 0x08: /* MUL */
13152         if (!is_long && !is_scalar) {
13153             static gen_helper_gvec_3 * const fns[3] = {
13154                 gen_helper_gvec_mul_idx_h,
13155                 gen_helper_gvec_mul_idx_s,
13156                 gen_helper_gvec_mul_idx_d,
13157             };
13158             tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
13159                                vec_full_reg_offset(s, rn),
13160                                vec_full_reg_offset(s, rm),
13161                                is_q ? 16 : 8, vec_full_reg_size(s),
13162                                index, fns[size - 1]);
13163             return;
13164         }
13165         break;
13166 
13167     case 0x10: /* MLA */
13168         if (!is_long && !is_scalar) {
13169             static gen_helper_gvec_4 * const fns[3] = {
13170                 gen_helper_gvec_mla_idx_h,
13171                 gen_helper_gvec_mla_idx_s,
13172                 gen_helper_gvec_mla_idx_d,
13173             };
13174             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13175                                vec_full_reg_offset(s, rn),
13176                                vec_full_reg_offset(s, rm),
13177                                vec_full_reg_offset(s, rd),
13178                                is_q ? 16 : 8, vec_full_reg_size(s),
13179                                index, fns[size - 1]);
13180             return;
13181         }
13182         break;
13183 
13184     case 0x14: /* MLS */
13185         if (!is_long && !is_scalar) {
13186             static gen_helper_gvec_4 * const fns[3] = {
13187                 gen_helper_gvec_mls_idx_h,
13188                 gen_helper_gvec_mls_idx_s,
13189                 gen_helper_gvec_mls_idx_d,
13190             };
13191             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13192                                vec_full_reg_offset(s, rn),
13193                                vec_full_reg_offset(s, rm),
13194                                vec_full_reg_offset(s, rd),
13195                                is_q ? 16 : 8, vec_full_reg_size(s),
13196                                index, fns[size - 1]);
13197             return;
13198         }
13199         break;
13200     }
13201 
13202     if (size == 3) {
13203         TCGv_i64 tcg_idx = tcg_temp_new_i64();
13204         int pass;
13205 
13206         assert(is_fp && is_q && !is_long);
13207 
13208         read_vec_element(s, tcg_idx, rm, index, MO_64);
13209 
13210         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13211             TCGv_i64 tcg_op = tcg_temp_new_i64();
13212             TCGv_i64 tcg_res = tcg_temp_new_i64();
13213 
13214             read_vec_element(s, tcg_op, rn, pass, MO_64);
13215 
13216             switch (16 * u + opcode) {
13217             case 0x05: /* FMLS */
13218                 /* As usual for ARM, separate negation for fused multiply-add */
13219                 gen_helper_vfp_negd(tcg_op, tcg_op);
13220                 /* fall through */
13221             case 0x01: /* FMLA */
13222                 read_vec_element(s, tcg_res, rd, pass, MO_64);
13223                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13224                 break;
13225             case 0x09: /* FMUL */
13226                 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13227                 break;
13228             case 0x19: /* FMULX */
13229                 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13230                 break;
13231             default:
13232                 g_assert_not_reached();
13233             }
13234 
13235             write_vec_element(s, tcg_res, rd, pass, MO_64);
13236         }
13237 
13238         clear_vec_high(s, !is_scalar, rd);
13239     } else if (!is_long) {
13240         /* 32 bit floating point, or 16 or 32 bit integer.
13241          * For the 16 bit scalar case we use the usual Neon helpers and
13242          * rely on the fact that 0 op 0 == 0 with no side effects.
13243          */
13244         TCGv_i32 tcg_idx = tcg_temp_new_i32();
13245         int pass, maxpasses;
13246 
13247         if (is_scalar) {
13248             maxpasses = 1;
13249         } else {
13250             maxpasses = is_q ? 4 : 2;
13251         }
13252 
13253         read_vec_element_i32(s, tcg_idx, rm, index, size);
13254 
13255         if (size == 1 && !is_scalar) {
13256             /* The simplest way to handle the 16x16 indexed ops is to duplicate
13257              * the index into both halves of the 32 bit tcg_idx and then use
13258              * the usual Neon helpers.
13259              */
13260             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13261         }
13262 
13263         for (pass = 0; pass < maxpasses; pass++) {
13264             TCGv_i32 tcg_op = tcg_temp_new_i32();
13265             TCGv_i32 tcg_res = tcg_temp_new_i32();
13266 
13267             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13268 
13269             switch (16 * u + opcode) {
13270             case 0x08: /* MUL */
13271             case 0x10: /* MLA */
13272             case 0x14: /* MLS */
13273             {
13274                 static NeonGenTwoOpFn * const fns[2][2] = {
13275                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13276                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
13277                 };
13278                 NeonGenTwoOpFn *genfn;
13279                 bool is_sub = opcode == 0x4;
13280 
13281                 if (size == 1) {
13282                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13283                 } else {
13284                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13285                 }
13286                 if (opcode == 0x8) {
13287                     break;
13288                 }
13289                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13290                 genfn = fns[size - 1][is_sub];
13291                 genfn(tcg_res, tcg_op, tcg_res);
13292                 break;
13293             }
13294             case 0x05: /* FMLS */
13295             case 0x01: /* FMLA */
13296                 read_vec_element_i32(s, tcg_res, rd, pass,
13297                                      is_scalar ? size : MO_32);
13298                 switch (size) {
13299                 case 1:
13300                     if (opcode == 0x5) {
13301                         /* As usual for ARM, separate negation for fused
13302                          * multiply-add */
13303                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13304                     }
13305                     if (is_scalar) {
13306                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13307                                                    tcg_res, fpst);
13308                     } else {
13309                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13310                                                     tcg_res, fpst);
13311                     }
13312                     break;
13313                 case 2:
13314                     if (opcode == 0x5) {
13315                         /* As usual for ARM, separate negation for
13316                          * fused multiply-add */
13317                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13318                     }
13319                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13320                                            tcg_res, fpst);
13321                     break;
13322                 default:
13323                     g_assert_not_reached();
13324                 }
13325                 break;
13326             case 0x09: /* FMUL */
13327                 switch (size) {
13328                 case 1:
13329                     if (is_scalar) {
13330                         gen_helper_advsimd_mulh(tcg_res, tcg_op,
13331                                                 tcg_idx, fpst);
13332                     } else {
13333                         gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13334                                                  tcg_idx, fpst);
13335                     }
13336                     break;
13337                 case 2:
13338                     gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13339                     break;
13340                 default:
13341                     g_assert_not_reached();
13342                 }
13343                 break;
13344             case 0x19: /* FMULX */
13345                 switch (size) {
13346                 case 1:
13347                     if (is_scalar) {
13348                         gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13349                                                  tcg_idx, fpst);
13350                     } else {
13351                         gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13352                                                   tcg_idx, fpst);
13353                     }
13354                     break;
13355                 case 2:
13356                     gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13357                     break;
13358                 default:
13359                     g_assert_not_reached();
13360                 }
13361                 break;
13362             case 0x0c: /* SQDMULH */
13363                 if (size == 1) {
13364                     gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13365                                                tcg_op, tcg_idx);
13366                 } else {
13367                     gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13368                                                tcg_op, tcg_idx);
13369                 }
13370                 break;
13371             case 0x0d: /* SQRDMULH */
13372                 if (size == 1) {
13373                     gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13374                                                 tcg_op, tcg_idx);
13375                 } else {
13376                     gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13377                                                 tcg_op, tcg_idx);
13378                 }
13379                 break;
13380             case 0x1d: /* SQRDMLAH */
13381                 read_vec_element_i32(s, tcg_res, rd, pass,
13382                                      is_scalar ? size : MO_32);
13383                 if (size == 1) {
13384                     gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13385                                                 tcg_op, tcg_idx, tcg_res);
13386                 } else {
13387                     gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13388                                                 tcg_op, tcg_idx, tcg_res);
13389                 }
13390                 break;
13391             case 0x1f: /* SQRDMLSH */
13392                 read_vec_element_i32(s, tcg_res, rd, pass,
13393                                      is_scalar ? size : MO_32);
13394                 if (size == 1) {
13395                     gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13396                                                 tcg_op, tcg_idx, tcg_res);
13397                 } else {
13398                     gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13399                                                 tcg_op, tcg_idx, tcg_res);
13400                 }
13401                 break;
13402             default:
13403                 g_assert_not_reached();
13404             }
13405 
13406             if (is_scalar) {
13407                 write_fp_sreg(s, rd, tcg_res);
13408             } else {
13409                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13410             }
13411         }
13412 
13413         clear_vec_high(s, is_q, rd);
13414     } else {
13415         /* long ops: 16x16->32 or 32x32->64 */
13416         TCGv_i64 tcg_res[2];
13417         int pass;
13418         bool satop = extract32(opcode, 0, 1);
13419         MemOp memop = MO_32;
13420 
13421         if (satop || !u) {
13422             memop |= MO_SIGN;
13423         }
13424 
13425         if (size == 2) {
13426             TCGv_i64 tcg_idx = tcg_temp_new_i64();
13427 
13428             read_vec_element(s, tcg_idx, rm, index, memop);
13429 
13430             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13431                 TCGv_i64 tcg_op = tcg_temp_new_i64();
13432                 TCGv_i64 tcg_passres;
13433                 int passelt;
13434 
13435                 if (is_scalar) {
13436                     passelt = 0;
13437                 } else {
13438                     passelt = pass + (is_q * 2);
13439                 }
13440 
13441                 read_vec_element(s, tcg_op, rn, passelt, memop);
13442 
13443                 tcg_res[pass] = tcg_temp_new_i64();
13444 
13445                 if (opcode == 0xa || opcode == 0xb) {
13446                     /* Non-accumulating ops */
13447                     tcg_passres = tcg_res[pass];
13448                 } else {
13449                     tcg_passres = tcg_temp_new_i64();
13450                 }
13451 
13452                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13453 
13454                 if (satop) {
13455                     /* saturating, doubling */
13456                     gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13457                                                       tcg_passres, tcg_passres);
13458                 }
13459 
13460                 if (opcode == 0xa || opcode == 0xb) {
13461                     continue;
13462                 }
13463 
13464                 /* Accumulating op: handle accumulate step */
13465                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13466 
13467                 switch (opcode) {
13468                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13469                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13470                     break;
13471                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13472                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13473                     break;
13474                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13475                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
13476                     /* fall through */
13477                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13478                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13479                                                       tcg_res[pass],
13480                                                       tcg_passres);
13481                     break;
13482                 default:
13483                     g_assert_not_reached();
13484                 }
13485             }
13486 
13487             clear_vec_high(s, !is_scalar, rd);
13488         } else {
13489             TCGv_i32 tcg_idx = tcg_temp_new_i32();
13490 
13491             assert(size == 1);
13492             read_vec_element_i32(s, tcg_idx, rm, index, size);
13493 
13494             if (!is_scalar) {
13495                 /* The simplest way to handle the 16x16 indexed ops is to
13496                  * duplicate the index into both halves of the 32 bit tcg_idx
13497                  * and then use the usual Neon helpers.
13498                  */
13499                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13500             }
13501 
13502             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13503                 TCGv_i32 tcg_op = tcg_temp_new_i32();
13504                 TCGv_i64 tcg_passres;
13505 
13506                 if (is_scalar) {
13507                     read_vec_element_i32(s, tcg_op, rn, pass, size);
13508                 } else {
13509                     read_vec_element_i32(s, tcg_op, rn,
13510                                          pass + (is_q * 2), MO_32);
13511                 }
13512 
13513                 tcg_res[pass] = tcg_temp_new_i64();
13514 
13515                 if (opcode == 0xa || opcode == 0xb) {
13516                     /* Non-accumulating ops */
13517                     tcg_passres = tcg_res[pass];
13518                 } else {
13519                     tcg_passres = tcg_temp_new_i64();
13520                 }
13521 
13522                 if (memop & MO_SIGN) {
13523                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13524                 } else {
13525                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13526                 }
13527                 if (satop) {
13528                     gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13529                                                       tcg_passres, tcg_passres);
13530                 }
13531 
13532                 if (opcode == 0xa || opcode == 0xb) {
13533                     continue;
13534                 }
13535 
13536                 /* Accumulating op: handle accumulate step */
13537                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13538 
13539                 switch (opcode) {
13540                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13541                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13542                                              tcg_passres);
13543                     break;
13544                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13545                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13546                                              tcg_passres);
13547                     break;
13548                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13549                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13550                     /* fall through */
13551                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13552                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13553                                                       tcg_res[pass],
13554                                                       tcg_passres);
13555                     break;
13556                 default:
13557                     g_assert_not_reached();
13558                 }
13559             }
13560 
13561             if (is_scalar) {
13562                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13563             }
13564         }
13565 
13566         if (is_scalar) {
13567             tcg_res[1] = tcg_constant_i64(0);
13568         }
13569 
13570         for (pass = 0; pass < 2; pass++) {
13571             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13572         }
13573     }
13574 }
13575 
13576 /* Crypto AES
13577  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13578  * +-----------------+------+-----------+--------+-----+------+------+
13579  * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13580  * +-----------------+------+-----------+--------+-----+------+------+
13581  */
13582 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13583 {
13584     int size = extract32(insn, 22, 2);
13585     int opcode = extract32(insn, 12, 5);
13586     int rn = extract32(insn, 5, 5);
13587     int rd = extract32(insn, 0, 5);
13588     int decrypt;
13589     gen_helper_gvec_2 *genfn2 = NULL;
13590     gen_helper_gvec_3 *genfn3 = NULL;
13591 
13592     if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13593         unallocated_encoding(s);
13594         return;
13595     }
13596 
13597     switch (opcode) {
13598     case 0x4: /* AESE */
13599         decrypt = 0;
13600         genfn3 = gen_helper_crypto_aese;
13601         break;
13602     case 0x6: /* AESMC */
13603         decrypt = 0;
13604         genfn2 = gen_helper_crypto_aesmc;
13605         break;
13606     case 0x5: /* AESD */
13607         decrypt = 1;
13608         genfn3 = gen_helper_crypto_aese;
13609         break;
13610     case 0x7: /* AESIMC */
13611         decrypt = 1;
13612         genfn2 = gen_helper_crypto_aesmc;
13613         break;
13614     default:
13615         unallocated_encoding(s);
13616         return;
13617     }
13618 
13619     if (!fp_access_check(s)) {
13620         return;
13621     }
13622     if (genfn2) {
13623         gen_gvec_op2_ool(s, true, rd, rn, decrypt, genfn2);
13624     } else {
13625         gen_gvec_op3_ool(s, true, rd, rd, rn, decrypt, genfn3);
13626     }
13627 }
13628 
13629 /* Crypto three-reg SHA
13630  *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
13631  * +-----------------+------+---+------+---+--------+-----+------+------+
13632  * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
13633  * +-----------------+------+---+------+---+--------+-----+------+------+
13634  */
13635 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13636 {
13637     int size = extract32(insn, 22, 2);
13638     int opcode = extract32(insn, 12, 3);
13639     int rm = extract32(insn, 16, 5);
13640     int rn = extract32(insn, 5, 5);
13641     int rd = extract32(insn, 0, 5);
13642     gen_helper_gvec_3 *genfn;
13643     bool feature;
13644 
13645     if (size != 0) {
13646         unallocated_encoding(s);
13647         return;
13648     }
13649 
13650     switch (opcode) {
13651     case 0: /* SHA1C */
13652         genfn = gen_helper_crypto_sha1c;
13653         feature = dc_isar_feature(aa64_sha1, s);
13654         break;
13655     case 1: /* SHA1P */
13656         genfn = gen_helper_crypto_sha1p;
13657         feature = dc_isar_feature(aa64_sha1, s);
13658         break;
13659     case 2: /* SHA1M */
13660         genfn = gen_helper_crypto_sha1m;
13661         feature = dc_isar_feature(aa64_sha1, s);
13662         break;
13663     case 3: /* SHA1SU0 */
13664         genfn = gen_helper_crypto_sha1su0;
13665         feature = dc_isar_feature(aa64_sha1, s);
13666         break;
13667     case 4: /* SHA256H */
13668         genfn = gen_helper_crypto_sha256h;
13669         feature = dc_isar_feature(aa64_sha256, s);
13670         break;
13671     case 5: /* SHA256H2 */
13672         genfn = gen_helper_crypto_sha256h2;
13673         feature = dc_isar_feature(aa64_sha256, s);
13674         break;
13675     case 6: /* SHA256SU1 */
13676         genfn = gen_helper_crypto_sha256su1;
13677         feature = dc_isar_feature(aa64_sha256, s);
13678         break;
13679     default:
13680         unallocated_encoding(s);
13681         return;
13682     }
13683 
13684     if (!feature) {
13685         unallocated_encoding(s);
13686         return;
13687     }
13688 
13689     if (!fp_access_check(s)) {
13690         return;
13691     }
13692     gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
13693 }
13694 
13695 /* Crypto two-reg SHA
13696  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13697  * +-----------------+------+-----------+--------+-----+------+------+
13698  * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13699  * +-----------------+------+-----------+--------+-----+------+------+
13700  */
13701 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13702 {
13703     int size = extract32(insn, 22, 2);
13704     int opcode = extract32(insn, 12, 5);
13705     int rn = extract32(insn, 5, 5);
13706     int rd = extract32(insn, 0, 5);
13707     gen_helper_gvec_2 *genfn;
13708     bool feature;
13709 
13710     if (size != 0) {
13711         unallocated_encoding(s);
13712         return;
13713     }
13714 
13715     switch (opcode) {
13716     case 0: /* SHA1H */
13717         feature = dc_isar_feature(aa64_sha1, s);
13718         genfn = gen_helper_crypto_sha1h;
13719         break;
13720     case 1: /* SHA1SU1 */
13721         feature = dc_isar_feature(aa64_sha1, s);
13722         genfn = gen_helper_crypto_sha1su1;
13723         break;
13724     case 2: /* SHA256SU0 */
13725         feature = dc_isar_feature(aa64_sha256, s);
13726         genfn = gen_helper_crypto_sha256su0;
13727         break;
13728     default:
13729         unallocated_encoding(s);
13730         return;
13731     }
13732 
13733     if (!feature) {
13734         unallocated_encoding(s);
13735         return;
13736     }
13737 
13738     if (!fp_access_check(s)) {
13739         return;
13740     }
13741     gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
13742 }
13743 
13744 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
13745 {
13746     tcg_gen_rotli_i64(d, m, 1);
13747     tcg_gen_xor_i64(d, d, n);
13748 }
13749 
13750 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
13751 {
13752     tcg_gen_rotli_vec(vece, d, m, 1);
13753     tcg_gen_xor_vec(vece, d, d, n);
13754 }
13755 
13756 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
13757                    uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
13758 {
13759     static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
13760     static const GVecGen3 op = {
13761         .fni8 = gen_rax1_i64,
13762         .fniv = gen_rax1_vec,
13763         .opt_opc = vecop_list,
13764         .fno = gen_helper_crypto_rax1,
13765         .vece = MO_64,
13766     };
13767     tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
13768 }
13769 
13770 /* Crypto three-reg SHA512
13771  *  31                   21 20  16 15  14  13 12  11  10  9    5 4    0
13772  * +-----------------------+------+---+---+-----+--------+------+------+
13773  * | 1 1 0 0 1 1 1 0 0 1 1 |  Rm  | 1 | O | 0 0 | opcode |  Rn  |  Rd  |
13774  * +-----------------------+------+---+---+-----+--------+------+------+
13775  */
13776 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13777 {
13778     int opcode = extract32(insn, 10, 2);
13779     int o =  extract32(insn, 14, 1);
13780     int rm = extract32(insn, 16, 5);
13781     int rn = extract32(insn, 5, 5);
13782     int rd = extract32(insn, 0, 5);
13783     bool feature;
13784     gen_helper_gvec_3 *oolfn = NULL;
13785     GVecGen3Fn *gvecfn = NULL;
13786 
13787     if (o == 0) {
13788         switch (opcode) {
13789         case 0: /* SHA512H */
13790             feature = dc_isar_feature(aa64_sha512, s);
13791             oolfn = gen_helper_crypto_sha512h;
13792             break;
13793         case 1: /* SHA512H2 */
13794             feature = dc_isar_feature(aa64_sha512, s);
13795             oolfn = gen_helper_crypto_sha512h2;
13796             break;
13797         case 2: /* SHA512SU1 */
13798             feature = dc_isar_feature(aa64_sha512, s);
13799             oolfn = gen_helper_crypto_sha512su1;
13800             break;
13801         case 3: /* RAX1 */
13802             feature = dc_isar_feature(aa64_sha3, s);
13803             gvecfn = gen_gvec_rax1;
13804             break;
13805         default:
13806             g_assert_not_reached();
13807         }
13808     } else {
13809         switch (opcode) {
13810         case 0: /* SM3PARTW1 */
13811             feature = dc_isar_feature(aa64_sm3, s);
13812             oolfn = gen_helper_crypto_sm3partw1;
13813             break;
13814         case 1: /* SM3PARTW2 */
13815             feature = dc_isar_feature(aa64_sm3, s);
13816             oolfn = gen_helper_crypto_sm3partw2;
13817             break;
13818         case 2: /* SM4EKEY */
13819             feature = dc_isar_feature(aa64_sm4, s);
13820             oolfn = gen_helper_crypto_sm4ekey;
13821             break;
13822         default:
13823             unallocated_encoding(s);
13824             return;
13825         }
13826     }
13827 
13828     if (!feature) {
13829         unallocated_encoding(s);
13830         return;
13831     }
13832 
13833     if (!fp_access_check(s)) {
13834         return;
13835     }
13836 
13837     if (oolfn) {
13838         gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
13839     } else {
13840         gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
13841     }
13842 }
13843 
13844 /* Crypto two-reg SHA512
13845  *  31                                     12  11  10  9    5 4    0
13846  * +-----------------------------------------+--------+------+------+
13847  * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode |  Rn  |  Rd  |
13848  * +-----------------------------------------+--------+------+------+
13849  */
13850 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13851 {
13852     int opcode = extract32(insn, 10, 2);
13853     int rn = extract32(insn, 5, 5);
13854     int rd = extract32(insn, 0, 5);
13855     bool feature;
13856 
13857     switch (opcode) {
13858     case 0: /* SHA512SU0 */
13859         feature = dc_isar_feature(aa64_sha512, s);
13860         break;
13861     case 1: /* SM4E */
13862         feature = dc_isar_feature(aa64_sm4, 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     switch (opcode) {
13879     case 0: /* SHA512SU0 */
13880         gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
13881         break;
13882     case 1: /* SM4E */
13883         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
13884         break;
13885     default:
13886         g_assert_not_reached();
13887     }
13888 }
13889 
13890 /* Crypto four-register
13891  *  31               23 22 21 20  16 15  14  10 9    5 4    0
13892  * +-------------------+-----+------+---+------+------+------+
13893  * | 1 1 0 0 1 1 1 0 0 | Op0 |  Rm  | 0 |  Ra  |  Rn  |  Rd  |
13894  * +-------------------+-----+------+---+------+------+------+
13895  */
13896 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13897 {
13898     int op0 = extract32(insn, 21, 2);
13899     int rm = extract32(insn, 16, 5);
13900     int ra = extract32(insn, 10, 5);
13901     int rn = extract32(insn, 5, 5);
13902     int rd = extract32(insn, 0, 5);
13903     bool feature;
13904 
13905     switch (op0) {
13906     case 0: /* EOR3 */
13907     case 1: /* BCAX */
13908         feature = dc_isar_feature(aa64_sha3, s);
13909         break;
13910     case 2: /* SM3SS1 */
13911         feature = dc_isar_feature(aa64_sm3, s);
13912         break;
13913     default:
13914         unallocated_encoding(s);
13915         return;
13916     }
13917 
13918     if (!feature) {
13919         unallocated_encoding(s);
13920         return;
13921     }
13922 
13923     if (!fp_access_check(s)) {
13924         return;
13925     }
13926 
13927     if (op0 < 2) {
13928         TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13929         int pass;
13930 
13931         tcg_op1 = tcg_temp_new_i64();
13932         tcg_op2 = tcg_temp_new_i64();
13933         tcg_op3 = tcg_temp_new_i64();
13934         tcg_res[0] = tcg_temp_new_i64();
13935         tcg_res[1] = tcg_temp_new_i64();
13936 
13937         for (pass = 0; pass < 2; pass++) {
13938             read_vec_element(s, tcg_op1, rn, pass, MO_64);
13939             read_vec_element(s, tcg_op2, rm, pass, MO_64);
13940             read_vec_element(s, tcg_op3, ra, pass, MO_64);
13941 
13942             if (op0 == 0) {
13943                 /* EOR3 */
13944                 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13945             } else {
13946                 /* BCAX */
13947                 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13948             }
13949             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13950         }
13951         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13952         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13953     } else {
13954         TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13955 
13956         tcg_op1 = tcg_temp_new_i32();
13957         tcg_op2 = tcg_temp_new_i32();
13958         tcg_op3 = tcg_temp_new_i32();
13959         tcg_res = tcg_temp_new_i32();
13960         tcg_zero = tcg_constant_i32(0);
13961 
13962         read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13963         read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13964         read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13965 
13966         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13967         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13968         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13969         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13970 
13971         write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13972         write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13973         write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13974         write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13975     }
13976 }
13977 
13978 /* Crypto XAR
13979  *  31                   21 20  16 15    10 9    5 4    0
13980  * +-----------------------+------+--------+------+------+
13981  * | 1 1 0 0 1 1 1 0 1 0 0 |  Rm  |  imm6  |  Rn  |  Rd  |
13982  * +-----------------------+------+--------+------+------+
13983  */
13984 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
13985 {
13986     int rm = extract32(insn, 16, 5);
13987     int imm6 = extract32(insn, 10, 6);
13988     int rn = extract32(insn, 5, 5);
13989     int rd = extract32(insn, 0, 5);
13990 
13991     if (!dc_isar_feature(aa64_sha3, s)) {
13992         unallocated_encoding(s);
13993         return;
13994     }
13995 
13996     if (!fp_access_check(s)) {
13997         return;
13998     }
13999 
14000     gen_gvec_xar(MO_64, vec_full_reg_offset(s, rd),
14001                  vec_full_reg_offset(s, rn),
14002                  vec_full_reg_offset(s, rm), imm6, 16,
14003                  vec_full_reg_size(s));
14004 }
14005 
14006 /* Crypto three-reg imm2
14007  *  31                   21 20  16 15  14 13 12  11  10  9    5 4    0
14008  * +-----------------------+------+-----+------+--------+------+------+
14009  * | 1 1 0 0 1 1 1 0 0 1 0 |  Rm  | 1 0 | imm2 | opcode |  Rn  |  Rd  |
14010  * +-----------------------+------+-----+------+--------+------+------+
14011  */
14012 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
14013 {
14014     static gen_helper_gvec_3 * const fns[4] = {
14015         gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
14016         gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
14017     };
14018     int opcode = extract32(insn, 10, 2);
14019     int imm2 = extract32(insn, 12, 2);
14020     int rm = extract32(insn, 16, 5);
14021     int rn = extract32(insn, 5, 5);
14022     int rd = extract32(insn, 0, 5);
14023 
14024     if (!dc_isar_feature(aa64_sm3, s)) {
14025         unallocated_encoding(s);
14026         return;
14027     }
14028 
14029     if (!fp_access_check(s)) {
14030         return;
14031     }
14032 
14033     gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
14034 }
14035 
14036 /* C3.6 Data processing - SIMD, inc Crypto
14037  *
14038  * As the decode gets a little complex we are using a table based
14039  * approach for this part of the decode.
14040  */
14041 static const AArch64DecodeTable data_proc_simd[] = {
14042     /* pattern  ,  mask     ,  fn                        */
14043     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
14044     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
14045     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
14046     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
14047     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
14048     { 0x0e000400, 0x9fe08400, disas_simd_copy },
14049     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
14050     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
14051     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
14052     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
14053     { 0x0e000000, 0xbf208c00, disas_simd_tb },
14054     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
14055     { 0x2e000000, 0xbf208400, disas_simd_ext },
14056     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
14057     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
14058     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
14059     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
14060     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
14061     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
14062     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
14063     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
14064     { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
14065     { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
14066     { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
14067     { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
14068     { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
14069     { 0xce000000, 0xff808000, disas_crypto_four_reg },
14070     { 0xce800000, 0xffe00000, disas_crypto_xar },
14071     { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
14072     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
14073     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
14074     { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
14075     { 0x00000000, 0x00000000, NULL }
14076 };
14077 
14078 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
14079 {
14080     /* Note that this is called with all non-FP cases from
14081      * table C3-6 so it must UNDEF for entries not specifically
14082      * allocated to instructions in that table.
14083      */
14084     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
14085     if (fn) {
14086         fn(s, insn);
14087     } else {
14088         unallocated_encoding(s);
14089     }
14090 }
14091 
14092 /* C3.6 Data processing - SIMD and floating point */
14093 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
14094 {
14095     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
14096         disas_data_proc_fp(s, insn);
14097     } else {
14098         /* SIMD, including crypto */
14099         disas_data_proc_simd(s, insn);
14100     }
14101 }
14102 
14103 /*
14104  * Include the generated SME FA64 decoder.
14105  */
14106 
14107 #include "decode-sme-fa64.c.inc"
14108 
14109 static bool trans_OK(DisasContext *s, arg_OK *a)
14110 {
14111     return true;
14112 }
14113 
14114 static bool trans_FAIL(DisasContext *s, arg_OK *a)
14115 {
14116     s->is_nonstreaming = true;
14117     return true;
14118 }
14119 
14120 /**
14121  * is_guarded_page:
14122  * @env: The cpu environment
14123  * @s: The DisasContext
14124  *
14125  * Return true if the page is guarded.
14126  */
14127 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
14128 {
14129     uint64_t addr = s->base.pc_first;
14130 #ifdef CONFIG_USER_ONLY
14131     return page_get_flags(addr) & PAGE_BTI;
14132 #else
14133     CPUTLBEntryFull *full;
14134     void *host;
14135     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
14136     int flags;
14137 
14138     /*
14139      * We test this immediately after reading an insn, which means
14140      * that the TLB entry must be present and valid, and thus this
14141      * access will never raise an exception.
14142      */
14143     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
14144                               false, &host, &full, 0);
14145     assert(!(flags & TLB_INVALID_MASK));
14146 
14147     return full->guarded;
14148 #endif
14149 }
14150 
14151 /**
14152  * btype_destination_ok:
14153  * @insn: The instruction at the branch destination
14154  * @bt: SCTLR_ELx.BT
14155  * @btype: PSTATE.BTYPE, and is non-zero
14156  *
14157  * On a guarded page, there are a limited number of insns
14158  * that may be present at the branch target:
14159  *   - branch target identifiers,
14160  *   - paciasp, pacibsp,
14161  *   - BRK insn
14162  *   - HLT insn
14163  * Anything else causes a Branch Target Exception.
14164  *
14165  * Return true if the branch is compatible, false to raise BTITRAP.
14166  */
14167 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
14168 {
14169     if ((insn & 0xfffff01fu) == 0xd503201fu) {
14170         /* HINT space */
14171         switch (extract32(insn, 5, 7)) {
14172         case 0b011001: /* PACIASP */
14173         case 0b011011: /* PACIBSP */
14174             /*
14175              * If SCTLR_ELx.BT, then PACI*SP are not compatible
14176              * with btype == 3.  Otherwise all btype are ok.
14177              */
14178             return !bt || btype != 3;
14179         case 0b100000: /* BTI */
14180             /* Not compatible with any btype.  */
14181             return false;
14182         case 0b100010: /* BTI c */
14183             /* Not compatible with btype == 3 */
14184             return btype != 3;
14185         case 0b100100: /* BTI j */
14186             /* Not compatible with btype == 2 */
14187             return btype != 2;
14188         case 0b100110: /* BTI jc */
14189             /* Compatible with any btype.  */
14190             return true;
14191         }
14192     } else {
14193         switch (insn & 0xffe0001fu) {
14194         case 0xd4200000u: /* BRK */
14195         case 0xd4400000u: /* HLT */
14196             /* Give priority to the breakpoint exception.  */
14197             return true;
14198         }
14199     }
14200     return false;
14201 }
14202 
14203 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14204                                           CPUState *cpu)
14205 {
14206     DisasContext *dc = container_of(dcbase, DisasContext, base);
14207     CPUARMState *env = cpu->env_ptr;
14208     ARMCPU *arm_cpu = env_archcpu(env);
14209     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
14210     int bound, core_mmu_idx;
14211 
14212     dc->isar = &arm_cpu->isar;
14213     dc->condjmp = 0;
14214     dc->pc_save = dc->base.pc_first;
14215     dc->aarch64 = true;
14216     dc->thumb = false;
14217     dc->sctlr_b = 0;
14218     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
14219     dc->condexec_mask = 0;
14220     dc->condexec_cond = 0;
14221     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
14222     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
14223     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
14224     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
14225     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
14226     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14227 #if !defined(CONFIG_USER_ONLY)
14228     dc->user = (dc->current_el == 0);
14229 #endif
14230     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
14231     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
14232     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
14233     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
14234     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
14235     dc->fgt_eret = EX_TBFLAG_A64(tb_flags, FGT_ERET);
14236     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
14237     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
14238     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
14239     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
14240     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
14241     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
14242     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
14243     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
14244     dc->ata = EX_TBFLAG_A64(tb_flags, ATA);
14245     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
14246     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
14247     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
14248     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
14249     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
14250     dc->vec_len = 0;
14251     dc->vec_stride = 0;
14252     dc->cp_regs = arm_cpu->cp_regs;
14253     dc->features = env->features;
14254     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
14255 
14256 #ifdef CONFIG_USER_ONLY
14257     /* In sve_probe_page, we assume TBI is enabled. */
14258     tcg_debug_assert(dc->tbid & 1);
14259 #endif
14260 
14261     /* Single step state. The code-generation logic here is:
14262      *  SS_ACTIVE == 0:
14263      *   generate code with no special handling for single-stepping (except
14264      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14265      *   this happens anyway because those changes are all system register or
14266      *   PSTATE writes).
14267      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14268      *   emit code for one insn
14269      *   emit code to clear PSTATE.SS
14270      *   emit code to generate software step exception for completed step
14271      *   end TB (as usual for having generated an exception)
14272      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14273      *   emit code to generate a software step exception
14274      *   end the TB
14275      */
14276     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
14277     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
14278     dc->is_ldex = false;
14279 
14280     /* Bound the number of insns to execute to those left on the page.  */
14281     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14282 
14283     /* If architectural single step active, limit to 1.  */
14284     if (dc->ss_active) {
14285         bound = 1;
14286     }
14287     dc->base.max_insns = MIN(dc->base.max_insns, bound);
14288 }
14289 
14290 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14291 {
14292 }
14293 
14294 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14295 {
14296     DisasContext *dc = container_of(dcbase, DisasContext, base);
14297     target_ulong pc_arg = dc->base.pc_next;
14298 
14299     if (tb_cflags(dcbase->tb) & CF_PCREL) {
14300         pc_arg &= ~TARGET_PAGE_MASK;
14301     }
14302     tcg_gen_insn_start(pc_arg, 0, 0);
14303     dc->insn_start = tcg_last_op();
14304 }
14305 
14306 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14307 {
14308     DisasContext *s = container_of(dcbase, DisasContext, base);
14309     CPUARMState *env = cpu->env_ptr;
14310     uint64_t pc = s->base.pc_next;
14311     uint32_t insn;
14312 
14313     /* Singlestep exceptions have the highest priority. */
14314     if (s->ss_active && !s->pstate_ss) {
14315         /* Singlestep state is Active-pending.
14316          * If we're in this state at the start of a TB then either
14317          *  a) we just took an exception to an EL which is being debugged
14318          *     and this is the first insn in the exception handler
14319          *  b) debug exceptions were masked and we just unmasked them
14320          *     without changing EL (eg by clearing PSTATE.D)
14321          * In either case we're going to take a swstep exception in the
14322          * "did not step an insn" case, and so the syndrome ISV and EX
14323          * bits should be zero.
14324          */
14325         assert(s->base.num_insns == 1);
14326         gen_swstep_exception(s, 0, 0);
14327         s->base.is_jmp = DISAS_NORETURN;
14328         s->base.pc_next = pc + 4;
14329         return;
14330     }
14331 
14332     if (pc & 3) {
14333         /*
14334          * PC alignment fault.  This has priority over the instruction abort
14335          * that we would receive from a translation fault via arm_ldl_code.
14336          * This should only be possible after an indirect branch, at the
14337          * start of the TB.
14338          */
14339         assert(s->base.num_insns == 1);
14340         gen_helper_exception_pc_alignment(cpu_env, tcg_constant_tl(pc));
14341         s->base.is_jmp = DISAS_NORETURN;
14342         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
14343         return;
14344     }
14345 
14346     s->pc_curr = pc;
14347     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
14348     s->insn = insn;
14349     s->base.pc_next = pc + 4;
14350 
14351     s->fp_access_checked = false;
14352     s->sve_access_checked = false;
14353 
14354     if (s->pstate_il) {
14355         /*
14356          * Illegal execution state. This has priority over BTI
14357          * exceptions, but comes after instruction abort exceptions.
14358          */
14359         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
14360         return;
14361     }
14362 
14363     if (dc_isar_feature(aa64_bti, s)) {
14364         if (s->base.num_insns == 1) {
14365             /*
14366              * At the first insn of the TB, compute s->guarded_page.
14367              * We delayed computing this until successfully reading
14368              * the first insn of the TB, above.  This (mostly) ensures
14369              * that the softmmu tlb entry has been populated, and the
14370              * page table GP bit is available.
14371              *
14372              * Note that we need to compute this even if btype == 0,
14373              * because this value is used for BR instructions later
14374              * where ENV is not available.
14375              */
14376             s->guarded_page = is_guarded_page(env, s);
14377 
14378             /* First insn can have btype set to non-zero.  */
14379             tcg_debug_assert(s->btype >= 0);
14380 
14381             /*
14382              * Note that the Branch Target Exception has fairly high
14383              * priority -- below debugging exceptions but above most
14384              * everything else.  This allows us to handle this now
14385              * instead of waiting until the insn is otherwise decoded.
14386              */
14387             if (s->btype != 0
14388                 && s->guarded_page
14389                 && !btype_destination_ok(insn, s->bt, s->btype)) {
14390                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
14391                 return;
14392             }
14393         } else {
14394             /* Not the first insn: btype must be 0.  */
14395             tcg_debug_assert(s->btype == 0);
14396         }
14397     }
14398 
14399     s->is_nonstreaming = false;
14400     if (s->sme_trap_nonstreaming) {
14401         disas_sme_fa64(s, insn);
14402     }
14403 
14404     switch (extract32(insn, 25, 4)) {
14405     case 0x0:
14406         if (!extract32(insn, 31, 1) || !disas_sme(s, insn)) {
14407             unallocated_encoding(s);
14408         }
14409         break;
14410     case 0x1: case 0x3: /* UNALLOCATED */
14411         unallocated_encoding(s);
14412         break;
14413     case 0x2:
14414         if (!disas_sve(s, insn)) {
14415             unallocated_encoding(s);
14416         }
14417         break;
14418     case 0x8: case 0x9: /* Data processing - immediate */
14419         disas_data_proc_imm(s, insn);
14420         break;
14421     case 0xa: case 0xb: /* Branch, exception generation and system insns */
14422         disas_b_exc_sys(s, insn);
14423         break;
14424     case 0x4:
14425     case 0x6:
14426     case 0xc:
14427     case 0xe:      /* Loads and stores */
14428         disas_ldst(s, insn);
14429         break;
14430     case 0x5:
14431     case 0xd:      /* Data processing - register */
14432         disas_data_proc_reg(s, insn);
14433         break;
14434     case 0x7:
14435     case 0xf:      /* Data processing - SIMD and floating point */
14436         disas_data_proc_simd_fp(s, insn);
14437         break;
14438     default:
14439         assert(FALSE); /* all 15 cases should be handled above */
14440         break;
14441     }
14442 
14443     /*
14444      * After execution of most insns, btype is reset to 0.
14445      * Note that we set btype == -1 when the insn sets btype.
14446      */
14447     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14448         reset_btype(s);
14449     }
14450 }
14451 
14452 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14453 {
14454     DisasContext *dc = container_of(dcbase, DisasContext, base);
14455 
14456     if (unlikely(dc->ss_active)) {
14457         /* Note that this means single stepping WFI doesn't halt the CPU.
14458          * For conditional branch insns this is harmless unreachable code as
14459          * gen_goto_tb() has already handled emitting the debug exception
14460          * (and thus a tb-jump is not possible when singlestepping).
14461          */
14462         switch (dc->base.is_jmp) {
14463         default:
14464             gen_a64_update_pc(dc, 4);
14465             /* fall through */
14466         case DISAS_EXIT:
14467         case DISAS_JUMP:
14468             gen_step_complete_exception(dc);
14469             break;
14470         case DISAS_NORETURN:
14471             break;
14472         }
14473     } else {
14474         switch (dc->base.is_jmp) {
14475         case DISAS_NEXT:
14476         case DISAS_TOO_MANY:
14477             gen_goto_tb(dc, 1, 4);
14478             break;
14479         default:
14480         case DISAS_UPDATE_EXIT:
14481             gen_a64_update_pc(dc, 4);
14482             /* fall through */
14483         case DISAS_EXIT:
14484             tcg_gen_exit_tb(NULL, 0);
14485             break;
14486         case DISAS_UPDATE_NOCHAIN:
14487             gen_a64_update_pc(dc, 4);
14488             /* fall through */
14489         case DISAS_JUMP:
14490             tcg_gen_lookup_and_goto_ptr();
14491             break;
14492         case DISAS_NORETURN:
14493         case DISAS_SWI:
14494             break;
14495         case DISAS_WFE:
14496             gen_a64_update_pc(dc, 4);
14497             gen_helper_wfe(cpu_env);
14498             break;
14499         case DISAS_YIELD:
14500             gen_a64_update_pc(dc, 4);
14501             gen_helper_yield(cpu_env);
14502             break;
14503         case DISAS_WFI:
14504             /*
14505              * This is a special case because we don't want to just halt
14506              * the CPU if trying to debug across a WFI.
14507              */
14508             gen_a64_update_pc(dc, 4);
14509             gen_helper_wfi(cpu_env, tcg_constant_i32(4));
14510             /*
14511              * The helper doesn't necessarily throw an exception, but we
14512              * must go back to the main loop to check for interrupts anyway.
14513              */
14514             tcg_gen_exit_tb(NULL, 0);
14515             break;
14516         }
14517     }
14518 }
14519 
14520 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14521                                  CPUState *cpu, FILE *logfile)
14522 {
14523     DisasContext *dc = container_of(dcbase, DisasContext, base);
14524 
14525     fprintf(logfile, "IN: %s\n", lookup_symbol(dc->base.pc_first));
14526     target_disas(logfile, cpu, dc->base.pc_first, dc->base.tb->size);
14527 }
14528 
14529 const TranslatorOps aarch64_translator_ops = {
14530     .init_disas_context = aarch64_tr_init_disas_context,
14531     .tb_start           = aarch64_tr_tb_start,
14532     .insn_start         = aarch64_tr_insn_start,
14533     .translate_insn     = aarch64_tr_translate_insn,
14534     .tb_stop            = aarch64_tr_tb_stop,
14535     .disas_log          = aarch64_tr_disas_log,
14536 };
14537