xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision 6f47e7c1)
1 /*
2  *  AArch64 translation
3  *
4  *  Copyright (c) 2013 Alexander Graf <agraf@suse.de>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "qemu/osdep.h"
20 
21 #include "translate.h"
22 #include "translate-a64.h"
23 #include "qemu/log.h"
24 #include "disas/disas.h"
25 #include "arm_ldst.h"
26 #include "semihosting/semihost.h"
27 #include "cpregs.h"
28 
29 static TCGv_i64 cpu_X[32];
30 static TCGv_i64 cpu_pc;
31 
32 /* Load/store exclusive handling */
33 static TCGv_i64 cpu_exclusive_high;
34 
35 static const char *regnames[] = {
36     "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
37     "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
38     "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
39     "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
40 };
41 
42 enum a64_shift_type {
43     A64_SHIFT_TYPE_LSL = 0,
44     A64_SHIFT_TYPE_LSR = 1,
45     A64_SHIFT_TYPE_ASR = 2,
46     A64_SHIFT_TYPE_ROR = 3
47 };
48 
49 /*
50  * Include the generated decoders.
51  */
52 
53 #include "decode-sme-fa64.c.inc"
54 #include "decode-a64.c.inc"
55 
56 /* Table based decoder typedefs - used when the relevant bits for decode
57  * are too awkwardly scattered across the instruction (eg SIMD).
58  */
59 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
60 
61 typedef struct AArch64DecodeTable {
62     uint32_t pattern;
63     uint32_t mask;
64     AArch64DecodeFn *disas_fn;
65 } AArch64DecodeTable;
66 
67 /* initialize TCG globals.  */
68 void a64_translate_init(void)
69 {
70     int i;
71 
72     cpu_pc = tcg_global_mem_new_i64(cpu_env,
73                                     offsetof(CPUARMState, pc),
74                                     "pc");
75     for (i = 0; i < 32; i++) {
76         cpu_X[i] = tcg_global_mem_new_i64(cpu_env,
77                                           offsetof(CPUARMState, xregs[i]),
78                                           regnames[i]);
79     }
80 
81     cpu_exclusive_high = tcg_global_mem_new_i64(cpu_env,
82         offsetof(CPUARMState, exclusive_high), "exclusive_high");
83 }
84 
85 /*
86  * Return the core mmu_idx to use for A64 "unprivileged load/store" insns
87  */
88 static int get_a64_user_mem_index(DisasContext *s)
89 {
90     /*
91      * If AccType_UNPRIV is not used, the insn uses AccType_NORMAL,
92      * which is the usual mmu_idx for this cpu state.
93      */
94     ARMMMUIdx useridx = s->mmu_idx;
95 
96     if (s->unpriv) {
97         /*
98          * We have pre-computed the condition for AccType_UNPRIV.
99          * Therefore we should never get here with a mmu_idx for
100          * which we do not know the corresponding user mmu_idx.
101          */
102         switch (useridx) {
103         case ARMMMUIdx_E10_1:
104         case ARMMMUIdx_E10_1_PAN:
105             useridx = ARMMMUIdx_E10_0;
106             break;
107         case ARMMMUIdx_E20_2:
108         case ARMMMUIdx_E20_2_PAN:
109             useridx = ARMMMUIdx_E20_0;
110             break;
111         default:
112             g_assert_not_reached();
113         }
114     }
115     return arm_to_core_mmu_idx(useridx);
116 }
117 
118 static void set_btype_raw(int val)
119 {
120     tcg_gen_st_i32(tcg_constant_i32(val), cpu_env,
121                    offsetof(CPUARMState, btype));
122 }
123 
124 static void set_btype(DisasContext *s, int val)
125 {
126     /* BTYPE is a 2-bit field, and 0 should be done with reset_btype.  */
127     tcg_debug_assert(val >= 1 && val <= 3);
128     set_btype_raw(val);
129     s->btype = -1;
130 }
131 
132 static void reset_btype(DisasContext *s)
133 {
134     if (s->btype != 0) {
135         set_btype_raw(0);
136         s->btype = 0;
137     }
138 }
139 
140 static void gen_pc_plus_diff(DisasContext *s, TCGv_i64 dest, target_long diff)
141 {
142     assert(s->pc_save != -1);
143     if (tb_cflags(s->base.tb) & CF_PCREL) {
144         tcg_gen_addi_i64(dest, cpu_pc, (s->pc_curr - s->pc_save) + diff);
145     } else {
146         tcg_gen_movi_i64(dest, s->pc_curr + diff);
147     }
148 }
149 
150 void gen_a64_update_pc(DisasContext *s, target_long diff)
151 {
152     gen_pc_plus_diff(s, cpu_pc, diff);
153     s->pc_save = s->pc_curr + diff;
154 }
155 
156 /*
157  * Handle Top Byte Ignore (TBI) bits.
158  *
159  * If address tagging is enabled via the TCR TBI bits:
160  *  + for EL2 and EL3 there is only one TBI bit, and if it is set
161  *    then the address is zero-extended, clearing bits [63:56]
162  *  + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
163  *    and TBI1 controls addressses with bit 55 == 1.
164  *    If the appropriate TBI bit is set for the address then
165  *    the address is sign-extended from bit 55 into bits [63:56]
166  *
167  * Here We have concatenated TBI{1,0} into tbi.
168  */
169 static void gen_top_byte_ignore(DisasContext *s, TCGv_i64 dst,
170                                 TCGv_i64 src, int tbi)
171 {
172     if (tbi == 0) {
173         /* Load unmodified address */
174         tcg_gen_mov_i64(dst, src);
175     } else if (!regime_has_2_ranges(s->mmu_idx)) {
176         /* Force tag byte to all zero */
177         tcg_gen_extract_i64(dst, src, 0, 56);
178     } else {
179         /* Sign-extend from bit 55.  */
180         tcg_gen_sextract_i64(dst, src, 0, 56);
181 
182         switch (tbi) {
183         case 1:
184             /* tbi0 but !tbi1: only use the extension if positive */
185             tcg_gen_and_i64(dst, dst, src);
186             break;
187         case 2:
188             /* !tbi0 but tbi1: only use the extension if negative */
189             tcg_gen_or_i64(dst, dst, src);
190             break;
191         case 3:
192             /* tbi0 and tbi1: always use the extension */
193             break;
194         default:
195             g_assert_not_reached();
196         }
197     }
198 }
199 
200 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
201 {
202     /*
203      * If address tagging is enabled for instructions via the TCR TBI bits,
204      * then loading an address into the PC will clear out any tag.
205      */
206     gen_top_byte_ignore(s, cpu_pc, src, s->tbii);
207     s->pc_save = -1;
208 }
209 
210 /*
211  * Handle MTE and/or TBI.
212  *
213  * For TBI, ideally, we would do nothing.  Proper behaviour on fault is
214  * for the tag to be present in the FAR_ELx register.  But for user-only
215  * mode we do not have a TLB with which to implement this, so we must
216  * remove the top byte now.
217  *
218  * Always return a fresh temporary that we can increment independently
219  * of the write-back address.
220  */
221 
222 TCGv_i64 clean_data_tbi(DisasContext *s, TCGv_i64 addr)
223 {
224     TCGv_i64 clean = tcg_temp_new_i64();
225 #ifdef CONFIG_USER_ONLY
226     gen_top_byte_ignore(s, clean, addr, s->tbid);
227 #else
228     tcg_gen_mov_i64(clean, addr);
229 #endif
230     return clean;
231 }
232 
233 /* Insert a zero tag into src, with the result at dst. */
234 static void gen_address_with_allocation_tag0(TCGv_i64 dst, TCGv_i64 src)
235 {
236     tcg_gen_andi_i64(dst, src, ~MAKE_64BIT_MASK(56, 4));
237 }
238 
239 static void gen_probe_access(DisasContext *s, TCGv_i64 ptr,
240                              MMUAccessType acc, int log2_size)
241 {
242     gen_helper_probe_access(cpu_env, ptr,
243                             tcg_constant_i32(acc),
244                             tcg_constant_i32(get_mem_index(s)),
245                             tcg_constant_i32(1 << log2_size));
246 }
247 
248 /*
249  * For MTE, check a single logical or atomic access.  This probes a single
250  * address, the exact one specified.  The size and alignment of the access
251  * is not relevant to MTE, per se, but watchpoints do require the size,
252  * and we want to recognize those before making any other changes to state.
253  */
254 static TCGv_i64 gen_mte_check1_mmuidx(DisasContext *s, TCGv_i64 addr,
255                                       bool is_write, bool tag_checked,
256                                       int log2_size, bool is_unpriv,
257                                       int core_idx)
258 {
259     if (tag_checked && s->mte_active[is_unpriv]) {
260         TCGv_i64 ret;
261         int desc = 0;
262 
263         desc = FIELD_DP32(desc, MTEDESC, MIDX, core_idx);
264         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
265         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
266         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
267         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, (1 << log2_size) - 1);
268 
269         ret = tcg_temp_new_i64();
270         gen_helper_mte_check(ret, cpu_env, tcg_constant_i32(desc), addr);
271 
272         return ret;
273     }
274     return clean_data_tbi(s, addr);
275 }
276 
277 TCGv_i64 gen_mte_check1(DisasContext *s, TCGv_i64 addr, bool is_write,
278                         bool tag_checked, int log2_size)
279 {
280     return gen_mte_check1_mmuidx(s, addr, is_write, tag_checked, log2_size,
281                                  false, get_mem_index(s));
282 }
283 
284 /*
285  * For MTE, check multiple logical sequential accesses.
286  */
287 TCGv_i64 gen_mte_checkN(DisasContext *s, TCGv_i64 addr, bool is_write,
288                         bool tag_checked, int size)
289 {
290     if (tag_checked && s->mte_active[0]) {
291         TCGv_i64 ret;
292         int desc = 0;
293 
294         desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
295         desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
296         desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
297         desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
298         desc = FIELD_DP32(desc, MTEDESC, SIZEM1, size - 1);
299 
300         ret = tcg_temp_new_i64();
301         gen_helper_mte_check(ret, cpu_env, tcg_constant_i32(desc), addr);
302 
303         return ret;
304     }
305     return clean_data_tbi(s, addr);
306 }
307 
308 typedef struct DisasCompare64 {
309     TCGCond cond;
310     TCGv_i64 value;
311 } DisasCompare64;
312 
313 static void a64_test_cc(DisasCompare64 *c64, int cc)
314 {
315     DisasCompare c32;
316 
317     arm_test_cc(&c32, cc);
318 
319     /*
320      * Sign-extend the 32-bit value so that the GE/LT comparisons work
321      * properly.  The NE/EQ comparisons are also fine with this choice.
322       */
323     c64->cond = c32.cond;
324     c64->value = tcg_temp_new_i64();
325     tcg_gen_ext_i32_i64(c64->value, c32.value);
326 }
327 
328 static void gen_rebuild_hflags(DisasContext *s)
329 {
330     gen_helper_rebuild_hflags_a64(cpu_env, tcg_constant_i32(s->current_el));
331 }
332 
333 static void gen_exception_internal(int excp)
334 {
335     assert(excp_is_internal(excp));
336     gen_helper_exception_internal(cpu_env, tcg_constant_i32(excp));
337 }
338 
339 static void gen_exception_internal_insn(DisasContext *s, int excp)
340 {
341     gen_a64_update_pc(s, 0);
342     gen_exception_internal(excp);
343     s->base.is_jmp = DISAS_NORETURN;
344 }
345 
346 static void gen_exception_bkpt_insn(DisasContext *s, uint32_t syndrome)
347 {
348     gen_a64_update_pc(s, 0);
349     gen_helper_exception_bkpt_insn(cpu_env, tcg_constant_i32(syndrome));
350     s->base.is_jmp = DISAS_NORETURN;
351 }
352 
353 static void gen_step_complete_exception(DisasContext *s)
354 {
355     /* We just completed step of an insn. Move from Active-not-pending
356      * to Active-pending, and then also take the swstep exception.
357      * This corresponds to making the (IMPDEF) choice to prioritize
358      * swstep exceptions over asynchronous exceptions taken to an exception
359      * level where debug is disabled. This choice has the advantage that
360      * we do not need to maintain internal state corresponding to the
361      * ISV/EX syndrome bits between completion of the step and generation
362      * of the exception, and our syndrome information is always correct.
363      */
364     gen_ss_advance(s);
365     gen_swstep_exception(s, 1, s->is_ldex);
366     s->base.is_jmp = DISAS_NORETURN;
367 }
368 
369 static inline bool use_goto_tb(DisasContext *s, uint64_t dest)
370 {
371     if (s->ss_active) {
372         return false;
373     }
374     return translator_use_goto_tb(&s->base, dest);
375 }
376 
377 static void gen_goto_tb(DisasContext *s, int n, int64_t diff)
378 {
379     if (use_goto_tb(s, s->pc_curr + diff)) {
380         /*
381          * For pcrel, the pc must always be up-to-date on entry to
382          * the linked TB, so that it can use simple additions for all
383          * further adjustments.  For !pcrel, the linked TB is compiled
384          * to know its full virtual address, so we can delay the
385          * update to pc to the unlinked path.  A long chain of links
386          * can thus avoid many updates to the PC.
387          */
388         if (tb_cflags(s->base.tb) & CF_PCREL) {
389             gen_a64_update_pc(s, diff);
390             tcg_gen_goto_tb(n);
391         } else {
392             tcg_gen_goto_tb(n);
393             gen_a64_update_pc(s, diff);
394         }
395         tcg_gen_exit_tb(s->base.tb, n);
396         s->base.is_jmp = DISAS_NORETURN;
397     } else {
398         gen_a64_update_pc(s, diff);
399         if (s->ss_active) {
400             gen_step_complete_exception(s);
401         } else {
402             tcg_gen_lookup_and_goto_ptr();
403             s->base.is_jmp = DISAS_NORETURN;
404         }
405     }
406 }
407 
408 /*
409  * Register access functions
410  *
411  * These functions are used for directly accessing a register in where
412  * changes to the final register value are likely to be made. If you
413  * need to use a register for temporary calculation (e.g. index type
414  * operations) use the read_* form.
415  *
416  * B1.2.1 Register mappings
417  *
418  * In instruction register encoding 31 can refer to ZR (zero register) or
419  * the SP (stack pointer) depending on context. In QEMU's case we map SP
420  * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
421  * This is the point of the _sp forms.
422  */
423 TCGv_i64 cpu_reg(DisasContext *s, int reg)
424 {
425     if (reg == 31) {
426         TCGv_i64 t = tcg_temp_new_i64();
427         tcg_gen_movi_i64(t, 0);
428         return t;
429     } else {
430         return cpu_X[reg];
431     }
432 }
433 
434 /* register access for when 31 == SP */
435 TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
436 {
437     return cpu_X[reg];
438 }
439 
440 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
441  * representing the register contents. This TCGv is an auto-freed
442  * temporary so it need not be explicitly freed, and may be modified.
443  */
444 TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
445 {
446     TCGv_i64 v = tcg_temp_new_i64();
447     if (reg != 31) {
448         if (sf) {
449             tcg_gen_mov_i64(v, cpu_X[reg]);
450         } else {
451             tcg_gen_ext32u_i64(v, cpu_X[reg]);
452         }
453     } else {
454         tcg_gen_movi_i64(v, 0);
455     }
456     return v;
457 }
458 
459 TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
460 {
461     TCGv_i64 v = tcg_temp_new_i64();
462     if (sf) {
463         tcg_gen_mov_i64(v, cpu_X[reg]);
464     } else {
465         tcg_gen_ext32u_i64(v, cpu_X[reg]);
466     }
467     return v;
468 }
469 
470 /* Return the offset into CPUARMState of a slice (from
471  * the least significant end) of FP register Qn (ie
472  * Dn, Sn, Hn or Bn).
473  * (Note that this is not the same mapping as for A32; see cpu.h)
474  */
475 static inline int fp_reg_offset(DisasContext *s, int regno, MemOp size)
476 {
477     return vec_reg_offset(s, regno, 0, size);
478 }
479 
480 /* Offset of the high half of the 128 bit vector Qn */
481 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
482 {
483     return vec_reg_offset(s, regno, 1, MO_64);
484 }
485 
486 /* Convenience accessors for reading and writing single and double
487  * FP registers. Writing clears the upper parts of the associated
488  * 128 bit vector register, as required by the architecture.
489  * Note that unlike the GP register accessors, the values returned
490  * by the read functions must be manually freed.
491  */
492 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
493 {
494     TCGv_i64 v = tcg_temp_new_i64();
495 
496     tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
497     return v;
498 }
499 
500 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
501 {
502     TCGv_i32 v = tcg_temp_new_i32();
503 
504     tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
505     return v;
506 }
507 
508 static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
509 {
510     TCGv_i32 v = tcg_temp_new_i32();
511 
512     tcg_gen_ld16u_i32(v, cpu_env, fp_reg_offset(s, reg, MO_16));
513     return v;
514 }
515 
516 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
517  * If SVE is not enabled, then there are only 128 bits in the vector.
518  */
519 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
520 {
521     unsigned ofs = fp_reg_offset(s, rd, MO_64);
522     unsigned vsz = vec_full_reg_size(s);
523 
524     /* Nop move, with side effect of clearing the tail. */
525     tcg_gen_gvec_mov(MO_64, ofs, ofs, is_q ? 16 : 8, vsz);
526 }
527 
528 void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
529 {
530     unsigned ofs = fp_reg_offset(s, reg, MO_64);
531 
532     tcg_gen_st_i64(v, cpu_env, ofs);
533     clear_vec_high(s, false, reg);
534 }
535 
536 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
537 {
538     TCGv_i64 tmp = tcg_temp_new_i64();
539 
540     tcg_gen_extu_i32_i64(tmp, v);
541     write_fp_dreg(s, reg, tmp);
542 }
543 
544 /* Expand a 2-operand AdvSIMD vector operation using an expander function.  */
545 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
546                          GVecGen2Fn *gvec_fn, int vece)
547 {
548     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
549             is_q ? 16 : 8, vec_full_reg_size(s));
550 }
551 
552 /* Expand a 2-operand + immediate AdvSIMD vector operation using
553  * an expander function.
554  */
555 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
556                           int64_t imm, GVecGen2iFn *gvec_fn, int vece)
557 {
558     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
559             imm, is_q ? 16 : 8, vec_full_reg_size(s));
560 }
561 
562 /* Expand a 3-operand AdvSIMD vector operation using an expander function.  */
563 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
564                          GVecGen3Fn *gvec_fn, int vece)
565 {
566     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
567             vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
568 }
569 
570 /* Expand a 4-operand AdvSIMD vector operation using an expander function.  */
571 static void gen_gvec_fn4(DisasContext *s, bool is_q, int rd, int rn, int rm,
572                          int rx, GVecGen4Fn *gvec_fn, int vece)
573 {
574     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
575             vec_full_reg_offset(s, rm), vec_full_reg_offset(s, rx),
576             is_q ? 16 : 8, vec_full_reg_size(s));
577 }
578 
579 /* Expand a 2-operand operation using an out-of-line helper.  */
580 static void gen_gvec_op2_ool(DisasContext *s, bool is_q, int rd,
581                              int rn, int data, gen_helper_gvec_2 *fn)
582 {
583     tcg_gen_gvec_2_ool(vec_full_reg_offset(s, rd),
584                        vec_full_reg_offset(s, rn),
585                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
586 }
587 
588 /* Expand a 3-operand operation using an out-of-line helper.  */
589 static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
590                              int rn, int rm, int data, gen_helper_gvec_3 *fn)
591 {
592     tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
593                        vec_full_reg_offset(s, rn),
594                        vec_full_reg_offset(s, rm),
595                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
596 }
597 
598 /* Expand a 3-operand + fpstatus pointer + simd data value operation using
599  * an out-of-line helper.
600  */
601 static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
602                               int rm, bool is_fp16, int data,
603                               gen_helper_gvec_3_ptr *fn)
604 {
605     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
606     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
607                        vec_full_reg_offset(s, rn),
608                        vec_full_reg_offset(s, rm), fpst,
609                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
610 }
611 
612 /* Expand a 3-operand + qc + operation using an out-of-line helper.  */
613 static void gen_gvec_op3_qc(DisasContext *s, bool is_q, int rd, int rn,
614                             int rm, gen_helper_gvec_3_ptr *fn)
615 {
616     TCGv_ptr qc_ptr = tcg_temp_new_ptr();
617 
618     tcg_gen_addi_ptr(qc_ptr, cpu_env, offsetof(CPUARMState, vfp.qc));
619     tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
620                        vec_full_reg_offset(s, rn),
621                        vec_full_reg_offset(s, rm), qc_ptr,
622                        is_q ? 16 : 8, vec_full_reg_size(s), 0, fn);
623 }
624 
625 /* Expand a 4-operand operation using an out-of-line helper.  */
626 static void gen_gvec_op4_ool(DisasContext *s, bool is_q, int rd, int rn,
627                              int rm, int ra, int data, gen_helper_gvec_4 *fn)
628 {
629     tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
630                        vec_full_reg_offset(s, rn),
631                        vec_full_reg_offset(s, rm),
632                        vec_full_reg_offset(s, ra),
633                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
634 }
635 
636 /*
637  * Expand a 4-operand + fpstatus pointer + simd data value operation using
638  * an out-of-line helper.
639  */
640 static void gen_gvec_op4_fpst(DisasContext *s, bool is_q, int rd, int rn,
641                               int rm, int ra, bool is_fp16, int data,
642                               gen_helper_gvec_4_ptr *fn)
643 {
644     TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
645     tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
646                        vec_full_reg_offset(s, rn),
647                        vec_full_reg_offset(s, rm),
648                        vec_full_reg_offset(s, ra), fpst,
649                        is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
650 }
651 
652 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
653  * than the 32 bit equivalent.
654  */
655 static inline void gen_set_NZ64(TCGv_i64 result)
656 {
657     tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
658     tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
659 }
660 
661 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
662 static inline void gen_logic_CC(int sf, TCGv_i64 result)
663 {
664     if (sf) {
665         gen_set_NZ64(result);
666     } else {
667         tcg_gen_extrl_i64_i32(cpu_ZF, result);
668         tcg_gen_mov_i32(cpu_NF, cpu_ZF);
669     }
670     tcg_gen_movi_i32(cpu_CF, 0);
671     tcg_gen_movi_i32(cpu_VF, 0);
672 }
673 
674 /* dest = T0 + T1; compute C, N, V and Z flags */
675 static void gen_add64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
676 {
677     TCGv_i64 result, flag, tmp;
678     result = tcg_temp_new_i64();
679     flag = tcg_temp_new_i64();
680     tmp = tcg_temp_new_i64();
681 
682     tcg_gen_movi_i64(tmp, 0);
683     tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
684 
685     tcg_gen_extrl_i64_i32(cpu_CF, flag);
686 
687     gen_set_NZ64(result);
688 
689     tcg_gen_xor_i64(flag, result, t0);
690     tcg_gen_xor_i64(tmp, t0, t1);
691     tcg_gen_andc_i64(flag, flag, tmp);
692     tcg_gen_extrh_i64_i32(cpu_VF, flag);
693 
694     tcg_gen_mov_i64(dest, result);
695 }
696 
697 static void gen_add32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
698 {
699     TCGv_i32 t0_32 = tcg_temp_new_i32();
700     TCGv_i32 t1_32 = tcg_temp_new_i32();
701     TCGv_i32 tmp = tcg_temp_new_i32();
702 
703     tcg_gen_movi_i32(tmp, 0);
704     tcg_gen_extrl_i64_i32(t0_32, t0);
705     tcg_gen_extrl_i64_i32(t1_32, t1);
706     tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
707     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
708     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
709     tcg_gen_xor_i32(tmp, t0_32, t1_32);
710     tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
711     tcg_gen_extu_i32_i64(dest, cpu_NF);
712 }
713 
714 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
715 {
716     if (sf) {
717         gen_add64_CC(dest, t0, t1);
718     } else {
719         gen_add32_CC(dest, t0, t1);
720     }
721 }
722 
723 /* dest = T0 - T1; compute C, N, V and Z flags */
724 static void gen_sub64_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
725 {
726     /* 64 bit arithmetic */
727     TCGv_i64 result, flag, tmp;
728 
729     result = tcg_temp_new_i64();
730     flag = tcg_temp_new_i64();
731     tcg_gen_sub_i64(result, t0, t1);
732 
733     gen_set_NZ64(result);
734 
735     tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
736     tcg_gen_extrl_i64_i32(cpu_CF, flag);
737 
738     tcg_gen_xor_i64(flag, result, t0);
739     tmp = tcg_temp_new_i64();
740     tcg_gen_xor_i64(tmp, t0, t1);
741     tcg_gen_and_i64(flag, flag, tmp);
742     tcg_gen_extrh_i64_i32(cpu_VF, flag);
743     tcg_gen_mov_i64(dest, result);
744 }
745 
746 static void gen_sub32_CC(TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
747 {
748     /* 32 bit arithmetic */
749     TCGv_i32 t0_32 = tcg_temp_new_i32();
750     TCGv_i32 t1_32 = tcg_temp_new_i32();
751     TCGv_i32 tmp;
752 
753     tcg_gen_extrl_i64_i32(t0_32, t0);
754     tcg_gen_extrl_i64_i32(t1_32, t1);
755     tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
756     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
757     tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
758     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
759     tmp = tcg_temp_new_i32();
760     tcg_gen_xor_i32(tmp, t0_32, t1_32);
761     tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
762     tcg_gen_extu_i32_i64(dest, cpu_NF);
763 }
764 
765 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
766 {
767     if (sf) {
768         gen_sub64_CC(dest, t0, t1);
769     } else {
770         gen_sub32_CC(dest, t0, t1);
771     }
772 }
773 
774 /* dest = T0 + T1 + CF; do not compute flags. */
775 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
776 {
777     TCGv_i64 flag = tcg_temp_new_i64();
778     tcg_gen_extu_i32_i64(flag, cpu_CF);
779     tcg_gen_add_i64(dest, t0, t1);
780     tcg_gen_add_i64(dest, dest, flag);
781 
782     if (!sf) {
783         tcg_gen_ext32u_i64(dest, dest);
784     }
785 }
786 
787 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
788 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
789 {
790     if (sf) {
791         TCGv_i64 result = tcg_temp_new_i64();
792         TCGv_i64 cf_64 = tcg_temp_new_i64();
793         TCGv_i64 vf_64 = tcg_temp_new_i64();
794         TCGv_i64 tmp = tcg_temp_new_i64();
795         TCGv_i64 zero = tcg_constant_i64(0);
796 
797         tcg_gen_extu_i32_i64(cf_64, cpu_CF);
798         tcg_gen_add2_i64(result, cf_64, t0, zero, cf_64, zero);
799         tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, zero);
800         tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
801         gen_set_NZ64(result);
802 
803         tcg_gen_xor_i64(vf_64, result, t0);
804         tcg_gen_xor_i64(tmp, t0, t1);
805         tcg_gen_andc_i64(vf_64, vf_64, tmp);
806         tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
807 
808         tcg_gen_mov_i64(dest, result);
809     } else {
810         TCGv_i32 t0_32 = tcg_temp_new_i32();
811         TCGv_i32 t1_32 = tcg_temp_new_i32();
812         TCGv_i32 tmp = tcg_temp_new_i32();
813         TCGv_i32 zero = tcg_constant_i32(0);
814 
815         tcg_gen_extrl_i64_i32(t0_32, t0);
816         tcg_gen_extrl_i64_i32(t1_32, t1);
817         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, zero, cpu_CF, zero);
818         tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, zero);
819 
820         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
821         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
822         tcg_gen_xor_i32(tmp, t0_32, t1_32);
823         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
824         tcg_gen_extu_i32_i64(dest, cpu_NF);
825     }
826 }
827 
828 /*
829  * Load/Store generators
830  */
831 
832 /*
833  * Store from GPR register to memory.
834  */
835 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
836                              TCGv_i64 tcg_addr, MemOp memop, int memidx,
837                              bool iss_valid,
838                              unsigned int iss_srt,
839                              bool iss_sf, bool iss_ar)
840 {
841     memop = finalize_memop(s, memop);
842     tcg_gen_qemu_st_i64(source, tcg_addr, memidx, memop);
843 
844     if (iss_valid) {
845         uint32_t syn;
846 
847         syn = syn_data_abort_with_iss(0,
848                                       (memop & MO_SIZE),
849                                       false,
850                                       iss_srt,
851                                       iss_sf,
852                                       iss_ar,
853                                       0, 0, 0, 0, 0, false);
854         disas_set_insn_syndrome(s, syn);
855     }
856 }
857 
858 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
859                       TCGv_i64 tcg_addr, MemOp memop,
860                       bool iss_valid,
861                       unsigned int iss_srt,
862                       bool iss_sf, bool iss_ar)
863 {
864     do_gpr_st_memidx(s, source, tcg_addr, memop, get_mem_index(s),
865                      iss_valid, iss_srt, iss_sf, iss_ar);
866 }
867 
868 /*
869  * Load from memory to GPR register
870  */
871 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
872                              MemOp memop, bool extend, int memidx,
873                              bool iss_valid, unsigned int iss_srt,
874                              bool iss_sf, bool iss_ar)
875 {
876     memop = finalize_memop(s, memop);
877     tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
878 
879     if (extend && (memop & MO_SIGN)) {
880         g_assert((memop & MO_SIZE) <= MO_32);
881         tcg_gen_ext32u_i64(dest, dest);
882     }
883 
884     if (iss_valid) {
885         uint32_t syn;
886 
887         syn = syn_data_abort_with_iss(0,
888                                       (memop & MO_SIZE),
889                                       (memop & MO_SIGN) != 0,
890                                       iss_srt,
891                                       iss_sf,
892                                       iss_ar,
893                                       0, 0, 0, 0, 0, false);
894         disas_set_insn_syndrome(s, syn);
895     }
896 }
897 
898 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
899                       MemOp memop, bool extend,
900                       bool iss_valid, unsigned int iss_srt,
901                       bool iss_sf, bool iss_ar)
902 {
903     do_gpr_ld_memidx(s, dest, tcg_addr, memop, extend, get_mem_index(s),
904                      iss_valid, iss_srt, iss_sf, iss_ar);
905 }
906 
907 /*
908  * Store from FP register to memory
909  */
910 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
911 {
912     /* This writes the bottom N bits of a 128 bit wide vector to memory */
913     TCGv_i64 tmplo = tcg_temp_new_i64();
914     MemOp mop = finalize_memop_asimd(s, size);
915 
916     tcg_gen_ld_i64(tmplo, cpu_env, fp_reg_offset(s, srcidx, MO_64));
917 
918     if (size < MO_128) {
919         tcg_gen_qemu_st_i64(tmplo, tcg_addr, get_mem_index(s), mop);
920     } else {
921         TCGv_i64 tmphi = tcg_temp_new_i64();
922         TCGv_i128 t16 = tcg_temp_new_i128();
923 
924         tcg_gen_ld_i64(tmphi, cpu_env, fp_reg_hi_offset(s, srcidx));
925         tcg_gen_concat_i64_i128(t16, tmplo, tmphi);
926 
927         tcg_gen_qemu_st_i128(t16, tcg_addr, get_mem_index(s), mop);
928     }
929 }
930 
931 /*
932  * Load from memory to FP register
933  */
934 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
935 {
936     /* This always zero-extends and writes to a full 128 bit wide vector */
937     TCGv_i64 tmplo = tcg_temp_new_i64();
938     TCGv_i64 tmphi = NULL;
939     MemOp mop = finalize_memop_asimd(s, size);
940 
941     if (size < MO_128) {
942         tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), mop);
943     } else {
944         TCGv_i128 t16 = tcg_temp_new_i128();
945 
946         tcg_gen_qemu_ld_i128(t16, tcg_addr, get_mem_index(s), mop);
947 
948         tmphi = tcg_temp_new_i64();
949         tcg_gen_extr_i128_i64(tmplo, tmphi, t16);
950     }
951 
952     tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
953 
954     if (tmphi) {
955         tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
956     }
957     clear_vec_high(s, tmphi != NULL, destidx);
958 }
959 
960 /*
961  * Vector load/store helpers.
962  *
963  * The principal difference between this and a FP load is that we don't
964  * zero extend as we are filling a partial chunk of the vector register.
965  * These functions don't support 128 bit loads/stores, which would be
966  * normal load/store operations.
967  *
968  * The _i32 versions are useful when operating on 32 bit quantities
969  * (eg for floating point single or using Neon helper functions).
970  */
971 
972 /* Get value of an element within a vector register */
973 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
974                              int element, MemOp memop)
975 {
976     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
977     switch ((unsigned)memop) {
978     case MO_8:
979         tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
980         break;
981     case MO_16:
982         tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
983         break;
984     case MO_32:
985         tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
986         break;
987     case MO_8|MO_SIGN:
988         tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
989         break;
990     case MO_16|MO_SIGN:
991         tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
992         break;
993     case MO_32|MO_SIGN:
994         tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
995         break;
996     case MO_64:
997     case MO_64|MO_SIGN:
998         tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
999         break;
1000     default:
1001         g_assert_not_reached();
1002     }
1003 }
1004 
1005 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1006                                  int element, MemOp memop)
1007 {
1008     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1009     switch (memop) {
1010     case MO_8:
1011         tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1012         break;
1013     case MO_16:
1014         tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1015         break;
1016     case MO_8|MO_SIGN:
1017         tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1018         break;
1019     case MO_16|MO_SIGN:
1020         tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1021         break;
1022     case MO_32:
1023     case MO_32|MO_SIGN:
1024         tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1025         break;
1026     default:
1027         g_assert_not_reached();
1028     }
1029 }
1030 
1031 /* Set value of an element within a vector register */
1032 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1033                               int element, MemOp memop)
1034 {
1035     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1036     switch (memop) {
1037     case MO_8:
1038         tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1039         break;
1040     case MO_16:
1041         tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1042         break;
1043     case MO_32:
1044         tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1045         break;
1046     case MO_64:
1047         tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1048         break;
1049     default:
1050         g_assert_not_reached();
1051     }
1052 }
1053 
1054 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1055                                   int destidx, int element, MemOp memop)
1056 {
1057     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1058     switch (memop) {
1059     case MO_8:
1060         tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1061         break;
1062     case MO_16:
1063         tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1064         break;
1065     case MO_32:
1066         tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1067         break;
1068     default:
1069         g_assert_not_reached();
1070     }
1071 }
1072 
1073 /* Store from vector register to memory */
1074 static void do_vec_st(DisasContext *s, int srcidx, int element,
1075                       TCGv_i64 tcg_addr, MemOp mop)
1076 {
1077     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1078 
1079     read_vec_element(s, tcg_tmp, srcidx, element, mop & MO_SIZE);
1080     tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1081 }
1082 
1083 /* Load from memory to vector register */
1084 static void do_vec_ld(DisasContext *s, int destidx, int element,
1085                       TCGv_i64 tcg_addr, MemOp mop)
1086 {
1087     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1088 
1089     tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1090     write_vec_element(s, tcg_tmp, destidx, element, mop & MO_SIZE);
1091 }
1092 
1093 /* Check that FP/Neon access is enabled. If it is, return
1094  * true. If not, emit code to generate an appropriate exception,
1095  * and return false; the caller should not emit any code for
1096  * the instruction. Note that this check must happen after all
1097  * unallocated-encoding checks (otherwise the syndrome information
1098  * for the resulting exception will be incorrect).
1099  */
1100 static bool fp_access_check_only(DisasContext *s)
1101 {
1102     if (s->fp_excp_el) {
1103         assert(!s->fp_access_checked);
1104         s->fp_access_checked = true;
1105 
1106         gen_exception_insn_el(s, 0, EXCP_UDEF,
1107                               syn_fp_access_trap(1, 0xe, false, 0),
1108                               s->fp_excp_el);
1109         return false;
1110     }
1111     s->fp_access_checked = true;
1112     return true;
1113 }
1114 
1115 static bool fp_access_check(DisasContext *s)
1116 {
1117     if (!fp_access_check_only(s)) {
1118         return false;
1119     }
1120     if (s->sme_trap_nonstreaming && s->is_nonstreaming) {
1121         gen_exception_insn(s, 0, EXCP_UDEF,
1122                            syn_smetrap(SME_ET_Streaming, false));
1123         return false;
1124     }
1125     return true;
1126 }
1127 
1128 /*
1129  * Check that SVE access is enabled.  If it is, return true.
1130  * If not, emit code to generate an appropriate exception and return false.
1131  * This function corresponds to CheckSVEEnabled().
1132  */
1133 bool sve_access_check(DisasContext *s)
1134 {
1135     if (s->pstate_sm || !dc_isar_feature(aa64_sve, s)) {
1136         assert(dc_isar_feature(aa64_sme, s));
1137         if (!sme_sm_enabled_check(s)) {
1138             goto fail_exit;
1139         }
1140     } else if (s->sve_excp_el) {
1141         gen_exception_insn_el(s, 0, EXCP_UDEF,
1142                               syn_sve_access_trap(), s->sve_excp_el);
1143         goto fail_exit;
1144     }
1145     s->sve_access_checked = true;
1146     return fp_access_check(s);
1147 
1148  fail_exit:
1149     /* Assert that we only raise one exception per instruction. */
1150     assert(!s->sve_access_checked);
1151     s->sve_access_checked = true;
1152     return false;
1153 }
1154 
1155 /*
1156  * Check that SME access is enabled, raise an exception if not.
1157  * Note that this function corresponds to CheckSMEAccess and is
1158  * only used directly for cpregs.
1159  */
1160 static bool sme_access_check(DisasContext *s)
1161 {
1162     if (s->sme_excp_el) {
1163         gen_exception_insn_el(s, 0, EXCP_UDEF,
1164                               syn_smetrap(SME_ET_AccessTrap, false),
1165                               s->sme_excp_el);
1166         return false;
1167     }
1168     return true;
1169 }
1170 
1171 /* This function corresponds to CheckSMEEnabled. */
1172 bool sme_enabled_check(DisasContext *s)
1173 {
1174     /*
1175      * Note that unlike sve_excp_el, we have not constrained sme_excp_el
1176      * to be zero when fp_excp_el has priority.  This is because we need
1177      * sme_excp_el by itself for cpregs access checks.
1178      */
1179     if (!s->fp_excp_el || s->sme_excp_el < s->fp_excp_el) {
1180         s->fp_access_checked = true;
1181         return sme_access_check(s);
1182     }
1183     return fp_access_check_only(s);
1184 }
1185 
1186 /* Common subroutine for CheckSMEAnd*Enabled. */
1187 bool sme_enabled_check_with_svcr(DisasContext *s, unsigned req)
1188 {
1189     if (!sme_enabled_check(s)) {
1190         return false;
1191     }
1192     if (FIELD_EX64(req, SVCR, SM) && !s->pstate_sm) {
1193         gen_exception_insn(s, 0, EXCP_UDEF,
1194                            syn_smetrap(SME_ET_NotStreaming, false));
1195         return false;
1196     }
1197     if (FIELD_EX64(req, SVCR, ZA) && !s->pstate_za) {
1198         gen_exception_insn(s, 0, EXCP_UDEF,
1199                            syn_smetrap(SME_ET_InactiveZA, false));
1200         return false;
1201     }
1202     return true;
1203 }
1204 
1205 /*
1206  * This utility function is for doing register extension with an
1207  * optional shift. You will likely want to pass a temporary for the
1208  * destination register. See DecodeRegExtend() in the ARM ARM.
1209  */
1210 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1211                               int option, unsigned int shift)
1212 {
1213     int extsize = extract32(option, 0, 2);
1214     bool is_signed = extract32(option, 2, 1);
1215 
1216     if (is_signed) {
1217         switch (extsize) {
1218         case 0:
1219             tcg_gen_ext8s_i64(tcg_out, tcg_in);
1220             break;
1221         case 1:
1222             tcg_gen_ext16s_i64(tcg_out, tcg_in);
1223             break;
1224         case 2:
1225             tcg_gen_ext32s_i64(tcg_out, tcg_in);
1226             break;
1227         case 3:
1228             tcg_gen_mov_i64(tcg_out, tcg_in);
1229             break;
1230         }
1231     } else {
1232         switch (extsize) {
1233         case 0:
1234             tcg_gen_ext8u_i64(tcg_out, tcg_in);
1235             break;
1236         case 1:
1237             tcg_gen_ext16u_i64(tcg_out, tcg_in);
1238             break;
1239         case 2:
1240             tcg_gen_ext32u_i64(tcg_out, tcg_in);
1241             break;
1242         case 3:
1243             tcg_gen_mov_i64(tcg_out, tcg_in);
1244             break;
1245         }
1246     }
1247 
1248     if (shift) {
1249         tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1250     }
1251 }
1252 
1253 static inline void gen_check_sp_alignment(DisasContext *s)
1254 {
1255     /* The AArch64 architecture mandates that (if enabled via PSTATE
1256      * or SCTLR bits) there is a check that SP is 16-aligned on every
1257      * SP-relative load or store (with an exception generated if it is not).
1258      * In line with general QEMU practice regarding misaligned accesses,
1259      * we omit these checks for the sake of guest program performance.
1260      * This function is provided as a hook so we can more easily add these
1261      * checks in future (possibly as a "favour catching guest program bugs
1262      * over speed" user selectable option).
1263      */
1264 }
1265 
1266 /*
1267  * This provides a simple table based table lookup decoder. It is
1268  * intended to be used when the relevant bits for decode are too
1269  * awkwardly placed and switch/if based logic would be confusing and
1270  * deeply nested. Since it's a linear search through the table, tables
1271  * should be kept small.
1272  *
1273  * It returns the first handler where insn & mask == pattern, or
1274  * NULL if there is no match.
1275  * The table is terminated by an empty mask (i.e. 0)
1276  */
1277 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1278                                                uint32_t insn)
1279 {
1280     const AArch64DecodeTable *tptr = table;
1281 
1282     while (tptr->mask) {
1283         if ((insn & tptr->mask) == tptr->pattern) {
1284             return tptr->disas_fn;
1285         }
1286         tptr++;
1287     }
1288     return NULL;
1289 }
1290 
1291 /*
1292  * The instruction disassembly implemented here matches
1293  * the instruction encoding classifications in chapter C4
1294  * of the ARM Architecture Reference Manual (DDI0487B_a);
1295  * classification names and decode diagrams here should generally
1296  * match up with those in the manual.
1297  */
1298 
1299 static bool trans_B(DisasContext *s, arg_i *a)
1300 {
1301     reset_btype(s);
1302     gen_goto_tb(s, 0, a->imm);
1303     return true;
1304 }
1305 
1306 static bool trans_BL(DisasContext *s, arg_i *a)
1307 {
1308     gen_pc_plus_diff(s, cpu_reg(s, 30), curr_insn_len(s));
1309     reset_btype(s);
1310     gen_goto_tb(s, 0, a->imm);
1311     return true;
1312 }
1313 
1314 
1315 static bool trans_CBZ(DisasContext *s, arg_cbz *a)
1316 {
1317     DisasLabel match;
1318     TCGv_i64 tcg_cmp;
1319 
1320     tcg_cmp = read_cpu_reg(s, a->rt, a->sf);
1321     reset_btype(s);
1322 
1323     match = gen_disas_label(s);
1324     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1325                         tcg_cmp, 0, match.label);
1326     gen_goto_tb(s, 0, 4);
1327     set_disas_label(s, match);
1328     gen_goto_tb(s, 1, a->imm);
1329     return true;
1330 }
1331 
1332 static bool trans_TBZ(DisasContext *s, arg_tbz *a)
1333 {
1334     DisasLabel match;
1335     TCGv_i64 tcg_cmp;
1336 
1337     tcg_cmp = tcg_temp_new_i64();
1338     tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, a->rt), 1ULL << a->bitpos);
1339 
1340     reset_btype(s);
1341 
1342     match = gen_disas_label(s);
1343     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1344                         tcg_cmp, 0, match.label);
1345     gen_goto_tb(s, 0, 4);
1346     set_disas_label(s, match);
1347     gen_goto_tb(s, 1, a->imm);
1348     return true;
1349 }
1350 
1351 static bool trans_B_cond(DisasContext *s, arg_B_cond *a)
1352 {
1353     reset_btype(s);
1354     if (a->cond < 0x0e) {
1355         /* genuinely conditional branches */
1356         DisasLabel match = gen_disas_label(s);
1357         arm_gen_test_cc(a->cond, match.label);
1358         gen_goto_tb(s, 0, 4);
1359         set_disas_label(s, match);
1360         gen_goto_tb(s, 1, a->imm);
1361     } else {
1362         /* 0xe and 0xf are both "always" conditions */
1363         gen_goto_tb(s, 0, a->imm);
1364     }
1365     return true;
1366 }
1367 
1368 static void set_btype_for_br(DisasContext *s, int rn)
1369 {
1370     if (dc_isar_feature(aa64_bti, s)) {
1371         /* BR to {x16,x17} or !guard -> 1, else 3.  */
1372         set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
1373     }
1374 }
1375 
1376 static void set_btype_for_blr(DisasContext *s)
1377 {
1378     if (dc_isar_feature(aa64_bti, s)) {
1379         /* BLR sets BTYPE to 2, regardless of source guarded page.  */
1380         set_btype(s, 2);
1381     }
1382 }
1383 
1384 static bool trans_BR(DisasContext *s, arg_r *a)
1385 {
1386     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1387     set_btype_for_br(s, a->rn);
1388     s->base.is_jmp = DISAS_JUMP;
1389     return true;
1390 }
1391 
1392 static bool trans_BLR(DisasContext *s, arg_r *a)
1393 {
1394     TCGv_i64 dst = cpu_reg(s, a->rn);
1395     TCGv_i64 lr = cpu_reg(s, 30);
1396     if (dst == lr) {
1397         TCGv_i64 tmp = tcg_temp_new_i64();
1398         tcg_gen_mov_i64(tmp, dst);
1399         dst = tmp;
1400     }
1401     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1402     gen_a64_set_pc(s, dst);
1403     set_btype_for_blr(s);
1404     s->base.is_jmp = DISAS_JUMP;
1405     return true;
1406 }
1407 
1408 static bool trans_RET(DisasContext *s, arg_r *a)
1409 {
1410     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1411     s->base.is_jmp = DISAS_JUMP;
1412     return true;
1413 }
1414 
1415 static TCGv_i64 auth_branch_target(DisasContext *s, TCGv_i64 dst,
1416                                    TCGv_i64 modifier, bool use_key_a)
1417 {
1418     TCGv_i64 truedst;
1419     /*
1420      * Return the branch target for a BRAA/RETA/etc, which is either
1421      * just the destination dst, or that value with the pauth check
1422      * done and the code removed from the high bits.
1423      */
1424     if (!s->pauth_active) {
1425         return dst;
1426     }
1427 
1428     truedst = tcg_temp_new_i64();
1429     if (use_key_a) {
1430         gen_helper_autia(truedst, cpu_env, dst, modifier);
1431     } else {
1432         gen_helper_autib(truedst, cpu_env, dst, modifier);
1433     }
1434     return truedst;
1435 }
1436 
1437 static bool trans_BRAZ(DisasContext *s, arg_braz *a)
1438 {
1439     TCGv_i64 dst;
1440 
1441     if (!dc_isar_feature(aa64_pauth, s)) {
1442         return false;
1443     }
1444 
1445     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1446     gen_a64_set_pc(s, dst);
1447     set_btype_for_br(s, a->rn);
1448     s->base.is_jmp = DISAS_JUMP;
1449     return true;
1450 }
1451 
1452 static bool trans_BLRAZ(DisasContext *s, arg_braz *a)
1453 {
1454     TCGv_i64 dst, lr;
1455 
1456     if (!dc_isar_feature(aa64_pauth, s)) {
1457         return false;
1458     }
1459 
1460     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1461     lr = cpu_reg(s, 30);
1462     if (dst == lr) {
1463         TCGv_i64 tmp = tcg_temp_new_i64();
1464         tcg_gen_mov_i64(tmp, dst);
1465         dst = tmp;
1466     }
1467     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1468     gen_a64_set_pc(s, dst);
1469     set_btype_for_blr(s);
1470     s->base.is_jmp = DISAS_JUMP;
1471     return true;
1472 }
1473 
1474 static bool trans_RETA(DisasContext *s, arg_reta *a)
1475 {
1476     TCGv_i64 dst;
1477 
1478     dst = auth_branch_target(s, cpu_reg(s, 30), cpu_X[31], !a->m);
1479     gen_a64_set_pc(s, dst);
1480     s->base.is_jmp = DISAS_JUMP;
1481     return true;
1482 }
1483 
1484 static bool trans_BRA(DisasContext *s, arg_bra *a)
1485 {
1486     TCGv_i64 dst;
1487 
1488     if (!dc_isar_feature(aa64_pauth, s)) {
1489         return false;
1490     }
1491     dst = auth_branch_target(s, cpu_reg(s,a->rn), cpu_reg_sp(s, a->rm), !a->m);
1492     gen_a64_set_pc(s, dst);
1493     set_btype_for_br(s, a->rn);
1494     s->base.is_jmp = DISAS_JUMP;
1495     return true;
1496 }
1497 
1498 static bool trans_BLRA(DisasContext *s, arg_bra *a)
1499 {
1500     TCGv_i64 dst, lr;
1501 
1502     if (!dc_isar_feature(aa64_pauth, s)) {
1503         return false;
1504     }
1505     dst = auth_branch_target(s, cpu_reg(s, a->rn), cpu_reg_sp(s, a->rm), !a->m);
1506     lr = cpu_reg(s, 30);
1507     if (dst == lr) {
1508         TCGv_i64 tmp = tcg_temp_new_i64();
1509         tcg_gen_mov_i64(tmp, dst);
1510         dst = tmp;
1511     }
1512     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1513     gen_a64_set_pc(s, dst);
1514     set_btype_for_blr(s);
1515     s->base.is_jmp = DISAS_JUMP;
1516     return true;
1517 }
1518 
1519 static bool trans_ERET(DisasContext *s, arg_ERET *a)
1520 {
1521     TCGv_i64 dst;
1522 
1523     if (s->current_el == 0) {
1524         return false;
1525     }
1526     if (s->fgt_eret) {
1527         gen_exception_insn_el(s, 0, EXCP_UDEF, 0, 2);
1528         return true;
1529     }
1530     dst = tcg_temp_new_i64();
1531     tcg_gen_ld_i64(dst, cpu_env,
1532                    offsetof(CPUARMState, elr_el[s->current_el]));
1533 
1534     translator_io_start(&s->base);
1535 
1536     gen_helper_exception_return(cpu_env, dst);
1537     /* Must exit loop to check un-masked IRQs */
1538     s->base.is_jmp = DISAS_EXIT;
1539     return true;
1540 }
1541 
1542 static bool trans_ERETA(DisasContext *s, arg_reta *a)
1543 {
1544     TCGv_i64 dst;
1545 
1546     if (!dc_isar_feature(aa64_pauth, s)) {
1547         return false;
1548     }
1549     if (s->current_el == 0) {
1550         return false;
1551     }
1552     /* The FGT trap takes precedence over an auth trap. */
1553     if (s->fgt_eret) {
1554         gen_exception_insn_el(s, 0, EXCP_UDEF, a->m ? 3 : 2, 2);
1555         return true;
1556     }
1557     dst = tcg_temp_new_i64();
1558     tcg_gen_ld_i64(dst, cpu_env,
1559                    offsetof(CPUARMState, elr_el[s->current_el]));
1560 
1561     dst = auth_branch_target(s, dst, cpu_X[31], !a->m);
1562 
1563     translator_io_start(&s->base);
1564 
1565     gen_helper_exception_return(cpu_env, dst);
1566     /* Must exit loop to check un-masked IRQs */
1567     s->base.is_jmp = DISAS_EXIT;
1568     return true;
1569 }
1570 
1571 /* HINT instruction group, including various allocated HINTs */
1572 static void handle_hint(DisasContext *s, uint32_t insn,
1573                         unsigned int op1, unsigned int op2, unsigned int crm)
1574 {
1575     unsigned int selector = crm << 3 | op2;
1576 
1577     if (op1 != 3) {
1578         unallocated_encoding(s);
1579         return;
1580     }
1581 
1582     switch (selector) {
1583     case 0b00000: /* NOP */
1584         break;
1585     case 0b00011: /* WFI */
1586         s->base.is_jmp = DISAS_WFI;
1587         break;
1588     case 0b00001: /* YIELD */
1589         /* When running in MTTCG we don't generate jumps to the yield and
1590          * WFE helpers as it won't affect the scheduling of other vCPUs.
1591          * If we wanted to more completely model WFE/SEV so we don't busy
1592          * spin unnecessarily we would need to do something more involved.
1593          */
1594         if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1595             s->base.is_jmp = DISAS_YIELD;
1596         }
1597         break;
1598     case 0b00010: /* WFE */
1599         if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1600             s->base.is_jmp = DISAS_WFE;
1601         }
1602         break;
1603     case 0b00100: /* SEV */
1604     case 0b00101: /* SEVL */
1605     case 0b00110: /* DGH */
1606         /* we treat all as NOP at least for now */
1607         break;
1608     case 0b00111: /* XPACLRI */
1609         if (s->pauth_active) {
1610             gen_helper_xpaci(cpu_X[30], cpu_env, cpu_X[30]);
1611         }
1612         break;
1613     case 0b01000: /* PACIA1716 */
1614         if (s->pauth_active) {
1615             gen_helper_pacia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1616         }
1617         break;
1618     case 0b01010: /* PACIB1716 */
1619         if (s->pauth_active) {
1620             gen_helper_pacib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1621         }
1622         break;
1623     case 0b01100: /* AUTIA1716 */
1624         if (s->pauth_active) {
1625             gen_helper_autia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1626         }
1627         break;
1628     case 0b01110: /* AUTIB1716 */
1629         if (s->pauth_active) {
1630             gen_helper_autib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1631         }
1632         break;
1633     case 0b10000: /* ESB */
1634         /* Without RAS, we must implement this as NOP. */
1635         if (dc_isar_feature(aa64_ras, s)) {
1636             /*
1637              * QEMU does not have a source of physical SErrors,
1638              * so we are only concerned with virtual SErrors.
1639              * The pseudocode in the ARM for this case is
1640              *   if PSTATE.EL IN {EL0, EL1} && EL2Enabled() then
1641              *      AArch64.vESBOperation();
1642              * Most of the condition can be evaluated at translation time.
1643              * Test for EL2 present, and defer test for SEL2 to runtime.
1644              */
1645             if (s->current_el <= 1 && arm_dc_feature(s, ARM_FEATURE_EL2)) {
1646                 gen_helper_vesb(cpu_env);
1647             }
1648         }
1649         break;
1650     case 0b11000: /* PACIAZ */
1651         if (s->pauth_active) {
1652             gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30],
1653                              tcg_constant_i64(0));
1654         }
1655         break;
1656     case 0b11001: /* PACIASP */
1657         if (s->pauth_active) {
1658             gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1659         }
1660         break;
1661     case 0b11010: /* PACIBZ */
1662         if (s->pauth_active) {
1663             gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30],
1664                              tcg_constant_i64(0));
1665         }
1666         break;
1667     case 0b11011: /* PACIBSP */
1668         if (s->pauth_active) {
1669             gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1670         }
1671         break;
1672     case 0b11100: /* AUTIAZ */
1673         if (s->pauth_active) {
1674             gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30],
1675                              tcg_constant_i64(0));
1676         }
1677         break;
1678     case 0b11101: /* AUTIASP */
1679         if (s->pauth_active) {
1680             gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1681         }
1682         break;
1683     case 0b11110: /* AUTIBZ */
1684         if (s->pauth_active) {
1685             gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30],
1686                              tcg_constant_i64(0));
1687         }
1688         break;
1689     case 0b11111: /* AUTIBSP */
1690         if (s->pauth_active) {
1691             gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1692         }
1693         break;
1694     default:
1695         /* default specified as NOP equivalent */
1696         break;
1697     }
1698 }
1699 
1700 static void gen_clrex(DisasContext *s, uint32_t insn)
1701 {
1702     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1703 }
1704 
1705 /* CLREX, DSB, DMB, ISB */
1706 static void handle_sync(DisasContext *s, uint32_t insn,
1707                         unsigned int op1, unsigned int op2, unsigned int crm)
1708 {
1709     TCGBar bar;
1710 
1711     if (op1 != 3) {
1712         unallocated_encoding(s);
1713         return;
1714     }
1715 
1716     switch (op2) {
1717     case 2: /* CLREX */
1718         gen_clrex(s, insn);
1719         return;
1720     case 4: /* DSB */
1721     case 5: /* DMB */
1722         switch (crm & 3) {
1723         case 1: /* MBReqTypes_Reads */
1724             bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1725             break;
1726         case 2: /* MBReqTypes_Writes */
1727             bar = TCG_BAR_SC | TCG_MO_ST_ST;
1728             break;
1729         default: /* MBReqTypes_All */
1730             bar = TCG_BAR_SC | TCG_MO_ALL;
1731             break;
1732         }
1733         tcg_gen_mb(bar);
1734         return;
1735     case 6: /* ISB */
1736         /* We need to break the TB after this insn to execute
1737          * a self-modified code correctly and also to take
1738          * any pending interrupts immediately.
1739          */
1740         reset_btype(s);
1741         gen_goto_tb(s, 0, 4);
1742         return;
1743 
1744     case 7: /* SB */
1745         if (crm != 0 || !dc_isar_feature(aa64_sb, s)) {
1746             goto do_unallocated;
1747         }
1748         /*
1749          * TODO: There is no speculation barrier opcode for TCG;
1750          * MB and end the TB instead.
1751          */
1752         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1753         gen_goto_tb(s, 0, 4);
1754         return;
1755 
1756     default:
1757     do_unallocated:
1758         unallocated_encoding(s);
1759         return;
1760     }
1761 }
1762 
1763 static void gen_xaflag(void)
1764 {
1765     TCGv_i32 z = tcg_temp_new_i32();
1766 
1767     tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1768 
1769     /*
1770      * (!C & !Z) << 31
1771      * (!(C | Z)) << 31
1772      * ~((C | Z) << 31)
1773      * ~-(C | Z)
1774      * (C | Z) - 1
1775      */
1776     tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1777     tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1778 
1779     /* !(Z & C) */
1780     tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1781     tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1782 
1783     /* (!C & Z) << 31 -> -(Z & ~C) */
1784     tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1785     tcg_gen_neg_i32(cpu_VF, cpu_VF);
1786 
1787     /* C | Z */
1788     tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1789 }
1790 
1791 static void gen_axflag(void)
1792 {
1793     tcg_gen_sari_i32(cpu_VF, cpu_VF, 31);         /* V ? -1 : 0 */
1794     tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF);     /* C & !V */
1795 
1796     /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1797     tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1798 
1799     tcg_gen_movi_i32(cpu_NF, 0);
1800     tcg_gen_movi_i32(cpu_VF, 0);
1801 }
1802 
1803 /* MSR (immediate) - move immediate to processor state field */
1804 static void handle_msr_i(DisasContext *s, uint32_t insn,
1805                          unsigned int op1, unsigned int op2, unsigned int crm)
1806 {
1807     int op = op1 << 3 | op2;
1808 
1809     /* End the TB by default, chaining is ok.  */
1810     s->base.is_jmp = DISAS_TOO_MANY;
1811 
1812     switch (op) {
1813     case 0x00: /* CFINV */
1814         if (crm != 0 || !dc_isar_feature(aa64_condm_4, s)) {
1815             goto do_unallocated;
1816         }
1817         tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1818         s->base.is_jmp = DISAS_NEXT;
1819         break;
1820 
1821     case 0x01: /* XAFlag */
1822         if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1823             goto do_unallocated;
1824         }
1825         gen_xaflag();
1826         s->base.is_jmp = DISAS_NEXT;
1827         break;
1828 
1829     case 0x02: /* AXFlag */
1830         if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1831             goto do_unallocated;
1832         }
1833         gen_axflag();
1834         s->base.is_jmp = DISAS_NEXT;
1835         break;
1836 
1837     case 0x03: /* UAO */
1838         if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1839             goto do_unallocated;
1840         }
1841         if (crm & 1) {
1842             set_pstate_bits(PSTATE_UAO);
1843         } else {
1844             clear_pstate_bits(PSTATE_UAO);
1845         }
1846         gen_rebuild_hflags(s);
1847         break;
1848 
1849     case 0x04: /* PAN */
1850         if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1851             goto do_unallocated;
1852         }
1853         if (crm & 1) {
1854             set_pstate_bits(PSTATE_PAN);
1855         } else {
1856             clear_pstate_bits(PSTATE_PAN);
1857         }
1858         gen_rebuild_hflags(s);
1859         break;
1860 
1861     case 0x05: /* SPSel */
1862         if (s->current_el == 0) {
1863             goto do_unallocated;
1864         }
1865         gen_helper_msr_i_spsel(cpu_env, tcg_constant_i32(crm & PSTATE_SP));
1866         break;
1867 
1868     case 0x19: /* SSBS */
1869         if (!dc_isar_feature(aa64_ssbs, s)) {
1870             goto do_unallocated;
1871         }
1872         if (crm & 1) {
1873             set_pstate_bits(PSTATE_SSBS);
1874         } else {
1875             clear_pstate_bits(PSTATE_SSBS);
1876         }
1877         /* Don't need to rebuild hflags since SSBS is a nop */
1878         break;
1879 
1880     case 0x1a: /* DIT */
1881         if (!dc_isar_feature(aa64_dit, s)) {
1882             goto do_unallocated;
1883         }
1884         if (crm & 1) {
1885             set_pstate_bits(PSTATE_DIT);
1886         } else {
1887             clear_pstate_bits(PSTATE_DIT);
1888         }
1889         /* There's no need to rebuild hflags because DIT is a nop */
1890         break;
1891 
1892     case 0x1e: /* DAIFSet */
1893         gen_helper_msr_i_daifset(cpu_env, tcg_constant_i32(crm));
1894         break;
1895 
1896     case 0x1f: /* DAIFClear */
1897         gen_helper_msr_i_daifclear(cpu_env, tcg_constant_i32(crm));
1898         /* For DAIFClear, exit the cpu loop to re-evaluate pending IRQs.  */
1899         s->base.is_jmp = DISAS_UPDATE_EXIT;
1900         break;
1901 
1902     case 0x1c: /* TCO */
1903         if (dc_isar_feature(aa64_mte, s)) {
1904             /* Full MTE is enabled -- set the TCO bit as directed. */
1905             if (crm & 1) {
1906                 set_pstate_bits(PSTATE_TCO);
1907             } else {
1908                 clear_pstate_bits(PSTATE_TCO);
1909             }
1910             gen_rebuild_hflags(s);
1911             /* Many factors, including TCO, go into MTE_ACTIVE. */
1912             s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
1913         } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
1914             /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI.  */
1915             s->base.is_jmp = DISAS_NEXT;
1916         } else {
1917             goto do_unallocated;
1918         }
1919         break;
1920 
1921     case 0x1b: /* SVCR* */
1922         if (!dc_isar_feature(aa64_sme, s) || crm < 2 || crm > 7) {
1923             goto do_unallocated;
1924         }
1925         if (sme_access_check(s)) {
1926             int old = s->pstate_sm | (s->pstate_za << 1);
1927             int new = (crm & 1) * 3;
1928             int msk = (crm >> 1) & 3;
1929 
1930             if ((old ^ new) & msk) {
1931                 /* At least one bit changes. */
1932                 gen_helper_set_svcr(cpu_env, tcg_constant_i32(new),
1933                                     tcg_constant_i32(msk));
1934             } else {
1935                 s->base.is_jmp = DISAS_NEXT;
1936             }
1937         }
1938         break;
1939 
1940     default:
1941     do_unallocated:
1942         unallocated_encoding(s);
1943         return;
1944     }
1945 }
1946 
1947 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1948 {
1949     TCGv_i32 tmp = tcg_temp_new_i32();
1950     TCGv_i32 nzcv = tcg_temp_new_i32();
1951 
1952     /* build bit 31, N */
1953     tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
1954     /* build bit 30, Z */
1955     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1956     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1957     /* build bit 29, C */
1958     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1959     /* build bit 28, V */
1960     tcg_gen_shri_i32(tmp, cpu_VF, 31);
1961     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1962     /* generate result */
1963     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1964 }
1965 
1966 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1967 {
1968     TCGv_i32 nzcv = tcg_temp_new_i32();
1969 
1970     /* take NZCV from R[t] */
1971     tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
1972 
1973     /* bit 31, N */
1974     tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
1975     /* bit 30, Z */
1976     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1977     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1978     /* bit 29, C */
1979     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1980     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1981     /* bit 28, V */
1982     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1983     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1984 }
1985 
1986 static void gen_sysreg_undef(DisasContext *s, bool isread,
1987                              uint8_t op0, uint8_t op1, uint8_t op2,
1988                              uint8_t crn, uint8_t crm, uint8_t rt)
1989 {
1990     /*
1991      * Generate code to emit an UNDEF with correct syndrome
1992      * information for a failed system register access.
1993      * This is EC_UNCATEGORIZED (ie a standard UNDEF) in most cases,
1994      * but if FEAT_IDST is implemented then read accesses to registers
1995      * in the feature ID space are reported with the EC_SYSTEMREGISTERTRAP
1996      * syndrome.
1997      */
1998     uint32_t syndrome;
1999 
2000     if (isread && dc_isar_feature(aa64_ids, s) &&
2001         arm_cpreg_encoding_in_idspace(op0, op1, op2, crn, crm)) {
2002         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2003     } else {
2004         syndrome = syn_uncategorized();
2005     }
2006     gen_exception_insn(s, 0, EXCP_UDEF, syndrome);
2007 }
2008 
2009 /* MRS - move from system register
2010  * MSR (register) - move to system register
2011  * SYS
2012  * SYSL
2013  * These are all essentially the same insn in 'read' and 'write'
2014  * versions, with varying op0 fields.
2015  */
2016 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
2017                        unsigned int op0, unsigned int op1, unsigned int op2,
2018                        unsigned int crn, unsigned int crm, unsigned int rt)
2019 {
2020     uint32_t key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
2021                                       crn, crm, op0, op1, op2);
2022     const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
2023     bool need_exit_tb = false;
2024     TCGv_ptr tcg_ri = NULL;
2025     TCGv_i64 tcg_rt;
2026 
2027     if (!ri) {
2028         /* Unknown register; this might be a guest error or a QEMU
2029          * unimplemented feature.
2030          */
2031         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
2032                       "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
2033                       isread ? "read" : "write", op0, op1, crn, crm, op2);
2034         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2035         return;
2036     }
2037 
2038     /* Check access permissions */
2039     if (!cp_access_ok(s->current_el, ri, isread)) {
2040         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2041         return;
2042     }
2043 
2044     if (ri->accessfn || (ri->fgt && s->fgt_active)) {
2045         /* Emit code to perform further access permissions checks at
2046          * runtime; this may result in an exception.
2047          */
2048         uint32_t syndrome;
2049 
2050         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2051         gen_a64_update_pc(s, 0);
2052         tcg_ri = tcg_temp_new_ptr();
2053         gen_helper_access_check_cp_reg(tcg_ri, cpu_env,
2054                                        tcg_constant_i32(key),
2055                                        tcg_constant_i32(syndrome),
2056                                        tcg_constant_i32(isread));
2057     } else if (ri->type & ARM_CP_RAISES_EXC) {
2058         /*
2059          * The readfn or writefn might raise an exception;
2060          * synchronize the CPU state in case it does.
2061          */
2062         gen_a64_update_pc(s, 0);
2063     }
2064 
2065     /* Handle special cases first */
2066     switch (ri->type & ARM_CP_SPECIAL_MASK) {
2067     case 0:
2068         break;
2069     case ARM_CP_NOP:
2070         return;
2071     case ARM_CP_NZCV:
2072         tcg_rt = cpu_reg(s, rt);
2073         if (isread) {
2074             gen_get_nzcv(tcg_rt);
2075         } else {
2076             gen_set_nzcv(tcg_rt);
2077         }
2078         return;
2079     case ARM_CP_CURRENTEL:
2080         /* Reads as current EL value from pstate, which is
2081          * guaranteed to be constant by the tb flags.
2082          */
2083         tcg_rt = cpu_reg(s, rt);
2084         tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
2085         return;
2086     case ARM_CP_DC_ZVA:
2087         /* Writes clear the aligned block of memory which rt points into. */
2088         if (s->mte_active[0]) {
2089             int desc = 0;
2090 
2091             desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
2092             desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
2093             desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
2094 
2095             tcg_rt = tcg_temp_new_i64();
2096             gen_helper_mte_check_zva(tcg_rt, cpu_env,
2097                                      tcg_constant_i32(desc), cpu_reg(s, rt));
2098         } else {
2099             tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
2100         }
2101         gen_helper_dc_zva(cpu_env, tcg_rt);
2102         return;
2103     case ARM_CP_DC_GVA:
2104         {
2105             TCGv_i64 clean_addr, tag;
2106 
2107             /*
2108              * DC_GVA, like DC_ZVA, requires that we supply the original
2109              * pointer for an invalid page.  Probe that address first.
2110              */
2111             tcg_rt = cpu_reg(s, rt);
2112             clean_addr = clean_data_tbi(s, tcg_rt);
2113             gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
2114 
2115             if (s->ata) {
2116                 /* Extract the tag from the register to match STZGM.  */
2117                 tag = tcg_temp_new_i64();
2118                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2119                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2120             }
2121         }
2122         return;
2123     case ARM_CP_DC_GZVA:
2124         {
2125             TCGv_i64 clean_addr, tag;
2126 
2127             /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
2128             tcg_rt = cpu_reg(s, rt);
2129             clean_addr = clean_data_tbi(s, tcg_rt);
2130             gen_helper_dc_zva(cpu_env, clean_addr);
2131 
2132             if (s->ata) {
2133                 /* Extract the tag from the register to match STZGM.  */
2134                 tag = tcg_temp_new_i64();
2135                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2136                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2137             }
2138         }
2139         return;
2140     default:
2141         g_assert_not_reached();
2142     }
2143     if ((ri->type & ARM_CP_FPU) && !fp_access_check_only(s)) {
2144         return;
2145     } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
2146         return;
2147     } else if ((ri->type & ARM_CP_SME) && !sme_access_check(s)) {
2148         return;
2149     }
2150 
2151     if (ri->type & ARM_CP_IO) {
2152         /* I/O operations must end the TB here (whether read or write) */
2153         need_exit_tb = translator_io_start(&s->base);
2154     }
2155 
2156     tcg_rt = cpu_reg(s, rt);
2157 
2158     if (isread) {
2159         if (ri->type & ARM_CP_CONST) {
2160             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
2161         } else if (ri->readfn) {
2162             if (!tcg_ri) {
2163                 tcg_ri = gen_lookup_cp_reg(key);
2164             }
2165             gen_helper_get_cp_reg64(tcg_rt, cpu_env, tcg_ri);
2166         } else {
2167             tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
2168         }
2169     } else {
2170         if (ri->type & ARM_CP_CONST) {
2171             /* If not forbidden by access permissions, treat as WI */
2172             return;
2173         } else if (ri->writefn) {
2174             if (!tcg_ri) {
2175                 tcg_ri = gen_lookup_cp_reg(key);
2176             }
2177             gen_helper_set_cp_reg64(cpu_env, tcg_ri, tcg_rt);
2178         } else {
2179             tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
2180         }
2181     }
2182 
2183     if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
2184         /*
2185          * A write to any coprocessor regiser that ends a TB
2186          * must rebuild the hflags for the next TB.
2187          */
2188         gen_rebuild_hflags(s);
2189         /*
2190          * We default to ending the TB on a coprocessor register write,
2191          * but allow this to be suppressed by the register definition
2192          * (usually only necessary to work around guest bugs).
2193          */
2194         need_exit_tb = true;
2195     }
2196     if (need_exit_tb) {
2197         s->base.is_jmp = DISAS_UPDATE_EXIT;
2198     }
2199 }
2200 
2201 /* System
2202  *  31                 22 21  20 19 18 16 15   12 11    8 7   5 4    0
2203  * +---------------------+---+-----+-----+-------+-------+-----+------+
2204  * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 |  CRn  |  CRm  | op2 |  Rt  |
2205  * +---------------------+---+-----+-----+-------+-------+-----+------+
2206  */
2207 static void disas_system(DisasContext *s, uint32_t insn)
2208 {
2209     unsigned int l, op0, op1, crn, crm, op2, rt;
2210     l = extract32(insn, 21, 1);
2211     op0 = extract32(insn, 19, 2);
2212     op1 = extract32(insn, 16, 3);
2213     crn = extract32(insn, 12, 4);
2214     crm = extract32(insn, 8, 4);
2215     op2 = extract32(insn, 5, 3);
2216     rt = extract32(insn, 0, 5);
2217 
2218     if (op0 == 0) {
2219         if (l || rt != 31) {
2220             unallocated_encoding(s);
2221             return;
2222         }
2223         switch (crn) {
2224         case 2: /* HINT (including allocated hints like NOP, YIELD, etc) */
2225             handle_hint(s, insn, op1, op2, crm);
2226             break;
2227         case 3: /* CLREX, DSB, DMB, ISB */
2228             handle_sync(s, insn, op1, op2, crm);
2229             break;
2230         case 4: /* MSR (immediate) */
2231             handle_msr_i(s, insn, op1, op2, crm);
2232             break;
2233         default:
2234             unallocated_encoding(s);
2235             break;
2236         }
2237         return;
2238     }
2239     handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
2240 }
2241 
2242 /* Exception generation
2243  *
2244  *  31             24 23 21 20                     5 4   2 1  0
2245  * +-----------------+-----+------------------------+-----+----+
2246  * | 1 1 0 1 0 1 0 0 | opc |          imm16         | op2 | LL |
2247  * +-----------------------+------------------------+----------+
2248  */
2249 static void disas_exc(DisasContext *s, uint32_t insn)
2250 {
2251     int opc = extract32(insn, 21, 3);
2252     int op2_ll = extract32(insn, 0, 5);
2253     int imm16 = extract32(insn, 5, 16);
2254     uint32_t syndrome;
2255 
2256     switch (opc) {
2257     case 0:
2258         /* For SVC, HVC and SMC we advance the single-step state
2259          * machine before taking the exception. This is architecturally
2260          * mandated, to ensure that single-stepping a system call
2261          * instruction works properly.
2262          */
2263         switch (op2_ll) {
2264         case 1:                                                     /* SVC */
2265             syndrome = syn_aa64_svc(imm16);
2266             if (s->fgt_svc) {
2267                 gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2268                 break;
2269             }
2270             gen_ss_advance(s);
2271             gen_exception_insn(s, 4, EXCP_SWI, syndrome);
2272             break;
2273         case 2:                                                     /* HVC */
2274             if (s->current_el == 0) {
2275                 unallocated_encoding(s);
2276                 break;
2277             }
2278             /* The pre HVC helper handles cases when HVC gets trapped
2279              * as an undefined insn by runtime configuration.
2280              */
2281             gen_a64_update_pc(s, 0);
2282             gen_helper_pre_hvc(cpu_env);
2283             gen_ss_advance(s);
2284             gen_exception_insn_el(s, 4, EXCP_HVC, syn_aa64_hvc(imm16), 2);
2285             break;
2286         case 3:                                                     /* SMC */
2287             if (s->current_el == 0) {
2288                 unallocated_encoding(s);
2289                 break;
2290             }
2291             gen_a64_update_pc(s, 0);
2292             gen_helper_pre_smc(cpu_env, tcg_constant_i32(syn_aa64_smc(imm16)));
2293             gen_ss_advance(s);
2294             gen_exception_insn_el(s, 4, EXCP_SMC, syn_aa64_smc(imm16), 3);
2295             break;
2296         default:
2297             unallocated_encoding(s);
2298             break;
2299         }
2300         break;
2301     case 1:
2302         if (op2_ll != 0) {
2303             unallocated_encoding(s);
2304             break;
2305         }
2306         /* BRK */
2307         gen_exception_bkpt_insn(s, syn_aa64_bkpt(imm16));
2308         break;
2309     case 2:
2310         if (op2_ll != 0) {
2311             unallocated_encoding(s);
2312             break;
2313         }
2314         /* HLT. This has two purposes.
2315          * Architecturally, it is an external halting debug instruction.
2316          * Since QEMU doesn't implement external debug, we treat this as
2317          * it is required for halting debug disabled: it will UNDEF.
2318          * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2319          */
2320         if (semihosting_enabled(s->current_el == 0) && imm16 == 0xf000) {
2321             gen_exception_internal_insn(s, EXCP_SEMIHOST);
2322         } else {
2323             unallocated_encoding(s);
2324         }
2325         break;
2326     case 5:
2327         if (op2_ll < 1 || op2_ll > 3) {
2328             unallocated_encoding(s);
2329             break;
2330         }
2331         /* DCPS1, DCPS2, DCPS3 */
2332         unallocated_encoding(s);
2333         break;
2334     default:
2335         unallocated_encoding(s);
2336         break;
2337     }
2338 }
2339 
2340 /* Branches, exception generating and system instructions */
2341 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
2342 {
2343     switch (extract32(insn, 25, 7)) {
2344     case 0x6a: /* Exception generation / System */
2345         if (insn & (1 << 24)) {
2346             if (extract32(insn, 22, 2) == 0) {
2347                 disas_system(s, insn);
2348             } else {
2349                 unallocated_encoding(s);
2350             }
2351         } else {
2352             disas_exc(s, insn);
2353         }
2354         break;
2355     default:
2356         unallocated_encoding(s);
2357         break;
2358     }
2359 }
2360 
2361 /*
2362  * Load/Store exclusive instructions are implemented by remembering
2363  * the value/address loaded, and seeing if these are the same
2364  * when the store is performed. This is not actually the architecturally
2365  * mandated semantics, but it works for typical guest code sequences
2366  * and avoids having to monitor regular stores.
2367  *
2368  * The store exclusive uses the atomic cmpxchg primitives to avoid
2369  * races in multi-threaded linux-user and when MTTCG softmmu is
2370  * enabled.
2371  */
2372 static void gen_load_exclusive(DisasContext *s, int rt, int rt2, int rn,
2373                                int size, bool is_pair)
2374 {
2375     int idx = get_mem_index(s);
2376     MemOp memop;
2377     TCGv_i64 dirty_addr, clean_addr;
2378 
2379     s->is_ldex = true;
2380     dirty_addr = cpu_reg_sp(s, rn);
2381     clean_addr = gen_mte_check1(s, dirty_addr, false, rn != 31, size);
2382 
2383     g_assert(size <= 3);
2384     if (is_pair) {
2385         g_assert(size >= 2);
2386         if (size == 2) {
2387             /* The pair must be single-copy atomic for the doubleword.  */
2388             memop = finalize_memop(s, MO_64 | MO_ALIGN);
2389             tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2390             if (s->be_data == MO_LE) {
2391                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2392                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2393             } else {
2394                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2395                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2396             }
2397         } else {
2398             /*
2399              * The pair must be single-copy atomic for *each* doubleword, not
2400              * the entire quadword, however it must be quadword aligned.
2401              * Expose the complete load to tcg, for ease of tlb lookup,
2402              * but indicate that only 8-byte atomicity is required.
2403              */
2404             TCGv_i128 t16 = tcg_temp_new_i128();
2405 
2406             memop = finalize_memop_atom(s, MO_128 | MO_ALIGN_16,
2407                                         MO_ATOM_IFALIGN_PAIR);
2408             tcg_gen_qemu_ld_i128(t16, clean_addr, idx, memop);
2409 
2410             if (s->be_data == MO_LE) {
2411                 tcg_gen_extr_i128_i64(cpu_exclusive_val,
2412                                       cpu_exclusive_high, t16);
2413             } else {
2414                 tcg_gen_extr_i128_i64(cpu_exclusive_high,
2415                                       cpu_exclusive_val, t16);
2416             }
2417             tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2418             tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2419         }
2420     } else {
2421         memop = finalize_memop(s, size | MO_ALIGN);
2422         tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2423         tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2424     }
2425     tcg_gen_mov_i64(cpu_exclusive_addr, clean_addr);
2426 }
2427 
2428 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2429                                 int rn, int size, int is_pair)
2430 {
2431     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2432      *     && (!is_pair || env->exclusive_high == [addr + datasize])) {
2433      *     [addr] = {Rt};
2434      *     if (is_pair) {
2435      *         [addr + datasize] = {Rt2};
2436      *     }
2437      *     {Rd} = 0;
2438      * } else {
2439      *     {Rd} = 1;
2440      * }
2441      * env->exclusive_addr = -1;
2442      */
2443     TCGLabel *fail_label = gen_new_label();
2444     TCGLabel *done_label = gen_new_label();
2445     TCGv_i64 tmp, dirty_addr, clean_addr;
2446 
2447     dirty_addr = cpu_reg_sp(s, rn);
2448     clean_addr = gen_mte_check1(s, dirty_addr, true, rn != 31, size);
2449 
2450     tcg_gen_brcond_i64(TCG_COND_NE, clean_addr, cpu_exclusive_addr, fail_label);
2451 
2452     tmp = tcg_temp_new_i64();
2453     if (is_pair) {
2454         if (size == 2) {
2455             if (s->be_data == MO_LE) {
2456                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2457             } else {
2458                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2459             }
2460             tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2461                                        cpu_exclusive_val, tmp,
2462                                        get_mem_index(s),
2463                                        MO_64 | MO_ALIGN | s->be_data);
2464             tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2465         } else {
2466             TCGv_i128 t16 = tcg_temp_new_i128();
2467             TCGv_i128 c16 = tcg_temp_new_i128();
2468             TCGv_i64 a, b;
2469 
2470             if (s->be_data == MO_LE) {
2471                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt), cpu_reg(s, rt2));
2472                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_val,
2473                                         cpu_exclusive_high);
2474             } else {
2475                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt2), cpu_reg(s, rt));
2476                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_high,
2477                                         cpu_exclusive_val);
2478             }
2479 
2480             tcg_gen_atomic_cmpxchg_i128(t16, cpu_exclusive_addr, c16, t16,
2481                                         get_mem_index(s),
2482                                         MO_128 | MO_ALIGN | s->be_data);
2483 
2484             a = tcg_temp_new_i64();
2485             b = tcg_temp_new_i64();
2486             if (s->be_data == MO_LE) {
2487                 tcg_gen_extr_i128_i64(a, b, t16);
2488             } else {
2489                 tcg_gen_extr_i128_i64(b, a, t16);
2490             }
2491 
2492             tcg_gen_xor_i64(a, a, cpu_exclusive_val);
2493             tcg_gen_xor_i64(b, b, cpu_exclusive_high);
2494             tcg_gen_or_i64(tmp, a, b);
2495 
2496             tcg_gen_setcondi_i64(TCG_COND_NE, tmp, tmp, 0);
2497         }
2498     } else {
2499         tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2500                                    cpu_reg(s, rt), get_mem_index(s),
2501                                    size | MO_ALIGN | s->be_data);
2502         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2503     }
2504     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2505     tcg_gen_br(done_label);
2506 
2507     gen_set_label(fail_label);
2508     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2509     gen_set_label(done_label);
2510     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2511 }
2512 
2513 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2514                                  int rn, int size)
2515 {
2516     TCGv_i64 tcg_rs = cpu_reg(s, rs);
2517     TCGv_i64 tcg_rt = cpu_reg(s, rt);
2518     int memidx = get_mem_index(s);
2519     TCGv_i64 clean_addr;
2520 
2521     if (rn == 31) {
2522         gen_check_sp_alignment(s);
2523     }
2524     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, size);
2525     tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt, memidx,
2526                                size | MO_ALIGN | s->be_data);
2527 }
2528 
2529 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2530                                       int rn, int size)
2531 {
2532     TCGv_i64 s1 = cpu_reg(s, rs);
2533     TCGv_i64 s2 = cpu_reg(s, rs + 1);
2534     TCGv_i64 t1 = cpu_reg(s, rt);
2535     TCGv_i64 t2 = cpu_reg(s, rt + 1);
2536     TCGv_i64 clean_addr;
2537     int memidx = get_mem_index(s);
2538 
2539     if (rn == 31) {
2540         gen_check_sp_alignment(s);
2541     }
2542 
2543     /* This is a single atomic access, despite the "pair". */
2544     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, size + 1);
2545 
2546     if (size == 2) {
2547         TCGv_i64 cmp = tcg_temp_new_i64();
2548         TCGv_i64 val = tcg_temp_new_i64();
2549 
2550         if (s->be_data == MO_LE) {
2551             tcg_gen_concat32_i64(val, t1, t2);
2552             tcg_gen_concat32_i64(cmp, s1, s2);
2553         } else {
2554             tcg_gen_concat32_i64(val, t2, t1);
2555             tcg_gen_concat32_i64(cmp, s2, s1);
2556         }
2557 
2558         tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx,
2559                                    MO_64 | MO_ALIGN | s->be_data);
2560 
2561         if (s->be_data == MO_LE) {
2562             tcg_gen_extr32_i64(s1, s2, cmp);
2563         } else {
2564             tcg_gen_extr32_i64(s2, s1, cmp);
2565         }
2566     } else {
2567         TCGv_i128 cmp = tcg_temp_new_i128();
2568         TCGv_i128 val = tcg_temp_new_i128();
2569 
2570         if (s->be_data == MO_LE) {
2571             tcg_gen_concat_i64_i128(val, t1, t2);
2572             tcg_gen_concat_i64_i128(cmp, s1, s2);
2573         } else {
2574             tcg_gen_concat_i64_i128(val, t2, t1);
2575             tcg_gen_concat_i64_i128(cmp, s2, s1);
2576         }
2577 
2578         tcg_gen_atomic_cmpxchg_i128(cmp, clean_addr, cmp, val, memidx,
2579                                     MO_128 | MO_ALIGN | s->be_data);
2580 
2581         if (s->be_data == MO_LE) {
2582             tcg_gen_extr_i128_i64(s1, s2, cmp);
2583         } else {
2584             tcg_gen_extr_i128_i64(s2, s1, cmp);
2585         }
2586     }
2587 }
2588 
2589 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
2590  * from the ARMv8 specs for LDR (Shared decode for all encodings).
2591  */
2592 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2593 {
2594     int opc0 = extract32(opc, 0, 1);
2595     int regsize;
2596 
2597     if (is_signed) {
2598         regsize = opc0 ? 32 : 64;
2599     } else {
2600         regsize = size == 3 ? 64 : 32;
2601     }
2602     return regsize == 64;
2603 }
2604 
2605 /* Load/store exclusive
2606  *
2607  *  31 30 29         24  23  22   21  20  16  15  14   10 9    5 4    0
2608  * +-----+-------------+----+---+----+------+----+-------+------+------+
2609  * | sz  | 0 0 1 0 0 0 | o2 | L | o1 |  Rs  | o0 |  Rt2  |  Rn  | Rt   |
2610  * +-----+-------------+----+---+----+------+----+-------+------+------+
2611  *
2612  *  sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2613  *   L: 0 -> store, 1 -> load
2614  *  o2: 0 -> exclusive, 1 -> not
2615  *  o1: 0 -> single register, 1 -> register pair
2616  *  o0: 1 -> load-acquire/store-release, 0 -> not
2617  */
2618 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2619 {
2620     int rt = extract32(insn, 0, 5);
2621     int rn = extract32(insn, 5, 5);
2622     int rt2 = extract32(insn, 10, 5);
2623     int rs = extract32(insn, 16, 5);
2624     int is_lasr = extract32(insn, 15, 1);
2625     int o2_L_o1_o0 = extract32(insn, 21, 3) * 2 | is_lasr;
2626     int size = extract32(insn, 30, 2);
2627     TCGv_i64 clean_addr;
2628 
2629     switch (o2_L_o1_o0) {
2630     case 0x0: /* STXR */
2631     case 0x1: /* STLXR */
2632         if (rn == 31) {
2633             gen_check_sp_alignment(s);
2634         }
2635         if (is_lasr) {
2636             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2637         }
2638         gen_store_exclusive(s, rs, rt, rt2, rn, size, false);
2639         return;
2640 
2641     case 0x4: /* LDXR */
2642     case 0x5: /* LDAXR */
2643         if (rn == 31) {
2644             gen_check_sp_alignment(s);
2645         }
2646         gen_load_exclusive(s, rt, rt2, rn, size, false);
2647         if (is_lasr) {
2648             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2649         }
2650         return;
2651 
2652     case 0x8: /* STLLR */
2653         if (!dc_isar_feature(aa64_lor, s)) {
2654             break;
2655         }
2656         /* StoreLORelease is the same as Store-Release for QEMU.  */
2657         /* fall through */
2658     case 0x9: /* STLR */
2659         /* Generate ISS for non-exclusive accesses including LASR.  */
2660         if (rn == 31) {
2661             gen_check_sp_alignment(s);
2662         }
2663         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2664         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2665                                     true, rn != 31, size);
2666         /* TODO: ARMv8.4-LSE SCTLR.nAA */
2667         do_gpr_st(s, cpu_reg(s, rt), clean_addr, size | MO_ALIGN, true, rt,
2668                   disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2669         return;
2670 
2671     case 0xc: /* LDLAR */
2672         if (!dc_isar_feature(aa64_lor, s)) {
2673             break;
2674         }
2675         /* LoadLOAcquire is the same as Load-Acquire for QEMU.  */
2676         /* fall through */
2677     case 0xd: /* LDAR */
2678         /* Generate ISS for non-exclusive accesses including LASR.  */
2679         if (rn == 31) {
2680             gen_check_sp_alignment(s);
2681         }
2682         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2683                                     false, rn != 31, size);
2684         /* TODO: ARMv8.4-LSE SCTLR.nAA */
2685         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size | MO_ALIGN, false, true,
2686                   rt, disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2687         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2688         return;
2689 
2690     case 0x2: case 0x3: /* CASP / STXP */
2691         if (size & 2) { /* STXP / STLXP */
2692             if (rn == 31) {
2693                 gen_check_sp_alignment(s);
2694             }
2695             if (is_lasr) {
2696                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2697             }
2698             gen_store_exclusive(s, rs, rt, rt2, rn, size, true);
2699             return;
2700         }
2701         if (rt2 == 31
2702             && ((rt | rs) & 1) == 0
2703             && dc_isar_feature(aa64_atomics, s)) {
2704             /* CASP / CASPL */
2705             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2706             return;
2707         }
2708         break;
2709 
2710     case 0x6: case 0x7: /* CASPA / LDXP */
2711         if (size & 2) { /* LDXP / LDAXP */
2712             if (rn == 31) {
2713                 gen_check_sp_alignment(s);
2714             }
2715             gen_load_exclusive(s, rt, rt2, rn, size, true);
2716             if (is_lasr) {
2717                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2718             }
2719             return;
2720         }
2721         if (rt2 == 31
2722             && ((rt | rs) & 1) == 0
2723             && dc_isar_feature(aa64_atomics, s)) {
2724             /* CASPA / CASPAL */
2725             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2726             return;
2727         }
2728         break;
2729 
2730     case 0xa: /* CAS */
2731     case 0xb: /* CASL */
2732     case 0xe: /* CASA */
2733     case 0xf: /* CASAL */
2734         if (rt2 == 31 && dc_isar_feature(aa64_atomics, s)) {
2735             gen_compare_and_swap(s, rs, rt, rn, size);
2736             return;
2737         }
2738         break;
2739     }
2740     unallocated_encoding(s);
2741 }
2742 
2743 /*
2744  * Load register (literal)
2745  *
2746  *  31 30 29   27  26 25 24 23                5 4     0
2747  * +-----+-------+---+-----+-------------------+-------+
2748  * | opc | 0 1 1 | V | 0 0 |     imm19         |  Rt   |
2749  * +-----+-------+---+-----+-------------------+-------+
2750  *
2751  * V: 1 -> vector (simd/fp)
2752  * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2753  *                   10-> 32 bit signed, 11 -> prefetch
2754  * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2755  */
2756 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2757 {
2758     int rt = extract32(insn, 0, 5);
2759     int64_t imm = sextract32(insn, 5, 19) << 2;
2760     bool is_vector = extract32(insn, 26, 1);
2761     int opc = extract32(insn, 30, 2);
2762     bool is_signed = false;
2763     int size = 2;
2764     TCGv_i64 tcg_rt, clean_addr;
2765 
2766     if (is_vector) {
2767         if (opc == 3) {
2768             unallocated_encoding(s);
2769             return;
2770         }
2771         size = 2 + opc;
2772         if (!fp_access_check(s)) {
2773             return;
2774         }
2775     } else {
2776         if (opc == 3) {
2777             /* PRFM (literal) : prefetch */
2778             return;
2779         }
2780         size = 2 + extract32(opc, 0, 1);
2781         is_signed = extract32(opc, 1, 1);
2782     }
2783 
2784     tcg_rt = cpu_reg(s, rt);
2785 
2786     clean_addr = tcg_temp_new_i64();
2787     gen_pc_plus_diff(s, clean_addr, imm);
2788     if (is_vector) {
2789         do_fp_ld(s, rt, clean_addr, size);
2790     } else {
2791         /* Only unsigned 32bit loads target 32bit registers.  */
2792         bool iss_sf = opc != 0;
2793 
2794         do_gpr_ld(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
2795                   false, true, rt, iss_sf, false);
2796     }
2797 }
2798 
2799 /*
2800  * LDNP (Load Pair - non-temporal hint)
2801  * LDP (Load Pair - non vector)
2802  * LDPSW (Load Pair Signed Word - non vector)
2803  * STNP (Store Pair - non-temporal hint)
2804  * STP (Store Pair - non vector)
2805  * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2806  * LDP (Load Pair of SIMD&FP)
2807  * STNP (Store Pair of SIMD&FP - non-temporal hint)
2808  * STP (Store Pair of SIMD&FP)
2809  *
2810  *  31 30 29   27  26  25 24   23  22 21   15 14   10 9    5 4    0
2811  * +-----+-------+---+---+-------+---+-----------------------------+
2812  * | opc | 1 0 1 | V | 0 | index | L |  imm7 |  Rt2  |  Rn  | Rt   |
2813  * +-----+-------+---+---+-------+---+-------+-------+------+------+
2814  *
2815  * opc: LDP/STP/LDNP/STNP        00 -> 32 bit, 10 -> 64 bit
2816  *      LDPSW/STGP               01
2817  *      LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2818  *   V: 0 -> GPR, 1 -> Vector
2819  * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2820  *      10 -> signed offset, 11 -> pre-index
2821  *   L: 0 -> Store 1 -> Load
2822  *
2823  * Rt, Rt2 = GPR or SIMD registers to be stored
2824  * Rn = general purpose register containing address
2825  * imm7 = signed offset (multiple of 4 or 8 depending on size)
2826  */
2827 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2828 {
2829     int rt = extract32(insn, 0, 5);
2830     int rn = extract32(insn, 5, 5);
2831     int rt2 = extract32(insn, 10, 5);
2832     uint64_t offset = sextract64(insn, 15, 7);
2833     int index = extract32(insn, 23, 2);
2834     bool is_vector = extract32(insn, 26, 1);
2835     bool is_load = extract32(insn, 22, 1);
2836     int opc = extract32(insn, 30, 2);
2837 
2838     bool is_signed = false;
2839     bool postindex = false;
2840     bool wback = false;
2841     bool set_tag = false;
2842 
2843     TCGv_i64 clean_addr, dirty_addr;
2844 
2845     int size;
2846 
2847     if (opc == 3) {
2848         unallocated_encoding(s);
2849         return;
2850     }
2851 
2852     if (is_vector) {
2853         size = 2 + opc;
2854     } else if (opc == 1 && !is_load) {
2855         /* STGP */
2856         if (!dc_isar_feature(aa64_mte_insn_reg, s) || index == 0) {
2857             unallocated_encoding(s);
2858             return;
2859         }
2860         size = 3;
2861         set_tag = true;
2862     } else {
2863         size = 2 + extract32(opc, 1, 1);
2864         is_signed = extract32(opc, 0, 1);
2865         if (!is_load && is_signed) {
2866             unallocated_encoding(s);
2867             return;
2868         }
2869     }
2870 
2871     switch (index) {
2872     case 1: /* post-index */
2873         postindex = true;
2874         wback = true;
2875         break;
2876     case 0:
2877         /* signed offset with "non-temporal" hint. Since we don't emulate
2878          * caches we don't care about hints to the cache system about
2879          * data access patterns, and handle this identically to plain
2880          * signed offset.
2881          */
2882         if (is_signed) {
2883             /* There is no non-temporal-hint version of LDPSW */
2884             unallocated_encoding(s);
2885             return;
2886         }
2887         postindex = false;
2888         break;
2889     case 2: /* signed offset, rn not updated */
2890         postindex = false;
2891         break;
2892     case 3: /* pre-index */
2893         postindex = false;
2894         wback = true;
2895         break;
2896     }
2897 
2898     if (is_vector && !fp_access_check(s)) {
2899         return;
2900     }
2901 
2902     offset <<= (set_tag ? LOG2_TAG_GRANULE : size);
2903 
2904     if (rn == 31) {
2905         gen_check_sp_alignment(s);
2906     }
2907 
2908     dirty_addr = read_cpu_reg_sp(s, rn, 1);
2909     if (!postindex) {
2910         tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2911     }
2912 
2913     if (set_tag) {
2914         if (!s->ata) {
2915             /*
2916              * TODO: We could rely on the stores below, at least for
2917              * system mode, if we arrange to add MO_ALIGN_16.
2918              */
2919             gen_helper_stg_stub(cpu_env, dirty_addr);
2920         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2921             gen_helper_stg_parallel(cpu_env, dirty_addr, dirty_addr);
2922         } else {
2923             gen_helper_stg(cpu_env, dirty_addr, dirty_addr);
2924         }
2925     }
2926 
2927     clean_addr = gen_mte_checkN(s, dirty_addr, !is_load,
2928                                 (wback || rn != 31) && !set_tag, 2 << size);
2929 
2930     if (is_vector) {
2931         if (is_load) {
2932             do_fp_ld(s, rt, clean_addr, size);
2933         } else {
2934             do_fp_st(s, rt, clean_addr, size);
2935         }
2936         tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2937         if (is_load) {
2938             do_fp_ld(s, rt2, clean_addr, size);
2939         } else {
2940             do_fp_st(s, rt2, clean_addr, size);
2941         }
2942     } else {
2943         TCGv_i64 tcg_rt = cpu_reg(s, rt);
2944         TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
2945         MemOp mop = size + 1;
2946 
2947         /*
2948          * With LSE2, non-sign-extending pairs are treated atomically if
2949          * aligned, and if unaligned one of the pair will be completely
2950          * within a 16-byte block and that element will be atomic.
2951          * Otherwise each element is separately atomic.
2952          * In all cases, issue one operation with the correct atomicity.
2953          *
2954          * This treats sign-extending loads like zero-extending loads,
2955          * since that reuses the most code below.
2956          */
2957         if (s->align_mem) {
2958             mop |= (size == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
2959         }
2960         mop = finalize_memop_pair(s, mop);
2961 
2962         if (is_load) {
2963             if (size == 2) {
2964                 int o2 = s->be_data == MO_LE ? 32 : 0;
2965                 int o1 = o2 ^ 32;
2966 
2967                 tcg_gen_qemu_ld_i64(tcg_rt, clean_addr, get_mem_index(s), mop);
2968                 if (is_signed) {
2969                     tcg_gen_sextract_i64(tcg_rt2, tcg_rt, o2, 32);
2970                     tcg_gen_sextract_i64(tcg_rt, tcg_rt, o1, 32);
2971                 } else {
2972                     tcg_gen_extract_i64(tcg_rt2, tcg_rt, o2, 32);
2973                     tcg_gen_extract_i64(tcg_rt, tcg_rt, o1, 32);
2974                 }
2975             } else {
2976                 TCGv_i128 tmp = tcg_temp_new_i128();
2977 
2978                 tcg_gen_qemu_ld_i128(tmp, clean_addr, get_mem_index(s), mop);
2979                 if (s->be_data == MO_LE) {
2980                     tcg_gen_extr_i128_i64(tcg_rt, tcg_rt2, tmp);
2981                 } else {
2982                     tcg_gen_extr_i128_i64(tcg_rt2, tcg_rt, tmp);
2983                 }
2984             }
2985         } else {
2986             if (size == 2) {
2987                 TCGv_i64 tmp = tcg_temp_new_i64();
2988 
2989                 if (s->be_data == MO_LE) {
2990                     tcg_gen_concat32_i64(tmp, tcg_rt, tcg_rt2);
2991                 } else {
2992                     tcg_gen_concat32_i64(tmp, tcg_rt2, tcg_rt);
2993                 }
2994                 tcg_gen_qemu_st_i64(tmp, clean_addr, get_mem_index(s), mop);
2995             } else {
2996                 TCGv_i128 tmp = tcg_temp_new_i128();
2997 
2998                 if (s->be_data == MO_LE) {
2999                     tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
3000                 } else {
3001                     tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
3002                 }
3003                 tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
3004             }
3005         }
3006     }
3007 
3008     if (wback) {
3009         if (postindex) {
3010             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3011         }
3012         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3013     }
3014 }
3015 
3016 /*
3017  * Load/store (immediate post-indexed)
3018  * Load/store (immediate pre-indexed)
3019  * Load/store (unscaled immediate)
3020  *
3021  * 31 30 29   27  26 25 24 23 22 21  20    12 11 10 9    5 4    0
3022  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3023  * |size| 1 1 1 | V | 0 0 | opc | 0 |  imm9  | idx |  Rn  |  Rt  |
3024  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3025  *
3026  * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
3027          10 -> unprivileged
3028  * V = 0 -> non-vector
3029  * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
3030  * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3031  */
3032 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
3033                                 int opc,
3034                                 int size,
3035                                 int rt,
3036                                 bool is_vector)
3037 {
3038     int rn = extract32(insn, 5, 5);
3039     int imm9 = sextract32(insn, 12, 9);
3040     int idx = extract32(insn, 10, 2);
3041     bool is_signed = false;
3042     bool is_store = false;
3043     bool is_extended = false;
3044     bool is_unpriv = (idx == 2);
3045     bool iss_valid;
3046     bool post_index;
3047     bool writeback;
3048     int memidx;
3049 
3050     TCGv_i64 clean_addr, dirty_addr;
3051 
3052     if (is_vector) {
3053         size |= (opc & 2) << 1;
3054         if (size > 4 || is_unpriv) {
3055             unallocated_encoding(s);
3056             return;
3057         }
3058         is_store = ((opc & 1) == 0);
3059         if (!fp_access_check(s)) {
3060             return;
3061         }
3062     } else {
3063         if (size == 3 && opc == 2) {
3064             /* PRFM - prefetch */
3065             if (idx != 0) {
3066                 unallocated_encoding(s);
3067                 return;
3068             }
3069             return;
3070         }
3071         if (opc == 3 && size > 1) {
3072             unallocated_encoding(s);
3073             return;
3074         }
3075         is_store = (opc == 0);
3076         is_signed = extract32(opc, 1, 1);
3077         is_extended = (size < 3) && extract32(opc, 0, 1);
3078     }
3079 
3080     switch (idx) {
3081     case 0:
3082     case 2:
3083         post_index = false;
3084         writeback = false;
3085         break;
3086     case 1:
3087         post_index = true;
3088         writeback = true;
3089         break;
3090     case 3:
3091         post_index = false;
3092         writeback = true;
3093         break;
3094     default:
3095         g_assert_not_reached();
3096     }
3097 
3098     iss_valid = !is_vector && !writeback;
3099 
3100     if (rn == 31) {
3101         gen_check_sp_alignment(s);
3102     }
3103 
3104     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3105     if (!post_index) {
3106         tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3107     }
3108 
3109     memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
3110     clean_addr = gen_mte_check1_mmuidx(s, dirty_addr, is_store,
3111                                        writeback || rn != 31,
3112                                        size, is_unpriv, memidx);
3113 
3114     if (is_vector) {
3115         if (is_store) {
3116             do_fp_st(s, rt, clean_addr, size);
3117         } else {
3118             do_fp_ld(s, rt, clean_addr, size);
3119         }
3120     } else {
3121         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3122         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3123 
3124         if (is_store) {
3125             do_gpr_st_memidx(s, tcg_rt, clean_addr, size, memidx,
3126                              iss_valid, rt, iss_sf, false);
3127         } else {
3128             do_gpr_ld_memidx(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
3129                              is_extended, memidx,
3130                              iss_valid, rt, iss_sf, false);
3131         }
3132     }
3133 
3134     if (writeback) {
3135         TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3136         if (post_index) {
3137             tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3138         }
3139         tcg_gen_mov_i64(tcg_rn, dirty_addr);
3140     }
3141 }
3142 
3143 /*
3144  * Load/store (register offset)
3145  *
3146  * 31 30 29   27  26 25 24 23 22 21  20  16 15 13 12 11 10 9  5 4  0
3147  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3148  * |size| 1 1 1 | V | 0 0 | opc | 1 |  Rm  | opt | S| 1 0 | Rn | Rt |
3149  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3150  *
3151  * For non-vector:
3152  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3153  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3154  * For vector:
3155  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3156  *   opc<0>: 0 -> store, 1 -> load
3157  * V: 1 -> vector/simd
3158  * opt: extend encoding (see DecodeRegExtend)
3159  * S: if S=1 then scale (essentially index by sizeof(size))
3160  * Rt: register to transfer into/out of
3161  * Rn: address register or SP for base
3162  * Rm: offset register or ZR for offset
3163  */
3164 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
3165                                    int opc,
3166                                    int size,
3167                                    int rt,
3168                                    bool is_vector)
3169 {
3170     int rn = extract32(insn, 5, 5);
3171     int shift = extract32(insn, 12, 1);
3172     int rm = extract32(insn, 16, 5);
3173     int opt = extract32(insn, 13, 3);
3174     bool is_signed = false;
3175     bool is_store = false;
3176     bool is_extended = false;
3177 
3178     TCGv_i64 tcg_rm, clean_addr, dirty_addr;
3179 
3180     if (extract32(opt, 1, 1) == 0) {
3181         unallocated_encoding(s);
3182         return;
3183     }
3184 
3185     if (is_vector) {
3186         size |= (opc & 2) << 1;
3187         if (size > 4) {
3188             unallocated_encoding(s);
3189             return;
3190         }
3191         is_store = !extract32(opc, 0, 1);
3192         if (!fp_access_check(s)) {
3193             return;
3194         }
3195     } else {
3196         if (size == 3 && opc == 2) {
3197             /* PRFM - prefetch */
3198             return;
3199         }
3200         if (opc == 3 && size > 1) {
3201             unallocated_encoding(s);
3202             return;
3203         }
3204         is_store = (opc == 0);
3205         is_signed = extract32(opc, 1, 1);
3206         is_extended = (size < 3) && extract32(opc, 0, 1);
3207     }
3208 
3209     if (rn == 31) {
3210         gen_check_sp_alignment(s);
3211     }
3212     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3213 
3214     tcg_rm = read_cpu_reg(s, rm, 1);
3215     ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
3216 
3217     tcg_gen_add_i64(dirty_addr, dirty_addr, tcg_rm);
3218     clean_addr = gen_mte_check1(s, dirty_addr, is_store, true, size);
3219 
3220     if (is_vector) {
3221         if (is_store) {
3222             do_fp_st(s, rt, clean_addr, size);
3223         } else {
3224             do_fp_ld(s, rt, clean_addr, size);
3225         }
3226     } else {
3227         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3228         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3229         if (is_store) {
3230             do_gpr_st(s, tcg_rt, clean_addr, size,
3231                       true, rt, iss_sf, false);
3232         } else {
3233             do_gpr_ld(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
3234                       is_extended, true, rt, iss_sf, false);
3235         }
3236     }
3237 }
3238 
3239 /*
3240  * Load/store (unsigned immediate)
3241  *
3242  * 31 30 29   27  26 25 24 23 22 21        10 9     5
3243  * +----+-------+---+-----+-----+------------+-------+------+
3244  * |size| 1 1 1 | V | 0 1 | opc |   imm12    |  Rn   |  Rt  |
3245  * +----+-------+---+-----+-----+------------+-------+------+
3246  *
3247  * For non-vector:
3248  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3249  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3250  * For vector:
3251  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3252  *   opc<0>: 0 -> store, 1 -> load
3253  * Rn: base address register (inc SP)
3254  * Rt: target register
3255  */
3256 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
3257                                         int opc,
3258                                         int size,
3259                                         int rt,
3260                                         bool is_vector)
3261 {
3262     int rn = extract32(insn, 5, 5);
3263     unsigned int imm12 = extract32(insn, 10, 12);
3264     unsigned int offset;
3265 
3266     TCGv_i64 clean_addr, dirty_addr;
3267 
3268     bool is_store;
3269     bool is_signed = false;
3270     bool is_extended = false;
3271 
3272     if (is_vector) {
3273         size |= (opc & 2) << 1;
3274         if (size > 4) {
3275             unallocated_encoding(s);
3276             return;
3277         }
3278         is_store = !extract32(opc, 0, 1);
3279         if (!fp_access_check(s)) {
3280             return;
3281         }
3282     } else {
3283         if (size == 3 && opc == 2) {
3284             /* PRFM - prefetch */
3285             return;
3286         }
3287         if (opc == 3 && size > 1) {
3288             unallocated_encoding(s);
3289             return;
3290         }
3291         is_store = (opc == 0);
3292         is_signed = extract32(opc, 1, 1);
3293         is_extended = (size < 3) && extract32(opc, 0, 1);
3294     }
3295 
3296     if (rn == 31) {
3297         gen_check_sp_alignment(s);
3298     }
3299     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3300     offset = imm12 << size;
3301     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3302     clean_addr = gen_mte_check1(s, dirty_addr, is_store, rn != 31, size);
3303 
3304     if (is_vector) {
3305         if (is_store) {
3306             do_fp_st(s, rt, clean_addr, size);
3307         } else {
3308             do_fp_ld(s, rt, clean_addr, size);
3309         }
3310     } else {
3311         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3312         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3313         if (is_store) {
3314             do_gpr_st(s, tcg_rt, clean_addr, size,
3315                       true, rt, iss_sf, false);
3316         } else {
3317             do_gpr_ld(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
3318                       is_extended, true, rt, iss_sf, false);
3319         }
3320     }
3321 }
3322 
3323 /* Atomic memory operations
3324  *
3325  *  31  30      27  26    24    22  21   16   15    12    10    5     0
3326  * +------+-------+---+-----+-----+---+----+----+-----+-----+----+-----+
3327  * | size | 1 1 1 | V | 0 0 | A R | 1 | Rs | o3 | opc | 0 0 | Rn |  Rt |
3328  * +------+-------+---+-----+-----+--------+----+-----+-----+----+-----+
3329  *
3330  * Rt: the result register
3331  * Rn: base address or SP
3332  * Rs: the source register for the operation
3333  * V: vector flag (always 0 as of v8.3)
3334  * A: acquire flag
3335  * R: release flag
3336  */
3337 static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
3338                               int size, int rt, bool is_vector)
3339 {
3340     int rs = extract32(insn, 16, 5);
3341     int rn = extract32(insn, 5, 5);
3342     int o3_opc = extract32(insn, 12, 4);
3343     bool r = extract32(insn, 22, 1);
3344     bool a = extract32(insn, 23, 1);
3345     TCGv_i64 tcg_rs, tcg_rt, clean_addr;
3346     AtomicThreeOpFn *fn = NULL;
3347     MemOp mop = s->be_data | size | MO_ALIGN;
3348 
3349     if (is_vector || !dc_isar_feature(aa64_atomics, s)) {
3350         unallocated_encoding(s);
3351         return;
3352     }
3353     switch (o3_opc) {
3354     case 000: /* LDADD */
3355         fn = tcg_gen_atomic_fetch_add_i64;
3356         break;
3357     case 001: /* LDCLR */
3358         fn = tcg_gen_atomic_fetch_and_i64;
3359         break;
3360     case 002: /* LDEOR */
3361         fn = tcg_gen_atomic_fetch_xor_i64;
3362         break;
3363     case 003: /* LDSET */
3364         fn = tcg_gen_atomic_fetch_or_i64;
3365         break;
3366     case 004: /* LDSMAX */
3367         fn = tcg_gen_atomic_fetch_smax_i64;
3368         mop |= MO_SIGN;
3369         break;
3370     case 005: /* LDSMIN */
3371         fn = tcg_gen_atomic_fetch_smin_i64;
3372         mop |= MO_SIGN;
3373         break;
3374     case 006: /* LDUMAX */
3375         fn = tcg_gen_atomic_fetch_umax_i64;
3376         break;
3377     case 007: /* LDUMIN */
3378         fn = tcg_gen_atomic_fetch_umin_i64;
3379         break;
3380     case 010: /* SWP */
3381         fn = tcg_gen_atomic_xchg_i64;
3382         break;
3383     case 014: /* LDAPR, LDAPRH, LDAPRB */
3384         if (!dc_isar_feature(aa64_rcpc_8_3, s) ||
3385             rs != 31 || a != 1 || r != 0) {
3386             unallocated_encoding(s);
3387             return;
3388         }
3389         break;
3390     default:
3391         unallocated_encoding(s);
3392         return;
3393     }
3394 
3395     if (rn == 31) {
3396         gen_check_sp_alignment(s);
3397     }
3398     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), false, rn != 31, size);
3399 
3400     if (o3_opc == 014) {
3401         /*
3402          * LDAPR* are a special case because they are a simple load, not a
3403          * fetch-and-do-something op.
3404          * The architectural consistency requirements here are weaker than
3405          * full load-acquire (we only need "load-acquire processor consistent"),
3406          * but we choose to implement them as full LDAQ.
3407          */
3408         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size, false,
3409                   true, rt, disas_ldst_compute_iss_sf(size, false, 0), true);
3410         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3411         return;
3412     }
3413 
3414     tcg_rs = read_cpu_reg(s, rs, true);
3415     tcg_rt = cpu_reg(s, rt);
3416 
3417     if (o3_opc == 1) { /* LDCLR */
3418         tcg_gen_not_i64(tcg_rs, tcg_rs);
3419     }
3420 
3421     /* The tcg atomic primitives are all full barriers.  Therefore we
3422      * can ignore the Acquire and Release bits of this instruction.
3423      */
3424     fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3425 
3426     if ((mop & MO_SIGN) && size != MO_64) {
3427         tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3428     }
3429 }
3430 
3431 /*
3432  * PAC memory operations
3433  *
3434  *  31  30      27  26    24    22  21       12  11  10    5     0
3435  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3436  * | size | 1 1 1 | V | 0 0 | M S | 1 |  imm9  | W | 1 | Rn |  Rt |
3437  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3438  *
3439  * Rt: the result register
3440  * Rn: base address or SP
3441  * V: vector flag (always 0 as of v8.3)
3442  * M: clear for key DA, set for key DB
3443  * W: pre-indexing flag
3444  * S: sign for imm9.
3445  */
3446 static void disas_ldst_pac(DisasContext *s, uint32_t insn,
3447                            int size, int rt, bool is_vector)
3448 {
3449     int rn = extract32(insn, 5, 5);
3450     bool is_wback = extract32(insn, 11, 1);
3451     bool use_key_a = !extract32(insn, 23, 1);
3452     int offset;
3453     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3454 
3455     if (size != 3 || is_vector || !dc_isar_feature(aa64_pauth, s)) {
3456         unallocated_encoding(s);
3457         return;
3458     }
3459 
3460     if (rn == 31) {
3461         gen_check_sp_alignment(s);
3462     }
3463     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3464 
3465     if (s->pauth_active) {
3466         if (use_key_a) {
3467             gen_helper_autda(dirty_addr, cpu_env, dirty_addr,
3468                              tcg_constant_i64(0));
3469         } else {
3470             gen_helper_autdb(dirty_addr, cpu_env, dirty_addr,
3471                              tcg_constant_i64(0));
3472         }
3473     }
3474 
3475     /* Form the 10-bit signed, scaled offset.  */
3476     offset = (extract32(insn, 22, 1) << 9) | extract32(insn, 12, 9);
3477     offset = sextract32(offset << size, 0, 10 + size);
3478     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3479 
3480     /* Note that "clean" and "dirty" here refer to TBI not PAC.  */
3481     clean_addr = gen_mte_check1(s, dirty_addr, false,
3482                                 is_wback || rn != 31, size);
3483 
3484     tcg_rt = cpu_reg(s, rt);
3485     do_gpr_ld(s, tcg_rt, clean_addr, size,
3486               /* extend */ false, /* iss_valid */ !is_wback,
3487               /* iss_srt */ rt, /* iss_sf */ true, /* iss_ar */ false);
3488 
3489     if (is_wback) {
3490         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3491     }
3492 }
3493 
3494 /*
3495  * LDAPR/STLR (unscaled immediate)
3496  *
3497  *  31  30            24    22  21       12    10    5     0
3498  * +------+-------------+-----+---+--------+-----+----+-----+
3499  * | size | 0 1 1 0 0 1 | opc | 0 |  imm9  | 0 0 | Rn |  Rt |
3500  * +------+-------------+-----+---+--------+-----+----+-----+
3501  *
3502  * Rt: source or destination register
3503  * Rn: base register
3504  * imm9: unscaled immediate offset
3505  * opc: 00: STLUR*, 01/10/11: various LDAPUR*
3506  * size: size of load/store
3507  */
3508 static void disas_ldst_ldapr_stlr(DisasContext *s, uint32_t insn)
3509 {
3510     int rt = extract32(insn, 0, 5);
3511     int rn = extract32(insn, 5, 5);
3512     int offset = sextract32(insn, 12, 9);
3513     int opc = extract32(insn, 22, 2);
3514     int size = extract32(insn, 30, 2);
3515     TCGv_i64 clean_addr, dirty_addr;
3516     bool is_store = false;
3517     bool extend = false;
3518     bool iss_sf;
3519     MemOp mop;
3520 
3521     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3522         unallocated_encoding(s);
3523         return;
3524     }
3525 
3526     /* TODO: ARMv8.4-LSE SCTLR.nAA */
3527     mop = size | MO_ALIGN;
3528 
3529     switch (opc) {
3530     case 0: /* STLURB */
3531         is_store = true;
3532         break;
3533     case 1: /* LDAPUR* */
3534         break;
3535     case 2: /* LDAPURS* 64-bit variant */
3536         if (size == 3) {
3537             unallocated_encoding(s);
3538             return;
3539         }
3540         mop |= MO_SIGN;
3541         break;
3542     case 3: /* LDAPURS* 32-bit variant */
3543         if (size > 1) {
3544             unallocated_encoding(s);
3545             return;
3546         }
3547         mop |= MO_SIGN;
3548         extend = true; /* zero-extend 32->64 after signed load */
3549         break;
3550     default:
3551         g_assert_not_reached();
3552     }
3553 
3554     iss_sf = disas_ldst_compute_iss_sf(size, (mop & MO_SIGN) != 0, opc);
3555 
3556     if (rn == 31) {
3557         gen_check_sp_alignment(s);
3558     }
3559 
3560     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3561     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3562     clean_addr = clean_data_tbi(s, dirty_addr);
3563 
3564     if (is_store) {
3565         /* Store-Release semantics */
3566         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3567         do_gpr_st(s, cpu_reg(s, rt), clean_addr, mop, true, rt, iss_sf, true);
3568     } else {
3569         /*
3570          * Load-AcquirePC semantics; we implement as the slightly more
3571          * restrictive Load-Acquire.
3572          */
3573         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop,
3574                   extend, true, rt, iss_sf, true);
3575         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3576     }
3577 }
3578 
3579 /* Load/store register (all forms) */
3580 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
3581 {
3582     int rt = extract32(insn, 0, 5);
3583     int opc = extract32(insn, 22, 2);
3584     bool is_vector = extract32(insn, 26, 1);
3585     int size = extract32(insn, 30, 2);
3586 
3587     switch (extract32(insn, 24, 2)) {
3588     case 0:
3589         if (extract32(insn, 21, 1) == 0) {
3590             /* Load/store register (unscaled immediate)
3591              * Load/store immediate pre/post-indexed
3592              * Load/store register unprivileged
3593              */
3594             disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
3595             return;
3596         }
3597         switch (extract32(insn, 10, 2)) {
3598         case 0:
3599             disas_ldst_atomic(s, insn, size, rt, is_vector);
3600             return;
3601         case 2:
3602             disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
3603             return;
3604         default:
3605             disas_ldst_pac(s, insn, size, rt, is_vector);
3606             return;
3607         }
3608         break;
3609     case 1:
3610         disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
3611         return;
3612     }
3613     unallocated_encoding(s);
3614 }
3615 
3616 /* AdvSIMD load/store multiple structures
3617  *
3618  *  31  30  29           23 22  21         16 15    12 11  10 9    5 4    0
3619  * +---+---+---------------+---+-------------+--------+------+------+------+
3620  * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size |  Rn  |  Rt  |
3621  * +---+---+---------------+---+-------------+--------+------+------+------+
3622  *
3623  * AdvSIMD load/store multiple structures (post-indexed)
3624  *
3625  *  31  30  29           23 22  21  20     16 15    12 11  10 9    5 4    0
3626  * +---+---+---------------+---+---+---------+--------+------+------+------+
3627  * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 |   Rm    | opcode | size |  Rn  |  Rt  |
3628  * +---+---+---------------+---+---+---------+--------+------+------+------+
3629  *
3630  * Rt: first (or only) SIMD&FP register to be transferred
3631  * Rn: base address or SP
3632  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3633  */
3634 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
3635 {
3636     int rt = extract32(insn, 0, 5);
3637     int rn = extract32(insn, 5, 5);
3638     int rm = extract32(insn, 16, 5);
3639     int size = extract32(insn, 10, 2);
3640     int opcode = extract32(insn, 12, 4);
3641     bool is_store = !extract32(insn, 22, 1);
3642     bool is_postidx = extract32(insn, 23, 1);
3643     bool is_q = extract32(insn, 30, 1);
3644     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3645     MemOp endian, align, mop;
3646 
3647     int total;    /* total bytes */
3648     int elements; /* elements per vector */
3649     int rpt;    /* num iterations */
3650     int selem;  /* structure elements */
3651     int r;
3652 
3653     if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
3654         unallocated_encoding(s);
3655         return;
3656     }
3657 
3658     if (!is_postidx && rm != 0) {
3659         unallocated_encoding(s);
3660         return;
3661     }
3662 
3663     /* From the shared decode logic */
3664     switch (opcode) {
3665     case 0x0:
3666         rpt = 1;
3667         selem = 4;
3668         break;
3669     case 0x2:
3670         rpt = 4;
3671         selem = 1;
3672         break;
3673     case 0x4:
3674         rpt = 1;
3675         selem = 3;
3676         break;
3677     case 0x6:
3678         rpt = 3;
3679         selem = 1;
3680         break;
3681     case 0x7:
3682         rpt = 1;
3683         selem = 1;
3684         break;
3685     case 0x8:
3686         rpt = 1;
3687         selem = 2;
3688         break;
3689     case 0xa:
3690         rpt = 2;
3691         selem = 1;
3692         break;
3693     default:
3694         unallocated_encoding(s);
3695         return;
3696     }
3697 
3698     if (size == 3 && !is_q && selem != 1) {
3699         /* reserved */
3700         unallocated_encoding(s);
3701         return;
3702     }
3703 
3704     if (!fp_access_check(s)) {
3705         return;
3706     }
3707 
3708     if (rn == 31) {
3709         gen_check_sp_alignment(s);
3710     }
3711 
3712     /* For our purposes, bytes are always little-endian.  */
3713     endian = s->be_data;
3714     if (size == 0) {
3715         endian = MO_LE;
3716     }
3717 
3718     total = rpt * selem * (is_q ? 16 : 8);
3719     tcg_rn = cpu_reg_sp(s, rn);
3720 
3721     /*
3722      * Issue the MTE check vs the logical repeat count, before we
3723      * promote consecutive little-endian elements below.
3724      */
3725     clean_addr = gen_mte_checkN(s, tcg_rn, is_store, is_postidx || rn != 31,
3726                                 total);
3727 
3728     /*
3729      * Consecutive little-endian elements from a single register
3730      * can be promoted to a larger little-endian operation.
3731      */
3732     align = MO_ALIGN;
3733     if (selem == 1 && endian == MO_LE) {
3734         align = pow2_align(size);
3735         size = 3;
3736     }
3737     if (!s->align_mem) {
3738         align = 0;
3739     }
3740     mop = endian | size | align;
3741 
3742     elements = (is_q ? 16 : 8) >> size;
3743     tcg_ebytes = tcg_constant_i64(1 << size);
3744     for (r = 0; r < rpt; r++) {
3745         int e;
3746         for (e = 0; e < elements; e++) {
3747             int xs;
3748             for (xs = 0; xs < selem; xs++) {
3749                 int tt = (rt + r + xs) % 32;
3750                 if (is_store) {
3751                     do_vec_st(s, tt, e, clean_addr, mop);
3752                 } else {
3753                     do_vec_ld(s, tt, e, clean_addr, mop);
3754                 }
3755                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3756             }
3757         }
3758     }
3759 
3760     if (!is_store) {
3761         /* For non-quad operations, setting a slice of the low
3762          * 64 bits of the register clears the high 64 bits (in
3763          * the ARM ARM pseudocode this is implicit in the fact
3764          * that 'rval' is a 64 bit wide variable).
3765          * For quad operations, we might still need to zero the
3766          * high bits of SVE.
3767          */
3768         for (r = 0; r < rpt * selem; r++) {
3769             int tt = (rt + r) % 32;
3770             clear_vec_high(s, is_q, tt);
3771         }
3772     }
3773 
3774     if (is_postidx) {
3775         if (rm == 31) {
3776             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3777         } else {
3778             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3779         }
3780     }
3781 }
3782 
3783 /* AdvSIMD load/store single structure
3784  *
3785  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3786  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3787  * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size |  Rn  |  Rt  |
3788  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3789  *
3790  * AdvSIMD load/store single structure (post-indexed)
3791  *
3792  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3793  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3794  * | 0 | Q | 0 0 1 1 0 1 1 | L R |     Rm    | opc | S | size |  Rn  |  Rt  |
3795  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3796  *
3797  * Rt: first (or only) SIMD&FP register to be transferred
3798  * Rn: base address or SP
3799  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3800  * index = encoded in Q:S:size dependent on size
3801  *
3802  * lane_size = encoded in R, opc
3803  * transfer width = encoded in opc, S, size
3804  */
3805 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
3806 {
3807     int rt = extract32(insn, 0, 5);
3808     int rn = extract32(insn, 5, 5);
3809     int rm = extract32(insn, 16, 5);
3810     int size = extract32(insn, 10, 2);
3811     int S = extract32(insn, 12, 1);
3812     int opc = extract32(insn, 13, 3);
3813     int R = extract32(insn, 21, 1);
3814     int is_load = extract32(insn, 22, 1);
3815     int is_postidx = extract32(insn, 23, 1);
3816     int is_q = extract32(insn, 30, 1);
3817 
3818     int scale = extract32(opc, 1, 2);
3819     int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
3820     bool replicate = false;
3821     int index = is_q << 3 | S << 2 | size;
3822     int xs, total;
3823     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3824     MemOp mop;
3825 
3826     if (extract32(insn, 31, 1)) {
3827         unallocated_encoding(s);
3828         return;
3829     }
3830     if (!is_postidx && rm != 0) {
3831         unallocated_encoding(s);
3832         return;
3833     }
3834 
3835     switch (scale) {
3836     case 3:
3837         if (!is_load || S) {
3838             unallocated_encoding(s);
3839             return;
3840         }
3841         scale = size;
3842         replicate = true;
3843         break;
3844     case 0:
3845         break;
3846     case 1:
3847         if (extract32(size, 0, 1)) {
3848             unallocated_encoding(s);
3849             return;
3850         }
3851         index >>= 1;
3852         break;
3853     case 2:
3854         if (extract32(size, 1, 1)) {
3855             unallocated_encoding(s);
3856             return;
3857         }
3858         if (!extract32(size, 0, 1)) {
3859             index >>= 2;
3860         } else {
3861             if (S) {
3862                 unallocated_encoding(s);
3863                 return;
3864             }
3865             index >>= 3;
3866             scale = 3;
3867         }
3868         break;
3869     default:
3870         g_assert_not_reached();
3871     }
3872 
3873     if (!fp_access_check(s)) {
3874         return;
3875     }
3876 
3877     if (rn == 31) {
3878         gen_check_sp_alignment(s);
3879     }
3880 
3881     total = selem << scale;
3882     tcg_rn = cpu_reg_sp(s, rn);
3883 
3884     clean_addr = gen_mte_checkN(s, tcg_rn, !is_load, is_postidx || rn != 31,
3885                                 total);
3886     mop = finalize_memop(s, scale);
3887 
3888     tcg_ebytes = tcg_constant_i64(1 << scale);
3889     for (xs = 0; xs < selem; xs++) {
3890         if (replicate) {
3891             /* Load and replicate to all elements */
3892             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3893 
3894             tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
3895             tcg_gen_gvec_dup_i64(scale, vec_full_reg_offset(s, rt),
3896                                  (is_q + 1) * 8, vec_full_reg_size(s),
3897                                  tcg_tmp);
3898         } else {
3899             /* Load/store one element per register */
3900             if (is_load) {
3901                 do_vec_ld(s, rt, index, clean_addr, mop);
3902             } else {
3903                 do_vec_st(s, rt, index, clean_addr, mop);
3904             }
3905         }
3906         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3907         rt = (rt + 1) % 32;
3908     }
3909 
3910     if (is_postidx) {
3911         if (rm == 31) {
3912             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3913         } else {
3914             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3915         }
3916     }
3917 }
3918 
3919 /*
3920  * Load/Store memory tags
3921  *
3922  *  31 30 29         24     22  21     12    10      5      0
3923  * +-----+-------------+-----+---+------+-----+------+------+
3924  * | 1 1 | 0 1 1 0 0 1 | op1 | 1 | imm9 | op2 |  Rn  |  Rt  |
3925  * +-----+-------------+-----+---+------+-----+------+------+
3926  */
3927 static void disas_ldst_tag(DisasContext *s, uint32_t insn)
3928 {
3929     int rt = extract32(insn, 0, 5);
3930     int rn = extract32(insn, 5, 5);
3931     uint64_t offset = sextract64(insn, 12, 9) << LOG2_TAG_GRANULE;
3932     int op2 = extract32(insn, 10, 2);
3933     int op1 = extract32(insn, 22, 2);
3934     bool is_load = false, is_pair = false, is_zero = false, is_mult = false;
3935     int index = 0;
3936     TCGv_i64 addr, clean_addr, tcg_rt;
3937 
3938     /* We checked insn bits [29:24,21] in the caller.  */
3939     if (extract32(insn, 30, 2) != 3) {
3940         goto do_unallocated;
3941     }
3942 
3943     /*
3944      * @index is a tri-state variable which has 3 states:
3945      * < 0 : post-index, writeback
3946      * = 0 : signed offset
3947      * > 0 : pre-index, writeback
3948      */
3949     switch (op1) {
3950     case 0:
3951         if (op2 != 0) {
3952             /* STG */
3953             index = op2 - 2;
3954         } else {
3955             /* STZGM */
3956             if (s->current_el == 0 || offset != 0) {
3957                 goto do_unallocated;
3958             }
3959             is_mult = is_zero = true;
3960         }
3961         break;
3962     case 1:
3963         if (op2 != 0) {
3964             /* STZG */
3965             is_zero = true;
3966             index = op2 - 2;
3967         } else {
3968             /* LDG */
3969             is_load = true;
3970         }
3971         break;
3972     case 2:
3973         if (op2 != 0) {
3974             /* ST2G */
3975             is_pair = true;
3976             index = op2 - 2;
3977         } else {
3978             /* STGM */
3979             if (s->current_el == 0 || offset != 0) {
3980                 goto do_unallocated;
3981             }
3982             is_mult = true;
3983         }
3984         break;
3985     case 3:
3986         if (op2 != 0) {
3987             /* STZ2G */
3988             is_pair = is_zero = true;
3989             index = op2 - 2;
3990         } else {
3991             /* LDGM */
3992             if (s->current_el == 0 || offset != 0) {
3993                 goto do_unallocated;
3994             }
3995             is_mult = is_load = true;
3996         }
3997         break;
3998 
3999     default:
4000     do_unallocated:
4001         unallocated_encoding(s);
4002         return;
4003     }
4004 
4005     if (is_mult
4006         ? !dc_isar_feature(aa64_mte, s)
4007         : !dc_isar_feature(aa64_mte_insn_reg, s)) {
4008         goto do_unallocated;
4009     }
4010 
4011     if (rn == 31) {
4012         gen_check_sp_alignment(s);
4013     }
4014 
4015     addr = read_cpu_reg_sp(s, rn, true);
4016     if (index >= 0) {
4017         /* pre-index or signed offset */
4018         tcg_gen_addi_i64(addr, addr, offset);
4019     }
4020 
4021     if (is_mult) {
4022         tcg_rt = cpu_reg(s, rt);
4023 
4024         if (is_zero) {
4025             int size = 4 << s->dcz_blocksize;
4026 
4027             if (s->ata) {
4028                 gen_helper_stzgm_tags(cpu_env, addr, tcg_rt);
4029             }
4030             /*
4031              * The non-tags portion of STZGM is mostly like DC_ZVA,
4032              * except the alignment happens before the access.
4033              */
4034             clean_addr = clean_data_tbi(s, addr);
4035             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4036             gen_helper_dc_zva(cpu_env, clean_addr);
4037         } else if (s->ata) {
4038             if (is_load) {
4039                 gen_helper_ldgm(tcg_rt, cpu_env, addr);
4040             } else {
4041                 gen_helper_stgm(cpu_env, addr, tcg_rt);
4042             }
4043         } else {
4044             MMUAccessType acc = is_load ? MMU_DATA_LOAD : MMU_DATA_STORE;
4045             int size = 4 << GMID_EL1_BS;
4046 
4047             clean_addr = clean_data_tbi(s, addr);
4048             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4049             gen_probe_access(s, clean_addr, acc, size);
4050 
4051             if (is_load) {
4052                 /* The result tags are zeros.  */
4053                 tcg_gen_movi_i64(tcg_rt, 0);
4054             }
4055         }
4056         return;
4057     }
4058 
4059     if (is_load) {
4060         tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
4061         tcg_rt = cpu_reg(s, rt);
4062         if (s->ata) {
4063             gen_helper_ldg(tcg_rt, cpu_env, addr, tcg_rt);
4064         } else {
4065             clean_addr = clean_data_tbi(s, addr);
4066             gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
4067             gen_address_with_allocation_tag0(tcg_rt, addr);
4068         }
4069     } else {
4070         tcg_rt = cpu_reg_sp(s, rt);
4071         if (!s->ata) {
4072             /*
4073              * For STG and ST2G, we need to check alignment and probe memory.
4074              * TODO: For STZG and STZ2G, we could rely on the stores below,
4075              * at least for system mode; user-only won't enforce alignment.
4076              */
4077             if (is_pair) {
4078                 gen_helper_st2g_stub(cpu_env, addr);
4079             } else {
4080                 gen_helper_stg_stub(cpu_env, addr);
4081             }
4082         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
4083             if (is_pair) {
4084                 gen_helper_st2g_parallel(cpu_env, addr, tcg_rt);
4085             } else {
4086                 gen_helper_stg_parallel(cpu_env, addr, tcg_rt);
4087             }
4088         } else {
4089             if (is_pair) {
4090                 gen_helper_st2g(cpu_env, addr, tcg_rt);
4091             } else {
4092                 gen_helper_stg(cpu_env, addr, tcg_rt);
4093             }
4094         }
4095     }
4096 
4097     if (is_zero) {
4098         TCGv_i64 clean_addr = clean_data_tbi(s, addr);
4099         TCGv_i64 zero64 = tcg_constant_i64(0);
4100         TCGv_i128 zero128 = tcg_temp_new_i128();
4101         int mem_index = get_mem_index(s);
4102         MemOp mop = finalize_memop(s, MO_128 | MO_ALIGN);
4103 
4104         tcg_gen_concat_i64_i128(zero128, zero64, zero64);
4105 
4106         /* This is 1 or 2 atomic 16-byte operations. */
4107         tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4108         if (is_pair) {
4109             tcg_gen_addi_i64(clean_addr, clean_addr, 16);
4110             tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4111         }
4112     }
4113 
4114     if (index != 0) {
4115         /* pre-index or post-index */
4116         if (index < 0) {
4117             /* post-index */
4118             tcg_gen_addi_i64(addr, addr, offset);
4119         }
4120         tcg_gen_mov_i64(cpu_reg_sp(s, rn), addr);
4121     }
4122 }
4123 
4124 /* Loads and stores */
4125 static void disas_ldst(DisasContext *s, uint32_t insn)
4126 {
4127     switch (extract32(insn, 24, 6)) {
4128     case 0x08: /* Load/store exclusive */
4129         disas_ldst_excl(s, insn);
4130         break;
4131     case 0x18: case 0x1c: /* Load register (literal) */
4132         disas_ld_lit(s, insn);
4133         break;
4134     case 0x28: case 0x29:
4135     case 0x2c: case 0x2d: /* Load/store pair (all forms) */
4136         disas_ldst_pair(s, insn);
4137         break;
4138     case 0x38: case 0x39:
4139     case 0x3c: case 0x3d: /* Load/store register (all forms) */
4140         disas_ldst_reg(s, insn);
4141         break;
4142     case 0x0c: /* AdvSIMD load/store multiple structures */
4143         disas_ldst_multiple_struct(s, insn);
4144         break;
4145     case 0x0d: /* AdvSIMD load/store single structure */
4146         disas_ldst_single_struct(s, insn);
4147         break;
4148     case 0x19:
4149         if (extract32(insn, 21, 1) != 0) {
4150             disas_ldst_tag(s, insn);
4151         } else if (extract32(insn, 10, 2) == 0) {
4152             disas_ldst_ldapr_stlr(s, insn);
4153         } else {
4154             unallocated_encoding(s);
4155         }
4156         break;
4157     default:
4158         unallocated_encoding(s);
4159         break;
4160     }
4161 }
4162 
4163 typedef void ArithTwoOp(TCGv_i64, TCGv_i64, TCGv_i64);
4164 
4165 static bool gen_rri(DisasContext *s, arg_rri_sf *a,
4166                     bool rd_sp, bool rn_sp, ArithTwoOp *fn)
4167 {
4168     TCGv_i64 tcg_rn = rn_sp ? cpu_reg_sp(s, a->rn) : cpu_reg(s, a->rn);
4169     TCGv_i64 tcg_rd = rd_sp ? cpu_reg_sp(s, a->rd) : cpu_reg(s, a->rd);
4170     TCGv_i64 tcg_imm = tcg_constant_i64(a->imm);
4171 
4172     fn(tcg_rd, tcg_rn, tcg_imm);
4173     if (!a->sf) {
4174         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4175     }
4176     return true;
4177 }
4178 
4179 /*
4180  * PC-rel. addressing
4181  */
4182 
4183 static bool trans_ADR(DisasContext *s, arg_ri *a)
4184 {
4185     gen_pc_plus_diff(s, cpu_reg(s, a->rd), a->imm);
4186     return true;
4187 }
4188 
4189 static bool trans_ADRP(DisasContext *s, arg_ri *a)
4190 {
4191     int64_t offset = (int64_t)a->imm << 12;
4192 
4193     /* The page offset is ok for CF_PCREL. */
4194     offset -= s->pc_curr & 0xfff;
4195     gen_pc_plus_diff(s, cpu_reg(s, a->rd), offset);
4196     return true;
4197 }
4198 
4199 /*
4200  * Add/subtract (immediate)
4201  */
4202 TRANS(ADD_i, gen_rri, a, 1, 1, tcg_gen_add_i64)
4203 TRANS(SUB_i, gen_rri, a, 1, 1, tcg_gen_sub_i64)
4204 TRANS(ADDS_i, gen_rri, a, 0, 1, a->sf ? gen_add64_CC : gen_add32_CC)
4205 TRANS(SUBS_i, gen_rri, a, 0, 1, a->sf ? gen_sub64_CC : gen_sub32_CC)
4206 
4207 /*
4208  * Add/subtract (immediate, with tags)
4209  */
4210 
4211 static bool gen_add_sub_imm_with_tags(DisasContext *s, arg_rri_tag *a,
4212                                       bool sub_op)
4213 {
4214     TCGv_i64 tcg_rn, tcg_rd;
4215     int imm;
4216 
4217     imm = a->uimm6 << LOG2_TAG_GRANULE;
4218     if (sub_op) {
4219         imm = -imm;
4220     }
4221 
4222     tcg_rn = cpu_reg_sp(s, a->rn);
4223     tcg_rd = cpu_reg_sp(s, a->rd);
4224 
4225     if (s->ata) {
4226         gen_helper_addsubg(tcg_rd, cpu_env, tcg_rn,
4227                            tcg_constant_i32(imm),
4228                            tcg_constant_i32(a->uimm4));
4229     } else {
4230         tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4231         gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4232     }
4233     return true;
4234 }
4235 
4236 TRANS_FEAT(ADDG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, false)
4237 TRANS_FEAT(SUBG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, true)
4238 
4239 /* The input should be a value in the bottom e bits (with higher
4240  * bits zero); returns that value replicated into every element
4241  * of size e in a 64 bit integer.
4242  */
4243 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4244 {
4245     assert(e != 0);
4246     while (e < 64) {
4247         mask |= mask << e;
4248         e *= 2;
4249     }
4250     return mask;
4251 }
4252 
4253 /*
4254  * Logical (immediate)
4255  */
4256 
4257 /*
4258  * Simplified variant of pseudocode DecodeBitMasks() for the case where we
4259  * only require the wmask. Returns false if the imms/immr/immn are a reserved
4260  * value (ie should cause a guest UNDEF exception), and true if they are
4261  * valid, in which case the decoded bit pattern is written to result.
4262  */
4263 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4264                             unsigned int imms, unsigned int immr)
4265 {
4266     uint64_t mask;
4267     unsigned e, levels, s, r;
4268     int len;
4269 
4270     assert(immn < 2 && imms < 64 && immr < 64);
4271 
4272     /* The bit patterns we create here are 64 bit patterns which
4273      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4274      * 64 bits each. Each element contains the same value: a run
4275      * of between 1 and e-1 non-zero bits, rotated within the
4276      * element by between 0 and e-1 bits.
4277      *
4278      * The element size and run length are encoded into immn (1 bit)
4279      * and imms (6 bits) as follows:
4280      * 64 bit elements: immn = 1, imms = <length of run - 1>
4281      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4282      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4283      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4284      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4285      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4286      * Notice that immn = 0, imms = 11111x is the only combination
4287      * not covered by one of the above options; this is reserved.
4288      * Further, <length of run - 1> all-ones is a reserved pattern.
4289      *
4290      * In all cases the rotation is by immr % e (and immr is 6 bits).
4291      */
4292 
4293     /* First determine the element size */
4294     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4295     if (len < 1) {
4296         /* This is the immn == 0, imms == 0x11111x case */
4297         return false;
4298     }
4299     e = 1 << len;
4300 
4301     levels = e - 1;
4302     s = imms & levels;
4303     r = immr & levels;
4304 
4305     if (s == levels) {
4306         /* <length of run - 1> mustn't be all-ones. */
4307         return false;
4308     }
4309 
4310     /* Create the value of one element: s+1 set bits rotated
4311      * by r within the element (which is e bits wide)...
4312      */
4313     mask = MAKE_64BIT_MASK(0, s + 1);
4314     if (r) {
4315         mask = (mask >> r) | (mask << (e - r));
4316         mask &= MAKE_64BIT_MASK(0, e);
4317     }
4318     /* ...then replicate the element over the whole 64 bit value */
4319     mask = bitfield_replicate(mask, e);
4320     *result = mask;
4321     return true;
4322 }
4323 
4324 static bool gen_rri_log(DisasContext *s, arg_rri_log *a, bool set_cc,
4325                         void (*fn)(TCGv_i64, TCGv_i64, int64_t))
4326 {
4327     TCGv_i64 tcg_rd, tcg_rn;
4328     uint64_t imm;
4329 
4330     /* Some immediate field values are reserved. */
4331     if (!logic_imm_decode_wmask(&imm, extract32(a->dbm, 12, 1),
4332                                 extract32(a->dbm, 0, 6),
4333                                 extract32(a->dbm, 6, 6))) {
4334         return false;
4335     }
4336     if (!a->sf) {
4337         imm &= 0xffffffffull;
4338     }
4339 
4340     tcg_rd = set_cc ? cpu_reg(s, a->rd) : cpu_reg_sp(s, a->rd);
4341     tcg_rn = cpu_reg(s, a->rn);
4342 
4343     fn(tcg_rd, tcg_rn, imm);
4344     if (set_cc) {
4345         gen_logic_CC(a->sf, tcg_rd);
4346     }
4347     if (!a->sf) {
4348         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4349     }
4350     return true;
4351 }
4352 
4353 TRANS(AND_i, gen_rri_log, a, false, tcg_gen_andi_i64)
4354 TRANS(ORR_i, gen_rri_log, a, false, tcg_gen_ori_i64)
4355 TRANS(EOR_i, gen_rri_log, a, false, tcg_gen_xori_i64)
4356 TRANS(ANDS_i, gen_rri_log, a, true, tcg_gen_andi_i64)
4357 
4358 /*
4359  * Move wide (immediate)
4360  */
4361 
4362 static bool trans_MOVZ(DisasContext *s, arg_movw *a)
4363 {
4364     int pos = a->hw << 4;
4365     tcg_gen_movi_i64(cpu_reg(s, a->rd), (uint64_t)a->imm << pos);
4366     return true;
4367 }
4368 
4369 static bool trans_MOVN(DisasContext *s, arg_movw *a)
4370 {
4371     int pos = a->hw << 4;
4372     uint64_t imm = a->imm;
4373 
4374     imm = ~(imm << pos);
4375     if (!a->sf) {
4376         imm = (uint32_t)imm;
4377     }
4378     tcg_gen_movi_i64(cpu_reg(s, a->rd), imm);
4379     return true;
4380 }
4381 
4382 static bool trans_MOVK(DisasContext *s, arg_movw *a)
4383 {
4384     int pos = a->hw << 4;
4385     TCGv_i64 tcg_rd, tcg_im;
4386 
4387     tcg_rd = cpu_reg(s, a->rd);
4388     tcg_im = tcg_constant_i64(a->imm);
4389     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_im, pos, 16);
4390     if (!a->sf) {
4391         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4392     }
4393     return true;
4394 }
4395 
4396 /*
4397  * Bitfield
4398  */
4399 
4400 static bool trans_SBFM(DisasContext *s, arg_SBFM *a)
4401 {
4402     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4403     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4404     unsigned int bitsize = a->sf ? 64 : 32;
4405     unsigned int ri = a->immr;
4406     unsigned int si = a->imms;
4407     unsigned int pos, len;
4408 
4409     if (si >= ri) {
4410         /* Wd<s-r:0> = Wn<s:r> */
4411         len = (si - ri) + 1;
4412         tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4413         if (!a->sf) {
4414             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4415         }
4416     } else {
4417         /* Wd<32+s-r,32-r> = Wn<s:0> */
4418         len = si + 1;
4419         pos = (bitsize - ri) & (bitsize - 1);
4420 
4421         if (len < ri) {
4422             /*
4423              * Sign extend the destination field from len to fill the
4424              * balance of the word.  Let the deposit below insert all
4425              * of those sign bits.
4426              */
4427             tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4428             len = ri;
4429         }
4430 
4431         /*
4432          * We start with zero, and we haven't modified any bits outside
4433          * bitsize, therefore no final zero-extension is unneeded for !sf.
4434          */
4435         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4436     }
4437     return true;
4438 }
4439 
4440 static bool trans_UBFM(DisasContext *s, arg_UBFM *a)
4441 {
4442     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4443     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4444     unsigned int bitsize = a->sf ? 64 : 32;
4445     unsigned int ri = a->immr;
4446     unsigned int si = a->imms;
4447     unsigned int pos, len;
4448 
4449     tcg_rd = cpu_reg(s, a->rd);
4450     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4451 
4452     if (si >= ri) {
4453         /* Wd<s-r:0> = Wn<s:r> */
4454         len = (si - ri) + 1;
4455         tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4456     } else {
4457         /* Wd<32+s-r,32-r> = Wn<s:0> */
4458         len = si + 1;
4459         pos = (bitsize - ri) & (bitsize - 1);
4460         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4461     }
4462     return true;
4463 }
4464 
4465 static bool trans_BFM(DisasContext *s, arg_BFM *a)
4466 {
4467     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4468     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4469     unsigned int bitsize = a->sf ? 64 : 32;
4470     unsigned int ri = a->immr;
4471     unsigned int si = a->imms;
4472     unsigned int pos, len;
4473 
4474     tcg_rd = cpu_reg(s, a->rd);
4475     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4476 
4477     if (si >= ri) {
4478         /* Wd<s-r:0> = Wn<s:r> */
4479         tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4480         len = (si - ri) + 1;
4481         pos = 0;
4482     } else {
4483         /* Wd<32+s-r,32-r> = Wn<s:0> */
4484         len = si + 1;
4485         pos = (bitsize - ri) & (bitsize - 1);
4486     }
4487 
4488     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4489     if (!a->sf) {
4490         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4491     }
4492     return true;
4493 }
4494 
4495 static bool trans_EXTR(DisasContext *s, arg_extract *a)
4496 {
4497     TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4498 
4499     tcg_rd = cpu_reg(s, a->rd);
4500 
4501     if (unlikely(a->imm == 0)) {
4502         /*
4503          * tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4504          * so an extract from bit 0 is a special case.
4505          */
4506         if (a->sf) {
4507             tcg_gen_mov_i64(tcg_rd, cpu_reg(s, a->rm));
4508         } else {
4509             tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, a->rm));
4510         }
4511     } else {
4512         tcg_rm = cpu_reg(s, a->rm);
4513         tcg_rn = cpu_reg(s, a->rn);
4514 
4515         if (a->sf) {
4516             /* Specialization to ROR happens in EXTRACT2.  */
4517             tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, a->imm);
4518         } else {
4519             TCGv_i32 t0 = tcg_temp_new_i32();
4520 
4521             tcg_gen_extrl_i64_i32(t0, tcg_rm);
4522             if (a->rm == a->rn) {
4523                 tcg_gen_rotri_i32(t0, t0, a->imm);
4524             } else {
4525                 TCGv_i32 t1 = tcg_temp_new_i32();
4526                 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4527                 tcg_gen_extract2_i32(t0, t0, t1, a->imm);
4528             }
4529             tcg_gen_extu_i32_i64(tcg_rd, t0);
4530         }
4531     }
4532     return true;
4533 }
4534 
4535 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4536  * Note that it is the caller's responsibility to ensure that the
4537  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4538  * mandated semantics for out of range shifts.
4539  */
4540 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4541                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4542 {
4543     switch (shift_type) {
4544     case A64_SHIFT_TYPE_LSL:
4545         tcg_gen_shl_i64(dst, src, shift_amount);
4546         break;
4547     case A64_SHIFT_TYPE_LSR:
4548         tcg_gen_shr_i64(dst, src, shift_amount);
4549         break;
4550     case A64_SHIFT_TYPE_ASR:
4551         if (!sf) {
4552             tcg_gen_ext32s_i64(dst, src);
4553         }
4554         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4555         break;
4556     case A64_SHIFT_TYPE_ROR:
4557         if (sf) {
4558             tcg_gen_rotr_i64(dst, src, shift_amount);
4559         } else {
4560             TCGv_i32 t0, t1;
4561             t0 = tcg_temp_new_i32();
4562             t1 = tcg_temp_new_i32();
4563             tcg_gen_extrl_i64_i32(t0, src);
4564             tcg_gen_extrl_i64_i32(t1, shift_amount);
4565             tcg_gen_rotr_i32(t0, t0, t1);
4566             tcg_gen_extu_i32_i64(dst, t0);
4567         }
4568         break;
4569     default:
4570         assert(FALSE); /* all shift types should be handled */
4571         break;
4572     }
4573 
4574     if (!sf) { /* zero extend final result */
4575         tcg_gen_ext32u_i64(dst, dst);
4576     }
4577 }
4578 
4579 /* Shift a TCGv src by immediate, put result in dst.
4580  * The shift amount must be in range (this should always be true as the
4581  * relevant instructions will UNDEF on bad shift immediates).
4582  */
4583 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4584                           enum a64_shift_type shift_type, unsigned int shift_i)
4585 {
4586     assert(shift_i < (sf ? 64 : 32));
4587 
4588     if (shift_i == 0) {
4589         tcg_gen_mov_i64(dst, src);
4590     } else {
4591         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
4592     }
4593 }
4594 
4595 /* Logical (shifted register)
4596  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
4597  * +----+-----+-----------+-------+---+------+--------+------+------+
4598  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
4599  * +----+-----+-----------+-------+---+------+--------+------+------+
4600  */
4601 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4602 {
4603     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4604     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4605 
4606     sf = extract32(insn, 31, 1);
4607     opc = extract32(insn, 29, 2);
4608     shift_type = extract32(insn, 22, 2);
4609     invert = extract32(insn, 21, 1);
4610     rm = extract32(insn, 16, 5);
4611     shift_amount = extract32(insn, 10, 6);
4612     rn = extract32(insn, 5, 5);
4613     rd = extract32(insn, 0, 5);
4614 
4615     if (!sf && (shift_amount & (1 << 5))) {
4616         unallocated_encoding(s);
4617         return;
4618     }
4619 
4620     tcg_rd = cpu_reg(s, rd);
4621 
4622     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4623         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4624          * register-register MOV and MVN, so it is worth special casing.
4625          */
4626         tcg_rm = cpu_reg(s, rm);
4627         if (invert) {
4628             tcg_gen_not_i64(tcg_rd, tcg_rm);
4629             if (!sf) {
4630                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4631             }
4632         } else {
4633             if (sf) {
4634                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4635             } else {
4636                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4637             }
4638         }
4639         return;
4640     }
4641 
4642     tcg_rm = read_cpu_reg(s, rm, sf);
4643 
4644     if (shift_amount) {
4645         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4646     }
4647 
4648     tcg_rn = cpu_reg(s, rn);
4649 
4650     switch (opc | (invert << 2)) {
4651     case 0: /* AND */
4652     case 3: /* ANDS */
4653         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4654         break;
4655     case 1: /* ORR */
4656         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4657         break;
4658     case 2: /* EOR */
4659         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4660         break;
4661     case 4: /* BIC */
4662     case 7: /* BICS */
4663         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4664         break;
4665     case 5: /* ORN */
4666         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4667         break;
4668     case 6: /* EON */
4669         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4670         break;
4671     default:
4672         assert(FALSE);
4673         break;
4674     }
4675 
4676     if (!sf) {
4677         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4678     }
4679 
4680     if (opc == 3) {
4681         gen_logic_CC(sf, tcg_rd);
4682     }
4683 }
4684 
4685 /*
4686  * Add/subtract (extended register)
4687  *
4688  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
4689  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4690  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
4691  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4692  *
4693  *  sf: 0 -> 32bit, 1 -> 64bit
4694  *  op: 0 -> add  , 1 -> sub
4695  *   S: 1 -> set flags
4696  * opt: 00
4697  * option: extension type (see DecodeRegExtend)
4698  * imm3: optional shift to Rm
4699  *
4700  * Rd = Rn + LSL(extend(Rm), amount)
4701  */
4702 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4703 {
4704     int rd = extract32(insn, 0, 5);
4705     int rn = extract32(insn, 5, 5);
4706     int imm3 = extract32(insn, 10, 3);
4707     int option = extract32(insn, 13, 3);
4708     int rm = extract32(insn, 16, 5);
4709     int opt = extract32(insn, 22, 2);
4710     bool setflags = extract32(insn, 29, 1);
4711     bool sub_op = extract32(insn, 30, 1);
4712     bool sf = extract32(insn, 31, 1);
4713 
4714     TCGv_i64 tcg_rm, tcg_rn; /* temps */
4715     TCGv_i64 tcg_rd;
4716     TCGv_i64 tcg_result;
4717 
4718     if (imm3 > 4 || opt != 0) {
4719         unallocated_encoding(s);
4720         return;
4721     }
4722 
4723     /* non-flag setting ops may use SP */
4724     if (!setflags) {
4725         tcg_rd = cpu_reg_sp(s, rd);
4726     } else {
4727         tcg_rd = cpu_reg(s, rd);
4728     }
4729     tcg_rn = read_cpu_reg_sp(s, rn, sf);
4730 
4731     tcg_rm = read_cpu_reg(s, rm, sf);
4732     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4733 
4734     tcg_result = tcg_temp_new_i64();
4735 
4736     if (!setflags) {
4737         if (sub_op) {
4738             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4739         } else {
4740             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4741         }
4742     } else {
4743         if (sub_op) {
4744             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4745         } else {
4746             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4747         }
4748     }
4749 
4750     if (sf) {
4751         tcg_gen_mov_i64(tcg_rd, tcg_result);
4752     } else {
4753         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4754     }
4755 }
4756 
4757 /*
4758  * Add/subtract (shifted register)
4759  *
4760  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
4761  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4762  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
4763  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4764  *
4765  *    sf: 0 -> 32bit, 1 -> 64bit
4766  *    op: 0 -> add  , 1 -> sub
4767  *     S: 1 -> set flags
4768  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4769  *  imm6: Shift amount to apply to Rm before the add/sub
4770  */
4771 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4772 {
4773     int rd = extract32(insn, 0, 5);
4774     int rn = extract32(insn, 5, 5);
4775     int imm6 = extract32(insn, 10, 6);
4776     int rm = extract32(insn, 16, 5);
4777     int shift_type = extract32(insn, 22, 2);
4778     bool setflags = extract32(insn, 29, 1);
4779     bool sub_op = extract32(insn, 30, 1);
4780     bool sf = extract32(insn, 31, 1);
4781 
4782     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4783     TCGv_i64 tcg_rn, tcg_rm;
4784     TCGv_i64 tcg_result;
4785 
4786     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4787         unallocated_encoding(s);
4788         return;
4789     }
4790 
4791     tcg_rn = read_cpu_reg(s, rn, sf);
4792     tcg_rm = read_cpu_reg(s, rm, sf);
4793 
4794     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4795 
4796     tcg_result = tcg_temp_new_i64();
4797 
4798     if (!setflags) {
4799         if (sub_op) {
4800             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4801         } else {
4802             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4803         }
4804     } else {
4805         if (sub_op) {
4806             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4807         } else {
4808             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4809         }
4810     }
4811 
4812     if (sf) {
4813         tcg_gen_mov_i64(tcg_rd, tcg_result);
4814     } else {
4815         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4816     }
4817 }
4818 
4819 /* Data-processing (3 source)
4820  *
4821  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
4822  *  +--+------+-----------+------+------+----+------+------+------+
4823  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
4824  *  +--+------+-----------+------+------+----+------+------+------+
4825  */
4826 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4827 {
4828     int rd = extract32(insn, 0, 5);
4829     int rn = extract32(insn, 5, 5);
4830     int ra = extract32(insn, 10, 5);
4831     int rm = extract32(insn, 16, 5);
4832     int op_id = (extract32(insn, 29, 3) << 4) |
4833         (extract32(insn, 21, 3) << 1) |
4834         extract32(insn, 15, 1);
4835     bool sf = extract32(insn, 31, 1);
4836     bool is_sub = extract32(op_id, 0, 1);
4837     bool is_high = extract32(op_id, 2, 1);
4838     bool is_signed = false;
4839     TCGv_i64 tcg_op1;
4840     TCGv_i64 tcg_op2;
4841     TCGv_i64 tcg_tmp;
4842 
4843     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
4844     switch (op_id) {
4845     case 0x42: /* SMADDL */
4846     case 0x43: /* SMSUBL */
4847     case 0x44: /* SMULH */
4848         is_signed = true;
4849         break;
4850     case 0x0: /* MADD (32bit) */
4851     case 0x1: /* MSUB (32bit) */
4852     case 0x40: /* MADD (64bit) */
4853     case 0x41: /* MSUB (64bit) */
4854     case 0x4a: /* UMADDL */
4855     case 0x4b: /* UMSUBL */
4856     case 0x4c: /* UMULH */
4857         break;
4858     default:
4859         unallocated_encoding(s);
4860         return;
4861     }
4862 
4863     if (is_high) {
4864         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
4865         TCGv_i64 tcg_rd = cpu_reg(s, rd);
4866         TCGv_i64 tcg_rn = cpu_reg(s, rn);
4867         TCGv_i64 tcg_rm = cpu_reg(s, rm);
4868 
4869         if (is_signed) {
4870             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4871         } else {
4872             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4873         }
4874         return;
4875     }
4876 
4877     tcg_op1 = tcg_temp_new_i64();
4878     tcg_op2 = tcg_temp_new_i64();
4879     tcg_tmp = tcg_temp_new_i64();
4880 
4881     if (op_id < 0x42) {
4882         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
4883         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
4884     } else {
4885         if (is_signed) {
4886             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
4887             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
4888         } else {
4889             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
4890             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
4891         }
4892     }
4893 
4894     if (ra == 31 && !is_sub) {
4895         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
4896         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
4897     } else {
4898         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
4899         if (is_sub) {
4900             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4901         } else {
4902             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4903         }
4904     }
4905 
4906     if (!sf) {
4907         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
4908     }
4909 }
4910 
4911 /* Add/subtract (with carry)
4912  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
4913  * +--+--+--+------------------------+------+-------------+------+-----+
4914  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
4915  * +--+--+--+------------------------+------+-------------+------+-----+
4916  */
4917 
4918 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
4919 {
4920     unsigned int sf, op, setflags, rm, rn, rd;
4921     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
4922 
4923     sf = extract32(insn, 31, 1);
4924     op = extract32(insn, 30, 1);
4925     setflags = extract32(insn, 29, 1);
4926     rm = extract32(insn, 16, 5);
4927     rn = extract32(insn, 5, 5);
4928     rd = extract32(insn, 0, 5);
4929 
4930     tcg_rd = cpu_reg(s, rd);
4931     tcg_rn = cpu_reg(s, rn);
4932 
4933     if (op) {
4934         tcg_y = tcg_temp_new_i64();
4935         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
4936     } else {
4937         tcg_y = cpu_reg(s, rm);
4938     }
4939 
4940     if (setflags) {
4941         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
4942     } else {
4943         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
4944     }
4945 }
4946 
4947 /*
4948  * Rotate right into flags
4949  *  31 30 29                21       15          10      5  4      0
4950  * +--+--+--+-----------------+--------+-----------+------+--+------+
4951  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
4952  * +--+--+--+-----------------+--------+-----------+------+--+------+
4953  */
4954 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
4955 {
4956     int mask = extract32(insn, 0, 4);
4957     int o2 = extract32(insn, 4, 1);
4958     int rn = extract32(insn, 5, 5);
4959     int imm6 = extract32(insn, 15, 6);
4960     int sf_op_s = extract32(insn, 29, 3);
4961     TCGv_i64 tcg_rn;
4962     TCGv_i32 nzcv;
4963 
4964     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
4965         unallocated_encoding(s);
4966         return;
4967     }
4968 
4969     tcg_rn = read_cpu_reg(s, rn, 1);
4970     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
4971 
4972     nzcv = tcg_temp_new_i32();
4973     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
4974 
4975     if (mask & 8) { /* N */
4976         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
4977     }
4978     if (mask & 4) { /* Z */
4979         tcg_gen_not_i32(cpu_ZF, nzcv);
4980         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
4981     }
4982     if (mask & 2) { /* C */
4983         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
4984     }
4985     if (mask & 1) { /* V */
4986         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
4987     }
4988 }
4989 
4990 /*
4991  * Evaluate into flags
4992  *  31 30 29                21        15   14        10      5  4      0
4993  * +--+--+--+-----------------+---------+----+---------+------+--+------+
4994  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
4995  * +--+--+--+-----------------+---------+----+---------+------+--+------+
4996  */
4997 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
4998 {
4999     int o3_mask = extract32(insn, 0, 5);
5000     int rn = extract32(insn, 5, 5);
5001     int o2 = extract32(insn, 15, 6);
5002     int sz = extract32(insn, 14, 1);
5003     int sf_op_s = extract32(insn, 29, 3);
5004     TCGv_i32 tmp;
5005     int shift;
5006 
5007     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
5008         !dc_isar_feature(aa64_condm_4, s)) {
5009         unallocated_encoding(s);
5010         return;
5011     }
5012     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
5013 
5014     tmp = tcg_temp_new_i32();
5015     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
5016     tcg_gen_shli_i32(cpu_NF, tmp, shift);
5017     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
5018     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
5019     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
5020 }
5021 
5022 /* Conditional compare (immediate / register)
5023  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
5024  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5025  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
5026  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5027  *        [1]                             y                [0]       [0]
5028  */
5029 static void disas_cc(DisasContext *s, uint32_t insn)
5030 {
5031     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
5032     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
5033     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
5034     DisasCompare c;
5035 
5036     if (!extract32(insn, 29, 1)) {
5037         unallocated_encoding(s);
5038         return;
5039     }
5040     if (insn & (1 << 10 | 1 << 4)) {
5041         unallocated_encoding(s);
5042         return;
5043     }
5044     sf = extract32(insn, 31, 1);
5045     op = extract32(insn, 30, 1);
5046     is_imm = extract32(insn, 11, 1);
5047     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
5048     cond = extract32(insn, 12, 4);
5049     rn = extract32(insn, 5, 5);
5050     nzcv = extract32(insn, 0, 4);
5051 
5052     /* Set T0 = !COND.  */
5053     tcg_t0 = tcg_temp_new_i32();
5054     arm_test_cc(&c, cond);
5055     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
5056 
5057     /* Load the arguments for the new comparison.  */
5058     if (is_imm) {
5059         tcg_y = tcg_temp_new_i64();
5060         tcg_gen_movi_i64(tcg_y, y);
5061     } else {
5062         tcg_y = cpu_reg(s, y);
5063     }
5064     tcg_rn = cpu_reg(s, rn);
5065 
5066     /* Set the flags for the new comparison.  */
5067     tcg_tmp = tcg_temp_new_i64();
5068     if (op) {
5069         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5070     } else {
5071         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5072     }
5073 
5074     /* If COND was false, force the flags to #nzcv.  Compute two masks
5075      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
5076      * For tcg hosts that support ANDC, we can make do with just T1.
5077      * In either case, allow the tcg optimizer to delete any unused mask.
5078      */
5079     tcg_t1 = tcg_temp_new_i32();
5080     tcg_t2 = tcg_temp_new_i32();
5081     tcg_gen_neg_i32(tcg_t1, tcg_t0);
5082     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
5083 
5084     if (nzcv & 8) { /* N */
5085         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
5086     } else {
5087         if (TCG_TARGET_HAS_andc_i32) {
5088             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
5089         } else {
5090             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
5091         }
5092     }
5093     if (nzcv & 4) { /* Z */
5094         if (TCG_TARGET_HAS_andc_i32) {
5095             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
5096         } else {
5097             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
5098         }
5099     } else {
5100         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
5101     }
5102     if (nzcv & 2) { /* C */
5103         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
5104     } else {
5105         if (TCG_TARGET_HAS_andc_i32) {
5106             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
5107         } else {
5108             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
5109         }
5110     }
5111     if (nzcv & 1) { /* V */
5112         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
5113     } else {
5114         if (TCG_TARGET_HAS_andc_i32) {
5115             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
5116         } else {
5117             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
5118         }
5119     }
5120 }
5121 
5122 /* Conditional select
5123  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
5124  * +----+----+---+-----------------+------+------+-----+------+------+
5125  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
5126  * +----+----+---+-----------------+------+------+-----+------+------+
5127  */
5128 static void disas_cond_select(DisasContext *s, uint32_t insn)
5129 {
5130     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
5131     TCGv_i64 tcg_rd, zero;
5132     DisasCompare64 c;
5133 
5134     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
5135         /* S == 1 or op2<1> == 1 */
5136         unallocated_encoding(s);
5137         return;
5138     }
5139     sf = extract32(insn, 31, 1);
5140     else_inv = extract32(insn, 30, 1);
5141     rm = extract32(insn, 16, 5);
5142     cond = extract32(insn, 12, 4);
5143     else_inc = extract32(insn, 10, 1);
5144     rn = extract32(insn, 5, 5);
5145     rd = extract32(insn, 0, 5);
5146 
5147     tcg_rd = cpu_reg(s, rd);
5148 
5149     a64_test_cc(&c, cond);
5150     zero = tcg_constant_i64(0);
5151 
5152     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
5153         /* CSET & CSETM.  */
5154         tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
5155         if (else_inv) {
5156             tcg_gen_neg_i64(tcg_rd, tcg_rd);
5157         }
5158     } else {
5159         TCGv_i64 t_true = cpu_reg(s, rn);
5160         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
5161         if (else_inv && else_inc) {
5162             tcg_gen_neg_i64(t_false, t_false);
5163         } else if (else_inv) {
5164             tcg_gen_not_i64(t_false, t_false);
5165         } else if (else_inc) {
5166             tcg_gen_addi_i64(t_false, t_false, 1);
5167         }
5168         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
5169     }
5170 
5171     if (!sf) {
5172         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5173     }
5174 }
5175 
5176 static void handle_clz(DisasContext *s, unsigned int sf,
5177                        unsigned int rn, unsigned int rd)
5178 {
5179     TCGv_i64 tcg_rd, tcg_rn;
5180     tcg_rd = cpu_reg(s, rd);
5181     tcg_rn = cpu_reg(s, rn);
5182 
5183     if (sf) {
5184         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
5185     } else {
5186         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5187         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5188         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
5189         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5190     }
5191 }
5192 
5193 static void handle_cls(DisasContext *s, unsigned int sf,
5194                        unsigned int rn, unsigned int rd)
5195 {
5196     TCGv_i64 tcg_rd, tcg_rn;
5197     tcg_rd = cpu_reg(s, rd);
5198     tcg_rn = cpu_reg(s, rn);
5199 
5200     if (sf) {
5201         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
5202     } else {
5203         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5204         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5205         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
5206         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5207     }
5208 }
5209 
5210 static void handle_rbit(DisasContext *s, unsigned int sf,
5211                         unsigned int rn, unsigned int rd)
5212 {
5213     TCGv_i64 tcg_rd, tcg_rn;
5214     tcg_rd = cpu_reg(s, rd);
5215     tcg_rn = cpu_reg(s, rn);
5216 
5217     if (sf) {
5218         gen_helper_rbit64(tcg_rd, tcg_rn);
5219     } else {
5220         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5221         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5222         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5223         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5224     }
5225 }
5226 
5227 /* REV with sf==1, opcode==3 ("REV64") */
5228 static void handle_rev64(DisasContext *s, unsigned int sf,
5229                          unsigned int rn, unsigned int rd)
5230 {
5231     if (!sf) {
5232         unallocated_encoding(s);
5233         return;
5234     }
5235     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5236 }
5237 
5238 /* REV with sf==0, opcode==2
5239  * REV32 (sf==1, opcode==2)
5240  */
5241 static void handle_rev32(DisasContext *s, unsigned int sf,
5242                          unsigned int rn, unsigned int rd)
5243 {
5244     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5245     TCGv_i64 tcg_rn = cpu_reg(s, rn);
5246 
5247     if (sf) {
5248         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
5249         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
5250     } else {
5251         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
5252     }
5253 }
5254 
5255 /* REV16 (opcode==1) */
5256 static void handle_rev16(DisasContext *s, unsigned int sf,
5257                          unsigned int rn, unsigned int rd)
5258 {
5259     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5260     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5261     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5262     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5263 
5264     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5265     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5266     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5267     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5268     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5269 }
5270 
5271 /* Data-processing (1 source)
5272  *   31  30  29  28             21 20     16 15    10 9    5 4    0
5273  * +----+---+---+-----------------+---------+--------+------+------+
5274  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
5275  * +----+---+---+-----------------+---------+--------+------+------+
5276  */
5277 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5278 {
5279     unsigned int sf, opcode, opcode2, rn, rd;
5280     TCGv_i64 tcg_rd;
5281 
5282     if (extract32(insn, 29, 1)) {
5283         unallocated_encoding(s);
5284         return;
5285     }
5286 
5287     sf = extract32(insn, 31, 1);
5288     opcode = extract32(insn, 10, 6);
5289     opcode2 = extract32(insn, 16, 5);
5290     rn = extract32(insn, 5, 5);
5291     rd = extract32(insn, 0, 5);
5292 
5293 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5294 
5295     switch (MAP(sf, opcode2, opcode)) {
5296     case MAP(0, 0x00, 0x00): /* RBIT */
5297     case MAP(1, 0x00, 0x00):
5298         handle_rbit(s, sf, rn, rd);
5299         break;
5300     case MAP(0, 0x00, 0x01): /* REV16 */
5301     case MAP(1, 0x00, 0x01):
5302         handle_rev16(s, sf, rn, rd);
5303         break;
5304     case MAP(0, 0x00, 0x02): /* REV/REV32 */
5305     case MAP(1, 0x00, 0x02):
5306         handle_rev32(s, sf, rn, rd);
5307         break;
5308     case MAP(1, 0x00, 0x03): /* REV64 */
5309         handle_rev64(s, sf, rn, rd);
5310         break;
5311     case MAP(0, 0x00, 0x04): /* CLZ */
5312     case MAP(1, 0x00, 0x04):
5313         handle_clz(s, sf, rn, rd);
5314         break;
5315     case MAP(0, 0x00, 0x05): /* CLS */
5316     case MAP(1, 0x00, 0x05):
5317         handle_cls(s, sf, rn, rd);
5318         break;
5319     case MAP(1, 0x01, 0x00): /* PACIA */
5320         if (s->pauth_active) {
5321             tcg_rd = cpu_reg(s, rd);
5322             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5323         } else if (!dc_isar_feature(aa64_pauth, s)) {
5324             goto do_unallocated;
5325         }
5326         break;
5327     case MAP(1, 0x01, 0x01): /* PACIB */
5328         if (s->pauth_active) {
5329             tcg_rd = cpu_reg(s, rd);
5330             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5331         } else if (!dc_isar_feature(aa64_pauth, s)) {
5332             goto do_unallocated;
5333         }
5334         break;
5335     case MAP(1, 0x01, 0x02): /* PACDA */
5336         if (s->pauth_active) {
5337             tcg_rd = cpu_reg(s, rd);
5338             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5339         } else if (!dc_isar_feature(aa64_pauth, s)) {
5340             goto do_unallocated;
5341         }
5342         break;
5343     case MAP(1, 0x01, 0x03): /* PACDB */
5344         if (s->pauth_active) {
5345             tcg_rd = cpu_reg(s, rd);
5346             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5347         } else if (!dc_isar_feature(aa64_pauth, s)) {
5348             goto do_unallocated;
5349         }
5350         break;
5351     case MAP(1, 0x01, 0x04): /* AUTIA */
5352         if (s->pauth_active) {
5353             tcg_rd = cpu_reg(s, rd);
5354             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5355         } else if (!dc_isar_feature(aa64_pauth, s)) {
5356             goto do_unallocated;
5357         }
5358         break;
5359     case MAP(1, 0x01, 0x05): /* AUTIB */
5360         if (s->pauth_active) {
5361             tcg_rd = cpu_reg(s, rd);
5362             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5363         } else if (!dc_isar_feature(aa64_pauth, s)) {
5364             goto do_unallocated;
5365         }
5366         break;
5367     case MAP(1, 0x01, 0x06): /* AUTDA */
5368         if (s->pauth_active) {
5369             tcg_rd = cpu_reg(s, rd);
5370             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5371         } else if (!dc_isar_feature(aa64_pauth, s)) {
5372             goto do_unallocated;
5373         }
5374         break;
5375     case MAP(1, 0x01, 0x07): /* AUTDB */
5376         if (s->pauth_active) {
5377             tcg_rd = cpu_reg(s, rd);
5378             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5379         } else if (!dc_isar_feature(aa64_pauth, s)) {
5380             goto do_unallocated;
5381         }
5382         break;
5383     case MAP(1, 0x01, 0x08): /* PACIZA */
5384         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5385             goto do_unallocated;
5386         } else if (s->pauth_active) {
5387             tcg_rd = cpu_reg(s, rd);
5388             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5389         }
5390         break;
5391     case MAP(1, 0x01, 0x09): /* PACIZB */
5392         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5393             goto do_unallocated;
5394         } else if (s->pauth_active) {
5395             tcg_rd = cpu_reg(s, rd);
5396             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5397         }
5398         break;
5399     case MAP(1, 0x01, 0x0a): /* PACDZA */
5400         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5401             goto do_unallocated;
5402         } else if (s->pauth_active) {
5403             tcg_rd = cpu_reg(s, rd);
5404             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5405         }
5406         break;
5407     case MAP(1, 0x01, 0x0b): /* PACDZB */
5408         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5409             goto do_unallocated;
5410         } else if (s->pauth_active) {
5411             tcg_rd = cpu_reg(s, rd);
5412             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5413         }
5414         break;
5415     case MAP(1, 0x01, 0x0c): /* AUTIZA */
5416         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5417             goto do_unallocated;
5418         } else if (s->pauth_active) {
5419             tcg_rd = cpu_reg(s, rd);
5420             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5421         }
5422         break;
5423     case MAP(1, 0x01, 0x0d): /* AUTIZB */
5424         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5425             goto do_unallocated;
5426         } else if (s->pauth_active) {
5427             tcg_rd = cpu_reg(s, rd);
5428             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5429         }
5430         break;
5431     case MAP(1, 0x01, 0x0e): /* AUTDZA */
5432         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5433             goto do_unallocated;
5434         } else if (s->pauth_active) {
5435             tcg_rd = cpu_reg(s, rd);
5436             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5437         }
5438         break;
5439     case MAP(1, 0x01, 0x0f): /* AUTDZB */
5440         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5441             goto do_unallocated;
5442         } else if (s->pauth_active) {
5443             tcg_rd = cpu_reg(s, rd);
5444             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5445         }
5446         break;
5447     case MAP(1, 0x01, 0x10): /* XPACI */
5448         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5449             goto do_unallocated;
5450         } else if (s->pauth_active) {
5451             tcg_rd = cpu_reg(s, rd);
5452             gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
5453         }
5454         break;
5455     case MAP(1, 0x01, 0x11): /* XPACD */
5456         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5457             goto do_unallocated;
5458         } else if (s->pauth_active) {
5459             tcg_rd = cpu_reg(s, rd);
5460             gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5461         }
5462         break;
5463     default:
5464     do_unallocated:
5465         unallocated_encoding(s);
5466         break;
5467     }
5468 
5469 #undef MAP
5470 }
5471 
5472 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5473                        unsigned int rm, unsigned int rn, unsigned int rd)
5474 {
5475     TCGv_i64 tcg_n, tcg_m, tcg_rd;
5476     tcg_rd = cpu_reg(s, rd);
5477 
5478     if (!sf && is_signed) {
5479         tcg_n = tcg_temp_new_i64();
5480         tcg_m = tcg_temp_new_i64();
5481         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5482         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5483     } else {
5484         tcg_n = read_cpu_reg(s, rn, sf);
5485         tcg_m = read_cpu_reg(s, rm, sf);
5486     }
5487 
5488     if (is_signed) {
5489         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5490     } else {
5491         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5492     }
5493 
5494     if (!sf) { /* zero extend final result */
5495         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5496     }
5497 }
5498 
5499 /* LSLV, LSRV, ASRV, RORV */
5500 static void handle_shift_reg(DisasContext *s,
5501                              enum a64_shift_type shift_type, unsigned int sf,
5502                              unsigned int rm, unsigned int rn, unsigned int rd)
5503 {
5504     TCGv_i64 tcg_shift = tcg_temp_new_i64();
5505     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5506     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5507 
5508     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5509     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5510 }
5511 
5512 /* CRC32[BHWX], CRC32C[BHWX] */
5513 static void handle_crc32(DisasContext *s,
5514                          unsigned int sf, unsigned int sz, bool crc32c,
5515                          unsigned int rm, unsigned int rn, unsigned int rd)
5516 {
5517     TCGv_i64 tcg_acc, tcg_val;
5518     TCGv_i32 tcg_bytes;
5519 
5520     if (!dc_isar_feature(aa64_crc32, s)
5521         || (sf == 1 && sz != 3)
5522         || (sf == 0 && sz == 3)) {
5523         unallocated_encoding(s);
5524         return;
5525     }
5526 
5527     if (sz == 3) {
5528         tcg_val = cpu_reg(s, rm);
5529     } else {
5530         uint64_t mask;
5531         switch (sz) {
5532         case 0:
5533             mask = 0xFF;
5534             break;
5535         case 1:
5536             mask = 0xFFFF;
5537             break;
5538         case 2:
5539             mask = 0xFFFFFFFF;
5540             break;
5541         default:
5542             g_assert_not_reached();
5543         }
5544         tcg_val = tcg_temp_new_i64();
5545         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5546     }
5547 
5548     tcg_acc = cpu_reg(s, rn);
5549     tcg_bytes = tcg_constant_i32(1 << sz);
5550 
5551     if (crc32c) {
5552         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5553     } else {
5554         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5555     }
5556 }
5557 
5558 /* Data-processing (2 source)
5559  *   31   30  29 28             21 20  16 15    10 9    5 4    0
5560  * +----+---+---+-----------------+------+--------+------+------+
5561  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
5562  * +----+---+---+-----------------+------+--------+------+------+
5563  */
5564 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5565 {
5566     unsigned int sf, rm, opcode, rn, rd, setflag;
5567     sf = extract32(insn, 31, 1);
5568     setflag = extract32(insn, 29, 1);
5569     rm = extract32(insn, 16, 5);
5570     opcode = extract32(insn, 10, 6);
5571     rn = extract32(insn, 5, 5);
5572     rd = extract32(insn, 0, 5);
5573 
5574     if (setflag && opcode != 0) {
5575         unallocated_encoding(s);
5576         return;
5577     }
5578 
5579     switch (opcode) {
5580     case 0: /* SUBP(S) */
5581         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5582             goto do_unallocated;
5583         } else {
5584             TCGv_i64 tcg_n, tcg_m, tcg_d;
5585 
5586             tcg_n = read_cpu_reg_sp(s, rn, true);
5587             tcg_m = read_cpu_reg_sp(s, rm, true);
5588             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5589             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5590             tcg_d = cpu_reg(s, rd);
5591 
5592             if (setflag) {
5593                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5594             } else {
5595                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5596             }
5597         }
5598         break;
5599     case 2: /* UDIV */
5600         handle_div(s, false, sf, rm, rn, rd);
5601         break;
5602     case 3: /* SDIV */
5603         handle_div(s, true, sf, rm, rn, rd);
5604         break;
5605     case 4: /* IRG */
5606         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5607             goto do_unallocated;
5608         }
5609         if (s->ata) {
5610             gen_helper_irg(cpu_reg_sp(s, rd), cpu_env,
5611                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
5612         } else {
5613             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5614                                              cpu_reg_sp(s, rn));
5615         }
5616         break;
5617     case 5: /* GMI */
5618         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5619             goto do_unallocated;
5620         } else {
5621             TCGv_i64 t = tcg_temp_new_i64();
5622 
5623             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
5624             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
5625             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
5626         }
5627         break;
5628     case 8: /* LSLV */
5629         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5630         break;
5631     case 9: /* LSRV */
5632         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5633         break;
5634     case 10: /* ASRV */
5635         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5636         break;
5637     case 11: /* RORV */
5638         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5639         break;
5640     case 12: /* PACGA */
5641         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5642             goto do_unallocated;
5643         }
5644         gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5645                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
5646         break;
5647     case 16:
5648     case 17:
5649     case 18:
5650     case 19:
5651     case 20:
5652     case 21:
5653     case 22:
5654     case 23: /* CRC32 */
5655     {
5656         int sz = extract32(opcode, 0, 2);
5657         bool crc32c = extract32(opcode, 2, 1);
5658         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5659         break;
5660     }
5661     default:
5662     do_unallocated:
5663         unallocated_encoding(s);
5664         break;
5665     }
5666 }
5667 
5668 /*
5669  * Data processing - register
5670  *  31  30 29  28      25    21  20  16      10         0
5671  * +--+---+--+---+-------+-----+-------+-------+---------+
5672  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
5673  * +--+---+--+---+-------+-----+-------+-------+---------+
5674  */
5675 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5676 {
5677     int op0 = extract32(insn, 30, 1);
5678     int op1 = extract32(insn, 28, 1);
5679     int op2 = extract32(insn, 21, 4);
5680     int op3 = extract32(insn, 10, 6);
5681 
5682     if (!op1) {
5683         if (op2 & 8) {
5684             if (op2 & 1) {
5685                 /* Add/sub (extended register) */
5686                 disas_add_sub_ext_reg(s, insn);
5687             } else {
5688                 /* Add/sub (shifted register) */
5689                 disas_add_sub_reg(s, insn);
5690             }
5691         } else {
5692             /* Logical (shifted register) */
5693             disas_logic_reg(s, insn);
5694         }
5695         return;
5696     }
5697 
5698     switch (op2) {
5699     case 0x0:
5700         switch (op3) {
5701         case 0x00: /* Add/subtract (with carry) */
5702             disas_adc_sbc(s, insn);
5703             break;
5704 
5705         case 0x01: /* Rotate right into flags */
5706         case 0x21:
5707             disas_rotate_right_into_flags(s, insn);
5708             break;
5709 
5710         case 0x02: /* Evaluate into flags */
5711         case 0x12:
5712         case 0x22:
5713         case 0x32:
5714             disas_evaluate_into_flags(s, insn);
5715             break;
5716 
5717         default:
5718             goto do_unallocated;
5719         }
5720         break;
5721 
5722     case 0x2: /* Conditional compare */
5723         disas_cc(s, insn); /* both imm and reg forms */
5724         break;
5725 
5726     case 0x4: /* Conditional select */
5727         disas_cond_select(s, insn);
5728         break;
5729 
5730     case 0x6: /* Data-processing */
5731         if (op0) {    /* (1 source) */
5732             disas_data_proc_1src(s, insn);
5733         } else {      /* (2 source) */
5734             disas_data_proc_2src(s, insn);
5735         }
5736         break;
5737     case 0x8 ... 0xf: /* (3 source) */
5738         disas_data_proc_3src(s, insn);
5739         break;
5740 
5741     default:
5742     do_unallocated:
5743         unallocated_encoding(s);
5744         break;
5745     }
5746 }
5747 
5748 static void handle_fp_compare(DisasContext *s, int size,
5749                               unsigned int rn, unsigned int rm,
5750                               bool cmp_with_zero, bool signal_all_nans)
5751 {
5752     TCGv_i64 tcg_flags = tcg_temp_new_i64();
5753     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
5754 
5755     if (size == MO_64) {
5756         TCGv_i64 tcg_vn, tcg_vm;
5757 
5758         tcg_vn = read_fp_dreg(s, rn);
5759         if (cmp_with_zero) {
5760             tcg_vm = tcg_constant_i64(0);
5761         } else {
5762             tcg_vm = read_fp_dreg(s, rm);
5763         }
5764         if (signal_all_nans) {
5765             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5766         } else {
5767             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5768         }
5769     } else {
5770         TCGv_i32 tcg_vn = tcg_temp_new_i32();
5771         TCGv_i32 tcg_vm = tcg_temp_new_i32();
5772 
5773         read_vec_element_i32(s, tcg_vn, rn, 0, size);
5774         if (cmp_with_zero) {
5775             tcg_gen_movi_i32(tcg_vm, 0);
5776         } else {
5777             read_vec_element_i32(s, tcg_vm, rm, 0, size);
5778         }
5779 
5780         switch (size) {
5781         case MO_32:
5782             if (signal_all_nans) {
5783                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5784             } else {
5785                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5786             }
5787             break;
5788         case MO_16:
5789             if (signal_all_nans) {
5790                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5791             } else {
5792                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5793             }
5794             break;
5795         default:
5796             g_assert_not_reached();
5797         }
5798     }
5799 
5800     gen_set_nzcv(tcg_flags);
5801 }
5802 
5803 /* Floating point compare
5804  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
5805  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5806  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
5807  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5808  */
5809 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5810 {
5811     unsigned int mos, type, rm, op, rn, opc, op2r;
5812     int size;
5813 
5814     mos = extract32(insn, 29, 3);
5815     type = extract32(insn, 22, 2);
5816     rm = extract32(insn, 16, 5);
5817     op = extract32(insn, 14, 2);
5818     rn = extract32(insn, 5, 5);
5819     opc = extract32(insn, 3, 2);
5820     op2r = extract32(insn, 0, 3);
5821 
5822     if (mos || op || op2r) {
5823         unallocated_encoding(s);
5824         return;
5825     }
5826 
5827     switch (type) {
5828     case 0:
5829         size = MO_32;
5830         break;
5831     case 1:
5832         size = MO_64;
5833         break;
5834     case 3:
5835         size = MO_16;
5836         if (dc_isar_feature(aa64_fp16, s)) {
5837             break;
5838         }
5839         /* fallthru */
5840     default:
5841         unallocated_encoding(s);
5842         return;
5843     }
5844 
5845     if (!fp_access_check(s)) {
5846         return;
5847     }
5848 
5849     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
5850 }
5851 
5852 /* Floating point conditional compare
5853  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
5854  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5855  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
5856  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5857  */
5858 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
5859 {
5860     unsigned int mos, type, rm, cond, rn, op, nzcv;
5861     TCGLabel *label_continue = NULL;
5862     int size;
5863 
5864     mos = extract32(insn, 29, 3);
5865     type = extract32(insn, 22, 2);
5866     rm = extract32(insn, 16, 5);
5867     cond = extract32(insn, 12, 4);
5868     rn = extract32(insn, 5, 5);
5869     op = extract32(insn, 4, 1);
5870     nzcv = extract32(insn, 0, 4);
5871 
5872     if (mos) {
5873         unallocated_encoding(s);
5874         return;
5875     }
5876 
5877     switch (type) {
5878     case 0:
5879         size = MO_32;
5880         break;
5881     case 1:
5882         size = MO_64;
5883         break;
5884     case 3:
5885         size = MO_16;
5886         if (dc_isar_feature(aa64_fp16, s)) {
5887             break;
5888         }
5889         /* fallthru */
5890     default:
5891         unallocated_encoding(s);
5892         return;
5893     }
5894 
5895     if (!fp_access_check(s)) {
5896         return;
5897     }
5898 
5899     if (cond < 0x0e) { /* not always */
5900         TCGLabel *label_match = gen_new_label();
5901         label_continue = gen_new_label();
5902         arm_gen_test_cc(cond, label_match);
5903         /* nomatch: */
5904         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
5905         tcg_gen_br(label_continue);
5906         gen_set_label(label_match);
5907     }
5908 
5909     handle_fp_compare(s, size, rn, rm, false, op);
5910 
5911     if (cond < 0x0e) {
5912         gen_set_label(label_continue);
5913     }
5914 }
5915 
5916 /* Floating point conditional select
5917  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
5918  * +---+---+---+-----------+------+---+------+------+-----+------+------+
5919  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
5920  * +---+---+---+-----------+------+---+------+------+-----+------+------+
5921  */
5922 static void disas_fp_csel(DisasContext *s, uint32_t insn)
5923 {
5924     unsigned int mos, type, rm, cond, rn, rd;
5925     TCGv_i64 t_true, t_false;
5926     DisasCompare64 c;
5927     MemOp sz;
5928 
5929     mos = extract32(insn, 29, 3);
5930     type = extract32(insn, 22, 2);
5931     rm = extract32(insn, 16, 5);
5932     cond = extract32(insn, 12, 4);
5933     rn = extract32(insn, 5, 5);
5934     rd = extract32(insn, 0, 5);
5935 
5936     if (mos) {
5937         unallocated_encoding(s);
5938         return;
5939     }
5940 
5941     switch (type) {
5942     case 0:
5943         sz = MO_32;
5944         break;
5945     case 1:
5946         sz = MO_64;
5947         break;
5948     case 3:
5949         sz = MO_16;
5950         if (dc_isar_feature(aa64_fp16, s)) {
5951             break;
5952         }
5953         /* fallthru */
5954     default:
5955         unallocated_encoding(s);
5956         return;
5957     }
5958 
5959     if (!fp_access_check(s)) {
5960         return;
5961     }
5962 
5963     /* Zero extend sreg & hreg inputs to 64 bits now.  */
5964     t_true = tcg_temp_new_i64();
5965     t_false = tcg_temp_new_i64();
5966     read_vec_element(s, t_true, rn, 0, sz);
5967     read_vec_element(s, t_false, rm, 0, sz);
5968 
5969     a64_test_cc(&c, cond);
5970     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
5971                         t_true, t_false);
5972 
5973     /* Note that sregs & hregs write back zeros to the high bits,
5974        and we've already done the zero-extension.  */
5975     write_fp_dreg(s, rd, t_true);
5976 }
5977 
5978 /* Floating-point data-processing (1 source) - half precision */
5979 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
5980 {
5981     TCGv_ptr fpst = NULL;
5982     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
5983     TCGv_i32 tcg_res = tcg_temp_new_i32();
5984 
5985     switch (opcode) {
5986     case 0x0: /* FMOV */
5987         tcg_gen_mov_i32(tcg_res, tcg_op);
5988         break;
5989     case 0x1: /* FABS */
5990         tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
5991         break;
5992     case 0x2: /* FNEG */
5993         tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
5994         break;
5995     case 0x3: /* FSQRT */
5996         fpst = fpstatus_ptr(FPST_FPCR_F16);
5997         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
5998         break;
5999     case 0x8: /* FRINTN */
6000     case 0x9: /* FRINTP */
6001     case 0xa: /* FRINTM */
6002     case 0xb: /* FRINTZ */
6003     case 0xc: /* FRINTA */
6004     {
6005         TCGv_i32 tcg_rmode;
6006 
6007         fpst = fpstatus_ptr(FPST_FPCR_F16);
6008         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
6009         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6010         gen_restore_rmode(tcg_rmode, fpst);
6011         break;
6012     }
6013     case 0xe: /* FRINTX */
6014         fpst = fpstatus_ptr(FPST_FPCR_F16);
6015         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
6016         break;
6017     case 0xf: /* FRINTI */
6018         fpst = fpstatus_ptr(FPST_FPCR_F16);
6019         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6020         break;
6021     default:
6022         g_assert_not_reached();
6023     }
6024 
6025     write_fp_sreg(s, rd, tcg_res);
6026 }
6027 
6028 /* Floating-point data-processing (1 source) - single precision */
6029 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
6030 {
6031     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
6032     TCGv_i32 tcg_op, tcg_res;
6033     TCGv_ptr fpst;
6034     int rmode = -1;
6035 
6036     tcg_op = read_fp_sreg(s, rn);
6037     tcg_res = tcg_temp_new_i32();
6038 
6039     switch (opcode) {
6040     case 0x0: /* FMOV */
6041         tcg_gen_mov_i32(tcg_res, tcg_op);
6042         goto done;
6043     case 0x1: /* FABS */
6044         gen_helper_vfp_abss(tcg_res, tcg_op);
6045         goto done;
6046     case 0x2: /* FNEG */
6047         gen_helper_vfp_negs(tcg_res, tcg_op);
6048         goto done;
6049     case 0x3: /* FSQRT */
6050         gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
6051         goto done;
6052     case 0x6: /* BFCVT */
6053         gen_fpst = gen_helper_bfcvt;
6054         break;
6055     case 0x8: /* FRINTN */
6056     case 0x9: /* FRINTP */
6057     case 0xa: /* FRINTM */
6058     case 0xb: /* FRINTZ */
6059     case 0xc: /* FRINTA */
6060         rmode = opcode & 7;
6061         gen_fpst = gen_helper_rints;
6062         break;
6063     case 0xe: /* FRINTX */
6064         gen_fpst = gen_helper_rints_exact;
6065         break;
6066     case 0xf: /* FRINTI */
6067         gen_fpst = gen_helper_rints;
6068         break;
6069     case 0x10: /* FRINT32Z */
6070         rmode = FPROUNDING_ZERO;
6071         gen_fpst = gen_helper_frint32_s;
6072         break;
6073     case 0x11: /* FRINT32X */
6074         gen_fpst = gen_helper_frint32_s;
6075         break;
6076     case 0x12: /* FRINT64Z */
6077         rmode = FPROUNDING_ZERO;
6078         gen_fpst = gen_helper_frint64_s;
6079         break;
6080     case 0x13: /* FRINT64X */
6081         gen_fpst = gen_helper_frint64_s;
6082         break;
6083     default:
6084         g_assert_not_reached();
6085     }
6086 
6087     fpst = fpstatus_ptr(FPST_FPCR);
6088     if (rmode >= 0) {
6089         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6090         gen_fpst(tcg_res, tcg_op, fpst);
6091         gen_restore_rmode(tcg_rmode, fpst);
6092     } else {
6093         gen_fpst(tcg_res, tcg_op, fpst);
6094     }
6095 
6096  done:
6097     write_fp_sreg(s, rd, tcg_res);
6098 }
6099 
6100 /* Floating-point data-processing (1 source) - double precision */
6101 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6102 {
6103     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6104     TCGv_i64 tcg_op, tcg_res;
6105     TCGv_ptr fpst;
6106     int rmode = -1;
6107 
6108     switch (opcode) {
6109     case 0x0: /* FMOV */
6110         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6111         return;
6112     }
6113 
6114     tcg_op = read_fp_dreg(s, rn);
6115     tcg_res = tcg_temp_new_i64();
6116 
6117     switch (opcode) {
6118     case 0x1: /* FABS */
6119         gen_helper_vfp_absd(tcg_res, tcg_op);
6120         goto done;
6121     case 0x2: /* FNEG */
6122         gen_helper_vfp_negd(tcg_res, tcg_op);
6123         goto done;
6124     case 0x3: /* FSQRT */
6125         gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
6126         goto done;
6127     case 0x8: /* FRINTN */
6128     case 0x9: /* FRINTP */
6129     case 0xa: /* FRINTM */
6130     case 0xb: /* FRINTZ */
6131     case 0xc: /* FRINTA */
6132         rmode = opcode & 7;
6133         gen_fpst = gen_helper_rintd;
6134         break;
6135     case 0xe: /* FRINTX */
6136         gen_fpst = gen_helper_rintd_exact;
6137         break;
6138     case 0xf: /* FRINTI */
6139         gen_fpst = gen_helper_rintd;
6140         break;
6141     case 0x10: /* FRINT32Z */
6142         rmode = FPROUNDING_ZERO;
6143         gen_fpst = gen_helper_frint32_d;
6144         break;
6145     case 0x11: /* FRINT32X */
6146         gen_fpst = gen_helper_frint32_d;
6147         break;
6148     case 0x12: /* FRINT64Z */
6149         rmode = FPROUNDING_ZERO;
6150         gen_fpst = gen_helper_frint64_d;
6151         break;
6152     case 0x13: /* FRINT64X */
6153         gen_fpst = gen_helper_frint64_d;
6154         break;
6155     default:
6156         g_assert_not_reached();
6157     }
6158 
6159     fpst = fpstatus_ptr(FPST_FPCR);
6160     if (rmode >= 0) {
6161         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6162         gen_fpst(tcg_res, tcg_op, fpst);
6163         gen_restore_rmode(tcg_rmode, fpst);
6164     } else {
6165         gen_fpst(tcg_res, tcg_op, fpst);
6166     }
6167 
6168  done:
6169     write_fp_dreg(s, rd, tcg_res);
6170 }
6171 
6172 static void handle_fp_fcvt(DisasContext *s, int opcode,
6173                            int rd, int rn, int dtype, int ntype)
6174 {
6175     switch (ntype) {
6176     case 0x0:
6177     {
6178         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6179         if (dtype == 1) {
6180             /* Single to double */
6181             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6182             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
6183             write_fp_dreg(s, rd, tcg_rd);
6184         } else {
6185             /* Single to half */
6186             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6187             TCGv_i32 ahp = get_ahp_flag();
6188             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6189 
6190             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6191             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6192             write_fp_sreg(s, rd, tcg_rd);
6193         }
6194         break;
6195     }
6196     case 0x1:
6197     {
6198         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6199         TCGv_i32 tcg_rd = tcg_temp_new_i32();
6200         if (dtype == 0) {
6201             /* Double to single */
6202             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
6203         } else {
6204             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6205             TCGv_i32 ahp = get_ahp_flag();
6206             /* Double to half */
6207             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6208             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6209         }
6210         write_fp_sreg(s, rd, tcg_rd);
6211         break;
6212     }
6213     case 0x3:
6214     {
6215         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6216         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6217         TCGv_i32 tcg_ahp = get_ahp_flag();
6218         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6219         if (dtype == 0) {
6220             /* Half to single */
6221             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6222             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6223             write_fp_sreg(s, rd, tcg_rd);
6224         } else {
6225             /* Half to double */
6226             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6227             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6228             write_fp_dreg(s, rd, tcg_rd);
6229         }
6230         break;
6231     }
6232     default:
6233         g_assert_not_reached();
6234     }
6235 }
6236 
6237 /* Floating point data-processing (1 source)
6238  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
6239  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6240  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
6241  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6242  */
6243 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6244 {
6245     int mos = extract32(insn, 29, 3);
6246     int type = extract32(insn, 22, 2);
6247     int opcode = extract32(insn, 15, 6);
6248     int rn = extract32(insn, 5, 5);
6249     int rd = extract32(insn, 0, 5);
6250 
6251     if (mos) {
6252         goto do_unallocated;
6253     }
6254 
6255     switch (opcode) {
6256     case 0x4: case 0x5: case 0x7:
6257     {
6258         /* FCVT between half, single and double precision */
6259         int dtype = extract32(opcode, 0, 2);
6260         if (type == 2 || dtype == type) {
6261             goto do_unallocated;
6262         }
6263         if (!fp_access_check(s)) {
6264             return;
6265         }
6266 
6267         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6268         break;
6269     }
6270 
6271     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6272         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6273             goto do_unallocated;
6274         }
6275         /* fall through */
6276     case 0x0 ... 0x3:
6277     case 0x8 ... 0xc:
6278     case 0xe ... 0xf:
6279         /* 32-to-32 and 64-to-64 ops */
6280         switch (type) {
6281         case 0:
6282             if (!fp_access_check(s)) {
6283                 return;
6284             }
6285             handle_fp_1src_single(s, opcode, rd, rn);
6286             break;
6287         case 1:
6288             if (!fp_access_check(s)) {
6289                 return;
6290             }
6291             handle_fp_1src_double(s, opcode, rd, rn);
6292             break;
6293         case 3:
6294             if (!dc_isar_feature(aa64_fp16, s)) {
6295                 goto do_unallocated;
6296             }
6297 
6298             if (!fp_access_check(s)) {
6299                 return;
6300             }
6301             handle_fp_1src_half(s, opcode, rd, rn);
6302             break;
6303         default:
6304             goto do_unallocated;
6305         }
6306         break;
6307 
6308     case 0x6:
6309         switch (type) {
6310         case 1: /* BFCVT */
6311             if (!dc_isar_feature(aa64_bf16, s)) {
6312                 goto do_unallocated;
6313             }
6314             if (!fp_access_check(s)) {
6315                 return;
6316             }
6317             handle_fp_1src_single(s, opcode, rd, rn);
6318             break;
6319         default:
6320             goto do_unallocated;
6321         }
6322         break;
6323 
6324     default:
6325     do_unallocated:
6326         unallocated_encoding(s);
6327         break;
6328     }
6329 }
6330 
6331 /* Floating-point data-processing (2 source) - single precision */
6332 static void handle_fp_2src_single(DisasContext *s, int opcode,
6333                                   int rd, int rn, int rm)
6334 {
6335     TCGv_i32 tcg_op1;
6336     TCGv_i32 tcg_op2;
6337     TCGv_i32 tcg_res;
6338     TCGv_ptr fpst;
6339 
6340     tcg_res = tcg_temp_new_i32();
6341     fpst = fpstatus_ptr(FPST_FPCR);
6342     tcg_op1 = read_fp_sreg(s, rn);
6343     tcg_op2 = read_fp_sreg(s, rm);
6344 
6345     switch (opcode) {
6346     case 0x0: /* FMUL */
6347         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6348         break;
6349     case 0x1: /* FDIV */
6350         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6351         break;
6352     case 0x2: /* FADD */
6353         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6354         break;
6355     case 0x3: /* FSUB */
6356         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6357         break;
6358     case 0x4: /* FMAX */
6359         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6360         break;
6361     case 0x5: /* FMIN */
6362         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6363         break;
6364     case 0x6: /* FMAXNM */
6365         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6366         break;
6367     case 0x7: /* FMINNM */
6368         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6369         break;
6370     case 0x8: /* FNMUL */
6371         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6372         gen_helper_vfp_negs(tcg_res, tcg_res);
6373         break;
6374     }
6375 
6376     write_fp_sreg(s, rd, tcg_res);
6377 }
6378 
6379 /* Floating-point data-processing (2 source) - double precision */
6380 static void handle_fp_2src_double(DisasContext *s, int opcode,
6381                                   int rd, int rn, int rm)
6382 {
6383     TCGv_i64 tcg_op1;
6384     TCGv_i64 tcg_op2;
6385     TCGv_i64 tcg_res;
6386     TCGv_ptr fpst;
6387 
6388     tcg_res = tcg_temp_new_i64();
6389     fpst = fpstatus_ptr(FPST_FPCR);
6390     tcg_op1 = read_fp_dreg(s, rn);
6391     tcg_op2 = read_fp_dreg(s, rm);
6392 
6393     switch (opcode) {
6394     case 0x0: /* FMUL */
6395         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6396         break;
6397     case 0x1: /* FDIV */
6398         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6399         break;
6400     case 0x2: /* FADD */
6401         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6402         break;
6403     case 0x3: /* FSUB */
6404         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6405         break;
6406     case 0x4: /* FMAX */
6407         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6408         break;
6409     case 0x5: /* FMIN */
6410         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6411         break;
6412     case 0x6: /* FMAXNM */
6413         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6414         break;
6415     case 0x7: /* FMINNM */
6416         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6417         break;
6418     case 0x8: /* FNMUL */
6419         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6420         gen_helper_vfp_negd(tcg_res, tcg_res);
6421         break;
6422     }
6423 
6424     write_fp_dreg(s, rd, tcg_res);
6425 }
6426 
6427 /* Floating-point data-processing (2 source) - half precision */
6428 static void handle_fp_2src_half(DisasContext *s, int opcode,
6429                                 int rd, int rn, int rm)
6430 {
6431     TCGv_i32 tcg_op1;
6432     TCGv_i32 tcg_op2;
6433     TCGv_i32 tcg_res;
6434     TCGv_ptr fpst;
6435 
6436     tcg_res = tcg_temp_new_i32();
6437     fpst = fpstatus_ptr(FPST_FPCR_F16);
6438     tcg_op1 = read_fp_hreg(s, rn);
6439     tcg_op2 = read_fp_hreg(s, rm);
6440 
6441     switch (opcode) {
6442     case 0x0: /* FMUL */
6443         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6444         break;
6445     case 0x1: /* FDIV */
6446         gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6447         break;
6448     case 0x2: /* FADD */
6449         gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6450         break;
6451     case 0x3: /* FSUB */
6452         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6453         break;
6454     case 0x4: /* FMAX */
6455         gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6456         break;
6457     case 0x5: /* FMIN */
6458         gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6459         break;
6460     case 0x6: /* FMAXNM */
6461         gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6462         break;
6463     case 0x7: /* FMINNM */
6464         gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6465         break;
6466     case 0x8: /* FNMUL */
6467         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6468         tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6469         break;
6470     default:
6471         g_assert_not_reached();
6472     }
6473 
6474     write_fp_sreg(s, rd, tcg_res);
6475 }
6476 
6477 /* Floating point data-processing (2 source)
6478  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
6479  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6480  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
6481  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6482  */
6483 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6484 {
6485     int mos = extract32(insn, 29, 3);
6486     int type = extract32(insn, 22, 2);
6487     int rd = extract32(insn, 0, 5);
6488     int rn = extract32(insn, 5, 5);
6489     int rm = extract32(insn, 16, 5);
6490     int opcode = extract32(insn, 12, 4);
6491 
6492     if (opcode > 8 || mos) {
6493         unallocated_encoding(s);
6494         return;
6495     }
6496 
6497     switch (type) {
6498     case 0:
6499         if (!fp_access_check(s)) {
6500             return;
6501         }
6502         handle_fp_2src_single(s, opcode, rd, rn, rm);
6503         break;
6504     case 1:
6505         if (!fp_access_check(s)) {
6506             return;
6507         }
6508         handle_fp_2src_double(s, opcode, rd, rn, rm);
6509         break;
6510     case 3:
6511         if (!dc_isar_feature(aa64_fp16, s)) {
6512             unallocated_encoding(s);
6513             return;
6514         }
6515         if (!fp_access_check(s)) {
6516             return;
6517         }
6518         handle_fp_2src_half(s, opcode, rd, rn, rm);
6519         break;
6520     default:
6521         unallocated_encoding(s);
6522     }
6523 }
6524 
6525 /* Floating-point data-processing (3 source) - single precision */
6526 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6527                                   int rd, int rn, int rm, int ra)
6528 {
6529     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6530     TCGv_i32 tcg_res = tcg_temp_new_i32();
6531     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6532 
6533     tcg_op1 = read_fp_sreg(s, rn);
6534     tcg_op2 = read_fp_sreg(s, rm);
6535     tcg_op3 = read_fp_sreg(s, ra);
6536 
6537     /* These are fused multiply-add, and must be done as one
6538      * floating point operation with no rounding between the
6539      * multiplication and addition steps.
6540      * NB that doing the negations here as separate steps is
6541      * correct : an input NaN should come out with its sign bit
6542      * flipped if it is a negated-input.
6543      */
6544     if (o1 == true) {
6545         gen_helper_vfp_negs(tcg_op3, tcg_op3);
6546     }
6547 
6548     if (o0 != o1) {
6549         gen_helper_vfp_negs(tcg_op1, tcg_op1);
6550     }
6551 
6552     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6553 
6554     write_fp_sreg(s, rd, tcg_res);
6555 }
6556 
6557 /* Floating-point data-processing (3 source) - double precision */
6558 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6559                                   int rd, int rn, int rm, int ra)
6560 {
6561     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6562     TCGv_i64 tcg_res = tcg_temp_new_i64();
6563     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6564 
6565     tcg_op1 = read_fp_dreg(s, rn);
6566     tcg_op2 = read_fp_dreg(s, rm);
6567     tcg_op3 = read_fp_dreg(s, ra);
6568 
6569     /* These are fused multiply-add, and must be done as one
6570      * floating point operation with no rounding between the
6571      * multiplication and addition steps.
6572      * NB that doing the negations here as separate steps is
6573      * correct : an input NaN should come out with its sign bit
6574      * flipped if it is a negated-input.
6575      */
6576     if (o1 == true) {
6577         gen_helper_vfp_negd(tcg_op3, tcg_op3);
6578     }
6579 
6580     if (o0 != o1) {
6581         gen_helper_vfp_negd(tcg_op1, tcg_op1);
6582     }
6583 
6584     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6585 
6586     write_fp_dreg(s, rd, tcg_res);
6587 }
6588 
6589 /* Floating-point data-processing (3 source) - half precision */
6590 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6591                                 int rd, int rn, int rm, int ra)
6592 {
6593     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6594     TCGv_i32 tcg_res = tcg_temp_new_i32();
6595     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6596 
6597     tcg_op1 = read_fp_hreg(s, rn);
6598     tcg_op2 = read_fp_hreg(s, rm);
6599     tcg_op3 = read_fp_hreg(s, ra);
6600 
6601     /* These are fused multiply-add, and must be done as one
6602      * floating point operation with no rounding between the
6603      * multiplication and addition steps.
6604      * NB that doing the negations here as separate steps is
6605      * correct : an input NaN should come out with its sign bit
6606      * flipped if it is a negated-input.
6607      */
6608     if (o1 == true) {
6609         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6610     }
6611 
6612     if (o0 != o1) {
6613         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6614     }
6615 
6616     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6617 
6618     write_fp_sreg(s, rd, tcg_res);
6619 }
6620 
6621 /* Floating point data-processing (3 source)
6622  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
6623  * +---+---+---+-----------+------+----+------+----+------+------+------+
6624  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6625  * +---+---+---+-----------+------+----+------+----+------+------+------+
6626  */
6627 static void disas_fp_3src(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 ra = extract32(insn, 10, 5);
6634     int rm = extract32(insn, 16, 5);
6635     bool o0 = extract32(insn, 15, 1);
6636     bool o1 = extract32(insn, 21, 1);
6637 
6638     if (mos) {
6639         unallocated_encoding(s);
6640         return;
6641     }
6642 
6643     switch (type) {
6644     case 0:
6645         if (!fp_access_check(s)) {
6646             return;
6647         }
6648         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6649         break;
6650     case 1:
6651         if (!fp_access_check(s)) {
6652             return;
6653         }
6654         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6655         break;
6656     case 3:
6657         if (!dc_isar_feature(aa64_fp16, s)) {
6658             unallocated_encoding(s);
6659             return;
6660         }
6661         if (!fp_access_check(s)) {
6662             return;
6663         }
6664         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6665         break;
6666     default:
6667         unallocated_encoding(s);
6668     }
6669 }
6670 
6671 /* Floating point immediate
6672  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
6673  * +---+---+---+-----------+------+---+------------+-------+------+------+
6674  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
6675  * +---+---+---+-----------+------+---+------------+-------+------+------+
6676  */
6677 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6678 {
6679     int rd = extract32(insn, 0, 5);
6680     int imm5 = extract32(insn, 5, 5);
6681     int imm8 = extract32(insn, 13, 8);
6682     int type = extract32(insn, 22, 2);
6683     int mos = extract32(insn, 29, 3);
6684     uint64_t imm;
6685     MemOp sz;
6686 
6687     if (mos || imm5) {
6688         unallocated_encoding(s);
6689         return;
6690     }
6691 
6692     switch (type) {
6693     case 0:
6694         sz = MO_32;
6695         break;
6696     case 1:
6697         sz = MO_64;
6698         break;
6699     case 3:
6700         sz = MO_16;
6701         if (dc_isar_feature(aa64_fp16, s)) {
6702             break;
6703         }
6704         /* fallthru */
6705     default:
6706         unallocated_encoding(s);
6707         return;
6708     }
6709 
6710     if (!fp_access_check(s)) {
6711         return;
6712     }
6713 
6714     imm = vfp_expand_imm(sz, imm8);
6715     write_fp_dreg(s, rd, tcg_constant_i64(imm));
6716 }
6717 
6718 /* Handle floating point <=> fixed point conversions. Note that we can
6719  * also deal with fp <=> integer conversions as a special case (scale == 64)
6720  * OPTME: consider handling that special case specially or at least skipping
6721  * the call to scalbn in the helpers for zero shifts.
6722  */
6723 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6724                            bool itof, int rmode, int scale, int sf, int type)
6725 {
6726     bool is_signed = !(opcode & 1);
6727     TCGv_ptr tcg_fpstatus;
6728     TCGv_i32 tcg_shift, tcg_single;
6729     TCGv_i64 tcg_double;
6730 
6731     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
6732 
6733     tcg_shift = tcg_constant_i32(64 - scale);
6734 
6735     if (itof) {
6736         TCGv_i64 tcg_int = cpu_reg(s, rn);
6737         if (!sf) {
6738             TCGv_i64 tcg_extend = tcg_temp_new_i64();
6739 
6740             if (is_signed) {
6741                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6742             } else {
6743                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6744             }
6745 
6746             tcg_int = tcg_extend;
6747         }
6748 
6749         switch (type) {
6750         case 1: /* float64 */
6751             tcg_double = tcg_temp_new_i64();
6752             if (is_signed) {
6753                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6754                                      tcg_shift, tcg_fpstatus);
6755             } else {
6756                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6757                                      tcg_shift, tcg_fpstatus);
6758             }
6759             write_fp_dreg(s, rd, tcg_double);
6760             break;
6761 
6762         case 0: /* float32 */
6763             tcg_single = tcg_temp_new_i32();
6764             if (is_signed) {
6765                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6766                                      tcg_shift, tcg_fpstatus);
6767             } else {
6768                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6769                                      tcg_shift, tcg_fpstatus);
6770             }
6771             write_fp_sreg(s, rd, tcg_single);
6772             break;
6773 
6774         case 3: /* float16 */
6775             tcg_single = tcg_temp_new_i32();
6776             if (is_signed) {
6777                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6778                                      tcg_shift, tcg_fpstatus);
6779             } else {
6780                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6781                                      tcg_shift, tcg_fpstatus);
6782             }
6783             write_fp_sreg(s, rd, tcg_single);
6784             break;
6785 
6786         default:
6787             g_assert_not_reached();
6788         }
6789     } else {
6790         TCGv_i64 tcg_int = cpu_reg(s, rd);
6791         TCGv_i32 tcg_rmode;
6792 
6793         if (extract32(opcode, 2, 1)) {
6794             /* There are too many rounding modes to all fit into rmode,
6795              * so FCVTA[US] is a special case.
6796              */
6797             rmode = FPROUNDING_TIEAWAY;
6798         }
6799 
6800         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
6801 
6802         switch (type) {
6803         case 1: /* float64 */
6804             tcg_double = read_fp_dreg(s, rn);
6805             if (is_signed) {
6806                 if (!sf) {
6807                     gen_helper_vfp_tosld(tcg_int, tcg_double,
6808                                          tcg_shift, tcg_fpstatus);
6809                 } else {
6810                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
6811                                          tcg_shift, tcg_fpstatus);
6812                 }
6813             } else {
6814                 if (!sf) {
6815                     gen_helper_vfp_tould(tcg_int, tcg_double,
6816                                          tcg_shift, tcg_fpstatus);
6817                 } else {
6818                     gen_helper_vfp_touqd(tcg_int, tcg_double,
6819                                          tcg_shift, tcg_fpstatus);
6820                 }
6821             }
6822             if (!sf) {
6823                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6824             }
6825             break;
6826 
6827         case 0: /* float32 */
6828             tcg_single = read_fp_sreg(s, rn);
6829             if (sf) {
6830                 if (is_signed) {
6831                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
6832                                          tcg_shift, tcg_fpstatus);
6833                 } else {
6834                     gen_helper_vfp_touqs(tcg_int, tcg_single,
6835                                          tcg_shift, tcg_fpstatus);
6836                 }
6837             } else {
6838                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6839                 if (is_signed) {
6840                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
6841                                          tcg_shift, tcg_fpstatus);
6842                 } else {
6843                     gen_helper_vfp_touls(tcg_dest, tcg_single,
6844                                          tcg_shift, tcg_fpstatus);
6845                 }
6846                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6847             }
6848             break;
6849 
6850         case 3: /* float16 */
6851             tcg_single = read_fp_sreg(s, rn);
6852             if (sf) {
6853                 if (is_signed) {
6854                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
6855                                          tcg_shift, tcg_fpstatus);
6856                 } else {
6857                     gen_helper_vfp_touqh(tcg_int, tcg_single,
6858                                          tcg_shift, tcg_fpstatus);
6859                 }
6860             } else {
6861                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6862                 if (is_signed) {
6863                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
6864                                          tcg_shift, tcg_fpstatus);
6865                 } else {
6866                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
6867                                          tcg_shift, tcg_fpstatus);
6868                 }
6869                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6870             }
6871             break;
6872 
6873         default:
6874             g_assert_not_reached();
6875         }
6876 
6877         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
6878     }
6879 }
6880 
6881 /* Floating point <-> fixed point conversions
6882  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
6883  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6884  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
6885  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6886  */
6887 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
6888 {
6889     int rd = extract32(insn, 0, 5);
6890     int rn = extract32(insn, 5, 5);
6891     int scale = extract32(insn, 10, 6);
6892     int opcode = extract32(insn, 16, 3);
6893     int rmode = extract32(insn, 19, 2);
6894     int type = extract32(insn, 22, 2);
6895     bool sbit = extract32(insn, 29, 1);
6896     bool sf = extract32(insn, 31, 1);
6897     bool itof;
6898 
6899     if (sbit || (!sf && scale < 32)) {
6900         unallocated_encoding(s);
6901         return;
6902     }
6903 
6904     switch (type) {
6905     case 0: /* float32 */
6906     case 1: /* float64 */
6907         break;
6908     case 3: /* float16 */
6909         if (dc_isar_feature(aa64_fp16, s)) {
6910             break;
6911         }
6912         /* fallthru */
6913     default:
6914         unallocated_encoding(s);
6915         return;
6916     }
6917 
6918     switch ((rmode << 3) | opcode) {
6919     case 0x2: /* SCVTF */
6920     case 0x3: /* UCVTF */
6921         itof = true;
6922         break;
6923     case 0x18: /* FCVTZS */
6924     case 0x19: /* FCVTZU */
6925         itof = false;
6926         break;
6927     default:
6928         unallocated_encoding(s);
6929         return;
6930     }
6931 
6932     if (!fp_access_check(s)) {
6933         return;
6934     }
6935 
6936     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
6937 }
6938 
6939 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
6940 {
6941     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
6942      * without conversion.
6943      */
6944 
6945     if (itof) {
6946         TCGv_i64 tcg_rn = cpu_reg(s, rn);
6947         TCGv_i64 tmp;
6948 
6949         switch (type) {
6950         case 0:
6951             /* 32 bit */
6952             tmp = tcg_temp_new_i64();
6953             tcg_gen_ext32u_i64(tmp, tcg_rn);
6954             write_fp_dreg(s, rd, tmp);
6955             break;
6956         case 1:
6957             /* 64 bit */
6958             write_fp_dreg(s, rd, tcg_rn);
6959             break;
6960         case 2:
6961             /* 64 bit to top half. */
6962             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
6963             clear_vec_high(s, true, rd);
6964             break;
6965         case 3:
6966             /* 16 bit */
6967             tmp = tcg_temp_new_i64();
6968             tcg_gen_ext16u_i64(tmp, tcg_rn);
6969             write_fp_dreg(s, rd, tmp);
6970             break;
6971         default:
6972             g_assert_not_reached();
6973         }
6974     } else {
6975         TCGv_i64 tcg_rd = cpu_reg(s, rd);
6976 
6977         switch (type) {
6978         case 0:
6979             /* 32 bit */
6980             tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
6981             break;
6982         case 1:
6983             /* 64 bit */
6984             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
6985             break;
6986         case 2:
6987             /* 64 bits from top half */
6988             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
6989             break;
6990         case 3:
6991             /* 16 bit */
6992             tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
6993             break;
6994         default:
6995             g_assert_not_reached();
6996         }
6997     }
6998 }
6999 
7000 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
7001 {
7002     TCGv_i64 t = read_fp_dreg(s, rn);
7003     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
7004 
7005     gen_helper_fjcvtzs(t, t, fpstatus);
7006 
7007     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
7008     tcg_gen_extrh_i64_i32(cpu_ZF, t);
7009     tcg_gen_movi_i32(cpu_CF, 0);
7010     tcg_gen_movi_i32(cpu_NF, 0);
7011     tcg_gen_movi_i32(cpu_VF, 0);
7012 }
7013 
7014 /* Floating point <-> integer conversions
7015  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
7016  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7017  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
7018  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7019  */
7020 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
7021 {
7022     int rd = extract32(insn, 0, 5);
7023     int rn = extract32(insn, 5, 5);
7024     int opcode = extract32(insn, 16, 3);
7025     int rmode = extract32(insn, 19, 2);
7026     int type = extract32(insn, 22, 2);
7027     bool sbit = extract32(insn, 29, 1);
7028     bool sf = extract32(insn, 31, 1);
7029     bool itof = false;
7030 
7031     if (sbit) {
7032         goto do_unallocated;
7033     }
7034 
7035     switch (opcode) {
7036     case 2: /* SCVTF */
7037     case 3: /* UCVTF */
7038         itof = true;
7039         /* fallthru */
7040     case 4: /* FCVTAS */
7041     case 5: /* FCVTAU */
7042         if (rmode != 0) {
7043             goto do_unallocated;
7044         }
7045         /* fallthru */
7046     case 0: /* FCVT[NPMZ]S */
7047     case 1: /* FCVT[NPMZ]U */
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                 goto do_unallocated;
7055             }
7056             break;
7057         default:
7058             goto do_unallocated;
7059         }
7060         if (!fp_access_check(s)) {
7061             return;
7062         }
7063         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
7064         break;
7065 
7066     default:
7067         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
7068         case 0b01100110: /* FMOV half <-> 32-bit int */
7069         case 0b01100111:
7070         case 0b11100110: /* FMOV half <-> 64-bit int */
7071         case 0b11100111:
7072             if (!dc_isar_feature(aa64_fp16, s)) {
7073                 goto do_unallocated;
7074             }
7075             /* fallthru */
7076         case 0b00000110: /* FMOV 32-bit */
7077         case 0b00000111:
7078         case 0b10100110: /* FMOV 64-bit */
7079         case 0b10100111:
7080         case 0b11001110: /* FMOV top half of 128-bit */
7081         case 0b11001111:
7082             if (!fp_access_check(s)) {
7083                 return;
7084             }
7085             itof = opcode & 1;
7086             handle_fmov(s, rd, rn, type, itof);
7087             break;
7088 
7089         case 0b00111110: /* FJCVTZS */
7090             if (!dc_isar_feature(aa64_jscvt, s)) {
7091                 goto do_unallocated;
7092             } else if (fp_access_check(s)) {
7093                 handle_fjcvtzs(s, rd, rn);
7094             }
7095             break;
7096 
7097         default:
7098         do_unallocated:
7099             unallocated_encoding(s);
7100             return;
7101         }
7102         break;
7103     }
7104 }
7105 
7106 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7107  *   31  30  29 28     25 24                          0
7108  * +---+---+---+---------+-----------------------------+
7109  * |   | 0 |   | 1 1 1 1 |                             |
7110  * +---+---+---+---------+-----------------------------+
7111  */
7112 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7113 {
7114     if (extract32(insn, 24, 1)) {
7115         /* Floating point data-processing (3 source) */
7116         disas_fp_3src(s, insn);
7117     } else if (extract32(insn, 21, 1) == 0) {
7118         /* Floating point to fixed point conversions */
7119         disas_fp_fixed_conv(s, insn);
7120     } else {
7121         switch (extract32(insn, 10, 2)) {
7122         case 1:
7123             /* Floating point conditional compare */
7124             disas_fp_ccomp(s, insn);
7125             break;
7126         case 2:
7127             /* Floating point data-processing (2 source) */
7128             disas_fp_2src(s, insn);
7129             break;
7130         case 3:
7131             /* Floating point conditional select */
7132             disas_fp_csel(s, insn);
7133             break;
7134         case 0:
7135             switch (ctz32(extract32(insn, 12, 4))) {
7136             case 0: /* [15:12] == xxx1 */
7137                 /* Floating point immediate */
7138                 disas_fp_imm(s, insn);
7139                 break;
7140             case 1: /* [15:12] == xx10 */
7141                 /* Floating point compare */
7142                 disas_fp_compare(s, insn);
7143                 break;
7144             case 2: /* [15:12] == x100 */
7145                 /* Floating point data-processing (1 source) */
7146                 disas_fp_1src(s, insn);
7147                 break;
7148             case 3: /* [15:12] == 1000 */
7149                 unallocated_encoding(s);
7150                 break;
7151             default: /* [15:12] == 0000 */
7152                 /* Floating point <-> integer conversions */
7153                 disas_fp_int_conv(s, insn);
7154                 break;
7155             }
7156             break;
7157         }
7158     }
7159 }
7160 
7161 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7162                      int pos)
7163 {
7164     /* Extract 64 bits from the middle of two concatenated 64 bit
7165      * vector register slices left:right. The extracted bits start
7166      * at 'pos' bits into the right (least significant) side.
7167      * We return the result in tcg_right, and guarantee not to
7168      * trash tcg_left.
7169      */
7170     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7171     assert(pos > 0 && pos < 64);
7172 
7173     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7174     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7175     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7176 }
7177 
7178 /* EXT
7179  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
7180  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7181  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
7182  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7183  */
7184 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7185 {
7186     int is_q = extract32(insn, 30, 1);
7187     int op2 = extract32(insn, 22, 2);
7188     int imm4 = extract32(insn, 11, 4);
7189     int rm = extract32(insn, 16, 5);
7190     int rn = extract32(insn, 5, 5);
7191     int rd = extract32(insn, 0, 5);
7192     int pos = imm4 << 3;
7193     TCGv_i64 tcg_resl, tcg_resh;
7194 
7195     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7196         unallocated_encoding(s);
7197         return;
7198     }
7199 
7200     if (!fp_access_check(s)) {
7201         return;
7202     }
7203 
7204     tcg_resh = tcg_temp_new_i64();
7205     tcg_resl = tcg_temp_new_i64();
7206 
7207     /* Vd gets bits starting at pos bits into Vm:Vn. This is
7208      * either extracting 128 bits from a 128:128 concatenation, or
7209      * extracting 64 bits from a 64:64 concatenation.
7210      */
7211     if (!is_q) {
7212         read_vec_element(s, tcg_resl, rn, 0, MO_64);
7213         if (pos != 0) {
7214             read_vec_element(s, tcg_resh, rm, 0, MO_64);
7215             do_ext64(s, tcg_resh, tcg_resl, pos);
7216         }
7217     } else {
7218         TCGv_i64 tcg_hh;
7219         typedef struct {
7220             int reg;
7221             int elt;
7222         } EltPosns;
7223         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7224         EltPosns *elt = eltposns;
7225 
7226         if (pos >= 64) {
7227             elt++;
7228             pos -= 64;
7229         }
7230 
7231         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7232         elt++;
7233         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7234         elt++;
7235         if (pos != 0) {
7236             do_ext64(s, tcg_resh, tcg_resl, pos);
7237             tcg_hh = tcg_temp_new_i64();
7238             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7239             do_ext64(s, tcg_hh, tcg_resh, pos);
7240         }
7241     }
7242 
7243     write_vec_element(s, tcg_resl, rd, 0, MO_64);
7244     if (is_q) {
7245         write_vec_element(s, tcg_resh, rd, 1, MO_64);
7246     }
7247     clear_vec_high(s, is_q, rd);
7248 }
7249 
7250 /* TBL/TBX
7251  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
7252  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7253  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
7254  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7255  */
7256 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7257 {
7258     int op2 = extract32(insn, 22, 2);
7259     int is_q = extract32(insn, 30, 1);
7260     int rm = extract32(insn, 16, 5);
7261     int rn = extract32(insn, 5, 5);
7262     int rd = extract32(insn, 0, 5);
7263     int is_tbx = extract32(insn, 12, 1);
7264     int len = (extract32(insn, 13, 2) + 1) * 16;
7265 
7266     if (op2 != 0) {
7267         unallocated_encoding(s);
7268         return;
7269     }
7270 
7271     if (!fp_access_check(s)) {
7272         return;
7273     }
7274 
7275     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7276                        vec_full_reg_offset(s, rm), cpu_env,
7277                        is_q ? 16 : 8, vec_full_reg_size(s),
7278                        (len << 6) | (is_tbx << 5) | rn,
7279                        gen_helper_simd_tblx);
7280 }
7281 
7282 /* ZIP/UZP/TRN
7283  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
7284  * +---+---+-------------+------+---+------+---+------------------+------+
7285  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
7286  * +---+---+-------------+------+---+------+---+------------------+------+
7287  */
7288 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7289 {
7290     int rd = extract32(insn, 0, 5);
7291     int rn = extract32(insn, 5, 5);
7292     int rm = extract32(insn, 16, 5);
7293     int size = extract32(insn, 22, 2);
7294     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7295      * bit 2 indicates 1 vs 2 variant of the insn.
7296      */
7297     int opcode = extract32(insn, 12, 2);
7298     bool part = extract32(insn, 14, 1);
7299     bool is_q = extract32(insn, 30, 1);
7300     int esize = 8 << size;
7301     int i;
7302     int datasize = is_q ? 128 : 64;
7303     int elements = datasize / esize;
7304     TCGv_i64 tcg_res[2], tcg_ele;
7305 
7306     if (opcode == 0 || (size == 3 && !is_q)) {
7307         unallocated_encoding(s);
7308         return;
7309     }
7310 
7311     if (!fp_access_check(s)) {
7312         return;
7313     }
7314 
7315     tcg_res[0] = tcg_temp_new_i64();
7316     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
7317     tcg_ele = tcg_temp_new_i64();
7318 
7319     for (i = 0; i < elements; i++) {
7320         int o, w;
7321 
7322         switch (opcode) {
7323         case 1: /* UZP1/2 */
7324         {
7325             int midpoint = elements / 2;
7326             if (i < midpoint) {
7327                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
7328             } else {
7329                 read_vec_element(s, tcg_ele, rm,
7330                                  2 * (i - midpoint) + part, size);
7331             }
7332             break;
7333         }
7334         case 2: /* TRN1/2 */
7335             if (i & 1) {
7336                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
7337             } else {
7338                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
7339             }
7340             break;
7341         case 3: /* ZIP1/2 */
7342         {
7343             int base = part * elements / 2;
7344             if (i & 1) {
7345                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
7346             } else {
7347                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
7348             }
7349             break;
7350         }
7351         default:
7352             g_assert_not_reached();
7353         }
7354 
7355         w = (i * esize) / 64;
7356         o = (i * esize) % 64;
7357         if (o == 0) {
7358             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
7359         } else {
7360             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
7361             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
7362         }
7363     }
7364 
7365     for (i = 0; i <= is_q; ++i) {
7366         write_vec_element(s, tcg_res[i], rd, i, MO_64);
7367     }
7368     clear_vec_high(s, is_q, rd);
7369 }
7370 
7371 /*
7372  * do_reduction_op helper
7373  *
7374  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7375  * important for correct NaN propagation that we do these
7376  * operations in exactly the order specified by the pseudocode.
7377  *
7378  * This is a recursive function, TCG temps should be freed by the
7379  * calling function once it is done with the values.
7380  */
7381 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7382                                 int esize, int size, int vmap, TCGv_ptr fpst)
7383 {
7384     if (esize == size) {
7385         int element;
7386         MemOp msize = esize == 16 ? MO_16 : MO_32;
7387         TCGv_i32 tcg_elem;
7388 
7389         /* We should have one register left here */
7390         assert(ctpop8(vmap) == 1);
7391         element = ctz32(vmap);
7392         assert(element < 8);
7393 
7394         tcg_elem = tcg_temp_new_i32();
7395         read_vec_element_i32(s, tcg_elem, rn, element, msize);
7396         return tcg_elem;
7397     } else {
7398         int bits = size / 2;
7399         int shift = ctpop8(vmap) / 2;
7400         int vmap_lo = (vmap >> shift) & vmap;
7401         int vmap_hi = (vmap & ~vmap_lo);
7402         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7403 
7404         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7405         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7406         tcg_res = tcg_temp_new_i32();
7407 
7408         switch (fpopcode) {
7409         case 0x0c: /* fmaxnmv half-precision */
7410             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7411             break;
7412         case 0x0f: /* fmaxv half-precision */
7413             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7414             break;
7415         case 0x1c: /* fminnmv half-precision */
7416             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7417             break;
7418         case 0x1f: /* fminv half-precision */
7419             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7420             break;
7421         case 0x2c: /* fmaxnmv */
7422             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7423             break;
7424         case 0x2f: /* fmaxv */
7425             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7426             break;
7427         case 0x3c: /* fminnmv */
7428             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7429             break;
7430         case 0x3f: /* fminv */
7431             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7432             break;
7433         default:
7434             g_assert_not_reached();
7435         }
7436         return tcg_res;
7437     }
7438 }
7439 
7440 /* AdvSIMD across lanes
7441  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7442  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7443  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7444  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7445  */
7446 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7447 {
7448     int rd = extract32(insn, 0, 5);
7449     int rn = extract32(insn, 5, 5);
7450     int size = extract32(insn, 22, 2);
7451     int opcode = extract32(insn, 12, 5);
7452     bool is_q = extract32(insn, 30, 1);
7453     bool is_u = extract32(insn, 29, 1);
7454     bool is_fp = false;
7455     bool is_min = false;
7456     int esize;
7457     int elements;
7458     int i;
7459     TCGv_i64 tcg_res, tcg_elt;
7460 
7461     switch (opcode) {
7462     case 0x1b: /* ADDV */
7463         if (is_u) {
7464             unallocated_encoding(s);
7465             return;
7466         }
7467         /* fall through */
7468     case 0x3: /* SADDLV, UADDLV */
7469     case 0xa: /* SMAXV, UMAXV */
7470     case 0x1a: /* SMINV, UMINV */
7471         if (size == 3 || (size == 2 && !is_q)) {
7472             unallocated_encoding(s);
7473             return;
7474         }
7475         break;
7476     case 0xc: /* FMAXNMV, FMINNMV */
7477     case 0xf: /* FMAXV, FMINV */
7478         /* Bit 1 of size field encodes min vs max and the actual size
7479          * depends on the encoding of the U bit. If not set (and FP16
7480          * enabled) then we do half-precision float instead of single
7481          * precision.
7482          */
7483         is_min = extract32(size, 1, 1);
7484         is_fp = true;
7485         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7486             size = 1;
7487         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7488             unallocated_encoding(s);
7489             return;
7490         } else {
7491             size = 2;
7492         }
7493         break;
7494     default:
7495         unallocated_encoding(s);
7496         return;
7497     }
7498 
7499     if (!fp_access_check(s)) {
7500         return;
7501     }
7502 
7503     esize = 8 << size;
7504     elements = (is_q ? 128 : 64) / esize;
7505 
7506     tcg_res = tcg_temp_new_i64();
7507     tcg_elt = tcg_temp_new_i64();
7508 
7509     /* These instructions operate across all lanes of a vector
7510      * to produce a single result. We can guarantee that a 64
7511      * bit intermediate is sufficient:
7512      *  + for [US]ADDLV the maximum element size is 32 bits, and
7513      *    the result type is 64 bits
7514      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7515      *    same as the element size, which is 32 bits at most
7516      * For the integer operations we can choose to work at 64
7517      * or 32 bits and truncate at the end; for simplicity
7518      * we use 64 bits always. The floating point
7519      * ops do require 32 bit intermediates, though.
7520      */
7521     if (!is_fp) {
7522         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7523 
7524         for (i = 1; i < elements; i++) {
7525             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7526 
7527             switch (opcode) {
7528             case 0x03: /* SADDLV / UADDLV */
7529             case 0x1b: /* ADDV */
7530                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7531                 break;
7532             case 0x0a: /* SMAXV / UMAXV */
7533                 if (is_u) {
7534                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7535                 } else {
7536                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7537                 }
7538                 break;
7539             case 0x1a: /* SMINV / UMINV */
7540                 if (is_u) {
7541                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7542                 } else {
7543                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7544                 }
7545                 break;
7546             default:
7547                 g_assert_not_reached();
7548             }
7549 
7550         }
7551     } else {
7552         /* Floating point vector reduction ops which work across 32
7553          * bit (single) or 16 bit (half-precision) intermediates.
7554          * Note that correct NaN propagation requires that we do these
7555          * operations in exactly the order specified by the pseudocode.
7556          */
7557         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7558         int fpopcode = opcode | is_min << 4 | is_u << 5;
7559         int vmap = (1 << elements) - 1;
7560         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7561                                              (is_q ? 128 : 64), vmap, fpst);
7562         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7563     }
7564 
7565     /* Now truncate the result to the width required for the final output */
7566     if (opcode == 0x03) {
7567         /* SADDLV, UADDLV: result is 2*esize */
7568         size++;
7569     }
7570 
7571     switch (size) {
7572     case 0:
7573         tcg_gen_ext8u_i64(tcg_res, tcg_res);
7574         break;
7575     case 1:
7576         tcg_gen_ext16u_i64(tcg_res, tcg_res);
7577         break;
7578     case 2:
7579         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7580         break;
7581     case 3:
7582         break;
7583     default:
7584         g_assert_not_reached();
7585     }
7586 
7587     write_fp_dreg(s, rd, tcg_res);
7588 }
7589 
7590 /* DUP (Element, Vector)
7591  *
7592  *  31  30   29              21 20    16 15        10  9    5 4    0
7593  * +---+---+-------------------+--------+-------------+------+------+
7594  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7595  * +---+---+-------------------+--------+-------------+------+------+
7596  *
7597  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7598  */
7599 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7600                              int imm5)
7601 {
7602     int size = ctz32(imm5);
7603     int index;
7604 
7605     if (size > 3 || (size == 3 && !is_q)) {
7606         unallocated_encoding(s);
7607         return;
7608     }
7609 
7610     if (!fp_access_check(s)) {
7611         return;
7612     }
7613 
7614     index = imm5 >> (size + 1);
7615     tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7616                          vec_reg_offset(s, rn, index, size),
7617                          is_q ? 16 : 8, vec_full_reg_size(s));
7618 }
7619 
7620 /* DUP (element, scalar)
7621  *  31                   21 20    16 15        10  9    5 4    0
7622  * +-----------------------+--------+-------------+------+------+
7623  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7624  * +-----------------------+--------+-------------+------+------+
7625  */
7626 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7627                               int imm5)
7628 {
7629     int size = ctz32(imm5);
7630     int index;
7631     TCGv_i64 tmp;
7632 
7633     if (size > 3) {
7634         unallocated_encoding(s);
7635         return;
7636     }
7637 
7638     if (!fp_access_check(s)) {
7639         return;
7640     }
7641 
7642     index = imm5 >> (size + 1);
7643 
7644     /* This instruction just extracts the specified element and
7645      * zero-extends it into the bottom of the destination register.
7646      */
7647     tmp = tcg_temp_new_i64();
7648     read_vec_element(s, tmp, rn, index, size);
7649     write_fp_dreg(s, rd, tmp);
7650 }
7651 
7652 /* DUP (General)
7653  *
7654  *  31  30   29              21 20    16 15        10  9    5 4    0
7655  * +---+---+-------------------+--------+-------------+------+------+
7656  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
7657  * +---+---+-------------------+--------+-------------+------+------+
7658  *
7659  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7660  */
7661 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7662                              int imm5)
7663 {
7664     int size = ctz32(imm5);
7665     uint32_t dofs, oprsz, maxsz;
7666 
7667     if (size > 3 || ((size == 3) && !is_q)) {
7668         unallocated_encoding(s);
7669         return;
7670     }
7671 
7672     if (!fp_access_check(s)) {
7673         return;
7674     }
7675 
7676     dofs = vec_full_reg_offset(s, rd);
7677     oprsz = is_q ? 16 : 8;
7678     maxsz = vec_full_reg_size(s);
7679 
7680     tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7681 }
7682 
7683 /* INS (Element)
7684  *
7685  *  31                   21 20    16 15  14    11  10 9    5 4    0
7686  * +-----------------------+--------+------------+---+------+------+
7687  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
7688  * +-----------------------+--------+------------+---+------+------+
7689  *
7690  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7691  * index: encoded in imm5<4:size+1>
7692  */
7693 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7694                              int imm4, int imm5)
7695 {
7696     int size = ctz32(imm5);
7697     int src_index, dst_index;
7698     TCGv_i64 tmp;
7699 
7700     if (size > 3) {
7701         unallocated_encoding(s);
7702         return;
7703     }
7704 
7705     if (!fp_access_check(s)) {
7706         return;
7707     }
7708 
7709     dst_index = extract32(imm5, 1+size, 5);
7710     src_index = extract32(imm4, size, 4);
7711 
7712     tmp = tcg_temp_new_i64();
7713 
7714     read_vec_element(s, tmp, rn, src_index, size);
7715     write_vec_element(s, tmp, rd, dst_index, size);
7716 
7717     /* INS is considered a 128-bit write for SVE. */
7718     clear_vec_high(s, true, rd);
7719 }
7720 
7721 
7722 /* INS (General)
7723  *
7724  *  31                   21 20    16 15        10  9    5 4    0
7725  * +-----------------------+--------+-------------+------+------+
7726  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
7727  * +-----------------------+--------+-------------+------+------+
7728  *
7729  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7730  * index: encoded in imm5<4:size+1>
7731  */
7732 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7733 {
7734     int size = ctz32(imm5);
7735     int idx;
7736 
7737     if (size > 3) {
7738         unallocated_encoding(s);
7739         return;
7740     }
7741 
7742     if (!fp_access_check(s)) {
7743         return;
7744     }
7745 
7746     idx = extract32(imm5, 1 + size, 4 - size);
7747     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7748 
7749     /* INS is considered a 128-bit write for SVE. */
7750     clear_vec_high(s, true, rd);
7751 }
7752 
7753 /*
7754  * UMOV (General)
7755  * SMOV (General)
7756  *
7757  *  31  30   29              21 20    16 15    12   10 9    5 4    0
7758  * +---+---+-------------------+--------+-------------+------+------+
7759  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
7760  * +---+---+-------------------+--------+-------------+------+------+
7761  *
7762  * U: unsigned when set
7763  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7764  */
7765 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7766                                   int rn, int rd, int imm5)
7767 {
7768     int size = ctz32(imm5);
7769     int element;
7770     TCGv_i64 tcg_rd;
7771 
7772     /* Check for UnallocatedEncodings */
7773     if (is_signed) {
7774         if (size > 2 || (size == 2 && !is_q)) {
7775             unallocated_encoding(s);
7776             return;
7777         }
7778     } else {
7779         if (size > 3
7780             || (size < 3 && is_q)
7781             || (size == 3 && !is_q)) {
7782             unallocated_encoding(s);
7783             return;
7784         }
7785     }
7786 
7787     if (!fp_access_check(s)) {
7788         return;
7789     }
7790 
7791     element = extract32(imm5, 1+size, 4);
7792 
7793     tcg_rd = cpu_reg(s, rd);
7794     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7795     if (is_signed && !is_q) {
7796         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7797     }
7798 }
7799 
7800 /* AdvSIMD copy
7801  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
7802  * +---+---+----+-----------------+------+---+------+---+------+------+
7803  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7804  * +---+---+----+-----------------+------+---+------+---+------+------+
7805  */
7806 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7807 {
7808     int rd = extract32(insn, 0, 5);
7809     int rn = extract32(insn, 5, 5);
7810     int imm4 = extract32(insn, 11, 4);
7811     int op = extract32(insn, 29, 1);
7812     int is_q = extract32(insn, 30, 1);
7813     int imm5 = extract32(insn, 16, 5);
7814 
7815     if (op) {
7816         if (is_q) {
7817             /* INS (element) */
7818             handle_simd_inse(s, rd, rn, imm4, imm5);
7819         } else {
7820             unallocated_encoding(s);
7821         }
7822     } else {
7823         switch (imm4) {
7824         case 0:
7825             /* DUP (element - vector) */
7826             handle_simd_dupe(s, is_q, rd, rn, imm5);
7827             break;
7828         case 1:
7829             /* DUP (general) */
7830             handle_simd_dupg(s, is_q, rd, rn, imm5);
7831             break;
7832         case 3:
7833             if (is_q) {
7834                 /* INS (general) */
7835                 handle_simd_insg(s, rd, rn, imm5);
7836             } else {
7837                 unallocated_encoding(s);
7838             }
7839             break;
7840         case 5:
7841         case 7:
7842             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
7843             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
7844             break;
7845         default:
7846             unallocated_encoding(s);
7847             break;
7848         }
7849     }
7850 }
7851 
7852 /* AdvSIMD modified immediate
7853  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
7854  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7855  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
7856  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7857  *
7858  * There are a number of operations that can be carried out here:
7859  *   MOVI - move (shifted) imm into register
7860  *   MVNI - move inverted (shifted) imm into register
7861  *   ORR  - bitwise OR of (shifted) imm with register
7862  *   BIC  - bitwise clear of (shifted) imm with register
7863  * With ARMv8.2 we also have:
7864  *   FMOV half-precision
7865  */
7866 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
7867 {
7868     int rd = extract32(insn, 0, 5);
7869     int cmode = extract32(insn, 12, 4);
7870     int o2 = extract32(insn, 11, 1);
7871     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
7872     bool is_neg = extract32(insn, 29, 1);
7873     bool is_q = extract32(insn, 30, 1);
7874     uint64_t imm = 0;
7875 
7876     if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
7877         /* Check for FMOV (vector, immediate) - half-precision */
7878         if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
7879             unallocated_encoding(s);
7880             return;
7881         }
7882     }
7883 
7884     if (!fp_access_check(s)) {
7885         return;
7886     }
7887 
7888     if (cmode == 15 && o2 && !is_neg) {
7889         /* FMOV (vector, immediate) - half-precision */
7890         imm = vfp_expand_imm(MO_16, abcdefgh);
7891         /* now duplicate across the lanes */
7892         imm = dup_const(MO_16, imm);
7893     } else {
7894         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
7895     }
7896 
7897     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
7898         /* MOVI or MVNI, with MVNI negation handled above.  */
7899         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
7900                              vec_full_reg_size(s), imm);
7901     } else {
7902         /* ORR or BIC, with BIC negation to AND handled above.  */
7903         if (is_neg) {
7904             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
7905         } else {
7906             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
7907         }
7908     }
7909 }
7910 
7911 /* AdvSIMD scalar copy
7912  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
7913  * +-----+----+-----------------+------+---+------+---+------+------+
7914  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7915  * +-----+----+-----------------+------+---+------+---+------+------+
7916  */
7917 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
7918 {
7919     int rd = extract32(insn, 0, 5);
7920     int rn = extract32(insn, 5, 5);
7921     int imm4 = extract32(insn, 11, 4);
7922     int imm5 = extract32(insn, 16, 5);
7923     int op = extract32(insn, 29, 1);
7924 
7925     if (op != 0 || imm4 != 0) {
7926         unallocated_encoding(s);
7927         return;
7928     }
7929 
7930     /* DUP (element, scalar) */
7931     handle_simd_dupes(s, rd, rn, imm5);
7932 }
7933 
7934 /* AdvSIMD scalar pairwise
7935  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7936  * +-----+---+-----------+------+-----------+--------+-----+------+------+
7937  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7938  * +-----+---+-----------+------+-----------+--------+-----+------+------+
7939  */
7940 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
7941 {
7942     int u = extract32(insn, 29, 1);
7943     int size = extract32(insn, 22, 2);
7944     int opcode = extract32(insn, 12, 5);
7945     int rn = extract32(insn, 5, 5);
7946     int rd = extract32(insn, 0, 5);
7947     TCGv_ptr fpst;
7948 
7949     /* For some ops (the FP ones), size[1] is part of the encoding.
7950      * For ADDP strictly it is not but size[1] is always 1 for valid
7951      * encodings.
7952      */
7953     opcode |= (extract32(size, 1, 1) << 5);
7954 
7955     switch (opcode) {
7956     case 0x3b: /* ADDP */
7957         if (u || size != 3) {
7958             unallocated_encoding(s);
7959             return;
7960         }
7961         if (!fp_access_check(s)) {
7962             return;
7963         }
7964 
7965         fpst = NULL;
7966         break;
7967     case 0xc: /* FMAXNMP */
7968     case 0xd: /* FADDP */
7969     case 0xf: /* FMAXP */
7970     case 0x2c: /* FMINNMP */
7971     case 0x2f: /* FMINP */
7972         /* FP op, size[0] is 32 or 64 bit*/
7973         if (!u) {
7974             if (!dc_isar_feature(aa64_fp16, s)) {
7975                 unallocated_encoding(s);
7976                 return;
7977             } else {
7978                 size = MO_16;
7979             }
7980         } else {
7981             size = extract32(size, 0, 1) ? MO_64 : MO_32;
7982         }
7983 
7984         if (!fp_access_check(s)) {
7985             return;
7986         }
7987 
7988         fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7989         break;
7990     default:
7991         unallocated_encoding(s);
7992         return;
7993     }
7994 
7995     if (size == MO_64) {
7996         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7997         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7998         TCGv_i64 tcg_res = tcg_temp_new_i64();
7999 
8000         read_vec_element(s, tcg_op1, rn, 0, MO_64);
8001         read_vec_element(s, tcg_op2, rn, 1, MO_64);
8002 
8003         switch (opcode) {
8004         case 0x3b: /* ADDP */
8005             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
8006             break;
8007         case 0xc: /* FMAXNMP */
8008             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8009             break;
8010         case 0xd: /* FADDP */
8011             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8012             break;
8013         case 0xf: /* FMAXP */
8014             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8015             break;
8016         case 0x2c: /* FMINNMP */
8017             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8018             break;
8019         case 0x2f: /* FMINP */
8020             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8021             break;
8022         default:
8023             g_assert_not_reached();
8024         }
8025 
8026         write_fp_dreg(s, rd, tcg_res);
8027     } else {
8028         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8029         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8030         TCGv_i32 tcg_res = tcg_temp_new_i32();
8031 
8032         read_vec_element_i32(s, tcg_op1, rn, 0, size);
8033         read_vec_element_i32(s, tcg_op2, rn, 1, size);
8034 
8035         if (size == MO_16) {
8036             switch (opcode) {
8037             case 0xc: /* FMAXNMP */
8038                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8039                 break;
8040             case 0xd: /* FADDP */
8041                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
8042                 break;
8043             case 0xf: /* FMAXP */
8044                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
8045                 break;
8046             case 0x2c: /* FMINNMP */
8047                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8048                 break;
8049             case 0x2f: /* FMINP */
8050                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
8051                 break;
8052             default:
8053                 g_assert_not_reached();
8054             }
8055         } else {
8056             switch (opcode) {
8057             case 0xc: /* FMAXNMP */
8058                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8059                 break;
8060             case 0xd: /* FADDP */
8061                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8062                 break;
8063             case 0xf: /* FMAXP */
8064                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8065                 break;
8066             case 0x2c: /* FMINNMP */
8067                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8068                 break;
8069             case 0x2f: /* FMINP */
8070                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8071                 break;
8072             default:
8073                 g_assert_not_reached();
8074             }
8075         }
8076 
8077         write_fp_sreg(s, rd, tcg_res);
8078     }
8079 }
8080 
8081 /*
8082  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8083  *
8084  * This code is handles the common shifting code and is used by both
8085  * the vector and scalar code.
8086  */
8087 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8088                                     TCGv_i64 tcg_rnd, bool accumulate,
8089                                     bool is_u, int size, int shift)
8090 {
8091     bool extended_result = false;
8092     bool round = tcg_rnd != NULL;
8093     int ext_lshift = 0;
8094     TCGv_i64 tcg_src_hi;
8095 
8096     if (round && size == 3) {
8097         extended_result = true;
8098         ext_lshift = 64 - shift;
8099         tcg_src_hi = tcg_temp_new_i64();
8100     } else if (shift == 64) {
8101         if (!accumulate && is_u) {
8102             /* result is zero */
8103             tcg_gen_movi_i64(tcg_res, 0);
8104             return;
8105         }
8106     }
8107 
8108     /* Deal with the rounding step */
8109     if (round) {
8110         if (extended_result) {
8111             TCGv_i64 tcg_zero = tcg_constant_i64(0);
8112             if (!is_u) {
8113                 /* take care of sign extending tcg_res */
8114                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8115                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8116                                  tcg_src, tcg_src_hi,
8117                                  tcg_rnd, tcg_zero);
8118             } else {
8119                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8120                                  tcg_src, tcg_zero,
8121                                  tcg_rnd, tcg_zero);
8122             }
8123         } else {
8124             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8125         }
8126     }
8127 
8128     /* Now do the shift right */
8129     if (round && extended_result) {
8130         /* extended case, >64 bit precision required */
8131         if (ext_lshift == 0) {
8132             /* special case, only high bits matter */
8133             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8134         } else {
8135             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8136             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8137             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8138         }
8139     } else {
8140         if (is_u) {
8141             if (shift == 64) {
8142                 /* essentially shifting in 64 zeros */
8143                 tcg_gen_movi_i64(tcg_src, 0);
8144             } else {
8145                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8146             }
8147         } else {
8148             if (shift == 64) {
8149                 /* effectively extending the sign-bit */
8150                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8151             } else {
8152                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8153             }
8154         }
8155     }
8156 
8157     if (accumulate) {
8158         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8159     } else {
8160         tcg_gen_mov_i64(tcg_res, tcg_src);
8161     }
8162 }
8163 
8164 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8165 static void handle_scalar_simd_shri(DisasContext *s,
8166                                     bool is_u, int immh, int immb,
8167                                     int opcode, int rn, int rd)
8168 {
8169     const int size = 3;
8170     int immhb = immh << 3 | immb;
8171     int shift = 2 * (8 << size) - immhb;
8172     bool accumulate = false;
8173     bool round = false;
8174     bool insert = false;
8175     TCGv_i64 tcg_rn;
8176     TCGv_i64 tcg_rd;
8177     TCGv_i64 tcg_round;
8178 
8179     if (!extract32(immh, 3, 1)) {
8180         unallocated_encoding(s);
8181         return;
8182     }
8183 
8184     if (!fp_access_check(s)) {
8185         return;
8186     }
8187 
8188     switch (opcode) {
8189     case 0x02: /* SSRA / USRA (accumulate) */
8190         accumulate = true;
8191         break;
8192     case 0x04: /* SRSHR / URSHR (rounding) */
8193         round = true;
8194         break;
8195     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8196         accumulate = round = true;
8197         break;
8198     case 0x08: /* SRI */
8199         insert = true;
8200         break;
8201     }
8202 
8203     if (round) {
8204         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8205     } else {
8206         tcg_round = NULL;
8207     }
8208 
8209     tcg_rn = read_fp_dreg(s, rn);
8210     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8211 
8212     if (insert) {
8213         /* shift count same as element size is valid but does nothing;
8214          * special case to avoid potential shift by 64.
8215          */
8216         int esize = 8 << size;
8217         if (shift != esize) {
8218             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8219             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8220         }
8221     } else {
8222         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8223                                 accumulate, is_u, size, shift);
8224     }
8225 
8226     write_fp_dreg(s, rd, tcg_rd);
8227 }
8228 
8229 /* SHL/SLI - Scalar shift left */
8230 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8231                                     int immh, int immb, int opcode,
8232                                     int rn, int rd)
8233 {
8234     int size = 32 - clz32(immh) - 1;
8235     int immhb = immh << 3 | immb;
8236     int shift = immhb - (8 << size);
8237     TCGv_i64 tcg_rn;
8238     TCGv_i64 tcg_rd;
8239 
8240     if (!extract32(immh, 3, 1)) {
8241         unallocated_encoding(s);
8242         return;
8243     }
8244 
8245     if (!fp_access_check(s)) {
8246         return;
8247     }
8248 
8249     tcg_rn = read_fp_dreg(s, rn);
8250     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8251 
8252     if (insert) {
8253         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8254     } else {
8255         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8256     }
8257 
8258     write_fp_dreg(s, rd, tcg_rd);
8259 }
8260 
8261 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8262  * (signed/unsigned) narrowing */
8263 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8264                                    bool is_u_shift, bool is_u_narrow,
8265                                    int immh, int immb, int opcode,
8266                                    int rn, int rd)
8267 {
8268     int immhb = immh << 3 | immb;
8269     int size = 32 - clz32(immh) - 1;
8270     int esize = 8 << size;
8271     int shift = (2 * esize) - immhb;
8272     int elements = is_scalar ? 1 : (64 / esize);
8273     bool round = extract32(opcode, 0, 1);
8274     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8275     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8276     TCGv_i32 tcg_rd_narrowed;
8277     TCGv_i64 tcg_final;
8278 
8279     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8280         { gen_helper_neon_narrow_sat_s8,
8281           gen_helper_neon_unarrow_sat8 },
8282         { gen_helper_neon_narrow_sat_s16,
8283           gen_helper_neon_unarrow_sat16 },
8284         { gen_helper_neon_narrow_sat_s32,
8285           gen_helper_neon_unarrow_sat32 },
8286         { NULL, NULL },
8287     };
8288     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8289         gen_helper_neon_narrow_sat_u8,
8290         gen_helper_neon_narrow_sat_u16,
8291         gen_helper_neon_narrow_sat_u32,
8292         NULL
8293     };
8294     NeonGenNarrowEnvFn *narrowfn;
8295 
8296     int i;
8297 
8298     assert(size < 4);
8299 
8300     if (extract32(immh, 3, 1)) {
8301         unallocated_encoding(s);
8302         return;
8303     }
8304 
8305     if (!fp_access_check(s)) {
8306         return;
8307     }
8308 
8309     if (is_u_shift) {
8310         narrowfn = unsigned_narrow_fns[size];
8311     } else {
8312         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8313     }
8314 
8315     tcg_rn = tcg_temp_new_i64();
8316     tcg_rd = tcg_temp_new_i64();
8317     tcg_rd_narrowed = tcg_temp_new_i32();
8318     tcg_final = tcg_temp_new_i64();
8319 
8320     if (round) {
8321         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8322     } else {
8323         tcg_round = NULL;
8324     }
8325 
8326     for (i = 0; i < elements; i++) {
8327         read_vec_element(s, tcg_rn, rn, i, ldop);
8328         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8329                                 false, is_u_shift, size+1, shift);
8330         narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8331         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8332         if (i == 0) {
8333             tcg_gen_mov_i64(tcg_final, tcg_rd);
8334         } else {
8335             tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8336         }
8337     }
8338 
8339     if (!is_q) {
8340         write_vec_element(s, tcg_final, rd, 0, MO_64);
8341     } else {
8342         write_vec_element(s, tcg_final, rd, 1, MO_64);
8343     }
8344     clear_vec_high(s, is_q, rd);
8345 }
8346 
8347 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8348 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8349                              bool src_unsigned, bool dst_unsigned,
8350                              int immh, int immb, int rn, int rd)
8351 {
8352     int immhb = immh << 3 | immb;
8353     int size = 32 - clz32(immh) - 1;
8354     int shift = immhb - (8 << size);
8355     int pass;
8356 
8357     assert(immh != 0);
8358     assert(!(scalar && is_q));
8359 
8360     if (!scalar) {
8361         if (!is_q && extract32(immh, 3, 1)) {
8362             unallocated_encoding(s);
8363             return;
8364         }
8365 
8366         /* Since we use the variable-shift helpers we must
8367          * replicate the shift count into each element of
8368          * the tcg_shift value.
8369          */
8370         switch (size) {
8371         case 0:
8372             shift |= shift << 8;
8373             /* fall through */
8374         case 1:
8375             shift |= shift << 16;
8376             break;
8377         case 2:
8378         case 3:
8379             break;
8380         default:
8381             g_assert_not_reached();
8382         }
8383     }
8384 
8385     if (!fp_access_check(s)) {
8386         return;
8387     }
8388 
8389     if (size == 3) {
8390         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8391         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8392             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8393             { NULL, gen_helper_neon_qshl_u64 },
8394         };
8395         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8396         int maxpass = is_q ? 2 : 1;
8397 
8398         for (pass = 0; pass < maxpass; pass++) {
8399             TCGv_i64 tcg_op = tcg_temp_new_i64();
8400 
8401             read_vec_element(s, tcg_op, rn, pass, MO_64);
8402             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8403             write_vec_element(s, tcg_op, rd, pass, MO_64);
8404         }
8405         clear_vec_high(s, is_q, rd);
8406     } else {
8407         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8408         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8409             {
8410                 { gen_helper_neon_qshl_s8,
8411                   gen_helper_neon_qshl_s16,
8412                   gen_helper_neon_qshl_s32 },
8413                 { gen_helper_neon_qshlu_s8,
8414                   gen_helper_neon_qshlu_s16,
8415                   gen_helper_neon_qshlu_s32 }
8416             }, {
8417                 { NULL, NULL, NULL },
8418                 { gen_helper_neon_qshl_u8,
8419                   gen_helper_neon_qshl_u16,
8420                   gen_helper_neon_qshl_u32 }
8421             }
8422         };
8423         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8424         MemOp memop = scalar ? size : MO_32;
8425         int maxpass = scalar ? 1 : is_q ? 4 : 2;
8426 
8427         for (pass = 0; pass < maxpass; pass++) {
8428             TCGv_i32 tcg_op = tcg_temp_new_i32();
8429 
8430             read_vec_element_i32(s, tcg_op, rn, pass, memop);
8431             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8432             if (scalar) {
8433                 switch (size) {
8434                 case 0:
8435                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
8436                     break;
8437                 case 1:
8438                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8439                     break;
8440                 case 2:
8441                     break;
8442                 default:
8443                     g_assert_not_reached();
8444                 }
8445                 write_fp_sreg(s, rd, tcg_op);
8446             } else {
8447                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8448             }
8449         }
8450 
8451         if (!scalar) {
8452             clear_vec_high(s, is_q, rd);
8453         }
8454     }
8455 }
8456 
8457 /* Common vector code for handling integer to FP conversion */
8458 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8459                                    int elements, int is_signed,
8460                                    int fracbits, int size)
8461 {
8462     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8463     TCGv_i32 tcg_shift = NULL;
8464 
8465     MemOp mop = size | (is_signed ? MO_SIGN : 0);
8466     int pass;
8467 
8468     if (fracbits || size == MO_64) {
8469         tcg_shift = tcg_constant_i32(fracbits);
8470     }
8471 
8472     if (size == MO_64) {
8473         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8474         TCGv_i64 tcg_double = tcg_temp_new_i64();
8475 
8476         for (pass = 0; pass < elements; pass++) {
8477             read_vec_element(s, tcg_int64, rn, pass, mop);
8478 
8479             if (is_signed) {
8480                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8481                                      tcg_shift, tcg_fpst);
8482             } else {
8483                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8484                                      tcg_shift, tcg_fpst);
8485             }
8486             if (elements == 1) {
8487                 write_fp_dreg(s, rd, tcg_double);
8488             } else {
8489                 write_vec_element(s, tcg_double, rd, pass, MO_64);
8490             }
8491         }
8492     } else {
8493         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8494         TCGv_i32 tcg_float = tcg_temp_new_i32();
8495 
8496         for (pass = 0; pass < elements; pass++) {
8497             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8498 
8499             switch (size) {
8500             case MO_32:
8501                 if (fracbits) {
8502                     if (is_signed) {
8503                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
8504                                              tcg_shift, tcg_fpst);
8505                     } else {
8506                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
8507                                              tcg_shift, tcg_fpst);
8508                     }
8509                 } else {
8510                     if (is_signed) {
8511                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8512                     } else {
8513                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8514                     }
8515                 }
8516                 break;
8517             case MO_16:
8518                 if (fracbits) {
8519                     if (is_signed) {
8520                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8521                                              tcg_shift, tcg_fpst);
8522                     } else {
8523                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8524                                              tcg_shift, tcg_fpst);
8525                     }
8526                 } else {
8527                     if (is_signed) {
8528                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8529                     } else {
8530                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8531                     }
8532                 }
8533                 break;
8534             default:
8535                 g_assert_not_reached();
8536             }
8537 
8538             if (elements == 1) {
8539                 write_fp_sreg(s, rd, tcg_float);
8540             } else {
8541                 write_vec_element_i32(s, tcg_float, rd, pass, size);
8542             }
8543         }
8544     }
8545 
8546     clear_vec_high(s, elements << size == 16, rd);
8547 }
8548 
8549 /* UCVTF/SCVTF - Integer to FP conversion */
8550 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8551                                          bool is_q, bool is_u,
8552                                          int immh, int immb, int opcode,
8553                                          int rn, int rd)
8554 {
8555     int size, elements, fracbits;
8556     int immhb = immh << 3 | immb;
8557 
8558     if (immh & 8) {
8559         size = MO_64;
8560         if (!is_scalar && !is_q) {
8561             unallocated_encoding(s);
8562             return;
8563         }
8564     } else if (immh & 4) {
8565         size = MO_32;
8566     } else if (immh & 2) {
8567         size = MO_16;
8568         if (!dc_isar_feature(aa64_fp16, s)) {
8569             unallocated_encoding(s);
8570             return;
8571         }
8572     } else {
8573         /* immh == 0 would be a failure of the decode logic */
8574         g_assert(immh == 1);
8575         unallocated_encoding(s);
8576         return;
8577     }
8578 
8579     if (is_scalar) {
8580         elements = 1;
8581     } else {
8582         elements = (8 << is_q) >> size;
8583     }
8584     fracbits = (16 << size) - immhb;
8585 
8586     if (!fp_access_check(s)) {
8587         return;
8588     }
8589 
8590     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8591 }
8592 
8593 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8594 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8595                                          bool is_q, bool is_u,
8596                                          int immh, int immb, int rn, int rd)
8597 {
8598     int immhb = immh << 3 | immb;
8599     int pass, size, fracbits;
8600     TCGv_ptr tcg_fpstatus;
8601     TCGv_i32 tcg_rmode, tcg_shift;
8602 
8603     if (immh & 0x8) {
8604         size = MO_64;
8605         if (!is_scalar && !is_q) {
8606             unallocated_encoding(s);
8607             return;
8608         }
8609     } else if (immh & 0x4) {
8610         size = MO_32;
8611     } else if (immh & 0x2) {
8612         size = MO_16;
8613         if (!dc_isar_feature(aa64_fp16, s)) {
8614             unallocated_encoding(s);
8615             return;
8616         }
8617     } else {
8618         /* Should have split out AdvSIMD modified immediate earlier.  */
8619         assert(immh == 1);
8620         unallocated_encoding(s);
8621         return;
8622     }
8623 
8624     if (!fp_access_check(s)) {
8625         return;
8626     }
8627 
8628     assert(!(is_scalar && is_q));
8629 
8630     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8631     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
8632     fracbits = (16 << size) - immhb;
8633     tcg_shift = tcg_constant_i32(fracbits);
8634 
8635     if (size == MO_64) {
8636         int maxpass = is_scalar ? 1 : 2;
8637 
8638         for (pass = 0; pass < maxpass; pass++) {
8639             TCGv_i64 tcg_op = tcg_temp_new_i64();
8640 
8641             read_vec_element(s, tcg_op, rn, pass, MO_64);
8642             if (is_u) {
8643                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8644             } else {
8645                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8646             }
8647             write_vec_element(s, tcg_op, rd, pass, MO_64);
8648         }
8649         clear_vec_high(s, is_q, rd);
8650     } else {
8651         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8652         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8653 
8654         switch (size) {
8655         case MO_16:
8656             if (is_u) {
8657                 fn = gen_helper_vfp_touhh;
8658             } else {
8659                 fn = gen_helper_vfp_toshh;
8660             }
8661             break;
8662         case MO_32:
8663             if (is_u) {
8664                 fn = gen_helper_vfp_touls;
8665             } else {
8666                 fn = gen_helper_vfp_tosls;
8667             }
8668             break;
8669         default:
8670             g_assert_not_reached();
8671         }
8672 
8673         for (pass = 0; pass < maxpass; pass++) {
8674             TCGv_i32 tcg_op = tcg_temp_new_i32();
8675 
8676             read_vec_element_i32(s, tcg_op, rn, pass, size);
8677             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8678             if (is_scalar) {
8679                 write_fp_sreg(s, rd, tcg_op);
8680             } else {
8681                 write_vec_element_i32(s, tcg_op, rd, pass, size);
8682             }
8683         }
8684         if (!is_scalar) {
8685             clear_vec_high(s, is_q, rd);
8686         }
8687     }
8688 
8689     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
8690 }
8691 
8692 /* AdvSIMD scalar shift by immediate
8693  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
8694  * +-----+---+-------------+------+------+--------+---+------+------+
8695  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8696  * +-----+---+-------------+------+------+--------+---+------+------+
8697  *
8698  * This is the scalar version so it works on a fixed sized registers
8699  */
8700 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8701 {
8702     int rd = extract32(insn, 0, 5);
8703     int rn = extract32(insn, 5, 5);
8704     int opcode = extract32(insn, 11, 5);
8705     int immb = extract32(insn, 16, 3);
8706     int immh = extract32(insn, 19, 4);
8707     bool is_u = extract32(insn, 29, 1);
8708 
8709     if (immh == 0) {
8710         unallocated_encoding(s);
8711         return;
8712     }
8713 
8714     switch (opcode) {
8715     case 0x08: /* SRI */
8716         if (!is_u) {
8717             unallocated_encoding(s);
8718             return;
8719         }
8720         /* fall through */
8721     case 0x00: /* SSHR / USHR */
8722     case 0x02: /* SSRA / USRA */
8723     case 0x04: /* SRSHR / URSHR */
8724     case 0x06: /* SRSRA / URSRA */
8725         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8726         break;
8727     case 0x0a: /* SHL / SLI */
8728         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8729         break;
8730     case 0x1c: /* SCVTF, UCVTF */
8731         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8732                                      opcode, rn, rd);
8733         break;
8734     case 0x10: /* SQSHRUN, SQSHRUN2 */
8735     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8736         if (!is_u) {
8737             unallocated_encoding(s);
8738             return;
8739         }
8740         handle_vec_simd_sqshrn(s, true, false, false, true,
8741                                immh, immb, opcode, rn, rd);
8742         break;
8743     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8744     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8745         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8746                                immh, immb, opcode, rn, rd);
8747         break;
8748     case 0xc: /* SQSHLU */
8749         if (!is_u) {
8750             unallocated_encoding(s);
8751             return;
8752         }
8753         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8754         break;
8755     case 0xe: /* SQSHL, UQSHL */
8756         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8757         break;
8758     case 0x1f: /* FCVTZS, FCVTZU */
8759         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8760         break;
8761     default:
8762         unallocated_encoding(s);
8763         break;
8764     }
8765 }
8766 
8767 /* AdvSIMD scalar three different
8768  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
8769  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8770  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
8771  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8772  */
8773 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8774 {
8775     bool is_u = extract32(insn, 29, 1);
8776     int size = extract32(insn, 22, 2);
8777     int opcode = extract32(insn, 12, 4);
8778     int rm = extract32(insn, 16, 5);
8779     int rn = extract32(insn, 5, 5);
8780     int rd = extract32(insn, 0, 5);
8781 
8782     if (is_u) {
8783         unallocated_encoding(s);
8784         return;
8785     }
8786 
8787     switch (opcode) {
8788     case 0x9: /* SQDMLAL, SQDMLAL2 */
8789     case 0xb: /* SQDMLSL, SQDMLSL2 */
8790     case 0xd: /* SQDMULL, SQDMULL2 */
8791         if (size == 0 || size == 3) {
8792             unallocated_encoding(s);
8793             return;
8794         }
8795         break;
8796     default:
8797         unallocated_encoding(s);
8798         return;
8799     }
8800 
8801     if (!fp_access_check(s)) {
8802         return;
8803     }
8804 
8805     if (size == 2) {
8806         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8807         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8808         TCGv_i64 tcg_res = tcg_temp_new_i64();
8809 
8810         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8811         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8812 
8813         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8814         gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
8815 
8816         switch (opcode) {
8817         case 0xd: /* SQDMULL, SQDMULL2 */
8818             break;
8819         case 0xb: /* SQDMLSL, SQDMLSL2 */
8820             tcg_gen_neg_i64(tcg_res, tcg_res);
8821             /* fall through */
8822         case 0x9: /* SQDMLAL, SQDMLAL2 */
8823             read_vec_element(s, tcg_op1, rd, 0, MO_64);
8824             gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
8825                                               tcg_res, tcg_op1);
8826             break;
8827         default:
8828             g_assert_not_reached();
8829         }
8830 
8831         write_fp_dreg(s, rd, tcg_res);
8832     } else {
8833         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8834         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8835         TCGv_i64 tcg_res = tcg_temp_new_i64();
8836 
8837         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8838         gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
8839 
8840         switch (opcode) {
8841         case 0xd: /* SQDMULL, SQDMULL2 */
8842             break;
8843         case 0xb: /* SQDMLSL, SQDMLSL2 */
8844             gen_helper_neon_negl_u32(tcg_res, tcg_res);
8845             /* fall through */
8846         case 0x9: /* SQDMLAL, SQDMLAL2 */
8847         {
8848             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
8849             read_vec_element(s, tcg_op3, rd, 0, MO_32);
8850             gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
8851                                               tcg_res, tcg_op3);
8852             break;
8853         }
8854         default:
8855             g_assert_not_reached();
8856         }
8857 
8858         tcg_gen_ext32u_i64(tcg_res, tcg_res);
8859         write_fp_dreg(s, rd, tcg_res);
8860     }
8861 }
8862 
8863 static void handle_3same_64(DisasContext *s, int opcode, bool u,
8864                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
8865 {
8866     /* Handle 64x64->64 opcodes which are shared between the scalar
8867      * and vector 3-same groups. We cover every opcode where size == 3
8868      * is valid in either the three-reg-same (integer, not pairwise)
8869      * or scalar-three-reg-same groups.
8870      */
8871     TCGCond cond;
8872 
8873     switch (opcode) {
8874     case 0x1: /* SQADD */
8875         if (u) {
8876             gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8877         } else {
8878             gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8879         }
8880         break;
8881     case 0x5: /* SQSUB */
8882         if (u) {
8883             gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8884         } else {
8885             gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8886         }
8887         break;
8888     case 0x6: /* CMGT, CMHI */
8889         /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
8890          * We implement this using setcond (test) and then negating.
8891          */
8892         cond = u ? TCG_COND_GTU : TCG_COND_GT;
8893     do_cmop:
8894         tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
8895         tcg_gen_neg_i64(tcg_rd, tcg_rd);
8896         break;
8897     case 0x7: /* CMGE, CMHS */
8898         cond = u ? TCG_COND_GEU : TCG_COND_GE;
8899         goto do_cmop;
8900     case 0x11: /* CMTST, CMEQ */
8901         if (u) {
8902             cond = TCG_COND_EQ;
8903             goto do_cmop;
8904         }
8905         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
8906         break;
8907     case 0x8: /* SSHL, USHL */
8908         if (u) {
8909             gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
8910         } else {
8911             gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
8912         }
8913         break;
8914     case 0x9: /* SQSHL, UQSHL */
8915         if (u) {
8916             gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8917         } else {
8918             gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8919         }
8920         break;
8921     case 0xa: /* SRSHL, URSHL */
8922         if (u) {
8923             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
8924         } else {
8925             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
8926         }
8927         break;
8928     case 0xb: /* SQRSHL, UQRSHL */
8929         if (u) {
8930             gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8931         } else {
8932             gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8933         }
8934         break;
8935     case 0x10: /* ADD, SUB */
8936         if (u) {
8937             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
8938         } else {
8939             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
8940         }
8941         break;
8942     default:
8943         g_assert_not_reached();
8944     }
8945 }
8946 
8947 /* Handle the 3-same-operands float operations; shared by the scalar
8948  * and vector encodings. The caller must filter out any encodings
8949  * not allocated for the encoding it is dealing with.
8950  */
8951 static void handle_3same_float(DisasContext *s, int size, int elements,
8952                                int fpopcode, int rd, int rn, int rm)
8953 {
8954     int pass;
8955     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
8956 
8957     for (pass = 0; pass < elements; pass++) {
8958         if (size) {
8959             /* Double */
8960             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8961             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8962             TCGv_i64 tcg_res = tcg_temp_new_i64();
8963 
8964             read_vec_element(s, tcg_op1, rn, pass, MO_64);
8965             read_vec_element(s, tcg_op2, rm, pass, MO_64);
8966 
8967             switch (fpopcode) {
8968             case 0x39: /* FMLS */
8969                 /* As usual for ARM, separate negation for fused multiply-add */
8970                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
8971                 /* fall through */
8972             case 0x19: /* FMLA */
8973                 read_vec_element(s, tcg_res, rd, pass, MO_64);
8974                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
8975                                        tcg_res, fpst);
8976                 break;
8977             case 0x18: /* FMAXNM */
8978                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8979                 break;
8980             case 0x1a: /* FADD */
8981                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8982                 break;
8983             case 0x1b: /* FMULX */
8984                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
8985                 break;
8986             case 0x1c: /* FCMEQ */
8987                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8988                 break;
8989             case 0x1e: /* FMAX */
8990                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8991                 break;
8992             case 0x1f: /* FRECPS */
8993                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8994                 break;
8995             case 0x38: /* FMINNM */
8996                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8997                 break;
8998             case 0x3a: /* FSUB */
8999                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9000                 break;
9001             case 0x3e: /* FMIN */
9002                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
9003                 break;
9004             case 0x3f: /* FRSQRTS */
9005                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9006                 break;
9007             case 0x5b: /* FMUL */
9008                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
9009                 break;
9010             case 0x5c: /* FCMGE */
9011                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9012                 break;
9013             case 0x5d: /* FACGE */
9014                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9015                 break;
9016             case 0x5f: /* FDIV */
9017                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
9018                 break;
9019             case 0x7a: /* FABD */
9020                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9021                 gen_helper_vfp_absd(tcg_res, tcg_res);
9022                 break;
9023             case 0x7c: /* FCMGT */
9024                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9025                 break;
9026             case 0x7d: /* FACGT */
9027                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9028                 break;
9029             default:
9030                 g_assert_not_reached();
9031             }
9032 
9033             write_vec_element(s, tcg_res, rd, pass, MO_64);
9034         } else {
9035             /* Single */
9036             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9037             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9038             TCGv_i32 tcg_res = tcg_temp_new_i32();
9039 
9040             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9041             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9042 
9043             switch (fpopcode) {
9044             case 0x39: /* FMLS */
9045                 /* As usual for ARM, separate negation for fused multiply-add */
9046                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
9047                 /* fall through */
9048             case 0x19: /* FMLA */
9049                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9050                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
9051                                        tcg_res, fpst);
9052                 break;
9053             case 0x1a: /* FADD */
9054                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
9055                 break;
9056             case 0x1b: /* FMULX */
9057                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
9058                 break;
9059             case 0x1c: /* FCMEQ */
9060                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9061                 break;
9062             case 0x1e: /* FMAX */
9063                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
9064                 break;
9065             case 0x1f: /* FRECPS */
9066                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9067                 break;
9068             case 0x18: /* FMAXNM */
9069                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
9070                 break;
9071             case 0x38: /* FMINNM */
9072                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
9073                 break;
9074             case 0x3a: /* FSUB */
9075                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9076                 break;
9077             case 0x3e: /* FMIN */
9078                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9079                 break;
9080             case 0x3f: /* FRSQRTS */
9081                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9082                 break;
9083             case 0x5b: /* FMUL */
9084                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9085                 break;
9086             case 0x5c: /* FCMGE */
9087                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9088                 break;
9089             case 0x5d: /* FACGE */
9090                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9091                 break;
9092             case 0x5f: /* FDIV */
9093                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9094                 break;
9095             case 0x7a: /* FABD */
9096                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9097                 gen_helper_vfp_abss(tcg_res, tcg_res);
9098                 break;
9099             case 0x7c: /* FCMGT */
9100                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9101                 break;
9102             case 0x7d: /* FACGT */
9103                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9104                 break;
9105             default:
9106                 g_assert_not_reached();
9107             }
9108 
9109             if (elements == 1) {
9110                 /* scalar single so clear high part */
9111                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9112 
9113                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9114                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9115             } else {
9116                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9117             }
9118         }
9119     }
9120 
9121     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9122 }
9123 
9124 /* AdvSIMD scalar three same
9125  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
9126  * +-----+---+-----------+------+---+------+--------+---+------+------+
9127  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
9128  * +-----+---+-----------+------+---+------+--------+---+------+------+
9129  */
9130 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9131 {
9132     int rd = extract32(insn, 0, 5);
9133     int rn = extract32(insn, 5, 5);
9134     int opcode = extract32(insn, 11, 5);
9135     int rm = extract32(insn, 16, 5);
9136     int size = extract32(insn, 22, 2);
9137     bool u = extract32(insn, 29, 1);
9138     TCGv_i64 tcg_rd;
9139 
9140     if (opcode >= 0x18) {
9141         /* Floating point: U, size[1] and opcode indicate operation */
9142         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9143         switch (fpopcode) {
9144         case 0x1b: /* FMULX */
9145         case 0x1f: /* FRECPS */
9146         case 0x3f: /* FRSQRTS */
9147         case 0x5d: /* FACGE */
9148         case 0x7d: /* FACGT */
9149         case 0x1c: /* FCMEQ */
9150         case 0x5c: /* FCMGE */
9151         case 0x7c: /* FCMGT */
9152         case 0x7a: /* FABD */
9153             break;
9154         default:
9155             unallocated_encoding(s);
9156             return;
9157         }
9158 
9159         if (!fp_access_check(s)) {
9160             return;
9161         }
9162 
9163         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9164         return;
9165     }
9166 
9167     switch (opcode) {
9168     case 0x1: /* SQADD, UQADD */
9169     case 0x5: /* SQSUB, UQSUB */
9170     case 0x9: /* SQSHL, UQSHL */
9171     case 0xb: /* SQRSHL, UQRSHL */
9172         break;
9173     case 0x8: /* SSHL, USHL */
9174     case 0xa: /* SRSHL, URSHL */
9175     case 0x6: /* CMGT, CMHI */
9176     case 0x7: /* CMGE, CMHS */
9177     case 0x11: /* CMTST, CMEQ */
9178     case 0x10: /* ADD, SUB (vector) */
9179         if (size != 3) {
9180             unallocated_encoding(s);
9181             return;
9182         }
9183         break;
9184     case 0x16: /* SQDMULH, SQRDMULH (vector) */
9185         if (size != 1 && size != 2) {
9186             unallocated_encoding(s);
9187             return;
9188         }
9189         break;
9190     default:
9191         unallocated_encoding(s);
9192         return;
9193     }
9194 
9195     if (!fp_access_check(s)) {
9196         return;
9197     }
9198 
9199     tcg_rd = tcg_temp_new_i64();
9200 
9201     if (size == 3) {
9202         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9203         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9204 
9205         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9206     } else {
9207         /* Do a single operation on the lowest element in the vector.
9208          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9209          * no side effects for all these operations.
9210          * OPTME: special-purpose helpers would avoid doing some
9211          * unnecessary work in the helper for the 8 and 16 bit cases.
9212          */
9213         NeonGenTwoOpEnvFn *genenvfn;
9214         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9215         TCGv_i32 tcg_rm = tcg_temp_new_i32();
9216         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9217 
9218         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9219         read_vec_element_i32(s, tcg_rm, rm, 0, size);
9220 
9221         switch (opcode) {
9222         case 0x1: /* SQADD, UQADD */
9223         {
9224             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9225                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9226                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9227                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9228             };
9229             genenvfn = fns[size][u];
9230             break;
9231         }
9232         case 0x5: /* SQSUB, UQSUB */
9233         {
9234             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9235                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9236                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9237                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9238             };
9239             genenvfn = fns[size][u];
9240             break;
9241         }
9242         case 0x9: /* SQSHL, UQSHL */
9243         {
9244             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9245                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9246                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9247                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9248             };
9249             genenvfn = fns[size][u];
9250             break;
9251         }
9252         case 0xb: /* SQRSHL, UQRSHL */
9253         {
9254             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9255                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9256                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9257                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9258             };
9259             genenvfn = fns[size][u];
9260             break;
9261         }
9262         case 0x16: /* SQDMULH, SQRDMULH */
9263         {
9264             static NeonGenTwoOpEnvFn * const fns[2][2] = {
9265                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9266                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9267             };
9268             assert(size == 1 || size == 2);
9269             genenvfn = fns[size - 1][u];
9270             break;
9271         }
9272         default:
9273             g_assert_not_reached();
9274         }
9275 
9276         genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9277         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9278     }
9279 
9280     write_fp_dreg(s, rd, tcg_rd);
9281 }
9282 
9283 /* AdvSIMD scalar three same FP16
9284  *  31 30  29 28       24 23  22 21 20  16 15 14 13    11 10  9  5 4  0
9285  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9286  * | 0 1 | U | 1 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 | Rn | Rd |
9287  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9288  * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9289  * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9290  */
9291 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9292                                                   uint32_t insn)
9293 {
9294     int rd = extract32(insn, 0, 5);
9295     int rn = extract32(insn, 5, 5);
9296     int opcode = extract32(insn, 11, 3);
9297     int rm = extract32(insn, 16, 5);
9298     bool u = extract32(insn, 29, 1);
9299     bool a = extract32(insn, 23, 1);
9300     int fpopcode = opcode | (a << 3) |  (u << 4);
9301     TCGv_ptr fpst;
9302     TCGv_i32 tcg_op1;
9303     TCGv_i32 tcg_op2;
9304     TCGv_i32 tcg_res;
9305 
9306     switch (fpopcode) {
9307     case 0x03: /* FMULX */
9308     case 0x04: /* FCMEQ (reg) */
9309     case 0x07: /* FRECPS */
9310     case 0x0f: /* FRSQRTS */
9311     case 0x14: /* FCMGE (reg) */
9312     case 0x15: /* FACGE */
9313     case 0x1a: /* FABD */
9314     case 0x1c: /* FCMGT (reg) */
9315     case 0x1d: /* FACGT */
9316         break;
9317     default:
9318         unallocated_encoding(s);
9319         return;
9320     }
9321 
9322     if (!dc_isar_feature(aa64_fp16, s)) {
9323         unallocated_encoding(s);
9324     }
9325 
9326     if (!fp_access_check(s)) {
9327         return;
9328     }
9329 
9330     fpst = fpstatus_ptr(FPST_FPCR_F16);
9331 
9332     tcg_op1 = read_fp_hreg(s, rn);
9333     tcg_op2 = read_fp_hreg(s, rm);
9334     tcg_res = tcg_temp_new_i32();
9335 
9336     switch (fpopcode) {
9337     case 0x03: /* FMULX */
9338         gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9339         break;
9340     case 0x04: /* FCMEQ (reg) */
9341         gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9342         break;
9343     case 0x07: /* FRECPS */
9344         gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9345         break;
9346     case 0x0f: /* FRSQRTS */
9347         gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9348         break;
9349     case 0x14: /* FCMGE (reg) */
9350         gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9351         break;
9352     case 0x15: /* FACGE */
9353         gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9354         break;
9355     case 0x1a: /* FABD */
9356         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9357         tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9358         break;
9359     case 0x1c: /* FCMGT (reg) */
9360         gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9361         break;
9362     case 0x1d: /* FACGT */
9363         gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9364         break;
9365     default:
9366         g_assert_not_reached();
9367     }
9368 
9369     write_fp_sreg(s, rd, tcg_res);
9370 }
9371 
9372 /* AdvSIMD scalar three same extra
9373  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9374  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9375  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9376  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9377  */
9378 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9379                                                    uint32_t insn)
9380 {
9381     int rd = extract32(insn, 0, 5);
9382     int rn = extract32(insn, 5, 5);
9383     int opcode = extract32(insn, 11, 4);
9384     int rm = extract32(insn, 16, 5);
9385     int size = extract32(insn, 22, 2);
9386     bool u = extract32(insn, 29, 1);
9387     TCGv_i32 ele1, ele2, ele3;
9388     TCGv_i64 res;
9389     bool feature;
9390 
9391     switch (u * 16 + opcode) {
9392     case 0x10: /* SQRDMLAH (vector) */
9393     case 0x11: /* SQRDMLSH (vector) */
9394         if (size != 1 && size != 2) {
9395             unallocated_encoding(s);
9396             return;
9397         }
9398         feature = dc_isar_feature(aa64_rdm, s);
9399         break;
9400     default:
9401         unallocated_encoding(s);
9402         return;
9403     }
9404     if (!feature) {
9405         unallocated_encoding(s);
9406         return;
9407     }
9408     if (!fp_access_check(s)) {
9409         return;
9410     }
9411 
9412     /* Do a single operation on the lowest element in the vector.
9413      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9414      * with no side effects for all these operations.
9415      * OPTME: special-purpose helpers would avoid doing some
9416      * unnecessary work in the helper for the 16 bit cases.
9417      */
9418     ele1 = tcg_temp_new_i32();
9419     ele2 = tcg_temp_new_i32();
9420     ele3 = tcg_temp_new_i32();
9421 
9422     read_vec_element_i32(s, ele1, rn, 0, size);
9423     read_vec_element_i32(s, ele2, rm, 0, size);
9424     read_vec_element_i32(s, ele3, rd, 0, size);
9425 
9426     switch (opcode) {
9427     case 0x0: /* SQRDMLAH */
9428         if (size == 1) {
9429             gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9430         } else {
9431             gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9432         }
9433         break;
9434     case 0x1: /* SQRDMLSH */
9435         if (size == 1) {
9436             gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9437         } else {
9438             gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9439         }
9440         break;
9441     default:
9442         g_assert_not_reached();
9443     }
9444 
9445     res = tcg_temp_new_i64();
9446     tcg_gen_extu_i32_i64(res, ele3);
9447     write_fp_dreg(s, rd, res);
9448 }
9449 
9450 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9451                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9452                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9453 {
9454     /* Handle 64->64 opcodes which are shared between the scalar and
9455      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9456      * is valid in either group and also the double-precision fp ops.
9457      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9458      * requires them.
9459      */
9460     TCGCond cond;
9461 
9462     switch (opcode) {
9463     case 0x4: /* CLS, CLZ */
9464         if (u) {
9465             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9466         } else {
9467             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9468         }
9469         break;
9470     case 0x5: /* NOT */
9471         /* This opcode is shared with CNT and RBIT but we have earlier
9472          * enforced that size == 3 if and only if this is the NOT insn.
9473          */
9474         tcg_gen_not_i64(tcg_rd, tcg_rn);
9475         break;
9476     case 0x7: /* SQABS, SQNEG */
9477         if (u) {
9478             gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9479         } else {
9480             gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9481         }
9482         break;
9483     case 0xa: /* CMLT */
9484         /* 64 bit integer comparison against zero, result is
9485          * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9486          * subtracting 1.
9487          */
9488         cond = TCG_COND_LT;
9489     do_cmop:
9490         tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9491         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9492         break;
9493     case 0x8: /* CMGT, CMGE */
9494         cond = u ? TCG_COND_GE : TCG_COND_GT;
9495         goto do_cmop;
9496     case 0x9: /* CMEQ, CMLE */
9497         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9498         goto do_cmop;
9499     case 0xb: /* ABS, NEG */
9500         if (u) {
9501             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9502         } else {
9503             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9504         }
9505         break;
9506     case 0x2f: /* FABS */
9507         gen_helper_vfp_absd(tcg_rd, tcg_rn);
9508         break;
9509     case 0x6f: /* FNEG */
9510         gen_helper_vfp_negd(tcg_rd, tcg_rn);
9511         break;
9512     case 0x7f: /* FSQRT */
9513         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9514         break;
9515     case 0x1a: /* FCVTNS */
9516     case 0x1b: /* FCVTMS */
9517     case 0x1c: /* FCVTAS */
9518     case 0x3a: /* FCVTPS */
9519     case 0x3b: /* FCVTZS */
9520         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9521         break;
9522     case 0x5a: /* FCVTNU */
9523     case 0x5b: /* FCVTMU */
9524     case 0x5c: /* FCVTAU */
9525     case 0x7a: /* FCVTPU */
9526     case 0x7b: /* FCVTZU */
9527         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9528         break;
9529     case 0x18: /* FRINTN */
9530     case 0x19: /* FRINTM */
9531     case 0x38: /* FRINTP */
9532     case 0x39: /* FRINTZ */
9533     case 0x58: /* FRINTA */
9534     case 0x79: /* FRINTI */
9535         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9536         break;
9537     case 0x59: /* FRINTX */
9538         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9539         break;
9540     case 0x1e: /* FRINT32Z */
9541     case 0x5e: /* FRINT32X */
9542         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9543         break;
9544     case 0x1f: /* FRINT64Z */
9545     case 0x5f: /* FRINT64X */
9546         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9547         break;
9548     default:
9549         g_assert_not_reached();
9550     }
9551 }
9552 
9553 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9554                                    bool is_scalar, bool is_u, bool is_q,
9555                                    int size, int rn, int rd)
9556 {
9557     bool is_double = (size == MO_64);
9558     TCGv_ptr fpst;
9559 
9560     if (!fp_access_check(s)) {
9561         return;
9562     }
9563 
9564     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9565 
9566     if (is_double) {
9567         TCGv_i64 tcg_op = tcg_temp_new_i64();
9568         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9569         TCGv_i64 tcg_res = tcg_temp_new_i64();
9570         NeonGenTwoDoubleOpFn *genfn;
9571         bool swap = false;
9572         int pass;
9573 
9574         switch (opcode) {
9575         case 0x2e: /* FCMLT (zero) */
9576             swap = true;
9577             /* fallthrough */
9578         case 0x2c: /* FCMGT (zero) */
9579             genfn = gen_helper_neon_cgt_f64;
9580             break;
9581         case 0x2d: /* FCMEQ (zero) */
9582             genfn = gen_helper_neon_ceq_f64;
9583             break;
9584         case 0x6d: /* FCMLE (zero) */
9585             swap = true;
9586             /* fall through */
9587         case 0x6c: /* FCMGE (zero) */
9588             genfn = gen_helper_neon_cge_f64;
9589             break;
9590         default:
9591             g_assert_not_reached();
9592         }
9593 
9594         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9595             read_vec_element(s, tcg_op, rn, pass, MO_64);
9596             if (swap) {
9597                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9598             } else {
9599                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9600             }
9601             write_vec_element(s, tcg_res, rd, pass, MO_64);
9602         }
9603 
9604         clear_vec_high(s, !is_scalar, rd);
9605     } else {
9606         TCGv_i32 tcg_op = tcg_temp_new_i32();
9607         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9608         TCGv_i32 tcg_res = tcg_temp_new_i32();
9609         NeonGenTwoSingleOpFn *genfn;
9610         bool swap = false;
9611         int pass, maxpasses;
9612 
9613         if (size == MO_16) {
9614             switch (opcode) {
9615             case 0x2e: /* FCMLT (zero) */
9616                 swap = true;
9617                 /* fall through */
9618             case 0x2c: /* FCMGT (zero) */
9619                 genfn = gen_helper_advsimd_cgt_f16;
9620                 break;
9621             case 0x2d: /* FCMEQ (zero) */
9622                 genfn = gen_helper_advsimd_ceq_f16;
9623                 break;
9624             case 0x6d: /* FCMLE (zero) */
9625                 swap = true;
9626                 /* fall through */
9627             case 0x6c: /* FCMGE (zero) */
9628                 genfn = gen_helper_advsimd_cge_f16;
9629                 break;
9630             default:
9631                 g_assert_not_reached();
9632             }
9633         } else {
9634             switch (opcode) {
9635             case 0x2e: /* FCMLT (zero) */
9636                 swap = true;
9637                 /* fall through */
9638             case 0x2c: /* FCMGT (zero) */
9639                 genfn = gen_helper_neon_cgt_f32;
9640                 break;
9641             case 0x2d: /* FCMEQ (zero) */
9642                 genfn = gen_helper_neon_ceq_f32;
9643                 break;
9644             case 0x6d: /* FCMLE (zero) */
9645                 swap = true;
9646                 /* fall through */
9647             case 0x6c: /* FCMGE (zero) */
9648                 genfn = gen_helper_neon_cge_f32;
9649                 break;
9650             default:
9651                 g_assert_not_reached();
9652             }
9653         }
9654 
9655         if (is_scalar) {
9656             maxpasses = 1;
9657         } else {
9658             int vector_size = 8 << is_q;
9659             maxpasses = vector_size >> size;
9660         }
9661 
9662         for (pass = 0; pass < maxpasses; pass++) {
9663             read_vec_element_i32(s, tcg_op, rn, pass, size);
9664             if (swap) {
9665                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9666             } else {
9667                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9668             }
9669             if (is_scalar) {
9670                 write_fp_sreg(s, rd, tcg_res);
9671             } else {
9672                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9673             }
9674         }
9675 
9676         if (!is_scalar) {
9677             clear_vec_high(s, is_q, rd);
9678         }
9679     }
9680 }
9681 
9682 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9683                                     bool is_scalar, bool is_u, bool is_q,
9684                                     int size, int rn, int rd)
9685 {
9686     bool is_double = (size == 3);
9687     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9688 
9689     if (is_double) {
9690         TCGv_i64 tcg_op = tcg_temp_new_i64();
9691         TCGv_i64 tcg_res = tcg_temp_new_i64();
9692         int pass;
9693 
9694         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9695             read_vec_element(s, tcg_op, rn, pass, MO_64);
9696             switch (opcode) {
9697             case 0x3d: /* FRECPE */
9698                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9699                 break;
9700             case 0x3f: /* FRECPX */
9701                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9702                 break;
9703             case 0x7d: /* FRSQRTE */
9704                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9705                 break;
9706             default:
9707                 g_assert_not_reached();
9708             }
9709             write_vec_element(s, tcg_res, rd, pass, MO_64);
9710         }
9711         clear_vec_high(s, !is_scalar, rd);
9712     } else {
9713         TCGv_i32 tcg_op = tcg_temp_new_i32();
9714         TCGv_i32 tcg_res = tcg_temp_new_i32();
9715         int pass, maxpasses;
9716 
9717         if (is_scalar) {
9718             maxpasses = 1;
9719         } else {
9720             maxpasses = is_q ? 4 : 2;
9721         }
9722 
9723         for (pass = 0; pass < maxpasses; pass++) {
9724             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9725 
9726             switch (opcode) {
9727             case 0x3c: /* URECPE */
9728                 gen_helper_recpe_u32(tcg_res, tcg_op);
9729                 break;
9730             case 0x3d: /* FRECPE */
9731                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9732                 break;
9733             case 0x3f: /* FRECPX */
9734                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9735                 break;
9736             case 0x7d: /* FRSQRTE */
9737                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9738                 break;
9739             default:
9740                 g_assert_not_reached();
9741             }
9742 
9743             if (is_scalar) {
9744                 write_fp_sreg(s, rd, tcg_res);
9745             } else {
9746                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9747             }
9748         }
9749         if (!is_scalar) {
9750             clear_vec_high(s, is_q, rd);
9751         }
9752     }
9753 }
9754 
9755 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9756                                 int opcode, bool u, bool is_q,
9757                                 int size, int rn, int rd)
9758 {
9759     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9760      * in the source becomes a size element in the destination).
9761      */
9762     int pass;
9763     TCGv_i32 tcg_res[2];
9764     int destelt = is_q ? 2 : 0;
9765     int passes = scalar ? 1 : 2;
9766 
9767     if (scalar) {
9768         tcg_res[1] = tcg_constant_i32(0);
9769     }
9770 
9771     for (pass = 0; pass < passes; pass++) {
9772         TCGv_i64 tcg_op = tcg_temp_new_i64();
9773         NeonGenNarrowFn *genfn = NULL;
9774         NeonGenNarrowEnvFn *genenvfn = NULL;
9775 
9776         if (scalar) {
9777             read_vec_element(s, tcg_op, rn, pass, size + 1);
9778         } else {
9779             read_vec_element(s, tcg_op, rn, pass, MO_64);
9780         }
9781         tcg_res[pass] = tcg_temp_new_i32();
9782 
9783         switch (opcode) {
9784         case 0x12: /* XTN, SQXTUN */
9785         {
9786             static NeonGenNarrowFn * const xtnfns[3] = {
9787                 gen_helper_neon_narrow_u8,
9788                 gen_helper_neon_narrow_u16,
9789                 tcg_gen_extrl_i64_i32,
9790             };
9791             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9792                 gen_helper_neon_unarrow_sat8,
9793                 gen_helper_neon_unarrow_sat16,
9794                 gen_helper_neon_unarrow_sat32,
9795             };
9796             if (u) {
9797                 genenvfn = sqxtunfns[size];
9798             } else {
9799                 genfn = xtnfns[size];
9800             }
9801             break;
9802         }
9803         case 0x14: /* SQXTN, UQXTN */
9804         {
9805             static NeonGenNarrowEnvFn * const fns[3][2] = {
9806                 { gen_helper_neon_narrow_sat_s8,
9807                   gen_helper_neon_narrow_sat_u8 },
9808                 { gen_helper_neon_narrow_sat_s16,
9809                   gen_helper_neon_narrow_sat_u16 },
9810                 { gen_helper_neon_narrow_sat_s32,
9811                   gen_helper_neon_narrow_sat_u32 },
9812             };
9813             genenvfn = fns[size][u];
9814             break;
9815         }
9816         case 0x16: /* FCVTN, FCVTN2 */
9817             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9818             if (size == 2) {
9819                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
9820             } else {
9821                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9822                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9823                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9824                 TCGv_i32 ahp = get_ahp_flag();
9825 
9826                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9827                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9828                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9829                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9830             }
9831             break;
9832         case 0x36: /* BFCVTN, BFCVTN2 */
9833             {
9834                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9835                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
9836             }
9837             break;
9838         case 0x56:  /* FCVTXN, FCVTXN2 */
9839             /* 64 bit to 32 bit float conversion
9840              * with von Neumann rounding (round to odd)
9841              */
9842             assert(size == 2);
9843             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
9844             break;
9845         default:
9846             g_assert_not_reached();
9847         }
9848 
9849         if (genfn) {
9850             genfn(tcg_res[pass], tcg_op);
9851         } else if (genenvfn) {
9852             genenvfn(tcg_res[pass], cpu_env, tcg_op);
9853         }
9854     }
9855 
9856     for (pass = 0; pass < 2; pass++) {
9857         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
9858     }
9859     clear_vec_high(s, is_q, rd);
9860 }
9861 
9862 /* Remaining saturating accumulating ops */
9863 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
9864                                 bool is_q, int size, int rn, int rd)
9865 {
9866     bool is_double = (size == 3);
9867 
9868     if (is_double) {
9869         TCGv_i64 tcg_rn = tcg_temp_new_i64();
9870         TCGv_i64 tcg_rd = tcg_temp_new_i64();
9871         int pass;
9872 
9873         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9874             read_vec_element(s, tcg_rn, rn, pass, MO_64);
9875             read_vec_element(s, tcg_rd, rd, pass, MO_64);
9876 
9877             if (is_u) { /* USQADD */
9878                 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9879             } else { /* SUQADD */
9880                 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9881             }
9882             write_vec_element(s, tcg_rd, rd, pass, MO_64);
9883         }
9884         clear_vec_high(s, !is_scalar, rd);
9885     } else {
9886         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9887         TCGv_i32 tcg_rd = tcg_temp_new_i32();
9888         int pass, maxpasses;
9889 
9890         if (is_scalar) {
9891             maxpasses = 1;
9892         } else {
9893             maxpasses = is_q ? 4 : 2;
9894         }
9895 
9896         for (pass = 0; pass < maxpasses; pass++) {
9897             if (is_scalar) {
9898                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
9899                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
9900             } else {
9901                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
9902                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9903             }
9904 
9905             if (is_u) { /* USQADD */
9906                 switch (size) {
9907                 case 0:
9908                     gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9909                     break;
9910                 case 1:
9911                     gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9912                     break;
9913                 case 2:
9914                     gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9915                     break;
9916                 default:
9917                     g_assert_not_reached();
9918                 }
9919             } else { /* SUQADD */
9920                 switch (size) {
9921                 case 0:
9922                     gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9923                     break;
9924                 case 1:
9925                     gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9926                     break;
9927                 case 2:
9928                     gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9929                     break;
9930                 default:
9931                     g_assert_not_reached();
9932                 }
9933             }
9934 
9935             if (is_scalar) {
9936                 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
9937             }
9938             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9939         }
9940         clear_vec_high(s, is_q, rd);
9941     }
9942 }
9943 
9944 /* AdvSIMD scalar two reg misc
9945  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
9946  * +-----+---+-----------+------+-----------+--------+-----+------+------+
9947  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
9948  * +-----+---+-----------+------+-----------+--------+-----+------+------+
9949  */
9950 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
9951 {
9952     int rd = extract32(insn, 0, 5);
9953     int rn = extract32(insn, 5, 5);
9954     int opcode = extract32(insn, 12, 5);
9955     int size = extract32(insn, 22, 2);
9956     bool u = extract32(insn, 29, 1);
9957     bool is_fcvt = false;
9958     int rmode;
9959     TCGv_i32 tcg_rmode;
9960     TCGv_ptr tcg_fpstatus;
9961 
9962     switch (opcode) {
9963     case 0x3: /* USQADD / SUQADD*/
9964         if (!fp_access_check(s)) {
9965             return;
9966         }
9967         handle_2misc_satacc(s, true, u, false, size, rn, rd);
9968         return;
9969     case 0x7: /* SQABS / SQNEG */
9970         break;
9971     case 0xa: /* CMLT */
9972         if (u) {
9973             unallocated_encoding(s);
9974             return;
9975         }
9976         /* fall through */
9977     case 0x8: /* CMGT, CMGE */
9978     case 0x9: /* CMEQ, CMLE */
9979     case 0xb: /* ABS, NEG */
9980         if (size != 3) {
9981             unallocated_encoding(s);
9982             return;
9983         }
9984         break;
9985     case 0x12: /* SQXTUN */
9986         if (!u) {
9987             unallocated_encoding(s);
9988             return;
9989         }
9990         /* fall through */
9991     case 0x14: /* SQXTN, UQXTN */
9992         if (size == 3) {
9993             unallocated_encoding(s);
9994             return;
9995         }
9996         if (!fp_access_check(s)) {
9997             return;
9998         }
9999         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10000         return;
10001     case 0xc ... 0xf:
10002     case 0x16 ... 0x1d:
10003     case 0x1f:
10004         /* Floating point: U, size[1] and opcode indicate operation;
10005          * size[0] indicates single or double precision.
10006          */
10007         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10008         size = extract32(size, 0, 1) ? 3 : 2;
10009         switch (opcode) {
10010         case 0x2c: /* FCMGT (zero) */
10011         case 0x2d: /* FCMEQ (zero) */
10012         case 0x2e: /* FCMLT (zero) */
10013         case 0x6c: /* FCMGE (zero) */
10014         case 0x6d: /* FCMLE (zero) */
10015             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10016             return;
10017         case 0x1d: /* SCVTF */
10018         case 0x5d: /* UCVTF */
10019         {
10020             bool is_signed = (opcode == 0x1d);
10021             if (!fp_access_check(s)) {
10022                 return;
10023             }
10024             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10025             return;
10026         }
10027         case 0x3d: /* FRECPE */
10028         case 0x3f: /* FRECPX */
10029         case 0x7d: /* FRSQRTE */
10030             if (!fp_access_check(s)) {
10031                 return;
10032             }
10033             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10034             return;
10035         case 0x1a: /* FCVTNS */
10036         case 0x1b: /* FCVTMS */
10037         case 0x3a: /* FCVTPS */
10038         case 0x3b: /* FCVTZS */
10039         case 0x5a: /* FCVTNU */
10040         case 0x5b: /* FCVTMU */
10041         case 0x7a: /* FCVTPU */
10042         case 0x7b: /* FCVTZU */
10043             is_fcvt = true;
10044             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10045             break;
10046         case 0x1c: /* FCVTAS */
10047         case 0x5c: /* FCVTAU */
10048             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10049             is_fcvt = true;
10050             rmode = FPROUNDING_TIEAWAY;
10051             break;
10052         case 0x56: /* FCVTXN, FCVTXN2 */
10053             if (size == 2) {
10054                 unallocated_encoding(s);
10055                 return;
10056             }
10057             if (!fp_access_check(s)) {
10058                 return;
10059             }
10060             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10061             return;
10062         default:
10063             unallocated_encoding(s);
10064             return;
10065         }
10066         break;
10067     default:
10068         unallocated_encoding(s);
10069         return;
10070     }
10071 
10072     if (!fp_access_check(s)) {
10073         return;
10074     }
10075 
10076     if (is_fcvt) {
10077         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
10078         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
10079     } else {
10080         tcg_fpstatus = NULL;
10081         tcg_rmode = NULL;
10082     }
10083 
10084     if (size == 3) {
10085         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10086         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10087 
10088         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10089         write_fp_dreg(s, rd, tcg_rd);
10090     } else {
10091         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10092         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10093 
10094         read_vec_element_i32(s, tcg_rn, rn, 0, size);
10095 
10096         switch (opcode) {
10097         case 0x7: /* SQABS, SQNEG */
10098         {
10099             NeonGenOneOpEnvFn *genfn;
10100             static NeonGenOneOpEnvFn * const fns[3][2] = {
10101                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10102                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10103                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10104             };
10105             genfn = fns[size][u];
10106             genfn(tcg_rd, cpu_env, tcg_rn);
10107             break;
10108         }
10109         case 0x1a: /* FCVTNS */
10110         case 0x1b: /* FCVTMS */
10111         case 0x1c: /* FCVTAS */
10112         case 0x3a: /* FCVTPS */
10113         case 0x3b: /* FCVTZS */
10114             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10115                                  tcg_fpstatus);
10116             break;
10117         case 0x5a: /* FCVTNU */
10118         case 0x5b: /* FCVTMU */
10119         case 0x5c: /* FCVTAU */
10120         case 0x7a: /* FCVTPU */
10121         case 0x7b: /* FCVTZU */
10122             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10123                                  tcg_fpstatus);
10124             break;
10125         default:
10126             g_assert_not_reached();
10127         }
10128 
10129         write_fp_sreg(s, rd, tcg_rd);
10130     }
10131 
10132     if (is_fcvt) {
10133         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
10134     }
10135 }
10136 
10137 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10138 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10139                                  int immh, int immb, int opcode, int rn, int rd)
10140 {
10141     int size = 32 - clz32(immh) - 1;
10142     int immhb = immh << 3 | immb;
10143     int shift = 2 * (8 << size) - immhb;
10144     GVecGen2iFn *gvec_fn;
10145 
10146     if (extract32(immh, 3, 1) && !is_q) {
10147         unallocated_encoding(s);
10148         return;
10149     }
10150     tcg_debug_assert(size <= 3);
10151 
10152     if (!fp_access_check(s)) {
10153         return;
10154     }
10155 
10156     switch (opcode) {
10157     case 0x02: /* SSRA / USRA (accumulate) */
10158         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10159         break;
10160 
10161     case 0x08: /* SRI */
10162         gvec_fn = gen_gvec_sri;
10163         break;
10164 
10165     case 0x00: /* SSHR / USHR */
10166         if (is_u) {
10167             if (shift == 8 << size) {
10168                 /* Shift count the same size as element size produces zero.  */
10169                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10170                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
10171                 return;
10172             }
10173             gvec_fn = tcg_gen_gvec_shri;
10174         } else {
10175             /* Shift count the same size as element size produces all sign.  */
10176             if (shift == 8 << size) {
10177                 shift -= 1;
10178             }
10179             gvec_fn = tcg_gen_gvec_sari;
10180         }
10181         break;
10182 
10183     case 0x04: /* SRSHR / URSHR (rounding) */
10184         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10185         break;
10186 
10187     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10188         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10189         break;
10190 
10191     default:
10192         g_assert_not_reached();
10193     }
10194 
10195     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10196 }
10197 
10198 /* SHL/SLI - Vector shift left */
10199 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10200                                  int immh, int immb, int opcode, int rn, int rd)
10201 {
10202     int size = 32 - clz32(immh) - 1;
10203     int immhb = immh << 3 | immb;
10204     int shift = immhb - (8 << size);
10205 
10206     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10207     assert(size >= 0 && size <= 3);
10208 
10209     if (extract32(immh, 3, 1) && !is_q) {
10210         unallocated_encoding(s);
10211         return;
10212     }
10213 
10214     if (!fp_access_check(s)) {
10215         return;
10216     }
10217 
10218     if (insert) {
10219         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10220     } else {
10221         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10222     }
10223 }
10224 
10225 /* USHLL/SHLL - Vector shift left with widening */
10226 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10227                                  int immh, int immb, int opcode, int rn, int rd)
10228 {
10229     int size = 32 - clz32(immh) - 1;
10230     int immhb = immh << 3 | immb;
10231     int shift = immhb - (8 << size);
10232     int dsize = 64;
10233     int esize = 8 << size;
10234     int elements = dsize/esize;
10235     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10236     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10237     int i;
10238 
10239     if (size >= 3) {
10240         unallocated_encoding(s);
10241         return;
10242     }
10243 
10244     if (!fp_access_check(s)) {
10245         return;
10246     }
10247 
10248     /* For the LL variants the store is larger than the load,
10249      * so if rd == rn we would overwrite parts of our input.
10250      * So load everything right now and use shifts in the main loop.
10251      */
10252     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10253 
10254     for (i = 0; i < elements; i++) {
10255         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10256         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10257         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10258         write_vec_element(s, tcg_rd, rd, i, size + 1);
10259     }
10260 }
10261 
10262 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10263 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10264                                  int immh, int immb, int opcode, int rn, int rd)
10265 {
10266     int immhb = immh << 3 | immb;
10267     int size = 32 - clz32(immh) - 1;
10268     int dsize = 64;
10269     int esize = 8 << size;
10270     int elements = dsize/esize;
10271     int shift = (2 * esize) - immhb;
10272     bool round = extract32(opcode, 0, 1);
10273     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10274     TCGv_i64 tcg_round;
10275     int i;
10276 
10277     if (extract32(immh, 3, 1)) {
10278         unallocated_encoding(s);
10279         return;
10280     }
10281 
10282     if (!fp_access_check(s)) {
10283         return;
10284     }
10285 
10286     tcg_rn = tcg_temp_new_i64();
10287     tcg_rd = tcg_temp_new_i64();
10288     tcg_final = tcg_temp_new_i64();
10289     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10290 
10291     if (round) {
10292         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10293     } else {
10294         tcg_round = NULL;
10295     }
10296 
10297     for (i = 0; i < elements; i++) {
10298         read_vec_element(s, tcg_rn, rn, i, size+1);
10299         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10300                                 false, true, size+1, shift);
10301 
10302         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10303     }
10304 
10305     if (!is_q) {
10306         write_vec_element(s, tcg_final, rd, 0, MO_64);
10307     } else {
10308         write_vec_element(s, tcg_final, rd, 1, MO_64);
10309     }
10310 
10311     clear_vec_high(s, is_q, rd);
10312 }
10313 
10314 
10315 /* AdvSIMD shift by immediate
10316  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10317  * +---+---+---+-------------+------+------+--------+---+------+------+
10318  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10319  * +---+---+---+-------------+------+------+--------+---+------+------+
10320  */
10321 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10322 {
10323     int rd = extract32(insn, 0, 5);
10324     int rn = extract32(insn, 5, 5);
10325     int opcode = extract32(insn, 11, 5);
10326     int immb = extract32(insn, 16, 3);
10327     int immh = extract32(insn, 19, 4);
10328     bool is_u = extract32(insn, 29, 1);
10329     bool is_q = extract32(insn, 30, 1);
10330 
10331     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10332     assert(immh != 0);
10333 
10334     switch (opcode) {
10335     case 0x08: /* SRI */
10336         if (!is_u) {
10337             unallocated_encoding(s);
10338             return;
10339         }
10340         /* fall through */
10341     case 0x00: /* SSHR / USHR */
10342     case 0x02: /* SSRA / USRA (accumulate) */
10343     case 0x04: /* SRSHR / URSHR (rounding) */
10344     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10345         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10346         break;
10347     case 0x0a: /* SHL / SLI */
10348         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10349         break;
10350     case 0x10: /* SHRN */
10351     case 0x11: /* RSHRN / SQRSHRUN */
10352         if (is_u) {
10353             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10354                                    opcode, rn, rd);
10355         } else {
10356             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10357         }
10358         break;
10359     case 0x12: /* SQSHRN / UQSHRN */
10360     case 0x13: /* SQRSHRN / UQRSHRN */
10361         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10362                                opcode, rn, rd);
10363         break;
10364     case 0x14: /* SSHLL / USHLL */
10365         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10366         break;
10367     case 0x1c: /* SCVTF / UCVTF */
10368         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10369                                      opcode, rn, rd);
10370         break;
10371     case 0xc: /* SQSHLU */
10372         if (!is_u) {
10373             unallocated_encoding(s);
10374             return;
10375         }
10376         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10377         break;
10378     case 0xe: /* SQSHL, UQSHL */
10379         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10380         break;
10381     case 0x1f: /* FCVTZS/ FCVTZU */
10382         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10383         return;
10384     default:
10385         unallocated_encoding(s);
10386         return;
10387     }
10388 }
10389 
10390 /* Generate code to do a "long" addition or subtraction, ie one done in
10391  * TCGv_i64 on vector lanes twice the width specified by size.
10392  */
10393 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10394                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10395 {
10396     static NeonGenTwo64OpFn * const fns[3][2] = {
10397         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10398         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10399         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10400     };
10401     NeonGenTwo64OpFn *genfn;
10402     assert(size < 3);
10403 
10404     genfn = fns[size][is_sub];
10405     genfn(tcg_res, tcg_op1, tcg_op2);
10406 }
10407 
10408 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10409                                 int opcode, int rd, int rn, int rm)
10410 {
10411     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10412     TCGv_i64 tcg_res[2];
10413     int pass, accop;
10414 
10415     tcg_res[0] = tcg_temp_new_i64();
10416     tcg_res[1] = tcg_temp_new_i64();
10417 
10418     /* Does this op do an adding accumulate, a subtracting accumulate,
10419      * or no accumulate at all?
10420      */
10421     switch (opcode) {
10422     case 5:
10423     case 8:
10424     case 9:
10425         accop = 1;
10426         break;
10427     case 10:
10428     case 11:
10429         accop = -1;
10430         break;
10431     default:
10432         accop = 0;
10433         break;
10434     }
10435 
10436     if (accop != 0) {
10437         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10438         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10439     }
10440 
10441     /* size == 2 means two 32x32->64 operations; this is worth special
10442      * casing because we can generally handle it inline.
10443      */
10444     if (size == 2) {
10445         for (pass = 0; pass < 2; pass++) {
10446             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10447             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10448             TCGv_i64 tcg_passres;
10449             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10450 
10451             int elt = pass + is_q * 2;
10452 
10453             read_vec_element(s, tcg_op1, rn, elt, memop);
10454             read_vec_element(s, tcg_op2, rm, elt, memop);
10455 
10456             if (accop == 0) {
10457                 tcg_passres = tcg_res[pass];
10458             } else {
10459                 tcg_passres = tcg_temp_new_i64();
10460             }
10461 
10462             switch (opcode) {
10463             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10464                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10465                 break;
10466             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10467                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10468                 break;
10469             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10470             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10471             {
10472                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10473                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10474 
10475                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10476                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10477                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10478                                     tcg_passres,
10479                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10480                 break;
10481             }
10482             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10483             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10484             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10485                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10486                 break;
10487             case 9: /* SQDMLAL, SQDMLAL2 */
10488             case 11: /* SQDMLSL, SQDMLSL2 */
10489             case 13: /* SQDMULL, SQDMULL2 */
10490                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10491                 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10492                                                   tcg_passres, tcg_passres);
10493                 break;
10494             default:
10495                 g_assert_not_reached();
10496             }
10497 
10498             if (opcode == 9 || opcode == 11) {
10499                 /* saturating accumulate ops */
10500                 if (accop < 0) {
10501                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10502                 }
10503                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10504                                                   tcg_res[pass], tcg_passres);
10505             } else if (accop > 0) {
10506                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10507             } else if (accop < 0) {
10508                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10509             }
10510         }
10511     } else {
10512         /* size 0 or 1, generally helper functions */
10513         for (pass = 0; pass < 2; pass++) {
10514             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10515             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10516             TCGv_i64 tcg_passres;
10517             int elt = pass + is_q * 2;
10518 
10519             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10520             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10521 
10522             if (accop == 0) {
10523                 tcg_passres = tcg_res[pass];
10524             } else {
10525                 tcg_passres = tcg_temp_new_i64();
10526             }
10527 
10528             switch (opcode) {
10529             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10530             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10531             {
10532                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10533                 static NeonGenWidenFn * const widenfns[2][2] = {
10534                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10535                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10536                 };
10537                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10538 
10539                 widenfn(tcg_op2_64, tcg_op2);
10540                 widenfn(tcg_passres, tcg_op1);
10541                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10542                               tcg_passres, tcg_op2_64);
10543                 break;
10544             }
10545             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10546             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10547                 if (size == 0) {
10548                     if (is_u) {
10549                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10550                     } else {
10551                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10552                     }
10553                 } else {
10554                     if (is_u) {
10555                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10556                     } else {
10557                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10558                     }
10559                 }
10560                 break;
10561             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10562             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10563             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10564                 if (size == 0) {
10565                     if (is_u) {
10566                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10567                     } else {
10568                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10569                     }
10570                 } else {
10571                     if (is_u) {
10572                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10573                     } else {
10574                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10575                     }
10576                 }
10577                 break;
10578             case 9: /* SQDMLAL, SQDMLAL2 */
10579             case 11: /* SQDMLSL, SQDMLSL2 */
10580             case 13: /* SQDMULL, SQDMULL2 */
10581                 assert(size == 1);
10582                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10583                 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10584                                                   tcg_passres, tcg_passres);
10585                 break;
10586             default:
10587                 g_assert_not_reached();
10588             }
10589 
10590             if (accop != 0) {
10591                 if (opcode == 9 || opcode == 11) {
10592                     /* saturating accumulate ops */
10593                     if (accop < 0) {
10594                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10595                     }
10596                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10597                                                       tcg_res[pass],
10598                                                       tcg_passres);
10599                 } else {
10600                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10601                                   tcg_res[pass], tcg_passres);
10602                 }
10603             }
10604         }
10605     }
10606 
10607     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10608     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10609 }
10610 
10611 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10612                             int opcode, int rd, int rn, int rm)
10613 {
10614     TCGv_i64 tcg_res[2];
10615     int part = is_q ? 2 : 0;
10616     int pass;
10617 
10618     for (pass = 0; pass < 2; pass++) {
10619         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10620         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10621         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10622         static NeonGenWidenFn * const widenfns[3][2] = {
10623             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10624             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10625             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10626         };
10627         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10628 
10629         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10630         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10631         widenfn(tcg_op2_wide, tcg_op2);
10632         tcg_res[pass] = tcg_temp_new_i64();
10633         gen_neon_addl(size, (opcode == 3),
10634                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10635     }
10636 
10637     for (pass = 0; pass < 2; pass++) {
10638         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10639     }
10640 }
10641 
10642 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10643 {
10644     tcg_gen_addi_i64(in, in, 1U << 31);
10645     tcg_gen_extrh_i64_i32(res, in);
10646 }
10647 
10648 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10649                                  int opcode, int rd, int rn, int rm)
10650 {
10651     TCGv_i32 tcg_res[2];
10652     int part = is_q ? 2 : 0;
10653     int pass;
10654 
10655     for (pass = 0; pass < 2; pass++) {
10656         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10657         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10658         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10659         static NeonGenNarrowFn * const narrowfns[3][2] = {
10660             { gen_helper_neon_narrow_high_u8,
10661               gen_helper_neon_narrow_round_high_u8 },
10662             { gen_helper_neon_narrow_high_u16,
10663               gen_helper_neon_narrow_round_high_u16 },
10664             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10665         };
10666         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10667 
10668         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10669         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10670 
10671         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10672 
10673         tcg_res[pass] = tcg_temp_new_i32();
10674         gennarrow(tcg_res[pass], tcg_wideres);
10675     }
10676 
10677     for (pass = 0; pass < 2; pass++) {
10678         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10679     }
10680     clear_vec_high(s, is_q, rd);
10681 }
10682 
10683 /* AdvSIMD three different
10684  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10685  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10686  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10687  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10688  */
10689 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10690 {
10691     /* Instructions in this group fall into three basic classes
10692      * (in each case with the operation working on each element in
10693      * the input vectors):
10694      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10695      *     128 bit input)
10696      * (2) wide 64 x 128 -> 128
10697      * (3) narrowing 128 x 128 -> 64
10698      * Here we do initial decode, catch unallocated cases and
10699      * dispatch to separate functions for each class.
10700      */
10701     int is_q = extract32(insn, 30, 1);
10702     int is_u = extract32(insn, 29, 1);
10703     int size = extract32(insn, 22, 2);
10704     int opcode = extract32(insn, 12, 4);
10705     int rm = extract32(insn, 16, 5);
10706     int rn = extract32(insn, 5, 5);
10707     int rd = extract32(insn, 0, 5);
10708 
10709     switch (opcode) {
10710     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10711     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10712         /* 64 x 128 -> 128 */
10713         if (size == 3) {
10714             unallocated_encoding(s);
10715             return;
10716         }
10717         if (!fp_access_check(s)) {
10718             return;
10719         }
10720         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10721         break;
10722     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10723     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10724         /* 128 x 128 -> 64 */
10725         if (size == 3) {
10726             unallocated_encoding(s);
10727             return;
10728         }
10729         if (!fp_access_check(s)) {
10730             return;
10731         }
10732         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10733         break;
10734     case 14: /* PMULL, PMULL2 */
10735         if (is_u) {
10736             unallocated_encoding(s);
10737             return;
10738         }
10739         switch (size) {
10740         case 0: /* PMULL.P8 */
10741             if (!fp_access_check(s)) {
10742                 return;
10743             }
10744             /* The Q field specifies lo/hi half input for this insn.  */
10745             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10746                              gen_helper_neon_pmull_h);
10747             break;
10748 
10749         case 3: /* PMULL.P64 */
10750             if (!dc_isar_feature(aa64_pmull, s)) {
10751                 unallocated_encoding(s);
10752                 return;
10753             }
10754             if (!fp_access_check(s)) {
10755                 return;
10756             }
10757             /* The Q field specifies lo/hi half input for this insn.  */
10758             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10759                              gen_helper_gvec_pmull_q);
10760             break;
10761 
10762         default:
10763             unallocated_encoding(s);
10764             break;
10765         }
10766         return;
10767     case 9: /* SQDMLAL, SQDMLAL2 */
10768     case 11: /* SQDMLSL, SQDMLSL2 */
10769     case 13: /* SQDMULL, SQDMULL2 */
10770         if (is_u || size == 0) {
10771             unallocated_encoding(s);
10772             return;
10773         }
10774         /* fall through */
10775     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10776     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10777     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10778     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10779     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10780     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10781     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10782         /* 64 x 64 -> 128 */
10783         if (size == 3) {
10784             unallocated_encoding(s);
10785             return;
10786         }
10787         if (!fp_access_check(s)) {
10788             return;
10789         }
10790 
10791         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10792         break;
10793     default:
10794         /* opcode 15 not allocated */
10795         unallocated_encoding(s);
10796         break;
10797     }
10798 }
10799 
10800 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10801 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10802 {
10803     int rd = extract32(insn, 0, 5);
10804     int rn = extract32(insn, 5, 5);
10805     int rm = extract32(insn, 16, 5);
10806     int size = extract32(insn, 22, 2);
10807     bool is_u = extract32(insn, 29, 1);
10808     bool is_q = extract32(insn, 30, 1);
10809 
10810     if (!fp_access_check(s)) {
10811         return;
10812     }
10813 
10814     switch (size + 4 * is_u) {
10815     case 0: /* AND */
10816         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10817         return;
10818     case 1: /* BIC */
10819         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10820         return;
10821     case 2: /* ORR */
10822         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10823         return;
10824     case 3: /* ORN */
10825         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10826         return;
10827     case 4: /* EOR */
10828         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10829         return;
10830 
10831     case 5: /* BSL bitwise select */
10832         gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
10833         return;
10834     case 6: /* BIT, bitwise insert if true */
10835         gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
10836         return;
10837     case 7: /* BIF, bitwise insert if false */
10838         gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
10839         return;
10840 
10841     default:
10842         g_assert_not_reached();
10843     }
10844 }
10845 
10846 /* Pairwise op subgroup of C3.6.16.
10847  *
10848  * This is called directly or via the handle_3same_float for float pairwise
10849  * operations where the opcode and size are calculated differently.
10850  */
10851 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
10852                                    int size, int rn, int rm, int rd)
10853 {
10854     TCGv_ptr fpst;
10855     int pass;
10856 
10857     /* Floating point operations need fpst */
10858     if (opcode >= 0x58) {
10859         fpst = fpstatus_ptr(FPST_FPCR);
10860     } else {
10861         fpst = NULL;
10862     }
10863 
10864     if (!fp_access_check(s)) {
10865         return;
10866     }
10867 
10868     /* These operations work on the concatenated rm:rn, with each pair of
10869      * adjacent elements being operated on to produce an element in the result.
10870      */
10871     if (size == 3) {
10872         TCGv_i64 tcg_res[2];
10873 
10874         for (pass = 0; pass < 2; pass++) {
10875             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10876             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10877             int passreg = (pass == 0) ? rn : rm;
10878 
10879             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
10880             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
10881             tcg_res[pass] = tcg_temp_new_i64();
10882 
10883             switch (opcode) {
10884             case 0x17: /* ADDP */
10885                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
10886                 break;
10887             case 0x58: /* FMAXNMP */
10888                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10889                 break;
10890             case 0x5a: /* FADDP */
10891                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10892                 break;
10893             case 0x5e: /* FMAXP */
10894                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10895                 break;
10896             case 0x78: /* FMINNMP */
10897                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10898                 break;
10899             case 0x7e: /* FMINP */
10900                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10901                 break;
10902             default:
10903                 g_assert_not_reached();
10904             }
10905         }
10906 
10907         for (pass = 0; pass < 2; pass++) {
10908             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10909         }
10910     } else {
10911         int maxpass = is_q ? 4 : 2;
10912         TCGv_i32 tcg_res[4];
10913 
10914         for (pass = 0; pass < maxpass; pass++) {
10915             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10916             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10917             NeonGenTwoOpFn *genfn = NULL;
10918             int passreg = pass < (maxpass / 2) ? rn : rm;
10919             int passelt = (is_q && (pass & 1)) ? 2 : 0;
10920 
10921             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
10922             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
10923             tcg_res[pass] = tcg_temp_new_i32();
10924 
10925             switch (opcode) {
10926             case 0x17: /* ADDP */
10927             {
10928                 static NeonGenTwoOpFn * const fns[3] = {
10929                     gen_helper_neon_padd_u8,
10930                     gen_helper_neon_padd_u16,
10931                     tcg_gen_add_i32,
10932                 };
10933                 genfn = fns[size];
10934                 break;
10935             }
10936             case 0x14: /* SMAXP, UMAXP */
10937             {
10938                 static NeonGenTwoOpFn * const fns[3][2] = {
10939                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
10940                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
10941                     { tcg_gen_smax_i32, tcg_gen_umax_i32 },
10942                 };
10943                 genfn = fns[size][u];
10944                 break;
10945             }
10946             case 0x15: /* SMINP, UMINP */
10947             {
10948                 static NeonGenTwoOpFn * const fns[3][2] = {
10949                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
10950                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
10951                     { tcg_gen_smin_i32, tcg_gen_umin_i32 },
10952                 };
10953                 genfn = fns[size][u];
10954                 break;
10955             }
10956             /* The FP operations are all on single floats (32 bit) */
10957             case 0x58: /* FMAXNMP */
10958                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10959                 break;
10960             case 0x5a: /* FADDP */
10961                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10962                 break;
10963             case 0x5e: /* FMAXP */
10964                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10965                 break;
10966             case 0x78: /* FMINNMP */
10967                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10968                 break;
10969             case 0x7e: /* FMINP */
10970                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10971                 break;
10972             default:
10973                 g_assert_not_reached();
10974             }
10975 
10976             /* FP ops called directly, otherwise call now */
10977             if (genfn) {
10978                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
10979             }
10980         }
10981 
10982         for (pass = 0; pass < maxpass; pass++) {
10983             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
10984         }
10985         clear_vec_high(s, is_q, rd);
10986     }
10987 }
10988 
10989 /* Floating point op subgroup of C3.6.16. */
10990 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
10991 {
10992     /* For floating point ops, the U, size[1] and opcode bits
10993      * together indicate the operation. size[0] indicates single
10994      * or double.
10995      */
10996     int fpopcode = extract32(insn, 11, 5)
10997         | (extract32(insn, 23, 1) << 5)
10998         | (extract32(insn, 29, 1) << 6);
10999     int is_q = extract32(insn, 30, 1);
11000     int size = extract32(insn, 22, 1);
11001     int rm = extract32(insn, 16, 5);
11002     int rn = extract32(insn, 5, 5);
11003     int rd = extract32(insn, 0, 5);
11004 
11005     int datasize = is_q ? 128 : 64;
11006     int esize = 32 << size;
11007     int elements = datasize / esize;
11008 
11009     if (size == 1 && !is_q) {
11010         unallocated_encoding(s);
11011         return;
11012     }
11013 
11014     switch (fpopcode) {
11015     case 0x58: /* FMAXNMP */
11016     case 0x5a: /* FADDP */
11017     case 0x5e: /* FMAXP */
11018     case 0x78: /* FMINNMP */
11019     case 0x7e: /* FMINP */
11020         if (size && !is_q) {
11021             unallocated_encoding(s);
11022             return;
11023         }
11024         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11025                                rn, rm, rd);
11026         return;
11027     case 0x1b: /* FMULX */
11028     case 0x1f: /* FRECPS */
11029     case 0x3f: /* FRSQRTS */
11030     case 0x5d: /* FACGE */
11031     case 0x7d: /* FACGT */
11032     case 0x19: /* FMLA */
11033     case 0x39: /* FMLS */
11034     case 0x18: /* FMAXNM */
11035     case 0x1a: /* FADD */
11036     case 0x1c: /* FCMEQ */
11037     case 0x1e: /* FMAX */
11038     case 0x38: /* FMINNM */
11039     case 0x3a: /* FSUB */
11040     case 0x3e: /* FMIN */
11041     case 0x5b: /* FMUL */
11042     case 0x5c: /* FCMGE */
11043     case 0x5f: /* FDIV */
11044     case 0x7a: /* FABD */
11045     case 0x7c: /* FCMGT */
11046         if (!fp_access_check(s)) {
11047             return;
11048         }
11049         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11050         return;
11051 
11052     case 0x1d: /* FMLAL  */
11053     case 0x3d: /* FMLSL  */
11054     case 0x59: /* FMLAL2 */
11055     case 0x79: /* FMLSL2 */
11056         if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11057             unallocated_encoding(s);
11058             return;
11059         }
11060         if (fp_access_check(s)) {
11061             int is_s = extract32(insn, 23, 1);
11062             int is_2 = extract32(insn, 29, 1);
11063             int data = (is_2 << 1) | is_s;
11064             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11065                                vec_full_reg_offset(s, rn),
11066                                vec_full_reg_offset(s, rm), cpu_env,
11067                                is_q ? 16 : 8, vec_full_reg_size(s),
11068                                data, gen_helper_gvec_fmlal_a64);
11069         }
11070         return;
11071 
11072     default:
11073         unallocated_encoding(s);
11074         return;
11075     }
11076 }
11077 
11078 /* Integer op subgroup of C3.6.16. */
11079 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11080 {
11081     int is_q = extract32(insn, 30, 1);
11082     int u = extract32(insn, 29, 1);
11083     int size = extract32(insn, 22, 2);
11084     int opcode = extract32(insn, 11, 5);
11085     int rm = extract32(insn, 16, 5);
11086     int rn = extract32(insn, 5, 5);
11087     int rd = extract32(insn, 0, 5);
11088     int pass;
11089     TCGCond cond;
11090 
11091     switch (opcode) {
11092     case 0x13: /* MUL, PMUL */
11093         if (u && size != 0) {
11094             unallocated_encoding(s);
11095             return;
11096         }
11097         /* fall through */
11098     case 0x0: /* SHADD, UHADD */
11099     case 0x2: /* SRHADD, URHADD */
11100     case 0x4: /* SHSUB, UHSUB */
11101     case 0xc: /* SMAX, UMAX */
11102     case 0xd: /* SMIN, UMIN */
11103     case 0xe: /* SABD, UABD */
11104     case 0xf: /* SABA, UABA */
11105     case 0x12: /* MLA, MLS */
11106         if (size == 3) {
11107             unallocated_encoding(s);
11108             return;
11109         }
11110         break;
11111     case 0x16: /* SQDMULH, SQRDMULH */
11112         if (size == 0 || size == 3) {
11113             unallocated_encoding(s);
11114             return;
11115         }
11116         break;
11117     default:
11118         if (size == 3 && !is_q) {
11119             unallocated_encoding(s);
11120             return;
11121         }
11122         break;
11123     }
11124 
11125     if (!fp_access_check(s)) {
11126         return;
11127     }
11128 
11129     switch (opcode) {
11130     case 0x01: /* SQADD, UQADD */
11131         if (u) {
11132             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11133         } else {
11134             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11135         }
11136         return;
11137     case 0x05: /* SQSUB, UQSUB */
11138         if (u) {
11139             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11140         } else {
11141             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11142         }
11143         return;
11144     case 0x08: /* SSHL, USHL */
11145         if (u) {
11146             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11147         } else {
11148             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11149         }
11150         return;
11151     case 0x0c: /* SMAX, UMAX */
11152         if (u) {
11153             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11154         } else {
11155             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11156         }
11157         return;
11158     case 0x0d: /* SMIN, UMIN */
11159         if (u) {
11160             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11161         } else {
11162             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11163         }
11164         return;
11165     case 0xe: /* SABD, UABD */
11166         if (u) {
11167             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11168         } else {
11169             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11170         }
11171         return;
11172     case 0xf: /* SABA, UABA */
11173         if (u) {
11174             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11175         } else {
11176             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11177         }
11178         return;
11179     case 0x10: /* ADD, SUB */
11180         if (u) {
11181             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11182         } else {
11183             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11184         }
11185         return;
11186     case 0x13: /* MUL, PMUL */
11187         if (!u) { /* MUL */
11188             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11189         } else {  /* PMUL */
11190             gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11191         }
11192         return;
11193     case 0x12: /* MLA, MLS */
11194         if (u) {
11195             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11196         } else {
11197             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11198         }
11199         return;
11200     case 0x16: /* SQDMULH, SQRDMULH */
11201         {
11202             static gen_helper_gvec_3_ptr * const fns[2][2] = {
11203                 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11204                 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11205             };
11206             gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11207         }
11208         return;
11209     case 0x11:
11210         if (!u) { /* CMTST */
11211             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11212             return;
11213         }
11214         /* else CMEQ */
11215         cond = TCG_COND_EQ;
11216         goto do_gvec_cmp;
11217     case 0x06: /* CMGT, CMHI */
11218         cond = u ? TCG_COND_GTU : TCG_COND_GT;
11219         goto do_gvec_cmp;
11220     case 0x07: /* CMGE, CMHS */
11221         cond = u ? TCG_COND_GEU : TCG_COND_GE;
11222     do_gvec_cmp:
11223         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11224                          vec_full_reg_offset(s, rn),
11225                          vec_full_reg_offset(s, rm),
11226                          is_q ? 16 : 8, vec_full_reg_size(s));
11227         return;
11228     }
11229 
11230     if (size == 3) {
11231         assert(is_q);
11232         for (pass = 0; pass < 2; pass++) {
11233             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11234             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11235             TCGv_i64 tcg_res = tcg_temp_new_i64();
11236 
11237             read_vec_element(s, tcg_op1, rn, pass, MO_64);
11238             read_vec_element(s, tcg_op2, rm, pass, MO_64);
11239 
11240             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11241 
11242             write_vec_element(s, tcg_res, rd, pass, MO_64);
11243         }
11244     } else {
11245         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11246             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11247             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11248             TCGv_i32 tcg_res = tcg_temp_new_i32();
11249             NeonGenTwoOpFn *genfn = NULL;
11250             NeonGenTwoOpEnvFn *genenvfn = NULL;
11251 
11252             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11253             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11254 
11255             switch (opcode) {
11256             case 0x0: /* SHADD, UHADD */
11257             {
11258                 static NeonGenTwoOpFn * const fns[3][2] = {
11259                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11260                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11261                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11262                 };
11263                 genfn = fns[size][u];
11264                 break;
11265             }
11266             case 0x2: /* SRHADD, URHADD */
11267             {
11268                 static NeonGenTwoOpFn * const fns[3][2] = {
11269                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11270                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11271                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11272                 };
11273                 genfn = fns[size][u];
11274                 break;
11275             }
11276             case 0x4: /* SHSUB, UHSUB */
11277             {
11278                 static NeonGenTwoOpFn * const fns[3][2] = {
11279                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11280                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11281                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11282                 };
11283                 genfn = fns[size][u];
11284                 break;
11285             }
11286             case 0x9: /* SQSHL, UQSHL */
11287             {
11288                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11289                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11290                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11291                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11292                 };
11293                 genenvfn = fns[size][u];
11294                 break;
11295             }
11296             case 0xa: /* SRSHL, URSHL */
11297             {
11298                 static NeonGenTwoOpFn * const fns[3][2] = {
11299                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11300                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11301                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11302                 };
11303                 genfn = fns[size][u];
11304                 break;
11305             }
11306             case 0xb: /* SQRSHL, UQRSHL */
11307             {
11308                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11309                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11310                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11311                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11312                 };
11313                 genenvfn = fns[size][u];
11314                 break;
11315             }
11316             default:
11317                 g_assert_not_reached();
11318             }
11319 
11320             if (genenvfn) {
11321                 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11322             } else {
11323                 genfn(tcg_res, tcg_op1, tcg_op2);
11324             }
11325 
11326             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11327         }
11328     }
11329     clear_vec_high(s, is_q, rd);
11330 }
11331 
11332 /* AdvSIMD three same
11333  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
11334  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11335  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
11336  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11337  */
11338 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11339 {
11340     int opcode = extract32(insn, 11, 5);
11341 
11342     switch (opcode) {
11343     case 0x3: /* logic ops */
11344         disas_simd_3same_logic(s, insn);
11345         break;
11346     case 0x17: /* ADDP */
11347     case 0x14: /* SMAXP, UMAXP */
11348     case 0x15: /* SMINP, UMINP */
11349     {
11350         /* Pairwise operations */
11351         int is_q = extract32(insn, 30, 1);
11352         int u = extract32(insn, 29, 1);
11353         int size = extract32(insn, 22, 2);
11354         int rm = extract32(insn, 16, 5);
11355         int rn = extract32(insn, 5, 5);
11356         int rd = extract32(insn, 0, 5);
11357         if (opcode == 0x17) {
11358             if (u || (size == 3 && !is_q)) {
11359                 unallocated_encoding(s);
11360                 return;
11361             }
11362         } else {
11363             if (size == 3) {
11364                 unallocated_encoding(s);
11365                 return;
11366             }
11367         }
11368         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11369         break;
11370     }
11371     case 0x18 ... 0x31:
11372         /* floating point ops, sz[1] and U are part of opcode */
11373         disas_simd_3same_float(s, insn);
11374         break;
11375     default:
11376         disas_simd_3same_int(s, insn);
11377         break;
11378     }
11379 }
11380 
11381 /*
11382  * Advanced SIMD three same (ARMv8.2 FP16 variants)
11383  *
11384  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
11385  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11386  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
11387  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11388  *
11389  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11390  * (register), FACGE, FABD, FCMGT (register) and FACGT.
11391  *
11392  */
11393 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11394 {
11395     int opcode = extract32(insn, 11, 3);
11396     int u = extract32(insn, 29, 1);
11397     int a = extract32(insn, 23, 1);
11398     int is_q = extract32(insn, 30, 1);
11399     int rm = extract32(insn, 16, 5);
11400     int rn = extract32(insn, 5, 5);
11401     int rd = extract32(insn, 0, 5);
11402     /*
11403      * For these floating point ops, the U, a and opcode bits
11404      * together indicate the operation.
11405      */
11406     int fpopcode = opcode | (a << 3) | (u << 4);
11407     int datasize = is_q ? 128 : 64;
11408     int elements = datasize / 16;
11409     bool pairwise;
11410     TCGv_ptr fpst;
11411     int pass;
11412 
11413     switch (fpopcode) {
11414     case 0x0: /* FMAXNM */
11415     case 0x1: /* FMLA */
11416     case 0x2: /* FADD */
11417     case 0x3: /* FMULX */
11418     case 0x4: /* FCMEQ */
11419     case 0x6: /* FMAX */
11420     case 0x7: /* FRECPS */
11421     case 0x8: /* FMINNM */
11422     case 0x9: /* FMLS */
11423     case 0xa: /* FSUB */
11424     case 0xe: /* FMIN */
11425     case 0xf: /* FRSQRTS */
11426     case 0x13: /* FMUL */
11427     case 0x14: /* FCMGE */
11428     case 0x15: /* FACGE */
11429     case 0x17: /* FDIV */
11430     case 0x1a: /* FABD */
11431     case 0x1c: /* FCMGT */
11432     case 0x1d: /* FACGT */
11433         pairwise = false;
11434         break;
11435     case 0x10: /* FMAXNMP */
11436     case 0x12: /* FADDP */
11437     case 0x16: /* FMAXP */
11438     case 0x18: /* FMINNMP */
11439     case 0x1e: /* FMINP */
11440         pairwise = true;
11441         break;
11442     default:
11443         unallocated_encoding(s);
11444         return;
11445     }
11446 
11447     if (!dc_isar_feature(aa64_fp16, s)) {
11448         unallocated_encoding(s);
11449         return;
11450     }
11451 
11452     if (!fp_access_check(s)) {
11453         return;
11454     }
11455 
11456     fpst = fpstatus_ptr(FPST_FPCR_F16);
11457 
11458     if (pairwise) {
11459         int maxpass = is_q ? 8 : 4;
11460         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11461         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11462         TCGv_i32 tcg_res[8];
11463 
11464         for (pass = 0; pass < maxpass; pass++) {
11465             int passreg = pass < (maxpass / 2) ? rn : rm;
11466             int passelt = (pass << 1) & (maxpass - 1);
11467 
11468             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11469             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11470             tcg_res[pass] = tcg_temp_new_i32();
11471 
11472             switch (fpopcode) {
11473             case 0x10: /* FMAXNMP */
11474                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11475                                            fpst);
11476                 break;
11477             case 0x12: /* FADDP */
11478                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11479                 break;
11480             case 0x16: /* FMAXP */
11481                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11482                 break;
11483             case 0x18: /* FMINNMP */
11484                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11485                                            fpst);
11486                 break;
11487             case 0x1e: /* FMINP */
11488                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11489                 break;
11490             default:
11491                 g_assert_not_reached();
11492             }
11493         }
11494 
11495         for (pass = 0; pass < maxpass; pass++) {
11496             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11497         }
11498     } else {
11499         for (pass = 0; pass < elements; pass++) {
11500             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11501             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11502             TCGv_i32 tcg_res = tcg_temp_new_i32();
11503 
11504             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11505             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11506 
11507             switch (fpopcode) {
11508             case 0x0: /* FMAXNM */
11509                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11510                 break;
11511             case 0x1: /* FMLA */
11512                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11513                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11514                                            fpst);
11515                 break;
11516             case 0x2: /* FADD */
11517                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11518                 break;
11519             case 0x3: /* FMULX */
11520                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11521                 break;
11522             case 0x4: /* FCMEQ */
11523                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11524                 break;
11525             case 0x6: /* FMAX */
11526                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11527                 break;
11528             case 0x7: /* FRECPS */
11529                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11530                 break;
11531             case 0x8: /* FMINNM */
11532                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11533                 break;
11534             case 0x9: /* FMLS */
11535                 /* As usual for ARM, separate negation for fused multiply-add */
11536                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11537                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11538                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11539                                            fpst);
11540                 break;
11541             case 0xa: /* FSUB */
11542                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11543                 break;
11544             case 0xe: /* FMIN */
11545                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11546                 break;
11547             case 0xf: /* FRSQRTS */
11548                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11549                 break;
11550             case 0x13: /* FMUL */
11551                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11552                 break;
11553             case 0x14: /* FCMGE */
11554                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11555                 break;
11556             case 0x15: /* FACGE */
11557                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11558                 break;
11559             case 0x17: /* FDIV */
11560                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11561                 break;
11562             case 0x1a: /* FABD */
11563                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11564                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11565                 break;
11566             case 0x1c: /* FCMGT */
11567                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11568                 break;
11569             case 0x1d: /* FACGT */
11570                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11571                 break;
11572             default:
11573                 g_assert_not_reached();
11574             }
11575 
11576             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11577         }
11578     }
11579 
11580     clear_vec_high(s, is_q, rd);
11581 }
11582 
11583 /* AdvSIMD three same extra
11584  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
11585  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11586  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
11587  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11588  */
11589 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11590 {
11591     int rd = extract32(insn, 0, 5);
11592     int rn = extract32(insn, 5, 5);
11593     int opcode = extract32(insn, 11, 4);
11594     int rm = extract32(insn, 16, 5);
11595     int size = extract32(insn, 22, 2);
11596     bool u = extract32(insn, 29, 1);
11597     bool is_q = extract32(insn, 30, 1);
11598     bool feature;
11599     int rot;
11600 
11601     switch (u * 16 + opcode) {
11602     case 0x10: /* SQRDMLAH (vector) */
11603     case 0x11: /* SQRDMLSH (vector) */
11604         if (size != 1 && size != 2) {
11605             unallocated_encoding(s);
11606             return;
11607         }
11608         feature = dc_isar_feature(aa64_rdm, s);
11609         break;
11610     case 0x02: /* SDOT (vector) */
11611     case 0x12: /* UDOT (vector) */
11612         if (size != MO_32) {
11613             unallocated_encoding(s);
11614             return;
11615         }
11616         feature = dc_isar_feature(aa64_dp, s);
11617         break;
11618     case 0x03: /* USDOT */
11619         if (size != MO_32) {
11620             unallocated_encoding(s);
11621             return;
11622         }
11623         feature = dc_isar_feature(aa64_i8mm, s);
11624         break;
11625     case 0x04: /* SMMLA */
11626     case 0x14: /* UMMLA */
11627     case 0x05: /* USMMLA */
11628         if (!is_q || size != MO_32) {
11629             unallocated_encoding(s);
11630             return;
11631         }
11632         feature = dc_isar_feature(aa64_i8mm, s);
11633         break;
11634     case 0x18: /* FCMLA, #0 */
11635     case 0x19: /* FCMLA, #90 */
11636     case 0x1a: /* FCMLA, #180 */
11637     case 0x1b: /* FCMLA, #270 */
11638     case 0x1c: /* FCADD, #90 */
11639     case 0x1e: /* FCADD, #270 */
11640         if (size == 0
11641             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11642             || (size == 3 && !is_q)) {
11643             unallocated_encoding(s);
11644             return;
11645         }
11646         feature = dc_isar_feature(aa64_fcma, s);
11647         break;
11648     case 0x1d: /* BFMMLA */
11649         if (size != MO_16 || !is_q) {
11650             unallocated_encoding(s);
11651             return;
11652         }
11653         feature = dc_isar_feature(aa64_bf16, s);
11654         break;
11655     case 0x1f:
11656         switch (size) {
11657         case 1: /* BFDOT */
11658         case 3: /* BFMLAL{B,T} */
11659             feature = dc_isar_feature(aa64_bf16, s);
11660             break;
11661         default:
11662             unallocated_encoding(s);
11663             return;
11664         }
11665         break;
11666     default:
11667         unallocated_encoding(s);
11668         return;
11669     }
11670     if (!feature) {
11671         unallocated_encoding(s);
11672         return;
11673     }
11674     if (!fp_access_check(s)) {
11675         return;
11676     }
11677 
11678     switch (opcode) {
11679     case 0x0: /* SQRDMLAH (vector) */
11680         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11681         return;
11682 
11683     case 0x1: /* SQRDMLSH (vector) */
11684         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11685         return;
11686 
11687     case 0x2: /* SDOT / UDOT */
11688         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11689                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11690         return;
11691 
11692     case 0x3: /* USDOT */
11693         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11694         return;
11695 
11696     case 0x04: /* SMMLA, UMMLA */
11697         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11698                          u ? gen_helper_gvec_ummla_b
11699                          : gen_helper_gvec_smmla_b);
11700         return;
11701     case 0x05: /* USMMLA */
11702         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11703         return;
11704 
11705     case 0x8: /* FCMLA, #0 */
11706     case 0x9: /* FCMLA, #90 */
11707     case 0xa: /* FCMLA, #180 */
11708     case 0xb: /* FCMLA, #270 */
11709         rot = extract32(opcode, 0, 2);
11710         switch (size) {
11711         case 1:
11712             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11713                               gen_helper_gvec_fcmlah);
11714             break;
11715         case 2:
11716             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11717                               gen_helper_gvec_fcmlas);
11718             break;
11719         case 3:
11720             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11721                               gen_helper_gvec_fcmlad);
11722             break;
11723         default:
11724             g_assert_not_reached();
11725         }
11726         return;
11727 
11728     case 0xc: /* FCADD, #90 */
11729     case 0xe: /* FCADD, #270 */
11730         rot = extract32(opcode, 1, 1);
11731         switch (size) {
11732         case 1:
11733             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11734                               gen_helper_gvec_fcaddh);
11735             break;
11736         case 2:
11737             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11738                               gen_helper_gvec_fcadds);
11739             break;
11740         case 3:
11741             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11742                               gen_helper_gvec_fcaddd);
11743             break;
11744         default:
11745             g_assert_not_reached();
11746         }
11747         return;
11748 
11749     case 0xd: /* BFMMLA */
11750         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11751         return;
11752     case 0xf:
11753         switch (size) {
11754         case 1: /* BFDOT */
11755             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11756             break;
11757         case 3: /* BFMLAL{B,T} */
11758             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11759                               gen_helper_gvec_bfmlal);
11760             break;
11761         default:
11762             g_assert_not_reached();
11763         }
11764         return;
11765 
11766     default:
11767         g_assert_not_reached();
11768     }
11769 }
11770 
11771 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11772                                   int size, int rn, int rd)
11773 {
11774     /* Handle 2-reg-misc ops which are widening (so each size element
11775      * in the source becomes a 2*size element in the destination.
11776      * The only instruction like this is FCVTL.
11777      */
11778     int pass;
11779 
11780     if (size == 3) {
11781         /* 32 -> 64 bit fp conversion */
11782         TCGv_i64 tcg_res[2];
11783         int srcelt = is_q ? 2 : 0;
11784 
11785         for (pass = 0; pass < 2; pass++) {
11786             TCGv_i32 tcg_op = tcg_temp_new_i32();
11787             tcg_res[pass] = tcg_temp_new_i64();
11788 
11789             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11790             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
11791         }
11792         for (pass = 0; pass < 2; pass++) {
11793             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11794         }
11795     } else {
11796         /* 16 -> 32 bit fp conversion */
11797         int srcelt = is_q ? 4 : 0;
11798         TCGv_i32 tcg_res[4];
11799         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11800         TCGv_i32 ahp = get_ahp_flag();
11801 
11802         for (pass = 0; pass < 4; pass++) {
11803             tcg_res[pass] = tcg_temp_new_i32();
11804 
11805             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11806             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11807                                            fpst, ahp);
11808         }
11809         for (pass = 0; pass < 4; pass++) {
11810             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11811         }
11812     }
11813 }
11814 
11815 static void handle_rev(DisasContext *s, int opcode, bool u,
11816                        bool is_q, int size, int rn, int rd)
11817 {
11818     int op = (opcode << 1) | u;
11819     int opsz = op + size;
11820     int grp_size = 3 - opsz;
11821     int dsize = is_q ? 128 : 64;
11822     int i;
11823 
11824     if (opsz >= 3) {
11825         unallocated_encoding(s);
11826         return;
11827     }
11828 
11829     if (!fp_access_check(s)) {
11830         return;
11831     }
11832 
11833     if (size == 0) {
11834         /* Special case bytes, use bswap op on each group of elements */
11835         int groups = dsize / (8 << grp_size);
11836 
11837         for (i = 0; i < groups; i++) {
11838             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11839 
11840             read_vec_element(s, tcg_tmp, rn, i, grp_size);
11841             switch (grp_size) {
11842             case MO_16:
11843                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11844                 break;
11845             case MO_32:
11846                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11847                 break;
11848             case MO_64:
11849                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11850                 break;
11851             default:
11852                 g_assert_not_reached();
11853             }
11854             write_vec_element(s, tcg_tmp, rd, i, grp_size);
11855         }
11856         clear_vec_high(s, is_q, rd);
11857     } else {
11858         int revmask = (1 << grp_size) - 1;
11859         int esize = 8 << size;
11860         int elements = dsize / esize;
11861         TCGv_i64 tcg_rn = tcg_temp_new_i64();
11862         TCGv_i64 tcg_rd[2];
11863 
11864         for (i = 0; i < 2; i++) {
11865             tcg_rd[i] = tcg_temp_new_i64();
11866             tcg_gen_movi_i64(tcg_rd[i], 0);
11867         }
11868 
11869         for (i = 0; i < elements; i++) {
11870             int e_rev = (i & 0xf) ^ revmask;
11871             int w = (e_rev * esize) / 64;
11872             int o = (e_rev * esize) % 64;
11873 
11874             read_vec_element(s, tcg_rn, rn, i, size);
11875             tcg_gen_deposit_i64(tcg_rd[w], tcg_rd[w], tcg_rn, o, esize);
11876         }
11877 
11878         for (i = 0; i < 2; i++) {
11879             write_vec_element(s, tcg_rd[i], rd, i, MO_64);
11880         }
11881         clear_vec_high(s, true, rd);
11882     }
11883 }
11884 
11885 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
11886                                   bool is_q, int size, int rn, int rd)
11887 {
11888     /* Implement the pairwise operations from 2-misc:
11889      * SADDLP, UADDLP, SADALP, UADALP.
11890      * These all add pairs of elements in the input to produce a
11891      * double-width result element in the output (possibly accumulating).
11892      */
11893     bool accum = (opcode == 0x6);
11894     int maxpass = is_q ? 2 : 1;
11895     int pass;
11896     TCGv_i64 tcg_res[2];
11897 
11898     if (size == 2) {
11899         /* 32 + 32 -> 64 op */
11900         MemOp memop = size + (u ? 0 : MO_SIGN);
11901 
11902         for (pass = 0; pass < maxpass; pass++) {
11903             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11904             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11905 
11906             tcg_res[pass] = tcg_temp_new_i64();
11907 
11908             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
11909             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
11910             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11911             if (accum) {
11912                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
11913                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
11914             }
11915         }
11916     } else {
11917         for (pass = 0; pass < maxpass; pass++) {
11918             TCGv_i64 tcg_op = tcg_temp_new_i64();
11919             NeonGenOne64OpFn *genfn;
11920             static NeonGenOne64OpFn * const fns[2][2] = {
11921                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
11922                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
11923             };
11924 
11925             genfn = fns[size][u];
11926 
11927             tcg_res[pass] = tcg_temp_new_i64();
11928 
11929             read_vec_element(s, tcg_op, rn, pass, MO_64);
11930             genfn(tcg_res[pass], tcg_op);
11931 
11932             if (accum) {
11933                 read_vec_element(s, tcg_op, rd, pass, MO_64);
11934                 if (size == 0) {
11935                     gen_helper_neon_addl_u16(tcg_res[pass],
11936                                              tcg_res[pass], tcg_op);
11937                 } else {
11938                     gen_helper_neon_addl_u32(tcg_res[pass],
11939                                              tcg_res[pass], tcg_op);
11940                 }
11941             }
11942         }
11943     }
11944     if (!is_q) {
11945         tcg_res[1] = tcg_constant_i64(0);
11946     }
11947     for (pass = 0; pass < 2; pass++) {
11948         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11949     }
11950 }
11951 
11952 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
11953 {
11954     /* Implement SHLL and SHLL2 */
11955     int pass;
11956     int part = is_q ? 2 : 0;
11957     TCGv_i64 tcg_res[2];
11958 
11959     for (pass = 0; pass < 2; pass++) {
11960         static NeonGenWidenFn * const widenfns[3] = {
11961             gen_helper_neon_widen_u8,
11962             gen_helper_neon_widen_u16,
11963             tcg_gen_extu_i32_i64,
11964         };
11965         NeonGenWidenFn *widenfn = widenfns[size];
11966         TCGv_i32 tcg_op = tcg_temp_new_i32();
11967 
11968         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
11969         tcg_res[pass] = tcg_temp_new_i64();
11970         widenfn(tcg_res[pass], tcg_op);
11971         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
11972     }
11973 
11974     for (pass = 0; pass < 2; pass++) {
11975         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11976     }
11977 }
11978 
11979 /* AdvSIMD two reg misc
11980  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
11981  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11982  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
11983  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11984  */
11985 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
11986 {
11987     int size = extract32(insn, 22, 2);
11988     int opcode = extract32(insn, 12, 5);
11989     bool u = extract32(insn, 29, 1);
11990     bool is_q = extract32(insn, 30, 1);
11991     int rn = extract32(insn, 5, 5);
11992     int rd = extract32(insn, 0, 5);
11993     bool need_fpstatus = false;
11994     int rmode = -1;
11995     TCGv_i32 tcg_rmode;
11996     TCGv_ptr tcg_fpstatus;
11997 
11998     switch (opcode) {
11999     case 0x0: /* REV64, REV32 */
12000     case 0x1: /* REV16 */
12001         handle_rev(s, opcode, u, is_q, size, rn, rd);
12002         return;
12003     case 0x5: /* CNT, NOT, RBIT */
12004         if (u && size == 0) {
12005             /* NOT */
12006             break;
12007         } else if (u && size == 1) {
12008             /* RBIT */
12009             break;
12010         } else if (!u && size == 0) {
12011             /* CNT */
12012             break;
12013         }
12014         unallocated_encoding(s);
12015         return;
12016     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12017     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12018         if (size == 3) {
12019             unallocated_encoding(s);
12020             return;
12021         }
12022         if (!fp_access_check(s)) {
12023             return;
12024         }
12025 
12026         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
12027         return;
12028     case 0x4: /* CLS, CLZ */
12029         if (size == 3) {
12030             unallocated_encoding(s);
12031             return;
12032         }
12033         break;
12034     case 0x2: /* SADDLP, UADDLP */
12035     case 0x6: /* SADALP, UADALP */
12036         if (size == 3) {
12037             unallocated_encoding(s);
12038             return;
12039         }
12040         if (!fp_access_check(s)) {
12041             return;
12042         }
12043         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
12044         return;
12045     case 0x13: /* SHLL, SHLL2 */
12046         if (u == 0 || size == 3) {
12047             unallocated_encoding(s);
12048             return;
12049         }
12050         if (!fp_access_check(s)) {
12051             return;
12052         }
12053         handle_shll(s, is_q, size, rn, rd);
12054         return;
12055     case 0xa: /* CMLT */
12056         if (u == 1) {
12057             unallocated_encoding(s);
12058             return;
12059         }
12060         /* fall through */
12061     case 0x8: /* CMGT, CMGE */
12062     case 0x9: /* CMEQ, CMLE */
12063     case 0xb: /* ABS, NEG */
12064         if (size == 3 && !is_q) {
12065             unallocated_encoding(s);
12066             return;
12067         }
12068         break;
12069     case 0x3: /* SUQADD, USQADD */
12070         if (size == 3 && !is_q) {
12071             unallocated_encoding(s);
12072             return;
12073         }
12074         if (!fp_access_check(s)) {
12075             return;
12076         }
12077         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12078         return;
12079     case 0x7: /* SQABS, SQNEG */
12080         if (size == 3 && !is_q) {
12081             unallocated_encoding(s);
12082             return;
12083         }
12084         break;
12085     case 0xc ... 0xf:
12086     case 0x16 ... 0x1f:
12087     {
12088         /* Floating point: U, size[1] and opcode indicate operation;
12089          * size[0] indicates single or double precision.
12090          */
12091         int is_double = extract32(size, 0, 1);
12092         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12093         size = is_double ? 3 : 2;
12094         switch (opcode) {
12095         case 0x2f: /* FABS */
12096         case 0x6f: /* FNEG */
12097             if (size == 3 && !is_q) {
12098                 unallocated_encoding(s);
12099                 return;
12100             }
12101             break;
12102         case 0x1d: /* SCVTF */
12103         case 0x5d: /* UCVTF */
12104         {
12105             bool is_signed = (opcode == 0x1d) ? true : false;
12106             int elements = is_double ? 2 : is_q ? 4 : 2;
12107             if (is_double && !is_q) {
12108                 unallocated_encoding(s);
12109                 return;
12110             }
12111             if (!fp_access_check(s)) {
12112                 return;
12113             }
12114             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12115             return;
12116         }
12117         case 0x2c: /* FCMGT (zero) */
12118         case 0x2d: /* FCMEQ (zero) */
12119         case 0x2e: /* FCMLT (zero) */
12120         case 0x6c: /* FCMGE (zero) */
12121         case 0x6d: /* FCMLE (zero) */
12122             if (size == 3 && !is_q) {
12123                 unallocated_encoding(s);
12124                 return;
12125             }
12126             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12127             return;
12128         case 0x7f: /* FSQRT */
12129             if (size == 3 && !is_q) {
12130                 unallocated_encoding(s);
12131                 return;
12132             }
12133             break;
12134         case 0x1a: /* FCVTNS */
12135         case 0x1b: /* FCVTMS */
12136         case 0x3a: /* FCVTPS */
12137         case 0x3b: /* FCVTZS */
12138         case 0x5a: /* FCVTNU */
12139         case 0x5b: /* FCVTMU */
12140         case 0x7a: /* FCVTPU */
12141         case 0x7b: /* FCVTZU */
12142             need_fpstatus = true;
12143             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12144             if (size == 3 && !is_q) {
12145                 unallocated_encoding(s);
12146                 return;
12147             }
12148             break;
12149         case 0x5c: /* FCVTAU */
12150         case 0x1c: /* FCVTAS */
12151             need_fpstatus = true;
12152             rmode = FPROUNDING_TIEAWAY;
12153             if (size == 3 && !is_q) {
12154                 unallocated_encoding(s);
12155                 return;
12156             }
12157             break;
12158         case 0x3c: /* URECPE */
12159             if (size == 3) {
12160                 unallocated_encoding(s);
12161                 return;
12162             }
12163             /* fall through */
12164         case 0x3d: /* FRECPE */
12165         case 0x7d: /* FRSQRTE */
12166             if (size == 3 && !is_q) {
12167                 unallocated_encoding(s);
12168                 return;
12169             }
12170             if (!fp_access_check(s)) {
12171                 return;
12172             }
12173             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12174             return;
12175         case 0x56: /* FCVTXN, FCVTXN2 */
12176             if (size == 2) {
12177                 unallocated_encoding(s);
12178                 return;
12179             }
12180             /* fall through */
12181         case 0x16: /* FCVTN, FCVTN2 */
12182             /* handle_2misc_narrow does a 2*size -> size operation, but these
12183              * instructions encode the source size rather than dest size.
12184              */
12185             if (!fp_access_check(s)) {
12186                 return;
12187             }
12188             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12189             return;
12190         case 0x36: /* BFCVTN, BFCVTN2 */
12191             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
12192                 unallocated_encoding(s);
12193                 return;
12194             }
12195             if (!fp_access_check(s)) {
12196                 return;
12197             }
12198             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12199             return;
12200         case 0x17: /* FCVTL, FCVTL2 */
12201             if (!fp_access_check(s)) {
12202                 return;
12203             }
12204             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12205             return;
12206         case 0x18: /* FRINTN */
12207         case 0x19: /* FRINTM */
12208         case 0x38: /* FRINTP */
12209         case 0x39: /* FRINTZ */
12210             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12211             /* fall through */
12212         case 0x59: /* FRINTX */
12213         case 0x79: /* FRINTI */
12214             need_fpstatus = true;
12215             if (size == 3 && !is_q) {
12216                 unallocated_encoding(s);
12217                 return;
12218             }
12219             break;
12220         case 0x58: /* FRINTA */
12221             rmode = FPROUNDING_TIEAWAY;
12222             need_fpstatus = true;
12223             if (size == 3 && !is_q) {
12224                 unallocated_encoding(s);
12225                 return;
12226             }
12227             break;
12228         case 0x7c: /* URSQRTE */
12229             if (size == 3) {
12230                 unallocated_encoding(s);
12231                 return;
12232             }
12233             break;
12234         case 0x1e: /* FRINT32Z */
12235         case 0x1f: /* FRINT64Z */
12236             rmode = FPROUNDING_ZERO;
12237             /* fall through */
12238         case 0x5e: /* FRINT32X */
12239         case 0x5f: /* FRINT64X */
12240             need_fpstatus = true;
12241             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12242                 unallocated_encoding(s);
12243                 return;
12244             }
12245             break;
12246         default:
12247             unallocated_encoding(s);
12248             return;
12249         }
12250         break;
12251     }
12252     default:
12253         unallocated_encoding(s);
12254         return;
12255     }
12256 
12257     if (!fp_access_check(s)) {
12258         return;
12259     }
12260 
12261     if (need_fpstatus || rmode >= 0) {
12262         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12263     } else {
12264         tcg_fpstatus = NULL;
12265     }
12266     if (rmode >= 0) {
12267         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12268     } else {
12269         tcg_rmode = NULL;
12270     }
12271 
12272     switch (opcode) {
12273     case 0x5:
12274         if (u && size == 0) { /* NOT */
12275             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12276             return;
12277         }
12278         break;
12279     case 0x8: /* CMGT, CMGE */
12280         if (u) {
12281             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12282         } else {
12283             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12284         }
12285         return;
12286     case 0x9: /* CMEQ, CMLE */
12287         if (u) {
12288             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12289         } else {
12290             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12291         }
12292         return;
12293     case 0xa: /* CMLT */
12294         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12295         return;
12296     case 0xb:
12297         if (u) { /* ABS, NEG */
12298             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12299         } else {
12300             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12301         }
12302         return;
12303     }
12304 
12305     if (size == 3) {
12306         /* All 64-bit element operations can be shared with scalar 2misc */
12307         int pass;
12308 
12309         /* Coverity claims (size == 3 && !is_q) has been eliminated
12310          * from all paths leading to here.
12311          */
12312         tcg_debug_assert(is_q);
12313         for (pass = 0; pass < 2; pass++) {
12314             TCGv_i64 tcg_op = tcg_temp_new_i64();
12315             TCGv_i64 tcg_res = tcg_temp_new_i64();
12316 
12317             read_vec_element(s, tcg_op, rn, pass, MO_64);
12318 
12319             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12320                             tcg_rmode, tcg_fpstatus);
12321 
12322             write_vec_element(s, tcg_res, rd, pass, MO_64);
12323         }
12324     } else {
12325         int pass;
12326 
12327         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12328             TCGv_i32 tcg_op = tcg_temp_new_i32();
12329             TCGv_i32 tcg_res = tcg_temp_new_i32();
12330 
12331             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12332 
12333             if (size == 2) {
12334                 /* Special cases for 32 bit elements */
12335                 switch (opcode) {
12336                 case 0x4: /* CLS */
12337                     if (u) {
12338                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12339                     } else {
12340                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
12341                     }
12342                     break;
12343                 case 0x7: /* SQABS, SQNEG */
12344                     if (u) {
12345                         gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12346                     } else {
12347                         gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12348                     }
12349                     break;
12350                 case 0x2f: /* FABS */
12351                     gen_helper_vfp_abss(tcg_res, tcg_op);
12352                     break;
12353                 case 0x6f: /* FNEG */
12354                     gen_helper_vfp_negs(tcg_res, tcg_op);
12355                     break;
12356                 case 0x7f: /* FSQRT */
12357                     gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12358                     break;
12359                 case 0x1a: /* FCVTNS */
12360                 case 0x1b: /* FCVTMS */
12361                 case 0x1c: /* FCVTAS */
12362                 case 0x3a: /* FCVTPS */
12363                 case 0x3b: /* FCVTZS */
12364                     gen_helper_vfp_tosls(tcg_res, tcg_op,
12365                                          tcg_constant_i32(0), tcg_fpstatus);
12366                     break;
12367                 case 0x5a: /* FCVTNU */
12368                 case 0x5b: /* FCVTMU */
12369                 case 0x5c: /* FCVTAU */
12370                 case 0x7a: /* FCVTPU */
12371                 case 0x7b: /* FCVTZU */
12372                     gen_helper_vfp_touls(tcg_res, tcg_op,
12373                                          tcg_constant_i32(0), tcg_fpstatus);
12374                     break;
12375                 case 0x18: /* FRINTN */
12376                 case 0x19: /* FRINTM */
12377                 case 0x38: /* FRINTP */
12378                 case 0x39: /* FRINTZ */
12379                 case 0x58: /* FRINTA */
12380                 case 0x79: /* FRINTI */
12381                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12382                     break;
12383                 case 0x59: /* FRINTX */
12384                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12385                     break;
12386                 case 0x7c: /* URSQRTE */
12387                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
12388                     break;
12389                 case 0x1e: /* FRINT32Z */
12390                 case 0x5e: /* FRINT32X */
12391                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12392                     break;
12393                 case 0x1f: /* FRINT64Z */
12394                 case 0x5f: /* FRINT64X */
12395                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12396                     break;
12397                 default:
12398                     g_assert_not_reached();
12399                 }
12400             } else {
12401                 /* Use helpers for 8 and 16 bit elements */
12402                 switch (opcode) {
12403                 case 0x5: /* CNT, RBIT */
12404                     /* For these two insns size is part of the opcode specifier
12405                      * (handled earlier); they always operate on byte elements.
12406                      */
12407                     if (u) {
12408                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12409                     } else {
12410                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12411                     }
12412                     break;
12413                 case 0x7: /* SQABS, SQNEG */
12414                 {
12415                     NeonGenOneOpEnvFn *genfn;
12416                     static NeonGenOneOpEnvFn * const fns[2][2] = {
12417                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12418                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12419                     };
12420                     genfn = fns[size][u];
12421                     genfn(tcg_res, cpu_env, tcg_op);
12422                     break;
12423                 }
12424                 case 0x4: /* CLS, CLZ */
12425                     if (u) {
12426                         if (size == 0) {
12427                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
12428                         } else {
12429                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
12430                         }
12431                     } else {
12432                         if (size == 0) {
12433                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
12434                         } else {
12435                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
12436                         }
12437                     }
12438                     break;
12439                 default:
12440                     g_assert_not_reached();
12441                 }
12442             }
12443 
12444             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12445         }
12446     }
12447     clear_vec_high(s, is_q, rd);
12448 
12449     if (tcg_rmode) {
12450         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12451     }
12452 }
12453 
12454 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12455  *
12456  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
12457  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12458  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12459  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12460  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12461  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12462  *
12463  * This actually covers two groups where scalar access is governed by
12464  * bit 28. A bunch of the instructions (float to integral) only exist
12465  * in the vector form and are un-allocated for the scalar decode. Also
12466  * in the scalar decode Q is always 1.
12467  */
12468 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12469 {
12470     int fpop, opcode, a, u;
12471     int rn, rd;
12472     bool is_q;
12473     bool is_scalar;
12474     bool only_in_vector = false;
12475 
12476     int pass;
12477     TCGv_i32 tcg_rmode = NULL;
12478     TCGv_ptr tcg_fpstatus = NULL;
12479     bool need_fpst = true;
12480     int rmode = -1;
12481 
12482     if (!dc_isar_feature(aa64_fp16, s)) {
12483         unallocated_encoding(s);
12484         return;
12485     }
12486 
12487     rd = extract32(insn, 0, 5);
12488     rn = extract32(insn, 5, 5);
12489 
12490     a = extract32(insn, 23, 1);
12491     u = extract32(insn, 29, 1);
12492     is_scalar = extract32(insn, 28, 1);
12493     is_q = extract32(insn, 30, 1);
12494 
12495     opcode = extract32(insn, 12, 5);
12496     fpop = deposit32(opcode, 5, 1, a);
12497     fpop = deposit32(fpop, 6, 1, u);
12498 
12499     switch (fpop) {
12500     case 0x1d: /* SCVTF */
12501     case 0x5d: /* UCVTF */
12502     {
12503         int elements;
12504 
12505         if (is_scalar) {
12506             elements = 1;
12507         } else {
12508             elements = (is_q ? 8 : 4);
12509         }
12510 
12511         if (!fp_access_check(s)) {
12512             return;
12513         }
12514         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12515         return;
12516     }
12517     break;
12518     case 0x2c: /* FCMGT (zero) */
12519     case 0x2d: /* FCMEQ (zero) */
12520     case 0x2e: /* FCMLT (zero) */
12521     case 0x6c: /* FCMGE (zero) */
12522     case 0x6d: /* FCMLE (zero) */
12523         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12524         return;
12525     case 0x3d: /* FRECPE */
12526     case 0x3f: /* FRECPX */
12527         break;
12528     case 0x18: /* FRINTN */
12529         only_in_vector = true;
12530         rmode = FPROUNDING_TIEEVEN;
12531         break;
12532     case 0x19: /* FRINTM */
12533         only_in_vector = true;
12534         rmode = FPROUNDING_NEGINF;
12535         break;
12536     case 0x38: /* FRINTP */
12537         only_in_vector = true;
12538         rmode = FPROUNDING_POSINF;
12539         break;
12540     case 0x39: /* FRINTZ */
12541         only_in_vector = true;
12542         rmode = FPROUNDING_ZERO;
12543         break;
12544     case 0x58: /* FRINTA */
12545         only_in_vector = true;
12546         rmode = FPROUNDING_TIEAWAY;
12547         break;
12548     case 0x59: /* FRINTX */
12549     case 0x79: /* FRINTI */
12550         only_in_vector = true;
12551         /* current rounding mode */
12552         break;
12553     case 0x1a: /* FCVTNS */
12554         rmode = FPROUNDING_TIEEVEN;
12555         break;
12556     case 0x1b: /* FCVTMS */
12557         rmode = FPROUNDING_NEGINF;
12558         break;
12559     case 0x1c: /* FCVTAS */
12560         rmode = FPROUNDING_TIEAWAY;
12561         break;
12562     case 0x3a: /* FCVTPS */
12563         rmode = FPROUNDING_POSINF;
12564         break;
12565     case 0x3b: /* FCVTZS */
12566         rmode = FPROUNDING_ZERO;
12567         break;
12568     case 0x5a: /* FCVTNU */
12569         rmode = FPROUNDING_TIEEVEN;
12570         break;
12571     case 0x5b: /* FCVTMU */
12572         rmode = FPROUNDING_NEGINF;
12573         break;
12574     case 0x5c: /* FCVTAU */
12575         rmode = FPROUNDING_TIEAWAY;
12576         break;
12577     case 0x7a: /* FCVTPU */
12578         rmode = FPROUNDING_POSINF;
12579         break;
12580     case 0x7b: /* FCVTZU */
12581         rmode = FPROUNDING_ZERO;
12582         break;
12583     case 0x2f: /* FABS */
12584     case 0x6f: /* FNEG */
12585         need_fpst = false;
12586         break;
12587     case 0x7d: /* FRSQRTE */
12588     case 0x7f: /* FSQRT (vector) */
12589         break;
12590     default:
12591         unallocated_encoding(s);
12592         return;
12593     }
12594 
12595 
12596     /* Check additional constraints for the scalar encoding */
12597     if (is_scalar) {
12598         if (!is_q) {
12599             unallocated_encoding(s);
12600             return;
12601         }
12602         /* FRINTxx is only in the vector form */
12603         if (only_in_vector) {
12604             unallocated_encoding(s);
12605             return;
12606         }
12607     }
12608 
12609     if (!fp_access_check(s)) {
12610         return;
12611     }
12612 
12613     if (rmode >= 0 || need_fpst) {
12614         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
12615     }
12616 
12617     if (rmode >= 0) {
12618         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12619     }
12620 
12621     if (is_scalar) {
12622         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12623         TCGv_i32 tcg_res = tcg_temp_new_i32();
12624 
12625         switch (fpop) {
12626         case 0x1a: /* FCVTNS */
12627         case 0x1b: /* FCVTMS */
12628         case 0x1c: /* FCVTAS */
12629         case 0x3a: /* FCVTPS */
12630         case 0x3b: /* FCVTZS */
12631             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12632             break;
12633         case 0x3d: /* FRECPE */
12634             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12635             break;
12636         case 0x3f: /* FRECPX */
12637             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12638             break;
12639         case 0x5a: /* FCVTNU */
12640         case 0x5b: /* FCVTMU */
12641         case 0x5c: /* FCVTAU */
12642         case 0x7a: /* FCVTPU */
12643         case 0x7b: /* FCVTZU */
12644             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12645             break;
12646         case 0x6f: /* FNEG */
12647             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12648             break;
12649         case 0x7d: /* FRSQRTE */
12650             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12651             break;
12652         default:
12653             g_assert_not_reached();
12654         }
12655 
12656         /* limit any sign extension going on */
12657         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12658         write_fp_sreg(s, rd, tcg_res);
12659     } else {
12660         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12661             TCGv_i32 tcg_op = tcg_temp_new_i32();
12662             TCGv_i32 tcg_res = tcg_temp_new_i32();
12663 
12664             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12665 
12666             switch (fpop) {
12667             case 0x1a: /* FCVTNS */
12668             case 0x1b: /* FCVTMS */
12669             case 0x1c: /* FCVTAS */
12670             case 0x3a: /* FCVTPS */
12671             case 0x3b: /* FCVTZS */
12672                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12673                 break;
12674             case 0x3d: /* FRECPE */
12675                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12676                 break;
12677             case 0x5a: /* FCVTNU */
12678             case 0x5b: /* FCVTMU */
12679             case 0x5c: /* FCVTAU */
12680             case 0x7a: /* FCVTPU */
12681             case 0x7b: /* FCVTZU */
12682                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12683                 break;
12684             case 0x18: /* FRINTN */
12685             case 0x19: /* FRINTM */
12686             case 0x38: /* FRINTP */
12687             case 0x39: /* FRINTZ */
12688             case 0x58: /* FRINTA */
12689             case 0x79: /* FRINTI */
12690                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12691                 break;
12692             case 0x59: /* FRINTX */
12693                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12694                 break;
12695             case 0x2f: /* FABS */
12696                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12697                 break;
12698             case 0x6f: /* FNEG */
12699                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12700                 break;
12701             case 0x7d: /* FRSQRTE */
12702                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12703                 break;
12704             case 0x7f: /* FSQRT */
12705                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12706                 break;
12707             default:
12708                 g_assert_not_reached();
12709             }
12710 
12711             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12712         }
12713 
12714         clear_vec_high(s, is_q, rd);
12715     }
12716 
12717     if (tcg_rmode) {
12718         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12719     }
12720 }
12721 
12722 /* AdvSIMD scalar x indexed element
12723  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12724  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12725  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12726  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12727  * AdvSIMD vector x indexed element
12728  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12729  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12730  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12731  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12732  */
12733 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12734 {
12735     /* This encoding has two kinds of instruction:
12736      *  normal, where we perform elt x idxelt => elt for each
12737      *     element in the vector
12738      *  long, where we perform elt x idxelt and generate a result of
12739      *     double the width of the input element
12740      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12741      */
12742     bool is_scalar = extract32(insn, 28, 1);
12743     bool is_q = extract32(insn, 30, 1);
12744     bool u = extract32(insn, 29, 1);
12745     int size = extract32(insn, 22, 2);
12746     int l = extract32(insn, 21, 1);
12747     int m = extract32(insn, 20, 1);
12748     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12749     int rm = extract32(insn, 16, 4);
12750     int opcode = extract32(insn, 12, 4);
12751     int h = extract32(insn, 11, 1);
12752     int rn = extract32(insn, 5, 5);
12753     int rd = extract32(insn, 0, 5);
12754     bool is_long = false;
12755     int is_fp = 0;
12756     bool is_fp16 = false;
12757     int index;
12758     TCGv_ptr fpst;
12759 
12760     switch (16 * u + opcode) {
12761     case 0x08: /* MUL */
12762     case 0x10: /* MLA */
12763     case 0x14: /* MLS */
12764         if (is_scalar) {
12765             unallocated_encoding(s);
12766             return;
12767         }
12768         break;
12769     case 0x02: /* SMLAL, SMLAL2 */
12770     case 0x12: /* UMLAL, UMLAL2 */
12771     case 0x06: /* SMLSL, SMLSL2 */
12772     case 0x16: /* UMLSL, UMLSL2 */
12773     case 0x0a: /* SMULL, SMULL2 */
12774     case 0x1a: /* UMULL, UMULL2 */
12775         if (is_scalar) {
12776             unallocated_encoding(s);
12777             return;
12778         }
12779         is_long = true;
12780         break;
12781     case 0x03: /* SQDMLAL, SQDMLAL2 */
12782     case 0x07: /* SQDMLSL, SQDMLSL2 */
12783     case 0x0b: /* SQDMULL, SQDMULL2 */
12784         is_long = true;
12785         break;
12786     case 0x0c: /* SQDMULH */
12787     case 0x0d: /* SQRDMULH */
12788         break;
12789     case 0x01: /* FMLA */
12790     case 0x05: /* FMLS */
12791     case 0x09: /* FMUL */
12792     case 0x19: /* FMULX */
12793         is_fp = 1;
12794         break;
12795     case 0x1d: /* SQRDMLAH */
12796     case 0x1f: /* SQRDMLSH */
12797         if (!dc_isar_feature(aa64_rdm, s)) {
12798             unallocated_encoding(s);
12799             return;
12800         }
12801         break;
12802     case 0x0e: /* SDOT */
12803     case 0x1e: /* UDOT */
12804         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12805             unallocated_encoding(s);
12806             return;
12807         }
12808         break;
12809     case 0x0f:
12810         switch (size) {
12811         case 0: /* SUDOT */
12812         case 2: /* USDOT */
12813             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12814                 unallocated_encoding(s);
12815                 return;
12816             }
12817             size = MO_32;
12818             break;
12819         case 1: /* BFDOT */
12820             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12821                 unallocated_encoding(s);
12822                 return;
12823             }
12824             size = MO_32;
12825             break;
12826         case 3: /* BFMLAL{B,T} */
12827             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12828                 unallocated_encoding(s);
12829                 return;
12830             }
12831             /* can't set is_fp without other incorrect size checks */
12832             size = MO_16;
12833             break;
12834         default:
12835             unallocated_encoding(s);
12836             return;
12837         }
12838         break;
12839     case 0x11: /* FCMLA #0 */
12840     case 0x13: /* FCMLA #90 */
12841     case 0x15: /* FCMLA #180 */
12842     case 0x17: /* FCMLA #270 */
12843         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
12844             unallocated_encoding(s);
12845             return;
12846         }
12847         is_fp = 2;
12848         break;
12849     case 0x00: /* FMLAL */
12850     case 0x04: /* FMLSL */
12851     case 0x18: /* FMLAL2 */
12852     case 0x1c: /* FMLSL2 */
12853         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
12854             unallocated_encoding(s);
12855             return;
12856         }
12857         size = MO_16;
12858         /* is_fp, but we pass cpu_env not fp_status.  */
12859         break;
12860     default:
12861         unallocated_encoding(s);
12862         return;
12863     }
12864 
12865     switch (is_fp) {
12866     case 1: /* normal fp */
12867         /* convert insn encoded size to MemOp size */
12868         switch (size) {
12869         case 0: /* half-precision */
12870             size = MO_16;
12871             is_fp16 = true;
12872             break;
12873         case MO_32: /* single precision */
12874         case MO_64: /* double precision */
12875             break;
12876         default:
12877             unallocated_encoding(s);
12878             return;
12879         }
12880         break;
12881 
12882     case 2: /* complex fp */
12883         /* Each indexable element is a complex pair.  */
12884         size += 1;
12885         switch (size) {
12886         case MO_32:
12887             if (h && !is_q) {
12888                 unallocated_encoding(s);
12889                 return;
12890             }
12891             is_fp16 = true;
12892             break;
12893         case MO_64:
12894             break;
12895         default:
12896             unallocated_encoding(s);
12897             return;
12898         }
12899         break;
12900 
12901     default: /* integer */
12902         switch (size) {
12903         case MO_8:
12904         case MO_64:
12905             unallocated_encoding(s);
12906             return;
12907         }
12908         break;
12909     }
12910     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
12911         unallocated_encoding(s);
12912         return;
12913     }
12914 
12915     /* Given MemOp size, adjust register and indexing.  */
12916     switch (size) {
12917     case MO_16:
12918         index = h << 2 | l << 1 | m;
12919         break;
12920     case MO_32:
12921         index = h << 1 | l;
12922         rm |= m << 4;
12923         break;
12924     case MO_64:
12925         if (l || !is_q) {
12926             unallocated_encoding(s);
12927             return;
12928         }
12929         index = h;
12930         rm |= m << 4;
12931         break;
12932     default:
12933         g_assert_not_reached();
12934     }
12935 
12936     if (!fp_access_check(s)) {
12937         return;
12938     }
12939 
12940     if (is_fp) {
12941         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
12942     } else {
12943         fpst = NULL;
12944     }
12945 
12946     switch (16 * u + opcode) {
12947     case 0x0e: /* SDOT */
12948     case 0x1e: /* UDOT */
12949         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12950                          u ? gen_helper_gvec_udot_idx_b
12951                          : gen_helper_gvec_sdot_idx_b);
12952         return;
12953     case 0x0f:
12954         switch (extract32(insn, 22, 2)) {
12955         case 0: /* SUDOT */
12956             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12957                              gen_helper_gvec_sudot_idx_b);
12958             return;
12959         case 1: /* BFDOT */
12960             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12961                              gen_helper_gvec_bfdot_idx);
12962             return;
12963         case 2: /* USDOT */
12964             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12965                              gen_helper_gvec_usdot_idx_b);
12966             return;
12967         case 3: /* BFMLAL{B,T} */
12968             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
12969                               gen_helper_gvec_bfmlal_idx);
12970             return;
12971         }
12972         g_assert_not_reached();
12973     case 0x11: /* FCMLA #0 */
12974     case 0x13: /* FCMLA #90 */
12975     case 0x15: /* FCMLA #180 */
12976     case 0x17: /* FCMLA #270 */
12977         {
12978             int rot = extract32(insn, 13, 2);
12979             int data = (index << 2) | rot;
12980             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
12981                                vec_full_reg_offset(s, rn),
12982                                vec_full_reg_offset(s, rm),
12983                                vec_full_reg_offset(s, rd), fpst,
12984                                is_q ? 16 : 8, vec_full_reg_size(s), data,
12985                                size == MO_64
12986                                ? gen_helper_gvec_fcmlas_idx
12987                                : gen_helper_gvec_fcmlah_idx);
12988         }
12989         return;
12990 
12991     case 0x00: /* FMLAL */
12992     case 0x04: /* FMLSL */
12993     case 0x18: /* FMLAL2 */
12994     case 0x1c: /* FMLSL2 */
12995         {
12996             int is_s = extract32(opcode, 2, 1);
12997             int is_2 = u;
12998             int data = (index << 2) | (is_2 << 1) | is_s;
12999             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13000                                vec_full_reg_offset(s, rn),
13001                                vec_full_reg_offset(s, rm), cpu_env,
13002                                is_q ? 16 : 8, vec_full_reg_size(s),
13003                                data, gen_helper_gvec_fmlal_idx_a64);
13004         }
13005         return;
13006 
13007     case 0x08: /* MUL */
13008         if (!is_long && !is_scalar) {
13009             static gen_helper_gvec_3 * const fns[3] = {
13010                 gen_helper_gvec_mul_idx_h,
13011                 gen_helper_gvec_mul_idx_s,
13012                 gen_helper_gvec_mul_idx_d,
13013             };
13014             tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
13015                                vec_full_reg_offset(s, rn),
13016                                vec_full_reg_offset(s, rm),
13017                                is_q ? 16 : 8, vec_full_reg_size(s),
13018                                index, fns[size - 1]);
13019             return;
13020         }
13021         break;
13022 
13023     case 0x10: /* MLA */
13024         if (!is_long && !is_scalar) {
13025             static gen_helper_gvec_4 * const fns[3] = {
13026                 gen_helper_gvec_mla_idx_h,
13027                 gen_helper_gvec_mla_idx_s,
13028                 gen_helper_gvec_mla_idx_d,
13029             };
13030             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13031                                vec_full_reg_offset(s, rn),
13032                                vec_full_reg_offset(s, rm),
13033                                vec_full_reg_offset(s, rd),
13034                                is_q ? 16 : 8, vec_full_reg_size(s),
13035                                index, fns[size - 1]);
13036             return;
13037         }
13038         break;
13039 
13040     case 0x14: /* MLS */
13041         if (!is_long && !is_scalar) {
13042             static gen_helper_gvec_4 * const fns[3] = {
13043                 gen_helper_gvec_mls_idx_h,
13044                 gen_helper_gvec_mls_idx_s,
13045                 gen_helper_gvec_mls_idx_d,
13046             };
13047             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13048                                vec_full_reg_offset(s, rn),
13049                                vec_full_reg_offset(s, rm),
13050                                vec_full_reg_offset(s, rd),
13051                                is_q ? 16 : 8, vec_full_reg_size(s),
13052                                index, fns[size - 1]);
13053             return;
13054         }
13055         break;
13056     }
13057 
13058     if (size == 3) {
13059         TCGv_i64 tcg_idx = tcg_temp_new_i64();
13060         int pass;
13061 
13062         assert(is_fp && is_q && !is_long);
13063 
13064         read_vec_element(s, tcg_idx, rm, index, MO_64);
13065 
13066         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13067             TCGv_i64 tcg_op = tcg_temp_new_i64();
13068             TCGv_i64 tcg_res = tcg_temp_new_i64();
13069 
13070             read_vec_element(s, tcg_op, rn, pass, MO_64);
13071 
13072             switch (16 * u + opcode) {
13073             case 0x05: /* FMLS */
13074                 /* As usual for ARM, separate negation for fused multiply-add */
13075                 gen_helper_vfp_negd(tcg_op, tcg_op);
13076                 /* fall through */
13077             case 0x01: /* FMLA */
13078                 read_vec_element(s, tcg_res, rd, pass, MO_64);
13079                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13080                 break;
13081             case 0x09: /* FMUL */
13082                 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13083                 break;
13084             case 0x19: /* FMULX */
13085                 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13086                 break;
13087             default:
13088                 g_assert_not_reached();
13089             }
13090 
13091             write_vec_element(s, tcg_res, rd, pass, MO_64);
13092         }
13093 
13094         clear_vec_high(s, !is_scalar, rd);
13095     } else if (!is_long) {
13096         /* 32 bit floating point, or 16 or 32 bit integer.
13097          * For the 16 bit scalar case we use the usual Neon helpers and
13098          * rely on the fact that 0 op 0 == 0 with no side effects.
13099          */
13100         TCGv_i32 tcg_idx = tcg_temp_new_i32();
13101         int pass, maxpasses;
13102 
13103         if (is_scalar) {
13104             maxpasses = 1;
13105         } else {
13106             maxpasses = is_q ? 4 : 2;
13107         }
13108 
13109         read_vec_element_i32(s, tcg_idx, rm, index, size);
13110 
13111         if (size == 1 && !is_scalar) {
13112             /* The simplest way to handle the 16x16 indexed ops is to duplicate
13113              * the index into both halves of the 32 bit tcg_idx and then use
13114              * the usual Neon helpers.
13115              */
13116             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13117         }
13118 
13119         for (pass = 0; pass < maxpasses; pass++) {
13120             TCGv_i32 tcg_op = tcg_temp_new_i32();
13121             TCGv_i32 tcg_res = tcg_temp_new_i32();
13122 
13123             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13124 
13125             switch (16 * u + opcode) {
13126             case 0x08: /* MUL */
13127             case 0x10: /* MLA */
13128             case 0x14: /* MLS */
13129             {
13130                 static NeonGenTwoOpFn * const fns[2][2] = {
13131                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13132                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
13133                 };
13134                 NeonGenTwoOpFn *genfn;
13135                 bool is_sub = opcode == 0x4;
13136 
13137                 if (size == 1) {
13138                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13139                 } else {
13140                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13141                 }
13142                 if (opcode == 0x8) {
13143                     break;
13144                 }
13145                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13146                 genfn = fns[size - 1][is_sub];
13147                 genfn(tcg_res, tcg_op, tcg_res);
13148                 break;
13149             }
13150             case 0x05: /* FMLS */
13151             case 0x01: /* FMLA */
13152                 read_vec_element_i32(s, tcg_res, rd, pass,
13153                                      is_scalar ? size : MO_32);
13154                 switch (size) {
13155                 case 1:
13156                     if (opcode == 0x5) {
13157                         /* As usual for ARM, separate negation for fused
13158                          * multiply-add */
13159                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13160                     }
13161                     if (is_scalar) {
13162                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13163                                                    tcg_res, fpst);
13164                     } else {
13165                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13166                                                     tcg_res, fpst);
13167                     }
13168                     break;
13169                 case 2:
13170                     if (opcode == 0x5) {
13171                         /* As usual for ARM, separate negation for
13172                          * fused multiply-add */
13173                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13174                     }
13175                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13176                                            tcg_res, fpst);
13177                     break;
13178                 default:
13179                     g_assert_not_reached();
13180                 }
13181                 break;
13182             case 0x09: /* FMUL */
13183                 switch (size) {
13184                 case 1:
13185                     if (is_scalar) {
13186                         gen_helper_advsimd_mulh(tcg_res, tcg_op,
13187                                                 tcg_idx, fpst);
13188                     } else {
13189                         gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13190                                                  tcg_idx, fpst);
13191                     }
13192                     break;
13193                 case 2:
13194                     gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13195                     break;
13196                 default:
13197                     g_assert_not_reached();
13198                 }
13199                 break;
13200             case 0x19: /* FMULX */
13201                 switch (size) {
13202                 case 1:
13203                     if (is_scalar) {
13204                         gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13205                                                  tcg_idx, fpst);
13206                     } else {
13207                         gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13208                                                   tcg_idx, fpst);
13209                     }
13210                     break;
13211                 case 2:
13212                     gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13213                     break;
13214                 default:
13215                     g_assert_not_reached();
13216                 }
13217                 break;
13218             case 0x0c: /* SQDMULH */
13219                 if (size == 1) {
13220                     gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13221                                                tcg_op, tcg_idx);
13222                 } else {
13223                     gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13224                                                tcg_op, tcg_idx);
13225                 }
13226                 break;
13227             case 0x0d: /* SQRDMULH */
13228                 if (size == 1) {
13229                     gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13230                                                 tcg_op, tcg_idx);
13231                 } else {
13232                     gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13233                                                 tcg_op, tcg_idx);
13234                 }
13235                 break;
13236             case 0x1d: /* SQRDMLAH */
13237                 read_vec_element_i32(s, tcg_res, rd, pass,
13238                                      is_scalar ? size : MO_32);
13239                 if (size == 1) {
13240                     gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13241                                                 tcg_op, tcg_idx, tcg_res);
13242                 } else {
13243                     gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13244                                                 tcg_op, tcg_idx, tcg_res);
13245                 }
13246                 break;
13247             case 0x1f: /* SQRDMLSH */
13248                 read_vec_element_i32(s, tcg_res, rd, pass,
13249                                      is_scalar ? size : MO_32);
13250                 if (size == 1) {
13251                     gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13252                                                 tcg_op, tcg_idx, tcg_res);
13253                 } else {
13254                     gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13255                                                 tcg_op, tcg_idx, tcg_res);
13256                 }
13257                 break;
13258             default:
13259                 g_assert_not_reached();
13260             }
13261 
13262             if (is_scalar) {
13263                 write_fp_sreg(s, rd, tcg_res);
13264             } else {
13265                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13266             }
13267         }
13268 
13269         clear_vec_high(s, is_q, rd);
13270     } else {
13271         /* long ops: 16x16->32 or 32x32->64 */
13272         TCGv_i64 tcg_res[2];
13273         int pass;
13274         bool satop = extract32(opcode, 0, 1);
13275         MemOp memop = MO_32;
13276 
13277         if (satop || !u) {
13278             memop |= MO_SIGN;
13279         }
13280 
13281         if (size == 2) {
13282             TCGv_i64 tcg_idx = tcg_temp_new_i64();
13283 
13284             read_vec_element(s, tcg_idx, rm, index, memop);
13285 
13286             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13287                 TCGv_i64 tcg_op = tcg_temp_new_i64();
13288                 TCGv_i64 tcg_passres;
13289                 int passelt;
13290 
13291                 if (is_scalar) {
13292                     passelt = 0;
13293                 } else {
13294                     passelt = pass + (is_q * 2);
13295                 }
13296 
13297                 read_vec_element(s, tcg_op, rn, passelt, memop);
13298 
13299                 tcg_res[pass] = tcg_temp_new_i64();
13300 
13301                 if (opcode == 0xa || opcode == 0xb) {
13302                     /* Non-accumulating ops */
13303                     tcg_passres = tcg_res[pass];
13304                 } else {
13305                     tcg_passres = tcg_temp_new_i64();
13306                 }
13307 
13308                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13309 
13310                 if (satop) {
13311                     /* saturating, doubling */
13312                     gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13313                                                       tcg_passres, tcg_passres);
13314                 }
13315 
13316                 if (opcode == 0xa || opcode == 0xb) {
13317                     continue;
13318                 }
13319 
13320                 /* Accumulating op: handle accumulate step */
13321                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13322 
13323                 switch (opcode) {
13324                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13325                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13326                     break;
13327                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13328                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13329                     break;
13330                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13331                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
13332                     /* fall through */
13333                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13334                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13335                                                       tcg_res[pass],
13336                                                       tcg_passres);
13337                     break;
13338                 default:
13339                     g_assert_not_reached();
13340                 }
13341             }
13342 
13343             clear_vec_high(s, !is_scalar, rd);
13344         } else {
13345             TCGv_i32 tcg_idx = tcg_temp_new_i32();
13346 
13347             assert(size == 1);
13348             read_vec_element_i32(s, tcg_idx, rm, index, size);
13349 
13350             if (!is_scalar) {
13351                 /* The simplest way to handle the 16x16 indexed ops is to
13352                  * duplicate the index into both halves of the 32 bit tcg_idx
13353                  * and then use the usual Neon helpers.
13354                  */
13355                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13356             }
13357 
13358             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13359                 TCGv_i32 tcg_op = tcg_temp_new_i32();
13360                 TCGv_i64 tcg_passres;
13361 
13362                 if (is_scalar) {
13363                     read_vec_element_i32(s, tcg_op, rn, pass, size);
13364                 } else {
13365                     read_vec_element_i32(s, tcg_op, rn,
13366                                          pass + (is_q * 2), MO_32);
13367                 }
13368 
13369                 tcg_res[pass] = tcg_temp_new_i64();
13370 
13371                 if (opcode == 0xa || opcode == 0xb) {
13372                     /* Non-accumulating ops */
13373                     tcg_passres = tcg_res[pass];
13374                 } else {
13375                     tcg_passres = tcg_temp_new_i64();
13376                 }
13377 
13378                 if (memop & MO_SIGN) {
13379                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13380                 } else {
13381                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13382                 }
13383                 if (satop) {
13384                     gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13385                                                       tcg_passres, tcg_passres);
13386                 }
13387 
13388                 if (opcode == 0xa || opcode == 0xb) {
13389                     continue;
13390                 }
13391 
13392                 /* Accumulating op: handle accumulate step */
13393                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13394 
13395                 switch (opcode) {
13396                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13397                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13398                                              tcg_passres);
13399                     break;
13400                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13401                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13402                                              tcg_passres);
13403                     break;
13404                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13405                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13406                     /* fall through */
13407                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13408                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13409                                                       tcg_res[pass],
13410                                                       tcg_passres);
13411                     break;
13412                 default:
13413                     g_assert_not_reached();
13414                 }
13415             }
13416 
13417             if (is_scalar) {
13418                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13419             }
13420         }
13421 
13422         if (is_scalar) {
13423             tcg_res[1] = tcg_constant_i64(0);
13424         }
13425 
13426         for (pass = 0; pass < 2; pass++) {
13427             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13428         }
13429     }
13430 }
13431 
13432 /* Crypto AES
13433  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13434  * +-----------------+------+-----------+--------+-----+------+------+
13435  * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13436  * +-----------------+------+-----------+--------+-----+------+------+
13437  */
13438 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13439 {
13440     int size = extract32(insn, 22, 2);
13441     int opcode = extract32(insn, 12, 5);
13442     int rn = extract32(insn, 5, 5);
13443     int rd = extract32(insn, 0, 5);
13444     int decrypt;
13445     gen_helper_gvec_2 *genfn2 = NULL;
13446     gen_helper_gvec_3 *genfn3 = NULL;
13447 
13448     if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13449         unallocated_encoding(s);
13450         return;
13451     }
13452 
13453     switch (opcode) {
13454     case 0x4: /* AESE */
13455         decrypt = 0;
13456         genfn3 = gen_helper_crypto_aese;
13457         break;
13458     case 0x6: /* AESMC */
13459         decrypt = 0;
13460         genfn2 = gen_helper_crypto_aesmc;
13461         break;
13462     case 0x5: /* AESD */
13463         decrypt = 1;
13464         genfn3 = gen_helper_crypto_aese;
13465         break;
13466     case 0x7: /* AESIMC */
13467         decrypt = 1;
13468         genfn2 = gen_helper_crypto_aesmc;
13469         break;
13470     default:
13471         unallocated_encoding(s);
13472         return;
13473     }
13474 
13475     if (!fp_access_check(s)) {
13476         return;
13477     }
13478     if (genfn2) {
13479         gen_gvec_op2_ool(s, true, rd, rn, decrypt, genfn2);
13480     } else {
13481         gen_gvec_op3_ool(s, true, rd, rd, rn, decrypt, genfn3);
13482     }
13483 }
13484 
13485 /* Crypto three-reg SHA
13486  *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
13487  * +-----------------+------+---+------+---+--------+-----+------+------+
13488  * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
13489  * +-----------------+------+---+------+---+--------+-----+------+------+
13490  */
13491 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13492 {
13493     int size = extract32(insn, 22, 2);
13494     int opcode = extract32(insn, 12, 3);
13495     int rm = extract32(insn, 16, 5);
13496     int rn = extract32(insn, 5, 5);
13497     int rd = extract32(insn, 0, 5);
13498     gen_helper_gvec_3 *genfn;
13499     bool feature;
13500 
13501     if (size != 0) {
13502         unallocated_encoding(s);
13503         return;
13504     }
13505 
13506     switch (opcode) {
13507     case 0: /* SHA1C */
13508         genfn = gen_helper_crypto_sha1c;
13509         feature = dc_isar_feature(aa64_sha1, s);
13510         break;
13511     case 1: /* SHA1P */
13512         genfn = gen_helper_crypto_sha1p;
13513         feature = dc_isar_feature(aa64_sha1, s);
13514         break;
13515     case 2: /* SHA1M */
13516         genfn = gen_helper_crypto_sha1m;
13517         feature = dc_isar_feature(aa64_sha1, s);
13518         break;
13519     case 3: /* SHA1SU0 */
13520         genfn = gen_helper_crypto_sha1su0;
13521         feature = dc_isar_feature(aa64_sha1, s);
13522         break;
13523     case 4: /* SHA256H */
13524         genfn = gen_helper_crypto_sha256h;
13525         feature = dc_isar_feature(aa64_sha256, s);
13526         break;
13527     case 5: /* SHA256H2 */
13528         genfn = gen_helper_crypto_sha256h2;
13529         feature = dc_isar_feature(aa64_sha256, s);
13530         break;
13531     case 6: /* SHA256SU1 */
13532         genfn = gen_helper_crypto_sha256su1;
13533         feature = dc_isar_feature(aa64_sha256, s);
13534         break;
13535     default:
13536         unallocated_encoding(s);
13537         return;
13538     }
13539 
13540     if (!feature) {
13541         unallocated_encoding(s);
13542         return;
13543     }
13544 
13545     if (!fp_access_check(s)) {
13546         return;
13547     }
13548     gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
13549 }
13550 
13551 /* Crypto two-reg SHA
13552  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13553  * +-----------------+------+-----------+--------+-----+------+------+
13554  * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13555  * +-----------------+------+-----------+--------+-----+------+------+
13556  */
13557 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13558 {
13559     int size = extract32(insn, 22, 2);
13560     int opcode = extract32(insn, 12, 5);
13561     int rn = extract32(insn, 5, 5);
13562     int rd = extract32(insn, 0, 5);
13563     gen_helper_gvec_2 *genfn;
13564     bool feature;
13565 
13566     if (size != 0) {
13567         unallocated_encoding(s);
13568         return;
13569     }
13570 
13571     switch (opcode) {
13572     case 0: /* SHA1H */
13573         feature = dc_isar_feature(aa64_sha1, s);
13574         genfn = gen_helper_crypto_sha1h;
13575         break;
13576     case 1: /* SHA1SU1 */
13577         feature = dc_isar_feature(aa64_sha1, s);
13578         genfn = gen_helper_crypto_sha1su1;
13579         break;
13580     case 2: /* SHA256SU0 */
13581         feature = dc_isar_feature(aa64_sha256, s);
13582         genfn = gen_helper_crypto_sha256su0;
13583         break;
13584     default:
13585         unallocated_encoding(s);
13586         return;
13587     }
13588 
13589     if (!feature) {
13590         unallocated_encoding(s);
13591         return;
13592     }
13593 
13594     if (!fp_access_check(s)) {
13595         return;
13596     }
13597     gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
13598 }
13599 
13600 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
13601 {
13602     tcg_gen_rotli_i64(d, m, 1);
13603     tcg_gen_xor_i64(d, d, n);
13604 }
13605 
13606 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
13607 {
13608     tcg_gen_rotli_vec(vece, d, m, 1);
13609     tcg_gen_xor_vec(vece, d, d, n);
13610 }
13611 
13612 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
13613                    uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
13614 {
13615     static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
13616     static const GVecGen3 op = {
13617         .fni8 = gen_rax1_i64,
13618         .fniv = gen_rax1_vec,
13619         .opt_opc = vecop_list,
13620         .fno = gen_helper_crypto_rax1,
13621         .vece = MO_64,
13622     };
13623     tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
13624 }
13625 
13626 /* Crypto three-reg SHA512
13627  *  31                   21 20  16 15  14  13 12  11  10  9    5 4    0
13628  * +-----------------------+------+---+---+-----+--------+------+------+
13629  * | 1 1 0 0 1 1 1 0 0 1 1 |  Rm  | 1 | O | 0 0 | opcode |  Rn  |  Rd  |
13630  * +-----------------------+------+---+---+-----+--------+------+------+
13631  */
13632 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13633 {
13634     int opcode = extract32(insn, 10, 2);
13635     int o =  extract32(insn, 14, 1);
13636     int rm = extract32(insn, 16, 5);
13637     int rn = extract32(insn, 5, 5);
13638     int rd = extract32(insn, 0, 5);
13639     bool feature;
13640     gen_helper_gvec_3 *oolfn = NULL;
13641     GVecGen3Fn *gvecfn = NULL;
13642 
13643     if (o == 0) {
13644         switch (opcode) {
13645         case 0: /* SHA512H */
13646             feature = dc_isar_feature(aa64_sha512, s);
13647             oolfn = gen_helper_crypto_sha512h;
13648             break;
13649         case 1: /* SHA512H2 */
13650             feature = dc_isar_feature(aa64_sha512, s);
13651             oolfn = gen_helper_crypto_sha512h2;
13652             break;
13653         case 2: /* SHA512SU1 */
13654             feature = dc_isar_feature(aa64_sha512, s);
13655             oolfn = gen_helper_crypto_sha512su1;
13656             break;
13657         case 3: /* RAX1 */
13658             feature = dc_isar_feature(aa64_sha3, s);
13659             gvecfn = gen_gvec_rax1;
13660             break;
13661         default:
13662             g_assert_not_reached();
13663         }
13664     } else {
13665         switch (opcode) {
13666         case 0: /* SM3PARTW1 */
13667             feature = dc_isar_feature(aa64_sm3, s);
13668             oolfn = gen_helper_crypto_sm3partw1;
13669             break;
13670         case 1: /* SM3PARTW2 */
13671             feature = dc_isar_feature(aa64_sm3, s);
13672             oolfn = gen_helper_crypto_sm3partw2;
13673             break;
13674         case 2: /* SM4EKEY */
13675             feature = dc_isar_feature(aa64_sm4, s);
13676             oolfn = gen_helper_crypto_sm4ekey;
13677             break;
13678         default:
13679             unallocated_encoding(s);
13680             return;
13681         }
13682     }
13683 
13684     if (!feature) {
13685         unallocated_encoding(s);
13686         return;
13687     }
13688 
13689     if (!fp_access_check(s)) {
13690         return;
13691     }
13692 
13693     if (oolfn) {
13694         gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
13695     } else {
13696         gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
13697     }
13698 }
13699 
13700 /* Crypto two-reg SHA512
13701  *  31                                     12  11  10  9    5 4    0
13702  * +-----------------------------------------+--------+------+------+
13703  * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode |  Rn  |  Rd  |
13704  * +-----------------------------------------+--------+------+------+
13705  */
13706 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13707 {
13708     int opcode = extract32(insn, 10, 2);
13709     int rn = extract32(insn, 5, 5);
13710     int rd = extract32(insn, 0, 5);
13711     bool feature;
13712 
13713     switch (opcode) {
13714     case 0: /* SHA512SU0 */
13715         feature = dc_isar_feature(aa64_sha512, s);
13716         break;
13717     case 1: /* SM4E */
13718         feature = dc_isar_feature(aa64_sm4, s);
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 
13734     switch (opcode) {
13735     case 0: /* SHA512SU0 */
13736         gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
13737         break;
13738     case 1: /* SM4E */
13739         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
13740         break;
13741     default:
13742         g_assert_not_reached();
13743     }
13744 }
13745 
13746 /* Crypto four-register
13747  *  31               23 22 21 20  16 15  14  10 9    5 4    0
13748  * +-------------------+-----+------+---+------+------+------+
13749  * | 1 1 0 0 1 1 1 0 0 | Op0 |  Rm  | 0 |  Ra  |  Rn  |  Rd  |
13750  * +-------------------+-----+------+---+------+------+------+
13751  */
13752 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13753 {
13754     int op0 = extract32(insn, 21, 2);
13755     int rm = extract32(insn, 16, 5);
13756     int ra = extract32(insn, 10, 5);
13757     int rn = extract32(insn, 5, 5);
13758     int rd = extract32(insn, 0, 5);
13759     bool feature;
13760 
13761     switch (op0) {
13762     case 0: /* EOR3 */
13763     case 1: /* BCAX */
13764         feature = dc_isar_feature(aa64_sha3, s);
13765         break;
13766     case 2: /* SM3SS1 */
13767         feature = dc_isar_feature(aa64_sm3, s);
13768         break;
13769     default:
13770         unallocated_encoding(s);
13771         return;
13772     }
13773 
13774     if (!feature) {
13775         unallocated_encoding(s);
13776         return;
13777     }
13778 
13779     if (!fp_access_check(s)) {
13780         return;
13781     }
13782 
13783     if (op0 < 2) {
13784         TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13785         int pass;
13786 
13787         tcg_op1 = tcg_temp_new_i64();
13788         tcg_op2 = tcg_temp_new_i64();
13789         tcg_op3 = tcg_temp_new_i64();
13790         tcg_res[0] = tcg_temp_new_i64();
13791         tcg_res[1] = tcg_temp_new_i64();
13792 
13793         for (pass = 0; pass < 2; pass++) {
13794             read_vec_element(s, tcg_op1, rn, pass, MO_64);
13795             read_vec_element(s, tcg_op2, rm, pass, MO_64);
13796             read_vec_element(s, tcg_op3, ra, pass, MO_64);
13797 
13798             if (op0 == 0) {
13799                 /* EOR3 */
13800                 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13801             } else {
13802                 /* BCAX */
13803                 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13804             }
13805             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13806         }
13807         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13808         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13809     } else {
13810         TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13811 
13812         tcg_op1 = tcg_temp_new_i32();
13813         tcg_op2 = tcg_temp_new_i32();
13814         tcg_op3 = tcg_temp_new_i32();
13815         tcg_res = tcg_temp_new_i32();
13816         tcg_zero = tcg_constant_i32(0);
13817 
13818         read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13819         read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13820         read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13821 
13822         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13823         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13824         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13825         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13826 
13827         write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13828         write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13829         write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13830         write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13831     }
13832 }
13833 
13834 /* Crypto XAR
13835  *  31                   21 20  16 15    10 9    5 4    0
13836  * +-----------------------+------+--------+------+------+
13837  * | 1 1 0 0 1 1 1 0 1 0 0 |  Rm  |  imm6  |  Rn  |  Rd  |
13838  * +-----------------------+------+--------+------+------+
13839  */
13840 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
13841 {
13842     int rm = extract32(insn, 16, 5);
13843     int imm6 = extract32(insn, 10, 6);
13844     int rn = extract32(insn, 5, 5);
13845     int rd = extract32(insn, 0, 5);
13846 
13847     if (!dc_isar_feature(aa64_sha3, s)) {
13848         unallocated_encoding(s);
13849         return;
13850     }
13851 
13852     if (!fp_access_check(s)) {
13853         return;
13854     }
13855 
13856     gen_gvec_xar(MO_64, vec_full_reg_offset(s, rd),
13857                  vec_full_reg_offset(s, rn),
13858                  vec_full_reg_offset(s, rm), imm6, 16,
13859                  vec_full_reg_size(s));
13860 }
13861 
13862 /* Crypto three-reg imm2
13863  *  31                   21 20  16 15  14 13 12  11  10  9    5 4    0
13864  * +-----------------------+------+-----+------+--------+------+------+
13865  * | 1 1 0 0 1 1 1 0 0 1 0 |  Rm  | 1 0 | imm2 | opcode |  Rn  |  Rd  |
13866  * +-----------------------+------+-----+------+--------+------+------+
13867  */
13868 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
13869 {
13870     static gen_helper_gvec_3 * const fns[4] = {
13871         gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
13872         gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
13873     };
13874     int opcode = extract32(insn, 10, 2);
13875     int imm2 = extract32(insn, 12, 2);
13876     int rm = extract32(insn, 16, 5);
13877     int rn = extract32(insn, 5, 5);
13878     int rd = extract32(insn, 0, 5);
13879 
13880     if (!dc_isar_feature(aa64_sm3, s)) {
13881         unallocated_encoding(s);
13882         return;
13883     }
13884 
13885     if (!fp_access_check(s)) {
13886         return;
13887     }
13888 
13889     gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
13890 }
13891 
13892 /* C3.6 Data processing - SIMD, inc Crypto
13893  *
13894  * As the decode gets a little complex we are using a table based
13895  * approach for this part of the decode.
13896  */
13897 static const AArch64DecodeTable data_proc_simd[] = {
13898     /* pattern  ,  mask     ,  fn                        */
13899     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
13900     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
13901     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
13902     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
13903     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
13904     { 0x0e000400, 0x9fe08400, disas_simd_copy },
13905     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
13906     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
13907     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
13908     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
13909     { 0x0e000000, 0xbf208c00, disas_simd_tb },
13910     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
13911     { 0x2e000000, 0xbf208400, disas_simd_ext },
13912     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
13913     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
13914     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
13915     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
13916     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
13917     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
13918     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
13919     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
13920     { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
13921     { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
13922     { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
13923     { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
13924     { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
13925     { 0xce000000, 0xff808000, disas_crypto_four_reg },
13926     { 0xce800000, 0xffe00000, disas_crypto_xar },
13927     { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
13928     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
13929     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
13930     { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
13931     { 0x00000000, 0x00000000, NULL }
13932 };
13933 
13934 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
13935 {
13936     /* Note that this is called with all non-FP cases from
13937      * table C3-6 so it must UNDEF for entries not specifically
13938      * allocated to instructions in that table.
13939      */
13940     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
13941     if (fn) {
13942         fn(s, insn);
13943     } else {
13944         unallocated_encoding(s);
13945     }
13946 }
13947 
13948 /* C3.6 Data processing - SIMD and floating point */
13949 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
13950 {
13951     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
13952         disas_data_proc_fp(s, insn);
13953     } else {
13954         /* SIMD, including crypto */
13955         disas_data_proc_simd(s, insn);
13956     }
13957 }
13958 
13959 static bool trans_OK(DisasContext *s, arg_OK *a)
13960 {
13961     return true;
13962 }
13963 
13964 static bool trans_FAIL(DisasContext *s, arg_OK *a)
13965 {
13966     s->is_nonstreaming = true;
13967     return true;
13968 }
13969 
13970 /**
13971  * is_guarded_page:
13972  * @env: The cpu environment
13973  * @s: The DisasContext
13974  *
13975  * Return true if the page is guarded.
13976  */
13977 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
13978 {
13979     uint64_t addr = s->base.pc_first;
13980 #ifdef CONFIG_USER_ONLY
13981     return page_get_flags(addr) & PAGE_BTI;
13982 #else
13983     CPUTLBEntryFull *full;
13984     void *host;
13985     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
13986     int flags;
13987 
13988     /*
13989      * We test this immediately after reading an insn, which means
13990      * that the TLB entry must be present and valid, and thus this
13991      * access will never raise an exception.
13992      */
13993     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
13994                               false, &host, &full, 0);
13995     assert(!(flags & TLB_INVALID_MASK));
13996 
13997     return full->guarded;
13998 #endif
13999 }
14000 
14001 /**
14002  * btype_destination_ok:
14003  * @insn: The instruction at the branch destination
14004  * @bt: SCTLR_ELx.BT
14005  * @btype: PSTATE.BTYPE, and is non-zero
14006  *
14007  * On a guarded page, there are a limited number of insns
14008  * that may be present at the branch target:
14009  *   - branch target identifiers,
14010  *   - paciasp, pacibsp,
14011  *   - BRK insn
14012  *   - HLT insn
14013  * Anything else causes a Branch Target Exception.
14014  *
14015  * Return true if the branch is compatible, false to raise BTITRAP.
14016  */
14017 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
14018 {
14019     if ((insn & 0xfffff01fu) == 0xd503201fu) {
14020         /* HINT space */
14021         switch (extract32(insn, 5, 7)) {
14022         case 0b011001: /* PACIASP */
14023         case 0b011011: /* PACIBSP */
14024             /*
14025              * If SCTLR_ELx.BT, then PACI*SP are not compatible
14026              * with btype == 3.  Otherwise all btype are ok.
14027              */
14028             return !bt || btype != 3;
14029         case 0b100000: /* BTI */
14030             /* Not compatible with any btype.  */
14031             return false;
14032         case 0b100010: /* BTI c */
14033             /* Not compatible with btype == 3 */
14034             return btype != 3;
14035         case 0b100100: /* BTI j */
14036             /* Not compatible with btype == 2 */
14037             return btype != 2;
14038         case 0b100110: /* BTI jc */
14039             /* Compatible with any btype.  */
14040             return true;
14041         }
14042     } else {
14043         switch (insn & 0xffe0001fu) {
14044         case 0xd4200000u: /* BRK */
14045         case 0xd4400000u: /* HLT */
14046             /* Give priority to the breakpoint exception.  */
14047             return true;
14048         }
14049     }
14050     return false;
14051 }
14052 
14053 /* C3.1 A64 instruction index by encoding */
14054 static void disas_a64_legacy(DisasContext *s, uint32_t insn)
14055 {
14056     switch (extract32(insn, 25, 4)) {
14057     case 0xa: case 0xb: /* Branch, exception generation and system insns */
14058         disas_b_exc_sys(s, insn);
14059         break;
14060     case 0x4:
14061     case 0x6:
14062     case 0xc:
14063     case 0xe:      /* Loads and stores */
14064         disas_ldst(s, insn);
14065         break;
14066     case 0x5:
14067     case 0xd:      /* Data processing - register */
14068         disas_data_proc_reg(s, insn);
14069         break;
14070     case 0x7:
14071     case 0xf:      /* Data processing - SIMD and floating point */
14072         disas_data_proc_simd_fp(s, insn);
14073         break;
14074     default:
14075         unallocated_encoding(s);
14076         break;
14077     }
14078 }
14079 
14080 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14081                                           CPUState *cpu)
14082 {
14083     DisasContext *dc = container_of(dcbase, DisasContext, base);
14084     CPUARMState *env = cpu->env_ptr;
14085     ARMCPU *arm_cpu = env_archcpu(env);
14086     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
14087     int bound, core_mmu_idx;
14088 
14089     dc->isar = &arm_cpu->isar;
14090     dc->condjmp = 0;
14091     dc->pc_save = dc->base.pc_first;
14092     dc->aarch64 = true;
14093     dc->thumb = false;
14094     dc->sctlr_b = 0;
14095     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
14096     dc->condexec_mask = 0;
14097     dc->condexec_cond = 0;
14098     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
14099     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
14100     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
14101     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
14102     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
14103     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14104 #if !defined(CONFIG_USER_ONLY)
14105     dc->user = (dc->current_el == 0);
14106 #endif
14107     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
14108     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
14109     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
14110     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
14111     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
14112     dc->fgt_eret = EX_TBFLAG_A64(tb_flags, FGT_ERET);
14113     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
14114     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
14115     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
14116     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
14117     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
14118     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
14119     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
14120     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
14121     dc->ata = EX_TBFLAG_A64(tb_flags, ATA);
14122     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
14123     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
14124     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
14125     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
14126     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
14127     dc->vec_len = 0;
14128     dc->vec_stride = 0;
14129     dc->cp_regs = arm_cpu->cp_regs;
14130     dc->features = env->features;
14131     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
14132 
14133 #ifdef CONFIG_USER_ONLY
14134     /* In sve_probe_page, we assume TBI is enabled. */
14135     tcg_debug_assert(dc->tbid & 1);
14136 #endif
14137 
14138     dc->lse2 = dc_isar_feature(aa64_lse2, dc);
14139 
14140     /* Single step state. The code-generation logic here is:
14141      *  SS_ACTIVE == 0:
14142      *   generate code with no special handling for single-stepping (except
14143      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14144      *   this happens anyway because those changes are all system register or
14145      *   PSTATE writes).
14146      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14147      *   emit code for one insn
14148      *   emit code to clear PSTATE.SS
14149      *   emit code to generate software step exception for completed step
14150      *   end TB (as usual for having generated an exception)
14151      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14152      *   emit code to generate a software step exception
14153      *   end the TB
14154      */
14155     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
14156     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
14157     dc->is_ldex = false;
14158 
14159     /* Bound the number of insns to execute to those left on the page.  */
14160     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14161 
14162     /* If architectural single step active, limit to 1.  */
14163     if (dc->ss_active) {
14164         bound = 1;
14165     }
14166     dc->base.max_insns = MIN(dc->base.max_insns, bound);
14167 }
14168 
14169 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14170 {
14171 }
14172 
14173 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14174 {
14175     DisasContext *dc = container_of(dcbase, DisasContext, base);
14176     target_ulong pc_arg = dc->base.pc_next;
14177 
14178     if (tb_cflags(dcbase->tb) & CF_PCREL) {
14179         pc_arg &= ~TARGET_PAGE_MASK;
14180     }
14181     tcg_gen_insn_start(pc_arg, 0, 0);
14182     dc->insn_start = tcg_last_op();
14183 }
14184 
14185 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14186 {
14187     DisasContext *s = container_of(dcbase, DisasContext, base);
14188     CPUARMState *env = cpu->env_ptr;
14189     uint64_t pc = s->base.pc_next;
14190     uint32_t insn;
14191 
14192     /* Singlestep exceptions have the highest priority. */
14193     if (s->ss_active && !s->pstate_ss) {
14194         /* Singlestep state is Active-pending.
14195          * If we're in this state at the start of a TB then either
14196          *  a) we just took an exception to an EL which is being debugged
14197          *     and this is the first insn in the exception handler
14198          *  b) debug exceptions were masked and we just unmasked them
14199          *     without changing EL (eg by clearing PSTATE.D)
14200          * In either case we're going to take a swstep exception in the
14201          * "did not step an insn" case, and so the syndrome ISV and EX
14202          * bits should be zero.
14203          */
14204         assert(s->base.num_insns == 1);
14205         gen_swstep_exception(s, 0, 0);
14206         s->base.is_jmp = DISAS_NORETURN;
14207         s->base.pc_next = pc + 4;
14208         return;
14209     }
14210 
14211     if (pc & 3) {
14212         /*
14213          * PC alignment fault.  This has priority over the instruction abort
14214          * that we would receive from a translation fault via arm_ldl_code.
14215          * This should only be possible after an indirect branch, at the
14216          * start of the TB.
14217          */
14218         assert(s->base.num_insns == 1);
14219         gen_helper_exception_pc_alignment(cpu_env, tcg_constant_tl(pc));
14220         s->base.is_jmp = DISAS_NORETURN;
14221         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
14222         return;
14223     }
14224 
14225     s->pc_curr = pc;
14226     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
14227     s->insn = insn;
14228     s->base.pc_next = pc + 4;
14229 
14230     s->fp_access_checked = false;
14231     s->sve_access_checked = false;
14232 
14233     if (s->pstate_il) {
14234         /*
14235          * Illegal execution state. This has priority over BTI
14236          * exceptions, but comes after instruction abort exceptions.
14237          */
14238         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
14239         return;
14240     }
14241 
14242     if (dc_isar_feature(aa64_bti, s)) {
14243         if (s->base.num_insns == 1) {
14244             /*
14245              * At the first insn of the TB, compute s->guarded_page.
14246              * We delayed computing this until successfully reading
14247              * the first insn of the TB, above.  This (mostly) ensures
14248              * that the softmmu tlb entry has been populated, and the
14249              * page table GP bit is available.
14250              *
14251              * Note that we need to compute this even if btype == 0,
14252              * because this value is used for BR instructions later
14253              * where ENV is not available.
14254              */
14255             s->guarded_page = is_guarded_page(env, s);
14256 
14257             /* First insn can have btype set to non-zero.  */
14258             tcg_debug_assert(s->btype >= 0);
14259 
14260             /*
14261              * Note that the Branch Target Exception has fairly high
14262              * priority -- below debugging exceptions but above most
14263              * everything else.  This allows us to handle this now
14264              * instead of waiting until the insn is otherwise decoded.
14265              */
14266             if (s->btype != 0
14267                 && s->guarded_page
14268                 && !btype_destination_ok(insn, s->bt, s->btype)) {
14269                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
14270                 return;
14271             }
14272         } else {
14273             /* Not the first insn: btype must be 0.  */
14274             tcg_debug_assert(s->btype == 0);
14275         }
14276     }
14277 
14278     s->is_nonstreaming = false;
14279     if (s->sme_trap_nonstreaming) {
14280         disas_sme_fa64(s, insn);
14281     }
14282 
14283     if (!disas_a64(s, insn) &&
14284         !disas_sme(s, insn) &&
14285         !disas_sve(s, insn)) {
14286         disas_a64_legacy(s, insn);
14287     }
14288 
14289     /*
14290      * After execution of most insns, btype is reset to 0.
14291      * Note that we set btype == -1 when the insn sets btype.
14292      */
14293     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14294         reset_btype(s);
14295     }
14296 }
14297 
14298 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14299 {
14300     DisasContext *dc = container_of(dcbase, DisasContext, base);
14301 
14302     if (unlikely(dc->ss_active)) {
14303         /* Note that this means single stepping WFI doesn't halt the CPU.
14304          * For conditional branch insns this is harmless unreachable code as
14305          * gen_goto_tb() has already handled emitting the debug exception
14306          * (and thus a tb-jump is not possible when singlestepping).
14307          */
14308         switch (dc->base.is_jmp) {
14309         default:
14310             gen_a64_update_pc(dc, 4);
14311             /* fall through */
14312         case DISAS_EXIT:
14313         case DISAS_JUMP:
14314             gen_step_complete_exception(dc);
14315             break;
14316         case DISAS_NORETURN:
14317             break;
14318         }
14319     } else {
14320         switch (dc->base.is_jmp) {
14321         case DISAS_NEXT:
14322         case DISAS_TOO_MANY:
14323             gen_goto_tb(dc, 1, 4);
14324             break;
14325         default:
14326         case DISAS_UPDATE_EXIT:
14327             gen_a64_update_pc(dc, 4);
14328             /* fall through */
14329         case DISAS_EXIT:
14330             tcg_gen_exit_tb(NULL, 0);
14331             break;
14332         case DISAS_UPDATE_NOCHAIN:
14333             gen_a64_update_pc(dc, 4);
14334             /* fall through */
14335         case DISAS_JUMP:
14336             tcg_gen_lookup_and_goto_ptr();
14337             break;
14338         case DISAS_NORETURN:
14339         case DISAS_SWI:
14340             break;
14341         case DISAS_WFE:
14342             gen_a64_update_pc(dc, 4);
14343             gen_helper_wfe(cpu_env);
14344             break;
14345         case DISAS_YIELD:
14346             gen_a64_update_pc(dc, 4);
14347             gen_helper_yield(cpu_env);
14348             break;
14349         case DISAS_WFI:
14350             /*
14351              * This is a special case because we don't want to just halt
14352              * the CPU if trying to debug across a WFI.
14353              */
14354             gen_a64_update_pc(dc, 4);
14355             gen_helper_wfi(cpu_env, tcg_constant_i32(4));
14356             /*
14357              * The helper doesn't necessarily throw an exception, but we
14358              * must go back to the main loop to check for interrupts anyway.
14359              */
14360             tcg_gen_exit_tb(NULL, 0);
14361             break;
14362         }
14363     }
14364 }
14365 
14366 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14367                                  CPUState *cpu, FILE *logfile)
14368 {
14369     DisasContext *dc = container_of(dcbase, DisasContext, base);
14370 
14371     fprintf(logfile, "IN: %s\n", lookup_symbol(dc->base.pc_first));
14372     target_disas(logfile, cpu, dc->base.pc_first, dc->base.tb->size);
14373 }
14374 
14375 const TranslatorOps aarch64_translator_ops = {
14376     .init_disas_context = aarch64_tr_init_disas_context,
14377     .tb_start           = aarch64_tr_tb_start,
14378     .insn_start         = aarch64_tr_insn_start,
14379     .translate_insn     = aarch64_tr_translate_insn,
14380     .tb_stop            = aarch64_tr_tb_stop,
14381     .disas_log          = aarch64_tr_disas_log,
14382 };
14383