xref: /openbmc/qemu/target/arm/tcg/translate-a64.c (revision a75b66f6)
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     tcg_gen_qemu_st_i64(source, tcg_addr, memidx, memop);
842 
843     if (iss_valid) {
844         uint32_t syn;
845 
846         syn = syn_data_abort_with_iss(0,
847                                       (memop & MO_SIZE),
848                                       false,
849                                       iss_srt,
850                                       iss_sf,
851                                       iss_ar,
852                                       0, 0, 0, 0, 0, false);
853         disas_set_insn_syndrome(s, syn);
854     }
855 }
856 
857 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
858                       TCGv_i64 tcg_addr, MemOp memop,
859                       bool iss_valid,
860                       unsigned int iss_srt,
861                       bool iss_sf, bool iss_ar)
862 {
863     do_gpr_st_memidx(s, source, tcg_addr, memop, get_mem_index(s),
864                      iss_valid, iss_srt, iss_sf, iss_ar);
865 }
866 
867 /*
868  * Load from memory to GPR register
869  */
870 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
871                              MemOp memop, bool extend, int memidx,
872                              bool iss_valid, unsigned int iss_srt,
873                              bool iss_sf, bool iss_ar)
874 {
875     tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
876 
877     if (extend && (memop & MO_SIGN)) {
878         g_assert((memop & MO_SIZE) <= MO_32);
879         tcg_gen_ext32u_i64(dest, dest);
880     }
881 
882     if (iss_valid) {
883         uint32_t syn;
884 
885         syn = syn_data_abort_with_iss(0,
886                                       (memop & MO_SIZE),
887                                       (memop & MO_SIGN) != 0,
888                                       iss_srt,
889                                       iss_sf,
890                                       iss_ar,
891                                       0, 0, 0, 0, 0, false);
892         disas_set_insn_syndrome(s, syn);
893     }
894 }
895 
896 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
897                       MemOp memop, bool extend,
898                       bool iss_valid, unsigned int iss_srt,
899                       bool iss_sf, bool iss_ar)
900 {
901     do_gpr_ld_memidx(s, dest, tcg_addr, memop, extend, get_mem_index(s),
902                      iss_valid, iss_srt, iss_sf, iss_ar);
903 }
904 
905 /*
906  * Store from FP register to memory
907  */
908 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
909 {
910     /* This writes the bottom N bits of a 128 bit wide vector to memory */
911     TCGv_i64 tmplo = tcg_temp_new_i64();
912     MemOp mop = finalize_memop_asimd(s, size);
913 
914     tcg_gen_ld_i64(tmplo, cpu_env, fp_reg_offset(s, srcidx, MO_64));
915 
916     if (size < MO_128) {
917         tcg_gen_qemu_st_i64(tmplo, tcg_addr, get_mem_index(s), mop);
918     } else {
919         TCGv_i64 tmphi = tcg_temp_new_i64();
920         TCGv_i128 t16 = tcg_temp_new_i128();
921 
922         tcg_gen_ld_i64(tmphi, cpu_env, fp_reg_hi_offset(s, srcidx));
923         tcg_gen_concat_i64_i128(t16, tmplo, tmphi);
924 
925         tcg_gen_qemu_st_i128(t16, tcg_addr, get_mem_index(s), mop);
926     }
927 }
928 
929 /*
930  * Load from memory to FP register
931  */
932 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
933 {
934     /* This always zero-extends and writes to a full 128 bit wide vector */
935     TCGv_i64 tmplo = tcg_temp_new_i64();
936     TCGv_i64 tmphi = NULL;
937     MemOp mop = finalize_memop_asimd(s, size);
938 
939     if (size < MO_128) {
940         tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), mop);
941     } else {
942         TCGv_i128 t16 = tcg_temp_new_i128();
943 
944         tcg_gen_qemu_ld_i128(t16, tcg_addr, get_mem_index(s), mop);
945 
946         tmphi = tcg_temp_new_i64();
947         tcg_gen_extr_i128_i64(tmplo, tmphi, t16);
948     }
949 
950     tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
951 
952     if (tmphi) {
953         tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
954     }
955     clear_vec_high(s, tmphi != NULL, destidx);
956 }
957 
958 /*
959  * Vector load/store helpers.
960  *
961  * The principal difference between this and a FP load is that we don't
962  * zero extend as we are filling a partial chunk of the vector register.
963  * These functions don't support 128 bit loads/stores, which would be
964  * normal load/store operations.
965  *
966  * The _i32 versions are useful when operating on 32 bit quantities
967  * (eg for floating point single or using Neon helper functions).
968  */
969 
970 /* Get value of an element within a vector register */
971 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
972                              int element, MemOp memop)
973 {
974     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
975     switch ((unsigned)memop) {
976     case MO_8:
977         tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
978         break;
979     case MO_16:
980         tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
981         break;
982     case MO_32:
983         tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
984         break;
985     case MO_8|MO_SIGN:
986         tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
987         break;
988     case MO_16|MO_SIGN:
989         tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
990         break;
991     case MO_32|MO_SIGN:
992         tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
993         break;
994     case MO_64:
995     case MO_64|MO_SIGN:
996         tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
997         break;
998     default:
999         g_assert_not_reached();
1000     }
1001 }
1002 
1003 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1004                                  int element, MemOp memop)
1005 {
1006     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1007     switch (memop) {
1008     case MO_8:
1009         tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1010         break;
1011     case MO_16:
1012         tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1013         break;
1014     case MO_8|MO_SIGN:
1015         tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1016         break;
1017     case MO_16|MO_SIGN:
1018         tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1019         break;
1020     case MO_32:
1021     case MO_32|MO_SIGN:
1022         tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1023         break;
1024     default:
1025         g_assert_not_reached();
1026     }
1027 }
1028 
1029 /* Set value of an element within a vector register */
1030 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1031                               int element, MemOp memop)
1032 {
1033     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1034     switch (memop) {
1035     case MO_8:
1036         tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1037         break;
1038     case MO_16:
1039         tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1040         break;
1041     case MO_32:
1042         tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1043         break;
1044     case MO_64:
1045         tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1046         break;
1047     default:
1048         g_assert_not_reached();
1049     }
1050 }
1051 
1052 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1053                                   int destidx, int element, MemOp memop)
1054 {
1055     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1056     switch (memop) {
1057     case MO_8:
1058         tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1059         break;
1060     case MO_16:
1061         tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1062         break;
1063     case MO_32:
1064         tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1065         break;
1066     default:
1067         g_assert_not_reached();
1068     }
1069 }
1070 
1071 /* Store from vector register to memory */
1072 static void do_vec_st(DisasContext *s, int srcidx, int element,
1073                       TCGv_i64 tcg_addr, MemOp mop)
1074 {
1075     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1076 
1077     read_vec_element(s, tcg_tmp, srcidx, element, mop & MO_SIZE);
1078     tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1079 }
1080 
1081 /* Load from memory to vector register */
1082 static void do_vec_ld(DisasContext *s, int destidx, int element,
1083                       TCGv_i64 tcg_addr, MemOp mop)
1084 {
1085     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1086 
1087     tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1088     write_vec_element(s, tcg_tmp, destidx, element, mop & MO_SIZE);
1089 }
1090 
1091 /* Check that FP/Neon access is enabled. If it is, return
1092  * true. If not, emit code to generate an appropriate exception,
1093  * and return false; the caller should not emit any code for
1094  * the instruction. Note that this check must happen after all
1095  * unallocated-encoding checks (otherwise the syndrome information
1096  * for the resulting exception will be incorrect).
1097  */
1098 static bool fp_access_check_only(DisasContext *s)
1099 {
1100     if (s->fp_excp_el) {
1101         assert(!s->fp_access_checked);
1102         s->fp_access_checked = true;
1103 
1104         gen_exception_insn_el(s, 0, EXCP_UDEF,
1105                               syn_fp_access_trap(1, 0xe, false, 0),
1106                               s->fp_excp_el);
1107         return false;
1108     }
1109     s->fp_access_checked = true;
1110     return true;
1111 }
1112 
1113 static bool fp_access_check(DisasContext *s)
1114 {
1115     if (!fp_access_check_only(s)) {
1116         return false;
1117     }
1118     if (s->sme_trap_nonstreaming && s->is_nonstreaming) {
1119         gen_exception_insn(s, 0, EXCP_UDEF,
1120                            syn_smetrap(SME_ET_Streaming, false));
1121         return false;
1122     }
1123     return true;
1124 }
1125 
1126 /*
1127  * Check that SVE access is enabled.  If it is, return true.
1128  * If not, emit code to generate an appropriate exception and return false.
1129  * This function corresponds to CheckSVEEnabled().
1130  */
1131 bool sve_access_check(DisasContext *s)
1132 {
1133     if (s->pstate_sm || !dc_isar_feature(aa64_sve, s)) {
1134         assert(dc_isar_feature(aa64_sme, s));
1135         if (!sme_sm_enabled_check(s)) {
1136             goto fail_exit;
1137         }
1138     } else if (s->sve_excp_el) {
1139         gen_exception_insn_el(s, 0, EXCP_UDEF,
1140                               syn_sve_access_trap(), s->sve_excp_el);
1141         goto fail_exit;
1142     }
1143     s->sve_access_checked = true;
1144     return fp_access_check(s);
1145 
1146  fail_exit:
1147     /* Assert that we only raise one exception per instruction. */
1148     assert(!s->sve_access_checked);
1149     s->sve_access_checked = true;
1150     return false;
1151 }
1152 
1153 /*
1154  * Check that SME access is enabled, raise an exception if not.
1155  * Note that this function corresponds to CheckSMEAccess and is
1156  * only used directly for cpregs.
1157  */
1158 static bool sme_access_check(DisasContext *s)
1159 {
1160     if (s->sme_excp_el) {
1161         gen_exception_insn_el(s, 0, EXCP_UDEF,
1162                               syn_smetrap(SME_ET_AccessTrap, false),
1163                               s->sme_excp_el);
1164         return false;
1165     }
1166     return true;
1167 }
1168 
1169 /* This function corresponds to CheckSMEEnabled. */
1170 bool sme_enabled_check(DisasContext *s)
1171 {
1172     /*
1173      * Note that unlike sve_excp_el, we have not constrained sme_excp_el
1174      * to be zero when fp_excp_el has priority.  This is because we need
1175      * sme_excp_el by itself for cpregs access checks.
1176      */
1177     if (!s->fp_excp_el || s->sme_excp_el < s->fp_excp_el) {
1178         s->fp_access_checked = true;
1179         return sme_access_check(s);
1180     }
1181     return fp_access_check_only(s);
1182 }
1183 
1184 /* Common subroutine for CheckSMEAnd*Enabled. */
1185 bool sme_enabled_check_with_svcr(DisasContext *s, unsigned req)
1186 {
1187     if (!sme_enabled_check(s)) {
1188         return false;
1189     }
1190     if (FIELD_EX64(req, SVCR, SM) && !s->pstate_sm) {
1191         gen_exception_insn(s, 0, EXCP_UDEF,
1192                            syn_smetrap(SME_ET_NotStreaming, false));
1193         return false;
1194     }
1195     if (FIELD_EX64(req, SVCR, ZA) && !s->pstate_za) {
1196         gen_exception_insn(s, 0, EXCP_UDEF,
1197                            syn_smetrap(SME_ET_InactiveZA, false));
1198         return false;
1199     }
1200     return true;
1201 }
1202 
1203 /*
1204  * This utility function is for doing register extension with an
1205  * optional shift. You will likely want to pass a temporary for the
1206  * destination register. See DecodeRegExtend() in the ARM ARM.
1207  */
1208 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1209                               int option, unsigned int shift)
1210 {
1211     int extsize = extract32(option, 0, 2);
1212     bool is_signed = extract32(option, 2, 1);
1213 
1214     if (is_signed) {
1215         switch (extsize) {
1216         case 0:
1217             tcg_gen_ext8s_i64(tcg_out, tcg_in);
1218             break;
1219         case 1:
1220             tcg_gen_ext16s_i64(tcg_out, tcg_in);
1221             break;
1222         case 2:
1223             tcg_gen_ext32s_i64(tcg_out, tcg_in);
1224             break;
1225         case 3:
1226             tcg_gen_mov_i64(tcg_out, tcg_in);
1227             break;
1228         }
1229     } else {
1230         switch (extsize) {
1231         case 0:
1232             tcg_gen_ext8u_i64(tcg_out, tcg_in);
1233             break;
1234         case 1:
1235             tcg_gen_ext16u_i64(tcg_out, tcg_in);
1236             break;
1237         case 2:
1238             tcg_gen_ext32u_i64(tcg_out, tcg_in);
1239             break;
1240         case 3:
1241             tcg_gen_mov_i64(tcg_out, tcg_in);
1242             break;
1243         }
1244     }
1245 
1246     if (shift) {
1247         tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1248     }
1249 }
1250 
1251 static inline void gen_check_sp_alignment(DisasContext *s)
1252 {
1253     /* The AArch64 architecture mandates that (if enabled via PSTATE
1254      * or SCTLR bits) there is a check that SP is 16-aligned on every
1255      * SP-relative load or store (with an exception generated if it is not).
1256      * In line with general QEMU practice regarding misaligned accesses,
1257      * we omit these checks for the sake of guest program performance.
1258      * This function is provided as a hook so we can more easily add these
1259      * checks in future (possibly as a "favour catching guest program bugs
1260      * over speed" user selectable option).
1261      */
1262 }
1263 
1264 /*
1265  * This provides a simple table based table lookup decoder. It is
1266  * intended to be used when the relevant bits for decode are too
1267  * awkwardly placed and switch/if based logic would be confusing and
1268  * deeply nested. Since it's a linear search through the table, tables
1269  * should be kept small.
1270  *
1271  * It returns the first handler where insn & mask == pattern, or
1272  * NULL if there is no match.
1273  * The table is terminated by an empty mask (i.e. 0)
1274  */
1275 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1276                                                uint32_t insn)
1277 {
1278     const AArch64DecodeTable *tptr = table;
1279 
1280     while (tptr->mask) {
1281         if ((insn & tptr->mask) == tptr->pattern) {
1282             return tptr->disas_fn;
1283         }
1284         tptr++;
1285     }
1286     return NULL;
1287 }
1288 
1289 /*
1290  * The instruction disassembly implemented here matches
1291  * the instruction encoding classifications in chapter C4
1292  * of the ARM Architecture Reference Manual (DDI0487B_a);
1293  * classification names and decode diagrams here should generally
1294  * match up with those in the manual.
1295  */
1296 
1297 static bool trans_B(DisasContext *s, arg_i *a)
1298 {
1299     reset_btype(s);
1300     gen_goto_tb(s, 0, a->imm);
1301     return true;
1302 }
1303 
1304 static bool trans_BL(DisasContext *s, arg_i *a)
1305 {
1306     gen_pc_plus_diff(s, cpu_reg(s, 30), curr_insn_len(s));
1307     reset_btype(s);
1308     gen_goto_tb(s, 0, a->imm);
1309     return true;
1310 }
1311 
1312 
1313 static bool trans_CBZ(DisasContext *s, arg_cbz *a)
1314 {
1315     DisasLabel match;
1316     TCGv_i64 tcg_cmp;
1317 
1318     tcg_cmp = read_cpu_reg(s, a->rt, a->sf);
1319     reset_btype(s);
1320 
1321     match = gen_disas_label(s);
1322     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1323                         tcg_cmp, 0, match.label);
1324     gen_goto_tb(s, 0, 4);
1325     set_disas_label(s, match);
1326     gen_goto_tb(s, 1, a->imm);
1327     return true;
1328 }
1329 
1330 static bool trans_TBZ(DisasContext *s, arg_tbz *a)
1331 {
1332     DisasLabel match;
1333     TCGv_i64 tcg_cmp;
1334 
1335     tcg_cmp = tcg_temp_new_i64();
1336     tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, a->rt), 1ULL << a->bitpos);
1337 
1338     reset_btype(s);
1339 
1340     match = gen_disas_label(s);
1341     tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
1342                         tcg_cmp, 0, match.label);
1343     gen_goto_tb(s, 0, 4);
1344     set_disas_label(s, match);
1345     gen_goto_tb(s, 1, a->imm);
1346     return true;
1347 }
1348 
1349 static bool trans_B_cond(DisasContext *s, arg_B_cond *a)
1350 {
1351     reset_btype(s);
1352     if (a->cond < 0x0e) {
1353         /* genuinely conditional branches */
1354         DisasLabel match = gen_disas_label(s);
1355         arm_gen_test_cc(a->cond, match.label);
1356         gen_goto_tb(s, 0, 4);
1357         set_disas_label(s, match);
1358         gen_goto_tb(s, 1, a->imm);
1359     } else {
1360         /* 0xe and 0xf are both "always" conditions */
1361         gen_goto_tb(s, 0, a->imm);
1362     }
1363     return true;
1364 }
1365 
1366 static void set_btype_for_br(DisasContext *s, int rn)
1367 {
1368     if (dc_isar_feature(aa64_bti, s)) {
1369         /* BR to {x16,x17} or !guard -> 1, else 3.  */
1370         set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
1371     }
1372 }
1373 
1374 static void set_btype_for_blr(DisasContext *s)
1375 {
1376     if (dc_isar_feature(aa64_bti, s)) {
1377         /* BLR sets BTYPE to 2, regardless of source guarded page.  */
1378         set_btype(s, 2);
1379     }
1380 }
1381 
1382 static bool trans_BR(DisasContext *s, arg_r *a)
1383 {
1384     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1385     set_btype_for_br(s, a->rn);
1386     s->base.is_jmp = DISAS_JUMP;
1387     return true;
1388 }
1389 
1390 static bool trans_BLR(DisasContext *s, arg_r *a)
1391 {
1392     TCGv_i64 dst = cpu_reg(s, a->rn);
1393     TCGv_i64 lr = cpu_reg(s, 30);
1394     if (dst == lr) {
1395         TCGv_i64 tmp = tcg_temp_new_i64();
1396         tcg_gen_mov_i64(tmp, dst);
1397         dst = tmp;
1398     }
1399     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1400     gen_a64_set_pc(s, dst);
1401     set_btype_for_blr(s);
1402     s->base.is_jmp = DISAS_JUMP;
1403     return true;
1404 }
1405 
1406 static bool trans_RET(DisasContext *s, arg_r *a)
1407 {
1408     gen_a64_set_pc(s, cpu_reg(s, a->rn));
1409     s->base.is_jmp = DISAS_JUMP;
1410     return true;
1411 }
1412 
1413 static TCGv_i64 auth_branch_target(DisasContext *s, TCGv_i64 dst,
1414                                    TCGv_i64 modifier, bool use_key_a)
1415 {
1416     TCGv_i64 truedst;
1417     /*
1418      * Return the branch target for a BRAA/RETA/etc, which is either
1419      * just the destination dst, or that value with the pauth check
1420      * done and the code removed from the high bits.
1421      */
1422     if (!s->pauth_active) {
1423         return dst;
1424     }
1425 
1426     truedst = tcg_temp_new_i64();
1427     if (use_key_a) {
1428         gen_helper_autia(truedst, cpu_env, dst, modifier);
1429     } else {
1430         gen_helper_autib(truedst, cpu_env, dst, modifier);
1431     }
1432     return truedst;
1433 }
1434 
1435 static bool trans_BRAZ(DisasContext *s, arg_braz *a)
1436 {
1437     TCGv_i64 dst;
1438 
1439     if (!dc_isar_feature(aa64_pauth, s)) {
1440         return false;
1441     }
1442 
1443     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1444     gen_a64_set_pc(s, dst);
1445     set_btype_for_br(s, a->rn);
1446     s->base.is_jmp = DISAS_JUMP;
1447     return true;
1448 }
1449 
1450 static bool trans_BLRAZ(DisasContext *s, arg_braz *a)
1451 {
1452     TCGv_i64 dst, lr;
1453 
1454     if (!dc_isar_feature(aa64_pauth, s)) {
1455         return false;
1456     }
1457 
1458     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
1459     lr = cpu_reg(s, 30);
1460     if (dst == lr) {
1461         TCGv_i64 tmp = tcg_temp_new_i64();
1462         tcg_gen_mov_i64(tmp, dst);
1463         dst = tmp;
1464     }
1465     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1466     gen_a64_set_pc(s, dst);
1467     set_btype_for_blr(s);
1468     s->base.is_jmp = DISAS_JUMP;
1469     return true;
1470 }
1471 
1472 static bool trans_RETA(DisasContext *s, arg_reta *a)
1473 {
1474     TCGv_i64 dst;
1475 
1476     dst = auth_branch_target(s, cpu_reg(s, 30), cpu_X[31], !a->m);
1477     gen_a64_set_pc(s, dst);
1478     s->base.is_jmp = DISAS_JUMP;
1479     return true;
1480 }
1481 
1482 static bool trans_BRA(DisasContext *s, arg_bra *a)
1483 {
1484     TCGv_i64 dst;
1485 
1486     if (!dc_isar_feature(aa64_pauth, s)) {
1487         return false;
1488     }
1489     dst = auth_branch_target(s, cpu_reg(s,a->rn), cpu_reg_sp(s, a->rm), !a->m);
1490     gen_a64_set_pc(s, dst);
1491     set_btype_for_br(s, a->rn);
1492     s->base.is_jmp = DISAS_JUMP;
1493     return true;
1494 }
1495 
1496 static bool trans_BLRA(DisasContext *s, arg_bra *a)
1497 {
1498     TCGv_i64 dst, lr;
1499 
1500     if (!dc_isar_feature(aa64_pauth, s)) {
1501         return false;
1502     }
1503     dst = auth_branch_target(s, cpu_reg(s, a->rn), cpu_reg_sp(s, a->rm), !a->m);
1504     lr = cpu_reg(s, 30);
1505     if (dst == lr) {
1506         TCGv_i64 tmp = tcg_temp_new_i64();
1507         tcg_gen_mov_i64(tmp, dst);
1508         dst = tmp;
1509     }
1510     gen_pc_plus_diff(s, lr, curr_insn_len(s));
1511     gen_a64_set_pc(s, dst);
1512     set_btype_for_blr(s);
1513     s->base.is_jmp = DISAS_JUMP;
1514     return true;
1515 }
1516 
1517 static bool trans_ERET(DisasContext *s, arg_ERET *a)
1518 {
1519     TCGv_i64 dst;
1520 
1521     if (s->current_el == 0) {
1522         return false;
1523     }
1524     if (s->fgt_eret) {
1525         gen_exception_insn_el(s, 0, EXCP_UDEF, 0, 2);
1526         return true;
1527     }
1528     dst = tcg_temp_new_i64();
1529     tcg_gen_ld_i64(dst, cpu_env,
1530                    offsetof(CPUARMState, elr_el[s->current_el]));
1531 
1532     translator_io_start(&s->base);
1533 
1534     gen_helper_exception_return(cpu_env, dst);
1535     /* Must exit loop to check un-masked IRQs */
1536     s->base.is_jmp = DISAS_EXIT;
1537     return true;
1538 }
1539 
1540 static bool trans_ERETA(DisasContext *s, arg_reta *a)
1541 {
1542     TCGv_i64 dst;
1543 
1544     if (!dc_isar_feature(aa64_pauth, s)) {
1545         return false;
1546     }
1547     if (s->current_el == 0) {
1548         return false;
1549     }
1550     /* The FGT trap takes precedence over an auth trap. */
1551     if (s->fgt_eret) {
1552         gen_exception_insn_el(s, 0, EXCP_UDEF, a->m ? 3 : 2, 2);
1553         return true;
1554     }
1555     dst = tcg_temp_new_i64();
1556     tcg_gen_ld_i64(dst, cpu_env,
1557                    offsetof(CPUARMState, elr_el[s->current_el]));
1558 
1559     dst = auth_branch_target(s, dst, cpu_X[31], !a->m);
1560 
1561     translator_io_start(&s->base);
1562 
1563     gen_helper_exception_return(cpu_env, dst);
1564     /* Must exit loop to check un-masked IRQs */
1565     s->base.is_jmp = DISAS_EXIT;
1566     return true;
1567 }
1568 
1569 /* HINT instruction group, including various allocated HINTs */
1570 static void handle_hint(DisasContext *s, uint32_t insn,
1571                         unsigned int op1, unsigned int op2, unsigned int crm)
1572 {
1573     unsigned int selector = crm << 3 | op2;
1574 
1575     if (op1 != 3) {
1576         unallocated_encoding(s);
1577         return;
1578     }
1579 
1580     switch (selector) {
1581     case 0b00000: /* NOP */
1582         break;
1583     case 0b00011: /* WFI */
1584         s->base.is_jmp = DISAS_WFI;
1585         break;
1586     case 0b00001: /* YIELD */
1587         /* When running in MTTCG we don't generate jumps to the yield and
1588          * WFE helpers as it won't affect the scheduling of other vCPUs.
1589          * If we wanted to more completely model WFE/SEV so we don't busy
1590          * spin unnecessarily we would need to do something more involved.
1591          */
1592         if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1593             s->base.is_jmp = DISAS_YIELD;
1594         }
1595         break;
1596     case 0b00010: /* WFE */
1597         if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1598             s->base.is_jmp = DISAS_WFE;
1599         }
1600         break;
1601     case 0b00100: /* SEV */
1602     case 0b00101: /* SEVL */
1603     case 0b00110: /* DGH */
1604         /* we treat all as NOP at least for now */
1605         break;
1606     case 0b00111: /* XPACLRI */
1607         if (s->pauth_active) {
1608             gen_helper_xpaci(cpu_X[30], cpu_env, cpu_X[30]);
1609         }
1610         break;
1611     case 0b01000: /* PACIA1716 */
1612         if (s->pauth_active) {
1613             gen_helper_pacia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1614         }
1615         break;
1616     case 0b01010: /* PACIB1716 */
1617         if (s->pauth_active) {
1618             gen_helper_pacib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1619         }
1620         break;
1621     case 0b01100: /* AUTIA1716 */
1622         if (s->pauth_active) {
1623             gen_helper_autia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1624         }
1625         break;
1626     case 0b01110: /* AUTIB1716 */
1627         if (s->pauth_active) {
1628             gen_helper_autib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1629         }
1630         break;
1631     case 0b10000: /* ESB */
1632         /* Without RAS, we must implement this as NOP. */
1633         if (dc_isar_feature(aa64_ras, s)) {
1634             /*
1635              * QEMU does not have a source of physical SErrors,
1636              * so we are only concerned with virtual SErrors.
1637              * The pseudocode in the ARM for this case is
1638              *   if PSTATE.EL IN {EL0, EL1} && EL2Enabled() then
1639              *      AArch64.vESBOperation();
1640              * Most of the condition can be evaluated at translation time.
1641              * Test for EL2 present, and defer test for SEL2 to runtime.
1642              */
1643             if (s->current_el <= 1 && arm_dc_feature(s, ARM_FEATURE_EL2)) {
1644                 gen_helper_vesb(cpu_env);
1645             }
1646         }
1647         break;
1648     case 0b11000: /* PACIAZ */
1649         if (s->pauth_active) {
1650             gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30],
1651                              tcg_constant_i64(0));
1652         }
1653         break;
1654     case 0b11001: /* PACIASP */
1655         if (s->pauth_active) {
1656             gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1657         }
1658         break;
1659     case 0b11010: /* PACIBZ */
1660         if (s->pauth_active) {
1661             gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30],
1662                              tcg_constant_i64(0));
1663         }
1664         break;
1665     case 0b11011: /* PACIBSP */
1666         if (s->pauth_active) {
1667             gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1668         }
1669         break;
1670     case 0b11100: /* AUTIAZ */
1671         if (s->pauth_active) {
1672             gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30],
1673                              tcg_constant_i64(0));
1674         }
1675         break;
1676     case 0b11101: /* AUTIASP */
1677         if (s->pauth_active) {
1678             gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1679         }
1680         break;
1681     case 0b11110: /* AUTIBZ */
1682         if (s->pauth_active) {
1683             gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30],
1684                              tcg_constant_i64(0));
1685         }
1686         break;
1687     case 0b11111: /* AUTIBSP */
1688         if (s->pauth_active) {
1689             gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1690         }
1691         break;
1692     default:
1693         /* default specified as NOP equivalent */
1694         break;
1695     }
1696 }
1697 
1698 static void gen_clrex(DisasContext *s, uint32_t insn)
1699 {
1700     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1701 }
1702 
1703 /* CLREX, DSB, DMB, ISB */
1704 static void handle_sync(DisasContext *s, uint32_t insn,
1705                         unsigned int op1, unsigned int op2, unsigned int crm)
1706 {
1707     TCGBar bar;
1708 
1709     if (op1 != 3) {
1710         unallocated_encoding(s);
1711         return;
1712     }
1713 
1714     switch (op2) {
1715     case 2: /* CLREX */
1716         gen_clrex(s, insn);
1717         return;
1718     case 4: /* DSB */
1719     case 5: /* DMB */
1720         switch (crm & 3) {
1721         case 1: /* MBReqTypes_Reads */
1722             bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1723             break;
1724         case 2: /* MBReqTypes_Writes */
1725             bar = TCG_BAR_SC | TCG_MO_ST_ST;
1726             break;
1727         default: /* MBReqTypes_All */
1728             bar = TCG_BAR_SC | TCG_MO_ALL;
1729             break;
1730         }
1731         tcg_gen_mb(bar);
1732         return;
1733     case 6: /* ISB */
1734         /* We need to break the TB after this insn to execute
1735          * a self-modified code correctly and also to take
1736          * any pending interrupts immediately.
1737          */
1738         reset_btype(s);
1739         gen_goto_tb(s, 0, 4);
1740         return;
1741 
1742     case 7: /* SB */
1743         if (crm != 0 || !dc_isar_feature(aa64_sb, s)) {
1744             goto do_unallocated;
1745         }
1746         /*
1747          * TODO: There is no speculation barrier opcode for TCG;
1748          * MB and end the TB instead.
1749          */
1750         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1751         gen_goto_tb(s, 0, 4);
1752         return;
1753 
1754     default:
1755     do_unallocated:
1756         unallocated_encoding(s);
1757         return;
1758     }
1759 }
1760 
1761 static void gen_xaflag(void)
1762 {
1763     TCGv_i32 z = tcg_temp_new_i32();
1764 
1765     tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1766 
1767     /*
1768      * (!C & !Z) << 31
1769      * (!(C | Z)) << 31
1770      * ~((C | Z) << 31)
1771      * ~-(C | Z)
1772      * (C | Z) - 1
1773      */
1774     tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1775     tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1776 
1777     /* !(Z & C) */
1778     tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1779     tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1780 
1781     /* (!C & Z) << 31 -> -(Z & ~C) */
1782     tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1783     tcg_gen_neg_i32(cpu_VF, cpu_VF);
1784 
1785     /* C | Z */
1786     tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1787 }
1788 
1789 static void gen_axflag(void)
1790 {
1791     tcg_gen_sari_i32(cpu_VF, cpu_VF, 31);         /* V ? -1 : 0 */
1792     tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF);     /* C & !V */
1793 
1794     /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1795     tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1796 
1797     tcg_gen_movi_i32(cpu_NF, 0);
1798     tcg_gen_movi_i32(cpu_VF, 0);
1799 }
1800 
1801 /* MSR (immediate) - move immediate to processor state field */
1802 static void handle_msr_i(DisasContext *s, uint32_t insn,
1803                          unsigned int op1, unsigned int op2, unsigned int crm)
1804 {
1805     int op = op1 << 3 | op2;
1806 
1807     /* End the TB by default, chaining is ok.  */
1808     s->base.is_jmp = DISAS_TOO_MANY;
1809 
1810     switch (op) {
1811     case 0x00: /* CFINV */
1812         if (crm != 0 || !dc_isar_feature(aa64_condm_4, s)) {
1813             goto do_unallocated;
1814         }
1815         tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1816         s->base.is_jmp = DISAS_NEXT;
1817         break;
1818 
1819     case 0x01: /* XAFlag */
1820         if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1821             goto do_unallocated;
1822         }
1823         gen_xaflag();
1824         s->base.is_jmp = DISAS_NEXT;
1825         break;
1826 
1827     case 0x02: /* AXFlag */
1828         if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1829             goto do_unallocated;
1830         }
1831         gen_axflag();
1832         s->base.is_jmp = DISAS_NEXT;
1833         break;
1834 
1835     case 0x03: /* UAO */
1836         if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1837             goto do_unallocated;
1838         }
1839         if (crm & 1) {
1840             set_pstate_bits(PSTATE_UAO);
1841         } else {
1842             clear_pstate_bits(PSTATE_UAO);
1843         }
1844         gen_rebuild_hflags(s);
1845         break;
1846 
1847     case 0x04: /* PAN */
1848         if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1849             goto do_unallocated;
1850         }
1851         if (crm & 1) {
1852             set_pstate_bits(PSTATE_PAN);
1853         } else {
1854             clear_pstate_bits(PSTATE_PAN);
1855         }
1856         gen_rebuild_hflags(s);
1857         break;
1858 
1859     case 0x05: /* SPSel */
1860         if (s->current_el == 0) {
1861             goto do_unallocated;
1862         }
1863         gen_helper_msr_i_spsel(cpu_env, tcg_constant_i32(crm & PSTATE_SP));
1864         break;
1865 
1866     case 0x19: /* SSBS */
1867         if (!dc_isar_feature(aa64_ssbs, s)) {
1868             goto do_unallocated;
1869         }
1870         if (crm & 1) {
1871             set_pstate_bits(PSTATE_SSBS);
1872         } else {
1873             clear_pstate_bits(PSTATE_SSBS);
1874         }
1875         /* Don't need to rebuild hflags since SSBS is a nop */
1876         break;
1877 
1878     case 0x1a: /* DIT */
1879         if (!dc_isar_feature(aa64_dit, s)) {
1880             goto do_unallocated;
1881         }
1882         if (crm & 1) {
1883             set_pstate_bits(PSTATE_DIT);
1884         } else {
1885             clear_pstate_bits(PSTATE_DIT);
1886         }
1887         /* There's no need to rebuild hflags because DIT is a nop */
1888         break;
1889 
1890     case 0x1e: /* DAIFSet */
1891         gen_helper_msr_i_daifset(cpu_env, tcg_constant_i32(crm));
1892         break;
1893 
1894     case 0x1f: /* DAIFClear */
1895         gen_helper_msr_i_daifclear(cpu_env, tcg_constant_i32(crm));
1896         /* For DAIFClear, exit the cpu loop to re-evaluate pending IRQs.  */
1897         s->base.is_jmp = DISAS_UPDATE_EXIT;
1898         break;
1899 
1900     case 0x1c: /* TCO */
1901         if (dc_isar_feature(aa64_mte, s)) {
1902             /* Full MTE is enabled -- set the TCO bit as directed. */
1903             if (crm & 1) {
1904                 set_pstate_bits(PSTATE_TCO);
1905             } else {
1906                 clear_pstate_bits(PSTATE_TCO);
1907             }
1908             gen_rebuild_hflags(s);
1909             /* Many factors, including TCO, go into MTE_ACTIVE. */
1910             s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
1911         } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
1912             /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI.  */
1913             s->base.is_jmp = DISAS_NEXT;
1914         } else {
1915             goto do_unallocated;
1916         }
1917         break;
1918 
1919     case 0x1b: /* SVCR* */
1920         if (!dc_isar_feature(aa64_sme, s) || crm < 2 || crm > 7) {
1921             goto do_unallocated;
1922         }
1923         if (sme_access_check(s)) {
1924             int old = s->pstate_sm | (s->pstate_za << 1);
1925             int new = (crm & 1) * 3;
1926             int msk = (crm >> 1) & 3;
1927 
1928             if ((old ^ new) & msk) {
1929                 /* At least one bit changes. */
1930                 gen_helper_set_svcr(cpu_env, tcg_constant_i32(new),
1931                                     tcg_constant_i32(msk));
1932             } else {
1933                 s->base.is_jmp = DISAS_NEXT;
1934             }
1935         }
1936         break;
1937 
1938     default:
1939     do_unallocated:
1940         unallocated_encoding(s);
1941         return;
1942     }
1943 }
1944 
1945 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1946 {
1947     TCGv_i32 tmp = tcg_temp_new_i32();
1948     TCGv_i32 nzcv = tcg_temp_new_i32();
1949 
1950     /* build bit 31, N */
1951     tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
1952     /* build bit 30, Z */
1953     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1954     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1955     /* build bit 29, C */
1956     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1957     /* build bit 28, V */
1958     tcg_gen_shri_i32(tmp, cpu_VF, 31);
1959     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1960     /* generate result */
1961     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1962 }
1963 
1964 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1965 {
1966     TCGv_i32 nzcv = tcg_temp_new_i32();
1967 
1968     /* take NZCV from R[t] */
1969     tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
1970 
1971     /* bit 31, N */
1972     tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
1973     /* bit 30, Z */
1974     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1975     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1976     /* bit 29, C */
1977     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1978     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1979     /* bit 28, V */
1980     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1981     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1982 }
1983 
1984 static void gen_sysreg_undef(DisasContext *s, bool isread,
1985                              uint8_t op0, uint8_t op1, uint8_t op2,
1986                              uint8_t crn, uint8_t crm, uint8_t rt)
1987 {
1988     /*
1989      * Generate code to emit an UNDEF with correct syndrome
1990      * information for a failed system register access.
1991      * This is EC_UNCATEGORIZED (ie a standard UNDEF) in most cases,
1992      * but if FEAT_IDST is implemented then read accesses to registers
1993      * in the feature ID space are reported with the EC_SYSTEMREGISTERTRAP
1994      * syndrome.
1995      */
1996     uint32_t syndrome;
1997 
1998     if (isread && dc_isar_feature(aa64_ids, s) &&
1999         arm_cpreg_encoding_in_idspace(op0, op1, op2, crn, crm)) {
2000         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2001     } else {
2002         syndrome = syn_uncategorized();
2003     }
2004     gen_exception_insn(s, 0, EXCP_UDEF, syndrome);
2005 }
2006 
2007 /* MRS - move from system register
2008  * MSR (register) - move to system register
2009  * SYS
2010  * SYSL
2011  * These are all essentially the same insn in 'read' and 'write'
2012  * versions, with varying op0 fields.
2013  */
2014 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
2015                        unsigned int op0, unsigned int op1, unsigned int op2,
2016                        unsigned int crn, unsigned int crm, unsigned int rt)
2017 {
2018     uint32_t key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
2019                                       crn, crm, op0, op1, op2);
2020     const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
2021     bool need_exit_tb = false;
2022     TCGv_ptr tcg_ri = NULL;
2023     TCGv_i64 tcg_rt;
2024 
2025     if (!ri) {
2026         /* Unknown register; this might be a guest error or a QEMU
2027          * unimplemented feature.
2028          */
2029         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
2030                       "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
2031                       isread ? "read" : "write", op0, op1, crn, crm, op2);
2032         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2033         return;
2034     }
2035 
2036     /* Check access permissions */
2037     if (!cp_access_ok(s->current_el, ri, isread)) {
2038         gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
2039         return;
2040     }
2041 
2042     if (ri->accessfn || (ri->fgt && s->fgt_active)) {
2043         /* Emit code to perform further access permissions checks at
2044          * runtime; this may result in an exception.
2045          */
2046         uint32_t syndrome;
2047 
2048         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
2049         gen_a64_update_pc(s, 0);
2050         tcg_ri = tcg_temp_new_ptr();
2051         gen_helper_access_check_cp_reg(tcg_ri, cpu_env,
2052                                        tcg_constant_i32(key),
2053                                        tcg_constant_i32(syndrome),
2054                                        tcg_constant_i32(isread));
2055     } else if (ri->type & ARM_CP_RAISES_EXC) {
2056         /*
2057          * The readfn or writefn might raise an exception;
2058          * synchronize the CPU state in case it does.
2059          */
2060         gen_a64_update_pc(s, 0);
2061     }
2062 
2063     /* Handle special cases first */
2064     switch (ri->type & ARM_CP_SPECIAL_MASK) {
2065     case 0:
2066         break;
2067     case ARM_CP_NOP:
2068         return;
2069     case ARM_CP_NZCV:
2070         tcg_rt = cpu_reg(s, rt);
2071         if (isread) {
2072             gen_get_nzcv(tcg_rt);
2073         } else {
2074             gen_set_nzcv(tcg_rt);
2075         }
2076         return;
2077     case ARM_CP_CURRENTEL:
2078         /* Reads as current EL value from pstate, which is
2079          * guaranteed to be constant by the tb flags.
2080          */
2081         tcg_rt = cpu_reg(s, rt);
2082         tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
2083         return;
2084     case ARM_CP_DC_ZVA:
2085         /* Writes clear the aligned block of memory which rt points into. */
2086         if (s->mte_active[0]) {
2087             int desc = 0;
2088 
2089             desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
2090             desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
2091             desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
2092 
2093             tcg_rt = tcg_temp_new_i64();
2094             gen_helper_mte_check_zva(tcg_rt, cpu_env,
2095                                      tcg_constant_i32(desc), cpu_reg(s, rt));
2096         } else {
2097             tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
2098         }
2099         gen_helper_dc_zva(cpu_env, tcg_rt);
2100         return;
2101     case ARM_CP_DC_GVA:
2102         {
2103             TCGv_i64 clean_addr, tag;
2104 
2105             /*
2106              * DC_GVA, like DC_ZVA, requires that we supply the original
2107              * pointer for an invalid page.  Probe that address first.
2108              */
2109             tcg_rt = cpu_reg(s, rt);
2110             clean_addr = clean_data_tbi(s, tcg_rt);
2111             gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
2112 
2113             if (s->ata) {
2114                 /* Extract the tag from the register to match STZGM.  */
2115                 tag = tcg_temp_new_i64();
2116                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2117                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2118             }
2119         }
2120         return;
2121     case ARM_CP_DC_GZVA:
2122         {
2123             TCGv_i64 clean_addr, tag;
2124 
2125             /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
2126             tcg_rt = cpu_reg(s, rt);
2127             clean_addr = clean_data_tbi(s, tcg_rt);
2128             gen_helper_dc_zva(cpu_env, clean_addr);
2129 
2130             if (s->ata) {
2131                 /* Extract the tag from the register to match STZGM.  */
2132                 tag = tcg_temp_new_i64();
2133                 tcg_gen_shri_i64(tag, tcg_rt, 56);
2134                 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2135             }
2136         }
2137         return;
2138     default:
2139         g_assert_not_reached();
2140     }
2141     if ((ri->type & ARM_CP_FPU) && !fp_access_check_only(s)) {
2142         return;
2143     } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
2144         return;
2145     } else if ((ri->type & ARM_CP_SME) && !sme_access_check(s)) {
2146         return;
2147     }
2148 
2149     if (ri->type & ARM_CP_IO) {
2150         /* I/O operations must end the TB here (whether read or write) */
2151         need_exit_tb = translator_io_start(&s->base);
2152     }
2153 
2154     tcg_rt = cpu_reg(s, rt);
2155 
2156     if (isread) {
2157         if (ri->type & ARM_CP_CONST) {
2158             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
2159         } else if (ri->readfn) {
2160             if (!tcg_ri) {
2161                 tcg_ri = gen_lookup_cp_reg(key);
2162             }
2163             gen_helper_get_cp_reg64(tcg_rt, cpu_env, tcg_ri);
2164         } else {
2165             tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
2166         }
2167     } else {
2168         if (ri->type & ARM_CP_CONST) {
2169             /* If not forbidden by access permissions, treat as WI */
2170             return;
2171         } else if (ri->writefn) {
2172             if (!tcg_ri) {
2173                 tcg_ri = gen_lookup_cp_reg(key);
2174             }
2175             gen_helper_set_cp_reg64(cpu_env, tcg_ri, tcg_rt);
2176         } else {
2177             tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
2178         }
2179     }
2180 
2181     if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
2182         /*
2183          * A write to any coprocessor regiser that ends a TB
2184          * must rebuild the hflags for the next TB.
2185          */
2186         gen_rebuild_hflags(s);
2187         /*
2188          * We default to ending the TB on a coprocessor register write,
2189          * but allow this to be suppressed by the register definition
2190          * (usually only necessary to work around guest bugs).
2191          */
2192         need_exit_tb = true;
2193     }
2194     if (need_exit_tb) {
2195         s->base.is_jmp = DISAS_UPDATE_EXIT;
2196     }
2197 }
2198 
2199 /* System
2200  *  31                 22 21  20 19 18 16 15   12 11    8 7   5 4    0
2201  * +---------------------+---+-----+-----+-------+-------+-----+------+
2202  * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 |  CRn  |  CRm  | op2 |  Rt  |
2203  * +---------------------+---+-----+-----+-------+-------+-----+------+
2204  */
2205 static void disas_system(DisasContext *s, uint32_t insn)
2206 {
2207     unsigned int l, op0, op1, crn, crm, op2, rt;
2208     l = extract32(insn, 21, 1);
2209     op0 = extract32(insn, 19, 2);
2210     op1 = extract32(insn, 16, 3);
2211     crn = extract32(insn, 12, 4);
2212     crm = extract32(insn, 8, 4);
2213     op2 = extract32(insn, 5, 3);
2214     rt = extract32(insn, 0, 5);
2215 
2216     if (op0 == 0) {
2217         if (l || rt != 31) {
2218             unallocated_encoding(s);
2219             return;
2220         }
2221         switch (crn) {
2222         case 2: /* HINT (including allocated hints like NOP, YIELD, etc) */
2223             handle_hint(s, insn, op1, op2, crm);
2224             break;
2225         case 3: /* CLREX, DSB, DMB, ISB */
2226             handle_sync(s, insn, op1, op2, crm);
2227             break;
2228         case 4: /* MSR (immediate) */
2229             handle_msr_i(s, insn, op1, op2, crm);
2230             break;
2231         default:
2232             unallocated_encoding(s);
2233             break;
2234         }
2235         return;
2236     }
2237     handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
2238 }
2239 
2240 /* Exception generation
2241  *
2242  *  31             24 23 21 20                     5 4   2 1  0
2243  * +-----------------+-----+------------------------+-----+----+
2244  * | 1 1 0 1 0 1 0 0 | opc |          imm16         | op2 | LL |
2245  * +-----------------------+------------------------+----------+
2246  */
2247 static void disas_exc(DisasContext *s, uint32_t insn)
2248 {
2249     int opc = extract32(insn, 21, 3);
2250     int op2_ll = extract32(insn, 0, 5);
2251     int imm16 = extract32(insn, 5, 16);
2252     uint32_t syndrome;
2253 
2254     switch (opc) {
2255     case 0:
2256         /* For SVC, HVC and SMC we advance the single-step state
2257          * machine before taking the exception. This is architecturally
2258          * mandated, to ensure that single-stepping a system call
2259          * instruction works properly.
2260          */
2261         switch (op2_ll) {
2262         case 1:                                                     /* SVC */
2263             syndrome = syn_aa64_svc(imm16);
2264             if (s->fgt_svc) {
2265                 gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2266                 break;
2267             }
2268             gen_ss_advance(s);
2269             gen_exception_insn(s, 4, EXCP_SWI, syndrome);
2270             break;
2271         case 2:                                                     /* HVC */
2272             if (s->current_el == 0) {
2273                 unallocated_encoding(s);
2274                 break;
2275             }
2276             /* The pre HVC helper handles cases when HVC gets trapped
2277              * as an undefined insn by runtime configuration.
2278              */
2279             gen_a64_update_pc(s, 0);
2280             gen_helper_pre_hvc(cpu_env);
2281             gen_ss_advance(s);
2282             gen_exception_insn_el(s, 4, EXCP_HVC, syn_aa64_hvc(imm16), 2);
2283             break;
2284         case 3:                                                     /* SMC */
2285             if (s->current_el == 0) {
2286                 unallocated_encoding(s);
2287                 break;
2288             }
2289             gen_a64_update_pc(s, 0);
2290             gen_helper_pre_smc(cpu_env, tcg_constant_i32(syn_aa64_smc(imm16)));
2291             gen_ss_advance(s);
2292             gen_exception_insn_el(s, 4, EXCP_SMC, syn_aa64_smc(imm16), 3);
2293             break;
2294         default:
2295             unallocated_encoding(s);
2296             break;
2297         }
2298         break;
2299     case 1:
2300         if (op2_ll != 0) {
2301             unallocated_encoding(s);
2302             break;
2303         }
2304         /* BRK */
2305         gen_exception_bkpt_insn(s, syn_aa64_bkpt(imm16));
2306         break;
2307     case 2:
2308         if (op2_ll != 0) {
2309             unallocated_encoding(s);
2310             break;
2311         }
2312         /* HLT. This has two purposes.
2313          * Architecturally, it is an external halting debug instruction.
2314          * Since QEMU doesn't implement external debug, we treat this as
2315          * it is required for halting debug disabled: it will UNDEF.
2316          * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2317          */
2318         if (semihosting_enabled(s->current_el == 0) && imm16 == 0xf000) {
2319             gen_exception_internal_insn(s, EXCP_SEMIHOST);
2320         } else {
2321             unallocated_encoding(s);
2322         }
2323         break;
2324     case 5:
2325         if (op2_ll < 1 || op2_ll > 3) {
2326             unallocated_encoding(s);
2327             break;
2328         }
2329         /* DCPS1, DCPS2, DCPS3 */
2330         unallocated_encoding(s);
2331         break;
2332     default:
2333         unallocated_encoding(s);
2334         break;
2335     }
2336 }
2337 
2338 /* Branches, exception generating and system instructions */
2339 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
2340 {
2341     switch (extract32(insn, 25, 7)) {
2342     case 0x6a: /* Exception generation / System */
2343         if (insn & (1 << 24)) {
2344             if (extract32(insn, 22, 2) == 0) {
2345                 disas_system(s, insn);
2346             } else {
2347                 unallocated_encoding(s);
2348             }
2349         } else {
2350             disas_exc(s, insn);
2351         }
2352         break;
2353     default:
2354         unallocated_encoding(s);
2355         break;
2356     }
2357 }
2358 
2359 /*
2360  * Load/Store exclusive instructions are implemented by remembering
2361  * the value/address loaded, and seeing if these are the same
2362  * when the store is performed. This is not actually the architecturally
2363  * mandated semantics, but it works for typical guest code sequences
2364  * and avoids having to monitor regular stores.
2365  *
2366  * The store exclusive uses the atomic cmpxchg primitives to avoid
2367  * races in multi-threaded linux-user and when MTTCG softmmu is
2368  * enabled.
2369  */
2370 static void gen_load_exclusive(DisasContext *s, int rt, int rt2, int rn,
2371                                int size, bool is_pair)
2372 {
2373     int idx = get_mem_index(s);
2374     MemOp memop;
2375     TCGv_i64 dirty_addr, clean_addr;
2376 
2377     s->is_ldex = true;
2378     dirty_addr = cpu_reg_sp(s, rn);
2379     clean_addr = gen_mte_check1(s, dirty_addr, false, rn != 31, size);
2380 
2381     g_assert(size <= 3);
2382     if (is_pair) {
2383         g_assert(size >= 2);
2384         if (size == 2) {
2385             /* The pair must be single-copy atomic for the doubleword.  */
2386             memop = finalize_memop(s, MO_64 | MO_ALIGN);
2387             tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2388             if (s->be_data == MO_LE) {
2389                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2390                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2391             } else {
2392                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2393                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2394             }
2395         } else {
2396             /*
2397              * The pair must be single-copy atomic for *each* doubleword, not
2398              * the entire quadword, however it must be quadword aligned.
2399              * Expose the complete load to tcg, for ease of tlb lookup,
2400              * but indicate that only 8-byte atomicity is required.
2401              */
2402             TCGv_i128 t16 = tcg_temp_new_i128();
2403 
2404             memop = finalize_memop_atom(s, MO_128 | MO_ALIGN_16,
2405                                         MO_ATOM_IFALIGN_PAIR);
2406             tcg_gen_qemu_ld_i128(t16, clean_addr, idx, memop);
2407 
2408             if (s->be_data == MO_LE) {
2409                 tcg_gen_extr_i128_i64(cpu_exclusive_val,
2410                                       cpu_exclusive_high, t16);
2411             } else {
2412                 tcg_gen_extr_i128_i64(cpu_exclusive_high,
2413                                       cpu_exclusive_val, t16);
2414             }
2415             tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2416             tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2417         }
2418     } else {
2419         memop = finalize_memop(s, size | MO_ALIGN);
2420         tcg_gen_qemu_ld_i64(cpu_exclusive_val, clean_addr, idx, memop);
2421         tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2422     }
2423     tcg_gen_mov_i64(cpu_exclusive_addr, clean_addr);
2424 }
2425 
2426 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2427                                 int rn, int size, int is_pair)
2428 {
2429     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2430      *     && (!is_pair || env->exclusive_high == [addr + datasize])) {
2431      *     [addr] = {Rt};
2432      *     if (is_pair) {
2433      *         [addr + datasize] = {Rt2};
2434      *     }
2435      *     {Rd} = 0;
2436      * } else {
2437      *     {Rd} = 1;
2438      * }
2439      * env->exclusive_addr = -1;
2440      */
2441     TCGLabel *fail_label = gen_new_label();
2442     TCGLabel *done_label = gen_new_label();
2443     TCGv_i64 tmp, dirty_addr, clean_addr;
2444 
2445     dirty_addr = cpu_reg_sp(s, rn);
2446     clean_addr = gen_mte_check1(s, dirty_addr, true, rn != 31, size);
2447 
2448     tcg_gen_brcond_i64(TCG_COND_NE, clean_addr, cpu_exclusive_addr, fail_label);
2449 
2450     tmp = tcg_temp_new_i64();
2451     if (is_pair) {
2452         if (size == 2) {
2453             if (s->be_data == MO_LE) {
2454                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2455             } else {
2456                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2457             }
2458             tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2459                                        cpu_exclusive_val, tmp,
2460                                        get_mem_index(s),
2461                                        MO_64 | MO_ALIGN | s->be_data);
2462             tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2463         } else {
2464             TCGv_i128 t16 = tcg_temp_new_i128();
2465             TCGv_i128 c16 = tcg_temp_new_i128();
2466             TCGv_i64 a, b;
2467 
2468             if (s->be_data == MO_LE) {
2469                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt), cpu_reg(s, rt2));
2470                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_val,
2471                                         cpu_exclusive_high);
2472             } else {
2473                 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt2), cpu_reg(s, rt));
2474                 tcg_gen_concat_i64_i128(c16, cpu_exclusive_high,
2475                                         cpu_exclusive_val);
2476             }
2477 
2478             tcg_gen_atomic_cmpxchg_i128(t16, cpu_exclusive_addr, c16, t16,
2479                                         get_mem_index(s),
2480                                         MO_128 | MO_ALIGN | s->be_data);
2481 
2482             a = tcg_temp_new_i64();
2483             b = tcg_temp_new_i64();
2484             if (s->be_data == MO_LE) {
2485                 tcg_gen_extr_i128_i64(a, b, t16);
2486             } else {
2487                 tcg_gen_extr_i128_i64(b, a, t16);
2488             }
2489 
2490             tcg_gen_xor_i64(a, a, cpu_exclusive_val);
2491             tcg_gen_xor_i64(b, b, cpu_exclusive_high);
2492             tcg_gen_or_i64(tmp, a, b);
2493 
2494             tcg_gen_setcondi_i64(TCG_COND_NE, tmp, tmp, 0);
2495         }
2496     } else {
2497         tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2498                                    cpu_reg(s, rt), get_mem_index(s),
2499                                    size | MO_ALIGN | s->be_data);
2500         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2501     }
2502     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2503     tcg_gen_br(done_label);
2504 
2505     gen_set_label(fail_label);
2506     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2507     gen_set_label(done_label);
2508     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2509 }
2510 
2511 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2512                                  int rn, int size)
2513 {
2514     TCGv_i64 tcg_rs = cpu_reg(s, rs);
2515     TCGv_i64 tcg_rt = cpu_reg(s, rt);
2516     int memidx = get_mem_index(s);
2517     TCGv_i64 clean_addr;
2518 
2519     if (rn == 31) {
2520         gen_check_sp_alignment(s);
2521     }
2522     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, size);
2523     tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt, memidx,
2524                                size | MO_ALIGN | s->be_data);
2525 }
2526 
2527 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2528                                       int rn, int size)
2529 {
2530     TCGv_i64 s1 = cpu_reg(s, rs);
2531     TCGv_i64 s2 = cpu_reg(s, rs + 1);
2532     TCGv_i64 t1 = cpu_reg(s, rt);
2533     TCGv_i64 t2 = cpu_reg(s, rt + 1);
2534     TCGv_i64 clean_addr;
2535     int memidx = get_mem_index(s);
2536 
2537     if (rn == 31) {
2538         gen_check_sp_alignment(s);
2539     }
2540 
2541     /* This is a single atomic access, despite the "pair". */
2542     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, size + 1);
2543 
2544     if (size == 2) {
2545         TCGv_i64 cmp = tcg_temp_new_i64();
2546         TCGv_i64 val = tcg_temp_new_i64();
2547 
2548         if (s->be_data == MO_LE) {
2549             tcg_gen_concat32_i64(val, t1, t2);
2550             tcg_gen_concat32_i64(cmp, s1, s2);
2551         } else {
2552             tcg_gen_concat32_i64(val, t2, t1);
2553             tcg_gen_concat32_i64(cmp, s2, s1);
2554         }
2555 
2556         tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx,
2557                                    MO_64 | MO_ALIGN | s->be_data);
2558 
2559         if (s->be_data == MO_LE) {
2560             tcg_gen_extr32_i64(s1, s2, cmp);
2561         } else {
2562             tcg_gen_extr32_i64(s2, s1, cmp);
2563         }
2564     } else {
2565         TCGv_i128 cmp = tcg_temp_new_i128();
2566         TCGv_i128 val = tcg_temp_new_i128();
2567 
2568         if (s->be_data == MO_LE) {
2569             tcg_gen_concat_i64_i128(val, t1, t2);
2570             tcg_gen_concat_i64_i128(cmp, s1, s2);
2571         } else {
2572             tcg_gen_concat_i64_i128(val, t2, t1);
2573             tcg_gen_concat_i64_i128(cmp, s2, s1);
2574         }
2575 
2576         tcg_gen_atomic_cmpxchg_i128(cmp, clean_addr, cmp, val, memidx,
2577                                     MO_128 | MO_ALIGN | s->be_data);
2578 
2579         if (s->be_data == MO_LE) {
2580             tcg_gen_extr_i128_i64(s1, s2, cmp);
2581         } else {
2582             tcg_gen_extr_i128_i64(s2, s1, cmp);
2583         }
2584     }
2585 }
2586 
2587 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
2588  * from the ARMv8 specs for LDR (Shared decode for all encodings).
2589  */
2590 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2591 {
2592     int opc0 = extract32(opc, 0, 1);
2593     int regsize;
2594 
2595     if (is_signed) {
2596         regsize = opc0 ? 32 : 64;
2597     } else {
2598         regsize = size == 3 ? 64 : 32;
2599     }
2600     return regsize == 64;
2601 }
2602 
2603 /* Load/store exclusive
2604  *
2605  *  31 30 29         24  23  22   21  20  16  15  14   10 9    5 4    0
2606  * +-----+-------------+----+---+----+------+----+-------+------+------+
2607  * | sz  | 0 0 1 0 0 0 | o2 | L | o1 |  Rs  | o0 |  Rt2  |  Rn  | Rt   |
2608  * +-----+-------------+----+---+----+------+----+-------+------+------+
2609  *
2610  *  sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2611  *   L: 0 -> store, 1 -> load
2612  *  o2: 0 -> exclusive, 1 -> not
2613  *  o1: 0 -> single register, 1 -> register pair
2614  *  o0: 1 -> load-acquire/store-release, 0 -> not
2615  */
2616 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2617 {
2618     int rt = extract32(insn, 0, 5);
2619     int rn = extract32(insn, 5, 5);
2620     int rt2 = extract32(insn, 10, 5);
2621     int rs = extract32(insn, 16, 5);
2622     int is_lasr = extract32(insn, 15, 1);
2623     int o2_L_o1_o0 = extract32(insn, 21, 3) * 2 | is_lasr;
2624     int size = extract32(insn, 30, 2);
2625     TCGv_i64 clean_addr;
2626     MemOp memop;
2627 
2628     switch (o2_L_o1_o0) {
2629     case 0x0: /* STXR */
2630     case 0x1: /* STLXR */
2631         if (rn == 31) {
2632             gen_check_sp_alignment(s);
2633         }
2634         if (is_lasr) {
2635             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2636         }
2637         gen_store_exclusive(s, rs, rt, rt2, rn, size, false);
2638         return;
2639 
2640     case 0x4: /* LDXR */
2641     case 0x5: /* LDAXR */
2642         if (rn == 31) {
2643             gen_check_sp_alignment(s);
2644         }
2645         gen_load_exclusive(s, rt, rt2, rn, size, false);
2646         if (is_lasr) {
2647             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2648         }
2649         return;
2650 
2651     case 0x8: /* STLLR */
2652         if (!dc_isar_feature(aa64_lor, s)) {
2653             break;
2654         }
2655         /* StoreLORelease is the same as Store-Release for QEMU.  */
2656         /* fall through */
2657     case 0x9: /* STLR */
2658         /* Generate ISS for non-exclusive accesses including LASR.  */
2659         if (rn == 31) {
2660             gen_check_sp_alignment(s);
2661         }
2662         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2663         /* TODO: ARMv8.4-LSE SCTLR.nAA */
2664         memop = finalize_memop(s, size | MO_ALIGN);
2665         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2666                                     true, rn != 31, size);
2667         do_gpr_st(s, cpu_reg(s, rt), clean_addr, memop, 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         /* TODO: ARMv8.4-LSE SCTLR.nAA */
2683         memop = finalize_memop(s, size | MO_ALIGN);
2684         clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2685                                     false, rn != 31, size);
2686         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, memop, false, true,
2687                   rt, disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2688         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2689         return;
2690 
2691     case 0x2: case 0x3: /* CASP / STXP */
2692         if (size & 2) { /* STXP / STLXP */
2693             if (rn == 31) {
2694                 gen_check_sp_alignment(s);
2695             }
2696             if (is_lasr) {
2697                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2698             }
2699             gen_store_exclusive(s, rs, rt, rt2, rn, size, true);
2700             return;
2701         }
2702         if (rt2 == 31
2703             && ((rt | rs) & 1) == 0
2704             && dc_isar_feature(aa64_atomics, s)) {
2705             /* CASP / CASPL */
2706             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2707             return;
2708         }
2709         break;
2710 
2711     case 0x6: case 0x7: /* CASPA / LDXP */
2712         if (size & 2) { /* LDXP / LDAXP */
2713             if (rn == 31) {
2714                 gen_check_sp_alignment(s);
2715             }
2716             gen_load_exclusive(s, rt, rt2, rn, size, true);
2717             if (is_lasr) {
2718                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2719             }
2720             return;
2721         }
2722         if (rt2 == 31
2723             && ((rt | rs) & 1) == 0
2724             && dc_isar_feature(aa64_atomics, s)) {
2725             /* CASPA / CASPAL */
2726             gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2727             return;
2728         }
2729         break;
2730 
2731     case 0xa: /* CAS */
2732     case 0xb: /* CASL */
2733     case 0xe: /* CASA */
2734     case 0xf: /* CASAL */
2735         if (rt2 == 31 && dc_isar_feature(aa64_atomics, s)) {
2736             gen_compare_and_swap(s, rs, rt, rn, size);
2737             return;
2738         }
2739         break;
2740     }
2741     unallocated_encoding(s);
2742 }
2743 
2744 /*
2745  * Load register (literal)
2746  *
2747  *  31 30 29   27  26 25 24 23                5 4     0
2748  * +-----+-------+---+-----+-------------------+-------+
2749  * | opc | 0 1 1 | V | 0 0 |     imm19         |  Rt   |
2750  * +-----+-------+---+-----+-------------------+-------+
2751  *
2752  * V: 1 -> vector (simd/fp)
2753  * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2754  *                   10-> 32 bit signed, 11 -> prefetch
2755  * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2756  */
2757 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2758 {
2759     int rt = extract32(insn, 0, 5);
2760     int64_t imm = sextract32(insn, 5, 19) << 2;
2761     bool is_vector = extract32(insn, 26, 1);
2762     int opc = extract32(insn, 30, 2);
2763     bool is_signed = false;
2764     int size = 2;
2765     TCGv_i64 tcg_rt, clean_addr;
2766 
2767     if (is_vector) {
2768         if (opc == 3) {
2769             unallocated_encoding(s);
2770             return;
2771         }
2772         size = 2 + opc;
2773         if (!fp_access_check(s)) {
2774             return;
2775         }
2776     } else {
2777         if (opc == 3) {
2778             /* PRFM (literal) : prefetch */
2779             return;
2780         }
2781         size = 2 + extract32(opc, 0, 1);
2782         is_signed = extract32(opc, 1, 1);
2783     }
2784 
2785     tcg_rt = cpu_reg(s, rt);
2786 
2787     clean_addr = tcg_temp_new_i64();
2788     gen_pc_plus_diff(s, clean_addr, imm);
2789     if (is_vector) {
2790         do_fp_ld(s, rt, clean_addr, size);
2791     } else {
2792         /* Only unsigned 32bit loads target 32bit registers.  */
2793         bool iss_sf = opc != 0;
2794         MemOp memop = finalize_memop(s, size + is_signed * MO_SIGN);
2795 
2796         do_gpr_ld(s, tcg_rt, clean_addr, memop, false, true, rt, iss_sf, false);
2797     }
2798 }
2799 
2800 /*
2801  * LDNP (Load Pair - non-temporal hint)
2802  * LDP (Load Pair - non vector)
2803  * LDPSW (Load Pair Signed Word - non vector)
2804  * STNP (Store Pair - non-temporal hint)
2805  * STP (Store Pair - non vector)
2806  * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2807  * LDP (Load Pair of SIMD&FP)
2808  * STNP (Store Pair of SIMD&FP - non-temporal hint)
2809  * STP (Store Pair of SIMD&FP)
2810  *
2811  *  31 30 29   27  26  25 24   23  22 21   15 14   10 9    5 4    0
2812  * +-----+-------+---+---+-------+---+-----------------------------+
2813  * | opc | 1 0 1 | V | 0 | index | L |  imm7 |  Rt2  |  Rn  | Rt   |
2814  * +-----+-------+---+---+-------+---+-------+-------+------+------+
2815  *
2816  * opc: LDP/STP/LDNP/STNP        00 -> 32 bit, 10 -> 64 bit
2817  *      LDPSW/STGP               01
2818  *      LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2819  *   V: 0 -> GPR, 1 -> Vector
2820  * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2821  *      10 -> signed offset, 11 -> pre-index
2822  *   L: 0 -> Store 1 -> Load
2823  *
2824  * Rt, Rt2 = GPR or SIMD registers to be stored
2825  * Rn = general purpose register containing address
2826  * imm7 = signed offset (multiple of 4 or 8 depending on size)
2827  */
2828 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2829 {
2830     int rt = extract32(insn, 0, 5);
2831     int rn = extract32(insn, 5, 5);
2832     int rt2 = extract32(insn, 10, 5);
2833     uint64_t offset = sextract64(insn, 15, 7);
2834     int index = extract32(insn, 23, 2);
2835     bool is_vector = extract32(insn, 26, 1);
2836     bool is_load = extract32(insn, 22, 1);
2837     int opc = extract32(insn, 30, 2);
2838 
2839     bool is_signed = false;
2840     bool postindex = false;
2841     bool wback = false;
2842     bool set_tag = false;
2843 
2844     TCGv_i64 clean_addr, dirty_addr;
2845 
2846     int size;
2847 
2848     if (opc == 3) {
2849         unallocated_encoding(s);
2850         return;
2851     }
2852 
2853     if (is_vector) {
2854         size = 2 + opc;
2855     } else if (opc == 1 && !is_load) {
2856         /* STGP */
2857         if (!dc_isar_feature(aa64_mte_insn_reg, s) || index == 0) {
2858             unallocated_encoding(s);
2859             return;
2860         }
2861         size = 3;
2862         set_tag = true;
2863     } else {
2864         size = 2 + extract32(opc, 1, 1);
2865         is_signed = extract32(opc, 0, 1);
2866         if (!is_load && is_signed) {
2867             unallocated_encoding(s);
2868             return;
2869         }
2870     }
2871 
2872     switch (index) {
2873     case 1: /* post-index */
2874         postindex = true;
2875         wback = true;
2876         break;
2877     case 0:
2878         /* signed offset with "non-temporal" hint. Since we don't emulate
2879          * caches we don't care about hints to the cache system about
2880          * data access patterns, and handle this identically to plain
2881          * signed offset.
2882          */
2883         if (is_signed) {
2884             /* There is no non-temporal-hint version of LDPSW */
2885             unallocated_encoding(s);
2886             return;
2887         }
2888         postindex = false;
2889         break;
2890     case 2: /* signed offset, rn not updated */
2891         postindex = false;
2892         break;
2893     case 3: /* pre-index */
2894         postindex = false;
2895         wback = true;
2896         break;
2897     }
2898 
2899     if (is_vector && !fp_access_check(s)) {
2900         return;
2901     }
2902 
2903     offset <<= (set_tag ? LOG2_TAG_GRANULE : size);
2904 
2905     if (rn == 31) {
2906         gen_check_sp_alignment(s);
2907     }
2908 
2909     dirty_addr = read_cpu_reg_sp(s, rn, 1);
2910     if (!postindex) {
2911         tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2912     }
2913 
2914     if (set_tag) {
2915         if (!s->ata) {
2916             /*
2917              * TODO: We could rely on the stores below, at least for
2918              * system mode, if we arrange to add MO_ALIGN_16.
2919              */
2920             gen_helper_stg_stub(cpu_env, dirty_addr);
2921         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2922             gen_helper_stg_parallel(cpu_env, dirty_addr, dirty_addr);
2923         } else {
2924             gen_helper_stg(cpu_env, dirty_addr, dirty_addr);
2925         }
2926     }
2927 
2928     clean_addr = gen_mte_checkN(s, dirty_addr, !is_load,
2929                                 (wback || rn != 31) && !set_tag, 2 << size);
2930 
2931     if (is_vector) {
2932         if (is_load) {
2933             do_fp_ld(s, rt, clean_addr, size);
2934         } else {
2935             do_fp_st(s, rt, clean_addr, size);
2936         }
2937         tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2938         if (is_load) {
2939             do_fp_ld(s, rt2, clean_addr, size);
2940         } else {
2941             do_fp_st(s, rt2, clean_addr, size);
2942         }
2943     } else {
2944         TCGv_i64 tcg_rt = cpu_reg(s, rt);
2945         TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
2946         MemOp mop = size + 1;
2947 
2948         /*
2949          * With LSE2, non-sign-extending pairs are treated atomically if
2950          * aligned, and if unaligned one of the pair will be completely
2951          * within a 16-byte block and that element will be atomic.
2952          * Otherwise each element is separately atomic.
2953          * In all cases, issue one operation with the correct atomicity.
2954          *
2955          * This treats sign-extending loads like zero-extending loads,
2956          * since that reuses the most code below.
2957          */
2958         if (s->align_mem) {
2959             mop |= (size == 2 ? MO_ALIGN_4 : MO_ALIGN_8);
2960         }
2961         mop = finalize_memop_pair(s, mop);
2962 
2963         if (is_load) {
2964             if (size == 2) {
2965                 int o2 = s->be_data == MO_LE ? 32 : 0;
2966                 int o1 = o2 ^ 32;
2967 
2968                 tcg_gen_qemu_ld_i64(tcg_rt, clean_addr, get_mem_index(s), mop);
2969                 if (is_signed) {
2970                     tcg_gen_sextract_i64(tcg_rt2, tcg_rt, o2, 32);
2971                     tcg_gen_sextract_i64(tcg_rt, tcg_rt, o1, 32);
2972                 } else {
2973                     tcg_gen_extract_i64(tcg_rt2, tcg_rt, o2, 32);
2974                     tcg_gen_extract_i64(tcg_rt, tcg_rt, o1, 32);
2975                 }
2976             } else {
2977                 TCGv_i128 tmp = tcg_temp_new_i128();
2978 
2979                 tcg_gen_qemu_ld_i128(tmp, clean_addr, get_mem_index(s), mop);
2980                 if (s->be_data == MO_LE) {
2981                     tcg_gen_extr_i128_i64(tcg_rt, tcg_rt2, tmp);
2982                 } else {
2983                     tcg_gen_extr_i128_i64(tcg_rt2, tcg_rt, tmp);
2984                 }
2985             }
2986         } else {
2987             if (size == 2) {
2988                 TCGv_i64 tmp = tcg_temp_new_i64();
2989 
2990                 if (s->be_data == MO_LE) {
2991                     tcg_gen_concat32_i64(tmp, tcg_rt, tcg_rt2);
2992                 } else {
2993                     tcg_gen_concat32_i64(tmp, tcg_rt2, tcg_rt);
2994                 }
2995                 tcg_gen_qemu_st_i64(tmp, clean_addr, get_mem_index(s), mop);
2996             } else {
2997                 TCGv_i128 tmp = tcg_temp_new_i128();
2998 
2999                 if (s->be_data == MO_LE) {
3000                     tcg_gen_concat_i64_i128(tmp, tcg_rt, tcg_rt2);
3001                 } else {
3002                     tcg_gen_concat_i64_i128(tmp, tcg_rt2, tcg_rt);
3003                 }
3004                 tcg_gen_qemu_st_i128(tmp, clean_addr, get_mem_index(s), mop);
3005             }
3006         }
3007     }
3008 
3009     if (wback) {
3010         if (postindex) {
3011             tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3012         }
3013         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3014     }
3015 }
3016 
3017 /*
3018  * Load/store (immediate post-indexed)
3019  * Load/store (immediate pre-indexed)
3020  * Load/store (unscaled immediate)
3021  *
3022  * 31 30 29   27  26 25 24 23 22 21  20    12 11 10 9    5 4    0
3023  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3024  * |size| 1 1 1 | V | 0 0 | opc | 0 |  imm9  | idx |  Rn  |  Rt  |
3025  * +----+-------+---+-----+-----+---+--------+-----+------+------+
3026  *
3027  * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
3028          10 -> unprivileged
3029  * V = 0 -> non-vector
3030  * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
3031  * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3032  */
3033 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
3034                                 int opc,
3035                                 int size,
3036                                 int rt,
3037                                 bool is_vector)
3038 {
3039     int rn = extract32(insn, 5, 5);
3040     int imm9 = sextract32(insn, 12, 9);
3041     int idx = extract32(insn, 10, 2);
3042     bool is_signed = false;
3043     bool is_store = false;
3044     bool is_extended = false;
3045     bool is_unpriv = (idx == 2);
3046     bool iss_valid;
3047     bool post_index;
3048     bool writeback;
3049     int memidx;
3050     MemOp memop;
3051     TCGv_i64 clean_addr, dirty_addr;
3052 
3053     if (is_vector) {
3054         size |= (opc & 2) << 1;
3055         if (size > 4 || is_unpriv) {
3056             unallocated_encoding(s);
3057             return;
3058         }
3059         is_store = ((opc & 1) == 0);
3060         if (!fp_access_check(s)) {
3061             return;
3062         }
3063     } else {
3064         if (size == 3 && opc == 2) {
3065             /* PRFM - prefetch */
3066             if (idx != 0) {
3067                 unallocated_encoding(s);
3068                 return;
3069             }
3070             return;
3071         }
3072         if (opc == 3 && size > 1) {
3073             unallocated_encoding(s);
3074             return;
3075         }
3076         is_store = (opc == 0);
3077         is_signed = !is_store && extract32(opc, 1, 1);
3078         is_extended = (size < 3) && extract32(opc, 0, 1);
3079     }
3080 
3081     switch (idx) {
3082     case 0:
3083     case 2:
3084         post_index = false;
3085         writeback = false;
3086         break;
3087     case 1:
3088         post_index = true;
3089         writeback = true;
3090         break;
3091     case 3:
3092         post_index = false;
3093         writeback = true;
3094         break;
3095     default:
3096         g_assert_not_reached();
3097     }
3098 
3099     iss_valid = !is_vector && !writeback;
3100 
3101     if (rn == 31) {
3102         gen_check_sp_alignment(s);
3103     }
3104 
3105     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3106     if (!post_index) {
3107         tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3108     }
3109 
3110     memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
3111     memop = finalize_memop(s, size + is_signed * MO_SIGN);
3112 
3113     clean_addr = gen_mte_check1_mmuidx(s, dirty_addr, is_store,
3114                                        writeback || rn != 31,
3115                                        size, is_unpriv, memidx);
3116 
3117     if (is_vector) {
3118         if (is_store) {
3119             do_fp_st(s, rt, clean_addr, size);
3120         } else {
3121             do_fp_ld(s, rt, clean_addr, size);
3122         }
3123     } else {
3124         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3125         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3126 
3127         if (is_store) {
3128             do_gpr_st_memidx(s, tcg_rt, clean_addr, memop, memidx,
3129                              iss_valid, rt, iss_sf, false);
3130         } else {
3131             do_gpr_ld_memidx(s, tcg_rt, clean_addr, memop,
3132                              is_extended, memidx,
3133                              iss_valid, rt, iss_sf, false);
3134         }
3135     }
3136 
3137     if (writeback) {
3138         TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3139         if (post_index) {
3140             tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3141         }
3142         tcg_gen_mov_i64(tcg_rn, dirty_addr);
3143     }
3144 }
3145 
3146 /*
3147  * Load/store (register offset)
3148  *
3149  * 31 30 29   27  26 25 24 23 22 21  20  16 15 13 12 11 10 9  5 4  0
3150  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3151  * |size| 1 1 1 | V | 0 0 | opc | 1 |  Rm  | opt | S| 1 0 | Rn | Rt |
3152  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3153  *
3154  * For non-vector:
3155  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3156  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3157  * For vector:
3158  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3159  *   opc<0>: 0 -> store, 1 -> load
3160  * V: 1 -> vector/simd
3161  * opt: extend encoding (see DecodeRegExtend)
3162  * S: if S=1 then scale (essentially index by sizeof(size))
3163  * Rt: register to transfer into/out of
3164  * Rn: address register or SP for base
3165  * Rm: offset register or ZR for offset
3166  */
3167 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
3168                                    int opc,
3169                                    int size,
3170                                    int rt,
3171                                    bool is_vector)
3172 {
3173     int rn = extract32(insn, 5, 5);
3174     int shift = extract32(insn, 12, 1);
3175     int rm = extract32(insn, 16, 5);
3176     int opt = extract32(insn, 13, 3);
3177     bool is_signed = false;
3178     bool is_store = false;
3179     bool is_extended = false;
3180     TCGv_i64 tcg_rm, clean_addr, dirty_addr;
3181     MemOp memop;
3182 
3183     if (extract32(opt, 1, 1) == 0) {
3184         unallocated_encoding(s);
3185         return;
3186     }
3187 
3188     if (is_vector) {
3189         size |= (opc & 2) << 1;
3190         if (size > 4) {
3191             unallocated_encoding(s);
3192             return;
3193         }
3194         is_store = !extract32(opc, 0, 1);
3195         if (!fp_access_check(s)) {
3196             return;
3197         }
3198     } else {
3199         if (size == 3 && opc == 2) {
3200             /* PRFM - prefetch */
3201             return;
3202         }
3203         if (opc == 3 && size > 1) {
3204             unallocated_encoding(s);
3205             return;
3206         }
3207         is_store = (opc == 0);
3208         is_signed = !is_store && extract32(opc, 1, 1);
3209         is_extended = (size < 3) && extract32(opc, 0, 1);
3210     }
3211 
3212     if (rn == 31) {
3213         gen_check_sp_alignment(s);
3214     }
3215     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3216 
3217     tcg_rm = read_cpu_reg(s, rm, 1);
3218     ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
3219 
3220     tcg_gen_add_i64(dirty_addr, dirty_addr, tcg_rm);
3221 
3222     memop = finalize_memop(s, size + is_signed * MO_SIGN);
3223     clean_addr = gen_mte_check1(s, dirty_addr, is_store, true, size);
3224 
3225     if (is_vector) {
3226         if (is_store) {
3227             do_fp_st(s, rt, clean_addr, size);
3228         } else {
3229             do_fp_ld(s, rt, clean_addr, size);
3230         }
3231     } else {
3232         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3233         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3234 
3235         if (is_store) {
3236             do_gpr_st(s, tcg_rt, clean_addr, memop,
3237                       true, rt, iss_sf, false);
3238         } else {
3239             do_gpr_ld(s, tcg_rt, clean_addr, memop,
3240                       is_extended, true, rt, iss_sf, false);
3241         }
3242     }
3243 }
3244 
3245 /*
3246  * Load/store (unsigned immediate)
3247  *
3248  * 31 30 29   27  26 25 24 23 22 21        10 9     5
3249  * +----+-------+---+-----+-----+------------+-------+------+
3250  * |size| 1 1 1 | V | 0 1 | opc |   imm12    |  Rn   |  Rt  |
3251  * +----+-------+---+-----+-----+------------+-------+------+
3252  *
3253  * For non-vector:
3254  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3255  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3256  * For vector:
3257  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3258  *   opc<0>: 0 -> store, 1 -> load
3259  * Rn: base address register (inc SP)
3260  * Rt: target register
3261  */
3262 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
3263                                         int opc,
3264                                         int size,
3265                                         int rt,
3266                                         bool is_vector)
3267 {
3268     int rn = extract32(insn, 5, 5);
3269     unsigned int imm12 = extract32(insn, 10, 12);
3270     unsigned int offset;
3271     TCGv_i64 clean_addr, dirty_addr;
3272     bool is_store;
3273     bool is_signed = false;
3274     bool is_extended = false;
3275     MemOp memop;
3276 
3277     if (is_vector) {
3278         size |= (opc & 2) << 1;
3279         if (size > 4) {
3280             unallocated_encoding(s);
3281             return;
3282         }
3283         is_store = !extract32(opc, 0, 1);
3284         if (!fp_access_check(s)) {
3285             return;
3286         }
3287     } else {
3288         if (size == 3 && opc == 2) {
3289             /* PRFM - prefetch */
3290             return;
3291         }
3292         if (opc == 3 && size > 1) {
3293             unallocated_encoding(s);
3294             return;
3295         }
3296         is_store = (opc == 0);
3297         is_signed = !is_store && extract32(opc, 1, 1);
3298         is_extended = (size < 3) && extract32(opc, 0, 1);
3299     }
3300 
3301     if (rn == 31) {
3302         gen_check_sp_alignment(s);
3303     }
3304     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3305     offset = imm12 << size;
3306     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3307 
3308     memop = finalize_memop(s, size + is_signed * MO_SIGN);
3309     clean_addr = gen_mte_check1(s, dirty_addr, is_store, rn != 31, size);
3310 
3311     if (is_vector) {
3312         if (is_store) {
3313             do_fp_st(s, rt, clean_addr, size);
3314         } else {
3315             do_fp_ld(s, rt, clean_addr, size);
3316         }
3317     } else {
3318         TCGv_i64 tcg_rt = cpu_reg(s, rt);
3319         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3320         if (is_store) {
3321             do_gpr_st(s, tcg_rt, clean_addr, memop, true, rt, iss_sf, false);
3322         } else {
3323             do_gpr_ld(s, tcg_rt, clean_addr, memop,
3324                       is_extended, true, rt, iss_sf, false);
3325         }
3326     }
3327 }
3328 
3329 /* Atomic memory operations
3330  *
3331  *  31  30      27  26    24    22  21   16   15    12    10    5     0
3332  * +------+-------+---+-----+-----+---+----+----+-----+-----+----+-----+
3333  * | size | 1 1 1 | V | 0 0 | A R | 1 | Rs | o3 | opc | 0 0 | Rn |  Rt |
3334  * +------+-------+---+-----+-----+--------+----+-----+-----+----+-----+
3335  *
3336  * Rt: the result register
3337  * Rn: base address or SP
3338  * Rs: the source register for the operation
3339  * V: vector flag (always 0 as of v8.3)
3340  * A: acquire flag
3341  * R: release flag
3342  */
3343 static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
3344                               int size, int rt, bool is_vector)
3345 {
3346     int rs = extract32(insn, 16, 5);
3347     int rn = extract32(insn, 5, 5);
3348     int o3_opc = extract32(insn, 12, 4);
3349     bool r = extract32(insn, 22, 1);
3350     bool a = extract32(insn, 23, 1);
3351     TCGv_i64 tcg_rs, tcg_rt, clean_addr;
3352     AtomicThreeOpFn *fn = NULL;
3353     MemOp mop = finalize_memop(s, size | MO_ALIGN);
3354 
3355     if (is_vector || !dc_isar_feature(aa64_atomics, s)) {
3356         unallocated_encoding(s);
3357         return;
3358     }
3359     switch (o3_opc) {
3360     case 000: /* LDADD */
3361         fn = tcg_gen_atomic_fetch_add_i64;
3362         break;
3363     case 001: /* LDCLR */
3364         fn = tcg_gen_atomic_fetch_and_i64;
3365         break;
3366     case 002: /* LDEOR */
3367         fn = tcg_gen_atomic_fetch_xor_i64;
3368         break;
3369     case 003: /* LDSET */
3370         fn = tcg_gen_atomic_fetch_or_i64;
3371         break;
3372     case 004: /* LDSMAX */
3373         fn = tcg_gen_atomic_fetch_smax_i64;
3374         mop |= MO_SIGN;
3375         break;
3376     case 005: /* LDSMIN */
3377         fn = tcg_gen_atomic_fetch_smin_i64;
3378         mop |= MO_SIGN;
3379         break;
3380     case 006: /* LDUMAX */
3381         fn = tcg_gen_atomic_fetch_umax_i64;
3382         break;
3383     case 007: /* LDUMIN */
3384         fn = tcg_gen_atomic_fetch_umin_i64;
3385         break;
3386     case 010: /* SWP */
3387         fn = tcg_gen_atomic_xchg_i64;
3388         break;
3389     case 014: /* LDAPR, LDAPRH, LDAPRB */
3390         if (!dc_isar_feature(aa64_rcpc_8_3, s) ||
3391             rs != 31 || a != 1 || r != 0) {
3392             unallocated_encoding(s);
3393             return;
3394         }
3395         break;
3396     default:
3397         unallocated_encoding(s);
3398         return;
3399     }
3400 
3401     if (rn == 31) {
3402         gen_check_sp_alignment(s);
3403     }
3404     clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), false, rn != 31, size);
3405 
3406     if (o3_opc == 014) {
3407         /*
3408          * LDAPR* are a special case because they are a simple load, not a
3409          * fetch-and-do-something op.
3410          * The architectural consistency requirements here are weaker than
3411          * full load-acquire (we only need "load-acquire processor consistent"),
3412          * but we choose to implement them as full LDAQ.
3413          */
3414         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop, false,
3415                   true, rt, disas_ldst_compute_iss_sf(size, false, 0), true);
3416         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3417         return;
3418     }
3419 
3420     tcg_rs = read_cpu_reg(s, rs, true);
3421     tcg_rt = cpu_reg(s, rt);
3422 
3423     if (o3_opc == 1) { /* LDCLR */
3424         tcg_gen_not_i64(tcg_rs, tcg_rs);
3425     }
3426 
3427     /* The tcg atomic primitives are all full barriers.  Therefore we
3428      * can ignore the Acquire and Release bits of this instruction.
3429      */
3430     fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3431 
3432     if ((mop & MO_SIGN) && size != MO_64) {
3433         tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3434     }
3435 }
3436 
3437 /*
3438  * PAC memory operations
3439  *
3440  *  31  30      27  26    24    22  21       12  11  10    5     0
3441  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3442  * | size | 1 1 1 | V | 0 0 | M S | 1 |  imm9  | W | 1 | Rn |  Rt |
3443  * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3444  *
3445  * Rt: the result register
3446  * Rn: base address or SP
3447  * V: vector flag (always 0 as of v8.3)
3448  * M: clear for key DA, set for key DB
3449  * W: pre-indexing flag
3450  * S: sign for imm9.
3451  */
3452 static void disas_ldst_pac(DisasContext *s, uint32_t insn,
3453                            int size, int rt, bool is_vector)
3454 {
3455     int rn = extract32(insn, 5, 5);
3456     bool is_wback = extract32(insn, 11, 1);
3457     bool use_key_a = !extract32(insn, 23, 1);
3458     int offset;
3459     TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3460     MemOp memop;
3461 
3462     if (size != 3 || is_vector || !dc_isar_feature(aa64_pauth, s)) {
3463         unallocated_encoding(s);
3464         return;
3465     }
3466 
3467     if (rn == 31) {
3468         gen_check_sp_alignment(s);
3469     }
3470     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3471 
3472     if (s->pauth_active) {
3473         if (use_key_a) {
3474             gen_helper_autda(dirty_addr, cpu_env, dirty_addr,
3475                              tcg_constant_i64(0));
3476         } else {
3477             gen_helper_autdb(dirty_addr, cpu_env, dirty_addr,
3478                              tcg_constant_i64(0));
3479         }
3480     }
3481 
3482     /* Form the 10-bit signed, scaled offset.  */
3483     offset = (extract32(insn, 22, 1) << 9) | extract32(insn, 12, 9);
3484     offset = sextract32(offset << size, 0, 10 + size);
3485     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3486 
3487     memop = finalize_memop(s, size);
3488 
3489     /* Note that "clean" and "dirty" here refer to TBI not PAC.  */
3490     clean_addr = gen_mte_check1(s, dirty_addr, false,
3491                                 is_wback || rn != 31, size);
3492 
3493     tcg_rt = cpu_reg(s, rt);
3494     do_gpr_ld(s, tcg_rt, clean_addr, memop,
3495               /* extend */ false, /* iss_valid */ !is_wback,
3496               /* iss_srt */ rt, /* iss_sf */ true, /* iss_ar */ false);
3497 
3498     if (is_wback) {
3499         tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3500     }
3501 }
3502 
3503 /*
3504  * LDAPR/STLR (unscaled immediate)
3505  *
3506  *  31  30            24    22  21       12    10    5     0
3507  * +------+-------------+-----+---+--------+-----+----+-----+
3508  * | size | 0 1 1 0 0 1 | opc | 0 |  imm9  | 0 0 | Rn |  Rt |
3509  * +------+-------------+-----+---+--------+-----+----+-----+
3510  *
3511  * Rt: source or destination register
3512  * Rn: base register
3513  * imm9: unscaled immediate offset
3514  * opc: 00: STLUR*, 01/10/11: various LDAPUR*
3515  * size: size of load/store
3516  */
3517 static void disas_ldst_ldapr_stlr(DisasContext *s, uint32_t insn)
3518 {
3519     int rt = extract32(insn, 0, 5);
3520     int rn = extract32(insn, 5, 5);
3521     int offset = sextract32(insn, 12, 9);
3522     int opc = extract32(insn, 22, 2);
3523     int size = extract32(insn, 30, 2);
3524     TCGv_i64 clean_addr, dirty_addr;
3525     bool is_store = false;
3526     bool extend = false;
3527     bool iss_sf;
3528     MemOp mop;
3529 
3530     if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3531         unallocated_encoding(s);
3532         return;
3533     }
3534 
3535     /* TODO: ARMv8.4-LSE SCTLR.nAA */
3536     mop = finalize_memop(s, size | MO_ALIGN);
3537 
3538     switch (opc) {
3539     case 0: /* STLURB */
3540         is_store = true;
3541         break;
3542     case 1: /* LDAPUR* */
3543         break;
3544     case 2: /* LDAPURS* 64-bit variant */
3545         if (size == 3) {
3546             unallocated_encoding(s);
3547             return;
3548         }
3549         mop |= MO_SIGN;
3550         break;
3551     case 3: /* LDAPURS* 32-bit variant */
3552         if (size > 1) {
3553             unallocated_encoding(s);
3554             return;
3555         }
3556         mop |= MO_SIGN;
3557         extend = true; /* zero-extend 32->64 after signed load */
3558         break;
3559     default:
3560         g_assert_not_reached();
3561     }
3562 
3563     iss_sf = disas_ldst_compute_iss_sf(size, (mop & MO_SIGN) != 0, opc);
3564 
3565     if (rn == 31) {
3566         gen_check_sp_alignment(s);
3567     }
3568 
3569     dirty_addr = read_cpu_reg_sp(s, rn, 1);
3570     tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3571     clean_addr = clean_data_tbi(s, dirty_addr);
3572 
3573     if (is_store) {
3574         /* Store-Release semantics */
3575         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3576         do_gpr_st(s, cpu_reg(s, rt), clean_addr, mop, true, rt, iss_sf, true);
3577     } else {
3578         /*
3579          * Load-AcquirePC semantics; we implement as the slightly more
3580          * restrictive Load-Acquire.
3581          */
3582         do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop,
3583                   extend, true, rt, iss_sf, true);
3584         tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3585     }
3586 }
3587 
3588 /* Load/store register (all forms) */
3589 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
3590 {
3591     int rt = extract32(insn, 0, 5);
3592     int opc = extract32(insn, 22, 2);
3593     bool is_vector = extract32(insn, 26, 1);
3594     int size = extract32(insn, 30, 2);
3595 
3596     switch (extract32(insn, 24, 2)) {
3597     case 0:
3598         if (extract32(insn, 21, 1) == 0) {
3599             /* Load/store register (unscaled immediate)
3600              * Load/store immediate pre/post-indexed
3601              * Load/store register unprivileged
3602              */
3603             disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
3604             return;
3605         }
3606         switch (extract32(insn, 10, 2)) {
3607         case 0:
3608             disas_ldst_atomic(s, insn, size, rt, is_vector);
3609             return;
3610         case 2:
3611             disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
3612             return;
3613         default:
3614             disas_ldst_pac(s, insn, size, rt, is_vector);
3615             return;
3616         }
3617         break;
3618     case 1:
3619         disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
3620         return;
3621     }
3622     unallocated_encoding(s);
3623 }
3624 
3625 /* AdvSIMD load/store multiple structures
3626  *
3627  *  31  30  29           23 22  21         16 15    12 11  10 9    5 4    0
3628  * +---+---+---------------+---+-------------+--------+------+------+------+
3629  * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size |  Rn  |  Rt  |
3630  * +---+---+---------------+---+-------------+--------+------+------+------+
3631  *
3632  * AdvSIMD load/store multiple structures (post-indexed)
3633  *
3634  *  31  30  29           23 22  21  20     16 15    12 11  10 9    5 4    0
3635  * +---+---+---------------+---+---+---------+--------+------+------+------+
3636  * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 |   Rm    | opcode | size |  Rn  |  Rt  |
3637  * +---+---+---------------+---+---+---------+--------+------+------+------+
3638  *
3639  * Rt: first (or only) SIMD&FP register to be transferred
3640  * Rn: base address or SP
3641  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3642  */
3643 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
3644 {
3645     int rt = extract32(insn, 0, 5);
3646     int rn = extract32(insn, 5, 5);
3647     int rm = extract32(insn, 16, 5);
3648     int size = extract32(insn, 10, 2);
3649     int opcode = extract32(insn, 12, 4);
3650     bool is_store = !extract32(insn, 22, 1);
3651     bool is_postidx = extract32(insn, 23, 1);
3652     bool is_q = extract32(insn, 30, 1);
3653     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3654     MemOp endian, align, mop;
3655 
3656     int total;    /* total bytes */
3657     int elements; /* elements per vector */
3658     int rpt;    /* num iterations */
3659     int selem;  /* structure elements */
3660     int r;
3661 
3662     if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
3663         unallocated_encoding(s);
3664         return;
3665     }
3666 
3667     if (!is_postidx && rm != 0) {
3668         unallocated_encoding(s);
3669         return;
3670     }
3671 
3672     /* From the shared decode logic */
3673     switch (opcode) {
3674     case 0x0:
3675         rpt = 1;
3676         selem = 4;
3677         break;
3678     case 0x2:
3679         rpt = 4;
3680         selem = 1;
3681         break;
3682     case 0x4:
3683         rpt = 1;
3684         selem = 3;
3685         break;
3686     case 0x6:
3687         rpt = 3;
3688         selem = 1;
3689         break;
3690     case 0x7:
3691         rpt = 1;
3692         selem = 1;
3693         break;
3694     case 0x8:
3695         rpt = 1;
3696         selem = 2;
3697         break;
3698     case 0xa:
3699         rpt = 2;
3700         selem = 1;
3701         break;
3702     default:
3703         unallocated_encoding(s);
3704         return;
3705     }
3706 
3707     if (size == 3 && !is_q && selem != 1) {
3708         /* reserved */
3709         unallocated_encoding(s);
3710         return;
3711     }
3712 
3713     if (!fp_access_check(s)) {
3714         return;
3715     }
3716 
3717     if (rn == 31) {
3718         gen_check_sp_alignment(s);
3719     }
3720 
3721     /* For our purposes, bytes are always little-endian.  */
3722     endian = s->be_data;
3723     if (size == 0) {
3724         endian = MO_LE;
3725     }
3726 
3727     total = rpt * selem * (is_q ? 16 : 8);
3728     tcg_rn = cpu_reg_sp(s, rn);
3729 
3730     /*
3731      * Issue the MTE check vs the logical repeat count, before we
3732      * promote consecutive little-endian elements below.
3733      */
3734     clean_addr = gen_mte_checkN(s, tcg_rn, is_store, is_postidx || rn != 31,
3735                                 total);
3736 
3737     /*
3738      * Consecutive little-endian elements from a single register
3739      * can be promoted to a larger little-endian operation.
3740      */
3741     align = MO_ALIGN;
3742     if (selem == 1 && endian == MO_LE) {
3743         align = pow2_align(size);
3744         size = 3;
3745     }
3746     if (!s->align_mem) {
3747         align = 0;
3748     }
3749     mop = endian | size | align;
3750 
3751     elements = (is_q ? 16 : 8) >> size;
3752     tcg_ebytes = tcg_constant_i64(1 << size);
3753     for (r = 0; r < rpt; r++) {
3754         int e;
3755         for (e = 0; e < elements; e++) {
3756             int xs;
3757             for (xs = 0; xs < selem; xs++) {
3758                 int tt = (rt + r + xs) % 32;
3759                 if (is_store) {
3760                     do_vec_st(s, tt, e, clean_addr, mop);
3761                 } else {
3762                     do_vec_ld(s, tt, e, clean_addr, mop);
3763                 }
3764                 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3765             }
3766         }
3767     }
3768 
3769     if (!is_store) {
3770         /* For non-quad operations, setting a slice of the low
3771          * 64 bits of the register clears the high 64 bits (in
3772          * the ARM ARM pseudocode this is implicit in the fact
3773          * that 'rval' is a 64 bit wide variable).
3774          * For quad operations, we might still need to zero the
3775          * high bits of SVE.
3776          */
3777         for (r = 0; r < rpt * selem; r++) {
3778             int tt = (rt + r) % 32;
3779             clear_vec_high(s, is_q, tt);
3780         }
3781     }
3782 
3783     if (is_postidx) {
3784         if (rm == 31) {
3785             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3786         } else {
3787             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3788         }
3789     }
3790 }
3791 
3792 /* AdvSIMD load/store single structure
3793  *
3794  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3795  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3796  * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size |  Rn  |  Rt  |
3797  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3798  *
3799  * AdvSIMD load/store single structure (post-indexed)
3800  *
3801  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
3802  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3803  * | 0 | Q | 0 0 1 1 0 1 1 | L R |     Rm    | opc | S | size |  Rn  |  Rt  |
3804  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3805  *
3806  * Rt: first (or only) SIMD&FP register to be transferred
3807  * Rn: base address or SP
3808  * Rm (post-index only): post-index register (when !31) or size dependent #imm
3809  * index = encoded in Q:S:size dependent on size
3810  *
3811  * lane_size = encoded in R, opc
3812  * transfer width = encoded in opc, S, size
3813  */
3814 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
3815 {
3816     int rt = extract32(insn, 0, 5);
3817     int rn = extract32(insn, 5, 5);
3818     int rm = extract32(insn, 16, 5);
3819     int size = extract32(insn, 10, 2);
3820     int S = extract32(insn, 12, 1);
3821     int opc = extract32(insn, 13, 3);
3822     int R = extract32(insn, 21, 1);
3823     int is_load = extract32(insn, 22, 1);
3824     int is_postidx = extract32(insn, 23, 1);
3825     int is_q = extract32(insn, 30, 1);
3826 
3827     int scale = extract32(opc, 1, 2);
3828     int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
3829     bool replicate = false;
3830     int index = is_q << 3 | S << 2 | size;
3831     int xs, total;
3832     TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3833     MemOp mop;
3834 
3835     if (extract32(insn, 31, 1)) {
3836         unallocated_encoding(s);
3837         return;
3838     }
3839     if (!is_postidx && rm != 0) {
3840         unallocated_encoding(s);
3841         return;
3842     }
3843 
3844     switch (scale) {
3845     case 3:
3846         if (!is_load || S) {
3847             unallocated_encoding(s);
3848             return;
3849         }
3850         scale = size;
3851         replicate = true;
3852         break;
3853     case 0:
3854         break;
3855     case 1:
3856         if (extract32(size, 0, 1)) {
3857             unallocated_encoding(s);
3858             return;
3859         }
3860         index >>= 1;
3861         break;
3862     case 2:
3863         if (extract32(size, 1, 1)) {
3864             unallocated_encoding(s);
3865             return;
3866         }
3867         if (!extract32(size, 0, 1)) {
3868             index >>= 2;
3869         } else {
3870             if (S) {
3871                 unallocated_encoding(s);
3872                 return;
3873             }
3874             index >>= 3;
3875             scale = 3;
3876         }
3877         break;
3878     default:
3879         g_assert_not_reached();
3880     }
3881 
3882     if (!fp_access_check(s)) {
3883         return;
3884     }
3885 
3886     if (rn == 31) {
3887         gen_check_sp_alignment(s);
3888     }
3889 
3890     total = selem << scale;
3891     tcg_rn = cpu_reg_sp(s, rn);
3892 
3893     clean_addr = gen_mte_checkN(s, tcg_rn, !is_load, is_postidx || rn != 31,
3894                                 total);
3895     mop = finalize_memop(s, scale);
3896 
3897     tcg_ebytes = tcg_constant_i64(1 << scale);
3898     for (xs = 0; xs < selem; xs++) {
3899         if (replicate) {
3900             /* Load and replicate to all elements */
3901             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3902 
3903             tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
3904             tcg_gen_gvec_dup_i64(scale, vec_full_reg_offset(s, rt),
3905                                  (is_q + 1) * 8, vec_full_reg_size(s),
3906                                  tcg_tmp);
3907         } else {
3908             /* Load/store one element per register */
3909             if (is_load) {
3910                 do_vec_ld(s, rt, index, clean_addr, mop);
3911             } else {
3912                 do_vec_st(s, rt, index, clean_addr, mop);
3913             }
3914         }
3915         tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3916         rt = (rt + 1) % 32;
3917     }
3918 
3919     if (is_postidx) {
3920         if (rm == 31) {
3921             tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3922         } else {
3923             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3924         }
3925     }
3926 }
3927 
3928 /*
3929  * Load/Store memory tags
3930  *
3931  *  31 30 29         24     22  21     12    10      5      0
3932  * +-----+-------------+-----+---+------+-----+------+------+
3933  * | 1 1 | 0 1 1 0 0 1 | op1 | 1 | imm9 | op2 |  Rn  |  Rt  |
3934  * +-----+-------------+-----+---+------+-----+------+------+
3935  */
3936 static void disas_ldst_tag(DisasContext *s, uint32_t insn)
3937 {
3938     int rt = extract32(insn, 0, 5);
3939     int rn = extract32(insn, 5, 5);
3940     uint64_t offset = sextract64(insn, 12, 9) << LOG2_TAG_GRANULE;
3941     int op2 = extract32(insn, 10, 2);
3942     int op1 = extract32(insn, 22, 2);
3943     bool is_load = false, is_pair = false, is_zero = false, is_mult = false;
3944     int index = 0;
3945     TCGv_i64 addr, clean_addr, tcg_rt;
3946 
3947     /* We checked insn bits [29:24,21] in the caller.  */
3948     if (extract32(insn, 30, 2) != 3) {
3949         goto do_unallocated;
3950     }
3951 
3952     /*
3953      * @index is a tri-state variable which has 3 states:
3954      * < 0 : post-index, writeback
3955      * = 0 : signed offset
3956      * > 0 : pre-index, writeback
3957      */
3958     switch (op1) {
3959     case 0:
3960         if (op2 != 0) {
3961             /* STG */
3962             index = op2 - 2;
3963         } else {
3964             /* STZGM */
3965             if (s->current_el == 0 || offset != 0) {
3966                 goto do_unallocated;
3967             }
3968             is_mult = is_zero = true;
3969         }
3970         break;
3971     case 1:
3972         if (op2 != 0) {
3973             /* STZG */
3974             is_zero = true;
3975             index = op2 - 2;
3976         } else {
3977             /* LDG */
3978             is_load = true;
3979         }
3980         break;
3981     case 2:
3982         if (op2 != 0) {
3983             /* ST2G */
3984             is_pair = true;
3985             index = op2 - 2;
3986         } else {
3987             /* STGM */
3988             if (s->current_el == 0 || offset != 0) {
3989                 goto do_unallocated;
3990             }
3991             is_mult = true;
3992         }
3993         break;
3994     case 3:
3995         if (op2 != 0) {
3996             /* STZ2G */
3997             is_pair = is_zero = true;
3998             index = op2 - 2;
3999         } else {
4000             /* LDGM */
4001             if (s->current_el == 0 || offset != 0) {
4002                 goto do_unallocated;
4003             }
4004             is_mult = is_load = true;
4005         }
4006         break;
4007 
4008     default:
4009     do_unallocated:
4010         unallocated_encoding(s);
4011         return;
4012     }
4013 
4014     if (is_mult
4015         ? !dc_isar_feature(aa64_mte, s)
4016         : !dc_isar_feature(aa64_mte_insn_reg, s)) {
4017         goto do_unallocated;
4018     }
4019 
4020     if (rn == 31) {
4021         gen_check_sp_alignment(s);
4022     }
4023 
4024     addr = read_cpu_reg_sp(s, rn, true);
4025     if (index >= 0) {
4026         /* pre-index or signed offset */
4027         tcg_gen_addi_i64(addr, addr, offset);
4028     }
4029 
4030     if (is_mult) {
4031         tcg_rt = cpu_reg(s, rt);
4032 
4033         if (is_zero) {
4034             int size = 4 << s->dcz_blocksize;
4035 
4036             if (s->ata) {
4037                 gen_helper_stzgm_tags(cpu_env, addr, tcg_rt);
4038             }
4039             /*
4040              * The non-tags portion of STZGM is mostly like DC_ZVA,
4041              * except the alignment happens before the access.
4042              */
4043             clean_addr = clean_data_tbi(s, addr);
4044             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4045             gen_helper_dc_zva(cpu_env, clean_addr);
4046         } else if (s->ata) {
4047             if (is_load) {
4048                 gen_helper_ldgm(tcg_rt, cpu_env, addr);
4049             } else {
4050                 gen_helper_stgm(cpu_env, addr, tcg_rt);
4051             }
4052         } else {
4053             MMUAccessType acc = is_load ? MMU_DATA_LOAD : MMU_DATA_STORE;
4054             int size = 4 << GMID_EL1_BS;
4055 
4056             clean_addr = clean_data_tbi(s, addr);
4057             tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4058             gen_probe_access(s, clean_addr, acc, size);
4059 
4060             if (is_load) {
4061                 /* The result tags are zeros.  */
4062                 tcg_gen_movi_i64(tcg_rt, 0);
4063             }
4064         }
4065         return;
4066     }
4067 
4068     if (is_load) {
4069         tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
4070         tcg_rt = cpu_reg(s, rt);
4071         if (s->ata) {
4072             gen_helper_ldg(tcg_rt, cpu_env, addr, tcg_rt);
4073         } else {
4074             clean_addr = clean_data_tbi(s, addr);
4075             gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
4076             gen_address_with_allocation_tag0(tcg_rt, addr);
4077         }
4078     } else {
4079         tcg_rt = cpu_reg_sp(s, rt);
4080         if (!s->ata) {
4081             /*
4082              * For STG and ST2G, we need to check alignment and probe memory.
4083              * TODO: For STZG and STZ2G, we could rely on the stores below,
4084              * at least for system mode; user-only won't enforce alignment.
4085              */
4086             if (is_pair) {
4087                 gen_helper_st2g_stub(cpu_env, addr);
4088             } else {
4089                 gen_helper_stg_stub(cpu_env, addr);
4090             }
4091         } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
4092             if (is_pair) {
4093                 gen_helper_st2g_parallel(cpu_env, addr, tcg_rt);
4094             } else {
4095                 gen_helper_stg_parallel(cpu_env, addr, tcg_rt);
4096             }
4097         } else {
4098             if (is_pair) {
4099                 gen_helper_st2g(cpu_env, addr, tcg_rt);
4100             } else {
4101                 gen_helper_stg(cpu_env, addr, tcg_rt);
4102             }
4103         }
4104     }
4105 
4106     if (is_zero) {
4107         TCGv_i64 clean_addr = clean_data_tbi(s, addr);
4108         TCGv_i64 zero64 = tcg_constant_i64(0);
4109         TCGv_i128 zero128 = tcg_temp_new_i128();
4110         int mem_index = get_mem_index(s);
4111         MemOp mop = finalize_memop(s, MO_128 | MO_ALIGN);
4112 
4113         tcg_gen_concat_i64_i128(zero128, zero64, zero64);
4114 
4115         /* This is 1 or 2 atomic 16-byte operations. */
4116         tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4117         if (is_pair) {
4118             tcg_gen_addi_i64(clean_addr, clean_addr, 16);
4119             tcg_gen_qemu_st_i128(zero128, clean_addr, mem_index, mop);
4120         }
4121     }
4122 
4123     if (index != 0) {
4124         /* pre-index or post-index */
4125         if (index < 0) {
4126             /* post-index */
4127             tcg_gen_addi_i64(addr, addr, offset);
4128         }
4129         tcg_gen_mov_i64(cpu_reg_sp(s, rn), addr);
4130     }
4131 }
4132 
4133 /* Loads and stores */
4134 static void disas_ldst(DisasContext *s, uint32_t insn)
4135 {
4136     switch (extract32(insn, 24, 6)) {
4137     case 0x08: /* Load/store exclusive */
4138         disas_ldst_excl(s, insn);
4139         break;
4140     case 0x18: case 0x1c: /* Load register (literal) */
4141         disas_ld_lit(s, insn);
4142         break;
4143     case 0x28: case 0x29:
4144     case 0x2c: case 0x2d: /* Load/store pair (all forms) */
4145         disas_ldst_pair(s, insn);
4146         break;
4147     case 0x38: case 0x39:
4148     case 0x3c: case 0x3d: /* Load/store register (all forms) */
4149         disas_ldst_reg(s, insn);
4150         break;
4151     case 0x0c: /* AdvSIMD load/store multiple structures */
4152         disas_ldst_multiple_struct(s, insn);
4153         break;
4154     case 0x0d: /* AdvSIMD load/store single structure */
4155         disas_ldst_single_struct(s, insn);
4156         break;
4157     case 0x19:
4158         if (extract32(insn, 21, 1) != 0) {
4159             disas_ldst_tag(s, insn);
4160         } else if (extract32(insn, 10, 2) == 0) {
4161             disas_ldst_ldapr_stlr(s, insn);
4162         } else {
4163             unallocated_encoding(s);
4164         }
4165         break;
4166     default:
4167         unallocated_encoding(s);
4168         break;
4169     }
4170 }
4171 
4172 typedef void ArithTwoOp(TCGv_i64, TCGv_i64, TCGv_i64);
4173 
4174 static bool gen_rri(DisasContext *s, arg_rri_sf *a,
4175                     bool rd_sp, bool rn_sp, ArithTwoOp *fn)
4176 {
4177     TCGv_i64 tcg_rn = rn_sp ? cpu_reg_sp(s, a->rn) : cpu_reg(s, a->rn);
4178     TCGv_i64 tcg_rd = rd_sp ? cpu_reg_sp(s, a->rd) : cpu_reg(s, a->rd);
4179     TCGv_i64 tcg_imm = tcg_constant_i64(a->imm);
4180 
4181     fn(tcg_rd, tcg_rn, tcg_imm);
4182     if (!a->sf) {
4183         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4184     }
4185     return true;
4186 }
4187 
4188 /*
4189  * PC-rel. addressing
4190  */
4191 
4192 static bool trans_ADR(DisasContext *s, arg_ri *a)
4193 {
4194     gen_pc_plus_diff(s, cpu_reg(s, a->rd), a->imm);
4195     return true;
4196 }
4197 
4198 static bool trans_ADRP(DisasContext *s, arg_ri *a)
4199 {
4200     int64_t offset = (int64_t)a->imm << 12;
4201 
4202     /* The page offset is ok for CF_PCREL. */
4203     offset -= s->pc_curr & 0xfff;
4204     gen_pc_plus_diff(s, cpu_reg(s, a->rd), offset);
4205     return true;
4206 }
4207 
4208 /*
4209  * Add/subtract (immediate)
4210  */
4211 TRANS(ADD_i, gen_rri, a, 1, 1, tcg_gen_add_i64)
4212 TRANS(SUB_i, gen_rri, a, 1, 1, tcg_gen_sub_i64)
4213 TRANS(ADDS_i, gen_rri, a, 0, 1, a->sf ? gen_add64_CC : gen_add32_CC)
4214 TRANS(SUBS_i, gen_rri, a, 0, 1, a->sf ? gen_sub64_CC : gen_sub32_CC)
4215 
4216 /*
4217  * Add/subtract (immediate, with tags)
4218  */
4219 
4220 static bool gen_add_sub_imm_with_tags(DisasContext *s, arg_rri_tag *a,
4221                                       bool sub_op)
4222 {
4223     TCGv_i64 tcg_rn, tcg_rd;
4224     int imm;
4225 
4226     imm = a->uimm6 << LOG2_TAG_GRANULE;
4227     if (sub_op) {
4228         imm = -imm;
4229     }
4230 
4231     tcg_rn = cpu_reg_sp(s, a->rn);
4232     tcg_rd = cpu_reg_sp(s, a->rd);
4233 
4234     if (s->ata) {
4235         gen_helper_addsubg(tcg_rd, cpu_env, tcg_rn,
4236                            tcg_constant_i32(imm),
4237                            tcg_constant_i32(a->uimm4));
4238     } else {
4239         tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4240         gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4241     }
4242     return true;
4243 }
4244 
4245 TRANS_FEAT(ADDG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, false)
4246 TRANS_FEAT(SUBG_i, aa64_mte_insn_reg, gen_add_sub_imm_with_tags, a, true)
4247 
4248 /* The input should be a value in the bottom e bits (with higher
4249  * bits zero); returns that value replicated into every element
4250  * of size e in a 64 bit integer.
4251  */
4252 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4253 {
4254     assert(e != 0);
4255     while (e < 64) {
4256         mask |= mask << e;
4257         e *= 2;
4258     }
4259     return mask;
4260 }
4261 
4262 /*
4263  * Logical (immediate)
4264  */
4265 
4266 /*
4267  * Simplified variant of pseudocode DecodeBitMasks() for the case where we
4268  * only require the wmask. Returns false if the imms/immr/immn are a reserved
4269  * value (ie should cause a guest UNDEF exception), and true if they are
4270  * valid, in which case the decoded bit pattern is written to result.
4271  */
4272 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4273                             unsigned int imms, unsigned int immr)
4274 {
4275     uint64_t mask;
4276     unsigned e, levels, s, r;
4277     int len;
4278 
4279     assert(immn < 2 && imms < 64 && immr < 64);
4280 
4281     /* The bit patterns we create here are 64 bit patterns which
4282      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4283      * 64 bits each. Each element contains the same value: a run
4284      * of between 1 and e-1 non-zero bits, rotated within the
4285      * element by between 0 and e-1 bits.
4286      *
4287      * The element size and run length are encoded into immn (1 bit)
4288      * and imms (6 bits) as follows:
4289      * 64 bit elements: immn = 1, imms = <length of run - 1>
4290      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4291      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4292      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4293      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4294      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4295      * Notice that immn = 0, imms = 11111x is the only combination
4296      * not covered by one of the above options; this is reserved.
4297      * Further, <length of run - 1> all-ones is a reserved pattern.
4298      *
4299      * In all cases the rotation is by immr % e (and immr is 6 bits).
4300      */
4301 
4302     /* First determine the element size */
4303     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4304     if (len < 1) {
4305         /* This is the immn == 0, imms == 0x11111x case */
4306         return false;
4307     }
4308     e = 1 << len;
4309 
4310     levels = e - 1;
4311     s = imms & levels;
4312     r = immr & levels;
4313 
4314     if (s == levels) {
4315         /* <length of run - 1> mustn't be all-ones. */
4316         return false;
4317     }
4318 
4319     /* Create the value of one element: s+1 set bits rotated
4320      * by r within the element (which is e bits wide)...
4321      */
4322     mask = MAKE_64BIT_MASK(0, s + 1);
4323     if (r) {
4324         mask = (mask >> r) | (mask << (e - r));
4325         mask &= MAKE_64BIT_MASK(0, e);
4326     }
4327     /* ...then replicate the element over the whole 64 bit value */
4328     mask = bitfield_replicate(mask, e);
4329     *result = mask;
4330     return true;
4331 }
4332 
4333 static bool gen_rri_log(DisasContext *s, arg_rri_log *a, bool set_cc,
4334                         void (*fn)(TCGv_i64, TCGv_i64, int64_t))
4335 {
4336     TCGv_i64 tcg_rd, tcg_rn;
4337     uint64_t imm;
4338 
4339     /* Some immediate field values are reserved. */
4340     if (!logic_imm_decode_wmask(&imm, extract32(a->dbm, 12, 1),
4341                                 extract32(a->dbm, 0, 6),
4342                                 extract32(a->dbm, 6, 6))) {
4343         return false;
4344     }
4345     if (!a->sf) {
4346         imm &= 0xffffffffull;
4347     }
4348 
4349     tcg_rd = set_cc ? cpu_reg(s, a->rd) : cpu_reg_sp(s, a->rd);
4350     tcg_rn = cpu_reg(s, a->rn);
4351 
4352     fn(tcg_rd, tcg_rn, imm);
4353     if (set_cc) {
4354         gen_logic_CC(a->sf, tcg_rd);
4355     }
4356     if (!a->sf) {
4357         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4358     }
4359     return true;
4360 }
4361 
4362 TRANS(AND_i, gen_rri_log, a, false, tcg_gen_andi_i64)
4363 TRANS(ORR_i, gen_rri_log, a, false, tcg_gen_ori_i64)
4364 TRANS(EOR_i, gen_rri_log, a, false, tcg_gen_xori_i64)
4365 TRANS(ANDS_i, gen_rri_log, a, true, tcg_gen_andi_i64)
4366 
4367 /*
4368  * Move wide (immediate)
4369  */
4370 
4371 static bool trans_MOVZ(DisasContext *s, arg_movw *a)
4372 {
4373     int pos = a->hw << 4;
4374     tcg_gen_movi_i64(cpu_reg(s, a->rd), (uint64_t)a->imm << pos);
4375     return true;
4376 }
4377 
4378 static bool trans_MOVN(DisasContext *s, arg_movw *a)
4379 {
4380     int pos = a->hw << 4;
4381     uint64_t imm = a->imm;
4382 
4383     imm = ~(imm << pos);
4384     if (!a->sf) {
4385         imm = (uint32_t)imm;
4386     }
4387     tcg_gen_movi_i64(cpu_reg(s, a->rd), imm);
4388     return true;
4389 }
4390 
4391 static bool trans_MOVK(DisasContext *s, arg_movw *a)
4392 {
4393     int pos = a->hw << 4;
4394     TCGv_i64 tcg_rd, tcg_im;
4395 
4396     tcg_rd = cpu_reg(s, a->rd);
4397     tcg_im = tcg_constant_i64(a->imm);
4398     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_im, pos, 16);
4399     if (!a->sf) {
4400         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4401     }
4402     return true;
4403 }
4404 
4405 /*
4406  * Bitfield
4407  */
4408 
4409 static bool trans_SBFM(DisasContext *s, arg_SBFM *a)
4410 {
4411     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4412     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4413     unsigned int bitsize = a->sf ? 64 : 32;
4414     unsigned int ri = a->immr;
4415     unsigned int si = a->imms;
4416     unsigned int pos, len;
4417 
4418     if (si >= ri) {
4419         /* Wd<s-r:0> = Wn<s:r> */
4420         len = (si - ri) + 1;
4421         tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4422         if (!a->sf) {
4423             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4424         }
4425     } else {
4426         /* Wd<32+s-r,32-r> = Wn<s:0> */
4427         len = si + 1;
4428         pos = (bitsize - ri) & (bitsize - 1);
4429 
4430         if (len < ri) {
4431             /*
4432              * Sign extend the destination field from len to fill the
4433              * balance of the word.  Let the deposit below insert all
4434              * of those sign bits.
4435              */
4436             tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4437             len = ri;
4438         }
4439 
4440         /*
4441          * We start with zero, and we haven't modified any bits outside
4442          * bitsize, therefore no final zero-extension is unneeded for !sf.
4443          */
4444         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4445     }
4446     return true;
4447 }
4448 
4449 static bool trans_UBFM(DisasContext *s, arg_UBFM *a)
4450 {
4451     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4452     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4453     unsigned int bitsize = a->sf ? 64 : 32;
4454     unsigned int ri = a->immr;
4455     unsigned int si = a->imms;
4456     unsigned int pos, len;
4457 
4458     tcg_rd = cpu_reg(s, a->rd);
4459     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4460 
4461     if (si >= ri) {
4462         /* Wd<s-r:0> = Wn<s:r> */
4463         len = (si - ri) + 1;
4464         tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4465     } else {
4466         /* Wd<32+s-r,32-r> = Wn<s:0> */
4467         len = si + 1;
4468         pos = (bitsize - ri) & (bitsize - 1);
4469         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4470     }
4471     return true;
4472 }
4473 
4474 static bool trans_BFM(DisasContext *s, arg_BFM *a)
4475 {
4476     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
4477     TCGv_i64 tcg_tmp = read_cpu_reg(s, a->rn, 1);
4478     unsigned int bitsize = a->sf ? 64 : 32;
4479     unsigned int ri = a->immr;
4480     unsigned int si = a->imms;
4481     unsigned int pos, len;
4482 
4483     tcg_rd = cpu_reg(s, a->rd);
4484     tcg_tmp = read_cpu_reg(s, a->rn, 1);
4485 
4486     if (si >= ri) {
4487         /* Wd<s-r:0> = Wn<s:r> */
4488         tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4489         len = (si - ri) + 1;
4490         pos = 0;
4491     } else {
4492         /* Wd<32+s-r,32-r> = Wn<s:0> */
4493         len = si + 1;
4494         pos = (bitsize - ri) & (bitsize - 1);
4495     }
4496 
4497     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4498     if (!a->sf) {
4499         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4500     }
4501     return true;
4502 }
4503 
4504 static bool trans_EXTR(DisasContext *s, arg_extract *a)
4505 {
4506     TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4507 
4508     tcg_rd = cpu_reg(s, a->rd);
4509 
4510     if (unlikely(a->imm == 0)) {
4511         /*
4512          * tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4513          * so an extract from bit 0 is a special case.
4514          */
4515         if (a->sf) {
4516             tcg_gen_mov_i64(tcg_rd, cpu_reg(s, a->rm));
4517         } else {
4518             tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, a->rm));
4519         }
4520     } else {
4521         tcg_rm = cpu_reg(s, a->rm);
4522         tcg_rn = cpu_reg(s, a->rn);
4523 
4524         if (a->sf) {
4525             /* Specialization to ROR happens in EXTRACT2.  */
4526             tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, a->imm);
4527         } else {
4528             TCGv_i32 t0 = tcg_temp_new_i32();
4529 
4530             tcg_gen_extrl_i64_i32(t0, tcg_rm);
4531             if (a->rm == a->rn) {
4532                 tcg_gen_rotri_i32(t0, t0, a->imm);
4533             } else {
4534                 TCGv_i32 t1 = tcg_temp_new_i32();
4535                 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4536                 tcg_gen_extract2_i32(t0, t0, t1, a->imm);
4537             }
4538             tcg_gen_extu_i32_i64(tcg_rd, t0);
4539         }
4540     }
4541     return true;
4542 }
4543 
4544 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4545  * Note that it is the caller's responsibility to ensure that the
4546  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4547  * mandated semantics for out of range shifts.
4548  */
4549 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4550                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4551 {
4552     switch (shift_type) {
4553     case A64_SHIFT_TYPE_LSL:
4554         tcg_gen_shl_i64(dst, src, shift_amount);
4555         break;
4556     case A64_SHIFT_TYPE_LSR:
4557         tcg_gen_shr_i64(dst, src, shift_amount);
4558         break;
4559     case A64_SHIFT_TYPE_ASR:
4560         if (!sf) {
4561             tcg_gen_ext32s_i64(dst, src);
4562         }
4563         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4564         break;
4565     case A64_SHIFT_TYPE_ROR:
4566         if (sf) {
4567             tcg_gen_rotr_i64(dst, src, shift_amount);
4568         } else {
4569             TCGv_i32 t0, t1;
4570             t0 = tcg_temp_new_i32();
4571             t1 = tcg_temp_new_i32();
4572             tcg_gen_extrl_i64_i32(t0, src);
4573             tcg_gen_extrl_i64_i32(t1, shift_amount);
4574             tcg_gen_rotr_i32(t0, t0, t1);
4575             tcg_gen_extu_i32_i64(dst, t0);
4576         }
4577         break;
4578     default:
4579         assert(FALSE); /* all shift types should be handled */
4580         break;
4581     }
4582 
4583     if (!sf) { /* zero extend final result */
4584         tcg_gen_ext32u_i64(dst, dst);
4585     }
4586 }
4587 
4588 /* Shift a TCGv src by immediate, put result in dst.
4589  * The shift amount must be in range (this should always be true as the
4590  * relevant instructions will UNDEF on bad shift immediates).
4591  */
4592 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4593                           enum a64_shift_type shift_type, unsigned int shift_i)
4594 {
4595     assert(shift_i < (sf ? 64 : 32));
4596 
4597     if (shift_i == 0) {
4598         tcg_gen_mov_i64(dst, src);
4599     } else {
4600         shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
4601     }
4602 }
4603 
4604 /* Logical (shifted register)
4605  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
4606  * +----+-----+-----------+-------+---+------+--------+------+------+
4607  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
4608  * +----+-----+-----------+-------+---+------+--------+------+------+
4609  */
4610 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4611 {
4612     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4613     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4614 
4615     sf = extract32(insn, 31, 1);
4616     opc = extract32(insn, 29, 2);
4617     shift_type = extract32(insn, 22, 2);
4618     invert = extract32(insn, 21, 1);
4619     rm = extract32(insn, 16, 5);
4620     shift_amount = extract32(insn, 10, 6);
4621     rn = extract32(insn, 5, 5);
4622     rd = extract32(insn, 0, 5);
4623 
4624     if (!sf && (shift_amount & (1 << 5))) {
4625         unallocated_encoding(s);
4626         return;
4627     }
4628 
4629     tcg_rd = cpu_reg(s, rd);
4630 
4631     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4632         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4633          * register-register MOV and MVN, so it is worth special casing.
4634          */
4635         tcg_rm = cpu_reg(s, rm);
4636         if (invert) {
4637             tcg_gen_not_i64(tcg_rd, tcg_rm);
4638             if (!sf) {
4639                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4640             }
4641         } else {
4642             if (sf) {
4643                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4644             } else {
4645                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4646             }
4647         }
4648         return;
4649     }
4650 
4651     tcg_rm = read_cpu_reg(s, rm, sf);
4652 
4653     if (shift_amount) {
4654         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4655     }
4656 
4657     tcg_rn = cpu_reg(s, rn);
4658 
4659     switch (opc | (invert << 2)) {
4660     case 0: /* AND */
4661     case 3: /* ANDS */
4662         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4663         break;
4664     case 1: /* ORR */
4665         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4666         break;
4667     case 2: /* EOR */
4668         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4669         break;
4670     case 4: /* BIC */
4671     case 7: /* BICS */
4672         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4673         break;
4674     case 5: /* ORN */
4675         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4676         break;
4677     case 6: /* EON */
4678         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4679         break;
4680     default:
4681         assert(FALSE);
4682         break;
4683     }
4684 
4685     if (!sf) {
4686         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4687     }
4688 
4689     if (opc == 3) {
4690         gen_logic_CC(sf, tcg_rd);
4691     }
4692 }
4693 
4694 /*
4695  * Add/subtract (extended register)
4696  *
4697  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
4698  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4699  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
4700  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4701  *
4702  *  sf: 0 -> 32bit, 1 -> 64bit
4703  *  op: 0 -> add  , 1 -> sub
4704  *   S: 1 -> set flags
4705  * opt: 00
4706  * option: extension type (see DecodeRegExtend)
4707  * imm3: optional shift to Rm
4708  *
4709  * Rd = Rn + LSL(extend(Rm), amount)
4710  */
4711 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4712 {
4713     int rd = extract32(insn, 0, 5);
4714     int rn = extract32(insn, 5, 5);
4715     int imm3 = extract32(insn, 10, 3);
4716     int option = extract32(insn, 13, 3);
4717     int rm = extract32(insn, 16, 5);
4718     int opt = extract32(insn, 22, 2);
4719     bool setflags = extract32(insn, 29, 1);
4720     bool sub_op = extract32(insn, 30, 1);
4721     bool sf = extract32(insn, 31, 1);
4722 
4723     TCGv_i64 tcg_rm, tcg_rn; /* temps */
4724     TCGv_i64 tcg_rd;
4725     TCGv_i64 tcg_result;
4726 
4727     if (imm3 > 4 || opt != 0) {
4728         unallocated_encoding(s);
4729         return;
4730     }
4731 
4732     /* non-flag setting ops may use SP */
4733     if (!setflags) {
4734         tcg_rd = cpu_reg_sp(s, rd);
4735     } else {
4736         tcg_rd = cpu_reg(s, rd);
4737     }
4738     tcg_rn = read_cpu_reg_sp(s, rn, sf);
4739 
4740     tcg_rm = read_cpu_reg(s, rm, sf);
4741     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4742 
4743     tcg_result = tcg_temp_new_i64();
4744 
4745     if (!setflags) {
4746         if (sub_op) {
4747             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4748         } else {
4749             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4750         }
4751     } else {
4752         if (sub_op) {
4753             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4754         } else {
4755             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4756         }
4757     }
4758 
4759     if (sf) {
4760         tcg_gen_mov_i64(tcg_rd, tcg_result);
4761     } else {
4762         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4763     }
4764 }
4765 
4766 /*
4767  * Add/subtract (shifted register)
4768  *
4769  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
4770  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4771  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
4772  * +--+--+--+-----------+-----+--+-------+---------+------+------+
4773  *
4774  *    sf: 0 -> 32bit, 1 -> 64bit
4775  *    op: 0 -> add  , 1 -> sub
4776  *     S: 1 -> set flags
4777  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4778  *  imm6: Shift amount to apply to Rm before the add/sub
4779  */
4780 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4781 {
4782     int rd = extract32(insn, 0, 5);
4783     int rn = extract32(insn, 5, 5);
4784     int imm6 = extract32(insn, 10, 6);
4785     int rm = extract32(insn, 16, 5);
4786     int shift_type = extract32(insn, 22, 2);
4787     bool setflags = extract32(insn, 29, 1);
4788     bool sub_op = extract32(insn, 30, 1);
4789     bool sf = extract32(insn, 31, 1);
4790 
4791     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4792     TCGv_i64 tcg_rn, tcg_rm;
4793     TCGv_i64 tcg_result;
4794 
4795     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4796         unallocated_encoding(s);
4797         return;
4798     }
4799 
4800     tcg_rn = read_cpu_reg(s, rn, sf);
4801     tcg_rm = read_cpu_reg(s, rm, sf);
4802 
4803     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4804 
4805     tcg_result = tcg_temp_new_i64();
4806 
4807     if (!setflags) {
4808         if (sub_op) {
4809             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4810         } else {
4811             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4812         }
4813     } else {
4814         if (sub_op) {
4815             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4816         } else {
4817             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4818         }
4819     }
4820 
4821     if (sf) {
4822         tcg_gen_mov_i64(tcg_rd, tcg_result);
4823     } else {
4824         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4825     }
4826 }
4827 
4828 /* Data-processing (3 source)
4829  *
4830  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
4831  *  +--+------+-----------+------+------+----+------+------+------+
4832  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
4833  *  +--+------+-----------+------+------+----+------+------+------+
4834  */
4835 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4836 {
4837     int rd = extract32(insn, 0, 5);
4838     int rn = extract32(insn, 5, 5);
4839     int ra = extract32(insn, 10, 5);
4840     int rm = extract32(insn, 16, 5);
4841     int op_id = (extract32(insn, 29, 3) << 4) |
4842         (extract32(insn, 21, 3) << 1) |
4843         extract32(insn, 15, 1);
4844     bool sf = extract32(insn, 31, 1);
4845     bool is_sub = extract32(op_id, 0, 1);
4846     bool is_high = extract32(op_id, 2, 1);
4847     bool is_signed = false;
4848     TCGv_i64 tcg_op1;
4849     TCGv_i64 tcg_op2;
4850     TCGv_i64 tcg_tmp;
4851 
4852     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
4853     switch (op_id) {
4854     case 0x42: /* SMADDL */
4855     case 0x43: /* SMSUBL */
4856     case 0x44: /* SMULH */
4857         is_signed = true;
4858         break;
4859     case 0x0: /* MADD (32bit) */
4860     case 0x1: /* MSUB (32bit) */
4861     case 0x40: /* MADD (64bit) */
4862     case 0x41: /* MSUB (64bit) */
4863     case 0x4a: /* UMADDL */
4864     case 0x4b: /* UMSUBL */
4865     case 0x4c: /* UMULH */
4866         break;
4867     default:
4868         unallocated_encoding(s);
4869         return;
4870     }
4871 
4872     if (is_high) {
4873         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
4874         TCGv_i64 tcg_rd = cpu_reg(s, rd);
4875         TCGv_i64 tcg_rn = cpu_reg(s, rn);
4876         TCGv_i64 tcg_rm = cpu_reg(s, rm);
4877 
4878         if (is_signed) {
4879             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4880         } else {
4881             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4882         }
4883         return;
4884     }
4885 
4886     tcg_op1 = tcg_temp_new_i64();
4887     tcg_op2 = tcg_temp_new_i64();
4888     tcg_tmp = tcg_temp_new_i64();
4889 
4890     if (op_id < 0x42) {
4891         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
4892         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
4893     } else {
4894         if (is_signed) {
4895             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
4896             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
4897         } else {
4898             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
4899             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
4900         }
4901     }
4902 
4903     if (ra == 31 && !is_sub) {
4904         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
4905         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
4906     } else {
4907         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
4908         if (is_sub) {
4909             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4910         } else {
4911             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4912         }
4913     }
4914 
4915     if (!sf) {
4916         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
4917     }
4918 }
4919 
4920 /* Add/subtract (with carry)
4921  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15       10  9    5 4   0
4922  * +--+--+--+------------------------+------+-------------+------+-----+
4923  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | 0 0 0 0 0 0 |  Rn  |  Rd |
4924  * +--+--+--+------------------------+------+-------------+------+-----+
4925  */
4926 
4927 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
4928 {
4929     unsigned int sf, op, setflags, rm, rn, rd;
4930     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
4931 
4932     sf = extract32(insn, 31, 1);
4933     op = extract32(insn, 30, 1);
4934     setflags = extract32(insn, 29, 1);
4935     rm = extract32(insn, 16, 5);
4936     rn = extract32(insn, 5, 5);
4937     rd = extract32(insn, 0, 5);
4938 
4939     tcg_rd = cpu_reg(s, rd);
4940     tcg_rn = cpu_reg(s, rn);
4941 
4942     if (op) {
4943         tcg_y = tcg_temp_new_i64();
4944         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
4945     } else {
4946         tcg_y = cpu_reg(s, rm);
4947     }
4948 
4949     if (setflags) {
4950         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
4951     } else {
4952         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
4953     }
4954 }
4955 
4956 /*
4957  * Rotate right into flags
4958  *  31 30 29                21       15          10      5  4      0
4959  * +--+--+--+-----------------+--------+-----------+------+--+------+
4960  * |sf|op| S| 1 1 0 1 0 0 0 0 |  imm6  | 0 0 0 0 1 |  Rn  |o2| mask |
4961  * +--+--+--+-----------------+--------+-----------+------+--+------+
4962  */
4963 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
4964 {
4965     int mask = extract32(insn, 0, 4);
4966     int o2 = extract32(insn, 4, 1);
4967     int rn = extract32(insn, 5, 5);
4968     int imm6 = extract32(insn, 15, 6);
4969     int sf_op_s = extract32(insn, 29, 3);
4970     TCGv_i64 tcg_rn;
4971     TCGv_i32 nzcv;
4972 
4973     if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
4974         unallocated_encoding(s);
4975         return;
4976     }
4977 
4978     tcg_rn = read_cpu_reg(s, rn, 1);
4979     tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
4980 
4981     nzcv = tcg_temp_new_i32();
4982     tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
4983 
4984     if (mask & 8) { /* N */
4985         tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
4986     }
4987     if (mask & 4) { /* Z */
4988         tcg_gen_not_i32(cpu_ZF, nzcv);
4989         tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
4990     }
4991     if (mask & 2) { /* C */
4992         tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
4993     }
4994     if (mask & 1) { /* V */
4995         tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
4996     }
4997 }
4998 
4999 /*
5000  * Evaluate into flags
5001  *  31 30 29                21        15   14        10      5  4      0
5002  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5003  * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 |  Rn  |o3| mask |
5004  * +--+--+--+-----------------+---------+----+---------+------+--+------+
5005  */
5006 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
5007 {
5008     int o3_mask = extract32(insn, 0, 5);
5009     int rn = extract32(insn, 5, 5);
5010     int o2 = extract32(insn, 15, 6);
5011     int sz = extract32(insn, 14, 1);
5012     int sf_op_s = extract32(insn, 29, 3);
5013     TCGv_i32 tmp;
5014     int shift;
5015 
5016     if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
5017         !dc_isar_feature(aa64_condm_4, s)) {
5018         unallocated_encoding(s);
5019         return;
5020     }
5021     shift = sz ? 16 : 24;  /* SETF16 or SETF8 */
5022 
5023     tmp = tcg_temp_new_i32();
5024     tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
5025     tcg_gen_shli_i32(cpu_NF, tmp, shift);
5026     tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
5027     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
5028     tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
5029 }
5030 
5031 /* Conditional compare (immediate / register)
5032  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
5033  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5034  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
5035  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5036  *        [1]                             y                [0]       [0]
5037  */
5038 static void disas_cc(DisasContext *s, uint32_t insn)
5039 {
5040     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
5041     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
5042     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
5043     DisasCompare c;
5044 
5045     if (!extract32(insn, 29, 1)) {
5046         unallocated_encoding(s);
5047         return;
5048     }
5049     if (insn & (1 << 10 | 1 << 4)) {
5050         unallocated_encoding(s);
5051         return;
5052     }
5053     sf = extract32(insn, 31, 1);
5054     op = extract32(insn, 30, 1);
5055     is_imm = extract32(insn, 11, 1);
5056     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
5057     cond = extract32(insn, 12, 4);
5058     rn = extract32(insn, 5, 5);
5059     nzcv = extract32(insn, 0, 4);
5060 
5061     /* Set T0 = !COND.  */
5062     tcg_t0 = tcg_temp_new_i32();
5063     arm_test_cc(&c, cond);
5064     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
5065 
5066     /* Load the arguments for the new comparison.  */
5067     if (is_imm) {
5068         tcg_y = tcg_temp_new_i64();
5069         tcg_gen_movi_i64(tcg_y, y);
5070     } else {
5071         tcg_y = cpu_reg(s, y);
5072     }
5073     tcg_rn = cpu_reg(s, rn);
5074 
5075     /* Set the flags for the new comparison.  */
5076     tcg_tmp = tcg_temp_new_i64();
5077     if (op) {
5078         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5079     } else {
5080         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5081     }
5082 
5083     /* If COND was false, force the flags to #nzcv.  Compute two masks
5084      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
5085      * For tcg hosts that support ANDC, we can make do with just T1.
5086      * In either case, allow the tcg optimizer to delete any unused mask.
5087      */
5088     tcg_t1 = tcg_temp_new_i32();
5089     tcg_t2 = tcg_temp_new_i32();
5090     tcg_gen_neg_i32(tcg_t1, tcg_t0);
5091     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
5092 
5093     if (nzcv & 8) { /* N */
5094         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
5095     } else {
5096         if (TCG_TARGET_HAS_andc_i32) {
5097             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
5098         } else {
5099             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
5100         }
5101     }
5102     if (nzcv & 4) { /* Z */
5103         if (TCG_TARGET_HAS_andc_i32) {
5104             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
5105         } else {
5106             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
5107         }
5108     } else {
5109         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
5110     }
5111     if (nzcv & 2) { /* C */
5112         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
5113     } else {
5114         if (TCG_TARGET_HAS_andc_i32) {
5115             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
5116         } else {
5117             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
5118         }
5119     }
5120     if (nzcv & 1) { /* V */
5121         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
5122     } else {
5123         if (TCG_TARGET_HAS_andc_i32) {
5124             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
5125         } else {
5126             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
5127         }
5128     }
5129 }
5130 
5131 /* Conditional select
5132  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
5133  * +----+----+---+-----------------+------+------+-----+------+------+
5134  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
5135  * +----+----+---+-----------------+------+------+-----+------+------+
5136  */
5137 static void disas_cond_select(DisasContext *s, uint32_t insn)
5138 {
5139     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
5140     TCGv_i64 tcg_rd, zero;
5141     DisasCompare64 c;
5142 
5143     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
5144         /* S == 1 or op2<1> == 1 */
5145         unallocated_encoding(s);
5146         return;
5147     }
5148     sf = extract32(insn, 31, 1);
5149     else_inv = extract32(insn, 30, 1);
5150     rm = extract32(insn, 16, 5);
5151     cond = extract32(insn, 12, 4);
5152     else_inc = extract32(insn, 10, 1);
5153     rn = extract32(insn, 5, 5);
5154     rd = extract32(insn, 0, 5);
5155 
5156     tcg_rd = cpu_reg(s, rd);
5157 
5158     a64_test_cc(&c, cond);
5159     zero = tcg_constant_i64(0);
5160 
5161     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
5162         /* CSET & CSETM.  */
5163         tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
5164         if (else_inv) {
5165             tcg_gen_neg_i64(tcg_rd, tcg_rd);
5166         }
5167     } else {
5168         TCGv_i64 t_true = cpu_reg(s, rn);
5169         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
5170         if (else_inv && else_inc) {
5171             tcg_gen_neg_i64(t_false, t_false);
5172         } else if (else_inv) {
5173             tcg_gen_not_i64(t_false, t_false);
5174         } else if (else_inc) {
5175             tcg_gen_addi_i64(t_false, t_false, 1);
5176         }
5177         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
5178     }
5179 
5180     if (!sf) {
5181         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5182     }
5183 }
5184 
5185 static void handle_clz(DisasContext *s, unsigned int sf,
5186                        unsigned int rn, unsigned int rd)
5187 {
5188     TCGv_i64 tcg_rd, tcg_rn;
5189     tcg_rd = cpu_reg(s, rd);
5190     tcg_rn = cpu_reg(s, rn);
5191 
5192     if (sf) {
5193         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
5194     } else {
5195         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5196         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5197         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
5198         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5199     }
5200 }
5201 
5202 static void handle_cls(DisasContext *s, unsigned int sf,
5203                        unsigned int rn, unsigned int rd)
5204 {
5205     TCGv_i64 tcg_rd, tcg_rn;
5206     tcg_rd = cpu_reg(s, rd);
5207     tcg_rn = cpu_reg(s, rn);
5208 
5209     if (sf) {
5210         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
5211     } else {
5212         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5213         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5214         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
5215         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5216     }
5217 }
5218 
5219 static void handle_rbit(DisasContext *s, unsigned int sf,
5220                         unsigned int rn, unsigned int rd)
5221 {
5222     TCGv_i64 tcg_rd, tcg_rn;
5223     tcg_rd = cpu_reg(s, rd);
5224     tcg_rn = cpu_reg(s, rn);
5225 
5226     if (sf) {
5227         gen_helper_rbit64(tcg_rd, tcg_rn);
5228     } else {
5229         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5230         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5231         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5232         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5233     }
5234 }
5235 
5236 /* REV with sf==1, opcode==3 ("REV64") */
5237 static void handle_rev64(DisasContext *s, unsigned int sf,
5238                          unsigned int rn, unsigned int rd)
5239 {
5240     if (!sf) {
5241         unallocated_encoding(s);
5242         return;
5243     }
5244     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5245 }
5246 
5247 /* REV with sf==0, opcode==2
5248  * REV32 (sf==1, opcode==2)
5249  */
5250 static void handle_rev32(DisasContext *s, unsigned int sf,
5251                          unsigned int rn, unsigned int rd)
5252 {
5253     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5254     TCGv_i64 tcg_rn = cpu_reg(s, rn);
5255 
5256     if (sf) {
5257         tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
5258         tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
5259     } else {
5260         tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
5261     }
5262 }
5263 
5264 /* REV16 (opcode==1) */
5265 static void handle_rev16(DisasContext *s, unsigned int sf,
5266                          unsigned int rn, unsigned int rd)
5267 {
5268     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5269     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5270     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5271     TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5272 
5273     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5274     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5275     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5276     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5277     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5278 }
5279 
5280 /* Data-processing (1 source)
5281  *   31  30  29  28             21 20     16 15    10 9    5 4    0
5282  * +----+---+---+-----------------+---------+--------+------+------+
5283  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
5284  * +----+---+---+-----------------+---------+--------+------+------+
5285  */
5286 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5287 {
5288     unsigned int sf, opcode, opcode2, rn, rd;
5289     TCGv_i64 tcg_rd;
5290 
5291     if (extract32(insn, 29, 1)) {
5292         unallocated_encoding(s);
5293         return;
5294     }
5295 
5296     sf = extract32(insn, 31, 1);
5297     opcode = extract32(insn, 10, 6);
5298     opcode2 = extract32(insn, 16, 5);
5299     rn = extract32(insn, 5, 5);
5300     rd = extract32(insn, 0, 5);
5301 
5302 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5303 
5304     switch (MAP(sf, opcode2, opcode)) {
5305     case MAP(0, 0x00, 0x00): /* RBIT */
5306     case MAP(1, 0x00, 0x00):
5307         handle_rbit(s, sf, rn, rd);
5308         break;
5309     case MAP(0, 0x00, 0x01): /* REV16 */
5310     case MAP(1, 0x00, 0x01):
5311         handle_rev16(s, sf, rn, rd);
5312         break;
5313     case MAP(0, 0x00, 0x02): /* REV/REV32 */
5314     case MAP(1, 0x00, 0x02):
5315         handle_rev32(s, sf, rn, rd);
5316         break;
5317     case MAP(1, 0x00, 0x03): /* REV64 */
5318         handle_rev64(s, sf, rn, rd);
5319         break;
5320     case MAP(0, 0x00, 0x04): /* CLZ */
5321     case MAP(1, 0x00, 0x04):
5322         handle_clz(s, sf, rn, rd);
5323         break;
5324     case MAP(0, 0x00, 0x05): /* CLS */
5325     case MAP(1, 0x00, 0x05):
5326         handle_cls(s, sf, rn, rd);
5327         break;
5328     case MAP(1, 0x01, 0x00): /* PACIA */
5329         if (s->pauth_active) {
5330             tcg_rd = cpu_reg(s, rd);
5331             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5332         } else if (!dc_isar_feature(aa64_pauth, s)) {
5333             goto do_unallocated;
5334         }
5335         break;
5336     case MAP(1, 0x01, 0x01): /* PACIB */
5337         if (s->pauth_active) {
5338             tcg_rd = cpu_reg(s, rd);
5339             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5340         } else if (!dc_isar_feature(aa64_pauth, s)) {
5341             goto do_unallocated;
5342         }
5343         break;
5344     case MAP(1, 0x01, 0x02): /* PACDA */
5345         if (s->pauth_active) {
5346             tcg_rd = cpu_reg(s, rd);
5347             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5348         } else if (!dc_isar_feature(aa64_pauth, s)) {
5349             goto do_unallocated;
5350         }
5351         break;
5352     case MAP(1, 0x01, 0x03): /* PACDB */
5353         if (s->pauth_active) {
5354             tcg_rd = cpu_reg(s, rd);
5355             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5356         } else if (!dc_isar_feature(aa64_pauth, s)) {
5357             goto do_unallocated;
5358         }
5359         break;
5360     case MAP(1, 0x01, 0x04): /* AUTIA */
5361         if (s->pauth_active) {
5362             tcg_rd = cpu_reg(s, rd);
5363             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5364         } else if (!dc_isar_feature(aa64_pauth, s)) {
5365             goto do_unallocated;
5366         }
5367         break;
5368     case MAP(1, 0x01, 0x05): /* AUTIB */
5369         if (s->pauth_active) {
5370             tcg_rd = cpu_reg(s, rd);
5371             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5372         } else if (!dc_isar_feature(aa64_pauth, s)) {
5373             goto do_unallocated;
5374         }
5375         break;
5376     case MAP(1, 0x01, 0x06): /* AUTDA */
5377         if (s->pauth_active) {
5378             tcg_rd = cpu_reg(s, rd);
5379             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5380         } else if (!dc_isar_feature(aa64_pauth, s)) {
5381             goto do_unallocated;
5382         }
5383         break;
5384     case MAP(1, 0x01, 0x07): /* AUTDB */
5385         if (s->pauth_active) {
5386             tcg_rd = cpu_reg(s, rd);
5387             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5388         } else if (!dc_isar_feature(aa64_pauth, s)) {
5389             goto do_unallocated;
5390         }
5391         break;
5392     case MAP(1, 0x01, 0x08): /* PACIZA */
5393         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5394             goto do_unallocated;
5395         } else if (s->pauth_active) {
5396             tcg_rd = cpu_reg(s, rd);
5397             gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5398         }
5399         break;
5400     case MAP(1, 0x01, 0x09): /* PACIZB */
5401         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5402             goto do_unallocated;
5403         } else if (s->pauth_active) {
5404             tcg_rd = cpu_reg(s, rd);
5405             gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5406         }
5407         break;
5408     case MAP(1, 0x01, 0x0a): /* PACDZA */
5409         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5410             goto do_unallocated;
5411         } else if (s->pauth_active) {
5412             tcg_rd = cpu_reg(s, rd);
5413             gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5414         }
5415         break;
5416     case MAP(1, 0x01, 0x0b): /* PACDZB */
5417         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5418             goto do_unallocated;
5419         } else if (s->pauth_active) {
5420             tcg_rd = cpu_reg(s, rd);
5421             gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5422         }
5423         break;
5424     case MAP(1, 0x01, 0x0c): /* AUTIZA */
5425         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5426             goto do_unallocated;
5427         } else if (s->pauth_active) {
5428             tcg_rd = cpu_reg(s, rd);
5429             gen_helper_autia(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5430         }
5431         break;
5432     case MAP(1, 0x01, 0x0d): /* AUTIZB */
5433         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5434             goto do_unallocated;
5435         } else if (s->pauth_active) {
5436             tcg_rd = cpu_reg(s, rd);
5437             gen_helper_autib(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5438         }
5439         break;
5440     case MAP(1, 0x01, 0x0e): /* AUTDZA */
5441         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5442             goto do_unallocated;
5443         } else if (s->pauth_active) {
5444             tcg_rd = cpu_reg(s, rd);
5445             gen_helper_autda(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5446         }
5447         break;
5448     case MAP(1, 0x01, 0x0f): /* AUTDZB */
5449         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5450             goto do_unallocated;
5451         } else if (s->pauth_active) {
5452             tcg_rd = cpu_reg(s, rd);
5453             gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, tcg_constant_i64(0));
5454         }
5455         break;
5456     case MAP(1, 0x01, 0x10): /* XPACI */
5457         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5458             goto do_unallocated;
5459         } else if (s->pauth_active) {
5460             tcg_rd = cpu_reg(s, rd);
5461             gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
5462         }
5463         break;
5464     case MAP(1, 0x01, 0x11): /* XPACD */
5465         if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5466             goto do_unallocated;
5467         } else if (s->pauth_active) {
5468             tcg_rd = cpu_reg(s, rd);
5469             gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5470         }
5471         break;
5472     default:
5473     do_unallocated:
5474         unallocated_encoding(s);
5475         break;
5476     }
5477 
5478 #undef MAP
5479 }
5480 
5481 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5482                        unsigned int rm, unsigned int rn, unsigned int rd)
5483 {
5484     TCGv_i64 tcg_n, tcg_m, tcg_rd;
5485     tcg_rd = cpu_reg(s, rd);
5486 
5487     if (!sf && is_signed) {
5488         tcg_n = tcg_temp_new_i64();
5489         tcg_m = tcg_temp_new_i64();
5490         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5491         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5492     } else {
5493         tcg_n = read_cpu_reg(s, rn, sf);
5494         tcg_m = read_cpu_reg(s, rm, sf);
5495     }
5496 
5497     if (is_signed) {
5498         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5499     } else {
5500         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5501     }
5502 
5503     if (!sf) { /* zero extend final result */
5504         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5505     }
5506 }
5507 
5508 /* LSLV, LSRV, ASRV, RORV */
5509 static void handle_shift_reg(DisasContext *s,
5510                              enum a64_shift_type shift_type, unsigned int sf,
5511                              unsigned int rm, unsigned int rn, unsigned int rd)
5512 {
5513     TCGv_i64 tcg_shift = tcg_temp_new_i64();
5514     TCGv_i64 tcg_rd = cpu_reg(s, rd);
5515     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5516 
5517     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5518     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5519 }
5520 
5521 /* CRC32[BHWX], CRC32C[BHWX] */
5522 static void handle_crc32(DisasContext *s,
5523                          unsigned int sf, unsigned int sz, bool crc32c,
5524                          unsigned int rm, unsigned int rn, unsigned int rd)
5525 {
5526     TCGv_i64 tcg_acc, tcg_val;
5527     TCGv_i32 tcg_bytes;
5528 
5529     if (!dc_isar_feature(aa64_crc32, s)
5530         || (sf == 1 && sz != 3)
5531         || (sf == 0 && sz == 3)) {
5532         unallocated_encoding(s);
5533         return;
5534     }
5535 
5536     if (sz == 3) {
5537         tcg_val = cpu_reg(s, rm);
5538     } else {
5539         uint64_t mask;
5540         switch (sz) {
5541         case 0:
5542             mask = 0xFF;
5543             break;
5544         case 1:
5545             mask = 0xFFFF;
5546             break;
5547         case 2:
5548             mask = 0xFFFFFFFF;
5549             break;
5550         default:
5551             g_assert_not_reached();
5552         }
5553         tcg_val = tcg_temp_new_i64();
5554         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5555     }
5556 
5557     tcg_acc = cpu_reg(s, rn);
5558     tcg_bytes = tcg_constant_i32(1 << sz);
5559 
5560     if (crc32c) {
5561         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5562     } else {
5563         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5564     }
5565 }
5566 
5567 /* Data-processing (2 source)
5568  *   31   30  29 28             21 20  16 15    10 9    5 4    0
5569  * +----+---+---+-----------------+------+--------+------+------+
5570  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
5571  * +----+---+---+-----------------+------+--------+------+------+
5572  */
5573 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5574 {
5575     unsigned int sf, rm, opcode, rn, rd, setflag;
5576     sf = extract32(insn, 31, 1);
5577     setflag = extract32(insn, 29, 1);
5578     rm = extract32(insn, 16, 5);
5579     opcode = extract32(insn, 10, 6);
5580     rn = extract32(insn, 5, 5);
5581     rd = extract32(insn, 0, 5);
5582 
5583     if (setflag && opcode != 0) {
5584         unallocated_encoding(s);
5585         return;
5586     }
5587 
5588     switch (opcode) {
5589     case 0: /* SUBP(S) */
5590         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5591             goto do_unallocated;
5592         } else {
5593             TCGv_i64 tcg_n, tcg_m, tcg_d;
5594 
5595             tcg_n = read_cpu_reg_sp(s, rn, true);
5596             tcg_m = read_cpu_reg_sp(s, rm, true);
5597             tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5598             tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5599             tcg_d = cpu_reg(s, rd);
5600 
5601             if (setflag) {
5602                 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5603             } else {
5604                 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5605             }
5606         }
5607         break;
5608     case 2: /* UDIV */
5609         handle_div(s, false, sf, rm, rn, rd);
5610         break;
5611     case 3: /* SDIV */
5612         handle_div(s, true, sf, rm, rn, rd);
5613         break;
5614     case 4: /* IRG */
5615         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5616             goto do_unallocated;
5617         }
5618         if (s->ata) {
5619             gen_helper_irg(cpu_reg_sp(s, rd), cpu_env,
5620                            cpu_reg_sp(s, rn), cpu_reg(s, rm));
5621         } else {
5622             gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5623                                              cpu_reg_sp(s, rn));
5624         }
5625         break;
5626     case 5: /* GMI */
5627         if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5628             goto do_unallocated;
5629         } else {
5630             TCGv_i64 t = tcg_temp_new_i64();
5631 
5632             tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
5633             tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
5634             tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
5635         }
5636         break;
5637     case 8: /* LSLV */
5638         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5639         break;
5640     case 9: /* LSRV */
5641         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5642         break;
5643     case 10: /* ASRV */
5644         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5645         break;
5646     case 11: /* RORV */
5647         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5648         break;
5649     case 12: /* PACGA */
5650         if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5651             goto do_unallocated;
5652         }
5653         gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5654                          cpu_reg(s, rn), cpu_reg_sp(s, rm));
5655         break;
5656     case 16:
5657     case 17:
5658     case 18:
5659     case 19:
5660     case 20:
5661     case 21:
5662     case 22:
5663     case 23: /* CRC32 */
5664     {
5665         int sz = extract32(opcode, 0, 2);
5666         bool crc32c = extract32(opcode, 2, 1);
5667         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5668         break;
5669     }
5670     default:
5671     do_unallocated:
5672         unallocated_encoding(s);
5673         break;
5674     }
5675 }
5676 
5677 /*
5678  * Data processing - register
5679  *  31  30 29  28      25    21  20  16      10         0
5680  * +--+---+--+---+-------+-----+-------+-------+---------+
5681  * |  |op0|  |op1| 1 0 1 | op2 |       |  op3  |         |
5682  * +--+---+--+---+-------+-----+-------+-------+---------+
5683  */
5684 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5685 {
5686     int op0 = extract32(insn, 30, 1);
5687     int op1 = extract32(insn, 28, 1);
5688     int op2 = extract32(insn, 21, 4);
5689     int op3 = extract32(insn, 10, 6);
5690 
5691     if (!op1) {
5692         if (op2 & 8) {
5693             if (op2 & 1) {
5694                 /* Add/sub (extended register) */
5695                 disas_add_sub_ext_reg(s, insn);
5696             } else {
5697                 /* Add/sub (shifted register) */
5698                 disas_add_sub_reg(s, insn);
5699             }
5700         } else {
5701             /* Logical (shifted register) */
5702             disas_logic_reg(s, insn);
5703         }
5704         return;
5705     }
5706 
5707     switch (op2) {
5708     case 0x0:
5709         switch (op3) {
5710         case 0x00: /* Add/subtract (with carry) */
5711             disas_adc_sbc(s, insn);
5712             break;
5713 
5714         case 0x01: /* Rotate right into flags */
5715         case 0x21:
5716             disas_rotate_right_into_flags(s, insn);
5717             break;
5718 
5719         case 0x02: /* Evaluate into flags */
5720         case 0x12:
5721         case 0x22:
5722         case 0x32:
5723             disas_evaluate_into_flags(s, insn);
5724             break;
5725 
5726         default:
5727             goto do_unallocated;
5728         }
5729         break;
5730 
5731     case 0x2: /* Conditional compare */
5732         disas_cc(s, insn); /* both imm and reg forms */
5733         break;
5734 
5735     case 0x4: /* Conditional select */
5736         disas_cond_select(s, insn);
5737         break;
5738 
5739     case 0x6: /* Data-processing */
5740         if (op0) {    /* (1 source) */
5741             disas_data_proc_1src(s, insn);
5742         } else {      /* (2 source) */
5743             disas_data_proc_2src(s, insn);
5744         }
5745         break;
5746     case 0x8 ... 0xf: /* (3 source) */
5747         disas_data_proc_3src(s, insn);
5748         break;
5749 
5750     default:
5751     do_unallocated:
5752         unallocated_encoding(s);
5753         break;
5754     }
5755 }
5756 
5757 static void handle_fp_compare(DisasContext *s, int size,
5758                               unsigned int rn, unsigned int rm,
5759                               bool cmp_with_zero, bool signal_all_nans)
5760 {
5761     TCGv_i64 tcg_flags = tcg_temp_new_i64();
5762     TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
5763 
5764     if (size == MO_64) {
5765         TCGv_i64 tcg_vn, tcg_vm;
5766 
5767         tcg_vn = read_fp_dreg(s, rn);
5768         if (cmp_with_zero) {
5769             tcg_vm = tcg_constant_i64(0);
5770         } else {
5771             tcg_vm = read_fp_dreg(s, rm);
5772         }
5773         if (signal_all_nans) {
5774             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5775         } else {
5776             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5777         }
5778     } else {
5779         TCGv_i32 tcg_vn = tcg_temp_new_i32();
5780         TCGv_i32 tcg_vm = tcg_temp_new_i32();
5781 
5782         read_vec_element_i32(s, tcg_vn, rn, 0, size);
5783         if (cmp_with_zero) {
5784             tcg_gen_movi_i32(tcg_vm, 0);
5785         } else {
5786             read_vec_element_i32(s, tcg_vm, rm, 0, size);
5787         }
5788 
5789         switch (size) {
5790         case MO_32:
5791             if (signal_all_nans) {
5792                 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5793             } else {
5794                 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5795             }
5796             break;
5797         case MO_16:
5798             if (signal_all_nans) {
5799                 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5800             } else {
5801                 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5802             }
5803             break;
5804         default:
5805             g_assert_not_reached();
5806         }
5807     }
5808 
5809     gen_set_nzcv(tcg_flags);
5810 }
5811 
5812 /* Floating point compare
5813  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
5814  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5815  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
5816  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5817  */
5818 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5819 {
5820     unsigned int mos, type, rm, op, rn, opc, op2r;
5821     int size;
5822 
5823     mos = extract32(insn, 29, 3);
5824     type = extract32(insn, 22, 2);
5825     rm = extract32(insn, 16, 5);
5826     op = extract32(insn, 14, 2);
5827     rn = extract32(insn, 5, 5);
5828     opc = extract32(insn, 3, 2);
5829     op2r = extract32(insn, 0, 3);
5830 
5831     if (mos || op || op2r) {
5832         unallocated_encoding(s);
5833         return;
5834     }
5835 
5836     switch (type) {
5837     case 0:
5838         size = MO_32;
5839         break;
5840     case 1:
5841         size = MO_64;
5842         break;
5843     case 3:
5844         size = MO_16;
5845         if (dc_isar_feature(aa64_fp16, s)) {
5846             break;
5847         }
5848         /* fallthru */
5849     default:
5850         unallocated_encoding(s);
5851         return;
5852     }
5853 
5854     if (!fp_access_check(s)) {
5855         return;
5856     }
5857 
5858     handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
5859 }
5860 
5861 /* Floating point conditional compare
5862  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
5863  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5864  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
5865  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5866  */
5867 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
5868 {
5869     unsigned int mos, type, rm, cond, rn, op, nzcv;
5870     TCGLabel *label_continue = NULL;
5871     int size;
5872 
5873     mos = extract32(insn, 29, 3);
5874     type = extract32(insn, 22, 2);
5875     rm = extract32(insn, 16, 5);
5876     cond = extract32(insn, 12, 4);
5877     rn = extract32(insn, 5, 5);
5878     op = extract32(insn, 4, 1);
5879     nzcv = extract32(insn, 0, 4);
5880 
5881     if (mos) {
5882         unallocated_encoding(s);
5883         return;
5884     }
5885 
5886     switch (type) {
5887     case 0:
5888         size = MO_32;
5889         break;
5890     case 1:
5891         size = MO_64;
5892         break;
5893     case 3:
5894         size = MO_16;
5895         if (dc_isar_feature(aa64_fp16, s)) {
5896             break;
5897         }
5898         /* fallthru */
5899     default:
5900         unallocated_encoding(s);
5901         return;
5902     }
5903 
5904     if (!fp_access_check(s)) {
5905         return;
5906     }
5907 
5908     if (cond < 0x0e) { /* not always */
5909         TCGLabel *label_match = gen_new_label();
5910         label_continue = gen_new_label();
5911         arm_gen_test_cc(cond, label_match);
5912         /* nomatch: */
5913         gen_set_nzcv(tcg_constant_i64(nzcv << 28));
5914         tcg_gen_br(label_continue);
5915         gen_set_label(label_match);
5916     }
5917 
5918     handle_fp_compare(s, size, rn, rm, false, op);
5919 
5920     if (cond < 0x0e) {
5921         gen_set_label(label_continue);
5922     }
5923 }
5924 
5925 /* Floating point conditional select
5926  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
5927  * +---+---+---+-----------+------+---+------+------+-----+------+------+
5928  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
5929  * +---+---+---+-----------+------+---+------+------+-----+------+------+
5930  */
5931 static void disas_fp_csel(DisasContext *s, uint32_t insn)
5932 {
5933     unsigned int mos, type, rm, cond, rn, rd;
5934     TCGv_i64 t_true, t_false;
5935     DisasCompare64 c;
5936     MemOp sz;
5937 
5938     mos = extract32(insn, 29, 3);
5939     type = extract32(insn, 22, 2);
5940     rm = extract32(insn, 16, 5);
5941     cond = extract32(insn, 12, 4);
5942     rn = extract32(insn, 5, 5);
5943     rd = extract32(insn, 0, 5);
5944 
5945     if (mos) {
5946         unallocated_encoding(s);
5947         return;
5948     }
5949 
5950     switch (type) {
5951     case 0:
5952         sz = MO_32;
5953         break;
5954     case 1:
5955         sz = MO_64;
5956         break;
5957     case 3:
5958         sz = MO_16;
5959         if (dc_isar_feature(aa64_fp16, s)) {
5960             break;
5961         }
5962         /* fallthru */
5963     default:
5964         unallocated_encoding(s);
5965         return;
5966     }
5967 
5968     if (!fp_access_check(s)) {
5969         return;
5970     }
5971 
5972     /* Zero extend sreg & hreg inputs to 64 bits now.  */
5973     t_true = tcg_temp_new_i64();
5974     t_false = tcg_temp_new_i64();
5975     read_vec_element(s, t_true, rn, 0, sz);
5976     read_vec_element(s, t_false, rm, 0, sz);
5977 
5978     a64_test_cc(&c, cond);
5979     tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
5980                         t_true, t_false);
5981 
5982     /* Note that sregs & hregs write back zeros to the high bits,
5983        and we've already done the zero-extension.  */
5984     write_fp_dreg(s, rd, t_true);
5985 }
5986 
5987 /* Floating-point data-processing (1 source) - half precision */
5988 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
5989 {
5990     TCGv_ptr fpst = NULL;
5991     TCGv_i32 tcg_op = read_fp_hreg(s, rn);
5992     TCGv_i32 tcg_res = tcg_temp_new_i32();
5993 
5994     switch (opcode) {
5995     case 0x0: /* FMOV */
5996         tcg_gen_mov_i32(tcg_res, tcg_op);
5997         break;
5998     case 0x1: /* FABS */
5999         tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
6000         break;
6001     case 0x2: /* FNEG */
6002         tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
6003         break;
6004     case 0x3: /* FSQRT */
6005         fpst = fpstatus_ptr(FPST_FPCR_F16);
6006         gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
6007         break;
6008     case 0x8: /* FRINTN */
6009     case 0x9: /* FRINTP */
6010     case 0xa: /* FRINTM */
6011     case 0xb: /* FRINTZ */
6012     case 0xc: /* FRINTA */
6013     {
6014         TCGv_i32 tcg_rmode;
6015 
6016         fpst = fpstatus_ptr(FPST_FPCR_F16);
6017         tcg_rmode = gen_set_rmode(opcode & 7, fpst);
6018         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6019         gen_restore_rmode(tcg_rmode, fpst);
6020         break;
6021     }
6022     case 0xe: /* FRINTX */
6023         fpst = fpstatus_ptr(FPST_FPCR_F16);
6024         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
6025         break;
6026     case 0xf: /* FRINTI */
6027         fpst = fpstatus_ptr(FPST_FPCR_F16);
6028         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6029         break;
6030     default:
6031         g_assert_not_reached();
6032     }
6033 
6034     write_fp_sreg(s, rd, tcg_res);
6035 }
6036 
6037 /* Floating-point data-processing (1 source) - single precision */
6038 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
6039 {
6040     void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
6041     TCGv_i32 tcg_op, tcg_res;
6042     TCGv_ptr fpst;
6043     int rmode = -1;
6044 
6045     tcg_op = read_fp_sreg(s, rn);
6046     tcg_res = tcg_temp_new_i32();
6047 
6048     switch (opcode) {
6049     case 0x0: /* FMOV */
6050         tcg_gen_mov_i32(tcg_res, tcg_op);
6051         goto done;
6052     case 0x1: /* FABS */
6053         gen_helper_vfp_abss(tcg_res, tcg_op);
6054         goto done;
6055     case 0x2: /* FNEG */
6056         gen_helper_vfp_negs(tcg_res, tcg_op);
6057         goto done;
6058     case 0x3: /* FSQRT */
6059         gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
6060         goto done;
6061     case 0x6: /* BFCVT */
6062         gen_fpst = gen_helper_bfcvt;
6063         break;
6064     case 0x8: /* FRINTN */
6065     case 0x9: /* FRINTP */
6066     case 0xa: /* FRINTM */
6067     case 0xb: /* FRINTZ */
6068     case 0xc: /* FRINTA */
6069         rmode = opcode & 7;
6070         gen_fpst = gen_helper_rints;
6071         break;
6072     case 0xe: /* FRINTX */
6073         gen_fpst = gen_helper_rints_exact;
6074         break;
6075     case 0xf: /* FRINTI */
6076         gen_fpst = gen_helper_rints;
6077         break;
6078     case 0x10: /* FRINT32Z */
6079         rmode = FPROUNDING_ZERO;
6080         gen_fpst = gen_helper_frint32_s;
6081         break;
6082     case 0x11: /* FRINT32X */
6083         gen_fpst = gen_helper_frint32_s;
6084         break;
6085     case 0x12: /* FRINT64Z */
6086         rmode = FPROUNDING_ZERO;
6087         gen_fpst = gen_helper_frint64_s;
6088         break;
6089     case 0x13: /* FRINT64X */
6090         gen_fpst = gen_helper_frint64_s;
6091         break;
6092     default:
6093         g_assert_not_reached();
6094     }
6095 
6096     fpst = fpstatus_ptr(FPST_FPCR);
6097     if (rmode >= 0) {
6098         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6099         gen_fpst(tcg_res, tcg_op, fpst);
6100         gen_restore_rmode(tcg_rmode, fpst);
6101     } else {
6102         gen_fpst(tcg_res, tcg_op, fpst);
6103     }
6104 
6105  done:
6106     write_fp_sreg(s, rd, tcg_res);
6107 }
6108 
6109 /* Floating-point data-processing (1 source) - double precision */
6110 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6111 {
6112     void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6113     TCGv_i64 tcg_op, tcg_res;
6114     TCGv_ptr fpst;
6115     int rmode = -1;
6116 
6117     switch (opcode) {
6118     case 0x0: /* FMOV */
6119         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6120         return;
6121     }
6122 
6123     tcg_op = read_fp_dreg(s, rn);
6124     tcg_res = tcg_temp_new_i64();
6125 
6126     switch (opcode) {
6127     case 0x1: /* FABS */
6128         gen_helper_vfp_absd(tcg_res, tcg_op);
6129         goto done;
6130     case 0x2: /* FNEG */
6131         gen_helper_vfp_negd(tcg_res, tcg_op);
6132         goto done;
6133     case 0x3: /* FSQRT */
6134         gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
6135         goto done;
6136     case 0x8: /* FRINTN */
6137     case 0x9: /* FRINTP */
6138     case 0xa: /* FRINTM */
6139     case 0xb: /* FRINTZ */
6140     case 0xc: /* FRINTA */
6141         rmode = opcode & 7;
6142         gen_fpst = gen_helper_rintd;
6143         break;
6144     case 0xe: /* FRINTX */
6145         gen_fpst = gen_helper_rintd_exact;
6146         break;
6147     case 0xf: /* FRINTI */
6148         gen_fpst = gen_helper_rintd;
6149         break;
6150     case 0x10: /* FRINT32Z */
6151         rmode = FPROUNDING_ZERO;
6152         gen_fpst = gen_helper_frint32_d;
6153         break;
6154     case 0x11: /* FRINT32X */
6155         gen_fpst = gen_helper_frint32_d;
6156         break;
6157     case 0x12: /* FRINT64Z */
6158         rmode = FPROUNDING_ZERO;
6159         gen_fpst = gen_helper_frint64_d;
6160         break;
6161     case 0x13: /* FRINT64X */
6162         gen_fpst = gen_helper_frint64_d;
6163         break;
6164     default:
6165         g_assert_not_reached();
6166     }
6167 
6168     fpst = fpstatus_ptr(FPST_FPCR);
6169     if (rmode >= 0) {
6170         TCGv_i32 tcg_rmode = gen_set_rmode(rmode, fpst);
6171         gen_fpst(tcg_res, tcg_op, fpst);
6172         gen_restore_rmode(tcg_rmode, fpst);
6173     } else {
6174         gen_fpst(tcg_res, tcg_op, fpst);
6175     }
6176 
6177  done:
6178     write_fp_dreg(s, rd, tcg_res);
6179 }
6180 
6181 static void handle_fp_fcvt(DisasContext *s, int opcode,
6182                            int rd, int rn, int dtype, int ntype)
6183 {
6184     switch (ntype) {
6185     case 0x0:
6186     {
6187         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6188         if (dtype == 1) {
6189             /* Single to double */
6190             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6191             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
6192             write_fp_dreg(s, rd, tcg_rd);
6193         } else {
6194             /* Single to half */
6195             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6196             TCGv_i32 ahp = get_ahp_flag();
6197             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6198 
6199             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6200             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6201             write_fp_sreg(s, rd, tcg_rd);
6202         }
6203         break;
6204     }
6205     case 0x1:
6206     {
6207         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6208         TCGv_i32 tcg_rd = tcg_temp_new_i32();
6209         if (dtype == 0) {
6210             /* Double to single */
6211             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
6212         } else {
6213             TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6214             TCGv_i32 ahp = get_ahp_flag();
6215             /* Double to half */
6216             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6217             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6218         }
6219         write_fp_sreg(s, rd, tcg_rd);
6220         break;
6221     }
6222     case 0x3:
6223     {
6224         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6225         TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6226         TCGv_i32 tcg_ahp = get_ahp_flag();
6227         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6228         if (dtype == 0) {
6229             /* Half to single */
6230             TCGv_i32 tcg_rd = tcg_temp_new_i32();
6231             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6232             write_fp_sreg(s, rd, tcg_rd);
6233         } else {
6234             /* Half to double */
6235             TCGv_i64 tcg_rd = tcg_temp_new_i64();
6236             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6237             write_fp_dreg(s, rd, tcg_rd);
6238         }
6239         break;
6240     }
6241     default:
6242         g_assert_not_reached();
6243     }
6244 }
6245 
6246 /* Floating point data-processing (1 source)
6247  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
6248  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6249  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
6250  * +---+---+---+-----------+------+---+--------+-----------+------+------+
6251  */
6252 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6253 {
6254     int mos = extract32(insn, 29, 3);
6255     int type = extract32(insn, 22, 2);
6256     int opcode = extract32(insn, 15, 6);
6257     int rn = extract32(insn, 5, 5);
6258     int rd = extract32(insn, 0, 5);
6259 
6260     if (mos) {
6261         goto do_unallocated;
6262     }
6263 
6264     switch (opcode) {
6265     case 0x4: case 0x5: case 0x7:
6266     {
6267         /* FCVT between half, single and double precision */
6268         int dtype = extract32(opcode, 0, 2);
6269         if (type == 2 || dtype == type) {
6270             goto do_unallocated;
6271         }
6272         if (!fp_access_check(s)) {
6273             return;
6274         }
6275 
6276         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6277         break;
6278     }
6279 
6280     case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6281         if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6282             goto do_unallocated;
6283         }
6284         /* fall through */
6285     case 0x0 ... 0x3:
6286     case 0x8 ... 0xc:
6287     case 0xe ... 0xf:
6288         /* 32-to-32 and 64-to-64 ops */
6289         switch (type) {
6290         case 0:
6291             if (!fp_access_check(s)) {
6292                 return;
6293             }
6294             handle_fp_1src_single(s, opcode, rd, rn);
6295             break;
6296         case 1:
6297             if (!fp_access_check(s)) {
6298                 return;
6299             }
6300             handle_fp_1src_double(s, opcode, rd, rn);
6301             break;
6302         case 3:
6303             if (!dc_isar_feature(aa64_fp16, s)) {
6304                 goto do_unallocated;
6305             }
6306 
6307             if (!fp_access_check(s)) {
6308                 return;
6309             }
6310             handle_fp_1src_half(s, opcode, rd, rn);
6311             break;
6312         default:
6313             goto do_unallocated;
6314         }
6315         break;
6316 
6317     case 0x6:
6318         switch (type) {
6319         case 1: /* BFCVT */
6320             if (!dc_isar_feature(aa64_bf16, s)) {
6321                 goto do_unallocated;
6322             }
6323             if (!fp_access_check(s)) {
6324                 return;
6325             }
6326             handle_fp_1src_single(s, opcode, rd, rn);
6327             break;
6328         default:
6329             goto do_unallocated;
6330         }
6331         break;
6332 
6333     default:
6334     do_unallocated:
6335         unallocated_encoding(s);
6336         break;
6337     }
6338 }
6339 
6340 /* Floating-point data-processing (2 source) - single precision */
6341 static void handle_fp_2src_single(DisasContext *s, int opcode,
6342                                   int rd, int rn, int rm)
6343 {
6344     TCGv_i32 tcg_op1;
6345     TCGv_i32 tcg_op2;
6346     TCGv_i32 tcg_res;
6347     TCGv_ptr fpst;
6348 
6349     tcg_res = tcg_temp_new_i32();
6350     fpst = fpstatus_ptr(FPST_FPCR);
6351     tcg_op1 = read_fp_sreg(s, rn);
6352     tcg_op2 = read_fp_sreg(s, rm);
6353 
6354     switch (opcode) {
6355     case 0x0: /* FMUL */
6356         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6357         break;
6358     case 0x1: /* FDIV */
6359         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6360         break;
6361     case 0x2: /* FADD */
6362         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6363         break;
6364     case 0x3: /* FSUB */
6365         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6366         break;
6367     case 0x4: /* FMAX */
6368         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6369         break;
6370     case 0x5: /* FMIN */
6371         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6372         break;
6373     case 0x6: /* FMAXNM */
6374         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6375         break;
6376     case 0x7: /* FMINNM */
6377         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6378         break;
6379     case 0x8: /* FNMUL */
6380         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6381         gen_helper_vfp_negs(tcg_res, tcg_res);
6382         break;
6383     }
6384 
6385     write_fp_sreg(s, rd, tcg_res);
6386 }
6387 
6388 /* Floating-point data-processing (2 source) - double precision */
6389 static void handle_fp_2src_double(DisasContext *s, int opcode,
6390                                   int rd, int rn, int rm)
6391 {
6392     TCGv_i64 tcg_op1;
6393     TCGv_i64 tcg_op2;
6394     TCGv_i64 tcg_res;
6395     TCGv_ptr fpst;
6396 
6397     tcg_res = tcg_temp_new_i64();
6398     fpst = fpstatus_ptr(FPST_FPCR);
6399     tcg_op1 = read_fp_dreg(s, rn);
6400     tcg_op2 = read_fp_dreg(s, rm);
6401 
6402     switch (opcode) {
6403     case 0x0: /* FMUL */
6404         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6405         break;
6406     case 0x1: /* FDIV */
6407         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6408         break;
6409     case 0x2: /* FADD */
6410         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6411         break;
6412     case 0x3: /* FSUB */
6413         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6414         break;
6415     case 0x4: /* FMAX */
6416         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6417         break;
6418     case 0x5: /* FMIN */
6419         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6420         break;
6421     case 0x6: /* FMAXNM */
6422         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6423         break;
6424     case 0x7: /* FMINNM */
6425         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6426         break;
6427     case 0x8: /* FNMUL */
6428         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6429         gen_helper_vfp_negd(tcg_res, tcg_res);
6430         break;
6431     }
6432 
6433     write_fp_dreg(s, rd, tcg_res);
6434 }
6435 
6436 /* Floating-point data-processing (2 source) - half precision */
6437 static void handle_fp_2src_half(DisasContext *s, int opcode,
6438                                 int rd, int rn, int rm)
6439 {
6440     TCGv_i32 tcg_op1;
6441     TCGv_i32 tcg_op2;
6442     TCGv_i32 tcg_res;
6443     TCGv_ptr fpst;
6444 
6445     tcg_res = tcg_temp_new_i32();
6446     fpst = fpstatus_ptr(FPST_FPCR_F16);
6447     tcg_op1 = read_fp_hreg(s, rn);
6448     tcg_op2 = read_fp_hreg(s, rm);
6449 
6450     switch (opcode) {
6451     case 0x0: /* FMUL */
6452         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6453         break;
6454     case 0x1: /* FDIV */
6455         gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6456         break;
6457     case 0x2: /* FADD */
6458         gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6459         break;
6460     case 0x3: /* FSUB */
6461         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6462         break;
6463     case 0x4: /* FMAX */
6464         gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6465         break;
6466     case 0x5: /* FMIN */
6467         gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6468         break;
6469     case 0x6: /* FMAXNM */
6470         gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6471         break;
6472     case 0x7: /* FMINNM */
6473         gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6474         break;
6475     case 0x8: /* FNMUL */
6476         gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6477         tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6478         break;
6479     default:
6480         g_assert_not_reached();
6481     }
6482 
6483     write_fp_sreg(s, rd, tcg_res);
6484 }
6485 
6486 /* Floating point data-processing (2 source)
6487  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
6488  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6489  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
6490  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6491  */
6492 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6493 {
6494     int mos = extract32(insn, 29, 3);
6495     int type = extract32(insn, 22, 2);
6496     int rd = extract32(insn, 0, 5);
6497     int rn = extract32(insn, 5, 5);
6498     int rm = extract32(insn, 16, 5);
6499     int opcode = extract32(insn, 12, 4);
6500 
6501     if (opcode > 8 || mos) {
6502         unallocated_encoding(s);
6503         return;
6504     }
6505 
6506     switch (type) {
6507     case 0:
6508         if (!fp_access_check(s)) {
6509             return;
6510         }
6511         handle_fp_2src_single(s, opcode, rd, rn, rm);
6512         break;
6513     case 1:
6514         if (!fp_access_check(s)) {
6515             return;
6516         }
6517         handle_fp_2src_double(s, opcode, rd, rn, rm);
6518         break;
6519     case 3:
6520         if (!dc_isar_feature(aa64_fp16, s)) {
6521             unallocated_encoding(s);
6522             return;
6523         }
6524         if (!fp_access_check(s)) {
6525             return;
6526         }
6527         handle_fp_2src_half(s, opcode, rd, rn, rm);
6528         break;
6529     default:
6530         unallocated_encoding(s);
6531     }
6532 }
6533 
6534 /* Floating-point data-processing (3 source) - single precision */
6535 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6536                                   int rd, int rn, int rm, int ra)
6537 {
6538     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6539     TCGv_i32 tcg_res = tcg_temp_new_i32();
6540     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6541 
6542     tcg_op1 = read_fp_sreg(s, rn);
6543     tcg_op2 = read_fp_sreg(s, rm);
6544     tcg_op3 = read_fp_sreg(s, ra);
6545 
6546     /* These are fused multiply-add, and must be done as one
6547      * floating point operation with no rounding between the
6548      * multiplication and addition steps.
6549      * NB that doing the negations here as separate steps is
6550      * correct : an input NaN should come out with its sign bit
6551      * flipped if it is a negated-input.
6552      */
6553     if (o1 == true) {
6554         gen_helper_vfp_negs(tcg_op3, tcg_op3);
6555     }
6556 
6557     if (o0 != o1) {
6558         gen_helper_vfp_negs(tcg_op1, tcg_op1);
6559     }
6560 
6561     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6562 
6563     write_fp_sreg(s, rd, tcg_res);
6564 }
6565 
6566 /* Floating-point data-processing (3 source) - double precision */
6567 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6568                                   int rd, int rn, int rm, int ra)
6569 {
6570     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6571     TCGv_i64 tcg_res = tcg_temp_new_i64();
6572     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6573 
6574     tcg_op1 = read_fp_dreg(s, rn);
6575     tcg_op2 = read_fp_dreg(s, rm);
6576     tcg_op3 = read_fp_dreg(s, ra);
6577 
6578     /* These are fused multiply-add, and must be done as one
6579      * floating point operation with no rounding between the
6580      * multiplication and addition steps.
6581      * NB that doing the negations here as separate steps is
6582      * correct : an input NaN should come out with its sign bit
6583      * flipped if it is a negated-input.
6584      */
6585     if (o1 == true) {
6586         gen_helper_vfp_negd(tcg_op3, tcg_op3);
6587     }
6588 
6589     if (o0 != o1) {
6590         gen_helper_vfp_negd(tcg_op1, tcg_op1);
6591     }
6592 
6593     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6594 
6595     write_fp_dreg(s, rd, tcg_res);
6596 }
6597 
6598 /* Floating-point data-processing (3 source) - half precision */
6599 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6600                                 int rd, int rn, int rm, int ra)
6601 {
6602     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6603     TCGv_i32 tcg_res = tcg_temp_new_i32();
6604     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6605 
6606     tcg_op1 = read_fp_hreg(s, rn);
6607     tcg_op2 = read_fp_hreg(s, rm);
6608     tcg_op3 = read_fp_hreg(s, ra);
6609 
6610     /* These are fused multiply-add, and must be done as one
6611      * floating point operation with no rounding between the
6612      * multiplication and addition steps.
6613      * NB that doing the negations here as separate steps is
6614      * correct : an input NaN should come out with its sign bit
6615      * flipped if it is a negated-input.
6616      */
6617     if (o1 == true) {
6618         tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6619     }
6620 
6621     if (o0 != o1) {
6622         tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6623     }
6624 
6625     gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6626 
6627     write_fp_sreg(s, rd, tcg_res);
6628 }
6629 
6630 /* Floating point data-processing (3 source)
6631  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
6632  * +---+---+---+-----------+------+----+------+----+------+------+------+
6633  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
6634  * +---+---+---+-----------+------+----+------+----+------+------+------+
6635  */
6636 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6637 {
6638     int mos = extract32(insn, 29, 3);
6639     int type = extract32(insn, 22, 2);
6640     int rd = extract32(insn, 0, 5);
6641     int rn = extract32(insn, 5, 5);
6642     int ra = extract32(insn, 10, 5);
6643     int rm = extract32(insn, 16, 5);
6644     bool o0 = extract32(insn, 15, 1);
6645     bool o1 = extract32(insn, 21, 1);
6646 
6647     if (mos) {
6648         unallocated_encoding(s);
6649         return;
6650     }
6651 
6652     switch (type) {
6653     case 0:
6654         if (!fp_access_check(s)) {
6655             return;
6656         }
6657         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6658         break;
6659     case 1:
6660         if (!fp_access_check(s)) {
6661             return;
6662         }
6663         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6664         break;
6665     case 3:
6666         if (!dc_isar_feature(aa64_fp16, s)) {
6667             unallocated_encoding(s);
6668             return;
6669         }
6670         if (!fp_access_check(s)) {
6671             return;
6672         }
6673         handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6674         break;
6675     default:
6676         unallocated_encoding(s);
6677     }
6678 }
6679 
6680 /* Floating point immediate
6681  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
6682  * +---+---+---+-----------+------+---+------------+-------+------+------+
6683  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
6684  * +---+---+---+-----------+------+---+------------+-------+------+------+
6685  */
6686 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6687 {
6688     int rd = extract32(insn, 0, 5);
6689     int imm5 = extract32(insn, 5, 5);
6690     int imm8 = extract32(insn, 13, 8);
6691     int type = extract32(insn, 22, 2);
6692     int mos = extract32(insn, 29, 3);
6693     uint64_t imm;
6694     MemOp sz;
6695 
6696     if (mos || imm5) {
6697         unallocated_encoding(s);
6698         return;
6699     }
6700 
6701     switch (type) {
6702     case 0:
6703         sz = MO_32;
6704         break;
6705     case 1:
6706         sz = MO_64;
6707         break;
6708     case 3:
6709         sz = MO_16;
6710         if (dc_isar_feature(aa64_fp16, s)) {
6711             break;
6712         }
6713         /* fallthru */
6714     default:
6715         unallocated_encoding(s);
6716         return;
6717     }
6718 
6719     if (!fp_access_check(s)) {
6720         return;
6721     }
6722 
6723     imm = vfp_expand_imm(sz, imm8);
6724     write_fp_dreg(s, rd, tcg_constant_i64(imm));
6725 }
6726 
6727 /* Handle floating point <=> fixed point conversions. Note that we can
6728  * also deal with fp <=> integer conversions as a special case (scale == 64)
6729  * OPTME: consider handling that special case specially or at least skipping
6730  * the call to scalbn in the helpers for zero shifts.
6731  */
6732 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6733                            bool itof, int rmode, int scale, int sf, int type)
6734 {
6735     bool is_signed = !(opcode & 1);
6736     TCGv_ptr tcg_fpstatus;
6737     TCGv_i32 tcg_shift, tcg_single;
6738     TCGv_i64 tcg_double;
6739 
6740     tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
6741 
6742     tcg_shift = tcg_constant_i32(64 - scale);
6743 
6744     if (itof) {
6745         TCGv_i64 tcg_int = cpu_reg(s, rn);
6746         if (!sf) {
6747             TCGv_i64 tcg_extend = tcg_temp_new_i64();
6748 
6749             if (is_signed) {
6750                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6751             } else {
6752                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6753             }
6754 
6755             tcg_int = tcg_extend;
6756         }
6757 
6758         switch (type) {
6759         case 1: /* float64 */
6760             tcg_double = tcg_temp_new_i64();
6761             if (is_signed) {
6762                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6763                                      tcg_shift, tcg_fpstatus);
6764             } else {
6765                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6766                                      tcg_shift, tcg_fpstatus);
6767             }
6768             write_fp_dreg(s, rd, tcg_double);
6769             break;
6770 
6771         case 0: /* float32 */
6772             tcg_single = tcg_temp_new_i32();
6773             if (is_signed) {
6774                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6775                                      tcg_shift, tcg_fpstatus);
6776             } else {
6777                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6778                                      tcg_shift, tcg_fpstatus);
6779             }
6780             write_fp_sreg(s, rd, tcg_single);
6781             break;
6782 
6783         case 3: /* float16 */
6784             tcg_single = tcg_temp_new_i32();
6785             if (is_signed) {
6786                 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6787                                      tcg_shift, tcg_fpstatus);
6788             } else {
6789                 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6790                                      tcg_shift, tcg_fpstatus);
6791             }
6792             write_fp_sreg(s, rd, tcg_single);
6793             break;
6794 
6795         default:
6796             g_assert_not_reached();
6797         }
6798     } else {
6799         TCGv_i64 tcg_int = cpu_reg(s, rd);
6800         TCGv_i32 tcg_rmode;
6801 
6802         if (extract32(opcode, 2, 1)) {
6803             /* There are too many rounding modes to all fit into rmode,
6804              * so FCVTA[US] is a special case.
6805              */
6806             rmode = FPROUNDING_TIEAWAY;
6807         }
6808 
6809         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
6810 
6811         switch (type) {
6812         case 1: /* float64 */
6813             tcg_double = read_fp_dreg(s, rn);
6814             if (is_signed) {
6815                 if (!sf) {
6816                     gen_helper_vfp_tosld(tcg_int, tcg_double,
6817                                          tcg_shift, tcg_fpstatus);
6818                 } else {
6819                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
6820                                          tcg_shift, tcg_fpstatus);
6821                 }
6822             } else {
6823                 if (!sf) {
6824                     gen_helper_vfp_tould(tcg_int, tcg_double,
6825                                          tcg_shift, tcg_fpstatus);
6826                 } else {
6827                     gen_helper_vfp_touqd(tcg_int, tcg_double,
6828                                          tcg_shift, tcg_fpstatus);
6829                 }
6830             }
6831             if (!sf) {
6832                 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6833             }
6834             break;
6835 
6836         case 0: /* float32 */
6837             tcg_single = read_fp_sreg(s, rn);
6838             if (sf) {
6839                 if (is_signed) {
6840                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
6841                                          tcg_shift, tcg_fpstatus);
6842                 } else {
6843                     gen_helper_vfp_touqs(tcg_int, tcg_single,
6844                                          tcg_shift, tcg_fpstatus);
6845                 }
6846             } else {
6847                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6848                 if (is_signed) {
6849                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
6850                                          tcg_shift, tcg_fpstatus);
6851                 } else {
6852                     gen_helper_vfp_touls(tcg_dest, tcg_single,
6853                                          tcg_shift, tcg_fpstatus);
6854                 }
6855                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6856             }
6857             break;
6858 
6859         case 3: /* float16 */
6860             tcg_single = read_fp_sreg(s, rn);
6861             if (sf) {
6862                 if (is_signed) {
6863                     gen_helper_vfp_tosqh(tcg_int, tcg_single,
6864                                          tcg_shift, tcg_fpstatus);
6865                 } else {
6866                     gen_helper_vfp_touqh(tcg_int, tcg_single,
6867                                          tcg_shift, tcg_fpstatus);
6868                 }
6869             } else {
6870                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6871                 if (is_signed) {
6872                     gen_helper_vfp_toslh(tcg_dest, tcg_single,
6873                                          tcg_shift, tcg_fpstatus);
6874                 } else {
6875                     gen_helper_vfp_toulh(tcg_dest, tcg_single,
6876                                          tcg_shift, tcg_fpstatus);
6877                 }
6878                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6879             }
6880             break;
6881 
6882         default:
6883             g_assert_not_reached();
6884         }
6885 
6886         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
6887     }
6888 }
6889 
6890 /* Floating point <-> fixed point conversions
6891  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
6892  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6893  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
6894  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6895  */
6896 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
6897 {
6898     int rd = extract32(insn, 0, 5);
6899     int rn = extract32(insn, 5, 5);
6900     int scale = extract32(insn, 10, 6);
6901     int opcode = extract32(insn, 16, 3);
6902     int rmode = extract32(insn, 19, 2);
6903     int type = extract32(insn, 22, 2);
6904     bool sbit = extract32(insn, 29, 1);
6905     bool sf = extract32(insn, 31, 1);
6906     bool itof;
6907 
6908     if (sbit || (!sf && scale < 32)) {
6909         unallocated_encoding(s);
6910         return;
6911     }
6912 
6913     switch (type) {
6914     case 0: /* float32 */
6915     case 1: /* float64 */
6916         break;
6917     case 3: /* float16 */
6918         if (dc_isar_feature(aa64_fp16, s)) {
6919             break;
6920         }
6921         /* fallthru */
6922     default:
6923         unallocated_encoding(s);
6924         return;
6925     }
6926 
6927     switch ((rmode << 3) | opcode) {
6928     case 0x2: /* SCVTF */
6929     case 0x3: /* UCVTF */
6930         itof = true;
6931         break;
6932     case 0x18: /* FCVTZS */
6933     case 0x19: /* FCVTZU */
6934         itof = false;
6935         break;
6936     default:
6937         unallocated_encoding(s);
6938         return;
6939     }
6940 
6941     if (!fp_access_check(s)) {
6942         return;
6943     }
6944 
6945     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
6946 }
6947 
6948 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
6949 {
6950     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
6951      * without conversion.
6952      */
6953 
6954     if (itof) {
6955         TCGv_i64 tcg_rn = cpu_reg(s, rn);
6956         TCGv_i64 tmp;
6957 
6958         switch (type) {
6959         case 0:
6960             /* 32 bit */
6961             tmp = tcg_temp_new_i64();
6962             tcg_gen_ext32u_i64(tmp, tcg_rn);
6963             write_fp_dreg(s, rd, tmp);
6964             break;
6965         case 1:
6966             /* 64 bit */
6967             write_fp_dreg(s, rd, tcg_rn);
6968             break;
6969         case 2:
6970             /* 64 bit to top half. */
6971             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
6972             clear_vec_high(s, true, rd);
6973             break;
6974         case 3:
6975             /* 16 bit */
6976             tmp = tcg_temp_new_i64();
6977             tcg_gen_ext16u_i64(tmp, tcg_rn);
6978             write_fp_dreg(s, rd, tmp);
6979             break;
6980         default:
6981             g_assert_not_reached();
6982         }
6983     } else {
6984         TCGv_i64 tcg_rd = cpu_reg(s, rd);
6985 
6986         switch (type) {
6987         case 0:
6988             /* 32 bit */
6989             tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
6990             break;
6991         case 1:
6992             /* 64 bit */
6993             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
6994             break;
6995         case 2:
6996             /* 64 bits from top half */
6997             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
6998             break;
6999         case 3:
7000             /* 16 bit */
7001             tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
7002             break;
7003         default:
7004             g_assert_not_reached();
7005         }
7006     }
7007 }
7008 
7009 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
7010 {
7011     TCGv_i64 t = read_fp_dreg(s, rn);
7012     TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
7013 
7014     gen_helper_fjcvtzs(t, t, fpstatus);
7015 
7016     tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
7017     tcg_gen_extrh_i64_i32(cpu_ZF, t);
7018     tcg_gen_movi_i32(cpu_CF, 0);
7019     tcg_gen_movi_i32(cpu_NF, 0);
7020     tcg_gen_movi_i32(cpu_VF, 0);
7021 }
7022 
7023 /* Floating point <-> integer conversions
7024  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
7025  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7026  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
7027  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7028  */
7029 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
7030 {
7031     int rd = extract32(insn, 0, 5);
7032     int rn = extract32(insn, 5, 5);
7033     int opcode = extract32(insn, 16, 3);
7034     int rmode = extract32(insn, 19, 2);
7035     int type = extract32(insn, 22, 2);
7036     bool sbit = extract32(insn, 29, 1);
7037     bool sf = extract32(insn, 31, 1);
7038     bool itof = false;
7039 
7040     if (sbit) {
7041         goto do_unallocated;
7042     }
7043 
7044     switch (opcode) {
7045     case 2: /* SCVTF */
7046     case 3: /* UCVTF */
7047         itof = true;
7048         /* fallthru */
7049     case 4: /* FCVTAS */
7050     case 5: /* FCVTAU */
7051         if (rmode != 0) {
7052             goto do_unallocated;
7053         }
7054         /* fallthru */
7055     case 0: /* FCVT[NPMZ]S */
7056     case 1: /* FCVT[NPMZ]U */
7057         switch (type) {
7058         case 0: /* float32 */
7059         case 1: /* float64 */
7060             break;
7061         case 3: /* float16 */
7062             if (!dc_isar_feature(aa64_fp16, s)) {
7063                 goto do_unallocated;
7064             }
7065             break;
7066         default:
7067             goto do_unallocated;
7068         }
7069         if (!fp_access_check(s)) {
7070             return;
7071         }
7072         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
7073         break;
7074 
7075     default:
7076         switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
7077         case 0b01100110: /* FMOV half <-> 32-bit int */
7078         case 0b01100111:
7079         case 0b11100110: /* FMOV half <-> 64-bit int */
7080         case 0b11100111:
7081             if (!dc_isar_feature(aa64_fp16, s)) {
7082                 goto do_unallocated;
7083             }
7084             /* fallthru */
7085         case 0b00000110: /* FMOV 32-bit */
7086         case 0b00000111:
7087         case 0b10100110: /* FMOV 64-bit */
7088         case 0b10100111:
7089         case 0b11001110: /* FMOV top half of 128-bit */
7090         case 0b11001111:
7091             if (!fp_access_check(s)) {
7092                 return;
7093             }
7094             itof = opcode & 1;
7095             handle_fmov(s, rd, rn, type, itof);
7096             break;
7097 
7098         case 0b00111110: /* FJCVTZS */
7099             if (!dc_isar_feature(aa64_jscvt, s)) {
7100                 goto do_unallocated;
7101             } else if (fp_access_check(s)) {
7102                 handle_fjcvtzs(s, rd, rn);
7103             }
7104             break;
7105 
7106         default:
7107         do_unallocated:
7108             unallocated_encoding(s);
7109             return;
7110         }
7111         break;
7112     }
7113 }
7114 
7115 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7116  *   31  30  29 28     25 24                          0
7117  * +---+---+---+---------+-----------------------------+
7118  * |   | 0 |   | 1 1 1 1 |                             |
7119  * +---+---+---+---------+-----------------------------+
7120  */
7121 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7122 {
7123     if (extract32(insn, 24, 1)) {
7124         /* Floating point data-processing (3 source) */
7125         disas_fp_3src(s, insn);
7126     } else if (extract32(insn, 21, 1) == 0) {
7127         /* Floating point to fixed point conversions */
7128         disas_fp_fixed_conv(s, insn);
7129     } else {
7130         switch (extract32(insn, 10, 2)) {
7131         case 1:
7132             /* Floating point conditional compare */
7133             disas_fp_ccomp(s, insn);
7134             break;
7135         case 2:
7136             /* Floating point data-processing (2 source) */
7137             disas_fp_2src(s, insn);
7138             break;
7139         case 3:
7140             /* Floating point conditional select */
7141             disas_fp_csel(s, insn);
7142             break;
7143         case 0:
7144             switch (ctz32(extract32(insn, 12, 4))) {
7145             case 0: /* [15:12] == xxx1 */
7146                 /* Floating point immediate */
7147                 disas_fp_imm(s, insn);
7148                 break;
7149             case 1: /* [15:12] == xx10 */
7150                 /* Floating point compare */
7151                 disas_fp_compare(s, insn);
7152                 break;
7153             case 2: /* [15:12] == x100 */
7154                 /* Floating point data-processing (1 source) */
7155                 disas_fp_1src(s, insn);
7156                 break;
7157             case 3: /* [15:12] == 1000 */
7158                 unallocated_encoding(s);
7159                 break;
7160             default: /* [15:12] == 0000 */
7161                 /* Floating point <-> integer conversions */
7162                 disas_fp_int_conv(s, insn);
7163                 break;
7164             }
7165             break;
7166         }
7167     }
7168 }
7169 
7170 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7171                      int pos)
7172 {
7173     /* Extract 64 bits from the middle of two concatenated 64 bit
7174      * vector register slices left:right. The extracted bits start
7175      * at 'pos' bits into the right (least significant) side.
7176      * We return the result in tcg_right, and guarantee not to
7177      * trash tcg_left.
7178      */
7179     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7180     assert(pos > 0 && pos < 64);
7181 
7182     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7183     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7184     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7185 }
7186 
7187 /* EXT
7188  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
7189  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7190  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
7191  * +---+---+-------------+-----+---+------+---+------+---+------+------+
7192  */
7193 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7194 {
7195     int is_q = extract32(insn, 30, 1);
7196     int op2 = extract32(insn, 22, 2);
7197     int imm4 = extract32(insn, 11, 4);
7198     int rm = extract32(insn, 16, 5);
7199     int rn = extract32(insn, 5, 5);
7200     int rd = extract32(insn, 0, 5);
7201     int pos = imm4 << 3;
7202     TCGv_i64 tcg_resl, tcg_resh;
7203 
7204     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7205         unallocated_encoding(s);
7206         return;
7207     }
7208 
7209     if (!fp_access_check(s)) {
7210         return;
7211     }
7212 
7213     tcg_resh = tcg_temp_new_i64();
7214     tcg_resl = tcg_temp_new_i64();
7215 
7216     /* Vd gets bits starting at pos bits into Vm:Vn. This is
7217      * either extracting 128 bits from a 128:128 concatenation, or
7218      * extracting 64 bits from a 64:64 concatenation.
7219      */
7220     if (!is_q) {
7221         read_vec_element(s, tcg_resl, rn, 0, MO_64);
7222         if (pos != 0) {
7223             read_vec_element(s, tcg_resh, rm, 0, MO_64);
7224             do_ext64(s, tcg_resh, tcg_resl, pos);
7225         }
7226     } else {
7227         TCGv_i64 tcg_hh;
7228         typedef struct {
7229             int reg;
7230             int elt;
7231         } EltPosns;
7232         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7233         EltPosns *elt = eltposns;
7234 
7235         if (pos >= 64) {
7236             elt++;
7237             pos -= 64;
7238         }
7239 
7240         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7241         elt++;
7242         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7243         elt++;
7244         if (pos != 0) {
7245             do_ext64(s, tcg_resh, tcg_resl, pos);
7246             tcg_hh = tcg_temp_new_i64();
7247             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7248             do_ext64(s, tcg_hh, tcg_resh, pos);
7249         }
7250     }
7251 
7252     write_vec_element(s, tcg_resl, rd, 0, MO_64);
7253     if (is_q) {
7254         write_vec_element(s, tcg_resh, rd, 1, MO_64);
7255     }
7256     clear_vec_high(s, is_q, rd);
7257 }
7258 
7259 /* TBL/TBX
7260  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
7261  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7262  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
7263  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7264  */
7265 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7266 {
7267     int op2 = extract32(insn, 22, 2);
7268     int is_q = extract32(insn, 30, 1);
7269     int rm = extract32(insn, 16, 5);
7270     int rn = extract32(insn, 5, 5);
7271     int rd = extract32(insn, 0, 5);
7272     int is_tbx = extract32(insn, 12, 1);
7273     int len = (extract32(insn, 13, 2) + 1) * 16;
7274 
7275     if (op2 != 0) {
7276         unallocated_encoding(s);
7277         return;
7278     }
7279 
7280     if (!fp_access_check(s)) {
7281         return;
7282     }
7283 
7284     tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7285                        vec_full_reg_offset(s, rm), cpu_env,
7286                        is_q ? 16 : 8, vec_full_reg_size(s),
7287                        (len << 6) | (is_tbx << 5) | rn,
7288                        gen_helper_simd_tblx);
7289 }
7290 
7291 /* ZIP/UZP/TRN
7292  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
7293  * +---+---+-------------+------+---+------+---+------------------+------+
7294  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
7295  * +---+---+-------------+------+---+------+---+------------------+------+
7296  */
7297 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7298 {
7299     int rd = extract32(insn, 0, 5);
7300     int rn = extract32(insn, 5, 5);
7301     int rm = extract32(insn, 16, 5);
7302     int size = extract32(insn, 22, 2);
7303     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7304      * bit 2 indicates 1 vs 2 variant of the insn.
7305      */
7306     int opcode = extract32(insn, 12, 2);
7307     bool part = extract32(insn, 14, 1);
7308     bool is_q = extract32(insn, 30, 1);
7309     int esize = 8 << size;
7310     int i;
7311     int datasize = is_q ? 128 : 64;
7312     int elements = datasize / esize;
7313     TCGv_i64 tcg_res[2], tcg_ele;
7314 
7315     if (opcode == 0 || (size == 3 && !is_q)) {
7316         unallocated_encoding(s);
7317         return;
7318     }
7319 
7320     if (!fp_access_check(s)) {
7321         return;
7322     }
7323 
7324     tcg_res[0] = tcg_temp_new_i64();
7325     tcg_res[1] = is_q ? tcg_temp_new_i64() : NULL;
7326     tcg_ele = tcg_temp_new_i64();
7327 
7328     for (i = 0; i < elements; i++) {
7329         int o, w;
7330 
7331         switch (opcode) {
7332         case 1: /* UZP1/2 */
7333         {
7334             int midpoint = elements / 2;
7335             if (i < midpoint) {
7336                 read_vec_element(s, tcg_ele, rn, 2 * i + part, size);
7337             } else {
7338                 read_vec_element(s, tcg_ele, rm,
7339                                  2 * (i - midpoint) + part, size);
7340             }
7341             break;
7342         }
7343         case 2: /* TRN1/2 */
7344             if (i & 1) {
7345                 read_vec_element(s, tcg_ele, rm, (i & ~1) + part, size);
7346             } else {
7347                 read_vec_element(s, tcg_ele, rn, (i & ~1) + part, size);
7348             }
7349             break;
7350         case 3: /* ZIP1/2 */
7351         {
7352             int base = part * elements / 2;
7353             if (i & 1) {
7354                 read_vec_element(s, tcg_ele, rm, base + (i >> 1), size);
7355             } else {
7356                 read_vec_element(s, tcg_ele, rn, base + (i >> 1), size);
7357             }
7358             break;
7359         }
7360         default:
7361             g_assert_not_reached();
7362         }
7363 
7364         w = (i * esize) / 64;
7365         o = (i * esize) % 64;
7366         if (o == 0) {
7367             tcg_gen_mov_i64(tcg_res[w], tcg_ele);
7368         } else {
7369             tcg_gen_shli_i64(tcg_ele, tcg_ele, o);
7370             tcg_gen_or_i64(tcg_res[w], tcg_res[w], tcg_ele);
7371         }
7372     }
7373 
7374     for (i = 0; i <= is_q; ++i) {
7375         write_vec_element(s, tcg_res[i], rd, i, MO_64);
7376     }
7377     clear_vec_high(s, is_q, rd);
7378 }
7379 
7380 /*
7381  * do_reduction_op helper
7382  *
7383  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7384  * important for correct NaN propagation that we do these
7385  * operations in exactly the order specified by the pseudocode.
7386  *
7387  * This is a recursive function, TCG temps should be freed by the
7388  * calling function once it is done with the values.
7389  */
7390 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7391                                 int esize, int size, int vmap, TCGv_ptr fpst)
7392 {
7393     if (esize == size) {
7394         int element;
7395         MemOp msize = esize == 16 ? MO_16 : MO_32;
7396         TCGv_i32 tcg_elem;
7397 
7398         /* We should have one register left here */
7399         assert(ctpop8(vmap) == 1);
7400         element = ctz32(vmap);
7401         assert(element < 8);
7402 
7403         tcg_elem = tcg_temp_new_i32();
7404         read_vec_element_i32(s, tcg_elem, rn, element, msize);
7405         return tcg_elem;
7406     } else {
7407         int bits = size / 2;
7408         int shift = ctpop8(vmap) / 2;
7409         int vmap_lo = (vmap >> shift) & vmap;
7410         int vmap_hi = (vmap & ~vmap_lo);
7411         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7412 
7413         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7414         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7415         tcg_res = tcg_temp_new_i32();
7416 
7417         switch (fpopcode) {
7418         case 0x0c: /* fmaxnmv half-precision */
7419             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7420             break;
7421         case 0x0f: /* fmaxv half-precision */
7422             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7423             break;
7424         case 0x1c: /* fminnmv half-precision */
7425             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7426             break;
7427         case 0x1f: /* fminv half-precision */
7428             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7429             break;
7430         case 0x2c: /* fmaxnmv */
7431             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7432             break;
7433         case 0x2f: /* fmaxv */
7434             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7435             break;
7436         case 0x3c: /* fminnmv */
7437             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7438             break;
7439         case 0x3f: /* fminv */
7440             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7441             break;
7442         default:
7443             g_assert_not_reached();
7444         }
7445         return tcg_res;
7446     }
7447 }
7448 
7449 /* AdvSIMD across lanes
7450  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7451  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7452  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7453  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7454  */
7455 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7456 {
7457     int rd = extract32(insn, 0, 5);
7458     int rn = extract32(insn, 5, 5);
7459     int size = extract32(insn, 22, 2);
7460     int opcode = extract32(insn, 12, 5);
7461     bool is_q = extract32(insn, 30, 1);
7462     bool is_u = extract32(insn, 29, 1);
7463     bool is_fp = false;
7464     bool is_min = false;
7465     int esize;
7466     int elements;
7467     int i;
7468     TCGv_i64 tcg_res, tcg_elt;
7469 
7470     switch (opcode) {
7471     case 0x1b: /* ADDV */
7472         if (is_u) {
7473             unallocated_encoding(s);
7474             return;
7475         }
7476         /* fall through */
7477     case 0x3: /* SADDLV, UADDLV */
7478     case 0xa: /* SMAXV, UMAXV */
7479     case 0x1a: /* SMINV, UMINV */
7480         if (size == 3 || (size == 2 && !is_q)) {
7481             unallocated_encoding(s);
7482             return;
7483         }
7484         break;
7485     case 0xc: /* FMAXNMV, FMINNMV */
7486     case 0xf: /* FMAXV, FMINV */
7487         /* Bit 1 of size field encodes min vs max and the actual size
7488          * depends on the encoding of the U bit. If not set (and FP16
7489          * enabled) then we do half-precision float instead of single
7490          * precision.
7491          */
7492         is_min = extract32(size, 1, 1);
7493         is_fp = true;
7494         if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7495             size = 1;
7496         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7497             unallocated_encoding(s);
7498             return;
7499         } else {
7500             size = 2;
7501         }
7502         break;
7503     default:
7504         unallocated_encoding(s);
7505         return;
7506     }
7507 
7508     if (!fp_access_check(s)) {
7509         return;
7510     }
7511 
7512     esize = 8 << size;
7513     elements = (is_q ? 128 : 64) / esize;
7514 
7515     tcg_res = tcg_temp_new_i64();
7516     tcg_elt = tcg_temp_new_i64();
7517 
7518     /* These instructions operate across all lanes of a vector
7519      * to produce a single result. We can guarantee that a 64
7520      * bit intermediate is sufficient:
7521      *  + for [US]ADDLV the maximum element size is 32 bits, and
7522      *    the result type is 64 bits
7523      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7524      *    same as the element size, which is 32 bits at most
7525      * For the integer operations we can choose to work at 64
7526      * or 32 bits and truncate at the end; for simplicity
7527      * we use 64 bits always. The floating point
7528      * ops do require 32 bit intermediates, though.
7529      */
7530     if (!is_fp) {
7531         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7532 
7533         for (i = 1; i < elements; i++) {
7534             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7535 
7536             switch (opcode) {
7537             case 0x03: /* SADDLV / UADDLV */
7538             case 0x1b: /* ADDV */
7539                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7540                 break;
7541             case 0x0a: /* SMAXV / UMAXV */
7542                 if (is_u) {
7543                     tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7544                 } else {
7545                     tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7546                 }
7547                 break;
7548             case 0x1a: /* SMINV / UMINV */
7549                 if (is_u) {
7550                     tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7551                 } else {
7552                     tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7553                 }
7554                 break;
7555             default:
7556                 g_assert_not_reached();
7557             }
7558 
7559         }
7560     } else {
7561         /* Floating point vector reduction ops which work across 32
7562          * bit (single) or 16 bit (half-precision) intermediates.
7563          * Note that correct NaN propagation requires that we do these
7564          * operations in exactly the order specified by the pseudocode.
7565          */
7566         TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7567         int fpopcode = opcode | is_min << 4 | is_u << 5;
7568         int vmap = (1 << elements) - 1;
7569         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7570                                              (is_q ? 128 : 64), vmap, fpst);
7571         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7572     }
7573 
7574     /* Now truncate the result to the width required for the final output */
7575     if (opcode == 0x03) {
7576         /* SADDLV, UADDLV: result is 2*esize */
7577         size++;
7578     }
7579 
7580     switch (size) {
7581     case 0:
7582         tcg_gen_ext8u_i64(tcg_res, tcg_res);
7583         break;
7584     case 1:
7585         tcg_gen_ext16u_i64(tcg_res, tcg_res);
7586         break;
7587     case 2:
7588         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7589         break;
7590     case 3:
7591         break;
7592     default:
7593         g_assert_not_reached();
7594     }
7595 
7596     write_fp_dreg(s, rd, tcg_res);
7597 }
7598 
7599 /* DUP (Element, Vector)
7600  *
7601  *  31  30   29              21 20    16 15        10  9    5 4    0
7602  * +---+---+-------------------+--------+-------------+------+------+
7603  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7604  * +---+---+-------------------+--------+-------------+------+------+
7605  *
7606  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7607  */
7608 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7609                              int imm5)
7610 {
7611     int size = ctz32(imm5);
7612     int index;
7613 
7614     if (size > 3 || (size == 3 && !is_q)) {
7615         unallocated_encoding(s);
7616         return;
7617     }
7618 
7619     if (!fp_access_check(s)) {
7620         return;
7621     }
7622 
7623     index = imm5 >> (size + 1);
7624     tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7625                          vec_reg_offset(s, rn, index, size),
7626                          is_q ? 16 : 8, vec_full_reg_size(s));
7627 }
7628 
7629 /* DUP (element, scalar)
7630  *  31                   21 20    16 15        10  9    5 4    0
7631  * +-----------------------+--------+-------------+------+------+
7632  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
7633  * +-----------------------+--------+-------------+------+------+
7634  */
7635 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7636                               int imm5)
7637 {
7638     int size = ctz32(imm5);
7639     int index;
7640     TCGv_i64 tmp;
7641 
7642     if (size > 3) {
7643         unallocated_encoding(s);
7644         return;
7645     }
7646 
7647     if (!fp_access_check(s)) {
7648         return;
7649     }
7650 
7651     index = imm5 >> (size + 1);
7652 
7653     /* This instruction just extracts the specified element and
7654      * zero-extends it into the bottom of the destination register.
7655      */
7656     tmp = tcg_temp_new_i64();
7657     read_vec_element(s, tmp, rn, index, size);
7658     write_fp_dreg(s, rd, tmp);
7659 }
7660 
7661 /* DUP (General)
7662  *
7663  *  31  30   29              21 20    16 15        10  9    5 4    0
7664  * +---+---+-------------------+--------+-------------+------+------+
7665  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
7666  * +---+---+-------------------+--------+-------------+------+------+
7667  *
7668  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7669  */
7670 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7671                              int imm5)
7672 {
7673     int size = ctz32(imm5);
7674     uint32_t dofs, oprsz, maxsz;
7675 
7676     if (size > 3 || ((size == 3) && !is_q)) {
7677         unallocated_encoding(s);
7678         return;
7679     }
7680 
7681     if (!fp_access_check(s)) {
7682         return;
7683     }
7684 
7685     dofs = vec_full_reg_offset(s, rd);
7686     oprsz = is_q ? 16 : 8;
7687     maxsz = vec_full_reg_size(s);
7688 
7689     tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7690 }
7691 
7692 /* INS (Element)
7693  *
7694  *  31                   21 20    16 15  14    11  10 9    5 4    0
7695  * +-----------------------+--------+------------+---+------+------+
7696  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
7697  * +-----------------------+--------+------------+---+------+------+
7698  *
7699  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7700  * index: encoded in imm5<4:size+1>
7701  */
7702 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7703                              int imm4, int imm5)
7704 {
7705     int size = ctz32(imm5);
7706     int src_index, dst_index;
7707     TCGv_i64 tmp;
7708 
7709     if (size > 3) {
7710         unallocated_encoding(s);
7711         return;
7712     }
7713 
7714     if (!fp_access_check(s)) {
7715         return;
7716     }
7717 
7718     dst_index = extract32(imm5, 1+size, 5);
7719     src_index = extract32(imm4, size, 4);
7720 
7721     tmp = tcg_temp_new_i64();
7722 
7723     read_vec_element(s, tmp, rn, src_index, size);
7724     write_vec_element(s, tmp, rd, dst_index, size);
7725 
7726     /* INS is considered a 128-bit write for SVE. */
7727     clear_vec_high(s, true, rd);
7728 }
7729 
7730 
7731 /* INS (General)
7732  *
7733  *  31                   21 20    16 15        10  9    5 4    0
7734  * +-----------------------+--------+-------------+------+------+
7735  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
7736  * +-----------------------+--------+-------------+------+------+
7737  *
7738  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7739  * index: encoded in imm5<4:size+1>
7740  */
7741 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7742 {
7743     int size = ctz32(imm5);
7744     int idx;
7745 
7746     if (size > 3) {
7747         unallocated_encoding(s);
7748         return;
7749     }
7750 
7751     if (!fp_access_check(s)) {
7752         return;
7753     }
7754 
7755     idx = extract32(imm5, 1 + size, 4 - size);
7756     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7757 
7758     /* INS is considered a 128-bit write for SVE. */
7759     clear_vec_high(s, true, rd);
7760 }
7761 
7762 /*
7763  * UMOV (General)
7764  * SMOV (General)
7765  *
7766  *  31  30   29              21 20    16 15    12   10 9    5 4    0
7767  * +---+---+-------------------+--------+-------------+------+------+
7768  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
7769  * +---+---+-------------------+--------+-------------+------+------+
7770  *
7771  * U: unsigned when set
7772  * size: encoded in imm5 (see ARM ARM LowestSetBit())
7773  */
7774 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7775                                   int rn, int rd, int imm5)
7776 {
7777     int size = ctz32(imm5);
7778     int element;
7779     TCGv_i64 tcg_rd;
7780 
7781     /* Check for UnallocatedEncodings */
7782     if (is_signed) {
7783         if (size > 2 || (size == 2 && !is_q)) {
7784             unallocated_encoding(s);
7785             return;
7786         }
7787     } else {
7788         if (size > 3
7789             || (size < 3 && is_q)
7790             || (size == 3 && !is_q)) {
7791             unallocated_encoding(s);
7792             return;
7793         }
7794     }
7795 
7796     if (!fp_access_check(s)) {
7797         return;
7798     }
7799 
7800     element = extract32(imm5, 1+size, 4);
7801 
7802     tcg_rd = cpu_reg(s, rd);
7803     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7804     if (is_signed && !is_q) {
7805         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7806     }
7807 }
7808 
7809 /* AdvSIMD copy
7810  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
7811  * +---+---+----+-----------------+------+---+------+---+------+------+
7812  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7813  * +---+---+----+-----------------+------+---+------+---+------+------+
7814  */
7815 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7816 {
7817     int rd = extract32(insn, 0, 5);
7818     int rn = extract32(insn, 5, 5);
7819     int imm4 = extract32(insn, 11, 4);
7820     int op = extract32(insn, 29, 1);
7821     int is_q = extract32(insn, 30, 1);
7822     int imm5 = extract32(insn, 16, 5);
7823 
7824     if (op) {
7825         if (is_q) {
7826             /* INS (element) */
7827             handle_simd_inse(s, rd, rn, imm4, imm5);
7828         } else {
7829             unallocated_encoding(s);
7830         }
7831     } else {
7832         switch (imm4) {
7833         case 0:
7834             /* DUP (element - vector) */
7835             handle_simd_dupe(s, is_q, rd, rn, imm5);
7836             break;
7837         case 1:
7838             /* DUP (general) */
7839             handle_simd_dupg(s, is_q, rd, rn, imm5);
7840             break;
7841         case 3:
7842             if (is_q) {
7843                 /* INS (general) */
7844                 handle_simd_insg(s, rd, rn, imm5);
7845             } else {
7846                 unallocated_encoding(s);
7847             }
7848             break;
7849         case 5:
7850         case 7:
7851             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
7852             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
7853             break;
7854         default:
7855             unallocated_encoding(s);
7856             break;
7857         }
7858     }
7859 }
7860 
7861 /* AdvSIMD modified immediate
7862  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
7863  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7864  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
7865  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7866  *
7867  * There are a number of operations that can be carried out here:
7868  *   MOVI - move (shifted) imm into register
7869  *   MVNI - move inverted (shifted) imm into register
7870  *   ORR  - bitwise OR of (shifted) imm with register
7871  *   BIC  - bitwise clear of (shifted) imm with register
7872  * With ARMv8.2 we also have:
7873  *   FMOV half-precision
7874  */
7875 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
7876 {
7877     int rd = extract32(insn, 0, 5);
7878     int cmode = extract32(insn, 12, 4);
7879     int o2 = extract32(insn, 11, 1);
7880     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
7881     bool is_neg = extract32(insn, 29, 1);
7882     bool is_q = extract32(insn, 30, 1);
7883     uint64_t imm = 0;
7884 
7885     if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
7886         /* Check for FMOV (vector, immediate) - half-precision */
7887         if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
7888             unallocated_encoding(s);
7889             return;
7890         }
7891     }
7892 
7893     if (!fp_access_check(s)) {
7894         return;
7895     }
7896 
7897     if (cmode == 15 && o2 && !is_neg) {
7898         /* FMOV (vector, immediate) - half-precision */
7899         imm = vfp_expand_imm(MO_16, abcdefgh);
7900         /* now duplicate across the lanes */
7901         imm = dup_const(MO_16, imm);
7902     } else {
7903         imm = asimd_imm_const(abcdefgh, cmode, is_neg);
7904     }
7905 
7906     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
7907         /* MOVI or MVNI, with MVNI negation handled above.  */
7908         tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
7909                              vec_full_reg_size(s), imm);
7910     } else {
7911         /* ORR or BIC, with BIC negation to AND handled above.  */
7912         if (is_neg) {
7913             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
7914         } else {
7915             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
7916         }
7917     }
7918 }
7919 
7920 /* AdvSIMD scalar copy
7921  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
7922  * +-----+----+-----------------+------+---+------+---+------+------+
7923  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
7924  * +-----+----+-----------------+------+---+------+---+------+------+
7925  */
7926 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
7927 {
7928     int rd = extract32(insn, 0, 5);
7929     int rn = extract32(insn, 5, 5);
7930     int imm4 = extract32(insn, 11, 4);
7931     int imm5 = extract32(insn, 16, 5);
7932     int op = extract32(insn, 29, 1);
7933 
7934     if (op != 0 || imm4 != 0) {
7935         unallocated_encoding(s);
7936         return;
7937     }
7938 
7939     /* DUP (element, scalar) */
7940     handle_simd_dupes(s, rd, rn, imm5);
7941 }
7942 
7943 /* AdvSIMD scalar pairwise
7944  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
7945  * +-----+---+-----------+------+-----------+--------+-----+------+------+
7946  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
7947  * +-----+---+-----------+------+-----------+--------+-----+------+------+
7948  */
7949 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
7950 {
7951     int u = extract32(insn, 29, 1);
7952     int size = extract32(insn, 22, 2);
7953     int opcode = extract32(insn, 12, 5);
7954     int rn = extract32(insn, 5, 5);
7955     int rd = extract32(insn, 0, 5);
7956     TCGv_ptr fpst;
7957 
7958     /* For some ops (the FP ones), size[1] is part of the encoding.
7959      * For ADDP strictly it is not but size[1] is always 1 for valid
7960      * encodings.
7961      */
7962     opcode |= (extract32(size, 1, 1) << 5);
7963 
7964     switch (opcode) {
7965     case 0x3b: /* ADDP */
7966         if (u || size != 3) {
7967             unallocated_encoding(s);
7968             return;
7969         }
7970         if (!fp_access_check(s)) {
7971             return;
7972         }
7973 
7974         fpst = NULL;
7975         break;
7976     case 0xc: /* FMAXNMP */
7977     case 0xd: /* FADDP */
7978     case 0xf: /* FMAXP */
7979     case 0x2c: /* FMINNMP */
7980     case 0x2f: /* FMINP */
7981         /* FP op, size[0] is 32 or 64 bit*/
7982         if (!u) {
7983             if (!dc_isar_feature(aa64_fp16, s)) {
7984                 unallocated_encoding(s);
7985                 return;
7986             } else {
7987                 size = MO_16;
7988             }
7989         } else {
7990             size = extract32(size, 0, 1) ? MO_64 : MO_32;
7991         }
7992 
7993         if (!fp_access_check(s)) {
7994             return;
7995         }
7996 
7997         fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7998         break;
7999     default:
8000         unallocated_encoding(s);
8001         return;
8002     }
8003 
8004     if (size == MO_64) {
8005         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8006         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8007         TCGv_i64 tcg_res = tcg_temp_new_i64();
8008 
8009         read_vec_element(s, tcg_op1, rn, 0, MO_64);
8010         read_vec_element(s, tcg_op2, rn, 1, MO_64);
8011 
8012         switch (opcode) {
8013         case 0x3b: /* ADDP */
8014             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
8015             break;
8016         case 0xc: /* FMAXNMP */
8017             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8018             break;
8019         case 0xd: /* FADDP */
8020             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8021             break;
8022         case 0xf: /* FMAXP */
8023             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8024             break;
8025         case 0x2c: /* FMINNMP */
8026             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8027             break;
8028         case 0x2f: /* FMINP */
8029             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8030             break;
8031         default:
8032             g_assert_not_reached();
8033         }
8034 
8035         write_fp_dreg(s, rd, tcg_res);
8036     } else {
8037         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8038         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8039         TCGv_i32 tcg_res = tcg_temp_new_i32();
8040 
8041         read_vec_element_i32(s, tcg_op1, rn, 0, size);
8042         read_vec_element_i32(s, tcg_op2, rn, 1, size);
8043 
8044         if (size == MO_16) {
8045             switch (opcode) {
8046             case 0xc: /* FMAXNMP */
8047                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8048                 break;
8049             case 0xd: /* FADDP */
8050                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
8051                 break;
8052             case 0xf: /* FMAXP */
8053                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
8054                 break;
8055             case 0x2c: /* FMINNMP */
8056                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8057                 break;
8058             case 0x2f: /* FMINP */
8059                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
8060                 break;
8061             default:
8062                 g_assert_not_reached();
8063             }
8064         } else {
8065             switch (opcode) {
8066             case 0xc: /* FMAXNMP */
8067                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8068                 break;
8069             case 0xd: /* FADDP */
8070                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8071                 break;
8072             case 0xf: /* FMAXP */
8073                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8074                 break;
8075             case 0x2c: /* FMINNMP */
8076                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8077                 break;
8078             case 0x2f: /* FMINP */
8079                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8080                 break;
8081             default:
8082                 g_assert_not_reached();
8083             }
8084         }
8085 
8086         write_fp_sreg(s, rd, tcg_res);
8087     }
8088 }
8089 
8090 /*
8091  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8092  *
8093  * This code is handles the common shifting code and is used by both
8094  * the vector and scalar code.
8095  */
8096 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8097                                     TCGv_i64 tcg_rnd, bool accumulate,
8098                                     bool is_u, int size, int shift)
8099 {
8100     bool extended_result = false;
8101     bool round = tcg_rnd != NULL;
8102     int ext_lshift = 0;
8103     TCGv_i64 tcg_src_hi;
8104 
8105     if (round && size == 3) {
8106         extended_result = true;
8107         ext_lshift = 64 - shift;
8108         tcg_src_hi = tcg_temp_new_i64();
8109     } else if (shift == 64) {
8110         if (!accumulate && is_u) {
8111             /* result is zero */
8112             tcg_gen_movi_i64(tcg_res, 0);
8113             return;
8114         }
8115     }
8116 
8117     /* Deal with the rounding step */
8118     if (round) {
8119         if (extended_result) {
8120             TCGv_i64 tcg_zero = tcg_constant_i64(0);
8121             if (!is_u) {
8122                 /* take care of sign extending tcg_res */
8123                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8124                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8125                                  tcg_src, tcg_src_hi,
8126                                  tcg_rnd, tcg_zero);
8127             } else {
8128                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8129                                  tcg_src, tcg_zero,
8130                                  tcg_rnd, tcg_zero);
8131             }
8132         } else {
8133             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8134         }
8135     }
8136 
8137     /* Now do the shift right */
8138     if (round && extended_result) {
8139         /* extended case, >64 bit precision required */
8140         if (ext_lshift == 0) {
8141             /* special case, only high bits matter */
8142             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8143         } else {
8144             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8145             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8146             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8147         }
8148     } else {
8149         if (is_u) {
8150             if (shift == 64) {
8151                 /* essentially shifting in 64 zeros */
8152                 tcg_gen_movi_i64(tcg_src, 0);
8153             } else {
8154                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8155             }
8156         } else {
8157             if (shift == 64) {
8158                 /* effectively extending the sign-bit */
8159                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8160             } else {
8161                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8162             }
8163         }
8164     }
8165 
8166     if (accumulate) {
8167         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8168     } else {
8169         tcg_gen_mov_i64(tcg_res, tcg_src);
8170     }
8171 }
8172 
8173 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8174 static void handle_scalar_simd_shri(DisasContext *s,
8175                                     bool is_u, int immh, int immb,
8176                                     int opcode, int rn, int rd)
8177 {
8178     const int size = 3;
8179     int immhb = immh << 3 | immb;
8180     int shift = 2 * (8 << size) - immhb;
8181     bool accumulate = false;
8182     bool round = false;
8183     bool insert = false;
8184     TCGv_i64 tcg_rn;
8185     TCGv_i64 tcg_rd;
8186     TCGv_i64 tcg_round;
8187 
8188     if (!extract32(immh, 3, 1)) {
8189         unallocated_encoding(s);
8190         return;
8191     }
8192 
8193     if (!fp_access_check(s)) {
8194         return;
8195     }
8196 
8197     switch (opcode) {
8198     case 0x02: /* SSRA / USRA (accumulate) */
8199         accumulate = true;
8200         break;
8201     case 0x04: /* SRSHR / URSHR (rounding) */
8202         round = true;
8203         break;
8204     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8205         accumulate = round = true;
8206         break;
8207     case 0x08: /* SRI */
8208         insert = true;
8209         break;
8210     }
8211 
8212     if (round) {
8213         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8214     } else {
8215         tcg_round = NULL;
8216     }
8217 
8218     tcg_rn = read_fp_dreg(s, rn);
8219     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8220 
8221     if (insert) {
8222         /* shift count same as element size is valid but does nothing;
8223          * special case to avoid potential shift by 64.
8224          */
8225         int esize = 8 << size;
8226         if (shift != esize) {
8227             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8228             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8229         }
8230     } else {
8231         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8232                                 accumulate, is_u, size, shift);
8233     }
8234 
8235     write_fp_dreg(s, rd, tcg_rd);
8236 }
8237 
8238 /* SHL/SLI - Scalar shift left */
8239 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8240                                     int immh, int immb, int opcode,
8241                                     int rn, int rd)
8242 {
8243     int size = 32 - clz32(immh) - 1;
8244     int immhb = immh << 3 | immb;
8245     int shift = immhb - (8 << size);
8246     TCGv_i64 tcg_rn;
8247     TCGv_i64 tcg_rd;
8248 
8249     if (!extract32(immh, 3, 1)) {
8250         unallocated_encoding(s);
8251         return;
8252     }
8253 
8254     if (!fp_access_check(s)) {
8255         return;
8256     }
8257 
8258     tcg_rn = read_fp_dreg(s, rn);
8259     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8260 
8261     if (insert) {
8262         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8263     } else {
8264         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8265     }
8266 
8267     write_fp_dreg(s, rd, tcg_rd);
8268 }
8269 
8270 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8271  * (signed/unsigned) narrowing */
8272 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8273                                    bool is_u_shift, bool is_u_narrow,
8274                                    int immh, int immb, int opcode,
8275                                    int rn, int rd)
8276 {
8277     int immhb = immh << 3 | immb;
8278     int size = 32 - clz32(immh) - 1;
8279     int esize = 8 << size;
8280     int shift = (2 * esize) - immhb;
8281     int elements = is_scalar ? 1 : (64 / esize);
8282     bool round = extract32(opcode, 0, 1);
8283     MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8284     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8285     TCGv_i32 tcg_rd_narrowed;
8286     TCGv_i64 tcg_final;
8287 
8288     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8289         { gen_helper_neon_narrow_sat_s8,
8290           gen_helper_neon_unarrow_sat8 },
8291         { gen_helper_neon_narrow_sat_s16,
8292           gen_helper_neon_unarrow_sat16 },
8293         { gen_helper_neon_narrow_sat_s32,
8294           gen_helper_neon_unarrow_sat32 },
8295         { NULL, NULL },
8296     };
8297     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8298         gen_helper_neon_narrow_sat_u8,
8299         gen_helper_neon_narrow_sat_u16,
8300         gen_helper_neon_narrow_sat_u32,
8301         NULL
8302     };
8303     NeonGenNarrowEnvFn *narrowfn;
8304 
8305     int i;
8306 
8307     assert(size < 4);
8308 
8309     if (extract32(immh, 3, 1)) {
8310         unallocated_encoding(s);
8311         return;
8312     }
8313 
8314     if (!fp_access_check(s)) {
8315         return;
8316     }
8317 
8318     if (is_u_shift) {
8319         narrowfn = unsigned_narrow_fns[size];
8320     } else {
8321         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8322     }
8323 
8324     tcg_rn = tcg_temp_new_i64();
8325     tcg_rd = tcg_temp_new_i64();
8326     tcg_rd_narrowed = tcg_temp_new_i32();
8327     tcg_final = tcg_temp_new_i64();
8328 
8329     if (round) {
8330         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8331     } else {
8332         tcg_round = NULL;
8333     }
8334 
8335     for (i = 0; i < elements; i++) {
8336         read_vec_element(s, tcg_rn, rn, i, ldop);
8337         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8338                                 false, is_u_shift, size+1, shift);
8339         narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8340         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8341         if (i == 0) {
8342             tcg_gen_mov_i64(tcg_final, tcg_rd);
8343         } else {
8344             tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8345         }
8346     }
8347 
8348     if (!is_q) {
8349         write_vec_element(s, tcg_final, rd, 0, MO_64);
8350     } else {
8351         write_vec_element(s, tcg_final, rd, 1, MO_64);
8352     }
8353     clear_vec_high(s, is_q, rd);
8354 }
8355 
8356 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8357 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8358                              bool src_unsigned, bool dst_unsigned,
8359                              int immh, int immb, int rn, int rd)
8360 {
8361     int immhb = immh << 3 | immb;
8362     int size = 32 - clz32(immh) - 1;
8363     int shift = immhb - (8 << size);
8364     int pass;
8365 
8366     assert(immh != 0);
8367     assert(!(scalar && is_q));
8368 
8369     if (!scalar) {
8370         if (!is_q && extract32(immh, 3, 1)) {
8371             unallocated_encoding(s);
8372             return;
8373         }
8374 
8375         /* Since we use the variable-shift helpers we must
8376          * replicate the shift count into each element of
8377          * the tcg_shift value.
8378          */
8379         switch (size) {
8380         case 0:
8381             shift |= shift << 8;
8382             /* fall through */
8383         case 1:
8384             shift |= shift << 16;
8385             break;
8386         case 2:
8387         case 3:
8388             break;
8389         default:
8390             g_assert_not_reached();
8391         }
8392     }
8393 
8394     if (!fp_access_check(s)) {
8395         return;
8396     }
8397 
8398     if (size == 3) {
8399         TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8400         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8401             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8402             { NULL, gen_helper_neon_qshl_u64 },
8403         };
8404         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8405         int maxpass = is_q ? 2 : 1;
8406 
8407         for (pass = 0; pass < maxpass; pass++) {
8408             TCGv_i64 tcg_op = tcg_temp_new_i64();
8409 
8410             read_vec_element(s, tcg_op, rn, pass, MO_64);
8411             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8412             write_vec_element(s, tcg_op, rd, pass, MO_64);
8413         }
8414         clear_vec_high(s, is_q, rd);
8415     } else {
8416         TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8417         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8418             {
8419                 { gen_helper_neon_qshl_s8,
8420                   gen_helper_neon_qshl_s16,
8421                   gen_helper_neon_qshl_s32 },
8422                 { gen_helper_neon_qshlu_s8,
8423                   gen_helper_neon_qshlu_s16,
8424                   gen_helper_neon_qshlu_s32 }
8425             }, {
8426                 { NULL, NULL, NULL },
8427                 { gen_helper_neon_qshl_u8,
8428                   gen_helper_neon_qshl_u16,
8429                   gen_helper_neon_qshl_u32 }
8430             }
8431         };
8432         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8433         MemOp memop = scalar ? size : MO_32;
8434         int maxpass = scalar ? 1 : is_q ? 4 : 2;
8435 
8436         for (pass = 0; pass < maxpass; pass++) {
8437             TCGv_i32 tcg_op = tcg_temp_new_i32();
8438 
8439             read_vec_element_i32(s, tcg_op, rn, pass, memop);
8440             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8441             if (scalar) {
8442                 switch (size) {
8443                 case 0:
8444                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
8445                     break;
8446                 case 1:
8447                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
8448                     break;
8449                 case 2:
8450                     break;
8451                 default:
8452                     g_assert_not_reached();
8453                 }
8454                 write_fp_sreg(s, rd, tcg_op);
8455             } else {
8456                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8457             }
8458         }
8459 
8460         if (!scalar) {
8461             clear_vec_high(s, is_q, rd);
8462         }
8463     }
8464 }
8465 
8466 /* Common vector code for handling integer to FP conversion */
8467 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8468                                    int elements, int is_signed,
8469                                    int fracbits, int size)
8470 {
8471     TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8472     TCGv_i32 tcg_shift = NULL;
8473 
8474     MemOp mop = size | (is_signed ? MO_SIGN : 0);
8475     int pass;
8476 
8477     if (fracbits || size == MO_64) {
8478         tcg_shift = tcg_constant_i32(fracbits);
8479     }
8480 
8481     if (size == MO_64) {
8482         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8483         TCGv_i64 tcg_double = tcg_temp_new_i64();
8484 
8485         for (pass = 0; pass < elements; pass++) {
8486             read_vec_element(s, tcg_int64, rn, pass, mop);
8487 
8488             if (is_signed) {
8489                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8490                                      tcg_shift, tcg_fpst);
8491             } else {
8492                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8493                                      tcg_shift, tcg_fpst);
8494             }
8495             if (elements == 1) {
8496                 write_fp_dreg(s, rd, tcg_double);
8497             } else {
8498                 write_vec_element(s, tcg_double, rd, pass, MO_64);
8499             }
8500         }
8501     } else {
8502         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8503         TCGv_i32 tcg_float = tcg_temp_new_i32();
8504 
8505         for (pass = 0; pass < elements; pass++) {
8506             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8507 
8508             switch (size) {
8509             case MO_32:
8510                 if (fracbits) {
8511                     if (is_signed) {
8512                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
8513                                              tcg_shift, tcg_fpst);
8514                     } else {
8515                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
8516                                              tcg_shift, tcg_fpst);
8517                     }
8518                 } else {
8519                     if (is_signed) {
8520                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8521                     } else {
8522                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8523                     }
8524                 }
8525                 break;
8526             case MO_16:
8527                 if (fracbits) {
8528                     if (is_signed) {
8529                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8530                                              tcg_shift, tcg_fpst);
8531                     } else {
8532                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8533                                              tcg_shift, tcg_fpst);
8534                     }
8535                 } else {
8536                     if (is_signed) {
8537                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8538                     } else {
8539                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8540                     }
8541                 }
8542                 break;
8543             default:
8544                 g_assert_not_reached();
8545             }
8546 
8547             if (elements == 1) {
8548                 write_fp_sreg(s, rd, tcg_float);
8549             } else {
8550                 write_vec_element_i32(s, tcg_float, rd, pass, size);
8551             }
8552         }
8553     }
8554 
8555     clear_vec_high(s, elements << size == 16, rd);
8556 }
8557 
8558 /* UCVTF/SCVTF - Integer to FP conversion */
8559 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8560                                          bool is_q, bool is_u,
8561                                          int immh, int immb, int opcode,
8562                                          int rn, int rd)
8563 {
8564     int size, elements, fracbits;
8565     int immhb = immh << 3 | immb;
8566 
8567     if (immh & 8) {
8568         size = MO_64;
8569         if (!is_scalar && !is_q) {
8570             unallocated_encoding(s);
8571             return;
8572         }
8573     } else if (immh & 4) {
8574         size = MO_32;
8575     } else if (immh & 2) {
8576         size = MO_16;
8577         if (!dc_isar_feature(aa64_fp16, s)) {
8578             unallocated_encoding(s);
8579             return;
8580         }
8581     } else {
8582         /* immh == 0 would be a failure of the decode logic */
8583         g_assert(immh == 1);
8584         unallocated_encoding(s);
8585         return;
8586     }
8587 
8588     if (is_scalar) {
8589         elements = 1;
8590     } else {
8591         elements = (8 << is_q) >> size;
8592     }
8593     fracbits = (16 << size) - immhb;
8594 
8595     if (!fp_access_check(s)) {
8596         return;
8597     }
8598 
8599     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8600 }
8601 
8602 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8603 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8604                                          bool is_q, bool is_u,
8605                                          int immh, int immb, int rn, int rd)
8606 {
8607     int immhb = immh << 3 | immb;
8608     int pass, size, fracbits;
8609     TCGv_ptr tcg_fpstatus;
8610     TCGv_i32 tcg_rmode, tcg_shift;
8611 
8612     if (immh & 0x8) {
8613         size = MO_64;
8614         if (!is_scalar && !is_q) {
8615             unallocated_encoding(s);
8616             return;
8617         }
8618     } else if (immh & 0x4) {
8619         size = MO_32;
8620     } else if (immh & 0x2) {
8621         size = MO_16;
8622         if (!dc_isar_feature(aa64_fp16, s)) {
8623             unallocated_encoding(s);
8624             return;
8625         }
8626     } else {
8627         /* Should have split out AdvSIMD modified immediate earlier.  */
8628         assert(immh == 1);
8629         unallocated_encoding(s);
8630         return;
8631     }
8632 
8633     if (!fp_access_check(s)) {
8634         return;
8635     }
8636 
8637     assert(!(is_scalar && is_q));
8638 
8639     tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8640     tcg_rmode = gen_set_rmode(FPROUNDING_ZERO, tcg_fpstatus);
8641     fracbits = (16 << size) - immhb;
8642     tcg_shift = tcg_constant_i32(fracbits);
8643 
8644     if (size == MO_64) {
8645         int maxpass = is_scalar ? 1 : 2;
8646 
8647         for (pass = 0; pass < maxpass; pass++) {
8648             TCGv_i64 tcg_op = tcg_temp_new_i64();
8649 
8650             read_vec_element(s, tcg_op, rn, pass, MO_64);
8651             if (is_u) {
8652                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8653             } else {
8654                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8655             }
8656             write_vec_element(s, tcg_op, rd, pass, MO_64);
8657         }
8658         clear_vec_high(s, is_q, rd);
8659     } else {
8660         void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8661         int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8662 
8663         switch (size) {
8664         case MO_16:
8665             if (is_u) {
8666                 fn = gen_helper_vfp_touhh;
8667             } else {
8668                 fn = gen_helper_vfp_toshh;
8669             }
8670             break;
8671         case MO_32:
8672             if (is_u) {
8673                 fn = gen_helper_vfp_touls;
8674             } else {
8675                 fn = gen_helper_vfp_tosls;
8676             }
8677             break;
8678         default:
8679             g_assert_not_reached();
8680         }
8681 
8682         for (pass = 0; pass < maxpass; pass++) {
8683             TCGv_i32 tcg_op = tcg_temp_new_i32();
8684 
8685             read_vec_element_i32(s, tcg_op, rn, pass, size);
8686             fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8687             if (is_scalar) {
8688                 write_fp_sreg(s, rd, tcg_op);
8689             } else {
8690                 write_vec_element_i32(s, tcg_op, rd, pass, size);
8691             }
8692         }
8693         if (!is_scalar) {
8694             clear_vec_high(s, is_q, rd);
8695         }
8696     }
8697 
8698     gen_restore_rmode(tcg_rmode, tcg_fpstatus);
8699 }
8700 
8701 /* AdvSIMD scalar shift by immediate
8702  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
8703  * +-----+---+-------------+------+------+--------+---+------+------+
8704  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8705  * +-----+---+-------------+------+------+--------+---+------+------+
8706  *
8707  * This is the scalar version so it works on a fixed sized registers
8708  */
8709 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8710 {
8711     int rd = extract32(insn, 0, 5);
8712     int rn = extract32(insn, 5, 5);
8713     int opcode = extract32(insn, 11, 5);
8714     int immb = extract32(insn, 16, 3);
8715     int immh = extract32(insn, 19, 4);
8716     bool is_u = extract32(insn, 29, 1);
8717 
8718     if (immh == 0) {
8719         unallocated_encoding(s);
8720         return;
8721     }
8722 
8723     switch (opcode) {
8724     case 0x08: /* SRI */
8725         if (!is_u) {
8726             unallocated_encoding(s);
8727             return;
8728         }
8729         /* fall through */
8730     case 0x00: /* SSHR / USHR */
8731     case 0x02: /* SSRA / USRA */
8732     case 0x04: /* SRSHR / URSHR */
8733     case 0x06: /* SRSRA / URSRA */
8734         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8735         break;
8736     case 0x0a: /* SHL / SLI */
8737         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8738         break;
8739     case 0x1c: /* SCVTF, UCVTF */
8740         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8741                                      opcode, rn, rd);
8742         break;
8743     case 0x10: /* SQSHRUN, SQSHRUN2 */
8744     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8745         if (!is_u) {
8746             unallocated_encoding(s);
8747             return;
8748         }
8749         handle_vec_simd_sqshrn(s, true, false, false, true,
8750                                immh, immb, opcode, rn, rd);
8751         break;
8752     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8753     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8754         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8755                                immh, immb, opcode, rn, rd);
8756         break;
8757     case 0xc: /* SQSHLU */
8758         if (!is_u) {
8759             unallocated_encoding(s);
8760             return;
8761         }
8762         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8763         break;
8764     case 0xe: /* SQSHL, UQSHL */
8765         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8766         break;
8767     case 0x1f: /* FCVTZS, FCVTZU */
8768         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8769         break;
8770     default:
8771         unallocated_encoding(s);
8772         break;
8773     }
8774 }
8775 
8776 /* AdvSIMD scalar three different
8777  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
8778  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8779  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
8780  * +-----+---+-----------+------+---+------+--------+-----+------+------+
8781  */
8782 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8783 {
8784     bool is_u = extract32(insn, 29, 1);
8785     int size = extract32(insn, 22, 2);
8786     int opcode = extract32(insn, 12, 4);
8787     int rm = extract32(insn, 16, 5);
8788     int rn = extract32(insn, 5, 5);
8789     int rd = extract32(insn, 0, 5);
8790 
8791     if (is_u) {
8792         unallocated_encoding(s);
8793         return;
8794     }
8795 
8796     switch (opcode) {
8797     case 0x9: /* SQDMLAL, SQDMLAL2 */
8798     case 0xb: /* SQDMLSL, SQDMLSL2 */
8799     case 0xd: /* SQDMULL, SQDMULL2 */
8800         if (size == 0 || size == 3) {
8801             unallocated_encoding(s);
8802             return;
8803         }
8804         break;
8805     default:
8806         unallocated_encoding(s);
8807         return;
8808     }
8809 
8810     if (!fp_access_check(s)) {
8811         return;
8812     }
8813 
8814     if (size == 2) {
8815         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8816         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8817         TCGv_i64 tcg_res = tcg_temp_new_i64();
8818 
8819         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8820         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8821 
8822         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8823         gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
8824 
8825         switch (opcode) {
8826         case 0xd: /* SQDMULL, SQDMULL2 */
8827             break;
8828         case 0xb: /* SQDMLSL, SQDMLSL2 */
8829             tcg_gen_neg_i64(tcg_res, tcg_res);
8830             /* fall through */
8831         case 0x9: /* SQDMLAL, SQDMLAL2 */
8832             read_vec_element(s, tcg_op1, rd, 0, MO_64);
8833             gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
8834                                               tcg_res, tcg_op1);
8835             break;
8836         default:
8837             g_assert_not_reached();
8838         }
8839 
8840         write_fp_dreg(s, rd, tcg_res);
8841     } else {
8842         TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8843         TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8844         TCGv_i64 tcg_res = tcg_temp_new_i64();
8845 
8846         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8847         gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
8848 
8849         switch (opcode) {
8850         case 0xd: /* SQDMULL, SQDMULL2 */
8851             break;
8852         case 0xb: /* SQDMLSL, SQDMLSL2 */
8853             gen_helper_neon_negl_u32(tcg_res, tcg_res);
8854             /* fall through */
8855         case 0x9: /* SQDMLAL, SQDMLAL2 */
8856         {
8857             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
8858             read_vec_element(s, tcg_op3, rd, 0, MO_32);
8859             gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
8860                                               tcg_res, tcg_op3);
8861             break;
8862         }
8863         default:
8864             g_assert_not_reached();
8865         }
8866 
8867         tcg_gen_ext32u_i64(tcg_res, tcg_res);
8868         write_fp_dreg(s, rd, tcg_res);
8869     }
8870 }
8871 
8872 static void handle_3same_64(DisasContext *s, int opcode, bool u,
8873                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
8874 {
8875     /* Handle 64x64->64 opcodes which are shared between the scalar
8876      * and vector 3-same groups. We cover every opcode where size == 3
8877      * is valid in either the three-reg-same (integer, not pairwise)
8878      * or scalar-three-reg-same groups.
8879      */
8880     TCGCond cond;
8881 
8882     switch (opcode) {
8883     case 0x1: /* SQADD */
8884         if (u) {
8885             gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8886         } else {
8887             gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8888         }
8889         break;
8890     case 0x5: /* SQSUB */
8891         if (u) {
8892             gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8893         } else {
8894             gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8895         }
8896         break;
8897     case 0x6: /* CMGT, CMHI */
8898         /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
8899          * We implement this using setcond (test) and then negating.
8900          */
8901         cond = u ? TCG_COND_GTU : TCG_COND_GT;
8902     do_cmop:
8903         tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
8904         tcg_gen_neg_i64(tcg_rd, tcg_rd);
8905         break;
8906     case 0x7: /* CMGE, CMHS */
8907         cond = u ? TCG_COND_GEU : TCG_COND_GE;
8908         goto do_cmop;
8909     case 0x11: /* CMTST, CMEQ */
8910         if (u) {
8911             cond = TCG_COND_EQ;
8912             goto do_cmop;
8913         }
8914         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
8915         break;
8916     case 0x8: /* SSHL, USHL */
8917         if (u) {
8918             gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
8919         } else {
8920             gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
8921         }
8922         break;
8923     case 0x9: /* SQSHL, UQSHL */
8924         if (u) {
8925             gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8926         } else {
8927             gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8928         }
8929         break;
8930     case 0xa: /* SRSHL, URSHL */
8931         if (u) {
8932             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
8933         } else {
8934             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
8935         }
8936         break;
8937     case 0xb: /* SQRSHL, UQRSHL */
8938         if (u) {
8939             gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8940         } else {
8941             gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8942         }
8943         break;
8944     case 0x10: /* ADD, SUB */
8945         if (u) {
8946             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
8947         } else {
8948             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
8949         }
8950         break;
8951     default:
8952         g_assert_not_reached();
8953     }
8954 }
8955 
8956 /* Handle the 3-same-operands float operations; shared by the scalar
8957  * and vector encodings. The caller must filter out any encodings
8958  * not allocated for the encoding it is dealing with.
8959  */
8960 static void handle_3same_float(DisasContext *s, int size, int elements,
8961                                int fpopcode, int rd, int rn, int rm)
8962 {
8963     int pass;
8964     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
8965 
8966     for (pass = 0; pass < elements; pass++) {
8967         if (size) {
8968             /* Double */
8969             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8970             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8971             TCGv_i64 tcg_res = tcg_temp_new_i64();
8972 
8973             read_vec_element(s, tcg_op1, rn, pass, MO_64);
8974             read_vec_element(s, tcg_op2, rm, pass, MO_64);
8975 
8976             switch (fpopcode) {
8977             case 0x39: /* FMLS */
8978                 /* As usual for ARM, separate negation for fused multiply-add */
8979                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
8980                 /* fall through */
8981             case 0x19: /* FMLA */
8982                 read_vec_element(s, tcg_res, rd, pass, MO_64);
8983                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
8984                                        tcg_res, fpst);
8985                 break;
8986             case 0x18: /* FMAXNM */
8987                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8988                 break;
8989             case 0x1a: /* FADD */
8990                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8991                 break;
8992             case 0x1b: /* FMULX */
8993                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
8994                 break;
8995             case 0x1c: /* FCMEQ */
8996                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8997                 break;
8998             case 0x1e: /* FMAX */
8999                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
9000                 break;
9001             case 0x1f: /* FRECPS */
9002                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9003                 break;
9004             case 0x38: /* FMINNM */
9005                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9006                 break;
9007             case 0x3a: /* FSUB */
9008                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9009                 break;
9010             case 0x3e: /* FMIN */
9011                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
9012                 break;
9013             case 0x3f: /* FRSQRTS */
9014                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9015                 break;
9016             case 0x5b: /* FMUL */
9017                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
9018                 break;
9019             case 0x5c: /* FCMGE */
9020                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9021                 break;
9022             case 0x5d: /* FACGE */
9023                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9024                 break;
9025             case 0x5f: /* FDIV */
9026                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
9027                 break;
9028             case 0x7a: /* FABD */
9029                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9030                 gen_helper_vfp_absd(tcg_res, tcg_res);
9031                 break;
9032             case 0x7c: /* FCMGT */
9033                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9034                 break;
9035             case 0x7d: /* FACGT */
9036                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9037                 break;
9038             default:
9039                 g_assert_not_reached();
9040             }
9041 
9042             write_vec_element(s, tcg_res, rd, pass, MO_64);
9043         } else {
9044             /* Single */
9045             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9046             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9047             TCGv_i32 tcg_res = tcg_temp_new_i32();
9048 
9049             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9050             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9051 
9052             switch (fpopcode) {
9053             case 0x39: /* FMLS */
9054                 /* As usual for ARM, separate negation for fused multiply-add */
9055                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
9056                 /* fall through */
9057             case 0x19: /* FMLA */
9058                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9059                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
9060                                        tcg_res, fpst);
9061                 break;
9062             case 0x1a: /* FADD */
9063                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
9064                 break;
9065             case 0x1b: /* FMULX */
9066                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
9067                 break;
9068             case 0x1c: /* FCMEQ */
9069                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9070                 break;
9071             case 0x1e: /* FMAX */
9072                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
9073                 break;
9074             case 0x1f: /* FRECPS */
9075                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9076                 break;
9077             case 0x18: /* FMAXNM */
9078                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
9079                 break;
9080             case 0x38: /* FMINNM */
9081                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
9082                 break;
9083             case 0x3a: /* FSUB */
9084                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9085                 break;
9086             case 0x3e: /* FMIN */
9087                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9088                 break;
9089             case 0x3f: /* FRSQRTS */
9090                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9091                 break;
9092             case 0x5b: /* FMUL */
9093                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9094                 break;
9095             case 0x5c: /* FCMGE */
9096                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9097                 break;
9098             case 0x5d: /* FACGE */
9099                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9100                 break;
9101             case 0x5f: /* FDIV */
9102                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9103                 break;
9104             case 0x7a: /* FABD */
9105                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9106                 gen_helper_vfp_abss(tcg_res, tcg_res);
9107                 break;
9108             case 0x7c: /* FCMGT */
9109                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9110                 break;
9111             case 0x7d: /* FACGT */
9112                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9113                 break;
9114             default:
9115                 g_assert_not_reached();
9116             }
9117 
9118             if (elements == 1) {
9119                 /* scalar single so clear high part */
9120                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9121 
9122                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9123                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9124             } else {
9125                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9126             }
9127         }
9128     }
9129 
9130     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9131 }
9132 
9133 /* AdvSIMD scalar three same
9134  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
9135  * +-----+---+-----------+------+---+------+--------+---+------+------+
9136  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
9137  * +-----+---+-----------+------+---+------+--------+---+------+------+
9138  */
9139 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9140 {
9141     int rd = extract32(insn, 0, 5);
9142     int rn = extract32(insn, 5, 5);
9143     int opcode = extract32(insn, 11, 5);
9144     int rm = extract32(insn, 16, 5);
9145     int size = extract32(insn, 22, 2);
9146     bool u = extract32(insn, 29, 1);
9147     TCGv_i64 tcg_rd;
9148 
9149     if (opcode >= 0x18) {
9150         /* Floating point: U, size[1] and opcode indicate operation */
9151         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9152         switch (fpopcode) {
9153         case 0x1b: /* FMULX */
9154         case 0x1f: /* FRECPS */
9155         case 0x3f: /* FRSQRTS */
9156         case 0x5d: /* FACGE */
9157         case 0x7d: /* FACGT */
9158         case 0x1c: /* FCMEQ */
9159         case 0x5c: /* FCMGE */
9160         case 0x7c: /* FCMGT */
9161         case 0x7a: /* FABD */
9162             break;
9163         default:
9164             unallocated_encoding(s);
9165             return;
9166         }
9167 
9168         if (!fp_access_check(s)) {
9169             return;
9170         }
9171 
9172         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9173         return;
9174     }
9175 
9176     switch (opcode) {
9177     case 0x1: /* SQADD, UQADD */
9178     case 0x5: /* SQSUB, UQSUB */
9179     case 0x9: /* SQSHL, UQSHL */
9180     case 0xb: /* SQRSHL, UQRSHL */
9181         break;
9182     case 0x8: /* SSHL, USHL */
9183     case 0xa: /* SRSHL, URSHL */
9184     case 0x6: /* CMGT, CMHI */
9185     case 0x7: /* CMGE, CMHS */
9186     case 0x11: /* CMTST, CMEQ */
9187     case 0x10: /* ADD, SUB (vector) */
9188         if (size != 3) {
9189             unallocated_encoding(s);
9190             return;
9191         }
9192         break;
9193     case 0x16: /* SQDMULH, SQRDMULH (vector) */
9194         if (size != 1 && size != 2) {
9195             unallocated_encoding(s);
9196             return;
9197         }
9198         break;
9199     default:
9200         unallocated_encoding(s);
9201         return;
9202     }
9203 
9204     if (!fp_access_check(s)) {
9205         return;
9206     }
9207 
9208     tcg_rd = tcg_temp_new_i64();
9209 
9210     if (size == 3) {
9211         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9212         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9213 
9214         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9215     } else {
9216         /* Do a single operation on the lowest element in the vector.
9217          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9218          * no side effects for all these operations.
9219          * OPTME: special-purpose helpers would avoid doing some
9220          * unnecessary work in the helper for the 8 and 16 bit cases.
9221          */
9222         NeonGenTwoOpEnvFn *genenvfn;
9223         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9224         TCGv_i32 tcg_rm = tcg_temp_new_i32();
9225         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9226 
9227         read_vec_element_i32(s, tcg_rn, rn, 0, size);
9228         read_vec_element_i32(s, tcg_rm, rm, 0, size);
9229 
9230         switch (opcode) {
9231         case 0x1: /* SQADD, UQADD */
9232         {
9233             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9234                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9235                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9236                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9237             };
9238             genenvfn = fns[size][u];
9239             break;
9240         }
9241         case 0x5: /* SQSUB, UQSUB */
9242         {
9243             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9244                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9245                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9246                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9247             };
9248             genenvfn = fns[size][u];
9249             break;
9250         }
9251         case 0x9: /* SQSHL, UQSHL */
9252         {
9253             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9254                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9255                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9256                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9257             };
9258             genenvfn = fns[size][u];
9259             break;
9260         }
9261         case 0xb: /* SQRSHL, UQRSHL */
9262         {
9263             static NeonGenTwoOpEnvFn * const fns[3][2] = {
9264                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9265                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9266                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9267             };
9268             genenvfn = fns[size][u];
9269             break;
9270         }
9271         case 0x16: /* SQDMULH, SQRDMULH */
9272         {
9273             static NeonGenTwoOpEnvFn * const fns[2][2] = {
9274                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9275                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9276             };
9277             assert(size == 1 || size == 2);
9278             genenvfn = fns[size - 1][u];
9279             break;
9280         }
9281         default:
9282             g_assert_not_reached();
9283         }
9284 
9285         genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9286         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9287     }
9288 
9289     write_fp_dreg(s, rd, tcg_rd);
9290 }
9291 
9292 /* AdvSIMD scalar three same FP16
9293  *  31 30  29 28       24 23  22 21 20  16 15 14 13    11 10  9  5 4  0
9294  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9295  * | 0 1 | U | 1 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 | Rn | Rd |
9296  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9297  * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9298  * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9299  */
9300 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9301                                                   uint32_t insn)
9302 {
9303     int rd = extract32(insn, 0, 5);
9304     int rn = extract32(insn, 5, 5);
9305     int opcode = extract32(insn, 11, 3);
9306     int rm = extract32(insn, 16, 5);
9307     bool u = extract32(insn, 29, 1);
9308     bool a = extract32(insn, 23, 1);
9309     int fpopcode = opcode | (a << 3) |  (u << 4);
9310     TCGv_ptr fpst;
9311     TCGv_i32 tcg_op1;
9312     TCGv_i32 tcg_op2;
9313     TCGv_i32 tcg_res;
9314 
9315     switch (fpopcode) {
9316     case 0x03: /* FMULX */
9317     case 0x04: /* FCMEQ (reg) */
9318     case 0x07: /* FRECPS */
9319     case 0x0f: /* FRSQRTS */
9320     case 0x14: /* FCMGE (reg) */
9321     case 0x15: /* FACGE */
9322     case 0x1a: /* FABD */
9323     case 0x1c: /* FCMGT (reg) */
9324     case 0x1d: /* FACGT */
9325         break;
9326     default:
9327         unallocated_encoding(s);
9328         return;
9329     }
9330 
9331     if (!dc_isar_feature(aa64_fp16, s)) {
9332         unallocated_encoding(s);
9333     }
9334 
9335     if (!fp_access_check(s)) {
9336         return;
9337     }
9338 
9339     fpst = fpstatus_ptr(FPST_FPCR_F16);
9340 
9341     tcg_op1 = read_fp_hreg(s, rn);
9342     tcg_op2 = read_fp_hreg(s, rm);
9343     tcg_res = tcg_temp_new_i32();
9344 
9345     switch (fpopcode) {
9346     case 0x03: /* FMULX */
9347         gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9348         break;
9349     case 0x04: /* FCMEQ (reg) */
9350         gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9351         break;
9352     case 0x07: /* FRECPS */
9353         gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9354         break;
9355     case 0x0f: /* FRSQRTS */
9356         gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9357         break;
9358     case 0x14: /* FCMGE (reg) */
9359         gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9360         break;
9361     case 0x15: /* FACGE */
9362         gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9363         break;
9364     case 0x1a: /* FABD */
9365         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9366         tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9367         break;
9368     case 0x1c: /* FCMGT (reg) */
9369         gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9370         break;
9371     case 0x1d: /* FACGT */
9372         gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9373         break;
9374     default:
9375         g_assert_not_reached();
9376     }
9377 
9378     write_fp_sreg(s, rd, tcg_res);
9379 }
9380 
9381 /* AdvSIMD scalar three same extra
9382  *  31 30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
9383  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9384  * | 0 1 | U | 1 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
9385  * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9386  */
9387 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9388                                                    uint32_t insn)
9389 {
9390     int rd = extract32(insn, 0, 5);
9391     int rn = extract32(insn, 5, 5);
9392     int opcode = extract32(insn, 11, 4);
9393     int rm = extract32(insn, 16, 5);
9394     int size = extract32(insn, 22, 2);
9395     bool u = extract32(insn, 29, 1);
9396     TCGv_i32 ele1, ele2, ele3;
9397     TCGv_i64 res;
9398     bool feature;
9399 
9400     switch (u * 16 + opcode) {
9401     case 0x10: /* SQRDMLAH (vector) */
9402     case 0x11: /* SQRDMLSH (vector) */
9403         if (size != 1 && size != 2) {
9404             unallocated_encoding(s);
9405             return;
9406         }
9407         feature = dc_isar_feature(aa64_rdm, s);
9408         break;
9409     default:
9410         unallocated_encoding(s);
9411         return;
9412     }
9413     if (!feature) {
9414         unallocated_encoding(s);
9415         return;
9416     }
9417     if (!fp_access_check(s)) {
9418         return;
9419     }
9420 
9421     /* Do a single operation on the lowest element in the vector.
9422      * We use the standard Neon helpers and rely on 0 OP 0 == 0
9423      * with no side effects for all these operations.
9424      * OPTME: special-purpose helpers would avoid doing some
9425      * unnecessary work in the helper for the 16 bit cases.
9426      */
9427     ele1 = tcg_temp_new_i32();
9428     ele2 = tcg_temp_new_i32();
9429     ele3 = tcg_temp_new_i32();
9430 
9431     read_vec_element_i32(s, ele1, rn, 0, size);
9432     read_vec_element_i32(s, ele2, rm, 0, size);
9433     read_vec_element_i32(s, ele3, rd, 0, size);
9434 
9435     switch (opcode) {
9436     case 0x0: /* SQRDMLAH */
9437         if (size == 1) {
9438             gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9439         } else {
9440             gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9441         }
9442         break;
9443     case 0x1: /* SQRDMLSH */
9444         if (size == 1) {
9445             gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9446         } else {
9447             gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9448         }
9449         break;
9450     default:
9451         g_assert_not_reached();
9452     }
9453 
9454     res = tcg_temp_new_i64();
9455     tcg_gen_extu_i32_i64(res, ele3);
9456     write_fp_dreg(s, rd, res);
9457 }
9458 
9459 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9460                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9461                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9462 {
9463     /* Handle 64->64 opcodes which are shared between the scalar and
9464      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9465      * is valid in either group and also the double-precision fp ops.
9466      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9467      * requires them.
9468      */
9469     TCGCond cond;
9470 
9471     switch (opcode) {
9472     case 0x4: /* CLS, CLZ */
9473         if (u) {
9474             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9475         } else {
9476             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9477         }
9478         break;
9479     case 0x5: /* NOT */
9480         /* This opcode is shared with CNT and RBIT but we have earlier
9481          * enforced that size == 3 if and only if this is the NOT insn.
9482          */
9483         tcg_gen_not_i64(tcg_rd, tcg_rn);
9484         break;
9485     case 0x7: /* SQABS, SQNEG */
9486         if (u) {
9487             gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9488         } else {
9489             gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9490         }
9491         break;
9492     case 0xa: /* CMLT */
9493         /* 64 bit integer comparison against zero, result is
9494          * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9495          * subtracting 1.
9496          */
9497         cond = TCG_COND_LT;
9498     do_cmop:
9499         tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9500         tcg_gen_neg_i64(tcg_rd, tcg_rd);
9501         break;
9502     case 0x8: /* CMGT, CMGE */
9503         cond = u ? TCG_COND_GE : TCG_COND_GT;
9504         goto do_cmop;
9505     case 0x9: /* CMEQ, CMLE */
9506         cond = u ? TCG_COND_LE : TCG_COND_EQ;
9507         goto do_cmop;
9508     case 0xb: /* ABS, NEG */
9509         if (u) {
9510             tcg_gen_neg_i64(tcg_rd, tcg_rn);
9511         } else {
9512             tcg_gen_abs_i64(tcg_rd, tcg_rn);
9513         }
9514         break;
9515     case 0x2f: /* FABS */
9516         gen_helper_vfp_absd(tcg_rd, tcg_rn);
9517         break;
9518     case 0x6f: /* FNEG */
9519         gen_helper_vfp_negd(tcg_rd, tcg_rn);
9520         break;
9521     case 0x7f: /* FSQRT */
9522         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9523         break;
9524     case 0x1a: /* FCVTNS */
9525     case 0x1b: /* FCVTMS */
9526     case 0x1c: /* FCVTAS */
9527     case 0x3a: /* FCVTPS */
9528     case 0x3b: /* FCVTZS */
9529         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9530         break;
9531     case 0x5a: /* FCVTNU */
9532     case 0x5b: /* FCVTMU */
9533     case 0x5c: /* FCVTAU */
9534     case 0x7a: /* FCVTPU */
9535     case 0x7b: /* FCVTZU */
9536         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9537         break;
9538     case 0x18: /* FRINTN */
9539     case 0x19: /* FRINTM */
9540     case 0x38: /* FRINTP */
9541     case 0x39: /* FRINTZ */
9542     case 0x58: /* FRINTA */
9543     case 0x79: /* FRINTI */
9544         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9545         break;
9546     case 0x59: /* FRINTX */
9547         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9548         break;
9549     case 0x1e: /* FRINT32Z */
9550     case 0x5e: /* FRINT32X */
9551         gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9552         break;
9553     case 0x1f: /* FRINT64Z */
9554     case 0x5f: /* FRINT64X */
9555         gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9556         break;
9557     default:
9558         g_assert_not_reached();
9559     }
9560 }
9561 
9562 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9563                                    bool is_scalar, bool is_u, bool is_q,
9564                                    int size, int rn, int rd)
9565 {
9566     bool is_double = (size == MO_64);
9567     TCGv_ptr fpst;
9568 
9569     if (!fp_access_check(s)) {
9570         return;
9571     }
9572 
9573     fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9574 
9575     if (is_double) {
9576         TCGv_i64 tcg_op = tcg_temp_new_i64();
9577         TCGv_i64 tcg_zero = tcg_constant_i64(0);
9578         TCGv_i64 tcg_res = tcg_temp_new_i64();
9579         NeonGenTwoDoubleOpFn *genfn;
9580         bool swap = false;
9581         int pass;
9582 
9583         switch (opcode) {
9584         case 0x2e: /* FCMLT (zero) */
9585             swap = true;
9586             /* fallthrough */
9587         case 0x2c: /* FCMGT (zero) */
9588             genfn = gen_helper_neon_cgt_f64;
9589             break;
9590         case 0x2d: /* FCMEQ (zero) */
9591             genfn = gen_helper_neon_ceq_f64;
9592             break;
9593         case 0x6d: /* FCMLE (zero) */
9594             swap = true;
9595             /* fall through */
9596         case 0x6c: /* FCMGE (zero) */
9597             genfn = gen_helper_neon_cge_f64;
9598             break;
9599         default:
9600             g_assert_not_reached();
9601         }
9602 
9603         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9604             read_vec_element(s, tcg_op, rn, pass, MO_64);
9605             if (swap) {
9606                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9607             } else {
9608                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9609             }
9610             write_vec_element(s, tcg_res, rd, pass, MO_64);
9611         }
9612 
9613         clear_vec_high(s, !is_scalar, rd);
9614     } else {
9615         TCGv_i32 tcg_op = tcg_temp_new_i32();
9616         TCGv_i32 tcg_zero = tcg_constant_i32(0);
9617         TCGv_i32 tcg_res = tcg_temp_new_i32();
9618         NeonGenTwoSingleOpFn *genfn;
9619         bool swap = false;
9620         int pass, maxpasses;
9621 
9622         if (size == MO_16) {
9623             switch (opcode) {
9624             case 0x2e: /* FCMLT (zero) */
9625                 swap = true;
9626                 /* fall through */
9627             case 0x2c: /* FCMGT (zero) */
9628                 genfn = gen_helper_advsimd_cgt_f16;
9629                 break;
9630             case 0x2d: /* FCMEQ (zero) */
9631                 genfn = gen_helper_advsimd_ceq_f16;
9632                 break;
9633             case 0x6d: /* FCMLE (zero) */
9634                 swap = true;
9635                 /* fall through */
9636             case 0x6c: /* FCMGE (zero) */
9637                 genfn = gen_helper_advsimd_cge_f16;
9638                 break;
9639             default:
9640                 g_assert_not_reached();
9641             }
9642         } else {
9643             switch (opcode) {
9644             case 0x2e: /* FCMLT (zero) */
9645                 swap = true;
9646                 /* fall through */
9647             case 0x2c: /* FCMGT (zero) */
9648                 genfn = gen_helper_neon_cgt_f32;
9649                 break;
9650             case 0x2d: /* FCMEQ (zero) */
9651                 genfn = gen_helper_neon_ceq_f32;
9652                 break;
9653             case 0x6d: /* FCMLE (zero) */
9654                 swap = true;
9655                 /* fall through */
9656             case 0x6c: /* FCMGE (zero) */
9657                 genfn = gen_helper_neon_cge_f32;
9658                 break;
9659             default:
9660                 g_assert_not_reached();
9661             }
9662         }
9663 
9664         if (is_scalar) {
9665             maxpasses = 1;
9666         } else {
9667             int vector_size = 8 << is_q;
9668             maxpasses = vector_size >> size;
9669         }
9670 
9671         for (pass = 0; pass < maxpasses; pass++) {
9672             read_vec_element_i32(s, tcg_op, rn, pass, size);
9673             if (swap) {
9674                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9675             } else {
9676                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9677             }
9678             if (is_scalar) {
9679                 write_fp_sreg(s, rd, tcg_res);
9680             } else {
9681                 write_vec_element_i32(s, tcg_res, rd, pass, size);
9682             }
9683         }
9684 
9685         if (!is_scalar) {
9686             clear_vec_high(s, is_q, rd);
9687         }
9688     }
9689 }
9690 
9691 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9692                                     bool is_scalar, bool is_u, bool is_q,
9693                                     int size, int rn, int rd)
9694 {
9695     bool is_double = (size == 3);
9696     TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9697 
9698     if (is_double) {
9699         TCGv_i64 tcg_op = tcg_temp_new_i64();
9700         TCGv_i64 tcg_res = tcg_temp_new_i64();
9701         int pass;
9702 
9703         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9704             read_vec_element(s, tcg_op, rn, pass, MO_64);
9705             switch (opcode) {
9706             case 0x3d: /* FRECPE */
9707                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9708                 break;
9709             case 0x3f: /* FRECPX */
9710                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9711                 break;
9712             case 0x7d: /* FRSQRTE */
9713                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9714                 break;
9715             default:
9716                 g_assert_not_reached();
9717             }
9718             write_vec_element(s, tcg_res, rd, pass, MO_64);
9719         }
9720         clear_vec_high(s, !is_scalar, rd);
9721     } else {
9722         TCGv_i32 tcg_op = tcg_temp_new_i32();
9723         TCGv_i32 tcg_res = tcg_temp_new_i32();
9724         int pass, maxpasses;
9725 
9726         if (is_scalar) {
9727             maxpasses = 1;
9728         } else {
9729             maxpasses = is_q ? 4 : 2;
9730         }
9731 
9732         for (pass = 0; pass < maxpasses; pass++) {
9733             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9734 
9735             switch (opcode) {
9736             case 0x3c: /* URECPE */
9737                 gen_helper_recpe_u32(tcg_res, tcg_op);
9738                 break;
9739             case 0x3d: /* FRECPE */
9740                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9741                 break;
9742             case 0x3f: /* FRECPX */
9743                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9744                 break;
9745             case 0x7d: /* FRSQRTE */
9746                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9747                 break;
9748             default:
9749                 g_assert_not_reached();
9750             }
9751 
9752             if (is_scalar) {
9753                 write_fp_sreg(s, rd, tcg_res);
9754             } else {
9755                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9756             }
9757         }
9758         if (!is_scalar) {
9759             clear_vec_high(s, is_q, rd);
9760         }
9761     }
9762 }
9763 
9764 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9765                                 int opcode, bool u, bool is_q,
9766                                 int size, int rn, int rd)
9767 {
9768     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9769      * in the source becomes a size element in the destination).
9770      */
9771     int pass;
9772     TCGv_i32 tcg_res[2];
9773     int destelt = is_q ? 2 : 0;
9774     int passes = scalar ? 1 : 2;
9775 
9776     if (scalar) {
9777         tcg_res[1] = tcg_constant_i32(0);
9778     }
9779 
9780     for (pass = 0; pass < passes; pass++) {
9781         TCGv_i64 tcg_op = tcg_temp_new_i64();
9782         NeonGenNarrowFn *genfn = NULL;
9783         NeonGenNarrowEnvFn *genenvfn = NULL;
9784 
9785         if (scalar) {
9786             read_vec_element(s, tcg_op, rn, pass, size + 1);
9787         } else {
9788             read_vec_element(s, tcg_op, rn, pass, MO_64);
9789         }
9790         tcg_res[pass] = tcg_temp_new_i32();
9791 
9792         switch (opcode) {
9793         case 0x12: /* XTN, SQXTUN */
9794         {
9795             static NeonGenNarrowFn * const xtnfns[3] = {
9796                 gen_helper_neon_narrow_u8,
9797                 gen_helper_neon_narrow_u16,
9798                 tcg_gen_extrl_i64_i32,
9799             };
9800             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9801                 gen_helper_neon_unarrow_sat8,
9802                 gen_helper_neon_unarrow_sat16,
9803                 gen_helper_neon_unarrow_sat32,
9804             };
9805             if (u) {
9806                 genenvfn = sqxtunfns[size];
9807             } else {
9808                 genfn = xtnfns[size];
9809             }
9810             break;
9811         }
9812         case 0x14: /* SQXTN, UQXTN */
9813         {
9814             static NeonGenNarrowEnvFn * const fns[3][2] = {
9815                 { gen_helper_neon_narrow_sat_s8,
9816                   gen_helper_neon_narrow_sat_u8 },
9817                 { gen_helper_neon_narrow_sat_s16,
9818                   gen_helper_neon_narrow_sat_u16 },
9819                 { gen_helper_neon_narrow_sat_s32,
9820                   gen_helper_neon_narrow_sat_u32 },
9821             };
9822             genenvfn = fns[size][u];
9823             break;
9824         }
9825         case 0x16: /* FCVTN, FCVTN2 */
9826             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9827             if (size == 2) {
9828                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
9829             } else {
9830                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9831                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9832                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9833                 TCGv_i32 ahp = get_ahp_flag();
9834 
9835                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9836                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9837                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9838                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9839             }
9840             break;
9841         case 0x36: /* BFCVTN, BFCVTN2 */
9842             {
9843                 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9844                 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
9845             }
9846             break;
9847         case 0x56:  /* FCVTXN, FCVTXN2 */
9848             /* 64 bit to 32 bit float conversion
9849              * with von Neumann rounding (round to odd)
9850              */
9851             assert(size == 2);
9852             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
9853             break;
9854         default:
9855             g_assert_not_reached();
9856         }
9857 
9858         if (genfn) {
9859             genfn(tcg_res[pass], tcg_op);
9860         } else if (genenvfn) {
9861             genenvfn(tcg_res[pass], cpu_env, tcg_op);
9862         }
9863     }
9864 
9865     for (pass = 0; pass < 2; pass++) {
9866         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
9867     }
9868     clear_vec_high(s, is_q, rd);
9869 }
9870 
9871 /* Remaining saturating accumulating ops */
9872 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
9873                                 bool is_q, int size, int rn, int rd)
9874 {
9875     bool is_double = (size == 3);
9876 
9877     if (is_double) {
9878         TCGv_i64 tcg_rn = tcg_temp_new_i64();
9879         TCGv_i64 tcg_rd = tcg_temp_new_i64();
9880         int pass;
9881 
9882         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9883             read_vec_element(s, tcg_rn, rn, pass, MO_64);
9884             read_vec_element(s, tcg_rd, rd, pass, MO_64);
9885 
9886             if (is_u) { /* USQADD */
9887                 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9888             } else { /* SUQADD */
9889                 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9890             }
9891             write_vec_element(s, tcg_rd, rd, pass, MO_64);
9892         }
9893         clear_vec_high(s, !is_scalar, rd);
9894     } else {
9895         TCGv_i32 tcg_rn = tcg_temp_new_i32();
9896         TCGv_i32 tcg_rd = tcg_temp_new_i32();
9897         int pass, maxpasses;
9898 
9899         if (is_scalar) {
9900             maxpasses = 1;
9901         } else {
9902             maxpasses = is_q ? 4 : 2;
9903         }
9904 
9905         for (pass = 0; pass < maxpasses; pass++) {
9906             if (is_scalar) {
9907                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
9908                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
9909             } else {
9910                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
9911                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9912             }
9913 
9914             if (is_u) { /* USQADD */
9915                 switch (size) {
9916                 case 0:
9917                     gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9918                     break;
9919                 case 1:
9920                     gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9921                     break;
9922                 case 2:
9923                     gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9924                     break;
9925                 default:
9926                     g_assert_not_reached();
9927                 }
9928             } else { /* SUQADD */
9929                 switch (size) {
9930                 case 0:
9931                     gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9932                     break;
9933                 case 1:
9934                     gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9935                     break;
9936                 case 2:
9937                     gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9938                     break;
9939                 default:
9940                     g_assert_not_reached();
9941                 }
9942             }
9943 
9944             if (is_scalar) {
9945                 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
9946             }
9947             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9948         }
9949         clear_vec_high(s, is_q, rd);
9950     }
9951 }
9952 
9953 /* AdvSIMD scalar two reg misc
9954  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
9955  * +-----+---+-----------+------+-----------+--------+-----+------+------+
9956  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
9957  * +-----+---+-----------+------+-----------+--------+-----+------+------+
9958  */
9959 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
9960 {
9961     int rd = extract32(insn, 0, 5);
9962     int rn = extract32(insn, 5, 5);
9963     int opcode = extract32(insn, 12, 5);
9964     int size = extract32(insn, 22, 2);
9965     bool u = extract32(insn, 29, 1);
9966     bool is_fcvt = false;
9967     int rmode;
9968     TCGv_i32 tcg_rmode;
9969     TCGv_ptr tcg_fpstatus;
9970 
9971     switch (opcode) {
9972     case 0x3: /* USQADD / SUQADD*/
9973         if (!fp_access_check(s)) {
9974             return;
9975         }
9976         handle_2misc_satacc(s, true, u, false, size, rn, rd);
9977         return;
9978     case 0x7: /* SQABS / SQNEG */
9979         break;
9980     case 0xa: /* CMLT */
9981         if (u) {
9982             unallocated_encoding(s);
9983             return;
9984         }
9985         /* fall through */
9986     case 0x8: /* CMGT, CMGE */
9987     case 0x9: /* CMEQ, CMLE */
9988     case 0xb: /* ABS, NEG */
9989         if (size != 3) {
9990             unallocated_encoding(s);
9991             return;
9992         }
9993         break;
9994     case 0x12: /* SQXTUN */
9995         if (!u) {
9996             unallocated_encoding(s);
9997             return;
9998         }
9999         /* fall through */
10000     case 0x14: /* SQXTN, UQXTN */
10001         if (size == 3) {
10002             unallocated_encoding(s);
10003             return;
10004         }
10005         if (!fp_access_check(s)) {
10006             return;
10007         }
10008         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10009         return;
10010     case 0xc ... 0xf:
10011     case 0x16 ... 0x1d:
10012     case 0x1f:
10013         /* Floating point: U, size[1] and opcode indicate operation;
10014          * size[0] indicates single or double precision.
10015          */
10016         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10017         size = extract32(size, 0, 1) ? 3 : 2;
10018         switch (opcode) {
10019         case 0x2c: /* FCMGT (zero) */
10020         case 0x2d: /* FCMEQ (zero) */
10021         case 0x2e: /* FCMLT (zero) */
10022         case 0x6c: /* FCMGE (zero) */
10023         case 0x6d: /* FCMLE (zero) */
10024             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10025             return;
10026         case 0x1d: /* SCVTF */
10027         case 0x5d: /* UCVTF */
10028         {
10029             bool is_signed = (opcode == 0x1d);
10030             if (!fp_access_check(s)) {
10031                 return;
10032             }
10033             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10034             return;
10035         }
10036         case 0x3d: /* FRECPE */
10037         case 0x3f: /* FRECPX */
10038         case 0x7d: /* FRSQRTE */
10039             if (!fp_access_check(s)) {
10040                 return;
10041             }
10042             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10043             return;
10044         case 0x1a: /* FCVTNS */
10045         case 0x1b: /* FCVTMS */
10046         case 0x3a: /* FCVTPS */
10047         case 0x3b: /* FCVTZS */
10048         case 0x5a: /* FCVTNU */
10049         case 0x5b: /* FCVTMU */
10050         case 0x7a: /* FCVTPU */
10051         case 0x7b: /* FCVTZU */
10052             is_fcvt = true;
10053             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10054             break;
10055         case 0x1c: /* FCVTAS */
10056         case 0x5c: /* FCVTAU */
10057             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10058             is_fcvt = true;
10059             rmode = FPROUNDING_TIEAWAY;
10060             break;
10061         case 0x56: /* FCVTXN, FCVTXN2 */
10062             if (size == 2) {
10063                 unallocated_encoding(s);
10064                 return;
10065             }
10066             if (!fp_access_check(s)) {
10067                 return;
10068             }
10069             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10070             return;
10071         default:
10072             unallocated_encoding(s);
10073             return;
10074         }
10075         break;
10076     default:
10077         unallocated_encoding(s);
10078         return;
10079     }
10080 
10081     if (!fp_access_check(s)) {
10082         return;
10083     }
10084 
10085     if (is_fcvt) {
10086         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
10087         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
10088     } else {
10089         tcg_fpstatus = NULL;
10090         tcg_rmode = NULL;
10091     }
10092 
10093     if (size == 3) {
10094         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10095         TCGv_i64 tcg_rd = tcg_temp_new_i64();
10096 
10097         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10098         write_fp_dreg(s, rd, tcg_rd);
10099     } else {
10100         TCGv_i32 tcg_rn = tcg_temp_new_i32();
10101         TCGv_i32 tcg_rd = tcg_temp_new_i32();
10102 
10103         read_vec_element_i32(s, tcg_rn, rn, 0, size);
10104 
10105         switch (opcode) {
10106         case 0x7: /* SQABS, SQNEG */
10107         {
10108             NeonGenOneOpEnvFn *genfn;
10109             static NeonGenOneOpEnvFn * const fns[3][2] = {
10110                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10111                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10112                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10113             };
10114             genfn = fns[size][u];
10115             genfn(tcg_rd, cpu_env, tcg_rn);
10116             break;
10117         }
10118         case 0x1a: /* FCVTNS */
10119         case 0x1b: /* FCVTMS */
10120         case 0x1c: /* FCVTAS */
10121         case 0x3a: /* FCVTPS */
10122         case 0x3b: /* FCVTZS */
10123             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10124                                  tcg_fpstatus);
10125             break;
10126         case 0x5a: /* FCVTNU */
10127         case 0x5b: /* FCVTMU */
10128         case 0x5c: /* FCVTAU */
10129         case 0x7a: /* FCVTPU */
10130         case 0x7b: /* FCVTZU */
10131             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10132                                  tcg_fpstatus);
10133             break;
10134         default:
10135             g_assert_not_reached();
10136         }
10137 
10138         write_fp_sreg(s, rd, tcg_rd);
10139     }
10140 
10141     if (is_fcvt) {
10142         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
10143     }
10144 }
10145 
10146 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10147 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10148                                  int immh, int immb, int opcode, int rn, int rd)
10149 {
10150     int size = 32 - clz32(immh) - 1;
10151     int immhb = immh << 3 | immb;
10152     int shift = 2 * (8 << size) - immhb;
10153     GVecGen2iFn *gvec_fn;
10154 
10155     if (extract32(immh, 3, 1) && !is_q) {
10156         unallocated_encoding(s);
10157         return;
10158     }
10159     tcg_debug_assert(size <= 3);
10160 
10161     if (!fp_access_check(s)) {
10162         return;
10163     }
10164 
10165     switch (opcode) {
10166     case 0x02: /* SSRA / USRA (accumulate) */
10167         gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10168         break;
10169 
10170     case 0x08: /* SRI */
10171         gvec_fn = gen_gvec_sri;
10172         break;
10173 
10174     case 0x00: /* SSHR / USHR */
10175         if (is_u) {
10176             if (shift == 8 << size) {
10177                 /* Shift count the same size as element size produces zero.  */
10178                 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10179                                      is_q ? 16 : 8, vec_full_reg_size(s), 0);
10180                 return;
10181             }
10182             gvec_fn = tcg_gen_gvec_shri;
10183         } else {
10184             /* Shift count the same size as element size produces all sign.  */
10185             if (shift == 8 << size) {
10186                 shift -= 1;
10187             }
10188             gvec_fn = tcg_gen_gvec_sari;
10189         }
10190         break;
10191 
10192     case 0x04: /* SRSHR / URSHR (rounding) */
10193         gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10194         break;
10195 
10196     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10197         gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10198         break;
10199 
10200     default:
10201         g_assert_not_reached();
10202     }
10203 
10204     gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10205 }
10206 
10207 /* SHL/SLI - Vector shift left */
10208 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10209                                  int immh, int immb, int opcode, int rn, int rd)
10210 {
10211     int size = 32 - clz32(immh) - 1;
10212     int immhb = immh << 3 | immb;
10213     int shift = immhb - (8 << size);
10214 
10215     /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10216     assert(size >= 0 && size <= 3);
10217 
10218     if (extract32(immh, 3, 1) && !is_q) {
10219         unallocated_encoding(s);
10220         return;
10221     }
10222 
10223     if (!fp_access_check(s)) {
10224         return;
10225     }
10226 
10227     if (insert) {
10228         gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10229     } else {
10230         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10231     }
10232 }
10233 
10234 /* USHLL/SHLL - Vector shift left with widening */
10235 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10236                                  int immh, int immb, int opcode, int rn, int rd)
10237 {
10238     int size = 32 - clz32(immh) - 1;
10239     int immhb = immh << 3 | immb;
10240     int shift = immhb - (8 << size);
10241     int dsize = 64;
10242     int esize = 8 << size;
10243     int elements = dsize/esize;
10244     TCGv_i64 tcg_rn = tcg_temp_new_i64();
10245     TCGv_i64 tcg_rd = tcg_temp_new_i64();
10246     int i;
10247 
10248     if (size >= 3) {
10249         unallocated_encoding(s);
10250         return;
10251     }
10252 
10253     if (!fp_access_check(s)) {
10254         return;
10255     }
10256 
10257     /* For the LL variants the store is larger than the load,
10258      * so if rd == rn we would overwrite parts of our input.
10259      * So load everything right now and use shifts in the main loop.
10260      */
10261     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10262 
10263     for (i = 0; i < elements; i++) {
10264         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10265         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10266         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10267         write_vec_element(s, tcg_rd, rd, i, size + 1);
10268     }
10269 }
10270 
10271 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10272 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10273                                  int immh, int immb, int opcode, int rn, int rd)
10274 {
10275     int immhb = immh << 3 | immb;
10276     int size = 32 - clz32(immh) - 1;
10277     int dsize = 64;
10278     int esize = 8 << size;
10279     int elements = dsize/esize;
10280     int shift = (2 * esize) - immhb;
10281     bool round = extract32(opcode, 0, 1);
10282     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10283     TCGv_i64 tcg_round;
10284     int i;
10285 
10286     if (extract32(immh, 3, 1)) {
10287         unallocated_encoding(s);
10288         return;
10289     }
10290 
10291     if (!fp_access_check(s)) {
10292         return;
10293     }
10294 
10295     tcg_rn = tcg_temp_new_i64();
10296     tcg_rd = tcg_temp_new_i64();
10297     tcg_final = tcg_temp_new_i64();
10298     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10299 
10300     if (round) {
10301         tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10302     } else {
10303         tcg_round = NULL;
10304     }
10305 
10306     for (i = 0; i < elements; i++) {
10307         read_vec_element(s, tcg_rn, rn, i, size+1);
10308         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10309                                 false, true, size+1, shift);
10310 
10311         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10312     }
10313 
10314     if (!is_q) {
10315         write_vec_element(s, tcg_final, rd, 0, MO_64);
10316     } else {
10317         write_vec_element(s, tcg_final, rd, 1, MO_64);
10318     }
10319 
10320     clear_vec_high(s, is_q, rd);
10321 }
10322 
10323 
10324 /* AdvSIMD shift by immediate
10325  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
10326  * +---+---+---+-------------+------+------+--------+---+------+------+
10327  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
10328  * +---+---+---+-------------+------+------+--------+---+------+------+
10329  */
10330 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10331 {
10332     int rd = extract32(insn, 0, 5);
10333     int rn = extract32(insn, 5, 5);
10334     int opcode = extract32(insn, 11, 5);
10335     int immb = extract32(insn, 16, 3);
10336     int immh = extract32(insn, 19, 4);
10337     bool is_u = extract32(insn, 29, 1);
10338     bool is_q = extract32(insn, 30, 1);
10339 
10340     /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10341     assert(immh != 0);
10342 
10343     switch (opcode) {
10344     case 0x08: /* SRI */
10345         if (!is_u) {
10346             unallocated_encoding(s);
10347             return;
10348         }
10349         /* fall through */
10350     case 0x00: /* SSHR / USHR */
10351     case 0x02: /* SSRA / USRA (accumulate) */
10352     case 0x04: /* SRSHR / URSHR (rounding) */
10353     case 0x06: /* SRSRA / URSRA (accum + rounding) */
10354         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10355         break;
10356     case 0x0a: /* SHL / SLI */
10357         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10358         break;
10359     case 0x10: /* SHRN */
10360     case 0x11: /* RSHRN / SQRSHRUN */
10361         if (is_u) {
10362             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10363                                    opcode, rn, rd);
10364         } else {
10365             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10366         }
10367         break;
10368     case 0x12: /* SQSHRN / UQSHRN */
10369     case 0x13: /* SQRSHRN / UQRSHRN */
10370         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10371                                opcode, rn, rd);
10372         break;
10373     case 0x14: /* SSHLL / USHLL */
10374         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10375         break;
10376     case 0x1c: /* SCVTF / UCVTF */
10377         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10378                                      opcode, rn, rd);
10379         break;
10380     case 0xc: /* SQSHLU */
10381         if (!is_u) {
10382             unallocated_encoding(s);
10383             return;
10384         }
10385         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10386         break;
10387     case 0xe: /* SQSHL, UQSHL */
10388         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10389         break;
10390     case 0x1f: /* FCVTZS/ FCVTZU */
10391         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10392         return;
10393     default:
10394         unallocated_encoding(s);
10395         return;
10396     }
10397 }
10398 
10399 /* Generate code to do a "long" addition or subtraction, ie one done in
10400  * TCGv_i64 on vector lanes twice the width specified by size.
10401  */
10402 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10403                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10404 {
10405     static NeonGenTwo64OpFn * const fns[3][2] = {
10406         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10407         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10408         { tcg_gen_add_i64, tcg_gen_sub_i64 },
10409     };
10410     NeonGenTwo64OpFn *genfn;
10411     assert(size < 3);
10412 
10413     genfn = fns[size][is_sub];
10414     genfn(tcg_res, tcg_op1, tcg_op2);
10415 }
10416 
10417 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10418                                 int opcode, int rd, int rn, int rm)
10419 {
10420     /* 3-reg-different widening insns: 64 x 64 -> 128 */
10421     TCGv_i64 tcg_res[2];
10422     int pass, accop;
10423 
10424     tcg_res[0] = tcg_temp_new_i64();
10425     tcg_res[1] = tcg_temp_new_i64();
10426 
10427     /* Does this op do an adding accumulate, a subtracting accumulate,
10428      * or no accumulate at all?
10429      */
10430     switch (opcode) {
10431     case 5:
10432     case 8:
10433     case 9:
10434         accop = 1;
10435         break;
10436     case 10:
10437     case 11:
10438         accop = -1;
10439         break;
10440     default:
10441         accop = 0;
10442         break;
10443     }
10444 
10445     if (accop != 0) {
10446         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10447         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10448     }
10449 
10450     /* size == 2 means two 32x32->64 operations; this is worth special
10451      * casing because we can generally handle it inline.
10452      */
10453     if (size == 2) {
10454         for (pass = 0; pass < 2; pass++) {
10455             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10456             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10457             TCGv_i64 tcg_passres;
10458             MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10459 
10460             int elt = pass + is_q * 2;
10461 
10462             read_vec_element(s, tcg_op1, rn, elt, memop);
10463             read_vec_element(s, tcg_op2, rm, elt, memop);
10464 
10465             if (accop == 0) {
10466                 tcg_passres = tcg_res[pass];
10467             } else {
10468                 tcg_passres = tcg_temp_new_i64();
10469             }
10470 
10471             switch (opcode) {
10472             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10473                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10474                 break;
10475             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10476                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10477                 break;
10478             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10479             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10480             {
10481                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10482                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10483 
10484                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10485                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10486                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10487                                     tcg_passres,
10488                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10489                 break;
10490             }
10491             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10492             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10493             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10494                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10495                 break;
10496             case 9: /* SQDMLAL, SQDMLAL2 */
10497             case 11: /* SQDMLSL, SQDMLSL2 */
10498             case 13: /* SQDMULL, SQDMULL2 */
10499                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10500                 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10501                                                   tcg_passres, tcg_passres);
10502                 break;
10503             default:
10504                 g_assert_not_reached();
10505             }
10506 
10507             if (opcode == 9 || opcode == 11) {
10508                 /* saturating accumulate ops */
10509                 if (accop < 0) {
10510                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10511                 }
10512                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10513                                                   tcg_res[pass], tcg_passres);
10514             } else if (accop > 0) {
10515                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10516             } else if (accop < 0) {
10517                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10518             }
10519         }
10520     } else {
10521         /* size 0 or 1, generally helper functions */
10522         for (pass = 0; pass < 2; pass++) {
10523             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10524             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10525             TCGv_i64 tcg_passres;
10526             int elt = pass + is_q * 2;
10527 
10528             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10529             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10530 
10531             if (accop == 0) {
10532                 tcg_passres = tcg_res[pass];
10533             } else {
10534                 tcg_passres = tcg_temp_new_i64();
10535             }
10536 
10537             switch (opcode) {
10538             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10539             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10540             {
10541                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10542                 static NeonGenWidenFn * const widenfns[2][2] = {
10543                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10544                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10545                 };
10546                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10547 
10548                 widenfn(tcg_op2_64, tcg_op2);
10549                 widenfn(tcg_passres, tcg_op1);
10550                 gen_neon_addl(size, (opcode == 2), tcg_passres,
10551                               tcg_passres, tcg_op2_64);
10552                 break;
10553             }
10554             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10555             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10556                 if (size == 0) {
10557                     if (is_u) {
10558                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10559                     } else {
10560                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10561                     }
10562                 } else {
10563                     if (is_u) {
10564                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10565                     } else {
10566                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10567                     }
10568                 }
10569                 break;
10570             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10571             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10572             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10573                 if (size == 0) {
10574                     if (is_u) {
10575                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10576                     } else {
10577                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10578                     }
10579                 } else {
10580                     if (is_u) {
10581                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10582                     } else {
10583                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10584                     }
10585                 }
10586                 break;
10587             case 9: /* SQDMLAL, SQDMLAL2 */
10588             case 11: /* SQDMLSL, SQDMLSL2 */
10589             case 13: /* SQDMULL, SQDMULL2 */
10590                 assert(size == 1);
10591                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10592                 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10593                                                   tcg_passres, tcg_passres);
10594                 break;
10595             default:
10596                 g_assert_not_reached();
10597             }
10598 
10599             if (accop != 0) {
10600                 if (opcode == 9 || opcode == 11) {
10601                     /* saturating accumulate ops */
10602                     if (accop < 0) {
10603                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10604                     }
10605                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10606                                                       tcg_res[pass],
10607                                                       tcg_passres);
10608                 } else {
10609                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
10610                                   tcg_res[pass], tcg_passres);
10611                 }
10612             }
10613         }
10614     }
10615 
10616     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10617     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10618 }
10619 
10620 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10621                             int opcode, int rd, int rn, int rm)
10622 {
10623     TCGv_i64 tcg_res[2];
10624     int part = is_q ? 2 : 0;
10625     int pass;
10626 
10627     for (pass = 0; pass < 2; pass++) {
10628         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10629         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10630         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10631         static NeonGenWidenFn * const widenfns[3][2] = {
10632             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10633             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10634             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10635         };
10636         NeonGenWidenFn *widenfn = widenfns[size][is_u];
10637 
10638         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10639         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10640         widenfn(tcg_op2_wide, tcg_op2);
10641         tcg_res[pass] = tcg_temp_new_i64();
10642         gen_neon_addl(size, (opcode == 3),
10643                       tcg_res[pass], tcg_op1, tcg_op2_wide);
10644     }
10645 
10646     for (pass = 0; pass < 2; pass++) {
10647         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10648     }
10649 }
10650 
10651 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10652 {
10653     tcg_gen_addi_i64(in, in, 1U << 31);
10654     tcg_gen_extrh_i64_i32(res, in);
10655 }
10656 
10657 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10658                                  int opcode, int rd, int rn, int rm)
10659 {
10660     TCGv_i32 tcg_res[2];
10661     int part = is_q ? 2 : 0;
10662     int pass;
10663 
10664     for (pass = 0; pass < 2; pass++) {
10665         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10666         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10667         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10668         static NeonGenNarrowFn * const narrowfns[3][2] = {
10669             { gen_helper_neon_narrow_high_u8,
10670               gen_helper_neon_narrow_round_high_u8 },
10671             { gen_helper_neon_narrow_high_u16,
10672               gen_helper_neon_narrow_round_high_u16 },
10673             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10674         };
10675         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10676 
10677         read_vec_element(s, tcg_op1, rn, pass, MO_64);
10678         read_vec_element(s, tcg_op2, rm, pass, MO_64);
10679 
10680         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10681 
10682         tcg_res[pass] = tcg_temp_new_i32();
10683         gennarrow(tcg_res[pass], tcg_wideres);
10684     }
10685 
10686     for (pass = 0; pass < 2; pass++) {
10687         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10688     }
10689     clear_vec_high(s, is_q, rd);
10690 }
10691 
10692 /* AdvSIMD three different
10693  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
10694  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10695  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
10696  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10697  */
10698 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10699 {
10700     /* Instructions in this group fall into three basic classes
10701      * (in each case with the operation working on each element in
10702      * the input vectors):
10703      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10704      *     128 bit input)
10705      * (2) wide 64 x 128 -> 128
10706      * (3) narrowing 128 x 128 -> 64
10707      * Here we do initial decode, catch unallocated cases and
10708      * dispatch to separate functions for each class.
10709      */
10710     int is_q = extract32(insn, 30, 1);
10711     int is_u = extract32(insn, 29, 1);
10712     int size = extract32(insn, 22, 2);
10713     int opcode = extract32(insn, 12, 4);
10714     int rm = extract32(insn, 16, 5);
10715     int rn = extract32(insn, 5, 5);
10716     int rd = extract32(insn, 0, 5);
10717 
10718     switch (opcode) {
10719     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10720     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10721         /* 64 x 128 -> 128 */
10722         if (size == 3) {
10723             unallocated_encoding(s);
10724             return;
10725         }
10726         if (!fp_access_check(s)) {
10727             return;
10728         }
10729         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10730         break;
10731     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10732     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10733         /* 128 x 128 -> 64 */
10734         if (size == 3) {
10735             unallocated_encoding(s);
10736             return;
10737         }
10738         if (!fp_access_check(s)) {
10739             return;
10740         }
10741         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10742         break;
10743     case 14: /* PMULL, PMULL2 */
10744         if (is_u) {
10745             unallocated_encoding(s);
10746             return;
10747         }
10748         switch (size) {
10749         case 0: /* PMULL.P8 */
10750             if (!fp_access_check(s)) {
10751                 return;
10752             }
10753             /* The Q field specifies lo/hi half input for this insn.  */
10754             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10755                              gen_helper_neon_pmull_h);
10756             break;
10757 
10758         case 3: /* PMULL.P64 */
10759             if (!dc_isar_feature(aa64_pmull, s)) {
10760                 unallocated_encoding(s);
10761                 return;
10762             }
10763             if (!fp_access_check(s)) {
10764                 return;
10765             }
10766             /* The Q field specifies lo/hi half input for this insn.  */
10767             gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10768                              gen_helper_gvec_pmull_q);
10769             break;
10770 
10771         default:
10772             unallocated_encoding(s);
10773             break;
10774         }
10775         return;
10776     case 9: /* SQDMLAL, SQDMLAL2 */
10777     case 11: /* SQDMLSL, SQDMLSL2 */
10778     case 13: /* SQDMULL, SQDMULL2 */
10779         if (is_u || size == 0) {
10780             unallocated_encoding(s);
10781             return;
10782         }
10783         /* fall through */
10784     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10785     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10786     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10787     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10788     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10789     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10790     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10791         /* 64 x 64 -> 128 */
10792         if (size == 3) {
10793             unallocated_encoding(s);
10794             return;
10795         }
10796         if (!fp_access_check(s)) {
10797             return;
10798         }
10799 
10800         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10801         break;
10802     default:
10803         /* opcode 15 not allocated */
10804         unallocated_encoding(s);
10805         break;
10806     }
10807 }
10808 
10809 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10810 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10811 {
10812     int rd = extract32(insn, 0, 5);
10813     int rn = extract32(insn, 5, 5);
10814     int rm = extract32(insn, 16, 5);
10815     int size = extract32(insn, 22, 2);
10816     bool is_u = extract32(insn, 29, 1);
10817     bool is_q = extract32(insn, 30, 1);
10818 
10819     if (!fp_access_check(s)) {
10820         return;
10821     }
10822 
10823     switch (size + 4 * is_u) {
10824     case 0: /* AND */
10825         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10826         return;
10827     case 1: /* BIC */
10828         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10829         return;
10830     case 2: /* ORR */
10831         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10832         return;
10833     case 3: /* ORN */
10834         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10835         return;
10836     case 4: /* EOR */
10837         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10838         return;
10839 
10840     case 5: /* BSL bitwise select */
10841         gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
10842         return;
10843     case 6: /* BIT, bitwise insert if true */
10844         gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
10845         return;
10846     case 7: /* BIF, bitwise insert if false */
10847         gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
10848         return;
10849 
10850     default:
10851         g_assert_not_reached();
10852     }
10853 }
10854 
10855 /* Pairwise op subgroup of C3.6.16.
10856  *
10857  * This is called directly or via the handle_3same_float for float pairwise
10858  * operations where the opcode and size are calculated differently.
10859  */
10860 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
10861                                    int size, int rn, int rm, int rd)
10862 {
10863     TCGv_ptr fpst;
10864     int pass;
10865 
10866     /* Floating point operations need fpst */
10867     if (opcode >= 0x58) {
10868         fpst = fpstatus_ptr(FPST_FPCR);
10869     } else {
10870         fpst = NULL;
10871     }
10872 
10873     if (!fp_access_check(s)) {
10874         return;
10875     }
10876 
10877     /* These operations work on the concatenated rm:rn, with each pair of
10878      * adjacent elements being operated on to produce an element in the result.
10879      */
10880     if (size == 3) {
10881         TCGv_i64 tcg_res[2];
10882 
10883         for (pass = 0; pass < 2; pass++) {
10884             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10885             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10886             int passreg = (pass == 0) ? rn : rm;
10887 
10888             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
10889             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
10890             tcg_res[pass] = tcg_temp_new_i64();
10891 
10892             switch (opcode) {
10893             case 0x17: /* ADDP */
10894                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
10895                 break;
10896             case 0x58: /* FMAXNMP */
10897                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10898                 break;
10899             case 0x5a: /* FADDP */
10900                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10901                 break;
10902             case 0x5e: /* FMAXP */
10903                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10904                 break;
10905             case 0x78: /* FMINNMP */
10906                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10907                 break;
10908             case 0x7e: /* FMINP */
10909                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10910                 break;
10911             default:
10912                 g_assert_not_reached();
10913             }
10914         }
10915 
10916         for (pass = 0; pass < 2; pass++) {
10917             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10918         }
10919     } else {
10920         int maxpass = is_q ? 4 : 2;
10921         TCGv_i32 tcg_res[4];
10922 
10923         for (pass = 0; pass < maxpass; pass++) {
10924             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10925             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10926             NeonGenTwoOpFn *genfn = NULL;
10927             int passreg = pass < (maxpass / 2) ? rn : rm;
10928             int passelt = (is_q && (pass & 1)) ? 2 : 0;
10929 
10930             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
10931             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
10932             tcg_res[pass] = tcg_temp_new_i32();
10933 
10934             switch (opcode) {
10935             case 0x17: /* ADDP */
10936             {
10937                 static NeonGenTwoOpFn * const fns[3] = {
10938                     gen_helper_neon_padd_u8,
10939                     gen_helper_neon_padd_u16,
10940                     tcg_gen_add_i32,
10941                 };
10942                 genfn = fns[size];
10943                 break;
10944             }
10945             case 0x14: /* SMAXP, UMAXP */
10946             {
10947                 static NeonGenTwoOpFn * const fns[3][2] = {
10948                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
10949                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
10950                     { tcg_gen_smax_i32, tcg_gen_umax_i32 },
10951                 };
10952                 genfn = fns[size][u];
10953                 break;
10954             }
10955             case 0x15: /* SMINP, UMINP */
10956             {
10957                 static NeonGenTwoOpFn * const fns[3][2] = {
10958                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
10959                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
10960                     { tcg_gen_smin_i32, tcg_gen_umin_i32 },
10961                 };
10962                 genfn = fns[size][u];
10963                 break;
10964             }
10965             /* The FP operations are all on single floats (32 bit) */
10966             case 0x58: /* FMAXNMP */
10967                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10968                 break;
10969             case 0x5a: /* FADDP */
10970                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10971                 break;
10972             case 0x5e: /* FMAXP */
10973                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10974                 break;
10975             case 0x78: /* FMINNMP */
10976                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10977                 break;
10978             case 0x7e: /* FMINP */
10979                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10980                 break;
10981             default:
10982                 g_assert_not_reached();
10983             }
10984 
10985             /* FP ops called directly, otherwise call now */
10986             if (genfn) {
10987                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
10988             }
10989         }
10990 
10991         for (pass = 0; pass < maxpass; pass++) {
10992             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
10993         }
10994         clear_vec_high(s, is_q, rd);
10995     }
10996 }
10997 
10998 /* Floating point op subgroup of C3.6.16. */
10999 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
11000 {
11001     /* For floating point ops, the U, size[1] and opcode bits
11002      * together indicate the operation. size[0] indicates single
11003      * or double.
11004      */
11005     int fpopcode = extract32(insn, 11, 5)
11006         | (extract32(insn, 23, 1) << 5)
11007         | (extract32(insn, 29, 1) << 6);
11008     int is_q = extract32(insn, 30, 1);
11009     int size = extract32(insn, 22, 1);
11010     int rm = extract32(insn, 16, 5);
11011     int rn = extract32(insn, 5, 5);
11012     int rd = extract32(insn, 0, 5);
11013 
11014     int datasize = is_q ? 128 : 64;
11015     int esize = 32 << size;
11016     int elements = datasize / esize;
11017 
11018     if (size == 1 && !is_q) {
11019         unallocated_encoding(s);
11020         return;
11021     }
11022 
11023     switch (fpopcode) {
11024     case 0x58: /* FMAXNMP */
11025     case 0x5a: /* FADDP */
11026     case 0x5e: /* FMAXP */
11027     case 0x78: /* FMINNMP */
11028     case 0x7e: /* FMINP */
11029         if (size && !is_q) {
11030             unallocated_encoding(s);
11031             return;
11032         }
11033         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11034                                rn, rm, rd);
11035         return;
11036     case 0x1b: /* FMULX */
11037     case 0x1f: /* FRECPS */
11038     case 0x3f: /* FRSQRTS */
11039     case 0x5d: /* FACGE */
11040     case 0x7d: /* FACGT */
11041     case 0x19: /* FMLA */
11042     case 0x39: /* FMLS */
11043     case 0x18: /* FMAXNM */
11044     case 0x1a: /* FADD */
11045     case 0x1c: /* FCMEQ */
11046     case 0x1e: /* FMAX */
11047     case 0x38: /* FMINNM */
11048     case 0x3a: /* FSUB */
11049     case 0x3e: /* FMIN */
11050     case 0x5b: /* FMUL */
11051     case 0x5c: /* FCMGE */
11052     case 0x5f: /* FDIV */
11053     case 0x7a: /* FABD */
11054     case 0x7c: /* FCMGT */
11055         if (!fp_access_check(s)) {
11056             return;
11057         }
11058         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11059         return;
11060 
11061     case 0x1d: /* FMLAL  */
11062     case 0x3d: /* FMLSL  */
11063     case 0x59: /* FMLAL2 */
11064     case 0x79: /* FMLSL2 */
11065         if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11066             unallocated_encoding(s);
11067             return;
11068         }
11069         if (fp_access_check(s)) {
11070             int is_s = extract32(insn, 23, 1);
11071             int is_2 = extract32(insn, 29, 1);
11072             int data = (is_2 << 1) | is_s;
11073             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11074                                vec_full_reg_offset(s, rn),
11075                                vec_full_reg_offset(s, rm), cpu_env,
11076                                is_q ? 16 : 8, vec_full_reg_size(s),
11077                                data, gen_helper_gvec_fmlal_a64);
11078         }
11079         return;
11080 
11081     default:
11082         unallocated_encoding(s);
11083         return;
11084     }
11085 }
11086 
11087 /* Integer op subgroup of C3.6.16. */
11088 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11089 {
11090     int is_q = extract32(insn, 30, 1);
11091     int u = extract32(insn, 29, 1);
11092     int size = extract32(insn, 22, 2);
11093     int opcode = extract32(insn, 11, 5);
11094     int rm = extract32(insn, 16, 5);
11095     int rn = extract32(insn, 5, 5);
11096     int rd = extract32(insn, 0, 5);
11097     int pass;
11098     TCGCond cond;
11099 
11100     switch (opcode) {
11101     case 0x13: /* MUL, PMUL */
11102         if (u && size != 0) {
11103             unallocated_encoding(s);
11104             return;
11105         }
11106         /* fall through */
11107     case 0x0: /* SHADD, UHADD */
11108     case 0x2: /* SRHADD, URHADD */
11109     case 0x4: /* SHSUB, UHSUB */
11110     case 0xc: /* SMAX, UMAX */
11111     case 0xd: /* SMIN, UMIN */
11112     case 0xe: /* SABD, UABD */
11113     case 0xf: /* SABA, UABA */
11114     case 0x12: /* MLA, MLS */
11115         if (size == 3) {
11116             unallocated_encoding(s);
11117             return;
11118         }
11119         break;
11120     case 0x16: /* SQDMULH, SQRDMULH */
11121         if (size == 0 || size == 3) {
11122             unallocated_encoding(s);
11123             return;
11124         }
11125         break;
11126     default:
11127         if (size == 3 && !is_q) {
11128             unallocated_encoding(s);
11129             return;
11130         }
11131         break;
11132     }
11133 
11134     if (!fp_access_check(s)) {
11135         return;
11136     }
11137 
11138     switch (opcode) {
11139     case 0x01: /* SQADD, UQADD */
11140         if (u) {
11141             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11142         } else {
11143             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11144         }
11145         return;
11146     case 0x05: /* SQSUB, UQSUB */
11147         if (u) {
11148             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11149         } else {
11150             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11151         }
11152         return;
11153     case 0x08: /* SSHL, USHL */
11154         if (u) {
11155             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11156         } else {
11157             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11158         }
11159         return;
11160     case 0x0c: /* SMAX, UMAX */
11161         if (u) {
11162             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11163         } else {
11164             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11165         }
11166         return;
11167     case 0x0d: /* SMIN, UMIN */
11168         if (u) {
11169             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11170         } else {
11171             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11172         }
11173         return;
11174     case 0xe: /* SABD, UABD */
11175         if (u) {
11176             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11177         } else {
11178             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11179         }
11180         return;
11181     case 0xf: /* SABA, UABA */
11182         if (u) {
11183             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11184         } else {
11185             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11186         }
11187         return;
11188     case 0x10: /* ADD, SUB */
11189         if (u) {
11190             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11191         } else {
11192             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11193         }
11194         return;
11195     case 0x13: /* MUL, PMUL */
11196         if (!u) { /* MUL */
11197             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11198         } else {  /* PMUL */
11199             gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11200         }
11201         return;
11202     case 0x12: /* MLA, MLS */
11203         if (u) {
11204             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11205         } else {
11206             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11207         }
11208         return;
11209     case 0x16: /* SQDMULH, SQRDMULH */
11210         {
11211             static gen_helper_gvec_3_ptr * const fns[2][2] = {
11212                 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11213                 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11214             };
11215             gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11216         }
11217         return;
11218     case 0x11:
11219         if (!u) { /* CMTST */
11220             gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11221             return;
11222         }
11223         /* else CMEQ */
11224         cond = TCG_COND_EQ;
11225         goto do_gvec_cmp;
11226     case 0x06: /* CMGT, CMHI */
11227         cond = u ? TCG_COND_GTU : TCG_COND_GT;
11228         goto do_gvec_cmp;
11229     case 0x07: /* CMGE, CMHS */
11230         cond = u ? TCG_COND_GEU : TCG_COND_GE;
11231     do_gvec_cmp:
11232         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11233                          vec_full_reg_offset(s, rn),
11234                          vec_full_reg_offset(s, rm),
11235                          is_q ? 16 : 8, vec_full_reg_size(s));
11236         return;
11237     }
11238 
11239     if (size == 3) {
11240         assert(is_q);
11241         for (pass = 0; pass < 2; pass++) {
11242             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11243             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11244             TCGv_i64 tcg_res = tcg_temp_new_i64();
11245 
11246             read_vec_element(s, tcg_op1, rn, pass, MO_64);
11247             read_vec_element(s, tcg_op2, rm, pass, MO_64);
11248 
11249             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11250 
11251             write_vec_element(s, tcg_res, rd, pass, MO_64);
11252         }
11253     } else {
11254         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11255             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11256             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11257             TCGv_i32 tcg_res = tcg_temp_new_i32();
11258             NeonGenTwoOpFn *genfn = NULL;
11259             NeonGenTwoOpEnvFn *genenvfn = NULL;
11260 
11261             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11262             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11263 
11264             switch (opcode) {
11265             case 0x0: /* SHADD, UHADD */
11266             {
11267                 static NeonGenTwoOpFn * const fns[3][2] = {
11268                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11269                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11270                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11271                 };
11272                 genfn = fns[size][u];
11273                 break;
11274             }
11275             case 0x2: /* SRHADD, URHADD */
11276             {
11277                 static NeonGenTwoOpFn * const fns[3][2] = {
11278                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11279                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11280                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11281                 };
11282                 genfn = fns[size][u];
11283                 break;
11284             }
11285             case 0x4: /* SHSUB, UHSUB */
11286             {
11287                 static NeonGenTwoOpFn * const fns[3][2] = {
11288                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11289                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11290                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11291                 };
11292                 genfn = fns[size][u];
11293                 break;
11294             }
11295             case 0x9: /* SQSHL, UQSHL */
11296             {
11297                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11298                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11299                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11300                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11301                 };
11302                 genenvfn = fns[size][u];
11303                 break;
11304             }
11305             case 0xa: /* SRSHL, URSHL */
11306             {
11307                 static NeonGenTwoOpFn * const fns[3][2] = {
11308                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11309                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11310                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11311                 };
11312                 genfn = fns[size][u];
11313                 break;
11314             }
11315             case 0xb: /* SQRSHL, UQRSHL */
11316             {
11317                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11318                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11319                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11320                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11321                 };
11322                 genenvfn = fns[size][u];
11323                 break;
11324             }
11325             default:
11326                 g_assert_not_reached();
11327             }
11328 
11329             if (genenvfn) {
11330                 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11331             } else {
11332                 genfn(tcg_res, tcg_op1, tcg_op2);
11333             }
11334 
11335             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11336         }
11337     }
11338     clear_vec_high(s, is_q, rd);
11339 }
11340 
11341 /* AdvSIMD three same
11342  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
11343  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11344  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
11345  * +---+---+---+-----------+------+---+------+--------+---+------+------+
11346  */
11347 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11348 {
11349     int opcode = extract32(insn, 11, 5);
11350 
11351     switch (opcode) {
11352     case 0x3: /* logic ops */
11353         disas_simd_3same_logic(s, insn);
11354         break;
11355     case 0x17: /* ADDP */
11356     case 0x14: /* SMAXP, UMAXP */
11357     case 0x15: /* SMINP, UMINP */
11358     {
11359         /* Pairwise operations */
11360         int is_q = extract32(insn, 30, 1);
11361         int u = extract32(insn, 29, 1);
11362         int size = extract32(insn, 22, 2);
11363         int rm = extract32(insn, 16, 5);
11364         int rn = extract32(insn, 5, 5);
11365         int rd = extract32(insn, 0, 5);
11366         if (opcode == 0x17) {
11367             if (u || (size == 3 && !is_q)) {
11368                 unallocated_encoding(s);
11369                 return;
11370             }
11371         } else {
11372             if (size == 3) {
11373                 unallocated_encoding(s);
11374                 return;
11375             }
11376         }
11377         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11378         break;
11379     }
11380     case 0x18 ... 0x31:
11381         /* floating point ops, sz[1] and U are part of opcode */
11382         disas_simd_3same_float(s, insn);
11383         break;
11384     default:
11385         disas_simd_3same_int(s, insn);
11386         break;
11387     }
11388 }
11389 
11390 /*
11391  * Advanced SIMD three same (ARMv8.2 FP16 variants)
11392  *
11393  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
11394  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11395  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
11396  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11397  *
11398  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11399  * (register), FACGE, FABD, FCMGT (register) and FACGT.
11400  *
11401  */
11402 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11403 {
11404     int opcode = extract32(insn, 11, 3);
11405     int u = extract32(insn, 29, 1);
11406     int a = extract32(insn, 23, 1);
11407     int is_q = extract32(insn, 30, 1);
11408     int rm = extract32(insn, 16, 5);
11409     int rn = extract32(insn, 5, 5);
11410     int rd = extract32(insn, 0, 5);
11411     /*
11412      * For these floating point ops, the U, a and opcode bits
11413      * together indicate the operation.
11414      */
11415     int fpopcode = opcode | (a << 3) | (u << 4);
11416     int datasize = is_q ? 128 : 64;
11417     int elements = datasize / 16;
11418     bool pairwise;
11419     TCGv_ptr fpst;
11420     int pass;
11421 
11422     switch (fpopcode) {
11423     case 0x0: /* FMAXNM */
11424     case 0x1: /* FMLA */
11425     case 0x2: /* FADD */
11426     case 0x3: /* FMULX */
11427     case 0x4: /* FCMEQ */
11428     case 0x6: /* FMAX */
11429     case 0x7: /* FRECPS */
11430     case 0x8: /* FMINNM */
11431     case 0x9: /* FMLS */
11432     case 0xa: /* FSUB */
11433     case 0xe: /* FMIN */
11434     case 0xf: /* FRSQRTS */
11435     case 0x13: /* FMUL */
11436     case 0x14: /* FCMGE */
11437     case 0x15: /* FACGE */
11438     case 0x17: /* FDIV */
11439     case 0x1a: /* FABD */
11440     case 0x1c: /* FCMGT */
11441     case 0x1d: /* FACGT */
11442         pairwise = false;
11443         break;
11444     case 0x10: /* FMAXNMP */
11445     case 0x12: /* FADDP */
11446     case 0x16: /* FMAXP */
11447     case 0x18: /* FMINNMP */
11448     case 0x1e: /* FMINP */
11449         pairwise = true;
11450         break;
11451     default:
11452         unallocated_encoding(s);
11453         return;
11454     }
11455 
11456     if (!dc_isar_feature(aa64_fp16, s)) {
11457         unallocated_encoding(s);
11458         return;
11459     }
11460 
11461     if (!fp_access_check(s)) {
11462         return;
11463     }
11464 
11465     fpst = fpstatus_ptr(FPST_FPCR_F16);
11466 
11467     if (pairwise) {
11468         int maxpass = is_q ? 8 : 4;
11469         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11470         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11471         TCGv_i32 tcg_res[8];
11472 
11473         for (pass = 0; pass < maxpass; pass++) {
11474             int passreg = pass < (maxpass / 2) ? rn : rm;
11475             int passelt = (pass << 1) & (maxpass - 1);
11476 
11477             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11478             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11479             tcg_res[pass] = tcg_temp_new_i32();
11480 
11481             switch (fpopcode) {
11482             case 0x10: /* FMAXNMP */
11483                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11484                                            fpst);
11485                 break;
11486             case 0x12: /* FADDP */
11487                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11488                 break;
11489             case 0x16: /* FMAXP */
11490                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11491                 break;
11492             case 0x18: /* FMINNMP */
11493                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11494                                            fpst);
11495                 break;
11496             case 0x1e: /* FMINP */
11497                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11498                 break;
11499             default:
11500                 g_assert_not_reached();
11501             }
11502         }
11503 
11504         for (pass = 0; pass < maxpass; pass++) {
11505             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11506         }
11507     } else {
11508         for (pass = 0; pass < elements; pass++) {
11509             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11510             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11511             TCGv_i32 tcg_res = tcg_temp_new_i32();
11512 
11513             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11514             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11515 
11516             switch (fpopcode) {
11517             case 0x0: /* FMAXNM */
11518                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11519                 break;
11520             case 0x1: /* FMLA */
11521                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11522                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11523                                            fpst);
11524                 break;
11525             case 0x2: /* FADD */
11526                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11527                 break;
11528             case 0x3: /* FMULX */
11529                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11530                 break;
11531             case 0x4: /* FCMEQ */
11532                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11533                 break;
11534             case 0x6: /* FMAX */
11535                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11536                 break;
11537             case 0x7: /* FRECPS */
11538                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11539                 break;
11540             case 0x8: /* FMINNM */
11541                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11542                 break;
11543             case 0x9: /* FMLS */
11544                 /* As usual for ARM, separate negation for fused multiply-add */
11545                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11546                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11547                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11548                                            fpst);
11549                 break;
11550             case 0xa: /* FSUB */
11551                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11552                 break;
11553             case 0xe: /* FMIN */
11554                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11555                 break;
11556             case 0xf: /* FRSQRTS */
11557                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11558                 break;
11559             case 0x13: /* FMUL */
11560                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11561                 break;
11562             case 0x14: /* FCMGE */
11563                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11564                 break;
11565             case 0x15: /* FACGE */
11566                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11567                 break;
11568             case 0x17: /* FDIV */
11569                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11570                 break;
11571             case 0x1a: /* FABD */
11572                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11573                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11574                 break;
11575             case 0x1c: /* FCMGT */
11576                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11577                 break;
11578             case 0x1d: /* FACGT */
11579                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11580                 break;
11581             default:
11582                 g_assert_not_reached();
11583             }
11584 
11585             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11586         }
11587     }
11588 
11589     clear_vec_high(s, is_q, rd);
11590 }
11591 
11592 /* AdvSIMD three same extra
11593  *  31   30  29 28       24 23  22  21 20  16  15 14    11  10 9  5 4  0
11594  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11595  * | 0 | Q | U | 0 1 1 1 0 | size | 0 |  Rm  | 1 | opcode | 1 | Rn | Rd |
11596  * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11597  */
11598 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11599 {
11600     int rd = extract32(insn, 0, 5);
11601     int rn = extract32(insn, 5, 5);
11602     int opcode = extract32(insn, 11, 4);
11603     int rm = extract32(insn, 16, 5);
11604     int size = extract32(insn, 22, 2);
11605     bool u = extract32(insn, 29, 1);
11606     bool is_q = extract32(insn, 30, 1);
11607     bool feature;
11608     int rot;
11609 
11610     switch (u * 16 + opcode) {
11611     case 0x10: /* SQRDMLAH (vector) */
11612     case 0x11: /* SQRDMLSH (vector) */
11613         if (size != 1 && size != 2) {
11614             unallocated_encoding(s);
11615             return;
11616         }
11617         feature = dc_isar_feature(aa64_rdm, s);
11618         break;
11619     case 0x02: /* SDOT (vector) */
11620     case 0x12: /* UDOT (vector) */
11621         if (size != MO_32) {
11622             unallocated_encoding(s);
11623             return;
11624         }
11625         feature = dc_isar_feature(aa64_dp, s);
11626         break;
11627     case 0x03: /* USDOT */
11628         if (size != MO_32) {
11629             unallocated_encoding(s);
11630             return;
11631         }
11632         feature = dc_isar_feature(aa64_i8mm, s);
11633         break;
11634     case 0x04: /* SMMLA */
11635     case 0x14: /* UMMLA */
11636     case 0x05: /* USMMLA */
11637         if (!is_q || size != MO_32) {
11638             unallocated_encoding(s);
11639             return;
11640         }
11641         feature = dc_isar_feature(aa64_i8mm, s);
11642         break;
11643     case 0x18: /* FCMLA, #0 */
11644     case 0x19: /* FCMLA, #90 */
11645     case 0x1a: /* FCMLA, #180 */
11646     case 0x1b: /* FCMLA, #270 */
11647     case 0x1c: /* FCADD, #90 */
11648     case 0x1e: /* FCADD, #270 */
11649         if (size == 0
11650             || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11651             || (size == 3 && !is_q)) {
11652             unallocated_encoding(s);
11653             return;
11654         }
11655         feature = dc_isar_feature(aa64_fcma, s);
11656         break;
11657     case 0x1d: /* BFMMLA */
11658         if (size != MO_16 || !is_q) {
11659             unallocated_encoding(s);
11660             return;
11661         }
11662         feature = dc_isar_feature(aa64_bf16, s);
11663         break;
11664     case 0x1f:
11665         switch (size) {
11666         case 1: /* BFDOT */
11667         case 3: /* BFMLAL{B,T} */
11668             feature = dc_isar_feature(aa64_bf16, s);
11669             break;
11670         default:
11671             unallocated_encoding(s);
11672             return;
11673         }
11674         break;
11675     default:
11676         unallocated_encoding(s);
11677         return;
11678     }
11679     if (!feature) {
11680         unallocated_encoding(s);
11681         return;
11682     }
11683     if (!fp_access_check(s)) {
11684         return;
11685     }
11686 
11687     switch (opcode) {
11688     case 0x0: /* SQRDMLAH (vector) */
11689         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11690         return;
11691 
11692     case 0x1: /* SQRDMLSH (vector) */
11693         gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11694         return;
11695 
11696     case 0x2: /* SDOT / UDOT */
11697         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
11698                          u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11699         return;
11700 
11701     case 0x3: /* USDOT */
11702         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
11703         return;
11704 
11705     case 0x04: /* SMMLA, UMMLA */
11706         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
11707                          u ? gen_helper_gvec_ummla_b
11708                          : gen_helper_gvec_smmla_b);
11709         return;
11710     case 0x05: /* USMMLA */
11711         gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
11712         return;
11713 
11714     case 0x8: /* FCMLA, #0 */
11715     case 0x9: /* FCMLA, #90 */
11716     case 0xa: /* FCMLA, #180 */
11717     case 0xb: /* FCMLA, #270 */
11718         rot = extract32(opcode, 0, 2);
11719         switch (size) {
11720         case 1:
11721             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
11722                               gen_helper_gvec_fcmlah);
11723             break;
11724         case 2:
11725             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11726                               gen_helper_gvec_fcmlas);
11727             break;
11728         case 3:
11729             gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
11730                               gen_helper_gvec_fcmlad);
11731             break;
11732         default:
11733             g_assert_not_reached();
11734         }
11735         return;
11736 
11737     case 0xc: /* FCADD, #90 */
11738     case 0xe: /* FCADD, #270 */
11739         rot = extract32(opcode, 1, 1);
11740         switch (size) {
11741         case 1:
11742             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11743                               gen_helper_gvec_fcaddh);
11744             break;
11745         case 2:
11746             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11747                               gen_helper_gvec_fcadds);
11748             break;
11749         case 3:
11750             gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11751                               gen_helper_gvec_fcaddd);
11752             break;
11753         default:
11754             g_assert_not_reached();
11755         }
11756         return;
11757 
11758     case 0xd: /* BFMMLA */
11759         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
11760         return;
11761     case 0xf:
11762         switch (size) {
11763         case 1: /* BFDOT */
11764             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
11765             break;
11766         case 3: /* BFMLAL{B,T} */
11767             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
11768                               gen_helper_gvec_bfmlal);
11769             break;
11770         default:
11771             g_assert_not_reached();
11772         }
11773         return;
11774 
11775     default:
11776         g_assert_not_reached();
11777     }
11778 }
11779 
11780 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11781                                   int size, int rn, int rd)
11782 {
11783     /* Handle 2-reg-misc ops which are widening (so each size element
11784      * in the source becomes a 2*size element in the destination.
11785      * The only instruction like this is FCVTL.
11786      */
11787     int pass;
11788 
11789     if (size == 3) {
11790         /* 32 -> 64 bit fp conversion */
11791         TCGv_i64 tcg_res[2];
11792         int srcelt = is_q ? 2 : 0;
11793 
11794         for (pass = 0; pass < 2; pass++) {
11795             TCGv_i32 tcg_op = tcg_temp_new_i32();
11796             tcg_res[pass] = tcg_temp_new_i64();
11797 
11798             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11799             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
11800         }
11801         for (pass = 0; pass < 2; pass++) {
11802             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11803         }
11804     } else {
11805         /* 16 -> 32 bit fp conversion */
11806         int srcelt = is_q ? 4 : 0;
11807         TCGv_i32 tcg_res[4];
11808         TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
11809         TCGv_i32 ahp = get_ahp_flag();
11810 
11811         for (pass = 0; pass < 4; pass++) {
11812             tcg_res[pass] = tcg_temp_new_i32();
11813 
11814             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11815             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11816                                            fpst, ahp);
11817         }
11818         for (pass = 0; pass < 4; pass++) {
11819             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11820         }
11821     }
11822 }
11823 
11824 static void handle_rev(DisasContext *s, int opcode, bool u,
11825                        bool is_q, int size, int rn, int rd)
11826 {
11827     int op = (opcode << 1) | u;
11828     int opsz = op + size;
11829     int grp_size = 3 - opsz;
11830     int dsize = is_q ? 128 : 64;
11831     int i;
11832 
11833     if (opsz >= 3) {
11834         unallocated_encoding(s);
11835         return;
11836     }
11837 
11838     if (!fp_access_check(s)) {
11839         return;
11840     }
11841 
11842     if (size == 0) {
11843         /* Special case bytes, use bswap op on each group of elements */
11844         int groups = dsize / (8 << grp_size);
11845 
11846         for (i = 0; i < groups; i++) {
11847             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11848 
11849             read_vec_element(s, tcg_tmp, rn, i, grp_size);
11850             switch (grp_size) {
11851             case MO_16:
11852                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11853                 break;
11854             case MO_32:
11855                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
11856                 break;
11857             case MO_64:
11858                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11859                 break;
11860             default:
11861                 g_assert_not_reached();
11862             }
11863             write_vec_element(s, tcg_tmp, rd, i, grp_size);
11864         }
11865         clear_vec_high(s, is_q, rd);
11866     } else {
11867         int revmask = (1 << grp_size) - 1;
11868         int esize = 8 << size;
11869         int elements = dsize / esize;
11870         TCGv_i64 tcg_rn = tcg_temp_new_i64();
11871         TCGv_i64 tcg_rd[2];
11872 
11873         for (i = 0; i < 2; i++) {
11874             tcg_rd[i] = tcg_temp_new_i64();
11875             tcg_gen_movi_i64(tcg_rd[i], 0);
11876         }
11877 
11878         for (i = 0; i < elements; i++) {
11879             int e_rev = (i & 0xf) ^ revmask;
11880             int w = (e_rev * esize) / 64;
11881             int o = (e_rev * esize) % 64;
11882 
11883             read_vec_element(s, tcg_rn, rn, i, size);
11884             tcg_gen_deposit_i64(tcg_rd[w], tcg_rd[w], tcg_rn, o, esize);
11885         }
11886 
11887         for (i = 0; i < 2; i++) {
11888             write_vec_element(s, tcg_rd[i], rd, i, MO_64);
11889         }
11890         clear_vec_high(s, true, rd);
11891     }
11892 }
11893 
11894 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
11895                                   bool is_q, int size, int rn, int rd)
11896 {
11897     /* Implement the pairwise operations from 2-misc:
11898      * SADDLP, UADDLP, SADALP, UADALP.
11899      * These all add pairs of elements in the input to produce a
11900      * double-width result element in the output (possibly accumulating).
11901      */
11902     bool accum = (opcode == 0x6);
11903     int maxpass = is_q ? 2 : 1;
11904     int pass;
11905     TCGv_i64 tcg_res[2];
11906 
11907     if (size == 2) {
11908         /* 32 + 32 -> 64 op */
11909         MemOp memop = size + (u ? 0 : MO_SIGN);
11910 
11911         for (pass = 0; pass < maxpass; pass++) {
11912             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11913             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11914 
11915             tcg_res[pass] = tcg_temp_new_i64();
11916 
11917             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
11918             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
11919             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11920             if (accum) {
11921                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
11922                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
11923             }
11924         }
11925     } else {
11926         for (pass = 0; pass < maxpass; pass++) {
11927             TCGv_i64 tcg_op = tcg_temp_new_i64();
11928             NeonGenOne64OpFn *genfn;
11929             static NeonGenOne64OpFn * const fns[2][2] = {
11930                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
11931                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
11932             };
11933 
11934             genfn = fns[size][u];
11935 
11936             tcg_res[pass] = tcg_temp_new_i64();
11937 
11938             read_vec_element(s, tcg_op, rn, pass, MO_64);
11939             genfn(tcg_res[pass], tcg_op);
11940 
11941             if (accum) {
11942                 read_vec_element(s, tcg_op, rd, pass, MO_64);
11943                 if (size == 0) {
11944                     gen_helper_neon_addl_u16(tcg_res[pass],
11945                                              tcg_res[pass], tcg_op);
11946                 } else {
11947                     gen_helper_neon_addl_u32(tcg_res[pass],
11948                                              tcg_res[pass], tcg_op);
11949                 }
11950             }
11951         }
11952     }
11953     if (!is_q) {
11954         tcg_res[1] = tcg_constant_i64(0);
11955     }
11956     for (pass = 0; pass < 2; pass++) {
11957         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11958     }
11959 }
11960 
11961 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
11962 {
11963     /* Implement SHLL and SHLL2 */
11964     int pass;
11965     int part = is_q ? 2 : 0;
11966     TCGv_i64 tcg_res[2];
11967 
11968     for (pass = 0; pass < 2; pass++) {
11969         static NeonGenWidenFn * const widenfns[3] = {
11970             gen_helper_neon_widen_u8,
11971             gen_helper_neon_widen_u16,
11972             tcg_gen_extu_i32_i64,
11973         };
11974         NeonGenWidenFn *widenfn = widenfns[size];
11975         TCGv_i32 tcg_op = tcg_temp_new_i32();
11976 
11977         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
11978         tcg_res[pass] = tcg_temp_new_i64();
11979         widenfn(tcg_res[pass], tcg_op);
11980         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
11981     }
11982 
11983     for (pass = 0; pass < 2; pass++) {
11984         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11985     }
11986 }
11987 
11988 /* AdvSIMD two reg misc
11989  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
11990  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11991  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
11992  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11993  */
11994 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
11995 {
11996     int size = extract32(insn, 22, 2);
11997     int opcode = extract32(insn, 12, 5);
11998     bool u = extract32(insn, 29, 1);
11999     bool is_q = extract32(insn, 30, 1);
12000     int rn = extract32(insn, 5, 5);
12001     int rd = extract32(insn, 0, 5);
12002     bool need_fpstatus = false;
12003     int rmode = -1;
12004     TCGv_i32 tcg_rmode;
12005     TCGv_ptr tcg_fpstatus;
12006 
12007     switch (opcode) {
12008     case 0x0: /* REV64, REV32 */
12009     case 0x1: /* REV16 */
12010         handle_rev(s, opcode, u, is_q, size, rn, rd);
12011         return;
12012     case 0x5: /* CNT, NOT, RBIT */
12013         if (u && size == 0) {
12014             /* NOT */
12015             break;
12016         } else if (u && size == 1) {
12017             /* RBIT */
12018             break;
12019         } else if (!u && size == 0) {
12020             /* CNT */
12021             break;
12022         }
12023         unallocated_encoding(s);
12024         return;
12025     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12026     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12027         if (size == 3) {
12028             unallocated_encoding(s);
12029             return;
12030         }
12031         if (!fp_access_check(s)) {
12032             return;
12033         }
12034 
12035         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
12036         return;
12037     case 0x4: /* CLS, CLZ */
12038         if (size == 3) {
12039             unallocated_encoding(s);
12040             return;
12041         }
12042         break;
12043     case 0x2: /* SADDLP, UADDLP */
12044     case 0x6: /* SADALP, UADALP */
12045         if (size == 3) {
12046             unallocated_encoding(s);
12047             return;
12048         }
12049         if (!fp_access_check(s)) {
12050             return;
12051         }
12052         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
12053         return;
12054     case 0x13: /* SHLL, SHLL2 */
12055         if (u == 0 || size == 3) {
12056             unallocated_encoding(s);
12057             return;
12058         }
12059         if (!fp_access_check(s)) {
12060             return;
12061         }
12062         handle_shll(s, is_q, size, rn, rd);
12063         return;
12064     case 0xa: /* CMLT */
12065         if (u == 1) {
12066             unallocated_encoding(s);
12067             return;
12068         }
12069         /* fall through */
12070     case 0x8: /* CMGT, CMGE */
12071     case 0x9: /* CMEQ, CMLE */
12072     case 0xb: /* ABS, NEG */
12073         if (size == 3 && !is_q) {
12074             unallocated_encoding(s);
12075             return;
12076         }
12077         break;
12078     case 0x3: /* SUQADD, USQADD */
12079         if (size == 3 && !is_q) {
12080             unallocated_encoding(s);
12081             return;
12082         }
12083         if (!fp_access_check(s)) {
12084             return;
12085         }
12086         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12087         return;
12088     case 0x7: /* SQABS, SQNEG */
12089         if (size == 3 && !is_q) {
12090             unallocated_encoding(s);
12091             return;
12092         }
12093         break;
12094     case 0xc ... 0xf:
12095     case 0x16 ... 0x1f:
12096     {
12097         /* Floating point: U, size[1] and opcode indicate operation;
12098          * size[0] indicates single or double precision.
12099          */
12100         int is_double = extract32(size, 0, 1);
12101         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12102         size = is_double ? 3 : 2;
12103         switch (opcode) {
12104         case 0x2f: /* FABS */
12105         case 0x6f: /* FNEG */
12106             if (size == 3 && !is_q) {
12107                 unallocated_encoding(s);
12108                 return;
12109             }
12110             break;
12111         case 0x1d: /* SCVTF */
12112         case 0x5d: /* UCVTF */
12113         {
12114             bool is_signed = (opcode == 0x1d) ? true : false;
12115             int elements = is_double ? 2 : is_q ? 4 : 2;
12116             if (is_double && !is_q) {
12117                 unallocated_encoding(s);
12118                 return;
12119             }
12120             if (!fp_access_check(s)) {
12121                 return;
12122             }
12123             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12124             return;
12125         }
12126         case 0x2c: /* FCMGT (zero) */
12127         case 0x2d: /* FCMEQ (zero) */
12128         case 0x2e: /* FCMLT (zero) */
12129         case 0x6c: /* FCMGE (zero) */
12130         case 0x6d: /* FCMLE (zero) */
12131             if (size == 3 && !is_q) {
12132                 unallocated_encoding(s);
12133                 return;
12134             }
12135             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12136             return;
12137         case 0x7f: /* FSQRT */
12138             if (size == 3 && !is_q) {
12139                 unallocated_encoding(s);
12140                 return;
12141             }
12142             break;
12143         case 0x1a: /* FCVTNS */
12144         case 0x1b: /* FCVTMS */
12145         case 0x3a: /* FCVTPS */
12146         case 0x3b: /* FCVTZS */
12147         case 0x5a: /* FCVTNU */
12148         case 0x5b: /* FCVTMU */
12149         case 0x7a: /* FCVTPU */
12150         case 0x7b: /* FCVTZU */
12151             need_fpstatus = true;
12152             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12153             if (size == 3 && !is_q) {
12154                 unallocated_encoding(s);
12155                 return;
12156             }
12157             break;
12158         case 0x5c: /* FCVTAU */
12159         case 0x1c: /* FCVTAS */
12160             need_fpstatus = true;
12161             rmode = FPROUNDING_TIEAWAY;
12162             if (size == 3 && !is_q) {
12163                 unallocated_encoding(s);
12164                 return;
12165             }
12166             break;
12167         case 0x3c: /* URECPE */
12168             if (size == 3) {
12169                 unallocated_encoding(s);
12170                 return;
12171             }
12172             /* fall through */
12173         case 0x3d: /* FRECPE */
12174         case 0x7d: /* FRSQRTE */
12175             if (size == 3 && !is_q) {
12176                 unallocated_encoding(s);
12177                 return;
12178             }
12179             if (!fp_access_check(s)) {
12180                 return;
12181             }
12182             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12183             return;
12184         case 0x56: /* FCVTXN, FCVTXN2 */
12185             if (size == 2) {
12186                 unallocated_encoding(s);
12187                 return;
12188             }
12189             /* fall through */
12190         case 0x16: /* FCVTN, FCVTN2 */
12191             /* handle_2misc_narrow does a 2*size -> size operation, but these
12192              * instructions encode the source size rather than dest size.
12193              */
12194             if (!fp_access_check(s)) {
12195                 return;
12196             }
12197             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12198             return;
12199         case 0x36: /* BFCVTN, BFCVTN2 */
12200             if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
12201                 unallocated_encoding(s);
12202                 return;
12203             }
12204             if (!fp_access_check(s)) {
12205                 return;
12206             }
12207             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12208             return;
12209         case 0x17: /* FCVTL, FCVTL2 */
12210             if (!fp_access_check(s)) {
12211                 return;
12212             }
12213             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12214             return;
12215         case 0x18: /* FRINTN */
12216         case 0x19: /* FRINTM */
12217         case 0x38: /* FRINTP */
12218         case 0x39: /* FRINTZ */
12219             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12220             /* fall through */
12221         case 0x59: /* FRINTX */
12222         case 0x79: /* FRINTI */
12223             need_fpstatus = true;
12224             if (size == 3 && !is_q) {
12225                 unallocated_encoding(s);
12226                 return;
12227             }
12228             break;
12229         case 0x58: /* FRINTA */
12230             rmode = FPROUNDING_TIEAWAY;
12231             need_fpstatus = true;
12232             if (size == 3 && !is_q) {
12233                 unallocated_encoding(s);
12234                 return;
12235             }
12236             break;
12237         case 0x7c: /* URSQRTE */
12238             if (size == 3) {
12239                 unallocated_encoding(s);
12240                 return;
12241             }
12242             break;
12243         case 0x1e: /* FRINT32Z */
12244         case 0x1f: /* FRINT64Z */
12245             rmode = FPROUNDING_ZERO;
12246             /* fall through */
12247         case 0x5e: /* FRINT32X */
12248         case 0x5f: /* FRINT64X */
12249             need_fpstatus = true;
12250             if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12251                 unallocated_encoding(s);
12252                 return;
12253             }
12254             break;
12255         default:
12256             unallocated_encoding(s);
12257             return;
12258         }
12259         break;
12260     }
12261     default:
12262         unallocated_encoding(s);
12263         return;
12264     }
12265 
12266     if (!fp_access_check(s)) {
12267         return;
12268     }
12269 
12270     if (need_fpstatus || rmode >= 0) {
12271         tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12272     } else {
12273         tcg_fpstatus = NULL;
12274     }
12275     if (rmode >= 0) {
12276         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12277     } else {
12278         tcg_rmode = NULL;
12279     }
12280 
12281     switch (opcode) {
12282     case 0x5:
12283         if (u && size == 0) { /* NOT */
12284             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12285             return;
12286         }
12287         break;
12288     case 0x8: /* CMGT, CMGE */
12289         if (u) {
12290             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12291         } else {
12292             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12293         }
12294         return;
12295     case 0x9: /* CMEQ, CMLE */
12296         if (u) {
12297             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12298         } else {
12299             gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12300         }
12301         return;
12302     case 0xa: /* CMLT */
12303         gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12304         return;
12305     case 0xb:
12306         if (u) { /* ABS, NEG */
12307             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12308         } else {
12309             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12310         }
12311         return;
12312     }
12313 
12314     if (size == 3) {
12315         /* All 64-bit element operations can be shared with scalar 2misc */
12316         int pass;
12317 
12318         /* Coverity claims (size == 3 && !is_q) has been eliminated
12319          * from all paths leading to here.
12320          */
12321         tcg_debug_assert(is_q);
12322         for (pass = 0; pass < 2; pass++) {
12323             TCGv_i64 tcg_op = tcg_temp_new_i64();
12324             TCGv_i64 tcg_res = tcg_temp_new_i64();
12325 
12326             read_vec_element(s, tcg_op, rn, pass, MO_64);
12327 
12328             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12329                             tcg_rmode, tcg_fpstatus);
12330 
12331             write_vec_element(s, tcg_res, rd, pass, MO_64);
12332         }
12333     } else {
12334         int pass;
12335 
12336         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12337             TCGv_i32 tcg_op = tcg_temp_new_i32();
12338             TCGv_i32 tcg_res = tcg_temp_new_i32();
12339 
12340             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12341 
12342             if (size == 2) {
12343                 /* Special cases for 32 bit elements */
12344                 switch (opcode) {
12345                 case 0x4: /* CLS */
12346                     if (u) {
12347                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12348                     } else {
12349                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
12350                     }
12351                     break;
12352                 case 0x7: /* SQABS, SQNEG */
12353                     if (u) {
12354                         gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12355                     } else {
12356                         gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12357                     }
12358                     break;
12359                 case 0x2f: /* FABS */
12360                     gen_helper_vfp_abss(tcg_res, tcg_op);
12361                     break;
12362                 case 0x6f: /* FNEG */
12363                     gen_helper_vfp_negs(tcg_res, tcg_op);
12364                     break;
12365                 case 0x7f: /* FSQRT */
12366                     gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12367                     break;
12368                 case 0x1a: /* FCVTNS */
12369                 case 0x1b: /* FCVTMS */
12370                 case 0x1c: /* FCVTAS */
12371                 case 0x3a: /* FCVTPS */
12372                 case 0x3b: /* FCVTZS */
12373                     gen_helper_vfp_tosls(tcg_res, tcg_op,
12374                                          tcg_constant_i32(0), tcg_fpstatus);
12375                     break;
12376                 case 0x5a: /* FCVTNU */
12377                 case 0x5b: /* FCVTMU */
12378                 case 0x5c: /* FCVTAU */
12379                 case 0x7a: /* FCVTPU */
12380                 case 0x7b: /* FCVTZU */
12381                     gen_helper_vfp_touls(tcg_res, tcg_op,
12382                                          tcg_constant_i32(0), tcg_fpstatus);
12383                     break;
12384                 case 0x18: /* FRINTN */
12385                 case 0x19: /* FRINTM */
12386                 case 0x38: /* FRINTP */
12387                 case 0x39: /* FRINTZ */
12388                 case 0x58: /* FRINTA */
12389                 case 0x79: /* FRINTI */
12390                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12391                     break;
12392                 case 0x59: /* FRINTX */
12393                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12394                     break;
12395                 case 0x7c: /* URSQRTE */
12396                     gen_helper_rsqrte_u32(tcg_res, tcg_op);
12397                     break;
12398                 case 0x1e: /* FRINT32Z */
12399                 case 0x5e: /* FRINT32X */
12400                     gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12401                     break;
12402                 case 0x1f: /* FRINT64Z */
12403                 case 0x5f: /* FRINT64X */
12404                     gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12405                     break;
12406                 default:
12407                     g_assert_not_reached();
12408                 }
12409             } else {
12410                 /* Use helpers for 8 and 16 bit elements */
12411                 switch (opcode) {
12412                 case 0x5: /* CNT, RBIT */
12413                     /* For these two insns size is part of the opcode specifier
12414                      * (handled earlier); they always operate on byte elements.
12415                      */
12416                     if (u) {
12417                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12418                     } else {
12419                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12420                     }
12421                     break;
12422                 case 0x7: /* SQABS, SQNEG */
12423                 {
12424                     NeonGenOneOpEnvFn *genfn;
12425                     static NeonGenOneOpEnvFn * const fns[2][2] = {
12426                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12427                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12428                     };
12429                     genfn = fns[size][u];
12430                     genfn(tcg_res, cpu_env, tcg_op);
12431                     break;
12432                 }
12433                 case 0x4: /* CLS, CLZ */
12434                     if (u) {
12435                         if (size == 0) {
12436                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
12437                         } else {
12438                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
12439                         }
12440                     } else {
12441                         if (size == 0) {
12442                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
12443                         } else {
12444                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
12445                         }
12446                     }
12447                     break;
12448                 default:
12449                     g_assert_not_reached();
12450                 }
12451             }
12452 
12453             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12454         }
12455     }
12456     clear_vec_high(s, is_q, rd);
12457 
12458     if (tcg_rmode) {
12459         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12460     }
12461 }
12462 
12463 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12464  *
12465  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
12466  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12467  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12468  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12469  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12470  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12471  *
12472  * This actually covers two groups where scalar access is governed by
12473  * bit 28. A bunch of the instructions (float to integral) only exist
12474  * in the vector form and are un-allocated for the scalar decode. Also
12475  * in the scalar decode Q is always 1.
12476  */
12477 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12478 {
12479     int fpop, opcode, a, u;
12480     int rn, rd;
12481     bool is_q;
12482     bool is_scalar;
12483     bool only_in_vector = false;
12484 
12485     int pass;
12486     TCGv_i32 tcg_rmode = NULL;
12487     TCGv_ptr tcg_fpstatus = NULL;
12488     bool need_fpst = true;
12489     int rmode = -1;
12490 
12491     if (!dc_isar_feature(aa64_fp16, s)) {
12492         unallocated_encoding(s);
12493         return;
12494     }
12495 
12496     rd = extract32(insn, 0, 5);
12497     rn = extract32(insn, 5, 5);
12498 
12499     a = extract32(insn, 23, 1);
12500     u = extract32(insn, 29, 1);
12501     is_scalar = extract32(insn, 28, 1);
12502     is_q = extract32(insn, 30, 1);
12503 
12504     opcode = extract32(insn, 12, 5);
12505     fpop = deposit32(opcode, 5, 1, a);
12506     fpop = deposit32(fpop, 6, 1, u);
12507 
12508     switch (fpop) {
12509     case 0x1d: /* SCVTF */
12510     case 0x5d: /* UCVTF */
12511     {
12512         int elements;
12513 
12514         if (is_scalar) {
12515             elements = 1;
12516         } else {
12517             elements = (is_q ? 8 : 4);
12518         }
12519 
12520         if (!fp_access_check(s)) {
12521             return;
12522         }
12523         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12524         return;
12525     }
12526     break;
12527     case 0x2c: /* FCMGT (zero) */
12528     case 0x2d: /* FCMEQ (zero) */
12529     case 0x2e: /* FCMLT (zero) */
12530     case 0x6c: /* FCMGE (zero) */
12531     case 0x6d: /* FCMLE (zero) */
12532         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12533         return;
12534     case 0x3d: /* FRECPE */
12535     case 0x3f: /* FRECPX */
12536         break;
12537     case 0x18: /* FRINTN */
12538         only_in_vector = true;
12539         rmode = FPROUNDING_TIEEVEN;
12540         break;
12541     case 0x19: /* FRINTM */
12542         only_in_vector = true;
12543         rmode = FPROUNDING_NEGINF;
12544         break;
12545     case 0x38: /* FRINTP */
12546         only_in_vector = true;
12547         rmode = FPROUNDING_POSINF;
12548         break;
12549     case 0x39: /* FRINTZ */
12550         only_in_vector = true;
12551         rmode = FPROUNDING_ZERO;
12552         break;
12553     case 0x58: /* FRINTA */
12554         only_in_vector = true;
12555         rmode = FPROUNDING_TIEAWAY;
12556         break;
12557     case 0x59: /* FRINTX */
12558     case 0x79: /* FRINTI */
12559         only_in_vector = true;
12560         /* current rounding mode */
12561         break;
12562     case 0x1a: /* FCVTNS */
12563         rmode = FPROUNDING_TIEEVEN;
12564         break;
12565     case 0x1b: /* FCVTMS */
12566         rmode = FPROUNDING_NEGINF;
12567         break;
12568     case 0x1c: /* FCVTAS */
12569         rmode = FPROUNDING_TIEAWAY;
12570         break;
12571     case 0x3a: /* FCVTPS */
12572         rmode = FPROUNDING_POSINF;
12573         break;
12574     case 0x3b: /* FCVTZS */
12575         rmode = FPROUNDING_ZERO;
12576         break;
12577     case 0x5a: /* FCVTNU */
12578         rmode = FPROUNDING_TIEEVEN;
12579         break;
12580     case 0x5b: /* FCVTMU */
12581         rmode = FPROUNDING_NEGINF;
12582         break;
12583     case 0x5c: /* FCVTAU */
12584         rmode = FPROUNDING_TIEAWAY;
12585         break;
12586     case 0x7a: /* FCVTPU */
12587         rmode = FPROUNDING_POSINF;
12588         break;
12589     case 0x7b: /* FCVTZU */
12590         rmode = FPROUNDING_ZERO;
12591         break;
12592     case 0x2f: /* FABS */
12593     case 0x6f: /* FNEG */
12594         need_fpst = false;
12595         break;
12596     case 0x7d: /* FRSQRTE */
12597     case 0x7f: /* FSQRT (vector) */
12598         break;
12599     default:
12600         unallocated_encoding(s);
12601         return;
12602     }
12603 
12604 
12605     /* Check additional constraints for the scalar encoding */
12606     if (is_scalar) {
12607         if (!is_q) {
12608             unallocated_encoding(s);
12609             return;
12610         }
12611         /* FRINTxx is only in the vector form */
12612         if (only_in_vector) {
12613             unallocated_encoding(s);
12614             return;
12615         }
12616     }
12617 
12618     if (!fp_access_check(s)) {
12619         return;
12620     }
12621 
12622     if (rmode >= 0 || need_fpst) {
12623         tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
12624     }
12625 
12626     if (rmode >= 0) {
12627         tcg_rmode = gen_set_rmode(rmode, tcg_fpstatus);
12628     }
12629 
12630     if (is_scalar) {
12631         TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12632         TCGv_i32 tcg_res = tcg_temp_new_i32();
12633 
12634         switch (fpop) {
12635         case 0x1a: /* FCVTNS */
12636         case 0x1b: /* FCVTMS */
12637         case 0x1c: /* FCVTAS */
12638         case 0x3a: /* FCVTPS */
12639         case 0x3b: /* FCVTZS */
12640             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12641             break;
12642         case 0x3d: /* FRECPE */
12643             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12644             break;
12645         case 0x3f: /* FRECPX */
12646             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12647             break;
12648         case 0x5a: /* FCVTNU */
12649         case 0x5b: /* FCVTMU */
12650         case 0x5c: /* FCVTAU */
12651         case 0x7a: /* FCVTPU */
12652         case 0x7b: /* FCVTZU */
12653             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12654             break;
12655         case 0x6f: /* FNEG */
12656             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12657             break;
12658         case 0x7d: /* FRSQRTE */
12659             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12660             break;
12661         default:
12662             g_assert_not_reached();
12663         }
12664 
12665         /* limit any sign extension going on */
12666         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12667         write_fp_sreg(s, rd, tcg_res);
12668     } else {
12669         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12670             TCGv_i32 tcg_op = tcg_temp_new_i32();
12671             TCGv_i32 tcg_res = tcg_temp_new_i32();
12672 
12673             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12674 
12675             switch (fpop) {
12676             case 0x1a: /* FCVTNS */
12677             case 0x1b: /* FCVTMS */
12678             case 0x1c: /* FCVTAS */
12679             case 0x3a: /* FCVTPS */
12680             case 0x3b: /* FCVTZS */
12681                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12682                 break;
12683             case 0x3d: /* FRECPE */
12684                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12685                 break;
12686             case 0x5a: /* FCVTNU */
12687             case 0x5b: /* FCVTMU */
12688             case 0x5c: /* FCVTAU */
12689             case 0x7a: /* FCVTPU */
12690             case 0x7b: /* FCVTZU */
12691                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12692                 break;
12693             case 0x18: /* FRINTN */
12694             case 0x19: /* FRINTM */
12695             case 0x38: /* FRINTP */
12696             case 0x39: /* FRINTZ */
12697             case 0x58: /* FRINTA */
12698             case 0x79: /* FRINTI */
12699                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12700                 break;
12701             case 0x59: /* FRINTX */
12702                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12703                 break;
12704             case 0x2f: /* FABS */
12705                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12706                 break;
12707             case 0x6f: /* FNEG */
12708                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12709                 break;
12710             case 0x7d: /* FRSQRTE */
12711                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12712                 break;
12713             case 0x7f: /* FSQRT */
12714                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12715                 break;
12716             default:
12717                 g_assert_not_reached();
12718             }
12719 
12720             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12721         }
12722 
12723         clear_vec_high(s, is_q, rd);
12724     }
12725 
12726     if (tcg_rmode) {
12727         gen_restore_rmode(tcg_rmode, tcg_fpstatus);
12728     }
12729 }
12730 
12731 /* AdvSIMD scalar x indexed element
12732  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12733  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12734  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12735  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12736  * AdvSIMD vector x indexed element
12737  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
12738  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12739  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
12740  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12741  */
12742 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12743 {
12744     /* This encoding has two kinds of instruction:
12745      *  normal, where we perform elt x idxelt => elt for each
12746      *     element in the vector
12747      *  long, where we perform elt x idxelt and generate a result of
12748      *     double the width of the input element
12749      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12750      */
12751     bool is_scalar = extract32(insn, 28, 1);
12752     bool is_q = extract32(insn, 30, 1);
12753     bool u = extract32(insn, 29, 1);
12754     int size = extract32(insn, 22, 2);
12755     int l = extract32(insn, 21, 1);
12756     int m = extract32(insn, 20, 1);
12757     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12758     int rm = extract32(insn, 16, 4);
12759     int opcode = extract32(insn, 12, 4);
12760     int h = extract32(insn, 11, 1);
12761     int rn = extract32(insn, 5, 5);
12762     int rd = extract32(insn, 0, 5);
12763     bool is_long = false;
12764     int is_fp = 0;
12765     bool is_fp16 = false;
12766     int index;
12767     TCGv_ptr fpst;
12768 
12769     switch (16 * u + opcode) {
12770     case 0x08: /* MUL */
12771     case 0x10: /* MLA */
12772     case 0x14: /* MLS */
12773         if (is_scalar) {
12774             unallocated_encoding(s);
12775             return;
12776         }
12777         break;
12778     case 0x02: /* SMLAL, SMLAL2 */
12779     case 0x12: /* UMLAL, UMLAL2 */
12780     case 0x06: /* SMLSL, SMLSL2 */
12781     case 0x16: /* UMLSL, UMLSL2 */
12782     case 0x0a: /* SMULL, SMULL2 */
12783     case 0x1a: /* UMULL, UMULL2 */
12784         if (is_scalar) {
12785             unallocated_encoding(s);
12786             return;
12787         }
12788         is_long = true;
12789         break;
12790     case 0x03: /* SQDMLAL, SQDMLAL2 */
12791     case 0x07: /* SQDMLSL, SQDMLSL2 */
12792     case 0x0b: /* SQDMULL, SQDMULL2 */
12793         is_long = true;
12794         break;
12795     case 0x0c: /* SQDMULH */
12796     case 0x0d: /* SQRDMULH */
12797         break;
12798     case 0x01: /* FMLA */
12799     case 0x05: /* FMLS */
12800     case 0x09: /* FMUL */
12801     case 0x19: /* FMULX */
12802         is_fp = 1;
12803         break;
12804     case 0x1d: /* SQRDMLAH */
12805     case 0x1f: /* SQRDMLSH */
12806         if (!dc_isar_feature(aa64_rdm, s)) {
12807             unallocated_encoding(s);
12808             return;
12809         }
12810         break;
12811     case 0x0e: /* SDOT */
12812     case 0x1e: /* UDOT */
12813         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12814             unallocated_encoding(s);
12815             return;
12816         }
12817         break;
12818     case 0x0f:
12819         switch (size) {
12820         case 0: /* SUDOT */
12821         case 2: /* USDOT */
12822             if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
12823                 unallocated_encoding(s);
12824                 return;
12825             }
12826             size = MO_32;
12827             break;
12828         case 1: /* BFDOT */
12829             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12830                 unallocated_encoding(s);
12831                 return;
12832             }
12833             size = MO_32;
12834             break;
12835         case 3: /* BFMLAL{B,T} */
12836             if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
12837                 unallocated_encoding(s);
12838                 return;
12839             }
12840             /* can't set is_fp without other incorrect size checks */
12841             size = MO_16;
12842             break;
12843         default:
12844             unallocated_encoding(s);
12845             return;
12846         }
12847         break;
12848     case 0x11: /* FCMLA #0 */
12849     case 0x13: /* FCMLA #90 */
12850     case 0x15: /* FCMLA #180 */
12851     case 0x17: /* FCMLA #270 */
12852         if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
12853             unallocated_encoding(s);
12854             return;
12855         }
12856         is_fp = 2;
12857         break;
12858     case 0x00: /* FMLAL */
12859     case 0x04: /* FMLSL */
12860     case 0x18: /* FMLAL2 */
12861     case 0x1c: /* FMLSL2 */
12862         if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
12863             unallocated_encoding(s);
12864             return;
12865         }
12866         size = MO_16;
12867         /* is_fp, but we pass cpu_env not fp_status.  */
12868         break;
12869     default:
12870         unallocated_encoding(s);
12871         return;
12872     }
12873 
12874     switch (is_fp) {
12875     case 1: /* normal fp */
12876         /* convert insn encoded size to MemOp size */
12877         switch (size) {
12878         case 0: /* half-precision */
12879             size = MO_16;
12880             is_fp16 = true;
12881             break;
12882         case MO_32: /* single precision */
12883         case MO_64: /* double precision */
12884             break;
12885         default:
12886             unallocated_encoding(s);
12887             return;
12888         }
12889         break;
12890 
12891     case 2: /* complex fp */
12892         /* Each indexable element is a complex pair.  */
12893         size += 1;
12894         switch (size) {
12895         case MO_32:
12896             if (h && !is_q) {
12897                 unallocated_encoding(s);
12898                 return;
12899             }
12900             is_fp16 = true;
12901             break;
12902         case MO_64:
12903             break;
12904         default:
12905             unallocated_encoding(s);
12906             return;
12907         }
12908         break;
12909 
12910     default: /* integer */
12911         switch (size) {
12912         case MO_8:
12913         case MO_64:
12914             unallocated_encoding(s);
12915             return;
12916         }
12917         break;
12918     }
12919     if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
12920         unallocated_encoding(s);
12921         return;
12922     }
12923 
12924     /* Given MemOp size, adjust register and indexing.  */
12925     switch (size) {
12926     case MO_16:
12927         index = h << 2 | l << 1 | m;
12928         break;
12929     case MO_32:
12930         index = h << 1 | l;
12931         rm |= m << 4;
12932         break;
12933     case MO_64:
12934         if (l || !is_q) {
12935             unallocated_encoding(s);
12936             return;
12937         }
12938         index = h;
12939         rm |= m << 4;
12940         break;
12941     default:
12942         g_assert_not_reached();
12943     }
12944 
12945     if (!fp_access_check(s)) {
12946         return;
12947     }
12948 
12949     if (is_fp) {
12950         fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
12951     } else {
12952         fpst = NULL;
12953     }
12954 
12955     switch (16 * u + opcode) {
12956     case 0x0e: /* SDOT */
12957     case 0x1e: /* UDOT */
12958         gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12959                          u ? gen_helper_gvec_udot_idx_b
12960                          : gen_helper_gvec_sdot_idx_b);
12961         return;
12962     case 0x0f:
12963         switch (extract32(insn, 22, 2)) {
12964         case 0: /* SUDOT */
12965             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12966                              gen_helper_gvec_sudot_idx_b);
12967             return;
12968         case 1: /* BFDOT */
12969             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12970                              gen_helper_gvec_bfdot_idx);
12971             return;
12972         case 2: /* USDOT */
12973             gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
12974                              gen_helper_gvec_usdot_idx_b);
12975             return;
12976         case 3: /* BFMLAL{B,T} */
12977             gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
12978                               gen_helper_gvec_bfmlal_idx);
12979             return;
12980         }
12981         g_assert_not_reached();
12982     case 0x11: /* FCMLA #0 */
12983     case 0x13: /* FCMLA #90 */
12984     case 0x15: /* FCMLA #180 */
12985     case 0x17: /* FCMLA #270 */
12986         {
12987             int rot = extract32(insn, 13, 2);
12988             int data = (index << 2) | rot;
12989             tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
12990                                vec_full_reg_offset(s, rn),
12991                                vec_full_reg_offset(s, rm),
12992                                vec_full_reg_offset(s, rd), fpst,
12993                                is_q ? 16 : 8, vec_full_reg_size(s), data,
12994                                size == MO_64
12995                                ? gen_helper_gvec_fcmlas_idx
12996                                : gen_helper_gvec_fcmlah_idx);
12997         }
12998         return;
12999 
13000     case 0x00: /* FMLAL */
13001     case 0x04: /* FMLSL */
13002     case 0x18: /* FMLAL2 */
13003     case 0x1c: /* FMLSL2 */
13004         {
13005             int is_s = extract32(opcode, 2, 1);
13006             int is_2 = u;
13007             int data = (index << 2) | (is_2 << 1) | is_s;
13008             tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13009                                vec_full_reg_offset(s, rn),
13010                                vec_full_reg_offset(s, rm), cpu_env,
13011                                is_q ? 16 : 8, vec_full_reg_size(s),
13012                                data, gen_helper_gvec_fmlal_idx_a64);
13013         }
13014         return;
13015 
13016     case 0x08: /* MUL */
13017         if (!is_long && !is_scalar) {
13018             static gen_helper_gvec_3 * const fns[3] = {
13019                 gen_helper_gvec_mul_idx_h,
13020                 gen_helper_gvec_mul_idx_s,
13021                 gen_helper_gvec_mul_idx_d,
13022             };
13023             tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
13024                                vec_full_reg_offset(s, rn),
13025                                vec_full_reg_offset(s, rm),
13026                                is_q ? 16 : 8, vec_full_reg_size(s),
13027                                index, fns[size - 1]);
13028             return;
13029         }
13030         break;
13031 
13032     case 0x10: /* MLA */
13033         if (!is_long && !is_scalar) {
13034             static gen_helper_gvec_4 * const fns[3] = {
13035                 gen_helper_gvec_mla_idx_h,
13036                 gen_helper_gvec_mla_idx_s,
13037                 gen_helper_gvec_mla_idx_d,
13038             };
13039             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13040                                vec_full_reg_offset(s, rn),
13041                                vec_full_reg_offset(s, rm),
13042                                vec_full_reg_offset(s, rd),
13043                                is_q ? 16 : 8, vec_full_reg_size(s),
13044                                index, fns[size - 1]);
13045             return;
13046         }
13047         break;
13048 
13049     case 0x14: /* MLS */
13050         if (!is_long && !is_scalar) {
13051             static gen_helper_gvec_4 * const fns[3] = {
13052                 gen_helper_gvec_mls_idx_h,
13053                 gen_helper_gvec_mls_idx_s,
13054                 gen_helper_gvec_mls_idx_d,
13055             };
13056             tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13057                                vec_full_reg_offset(s, rn),
13058                                vec_full_reg_offset(s, rm),
13059                                vec_full_reg_offset(s, rd),
13060                                is_q ? 16 : 8, vec_full_reg_size(s),
13061                                index, fns[size - 1]);
13062             return;
13063         }
13064         break;
13065     }
13066 
13067     if (size == 3) {
13068         TCGv_i64 tcg_idx = tcg_temp_new_i64();
13069         int pass;
13070 
13071         assert(is_fp && is_q && !is_long);
13072 
13073         read_vec_element(s, tcg_idx, rm, index, MO_64);
13074 
13075         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13076             TCGv_i64 tcg_op = tcg_temp_new_i64();
13077             TCGv_i64 tcg_res = tcg_temp_new_i64();
13078 
13079             read_vec_element(s, tcg_op, rn, pass, MO_64);
13080 
13081             switch (16 * u + opcode) {
13082             case 0x05: /* FMLS */
13083                 /* As usual for ARM, separate negation for fused multiply-add */
13084                 gen_helper_vfp_negd(tcg_op, tcg_op);
13085                 /* fall through */
13086             case 0x01: /* FMLA */
13087                 read_vec_element(s, tcg_res, rd, pass, MO_64);
13088                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13089                 break;
13090             case 0x09: /* FMUL */
13091                 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13092                 break;
13093             case 0x19: /* FMULX */
13094                 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13095                 break;
13096             default:
13097                 g_assert_not_reached();
13098             }
13099 
13100             write_vec_element(s, tcg_res, rd, pass, MO_64);
13101         }
13102 
13103         clear_vec_high(s, !is_scalar, rd);
13104     } else if (!is_long) {
13105         /* 32 bit floating point, or 16 or 32 bit integer.
13106          * For the 16 bit scalar case we use the usual Neon helpers and
13107          * rely on the fact that 0 op 0 == 0 with no side effects.
13108          */
13109         TCGv_i32 tcg_idx = tcg_temp_new_i32();
13110         int pass, maxpasses;
13111 
13112         if (is_scalar) {
13113             maxpasses = 1;
13114         } else {
13115             maxpasses = is_q ? 4 : 2;
13116         }
13117 
13118         read_vec_element_i32(s, tcg_idx, rm, index, size);
13119 
13120         if (size == 1 && !is_scalar) {
13121             /* The simplest way to handle the 16x16 indexed ops is to duplicate
13122              * the index into both halves of the 32 bit tcg_idx and then use
13123              * the usual Neon helpers.
13124              */
13125             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13126         }
13127 
13128         for (pass = 0; pass < maxpasses; pass++) {
13129             TCGv_i32 tcg_op = tcg_temp_new_i32();
13130             TCGv_i32 tcg_res = tcg_temp_new_i32();
13131 
13132             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13133 
13134             switch (16 * u + opcode) {
13135             case 0x08: /* MUL */
13136             case 0x10: /* MLA */
13137             case 0x14: /* MLS */
13138             {
13139                 static NeonGenTwoOpFn * const fns[2][2] = {
13140                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13141                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
13142                 };
13143                 NeonGenTwoOpFn *genfn;
13144                 bool is_sub = opcode == 0x4;
13145 
13146                 if (size == 1) {
13147                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13148                 } else {
13149                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13150                 }
13151                 if (opcode == 0x8) {
13152                     break;
13153                 }
13154                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13155                 genfn = fns[size - 1][is_sub];
13156                 genfn(tcg_res, tcg_op, tcg_res);
13157                 break;
13158             }
13159             case 0x05: /* FMLS */
13160             case 0x01: /* FMLA */
13161                 read_vec_element_i32(s, tcg_res, rd, pass,
13162                                      is_scalar ? size : MO_32);
13163                 switch (size) {
13164                 case 1:
13165                     if (opcode == 0x5) {
13166                         /* As usual for ARM, separate negation for fused
13167                          * multiply-add */
13168                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13169                     }
13170                     if (is_scalar) {
13171                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13172                                                    tcg_res, fpst);
13173                     } else {
13174                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13175                                                     tcg_res, fpst);
13176                     }
13177                     break;
13178                 case 2:
13179                     if (opcode == 0x5) {
13180                         /* As usual for ARM, separate negation for
13181                          * fused multiply-add */
13182                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13183                     }
13184                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13185                                            tcg_res, fpst);
13186                     break;
13187                 default:
13188                     g_assert_not_reached();
13189                 }
13190                 break;
13191             case 0x09: /* FMUL */
13192                 switch (size) {
13193                 case 1:
13194                     if (is_scalar) {
13195                         gen_helper_advsimd_mulh(tcg_res, tcg_op,
13196                                                 tcg_idx, fpst);
13197                     } else {
13198                         gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13199                                                  tcg_idx, fpst);
13200                     }
13201                     break;
13202                 case 2:
13203                     gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13204                     break;
13205                 default:
13206                     g_assert_not_reached();
13207                 }
13208                 break;
13209             case 0x19: /* FMULX */
13210                 switch (size) {
13211                 case 1:
13212                     if (is_scalar) {
13213                         gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13214                                                  tcg_idx, fpst);
13215                     } else {
13216                         gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13217                                                   tcg_idx, fpst);
13218                     }
13219                     break;
13220                 case 2:
13221                     gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13222                     break;
13223                 default:
13224                     g_assert_not_reached();
13225                 }
13226                 break;
13227             case 0x0c: /* SQDMULH */
13228                 if (size == 1) {
13229                     gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13230                                                tcg_op, tcg_idx);
13231                 } else {
13232                     gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13233                                                tcg_op, tcg_idx);
13234                 }
13235                 break;
13236             case 0x0d: /* SQRDMULH */
13237                 if (size == 1) {
13238                     gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13239                                                 tcg_op, tcg_idx);
13240                 } else {
13241                     gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13242                                                 tcg_op, tcg_idx);
13243                 }
13244                 break;
13245             case 0x1d: /* SQRDMLAH */
13246                 read_vec_element_i32(s, tcg_res, rd, pass,
13247                                      is_scalar ? size : MO_32);
13248                 if (size == 1) {
13249                     gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13250                                                 tcg_op, tcg_idx, tcg_res);
13251                 } else {
13252                     gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13253                                                 tcg_op, tcg_idx, tcg_res);
13254                 }
13255                 break;
13256             case 0x1f: /* SQRDMLSH */
13257                 read_vec_element_i32(s, tcg_res, rd, pass,
13258                                      is_scalar ? size : MO_32);
13259                 if (size == 1) {
13260                     gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13261                                                 tcg_op, tcg_idx, tcg_res);
13262                 } else {
13263                     gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13264                                                 tcg_op, tcg_idx, tcg_res);
13265                 }
13266                 break;
13267             default:
13268                 g_assert_not_reached();
13269             }
13270 
13271             if (is_scalar) {
13272                 write_fp_sreg(s, rd, tcg_res);
13273             } else {
13274                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13275             }
13276         }
13277 
13278         clear_vec_high(s, is_q, rd);
13279     } else {
13280         /* long ops: 16x16->32 or 32x32->64 */
13281         TCGv_i64 tcg_res[2];
13282         int pass;
13283         bool satop = extract32(opcode, 0, 1);
13284         MemOp memop = MO_32;
13285 
13286         if (satop || !u) {
13287             memop |= MO_SIGN;
13288         }
13289 
13290         if (size == 2) {
13291             TCGv_i64 tcg_idx = tcg_temp_new_i64();
13292 
13293             read_vec_element(s, tcg_idx, rm, index, memop);
13294 
13295             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13296                 TCGv_i64 tcg_op = tcg_temp_new_i64();
13297                 TCGv_i64 tcg_passres;
13298                 int passelt;
13299 
13300                 if (is_scalar) {
13301                     passelt = 0;
13302                 } else {
13303                     passelt = pass + (is_q * 2);
13304                 }
13305 
13306                 read_vec_element(s, tcg_op, rn, passelt, memop);
13307 
13308                 tcg_res[pass] = tcg_temp_new_i64();
13309 
13310                 if (opcode == 0xa || opcode == 0xb) {
13311                     /* Non-accumulating ops */
13312                     tcg_passres = tcg_res[pass];
13313                 } else {
13314                     tcg_passres = tcg_temp_new_i64();
13315                 }
13316 
13317                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13318 
13319                 if (satop) {
13320                     /* saturating, doubling */
13321                     gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13322                                                       tcg_passres, tcg_passres);
13323                 }
13324 
13325                 if (opcode == 0xa || opcode == 0xb) {
13326                     continue;
13327                 }
13328 
13329                 /* Accumulating op: handle accumulate step */
13330                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13331 
13332                 switch (opcode) {
13333                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13334                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13335                     break;
13336                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13337                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13338                     break;
13339                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13340                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
13341                     /* fall through */
13342                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13343                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13344                                                       tcg_res[pass],
13345                                                       tcg_passres);
13346                     break;
13347                 default:
13348                     g_assert_not_reached();
13349                 }
13350             }
13351 
13352             clear_vec_high(s, !is_scalar, rd);
13353         } else {
13354             TCGv_i32 tcg_idx = tcg_temp_new_i32();
13355 
13356             assert(size == 1);
13357             read_vec_element_i32(s, tcg_idx, rm, index, size);
13358 
13359             if (!is_scalar) {
13360                 /* The simplest way to handle the 16x16 indexed ops is to
13361                  * duplicate the index into both halves of the 32 bit tcg_idx
13362                  * and then use the usual Neon helpers.
13363                  */
13364                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13365             }
13366 
13367             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13368                 TCGv_i32 tcg_op = tcg_temp_new_i32();
13369                 TCGv_i64 tcg_passres;
13370 
13371                 if (is_scalar) {
13372                     read_vec_element_i32(s, tcg_op, rn, pass, size);
13373                 } else {
13374                     read_vec_element_i32(s, tcg_op, rn,
13375                                          pass + (is_q * 2), MO_32);
13376                 }
13377 
13378                 tcg_res[pass] = tcg_temp_new_i64();
13379 
13380                 if (opcode == 0xa || opcode == 0xb) {
13381                     /* Non-accumulating ops */
13382                     tcg_passres = tcg_res[pass];
13383                 } else {
13384                     tcg_passres = tcg_temp_new_i64();
13385                 }
13386 
13387                 if (memop & MO_SIGN) {
13388                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13389                 } else {
13390                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13391                 }
13392                 if (satop) {
13393                     gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13394                                                       tcg_passres, tcg_passres);
13395                 }
13396 
13397                 if (opcode == 0xa || opcode == 0xb) {
13398                     continue;
13399                 }
13400 
13401                 /* Accumulating op: handle accumulate step */
13402                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13403 
13404                 switch (opcode) {
13405                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13406                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13407                                              tcg_passres);
13408                     break;
13409                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13410                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13411                                              tcg_passres);
13412                     break;
13413                 case 0x7: /* SQDMLSL, SQDMLSL2 */
13414                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13415                     /* fall through */
13416                 case 0x3: /* SQDMLAL, SQDMLAL2 */
13417                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13418                                                       tcg_res[pass],
13419                                                       tcg_passres);
13420                     break;
13421                 default:
13422                     g_assert_not_reached();
13423                 }
13424             }
13425 
13426             if (is_scalar) {
13427                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13428             }
13429         }
13430 
13431         if (is_scalar) {
13432             tcg_res[1] = tcg_constant_i64(0);
13433         }
13434 
13435         for (pass = 0; pass < 2; pass++) {
13436             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13437         }
13438     }
13439 }
13440 
13441 /* Crypto AES
13442  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13443  * +-----------------+------+-----------+--------+-----+------+------+
13444  * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13445  * +-----------------+------+-----------+--------+-----+------+------+
13446  */
13447 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13448 {
13449     int size = extract32(insn, 22, 2);
13450     int opcode = extract32(insn, 12, 5);
13451     int rn = extract32(insn, 5, 5);
13452     int rd = extract32(insn, 0, 5);
13453     int decrypt;
13454     gen_helper_gvec_2 *genfn2 = NULL;
13455     gen_helper_gvec_3 *genfn3 = NULL;
13456 
13457     if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13458         unallocated_encoding(s);
13459         return;
13460     }
13461 
13462     switch (opcode) {
13463     case 0x4: /* AESE */
13464         decrypt = 0;
13465         genfn3 = gen_helper_crypto_aese;
13466         break;
13467     case 0x6: /* AESMC */
13468         decrypt = 0;
13469         genfn2 = gen_helper_crypto_aesmc;
13470         break;
13471     case 0x5: /* AESD */
13472         decrypt = 1;
13473         genfn3 = gen_helper_crypto_aese;
13474         break;
13475     case 0x7: /* AESIMC */
13476         decrypt = 1;
13477         genfn2 = gen_helper_crypto_aesmc;
13478         break;
13479     default:
13480         unallocated_encoding(s);
13481         return;
13482     }
13483 
13484     if (!fp_access_check(s)) {
13485         return;
13486     }
13487     if (genfn2) {
13488         gen_gvec_op2_ool(s, true, rd, rn, decrypt, genfn2);
13489     } else {
13490         gen_gvec_op3_ool(s, true, rd, rd, rn, decrypt, genfn3);
13491     }
13492 }
13493 
13494 /* Crypto three-reg SHA
13495  *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
13496  * +-----------------+------+---+------+---+--------+-----+------+------+
13497  * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
13498  * +-----------------+------+---+------+---+--------+-----+------+------+
13499  */
13500 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13501 {
13502     int size = extract32(insn, 22, 2);
13503     int opcode = extract32(insn, 12, 3);
13504     int rm = extract32(insn, 16, 5);
13505     int rn = extract32(insn, 5, 5);
13506     int rd = extract32(insn, 0, 5);
13507     gen_helper_gvec_3 *genfn;
13508     bool feature;
13509 
13510     if (size != 0) {
13511         unallocated_encoding(s);
13512         return;
13513     }
13514 
13515     switch (opcode) {
13516     case 0: /* SHA1C */
13517         genfn = gen_helper_crypto_sha1c;
13518         feature = dc_isar_feature(aa64_sha1, s);
13519         break;
13520     case 1: /* SHA1P */
13521         genfn = gen_helper_crypto_sha1p;
13522         feature = dc_isar_feature(aa64_sha1, s);
13523         break;
13524     case 2: /* SHA1M */
13525         genfn = gen_helper_crypto_sha1m;
13526         feature = dc_isar_feature(aa64_sha1, s);
13527         break;
13528     case 3: /* SHA1SU0 */
13529         genfn = gen_helper_crypto_sha1su0;
13530         feature = dc_isar_feature(aa64_sha1, s);
13531         break;
13532     case 4: /* SHA256H */
13533         genfn = gen_helper_crypto_sha256h;
13534         feature = dc_isar_feature(aa64_sha256, s);
13535         break;
13536     case 5: /* SHA256H2 */
13537         genfn = gen_helper_crypto_sha256h2;
13538         feature = dc_isar_feature(aa64_sha256, s);
13539         break;
13540     case 6: /* SHA256SU1 */
13541         genfn = gen_helper_crypto_sha256su1;
13542         feature = dc_isar_feature(aa64_sha256, s);
13543         break;
13544     default:
13545         unallocated_encoding(s);
13546         return;
13547     }
13548 
13549     if (!feature) {
13550         unallocated_encoding(s);
13551         return;
13552     }
13553 
13554     if (!fp_access_check(s)) {
13555         return;
13556     }
13557     gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
13558 }
13559 
13560 /* Crypto two-reg SHA
13561  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
13562  * +-----------------+------+-----------+--------+-----+------+------+
13563  * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
13564  * +-----------------+------+-----------+--------+-----+------+------+
13565  */
13566 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13567 {
13568     int size = extract32(insn, 22, 2);
13569     int opcode = extract32(insn, 12, 5);
13570     int rn = extract32(insn, 5, 5);
13571     int rd = extract32(insn, 0, 5);
13572     gen_helper_gvec_2 *genfn;
13573     bool feature;
13574 
13575     if (size != 0) {
13576         unallocated_encoding(s);
13577         return;
13578     }
13579 
13580     switch (opcode) {
13581     case 0: /* SHA1H */
13582         feature = dc_isar_feature(aa64_sha1, s);
13583         genfn = gen_helper_crypto_sha1h;
13584         break;
13585     case 1: /* SHA1SU1 */
13586         feature = dc_isar_feature(aa64_sha1, s);
13587         genfn = gen_helper_crypto_sha1su1;
13588         break;
13589     case 2: /* SHA256SU0 */
13590         feature = dc_isar_feature(aa64_sha256, s);
13591         genfn = gen_helper_crypto_sha256su0;
13592         break;
13593     default:
13594         unallocated_encoding(s);
13595         return;
13596     }
13597 
13598     if (!feature) {
13599         unallocated_encoding(s);
13600         return;
13601     }
13602 
13603     if (!fp_access_check(s)) {
13604         return;
13605     }
13606     gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
13607 }
13608 
13609 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
13610 {
13611     tcg_gen_rotli_i64(d, m, 1);
13612     tcg_gen_xor_i64(d, d, n);
13613 }
13614 
13615 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
13616 {
13617     tcg_gen_rotli_vec(vece, d, m, 1);
13618     tcg_gen_xor_vec(vece, d, d, n);
13619 }
13620 
13621 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
13622                    uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
13623 {
13624     static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
13625     static const GVecGen3 op = {
13626         .fni8 = gen_rax1_i64,
13627         .fniv = gen_rax1_vec,
13628         .opt_opc = vecop_list,
13629         .fno = gen_helper_crypto_rax1,
13630         .vece = MO_64,
13631     };
13632     tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
13633 }
13634 
13635 /* Crypto three-reg SHA512
13636  *  31                   21 20  16 15  14  13 12  11  10  9    5 4    0
13637  * +-----------------------+------+---+---+-----+--------+------+------+
13638  * | 1 1 0 0 1 1 1 0 0 1 1 |  Rm  | 1 | O | 0 0 | opcode |  Rn  |  Rd  |
13639  * +-----------------------+------+---+---+-----+--------+------+------+
13640  */
13641 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13642 {
13643     int opcode = extract32(insn, 10, 2);
13644     int o =  extract32(insn, 14, 1);
13645     int rm = extract32(insn, 16, 5);
13646     int rn = extract32(insn, 5, 5);
13647     int rd = extract32(insn, 0, 5);
13648     bool feature;
13649     gen_helper_gvec_3 *oolfn = NULL;
13650     GVecGen3Fn *gvecfn = NULL;
13651 
13652     if (o == 0) {
13653         switch (opcode) {
13654         case 0: /* SHA512H */
13655             feature = dc_isar_feature(aa64_sha512, s);
13656             oolfn = gen_helper_crypto_sha512h;
13657             break;
13658         case 1: /* SHA512H2 */
13659             feature = dc_isar_feature(aa64_sha512, s);
13660             oolfn = gen_helper_crypto_sha512h2;
13661             break;
13662         case 2: /* SHA512SU1 */
13663             feature = dc_isar_feature(aa64_sha512, s);
13664             oolfn = gen_helper_crypto_sha512su1;
13665             break;
13666         case 3: /* RAX1 */
13667             feature = dc_isar_feature(aa64_sha3, s);
13668             gvecfn = gen_gvec_rax1;
13669             break;
13670         default:
13671             g_assert_not_reached();
13672         }
13673     } else {
13674         switch (opcode) {
13675         case 0: /* SM3PARTW1 */
13676             feature = dc_isar_feature(aa64_sm3, s);
13677             oolfn = gen_helper_crypto_sm3partw1;
13678             break;
13679         case 1: /* SM3PARTW2 */
13680             feature = dc_isar_feature(aa64_sm3, s);
13681             oolfn = gen_helper_crypto_sm3partw2;
13682             break;
13683         case 2: /* SM4EKEY */
13684             feature = dc_isar_feature(aa64_sm4, s);
13685             oolfn = gen_helper_crypto_sm4ekey;
13686             break;
13687         default:
13688             unallocated_encoding(s);
13689             return;
13690         }
13691     }
13692 
13693     if (!feature) {
13694         unallocated_encoding(s);
13695         return;
13696     }
13697 
13698     if (!fp_access_check(s)) {
13699         return;
13700     }
13701 
13702     if (oolfn) {
13703         gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
13704     } else {
13705         gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
13706     }
13707 }
13708 
13709 /* Crypto two-reg SHA512
13710  *  31                                     12  11  10  9    5 4    0
13711  * +-----------------------------------------+--------+------+------+
13712  * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode |  Rn  |  Rd  |
13713  * +-----------------------------------------+--------+------+------+
13714  */
13715 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13716 {
13717     int opcode = extract32(insn, 10, 2);
13718     int rn = extract32(insn, 5, 5);
13719     int rd = extract32(insn, 0, 5);
13720     bool feature;
13721 
13722     switch (opcode) {
13723     case 0: /* SHA512SU0 */
13724         feature = dc_isar_feature(aa64_sha512, s);
13725         break;
13726     case 1: /* SM4E */
13727         feature = dc_isar_feature(aa64_sm4, s);
13728         break;
13729     default:
13730         unallocated_encoding(s);
13731         return;
13732     }
13733 
13734     if (!feature) {
13735         unallocated_encoding(s);
13736         return;
13737     }
13738 
13739     if (!fp_access_check(s)) {
13740         return;
13741     }
13742 
13743     switch (opcode) {
13744     case 0: /* SHA512SU0 */
13745         gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
13746         break;
13747     case 1: /* SM4E */
13748         gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
13749         break;
13750     default:
13751         g_assert_not_reached();
13752     }
13753 }
13754 
13755 /* Crypto four-register
13756  *  31               23 22 21 20  16 15  14  10 9    5 4    0
13757  * +-------------------+-----+------+---+------+------+------+
13758  * | 1 1 0 0 1 1 1 0 0 | Op0 |  Rm  | 0 |  Ra  |  Rn  |  Rd  |
13759  * +-------------------+-----+------+---+------+------+------+
13760  */
13761 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13762 {
13763     int op0 = extract32(insn, 21, 2);
13764     int rm = extract32(insn, 16, 5);
13765     int ra = extract32(insn, 10, 5);
13766     int rn = extract32(insn, 5, 5);
13767     int rd = extract32(insn, 0, 5);
13768     bool feature;
13769 
13770     switch (op0) {
13771     case 0: /* EOR3 */
13772     case 1: /* BCAX */
13773         feature = dc_isar_feature(aa64_sha3, s);
13774         break;
13775     case 2: /* SM3SS1 */
13776         feature = dc_isar_feature(aa64_sm3, s);
13777         break;
13778     default:
13779         unallocated_encoding(s);
13780         return;
13781     }
13782 
13783     if (!feature) {
13784         unallocated_encoding(s);
13785         return;
13786     }
13787 
13788     if (!fp_access_check(s)) {
13789         return;
13790     }
13791 
13792     if (op0 < 2) {
13793         TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13794         int pass;
13795 
13796         tcg_op1 = tcg_temp_new_i64();
13797         tcg_op2 = tcg_temp_new_i64();
13798         tcg_op3 = tcg_temp_new_i64();
13799         tcg_res[0] = tcg_temp_new_i64();
13800         tcg_res[1] = tcg_temp_new_i64();
13801 
13802         for (pass = 0; pass < 2; pass++) {
13803             read_vec_element(s, tcg_op1, rn, pass, MO_64);
13804             read_vec_element(s, tcg_op2, rm, pass, MO_64);
13805             read_vec_element(s, tcg_op3, ra, pass, MO_64);
13806 
13807             if (op0 == 0) {
13808                 /* EOR3 */
13809                 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13810             } else {
13811                 /* BCAX */
13812                 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13813             }
13814             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13815         }
13816         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13817         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13818     } else {
13819         TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13820 
13821         tcg_op1 = tcg_temp_new_i32();
13822         tcg_op2 = tcg_temp_new_i32();
13823         tcg_op3 = tcg_temp_new_i32();
13824         tcg_res = tcg_temp_new_i32();
13825         tcg_zero = tcg_constant_i32(0);
13826 
13827         read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13828         read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13829         read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13830 
13831         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13832         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13833         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13834         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13835 
13836         write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13837         write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13838         write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13839         write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13840     }
13841 }
13842 
13843 /* Crypto XAR
13844  *  31                   21 20  16 15    10 9    5 4    0
13845  * +-----------------------+------+--------+------+------+
13846  * | 1 1 0 0 1 1 1 0 1 0 0 |  Rm  |  imm6  |  Rn  |  Rd  |
13847  * +-----------------------+------+--------+------+------+
13848  */
13849 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
13850 {
13851     int rm = extract32(insn, 16, 5);
13852     int imm6 = extract32(insn, 10, 6);
13853     int rn = extract32(insn, 5, 5);
13854     int rd = extract32(insn, 0, 5);
13855 
13856     if (!dc_isar_feature(aa64_sha3, s)) {
13857         unallocated_encoding(s);
13858         return;
13859     }
13860 
13861     if (!fp_access_check(s)) {
13862         return;
13863     }
13864 
13865     gen_gvec_xar(MO_64, vec_full_reg_offset(s, rd),
13866                  vec_full_reg_offset(s, rn),
13867                  vec_full_reg_offset(s, rm), imm6, 16,
13868                  vec_full_reg_size(s));
13869 }
13870 
13871 /* Crypto three-reg imm2
13872  *  31                   21 20  16 15  14 13 12  11  10  9    5 4    0
13873  * +-----------------------+------+-----+------+--------+------+------+
13874  * | 1 1 0 0 1 1 1 0 0 1 0 |  Rm  | 1 0 | imm2 | opcode |  Rn  |  Rd  |
13875  * +-----------------------+------+-----+------+--------+------+------+
13876  */
13877 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
13878 {
13879     static gen_helper_gvec_3 * const fns[4] = {
13880         gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
13881         gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
13882     };
13883     int opcode = extract32(insn, 10, 2);
13884     int imm2 = extract32(insn, 12, 2);
13885     int rm = extract32(insn, 16, 5);
13886     int rn = extract32(insn, 5, 5);
13887     int rd = extract32(insn, 0, 5);
13888 
13889     if (!dc_isar_feature(aa64_sm3, s)) {
13890         unallocated_encoding(s);
13891         return;
13892     }
13893 
13894     if (!fp_access_check(s)) {
13895         return;
13896     }
13897 
13898     gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
13899 }
13900 
13901 /* C3.6 Data processing - SIMD, inc Crypto
13902  *
13903  * As the decode gets a little complex we are using a table based
13904  * approach for this part of the decode.
13905  */
13906 static const AArch64DecodeTable data_proc_simd[] = {
13907     /* pattern  ,  mask     ,  fn                        */
13908     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
13909     { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
13910     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
13911     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
13912     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
13913     { 0x0e000400, 0x9fe08400, disas_simd_copy },
13914     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
13915     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
13916     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
13917     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
13918     { 0x0e000000, 0xbf208c00, disas_simd_tb },
13919     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
13920     { 0x2e000000, 0xbf208400, disas_simd_ext },
13921     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
13922     { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
13923     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
13924     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
13925     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
13926     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
13927     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
13928     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
13929     { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
13930     { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
13931     { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
13932     { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
13933     { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
13934     { 0xce000000, 0xff808000, disas_crypto_four_reg },
13935     { 0xce800000, 0xffe00000, disas_crypto_xar },
13936     { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
13937     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
13938     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
13939     { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
13940     { 0x00000000, 0x00000000, NULL }
13941 };
13942 
13943 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
13944 {
13945     /* Note that this is called with all non-FP cases from
13946      * table C3-6 so it must UNDEF for entries not specifically
13947      * allocated to instructions in that table.
13948      */
13949     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
13950     if (fn) {
13951         fn(s, insn);
13952     } else {
13953         unallocated_encoding(s);
13954     }
13955 }
13956 
13957 /* C3.6 Data processing - SIMD and floating point */
13958 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
13959 {
13960     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
13961         disas_data_proc_fp(s, insn);
13962     } else {
13963         /* SIMD, including crypto */
13964         disas_data_proc_simd(s, insn);
13965     }
13966 }
13967 
13968 static bool trans_OK(DisasContext *s, arg_OK *a)
13969 {
13970     return true;
13971 }
13972 
13973 static bool trans_FAIL(DisasContext *s, arg_OK *a)
13974 {
13975     s->is_nonstreaming = true;
13976     return true;
13977 }
13978 
13979 /**
13980  * is_guarded_page:
13981  * @env: The cpu environment
13982  * @s: The DisasContext
13983  *
13984  * Return true if the page is guarded.
13985  */
13986 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
13987 {
13988     uint64_t addr = s->base.pc_first;
13989 #ifdef CONFIG_USER_ONLY
13990     return page_get_flags(addr) & PAGE_BTI;
13991 #else
13992     CPUTLBEntryFull *full;
13993     void *host;
13994     int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
13995     int flags;
13996 
13997     /*
13998      * We test this immediately after reading an insn, which means
13999      * that the TLB entry must be present and valid, and thus this
14000      * access will never raise an exception.
14001      */
14002     flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
14003                               false, &host, &full, 0);
14004     assert(!(flags & TLB_INVALID_MASK));
14005 
14006     return full->guarded;
14007 #endif
14008 }
14009 
14010 /**
14011  * btype_destination_ok:
14012  * @insn: The instruction at the branch destination
14013  * @bt: SCTLR_ELx.BT
14014  * @btype: PSTATE.BTYPE, and is non-zero
14015  *
14016  * On a guarded page, there are a limited number of insns
14017  * that may be present at the branch target:
14018  *   - branch target identifiers,
14019  *   - paciasp, pacibsp,
14020  *   - BRK insn
14021  *   - HLT insn
14022  * Anything else causes a Branch Target Exception.
14023  *
14024  * Return true if the branch is compatible, false to raise BTITRAP.
14025  */
14026 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
14027 {
14028     if ((insn & 0xfffff01fu) == 0xd503201fu) {
14029         /* HINT space */
14030         switch (extract32(insn, 5, 7)) {
14031         case 0b011001: /* PACIASP */
14032         case 0b011011: /* PACIBSP */
14033             /*
14034              * If SCTLR_ELx.BT, then PACI*SP are not compatible
14035              * with btype == 3.  Otherwise all btype are ok.
14036              */
14037             return !bt || btype != 3;
14038         case 0b100000: /* BTI */
14039             /* Not compatible with any btype.  */
14040             return false;
14041         case 0b100010: /* BTI c */
14042             /* Not compatible with btype == 3 */
14043             return btype != 3;
14044         case 0b100100: /* BTI j */
14045             /* Not compatible with btype == 2 */
14046             return btype != 2;
14047         case 0b100110: /* BTI jc */
14048             /* Compatible with any btype.  */
14049             return true;
14050         }
14051     } else {
14052         switch (insn & 0xffe0001fu) {
14053         case 0xd4200000u: /* BRK */
14054         case 0xd4400000u: /* HLT */
14055             /* Give priority to the breakpoint exception.  */
14056             return true;
14057         }
14058     }
14059     return false;
14060 }
14061 
14062 /* C3.1 A64 instruction index by encoding */
14063 static void disas_a64_legacy(DisasContext *s, uint32_t insn)
14064 {
14065     switch (extract32(insn, 25, 4)) {
14066     case 0xa: case 0xb: /* Branch, exception generation and system insns */
14067         disas_b_exc_sys(s, insn);
14068         break;
14069     case 0x4:
14070     case 0x6:
14071     case 0xc:
14072     case 0xe:      /* Loads and stores */
14073         disas_ldst(s, insn);
14074         break;
14075     case 0x5:
14076     case 0xd:      /* Data processing - register */
14077         disas_data_proc_reg(s, insn);
14078         break;
14079     case 0x7:
14080     case 0xf:      /* Data processing - SIMD and floating point */
14081         disas_data_proc_simd_fp(s, insn);
14082         break;
14083     default:
14084         unallocated_encoding(s);
14085         break;
14086     }
14087 }
14088 
14089 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14090                                           CPUState *cpu)
14091 {
14092     DisasContext *dc = container_of(dcbase, DisasContext, base);
14093     CPUARMState *env = cpu->env_ptr;
14094     ARMCPU *arm_cpu = env_archcpu(env);
14095     CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
14096     int bound, core_mmu_idx;
14097 
14098     dc->isar = &arm_cpu->isar;
14099     dc->condjmp = 0;
14100     dc->pc_save = dc->base.pc_first;
14101     dc->aarch64 = true;
14102     dc->thumb = false;
14103     dc->sctlr_b = 0;
14104     dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
14105     dc->condexec_mask = 0;
14106     dc->condexec_cond = 0;
14107     core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
14108     dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
14109     dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
14110     dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
14111     dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
14112     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14113 #if !defined(CONFIG_USER_ONLY)
14114     dc->user = (dc->current_el == 0);
14115 #endif
14116     dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
14117     dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
14118     dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
14119     dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
14120     dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
14121     dc->fgt_eret = EX_TBFLAG_A64(tb_flags, FGT_ERET);
14122     dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
14123     dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
14124     dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
14125     dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
14126     dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
14127     dc->bt = EX_TBFLAG_A64(tb_flags, BT);
14128     dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
14129     dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
14130     dc->ata = EX_TBFLAG_A64(tb_flags, ATA);
14131     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
14132     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
14133     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
14134     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
14135     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
14136     dc->vec_len = 0;
14137     dc->vec_stride = 0;
14138     dc->cp_regs = arm_cpu->cp_regs;
14139     dc->features = env->features;
14140     dc->dcz_blocksize = arm_cpu->dcz_blocksize;
14141 
14142 #ifdef CONFIG_USER_ONLY
14143     /* In sve_probe_page, we assume TBI is enabled. */
14144     tcg_debug_assert(dc->tbid & 1);
14145 #endif
14146 
14147     dc->lse2 = dc_isar_feature(aa64_lse2, dc);
14148 
14149     /* Single step state. The code-generation logic here is:
14150      *  SS_ACTIVE == 0:
14151      *   generate code with no special handling for single-stepping (except
14152      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14153      *   this happens anyway because those changes are all system register or
14154      *   PSTATE writes).
14155      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14156      *   emit code for one insn
14157      *   emit code to clear PSTATE.SS
14158      *   emit code to generate software step exception for completed step
14159      *   end TB (as usual for having generated an exception)
14160      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14161      *   emit code to generate a software step exception
14162      *   end the TB
14163      */
14164     dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
14165     dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
14166     dc->is_ldex = false;
14167 
14168     /* Bound the number of insns to execute to those left on the page.  */
14169     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14170 
14171     /* If architectural single step active, limit to 1.  */
14172     if (dc->ss_active) {
14173         bound = 1;
14174     }
14175     dc->base.max_insns = MIN(dc->base.max_insns, bound);
14176 }
14177 
14178 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14179 {
14180 }
14181 
14182 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14183 {
14184     DisasContext *dc = container_of(dcbase, DisasContext, base);
14185     target_ulong pc_arg = dc->base.pc_next;
14186 
14187     if (tb_cflags(dcbase->tb) & CF_PCREL) {
14188         pc_arg &= ~TARGET_PAGE_MASK;
14189     }
14190     tcg_gen_insn_start(pc_arg, 0, 0);
14191     dc->insn_start = tcg_last_op();
14192 }
14193 
14194 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14195 {
14196     DisasContext *s = container_of(dcbase, DisasContext, base);
14197     CPUARMState *env = cpu->env_ptr;
14198     uint64_t pc = s->base.pc_next;
14199     uint32_t insn;
14200 
14201     /* Singlestep exceptions have the highest priority. */
14202     if (s->ss_active && !s->pstate_ss) {
14203         /* Singlestep state is Active-pending.
14204          * If we're in this state at the start of a TB then either
14205          *  a) we just took an exception to an EL which is being debugged
14206          *     and this is the first insn in the exception handler
14207          *  b) debug exceptions were masked and we just unmasked them
14208          *     without changing EL (eg by clearing PSTATE.D)
14209          * In either case we're going to take a swstep exception in the
14210          * "did not step an insn" case, and so the syndrome ISV and EX
14211          * bits should be zero.
14212          */
14213         assert(s->base.num_insns == 1);
14214         gen_swstep_exception(s, 0, 0);
14215         s->base.is_jmp = DISAS_NORETURN;
14216         s->base.pc_next = pc + 4;
14217         return;
14218     }
14219 
14220     if (pc & 3) {
14221         /*
14222          * PC alignment fault.  This has priority over the instruction abort
14223          * that we would receive from a translation fault via arm_ldl_code.
14224          * This should only be possible after an indirect branch, at the
14225          * start of the TB.
14226          */
14227         assert(s->base.num_insns == 1);
14228         gen_helper_exception_pc_alignment(cpu_env, tcg_constant_tl(pc));
14229         s->base.is_jmp = DISAS_NORETURN;
14230         s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
14231         return;
14232     }
14233 
14234     s->pc_curr = pc;
14235     insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
14236     s->insn = insn;
14237     s->base.pc_next = pc + 4;
14238 
14239     s->fp_access_checked = false;
14240     s->sve_access_checked = false;
14241 
14242     if (s->pstate_il) {
14243         /*
14244          * Illegal execution state. This has priority over BTI
14245          * exceptions, but comes after instruction abort exceptions.
14246          */
14247         gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
14248         return;
14249     }
14250 
14251     if (dc_isar_feature(aa64_bti, s)) {
14252         if (s->base.num_insns == 1) {
14253             /*
14254              * At the first insn of the TB, compute s->guarded_page.
14255              * We delayed computing this until successfully reading
14256              * the first insn of the TB, above.  This (mostly) ensures
14257              * that the softmmu tlb entry has been populated, and the
14258              * page table GP bit is available.
14259              *
14260              * Note that we need to compute this even if btype == 0,
14261              * because this value is used for BR instructions later
14262              * where ENV is not available.
14263              */
14264             s->guarded_page = is_guarded_page(env, s);
14265 
14266             /* First insn can have btype set to non-zero.  */
14267             tcg_debug_assert(s->btype >= 0);
14268 
14269             /*
14270              * Note that the Branch Target Exception has fairly high
14271              * priority -- below debugging exceptions but above most
14272              * everything else.  This allows us to handle this now
14273              * instead of waiting until the insn is otherwise decoded.
14274              */
14275             if (s->btype != 0
14276                 && s->guarded_page
14277                 && !btype_destination_ok(insn, s->bt, s->btype)) {
14278                 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
14279                 return;
14280             }
14281         } else {
14282             /* Not the first insn: btype must be 0.  */
14283             tcg_debug_assert(s->btype == 0);
14284         }
14285     }
14286 
14287     s->is_nonstreaming = false;
14288     if (s->sme_trap_nonstreaming) {
14289         disas_sme_fa64(s, insn);
14290     }
14291 
14292     if (!disas_a64(s, insn) &&
14293         !disas_sme(s, insn) &&
14294         !disas_sve(s, insn)) {
14295         disas_a64_legacy(s, insn);
14296     }
14297 
14298     /*
14299      * After execution of most insns, btype is reset to 0.
14300      * Note that we set btype == -1 when the insn sets btype.
14301      */
14302     if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14303         reset_btype(s);
14304     }
14305 }
14306 
14307 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14308 {
14309     DisasContext *dc = container_of(dcbase, DisasContext, base);
14310 
14311     if (unlikely(dc->ss_active)) {
14312         /* Note that this means single stepping WFI doesn't halt the CPU.
14313          * For conditional branch insns this is harmless unreachable code as
14314          * gen_goto_tb() has already handled emitting the debug exception
14315          * (and thus a tb-jump is not possible when singlestepping).
14316          */
14317         switch (dc->base.is_jmp) {
14318         default:
14319             gen_a64_update_pc(dc, 4);
14320             /* fall through */
14321         case DISAS_EXIT:
14322         case DISAS_JUMP:
14323             gen_step_complete_exception(dc);
14324             break;
14325         case DISAS_NORETURN:
14326             break;
14327         }
14328     } else {
14329         switch (dc->base.is_jmp) {
14330         case DISAS_NEXT:
14331         case DISAS_TOO_MANY:
14332             gen_goto_tb(dc, 1, 4);
14333             break;
14334         default:
14335         case DISAS_UPDATE_EXIT:
14336             gen_a64_update_pc(dc, 4);
14337             /* fall through */
14338         case DISAS_EXIT:
14339             tcg_gen_exit_tb(NULL, 0);
14340             break;
14341         case DISAS_UPDATE_NOCHAIN:
14342             gen_a64_update_pc(dc, 4);
14343             /* fall through */
14344         case DISAS_JUMP:
14345             tcg_gen_lookup_and_goto_ptr();
14346             break;
14347         case DISAS_NORETURN:
14348         case DISAS_SWI:
14349             break;
14350         case DISAS_WFE:
14351             gen_a64_update_pc(dc, 4);
14352             gen_helper_wfe(cpu_env);
14353             break;
14354         case DISAS_YIELD:
14355             gen_a64_update_pc(dc, 4);
14356             gen_helper_yield(cpu_env);
14357             break;
14358         case DISAS_WFI:
14359             /*
14360              * This is a special case because we don't want to just halt
14361              * the CPU if trying to debug across a WFI.
14362              */
14363             gen_a64_update_pc(dc, 4);
14364             gen_helper_wfi(cpu_env, tcg_constant_i32(4));
14365             /*
14366              * The helper doesn't necessarily throw an exception, but we
14367              * must go back to the main loop to check for interrupts anyway.
14368              */
14369             tcg_gen_exit_tb(NULL, 0);
14370             break;
14371         }
14372     }
14373 }
14374 
14375 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14376                                  CPUState *cpu, FILE *logfile)
14377 {
14378     DisasContext *dc = container_of(dcbase, DisasContext, base);
14379 
14380     fprintf(logfile, "IN: %s\n", lookup_symbol(dc->base.pc_first));
14381     target_disas(logfile, cpu, dc->base.pc_first, dc->base.tb->size);
14382 }
14383 
14384 const TranslatorOps aarch64_translator_ops = {
14385     .init_disas_context = aarch64_tr_init_disas_context,
14386     .tb_start           = aarch64_tr_tb_start,
14387     .insn_start         = aarch64_tr_insn_start,
14388     .translate_insn     = aarch64_tr_translate_insn,
14389     .tb_stop            = aarch64_tr_tb_stop,
14390     .disas_log          = aarch64_tr_disas_log,
14391 };
14392