xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision a2c4fb8c)
1 /*
2  *  AArch64 translation
3  *
4  *  Copyright (c) 2013 Alexander Graf <agraf@suse.de>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "qemu/osdep.h"
20 
21 #include "cpu.h"
22 #include "exec/exec-all.h"
23 #include "tcg/tcg-op.h"
24 #include "tcg/tcg-op-gvec.h"
25 #include "qemu/log.h"
26 #include "arm_ldst.h"
27 #include "translate.h"
28 #include "internals.h"
29 #include "qemu/host-utils.h"
30 #include "semihosting/semihost.h"
31 #include "exec/gen-icount.h"
32 #include "exec/helper-proto.h"
33 #include "exec/helper-gen.h"
34 #include "exec/log.h"
35 #include "cpregs.h"
36 #include "translate-a64.h"
37 #include "qemu/atomic128.h"
38 
39 static TCGv_i64 cpu_X[32];
40 static TCGv_i64 cpu_pc;
41 
42 /* Load/store exclusive handling */
43 static TCGv_i64 cpu_exclusive_high;
44 
45 static const char *regnames[] = {
46     "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
47     "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
48     "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
49     "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
50 };
51 
52 enum a64_shift_type {
53     A64_SHIFT_TYPE_LSL = 0,
54     A64_SHIFT_TYPE_LSR = 1,
55     A64_SHIFT_TYPE_ASR = 2,
56     A64_SHIFT_TYPE_ROR = 3
57 };
58 
59 /* Table based decoder typedefs - used when the relevant bits for decode
60  * are too awkwardly scattered across the instruction (eg SIMD).
61  */
62 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
63 
64 typedef struct AArch64DecodeTable {
65     uint32_t pattern;
66     uint32_t mask;
67     AArch64DecodeFn *disas_fn;
68 } AArch64DecodeTable;
69 
70 /* initialize TCG globals.  */
71 void a64_translate_init(void)
72 {
73     int i;
74 
75     cpu_pc = tcg_global_mem_new_i64(cpu_env,
76                                     offsetof(CPUARMState, pc),
77                                     "pc");
78     for (i = 0; i < 32; i++) {
79         cpu_X[i] = tcg_global_mem_new_i64(cpu_env,
80                                           offsetof(CPUARMState, xregs[i]),
81                                           regnames[i]);
82     }
83 
84     cpu_exclusive_high = tcg_global_mem_new_i64(cpu_env,
85         offsetof(CPUARMState, exclusive_high), "exclusive_high");
86 }
87 
88 /*
89  * Return the core mmu_idx to use for A64 "unprivileged load/store" insns
90  */
91 static int get_a64_user_mem_index(DisasContext *s)
92 {
93     /*
94      * If AccType_UNPRIV is not used, the insn uses AccType_NORMAL,
95      * which is the usual mmu_idx for this cpu state.
96      */
97     ARMMMUIdx useridx = s->mmu_idx;
98 
99     if (s->unpriv) {
100         /*
101          * We have pre-computed the condition for AccType_UNPRIV.
102          * Therefore we should never get here with a mmu_idx for
103          * which we do not know the corresponding user mmu_idx.
104          */
105         switch (useridx) {
106         case ARMMMUIdx_E10_1:
107         case ARMMMUIdx_E10_1_PAN:
108             useridx = ARMMMUIdx_E10_0;
109             break;
110         case ARMMMUIdx_E20_2:
111         case ARMMMUIdx_E20_2_PAN:
112             useridx = ARMMMUIdx_E20_0;
113             break;
114         default:
115             g_assert_not_reached();
116         }
117     }
118     return arm_to_core_mmu_idx(useridx);
119 }
120 
121 static void set_btype_raw(int val)
122 {
123     tcg_gen_st_i32(tcg_constant_i32(val), cpu_env,
124                    offsetof(CPUARMState, btype));
125 }
126 
127 static void set_btype(DisasContext *s, int val)
128 {
129     /* BTYPE is a 2-bit field, and 0 should be done with reset_btype.  */
130     tcg_debug_assert(val >= 1 && val <= 3);
131     set_btype_raw(val);
132     s->btype = -1;
133 }
134 
135 static void reset_btype(DisasContext *s)
136 {
137     if (s->btype != 0) {
138         set_btype_raw(0);
139         s->btype = 0;
140     }
141 }
142 
143 static void gen_pc_plus_diff(DisasContext *s, TCGv_i64 dest, target_long diff)
144 {
145     assert(s->pc_save != -1);
146     if (tb_cflags(s->base.tb) & CF_PCREL) {
147         tcg_gen_addi_i64(dest, cpu_pc, (s->pc_curr - s->pc_save) + diff);
148     } else {
149         tcg_gen_movi_i64(dest, s->pc_curr + diff);
150     }
151 }
152 
153 void gen_a64_update_pc(DisasContext *s, target_long diff)
154 {
155     gen_pc_plus_diff(s, cpu_pc, diff);
156     s->pc_save = s->pc_curr + diff;
157 }
158 
159 /*
160  * Handle Top Byte Ignore (TBI) bits.
161  *
162  * If address tagging is enabled via the TCR TBI bits:
163  *  + for EL2 and EL3 there is only one TBI bit, and if it is set
164  *    then the address is zero-extended, clearing bits [63:56]
165  *  + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
166  *    and TBI1 controls addressses with bit 55 == 1.
167  *    If the appropriate TBI bit is set for the address then
168  *    the address is sign-extended from bit 55 into bits [63:56]
169  *
170  * Here We have concatenated TBI{1,0} into tbi.
171  */
172 static void gen_top_byte_ignore(DisasContext *s, TCGv_i64 dst,
173                                 TCGv_i64 src, int tbi)
174 {
175     if (tbi == 0) {
176         /* Load unmodified address */
177         tcg_gen_mov_i64(dst, src);
178     } else if (!regime_has_2_ranges(s->mmu_idx)) {
179         /* Force tag byte to all zero */
180         tcg_gen_extract_i64(dst, src, 0, 56);
181     } else {
182         /* Sign-extend from bit 55.  */
183         tcg_gen_sextract_i64(dst, src, 0, 56);
184 
185         switch (tbi) {
186         case 1:
187             /* tbi0 but !tbi1: only use the extension if positive */
188             tcg_gen_and_i64(dst, dst, src);
189             break;
190         case 2:
191             /* !tbi0 but tbi1: only use the extension if negative */
192             tcg_gen_or_i64(dst, dst, src);
193             break;
194         case 3:
195             /* tbi0 and tbi1: always use the extension */
196             break;
197         default:
198             g_assert_not_reached();
199         }
200     }
201 }
202 
203 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
204 {
205     /*
206      * If address tagging is enabled for instructions via the TCR TBI bits,
207      * then loading an address into the PC will clear out any tag.
208      */
209     gen_top_byte_ignore(s, cpu_pc, src, s->tbii);
210     s->pc_save = -1;
211 }
212 
213 /*
214  * Handle MTE and/or TBI.
215  *
216  * For TBI, ideally, we would do nothing.  Proper behaviour on fault is
217  * for the tag to be present in the FAR_ELx register.  But for user-only
218  * mode we do not have a TLB with which to implement this, so we must
219  * remove the top byte now.
220  *
221  * Always return a fresh temporary that we can increment independently
222  * of the write-back address.
223  */
224 
225 TCGv_i64 clean_data_tbi(DisasContext *s, TCGv_i64 addr)
226 {
227     TCGv_i64 clean = tcg_temp_new_i64();
228 #ifdef CONFIG_USER_ONLY
229     gen_top_byte_ignore(s, clean, addr, s->tbid);
230 #else
231     tcg_gen_mov_i64(clean, addr);
232 #endif
233     return clean;
234 }
235 
236 /* Insert a zero tag into src, with the result at dst. */
237 static void gen_address_with_allocation_tag0(TCGv_i64 dst, TCGv_i64 src)
238 {
239     tcg_gen_andi_i64(dst, src, ~MAKE_64BIT_MASK(56, 4));
240 }
241 
242 static void gen_probe_access(DisasContext *s, TCGv_i64 ptr,
243                              MMUAccessType acc, int log2_size)
244 {
245     gen_helper_probe_access(cpu_env, ptr,
246                             tcg_constant_i32(acc),
247                             tcg_constant_i32(get_mem_index(s)),
248                             tcg_constant_i32(1 << log2_size));
249 }
250 
251 /*
252  * For MTE, check a single logical or atomic access.  This probes a single
253  * address, the exact one specified.  The size and alignment of the access
254  * is not relevant to MTE, per se, but watchpoints do require the size,
255  * and we want to recognize those before making any other changes to state.
256  */
257 static TCGv_i64 gen_mte_check1_mmuidx(DisasContext *s, TCGv_i64 addr,
258                                       bool is_write, bool tag_checked,
259                                       int log2_size, bool is_unpriv,
260                                       int core_idx)
261 {
262     if (tag_checked && s->mte_active[is_unpriv]) {
263         TCGv_i64 ret;
264         int desc = 0;
265 
266         desc = FIELD_DP32(desc, MTEDESC, MIDX, core_idx);
267         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
268         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
269         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
270         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, (1 << log2_size) - 1);
271 
272         ret = tcg_temp_new_i64();
273         gen_helper_mte_check(ret, cpu_env, tcg_constant_i32(desc), addr);
274 
275         return ret;
276     }
277     return clean_data_tbi(s, addr);
278 }
279 
280 TCGv_i64 gen_mte_check1(DisasContext *s, TCGv_i64 addr, bool is_write,
281                         bool tag_checked, int log2_size)
282 {
283     return gen_mte_check1_mmuidx(s, addr, is_write, tag_checked, log2_size,
284                                  false, get_mem_index(s));
285 }
286 
287 /*
288  * For MTE, check multiple logical sequential accesses.
289  */
290 TCGv_i64 gen_mte_checkN(DisasContext *s, TCGv_i64 addr, bool is_write,
291                         bool tag_checked, int size)
292 {
293     if (tag_checked && s->mte_active[0]) {
294         TCGv_i64 ret;
295         int desc = 0;
296 
297         desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
298         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
299         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
300         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
301         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, size - 1);
302 
303         ret = tcg_temp_new_i64();
304         gen_helper_mte_check(ret, cpu_env, tcg_constant_i32(desc), addr);
305 
306         return ret;
307     }
308     return clean_data_tbi(s, addr);
309 }
310 
311 typedef struct DisasCompare64 {
312     TCGCond cond;
313     TCGv_i64 value;
314 } DisasCompare64;
315 
316 static void a64_test_cc(DisasCompare64 *c64, int cc)
317 {
318     DisasCompare c32;
319 
320     arm_test_cc(&c32, cc);
321 
322     /*
323      * Sign-extend the 32-bit value so that the GE/LT comparisons work
324      * properly.  The NE/EQ comparisons are also fine with this choice.
325       */
326     c64->cond = c32.cond;
327     c64->value = tcg_temp_new_i64();
328     tcg_gen_ext_i32_i64(c64->value, c32.value);
329 }
330 
331 static void gen_rebuild_hflags(DisasContext *s)
332 {
333     gen_helper_rebuild_hflags_a64(cpu_env, tcg_constant_i32(s->current_el));
334 }
335 
336 static void gen_exception_internal(int excp)
337 {
338     assert(excp_is_internal(excp));
339     gen_helper_exception_internal(cpu_env, tcg_constant_i32(excp));
340 }
341 
342 static void gen_exception_internal_insn(DisasContext *s, int excp)
343 {
344     gen_a64_update_pc(s, 0);
345     gen_exception_internal(excp);
346     s->base.is_jmp = DISAS_NORETURN;
347 }
348 
349 static void gen_exception_bkpt_insn(DisasContext *s, uint32_t syndrome)
350 {
351     gen_a64_update_pc(s, 0);
352     gen_helper_exception_bkpt_insn(cpu_env, tcg_constant_i32(syndrome));
353     s->base.is_jmp = DISAS_NORETURN;
354 }
355 
356 static void gen_step_complete_exception(DisasContext *s)
357 {
358     /* We just completed step of an insn. Move from Active-not-pending
359      * to Active-pending, and then also take the swstep exception.
360      * This corresponds to making the (IMPDEF) choice to prioritize
361      * swstep exceptions over asynchronous exceptions taken to an exception
362      * level where debug is disabled. This choice has the advantage that
363      * we do not need to maintain internal state corresponding to the
364      * ISV/EX syndrome bits between completion of the step and generation
365      * of the exception, and our syndrome information is always correct.
366      */
367     gen_ss_advance(s);
368     gen_swstep_exception(s, 1, s->is_ldex);
369     s->base.is_jmp = DISAS_NORETURN;
370 }
371 
372 static inline bool use_goto_tb(DisasContext *s, uint64_t dest)
373 {
374     if (s->ss_active) {
375         return false;
376     }
377     return translator_use_goto_tb(&s->base, dest);
378 }
379 
380 static void gen_goto_tb(DisasContext *s, int n, int64_t diff)
381 {
382     if (use_goto_tb(s, s->pc_curr + diff)) {
383         /*
384          * For pcrel, the pc must always be up-to-date on entry to
385          * the linked TB, so that it can use simple additions for all
386          * further adjustments.  For !pcrel, the linked TB is compiled
387          * to know its full virtual address, so we can delay the
388          * update to pc to the unlinked path.  A long chain of links
389          * can thus avoid many updates to the PC.
390          */
391         if (tb_cflags(s->base.tb) & CF_PCREL) {
392             gen_a64_update_pc(s, diff);
393             tcg_gen_goto_tb(n);
394         } else {
395             tcg_gen_goto_tb(n);
396             gen_a64_update_pc(s, diff);
397         }
398         tcg_gen_exit_tb(s->base.tb, n);
399         s->base.is_jmp = DISAS_NORETURN;
400     } else {
401         gen_a64_update_pc(s, diff);
402         if (s->ss_active) {
403             gen_step_complete_exception(s);
404         } else {
405             tcg_gen_lookup_and_goto_ptr();
406             s->base.is_jmp = DISAS_NORETURN;
407         }
408     }
409 }
410 
411 /*
412  * Register access functions
413  *
414  * These functions are used for directly accessing a register in where
415  * changes to the final register value are likely to be made. If you
416  * need to use a register for temporary calculation (e.g. index type
417  * operations) use the read_* form.
418  *
419  * B1.2.1 Register mappings
420  *
421  * In instruction register encoding 31 can refer to ZR (zero register) or
422  * the SP (stack pointer) depending on context. In QEMU's case we map SP
423  * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
424  * This is the point of the _sp forms.
425  */
426 TCGv_i64 cpu_reg(DisasContext *s, int reg)
427 {
428     if (reg == 31) {
429         TCGv_i64 t = tcg_temp_new_i64();
430         tcg_gen_movi_i64(t, 0);
431         return t;
432     } else {
433         return cpu_X[reg];
434     }
435 }
436 
437 /* register access for when 31 == SP */
438 TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
439 {
440     return cpu_X[reg];
441 }
442 
443 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
444  * representing the register contents. This TCGv is an auto-freed
445  * temporary so it need not be explicitly freed, and may be modified.
446  */
447 TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
448 {
449     TCGv_i64 v = tcg_temp_new_i64();
450     if (reg != 31) {
451         if (sf) {
452             tcg_gen_mov_i64(v, cpu_X[reg]);
453         } else {
454             tcg_gen_ext32u_i64(v, cpu_X[reg]);
455         }
456     } else {
457         tcg_gen_movi_i64(v, 0);
458     }
459     return v;
460 }
461 
462 TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
463 {
464     TCGv_i64 v = tcg_temp_new_i64();
465     if (sf) {
466         tcg_gen_mov_i64(v, cpu_X[reg]);
467     } else {
468         tcg_gen_ext32u_i64(v, cpu_X[reg]);
469     }
470     return v;
471 }
472 
473 /* Return the offset into CPUARMState of a slice (from
474  * the least significant end) of FP register Qn (ie
475  * Dn, Sn, Hn or Bn).
476  * (Note that this is not the same mapping as for A32; see cpu.h)
477  */
478 static inline int fp_reg_offset(DisasContext *s, int regno, MemOp size)
479 {
480     return vec_reg_offset(s, regno, 0, size);
481 }
482 
483 /* Offset of the high half of the 128 bit vector Qn */
484 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
485 {
486     return vec_reg_offset(s, regno, 1, MO_64);
487 }
488 
489 /* Convenience accessors for reading and writing single and double
490  * FP registers. Writing clears the upper parts of the associated
491  * 128 bit vector register, as required by the architecture.
492  * Note that unlike the GP register accessors, the values returned
493  * by the read functions must be manually freed.
494  */
495 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
496 {
497     TCGv_i64 v = tcg_temp_new_i64();
498 
499     tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
500     return v;
501 }
502 
503 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
504 {
505     TCGv_i32 v = tcg_temp_new_i32();
506 
507     tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
508     return v;
509 }
510 
511 static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
512 {
513     TCGv_i32 v = tcg_temp_new_i32();
514 
515     tcg_gen_ld16u_i32(v, cpu_env, fp_reg_offset(s, reg, MO_16));
516     return v;
517 }
518 
519 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
520  * If SVE is not enabled, then there are only 128 bits in the vector.
521  */
522 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
523 {
524     unsigned ofs = fp_reg_offset(s, rd, MO_64);
525     unsigned vsz = vec_full_reg_size(s);
526 
527     /* Nop move, with side effect of clearing the tail. */
528     tcg_gen_gvec_mov(MO_64, ofs, ofs, is_q ? 16 : 8, vsz);
529 }
530 
531 void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
532 {
533     unsigned ofs = fp_reg_offset(s, reg, MO_64);
534 
535     tcg_gen_st_i64(v, cpu_env, ofs);
536     clear_vec_high(s, false, reg);
537 }
538 
539 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
540 {
541     TCGv_i64 tmp = tcg_temp_new_i64();
542 
543     tcg_gen_extu_i32_i64(tmp, v);
544     write_fp_dreg(s, reg, tmp);
545 }
546 
547 /* Expand a 2-operand AdvSIMD vector operation using an expander function.  */
548 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
549                          GVecGen2Fn *gvec_fn, int vece)
550 {
551     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
552             is_q ? 16 : 8, vec_full_reg_size(s));
553 }
554 
555 /* Expand a 2-operand + immediate AdvSIMD vector operation using
556  * an expander function.
557  */
558 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
559                           int64_t imm, GVecGen2iFn *gvec_fn, int vece)
560 {
561     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
562             imm, is_q ? 16 : 8, vec_full_reg_size(s));
563 }
564 
565 /* Expand a 3-operand AdvSIMD vector operation using an expander function.  */
566 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
567                          GVecGen3Fn *gvec_fn, int vece)
568 {
569     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
570             vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
571 }
572 
573 /* Expand a 4-operand AdvSIMD vector operation using an expander function.  */
574 static void gen_gvec_fn4(DisasContext *s, bool is_q, int rd, int rn, int rm,
575                          int rx, GVecGen4Fn *gvec_fn, int vece)
576 {
577     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
578             vec_full_reg_offset(s, rm), vec_full_reg_offset(s, rx),
579             is_q ? 16 : 8, vec_full_reg_size(s));
580 }
581 
582 /* Expand a 2-operand operation using an out-of-line helper.  */
583 static void gen_gvec_op2_ool(DisasContext *s, bool is_q, int rd,
584                              int rn, int data, gen_helper_gvec_2 *fn)
585 {
586     tcg_gen_gvec_2_ool(vec_full_reg_offset(s, rd),
587                        vec_full_reg_offset(s, rn),
588                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
589 }
590 
591 /* Expand a 3-operand operation using an out-of-line helper.  */
592 static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
593                              int rn, int rm, int data, gen_helper_gvec_3 *fn)
594 {
595     tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
596                        vec_full_reg_offset(s, rn),
597                        vec_full_reg_offset(s, rm),
598                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
599 }
600 
601 /* Expand a 3-operand + fpstatus pointer + simd data value operation using
602  * an out-of-line helper.
603  */
604 static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
605                               int rm, bool is_fp16, int data,
606                               gen_helper_gvec_3_ptr *fn)
607 {
608     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
609     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
610                        vec_full_reg_offset(s, rn),
611                        vec_full_reg_offset(s, rm), fpst,
612                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
613 }
614 
615 /* Expand a 3-operand + qc + operation using an out-of-line helper.  */
616 static void gen_gvec_op3_qc(DisasContext *s, bool is_q, int rd, int rn,
617                             int rm, gen_helper_gvec_3_ptr *fn)
618 {
619     TCGv_ptr qc_ptr = tcg_temp_new_ptr();
620 
621     tcg_gen_addi_ptr(qc_ptr, cpu_env, offsetof(CPUARMState, vfp.qc));
622     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
623                        vec_full_reg_offset(s, rn),
624                        vec_full_reg_offset(s, rm), qc_ptr,
625                        is_q ? 16 : 8, vec_full_reg_size(s), 0, fn);
626 }
627 
628 /* Expand a 4-operand operation using an out-of-line helper.  */
629 static void gen_gvec_op4_ool(DisasContext *s, bool is_q, int rd, int rn,
630                              int rm, int ra, int data, gen_helper_gvec_4 *fn)
631 {
632     tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
633                        vec_full_reg_offset(s, rn),
634                        vec_full_reg_offset(s, rm),
635                        vec_full_reg_offset(s, ra),
636                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
637 }
638 
639 /*
640  * Expand a 4-operand + fpstatus pointer + simd data value operation using
641  * an out-of-line helper.
642  */
643 static void gen_gvec_op4_fpst(DisasContext *s, bool is_q, int rd, int rn,
644                               int rm, int ra, bool is_fp16, int data,
645                               gen_helper_gvec_4_ptr *fn)
646 {
647     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
648     tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
649                        vec_full_reg_offset(s, rn),
650                        vec_full_reg_offset(s, rm),
651                        vec_full_reg_offset(s, ra), fpst,
652                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
653 }
654 
655 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
656  * than the 32 bit equivalent.
657  */
658 static inline void gen_set_NZ64(TCGv_i64 result)
659 {
660     tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
661     tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
662 }
663 
664 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
665 static inline void gen_logic_CC(int sf, TCGv_i64 result)
666 {
667     if (sf) {
668         gen_set_NZ64(result);
669     } else {
670         tcg_gen_extrl_i64_i32(cpu_ZF, result);
671         tcg_gen_mov_i32(cpu_NF, cpu_ZF);
672     }
673     tcg_gen_movi_i32(cpu_CF, 0);
674     tcg_gen_movi_i32(cpu_VF, 0);
675 }
676 
677 /* dest = T0 + T1; compute C, N, V and Z flags */
678 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
679 {
680     if (sf) {
681         TCGv_i64 result, flag, tmp;
682         result = tcg_temp_new_i64();
683         flag = tcg_temp_new_i64();
684         tmp = tcg_temp_new_i64();
685 
686         tcg_gen_movi_i64(tmp, 0);
687         tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
688 
689         tcg_gen_extrl_i64_i32(cpu_CF, flag);
690 
691         gen_set_NZ64(result);
692 
693         tcg_gen_xor_i64(flag, result, t0);
694         tcg_gen_xor_i64(tmp, t0, t1);
695         tcg_gen_andc_i64(flag, flag, tmp);
696         tcg_gen_extrh_i64_i32(cpu_VF, flag);
697 
698         tcg_gen_mov_i64(dest, result);
699     } else {
700         /* 32 bit arithmetic */
701         TCGv_i32 t0_32 = tcg_temp_new_i32();
702         TCGv_i32 t1_32 = tcg_temp_new_i32();
703         TCGv_i32 tmp = tcg_temp_new_i32();
704 
705         tcg_gen_movi_i32(tmp, 0);
706         tcg_gen_extrl_i64_i32(t0_32, t0);
707         tcg_gen_extrl_i64_i32(t1_32, t1);
708         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
709         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
710         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
711         tcg_gen_xor_i32(tmp, t0_32, t1_32);
712         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
713         tcg_gen_extu_i32_i64(dest, cpu_NF);
714     }
715 }
716 
717 /* dest = T0 - T1; compute C, N, V and Z flags */
718 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
719 {
720     if (sf) {
721         /* 64 bit arithmetic */
722         TCGv_i64 result, flag, tmp;
723 
724         result = tcg_temp_new_i64();
725         flag = tcg_temp_new_i64();
726         tcg_gen_sub_i64(result, t0, t1);
727 
728         gen_set_NZ64(result);
729 
730         tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
731         tcg_gen_extrl_i64_i32(cpu_CF, flag);
732 
733         tcg_gen_xor_i64(flag, result, t0);
734         tmp = tcg_temp_new_i64();
735         tcg_gen_xor_i64(tmp, t0, t1);
736         tcg_gen_and_i64(flag, flag, tmp);
737         tcg_gen_extrh_i64_i32(cpu_VF, flag);
738         tcg_gen_mov_i64(dest, result);
739     } else {
740         /* 32 bit arithmetic */
741         TCGv_i32 t0_32 = tcg_temp_new_i32();
742         TCGv_i32 t1_32 = tcg_temp_new_i32();
743         TCGv_i32 tmp;
744 
745         tcg_gen_extrl_i64_i32(t0_32, t0);
746         tcg_gen_extrl_i64_i32(t1_32, t1);
747         tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
748         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
749         tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
750         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
751         tmp = tcg_temp_new_i32();
752         tcg_gen_xor_i32(tmp, t0_32, t1_32);
753         tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
754         tcg_gen_extu_i32_i64(dest, cpu_NF);
755     }
756 }
757 
758 /* dest = T0 + T1 + CF; do not compute flags. */
759 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
760 {
761     TCGv_i64 flag = tcg_temp_new_i64();
762     tcg_gen_extu_i32_i64(flag, cpu_CF);
763     tcg_gen_add_i64(dest, t0, t1);
764     tcg_gen_add_i64(dest, dest, flag);
765 
766     if (!sf) {
767         tcg_gen_ext32u_i64(dest, dest);
768     }
769 }
770 
771 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
772 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
773 {
774     if (sf) {
775         TCGv_i64 result = tcg_temp_new_i64();
776         TCGv_i64 cf_64 = tcg_temp_new_i64();
777         TCGv_i64 vf_64 = tcg_temp_new_i64();
778         TCGv_i64 tmp = tcg_temp_new_i64();
779         TCGv_i64 zero = tcg_constant_i64(0);
780 
781         tcg_gen_extu_i32_i64(cf_64, cpu_CF);
782         tcg_gen_add2_i64(result, cf_64, t0, zero, cf_64, zero);
783         tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, zero);
784         tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
785         gen_set_NZ64(result);
786 
787         tcg_gen_xor_i64(vf_64, result, t0);
788         tcg_gen_xor_i64(tmp, t0, t1);
789         tcg_gen_andc_i64(vf_64, vf_64, tmp);
790         tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
791 
792         tcg_gen_mov_i64(dest, result);
793     } else {
794         TCGv_i32 t0_32 = tcg_temp_new_i32();
795         TCGv_i32 t1_32 = tcg_temp_new_i32();
796         TCGv_i32 tmp = tcg_temp_new_i32();
797         TCGv_i32 zero = tcg_constant_i32(0);
798 
799         tcg_gen_extrl_i64_i32(t0_32, t0);
800         tcg_gen_extrl_i64_i32(t1_32, t1);
801         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, zero, cpu_CF, zero);
802         tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, zero);
803 
804         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
805         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
806         tcg_gen_xor_i32(tmp, t0_32, t1_32);
807         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
808         tcg_gen_extu_i32_i64(dest, cpu_NF);
809     }
810 }
811 
812 /*
813  * Load/Store generators
814  */
815 
816 /*
817  * Store from GPR register to memory.
818  */
819 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
820                              TCGv_i64 tcg_addr, MemOp memop, int memidx,
821                              bool iss_valid,
822                              unsigned int iss_srt,
823                              bool iss_sf, bool iss_ar)
824 {
825     memop = finalize_memop(s, memop);
826     tcg_gen_qemu_st_i64(source, tcg_addr, memidx, memop);
827 
828     if (iss_valid) {
829         uint32_t syn;
830 
831         syn = syn_data_abort_with_iss(0,
832                                       (memop & MO_SIZE),
833                                       false,
834                                       iss_srt,
835                                       iss_sf,
836                                       iss_ar,
837                                       0, 0, 0, 0, 0, false);
838         disas_set_insn_syndrome(s, syn);
839     }
840 }
841 
842 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
843                       TCGv_i64 tcg_addr, MemOp memop,
844                       bool iss_valid,
845                       unsigned int iss_srt,
846                       bool iss_sf, bool iss_ar)
847 {
848     do_gpr_st_memidx(s, source, tcg_addr, memop, get_mem_index(s),
849                      iss_valid, iss_srt, iss_sf, iss_ar);
850 }
851 
852 /*
853  * Load from memory to GPR register
854  */
855 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
856                              MemOp memop, bool extend, int memidx,
857                              bool iss_valid, unsigned int iss_srt,
858                              bool iss_sf, bool iss_ar)
859 {
860     memop = finalize_memop(s, memop);
861     tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
862 
863     if (extend && (memop & MO_SIGN)) {
864         g_assert((memop & MO_SIZE) <= MO_32);
865         tcg_gen_ext32u_i64(dest, dest);
866     }
867 
868     if (iss_valid) {
869         uint32_t syn;
870 
871         syn = syn_data_abort_with_iss(0,
872                                       (memop & MO_SIZE),
873                                       (memop & MO_SIGN) != 0,
874                                       iss_srt,
875                                       iss_sf,
876                                       iss_ar,
877                                       0, 0, 0, 0, 0, false);
878         disas_set_insn_syndrome(s, syn);
879     }
880 }
881 
882 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
883                       MemOp memop, bool extend,
884                       bool iss_valid, unsigned int iss_srt,
885                       bool iss_sf, bool iss_ar)
886 {
887     do_gpr_ld_memidx(s, dest, tcg_addr, memop, extend, get_mem_index(s),
888                      iss_valid, iss_srt, iss_sf, iss_ar);
889 }
890 
891 /*
892  * Store from FP register to memory
893  */
894 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
895 {
896     /* This writes the bottom N bits of a 128 bit wide vector to memory */
897     TCGv_i64 tmplo = tcg_temp_new_i64();
898     MemOp mop;
899 
900     tcg_gen_ld_i64(tmplo, cpu_env, fp_reg_offset(s, srcidx, MO_64));
901 
902     if (size < 4) {
903         mop = finalize_memop(s, size);
904         tcg_gen_qemu_st_i64(tmplo, tcg_addr, get_mem_index(s), mop);
905     } else {
906         bool be = s->be_data == MO_BE;
907         TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
908         TCGv_i64 tmphi = tcg_temp_new_i64();
909 
910         tcg_gen_ld_i64(tmphi, cpu_env, fp_reg_hi_offset(s, srcidx));
911 
912         mop = s->be_data | MO_UQ;
913         tcg_gen_qemu_st_i64(be ? tmphi : tmplo, tcg_addr, get_mem_index(s),
914                             mop | (s->align_mem ? MO_ALIGN_16 : 0));
915         tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
916         tcg_gen_qemu_st_i64(be ? tmplo : tmphi, tcg_hiaddr,
917                             get_mem_index(s), mop);
918     }
919 }
920 
921 /*
922  * Load from memory to FP register
923  */
924 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
925 {
926     /* This always zero-extends and writes to a full 128 bit wide vector */
927     TCGv_i64 tmplo = tcg_temp_new_i64();
928     TCGv_i64 tmphi = NULL;
929     MemOp mop;
930 
931     if (size < 4) {
932         mop = finalize_memop(s, size);
933         tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), mop);
934     } else {
935         bool be = s->be_data == MO_BE;
936         TCGv_i64 tcg_hiaddr;
937 
938         tmphi = tcg_temp_new_i64();
939         tcg_hiaddr = tcg_temp_new_i64();
940 
941         mop = s->be_data | MO_UQ;
942         tcg_gen_qemu_ld_i64(be ? tmphi : tmplo, tcg_addr, get_mem_index(s),
943                             mop | (s->align_mem ? MO_ALIGN_16 : 0));
944         tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
945         tcg_gen_qemu_ld_i64(be ? tmplo : tmphi, tcg_hiaddr,
946                             get_mem_index(s), mop);
947     }
948 
949     tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
950 
951     if (tmphi) {
952         tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
953     }
954     clear_vec_high(s, tmphi != NULL, destidx);
955 }
956 
957 /*
958  * Vector load/store helpers.
959  *
960  * The principal difference between this and a FP load is that we don't
961  * zero extend as we are filling a partial chunk of the vector register.
962  * These functions don't support 128 bit loads/stores, which would be
963  * normal load/store operations.
964  *
965  * The _i32 versions are useful when operating on 32 bit quantities
966  * (eg for floating point single or using Neon helper functions).
967  */
968 
969 /* Get value of an element within a vector register */
970 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
971                              int element, MemOp memop)
972 {
973     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
974     switch ((unsigned)memop) {
975     case MO_8:
976         tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
977         break;
978     case MO_16:
979         tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
980         break;
981     case MO_32:
982         tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
983         break;
984     case MO_8|MO_SIGN:
985         tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
986         break;
987     case MO_16|MO_SIGN:
988         tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
989         break;
990     case MO_32|MO_SIGN:
991         tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
992         break;
993     case MO_64:
994     case MO_64|MO_SIGN:
995         tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
996         break;
997     default:
998         g_assert_not_reached();
999     }
1000 }
1001 
1002 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1003                                  int element, MemOp memop)
1004 {
1005     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1006     switch (memop) {
1007     case MO_8:
1008         tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1009         break;
1010     case MO_16:
1011         tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1012         break;
1013     case MO_8|MO_SIGN:
1014         tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1015         break;
1016     case MO_16|MO_SIGN:
1017         tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1018         break;
1019     case MO_32:
1020     case MO_32|MO_SIGN:
1021         tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1022         break;
1023     default:
1024         g_assert_not_reached();
1025     }
1026 }
1027 
1028 /* Set value of an element within a vector register */
1029 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1030                               int element, MemOp memop)
1031 {
1032     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1033     switch (memop) {
1034     case MO_8:
1035         tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1036         break;
1037     case MO_16:
1038         tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1039         break;
1040     case MO_32:
1041         tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1042         break;
1043     case MO_64:
1044         tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1045         break;
1046     default:
1047         g_assert_not_reached();
1048     }
1049 }
1050 
1051 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1052                                   int destidx, int element, MemOp memop)
1053 {
1054     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1055     switch (memop) {
1056     case MO_8:
1057         tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1058         break;
1059     case MO_16:
1060         tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1061         break;
1062     case MO_32:
1063         tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1064         break;
1065     default:
1066         g_assert_not_reached();
1067     }
1068 }
1069 
1070 /* Store from vector register to memory */
1071 static void do_vec_st(DisasContext *s, int srcidx, int element,
1072                       TCGv_i64 tcg_addr, MemOp mop)
1073 {
1074     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1075 
1076     read_vec_element(s, tcg_tmp, srcidx, element, mop & MO_SIZE);
1077     tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1078 }
1079 
1080 /* Load from memory to vector register */
1081 static void do_vec_ld(DisasContext *s, int destidx, int element,
1082                       TCGv_i64 tcg_addr, MemOp mop)
1083 {
1084     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1085 
1086     tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1087     write_vec_element(s, tcg_tmp, destidx, element, mop & MO_SIZE);
1088 }
1089 
1090 /* Check that FP/Neon access is enabled. If it is, return
1091  * true. If not, emit code to generate an appropriate exception,
1092  * and return false; the caller should not emit any code for
1093  * the instruction. Note that this check must happen after all
1094  * unallocated-encoding checks (otherwise the syndrome information
1095  * for the resulting exception will be incorrect).
1096  */
1097 static bool fp_access_check_only(DisasContext *s)
1098 {
1099     if (s->fp_excp_el) {
1100         assert(!s->fp_access_checked);
1101         s->fp_access_checked = true;
1102 
1103         gen_exception_insn_el(s, 0, EXCP_UDEF,
1104                               syn_fp_access_trap(1, 0xe, false, 0),
1105                               s->fp_excp_el);
1106         return false;
1107     }
1108     s->fp_access_checked = true;
1109     return true;
1110 }
1111 
1112 static bool fp_access_check(DisasContext *s)
1113 {
1114     if (!fp_access_check_only(s)) {
1115         return false;
1116     }
1117     if (s->sme_trap_nonstreaming && s->is_nonstreaming) {
1118         gen_exception_insn(s, 0, EXCP_UDEF,
1119                            syn_smetrap(SME_ET_Streaming, false));
1120         return false;
1121     }
1122     return true;
1123 }
1124 
1125 /*
1126  * Check that SVE access is enabled.  If it is, return true.
1127  * If not, emit code to generate an appropriate exception and return false.
1128  * This function corresponds to CheckSVEEnabled().
1129  */
1130 bool sve_access_check(DisasContext *s)
1131 {
1132     if (s->pstate_sm || !dc_isar_feature(aa64_sve, s)) {
1133         assert(dc_isar_feature(aa64_sme, s));
1134         if (!sme_sm_enabled_check(s)) {
1135             goto fail_exit;
1136         }
1137     } else if (s->sve_excp_el) {
1138         gen_exception_insn_el(s, 0, EXCP_UDEF,
1139                               syn_sve_access_trap(), s->sve_excp_el);
1140         goto fail_exit;
1141     }
1142     s->sve_access_checked = true;
1143     return fp_access_check(s);
1144 
1145  fail_exit:
1146     /* Assert that we only raise one exception per instruction. */
1147     assert(!s->sve_access_checked);
1148     s->sve_access_checked = true;
1149     return false;
1150 }
1151 
1152 /*
1153  * Check that SME access is enabled, raise an exception if not.
1154  * Note that this function corresponds to CheckSMEAccess and is
1155  * only used directly for cpregs.
1156  */
1157 static bool sme_access_check(DisasContext *s)
1158 {
1159     if (s->sme_excp_el) {
1160         gen_exception_insn_el(s, 0, EXCP_UDEF,
1161                               syn_smetrap(SME_ET_AccessTrap, false),
1162                               s->sme_excp_el);
1163         return false;
1164     }
1165     return true;
1166 }
1167 
1168 /* This function corresponds to CheckSMEEnabled. */
1169 bool sme_enabled_check(DisasContext *s)
1170 {
1171     /*
1172      * Note that unlike sve_excp_el, we have not constrained sme_excp_el
1173      * to be zero when fp_excp_el has priority.  This is because we need
1174      * sme_excp_el by itself for cpregs access checks.
1175      */
1176     if (!s->fp_excp_el || s->sme_excp_el < s->fp_excp_el) {
1177         s->fp_access_checked = true;
1178         return sme_access_check(s);
1179     }
1180     return fp_access_check_only(s);
1181 }
1182 
1183 /* Common subroutine for CheckSMEAnd*Enabled. */
1184 bool sme_enabled_check_with_svcr(DisasContext *s, unsigned req)
1185 {
1186     if (!sme_enabled_check(s)) {
1187         return false;
1188     }
1189     if (FIELD_EX64(req, SVCR, SM) && !s->pstate_sm) {
1190         gen_exception_insn(s, 0, EXCP_UDEF,
1191                            syn_smetrap(SME_ET_NotStreaming, false));
1192         return false;
1193     }
1194     if (FIELD_EX64(req, SVCR, ZA) && !s->pstate_za) {
1195         gen_exception_insn(s, 0, EXCP_UDEF,
1196                            syn_smetrap(SME_ET_InactiveZA, false));
1197         return false;
1198     }
1199     return true;
1200 }
1201 
1202 /*
1203  * This utility function is for doing register extension with an
1204  * optional shift. You will likely want to pass a temporary for the
1205  * destination register. See DecodeRegExtend() in the ARM ARM.
1206  */
1207 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1208                               int option, unsigned int shift)
1209 {
1210     int extsize = extract32(option, 0, 2);
1211     bool is_signed = extract32(option, 2, 1);
1212 
1213     if (is_signed) {
1214         switch (extsize) {
1215         case 0:
1216             tcg_gen_ext8s_i64(tcg_out, tcg_in);
1217             break;
1218         case 1:
1219             tcg_gen_ext16s_i64(tcg_out, tcg_in);
1220             break;
1221         case 2:
1222             tcg_gen_ext32s_i64(tcg_out, tcg_in);
1223             break;
1224         case 3:
1225             tcg_gen_mov_i64(tcg_out, tcg_in);
1226             break;
1227         }
1228     } else {
1229         switch (extsize) {
1230         case 0:
1231             tcg_gen_ext8u_i64(tcg_out, tcg_in);
1232             break;
1233         case 1:
1234             tcg_gen_ext16u_i64(tcg_out, tcg_in);
1235             break;
1236         case 2:
1237             tcg_gen_ext32u_i64(tcg_out, tcg_in);
1238             break;
1239         case 3:
1240             tcg_gen_mov_i64(tcg_out, tcg_in);
1241             break;
1242         }
1243     }
1244 
1245     if (shift) {
1246         tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1247     }
1248 }
1249 
1250 static inline void gen_check_sp_alignment(DisasContext *s)
1251 {
1252     /* The AArch64 architecture mandates that (if enabled via PSTATE
1253      * or SCTLR bits) there is a check that SP is 16-aligned on every
1254      * SP-relative load or store (with an exception generated if it is not).
1255      * In line with general QEMU practice regarding misaligned accesses,
1256      * we omit these checks for the sake of guest program performance.
1257      * This function is provided as a hook so we can more easily add these
1258      * checks in future (possibly as a "favour catching guest program bugs
1259      * over speed" user selectable option).
1260      */
1261 }
1262 
1263 /*
1264  * This provides a simple table based table lookup decoder. It is
1265  * intended to be used when the relevant bits for decode are too
1266  * awkwardly placed and switch/if based logic would be confusing and
1267  * deeply nested. Since it's a linear search through the table, tables
1268  * should be kept small.
1269  *
1270  * It returns the first handler where insn & mask == pattern, or
1271  * NULL if there is no match.
1272  * The table is terminated by an empty mask (i.e. 0)
1273  */
1274 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1275                                                uint32_t insn)
1276 {
1277     const AArch64DecodeTable *tptr = table;
1278 
1279     while (tptr->mask) {
1280         if ((insn & tptr->mask) == tptr->pattern) {
1281             return tptr->disas_fn;
1282         }
1283         tptr++;
1284     }
1285     return NULL;
1286 }
1287 
1288 /*
1289  * The instruction disassembly implemented here matches
1290  * the instruction encoding classifications in chapter C4
1291  * of the ARM Architecture Reference Manual (DDI0487B_a);
1292  * classification names and decode diagrams here should generally
1293  * match up with those in the manual.
1294  */
1295 
1296 /* Unconditional branch (immediate)
1297  *   31  30       26 25                                  0
1298  * +----+-----------+-------------------------------------+
1299  * | op | 0 0 1 0 1 |                 imm26               |
1300  * +----+-----------+-------------------------------------+
1301  */
1302 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
1303 {
1304     int64_t diff = sextract32(insn, 0, 26) * 4;
1305 
1306     if (insn & (1U << 31)) {
1307         /* BL Branch with link */
1308         gen_pc_plus_diff(s, cpu_reg(s, 30), curr_insn_len(s));
1309     }
1310 
1311     /* B Branch / BL Branch with link */
1312     reset_btype(s);
1313     gen_goto_tb(s, 0, diff);
1314 }
1315 
1316 /* Compare and branch (immediate)
1317  *   31  30         25  24  23                  5 4      0
1318  * +----+-------------+----+---------------------+--------+
1319  * | sf | 0 1 1 0 1 0 | op |         imm19       |   Rt   |
1320  * +----+-------------+----+---------------------+--------+
1321  */
1322 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
1323 {
1324     unsigned int sf, op, rt;
1325     int64_t diff;
1326     DisasLabel match;
1327     TCGv_i64 tcg_cmp;
1328 
1329     sf = extract32(insn, 31, 1);
1330     op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
1331     rt = extract32(insn, 0, 5);
1332     diff = sextract32(insn, 5, 19) * 4;
1333 
1334     tcg_cmp = read_cpu_reg(s, rt, sf);
1335     reset_btype(s);
1336 
1337     match = gen_disas_label(s);
1338     tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1339                         tcg_cmp, 0, match.label);
1340     gen_goto_tb(s, 0, 4);
1341     set_disas_label(s, match);
1342     gen_goto_tb(s, 1, diff);
1343 }
1344 
1345 /* Test and branch (immediate)
1346  *   31  30         25  24  23   19 18          5 4    0
1347  * +----+-------------+----+-------+-------------+------+
1348  * | b5 | 0 1 1 0 1 1 | op |  b40  |    imm14    |  Rt  |
1349  * +----+-------------+----+-------+-------------+------+
1350  */
1351 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1352 {
1353     unsigned int bit_pos, op, rt;
1354     int64_t diff;
1355     DisasLabel match;
1356     TCGv_i64 tcg_cmp;
1357 
1358     bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1359     op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1360     diff = sextract32(insn, 5, 14) * 4;
1361     rt = extract32(insn, 0, 5);
1362 
1363     tcg_cmp = tcg_temp_new_i64();
1364     tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1365 
1366     reset_btype(s);
1367 
1368     match = gen_disas_label(s);
1369     tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1370                         tcg_cmp, 0, match.label);
1371     gen_goto_tb(s, 0, 4);
1372     set_disas_label(s, match);
1373     gen_goto_tb(s, 1, diff);
1374 }
1375 
1376 /* Conditional branch (immediate)
1377  *  31           25  24  23                  5   4  3    0
1378  * +---------------+----+---------------------+----+------+
1379  * | 0 1 0 1 0 1 0 | o1 |         imm19       | o0 | cond |
1380  * +---------------+----+---------------------+----+------+
1381  */
1382 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1383 {
1384     unsigned int cond;
1385     int64_t diff;
1386 
1387     if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1388         unallocated_encoding(s);
1389         return;
1390     }
1391     diff = sextract32(insn, 5, 19) * 4;
1392     cond = extract32(insn, 0, 4);
1393 
1394     reset_btype(s);
1395     if (cond < 0x0e) {
1396         /* genuinely conditional branches */
1397         DisasLabel match = gen_disas_label(s);
1398         arm_gen_test_cc(cond, match.label);
1399         gen_goto_tb(s, 0, 4);
1400         set_disas_label(s, match);
1401         gen_goto_tb(s, 1, diff);
1402     } else {
1403         /* 0xe and 0xf are both "always" conditions */
1404         gen_goto_tb(s, 0, diff);
1405     }
1406 }
1407 
1408 /* HINT instruction group, including various allocated HINTs */
1409 static void handle_hint(DisasContext *s, uint32_t insn,
1410                         unsigned int op1, unsigned int op2, unsigned int crm)
1411 {
1412     unsigned int selector = crm << 3 | op2;
1413 
1414     if (op1 != 3) {
1415         unallocated_encoding(s);
1416         return;
1417     }
1418 
1419     switch (selector) {
1420     case 0b00000: /* NOP */
1421         break;
1422     case 0b00011: /* WFI */
1423         s->base.is_jmp = DISAS_WFI;
1424         break;
1425     case 0b00001: /* YIELD */
1426         /* When running in MTTCG we don't generate jumps to the yield and
1427          * WFE helpers as it won't affect the scheduling of other vCPUs.
1428          * If we wanted to more completely model WFE/SEV so we don't busy
1429          * spin unnecessarily we would need to do something more involved.
1430          */
1431         if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1432             s->base.is_jmp = DISAS_YIELD;
1433         }
1434         break;
1435     case 0b00010: /* WFE */
1436         if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1437             s->base.is_jmp = DISAS_WFE;
1438         }
1439         break;
1440     case 0b00100: /* SEV */
1441     case 0b00101: /* SEVL */
1442     case 0b00110: /* DGH */
1443         /* we treat all as NOP at least for now */
1444         break;
1445     case 0b00111: /* XPACLRI */
1446         if (s->pauth_active) {
1447             gen_helper_xpaci(cpu_X[30], cpu_env, cpu_X[30]);
1448         }
1449         break;
1450     case 0b01000: /* PACIA1716 */
1451         if (s->pauth_active) {
1452             gen_helper_pacia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1453         }
1454         break;
1455     case 0b01010: /* PACIB1716 */
1456         if (s->pauth_active) {
1457             gen_helper_pacib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1458         }
1459         break;
1460     case 0b01100: /* AUTIA1716 */
1461         if (s->pauth_active) {
1462             gen_helper_autia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1463         }
1464         break;
1465     case 0b01110: /* AUTIB1716 */
1466         if (s->pauth_active) {
1467             gen_helper_autib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1468         }
1469         break;
1470     case 0b10000: /* ESB */
1471         /* Without RAS, we must implement this as NOP. */
1472         if (dc_isar_feature(aa64_ras, s)) {
1473             /*
1474              * QEMU does not have a source of physical SErrors,
1475              * so we are only concerned with virtual SErrors.
1476              * The pseudocode in the ARM for this case is
1477              *   if PSTATE.EL IN {EL0, EL1} && EL2Enabled() then
1478              *      AArch64.vESBOperation();
1479              * Most of the condition can be evaluated at translation time.
1480              * Test for EL2 present, and defer test for SEL2 to runtime.
1481              */
1482             if (s->current_el <= 1 && arm_dc_feature(s, ARM_FEATURE_EL2)) {
1483                 gen_helper_vesb(cpu_env);
1484             }
1485         }
1486         break;
1487     case 0b11000: /* PACIAZ */
1488         if (s->pauth_active) {
1489             gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30],
1490                              tcg_constant_i64(0));
1491         }
1492         break;
1493     case 0b11001: /* PACIASP */
1494         if (s->pauth_active) {
1495             gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1496         }
1497         break;
1498     case 0b11010: /* PACIBZ */
1499         if (s->pauth_active) {
1500             gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30],
1501                              tcg_constant_i64(0));
1502         }
1503         break;
1504     case 0b11011: /* PACIBSP */
1505         if (s->pauth_active) {
1506             gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1507         }
1508         break;
1509     case 0b11100: /* AUTIAZ */
1510         if (s->pauth_active) {
1511             gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30],
1512                              tcg_constant_i64(0));
1513         }
1514         break;
1515     case 0b11101: /* AUTIASP */
1516         if (s->pauth_active) {
1517             gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1518         }
1519         break;
1520     case 0b11110: /* AUTIBZ */
1521         if (s->pauth_active) {
1522             gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30],
1523                              tcg_constant_i64(0));
1524         }
1525         break;
1526     case 0b11111: /* AUTIBSP */
1527         if (s->pauth_active) {
1528             gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1529         }
1530         break;
1531     default:
1532         /* default specified as NOP equivalent */
1533         break;
1534     }
1535 }
1536 
1537 static void gen_clrex(DisasContext *s, uint32_t insn)
1538 {
1539     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1540 }
1541 
1542 /* CLREX, DSB, DMB, ISB */
1543 static void handle_sync(DisasContext *s, uint32_t insn,
1544                         unsigned int op1, unsigned int op2, unsigned int crm)
1545 {
1546     TCGBar bar;
1547 
1548     if (op1 != 3) {
1549         unallocated_encoding(s);
1550         return;
1551     }
1552 
1553     switch (op2) {
1554     case 2: /* CLREX */
1555         gen_clrex(s, insn);
1556         return;
1557     case 4: /* DSB */
1558     case 5: /* DMB */
1559         switch (crm & 3) {
1560         case 1: /* MBReqTypes_Reads */
1561             bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1562             break;
1563         case 2: /* MBReqTypes_Writes */
1564             bar = TCG_BAR_SC | TCG_MO_ST_ST;
1565             break;
1566         default: /* MBReqTypes_All */
1567             bar = TCG_BAR_SC | TCG_MO_ALL;
1568             break;
1569         }
1570         tcg_gen_mb(bar);
1571         return;
1572     case 6: /* ISB */
1573         /* We need to break the TB after this insn to execute
1574          * a self-modified code correctly and also to take
1575          * any pending interrupts immediately.
1576          */
1577         reset_btype(s);
1578         gen_goto_tb(s, 0, 4);
1579         return;
1580 
1581     case 7: /* SB */
1582         if (crm != 0 || !dc_isar_feature(aa64_sb, s)) {
1583             goto do_unallocated;
1584         }
1585         /*
1586          * TODO: There is no speculation barrier opcode for TCG;
1587          * MB and end the TB instead.
1588          */
1589         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1590         gen_goto_tb(s, 0, 4);
1591         return;
1592 
1593     default:
1594     do_unallocated:
1595         unallocated_encoding(s);
1596         return;
1597     }
1598 }
1599 
1600 static void gen_xaflag(void)
1601 {
1602     TCGv_i32 z = tcg_temp_new_i32();
1603 
1604     tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1605 
1606     /*
1607      * (!C & !Z) << 31
1608      * (!(C | Z)) << 31
1609      * ~((C | Z) << 31)
1610      * ~-(C | Z)
1611      * (C | Z) - 1
1612      */
1613     tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1614     tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1615 
1616     /* !(Z & C) */
1617     tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1618     tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1619 
1620     /* (!C & Z) << 31 -> -(Z & ~C) */
1621     tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1622     tcg_gen_neg_i32(cpu_VF, cpu_VF);
1623 
1624     /* C | Z */
1625     tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1626 }
1627 
1628 static void gen_axflag(void)
1629 {
1630     tcg_gen_sari_i32(cpu_VF, cpu_VF, 31);         /* V ? -1 : 0 */
1631     tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF);     /* C & !V */
1632 
1633     /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1634     tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1635 
1636     tcg_gen_movi_i32(cpu_NF, 0);
1637     tcg_gen_movi_i32(cpu_VF, 0);
1638 }
1639 
1640 /* MSR (immediate) - move immediate to processor state field */
1641 static void handle_msr_i(DisasContext *s, uint32_t insn,
1642                          unsigned int op1, unsigned int op2, unsigned int crm)
1643 {
1644     int op = op1 << 3 | op2;
1645 
1646     /* End the TB by default, chaining is ok.  */
1647     s->base.is_jmp = DISAS_TOO_MANY;
1648 
1649     switch (op) {
1650     case 0x00: /* CFINV */
1651         if (crm != 0 || !dc_isar_feature(aa64_condm_4, s)) {
1652             goto do_unallocated;
1653         }
1654         tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1655         s->base.is_jmp = DISAS_NEXT;
1656         break;
1657 
1658     case 0x01: /* XAFlag */
1659         if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1660             goto do_unallocated;
1661         }
1662         gen_xaflag();
1663         s->base.is_jmp = DISAS_NEXT;
1664         break;
1665 
1666     case 0x02: /* AXFlag */
1667         if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1668             goto do_unallocated;
1669         }
1670         gen_axflag();
1671         s->base.is_jmp = DISAS_NEXT;
1672         break;
1673 
1674     case 0x03: /* UAO */
1675         if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1676             goto do_unallocated;
1677         }
1678         if (crm & 1) {
1679             set_pstate_bits(PSTATE_UAO);
1680         } else {
1681             clear_pstate_bits(PSTATE_UAO);
1682         }
1683         gen_rebuild_hflags(s);
1684         break;
1685 
1686     case 0x04: /* PAN */
1687         if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1688             goto do_unallocated;
1689         }
1690         if (crm & 1) {
1691             set_pstate_bits(PSTATE_PAN);
1692         } else {
1693             clear_pstate_bits(PSTATE_PAN);
1694         }
1695         gen_rebuild_hflags(s);
1696         break;
1697 
1698     case 0x05: /* SPSel */
1699         if (s->current_el == 0) {
1700             goto do_unallocated;
1701         }
1702         gen_helper_msr_i_spsel(cpu_env, tcg_constant_i32(crm & PSTATE_SP));
1703         break;
1704 
1705     case 0x19: /* SSBS */
1706         if (!dc_isar_feature(aa64_ssbs, s)) {
1707             goto do_unallocated;
1708         }
1709         if (crm & 1) {
1710             set_pstate_bits(PSTATE_SSBS);
1711         } else {
1712             clear_pstate_bits(PSTATE_SSBS);
1713         }
1714         /* Don't need to rebuild hflags since SSBS is a nop */
1715         break;
1716 
1717     case 0x1a: /* DIT */
1718         if (!dc_isar_feature(aa64_dit, s)) {
1719             goto do_unallocated;
1720         }
1721         if (crm & 1) {
1722             set_pstate_bits(PSTATE_DIT);
1723         } else {
1724             clear_pstate_bits(PSTATE_DIT);
1725         }
1726         /* There's no need to rebuild hflags because DIT is a nop */
1727         break;
1728 
1729     case 0x1e: /* DAIFSet */
1730         gen_helper_msr_i_daifset(cpu_env, tcg_constant_i32(crm));
1731         break;
1732 
1733     case 0x1f: /* DAIFClear */
1734         gen_helper_msr_i_daifclear(cpu_env, tcg_constant_i32(crm));
1735         /* For DAIFClear, exit the cpu loop to re-evaluate pending IRQs.  */
1736         s->base.is_jmp = DISAS_UPDATE_EXIT;
1737         break;
1738 
1739     case 0x1c: /* TCO */
1740         if (dc_isar_feature(aa64_mte, s)) {
1741             /* Full MTE is enabled -- set the TCO bit as directed. */
1742             if (crm & 1) {
1743                 set_pstate_bits(PSTATE_TCO);
1744             } else {
1745                 clear_pstate_bits(PSTATE_TCO);
1746             }
1747             gen_rebuild_hflags(s);
1748             /* Many factors, including TCO, go into MTE_ACTIVE. */
1749             s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
1750         } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
1751             /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI.  */
1752             s->base.is_jmp = DISAS_NEXT;
1753         } else {
1754             goto do_unallocated;
1755         }
1756         break;
1757 
1758     case 0x1b: /* SVCR* */
1759         if (!dc_isar_feature(aa64_sme, s) || crm < 2 || crm > 7) {
1760             goto do_unallocated;
1761         }
1762         if (sme_access_check(s)) {
1763             int old = s->pstate_sm | (s->pstate_za << 1);
1764             int new = (crm & 1) * 3;
1765             int msk = (crm >> 1) & 3;
1766 
1767             if ((old ^ new) & msk) {
1768                 /* At least one bit changes. */
1769                 gen_helper_set_svcr(cpu_env, tcg_constant_i32(new),
1770                                     tcg_constant_i32(msk));
1771             } else {
1772                 s->base.is_jmp = DISAS_NEXT;
1773             }
1774         }
1775         break;
1776 
1777     default:
1778     do_unallocated:
1779         unallocated_encoding(s);
1780         return;
1781     }
1782 }
1783 
1784 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1785 {
1786     TCGv_i32 tmp = tcg_temp_new_i32();
1787     TCGv_i32 nzcv = tcg_temp_new_i32();
1788 
1789     /* build bit 31, N */
1790     tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
1791     /* build bit 30, Z */
1792     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1793     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1794     /* build bit 29, C */
1795     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1796     /* build bit 28, V */
1797     tcg_gen_shri_i32(tmp, cpu_VF, 31);
1798     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1799     /* generate result */
1800     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1801 }
1802 
1803 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1804 {
1805     TCGv_i32 nzcv = tcg_temp_new_i32();
1806 
1807     /* take NZCV from R[t] */
1808     tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
1809 
1810     /* bit 31, N */
1811     tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
1812     /* bit 30, Z */
1813     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1814     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1815     /* bit 29, C */
1816     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1817     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1818     /* bit 28, V */
1819     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1820     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1821 }
1822 
1823 static void gen_sysreg_undef(DisasContext *s, bool isread,
1824                              uint8_t op0, uint8_t op1, uint8_t op2,
1825                              uint8_t crn, uint8_t crm, uint8_t rt)
1826 {
1827     /*
1828      * Generate code to emit an UNDEF with correct syndrome
1829      * information for a failed system register access.
1830      * This is EC_UNCATEGORIZED (ie a standard UNDEF) in most cases,
1831      * but if FEAT_IDST is implemented then read accesses to registers
1832      * in the feature ID space are reported with the EC_SYSTEMREGISTERTRAP
1833      * syndrome.
1834      */
1835     uint32_t syndrome;
1836 
1837     if (isread && dc_isar_feature(aa64_ids, s) &&
1838         arm_cpreg_encoding_in_idspace(op0, op1, op2, crn, crm)) {
1839         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1840     } else {
1841         syndrome = syn_uncategorized();
1842     }
1843     gen_exception_insn(s, 0, EXCP_UDEF, syndrome);
1844 }
1845 
1846 /* MRS - move from system register
1847  * MSR (register) - move to system register
1848  * SYS
1849  * SYSL
1850  * These are all essentially the same insn in 'read' and 'write'
1851  * versions, with varying op0 fields.
1852  */
1853 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1854                        unsigned int op0, unsigned int op1, unsigned int op2,
1855                        unsigned int crn, unsigned int crm, unsigned int rt)
1856 {
1857     uint32_t key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1858                                       crn, crm, op0, op1, op2);
1859     const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
1860     TCGv_ptr tcg_ri = NULL;
1861     TCGv_i64 tcg_rt;
1862 
1863     if (!ri) {
1864         /* Unknown register; this might be a guest error or a QEMU
1865          * unimplemented feature.
1866          */
1867         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1868                       "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1869                       isread ? "read" : "write", op0, op1, crn, crm, op2);
1870         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
1871         return;
1872     }
1873 
1874     /* Check access permissions */
1875     if (!cp_access_ok(s->current_el, ri, isread)) {
1876         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
1877         return;
1878     }
1879 
1880     if (ri->accessfn || (ri->fgt && s->fgt_active)) {
1881         /* Emit code to perform further access permissions checks at
1882          * runtime; this may result in an exception.
1883          */
1884         uint32_t syndrome;
1885 
1886         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1887         gen_a64_update_pc(s, 0);
1888         tcg_ri = tcg_temp_new_ptr();
1889         gen_helper_access_check_cp_reg(tcg_ri, cpu_env,
1890                                        tcg_constant_i32(key),
1891                                        tcg_constant_i32(syndrome),
1892                                        tcg_constant_i32(isread));
1893     } else if (ri->type & ARM_CP_RAISES_EXC) {
1894         /*
1895          * The readfn or writefn might raise an exception;
1896          * synchronize the CPU state in case it does.
1897          */
1898         gen_a64_update_pc(s, 0);
1899     }
1900 
1901     /* Handle special cases first */
1902     switch (ri->type & ARM_CP_SPECIAL_MASK) {
1903     case 0:
1904         break;
1905     case ARM_CP_NOP:
1906         return;
1907     case ARM_CP_NZCV:
1908         tcg_rt = cpu_reg(s, rt);
1909         if (isread) {
1910             gen_get_nzcv(tcg_rt);
1911         } else {
1912             gen_set_nzcv(tcg_rt);
1913         }
1914         return;
1915     case ARM_CP_CURRENTEL:
1916         /* Reads as current EL value from pstate, which is
1917          * guaranteed to be constant by the tb flags.
1918          */
1919         tcg_rt = cpu_reg(s, rt);
1920         tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
1921         return;
1922     case ARM_CP_DC_ZVA:
1923         /* Writes clear the aligned block of memory which rt points into. */
1924         if (s->mte_active[0]) {
1925             int desc = 0;
1926 
1927             desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
1928             desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
1929             desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
1930 
1931             tcg_rt = tcg_temp_new_i64();
1932             gen_helper_mte_check_zva(tcg_rt, cpu_env,
1933                                      tcg_constant_i32(desc), cpu_reg(s, rt));
1934         } else {
1935             tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
1936         }
1937         gen_helper_dc_zva(cpu_env, tcg_rt);
1938         return;
1939     case ARM_CP_DC_GVA:
1940         {
1941             TCGv_i64 clean_addr, tag;
1942 
1943             /*
1944              * DC_GVA, like DC_ZVA, requires that we supply the original
1945              * pointer for an invalid page.  Probe that address first.
1946              */
1947             tcg_rt = cpu_reg(s, rt);
1948             clean_addr = clean_data_tbi(s, tcg_rt);
1949             gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
1950 
1951             if (s->ata) {
1952                 /* Extract the tag from the register to match STZGM.  */
1953                 tag = tcg_temp_new_i64();
1954                 tcg_gen_shri_i64(tag, tcg_rt, 56);
1955                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
1956             }
1957         }
1958         return;
1959     case ARM_CP_DC_GZVA:
1960         {
1961             TCGv_i64 clean_addr, tag;
1962 
1963             /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
1964             tcg_rt = cpu_reg(s, rt);
1965             clean_addr = clean_data_tbi(s, tcg_rt);
1966             gen_helper_dc_zva(cpu_env, clean_addr);
1967 
1968             if (s->ata) {
1969                 /* Extract the tag from the register to match STZGM.  */
1970                 tag = tcg_temp_new_i64();
1971                 tcg_gen_shri_i64(tag, tcg_rt, 56);
1972                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
1973             }
1974         }
1975         return;
1976     default:
1977         g_assert_not_reached();
1978     }
1979     if ((ri->type & ARM_CP_FPU) && !fp_access_check_only(s)) {
1980         return;
1981     } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
1982         return;
1983     } else if ((ri->type & ARM_CP_SME) && !sme_access_check(s)) {
1984         return;
1985     }
1986 
1987     if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1988         gen_io_start();
1989     }
1990 
1991     tcg_rt = cpu_reg(s, rt);
1992 
1993     if (isread) {
1994         if (ri->type & ARM_CP_CONST) {
1995             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1996         } else if (ri->readfn) {
1997             if (!tcg_ri) {
1998                 tcg_ri = gen_lookup_cp_reg(key);
1999             }
2000             gen_helper_get_cp_reg64(tcg_rt, cpu_env, tcg_ri);
2001         } else {
2002             tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
2003         }
2004     } else {
2005         if (ri->type & ARM_CP_CONST) {
2006             /* If not forbidden by access permissions, treat as WI */
2007             return;
2008         } else if (ri->writefn) {
2009             if (!tcg_ri) {
2010                 tcg_ri = gen_lookup_cp_reg(key);
2011             }
2012             gen_helper_set_cp_reg64(cpu_env, tcg_ri, tcg_rt);
2013         } else {
2014             tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
2015         }
2016     }
2017 
2018     if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
2019         /* I/O operations must end the TB here (whether read or write) */
2020         s->base.is_jmp = DISAS_UPDATE_EXIT;
2021     }
2022     if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
2023         /*
2024          * A write to any coprocessor regiser that ends a TB
2025          * must rebuild the hflags for the next TB.
2026          */
2027         gen_rebuild_hflags(s);
2028         /*
2029          * We default to ending the TB on a coprocessor register write,
2030          * but allow this to be suppressed by the register definition
2031          * (usually only necessary to work around guest bugs).
2032          */
2033         s->base.is_jmp = DISAS_UPDATE_EXIT;
2034     }
2035 }
2036 
2037 /* System
2038  *  31                 22 21  20 19 18 16 15   12 11    8 7   5 4    0
2039  * +---------------------+---+-----+-----+-------+-------+-----+------+
2040  * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 |  CRn  |  CRm  | op2 |  Rt  |
2041  * +---------------------+---+-----+-----+-------+-------+-----+------+
2042  */
2043 static void disas_system(DisasContext *s, uint32_t insn)
2044 {
2045     unsigned int l, op0, op1, crn, crm, op2, rt;
2046     l = extract32(insn, 21, 1);
2047     op0 = extract32(insn, 19, 2);
2048     op1 = extract32(insn, 16, 3);
2049     crn = extract32(insn, 12, 4);
2050     crm = extract32(insn, 8, 4);
2051     op2 = extract32(insn, 5, 3);
2052     rt = extract32(insn, 0, 5);
2053 
2054     if (op0 == 0) {
2055         if (l || rt != 31) {
2056             unallocated_encoding(s);
2057             return;
2058         }
2059         switch (crn) {
2060         case 2: /* HINT (including allocated hints like NOP, YIELD, etc) */
2061             handle_hint(s, insn, op1, op2, crm);
2062             break;
2063         case 3: /* CLREX, DSB, DMB, ISB */
2064             handle_sync(s, insn, op1, op2, crm);
2065             break;
2066         case 4: /* MSR (immediate) */
2067             handle_msr_i(s, insn, op1, op2, crm);
2068             break;
2069         default:
2070             unallocated_encoding(s);
2071             break;
2072         }
2073         return;
2074     }
2075     handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
2076 }
2077 
2078 /* Exception generation
2079  *
2080  *  31             24 23 21 20                     5 4   2 1  0
2081  * +-----------------+-----+------------------------+-----+----+
2082  * | 1 1 0 1 0 1 0 0 | opc |          imm16         | op2 | LL |
2083  * +-----------------------+------------------------+----------+
2084  */
2085 static void disas_exc(DisasContext *s, uint32_t insn)
2086 {
2087     int opc = extract32(insn, 21, 3);
2088     int op2_ll = extract32(insn, 0, 5);
2089     int imm16 = extract32(insn, 5, 16);
2090     uint32_t syndrome;
2091 
2092     switch (opc) {
2093     case 0:
2094         /* For SVC, HVC and SMC we advance the single-step state
2095          * machine before taking the exception. This is architecturally
2096          * mandated, to ensure that single-stepping a system call
2097          * instruction works properly.
2098          */
2099         switch (op2_ll) {
2100         case 1:                                                     /* SVC */
2101             syndrome = syn_aa64_svc(imm16);
2102             if (s->fgt_svc) {
2103                 gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2104                 break;
2105             }
2106             gen_ss_advance(s);
2107             gen_exception_insn(s, 4, EXCP_SWI, syndrome);
2108             break;
2109         case 2:                                                     /* HVC */
2110             if (s->current_el == 0) {
2111                 unallocated_encoding(s);
2112                 break;
2113             }
2114             /* The pre HVC helper handles cases when HVC gets trapped
2115              * as an undefined insn by runtime configuration.
2116              */
2117             gen_a64_update_pc(s, 0);
2118             gen_helper_pre_hvc(cpu_env);
2119             gen_ss_advance(s);
2120             gen_exception_insn_el(s, 4, EXCP_HVC, syn_aa64_hvc(imm16), 2);
2121             break;
2122         case 3:                                                     /* SMC */
2123             if (s->current_el == 0) {
2124                 unallocated_encoding(s);
2125                 break;
2126             }
2127             gen_a64_update_pc(s, 0);
2128             gen_helper_pre_smc(cpu_env, tcg_constant_i32(syn_aa64_smc(imm16)));
2129             gen_ss_advance(s);
2130             gen_exception_insn_el(s, 4, EXCP_SMC, syn_aa64_smc(imm16), 3);
2131             break;
2132         default:
2133             unallocated_encoding(s);
2134             break;
2135         }
2136         break;
2137     case 1:
2138         if (op2_ll != 0) {
2139             unallocated_encoding(s);
2140             break;
2141         }
2142         /* BRK */
2143         gen_exception_bkpt_insn(s, syn_aa64_bkpt(imm16));
2144         break;
2145     case 2:
2146         if (op2_ll != 0) {
2147             unallocated_encoding(s);
2148             break;
2149         }
2150         /* HLT. This has two purposes.
2151          * Architecturally, it is an external halting debug instruction.
2152          * Since QEMU doesn't implement external debug, we treat this as
2153          * it is required for halting debug disabled: it will UNDEF.
2154          * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2155          */
2156         if (semihosting_enabled(s->current_el == 0) && imm16 == 0xf000) {
2157             gen_exception_internal_insn(s, EXCP_SEMIHOST);
2158         } else {
2159             unallocated_encoding(s);
2160         }
2161         break;
2162     case 5:
2163         if (op2_ll < 1 || op2_ll > 3) {
2164             unallocated_encoding(s);
2165             break;
2166         }
2167         /* DCPS1, DCPS2, DCPS3 */
2168         unallocated_encoding(s);
2169         break;
2170     default:
2171         unallocated_encoding(s);
2172         break;
2173     }
2174 }
2175 
2176 /* Unconditional branch (register)
2177  *  31           25 24   21 20   16 15   10 9    5 4     0
2178  * +---------------+-------+-------+-------+------+-------+
2179  * | 1 1 0 1 0 1 1 |  opc  |  op2  |  op3  |  Rn  |  op4  |
2180  * +---------------+-------+-------+-------+------+-------+
2181  */
2182 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
2183 {
2184     unsigned int opc, op2, op3, rn, op4;
2185     unsigned btype_mod = 2;   /* 0: BR, 1: BLR, 2: other */
2186     TCGv_i64 dst;
2187     TCGv_i64 modifier;
2188 
2189     opc = extract32(insn, 21, 4);
2190     op2 = extract32(insn, 16, 5);
2191     op3 = extract32(insn, 10, 6);
2192     rn = extract32(insn, 5, 5);
2193     op4 = extract32(insn, 0, 5);
2194 
2195     if (op2 != 0x1f) {
2196         goto do_unallocated;
2197     }
2198 
2199     switch (opc) {
2200     case 0: /* BR */
2201     case 1: /* BLR */
2202     case 2: /* RET */
2203         btype_mod = opc;
2204         switch (op3) {
2205         case 0:
2206             /* BR, BLR, RET */
2207             if (op4 != 0) {
2208                 goto do_unallocated;
2209             }
2210             dst = cpu_reg(s, rn);
2211             break;
2212 
2213         case 2:
2214         case 3:
2215             if (!dc_isar_feature(aa64_pauth, s)) {
2216                 goto do_unallocated;
2217             }
2218             if (opc == 2) {
2219                 /* RETAA, RETAB */
2220                 if (rn != 0x1f || op4 != 0x1f) {
2221                     goto do_unallocated;
2222                 }
2223                 rn = 30;
2224                 modifier = cpu_X[31];
2225             } else {
2226                 /* BRAAZ, BRABZ, BLRAAZ, BLRABZ */
2227                 if (op4 != 0x1f) {
2228                     goto do_unallocated;
2229                 }
2230                 modifier = tcg_constant_i64(0);
2231             }
2232             if (s->pauth_active) {
2233                 dst = tcg_temp_new_i64();
2234                 if (op3 == 2) {
2235                     gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
2236                 } else {
2237                     gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2238                 }
2239             } else {
2240                 dst = cpu_reg(s, rn);
2241             }
2242             break;
2243 
2244         default:
2245             goto do_unallocated;
2246         }
2247         /* BLR also needs to load return address */
2248         if (opc == 1) {
2249             TCGv_i64 lr = cpu_reg(s, 30);
2250             if (dst == lr) {
2251                 TCGv_i64 tmp = tcg_temp_new_i64();
2252                 tcg_gen_mov_i64(tmp, dst);
2253                 dst = tmp;
2254             }
2255             gen_pc_plus_diff(s, lr, curr_insn_len(s));
2256         }
2257         gen_a64_set_pc(s, dst);
2258         break;
2259 
2260     case 8: /* BRAA */
2261     case 9: /* BLRAA */
2262         if (!dc_isar_feature(aa64_pauth, s)) {
2263             goto do_unallocated;
2264         }
2265         if ((op3 & ~1) != 2) {
2266             goto do_unallocated;
2267         }
2268         btype_mod = opc & 1;
2269         if (s->pauth_active) {
2270             dst = tcg_temp_new_i64();
2271             modifier = cpu_reg_sp(s, op4);
2272             if (op3 == 2) {
2273                 gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
2274             } else {
2275                 gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2276             }
2277         } else {
2278             dst = cpu_reg(s, rn);
2279         }
2280         /* BLRAA also needs to load return address */
2281         if (opc == 9) {
2282             TCGv_i64 lr = cpu_reg(s, 30);
2283             if (dst == lr) {
2284                 TCGv_i64 tmp = tcg_temp_new_i64();
2285                 tcg_gen_mov_i64(tmp, dst);
2286                 dst = tmp;
2287             }
2288             gen_pc_plus_diff(s, lr, curr_insn_len(s));
2289         }
2290         gen_a64_set_pc(s, dst);
2291         break;
2292 
2293     case 4: /* ERET */
2294         if (s->current_el == 0) {
2295             goto do_unallocated;
2296         }
2297         switch (op3) {
2298         case 0: /* ERET */
2299             if (op4 != 0) {
2300                 goto do_unallocated;
2301             }
2302             if (s->fgt_eret) {
2303                 gen_exception_insn_el(s, 0, EXCP_UDEF, syn_erettrap(op3), 2);
2304                 return;
2305             }
2306             dst = tcg_temp_new_i64();
2307             tcg_gen_ld_i64(dst, cpu_env,
2308                            offsetof(CPUARMState, elr_el[s->current_el]));
2309             break;
2310 
2311         case 2: /* ERETAA */
2312         case 3: /* ERETAB */
2313             if (!dc_isar_feature(aa64_pauth, s)) {
2314                 goto do_unallocated;
2315             }
2316             if (rn != 0x1f || op4 != 0x1f) {
2317                 goto do_unallocated;
2318             }
2319             /* The FGT trap takes precedence over an auth trap. */
2320             if (s->fgt_eret) {
2321                 gen_exception_insn_el(s, 0, EXCP_UDEF, syn_erettrap(op3), 2);
2322                 return;
2323             }
2324             dst = tcg_temp_new_i64();
2325             tcg_gen_ld_i64(dst, cpu_env,
2326                            offsetof(CPUARMState, elr_el[s->current_el]));
2327             if (s->pauth_active) {
2328                 modifier = cpu_X[31];
2329                 if (op3 == 2) {
2330                     gen_helper_autia(dst, cpu_env, dst, modifier);
2331                 } else {
2332                     gen_helper_autib(dst, cpu_env, dst, modifier);
2333                 }
2334             }
2335             break;
2336 
2337         default:
2338             goto do_unallocated;
2339         }
2340         if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2341             gen_io_start();
2342         }
2343 
2344         gen_helper_exception_return(cpu_env, dst);
2345         /* Must exit loop to check un-masked IRQs */
2346         s->base.is_jmp = DISAS_EXIT;
2347         return;
2348 
2349     case 5: /* DRPS */
2350         if (op3 != 0 || op4 != 0 || rn != 0x1f) {
2351             goto do_unallocated;
2352         } else {
2353             unallocated_encoding(s);
2354         }
2355         return;
2356 
2357     default:
2358     do_unallocated:
2359         unallocated_encoding(s);
2360         return;
2361     }
2362 
2363     switch (btype_mod) {
2364     case 0: /* BR */
2365         if (dc_isar_feature(aa64_bti, s)) {
2366             /* BR to {x16,x17} or !guard -> 1, else 3.  */
2367             set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
2368         }
2369         break;
2370 
2371     case 1: /* BLR */
2372         if (dc_isar_feature(aa64_bti, s)) {
2373             /* BLR sets BTYPE to 2, regardless of source guarded page.  */
2374             set_btype(s, 2);
2375         }
2376         break;
2377 
2378     default: /* RET or none of the above.  */
2379         /* BTYPE will be set to 0 by normal end-of-insn processing.  */
2380         break;
2381     }
2382 
2383     s->base.is_jmp = DISAS_JUMP;
2384 }
2385 
2386 /* Branches, exception generating and system instructions */
2387 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
2388 {
2389     switch (extract32(insn, 25, 7)) {
2390     case 0x0a: case 0x0b:
2391     case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
2392         disas_uncond_b_imm(s, insn);
2393         break;
2394     case 0x1a: case 0x5a: /* Compare & branch (immediate) */
2395         disas_comp_b_imm(s, insn);
2396         break;
2397     case 0x1b: case 0x5b: /* Test & branch (immediate) */
2398         disas_test_b_imm(s, insn);
2399         break;
2400     case 0x2a: /* Conditional branch (immediate) */
2401         disas_cond_b_imm(s, insn);
2402         break;
2403     case 0x6a: /* Exception generation / System */
2404         if (insn & (1 << 24)) {
2405             if (extract32(insn, 22, 2) == 0) {
2406                 disas_system(s, insn);
2407             } else {
2408                 unallocated_encoding(s);
2409             }
2410         } else {
2411             disas_exc(s, insn);
2412         }
2413         break;
2414     case 0x6b: /* Unconditional branch (register) */
2415         disas_uncond_b_reg(s, insn);
2416         break;
2417     default:
2418         unallocated_encoding(s);
2419         break;
2420     }
2421 }
2422 
2423 /*
2424  * Load/Store exclusive instructions are implemented by remembering
2425  * the value/address loaded, and seeing if these are the same
2426  * when the store is performed. This is not actually the architecturally
2427  * mandated semantics, but it works for typical guest code sequences
2428  * and avoids having to monitor regular stores.
2429  *
2430  * The store exclusive uses the atomic cmpxchg primitives to avoid
2431  * races in multi-threaded linux-user and when MTTCG softmmu is
2432  * enabled.
2433  */
2434 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
2435                                TCGv_i64 addr, int size, bool is_pair)
2436 {
2437     int idx = get_mem_index(s);
2438     MemOp memop = s->be_data;
2439 
2440     g_assert(size <= 3);
2441     if (is_pair) {
2442         g_assert(size >= 2);
2443         if (size == 2) {
2444             /* The pair must be single-copy atomic for the doubleword.  */
2445             memop |= MO_64 | MO_ALIGN;
2446             tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2447             if (s->be_data == MO_LE) {
2448                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2449                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2450             } else {
2451                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2452                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2453             }
2454         } else {
2455             /* The pair must be single-copy atomic for *each* doubleword, not
2456                the entire quadword, however it must be quadword aligned.  */
2457             memop |= MO_64;
2458             tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx,
2459                                 memop | MO_ALIGN_16);
2460 
2461             TCGv_i64 addr2 = tcg_temp_new_i64();
2462             tcg_gen_addi_i64(addr2, addr, 8);
2463             tcg_gen_qemu_ld_i64(cpu_exclusive_high, addr2, idx, memop);
2464 
2465             tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2466             tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2467         }
2468     } else {
2469         memop |= size | MO_ALIGN;
2470         tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2471         tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2472     }
2473     tcg_gen_mov_i64(cpu_exclusive_addr, addr);
2474 }
2475 
2476 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2477                                 TCGv_i64 addr, int size, int is_pair)
2478 {
2479     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2480      *     && (!is_pair || env->exclusive_high == [addr + datasize])) {
2481      *     [addr] = {Rt};
2482      *     if (is_pair) {
2483      *         [addr + datasize] = {Rt2};
2484      *     }
2485      *     {Rd} = 0;
2486      * } else {
2487      *     {Rd} = 1;
2488      * }
2489      * env->exclusive_addr = -1;
2490      */
2491     TCGLabel *fail_label = gen_new_label();
2492     TCGLabel *done_label = gen_new_label();
2493     TCGv_i64 tmp;
2494 
2495     tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
2496 
2497     tmp = tcg_temp_new_i64();
2498     if (is_pair) {
2499         if (size == 2) {
2500             if (s->be_data == MO_LE) {
2501                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2502             } else {
2503                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2504             }
2505             tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2506                                        cpu_exclusive_val, tmp,
2507                                        get_mem_index(s),
2508                                        MO_64 | MO_ALIGN | s->be_data);
2509             tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2510         } else {
2511             TCGv_i128 t16 = tcg_temp_new_i128();
2512             TCGv_i128 c16 = tcg_temp_new_i128();
2513             TCGv_i64 a, b;
2514 
2515             if (s->be_data == MO_LE) {
2516                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt), cpu_reg(s, rt2));
2517                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_val,
2518                                         cpu_exclusive_high);
2519             } else {
2520                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt2), cpu_reg(s, rt));
2521                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_high,
2522                                         cpu_exclusive_val);
2523             }
2524 
2525             tcg_gen_atomic_cmpxchg_i128(t16, cpu_exclusive_addr, c16, t16,
2526                                         get_mem_index(s),
2527                                         MO_128 | MO_ALIGN | s->be_data);
2528 
2529             a = tcg_temp_new_i64();
2530             b = tcg_temp_new_i64();
2531             if (s->be_data == MO_LE) {
2532                 tcg_gen_extr_i128_i64(a, b, t16);
2533             } else {
2534                 tcg_gen_extr_i128_i64(b, a, t16);
2535             }
2536 
2537             tcg_gen_xor_i64(a, a, cpu_exclusive_val);
2538             tcg_gen_xor_i64(b, b, cpu_exclusive_high);
2539             tcg_gen_or_i64(tmp, a, b);
2540 
2541             tcg_gen_setcondi_i64(TCG_COND_NE, tmp, tmp, 0);
2542         }
2543     } else {
2544         tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2545                                    cpu_reg(s, rt), get_mem_index(s),
2546                                    size | MO_ALIGN | s->be_data);
2547         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2548     }
2549     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2550     tcg_gen_br(done_label);
2551 
2552     gen_set_label(fail_label);
2553     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2554     gen_set_label(done_label);
2555     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2556 }
2557 
2558 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2559                                  int rn, int size)
2560 {
2561     TCGv_i64 tcg_rs = cpu_reg(s, rs);
2562     TCGv_i64 tcg_rt = cpu_reg(s, rt);
2563     int memidx = get_mem_index(s);
2564     TCGv_i64 clean_addr;
2565 
2566     if (rn == 31) {
2567         gen_check_sp_alignment(s);
2568     }
2569     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, size);
2570     tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt, memidx,
2571                                size | MO_ALIGN | s->be_data);
2572 }
2573 
2574 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2575                                       int rn, int size)
2576 {
2577     TCGv_i64 s1 = cpu_reg(s, rs);
2578     TCGv_i64 s2 = cpu_reg(s, rs + 1);
2579     TCGv_i64 t1 = cpu_reg(s, rt);
2580     TCGv_i64 t2 = cpu_reg(s, rt + 1);
2581     TCGv_i64 clean_addr;
2582     int memidx = get_mem_index(s);
2583 
2584     if (rn == 31) {
2585         gen_check_sp_alignment(s);
2586     }
2587 
2588     /* This is a single atomic access, despite the "pair". */
2589     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, size + 1);
2590 
2591     if (size == 2) {
2592         TCGv_i64 cmp = tcg_temp_new_i64();
2593         TCGv_i64 val = tcg_temp_new_i64();
2594 
2595         if (s->be_data == MO_LE) {
2596             tcg_gen_concat32_i64(val, t1, t2);
2597             tcg_gen_concat32_i64(cmp, s1, s2);
2598         } else {
2599             tcg_gen_concat32_i64(val, t2, t1);
2600             tcg_gen_concat32_i64(cmp, s2, s1);
2601         }
2602 
2603         tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx,
2604                                    MO_64 | MO_ALIGN | s->be_data);
2605 
2606         if (s->be_data == MO_LE) {
2607             tcg_gen_extr32_i64(s1, s2, cmp);
2608         } else {
2609             tcg_gen_extr32_i64(s2, s1, cmp);
2610         }
2611     } else {
2612         TCGv_i128 cmp = tcg_temp_new_i128();
2613         TCGv_i128 val = tcg_temp_new_i128();
2614 
2615         if (s->be_data == MO_LE) {
2616             tcg_gen_concat_i64_i128(val, t1, t2);
2617             tcg_gen_concat_i64_i128(cmp, s1, s2);
2618         } else {
2619             tcg_gen_concat_i64_i128(val, t2, t1);
2620             tcg_gen_concat_i64_i128(cmp, s2, s1);
2621         }
2622 
2623         tcg_gen_atomic_cmpxchg_i128(cmp, clean_addr, cmp, val, memidx,
2624                                     MO_128 | MO_ALIGN | s->be_data);
2625 
2626         if (s->be_data == MO_LE) {
2627             tcg_gen_extr_i128_i64(s1, s2, cmp);
2628         } else {
2629             tcg_gen_extr_i128_i64(s2, s1, cmp);
2630         }
2631     }
2632 }
2633 
2634 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
2635  * from the ARMv8 specs for LDR (Shared decode for all encodings).
2636  */
2637 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2638 {
2639     int opc0 = extract32(opc, 0, 1);
2640     int regsize;
2641 
2642     if (is_signed) {
2643         regsize = opc0 ? 32 : 64;
2644     } else {
2645         regsize = size == 3 ? 64 : 32;
2646     }
2647     return regsize == 64;
2648 }
2649 
2650 /* Load/store exclusive
2651  *
2652  *  31 30 29         24  23  22   21  20  16  15  14   10 9    5 4    0
2653  * +-----+-------------+----+---+----+------+----+-------+------+------+
2654  * | sz  | 0 0 1 0 0 0 | o2 | L | o1 |  Rs  | o0 |  Rt2  |  Rn  | Rt   |
2655  * +-----+-------------+----+---+----+------+----+-------+------+------+
2656  *
2657  *  sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2658  *   L: 0 -> store, 1 -> load
2659  *  o2: 0 -> exclusive, 1 -> not
2660  *  o1: 0 -> single register, 1 -> register pair
2661  *  o0: 1 -> load-acquire/store-release, 0 -> not
2662  */
2663 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2664 {
2665     int rt = extract32(insn, 0, 5);
2666     int rn = extract32(insn, 5, 5);
2667     int rt2 = extract32(insn, 10, 5);
2668     int rs = extract32(insn, 16, 5);
2669     int is_lasr = extract32(insn, 15, 1);
2670     int o2_L_o1_o0 = extract32(insn, 21, 3) * 2 | is_lasr;
2671     int size = extract32(insn, 30, 2);
2672     TCGv_i64 clean_addr;
2673 
2674     switch (o2_L_o1_o0) {
2675     case 0x0: /* STXR */
2676     case 0x1: /* STLXR */
2677         if (rn == 31) {
2678             gen_check_sp_alignment(s);
2679         }
2680         if (is_lasr) {
2681             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2682         }
2683         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2684                                     true, rn != 31, size);
2685         gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, false);
2686         return;
2687 
2688     case 0x4: /* LDXR */
2689     case 0x5: /* LDAXR */
2690         if (rn == 31) {
2691             gen_check_sp_alignment(s);
2692         }
2693         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2694                                     false, rn != 31, size);
2695         s->is_ldex = true;
2696         gen_load_exclusive(s, rt, rt2, clean_addr, size, false);
2697         if (is_lasr) {
2698             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2699         }
2700         return;
2701 
2702     case 0x8: /* STLLR */
2703         if (!dc_isar_feature(aa64_lor, s)) {
2704             break;
2705         }
2706         /* StoreLORelease is the same as Store-Release for QEMU.  */
2707         /* fall through */
2708     case 0x9: /* STLR */
2709         /* Generate ISS for non-exclusive accesses including LASR.  */
2710         if (rn == 31) {
2711             gen_check_sp_alignment(s);
2712         }
2713         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2714         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2715                                     true, rn != 31, size);
2716         /* TODO: ARMv8.4-LSE SCTLR.nAA */
2717         do_gpr_st(s, cpu_reg(s, rt), clean_addr, size | MO_ALIGN, true, rt,
2718                   disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2719         return;
2720 
2721     case 0xc: /* LDLAR */
2722         if (!dc_isar_feature(aa64_lor, s)) {
2723             break;
2724         }
2725         /* LoadLOAcquire is the same as Load-Acquire for QEMU.  */
2726         /* fall through */
2727     case 0xd: /* LDAR */
2728         /* Generate ISS for non-exclusive accesses including LASR.  */
2729         if (rn == 31) {
2730             gen_check_sp_alignment(s);
2731         }
2732         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2733                                     false, rn != 31, size);
2734         /* TODO: ARMv8.4-LSE SCTLR.nAA */
2735         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size | MO_ALIGN, false, true,
2736                   rt, disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2737         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2738         return;
2739 
2740     case 0x2: case 0x3: /* CASP / STXP */
2741         if (size & 2) { /* STXP / STLXP */
2742             if (rn == 31) {
2743                 gen_check_sp_alignment(s);
2744             }
2745             if (is_lasr) {
2746                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2747             }
2748             clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2749                                         true, rn != 31, size);
2750             gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, true);
2751             return;
2752         }
2753         if (rt2 == 31
2754             && ((rt | rs) & 1) == 0
2755             && dc_isar_feature(aa64_atomics, s)) {
2756             /* CASP / CASPL */
2757             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2758             return;
2759         }
2760         break;
2761 
2762     case 0x6: case 0x7: /* CASPA / LDXP */
2763         if (size & 2) { /* LDXP / LDAXP */
2764             if (rn == 31) {
2765                 gen_check_sp_alignment(s);
2766             }
2767             clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2768                                         false, rn != 31, size);
2769             s->is_ldex = true;
2770             gen_load_exclusive(s, rt, rt2, clean_addr, size, true);
2771             if (is_lasr) {
2772                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2773             }
2774             return;
2775         }
2776         if (rt2 == 31
2777             && ((rt | rs) & 1) == 0
2778             && dc_isar_feature(aa64_atomics, s)) {
2779             /* CASPA / CASPAL */
2780             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2781             return;
2782         }
2783         break;
2784 
2785     case 0xa: /* CAS */
2786     case 0xb: /* CASL */
2787     case 0xe: /* CASA */
2788     case 0xf: /* CASAL */
2789         if (rt2 == 31 && dc_isar_feature(aa64_atomics, s)) {
2790             gen_compare_and_swap(s, rs, rt, rn, size);
2791             return;
2792         }
2793         break;
2794     }
2795     unallocated_encoding(s);
2796 }
2797 
2798 /*
2799  * Load register (literal)
2800  *
2801  *  31 30 29   27  26 25 24 23                5 4     0
2802  * +-----+-------+---+-----+-------------------+-------+
2803  * | opc | 0 1 1 | V | 0 0 |     imm19         |  Rt   |
2804  * +-----+-------+---+-----+-------------------+-------+
2805  *
2806  * V: 1 -> vector (simd/fp)
2807  * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2808  *                   10-> 32 bit signed, 11 -> prefetch
2809  * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2810  */
2811 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2812 {
2813     int rt = extract32(insn, 0, 5);
2814     int64_t imm = sextract32(insn, 5, 19) << 2;
2815     bool is_vector = extract32(insn, 26, 1);
2816     int opc = extract32(insn, 30, 2);
2817     bool is_signed = false;
2818     int size = 2;
2819     TCGv_i64 tcg_rt, clean_addr;
2820 
2821     if (is_vector) {
2822         if (opc == 3) {
2823             unallocated_encoding(s);
2824             return;
2825         }
2826         size = 2 + opc;
2827         if (!fp_access_check(s)) {
2828             return;
2829         }
2830     } else {
2831         if (opc == 3) {
2832             /* PRFM (literal) : prefetch */
2833             return;
2834         }
2835         size = 2 + extract32(opc, 0, 1);
2836         is_signed = extract32(opc, 1, 1);
2837     }
2838 
2839     tcg_rt = cpu_reg(s, rt);
2840 
2841     clean_addr = tcg_temp_new_i64();
2842     gen_pc_plus_diff(s, clean_addr, imm);
2843     if (is_vector) {
2844         do_fp_ld(s, rt, clean_addr, size);
2845     } else {
2846         /* Only unsigned 32bit loads target 32bit registers.  */
2847         bool iss_sf = opc != 0;
2848 
2849         do_gpr_ld(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
2850                   false, true, rt, iss_sf, false);
2851     }
2852 }
2853 
2854 /*
2855  * LDNP (Load Pair - non-temporal hint)
2856  * LDP (Load Pair - non vector)
2857  * LDPSW (Load Pair Signed Word - non vector)
2858  * STNP (Store Pair - non-temporal hint)
2859  * STP (Store Pair - non vector)
2860  * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2861  * LDP (Load Pair of SIMD&FP)
2862  * STNP (Store Pair of SIMD&FP - non-temporal hint)
2863  * STP (Store Pair of SIMD&FP)
2864  *
2865  *  31 30 29   27  26  25 24   23  22 21   15 14   10 9    5 4    0
2866  * +-----+-------+---+---+-------+---+-----------------------------+
2867  * | opc | 1 0 1 | V | 0 | index | L |  imm7 |  Rt2  |  Rn  | Rt   |
2868  * +-----+-------+---+---+-------+---+-------+-------+------+------+
2869  *
2870  * opc: LDP/STP/LDNP/STNP        00 -> 32 bit, 10 -> 64 bit
2871  *      LDPSW/STGP               01
2872  *      LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2873  *   V: 0 -> GPR, 1 -> Vector
2874  * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2875  *      10 -> signed offset, 11 -> pre-index
2876  *   L: 0 -> Store 1 -> Load
2877  *
2878  * Rt, Rt2 = GPR or SIMD registers to be stored
2879  * Rn = general purpose register containing address
2880  * imm7 = signed offset (multiple of 4 or 8 depending on size)
2881  */
2882 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2883 {
2884     int rt = extract32(insn, 0, 5);
2885     int rn = extract32(insn, 5, 5);
2886     int rt2 = extract32(insn, 10, 5);
2887     uint64_t offset = sextract64(insn, 15, 7);
2888     int index = extract32(insn, 23, 2);
2889     bool is_vector = extract32(insn, 26, 1);
2890     bool is_load = extract32(insn, 22, 1);
2891     int opc = extract32(insn, 30, 2);
2892 
2893     bool is_signed = false;
2894     bool postindex = false;
2895     bool wback = false;
2896     bool set_tag = false;
2897 
2898     TCGv_i64 clean_addr, dirty_addr;
2899 
2900     int size;
2901 
2902     if (opc == 3) {
2903         unallocated_encoding(s);
2904         return;
2905     }
2906 
2907     if (is_vector) {
2908         size = 2 + opc;
2909     } else if (opc == 1 && !is_load) {
2910         /* STGP */
2911         if (!dc_isar_feature(aa64_mte_insn_reg, s) || index == 0) {
2912             unallocated_encoding(s);
2913             return;
2914         }
2915         size = 3;
2916         set_tag = true;
2917     } else {
2918         size = 2 + extract32(opc, 1, 1);
2919         is_signed = extract32(opc, 0, 1);
2920         if (!is_load && is_signed) {
2921             unallocated_encoding(s);
2922             return;
2923         }
2924     }
2925 
2926     switch (index) {
2927     case 1: /* post-index */
2928         postindex = true;
2929         wback = true;
2930         break;
2931     case 0:
2932         /* signed offset with "non-temporal" hint. Since we don't emulate
2933          * caches we don't care about hints to the cache system about
2934          * data access patterns, and handle this identically to plain
2935          * signed offset.
2936          */
2937         if (is_signed) {
2938             /* There is no non-temporal-hint version of LDPSW */
2939             unallocated_encoding(s);
2940             return;
2941         }
2942         postindex = false;
2943         break;
2944     case 2: /* signed offset, rn not updated */
2945         postindex = false;
2946         break;
2947     case 3: /* pre-index */
2948         postindex = false;
2949         wback = true;
2950         break;
2951     }
2952 
2953     if (is_vector && !fp_access_check(s)) {
2954         return;
2955     }
2956 
2957     offset <<= (set_tag ? LOG2_TAG_GRANULE : size);
2958 
2959     if (rn == 31) {
2960         gen_check_sp_alignment(s);
2961     }
2962 
2963     dirty_addr = read_cpu_reg_sp(s, rn, 1);
2964     if (!postindex) {
2965         tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2966     }
2967 
2968     if (set_tag) {
2969         if (!s->ata) {
2970             /*
2971              * TODO: We could rely on the stores below, at least for
2972              * system mode, if we arrange to add MO_ALIGN_16.
2973              */
2974             gen_helper_stg_stub(cpu_env, dirty_addr);
2975         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2976             gen_helper_stg_parallel(cpu_env, dirty_addr, dirty_addr);
2977         } else {
2978             gen_helper_stg(cpu_env, dirty_addr, dirty_addr);
2979         }
2980     }
2981 
2982     clean_addr = gen_mte_checkN(s, dirty_addr, !is_load,
2983                                 (wback || rn != 31) && !set_tag, 2 << size);
2984 
2985     if (is_vector) {
2986         if (is_load) {
2987             do_fp_ld(s, rt, clean_addr, size);
2988         } else {
2989             do_fp_st(s, rt, clean_addr, size);
2990         }
2991         tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2992         if (is_load) {
2993             do_fp_ld(s, rt2, clean_addr, size);
2994         } else {
2995             do_fp_st(s, rt2, clean_addr, size);
2996         }
2997     } else {
2998         TCGv_i64 tcg_rt = cpu_reg(s, rt);
2999         TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
3000 
3001         if (is_load) {
3002             TCGv_i64 tmp = tcg_temp_new_i64();
3003 
3004             /* Do not modify tcg_rt before recognizing any exception
3005              * from the second load.
3006              */
3007             do_gpr_ld(s, tmp, clean_addr, size + is_signed * MO_SIGN,
3008                       false, false, 0, false, false);
3009             tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
3010             do_gpr_ld(s, tcg_rt2, clean_addr, size + is_signed * MO_SIGN,
3011                       false, false, 0, false, false);
3012 
3013             tcg_gen_mov_i64(tcg_rt, tmp);
3014         } else {
3015             do_gpr_st(s, tcg_rt, clean_addr, size,
3016                       false, 0, false, false);
3017             tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
3018             do_gpr_st(s, tcg_rt2, clean_addr, size,
3019                       false, 0, false, false);
3020         }
3021     }
3022 
3023     if (wback) {
3024         if (postindex) {
3025             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3026         }
3027         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3028     }
3029 }
3030 
3031 /*
3032  * Load/store (immediate post-indexed)
3033  * Load/store (immediate pre-indexed)
3034  * Load/store (unscaled immediate)
3035  *
3036  * 31 30 29   27  26 25 24 23 22 21  20    12 11 10 9    5 4    0
3037  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3038  * |size| 1 1 1 | V | 0 0 | opc | 0 |  imm9  | idx |  Rn  |  Rt  |
3039  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3040  *
3041  * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
3042          10 -> unprivileged
3043  * V = 0 -> non-vector
3044  * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
3045  * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3046  */
3047 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
3048                                 int opc,
3049                                 int size,
3050                                 int rt,
3051                                 bool is_vector)
3052 {
3053     int rn = extract32(insn, 5, 5);
3054     int imm9 = sextract32(insn, 12, 9);
3055     int idx = extract32(insn, 10, 2);
3056     bool is_signed = false;
3057     bool is_store = false;
3058     bool is_extended = false;
3059     bool is_unpriv = (idx == 2);
3060     bool iss_valid;
3061     bool post_index;
3062     bool writeback;
3063     int memidx;
3064 
3065     TCGv_i64 clean_addr, dirty_addr;
3066 
3067     if (is_vector) {
3068         size |= (opc & 2) << 1;
3069         if (size > 4 || is_unpriv) {
3070             unallocated_encoding(s);
3071             return;
3072         }
3073         is_store = ((opc & 1) == 0);
3074         if (!fp_access_check(s)) {
3075             return;
3076         }
3077     } else {
3078         if (size == 3 && opc == 2) {
3079             /* PRFM - prefetch */
3080             if (idx != 0) {
3081                 unallocated_encoding(s);
3082                 return;
3083             }
3084             return;
3085         }
3086         if (opc == 3 && size > 1) {
3087             unallocated_encoding(s);
3088             return;
3089         }
3090         is_store = (opc == 0);
3091         is_signed = extract32(opc, 1, 1);
3092         is_extended = (size < 3) && extract32(opc, 0, 1);
3093     }
3094 
3095     switch (idx) {
3096     case 0:
3097     case 2:
3098         post_index = false;
3099         writeback = false;
3100         break;
3101     case 1:
3102         post_index = true;
3103         writeback = true;
3104         break;
3105     case 3:
3106         post_index = false;
3107         writeback = true;
3108         break;
3109     default:
3110         g_assert_not_reached();
3111     }
3112 
3113     iss_valid = !is_vector && !writeback;
3114 
3115     if (rn == 31) {
3116         gen_check_sp_alignment(s);
3117     }
3118 
3119     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3120     if (!post_index) {
3121         tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3122     }
3123 
3124     memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
3125     clean_addr = gen_mte_check1_mmuidx(s, dirty_addr, is_store,
3126                                        writeback || rn != 31,
3127                                        size, is_unpriv, memidx);
3128 
3129     if (is_vector) {
3130         if (is_store) {
3131             do_fp_st(s, rt, clean_addr, size);
3132         } else {
3133             do_fp_ld(s, rt, clean_addr, size);
3134         }
3135     } else {
3136         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3137         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3138 
3139         if (is_store) {
3140             do_gpr_st_memidx(s, tcg_rt, clean_addr, size, memidx,
3141                              iss_valid, rt, iss_sf, false);
3142         } else {
3143             do_gpr_ld_memidx(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
3144                              is_extended, memidx,
3145                              iss_valid, rt, iss_sf, false);
3146         }
3147     }
3148 
3149     if (writeback) {
3150         TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3151         if (post_index) {
3152             tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3153         }
3154         tcg_gen_mov_i64(tcg_rn, dirty_addr);
3155     }
3156 }
3157 
3158 /*
3159  * Load/store (register offset)
3160  *
3161  * 31 30 29   27  26 25 24 23 22 21  20  16 15 13 12 11 10 9  5 4  0
3162  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3163  * |size| 1 1 1 | V | 0 0 | opc | 1 |  Rm  | opt | S| 1 0 | Rn | Rt |
3164  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3165  *
3166  * For non-vector:
3167  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3168  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3169  * For vector:
3170  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3171  *   opc<0>: 0 -> store, 1 -> load
3172  * V: 1 -> vector/simd
3173  * opt: extend encoding (see DecodeRegExtend)
3174  * S: if S=1 then scale (essentially index by sizeof(size))
3175  * Rt: register to transfer into/out of
3176  * Rn: address register or SP for base
3177  * Rm: offset register or ZR for offset
3178  */
3179 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
3180                                    int opc,
3181                                    int size,
3182                                    int rt,
3183                                    bool is_vector)
3184 {
3185     int rn = extract32(insn, 5, 5);
3186     int shift = extract32(insn, 12, 1);
3187     int rm = extract32(insn, 16, 5);
3188     int opt = extract32(insn, 13, 3);
3189     bool is_signed = false;
3190     bool is_store = false;
3191     bool is_extended = false;
3192 
3193     TCGv_i64 tcg_rm, clean_addr, dirty_addr;
3194 
3195     if (extract32(opt, 1, 1) == 0) {
3196         unallocated_encoding(s);
3197         return;
3198     }
3199 
3200     if (is_vector) {
3201         size |= (opc & 2) << 1;
3202         if (size > 4) {
3203             unallocated_encoding(s);
3204             return;
3205         }
3206         is_store = !extract32(opc, 0, 1);
3207         if (!fp_access_check(s)) {
3208             return;
3209         }
3210     } else {
3211         if (size == 3 && opc == 2) {
3212             /* PRFM - prefetch */
3213             return;
3214         }
3215         if (opc == 3 && size > 1) {
3216             unallocated_encoding(s);
3217             return;
3218         }
3219         is_store = (opc == 0);
3220         is_signed = extract32(opc, 1, 1);
3221         is_extended = (size < 3) && extract32(opc, 0, 1);
3222     }
3223 
3224     if (rn == 31) {
3225         gen_check_sp_alignment(s);
3226     }
3227     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3228 
3229     tcg_rm = read_cpu_reg(s, rm, 1);
3230     ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
3231 
3232     tcg_gen_add_i64(dirty_addr, dirty_addr, tcg_rm);
3233     clean_addr = gen_mte_check1(s, dirty_addr, is_store, true, size);
3234 
3235     if (is_vector) {
3236         if (is_store) {
3237             do_fp_st(s, rt, clean_addr, size);
3238         } else {
3239             do_fp_ld(s, rt, clean_addr, size);
3240         }
3241     } else {
3242         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3243         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3244         if (is_store) {
3245             do_gpr_st(s, tcg_rt, clean_addr, size,
3246                       true, rt, iss_sf, false);
3247         } else {
3248             do_gpr_ld(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
3249                       is_extended, true, rt, iss_sf, false);
3250         }
3251     }
3252 }
3253 
3254 /*
3255  * Load/store (unsigned immediate)
3256  *
3257  * 31 30 29   27  26 25 24 23 22 21        10 9     5
3258  * +----+-------+---+-----+-----+------------+-------+------+
3259  * |size| 1 1 1 | V | 0 1 | opc |   imm12    |  Rn   |  Rt  |
3260  * +----+-------+---+-----+-----+------------+-------+------+
3261  *
3262  * For non-vector:
3263  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3264  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3265  * For vector:
3266  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3267  *   opc<0>: 0 -> store, 1 -> load
3268  * Rn: base address register (inc SP)
3269  * Rt: target register
3270  */
3271 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
3272                                         int opc,
3273                                         int size,
3274                                         int rt,
3275                                         bool is_vector)
3276 {
3277     int rn = extract32(insn, 5, 5);
3278     unsigned int imm12 = extract32(insn, 10, 12);
3279     unsigned int offset;
3280 
3281     TCGv_i64 clean_addr, dirty_addr;
3282 
3283     bool is_store;
3284     bool is_signed = false;
3285     bool is_extended = false;
3286 
3287     if (is_vector) {
3288         size |= (opc & 2) << 1;
3289         if (size > 4) {
3290             unallocated_encoding(s);
3291             return;
3292         }
3293         is_store = !extract32(opc, 0, 1);
3294         if (!fp_access_check(s)) {
3295             return;
3296         }
3297     } else {
3298         if (size == 3 && opc == 2) {
3299             /* PRFM - prefetch */
3300             return;
3301         }
3302         if (opc == 3 && size > 1) {
3303             unallocated_encoding(s);
3304             return;
3305         }
3306         is_store = (opc == 0);
3307         is_signed = extract32(opc, 1, 1);
3308         is_extended = (size < 3) && extract32(opc, 0, 1);
3309     }
3310 
3311     if (rn == 31) {
3312         gen_check_sp_alignment(s);
3313     }
3314     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3315     offset = imm12 << size;
3316     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3317     clean_addr = gen_mte_check1(s, dirty_addr, is_store, rn != 31, size);
3318 
3319     if (is_vector) {
3320         if (is_store) {
3321             do_fp_st(s, rt, clean_addr, size);
3322         } else {
3323             do_fp_ld(s, rt, clean_addr, size);
3324         }
3325     } else {
3326         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3327         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3328         if (is_store) {
3329             do_gpr_st(s, tcg_rt, clean_addr, size,
3330                       true, rt, iss_sf, false);
3331         } else {
3332             do_gpr_ld(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
3333                       is_extended, true, rt, iss_sf, false);
3334         }
3335     }
3336 }
3337 
3338 /* Atomic memory operations
3339  *
3340  *  31  30      27  26    24    22  21   16   15    12    10    5     0
3341  * +------+-------+---+-----+-----+---+----+----+-----+-----+----+-----+
3342  * | size | 1 1 1 | V | 0 0 | A R | 1 | Rs | o3 | opc | 0 0 | Rn |  Rt |
3343  * +------+-------+---+-----+-----+--------+----+-----+-----+----+-----+
3344  *
3345  * Rt: the result register
3346  * Rn: base address or SP
3347  * Rs: the source register for the operation
3348  * V: vector flag (always 0 as of v8.3)
3349  * A: acquire flag
3350  * R: release flag
3351  */
3352 static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
3353                               int size, int rt, bool is_vector)
3354 {
3355     int rs = extract32(insn, 16, 5);
3356     int rn = extract32(insn, 5, 5);
3357     int o3_opc = extract32(insn, 12, 4);
3358     bool r = extract32(insn, 22, 1);
3359     bool a = extract32(insn, 23, 1);
3360     TCGv_i64 tcg_rs, tcg_rt, clean_addr;
3361     AtomicThreeOpFn *fn = NULL;
3362     MemOp mop = s->be_data | size | MO_ALIGN;
3363 
3364     if (is_vector || !dc_isar_feature(aa64_atomics, s)) {
3365         unallocated_encoding(s);
3366         return;
3367     }
3368     switch (o3_opc) {
3369     case 000: /* LDADD */
3370         fn = tcg_gen_atomic_fetch_add_i64;
3371         break;
3372     case 001: /* LDCLR */
3373         fn = tcg_gen_atomic_fetch_and_i64;
3374         break;
3375     case 002: /* LDEOR */
3376         fn = tcg_gen_atomic_fetch_xor_i64;
3377         break;
3378     case 003: /* LDSET */
3379         fn = tcg_gen_atomic_fetch_or_i64;
3380         break;
3381     case 004: /* LDSMAX */
3382         fn = tcg_gen_atomic_fetch_smax_i64;
3383         mop |= MO_SIGN;
3384         break;
3385     case 005: /* LDSMIN */
3386         fn = tcg_gen_atomic_fetch_smin_i64;
3387         mop |= MO_SIGN;
3388         break;
3389     case 006: /* LDUMAX */
3390         fn = tcg_gen_atomic_fetch_umax_i64;
3391         break;
3392     case 007: /* LDUMIN */
3393         fn = tcg_gen_atomic_fetch_umin_i64;
3394         break;
3395     case 010: /* SWP */
3396         fn = tcg_gen_atomic_xchg_i64;
3397         break;
3398     case 014: /* LDAPR, LDAPRH, LDAPRB */
3399         if (!dc_isar_feature(aa64_rcpc_8_3, s) ||
3400             rs != 31 || a != 1 || r != 0) {
3401             unallocated_encoding(s);
3402             return;
3403         }
3404         break;
3405     default:
3406         unallocated_encoding(s);
3407         return;
3408     }
3409 
3410     if (rn == 31) {
3411         gen_check_sp_alignment(s);
3412     }
3413     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), false, rn != 31, size);
3414 
3415     if (o3_opc == 014) {
3416         /*
3417          * LDAPR* are a special case because they are a simple load, not a
3418          * fetch-and-do-something op.
3419          * The architectural consistency requirements here are weaker than
3420          * full load-acquire (we only need "load-acquire processor consistent"),
3421          * but we choose to implement them as full LDAQ.
3422          */
3423         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size, false,
3424                   true, rt, disas_ldst_compute_iss_sf(size, false, 0), true);
3425         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3426         return;
3427     }
3428 
3429     tcg_rs = read_cpu_reg(s, rs, true);
3430     tcg_rt = cpu_reg(s, rt);
3431 
3432     if (o3_opc == 1) { /* LDCLR */
3433         tcg_gen_not_i64(tcg_rs, tcg_rs);
3434     }
3435 
3436     /* The tcg atomic primitives are all full barriers.  Therefore we
3437      * can ignore the Acquire and Release bits of this instruction.
3438      */
3439     fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3440 
3441     if ((mop & MO_SIGN) && size != MO_64) {
3442         tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3443     }
3444 }
3445 
3446 /*
3447  * PAC memory operations
3448  *
3449  *  31  30      27  26    24    22  21       12  11  10    5     0
3450  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3451  * | size | 1 1 1 | V | 0 0 | M S | 1 |  imm9  | W | 1 | Rn |  Rt |
3452  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3453  *
3454  * Rt: the result register
3455  * Rn: base address or SP
3456  * V: vector flag (always 0 as of v8.3)
3457  * M: clear for key DA, set for key DB
3458  * W: pre-indexing flag
3459  * S: sign for imm9.
3460  */
3461 static void disas_ldst_pac(DisasContext *s, uint32_t insn,
3462                            int size, int rt, bool is_vector)
3463 {
3464     int rn = extract32(insn, 5, 5);
3465     bool is_wback = extract32(insn, 11, 1);
3466     bool use_key_a = !extract32(insn, 23, 1);
3467     int offset;
3468     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3469 
3470     if (size != 3 || is_vector || !dc_isar_feature(aa64_pauth, s)) {
3471         unallocated_encoding(s);
3472         return;
3473     }
3474 
3475     if (rn == 31) {
3476         gen_check_sp_alignment(s);
3477     }
3478     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3479 
3480     if (s->pauth_active) {
3481         if (use_key_a) {
3482             gen_helper_autda(dirty_addr, cpu_env, dirty_addr,
3483                              tcg_constant_i64(0));
3484         } else {
3485             gen_helper_autdb(dirty_addr, cpu_env, dirty_addr,
3486                              tcg_constant_i64(0));
3487         }
3488     }
3489 
3490     /* Form the 10-bit signed, scaled offset.  */
3491     offset = (extract32(insn, 22, 1) << 9) | extract32(insn, 12, 9);
3492     offset = sextract32(offset << size, 0, 10 + size);
3493     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3494 
3495     /* Note that "clean" and "dirty" here refer to TBI not PAC.  */
3496     clean_addr = gen_mte_check1(s, dirty_addr, false,
3497                                 is_wback || rn != 31, size);
3498 
3499     tcg_rt = cpu_reg(s, rt);
3500     do_gpr_ld(s, tcg_rt, clean_addr, size,
3501               /* extend */ false, /* iss_valid */ !is_wback,
3502               /* iss_srt */ rt, /* iss_sf */ true, /* iss_ar */ false);
3503 
3504     if (is_wback) {
3505         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3506     }
3507 }
3508 
3509 /*
3510  * LDAPR/STLR (unscaled immediate)
3511  *
3512  *  31  30            24    22  21       12    10    5     0
3513  * +------+-------------+-----+---+--------+-----+----+-----+
3514  * | size | 0 1 1 0 0 1 | opc | 0 |  imm9  | 0 0 | Rn |  Rt |
3515  * +------+-------------+-----+---+--------+-----+----+-----+
3516  *
3517  * Rt: source or destination register
3518  * Rn: base register
3519  * imm9: unscaled immediate offset
3520  * opc: 00: STLUR*, 01/10/11: various LDAPUR*
3521  * size: size of load/store
3522  */
3523 static void disas_ldst_ldapr_stlr(DisasContext *s, uint32_t insn)
3524 {
3525     int rt = extract32(insn, 0, 5);
3526     int rn = extract32(insn, 5, 5);
3527     int offset = sextract32(insn, 12, 9);
3528     int opc = extract32(insn, 22, 2);
3529     int size = extract32(insn, 30, 2);
3530     TCGv_i64 clean_addr, dirty_addr;
3531     bool is_store = false;
3532     bool extend = false;
3533     bool iss_sf;
3534     MemOp mop;
3535 
3536     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3537         unallocated_encoding(s);
3538         return;
3539     }
3540 
3541     /* TODO: ARMv8.4-LSE SCTLR.nAA */
3542     mop = size | MO_ALIGN;
3543 
3544     switch (opc) {
3545     case 0: /* STLURB */
3546         is_store = true;
3547         break;
3548     case 1: /* LDAPUR* */
3549         break;
3550     case 2: /* LDAPURS* 64-bit variant */
3551         if (size == 3) {
3552             unallocated_encoding(s);
3553             return;
3554         }
3555         mop |= MO_SIGN;
3556         break;
3557     case 3: /* LDAPURS* 32-bit variant */
3558         if (size > 1) {
3559             unallocated_encoding(s);
3560             return;
3561         }
3562         mop |= MO_SIGN;
3563         extend = true; /* zero-extend 32->64 after signed load */
3564         break;
3565     default:
3566         g_assert_not_reached();
3567     }
3568 
3569     iss_sf = disas_ldst_compute_iss_sf(size, (mop & MO_SIGN) != 0, opc);
3570 
3571     if (rn == 31) {
3572         gen_check_sp_alignment(s);
3573     }
3574 
3575     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3576     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3577     clean_addr = clean_data_tbi(s, dirty_addr);
3578 
3579     if (is_store) {
3580         /* Store-Release semantics */
3581         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3582         do_gpr_st(s, cpu_reg(s, rt), clean_addr, mop, true, rt, iss_sf, true);
3583     } else {
3584         /*
3585          * Load-AcquirePC semantics; we implement as the slightly more
3586          * restrictive Load-Acquire.
3587          */
3588         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop,
3589                   extend, true, rt, iss_sf, true);
3590         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3591     }
3592 }
3593 
3594 /* Load/store register (all forms) */
3595 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
3596 {
3597     int rt = extract32(insn, 0, 5);
3598     int opc = extract32(insn, 22, 2);
3599     bool is_vector = extract32(insn, 26, 1);
3600     int size = extract32(insn, 30, 2);
3601 
3602     switch (extract32(insn, 24, 2)) {
3603     case 0:
3604         if (extract32(insn, 21, 1) == 0) {
3605             /* Load/store register (unscaled immediate)
3606              * Load/store immediate pre/post-indexed
3607              * Load/store register unprivileged
3608              */
3609             disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
3610             return;
3611         }
3612         switch (extract32(insn, 10, 2)) {
3613         case 0:
3614             disas_ldst_atomic(s, insn, size, rt, is_vector);
3615             return;
3616         case 2:
3617             disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
3618             return;
3619         default:
3620             disas_ldst_pac(s, insn, size, rt, is_vector);
3621             return;
3622         }
3623         break;
3624     case 1:
3625         disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
3626         return;
3627     }
3628     unallocated_encoding(s);
3629 }
3630 
3631 /* AdvSIMD load/store multiple structures
3632  *
3633  *  31  30  29           23 22  21         16 15    12 11  10 9    5 4    0
3634  * +---+---+---------------+---+-------------+--------+------+------+------+
3635  * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size |  Rn  |  Rt  |
3636  * +---+---+---------------+---+-------------+--------+------+------+------+
3637  *
3638  * AdvSIMD load/store multiple structures (post-indexed)
3639  *
3640  *  31  30  29           23 22  21  20     16 15    12 11  10 9    5 4    0
3641  * +---+---+---------------+---+---+---------+--------+------+------+------+
3642  * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 |   Rm    | opcode | size |  Rn  |  Rt  |
3643  * +---+---+---------------+---+---+---------+--------+------+------+------+
3644  *
3645  * Rt: first (or only) SIMD&FP register to be transferred
3646  * Rn: base address or SP
3647  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3648  */
3649 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
3650 {
3651     int rt = extract32(insn, 0, 5);
3652     int rn = extract32(insn, 5, 5);
3653     int rm = extract32(insn, 16, 5);
3654     int size = extract32(insn, 10, 2);
3655     int opcode = extract32(insn, 12, 4);
3656     bool is_store = !extract32(insn, 22, 1);
3657     bool is_postidx = extract32(insn, 23, 1);
3658     bool is_q = extract32(insn, 30, 1);
3659     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3660     MemOp endian, align, mop;
3661 
3662     int total;    /* total bytes */
3663     int elements; /* elements per vector */
3664     int rpt;    /* num iterations */
3665     int selem;  /* structure elements */
3666     int r;
3667 
3668     if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
3669         unallocated_encoding(s);
3670         return;
3671     }
3672 
3673     if (!is_postidx && rm != 0) {
3674         unallocated_encoding(s);
3675         return;
3676     }
3677 
3678     /* From the shared decode logic */
3679     switch (opcode) {
3680     case 0x0:
3681         rpt = 1;
3682         selem = 4;
3683         break;
3684     case 0x2:
3685         rpt = 4;
3686         selem = 1;
3687         break;
3688     case 0x4:
3689         rpt = 1;
3690         selem = 3;
3691         break;
3692     case 0x6:
3693         rpt = 3;
3694         selem = 1;
3695         break;
3696     case 0x7:
3697         rpt = 1;
3698         selem = 1;
3699         break;
3700     case 0x8:
3701         rpt = 1;
3702         selem = 2;
3703         break;
3704     case 0xa:
3705         rpt = 2;
3706         selem = 1;
3707         break;
3708     default:
3709         unallocated_encoding(s);
3710         return;
3711     }
3712 
3713     if (size == 3 && !is_q && selem != 1) {
3714         /* reserved */
3715         unallocated_encoding(s);
3716         return;
3717     }
3718 
3719     if (!fp_access_check(s)) {
3720         return;
3721     }
3722 
3723     if (rn == 31) {
3724         gen_check_sp_alignment(s);
3725     }
3726 
3727     /* For our purposes, bytes are always little-endian.  */
3728     endian = s->be_data;
3729     if (size == 0) {
3730         endian = MO_LE;
3731     }
3732 
3733     total = rpt * selem * (is_q ? 16 : 8);
3734     tcg_rn = cpu_reg_sp(s, rn);
3735 
3736     /*
3737      * Issue the MTE check vs the logical repeat count, before we
3738      * promote consecutive little-endian elements below.
3739      */
3740     clean_addr = gen_mte_checkN(s, tcg_rn, is_store, is_postidx || rn != 31,
3741                                 total);
3742 
3743     /*
3744      * Consecutive little-endian elements from a single register
3745      * can be promoted to a larger little-endian operation.
3746      */
3747     align = MO_ALIGN;
3748     if (selem == 1 && endian == MO_LE) {
3749         align = pow2_align(size);
3750         size = 3;
3751     }
3752     if (!s->align_mem) {
3753         align = 0;
3754     }
3755     mop = endian | size | align;
3756 
3757     elements = (is_q ? 16 : 8) >> size;
3758     tcg_ebytes = tcg_constant_i64(1 << size);
3759     for (r = 0; r < rpt; r++) {
3760         int e;
3761         for (e = 0; e < elements; e++) {
3762             int xs;
3763             for (xs = 0; xs < selem; xs++) {
3764                 int tt = (rt + r + xs) % 32;
3765                 if (is_store) {
3766                     do_vec_st(s, tt, e, clean_addr, mop);
3767                 } else {
3768                     do_vec_ld(s, tt, e, clean_addr, mop);
3769                 }
3770                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3771             }
3772         }
3773     }
3774 
3775     if (!is_store) {
3776         /* For non-quad operations, setting a slice of the low
3777          * 64 bits of the register clears the high 64 bits (in
3778          * the ARM ARM pseudocode this is implicit in the fact
3779          * that 'rval' is a 64 bit wide variable).
3780          * For quad operations, we might still need to zero the
3781          * high bits of SVE.
3782          */
3783         for (r = 0; r < rpt * selem; r++) {
3784             int tt = (rt + r) % 32;
3785             clear_vec_high(s, is_q, tt);
3786         }
3787     }
3788 
3789     if (is_postidx) {
3790         if (rm == 31) {
3791             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3792         } else {
3793             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3794         }
3795     }
3796 }
3797 
3798 /* AdvSIMD load/store single structure
3799  *
3800  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3801  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3802  * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size |  Rn  |  Rt  |
3803  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3804  *
3805  * AdvSIMD load/store single structure (post-indexed)
3806  *
3807  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3808  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3809  * | 0 | Q | 0 0 1 1 0 1 1 | L R |     Rm    | opc | S | size |  Rn  |  Rt  |
3810  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3811  *
3812  * Rt: first (or only) SIMD&FP register to be transferred
3813  * Rn: base address or SP
3814  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3815  * index = encoded in Q:S:size dependent on size
3816  *
3817  * lane_size = encoded in R, opc
3818  * transfer width = encoded in opc, S, size
3819  */
3820 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
3821 {
3822     int rt = extract32(insn, 0, 5);
3823     int rn = extract32(insn, 5, 5);
3824     int rm = extract32(insn, 16, 5);
3825     int size = extract32(insn, 10, 2);
3826     int S = extract32(insn, 12, 1);
3827     int opc = extract32(insn, 13, 3);
3828     int R = extract32(insn, 21, 1);
3829     int is_load = extract32(insn, 22, 1);
3830     int is_postidx = extract32(insn, 23, 1);
3831     int is_q = extract32(insn, 30, 1);
3832 
3833     int scale = extract32(opc, 1, 2);
3834     int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
3835     bool replicate = false;
3836     int index = is_q << 3 | S << 2 | size;
3837     int xs, total;
3838     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3839     MemOp mop;
3840 
3841     if (extract32(insn, 31, 1)) {
3842         unallocated_encoding(s);
3843         return;
3844     }
3845     if (!is_postidx && rm != 0) {
3846         unallocated_encoding(s);
3847         return;
3848     }
3849 
3850     switch (scale) {
3851     case 3:
3852         if (!is_load || S) {
3853             unallocated_encoding(s);
3854             return;
3855         }
3856         scale = size;
3857         replicate = true;
3858         break;
3859     case 0:
3860         break;
3861     case 1:
3862         if (extract32(size, 0, 1)) {
3863             unallocated_encoding(s);
3864             return;
3865         }
3866         index >>= 1;
3867         break;
3868     case 2:
3869         if (extract32(size, 1, 1)) {
3870             unallocated_encoding(s);
3871             return;
3872         }
3873         if (!extract32(size, 0, 1)) {
3874             index >>= 2;
3875         } else {
3876             if (S) {
3877                 unallocated_encoding(s);
3878                 return;
3879             }
3880             index >>= 3;
3881             scale = 3;
3882         }
3883         break;
3884     default:
3885         g_assert_not_reached();
3886     }
3887 
3888     if (!fp_access_check(s)) {
3889         return;
3890     }
3891 
3892     if (rn == 31) {
3893         gen_check_sp_alignment(s);
3894     }
3895 
3896     total = selem << scale;
3897     tcg_rn = cpu_reg_sp(s, rn);
3898 
3899     clean_addr = gen_mte_checkN(s, tcg_rn, !is_load, is_postidx || rn != 31,
3900                                 total);
3901     mop = finalize_memop(s, scale);
3902 
3903     tcg_ebytes = tcg_constant_i64(1 << scale);
3904     for (xs = 0; xs < selem; xs++) {
3905         if (replicate) {
3906             /* Load and replicate to all elements */
3907             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3908 
3909             tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
3910             tcg_gen_gvec_dup_i64(scale, vec_full_reg_offset(s, rt),
3911                                  (is_q + 1) * 8, vec_full_reg_size(s),
3912                                  tcg_tmp);
3913         } else {
3914             /* Load/store one element per register */
3915             if (is_load) {
3916                 do_vec_ld(s, rt, index, clean_addr, mop);
3917             } else {
3918                 do_vec_st(s, rt, index, clean_addr, mop);
3919             }
3920         }
3921         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3922         rt = (rt + 1) % 32;
3923     }
3924 
3925     if (is_postidx) {
3926         if (rm == 31) {
3927             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3928         } else {
3929             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3930         }
3931     }
3932 }
3933 
3934 /*
3935  * Load/Store memory tags
3936  *
3937  *  31 30 29         24     22  21     12    10      5      0
3938  * +-----+-------------+-----+---+------+-----+------+------+
3939  * | 1 1 | 0 1 1 0 0 1 | op1 | 1 | imm9 | op2 |  Rn  |  Rt  |
3940  * +-----+-------------+-----+---+------+-----+------+------+
3941  */
3942 static void disas_ldst_tag(DisasContext *s, uint32_t insn)
3943 {
3944     int rt = extract32(insn, 0, 5);
3945     int rn = extract32(insn, 5, 5);
3946     uint64_t offset = sextract64(insn, 12, 9) << LOG2_TAG_GRANULE;
3947     int op2 = extract32(insn, 10, 2);
3948     int op1 = extract32(insn, 22, 2);
3949     bool is_load = false, is_pair = false, is_zero = false, is_mult = false;
3950     int index = 0;
3951     TCGv_i64 addr, clean_addr, tcg_rt;
3952 
3953     /* We checked insn bits [29:24,21] in the caller.  */
3954     if (extract32(insn, 30, 2) != 3) {
3955         goto do_unallocated;
3956     }
3957 
3958     /*
3959      * @index is a tri-state variable which has 3 states:
3960      * < 0 : post-index, writeback
3961      * = 0 : signed offset
3962      * > 0 : pre-index, writeback
3963      */
3964     switch (op1) {
3965     case 0:
3966         if (op2 != 0) {
3967             /* STG */
3968             index = op2 - 2;
3969         } else {
3970             /* STZGM */
3971             if (s->current_el == 0 || offset != 0) {
3972                 goto do_unallocated;
3973             }
3974             is_mult = is_zero = true;
3975         }
3976         break;
3977     case 1:
3978         if (op2 != 0) {
3979             /* STZG */
3980             is_zero = true;
3981             index = op2 - 2;
3982         } else {
3983             /* LDG */
3984             is_load = true;
3985         }
3986         break;
3987     case 2:
3988         if (op2 != 0) {
3989             /* ST2G */
3990             is_pair = true;
3991             index = op2 - 2;
3992         } else {
3993             /* STGM */
3994             if (s->current_el == 0 || offset != 0) {
3995                 goto do_unallocated;
3996             }
3997             is_mult = true;
3998         }
3999         break;
4000     case 3:
4001         if (op2 != 0) {
4002             /* STZ2G */
4003             is_pair = is_zero = true;
4004             index = op2 - 2;
4005         } else {
4006             /* LDGM */
4007             if (s->current_el == 0 || offset != 0) {
4008                 goto do_unallocated;
4009             }
4010             is_mult = is_load = true;
4011         }
4012         break;
4013 
4014     default:
4015     do_unallocated:
4016         unallocated_encoding(s);
4017         return;
4018     }
4019 
4020     if (is_mult
4021         ? !dc_isar_feature(aa64_mte, s)
4022         : !dc_isar_feature(aa64_mte_insn_reg, s)) {
4023         goto do_unallocated;
4024     }
4025 
4026     if (rn == 31) {
4027         gen_check_sp_alignment(s);
4028     }
4029 
4030     addr = read_cpu_reg_sp(s, rn, true);
4031     if (index >= 0) {
4032         /* pre-index or signed offset */
4033         tcg_gen_addi_i64(addr, addr, offset);
4034     }
4035 
4036     if (is_mult) {
4037         tcg_rt = cpu_reg(s, rt);
4038 
4039         if (is_zero) {
4040             int size = 4 << s->dcz_blocksize;
4041 
4042             if (s->ata) {
4043                 gen_helper_stzgm_tags(cpu_env, addr, tcg_rt);
4044             }
4045             /*
4046              * The non-tags portion of STZGM is mostly like DC_ZVA,
4047              * except the alignment happens before the access.
4048              */
4049             clean_addr = clean_data_tbi(s, addr);
4050             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4051             gen_helper_dc_zva(cpu_env, clean_addr);
4052         } else if (s->ata) {
4053             if (is_load) {
4054                 gen_helper_ldgm(tcg_rt, cpu_env, addr);
4055             } else {
4056                 gen_helper_stgm(cpu_env, addr, tcg_rt);
4057             }
4058         } else {
4059             MMUAccessType acc = is_load ? MMU_DATA_LOAD : MMU_DATA_STORE;
4060             int size = 4 << GMID_EL1_BS;
4061 
4062             clean_addr = clean_data_tbi(s, addr);
4063             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4064             gen_probe_access(s, clean_addr, acc, size);
4065 
4066             if (is_load) {
4067                 /* The result tags are zeros.  */
4068                 tcg_gen_movi_i64(tcg_rt, 0);
4069             }
4070         }
4071         return;
4072     }
4073 
4074     if (is_load) {
4075         tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
4076         tcg_rt = cpu_reg(s, rt);
4077         if (s->ata) {
4078             gen_helper_ldg(tcg_rt, cpu_env, addr, tcg_rt);
4079         } else {
4080             clean_addr = clean_data_tbi(s, addr);
4081             gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
4082             gen_address_with_allocation_tag0(tcg_rt, addr);
4083         }
4084     } else {
4085         tcg_rt = cpu_reg_sp(s, rt);
4086         if (!s->ata) {
4087             /*
4088              * For STG and ST2G, we need to check alignment and probe memory.
4089              * TODO: For STZG and STZ2G, we could rely on the stores below,
4090              * at least for system mode; user-only won't enforce alignment.
4091              */
4092             if (is_pair) {
4093                 gen_helper_st2g_stub(cpu_env, addr);
4094             } else {
4095                 gen_helper_stg_stub(cpu_env, addr);
4096             }
4097         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
4098             if (is_pair) {
4099                 gen_helper_st2g_parallel(cpu_env, addr, tcg_rt);
4100             } else {
4101                 gen_helper_stg_parallel(cpu_env, addr, tcg_rt);
4102             }
4103         } else {
4104             if (is_pair) {
4105                 gen_helper_st2g(cpu_env, addr, tcg_rt);
4106             } else {
4107                 gen_helper_stg(cpu_env, addr, tcg_rt);
4108             }
4109         }
4110     }
4111 
4112     if (is_zero) {
4113         TCGv_i64 clean_addr = clean_data_tbi(s, addr);
4114         TCGv_i64 tcg_zero = tcg_constant_i64(0);
4115         int mem_index = get_mem_index(s);
4116         int i, n = (1 + is_pair) << LOG2_TAG_GRANULE;
4117 
4118         tcg_gen_qemu_st_i64(tcg_zero, clean_addr, mem_index,
4119                             MO_UQ | MO_ALIGN_16);
4120         for (i = 8; i < n; i += 8) {
4121             tcg_gen_addi_i64(clean_addr, clean_addr, 8);
4122             tcg_gen_qemu_st_i64(tcg_zero, clean_addr, mem_index, MO_UQ);
4123         }
4124     }
4125 
4126     if (index != 0) {
4127         /* pre-index or post-index */
4128         if (index < 0) {
4129             /* post-index */
4130             tcg_gen_addi_i64(addr, addr, offset);
4131         }
4132         tcg_gen_mov_i64(cpu_reg_sp(s, rn), addr);
4133     }
4134 }
4135 
4136 /* Loads and stores */
4137 static void disas_ldst(DisasContext *s, uint32_t insn)
4138 {
4139     switch (extract32(insn, 24, 6)) {
4140     case 0x08: /* Load/store exclusive */
4141         disas_ldst_excl(s, insn);
4142         break;
4143     case 0x18: case 0x1c: /* Load register (literal) */
4144         disas_ld_lit(s, insn);
4145         break;
4146     case 0x28: case 0x29:
4147     case 0x2c: case 0x2d: /* Load/store pair (all forms) */
4148         disas_ldst_pair(s, insn);
4149         break;
4150     case 0x38: case 0x39:
4151     case 0x3c: case 0x3d: /* Load/store register (all forms) */
4152         disas_ldst_reg(s, insn);
4153         break;
4154     case 0x0c: /* AdvSIMD load/store multiple structures */
4155         disas_ldst_multiple_struct(s, insn);
4156         break;
4157     case 0x0d: /* AdvSIMD load/store single structure */
4158         disas_ldst_single_struct(s, insn);
4159         break;
4160     case 0x19:
4161         if (extract32(insn, 21, 1) != 0) {
4162             disas_ldst_tag(s, insn);
4163         } else if (extract32(insn, 10, 2) == 0) {
4164             disas_ldst_ldapr_stlr(s, insn);
4165         } else {
4166             unallocated_encoding(s);
4167         }
4168         break;
4169     default:
4170         unallocated_encoding(s);
4171         break;
4172     }
4173 }
4174 
4175 /* PC-rel. addressing
4176  *   31  30   29 28       24 23                5 4    0
4177  * +----+-------+-----------+-------------------+------+
4178  * | op | immlo | 1 0 0 0 0 |       immhi       |  Rd  |
4179  * +----+-------+-----------+-------------------+------+
4180  */
4181 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
4182 {
4183     unsigned int page, rd;
4184     int64_t offset;
4185 
4186     page = extract32(insn, 31, 1);
4187     /* SignExtend(immhi:immlo) -> offset */
4188     offset = sextract64(insn, 5, 19);
4189     offset = offset << 2 | extract32(insn, 29, 2);
4190     rd = extract32(insn, 0, 5);
4191 
4192     if (page) {
4193         /* ADRP (page based) */
4194         offset <<= 12;
4195         /* The page offset is ok for CF_PCREL. */
4196         offset -= s->pc_curr & 0xfff;
4197     }
4198 
4199     gen_pc_plus_diff(s, cpu_reg(s, rd), offset);
4200 }
4201 
4202 /*
4203  * Add/subtract (immediate)
4204  *
4205  *  31 30 29 28         23 22 21         10 9   5 4   0
4206  * +--+--+--+-------------+--+-------------+-----+-----+
4207  * |sf|op| S| 1 0 0 0 1 0 |sh|    imm12    |  Rn | Rd  |
4208  * +--+--+--+-------------+--+-------------+-----+-----+
4209  *
4210  *    sf: 0 -> 32bit, 1 -> 64bit
4211  *    op: 0 -> add  , 1 -> sub
4212  *     S: 1 -> set flags
4213  *    sh: 1 -> LSL imm by 12
4214  */
4215 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
4216 {
4217     int rd = extract32(insn, 0, 5);
4218     int rn = extract32(insn, 5, 5);
4219     uint64_t imm = extract32(insn, 10, 12);
4220     bool shift = extract32(insn, 22, 1);
4221     bool setflags = extract32(insn, 29, 1);
4222     bool sub_op = extract32(insn, 30, 1);
4223     bool is_64bit = extract32(insn, 31, 1);
4224 
4225     TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
4226     TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
4227     TCGv_i64 tcg_result;
4228 
4229     if (shift) {
4230         imm <<= 12;
4231     }
4232 
4233     tcg_result = tcg_temp_new_i64();
4234     if (!setflags) {
4235         if (sub_op) {
4236             tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
4237         } else {
4238             tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
4239         }
4240     } else {
4241         TCGv_i64 tcg_imm = tcg_constant_i64(imm);
4242         if (sub_op) {
4243             gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
4244         } else {
4245             gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
4246         }
4247     }
4248 
4249     if (is_64bit) {
4250         tcg_gen_mov_i64(tcg_rd, tcg_result);
4251     } else {
4252         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4253     }
4254 }
4255 
4256 /*
4257  * Add/subtract (immediate, with tags)
4258  *
4259  *  31 30 29 28         23 22 21     16 14      10 9   5 4   0
4260  * +--+--+--+-------------+--+---------+--+-------+-----+-----+
4261  * |sf|op| S| 1 0 0 0 1 1 |o2|  uimm6  |o3| uimm4 |  Rn | Rd  |
4262  * +--+--+--+-------------+--+---------+--+-------+-----+-----+
4263  *
4264  *    op: 0 -> add, 1 -> sub
4265  */
4266 static void disas_add_sub_imm_with_tags(DisasContext *s, uint32_t insn)
4267 {
4268     int rd = extract32(insn, 0, 5);
4269     int rn = extract32(insn, 5, 5);
4270     int uimm4 = extract32(insn, 10, 4);
4271     int uimm6 = extract32(insn, 16, 6);
4272     bool sub_op = extract32(insn, 30, 1);
4273     TCGv_i64 tcg_rn, tcg_rd;
4274     int imm;
4275 
4276     /* Test all of sf=1, S=0, o2=0, o3=0.  */
4277     if ((insn & 0xa040c000u) != 0x80000000u ||
4278         !dc_isar_feature(aa64_mte_insn_reg, s)) {
4279         unallocated_encoding(s);
4280         return;
4281     }
4282 
4283     imm = uimm6 << LOG2_TAG_GRANULE;
4284     if (sub_op) {
4285         imm = -imm;
4286     }
4287 
4288     tcg_rn = cpu_reg_sp(s, rn);
4289     tcg_rd = cpu_reg_sp(s, rd);
4290 
4291     if (s->ata) {
4292         gen_helper_addsubg(tcg_rd, cpu_env, tcg_rn,
4293                            tcg_constant_i32(imm),
4294                            tcg_constant_i32(uimm4));
4295     } else {
4296         tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4297         gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4298     }
4299 }
4300 
4301 /* The input should be a value in the bottom e bits (with higher
4302  * bits zero); returns that value replicated into every element
4303  * of size e in a 64 bit integer.
4304  */
4305 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4306 {
4307     assert(e != 0);
4308     while (e < 64) {
4309         mask |= mask << e;
4310         e *= 2;
4311     }
4312     return mask;
4313 }
4314 
4315 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
4316 static inline uint64_t bitmask64(unsigned int length)
4317 {
4318     assert(length > 0 && length <= 64);
4319     return ~0ULL >> (64 - length);
4320 }
4321 
4322 /* Simplified variant of pseudocode DecodeBitMasks() for the case where we
4323  * only require the wmask. Returns false if the imms/immr/immn are a reserved
4324  * value (ie should cause a guest UNDEF exception), and true if they are
4325  * valid, in which case the decoded bit pattern is written to result.
4326  */
4327 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4328                             unsigned int imms, unsigned int immr)
4329 {
4330     uint64_t mask;
4331     unsigned e, levels, s, r;
4332     int len;
4333 
4334     assert(immn < 2 && imms < 64 && immr < 64);
4335 
4336     /* The bit patterns we create here are 64 bit patterns which
4337      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4338      * 64 bits each. Each element contains the same value: a run
4339      * of between 1 and e-1 non-zero bits, rotated within the
4340      * element by between 0 and e-1 bits.
4341      *
4342      * The element size and run length are encoded into immn (1 bit)
4343      * and imms (6 bits) as follows:
4344      * 64 bit elements: immn = 1, imms = <length of run - 1>
4345      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4346      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4347      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4348      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4349      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4350      * Notice that immn = 0, imms = 11111x is the only combination
4351      * not covered by one of the above options; this is reserved.
4352      * Further, <length of run - 1> all-ones is a reserved pattern.
4353      *
4354      * In all cases the rotation is by immr % e (and immr is 6 bits).
4355      */
4356 
4357     /* First determine the element size */
4358     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4359     if (len < 1) {
4360         /* This is the immn == 0, imms == 0x11111x case */
4361         return false;
4362     }
4363     e = 1 << len;
4364 
4365     levels = e - 1;
4366     s = imms & levels;
4367     r = immr & levels;
4368 
4369     if (s == levels) {
4370         /* <length of run - 1> mustn't be all-ones. */
4371         return false;
4372     }
4373 
4374     /* Create the value of one element: s+1 set bits rotated
4375      * by r within the element (which is e bits wide)...
4376      */
4377     mask = bitmask64(s + 1);
4378     if (r) {
4379         mask = (mask >> r) | (mask << (e - r));
4380         mask &= bitmask64(e);
4381     }
4382     /* ...then replicate the element over the whole 64 bit value */
4383     mask = bitfield_replicate(mask, e);
4384     *result = mask;
4385     return true;
4386 }
4387 
4388 /* Logical (immediate)
4389  *   31  30 29 28         23 22  21  16 15  10 9    5 4    0
4390  * +----+-----+-------------+---+------+------+------+------+
4391  * | sf | opc | 1 0 0 1 0 0 | N | immr | imms |  Rn  |  Rd  |
4392  * +----+-----+-------------+---+------+------+------+------+
4393  */
4394 static void disas_logic_imm(DisasContext *s, uint32_t insn)
4395 {
4396     unsigned int sf, opc, is_n, immr, imms, rn, rd;
4397     TCGv_i64 tcg_rd, tcg_rn;
4398     uint64_t wmask;
4399     bool is_and = false;
4400 
4401     sf = extract32(insn, 31, 1);
4402     opc = extract32(insn, 29, 2);
4403     is_n = extract32(insn, 22, 1);
4404     immr = extract32(insn, 16, 6);
4405     imms = extract32(insn, 10, 6);
4406     rn = extract32(insn, 5, 5);
4407     rd = extract32(insn, 0, 5);
4408 
4409     if (!sf && is_n) {
4410         unallocated_encoding(s);
4411         return;
4412     }
4413 
4414     if (opc == 0x3) { /* ANDS */
4415         tcg_rd = cpu_reg(s, rd);
4416     } else {
4417         tcg_rd = cpu_reg_sp(s, rd);
4418     }
4419     tcg_rn = cpu_reg(s, rn);
4420 
4421     if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
4422         /* some immediate field values are reserved */
4423         unallocated_encoding(s);
4424         return;
4425     }
4426 
4427     if (!sf) {
4428         wmask &= 0xffffffff;
4429     }
4430 
4431     switch (opc) {
4432     case 0x3: /* ANDS */
4433     case 0x0: /* AND */
4434         tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
4435         is_and = true;
4436         break;
4437     case 0x1: /* ORR */
4438         tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
4439         break;
4440     case 0x2: /* EOR */
4441         tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
4442         break;
4443     default:
4444         assert(FALSE); /* must handle all above */
4445         break;
4446     }
4447 
4448     if (!sf && !is_and) {
4449         /* zero extend final result; we know we can skip this for AND
4450          * since the immediate had the high 32 bits clear.
4451          */
4452         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4453     }
4454 
4455     if (opc == 3) { /* ANDS */
4456         gen_logic_CC(sf, tcg_rd);
4457     }
4458 }
4459 
4460 /*
4461  * Move wide (immediate)
4462  *
4463  *  31 30 29 28         23 22 21 20             5 4    0
4464  * +--+-----+-------------+-----+----------------+------+
4465  * |sf| opc | 1 0 0 1 0 1 |  hw |  imm16         |  Rd  |
4466  * +--+-----+-------------+-----+----------------+------+
4467  *
4468  * sf: 0 -> 32 bit, 1 -> 64 bit
4469  * opc: 00 -> N, 10 -> Z, 11 -> K
4470  * hw: shift/16 (0,16, and sf only 32, 48)
4471  */
4472 static void disas_movw_imm(DisasContext *s, uint32_t insn)
4473 {
4474     int rd = extract32(insn, 0, 5);
4475     uint64_t imm = extract32(insn, 5, 16);
4476     int sf = extract32(insn, 31, 1);
4477     int opc = extract32(insn, 29, 2);
4478     int pos = extract32(insn, 21, 2) << 4;
4479     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4480 
4481     if (!sf && (pos >= 32)) {
4482         unallocated_encoding(s);
4483         return;
4484     }
4485 
4486     switch (opc) {
4487     case 0: /* MOVN */
4488     case 2: /* MOVZ */
4489         imm <<= pos;
4490         if (opc == 0) {
4491             imm = ~imm;
4492         }
4493         if (!sf) {
4494             imm &= 0xffffffffu;
4495         }
4496         tcg_gen_movi_i64(tcg_rd, imm);
4497         break;
4498     case 3: /* MOVK */
4499         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_constant_i64(imm), pos, 16);
4500         if (!sf) {
4501             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4502         }
4503         break;
4504     default:
4505         unallocated_encoding(s);
4506         break;
4507     }
4508 }
4509 
4510 /* Bitfield
4511  *   31  30 29 28         23 22  21  16 15  10 9    5 4    0
4512  * +----+-----+-------------+---+------+------+------+------+
4513  * | sf | opc | 1 0 0 1 1 0 | N | immr | imms |  Rn  |  Rd  |
4514  * +----+-----+-------------+---+------+------+------+------+
4515  */
4516 static void disas_bitfield(DisasContext *s, uint32_t insn)
4517 {
4518     unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
4519     TCGv_i64 tcg_rd, tcg_tmp;
4520 
4521     sf = extract32(insn, 31, 1);
4522     opc = extract32(insn, 29, 2);
4523     n = extract32(insn, 22, 1);
4524     ri = extract32(insn, 16, 6);
4525     si = extract32(insn, 10, 6);
4526     rn = extract32(insn, 5, 5);
4527     rd = extract32(insn, 0, 5);
4528     bitsize = sf ? 64 : 32;
4529 
4530     if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
4531         unallocated_encoding(s);
4532         return;
4533     }
4534 
4535     tcg_rd = cpu_reg(s, rd);
4536 
4537     /* Suppress the zero-extend for !sf.  Since RI and SI are constrained
4538        to be smaller than bitsize, we'll never reference data outside the
4539        low 32-bits anyway.  */
4540     tcg_tmp = read_cpu_reg(s, rn, 1);
4541 
4542     /* Recognize simple(r) extractions.  */
4543     if (si >= ri) {
4544         /* Wd<s-r:0> = Wn<s:r> */
4545         len = (si - ri) + 1;
4546         if (opc == 0) { /* SBFM: ASR, SBFX, SXTB, SXTH, SXTW */
4547             tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4548             goto done;
4549         } else if (opc == 2) { /* UBFM: UBFX, LSR, UXTB, UXTH */
4550             tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4551             return;
4552         }
4553         /* opc == 1, BFXIL fall through to deposit */
4554         tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4555         pos = 0;
4556     } else {
4557         /* Handle the ri > si case with a deposit
4558          * Wd<32+s-r,32-r> = Wn<s:0>
4559          */
4560         len = si + 1;
4561         pos = (bitsize - ri) & (bitsize - 1);
4562     }
4563 
4564     if (opc == 0 && len < ri) {
4565         /* SBFM: sign extend the destination field from len to fill
4566            the balance of the word.  Let the deposit below insert all
4567            of those sign bits.  */
4568         tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4569         len = ri;
4570     }
4571 
4572     if (opc == 1) { /* BFM, BFXIL */
4573         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4574     } else {
4575         /* SBFM or UBFM: We start with zero, and we haven't modified
4576            any bits outside bitsize, therefore the zero-extension
4577            below is unneeded.  */
4578         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4579         return;
4580     }
4581 
4582  done:
4583     if (!sf) { /* zero extend final result */
4584         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4585     }
4586 }
4587 
4588 /* Extract
4589  *   31  30  29 28         23 22   21  20  16 15    10 9    5 4    0
4590  * +----+------+-------------+---+----+------+--------+------+------+
4591  * | sf | op21 | 1 0 0 1 1 1 | N | o0 |  Rm  |  imms  |  Rn  |  Rd  |
4592  * +----+------+-------------+---+----+------+--------+------+------+
4593  */
4594 static void disas_extract(DisasContext *s, uint32_t insn)
4595 {
4596     unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
4597 
4598     sf = extract32(insn, 31, 1);
4599     n = extract32(insn, 22, 1);
4600     rm = extract32(insn, 16, 5);
4601     imm = extract32(insn, 10, 6);
4602     rn = extract32(insn, 5, 5);
4603     rd = extract32(insn, 0, 5);
4604     op21 = extract32(insn, 29, 2);
4605     op0 = extract32(insn, 21, 1);
4606     bitsize = sf ? 64 : 32;
4607 
4608     if (sf != n || op21 || op0 || imm >= bitsize) {
4609         unallocated_encoding(s);
4610     } else {
4611         TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4612 
4613         tcg_rd = cpu_reg(s, rd);
4614 
4615         if (unlikely(imm == 0)) {
4616             /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4617              * so an extract from bit 0 is a special case.
4618              */
4619             if (sf) {
4620                 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
4621             } else {
4622                 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
4623             }
4624         } else {
4625             tcg_rm = cpu_reg(s, rm);
4626             tcg_rn = cpu_reg(s, rn);
4627 
4628             if (sf) {
4629                 /* Specialization to ROR happens in EXTRACT2.  */
4630                 tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, imm);
4631             } else {
4632                 TCGv_i32 t0 = tcg_temp_new_i32();
4633 
4634                 tcg_gen_extrl_i64_i32(t0, tcg_rm);
4635                 if (rm == rn) {
4636                     tcg_gen_rotri_i32(t0, t0, imm);
4637                 } else {
4638                     TCGv_i32 t1 = tcg_temp_new_i32();
4639                     tcg_gen_extrl_i64_i32(t1, tcg_rn);
4640                     tcg_gen_extract2_i32(t0, t0, t1, imm);
4641                 }
4642                 tcg_gen_extu_i32_i64(tcg_rd, t0);
4643             }
4644         }
4645     }
4646 }
4647 
4648 /* Data processing - immediate */
4649 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
4650 {
4651     switch (extract32(insn, 23, 6)) {
4652     case 0x20: case 0x21: /* PC-rel. addressing */
4653         disas_pc_rel_adr(s, insn);
4654         break;
4655     case 0x22: /* Add/subtract (immediate) */
4656         disas_add_sub_imm(s, insn);
4657         break;
4658     case 0x23: /* Add/subtract (immediate, with tags) */
4659         disas_add_sub_imm_with_tags(s, insn);
4660         break;
4661     case 0x24: /* Logical (immediate) */
4662         disas_logic_imm(s, insn);
4663         break;
4664     case 0x25: /* Move wide (immediate) */
4665         disas_movw_imm(s, insn);
4666         break;
4667     case 0x26: /* Bitfield */
4668         disas_bitfield(s, insn);
4669         break;
4670     case 0x27: /* Extract */
4671         disas_extract(s, insn);
4672         break;
4673     default:
4674         unallocated_encoding(s);
4675         break;
4676     }
4677 }
4678 
4679 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4680  * Note that it is the caller's responsibility to ensure that the
4681  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4682  * mandated semantics for out of range shifts.
4683  */
4684 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4685                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4686 {
4687     switch (shift_type) {
4688     case A64_SHIFT_TYPE_LSL:
4689         tcg_gen_shl_i64(dst, src, shift_amount);
4690         break;
4691     case A64_SHIFT_TYPE_LSR:
4692         tcg_gen_shr_i64(dst, src, shift_amount);
4693         break;
4694     case A64_SHIFT_TYPE_ASR:
4695         if (!sf) {
4696             tcg_gen_ext32s_i64(dst, src);
4697         }
4698         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4699         break;
4700     case A64_SHIFT_TYPE_ROR:
4701         if (sf) {
4702             tcg_gen_rotr_i64(dst, src, shift_amount);
4703         } else {
4704             TCGv_i32 t0, t1;
4705             t0 = tcg_temp_new_i32();
4706             t1 = tcg_temp_new_i32();
4707             tcg_gen_extrl_i64_i32(t0, src);
4708             tcg_gen_extrl_i64_i32(t1, shift_amount);
4709             tcg_gen_rotr_i32(t0, t0, t1);
4710             tcg_gen_extu_i32_i64(dst, t0);
4711         }
4712         break;
4713     default:
4714         assert(FALSE); /* all shift types should be handled */
4715         break;
4716     }
4717 
4718     if (!sf) { /* zero extend final result */
4719         tcg_gen_ext32u_i64(dst, dst);
4720     }
4721 }
4722 
4723 /* Shift a TCGv src by immediate, put result in dst.
4724  * The shift amount must be in range (this should always be true as the
4725  * relevant instructions will UNDEF on bad shift immediates).
4726  */
4727 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4728                           enum a64_shift_type shift_type, unsigned int shift_i)
4729 {
4730     assert(shift_i < (sf ? 64 : 32));
4731 
4732     if (shift_i == 0) {
4733         tcg_gen_mov_i64(dst, src);
4734     } else {
4735         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
4736     }
4737 }
4738 
4739 /* Logical (shifted register)
4740  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
4741  * +----+-----+-----------+-------+---+------+--------+------+------+
4742  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
4743  * +----+-----+-----------+-------+---+------+--------+------+------+
4744  */
4745 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4746 {
4747     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4748     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4749 
4750     sf = extract32(insn, 31, 1);
4751     opc = extract32(insn, 29, 2);
4752     shift_type = extract32(insn, 22, 2);
4753     invert = extract32(insn, 21, 1);
4754     rm = extract32(insn, 16, 5);
4755     shift_amount = extract32(insn, 10, 6);
4756     rn = extract32(insn, 5, 5);
4757     rd = extract32(insn, 0, 5);
4758 
4759     if (!sf && (shift_amount & (1 << 5))) {
4760         unallocated_encoding(s);
4761         return;
4762     }
4763 
4764     tcg_rd = cpu_reg(s, rd);
4765 
4766     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4767         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4768          * register-register MOV and MVN, so it is worth special casing.
4769          */
4770         tcg_rm = cpu_reg(s, rm);
4771         if (invert) {
4772             tcg_gen_not_i64(tcg_rd, tcg_rm);
4773             if (!sf) {
4774                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4775             }
4776         } else {
4777             if (sf) {
4778                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4779             } else {
4780                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4781             }
4782         }
4783         return;
4784     }
4785 
4786     tcg_rm = read_cpu_reg(s, rm, sf);
4787 
4788     if (shift_amount) {
4789         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4790     }
4791 
4792     tcg_rn = cpu_reg(s, rn);
4793 
4794     switch (opc | (invert << 2)) {
4795     case 0: /* AND */
4796     case 3: /* ANDS */
4797         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4798         break;
4799     case 1: /* ORR */
4800         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4801         break;
4802     case 2: /* EOR */
4803         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4804         break;
4805     case 4: /* BIC */
4806     case 7: /* BICS */
4807         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4808         break;
4809     case 5: /* ORN */
4810         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4811         break;
4812     case 6: /* EON */
4813         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4814         break;
4815     default:
4816         assert(FALSE);
4817         break;
4818     }
4819 
4820     if (!sf) {
4821         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4822     }
4823 
4824     if (opc == 3) {
4825         gen_logic_CC(sf, tcg_rd);
4826     }
4827 }
4828 
4829 /*
4830  * Add/subtract (extended register)
4831  *
4832  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
4833  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4834  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
4835  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4836  *
4837  *  sf: 0 -> 32bit, 1 -> 64bit
4838  *  op: 0 -> add  , 1 -> sub
4839  *   S: 1 -> set flags
4840  * opt: 00
4841  * option: extension type (see DecodeRegExtend)
4842  * imm3: optional shift to Rm
4843  *
4844  * Rd = Rn + LSL(extend(Rm), amount)
4845  */
4846 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4847 {
4848     int rd = extract32(insn, 0, 5);
4849     int rn = extract32(insn, 5, 5);
4850     int imm3 = extract32(insn, 10, 3);
4851     int option = extract32(insn, 13, 3);
4852     int rm = extract32(insn, 16, 5);
4853     int opt = extract32(insn, 22, 2);
4854     bool setflags = extract32(insn, 29, 1);
4855     bool sub_op = extract32(insn, 30, 1);
4856     bool sf = extract32(insn, 31, 1);
4857 
4858     TCGv_i64 tcg_rm, tcg_rn; /* temps */
4859     TCGv_i64 tcg_rd;
4860     TCGv_i64 tcg_result;
4861 
4862     if (imm3 > 4 || opt != 0) {
4863         unallocated_encoding(s);
4864         return;
4865     }
4866 
4867     /* non-flag setting ops may use SP */
4868     if (!setflags) {
4869         tcg_rd = cpu_reg_sp(s, rd);
4870     } else {
4871         tcg_rd = cpu_reg(s, rd);
4872     }
4873     tcg_rn = read_cpu_reg_sp(s, rn, sf);
4874 
4875     tcg_rm = read_cpu_reg(s, rm, sf);
4876     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4877 
4878     tcg_result = tcg_temp_new_i64();
4879 
4880     if (!setflags) {
4881         if (sub_op) {
4882             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4883         } else {
4884             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4885         }
4886     } else {
4887         if (sub_op) {
4888             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4889         } else {
4890             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4891         }
4892     }
4893 
4894     if (sf) {
4895         tcg_gen_mov_i64(tcg_rd, tcg_result);
4896     } else {
4897         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4898     }
4899 }
4900 
4901 /*
4902  * Add/subtract (shifted register)
4903  *
4904  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
4905  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4906  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
4907  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4908  *
4909  *    sf: 0 -> 32bit, 1 -> 64bit
4910  *    op: 0 -> add  , 1 -> sub
4911  *     S: 1 -> set flags
4912  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4913  *  imm6: Shift amount to apply to Rm before the add/sub
4914  */
4915 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4916 {
4917     int rd = extract32(insn, 0, 5);
4918     int rn = extract32(insn, 5, 5);
4919     int imm6 = extract32(insn, 10, 6);
4920     int rm = extract32(insn, 16, 5);
4921     int shift_type = extract32(insn, 22, 2);
4922     bool setflags = extract32(insn, 29, 1);
4923     bool sub_op = extract32(insn, 30, 1);
4924     bool sf = extract32(insn, 31, 1);
4925 
4926     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4927     TCGv_i64 tcg_rn, tcg_rm;
4928     TCGv_i64 tcg_result;
4929 
4930     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4931         unallocated_encoding(s);
4932         return;
4933     }
4934 
4935     tcg_rn = read_cpu_reg(s, rn, sf);
4936     tcg_rm = read_cpu_reg(s, rm, sf);
4937 
4938     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4939 
4940     tcg_result = tcg_temp_new_i64();
4941 
4942     if (!setflags) {
4943         if (sub_op) {
4944             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4945         } else {
4946             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4947         }
4948     } else {
4949         if (sub_op) {
4950             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4951         } else {
4952             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4953         }
4954     }
4955 
4956     if (sf) {
4957         tcg_gen_mov_i64(tcg_rd, tcg_result);
4958     } else {
4959         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4960     }
4961 }
4962 
4963 /* Data-processing (3 source)
4964  *
4965  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
4966  *  +--+------+-----------+------+------+----+------+------+------+
4967  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
4968  *  +--+------+-----------+------+------+----+------+------+------+
4969  */
4970 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4971 {
4972     int rd = extract32(insn, 0, 5);
4973     int rn = extract32(insn, 5, 5);
4974     int ra = extract32(insn, 10, 5);
4975     int rm = extract32(insn, 16, 5);
4976     int op_id = (extract32(insn, 29, 3) << 4) |
4977         (extract32(insn, 21, 3) << 1) |
4978         extract32(insn, 15, 1);
4979     bool sf = extract32(insn, 31, 1);
4980     bool is_sub = extract32(op_id, 0, 1);
4981     bool is_high = extract32(op_id, 2, 1);
4982     bool is_signed = false;
4983     TCGv_i64 tcg_op1;
4984     TCGv_i64 tcg_op2;
4985     TCGv_i64 tcg_tmp;
4986 
4987     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
4988     switch (op_id) {
4989     case 0x42: /* SMADDL */
4990     case 0x43: /* SMSUBL */
4991     case 0x44: /* SMULH */
4992         is_signed = true;
4993         break;
4994     case 0x0: /* MADD (32bit) */
4995     case 0x1: /* MSUB (32bit) */
4996     case 0x40: /* MADD (64bit) */
4997     case 0x41: /* MSUB (64bit) */
4998     case 0x4a: /* UMADDL */
4999     case 0x4b: /* UMSUBL */
5000     case 0x4c: /* UMULH */
5001         break;
5002     default:
5003         unallocated_encoding(s);
5004         return;
5005     }
5006 
5007     if (is_high) {
5008         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
5009         TCGv_i64 tcg_rd = cpu_reg(s, rd);
5010         TCGv_i64 tcg_rn = cpu_reg(s, rn);
5011         TCGv_i64 tcg_rm = cpu_reg(s, rm);
5012 
5013         if (is_signed) {
5014             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5015         } else {
5016             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5017         }
5018         return;
5019     }
5020 
5021     tcg_op1 = tcg_temp_new_i64();
5022     tcg_op2 = tcg_temp_new_i64();
5023     tcg_tmp = tcg_temp_new_i64();
5024 
5025     if (op_id < 0x42) {
5026         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
5027         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
5028     } else {
5029         if (is_signed) {
5030             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
5031             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
5032         } else {
5033             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
5034             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
5035         }
5036     }
5037 
5038     if (ra == 31 && !is_sub) {
5039         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
5040         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
5041     } else {
5042         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
5043         if (is_sub) {
5044             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5045         } else {
5046             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5047         }
5048     }
5049 
5050     if (!sf) {
5051         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
5052     }
5053 }
5054 
5055 /* Add/subtract (with carry)
5056  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
5057  * +--+--+--+------------------------+------+-------------+------+-----+
5058  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
5059  * +--+--+--+------------------------+------+-------------+------+-----+
5060  */
5061 
5062 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
5063 {
5064     unsigned int sf, op, setflags, rm, rn, rd;
5065     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
5066 
5067     sf = extract32(insn, 31, 1);
5068     op = extract32(insn, 30, 1);
5069     setflags = extract32(insn, 29, 1);
5070     rm = extract32(insn, 16, 5);
5071     rn = extract32(insn, 5, 5);
5072     rd = extract32(insn, 0, 5);
5073 
5074     tcg_rd = cpu_reg(s, rd);
5075     tcg_rn = cpu_reg(s, rn);
5076 
5077     if (op) {
5078         tcg_y = tcg_temp_new_i64();
5079         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
5080     } else {
5081         tcg_y = cpu_reg(s, rm);
5082     }
5083 
5084     if (setflags) {
5085         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
5086     } else {
5087         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
5088     }
5089 }
5090 
5091 /*
5092  * Rotate right into flags
5093  *  31 30 29                21       15          10      5  4      0
5094  * +--+--+--+-----------------+--------+-----------+------+--+------+
5095  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
5096  * +--+--+--+-----------------+--------+-----------+------+--+------+
5097  */
5098 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
5099 {
5100     int mask = extract32(insn, 0, 4);
5101     int o2 = extract32(insn, 4, 1);
5102     int rn = extract32(insn, 5, 5);
5103     int imm6 = extract32(insn, 15, 6);
5104     int sf_op_s = extract32(insn, 29, 3);
5105     TCGv_i64 tcg_rn;
5106     TCGv_i32 nzcv;
5107 
5108     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
5109         unallocated_encoding(s);
5110         return;
5111     }
5112 
5113     tcg_rn = read_cpu_reg(s, rn, 1);
5114     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
5115 
5116     nzcv = tcg_temp_new_i32();
5117     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
5118 
5119     if (mask & 8) { /* N */
5120         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
5121     }
5122     if (mask & 4) { /* Z */
5123         tcg_gen_not_i32(cpu_ZF, nzcv);
5124         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
5125     }
5126     if (mask & 2) { /* C */
5127         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
5128     }
5129     if (mask & 1) { /* V */
5130         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
5131     }
5132 }
5133 
5134 /*
5135  * Evaluate into flags
5136  *  31 30 29                21        15   14        10      5  4      0
5137  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5138  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
5139  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5140  */
5141 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
5142 {
5143     int o3_mask = extract32(insn, 0, 5);
5144     int rn = extract32(insn, 5, 5);
5145     int o2 = extract32(insn, 15, 6);
5146     int sz = extract32(insn, 14, 1);
5147     int sf_op_s = extract32(insn, 29, 3);
5148     TCGv_i32 tmp;
5149     int shift;
5150 
5151     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
5152         !dc_isar_feature(aa64_condm_4, s)) {
5153         unallocated_encoding(s);
5154         return;
5155     }
5156     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
5157 
5158     tmp = tcg_temp_new_i32();
5159     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
5160     tcg_gen_shli_i32(cpu_NF, tmp, shift);
5161     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
5162     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
5163     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
5164 }
5165 
5166 /* Conditional compare (immediate / register)
5167  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
5168  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5169  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
5170  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5171  *        [1]                             y                [0]       [0]
5172  */
5173 static void disas_cc(DisasContext *s, uint32_t insn)
5174 {
5175     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
5176     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
5177     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
5178     DisasCompare c;
5179 
5180     if (!extract32(insn, 29, 1)) {
5181         unallocated_encoding(s);
5182         return;
5183     }
5184     if (insn & (1 << 10 | 1 << 4)) {
5185         unallocated_encoding(s);
5186         return;
5187     }
5188     sf = extract32(insn, 31, 1);
5189     op = extract32(insn, 30, 1);
5190     is_imm = extract32(insn, 11, 1);
5191     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
5192     cond = extract32(insn, 12, 4);
5193     rn = extract32(insn, 5, 5);
5194     nzcv = extract32(insn, 0, 4);
5195 
5196     /* Set T0 = !COND.  */
5197     tcg_t0 = tcg_temp_new_i32();
5198     arm_test_cc(&c, cond);
5199     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
5200 
5201     /* Load the arguments for the new comparison.  */
5202     if (is_imm) {
5203         tcg_y = tcg_temp_new_i64();
5204         tcg_gen_movi_i64(tcg_y, y);
5205     } else {
5206         tcg_y = cpu_reg(s, y);
5207     }
5208     tcg_rn = cpu_reg(s, rn);
5209 
5210     /* Set the flags for the new comparison.  */
5211     tcg_tmp = tcg_temp_new_i64();
5212     if (op) {
5213         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5214     } else {
5215         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5216     }
5217 
5218     /* If COND was false, force the flags to #nzcv.  Compute two masks
5219      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
5220      * For tcg hosts that support ANDC, we can make do with just T1.
5221      * In either case, allow the tcg optimizer to delete any unused mask.
5222      */
5223     tcg_t1 = tcg_temp_new_i32();
5224     tcg_t2 = tcg_temp_new_i32();
5225     tcg_gen_neg_i32(tcg_t1, tcg_t0);
5226     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
5227 
5228     if (nzcv & 8) { /* N */
5229         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
5230     } else {
5231         if (TCG_TARGET_HAS_andc_i32) {
5232             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
5233         } else {
5234             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
5235         }
5236     }
5237     if (nzcv & 4) { /* Z */
5238         if (TCG_TARGET_HAS_andc_i32) {
5239             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
5240         } else {
5241             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
5242         }
5243     } else {
5244         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
5245     }
5246     if (nzcv & 2) { /* C */
5247         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
5248     } else {
5249         if (TCG_TARGET_HAS_andc_i32) {
5250             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
5251         } else {
5252             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
5253         }
5254     }
5255     if (nzcv & 1) { /* V */
5256         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
5257     } else {
5258         if (TCG_TARGET_HAS_andc_i32) {
5259             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
5260         } else {
5261             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
5262         }
5263     }
5264 }
5265 
5266 /* Conditional select
5267  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
5268  * +----+----+---+-----------------+------+------+-----+------+------+
5269  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
5270  * +----+----+---+-----------------+------+------+-----+------+------+
5271  */
5272 static void disas_cond_select(DisasContext *s, uint32_t insn)
5273 {
5274     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
5275     TCGv_i64 tcg_rd, zero;
5276     DisasCompare64 c;
5277 
5278     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
5279         /* S == 1 or op2<1> == 1 */
5280         unallocated_encoding(s);
5281         return;
5282     }
5283     sf = extract32(insn, 31, 1);
5284     else_inv = extract32(insn, 30, 1);
5285     rm = extract32(insn, 16, 5);
5286     cond = extract32(insn, 12, 4);
5287     else_inc = extract32(insn, 10, 1);
5288     rn = extract32(insn, 5, 5);
5289     rd = extract32(insn, 0, 5);
5290 
5291     tcg_rd = cpu_reg(s, rd);
5292 
5293     a64_test_cc(&c, cond);
5294     zero = tcg_constant_i64(0);
5295 
5296     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
5297         /* CSET & CSETM.  */
5298         tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
5299         if (else_inv) {
5300             tcg_gen_neg_i64(tcg_rd, tcg_rd);
5301         }
5302     } else {
5303         TCGv_i64 t_true = cpu_reg(s, rn);
5304         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
5305         if (else_inv && else_inc) {
5306             tcg_gen_neg_i64(t_false, t_false);
5307         } else if (else_inv) {
5308             tcg_gen_not_i64(t_false, t_false);
5309         } else if (else_inc) {
5310             tcg_gen_addi_i64(t_false, t_false, 1);
5311         }
5312         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
5313     }
5314 
5315     if (!sf) {
5316         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5317     }
5318 }
5319 
5320 static void handle_clz(DisasContext *s, unsigned int sf,
5321                        unsigned int rn, unsigned int rd)
5322 {
5323     TCGv_i64 tcg_rd, tcg_rn;
5324     tcg_rd = cpu_reg(s, rd);
5325     tcg_rn = cpu_reg(s, rn);
5326 
5327     if (sf) {
5328         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
5329     } else {
5330         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5331         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5332         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
5333         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5334     }
5335 }
5336 
5337 static void handle_cls(DisasContext *s, unsigned int sf,
5338                        unsigned int rn, unsigned int rd)
5339 {
5340     TCGv_i64 tcg_rd, tcg_rn;
5341     tcg_rd = cpu_reg(s, rd);
5342     tcg_rn = cpu_reg(s, rn);
5343 
5344     if (sf) {
5345         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
5346     } else {
5347         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5348         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5349         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
5350         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5351     }
5352 }
5353 
5354 static void handle_rbit(DisasContext *s, unsigned int sf,
5355                         unsigned int rn, unsigned int rd)
5356 {
5357     TCGv_i64 tcg_rd, tcg_rn;
5358     tcg_rd = cpu_reg(s, rd);
5359     tcg_rn = cpu_reg(s, rn);
5360 
5361     if (sf) {
5362         gen_helper_rbit64(tcg_rd, tcg_rn);
5363     } else {
5364         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5365         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5366         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5367         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5368     }
5369 }
5370 
5371 /* REV with sf==1, opcode==3 ("REV64") */
5372 static void handle_rev64(DisasContext *s, unsigned int sf,
5373                          unsigned int rn, unsigned int rd)
5374 {
5375     if (!sf) {
5376         unallocated_encoding(s);
5377         return;
5378     }
5379     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5380 }
5381 
5382 /* REV with sf==0, opcode==2
5383  * REV32 (sf==1, opcode==2)
5384  */
5385 static void handle_rev32(DisasContext *s, unsigned int sf,
5386                          unsigned int rn, unsigned int rd)
5387 {
5388     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5389     TCGv_i64 tcg_rn = cpu_reg(s, rn);
5390 
5391     if (sf) {
5392         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
5393         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
5394     } else {
5395         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
5396     }
5397 }
5398 
5399 /* REV16 (opcode==1) */
5400 static void handle_rev16(DisasContext *s, unsigned int sf,
5401                          unsigned int rn, unsigned int rd)
5402 {
5403     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5404     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5405     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5406     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5407 
5408     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5409     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5410     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5411     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5412     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5413 }
5414 
5415 /* Data-processing (1 source)
5416  *   31  30  29  28             21 20     16 15    10 9    5 4    0
5417  * +----+---+---+-----------------+---------+--------+------+------+
5418  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
5419  * +----+---+---+-----------------+---------+--------+------+------+
5420  */
5421 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5422 {
5423     unsigned int sf, opcode, opcode2, rn, rd;
5424     TCGv_i64 tcg_rd;
5425 
5426     if (extract32(insn, 29, 1)) {
5427         unallocated_encoding(s);
5428         return;
5429     }
5430 
5431     sf = extract32(insn, 31, 1);
5432     opcode = extract32(insn, 10, 6);
5433     opcode2 = extract32(insn, 16, 5);
5434     rn = extract32(insn, 5, 5);
5435     rd = extract32(insn, 0, 5);
5436 
5437 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5438 
5439     switch (MAP(sf, opcode2, opcode)) {
5440     case MAP(0, 0x00, 0x00): /* RBIT */
5441     case MAP(1, 0x00, 0x00):
5442         handle_rbit(s, sf, rn, rd);
5443         break;
5444     case MAP(0, 0x00, 0x01): /* REV16 */
5445     case MAP(1, 0x00, 0x01):
5446         handle_rev16(s, sf, rn, rd);
5447         break;
5448     case MAP(0, 0x00, 0x02): /* REV/REV32 */
5449     case MAP(1, 0x00, 0x02):
5450         handle_rev32(s, sf, rn, rd);
5451         break;
5452     case MAP(1, 0x00, 0x03): /* REV64 */
5453         handle_rev64(s, sf, rn, rd);
5454         break;
5455     case MAP(0, 0x00, 0x04): /* CLZ */
5456     case MAP(1, 0x00, 0x04):
5457         handle_clz(s, sf, rn, rd);
5458         break;
5459     case MAP(0, 0x00, 0x05): /* CLS */
5460     case MAP(1, 0x00, 0x05):
5461         handle_cls(s, sf, rn, rd);
5462         break;
5463     case MAP(1, 0x01, 0x00): /* PACIA */
5464         if (s->pauth_active) {
5465             tcg_rd = cpu_reg(s, rd);
5466             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5467         } else if (!dc_isar_feature(aa64_pauth, s)) {
5468             goto do_unallocated;
5469         }
5470         break;
5471     case MAP(1, 0x01, 0x01): /* PACIB */
5472         if (s->pauth_active) {
5473             tcg_rd = cpu_reg(s, rd);
5474             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5475         } else if (!dc_isar_feature(aa64_pauth, s)) {
5476             goto do_unallocated;
5477         }
5478         break;
5479     case MAP(1, 0x01, 0x02): /* PACDA */
5480         if (s->pauth_active) {
5481             tcg_rd = cpu_reg(s, rd);
5482             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5483         } else if (!dc_isar_feature(aa64_pauth, s)) {
5484             goto do_unallocated;
5485         }
5486         break;
5487     case MAP(1, 0x01, 0x03): /* PACDB */
5488         if (s->pauth_active) {
5489             tcg_rd = cpu_reg(s, rd);
5490             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5491         } else if (!dc_isar_feature(aa64_pauth, s)) {
5492             goto do_unallocated;
5493         }
5494         break;
5495     case MAP(1, 0x01, 0x04): /* AUTIA */
5496         if (s->pauth_active) {
5497             tcg_rd = cpu_reg(s, rd);
5498             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5499         } else if (!dc_isar_feature(aa64_pauth, s)) {
5500             goto do_unallocated;
5501         }
5502         break;
5503     case MAP(1, 0x01, 0x05): /* AUTIB */
5504         if (s->pauth_active) {
5505             tcg_rd = cpu_reg(s, rd);
5506             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5507         } else if (!dc_isar_feature(aa64_pauth, s)) {
5508             goto do_unallocated;
5509         }
5510         break;
5511     case MAP(1, 0x01, 0x06): /* AUTDA */
5512         if (s->pauth_active) {
5513             tcg_rd = cpu_reg(s, rd);
5514             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5515         } else if (!dc_isar_feature(aa64_pauth, s)) {
5516             goto do_unallocated;
5517         }
5518         break;
5519     case MAP(1, 0x01, 0x07): /* AUTDB */
5520         if (s->pauth_active) {
5521             tcg_rd = cpu_reg(s, rd);
5522             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5523         } else if (!dc_isar_feature(aa64_pauth, s)) {
5524             goto do_unallocated;
5525         }
5526         break;
5527     case MAP(1, 0x01, 0x08): /* PACIZA */
5528         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5529             goto do_unallocated;
5530         } else if (s->pauth_active) {
5531             tcg_rd = cpu_reg(s, rd);
5532             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5533         }
5534         break;
5535     case MAP(1, 0x01, 0x09): /* PACIZB */
5536         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5537             goto do_unallocated;
5538         } else if (s->pauth_active) {
5539             tcg_rd = cpu_reg(s, rd);
5540             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5541         }
5542         break;
5543     case MAP(1, 0x01, 0x0a): /* PACDZA */
5544         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5545             goto do_unallocated;
5546         } else if (s->pauth_active) {
5547             tcg_rd = cpu_reg(s, rd);
5548             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5549         }
5550         break;
5551     case MAP(1, 0x01, 0x0b): /* PACDZB */
5552         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5553             goto do_unallocated;
5554         } else if (s->pauth_active) {
5555             tcg_rd = cpu_reg(s, rd);
5556             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5557         }
5558         break;
5559     case MAP(1, 0x01, 0x0c): /* AUTIZA */
5560         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5561             goto do_unallocated;
5562         } else if (s->pauth_active) {
5563             tcg_rd = cpu_reg(s, rd);
5564             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5565         }
5566         break;
5567     case MAP(1, 0x01, 0x0d): /* AUTIZB */
5568         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5569             goto do_unallocated;
5570         } else if (s->pauth_active) {
5571             tcg_rd = cpu_reg(s, rd);
5572             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5573         }
5574         break;
5575     case MAP(1, 0x01, 0x0e): /* AUTDZA */
5576         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5577             goto do_unallocated;
5578         } else if (s->pauth_active) {
5579             tcg_rd = cpu_reg(s, rd);
5580             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5581         }
5582         break;
5583     case MAP(1, 0x01, 0x0f): /* AUTDZB */
5584         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5585             goto do_unallocated;
5586         } else if (s->pauth_active) {
5587             tcg_rd = cpu_reg(s, rd);
5588             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5589         }
5590         break;
5591     case MAP(1, 0x01, 0x10): /* XPACI */
5592         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5593             goto do_unallocated;
5594         } else if (s->pauth_active) {
5595             tcg_rd = cpu_reg(s, rd);
5596             gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
5597         }
5598         break;
5599     case MAP(1, 0x01, 0x11): /* XPACD */
5600         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5601             goto do_unallocated;
5602         } else if (s->pauth_active) {
5603             tcg_rd = cpu_reg(s, rd);
5604             gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5605         }
5606         break;
5607     default:
5608     do_unallocated:
5609         unallocated_encoding(s);
5610         break;
5611     }
5612 
5613 #undef MAP
5614 }
5615 
5616 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5617                        unsigned int rm, unsigned int rn, unsigned int rd)
5618 {
5619     TCGv_i64 tcg_n, tcg_m, tcg_rd;
5620     tcg_rd = cpu_reg(s, rd);
5621 
5622     if (!sf && is_signed) {
5623         tcg_n = tcg_temp_new_i64();
5624         tcg_m = tcg_temp_new_i64();
5625         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5626         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5627     } else {
5628         tcg_n = read_cpu_reg(s, rn, sf);
5629         tcg_m = read_cpu_reg(s, rm, sf);
5630     }
5631 
5632     if (is_signed) {
5633         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5634     } else {
5635         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5636     }
5637 
5638     if (!sf) { /* zero extend final result */
5639         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5640     }
5641 }
5642 
5643 /* LSLV, LSRV, ASRV, RORV */
5644 static void handle_shift_reg(DisasContext *s,
5645                              enum a64_shift_type shift_type, unsigned int sf,
5646                              unsigned int rm, unsigned int rn, unsigned int rd)
5647 {
5648     TCGv_i64 tcg_shift = tcg_temp_new_i64();
5649     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5650     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5651 
5652     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5653     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5654 }
5655 
5656 /* CRC32[BHWX], CRC32C[BHWX] */
5657 static void handle_crc32(DisasContext *s,
5658                          unsigned int sf, unsigned int sz, bool crc32c,
5659                          unsigned int rm, unsigned int rn, unsigned int rd)
5660 {
5661     TCGv_i64 tcg_acc, tcg_val;
5662     TCGv_i32 tcg_bytes;
5663 
5664     if (!dc_isar_feature(aa64_crc32, s)
5665         || (sf == 1 && sz != 3)
5666         || (sf == 0 && sz == 3)) {
5667         unallocated_encoding(s);
5668         return;
5669     }
5670 
5671     if (sz == 3) {
5672         tcg_val = cpu_reg(s, rm);
5673     } else {
5674         uint64_t mask;
5675         switch (sz) {
5676         case 0:
5677             mask = 0xFF;
5678             break;
5679         case 1:
5680             mask = 0xFFFF;
5681             break;
5682         case 2:
5683             mask = 0xFFFFFFFF;
5684             break;
5685         default:
5686             g_assert_not_reached();
5687         }
5688         tcg_val = tcg_temp_new_i64();
5689         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5690     }
5691 
5692     tcg_acc = cpu_reg(s, rn);
5693     tcg_bytes = tcg_constant_i32(1 << sz);
5694 
5695     if (crc32c) {
5696         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5697     } else {
5698         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5699     }
5700 }
5701 
5702 /* Data-processing (2 source)
5703  *   31   30  29 28             21 20  16 15    10 9    5 4    0
5704  * +----+---+---+-----------------+------+--------+------+------+
5705  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
5706  * +----+---+---+-----------------+------+--------+------+------+
5707  */
5708 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5709 {
5710     unsigned int sf, rm, opcode, rn, rd, setflag;
5711     sf = extract32(insn, 31, 1);
5712     setflag = extract32(insn, 29, 1);
5713     rm = extract32(insn, 16, 5);
5714     opcode = extract32(insn, 10, 6);
5715     rn = extract32(insn, 5, 5);
5716     rd = extract32(insn, 0, 5);
5717 
5718     if (setflag && opcode != 0) {
5719         unallocated_encoding(s);
5720         return;
5721     }
5722 
5723     switch (opcode) {
5724     case 0: /* SUBP(S) */
5725         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5726             goto do_unallocated;
5727         } else {
5728             TCGv_i64 tcg_n, tcg_m, tcg_d;
5729 
5730             tcg_n = read_cpu_reg_sp(s, rn, true);
5731             tcg_m = read_cpu_reg_sp(s, rm, true);
5732             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5733             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5734             tcg_d = cpu_reg(s, rd);
5735 
5736             if (setflag) {
5737                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5738             } else {
5739                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5740             }
5741         }
5742         break;
5743     case 2: /* UDIV */
5744         handle_div(s, false, sf, rm, rn, rd);
5745         break;
5746     case 3: /* SDIV */
5747         handle_div(s, true, sf, rm, rn, rd);
5748         break;
5749     case 4: /* IRG */
5750         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5751             goto do_unallocated;
5752         }
5753         if (s->ata) {
5754             gen_helper_irg(cpu_reg_sp(s, rd), cpu_env,
5755                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
5756         } else {
5757             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5758                                              cpu_reg_sp(s, rn));
5759         }
5760         break;
5761     case 5: /* GMI */
5762         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5763             goto do_unallocated;
5764         } else {
5765             TCGv_i64 t = tcg_temp_new_i64();
5766 
5767             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
5768             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
5769             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
5770         }
5771         break;
5772     case 8: /* LSLV */
5773         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5774         break;
5775     case 9: /* LSRV */
5776         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5777         break;
5778     case 10: /* ASRV */
5779         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5780         break;
5781     case 11: /* RORV */
5782         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5783         break;
5784     case 12: /* PACGA */
5785         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5786             goto do_unallocated;
5787         }
5788         gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5789                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
5790         break;
5791     case 16:
5792     case 17:
5793     case 18:
5794     case 19:
5795     case 20:
5796     case 21:
5797     case 22:
5798     case 23: /* CRC32 */
5799     {
5800         int sz = extract32(opcode, 0, 2);
5801         bool crc32c = extract32(opcode, 2, 1);
5802         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5803         break;
5804     }
5805     default:
5806     do_unallocated:
5807         unallocated_encoding(s);
5808         break;
5809     }
5810 }
5811 
5812 /*
5813  * Data processing - register
5814  *  31  30 29  28      25    21  20  16      10         0
5815  * +--+---+--+---+-------+-----+-------+-------+---------+
5816  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
5817  * +--+---+--+---+-------+-----+-------+-------+---------+
5818  */
5819 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5820 {
5821     int op0 = extract32(insn, 30, 1);
5822     int op1 = extract32(insn, 28, 1);
5823     int op2 = extract32(insn, 21, 4);
5824     int op3 = extract32(insn, 10, 6);
5825 
5826     if (!op1) {
5827         if (op2 & 8) {
5828             if (op2 & 1) {
5829                 /* Add/sub (extended register) */
5830                 disas_add_sub_ext_reg(s, insn);
5831             } else {
5832                 /* Add/sub (shifted register) */
5833                 disas_add_sub_reg(s, insn);
5834             }
5835         } else {
5836             /* Logical (shifted register) */
5837             disas_logic_reg(s, insn);
5838         }
5839         return;
5840     }
5841 
5842     switch (op2) {
5843     case 0x0:
5844         switch (op3) {
5845         case 0x00: /* Add/subtract (with carry) */
5846             disas_adc_sbc(s, insn);
5847             break;
5848 
5849         case 0x01: /* Rotate right into flags */
5850         case 0x21:
5851             disas_rotate_right_into_flags(s, insn);
5852             break;
5853 
5854         case 0x02: /* Evaluate into flags */
5855         case 0x12:
5856         case 0x22:
5857         case 0x32:
5858             disas_evaluate_into_flags(s, insn);
5859             break;
5860 
5861         default:
5862             goto do_unallocated;
5863         }
5864         break;
5865 
5866     case 0x2: /* Conditional compare */
5867         disas_cc(s, insn); /* both imm and reg forms */
5868         break;
5869 
5870     case 0x4: /* Conditional select */
5871         disas_cond_select(s, insn);
5872         break;
5873 
5874     case 0x6: /* Data-processing */
5875         if (op0) {    /* (1 source) */
5876             disas_data_proc_1src(s, insn);
5877         } else {      /* (2 source) */
5878             disas_data_proc_2src(s, insn);
5879         }
5880         break;
5881     case 0x8 ... 0xf: /* (3 source) */
5882         disas_data_proc_3src(s, insn);
5883         break;
5884 
5885     default:
5886     do_unallocated:
5887         unallocated_encoding(s);
5888         break;
5889     }
5890 }
5891 
5892 static void handle_fp_compare(DisasContext *s, int size,
5893                               unsigned int rn, unsigned int rm,
5894                               bool cmp_with_zero, bool signal_all_nans)
5895 {
5896     TCGv_i64 tcg_flags = tcg_temp_new_i64();
5897     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
5898 
5899     if (size == MO_64) {
5900         TCGv_i64 tcg_vn, tcg_vm;
5901 
5902         tcg_vn = read_fp_dreg(s, rn);
5903         if (cmp_with_zero) {
5904             tcg_vm = tcg_constant_i64(0);
5905         } else {
5906             tcg_vm = read_fp_dreg(s, rm);
5907         }
5908         if (signal_all_nans) {
5909             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5910         } else {
5911             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5912         }
5913     } else {
5914         TCGv_i32 tcg_vn = tcg_temp_new_i32();
5915         TCGv_i32 tcg_vm = tcg_temp_new_i32();
5916 
5917         read_vec_element_i32(s, tcg_vn, rn, 0, size);
5918         if (cmp_with_zero) {
5919             tcg_gen_movi_i32(tcg_vm, 0);
5920         } else {
5921             read_vec_element_i32(s, tcg_vm, rm, 0, size);
5922         }
5923 
5924         switch (size) {
5925         case MO_32:
5926             if (signal_all_nans) {
5927                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5928             } else {
5929                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5930             }
5931             break;
5932         case MO_16:
5933             if (signal_all_nans) {
5934                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5935             } else {
5936                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5937             }
5938             break;
5939         default:
5940             g_assert_not_reached();
5941         }
5942     }
5943 
5944     gen_set_nzcv(tcg_flags);
5945 }
5946 
5947 /* Floating point compare
5948  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
5949  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5950  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
5951  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5952  */
5953 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5954 {
5955     unsigned int mos, type, rm, op, rn, opc, op2r;
5956     int size;
5957 
5958     mos = extract32(insn, 29, 3);
5959     type = extract32(insn, 22, 2);
5960     rm = extract32(insn, 16, 5);
5961     op = extract32(insn, 14, 2);
5962     rn = extract32(insn, 5, 5);
5963     opc = extract32(insn, 3, 2);
5964     op2r = extract32(insn, 0, 3);
5965 
5966     if (mos || op || op2r) {
5967         unallocated_encoding(s);
5968         return;
5969     }
5970 
5971     switch (type) {
5972     case 0:
5973         size = MO_32;
5974         break;
5975     case 1:
5976         size = MO_64;
5977         break;
5978     case 3:
5979         size = MO_16;
5980         if (dc_isar_feature(aa64_fp16, s)) {
5981             break;
5982         }
5983         /* fallthru */
5984     default:
5985         unallocated_encoding(s);
5986         return;
5987     }
5988 
5989     if (!fp_access_check(s)) {
5990         return;
5991     }
5992 
5993     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
5994 }
5995 
5996 /* Floating point conditional compare
5997  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
5998  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5999  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
6000  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6001  */
6002 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
6003 {
6004     unsigned int mos, type, rm, cond, rn, op, nzcv;
6005     TCGLabel *label_continue = NULL;
6006     int size;
6007 
6008     mos = extract32(insn, 29, 3);
6009     type = extract32(insn, 22, 2);
6010     rm = extract32(insn, 16, 5);
6011     cond = extract32(insn, 12, 4);
6012     rn = extract32(insn, 5, 5);
6013     op = extract32(insn, 4, 1);
6014     nzcv = extract32(insn, 0, 4);
6015 
6016     if (mos) {
6017         unallocated_encoding(s);
6018         return;
6019     }
6020 
6021     switch (type) {
6022     case 0:
6023         size = MO_32;
6024         break;
6025     case 1:
6026         size = MO_64;
6027         break;
6028     case 3:
6029         size = MO_16;
6030         if (dc_isar_feature(aa64_fp16, s)) {
6031             break;
6032         }
6033         /* fallthru */
6034     default:
6035         unallocated_encoding(s);
6036         return;
6037     }
6038 
6039     if (!fp_access_check(s)) {
6040         return;
6041     }
6042 
6043     if (cond < 0x0e) { /* not always */
6044         TCGLabel *label_match = gen_new_label();
6045         label_continue = gen_new_label();
6046         arm_gen_test_cc(cond, label_match);
6047         /* nomatch: */
6048         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
6049         tcg_gen_br(label_continue);
6050         gen_set_label(label_match);
6051     }
6052 
6053     handle_fp_compare(s, size, rn, rm, false, op);
6054 
6055     if (cond < 0x0e) {
6056         gen_set_label(label_continue);
6057     }
6058 }
6059 
6060 /* Floating point conditional select
6061  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
6062  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6063  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
6064  * +---+---+---+-----------+------+---+------+------+-----+------+------+
6065  */
6066 static void disas_fp_csel(DisasContext *s, uint32_t insn)
6067 {
6068     unsigned int mos, type, rm, cond, rn, rd;
6069     TCGv_i64 t_true, t_false;
6070     DisasCompare64 c;
6071     MemOp sz;
6072 
6073     mos = extract32(insn, 29, 3);
6074     type = extract32(insn, 22, 2);
6075     rm = extract32(insn, 16, 5);
6076     cond = extract32(insn, 12, 4);
6077     rn = extract32(insn, 5, 5);
6078     rd = extract32(insn, 0, 5);
6079 
6080     if (mos) {
6081         unallocated_encoding(s);
6082         return;
6083     }
6084 
6085     switch (type) {
6086     case 0:
6087         sz = MO_32;
6088         break;
6089     case 1:
6090         sz = MO_64;
6091         break;
6092     case 3:
6093         sz = MO_16;
6094         if (dc_isar_feature(aa64_fp16, s)) {
6095             break;
6096         }
6097         /* fallthru */
6098     default:
6099         unallocated_encoding(s);
6100         return;
6101     }
6102 
6103     if (!fp_access_check(s)) {
6104         return;
6105     }
6106 
6107     /* Zero extend sreg & hreg inputs to 64 bits now.  */
6108     t_true = tcg_temp_new_i64();
6109     t_false = tcg_temp_new_i64();
6110     read_vec_element(s, t_true, rn, 0, sz);
6111     read_vec_element(s, t_false, rm, 0, sz);
6112 
6113     a64_test_cc(&c, cond);
6114     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
6115                         t_true, t_false);
6116 
6117     /* Note that sregs & hregs write back zeros to the high bits,
6118        and we've already done the zero-extension.  */
6119     write_fp_dreg(s, rd, t_true);
6120 }
6121 
6122 /* Floating-point data-processing (1 source) - half precision */
6123 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
6124 {
6125     TCGv_ptr fpst = NULL;
6126     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
6127     TCGv_i32 tcg_res = tcg_temp_new_i32();
6128 
6129     switch (opcode) {
6130     case 0x0: /* FMOV */
6131         tcg_gen_mov_i32(tcg_res, tcg_op);
6132         break;
6133     case 0x1: /* FABS */
6134         tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
6135         break;
6136     case 0x2: /* FNEG */
6137         tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
6138         break;
6139     case 0x3: /* FSQRT */
6140         fpst = fpstatus_ptr(FPST_FPCR_F16);
6141         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
6142         break;
6143     case 0x8: /* FRINTN */
6144     case 0x9: /* FRINTP */
6145     case 0xa: /* FRINTM */
6146     case 0xb: /* FRINTZ */
6147     case 0xc: /* FRINTA */
6148     {
6149         TCGv_i32 tcg_rmode;
6150 
6151         fpst = fpstatus_ptr(FPST_FPCR_F16);
6152         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
6153         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6154         gen_restore_rmode(tcg_rmode, fpst);
6155         break;
6156     }
6157     case 0xe: /* FRINTX */
6158         fpst = fpstatus_ptr(FPST_FPCR_F16);
6159         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
6160         break;
6161     case 0xf: /* FRINTI */
6162         fpst = fpstatus_ptr(FPST_FPCR_F16);
6163         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6164         break;
6165     default:
6166         g_assert_not_reached();
6167     }
6168 
6169     write_fp_sreg(s, rd, tcg_res);
6170 }
6171 
6172 /* Floating-point data-processing (1 source) - single precision */
6173 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
6174 {
6175     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
6176     TCGv_i32 tcg_op, tcg_res;
6177     TCGv_ptr fpst;
6178     int rmode = -1;
6179 
6180     tcg_op = read_fp_sreg(s, rn);
6181     tcg_res = tcg_temp_new_i32();
6182 
6183     switch (opcode) {
6184     case 0x0: /* FMOV */
6185         tcg_gen_mov_i32(tcg_res, tcg_op);
6186         goto done;
6187     case 0x1: /* FABS */
6188         gen_helper_vfp_abss(tcg_res, tcg_op);
6189         goto done;
6190     case 0x2: /* FNEG */
6191         gen_helper_vfp_negs(tcg_res, tcg_op);
6192         goto done;
6193     case 0x3: /* FSQRT */
6194         gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
6195         goto done;
6196     case 0x6: /* BFCVT */
6197         gen_fpst = gen_helper_bfcvt;
6198         break;
6199     case 0x8: /* FRINTN */
6200     case 0x9: /* FRINTP */
6201     case 0xa: /* FRINTM */
6202     case 0xb: /* FRINTZ */
6203     case 0xc: /* FRINTA */
6204         rmode = opcode & 7;
6205         gen_fpst = gen_helper_rints;
6206         break;
6207     case 0xe: /* FRINTX */
6208         gen_fpst = gen_helper_rints_exact;
6209         break;
6210     case 0xf: /* FRINTI */
6211         gen_fpst = gen_helper_rints;
6212         break;
6213     case 0x10: /* FRINT32Z */
6214         rmode = FPROUNDING_ZERO;
6215         gen_fpst = gen_helper_frint32_s;
6216         break;
6217     case 0x11: /* FRINT32X */
6218         gen_fpst = gen_helper_frint32_s;
6219         break;
6220     case 0x12: /* FRINT64Z */
6221         rmode = FPROUNDING_ZERO;
6222         gen_fpst = gen_helper_frint64_s;
6223         break;
6224     case 0x13: /* FRINT64X */
6225         gen_fpst = gen_helper_frint64_s;
6226         break;
6227     default:
6228         g_assert_not_reached();
6229     }
6230 
6231     fpst = fpstatus_ptr(FPST_FPCR);
6232     if (rmode >= 0) {
6233         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6234         gen_fpst(tcg_res, tcg_op, fpst);
6235         gen_restore_rmode(tcg_rmode, fpst);
6236     } else {
6237         gen_fpst(tcg_res, tcg_op, fpst);
6238     }
6239 
6240  done:
6241     write_fp_sreg(s, rd, tcg_res);
6242 }
6243 
6244 /* Floating-point data-processing (1 source) - double precision */
6245 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6246 {
6247     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6248     TCGv_i64 tcg_op, tcg_res;
6249     TCGv_ptr fpst;
6250     int rmode = -1;
6251 
6252     switch (opcode) {
6253     case 0x0: /* FMOV */
6254         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6255         return;
6256     }
6257 
6258     tcg_op = read_fp_dreg(s, rn);
6259     tcg_res = tcg_temp_new_i64();
6260 
6261     switch (opcode) {
6262     case 0x1: /* FABS */
6263         gen_helper_vfp_absd(tcg_res, tcg_op);
6264         goto done;
6265     case 0x2: /* FNEG */
6266         gen_helper_vfp_negd(tcg_res, tcg_op);
6267         goto done;
6268     case 0x3: /* FSQRT */
6269         gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
6270         goto done;
6271     case 0x8: /* FRINTN */
6272     case 0x9: /* FRINTP */
6273     case 0xa: /* FRINTM */
6274     case 0xb: /* FRINTZ */
6275     case 0xc: /* FRINTA */
6276         rmode = opcode & 7;
6277         gen_fpst = gen_helper_rintd;
6278         break;
6279     case 0xe: /* FRINTX */
6280         gen_fpst = gen_helper_rintd_exact;
6281         break;
6282     case 0xf: /* FRINTI */
6283         gen_fpst = gen_helper_rintd;
6284         break;
6285     case 0x10: /* FRINT32Z */
6286         rmode = FPROUNDING_ZERO;
6287         gen_fpst = gen_helper_frint32_d;
6288         break;
6289     case 0x11: /* FRINT32X */
6290         gen_fpst = gen_helper_frint32_d;
6291         break;
6292     case 0x12: /* FRINT64Z */
6293         rmode = FPROUNDING_ZERO;
6294         gen_fpst = gen_helper_frint64_d;
6295         break;
6296     case 0x13: /* FRINT64X */
6297         gen_fpst = gen_helper_frint64_d;
6298         break;
6299     default:
6300         g_assert_not_reached();
6301     }
6302 
6303     fpst = fpstatus_ptr(FPST_FPCR);
6304     if (rmode >= 0) {
6305         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6306         gen_fpst(tcg_res, tcg_op, fpst);
6307         gen_restore_rmode(tcg_rmode, fpst);
6308     } else {
6309         gen_fpst(tcg_res, tcg_op, fpst);
6310     }
6311 
6312  done:
6313     write_fp_dreg(s, rd, tcg_res);
6314 }
6315 
6316 static void handle_fp_fcvt(DisasContext *s, int opcode,
6317                            int rd, int rn, int dtype, int ntype)
6318 {
6319     switch (ntype) {
6320     case 0x0:
6321     {
6322         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6323         if (dtype == 1) {
6324             /* Single to double */
6325             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6326             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
6327             write_fp_dreg(s, rd, tcg_rd);
6328         } else {
6329             /* Single to half */
6330             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6331             TCGv_i32 ahp = get_ahp_flag();
6332             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6333 
6334             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6335             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6336             write_fp_sreg(s, rd, tcg_rd);
6337         }
6338         break;
6339     }
6340     case 0x1:
6341     {
6342         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6343         TCGv_i32 tcg_rd = tcg_temp_new_i32();
6344         if (dtype == 0) {
6345             /* Double to single */
6346             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
6347         } else {
6348             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6349             TCGv_i32 ahp = get_ahp_flag();
6350             /* Double to half */
6351             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6352             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6353         }
6354         write_fp_sreg(s, rd, tcg_rd);
6355         break;
6356     }
6357     case 0x3:
6358     {
6359         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6360         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6361         TCGv_i32 tcg_ahp = get_ahp_flag();
6362         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6363         if (dtype == 0) {
6364             /* Half to single */
6365             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6366             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6367             write_fp_sreg(s, rd, tcg_rd);
6368         } else {
6369             /* Half to double */
6370             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6371             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6372             write_fp_dreg(s, rd, tcg_rd);
6373         }
6374         break;
6375     }
6376     default:
6377         g_assert_not_reached();
6378     }
6379 }
6380 
6381 /* Floating point data-processing (1 source)
6382  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
6383  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6384  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
6385  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6386  */
6387 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6388 {
6389     int mos = extract32(insn, 29, 3);
6390     int type = extract32(insn, 22, 2);
6391     int opcode = extract32(insn, 15, 6);
6392     int rn = extract32(insn, 5, 5);
6393     int rd = extract32(insn, 0, 5);
6394 
6395     if (mos) {
6396         goto do_unallocated;
6397     }
6398 
6399     switch (opcode) {
6400     case 0x4: case 0x5: case 0x7:
6401     {
6402         /* FCVT between half, single and double precision */
6403         int dtype = extract32(opcode, 0, 2);
6404         if (type == 2 || dtype == type) {
6405             goto do_unallocated;
6406         }
6407         if (!fp_access_check(s)) {
6408             return;
6409         }
6410 
6411         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6412         break;
6413     }
6414 
6415     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6416         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6417             goto do_unallocated;
6418         }
6419         /* fall through */
6420     case 0x0 ... 0x3:
6421     case 0x8 ... 0xc:
6422     case 0xe ... 0xf:
6423         /* 32-to-32 and 64-to-64 ops */
6424         switch (type) {
6425         case 0:
6426             if (!fp_access_check(s)) {
6427                 return;
6428             }
6429             handle_fp_1src_single(s, opcode, rd, rn);
6430             break;
6431         case 1:
6432             if (!fp_access_check(s)) {
6433                 return;
6434             }
6435             handle_fp_1src_double(s, opcode, rd, rn);
6436             break;
6437         case 3:
6438             if (!dc_isar_feature(aa64_fp16, s)) {
6439                 goto do_unallocated;
6440             }
6441 
6442             if (!fp_access_check(s)) {
6443                 return;
6444             }
6445             handle_fp_1src_half(s, opcode, rd, rn);
6446             break;
6447         default:
6448             goto do_unallocated;
6449         }
6450         break;
6451 
6452     case 0x6:
6453         switch (type) {
6454         case 1: /* BFCVT */
6455             if (!dc_isar_feature(aa64_bf16, s)) {
6456                 goto do_unallocated;
6457             }
6458             if (!fp_access_check(s)) {
6459                 return;
6460             }
6461             handle_fp_1src_single(s, opcode, rd, rn);
6462             break;
6463         default:
6464             goto do_unallocated;
6465         }
6466         break;
6467 
6468     default:
6469     do_unallocated:
6470         unallocated_encoding(s);
6471         break;
6472     }
6473 }
6474 
6475 /* Floating-point data-processing (2 source) - single precision */
6476 static void handle_fp_2src_single(DisasContext *s, int opcode,
6477                                   int rd, int rn, int rm)
6478 {
6479     TCGv_i32 tcg_op1;
6480     TCGv_i32 tcg_op2;
6481     TCGv_i32 tcg_res;
6482     TCGv_ptr fpst;
6483 
6484     tcg_res = tcg_temp_new_i32();
6485     fpst = fpstatus_ptr(FPST_FPCR);
6486     tcg_op1 = read_fp_sreg(s, rn);
6487     tcg_op2 = read_fp_sreg(s, rm);
6488 
6489     switch (opcode) {
6490     case 0x0: /* FMUL */
6491         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6492         break;
6493     case 0x1: /* FDIV */
6494         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6495         break;
6496     case 0x2: /* FADD */
6497         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6498         break;
6499     case 0x3: /* FSUB */
6500         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6501         break;
6502     case 0x4: /* FMAX */
6503         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6504         break;
6505     case 0x5: /* FMIN */
6506         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6507         break;
6508     case 0x6: /* FMAXNM */
6509         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6510         break;
6511     case 0x7: /* FMINNM */
6512         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6513         break;
6514     case 0x8: /* FNMUL */
6515         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6516         gen_helper_vfp_negs(tcg_res, tcg_res);
6517         break;
6518     }
6519 
6520     write_fp_sreg(s, rd, tcg_res);
6521 }
6522 
6523 /* Floating-point data-processing (2 source) - double precision */
6524 static void handle_fp_2src_double(DisasContext *s, int opcode,
6525                                   int rd, int rn, int rm)
6526 {
6527     TCGv_i64 tcg_op1;
6528     TCGv_i64 tcg_op2;
6529     TCGv_i64 tcg_res;
6530     TCGv_ptr fpst;
6531 
6532     tcg_res = tcg_temp_new_i64();
6533     fpst = fpstatus_ptr(FPST_FPCR);
6534     tcg_op1 = read_fp_dreg(s, rn);
6535     tcg_op2 = read_fp_dreg(s, rm);
6536 
6537     switch (opcode) {
6538     case 0x0: /* FMUL */
6539         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6540         break;
6541     case 0x1: /* FDIV */
6542         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6543         break;
6544     case 0x2: /* FADD */
6545         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6546         break;
6547     case 0x3: /* FSUB */
6548         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6549         break;
6550     case 0x4: /* FMAX */
6551         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6552         break;
6553     case 0x5: /* FMIN */
6554         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6555         break;
6556     case 0x6: /* FMAXNM */
6557         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6558         break;
6559     case 0x7: /* FMINNM */
6560         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6561         break;
6562     case 0x8: /* FNMUL */
6563         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6564         gen_helper_vfp_negd(tcg_res, tcg_res);
6565         break;
6566     }
6567 
6568     write_fp_dreg(s, rd, tcg_res);
6569 }
6570 
6571 /* Floating-point data-processing (2 source) - half precision */
6572 static void handle_fp_2src_half(DisasContext *s, int opcode,
6573                                 int rd, int rn, int rm)
6574 {
6575     TCGv_i32 tcg_op1;
6576     TCGv_i32 tcg_op2;
6577     TCGv_i32 tcg_res;
6578     TCGv_ptr fpst;
6579 
6580     tcg_res = tcg_temp_new_i32();
6581     fpst = fpstatus_ptr(FPST_FPCR_F16);
6582     tcg_op1 = read_fp_hreg(s, rn);
6583     tcg_op2 = read_fp_hreg(s, rm);
6584 
6585     switch (opcode) {
6586     case 0x0: /* FMUL */
6587         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6588         break;
6589     case 0x1: /* FDIV */
6590         gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6591         break;
6592     case 0x2: /* FADD */
6593         gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6594         break;
6595     case 0x3: /* FSUB */
6596         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6597         break;
6598     case 0x4: /* FMAX */
6599         gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6600         break;
6601     case 0x5: /* FMIN */
6602         gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6603         break;
6604     case 0x6: /* FMAXNM */
6605         gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6606         break;
6607     case 0x7: /* FMINNM */
6608         gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6609         break;
6610     case 0x8: /* FNMUL */
6611         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6612         tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6613         break;
6614     default:
6615         g_assert_not_reached();
6616     }
6617 
6618     write_fp_sreg(s, rd, tcg_res);
6619 }
6620 
6621 /* Floating point data-processing (2 source)
6622  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
6623  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6624  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
6625  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6626  */
6627 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6628 {
6629     int mos = extract32(insn, 29, 3);
6630     int type = extract32(insn, 22, 2);
6631     int rd = extract32(insn, 0, 5);
6632     int rn = extract32(insn, 5, 5);
6633     int rm = extract32(insn, 16, 5);
6634     int opcode = extract32(insn, 12, 4);
6635 
6636     if (opcode > 8 || mos) {
6637         unallocated_encoding(s);
6638         return;
6639     }
6640 
6641     switch (type) {
6642     case 0:
6643         if (!fp_access_check(s)) {
6644             return;
6645         }
6646         handle_fp_2src_single(s, opcode, rd, rn, rm);
6647         break;
6648     case 1:
6649         if (!fp_access_check(s)) {
6650             return;
6651         }
6652         handle_fp_2src_double(s, opcode, rd, rn, rm);
6653         break;
6654     case 3:
6655         if (!dc_isar_feature(aa64_fp16, s)) {
6656             unallocated_encoding(s);
6657             return;
6658         }
6659         if (!fp_access_check(s)) {
6660             return;
6661         }
6662         handle_fp_2src_half(s, opcode, rd, rn, rm);
6663         break;
6664     default:
6665         unallocated_encoding(s);
6666     }
6667 }
6668 
6669 /* Floating-point data-processing (3 source) - single precision */
6670 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6671                                   int rd, int rn, int rm, int ra)
6672 {
6673     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6674     TCGv_i32 tcg_res = tcg_temp_new_i32();
6675     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6676 
6677     tcg_op1 = read_fp_sreg(s, rn);
6678     tcg_op2 = read_fp_sreg(s, rm);
6679     tcg_op3 = read_fp_sreg(s, ra);
6680 
6681     /* These are fused multiply-add, and must be done as one
6682      * floating point operation with no rounding between the
6683      * multiplication and addition steps.
6684      * NB that doing the negations here as separate steps is
6685      * correct : an input NaN should come out with its sign bit
6686      * flipped if it is a negated-input.
6687      */
6688     if (o1 == true) {
6689         gen_helper_vfp_negs(tcg_op3, tcg_op3);
6690     }
6691 
6692     if (o0 != o1) {
6693         gen_helper_vfp_negs(tcg_op1, tcg_op1);
6694     }
6695 
6696     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6697 
6698     write_fp_sreg(s, rd, tcg_res);
6699 }
6700 
6701 /* Floating-point data-processing (3 source) - double precision */
6702 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6703                                   int rd, int rn, int rm, int ra)
6704 {
6705     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6706     TCGv_i64 tcg_res = tcg_temp_new_i64();
6707     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6708 
6709     tcg_op1 = read_fp_dreg(s, rn);
6710     tcg_op2 = read_fp_dreg(s, rm);
6711     tcg_op3 = read_fp_dreg(s, ra);
6712 
6713     /* These are fused multiply-add, and must be done as one
6714      * floating point operation with no rounding between the
6715      * multiplication and addition steps.
6716      * NB that doing the negations here as separate steps is
6717      * correct : an input NaN should come out with its sign bit
6718      * flipped if it is a negated-input.
6719      */
6720     if (o1 == true) {
6721         gen_helper_vfp_negd(tcg_op3, tcg_op3);
6722     }
6723 
6724     if (o0 != o1) {
6725         gen_helper_vfp_negd(tcg_op1, tcg_op1);
6726     }
6727 
6728     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6729 
6730     write_fp_dreg(s, rd, tcg_res);
6731 }
6732 
6733 /* Floating-point data-processing (3 source) - half precision */
6734 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6735                                 int rd, int rn, int rm, int ra)
6736 {
6737     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6738     TCGv_i32 tcg_res = tcg_temp_new_i32();
6739     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6740 
6741     tcg_op1 = read_fp_hreg(s, rn);
6742     tcg_op2 = read_fp_hreg(s, rm);
6743     tcg_op3 = read_fp_hreg(s, ra);
6744 
6745     /* These are fused multiply-add, and must be done as one
6746      * floating point operation with no rounding between the
6747      * multiplication and addition steps.
6748      * NB that doing the negations here as separate steps is
6749      * correct : an input NaN should come out with its sign bit
6750      * flipped if it is a negated-input.
6751      */
6752     if (o1 == true) {
6753         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6754     }
6755 
6756     if (o0 != o1) {
6757         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6758     }
6759 
6760     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6761 
6762     write_fp_sreg(s, rd, tcg_res);
6763 }
6764 
6765 /* Floating point data-processing (3 source)
6766  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
6767  * +---+---+---+-----------+------+----+------+----+------+------+------+
6768  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6769  * +---+---+---+-----------+------+----+------+----+------+------+------+
6770  */
6771 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6772 {
6773     int mos = extract32(insn, 29, 3);
6774     int type = extract32(insn, 22, 2);
6775     int rd = extract32(insn, 0, 5);
6776     int rn = extract32(insn, 5, 5);
6777     int ra = extract32(insn, 10, 5);
6778     int rm = extract32(insn, 16, 5);
6779     bool o0 = extract32(insn, 15, 1);
6780     bool o1 = extract32(insn, 21, 1);
6781 
6782     if (mos) {
6783         unallocated_encoding(s);
6784         return;
6785     }
6786 
6787     switch (type) {
6788     case 0:
6789         if (!fp_access_check(s)) {
6790             return;
6791         }
6792         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6793         break;
6794     case 1:
6795         if (!fp_access_check(s)) {
6796             return;
6797         }
6798         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6799         break;
6800     case 3:
6801         if (!dc_isar_feature(aa64_fp16, s)) {
6802             unallocated_encoding(s);
6803             return;
6804         }
6805         if (!fp_access_check(s)) {
6806             return;
6807         }
6808         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6809         break;
6810     default:
6811         unallocated_encoding(s);
6812     }
6813 }
6814 
6815 /* Floating point immediate
6816  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
6817  * +---+---+---+-----------+------+---+------------+-------+------+------+
6818  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
6819  * +---+---+---+-----------+------+---+------------+-------+------+------+
6820  */
6821 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6822 {
6823     int rd = extract32(insn, 0, 5);
6824     int imm5 = extract32(insn, 5, 5);
6825     int imm8 = extract32(insn, 13, 8);
6826     int type = extract32(insn, 22, 2);
6827     int mos = extract32(insn, 29, 3);
6828     uint64_t imm;
6829     MemOp sz;
6830 
6831     if (mos || imm5) {
6832         unallocated_encoding(s);
6833         return;
6834     }
6835 
6836     switch (type) {
6837     case 0:
6838         sz = MO_32;
6839         break;
6840     case 1:
6841         sz = MO_64;
6842         break;
6843     case 3:
6844         sz = MO_16;
6845         if (dc_isar_feature(aa64_fp16, s)) {
6846             break;
6847         }
6848         /* fallthru */
6849     default:
6850         unallocated_encoding(s);
6851         return;
6852     }
6853 
6854     if (!fp_access_check(s)) {
6855         return;
6856     }
6857 
6858     imm = vfp_expand_imm(sz, imm8);
6859     write_fp_dreg(s, rd, tcg_constant_i64(imm));
6860 }
6861 
6862 /* Handle floating point <=> fixed point conversions. Note that we can
6863  * also deal with fp <=> integer conversions as a special case (scale == 64)
6864  * OPTME: consider handling that special case specially or at least skipping
6865  * the call to scalbn in the helpers for zero shifts.
6866  */
6867 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6868                            bool itof, int rmode, int scale, int sf, int type)
6869 {
6870     bool is_signed = !(opcode & 1);
6871     TCGv_ptr tcg_fpstatus;
6872     TCGv_i32 tcg_shift, tcg_single;
6873     TCGv_i64 tcg_double;
6874 
6875     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
6876 
6877     tcg_shift = tcg_constant_i32(64 - scale);
6878 
6879     if (itof) {
6880         TCGv_i64 tcg_int = cpu_reg(s, rn);
6881         if (!sf) {
6882             TCGv_i64 tcg_extend = tcg_temp_new_i64();
6883 
6884             if (is_signed) {
6885                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6886             } else {
6887                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6888             }
6889 
6890             tcg_int = tcg_extend;
6891         }
6892 
6893         switch (type) {
6894         case 1: /* float64 */
6895             tcg_double = tcg_temp_new_i64();
6896             if (is_signed) {
6897                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6898                                      tcg_shift, tcg_fpstatus);
6899             } else {
6900                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6901                                      tcg_shift, tcg_fpstatus);
6902             }
6903             write_fp_dreg(s, rd, tcg_double);
6904             break;
6905 
6906         case 0: /* float32 */
6907             tcg_single = tcg_temp_new_i32();
6908             if (is_signed) {
6909                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6910                                      tcg_shift, tcg_fpstatus);
6911             } else {
6912                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6913                                      tcg_shift, tcg_fpstatus);
6914             }
6915             write_fp_sreg(s, rd, tcg_single);
6916             break;
6917 
6918         case 3: /* float16 */
6919             tcg_single = tcg_temp_new_i32();
6920             if (is_signed) {
6921                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6922                                      tcg_shift, tcg_fpstatus);
6923             } else {
6924                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6925                                      tcg_shift, tcg_fpstatus);
6926             }
6927             write_fp_sreg(s, rd, tcg_single);
6928             break;
6929 
6930         default:
6931             g_assert_not_reached();
6932         }
6933     } else {
6934         TCGv_i64 tcg_int = cpu_reg(s, rd);
6935         TCGv_i32 tcg_rmode;
6936 
6937         if (extract32(opcode, 2, 1)) {
6938             /* There are too many rounding modes to all fit into rmode,
6939              * so FCVTA[US] is a special case.
6940              */
6941             rmode = FPROUNDING_TIEAWAY;
6942         }
6943 
6944         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
6945 
6946         switch (type) {
6947         case 1: /* float64 */
6948             tcg_double = read_fp_dreg(s, rn);
6949             if (is_signed) {
6950                 if (!sf) {
6951                     gen_helper_vfp_tosld(tcg_int, tcg_double,
6952                                          tcg_shift, tcg_fpstatus);
6953                 } else {
6954                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
6955                                          tcg_shift, tcg_fpstatus);
6956                 }
6957             } else {
6958                 if (!sf) {
6959                     gen_helper_vfp_tould(tcg_int, tcg_double,
6960                                          tcg_shift, tcg_fpstatus);
6961                 } else {
6962                     gen_helper_vfp_touqd(tcg_int, tcg_double,
6963                                          tcg_shift, tcg_fpstatus);
6964                 }
6965             }
6966             if (!sf) {
6967                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6968             }
6969             break;
6970 
6971         case 0: /* float32 */
6972             tcg_single = read_fp_sreg(s, rn);
6973             if (sf) {
6974                 if (is_signed) {
6975                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
6976                                          tcg_shift, tcg_fpstatus);
6977                 } else {
6978                     gen_helper_vfp_touqs(tcg_int, tcg_single,
6979                                          tcg_shift, tcg_fpstatus);
6980                 }
6981             } else {
6982                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6983                 if (is_signed) {
6984                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
6985                                          tcg_shift, tcg_fpstatus);
6986                 } else {
6987                     gen_helper_vfp_touls(tcg_dest, tcg_single,
6988                                          tcg_shift, tcg_fpstatus);
6989                 }
6990                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6991             }
6992             break;
6993 
6994         case 3: /* float16 */
6995             tcg_single = read_fp_sreg(s, rn);
6996             if (sf) {
6997                 if (is_signed) {
6998                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
6999                                          tcg_shift, tcg_fpstatus);
7000                 } else {
7001                     gen_helper_vfp_touqh(tcg_int, tcg_single,
7002                                          tcg_shift, tcg_fpstatus);
7003                 }
7004             } else {
7005                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7006                 if (is_signed) {
7007                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
7008                                          tcg_shift, tcg_fpstatus);
7009                 } else {
7010                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
7011                                          tcg_shift, tcg_fpstatus);
7012                 }
7013                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7014             }
7015             break;
7016 
7017         default:
7018             g_assert_not_reached();
7019         }
7020 
7021         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
7022     }
7023 }
7024 
7025 /* Floating point <-> fixed point conversions
7026  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
7027  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7028  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
7029  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7030  */
7031 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
7032 {
7033     int rd = extract32(insn, 0, 5);
7034     int rn = extract32(insn, 5, 5);
7035     int scale = extract32(insn, 10, 6);
7036     int opcode = extract32(insn, 16, 3);
7037     int rmode = extract32(insn, 19, 2);
7038     int type = extract32(insn, 22, 2);
7039     bool sbit = extract32(insn, 29, 1);
7040     bool sf = extract32(insn, 31, 1);
7041     bool itof;
7042 
7043     if (sbit || (!sf && scale < 32)) {
7044         unallocated_encoding(s);
7045         return;
7046     }
7047 
7048     switch (type) {
7049     case 0: /* float32 */
7050     case 1: /* float64 */
7051         break;
7052     case 3: /* float16 */
7053         if (dc_isar_feature(aa64_fp16, s)) {
7054             break;
7055         }
7056         /* fallthru */
7057     default:
7058         unallocated_encoding(s);
7059         return;
7060     }
7061 
7062     switch ((rmode << 3) | opcode) {
7063     case 0x2: /* SCVTF */
7064     case 0x3: /* UCVTF */
7065         itof = true;
7066         break;
7067     case 0x18: /* FCVTZS */
7068     case 0x19: /* FCVTZU */
7069         itof = false;
7070         break;
7071     default:
7072         unallocated_encoding(s);
7073         return;
7074     }
7075 
7076     if (!fp_access_check(s)) {
7077         return;
7078     }
7079 
7080     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
7081 }
7082 
7083 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
7084 {
7085     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
7086      * without conversion.
7087      */
7088 
7089     if (itof) {
7090         TCGv_i64 tcg_rn = cpu_reg(s, rn);
7091         TCGv_i64 tmp;
7092 
7093         switch (type) {
7094         case 0:
7095             /* 32 bit */
7096             tmp = tcg_temp_new_i64();
7097             tcg_gen_ext32u_i64(tmp, tcg_rn);
7098             write_fp_dreg(s, rd, tmp);
7099             break;
7100         case 1:
7101             /* 64 bit */
7102             write_fp_dreg(s, rd, tcg_rn);
7103             break;
7104         case 2:
7105             /* 64 bit to top half. */
7106             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
7107             clear_vec_high(s, true, rd);
7108             break;
7109         case 3:
7110             /* 16 bit */
7111             tmp = tcg_temp_new_i64();
7112             tcg_gen_ext16u_i64(tmp, tcg_rn);
7113             write_fp_dreg(s, rd, tmp);
7114             break;
7115         default:
7116             g_assert_not_reached();
7117         }
7118     } else {
7119         TCGv_i64 tcg_rd = cpu_reg(s, rd);
7120 
7121         switch (type) {
7122         case 0:
7123             /* 32 bit */
7124             tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
7125             break;
7126         case 1:
7127             /* 64 bit */
7128             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
7129             break;
7130         case 2:
7131             /* 64 bits from top half */
7132             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
7133             break;
7134         case 3:
7135             /* 16 bit */
7136             tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
7137             break;
7138         default:
7139             g_assert_not_reached();
7140         }
7141     }
7142 }
7143 
7144 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
7145 {
7146     TCGv_i64 t = read_fp_dreg(s, rn);
7147     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
7148 
7149     gen_helper_fjcvtzs(t, t, fpstatus);
7150 
7151     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
7152     tcg_gen_extrh_i64_i32(cpu_ZF, t);
7153     tcg_gen_movi_i32(cpu_CF, 0);
7154     tcg_gen_movi_i32(cpu_NF, 0);
7155     tcg_gen_movi_i32(cpu_VF, 0);
7156 }
7157 
7158 /* Floating point <-> integer conversions
7159  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
7160  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7161  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
7162  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7163  */
7164 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
7165 {
7166     int rd = extract32(insn, 0, 5);
7167     int rn = extract32(insn, 5, 5);
7168     int opcode = extract32(insn, 16, 3);
7169     int rmode = extract32(insn, 19, 2);
7170     int type = extract32(insn, 22, 2);
7171     bool sbit = extract32(insn, 29, 1);
7172     bool sf = extract32(insn, 31, 1);
7173     bool itof = false;
7174 
7175     if (sbit) {
7176         goto do_unallocated;
7177     }
7178 
7179     switch (opcode) {
7180     case 2: /* SCVTF */
7181     case 3: /* UCVTF */
7182         itof = true;
7183         /* fallthru */
7184     case 4: /* FCVTAS */
7185     case 5: /* FCVTAU */
7186         if (rmode != 0) {
7187             goto do_unallocated;
7188         }
7189         /* fallthru */
7190     case 0: /* FCVT[NPMZ]S */
7191     case 1: /* FCVT[NPMZ]U */
7192         switch (type) {
7193         case 0: /* float32 */
7194         case 1: /* float64 */
7195             break;
7196         case 3: /* float16 */
7197             if (!dc_isar_feature(aa64_fp16, s)) {
7198                 goto do_unallocated;
7199             }
7200             break;
7201         default:
7202             goto do_unallocated;
7203         }
7204         if (!fp_access_check(s)) {
7205             return;
7206         }
7207         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
7208         break;
7209 
7210     default:
7211         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
7212         case 0b01100110: /* FMOV half <-> 32-bit int */
7213         case 0b01100111:
7214         case 0b11100110: /* FMOV half <-> 64-bit int */
7215         case 0b11100111:
7216             if (!dc_isar_feature(aa64_fp16, s)) {
7217                 goto do_unallocated;
7218             }
7219             /* fallthru */
7220         case 0b00000110: /* FMOV 32-bit */
7221         case 0b00000111:
7222         case 0b10100110: /* FMOV 64-bit */
7223         case 0b10100111:
7224         case 0b11001110: /* FMOV top half of 128-bit */
7225         case 0b11001111:
7226             if (!fp_access_check(s)) {
7227                 return;
7228             }
7229             itof = opcode & 1;
7230             handle_fmov(s, rd, rn, type, itof);
7231             break;
7232 
7233         case 0b00111110: /* FJCVTZS */
7234             if (!dc_isar_feature(aa64_jscvt, s)) {
7235                 goto do_unallocated;
7236             } else if (fp_access_check(s)) {
7237                 handle_fjcvtzs(s, rd, rn);
7238             }
7239             break;
7240 
7241         default:
7242         do_unallocated:
7243             unallocated_encoding(s);
7244             return;
7245         }
7246         break;
7247     }
7248 }
7249 
7250 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7251  *   31  30  29 28     25 24                          0
7252  * +---+---+---+---------+-----------------------------+
7253  * |   | 0 |   | 1 1 1 1 |                             |
7254  * +---+---+---+---------+-----------------------------+
7255  */
7256 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7257 {
7258     if (extract32(insn, 24, 1)) {
7259         /* Floating point data-processing (3 source) */
7260         disas_fp_3src(s, insn);
7261     } else if (extract32(insn, 21, 1) == 0) {
7262         /* Floating point to fixed point conversions */
7263         disas_fp_fixed_conv(s, insn);
7264     } else {
7265         switch (extract32(insn, 10, 2)) {
7266         case 1:
7267             /* Floating point conditional compare */
7268             disas_fp_ccomp(s, insn);
7269             break;
7270         case 2:
7271             /* Floating point data-processing (2 source) */
7272             disas_fp_2src(s, insn);
7273             break;
7274         case 3:
7275             /* Floating point conditional select */
7276             disas_fp_csel(s, insn);
7277             break;
7278         case 0:
7279             switch (ctz32(extract32(insn, 12, 4))) {
7280             case 0: /* [15:12] == xxx1 */
7281                 /* Floating point immediate */
7282                 disas_fp_imm(s, insn);
7283                 break;
7284             case 1: /* [15:12] == xx10 */
7285                 /* Floating point compare */
7286                 disas_fp_compare(s, insn);
7287                 break;
7288             case 2: /* [15:12] == x100 */
7289                 /* Floating point data-processing (1 source) */
7290                 disas_fp_1src(s, insn);
7291                 break;
7292             case 3: /* [15:12] == 1000 */
7293                 unallocated_encoding(s);
7294                 break;
7295             default: /* [15:12] == 0000 */
7296                 /* Floating point <-> integer conversions */
7297                 disas_fp_int_conv(s, insn);
7298                 break;
7299             }
7300             break;
7301         }
7302     }
7303 }
7304 
7305 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7306                      int pos)
7307 {
7308     /* Extract 64 bits from the middle of two concatenated 64 bit
7309      * vector register slices left:right. The extracted bits start
7310      * at 'pos' bits into the right (least significant) side.
7311      * We return the result in tcg_right, and guarantee not to
7312      * trash tcg_left.
7313      */
7314     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7315     assert(pos > 0 && pos < 64);
7316 
7317     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7318     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7319     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7320 }
7321 
7322 /* EXT
7323  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
7324  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7325  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
7326  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7327  */
7328 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7329 {
7330     int is_q = extract32(insn, 30, 1);
7331     int op2 = extract32(insn, 22, 2);
7332     int imm4 = extract32(insn, 11, 4);
7333     int rm = extract32(insn, 16, 5);
7334     int rn = extract32(insn, 5, 5);
7335     int rd = extract32(insn, 0, 5);
7336     int pos = imm4 << 3;
7337     TCGv_i64 tcg_resl, tcg_resh;
7338 
7339     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7340         unallocated_encoding(s);
7341         return;
7342     }
7343 
7344     if (!fp_access_check(s)) {
7345         return;
7346     }
7347 
7348     tcg_resh = tcg_temp_new_i64();
7349     tcg_resl = tcg_temp_new_i64();
7350 
7351     /* Vd gets bits starting at pos bits into Vm:Vn. This is
7352      * either extracting 128 bits from a 128:128 concatenation, or
7353      * extracting 64 bits from a 64:64 concatenation.
7354      */
7355     if (!is_q) {
7356         read_vec_element(s, tcg_resl, rn, 0, MO_64);
7357         if (pos != 0) {
7358             read_vec_element(s, tcg_resh, rm, 0, MO_64);
7359             do_ext64(s, tcg_resh, tcg_resl, pos);
7360         }
7361     } else {
7362         TCGv_i64 tcg_hh;
7363         typedef struct {
7364             int reg;
7365             int elt;
7366         } EltPosns;
7367         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7368         EltPosns *elt = eltposns;
7369 
7370         if (pos >= 64) {
7371             elt++;
7372             pos -= 64;
7373         }
7374 
7375         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7376         elt++;
7377         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7378         elt++;
7379         if (pos != 0) {
7380             do_ext64(s, tcg_resh, tcg_resl, pos);
7381             tcg_hh = tcg_temp_new_i64();
7382             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7383             do_ext64(s, tcg_hh, tcg_resh, pos);
7384         }
7385     }
7386 
7387     write_vec_element(s, tcg_resl, rd, 0, MO_64);
7388     if (is_q) {
7389         write_vec_element(s, tcg_resh, rd, 1, MO_64);
7390     }
7391     clear_vec_high(s, is_q, rd);
7392 }
7393 
7394 /* TBL/TBX
7395  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
7396  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7397  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
7398  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7399  */
7400 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7401 {
7402     int op2 = extract32(insn, 22, 2);
7403     int is_q = extract32(insn, 30, 1);
7404     int rm = extract32(insn, 16, 5);
7405     int rn = extract32(insn, 5, 5);
7406     int rd = extract32(insn, 0, 5);
7407     int is_tbx = extract32(insn, 12, 1);
7408     int len = (extract32(insn, 13, 2) + 1) * 16;
7409 
7410     if (op2 != 0) {
7411         unallocated_encoding(s);
7412         return;
7413     }
7414 
7415     if (!fp_access_check(s)) {
7416         return;
7417     }
7418 
7419     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7420                        vec_full_reg_offset(s, rm), cpu_env,
7421                        is_q ? 16 : 8, vec_full_reg_size(s),
7422                        (len << 6) | (is_tbx << 5) | rn,
7423                        gen_helper_simd_tblx);
7424 }
7425 
7426 /* ZIP/UZP/TRN
7427  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
7428  * +---+---+-------------+------+---+------+---+------------------+------+
7429  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
7430  * +---+---+-------------+------+---+------+---+------------------+------+
7431  */
7432 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7433 {
7434     int rd = extract32(insn, 0, 5);
7435     int rn = extract32(insn, 5, 5);
7436     int rm = extract32(insn, 16, 5);
7437     int size = extract32(insn, 22, 2);
7438     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7439      * bit 2 indicates 1 vs 2 variant of the insn.
7440      */
7441     int opcode = extract32(insn, 12, 2);
7442     bool part = extract32(insn, 14, 1);
7443     bool is_q = extract32(insn, 30, 1);
7444     int esize = 8 << size;
7445     int i;
7446     int datasize = is_q ? 128 : 64;
7447     int elements = datasize / esize;
7448     TCGv_i64 tcg_res[2], tcg_ele;
7449 
7450     if (opcode == 0 || (size == 3 && !is_q)) {
7451         unallocated_encoding(s);
7452         return;
7453     }
7454 
7455     if (!fp_access_check(s)) {
7456         return;
7457     }
7458 
7459     tcg_res[0] = tcg_temp_new_i64();
7460     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
7461     tcg_ele = tcg_temp_new_i64();
7462 
7463     for (i = 0; i < elements; i++) {
7464         int o, w;
7465 
7466         switch (opcode) {
7467         case 1: /* UZP1/2 */
7468         {
7469             int midpoint = elements / 2;
7470             if (i < midpoint) {
7471                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
7472             } else {
7473                 read_vec_element(s, tcg_ele, rm,
7474                                  2 * (i - midpoint) + part, size);
7475             }
7476             break;
7477         }
7478         case 2: /* TRN1/2 */
7479             if (i & 1) {
7480                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
7481             } else {
7482                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
7483             }
7484             break;
7485         case 3: /* ZIP1/2 */
7486         {
7487             int base = part * elements / 2;
7488             if (i & 1) {
7489                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
7490             } else {
7491                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
7492             }
7493             break;
7494         }
7495         default:
7496             g_assert_not_reached();
7497         }
7498 
7499         w = (i * esize) / 64;
7500         o = (i * esize) % 64;
7501         if (o == 0) {
7502             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
7503         } else {
7504             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
7505             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
7506         }
7507     }
7508 
7509     for (i = 0; i <= is_q; ++i) {
7510         write_vec_element(s, tcg_res[i], rd, i, MO_64);
7511     }
7512     clear_vec_high(s, is_q, rd);
7513 }
7514 
7515 /*
7516  * do_reduction_op helper
7517  *
7518  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7519  * important for correct NaN propagation that we do these
7520  * operations in exactly the order specified by the pseudocode.
7521  *
7522  * This is a recursive function, TCG temps should be freed by the
7523  * calling function once it is done with the values.
7524  */
7525 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7526                                 int esize, int size, int vmap, TCGv_ptr fpst)
7527 {
7528     if (esize == size) {
7529         int element;
7530         MemOp msize = esize == 16 ? MO_16 : MO_32;
7531         TCGv_i32 tcg_elem;
7532 
7533         /* We should have one register left here */
7534         assert(ctpop8(vmap) == 1);
7535         element = ctz32(vmap);
7536         assert(element < 8);
7537 
7538         tcg_elem = tcg_temp_new_i32();
7539         read_vec_element_i32(s, tcg_elem, rn, element, msize);
7540         return tcg_elem;
7541     } else {
7542         int bits = size / 2;
7543         int shift = ctpop8(vmap) / 2;
7544         int vmap_lo = (vmap >> shift) & vmap;
7545         int vmap_hi = (vmap & ~vmap_lo);
7546         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7547 
7548         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7549         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7550         tcg_res = tcg_temp_new_i32();
7551 
7552         switch (fpopcode) {
7553         case 0x0c: /* fmaxnmv half-precision */
7554             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7555             break;
7556         case 0x0f: /* fmaxv half-precision */
7557             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7558             break;
7559         case 0x1c: /* fminnmv half-precision */
7560             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7561             break;
7562         case 0x1f: /* fminv half-precision */
7563             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7564             break;
7565         case 0x2c: /* fmaxnmv */
7566             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7567             break;
7568         case 0x2f: /* fmaxv */
7569             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7570             break;
7571         case 0x3c: /* fminnmv */
7572             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7573             break;
7574         case 0x3f: /* fminv */
7575             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7576             break;
7577         default:
7578             g_assert_not_reached();
7579         }
7580         return tcg_res;
7581     }
7582 }
7583 
7584 /* AdvSIMD across lanes
7585  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7586  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7587  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7588  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7589  */
7590 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7591 {
7592     int rd = extract32(insn, 0, 5);
7593     int rn = extract32(insn, 5, 5);
7594     int size = extract32(insn, 22, 2);
7595     int opcode = extract32(insn, 12, 5);
7596     bool is_q = extract32(insn, 30, 1);
7597     bool is_u = extract32(insn, 29, 1);
7598     bool is_fp = false;
7599     bool is_min = false;
7600     int esize;
7601     int elements;
7602     int i;
7603     TCGv_i64 tcg_res, tcg_elt;
7604 
7605     switch (opcode) {
7606     case 0x1b: /* ADDV */
7607         if (is_u) {
7608             unallocated_encoding(s);
7609             return;
7610         }
7611         /* fall through */
7612     case 0x3: /* SADDLV, UADDLV */
7613     case 0xa: /* SMAXV, UMAXV */
7614     case 0x1a: /* SMINV, UMINV */
7615         if (size == 3 || (size == 2 && !is_q)) {
7616             unallocated_encoding(s);
7617             return;
7618         }
7619         break;
7620     case 0xc: /* FMAXNMV, FMINNMV */
7621     case 0xf: /* FMAXV, FMINV */
7622         /* Bit 1 of size field encodes min vs max and the actual size
7623          * depends on the encoding of the U bit. If not set (and FP16
7624          * enabled) then we do half-precision float instead of single
7625          * precision.
7626          */
7627         is_min = extract32(size, 1, 1);
7628         is_fp = true;
7629         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7630             size = 1;
7631         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7632             unallocated_encoding(s);
7633             return;
7634         } else {
7635             size = 2;
7636         }
7637         break;
7638     default:
7639         unallocated_encoding(s);
7640         return;
7641     }
7642 
7643     if (!fp_access_check(s)) {
7644         return;
7645     }
7646 
7647     esize = 8 << size;
7648     elements = (is_q ? 128 : 64) / esize;
7649 
7650     tcg_res = tcg_temp_new_i64();
7651     tcg_elt = tcg_temp_new_i64();
7652 
7653     /* These instructions operate across all lanes of a vector
7654      * to produce a single result. We can guarantee that a 64
7655      * bit intermediate is sufficient:
7656      *  + for [US]ADDLV the maximum element size is 32 bits, and
7657      *    the result type is 64 bits
7658      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7659      *    same as the element size, which is 32 bits at most
7660      * For the integer operations we can choose to work at 64
7661      * or 32 bits and truncate at the end; for simplicity
7662      * we use 64 bits always. The floating point
7663      * ops do require 32 bit intermediates, though.
7664      */
7665     if (!is_fp) {
7666         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7667 
7668         for (i = 1; i < elements; i++) {
7669             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7670 
7671             switch (opcode) {
7672             case 0x03: /* SADDLV / UADDLV */
7673             case 0x1b: /* ADDV */
7674                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7675                 break;
7676             case 0x0a: /* SMAXV / UMAXV */
7677                 if (is_u) {
7678                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7679                 } else {
7680                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7681                 }
7682                 break;
7683             case 0x1a: /* SMINV / UMINV */
7684                 if (is_u) {
7685                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7686                 } else {
7687                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7688                 }
7689                 break;
7690             default:
7691                 g_assert_not_reached();
7692             }
7693 
7694         }
7695     } else {
7696         /* Floating point vector reduction ops which work across 32
7697          * bit (single) or 16 bit (half-precision) intermediates.
7698          * Note that correct NaN propagation requires that we do these
7699          * operations in exactly the order specified by the pseudocode.
7700          */
7701         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7702         int fpopcode = opcode | is_min << 4 | is_u << 5;
7703         int vmap = (1 << elements) - 1;
7704         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7705                                              (is_q ? 128 : 64), vmap, fpst);
7706         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7707     }
7708 
7709     /* Now truncate the result to the width required for the final output */
7710     if (opcode == 0x03) {
7711         /* SADDLV, UADDLV: result is 2*esize */
7712         size++;
7713     }
7714 
7715     switch (size) {
7716     case 0:
7717         tcg_gen_ext8u_i64(tcg_res, tcg_res);
7718         break;
7719     case 1:
7720         tcg_gen_ext16u_i64(tcg_res, tcg_res);
7721         break;
7722     case 2:
7723         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7724         break;
7725     case 3:
7726         break;
7727     default:
7728         g_assert_not_reached();
7729     }
7730 
7731     write_fp_dreg(s, rd, tcg_res);
7732 }
7733 
7734 /* DUP (Element, Vector)
7735  *
7736  *  31  30   29              21 20    16 15        10  9    5 4    0
7737  * +---+---+-------------------+--------+-------------+------+------+
7738  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7739  * +---+---+-------------------+--------+-------------+------+------+
7740  *
7741  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7742  */
7743 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7744                              int imm5)
7745 {
7746     int size = ctz32(imm5);
7747     int index;
7748 
7749     if (size > 3 || (size == 3 && !is_q)) {
7750         unallocated_encoding(s);
7751         return;
7752     }
7753 
7754     if (!fp_access_check(s)) {
7755         return;
7756     }
7757 
7758     index = imm5 >> (size + 1);
7759     tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7760                          vec_reg_offset(s, rn, index, size),
7761                          is_q ? 16 : 8, vec_full_reg_size(s));
7762 }
7763 
7764 /* DUP (element, scalar)
7765  *  31                   21 20    16 15        10  9    5 4    0
7766  * +-----------------------+--------+-------------+------+------+
7767  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7768  * +-----------------------+--------+-------------+------+------+
7769  */
7770 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7771                               int imm5)
7772 {
7773     int size = ctz32(imm5);
7774     int index;
7775     TCGv_i64 tmp;
7776 
7777     if (size > 3) {
7778         unallocated_encoding(s);
7779         return;
7780     }
7781 
7782     if (!fp_access_check(s)) {
7783         return;
7784     }
7785 
7786     index = imm5 >> (size + 1);
7787 
7788     /* This instruction just extracts the specified element and
7789      * zero-extends it into the bottom of the destination register.
7790      */
7791     tmp = tcg_temp_new_i64();
7792     read_vec_element(s, tmp, rn, index, size);
7793     write_fp_dreg(s, rd, tmp);
7794 }
7795 
7796 /* DUP (General)
7797  *
7798  *  31  30   29              21 20    16 15        10  9    5 4    0
7799  * +---+---+-------------------+--------+-------------+------+------+
7800  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
7801  * +---+---+-------------------+--------+-------------+------+------+
7802  *
7803  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7804  */
7805 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7806                              int imm5)
7807 {
7808     int size = ctz32(imm5);
7809     uint32_t dofs, oprsz, maxsz;
7810 
7811     if (size > 3 || ((size == 3) && !is_q)) {
7812         unallocated_encoding(s);
7813         return;
7814     }
7815 
7816     if (!fp_access_check(s)) {
7817         return;
7818     }
7819 
7820     dofs = vec_full_reg_offset(s, rd);
7821     oprsz = is_q ? 16 : 8;
7822     maxsz = vec_full_reg_size(s);
7823 
7824     tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7825 }
7826 
7827 /* INS (Element)
7828  *
7829  *  31                   21 20    16 15  14    11  10 9    5 4    0
7830  * +-----------------------+--------+------------+---+------+------+
7831  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
7832  * +-----------------------+--------+------------+---+------+------+
7833  *
7834  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7835  * index: encoded in imm5<4:size+1>
7836  */
7837 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7838                              int imm4, int imm5)
7839 {
7840     int size = ctz32(imm5);
7841     int src_index, dst_index;
7842     TCGv_i64 tmp;
7843 
7844     if (size > 3) {
7845         unallocated_encoding(s);
7846         return;
7847     }
7848 
7849     if (!fp_access_check(s)) {
7850         return;
7851     }
7852 
7853     dst_index = extract32(imm5, 1+size, 5);
7854     src_index = extract32(imm4, size, 4);
7855 
7856     tmp = tcg_temp_new_i64();
7857 
7858     read_vec_element(s, tmp, rn, src_index, size);
7859     write_vec_element(s, tmp, rd, dst_index, size);
7860 
7861     /* INS is considered a 128-bit write for SVE. */
7862     clear_vec_high(s, true, rd);
7863 }
7864 
7865 
7866 /* INS (General)
7867  *
7868  *  31                   21 20    16 15        10  9    5 4    0
7869  * +-----------------------+--------+-------------+------+------+
7870  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
7871  * +-----------------------+--------+-------------+------+------+
7872  *
7873  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7874  * index: encoded in imm5<4:size+1>
7875  */
7876 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7877 {
7878     int size = ctz32(imm5);
7879     int idx;
7880 
7881     if (size > 3) {
7882         unallocated_encoding(s);
7883         return;
7884     }
7885 
7886     if (!fp_access_check(s)) {
7887         return;
7888     }
7889 
7890     idx = extract32(imm5, 1 + size, 4 - size);
7891     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7892 
7893     /* INS is considered a 128-bit write for SVE. */
7894     clear_vec_high(s, true, rd);
7895 }
7896 
7897 /*
7898  * UMOV (General)
7899  * SMOV (General)
7900  *
7901  *  31  30   29              21 20    16 15    12   10 9    5 4    0
7902  * +---+---+-------------------+--------+-------------+------+------+
7903  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
7904  * +---+---+-------------------+--------+-------------+------+------+
7905  *
7906  * U: unsigned when set
7907  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7908  */
7909 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7910                                   int rn, int rd, int imm5)
7911 {
7912     int size = ctz32(imm5);
7913     int element;
7914     TCGv_i64 tcg_rd;
7915 
7916     /* Check for UnallocatedEncodings */
7917     if (is_signed) {
7918         if (size > 2 || (size == 2 && !is_q)) {
7919             unallocated_encoding(s);
7920             return;
7921         }
7922     } else {
7923         if (size > 3
7924             || (size < 3 && is_q)
7925             || (size == 3 && !is_q)) {
7926             unallocated_encoding(s);
7927             return;
7928         }
7929     }
7930 
7931     if (!fp_access_check(s)) {
7932         return;
7933     }
7934 
7935     element = extract32(imm5, 1+size, 4);
7936 
7937     tcg_rd = cpu_reg(s, rd);
7938     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7939     if (is_signed && !is_q) {
7940         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7941     }
7942 }
7943 
7944 /* AdvSIMD copy
7945  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
7946  * +---+---+----+-----------------+------+---+------+---+------+------+
7947  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7948  * +---+---+----+-----------------+------+---+------+---+------+------+
7949  */
7950 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7951 {
7952     int rd = extract32(insn, 0, 5);
7953     int rn = extract32(insn, 5, 5);
7954     int imm4 = extract32(insn, 11, 4);
7955     int op = extract32(insn, 29, 1);
7956     int is_q = extract32(insn, 30, 1);
7957     int imm5 = extract32(insn, 16, 5);
7958 
7959     if (op) {
7960         if (is_q) {
7961             /* INS (element) */
7962             handle_simd_inse(s, rd, rn, imm4, imm5);
7963         } else {
7964             unallocated_encoding(s);
7965         }
7966     } else {
7967         switch (imm4) {
7968         case 0:
7969             /* DUP (element - vector) */
7970             handle_simd_dupe(s, is_q, rd, rn, imm5);
7971             break;
7972         case 1:
7973             /* DUP (general) */
7974             handle_simd_dupg(s, is_q, rd, rn, imm5);
7975             break;
7976         case 3:
7977             if (is_q) {
7978                 /* INS (general) */
7979                 handle_simd_insg(s, rd, rn, imm5);
7980             } else {
7981                 unallocated_encoding(s);
7982             }
7983             break;
7984         case 5:
7985         case 7:
7986             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
7987             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
7988             break;
7989         default:
7990             unallocated_encoding(s);
7991             break;
7992         }
7993     }
7994 }
7995 
7996 /* AdvSIMD modified immediate
7997  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
7998  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7999  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
8000  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8001  *
8002  * There are a number of operations that can be carried out here:
8003  *   MOVI - move (shifted) imm into register
8004  *   MVNI - move inverted (shifted) imm into register
8005  *   ORR  - bitwise OR of (shifted) imm with register
8006  *   BIC  - bitwise clear of (shifted) imm with register
8007  * With ARMv8.2 we also have:
8008  *   FMOV half-precision
8009  */
8010 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
8011 {
8012     int rd = extract32(insn, 0, 5);
8013     int cmode = extract32(insn, 12, 4);
8014     int o2 = extract32(insn, 11, 1);
8015     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
8016     bool is_neg = extract32(insn, 29, 1);
8017     bool is_q = extract32(insn, 30, 1);
8018     uint64_t imm = 0;
8019 
8020     if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
8021         /* Check for FMOV (vector, immediate) - half-precision */
8022         if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
8023             unallocated_encoding(s);
8024             return;
8025         }
8026     }
8027 
8028     if (!fp_access_check(s)) {
8029         return;
8030     }
8031 
8032     if (cmode == 15 && o2 && !is_neg) {
8033         /* FMOV (vector, immediate) - half-precision */
8034         imm = vfp_expand_imm(MO_16, abcdefgh);
8035         /* now duplicate across the lanes */
8036         imm = dup_const(MO_16, imm);
8037     } else {
8038         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
8039     }
8040 
8041     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
8042         /* MOVI or MVNI, with MVNI negation handled above.  */
8043         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
8044                              vec_full_reg_size(s), imm);
8045     } else {
8046         /* ORR or BIC, with BIC negation to AND handled above.  */
8047         if (is_neg) {
8048             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
8049         } else {
8050             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
8051         }
8052     }
8053 }
8054 
8055 /* AdvSIMD scalar copy
8056  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
8057  * +-----+----+-----------------+------+---+------+---+------+------+
8058  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
8059  * +-----+----+-----------------+------+---+------+---+------+------+
8060  */
8061 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
8062 {
8063     int rd = extract32(insn, 0, 5);
8064     int rn = extract32(insn, 5, 5);
8065     int imm4 = extract32(insn, 11, 4);
8066     int imm5 = extract32(insn, 16, 5);
8067     int op = extract32(insn, 29, 1);
8068 
8069     if (op != 0 || imm4 != 0) {
8070         unallocated_encoding(s);
8071         return;
8072     }
8073 
8074     /* DUP (element, scalar) */
8075     handle_simd_dupes(s, rd, rn, imm5);
8076 }
8077 
8078 /* AdvSIMD scalar pairwise
8079  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
8080  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8081  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
8082  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8083  */
8084 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
8085 {
8086     int u = extract32(insn, 29, 1);
8087     int size = extract32(insn, 22, 2);
8088     int opcode = extract32(insn, 12, 5);
8089     int rn = extract32(insn, 5, 5);
8090     int rd = extract32(insn, 0, 5);
8091     TCGv_ptr fpst;
8092 
8093     /* For some ops (the FP ones), size[1] is part of the encoding.
8094      * For ADDP strictly it is not but size[1] is always 1 for valid
8095      * encodings.
8096      */
8097     opcode |= (extract32(size, 1, 1) << 5);
8098 
8099     switch (opcode) {
8100     case 0x3b: /* ADDP */
8101         if (u || size != 3) {
8102             unallocated_encoding(s);
8103             return;
8104         }
8105         if (!fp_access_check(s)) {
8106             return;
8107         }
8108 
8109         fpst = NULL;
8110         break;
8111     case 0xc: /* FMAXNMP */
8112     case 0xd: /* FADDP */
8113     case 0xf: /* FMAXP */
8114     case 0x2c: /* FMINNMP */
8115     case 0x2f: /* FMINP */
8116         /* FP op, size[0] is 32 or 64 bit*/
8117         if (!u) {
8118             if (!dc_isar_feature(aa64_fp16, s)) {
8119                 unallocated_encoding(s);
8120                 return;
8121             } else {
8122                 size = MO_16;
8123             }
8124         } else {
8125             size = extract32(size, 0, 1) ? MO_64 : MO_32;
8126         }
8127 
8128         if (!fp_access_check(s)) {
8129             return;
8130         }
8131 
8132         fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8133         break;
8134     default:
8135         unallocated_encoding(s);
8136         return;
8137     }
8138 
8139     if (size == MO_64) {
8140         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8141         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8142         TCGv_i64 tcg_res = tcg_temp_new_i64();
8143 
8144         read_vec_element(s, tcg_op1, rn, 0, MO_64);
8145         read_vec_element(s, tcg_op2, rn, 1, MO_64);
8146 
8147         switch (opcode) {
8148         case 0x3b: /* ADDP */
8149             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
8150             break;
8151         case 0xc: /* FMAXNMP */
8152             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8153             break;
8154         case 0xd: /* FADDP */
8155             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8156             break;
8157         case 0xf: /* FMAXP */
8158             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8159             break;
8160         case 0x2c: /* FMINNMP */
8161             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8162             break;
8163         case 0x2f: /* FMINP */
8164             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8165             break;
8166         default:
8167             g_assert_not_reached();
8168         }
8169 
8170         write_fp_dreg(s, rd, tcg_res);
8171     } else {
8172         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8173         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8174         TCGv_i32 tcg_res = tcg_temp_new_i32();
8175 
8176         read_vec_element_i32(s, tcg_op1, rn, 0, size);
8177         read_vec_element_i32(s, tcg_op2, rn, 1, size);
8178 
8179         if (size == MO_16) {
8180             switch (opcode) {
8181             case 0xc: /* FMAXNMP */
8182                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8183                 break;
8184             case 0xd: /* FADDP */
8185                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
8186                 break;
8187             case 0xf: /* FMAXP */
8188                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
8189                 break;
8190             case 0x2c: /* FMINNMP */
8191                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8192                 break;
8193             case 0x2f: /* FMINP */
8194                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
8195                 break;
8196             default:
8197                 g_assert_not_reached();
8198             }
8199         } else {
8200             switch (opcode) {
8201             case 0xc: /* FMAXNMP */
8202                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8203                 break;
8204             case 0xd: /* FADDP */
8205                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8206                 break;
8207             case 0xf: /* FMAXP */
8208                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8209                 break;
8210             case 0x2c: /* FMINNMP */
8211                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8212                 break;
8213             case 0x2f: /* FMINP */
8214                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8215                 break;
8216             default:
8217                 g_assert_not_reached();
8218             }
8219         }
8220 
8221         write_fp_sreg(s, rd, tcg_res);
8222     }
8223 }
8224 
8225 /*
8226  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8227  *
8228  * This code is handles the common shifting code and is used by both
8229  * the vector and scalar code.
8230  */
8231 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8232                                     TCGv_i64 tcg_rnd, bool accumulate,
8233                                     bool is_u, int size, int shift)
8234 {
8235     bool extended_result = false;
8236     bool round = tcg_rnd != NULL;
8237     int ext_lshift = 0;
8238     TCGv_i64 tcg_src_hi;
8239 
8240     if (round && size == 3) {
8241         extended_result = true;
8242         ext_lshift = 64 - shift;
8243         tcg_src_hi = tcg_temp_new_i64();
8244     } else if (shift == 64) {
8245         if (!accumulate && is_u) {
8246             /* result is zero */
8247             tcg_gen_movi_i64(tcg_res, 0);
8248             return;
8249         }
8250     }
8251 
8252     /* Deal with the rounding step */
8253     if (round) {
8254         if (extended_result) {
8255             TCGv_i64 tcg_zero = tcg_constant_i64(0);
8256             if (!is_u) {
8257                 /* take care of sign extending tcg_res */
8258                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8259                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8260                                  tcg_src, tcg_src_hi,
8261                                  tcg_rnd, tcg_zero);
8262             } else {
8263                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8264                                  tcg_src, tcg_zero,
8265                                  tcg_rnd, tcg_zero);
8266             }
8267         } else {
8268             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8269         }
8270     }
8271 
8272     /* Now do the shift right */
8273     if (round && extended_result) {
8274         /* extended case, >64 bit precision required */
8275         if (ext_lshift == 0) {
8276             /* special case, only high bits matter */
8277             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8278         } else {
8279             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8280             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8281             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8282         }
8283     } else {
8284         if (is_u) {
8285             if (shift == 64) {
8286                 /* essentially shifting in 64 zeros */
8287                 tcg_gen_movi_i64(tcg_src, 0);
8288             } else {
8289                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8290             }
8291         } else {
8292             if (shift == 64) {
8293                 /* effectively extending the sign-bit */
8294                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8295             } else {
8296                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8297             }
8298         }
8299     }
8300 
8301     if (accumulate) {
8302         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8303     } else {
8304         tcg_gen_mov_i64(tcg_res, tcg_src);
8305     }
8306 }
8307 
8308 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8309 static void handle_scalar_simd_shri(DisasContext *s,
8310                                     bool is_u, int immh, int immb,
8311                                     int opcode, int rn, int rd)
8312 {
8313     const int size = 3;
8314     int immhb = immh << 3 | immb;
8315     int shift = 2 * (8 << size) - immhb;
8316     bool accumulate = false;
8317     bool round = false;
8318     bool insert = false;
8319     TCGv_i64 tcg_rn;
8320     TCGv_i64 tcg_rd;
8321     TCGv_i64 tcg_round;
8322 
8323     if (!extract32(immh, 3, 1)) {
8324         unallocated_encoding(s);
8325         return;
8326     }
8327 
8328     if (!fp_access_check(s)) {
8329         return;
8330     }
8331 
8332     switch (opcode) {
8333     case 0x02: /* SSRA / USRA (accumulate) */
8334         accumulate = true;
8335         break;
8336     case 0x04: /* SRSHR / URSHR (rounding) */
8337         round = true;
8338         break;
8339     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8340         accumulate = round = true;
8341         break;
8342     case 0x08: /* SRI */
8343         insert = true;
8344         break;
8345     }
8346 
8347     if (round) {
8348         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8349     } else {
8350         tcg_round = NULL;
8351     }
8352 
8353     tcg_rn = read_fp_dreg(s, rn);
8354     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8355 
8356     if (insert) {
8357         /* shift count same as element size is valid but does nothing;
8358          * special case to avoid potential shift by 64.
8359          */
8360         int esize = 8 << size;
8361         if (shift != esize) {
8362             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8363             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8364         }
8365     } else {
8366         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8367                                 accumulate, is_u, size, shift);
8368     }
8369 
8370     write_fp_dreg(s, rd, tcg_rd);
8371 }
8372 
8373 /* SHL/SLI - Scalar shift left */
8374 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8375                                     int immh, int immb, int opcode,
8376                                     int rn, int rd)
8377 {
8378     int size = 32 - clz32(immh) - 1;
8379     int immhb = immh << 3 | immb;
8380     int shift = immhb - (8 << size);
8381     TCGv_i64 tcg_rn;
8382     TCGv_i64 tcg_rd;
8383 
8384     if (!extract32(immh, 3, 1)) {
8385         unallocated_encoding(s);
8386         return;
8387     }
8388 
8389     if (!fp_access_check(s)) {
8390         return;
8391     }
8392 
8393     tcg_rn = read_fp_dreg(s, rn);
8394     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8395 
8396     if (insert) {
8397         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8398     } else {
8399         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8400     }
8401 
8402     write_fp_dreg(s, rd, tcg_rd);
8403 }
8404 
8405 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8406  * (signed/unsigned) narrowing */
8407 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8408                                    bool is_u_shift, bool is_u_narrow,
8409                                    int immh, int immb, int opcode,
8410                                    int rn, int rd)
8411 {
8412     int immhb = immh << 3 | immb;
8413     int size = 32 - clz32(immh) - 1;
8414     int esize = 8 << size;
8415     int shift = (2 * esize) - immhb;
8416     int elements = is_scalar ? 1 : (64 / esize);
8417     bool round = extract32(opcode, 0, 1);
8418     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8419     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8420     TCGv_i32 tcg_rd_narrowed;
8421     TCGv_i64 tcg_final;
8422 
8423     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8424         { gen_helper_neon_narrow_sat_s8,
8425           gen_helper_neon_unarrow_sat8 },
8426         { gen_helper_neon_narrow_sat_s16,
8427           gen_helper_neon_unarrow_sat16 },
8428         { gen_helper_neon_narrow_sat_s32,
8429           gen_helper_neon_unarrow_sat32 },
8430         { NULL, NULL },
8431     };
8432     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8433         gen_helper_neon_narrow_sat_u8,
8434         gen_helper_neon_narrow_sat_u16,
8435         gen_helper_neon_narrow_sat_u32,
8436         NULL
8437     };
8438     NeonGenNarrowEnvFn *narrowfn;
8439 
8440     int i;
8441 
8442     assert(size < 4);
8443 
8444     if (extract32(immh, 3, 1)) {
8445         unallocated_encoding(s);
8446         return;
8447     }
8448 
8449     if (!fp_access_check(s)) {
8450         return;
8451     }
8452 
8453     if (is_u_shift) {
8454         narrowfn = unsigned_narrow_fns[size];
8455     } else {
8456         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8457     }
8458 
8459     tcg_rn = tcg_temp_new_i64();
8460     tcg_rd = tcg_temp_new_i64();
8461     tcg_rd_narrowed = tcg_temp_new_i32();
8462     tcg_final = tcg_const_i64(0);
8463 
8464     if (round) {
8465         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8466     } else {
8467         tcg_round = NULL;
8468     }
8469 
8470     for (i = 0; i < elements; i++) {
8471         read_vec_element(s, tcg_rn, rn, i, ldop);
8472         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8473                                 false, is_u_shift, size+1, shift);
8474         narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8475         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8476         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8477     }
8478 
8479     if (!is_q) {
8480         write_vec_element(s, tcg_final, rd, 0, MO_64);
8481     } else {
8482         write_vec_element(s, tcg_final, rd, 1, MO_64);
8483     }
8484     clear_vec_high(s, is_q, rd);
8485 }
8486 
8487 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8488 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8489                              bool src_unsigned, bool dst_unsigned,
8490                              int immh, int immb, int rn, int rd)
8491 {
8492     int immhb = immh << 3 | immb;
8493     int size = 32 - clz32(immh) - 1;
8494     int shift = immhb - (8 << size);
8495     int pass;
8496 
8497     assert(immh != 0);
8498     assert(!(scalar && is_q));
8499 
8500     if (!scalar) {
8501         if (!is_q && extract32(immh, 3, 1)) {
8502             unallocated_encoding(s);
8503             return;
8504         }
8505 
8506         /* Since we use the variable-shift helpers we must
8507          * replicate the shift count into each element of
8508          * the tcg_shift value.
8509          */
8510         switch (size) {
8511         case 0:
8512             shift |= shift << 8;
8513             /* fall through */
8514         case 1:
8515             shift |= shift << 16;
8516             break;
8517         case 2:
8518         case 3:
8519             break;
8520         default:
8521             g_assert_not_reached();
8522         }
8523     }
8524 
8525     if (!fp_access_check(s)) {
8526         return;
8527     }
8528 
8529     if (size == 3) {
8530         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8531         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8532             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8533             { NULL, gen_helper_neon_qshl_u64 },
8534         };
8535         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8536         int maxpass = is_q ? 2 : 1;
8537 
8538         for (pass = 0; pass < maxpass; pass++) {
8539             TCGv_i64 tcg_op = tcg_temp_new_i64();
8540 
8541             read_vec_element(s, tcg_op, rn, pass, MO_64);
8542             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8543             write_vec_element(s, tcg_op, rd, pass, MO_64);
8544         }
8545         clear_vec_high(s, is_q, rd);
8546     } else {
8547         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8548         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8549             {
8550                 { gen_helper_neon_qshl_s8,
8551                   gen_helper_neon_qshl_s16,
8552                   gen_helper_neon_qshl_s32 },
8553                 { gen_helper_neon_qshlu_s8,
8554                   gen_helper_neon_qshlu_s16,
8555                   gen_helper_neon_qshlu_s32 }
8556             }, {
8557                 { NULL, NULL, NULL },
8558                 { gen_helper_neon_qshl_u8,
8559                   gen_helper_neon_qshl_u16,
8560                   gen_helper_neon_qshl_u32 }
8561             }
8562         };
8563         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8564         MemOp memop = scalar ? size : MO_32;
8565         int maxpass = scalar ? 1 : is_q ? 4 : 2;
8566 
8567         for (pass = 0; pass < maxpass; pass++) {
8568             TCGv_i32 tcg_op = tcg_temp_new_i32();
8569 
8570             read_vec_element_i32(s, tcg_op, rn, pass, memop);
8571             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8572             if (scalar) {
8573                 switch (size) {
8574                 case 0:
8575                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
8576                     break;
8577                 case 1:
8578                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8579                     break;
8580                 case 2:
8581                     break;
8582                 default:
8583                     g_assert_not_reached();
8584                 }
8585                 write_fp_sreg(s, rd, tcg_op);
8586             } else {
8587                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8588             }
8589         }
8590 
8591         if (!scalar) {
8592             clear_vec_high(s, is_q, rd);
8593         }
8594     }
8595 }
8596 
8597 /* Common vector code for handling integer to FP conversion */
8598 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8599                                    int elements, int is_signed,
8600                                    int fracbits, int size)
8601 {
8602     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8603     TCGv_i32 tcg_shift = NULL;
8604 
8605     MemOp mop = size | (is_signed ? MO_SIGN : 0);
8606     int pass;
8607 
8608     if (fracbits || size == MO_64) {
8609         tcg_shift = tcg_constant_i32(fracbits);
8610     }
8611 
8612     if (size == MO_64) {
8613         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8614         TCGv_i64 tcg_double = tcg_temp_new_i64();
8615 
8616         for (pass = 0; pass < elements; pass++) {
8617             read_vec_element(s, tcg_int64, rn, pass, mop);
8618 
8619             if (is_signed) {
8620                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8621                                      tcg_shift, tcg_fpst);
8622             } else {
8623                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8624                                      tcg_shift, tcg_fpst);
8625             }
8626             if (elements == 1) {
8627                 write_fp_dreg(s, rd, tcg_double);
8628             } else {
8629                 write_vec_element(s, tcg_double, rd, pass, MO_64);
8630             }
8631         }
8632     } else {
8633         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8634         TCGv_i32 tcg_float = tcg_temp_new_i32();
8635 
8636         for (pass = 0; pass < elements; pass++) {
8637             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8638 
8639             switch (size) {
8640             case MO_32:
8641                 if (fracbits) {
8642                     if (is_signed) {
8643                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
8644                                              tcg_shift, tcg_fpst);
8645                     } else {
8646                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
8647                                              tcg_shift, tcg_fpst);
8648                     }
8649                 } else {
8650                     if (is_signed) {
8651                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8652                     } else {
8653                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8654                     }
8655                 }
8656                 break;
8657             case MO_16:
8658                 if (fracbits) {
8659                     if (is_signed) {
8660                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8661                                              tcg_shift, tcg_fpst);
8662                     } else {
8663                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8664                                              tcg_shift, tcg_fpst);
8665                     }
8666                 } else {
8667                     if (is_signed) {
8668                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8669                     } else {
8670                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8671                     }
8672                 }
8673                 break;
8674             default:
8675                 g_assert_not_reached();
8676             }
8677 
8678             if (elements == 1) {
8679                 write_fp_sreg(s, rd, tcg_float);
8680             } else {
8681                 write_vec_element_i32(s, tcg_float, rd, pass, size);
8682             }
8683         }
8684     }
8685 
8686     clear_vec_high(s, elements << size == 16, rd);
8687 }
8688 
8689 /* UCVTF/SCVTF - Integer to FP conversion */
8690 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8691                                          bool is_q, bool is_u,
8692                                          int immh, int immb, int opcode,
8693                                          int rn, int rd)
8694 {
8695     int size, elements, fracbits;
8696     int immhb = immh << 3 | immb;
8697 
8698     if (immh & 8) {
8699         size = MO_64;
8700         if (!is_scalar && !is_q) {
8701             unallocated_encoding(s);
8702             return;
8703         }
8704     } else if (immh & 4) {
8705         size = MO_32;
8706     } else if (immh & 2) {
8707         size = MO_16;
8708         if (!dc_isar_feature(aa64_fp16, s)) {
8709             unallocated_encoding(s);
8710             return;
8711         }
8712     } else {
8713         /* immh == 0 would be a failure of the decode logic */
8714         g_assert(immh == 1);
8715         unallocated_encoding(s);
8716         return;
8717     }
8718 
8719     if (is_scalar) {
8720         elements = 1;
8721     } else {
8722         elements = (8 << is_q) >> size;
8723     }
8724     fracbits = (16 << size) - immhb;
8725 
8726     if (!fp_access_check(s)) {
8727         return;
8728     }
8729 
8730     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8731 }
8732 
8733 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8734 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8735                                          bool is_q, bool is_u,
8736                                          int immh, int immb, int rn, int rd)
8737 {
8738     int immhb = immh << 3 | immb;
8739     int pass, size, fracbits;
8740     TCGv_ptr tcg_fpstatus;
8741     TCGv_i32 tcg_rmode, tcg_shift;
8742 
8743     if (immh & 0x8) {
8744         size = MO_64;
8745         if (!is_scalar && !is_q) {
8746             unallocated_encoding(s);
8747             return;
8748         }
8749     } else if (immh & 0x4) {
8750         size = MO_32;
8751     } else if (immh & 0x2) {
8752         size = MO_16;
8753         if (!dc_isar_feature(aa64_fp16, s)) {
8754             unallocated_encoding(s);
8755             return;
8756         }
8757     } else {
8758         /* Should have split out AdvSIMD modified immediate earlier.  */
8759         assert(immh == 1);
8760         unallocated_encoding(s);
8761         return;
8762     }
8763 
8764     if (!fp_access_check(s)) {
8765         return;
8766     }
8767 
8768     assert(!(is_scalar && is_q));
8769 
8770     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8771     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
8772     fracbits = (16 << size) - immhb;
8773     tcg_shift = tcg_constant_i32(fracbits);
8774 
8775     if (size == MO_64) {
8776         int maxpass = is_scalar ? 1 : 2;
8777 
8778         for (pass = 0; pass < maxpass; pass++) {
8779             TCGv_i64 tcg_op = tcg_temp_new_i64();
8780 
8781             read_vec_element(s, tcg_op, rn, pass, MO_64);
8782             if (is_u) {
8783                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8784             } else {
8785                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8786             }
8787             write_vec_element(s, tcg_op, rd, pass, MO_64);
8788         }
8789         clear_vec_high(s, is_q, rd);
8790     } else {
8791         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8792         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8793 
8794         switch (size) {
8795         case MO_16:
8796             if (is_u) {
8797                 fn = gen_helper_vfp_touhh;
8798             } else {
8799                 fn = gen_helper_vfp_toshh;
8800             }
8801             break;
8802         case MO_32:
8803             if (is_u) {
8804                 fn = gen_helper_vfp_touls;
8805             } else {
8806                 fn = gen_helper_vfp_tosls;
8807             }
8808             break;
8809         default:
8810             g_assert_not_reached();
8811         }
8812 
8813         for (pass = 0; pass < maxpass; pass++) {
8814             TCGv_i32 tcg_op = tcg_temp_new_i32();
8815 
8816             read_vec_element_i32(s, tcg_op, rn, pass, size);
8817             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8818             if (is_scalar) {
8819                 write_fp_sreg(s, rd, tcg_op);
8820             } else {
8821                 write_vec_element_i32(s, tcg_op, rd, pass, size);
8822             }
8823         }
8824         if (!is_scalar) {
8825             clear_vec_high(s, is_q, rd);
8826         }
8827     }
8828 
8829     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
8830 }
8831 
8832 /* AdvSIMD scalar shift by immediate
8833  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
8834  * +-----+---+-------------+------+------+--------+---+------+------+
8835  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8836  * +-----+---+-------------+------+------+--------+---+------+------+
8837  *
8838  * This is the scalar version so it works on a fixed sized registers
8839  */
8840 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8841 {
8842     int rd = extract32(insn, 0, 5);
8843     int rn = extract32(insn, 5, 5);
8844     int opcode = extract32(insn, 11, 5);
8845     int immb = extract32(insn, 16, 3);
8846     int immh = extract32(insn, 19, 4);
8847     bool is_u = extract32(insn, 29, 1);
8848 
8849     if (immh == 0) {
8850         unallocated_encoding(s);
8851         return;
8852     }
8853 
8854     switch (opcode) {
8855     case 0x08: /* SRI */
8856         if (!is_u) {
8857             unallocated_encoding(s);
8858             return;
8859         }
8860         /* fall through */
8861     case 0x00: /* SSHR / USHR */
8862     case 0x02: /* SSRA / USRA */
8863     case 0x04: /* SRSHR / URSHR */
8864     case 0x06: /* SRSRA / URSRA */
8865         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8866         break;
8867     case 0x0a: /* SHL / SLI */
8868         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8869         break;
8870     case 0x1c: /* SCVTF, UCVTF */
8871         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8872                                      opcode, rn, rd);
8873         break;
8874     case 0x10: /* SQSHRUN, SQSHRUN2 */
8875     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8876         if (!is_u) {
8877             unallocated_encoding(s);
8878             return;
8879         }
8880         handle_vec_simd_sqshrn(s, true, false, false, true,
8881                                immh, immb, opcode, rn, rd);
8882         break;
8883     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8884     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8885         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8886                                immh, immb, opcode, rn, rd);
8887         break;
8888     case 0xc: /* SQSHLU */
8889         if (!is_u) {
8890             unallocated_encoding(s);
8891             return;
8892         }
8893         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8894         break;
8895     case 0xe: /* SQSHL, UQSHL */
8896         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8897         break;
8898     case 0x1f: /* FCVTZS, FCVTZU */
8899         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8900         break;
8901     default:
8902         unallocated_encoding(s);
8903         break;
8904     }
8905 }
8906 
8907 /* AdvSIMD scalar three different
8908  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
8909  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8910  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
8911  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8912  */
8913 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8914 {
8915     bool is_u = extract32(insn, 29, 1);
8916     int size = extract32(insn, 22, 2);
8917     int opcode = extract32(insn, 12, 4);
8918     int rm = extract32(insn, 16, 5);
8919     int rn = extract32(insn, 5, 5);
8920     int rd = extract32(insn, 0, 5);
8921 
8922     if (is_u) {
8923         unallocated_encoding(s);
8924         return;
8925     }
8926 
8927     switch (opcode) {
8928     case 0x9: /* SQDMLAL, SQDMLAL2 */
8929     case 0xb: /* SQDMLSL, SQDMLSL2 */
8930     case 0xd: /* SQDMULL, SQDMULL2 */
8931         if (size == 0 || size == 3) {
8932             unallocated_encoding(s);
8933             return;
8934         }
8935         break;
8936     default:
8937         unallocated_encoding(s);
8938         return;
8939     }
8940 
8941     if (!fp_access_check(s)) {
8942         return;
8943     }
8944 
8945     if (size == 2) {
8946         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8947         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8948         TCGv_i64 tcg_res = tcg_temp_new_i64();
8949 
8950         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8951         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8952 
8953         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8954         gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
8955 
8956         switch (opcode) {
8957         case 0xd: /* SQDMULL, SQDMULL2 */
8958             break;
8959         case 0xb: /* SQDMLSL, SQDMLSL2 */
8960             tcg_gen_neg_i64(tcg_res, tcg_res);
8961             /* fall through */
8962         case 0x9: /* SQDMLAL, SQDMLAL2 */
8963             read_vec_element(s, tcg_op1, rd, 0, MO_64);
8964             gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
8965                                               tcg_res, tcg_op1);
8966             break;
8967         default:
8968             g_assert_not_reached();
8969         }
8970 
8971         write_fp_dreg(s, rd, tcg_res);
8972     } else {
8973         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8974         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8975         TCGv_i64 tcg_res = tcg_temp_new_i64();
8976 
8977         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8978         gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
8979 
8980         switch (opcode) {
8981         case 0xd: /* SQDMULL, SQDMULL2 */
8982             break;
8983         case 0xb: /* SQDMLSL, SQDMLSL2 */
8984             gen_helper_neon_negl_u32(tcg_res, tcg_res);
8985             /* fall through */
8986         case 0x9: /* SQDMLAL, SQDMLAL2 */
8987         {
8988             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
8989             read_vec_element(s, tcg_op3, rd, 0, MO_32);
8990             gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
8991                                               tcg_res, tcg_op3);
8992             break;
8993         }
8994         default:
8995             g_assert_not_reached();
8996         }
8997 
8998         tcg_gen_ext32u_i64(tcg_res, tcg_res);
8999         write_fp_dreg(s, rd, tcg_res);
9000     }
9001 }
9002 
9003 static void handle_3same_64(DisasContext *s, int opcode, bool u,
9004                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
9005 {
9006     /* Handle 64x64->64 opcodes which are shared between the scalar
9007      * and vector 3-same groups. We cover every opcode where size == 3
9008      * is valid in either the three-reg-same (integer, not pairwise)
9009      * or scalar-three-reg-same groups.
9010      */
9011     TCGCond cond;
9012 
9013     switch (opcode) {
9014     case 0x1: /* SQADD */
9015         if (u) {
9016             gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9017         } else {
9018             gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9019         }
9020         break;
9021     case 0x5: /* SQSUB */
9022         if (u) {
9023             gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9024         } else {
9025             gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9026         }
9027         break;
9028     case 0x6: /* CMGT, CMHI */
9029         /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
9030          * We implement this using setcond (test) and then negating.
9031          */
9032         cond = u ? TCG_COND_GTU : TCG_COND_GT;
9033     do_cmop:
9034         tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
9035         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9036         break;
9037     case 0x7: /* CMGE, CMHS */
9038         cond = u ? TCG_COND_GEU : TCG_COND_GE;
9039         goto do_cmop;
9040     case 0x11: /* CMTST, CMEQ */
9041         if (u) {
9042             cond = TCG_COND_EQ;
9043             goto do_cmop;
9044         }
9045         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
9046         break;
9047     case 0x8: /* SSHL, USHL */
9048         if (u) {
9049             gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
9050         } else {
9051             gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
9052         }
9053         break;
9054     case 0x9: /* SQSHL, UQSHL */
9055         if (u) {
9056             gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9057         } else {
9058             gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9059         }
9060         break;
9061     case 0xa: /* SRSHL, URSHL */
9062         if (u) {
9063             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
9064         } else {
9065             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
9066         }
9067         break;
9068     case 0xb: /* SQRSHL, UQRSHL */
9069         if (u) {
9070             gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9071         } else {
9072             gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9073         }
9074         break;
9075     case 0x10: /* ADD, SUB */
9076         if (u) {
9077             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
9078         } else {
9079             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
9080         }
9081         break;
9082     default:
9083         g_assert_not_reached();
9084     }
9085 }
9086 
9087 /* Handle the 3-same-operands float operations; shared by the scalar
9088  * and vector encodings. The caller must filter out any encodings
9089  * not allocated for the encoding it is dealing with.
9090  */
9091 static void handle_3same_float(DisasContext *s, int size, int elements,
9092                                int fpopcode, int rd, int rn, int rm)
9093 {
9094     int pass;
9095     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9096 
9097     for (pass = 0; pass < elements; pass++) {
9098         if (size) {
9099             /* Double */
9100             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9101             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9102             TCGv_i64 tcg_res = tcg_temp_new_i64();
9103 
9104             read_vec_element(s, tcg_op1, rn, pass, MO_64);
9105             read_vec_element(s, tcg_op2, rm, pass, MO_64);
9106 
9107             switch (fpopcode) {
9108             case 0x39: /* FMLS */
9109                 /* As usual for ARM, separate negation for fused multiply-add */
9110                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
9111                 /* fall through */
9112             case 0x19: /* FMLA */
9113                 read_vec_element(s, tcg_res, rd, pass, MO_64);
9114                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
9115                                        tcg_res, fpst);
9116                 break;
9117             case 0x18: /* FMAXNM */
9118                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9119                 break;
9120             case 0x1a: /* FADD */
9121                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
9122                 break;
9123             case 0x1b: /* FMULX */
9124                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
9125                 break;
9126             case 0x1c: /* FCMEQ */
9127                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9128                 break;
9129             case 0x1e: /* FMAX */
9130                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
9131                 break;
9132             case 0x1f: /* FRECPS */
9133                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9134                 break;
9135             case 0x38: /* FMINNM */
9136                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9137                 break;
9138             case 0x3a: /* FSUB */
9139                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9140                 break;
9141             case 0x3e: /* FMIN */
9142                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
9143                 break;
9144             case 0x3f: /* FRSQRTS */
9145                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9146                 break;
9147             case 0x5b: /* FMUL */
9148                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
9149                 break;
9150             case 0x5c: /* FCMGE */
9151                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9152                 break;
9153             case 0x5d: /* FACGE */
9154                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9155                 break;
9156             case 0x5f: /* FDIV */
9157                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
9158                 break;
9159             case 0x7a: /* FABD */
9160                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9161                 gen_helper_vfp_absd(tcg_res, tcg_res);
9162                 break;
9163             case 0x7c: /* FCMGT */
9164                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9165                 break;
9166             case 0x7d: /* FACGT */
9167                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9168                 break;
9169             default:
9170                 g_assert_not_reached();
9171             }
9172 
9173             write_vec_element(s, tcg_res, rd, pass, MO_64);
9174         } else {
9175             /* Single */
9176             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9177             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9178             TCGv_i32 tcg_res = tcg_temp_new_i32();
9179 
9180             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9181             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9182 
9183             switch (fpopcode) {
9184             case 0x39: /* FMLS */
9185                 /* As usual for ARM, separate negation for fused multiply-add */
9186                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
9187                 /* fall through */
9188             case 0x19: /* FMLA */
9189                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9190                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
9191                                        tcg_res, fpst);
9192                 break;
9193             case 0x1a: /* FADD */
9194                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
9195                 break;
9196             case 0x1b: /* FMULX */
9197                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
9198                 break;
9199             case 0x1c: /* FCMEQ */
9200                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9201                 break;
9202             case 0x1e: /* FMAX */
9203                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
9204                 break;
9205             case 0x1f: /* FRECPS */
9206                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9207                 break;
9208             case 0x18: /* FMAXNM */
9209                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
9210                 break;
9211             case 0x38: /* FMINNM */
9212                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
9213                 break;
9214             case 0x3a: /* FSUB */
9215                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9216                 break;
9217             case 0x3e: /* FMIN */
9218                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9219                 break;
9220             case 0x3f: /* FRSQRTS */
9221                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9222                 break;
9223             case 0x5b: /* FMUL */
9224                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9225                 break;
9226             case 0x5c: /* FCMGE */
9227                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9228                 break;
9229             case 0x5d: /* FACGE */
9230                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9231                 break;
9232             case 0x5f: /* FDIV */
9233                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9234                 break;
9235             case 0x7a: /* FABD */
9236                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9237                 gen_helper_vfp_abss(tcg_res, tcg_res);
9238                 break;
9239             case 0x7c: /* FCMGT */
9240                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9241                 break;
9242             case 0x7d: /* FACGT */
9243                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9244                 break;
9245             default:
9246                 g_assert_not_reached();
9247             }
9248 
9249             if (elements == 1) {
9250                 /* scalar single so clear high part */
9251                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9252 
9253                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9254                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9255             } else {
9256                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9257             }
9258         }
9259     }
9260 
9261     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9262 }
9263 
9264 /* AdvSIMD scalar three same
9265  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
9266  * +-----+---+-----------+------+---+------+--------+---+------+------+
9267  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
9268  * +-----+---+-----------+------+---+------+--------+---+------+------+
9269  */
9270 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9271 {
9272     int rd = extract32(insn, 0, 5);
9273     int rn = extract32(insn, 5, 5);
9274     int opcode = extract32(insn, 11, 5);
9275     int rm = extract32(insn, 16, 5);
9276     int size = extract32(insn, 22, 2);
9277     bool u = extract32(insn, 29, 1);
9278     TCGv_i64 tcg_rd;
9279 
9280     if (opcode >= 0x18) {
9281         /* Floating point: U, size[1] and opcode indicate operation */
9282         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9283         switch (fpopcode) {
9284         case 0x1b: /* FMULX */
9285         case 0x1f: /* FRECPS */
9286         case 0x3f: /* FRSQRTS */
9287         case 0x5d: /* FACGE */
9288         case 0x7d: /* FACGT */
9289         case 0x1c: /* FCMEQ */
9290         case 0x5c: /* FCMGE */
9291         case 0x7c: /* FCMGT */
9292         case 0x7a: /* FABD */
9293             break;
9294         default:
9295             unallocated_encoding(s);
9296             return;
9297         }
9298 
9299         if (!fp_access_check(s)) {
9300             return;
9301         }
9302 
9303         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9304         return;
9305     }
9306 
9307     switch (opcode) {
9308     case 0x1: /* SQADD, UQADD */
9309     case 0x5: /* SQSUB, UQSUB */
9310     case 0x9: /* SQSHL, UQSHL */
9311     case 0xb: /* SQRSHL, UQRSHL */
9312         break;
9313     case 0x8: /* SSHL, USHL */
9314     case 0xa: /* SRSHL, URSHL */
9315     case 0x6: /* CMGT, CMHI */
9316     case 0x7: /* CMGE, CMHS */
9317     case 0x11: /* CMTST, CMEQ */
9318     case 0x10: /* ADD, SUB (vector) */
9319         if (size != 3) {
9320             unallocated_encoding(s);
9321             return;
9322         }
9323         break;
9324     case 0x16: /* SQDMULH, SQRDMULH (vector) */
9325         if (size != 1 && size != 2) {
9326             unallocated_encoding(s);
9327             return;
9328         }
9329         break;
9330     default:
9331         unallocated_encoding(s);
9332         return;
9333     }
9334 
9335     if (!fp_access_check(s)) {
9336         return;
9337     }
9338 
9339     tcg_rd = tcg_temp_new_i64();
9340 
9341     if (size == 3) {
9342         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9343         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9344 
9345         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9346     } else {
9347         /* Do a single operation on the lowest element in the vector.
9348          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9349          * no side effects for all these operations.
9350          * OPTME: special-purpose helpers would avoid doing some
9351          * unnecessary work in the helper for the 8 and 16 bit cases.
9352          */
9353         NeonGenTwoOpEnvFn *genenvfn;
9354         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9355         TCGv_i32 tcg_rm = tcg_temp_new_i32();
9356         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9357 
9358         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9359         read_vec_element_i32(s, tcg_rm, rm, 0, size);
9360 
9361         switch (opcode) {
9362         case 0x1: /* SQADD, UQADD */
9363         {
9364             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9365                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9366                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9367                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9368             };
9369             genenvfn = fns[size][u];
9370             break;
9371         }
9372         case 0x5: /* SQSUB, UQSUB */
9373         {
9374             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9375                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9376                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9377                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9378             };
9379             genenvfn = fns[size][u];
9380             break;
9381         }
9382         case 0x9: /* SQSHL, UQSHL */
9383         {
9384             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9385                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9386                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9387                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9388             };
9389             genenvfn = fns[size][u];
9390             break;
9391         }
9392         case 0xb: /* SQRSHL, UQRSHL */
9393         {
9394             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9395                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9396                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9397                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9398             };
9399             genenvfn = fns[size][u];
9400             break;
9401         }
9402         case 0x16: /* SQDMULH, SQRDMULH */
9403         {
9404             static NeonGenTwoOpEnvFn * const fns[2][2] = {
9405                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9406                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9407             };
9408             assert(size == 1 || size == 2);
9409             genenvfn = fns[size - 1][u];
9410             break;
9411         }
9412         default:
9413             g_assert_not_reached();
9414         }
9415 
9416         genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9417         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9418     }
9419 
9420     write_fp_dreg(s, rd, tcg_rd);
9421 }
9422 
9423 /* AdvSIMD scalar three same FP16
9424  *  31 30  29 28       24 23  22 21 20  16 15 14 13    11 10  9  5 4  0
9425  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9426  * | 0 1 | U | 1 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 | Rn | Rd |
9427  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9428  * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9429  * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9430  */
9431 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9432                                                   uint32_t insn)
9433 {
9434     int rd = extract32(insn, 0, 5);
9435     int rn = extract32(insn, 5, 5);
9436     int opcode = extract32(insn, 11, 3);
9437     int rm = extract32(insn, 16, 5);
9438     bool u = extract32(insn, 29, 1);
9439     bool a = extract32(insn, 23, 1);
9440     int fpopcode = opcode | (a << 3) |  (u << 4);
9441     TCGv_ptr fpst;
9442     TCGv_i32 tcg_op1;
9443     TCGv_i32 tcg_op2;
9444     TCGv_i32 tcg_res;
9445 
9446     switch (fpopcode) {
9447     case 0x03: /* FMULX */
9448     case 0x04: /* FCMEQ (reg) */
9449     case 0x07: /* FRECPS */
9450     case 0x0f: /* FRSQRTS */
9451     case 0x14: /* FCMGE (reg) */
9452     case 0x15: /* FACGE */
9453     case 0x1a: /* FABD */
9454     case 0x1c: /* FCMGT (reg) */
9455     case 0x1d: /* FACGT */
9456         break;
9457     default:
9458         unallocated_encoding(s);
9459         return;
9460     }
9461 
9462     if (!dc_isar_feature(aa64_fp16, s)) {
9463         unallocated_encoding(s);
9464     }
9465 
9466     if (!fp_access_check(s)) {
9467         return;
9468     }
9469 
9470     fpst = fpstatus_ptr(FPST_FPCR_F16);
9471 
9472     tcg_op1 = read_fp_hreg(s, rn);
9473     tcg_op2 = read_fp_hreg(s, rm);
9474     tcg_res = tcg_temp_new_i32();
9475 
9476     switch (fpopcode) {
9477     case 0x03: /* FMULX */
9478         gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9479         break;
9480     case 0x04: /* FCMEQ (reg) */
9481         gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9482         break;
9483     case 0x07: /* FRECPS */
9484         gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9485         break;
9486     case 0x0f: /* FRSQRTS */
9487         gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9488         break;
9489     case 0x14: /* FCMGE (reg) */
9490         gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9491         break;
9492     case 0x15: /* FACGE */
9493         gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9494         break;
9495     case 0x1a: /* FABD */
9496         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9497         tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9498         break;
9499     case 0x1c: /* FCMGT (reg) */
9500         gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9501         break;
9502     case 0x1d: /* FACGT */
9503         gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9504         break;
9505     default:
9506         g_assert_not_reached();
9507     }
9508 
9509     write_fp_sreg(s, rd, tcg_res);
9510 }
9511 
9512 /* AdvSIMD scalar three same extra
9513  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9514  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9515  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9516  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9517  */
9518 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9519                                                    uint32_t insn)
9520 {
9521     int rd = extract32(insn, 0, 5);
9522     int rn = extract32(insn, 5, 5);
9523     int opcode = extract32(insn, 11, 4);
9524     int rm = extract32(insn, 16, 5);
9525     int size = extract32(insn, 22, 2);
9526     bool u = extract32(insn, 29, 1);
9527     TCGv_i32 ele1, ele2, ele3;
9528     TCGv_i64 res;
9529     bool feature;
9530 
9531     switch (u * 16 + opcode) {
9532     case 0x10: /* SQRDMLAH (vector) */
9533     case 0x11: /* SQRDMLSH (vector) */
9534         if (size != 1 && size != 2) {
9535             unallocated_encoding(s);
9536             return;
9537         }
9538         feature = dc_isar_feature(aa64_rdm, s);
9539         break;
9540     default:
9541         unallocated_encoding(s);
9542         return;
9543     }
9544     if (!feature) {
9545         unallocated_encoding(s);
9546         return;
9547     }
9548     if (!fp_access_check(s)) {
9549         return;
9550     }
9551 
9552     /* Do a single operation on the lowest element in the vector.
9553      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9554      * with no side effects for all these operations.
9555      * OPTME: special-purpose helpers would avoid doing some
9556      * unnecessary work in the helper for the 16 bit cases.
9557      */
9558     ele1 = tcg_temp_new_i32();
9559     ele2 = tcg_temp_new_i32();
9560     ele3 = tcg_temp_new_i32();
9561 
9562     read_vec_element_i32(s, ele1, rn, 0, size);
9563     read_vec_element_i32(s, ele2, rm, 0, size);
9564     read_vec_element_i32(s, ele3, rd, 0, size);
9565 
9566     switch (opcode) {
9567     case 0x0: /* SQRDMLAH */
9568         if (size == 1) {
9569             gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9570         } else {
9571             gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9572         }
9573         break;
9574     case 0x1: /* SQRDMLSH */
9575         if (size == 1) {
9576             gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9577         } else {
9578             gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9579         }
9580         break;
9581     default:
9582         g_assert_not_reached();
9583     }
9584 
9585     res = tcg_temp_new_i64();
9586     tcg_gen_extu_i32_i64(res, ele3);
9587     write_fp_dreg(s, rd, res);
9588 }
9589 
9590 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9591                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9592                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9593 {
9594     /* Handle 64->64 opcodes which are shared between the scalar and
9595      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9596      * is valid in either group and also the double-precision fp ops.
9597      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9598      * requires them.
9599      */
9600     TCGCond cond;
9601 
9602     switch (opcode) {
9603     case 0x4: /* CLS, CLZ */
9604         if (u) {
9605             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9606         } else {
9607             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9608         }
9609         break;
9610     case 0x5: /* NOT */
9611         /* This opcode is shared with CNT and RBIT but we have earlier
9612          * enforced that size == 3 if and only if this is the NOT insn.
9613          */
9614         tcg_gen_not_i64(tcg_rd, tcg_rn);
9615         break;
9616     case 0x7: /* SQABS, SQNEG */
9617         if (u) {
9618             gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9619         } else {
9620             gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9621         }
9622         break;
9623     case 0xa: /* CMLT */
9624         /* 64 bit integer comparison against zero, result is
9625          * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9626          * subtracting 1.
9627          */
9628         cond = TCG_COND_LT;
9629     do_cmop:
9630         tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9631         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9632         break;
9633     case 0x8: /* CMGT, CMGE */
9634         cond = u ? TCG_COND_GE : TCG_COND_GT;
9635         goto do_cmop;
9636     case 0x9: /* CMEQ, CMLE */
9637         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9638         goto do_cmop;
9639     case 0xb: /* ABS, NEG */
9640         if (u) {
9641             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9642         } else {
9643             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9644         }
9645         break;
9646     case 0x2f: /* FABS */
9647         gen_helper_vfp_absd(tcg_rd, tcg_rn);
9648         break;
9649     case 0x6f: /* FNEG */
9650         gen_helper_vfp_negd(tcg_rd, tcg_rn);
9651         break;
9652     case 0x7f: /* FSQRT */
9653         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9654         break;
9655     case 0x1a: /* FCVTNS */
9656     case 0x1b: /* FCVTMS */
9657     case 0x1c: /* FCVTAS */
9658     case 0x3a: /* FCVTPS */
9659     case 0x3b: /* FCVTZS */
9660         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9661         break;
9662     case 0x5a: /* FCVTNU */
9663     case 0x5b: /* FCVTMU */
9664     case 0x5c: /* FCVTAU */
9665     case 0x7a: /* FCVTPU */
9666     case 0x7b: /* FCVTZU */
9667         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9668         break;
9669     case 0x18: /* FRINTN */
9670     case 0x19: /* FRINTM */
9671     case 0x38: /* FRINTP */
9672     case 0x39: /* FRINTZ */
9673     case 0x58: /* FRINTA */
9674     case 0x79: /* FRINTI */
9675         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9676         break;
9677     case 0x59: /* FRINTX */
9678         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9679         break;
9680     case 0x1e: /* FRINT32Z */
9681     case 0x5e: /* FRINT32X */
9682         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9683         break;
9684     case 0x1f: /* FRINT64Z */
9685     case 0x5f: /* FRINT64X */
9686         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9687         break;
9688     default:
9689         g_assert_not_reached();
9690     }
9691 }
9692 
9693 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9694                                    bool is_scalar, bool is_u, bool is_q,
9695                                    int size, int rn, int rd)
9696 {
9697     bool is_double = (size == MO_64);
9698     TCGv_ptr fpst;
9699 
9700     if (!fp_access_check(s)) {
9701         return;
9702     }
9703 
9704     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9705 
9706     if (is_double) {
9707         TCGv_i64 tcg_op = tcg_temp_new_i64();
9708         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9709         TCGv_i64 tcg_res = tcg_temp_new_i64();
9710         NeonGenTwoDoubleOpFn *genfn;
9711         bool swap = false;
9712         int pass;
9713 
9714         switch (opcode) {
9715         case 0x2e: /* FCMLT (zero) */
9716             swap = true;
9717             /* fallthrough */
9718         case 0x2c: /* FCMGT (zero) */
9719             genfn = gen_helper_neon_cgt_f64;
9720             break;
9721         case 0x2d: /* FCMEQ (zero) */
9722             genfn = gen_helper_neon_ceq_f64;
9723             break;
9724         case 0x6d: /* FCMLE (zero) */
9725             swap = true;
9726             /* fall through */
9727         case 0x6c: /* FCMGE (zero) */
9728             genfn = gen_helper_neon_cge_f64;
9729             break;
9730         default:
9731             g_assert_not_reached();
9732         }
9733 
9734         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9735             read_vec_element(s, tcg_op, rn, pass, MO_64);
9736             if (swap) {
9737                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9738             } else {
9739                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9740             }
9741             write_vec_element(s, tcg_res, rd, pass, MO_64);
9742         }
9743 
9744         clear_vec_high(s, !is_scalar, rd);
9745     } else {
9746         TCGv_i32 tcg_op = tcg_temp_new_i32();
9747         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9748         TCGv_i32 tcg_res = tcg_temp_new_i32();
9749         NeonGenTwoSingleOpFn *genfn;
9750         bool swap = false;
9751         int pass, maxpasses;
9752 
9753         if (size == MO_16) {
9754             switch (opcode) {
9755             case 0x2e: /* FCMLT (zero) */
9756                 swap = true;
9757                 /* fall through */
9758             case 0x2c: /* FCMGT (zero) */
9759                 genfn = gen_helper_advsimd_cgt_f16;
9760                 break;
9761             case 0x2d: /* FCMEQ (zero) */
9762                 genfn = gen_helper_advsimd_ceq_f16;
9763                 break;
9764             case 0x6d: /* FCMLE (zero) */
9765                 swap = true;
9766                 /* fall through */
9767             case 0x6c: /* FCMGE (zero) */
9768                 genfn = gen_helper_advsimd_cge_f16;
9769                 break;
9770             default:
9771                 g_assert_not_reached();
9772             }
9773         } else {
9774             switch (opcode) {
9775             case 0x2e: /* FCMLT (zero) */
9776                 swap = true;
9777                 /* fall through */
9778             case 0x2c: /* FCMGT (zero) */
9779                 genfn = gen_helper_neon_cgt_f32;
9780                 break;
9781             case 0x2d: /* FCMEQ (zero) */
9782                 genfn = gen_helper_neon_ceq_f32;
9783                 break;
9784             case 0x6d: /* FCMLE (zero) */
9785                 swap = true;
9786                 /* fall through */
9787             case 0x6c: /* FCMGE (zero) */
9788                 genfn = gen_helper_neon_cge_f32;
9789                 break;
9790             default:
9791                 g_assert_not_reached();
9792             }
9793         }
9794 
9795         if (is_scalar) {
9796             maxpasses = 1;
9797         } else {
9798             int vector_size = 8 << is_q;
9799             maxpasses = vector_size >> size;
9800         }
9801 
9802         for (pass = 0; pass < maxpasses; pass++) {
9803             read_vec_element_i32(s, tcg_op, rn, pass, size);
9804             if (swap) {
9805                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9806             } else {
9807                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9808             }
9809             if (is_scalar) {
9810                 write_fp_sreg(s, rd, tcg_res);
9811             } else {
9812                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9813             }
9814         }
9815 
9816         if (!is_scalar) {
9817             clear_vec_high(s, is_q, rd);
9818         }
9819     }
9820 }
9821 
9822 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9823                                     bool is_scalar, bool is_u, bool is_q,
9824                                     int size, int rn, int rd)
9825 {
9826     bool is_double = (size == 3);
9827     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9828 
9829     if (is_double) {
9830         TCGv_i64 tcg_op = tcg_temp_new_i64();
9831         TCGv_i64 tcg_res = tcg_temp_new_i64();
9832         int pass;
9833 
9834         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9835             read_vec_element(s, tcg_op, rn, pass, MO_64);
9836             switch (opcode) {
9837             case 0x3d: /* FRECPE */
9838                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9839                 break;
9840             case 0x3f: /* FRECPX */
9841                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9842                 break;
9843             case 0x7d: /* FRSQRTE */
9844                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9845                 break;
9846             default:
9847                 g_assert_not_reached();
9848             }
9849             write_vec_element(s, tcg_res, rd, pass, MO_64);
9850         }
9851         clear_vec_high(s, !is_scalar, rd);
9852     } else {
9853         TCGv_i32 tcg_op = tcg_temp_new_i32();
9854         TCGv_i32 tcg_res = tcg_temp_new_i32();
9855         int pass, maxpasses;
9856 
9857         if (is_scalar) {
9858             maxpasses = 1;
9859         } else {
9860             maxpasses = is_q ? 4 : 2;
9861         }
9862 
9863         for (pass = 0; pass < maxpasses; pass++) {
9864             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9865 
9866             switch (opcode) {
9867             case 0x3c: /* URECPE */
9868                 gen_helper_recpe_u32(tcg_res, tcg_op);
9869                 break;
9870             case 0x3d: /* FRECPE */
9871                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9872                 break;
9873             case 0x3f: /* FRECPX */
9874                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9875                 break;
9876             case 0x7d: /* FRSQRTE */
9877                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9878                 break;
9879             default:
9880                 g_assert_not_reached();
9881             }
9882 
9883             if (is_scalar) {
9884                 write_fp_sreg(s, rd, tcg_res);
9885             } else {
9886                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9887             }
9888         }
9889         if (!is_scalar) {
9890             clear_vec_high(s, is_q, rd);
9891         }
9892     }
9893 }
9894 
9895 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9896                                 int opcode, bool u, bool is_q,
9897                                 int size, int rn, int rd)
9898 {
9899     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9900      * in the source becomes a size element in the destination).
9901      */
9902     int pass;
9903     TCGv_i32 tcg_res[2];
9904     int destelt = is_q ? 2 : 0;
9905     int passes = scalar ? 1 : 2;
9906 
9907     if (scalar) {
9908         tcg_res[1] = tcg_constant_i32(0);
9909     }
9910 
9911     for (pass = 0; pass < passes; pass++) {
9912         TCGv_i64 tcg_op = tcg_temp_new_i64();
9913         NeonGenNarrowFn *genfn = NULL;
9914         NeonGenNarrowEnvFn *genenvfn = NULL;
9915 
9916         if (scalar) {
9917             read_vec_element(s, tcg_op, rn, pass, size + 1);
9918         } else {
9919             read_vec_element(s, tcg_op, rn, pass, MO_64);
9920         }
9921         tcg_res[pass] = tcg_temp_new_i32();
9922 
9923         switch (opcode) {
9924         case 0x12: /* XTN, SQXTUN */
9925         {
9926             static NeonGenNarrowFn * const xtnfns[3] = {
9927                 gen_helper_neon_narrow_u8,
9928                 gen_helper_neon_narrow_u16,
9929                 tcg_gen_extrl_i64_i32,
9930             };
9931             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9932                 gen_helper_neon_unarrow_sat8,
9933                 gen_helper_neon_unarrow_sat16,
9934                 gen_helper_neon_unarrow_sat32,
9935             };
9936             if (u) {
9937                 genenvfn = sqxtunfns[size];
9938             } else {
9939                 genfn = xtnfns[size];
9940             }
9941             break;
9942         }
9943         case 0x14: /* SQXTN, UQXTN */
9944         {
9945             static NeonGenNarrowEnvFn * const fns[3][2] = {
9946                 { gen_helper_neon_narrow_sat_s8,
9947                   gen_helper_neon_narrow_sat_u8 },
9948                 { gen_helper_neon_narrow_sat_s16,
9949                   gen_helper_neon_narrow_sat_u16 },
9950                 { gen_helper_neon_narrow_sat_s32,
9951                   gen_helper_neon_narrow_sat_u32 },
9952             };
9953             genenvfn = fns[size][u];
9954             break;
9955         }
9956         case 0x16: /* FCVTN, FCVTN2 */
9957             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9958             if (size == 2) {
9959                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
9960             } else {
9961                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9962                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9963                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9964                 TCGv_i32 ahp = get_ahp_flag();
9965 
9966                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9967                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9968                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9969                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9970             }
9971             break;
9972         case 0x36: /* BFCVTN, BFCVTN2 */
9973             {
9974                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9975                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
9976             }
9977             break;
9978         case 0x56:  /* FCVTXN, FCVTXN2 */
9979             /* 64 bit to 32 bit float conversion
9980              * with von Neumann rounding (round to odd)
9981              */
9982             assert(size == 2);
9983             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
9984             break;
9985         default:
9986             g_assert_not_reached();
9987         }
9988 
9989         if (genfn) {
9990             genfn(tcg_res[pass], tcg_op);
9991         } else if (genenvfn) {
9992             genenvfn(tcg_res[pass], cpu_env, tcg_op);
9993         }
9994     }
9995 
9996     for (pass = 0; pass < 2; pass++) {
9997         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
9998     }
9999     clear_vec_high(s, is_q, rd);
10000 }
10001 
10002 /* Remaining saturating accumulating ops */
10003 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
10004                                 bool is_q, int size, int rn, int rd)
10005 {
10006     bool is_double = (size == 3);
10007 
10008     if (is_double) {
10009         TCGv_i64 tcg_rn = tcg_temp_new_i64();
10010         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10011         int pass;
10012 
10013         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10014             read_vec_element(s, tcg_rn, rn, pass, MO_64);
10015             read_vec_element(s, tcg_rd, rd, pass, MO_64);
10016 
10017             if (is_u) { /* USQADD */
10018                 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10019             } else { /* SUQADD */
10020                 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10021             }
10022             write_vec_element(s, tcg_rd, rd, pass, MO_64);
10023         }
10024         clear_vec_high(s, !is_scalar, rd);
10025     } else {
10026         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10027         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10028         int pass, maxpasses;
10029 
10030         if (is_scalar) {
10031             maxpasses = 1;
10032         } else {
10033             maxpasses = is_q ? 4 : 2;
10034         }
10035 
10036         for (pass = 0; pass < maxpasses; pass++) {
10037             if (is_scalar) {
10038                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
10039                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
10040             } else {
10041                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
10042                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10043             }
10044 
10045             if (is_u) { /* USQADD */
10046                 switch (size) {
10047                 case 0:
10048                     gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10049                     break;
10050                 case 1:
10051                     gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10052                     break;
10053                 case 2:
10054                     gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10055                     break;
10056                 default:
10057                     g_assert_not_reached();
10058                 }
10059             } else { /* SUQADD */
10060                 switch (size) {
10061                 case 0:
10062                     gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10063                     break;
10064                 case 1:
10065                     gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10066                     break;
10067                 case 2:
10068                     gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10069                     break;
10070                 default:
10071                     g_assert_not_reached();
10072                 }
10073             }
10074 
10075             if (is_scalar) {
10076                 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
10077             }
10078             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10079         }
10080         clear_vec_high(s, is_q, rd);
10081     }
10082 }
10083 
10084 /* AdvSIMD scalar two reg misc
10085  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
10086  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10087  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
10088  * +-----+---+-----------+------+-----------+--------+-----+------+------+
10089  */
10090 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
10091 {
10092     int rd = extract32(insn, 0, 5);
10093     int rn = extract32(insn, 5, 5);
10094     int opcode = extract32(insn, 12, 5);
10095     int size = extract32(insn, 22, 2);
10096     bool u = extract32(insn, 29, 1);
10097     bool is_fcvt = false;
10098     int rmode;
10099     TCGv_i32 tcg_rmode;
10100     TCGv_ptr tcg_fpstatus;
10101 
10102     switch (opcode) {
10103     case 0x3: /* USQADD / SUQADD*/
10104         if (!fp_access_check(s)) {
10105             return;
10106         }
10107         handle_2misc_satacc(s, true, u, false, size, rn, rd);
10108         return;
10109     case 0x7: /* SQABS / SQNEG */
10110         break;
10111     case 0xa: /* CMLT */
10112         if (u) {
10113             unallocated_encoding(s);
10114             return;
10115         }
10116         /* fall through */
10117     case 0x8: /* CMGT, CMGE */
10118     case 0x9: /* CMEQ, CMLE */
10119     case 0xb: /* ABS, NEG */
10120         if (size != 3) {
10121             unallocated_encoding(s);
10122             return;
10123         }
10124         break;
10125     case 0x12: /* SQXTUN */
10126         if (!u) {
10127             unallocated_encoding(s);
10128             return;
10129         }
10130         /* fall through */
10131     case 0x14: /* SQXTN, UQXTN */
10132         if (size == 3) {
10133             unallocated_encoding(s);
10134             return;
10135         }
10136         if (!fp_access_check(s)) {
10137             return;
10138         }
10139         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10140         return;
10141     case 0xc ... 0xf:
10142     case 0x16 ... 0x1d:
10143     case 0x1f:
10144         /* Floating point: U, size[1] and opcode indicate operation;
10145          * size[0] indicates single or double precision.
10146          */
10147         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10148         size = extract32(size, 0, 1) ? 3 : 2;
10149         switch (opcode) {
10150         case 0x2c: /* FCMGT (zero) */
10151         case 0x2d: /* FCMEQ (zero) */
10152         case 0x2e: /* FCMLT (zero) */
10153         case 0x6c: /* FCMGE (zero) */
10154         case 0x6d: /* FCMLE (zero) */
10155             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10156             return;
10157         case 0x1d: /* SCVTF */
10158         case 0x5d: /* UCVTF */
10159         {
10160             bool is_signed = (opcode == 0x1d);
10161             if (!fp_access_check(s)) {
10162                 return;
10163             }
10164             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10165             return;
10166         }
10167         case 0x3d: /* FRECPE */
10168         case 0x3f: /* FRECPX */
10169         case 0x7d: /* FRSQRTE */
10170             if (!fp_access_check(s)) {
10171                 return;
10172             }
10173             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10174             return;
10175         case 0x1a: /* FCVTNS */
10176         case 0x1b: /* FCVTMS */
10177         case 0x3a: /* FCVTPS */
10178         case 0x3b: /* FCVTZS */
10179         case 0x5a: /* FCVTNU */
10180         case 0x5b: /* FCVTMU */
10181         case 0x7a: /* FCVTPU */
10182         case 0x7b: /* FCVTZU */
10183             is_fcvt = true;
10184             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10185             break;
10186         case 0x1c: /* FCVTAS */
10187         case 0x5c: /* FCVTAU */
10188             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10189             is_fcvt = true;
10190             rmode = FPROUNDING_TIEAWAY;
10191             break;
10192         case 0x56: /* FCVTXN, FCVTXN2 */
10193             if (size == 2) {
10194                 unallocated_encoding(s);
10195                 return;
10196             }
10197             if (!fp_access_check(s)) {
10198                 return;
10199             }
10200             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10201             return;
10202         default:
10203             unallocated_encoding(s);
10204             return;
10205         }
10206         break;
10207     default:
10208         unallocated_encoding(s);
10209         return;
10210     }
10211 
10212     if (!fp_access_check(s)) {
10213         return;
10214     }
10215 
10216     if (is_fcvt) {
10217         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
10218         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
10219     } else {
10220         tcg_fpstatus = NULL;
10221         tcg_rmode = NULL;
10222     }
10223 
10224     if (size == 3) {
10225         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10226         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10227 
10228         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10229         write_fp_dreg(s, rd, tcg_rd);
10230     } else {
10231         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10232         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10233 
10234         read_vec_element_i32(s, tcg_rn, rn, 0, size);
10235 
10236         switch (opcode) {
10237         case 0x7: /* SQABS, SQNEG */
10238         {
10239             NeonGenOneOpEnvFn *genfn;
10240             static NeonGenOneOpEnvFn * const fns[3][2] = {
10241                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10242                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10243                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10244             };
10245             genfn = fns[size][u];
10246             genfn(tcg_rd, cpu_env, tcg_rn);
10247             break;
10248         }
10249         case 0x1a: /* FCVTNS */
10250         case 0x1b: /* FCVTMS */
10251         case 0x1c: /* FCVTAS */
10252         case 0x3a: /* FCVTPS */
10253         case 0x3b: /* FCVTZS */
10254             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10255                                  tcg_fpstatus);
10256             break;
10257         case 0x5a: /* FCVTNU */
10258         case 0x5b: /* FCVTMU */
10259         case 0x5c: /* FCVTAU */
10260         case 0x7a: /* FCVTPU */
10261         case 0x7b: /* FCVTZU */
10262             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10263                                  tcg_fpstatus);
10264             break;
10265         default:
10266             g_assert_not_reached();
10267         }
10268 
10269         write_fp_sreg(s, rd, tcg_rd);
10270     }
10271 
10272     if (is_fcvt) {
10273         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
10274     }
10275 }
10276 
10277 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10278 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10279                                  int immh, int immb, int opcode, int rn, int rd)
10280 {
10281     int size = 32 - clz32(immh) - 1;
10282     int immhb = immh << 3 | immb;
10283     int shift = 2 * (8 << size) - immhb;
10284     GVecGen2iFn *gvec_fn;
10285 
10286     if (extract32(immh, 3, 1) && !is_q) {
10287         unallocated_encoding(s);
10288         return;
10289     }
10290     tcg_debug_assert(size <= 3);
10291 
10292     if (!fp_access_check(s)) {
10293         return;
10294     }
10295 
10296     switch (opcode) {
10297     case 0x02: /* SSRA / USRA (accumulate) */
10298         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10299         break;
10300 
10301     case 0x08: /* SRI */
10302         gvec_fn = gen_gvec_sri;
10303         break;
10304 
10305     case 0x00: /* SSHR / USHR */
10306         if (is_u) {
10307             if (shift == 8 << size) {
10308                 /* Shift count the same size as element size produces zero.  */
10309                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10310                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
10311                 return;
10312             }
10313             gvec_fn = tcg_gen_gvec_shri;
10314         } else {
10315             /* Shift count the same size as element size produces all sign.  */
10316             if (shift == 8 << size) {
10317                 shift -= 1;
10318             }
10319             gvec_fn = tcg_gen_gvec_sari;
10320         }
10321         break;
10322 
10323     case 0x04: /* SRSHR / URSHR (rounding) */
10324         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10325         break;
10326 
10327     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10328         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10329         break;
10330 
10331     default:
10332         g_assert_not_reached();
10333     }
10334 
10335     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10336 }
10337 
10338 /* SHL/SLI - Vector shift left */
10339 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10340                                  int immh, int immb, int opcode, int rn, int rd)
10341 {
10342     int size = 32 - clz32(immh) - 1;
10343     int immhb = immh << 3 | immb;
10344     int shift = immhb - (8 << size);
10345 
10346     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10347     assert(size >= 0 && size <= 3);
10348 
10349     if (extract32(immh, 3, 1) && !is_q) {
10350         unallocated_encoding(s);
10351         return;
10352     }
10353 
10354     if (!fp_access_check(s)) {
10355         return;
10356     }
10357 
10358     if (insert) {
10359         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10360     } else {
10361         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10362     }
10363 }
10364 
10365 /* USHLL/SHLL - Vector shift left with widening */
10366 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10367                                  int immh, int immb, int opcode, int rn, int rd)
10368 {
10369     int size = 32 - clz32(immh) - 1;
10370     int immhb = immh << 3 | immb;
10371     int shift = immhb - (8 << size);
10372     int dsize = 64;
10373     int esize = 8 << size;
10374     int elements = dsize/esize;
10375     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10376     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10377     int i;
10378 
10379     if (size >= 3) {
10380         unallocated_encoding(s);
10381         return;
10382     }
10383 
10384     if (!fp_access_check(s)) {
10385         return;
10386     }
10387 
10388     /* For the LL variants the store is larger than the load,
10389      * so if rd == rn we would overwrite parts of our input.
10390      * So load everything right now and use shifts in the main loop.
10391      */
10392     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10393 
10394     for (i = 0; i < elements; i++) {
10395         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10396         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10397         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10398         write_vec_element(s, tcg_rd, rd, i, size + 1);
10399     }
10400 }
10401 
10402 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10403 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10404                                  int immh, int immb, int opcode, int rn, int rd)
10405 {
10406     int immhb = immh << 3 | immb;
10407     int size = 32 - clz32(immh) - 1;
10408     int dsize = 64;
10409     int esize = 8 << size;
10410     int elements = dsize/esize;
10411     int shift = (2 * esize) - immhb;
10412     bool round = extract32(opcode, 0, 1);
10413     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10414     TCGv_i64 tcg_round;
10415     int i;
10416 
10417     if (extract32(immh, 3, 1)) {
10418         unallocated_encoding(s);
10419         return;
10420     }
10421 
10422     if (!fp_access_check(s)) {
10423         return;
10424     }
10425 
10426     tcg_rn = tcg_temp_new_i64();
10427     tcg_rd = tcg_temp_new_i64();
10428     tcg_final = tcg_temp_new_i64();
10429     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10430 
10431     if (round) {
10432         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10433     } else {
10434         tcg_round = NULL;
10435     }
10436 
10437     for (i = 0; i < elements; i++) {
10438         read_vec_element(s, tcg_rn, rn, i, size+1);
10439         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10440                                 false, true, size+1, shift);
10441 
10442         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10443     }
10444 
10445     if (!is_q) {
10446         write_vec_element(s, tcg_final, rd, 0, MO_64);
10447     } else {
10448         write_vec_element(s, tcg_final, rd, 1, MO_64);
10449     }
10450 
10451     clear_vec_high(s, is_q, rd);
10452 }
10453 
10454 
10455 /* AdvSIMD shift by immediate
10456  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10457  * +---+---+---+-------------+------+------+--------+---+------+------+
10458  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10459  * +---+---+---+-------------+------+------+--------+---+------+------+
10460  */
10461 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10462 {
10463     int rd = extract32(insn, 0, 5);
10464     int rn = extract32(insn, 5, 5);
10465     int opcode = extract32(insn, 11, 5);
10466     int immb = extract32(insn, 16, 3);
10467     int immh = extract32(insn, 19, 4);
10468     bool is_u = extract32(insn, 29, 1);
10469     bool is_q = extract32(insn, 30, 1);
10470 
10471     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10472     assert(immh != 0);
10473 
10474     switch (opcode) {
10475     case 0x08: /* SRI */
10476         if (!is_u) {
10477             unallocated_encoding(s);
10478             return;
10479         }
10480         /* fall through */
10481     case 0x00: /* SSHR / USHR */
10482     case 0x02: /* SSRA / USRA (accumulate) */
10483     case 0x04: /* SRSHR / URSHR (rounding) */
10484     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10485         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10486         break;
10487     case 0x0a: /* SHL / SLI */
10488         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10489         break;
10490     case 0x10: /* SHRN */
10491     case 0x11: /* RSHRN / SQRSHRUN */
10492         if (is_u) {
10493             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10494                                    opcode, rn, rd);
10495         } else {
10496             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10497         }
10498         break;
10499     case 0x12: /* SQSHRN / UQSHRN */
10500     case 0x13: /* SQRSHRN / UQRSHRN */
10501         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10502                                opcode, rn, rd);
10503         break;
10504     case 0x14: /* SSHLL / USHLL */
10505         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10506         break;
10507     case 0x1c: /* SCVTF / UCVTF */
10508         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10509                                      opcode, rn, rd);
10510         break;
10511     case 0xc: /* SQSHLU */
10512         if (!is_u) {
10513             unallocated_encoding(s);
10514             return;
10515         }
10516         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10517         break;
10518     case 0xe: /* SQSHL, UQSHL */
10519         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10520         break;
10521     case 0x1f: /* FCVTZS/ FCVTZU */
10522         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10523         return;
10524     default:
10525         unallocated_encoding(s);
10526         return;
10527     }
10528 }
10529 
10530 /* Generate code to do a "long" addition or subtraction, ie one done in
10531  * TCGv_i64 on vector lanes twice the width specified by size.
10532  */
10533 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10534                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10535 {
10536     static NeonGenTwo64OpFn * const fns[3][2] = {
10537         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10538         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10539         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10540     };
10541     NeonGenTwo64OpFn *genfn;
10542     assert(size < 3);
10543 
10544     genfn = fns[size][is_sub];
10545     genfn(tcg_res, tcg_op1, tcg_op2);
10546 }
10547 
10548 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10549                                 int opcode, int rd, int rn, int rm)
10550 {
10551     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10552     TCGv_i64 tcg_res[2];
10553     int pass, accop;
10554 
10555     tcg_res[0] = tcg_temp_new_i64();
10556     tcg_res[1] = tcg_temp_new_i64();
10557 
10558     /* Does this op do an adding accumulate, a subtracting accumulate,
10559      * or no accumulate at all?
10560      */
10561     switch (opcode) {
10562     case 5:
10563     case 8:
10564     case 9:
10565         accop = 1;
10566         break;
10567     case 10:
10568     case 11:
10569         accop = -1;
10570         break;
10571     default:
10572         accop = 0;
10573         break;
10574     }
10575 
10576     if (accop != 0) {
10577         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10578         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10579     }
10580 
10581     /* size == 2 means two 32x32->64 operations; this is worth special
10582      * casing because we can generally handle it inline.
10583      */
10584     if (size == 2) {
10585         for (pass = 0; pass < 2; pass++) {
10586             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10587             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10588             TCGv_i64 tcg_passres;
10589             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10590 
10591             int elt = pass + is_q * 2;
10592 
10593             read_vec_element(s, tcg_op1, rn, elt, memop);
10594             read_vec_element(s, tcg_op2, rm, elt, memop);
10595 
10596             if (accop == 0) {
10597                 tcg_passres = tcg_res[pass];
10598             } else {
10599                 tcg_passres = tcg_temp_new_i64();
10600             }
10601 
10602             switch (opcode) {
10603             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10604                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10605                 break;
10606             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10607                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10608                 break;
10609             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10610             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10611             {
10612                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10613                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10614 
10615                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10616                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10617                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10618                                     tcg_passres,
10619                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10620                 break;
10621             }
10622             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10623             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10624             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10625                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10626                 break;
10627             case 9: /* SQDMLAL, SQDMLAL2 */
10628             case 11: /* SQDMLSL, SQDMLSL2 */
10629             case 13: /* SQDMULL, SQDMULL2 */
10630                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10631                 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10632                                                   tcg_passres, tcg_passres);
10633                 break;
10634             default:
10635                 g_assert_not_reached();
10636             }
10637 
10638             if (opcode == 9 || opcode == 11) {
10639                 /* saturating accumulate ops */
10640                 if (accop < 0) {
10641                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10642                 }
10643                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10644                                                   tcg_res[pass], tcg_passres);
10645             } else if (accop > 0) {
10646                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10647             } else if (accop < 0) {
10648                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10649             }
10650         }
10651     } else {
10652         /* size 0 or 1, generally helper functions */
10653         for (pass = 0; pass < 2; pass++) {
10654             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10655             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10656             TCGv_i64 tcg_passres;
10657             int elt = pass + is_q * 2;
10658 
10659             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10660             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10661 
10662             if (accop == 0) {
10663                 tcg_passres = tcg_res[pass];
10664             } else {
10665                 tcg_passres = tcg_temp_new_i64();
10666             }
10667 
10668             switch (opcode) {
10669             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10670             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10671             {
10672                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10673                 static NeonGenWidenFn * const widenfns[2][2] = {
10674                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10675                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10676                 };
10677                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10678 
10679                 widenfn(tcg_op2_64, tcg_op2);
10680                 widenfn(tcg_passres, tcg_op1);
10681                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10682                               tcg_passres, tcg_op2_64);
10683                 break;
10684             }
10685             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10686             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10687                 if (size == 0) {
10688                     if (is_u) {
10689                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10690                     } else {
10691                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10692                     }
10693                 } else {
10694                     if (is_u) {
10695                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10696                     } else {
10697                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10698                     }
10699                 }
10700                 break;
10701             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10702             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10703             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10704                 if (size == 0) {
10705                     if (is_u) {
10706                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10707                     } else {
10708                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10709                     }
10710                 } else {
10711                     if (is_u) {
10712                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10713                     } else {
10714                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10715                     }
10716                 }
10717                 break;
10718             case 9: /* SQDMLAL, SQDMLAL2 */
10719             case 11: /* SQDMLSL, SQDMLSL2 */
10720             case 13: /* SQDMULL, SQDMULL2 */
10721                 assert(size == 1);
10722                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10723                 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10724                                                   tcg_passres, tcg_passres);
10725                 break;
10726             default:
10727                 g_assert_not_reached();
10728             }
10729 
10730             if (accop != 0) {
10731                 if (opcode == 9 || opcode == 11) {
10732                     /* saturating accumulate ops */
10733                     if (accop < 0) {
10734                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10735                     }
10736                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10737                                                       tcg_res[pass],
10738                                                       tcg_passres);
10739                 } else {
10740                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10741                                   tcg_res[pass], tcg_passres);
10742                 }
10743             }
10744         }
10745     }
10746 
10747     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10748     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10749 }
10750 
10751 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10752                             int opcode, int rd, int rn, int rm)
10753 {
10754     TCGv_i64 tcg_res[2];
10755     int part = is_q ? 2 : 0;
10756     int pass;
10757 
10758     for (pass = 0; pass < 2; pass++) {
10759         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10760         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10761         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10762         static NeonGenWidenFn * const widenfns[3][2] = {
10763             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10764             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10765             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10766         };
10767         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10768 
10769         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10770         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10771         widenfn(tcg_op2_wide, tcg_op2);
10772         tcg_res[pass] = tcg_temp_new_i64();
10773         gen_neon_addl(size, (opcode == 3),
10774                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10775     }
10776 
10777     for (pass = 0; pass < 2; pass++) {
10778         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10779     }
10780 }
10781 
10782 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10783 {
10784     tcg_gen_addi_i64(in, in, 1U << 31);
10785     tcg_gen_extrh_i64_i32(res, in);
10786 }
10787 
10788 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10789                                  int opcode, int rd, int rn, int rm)
10790 {
10791     TCGv_i32 tcg_res[2];
10792     int part = is_q ? 2 : 0;
10793     int pass;
10794 
10795     for (pass = 0; pass < 2; pass++) {
10796         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10797         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10798         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10799         static NeonGenNarrowFn * const narrowfns[3][2] = {
10800             { gen_helper_neon_narrow_high_u8,
10801               gen_helper_neon_narrow_round_high_u8 },
10802             { gen_helper_neon_narrow_high_u16,
10803               gen_helper_neon_narrow_round_high_u16 },
10804             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10805         };
10806         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10807 
10808         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10809         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10810 
10811         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10812 
10813         tcg_res[pass] = tcg_temp_new_i32();
10814         gennarrow(tcg_res[pass], tcg_wideres);
10815     }
10816 
10817     for (pass = 0; pass < 2; pass++) {
10818         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10819     }
10820     clear_vec_high(s, is_q, rd);
10821 }
10822 
10823 /* AdvSIMD three different
10824  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10825  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10826  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10827  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10828  */
10829 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10830 {
10831     /* Instructions in this group fall into three basic classes
10832      * (in each case with the operation working on each element in
10833      * the input vectors):
10834      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10835      *     128 bit input)
10836      * (2) wide 64 x 128 -> 128
10837      * (3) narrowing 128 x 128 -> 64
10838      * Here we do initial decode, catch unallocated cases and
10839      * dispatch to separate functions for each class.
10840      */
10841     int is_q = extract32(insn, 30, 1);
10842     int is_u = extract32(insn, 29, 1);
10843     int size = extract32(insn, 22, 2);
10844     int opcode = extract32(insn, 12, 4);
10845     int rm = extract32(insn, 16, 5);
10846     int rn = extract32(insn, 5, 5);
10847     int rd = extract32(insn, 0, 5);
10848 
10849     switch (opcode) {
10850     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10851     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10852         /* 64 x 128 -> 128 */
10853         if (size == 3) {
10854             unallocated_encoding(s);
10855             return;
10856         }
10857         if (!fp_access_check(s)) {
10858             return;
10859         }
10860         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10861         break;
10862     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10863     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10864         /* 128 x 128 -> 64 */
10865         if (size == 3) {
10866             unallocated_encoding(s);
10867             return;
10868         }
10869         if (!fp_access_check(s)) {
10870             return;
10871         }
10872         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10873         break;
10874     case 14: /* PMULL, PMULL2 */
10875         if (is_u) {
10876             unallocated_encoding(s);
10877             return;
10878         }
10879         switch (size) {
10880         case 0: /* PMULL.P8 */
10881             if (!fp_access_check(s)) {
10882                 return;
10883             }
10884             /* The Q field specifies lo/hi half input for this insn.  */
10885             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10886                              gen_helper_neon_pmull_h);
10887             break;
10888 
10889         case 3: /* PMULL.P64 */
10890             if (!dc_isar_feature(aa64_pmull, s)) {
10891                 unallocated_encoding(s);
10892                 return;
10893             }
10894             if (!fp_access_check(s)) {
10895                 return;
10896             }
10897             /* The Q field specifies lo/hi half input for this insn.  */
10898             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10899                              gen_helper_gvec_pmull_q);
10900             break;
10901 
10902         default:
10903             unallocated_encoding(s);
10904             break;
10905         }
10906         return;
10907     case 9: /* SQDMLAL, SQDMLAL2 */
10908     case 11: /* SQDMLSL, SQDMLSL2 */
10909     case 13: /* SQDMULL, SQDMULL2 */
10910         if (is_u || size == 0) {
10911             unallocated_encoding(s);
10912             return;
10913         }
10914         /* fall through */
10915     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10916     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10917     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10918     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10919     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10920     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10921     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10922         /* 64 x 64 -> 128 */
10923         if (size == 3) {
10924             unallocated_encoding(s);
10925             return;
10926         }
10927         if (!fp_access_check(s)) {
10928             return;
10929         }
10930 
10931         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10932         break;
10933     default:
10934         /* opcode 15 not allocated */
10935         unallocated_encoding(s);
10936         break;
10937     }
10938 }
10939 
10940 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10941 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10942 {
10943     int rd = extract32(insn, 0, 5);
10944     int rn = extract32(insn, 5, 5);
10945     int rm = extract32(insn, 16, 5);
10946     int size = extract32(insn, 22, 2);
10947     bool is_u = extract32(insn, 29, 1);
10948     bool is_q = extract32(insn, 30, 1);
10949 
10950     if (!fp_access_check(s)) {
10951         return;
10952     }
10953 
10954     switch (size + 4 * is_u) {
10955     case 0: /* AND */
10956         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10957         return;
10958     case 1: /* BIC */
10959         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10960         return;
10961     case 2: /* ORR */
10962         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10963         return;
10964     case 3: /* ORN */
10965         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10966         return;
10967     case 4: /* EOR */
10968         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10969         return;
10970 
10971     case 5: /* BSL bitwise select */
10972         gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
10973         return;
10974     case 6: /* BIT, bitwise insert if true */
10975         gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
10976         return;
10977     case 7: /* BIF, bitwise insert if false */
10978         gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
10979         return;
10980 
10981     default:
10982         g_assert_not_reached();
10983     }
10984 }
10985 
10986 /* Pairwise op subgroup of C3.6.16.
10987  *
10988  * This is called directly or via the handle_3same_float for float pairwise
10989  * operations where the opcode and size are calculated differently.
10990  */
10991 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
10992                                    int size, int rn, int rm, int rd)
10993 {
10994     TCGv_ptr fpst;
10995     int pass;
10996 
10997     /* Floating point operations need fpst */
10998     if (opcode >= 0x58) {
10999         fpst = fpstatus_ptr(FPST_FPCR);
11000     } else {
11001         fpst = NULL;
11002     }
11003 
11004     if (!fp_access_check(s)) {
11005         return;
11006     }
11007 
11008     /* These operations work on the concatenated rm:rn, with each pair of
11009      * adjacent elements being operated on to produce an element in the result.
11010      */
11011     if (size == 3) {
11012         TCGv_i64 tcg_res[2];
11013 
11014         for (pass = 0; pass < 2; pass++) {
11015             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11016             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11017             int passreg = (pass == 0) ? rn : rm;
11018 
11019             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
11020             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
11021             tcg_res[pass] = tcg_temp_new_i64();
11022 
11023             switch (opcode) {
11024             case 0x17: /* ADDP */
11025                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11026                 break;
11027             case 0x58: /* FMAXNMP */
11028                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11029                 break;
11030             case 0x5a: /* FADDP */
11031                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11032                 break;
11033             case 0x5e: /* FMAXP */
11034                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11035                 break;
11036             case 0x78: /* FMINNMP */
11037                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11038                 break;
11039             case 0x7e: /* FMINP */
11040                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11041                 break;
11042             default:
11043                 g_assert_not_reached();
11044             }
11045         }
11046 
11047         for (pass = 0; pass < 2; pass++) {
11048             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11049         }
11050     } else {
11051         int maxpass = is_q ? 4 : 2;
11052         TCGv_i32 tcg_res[4];
11053 
11054         for (pass = 0; pass < maxpass; pass++) {
11055             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11056             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11057             NeonGenTwoOpFn *genfn = NULL;
11058             int passreg = pass < (maxpass / 2) ? rn : rm;
11059             int passelt = (is_q && (pass & 1)) ? 2 : 0;
11060 
11061             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
11062             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
11063             tcg_res[pass] = tcg_temp_new_i32();
11064 
11065             switch (opcode) {
11066             case 0x17: /* ADDP */
11067             {
11068                 static NeonGenTwoOpFn * const fns[3] = {
11069                     gen_helper_neon_padd_u8,
11070                     gen_helper_neon_padd_u16,
11071                     tcg_gen_add_i32,
11072                 };
11073                 genfn = fns[size];
11074                 break;
11075             }
11076             case 0x14: /* SMAXP, UMAXP */
11077             {
11078                 static NeonGenTwoOpFn * const fns[3][2] = {
11079                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
11080                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
11081                     { tcg_gen_smax_i32, tcg_gen_umax_i32 },
11082                 };
11083                 genfn = fns[size][u];
11084                 break;
11085             }
11086             case 0x15: /* SMINP, UMINP */
11087             {
11088                 static NeonGenTwoOpFn * const fns[3][2] = {
11089                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
11090                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
11091                     { tcg_gen_smin_i32, tcg_gen_umin_i32 },
11092                 };
11093                 genfn = fns[size][u];
11094                 break;
11095             }
11096             /* The FP operations are all on single floats (32 bit) */
11097             case 0x58: /* FMAXNMP */
11098                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11099                 break;
11100             case 0x5a: /* FADDP */
11101                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11102                 break;
11103             case 0x5e: /* FMAXP */
11104                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11105                 break;
11106             case 0x78: /* FMINNMP */
11107                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11108                 break;
11109             case 0x7e: /* FMINP */
11110                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11111                 break;
11112             default:
11113                 g_assert_not_reached();
11114             }
11115 
11116             /* FP ops called directly, otherwise call now */
11117             if (genfn) {
11118                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
11119             }
11120         }
11121 
11122         for (pass = 0; pass < maxpass; pass++) {
11123             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11124         }
11125         clear_vec_high(s, is_q, rd);
11126     }
11127 }
11128 
11129 /* Floating point op subgroup of C3.6.16. */
11130 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
11131 {
11132     /* For floating point ops, the U, size[1] and opcode bits
11133      * together indicate the operation. size[0] indicates single
11134      * or double.
11135      */
11136     int fpopcode = extract32(insn, 11, 5)
11137         | (extract32(insn, 23, 1) << 5)
11138         | (extract32(insn, 29, 1) << 6);
11139     int is_q = extract32(insn, 30, 1);
11140     int size = extract32(insn, 22, 1);
11141     int rm = extract32(insn, 16, 5);
11142     int rn = extract32(insn, 5, 5);
11143     int rd = extract32(insn, 0, 5);
11144 
11145     int datasize = is_q ? 128 : 64;
11146     int esize = 32 << size;
11147     int elements = datasize / esize;
11148 
11149     if (size == 1 && !is_q) {
11150         unallocated_encoding(s);
11151         return;
11152     }
11153 
11154     switch (fpopcode) {
11155     case 0x58: /* FMAXNMP */
11156     case 0x5a: /* FADDP */
11157     case 0x5e: /* FMAXP */
11158     case 0x78: /* FMINNMP */
11159     case 0x7e: /* FMINP */
11160         if (size && !is_q) {
11161             unallocated_encoding(s);
11162             return;
11163         }
11164         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11165                                rn, rm, rd);
11166         return;
11167     case 0x1b: /* FMULX */
11168     case 0x1f: /* FRECPS */
11169     case 0x3f: /* FRSQRTS */
11170     case 0x5d: /* FACGE */
11171     case 0x7d: /* FACGT */
11172     case 0x19: /* FMLA */
11173     case 0x39: /* FMLS */
11174     case 0x18: /* FMAXNM */
11175     case 0x1a: /* FADD */
11176     case 0x1c: /* FCMEQ */
11177     case 0x1e: /* FMAX */
11178     case 0x38: /* FMINNM */
11179     case 0x3a: /* FSUB */
11180     case 0x3e: /* FMIN */
11181     case 0x5b: /* FMUL */
11182     case 0x5c: /* FCMGE */
11183     case 0x5f: /* FDIV */
11184     case 0x7a: /* FABD */
11185     case 0x7c: /* FCMGT */
11186         if (!fp_access_check(s)) {
11187             return;
11188         }
11189         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11190         return;
11191 
11192     case 0x1d: /* FMLAL  */
11193     case 0x3d: /* FMLSL  */
11194     case 0x59: /* FMLAL2 */
11195     case 0x79: /* FMLSL2 */
11196         if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11197             unallocated_encoding(s);
11198             return;
11199         }
11200         if (fp_access_check(s)) {
11201             int is_s = extract32(insn, 23, 1);
11202             int is_2 = extract32(insn, 29, 1);
11203             int data = (is_2 << 1) | is_s;
11204             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11205                                vec_full_reg_offset(s, rn),
11206                                vec_full_reg_offset(s, rm), cpu_env,
11207                                is_q ? 16 : 8, vec_full_reg_size(s),
11208                                data, gen_helper_gvec_fmlal_a64);
11209         }
11210         return;
11211 
11212     default:
11213         unallocated_encoding(s);
11214         return;
11215     }
11216 }
11217 
11218 /* Integer op subgroup of C3.6.16. */
11219 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11220 {
11221     int is_q = extract32(insn, 30, 1);
11222     int u = extract32(insn, 29, 1);
11223     int size = extract32(insn, 22, 2);
11224     int opcode = extract32(insn, 11, 5);
11225     int rm = extract32(insn, 16, 5);
11226     int rn = extract32(insn, 5, 5);
11227     int rd = extract32(insn, 0, 5);
11228     int pass;
11229     TCGCond cond;
11230 
11231     switch (opcode) {
11232     case 0x13: /* MUL, PMUL */
11233         if (u && size != 0) {
11234             unallocated_encoding(s);
11235             return;
11236         }
11237         /* fall through */
11238     case 0x0: /* SHADD, UHADD */
11239     case 0x2: /* SRHADD, URHADD */
11240     case 0x4: /* SHSUB, UHSUB */
11241     case 0xc: /* SMAX, UMAX */
11242     case 0xd: /* SMIN, UMIN */
11243     case 0xe: /* SABD, UABD */
11244     case 0xf: /* SABA, UABA */
11245     case 0x12: /* MLA, MLS */
11246         if (size == 3) {
11247             unallocated_encoding(s);
11248             return;
11249         }
11250         break;
11251     case 0x16: /* SQDMULH, SQRDMULH */
11252         if (size == 0 || size == 3) {
11253             unallocated_encoding(s);
11254             return;
11255         }
11256         break;
11257     default:
11258         if (size == 3 && !is_q) {
11259             unallocated_encoding(s);
11260             return;
11261         }
11262         break;
11263     }
11264 
11265     if (!fp_access_check(s)) {
11266         return;
11267     }
11268 
11269     switch (opcode) {
11270     case 0x01: /* SQADD, UQADD */
11271         if (u) {
11272             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11273         } else {
11274             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11275         }
11276         return;
11277     case 0x05: /* SQSUB, UQSUB */
11278         if (u) {
11279             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11280         } else {
11281             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11282         }
11283         return;
11284     case 0x08: /* SSHL, USHL */
11285         if (u) {
11286             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11287         } else {
11288             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11289         }
11290         return;
11291     case 0x0c: /* SMAX, UMAX */
11292         if (u) {
11293             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11294         } else {
11295             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11296         }
11297         return;
11298     case 0x0d: /* SMIN, UMIN */
11299         if (u) {
11300             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11301         } else {
11302             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11303         }
11304         return;
11305     case 0xe: /* SABD, UABD */
11306         if (u) {
11307             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11308         } else {
11309             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11310         }
11311         return;
11312     case 0xf: /* SABA, UABA */
11313         if (u) {
11314             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11315         } else {
11316             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11317         }
11318         return;
11319     case 0x10: /* ADD, SUB */
11320         if (u) {
11321             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11322         } else {
11323             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11324         }
11325         return;
11326     case 0x13: /* MUL, PMUL */
11327         if (!u) { /* MUL */
11328             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11329         } else {  /* PMUL */
11330             gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11331         }
11332         return;
11333     case 0x12: /* MLA, MLS */
11334         if (u) {
11335             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11336         } else {
11337             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11338         }
11339         return;
11340     case 0x16: /* SQDMULH, SQRDMULH */
11341         {
11342             static gen_helper_gvec_3_ptr * const fns[2][2] = {
11343                 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11344                 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11345             };
11346             gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11347         }
11348         return;
11349     case 0x11:
11350         if (!u) { /* CMTST */
11351             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11352             return;
11353         }
11354         /* else CMEQ */
11355         cond = TCG_COND_EQ;
11356         goto do_gvec_cmp;
11357     case 0x06: /* CMGT, CMHI */
11358         cond = u ? TCG_COND_GTU : TCG_COND_GT;
11359         goto do_gvec_cmp;
11360     case 0x07: /* CMGE, CMHS */
11361         cond = u ? TCG_COND_GEU : TCG_COND_GE;
11362     do_gvec_cmp:
11363         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11364                          vec_full_reg_offset(s, rn),
11365                          vec_full_reg_offset(s, rm),
11366                          is_q ? 16 : 8, vec_full_reg_size(s));
11367         return;
11368     }
11369 
11370     if (size == 3) {
11371         assert(is_q);
11372         for (pass = 0; pass < 2; pass++) {
11373             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11374             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11375             TCGv_i64 tcg_res = tcg_temp_new_i64();
11376 
11377             read_vec_element(s, tcg_op1, rn, pass, MO_64);
11378             read_vec_element(s, tcg_op2, rm, pass, MO_64);
11379 
11380             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11381 
11382             write_vec_element(s, tcg_res, rd, pass, MO_64);
11383         }
11384     } else {
11385         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11386             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11387             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11388             TCGv_i32 tcg_res = tcg_temp_new_i32();
11389             NeonGenTwoOpFn *genfn = NULL;
11390             NeonGenTwoOpEnvFn *genenvfn = NULL;
11391 
11392             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11393             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11394 
11395             switch (opcode) {
11396             case 0x0: /* SHADD, UHADD */
11397             {
11398                 static NeonGenTwoOpFn * const fns[3][2] = {
11399                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11400                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11401                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11402                 };
11403                 genfn = fns[size][u];
11404                 break;
11405             }
11406             case 0x2: /* SRHADD, URHADD */
11407             {
11408                 static NeonGenTwoOpFn * const fns[3][2] = {
11409                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11410                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11411                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11412                 };
11413                 genfn = fns[size][u];
11414                 break;
11415             }
11416             case 0x4: /* SHSUB, UHSUB */
11417             {
11418                 static NeonGenTwoOpFn * const fns[3][2] = {
11419                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11420                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11421                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11422                 };
11423                 genfn = fns[size][u];
11424                 break;
11425             }
11426             case 0x9: /* SQSHL, UQSHL */
11427             {
11428                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11429                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11430                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11431                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11432                 };
11433                 genenvfn = fns[size][u];
11434                 break;
11435             }
11436             case 0xa: /* SRSHL, URSHL */
11437             {
11438                 static NeonGenTwoOpFn * const fns[3][2] = {
11439                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11440                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11441                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11442                 };
11443                 genfn = fns[size][u];
11444                 break;
11445             }
11446             case 0xb: /* SQRSHL, UQRSHL */
11447             {
11448                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11449                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11450                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11451                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11452                 };
11453                 genenvfn = fns[size][u];
11454                 break;
11455             }
11456             default:
11457                 g_assert_not_reached();
11458             }
11459 
11460             if (genenvfn) {
11461                 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11462             } else {
11463                 genfn(tcg_res, tcg_op1, tcg_op2);
11464             }
11465 
11466             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11467         }
11468     }
11469     clear_vec_high(s, is_q, rd);
11470 }
11471 
11472 /* AdvSIMD three same
11473  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
11474  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11475  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
11476  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11477  */
11478 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11479 {
11480     int opcode = extract32(insn, 11, 5);
11481 
11482     switch (opcode) {
11483     case 0x3: /* logic ops */
11484         disas_simd_3same_logic(s, insn);
11485         break;
11486     case 0x17: /* ADDP */
11487     case 0x14: /* SMAXP, UMAXP */
11488     case 0x15: /* SMINP, UMINP */
11489     {
11490         /* Pairwise operations */
11491         int is_q = extract32(insn, 30, 1);
11492         int u = extract32(insn, 29, 1);
11493         int size = extract32(insn, 22, 2);
11494         int rm = extract32(insn, 16, 5);
11495         int rn = extract32(insn, 5, 5);
11496         int rd = extract32(insn, 0, 5);
11497         if (opcode == 0x17) {
11498             if (u || (size == 3 && !is_q)) {
11499                 unallocated_encoding(s);
11500                 return;
11501             }
11502         } else {
11503             if (size == 3) {
11504                 unallocated_encoding(s);
11505                 return;
11506             }
11507         }
11508         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11509         break;
11510     }
11511     case 0x18 ... 0x31:
11512         /* floating point ops, sz[1] and U are part of opcode */
11513         disas_simd_3same_float(s, insn);
11514         break;
11515     default:
11516         disas_simd_3same_int(s, insn);
11517         break;
11518     }
11519 }
11520 
11521 /*
11522  * Advanced SIMD three same (ARMv8.2 FP16 variants)
11523  *
11524  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
11525  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11526  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
11527  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11528  *
11529  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11530  * (register), FACGE, FABD, FCMGT (register) and FACGT.
11531  *
11532  */
11533 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11534 {
11535     int opcode = extract32(insn, 11, 3);
11536     int u = extract32(insn, 29, 1);
11537     int a = extract32(insn, 23, 1);
11538     int is_q = extract32(insn, 30, 1);
11539     int rm = extract32(insn, 16, 5);
11540     int rn = extract32(insn, 5, 5);
11541     int rd = extract32(insn, 0, 5);
11542     /*
11543      * For these floating point ops, the U, a and opcode bits
11544      * together indicate the operation.
11545      */
11546     int fpopcode = opcode | (a << 3) | (u << 4);
11547     int datasize = is_q ? 128 : 64;
11548     int elements = datasize / 16;
11549     bool pairwise;
11550     TCGv_ptr fpst;
11551     int pass;
11552 
11553     switch (fpopcode) {
11554     case 0x0: /* FMAXNM */
11555     case 0x1: /* FMLA */
11556     case 0x2: /* FADD */
11557     case 0x3: /* FMULX */
11558     case 0x4: /* FCMEQ */
11559     case 0x6: /* FMAX */
11560     case 0x7: /* FRECPS */
11561     case 0x8: /* FMINNM */
11562     case 0x9: /* FMLS */
11563     case 0xa: /* FSUB */
11564     case 0xe: /* FMIN */
11565     case 0xf: /* FRSQRTS */
11566     case 0x13: /* FMUL */
11567     case 0x14: /* FCMGE */
11568     case 0x15: /* FACGE */
11569     case 0x17: /* FDIV */
11570     case 0x1a: /* FABD */
11571     case 0x1c: /* FCMGT */
11572     case 0x1d: /* FACGT */
11573         pairwise = false;
11574         break;
11575     case 0x10: /* FMAXNMP */
11576     case 0x12: /* FADDP */
11577     case 0x16: /* FMAXP */
11578     case 0x18: /* FMINNMP */
11579     case 0x1e: /* FMINP */
11580         pairwise = true;
11581         break;
11582     default:
11583         unallocated_encoding(s);
11584         return;
11585     }
11586 
11587     if (!dc_isar_feature(aa64_fp16, s)) {
11588         unallocated_encoding(s);
11589         return;
11590     }
11591 
11592     if (!fp_access_check(s)) {
11593         return;
11594     }
11595 
11596     fpst = fpstatus_ptr(FPST_FPCR_F16);
11597 
11598     if (pairwise) {
11599         int maxpass = is_q ? 8 : 4;
11600         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11601         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11602         TCGv_i32 tcg_res[8];
11603 
11604         for (pass = 0; pass < maxpass; pass++) {
11605             int passreg = pass < (maxpass / 2) ? rn : rm;
11606             int passelt = (pass << 1) & (maxpass - 1);
11607 
11608             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11609             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11610             tcg_res[pass] = tcg_temp_new_i32();
11611 
11612             switch (fpopcode) {
11613             case 0x10: /* FMAXNMP */
11614                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11615                                            fpst);
11616                 break;
11617             case 0x12: /* FADDP */
11618                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11619                 break;
11620             case 0x16: /* FMAXP */
11621                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11622                 break;
11623             case 0x18: /* FMINNMP */
11624                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11625                                            fpst);
11626                 break;
11627             case 0x1e: /* FMINP */
11628                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11629                 break;
11630             default:
11631                 g_assert_not_reached();
11632             }
11633         }
11634 
11635         for (pass = 0; pass < maxpass; pass++) {
11636             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11637         }
11638     } else {
11639         for (pass = 0; pass < elements; pass++) {
11640             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11641             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11642             TCGv_i32 tcg_res = tcg_temp_new_i32();
11643 
11644             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11645             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11646 
11647             switch (fpopcode) {
11648             case 0x0: /* FMAXNM */
11649                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11650                 break;
11651             case 0x1: /* FMLA */
11652                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11653                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11654                                            fpst);
11655                 break;
11656             case 0x2: /* FADD */
11657                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11658                 break;
11659             case 0x3: /* FMULX */
11660                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11661                 break;
11662             case 0x4: /* FCMEQ */
11663                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11664                 break;
11665             case 0x6: /* FMAX */
11666                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11667                 break;
11668             case 0x7: /* FRECPS */
11669                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11670                 break;
11671             case 0x8: /* FMINNM */
11672                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11673                 break;
11674             case 0x9: /* FMLS */
11675                 /* As usual for ARM, separate negation for fused multiply-add */
11676                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11677                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11678                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11679                                            fpst);
11680                 break;
11681             case 0xa: /* FSUB */
11682                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11683                 break;
11684             case 0xe: /* FMIN */
11685                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11686                 break;
11687             case 0xf: /* FRSQRTS */
11688                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11689                 break;
11690             case 0x13: /* FMUL */
11691                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11692                 break;
11693             case 0x14: /* FCMGE */
11694                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11695                 break;
11696             case 0x15: /* FACGE */
11697                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11698                 break;
11699             case 0x17: /* FDIV */
11700                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11701                 break;
11702             case 0x1a: /* FABD */
11703                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11704                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11705                 break;
11706             case 0x1c: /* FCMGT */
11707                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11708                 break;
11709             case 0x1d: /* FACGT */
11710                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11711                 break;
11712             default:
11713                 g_assert_not_reached();
11714             }
11715 
11716             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11717         }
11718     }
11719 
11720     clear_vec_high(s, is_q, rd);
11721 }
11722 
11723 /* AdvSIMD three same extra
11724  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
11725  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11726  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
11727  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11728  */
11729 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11730 {
11731     int rd = extract32(insn, 0, 5);
11732     int rn = extract32(insn, 5, 5);
11733     int opcode = extract32(insn, 11, 4);
11734     int rm = extract32(insn, 16, 5);
11735     int size = extract32(insn, 22, 2);
11736     bool u = extract32(insn, 29, 1);
11737     bool is_q = extract32(insn, 30, 1);
11738     bool feature;
11739     int rot;
11740 
11741     switch (u * 16 + opcode) {
11742     case 0x10: /* SQRDMLAH (vector) */
11743     case 0x11: /* SQRDMLSH (vector) */
11744         if (size != 1 && size != 2) {
11745             unallocated_encoding(s);
11746             return;
11747         }
11748         feature = dc_isar_feature(aa64_rdm, s);
11749         break;
11750     case 0x02: /* SDOT (vector) */
11751     case 0x12: /* UDOT (vector) */
11752         if (size != MO_32) {
11753             unallocated_encoding(s);
11754             return;
11755         }
11756         feature = dc_isar_feature(aa64_dp, s);
11757         break;
11758     case 0x03: /* USDOT */
11759         if (size != MO_32) {
11760             unallocated_encoding(s);
11761             return;
11762         }
11763         feature = dc_isar_feature(aa64_i8mm, s);
11764         break;
11765     case 0x04: /* SMMLA */
11766     case 0x14: /* UMMLA */
11767     case 0x05: /* USMMLA */
11768         if (!is_q || size != MO_32) {
11769             unallocated_encoding(s);
11770             return;
11771         }
11772         feature = dc_isar_feature(aa64_i8mm, s);
11773         break;
11774     case 0x18: /* FCMLA, #0 */
11775     case 0x19: /* FCMLA, #90 */
11776     case 0x1a: /* FCMLA, #180 */
11777     case 0x1b: /* FCMLA, #270 */
11778     case 0x1c: /* FCADD, #90 */
11779     case 0x1e: /* FCADD, #270 */
11780         if (size == 0
11781             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11782             || (size == 3 && !is_q)) {
11783             unallocated_encoding(s);
11784             return;
11785         }
11786         feature = dc_isar_feature(aa64_fcma, s);
11787         break;
11788     case 0x1d: /* BFMMLA */
11789         if (size != MO_16 || !is_q) {
11790             unallocated_encoding(s);
11791             return;
11792         }
11793         feature = dc_isar_feature(aa64_bf16, s);
11794         break;
11795     case 0x1f:
11796         switch (size) {
11797         case 1: /* BFDOT */
11798         case 3: /* BFMLAL{B,T} */
11799             feature = dc_isar_feature(aa64_bf16, s);
11800             break;
11801         default:
11802             unallocated_encoding(s);
11803             return;
11804         }
11805         break;
11806     default:
11807         unallocated_encoding(s);
11808         return;
11809     }
11810     if (!feature) {
11811         unallocated_encoding(s);
11812         return;
11813     }
11814     if (!fp_access_check(s)) {
11815         return;
11816     }
11817 
11818     switch (opcode) {
11819     case 0x0: /* SQRDMLAH (vector) */
11820         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11821         return;
11822 
11823     case 0x1: /* SQRDMLSH (vector) */
11824         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11825         return;
11826 
11827     case 0x2: /* SDOT / UDOT */
11828         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11829                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11830         return;
11831 
11832     case 0x3: /* USDOT */
11833         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11834         return;
11835 
11836     case 0x04: /* SMMLA, UMMLA */
11837         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11838                          u ? gen_helper_gvec_ummla_b
11839                          : gen_helper_gvec_smmla_b);
11840         return;
11841     case 0x05: /* USMMLA */
11842         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11843         return;
11844 
11845     case 0x8: /* FCMLA, #0 */
11846     case 0x9: /* FCMLA, #90 */
11847     case 0xa: /* FCMLA, #180 */
11848     case 0xb: /* FCMLA, #270 */
11849         rot = extract32(opcode, 0, 2);
11850         switch (size) {
11851         case 1:
11852             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11853                               gen_helper_gvec_fcmlah);
11854             break;
11855         case 2:
11856             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11857                               gen_helper_gvec_fcmlas);
11858             break;
11859         case 3:
11860             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11861                               gen_helper_gvec_fcmlad);
11862             break;
11863         default:
11864             g_assert_not_reached();
11865         }
11866         return;
11867 
11868     case 0xc: /* FCADD, #90 */
11869     case 0xe: /* FCADD, #270 */
11870         rot = extract32(opcode, 1, 1);
11871         switch (size) {
11872         case 1:
11873             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11874                               gen_helper_gvec_fcaddh);
11875             break;
11876         case 2:
11877             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11878                               gen_helper_gvec_fcadds);
11879             break;
11880         case 3:
11881             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11882                               gen_helper_gvec_fcaddd);
11883             break;
11884         default:
11885             g_assert_not_reached();
11886         }
11887         return;
11888 
11889     case 0xd: /* BFMMLA */
11890         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11891         return;
11892     case 0xf:
11893         switch (size) {
11894         case 1: /* BFDOT */
11895             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11896             break;
11897         case 3: /* BFMLAL{B,T} */
11898             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11899                               gen_helper_gvec_bfmlal);
11900             break;
11901         default:
11902             g_assert_not_reached();
11903         }
11904         return;
11905 
11906     default:
11907         g_assert_not_reached();
11908     }
11909 }
11910 
11911 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11912                                   int size, int rn, int rd)
11913 {
11914     /* Handle 2-reg-misc ops which are widening (so each size element
11915      * in the source becomes a 2*size element in the destination.
11916      * The only instruction like this is FCVTL.
11917      */
11918     int pass;
11919 
11920     if (size == 3) {
11921         /* 32 -> 64 bit fp conversion */
11922         TCGv_i64 tcg_res[2];
11923         int srcelt = is_q ? 2 : 0;
11924 
11925         for (pass = 0; pass < 2; pass++) {
11926             TCGv_i32 tcg_op = tcg_temp_new_i32();
11927             tcg_res[pass] = tcg_temp_new_i64();
11928 
11929             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11930             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
11931         }
11932         for (pass = 0; pass < 2; pass++) {
11933             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11934         }
11935     } else {
11936         /* 16 -> 32 bit fp conversion */
11937         int srcelt = is_q ? 4 : 0;
11938         TCGv_i32 tcg_res[4];
11939         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11940         TCGv_i32 ahp = get_ahp_flag();
11941 
11942         for (pass = 0; pass < 4; pass++) {
11943             tcg_res[pass] = tcg_temp_new_i32();
11944 
11945             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11946             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11947                                            fpst, ahp);
11948         }
11949         for (pass = 0; pass < 4; pass++) {
11950             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11951         }
11952     }
11953 }
11954 
11955 static void handle_rev(DisasContext *s, int opcode, bool u,
11956                        bool is_q, int size, int rn, int rd)
11957 {
11958     int op = (opcode << 1) | u;
11959     int opsz = op + size;
11960     int grp_size = 3 - opsz;
11961     int dsize = is_q ? 128 : 64;
11962     int i;
11963 
11964     if (opsz >= 3) {
11965         unallocated_encoding(s);
11966         return;
11967     }
11968 
11969     if (!fp_access_check(s)) {
11970         return;
11971     }
11972 
11973     if (size == 0) {
11974         /* Special case bytes, use bswap op on each group of elements */
11975         int groups = dsize / (8 << grp_size);
11976 
11977         for (i = 0; i < groups; i++) {
11978             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11979 
11980             read_vec_element(s, tcg_tmp, rn, i, grp_size);
11981             switch (grp_size) {
11982             case MO_16:
11983                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11984                 break;
11985             case MO_32:
11986                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11987                 break;
11988             case MO_64:
11989                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11990                 break;
11991             default:
11992                 g_assert_not_reached();
11993             }
11994             write_vec_element(s, tcg_tmp, rd, i, grp_size);
11995         }
11996         clear_vec_high(s, is_q, rd);
11997     } else {
11998         int revmask = (1 << grp_size) - 1;
11999         int esize = 8 << size;
12000         int elements = dsize / esize;
12001         TCGv_i64 tcg_rn = tcg_temp_new_i64();
12002         TCGv_i64 tcg_rd = tcg_const_i64(0);
12003         TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
12004 
12005         for (i = 0; i < elements; i++) {
12006             int e_rev = (i & 0xf) ^ revmask;
12007             int off = e_rev * esize;
12008             read_vec_element(s, tcg_rn, rn, i, size);
12009             if (off >= 64) {
12010                 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
12011                                     tcg_rn, off - 64, esize);
12012             } else {
12013                 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
12014             }
12015         }
12016         write_vec_element(s, tcg_rd, rd, 0, MO_64);
12017         write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
12018     }
12019 }
12020 
12021 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
12022                                   bool is_q, int size, int rn, int rd)
12023 {
12024     /* Implement the pairwise operations from 2-misc:
12025      * SADDLP, UADDLP, SADALP, UADALP.
12026      * These all add pairs of elements in the input to produce a
12027      * double-width result element in the output (possibly accumulating).
12028      */
12029     bool accum = (opcode == 0x6);
12030     int maxpass = is_q ? 2 : 1;
12031     int pass;
12032     TCGv_i64 tcg_res[2];
12033 
12034     if (size == 2) {
12035         /* 32 + 32 -> 64 op */
12036         MemOp memop = size + (u ? 0 : MO_SIGN);
12037 
12038         for (pass = 0; pass < maxpass; pass++) {
12039             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
12040             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
12041 
12042             tcg_res[pass] = tcg_temp_new_i64();
12043 
12044             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
12045             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
12046             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
12047             if (accum) {
12048                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
12049                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12050             }
12051         }
12052     } else {
12053         for (pass = 0; pass < maxpass; pass++) {
12054             TCGv_i64 tcg_op = tcg_temp_new_i64();
12055             NeonGenOne64OpFn *genfn;
12056             static NeonGenOne64OpFn * const fns[2][2] = {
12057                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
12058                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
12059             };
12060 
12061             genfn = fns[size][u];
12062 
12063             tcg_res[pass] = tcg_temp_new_i64();
12064 
12065             read_vec_element(s, tcg_op, rn, pass, MO_64);
12066             genfn(tcg_res[pass], tcg_op);
12067 
12068             if (accum) {
12069                 read_vec_element(s, tcg_op, rd, pass, MO_64);
12070                 if (size == 0) {
12071                     gen_helper_neon_addl_u16(tcg_res[pass],
12072                                              tcg_res[pass], tcg_op);
12073                 } else {
12074                     gen_helper_neon_addl_u32(tcg_res[pass],
12075                                              tcg_res[pass], tcg_op);
12076                 }
12077             }
12078         }
12079     }
12080     if (!is_q) {
12081         tcg_res[1] = tcg_constant_i64(0);
12082     }
12083     for (pass = 0; pass < 2; pass++) {
12084         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12085     }
12086 }
12087 
12088 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
12089 {
12090     /* Implement SHLL and SHLL2 */
12091     int pass;
12092     int part = is_q ? 2 : 0;
12093     TCGv_i64 tcg_res[2];
12094 
12095     for (pass = 0; pass < 2; pass++) {
12096         static NeonGenWidenFn * const widenfns[3] = {
12097             gen_helper_neon_widen_u8,
12098             gen_helper_neon_widen_u16,
12099             tcg_gen_extu_i32_i64,
12100         };
12101         NeonGenWidenFn *widenfn = widenfns[size];
12102         TCGv_i32 tcg_op = tcg_temp_new_i32();
12103 
12104         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
12105         tcg_res[pass] = tcg_temp_new_i64();
12106         widenfn(tcg_res[pass], tcg_op);
12107         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
12108     }
12109 
12110     for (pass = 0; pass < 2; pass++) {
12111         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12112     }
12113 }
12114 
12115 /* AdvSIMD two reg misc
12116  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
12117  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12118  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12119  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12120  */
12121 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
12122 {
12123     int size = extract32(insn, 22, 2);
12124     int opcode = extract32(insn, 12, 5);
12125     bool u = extract32(insn, 29, 1);
12126     bool is_q = extract32(insn, 30, 1);
12127     int rn = extract32(insn, 5, 5);
12128     int rd = extract32(insn, 0, 5);
12129     bool need_fpstatus = false;
12130     int rmode = -1;
12131     TCGv_i32 tcg_rmode;
12132     TCGv_ptr tcg_fpstatus;
12133 
12134     switch (opcode) {
12135     case 0x0: /* REV64, REV32 */
12136     case 0x1: /* REV16 */
12137         handle_rev(s, opcode, u, is_q, size, rn, rd);
12138         return;
12139     case 0x5: /* CNT, NOT, RBIT */
12140         if (u && size == 0) {
12141             /* NOT */
12142             break;
12143         } else if (u && size == 1) {
12144             /* RBIT */
12145             break;
12146         } else if (!u && size == 0) {
12147             /* CNT */
12148             break;
12149         }
12150         unallocated_encoding(s);
12151         return;
12152     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12153     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12154         if (size == 3) {
12155             unallocated_encoding(s);
12156             return;
12157         }
12158         if (!fp_access_check(s)) {
12159             return;
12160         }
12161 
12162         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
12163         return;
12164     case 0x4: /* CLS, CLZ */
12165         if (size == 3) {
12166             unallocated_encoding(s);
12167             return;
12168         }
12169         break;
12170     case 0x2: /* SADDLP, UADDLP */
12171     case 0x6: /* SADALP, UADALP */
12172         if (size == 3) {
12173             unallocated_encoding(s);
12174             return;
12175         }
12176         if (!fp_access_check(s)) {
12177             return;
12178         }
12179         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
12180         return;
12181     case 0x13: /* SHLL, SHLL2 */
12182         if (u == 0 || size == 3) {
12183             unallocated_encoding(s);
12184             return;
12185         }
12186         if (!fp_access_check(s)) {
12187             return;
12188         }
12189         handle_shll(s, is_q, size, rn, rd);
12190         return;
12191     case 0xa: /* CMLT */
12192         if (u == 1) {
12193             unallocated_encoding(s);
12194             return;
12195         }
12196         /* fall through */
12197     case 0x8: /* CMGT, CMGE */
12198     case 0x9: /* CMEQ, CMLE */
12199     case 0xb: /* ABS, NEG */
12200         if (size == 3 && !is_q) {
12201             unallocated_encoding(s);
12202             return;
12203         }
12204         break;
12205     case 0x3: /* SUQADD, USQADD */
12206         if (size == 3 && !is_q) {
12207             unallocated_encoding(s);
12208             return;
12209         }
12210         if (!fp_access_check(s)) {
12211             return;
12212         }
12213         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12214         return;
12215     case 0x7: /* SQABS, SQNEG */
12216         if (size == 3 && !is_q) {
12217             unallocated_encoding(s);
12218             return;
12219         }
12220         break;
12221     case 0xc ... 0xf:
12222     case 0x16 ... 0x1f:
12223     {
12224         /* Floating point: U, size[1] and opcode indicate operation;
12225          * size[0] indicates single or double precision.
12226          */
12227         int is_double = extract32(size, 0, 1);
12228         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12229         size = is_double ? 3 : 2;
12230         switch (opcode) {
12231         case 0x2f: /* FABS */
12232         case 0x6f: /* FNEG */
12233             if (size == 3 && !is_q) {
12234                 unallocated_encoding(s);
12235                 return;
12236             }
12237             break;
12238         case 0x1d: /* SCVTF */
12239         case 0x5d: /* UCVTF */
12240         {
12241             bool is_signed = (opcode == 0x1d) ? true : false;
12242             int elements = is_double ? 2 : is_q ? 4 : 2;
12243             if (is_double && !is_q) {
12244                 unallocated_encoding(s);
12245                 return;
12246             }
12247             if (!fp_access_check(s)) {
12248                 return;
12249             }
12250             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12251             return;
12252         }
12253         case 0x2c: /* FCMGT (zero) */
12254         case 0x2d: /* FCMEQ (zero) */
12255         case 0x2e: /* FCMLT (zero) */
12256         case 0x6c: /* FCMGE (zero) */
12257         case 0x6d: /* FCMLE (zero) */
12258             if (size == 3 && !is_q) {
12259                 unallocated_encoding(s);
12260                 return;
12261             }
12262             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12263             return;
12264         case 0x7f: /* FSQRT */
12265             if (size == 3 && !is_q) {
12266                 unallocated_encoding(s);
12267                 return;
12268             }
12269             break;
12270         case 0x1a: /* FCVTNS */
12271         case 0x1b: /* FCVTMS */
12272         case 0x3a: /* FCVTPS */
12273         case 0x3b: /* FCVTZS */
12274         case 0x5a: /* FCVTNU */
12275         case 0x5b: /* FCVTMU */
12276         case 0x7a: /* FCVTPU */
12277         case 0x7b: /* FCVTZU */
12278             need_fpstatus = true;
12279             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12280             if (size == 3 && !is_q) {
12281                 unallocated_encoding(s);
12282                 return;
12283             }
12284             break;
12285         case 0x5c: /* FCVTAU */
12286         case 0x1c: /* FCVTAS */
12287             need_fpstatus = true;
12288             rmode = FPROUNDING_TIEAWAY;
12289             if (size == 3 && !is_q) {
12290                 unallocated_encoding(s);
12291                 return;
12292             }
12293             break;
12294         case 0x3c: /* URECPE */
12295             if (size == 3) {
12296                 unallocated_encoding(s);
12297                 return;
12298             }
12299             /* fall through */
12300         case 0x3d: /* FRECPE */
12301         case 0x7d: /* FRSQRTE */
12302             if (size == 3 && !is_q) {
12303                 unallocated_encoding(s);
12304                 return;
12305             }
12306             if (!fp_access_check(s)) {
12307                 return;
12308             }
12309             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12310             return;
12311         case 0x56: /* FCVTXN, FCVTXN2 */
12312             if (size == 2) {
12313                 unallocated_encoding(s);
12314                 return;
12315             }
12316             /* fall through */
12317         case 0x16: /* FCVTN, FCVTN2 */
12318             /* handle_2misc_narrow does a 2*size -> size operation, but these
12319              * instructions encode the source size rather than dest size.
12320              */
12321             if (!fp_access_check(s)) {
12322                 return;
12323             }
12324             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12325             return;
12326         case 0x36: /* BFCVTN, BFCVTN2 */
12327             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
12328                 unallocated_encoding(s);
12329                 return;
12330             }
12331             if (!fp_access_check(s)) {
12332                 return;
12333             }
12334             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12335             return;
12336         case 0x17: /* FCVTL, FCVTL2 */
12337             if (!fp_access_check(s)) {
12338                 return;
12339             }
12340             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12341             return;
12342         case 0x18: /* FRINTN */
12343         case 0x19: /* FRINTM */
12344         case 0x38: /* FRINTP */
12345         case 0x39: /* FRINTZ */
12346             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12347             /* fall through */
12348         case 0x59: /* FRINTX */
12349         case 0x79: /* FRINTI */
12350             need_fpstatus = true;
12351             if (size == 3 && !is_q) {
12352                 unallocated_encoding(s);
12353                 return;
12354             }
12355             break;
12356         case 0x58: /* FRINTA */
12357             rmode = FPROUNDING_TIEAWAY;
12358             need_fpstatus = true;
12359             if (size == 3 && !is_q) {
12360                 unallocated_encoding(s);
12361                 return;
12362             }
12363             break;
12364         case 0x7c: /* URSQRTE */
12365             if (size == 3) {
12366                 unallocated_encoding(s);
12367                 return;
12368             }
12369             break;
12370         case 0x1e: /* FRINT32Z */
12371         case 0x1f: /* FRINT64Z */
12372             rmode = FPROUNDING_ZERO;
12373             /* fall through */
12374         case 0x5e: /* FRINT32X */
12375         case 0x5f: /* FRINT64X */
12376             need_fpstatus = true;
12377             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12378                 unallocated_encoding(s);
12379                 return;
12380             }
12381             break;
12382         default:
12383             unallocated_encoding(s);
12384             return;
12385         }
12386         break;
12387     }
12388     default:
12389         unallocated_encoding(s);
12390         return;
12391     }
12392 
12393     if (!fp_access_check(s)) {
12394         return;
12395     }
12396 
12397     if (need_fpstatus || rmode >= 0) {
12398         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12399     } else {
12400         tcg_fpstatus = NULL;
12401     }
12402     if (rmode >= 0) {
12403         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12404     } else {
12405         tcg_rmode = NULL;
12406     }
12407 
12408     switch (opcode) {
12409     case 0x5:
12410         if (u && size == 0) { /* NOT */
12411             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12412             return;
12413         }
12414         break;
12415     case 0x8: /* CMGT, CMGE */
12416         if (u) {
12417             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12418         } else {
12419             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12420         }
12421         return;
12422     case 0x9: /* CMEQ, CMLE */
12423         if (u) {
12424             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12425         } else {
12426             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12427         }
12428         return;
12429     case 0xa: /* CMLT */
12430         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12431         return;
12432     case 0xb:
12433         if (u) { /* ABS, NEG */
12434             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12435         } else {
12436             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12437         }
12438         return;
12439     }
12440 
12441     if (size == 3) {
12442         /* All 64-bit element operations can be shared with scalar 2misc */
12443         int pass;
12444 
12445         /* Coverity claims (size == 3 && !is_q) has been eliminated
12446          * from all paths leading to here.
12447          */
12448         tcg_debug_assert(is_q);
12449         for (pass = 0; pass < 2; pass++) {
12450             TCGv_i64 tcg_op = tcg_temp_new_i64();
12451             TCGv_i64 tcg_res = tcg_temp_new_i64();
12452 
12453             read_vec_element(s, tcg_op, rn, pass, MO_64);
12454 
12455             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12456                             tcg_rmode, tcg_fpstatus);
12457 
12458             write_vec_element(s, tcg_res, rd, pass, MO_64);
12459         }
12460     } else {
12461         int pass;
12462 
12463         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12464             TCGv_i32 tcg_op = tcg_temp_new_i32();
12465             TCGv_i32 tcg_res = tcg_temp_new_i32();
12466 
12467             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12468 
12469             if (size == 2) {
12470                 /* Special cases for 32 bit elements */
12471                 switch (opcode) {
12472                 case 0x4: /* CLS */
12473                     if (u) {
12474                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12475                     } else {
12476                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
12477                     }
12478                     break;
12479                 case 0x7: /* SQABS, SQNEG */
12480                     if (u) {
12481                         gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12482                     } else {
12483                         gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12484                     }
12485                     break;
12486                 case 0x2f: /* FABS */
12487                     gen_helper_vfp_abss(tcg_res, tcg_op);
12488                     break;
12489                 case 0x6f: /* FNEG */
12490                     gen_helper_vfp_negs(tcg_res, tcg_op);
12491                     break;
12492                 case 0x7f: /* FSQRT */
12493                     gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12494                     break;
12495                 case 0x1a: /* FCVTNS */
12496                 case 0x1b: /* FCVTMS */
12497                 case 0x1c: /* FCVTAS */
12498                 case 0x3a: /* FCVTPS */
12499                 case 0x3b: /* FCVTZS */
12500                     gen_helper_vfp_tosls(tcg_res, tcg_op,
12501                                          tcg_constant_i32(0), tcg_fpstatus);
12502                     break;
12503                 case 0x5a: /* FCVTNU */
12504                 case 0x5b: /* FCVTMU */
12505                 case 0x5c: /* FCVTAU */
12506                 case 0x7a: /* FCVTPU */
12507                 case 0x7b: /* FCVTZU */
12508                     gen_helper_vfp_touls(tcg_res, tcg_op,
12509                                          tcg_constant_i32(0), tcg_fpstatus);
12510                     break;
12511                 case 0x18: /* FRINTN */
12512                 case 0x19: /* FRINTM */
12513                 case 0x38: /* FRINTP */
12514                 case 0x39: /* FRINTZ */
12515                 case 0x58: /* FRINTA */
12516                 case 0x79: /* FRINTI */
12517                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12518                     break;
12519                 case 0x59: /* FRINTX */
12520                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12521                     break;
12522                 case 0x7c: /* URSQRTE */
12523                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
12524                     break;
12525                 case 0x1e: /* FRINT32Z */
12526                 case 0x5e: /* FRINT32X */
12527                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12528                     break;
12529                 case 0x1f: /* FRINT64Z */
12530                 case 0x5f: /* FRINT64X */
12531                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12532                     break;
12533                 default:
12534                     g_assert_not_reached();
12535                 }
12536             } else {
12537                 /* Use helpers for 8 and 16 bit elements */
12538                 switch (opcode) {
12539                 case 0x5: /* CNT, RBIT */
12540                     /* For these two insns size is part of the opcode specifier
12541                      * (handled earlier); they always operate on byte elements.
12542                      */
12543                     if (u) {
12544                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12545                     } else {
12546                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12547                     }
12548                     break;
12549                 case 0x7: /* SQABS, SQNEG */
12550                 {
12551                     NeonGenOneOpEnvFn *genfn;
12552                     static NeonGenOneOpEnvFn * const fns[2][2] = {
12553                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12554                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12555                     };
12556                     genfn = fns[size][u];
12557                     genfn(tcg_res, cpu_env, tcg_op);
12558                     break;
12559                 }
12560                 case 0x4: /* CLS, CLZ */
12561                     if (u) {
12562                         if (size == 0) {
12563                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
12564                         } else {
12565                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
12566                         }
12567                     } else {
12568                         if (size == 0) {
12569                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
12570                         } else {
12571                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
12572                         }
12573                     }
12574                     break;
12575                 default:
12576                     g_assert_not_reached();
12577                 }
12578             }
12579 
12580             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12581         }
12582     }
12583     clear_vec_high(s, is_q, rd);
12584 
12585     if (tcg_rmode) {
12586         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12587     }
12588 }
12589 
12590 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12591  *
12592  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
12593  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12594  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12595  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12596  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12597  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12598  *
12599  * This actually covers two groups where scalar access is governed by
12600  * bit 28. A bunch of the instructions (float to integral) only exist
12601  * in the vector form and are un-allocated for the scalar decode. Also
12602  * in the scalar decode Q is always 1.
12603  */
12604 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12605 {
12606     int fpop, opcode, a, u;
12607     int rn, rd;
12608     bool is_q;
12609     bool is_scalar;
12610     bool only_in_vector = false;
12611 
12612     int pass;
12613     TCGv_i32 tcg_rmode = NULL;
12614     TCGv_ptr tcg_fpstatus = NULL;
12615     bool need_fpst = true;
12616     int rmode = -1;
12617 
12618     if (!dc_isar_feature(aa64_fp16, s)) {
12619         unallocated_encoding(s);
12620         return;
12621     }
12622 
12623     rd = extract32(insn, 0, 5);
12624     rn = extract32(insn, 5, 5);
12625 
12626     a = extract32(insn, 23, 1);
12627     u = extract32(insn, 29, 1);
12628     is_scalar = extract32(insn, 28, 1);
12629     is_q = extract32(insn, 30, 1);
12630 
12631     opcode = extract32(insn, 12, 5);
12632     fpop = deposit32(opcode, 5, 1, a);
12633     fpop = deposit32(fpop, 6, 1, u);
12634 
12635     switch (fpop) {
12636     case 0x1d: /* SCVTF */
12637     case 0x5d: /* UCVTF */
12638     {
12639         int elements;
12640 
12641         if (is_scalar) {
12642             elements = 1;
12643         } else {
12644             elements = (is_q ? 8 : 4);
12645         }
12646 
12647         if (!fp_access_check(s)) {
12648             return;
12649         }
12650         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12651         return;
12652     }
12653     break;
12654     case 0x2c: /* FCMGT (zero) */
12655     case 0x2d: /* FCMEQ (zero) */
12656     case 0x2e: /* FCMLT (zero) */
12657     case 0x6c: /* FCMGE (zero) */
12658     case 0x6d: /* FCMLE (zero) */
12659         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12660         return;
12661     case 0x3d: /* FRECPE */
12662     case 0x3f: /* FRECPX */
12663         break;
12664     case 0x18: /* FRINTN */
12665         only_in_vector = true;
12666         rmode = FPROUNDING_TIEEVEN;
12667         break;
12668     case 0x19: /* FRINTM */
12669         only_in_vector = true;
12670         rmode = FPROUNDING_NEGINF;
12671         break;
12672     case 0x38: /* FRINTP */
12673         only_in_vector = true;
12674         rmode = FPROUNDING_POSINF;
12675         break;
12676     case 0x39: /* FRINTZ */
12677         only_in_vector = true;
12678         rmode = FPROUNDING_ZERO;
12679         break;
12680     case 0x58: /* FRINTA */
12681         only_in_vector = true;
12682         rmode = FPROUNDING_TIEAWAY;
12683         break;
12684     case 0x59: /* FRINTX */
12685     case 0x79: /* FRINTI */
12686         only_in_vector = true;
12687         /* current rounding mode */
12688         break;
12689     case 0x1a: /* FCVTNS */
12690         rmode = FPROUNDING_TIEEVEN;
12691         break;
12692     case 0x1b: /* FCVTMS */
12693         rmode = FPROUNDING_NEGINF;
12694         break;
12695     case 0x1c: /* FCVTAS */
12696         rmode = FPROUNDING_TIEAWAY;
12697         break;
12698     case 0x3a: /* FCVTPS */
12699         rmode = FPROUNDING_POSINF;
12700         break;
12701     case 0x3b: /* FCVTZS */
12702         rmode = FPROUNDING_ZERO;
12703         break;
12704     case 0x5a: /* FCVTNU */
12705         rmode = FPROUNDING_TIEEVEN;
12706         break;
12707     case 0x5b: /* FCVTMU */
12708         rmode = FPROUNDING_NEGINF;
12709         break;
12710     case 0x5c: /* FCVTAU */
12711         rmode = FPROUNDING_TIEAWAY;
12712         break;
12713     case 0x7a: /* FCVTPU */
12714         rmode = FPROUNDING_POSINF;
12715         break;
12716     case 0x7b: /* FCVTZU */
12717         rmode = FPROUNDING_ZERO;
12718         break;
12719     case 0x2f: /* FABS */
12720     case 0x6f: /* FNEG */
12721         need_fpst = false;
12722         break;
12723     case 0x7d: /* FRSQRTE */
12724     case 0x7f: /* FSQRT (vector) */
12725         break;
12726     default:
12727         unallocated_encoding(s);
12728         return;
12729     }
12730 
12731 
12732     /* Check additional constraints for the scalar encoding */
12733     if (is_scalar) {
12734         if (!is_q) {
12735             unallocated_encoding(s);
12736             return;
12737         }
12738         /* FRINTxx is only in the vector form */
12739         if (only_in_vector) {
12740             unallocated_encoding(s);
12741             return;
12742         }
12743     }
12744 
12745     if (!fp_access_check(s)) {
12746         return;
12747     }
12748 
12749     if (rmode >= 0 || need_fpst) {
12750         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
12751     }
12752 
12753     if (rmode >= 0) {
12754         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12755     }
12756 
12757     if (is_scalar) {
12758         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12759         TCGv_i32 tcg_res = tcg_temp_new_i32();
12760 
12761         switch (fpop) {
12762         case 0x1a: /* FCVTNS */
12763         case 0x1b: /* FCVTMS */
12764         case 0x1c: /* FCVTAS */
12765         case 0x3a: /* FCVTPS */
12766         case 0x3b: /* FCVTZS */
12767             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12768             break;
12769         case 0x3d: /* FRECPE */
12770             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12771             break;
12772         case 0x3f: /* FRECPX */
12773             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12774             break;
12775         case 0x5a: /* FCVTNU */
12776         case 0x5b: /* FCVTMU */
12777         case 0x5c: /* FCVTAU */
12778         case 0x7a: /* FCVTPU */
12779         case 0x7b: /* FCVTZU */
12780             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12781             break;
12782         case 0x6f: /* FNEG */
12783             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12784             break;
12785         case 0x7d: /* FRSQRTE */
12786             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12787             break;
12788         default:
12789             g_assert_not_reached();
12790         }
12791 
12792         /* limit any sign extension going on */
12793         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12794         write_fp_sreg(s, rd, tcg_res);
12795     } else {
12796         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12797             TCGv_i32 tcg_op = tcg_temp_new_i32();
12798             TCGv_i32 tcg_res = tcg_temp_new_i32();
12799 
12800             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12801 
12802             switch (fpop) {
12803             case 0x1a: /* FCVTNS */
12804             case 0x1b: /* FCVTMS */
12805             case 0x1c: /* FCVTAS */
12806             case 0x3a: /* FCVTPS */
12807             case 0x3b: /* FCVTZS */
12808                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12809                 break;
12810             case 0x3d: /* FRECPE */
12811                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12812                 break;
12813             case 0x5a: /* FCVTNU */
12814             case 0x5b: /* FCVTMU */
12815             case 0x5c: /* FCVTAU */
12816             case 0x7a: /* FCVTPU */
12817             case 0x7b: /* FCVTZU */
12818                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12819                 break;
12820             case 0x18: /* FRINTN */
12821             case 0x19: /* FRINTM */
12822             case 0x38: /* FRINTP */
12823             case 0x39: /* FRINTZ */
12824             case 0x58: /* FRINTA */
12825             case 0x79: /* FRINTI */
12826                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12827                 break;
12828             case 0x59: /* FRINTX */
12829                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12830                 break;
12831             case 0x2f: /* FABS */
12832                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12833                 break;
12834             case 0x6f: /* FNEG */
12835                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12836                 break;
12837             case 0x7d: /* FRSQRTE */
12838                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12839                 break;
12840             case 0x7f: /* FSQRT */
12841                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12842                 break;
12843             default:
12844                 g_assert_not_reached();
12845             }
12846 
12847             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12848         }
12849 
12850         clear_vec_high(s, is_q, rd);
12851     }
12852 
12853     if (tcg_rmode) {
12854         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12855     }
12856 }
12857 
12858 /* AdvSIMD scalar x indexed element
12859  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12860  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12861  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12862  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12863  * AdvSIMD vector x indexed element
12864  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12865  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12866  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12867  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12868  */
12869 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12870 {
12871     /* This encoding has two kinds of instruction:
12872      *  normal, where we perform elt x idxelt => elt for each
12873      *     element in the vector
12874      *  long, where we perform elt x idxelt and generate a result of
12875      *     double the width of the input element
12876      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12877      */
12878     bool is_scalar = extract32(insn, 28, 1);
12879     bool is_q = extract32(insn, 30, 1);
12880     bool u = extract32(insn, 29, 1);
12881     int size = extract32(insn, 22, 2);
12882     int l = extract32(insn, 21, 1);
12883     int m = extract32(insn, 20, 1);
12884     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12885     int rm = extract32(insn, 16, 4);
12886     int opcode = extract32(insn, 12, 4);
12887     int h = extract32(insn, 11, 1);
12888     int rn = extract32(insn, 5, 5);
12889     int rd = extract32(insn, 0, 5);
12890     bool is_long = false;
12891     int is_fp = 0;
12892     bool is_fp16 = false;
12893     int index;
12894     TCGv_ptr fpst;
12895 
12896     switch (16 * u + opcode) {
12897     case 0x08: /* MUL */
12898     case 0x10: /* MLA */
12899     case 0x14: /* MLS */
12900         if (is_scalar) {
12901             unallocated_encoding(s);
12902             return;
12903         }
12904         break;
12905     case 0x02: /* SMLAL, SMLAL2 */
12906     case 0x12: /* UMLAL, UMLAL2 */
12907     case 0x06: /* SMLSL, SMLSL2 */
12908     case 0x16: /* UMLSL, UMLSL2 */
12909     case 0x0a: /* SMULL, SMULL2 */
12910     case 0x1a: /* UMULL, UMULL2 */
12911         if (is_scalar) {
12912             unallocated_encoding(s);
12913             return;
12914         }
12915         is_long = true;
12916         break;
12917     case 0x03: /* SQDMLAL, SQDMLAL2 */
12918     case 0x07: /* SQDMLSL, SQDMLSL2 */
12919     case 0x0b: /* SQDMULL, SQDMULL2 */
12920         is_long = true;
12921         break;
12922     case 0x0c: /* SQDMULH */
12923     case 0x0d: /* SQRDMULH */
12924         break;
12925     case 0x01: /* FMLA */
12926     case 0x05: /* FMLS */
12927     case 0x09: /* FMUL */
12928     case 0x19: /* FMULX */
12929         is_fp = 1;
12930         break;
12931     case 0x1d: /* SQRDMLAH */
12932     case 0x1f: /* SQRDMLSH */
12933         if (!dc_isar_feature(aa64_rdm, s)) {
12934             unallocated_encoding(s);
12935             return;
12936         }
12937         break;
12938     case 0x0e: /* SDOT */
12939     case 0x1e: /* UDOT */
12940         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12941             unallocated_encoding(s);
12942             return;
12943         }
12944         break;
12945     case 0x0f:
12946         switch (size) {
12947         case 0: /* SUDOT */
12948         case 2: /* USDOT */
12949             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12950                 unallocated_encoding(s);
12951                 return;
12952             }
12953             size = MO_32;
12954             break;
12955         case 1: /* BFDOT */
12956             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12957                 unallocated_encoding(s);
12958                 return;
12959             }
12960             size = MO_32;
12961             break;
12962         case 3: /* BFMLAL{B,T} */
12963             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12964                 unallocated_encoding(s);
12965                 return;
12966             }
12967             /* can't set is_fp without other incorrect size checks */
12968             size = MO_16;
12969             break;
12970         default:
12971             unallocated_encoding(s);
12972             return;
12973         }
12974         break;
12975     case 0x11: /* FCMLA #0 */
12976     case 0x13: /* FCMLA #90 */
12977     case 0x15: /* FCMLA #180 */
12978     case 0x17: /* FCMLA #270 */
12979         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
12980             unallocated_encoding(s);
12981             return;
12982         }
12983         is_fp = 2;
12984         break;
12985     case 0x00: /* FMLAL */
12986     case 0x04: /* FMLSL */
12987     case 0x18: /* FMLAL2 */
12988     case 0x1c: /* FMLSL2 */
12989         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
12990             unallocated_encoding(s);
12991             return;
12992         }
12993         size = MO_16;
12994         /* is_fp, but we pass cpu_env not fp_status.  */
12995         break;
12996     default:
12997         unallocated_encoding(s);
12998         return;
12999     }
13000 
13001     switch (is_fp) {
13002     case 1: /* normal fp */
13003         /* convert insn encoded size to MemOp size */
13004         switch (size) {
13005         case 0: /* half-precision */
13006             size = MO_16;
13007             is_fp16 = true;
13008             break;
13009         case MO_32: /* single precision */
13010         case MO_64: /* double precision */
13011             break;
13012         default:
13013             unallocated_encoding(s);
13014             return;
13015         }
13016         break;
13017 
13018     case 2: /* complex fp */
13019         /* Each indexable element is a complex pair.  */
13020         size += 1;
13021         switch (size) {
13022         case MO_32:
13023             if (h && !is_q) {
13024                 unallocated_encoding(s);
13025                 return;
13026             }
13027             is_fp16 = true;
13028             break;
13029         case MO_64:
13030             break;
13031         default:
13032             unallocated_encoding(s);
13033             return;
13034         }
13035         break;
13036 
13037     default: /* integer */
13038         switch (size) {
13039         case MO_8:
13040         case MO_64:
13041             unallocated_encoding(s);
13042             return;
13043         }
13044         break;
13045     }
13046     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
13047         unallocated_encoding(s);
13048         return;
13049     }
13050 
13051     /* Given MemOp size, adjust register and indexing.  */
13052     switch (size) {
13053     case MO_16:
13054         index = h << 2 | l << 1 | m;
13055         break;
13056     case MO_32:
13057         index = h << 1 | l;
13058         rm |= m << 4;
13059         break;
13060     case MO_64:
13061         if (l || !is_q) {
13062             unallocated_encoding(s);
13063             return;
13064         }
13065         index = h;
13066         rm |= m << 4;
13067         break;
13068     default:
13069         g_assert_not_reached();
13070     }
13071 
13072     if (!fp_access_check(s)) {
13073         return;
13074     }
13075 
13076     if (is_fp) {
13077         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
13078     } else {
13079         fpst = NULL;
13080     }
13081 
13082     switch (16 * u + opcode) {
13083     case 0x0e: /* SDOT */
13084     case 0x1e: /* UDOT */
13085         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13086                          u ? gen_helper_gvec_udot_idx_b
13087                          : gen_helper_gvec_sdot_idx_b);
13088         return;
13089     case 0x0f:
13090         switch (extract32(insn, 22, 2)) {
13091         case 0: /* SUDOT */
13092             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13093                              gen_helper_gvec_sudot_idx_b);
13094             return;
13095         case 1: /* BFDOT */
13096             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13097                              gen_helper_gvec_bfdot_idx);
13098             return;
13099         case 2: /* USDOT */
13100             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13101                              gen_helper_gvec_usdot_idx_b);
13102             return;
13103         case 3: /* BFMLAL{B,T} */
13104             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
13105                               gen_helper_gvec_bfmlal_idx);
13106             return;
13107         }
13108         g_assert_not_reached();
13109     case 0x11: /* FCMLA #0 */
13110     case 0x13: /* FCMLA #90 */
13111     case 0x15: /* FCMLA #180 */
13112     case 0x17: /* FCMLA #270 */
13113         {
13114             int rot = extract32(insn, 13, 2);
13115             int data = (index << 2) | rot;
13116             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
13117                                vec_full_reg_offset(s, rn),
13118                                vec_full_reg_offset(s, rm),
13119                                vec_full_reg_offset(s, rd), fpst,
13120                                is_q ? 16 : 8, vec_full_reg_size(s), data,
13121                                size == MO_64
13122                                ? gen_helper_gvec_fcmlas_idx
13123                                : gen_helper_gvec_fcmlah_idx);
13124         }
13125         return;
13126 
13127     case 0x00: /* FMLAL */
13128     case 0x04: /* FMLSL */
13129     case 0x18: /* FMLAL2 */
13130     case 0x1c: /* FMLSL2 */
13131         {
13132             int is_s = extract32(opcode, 2, 1);
13133             int is_2 = u;
13134             int data = (index << 2) | (is_2 << 1) | is_s;
13135             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13136                                vec_full_reg_offset(s, rn),
13137                                vec_full_reg_offset(s, rm), cpu_env,
13138                                is_q ? 16 : 8, vec_full_reg_size(s),
13139                                data, gen_helper_gvec_fmlal_idx_a64);
13140         }
13141         return;
13142 
13143     case 0x08: /* MUL */
13144         if (!is_long && !is_scalar) {
13145             static gen_helper_gvec_3 * const fns[3] = {
13146                 gen_helper_gvec_mul_idx_h,
13147                 gen_helper_gvec_mul_idx_s,
13148                 gen_helper_gvec_mul_idx_d,
13149             };
13150             tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
13151                                vec_full_reg_offset(s, rn),
13152                                vec_full_reg_offset(s, rm),
13153                                is_q ? 16 : 8, vec_full_reg_size(s),
13154                                index, fns[size - 1]);
13155             return;
13156         }
13157         break;
13158 
13159     case 0x10: /* MLA */
13160         if (!is_long && !is_scalar) {
13161             static gen_helper_gvec_4 * const fns[3] = {
13162                 gen_helper_gvec_mla_idx_h,
13163                 gen_helper_gvec_mla_idx_s,
13164                 gen_helper_gvec_mla_idx_d,
13165             };
13166             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13167                                vec_full_reg_offset(s, rn),
13168                                vec_full_reg_offset(s, rm),
13169                                vec_full_reg_offset(s, rd),
13170                                is_q ? 16 : 8, vec_full_reg_size(s),
13171                                index, fns[size - 1]);
13172             return;
13173         }
13174         break;
13175 
13176     case 0x14: /* MLS */
13177         if (!is_long && !is_scalar) {
13178             static gen_helper_gvec_4 * const fns[3] = {
13179                 gen_helper_gvec_mls_idx_h,
13180                 gen_helper_gvec_mls_idx_s,
13181                 gen_helper_gvec_mls_idx_d,
13182             };
13183             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13184                                vec_full_reg_offset(s, rn),
13185                                vec_full_reg_offset(s, rm),
13186                                vec_full_reg_offset(s, rd),
13187                                is_q ? 16 : 8, vec_full_reg_size(s),
13188                                index, fns[size - 1]);
13189             return;
13190         }
13191         break;
13192     }
13193 
13194     if (size == 3) {
13195         TCGv_i64 tcg_idx = tcg_temp_new_i64();
13196         int pass;
13197 
13198         assert(is_fp && is_q && !is_long);
13199 
13200         read_vec_element(s, tcg_idx, rm, index, MO_64);
13201 
13202         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13203             TCGv_i64 tcg_op = tcg_temp_new_i64();
13204             TCGv_i64 tcg_res = tcg_temp_new_i64();
13205 
13206             read_vec_element(s, tcg_op, rn, pass, MO_64);
13207 
13208             switch (16 * u + opcode) {
13209             case 0x05: /* FMLS */
13210                 /* As usual for ARM, separate negation for fused multiply-add */
13211                 gen_helper_vfp_negd(tcg_op, tcg_op);
13212                 /* fall through */
13213             case 0x01: /* FMLA */
13214                 read_vec_element(s, tcg_res, rd, pass, MO_64);
13215                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13216                 break;
13217             case 0x09: /* FMUL */
13218                 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13219                 break;
13220             case 0x19: /* FMULX */
13221                 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13222                 break;
13223             default:
13224                 g_assert_not_reached();
13225             }
13226 
13227             write_vec_element(s, tcg_res, rd, pass, MO_64);
13228         }
13229 
13230         clear_vec_high(s, !is_scalar, rd);
13231     } else if (!is_long) {
13232         /* 32 bit floating point, or 16 or 32 bit integer.
13233          * For the 16 bit scalar case we use the usual Neon helpers and
13234          * rely on the fact that 0 op 0 == 0 with no side effects.
13235          */
13236         TCGv_i32 tcg_idx = tcg_temp_new_i32();
13237         int pass, maxpasses;
13238 
13239         if (is_scalar) {
13240             maxpasses = 1;
13241         } else {
13242             maxpasses = is_q ? 4 : 2;
13243         }
13244 
13245         read_vec_element_i32(s, tcg_idx, rm, index, size);
13246 
13247         if (size == 1 && !is_scalar) {
13248             /* The simplest way to handle the 16x16 indexed ops is to duplicate
13249              * the index into both halves of the 32 bit tcg_idx and then use
13250              * the usual Neon helpers.
13251              */
13252             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13253         }
13254 
13255         for (pass = 0; pass < maxpasses; pass++) {
13256             TCGv_i32 tcg_op = tcg_temp_new_i32();
13257             TCGv_i32 tcg_res = tcg_temp_new_i32();
13258 
13259             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13260 
13261             switch (16 * u + opcode) {
13262             case 0x08: /* MUL */
13263             case 0x10: /* MLA */
13264             case 0x14: /* MLS */
13265             {
13266                 static NeonGenTwoOpFn * const fns[2][2] = {
13267                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13268                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
13269                 };
13270                 NeonGenTwoOpFn *genfn;
13271                 bool is_sub = opcode == 0x4;
13272 
13273                 if (size == 1) {
13274                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13275                 } else {
13276                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13277                 }
13278                 if (opcode == 0x8) {
13279                     break;
13280                 }
13281                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13282                 genfn = fns[size - 1][is_sub];
13283                 genfn(tcg_res, tcg_op, tcg_res);
13284                 break;
13285             }
13286             case 0x05: /* FMLS */
13287             case 0x01: /* FMLA */
13288                 read_vec_element_i32(s, tcg_res, rd, pass,
13289                                      is_scalar ? size : MO_32);
13290                 switch (size) {
13291                 case 1:
13292                     if (opcode == 0x5) {
13293                         /* As usual for ARM, separate negation for fused
13294                          * multiply-add */
13295                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13296                     }
13297                     if (is_scalar) {
13298                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13299                                                    tcg_res, fpst);
13300                     } else {
13301                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13302                                                     tcg_res, fpst);
13303                     }
13304                     break;
13305                 case 2:
13306                     if (opcode == 0x5) {
13307                         /* As usual for ARM, separate negation for
13308                          * fused multiply-add */
13309                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13310                     }
13311                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13312                                            tcg_res, fpst);
13313                     break;
13314                 default:
13315                     g_assert_not_reached();
13316                 }
13317                 break;
13318             case 0x09: /* FMUL */
13319                 switch (size) {
13320                 case 1:
13321                     if (is_scalar) {
13322                         gen_helper_advsimd_mulh(tcg_res, tcg_op,
13323                                                 tcg_idx, fpst);
13324                     } else {
13325                         gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13326                                                  tcg_idx, fpst);
13327                     }
13328                     break;
13329                 case 2:
13330                     gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13331                     break;
13332                 default:
13333                     g_assert_not_reached();
13334                 }
13335                 break;
13336             case 0x19: /* FMULX */
13337                 switch (size) {
13338                 case 1:
13339                     if (is_scalar) {
13340                         gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13341                                                  tcg_idx, fpst);
13342                     } else {
13343                         gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13344                                                   tcg_idx, fpst);
13345                     }
13346                     break;
13347                 case 2:
13348                     gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13349                     break;
13350                 default:
13351                     g_assert_not_reached();
13352                 }
13353                 break;
13354             case 0x0c: /* SQDMULH */
13355                 if (size == 1) {
13356                     gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13357                                                tcg_op, tcg_idx);
13358                 } else {
13359                     gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13360                                                tcg_op, tcg_idx);
13361                 }
13362                 break;
13363             case 0x0d: /* SQRDMULH */
13364                 if (size == 1) {
13365                     gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13366                                                 tcg_op, tcg_idx);
13367                 } else {
13368                     gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13369                                                 tcg_op, tcg_idx);
13370                 }
13371                 break;
13372             case 0x1d: /* SQRDMLAH */
13373                 read_vec_element_i32(s, tcg_res, rd, pass,
13374                                      is_scalar ? size : MO_32);
13375                 if (size == 1) {
13376                     gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13377                                                 tcg_op, tcg_idx, tcg_res);
13378                 } else {
13379                     gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13380                                                 tcg_op, tcg_idx, tcg_res);
13381                 }
13382                 break;
13383             case 0x1f: /* SQRDMLSH */
13384                 read_vec_element_i32(s, tcg_res, rd, pass,
13385                                      is_scalar ? size : MO_32);
13386                 if (size == 1) {
13387                     gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13388                                                 tcg_op, tcg_idx, tcg_res);
13389                 } else {
13390                     gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13391                                                 tcg_op, tcg_idx, tcg_res);
13392                 }
13393                 break;
13394             default:
13395                 g_assert_not_reached();
13396             }
13397 
13398             if (is_scalar) {
13399                 write_fp_sreg(s, rd, tcg_res);
13400             } else {
13401                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13402             }
13403         }
13404 
13405         clear_vec_high(s, is_q, rd);
13406     } else {
13407         /* long ops: 16x16->32 or 32x32->64 */
13408         TCGv_i64 tcg_res[2];
13409         int pass;
13410         bool satop = extract32(opcode, 0, 1);
13411         MemOp memop = MO_32;
13412 
13413         if (satop || !u) {
13414             memop |= MO_SIGN;
13415         }
13416 
13417         if (size == 2) {
13418             TCGv_i64 tcg_idx = tcg_temp_new_i64();
13419 
13420             read_vec_element(s, tcg_idx, rm, index, memop);
13421 
13422             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13423                 TCGv_i64 tcg_op = tcg_temp_new_i64();
13424                 TCGv_i64 tcg_passres;
13425                 int passelt;
13426 
13427                 if (is_scalar) {
13428                     passelt = 0;
13429                 } else {
13430                     passelt = pass + (is_q * 2);
13431                 }
13432 
13433                 read_vec_element(s, tcg_op, rn, passelt, memop);
13434 
13435                 tcg_res[pass] = tcg_temp_new_i64();
13436 
13437                 if (opcode == 0xa || opcode == 0xb) {
13438                     /* Non-accumulating ops */
13439                     tcg_passres = tcg_res[pass];
13440                 } else {
13441                     tcg_passres = tcg_temp_new_i64();
13442                 }
13443 
13444                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13445 
13446                 if (satop) {
13447                     /* saturating, doubling */
13448                     gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13449                                                       tcg_passres, tcg_passres);
13450                 }
13451 
13452                 if (opcode == 0xa || opcode == 0xb) {
13453                     continue;
13454                 }
13455 
13456                 /* Accumulating op: handle accumulate step */
13457                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13458 
13459                 switch (opcode) {
13460                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13461                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13462                     break;
13463                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13464                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13465                     break;
13466                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13467                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
13468                     /* fall through */
13469                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13470                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13471                                                       tcg_res[pass],
13472                                                       tcg_passres);
13473                     break;
13474                 default:
13475                     g_assert_not_reached();
13476                 }
13477             }
13478 
13479             clear_vec_high(s, !is_scalar, rd);
13480         } else {
13481             TCGv_i32 tcg_idx = tcg_temp_new_i32();
13482 
13483             assert(size == 1);
13484             read_vec_element_i32(s, tcg_idx, rm, index, size);
13485 
13486             if (!is_scalar) {
13487                 /* The simplest way to handle the 16x16 indexed ops is to
13488                  * duplicate the index into both halves of the 32 bit tcg_idx
13489                  * and then use the usual Neon helpers.
13490                  */
13491                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13492             }
13493 
13494             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13495                 TCGv_i32 tcg_op = tcg_temp_new_i32();
13496                 TCGv_i64 tcg_passres;
13497 
13498                 if (is_scalar) {
13499                     read_vec_element_i32(s, tcg_op, rn, pass, size);
13500                 } else {
13501                     read_vec_element_i32(s, tcg_op, rn,
13502                                          pass + (is_q * 2), MO_32);
13503                 }
13504 
13505                 tcg_res[pass] = tcg_temp_new_i64();
13506 
13507                 if (opcode == 0xa || opcode == 0xb) {
13508                     /* Non-accumulating ops */
13509                     tcg_passres = tcg_res[pass];
13510                 } else {
13511                     tcg_passres = tcg_temp_new_i64();
13512                 }
13513 
13514                 if (memop & MO_SIGN) {
13515                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13516                 } else {
13517                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13518                 }
13519                 if (satop) {
13520                     gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13521                                                       tcg_passres, tcg_passres);
13522                 }
13523 
13524                 if (opcode == 0xa || opcode == 0xb) {
13525                     continue;
13526                 }
13527 
13528                 /* Accumulating op: handle accumulate step */
13529                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13530 
13531                 switch (opcode) {
13532                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13533                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13534                                              tcg_passres);
13535                     break;
13536                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13537                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13538                                              tcg_passres);
13539                     break;
13540                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13541                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13542                     /* fall through */
13543                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13544                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13545                                                       tcg_res[pass],
13546                                                       tcg_passres);
13547                     break;
13548                 default:
13549                     g_assert_not_reached();
13550                 }
13551             }
13552 
13553             if (is_scalar) {
13554                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13555             }
13556         }
13557 
13558         if (is_scalar) {
13559             tcg_res[1] = tcg_constant_i64(0);
13560         }
13561 
13562         for (pass = 0; pass < 2; pass++) {
13563             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13564         }
13565     }
13566 }
13567 
13568 /* Crypto AES
13569  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13570  * +-----------------+------+-----------+--------+-----+------+------+
13571  * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13572  * +-----------------+------+-----------+--------+-----+------+------+
13573  */
13574 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13575 {
13576     int size = extract32(insn, 22, 2);
13577     int opcode = extract32(insn, 12, 5);
13578     int rn = extract32(insn, 5, 5);
13579     int rd = extract32(insn, 0, 5);
13580     int decrypt;
13581     gen_helper_gvec_2 *genfn2 = NULL;
13582     gen_helper_gvec_3 *genfn3 = NULL;
13583 
13584     if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13585         unallocated_encoding(s);
13586         return;
13587     }
13588 
13589     switch (opcode) {
13590     case 0x4: /* AESE */
13591         decrypt = 0;
13592         genfn3 = gen_helper_crypto_aese;
13593         break;
13594     case 0x6: /* AESMC */
13595         decrypt = 0;
13596         genfn2 = gen_helper_crypto_aesmc;
13597         break;
13598     case 0x5: /* AESD */
13599         decrypt = 1;
13600         genfn3 = gen_helper_crypto_aese;
13601         break;
13602     case 0x7: /* AESIMC */
13603         decrypt = 1;
13604         genfn2 = gen_helper_crypto_aesmc;
13605         break;
13606     default:
13607         unallocated_encoding(s);
13608         return;
13609     }
13610 
13611     if (!fp_access_check(s)) {
13612         return;
13613     }
13614     if (genfn2) {
13615         gen_gvec_op2_ool(s, true, rd, rn, decrypt, genfn2);
13616     } else {
13617         gen_gvec_op3_ool(s, true, rd, rd, rn, decrypt, genfn3);
13618     }
13619 }
13620 
13621 /* Crypto three-reg SHA
13622  *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
13623  * +-----------------+------+---+------+---+--------+-----+------+------+
13624  * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
13625  * +-----------------+------+---+------+---+--------+-----+------+------+
13626  */
13627 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13628 {
13629     int size = extract32(insn, 22, 2);
13630     int opcode = extract32(insn, 12, 3);
13631     int rm = extract32(insn, 16, 5);
13632     int rn = extract32(insn, 5, 5);
13633     int rd = extract32(insn, 0, 5);
13634     gen_helper_gvec_3 *genfn;
13635     bool feature;
13636 
13637     if (size != 0) {
13638         unallocated_encoding(s);
13639         return;
13640     }
13641 
13642     switch (opcode) {
13643     case 0: /* SHA1C */
13644         genfn = gen_helper_crypto_sha1c;
13645         feature = dc_isar_feature(aa64_sha1, s);
13646         break;
13647     case 1: /* SHA1P */
13648         genfn = gen_helper_crypto_sha1p;
13649         feature = dc_isar_feature(aa64_sha1, s);
13650         break;
13651     case 2: /* SHA1M */
13652         genfn = gen_helper_crypto_sha1m;
13653         feature = dc_isar_feature(aa64_sha1, s);
13654         break;
13655     case 3: /* SHA1SU0 */
13656         genfn = gen_helper_crypto_sha1su0;
13657         feature = dc_isar_feature(aa64_sha1, s);
13658         break;
13659     case 4: /* SHA256H */
13660         genfn = gen_helper_crypto_sha256h;
13661         feature = dc_isar_feature(aa64_sha256, s);
13662         break;
13663     case 5: /* SHA256H2 */
13664         genfn = gen_helper_crypto_sha256h2;
13665         feature = dc_isar_feature(aa64_sha256, s);
13666         break;
13667     case 6: /* SHA256SU1 */
13668         genfn = gen_helper_crypto_sha256su1;
13669         feature = dc_isar_feature(aa64_sha256, s);
13670         break;
13671     default:
13672         unallocated_encoding(s);
13673         return;
13674     }
13675 
13676     if (!feature) {
13677         unallocated_encoding(s);
13678         return;
13679     }
13680 
13681     if (!fp_access_check(s)) {
13682         return;
13683     }
13684     gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
13685 }
13686 
13687 /* Crypto two-reg SHA
13688  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13689  * +-----------------+------+-----------+--------+-----+------+------+
13690  * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13691  * +-----------------+------+-----------+--------+-----+------+------+
13692  */
13693 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13694 {
13695     int size = extract32(insn, 22, 2);
13696     int opcode = extract32(insn, 12, 5);
13697     int rn = extract32(insn, 5, 5);
13698     int rd = extract32(insn, 0, 5);
13699     gen_helper_gvec_2 *genfn;
13700     bool feature;
13701 
13702     if (size != 0) {
13703         unallocated_encoding(s);
13704         return;
13705     }
13706 
13707     switch (opcode) {
13708     case 0: /* SHA1H */
13709         feature = dc_isar_feature(aa64_sha1, s);
13710         genfn = gen_helper_crypto_sha1h;
13711         break;
13712     case 1: /* SHA1SU1 */
13713         feature = dc_isar_feature(aa64_sha1, s);
13714         genfn = gen_helper_crypto_sha1su1;
13715         break;
13716     case 2: /* SHA256SU0 */
13717         feature = dc_isar_feature(aa64_sha256, s);
13718         genfn = gen_helper_crypto_sha256su0;
13719         break;
13720     default:
13721         unallocated_encoding(s);
13722         return;
13723     }
13724 
13725     if (!feature) {
13726         unallocated_encoding(s);
13727         return;
13728     }
13729 
13730     if (!fp_access_check(s)) {
13731         return;
13732     }
13733     gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
13734 }
13735 
13736 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
13737 {
13738     tcg_gen_rotli_i64(d, m, 1);
13739     tcg_gen_xor_i64(d, d, n);
13740 }
13741 
13742 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
13743 {
13744     tcg_gen_rotli_vec(vece, d, m, 1);
13745     tcg_gen_xor_vec(vece, d, d, n);
13746 }
13747 
13748 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
13749                    uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
13750 {
13751     static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
13752     static const GVecGen3 op = {
13753         .fni8 = gen_rax1_i64,
13754         .fniv = gen_rax1_vec,
13755         .opt_opc = vecop_list,
13756         .fno = gen_helper_crypto_rax1,
13757         .vece = MO_64,
13758     };
13759     tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
13760 }
13761 
13762 /* Crypto three-reg SHA512
13763  *  31                   21 20  16 15  14  13 12  11  10  9    5 4    0
13764  * +-----------------------+------+---+---+-----+--------+------+------+
13765  * | 1 1 0 0 1 1 1 0 0 1 1 |  Rm  | 1 | O | 0 0 | opcode |  Rn  |  Rd  |
13766  * +-----------------------+------+---+---+-----+--------+------+------+
13767  */
13768 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13769 {
13770     int opcode = extract32(insn, 10, 2);
13771     int o =  extract32(insn, 14, 1);
13772     int rm = extract32(insn, 16, 5);
13773     int rn = extract32(insn, 5, 5);
13774     int rd = extract32(insn, 0, 5);
13775     bool feature;
13776     gen_helper_gvec_3 *oolfn = NULL;
13777     GVecGen3Fn *gvecfn = NULL;
13778 
13779     if (o == 0) {
13780         switch (opcode) {
13781         case 0: /* SHA512H */
13782             feature = dc_isar_feature(aa64_sha512, s);
13783             oolfn = gen_helper_crypto_sha512h;
13784             break;
13785         case 1: /* SHA512H2 */
13786             feature = dc_isar_feature(aa64_sha512, s);
13787             oolfn = gen_helper_crypto_sha512h2;
13788             break;
13789         case 2: /* SHA512SU1 */
13790             feature = dc_isar_feature(aa64_sha512, s);
13791             oolfn = gen_helper_crypto_sha512su1;
13792             break;
13793         case 3: /* RAX1 */
13794             feature = dc_isar_feature(aa64_sha3, s);
13795             gvecfn = gen_gvec_rax1;
13796             break;
13797         default:
13798             g_assert_not_reached();
13799         }
13800     } else {
13801         switch (opcode) {
13802         case 0: /* SM3PARTW1 */
13803             feature = dc_isar_feature(aa64_sm3, s);
13804             oolfn = gen_helper_crypto_sm3partw1;
13805             break;
13806         case 1: /* SM3PARTW2 */
13807             feature = dc_isar_feature(aa64_sm3, s);
13808             oolfn = gen_helper_crypto_sm3partw2;
13809             break;
13810         case 2: /* SM4EKEY */
13811             feature = dc_isar_feature(aa64_sm4, s);
13812             oolfn = gen_helper_crypto_sm4ekey;
13813             break;
13814         default:
13815             unallocated_encoding(s);
13816             return;
13817         }
13818     }
13819 
13820     if (!feature) {
13821         unallocated_encoding(s);
13822         return;
13823     }
13824 
13825     if (!fp_access_check(s)) {
13826         return;
13827     }
13828 
13829     if (oolfn) {
13830         gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
13831     } else {
13832         gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
13833     }
13834 }
13835 
13836 /* Crypto two-reg SHA512
13837  *  31                                     12  11  10  9    5 4    0
13838  * +-----------------------------------------+--------+------+------+
13839  * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode |  Rn  |  Rd  |
13840  * +-----------------------------------------+--------+------+------+
13841  */
13842 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13843 {
13844     int opcode = extract32(insn, 10, 2);
13845     int rn = extract32(insn, 5, 5);
13846     int rd = extract32(insn, 0, 5);
13847     bool feature;
13848 
13849     switch (opcode) {
13850     case 0: /* SHA512SU0 */
13851         feature = dc_isar_feature(aa64_sha512, s);
13852         break;
13853     case 1: /* SM4E */
13854         feature = dc_isar_feature(aa64_sm4, s);
13855         break;
13856     default:
13857         unallocated_encoding(s);
13858         return;
13859     }
13860 
13861     if (!feature) {
13862         unallocated_encoding(s);
13863         return;
13864     }
13865 
13866     if (!fp_access_check(s)) {
13867         return;
13868     }
13869 
13870     switch (opcode) {
13871     case 0: /* SHA512SU0 */
13872         gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
13873         break;
13874     case 1: /* SM4E */
13875         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
13876         break;
13877     default:
13878         g_assert_not_reached();
13879     }
13880 }
13881 
13882 /* Crypto four-register
13883  *  31               23 22 21 20  16 15  14  10 9    5 4    0
13884  * +-------------------+-----+------+---+------+------+------+
13885  * | 1 1 0 0 1 1 1 0 0 | Op0 |  Rm  | 0 |  Ra  |  Rn  |  Rd  |
13886  * +-------------------+-----+------+---+------+------+------+
13887  */
13888 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13889 {
13890     int op0 = extract32(insn, 21, 2);
13891     int rm = extract32(insn, 16, 5);
13892     int ra = extract32(insn, 10, 5);
13893     int rn = extract32(insn, 5, 5);
13894     int rd = extract32(insn, 0, 5);
13895     bool feature;
13896 
13897     switch (op0) {
13898     case 0: /* EOR3 */
13899     case 1: /* BCAX */
13900         feature = dc_isar_feature(aa64_sha3, s);
13901         break;
13902     case 2: /* SM3SS1 */
13903         feature = dc_isar_feature(aa64_sm3, s);
13904         break;
13905     default:
13906         unallocated_encoding(s);
13907         return;
13908     }
13909 
13910     if (!feature) {
13911         unallocated_encoding(s);
13912         return;
13913     }
13914 
13915     if (!fp_access_check(s)) {
13916         return;
13917     }
13918 
13919     if (op0 < 2) {
13920         TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13921         int pass;
13922 
13923         tcg_op1 = tcg_temp_new_i64();
13924         tcg_op2 = tcg_temp_new_i64();
13925         tcg_op3 = tcg_temp_new_i64();
13926         tcg_res[0] = tcg_temp_new_i64();
13927         tcg_res[1] = tcg_temp_new_i64();
13928 
13929         for (pass = 0; pass < 2; pass++) {
13930             read_vec_element(s, tcg_op1, rn, pass, MO_64);
13931             read_vec_element(s, tcg_op2, rm, pass, MO_64);
13932             read_vec_element(s, tcg_op3, ra, pass, MO_64);
13933 
13934             if (op0 == 0) {
13935                 /* EOR3 */
13936                 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13937             } else {
13938                 /* BCAX */
13939                 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13940             }
13941             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13942         }
13943         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13944         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13945     } else {
13946         TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13947 
13948         tcg_op1 = tcg_temp_new_i32();
13949         tcg_op2 = tcg_temp_new_i32();
13950         tcg_op3 = tcg_temp_new_i32();
13951         tcg_res = tcg_temp_new_i32();
13952         tcg_zero = tcg_constant_i32(0);
13953 
13954         read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13955         read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13956         read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13957 
13958         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13959         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13960         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13961         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13962 
13963         write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13964         write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13965         write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13966         write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13967     }
13968 }
13969 
13970 /* Crypto XAR
13971  *  31                   21 20  16 15    10 9    5 4    0
13972  * +-----------------------+------+--------+------+------+
13973  * | 1 1 0 0 1 1 1 0 1 0 0 |  Rm  |  imm6  |  Rn  |  Rd  |
13974  * +-----------------------+------+--------+------+------+
13975  */
13976 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
13977 {
13978     int rm = extract32(insn, 16, 5);
13979     int imm6 = extract32(insn, 10, 6);
13980     int rn = extract32(insn, 5, 5);
13981     int rd = extract32(insn, 0, 5);
13982 
13983     if (!dc_isar_feature(aa64_sha3, s)) {
13984         unallocated_encoding(s);
13985         return;
13986     }
13987 
13988     if (!fp_access_check(s)) {
13989         return;
13990     }
13991 
13992     gen_gvec_xar(MO_64, vec_full_reg_offset(s, rd),
13993                  vec_full_reg_offset(s, rn),
13994                  vec_full_reg_offset(s, rm), imm6, 16,
13995                  vec_full_reg_size(s));
13996 }
13997 
13998 /* Crypto three-reg imm2
13999  *  31                   21 20  16 15  14 13 12  11  10  9    5 4    0
14000  * +-----------------------+------+-----+------+--------+------+------+
14001  * | 1 1 0 0 1 1 1 0 0 1 0 |  Rm  | 1 0 | imm2 | opcode |  Rn  |  Rd  |
14002  * +-----------------------+------+-----+------+--------+------+------+
14003  */
14004 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
14005 {
14006     static gen_helper_gvec_3 * const fns[4] = {
14007         gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
14008         gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
14009     };
14010     int opcode = extract32(insn, 10, 2);
14011     int imm2 = extract32(insn, 12, 2);
14012     int rm = extract32(insn, 16, 5);
14013     int rn = extract32(insn, 5, 5);
14014     int rd = extract32(insn, 0, 5);
14015 
14016     if (!dc_isar_feature(aa64_sm3, s)) {
14017         unallocated_encoding(s);
14018         return;
14019     }
14020 
14021     if (!fp_access_check(s)) {
14022         return;
14023     }
14024 
14025     gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
14026 }
14027 
14028 /* C3.6 Data processing - SIMD, inc Crypto
14029  *
14030  * As the decode gets a little complex we are using a table based
14031  * approach for this part of the decode.
14032  */
14033 static const AArch64DecodeTable data_proc_simd[] = {
14034     /* pattern  ,  mask     ,  fn                        */
14035     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
14036     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
14037     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
14038     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
14039     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
14040     { 0x0e000400, 0x9fe08400, disas_simd_copy },
14041     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
14042     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
14043     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
14044     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
14045     { 0x0e000000, 0xbf208c00, disas_simd_tb },
14046     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
14047     { 0x2e000000, 0xbf208400, disas_simd_ext },
14048     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
14049     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
14050     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
14051     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
14052     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
14053     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
14054     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
14055     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
14056     { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
14057     { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
14058     { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
14059     { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
14060     { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
14061     { 0xce000000, 0xff808000, disas_crypto_four_reg },
14062     { 0xce800000, 0xffe00000, disas_crypto_xar },
14063     { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
14064     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
14065     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
14066     { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
14067     { 0x00000000, 0x00000000, NULL }
14068 };
14069 
14070 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
14071 {
14072     /* Note that this is called with all non-FP cases from
14073      * table C3-6 so it must UNDEF for entries not specifically
14074      * allocated to instructions in that table.
14075      */
14076     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
14077     if (fn) {
14078         fn(s, insn);
14079     } else {
14080         unallocated_encoding(s);
14081     }
14082 }
14083 
14084 /* C3.6 Data processing - SIMD and floating point */
14085 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
14086 {
14087     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
14088         disas_data_proc_fp(s, insn);
14089     } else {
14090         /* SIMD, including crypto */
14091         disas_data_proc_simd(s, insn);
14092     }
14093 }
14094 
14095 /*
14096  * Include the generated SME FA64 decoder.
14097  */
14098 
14099 #include "decode-sme-fa64.c.inc"
14100 
14101 static bool trans_OK(DisasContext *s, arg_OK *a)
14102 {
14103     return true;
14104 }
14105 
14106 static bool trans_FAIL(DisasContext *s, arg_OK *a)
14107 {
14108     s->is_nonstreaming = true;
14109     return true;
14110 }
14111 
14112 /**
14113  * is_guarded_page:
14114  * @env: The cpu environment
14115  * @s: The DisasContext
14116  *
14117  * Return true if the page is guarded.
14118  */
14119 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
14120 {
14121     uint64_t addr = s->base.pc_first;
14122 #ifdef CONFIG_USER_ONLY
14123     return page_get_flags(addr) & PAGE_BTI;
14124 #else
14125     CPUTLBEntryFull *full;
14126     void *host;
14127     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
14128     int flags;
14129 
14130     /*
14131      * We test this immediately after reading an insn, which means
14132      * that the TLB entry must be present and valid, and thus this
14133      * access will never raise an exception.
14134      */
14135     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
14136                               false, &host, &full, 0);
14137     assert(!(flags & TLB_INVALID_MASK));
14138 
14139     return full->guarded;
14140 #endif
14141 }
14142 
14143 /**
14144  * btype_destination_ok:
14145  * @insn: The instruction at the branch destination
14146  * @bt: SCTLR_ELx.BT
14147  * @btype: PSTATE.BTYPE, and is non-zero
14148  *
14149  * On a guarded page, there are a limited number of insns
14150  * that may be present at the branch target:
14151  *   - branch target identifiers,
14152  *   - paciasp, pacibsp,
14153  *   - BRK insn
14154  *   - HLT insn
14155  * Anything else causes a Branch Target Exception.
14156  *
14157  * Return true if the branch is compatible, false to raise BTITRAP.
14158  */
14159 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
14160 {
14161     if ((insn & 0xfffff01fu) == 0xd503201fu) {
14162         /* HINT space */
14163         switch (extract32(insn, 5, 7)) {
14164         case 0b011001: /* PACIASP */
14165         case 0b011011: /* PACIBSP */
14166             /*
14167              * If SCTLR_ELx.BT, then PACI*SP are not compatible
14168              * with btype == 3.  Otherwise all btype are ok.
14169              */
14170             return !bt || btype != 3;
14171         case 0b100000: /* BTI */
14172             /* Not compatible with any btype.  */
14173             return false;
14174         case 0b100010: /* BTI c */
14175             /* Not compatible with btype == 3 */
14176             return btype != 3;
14177         case 0b100100: /* BTI j */
14178             /* Not compatible with btype == 2 */
14179             return btype != 2;
14180         case 0b100110: /* BTI jc */
14181             /* Compatible with any btype.  */
14182             return true;
14183         }
14184     } else {
14185         switch (insn & 0xffe0001fu) {
14186         case 0xd4200000u: /* BRK */
14187         case 0xd4400000u: /* HLT */
14188             /* Give priority to the breakpoint exception.  */
14189             return true;
14190         }
14191     }
14192     return false;
14193 }
14194 
14195 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14196                                           CPUState *cpu)
14197 {
14198     DisasContext *dc = container_of(dcbase, DisasContext, base);
14199     CPUARMState *env = cpu->env_ptr;
14200     ARMCPU *arm_cpu = env_archcpu(env);
14201     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
14202     int bound, core_mmu_idx;
14203 
14204     dc->isar = &arm_cpu->isar;
14205     dc->condjmp = 0;
14206     dc->pc_save = dc->base.pc_first;
14207     dc->aarch64 = true;
14208     dc->thumb = false;
14209     dc->sctlr_b = 0;
14210     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
14211     dc->condexec_mask = 0;
14212     dc->condexec_cond = 0;
14213     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
14214     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
14215     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
14216     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
14217     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
14218     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14219 #if !defined(CONFIG_USER_ONLY)
14220     dc->user = (dc->current_el == 0);
14221 #endif
14222     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
14223     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
14224     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
14225     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
14226     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
14227     dc->fgt_eret = EX_TBFLAG_A64(tb_flags, FGT_ERET);
14228     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
14229     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
14230     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
14231     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
14232     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
14233     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
14234     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
14235     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
14236     dc->ata = EX_TBFLAG_A64(tb_flags, ATA);
14237     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
14238     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
14239     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
14240     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
14241     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
14242     dc->vec_len = 0;
14243     dc->vec_stride = 0;
14244     dc->cp_regs = arm_cpu->cp_regs;
14245     dc->features = env->features;
14246     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
14247 
14248 #ifdef CONFIG_USER_ONLY
14249     /* In sve_probe_page, we assume TBI is enabled. */
14250     tcg_debug_assert(dc->tbid & 1);
14251 #endif
14252 
14253     /* Single step state. The code-generation logic here is:
14254      *  SS_ACTIVE == 0:
14255      *   generate code with no special handling for single-stepping (except
14256      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14257      *   this happens anyway because those changes are all system register or
14258      *   PSTATE writes).
14259      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14260      *   emit code for one insn
14261      *   emit code to clear PSTATE.SS
14262      *   emit code to generate software step exception for completed step
14263      *   end TB (as usual for having generated an exception)
14264      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14265      *   emit code to generate a software step exception
14266      *   end the TB
14267      */
14268     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
14269     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
14270     dc->is_ldex = false;
14271 
14272     /* Bound the number of insns to execute to those left on the page.  */
14273     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14274 
14275     /* If architectural single step active, limit to 1.  */
14276     if (dc->ss_active) {
14277         bound = 1;
14278     }
14279     dc->base.max_insns = MIN(dc->base.max_insns, bound);
14280 }
14281 
14282 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14283 {
14284 }
14285 
14286 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14287 {
14288     DisasContext *dc = container_of(dcbase, DisasContext, base);
14289     target_ulong pc_arg = dc->base.pc_next;
14290 
14291     if (tb_cflags(dcbase->tb) & CF_PCREL) {
14292         pc_arg &= ~TARGET_PAGE_MASK;
14293     }
14294     tcg_gen_insn_start(pc_arg, 0, 0);
14295     dc->insn_start = tcg_last_op();
14296 }
14297 
14298 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14299 {
14300     DisasContext *s = container_of(dcbase, DisasContext, base);
14301     CPUARMState *env = cpu->env_ptr;
14302     uint64_t pc = s->base.pc_next;
14303     uint32_t insn;
14304 
14305     /* Singlestep exceptions have the highest priority. */
14306     if (s->ss_active && !s->pstate_ss) {
14307         /* Singlestep state is Active-pending.
14308          * If we're in this state at the start of a TB then either
14309          *  a) we just took an exception to an EL which is being debugged
14310          *     and this is the first insn in the exception handler
14311          *  b) debug exceptions were masked and we just unmasked them
14312          *     without changing EL (eg by clearing PSTATE.D)
14313          * In either case we're going to take a swstep exception in the
14314          * "did not step an insn" case, and so the syndrome ISV and EX
14315          * bits should be zero.
14316          */
14317         assert(s->base.num_insns == 1);
14318         gen_swstep_exception(s, 0, 0);
14319         s->base.is_jmp = DISAS_NORETURN;
14320         s->base.pc_next = pc + 4;
14321         return;
14322     }
14323 
14324     if (pc & 3) {
14325         /*
14326          * PC alignment fault.  This has priority over the instruction abort
14327          * that we would receive from a translation fault via arm_ldl_code.
14328          * This should only be possible after an indirect branch, at the
14329          * start of the TB.
14330          */
14331         assert(s->base.num_insns == 1);
14332         gen_helper_exception_pc_alignment(cpu_env, tcg_constant_tl(pc));
14333         s->base.is_jmp = DISAS_NORETURN;
14334         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
14335         return;
14336     }
14337 
14338     s->pc_curr = pc;
14339     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
14340     s->insn = insn;
14341     s->base.pc_next = pc + 4;
14342 
14343     s->fp_access_checked = false;
14344     s->sve_access_checked = false;
14345 
14346     if (s->pstate_il) {
14347         /*
14348          * Illegal execution state. This has priority over BTI
14349          * exceptions, but comes after instruction abort exceptions.
14350          */
14351         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
14352         return;
14353     }
14354 
14355     if (dc_isar_feature(aa64_bti, s)) {
14356         if (s->base.num_insns == 1) {
14357             /*
14358              * At the first insn of the TB, compute s->guarded_page.
14359              * We delayed computing this until successfully reading
14360              * the first insn of the TB, above.  This (mostly) ensures
14361              * that the softmmu tlb entry has been populated, and the
14362              * page table GP bit is available.
14363              *
14364              * Note that we need to compute this even if btype == 0,
14365              * because this value is used for BR instructions later
14366              * where ENV is not available.
14367              */
14368             s->guarded_page = is_guarded_page(env, s);
14369 
14370             /* First insn can have btype set to non-zero.  */
14371             tcg_debug_assert(s->btype >= 0);
14372 
14373             /*
14374              * Note that the Branch Target Exception has fairly high
14375              * priority -- below debugging exceptions but above most
14376              * everything else.  This allows us to handle this now
14377              * instead of waiting until the insn is otherwise decoded.
14378              */
14379             if (s->btype != 0
14380                 && s->guarded_page
14381                 && !btype_destination_ok(insn, s->bt, s->btype)) {
14382                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
14383                 return;
14384             }
14385         } else {
14386             /* Not the first insn: btype must be 0.  */
14387             tcg_debug_assert(s->btype == 0);
14388         }
14389     }
14390 
14391     s->is_nonstreaming = false;
14392     if (s->sme_trap_nonstreaming) {
14393         disas_sme_fa64(s, insn);
14394     }
14395 
14396     switch (extract32(insn, 25, 4)) {
14397     case 0x0:
14398         if (!extract32(insn, 31, 1) || !disas_sme(s, insn)) {
14399             unallocated_encoding(s);
14400         }
14401         break;
14402     case 0x1: case 0x3: /* UNALLOCATED */
14403         unallocated_encoding(s);
14404         break;
14405     case 0x2:
14406         if (!disas_sve(s, insn)) {
14407             unallocated_encoding(s);
14408         }
14409         break;
14410     case 0x8: case 0x9: /* Data processing - immediate */
14411         disas_data_proc_imm(s, insn);
14412         break;
14413     case 0xa: case 0xb: /* Branch, exception generation and system insns */
14414         disas_b_exc_sys(s, insn);
14415         break;
14416     case 0x4:
14417     case 0x6:
14418     case 0xc:
14419     case 0xe:      /* Loads and stores */
14420         disas_ldst(s, insn);
14421         break;
14422     case 0x5:
14423     case 0xd:      /* Data processing - register */
14424         disas_data_proc_reg(s, insn);
14425         break;
14426     case 0x7:
14427     case 0xf:      /* Data processing - SIMD and floating point */
14428         disas_data_proc_simd_fp(s, insn);
14429         break;
14430     default:
14431         assert(FALSE); /* all 15 cases should be handled above */
14432         break;
14433     }
14434 
14435     /*
14436      * After execution of most insns, btype is reset to 0.
14437      * Note that we set btype == -1 when the insn sets btype.
14438      */
14439     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14440         reset_btype(s);
14441     }
14442 }
14443 
14444 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14445 {
14446     DisasContext *dc = container_of(dcbase, DisasContext, base);
14447 
14448     if (unlikely(dc->ss_active)) {
14449         /* Note that this means single stepping WFI doesn't halt the CPU.
14450          * For conditional branch insns this is harmless unreachable code as
14451          * gen_goto_tb() has already handled emitting the debug exception
14452          * (and thus a tb-jump is not possible when singlestepping).
14453          */
14454         switch (dc->base.is_jmp) {
14455         default:
14456             gen_a64_update_pc(dc, 4);
14457             /* fall through */
14458         case DISAS_EXIT:
14459         case DISAS_JUMP:
14460             gen_step_complete_exception(dc);
14461             break;
14462         case DISAS_NORETURN:
14463             break;
14464         }
14465     } else {
14466         switch (dc->base.is_jmp) {
14467         case DISAS_NEXT:
14468         case DISAS_TOO_MANY:
14469             gen_goto_tb(dc, 1, 4);
14470             break;
14471         default:
14472         case DISAS_UPDATE_EXIT:
14473             gen_a64_update_pc(dc, 4);
14474             /* fall through */
14475         case DISAS_EXIT:
14476             tcg_gen_exit_tb(NULL, 0);
14477             break;
14478         case DISAS_UPDATE_NOCHAIN:
14479             gen_a64_update_pc(dc, 4);
14480             /* fall through */
14481         case DISAS_JUMP:
14482             tcg_gen_lookup_and_goto_ptr();
14483             break;
14484         case DISAS_NORETURN:
14485         case DISAS_SWI:
14486             break;
14487         case DISAS_WFE:
14488             gen_a64_update_pc(dc, 4);
14489             gen_helper_wfe(cpu_env);
14490             break;
14491         case DISAS_YIELD:
14492             gen_a64_update_pc(dc, 4);
14493             gen_helper_yield(cpu_env);
14494             break;
14495         case DISAS_WFI:
14496             /*
14497              * This is a special case because we don't want to just halt
14498              * the CPU if trying to debug across a WFI.
14499              */
14500             gen_a64_update_pc(dc, 4);
14501             gen_helper_wfi(cpu_env, tcg_constant_i32(4));
14502             /*
14503              * The helper doesn't necessarily throw an exception, but we
14504              * must go back to the main loop to check for interrupts anyway.
14505              */
14506             tcg_gen_exit_tb(NULL, 0);
14507             break;
14508         }
14509     }
14510 }
14511 
14512 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14513                                  CPUState *cpu, FILE *logfile)
14514 {
14515     DisasContext *dc = container_of(dcbase, DisasContext, base);
14516 
14517     fprintf(logfile, "IN: %s\n", lookup_symbol(dc->base.pc_first));
14518     target_disas(logfile, cpu, dc->base.pc_first, dc->base.tb->size);
14519 }
14520 
14521 const TranslatorOps aarch64_translator_ops = {
14522     .init_disas_context = aarch64_tr_init_disas_context,
14523     .tb_start           = aarch64_tr_tb_start,
14524     .insn_start         = aarch64_tr_insn_start,
14525     .translate_insn     = aarch64_tr_translate_insn,
14526     .tb_stop            = aarch64_tr_tb_stop,
14527     .disas_log          = aarch64_tr_disas_log,
14528 };
14529