xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision d3860a57)
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 = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
6150         fpst = fpstatus_ptr(FPST_FPCR_F16);
6151 
6152         gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6153         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6154 
6155         gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6156         break;
6157     }
6158     case 0xe: /* FRINTX */
6159         fpst = fpstatus_ptr(FPST_FPCR_F16);
6160         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
6161         break;
6162     case 0xf: /* FRINTI */
6163         fpst = fpstatus_ptr(FPST_FPCR_F16);
6164         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6165         break;
6166     default:
6167         g_assert_not_reached();
6168     }
6169 
6170     write_fp_sreg(s, rd, tcg_res);
6171 }
6172 
6173 /* Floating-point data-processing (1 source) - single precision */
6174 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
6175 {
6176     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
6177     TCGv_i32 tcg_op, tcg_res;
6178     TCGv_ptr fpst;
6179     int rmode = -1;
6180 
6181     tcg_op = read_fp_sreg(s, rn);
6182     tcg_res = tcg_temp_new_i32();
6183 
6184     switch (opcode) {
6185     case 0x0: /* FMOV */
6186         tcg_gen_mov_i32(tcg_res, tcg_op);
6187         goto done;
6188     case 0x1: /* FABS */
6189         gen_helper_vfp_abss(tcg_res, tcg_op);
6190         goto done;
6191     case 0x2: /* FNEG */
6192         gen_helper_vfp_negs(tcg_res, tcg_op);
6193         goto done;
6194     case 0x3: /* FSQRT */
6195         gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
6196         goto done;
6197     case 0x6: /* BFCVT */
6198         gen_fpst = gen_helper_bfcvt;
6199         break;
6200     case 0x8: /* FRINTN */
6201     case 0x9: /* FRINTP */
6202     case 0xa: /* FRINTM */
6203     case 0xb: /* FRINTZ */
6204     case 0xc: /* FRINTA */
6205         rmode = arm_rmode_to_sf(opcode & 7);
6206         gen_fpst = gen_helper_rints;
6207         break;
6208     case 0xe: /* FRINTX */
6209         gen_fpst = gen_helper_rints_exact;
6210         break;
6211     case 0xf: /* FRINTI */
6212         gen_fpst = gen_helper_rints;
6213         break;
6214     case 0x10: /* FRINT32Z */
6215         rmode = float_round_to_zero;
6216         gen_fpst = gen_helper_frint32_s;
6217         break;
6218     case 0x11: /* FRINT32X */
6219         gen_fpst = gen_helper_frint32_s;
6220         break;
6221     case 0x12: /* FRINT64Z */
6222         rmode = float_round_to_zero;
6223         gen_fpst = gen_helper_frint64_s;
6224         break;
6225     case 0x13: /* FRINT64X */
6226         gen_fpst = gen_helper_frint64_s;
6227         break;
6228     default:
6229         g_assert_not_reached();
6230     }
6231 
6232     fpst = fpstatus_ptr(FPST_FPCR);
6233     if (rmode >= 0) {
6234         TCGv_i32 tcg_rmode = tcg_const_i32(rmode);
6235         gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6236         gen_fpst(tcg_res, tcg_op, fpst);
6237         gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6238     } else {
6239         gen_fpst(tcg_res, tcg_op, fpst);
6240     }
6241 
6242  done:
6243     write_fp_sreg(s, rd, tcg_res);
6244 }
6245 
6246 /* Floating-point data-processing (1 source) - double precision */
6247 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6248 {
6249     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6250     TCGv_i64 tcg_op, tcg_res;
6251     TCGv_ptr fpst;
6252     int rmode = -1;
6253 
6254     switch (opcode) {
6255     case 0x0: /* FMOV */
6256         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6257         return;
6258     }
6259 
6260     tcg_op = read_fp_dreg(s, rn);
6261     tcg_res = tcg_temp_new_i64();
6262 
6263     switch (opcode) {
6264     case 0x1: /* FABS */
6265         gen_helper_vfp_absd(tcg_res, tcg_op);
6266         goto done;
6267     case 0x2: /* FNEG */
6268         gen_helper_vfp_negd(tcg_res, tcg_op);
6269         goto done;
6270     case 0x3: /* FSQRT */
6271         gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
6272         goto done;
6273     case 0x8: /* FRINTN */
6274     case 0x9: /* FRINTP */
6275     case 0xa: /* FRINTM */
6276     case 0xb: /* FRINTZ */
6277     case 0xc: /* FRINTA */
6278         rmode = arm_rmode_to_sf(opcode & 7);
6279         gen_fpst = gen_helper_rintd;
6280         break;
6281     case 0xe: /* FRINTX */
6282         gen_fpst = gen_helper_rintd_exact;
6283         break;
6284     case 0xf: /* FRINTI */
6285         gen_fpst = gen_helper_rintd;
6286         break;
6287     case 0x10: /* FRINT32Z */
6288         rmode = float_round_to_zero;
6289         gen_fpst = gen_helper_frint32_d;
6290         break;
6291     case 0x11: /* FRINT32X */
6292         gen_fpst = gen_helper_frint32_d;
6293         break;
6294     case 0x12: /* FRINT64Z */
6295         rmode = float_round_to_zero;
6296         gen_fpst = gen_helper_frint64_d;
6297         break;
6298     case 0x13: /* FRINT64X */
6299         gen_fpst = gen_helper_frint64_d;
6300         break;
6301     default:
6302         g_assert_not_reached();
6303     }
6304 
6305     fpst = fpstatus_ptr(FPST_FPCR);
6306     if (rmode >= 0) {
6307         TCGv_i32 tcg_rmode = tcg_const_i32(rmode);
6308         gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6309         gen_fpst(tcg_res, tcg_op, fpst);
6310         gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6311     } else {
6312         gen_fpst(tcg_res, tcg_op, fpst);
6313     }
6314 
6315  done:
6316     write_fp_dreg(s, rd, tcg_res);
6317 }
6318 
6319 static void handle_fp_fcvt(DisasContext *s, int opcode,
6320                            int rd, int rn, int dtype, int ntype)
6321 {
6322     switch (ntype) {
6323     case 0x0:
6324     {
6325         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6326         if (dtype == 1) {
6327             /* Single to double */
6328             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6329             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
6330             write_fp_dreg(s, rd, tcg_rd);
6331         } else {
6332             /* Single to half */
6333             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6334             TCGv_i32 ahp = get_ahp_flag();
6335             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6336 
6337             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6338             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6339             write_fp_sreg(s, rd, tcg_rd);
6340         }
6341         break;
6342     }
6343     case 0x1:
6344     {
6345         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6346         TCGv_i32 tcg_rd = tcg_temp_new_i32();
6347         if (dtype == 0) {
6348             /* Double to single */
6349             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
6350         } else {
6351             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6352             TCGv_i32 ahp = get_ahp_flag();
6353             /* Double to half */
6354             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6355             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6356         }
6357         write_fp_sreg(s, rd, tcg_rd);
6358         break;
6359     }
6360     case 0x3:
6361     {
6362         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6363         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6364         TCGv_i32 tcg_ahp = get_ahp_flag();
6365         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6366         if (dtype == 0) {
6367             /* Half to single */
6368             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6369             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6370             write_fp_sreg(s, rd, tcg_rd);
6371         } else {
6372             /* Half to double */
6373             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6374             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6375             write_fp_dreg(s, rd, tcg_rd);
6376         }
6377         break;
6378     }
6379     default:
6380         g_assert_not_reached();
6381     }
6382 }
6383 
6384 /* Floating point data-processing (1 source)
6385  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
6386  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6387  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
6388  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6389  */
6390 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6391 {
6392     int mos = extract32(insn, 29, 3);
6393     int type = extract32(insn, 22, 2);
6394     int opcode = extract32(insn, 15, 6);
6395     int rn = extract32(insn, 5, 5);
6396     int rd = extract32(insn, 0, 5);
6397 
6398     if (mos) {
6399         goto do_unallocated;
6400     }
6401 
6402     switch (opcode) {
6403     case 0x4: case 0x5: case 0x7:
6404     {
6405         /* FCVT between half, single and double precision */
6406         int dtype = extract32(opcode, 0, 2);
6407         if (type == 2 || dtype == type) {
6408             goto do_unallocated;
6409         }
6410         if (!fp_access_check(s)) {
6411             return;
6412         }
6413 
6414         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6415         break;
6416     }
6417 
6418     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6419         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6420             goto do_unallocated;
6421         }
6422         /* fall through */
6423     case 0x0 ... 0x3:
6424     case 0x8 ... 0xc:
6425     case 0xe ... 0xf:
6426         /* 32-to-32 and 64-to-64 ops */
6427         switch (type) {
6428         case 0:
6429             if (!fp_access_check(s)) {
6430                 return;
6431             }
6432             handle_fp_1src_single(s, opcode, rd, rn);
6433             break;
6434         case 1:
6435             if (!fp_access_check(s)) {
6436                 return;
6437             }
6438             handle_fp_1src_double(s, opcode, rd, rn);
6439             break;
6440         case 3:
6441             if (!dc_isar_feature(aa64_fp16, s)) {
6442                 goto do_unallocated;
6443             }
6444 
6445             if (!fp_access_check(s)) {
6446                 return;
6447             }
6448             handle_fp_1src_half(s, opcode, rd, rn);
6449             break;
6450         default:
6451             goto do_unallocated;
6452         }
6453         break;
6454 
6455     case 0x6:
6456         switch (type) {
6457         case 1: /* BFCVT */
6458             if (!dc_isar_feature(aa64_bf16, s)) {
6459                 goto do_unallocated;
6460             }
6461             if (!fp_access_check(s)) {
6462                 return;
6463             }
6464             handle_fp_1src_single(s, opcode, rd, rn);
6465             break;
6466         default:
6467             goto do_unallocated;
6468         }
6469         break;
6470 
6471     default:
6472     do_unallocated:
6473         unallocated_encoding(s);
6474         break;
6475     }
6476 }
6477 
6478 /* Floating-point data-processing (2 source) - single precision */
6479 static void handle_fp_2src_single(DisasContext *s, int opcode,
6480                                   int rd, int rn, int rm)
6481 {
6482     TCGv_i32 tcg_op1;
6483     TCGv_i32 tcg_op2;
6484     TCGv_i32 tcg_res;
6485     TCGv_ptr fpst;
6486 
6487     tcg_res = tcg_temp_new_i32();
6488     fpst = fpstatus_ptr(FPST_FPCR);
6489     tcg_op1 = read_fp_sreg(s, rn);
6490     tcg_op2 = read_fp_sreg(s, rm);
6491 
6492     switch (opcode) {
6493     case 0x0: /* FMUL */
6494         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6495         break;
6496     case 0x1: /* FDIV */
6497         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6498         break;
6499     case 0x2: /* FADD */
6500         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6501         break;
6502     case 0x3: /* FSUB */
6503         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6504         break;
6505     case 0x4: /* FMAX */
6506         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6507         break;
6508     case 0x5: /* FMIN */
6509         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6510         break;
6511     case 0x6: /* FMAXNM */
6512         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6513         break;
6514     case 0x7: /* FMINNM */
6515         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6516         break;
6517     case 0x8: /* FNMUL */
6518         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6519         gen_helper_vfp_negs(tcg_res, tcg_res);
6520         break;
6521     }
6522 
6523     write_fp_sreg(s, rd, tcg_res);
6524 }
6525 
6526 /* Floating-point data-processing (2 source) - double precision */
6527 static void handle_fp_2src_double(DisasContext *s, int opcode,
6528                                   int rd, int rn, int rm)
6529 {
6530     TCGv_i64 tcg_op1;
6531     TCGv_i64 tcg_op2;
6532     TCGv_i64 tcg_res;
6533     TCGv_ptr fpst;
6534 
6535     tcg_res = tcg_temp_new_i64();
6536     fpst = fpstatus_ptr(FPST_FPCR);
6537     tcg_op1 = read_fp_dreg(s, rn);
6538     tcg_op2 = read_fp_dreg(s, rm);
6539 
6540     switch (opcode) {
6541     case 0x0: /* FMUL */
6542         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6543         break;
6544     case 0x1: /* FDIV */
6545         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6546         break;
6547     case 0x2: /* FADD */
6548         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6549         break;
6550     case 0x3: /* FSUB */
6551         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6552         break;
6553     case 0x4: /* FMAX */
6554         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6555         break;
6556     case 0x5: /* FMIN */
6557         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6558         break;
6559     case 0x6: /* FMAXNM */
6560         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6561         break;
6562     case 0x7: /* FMINNM */
6563         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6564         break;
6565     case 0x8: /* FNMUL */
6566         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6567         gen_helper_vfp_negd(tcg_res, tcg_res);
6568         break;
6569     }
6570 
6571     write_fp_dreg(s, rd, tcg_res);
6572 }
6573 
6574 /* Floating-point data-processing (2 source) - half precision */
6575 static void handle_fp_2src_half(DisasContext *s, int opcode,
6576                                 int rd, int rn, int rm)
6577 {
6578     TCGv_i32 tcg_op1;
6579     TCGv_i32 tcg_op2;
6580     TCGv_i32 tcg_res;
6581     TCGv_ptr fpst;
6582 
6583     tcg_res = tcg_temp_new_i32();
6584     fpst = fpstatus_ptr(FPST_FPCR_F16);
6585     tcg_op1 = read_fp_hreg(s, rn);
6586     tcg_op2 = read_fp_hreg(s, rm);
6587 
6588     switch (opcode) {
6589     case 0x0: /* FMUL */
6590         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6591         break;
6592     case 0x1: /* FDIV */
6593         gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6594         break;
6595     case 0x2: /* FADD */
6596         gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6597         break;
6598     case 0x3: /* FSUB */
6599         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6600         break;
6601     case 0x4: /* FMAX */
6602         gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6603         break;
6604     case 0x5: /* FMIN */
6605         gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6606         break;
6607     case 0x6: /* FMAXNM */
6608         gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6609         break;
6610     case 0x7: /* FMINNM */
6611         gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6612         break;
6613     case 0x8: /* FNMUL */
6614         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6615         tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6616         break;
6617     default:
6618         g_assert_not_reached();
6619     }
6620 
6621     write_fp_sreg(s, rd, tcg_res);
6622 }
6623 
6624 /* Floating point data-processing (2 source)
6625  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
6626  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6627  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
6628  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6629  */
6630 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6631 {
6632     int mos = extract32(insn, 29, 3);
6633     int type = extract32(insn, 22, 2);
6634     int rd = extract32(insn, 0, 5);
6635     int rn = extract32(insn, 5, 5);
6636     int rm = extract32(insn, 16, 5);
6637     int opcode = extract32(insn, 12, 4);
6638 
6639     if (opcode > 8 || mos) {
6640         unallocated_encoding(s);
6641         return;
6642     }
6643 
6644     switch (type) {
6645     case 0:
6646         if (!fp_access_check(s)) {
6647             return;
6648         }
6649         handle_fp_2src_single(s, opcode, rd, rn, rm);
6650         break;
6651     case 1:
6652         if (!fp_access_check(s)) {
6653             return;
6654         }
6655         handle_fp_2src_double(s, opcode, rd, rn, rm);
6656         break;
6657     case 3:
6658         if (!dc_isar_feature(aa64_fp16, s)) {
6659             unallocated_encoding(s);
6660             return;
6661         }
6662         if (!fp_access_check(s)) {
6663             return;
6664         }
6665         handle_fp_2src_half(s, opcode, rd, rn, rm);
6666         break;
6667     default:
6668         unallocated_encoding(s);
6669     }
6670 }
6671 
6672 /* Floating-point data-processing (3 source) - single precision */
6673 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6674                                   int rd, int rn, int rm, int ra)
6675 {
6676     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6677     TCGv_i32 tcg_res = tcg_temp_new_i32();
6678     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6679 
6680     tcg_op1 = read_fp_sreg(s, rn);
6681     tcg_op2 = read_fp_sreg(s, rm);
6682     tcg_op3 = read_fp_sreg(s, ra);
6683 
6684     /* These are fused multiply-add, and must be done as one
6685      * floating point operation with no rounding between the
6686      * multiplication and addition steps.
6687      * NB that doing the negations here as separate steps is
6688      * correct : an input NaN should come out with its sign bit
6689      * flipped if it is a negated-input.
6690      */
6691     if (o1 == true) {
6692         gen_helper_vfp_negs(tcg_op3, tcg_op3);
6693     }
6694 
6695     if (o0 != o1) {
6696         gen_helper_vfp_negs(tcg_op1, tcg_op1);
6697     }
6698 
6699     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6700 
6701     write_fp_sreg(s, rd, tcg_res);
6702 }
6703 
6704 /* Floating-point data-processing (3 source) - double precision */
6705 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6706                                   int rd, int rn, int rm, int ra)
6707 {
6708     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6709     TCGv_i64 tcg_res = tcg_temp_new_i64();
6710     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6711 
6712     tcg_op1 = read_fp_dreg(s, rn);
6713     tcg_op2 = read_fp_dreg(s, rm);
6714     tcg_op3 = read_fp_dreg(s, ra);
6715 
6716     /* These are fused multiply-add, and must be done as one
6717      * floating point operation with no rounding between the
6718      * multiplication and addition steps.
6719      * NB that doing the negations here as separate steps is
6720      * correct : an input NaN should come out with its sign bit
6721      * flipped if it is a negated-input.
6722      */
6723     if (o1 == true) {
6724         gen_helper_vfp_negd(tcg_op3, tcg_op3);
6725     }
6726 
6727     if (o0 != o1) {
6728         gen_helper_vfp_negd(tcg_op1, tcg_op1);
6729     }
6730 
6731     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6732 
6733     write_fp_dreg(s, rd, tcg_res);
6734 }
6735 
6736 /* Floating-point data-processing (3 source) - half precision */
6737 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6738                                 int rd, int rn, int rm, int ra)
6739 {
6740     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6741     TCGv_i32 tcg_res = tcg_temp_new_i32();
6742     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6743 
6744     tcg_op1 = read_fp_hreg(s, rn);
6745     tcg_op2 = read_fp_hreg(s, rm);
6746     tcg_op3 = read_fp_hreg(s, ra);
6747 
6748     /* These are fused multiply-add, and must be done as one
6749      * floating point operation with no rounding between the
6750      * multiplication and addition steps.
6751      * NB that doing the negations here as separate steps is
6752      * correct : an input NaN should come out with its sign bit
6753      * flipped if it is a negated-input.
6754      */
6755     if (o1 == true) {
6756         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6757     }
6758 
6759     if (o0 != o1) {
6760         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6761     }
6762 
6763     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6764 
6765     write_fp_sreg(s, rd, tcg_res);
6766 }
6767 
6768 /* Floating point data-processing (3 source)
6769  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
6770  * +---+---+---+-----------+------+----+------+----+------+------+------+
6771  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6772  * +---+---+---+-----------+------+----+------+----+------+------+------+
6773  */
6774 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6775 {
6776     int mos = extract32(insn, 29, 3);
6777     int type = extract32(insn, 22, 2);
6778     int rd = extract32(insn, 0, 5);
6779     int rn = extract32(insn, 5, 5);
6780     int ra = extract32(insn, 10, 5);
6781     int rm = extract32(insn, 16, 5);
6782     bool o0 = extract32(insn, 15, 1);
6783     bool o1 = extract32(insn, 21, 1);
6784 
6785     if (mos) {
6786         unallocated_encoding(s);
6787         return;
6788     }
6789 
6790     switch (type) {
6791     case 0:
6792         if (!fp_access_check(s)) {
6793             return;
6794         }
6795         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6796         break;
6797     case 1:
6798         if (!fp_access_check(s)) {
6799             return;
6800         }
6801         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6802         break;
6803     case 3:
6804         if (!dc_isar_feature(aa64_fp16, s)) {
6805             unallocated_encoding(s);
6806             return;
6807         }
6808         if (!fp_access_check(s)) {
6809             return;
6810         }
6811         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6812         break;
6813     default:
6814         unallocated_encoding(s);
6815     }
6816 }
6817 
6818 /* Floating point immediate
6819  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
6820  * +---+---+---+-----------+------+---+------------+-------+------+------+
6821  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
6822  * +---+---+---+-----------+------+---+------------+-------+------+------+
6823  */
6824 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6825 {
6826     int rd = extract32(insn, 0, 5);
6827     int imm5 = extract32(insn, 5, 5);
6828     int imm8 = extract32(insn, 13, 8);
6829     int type = extract32(insn, 22, 2);
6830     int mos = extract32(insn, 29, 3);
6831     uint64_t imm;
6832     MemOp sz;
6833 
6834     if (mos || imm5) {
6835         unallocated_encoding(s);
6836         return;
6837     }
6838 
6839     switch (type) {
6840     case 0:
6841         sz = MO_32;
6842         break;
6843     case 1:
6844         sz = MO_64;
6845         break;
6846     case 3:
6847         sz = MO_16;
6848         if (dc_isar_feature(aa64_fp16, s)) {
6849             break;
6850         }
6851         /* fallthru */
6852     default:
6853         unallocated_encoding(s);
6854         return;
6855     }
6856 
6857     if (!fp_access_check(s)) {
6858         return;
6859     }
6860 
6861     imm = vfp_expand_imm(sz, imm8);
6862     write_fp_dreg(s, rd, tcg_constant_i64(imm));
6863 }
6864 
6865 /* Handle floating point <=> fixed point conversions. Note that we can
6866  * also deal with fp <=> integer conversions as a special case (scale == 64)
6867  * OPTME: consider handling that special case specially or at least skipping
6868  * the call to scalbn in the helpers for zero shifts.
6869  */
6870 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6871                            bool itof, int rmode, int scale, int sf, int type)
6872 {
6873     bool is_signed = !(opcode & 1);
6874     TCGv_ptr tcg_fpstatus;
6875     TCGv_i32 tcg_shift, tcg_single;
6876     TCGv_i64 tcg_double;
6877 
6878     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
6879 
6880     tcg_shift = tcg_constant_i32(64 - scale);
6881 
6882     if (itof) {
6883         TCGv_i64 tcg_int = cpu_reg(s, rn);
6884         if (!sf) {
6885             TCGv_i64 tcg_extend = tcg_temp_new_i64();
6886 
6887             if (is_signed) {
6888                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6889             } else {
6890                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6891             }
6892 
6893             tcg_int = tcg_extend;
6894         }
6895 
6896         switch (type) {
6897         case 1: /* float64 */
6898             tcg_double = tcg_temp_new_i64();
6899             if (is_signed) {
6900                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6901                                      tcg_shift, tcg_fpstatus);
6902             } else {
6903                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6904                                      tcg_shift, tcg_fpstatus);
6905             }
6906             write_fp_dreg(s, rd, tcg_double);
6907             break;
6908 
6909         case 0: /* float32 */
6910             tcg_single = tcg_temp_new_i32();
6911             if (is_signed) {
6912                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6913                                      tcg_shift, tcg_fpstatus);
6914             } else {
6915                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6916                                      tcg_shift, tcg_fpstatus);
6917             }
6918             write_fp_sreg(s, rd, tcg_single);
6919             break;
6920 
6921         case 3: /* float16 */
6922             tcg_single = tcg_temp_new_i32();
6923             if (is_signed) {
6924                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6925                                      tcg_shift, tcg_fpstatus);
6926             } else {
6927                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6928                                      tcg_shift, tcg_fpstatus);
6929             }
6930             write_fp_sreg(s, rd, tcg_single);
6931             break;
6932 
6933         default:
6934             g_assert_not_reached();
6935         }
6936     } else {
6937         TCGv_i64 tcg_int = cpu_reg(s, rd);
6938         TCGv_i32 tcg_rmode;
6939 
6940         if (extract32(opcode, 2, 1)) {
6941             /* There are too many rounding modes to all fit into rmode,
6942              * so FCVTA[US] is a special case.
6943              */
6944             rmode = FPROUNDING_TIEAWAY;
6945         }
6946 
6947         tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
6948 
6949         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
6950 
6951         switch (type) {
6952         case 1: /* float64 */
6953             tcg_double = read_fp_dreg(s, rn);
6954             if (is_signed) {
6955                 if (!sf) {
6956                     gen_helper_vfp_tosld(tcg_int, tcg_double,
6957                                          tcg_shift, tcg_fpstatus);
6958                 } else {
6959                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
6960                                          tcg_shift, tcg_fpstatus);
6961                 }
6962             } else {
6963                 if (!sf) {
6964                     gen_helper_vfp_tould(tcg_int, tcg_double,
6965                                          tcg_shift, tcg_fpstatus);
6966                 } else {
6967                     gen_helper_vfp_touqd(tcg_int, tcg_double,
6968                                          tcg_shift, tcg_fpstatus);
6969                 }
6970             }
6971             if (!sf) {
6972                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6973             }
6974             break;
6975 
6976         case 0: /* float32 */
6977             tcg_single = read_fp_sreg(s, rn);
6978             if (sf) {
6979                 if (is_signed) {
6980                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
6981                                          tcg_shift, tcg_fpstatus);
6982                 } else {
6983                     gen_helper_vfp_touqs(tcg_int, tcg_single,
6984                                          tcg_shift, tcg_fpstatus);
6985                 }
6986             } else {
6987                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6988                 if (is_signed) {
6989                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
6990                                          tcg_shift, tcg_fpstatus);
6991                 } else {
6992                     gen_helper_vfp_touls(tcg_dest, tcg_single,
6993                                          tcg_shift, tcg_fpstatus);
6994                 }
6995                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6996             }
6997             break;
6998 
6999         case 3: /* float16 */
7000             tcg_single = read_fp_sreg(s, rn);
7001             if (sf) {
7002                 if (is_signed) {
7003                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
7004                                          tcg_shift, tcg_fpstatus);
7005                 } else {
7006                     gen_helper_vfp_touqh(tcg_int, tcg_single,
7007                                          tcg_shift, tcg_fpstatus);
7008                 }
7009             } else {
7010                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7011                 if (is_signed) {
7012                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
7013                                          tcg_shift, tcg_fpstatus);
7014                 } else {
7015                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
7016                                          tcg_shift, tcg_fpstatus);
7017                 }
7018                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7019             }
7020             break;
7021 
7022         default:
7023             g_assert_not_reached();
7024         }
7025 
7026         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
7027     }
7028 }
7029 
7030 /* Floating point <-> fixed point conversions
7031  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
7032  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7033  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
7034  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7035  */
7036 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
7037 {
7038     int rd = extract32(insn, 0, 5);
7039     int rn = extract32(insn, 5, 5);
7040     int scale = extract32(insn, 10, 6);
7041     int opcode = extract32(insn, 16, 3);
7042     int rmode = extract32(insn, 19, 2);
7043     int type = extract32(insn, 22, 2);
7044     bool sbit = extract32(insn, 29, 1);
7045     bool sf = extract32(insn, 31, 1);
7046     bool itof;
7047 
7048     if (sbit || (!sf && scale < 32)) {
7049         unallocated_encoding(s);
7050         return;
7051     }
7052 
7053     switch (type) {
7054     case 0: /* float32 */
7055     case 1: /* float64 */
7056         break;
7057     case 3: /* float16 */
7058         if (dc_isar_feature(aa64_fp16, s)) {
7059             break;
7060         }
7061         /* fallthru */
7062     default:
7063         unallocated_encoding(s);
7064         return;
7065     }
7066 
7067     switch ((rmode << 3) | opcode) {
7068     case 0x2: /* SCVTF */
7069     case 0x3: /* UCVTF */
7070         itof = true;
7071         break;
7072     case 0x18: /* FCVTZS */
7073     case 0x19: /* FCVTZU */
7074         itof = false;
7075         break;
7076     default:
7077         unallocated_encoding(s);
7078         return;
7079     }
7080 
7081     if (!fp_access_check(s)) {
7082         return;
7083     }
7084 
7085     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
7086 }
7087 
7088 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
7089 {
7090     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
7091      * without conversion.
7092      */
7093 
7094     if (itof) {
7095         TCGv_i64 tcg_rn = cpu_reg(s, rn);
7096         TCGv_i64 tmp;
7097 
7098         switch (type) {
7099         case 0:
7100             /* 32 bit */
7101             tmp = tcg_temp_new_i64();
7102             tcg_gen_ext32u_i64(tmp, tcg_rn);
7103             write_fp_dreg(s, rd, tmp);
7104             break;
7105         case 1:
7106             /* 64 bit */
7107             write_fp_dreg(s, rd, tcg_rn);
7108             break;
7109         case 2:
7110             /* 64 bit to top half. */
7111             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
7112             clear_vec_high(s, true, rd);
7113             break;
7114         case 3:
7115             /* 16 bit */
7116             tmp = tcg_temp_new_i64();
7117             tcg_gen_ext16u_i64(tmp, tcg_rn);
7118             write_fp_dreg(s, rd, tmp);
7119             break;
7120         default:
7121             g_assert_not_reached();
7122         }
7123     } else {
7124         TCGv_i64 tcg_rd = cpu_reg(s, rd);
7125 
7126         switch (type) {
7127         case 0:
7128             /* 32 bit */
7129             tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
7130             break;
7131         case 1:
7132             /* 64 bit */
7133             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
7134             break;
7135         case 2:
7136             /* 64 bits from top half */
7137             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
7138             break;
7139         case 3:
7140             /* 16 bit */
7141             tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
7142             break;
7143         default:
7144             g_assert_not_reached();
7145         }
7146     }
7147 }
7148 
7149 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
7150 {
7151     TCGv_i64 t = read_fp_dreg(s, rn);
7152     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
7153 
7154     gen_helper_fjcvtzs(t, t, fpstatus);
7155 
7156     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
7157     tcg_gen_extrh_i64_i32(cpu_ZF, t);
7158     tcg_gen_movi_i32(cpu_CF, 0);
7159     tcg_gen_movi_i32(cpu_NF, 0);
7160     tcg_gen_movi_i32(cpu_VF, 0);
7161 }
7162 
7163 /* Floating point <-> integer conversions
7164  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
7165  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7166  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
7167  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7168  */
7169 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
7170 {
7171     int rd = extract32(insn, 0, 5);
7172     int rn = extract32(insn, 5, 5);
7173     int opcode = extract32(insn, 16, 3);
7174     int rmode = extract32(insn, 19, 2);
7175     int type = extract32(insn, 22, 2);
7176     bool sbit = extract32(insn, 29, 1);
7177     bool sf = extract32(insn, 31, 1);
7178     bool itof = false;
7179 
7180     if (sbit) {
7181         goto do_unallocated;
7182     }
7183 
7184     switch (opcode) {
7185     case 2: /* SCVTF */
7186     case 3: /* UCVTF */
7187         itof = true;
7188         /* fallthru */
7189     case 4: /* FCVTAS */
7190     case 5: /* FCVTAU */
7191         if (rmode != 0) {
7192             goto do_unallocated;
7193         }
7194         /* fallthru */
7195     case 0: /* FCVT[NPMZ]S */
7196     case 1: /* FCVT[NPMZ]U */
7197         switch (type) {
7198         case 0: /* float32 */
7199         case 1: /* float64 */
7200             break;
7201         case 3: /* float16 */
7202             if (!dc_isar_feature(aa64_fp16, s)) {
7203                 goto do_unallocated;
7204             }
7205             break;
7206         default:
7207             goto do_unallocated;
7208         }
7209         if (!fp_access_check(s)) {
7210             return;
7211         }
7212         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
7213         break;
7214 
7215     default:
7216         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
7217         case 0b01100110: /* FMOV half <-> 32-bit int */
7218         case 0b01100111:
7219         case 0b11100110: /* FMOV half <-> 64-bit int */
7220         case 0b11100111:
7221             if (!dc_isar_feature(aa64_fp16, s)) {
7222                 goto do_unallocated;
7223             }
7224             /* fallthru */
7225         case 0b00000110: /* FMOV 32-bit */
7226         case 0b00000111:
7227         case 0b10100110: /* FMOV 64-bit */
7228         case 0b10100111:
7229         case 0b11001110: /* FMOV top half of 128-bit */
7230         case 0b11001111:
7231             if (!fp_access_check(s)) {
7232                 return;
7233             }
7234             itof = opcode & 1;
7235             handle_fmov(s, rd, rn, type, itof);
7236             break;
7237 
7238         case 0b00111110: /* FJCVTZS */
7239             if (!dc_isar_feature(aa64_jscvt, s)) {
7240                 goto do_unallocated;
7241             } else if (fp_access_check(s)) {
7242                 handle_fjcvtzs(s, rd, rn);
7243             }
7244             break;
7245 
7246         default:
7247         do_unallocated:
7248             unallocated_encoding(s);
7249             return;
7250         }
7251         break;
7252     }
7253 }
7254 
7255 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7256  *   31  30  29 28     25 24                          0
7257  * +---+---+---+---------+-----------------------------+
7258  * |   | 0 |   | 1 1 1 1 |                             |
7259  * +---+---+---+---------+-----------------------------+
7260  */
7261 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7262 {
7263     if (extract32(insn, 24, 1)) {
7264         /* Floating point data-processing (3 source) */
7265         disas_fp_3src(s, insn);
7266     } else if (extract32(insn, 21, 1) == 0) {
7267         /* Floating point to fixed point conversions */
7268         disas_fp_fixed_conv(s, insn);
7269     } else {
7270         switch (extract32(insn, 10, 2)) {
7271         case 1:
7272             /* Floating point conditional compare */
7273             disas_fp_ccomp(s, insn);
7274             break;
7275         case 2:
7276             /* Floating point data-processing (2 source) */
7277             disas_fp_2src(s, insn);
7278             break;
7279         case 3:
7280             /* Floating point conditional select */
7281             disas_fp_csel(s, insn);
7282             break;
7283         case 0:
7284             switch (ctz32(extract32(insn, 12, 4))) {
7285             case 0: /* [15:12] == xxx1 */
7286                 /* Floating point immediate */
7287                 disas_fp_imm(s, insn);
7288                 break;
7289             case 1: /* [15:12] == xx10 */
7290                 /* Floating point compare */
7291                 disas_fp_compare(s, insn);
7292                 break;
7293             case 2: /* [15:12] == x100 */
7294                 /* Floating point data-processing (1 source) */
7295                 disas_fp_1src(s, insn);
7296                 break;
7297             case 3: /* [15:12] == 1000 */
7298                 unallocated_encoding(s);
7299                 break;
7300             default: /* [15:12] == 0000 */
7301                 /* Floating point <-> integer conversions */
7302                 disas_fp_int_conv(s, insn);
7303                 break;
7304             }
7305             break;
7306         }
7307     }
7308 }
7309 
7310 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7311                      int pos)
7312 {
7313     /* Extract 64 bits from the middle of two concatenated 64 bit
7314      * vector register slices left:right. The extracted bits start
7315      * at 'pos' bits into the right (least significant) side.
7316      * We return the result in tcg_right, and guarantee not to
7317      * trash tcg_left.
7318      */
7319     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7320     assert(pos > 0 && pos < 64);
7321 
7322     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7323     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7324     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7325 }
7326 
7327 /* EXT
7328  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
7329  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7330  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
7331  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7332  */
7333 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7334 {
7335     int is_q = extract32(insn, 30, 1);
7336     int op2 = extract32(insn, 22, 2);
7337     int imm4 = extract32(insn, 11, 4);
7338     int rm = extract32(insn, 16, 5);
7339     int rn = extract32(insn, 5, 5);
7340     int rd = extract32(insn, 0, 5);
7341     int pos = imm4 << 3;
7342     TCGv_i64 tcg_resl, tcg_resh;
7343 
7344     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7345         unallocated_encoding(s);
7346         return;
7347     }
7348 
7349     if (!fp_access_check(s)) {
7350         return;
7351     }
7352 
7353     tcg_resh = tcg_temp_new_i64();
7354     tcg_resl = tcg_temp_new_i64();
7355 
7356     /* Vd gets bits starting at pos bits into Vm:Vn. This is
7357      * either extracting 128 bits from a 128:128 concatenation, or
7358      * extracting 64 bits from a 64:64 concatenation.
7359      */
7360     if (!is_q) {
7361         read_vec_element(s, tcg_resl, rn, 0, MO_64);
7362         if (pos != 0) {
7363             read_vec_element(s, tcg_resh, rm, 0, MO_64);
7364             do_ext64(s, tcg_resh, tcg_resl, pos);
7365         }
7366     } else {
7367         TCGv_i64 tcg_hh;
7368         typedef struct {
7369             int reg;
7370             int elt;
7371         } EltPosns;
7372         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7373         EltPosns *elt = eltposns;
7374 
7375         if (pos >= 64) {
7376             elt++;
7377             pos -= 64;
7378         }
7379 
7380         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7381         elt++;
7382         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7383         elt++;
7384         if (pos != 0) {
7385             do_ext64(s, tcg_resh, tcg_resl, pos);
7386             tcg_hh = tcg_temp_new_i64();
7387             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7388             do_ext64(s, tcg_hh, tcg_resh, pos);
7389         }
7390     }
7391 
7392     write_vec_element(s, tcg_resl, rd, 0, MO_64);
7393     if (is_q) {
7394         write_vec_element(s, tcg_resh, rd, 1, MO_64);
7395     }
7396     clear_vec_high(s, is_q, rd);
7397 }
7398 
7399 /* TBL/TBX
7400  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
7401  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7402  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
7403  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7404  */
7405 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7406 {
7407     int op2 = extract32(insn, 22, 2);
7408     int is_q = extract32(insn, 30, 1);
7409     int rm = extract32(insn, 16, 5);
7410     int rn = extract32(insn, 5, 5);
7411     int rd = extract32(insn, 0, 5);
7412     int is_tbx = extract32(insn, 12, 1);
7413     int len = (extract32(insn, 13, 2) + 1) * 16;
7414 
7415     if (op2 != 0) {
7416         unallocated_encoding(s);
7417         return;
7418     }
7419 
7420     if (!fp_access_check(s)) {
7421         return;
7422     }
7423 
7424     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7425                        vec_full_reg_offset(s, rm), cpu_env,
7426                        is_q ? 16 : 8, vec_full_reg_size(s),
7427                        (len << 6) | (is_tbx << 5) | rn,
7428                        gen_helper_simd_tblx);
7429 }
7430 
7431 /* ZIP/UZP/TRN
7432  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
7433  * +---+---+-------------+------+---+------+---+------------------+------+
7434  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
7435  * +---+---+-------------+------+---+------+---+------------------+------+
7436  */
7437 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7438 {
7439     int rd = extract32(insn, 0, 5);
7440     int rn = extract32(insn, 5, 5);
7441     int rm = extract32(insn, 16, 5);
7442     int size = extract32(insn, 22, 2);
7443     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7444      * bit 2 indicates 1 vs 2 variant of the insn.
7445      */
7446     int opcode = extract32(insn, 12, 2);
7447     bool part = extract32(insn, 14, 1);
7448     bool is_q = extract32(insn, 30, 1);
7449     int esize = 8 << size;
7450     int i, ofs;
7451     int datasize = is_q ? 128 : 64;
7452     int elements = datasize / esize;
7453     TCGv_i64 tcg_res, tcg_resl, tcg_resh;
7454 
7455     if (opcode == 0 || (size == 3 && !is_q)) {
7456         unallocated_encoding(s);
7457         return;
7458     }
7459 
7460     if (!fp_access_check(s)) {
7461         return;
7462     }
7463 
7464     tcg_resl = tcg_const_i64(0);
7465     tcg_resh = is_q ? tcg_const_i64(0) : NULL;
7466     tcg_res = tcg_temp_new_i64();
7467 
7468     for (i = 0; i < elements; i++) {
7469         switch (opcode) {
7470         case 1: /* UZP1/2 */
7471         {
7472             int midpoint = elements / 2;
7473             if (i < midpoint) {
7474                 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
7475             } else {
7476                 read_vec_element(s, tcg_res, rm,
7477                                  2 * (i - midpoint) + part, size);
7478             }
7479             break;
7480         }
7481         case 2: /* TRN1/2 */
7482             if (i & 1) {
7483                 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
7484             } else {
7485                 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
7486             }
7487             break;
7488         case 3: /* ZIP1/2 */
7489         {
7490             int base = part * elements / 2;
7491             if (i & 1) {
7492                 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
7493             } else {
7494                 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
7495             }
7496             break;
7497         }
7498         default:
7499             g_assert_not_reached();
7500         }
7501 
7502         ofs = i * esize;
7503         if (ofs < 64) {
7504             tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
7505             tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
7506         } else {
7507             tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
7508             tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
7509         }
7510     }
7511 
7512     write_vec_element(s, tcg_resl, rd, 0, MO_64);
7513     if (is_q) {
7514         write_vec_element(s, tcg_resh, rd, 1, MO_64);
7515     }
7516     clear_vec_high(s, is_q, rd);
7517 }
7518 
7519 /*
7520  * do_reduction_op helper
7521  *
7522  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7523  * important for correct NaN propagation that we do these
7524  * operations in exactly the order specified by the pseudocode.
7525  *
7526  * This is a recursive function, TCG temps should be freed by the
7527  * calling function once it is done with the values.
7528  */
7529 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7530                                 int esize, int size, int vmap, TCGv_ptr fpst)
7531 {
7532     if (esize == size) {
7533         int element;
7534         MemOp msize = esize == 16 ? MO_16 : MO_32;
7535         TCGv_i32 tcg_elem;
7536 
7537         /* We should have one register left here */
7538         assert(ctpop8(vmap) == 1);
7539         element = ctz32(vmap);
7540         assert(element < 8);
7541 
7542         tcg_elem = tcg_temp_new_i32();
7543         read_vec_element_i32(s, tcg_elem, rn, element, msize);
7544         return tcg_elem;
7545     } else {
7546         int bits = size / 2;
7547         int shift = ctpop8(vmap) / 2;
7548         int vmap_lo = (vmap >> shift) & vmap;
7549         int vmap_hi = (vmap & ~vmap_lo);
7550         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7551 
7552         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7553         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7554         tcg_res = tcg_temp_new_i32();
7555 
7556         switch (fpopcode) {
7557         case 0x0c: /* fmaxnmv half-precision */
7558             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7559             break;
7560         case 0x0f: /* fmaxv half-precision */
7561             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7562             break;
7563         case 0x1c: /* fminnmv half-precision */
7564             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7565             break;
7566         case 0x1f: /* fminv half-precision */
7567             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7568             break;
7569         case 0x2c: /* fmaxnmv */
7570             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7571             break;
7572         case 0x2f: /* fmaxv */
7573             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7574             break;
7575         case 0x3c: /* fminnmv */
7576             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7577             break;
7578         case 0x3f: /* fminv */
7579             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7580             break;
7581         default:
7582             g_assert_not_reached();
7583         }
7584         return tcg_res;
7585     }
7586 }
7587 
7588 /* AdvSIMD across lanes
7589  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7590  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7591  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7592  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7593  */
7594 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7595 {
7596     int rd = extract32(insn, 0, 5);
7597     int rn = extract32(insn, 5, 5);
7598     int size = extract32(insn, 22, 2);
7599     int opcode = extract32(insn, 12, 5);
7600     bool is_q = extract32(insn, 30, 1);
7601     bool is_u = extract32(insn, 29, 1);
7602     bool is_fp = false;
7603     bool is_min = false;
7604     int esize;
7605     int elements;
7606     int i;
7607     TCGv_i64 tcg_res, tcg_elt;
7608 
7609     switch (opcode) {
7610     case 0x1b: /* ADDV */
7611         if (is_u) {
7612             unallocated_encoding(s);
7613             return;
7614         }
7615         /* fall through */
7616     case 0x3: /* SADDLV, UADDLV */
7617     case 0xa: /* SMAXV, UMAXV */
7618     case 0x1a: /* SMINV, UMINV */
7619         if (size == 3 || (size == 2 && !is_q)) {
7620             unallocated_encoding(s);
7621             return;
7622         }
7623         break;
7624     case 0xc: /* FMAXNMV, FMINNMV */
7625     case 0xf: /* FMAXV, FMINV */
7626         /* Bit 1 of size field encodes min vs max and the actual size
7627          * depends on the encoding of the U bit. If not set (and FP16
7628          * enabled) then we do half-precision float instead of single
7629          * precision.
7630          */
7631         is_min = extract32(size, 1, 1);
7632         is_fp = true;
7633         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7634             size = 1;
7635         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7636             unallocated_encoding(s);
7637             return;
7638         } else {
7639             size = 2;
7640         }
7641         break;
7642     default:
7643         unallocated_encoding(s);
7644         return;
7645     }
7646 
7647     if (!fp_access_check(s)) {
7648         return;
7649     }
7650 
7651     esize = 8 << size;
7652     elements = (is_q ? 128 : 64) / esize;
7653 
7654     tcg_res = tcg_temp_new_i64();
7655     tcg_elt = tcg_temp_new_i64();
7656 
7657     /* These instructions operate across all lanes of a vector
7658      * to produce a single result. We can guarantee that a 64
7659      * bit intermediate is sufficient:
7660      *  + for [US]ADDLV the maximum element size is 32 bits, and
7661      *    the result type is 64 bits
7662      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7663      *    same as the element size, which is 32 bits at most
7664      * For the integer operations we can choose to work at 64
7665      * or 32 bits and truncate at the end; for simplicity
7666      * we use 64 bits always. The floating point
7667      * ops do require 32 bit intermediates, though.
7668      */
7669     if (!is_fp) {
7670         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7671 
7672         for (i = 1; i < elements; i++) {
7673             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7674 
7675             switch (opcode) {
7676             case 0x03: /* SADDLV / UADDLV */
7677             case 0x1b: /* ADDV */
7678                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7679                 break;
7680             case 0x0a: /* SMAXV / UMAXV */
7681                 if (is_u) {
7682                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7683                 } else {
7684                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7685                 }
7686                 break;
7687             case 0x1a: /* SMINV / UMINV */
7688                 if (is_u) {
7689                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7690                 } else {
7691                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7692                 }
7693                 break;
7694             default:
7695                 g_assert_not_reached();
7696             }
7697 
7698         }
7699     } else {
7700         /* Floating point vector reduction ops which work across 32
7701          * bit (single) or 16 bit (half-precision) intermediates.
7702          * Note that correct NaN propagation requires that we do these
7703          * operations in exactly the order specified by the pseudocode.
7704          */
7705         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7706         int fpopcode = opcode | is_min << 4 | is_u << 5;
7707         int vmap = (1 << elements) - 1;
7708         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7709                                              (is_q ? 128 : 64), vmap, fpst);
7710         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7711     }
7712 
7713     /* Now truncate the result to the width required for the final output */
7714     if (opcode == 0x03) {
7715         /* SADDLV, UADDLV: result is 2*esize */
7716         size++;
7717     }
7718 
7719     switch (size) {
7720     case 0:
7721         tcg_gen_ext8u_i64(tcg_res, tcg_res);
7722         break;
7723     case 1:
7724         tcg_gen_ext16u_i64(tcg_res, tcg_res);
7725         break;
7726     case 2:
7727         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7728         break;
7729     case 3:
7730         break;
7731     default:
7732         g_assert_not_reached();
7733     }
7734 
7735     write_fp_dreg(s, rd, tcg_res);
7736 }
7737 
7738 /* DUP (Element, Vector)
7739  *
7740  *  31  30   29              21 20    16 15        10  9    5 4    0
7741  * +---+---+-------------------+--------+-------------+------+------+
7742  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7743  * +---+---+-------------------+--------+-------------+------+------+
7744  *
7745  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7746  */
7747 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7748                              int imm5)
7749 {
7750     int size = ctz32(imm5);
7751     int index;
7752 
7753     if (size > 3 || (size == 3 && !is_q)) {
7754         unallocated_encoding(s);
7755         return;
7756     }
7757 
7758     if (!fp_access_check(s)) {
7759         return;
7760     }
7761 
7762     index = imm5 >> (size + 1);
7763     tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7764                          vec_reg_offset(s, rn, index, size),
7765                          is_q ? 16 : 8, vec_full_reg_size(s));
7766 }
7767 
7768 /* DUP (element, scalar)
7769  *  31                   21 20    16 15        10  9    5 4    0
7770  * +-----------------------+--------+-------------+------+------+
7771  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7772  * +-----------------------+--------+-------------+------+------+
7773  */
7774 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7775                               int imm5)
7776 {
7777     int size = ctz32(imm5);
7778     int index;
7779     TCGv_i64 tmp;
7780 
7781     if (size > 3) {
7782         unallocated_encoding(s);
7783         return;
7784     }
7785 
7786     if (!fp_access_check(s)) {
7787         return;
7788     }
7789 
7790     index = imm5 >> (size + 1);
7791 
7792     /* This instruction just extracts the specified element and
7793      * zero-extends it into the bottom of the destination register.
7794      */
7795     tmp = tcg_temp_new_i64();
7796     read_vec_element(s, tmp, rn, index, size);
7797     write_fp_dreg(s, rd, tmp);
7798 }
7799 
7800 /* DUP (General)
7801  *
7802  *  31  30   29              21 20    16 15        10  9    5 4    0
7803  * +---+---+-------------------+--------+-------------+------+------+
7804  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
7805  * +---+---+-------------------+--------+-------------+------+------+
7806  *
7807  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7808  */
7809 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7810                              int imm5)
7811 {
7812     int size = ctz32(imm5);
7813     uint32_t dofs, oprsz, maxsz;
7814 
7815     if (size > 3 || ((size == 3) && !is_q)) {
7816         unallocated_encoding(s);
7817         return;
7818     }
7819 
7820     if (!fp_access_check(s)) {
7821         return;
7822     }
7823 
7824     dofs = vec_full_reg_offset(s, rd);
7825     oprsz = is_q ? 16 : 8;
7826     maxsz = vec_full_reg_size(s);
7827 
7828     tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7829 }
7830 
7831 /* INS (Element)
7832  *
7833  *  31                   21 20    16 15  14    11  10 9    5 4    0
7834  * +-----------------------+--------+------------+---+------+------+
7835  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
7836  * +-----------------------+--------+------------+---+------+------+
7837  *
7838  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7839  * index: encoded in imm5<4:size+1>
7840  */
7841 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7842                              int imm4, int imm5)
7843 {
7844     int size = ctz32(imm5);
7845     int src_index, dst_index;
7846     TCGv_i64 tmp;
7847 
7848     if (size > 3) {
7849         unallocated_encoding(s);
7850         return;
7851     }
7852 
7853     if (!fp_access_check(s)) {
7854         return;
7855     }
7856 
7857     dst_index = extract32(imm5, 1+size, 5);
7858     src_index = extract32(imm4, size, 4);
7859 
7860     tmp = tcg_temp_new_i64();
7861 
7862     read_vec_element(s, tmp, rn, src_index, size);
7863     write_vec_element(s, tmp, rd, dst_index, size);
7864 
7865     /* INS is considered a 128-bit write for SVE. */
7866     clear_vec_high(s, true, rd);
7867 }
7868 
7869 
7870 /* INS (General)
7871  *
7872  *  31                   21 20    16 15        10  9    5 4    0
7873  * +-----------------------+--------+-------------+------+------+
7874  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
7875  * +-----------------------+--------+-------------+------+------+
7876  *
7877  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7878  * index: encoded in imm5<4:size+1>
7879  */
7880 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7881 {
7882     int size = ctz32(imm5);
7883     int idx;
7884 
7885     if (size > 3) {
7886         unallocated_encoding(s);
7887         return;
7888     }
7889 
7890     if (!fp_access_check(s)) {
7891         return;
7892     }
7893 
7894     idx = extract32(imm5, 1 + size, 4 - size);
7895     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7896 
7897     /* INS is considered a 128-bit write for SVE. */
7898     clear_vec_high(s, true, rd);
7899 }
7900 
7901 /*
7902  * UMOV (General)
7903  * SMOV (General)
7904  *
7905  *  31  30   29              21 20    16 15    12   10 9    5 4    0
7906  * +---+---+-------------------+--------+-------------+------+------+
7907  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
7908  * +---+---+-------------------+--------+-------------+------+------+
7909  *
7910  * U: unsigned when set
7911  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7912  */
7913 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7914                                   int rn, int rd, int imm5)
7915 {
7916     int size = ctz32(imm5);
7917     int element;
7918     TCGv_i64 tcg_rd;
7919 
7920     /* Check for UnallocatedEncodings */
7921     if (is_signed) {
7922         if (size > 2 || (size == 2 && !is_q)) {
7923             unallocated_encoding(s);
7924             return;
7925         }
7926     } else {
7927         if (size > 3
7928             || (size < 3 && is_q)
7929             || (size == 3 && !is_q)) {
7930             unallocated_encoding(s);
7931             return;
7932         }
7933     }
7934 
7935     if (!fp_access_check(s)) {
7936         return;
7937     }
7938 
7939     element = extract32(imm5, 1+size, 4);
7940 
7941     tcg_rd = cpu_reg(s, rd);
7942     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7943     if (is_signed && !is_q) {
7944         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7945     }
7946 }
7947 
7948 /* AdvSIMD copy
7949  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
7950  * +---+---+----+-----------------+------+---+------+---+------+------+
7951  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7952  * +---+---+----+-----------------+------+---+------+---+------+------+
7953  */
7954 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7955 {
7956     int rd = extract32(insn, 0, 5);
7957     int rn = extract32(insn, 5, 5);
7958     int imm4 = extract32(insn, 11, 4);
7959     int op = extract32(insn, 29, 1);
7960     int is_q = extract32(insn, 30, 1);
7961     int imm5 = extract32(insn, 16, 5);
7962 
7963     if (op) {
7964         if (is_q) {
7965             /* INS (element) */
7966             handle_simd_inse(s, rd, rn, imm4, imm5);
7967         } else {
7968             unallocated_encoding(s);
7969         }
7970     } else {
7971         switch (imm4) {
7972         case 0:
7973             /* DUP (element - vector) */
7974             handle_simd_dupe(s, is_q, rd, rn, imm5);
7975             break;
7976         case 1:
7977             /* DUP (general) */
7978             handle_simd_dupg(s, is_q, rd, rn, imm5);
7979             break;
7980         case 3:
7981             if (is_q) {
7982                 /* INS (general) */
7983                 handle_simd_insg(s, rd, rn, imm5);
7984             } else {
7985                 unallocated_encoding(s);
7986             }
7987             break;
7988         case 5:
7989         case 7:
7990             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
7991             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
7992             break;
7993         default:
7994             unallocated_encoding(s);
7995             break;
7996         }
7997     }
7998 }
7999 
8000 /* AdvSIMD modified immediate
8001  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
8002  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8003  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
8004  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8005  *
8006  * There are a number of operations that can be carried out here:
8007  *   MOVI - move (shifted) imm into register
8008  *   MVNI - move inverted (shifted) imm into register
8009  *   ORR  - bitwise OR of (shifted) imm with register
8010  *   BIC  - bitwise clear of (shifted) imm with register
8011  * With ARMv8.2 we also have:
8012  *   FMOV half-precision
8013  */
8014 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
8015 {
8016     int rd = extract32(insn, 0, 5);
8017     int cmode = extract32(insn, 12, 4);
8018     int o2 = extract32(insn, 11, 1);
8019     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
8020     bool is_neg = extract32(insn, 29, 1);
8021     bool is_q = extract32(insn, 30, 1);
8022     uint64_t imm = 0;
8023 
8024     if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
8025         /* Check for FMOV (vector, immediate) - half-precision */
8026         if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
8027             unallocated_encoding(s);
8028             return;
8029         }
8030     }
8031 
8032     if (!fp_access_check(s)) {
8033         return;
8034     }
8035 
8036     if (cmode == 15 && o2 && !is_neg) {
8037         /* FMOV (vector, immediate) - half-precision */
8038         imm = vfp_expand_imm(MO_16, abcdefgh);
8039         /* now duplicate across the lanes */
8040         imm = dup_const(MO_16, imm);
8041     } else {
8042         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
8043     }
8044 
8045     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
8046         /* MOVI or MVNI, with MVNI negation handled above.  */
8047         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
8048                              vec_full_reg_size(s), imm);
8049     } else {
8050         /* ORR or BIC, with BIC negation to AND handled above.  */
8051         if (is_neg) {
8052             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
8053         } else {
8054             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
8055         }
8056     }
8057 }
8058 
8059 /* AdvSIMD scalar copy
8060  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
8061  * +-----+----+-----------------+------+---+------+---+------+------+
8062  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
8063  * +-----+----+-----------------+------+---+------+---+------+------+
8064  */
8065 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
8066 {
8067     int rd = extract32(insn, 0, 5);
8068     int rn = extract32(insn, 5, 5);
8069     int imm4 = extract32(insn, 11, 4);
8070     int imm5 = extract32(insn, 16, 5);
8071     int op = extract32(insn, 29, 1);
8072 
8073     if (op != 0 || imm4 != 0) {
8074         unallocated_encoding(s);
8075         return;
8076     }
8077 
8078     /* DUP (element, scalar) */
8079     handle_simd_dupes(s, rd, rn, imm5);
8080 }
8081 
8082 /* AdvSIMD scalar pairwise
8083  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
8084  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8085  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
8086  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8087  */
8088 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
8089 {
8090     int u = extract32(insn, 29, 1);
8091     int size = extract32(insn, 22, 2);
8092     int opcode = extract32(insn, 12, 5);
8093     int rn = extract32(insn, 5, 5);
8094     int rd = extract32(insn, 0, 5);
8095     TCGv_ptr fpst;
8096 
8097     /* For some ops (the FP ones), size[1] is part of the encoding.
8098      * For ADDP strictly it is not but size[1] is always 1 for valid
8099      * encodings.
8100      */
8101     opcode |= (extract32(size, 1, 1) << 5);
8102 
8103     switch (opcode) {
8104     case 0x3b: /* ADDP */
8105         if (u || size != 3) {
8106             unallocated_encoding(s);
8107             return;
8108         }
8109         if (!fp_access_check(s)) {
8110             return;
8111         }
8112 
8113         fpst = NULL;
8114         break;
8115     case 0xc: /* FMAXNMP */
8116     case 0xd: /* FADDP */
8117     case 0xf: /* FMAXP */
8118     case 0x2c: /* FMINNMP */
8119     case 0x2f: /* FMINP */
8120         /* FP op, size[0] is 32 or 64 bit*/
8121         if (!u) {
8122             if (!dc_isar_feature(aa64_fp16, s)) {
8123                 unallocated_encoding(s);
8124                 return;
8125             } else {
8126                 size = MO_16;
8127             }
8128         } else {
8129             size = extract32(size, 0, 1) ? MO_64 : MO_32;
8130         }
8131 
8132         if (!fp_access_check(s)) {
8133             return;
8134         }
8135 
8136         fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8137         break;
8138     default:
8139         unallocated_encoding(s);
8140         return;
8141     }
8142 
8143     if (size == MO_64) {
8144         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8145         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8146         TCGv_i64 tcg_res = tcg_temp_new_i64();
8147 
8148         read_vec_element(s, tcg_op1, rn, 0, MO_64);
8149         read_vec_element(s, tcg_op2, rn, 1, MO_64);
8150 
8151         switch (opcode) {
8152         case 0x3b: /* ADDP */
8153             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
8154             break;
8155         case 0xc: /* FMAXNMP */
8156             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8157             break;
8158         case 0xd: /* FADDP */
8159             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8160             break;
8161         case 0xf: /* FMAXP */
8162             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8163             break;
8164         case 0x2c: /* FMINNMP */
8165             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8166             break;
8167         case 0x2f: /* FMINP */
8168             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8169             break;
8170         default:
8171             g_assert_not_reached();
8172         }
8173 
8174         write_fp_dreg(s, rd, tcg_res);
8175     } else {
8176         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8177         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8178         TCGv_i32 tcg_res = tcg_temp_new_i32();
8179 
8180         read_vec_element_i32(s, tcg_op1, rn, 0, size);
8181         read_vec_element_i32(s, tcg_op2, rn, 1, size);
8182 
8183         if (size == MO_16) {
8184             switch (opcode) {
8185             case 0xc: /* FMAXNMP */
8186                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8187                 break;
8188             case 0xd: /* FADDP */
8189                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
8190                 break;
8191             case 0xf: /* FMAXP */
8192                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
8193                 break;
8194             case 0x2c: /* FMINNMP */
8195                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8196                 break;
8197             case 0x2f: /* FMINP */
8198                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
8199                 break;
8200             default:
8201                 g_assert_not_reached();
8202             }
8203         } else {
8204             switch (opcode) {
8205             case 0xc: /* FMAXNMP */
8206                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8207                 break;
8208             case 0xd: /* FADDP */
8209                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8210                 break;
8211             case 0xf: /* FMAXP */
8212                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8213                 break;
8214             case 0x2c: /* FMINNMP */
8215                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8216                 break;
8217             case 0x2f: /* FMINP */
8218                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8219                 break;
8220             default:
8221                 g_assert_not_reached();
8222             }
8223         }
8224 
8225         write_fp_sreg(s, rd, tcg_res);
8226     }
8227 }
8228 
8229 /*
8230  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8231  *
8232  * This code is handles the common shifting code and is used by both
8233  * the vector and scalar code.
8234  */
8235 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8236                                     TCGv_i64 tcg_rnd, bool accumulate,
8237                                     bool is_u, int size, int shift)
8238 {
8239     bool extended_result = false;
8240     bool round = tcg_rnd != NULL;
8241     int ext_lshift = 0;
8242     TCGv_i64 tcg_src_hi;
8243 
8244     if (round && size == 3) {
8245         extended_result = true;
8246         ext_lshift = 64 - shift;
8247         tcg_src_hi = tcg_temp_new_i64();
8248     } else if (shift == 64) {
8249         if (!accumulate && is_u) {
8250             /* result is zero */
8251             tcg_gen_movi_i64(tcg_res, 0);
8252             return;
8253         }
8254     }
8255 
8256     /* Deal with the rounding step */
8257     if (round) {
8258         if (extended_result) {
8259             TCGv_i64 tcg_zero = tcg_constant_i64(0);
8260             if (!is_u) {
8261                 /* take care of sign extending tcg_res */
8262                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8263                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8264                                  tcg_src, tcg_src_hi,
8265                                  tcg_rnd, tcg_zero);
8266             } else {
8267                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8268                                  tcg_src, tcg_zero,
8269                                  tcg_rnd, tcg_zero);
8270             }
8271         } else {
8272             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8273         }
8274     }
8275 
8276     /* Now do the shift right */
8277     if (round && extended_result) {
8278         /* extended case, >64 bit precision required */
8279         if (ext_lshift == 0) {
8280             /* special case, only high bits matter */
8281             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8282         } else {
8283             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8284             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8285             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8286         }
8287     } else {
8288         if (is_u) {
8289             if (shift == 64) {
8290                 /* essentially shifting in 64 zeros */
8291                 tcg_gen_movi_i64(tcg_src, 0);
8292             } else {
8293                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8294             }
8295         } else {
8296             if (shift == 64) {
8297                 /* effectively extending the sign-bit */
8298                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8299             } else {
8300                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8301             }
8302         }
8303     }
8304 
8305     if (accumulate) {
8306         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8307     } else {
8308         tcg_gen_mov_i64(tcg_res, tcg_src);
8309     }
8310 }
8311 
8312 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8313 static void handle_scalar_simd_shri(DisasContext *s,
8314                                     bool is_u, int immh, int immb,
8315                                     int opcode, int rn, int rd)
8316 {
8317     const int size = 3;
8318     int immhb = immh << 3 | immb;
8319     int shift = 2 * (8 << size) - immhb;
8320     bool accumulate = false;
8321     bool round = false;
8322     bool insert = false;
8323     TCGv_i64 tcg_rn;
8324     TCGv_i64 tcg_rd;
8325     TCGv_i64 tcg_round;
8326 
8327     if (!extract32(immh, 3, 1)) {
8328         unallocated_encoding(s);
8329         return;
8330     }
8331 
8332     if (!fp_access_check(s)) {
8333         return;
8334     }
8335 
8336     switch (opcode) {
8337     case 0x02: /* SSRA / USRA (accumulate) */
8338         accumulate = true;
8339         break;
8340     case 0x04: /* SRSHR / URSHR (rounding) */
8341         round = true;
8342         break;
8343     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8344         accumulate = round = true;
8345         break;
8346     case 0x08: /* SRI */
8347         insert = true;
8348         break;
8349     }
8350 
8351     if (round) {
8352         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8353     } else {
8354         tcg_round = NULL;
8355     }
8356 
8357     tcg_rn = read_fp_dreg(s, rn);
8358     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8359 
8360     if (insert) {
8361         /* shift count same as element size is valid but does nothing;
8362          * special case to avoid potential shift by 64.
8363          */
8364         int esize = 8 << size;
8365         if (shift != esize) {
8366             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8367             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8368         }
8369     } else {
8370         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8371                                 accumulate, is_u, size, shift);
8372     }
8373 
8374     write_fp_dreg(s, rd, tcg_rd);
8375 }
8376 
8377 /* SHL/SLI - Scalar shift left */
8378 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8379                                     int immh, int immb, int opcode,
8380                                     int rn, int rd)
8381 {
8382     int size = 32 - clz32(immh) - 1;
8383     int immhb = immh << 3 | immb;
8384     int shift = immhb - (8 << size);
8385     TCGv_i64 tcg_rn;
8386     TCGv_i64 tcg_rd;
8387 
8388     if (!extract32(immh, 3, 1)) {
8389         unallocated_encoding(s);
8390         return;
8391     }
8392 
8393     if (!fp_access_check(s)) {
8394         return;
8395     }
8396 
8397     tcg_rn = read_fp_dreg(s, rn);
8398     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8399 
8400     if (insert) {
8401         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8402     } else {
8403         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8404     }
8405 
8406     write_fp_dreg(s, rd, tcg_rd);
8407 }
8408 
8409 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8410  * (signed/unsigned) narrowing */
8411 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8412                                    bool is_u_shift, bool is_u_narrow,
8413                                    int immh, int immb, int opcode,
8414                                    int rn, int rd)
8415 {
8416     int immhb = immh << 3 | immb;
8417     int size = 32 - clz32(immh) - 1;
8418     int esize = 8 << size;
8419     int shift = (2 * esize) - immhb;
8420     int elements = is_scalar ? 1 : (64 / esize);
8421     bool round = extract32(opcode, 0, 1);
8422     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8423     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8424     TCGv_i32 tcg_rd_narrowed;
8425     TCGv_i64 tcg_final;
8426 
8427     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8428         { gen_helper_neon_narrow_sat_s8,
8429           gen_helper_neon_unarrow_sat8 },
8430         { gen_helper_neon_narrow_sat_s16,
8431           gen_helper_neon_unarrow_sat16 },
8432         { gen_helper_neon_narrow_sat_s32,
8433           gen_helper_neon_unarrow_sat32 },
8434         { NULL, NULL },
8435     };
8436     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8437         gen_helper_neon_narrow_sat_u8,
8438         gen_helper_neon_narrow_sat_u16,
8439         gen_helper_neon_narrow_sat_u32,
8440         NULL
8441     };
8442     NeonGenNarrowEnvFn *narrowfn;
8443 
8444     int i;
8445 
8446     assert(size < 4);
8447 
8448     if (extract32(immh, 3, 1)) {
8449         unallocated_encoding(s);
8450         return;
8451     }
8452 
8453     if (!fp_access_check(s)) {
8454         return;
8455     }
8456 
8457     if (is_u_shift) {
8458         narrowfn = unsigned_narrow_fns[size];
8459     } else {
8460         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8461     }
8462 
8463     tcg_rn = tcg_temp_new_i64();
8464     tcg_rd = tcg_temp_new_i64();
8465     tcg_rd_narrowed = tcg_temp_new_i32();
8466     tcg_final = tcg_const_i64(0);
8467 
8468     if (round) {
8469         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8470     } else {
8471         tcg_round = NULL;
8472     }
8473 
8474     for (i = 0; i < elements; i++) {
8475         read_vec_element(s, tcg_rn, rn, i, ldop);
8476         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8477                                 false, is_u_shift, size+1, shift);
8478         narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8479         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8480         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
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_rmode = tcg_const_i32(arm_rmode_to_sf(FPROUNDING_ZERO));
8775     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8776     gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
8777     fracbits = (16 << size) - immhb;
8778     tcg_shift = tcg_constant_i32(fracbits);
8779 
8780     if (size == MO_64) {
8781         int maxpass = is_scalar ? 1 : 2;
8782 
8783         for (pass = 0; pass < maxpass; pass++) {
8784             TCGv_i64 tcg_op = tcg_temp_new_i64();
8785 
8786             read_vec_element(s, tcg_op, rn, pass, MO_64);
8787             if (is_u) {
8788                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8789             } else {
8790                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8791             }
8792             write_vec_element(s, tcg_op, rd, pass, MO_64);
8793         }
8794         clear_vec_high(s, is_q, rd);
8795     } else {
8796         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8797         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8798 
8799         switch (size) {
8800         case MO_16:
8801             if (is_u) {
8802                 fn = gen_helper_vfp_touhh;
8803             } else {
8804                 fn = gen_helper_vfp_toshh;
8805             }
8806             break;
8807         case MO_32:
8808             if (is_u) {
8809                 fn = gen_helper_vfp_touls;
8810             } else {
8811                 fn = gen_helper_vfp_tosls;
8812             }
8813             break;
8814         default:
8815             g_assert_not_reached();
8816         }
8817 
8818         for (pass = 0; pass < maxpass; pass++) {
8819             TCGv_i32 tcg_op = tcg_temp_new_i32();
8820 
8821             read_vec_element_i32(s, tcg_op, rn, pass, size);
8822             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8823             if (is_scalar) {
8824                 write_fp_sreg(s, rd, tcg_op);
8825             } else {
8826                 write_vec_element_i32(s, tcg_op, rd, pass, size);
8827             }
8828         }
8829         if (!is_scalar) {
8830             clear_vec_high(s, is_q, rd);
8831         }
8832     }
8833 
8834     gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
8835 }
8836 
8837 /* AdvSIMD scalar shift by immediate
8838  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
8839  * +-----+---+-------------+------+------+--------+---+------+------+
8840  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8841  * +-----+---+-------------+------+------+--------+---+------+------+
8842  *
8843  * This is the scalar version so it works on a fixed sized registers
8844  */
8845 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8846 {
8847     int rd = extract32(insn, 0, 5);
8848     int rn = extract32(insn, 5, 5);
8849     int opcode = extract32(insn, 11, 5);
8850     int immb = extract32(insn, 16, 3);
8851     int immh = extract32(insn, 19, 4);
8852     bool is_u = extract32(insn, 29, 1);
8853 
8854     if (immh == 0) {
8855         unallocated_encoding(s);
8856         return;
8857     }
8858 
8859     switch (opcode) {
8860     case 0x08: /* SRI */
8861         if (!is_u) {
8862             unallocated_encoding(s);
8863             return;
8864         }
8865         /* fall through */
8866     case 0x00: /* SSHR / USHR */
8867     case 0x02: /* SSRA / USRA */
8868     case 0x04: /* SRSHR / URSHR */
8869     case 0x06: /* SRSRA / URSRA */
8870         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8871         break;
8872     case 0x0a: /* SHL / SLI */
8873         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8874         break;
8875     case 0x1c: /* SCVTF, UCVTF */
8876         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8877                                      opcode, rn, rd);
8878         break;
8879     case 0x10: /* SQSHRUN, SQSHRUN2 */
8880     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8881         if (!is_u) {
8882             unallocated_encoding(s);
8883             return;
8884         }
8885         handle_vec_simd_sqshrn(s, true, false, false, true,
8886                                immh, immb, opcode, rn, rd);
8887         break;
8888     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8889     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8890         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8891                                immh, immb, opcode, rn, rd);
8892         break;
8893     case 0xc: /* SQSHLU */
8894         if (!is_u) {
8895             unallocated_encoding(s);
8896             return;
8897         }
8898         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8899         break;
8900     case 0xe: /* SQSHL, UQSHL */
8901         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8902         break;
8903     case 0x1f: /* FCVTZS, FCVTZU */
8904         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8905         break;
8906     default:
8907         unallocated_encoding(s);
8908         break;
8909     }
8910 }
8911 
8912 /* AdvSIMD scalar three different
8913  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
8914  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8915  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
8916  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8917  */
8918 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8919 {
8920     bool is_u = extract32(insn, 29, 1);
8921     int size = extract32(insn, 22, 2);
8922     int opcode = extract32(insn, 12, 4);
8923     int rm = extract32(insn, 16, 5);
8924     int rn = extract32(insn, 5, 5);
8925     int rd = extract32(insn, 0, 5);
8926 
8927     if (is_u) {
8928         unallocated_encoding(s);
8929         return;
8930     }
8931 
8932     switch (opcode) {
8933     case 0x9: /* SQDMLAL, SQDMLAL2 */
8934     case 0xb: /* SQDMLSL, SQDMLSL2 */
8935     case 0xd: /* SQDMULL, SQDMULL2 */
8936         if (size == 0 || size == 3) {
8937             unallocated_encoding(s);
8938             return;
8939         }
8940         break;
8941     default:
8942         unallocated_encoding(s);
8943         return;
8944     }
8945 
8946     if (!fp_access_check(s)) {
8947         return;
8948     }
8949 
8950     if (size == 2) {
8951         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8952         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8953         TCGv_i64 tcg_res = tcg_temp_new_i64();
8954 
8955         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8956         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8957 
8958         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8959         gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
8960 
8961         switch (opcode) {
8962         case 0xd: /* SQDMULL, SQDMULL2 */
8963             break;
8964         case 0xb: /* SQDMLSL, SQDMLSL2 */
8965             tcg_gen_neg_i64(tcg_res, tcg_res);
8966             /* fall through */
8967         case 0x9: /* SQDMLAL, SQDMLAL2 */
8968             read_vec_element(s, tcg_op1, rd, 0, MO_64);
8969             gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
8970                                               tcg_res, tcg_op1);
8971             break;
8972         default:
8973             g_assert_not_reached();
8974         }
8975 
8976         write_fp_dreg(s, rd, tcg_res);
8977     } else {
8978         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8979         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8980         TCGv_i64 tcg_res = tcg_temp_new_i64();
8981 
8982         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8983         gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
8984 
8985         switch (opcode) {
8986         case 0xd: /* SQDMULL, SQDMULL2 */
8987             break;
8988         case 0xb: /* SQDMLSL, SQDMLSL2 */
8989             gen_helper_neon_negl_u32(tcg_res, tcg_res);
8990             /* fall through */
8991         case 0x9: /* SQDMLAL, SQDMLAL2 */
8992         {
8993             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
8994             read_vec_element(s, tcg_op3, rd, 0, MO_32);
8995             gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
8996                                               tcg_res, tcg_op3);
8997             break;
8998         }
8999         default:
9000             g_assert_not_reached();
9001         }
9002 
9003         tcg_gen_ext32u_i64(tcg_res, tcg_res);
9004         write_fp_dreg(s, rd, tcg_res);
9005     }
9006 }
9007 
9008 static void handle_3same_64(DisasContext *s, int opcode, bool u,
9009                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
9010 {
9011     /* Handle 64x64->64 opcodes which are shared between the scalar
9012      * and vector 3-same groups. We cover every opcode where size == 3
9013      * is valid in either the three-reg-same (integer, not pairwise)
9014      * or scalar-three-reg-same groups.
9015      */
9016     TCGCond cond;
9017 
9018     switch (opcode) {
9019     case 0x1: /* SQADD */
9020         if (u) {
9021             gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9022         } else {
9023             gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9024         }
9025         break;
9026     case 0x5: /* SQSUB */
9027         if (u) {
9028             gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9029         } else {
9030             gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9031         }
9032         break;
9033     case 0x6: /* CMGT, CMHI */
9034         /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
9035          * We implement this using setcond (test) and then negating.
9036          */
9037         cond = u ? TCG_COND_GTU : TCG_COND_GT;
9038     do_cmop:
9039         tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
9040         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9041         break;
9042     case 0x7: /* CMGE, CMHS */
9043         cond = u ? TCG_COND_GEU : TCG_COND_GE;
9044         goto do_cmop;
9045     case 0x11: /* CMTST, CMEQ */
9046         if (u) {
9047             cond = TCG_COND_EQ;
9048             goto do_cmop;
9049         }
9050         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
9051         break;
9052     case 0x8: /* SSHL, USHL */
9053         if (u) {
9054             gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
9055         } else {
9056             gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
9057         }
9058         break;
9059     case 0x9: /* SQSHL, UQSHL */
9060         if (u) {
9061             gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9062         } else {
9063             gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9064         }
9065         break;
9066     case 0xa: /* SRSHL, URSHL */
9067         if (u) {
9068             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
9069         } else {
9070             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
9071         }
9072         break;
9073     case 0xb: /* SQRSHL, UQRSHL */
9074         if (u) {
9075             gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9076         } else {
9077             gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9078         }
9079         break;
9080     case 0x10: /* ADD, SUB */
9081         if (u) {
9082             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
9083         } else {
9084             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
9085         }
9086         break;
9087     default:
9088         g_assert_not_reached();
9089     }
9090 }
9091 
9092 /* Handle the 3-same-operands float operations; shared by the scalar
9093  * and vector encodings. The caller must filter out any encodings
9094  * not allocated for the encoding it is dealing with.
9095  */
9096 static void handle_3same_float(DisasContext *s, int size, int elements,
9097                                int fpopcode, int rd, int rn, int rm)
9098 {
9099     int pass;
9100     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9101 
9102     for (pass = 0; pass < elements; pass++) {
9103         if (size) {
9104             /* Double */
9105             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9106             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9107             TCGv_i64 tcg_res = tcg_temp_new_i64();
9108 
9109             read_vec_element(s, tcg_op1, rn, pass, MO_64);
9110             read_vec_element(s, tcg_op2, rm, pass, MO_64);
9111 
9112             switch (fpopcode) {
9113             case 0x39: /* FMLS */
9114                 /* As usual for ARM, separate negation for fused multiply-add */
9115                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
9116                 /* fall through */
9117             case 0x19: /* FMLA */
9118                 read_vec_element(s, tcg_res, rd, pass, MO_64);
9119                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
9120                                        tcg_res, fpst);
9121                 break;
9122             case 0x18: /* FMAXNM */
9123                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9124                 break;
9125             case 0x1a: /* FADD */
9126                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
9127                 break;
9128             case 0x1b: /* FMULX */
9129                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
9130                 break;
9131             case 0x1c: /* FCMEQ */
9132                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9133                 break;
9134             case 0x1e: /* FMAX */
9135                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
9136                 break;
9137             case 0x1f: /* FRECPS */
9138                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9139                 break;
9140             case 0x38: /* FMINNM */
9141                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9142                 break;
9143             case 0x3a: /* FSUB */
9144                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9145                 break;
9146             case 0x3e: /* FMIN */
9147                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
9148                 break;
9149             case 0x3f: /* FRSQRTS */
9150                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9151                 break;
9152             case 0x5b: /* FMUL */
9153                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
9154                 break;
9155             case 0x5c: /* FCMGE */
9156                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9157                 break;
9158             case 0x5d: /* FACGE */
9159                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9160                 break;
9161             case 0x5f: /* FDIV */
9162                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
9163                 break;
9164             case 0x7a: /* FABD */
9165                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9166                 gen_helper_vfp_absd(tcg_res, tcg_res);
9167                 break;
9168             case 0x7c: /* FCMGT */
9169                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9170                 break;
9171             case 0x7d: /* FACGT */
9172                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9173                 break;
9174             default:
9175                 g_assert_not_reached();
9176             }
9177 
9178             write_vec_element(s, tcg_res, rd, pass, MO_64);
9179         } else {
9180             /* Single */
9181             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9182             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9183             TCGv_i32 tcg_res = tcg_temp_new_i32();
9184 
9185             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9186             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9187 
9188             switch (fpopcode) {
9189             case 0x39: /* FMLS */
9190                 /* As usual for ARM, separate negation for fused multiply-add */
9191                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
9192                 /* fall through */
9193             case 0x19: /* FMLA */
9194                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9195                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
9196                                        tcg_res, fpst);
9197                 break;
9198             case 0x1a: /* FADD */
9199                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
9200                 break;
9201             case 0x1b: /* FMULX */
9202                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
9203                 break;
9204             case 0x1c: /* FCMEQ */
9205                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9206                 break;
9207             case 0x1e: /* FMAX */
9208                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
9209                 break;
9210             case 0x1f: /* FRECPS */
9211                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9212                 break;
9213             case 0x18: /* FMAXNM */
9214                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
9215                 break;
9216             case 0x38: /* FMINNM */
9217                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
9218                 break;
9219             case 0x3a: /* FSUB */
9220                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9221                 break;
9222             case 0x3e: /* FMIN */
9223                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9224                 break;
9225             case 0x3f: /* FRSQRTS */
9226                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9227                 break;
9228             case 0x5b: /* FMUL */
9229                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9230                 break;
9231             case 0x5c: /* FCMGE */
9232                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9233                 break;
9234             case 0x5d: /* FACGE */
9235                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9236                 break;
9237             case 0x5f: /* FDIV */
9238                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9239                 break;
9240             case 0x7a: /* FABD */
9241                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9242                 gen_helper_vfp_abss(tcg_res, tcg_res);
9243                 break;
9244             case 0x7c: /* FCMGT */
9245                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9246                 break;
9247             case 0x7d: /* FACGT */
9248                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9249                 break;
9250             default:
9251                 g_assert_not_reached();
9252             }
9253 
9254             if (elements == 1) {
9255                 /* scalar single so clear high part */
9256                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9257 
9258                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9259                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9260             } else {
9261                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9262             }
9263         }
9264     }
9265 
9266     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9267 }
9268 
9269 /* AdvSIMD scalar three same
9270  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
9271  * +-----+---+-----------+------+---+------+--------+---+------+------+
9272  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
9273  * +-----+---+-----------+------+---+------+--------+---+------+------+
9274  */
9275 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9276 {
9277     int rd = extract32(insn, 0, 5);
9278     int rn = extract32(insn, 5, 5);
9279     int opcode = extract32(insn, 11, 5);
9280     int rm = extract32(insn, 16, 5);
9281     int size = extract32(insn, 22, 2);
9282     bool u = extract32(insn, 29, 1);
9283     TCGv_i64 tcg_rd;
9284 
9285     if (opcode >= 0x18) {
9286         /* Floating point: U, size[1] and opcode indicate operation */
9287         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9288         switch (fpopcode) {
9289         case 0x1b: /* FMULX */
9290         case 0x1f: /* FRECPS */
9291         case 0x3f: /* FRSQRTS */
9292         case 0x5d: /* FACGE */
9293         case 0x7d: /* FACGT */
9294         case 0x1c: /* FCMEQ */
9295         case 0x5c: /* FCMGE */
9296         case 0x7c: /* FCMGT */
9297         case 0x7a: /* FABD */
9298             break;
9299         default:
9300             unallocated_encoding(s);
9301             return;
9302         }
9303 
9304         if (!fp_access_check(s)) {
9305             return;
9306         }
9307 
9308         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9309         return;
9310     }
9311 
9312     switch (opcode) {
9313     case 0x1: /* SQADD, UQADD */
9314     case 0x5: /* SQSUB, UQSUB */
9315     case 0x9: /* SQSHL, UQSHL */
9316     case 0xb: /* SQRSHL, UQRSHL */
9317         break;
9318     case 0x8: /* SSHL, USHL */
9319     case 0xa: /* SRSHL, URSHL */
9320     case 0x6: /* CMGT, CMHI */
9321     case 0x7: /* CMGE, CMHS */
9322     case 0x11: /* CMTST, CMEQ */
9323     case 0x10: /* ADD, SUB (vector) */
9324         if (size != 3) {
9325             unallocated_encoding(s);
9326             return;
9327         }
9328         break;
9329     case 0x16: /* SQDMULH, SQRDMULH (vector) */
9330         if (size != 1 && size != 2) {
9331             unallocated_encoding(s);
9332             return;
9333         }
9334         break;
9335     default:
9336         unallocated_encoding(s);
9337         return;
9338     }
9339 
9340     if (!fp_access_check(s)) {
9341         return;
9342     }
9343 
9344     tcg_rd = tcg_temp_new_i64();
9345 
9346     if (size == 3) {
9347         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9348         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9349 
9350         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9351     } else {
9352         /* Do a single operation on the lowest element in the vector.
9353          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9354          * no side effects for all these operations.
9355          * OPTME: special-purpose helpers would avoid doing some
9356          * unnecessary work in the helper for the 8 and 16 bit cases.
9357          */
9358         NeonGenTwoOpEnvFn *genenvfn;
9359         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9360         TCGv_i32 tcg_rm = tcg_temp_new_i32();
9361         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9362 
9363         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9364         read_vec_element_i32(s, tcg_rm, rm, 0, size);
9365 
9366         switch (opcode) {
9367         case 0x1: /* SQADD, UQADD */
9368         {
9369             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9370                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9371                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9372                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9373             };
9374             genenvfn = fns[size][u];
9375             break;
9376         }
9377         case 0x5: /* SQSUB, UQSUB */
9378         {
9379             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9380                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9381                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9382                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9383             };
9384             genenvfn = fns[size][u];
9385             break;
9386         }
9387         case 0x9: /* SQSHL, UQSHL */
9388         {
9389             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9390                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9391                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9392                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9393             };
9394             genenvfn = fns[size][u];
9395             break;
9396         }
9397         case 0xb: /* SQRSHL, UQRSHL */
9398         {
9399             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9400                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9401                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9402                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9403             };
9404             genenvfn = fns[size][u];
9405             break;
9406         }
9407         case 0x16: /* SQDMULH, SQRDMULH */
9408         {
9409             static NeonGenTwoOpEnvFn * const fns[2][2] = {
9410                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9411                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9412             };
9413             assert(size == 1 || size == 2);
9414             genenvfn = fns[size - 1][u];
9415             break;
9416         }
9417         default:
9418             g_assert_not_reached();
9419         }
9420 
9421         genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9422         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9423     }
9424 
9425     write_fp_dreg(s, rd, tcg_rd);
9426 }
9427 
9428 /* AdvSIMD scalar three same FP16
9429  *  31 30  29 28       24 23  22 21 20  16 15 14 13    11 10  9  5 4  0
9430  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9431  * | 0 1 | U | 1 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 | Rn | Rd |
9432  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9433  * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9434  * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9435  */
9436 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9437                                                   uint32_t insn)
9438 {
9439     int rd = extract32(insn, 0, 5);
9440     int rn = extract32(insn, 5, 5);
9441     int opcode = extract32(insn, 11, 3);
9442     int rm = extract32(insn, 16, 5);
9443     bool u = extract32(insn, 29, 1);
9444     bool a = extract32(insn, 23, 1);
9445     int fpopcode = opcode | (a << 3) |  (u << 4);
9446     TCGv_ptr fpst;
9447     TCGv_i32 tcg_op1;
9448     TCGv_i32 tcg_op2;
9449     TCGv_i32 tcg_res;
9450 
9451     switch (fpopcode) {
9452     case 0x03: /* FMULX */
9453     case 0x04: /* FCMEQ (reg) */
9454     case 0x07: /* FRECPS */
9455     case 0x0f: /* FRSQRTS */
9456     case 0x14: /* FCMGE (reg) */
9457     case 0x15: /* FACGE */
9458     case 0x1a: /* FABD */
9459     case 0x1c: /* FCMGT (reg) */
9460     case 0x1d: /* FACGT */
9461         break;
9462     default:
9463         unallocated_encoding(s);
9464         return;
9465     }
9466 
9467     if (!dc_isar_feature(aa64_fp16, s)) {
9468         unallocated_encoding(s);
9469     }
9470 
9471     if (!fp_access_check(s)) {
9472         return;
9473     }
9474 
9475     fpst = fpstatus_ptr(FPST_FPCR_F16);
9476 
9477     tcg_op1 = read_fp_hreg(s, rn);
9478     tcg_op2 = read_fp_hreg(s, rm);
9479     tcg_res = tcg_temp_new_i32();
9480 
9481     switch (fpopcode) {
9482     case 0x03: /* FMULX */
9483         gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9484         break;
9485     case 0x04: /* FCMEQ (reg) */
9486         gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9487         break;
9488     case 0x07: /* FRECPS */
9489         gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9490         break;
9491     case 0x0f: /* FRSQRTS */
9492         gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9493         break;
9494     case 0x14: /* FCMGE (reg) */
9495         gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9496         break;
9497     case 0x15: /* FACGE */
9498         gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9499         break;
9500     case 0x1a: /* FABD */
9501         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9502         tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9503         break;
9504     case 0x1c: /* FCMGT (reg) */
9505         gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9506         break;
9507     case 0x1d: /* FACGT */
9508         gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9509         break;
9510     default:
9511         g_assert_not_reached();
9512     }
9513 
9514     write_fp_sreg(s, rd, tcg_res);
9515 }
9516 
9517 /* AdvSIMD scalar three same extra
9518  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9519  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9520  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9521  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9522  */
9523 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9524                                                    uint32_t insn)
9525 {
9526     int rd = extract32(insn, 0, 5);
9527     int rn = extract32(insn, 5, 5);
9528     int opcode = extract32(insn, 11, 4);
9529     int rm = extract32(insn, 16, 5);
9530     int size = extract32(insn, 22, 2);
9531     bool u = extract32(insn, 29, 1);
9532     TCGv_i32 ele1, ele2, ele3;
9533     TCGv_i64 res;
9534     bool feature;
9535 
9536     switch (u * 16 + opcode) {
9537     case 0x10: /* SQRDMLAH (vector) */
9538     case 0x11: /* SQRDMLSH (vector) */
9539         if (size != 1 && size != 2) {
9540             unallocated_encoding(s);
9541             return;
9542         }
9543         feature = dc_isar_feature(aa64_rdm, s);
9544         break;
9545     default:
9546         unallocated_encoding(s);
9547         return;
9548     }
9549     if (!feature) {
9550         unallocated_encoding(s);
9551         return;
9552     }
9553     if (!fp_access_check(s)) {
9554         return;
9555     }
9556 
9557     /* Do a single operation on the lowest element in the vector.
9558      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9559      * with no side effects for all these operations.
9560      * OPTME: special-purpose helpers would avoid doing some
9561      * unnecessary work in the helper for the 16 bit cases.
9562      */
9563     ele1 = tcg_temp_new_i32();
9564     ele2 = tcg_temp_new_i32();
9565     ele3 = tcg_temp_new_i32();
9566 
9567     read_vec_element_i32(s, ele1, rn, 0, size);
9568     read_vec_element_i32(s, ele2, rm, 0, size);
9569     read_vec_element_i32(s, ele3, rd, 0, size);
9570 
9571     switch (opcode) {
9572     case 0x0: /* SQRDMLAH */
9573         if (size == 1) {
9574             gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9575         } else {
9576             gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9577         }
9578         break;
9579     case 0x1: /* SQRDMLSH */
9580         if (size == 1) {
9581             gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9582         } else {
9583             gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9584         }
9585         break;
9586     default:
9587         g_assert_not_reached();
9588     }
9589 
9590     res = tcg_temp_new_i64();
9591     tcg_gen_extu_i32_i64(res, ele3);
9592     write_fp_dreg(s, rd, res);
9593 }
9594 
9595 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9596                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9597                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9598 {
9599     /* Handle 64->64 opcodes which are shared between the scalar and
9600      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9601      * is valid in either group and also the double-precision fp ops.
9602      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9603      * requires them.
9604      */
9605     TCGCond cond;
9606 
9607     switch (opcode) {
9608     case 0x4: /* CLS, CLZ */
9609         if (u) {
9610             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9611         } else {
9612             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9613         }
9614         break;
9615     case 0x5: /* NOT */
9616         /* This opcode is shared with CNT and RBIT but we have earlier
9617          * enforced that size == 3 if and only if this is the NOT insn.
9618          */
9619         tcg_gen_not_i64(tcg_rd, tcg_rn);
9620         break;
9621     case 0x7: /* SQABS, SQNEG */
9622         if (u) {
9623             gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9624         } else {
9625             gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9626         }
9627         break;
9628     case 0xa: /* CMLT */
9629         /* 64 bit integer comparison against zero, result is
9630          * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9631          * subtracting 1.
9632          */
9633         cond = TCG_COND_LT;
9634     do_cmop:
9635         tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9636         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9637         break;
9638     case 0x8: /* CMGT, CMGE */
9639         cond = u ? TCG_COND_GE : TCG_COND_GT;
9640         goto do_cmop;
9641     case 0x9: /* CMEQ, CMLE */
9642         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9643         goto do_cmop;
9644     case 0xb: /* ABS, NEG */
9645         if (u) {
9646             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9647         } else {
9648             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9649         }
9650         break;
9651     case 0x2f: /* FABS */
9652         gen_helper_vfp_absd(tcg_rd, tcg_rn);
9653         break;
9654     case 0x6f: /* FNEG */
9655         gen_helper_vfp_negd(tcg_rd, tcg_rn);
9656         break;
9657     case 0x7f: /* FSQRT */
9658         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9659         break;
9660     case 0x1a: /* FCVTNS */
9661     case 0x1b: /* FCVTMS */
9662     case 0x1c: /* FCVTAS */
9663     case 0x3a: /* FCVTPS */
9664     case 0x3b: /* FCVTZS */
9665         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9666         break;
9667     case 0x5a: /* FCVTNU */
9668     case 0x5b: /* FCVTMU */
9669     case 0x5c: /* FCVTAU */
9670     case 0x7a: /* FCVTPU */
9671     case 0x7b: /* FCVTZU */
9672         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9673         break;
9674     case 0x18: /* FRINTN */
9675     case 0x19: /* FRINTM */
9676     case 0x38: /* FRINTP */
9677     case 0x39: /* FRINTZ */
9678     case 0x58: /* FRINTA */
9679     case 0x79: /* FRINTI */
9680         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9681         break;
9682     case 0x59: /* FRINTX */
9683         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9684         break;
9685     case 0x1e: /* FRINT32Z */
9686     case 0x5e: /* FRINT32X */
9687         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9688         break;
9689     case 0x1f: /* FRINT64Z */
9690     case 0x5f: /* FRINT64X */
9691         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9692         break;
9693     default:
9694         g_assert_not_reached();
9695     }
9696 }
9697 
9698 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9699                                    bool is_scalar, bool is_u, bool is_q,
9700                                    int size, int rn, int rd)
9701 {
9702     bool is_double = (size == MO_64);
9703     TCGv_ptr fpst;
9704 
9705     if (!fp_access_check(s)) {
9706         return;
9707     }
9708 
9709     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9710 
9711     if (is_double) {
9712         TCGv_i64 tcg_op = tcg_temp_new_i64();
9713         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9714         TCGv_i64 tcg_res = tcg_temp_new_i64();
9715         NeonGenTwoDoubleOpFn *genfn;
9716         bool swap = false;
9717         int pass;
9718 
9719         switch (opcode) {
9720         case 0x2e: /* FCMLT (zero) */
9721             swap = true;
9722             /* fallthrough */
9723         case 0x2c: /* FCMGT (zero) */
9724             genfn = gen_helper_neon_cgt_f64;
9725             break;
9726         case 0x2d: /* FCMEQ (zero) */
9727             genfn = gen_helper_neon_ceq_f64;
9728             break;
9729         case 0x6d: /* FCMLE (zero) */
9730             swap = true;
9731             /* fall through */
9732         case 0x6c: /* FCMGE (zero) */
9733             genfn = gen_helper_neon_cge_f64;
9734             break;
9735         default:
9736             g_assert_not_reached();
9737         }
9738 
9739         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9740             read_vec_element(s, tcg_op, rn, pass, MO_64);
9741             if (swap) {
9742                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9743             } else {
9744                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9745             }
9746             write_vec_element(s, tcg_res, rd, pass, MO_64);
9747         }
9748 
9749         clear_vec_high(s, !is_scalar, rd);
9750     } else {
9751         TCGv_i32 tcg_op = tcg_temp_new_i32();
9752         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9753         TCGv_i32 tcg_res = tcg_temp_new_i32();
9754         NeonGenTwoSingleOpFn *genfn;
9755         bool swap = false;
9756         int pass, maxpasses;
9757 
9758         if (size == MO_16) {
9759             switch (opcode) {
9760             case 0x2e: /* FCMLT (zero) */
9761                 swap = true;
9762                 /* fall through */
9763             case 0x2c: /* FCMGT (zero) */
9764                 genfn = gen_helper_advsimd_cgt_f16;
9765                 break;
9766             case 0x2d: /* FCMEQ (zero) */
9767                 genfn = gen_helper_advsimd_ceq_f16;
9768                 break;
9769             case 0x6d: /* FCMLE (zero) */
9770                 swap = true;
9771                 /* fall through */
9772             case 0x6c: /* FCMGE (zero) */
9773                 genfn = gen_helper_advsimd_cge_f16;
9774                 break;
9775             default:
9776                 g_assert_not_reached();
9777             }
9778         } else {
9779             switch (opcode) {
9780             case 0x2e: /* FCMLT (zero) */
9781                 swap = true;
9782                 /* fall through */
9783             case 0x2c: /* FCMGT (zero) */
9784                 genfn = gen_helper_neon_cgt_f32;
9785                 break;
9786             case 0x2d: /* FCMEQ (zero) */
9787                 genfn = gen_helper_neon_ceq_f32;
9788                 break;
9789             case 0x6d: /* FCMLE (zero) */
9790                 swap = true;
9791                 /* fall through */
9792             case 0x6c: /* FCMGE (zero) */
9793                 genfn = gen_helper_neon_cge_f32;
9794                 break;
9795             default:
9796                 g_assert_not_reached();
9797             }
9798         }
9799 
9800         if (is_scalar) {
9801             maxpasses = 1;
9802         } else {
9803             int vector_size = 8 << is_q;
9804             maxpasses = vector_size >> size;
9805         }
9806 
9807         for (pass = 0; pass < maxpasses; pass++) {
9808             read_vec_element_i32(s, tcg_op, rn, pass, size);
9809             if (swap) {
9810                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9811             } else {
9812                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9813             }
9814             if (is_scalar) {
9815                 write_fp_sreg(s, rd, tcg_res);
9816             } else {
9817                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9818             }
9819         }
9820 
9821         if (!is_scalar) {
9822             clear_vec_high(s, is_q, rd);
9823         }
9824     }
9825 }
9826 
9827 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9828                                     bool is_scalar, bool is_u, bool is_q,
9829                                     int size, int rn, int rd)
9830 {
9831     bool is_double = (size == 3);
9832     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9833 
9834     if (is_double) {
9835         TCGv_i64 tcg_op = tcg_temp_new_i64();
9836         TCGv_i64 tcg_res = tcg_temp_new_i64();
9837         int pass;
9838 
9839         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9840             read_vec_element(s, tcg_op, rn, pass, MO_64);
9841             switch (opcode) {
9842             case 0x3d: /* FRECPE */
9843                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9844                 break;
9845             case 0x3f: /* FRECPX */
9846                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9847                 break;
9848             case 0x7d: /* FRSQRTE */
9849                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9850                 break;
9851             default:
9852                 g_assert_not_reached();
9853             }
9854             write_vec_element(s, tcg_res, rd, pass, MO_64);
9855         }
9856         clear_vec_high(s, !is_scalar, rd);
9857     } else {
9858         TCGv_i32 tcg_op = tcg_temp_new_i32();
9859         TCGv_i32 tcg_res = tcg_temp_new_i32();
9860         int pass, maxpasses;
9861 
9862         if (is_scalar) {
9863             maxpasses = 1;
9864         } else {
9865             maxpasses = is_q ? 4 : 2;
9866         }
9867 
9868         for (pass = 0; pass < maxpasses; pass++) {
9869             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9870 
9871             switch (opcode) {
9872             case 0x3c: /* URECPE */
9873                 gen_helper_recpe_u32(tcg_res, tcg_op);
9874                 break;
9875             case 0x3d: /* FRECPE */
9876                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9877                 break;
9878             case 0x3f: /* FRECPX */
9879                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9880                 break;
9881             case 0x7d: /* FRSQRTE */
9882                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9883                 break;
9884             default:
9885                 g_assert_not_reached();
9886             }
9887 
9888             if (is_scalar) {
9889                 write_fp_sreg(s, rd, tcg_res);
9890             } else {
9891                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9892             }
9893         }
9894         if (!is_scalar) {
9895             clear_vec_high(s, is_q, rd);
9896         }
9897     }
9898 }
9899 
9900 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9901                                 int opcode, bool u, bool is_q,
9902                                 int size, int rn, int rd)
9903 {
9904     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9905      * in the source becomes a size element in the destination).
9906      */
9907     int pass;
9908     TCGv_i32 tcg_res[2];
9909     int destelt = is_q ? 2 : 0;
9910     int passes = scalar ? 1 : 2;
9911 
9912     if (scalar) {
9913         tcg_res[1] = tcg_constant_i32(0);
9914     }
9915 
9916     for (pass = 0; pass < passes; pass++) {
9917         TCGv_i64 tcg_op = tcg_temp_new_i64();
9918         NeonGenNarrowFn *genfn = NULL;
9919         NeonGenNarrowEnvFn *genenvfn = NULL;
9920 
9921         if (scalar) {
9922             read_vec_element(s, tcg_op, rn, pass, size + 1);
9923         } else {
9924             read_vec_element(s, tcg_op, rn, pass, MO_64);
9925         }
9926         tcg_res[pass] = tcg_temp_new_i32();
9927 
9928         switch (opcode) {
9929         case 0x12: /* XTN, SQXTUN */
9930         {
9931             static NeonGenNarrowFn * const xtnfns[3] = {
9932                 gen_helper_neon_narrow_u8,
9933                 gen_helper_neon_narrow_u16,
9934                 tcg_gen_extrl_i64_i32,
9935             };
9936             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9937                 gen_helper_neon_unarrow_sat8,
9938                 gen_helper_neon_unarrow_sat16,
9939                 gen_helper_neon_unarrow_sat32,
9940             };
9941             if (u) {
9942                 genenvfn = sqxtunfns[size];
9943             } else {
9944                 genfn = xtnfns[size];
9945             }
9946             break;
9947         }
9948         case 0x14: /* SQXTN, UQXTN */
9949         {
9950             static NeonGenNarrowEnvFn * const fns[3][2] = {
9951                 { gen_helper_neon_narrow_sat_s8,
9952                   gen_helper_neon_narrow_sat_u8 },
9953                 { gen_helper_neon_narrow_sat_s16,
9954                   gen_helper_neon_narrow_sat_u16 },
9955                 { gen_helper_neon_narrow_sat_s32,
9956                   gen_helper_neon_narrow_sat_u32 },
9957             };
9958             genenvfn = fns[size][u];
9959             break;
9960         }
9961         case 0x16: /* FCVTN, FCVTN2 */
9962             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9963             if (size == 2) {
9964                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
9965             } else {
9966                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9967                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9968                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9969                 TCGv_i32 ahp = get_ahp_flag();
9970 
9971                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9972                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9973                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9974                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9975             }
9976             break;
9977         case 0x36: /* BFCVTN, BFCVTN2 */
9978             {
9979                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9980                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
9981             }
9982             break;
9983         case 0x56:  /* FCVTXN, FCVTXN2 */
9984             /* 64 bit to 32 bit float conversion
9985              * with von Neumann rounding (round to odd)
9986              */
9987             assert(size == 2);
9988             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
9989             break;
9990         default:
9991             g_assert_not_reached();
9992         }
9993 
9994         if (genfn) {
9995             genfn(tcg_res[pass], tcg_op);
9996         } else if (genenvfn) {
9997             genenvfn(tcg_res[pass], cpu_env, tcg_op);
9998         }
9999     }
10000 
10001     for (pass = 0; pass < 2; pass++) {
10002         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
10003     }
10004     clear_vec_high(s, is_q, rd);
10005 }
10006 
10007 /* Remaining saturating accumulating ops */
10008 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
10009                                 bool is_q, int size, int rn, int rd)
10010 {
10011     bool is_double = (size == 3);
10012 
10013     if (is_double) {
10014         TCGv_i64 tcg_rn = tcg_temp_new_i64();
10015         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10016         int pass;
10017 
10018         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10019             read_vec_element(s, tcg_rn, rn, pass, MO_64);
10020             read_vec_element(s, tcg_rd, rd, pass, MO_64);
10021 
10022             if (is_u) { /* USQADD */
10023                 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10024             } else { /* SUQADD */
10025                 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10026             }
10027             write_vec_element(s, tcg_rd, rd, pass, MO_64);
10028         }
10029         clear_vec_high(s, !is_scalar, rd);
10030     } else {
10031         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10032         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10033         int pass, maxpasses;
10034 
10035         if (is_scalar) {
10036             maxpasses = 1;
10037         } else {
10038             maxpasses = is_q ? 4 : 2;
10039         }
10040 
10041         for (pass = 0; pass < maxpasses; pass++) {
10042             if (is_scalar) {
10043                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
10044                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
10045             } else {
10046                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
10047                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10048             }
10049 
10050             if (is_u) { /* USQADD */
10051                 switch (size) {
10052                 case 0:
10053                     gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10054                     break;
10055                 case 1:
10056                     gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10057                     break;
10058                 case 2:
10059                     gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10060                     break;
10061                 default:
10062                     g_assert_not_reached();
10063                 }
10064             } else { /* SUQADD */
10065                 switch (size) {
10066                 case 0:
10067                     gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10068                     break;
10069                 case 1:
10070                     gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10071                     break;
10072                 case 2:
10073                     gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10074                     break;
10075                 default:
10076                     g_assert_not_reached();
10077                 }
10078             }
10079 
10080             if (is_scalar) {
10081                 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
10082             }
10083             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10084         }
10085         clear_vec_high(s, is_q, rd);
10086     }
10087 }
10088 
10089 /* AdvSIMD scalar two reg misc
10090  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
10091  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10092  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
10093  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10094  */
10095 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
10096 {
10097     int rd = extract32(insn, 0, 5);
10098     int rn = extract32(insn, 5, 5);
10099     int opcode = extract32(insn, 12, 5);
10100     int size = extract32(insn, 22, 2);
10101     bool u = extract32(insn, 29, 1);
10102     bool is_fcvt = false;
10103     int rmode;
10104     TCGv_i32 tcg_rmode;
10105     TCGv_ptr tcg_fpstatus;
10106 
10107     switch (opcode) {
10108     case 0x3: /* USQADD / SUQADD*/
10109         if (!fp_access_check(s)) {
10110             return;
10111         }
10112         handle_2misc_satacc(s, true, u, false, size, rn, rd);
10113         return;
10114     case 0x7: /* SQABS / SQNEG */
10115         break;
10116     case 0xa: /* CMLT */
10117         if (u) {
10118             unallocated_encoding(s);
10119             return;
10120         }
10121         /* fall through */
10122     case 0x8: /* CMGT, CMGE */
10123     case 0x9: /* CMEQ, CMLE */
10124     case 0xb: /* ABS, NEG */
10125         if (size != 3) {
10126             unallocated_encoding(s);
10127             return;
10128         }
10129         break;
10130     case 0x12: /* SQXTUN */
10131         if (!u) {
10132             unallocated_encoding(s);
10133             return;
10134         }
10135         /* fall through */
10136     case 0x14: /* SQXTN, UQXTN */
10137         if (size == 3) {
10138             unallocated_encoding(s);
10139             return;
10140         }
10141         if (!fp_access_check(s)) {
10142             return;
10143         }
10144         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10145         return;
10146     case 0xc ... 0xf:
10147     case 0x16 ... 0x1d:
10148     case 0x1f:
10149         /* Floating point: U, size[1] and opcode indicate operation;
10150          * size[0] indicates single or double precision.
10151          */
10152         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10153         size = extract32(size, 0, 1) ? 3 : 2;
10154         switch (opcode) {
10155         case 0x2c: /* FCMGT (zero) */
10156         case 0x2d: /* FCMEQ (zero) */
10157         case 0x2e: /* FCMLT (zero) */
10158         case 0x6c: /* FCMGE (zero) */
10159         case 0x6d: /* FCMLE (zero) */
10160             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10161             return;
10162         case 0x1d: /* SCVTF */
10163         case 0x5d: /* UCVTF */
10164         {
10165             bool is_signed = (opcode == 0x1d);
10166             if (!fp_access_check(s)) {
10167                 return;
10168             }
10169             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10170             return;
10171         }
10172         case 0x3d: /* FRECPE */
10173         case 0x3f: /* FRECPX */
10174         case 0x7d: /* FRSQRTE */
10175             if (!fp_access_check(s)) {
10176                 return;
10177             }
10178             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10179             return;
10180         case 0x1a: /* FCVTNS */
10181         case 0x1b: /* FCVTMS */
10182         case 0x3a: /* FCVTPS */
10183         case 0x3b: /* FCVTZS */
10184         case 0x5a: /* FCVTNU */
10185         case 0x5b: /* FCVTMU */
10186         case 0x7a: /* FCVTPU */
10187         case 0x7b: /* FCVTZU */
10188             is_fcvt = true;
10189             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10190             break;
10191         case 0x1c: /* FCVTAS */
10192         case 0x5c: /* FCVTAU */
10193             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10194             is_fcvt = true;
10195             rmode = FPROUNDING_TIEAWAY;
10196             break;
10197         case 0x56: /* FCVTXN, FCVTXN2 */
10198             if (size == 2) {
10199                 unallocated_encoding(s);
10200                 return;
10201             }
10202             if (!fp_access_check(s)) {
10203                 return;
10204             }
10205             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10206             return;
10207         default:
10208             unallocated_encoding(s);
10209             return;
10210         }
10211         break;
10212     default:
10213         unallocated_encoding(s);
10214         return;
10215     }
10216 
10217     if (!fp_access_check(s)) {
10218         return;
10219     }
10220 
10221     if (is_fcvt) {
10222         tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
10223         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
10224         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
10225     } else {
10226         tcg_rmode = NULL;
10227         tcg_fpstatus = NULL;
10228     }
10229 
10230     if (size == 3) {
10231         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10232         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10233 
10234         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10235         write_fp_dreg(s, rd, tcg_rd);
10236     } else {
10237         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10238         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10239 
10240         read_vec_element_i32(s, tcg_rn, rn, 0, size);
10241 
10242         switch (opcode) {
10243         case 0x7: /* SQABS, SQNEG */
10244         {
10245             NeonGenOneOpEnvFn *genfn;
10246             static NeonGenOneOpEnvFn * const fns[3][2] = {
10247                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10248                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10249                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10250             };
10251             genfn = fns[size][u];
10252             genfn(tcg_rd, cpu_env, tcg_rn);
10253             break;
10254         }
10255         case 0x1a: /* FCVTNS */
10256         case 0x1b: /* FCVTMS */
10257         case 0x1c: /* FCVTAS */
10258         case 0x3a: /* FCVTPS */
10259         case 0x3b: /* FCVTZS */
10260             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10261                                  tcg_fpstatus);
10262             break;
10263         case 0x5a: /* FCVTNU */
10264         case 0x5b: /* FCVTMU */
10265         case 0x5c: /* FCVTAU */
10266         case 0x7a: /* FCVTPU */
10267         case 0x7b: /* FCVTZU */
10268             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10269                                  tcg_fpstatus);
10270             break;
10271         default:
10272             g_assert_not_reached();
10273         }
10274 
10275         write_fp_sreg(s, rd, tcg_rd);
10276     }
10277 
10278     if (is_fcvt) {
10279         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
10280     }
10281 }
10282 
10283 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10284 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10285                                  int immh, int immb, int opcode, int rn, int rd)
10286 {
10287     int size = 32 - clz32(immh) - 1;
10288     int immhb = immh << 3 | immb;
10289     int shift = 2 * (8 << size) - immhb;
10290     GVecGen2iFn *gvec_fn;
10291 
10292     if (extract32(immh, 3, 1) && !is_q) {
10293         unallocated_encoding(s);
10294         return;
10295     }
10296     tcg_debug_assert(size <= 3);
10297 
10298     if (!fp_access_check(s)) {
10299         return;
10300     }
10301 
10302     switch (opcode) {
10303     case 0x02: /* SSRA / USRA (accumulate) */
10304         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10305         break;
10306 
10307     case 0x08: /* SRI */
10308         gvec_fn = gen_gvec_sri;
10309         break;
10310 
10311     case 0x00: /* SSHR / USHR */
10312         if (is_u) {
10313             if (shift == 8 << size) {
10314                 /* Shift count the same size as element size produces zero.  */
10315                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10316                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
10317                 return;
10318             }
10319             gvec_fn = tcg_gen_gvec_shri;
10320         } else {
10321             /* Shift count the same size as element size produces all sign.  */
10322             if (shift == 8 << size) {
10323                 shift -= 1;
10324             }
10325             gvec_fn = tcg_gen_gvec_sari;
10326         }
10327         break;
10328 
10329     case 0x04: /* SRSHR / URSHR (rounding) */
10330         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10331         break;
10332 
10333     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10334         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10335         break;
10336 
10337     default:
10338         g_assert_not_reached();
10339     }
10340 
10341     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10342 }
10343 
10344 /* SHL/SLI - Vector shift left */
10345 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10346                                  int immh, int immb, int opcode, int rn, int rd)
10347 {
10348     int size = 32 - clz32(immh) - 1;
10349     int immhb = immh << 3 | immb;
10350     int shift = immhb - (8 << size);
10351 
10352     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10353     assert(size >= 0 && size <= 3);
10354 
10355     if (extract32(immh, 3, 1) && !is_q) {
10356         unallocated_encoding(s);
10357         return;
10358     }
10359 
10360     if (!fp_access_check(s)) {
10361         return;
10362     }
10363 
10364     if (insert) {
10365         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10366     } else {
10367         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10368     }
10369 }
10370 
10371 /* USHLL/SHLL - Vector shift left with widening */
10372 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10373                                  int immh, int immb, int opcode, int rn, int rd)
10374 {
10375     int size = 32 - clz32(immh) - 1;
10376     int immhb = immh << 3 | immb;
10377     int shift = immhb - (8 << size);
10378     int dsize = 64;
10379     int esize = 8 << size;
10380     int elements = dsize/esize;
10381     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10382     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10383     int i;
10384 
10385     if (size >= 3) {
10386         unallocated_encoding(s);
10387         return;
10388     }
10389 
10390     if (!fp_access_check(s)) {
10391         return;
10392     }
10393 
10394     /* For the LL variants the store is larger than the load,
10395      * so if rd == rn we would overwrite parts of our input.
10396      * So load everything right now and use shifts in the main loop.
10397      */
10398     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10399 
10400     for (i = 0; i < elements; i++) {
10401         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10402         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10403         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10404         write_vec_element(s, tcg_rd, rd, i, size + 1);
10405     }
10406 }
10407 
10408 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10409 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10410                                  int immh, int immb, int opcode, int rn, int rd)
10411 {
10412     int immhb = immh << 3 | immb;
10413     int size = 32 - clz32(immh) - 1;
10414     int dsize = 64;
10415     int esize = 8 << size;
10416     int elements = dsize/esize;
10417     int shift = (2 * esize) - immhb;
10418     bool round = extract32(opcode, 0, 1);
10419     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10420     TCGv_i64 tcg_round;
10421     int i;
10422 
10423     if (extract32(immh, 3, 1)) {
10424         unallocated_encoding(s);
10425         return;
10426     }
10427 
10428     if (!fp_access_check(s)) {
10429         return;
10430     }
10431 
10432     tcg_rn = tcg_temp_new_i64();
10433     tcg_rd = tcg_temp_new_i64();
10434     tcg_final = tcg_temp_new_i64();
10435     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10436 
10437     if (round) {
10438         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10439     } else {
10440         tcg_round = NULL;
10441     }
10442 
10443     for (i = 0; i < elements; i++) {
10444         read_vec_element(s, tcg_rn, rn, i, size+1);
10445         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10446                                 false, true, size+1, shift);
10447 
10448         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10449     }
10450 
10451     if (!is_q) {
10452         write_vec_element(s, tcg_final, rd, 0, MO_64);
10453     } else {
10454         write_vec_element(s, tcg_final, rd, 1, MO_64);
10455     }
10456 
10457     clear_vec_high(s, is_q, rd);
10458 }
10459 
10460 
10461 /* AdvSIMD shift by immediate
10462  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10463  * +---+---+---+-------------+------+------+--------+---+------+------+
10464  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10465  * +---+---+---+-------------+------+------+--------+---+------+------+
10466  */
10467 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10468 {
10469     int rd = extract32(insn, 0, 5);
10470     int rn = extract32(insn, 5, 5);
10471     int opcode = extract32(insn, 11, 5);
10472     int immb = extract32(insn, 16, 3);
10473     int immh = extract32(insn, 19, 4);
10474     bool is_u = extract32(insn, 29, 1);
10475     bool is_q = extract32(insn, 30, 1);
10476 
10477     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10478     assert(immh != 0);
10479 
10480     switch (opcode) {
10481     case 0x08: /* SRI */
10482         if (!is_u) {
10483             unallocated_encoding(s);
10484             return;
10485         }
10486         /* fall through */
10487     case 0x00: /* SSHR / USHR */
10488     case 0x02: /* SSRA / USRA (accumulate) */
10489     case 0x04: /* SRSHR / URSHR (rounding) */
10490     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10491         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10492         break;
10493     case 0x0a: /* SHL / SLI */
10494         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10495         break;
10496     case 0x10: /* SHRN */
10497     case 0x11: /* RSHRN / SQRSHRUN */
10498         if (is_u) {
10499             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10500                                    opcode, rn, rd);
10501         } else {
10502             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10503         }
10504         break;
10505     case 0x12: /* SQSHRN / UQSHRN */
10506     case 0x13: /* SQRSHRN / UQRSHRN */
10507         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10508                                opcode, rn, rd);
10509         break;
10510     case 0x14: /* SSHLL / USHLL */
10511         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10512         break;
10513     case 0x1c: /* SCVTF / UCVTF */
10514         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10515                                      opcode, rn, rd);
10516         break;
10517     case 0xc: /* SQSHLU */
10518         if (!is_u) {
10519             unallocated_encoding(s);
10520             return;
10521         }
10522         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10523         break;
10524     case 0xe: /* SQSHL, UQSHL */
10525         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10526         break;
10527     case 0x1f: /* FCVTZS/ FCVTZU */
10528         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10529         return;
10530     default:
10531         unallocated_encoding(s);
10532         return;
10533     }
10534 }
10535 
10536 /* Generate code to do a "long" addition or subtraction, ie one done in
10537  * TCGv_i64 on vector lanes twice the width specified by size.
10538  */
10539 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10540                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10541 {
10542     static NeonGenTwo64OpFn * const fns[3][2] = {
10543         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10544         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10545         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10546     };
10547     NeonGenTwo64OpFn *genfn;
10548     assert(size < 3);
10549 
10550     genfn = fns[size][is_sub];
10551     genfn(tcg_res, tcg_op1, tcg_op2);
10552 }
10553 
10554 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10555                                 int opcode, int rd, int rn, int rm)
10556 {
10557     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10558     TCGv_i64 tcg_res[2];
10559     int pass, accop;
10560 
10561     tcg_res[0] = tcg_temp_new_i64();
10562     tcg_res[1] = tcg_temp_new_i64();
10563 
10564     /* Does this op do an adding accumulate, a subtracting accumulate,
10565      * or no accumulate at all?
10566      */
10567     switch (opcode) {
10568     case 5:
10569     case 8:
10570     case 9:
10571         accop = 1;
10572         break;
10573     case 10:
10574     case 11:
10575         accop = -1;
10576         break;
10577     default:
10578         accop = 0;
10579         break;
10580     }
10581 
10582     if (accop != 0) {
10583         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10584         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10585     }
10586 
10587     /* size == 2 means two 32x32->64 operations; this is worth special
10588      * casing because we can generally handle it inline.
10589      */
10590     if (size == 2) {
10591         for (pass = 0; pass < 2; pass++) {
10592             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10593             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10594             TCGv_i64 tcg_passres;
10595             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10596 
10597             int elt = pass + is_q * 2;
10598 
10599             read_vec_element(s, tcg_op1, rn, elt, memop);
10600             read_vec_element(s, tcg_op2, rm, elt, memop);
10601 
10602             if (accop == 0) {
10603                 tcg_passres = tcg_res[pass];
10604             } else {
10605                 tcg_passres = tcg_temp_new_i64();
10606             }
10607 
10608             switch (opcode) {
10609             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10610                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10611                 break;
10612             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10613                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10614                 break;
10615             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10616             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10617             {
10618                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10619                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10620 
10621                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10622                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10623                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10624                                     tcg_passres,
10625                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10626                 break;
10627             }
10628             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10629             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10630             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10631                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10632                 break;
10633             case 9: /* SQDMLAL, SQDMLAL2 */
10634             case 11: /* SQDMLSL, SQDMLSL2 */
10635             case 13: /* SQDMULL, SQDMULL2 */
10636                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10637                 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10638                                                   tcg_passres, tcg_passres);
10639                 break;
10640             default:
10641                 g_assert_not_reached();
10642             }
10643 
10644             if (opcode == 9 || opcode == 11) {
10645                 /* saturating accumulate ops */
10646                 if (accop < 0) {
10647                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10648                 }
10649                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10650                                                   tcg_res[pass], tcg_passres);
10651             } else if (accop > 0) {
10652                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10653             } else if (accop < 0) {
10654                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10655             }
10656         }
10657     } else {
10658         /* size 0 or 1, generally helper functions */
10659         for (pass = 0; pass < 2; pass++) {
10660             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10661             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10662             TCGv_i64 tcg_passres;
10663             int elt = pass + is_q * 2;
10664 
10665             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10666             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10667 
10668             if (accop == 0) {
10669                 tcg_passres = tcg_res[pass];
10670             } else {
10671                 tcg_passres = tcg_temp_new_i64();
10672             }
10673 
10674             switch (opcode) {
10675             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10676             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10677             {
10678                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10679                 static NeonGenWidenFn * const widenfns[2][2] = {
10680                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10681                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10682                 };
10683                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10684 
10685                 widenfn(tcg_op2_64, tcg_op2);
10686                 widenfn(tcg_passres, tcg_op1);
10687                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10688                               tcg_passres, tcg_op2_64);
10689                 break;
10690             }
10691             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10692             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10693                 if (size == 0) {
10694                     if (is_u) {
10695                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10696                     } else {
10697                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10698                     }
10699                 } else {
10700                     if (is_u) {
10701                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10702                     } else {
10703                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10704                     }
10705                 }
10706                 break;
10707             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10708             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10709             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10710                 if (size == 0) {
10711                     if (is_u) {
10712                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10713                     } else {
10714                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10715                     }
10716                 } else {
10717                     if (is_u) {
10718                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10719                     } else {
10720                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10721                     }
10722                 }
10723                 break;
10724             case 9: /* SQDMLAL, SQDMLAL2 */
10725             case 11: /* SQDMLSL, SQDMLSL2 */
10726             case 13: /* SQDMULL, SQDMULL2 */
10727                 assert(size == 1);
10728                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10729                 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10730                                                   tcg_passres, tcg_passres);
10731                 break;
10732             default:
10733                 g_assert_not_reached();
10734             }
10735 
10736             if (accop != 0) {
10737                 if (opcode == 9 || opcode == 11) {
10738                     /* saturating accumulate ops */
10739                     if (accop < 0) {
10740                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10741                     }
10742                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10743                                                       tcg_res[pass],
10744                                                       tcg_passres);
10745                 } else {
10746                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10747                                   tcg_res[pass], tcg_passres);
10748                 }
10749             }
10750         }
10751     }
10752 
10753     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10754     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10755 }
10756 
10757 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10758                             int opcode, int rd, int rn, int rm)
10759 {
10760     TCGv_i64 tcg_res[2];
10761     int part = is_q ? 2 : 0;
10762     int pass;
10763 
10764     for (pass = 0; pass < 2; pass++) {
10765         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10766         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10767         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10768         static NeonGenWidenFn * const widenfns[3][2] = {
10769             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10770             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10771             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10772         };
10773         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10774 
10775         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10776         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10777         widenfn(tcg_op2_wide, tcg_op2);
10778         tcg_res[pass] = tcg_temp_new_i64();
10779         gen_neon_addl(size, (opcode == 3),
10780                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10781     }
10782 
10783     for (pass = 0; pass < 2; pass++) {
10784         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10785     }
10786 }
10787 
10788 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10789 {
10790     tcg_gen_addi_i64(in, in, 1U << 31);
10791     tcg_gen_extrh_i64_i32(res, in);
10792 }
10793 
10794 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10795                                  int opcode, int rd, int rn, int rm)
10796 {
10797     TCGv_i32 tcg_res[2];
10798     int part = is_q ? 2 : 0;
10799     int pass;
10800 
10801     for (pass = 0; pass < 2; pass++) {
10802         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10803         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10804         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10805         static NeonGenNarrowFn * const narrowfns[3][2] = {
10806             { gen_helper_neon_narrow_high_u8,
10807               gen_helper_neon_narrow_round_high_u8 },
10808             { gen_helper_neon_narrow_high_u16,
10809               gen_helper_neon_narrow_round_high_u16 },
10810             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10811         };
10812         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10813 
10814         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10815         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10816 
10817         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10818 
10819         tcg_res[pass] = tcg_temp_new_i32();
10820         gennarrow(tcg_res[pass], tcg_wideres);
10821     }
10822 
10823     for (pass = 0; pass < 2; pass++) {
10824         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10825     }
10826     clear_vec_high(s, is_q, rd);
10827 }
10828 
10829 /* AdvSIMD three different
10830  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10831  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10832  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10833  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10834  */
10835 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10836 {
10837     /* Instructions in this group fall into three basic classes
10838      * (in each case with the operation working on each element in
10839      * the input vectors):
10840      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10841      *     128 bit input)
10842      * (2) wide 64 x 128 -> 128
10843      * (3) narrowing 128 x 128 -> 64
10844      * Here we do initial decode, catch unallocated cases and
10845      * dispatch to separate functions for each class.
10846      */
10847     int is_q = extract32(insn, 30, 1);
10848     int is_u = extract32(insn, 29, 1);
10849     int size = extract32(insn, 22, 2);
10850     int opcode = extract32(insn, 12, 4);
10851     int rm = extract32(insn, 16, 5);
10852     int rn = extract32(insn, 5, 5);
10853     int rd = extract32(insn, 0, 5);
10854 
10855     switch (opcode) {
10856     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10857     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10858         /* 64 x 128 -> 128 */
10859         if (size == 3) {
10860             unallocated_encoding(s);
10861             return;
10862         }
10863         if (!fp_access_check(s)) {
10864             return;
10865         }
10866         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10867         break;
10868     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10869     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10870         /* 128 x 128 -> 64 */
10871         if (size == 3) {
10872             unallocated_encoding(s);
10873             return;
10874         }
10875         if (!fp_access_check(s)) {
10876             return;
10877         }
10878         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10879         break;
10880     case 14: /* PMULL, PMULL2 */
10881         if (is_u) {
10882             unallocated_encoding(s);
10883             return;
10884         }
10885         switch (size) {
10886         case 0: /* PMULL.P8 */
10887             if (!fp_access_check(s)) {
10888                 return;
10889             }
10890             /* The Q field specifies lo/hi half input for this insn.  */
10891             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10892                              gen_helper_neon_pmull_h);
10893             break;
10894 
10895         case 3: /* PMULL.P64 */
10896             if (!dc_isar_feature(aa64_pmull, s)) {
10897                 unallocated_encoding(s);
10898                 return;
10899             }
10900             if (!fp_access_check(s)) {
10901                 return;
10902             }
10903             /* The Q field specifies lo/hi half input for this insn.  */
10904             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10905                              gen_helper_gvec_pmull_q);
10906             break;
10907 
10908         default:
10909             unallocated_encoding(s);
10910             break;
10911         }
10912         return;
10913     case 9: /* SQDMLAL, SQDMLAL2 */
10914     case 11: /* SQDMLSL, SQDMLSL2 */
10915     case 13: /* SQDMULL, SQDMULL2 */
10916         if (is_u || size == 0) {
10917             unallocated_encoding(s);
10918             return;
10919         }
10920         /* fall through */
10921     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10922     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10923     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10924     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10925     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10926     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10927     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10928         /* 64 x 64 -> 128 */
10929         if (size == 3) {
10930             unallocated_encoding(s);
10931             return;
10932         }
10933         if (!fp_access_check(s)) {
10934             return;
10935         }
10936 
10937         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10938         break;
10939     default:
10940         /* opcode 15 not allocated */
10941         unallocated_encoding(s);
10942         break;
10943     }
10944 }
10945 
10946 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10947 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10948 {
10949     int rd = extract32(insn, 0, 5);
10950     int rn = extract32(insn, 5, 5);
10951     int rm = extract32(insn, 16, 5);
10952     int size = extract32(insn, 22, 2);
10953     bool is_u = extract32(insn, 29, 1);
10954     bool is_q = extract32(insn, 30, 1);
10955 
10956     if (!fp_access_check(s)) {
10957         return;
10958     }
10959 
10960     switch (size + 4 * is_u) {
10961     case 0: /* AND */
10962         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10963         return;
10964     case 1: /* BIC */
10965         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10966         return;
10967     case 2: /* ORR */
10968         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10969         return;
10970     case 3: /* ORN */
10971         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10972         return;
10973     case 4: /* EOR */
10974         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10975         return;
10976 
10977     case 5: /* BSL bitwise select */
10978         gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
10979         return;
10980     case 6: /* BIT, bitwise insert if true */
10981         gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
10982         return;
10983     case 7: /* BIF, bitwise insert if false */
10984         gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
10985         return;
10986 
10987     default:
10988         g_assert_not_reached();
10989     }
10990 }
10991 
10992 /* Pairwise op subgroup of C3.6.16.
10993  *
10994  * This is called directly or via the handle_3same_float for float pairwise
10995  * operations where the opcode and size are calculated differently.
10996  */
10997 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
10998                                    int size, int rn, int rm, int rd)
10999 {
11000     TCGv_ptr fpst;
11001     int pass;
11002 
11003     /* Floating point operations need fpst */
11004     if (opcode >= 0x58) {
11005         fpst = fpstatus_ptr(FPST_FPCR);
11006     } else {
11007         fpst = NULL;
11008     }
11009 
11010     if (!fp_access_check(s)) {
11011         return;
11012     }
11013 
11014     /* These operations work on the concatenated rm:rn, with each pair of
11015      * adjacent elements being operated on to produce an element in the result.
11016      */
11017     if (size == 3) {
11018         TCGv_i64 tcg_res[2];
11019 
11020         for (pass = 0; pass < 2; pass++) {
11021             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11022             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11023             int passreg = (pass == 0) ? rn : rm;
11024 
11025             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
11026             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
11027             tcg_res[pass] = tcg_temp_new_i64();
11028 
11029             switch (opcode) {
11030             case 0x17: /* ADDP */
11031                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11032                 break;
11033             case 0x58: /* FMAXNMP */
11034                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11035                 break;
11036             case 0x5a: /* FADDP */
11037                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11038                 break;
11039             case 0x5e: /* FMAXP */
11040                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11041                 break;
11042             case 0x78: /* FMINNMP */
11043                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11044                 break;
11045             case 0x7e: /* FMINP */
11046                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11047                 break;
11048             default:
11049                 g_assert_not_reached();
11050             }
11051         }
11052 
11053         for (pass = 0; pass < 2; pass++) {
11054             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11055         }
11056     } else {
11057         int maxpass = is_q ? 4 : 2;
11058         TCGv_i32 tcg_res[4];
11059 
11060         for (pass = 0; pass < maxpass; pass++) {
11061             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11062             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11063             NeonGenTwoOpFn *genfn = NULL;
11064             int passreg = pass < (maxpass / 2) ? rn : rm;
11065             int passelt = (is_q && (pass & 1)) ? 2 : 0;
11066 
11067             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
11068             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
11069             tcg_res[pass] = tcg_temp_new_i32();
11070 
11071             switch (opcode) {
11072             case 0x17: /* ADDP */
11073             {
11074                 static NeonGenTwoOpFn * const fns[3] = {
11075                     gen_helper_neon_padd_u8,
11076                     gen_helper_neon_padd_u16,
11077                     tcg_gen_add_i32,
11078                 };
11079                 genfn = fns[size];
11080                 break;
11081             }
11082             case 0x14: /* SMAXP, UMAXP */
11083             {
11084                 static NeonGenTwoOpFn * const fns[3][2] = {
11085                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
11086                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
11087                     { tcg_gen_smax_i32, tcg_gen_umax_i32 },
11088                 };
11089                 genfn = fns[size][u];
11090                 break;
11091             }
11092             case 0x15: /* SMINP, UMINP */
11093             {
11094                 static NeonGenTwoOpFn * const fns[3][2] = {
11095                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
11096                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
11097                     { tcg_gen_smin_i32, tcg_gen_umin_i32 },
11098                 };
11099                 genfn = fns[size][u];
11100                 break;
11101             }
11102             /* The FP operations are all on single floats (32 bit) */
11103             case 0x58: /* FMAXNMP */
11104                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11105                 break;
11106             case 0x5a: /* FADDP */
11107                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11108                 break;
11109             case 0x5e: /* FMAXP */
11110                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11111                 break;
11112             case 0x78: /* FMINNMP */
11113                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11114                 break;
11115             case 0x7e: /* FMINP */
11116                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11117                 break;
11118             default:
11119                 g_assert_not_reached();
11120             }
11121 
11122             /* FP ops called directly, otherwise call now */
11123             if (genfn) {
11124                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
11125             }
11126         }
11127 
11128         for (pass = 0; pass < maxpass; pass++) {
11129             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11130         }
11131         clear_vec_high(s, is_q, rd);
11132     }
11133 }
11134 
11135 /* Floating point op subgroup of C3.6.16. */
11136 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
11137 {
11138     /* For floating point ops, the U, size[1] and opcode bits
11139      * together indicate the operation. size[0] indicates single
11140      * or double.
11141      */
11142     int fpopcode = extract32(insn, 11, 5)
11143         | (extract32(insn, 23, 1) << 5)
11144         | (extract32(insn, 29, 1) << 6);
11145     int is_q = extract32(insn, 30, 1);
11146     int size = extract32(insn, 22, 1);
11147     int rm = extract32(insn, 16, 5);
11148     int rn = extract32(insn, 5, 5);
11149     int rd = extract32(insn, 0, 5);
11150 
11151     int datasize = is_q ? 128 : 64;
11152     int esize = 32 << size;
11153     int elements = datasize / esize;
11154 
11155     if (size == 1 && !is_q) {
11156         unallocated_encoding(s);
11157         return;
11158     }
11159 
11160     switch (fpopcode) {
11161     case 0x58: /* FMAXNMP */
11162     case 0x5a: /* FADDP */
11163     case 0x5e: /* FMAXP */
11164     case 0x78: /* FMINNMP */
11165     case 0x7e: /* FMINP */
11166         if (size && !is_q) {
11167             unallocated_encoding(s);
11168             return;
11169         }
11170         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11171                                rn, rm, rd);
11172         return;
11173     case 0x1b: /* FMULX */
11174     case 0x1f: /* FRECPS */
11175     case 0x3f: /* FRSQRTS */
11176     case 0x5d: /* FACGE */
11177     case 0x7d: /* FACGT */
11178     case 0x19: /* FMLA */
11179     case 0x39: /* FMLS */
11180     case 0x18: /* FMAXNM */
11181     case 0x1a: /* FADD */
11182     case 0x1c: /* FCMEQ */
11183     case 0x1e: /* FMAX */
11184     case 0x38: /* FMINNM */
11185     case 0x3a: /* FSUB */
11186     case 0x3e: /* FMIN */
11187     case 0x5b: /* FMUL */
11188     case 0x5c: /* FCMGE */
11189     case 0x5f: /* FDIV */
11190     case 0x7a: /* FABD */
11191     case 0x7c: /* FCMGT */
11192         if (!fp_access_check(s)) {
11193             return;
11194         }
11195         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11196         return;
11197 
11198     case 0x1d: /* FMLAL  */
11199     case 0x3d: /* FMLSL  */
11200     case 0x59: /* FMLAL2 */
11201     case 0x79: /* FMLSL2 */
11202         if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11203             unallocated_encoding(s);
11204             return;
11205         }
11206         if (fp_access_check(s)) {
11207             int is_s = extract32(insn, 23, 1);
11208             int is_2 = extract32(insn, 29, 1);
11209             int data = (is_2 << 1) | is_s;
11210             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11211                                vec_full_reg_offset(s, rn),
11212                                vec_full_reg_offset(s, rm), cpu_env,
11213                                is_q ? 16 : 8, vec_full_reg_size(s),
11214                                data, gen_helper_gvec_fmlal_a64);
11215         }
11216         return;
11217 
11218     default:
11219         unallocated_encoding(s);
11220         return;
11221     }
11222 }
11223 
11224 /* Integer op subgroup of C3.6.16. */
11225 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11226 {
11227     int is_q = extract32(insn, 30, 1);
11228     int u = extract32(insn, 29, 1);
11229     int size = extract32(insn, 22, 2);
11230     int opcode = extract32(insn, 11, 5);
11231     int rm = extract32(insn, 16, 5);
11232     int rn = extract32(insn, 5, 5);
11233     int rd = extract32(insn, 0, 5);
11234     int pass;
11235     TCGCond cond;
11236 
11237     switch (opcode) {
11238     case 0x13: /* MUL, PMUL */
11239         if (u && size != 0) {
11240             unallocated_encoding(s);
11241             return;
11242         }
11243         /* fall through */
11244     case 0x0: /* SHADD, UHADD */
11245     case 0x2: /* SRHADD, URHADD */
11246     case 0x4: /* SHSUB, UHSUB */
11247     case 0xc: /* SMAX, UMAX */
11248     case 0xd: /* SMIN, UMIN */
11249     case 0xe: /* SABD, UABD */
11250     case 0xf: /* SABA, UABA */
11251     case 0x12: /* MLA, MLS */
11252         if (size == 3) {
11253             unallocated_encoding(s);
11254             return;
11255         }
11256         break;
11257     case 0x16: /* SQDMULH, SQRDMULH */
11258         if (size == 0 || size == 3) {
11259             unallocated_encoding(s);
11260             return;
11261         }
11262         break;
11263     default:
11264         if (size == 3 && !is_q) {
11265             unallocated_encoding(s);
11266             return;
11267         }
11268         break;
11269     }
11270 
11271     if (!fp_access_check(s)) {
11272         return;
11273     }
11274 
11275     switch (opcode) {
11276     case 0x01: /* SQADD, UQADD */
11277         if (u) {
11278             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11279         } else {
11280             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11281         }
11282         return;
11283     case 0x05: /* SQSUB, UQSUB */
11284         if (u) {
11285             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11286         } else {
11287             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11288         }
11289         return;
11290     case 0x08: /* SSHL, USHL */
11291         if (u) {
11292             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11293         } else {
11294             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11295         }
11296         return;
11297     case 0x0c: /* SMAX, UMAX */
11298         if (u) {
11299             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11300         } else {
11301             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11302         }
11303         return;
11304     case 0x0d: /* SMIN, UMIN */
11305         if (u) {
11306             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11307         } else {
11308             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11309         }
11310         return;
11311     case 0xe: /* SABD, UABD */
11312         if (u) {
11313             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11314         } else {
11315             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11316         }
11317         return;
11318     case 0xf: /* SABA, UABA */
11319         if (u) {
11320             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11321         } else {
11322             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11323         }
11324         return;
11325     case 0x10: /* ADD, SUB */
11326         if (u) {
11327             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11328         } else {
11329             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11330         }
11331         return;
11332     case 0x13: /* MUL, PMUL */
11333         if (!u) { /* MUL */
11334             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11335         } else {  /* PMUL */
11336             gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11337         }
11338         return;
11339     case 0x12: /* MLA, MLS */
11340         if (u) {
11341             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11342         } else {
11343             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11344         }
11345         return;
11346     case 0x16: /* SQDMULH, SQRDMULH */
11347         {
11348             static gen_helper_gvec_3_ptr * const fns[2][2] = {
11349                 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11350                 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11351             };
11352             gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11353         }
11354         return;
11355     case 0x11:
11356         if (!u) { /* CMTST */
11357             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11358             return;
11359         }
11360         /* else CMEQ */
11361         cond = TCG_COND_EQ;
11362         goto do_gvec_cmp;
11363     case 0x06: /* CMGT, CMHI */
11364         cond = u ? TCG_COND_GTU : TCG_COND_GT;
11365         goto do_gvec_cmp;
11366     case 0x07: /* CMGE, CMHS */
11367         cond = u ? TCG_COND_GEU : TCG_COND_GE;
11368     do_gvec_cmp:
11369         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11370                          vec_full_reg_offset(s, rn),
11371                          vec_full_reg_offset(s, rm),
11372                          is_q ? 16 : 8, vec_full_reg_size(s));
11373         return;
11374     }
11375 
11376     if (size == 3) {
11377         assert(is_q);
11378         for (pass = 0; pass < 2; pass++) {
11379             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11380             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11381             TCGv_i64 tcg_res = tcg_temp_new_i64();
11382 
11383             read_vec_element(s, tcg_op1, rn, pass, MO_64);
11384             read_vec_element(s, tcg_op2, rm, pass, MO_64);
11385 
11386             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11387 
11388             write_vec_element(s, tcg_res, rd, pass, MO_64);
11389         }
11390     } else {
11391         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11392             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11393             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11394             TCGv_i32 tcg_res = tcg_temp_new_i32();
11395             NeonGenTwoOpFn *genfn = NULL;
11396             NeonGenTwoOpEnvFn *genenvfn = NULL;
11397 
11398             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11399             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11400 
11401             switch (opcode) {
11402             case 0x0: /* SHADD, UHADD */
11403             {
11404                 static NeonGenTwoOpFn * const fns[3][2] = {
11405                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11406                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11407                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11408                 };
11409                 genfn = fns[size][u];
11410                 break;
11411             }
11412             case 0x2: /* SRHADD, URHADD */
11413             {
11414                 static NeonGenTwoOpFn * const fns[3][2] = {
11415                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11416                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11417                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11418                 };
11419                 genfn = fns[size][u];
11420                 break;
11421             }
11422             case 0x4: /* SHSUB, UHSUB */
11423             {
11424                 static NeonGenTwoOpFn * const fns[3][2] = {
11425                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11426                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11427                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11428                 };
11429                 genfn = fns[size][u];
11430                 break;
11431             }
11432             case 0x9: /* SQSHL, UQSHL */
11433             {
11434                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11435                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11436                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11437                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11438                 };
11439                 genenvfn = fns[size][u];
11440                 break;
11441             }
11442             case 0xa: /* SRSHL, URSHL */
11443             {
11444                 static NeonGenTwoOpFn * const fns[3][2] = {
11445                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11446                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11447                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11448                 };
11449                 genfn = fns[size][u];
11450                 break;
11451             }
11452             case 0xb: /* SQRSHL, UQRSHL */
11453             {
11454                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11455                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11456                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11457                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11458                 };
11459                 genenvfn = fns[size][u];
11460                 break;
11461             }
11462             default:
11463                 g_assert_not_reached();
11464             }
11465 
11466             if (genenvfn) {
11467                 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11468             } else {
11469                 genfn(tcg_res, tcg_op1, tcg_op2);
11470             }
11471 
11472             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11473         }
11474     }
11475     clear_vec_high(s, is_q, rd);
11476 }
11477 
11478 /* AdvSIMD three same
11479  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
11480  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11481  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
11482  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11483  */
11484 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11485 {
11486     int opcode = extract32(insn, 11, 5);
11487 
11488     switch (opcode) {
11489     case 0x3: /* logic ops */
11490         disas_simd_3same_logic(s, insn);
11491         break;
11492     case 0x17: /* ADDP */
11493     case 0x14: /* SMAXP, UMAXP */
11494     case 0x15: /* SMINP, UMINP */
11495     {
11496         /* Pairwise operations */
11497         int is_q = extract32(insn, 30, 1);
11498         int u = extract32(insn, 29, 1);
11499         int size = extract32(insn, 22, 2);
11500         int rm = extract32(insn, 16, 5);
11501         int rn = extract32(insn, 5, 5);
11502         int rd = extract32(insn, 0, 5);
11503         if (opcode == 0x17) {
11504             if (u || (size == 3 && !is_q)) {
11505                 unallocated_encoding(s);
11506                 return;
11507             }
11508         } else {
11509             if (size == 3) {
11510                 unallocated_encoding(s);
11511                 return;
11512             }
11513         }
11514         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11515         break;
11516     }
11517     case 0x18 ... 0x31:
11518         /* floating point ops, sz[1] and U are part of opcode */
11519         disas_simd_3same_float(s, insn);
11520         break;
11521     default:
11522         disas_simd_3same_int(s, insn);
11523         break;
11524     }
11525 }
11526 
11527 /*
11528  * Advanced SIMD three same (ARMv8.2 FP16 variants)
11529  *
11530  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
11531  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11532  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
11533  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11534  *
11535  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11536  * (register), FACGE, FABD, FCMGT (register) and FACGT.
11537  *
11538  */
11539 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11540 {
11541     int opcode = extract32(insn, 11, 3);
11542     int u = extract32(insn, 29, 1);
11543     int a = extract32(insn, 23, 1);
11544     int is_q = extract32(insn, 30, 1);
11545     int rm = extract32(insn, 16, 5);
11546     int rn = extract32(insn, 5, 5);
11547     int rd = extract32(insn, 0, 5);
11548     /*
11549      * For these floating point ops, the U, a and opcode bits
11550      * together indicate the operation.
11551      */
11552     int fpopcode = opcode | (a << 3) | (u << 4);
11553     int datasize = is_q ? 128 : 64;
11554     int elements = datasize / 16;
11555     bool pairwise;
11556     TCGv_ptr fpst;
11557     int pass;
11558 
11559     switch (fpopcode) {
11560     case 0x0: /* FMAXNM */
11561     case 0x1: /* FMLA */
11562     case 0x2: /* FADD */
11563     case 0x3: /* FMULX */
11564     case 0x4: /* FCMEQ */
11565     case 0x6: /* FMAX */
11566     case 0x7: /* FRECPS */
11567     case 0x8: /* FMINNM */
11568     case 0x9: /* FMLS */
11569     case 0xa: /* FSUB */
11570     case 0xe: /* FMIN */
11571     case 0xf: /* FRSQRTS */
11572     case 0x13: /* FMUL */
11573     case 0x14: /* FCMGE */
11574     case 0x15: /* FACGE */
11575     case 0x17: /* FDIV */
11576     case 0x1a: /* FABD */
11577     case 0x1c: /* FCMGT */
11578     case 0x1d: /* FACGT */
11579         pairwise = false;
11580         break;
11581     case 0x10: /* FMAXNMP */
11582     case 0x12: /* FADDP */
11583     case 0x16: /* FMAXP */
11584     case 0x18: /* FMINNMP */
11585     case 0x1e: /* FMINP */
11586         pairwise = true;
11587         break;
11588     default:
11589         unallocated_encoding(s);
11590         return;
11591     }
11592 
11593     if (!dc_isar_feature(aa64_fp16, s)) {
11594         unallocated_encoding(s);
11595         return;
11596     }
11597 
11598     if (!fp_access_check(s)) {
11599         return;
11600     }
11601 
11602     fpst = fpstatus_ptr(FPST_FPCR_F16);
11603 
11604     if (pairwise) {
11605         int maxpass = is_q ? 8 : 4;
11606         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11607         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11608         TCGv_i32 tcg_res[8];
11609 
11610         for (pass = 0; pass < maxpass; pass++) {
11611             int passreg = pass < (maxpass / 2) ? rn : rm;
11612             int passelt = (pass << 1) & (maxpass - 1);
11613 
11614             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11615             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11616             tcg_res[pass] = tcg_temp_new_i32();
11617 
11618             switch (fpopcode) {
11619             case 0x10: /* FMAXNMP */
11620                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11621                                            fpst);
11622                 break;
11623             case 0x12: /* FADDP */
11624                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11625                 break;
11626             case 0x16: /* FMAXP */
11627                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11628                 break;
11629             case 0x18: /* FMINNMP */
11630                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11631                                            fpst);
11632                 break;
11633             case 0x1e: /* FMINP */
11634                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11635                 break;
11636             default:
11637                 g_assert_not_reached();
11638             }
11639         }
11640 
11641         for (pass = 0; pass < maxpass; pass++) {
11642             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11643         }
11644     } else {
11645         for (pass = 0; pass < elements; pass++) {
11646             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11647             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11648             TCGv_i32 tcg_res = tcg_temp_new_i32();
11649 
11650             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11651             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11652 
11653             switch (fpopcode) {
11654             case 0x0: /* FMAXNM */
11655                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11656                 break;
11657             case 0x1: /* FMLA */
11658                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11659                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11660                                            fpst);
11661                 break;
11662             case 0x2: /* FADD */
11663                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11664                 break;
11665             case 0x3: /* FMULX */
11666                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11667                 break;
11668             case 0x4: /* FCMEQ */
11669                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11670                 break;
11671             case 0x6: /* FMAX */
11672                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11673                 break;
11674             case 0x7: /* FRECPS */
11675                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11676                 break;
11677             case 0x8: /* FMINNM */
11678                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11679                 break;
11680             case 0x9: /* FMLS */
11681                 /* As usual for ARM, separate negation for fused multiply-add */
11682                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11683                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11684                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11685                                            fpst);
11686                 break;
11687             case 0xa: /* FSUB */
11688                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11689                 break;
11690             case 0xe: /* FMIN */
11691                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11692                 break;
11693             case 0xf: /* FRSQRTS */
11694                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11695                 break;
11696             case 0x13: /* FMUL */
11697                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11698                 break;
11699             case 0x14: /* FCMGE */
11700                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11701                 break;
11702             case 0x15: /* FACGE */
11703                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11704                 break;
11705             case 0x17: /* FDIV */
11706                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11707                 break;
11708             case 0x1a: /* FABD */
11709                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11710                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11711                 break;
11712             case 0x1c: /* FCMGT */
11713                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11714                 break;
11715             case 0x1d: /* FACGT */
11716                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11717                 break;
11718             default:
11719                 g_assert_not_reached();
11720             }
11721 
11722             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11723         }
11724     }
11725 
11726     clear_vec_high(s, is_q, rd);
11727 }
11728 
11729 /* AdvSIMD three same extra
11730  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
11731  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11732  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
11733  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11734  */
11735 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11736 {
11737     int rd = extract32(insn, 0, 5);
11738     int rn = extract32(insn, 5, 5);
11739     int opcode = extract32(insn, 11, 4);
11740     int rm = extract32(insn, 16, 5);
11741     int size = extract32(insn, 22, 2);
11742     bool u = extract32(insn, 29, 1);
11743     bool is_q = extract32(insn, 30, 1);
11744     bool feature;
11745     int rot;
11746 
11747     switch (u * 16 + opcode) {
11748     case 0x10: /* SQRDMLAH (vector) */
11749     case 0x11: /* SQRDMLSH (vector) */
11750         if (size != 1 && size != 2) {
11751             unallocated_encoding(s);
11752             return;
11753         }
11754         feature = dc_isar_feature(aa64_rdm, s);
11755         break;
11756     case 0x02: /* SDOT (vector) */
11757     case 0x12: /* UDOT (vector) */
11758         if (size != MO_32) {
11759             unallocated_encoding(s);
11760             return;
11761         }
11762         feature = dc_isar_feature(aa64_dp, s);
11763         break;
11764     case 0x03: /* USDOT */
11765         if (size != MO_32) {
11766             unallocated_encoding(s);
11767             return;
11768         }
11769         feature = dc_isar_feature(aa64_i8mm, s);
11770         break;
11771     case 0x04: /* SMMLA */
11772     case 0x14: /* UMMLA */
11773     case 0x05: /* USMMLA */
11774         if (!is_q || size != MO_32) {
11775             unallocated_encoding(s);
11776             return;
11777         }
11778         feature = dc_isar_feature(aa64_i8mm, s);
11779         break;
11780     case 0x18: /* FCMLA, #0 */
11781     case 0x19: /* FCMLA, #90 */
11782     case 0x1a: /* FCMLA, #180 */
11783     case 0x1b: /* FCMLA, #270 */
11784     case 0x1c: /* FCADD, #90 */
11785     case 0x1e: /* FCADD, #270 */
11786         if (size == 0
11787             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11788             || (size == 3 && !is_q)) {
11789             unallocated_encoding(s);
11790             return;
11791         }
11792         feature = dc_isar_feature(aa64_fcma, s);
11793         break;
11794     case 0x1d: /* BFMMLA */
11795         if (size != MO_16 || !is_q) {
11796             unallocated_encoding(s);
11797             return;
11798         }
11799         feature = dc_isar_feature(aa64_bf16, s);
11800         break;
11801     case 0x1f:
11802         switch (size) {
11803         case 1: /* BFDOT */
11804         case 3: /* BFMLAL{B,T} */
11805             feature = dc_isar_feature(aa64_bf16, s);
11806             break;
11807         default:
11808             unallocated_encoding(s);
11809             return;
11810         }
11811         break;
11812     default:
11813         unallocated_encoding(s);
11814         return;
11815     }
11816     if (!feature) {
11817         unallocated_encoding(s);
11818         return;
11819     }
11820     if (!fp_access_check(s)) {
11821         return;
11822     }
11823 
11824     switch (opcode) {
11825     case 0x0: /* SQRDMLAH (vector) */
11826         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11827         return;
11828 
11829     case 0x1: /* SQRDMLSH (vector) */
11830         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11831         return;
11832 
11833     case 0x2: /* SDOT / UDOT */
11834         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11835                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11836         return;
11837 
11838     case 0x3: /* USDOT */
11839         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11840         return;
11841 
11842     case 0x04: /* SMMLA, UMMLA */
11843         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11844                          u ? gen_helper_gvec_ummla_b
11845                          : gen_helper_gvec_smmla_b);
11846         return;
11847     case 0x05: /* USMMLA */
11848         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11849         return;
11850 
11851     case 0x8: /* FCMLA, #0 */
11852     case 0x9: /* FCMLA, #90 */
11853     case 0xa: /* FCMLA, #180 */
11854     case 0xb: /* FCMLA, #270 */
11855         rot = extract32(opcode, 0, 2);
11856         switch (size) {
11857         case 1:
11858             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11859                               gen_helper_gvec_fcmlah);
11860             break;
11861         case 2:
11862             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11863                               gen_helper_gvec_fcmlas);
11864             break;
11865         case 3:
11866             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11867                               gen_helper_gvec_fcmlad);
11868             break;
11869         default:
11870             g_assert_not_reached();
11871         }
11872         return;
11873 
11874     case 0xc: /* FCADD, #90 */
11875     case 0xe: /* FCADD, #270 */
11876         rot = extract32(opcode, 1, 1);
11877         switch (size) {
11878         case 1:
11879             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11880                               gen_helper_gvec_fcaddh);
11881             break;
11882         case 2:
11883             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11884                               gen_helper_gvec_fcadds);
11885             break;
11886         case 3:
11887             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11888                               gen_helper_gvec_fcaddd);
11889             break;
11890         default:
11891             g_assert_not_reached();
11892         }
11893         return;
11894 
11895     case 0xd: /* BFMMLA */
11896         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11897         return;
11898     case 0xf:
11899         switch (size) {
11900         case 1: /* BFDOT */
11901             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11902             break;
11903         case 3: /* BFMLAL{B,T} */
11904             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11905                               gen_helper_gvec_bfmlal);
11906             break;
11907         default:
11908             g_assert_not_reached();
11909         }
11910         return;
11911 
11912     default:
11913         g_assert_not_reached();
11914     }
11915 }
11916 
11917 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11918                                   int size, int rn, int rd)
11919 {
11920     /* Handle 2-reg-misc ops which are widening (so each size element
11921      * in the source becomes a 2*size element in the destination.
11922      * The only instruction like this is FCVTL.
11923      */
11924     int pass;
11925 
11926     if (size == 3) {
11927         /* 32 -> 64 bit fp conversion */
11928         TCGv_i64 tcg_res[2];
11929         int srcelt = is_q ? 2 : 0;
11930 
11931         for (pass = 0; pass < 2; pass++) {
11932             TCGv_i32 tcg_op = tcg_temp_new_i32();
11933             tcg_res[pass] = tcg_temp_new_i64();
11934 
11935             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11936             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
11937         }
11938         for (pass = 0; pass < 2; pass++) {
11939             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11940         }
11941     } else {
11942         /* 16 -> 32 bit fp conversion */
11943         int srcelt = is_q ? 4 : 0;
11944         TCGv_i32 tcg_res[4];
11945         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11946         TCGv_i32 ahp = get_ahp_flag();
11947 
11948         for (pass = 0; pass < 4; pass++) {
11949             tcg_res[pass] = tcg_temp_new_i32();
11950 
11951             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11952             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11953                                            fpst, ahp);
11954         }
11955         for (pass = 0; pass < 4; pass++) {
11956             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11957         }
11958     }
11959 }
11960 
11961 static void handle_rev(DisasContext *s, int opcode, bool u,
11962                        bool is_q, int size, int rn, int rd)
11963 {
11964     int op = (opcode << 1) | u;
11965     int opsz = op + size;
11966     int grp_size = 3 - opsz;
11967     int dsize = is_q ? 128 : 64;
11968     int i;
11969 
11970     if (opsz >= 3) {
11971         unallocated_encoding(s);
11972         return;
11973     }
11974 
11975     if (!fp_access_check(s)) {
11976         return;
11977     }
11978 
11979     if (size == 0) {
11980         /* Special case bytes, use bswap op on each group of elements */
11981         int groups = dsize / (8 << grp_size);
11982 
11983         for (i = 0; i < groups; i++) {
11984             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11985 
11986             read_vec_element(s, tcg_tmp, rn, i, grp_size);
11987             switch (grp_size) {
11988             case MO_16:
11989                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11990                 break;
11991             case MO_32:
11992                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11993                 break;
11994             case MO_64:
11995                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11996                 break;
11997             default:
11998                 g_assert_not_reached();
11999             }
12000             write_vec_element(s, tcg_tmp, rd, i, grp_size);
12001         }
12002         clear_vec_high(s, is_q, rd);
12003     } else {
12004         int revmask = (1 << grp_size) - 1;
12005         int esize = 8 << size;
12006         int elements = dsize / esize;
12007         TCGv_i64 tcg_rn = tcg_temp_new_i64();
12008         TCGv_i64 tcg_rd = tcg_const_i64(0);
12009         TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
12010 
12011         for (i = 0; i < elements; i++) {
12012             int e_rev = (i & 0xf) ^ revmask;
12013             int off = e_rev * esize;
12014             read_vec_element(s, tcg_rn, rn, i, size);
12015             if (off >= 64) {
12016                 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
12017                                     tcg_rn, off - 64, esize);
12018             } else {
12019                 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
12020             }
12021         }
12022         write_vec_element(s, tcg_rd, rd, 0, MO_64);
12023         write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
12024     }
12025 }
12026 
12027 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
12028                                   bool is_q, int size, int rn, int rd)
12029 {
12030     /* Implement the pairwise operations from 2-misc:
12031      * SADDLP, UADDLP, SADALP, UADALP.
12032      * These all add pairs of elements in the input to produce a
12033      * double-width result element in the output (possibly accumulating).
12034      */
12035     bool accum = (opcode == 0x6);
12036     int maxpass = is_q ? 2 : 1;
12037     int pass;
12038     TCGv_i64 tcg_res[2];
12039 
12040     if (size == 2) {
12041         /* 32 + 32 -> 64 op */
12042         MemOp memop = size + (u ? 0 : MO_SIGN);
12043 
12044         for (pass = 0; pass < maxpass; pass++) {
12045             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
12046             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
12047 
12048             tcg_res[pass] = tcg_temp_new_i64();
12049 
12050             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
12051             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
12052             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
12053             if (accum) {
12054                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
12055                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12056             }
12057         }
12058     } else {
12059         for (pass = 0; pass < maxpass; pass++) {
12060             TCGv_i64 tcg_op = tcg_temp_new_i64();
12061             NeonGenOne64OpFn *genfn;
12062             static NeonGenOne64OpFn * const fns[2][2] = {
12063                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
12064                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
12065             };
12066 
12067             genfn = fns[size][u];
12068 
12069             tcg_res[pass] = tcg_temp_new_i64();
12070 
12071             read_vec_element(s, tcg_op, rn, pass, MO_64);
12072             genfn(tcg_res[pass], tcg_op);
12073 
12074             if (accum) {
12075                 read_vec_element(s, tcg_op, rd, pass, MO_64);
12076                 if (size == 0) {
12077                     gen_helper_neon_addl_u16(tcg_res[pass],
12078                                              tcg_res[pass], tcg_op);
12079                 } else {
12080                     gen_helper_neon_addl_u32(tcg_res[pass],
12081                                              tcg_res[pass], tcg_op);
12082                 }
12083             }
12084         }
12085     }
12086     if (!is_q) {
12087         tcg_res[1] = tcg_constant_i64(0);
12088     }
12089     for (pass = 0; pass < 2; pass++) {
12090         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12091     }
12092 }
12093 
12094 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
12095 {
12096     /* Implement SHLL and SHLL2 */
12097     int pass;
12098     int part = is_q ? 2 : 0;
12099     TCGv_i64 tcg_res[2];
12100 
12101     for (pass = 0; pass < 2; pass++) {
12102         static NeonGenWidenFn * const widenfns[3] = {
12103             gen_helper_neon_widen_u8,
12104             gen_helper_neon_widen_u16,
12105             tcg_gen_extu_i32_i64,
12106         };
12107         NeonGenWidenFn *widenfn = widenfns[size];
12108         TCGv_i32 tcg_op = tcg_temp_new_i32();
12109 
12110         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
12111         tcg_res[pass] = tcg_temp_new_i64();
12112         widenfn(tcg_res[pass], tcg_op);
12113         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
12114     }
12115 
12116     for (pass = 0; pass < 2; pass++) {
12117         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12118     }
12119 }
12120 
12121 /* AdvSIMD two reg misc
12122  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
12123  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12124  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12125  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12126  */
12127 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
12128 {
12129     int size = extract32(insn, 22, 2);
12130     int opcode = extract32(insn, 12, 5);
12131     bool u = extract32(insn, 29, 1);
12132     bool is_q = extract32(insn, 30, 1);
12133     int rn = extract32(insn, 5, 5);
12134     int rd = extract32(insn, 0, 5);
12135     bool need_fpstatus = false;
12136     bool need_rmode = false;
12137     int rmode = -1;
12138     TCGv_i32 tcg_rmode;
12139     TCGv_ptr tcg_fpstatus;
12140 
12141     switch (opcode) {
12142     case 0x0: /* REV64, REV32 */
12143     case 0x1: /* REV16 */
12144         handle_rev(s, opcode, u, is_q, size, rn, rd);
12145         return;
12146     case 0x5: /* CNT, NOT, RBIT */
12147         if (u && size == 0) {
12148             /* NOT */
12149             break;
12150         } else if (u && size == 1) {
12151             /* RBIT */
12152             break;
12153         } else if (!u && size == 0) {
12154             /* CNT */
12155             break;
12156         }
12157         unallocated_encoding(s);
12158         return;
12159     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12160     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12161         if (size == 3) {
12162             unallocated_encoding(s);
12163             return;
12164         }
12165         if (!fp_access_check(s)) {
12166             return;
12167         }
12168 
12169         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
12170         return;
12171     case 0x4: /* CLS, CLZ */
12172         if (size == 3) {
12173             unallocated_encoding(s);
12174             return;
12175         }
12176         break;
12177     case 0x2: /* SADDLP, UADDLP */
12178     case 0x6: /* SADALP, UADALP */
12179         if (size == 3) {
12180             unallocated_encoding(s);
12181             return;
12182         }
12183         if (!fp_access_check(s)) {
12184             return;
12185         }
12186         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
12187         return;
12188     case 0x13: /* SHLL, SHLL2 */
12189         if (u == 0 || size == 3) {
12190             unallocated_encoding(s);
12191             return;
12192         }
12193         if (!fp_access_check(s)) {
12194             return;
12195         }
12196         handle_shll(s, is_q, size, rn, rd);
12197         return;
12198     case 0xa: /* CMLT */
12199         if (u == 1) {
12200             unallocated_encoding(s);
12201             return;
12202         }
12203         /* fall through */
12204     case 0x8: /* CMGT, CMGE */
12205     case 0x9: /* CMEQ, CMLE */
12206     case 0xb: /* ABS, NEG */
12207         if (size == 3 && !is_q) {
12208             unallocated_encoding(s);
12209             return;
12210         }
12211         break;
12212     case 0x3: /* SUQADD, USQADD */
12213         if (size == 3 && !is_q) {
12214             unallocated_encoding(s);
12215             return;
12216         }
12217         if (!fp_access_check(s)) {
12218             return;
12219         }
12220         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12221         return;
12222     case 0x7: /* SQABS, SQNEG */
12223         if (size == 3 && !is_q) {
12224             unallocated_encoding(s);
12225             return;
12226         }
12227         break;
12228     case 0xc ... 0xf:
12229     case 0x16 ... 0x1f:
12230     {
12231         /* Floating point: U, size[1] and opcode indicate operation;
12232          * size[0] indicates single or double precision.
12233          */
12234         int is_double = extract32(size, 0, 1);
12235         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12236         size = is_double ? 3 : 2;
12237         switch (opcode) {
12238         case 0x2f: /* FABS */
12239         case 0x6f: /* FNEG */
12240             if (size == 3 && !is_q) {
12241                 unallocated_encoding(s);
12242                 return;
12243             }
12244             break;
12245         case 0x1d: /* SCVTF */
12246         case 0x5d: /* UCVTF */
12247         {
12248             bool is_signed = (opcode == 0x1d) ? true : false;
12249             int elements = is_double ? 2 : is_q ? 4 : 2;
12250             if (is_double && !is_q) {
12251                 unallocated_encoding(s);
12252                 return;
12253             }
12254             if (!fp_access_check(s)) {
12255                 return;
12256             }
12257             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12258             return;
12259         }
12260         case 0x2c: /* FCMGT (zero) */
12261         case 0x2d: /* FCMEQ (zero) */
12262         case 0x2e: /* FCMLT (zero) */
12263         case 0x6c: /* FCMGE (zero) */
12264         case 0x6d: /* FCMLE (zero) */
12265             if (size == 3 && !is_q) {
12266                 unallocated_encoding(s);
12267                 return;
12268             }
12269             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12270             return;
12271         case 0x7f: /* FSQRT */
12272             if (size == 3 && !is_q) {
12273                 unallocated_encoding(s);
12274                 return;
12275             }
12276             break;
12277         case 0x1a: /* FCVTNS */
12278         case 0x1b: /* FCVTMS */
12279         case 0x3a: /* FCVTPS */
12280         case 0x3b: /* FCVTZS */
12281         case 0x5a: /* FCVTNU */
12282         case 0x5b: /* FCVTMU */
12283         case 0x7a: /* FCVTPU */
12284         case 0x7b: /* FCVTZU */
12285             need_fpstatus = true;
12286             need_rmode = 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             need_rmode = true;
12297             rmode = FPROUNDING_TIEAWAY;
12298             if (size == 3 && !is_q) {
12299                 unallocated_encoding(s);
12300                 return;
12301             }
12302             break;
12303         case 0x3c: /* URECPE */
12304             if (size == 3) {
12305                 unallocated_encoding(s);
12306                 return;
12307             }
12308             /* fall through */
12309         case 0x3d: /* FRECPE */
12310         case 0x7d: /* FRSQRTE */
12311             if (size == 3 && !is_q) {
12312                 unallocated_encoding(s);
12313                 return;
12314             }
12315             if (!fp_access_check(s)) {
12316                 return;
12317             }
12318             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12319             return;
12320         case 0x56: /* FCVTXN, FCVTXN2 */
12321             if (size == 2) {
12322                 unallocated_encoding(s);
12323                 return;
12324             }
12325             /* fall through */
12326         case 0x16: /* FCVTN, FCVTN2 */
12327             /* handle_2misc_narrow does a 2*size -> size operation, but these
12328              * instructions encode the source size rather than dest size.
12329              */
12330             if (!fp_access_check(s)) {
12331                 return;
12332             }
12333             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12334             return;
12335         case 0x36: /* BFCVTN, BFCVTN2 */
12336             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
12337                 unallocated_encoding(s);
12338                 return;
12339             }
12340             if (!fp_access_check(s)) {
12341                 return;
12342             }
12343             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12344             return;
12345         case 0x17: /* FCVTL, FCVTL2 */
12346             if (!fp_access_check(s)) {
12347                 return;
12348             }
12349             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12350             return;
12351         case 0x18: /* FRINTN */
12352         case 0x19: /* FRINTM */
12353         case 0x38: /* FRINTP */
12354         case 0x39: /* FRINTZ */
12355             need_rmode = true;
12356             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12357             /* fall through */
12358         case 0x59: /* FRINTX */
12359         case 0x79: /* FRINTI */
12360             need_fpstatus = true;
12361             if (size == 3 && !is_q) {
12362                 unallocated_encoding(s);
12363                 return;
12364             }
12365             break;
12366         case 0x58: /* FRINTA */
12367             need_rmode = true;
12368             rmode = FPROUNDING_TIEAWAY;
12369             need_fpstatus = true;
12370             if (size == 3 && !is_q) {
12371                 unallocated_encoding(s);
12372                 return;
12373             }
12374             break;
12375         case 0x7c: /* URSQRTE */
12376             if (size == 3) {
12377                 unallocated_encoding(s);
12378                 return;
12379             }
12380             break;
12381         case 0x1e: /* FRINT32Z */
12382         case 0x1f: /* FRINT64Z */
12383             need_rmode = true;
12384             rmode = FPROUNDING_ZERO;
12385             /* fall through */
12386         case 0x5e: /* FRINT32X */
12387         case 0x5f: /* FRINT64X */
12388             need_fpstatus = true;
12389             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12390                 unallocated_encoding(s);
12391                 return;
12392             }
12393             break;
12394         default:
12395             unallocated_encoding(s);
12396             return;
12397         }
12398         break;
12399     }
12400     default:
12401         unallocated_encoding(s);
12402         return;
12403     }
12404 
12405     if (!fp_access_check(s)) {
12406         return;
12407     }
12408 
12409     if (need_fpstatus || need_rmode) {
12410         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12411     } else {
12412         tcg_fpstatus = NULL;
12413     }
12414     if (need_rmode) {
12415         tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
12416         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12417     } else {
12418         tcg_rmode = NULL;
12419     }
12420 
12421     switch (opcode) {
12422     case 0x5:
12423         if (u && size == 0) { /* NOT */
12424             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12425             return;
12426         }
12427         break;
12428     case 0x8: /* CMGT, CMGE */
12429         if (u) {
12430             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12431         } else {
12432             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12433         }
12434         return;
12435     case 0x9: /* CMEQ, CMLE */
12436         if (u) {
12437             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12438         } else {
12439             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12440         }
12441         return;
12442     case 0xa: /* CMLT */
12443         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12444         return;
12445     case 0xb:
12446         if (u) { /* ABS, NEG */
12447             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12448         } else {
12449             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12450         }
12451         return;
12452     }
12453 
12454     if (size == 3) {
12455         /* All 64-bit element operations can be shared with scalar 2misc */
12456         int pass;
12457 
12458         /* Coverity claims (size == 3 && !is_q) has been eliminated
12459          * from all paths leading to here.
12460          */
12461         tcg_debug_assert(is_q);
12462         for (pass = 0; pass < 2; pass++) {
12463             TCGv_i64 tcg_op = tcg_temp_new_i64();
12464             TCGv_i64 tcg_res = tcg_temp_new_i64();
12465 
12466             read_vec_element(s, tcg_op, rn, pass, MO_64);
12467 
12468             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12469                             tcg_rmode, tcg_fpstatus);
12470 
12471             write_vec_element(s, tcg_res, rd, pass, MO_64);
12472         }
12473     } else {
12474         int pass;
12475 
12476         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12477             TCGv_i32 tcg_op = tcg_temp_new_i32();
12478             TCGv_i32 tcg_res = tcg_temp_new_i32();
12479 
12480             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12481 
12482             if (size == 2) {
12483                 /* Special cases for 32 bit elements */
12484                 switch (opcode) {
12485                 case 0x4: /* CLS */
12486                     if (u) {
12487                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12488                     } else {
12489                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
12490                     }
12491                     break;
12492                 case 0x7: /* SQABS, SQNEG */
12493                     if (u) {
12494                         gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12495                     } else {
12496                         gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12497                     }
12498                     break;
12499                 case 0x2f: /* FABS */
12500                     gen_helper_vfp_abss(tcg_res, tcg_op);
12501                     break;
12502                 case 0x6f: /* FNEG */
12503                     gen_helper_vfp_negs(tcg_res, tcg_op);
12504                     break;
12505                 case 0x7f: /* FSQRT */
12506                     gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12507                     break;
12508                 case 0x1a: /* FCVTNS */
12509                 case 0x1b: /* FCVTMS */
12510                 case 0x1c: /* FCVTAS */
12511                 case 0x3a: /* FCVTPS */
12512                 case 0x3b: /* FCVTZS */
12513                     gen_helper_vfp_tosls(tcg_res, tcg_op,
12514                                          tcg_constant_i32(0), tcg_fpstatus);
12515                     break;
12516                 case 0x5a: /* FCVTNU */
12517                 case 0x5b: /* FCVTMU */
12518                 case 0x5c: /* FCVTAU */
12519                 case 0x7a: /* FCVTPU */
12520                 case 0x7b: /* FCVTZU */
12521                     gen_helper_vfp_touls(tcg_res, tcg_op,
12522                                          tcg_constant_i32(0), tcg_fpstatus);
12523                     break;
12524                 case 0x18: /* FRINTN */
12525                 case 0x19: /* FRINTM */
12526                 case 0x38: /* FRINTP */
12527                 case 0x39: /* FRINTZ */
12528                 case 0x58: /* FRINTA */
12529                 case 0x79: /* FRINTI */
12530                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12531                     break;
12532                 case 0x59: /* FRINTX */
12533                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12534                     break;
12535                 case 0x7c: /* URSQRTE */
12536                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
12537                     break;
12538                 case 0x1e: /* FRINT32Z */
12539                 case 0x5e: /* FRINT32X */
12540                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12541                     break;
12542                 case 0x1f: /* FRINT64Z */
12543                 case 0x5f: /* FRINT64X */
12544                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12545                     break;
12546                 default:
12547                     g_assert_not_reached();
12548                 }
12549             } else {
12550                 /* Use helpers for 8 and 16 bit elements */
12551                 switch (opcode) {
12552                 case 0x5: /* CNT, RBIT */
12553                     /* For these two insns size is part of the opcode specifier
12554                      * (handled earlier); they always operate on byte elements.
12555                      */
12556                     if (u) {
12557                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12558                     } else {
12559                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12560                     }
12561                     break;
12562                 case 0x7: /* SQABS, SQNEG */
12563                 {
12564                     NeonGenOneOpEnvFn *genfn;
12565                     static NeonGenOneOpEnvFn * const fns[2][2] = {
12566                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12567                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12568                     };
12569                     genfn = fns[size][u];
12570                     genfn(tcg_res, cpu_env, tcg_op);
12571                     break;
12572                 }
12573                 case 0x4: /* CLS, CLZ */
12574                     if (u) {
12575                         if (size == 0) {
12576                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
12577                         } else {
12578                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
12579                         }
12580                     } else {
12581                         if (size == 0) {
12582                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
12583                         } else {
12584                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
12585                         }
12586                     }
12587                     break;
12588                 default:
12589                     g_assert_not_reached();
12590                 }
12591             }
12592 
12593             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12594         }
12595     }
12596     clear_vec_high(s, is_q, rd);
12597 
12598     if (need_rmode) {
12599         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12600     }
12601 }
12602 
12603 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12604  *
12605  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
12606  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12607  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12608  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12609  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12610  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12611  *
12612  * This actually covers two groups where scalar access is governed by
12613  * bit 28. A bunch of the instructions (float to integral) only exist
12614  * in the vector form and are un-allocated for the scalar decode. Also
12615  * in the scalar decode Q is always 1.
12616  */
12617 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12618 {
12619     int fpop, opcode, a, u;
12620     int rn, rd;
12621     bool is_q;
12622     bool is_scalar;
12623     bool only_in_vector = false;
12624 
12625     int pass;
12626     TCGv_i32 tcg_rmode = NULL;
12627     TCGv_ptr tcg_fpstatus = NULL;
12628     bool need_rmode = false;
12629     bool need_fpst = true;
12630     int rmode;
12631 
12632     if (!dc_isar_feature(aa64_fp16, s)) {
12633         unallocated_encoding(s);
12634         return;
12635     }
12636 
12637     rd = extract32(insn, 0, 5);
12638     rn = extract32(insn, 5, 5);
12639 
12640     a = extract32(insn, 23, 1);
12641     u = extract32(insn, 29, 1);
12642     is_scalar = extract32(insn, 28, 1);
12643     is_q = extract32(insn, 30, 1);
12644 
12645     opcode = extract32(insn, 12, 5);
12646     fpop = deposit32(opcode, 5, 1, a);
12647     fpop = deposit32(fpop, 6, 1, u);
12648 
12649     switch (fpop) {
12650     case 0x1d: /* SCVTF */
12651     case 0x5d: /* UCVTF */
12652     {
12653         int elements;
12654 
12655         if (is_scalar) {
12656             elements = 1;
12657         } else {
12658             elements = (is_q ? 8 : 4);
12659         }
12660 
12661         if (!fp_access_check(s)) {
12662             return;
12663         }
12664         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12665         return;
12666     }
12667     break;
12668     case 0x2c: /* FCMGT (zero) */
12669     case 0x2d: /* FCMEQ (zero) */
12670     case 0x2e: /* FCMLT (zero) */
12671     case 0x6c: /* FCMGE (zero) */
12672     case 0x6d: /* FCMLE (zero) */
12673         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12674         return;
12675     case 0x3d: /* FRECPE */
12676     case 0x3f: /* FRECPX */
12677         break;
12678     case 0x18: /* FRINTN */
12679         need_rmode = true;
12680         only_in_vector = true;
12681         rmode = FPROUNDING_TIEEVEN;
12682         break;
12683     case 0x19: /* FRINTM */
12684         need_rmode = true;
12685         only_in_vector = true;
12686         rmode = FPROUNDING_NEGINF;
12687         break;
12688     case 0x38: /* FRINTP */
12689         need_rmode = true;
12690         only_in_vector = true;
12691         rmode = FPROUNDING_POSINF;
12692         break;
12693     case 0x39: /* FRINTZ */
12694         need_rmode = true;
12695         only_in_vector = true;
12696         rmode = FPROUNDING_ZERO;
12697         break;
12698     case 0x58: /* FRINTA */
12699         need_rmode = true;
12700         only_in_vector = true;
12701         rmode = FPROUNDING_TIEAWAY;
12702         break;
12703     case 0x59: /* FRINTX */
12704     case 0x79: /* FRINTI */
12705         only_in_vector = true;
12706         /* current rounding mode */
12707         break;
12708     case 0x1a: /* FCVTNS */
12709         need_rmode = true;
12710         rmode = FPROUNDING_TIEEVEN;
12711         break;
12712     case 0x1b: /* FCVTMS */
12713         need_rmode = true;
12714         rmode = FPROUNDING_NEGINF;
12715         break;
12716     case 0x1c: /* FCVTAS */
12717         need_rmode = true;
12718         rmode = FPROUNDING_TIEAWAY;
12719         break;
12720     case 0x3a: /* FCVTPS */
12721         need_rmode = true;
12722         rmode = FPROUNDING_POSINF;
12723         break;
12724     case 0x3b: /* FCVTZS */
12725         need_rmode = true;
12726         rmode = FPROUNDING_ZERO;
12727         break;
12728     case 0x5a: /* FCVTNU */
12729         need_rmode = true;
12730         rmode = FPROUNDING_TIEEVEN;
12731         break;
12732     case 0x5b: /* FCVTMU */
12733         need_rmode = true;
12734         rmode = FPROUNDING_NEGINF;
12735         break;
12736     case 0x5c: /* FCVTAU */
12737         need_rmode = true;
12738         rmode = FPROUNDING_TIEAWAY;
12739         break;
12740     case 0x7a: /* FCVTPU */
12741         need_rmode = true;
12742         rmode = FPROUNDING_POSINF;
12743         break;
12744     case 0x7b: /* FCVTZU */
12745         need_rmode = true;
12746         rmode = FPROUNDING_ZERO;
12747         break;
12748     case 0x2f: /* FABS */
12749     case 0x6f: /* FNEG */
12750         need_fpst = false;
12751         break;
12752     case 0x7d: /* FRSQRTE */
12753     case 0x7f: /* FSQRT (vector) */
12754         break;
12755     default:
12756         unallocated_encoding(s);
12757         return;
12758     }
12759 
12760 
12761     /* Check additional constraints for the scalar encoding */
12762     if (is_scalar) {
12763         if (!is_q) {
12764             unallocated_encoding(s);
12765             return;
12766         }
12767         /* FRINTxx is only in the vector form */
12768         if (only_in_vector) {
12769             unallocated_encoding(s);
12770             return;
12771         }
12772     }
12773 
12774     if (!fp_access_check(s)) {
12775         return;
12776     }
12777 
12778     if (need_rmode || need_fpst) {
12779         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
12780     }
12781 
12782     if (need_rmode) {
12783         tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
12784         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12785     }
12786 
12787     if (is_scalar) {
12788         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12789         TCGv_i32 tcg_res = tcg_temp_new_i32();
12790 
12791         switch (fpop) {
12792         case 0x1a: /* FCVTNS */
12793         case 0x1b: /* FCVTMS */
12794         case 0x1c: /* FCVTAS */
12795         case 0x3a: /* FCVTPS */
12796         case 0x3b: /* FCVTZS */
12797             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12798             break;
12799         case 0x3d: /* FRECPE */
12800             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12801             break;
12802         case 0x3f: /* FRECPX */
12803             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12804             break;
12805         case 0x5a: /* FCVTNU */
12806         case 0x5b: /* FCVTMU */
12807         case 0x5c: /* FCVTAU */
12808         case 0x7a: /* FCVTPU */
12809         case 0x7b: /* FCVTZU */
12810             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12811             break;
12812         case 0x6f: /* FNEG */
12813             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12814             break;
12815         case 0x7d: /* FRSQRTE */
12816             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12817             break;
12818         default:
12819             g_assert_not_reached();
12820         }
12821 
12822         /* limit any sign extension going on */
12823         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12824         write_fp_sreg(s, rd, tcg_res);
12825     } else {
12826         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12827             TCGv_i32 tcg_op = tcg_temp_new_i32();
12828             TCGv_i32 tcg_res = tcg_temp_new_i32();
12829 
12830             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12831 
12832             switch (fpop) {
12833             case 0x1a: /* FCVTNS */
12834             case 0x1b: /* FCVTMS */
12835             case 0x1c: /* FCVTAS */
12836             case 0x3a: /* FCVTPS */
12837             case 0x3b: /* FCVTZS */
12838                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12839                 break;
12840             case 0x3d: /* FRECPE */
12841                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12842                 break;
12843             case 0x5a: /* FCVTNU */
12844             case 0x5b: /* FCVTMU */
12845             case 0x5c: /* FCVTAU */
12846             case 0x7a: /* FCVTPU */
12847             case 0x7b: /* FCVTZU */
12848                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12849                 break;
12850             case 0x18: /* FRINTN */
12851             case 0x19: /* FRINTM */
12852             case 0x38: /* FRINTP */
12853             case 0x39: /* FRINTZ */
12854             case 0x58: /* FRINTA */
12855             case 0x79: /* FRINTI */
12856                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12857                 break;
12858             case 0x59: /* FRINTX */
12859                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12860                 break;
12861             case 0x2f: /* FABS */
12862                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12863                 break;
12864             case 0x6f: /* FNEG */
12865                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12866                 break;
12867             case 0x7d: /* FRSQRTE */
12868                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12869                 break;
12870             case 0x7f: /* FSQRT */
12871                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12872                 break;
12873             default:
12874                 g_assert_not_reached();
12875             }
12876 
12877             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12878         }
12879 
12880         clear_vec_high(s, is_q, rd);
12881     }
12882 
12883     if (tcg_rmode) {
12884         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12885     }
12886 }
12887 
12888 /* AdvSIMD scalar x indexed element
12889  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12890  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12891  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12892  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12893  * AdvSIMD vector x indexed element
12894  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12895  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12896  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12897  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12898  */
12899 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12900 {
12901     /* This encoding has two kinds of instruction:
12902      *  normal, where we perform elt x idxelt => elt for each
12903      *     element in the vector
12904      *  long, where we perform elt x idxelt and generate a result of
12905      *     double the width of the input element
12906      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12907      */
12908     bool is_scalar = extract32(insn, 28, 1);
12909     bool is_q = extract32(insn, 30, 1);
12910     bool u = extract32(insn, 29, 1);
12911     int size = extract32(insn, 22, 2);
12912     int l = extract32(insn, 21, 1);
12913     int m = extract32(insn, 20, 1);
12914     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12915     int rm = extract32(insn, 16, 4);
12916     int opcode = extract32(insn, 12, 4);
12917     int h = extract32(insn, 11, 1);
12918     int rn = extract32(insn, 5, 5);
12919     int rd = extract32(insn, 0, 5);
12920     bool is_long = false;
12921     int is_fp = 0;
12922     bool is_fp16 = false;
12923     int index;
12924     TCGv_ptr fpst;
12925 
12926     switch (16 * u + opcode) {
12927     case 0x08: /* MUL */
12928     case 0x10: /* MLA */
12929     case 0x14: /* MLS */
12930         if (is_scalar) {
12931             unallocated_encoding(s);
12932             return;
12933         }
12934         break;
12935     case 0x02: /* SMLAL, SMLAL2 */
12936     case 0x12: /* UMLAL, UMLAL2 */
12937     case 0x06: /* SMLSL, SMLSL2 */
12938     case 0x16: /* UMLSL, UMLSL2 */
12939     case 0x0a: /* SMULL, SMULL2 */
12940     case 0x1a: /* UMULL, UMULL2 */
12941         if (is_scalar) {
12942             unallocated_encoding(s);
12943             return;
12944         }
12945         is_long = true;
12946         break;
12947     case 0x03: /* SQDMLAL, SQDMLAL2 */
12948     case 0x07: /* SQDMLSL, SQDMLSL2 */
12949     case 0x0b: /* SQDMULL, SQDMULL2 */
12950         is_long = true;
12951         break;
12952     case 0x0c: /* SQDMULH */
12953     case 0x0d: /* SQRDMULH */
12954         break;
12955     case 0x01: /* FMLA */
12956     case 0x05: /* FMLS */
12957     case 0x09: /* FMUL */
12958     case 0x19: /* FMULX */
12959         is_fp = 1;
12960         break;
12961     case 0x1d: /* SQRDMLAH */
12962     case 0x1f: /* SQRDMLSH */
12963         if (!dc_isar_feature(aa64_rdm, s)) {
12964             unallocated_encoding(s);
12965             return;
12966         }
12967         break;
12968     case 0x0e: /* SDOT */
12969     case 0x1e: /* UDOT */
12970         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12971             unallocated_encoding(s);
12972             return;
12973         }
12974         break;
12975     case 0x0f:
12976         switch (size) {
12977         case 0: /* SUDOT */
12978         case 2: /* USDOT */
12979             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12980                 unallocated_encoding(s);
12981                 return;
12982             }
12983             size = MO_32;
12984             break;
12985         case 1: /* BFDOT */
12986             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12987                 unallocated_encoding(s);
12988                 return;
12989             }
12990             size = MO_32;
12991             break;
12992         case 3: /* BFMLAL{B,T} */
12993             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12994                 unallocated_encoding(s);
12995                 return;
12996             }
12997             /* can't set is_fp without other incorrect size checks */
12998             size = MO_16;
12999             break;
13000         default:
13001             unallocated_encoding(s);
13002             return;
13003         }
13004         break;
13005     case 0x11: /* FCMLA #0 */
13006     case 0x13: /* FCMLA #90 */
13007     case 0x15: /* FCMLA #180 */
13008     case 0x17: /* FCMLA #270 */
13009         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
13010             unallocated_encoding(s);
13011             return;
13012         }
13013         is_fp = 2;
13014         break;
13015     case 0x00: /* FMLAL */
13016     case 0x04: /* FMLSL */
13017     case 0x18: /* FMLAL2 */
13018     case 0x1c: /* FMLSL2 */
13019         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
13020             unallocated_encoding(s);
13021             return;
13022         }
13023         size = MO_16;
13024         /* is_fp, but we pass cpu_env not fp_status.  */
13025         break;
13026     default:
13027         unallocated_encoding(s);
13028         return;
13029     }
13030 
13031     switch (is_fp) {
13032     case 1: /* normal fp */
13033         /* convert insn encoded size to MemOp size */
13034         switch (size) {
13035         case 0: /* half-precision */
13036             size = MO_16;
13037             is_fp16 = true;
13038             break;
13039         case MO_32: /* single precision */
13040         case MO_64: /* double precision */
13041             break;
13042         default:
13043             unallocated_encoding(s);
13044             return;
13045         }
13046         break;
13047 
13048     case 2: /* complex fp */
13049         /* Each indexable element is a complex pair.  */
13050         size += 1;
13051         switch (size) {
13052         case MO_32:
13053             if (h && !is_q) {
13054                 unallocated_encoding(s);
13055                 return;
13056             }
13057             is_fp16 = true;
13058             break;
13059         case MO_64:
13060             break;
13061         default:
13062             unallocated_encoding(s);
13063             return;
13064         }
13065         break;
13066 
13067     default: /* integer */
13068         switch (size) {
13069         case MO_8:
13070         case MO_64:
13071             unallocated_encoding(s);
13072             return;
13073         }
13074         break;
13075     }
13076     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
13077         unallocated_encoding(s);
13078         return;
13079     }
13080 
13081     /* Given MemOp size, adjust register and indexing.  */
13082     switch (size) {
13083     case MO_16:
13084         index = h << 2 | l << 1 | m;
13085         break;
13086     case MO_32:
13087         index = h << 1 | l;
13088         rm |= m << 4;
13089         break;
13090     case MO_64:
13091         if (l || !is_q) {
13092             unallocated_encoding(s);
13093             return;
13094         }
13095         index = h;
13096         rm |= m << 4;
13097         break;
13098     default:
13099         g_assert_not_reached();
13100     }
13101 
13102     if (!fp_access_check(s)) {
13103         return;
13104     }
13105 
13106     if (is_fp) {
13107         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
13108     } else {
13109         fpst = NULL;
13110     }
13111 
13112     switch (16 * u + opcode) {
13113     case 0x0e: /* SDOT */
13114     case 0x1e: /* UDOT */
13115         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13116                          u ? gen_helper_gvec_udot_idx_b
13117                          : gen_helper_gvec_sdot_idx_b);
13118         return;
13119     case 0x0f:
13120         switch (extract32(insn, 22, 2)) {
13121         case 0: /* SUDOT */
13122             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13123                              gen_helper_gvec_sudot_idx_b);
13124             return;
13125         case 1: /* BFDOT */
13126             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13127                              gen_helper_gvec_bfdot_idx);
13128             return;
13129         case 2: /* USDOT */
13130             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13131                              gen_helper_gvec_usdot_idx_b);
13132             return;
13133         case 3: /* BFMLAL{B,T} */
13134             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
13135                               gen_helper_gvec_bfmlal_idx);
13136             return;
13137         }
13138         g_assert_not_reached();
13139     case 0x11: /* FCMLA #0 */
13140     case 0x13: /* FCMLA #90 */
13141     case 0x15: /* FCMLA #180 */
13142     case 0x17: /* FCMLA #270 */
13143         {
13144             int rot = extract32(insn, 13, 2);
13145             int data = (index << 2) | rot;
13146             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
13147                                vec_full_reg_offset(s, rn),
13148                                vec_full_reg_offset(s, rm),
13149                                vec_full_reg_offset(s, rd), fpst,
13150                                is_q ? 16 : 8, vec_full_reg_size(s), data,
13151                                size == MO_64
13152                                ? gen_helper_gvec_fcmlas_idx
13153                                : gen_helper_gvec_fcmlah_idx);
13154         }
13155         return;
13156 
13157     case 0x00: /* FMLAL */
13158     case 0x04: /* FMLSL */
13159     case 0x18: /* FMLAL2 */
13160     case 0x1c: /* FMLSL2 */
13161         {
13162             int is_s = extract32(opcode, 2, 1);
13163             int is_2 = u;
13164             int data = (index << 2) | (is_2 << 1) | is_s;
13165             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13166                                vec_full_reg_offset(s, rn),
13167                                vec_full_reg_offset(s, rm), cpu_env,
13168                                is_q ? 16 : 8, vec_full_reg_size(s),
13169                                data, gen_helper_gvec_fmlal_idx_a64);
13170         }
13171         return;
13172 
13173     case 0x08: /* MUL */
13174         if (!is_long && !is_scalar) {
13175             static gen_helper_gvec_3 * const fns[3] = {
13176                 gen_helper_gvec_mul_idx_h,
13177                 gen_helper_gvec_mul_idx_s,
13178                 gen_helper_gvec_mul_idx_d,
13179             };
13180             tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
13181                                vec_full_reg_offset(s, rn),
13182                                vec_full_reg_offset(s, rm),
13183                                is_q ? 16 : 8, vec_full_reg_size(s),
13184                                index, fns[size - 1]);
13185             return;
13186         }
13187         break;
13188 
13189     case 0x10: /* MLA */
13190         if (!is_long && !is_scalar) {
13191             static gen_helper_gvec_4 * const fns[3] = {
13192                 gen_helper_gvec_mla_idx_h,
13193                 gen_helper_gvec_mla_idx_s,
13194                 gen_helper_gvec_mla_idx_d,
13195             };
13196             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13197                                vec_full_reg_offset(s, rn),
13198                                vec_full_reg_offset(s, rm),
13199                                vec_full_reg_offset(s, rd),
13200                                is_q ? 16 : 8, vec_full_reg_size(s),
13201                                index, fns[size - 1]);
13202             return;
13203         }
13204         break;
13205 
13206     case 0x14: /* MLS */
13207         if (!is_long && !is_scalar) {
13208             static gen_helper_gvec_4 * const fns[3] = {
13209                 gen_helper_gvec_mls_idx_h,
13210                 gen_helper_gvec_mls_idx_s,
13211                 gen_helper_gvec_mls_idx_d,
13212             };
13213             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13214                                vec_full_reg_offset(s, rn),
13215                                vec_full_reg_offset(s, rm),
13216                                vec_full_reg_offset(s, rd),
13217                                is_q ? 16 : 8, vec_full_reg_size(s),
13218                                index, fns[size - 1]);
13219             return;
13220         }
13221         break;
13222     }
13223 
13224     if (size == 3) {
13225         TCGv_i64 tcg_idx = tcg_temp_new_i64();
13226         int pass;
13227 
13228         assert(is_fp && is_q && !is_long);
13229 
13230         read_vec_element(s, tcg_idx, rm, index, MO_64);
13231 
13232         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13233             TCGv_i64 tcg_op = tcg_temp_new_i64();
13234             TCGv_i64 tcg_res = tcg_temp_new_i64();
13235 
13236             read_vec_element(s, tcg_op, rn, pass, MO_64);
13237 
13238             switch (16 * u + opcode) {
13239             case 0x05: /* FMLS */
13240                 /* As usual for ARM, separate negation for fused multiply-add */
13241                 gen_helper_vfp_negd(tcg_op, tcg_op);
13242                 /* fall through */
13243             case 0x01: /* FMLA */
13244                 read_vec_element(s, tcg_res, rd, pass, MO_64);
13245                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13246                 break;
13247             case 0x09: /* FMUL */
13248                 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13249                 break;
13250             case 0x19: /* FMULX */
13251                 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13252                 break;
13253             default:
13254                 g_assert_not_reached();
13255             }
13256 
13257             write_vec_element(s, tcg_res, rd, pass, MO_64);
13258         }
13259 
13260         clear_vec_high(s, !is_scalar, rd);
13261     } else if (!is_long) {
13262         /* 32 bit floating point, or 16 or 32 bit integer.
13263          * For the 16 bit scalar case we use the usual Neon helpers and
13264          * rely on the fact that 0 op 0 == 0 with no side effects.
13265          */
13266         TCGv_i32 tcg_idx = tcg_temp_new_i32();
13267         int pass, maxpasses;
13268 
13269         if (is_scalar) {
13270             maxpasses = 1;
13271         } else {
13272             maxpasses = is_q ? 4 : 2;
13273         }
13274 
13275         read_vec_element_i32(s, tcg_idx, rm, index, size);
13276 
13277         if (size == 1 && !is_scalar) {
13278             /* The simplest way to handle the 16x16 indexed ops is to duplicate
13279              * the index into both halves of the 32 bit tcg_idx and then use
13280              * the usual Neon helpers.
13281              */
13282             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13283         }
13284 
13285         for (pass = 0; pass < maxpasses; pass++) {
13286             TCGv_i32 tcg_op = tcg_temp_new_i32();
13287             TCGv_i32 tcg_res = tcg_temp_new_i32();
13288 
13289             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13290 
13291             switch (16 * u + opcode) {
13292             case 0x08: /* MUL */
13293             case 0x10: /* MLA */
13294             case 0x14: /* MLS */
13295             {
13296                 static NeonGenTwoOpFn * const fns[2][2] = {
13297                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13298                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
13299                 };
13300                 NeonGenTwoOpFn *genfn;
13301                 bool is_sub = opcode == 0x4;
13302 
13303                 if (size == 1) {
13304                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13305                 } else {
13306                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13307                 }
13308                 if (opcode == 0x8) {
13309                     break;
13310                 }
13311                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13312                 genfn = fns[size - 1][is_sub];
13313                 genfn(tcg_res, tcg_op, tcg_res);
13314                 break;
13315             }
13316             case 0x05: /* FMLS */
13317             case 0x01: /* FMLA */
13318                 read_vec_element_i32(s, tcg_res, rd, pass,
13319                                      is_scalar ? size : MO_32);
13320                 switch (size) {
13321                 case 1:
13322                     if (opcode == 0x5) {
13323                         /* As usual for ARM, separate negation for fused
13324                          * multiply-add */
13325                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13326                     }
13327                     if (is_scalar) {
13328                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13329                                                    tcg_res, fpst);
13330                     } else {
13331                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13332                                                     tcg_res, fpst);
13333                     }
13334                     break;
13335                 case 2:
13336                     if (opcode == 0x5) {
13337                         /* As usual for ARM, separate negation for
13338                          * fused multiply-add */
13339                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13340                     }
13341                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13342                                            tcg_res, fpst);
13343                     break;
13344                 default:
13345                     g_assert_not_reached();
13346                 }
13347                 break;
13348             case 0x09: /* FMUL */
13349                 switch (size) {
13350                 case 1:
13351                     if (is_scalar) {
13352                         gen_helper_advsimd_mulh(tcg_res, tcg_op,
13353                                                 tcg_idx, fpst);
13354                     } else {
13355                         gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13356                                                  tcg_idx, fpst);
13357                     }
13358                     break;
13359                 case 2:
13360                     gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13361                     break;
13362                 default:
13363                     g_assert_not_reached();
13364                 }
13365                 break;
13366             case 0x19: /* FMULX */
13367                 switch (size) {
13368                 case 1:
13369                     if (is_scalar) {
13370                         gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13371                                                  tcg_idx, fpst);
13372                     } else {
13373                         gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13374                                                   tcg_idx, fpst);
13375                     }
13376                     break;
13377                 case 2:
13378                     gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13379                     break;
13380                 default:
13381                     g_assert_not_reached();
13382                 }
13383                 break;
13384             case 0x0c: /* SQDMULH */
13385                 if (size == 1) {
13386                     gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13387                                                tcg_op, tcg_idx);
13388                 } else {
13389                     gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13390                                                tcg_op, tcg_idx);
13391                 }
13392                 break;
13393             case 0x0d: /* SQRDMULH */
13394                 if (size == 1) {
13395                     gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13396                                                 tcg_op, tcg_idx);
13397                 } else {
13398                     gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13399                                                 tcg_op, tcg_idx);
13400                 }
13401                 break;
13402             case 0x1d: /* SQRDMLAH */
13403                 read_vec_element_i32(s, tcg_res, rd, pass,
13404                                      is_scalar ? size : MO_32);
13405                 if (size == 1) {
13406                     gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13407                                                 tcg_op, tcg_idx, tcg_res);
13408                 } else {
13409                     gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13410                                                 tcg_op, tcg_idx, tcg_res);
13411                 }
13412                 break;
13413             case 0x1f: /* SQRDMLSH */
13414                 read_vec_element_i32(s, tcg_res, rd, pass,
13415                                      is_scalar ? size : MO_32);
13416                 if (size == 1) {
13417                     gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13418                                                 tcg_op, tcg_idx, tcg_res);
13419                 } else {
13420                     gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13421                                                 tcg_op, tcg_idx, tcg_res);
13422                 }
13423                 break;
13424             default:
13425                 g_assert_not_reached();
13426             }
13427 
13428             if (is_scalar) {
13429                 write_fp_sreg(s, rd, tcg_res);
13430             } else {
13431                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13432             }
13433         }
13434 
13435         clear_vec_high(s, is_q, rd);
13436     } else {
13437         /* long ops: 16x16->32 or 32x32->64 */
13438         TCGv_i64 tcg_res[2];
13439         int pass;
13440         bool satop = extract32(opcode, 0, 1);
13441         MemOp memop = MO_32;
13442 
13443         if (satop || !u) {
13444             memop |= MO_SIGN;
13445         }
13446 
13447         if (size == 2) {
13448             TCGv_i64 tcg_idx = tcg_temp_new_i64();
13449 
13450             read_vec_element(s, tcg_idx, rm, index, memop);
13451 
13452             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13453                 TCGv_i64 tcg_op = tcg_temp_new_i64();
13454                 TCGv_i64 tcg_passres;
13455                 int passelt;
13456 
13457                 if (is_scalar) {
13458                     passelt = 0;
13459                 } else {
13460                     passelt = pass + (is_q * 2);
13461                 }
13462 
13463                 read_vec_element(s, tcg_op, rn, passelt, memop);
13464 
13465                 tcg_res[pass] = tcg_temp_new_i64();
13466 
13467                 if (opcode == 0xa || opcode == 0xb) {
13468                     /* Non-accumulating ops */
13469                     tcg_passres = tcg_res[pass];
13470                 } else {
13471                     tcg_passres = tcg_temp_new_i64();
13472                 }
13473 
13474                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13475 
13476                 if (satop) {
13477                     /* saturating, doubling */
13478                     gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13479                                                       tcg_passres, tcg_passres);
13480                 }
13481 
13482                 if (opcode == 0xa || opcode == 0xb) {
13483                     continue;
13484                 }
13485 
13486                 /* Accumulating op: handle accumulate step */
13487                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13488 
13489                 switch (opcode) {
13490                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13491                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13492                     break;
13493                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13494                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13495                     break;
13496                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13497                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
13498                     /* fall through */
13499                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13500                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13501                                                       tcg_res[pass],
13502                                                       tcg_passres);
13503                     break;
13504                 default:
13505                     g_assert_not_reached();
13506                 }
13507             }
13508 
13509             clear_vec_high(s, !is_scalar, rd);
13510         } else {
13511             TCGv_i32 tcg_idx = tcg_temp_new_i32();
13512 
13513             assert(size == 1);
13514             read_vec_element_i32(s, tcg_idx, rm, index, size);
13515 
13516             if (!is_scalar) {
13517                 /* The simplest way to handle the 16x16 indexed ops is to
13518                  * duplicate the index into both halves of the 32 bit tcg_idx
13519                  * and then use the usual Neon helpers.
13520                  */
13521                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13522             }
13523 
13524             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13525                 TCGv_i32 tcg_op = tcg_temp_new_i32();
13526                 TCGv_i64 tcg_passres;
13527 
13528                 if (is_scalar) {
13529                     read_vec_element_i32(s, tcg_op, rn, pass, size);
13530                 } else {
13531                     read_vec_element_i32(s, tcg_op, rn,
13532                                          pass + (is_q * 2), MO_32);
13533                 }
13534 
13535                 tcg_res[pass] = tcg_temp_new_i64();
13536 
13537                 if (opcode == 0xa || opcode == 0xb) {
13538                     /* Non-accumulating ops */
13539                     tcg_passres = tcg_res[pass];
13540                 } else {
13541                     tcg_passres = tcg_temp_new_i64();
13542                 }
13543 
13544                 if (memop & MO_SIGN) {
13545                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13546                 } else {
13547                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13548                 }
13549                 if (satop) {
13550                     gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13551                                                       tcg_passres, tcg_passres);
13552                 }
13553 
13554                 if (opcode == 0xa || opcode == 0xb) {
13555                     continue;
13556                 }
13557 
13558                 /* Accumulating op: handle accumulate step */
13559                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13560 
13561                 switch (opcode) {
13562                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13563                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13564                                              tcg_passres);
13565                     break;
13566                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13567                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13568                                              tcg_passres);
13569                     break;
13570                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13571                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13572                     /* fall through */
13573                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13574                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13575                                                       tcg_res[pass],
13576                                                       tcg_passres);
13577                     break;
13578                 default:
13579                     g_assert_not_reached();
13580                 }
13581             }
13582 
13583             if (is_scalar) {
13584                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13585             }
13586         }
13587 
13588         if (is_scalar) {
13589             tcg_res[1] = tcg_constant_i64(0);
13590         }
13591 
13592         for (pass = 0; pass < 2; pass++) {
13593             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13594         }
13595     }
13596 }
13597 
13598 /* Crypto AES
13599  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13600  * +-----------------+------+-----------+--------+-----+------+------+
13601  * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13602  * +-----------------+------+-----------+--------+-----+------+------+
13603  */
13604 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13605 {
13606     int size = extract32(insn, 22, 2);
13607     int opcode = extract32(insn, 12, 5);
13608     int rn = extract32(insn, 5, 5);
13609     int rd = extract32(insn, 0, 5);
13610     int decrypt;
13611     gen_helper_gvec_2 *genfn2 = NULL;
13612     gen_helper_gvec_3 *genfn3 = NULL;
13613 
13614     if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13615         unallocated_encoding(s);
13616         return;
13617     }
13618 
13619     switch (opcode) {
13620     case 0x4: /* AESE */
13621         decrypt = 0;
13622         genfn3 = gen_helper_crypto_aese;
13623         break;
13624     case 0x6: /* AESMC */
13625         decrypt = 0;
13626         genfn2 = gen_helper_crypto_aesmc;
13627         break;
13628     case 0x5: /* AESD */
13629         decrypt = 1;
13630         genfn3 = gen_helper_crypto_aese;
13631         break;
13632     case 0x7: /* AESIMC */
13633         decrypt = 1;
13634         genfn2 = gen_helper_crypto_aesmc;
13635         break;
13636     default:
13637         unallocated_encoding(s);
13638         return;
13639     }
13640 
13641     if (!fp_access_check(s)) {
13642         return;
13643     }
13644     if (genfn2) {
13645         gen_gvec_op2_ool(s, true, rd, rn, decrypt, genfn2);
13646     } else {
13647         gen_gvec_op3_ool(s, true, rd, rd, rn, decrypt, genfn3);
13648     }
13649 }
13650 
13651 /* Crypto three-reg SHA
13652  *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
13653  * +-----------------+------+---+------+---+--------+-----+------+------+
13654  * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
13655  * +-----------------+------+---+------+---+--------+-----+------+------+
13656  */
13657 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13658 {
13659     int size = extract32(insn, 22, 2);
13660     int opcode = extract32(insn, 12, 3);
13661     int rm = extract32(insn, 16, 5);
13662     int rn = extract32(insn, 5, 5);
13663     int rd = extract32(insn, 0, 5);
13664     gen_helper_gvec_3 *genfn;
13665     bool feature;
13666 
13667     if (size != 0) {
13668         unallocated_encoding(s);
13669         return;
13670     }
13671 
13672     switch (opcode) {
13673     case 0: /* SHA1C */
13674         genfn = gen_helper_crypto_sha1c;
13675         feature = dc_isar_feature(aa64_sha1, s);
13676         break;
13677     case 1: /* SHA1P */
13678         genfn = gen_helper_crypto_sha1p;
13679         feature = dc_isar_feature(aa64_sha1, s);
13680         break;
13681     case 2: /* SHA1M */
13682         genfn = gen_helper_crypto_sha1m;
13683         feature = dc_isar_feature(aa64_sha1, s);
13684         break;
13685     case 3: /* SHA1SU0 */
13686         genfn = gen_helper_crypto_sha1su0;
13687         feature = dc_isar_feature(aa64_sha1, s);
13688         break;
13689     case 4: /* SHA256H */
13690         genfn = gen_helper_crypto_sha256h;
13691         feature = dc_isar_feature(aa64_sha256, s);
13692         break;
13693     case 5: /* SHA256H2 */
13694         genfn = gen_helper_crypto_sha256h2;
13695         feature = dc_isar_feature(aa64_sha256, s);
13696         break;
13697     case 6: /* SHA256SU1 */
13698         genfn = gen_helper_crypto_sha256su1;
13699         feature = dc_isar_feature(aa64_sha256, s);
13700         break;
13701     default:
13702         unallocated_encoding(s);
13703         return;
13704     }
13705 
13706     if (!feature) {
13707         unallocated_encoding(s);
13708         return;
13709     }
13710 
13711     if (!fp_access_check(s)) {
13712         return;
13713     }
13714     gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
13715 }
13716 
13717 /* Crypto two-reg SHA
13718  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13719  * +-----------------+------+-----------+--------+-----+------+------+
13720  * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13721  * +-----------------+------+-----------+--------+-----+------+------+
13722  */
13723 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13724 {
13725     int size = extract32(insn, 22, 2);
13726     int opcode = extract32(insn, 12, 5);
13727     int rn = extract32(insn, 5, 5);
13728     int rd = extract32(insn, 0, 5);
13729     gen_helper_gvec_2 *genfn;
13730     bool feature;
13731 
13732     if (size != 0) {
13733         unallocated_encoding(s);
13734         return;
13735     }
13736 
13737     switch (opcode) {
13738     case 0: /* SHA1H */
13739         feature = dc_isar_feature(aa64_sha1, s);
13740         genfn = gen_helper_crypto_sha1h;
13741         break;
13742     case 1: /* SHA1SU1 */
13743         feature = dc_isar_feature(aa64_sha1, s);
13744         genfn = gen_helper_crypto_sha1su1;
13745         break;
13746     case 2: /* SHA256SU0 */
13747         feature = dc_isar_feature(aa64_sha256, s);
13748         genfn = gen_helper_crypto_sha256su0;
13749         break;
13750     default:
13751         unallocated_encoding(s);
13752         return;
13753     }
13754 
13755     if (!feature) {
13756         unallocated_encoding(s);
13757         return;
13758     }
13759 
13760     if (!fp_access_check(s)) {
13761         return;
13762     }
13763     gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
13764 }
13765 
13766 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
13767 {
13768     tcg_gen_rotli_i64(d, m, 1);
13769     tcg_gen_xor_i64(d, d, n);
13770 }
13771 
13772 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
13773 {
13774     tcg_gen_rotli_vec(vece, d, m, 1);
13775     tcg_gen_xor_vec(vece, d, d, n);
13776 }
13777 
13778 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
13779                    uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
13780 {
13781     static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
13782     static const GVecGen3 op = {
13783         .fni8 = gen_rax1_i64,
13784         .fniv = gen_rax1_vec,
13785         .opt_opc = vecop_list,
13786         .fno = gen_helper_crypto_rax1,
13787         .vece = MO_64,
13788     };
13789     tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
13790 }
13791 
13792 /* Crypto three-reg SHA512
13793  *  31                   21 20  16 15  14  13 12  11  10  9    5 4    0
13794  * +-----------------------+------+---+---+-----+--------+------+------+
13795  * | 1 1 0 0 1 1 1 0 0 1 1 |  Rm  | 1 | O | 0 0 | opcode |  Rn  |  Rd  |
13796  * +-----------------------+------+---+---+-----+--------+------+------+
13797  */
13798 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13799 {
13800     int opcode = extract32(insn, 10, 2);
13801     int o =  extract32(insn, 14, 1);
13802     int rm = extract32(insn, 16, 5);
13803     int rn = extract32(insn, 5, 5);
13804     int rd = extract32(insn, 0, 5);
13805     bool feature;
13806     gen_helper_gvec_3 *oolfn = NULL;
13807     GVecGen3Fn *gvecfn = NULL;
13808 
13809     if (o == 0) {
13810         switch (opcode) {
13811         case 0: /* SHA512H */
13812             feature = dc_isar_feature(aa64_sha512, s);
13813             oolfn = gen_helper_crypto_sha512h;
13814             break;
13815         case 1: /* SHA512H2 */
13816             feature = dc_isar_feature(aa64_sha512, s);
13817             oolfn = gen_helper_crypto_sha512h2;
13818             break;
13819         case 2: /* SHA512SU1 */
13820             feature = dc_isar_feature(aa64_sha512, s);
13821             oolfn = gen_helper_crypto_sha512su1;
13822             break;
13823         case 3: /* RAX1 */
13824             feature = dc_isar_feature(aa64_sha3, s);
13825             gvecfn = gen_gvec_rax1;
13826             break;
13827         default:
13828             g_assert_not_reached();
13829         }
13830     } else {
13831         switch (opcode) {
13832         case 0: /* SM3PARTW1 */
13833             feature = dc_isar_feature(aa64_sm3, s);
13834             oolfn = gen_helper_crypto_sm3partw1;
13835             break;
13836         case 1: /* SM3PARTW2 */
13837             feature = dc_isar_feature(aa64_sm3, s);
13838             oolfn = gen_helper_crypto_sm3partw2;
13839             break;
13840         case 2: /* SM4EKEY */
13841             feature = dc_isar_feature(aa64_sm4, s);
13842             oolfn = gen_helper_crypto_sm4ekey;
13843             break;
13844         default:
13845             unallocated_encoding(s);
13846             return;
13847         }
13848     }
13849 
13850     if (!feature) {
13851         unallocated_encoding(s);
13852         return;
13853     }
13854 
13855     if (!fp_access_check(s)) {
13856         return;
13857     }
13858 
13859     if (oolfn) {
13860         gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
13861     } else {
13862         gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
13863     }
13864 }
13865 
13866 /* Crypto two-reg SHA512
13867  *  31                                     12  11  10  9    5 4    0
13868  * +-----------------------------------------+--------+------+------+
13869  * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode |  Rn  |  Rd  |
13870  * +-----------------------------------------+--------+------+------+
13871  */
13872 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13873 {
13874     int opcode = extract32(insn, 10, 2);
13875     int rn = extract32(insn, 5, 5);
13876     int rd = extract32(insn, 0, 5);
13877     bool feature;
13878 
13879     switch (opcode) {
13880     case 0: /* SHA512SU0 */
13881         feature = dc_isar_feature(aa64_sha512, s);
13882         break;
13883     case 1: /* SM4E */
13884         feature = dc_isar_feature(aa64_sm4, s);
13885         break;
13886     default:
13887         unallocated_encoding(s);
13888         return;
13889     }
13890 
13891     if (!feature) {
13892         unallocated_encoding(s);
13893         return;
13894     }
13895 
13896     if (!fp_access_check(s)) {
13897         return;
13898     }
13899 
13900     switch (opcode) {
13901     case 0: /* SHA512SU0 */
13902         gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
13903         break;
13904     case 1: /* SM4E */
13905         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
13906         break;
13907     default:
13908         g_assert_not_reached();
13909     }
13910 }
13911 
13912 /* Crypto four-register
13913  *  31               23 22 21 20  16 15  14  10 9    5 4    0
13914  * +-------------------+-----+------+---+------+------+------+
13915  * | 1 1 0 0 1 1 1 0 0 | Op0 |  Rm  | 0 |  Ra  |  Rn  |  Rd  |
13916  * +-------------------+-----+------+---+------+------+------+
13917  */
13918 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13919 {
13920     int op0 = extract32(insn, 21, 2);
13921     int rm = extract32(insn, 16, 5);
13922     int ra = extract32(insn, 10, 5);
13923     int rn = extract32(insn, 5, 5);
13924     int rd = extract32(insn, 0, 5);
13925     bool feature;
13926 
13927     switch (op0) {
13928     case 0: /* EOR3 */
13929     case 1: /* BCAX */
13930         feature = dc_isar_feature(aa64_sha3, s);
13931         break;
13932     case 2: /* SM3SS1 */
13933         feature = dc_isar_feature(aa64_sm3, s);
13934         break;
13935     default:
13936         unallocated_encoding(s);
13937         return;
13938     }
13939 
13940     if (!feature) {
13941         unallocated_encoding(s);
13942         return;
13943     }
13944 
13945     if (!fp_access_check(s)) {
13946         return;
13947     }
13948 
13949     if (op0 < 2) {
13950         TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13951         int pass;
13952 
13953         tcg_op1 = tcg_temp_new_i64();
13954         tcg_op2 = tcg_temp_new_i64();
13955         tcg_op3 = tcg_temp_new_i64();
13956         tcg_res[0] = tcg_temp_new_i64();
13957         tcg_res[1] = tcg_temp_new_i64();
13958 
13959         for (pass = 0; pass < 2; pass++) {
13960             read_vec_element(s, tcg_op1, rn, pass, MO_64);
13961             read_vec_element(s, tcg_op2, rm, pass, MO_64);
13962             read_vec_element(s, tcg_op3, ra, pass, MO_64);
13963 
13964             if (op0 == 0) {
13965                 /* EOR3 */
13966                 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13967             } else {
13968                 /* BCAX */
13969                 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13970             }
13971             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13972         }
13973         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13974         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13975     } else {
13976         TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13977 
13978         tcg_op1 = tcg_temp_new_i32();
13979         tcg_op2 = tcg_temp_new_i32();
13980         tcg_op3 = tcg_temp_new_i32();
13981         tcg_res = tcg_temp_new_i32();
13982         tcg_zero = tcg_constant_i32(0);
13983 
13984         read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13985         read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13986         read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13987 
13988         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13989         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13990         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13991         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13992 
13993         write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13994         write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13995         write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13996         write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13997     }
13998 }
13999 
14000 /* Crypto XAR
14001  *  31                   21 20  16 15    10 9    5 4    0
14002  * +-----------------------+------+--------+------+------+
14003  * | 1 1 0 0 1 1 1 0 1 0 0 |  Rm  |  imm6  |  Rn  |  Rd  |
14004  * +-----------------------+------+--------+------+------+
14005  */
14006 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
14007 {
14008     int rm = extract32(insn, 16, 5);
14009     int imm6 = extract32(insn, 10, 6);
14010     int rn = extract32(insn, 5, 5);
14011     int rd = extract32(insn, 0, 5);
14012 
14013     if (!dc_isar_feature(aa64_sha3, s)) {
14014         unallocated_encoding(s);
14015         return;
14016     }
14017 
14018     if (!fp_access_check(s)) {
14019         return;
14020     }
14021 
14022     gen_gvec_xar(MO_64, vec_full_reg_offset(s, rd),
14023                  vec_full_reg_offset(s, rn),
14024                  vec_full_reg_offset(s, rm), imm6, 16,
14025                  vec_full_reg_size(s));
14026 }
14027 
14028 /* Crypto three-reg imm2
14029  *  31                   21 20  16 15  14 13 12  11  10  9    5 4    0
14030  * +-----------------------+------+-----+------+--------+------+------+
14031  * | 1 1 0 0 1 1 1 0 0 1 0 |  Rm  | 1 0 | imm2 | opcode |  Rn  |  Rd  |
14032  * +-----------------------+------+-----+------+--------+------+------+
14033  */
14034 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
14035 {
14036     static gen_helper_gvec_3 * const fns[4] = {
14037         gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
14038         gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
14039     };
14040     int opcode = extract32(insn, 10, 2);
14041     int imm2 = extract32(insn, 12, 2);
14042     int rm = extract32(insn, 16, 5);
14043     int rn = extract32(insn, 5, 5);
14044     int rd = extract32(insn, 0, 5);
14045 
14046     if (!dc_isar_feature(aa64_sm3, s)) {
14047         unallocated_encoding(s);
14048         return;
14049     }
14050 
14051     if (!fp_access_check(s)) {
14052         return;
14053     }
14054 
14055     gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
14056 }
14057 
14058 /* C3.6 Data processing - SIMD, inc Crypto
14059  *
14060  * As the decode gets a little complex we are using a table based
14061  * approach for this part of the decode.
14062  */
14063 static const AArch64DecodeTable data_proc_simd[] = {
14064     /* pattern  ,  mask     ,  fn                        */
14065     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
14066     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
14067     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
14068     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
14069     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
14070     { 0x0e000400, 0x9fe08400, disas_simd_copy },
14071     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
14072     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
14073     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
14074     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
14075     { 0x0e000000, 0xbf208c00, disas_simd_tb },
14076     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
14077     { 0x2e000000, 0xbf208400, disas_simd_ext },
14078     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
14079     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
14080     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
14081     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
14082     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
14083     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
14084     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
14085     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
14086     { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
14087     { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
14088     { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
14089     { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
14090     { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
14091     { 0xce000000, 0xff808000, disas_crypto_four_reg },
14092     { 0xce800000, 0xffe00000, disas_crypto_xar },
14093     { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
14094     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
14095     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
14096     { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
14097     { 0x00000000, 0x00000000, NULL }
14098 };
14099 
14100 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
14101 {
14102     /* Note that this is called with all non-FP cases from
14103      * table C3-6 so it must UNDEF for entries not specifically
14104      * allocated to instructions in that table.
14105      */
14106     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
14107     if (fn) {
14108         fn(s, insn);
14109     } else {
14110         unallocated_encoding(s);
14111     }
14112 }
14113 
14114 /* C3.6 Data processing - SIMD and floating point */
14115 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
14116 {
14117     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
14118         disas_data_proc_fp(s, insn);
14119     } else {
14120         /* SIMD, including crypto */
14121         disas_data_proc_simd(s, insn);
14122     }
14123 }
14124 
14125 /*
14126  * Include the generated SME FA64 decoder.
14127  */
14128 
14129 #include "decode-sme-fa64.c.inc"
14130 
14131 static bool trans_OK(DisasContext *s, arg_OK *a)
14132 {
14133     return true;
14134 }
14135 
14136 static bool trans_FAIL(DisasContext *s, arg_OK *a)
14137 {
14138     s->is_nonstreaming = true;
14139     return true;
14140 }
14141 
14142 /**
14143  * is_guarded_page:
14144  * @env: The cpu environment
14145  * @s: The DisasContext
14146  *
14147  * Return true if the page is guarded.
14148  */
14149 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
14150 {
14151     uint64_t addr = s->base.pc_first;
14152 #ifdef CONFIG_USER_ONLY
14153     return page_get_flags(addr) & PAGE_BTI;
14154 #else
14155     CPUTLBEntryFull *full;
14156     void *host;
14157     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
14158     int flags;
14159 
14160     /*
14161      * We test this immediately after reading an insn, which means
14162      * that the TLB entry must be present and valid, and thus this
14163      * access will never raise an exception.
14164      */
14165     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
14166                               false, &host, &full, 0);
14167     assert(!(flags & TLB_INVALID_MASK));
14168 
14169     return full->guarded;
14170 #endif
14171 }
14172 
14173 /**
14174  * btype_destination_ok:
14175  * @insn: The instruction at the branch destination
14176  * @bt: SCTLR_ELx.BT
14177  * @btype: PSTATE.BTYPE, and is non-zero
14178  *
14179  * On a guarded page, there are a limited number of insns
14180  * that may be present at the branch target:
14181  *   - branch target identifiers,
14182  *   - paciasp, pacibsp,
14183  *   - BRK insn
14184  *   - HLT insn
14185  * Anything else causes a Branch Target Exception.
14186  *
14187  * Return true if the branch is compatible, false to raise BTITRAP.
14188  */
14189 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
14190 {
14191     if ((insn & 0xfffff01fu) == 0xd503201fu) {
14192         /* HINT space */
14193         switch (extract32(insn, 5, 7)) {
14194         case 0b011001: /* PACIASP */
14195         case 0b011011: /* PACIBSP */
14196             /*
14197              * If SCTLR_ELx.BT, then PACI*SP are not compatible
14198              * with btype == 3.  Otherwise all btype are ok.
14199              */
14200             return !bt || btype != 3;
14201         case 0b100000: /* BTI */
14202             /* Not compatible with any btype.  */
14203             return false;
14204         case 0b100010: /* BTI c */
14205             /* Not compatible with btype == 3 */
14206             return btype != 3;
14207         case 0b100100: /* BTI j */
14208             /* Not compatible with btype == 2 */
14209             return btype != 2;
14210         case 0b100110: /* BTI jc */
14211             /* Compatible with any btype.  */
14212             return true;
14213         }
14214     } else {
14215         switch (insn & 0xffe0001fu) {
14216         case 0xd4200000u: /* BRK */
14217         case 0xd4400000u: /* HLT */
14218             /* Give priority to the breakpoint exception.  */
14219             return true;
14220         }
14221     }
14222     return false;
14223 }
14224 
14225 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14226                                           CPUState *cpu)
14227 {
14228     DisasContext *dc = container_of(dcbase, DisasContext, base);
14229     CPUARMState *env = cpu->env_ptr;
14230     ARMCPU *arm_cpu = env_archcpu(env);
14231     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
14232     int bound, core_mmu_idx;
14233 
14234     dc->isar = &arm_cpu->isar;
14235     dc->condjmp = 0;
14236     dc->pc_save = dc->base.pc_first;
14237     dc->aarch64 = true;
14238     dc->thumb = false;
14239     dc->sctlr_b = 0;
14240     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
14241     dc->condexec_mask = 0;
14242     dc->condexec_cond = 0;
14243     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
14244     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
14245     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
14246     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
14247     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
14248     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14249 #if !defined(CONFIG_USER_ONLY)
14250     dc->user = (dc->current_el == 0);
14251 #endif
14252     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
14253     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
14254     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
14255     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
14256     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
14257     dc->fgt_eret = EX_TBFLAG_A64(tb_flags, FGT_ERET);
14258     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
14259     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
14260     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
14261     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
14262     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
14263     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
14264     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
14265     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
14266     dc->ata = EX_TBFLAG_A64(tb_flags, ATA);
14267     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
14268     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
14269     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
14270     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
14271     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
14272     dc->vec_len = 0;
14273     dc->vec_stride = 0;
14274     dc->cp_regs = arm_cpu->cp_regs;
14275     dc->features = env->features;
14276     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
14277 
14278 #ifdef CONFIG_USER_ONLY
14279     /* In sve_probe_page, we assume TBI is enabled. */
14280     tcg_debug_assert(dc->tbid & 1);
14281 #endif
14282 
14283     /* Single step state. The code-generation logic here is:
14284      *  SS_ACTIVE == 0:
14285      *   generate code with no special handling for single-stepping (except
14286      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14287      *   this happens anyway because those changes are all system register or
14288      *   PSTATE writes).
14289      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14290      *   emit code for one insn
14291      *   emit code to clear PSTATE.SS
14292      *   emit code to generate software step exception for completed step
14293      *   end TB (as usual for having generated an exception)
14294      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14295      *   emit code to generate a software step exception
14296      *   end the TB
14297      */
14298     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
14299     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
14300     dc->is_ldex = false;
14301 
14302     /* Bound the number of insns to execute to those left on the page.  */
14303     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14304 
14305     /* If architectural single step active, limit to 1.  */
14306     if (dc->ss_active) {
14307         bound = 1;
14308     }
14309     dc->base.max_insns = MIN(dc->base.max_insns, bound);
14310 }
14311 
14312 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14313 {
14314 }
14315 
14316 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14317 {
14318     DisasContext *dc = container_of(dcbase, DisasContext, base);
14319     target_ulong pc_arg = dc->base.pc_next;
14320 
14321     if (tb_cflags(dcbase->tb) & CF_PCREL) {
14322         pc_arg &= ~TARGET_PAGE_MASK;
14323     }
14324     tcg_gen_insn_start(pc_arg, 0, 0);
14325     dc->insn_start = tcg_last_op();
14326 }
14327 
14328 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14329 {
14330     DisasContext *s = container_of(dcbase, DisasContext, base);
14331     CPUARMState *env = cpu->env_ptr;
14332     uint64_t pc = s->base.pc_next;
14333     uint32_t insn;
14334 
14335     /* Singlestep exceptions have the highest priority. */
14336     if (s->ss_active && !s->pstate_ss) {
14337         /* Singlestep state is Active-pending.
14338          * If we're in this state at the start of a TB then either
14339          *  a) we just took an exception to an EL which is being debugged
14340          *     and this is the first insn in the exception handler
14341          *  b) debug exceptions were masked and we just unmasked them
14342          *     without changing EL (eg by clearing PSTATE.D)
14343          * In either case we're going to take a swstep exception in the
14344          * "did not step an insn" case, and so the syndrome ISV and EX
14345          * bits should be zero.
14346          */
14347         assert(s->base.num_insns == 1);
14348         gen_swstep_exception(s, 0, 0);
14349         s->base.is_jmp = DISAS_NORETURN;
14350         s->base.pc_next = pc + 4;
14351         return;
14352     }
14353 
14354     if (pc & 3) {
14355         /*
14356          * PC alignment fault.  This has priority over the instruction abort
14357          * that we would receive from a translation fault via arm_ldl_code.
14358          * This should only be possible after an indirect branch, at the
14359          * start of the TB.
14360          */
14361         assert(s->base.num_insns == 1);
14362         gen_helper_exception_pc_alignment(cpu_env, tcg_constant_tl(pc));
14363         s->base.is_jmp = DISAS_NORETURN;
14364         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
14365         return;
14366     }
14367 
14368     s->pc_curr = pc;
14369     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
14370     s->insn = insn;
14371     s->base.pc_next = pc + 4;
14372 
14373     s->fp_access_checked = false;
14374     s->sve_access_checked = false;
14375 
14376     if (s->pstate_il) {
14377         /*
14378          * Illegal execution state. This has priority over BTI
14379          * exceptions, but comes after instruction abort exceptions.
14380          */
14381         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
14382         return;
14383     }
14384 
14385     if (dc_isar_feature(aa64_bti, s)) {
14386         if (s->base.num_insns == 1) {
14387             /*
14388              * At the first insn of the TB, compute s->guarded_page.
14389              * We delayed computing this until successfully reading
14390              * the first insn of the TB, above.  This (mostly) ensures
14391              * that the softmmu tlb entry has been populated, and the
14392              * page table GP bit is available.
14393              *
14394              * Note that we need to compute this even if btype == 0,
14395              * because this value is used for BR instructions later
14396              * where ENV is not available.
14397              */
14398             s->guarded_page = is_guarded_page(env, s);
14399 
14400             /* First insn can have btype set to non-zero.  */
14401             tcg_debug_assert(s->btype >= 0);
14402 
14403             /*
14404              * Note that the Branch Target Exception has fairly high
14405              * priority -- below debugging exceptions but above most
14406              * everything else.  This allows us to handle this now
14407              * instead of waiting until the insn is otherwise decoded.
14408              */
14409             if (s->btype != 0
14410                 && s->guarded_page
14411                 && !btype_destination_ok(insn, s->bt, s->btype)) {
14412                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
14413                 return;
14414             }
14415         } else {
14416             /* Not the first insn: btype must be 0.  */
14417             tcg_debug_assert(s->btype == 0);
14418         }
14419     }
14420 
14421     s->is_nonstreaming = false;
14422     if (s->sme_trap_nonstreaming) {
14423         disas_sme_fa64(s, insn);
14424     }
14425 
14426     switch (extract32(insn, 25, 4)) {
14427     case 0x0:
14428         if (!extract32(insn, 31, 1) || !disas_sme(s, insn)) {
14429             unallocated_encoding(s);
14430         }
14431         break;
14432     case 0x1: case 0x3: /* UNALLOCATED */
14433         unallocated_encoding(s);
14434         break;
14435     case 0x2:
14436         if (!disas_sve(s, insn)) {
14437             unallocated_encoding(s);
14438         }
14439         break;
14440     case 0x8: case 0x9: /* Data processing - immediate */
14441         disas_data_proc_imm(s, insn);
14442         break;
14443     case 0xa: case 0xb: /* Branch, exception generation and system insns */
14444         disas_b_exc_sys(s, insn);
14445         break;
14446     case 0x4:
14447     case 0x6:
14448     case 0xc:
14449     case 0xe:      /* Loads and stores */
14450         disas_ldst(s, insn);
14451         break;
14452     case 0x5:
14453     case 0xd:      /* Data processing - register */
14454         disas_data_proc_reg(s, insn);
14455         break;
14456     case 0x7:
14457     case 0xf:      /* Data processing - SIMD and floating point */
14458         disas_data_proc_simd_fp(s, insn);
14459         break;
14460     default:
14461         assert(FALSE); /* all 15 cases should be handled above */
14462         break;
14463     }
14464 
14465     /*
14466      * After execution of most insns, btype is reset to 0.
14467      * Note that we set btype == -1 when the insn sets btype.
14468      */
14469     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14470         reset_btype(s);
14471     }
14472 }
14473 
14474 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14475 {
14476     DisasContext *dc = container_of(dcbase, DisasContext, base);
14477 
14478     if (unlikely(dc->ss_active)) {
14479         /* Note that this means single stepping WFI doesn't halt the CPU.
14480          * For conditional branch insns this is harmless unreachable code as
14481          * gen_goto_tb() has already handled emitting the debug exception
14482          * (and thus a tb-jump is not possible when singlestepping).
14483          */
14484         switch (dc->base.is_jmp) {
14485         default:
14486             gen_a64_update_pc(dc, 4);
14487             /* fall through */
14488         case DISAS_EXIT:
14489         case DISAS_JUMP:
14490             gen_step_complete_exception(dc);
14491             break;
14492         case DISAS_NORETURN:
14493             break;
14494         }
14495     } else {
14496         switch (dc->base.is_jmp) {
14497         case DISAS_NEXT:
14498         case DISAS_TOO_MANY:
14499             gen_goto_tb(dc, 1, 4);
14500             break;
14501         default:
14502         case DISAS_UPDATE_EXIT:
14503             gen_a64_update_pc(dc, 4);
14504             /* fall through */
14505         case DISAS_EXIT:
14506             tcg_gen_exit_tb(NULL, 0);
14507             break;
14508         case DISAS_UPDATE_NOCHAIN:
14509             gen_a64_update_pc(dc, 4);
14510             /* fall through */
14511         case DISAS_JUMP:
14512             tcg_gen_lookup_and_goto_ptr();
14513             break;
14514         case DISAS_NORETURN:
14515         case DISAS_SWI:
14516             break;
14517         case DISAS_WFE:
14518             gen_a64_update_pc(dc, 4);
14519             gen_helper_wfe(cpu_env);
14520             break;
14521         case DISAS_YIELD:
14522             gen_a64_update_pc(dc, 4);
14523             gen_helper_yield(cpu_env);
14524             break;
14525         case DISAS_WFI:
14526             /*
14527              * This is a special case because we don't want to just halt
14528              * the CPU if trying to debug across a WFI.
14529              */
14530             gen_a64_update_pc(dc, 4);
14531             gen_helper_wfi(cpu_env, tcg_constant_i32(4));
14532             /*
14533              * The helper doesn't necessarily throw an exception, but we
14534              * must go back to the main loop to check for interrupts anyway.
14535              */
14536             tcg_gen_exit_tb(NULL, 0);
14537             break;
14538         }
14539     }
14540 }
14541 
14542 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14543                                  CPUState *cpu, FILE *logfile)
14544 {
14545     DisasContext *dc = container_of(dcbase, DisasContext, base);
14546 
14547     fprintf(logfile, "IN: %s\n", lookup_symbol(dc->base.pc_first));
14548     target_disas(logfile, cpu, dc->base.pc_first, dc->base.tb->size);
14549 }
14550 
14551 const TranslatorOps aarch64_translator_ops = {
14552     .init_disas_context = aarch64_tr_init_disas_context,
14553     .tb_start           = aarch64_tr_tb_start,
14554     .insn_start         = aarch64_tr_insn_start,
14555     .translate_insn     = aarch64_tr_translate_insn,
14556     .tb_stop            = aarch64_tr_tb_stop,
14557     .disas_log          = aarch64_tr_disas_log,
14558 };
14559