]> Git Repo - qemu.git/blob - target/arm/translate-a64.c
Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[qemu.git] / target / arm / translate-a64.c
1 /*
2  *  AArch64 translation
3  *
4  *  Copyright (c) 2013 Alexander Graf <[email protected]>
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 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "qemu/osdep.h"
20
21 #include "cpu.h"
22 #include "exec/exec-all.h"
23 #include "tcg-op.h"
24 #include "tcg-op-gvec.h"
25 #include "qemu/log.h"
26 #include "arm_ldst.h"
27 #include "translate.h"
28 #include "internals.h"
29 #include "qemu/host-utils.h"
30
31 #include "exec/semihost.h"
32 #include "exec/gen-icount.h"
33
34 #include "exec/helper-proto.h"
35 #include "exec/helper-gen.h"
36 #include "exec/log.h"
37
38 #include "trace-tcg.h"
39
40 static TCGv_i64 cpu_X[32];
41 static TCGv_i64 cpu_pc;
42
43 /* Load/store exclusive handling */
44 static TCGv_i64 cpu_exclusive_high;
45 static TCGv_i64 cpu_reg(DisasContext *s, int reg);
46
47 static const char *regnames[] = {
48     "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
49     "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
50     "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
51     "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
52 };
53
54 enum a64_shift_type {
55     A64_SHIFT_TYPE_LSL = 0,
56     A64_SHIFT_TYPE_LSR = 1,
57     A64_SHIFT_TYPE_ASR = 2,
58     A64_SHIFT_TYPE_ROR = 3
59 };
60
61 /* Table based decoder typedefs - used when the relevant bits for decode
62  * are too awkwardly scattered across the instruction (eg SIMD).
63  */
64 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
65
66 typedef struct AArch64DecodeTable {
67     uint32_t pattern;
68     uint32_t mask;
69     AArch64DecodeFn *disas_fn;
70 } AArch64DecodeTable;
71
72 /* Function prototype for gen_ functions for calling Neon helpers */
73 typedef void NeonGenOneOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32);
74 typedef void NeonGenTwoOpFn(TCGv_i32, TCGv_i32, TCGv_i32);
75 typedef void NeonGenTwoOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32, TCGv_i32);
76 typedef void NeonGenTwo64OpFn(TCGv_i64, TCGv_i64, TCGv_i64);
77 typedef void NeonGenTwo64OpEnvFn(TCGv_i64, TCGv_ptr, TCGv_i64, TCGv_i64);
78 typedef void NeonGenNarrowFn(TCGv_i32, TCGv_i64);
79 typedef void NeonGenNarrowEnvFn(TCGv_i32, TCGv_ptr, TCGv_i64);
80 typedef void NeonGenWidenFn(TCGv_i64, TCGv_i32);
81 typedef void NeonGenTwoSingleOPFn(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
82 typedef void NeonGenTwoDoubleOPFn(TCGv_i64, TCGv_i64, TCGv_i64, TCGv_ptr);
83 typedef void NeonGenOneOpFn(TCGv_i64, TCGv_i64);
84 typedef void CryptoTwoOpFn(TCGv_ptr, TCGv_ptr);
85 typedef void CryptoThreeOpIntFn(TCGv_ptr, TCGv_ptr, TCGv_i32);
86 typedef void CryptoThreeOpFn(TCGv_ptr, TCGv_ptr, TCGv_ptr);
87
88 /* Note that the gvec expanders operate on offsets + sizes.  */
89 typedef void GVecGen2Fn(unsigned, uint32_t, uint32_t, uint32_t, uint32_t);
90 typedef void GVecGen2iFn(unsigned, uint32_t, uint32_t, int64_t,
91                          uint32_t, uint32_t);
92 typedef void GVecGen3Fn(unsigned, uint32_t, uint32_t,
93                         uint32_t, uint32_t, uint32_t);
94
95 /* initialize TCG globals.  */
96 void a64_translate_init(void)
97 {
98     int i;
99
100     cpu_pc = tcg_global_mem_new_i64(cpu_env,
101                                     offsetof(CPUARMState, pc),
102                                     "pc");
103     for (i = 0; i < 32; i++) {
104         cpu_X[i] = tcg_global_mem_new_i64(cpu_env,
105                                           offsetof(CPUARMState, xregs[i]),
106                                           regnames[i]);
107     }
108
109     cpu_exclusive_high = tcg_global_mem_new_i64(cpu_env,
110         offsetof(CPUARMState, exclusive_high), "exclusive_high");
111 }
112
113 static inline int get_a64_user_mem_index(DisasContext *s)
114 {
115     /* Return the core mmu_idx to use for A64 "unprivileged load/store" insns:
116      *  if EL1, access as if EL0; otherwise access at current EL
117      */
118     ARMMMUIdx useridx;
119
120     switch (s->mmu_idx) {
121     case ARMMMUIdx_S12NSE1:
122         useridx = ARMMMUIdx_S12NSE0;
123         break;
124     case ARMMMUIdx_S1SE1:
125         useridx = ARMMMUIdx_S1SE0;
126         break;
127     case ARMMMUIdx_S2NS:
128         g_assert_not_reached();
129     default:
130         useridx = s->mmu_idx;
131         break;
132     }
133     return arm_to_core_mmu_idx(useridx);
134 }
135
136 void aarch64_cpu_dump_state(CPUState *cs, FILE *f,
137                             fprintf_function cpu_fprintf, int flags)
138 {
139     ARMCPU *cpu = ARM_CPU(cs);
140     CPUARMState *env = &cpu->env;
141     uint32_t psr = pstate_read(env);
142     int i;
143     int el = arm_current_el(env);
144     const char *ns_status;
145
146     cpu_fprintf(f, "PC=%016"PRIx64"  SP=%016"PRIx64"\n",
147             env->pc, env->xregs[31]);
148     for (i = 0; i < 31; i++) {
149         cpu_fprintf(f, "X%02d=%016"PRIx64, i, env->xregs[i]);
150         if ((i % 4) == 3) {
151             cpu_fprintf(f, "\n");
152         } else {
153             cpu_fprintf(f, " ");
154         }
155     }
156
157     if (arm_feature(env, ARM_FEATURE_EL3) && el != 3) {
158         ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
159     } else {
160         ns_status = "";
161     }
162
163     cpu_fprintf(f, "\nPSTATE=%08x %c%c%c%c %sEL%d%c\n",
164                 psr,
165                 psr & PSTATE_N ? 'N' : '-',
166                 psr & PSTATE_Z ? 'Z' : '-',
167                 psr & PSTATE_C ? 'C' : '-',
168                 psr & PSTATE_V ? 'V' : '-',
169                 ns_status,
170                 el,
171                 psr & PSTATE_SP ? 'h' : 't');
172
173     if (flags & CPU_DUMP_FPU) {
174         int numvfpregs = 32;
175         for (i = 0; i < numvfpregs; i++) {
176             uint64_t *q = aa64_vfp_qreg(env, i);
177             uint64_t vlo = q[0];
178             uint64_t vhi = q[1];
179             cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 "%c",
180                         i, vhi, vlo, (i & 1 ? '\n' : ' '));
181         }
182         cpu_fprintf(f, "FPCR: %08x  FPSR: %08x\n",
183                     vfp_get_fpcr(env), vfp_get_fpsr(env));
184     }
185 }
186
187 void gen_a64_set_pc_im(uint64_t val)
188 {
189     tcg_gen_movi_i64(cpu_pc, val);
190 }
191
192 /* Load the PC from a generic TCG variable.
193  *
194  * If address tagging is enabled via the TCR TBI bits, then loading
195  * an address into the PC will clear out any tag in the it:
196  *  + for EL2 and EL3 there is only one TBI bit, and if it is set
197  *    then the address is zero-extended, clearing bits [63:56]
198  *  + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
199  *    and TBI1 controls addressses with bit 55 == 1.
200  *    If the appropriate TBI bit is set for the address then
201  *    the address is sign-extended from bit 55 into bits [63:56]
202  *
203  * We can avoid doing this for relative-branches, because the
204  * PC + offset can never overflow into the tag bits (assuming
205  * that virtual addresses are less than 56 bits wide, as they
206  * are currently), but we must handle it for branch-to-register.
207  */
208 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
209 {
210
211     if (s->current_el <= 1) {
212         /* Test if NEITHER or BOTH TBI values are set.  If so, no need to
213          * examine bit 55 of address, can just generate code.
214          * If mixed, then test via generated code
215          */
216         if (s->tbi0 && s->tbi1) {
217             TCGv_i64 tmp_reg = tcg_temp_new_i64();
218             /* Both bits set, sign extension from bit 55 into [63:56] will
219              * cover both cases
220              */
221             tcg_gen_shli_i64(tmp_reg, src, 8);
222             tcg_gen_sari_i64(cpu_pc, tmp_reg, 8);
223             tcg_temp_free_i64(tmp_reg);
224         } else if (!s->tbi0 && !s->tbi1) {
225             /* Neither bit set, just load it as-is */
226             tcg_gen_mov_i64(cpu_pc, src);
227         } else {
228             TCGv_i64 tcg_tmpval = tcg_temp_new_i64();
229             TCGv_i64 tcg_bit55  = tcg_temp_new_i64();
230             TCGv_i64 tcg_zero   = tcg_const_i64(0);
231
232             tcg_gen_andi_i64(tcg_bit55, src, (1ull << 55));
233
234             if (s->tbi0) {
235                 /* tbi0==1, tbi1==0, so 0-fill upper byte if bit 55 = 0 */
236                 tcg_gen_andi_i64(tcg_tmpval, src,
237                                  0x00FFFFFFFFFFFFFFull);
238                 tcg_gen_movcond_i64(TCG_COND_EQ, cpu_pc, tcg_bit55, tcg_zero,
239                                     tcg_tmpval, src);
240             } else {
241                 /* tbi0==0, tbi1==1, so 1-fill upper byte if bit 55 = 1 */
242                 tcg_gen_ori_i64(tcg_tmpval, src,
243                                 0xFF00000000000000ull);
244                 tcg_gen_movcond_i64(TCG_COND_NE, cpu_pc, tcg_bit55, tcg_zero,
245                                     tcg_tmpval, src);
246             }
247             tcg_temp_free_i64(tcg_zero);
248             tcg_temp_free_i64(tcg_bit55);
249             tcg_temp_free_i64(tcg_tmpval);
250         }
251     } else {  /* EL > 1 */
252         if (s->tbi0) {
253             /* Force tag byte to all zero */
254             tcg_gen_andi_i64(cpu_pc, src, 0x00FFFFFFFFFFFFFFull);
255         } else {
256             /* Load unmodified address */
257             tcg_gen_mov_i64(cpu_pc, src);
258         }
259     }
260 }
261
262 typedef struct DisasCompare64 {
263     TCGCond cond;
264     TCGv_i64 value;
265 } DisasCompare64;
266
267 static void a64_test_cc(DisasCompare64 *c64, int cc)
268 {
269     DisasCompare c32;
270
271     arm_test_cc(&c32, cc);
272
273     /* Sign-extend the 32-bit value so that the GE/LT comparisons work
274        * properly.  The NE/EQ comparisons are also fine with this choice.  */
275     c64->cond = c32.cond;
276     c64->value = tcg_temp_new_i64();
277     tcg_gen_ext_i32_i64(c64->value, c32.value);
278
279     arm_free_cc(&c32);
280 }
281
282 static void a64_free_cc(DisasCompare64 *c64)
283 {
284     tcg_temp_free_i64(c64->value);
285 }
286
287 static void gen_exception_internal(int excp)
288 {
289     TCGv_i32 tcg_excp = tcg_const_i32(excp);
290
291     assert(excp_is_internal(excp));
292     gen_helper_exception_internal(cpu_env, tcg_excp);
293     tcg_temp_free_i32(tcg_excp);
294 }
295
296 static void gen_exception(int excp, uint32_t syndrome, uint32_t target_el)
297 {
298     TCGv_i32 tcg_excp = tcg_const_i32(excp);
299     TCGv_i32 tcg_syn = tcg_const_i32(syndrome);
300     TCGv_i32 tcg_el = tcg_const_i32(target_el);
301
302     gen_helper_exception_with_syndrome(cpu_env, tcg_excp,
303                                        tcg_syn, tcg_el);
304     tcg_temp_free_i32(tcg_el);
305     tcg_temp_free_i32(tcg_syn);
306     tcg_temp_free_i32(tcg_excp);
307 }
308
309 static void gen_exception_internal_insn(DisasContext *s, int offset, int excp)
310 {
311     gen_a64_set_pc_im(s->pc - offset);
312     gen_exception_internal(excp);
313     s->base.is_jmp = DISAS_NORETURN;
314 }
315
316 static void gen_exception_insn(DisasContext *s, int offset, int excp,
317                                uint32_t syndrome, uint32_t target_el)
318 {
319     gen_a64_set_pc_im(s->pc - offset);
320     gen_exception(excp, syndrome, target_el);
321     s->base.is_jmp = DISAS_NORETURN;
322 }
323
324 static void gen_ss_advance(DisasContext *s)
325 {
326     /* If the singlestep state is Active-not-pending, advance to
327      * Active-pending.
328      */
329     if (s->ss_active) {
330         s->pstate_ss = 0;
331         gen_helper_clear_pstate_ss(cpu_env);
332     }
333 }
334
335 static void gen_step_complete_exception(DisasContext *s)
336 {
337     /* We just completed step of an insn. Move from Active-not-pending
338      * to Active-pending, and then also take the swstep exception.
339      * This corresponds to making the (IMPDEF) choice to prioritize
340      * swstep exceptions over asynchronous exceptions taken to an exception
341      * level where debug is disabled. This choice has the advantage that
342      * we do not need to maintain internal state corresponding to the
343      * ISV/EX syndrome bits between completion of the step and generation
344      * of the exception, and our syndrome information is always correct.
345      */
346     gen_ss_advance(s);
347     gen_exception(EXCP_UDEF, syn_swstep(s->ss_same_el, 1, s->is_ldex),
348                   default_exception_el(s));
349     s->base.is_jmp = DISAS_NORETURN;
350 }
351
352 static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
353 {
354     /* No direct tb linking with singlestep (either QEMU's or the ARM
355      * debug architecture kind) or deterministic io
356      */
357     if (s->base.singlestep_enabled || s->ss_active ||
358         (tb_cflags(s->base.tb) & CF_LAST_IO)) {
359         return false;
360     }
361
362 #ifndef CONFIG_USER_ONLY
363     /* Only link tbs from inside the same guest page */
364     if ((s->base.tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
365         return false;
366     }
367 #endif
368
369     return true;
370 }
371
372 static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
373 {
374     TranslationBlock *tb;
375
376     tb = s->base.tb;
377     if (use_goto_tb(s, n, dest)) {
378         tcg_gen_goto_tb(n);
379         gen_a64_set_pc_im(dest);
380         tcg_gen_exit_tb((intptr_t)tb + n);
381         s->base.is_jmp = DISAS_NORETURN;
382     } else {
383         gen_a64_set_pc_im(dest);
384         if (s->ss_active) {
385             gen_step_complete_exception(s);
386         } else if (s->base.singlestep_enabled) {
387             gen_exception_internal(EXCP_DEBUG);
388         } else {
389             tcg_gen_lookup_and_goto_ptr();
390             s->base.is_jmp = DISAS_NORETURN;
391         }
392     }
393 }
394
395 static void unallocated_encoding(DisasContext *s)
396 {
397     /* Unallocated and reserved encodings are uncategorized */
398     gen_exception_insn(s, 4, EXCP_UDEF, syn_uncategorized(),
399                        default_exception_el(s));
400 }
401
402 #define unsupported_encoding(s, insn)                                    \
403     do {                                                                 \
404         qemu_log_mask(LOG_UNIMP,                                         \
405                       "%s:%d: unsupported instruction encoding 0x%08x "  \
406                       "at pc=%016" PRIx64 "\n",                          \
407                       __FILE__, __LINE__, insn, s->pc - 4);              \
408         unallocated_encoding(s);                                         \
409     } while (0)
410
411 static void init_tmp_a64_array(DisasContext *s)
412 {
413 #ifdef CONFIG_DEBUG_TCG
414     memset(s->tmp_a64, 0, sizeof(s->tmp_a64));
415 #endif
416     s->tmp_a64_count = 0;
417 }
418
419 static void free_tmp_a64(DisasContext *s)
420 {
421     int i;
422     for (i = 0; i < s->tmp_a64_count; i++) {
423         tcg_temp_free_i64(s->tmp_a64[i]);
424     }
425     init_tmp_a64_array(s);
426 }
427
428 static TCGv_i64 new_tmp_a64(DisasContext *s)
429 {
430     assert(s->tmp_a64_count < TMP_A64_MAX);
431     return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
432 }
433
434 static TCGv_i64 new_tmp_a64_zero(DisasContext *s)
435 {
436     TCGv_i64 t = new_tmp_a64(s);
437     tcg_gen_movi_i64(t, 0);
438     return t;
439 }
440
441 /*
442  * Register access functions
443  *
444  * These functions are used for directly accessing a register in where
445  * changes to the final register value are likely to be made. If you
446  * need to use a register for temporary calculation (e.g. index type
447  * operations) use the read_* form.
448  *
449  * B1.2.1 Register mappings
450  *
451  * In instruction register encoding 31 can refer to ZR (zero register) or
452  * the SP (stack pointer) depending on context. In QEMU's case we map SP
453  * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
454  * This is the point of the _sp forms.
455  */
456 static TCGv_i64 cpu_reg(DisasContext *s, int reg)
457 {
458     if (reg == 31) {
459         return new_tmp_a64_zero(s);
460     } else {
461         return cpu_X[reg];
462     }
463 }
464
465 /* register access for when 31 == SP */
466 static TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
467 {
468     return cpu_X[reg];
469 }
470
471 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
472  * representing the register contents. This TCGv is an auto-freed
473  * temporary so it need not be explicitly freed, and may be modified.
474  */
475 static TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
476 {
477     TCGv_i64 v = new_tmp_a64(s);
478     if (reg != 31) {
479         if (sf) {
480             tcg_gen_mov_i64(v, cpu_X[reg]);
481         } else {
482             tcg_gen_ext32u_i64(v, cpu_X[reg]);
483         }
484     } else {
485         tcg_gen_movi_i64(v, 0);
486     }
487     return v;
488 }
489
490 static TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
491 {
492     TCGv_i64 v = new_tmp_a64(s);
493     if (sf) {
494         tcg_gen_mov_i64(v, cpu_X[reg]);
495     } else {
496         tcg_gen_ext32u_i64(v, cpu_X[reg]);
497     }
498     return v;
499 }
500
501 /* We should have at some point before trying to access an FP register
502  * done the necessary access check, so assert that
503  * (a) we did the check and
504  * (b) we didn't then just plough ahead anyway if it failed.
505  * Print the instruction pattern in the abort message so we can figure
506  * out what we need to fix if a user encounters this problem in the wild.
507  */
508 static inline void assert_fp_access_checked(DisasContext *s)
509 {
510 #ifdef CONFIG_DEBUG_TCG
511     if (unlikely(!s->fp_access_checked || s->fp_excp_el)) {
512         fprintf(stderr, "target-arm: FP access check missing for "
513                 "instruction 0x%08x\n", s->insn);
514         abort();
515     }
516 #endif
517 }
518
519 /* Return the offset into CPUARMState of an element of specified
520  * size, 'element' places in from the least significant end of
521  * the FP/vector register Qn.
522  */
523 static inline int vec_reg_offset(DisasContext *s, int regno,
524                                  int element, TCGMemOp size)
525 {
526     int offs = 0;
527 #ifdef HOST_WORDS_BIGENDIAN
528     /* This is complicated slightly because vfp.zregs[n].d[0] is
529      * still the low half and vfp.zregs[n].d[1] the high half
530      * of the 128 bit vector, even on big endian systems.
531      * Calculate the offset assuming a fully bigendian 128 bits,
532      * then XOR to account for the order of the two 64 bit halves.
533      */
534     offs += (16 - ((element + 1) * (1 << size)));
535     offs ^= 8;
536 #else
537     offs += element * (1 << size);
538 #endif
539     offs += offsetof(CPUARMState, vfp.zregs[regno]);
540     assert_fp_access_checked(s);
541     return offs;
542 }
543
544 /* Return the offset info CPUARMState of the "whole" vector register Qn.  */
545 static inline int vec_full_reg_offset(DisasContext *s, int regno)
546 {
547     assert_fp_access_checked(s);
548     return offsetof(CPUARMState, vfp.zregs[regno]);
549 }
550
551 /* Return a newly allocated pointer to the vector register.  */
552 static TCGv_ptr vec_full_reg_ptr(DisasContext *s, int regno)
553 {
554     TCGv_ptr ret = tcg_temp_new_ptr();
555     tcg_gen_addi_ptr(ret, cpu_env, vec_full_reg_offset(s, regno));
556     return ret;
557 }
558
559 /* Return the byte size of the "whole" vector register, VL / 8.  */
560 static inline int vec_full_reg_size(DisasContext *s)
561 {
562     /* FIXME SVE: We should put the composite ZCR_EL* value into tb->flags.
563        In the meantime this is just the AdvSIMD length of 128.  */
564     return 128 / 8;
565 }
566
567 /* Return the offset into CPUARMState of a slice (from
568  * the least significant end) of FP register Qn (ie
569  * Dn, Sn, Hn or Bn).
570  * (Note that this is not the same mapping as for A32; see cpu.h)
571  */
572 static inline int fp_reg_offset(DisasContext *s, int regno, TCGMemOp size)
573 {
574     return vec_reg_offset(s, regno, 0, size);
575 }
576
577 /* Offset of the high half of the 128 bit vector Qn */
578 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
579 {
580     return vec_reg_offset(s, regno, 1, MO_64);
581 }
582
583 /* Convenience accessors for reading and writing single and double
584  * FP registers. Writing clears the upper parts of the associated
585  * 128 bit vector register, as required by the architecture.
586  * Note that unlike the GP register accessors, the values returned
587  * by the read functions must be manually freed.
588  */
589 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
590 {
591     TCGv_i64 v = tcg_temp_new_i64();
592
593     tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
594     return v;
595 }
596
597 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
598 {
599     TCGv_i32 v = tcg_temp_new_i32();
600
601     tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
602     return v;
603 }
604
605 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
606  * If SVE is not enabled, then there are only 128 bits in the vector.
607  */
608 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
609 {
610     unsigned ofs = fp_reg_offset(s, rd, MO_64);
611     unsigned vsz = vec_full_reg_size(s);
612
613     if (!is_q) {
614         TCGv_i64 tcg_zero = tcg_const_i64(0);
615         tcg_gen_st_i64(tcg_zero, cpu_env, ofs + 8);
616         tcg_temp_free_i64(tcg_zero);
617     }
618     if (vsz > 16) {
619         tcg_gen_gvec_dup8i(ofs + 16, vsz - 16, vsz - 16, 0);
620     }
621 }
622
623 static void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
624 {
625     unsigned ofs = fp_reg_offset(s, reg, MO_64);
626
627     tcg_gen_st_i64(v, cpu_env, ofs);
628     clear_vec_high(s, false, reg);
629 }
630
631 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
632 {
633     TCGv_i64 tmp = tcg_temp_new_i64();
634
635     tcg_gen_extu_i32_i64(tmp, v);
636     write_fp_dreg(s, reg, tmp);
637     tcg_temp_free_i64(tmp);
638 }
639
640 static TCGv_ptr get_fpstatus_ptr(bool is_f16)
641 {
642     TCGv_ptr statusptr = tcg_temp_new_ptr();
643     int offset;
644
645     /* In A64 all instructions (both FP and Neon) use the FPCR; there
646      * is no equivalent of the A32 Neon "standard FPSCR value".
647      * However half-precision operations operate under a different
648      * FZ16 flag and use vfp.fp_status_f16 instead of vfp.fp_status.
649      */
650     if (is_f16) {
651         offset = offsetof(CPUARMState, vfp.fp_status_f16);
652     } else {
653         offset = offsetof(CPUARMState, vfp.fp_status);
654     }
655     tcg_gen_addi_ptr(statusptr, cpu_env, offset);
656     return statusptr;
657 }
658
659 /* Expand a 2-operand AdvSIMD vector operation using an expander function.  */
660 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
661                          GVecGen2Fn *gvec_fn, int vece)
662 {
663     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
664             is_q ? 16 : 8, vec_full_reg_size(s));
665 }
666
667 /* Expand a 2-operand + immediate AdvSIMD vector operation using
668  * an expander function.
669  */
670 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
671                           int64_t imm, GVecGen2iFn *gvec_fn, int vece)
672 {
673     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
674             imm, is_q ? 16 : 8, vec_full_reg_size(s));
675 }
676
677 /* Expand a 3-operand AdvSIMD vector operation using an expander function.  */
678 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
679                          GVecGen3Fn *gvec_fn, int vece)
680 {
681     gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
682             vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
683 }
684
685 /* Expand a 2-operand + immediate AdvSIMD vector operation using
686  * an op descriptor.
687  */
688 static void gen_gvec_op2i(DisasContext *s, bool is_q, int rd,
689                           int rn, int64_t imm, const GVecGen2i *gvec_op)
690 {
691     tcg_gen_gvec_2i(vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
692                     is_q ? 16 : 8, vec_full_reg_size(s), imm, gvec_op);
693 }
694
695 /* Expand a 3-operand AdvSIMD vector operation using an op descriptor.  */
696 static void gen_gvec_op3(DisasContext *s, bool is_q, int rd,
697                          int rn, int rm, const GVecGen3 *gvec_op)
698 {
699     tcg_gen_gvec_3(vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
700                    vec_full_reg_offset(s, rm), is_q ? 16 : 8,
701                    vec_full_reg_size(s), gvec_op);
702 }
703
704 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
705  * than the 32 bit equivalent.
706  */
707 static inline void gen_set_NZ64(TCGv_i64 result)
708 {
709     tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
710     tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
711 }
712
713 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
714 static inline void gen_logic_CC(int sf, TCGv_i64 result)
715 {
716     if (sf) {
717         gen_set_NZ64(result);
718     } else {
719         tcg_gen_extrl_i64_i32(cpu_ZF, result);
720         tcg_gen_mov_i32(cpu_NF, cpu_ZF);
721     }
722     tcg_gen_movi_i32(cpu_CF, 0);
723     tcg_gen_movi_i32(cpu_VF, 0);
724 }
725
726 /* dest = T0 + T1; compute C, N, V and Z flags */
727 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
728 {
729     if (sf) {
730         TCGv_i64 result, flag, tmp;
731         result = tcg_temp_new_i64();
732         flag = tcg_temp_new_i64();
733         tmp = tcg_temp_new_i64();
734
735         tcg_gen_movi_i64(tmp, 0);
736         tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
737
738         tcg_gen_extrl_i64_i32(cpu_CF, flag);
739
740         gen_set_NZ64(result);
741
742         tcg_gen_xor_i64(flag, result, t0);
743         tcg_gen_xor_i64(tmp, t0, t1);
744         tcg_gen_andc_i64(flag, flag, tmp);
745         tcg_temp_free_i64(tmp);
746         tcg_gen_extrh_i64_i32(cpu_VF, flag);
747
748         tcg_gen_mov_i64(dest, result);
749         tcg_temp_free_i64(result);
750         tcg_temp_free_i64(flag);
751     } else {
752         /* 32 bit arithmetic */
753         TCGv_i32 t0_32 = tcg_temp_new_i32();
754         TCGv_i32 t1_32 = tcg_temp_new_i32();
755         TCGv_i32 tmp = tcg_temp_new_i32();
756
757         tcg_gen_movi_i32(tmp, 0);
758         tcg_gen_extrl_i64_i32(t0_32, t0);
759         tcg_gen_extrl_i64_i32(t1_32, t1);
760         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
761         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
762         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
763         tcg_gen_xor_i32(tmp, t0_32, t1_32);
764         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
765         tcg_gen_extu_i32_i64(dest, cpu_NF);
766
767         tcg_temp_free_i32(tmp);
768         tcg_temp_free_i32(t0_32);
769         tcg_temp_free_i32(t1_32);
770     }
771 }
772
773 /* dest = T0 - T1; compute C, N, V and Z flags */
774 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
775 {
776     if (sf) {
777         /* 64 bit arithmetic */
778         TCGv_i64 result, flag, tmp;
779
780         result = tcg_temp_new_i64();
781         flag = tcg_temp_new_i64();
782         tcg_gen_sub_i64(result, t0, t1);
783
784         gen_set_NZ64(result);
785
786         tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
787         tcg_gen_extrl_i64_i32(cpu_CF, flag);
788
789         tcg_gen_xor_i64(flag, result, t0);
790         tmp = tcg_temp_new_i64();
791         tcg_gen_xor_i64(tmp, t0, t1);
792         tcg_gen_and_i64(flag, flag, tmp);
793         tcg_temp_free_i64(tmp);
794         tcg_gen_extrh_i64_i32(cpu_VF, flag);
795         tcg_gen_mov_i64(dest, result);
796         tcg_temp_free_i64(flag);
797         tcg_temp_free_i64(result);
798     } else {
799         /* 32 bit arithmetic */
800         TCGv_i32 t0_32 = tcg_temp_new_i32();
801         TCGv_i32 t1_32 = tcg_temp_new_i32();
802         TCGv_i32 tmp;
803
804         tcg_gen_extrl_i64_i32(t0_32, t0);
805         tcg_gen_extrl_i64_i32(t1_32, t1);
806         tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
807         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
808         tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
809         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
810         tmp = tcg_temp_new_i32();
811         tcg_gen_xor_i32(tmp, t0_32, t1_32);
812         tcg_temp_free_i32(t0_32);
813         tcg_temp_free_i32(t1_32);
814         tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
815         tcg_temp_free_i32(tmp);
816         tcg_gen_extu_i32_i64(dest, cpu_NF);
817     }
818 }
819
820 /* dest = T0 + T1 + CF; do not compute flags. */
821 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
822 {
823     TCGv_i64 flag = tcg_temp_new_i64();
824     tcg_gen_extu_i32_i64(flag, cpu_CF);
825     tcg_gen_add_i64(dest, t0, t1);
826     tcg_gen_add_i64(dest, dest, flag);
827     tcg_temp_free_i64(flag);
828
829     if (!sf) {
830         tcg_gen_ext32u_i64(dest, dest);
831     }
832 }
833
834 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
835 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
836 {
837     if (sf) {
838         TCGv_i64 result, cf_64, vf_64, tmp;
839         result = tcg_temp_new_i64();
840         cf_64 = tcg_temp_new_i64();
841         vf_64 = tcg_temp_new_i64();
842         tmp = tcg_const_i64(0);
843
844         tcg_gen_extu_i32_i64(cf_64, cpu_CF);
845         tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
846         tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
847         tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
848         gen_set_NZ64(result);
849
850         tcg_gen_xor_i64(vf_64, result, t0);
851         tcg_gen_xor_i64(tmp, t0, t1);
852         tcg_gen_andc_i64(vf_64, vf_64, tmp);
853         tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
854
855         tcg_gen_mov_i64(dest, result);
856
857         tcg_temp_free_i64(tmp);
858         tcg_temp_free_i64(vf_64);
859         tcg_temp_free_i64(cf_64);
860         tcg_temp_free_i64(result);
861     } else {
862         TCGv_i32 t0_32, t1_32, tmp;
863         t0_32 = tcg_temp_new_i32();
864         t1_32 = tcg_temp_new_i32();
865         tmp = tcg_const_i32(0);
866
867         tcg_gen_extrl_i64_i32(t0_32, t0);
868         tcg_gen_extrl_i64_i32(t1_32, t1);
869         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
870         tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
871
872         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
873         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
874         tcg_gen_xor_i32(tmp, t0_32, t1_32);
875         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
876         tcg_gen_extu_i32_i64(dest, cpu_NF);
877
878         tcg_temp_free_i32(tmp);
879         tcg_temp_free_i32(t1_32);
880         tcg_temp_free_i32(t0_32);
881     }
882 }
883
884 /*
885  * Load/Store generators
886  */
887
888 /*
889  * Store from GPR register to memory.
890  */
891 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
892                              TCGv_i64 tcg_addr, int size, int memidx,
893                              bool iss_valid,
894                              unsigned int iss_srt,
895                              bool iss_sf, bool iss_ar)
896 {
897     g_assert(size <= 3);
898     tcg_gen_qemu_st_i64(source, tcg_addr, memidx, s->be_data + size);
899
900     if (iss_valid) {
901         uint32_t syn;
902
903         syn = syn_data_abort_with_iss(0,
904                                       size,
905                                       false,
906                                       iss_srt,
907                                       iss_sf,
908                                       iss_ar,
909                                       0, 0, 0, 0, 0, false);
910         disas_set_insn_syndrome(s, syn);
911     }
912 }
913
914 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
915                       TCGv_i64 tcg_addr, int size,
916                       bool iss_valid,
917                       unsigned int iss_srt,
918                       bool iss_sf, bool iss_ar)
919 {
920     do_gpr_st_memidx(s, source, tcg_addr, size, get_mem_index(s),
921                      iss_valid, iss_srt, iss_sf, iss_ar);
922 }
923
924 /*
925  * Load from memory to GPR register
926  */
927 static void do_gpr_ld_memidx(DisasContext *s,
928                              TCGv_i64 dest, TCGv_i64 tcg_addr,
929                              int size, bool is_signed,
930                              bool extend, int memidx,
931                              bool iss_valid, unsigned int iss_srt,
932                              bool iss_sf, bool iss_ar)
933 {
934     TCGMemOp memop = s->be_data + size;
935
936     g_assert(size <= 3);
937
938     if (is_signed) {
939         memop += MO_SIGN;
940     }
941
942     tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
943
944     if (extend && is_signed) {
945         g_assert(size < 3);
946         tcg_gen_ext32u_i64(dest, dest);
947     }
948
949     if (iss_valid) {
950         uint32_t syn;
951
952         syn = syn_data_abort_with_iss(0,
953                                       size,
954                                       is_signed,
955                                       iss_srt,
956                                       iss_sf,
957                                       iss_ar,
958                                       0, 0, 0, 0, 0, false);
959         disas_set_insn_syndrome(s, syn);
960     }
961 }
962
963 static void do_gpr_ld(DisasContext *s,
964                       TCGv_i64 dest, TCGv_i64 tcg_addr,
965                       int size, bool is_signed, bool extend,
966                       bool iss_valid, unsigned int iss_srt,
967                       bool iss_sf, bool iss_ar)
968 {
969     do_gpr_ld_memidx(s, dest, tcg_addr, size, is_signed, extend,
970                      get_mem_index(s),
971                      iss_valid, iss_srt, iss_sf, iss_ar);
972 }
973
974 /*
975  * Store from FP register to memory
976  */
977 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
978 {
979     /* This writes the bottom N bits of a 128 bit wide vector to memory */
980     TCGv_i64 tmp = tcg_temp_new_i64();
981     tcg_gen_ld_i64(tmp, cpu_env, fp_reg_offset(s, srcidx, MO_64));
982     if (size < 4) {
983         tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s),
984                             s->be_data + size);
985     } else {
986         bool be = s->be_data == MO_BE;
987         TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
988
989         tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
990         tcg_gen_qemu_st_i64(tmp, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
991                             s->be_data | MO_Q);
992         tcg_gen_ld_i64(tmp, cpu_env, fp_reg_hi_offset(s, srcidx));
993         tcg_gen_qemu_st_i64(tmp, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
994                             s->be_data | MO_Q);
995         tcg_temp_free_i64(tcg_hiaddr);
996     }
997
998     tcg_temp_free_i64(tmp);
999 }
1000
1001 /*
1002  * Load from memory to FP register
1003  */
1004 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
1005 {
1006     /* This always zero-extends and writes to a full 128 bit wide vector */
1007     TCGv_i64 tmplo = tcg_temp_new_i64();
1008     TCGv_i64 tmphi;
1009
1010     if (size < 4) {
1011         TCGMemOp memop = s->be_data + size;
1012         tmphi = tcg_const_i64(0);
1013         tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
1014     } else {
1015         bool be = s->be_data == MO_BE;
1016         TCGv_i64 tcg_hiaddr;
1017
1018         tmphi = tcg_temp_new_i64();
1019         tcg_hiaddr = tcg_temp_new_i64();
1020
1021         tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
1022         tcg_gen_qemu_ld_i64(tmplo, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
1023                             s->be_data | MO_Q);
1024         tcg_gen_qemu_ld_i64(tmphi, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
1025                             s->be_data | MO_Q);
1026         tcg_temp_free_i64(tcg_hiaddr);
1027     }
1028
1029     tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
1030     tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
1031
1032     tcg_temp_free_i64(tmplo);
1033     tcg_temp_free_i64(tmphi);
1034
1035     clear_vec_high(s, true, destidx);
1036 }
1037
1038 /*
1039  * Vector load/store helpers.
1040  *
1041  * The principal difference between this and a FP load is that we don't
1042  * zero extend as we are filling a partial chunk of the vector register.
1043  * These functions don't support 128 bit loads/stores, which would be
1044  * normal load/store operations.
1045  *
1046  * The _i32 versions are useful when operating on 32 bit quantities
1047  * (eg for floating point single or using Neon helper functions).
1048  */
1049
1050 /* Get value of an element within a vector register */
1051 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
1052                              int element, TCGMemOp memop)
1053 {
1054     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1055     switch (memop) {
1056     case MO_8:
1057         tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
1058         break;
1059     case MO_16:
1060         tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
1061         break;
1062     case MO_32:
1063         tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
1064         break;
1065     case MO_8|MO_SIGN:
1066         tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
1067         break;
1068     case MO_16|MO_SIGN:
1069         tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
1070         break;
1071     case MO_32|MO_SIGN:
1072         tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
1073         break;
1074     case MO_64:
1075     case MO_64|MO_SIGN:
1076         tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
1077         break;
1078     default:
1079         g_assert_not_reached();
1080     }
1081 }
1082
1083 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1084                                  int element, TCGMemOp memop)
1085 {
1086     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1087     switch (memop) {
1088     case MO_8:
1089         tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1090         break;
1091     case MO_16:
1092         tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1093         break;
1094     case MO_8|MO_SIGN:
1095         tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1096         break;
1097     case MO_16|MO_SIGN:
1098         tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1099         break;
1100     case MO_32:
1101     case MO_32|MO_SIGN:
1102         tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1103         break;
1104     default:
1105         g_assert_not_reached();
1106     }
1107 }
1108
1109 /* Set value of an element within a vector register */
1110 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1111                               int element, TCGMemOp memop)
1112 {
1113     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1114     switch (memop) {
1115     case MO_8:
1116         tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1117         break;
1118     case MO_16:
1119         tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1120         break;
1121     case MO_32:
1122         tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1123         break;
1124     case MO_64:
1125         tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1126         break;
1127     default:
1128         g_assert_not_reached();
1129     }
1130 }
1131
1132 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1133                                   int destidx, int element, TCGMemOp memop)
1134 {
1135     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1136     switch (memop) {
1137     case MO_8:
1138         tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1139         break;
1140     case MO_16:
1141         tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1142         break;
1143     case MO_32:
1144         tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1145         break;
1146     default:
1147         g_assert_not_reached();
1148     }
1149 }
1150
1151 /* Store from vector register to memory */
1152 static void do_vec_st(DisasContext *s, int srcidx, int element,
1153                       TCGv_i64 tcg_addr, int size)
1154 {
1155     TCGMemOp memop = s->be_data + size;
1156     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1157
1158     read_vec_element(s, tcg_tmp, srcidx, element, size);
1159     tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
1160
1161     tcg_temp_free_i64(tcg_tmp);
1162 }
1163
1164 /* Load from memory to vector register */
1165 static void do_vec_ld(DisasContext *s, int destidx, int element,
1166                       TCGv_i64 tcg_addr, int size)
1167 {
1168     TCGMemOp memop = s->be_data + size;
1169     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1170
1171     tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
1172     write_vec_element(s, tcg_tmp, destidx, element, size);
1173
1174     tcg_temp_free_i64(tcg_tmp);
1175 }
1176
1177 /* Check that FP/Neon access is enabled. If it is, return
1178  * true. If not, emit code to generate an appropriate exception,
1179  * and return false; the caller should not emit any code for
1180  * the instruction. Note that this check must happen after all
1181  * unallocated-encoding checks (otherwise the syndrome information
1182  * for the resulting exception will be incorrect).
1183  */
1184 static inline bool fp_access_check(DisasContext *s)
1185 {
1186     assert(!s->fp_access_checked);
1187     s->fp_access_checked = true;
1188
1189     if (!s->fp_excp_el) {
1190         return true;
1191     }
1192
1193     gen_exception_insn(s, 4, EXCP_UDEF, syn_fp_access_trap(1, 0xe, false),
1194                        s->fp_excp_el);
1195     return false;
1196 }
1197
1198 /* Check that SVE access is enabled.  If it is, return true.
1199  * If not, emit code to generate an appropriate exception and return false.
1200  */
1201 static inline bool sve_access_check(DisasContext *s)
1202 {
1203     if (s->sve_excp_el) {
1204         gen_exception_insn(s, 4, EXCP_UDEF, syn_sve_access_trap(),
1205                            s->sve_excp_el);
1206         return false;
1207     }
1208     return true;
1209 }
1210
1211 /*
1212  * This utility function is for doing register extension with an
1213  * optional shift. You will likely want to pass a temporary for the
1214  * destination register. See DecodeRegExtend() in the ARM ARM.
1215  */
1216 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1217                               int option, unsigned int shift)
1218 {
1219     int extsize = extract32(option, 0, 2);
1220     bool is_signed = extract32(option, 2, 1);
1221
1222     if (is_signed) {
1223         switch (extsize) {
1224         case 0:
1225             tcg_gen_ext8s_i64(tcg_out, tcg_in);
1226             break;
1227         case 1:
1228             tcg_gen_ext16s_i64(tcg_out, tcg_in);
1229             break;
1230         case 2:
1231             tcg_gen_ext32s_i64(tcg_out, tcg_in);
1232             break;
1233         case 3:
1234             tcg_gen_mov_i64(tcg_out, tcg_in);
1235             break;
1236         }
1237     } else {
1238         switch (extsize) {
1239         case 0:
1240             tcg_gen_ext8u_i64(tcg_out, tcg_in);
1241             break;
1242         case 1:
1243             tcg_gen_ext16u_i64(tcg_out, tcg_in);
1244             break;
1245         case 2:
1246             tcg_gen_ext32u_i64(tcg_out, tcg_in);
1247             break;
1248         case 3:
1249             tcg_gen_mov_i64(tcg_out, tcg_in);
1250             break;
1251         }
1252     }
1253
1254     if (shift) {
1255         tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1256     }
1257 }
1258
1259 static inline void gen_check_sp_alignment(DisasContext *s)
1260 {
1261     /* The AArch64 architecture mandates that (if enabled via PSTATE
1262      * or SCTLR bits) there is a check that SP is 16-aligned on every
1263      * SP-relative load or store (with an exception generated if it is not).
1264      * In line with general QEMU practice regarding misaligned accesses,
1265      * we omit these checks for the sake of guest program performance.
1266      * This function is provided as a hook so we can more easily add these
1267      * checks in future (possibly as a "favour catching guest program bugs
1268      * over speed" user selectable option).
1269      */
1270 }
1271
1272 /*
1273  * This provides a simple table based table lookup decoder. It is
1274  * intended to be used when the relevant bits for decode are too
1275  * awkwardly placed and switch/if based logic would be confusing and
1276  * deeply nested. Since it's a linear search through the table, tables
1277  * should be kept small.
1278  *
1279  * It returns the first handler where insn & mask == pattern, or
1280  * NULL if there is no match.
1281  * The table is terminated by an empty mask (i.e. 0)
1282  */
1283 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1284                                                uint32_t insn)
1285 {
1286     const AArch64DecodeTable *tptr = table;
1287
1288     while (tptr->mask) {
1289         if ((insn & tptr->mask) == tptr->pattern) {
1290             return tptr->disas_fn;
1291         }
1292         tptr++;
1293     }
1294     return NULL;
1295 }
1296
1297 /*
1298  * The instruction disassembly implemented here matches
1299  * the instruction encoding classifications in chapter C4
1300  * of the ARM Architecture Reference Manual (DDI0487B_a);
1301  * classification names and decode diagrams here should generally
1302  * match up with those in the manual.
1303  */
1304
1305 /* Unconditional branch (immediate)
1306  *   31  30       26 25                                  0
1307  * +----+-----------+-------------------------------------+
1308  * | op | 0 0 1 0 1 |                 imm26               |
1309  * +----+-----------+-------------------------------------+
1310  */
1311 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
1312 {
1313     uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
1314
1315     if (insn & (1U << 31)) {
1316         /* BL Branch with link */
1317         tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1318     }
1319
1320     /* B Branch / BL Branch with link */
1321     gen_goto_tb(s, 0, addr);
1322 }
1323
1324 /* Compare and branch (immediate)
1325  *   31  30         25  24  23                  5 4      0
1326  * +----+-------------+----+---------------------+--------+
1327  * | sf | 0 1 1 0 1 0 | op |         imm19       |   Rt   |
1328  * +----+-------------+----+---------------------+--------+
1329  */
1330 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
1331 {
1332     unsigned int sf, op, rt;
1333     uint64_t addr;
1334     TCGLabel *label_match;
1335     TCGv_i64 tcg_cmp;
1336
1337     sf = extract32(insn, 31, 1);
1338     op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
1339     rt = extract32(insn, 0, 5);
1340     addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1341
1342     tcg_cmp = read_cpu_reg(s, rt, sf);
1343     label_match = gen_new_label();
1344
1345     tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1346                         tcg_cmp, 0, label_match);
1347
1348     gen_goto_tb(s, 0, s->pc);
1349     gen_set_label(label_match);
1350     gen_goto_tb(s, 1, addr);
1351 }
1352
1353 /* Test and branch (immediate)
1354  *   31  30         25  24  23   19 18          5 4    0
1355  * +----+-------------+----+-------+-------------+------+
1356  * | b5 | 0 1 1 0 1 1 | op |  b40  |    imm14    |  Rt  |
1357  * +----+-------------+----+-------+-------------+------+
1358  */
1359 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1360 {
1361     unsigned int bit_pos, op, rt;
1362     uint64_t addr;
1363     TCGLabel *label_match;
1364     TCGv_i64 tcg_cmp;
1365
1366     bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1367     op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1368     addr = s->pc + sextract32(insn, 5, 14) * 4 - 4;
1369     rt = extract32(insn, 0, 5);
1370
1371     tcg_cmp = tcg_temp_new_i64();
1372     tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1373     label_match = gen_new_label();
1374     tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1375                         tcg_cmp, 0, label_match);
1376     tcg_temp_free_i64(tcg_cmp);
1377     gen_goto_tb(s, 0, s->pc);
1378     gen_set_label(label_match);
1379     gen_goto_tb(s, 1, addr);
1380 }
1381
1382 /* Conditional branch (immediate)
1383  *  31           25  24  23                  5   4  3    0
1384  * +---------------+----+---------------------+----+------+
1385  * | 0 1 0 1 0 1 0 | o1 |         imm19       | o0 | cond |
1386  * +---------------+----+---------------------+----+------+
1387  */
1388 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1389 {
1390     unsigned int cond;
1391     uint64_t addr;
1392
1393     if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1394         unallocated_encoding(s);
1395         return;
1396     }
1397     addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1398     cond = extract32(insn, 0, 4);
1399
1400     if (cond < 0x0e) {
1401         /* genuinely conditional branches */
1402         TCGLabel *label_match = gen_new_label();
1403         arm_gen_test_cc(cond, label_match);
1404         gen_goto_tb(s, 0, s->pc);
1405         gen_set_label(label_match);
1406         gen_goto_tb(s, 1, addr);
1407     } else {
1408         /* 0xe and 0xf are both "always" conditions */
1409         gen_goto_tb(s, 0, addr);
1410     }
1411 }
1412
1413 /* HINT instruction group, including various allocated HINTs */
1414 static void handle_hint(DisasContext *s, uint32_t insn,
1415                         unsigned int op1, unsigned int op2, unsigned int crm)
1416 {
1417     unsigned int selector = crm << 3 | op2;
1418
1419     if (op1 != 3) {
1420         unallocated_encoding(s);
1421         return;
1422     }
1423
1424     switch (selector) {
1425     case 0: /* NOP */
1426         return;
1427     case 3: /* WFI */
1428         s->base.is_jmp = DISAS_WFI;
1429         return;
1430         /* When running in MTTCG we don't generate jumps to the yield and
1431          * WFE helpers as it won't affect the scheduling of other vCPUs.
1432          * If we wanted to more completely model WFE/SEV so we don't busy
1433          * spin unnecessarily we would need to do something more involved.
1434          */
1435     case 1: /* YIELD */
1436         if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1437             s->base.is_jmp = DISAS_YIELD;
1438         }
1439         return;
1440     case 2: /* WFE */
1441         if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1442             s->base.is_jmp = DISAS_WFE;
1443         }
1444         return;
1445     case 4: /* SEV */
1446     case 5: /* SEVL */
1447         /* we treat all as NOP at least for now */
1448         return;
1449     default:
1450         /* default specified as NOP equivalent */
1451         return;
1452     }
1453 }
1454
1455 static void gen_clrex(DisasContext *s, uint32_t insn)
1456 {
1457     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1458 }
1459
1460 /* CLREX, DSB, DMB, ISB */
1461 static void handle_sync(DisasContext *s, uint32_t insn,
1462                         unsigned int op1, unsigned int op2, unsigned int crm)
1463 {
1464     TCGBar bar;
1465
1466     if (op1 != 3) {
1467         unallocated_encoding(s);
1468         return;
1469     }
1470
1471     switch (op2) {
1472     case 2: /* CLREX */
1473         gen_clrex(s, insn);
1474         return;
1475     case 4: /* DSB */
1476     case 5: /* DMB */
1477         switch (crm & 3) {
1478         case 1: /* MBReqTypes_Reads */
1479             bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1480             break;
1481         case 2: /* MBReqTypes_Writes */
1482             bar = TCG_BAR_SC | TCG_MO_ST_ST;
1483             break;
1484         default: /* MBReqTypes_All */
1485             bar = TCG_BAR_SC | TCG_MO_ALL;
1486             break;
1487         }
1488         tcg_gen_mb(bar);
1489         return;
1490     case 6: /* ISB */
1491         /* We need to break the TB after this insn to execute
1492          * a self-modified code correctly and also to take
1493          * any pending interrupts immediately.
1494          */
1495         gen_goto_tb(s, 0, s->pc);
1496         return;
1497     default:
1498         unallocated_encoding(s);
1499         return;
1500     }
1501 }
1502
1503 /* MSR (immediate) - move immediate to processor state field */
1504 static void handle_msr_i(DisasContext *s, uint32_t insn,
1505                          unsigned int op1, unsigned int op2, unsigned int crm)
1506 {
1507     int op = op1 << 3 | op2;
1508     switch (op) {
1509     case 0x05: /* SPSel */
1510         if (s->current_el == 0) {
1511             unallocated_encoding(s);
1512             return;
1513         }
1514         /* fall through */
1515     case 0x1e: /* DAIFSet */
1516     case 0x1f: /* DAIFClear */
1517     {
1518         TCGv_i32 tcg_imm = tcg_const_i32(crm);
1519         TCGv_i32 tcg_op = tcg_const_i32(op);
1520         gen_a64_set_pc_im(s->pc - 4);
1521         gen_helper_msr_i_pstate(cpu_env, tcg_op, tcg_imm);
1522         tcg_temp_free_i32(tcg_imm);
1523         tcg_temp_free_i32(tcg_op);
1524         /* For DAIFClear, exit the cpu loop to re-evaluate pending IRQs.  */
1525         gen_a64_set_pc_im(s->pc);
1526         s->base.is_jmp = (op == 0x1f ? DISAS_EXIT : DISAS_JUMP);
1527         break;
1528     }
1529     default:
1530         unallocated_encoding(s);
1531         return;
1532     }
1533 }
1534
1535 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1536 {
1537     TCGv_i32 tmp = tcg_temp_new_i32();
1538     TCGv_i32 nzcv = tcg_temp_new_i32();
1539
1540     /* build bit 31, N */
1541     tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
1542     /* build bit 30, Z */
1543     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1544     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1545     /* build bit 29, C */
1546     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1547     /* build bit 28, V */
1548     tcg_gen_shri_i32(tmp, cpu_VF, 31);
1549     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1550     /* generate result */
1551     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1552
1553     tcg_temp_free_i32(nzcv);
1554     tcg_temp_free_i32(tmp);
1555 }
1556
1557 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1558
1559 {
1560     TCGv_i32 nzcv = tcg_temp_new_i32();
1561
1562     /* take NZCV from R[t] */
1563     tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
1564
1565     /* bit 31, N */
1566     tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
1567     /* bit 30, Z */
1568     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1569     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1570     /* bit 29, C */
1571     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1572     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1573     /* bit 28, V */
1574     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1575     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1576     tcg_temp_free_i32(nzcv);
1577 }
1578
1579 /* MRS - move from system register
1580  * MSR (register) - move to system register
1581  * SYS
1582  * SYSL
1583  * These are all essentially the same insn in 'read' and 'write'
1584  * versions, with varying op0 fields.
1585  */
1586 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1587                        unsigned int op0, unsigned int op1, unsigned int op2,
1588                        unsigned int crn, unsigned int crm, unsigned int rt)
1589 {
1590     const ARMCPRegInfo *ri;
1591     TCGv_i64 tcg_rt;
1592
1593     ri = get_arm_cp_reginfo(s->cp_regs,
1594                             ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1595                                                crn, crm, op0, op1, op2));
1596
1597     if (!ri) {
1598         /* Unknown register; this might be a guest error or a QEMU
1599          * unimplemented feature.
1600          */
1601         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1602                       "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1603                       isread ? "read" : "write", op0, op1, crn, crm, op2);
1604         unallocated_encoding(s);
1605         return;
1606     }
1607
1608     /* Check access permissions */
1609     if (!cp_access_ok(s->current_el, ri, isread)) {
1610         unallocated_encoding(s);
1611         return;
1612     }
1613
1614     if (ri->accessfn) {
1615         /* Emit code to perform further access permissions checks at
1616          * runtime; this may result in an exception.
1617          */
1618         TCGv_ptr tmpptr;
1619         TCGv_i32 tcg_syn, tcg_isread;
1620         uint32_t syndrome;
1621
1622         gen_a64_set_pc_im(s->pc - 4);
1623         tmpptr = tcg_const_ptr(ri);
1624         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1625         tcg_syn = tcg_const_i32(syndrome);
1626         tcg_isread = tcg_const_i32(isread);
1627         gen_helper_access_check_cp_reg(cpu_env, tmpptr, tcg_syn, tcg_isread);
1628         tcg_temp_free_ptr(tmpptr);
1629         tcg_temp_free_i32(tcg_syn);
1630         tcg_temp_free_i32(tcg_isread);
1631     }
1632
1633     /* Handle special cases first */
1634     switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
1635     case ARM_CP_NOP:
1636         return;
1637     case ARM_CP_NZCV:
1638         tcg_rt = cpu_reg(s, rt);
1639         if (isread) {
1640             gen_get_nzcv(tcg_rt);
1641         } else {
1642             gen_set_nzcv(tcg_rt);
1643         }
1644         return;
1645     case ARM_CP_CURRENTEL:
1646         /* Reads as current EL value from pstate, which is
1647          * guaranteed to be constant by the tb flags.
1648          */
1649         tcg_rt = cpu_reg(s, rt);
1650         tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
1651         return;
1652     case ARM_CP_DC_ZVA:
1653         /* Writes clear the aligned block of memory which rt points into. */
1654         tcg_rt = cpu_reg(s, rt);
1655         gen_helper_dc_zva(cpu_env, tcg_rt);
1656         return;
1657     default:
1658         break;
1659     }
1660     if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
1661         return;
1662     }
1663     if ((ri->type & ARM_CP_FPU) && !fp_access_check(s)) {
1664         return;
1665     }
1666
1667     if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1668         gen_io_start();
1669     }
1670
1671     tcg_rt = cpu_reg(s, rt);
1672
1673     if (isread) {
1674         if (ri->type & ARM_CP_CONST) {
1675             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1676         } else if (ri->readfn) {
1677             TCGv_ptr tmpptr;
1678             tmpptr = tcg_const_ptr(ri);
1679             gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1680             tcg_temp_free_ptr(tmpptr);
1681         } else {
1682             tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1683         }
1684     } else {
1685         if (ri->type & ARM_CP_CONST) {
1686             /* If not forbidden by access permissions, treat as WI */
1687             return;
1688         } else if (ri->writefn) {
1689             TCGv_ptr tmpptr;
1690             tmpptr = tcg_const_ptr(ri);
1691             gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1692             tcg_temp_free_ptr(tmpptr);
1693         } else {
1694             tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
1695         }
1696     }
1697
1698     if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1699         /* I/O operations must end the TB here (whether read or write) */
1700         gen_io_end();
1701         s->base.is_jmp = DISAS_UPDATE;
1702     } else if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
1703         /* We default to ending the TB on a coprocessor register write,
1704          * but allow this to be suppressed by the register definition
1705          * (usually only necessary to work around guest bugs).
1706          */
1707         s->base.is_jmp = DISAS_UPDATE;
1708     }
1709 }
1710
1711 /* System
1712  *  31                 22 21  20 19 18 16 15   12 11    8 7   5 4    0
1713  * +---------------------+---+-----+-----+-------+-------+-----+------+
1714  * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 |  CRn  |  CRm  | op2 |  Rt  |
1715  * +---------------------+---+-----+-----+-------+-------+-----+------+
1716  */
1717 static void disas_system(DisasContext *s, uint32_t insn)
1718 {
1719     unsigned int l, op0, op1, crn, crm, op2, rt;
1720     l = extract32(insn, 21, 1);
1721     op0 = extract32(insn, 19, 2);
1722     op1 = extract32(insn, 16, 3);
1723     crn = extract32(insn, 12, 4);
1724     crm = extract32(insn, 8, 4);
1725     op2 = extract32(insn, 5, 3);
1726     rt = extract32(insn, 0, 5);
1727
1728     if (op0 == 0) {
1729         if (l || rt != 31) {
1730             unallocated_encoding(s);
1731             return;
1732         }
1733         switch (crn) {
1734         case 2: /* HINT (including allocated hints like NOP, YIELD, etc) */
1735             handle_hint(s, insn, op1, op2, crm);
1736             break;
1737         case 3: /* CLREX, DSB, DMB, ISB */
1738             handle_sync(s, insn, op1, op2, crm);
1739             break;
1740         case 4: /* MSR (immediate) */
1741             handle_msr_i(s, insn, op1, op2, crm);
1742             break;
1743         default:
1744             unallocated_encoding(s);
1745             break;
1746         }
1747         return;
1748     }
1749     handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
1750 }
1751
1752 /* Exception generation
1753  *
1754  *  31             24 23 21 20                     5 4   2 1  0
1755  * +-----------------+-----+------------------------+-----+----+
1756  * | 1 1 0 1 0 1 0 0 | opc |          imm16         | op2 | LL |
1757  * +-----------------------+------------------------+----------+
1758  */
1759 static void disas_exc(DisasContext *s, uint32_t insn)
1760 {
1761     int opc = extract32(insn, 21, 3);
1762     int op2_ll = extract32(insn, 0, 5);
1763     int imm16 = extract32(insn, 5, 16);
1764     TCGv_i32 tmp;
1765
1766     switch (opc) {
1767     case 0:
1768         /* For SVC, HVC and SMC we advance the single-step state
1769          * machine before taking the exception. This is architecturally
1770          * mandated, to ensure that single-stepping a system call
1771          * instruction works properly.
1772          */
1773         switch (op2_ll) {
1774         case 1:                                                     /* SVC */
1775             gen_ss_advance(s);
1776             gen_exception_insn(s, 0, EXCP_SWI, syn_aa64_svc(imm16),
1777                                default_exception_el(s));
1778             break;
1779         case 2:                                                     /* HVC */
1780             if (s->current_el == 0) {
1781                 unallocated_encoding(s);
1782                 break;
1783             }
1784             /* The pre HVC helper handles cases when HVC gets trapped
1785              * as an undefined insn by runtime configuration.
1786              */
1787             gen_a64_set_pc_im(s->pc - 4);
1788             gen_helper_pre_hvc(cpu_env);
1789             gen_ss_advance(s);
1790             gen_exception_insn(s, 0, EXCP_HVC, syn_aa64_hvc(imm16), 2);
1791             break;
1792         case 3:                                                     /* SMC */
1793             if (s->current_el == 0) {
1794                 unallocated_encoding(s);
1795                 break;
1796             }
1797             gen_a64_set_pc_im(s->pc - 4);
1798             tmp = tcg_const_i32(syn_aa64_smc(imm16));
1799             gen_helper_pre_smc(cpu_env, tmp);
1800             tcg_temp_free_i32(tmp);
1801             gen_ss_advance(s);
1802             gen_exception_insn(s, 0, EXCP_SMC, syn_aa64_smc(imm16), 3);
1803             break;
1804         default:
1805             unallocated_encoding(s);
1806             break;
1807         }
1808         break;
1809     case 1:
1810         if (op2_ll != 0) {
1811             unallocated_encoding(s);
1812             break;
1813         }
1814         /* BRK */
1815         gen_exception_insn(s, 4, EXCP_BKPT, syn_aa64_bkpt(imm16),
1816                            default_exception_el(s));
1817         break;
1818     case 2:
1819         if (op2_ll != 0) {
1820             unallocated_encoding(s);
1821             break;
1822         }
1823         /* HLT. This has two purposes.
1824          * Architecturally, it is an external halting debug instruction.
1825          * Since QEMU doesn't implement external debug, we treat this as
1826          * it is required for halting debug disabled: it will UNDEF.
1827          * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
1828          */
1829         if (semihosting_enabled() && imm16 == 0xf000) {
1830 #ifndef CONFIG_USER_ONLY
1831             /* In system mode, don't allow userspace access to semihosting,
1832              * to provide some semblance of security (and for consistency
1833              * with our 32-bit semihosting).
1834              */
1835             if (s->current_el == 0) {
1836                 unsupported_encoding(s, insn);
1837                 break;
1838             }
1839 #endif
1840             gen_exception_internal_insn(s, 0, EXCP_SEMIHOST);
1841         } else {
1842             unsupported_encoding(s, insn);
1843         }
1844         break;
1845     case 5:
1846         if (op2_ll < 1 || op2_ll > 3) {
1847             unallocated_encoding(s);
1848             break;
1849         }
1850         /* DCPS1, DCPS2, DCPS3 */
1851         unsupported_encoding(s, insn);
1852         break;
1853     default:
1854         unallocated_encoding(s);
1855         break;
1856     }
1857 }
1858
1859 /* Unconditional branch (register)
1860  *  31           25 24   21 20   16 15   10 9    5 4     0
1861  * +---------------+-------+-------+-------+------+-------+
1862  * | 1 1 0 1 0 1 1 |  opc  |  op2  |  op3  |  Rn  |  op4  |
1863  * +---------------+-------+-------+-------+------+-------+
1864  */
1865 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
1866 {
1867     unsigned int opc, op2, op3, rn, op4;
1868
1869     opc = extract32(insn, 21, 4);
1870     op2 = extract32(insn, 16, 5);
1871     op3 = extract32(insn, 10, 6);
1872     rn = extract32(insn, 5, 5);
1873     op4 = extract32(insn, 0, 5);
1874
1875     if (op4 != 0x0 || op3 != 0x0 || op2 != 0x1f) {
1876         unallocated_encoding(s);
1877         return;
1878     }
1879
1880     switch (opc) {
1881     case 0: /* BR */
1882     case 1: /* BLR */
1883     case 2: /* RET */
1884         gen_a64_set_pc(s, cpu_reg(s, rn));
1885         /* BLR also needs to load return address */
1886         if (opc == 1) {
1887             tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1888         }
1889         break;
1890     case 4: /* ERET */
1891         if (s->current_el == 0) {
1892             unallocated_encoding(s);
1893             return;
1894         }
1895         gen_helper_exception_return(cpu_env);
1896         /* Must exit loop to check un-masked IRQs */
1897         s->base.is_jmp = DISAS_EXIT;
1898         return;
1899     case 5: /* DRPS */
1900         if (rn != 0x1f) {
1901             unallocated_encoding(s);
1902         } else {
1903             unsupported_encoding(s, insn);
1904         }
1905         return;
1906     default:
1907         unallocated_encoding(s);
1908         return;
1909     }
1910
1911     s->base.is_jmp = DISAS_JUMP;
1912 }
1913
1914 /* Branches, exception generating and system instructions */
1915 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
1916 {
1917     switch (extract32(insn, 25, 7)) {
1918     case 0x0a: case 0x0b:
1919     case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
1920         disas_uncond_b_imm(s, insn);
1921         break;
1922     case 0x1a: case 0x5a: /* Compare & branch (immediate) */
1923         disas_comp_b_imm(s, insn);
1924         break;
1925     case 0x1b: case 0x5b: /* Test & branch (immediate) */
1926         disas_test_b_imm(s, insn);
1927         break;
1928     case 0x2a: /* Conditional branch (immediate) */
1929         disas_cond_b_imm(s, insn);
1930         break;
1931     case 0x6a: /* Exception generation / System */
1932         if (insn & (1 << 24)) {
1933             disas_system(s, insn);
1934         } else {
1935             disas_exc(s, insn);
1936         }
1937         break;
1938     case 0x6b: /* Unconditional branch (register) */
1939         disas_uncond_b_reg(s, insn);
1940         break;
1941     default:
1942         unallocated_encoding(s);
1943         break;
1944     }
1945 }
1946
1947 /*
1948  * Load/Store exclusive instructions are implemented by remembering
1949  * the value/address loaded, and seeing if these are the same
1950  * when the store is performed. This is not actually the architecturally
1951  * mandated semantics, but it works for typical guest code sequences
1952  * and avoids having to monitor regular stores.
1953  *
1954  * The store exclusive uses the atomic cmpxchg primitives to avoid
1955  * races in multi-threaded linux-user and when MTTCG softmmu is
1956  * enabled.
1957  */
1958 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
1959                                TCGv_i64 addr, int size, bool is_pair)
1960 {
1961     int idx = get_mem_index(s);
1962     TCGMemOp memop = s->be_data;
1963
1964     g_assert(size <= 3);
1965     if (is_pair) {
1966         g_assert(size >= 2);
1967         if (size == 2) {
1968             /* The pair must be single-copy atomic for the doubleword.  */
1969             memop |= MO_64 | MO_ALIGN;
1970             tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
1971             if (s->be_data == MO_LE) {
1972                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
1973                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
1974             } else {
1975                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
1976                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
1977             }
1978         } else {
1979             /* The pair must be single-copy atomic for *each* doubleword, not
1980                the entire quadword, however it must be quadword aligned.  */
1981             memop |= MO_64;
1982             tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx,
1983                                 memop | MO_ALIGN_16);
1984
1985             TCGv_i64 addr2 = tcg_temp_new_i64();
1986             tcg_gen_addi_i64(addr2, addr, 8);
1987             tcg_gen_qemu_ld_i64(cpu_exclusive_high, addr2, idx, memop);
1988             tcg_temp_free_i64(addr2);
1989
1990             tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
1991             tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
1992         }
1993     } else {
1994         memop |= size | MO_ALIGN;
1995         tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
1996         tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
1997     }
1998     tcg_gen_mov_i64(cpu_exclusive_addr, addr);
1999 }
2000
2001 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2002                                 TCGv_i64 addr, int size, int is_pair)
2003 {
2004     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2005      *     && (!is_pair || env->exclusive_high == [addr + datasize])) {
2006      *     [addr] = {Rt};
2007      *     if (is_pair) {
2008      *         [addr + datasize] = {Rt2};
2009      *     }
2010      *     {Rd} = 0;
2011      * } else {
2012      *     {Rd} = 1;
2013      * }
2014      * env->exclusive_addr = -1;
2015      */
2016     TCGLabel *fail_label = gen_new_label();
2017     TCGLabel *done_label = gen_new_label();
2018     TCGv_i64 tmp;
2019
2020     tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
2021
2022     tmp = tcg_temp_new_i64();
2023     if (is_pair) {
2024         if (size == 2) {
2025             if (s->be_data == MO_LE) {
2026                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2027             } else {
2028                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2029             }
2030             tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2031                                        cpu_exclusive_val, tmp,
2032                                        get_mem_index(s),
2033                                        MO_64 | MO_ALIGN | s->be_data);
2034             tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2035         } else if (s->be_data == MO_LE) {
2036             if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2037                 gen_helper_paired_cmpxchg64_le_parallel(tmp, cpu_env,
2038                                                         cpu_exclusive_addr,
2039                                                         cpu_reg(s, rt),
2040                                                         cpu_reg(s, rt2));
2041             } else {
2042                 gen_helper_paired_cmpxchg64_le(tmp, cpu_env, cpu_exclusive_addr,
2043                                                cpu_reg(s, rt), cpu_reg(s, rt2));
2044             }
2045         } else {
2046             if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2047                 gen_helper_paired_cmpxchg64_be_parallel(tmp, cpu_env,
2048                                                         cpu_exclusive_addr,
2049                                                         cpu_reg(s, rt),
2050                                                         cpu_reg(s, rt2));
2051             } else {
2052                 gen_helper_paired_cmpxchg64_be(tmp, cpu_env, cpu_exclusive_addr,
2053                                                cpu_reg(s, rt), cpu_reg(s, rt2));
2054             }
2055         }
2056     } else {
2057         tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2058                                    cpu_reg(s, rt), get_mem_index(s),
2059                                    size | MO_ALIGN | s->be_data);
2060         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2061     }
2062     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2063     tcg_temp_free_i64(tmp);
2064     tcg_gen_br(done_label);
2065
2066     gen_set_label(fail_label);
2067     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2068     gen_set_label(done_label);
2069     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2070 }
2071
2072 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
2073  * from the ARMv8 specs for LDR (Shared decode for all encodings).
2074  */
2075 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2076 {
2077     int opc0 = extract32(opc, 0, 1);
2078     int regsize;
2079
2080     if (is_signed) {
2081         regsize = opc0 ? 32 : 64;
2082     } else {
2083         regsize = size == 3 ? 64 : 32;
2084     }
2085     return regsize == 64;
2086 }
2087
2088 /* Load/store exclusive
2089  *
2090  *  31 30 29         24  23  22   21  20  16  15  14   10 9    5 4    0
2091  * +-----+-------------+----+---+----+------+----+-------+------+------+
2092  * | sz  | 0 0 1 0 0 0 | o2 | L | o1 |  Rs  | o0 |  Rt2  |  Rn  | Rt   |
2093  * +-----+-------------+----+---+----+------+----+-------+------+------+
2094  *
2095  *  sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2096  *   L: 0 -> store, 1 -> load
2097  *  o2: 0 -> exclusive, 1 -> not
2098  *  o1: 0 -> single register, 1 -> register pair
2099  *  o0: 1 -> load-acquire/store-release, 0 -> not
2100  */
2101 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2102 {
2103     int rt = extract32(insn, 0, 5);
2104     int rn = extract32(insn, 5, 5);
2105     int rt2 = extract32(insn, 10, 5);
2106     int is_lasr = extract32(insn, 15, 1);
2107     int rs = extract32(insn, 16, 5);
2108     int is_pair = extract32(insn, 21, 1);
2109     int is_store = !extract32(insn, 22, 1);
2110     int is_excl = !extract32(insn, 23, 1);
2111     int size = extract32(insn, 30, 2);
2112     TCGv_i64 tcg_addr;
2113
2114     if ((!is_excl && !is_pair && !is_lasr) ||
2115         (!is_excl && is_pair) ||
2116         (is_pair && size < 2)) {
2117         unallocated_encoding(s);
2118         return;
2119     }
2120
2121     if (rn == 31) {
2122         gen_check_sp_alignment(s);
2123     }
2124     tcg_addr = read_cpu_reg_sp(s, rn, 1);
2125
2126     /* Note that since TCG is single threaded load-acquire/store-release
2127      * semantics require no extra if (is_lasr) { ... } handling.
2128      */
2129
2130     if (is_excl) {
2131         if (!is_store) {
2132             s->is_ldex = true;
2133             gen_load_exclusive(s, rt, rt2, tcg_addr, size, is_pair);
2134             if (is_lasr) {
2135                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2136             }
2137         } else {
2138             if (is_lasr) {
2139                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2140             }
2141             gen_store_exclusive(s, rs, rt, rt2, tcg_addr, size, is_pair);
2142         }
2143     } else {
2144         TCGv_i64 tcg_rt = cpu_reg(s, rt);
2145         bool iss_sf = disas_ldst_compute_iss_sf(size, false, 0);
2146
2147         /* Generate ISS for non-exclusive accesses including LASR.  */
2148         if (is_store) {
2149             if (is_lasr) {
2150                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2151             }
2152             do_gpr_st(s, tcg_rt, tcg_addr, size,
2153                       true, rt, iss_sf, is_lasr);
2154         } else {
2155             do_gpr_ld(s, tcg_rt, tcg_addr, size, false, false,
2156                       true, rt, iss_sf, is_lasr);
2157             if (is_lasr) {
2158                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2159             }
2160         }
2161     }
2162 }
2163
2164 /*
2165  * Load register (literal)
2166  *
2167  *  31 30 29   27  26 25 24 23                5 4     0
2168  * +-----+-------+---+-----+-------------------+-------+
2169  * | opc | 0 1 1 | V | 0 0 |     imm19         |  Rt   |
2170  * +-----+-------+---+-----+-------------------+-------+
2171  *
2172  * V: 1 -> vector (simd/fp)
2173  * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2174  *                   10-> 32 bit signed, 11 -> prefetch
2175  * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2176  */
2177 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2178 {
2179     int rt = extract32(insn, 0, 5);
2180     int64_t imm = sextract32(insn, 5, 19) << 2;
2181     bool is_vector = extract32(insn, 26, 1);
2182     int opc = extract32(insn, 30, 2);
2183     bool is_signed = false;
2184     int size = 2;
2185     TCGv_i64 tcg_rt, tcg_addr;
2186
2187     if (is_vector) {
2188         if (opc == 3) {
2189             unallocated_encoding(s);
2190             return;
2191         }
2192         size = 2 + opc;
2193         if (!fp_access_check(s)) {
2194             return;
2195         }
2196     } else {
2197         if (opc == 3) {
2198             /* PRFM (literal) : prefetch */
2199             return;
2200         }
2201         size = 2 + extract32(opc, 0, 1);
2202         is_signed = extract32(opc, 1, 1);
2203     }
2204
2205     tcg_rt = cpu_reg(s, rt);
2206
2207     tcg_addr = tcg_const_i64((s->pc - 4) + imm);
2208     if (is_vector) {
2209         do_fp_ld(s, rt, tcg_addr, size);
2210     } else {
2211         /* Only unsigned 32bit loads target 32bit registers.  */
2212         bool iss_sf = opc != 0;
2213
2214         do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false,
2215                   true, rt, iss_sf, false);
2216     }
2217     tcg_temp_free_i64(tcg_addr);
2218 }
2219
2220 /*
2221  * LDNP (Load Pair - non-temporal hint)
2222  * LDP (Load Pair - non vector)
2223  * LDPSW (Load Pair Signed Word - non vector)
2224  * STNP (Store Pair - non-temporal hint)
2225  * STP (Store Pair - non vector)
2226  * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2227  * LDP (Load Pair of SIMD&FP)
2228  * STNP (Store Pair of SIMD&FP - non-temporal hint)
2229  * STP (Store Pair of SIMD&FP)
2230  *
2231  *  31 30 29   27  26  25 24   23  22 21   15 14   10 9    5 4    0
2232  * +-----+-------+---+---+-------+---+-----------------------------+
2233  * | opc | 1 0 1 | V | 0 | index | L |  imm7 |  Rt2  |  Rn  | Rt   |
2234  * +-----+-------+---+---+-------+---+-------+-------+------+------+
2235  *
2236  * opc: LDP/STP/LDNP/STNP        00 -> 32 bit, 10 -> 64 bit
2237  *      LDPSW                    01
2238  *      LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2239  *   V: 0 -> GPR, 1 -> Vector
2240  * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2241  *      10 -> signed offset, 11 -> pre-index
2242  *   L: 0 -> Store 1 -> Load
2243  *
2244  * Rt, Rt2 = GPR or SIMD registers to be stored
2245  * Rn = general purpose register containing address
2246  * imm7 = signed offset (multiple of 4 or 8 depending on size)
2247  */
2248 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2249 {
2250     int rt = extract32(insn, 0, 5);
2251     int rn = extract32(insn, 5, 5);
2252     int rt2 = extract32(insn, 10, 5);
2253     uint64_t offset = sextract64(insn, 15, 7);
2254     int index = extract32(insn, 23, 2);
2255     bool is_vector = extract32(insn, 26, 1);
2256     bool is_load = extract32(insn, 22, 1);
2257     int opc = extract32(insn, 30, 2);
2258
2259     bool is_signed = false;
2260     bool postindex = false;
2261     bool wback = false;
2262
2263     TCGv_i64 tcg_addr; /* calculated address */
2264     int size;
2265
2266     if (opc == 3) {
2267         unallocated_encoding(s);
2268         return;
2269     }
2270
2271     if (is_vector) {
2272         size = 2 + opc;
2273     } else {
2274         size = 2 + extract32(opc, 1, 1);
2275         is_signed = extract32(opc, 0, 1);
2276         if (!is_load && is_signed) {
2277             unallocated_encoding(s);
2278             return;
2279         }
2280     }
2281
2282     switch (index) {
2283     case 1: /* post-index */
2284         postindex = true;
2285         wback = true;
2286         break;
2287     case 0:
2288         /* signed offset with "non-temporal" hint. Since we don't emulate
2289          * caches we don't care about hints to the cache system about
2290          * data access patterns, and handle this identically to plain
2291          * signed offset.
2292          */
2293         if (is_signed) {
2294             /* There is no non-temporal-hint version of LDPSW */
2295             unallocated_encoding(s);
2296             return;
2297         }
2298         postindex = false;
2299         break;
2300     case 2: /* signed offset, rn not updated */
2301         postindex = false;
2302         break;
2303     case 3: /* pre-index */
2304         postindex = false;
2305         wback = true;
2306         break;
2307     }
2308
2309     if (is_vector && !fp_access_check(s)) {
2310         return;
2311     }
2312
2313     offset <<= size;
2314
2315     if (rn == 31) {
2316         gen_check_sp_alignment(s);
2317     }
2318
2319     tcg_addr = read_cpu_reg_sp(s, rn, 1);
2320
2321     if (!postindex) {
2322         tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
2323     }
2324
2325     if (is_vector) {
2326         if (is_load) {
2327             do_fp_ld(s, rt, tcg_addr, size);
2328         } else {
2329             do_fp_st(s, rt, tcg_addr, size);
2330         }
2331         tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
2332         if (is_load) {
2333             do_fp_ld(s, rt2, tcg_addr, size);
2334         } else {
2335             do_fp_st(s, rt2, tcg_addr, size);
2336         }
2337     } else {
2338         TCGv_i64 tcg_rt = cpu_reg(s, rt);
2339         TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
2340
2341         if (is_load) {
2342             TCGv_i64 tmp = tcg_temp_new_i64();
2343
2344             /* Do not modify tcg_rt before recognizing any exception
2345              * from the second load.
2346              */
2347             do_gpr_ld(s, tmp, tcg_addr, size, is_signed, false,
2348                       false, 0, false, false);
2349             tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
2350             do_gpr_ld(s, tcg_rt2, tcg_addr, size, is_signed, false,
2351                       false, 0, false, false);
2352
2353             tcg_gen_mov_i64(tcg_rt, tmp);
2354             tcg_temp_free_i64(tmp);
2355         } else {
2356             do_gpr_st(s, tcg_rt, tcg_addr, size,
2357                       false, 0, false, false);
2358             tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
2359             do_gpr_st(s, tcg_rt2, tcg_addr, size,
2360                       false, 0, false, false);
2361         }
2362     }
2363
2364     if (wback) {
2365         if (postindex) {
2366             tcg_gen_addi_i64(tcg_addr, tcg_addr, offset - (1 << size));
2367         } else {
2368             tcg_gen_subi_i64(tcg_addr, tcg_addr, 1 << size);
2369         }
2370         tcg_gen_mov_i64(cpu_reg_sp(s, rn), tcg_addr);
2371     }
2372 }
2373
2374 /*
2375  * Load/store (immediate post-indexed)
2376  * Load/store (immediate pre-indexed)
2377  * Load/store (unscaled immediate)
2378  *
2379  * 31 30 29   27  26 25 24 23 22 21  20    12 11 10 9    5 4    0
2380  * +----+-------+---+-----+-----+---+--------+-----+------+------+
2381  * |size| 1 1 1 | V | 0 0 | opc | 0 |  imm9  | idx |  Rn  |  Rt  |
2382  * +----+-------+---+-----+-----+---+--------+-----+------+------+
2383  *
2384  * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
2385          10 -> unprivileged
2386  * V = 0 -> non-vector
2387  * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
2388  * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2389  */
2390 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
2391                                 int opc,
2392                                 int size,
2393                                 int rt,
2394                                 bool is_vector)
2395 {
2396     int rn = extract32(insn, 5, 5);
2397     int imm9 = sextract32(insn, 12, 9);
2398     int idx = extract32(insn, 10, 2);
2399     bool is_signed = false;
2400     bool is_store = false;
2401     bool is_extended = false;
2402     bool is_unpriv = (idx == 2);
2403     bool iss_valid = !is_vector;
2404     bool post_index;
2405     bool writeback;
2406
2407     TCGv_i64 tcg_addr;
2408
2409     if (is_vector) {
2410         size |= (opc & 2) << 1;
2411         if (size > 4 || is_unpriv) {
2412             unallocated_encoding(s);
2413             return;
2414         }
2415         is_store = ((opc & 1) == 0);
2416         if (!fp_access_check(s)) {
2417             return;
2418         }
2419     } else {
2420         if (size == 3 && opc == 2) {
2421             /* PRFM - prefetch */
2422             if (is_unpriv) {
2423                 unallocated_encoding(s);
2424                 return;
2425             }
2426             return;
2427         }
2428         if (opc == 3 && size > 1) {
2429             unallocated_encoding(s);
2430             return;
2431         }
2432         is_store = (opc == 0);
2433         is_signed = extract32(opc, 1, 1);
2434         is_extended = (size < 3) && extract32(opc, 0, 1);
2435     }
2436
2437     switch (idx) {
2438     case 0:
2439     case 2:
2440         post_index = false;
2441         writeback = false;
2442         break;
2443     case 1:
2444         post_index = true;
2445         writeback = true;
2446         break;
2447     case 3:
2448         post_index = false;
2449         writeback = true;
2450         break;
2451     default:
2452         g_assert_not_reached();
2453     }
2454
2455     if (rn == 31) {
2456         gen_check_sp_alignment(s);
2457     }
2458     tcg_addr = read_cpu_reg_sp(s, rn, 1);
2459
2460     if (!post_index) {
2461         tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
2462     }
2463
2464     if (is_vector) {
2465         if (is_store) {
2466             do_fp_st(s, rt, tcg_addr, size);
2467         } else {
2468             do_fp_ld(s, rt, tcg_addr, size);
2469         }
2470     } else {
2471         TCGv_i64 tcg_rt = cpu_reg(s, rt);
2472         int memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
2473         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2474
2475         if (is_store) {
2476             do_gpr_st_memidx(s, tcg_rt, tcg_addr, size, memidx,
2477                              iss_valid, rt, iss_sf, false);
2478         } else {
2479             do_gpr_ld_memidx(s, tcg_rt, tcg_addr, size,
2480                              is_signed, is_extended, memidx,
2481                              iss_valid, rt, iss_sf, false);
2482         }
2483     }
2484
2485     if (writeback) {
2486         TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2487         if (post_index) {
2488             tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
2489         }
2490         tcg_gen_mov_i64(tcg_rn, tcg_addr);
2491     }
2492 }
2493
2494 /*
2495  * Load/store (register offset)
2496  *
2497  * 31 30 29   27  26 25 24 23 22 21  20  16 15 13 12 11 10 9  5 4  0
2498  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2499  * |size| 1 1 1 | V | 0 0 | opc | 1 |  Rm  | opt | S| 1 0 | Rn | Rt |
2500  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2501  *
2502  * For non-vector:
2503  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2504  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2505  * For vector:
2506  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2507  *   opc<0>: 0 -> store, 1 -> load
2508  * V: 1 -> vector/simd
2509  * opt: extend encoding (see DecodeRegExtend)
2510  * S: if S=1 then scale (essentially index by sizeof(size))
2511  * Rt: register to transfer into/out of
2512  * Rn: address register or SP for base
2513  * Rm: offset register or ZR for offset
2514  */
2515 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
2516                                    int opc,
2517                                    int size,
2518                                    int rt,
2519                                    bool is_vector)
2520 {
2521     int rn = extract32(insn, 5, 5);
2522     int shift = extract32(insn, 12, 1);
2523     int rm = extract32(insn, 16, 5);
2524     int opt = extract32(insn, 13, 3);
2525     bool is_signed = false;
2526     bool is_store = false;
2527     bool is_extended = false;
2528
2529     TCGv_i64 tcg_rm;
2530     TCGv_i64 tcg_addr;
2531
2532     if (extract32(opt, 1, 1) == 0) {
2533         unallocated_encoding(s);
2534         return;
2535     }
2536
2537     if (is_vector) {
2538         size |= (opc & 2) << 1;
2539         if (size > 4) {
2540             unallocated_encoding(s);
2541             return;
2542         }
2543         is_store = !extract32(opc, 0, 1);
2544         if (!fp_access_check(s)) {
2545             return;
2546         }
2547     } else {
2548         if (size == 3 && opc == 2) {
2549             /* PRFM - prefetch */
2550             return;
2551         }
2552         if (opc == 3 && size > 1) {
2553             unallocated_encoding(s);
2554             return;
2555         }
2556         is_store = (opc == 0);
2557         is_signed = extract32(opc, 1, 1);
2558         is_extended = (size < 3) && extract32(opc, 0, 1);
2559     }
2560
2561     if (rn == 31) {
2562         gen_check_sp_alignment(s);
2563     }
2564     tcg_addr = read_cpu_reg_sp(s, rn, 1);
2565
2566     tcg_rm = read_cpu_reg(s, rm, 1);
2567     ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
2568
2569     tcg_gen_add_i64(tcg_addr, tcg_addr, tcg_rm);
2570
2571     if (is_vector) {
2572         if (is_store) {
2573             do_fp_st(s, rt, tcg_addr, size);
2574         } else {
2575             do_fp_ld(s, rt, tcg_addr, size);
2576         }
2577     } else {
2578         TCGv_i64 tcg_rt = cpu_reg(s, rt);
2579         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2580         if (is_store) {
2581             do_gpr_st(s, tcg_rt, tcg_addr, size,
2582                       true, rt, iss_sf, false);
2583         } else {
2584             do_gpr_ld(s, tcg_rt, tcg_addr, size,
2585                       is_signed, is_extended,
2586                       true, rt, iss_sf, false);
2587         }
2588     }
2589 }
2590
2591 /*
2592  * Load/store (unsigned immediate)
2593  *
2594  * 31 30 29   27  26 25 24 23 22 21        10 9     5
2595  * +----+-------+---+-----+-----+------------+-------+------+
2596  * |size| 1 1 1 | V | 0 1 | opc |   imm12    |  Rn   |  Rt  |
2597  * +----+-------+---+-----+-----+------------+-------+------+
2598  *
2599  * For non-vector:
2600  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2601  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2602  * For vector:
2603  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2604  *   opc<0>: 0 -> store, 1 -> load
2605  * Rn: base address register (inc SP)
2606  * Rt: target register
2607  */
2608 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
2609                                         int opc,
2610                                         int size,
2611                                         int rt,
2612                                         bool is_vector)
2613 {
2614     int rn = extract32(insn, 5, 5);
2615     unsigned int imm12 = extract32(insn, 10, 12);
2616     unsigned int offset;
2617
2618     TCGv_i64 tcg_addr;
2619
2620     bool is_store;
2621     bool is_signed = false;
2622     bool is_extended = false;
2623
2624     if (is_vector) {
2625         size |= (opc & 2) << 1;
2626         if (size > 4) {
2627             unallocated_encoding(s);
2628             return;
2629         }
2630         is_store = !extract32(opc, 0, 1);
2631         if (!fp_access_check(s)) {
2632             return;
2633         }
2634     } else {
2635         if (size == 3 && opc == 2) {
2636             /* PRFM - prefetch */
2637             return;
2638         }
2639         if (opc == 3 && size > 1) {
2640             unallocated_encoding(s);
2641             return;
2642         }
2643         is_store = (opc == 0);
2644         is_signed = extract32(opc, 1, 1);
2645         is_extended = (size < 3) && extract32(opc, 0, 1);
2646     }
2647
2648     if (rn == 31) {
2649         gen_check_sp_alignment(s);
2650     }
2651     tcg_addr = read_cpu_reg_sp(s, rn, 1);
2652     offset = imm12 << size;
2653     tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
2654
2655     if (is_vector) {
2656         if (is_store) {
2657             do_fp_st(s, rt, tcg_addr, size);
2658         } else {
2659             do_fp_ld(s, rt, tcg_addr, size);
2660         }
2661     } else {
2662         TCGv_i64 tcg_rt = cpu_reg(s, rt);
2663         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2664         if (is_store) {
2665             do_gpr_st(s, tcg_rt, tcg_addr, size,
2666                       true, rt, iss_sf, false);
2667         } else {
2668             do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended,
2669                       true, rt, iss_sf, false);
2670         }
2671     }
2672 }
2673
2674 /* Load/store register (all forms) */
2675 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
2676 {
2677     int rt = extract32(insn, 0, 5);
2678     int opc = extract32(insn, 22, 2);
2679     bool is_vector = extract32(insn, 26, 1);
2680     int size = extract32(insn, 30, 2);
2681
2682     switch (extract32(insn, 24, 2)) {
2683     case 0:
2684         if (extract32(insn, 21, 1) == 1 && extract32(insn, 10, 2) == 2) {
2685             disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
2686         } else {
2687             /* Load/store register (unscaled immediate)
2688              * Load/store immediate pre/post-indexed
2689              * Load/store register unprivileged
2690              */
2691             disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
2692         }
2693         break;
2694     case 1:
2695         disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
2696         break;
2697     default:
2698         unallocated_encoding(s);
2699         break;
2700     }
2701 }
2702
2703 /* AdvSIMD load/store multiple structures
2704  *
2705  *  31  30  29           23 22  21         16 15    12 11  10 9    5 4    0
2706  * +---+---+---------------+---+-------------+--------+------+------+------+
2707  * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size |  Rn  |  Rt  |
2708  * +---+---+---------------+---+-------------+--------+------+------+------+
2709  *
2710  * AdvSIMD load/store multiple structures (post-indexed)
2711  *
2712  *  31  30  29           23 22  21  20     16 15    12 11  10 9    5 4    0
2713  * +---+---+---------------+---+---+---------+--------+------+------+------+
2714  * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 |   Rm    | opcode | size |  Rn  |  Rt  |
2715  * +---+---+---------------+---+---+---------+--------+------+------+------+
2716  *
2717  * Rt: first (or only) SIMD&FP register to be transferred
2718  * Rn: base address or SP
2719  * Rm (post-index only): post-index register (when !31) or size dependent #imm
2720  */
2721 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
2722 {
2723     int rt = extract32(insn, 0, 5);
2724     int rn = extract32(insn, 5, 5);
2725     int size = extract32(insn, 10, 2);
2726     int opcode = extract32(insn, 12, 4);
2727     bool is_store = !extract32(insn, 22, 1);
2728     bool is_postidx = extract32(insn, 23, 1);
2729     bool is_q = extract32(insn, 30, 1);
2730     TCGv_i64 tcg_addr, tcg_rn;
2731
2732     int ebytes = 1 << size;
2733     int elements = (is_q ? 128 : 64) / (8 << size);
2734     int rpt;    /* num iterations */
2735     int selem;  /* structure elements */
2736     int r;
2737
2738     if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
2739         unallocated_encoding(s);
2740         return;
2741     }
2742
2743     /* From the shared decode logic */
2744     switch (opcode) {
2745     case 0x0:
2746         rpt = 1;
2747         selem = 4;
2748         break;
2749     case 0x2:
2750         rpt = 4;
2751         selem = 1;
2752         break;
2753     case 0x4:
2754         rpt = 1;
2755         selem = 3;
2756         break;
2757     case 0x6:
2758         rpt = 3;
2759         selem = 1;
2760         break;
2761     case 0x7:
2762         rpt = 1;
2763         selem = 1;
2764         break;
2765     case 0x8:
2766         rpt = 1;
2767         selem = 2;
2768         break;
2769     case 0xa:
2770         rpt = 2;
2771         selem = 1;
2772         break;
2773     default:
2774         unallocated_encoding(s);
2775         return;
2776     }
2777
2778     if (size == 3 && !is_q && selem != 1) {
2779         /* reserved */
2780         unallocated_encoding(s);
2781         return;
2782     }
2783
2784     if (!fp_access_check(s)) {
2785         return;
2786     }
2787
2788     if (rn == 31) {
2789         gen_check_sp_alignment(s);
2790     }
2791
2792     tcg_rn = cpu_reg_sp(s, rn);
2793     tcg_addr = tcg_temp_new_i64();
2794     tcg_gen_mov_i64(tcg_addr, tcg_rn);
2795
2796     for (r = 0; r < rpt; r++) {
2797         int e;
2798         for (e = 0; e < elements; e++) {
2799             int tt = (rt + r) % 32;
2800             int xs;
2801             for (xs = 0; xs < selem; xs++) {
2802                 if (is_store) {
2803                     do_vec_st(s, tt, e, tcg_addr, size);
2804                 } else {
2805                     do_vec_ld(s, tt, e, tcg_addr, size);
2806
2807                     /* For non-quad operations, setting a slice of the low
2808                      * 64 bits of the register clears the high 64 bits (in
2809                      * the ARM ARM pseudocode this is implicit in the fact
2810                      * that 'rval' is a 64 bit wide variable).
2811                      * For quad operations, we might still need to zero the
2812                      * high bits of SVE.  We optimize by noticing that we only
2813                      * need to do this the first time we touch a register.
2814                      */
2815                     if (e == 0 && (r == 0 || xs == selem - 1)) {
2816                         clear_vec_high(s, is_q, tt);
2817                     }
2818                 }
2819                 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2820                 tt = (tt + 1) % 32;
2821             }
2822         }
2823     }
2824
2825     if (is_postidx) {
2826         int rm = extract32(insn, 16, 5);
2827         if (rm == 31) {
2828             tcg_gen_mov_i64(tcg_rn, tcg_addr);
2829         } else {
2830             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2831         }
2832     }
2833     tcg_temp_free_i64(tcg_addr);
2834 }
2835
2836 /* AdvSIMD load/store single structure
2837  *
2838  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
2839  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2840  * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size |  Rn  |  Rt  |
2841  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2842  *
2843  * AdvSIMD load/store single structure (post-indexed)
2844  *
2845  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
2846  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2847  * | 0 | Q | 0 0 1 1 0 1 1 | L R |     Rm    | opc | S | size |  Rn  |  Rt  |
2848  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2849  *
2850  * Rt: first (or only) SIMD&FP register to be transferred
2851  * Rn: base address or SP
2852  * Rm (post-index only): post-index register (when !31) or size dependent #imm
2853  * index = encoded in Q:S:size dependent on size
2854  *
2855  * lane_size = encoded in R, opc
2856  * transfer width = encoded in opc, S, size
2857  */
2858 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
2859 {
2860     int rt = extract32(insn, 0, 5);
2861     int rn = extract32(insn, 5, 5);
2862     int size = extract32(insn, 10, 2);
2863     int S = extract32(insn, 12, 1);
2864     int opc = extract32(insn, 13, 3);
2865     int R = extract32(insn, 21, 1);
2866     int is_load = extract32(insn, 22, 1);
2867     int is_postidx = extract32(insn, 23, 1);
2868     int is_q = extract32(insn, 30, 1);
2869
2870     int scale = extract32(opc, 1, 2);
2871     int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
2872     bool replicate = false;
2873     int index = is_q << 3 | S << 2 | size;
2874     int ebytes, xs;
2875     TCGv_i64 tcg_addr, tcg_rn;
2876
2877     switch (scale) {
2878     case 3:
2879         if (!is_load || S) {
2880             unallocated_encoding(s);
2881             return;
2882         }
2883         scale = size;
2884         replicate = true;
2885         break;
2886     case 0:
2887         break;
2888     case 1:
2889         if (extract32(size, 0, 1)) {
2890             unallocated_encoding(s);
2891             return;
2892         }
2893         index >>= 1;
2894         break;
2895     case 2:
2896         if (extract32(size, 1, 1)) {
2897             unallocated_encoding(s);
2898             return;
2899         }
2900         if (!extract32(size, 0, 1)) {
2901             index >>= 2;
2902         } else {
2903             if (S) {
2904                 unallocated_encoding(s);
2905                 return;
2906             }
2907             index >>= 3;
2908             scale = 3;
2909         }
2910         break;
2911     default:
2912         g_assert_not_reached();
2913     }
2914
2915     if (!fp_access_check(s)) {
2916         return;
2917     }
2918
2919     ebytes = 1 << scale;
2920
2921     if (rn == 31) {
2922         gen_check_sp_alignment(s);
2923     }
2924
2925     tcg_rn = cpu_reg_sp(s, rn);
2926     tcg_addr = tcg_temp_new_i64();
2927     tcg_gen_mov_i64(tcg_addr, tcg_rn);
2928
2929     for (xs = 0; xs < selem; xs++) {
2930         if (replicate) {
2931             /* Load and replicate to all elements */
2932             uint64_t mulconst;
2933             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
2934
2935             tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr,
2936                                 get_mem_index(s), s->be_data + scale);
2937             switch (scale) {
2938             case 0:
2939                 mulconst = 0x0101010101010101ULL;
2940                 break;
2941             case 1:
2942                 mulconst = 0x0001000100010001ULL;
2943                 break;
2944             case 2:
2945                 mulconst = 0x0000000100000001ULL;
2946                 break;
2947             case 3:
2948                 mulconst = 0;
2949                 break;
2950             default:
2951                 g_assert_not_reached();
2952             }
2953             if (mulconst) {
2954                 tcg_gen_muli_i64(tcg_tmp, tcg_tmp, mulconst);
2955             }
2956             write_vec_element(s, tcg_tmp, rt, 0, MO_64);
2957             if (is_q) {
2958                 write_vec_element(s, tcg_tmp, rt, 1, MO_64);
2959             }
2960             tcg_temp_free_i64(tcg_tmp);
2961             clear_vec_high(s, is_q, rt);
2962         } else {
2963             /* Load/store one element per register */
2964             if (is_load) {
2965                 do_vec_ld(s, rt, index, tcg_addr, scale);
2966             } else {
2967                 do_vec_st(s, rt, index, tcg_addr, scale);
2968             }
2969         }
2970         tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2971         rt = (rt + 1) % 32;
2972     }
2973
2974     if (is_postidx) {
2975         int rm = extract32(insn, 16, 5);
2976         if (rm == 31) {
2977             tcg_gen_mov_i64(tcg_rn, tcg_addr);
2978         } else {
2979             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2980         }
2981     }
2982     tcg_temp_free_i64(tcg_addr);
2983 }
2984
2985 /* Loads and stores */
2986 static void disas_ldst(DisasContext *s, uint32_t insn)
2987 {
2988     switch (extract32(insn, 24, 6)) {
2989     case 0x08: /* Load/store exclusive */
2990         disas_ldst_excl(s, insn);
2991         break;
2992     case 0x18: case 0x1c: /* Load register (literal) */
2993         disas_ld_lit(s, insn);
2994         break;
2995     case 0x28: case 0x29:
2996     case 0x2c: case 0x2d: /* Load/store pair (all forms) */
2997         disas_ldst_pair(s, insn);
2998         break;
2999     case 0x38: case 0x39:
3000     case 0x3c: case 0x3d: /* Load/store register (all forms) */
3001         disas_ldst_reg(s, insn);
3002         break;
3003     case 0x0c: /* AdvSIMD load/store multiple structures */
3004         disas_ldst_multiple_struct(s, insn);
3005         break;
3006     case 0x0d: /* AdvSIMD load/store single structure */
3007         disas_ldst_single_struct(s, insn);
3008         break;
3009     default:
3010         unallocated_encoding(s);
3011         break;
3012     }
3013 }
3014
3015 /* PC-rel. addressing
3016  *   31  30   29 28       24 23                5 4    0
3017  * +----+-------+-----------+-------------------+------+
3018  * | op | immlo | 1 0 0 0 0 |       immhi       |  Rd  |
3019  * +----+-------+-----------+-------------------+------+
3020  */
3021 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
3022 {
3023     unsigned int page, rd;
3024     uint64_t base;
3025     uint64_t offset;
3026
3027     page = extract32(insn, 31, 1);
3028     /* SignExtend(immhi:immlo) -> offset */
3029     offset = sextract64(insn, 5, 19);
3030     offset = offset << 2 | extract32(insn, 29, 2);
3031     rd = extract32(insn, 0, 5);
3032     base = s->pc - 4;
3033
3034     if (page) {
3035         /* ADRP (page based) */
3036         base &= ~0xfff;
3037         offset <<= 12;
3038     }
3039
3040     tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
3041 }
3042
3043 /*
3044  * Add/subtract (immediate)
3045  *
3046  *  31 30 29 28       24 23 22 21         10 9   5 4   0
3047  * +--+--+--+-----------+-----+-------------+-----+-----+
3048  * |sf|op| S| 1 0 0 0 1 |shift|    imm12    |  Rn | Rd  |
3049  * +--+--+--+-----------+-----+-------------+-----+-----+
3050  *
3051  *    sf: 0 -> 32bit, 1 -> 64bit
3052  *    op: 0 -> add  , 1 -> sub
3053  *     S: 1 -> set flags
3054  * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
3055  */
3056 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
3057 {
3058     int rd = extract32(insn, 0, 5);
3059     int rn = extract32(insn, 5, 5);
3060     uint64_t imm = extract32(insn, 10, 12);
3061     int shift = extract32(insn, 22, 2);
3062     bool setflags = extract32(insn, 29, 1);
3063     bool sub_op = extract32(insn, 30, 1);
3064     bool is_64bit = extract32(insn, 31, 1);
3065
3066     TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3067     TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
3068     TCGv_i64 tcg_result;
3069
3070     switch (shift) {
3071     case 0x0:
3072         break;
3073     case 0x1:
3074         imm <<= 12;
3075         break;
3076     default:
3077         unallocated_encoding(s);
3078         return;
3079     }
3080
3081     tcg_result = tcg_temp_new_i64();
3082     if (!setflags) {
3083         if (sub_op) {
3084             tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
3085         } else {
3086             tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
3087         }
3088     } else {
3089         TCGv_i64 tcg_imm = tcg_const_i64(imm);
3090         if (sub_op) {
3091             gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
3092         } else {
3093             gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
3094         }
3095         tcg_temp_free_i64(tcg_imm);
3096     }
3097
3098     if (is_64bit) {
3099         tcg_gen_mov_i64(tcg_rd, tcg_result);
3100     } else {
3101         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3102     }
3103
3104     tcg_temp_free_i64(tcg_result);
3105 }
3106
3107 /* The input should be a value in the bottom e bits (with higher
3108  * bits zero); returns that value replicated into every element
3109  * of size e in a 64 bit integer.
3110  */
3111 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
3112 {
3113     assert(e != 0);
3114     while (e < 64) {
3115         mask |= mask << e;
3116         e *= 2;
3117     }
3118     return mask;
3119 }
3120
3121 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
3122 static inline uint64_t bitmask64(unsigned int length)
3123 {
3124     assert(length > 0 && length <= 64);
3125     return ~0ULL >> (64 - length);
3126 }
3127
3128 /* Simplified variant of pseudocode DecodeBitMasks() for the case where we
3129  * only require the wmask. Returns false if the imms/immr/immn are a reserved
3130  * value (ie should cause a guest UNDEF exception), and true if they are
3131  * valid, in which case the decoded bit pattern is written to result.
3132  */
3133 static bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
3134                                    unsigned int imms, unsigned int immr)
3135 {
3136     uint64_t mask;
3137     unsigned e, levels, s, r;
3138     int len;
3139
3140     assert(immn < 2 && imms < 64 && immr < 64);
3141
3142     /* The bit patterns we create here are 64 bit patterns which
3143      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
3144      * 64 bits each. Each element contains the same value: a run
3145      * of between 1 and e-1 non-zero bits, rotated within the
3146      * element by between 0 and e-1 bits.
3147      *
3148      * The element size and run length are encoded into immn (1 bit)
3149      * and imms (6 bits) as follows:
3150      * 64 bit elements: immn = 1, imms = <length of run - 1>
3151      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
3152      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
3153      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
3154      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
3155      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
3156      * Notice that immn = 0, imms = 11111x is the only combination
3157      * not covered by one of the above options; this is reserved.
3158      * Further, <length of run - 1> all-ones is a reserved pattern.
3159      *
3160      * In all cases the rotation is by immr % e (and immr is 6 bits).
3161      */
3162
3163     /* First determine the element size */
3164     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
3165     if (len < 1) {
3166         /* This is the immn == 0, imms == 0x11111x case */
3167         return false;
3168     }
3169     e = 1 << len;
3170
3171     levels = e - 1;
3172     s = imms & levels;
3173     r = immr & levels;
3174
3175     if (s == levels) {
3176         /* <length of run - 1> mustn't be all-ones. */
3177         return false;
3178     }
3179
3180     /* Create the value of one element: s+1 set bits rotated
3181      * by r within the element (which is e bits wide)...
3182      */
3183     mask = bitmask64(s + 1);
3184     if (r) {
3185         mask = (mask >> r) | (mask << (e - r));
3186         mask &= bitmask64(e);
3187     }
3188     /* ...then replicate the element over the whole 64 bit value */
3189     mask = bitfield_replicate(mask, e);
3190     *result = mask;
3191     return true;
3192 }
3193
3194 /* Logical (immediate)
3195  *   31  30 29 28         23 22  21  16 15  10 9    5 4    0
3196  * +----+-----+-------------+---+------+------+------+------+
3197  * | sf | opc | 1 0 0 1 0 0 | N | immr | imms |  Rn  |  Rd  |
3198  * +----+-----+-------------+---+------+------+------+------+
3199  */
3200 static void disas_logic_imm(DisasContext *s, uint32_t insn)
3201 {
3202     unsigned int sf, opc, is_n, immr, imms, rn, rd;
3203     TCGv_i64 tcg_rd, tcg_rn;
3204     uint64_t wmask;
3205     bool is_and = false;
3206
3207     sf = extract32(insn, 31, 1);
3208     opc = extract32(insn, 29, 2);
3209     is_n = extract32(insn, 22, 1);
3210     immr = extract32(insn, 16, 6);
3211     imms = extract32(insn, 10, 6);
3212     rn = extract32(insn, 5, 5);
3213     rd = extract32(insn, 0, 5);
3214
3215     if (!sf && is_n) {
3216         unallocated_encoding(s);
3217         return;
3218     }
3219
3220     if (opc == 0x3) { /* ANDS */
3221         tcg_rd = cpu_reg(s, rd);
3222     } else {
3223         tcg_rd = cpu_reg_sp(s, rd);
3224     }
3225     tcg_rn = cpu_reg(s, rn);
3226
3227     if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
3228         /* some immediate field values are reserved */
3229         unallocated_encoding(s);
3230         return;
3231     }
3232
3233     if (!sf) {
3234         wmask &= 0xffffffff;
3235     }
3236
3237     switch (opc) {
3238     case 0x3: /* ANDS */
3239     case 0x0: /* AND */
3240         tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
3241         is_and = true;
3242         break;
3243     case 0x1: /* ORR */
3244         tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
3245         break;
3246     case 0x2: /* EOR */
3247         tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
3248         break;
3249     default:
3250         assert(FALSE); /* must handle all above */
3251         break;
3252     }
3253
3254     if (!sf && !is_and) {
3255         /* zero extend final result; we know we can skip this for AND
3256          * since the immediate had the high 32 bits clear.
3257          */
3258         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3259     }
3260
3261     if (opc == 3) { /* ANDS */
3262         gen_logic_CC(sf, tcg_rd);
3263     }
3264 }
3265
3266 /*
3267  * Move wide (immediate)
3268  *
3269  *  31 30 29 28         23 22 21 20             5 4    0
3270  * +--+-----+-------------+-----+----------------+------+
3271  * |sf| opc | 1 0 0 1 0 1 |  hw |  imm16         |  Rd  |
3272  * +--+-----+-------------+-----+----------------+------+
3273  *
3274  * sf: 0 -> 32 bit, 1 -> 64 bit
3275  * opc: 00 -> N, 10 -> Z, 11 -> K
3276  * hw: shift/16 (0,16, and sf only 32, 48)
3277  */
3278 static void disas_movw_imm(DisasContext *s, uint32_t insn)
3279 {
3280     int rd = extract32(insn, 0, 5);
3281     uint64_t imm = extract32(insn, 5, 16);
3282     int sf = extract32(insn, 31, 1);
3283     int opc = extract32(insn, 29, 2);
3284     int pos = extract32(insn, 21, 2) << 4;
3285     TCGv_i64 tcg_rd = cpu_reg(s, rd);
3286     TCGv_i64 tcg_imm;
3287
3288     if (!sf && (pos >= 32)) {
3289         unallocated_encoding(s);
3290         return;
3291     }
3292
3293     switch (opc) {
3294     case 0: /* MOVN */
3295     case 2: /* MOVZ */
3296         imm <<= pos;
3297         if (opc == 0) {
3298             imm = ~imm;
3299         }
3300         if (!sf) {
3301             imm &= 0xffffffffu;
3302         }
3303         tcg_gen_movi_i64(tcg_rd, imm);
3304         break;
3305     case 3: /* MOVK */
3306         tcg_imm = tcg_const_i64(imm);
3307         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
3308         tcg_temp_free_i64(tcg_imm);
3309         if (!sf) {
3310             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3311         }
3312         break;
3313     default:
3314         unallocated_encoding(s);
3315         break;
3316     }
3317 }
3318
3319 /* Bitfield
3320  *   31  30 29 28         23 22  21  16 15  10 9    5 4    0
3321  * +----+-----+-------------+---+------+------+------+------+
3322  * | sf | opc | 1 0 0 1 1 0 | N | immr | imms |  Rn  |  Rd  |
3323  * +----+-----+-------------+---+------+------+------+------+
3324  */
3325 static void disas_bitfield(DisasContext *s, uint32_t insn)
3326 {
3327     unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
3328     TCGv_i64 tcg_rd, tcg_tmp;
3329
3330     sf = extract32(insn, 31, 1);
3331     opc = extract32(insn, 29, 2);
3332     n = extract32(insn, 22, 1);
3333     ri = extract32(insn, 16, 6);
3334     si = extract32(insn, 10, 6);
3335     rn = extract32(insn, 5, 5);
3336     rd = extract32(insn, 0, 5);
3337     bitsize = sf ? 64 : 32;
3338
3339     if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
3340         unallocated_encoding(s);
3341         return;
3342     }
3343
3344     tcg_rd = cpu_reg(s, rd);
3345
3346     /* Suppress the zero-extend for !sf.  Since RI and SI are constrained
3347        to be smaller than bitsize, we'll never reference data outside the
3348        low 32-bits anyway.  */
3349     tcg_tmp = read_cpu_reg(s, rn, 1);
3350
3351     /* Recognize simple(r) extractions.  */
3352     if (si >= ri) {
3353         /* Wd<s-r:0> = Wn<s:r> */
3354         len = (si - ri) + 1;
3355         if (opc == 0) { /* SBFM: ASR, SBFX, SXTB, SXTH, SXTW */
3356             tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
3357             goto done;
3358         } else if (opc == 2) { /* UBFM: UBFX, LSR, UXTB, UXTH */
3359             tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
3360             return;
3361         }
3362         /* opc == 1, BXFIL fall through to deposit */
3363         tcg_gen_extract_i64(tcg_tmp, tcg_tmp, ri, len);
3364         pos = 0;
3365     } else {
3366         /* Handle the ri > si case with a deposit
3367          * Wd<32+s-r,32-r> = Wn<s:0>
3368          */
3369         len = si + 1;
3370         pos = (bitsize - ri) & (bitsize - 1);
3371     }
3372
3373     if (opc == 0 && len < ri) {
3374         /* SBFM: sign extend the destination field from len to fill
3375            the balance of the word.  Let the deposit below insert all
3376            of those sign bits.  */
3377         tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
3378         len = ri;
3379     }
3380
3381     if (opc == 1) { /* BFM, BXFIL */
3382         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
3383     } else {
3384         /* SBFM or UBFM: We start with zero, and we haven't modified
3385            any bits outside bitsize, therefore the zero-extension
3386            below is unneeded.  */
3387         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
3388         return;
3389     }
3390
3391  done:
3392     if (!sf) { /* zero extend final result */
3393         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3394     }
3395 }
3396
3397 /* Extract
3398  *   31  30  29 28         23 22   21  20  16 15    10 9    5 4    0
3399  * +----+------+-------------+---+----+------+--------+------+------+
3400  * | sf | op21 | 1 0 0 1 1 1 | N | o0 |  Rm  |  imms  |  Rn  |  Rd  |
3401  * +----+------+-------------+---+----+------+--------+------+------+
3402  */
3403 static void disas_extract(DisasContext *s, uint32_t insn)
3404 {
3405     unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
3406
3407     sf = extract32(insn, 31, 1);
3408     n = extract32(insn, 22, 1);
3409     rm = extract32(insn, 16, 5);
3410     imm = extract32(insn, 10, 6);
3411     rn = extract32(insn, 5, 5);
3412     rd = extract32(insn, 0, 5);
3413     op21 = extract32(insn, 29, 2);
3414     op0 = extract32(insn, 21, 1);
3415     bitsize = sf ? 64 : 32;
3416
3417     if (sf != n || op21 || op0 || imm >= bitsize) {
3418         unallocated_encoding(s);
3419     } else {
3420         TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
3421
3422         tcg_rd = cpu_reg(s, rd);
3423
3424         if (unlikely(imm == 0)) {
3425             /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
3426              * so an extract from bit 0 is a special case.
3427              */
3428             if (sf) {
3429                 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
3430             } else {
3431                 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
3432             }
3433         } else if (rm == rn) { /* ROR */
3434             tcg_rm = cpu_reg(s, rm);
3435             if (sf) {
3436                 tcg_gen_rotri_i64(tcg_rd, tcg_rm, imm);
3437             } else {
3438                 TCGv_i32 tmp = tcg_temp_new_i32();
3439                 tcg_gen_extrl_i64_i32(tmp, tcg_rm);
3440                 tcg_gen_rotri_i32(tmp, tmp, imm);
3441                 tcg_gen_extu_i32_i64(tcg_rd, tmp);
3442                 tcg_temp_free_i32(tmp);
3443             }
3444         } else {
3445             tcg_rm = read_cpu_reg(s, rm, sf);
3446             tcg_rn = read_cpu_reg(s, rn, sf);
3447             tcg_gen_shri_i64(tcg_rm, tcg_rm, imm);
3448             tcg_gen_shli_i64(tcg_rn, tcg_rn, bitsize - imm);
3449             tcg_gen_or_i64(tcg_rd, tcg_rm, tcg_rn);
3450             if (!sf) {
3451                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3452             }
3453         }
3454     }
3455 }
3456
3457 /* Data processing - immediate */
3458 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
3459 {
3460     switch (extract32(insn, 23, 6)) {
3461     case 0x20: case 0x21: /* PC-rel. addressing */
3462         disas_pc_rel_adr(s, insn);
3463         break;
3464     case 0x22: case 0x23: /* Add/subtract (immediate) */
3465         disas_add_sub_imm(s, insn);
3466         break;
3467     case 0x24: /* Logical (immediate) */
3468         disas_logic_imm(s, insn);
3469         break;
3470     case 0x25: /* Move wide (immediate) */
3471         disas_movw_imm(s, insn);
3472         break;
3473     case 0x26: /* Bitfield */
3474         disas_bitfield(s, insn);
3475         break;
3476     case 0x27: /* Extract */
3477         disas_extract(s, insn);
3478         break;
3479     default:
3480         unallocated_encoding(s);
3481         break;
3482     }
3483 }
3484
3485 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
3486  * Note that it is the caller's responsibility to ensure that the
3487  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
3488  * mandated semantics for out of range shifts.
3489  */
3490 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
3491                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
3492 {
3493     switch (shift_type) {
3494     case A64_SHIFT_TYPE_LSL:
3495         tcg_gen_shl_i64(dst, src, shift_amount);
3496         break;
3497     case A64_SHIFT_TYPE_LSR:
3498         tcg_gen_shr_i64(dst, src, shift_amount);
3499         break;
3500     case A64_SHIFT_TYPE_ASR:
3501         if (!sf) {
3502             tcg_gen_ext32s_i64(dst, src);
3503         }
3504         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
3505         break;
3506     case A64_SHIFT_TYPE_ROR:
3507         if (sf) {
3508             tcg_gen_rotr_i64(dst, src, shift_amount);
3509         } else {
3510             TCGv_i32 t0, t1;
3511             t0 = tcg_temp_new_i32();
3512             t1 = tcg_temp_new_i32();
3513             tcg_gen_extrl_i64_i32(t0, src);
3514             tcg_gen_extrl_i64_i32(t1, shift_amount);
3515             tcg_gen_rotr_i32(t0, t0, t1);
3516             tcg_gen_extu_i32_i64(dst, t0);
3517             tcg_temp_free_i32(t0);
3518             tcg_temp_free_i32(t1);
3519         }
3520         break;
3521     default:
3522         assert(FALSE); /* all shift types should be handled */
3523         break;
3524     }
3525
3526     if (!sf) { /* zero extend final result */
3527         tcg_gen_ext32u_i64(dst, dst);
3528     }
3529 }
3530
3531 /* Shift a TCGv src by immediate, put result in dst.
3532  * The shift amount must be in range (this should always be true as the
3533  * relevant instructions will UNDEF on bad shift immediates).
3534  */
3535 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
3536                           enum a64_shift_type shift_type, unsigned int shift_i)
3537 {
3538     assert(shift_i < (sf ? 64 : 32));
3539
3540     if (shift_i == 0) {
3541         tcg_gen_mov_i64(dst, src);
3542     } else {
3543         TCGv_i64 shift_const;
3544
3545         shift_const = tcg_const_i64(shift_i);
3546         shift_reg(dst, src, sf, shift_type, shift_const);
3547         tcg_temp_free_i64(shift_const);
3548     }
3549 }
3550
3551 /* Logical (shifted register)
3552  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
3553  * +----+-----+-----------+-------+---+------+--------+------+------+
3554  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
3555  * +----+-----+-----------+-------+---+------+--------+------+------+
3556  */
3557 static void disas_logic_reg(DisasContext *s, uint32_t insn)
3558 {
3559     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
3560     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
3561
3562     sf = extract32(insn, 31, 1);
3563     opc = extract32(insn, 29, 2);
3564     shift_type = extract32(insn, 22, 2);
3565     invert = extract32(insn, 21, 1);
3566     rm = extract32(insn, 16, 5);
3567     shift_amount = extract32(insn, 10, 6);
3568     rn = extract32(insn, 5, 5);
3569     rd = extract32(insn, 0, 5);
3570
3571     if (!sf && (shift_amount & (1 << 5))) {
3572         unallocated_encoding(s);
3573         return;
3574     }
3575
3576     tcg_rd = cpu_reg(s, rd);
3577
3578     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
3579         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
3580          * register-register MOV and MVN, so it is worth special casing.
3581          */
3582         tcg_rm = cpu_reg(s, rm);
3583         if (invert) {
3584             tcg_gen_not_i64(tcg_rd, tcg_rm);
3585             if (!sf) {
3586                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3587             }
3588         } else {
3589             if (sf) {
3590                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
3591             } else {
3592                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
3593             }
3594         }
3595         return;
3596     }
3597
3598     tcg_rm = read_cpu_reg(s, rm, sf);
3599
3600     if (shift_amount) {
3601         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
3602     }
3603
3604     tcg_rn = cpu_reg(s, rn);
3605
3606     switch (opc | (invert << 2)) {
3607     case 0: /* AND */
3608     case 3: /* ANDS */
3609         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
3610         break;
3611     case 1: /* ORR */
3612         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
3613         break;
3614     case 2: /* EOR */
3615         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
3616         break;
3617     case 4: /* BIC */
3618     case 7: /* BICS */
3619         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
3620         break;
3621     case 5: /* ORN */
3622         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
3623         break;
3624     case 6: /* EON */
3625         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
3626         break;
3627     default:
3628         assert(FALSE);
3629         break;
3630     }
3631
3632     if (!sf) {
3633         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3634     }
3635
3636     if (opc == 3) {
3637         gen_logic_CC(sf, tcg_rd);
3638     }
3639 }
3640
3641 /*
3642  * Add/subtract (extended register)
3643  *
3644  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
3645  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3646  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
3647  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3648  *
3649  *  sf: 0 -> 32bit, 1 -> 64bit
3650  *  op: 0 -> add  , 1 -> sub
3651  *   S: 1 -> set flags
3652  * opt: 00
3653  * option: extension type (see DecodeRegExtend)
3654  * imm3: optional shift to Rm
3655  *
3656  * Rd = Rn + LSL(extend(Rm), amount)
3657  */
3658 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
3659 {
3660     int rd = extract32(insn, 0, 5);
3661     int rn = extract32(insn, 5, 5);
3662     int imm3 = extract32(insn, 10, 3);
3663     int option = extract32(insn, 13, 3);
3664     int rm = extract32(insn, 16, 5);
3665     bool setflags = extract32(insn, 29, 1);
3666     bool sub_op = extract32(insn, 30, 1);
3667     bool sf = extract32(insn, 31, 1);
3668
3669     TCGv_i64 tcg_rm, tcg_rn; /* temps */
3670     TCGv_i64 tcg_rd;
3671     TCGv_i64 tcg_result;
3672
3673     if (imm3 > 4) {
3674         unallocated_encoding(s);
3675         return;
3676     }
3677
3678     /* non-flag setting ops may use SP */
3679     if (!setflags) {
3680         tcg_rd = cpu_reg_sp(s, rd);
3681     } else {
3682         tcg_rd = cpu_reg(s, rd);
3683     }
3684     tcg_rn = read_cpu_reg_sp(s, rn, sf);
3685
3686     tcg_rm = read_cpu_reg(s, rm, sf);
3687     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
3688
3689     tcg_result = tcg_temp_new_i64();
3690
3691     if (!setflags) {
3692         if (sub_op) {
3693             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3694         } else {
3695             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3696         }
3697     } else {
3698         if (sub_op) {
3699             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3700         } else {
3701             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3702         }
3703     }
3704
3705     if (sf) {
3706         tcg_gen_mov_i64(tcg_rd, tcg_result);
3707     } else {
3708         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3709     }
3710
3711     tcg_temp_free_i64(tcg_result);
3712 }
3713
3714 /*
3715  * Add/subtract (shifted register)
3716  *
3717  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
3718  * +--+--+--+-----------+-----+--+-------+---------+------+------+
3719  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
3720  * +--+--+--+-----------+-----+--+-------+---------+------+------+
3721  *
3722  *    sf: 0 -> 32bit, 1 -> 64bit
3723  *    op: 0 -> add  , 1 -> sub
3724  *     S: 1 -> set flags
3725  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
3726  *  imm6: Shift amount to apply to Rm before the add/sub
3727  */
3728 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
3729 {
3730     int rd = extract32(insn, 0, 5);
3731     int rn = extract32(insn, 5, 5);
3732     int imm6 = extract32(insn, 10, 6);
3733     int rm = extract32(insn, 16, 5);
3734     int shift_type = extract32(insn, 22, 2);
3735     bool setflags = extract32(insn, 29, 1);
3736     bool sub_op = extract32(insn, 30, 1);
3737     bool sf = extract32(insn, 31, 1);
3738
3739     TCGv_i64 tcg_rd = cpu_reg(s, rd);
3740     TCGv_i64 tcg_rn, tcg_rm;
3741     TCGv_i64 tcg_result;
3742
3743     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
3744         unallocated_encoding(s);
3745         return;
3746     }
3747
3748     tcg_rn = read_cpu_reg(s, rn, sf);
3749     tcg_rm = read_cpu_reg(s, rm, sf);
3750
3751     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
3752
3753     tcg_result = tcg_temp_new_i64();
3754
3755     if (!setflags) {
3756         if (sub_op) {
3757             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3758         } else {
3759             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3760         }
3761     } else {
3762         if (sub_op) {
3763             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3764         } else {
3765             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3766         }
3767     }
3768
3769     if (sf) {
3770         tcg_gen_mov_i64(tcg_rd, tcg_result);
3771     } else {
3772         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3773     }
3774
3775     tcg_temp_free_i64(tcg_result);
3776 }
3777
3778 /* Data-processing (3 source)
3779  *
3780  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
3781  *  +--+------+-----------+------+------+----+------+------+------+
3782  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
3783  *  +--+------+-----------+------+------+----+------+------+------+
3784  */
3785 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
3786 {
3787     int rd = extract32(insn, 0, 5);
3788     int rn = extract32(insn, 5, 5);
3789     int ra = extract32(insn, 10, 5);
3790     int rm = extract32(insn, 16, 5);
3791     int op_id = (extract32(insn, 29, 3) << 4) |
3792         (extract32(insn, 21, 3) << 1) |
3793         extract32(insn, 15, 1);
3794     bool sf = extract32(insn, 31, 1);
3795     bool is_sub = extract32(op_id, 0, 1);
3796     bool is_high = extract32(op_id, 2, 1);
3797     bool is_signed = false;
3798     TCGv_i64 tcg_op1;
3799     TCGv_i64 tcg_op2;
3800     TCGv_i64 tcg_tmp;
3801
3802     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
3803     switch (op_id) {
3804     case 0x42: /* SMADDL */
3805     case 0x43: /* SMSUBL */
3806     case 0x44: /* SMULH */
3807         is_signed = true;
3808         break;
3809     case 0x0: /* MADD (32bit) */
3810     case 0x1: /* MSUB (32bit) */
3811     case 0x40: /* MADD (64bit) */
3812     case 0x41: /* MSUB (64bit) */
3813     case 0x4a: /* UMADDL */
3814     case 0x4b: /* UMSUBL */
3815     case 0x4c: /* UMULH */
3816         break;
3817     default:
3818         unallocated_encoding(s);
3819         return;
3820     }
3821
3822     if (is_high) {
3823         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
3824         TCGv_i64 tcg_rd = cpu_reg(s, rd);
3825         TCGv_i64 tcg_rn = cpu_reg(s, rn);
3826         TCGv_i64 tcg_rm = cpu_reg(s, rm);
3827
3828         if (is_signed) {
3829             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3830         } else {
3831             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3832         }
3833
3834         tcg_temp_free_i64(low_bits);
3835         return;
3836     }
3837
3838     tcg_op1 = tcg_temp_new_i64();
3839     tcg_op2 = tcg_temp_new_i64();
3840     tcg_tmp = tcg_temp_new_i64();
3841
3842     if (op_id < 0x42) {
3843         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
3844         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
3845     } else {
3846         if (is_signed) {
3847             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
3848             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
3849         } else {
3850             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
3851             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
3852         }
3853     }
3854
3855     if (ra == 31 && !is_sub) {
3856         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
3857         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
3858     } else {
3859         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
3860         if (is_sub) {
3861             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3862         } else {
3863             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3864         }
3865     }
3866
3867     if (!sf) {
3868         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
3869     }
3870
3871     tcg_temp_free_i64(tcg_op1);
3872     tcg_temp_free_i64(tcg_op2);
3873     tcg_temp_free_i64(tcg_tmp);
3874 }
3875
3876 /* Add/subtract (with carry)
3877  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15   10  9    5 4   0
3878  * +--+--+--+------------------------+------+---------+------+-----+
3879  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | opcode2 |  Rn  |  Rd |
3880  * +--+--+--+------------------------+------+---------+------+-----+
3881  *                                            [000000]
3882  */
3883
3884 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
3885 {
3886     unsigned int sf, op, setflags, rm, rn, rd;
3887     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
3888
3889     if (extract32(insn, 10, 6) != 0) {
3890         unallocated_encoding(s);
3891         return;
3892     }
3893
3894     sf = extract32(insn, 31, 1);
3895     op = extract32(insn, 30, 1);
3896     setflags = extract32(insn, 29, 1);
3897     rm = extract32(insn, 16, 5);
3898     rn = extract32(insn, 5, 5);
3899     rd = extract32(insn, 0, 5);
3900
3901     tcg_rd = cpu_reg(s, rd);
3902     tcg_rn = cpu_reg(s, rn);
3903
3904     if (op) {
3905         tcg_y = new_tmp_a64(s);
3906         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
3907     } else {
3908         tcg_y = cpu_reg(s, rm);
3909     }
3910
3911     if (setflags) {
3912         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
3913     } else {
3914         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
3915     }
3916 }
3917
3918 /* Conditional compare (immediate / register)
3919  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
3920  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3921  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
3922  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3923  *        [1]                             y                [0]       [0]
3924  */
3925 static void disas_cc(DisasContext *s, uint32_t insn)
3926 {
3927     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
3928     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
3929     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
3930     DisasCompare c;
3931
3932     if (!extract32(insn, 29, 1)) {
3933         unallocated_encoding(s);
3934         return;
3935     }
3936     if (insn & (1 << 10 | 1 << 4)) {
3937         unallocated_encoding(s);
3938         return;
3939     }
3940     sf = extract32(insn, 31, 1);
3941     op = extract32(insn, 30, 1);
3942     is_imm = extract32(insn, 11, 1);
3943     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
3944     cond = extract32(insn, 12, 4);
3945     rn = extract32(insn, 5, 5);
3946     nzcv = extract32(insn, 0, 4);
3947
3948     /* Set T0 = !COND.  */
3949     tcg_t0 = tcg_temp_new_i32();
3950     arm_test_cc(&c, cond);
3951     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
3952     arm_free_cc(&c);
3953
3954     /* Load the arguments for the new comparison.  */
3955     if (is_imm) {
3956         tcg_y = new_tmp_a64(s);
3957         tcg_gen_movi_i64(tcg_y, y);
3958     } else {
3959         tcg_y = cpu_reg(s, y);
3960     }
3961     tcg_rn = cpu_reg(s, rn);
3962
3963     /* Set the flags for the new comparison.  */
3964     tcg_tmp = tcg_temp_new_i64();
3965     if (op) {
3966         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3967     } else {
3968         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3969     }
3970     tcg_temp_free_i64(tcg_tmp);
3971
3972     /* If COND was false, force the flags to #nzcv.  Compute two masks
3973      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
3974      * For tcg hosts that support ANDC, we can make do with just T1.
3975      * In either case, allow the tcg optimizer to delete any unused mask.
3976      */
3977     tcg_t1 = tcg_temp_new_i32();
3978     tcg_t2 = tcg_temp_new_i32();
3979     tcg_gen_neg_i32(tcg_t1, tcg_t0);
3980     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
3981
3982     if (nzcv & 8) { /* N */
3983         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
3984     } else {
3985         if (TCG_TARGET_HAS_andc_i32) {
3986             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
3987         } else {
3988             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
3989         }
3990     }
3991     if (nzcv & 4) { /* Z */
3992         if (TCG_TARGET_HAS_andc_i32) {
3993             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
3994         } else {
3995             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
3996         }
3997     } else {
3998         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
3999     }
4000     if (nzcv & 2) { /* C */
4001         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
4002     } else {
4003         if (TCG_TARGET_HAS_andc_i32) {
4004             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
4005         } else {
4006             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
4007         }
4008     }
4009     if (nzcv & 1) { /* V */
4010         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
4011     } else {
4012         if (TCG_TARGET_HAS_andc_i32) {
4013             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
4014         } else {
4015             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
4016         }
4017     }
4018     tcg_temp_free_i32(tcg_t0);
4019     tcg_temp_free_i32(tcg_t1);
4020     tcg_temp_free_i32(tcg_t2);
4021 }
4022
4023 /* Conditional select
4024  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
4025  * +----+----+---+-----------------+------+------+-----+------+------+
4026  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
4027  * +----+----+---+-----------------+------+------+-----+------+------+
4028  */
4029 static void disas_cond_select(DisasContext *s, uint32_t insn)
4030 {
4031     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
4032     TCGv_i64 tcg_rd, zero;
4033     DisasCompare64 c;
4034
4035     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
4036         /* S == 1 or op2<1> == 1 */
4037         unallocated_encoding(s);
4038         return;
4039     }
4040     sf = extract32(insn, 31, 1);
4041     else_inv = extract32(insn, 30, 1);
4042     rm = extract32(insn, 16, 5);
4043     cond = extract32(insn, 12, 4);
4044     else_inc = extract32(insn, 10, 1);
4045     rn = extract32(insn, 5, 5);
4046     rd = extract32(insn, 0, 5);
4047
4048     tcg_rd = cpu_reg(s, rd);
4049
4050     a64_test_cc(&c, cond);
4051     zero = tcg_const_i64(0);
4052
4053     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
4054         /* CSET & CSETM.  */
4055         tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
4056         if (else_inv) {
4057             tcg_gen_neg_i64(tcg_rd, tcg_rd);
4058         }
4059     } else {
4060         TCGv_i64 t_true = cpu_reg(s, rn);
4061         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
4062         if (else_inv && else_inc) {
4063             tcg_gen_neg_i64(t_false, t_false);
4064         } else if (else_inv) {
4065             tcg_gen_not_i64(t_false, t_false);
4066         } else if (else_inc) {
4067             tcg_gen_addi_i64(t_false, t_false, 1);
4068         }
4069         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
4070     }
4071
4072     tcg_temp_free_i64(zero);
4073     a64_free_cc(&c);
4074
4075     if (!sf) {
4076         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4077     }
4078 }
4079
4080 static void handle_clz(DisasContext *s, unsigned int sf,
4081                        unsigned int rn, unsigned int rd)
4082 {
4083     TCGv_i64 tcg_rd, tcg_rn;
4084     tcg_rd = cpu_reg(s, rd);
4085     tcg_rn = cpu_reg(s, rn);
4086
4087     if (sf) {
4088         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
4089     } else {
4090         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4091         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4092         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
4093         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4094         tcg_temp_free_i32(tcg_tmp32);
4095     }
4096 }
4097
4098 static void handle_cls(DisasContext *s, unsigned int sf,
4099                        unsigned int rn, unsigned int rd)
4100 {
4101     TCGv_i64 tcg_rd, tcg_rn;
4102     tcg_rd = cpu_reg(s, rd);
4103     tcg_rn = cpu_reg(s, rn);
4104
4105     if (sf) {
4106         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
4107     } else {
4108         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4109         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4110         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
4111         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4112         tcg_temp_free_i32(tcg_tmp32);
4113     }
4114 }
4115
4116 static void handle_rbit(DisasContext *s, unsigned int sf,
4117                         unsigned int rn, unsigned int rd)
4118 {
4119     TCGv_i64 tcg_rd, tcg_rn;
4120     tcg_rd = cpu_reg(s, rd);
4121     tcg_rn = cpu_reg(s, rn);
4122
4123     if (sf) {
4124         gen_helper_rbit64(tcg_rd, tcg_rn);
4125     } else {
4126         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4127         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4128         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
4129         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4130         tcg_temp_free_i32(tcg_tmp32);
4131     }
4132 }
4133
4134 /* REV with sf==1, opcode==3 ("REV64") */
4135 static void handle_rev64(DisasContext *s, unsigned int sf,
4136                          unsigned int rn, unsigned int rd)
4137 {
4138     if (!sf) {
4139         unallocated_encoding(s);
4140         return;
4141     }
4142     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
4143 }
4144
4145 /* REV with sf==0, opcode==2
4146  * REV32 (sf==1, opcode==2)
4147  */
4148 static void handle_rev32(DisasContext *s, unsigned int sf,
4149                          unsigned int rn, unsigned int rd)
4150 {
4151     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4152
4153     if (sf) {
4154         TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4155         TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4156
4157         /* bswap32_i64 requires zero high word */
4158         tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
4159         tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
4160         tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
4161         tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
4162         tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
4163
4164         tcg_temp_free_i64(tcg_tmp);
4165     } else {
4166         tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
4167         tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
4168     }
4169 }
4170
4171 /* REV16 (opcode==1) */
4172 static void handle_rev16(DisasContext *s, unsigned int sf,
4173                          unsigned int rn, unsigned int rd)
4174 {
4175     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4176     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4177     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4178     TCGv_i64 mask = tcg_const_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
4179
4180     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
4181     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
4182     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
4183     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
4184     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
4185
4186     tcg_temp_free_i64(mask);
4187     tcg_temp_free_i64(tcg_tmp);
4188 }
4189
4190 /* Data-processing (1 source)
4191  *   31  30  29  28             21 20     16 15    10 9    5 4    0
4192  * +----+---+---+-----------------+---------+--------+------+------+
4193  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
4194  * +----+---+---+-----------------+---------+--------+------+------+
4195  */
4196 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
4197 {
4198     unsigned int sf, opcode, rn, rd;
4199
4200     if (extract32(insn, 29, 1) || extract32(insn, 16, 5)) {
4201         unallocated_encoding(s);
4202         return;
4203     }
4204
4205     sf = extract32(insn, 31, 1);
4206     opcode = extract32(insn, 10, 6);
4207     rn = extract32(insn, 5, 5);
4208     rd = extract32(insn, 0, 5);
4209
4210     switch (opcode) {
4211     case 0: /* RBIT */
4212         handle_rbit(s, sf, rn, rd);
4213         break;
4214     case 1: /* REV16 */
4215         handle_rev16(s, sf, rn, rd);
4216         break;
4217     case 2: /* REV32 */
4218         handle_rev32(s, sf, rn, rd);
4219         break;
4220     case 3: /* REV64 */
4221         handle_rev64(s, sf, rn, rd);
4222         break;
4223     case 4: /* CLZ */
4224         handle_clz(s, sf, rn, rd);
4225         break;
4226     case 5: /* CLS */
4227         handle_cls(s, sf, rn, rd);
4228         break;
4229     }
4230 }
4231
4232 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
4233                        unsigned int rm, unsigned int rn, unsigned int rd)
4234 {
4235     TCGv_i64 tcg_n, tcg_m, tcg_rd;
4236     tcg_rd = cpu_reg(s, rd);
4237
4238     if (!sf && is_signed) {
4239         tcg_n = new_tmp_a64(s);
4240         tcg_m = new_tmp_a64(s);
4241         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
4242         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
4243     } else {
4244         tcg_n = read_cpu_reg(s, rn, sf);
4245         tcg_m = read_cpu_reg(s, rm, sf);
4246     }
4247
4248     if (is_signed) {
4249         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
4250     } else {
4251         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
4252     }
4253
4254     if (!sf) { /* zero extend final result */
4255         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4256     }
4257 }
4258
4259 /* LSLV, LSRV, ASRV, RORV */
4260 static void handle_shift_reg(DisasContext *s,
4261                              enum a64_shift_type shift_type, unsigned int sf,
4262                              unsigned int rm, unsigned int rn, unsigned int rd)
4263 {
4264     TCGv_i64 tcg_shift = tcg_temp_new_i64();
4265     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4266     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4267
4268     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
4269     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
4270     tcg_temp_free_i64(tcg_shift);
4271 }
4272
4273 /* CRC32[BHWX], CRC32C[BHWX] */
4274 static void handle_crc32(DisasContext *s,
4275                          unsigned int sf, unsigned int sz, bool crc32c,
4276                          unsigned int rm, unsigned int rn, unsigned int rd)
4277 {
4278     TCGv_i64 tcg_acc, tcg_val;
4279     TCGv_i32 tcg_bytes;
4280
4281     if (!arm_dc_feature(s, ARM_FEATURE_CRC)
4282         || (sf == 1 && sz != 3)
4283         || (sf == 0 && sz == 3)) {
4284         unallocated_encoding(s);
4285         return;
4286     }
4287
4288     if (sz == 3) {
4289         tcg_val = cpu_reg(s, rm);
4290     } else {
4291         uint64_t mask;
4292         switch (sz) {
4293         case 0:
4294             mask = 0xFF;
4295             break;
4296         case 1:
4297             mask = 0xFFFF;
4298             break;
4299         case 2:
4300             mask = 0xFFFFFFFF;
4301             break;
4302         default:
4303             g_assert_not_reached();
4304         }
4305         tcg_val = new_tmp_a64(s);
4306         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
4307     }
4308
4309     tcg_acc = cpu_reg(s, rn);
4310     tcg_bytes = tcg_const_i32(1 << sz);
4311
4312     if (crc32c) {
4313         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
4314     } else {
4315         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
4316     }
4317
4318     tcg_temp_free_i32(tcg_bytes);
4319 }
4320
4321 /* Data-processing (2 source)
4322  *   31   30  29 28             21 20  16 15    10 9    5 4    0
4323  * +----+---+---+-----------------+------+--------+------+------+
4324  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
4325  * +----+---+---+-----------------+------+--------+------+------+
4326  */
4327 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
4328 {
4329     unsigned int sf, rm, opcode, rn, rd;
4330     sf = extract32(insn, 31, 1);
4331     rm = extract32(insn, 16, 5);
4332     opcode = extract32(insn, 10, 6);
4333     rn = extract32(insn, 5, 5);
4334     rd = extract32(insn, 0, 5);
4335
4336     if (extract32(insn, 29, 1)) {
4337         unallocated_encoding(s);
4338         return;
4339     }
4340
4341     switch (opcode) {
4342     case 2: /* UDIV */
4343         handle_div(s, false, sf, rm, rn, rd);
4344         break;
4345     case 3: /* SDIV */
4346         handle_div(s, true, sf, rm, rn, rd);
4347         break;
4348     case 8: /* LSLV */
4349         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
4350         break;
4351     case 9: /* LSRV */
4352         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
4353         break;
4354     case 10: /* ASRV */
4355         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
4356         break;
4357     case 11: /* RORV */
4358         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
4359         break;
4360     case 16:
4361     case 17:
4362     case 18:
4363     case 19:
4364     case 20:
4365     case 21:
4366     case 22:
4367     case 23: /* CRC32 */
4368     {
4369         int sz = extract32(opcode, 0, 2);
4370         bool crc32c = extract32(opcode, 2, 1);
4371         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
4372         break;
4373     }
4374     default:
4375         unallocated_encoding(s);
4376         break;
4377     }
4378 }
4379
4380 /* Data processing - register */
4381 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
4382 {
4383     switch (extract32(insn, 24, 5)) {
4384     case 0x0a: /* Logical (shifted register) */
4385         disas_logic_reg(s, insn);
4386         break;
4387     case 0x0b: /* Add/subtract */
4388         if (insn & (1 << 21)) { /* (extended register) */
4389             disas_add_sub_ext_reg(s, insn);
4390         } else {
4391             disas_add_sub_reg(s, insn);
4392         }
4393         break;
4394     case 0x1b: /* Data-processing (3 source) */
4395         disas_data_proc_3src(s, insn);
4396         break;
4397     case 0x1a:
4398         switch (extract32(insn, 21, 3)) {
4399         case 0x0: /* Add/subtract (with carry) */
4400             disas_adc_sbc(s, insn);
4401             break;
4402         case 0x2: /* Conditional compare */
4403             disas_cc(s, insn); /* both imm and reg forms */
4404             break;
4405         case 0x4: /* Conditional select */
4406             disas_cond_select(s, insn);
4407             break;
4408         case 0x6: /* Data-processing */
4409             if (insn & (1 << 30)) { /* (1 source) */
4410                 disas_data_proc_1src(s, insn);
4411             } else {            /* (2 source) */
4412                 disas_data_proc_2src(s, insn);
4413             }
4414             break;
4415         default:
4416             unallocated_encoding(s);
4417             break;
4418         }
4419         break;
4420     default:
4421         unallocated_encoding(s);
4422         break;
4423     }
4424 }
4425
4426 static void handle_fp_compare(DisasContext *s, bool is_double,
4427                               unsigned int rn, unsigned int rm,
4428                               bool cmp_with_zero, bool signal_all_nans)
4429 {
4430     TCGv_i64 tcg_flags = tcg_temp_new_i64();
4431     TCGv_ptr fpst = get_fpstatus_ptr(false);
4432
4433     if (is_double) {
4434         TCGv_i64 tcg_vn, tcg_vm;
4435
4436         tcg_vn = read_fp_dreg(s, rn);
4437         if (cmp_with_zero) {
4438             tcg_vm = tcg_const_i64(0);
4439         } else {
4440             tcg_vm = read_fp_dreg(s, rm);
4441         }
4442         if (signal_all_nans) {
4443             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4444         } else {
4445             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4446         }
4447         tcg_temp_free_i64(tcg_vn);
4448         tcg_temp_free_i64(tcg_vm);
4449     } else {
4450         TCGv_i32 tcg_vn, tcg_vm;
4451
4452         tcg_vn = read_fp_sreg(s, rn);
4453         if (cmp_with_zero) {
4454             tcg_vm = tcg_const_i32(0);
4455         } else {
4456             tcg_vm = read_fp_sreg(s, rm);
4457         }
4458         if (signal_all_nans) {
4459             gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4460         } else {
4461             gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4462         }
4463         tcg_temp_free_i32(tcg_vn);
4464         tcg_temp_free_i32(tcg_vm);
4465     }
4466
4467     tcg_temp_free_ptr(fpst);
4468
4469     gen_set_nzcv(tcg_flags);
4470
4471     tcg_temp_free_i64(tcg_flags);
4472 }
4473
4474 /* Floating point compare
4475  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
4476  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
4477  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
4478  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
4479  */
4480 static void disas_fp_compare(DisasContext *s, uint32_t insn)
4481 {
4482     unsigned int mos, type, rm, op, rn, opc, op2r;
4483
4484     mos = extract32(insn, 29, 3);
4485     type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4486     rm = extract32(insn, 16, 5);
4487     op = extract32(insn, 14, 2);
4488     rn = extract32(insn, 5, 5);
4489     opc = extract32(insn, 3, 2);
4490     op2r = extract32(insn, 0, 3);
4491
4492     if (mos || op || op2r || type > 1) {
4493         unallocated_encoding(s);
4494         return;
4495     }
4496
4497     if (!fp_access_check(s)) {
4498         return;
4499     }
4500
4501     handle_fp_compare(s, type, rn, rm, opc & 1, opc & 2);
4502 }
4503
4504 /* Floating point conditional compare
4505  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
4506  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
4507  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
4508  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
4509  */
4510 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
4511 {
4512     unsigned int mos, type, rm, cond, rn, op, nzcv;
4513     TCGv_i64 tcg_flags;
4514     TCGLabel *label_continue = NULL;
4515
4516     mos = extract32(insn, 29, 3);
4517     type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4518     rm = extract32(insn, 16, 5);
4519     cond = extract32(insn, 12, 4);
4520     rn = extract32(insn, 5, 5);
4521     op = extract32(insn, 4, 1);
4522     nzcv = extract32(insn, 0, 4);
4523
4524     if (mos || type > 1) {
4525         unallocated_encoding(s);
4526         return;
4527     }
4528
4529     if (!fp_access_check(s)) {
4530         return;
4531     }
4532
4533     if (cond < 0x0e) { /* not always */
4534         TCGLabel *label_match = gen_new_label();
4535         label_continue = gen_new_label();
4536         arm_gen_test_cc(cond, label_match);
4537         /* nomatch: */
4538         tcg_flags = tcg_const_i64(nzcv << 28);
4539         gen_set_nzcv(tcg_flags);
4540         tcg_temp_free_i64(tcg_flags);
4541         tcg_gen_br(label_continue);
4542         gen_set_label(label_match);
4543     }
4544
4545     handle_fp_compare(s, type, rn, rm, false, op);
4546
4547     if (cond < 0x0e) {
4548         gen_set_label(label_continue);
4549     }
4550 }
4551
4552 /* Floating point conditional select
4553  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
4554  * +---+---+---+-----------+------+---+------+------+-----+------+------+
4555  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
4556  * +---+---+---+-----------+------+---+------+------+-----+------+------+
4557  */
4558 static void disas_fp_csel(DisasContext *s, uint32_t insn)
4559 {
4560     unsigned int mos, type, rm, cond, rn, rd;
4561     TCGv_i64 t_true, t_false, t_zero;
4562     DisasCompare64 c;
4563
4564     mos = extract32(insn, 29, 3);
4565     type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4566     rm = extract32(insn, 16, 5);
4567     cond = extract32(insn, 12, 4);
4568     rn = extract32(insn, 5, 5);
4569     rd = extract32(insn, 0, 5);
4570
4571     if (mos || type > 1) {
4572         unallocated_encoding(s);
4573         return;
4574     }
4575
4576     if (!fp_access_check(s)) {
4577         return;
4578     }
4579
4580     /* Zero extend sreg inputs to 64 bits now.  */
4581     t_true = tcg_temp_new_i64();
4582     t_false = tcg_temp_new_i64();
4583     read_vec_element(s, t_true, rn, 0, type ? MO_64 : MO_32);
4584     read_vec_element(s, t_false, rm, 0, type ? MO_64 : MO_32);
4585
4586     a64_test_cc(&c, cond);
4587     t_zero = tcg_const_i64(0);
4588     tcg_gen_movcond_i64(c.cond, t_true, c.value, t_zero, t_true, t_false);
4589     tcg_temp_free_i64(t_zero);
4590     tcg_temp_free_i64(t_false);
4591     a64_free_cc(&c);
4592
4593     /* Note that sregs write back zeros to the high bits,
4594        and we've already done the zero-extension.  */
4595     write_fp_dreg(s, rd, t_true);
4596     tcg_temp_free_i64(t_true);
4597 }
4598
4599 /* Floating-point data-processing (1 source) - half precision */
4600 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
4601 {
4602     TCGv_ptr fpst = NULL;
4603     TCGv_i32 tcg_op = tcg_temp_new_i32();
4604     TCGv_i32 tcg_res = tcg_temp_new_i32();
4605
4606     read_vec_element_i32(s, tcg_op, rn, 0, MO_16);
4607
4608     switch (opcode) {
4609     case 0x0: /* FMOV */
4610         tcg_gen_mov_i32(tcg_res, tcg_op);
4611         break;
4612     case 0x1: /* FABS */
4613         tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
4614         break;
4615     case 0x2: /* FNEG */
4616         tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
4617         break;
4618     case 0x3: /* FSQRT */
4619         gen_helper_sqrt_f16(tcg_res, tcg_op, cpu_env);
4620         break;
4621     case 0x8: /* FRINTN */
4622     case 0x9: /* FRINTP */
4623     case 0xa: /* FRINTM */
4624     case 0xb: /* FRINTZ */
4625     case 0xc: /* FRINTA */
4626     {
4627         TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4628         fpst = get_fpstatus_ptr(true);
4629
4630         gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
4631         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
4632
4633         gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
4634         tcg_temp_free_i32(tcg_rmode);
4635         break;
4636     }
4637     case 0xe: /* FRINTX */
4638         fpst = get_fpstatus_ptr(true);
4639         gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
4640         break;
4641     case 0xf: /* FRINTI */
4642         fpst = get_fpstatus_ptr(true);
4643         gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
4644         break;
4645     default:
4646         abort();
4647     }
4648
4649     write_fp_sreg(s, rd, tcg_res);
4650
4651     if (fpst) {
4652         tcg_temp_free_ptr(fpst);
4653     }
4654     tcg_temp_free_i32(tcg_op);
4655     tcg_temp_free_i32(tcg_res);
4656 }
4657
4658 /* Floating-point data-processing (1 source) - single precision */
4659 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
4660 {
4661     TCGv_ptr fpst;
4662     TCGv_i32 tcg_op;
4663     TCGv_i32 tcg_res;
4664
4665     fpst = get_fpstatus_ptr(false);
4666     tcg_op = read_fp_sreg(s, rn);
4667     tcg_res = tcg_temp_new_i32();
4668
4669     switch (opcode) {
4670     case 0x0: /* FMOV */
4671         tcg_gen_mov_i32(tcg_res, tcg_op);
4672         break;
4673     case 0x1: /* FABS */
4674         gen_helper_vfp_abss(tcg_res, tcg_op);
4675         break;
4676     case 0x2: /* FNEG */
4677         gen_helper_vfp_negs(tcg_res, tcg_op);
4678         break;
4679     case 0x3: /* FSQRT */
4680         gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
4681         break;
4682     case 0x8: /* FRINTN */
4683     case 0x9: /* FRINTP */
4684     case 0xa: /* FRINTM */
4685     case 0xb: /* FRINTZ */
4686     case 0xc: /* FRINTA */
4687     {
4688         TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4689
4690         gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
4691         gen_helper_rints(tcg_res, tcg_op, fpst);
4692
4693         gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
4694         tcg_temp_free_i32(tcg_rmode);
4695         break;
4696     }
4697     case 0xe: /* FRINTX */
4698         gen_helper_rints_exact(tcg_res, tcg_op, fpst);
4699         break;
4700     case 0xf: /* FRINTI */
4701         gen_helper_rints(tcg_res, tcg_op, fpst);
4702         break;
4703     default:
4704         abort();
4705     }
4706
4707     write_fp_sreg(s, rd, tcg_res);
4708
4709     tcg_temp_free_ptr(fpst);
4710     tcg_temp_free_i32(tcg_op);
4711     tcg_temp_free_i32(tcg_res);
4712 }
4713
4714 /* Floating-point data-processing (1 source) - double precision */
4715 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
4716 {
4717     TCGv_ptr fpst;
4718     TCGv_i64 tcg_op;
4719     TCGv_i64 tcg_res;
4720
4721     switch (opcode) {
4722     case 0x0: /* FMOV */
4723         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
4724         return;
4725     }
4726
4727     fpst = get_fpstatus_ptr(false);
4728     tcg_op = read_fp_dreg(s, rn);
4729     tcg_res = tcg_temp_new_i64();
4730
4731     switch (opcode) {
4732     case 0x1: /* FABS */
4733         gen_helper_vfp_absd(tcg_res, tcg_op);
4734         break;
4735     case 0x2: /* FNEG */
4736         gen_helper_vfp_negd(tcg_res, tcg_op);
4737         break;
4738     case 0x3: /* FSQRT */
4739         gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
4740         break;
4741     case 0x8: /* FRINTN */
4742     case 0x9: /* FRINTP */
4743     case 0xa: /* FRINTM */
4744     case 0xb: /* FRINTZ */
4745     case 0xc: /* FRINTA */
4746     {
4747         TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4748
4749         gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
4750         gen_helper_rintd(tcg_res, tcg_op, fpst);
4751
4752         gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
4753         tcg_temp_free_i32(tcg_rmode);
4754         break;
4755     }
4756     case 0xe: /* FRINTX */
4757         gen_helper_rintd_exact(tcg_res, tcg_op, fpst);
4758         break;
4759     case 0xf: /* FRINTI */
4760         gen_helper_rintd(tcg_res, tcg_op, fpst);
4761         break;
4762     default:
4763         abort();
4764     }
4765
4766     write_fp_dreg(s, rd, tcg_res);
4767
4768     tcg_temp_free_ptr(fpst);
4769     tcg_temp_free_i64(tcg_op);
4770     tcg_temp_free_i64(tcg_res);
4771 }
4772
4773 static void handle_fp_fcvt(DisasContext *s, int opcode,
4774                            int rd, int rn, int dtype, int ntype)
4775 {
4776     switch (ntype) {
4777     case 0x0:
4778     {
4779         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4780         if (dtype == 1) {
4781             /* Single to double */
4782             TCGv_i64 tcg_rd = tcg_temp_new_i64();
4783             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
4784             write_fp_dreg(s, rd, tcg_rd);
4785             tcg_temp_free_i64(tcg_rd);
4786         } else {
4787             /* Single to half */
4788             TCGv_i32 tcg_rd = tcg_temp_new_i32();
4789             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, cpu_env);
4790             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4791             write_fp_sreg(s, rd, tcg_rd);
4792             tcg_temp_free_i32(tcg_rd);
4793         }
4794         tcg_temp_free_i32(tcg_rn);
4795         break;
4796     }
4797     case 0x1:
4798     {
4799         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
4800         TCGv_i32 tcg_rd = tcg_temp_new_i32();
4801         if (dtype == 0) {
4802             /* Double to single */
4803             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
4804         } else {
4805             /* Double to half */
4806             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, cpu_env);
4807             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4808         }
4809         write_fp_sreg(s, rd, tcg_rd);
4810         tcg_temp_free_i32(tcg_rd);
4811         tcg_temp_free_i64(tcg_rn);
4812         break;
4813     }
4814     case 0x3:
4815     {
4816         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4817         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
4818         if (dtype == 0) {
4819             /* Half to single */
4820             TCGv_i32 tcg_rd = tcg_temp_new_i32();
4821             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, cpu_env);
4822             write_fp_sreg(s, rd, tcg_rd);
4823             tcg_temp_free_i32(tcg_rd);
4824         } else {
4825             /* Half to double */
4826             TCGv_i64 tcg_rd = tcg_temp_new_i64();
4827             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, cpu_env);
4828             write_fp_dreg(s, rd, tcg_rd);
4829             tcg_temp_free_i64(tcg_rd);
4830         }
4831         tcg_temp_free_i32(tcg_rn);
4832         break;
4833     }
4834     default:
4835         abort();
4836     }
4837 }
4838
4839 /* Floating point data-processing (1 source)
4840  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
4841  * +---+---+---+-----------+------+---+--------+-----------+------+------+
4842  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
4843  * +---+---+---+-----------+------+---+--------+-----------+------+------+
4844  */
4845 static void disas_fp_1src(DisasContext *s, uint32_t insn)
4846 {
4847     int type = extract32(insn, 22, 2);
4848     int opcode = extract32(insn, 15, 6);
4849     int rn = extract32(insn, 5, 5);
4850     int rd = extract32(insn, 0, 5);
4851
4852     switch (opcode) {
4853     case 0x4: case 0x5: case 0x7:
4854     {
4855         /* FCVT between half, single and double precision */
4856         int dtype = extract32(opcode, 0, 2);
4857         if (type == 2 || dtype == type) {
4858             unallocated_encoding(s);
4859             return;
4860         }
4861         if (!fp_access_check(s)) {
4862             return;
4863         }
4864
4865         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
4866         break;
4867     }
4868     case 0x0 ... 0x3:
4869     case 0x8 ... 0xc:
4870     case 0xe ... 0xf:
4871         /* 32-to-32 and 64-to-64 ops */
4872         switch (type) {
4873         case 0:
4874             if (!fp_access_check(s)) {
4875                 return;
4876             }
4877
4878             handle_fp_1src_single(s, opcode, rd, rn);
4879             break;
4880         case 1:
4881             if (!fp_access_check(s)) {
4882                 return;
4883             }
4884
4885             handle_fp_1src_double(s, opcode, rd, rn);
4886             break;
4887         case 3:
4888             if (!arm_dc_feature(s, ARM_FEATURE_V8_FP16)) {
4889                 unallocated_encoding(s);
4890                 return;
4891             }
4892
4893             if (!fp_access_check(s)) {
4894                 return;
4895             }
4896
4897             handle_fp_1src_half(s, opcode, rd, rn);
4898             break;
4899         default:
4900             unallocated_encoding(s);
4901         }
4902         break;
4903     default:
4904         unallocated_encoding(s);
4905         break;
4906     }
4907 }
4908
4909 /* Floating-point data-processing (2 source) - single precision */
4910 static void handle_fp_2src_single(DisasContext *s, int opcode,
4911                                   int rd, int rn, int rm)
4912 {
4913     TCGv_i32 tcg_op1;
4914     TCGv_i32 tcg_op2;
4915     TCGv_i32 tcg_res;
4916     TCGv_ptr fpst;
4917
4918     tcg_res = tcg_temp_new_i32();
4919     fpst = get_fpstatus_ptr(false);
4920     tcg_op1 = read_fp_sreg(s, rn);
4921     tcg_op2 = read_fp_sreg(s, rm);
4922
4923     switch (opcode) {
4924     case 0x0: /* FMUL */
4925         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4926         break;
4927     case 0x1: /* FDIV */
4928         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
4929         break;
4930     case 0x2: /* FADD */
4931         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
4932         break;
4933     case 0x3: /* FSUB */
4934         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
4935         break;
4936     case 0x4: /* FMAX */
4937         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
4938         break;
4939     case 0x5: /* FMIN */
4940         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
4941         break;
4942     case 0x6: /* FMAXNM */
4943         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
4944         break;
4945     case 0x7: /* FMINNM */
4946         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
4947         break;
4948     case 0x8: /* FNMUL */
4949         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4950         gen_helper_vfp_negs(tcg_res, tcg_res);
4951         break;
4952     }
4953
4954     write_fp_sreg(s, rd, tcg_res);
4955
4956     tcg_temp_free_ptr(fpst);
4957     tcg_temp_free_i32(tcg_op1);
4958     tcg_temp_free_i32(tcg_op2);
4959     tcg_temp_free_i32(tcg_res);
4960 }
4961
4962 /* Floating-point data-processing (2 source) - double precision */
4963 static void handle_fp_2src_double(DisasContext *s, int opcode,
4964                                   int rd, int rn, int rm)
4965 {
4966     TCGv_i64 tcg_op1;
4967     TCGv_i64 tcg_op2;
4968     TCGv_i64 tcg_res;
4969     TCGv_ptr fpst;
4970
4971     tcg_res = tcg_temp_new_i64();
4972     fpst = get_fpstatus_ptr(false);
4973     tcg_op1 = read_fp_dreg(s, rn);
4974     tcg_op2 = read_fp_dreg(s, rm);
4975
4976     switch (opcode) {
4977     case 0x0: /* FMUL */
4978         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4979         break;
4980     case 0x1: /* FDIV */
4981         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
4982         break;
4983     case 0x2: /* FADD */
4984         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
4985         break;
4986     case 0x3: /* FSUB */
4987         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
4988         break;
4989     case 0x4: /* FMAX */
4990         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
4991         break;
4992     case 0x5: /* FMIN */
4993         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
4994         break;
4995     case 0x6: /* FMAXNM */
4996         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4997         break;
4998     case 0x7: /* FMINNM */
4999         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
5000         break;
5001     case 0x8: /* FNMUL */
5002         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
5003         gen_helper_vfp_negd(tcg_res, tcg_res);
5004         break;
5005     }
5006
5007     write_fp_dreg(s, rd, tcg_res);
5008
5009     tcg_temp_free_ptr(fpst);
5010     tcg_temp_free_i64(tcg_op1);
5011     tcg_temp_free_i64(tcg_op2);
5012     tcg_temp_free_i64(tcg_res);
5013 }
5014
5015 /* Floating point data-processing (2 source)
5016  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
5017  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
5018  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
5019  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
5020  */
5021 static void disas_fp_2src(DisasContext *s, uint32_t insn)
5022 {
5023     int type = extract32(insn, 22, 2);
5024     int rd = extract32(insn, 0, 5);
5025     int rn = extract32(insn, 5, 5);
5026     int rm = extract32(insn, 16, 5);
5027     int opcode = extract32(insn, 12, 4);
5028
5029     if (opcode > 8) {
5030         unallocated_encoding(s);
5031         return;
5032     }
5033
5034     switch (type) {
5035     case 0:
5036         if (!fp_access_check(s)) {
5037             return;
5038         }
5039         handle_fp_2src_single(s, opcode, rd, rn, rm);
5040         break;
5041     case 1:
5042         if (!fp_access_check(s)) {
5043             return;
5044         }
5045         handle_fp_2src_double(s, opcode, rd, rn, rm);
5046         break;
5047     default:
5048         unallocated_encoding(s);
5049     }
5050 }
5051
5052 /* Floating-point data-processing (3 source) - single precision */
5053 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
5054                                   int rd, int rn, int rm, int ra)
5055 {
5056     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
5057     TCGv_i32 tcg_res = tcg_temp_new_i32();
5058     TCGv_ptr fpst = get_fpstatus_ptr(false);
5059
5060     tcg_op1 = read_fp_sreg(s, rn);
5061     tcg_op2 = read_fp_sreg(s, rm);
5062     tcg_op3 = read_fp_sreg(s, ra);
5063
5064     /* These are fused multiply-add, and must be done as one
5065      * floating point operation with no rounding between the
5066      * multiplication and addition steps.
5067      * NB that doing the negations here as separate steps is
5068      * correct : an input NaN should come out with its sign bit
5069      * flipped if it is a negated-input.
5070      */
5071     if (o1 == true) {
5072         gen_helper_vfp_negs(tcg_op3, tcg_op3);
5073     }
5074
5075     if (o0 != o1) {
5076         gen_helper_vfp_negs(tcg_op1, tcg_op1);
5077     }
5078
5079     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
5080
5081     write_fp_sreg(s, rd, tcg_res);
5082
5083     tcg_temp_free_ptr(fpst);
5084     tcg_temp_free_i32(tcg_op1);
5085     tcg_temp_free_i32(tcg_op2);
5086     tcg_temp_free_i32(tcg_op3);
5087     tcg_temp_free_i32(tcg_res);
5088 }
5089
5090 /* Floating-point data-processing (3 source) - double precision */
5091 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
5092                                   int rd, int rn, int rm, int ra)
5093 {
5094     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
5095     TCGv_i64 tcg_res = tcg_temp_new_i64();
5096     TCGv_ptr fpst = get_fpstatus_ptr(false);
5097
5098     tcg_op1 = read_fp_dreg(s, rn);
5099     tcg_op2 = read_fp_dreg(s, rm);
5100     tcg_op3 = read_fp_dreg(s, ra);
5101
5102     /* These are fused multiply-add, and must be done as one
5103      * floating point operation with no rounding between the
5104      * multiplication and addition steps.
5105      * NB that doing the negations here as separate steps is
5106      * correct : an input NaN should come out with its sign bit
5107      * flipped if it is a negated-input.
5108      */
5109     if (o1 == true) {
5110         gen_helper_vfp_negd(tcg_op3, tcg_op3);
5111     }
5112
5113     if (o0 != o1) {
5114         gen_helper_vfp_negd(tcg_op1, tcg_op1);
5115     }
5116
5117     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
5118
5119     write_fp_dreg(s, rd, tcg_res);
5120
5121     tcg_temp_free_ptr(fpst);
5122     tcg_temp_free_i64(tcg_op1);
5123     tcg_temp_free_i64(tcg_op2);
5124     tcg_temp_free_i64(tcg_op3);
5125     tcg_temp_free_i64(tcg_res);
5126 }
5127
5128 /* Floating point data-processing (3 source)
5129  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
5130  * +---+---+---+-----------+------+----+------+----+------+------+------+
5131  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
5132  * +---+---+---+-----------+------+----+------+----+------+------+------+
5133  */
5134 static void disas_fp_3src(DisasContext *s, uint32_t insn)
5135 {
5136     int type = extract32(insn, 22, 2);
5137     int rd = extract32(insn, 0, 5);
5138     int rn = extract32(insn, 5, 5);
5139     int ra = extract32(insn, 10, 5);
5140     int rm = extract32(insn, 16, 5);
5141     bool o0 = extract32(insn, 15, 1);
5142     bool o1 = extract32(insn, 21, 1);
5143
5144     switch (type) {
5145     case 0:
5146         if (!fp_access_check(s)) {
5147             return;
5148         }
5149         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
5150         break;
5151     case 1:
5152         if (!fp_access_check(s)) {
5153             return;
5154         }
5155         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
5156         break;
5157     default:
5158         unallocated_encoding(s);
5159     }
5160 }
5161
5162 /* The imm8 encodes the sign bit, enough bits to represent an exponent in
5163  * the range 01....1xx to 10....0xx, and the most significant 4 bits of
5164  * the mantissa; see VFPExpandImm() in the v8 ARM ARM.
5165  */
5166 static uint64_t vfp_expand_imm(int size, uint8_t imm8)
5167 {
5168     uint64_t imm;
5169
5170     switch (size) {
5171     case MO_64:
5172         imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
5173             (extract32(imm8, 6, 1) ? 0x3fc0 : 0x4000) |
5174             extract32(imm8, 0, 6);
5175         imm <<= 48;
5176         break;
5177     case MO_32:
5178         imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
5179             (extract32(imm8, 6, 1) ? 0x3e00 : 0x4000) |
5180             (extract32(imm8, 0, 6) << 3);
5181         imm <<= 16;
5182         break;
5183     case MO_16:
5184         imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
5185             (extract32(imm8, 6, 1) ? 0x3000 : 0x4000) |
5186             (extract32(imm8, 0, 6) << 6);
5187         break;
5188     default:
5189         g_assert_not_reached();
5190     }
5191     return imm;
5192 }
5193
5194 /* Floating point immediate
5195  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
5196  * +---+---+---+-----------+------+---+------------+-------+------+------+
5197  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
5198  * +---+---+---+-----------+------+---+------------+-------+------+------+
5199  */
5200 static void disas_fp_imm(DisasContext *s, uint32_t insn)
5201 {
5202     int rd = extract32(insn, 0, 5);
5203     int imm8 = extract32(insn, 13, 8);
5204     int is_double = extract32(insn, 22, 2);
5205     uint64_t imm;
5206     TCGv_i64 tcg_res;
5207
5208     if (is_double > 1) {
5209         unallocated_encoding(s);
5210         return;
5211     }
5212
5213     if (!fp_access_check(s)) {
5214         return;
5215     }
5216
5217     imm = vfp_expand_imm(MO_32 + is_double, imm8);
5218
5219     tcg_res = tcg_const_i64(imm);
5220     write_fp_dreg(s, rd, tcg_res);
5221     tcg_temp_free_i64(tcg_res);
5222 }
5223
5224 /* Handle floating point <=> fixed point conversions. Note that we can
5225  * also deal with fp <=> integer conversions as a special case (scale == 64)
5226  * OPTME: consider handling that special case specially or at least skipping
5227  * the call to scalbn in the helpers for zero shifts.
5228  */
5229 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
5230                            bool itof, int rmode, int scale, int sf, int type)
5231 {
5232     bool is_signed = !(opcode & 1);
5233     bool is_double = type;
5234     TCGv_ptr tcg_fpstatus;
5235     TCGv_i32 tcg_shift;
5236
5237     tcg_fpstatus = get_fpstatus_ptr(false);
5238
5239     tcg_shift = tcg_const_i32(64 - scale);
5240
5241     if (itof) {
5242         TCGv_i64 tcg_int = cpu_reg(s, rn);
5243         if (!sf) {
5244             TCGv_i64 tcg_extend = new_tmp_a64(s);
5245
5246             if (is_signed) {
5247                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
5248             } else {
5249                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
5250             }
5251
5252             tcg_int = tcg_extend;
5253         }
5254
5255         if (is_double) {
5256             TCGv_i64 tcg_double = tcg_temp_new_i64();
5257             if (is_signed) {
5258                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
5259                                      tcg_shift, tcg_fpstatus);
5260             } else {
5261                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
5262                                      tcg_shift, tcg_fpstatus);
5263             }
5264             write_fp_dreg(s, rd, tcg_double);
5265             tcg_temp_free_i64(tcg_double);
5266         } else {
5267             TCGv_i32 tcg_single = tcg_temp_new_i32();
5268             if (is_signed) {
5269                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
5270                                      tcg_shift, tcg_fpstatus);
5271             } else {
5272                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
5273                                      tcg_shift, tcg_fpstatus);
5274             }
5275             write_fp_sreg(s, rd, tcg_single);
5276             tcg_temp_free_i32(tcg_single);
5277         }
5278     } else {
5279         TCGv_i64 tcg_int = cpu_reg(s, rd);
5280         TCGv_i32 tcg_rmode;
5281
5282         if (extract32(opcode, 2, 1)) {
5283             /* There are too many rounding modes to all fit into rmode,
5284              * so FCVTA[US] is a special case.
5285              */
5286             rmode = FPROUNDING_TIEAWAY;
5287         }
5288
5289         tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
5290
5291         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
5292
5293         if (is_double) {
5294             TCGv_i64 tcg_double = read_fp_dreg(s, rn);
5295             if (is_signed) {
5296                 if (!sf) {
5297                     gen_helper_vfp_tosld(tcg_int, tcg_double,
5298                                          tcg_shift, tcg_fpstatus);
5299                 } else {
5300                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
5301                                          tcg_shift, tcg_fpstatus);
5302                 }
5303             } else {
5304                 if (!sf) {
5305                     gen_helper_vfp_tould(tcg_int, tcg_double,
5306                                          tcg_shift, tcg_fpstatus);
5307                 } else {
5308                     gen_helper_vfp_touqd(tcg_int, tcg_double,
5309                                          tcg_shift, tcg_fpstatus);
5310                 }
5311             }
5312             tcg_temp_free_i64(tcg_double);
5313         } else {
5314             TCGv_i32 tcg_single = read_fp_sreg(s, rn);
5315             if (sf) {
5316                 if (is_signed) {
5317                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
5318                                          tcg_shift, tcg_fpstatus);
5319                 } else {
5320                     gen_helper_vfp_touqs(tcg_int, tcg_single,
5321                                          tcg_shift, tcg_fpstatus);
5322                 }
5323             } else {
5324                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
5325                 if (is_signed) {
5326                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
5327                                          tcg_shift, tcg_fpstatus);
5328                 } else {
5329                     gen_helper_vfp_touls(tcg_dest, tcg_single,
5330                                          tcg_shift, tcg_fpstatus);
5331                 }
5332                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
5333                 tcg_temp_free_i32(tcg_dest);
5334             }
5335             tcg_temp_free_i32(tcg_single);
5336         }
5337
5338         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
5339         tcg_temp_free_i32(tcg_rmode);
5340
5341         if (!sf) {
5342             tcg_gen_ext32u_i64(tcg_int, tcg_int);
5343         }
5344     }
5345
5346     tcg_temp_free_ptr(tcg_fpstatus);
5347     tcg_temp_free_i32(tcg_shift);
5348 }
5349
5350 /* Floating point <-> fixed point conversions
5351  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
5352  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
5353  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
5354  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
5355  */
5356 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
5357 {
5358     int rd = extract32(insn, 0, 5);
5359     int rn = extract32(insn, 5, 5);
5360     int scale = extract32(insn, 10, 6);
5361     int opcode = extract32(insn, 16, 3);
5362     int rmode = extract32(insn, 19, 2);
5363     int type = extract32(insn, 22, 2);
5364     bool sbit = extract32(insn, 29, 1);
5365     bool sf = extract32(insn, 31, 1);
5366     bool itof;
5367
5368     if (sbit || (type > 1)
5369         || (!sf && scale < 32)) {
5370         unallocated_encoding(s);
5371         return;
5372     }
5373
5374     switch ((rmode << 3) | opcode) {
5375     case 0x2: /* SCVTF */
5376     case 0x3: /* UCVTF */
5377         itof = true;
5378         break;
5379     case 0x18: /* FCVTZS */
5380     case 0x19: /* FCVTZU */
5381         itof = false;
5382         break;
5383     default:
5384         unallocated_encoding(s);
5385         return;
5386     }
5387
5388     if (!fp_access_check(s)) {
5389         return;
5390     }
5391
5392     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
5393 }
5394
5395 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
5396 {
5397     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
5398      * without conversion.
5399      */
5400
5401     if (itof) {
5402         TCGv_i64 tcg_rn = cpu_reg(s, rn);
5403
5404         switch (type) {
5405         case 0:
5406         {
5407             /* 32 bit */
5408             TCGv_i64 tmp = tcg_temp_new_i64();
5409             tcg_gen_ext32u_i64(tmp, tcg_rn);
5410             tcg_gen_st_i64(tmp, cpu_env, fp_reg_offset(s, rd, MO_64));
5411             tcg_gen_movi_i64(tmp, 0);
5412             tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(s, rd));
5413             tcg_temp_free_i64(tmp);
5414             break;
5415         }
5416         case 1:
5417         {
5418             /* 64 bit */
5419             TCGv_i64 tmp = tcg_const_i64(0);
5420             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_offset(s, rd, MO_64));
5421             tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(s, rd));
5422             tcg_temp_free_i64(tmp);
5423             break;
5424         }
5425         case 2:
5426             /* 64 bit to top half. */
5427             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
5428             break;
5429         }
5430     } else {
5431         TCGv_i64 tcg_rd = cpu_reg(s, rd);
5432
5433         switch (type) {
5434         case 0:
5435             /* 32 bit */
5436             tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
5437             break;
5438         case 1:
5439             /* 64 bit */
5440             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
5441             break;
5442         case 2:
5443             /* 64 bits from top half */
5444             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
5445             break;
5446         }
5447     }
5448 }
5449
5450 /* Floating point <-> integer conversions
5451  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
5452  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
5453  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
5454  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
5455  */
5456 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
5457 {
5458     int rd = extract32(insn, 0, 5);
5459     int rn = extract32(insn, 5, 5);
5460     int opcode = extract32(insn, 16, 3);
5461     int rmode = extract32(insn, 19, 2);
5462     int type = extract32(insn, 22, 2);
5463     bool sbit = extract32(insn, 29, 1);
5464     bool sf = extract32(insn, 31, 1);
5465
5466     if (sbit) {
5467         unallocated_encoding(s);
5468         return;
5469     }
5470
5471     if (opcode > 5) {
5472         /* FMOV */
5473         bool itof = opcode & 1;
5474
5475         if (rmode >= 2) {
5476             unallocated_encoding(s);
5477             return;
5478         }
5479
5480         switch (sf << 3 | type << 1 | rmode) {
5481         case 0x0: /* 32 bit */
5482         case 0xa: /* 64 bit */
5483         case 0xd: /* 64 bit to top half of quad */
5484             break;
5485         default:
5486             /* all other sf/type/rmode combinations are invalid */
5487             unallocated_encoding(s);
5488             break;
5489         }
5490
5491         if (!fp_access_check(s)) {
5492             return;
5493         }
5494         handle_fmov(s, rd, rn, type, itof);
5495     } else {
5496         /* actual FP conversions */
5497         bool itof = extract32(opcode, 1, 1);
5498
5499         if (type > 1 || (rmode != 0 && opcode > 1)) {
5500             unallocated_encoding(s);
5501             return;
5502         }
5503
5504         if (!fp_access_check(s)) {
5505             return;
5506         }
5507         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
5508     }
5509 }
5510
5511 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
5512  *   31  30  29 28     25 24                          0
5513  * +---+---+---+---------+-----------------------------+
5514  * |   | 0 |   | 1 1 1 1 |                             |
5515  * +---+---+---+---------+-----------------------------+
5516  */
5517 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
5518 {
5519     if (extract32(insn, 24, 1)) {
5520         /* Floating point data-processing (3 source) */
5521         disas_fp_3src(s, insn);
5522     } else if (extract32(insn, 21, 1) == 0) {
5523         /* Floating point to fixed point conversions */
5524         disas_fp_fixed_conv(s, insn);
5525     } else {
5526         switch (extract32(insn, 10, 2)) {
5527         case 1:
5528             /* Floating point conditional compare */
5529             disas_fp_ccomp(s, insn);
5530             break;
5531         case 2:
5532             /* Floating point data-processing (2 source) */
5533             disas_fp_2src(s, insn);
5534             break;
5535         case 3:
5536             /* Floating point conditional select */
5537             disas_fp_csel(s, insn);
5538             break;
5539         case 0:
5540             switch (ctz32(extract32(insn, 12, 4))) {
5541             case 0: /* [15:12] == xxx1 */
5542                 /* Floating point immediate */
5543                 disas_fp_imm(s, insn);
5544                 break;
5545             case 1: /* [15:12] == xx10 */
5546                 /* Floating point compare */
5547                 disas_fp_compare(s, insn);
5548                 break;
5549             case 2: /* [15:12] == x100 */
5550                 /* Floating point data-processing (1 source) */
5551                 disas_fp_1src(s, insn);
5552                 break;
5553             case 3: /* [15:12] == 1000 */
5554                 unallocated_encoding(s);
5555                 break;
5556             default: /* [15:12] == 0000 */
5557                 /* Floating point <-> integer conversions */
5558                 disas_fp_int_conv(s, insn);
5559                 break;
5560             }
5561             break;
5562         }
5563     }
5564 }
5565
5566 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
5567                      int pos)
5568 {
5569     /* Extract 64 bits from the middle of two concatenated 64 bit
5570      * vector register slices left:right. The extracted bits start
5571      * at 'pos' bits into the right (least significant) side.
5572      * We return the result in tcg_right, and guarantee not to
5573      * trash tcg_left.
5574      */
5575     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5576     assert(pos > 0 && pos < 64);
5577
5578     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
5579     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
5580     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
5581
5582     tcg_temp_free_i64(tcg_tmp);
5583 }
5584
5585 /* EXT
5586  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
5587  * +---+---+-------------+-----+---+------+---+------+---+------+------+
5588  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
5589  * +---+---+-------------+-----+---+------+---+------+---+------+------+
5590  */
5591 static void disas_simd_ext(DisasContext *s, uint32_t insn)
5592 {
5593     int is_q = extract32(insn, 30, 1);
5594     int op2 = extract32(insn, 22, 2);
5595     int imm4 = extract32(insn, 11, 4);
5596     int rm = extract32(insn, 16, 5);
5597     int rn = extract32(insn, 5, 5);
5598     int rd = extract32(insn, 0, 5);
5599     int pos = imm4 << 3;
5600     TCGv_i64 tcg_resl, tcg_resh;
5601
5602     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
5603         unallocated_encoding(s);
5604         return;
5605     }
5606
5607     if (!fp_access_check(s)) {
5608         return;
5609     }
5610
5611     tcg_resh = tcg_temp_new_i64();
5612     tcg_resl = tcg_temp_new_i64();
5613
5614     /* Vd gets bits starting at pos bits into Vm:Vn. This is
5615      * either extracting 128 bits from a 128:128 concatenation, or
5616      * extracting 64 bits from a 64:64 concatenation.
5617      */
5618     if (!is_q) {
5619         read_vec_element(s, tcg_resl, rn, 0, MO_64);
5620         if (pos != 0) {
5621             read_vec_element(s, tcg_resh, rm, 0, MO_64);
5622             do_ext64(s, tcg_resh, tcg_resl, pos);
5623         }
5624         tcg_gen_movi_i64(tcg_resh, 0);
5625     } else {
5626         TCGv_i64 tcg_hh;
5627         typedef struct {
5628             int reg;
5629             int elt;
5630         } EltPosns;
5631         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
5632         EltPosns *elt = eltposns;
5633
5634         if (pos >= 64) {
5635             elt++;
5636             pos -= 64;
5637         }
5638
5639         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
5640         elt++;
5641         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
5642         elt++;
5643         if (pos != 0) {
5644             do_ext64(s, tcg_resh, tcg_resl, pos);
5645             tcg_hh = tcg_temp_new_i64();
5646             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
5647             do_ext64(s, tcg_hh, tcg_resh, pos);
5648             tcg_temp_free_i64(tcg_hh);
5649         }
5650     }
5651
5652     write_vec_element(s, tcg_resl, rd, 0, MO_64);
5653     tcg_temp_free_i64(tcg_resl);
5654     write_vec_element(s, tcg_resh, rd, 1, MO_64);
5655     tcg_temp_free_i64(tcg_resh);
5656 }
5657
5658 /* TBL/TBX
5659  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
5660  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
5661  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
5662  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
5663  */
5664 static void disas_simd_tb(DisasContext *s, uint32_t insn)
5665 {
5666     int op2 = extract32(insn, 22, 2);
5667     int is_q = extract32(insn, 30, 1);
5668     int rm = extract32(insn, 16, 5);
5669     int rn = extract32(insn, 5, 5);
5670     int rd = extract32(insn, 0, 5);
5671     int is_tblx = extract32(insn, 12, 1);
5672     int len = extract32(insn, 13, 2);
5673     TCGv_i64 tcg_resl, tcg_resh, tcg_idx;
5674     TCGv_i32 tcg_regno, tcg_numregs;
5675
5676     if (op2 != 0) {
5677         unallocated_encoding(s);
5678         return;
5679     }
5680
5681     if (!fp_access_check(s)) {
5682         return;
5683     }
5684
5685     /* This does a table lookup: for every byte element in the input
5686      * we index into a table formed from up to four vector registers,
5687      * and then the output is the result of the lookups. Our helper
5688      * function does the lookup operation for a single 64 bit part of
5689      * the input.
5690      */
5691     tcg_resl = tcg_temp_new_i64();
5692     tcg_resh = tcg_temp_new_i64();
5693
5694     if (is_tblx) {
5695         read_vec_element(s, tcg_resl, rd, 0, MO_64);
5696     } else {
5697         tcg_gen_movi_i64(tcg_resl, 0);
5698     }
5699     if (is_tblx && is_q) {
5700         read_vec_element(s, tcg_resh, rd, 1, MO_64);
5701     } else {
5702         tcg_gen_movi_i64(tcg_resh, 0);
5703     }
5704
5705     tcg_idx = tcg_temp_new_i64();
5706     tcg_regno = tcg_const_i32(rn);
5707     tcg_numregs = tcg_const_i32(len + 1);
5708     read_vec_element(s, tcg_idx, rm, 0, MO_64);
5709     gen_helper_simd_tbl(tcg_resl, cpu_env, tcg_resl, tcg_idx,
5710                         tcg_regno, tcg_numregs);
5711     if (is_q) {
5712         read_vec_element(s, tcg_idx, rm, 1, MO_64);
5713         gen_helper_simd_tbl(tcg_resh, cpu_env, tcg_resh, tcg_idx,
5714                             tcg_regno, tcg_numregs);
5715     }
5716     tcg_temp_free_i64(tcg_idx);
5717     tcg_temp_free_i32(tcg_regno);
5718     tcg_temp_free_i32(tcg_numregs);
5719
5720     write_vec_element(s, tcg_resl, rd, 0, MO_64);
5721     tcg_temp_free_i64(tcg_resl);
5722     write_vec_element(s, tcg_resh, rd, 1, MO_64);
5723     tcg_temp_free_i64(tcg_resh);
5724 }
5725
5726 /* ZIP/UZP/TRN
5727  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
5728  * +---+---+-------------+------+---+------+---+------------------+------+
5729  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
5730  * +---+---+-------------+------+---+------+---+------------------+------+
5731  */
5732 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
5733 {
5734     int rd = extract32(insn, 0, 5);
5735     int rn = extract32(insn, 5, 5);
5736     int rm = extract32(insn, 16, 5);
5737     int size = extract32(insn, 22, 2);
5738     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
5739      * bit 2 indicates 1 vs 2 variant of the insn.
5740      */
5741     int opcode = extract32(insn, 12, 2);
5742     bool part = extract32(insn, 14, 1);
5743     bool is_q = extract32(insn, 30, 1);
5744     int esize = 8 << size;
5745     int i, ofs;
5746     int datasize = is_q ? 128 : 64;
5747     int elements = datasize / esize;
5748     TCGv_i64 tcg_res, tcg_resl, tcg_resh;
5749
5750     if (opcode == 0 || (size == 3 && !is_q)) {
5751         unallocated_encoding(s);
5752         return;
5753     }
5754
5755     if (!fp_access_check(s)) {
5756         return;
5757     }
5758
5759     tcg_resl = tcg_const_i64(0);
5760     tcg_resh = tcg_const_i64(0);
5761     tcg_res = tcg_temp_new_i64();
5762
5763     for (i = 0; i < elements; i++) {
5764         switch (opcode) {
5765         case 1: /* UZP1/2 */
5766         {
5767             int midpoint = elements / 2;
5768             if (i < midpoint) {
5769                 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
5770             } else {
5771                 read_vec_element(s, tcg_res, rm,
5772                                  2 * (i - midpoint) + part, size);
5773             }
5774             break;
5775         }
5776         case 2: /* TRN1/2 */
5777             if (i & 1) {
5778                 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
5779             } else {
5780                 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
5781             }
5782             break;
5783         case 3: /* ZIP1/2 */
5784         {
5785             int base = part * elements / 2;
5786             if (i & 1) {
5787                 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
5788             } else {
5789                 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
5790             }
5791             break;
5792         }
5793         default:
5794             g_assert_not_reached();
5795         }
5796
5797         ofs = i * esize;
5798         if (ofs < 64) {
5799             tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
5800             tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
5801         } else {
5802             tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
5803             tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
5804         }
5805     }
5806
5807     tcg_temp_free_i64(tcg_res);
5808
5809     write_vec_element(s, tcg_resl, rd, 0, MO_64);
5810     tcg_temp_free_i64(tcg_resl);
5811     write_vec_element(s, tcg_resh, rd, 1, MO_64);
5812     tcg_temp_free_i64(tcg_resh);
5813 }
5814
5815 /*
5816  * do_reduction_op helper
5817  *
5818  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
5819  * important for correct NaN propagation that we do these
5820  * operations in exactly the order specified by the pseudocode.
5821  *
5822  * This is a recursive function, TCG temps should be freed by the
5823  * calling function once it is done with the values.
5824  */
5825 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
5826                                 int esize, int size, int vmap, TCGv_ptr fpst)
5827 {
5828     if (esize == size) {
5829         int element;
5830         TCGMemOp msize = esize == 16 ? MO_16 : MO_32;
5831         TCGv_i32 tcg_elem;
5832
5833         /* We should have one register left here */
5834         assert(ctpop8(vmap) == 1);
5835         element = ctz32(vmap);
5836         assert(element < 8);
5837
5838         tcg_elem = tcg_temp_new_i32();
5839         read_vec_element_i32(s, tcg_elem, rn, element, msize);
5840         return tcg_elem;
5841     } else {
5842         int bits = size / 2;
5843         int shift = ctpop8(vmap) / 2;
5844         int vmap_lo = (vmap >> shift) & vmap;
5845         int vmap_hi = (vmap & ~vmap_lo);
5846         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
5847
5848         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
5849         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
5850         tcg_res = tcg_temp_new_i32();
5851
5852         switch (fpopcode) {
5853         case 0x0c: /* fmaxnmv half-precision */
5854             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
5855             break;
5856         case 0x0f: /* fmaxv half-precision */
5857             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
5858             break;
5859         case 0x1c: /* fminnmv half-precision */
5860             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
5861             break;
5862         case 0x1f: /* fminv half-precision */
5863             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
5864             break;
5865         case 0x2c: /* fmaxnmv */
5866             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
5867             break;
5868         case 0x2f: /* fmaxv */
5869             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
5870             break;
5871         case 0x3c: /* fminnmv */
5872             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
5873             break;
5874         case 0x3f: /* fminv */
5875             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
5876             break;
5877         default:
5878             g_assert_not_reached();
5879         }
5880
5881         tcg_temp_free_i32(tcg_hi);
5882         tcg_temp_free_i32(tcg_lo);
5883         return tcg_res;
5884     }
5885 }
5886
5887 /* AdvSIMD across lanes
5888  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
5889  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5890  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
5891  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5892  */
5893 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
5894 {
5895     int rd = extract32(insn, 0, 5);
5896     int rn = extract32(insn, 5, 5);
5897     int size = extract32(insn, 22, 2);
5898     int opcode = extract32(insn, 12, 5);
5899     bool is_q = extract32(insn, 30, 1);
5900     bool is_u = extract32(insn, 29, 1);
5901     bool is_fp = false;
5902     bool is_min = false;
5903     int esize;
5904     int elements;
5905     int i;
5906     TCGv_i64 tcg_res, tcg_elt;
5907
5908     switch (opcode) {
5909     case 0x1b: /* ADDV */
5910         if (is_u) {
5911             unallocated_encoding(s);
5912             return;
5913         }
5914         /* fall through */
5915     case 0x3: /* SADDLV, UADDLV */
5916     case 0xa: /* SMAXV, UMAXV */
5917     case 0x1a: /* SMINV, UMINV */
5918         if (size == 3 || (size == 2 && !is_q)) {
5919             unallocated_encoding(s);
5920             return;
5921         }
5922         break;
5923     case 0xc: /* FMAXNMV, FMINNMV */
5924     case 0xf: /* FMAXV, FMINV */
5925         /* Bit 1 of size field encodes min vs max and the actual size
5926          * depends on the encoding of the U bit. If not set (and FP16
5927          * enabled) then we do half-precision float instead of single
5928          * precision.
5929          */
5930         is_min = extract32(size, 1, 1);
5931         is_fp = true;
5932         if (!is_u && arm_dc_feature(s, ARM_FEATURE_V8_FP16)) {
5933             size = 1;
5934         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
5935             unallocated_encoding(s);
5936             return;
5937         } else {
5938             size = 2;
5939         }
5940         break;
5941     default:
5942         unallocated_encoding(s);
5943         return;
5944     }
5945
5946     if (!fp_access_check(s)) {
5947         return;
5948     }
5949
5950     esize = 8 << size;
5951     elements = (is_q ? 128 : 64) / esize;
5952
5953     tcg_res = tcg_temp_new_i64();
5954     tcg_elt = tcg_temp_new_i64();
5955
5956     /* These instructions operate across all lanes of a vector
5957      * to produce a single result. We can guarantee that a 64
5958      * bit intermediate is sufficient:
5959      *  + for [US]ADDLV the maximum element size is 32 bits, and
5960      *    the result type is 64 bits
5961      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
5962      *    same as the element size, which is 32 bits at most
5963      * For the integer operations we can choose to work at 64
5964      * or 32 bits and truncate at the end; for simplicity
5965      * we use 64 bits always. The floating point
5966      * ops do require 32 bit intermediates, though.
5967      */
5968     if (!is_fp) {
5969         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
5970
5971         for (i = 1; i < elements; i++) {
5972             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
5973
5974             switch (opcode) {
5975             case 0x03: /* SADDLV / UADDLV */
5976             case 0x1b: /* ADDV */
5977                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
5978                 break;
5979             case 0x0a: /* SMAXV / UMAXV */
5980                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
5981                                     tcg_res,
5982                                     tcg_res, tcg_elt, tcg_res, tcg_elt);
5983                 break;
5984             case 0x1a: /* SMINV / UMINV */
5985                 tcg_gen_movcond_i64(is_u ? TCG_COND_LEU : TCG_COND_LE,
5986                                     tcg_res,
5987                                     tcg_res, tcg_elt, tcg_res, tcg_elt);
5988                 break;
5989                 break;
5990             default:
5991                 g_assert_not_reached();
5992             }
5993
5994         }
5995     } else {
5996         /* Floating point vector reduction ops which work across 32
5997          * bit (single) or 16 bit (half-precision) intermediates.
5998          * Note that correct NaN propagation requires that we do these
5999          * operations in exactly the order specified by the pseudocode.
6000          */
6001         TCGv_ptr fpst = get_fpstatus_ptr(size == MO_16);
6002         int fpopcode = opcode | is_min << 4 | is_u << 5;
6003         int vmap = (1 << elements) - 1;
6004         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
6005                                              (is_q ? 128 : 64), vmap, fpst);
6006         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
6007         tcg_temp_free_i32(tcg_res32);
6008         tcg_temp_free_ptr(fpst);
6009     }
6010
6011     tcg_temp_free_i64(tcg_elt);
6012
6013     /* Now truncate the result to the width required for the final output */
6014     if (opcode == 0x03) {
6015         /* SADDLV, UADDLV: result is 2*esize */
6016         size++;
6017     }
6018
6019     switch (size) {
6020     case 0:
6021         tcg_gen_ext8u_i64(tcg_res, tcg_res);
6022         break;
6023     case 1:
6024         tcg_gen_ext16u_i64(tcg_res, tcg_res);
6025         break;
6026     case 2:
6027         tcg_gen_ext32u_i64(tcg_res, tcg_res);
6028         break;
6029     case 3:
6030         break;
6031     default:
6032         g_assert_not_reached();
6033     }
6034
6035     write_fp_dreg(s, rd, tcg_res);
6036     tcg_temp_free_i64(tcg_res);
6037 }
6038
6039 /* DUP (Element, Vector)
6040  *
6041  *  31  30   29              21 20    16 15        10  9    5 4    0
6042  * +---+---+-------------------+--------+-------------+------+------+
6043  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
6044  * +---+---+-------------------+--------+-------------+------+------+
6045  *
6046  * size: encoded in imm5 (see ARM ARM LowestSetBit())
6047  */
6048 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
6049                              int imm5)
6050 {
6051     int size = ctz32(imm5);
6052     int index = imm5 >> (size + 1);
6053
6054     if (size > 3 || (size == 3 && !is_q)) {
6055         unallocated_encoding(s);
6056         return;
6057     }
6058
6059     if (!fp_access_check(s)) {
6060         return;
6061     }
6062
6063     tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
6064                          vec_reg_offset(s, rn, index, size),
6065                          is_q ? 16 : 8, vec_full_reg_size(s));
6066 }
6067
6068 /* DUP (element, scalar)
6069  *  31                   21 20    16 15        10  9    5 4    0
6070  * +-----------------------+--------+-------------+------+------+
6071  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
6072  * +-----------------------+--------+-------------+------+------+
6073  */
6074 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
6075                               int imm5)
6076 {
6077     int size = ctz32(imm5);
6078     int index;
6079     TCGv_i64 tmp;
6080
6081     if (size > 3) {
6082         unallocated_encoding(s);
6083         return;
6084     }
6085
6086     if (!fp_access_check(s)) {
6087         return;
6088     }
6089
6090     index = imm5 >> (size + 1);
6091
6092     /* This instruction just extracts the specified element and
6093      * zero-extends it into the bottom of the destination register.
6094      */
6095     tmp = tcg_temp_new_i64();
6096     read_vec_element(s, tmp, rn, index, size);
6097     write_fp_dreg(s, rd, tmp);
6098     tcg_temp_free_i64(tmp);
6099 }
6100
6101 /* DUP (General)
6102  *
6103  *  31  30   29              21 20    16 15        10  9    5 4    0
6104  * +---+---+-------------------+--------+-------------+------+------+
6105  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
6106  * +---+---+-------------------+--------+-------------+------+------+
6107  *
6108  * size: encoded in imm5 (see ARM ARM LowestSetBit())
6109  */
6110 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
6111                              int imm5)
6112 {
6113     int size = ctz32(imm5);
6114     uint32_t dofs, oprsz, maxsz;
6115
6116     if (size > 3 || ((size == 3) && !is_q)) {
6117         unallocated_encoding(s);
6118         return;
6119     }
6120
6121     if (!fp_access_check(s)) {
6122         return;
6123     }
6124
6125     dofs = vec_full_reg_offset(s, rd);
6126     oprsz = is_q ? 16 : 8;
6127     maxsz = vec_full_reg_size(s);
6128
6129     tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
6130 }
6131
6132 /* INS (Element)
6133  *
6134  *  31                   21 20    16 15  14    11  10 9    5 4    0
6135  * +-----------------------+--------+------------+---+------+------+
6136  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
6137  * +-----------------------+--------+------------+---+------+------+
6138  *
6139  * size: encoded in imm5 (see ARM ARM LowestSetBit())
6140  * index: encoded in imm5<4:size+1>
6141  */
6142 static void handle_simd_inse(DisasContext *s, int rd, int rn,
6143                              int imm4, int imm5)
6144 {
6145     int size = ctz32(imm5);
6146     int src_index, dst_index;
6147     TCGv_i64 tmp;
6148
6149     if (size > 3) {
6150         unallocated_encoding(s);
6151         return;
6152     }
6153
6154     if (!fp_access_check(s)) {
6155         return;
6156     }
6157
6158     dst_index = extract32(imm5, 1+size, 5);
6159     src_index = extract32(imm4, size, 4);
6160
6161     tmp = tcg_temp_new_i64();
6162
6163     read_vec_element(s, tmp, rn, src_index, size);
6164     write_vec_element(s, tmp, rd, dst_index, size);
6165
6166     tcg_temp_free_i64(tmp);
6167 }
6168
6169
6170 /* INS (General)
6171  *
6172  *  31                   21 20    16 15        10  9    5 4    0
6173  * +-----------------------+--------+-------------+------+------+
6174  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
6175  * +-----------------------+--------+-------------+------+------+
6176  *
6177  * size: encoded in imm5 (see ARM ARM LowestSetBit())
6178  * index: encoded in imm5<4:size+1>
6179  */
6180 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
6181 {
6182     int size = ctz32(imm5);
6183     int idx;
6184
6185     if (size > 3) {
6186         unallocated_encoding(s);
6187         return;
6188     }
6189
6190     if (!fp_access_check(s)) {
6191         return;
6192     }
6193
6194     idx = extract32(imm5, 1 + size, 4 - size);
6195     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
6196 }
6197
6198 /*
6199  * UMOV (General)
6200  * SMOV (General)
6201  *
6202  *  31  30   29              21 20    16 15    12   10 9    5 4    0
6203  * +---+---+-------------------+--------+-------------+------+------+
6204  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
6205  * +---+---+-------------------+--------+-------------+------+------+
6206  *
6207  * U: unsigned when set
6208  * size: encoded in imm5 (see ARM ARM LowestSetBit())
6209  */
6210 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
6211                                   int rn, int rd, int imm5)
6212 {
6213     int size = ctz32(imm5);
6214     int element;
6215     TCGv_i64 tcg_rd;
6216
6217     /* Check for UnallocatedEncodings */
6218     if (is_signed) {
6219         if (size > 2 || (size == 2 && !is_q)) {
6220             unallocated_encoding(s);
6221             return;
6222         }
6223     } else {
6224         if (size > 3
6225             || (size < 3 && is_q)
6226             || (size == 3 && !is_q)) {
6227             unallocated_encoding(s);
6228             return;
6229         }
6230     }
6231
6232     if (!fp_access_check(s)) {
6233         return;
6234     }
6235
6236     element = extract32(imm5, 1+size, 4);
6237
6238     tcg_rd = cpu_reg(s, rd);
6239     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
6240     if (is_signed && !is_q) {
6241         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
6242     }
6243 }
6244
6245 /* AdvSIMD copy
6246  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
6247  * +---+---+----+-----------------+------+---+------+---+------+------+
6248  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
6249  * +---+---+----+-----------------+------+---+------+---+------+------+
6250  */
6251 static void disas_simd_copy(DisasContext *s, uint32_t insn)
6252 {
6253     int rd = extract32(insn, 0, 5);
6254     int rn = extract32(insn, 5, 5);
6255     int imm4 = extract32(insn, 11, 4);
6256     int op = extract32(insn, 29, 1);
6257     int is_q = extract32(insn, 30, 1);
6258     int imm5 = extract32(insn, 16, 5);
6259
6260     if (op) {
6261         if (is_q) {
6262             /* INS (element) */
6263             handle_simd_inse(s, rd, rn, imm4, imm5);
6264         } else {
6265             unallocated_encoding(s);
6266         }
6267     } else {
6268         switch (imm4) {
6269         case 0:
6270             /* DUP (element - vector) */
6271             handle_simd_dupe(s, is_q, rd, rn, imm5);
6272             break;
6273         case 1:
6274             /* DUP (general) */
6275             handle_simd_dupg(s, is_q, rd, rn, imm5);
6276             break;
6277         case 3:
6278             if (is_q) {
6279                 /* INS (general) */
6280                 handle_simd_insg(s, rd, rn, imm5);
6281             } else {
6282                 unallocated_encoding(s);
6283             }
6284             break;
6285         case 5:
6286         case 7:
6287             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
6288             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
6289             break;
6290         default:
6291             unallocated_encoding(s);
6292             break;
6293         }
6294     }
6295 }
6296
6297 /* AdvSIMD modified immediate
6298  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
6299  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
6300  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
6301  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
6302  *
6303  * There are a number of operations that can be carried out here:
6304  *   MOVI - move (shifted) imm into register
6305  *   MVNI - move inverted (shifted) imm into register
6306  *   ORR  - bitwise OR of (shifted) imm with register
6307  *   BIC  - bitwise clear of (shifted) imm with register
6308  * With ARMv8.2 we also have:
6309  *   FMOV half-precision
6310  */
6311 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
6312 {
6313     int rd = extract32(insn, 0, 5);
6314     int cmode = extract32(insn, 12, 4);
6315     int cmode_3_1 = extract32(cmode, 1, 3);
6316     int cmode_0 = extract32(cmode, 0, 1);
6317     int o2 = extract32(insn, 11, 1);
6318     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
6319     bool is_neg = extract32(insn, 29, 1);
6320     bool is_q = extract32(insn, 30, 1);
6321     uint64_t imm = 0;
6322
6323     if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
6324         /* Check for FMOV (vector, immediate) - half-precision */
6325         if (!(arm_dc_feature(s, ARM_FEATURE_V8_FP16) && o2 && cmode == 0xf)) {
6326             unallocated_encoding(s);
6327             return;
6328         }
6329     }
6330
6331     if (!fp_access_check(s)) {
6332         return;
6333     }
6334
6335     /* See AdvSIMDExpandImm() in ARM ARM */
6336     switch (cmode_3_1) {
6337     case 0: /* Replicate(Zeros(24):imm8, 2) */
6338     case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
6339     case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
6340     case 3: /* Replicate(imm8:Zeros(24), 2) */
6341     {
6342         int shift = cmode_3_1 * 8;
6343         imm = bitfield_replicate(abcdefgh << shift, 32);
6344         break;
6345     }
6346     case 4: /* Replicate(Zeros(8):imm8, 4) */
6347     case 5: /* Replicate(imm8:Zeros(8), 4) */
6348     {
6349         int shift = (cmode_3_1 & 0x1) * 8;
6350         imm = bitfield_replicate(abcdefgh << shift, 16);
6351         break;
6352     }
6353     case 6:
6354         if (cmode_0) {
6355             /* Replicate(Zeros(8):imm8:Ones(16), 2) */
6356             imm = (abcdefgh << 16) | 0xffff;
6357         } else {
6358             /* Replicate(Zeros(16):imm8:Ones(8), 2) */
6359             imm = (abcdefgh << 8) | 0xff;
6360         }
6361         imm = bitfield_replicate(imm, 32);
6362         break;
6363     case 7:
6364         if (!cmode_0 && !is_neg) {
6365             imm = bitfield_replicate(abcdefgh, 8);
6366         } else if (!cmode_0 && is_neg) {
6367             int i;
6368             imm = 0;
6369             for (i = 0; i < 8; i++) {
6370                 if ((abcdefgh) & (1 << i)) {
6371                     imm |= 0xffULL << (i * 8);
6372                 }
6373             }
6374         } else if (cmode_0) {
6375             if (is_neg) {
6376                 imm = (abcdefgh & 0x3f) << 48;
6377                 if (abcdefgh & 0x80) {
6378                     imm |= 0x8000000000000000ULL;
6379                 }
6380                 if (abcdefgh & 0x40) {
6381                     imm |= 0x3fc0000000000000ULL;
6382                 } else {
6383                     imm |= 0x4000000000000000ULL;
6384                 }
6385             } else {
6386                 if (o2) {
6387                     /* FMOV (vector, immediate) - half-precision */
6388                     imm = vfp_expand_imm(MO_16, abcdefgh);
6389                     /* now duplicate across the lanes */
6390                     imm = bitfield_replicate(imm, 16);
6391                 } else {
6392                     imm = (abcdefgh & 0x3f) << 19;
6393                     if (abcdefgh & 0x80) {
6394                         imm |= 0x80000000;
6395                     }
6396                     if (abcdefgh & 0x40) {
6397                         imm |= 0x3e000000;
6398                     } else {
6399                         imm |= 0x40000000;
6400                     }
6401                     imm |= (imm << 32);
6402                 }
6403             }
6404         }
6405         break;
6406     default:
6407         fprintf(stderr, "%s: cmode_3_1: %x\n", __func__, cmode_3_1);
6408         g_assert_not_reached();
6409     }
6410
6411     if (cmode_3_1 != 7 && is_neg) {
6412         imm = ~imm;
6413     }
6414
6415     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
6416         /* MOVI or MVNI, with MVNI negation handled above.  */
6417         tcg_gen_gvec_dup64i(vec_full_reg_offset(s, rd), is_q ? 16 : 8,
6418                             vec_full_reg_size(s), imm);
6419     } else {
6420         /* ORR or BIC, with BIC negation to AND handled above.  */
6421         if (is_neg) {
6422             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
6423         } else {
6424             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
6425         }
6426     }
6427 }
6428
6429 /* AdvSIMD scalar copy
6430  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
6431  * +-----+----+-----------------+------+---+------+---+------+------+
6432  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
6433  * +-----+----+-----------------+------+---+------+---+------+------+
6434  */
6435 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
6436 {
6437     int rd = extract32(insn, 0, 5);
6438     int rn = extract32(insn, 5, 5);
6439     int imm4 = extract32(insn, 11, 4);
6440     int imm5 = extract32(insn, 16, 5);
6441     int op = extract32(insn, 29, 1);
6442
6443     if (op != 0 || imm4 != 0) {
6444         unallocated_encoding(s);
6445         return;
6446     }
6447
6448     /* DUP (element, scalar) */
6449     handle_simd_dupes(s, rd, rn, imm5);
6450 }
6451
6452 /* AdvSIMD scalar pairwise
6453  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
6454  * +-----+---+-----------+------+-----------+--------+-----+------+------+
6455  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
6456  * +-----+---+-----------+------+-----------+--------+-----+------+------+
6457  */
6458 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
6459 {
6460     int u = extract32(insn, 29, 1);
6461     int size = extract32(insn, 22, 2);
6462     int opcode = extract32(insn, 12, 5);
6463     int rn = extract32(insn, 5, 5);
6464     int rd = extract32(insn, 0, 5);
6465     TCGv_ptr fpst;
6466
6467     /* For some ops (the FP ones), size[1] is part of the encoding.
6468      * For ADDP strictly it is not but size[1] is always 1 for valid
6469      * encodings.
6470      */
6471     opcode |= (extract32(size, 1, 1) << 5);
6472
6473     switch (opcode) {
6474     case 0x3b: /* ADDP */
6475         if (u || size != 3) {
6476             unallocated_encoding(s);
6477             return;
6478         }
6479         if (!fp_access_check(s)) {
6480             return;
6481         }
6482
6483         fpst = NULL;
6484         break;
6485     case 0xc: /* FMAXNMP */
6486     case 0xd: /* FADDP */
6487     case 0xf: /* FMAXP */
6488     case 0x2c: /* FMINNMP */
6489     case 0x2f: /* FMINP */
6490         /* FP op, size[0] is 32 or 64 bit*/
6491         if (!u) {
6492             if (!arm_dc_feature(s, ARM_FEATURE_V8_FP16)) {
6493                 unallocated_encoding(s);
6494                 return;
6495             } else {
6496                 size = MO_16;
6497             }
6498         } else {
6499             size = extract32(size, 0, 1) ? MO_64 : MO_32;
6500         }
6501
6502         if (!fp_access_check(s)) {
6503             return;
6504         }
6505
6506         fpst = get_fpstatus_ptr(size == MO_16);
6507         break;
6508     default:
6509         unallocated_encoding(s);
6510         return;
6511     }
6512
6513     if (size == MO_64) {
6514         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6515         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6516         TCGv_i64 tcg_res = tcg_temp_new_i64();
6517
6518         read_vec_element(s, tcg_op1, rn, 0, MO_64);
6519         read_vec_element(s, tcg_op2, rn, 1, MO_64);
6520
6521         switch (opcode) {
6522         case 0x3b: /* ADDP */
6523             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
6524             break;
6525         case 0xc: /* FMAXNMP */
6526             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6527             break;
6528         case 0xd: /* FADDP */
6529             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6530             break;
6531         case 0xf: /* FMAXP */
6532             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6533             break;
6534         case 0x2c: /* FMINNMP */
6535             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6536             break;
6537         case 0x2f: /* FMINP */
6538             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6539             break;
6540         default:
6541             g_assert_not_reached();
6542         }
6543
6544         write_fp_dreg(s, rd, tcg_res);
6545
6546         tcg_temp_free_i64(tcg_op1);
6547         tcg_temp_free_i64(tcg_op2);
6548         tcg_temp_free_i64(tcg_res);
6549     } else {
6550         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6551         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6552         TCGv_i32 tcg_res = tcg_temp_new_i32();
6553
6554         read_vec_element_i32(s, tcg_op1, rn, 0, size);
6555         read_vec_element_i32(s, tcg_op2, rn, 1, size);
6556
6557         if (size == MO_16) {
6558             switch (opcode) {
6559             case 0xc: /* FMAXNMP */
6560                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6561                 break;
6562             case 0xd: /* FADDP */
6563                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6564                 break;
6565             case 0xf: /* FMAXP */
6566                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6567                 break;
6568             case 0x2c: /* FMINNMP */
6569                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6570                 break;
6571             case 0x2f: /* FMINP */
6572                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6573                 break;
6574             default:
6575                 g_assert_not_reached();
6576             }
6577         } else {
6578             switch (opcode) {
6579             case 0xc: /* FMAXNMP */
6580                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6581                 break;
6582             case 0xd: /* FADDP */
6583                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6584                 break;
6585             case 0xf: /* FMAXP */
6586                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6587                 break;
6588             case 0x2c: /* FMINNMP */
6589                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6590                 break;
6591             case 0x2f: /* FMINP */
6592                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6593                 break;
6594             default:
6595                 g_assert_not_reached();
6596             }
6597         }
6598
6599         write_fp_sreg(s, rd, tcg_res);
6600
6601         tcg_temp_free_i32(tcg_op1);
6602         tcg_temp_free_i32(tcg_op2);
6603         tcg_temp_free_i32(tcg_res);
6604     }
6605
6606     if (fpst) {
6607         tcg_temp_free_ptr(fpst);
6608     }
6609 }
6610
6611 /*
6612  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
6613  *
6614  * This code is handles the common shifting code and is used by both
6615  * the vector and scalar code.
6616  */
6617 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6618                                     TCGv_i64 tcg_rnd, bool accumulate,
6619                                     bool is_u, int size, int shift)
6620 {
6621     bool extended_result = false;
6622     bool round = tcg_rnd != NULL;
6623     int ext_lshift = 0;
6624     TCGv_i64 tcg_src_hi;
6625
6626     if (round && size == 3) {
6627         extended_result = true;
6628         ext_lshift = 64 - shift;
6629         tcg_src_hi = tcg_temp_new_i64();
6630     } else if (shift == 64) {
6631         if (!accumulate && is_u) {
6632             /* result is zero */
6633             tcg_gen_movi_i64(tcg_res, 0);
6634             return;
6635         }
6636     }
6637
6638     /* Deal with the rounding step */
6639     if (round) {
6640         if (extended_result) {
6641             TCGv_i64 tcg_zero = tcg_const_i64(0);
6642             if (!is_u) {
6643                 /* take care of sign extending tcg_res */
6644                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
6645                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
6646                                  tcg_src, tcg_src_hi,
6647                                  tcg_rnd, tcg_zero);
6648             } else {
6649                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
6650                                  tcg_src, tcg_zero,
6651                                  tcg_rnd, tcg_zero);
6652             }
6653             tcg_temp_free_i64(tcg_zero);
6654         } else {
6655             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
6656         }
6657     }
6658
6659     /* Now do the shift right */
6660     if (round && extended_result) {
6661         /* extended case, >64 bit precision required */
6662         if (ext_lshift == 0) {
6663             /* special case, only high bits matter */
6664             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
6665         } else {
6666             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6667             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
6668             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
6669         }
6670     } else {
6671         if (is_u) {
6672             if (shift == 64) {
6673                 /* essentially shifting in 64 zeros */
6674                 tcg_gen_movi_i64(tcg_src, 0);
6675             } else {
6676                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6677             }
6678         } else {
6679             if (shift == 64) {
6680                 /* effectively extending the sign-bit */
6681                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
6682             } else {
6683                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
6684             }
6685         }
6686     }
6687
6688     if (accumulate) {
6689         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
6690     } else {
6691         tcg_gen_mov_i64(tcg_res, tcg_src);
6692     }
6693
6694     if (extended_result) {
6695         tcg_temp_free_i64(tcg_src_hi);
6696     }
6697 }
6698
6699 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
6700 static void handle_scalar_simd_shri(DisasContext *s,
6701                                     bool is_u, int immh, int immb,
6702                                     int opcode, int rn, int rd)
6703 {
6704     const int size = 3;
6705     int immhb = immh << 3 | immb;
6706     int shift = 2 * (8 << size) - immhb;
6707     bool accumulate = false;
6708     bool round = false;
6709     bool insert = false;
6710     TCGv_i64 tcg_rn;
6711     TCGv_i64 tcg_rd;
6712     TCGv_i64 tcg_round;
6713
6714     if (!extract32(immh, 3, 1)) {
6715         unallocated_encoding(s);
6716         return;
6717     }
6718
6719     if (!fp_access_check(s)) {
6720         return;
6721     }
6722
6723     switch (opcode) {
6724     case 0x02: /* SSRA / USRA (accumulate) */
6725         accumulate = true;
6726         break;
6727     case 0x04: /* SRSHR / URSHR (rounding) */
6728         round = true;
6729         break;
6730     case 0x06: /* SRSRA / URSRA (accum + rounding) */
6731         accumulate = round = true;
6732         break;
6733     case 0x08: /* SRI */
6734         insert = true;
6735         break;
6736     }
6737
6738     if (round) {
6739         uint64_t round_const = 1ULL << (shift - 1);
6740         tcg_round = tcg_const_i64(round_const);
6741     } else {
6742         tcg_round = NULL;
6743     }
6744
6745     tcg_rn = read_fp_dreg(s, rn);
6746     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6747
6748     if (insert) {
6749         /* shift count same as element size is valid but does nothing;
6750          * special case to avoid potential shift by 64.
6751          */
6752         int esize = 8 << size;
6753         if (shift != esize) {
6754             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
6755             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
6756         }
6757     } else {
6758         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6759                                 accumulate, is_u, size, shift);
6760     }
6761
6762     write_fp_dreg(s, rd, tcg_rd);
6763
6764     tcg_temp_free_i64(tcg_rn);
6765     tcg_temp_free_i64(tcg_rd);
6766     if (round) {
6767         tcg_temp_free_i64(tcg_round);
6768     }
6769 }
6770
6771 /* SHL/SLI - Scalar shift left */
6772 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
6773                                     int immh, int immb, int opcode,
6774                                     int rn, int rd)
6775 {
6776     int size = 32 - clz32(immh) - 1;
6777     int immhb = immh << 3 | immb;
6778     int shift = immhb - (8 << size);
6779     TCGv_i64 tcg_rn = new_tmp_a64(s);
6780     TCGv_i64 tcg_rd = new_tmp_a64(s);
6781
6782     if (!extract32(immh, 3, 1)) {
6783         unallocated_encoding(s);
6784         return;
6785     }
6786
6787     if (!fp_access_check(s)) {
6788         return;
6789     }
6790
6791     tcg_rn = read_fp_dreg(s, rn);
6792     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6793
6794     if (insert) {
6795         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
6796     } else {
6797         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
6798     }
6799
6800     write_fp_dreg(s, rd, tcg_rd);
6801
6802     tcg_temp_free_i64(tcg_rn);
6803     tcg_temp_free_i64(tcg_rd);
6804 }
6805
6806 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
6807  * (signed/unsigned) narrowing */
6808 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
6809                                    bool is_u_shift, bool is_u_narrow,
6810                                    int immh, int immb, int opcode,
6811                                    int rn, int rd)
6812 {
6813     int immhb = immh << 3 | immb;
6814     int size = 32 - clz32(immh) - 1;
6815     int esize = 8 << size;
6816     int shift = (2 * esize) - immhb;
6817     int elements = is_scalar ? 1 : (64 / esize);
6818     bool round = extract32(opcode, 0, 1);
6819     TCGMemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
6820     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
6821     TCGv_i32 tcg_rd_narrowed;
6822     TCGv_i64 tcg_final;
6823
6824     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
6825         { gen_helper_neon_narrow_sat_s8,
6826           gen_helper_neon_unarrow_sat8 },
6827         { gen_helper_neon_narrow_sat_s16,
6828           gen_helper_neon_unarrow_sat16 },
6829         { gen_helper_neon_narrow_sat_s32,
6830           gen_helper_neon_unarrow_sat32 },
6831         { NULL, NULL },
6832     };
6833     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
6834         gen_helper_neon_narrow_sat_u8,
6835         gen_helper_neon_narrow_sat_u16,
6836         gen_helper_neon_narrow_sat_u32,
6837         NULL
6838     };
6839     NeonGenNarrowEnvFn *narrowfn;
6840
6841     int i;
6842
6843     assert(size < 4);
6844
6845     if (extract32(immh, 3, 1)) {
6846         unallocated_encoding(s);
6847         return;
6848     }
6849
6850     if (!fp_access_check(s)) {
6851         return;
6852     }
6853
6854     if (is_u_shift) {
6855         narrowfn = unsigned_narrow_fns[size];
6856     } else {
6857         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
6858     }
6859
6860     tcg_rn = tcg_temp_new_i64();
6861     tcg_rd = tcg_temp_new_i64();
6862     tcg_rd_narrowed = tcg_temp_new_i32();
6863     tcg_final = tcg_const_i64(0);
6864
6865     if (round) {
6866         uint64_t round_const = 1ULL << (shift - 1);
6867         tcg_round = tcg_const_i64(round_const);
6868     } else {
6869         tcg_round = NULL;
6870     }
6871
6872     for (i = 0; i < elements; i++) {
6873         read_vec_element(s, tcg_rn, rn, i, ldop);
6874         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6875                                 false, is_u_shift, size+1, shift);
6876         narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
6877         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
6878         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
6879     }
6880
6881     if (!is_q) {
6882         write_vec_element(s, tcg_final, rd, 0, MO_64);
6883     } else {
6884         write_vec_element(s, tcg_final, rd, 1, MO_64);
6885     }
6886
6887     if (round) {
6888         tcg_temp_free_i64(tcg_round);
6889     }
6890     tcg_temp_free_i64(tcg_rn);
6891     tcg_temp_free_i64(tcg_rd);
6892     tcg_temp_free_i32(tcg_rd_narrowed);
6893     tcg_temp_free_i64(tcg_final);
6894
6895     clear_vec_high(s, is_q, rd);
6896 }
6897
6898 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
6899 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
6900                              bool src_unsigned, bool dst_unsigned,
6901                              int immh, int immb, int rn, int rd)
6902 {
6903     int immhb = immh << 3 | immb;
6904     int size = 32 - clz32(immh) - 1;
6905     int shift = immhb - (8 << size);
6906     int pass;
6907
6908     assert(immh != 0);
6909     assert(!(scalar && is_q));
6910
6911     if (!scalar) {
6912         if (!is_q && extract32(immh, 3, 1)) {
6913             unallocated_encoding(s);
6914             return;
6915         }
6916
6917         /* Since we use the variable-shift helpers we must
6918          * replicate the shift count into each element of
6919          * the tcg_shift value.
6920          */
6921         switch (size) {
6922         case 0:
6923             shift |= shift << 8;
6924             /* fall through */
6925         case 1:
6926             shift |= shift << 16;
6927             break;
6928         case 2:
6929         case 3:
6930             break;
6931         default:
6932             g_assert_not_reached();
6933         }
6934     }
6935
6936     if (!fp_access_check(s)) {
6937         return;
6938     }
6939
6940     if (size == 3) {
6941         TCGv_i64 tcg_shift = tcg_const_i64(shift);
6942         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
6943             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
6944             { NULL, gen_helper_neon_qshl_u64 },
6945         };
6946         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
6947         int maxpass = is_q ? 2 : 1;
6948
6949         for (pass = 0; pass < maxpass; pass++) {
6950             TCGv_i64 tcg_op = tcg_temp_new_i64();
6951
6952             read_vec_element(s, tcg_op, rn, pass, MO_64);
6953             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6954             write_vec_element(s, tcg_op, rd, pass, MO_64);
6955
6956             tcg_temp_free_i64(tcg_op);
6957         }
6958         tcg_temp_free_i64(tcg_shift);
6959         clear_vec_high(s, is_q, rd);
6960     } else {
6961         TCGv_i32 tcg_shift = tcg_const_i32(shift);
6962         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
6963             {
6964                 { gen_helper_neon_qshl_s8,
6965                   gen_helper_neon_qshl_s16,
6966                   gen_helper_neon_qshl_s32 },
6967                 { gen_helper_neon_qshlu_s8,
6968                   gen_helper_neon_qshlu_s16,
6969                   gen_helper_neon_qshlu_s32 }
6970             }, {
6971                 { NULL, NULL, NULL },
6972                 { gen_helper_neon_qshl_u8,
6973                   gen_helper_neon_qshl_u16,
6974                   gen_helper_neon_qshl_u32 }
6975             }
6976         };
6977         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
6978         TCGMemOp memop = scalar ? size : MO_32;
6979         int maxpass = scalar ? 1 : is_q ? 4 : 2;
6980
6981         for (pass = 0; pass < maxpass; pass++) {
6982             TCGv_i32 tcg_op = tcg_temp_new_i32();
6983
6984             read_vec_element_i32(s, tcg_op, rn, pass, memop);
6985             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6986             if (scalar) {
6987                 switch (size) {
6988                 case 0:
6989                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
6990                     break;
6991                 case 1:
6992                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
6993                     break;
6994                 case 2:
6995                     break;
6996                 default:
6997                     g_assert_not_reached();
6998                 }
6999                 write_fp_sreg(s, rd, tcg_op);
7000             } else {
7001                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
7002             }
7003
7004             tcg_temp_free_i32(tcg_op);
7005         }
7006         tcg_temp_free_i32(tcg_shift);
7007
7008         if (!scalar) {
7009             clear_vec_high(s, is_q, rd);
7010         }
7011     }
7012 }
7013
7014 /* Common vector code for handling integer to FP conversion */
7015 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
7016                                    int elements, int is_signed,
7017                                    int fracbits, int size)
7018 {
7019     TCGv_ptr tcg_fpst = get_fpstatus_ptr(size == MO_16);
7020     TCGv_i32 tcg_shift = NULL;
7021
7022     TCGMemOp mop = size | (is_signed ? MO_SIGN : 0);
7023     int pass;
7024
7025     if (fracbits || size == MO_64) {
7026         tcg_shift = tcg_const_i32(fracbits);
7027     }
7028
7029     if (size == MO_64) {
7030         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
7031         TCGv_i64 tcg_double = tcg_temp_new_i64();
7032
7033         for (pass = 0; pass < elements; pass++) {
7034             read_vec_element(s, tcg_int64, rn, pass, mop);
7035
7036             if (is_signed) {
7037                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
7038                                      tcg_shift, tcg_fpst);
7039             } else {
7040                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
7041                                      tcg_shift, tcg_fpst);
7042             }
7043             if (elements == 1) {
7044                 write_fp_dreg(s, rd, tcg_double);
7045             } else {
7046                 write_vec_element(s, tcg_double, rd, pass, MO_64);
7047             }
7048         }
7049
7050         tcg_temp_free_i64(tcg_int64);
7051         tcg_temp_free_i64(tcg_double);
7052
7053     } else {
7054         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
7055         TCGv_i32 tcg_float = tcg_temp_new_i32();
7056
7057         for (pass = 0; pass < elements; pass++) {
7058             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
7059
7060             switch (size) {
7061             case MO_32:
7062                 if (fracbits) {
7063                     if (is_signed) {
7064                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
7065                                              tcg_shift, tcg_fpst);
7066                     } else {
7067                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
7068                                              tcg_shift, tcg_fpst);
7069                     }
7070                 } else {
7071                     if (is_signed) {
7072                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
7073                     } else {
7074                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
7075                     }
7076                 }
7077                 break;
7078             case MO_16:
7079                 if (fracbits) {
7080                     if (is_signed) {
7081                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
7082                                              tcg_shift, tcg_fpst);
7083                     } else {
7084                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
7085                                              tcg_shift, tcg_fpst);
7086                     }
7087                 } else {
7088                     if (is_signed) {
7089                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
7090                     } else {
7091                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
7092                     }
7093                 }
7094                 break;
7095             default:
7096                 g_assert_not_reached();
7097             }
7098
7099             if (elements == 1) {
7100                 write_fp_sreg(s, rd, tcg_float);
7101             } else {
7102                 write_vec_element_i32(s, tcg_float, rd, pass, size);
7103             }
7104         }
7105
7106         tcg_temp_free_i32(tcg_int32);
7107         tcg_temp_free_i32(tcg_float);
7108     }
7109
7110     tcg_temp_free_ptr(tcg_fpst);
7111     if (tcg_shift) {
7112         tcg_temp_free_i32(tcg_shift);
7113     }
7114
7115     clear_vec_high(s, elements << size == 16, rd);
7116 }
7117
7118 /* UCVTF/SCVTF - Integer to FP conversion */
7119 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
7120                                          bool is_q, bool is_u,
7121                                          int immh, int immb, int opcode,
7122                                          int rn, int rd)
7123 {
7124     bool is_double = extract32(immh, 3, 1);
7125     int size = is_double ? MO_64 : MO_32;
7126     int elements;
7127     int immhb = immh << 3 | immb;
7128     int fracbits = (is_double ? 128 : 64) - immhb;
7129
7130     if (!extract32(immh, 2, 2)) {
7131         unallocated_encoding(s);
7132         return;
7133     }
7134
7135     if (is_scalar) {
7136         elements = 1;
7137     } else {
7138         elements = is_double ? 2 : is_q ? 4 : 2;
7139         if (is_double && !is_q) {
7140             unallocated_encoding(s);
7141             return;
7142         }
7143     }
7144
7145     if (!fp_access_check(s)) {
7146         return;
7147     }
7148
7149     /* immh == 0 would be a failure of the decode logic */
7150     g_assert(immh);
7151
7152     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
7153 }
7154
7155 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
7156 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
7157                                          bool is_q, bool is_u,
7158                                          int immh, int immb, int rn, int rd)
7159 {
7160     bool is_double = extract32(immh, 3, 1);
7161     int immhb = immh << 3 | immb;
7162     int fracbits = (is_double ? 128 : 64) - immhb;
7163     int pass;
7164     TCGv_ptr tcg_fpstatus;
7165     TCGv_i32 tcg_rmode, tcg_shift;
7166
7167     if (!extract32(immh, 2, 2)) {
7168         unallocated_encoding(s);
7169         return;
7170     }
7171
7172     if (!is_scalar && !is_q && is_double) {
7173         unallocated_encoding(s);
7174         return;
7175     }
7176
7177     if (!fp_access_check(s)) {
7178         return;
7179     }
7180
7181     assert(!(is_scalar && is_q));
7182
7183     tcg_rmode = tcg_const_i32(arm_rmode_to_sf(FPROUNDING_ZERO));
7184     tcg_fpstatus = get_fpstatus_ptr(false);
7185     gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
7186     tcg_shift = tcg_const_i32(fracbits);
7187
7188     if (is_double) {
7189         int maxpass = is_scalar ? 1 : 2;
7190
7191         for (pass = 0; pass < maxpass; pass++) {
7192             TCGv_i64 tcg_op = tcg_temp_new_i64();
7193
7194             read_vec_element(s, tcg_op, rn, pass, MO_64);
7195             if (is_u) {
7196                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
7197             } else {
7198                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
7199             }
7200             write_vec_element(s, tcg_op, rd, pass, MO_64);
7201             tcg_temp_free_i64(tcg_op);
7202         }
7203         clear_vec_high(s, is_q, rd);
7204     } else {
7205         int maxpass = is_scalar ? 1 : is_q ? 4 : 2;
7206         for (pass = 0; pass < maxpass; pass++) {
7207             TCGv_i32 tcg_op = tcg_temp_new_i32();
7208
7209             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7210             if (is_u) {
7211                 gen_helper_vfp_touls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
7212             } else {
7213                 gen_helper_vfp_tosls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
7214             }
7215             if (is_scalar) {
7216                 write_fp_sreg(s, rd, tcg_op);
7217             } else {
7218                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
7219             }
7220             tcg_temp_free_i32(tcg_op);
7221         }
7222         if (!is_scalar) {
7223             clear_vec_high(s, is_q, rd);
7224         }
7225     }
7226
7227     tcg_temp_free_ptr(tcg_fpstatus);
7228     tcg_temp_free_i32(tcg_shift);
7229     gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
7230     tcg_temp_free_i32(tcg_rmode);
7231 }
7232
7233 /* AdvSIMD scalar shift by immediate
7234  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
7235  * +-----+---+-------------+------+------+--------+---+------+------+
7236  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
7237  * +-----+---+-------------+------+------+--------+---+------+------+
7238  *
7239  * This is the scalar version so it works on a fixed sized registers
7240  */
7241 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
7242 {
7243     int rd = extract32(insn, 0, 5);
7244     int rn = extract32(insn, 5, 5);
7245     int opcode = extract32(insn, 11, 5);
7246     int immb = extract32(insn, 16, 3);
7247     int immh = extract32(insn, 19, 4);
7248     bool is_u = extract32(insn, 29, 1);
7249
7250     if (immh == 0) {
7251         unallocated_encoding(s);
7252         return;
7253     }
7254
7255     switch (opcode) {
7256     case 0x08: /* SRI */
7257         if (!is_u) {
7258             unallocated_encoding(s);
7259             return;
7260         }
7261         /* fall through */
7262     case 0x00: /* SSHR / USHR */
7263     case 0x02: /* SSRA / USRA */
7264     case 0x04: /* SRSHR / URSHR */
7265     case 0x06: /* SRSRA / URSRA */
7266         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
7267         break;
7268     case 0x0a: /* SHL / SLI */
7269         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
7270         break;
7271     case 0x1c: /* SCVTF, UCVTF */
7272         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
7273                                      opcode, rn, rd);
7274         break;
7275     case 0x10: /* SQSHRUN, SQSHRUN2 */
7276     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
7277         if (!is_u) {
7278             unallocated_encoding(s);
7279             return;
7280         }
7281         handle_vec_simd_sqshrn(s, true, false, false, true,
7282                                immh, immb, opcode, rn, rd);
7283         break;
7284     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
7285     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
7286         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
7287                                immh, immb, opcode, rn, rd);
7288         break;
7289     case 0xc: /* SQSHLU */
7290         if (!is_u) {
7291             unallocated_encoding(s);
7292             return;
7293         }
7294         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
7295         break;
7296     case 0xe: /* SQSHL, UQSHL */
7297         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
7298         break;
7299     case 0x1f: /* FCVTZS, FCVTZU */
7300         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
7301         break;
7302     default:
7303         unallocated_encoding(s);
7304         break;
7305     }
7306 }
7307
7308 /* AdvSIMD scalar three different
7309  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
7310  * +-----+---+-----------+------+---+------+--------+-----+------+------+
7311  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
7312  * +-----+---+-----------+------+---+------+--------+-----+------+------+
7313  */
7314 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
7315 {
7316     bool is_u = extract32(insn, 29, 1);
7317     int size = extract32(insn, 22, 2);
7318     int opcode = extract32(insn, 12, 4);
7319     int rm = extract32(insn, 16, 5);
7320     int rn = extract32(insn, 5, 5);
7321     int rd = extract32(insn, 0, 5);
7322
7323     if (is_u) {
7324         unallocated_encoding(s);
7325         return;
7326     }
7327
7328     switch (opcode) {
7329     case 0x9: /* SQDMLAL, SQDMLAL2 */
7330     case 0xb: /* SQDMLSL, SQDMLSL2 */
7331     case 0xd: /* SQDMULL, SQDMULL2 */
7332         if (size == 0 || size == 3) {
7333             unallocated_encoding(s);
7334             return;
7335         }
7336         break;
7337     default:
7338         unallocated_encoding(s);
7339         return;
7340     }
7341
7342     if (!fp_access_check(s)) {
7343         return;
7344     }
7345
7346     if (size == 2) {
7347         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7348         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7349         TCGv_i64 tcg_res = tcg_temp_new_i64();
7350
7351         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
7352         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
7353
7354         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
7355         gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
7356
7357         switch (opcode) {
7358         case 0xd: /* SQDMULL, SQDMULL2 */
7359             break;
7360         case 0xb: /* SQDMLSL, SQDMLSL2 */
7361             tcg_gen_neg_i64(tcg_res, tcg_res);
7362             /* fall through */
7363         case 0x9: /* SQDMLAL, SQDMLAL2 */
7364             read_vec_element(s, tcg_op1, rd, 0, MO_64);
7365             gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
7366                                               tcg_res, tcg_op1);
7367             break;
7368         default:
7369             g_assert_not_reached();
7370         }
7371
7372         write_fp_dreg(s, rd, tcg_res);
7373
7374         tcg_temp_free_i64(tcg_op1);
7375         tcg_temp_free_i64(tcg_op2);
7376         tcg_temp_free_i64(tcg_res);
7377     } else {
7378         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7379         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7380         TCGv_i64 tcg_res = tcg_temp_new_i64();
7381
7382         read_vec_element_i32(s, tcg_op1, rn, 0, MO_16);
7383         read_vec_element_i32(s, tcg_op2, rm, 0, MO_16);
7384
7385         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
7386         gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
7387
7388         switch (opcode) {
7389         case 0xd: /* SQDMULL, SQDMULL2 */
7390             break;
7391         case 0xb: /* SQDMLSL, SQDMLSL2 */
7392             gen_helper_neon_negl_u32(tcg_res, tcg_res);
7393             /* fall through */
7394         case 0x9: /* SQDMLAL, SQDMLAL2 */
7395         {
7396             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
7397             read_vec_element(s, tcg_op3, rd, 0, MO_32);
7398             gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
7399                                               tcg_res, tcg_op3);
7400             tcg_temp_free_i64(tcg_op3);
7401             break;
7402         }
7403         default:
7404             g_assert_not_reached();
7405         }
7406
7407         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7408         write_fp_dreg(s, rd, tcg_res);
7409
7410         tcg_temp_free_i32(tcg_op1);
7411         tcg_temp_free_i32(tcg_op2);
7412         tcg_temp_free_i64(tcg_res);
7413     }
7414 }
7415
7416 /* CMTST : test is "if (X & Y != 0)". */
7417 static void gen_cmtst_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
7418 {
7419     tcg_gen_and_i32(d, a, b);
7420     tcg_gen_setcondi_i32(TCG_COND_NE, d, d, 0);
7421     tcg_gen_neg_i32(d, d);
7422 }
7423
7424 static void gen_cmtst_i64(TCGv_i64 d, TCGv_i64 a, TCGv_i64 b)
7425 {
7426     tcg_gen_and_i64(d, a, b);
7427     tcg_gen_setcondi_i64(TCG_COND_NE, d, d, 0);
7428     tcg_gen_neg_i64(d, d);
7429 }
7430
7431 static void gen_cmtst_vec(unsigned vece, TCGv_vec d, TCGv_vec a, TCGv_vec b)
7432 {
7433     tcg_gen_and_vec(vece, d, a, b);
7434     tcg_gen_dupi_vec(vece, a, 0);
7435     tcg_gen_cmp_vec(TCG_COND_NE, vece, d, d, a);
7436 }
7437
7438 static void handle_3same_64(DisasContext *s, int opcode, bool u,
7439                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
7440 {
7441     /* Handle 64x64->64 opcodes which are shared between the scalar
7442      * and vector 3-same groups. We cover every opcode where size == 3
7443      * is valid in either the three-reg-same (integer, not pairwise)
7444      * or scalar-three-reg-same groups.
7445      */
7446     TCGCond cond;
7447
7448     switch (opcode) {
7449     case 0x1: /* SQADD */
7450         if (u) {
7451             gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7452         } else {
7453             gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7454         }
7455         break;
7456     case 0x5: /* SQSUB */
7457         if (u) {
7458             gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7459         } else {
7460             gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7461         }
7462         break;
7463     case 0x6: /* CMGT, CMHI */
7464         /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
7465          * We implement this using setcond (test) and then negating.
7466          */
7467         cond = u ? TCG_COND_GTU : TCG_COND_GT;
7468     do_cmop:
7469         tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
7470         tcg_gen_neg_i64(tcg_rd, tcg_rd);
7471         break;
7472     case 0x7: /* CMGE, CMHS */
7473         cond = u ? TCG_COND_GEU : TCG_COND_GE;
7474         goto do_cmop;
7475     case 0x11: /* CMTST, CMEQ */
7476         if (u) {
7477             cond = TCG_COND_EQ;
7478             goto do_cmop;
7479         }
7480         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
7481         break;
7482     case 0x8: /* SSHL, USHL */
7483         if (u) {
7484             gen_helper_neon_shl_u64(tcg_rd, tcg_rn, tcg_rm);
7485         } else {
7486             gen_helper_neon_shl_s64(tcg_rd, tcg_rn, tcg_rm);
7487         }
7488         break;
7489     case 0x9: /* SQSHL, UQSHL */
7490         if (u) {
7491             gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7492         } else {
7493             gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7494         }
7495         break;
7496     case 0xa: /* SRSHL, URSHL */
7497         if (u) {
7498             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
7499         } else {
7500             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
7501         }
7502         break;
7503     case 0xb: /* SQRSHL, UQRSHL */
7504         if (u) {
7505             gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7506         } else {
7507             gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7508         }
7509         break;
7510     case 0x10: /* ADD, SUB */
7511         if (u) {
7512             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
7513         } else {
7514             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
7515         }
7516         break;
7517     default:
7518         g_assert_not_reached();
7519     }
7520 }
7521
7522 /* Handle the 3-same-operands float operations; shared by the scalar
7523  * and vector encodings. The caller must filter out any encodings
7524  * not allocated for the encoding it is dealing with.
7525  */
7526 static void handle_3same_float(DisasContext *s, int size, int elements,
7527                                int fpopcode, int rd, int rn, int rm)
7528 {
7529     int pass;
7530     TCGv_ptr fpst = get_fpstatus_ptr(false);
7531
7532     for (pass = 0; pass < elements; pass++) {
7533         if (size) {
7534             /* Double */
7535             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7536             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7537             TCGv_i64 tcg_res = tcg_temp_new_i64();
7538
7539             read_vec_element(s, tcg_op1, rn, pass, MO_64);
7540             read_vec_element(s, tcg_op2, rm, pass, MO_64);
7541
7542             switch (fpopcode) {
7543             case 0x39: /* FMLS */
7544                 /* As usual for ARM, separate negation for fused multiply-add */
7545                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
7546                 /* fall through */
7547             case 0x19: /* FMLA */
7548                 read_vec_element(s, tcg_res, rd, pass, MO_64);
7549                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
7550                                        tcg_res, fpst);
7551                 break;
7552             case 0x18: /* FMAXNM */
7553                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7554                 break;
7555             case 0x1a: /* FADD */
7556                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
7557                 break;
7558             case 0x1b: /* FMULX */
7559                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
7560                 break;
7561             case 0x1c: /* FCMEQ */
7562                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7563                 break;
7564             case 0x1e: /* FMAX */
7565                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
7566                 break;
7567             case 0x1f: /* FRECPS */
7568                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7569                 break;
7570             case 0x38: /* FMINNM */
7571                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7572                 break;
7573             case 0x3a: /* FSUB */
7574                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
7575                 break;
7576             case 0x3e: /* FMIN */
7577                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
7578                 break;
7579             case 0x3f: /* FRSQRTS */
7580                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7581                 break;
7582             case 0x5b: /* FMUL */
7583                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
7584                 break;
7585             case 0x5c: /* FCMGE */
7586                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7587                 break;
7588             case 0x5d: /* FACGE */
7589                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7590                 break;
7591             case 0x5f: /* FDIV */
7592                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
7593                 break;
7594             case 0x7a: /* FABD */
7595                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
7596                 gen_helper_vfp_absd(tcg_res, tcg_res);
7597                 break;
7598             case 0x7c: /* FCMGT */
7599                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7600                 break;
7601             case 0x7d: /* FACGT */
7602                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7603                 break;
7604             default:
7605                 g_assert_not_reached();
7606             }
7607
7608             write_vec_element(s, tcg_res, rd, pass, MO_64);
7609
7610             tcg_temp_free_i64(tcg_res);
7611             tcg_temp_free_i64(tcg_op1);
7612             tcg_temp_free_i64(tcg_op2);
7613         } else {
7614             /* Single */
7615             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7616             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7617             TCGv_i32 tcg_res = tcg_temp_new_i32();
7618
7619             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
7620             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
7621
7622             switch (fpopcode) {
7623             case 0x39: /* FMLS */
7624                 /* As usual for ARM, separate negation for fused multiply-add */
7625                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
7626                 /* fall through */
7627             case 0x19: /* FMLA */
7628                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7629                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
7630                                        tcg_res, fpst);
7631                 break;
7632             case 0x1a: /* FADD */
7633                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
7634                 break;
7635             case 0x1b: /* FMULX */
7636                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
7637                 break;
7638             case 0x1c: /* FCMEQ */
7639                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7640                 break;
7641             case 0x1e: /* FMAX */
7642                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
7643                 break;
7644             case 0x1f: /* FRECPS */
7645                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7646                 break;
7647             case 0x18: /* FMAXNM */
7648                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
7649                 break;
7650             case 0x38: /* FMINNM */
7651                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
7652                 break;
7653             case 0x3a: /* FSUB */
7654                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
7655                 break;
7656             case 0x3e: /* FMIN */
7657                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
7658                 break;
7659             case 0x3f: /* FRSQRTS */
7660                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7661                 break;
7662             case 0x5b: /* FMUL */
7663                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
7664                 break;
7665             case 0x5c: /* FCMGE */
7666                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7667                 break;
7668             case 0x5d: /* FACGE */
7669                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7670                 break;
7671             case 0x5f: /* FDIV */
7672                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
7673                 break;
7674             case 0x7a: /* FABD */
7675                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
7676                 gen_helper_vfp_abss(tcg_res, tcg_res);
7677                 break;
7678             case 0x7c: /* FCMGT */
7679                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7680                 break;
7681             case 0x7d: /* FACGT */
7682                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7683                 break;
7684             default:
7685                 g_assert_not_reached();
7686             }
7687
7688             if (elements == 1) {
7689                 /* scalar single so clear high part */
7690                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7691
7692                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
7693                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
7694                 tcg_temp_free_i64(tcg_tmp);
7695             } else {
7696                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7697             }
7698
7699             tcg_temp_free_i32(tcg_res);
7700             tcg_temp_free_i32(tcg_op1);
7701             tcg_temp_free_i32(tcg_op2);
7702         }
7703     }
7704
7705     tcg_temp_free_ptr(fpst);
7706
7707     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
7708 }
7709
7710 /* AdvSIMD scalar three same
7711  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
7712  * +-----+---+-----------+------+---+------+--------+---+------+------+
7713  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
7714  * +-----+---+-----------+------+---+------+--------+---+------+------+
7715  */
7716 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
7717 {
7718     int rd = extract32(insn, 0, 5);
7719     int rn = extract32(insn, 5, 5);
7720     int opcode = extract32(insn, 11, 5);
7721     int rm = extract32(insn, 16, 5);
7722     int size = extract32(insn, 22, 2);
7723     bool u = extract32(insn, 29, 1);
7724     TCGv_i64 tcg_rd;
7725
7726     if (opcode >= 0x18) {
7727         /* Floating point: U, size[1] and opcode indicate operation */
7728         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
7729         switch (fpopcode) {
7730         case 0x1b: /* FMULX */
7731         case 0x1f: /* FRECPS */
7732         case 0x3f: /* FRSQRTS */
7733         case 0x5d: /* FACGE */
7734         case 0x7d: /* FACGT */
7735         case 0x1c: /* FCMEQ */
7736         case 0x5c: /* FCMGE */
7737         case 0x7c: /* FCMGT */
7738         case 0x7a: /* FABD */
7739             break;
7740         default:
7741             unallocated_encoding(s);
7742             return;
7743         }
7744
7745         if (!fp_access_check(s)) {
7746             return;
7747         }
7748
7749         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
7750         return;
7751     }
7752
7753     switch (opcode) {
7754     case 0x1: /* SQADD, UQADD */
7755     case 0x5: /* SQSUB, UQSUB */
7756     case 0x9: /* SQSHL, UQSHL */
7757     case 0xb: /* SQRSHL, UQRSHL */
7758         break;
7759     case 0x8: /* SSHL, USHL */
7760     case 0xa: /* SRSHL, URSHL */
7761     case 0x6: /* CMGT, CMHI */
7762     case 0x7: /* CMGE, CMHS */
7763     case 0x11: /* CMTST, CMEQ */
7764     case 0x10: /* ADD, SUB (vector) */
7765         if (size != 3) {
7766             unallocated_encoding(s);
7767             return;
7768         }
7769         break;
7770     case 0x16: /* SQDMULH, SQRDMULH (vector) */
7771         if (size != 1 && size != 2) {
7772             unallocated_encoding(s);
7773             return;
7774         }
7775         break;
7776     default:
7777         unallocated_encoding(s);
7778         return;
7779     }
7780
7781     if (!fp_access_check(s)) {
7782         return;
7783     }
7784
7785     tcg_rd = tcg_temp_new_i64();
7786
7787     if (size == 3) {
7788         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
7789         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
7790
7791         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
7792         tcg_temp_free_i64(tcg_rn);
7793         tcg_temp_free_i64(tcg_rm);
7794     } else {
7795         /* Do a single operation on the lowest element in the vector.
7796          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
7797          * no side effects for all these operations.
7798          * OPTME: special-purpose helpers would avoid doing some
7799          * unnecessary work in the helper for the 8 and 16 bit cases.
7800          */
7801         NeonGenTwoOpEnvFn *genenvfn;
7802         TCGv_i32 tcg_rn = tcg_temp_new_i32();
7803         TCGv_i32 tcg_rm = tcg_temp_new_i32();
7804         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
7805
7806         read_vec_element_i32(s, tcg_rn, rn, 0, size);
7807         read_vec_element_i32(s, tcg_rm, rm, 0, size);
7808
7809         switch (opcode) {
7810         case 0x1: /* SQADD, UQADD */
7811         {
7812             static NeonGenTwoOpEnvFn * const fns[3][2] = {
7813                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
7814                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
7815                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
7816             };
7817             genenvfn = fns[size][u];
7818             break;
7819         }
7820         case 0x5: /* SQSUB, UQSUB */
7821         {
7822             static NeonGenTwoOpEnvFn * const fns[3][2] = {
7823                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
7824                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
7825                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
7826             };
7827             genenvfn = fns[size][u];
7828             break;
7829         }
7830         case 0x9: /* SQSHL, UQSHL */
7831         {
7832             static NeonGenTwoOpEnvFn * const fns[3][2] = {
7833                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
7834                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
7835                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
7836             };
7837             genenvfn = fns[size][u];
7838             break;
7839         }
7840         case 0xb: /* SQRSHL, UQRSHL */
7841         {
7842             static NeonGenTwoOpEnvFn * const fns[3][2] = {
7843                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
7844                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
7845                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
7846             };
7847             genenvfn = fns[size][u];
7848             break;
7849         }
7850         case 0x16: /* SQDMULH, SQRDMULH */
7851         {
7852             static NeonGenTwoOpEnvFn * const fns[2][2] = {
7853                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
7854                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
7855             };
7856             assert(size == 1 || size == 2);
7857             genenvfn = fns[size - 1][u];
7858             break;
7859         }
7860         default:
7861             g_assert_not_reached();
7862         }
7863
7864         genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
7865         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
7866         tcg_temp_free_i32(tcg_rd32);
7867         tcg_temp_free_i32(tcg_rn);
7868         tcg_temp_free_i32(tcg_rm);
7869     }
7870
7871     write_fp_dreg(s, rd, tcg_rd);
7872
7873     tcg_temp_free_i64(tcg_rd);
7874 }
7875
7876 /* AdvSIMD scalar three same FP16
7877  *  31 30  29 28       24 23  22 21 20  16 15 14 13    11 10  9  5 4  0
7878  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
7879  * | 0 1 | U | 1 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 | Rn | Rd |
7880  * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
7881  * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
7882  * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
7883  */
7884 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
7885                                                   uint32_t insn)
7886 {
7887     int rd = extract32(insn, 0, 5);
7888     int rn = extract32(insn, 5, 5);
7889     int opcode = extract32(insn, 11, 3);
7890     int rm = extract32(insn, 16, 5);
7891     bool u = extract32(insn, 29, 1);
7892     bool a = extract32(insn, 23, 1);
7893     int fpopcode = opcode | (a << 3) |  (u << 4);
7894     TCGv_ptr fpst;
7895     TCGv_i32 tcg_op1;
7896     TCGv_i32 tcg_op2;
7897     TCGv_i32 tcg_res;
7898
7899     switch (fpopcode) {
7900     case 0x03: /* FMULX */
7901     case 0x04: /* FCMEQ (reg) */
7902     case 0x07: /* FRECPS */
7903     case 0x0f: /* FRSQRTS */
7904     case 0x14: /* FCMGE (reg) */
7905     case 0x15: /* FACGE */
7906     case 0x1a: /* FABD */
7907     case 0x1c: /* FCMGT (reg) */
7908     case 0x1d: /* FACGT */
7909         break;
7910     default:
7911         unallocated_encoding(s);
7912         return;
7913     }
7914
7915     if (!arm_dc_feature(s, ARM_FEATURE_V8_FP16)) {
7916         unallocated_encoding(s);
7917     }
7918
7919     if (!fp_access_check(s)) {
7920         return;
7921     }
7922
7923     fpst = get_fpstatus_ptr(true);
7924
7925     tcg_op1 = tcg_temp_new_i32();
7926     tcg_op2 = tcg_temp_new_i32();
7927     tcg_res = tcg_temp_new_i32();
7928
7929     read_vec_element_i32(s, tcg_op1, rn, 0, MO_16);
7930     read_vec_element_i32(s, tcg_op2, rm, 0, MO_16);
7931
7932     switch (fpopcode) {
7933     case 0x03: /* FMULX */
7934         gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
7935         break;
7936     case 0x04: /* FCMEQ (reg) */
7937         gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
7938         break;
7939     case 0x07: /* FRECPS */
7940         gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
7941         break;
7942     case 0x0f: /* FRSQRTS */
7943         gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
7944         break;
7945     case 0x14: /* FCMGE (reg) */
7946         gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
7947         break;
7948     case 0x15: /* FACGE */
7949         gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
7950         break;
7951     case 0x1a: /* FABD */
7952         gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
7953         tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
7954         break;
7955     case 0x1c: /* FCMGT (reg) */
7956         gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
7957         break;
7958     case 0x1d: /* FACGT */
7959         gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
7960         break;
7961     default:
7962         g_assert_not_reached();
7963     }
7964
7965     write_fp_sreg(s, rd, tcg_res);
7966
7967
7968     tcg_temp_free_i32(tcg_res);
7969     tcg_temp_free_i32(tcg_op1);
7970     tcg_temp_free_i32(tcg_op2);
7971     tcg_temp_free_ptr(fpst);
7972 }
7973
7974 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
7975                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
7976                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
7977 {
7978     /* Handle 64->64 opcodes which are shared between the scalar and
7979      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
7980      * is valid in either group and also the double-precision fp ops.
7981      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
7982      * requires them.
7983      */
7984     TCGCond cond;
7985
7986     switch (opcode) {
7987     case 0x4: /* CLS, CLZ */
7988         if (u) {
7989             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
7990         } else {
7991             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
7992         }
7993         break;
7994     case 0x5: /* NOT */
7995         /* This opcode is shared with CNT and RBIT but we have earlier
7996          * enforced that size == 3 if and only if this is the NOT insn.
7997          */
7998         tcg_gen_not_i64(tcg_rd, tcg_rn);
7999         break;
8000     case 0x7: /* SQABS, SQNEG */
8001         if (u) {
8002             gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
8003         } else {
8004             gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
8005         }
8006         break;
8007     case 0xa: /* CMLT */
8008         /* 64 bit integer comparison against zero, result is
8009          * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
8010          * subtracting 1.
8011          */
8012         cond = TCG_COND_LT;
8013     do_cmop:
8014         tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
8015         tcg_gen_neg_i64(tcg_rd, tcg_rd);
8016         break;
8017     case 0x8: /* CMGT, CMGE */
8018         cond = u ? TCG_COND_GE : TCG_COND_GT;
8019         goto do_cmop;
8020     case 0x9: /* CMEQ, CMLE */
8021         cond = u ? TCG_COND_LE : TCG_COND_EQ;
8022         goto do_cmop;
8023     case 0xb: /* ABS, NEG */
8024         if (u) {
8025             tcg_gen_neg_i64(tcg_rd, tcg_rn);
8026         } else {
8027             TCGv_i64 tcg_zero = tcg_const_i64(0);
8028             tcg_gen_neg_i64(tcg_rd, tcg_rn);
8029             tcg_gen_movcond_i64(TCG_COND_GT, tcg_rd, tcg_rn, tcg_zero,
8030                                 tcg_rn, tcg_rd);
8031             tcg_temp_free_i64(tcg_zero);
8032         }
8033         break;
8034     case 0x2f: /* FABS */
8035         gen_helper_vfp_absd(tcg_rd, tcg_rn);
8036         break;
8037     case 0x6f: /* FNEG */
8038         gen_helper_vfp_negd(tcg_rd, tcg_rn);
8039         break;
8040     case 0x7f: /* FSQRT */
8041         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
8042         break;
8043     case 0x1a: /* FCVTNS */
8044     case 0x1b: /* FCVTMS */
8045     case 0x1c: /* FCVTAS */
8046     case 0x3a: /* FCVTPS */
8047     case 0x3b: /* FCVTZS */
8048     {
8049         TCGv_i32 tcg_shift = tcg_const_i32(0);
8050         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
8051         tcg_temp_free_i32(tcg_shift);
8052         break;
8053     }
8054     case 0x5a: /* FCVTNU */
8055     case 0x5b: /* FCVTMU */
8056     case 0x5c: /* FCVTAU */
8057     case 0x7a: /* FCVTPU */
8058     case 0x7b: /* FCVTZU */
8059     {
8060         TCGv_i32 tcg_shift = tcg_const_i32(0);
8061         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
8062         tcg_temp_free_i32(tcg_shift);
8063         break;
8064     }
8065     case 0x18: /* FRINTN */
8066     case 0x19: /* FRINTM */
8067     case 0x38: /* FRINTP */
8068     case 0x39: /* FRINTZ */
8069     case 0x58: /* FRINTA */
8070     case 0x79: /* FRINTI */
8071         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
8072         break;
8073     case 0x59: /* FRINTX */
8074         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
8075         break;
8076     default:
8077         g_assert_not_reached();
8078     }
8079 }
8080
8081 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
8082                                    bool is_scalar, bool is_u, bool is_q,
8083                                    int size, int rn, int rd)
8084 {
8085     bool is_double = (size == MO_64);
8086     TCGv_ptr fpst;
8087
8088     if (!fp_access_check(s)) {
8089         return;
8090     }
8091
8092     fpst = get_fpstatus_ptr(size == MO_16);
8093
8094     if (is_double) {
8095         TCGv_i64 tcg_op = tcg_temp_new_i64();
8096         TCGv_i64 tcg_zero = tcg_const_i64(0);
8097         TCGv_i64 tcg_res = tcg_temp_new_i64();
8098         NeonGenTwoDoubleOPFn *genfn;
8099         bool swap = false;
8100         int pass;
8101
8102         switch (opcode) {
8103         case 0x2e: /* FCMLT (zero) */
8104             swap = true;
8105             /* fallthrough */
8106         case 0x2c: /* FCMGT (zero) */
8107             genfn = gen_helper_neon_cgt_f64;
8108             break;
8109         case 0x2d: /* FCMEQ (zero) */
8110             genfn = gen_helper_neon_ceq_f64;
8111             break;
8112         case 0x6d: /* FCMLE (zero) */
8113             swap = true;
8114             /* fall through */
8115         case 0x6c: /* FCMGE (zero) */
8116             genfn = gen_helper_neon_cge_f64;
8117             break;
8118         default:
8119             g_assert_not_reached();
8120         }
8121
8122         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
8123             read_vec_element(s, tcg_op, rn, pass, MO_64);
8124             if (swap) {
8125                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
8126             } else {
8127                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
8128             }
8129             write_vec_element(s, tcg_res, rd, pass, MO_64);
8130         }
8131         tcg_temp_free_i64(tcg_res);
8132         tcg_temp_free_i64(tcg_zero);
8133         tcg_temp_free_i64(tcg_op);
8134
8135         clear_vec_high(s, !is_scalar, rd);
8136     } else {
8137         TCGv_i32 tcg_op = tcg_temp_new_i32();
8138         TCGv_i32 tcg_zero = tcg_const_i32(0);
8139         TCGv_i32 tcg_res = tcg_temp_new_i32();
8140         NeonGenTwoSingleOPFn *genfn;
8141         bool swap = false;
8142         int pass, maxpasses;
8143
8144         if (size == MO_16) {
8145             switch (opcode) {
8146             case 0x2e: /* FCMLT (zero) */
8147                 swap = true;
8148                 /* fall through */
8149             case 0x2c: /* FCMGT (zero) */
8150                 genfn = gen_helper_advsimd_cgt_f16;
8151                 break;
8152             case 0x2d: /* FCMEQ (zero) */
8153                 genfn = gen_helper_advsimd_ceq_f16;
8154                 break;
8155             case 0x6d: /* FCMLE (zero) */
8156                 swap = true;
8157                 /* fall through */
8158             case 0x6c: /* FCMGE (zero) */
8159                 genfn = gen_helper_advsimd_cge_f16;
8160                 break;
8161             default:
8162                 g_assert_not_reached();
8163             }
8164         } else {
8165             switch (opcode) {
8166             case 0x2e: /* FCMLT (zero) */
8167                 swap = true;
8168                 /* fall through */
8169             case 0x2c: /* FCMGT (zero) */
8170                 genfn = gen_helper_neon_cgt_f32;
8171                 break;
8172             case 0x2d: /* FCMEQ (zero) */
8173                 genfn = gen_helper_neon_ceq_f32;
8174                 break;
8175             case 0x6d: /* FCMLE (zero) */
8176                 swap = true;
8177                 /* fall through */
8178             case 0x6c: /* FCMGE (zero) */
8179                 genfn = gen_helper_neon_cge_f32;
8180                 break;
8181             default:
8182                 g_assert_not_reached();
8183             }
8184         }
8185
8186         if (is_scalar) {
8187             maxpasses = 1;
8188         } else {
8189             int vector_size = 8 << is_q;
8190             maxpasses = vector_size >> size;
8191         }
8192
8193         for (pass = 0; pass < maxpasses; pass++) {
8194             read_vec_element_i32(s, tcg_op, rn, pass, size);
8195             if (swap) {
8196                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
8197             } else {
8198                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
8199             }
8200             if (is_scalar) {
8201                 write_fp_sreg(s, rd, tcg_res);
8202             } else {
8203                 write_vec_element_i32(s, tcg_res, rd, pass, size);
8204             }
8205         }
8206         tcg_temp_free_i32(tcg_res);
8207         tcg_temp_free_i32(tcg_zero);
8208         tcg_temp_free_i32(tcg_op);
8209         if (!is_scalar) {
8210             clear_vec_high(s, is_q, rd);
8211         }
8212     }
8213
8214     tcg_temp_free_ptr(fpst);
8215 }
8216
8217 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
8218                                     bool is_scalar, bool is_u, bool is_q,
8219                                     int size, int rn, int rd)
8220 {
8221     bool is_double = (size == 3);
8222     TCGv_ptr fpst = get_fpstatus_ptr(false);
8223
8224     if (is_double) {
8225         TCGv_i64 tcg_op = tcg_temp_new_i64();
8226         TCGv_i64 tcg_res = tcg_temp_new_i64();
8227         int pass;
8228
8229         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
8230             read_vec_element(s, tcg_op, rn, pass, MO_64);
8231             switch (opcode) {
8232             case 0x3d: /* FRECPE */
8233                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
8234                 break;
8235             case 0x3f: /* FRECPX */
8236                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
8237                 break;
8238             case 0x7d: /* FRSQRTE */
8239                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
8240                 break;
8241             default:
8242                 g_assert_not_reached();
8243             }
8244             write_vec_element(s, tcg_res, rd, pass, MO_64);
8245         }
8246         tcg_temp_free_i64(tcg_res);
8247         tcg_temp_free_i64(tcg_op);
8248         clear_vec_high(s, !is_scalar, rd);
8249     } else {
8250         TCGv_i32 tcg_op = tcg_temp_new_i32();
8251         TCGv_i32 tcg_res = tcg_temp_new_i32();
8252         int pass, maxpasses;
8253
8254         if (is_scalar) {
8255             maxpasses = 1;
8256         } else {
8257             maxpasses = is_q ? 4 : 2;
8258         }
8259
8260         for (pass = 0; pass < maxpasses; pass++) {
8261             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
8262
8263             switch (opcode) {
8264             case 0x3c: /* URECPE */
8265                 gen_helper_recpe_u32(tcg_res, tcg_op, fpst);
8266                 break;
8267             case 0x3d: /* FRECPE */
8268                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
8269                 break;
8270             case 0x3f: /* FRECPX */
8271                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
8272                 break;
8273             case 0x7d: /* FRSQRTE */
8274                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
8275                 break;
8276             default:
8277                 g_assert_not_reached();
8278             }
8279
8280             if (is_scalar) {
8281                 write_fp_sreg(s, rd, tcg_res);
8282             } else {
8283                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
8284             }
8285         }
8286         tcg_temp_free_i32(tcg_res);
8287         tcg_temp_free_i32(tcg_op);
8288         if (!is_scalar) {
8289             clear_vec_high(s, is_q, rd);
8290         }
8291     }
8292     tcg_temp_free_ptr(fpst);
8293 }
8294
8295 static void handle_2misc_narrow(DisasContext *s, bool scalar,
8296                                 int opcode, bool u, bool is_q,
8297                                 int size, int rn, int rd)
8298 {
8299     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
8300      * in the source becomes a size element in the destination).
8301      */
8302     int pass;
8303     TCGv_i32 tcg_res[2];
8304     int destelt = is_q ? 2 : 0;
8305     int passes = scalar ? 1 : 2;
8306
8307     if (scalar) {
8308         tcg_res[1] = tcg_const_i32(0);
8309     }
8310
8311     for (pass = 0; pass < passes; pass++) {
8312         TCGv_i64 tcg_op = tcg_temp_new_i64();
8313         NeonGenNarrowFn *genfn = NULL;
8314         NeonGenNarrowEnvFn *genenvfn = NULL;
8315
8316         if (scalar) {
8317             read_vec_element(s, tcg_op, rn, pass, size + 1);
8318         } else {
8319             read_vec_element(s, tcg_op, rn, pass, MO_64);
8320         }
8321         tcg_res[pass] = tcg_temp_new_i32();
8322
8323         switch (opcode) {
8324         case 0x12: /* XTN, SQXTUN */
8325         {
8326             static NeonGenNarrowFn * const xtnfns[3] = {
8327                 gen_helper_neon_narrow_u8,
8328                 gen_helper_neon_narrow_u16,
8329                 tcg_gen_extrl_i64_i32,
8330             };
8331             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
8332                 gen_helper_neon_unarrow_sat8,
8333                 gen_helper_neon_unarrow_sat16,
8334                 gen_helper_neon_unarrow_sat32,
8335             };
8336             if (u) {
8337                 genenvfn = sqxtunfns[size];
8338             } else {
8339                 genfn = xtnfns[size];
8340             }
8341             break;
8342         }
8343         case 0x14: /* SQXTN, UQXTN */
8344         {
8345             static NeonGenNarrowEnvFn * const fns[3][2] = {
8346                 { gen_helper_neon_narrow_sat_s8,
8347                   gen_helper_neon_narrow_sat_u8 },
8348                 { gen_helper_neon_narrow_sat_s16,
8349                   gen_helper_neon_narrow_sat_u16 },
8350                 { gen_helper_neon_narrow_sat_s32,
8351                   gen_helper_neon_narrow_sat_u32 },
8352             };
8353             genenvfn = fns[size][u];
8354             break;
8355         }
8356         case 0x16: /* FCVTN, FCVTN2 */
8357             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
8358             if (size == 2) {
8359                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
8360             } else {
8361                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
8362                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
8363                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
8364                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, cpu_env);
8365                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, cpu_env);
8366                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
8367                 tcg_temp_free_i32(tcg_lo);
8368                 tcg_temp_free_i32(tcg_hi);
8369             }
8370             break;
8371         case 0x56:  /* FCVTXN, FCVTXN2 */
8372             /* 64 bit to 32 bit float conversion
8373              * with von Neumann rounding (round to odd)
8374              */
8375             assert(size == 2);
8376             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
8377             break;
8378         default:
8379             g_assert_not_reached();
8380         }
8381
8382         if (genfn) {
8383             genfn(tcg_res[pass], tcg_op);
8384         } else if (genenvfn) {
8385             genenvfn(tcg_res[pass], cpu_env, tcg_op);
8386         }
8387
8388         tcg_temp_free_i64(tcg_op);
8389     }
8390
8391     for (pass = 0; pass < 2; pass++) {
8392         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
8393         tcg_temp_free_i32(tcg_res[pass]);
8394     }
8395     clear_vec_high(s, is_q, rd);
8396 }
8397
8398 /* Remaining saturating accumulating ops */
8399 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
8400                                 bool is_q, int size, int rn, int rd)
8401 {
8402     bool is_double = (size == 3);
8403
8404     if (is_double) {
8405         TCGv_i64 tcg_rn = tcg_temp_new_i64();
8406         TCGv_i64 tcg_rd = tcg_temp_new_i64();
8407         int pass;
8408
8409         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
8410             read_vec_element(s, tcg_rn, rn, pass, MO_64);
8411             read_vec_element(s, tcg_rd, rd, pass, MO_64);
8412
8413             if (is_u) { /* USQADD */
8414                 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8415             } else { /* SUQADD */
8416                 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8417             }
8418             write_vec_element(s, tcg_rd, rd, pass, MO_64);
8419         }
8420         tcg_temp_free_i64(tcg_rd);
8421         tcg_temp_free_i64(tcg_rn);
8422         clear_vec_high(s, !is_scalar, rd);
8423     } else {
8424         TCGv_i32 tcg_rn = tcg_temp_new_i32();
8425         TCGv_i32 tcg_rd = tcg_temp_new_i32();
8426         int pass, maxpasses;
8427
8428         if (is_scalar) {
8429             maxpasses = 1;
8430         } else {
8431             maxpasses = is_q ? 4 : 2;
8432         }
8433
8434         for (pass = 0; pass < maxpasses; pass++) {
8435             if (is_scalar) {
8436                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
8437                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
8438             } else {
8439                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
8440                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
8441             }
8442
8443             if (is_u) { /* USQADD */
8444                 switch (size) {
8445                 case 0:
8446                     gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8447                     break;
8448                 case 1:
8449                     gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8450                     break;
8451                 case 2:
8452                     gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8453                     break;
8454                 default:
8455                     g_assert_not_reached();
8456                 }
8457             } else { /* SUQADD */
8458                 switch (size) {
8459                 case 0:
8460                     gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8461                     break;
8462                 case 1:
8463                     gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8464                     break;
8465                 case 2:
8466                     gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8467                     break;
8468                 default:
8469                     g_assert_not_reached();
8470                 }
8471             }
8472
8473             if (is_scalar) {
8474                 TCGv_i64 tcg_zero = tcg_const_i64(0);
8475                 write_vec_element(s, tcg_zero, rd, 0, MO_64);
8476                 tcg_temp_free_i64(tcg_zero);
8477             }
8478             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
8479         }
8480         tcg_temp_free_i32(tcg_rd);
8481         tcg_temp_free_i32(tcg_rn);
8482         clear_vec_high(s, is_q, rd);
8483     }
8484 }
8485
8486 /* AdvSIMD scalar two reg misc
8487  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
8488  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8489  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
8490  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8491  */
8492 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
8493 {
8494     int rd = extract32(insn, 0, 5);
8495     int rn = extract32(insn, 5, 5);
8496     int opcode = extract32(insn, 12, 5);
8497     int size = extract32(insn, 22, 2);
8498     bool u = extract32(insn, 29, 1);
8499     bool is_fcvt = false;
8500     int rmode;
8501     TCGv_i32 tcg_rmode;
8502     TCGv_ptr tcg_fpstatus;
8503
8504     switch (opcode) {
8505     case 0x3: /* USQADD / SUQADD*/
8506         if (!fp_access_check(s)) {
8507             return;
8508         }
8509         handle_2misc_satacc(s, true, u, false, size, rn, rd);
8510         return;
8511     case 0x7: /* SQABS / SQNEG */
8512         break;
8513     case 0xa: /* CMLT */
8514         if (u) {
8515             unallocated_encoding(s);
8516             return;
8517         }
8518         /* fall through */
8519     case 0x8: /* CMGT, CMGE */
8520     case 0x9: /* CMEQ, CMLE */
8521     case 0xb: /* ABS, NEG */
8522         if (size != 3) {
8523             unallocated_encoding(s);
8524             return;
8525         }
8526         break;
8527     case 0x12: /* SQXTUN */
8528         if (!u) {
8529             unallocated_encoding(s);
8530             return;
8531         }
8532         /* fall through */
8533     case 0x14: /* SQXTN, UQXTN */
8534         if (size == 3) {
8535             unallocated_encoding(s);
8536             return;
8537         }
8538         if (!fp_access_check(s)) {
8539             return;
8540         }
8541         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
8542         return;
8543     case 0xc ... 0xf:
8544     case 0x16 ... 0x1d:
8545     case 0x1f:
8546         /* Floating point: U, size[1] and opcode indicate operation;
8547          * size[0] indicates single or double precision.
8548          */
8549         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
8550         size = extract32(size, 0, 1) ? 3 : 2;
8551         switch (opcode) {
8552         case 0x2c: /* FCMGT (zero) */
8553         case 0x2d: /* FCMEQ (zero) */
8554         case 0x2e: /* FCMLT (zero) */
8555         case 0x6c: /* FCMGE (zero) */
8556         case 0x6d: /* FCMLE (zero) */
8557             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
8558             return;
8559         case 0x1d: /* SCVTF */
8560         case 0x5d: /* UCVTF */
8561         {
8562             bool is_signed = (opcode == 0x1d);
8563             if (!fp_access_check(s)) {
8564                 return;
8565             }
8566             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
8567             return;
8568         }
8569         case 0x3d: /* FRECPE */
8570         case 0x3f: /* FRECPX */
8571         case 0x7d: /* FRSQRTE */
8572             if (!fp_access_check(s)) {
8573                 return;
8574             }
8575             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
8576             return;
8577         case 0x1a: /* FCVTNS */
8578         case 0x1b: /* FCVTMS */
8579         case 0x3a: /* FCVTPS */
8580         case 0x3b: /* FCVTZS */
8581         case 0x5a: /* FCVTNU */
8582         case 0x5b: /* FCVTMU */
8583         case 0x7a: /* FCVTPU */
8584         case 0x7b: /* FCVTZU */
8585             is_fcvt = true;
8586             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
8587             break;
8588         case 0x1c: /* FCVTAS */
8589         case 0x5c: /* FCVTAU */
8590             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
8591             is_fcvt = true;
8592             rmode = FPROUNDING_TIEAWAY;
8593             break;
8594         case 0x56: /* FCVTXN, FCVTXN2 */
8595             if (size == 2) {
8596                 unallocated_encoding(s);
8597                 return;
8598             }
8599             if (!fp_access_check(s)) {
8600                 return;
8601             }
8602             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
8603             return;
8604         default:
8605             unallocated_encoding(s);
8606             return;
8607         }
8608         break;
8609     default:
8610         unallocated_encoding(s);
8611         return;
8612     }
8613
8614     if (!fp_access_check(s)) {
8615         return;
8616     }
8617
8618     if (is_fcvt) {
8619         tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
8620         tcg_fpstatus = get_fpstatus_ptr(false);
8621         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
8622     } else {
8623         tcg_rmode = NULL;
8624         tcg_fpstatus = NULL;
8625     }
8626
8627     if (size == 3) {
8628         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
8629         TCGv_i64 tcg_rd = tcg_temp_new_i64();
8630
8631         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
8632         write_fp_dreg(s, rd, tcg_rd);
8633         tcg_temp_free_i64(tcg_rd);
8634         tcg_temp_free_i64(tcg_rn);
8635     } else {
8636         TCGv_i32 tcg_rn = tcg_temp_new_i32();
8637         TCGv_i32 tcg_rd = tcg_temp_new_i32();
8638
8639         read_vec_element_i32(s, tcg_rn, rn, 0, size);
8640
8641         switch (opcode) {
8642         case 0x7: /* SQABS, SQNEG */
8643         {
8644             NeonGenOneOpEnvFn *genfn;
8645             static NeonGenOneOpEnvFn * const fns[3][2] = {
8646                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
8647                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
8648                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
8649             };
8650             genfn = fns[size][u];
8651             genfn(tcg_rd, cpu_env, tcg_rn);
8652             break;
8653         }
8654         case 0x1a: /* FCVTNS */
8655         case 0x1b: /* FCVTMS */
8656         case 0x1c: /* FCVTAS */
8657         case 0x3a: /* FCVTPS */
8658         case 0x3b: /* FCVTZS */
8659         {
8660             TCGv_i32 tcg_shift = tcg_const_i32(0);
8661             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
8662             tcg_temp_free_i32(tcg_shift);
8663             break;
8664         }
8665         case 0x5a: /* FCVTNU */
8666         case 0x5b: /* FCVTMU */
8667         case 0x5c: /* FCVTAU */
8668         case 0x7a: /* FCVTPU */
8669         case 0x7b: /* FCVTZU */
8670         {
8671             TCGv_i32 tcg_shift = tcg_const_i32(0);
8672             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
8673             tcg_temp_free_i32(tcg_shift);
8674             break;
8675         }
8676         default:
8677             g_assert_not_reached();
8678         }
8679
8680         write_fp_sreg(s, rd, tcg_rd);
8681         tcg_temp_free_i32(tcg_rd);
8682         tcg_temp_free_i32(tcg_rn);
8683     }
8684
8685     if (is_fcvt) {
8686         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
8687         tcg_temp_free_i32(tcg_rmode);
8688         tcg_temp_free_ptr(tcg_fpstatus);
8689     }
8690 }
8691
8692 static void gen_ssra8_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8693 {
8694     tcg_gen_vec_sar8i_i64(a, a, shift);
8695     tcg_gen_vec_add8_i64(d, d, a);
8696 }
8697
8698 static void gen_ssra16_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8699 {
8700     tcg_gen_vec_sar16i_i64(a, a, shift);
8701     tcg_gen_vec_add16_i64(d, d, a);
8702 }
8703
8704 static void gen_ssra32_i32(TCGv_i32 d, TCGv_i32 a, int32_t shift)
8705 {
8706     tcg_gen_sari_i32(a, a, shift);
8707     tcg_gen_add_i32(d, d, a);
8708 }
8709
8710 static void gen_ssra64_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8711 {
8712     tcg_gen_sari_i64(a, a, shift);
8713     tcg_gen_add_i64(d, d, a);
8714 }
8715
8716 static void gen_ssra_vec(unsigned vece, TCGv_vec d, TCGv_vec a, int64_t sh)
8717 {
8718     tcg_gen_sari_vec(vece, a, a, sh);
8719     tcg_gen_add_vec(vece, d, d, a);
8720 }
8721
8722 static void gen_usra8_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8723 {
8724     tcg_gen_vec_shr8i_i64(a, a, shift);
8725     tcg_gen_vec_add8_i64(d, d, a);
8726 }
8727
8728 static void gen_usra16_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8729 {
8730     tcg_gen_vec_shr16i_i64(a, a, shift);
8731     tcg_gen_vec_add16_i64(d, d, a);
8732 }
8733
8734 static void gen_usra32_i32(TCGv_i32 d, TCGv_i32 a, int32_t shift)
8735 {
8736     tcg_gen_shri_i32(a, a, shift);
8737     tcg_gen_add_i32(d, d, a);
8738 }
8739
8740 static void gen_usra64_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8741 {
8742     tcg_gen_shri_i64(a, a, shift);
8743     tcg_gen_add_i64(d, d, a);
8744 }
8745
8746 static void gen_usra_vec(unsigned vece, TCGv_vec d, TCGv_vec a, int64_t sh)
8747 {
8748     tcg_gen_shri_vec(vece, a, a, sh);
8749     tcg_gen_add_vec(vece, d, d, a);
8750 }
8751
8752 static void gen_shr8_ins_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8753 {
8754     uint64_t mask = dup_const(MO_8, 0xff >> shift);
8755     TCGv_i64 t = tcg_temp_new_i64();
8756
8757     tcg_gen_shri_i64(t, a, shift);
8758     tcg_gen_andi_i64(t, t, mask);
8759     tcg_gen_andi_i64(d, d, ~mask);
8760     tcg_gen_or_i64(d, d, t);
8761     tcg_temp_free_i64(t);
8762 }
8763
8764 static void gen_shr16_ins_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8765 {
8766     uint64_t mask = dup_const(MO_16, 0xffff >> shift);
8767     TCGv_i64 t = tcg_temp_new_i64();
8768
8769     tcg_gen_shri_i64(t, a, shift);
8770     tcg_gen_andi_i64(t, t, mask);
8771     tcg_gen_andi_i64(d, d, ~mask);
8772     tcg_gen_or_i64(d, d, t);
8773     tcg_temp_free_i64(t);
8774 }
8775
8776 static void gen_shr32_ins_i32(TCGv_i32 d, TCGv_i32 a, int32_t shift)
8777 {
8778     tcg_gen_shri_i32(a, a, shift);
8779     tcg_gen_deposit_i32(d, d, a, 0, 32 - shift);
8780 }
8781
8782 static void gen_shr64_ins_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8783 {
8784     tcg_gen_shri_i64(a, a, shift);
8785     tcg_gen_deposit_i64(d, d, a, 0, 64 - shift);
8786 }
8787
8788 static void gen_shr_ins_vec(unsigned vece, TCGv_vec d, TCGv_vec a, int64_t sh)
8789 {
8790     uint64_t mask = (2ull << ((8 << vece) - 1)) - 1;
8791     TCGv_vec t = tcg_temp_new_vec_matching(d);
8792     TCGv_vec m = tcg_temp_new_vec_matching(d);
8793
8794     tcg_gen_dupi_vec(vece, m, mask ^ (mask >> sh));
8795     tcg_gen_shri_vec(vece, t, a, sh);
8796     tcg_gen_and_vec(vece, d, d, m);
8797     tcg_gen_or_vec(vece, d, d, t);
8798
8799     tcg_temp_free_vec(t);
8800     tcg_temp_free_vec(m);
8801 }
8802
8803 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
8804 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
8805                                  int immh, int immb, int opcode, int rn, int rd)
8806 {
8807     static const GVecGen2i ssra_op[4] = {
8808         { .fni8 = gen_ssra8_i64,
8809           .fniv = gen_ssra_vec,
8810           .load_dest = true,
8811           .opc = INDEX_op_sari_vec,
8812           .vece = MO_8 },
8813         { .fni8 = gen_ssra16_i64,
8814           .fniv = gen_ssra_vec,
8815           .load_dest = true,
8816           .opc = INDEX_op_sari_vec,
8817           .vece = MO_16 },
8818         { .fni4 = gen_ssra32_i32,
8819           .fniv = gen_ssra_vec,
8820           .load_dest = true,
8821           .opc = INDEX_op_sari_vec,
8822           .vece = MO_32 },
8823         { .fni8 = gen_ssra64_i64,
8824           .fniv = gen_ssra_vec,
8825           .prefer_i64 = TCG_TARGET_REG_BITS == 64,
8826           .load_dest = true,
8827           .opc = INDEX_op_sari_vec,
8828           .vece = MO_64 },
8829     };
8830     static const GVecGen2i usra_op[4] = {
8831         { .fni8 = gen_usra8_i64,
8832           .fniv = gen_usra_vec,
8833           .load_dest = true,
8834           .opc = INDEX_op_shri_vec,
8835           .vece = MO_8, },
8836         { .fni8 = gen_usra16_i64,
8837           .fniv = gen_usra_vec,
8838           .load_dest = true,
8839           .opc = INDEX_op_shri_vec,
8840           .vece = MO_16, },
8841         { .fni4 = gen_usra32_i32,
8842           .fniv = gen_usra_vec,
8843           .load_dest = true,
8844           .opc = INDEX_op_shri_vec,
8845           .vece = MO_32, },
8846         { .fni8 = gen_usra64_i64,
8847           .fniv = gen_usra_vec,
8848           .prefer_i64 = TCG_TARGET_REG_BITS == 64,
8849           .load_dest = true,
8850           .opc = INDEX_op_shri_vec,
8851           .vece = MO_64, },
8852     };
8853     static const GVecGen2i sri_op[4] = {
8854         { .fni8 = gen_shr8_ins_i64,
8855           .fniv = gen_shr_ins_vec,
8856           .load_dest = true,
8857           .opc = INDEX_op_shri_vec,
8858           .vece = MO_8 },
8859         { .fni8 = gen_shr16_ins_i64,
8860           .fniv = gen_shr_ins_vec,
8861           .load_dest = true,
8862           .opc = INDEX_op_shri_vec,
8863           .vece = MO_16 },
8864         { .fni4 = gen_shr32_ins_i32,
8865           .fniv = gen_shr_ins_vec,
8866           .load_dest = true,
8867           .opc = INDEX_op_shri_vec,
8868           .vece = MO_32 },
8869         { .fni8 = gen_shr64_ins_i64,
8870           .fniv = gen_shr_ins_vec,
8871           .prefer_i64 = TCG_TARGET_REG_BITS == 64,
8872           .load_dest = true,
8873           .opc = INDEX_op_shri_vec,
8874           .vece = MO_64 },
8875     };
8876
8877     int size = 32 - clz32(immh) - 1;
8878     int immhb = immh << 3 | immb;
8879     int shift = 2 * (8 << size) - immhb;
8880     bool accumulate = false;
8881     int dsize = is_q ? 128 : 64;
8882     int esize = 8 << size;
8883     int elements = dsize/esize;
8884     TCGMemOp memop = size | (is_u ? 0 : MO_SIGN);
8885     TCGv_i64 tcg_rn = new_tmp_a64(s);
8886     TCGv_i64 tcg_rd = new_tmp_a64(s);
8887     TCGv_i64 tcg_round;
8888     uint64_t round_const;
8889     int i;
8890
8891     if (extract32(immh, 3, 1) && !is_q) {
8892         unallocated_encoding(s);
8893         return;
8894     }
8895
8896     if (size > 3 && !is_q) {
8897         unallocated_encoding(s);
8898         return;
8899     }
8900
8901     if (!fp_access_check(s)) {
8902         return;
8903     }
8904
8905     switch (opcode) {
8906     case 0x02: /* SSRA / USRA (accumulate) */
8907         if (is_u) {
8908             /* Shift count same as element size produces zero to add.  */
8909             if (shift == 8 << size) {
8910                 goto done;
8911             }
8912             gen_gvec_op2i(s, is_q, rd, rn, shift, &usra_op[size]);
8913         } else {
8914             /* Shift count same as element size produces all sign to add.  */
8915             if (shift == 8 << size) {
8916                 shift -= 1;
8917             }
8918             gen_gvec_op2i(s, is_q, rd, rn, shift, &ssra_op[size]);
8919         }
8920         return;
8921     case 0x08: /* SRI */
8922         /* Shift count same as element size is valid but does nothing.  */
8923         if (shift == 8 << size) {
8924             goto done;
8925         }
8926         gen_gvec_op2i(s, is_q, rd, rn, shift, &sri_op[size]);
8927         return;
8928
8929     case 0x00: /* SSHR / USHR */
8930         if (is_u) {
8931             if (shift == 8 << size) {
8932                 /* Shift count the same size as element size produces zero.  */
8933                 tcg_gen_gvec_dup8i(vec_full_reg_offset(s, rd),
8934                                    is_q ? 16 : 8, vec_full_reg_size(s), 0);
8935             } else {
8936                 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shri, size);
8937             }
8938         } else {
8939             /* Shift count the same size as element size produces all sign.  */
8940             if (shift == 8 << size) {
8941                 shift -= 1;
8942             }
8943             gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_sari, size);
8944         }
8945         return;
8946
8947     case 0x04: /* SRSHR / URSHR (rounding) */
8948         break;
8949     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8950         accumulate = true;
8951         break;
8952     default:
8953         g_assert_not_reached();
8954     }
8955
8956     round_const = 1ULL << (shift - 1);
8957     tcg_round = tcg_const_i64(round_const);
8958
8959     for (i = 0; i < elements; i++) {
8960         read_vec_element(s, tcg_rn, rn, i, memop);
8961         if (accumulate) {
8962             read_vec_element(s, tcg_rd, rd, i, memop);
8963         }
8964
8965         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8966                                 accumulate, is_u, size, shift);
8967
8968         write_vec_element(s, tcg_rd, rd, i, size);
8969     }
8970     tcg_temp_free_i64(tcg_round);
8971
8972  done:
8973     clear_vec_high(s, is_q, rd);
8974 }
8975
8976 static void gen_shl8_ins_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8977 {
8978     uint64_t mask = dup_const(MO_8, 0xff << shift);
8979     TCGv_i64 t = tcg_temp_new_i64();
8980
8981     tcg_gen_shli_i64(t, a, shift);
8982     tcg_gen_andi_i64(t, t, mask);
8983     tcg_gen_andi_i64(d, d, ~mask);
8984     tcg_gen_or_i64(d, d, t);
8985     tcg_temp_free_i64(t);
8986 }
8987
8988 static void gen_shl16_ins_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8989 {
8990     uint64_t mask = dup_const(MO_16, 0xffff << shift);
8991     TCGv_i64 t = tcg_temp_new_i64();
8992
8993     tcg_gen_shli_i64(t, a, shift);
8994     tcg_gen_andi_i64(t, t, mask);
8995     tcg_gen_andi_i64(d, d, ~mask);
8996     tcg_gen_or_i64(d, d, t);
8997     tcg_temp_free_i64(t);
8998 }
8999
9000 static void gen_shl32_ins_i32(TCGv_i32 d, TCGv_i32 a, int32_t shift)
9001 {
9002     tcg_gen_deposit_i32(d, d, a, shift, 32 - shift);
9003 }
9004
9005 static void gen_shl64_ins_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
9006 {
9007     tcg_gen_deposit_i64(d, d, a, shift, 64 - shift);
9008 }
9009
9010 static void gen_shl_ins_vec(unsigned vece, TCGv_vec d, TCGv_vec a, int64_t sh)
9011 {
9012     uint64_t mask = (1ull << sh) - 1;
9013     TCGv_vec t = tcg_temp_new_vec_matching(d);
9014     TCGv_vec m = tcg_temp_new_vec_matching(d);
9015
9016     tcg_gen_dupi_vec(vece, m, mask);
9017     tcg_gen_shli_vec(vece, t, a, sh);
9018     tcg_gen_and_vec(vece, d, d, m);
9019     tcg_gen_or_vec(vece, d, d, t);
9020
9021     tcg_temp_free_vec(t);
9022     tcg_temp_free_vec(m);
9023 }
9024
9025 /* SHL/SLI - Vector shift left */
9026 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
9027                                  int immh, int immb, int opcode, int rn, int rd)
9028 {
9029     static const GVecGen2i shi_op[4] = {
9030         { .fni8 = gen_shl8_ins_i64,
9031           .fniv = gen_shl_ins_vec,
9032           .opc = INDEX_op_shli_vec,
9033           .prefer_i64 = TCG_TARGET_REG_BITS == 64,
9034           .load_dest = true,
9035           .vece = MO_8 },
9036         { .fni8 = gen_shl16_ins_i64,
9037           .fniv = gen_shl_ins_vec,
9038           .opc = INDEX_op_shli_vec,
9039           .prefer_i64 = TCG_TARGET_REG_BITS == 64,
9040           .load_dest = true,
9041           .vece = MO_16 },
9042         { .fni4 = gen_shl32_ins_i32,
9043           .fniv = gen_shl_ins_vec,
9044           .opc = INDEX_op_shli_vec,
9045           .prefer_i64 = TCG_TARGET_REG_BITS == 64,
9046           .load_dest = true,
9047           .vece = MO_32 },
9048         { .fni8 = gen_shl64_ins_i64,
9049           .fniv = gen_shl_ins_vec,
9050           .opc = INDEX_op_shli_vec,
9051           .prefer_i64 = TCG_TARGET_REG_BITS == 64,
9052           .load_dest = true,
9053           .vece = MO_64 },
9054     };
9055     int size = 32 - clz32(immh) - 1;
9056     int immhb = immh << 3 | immb;
9057     int shift = immhb - (8 << size);
9058
9059     if (extract32(immh, 3, 1) && !is_q) {
9060         unallocated_encoding(s);
9061         return;
9062     }
9063
9064     if (size > 3 && !is_q) {
9065         unallocated_encoding(s);
9066         return;
9067     }
9068
9069     if (!fp_access_check(s)) {
9070         return;
9071     }
9072
9073     if (insert) {
9074         gen_gvec_op2i(s, is_q, rd, rn, shift, &shi_op[size]);
9075     } else {
9076         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
9077     }
9078 }
9079
9080 /* USHLL/SHLL - Vector shift left with widening */
9081 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
9082                                  int immh, int immb, int opcode, int rn, int rd)
9083 {
9084     int size = 32 - clz32(immh) - 1;
9085     int immhb = immh << 3 | immb;
9086     int shift = immhb - (8 << size);
9087     int dsize = 64;
9088     int esize = 8 << size;
9089     int elements = dsize/esize;
9090     TCGv_i64 tcg_rn = new_tmp_a64(s);
9091     TCGv_i64 tcg_rd = new_tmp_a64(s);
9092     int i;
9093
9094     if (size >= 3) {
9095         unallocated_encoding(s);
9096         return;
9097     }
9098
9099     if (!fp_access_check(s)) {
9100         return;
9101     }
9102
9103     /* For the LL variants the store is larger than the load,
9104      * so if rd == rn we would overwrite parts of our input.
9105      * So load everything right now and use shifts in the main loop.
9106      */
9107     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
9108
9109     for (i = 0; i < elements; i++) {
9110         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
9111         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
9112         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
9113         write_vec_element(s, tcg_rd, rd, i, size + 1);
9114     }
9115 }
9116
9117 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
9118 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
9119                                  int immh, int immb, int opcode, int rn, int rd)
9120 {
9121     int immhb = immh << 3 | immb;
9122     int size = 32 - clz32(immh) - 1;
9123     int dsize = 64;
9124     int esize = 8 << size;
9125     int elements = dsize/esize;
9126     int shift = (2 * esize) - immhb;
9127     bool round = extract32(opcode, 0, 1);
9128     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
9129     TCGv_i64 tcg_round;
9130     int i;
9131
9132     if (extract32(immh, 3, 1)) {
9133         unallocated_encoding(s);
9134         return;
9135     }
9136
9137     if (!fp_access_check(s)) {
9138         return;
9139     }
9140
9141     tcg_rn = tcg_temp_new_i64();
9142     tcg_rd = tcg_temp_new_i64();
9143     tcg_final = tcg_temp_new_i64();
9144     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
9145
9146     if (round) {
9147         uint64_t round_const = 1ULL << (shift - 1);
9148         tcg_round = tcg_const_i64(round_const);
9149     } else {
9150         tcg_round = NULL;
9151     }
9152
9153     for (i = 0; i < elements; i++) {
9154         read_vec_element(s, tcg_rn, rn, i, size+1);
9155         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
9156                                 false, true, size+1, shift);
9157
9158         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
9159     }
9160
9161     if (!is_q) {
9162         write_vec_element(s, tcg_final, rd, 0, MO_64);
9163     } else {
9164         write_vec_element(s, tcg_final, rd, 1, MO_64);
9165     }
9166     if (round) {
9167         tcg_temp_free_i64(tcg_round);
9168     }
9169     tcg_temp_free_i64(tcg_rn);
9170     tcg_temp_free_i64(tcg_rd);
9171     tcg_temp_free_i64(tcg_final);
9172
9173     clear_vec_high(s, is_q, rd);
9174 }
9175
9176
9177 /* AdvSIMD shift by immediate
9178  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
9179  * +---+---+---+-------------+------+------+--------+---+------+------+
9180  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
9181  * +---+---+---+-------------+------+------+--------+---+------+------+
9182  */
9183 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
9184 {
9185     int rd = extract32(insn, 0, 5);
9186     int rn = extract32(insn, 5, 5);
9187     int opcode = extract32(insn, 11, 5);
9188     int immb = extract32(insn, 16, 3);
9189     int immh = extract32(insn, 19, 4);
9190     bool is_u = extract32(insn, 29, 1);
9191     bool is_q = extract32(insn, 30, 1);
9192
9193     switch (opcode) {
9194     case 0x08: /* SRI */
9195         if (!is_u) {
9196             unallocated_encoding(s);
9197             return;
9198         }
9199         /* fall through */
9200     case 0x00: /* SSHR / USHR */
9201     case 0x02: /* SSRA / USRA (accumulate) */
9202     case 0x04: /* SRSHR / URSHR (rounding) */
9203     case 0x06: /* SRSRA / URSRA (accum + rounding) */
9204         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
9205         break;
9206     case 0x0a: /* SHL / SLI */
9207         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
9208         break;
9209     case 0x10: /* SHRN */
9210     case 0x11: /* RSHRN / SQRSHRUN */
9211         if (is_u) {
9212             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
9213                                    opcode, rn, rd);
9214         } else {
9215             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
9216         }
9217         break;
9218     case 0x12: /* SQSHRN / UQSHRN */
9219     case 0x13: /* SQRSHRN / UQRSHRN */
9220         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
9221                                opcode, rn, rd);
9222         break;
9223     case 0x14: /* SSHLL / USHLL */
9224         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
9225         break;
9226     case 0x1c: /* SCVTF / UCVTF */
9227         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
9228                                      opcode, rn, rd);
9229         break;
9230     case 0xc: /* SQSHLU */
9231         if (!is_u) {
9232             unallocated_encoding(s);
9233             return;
9234         }
9235         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
9236         break;
9237     case 0xe: /* SQSHL, UQSHL */
9238         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
9239         break;
9240     case 0x1f: /* FCVTZS/ FCVTZU */
9241         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
9242         return;
9243     default:
9244         unallocated_encoding(s);
9245         return;
9246     }
9247 }
9248
9249 /* Generate code to do a "long" addition or subtraction, ie one done in
9250  * TCGv_i64 on vector lanes twice the width specified by size.
9251  */
9252 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
9253                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
9254 {
9255     static NeonGenTwo64OpFn * const fns[3][2] = {
9256         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
9257         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
9258         { tcg_gen_add_i64, tcg_gen_sub_i64 },
9259     };
9260     NeonGenTwo64OpFn *genfn;
9261     assert(size < 3);
9262
9263     genfn = fns[size][is_sub];
9264     genfn(tcg_res, tcg_op1, tcg_op2);
9265 }
9266
9267 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
9268                                 int opcode, int rd, int rn, int rm)
9269 {
9270     /* 3-reg-different widening insns: 64 x 64 -> 128 */
9271     TCGv_i64 tcg_res[2];
9272     int pass, accop;
9273
9274     tcg_res[0] = tcg_temp_new_i64();
9275     tcg_res[1] = tcg_temp_new_i64();
9276
9277     /* Does this op do an adding accumulate, a subtracting accumulate,
9278      * or no accumulate at all?
9279      */
9280     switch (opcode) {
9281     case 5:
9282     case 8:
9283     case 9:
9284         accop = 1;
9285         break;
9286     case 10:
9287     case 11:
9288         accop = -1;
9289         break;
9290     default:
9291         accop = 0;
9292         break;
9293     }
9294
9295     if (accop != 0) {
9296         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
9297         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
9298     }
9299
9300     /* size == 2 means two 32x32->64 operations; this is worth special
9301      * casing because we can generally handle it inline.
9302      */
9303     if (size == 2) {
9304         for (pass = 0; pass < 2; pass++) {
9305             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9306             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9307             TCGv_i64 tcg_passres;
9308             TCGMemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
9309
9310             int elt = pass + is_q * 2;
9311
9312             read_vec_element(s, tcg_op1, rn, elt, memop);
9313             read_vec_element(s, tcg_op2, rm, elt, memop);
9314
9315             if (accop == 0) {
9316                 tcg_passres = tcg_res[pass];
9317             } else {
9318                 tcg_passres = tcg_temp_new_i64();
9319             }
9320
9321             switch (opcode) {
9322             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
9323                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
9324                 break;
9325             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
9326                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
9327                 break;
9328             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
9329             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
9330             {
9331                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
9332                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
9333
9334                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
9335                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
9336                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
9337                                     tcg_passres,
9338                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
9339                 tcg_temp_free_i64(tcg_tmp1);
9340                 tcg_temp_free_i64(tcg_tmp2);
9341                 break;
9342             }
9343             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
9344             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
9345             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
9346                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
9347                 break;
9348             case 9: /* SQDMLAL, SQDMLAL2 */
9349             case 11: /* SQDMLSL, SQDMLSL2 */
9350             case 13: /* SQDMULL, SQDMULL2 */
9351                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
9352                 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
9353                                                   tcg_passres, tcg_passres);
9354                 break;
9355             default:
9356                 g_assert_not_reached();
9357             }
9358
9359             if (opcode == 9 || opcode == 11) {
9360                 /* saturating accumulate ops */
9361                 if (accop < 0) {
9362                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
9363                 }
9364                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
9365                                                   tcg_res[pass], tcg_passres);
9366             } else if (accop > 0) {
9367                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
9368             } else if (accop < 0) {
9369                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
9370             }
9371
9372             if (accop != 0) {
9373                 tcg_temp_free_i64(tcg_passres);
9374             }
9375
9376             tcg_temp_free_i64(tcg_op1);
9377             tcg_temp_free_i64(tcg_op2);
9378         }
9379     } else {
9380         /* size 0 or 1, generally helper functions */
9381         for (pass = 0; pass < 2; pass++) {
9382             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9383             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9384             TCGv_i64 tcg_passres;
9385             int elt = pass + is_q * 2;
9386
9387             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
9388             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
9389
9390             if (accop == 0) {
9391                 tcg_passres = tcg_res[pass];
9392             } else {
9393                 tcg_passres = tcg_temp_new_i64();
9394             }
9395
9396             switch (opcode) {
9397             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
9398             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
9399             {
9400                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
9401                 static NeonGenWidenFn * const widenfns[2][2] = {
9402                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
9403                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
9404                 };
9405                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
9406
9407                 widenfn(tcg_op2_64, tcg_op2);
9408                 widenfn(tcg_passres, tcg_op1);
9409                 gen_neon_addl(size, (opcode == 2), tcg_passres,
9410                               tcg_passres, tcg_op2_64);
9411                 tcg_temp_free_i64(tcg_op2_64);
9412                 break;
9413             }
9414             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
9415             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
9416                 if (size == 0) {
9417                     if (is_u) {
9418                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
9419                     } else {
9420                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
9421                     }
9422                 } else {
9423                     if (is_u) {
9424                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
9425                     } else {
9426                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
9427                     }
9428                 }
9429                 break;
9430             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
9431             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
9432             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
9433                 if (size == 0) {
9434                     if (is_u) {
9435                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
9436                     } else {
9437                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
9438                     }
9439                 } else {
9440                     if (is_u) {
9441                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
9442                     } else {
9443                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
9444                     }
9445                 }
9446                 break;
9447             case 9: /* SQDMLAL, SQDMLAL2 */
9448             case 11: /* SQDMLSL, SQDMLSL2 */
9449             case 13: /* SQDMULL, SQDMULL2 */
9450                 assert(size == 1);
9451                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
9452                 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
9453                                                   tcg_passres, tcg_passres);
9454                 break;
9455             case 14: /* PMULL */
9456                 assert(size == 0);
9457                 gen_helper_neon_mull_p8(tcg_passres, tcg_op1, tcg_op2);
9458                 break;
9459             default:
9460                 g_assert_not_reached();
9461             }
9462             tcg_temp_free_i32(tcg_op1);
9463             tcg_temp_free_i32(tcg_op2);
9464
9465             if (accop != 0) {
9466                 if (opcode == 9 || opcode == 11) {
9467                     /* saturating accumulate ops */
9468                     if (accop < 0) {
9469                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
9470                     }
9471                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
9472                                                       tcg_res[pass],
9473                                                       tcg_passres);
9474                 } else {
9475                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
9476                                   tcg_res[pass], tcg_passres);
9477                 }
9478                 tcg_temp_free_i64(tcg_passres);
9479             }
9480         }
9481     }
9482
9483     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
9484     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
9485     tcg_temp_free_i64(tcg_res[0]);
9486     tcg_temp_free_i64(tcg_res[1]);
9487 }
9488
9489 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
9490                             int opcode, int rd, int rn, int rm)
9491 {
9492     TCGv_i64 tcg_res[2];
9493     int part = is_q ? 2 : 0;
9494     int pass;
9495
9496     for (pass = 0; pass < 2; pass++) {
9497         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9498         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9499         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
9500         static NeonGenWidenFn * const widenfns[3][2] = {
9501             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
9502             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
9503             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
9504         };
9505         NeonGenWidenFn *widenfn = widenfns[size][is_u];
9506
9507         read_vec_element(s, tcg_op1, rn, pass, MO_64);
9508         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
9509         widenfn(tcg_op2_wide, tcg_op2);
9510         tcg_temp_free_i32(tcg_op2);
9511         tcg_res[pass] = tcg_temp_new_i64();
9512         gen_neon_addl(size, (opcode == 3),
9513                       tcg_res[pass], tcg_op1, tcg_op2_wide);
9514         tcg_temp_free_i64(tcg_op1);
9515         tcg_temp_free_i64(tcg_op2_wide);
9516     }
9517
9518     for (pass = 0; pass < 2; pass++) {
9519         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9520         tcg_temp_free_i64(tcg_res[pass]);
9521     }
9522 }
9523
9524 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
9525 {
9526     tcg_gen_addi_i64(in, in, 1U << 31);
9527     tcg_gen_extrh_i64_i32(res, in);
9528 }
9529
9530 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
9531                                  int opcode, int rd, int rn, int rm)
9532 {
9533     TCGv_i32 tcg_res[2];
9534     int part = is_q ? 2 : 0;
9535     int pass;
9536
9537     for (pass = 0; pass < 2; pass++) {
9538         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9539         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9540         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
9541         static NeonGenNarrowFn * const narrowfns[3][2] = {
9542             { gen_helper_neon_narrow_high_u8,
9543               gen_helper_neon_narrow_round_high_u8 },
9544             { gen_helper_neon_narrow_high_u16,
9545               gen_helper_neon_narrow_round_high_u16 },
9546             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
9547         };
9548         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
9549
9550         read_vec_element(s, tcg_op1, rn, pass, MO_64);
9551         read_vec_element(s, tcg_op2, rm, pass, MO_64);
9552
9553         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
9554
9555         tcg_temp_free_i64(tcg_op1);
9556         tcg_temp_free_i64(tcg_op2);
9557
9558         tcg_res[pass] = tcg_temp_new_i32();
9559         gennarrow(tcg_res[pass], tcg_wideres);
9560         tcg_temp_free_i64(tcg_wideres);
9561     }
9562
9563     for (pass = 0; pass < 2; pass++) {
9564         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
9565         tcg_temp_free_i32(tcg_res[pass]);
9566     }
9567     clear_vec_high(s, is_q, rd);
9568 }
9569
9570 static void handle_pmull_64(DisasContext *s, int is_q, int rd, int rn, int rm)
9571 {
9572     /* PMULL of 64 x 64 -> 128 is an odd special case because it
9573      * is the only three-reg-diff instruction which produces a
9574      * 128-bit wide result from a single operation. However since
9575      * it's possible to calculate the two halves more or less
9576      * separately we just use two helper calls.
9577      */
9578     TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9579     TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9580     TCGv_i64 tcg_res = tcg_temp_new_i64();
9581
9582     read_vec_element(s, tcg_op1, rn, is_q, MO_64);
9583     read_vec_element(s, tcg_op2, rm, is_q, MO_64);
9584     gen_helper_neon_pmull_64_lo(tcg_res, tcg_op1, tcg_op2);
9585     write_vec_element(s, tcg_res, rd, 0, MO_64);
9586     gen_helper_neon_pmull_64_hi(tcg_res, tcg_op1, tcg_op2);
9587     write_vec_element(s, tcg_res, rd, 1, MO_64);
9588
9589     tcg_temp_free_i64(tcg_op1);
9590     tcg_temp_free_i64(tcg_op2);
9591     tcg_temp_free_i64(tcg_res);
9592 }
9593
9594 /* AdvSIMD three different
9595  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
9596  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
9597  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
9598  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
9599  */
9600 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
9601 {
9602     /* Instructions in this group fall into three basic classes
9603      * (in each case with the operation working on each element in
9604      * the input vectors):
9605      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
9606      *     128 bit input)
9607      * (2) wide 64 x 128 -> 128
9608      * (3) narrowing 128 x 128 -> 64
9609      * Here we do initial decode, catch unallocated cases and
9610      * dispatch to separate functions for each class.
9611      */
9612     int is_q = extract32(insn, 30, 1);
9613     int is_u = extract32(insn, 29, 1);
9614     int size = extract32(insn, 22, 2);
9615     int opcode = extract32(insn, 12, 4);
9616     int rm = extract32(insn, 16, 5);
9617     int rn = extract32(insn, 5, 5);
9618     int rd = extract32(insn, 0, 5);
9619
9620     switch (opcode) {
9621     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
9622     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
9623         /* 64 x 128 -> 128 */
9624         if (size == 3) {
9625             unallocated_encoding(s);
9626             return;
9627         }
9628         if (!fp_access_check(s)) {
9629             return;
9630         }
9631         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
9632         break;
9633     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
9634     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
9635         /* 128 x 128 -> 64 */
9636         if (size == 3) {
9637             unallocated_encoding(s);
9638             return;
9639         }
9640         if (!fp_access_check(s)) {
9641             return;
9642         }
9643         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
9644         break;
9645     case 14: /* PMULL, PMULL2 */
9646         if (is_u || size == 1 || size == 2) {
9647             unallocated_encoding(s);
9648             return;
9649         }
9650         if (size == 3) {
9651             if (!arm_dc_feature(s, ARM_FEATURE_V8_PMULL)) {
9652                 unallocated_encoding(s);
9653                 return;
9654             }
9655             if (!fp_access_check(s)) {
9656                 return;
9657             }
9658             handle_pmull_64(s, is_q, rd, rn, rm);
9659             return;
9660         }
9661         goto is_widening;
9662     case 9: /* SQDMLAL, SQDMLAL2 */
9663     case 11: /* SQDMLSL, SQDMLSL2 */
9664     case 13: /* SQDMULL, SQDMULL2 */
9665         if (is_u || size == 0) {
9666             unallocated_encoding(s);
9667             return;
9668         }
9669         /* fall through */
9670     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
9671     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
9672     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
9673     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
9674     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
9675     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
9676     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
9677         /* 64 x 64 -> 128 */
9678         if (size == 3) {
9679             unallocated_encoding(s);
9680             return;
9681         }
9682     is_widening:
9683         if (!fp_access_check(s)) {
9684             return;
9685         }
9686
9687         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
9688         break;
9689     default:
9690         /* opcode 15 not allocated */
9691         unallocated_encoding(s);
9692         break;
9693     }
9694 }
9695
9696 static void gen_bsl_i64(TCGv_i64 rd, TCGv_i64 rn, TCGv_i64 rm)
9697 {
9698     tcg_gen_xor_i64(rn, rn, rm);
9699     tcg_gen_and_i64(rn, rn, rd);
9700     tcg_gen_xor_i64(rd, rm, rn);
9701 }
9702
9703 static void gen_bit_i64(TCGv_i64 rd, TCGv_i64 rn, TCGv_i64 rm)
9704 {
9705     tcg_gen_xor_i64(rn, rn, rd);
9706     tcg_gen_and_i64(rn, rn, rm);
9707     tcg_gen_xor_i64(rd, rd, rn);
9708 }
9709
9710 static void gen_bif_i64(TCGv_i64 rd, TCGv_i64 rn, TCGv_i64 rm)
9711 {
9712     tcg_gen_xor_i64(rn, rn, rd);
9713     tcg_gen_andc_i64(rn, rn, rm);
9714     tcg_gen_xor_i64(rd, rd, rn);
9715 }
9716
9717 static void gen_bsl_vec(unsigned vece, TCGv_vec rd, TCGv_vec rn, TCGv_vec rm)
9718 {
9719     tcg_gen_xor_vec(vece, rn, rn, rm);
9720     tcg_gen_and_vec(vece, rn, rn, rd);
9721     tcg_gen_xor_vec(vece, rd, rm, rn);
9722 }
9723
9724 static void gen_bit_vec(unsigned vece, TCGv_vec rd, TCGv_vec rn, TCGv_vec rm)
9725 {
9726     tcg_gen_xor_vec(vece, rn, rn, rd);
9727     tcg_gen_and_vec(vece, rn, rn, rm);
9728     tcg_gen_xor_vec(vece, rd, rd, rn);
9729 }
9730
9731 static void gen_bif_vec(unsigned vece, TCGv_vec rd, TCGv_vec rn, TCGv_vec rm)
9732 {
9733     tcg_gen_xor_vec(vece, rn, rn, rd);
9734     tcg_gen_andc_vec(vece, rn, rn, rm);
9735     tcg_gen_xor_vec(vece, rd, rd, rn);
9736 }
9737
9738 /* Logic op (opcode == 3) subgroup of C3.6.16. */
9739 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
9740 {
9741     static const GVecGen3 bsl_op = {
9742         .fni8 = gen_bsl_i64,
9743         .fniv = gen_bsl_vec,
9744         .prefer_i64 = TCG_TARGET_REG_BITS == 64,
9745         .load_dest = true
9746     };
9747     static const GVecGen3 bit_op = {
9748         .fni8 = gen_bit_i64,
9749         .fniv = gen_bit_vec,
9750         .prefer_i64 = TCG_TARGET_REG_BITS == 64,
9751         .load_dest = true
9752     };
9753     static const GVecGen3 bif_op = {
9754         .fni8 = gen_bif_i64,
9755         .fniv = gen_bif_vec,
9756         .prefer_i64 = TCG_TARGET_REG_BITS == 64,
9757         .load_dest = true
9758     };
9759
9760     int rd = extract32(insn, 0, 5);
9761     int rn = extract32(insn, 5, 5);
9762     int rm = extract32(insn, 16, 5);
9763     int size = extract32(insn, 22, 2);
9764     bool is_u = extract32(insn, 29, 1);
9765     bool is_q = extract32(insn, 30, 1);
9766
9767     if (!fp_access_check(s)) {
9768         return;
9769     }
9770
9771     switch (size + 4 * is_u) {
9772     case 0: /* AND */
9773         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
9774         return;
9775     case 1: /* BIC */
9776         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
9777         return;
9778     case 2: /* ORR */
9779         if (rn == rm) { /* MOV */
9780             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_mov, 0);
9781         } else {
9782             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
9783         }
9784         return;
9785     case 3: /* ORN */
9786         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
9787         return;
9788     case 4: /* EOR */
9789         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
9790         return;
9791
9792     case 5: /* BSL bitwise select */
9793         gen_gvec_op3(s, is_q, rd, rn, rm, &bsl_op);
9794         return;
9795     case 6: /* BIT, bitwise insert if true */
9796         gen_gvec_op3(s, is_q, rd, rn, rm, &bit_op);
9797         return;
9798     case 7: /* BIF, bitwise insert if false */
9799         gen_gvec_op3(s, is_q, rd, rn, rm, &bif_op);
9800         return;
9801
9802     default:
9803         g_assert_not_reached();
9804     }
9805 }
9806
9807 /* Helper functions for 32 bit comparisons */
9808 static void gen_max_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9809 {
9810     tcg_gen_movcond_i32(TCG_COND_GE, res, op1, op2, op1, op2);
9811 }
9812
9813 static void gen_max_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9814 {
9815     tcg_gen_movcond_i32(TCG_COND_GEU, res, op1, op2, op1, op2);
9816 }
9817
9818 static void gen_min_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9819 {
9820     tcg_gen_movcond_i32(TCG_COND_LE, res, op1, op2, op1, op2);
9821 }
9822
9823 static void gen_min_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9824 {
9825     tcg_gen_movcond_i32(TCG_COND_LEU, res, op1, op2, op1, op2);
9826 }
9827
9828 /* Pairwise op subgroup of C3.6.16.
9829  *
9830  * This is called directly or via the handle_3same_float for float pairwise
9831  * operations where the opcode and size are calculated differently.
9832  */
9833 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
9834                                    int size, int rn, int rm, int rd)
9835 {
9836     TCGv_ptr fpst;
9837     int pass;
9838
9839     /* Floating point operations need fpst */
9840     if (opcode >= 0x58) {
9841         fpst = get_fpstatus_ptr(false);
9842     } else {
9843         fpst = NULL;
9844     }
9845
9846     if (!fp_access_check(s)) {
9847         return;
9848     }
9849
9850     /* These operations work on the concatenated rm:rn, with each pair of
9851      * adjacent elements being operated on to produce an element in the result.
9852      */
9853     if (size == 3) {
9854         TCGv_i64 tcg_res[2];
9855
9856         for (pass = 0; pass < 2; pass++) {
9857             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9858             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9859             int passreg = (pass == 0) ? rn : rm;
9860
9861             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
9862             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
9863             tcg_res[pass] = tcg_temp_new_i64();
9864
9865             switch (opcode) {
9866             case 0x17: /* ADDP */
9867                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
9868                 break;
9869             case 0x58: /* FMAXNMP */
9870                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9871                 break;
9872             case 0x5a: /* FADDP */
9873                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9874                 break;
9875             case 0x5e: /* FMAXP */
9876                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9877                 break;
9878             case 0x78: /* FMINNMP */
9879                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9880                 break;
9881             case 0x7e: /* FMINP */
9882                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9883                 break;
9884             default:
9885                 g_assert_not_reached();
9886             }
9887
9888             tcg_temp_free_i64(tcg_op1);
9889             tcg_temp_free_i64(tcg_op2);
9890         }
9891
9892         for (pass = 0; pass < 2; pass++) {
9893             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9894             tcg_temp_free_i64(tcg_res[pass]);
9895         }
9896     } else {
9897         int maxpass = is_q ? 4 : 2;
9898         TCGv_i32 tcg_res[4];
9899
9900         for (pass = 0; pass < maxpass; pass++) {
9901             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9902             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9903             NeonGenTwoOpFn *genfn = NULL;
9904             int passreg = pass < (maxpass / 2) ? rn : rm;
9905             int passelt = (is_q && (pass & 1)) ? 2 : 0;
9906
9907             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
9908             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
9909             tcg_res[pass] = tcg_temp_new_i32();
9910
9911             switch (opcode) {
9912             case 0x17: /* ADDP */
9913             {
9914                 static NeonGenTwoOpFn * const fns[3] = {
9915                     gen_helper_neon_padd_u8,
9916                     gen_helper_neon_padd_u16,
9917                     tcg_gen_add_i32,
9918                 };
9919                 genfn = fns[size];
9920                 break;
9921             }
9922             case 0x14: /* SMAXP, UMAXP */
9923             {
9924                 static NeonGenTwoOpFn * const fns[3][2] = {
9925                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
9926                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
9927                     { gen_max_s32, gen_max_u32 },
9928                 };
9929                 genfn = fns[size][u];
9930                 break;
9931             }
9932             case 0x15: /* SMINP, UMINP */
9933             {
9934                 static NeonGenTwoOpFn * const fns[3][2] = {
9935                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
9936                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
9937                     { gen_min_s32, gen_min_u32 },
9938                 };
9939                 genfn = fns[size][u];
9940                 break;
9941             }
9942             /* The FP operations are all on single floats (32 bit) */
9943             case 0x58: /* FMAXNMP */
9944                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9945                 break;
9946             case 0x5a: /* FADDP */
9947                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9948                 break;
9949             case 0x5e: /* FMAXP */
9950                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9951                 break;
9952             case 0x78: /* FMINNMP */
9953                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9954                 break;
9955             case 0x7e: /* FMINP */
9956                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9957                 break;
9958             default:
9959                 g_assert_not_reached();
9960             }
9961
9962             /* FP ops called directly, otherwise call now */
9963             if (genfn) {
9964                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
9965             }
9966
9967             tcg_temp_free_i32(tcg_op1);
9968             tcg_temp_free_i32(tcg_op2);
9969         }
9970
9971         for (pass = 0; pass < maxpass; pass++) {
9972             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
9973             tcg_temp_free_i32(tcg_res[pass]);
9974         }
9975         clear_vec_high(s, is_q, rd);
9976     }
9977
9978     if (fpst) {
9979         tcg_temp_free_ptr(fpst);
9980     }
9981 }
9982
9983 /* Floating point op subgroup of C3.6.16. */
9984 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
9985 {
9986     /* For floating point ops, the U, size[1] and opcode bits
9987      * together indicate the operation. size[0] indicates single
9988      * or double.
9989      */
9990     int fpopcode = extract32(insn, 11, 5)
9991         | (extract32(insn, 23, 1) << 5)
9992         | (extract32(insn, 29, 1) << 6);
9993     int is_q = extract32(insn, 30, 1);
9994     int size = extract32(insn, 22, 1);
9995     int rm = extract32(insn, 16, 5);
9996     int rn = extract32(insn, 5, 5);
9997     int rd = extract32(insn, 0, 5);
9998
9999     int datasize = is_q ? 128 : 64;
10000     int esize = 32 << size;
10001     int elements = datasize / esize;
10002
10003     if (size == 1 && !is_q) {
10004         unallocated_encoding(s);
10005         return;
10006     }
10007
10008     switch (fpopcode) {
10009     case 0x58: /* FMAXNMP */
10010     case 0x5a: /* FADDP */
10011     case 0x5e: /* FMAXP */
10012     case 0x78: /* FMINNMP */
10013     case 0x7e: /* FMINP */
10014         if (size && !is_q) {
10015             unallocated_encoding(s);
10016             return;
10017         }
10018         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
10019                                rn, rm, rd);
10020         return;
10021     case 0x1b: /* FMULX */
10022     case 0x1f: /* FRECPS */
10023     case 0x3f: /* FRSQRTS */
10024     case 0x5d: /* FACGE */
10025     case 0x7d: /* FACGT */
10026     case 0x19: /* FMLA */
10027     case 0x39: /* FMLS */
10028     case 0x18: /* FMAXNM */
10029     case 0x1a: /* FADD */
10030     case 0x1c: /* FCMEQ */
10031     case 0x1e: /* FMAX */
10032     case 0x38: /* FMINNM */
10033     case 0x3a: /* FSUB */
10034     case 0x3e: /* FMIN */
10035     case 0x5b: /* FMUL */
10036     case 0x5c: /* FCMGE */
10037     case 0x5f: /* FDIV */
10038     case 0x7a: /* FABD */
10039     case 0x7c: /* FCMGT */
10040         if (!fp_access_check(s)) {
10041             return;
10042         }
10043
10044         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
10045         return;
10046     default:
10047         unallocated_encoding(s);
10048         return;
10049     }
10050 }
10051
10052 static void gen_mla8_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
10053 {
10054     gen_helper_neon_mul_u8(a, a, b);
10055     gen_helper_neon_add_u8(d, d, a);
10056 }
10057
10058 static void gen_mla16_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
10059 {
10060     gen_helper_neon_mul_u16(a, a, b);
10061     gen_helper_neon_add_u16(d, d, a);
10062 }
10063
10064 static void gen_mla32_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
10065 {
10066     tcg_gen_mul_i32(a, a, b);
10067     tcg_gen_add_i32(d, d, a);
10068 }
10069
10070 static void gen_mla64_i64(TCGv_i64 d, TCGv_i64 a, TCGv_i64 b)
10071 {
10072     tcg_gen_mul_i64(a, a, b);
10073     tcg_gen_add_i64(d, d, a);
10074 }
10075
10076 static void gen_mla_vec(unsigned vece, TCGv_vec d, TCGv_vec a, TCGv_vec b)
10077 {
10078     tcg_gen_mul_vec(vece, a, a, b);
10079     tcg_gen_add_vec(vece, d, d, a);
10080 }
10081
10082 static void gen_mls8_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
10083 {
10084     gen_helper_neon_mul_u8(a, a, b);
10085     gen_helper_neon_sub_u8(d, d, a);
10086 }
10087
10088 static void gen_mls16_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
10089 {
10090     gen_helper_neon_mul_u16(a, a, b);
10091     gen_helper_neon_sub_u16(d, d, a);
10092 }
10093
10094 static void gen_mls32_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
10095 {
10096     tcg_gen_mul_i32(a, a, b);
10097     tcg_gen_sub_i32(d, d, a);
10098 }
10099
10100 static void gen_mls64_i64(TCGv_i64 d, TCGv_i64 a, TCGv_i64 b)
10101 {
10102     tcg_gen_mul_i64(a, a, b);
10103     tcg_gen_sub_i64(d, d, a);
10104 }
10105
10106 static void gen_mls_vec(unsigned vece, TCGv_vec d, TCGv_vec a, TCGv_vec b)
10107 {
10108     tcg_gen_mul_vec(vece, a, a, b);
10109     tcg_gen_sub_vec(vece, d, d, a);
10110 }
10111
10112 /* Integer op subgroup of C3.6.16. */
10113 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
10114 {
10115     static const GVecGen3 cmtst_op[4] = {
10116         { .fni4 = gen_helper_neon_tst_u8,
10117           .fniv = gen_cmtst_vec,
10118           .vece = MO_8 },
10119         { .fni4 = gen_helper_neon_tst_u16,
10120           .fniv = gen_cmtst_vec,
10121           .vece = MO_16 },
10122         { .fni4 = gen_cmtst_i32,
10123           .fniv = gen_cmtst_vec,
10124           .vece = MO_32 },
10125         { .fni8 = gen_cmtst_i64,
10126           .fniv = gen_cmtst_vec,
10127           .prefer_i64 = TCG_TARGET_REG_BITS == 64,
10128           .vece = MO_64 },
10129     };
10130     static const GVecGen3 mla_op[4] = {
10131         { .fni4 = gen_mla8_i32,
10132           .fniv = gen_mla_vec,
10133           .opc = INDEX_op_mul_vec,
10134           .load_dest = true,
10135           .vece = MO_8 },
10136         { .fni4 = gen_mla16_i32,
10137           .fniv = gen_mla_vec,
10138           .opc = INDEX_op_mul_vec,
10139           .load_dest = true,
10140           .vece = MO_16 },
10141         { .fni4 = gen_mla32_i32,
10142           .fniv = gen_mla_vec,
10143           .opc = INDEX_op_mul_vec,
10144           .load_dest = true,
10145           .vece = MO_32 },
10146         { .fni8 = gen_mla64_i64,
10147           .fniv = gen_mla_vec,
10148           .opc = INDEX_op_mul_vec,
10149           .prefer_i64 = TCG_TARGET_REG_BITS == 64,
10150           .load_dest = true,
10151           .vece = MO_64 },
10152     };
10153     static const GVecGen3 mls_op[4] = {
10154         { .fni4 = gen_mls8_i32,
10155           .fniv = gen_mls_vec,
10156           .opc = INDEX_op_mul_vec,
10157           .load_dest = true,
10158           .vece = MO_8 },
10159         { .fni4 = gen_mls16_i32,
10160           .fniv = gen_mls_vec,
10161           .opc = INDEX_op_mul_vec,
10162           .load_dest = true,
10163           .vece = MO_16 },
10164         { .fni4 = gen_mls32_i32,
10165           .fniv = gen_mls_vec,
10166           .opc = INDEX_op_mul_vec,
10167           .load_dest = true,
10168           .vece = MO_32 },
10169         { .fni8 = gen_mls64_i64,
10170           .fniv = gen_mls_vec,
10171           .opc = INDEX_op_mul_vec,
10172           .prefer_i64 = TCG_TARGET_REG_BITS == 64,
10173           .load_dest = true,
10174           .vece = MO_64 },
10175     };
10176
10177     int is_q = extract32(insn, 30, 1);
10178     int u = extract32(insn, 29, 1);
10179     int size = extract32(insn, 22, 2);
10180     int opcode = extract32(insn, 11, 5);
10181     int rm = extract32(insn, 16, 5);
10182     int rn = extract32(insn, 5, 5);
10183     int rd = extract32(insn, 0, 5);
10184     int pass;
10185     TCGCond cond;
10186
10187     switch (opcode) {
10188     case 0x13: /* MUL, PMUL */
10189         if (u && size != 0) {
10190             unallocated_encoding(s);
10191             return;
10192         }
10193         /* fall through */
10194     case 0x0: /* SHADD, UHADD */
10195     case 0x2: /* SRHADD, URHADD */
10196     case 0x4: /* SHSUB, UHSUB */
10197     case 0xc: /* SMAX, UMAX */
10198     case 0xd: /* SMIN, UMIN */
10199     case 0xe: /* SABD, UABD */
10200     case 0xf: /* SABA, UABA */
10201     case 0x12: /* MLA, MLS */
10202         if (size == 3) {
10203             unallocated_encoding(s);
10204             return;
10205         }
10206         break;
10207     case 0x16: /* SQDMULH, SQRDMULH */
10208         if (size == 0 || size == 3) {
10209             unallocated_encoding(s);
10210             return;
10211         }
10212         break;
10213     default:
10214         if (size == 3 && !is_q) {
10215             unallocated_encoding(s);
10216             return;
10217         }
10218         break;
10219     }
10220
10221     if (!fp_access_check(s)) {
10222         return;
10223     }
10224
10225     switch (opcode) {
10226     case 0x10: /* ADD, SUB */
10227         if (u) {
10228             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
10229         } else {
10230             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
10231         }
10232         return;
10233     case 0x13: /* MUL, PMUL */
10234         if (!u) { /* MUL */
10235             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
10236             return;
10237         }
10238         break;
10239     case 0x12: /* MLA, MLS */
10240         if (u) {
10241             gen_gvec_op3(s, is_q, rd, rn, rm, &mls_op[size]);
10242         } else {
10243             gen_gvec_op3(s, is_q, rd, rn, rm, &mla_op[size]);
10244         }
10245         return;
10246     case 0x11:
10247         if (!u) { /* CMTST */
10248             gen_gvec_op3(s, is_q, rd, rn, rm, &cmtst_op[size]);
10249             return;
10250         }
10251         /* else CMEQ */
10252         cond = TCG_COND_EQ;
10253         goto do_gvec_cmp;
10254     case 0x06: /* CMGT, CMHI */
10255         cond = u ? TCG_COND_GTU : TCG_COND_GT;
10256         goto do_gvec_cmp;
10257     case 0x07: /* CMGE, CMHS */
10258         cond = u ? TCG_COND_GEU : TCG_COND_GE;
10259     do_gvec_cmp:
10260         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
10261                          vec_full_reg_offset(s, rn),
10262                          vec_full_reg_offset(s, rm),
10263                          is_q ? 16 : 8, vec_full_reg_size(s));
10264         return;
10265     }
10266
10267     if (size == 3) {
10268         assert(is_q);
10269         for (pass = 0; pass < 2; pass++) {
10270             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10271             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10272             TCGv_i64 tcg_res = tcg_temp_new_i64();
10273
10274             read_vec_element(s, tcg_op1, rn, pass, MO_64);
10275             read_vec_element(s, tcg_op2, rm, pass, MO_64);
10276
10277             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
10278
10279             write_vec_element(s, tcg_res, rd, pass, MO_64);
10280
10281             tcg_temp_free_i64(tcg_res);
10282             tcg_temp_free_i64(tcg_op1);
10283             tcg_temp_free_i64(tcg_op2);
10284         }
10285     } else {
10286         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
10287             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10288             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10289             TCGv_i32 tcg_res = tcg_temp_new_i32();
10290             NeonGenTwoOpFn *genfn = NULL;
10291             NeonGenTwoOpEnvFn *genenvfn = NULL;
10292
10293             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
10294             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
10295
10296             switch (opcode) {
10297             case 0x0: /* SHADD, UHADD */
10298             {
10299                 static NeonGenTwoOpFn * const fns[3][2] = {
10300                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
10301                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
10302                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
10303                 };
10304                 genfn = fns[size][u];
10305                 break;
10306             }
10307             case 0x1: /* SQADD, UQADD */
10308             {
10309                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
10310                     { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
10311                     { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
10312                     { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
10313                 };
10314                 genenvfn = fns[size][u];
10315                 break;
10316             }
10317             case 0x2: /* SRHADD, URHADD */
10318             {
10319                 static NeonGenTwoOpFn * const fns[3][2] = {
10320                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
10321                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
10322                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
10323                 };
10324                 genfn = fns[size][u];
10325                 break;
10326             }
10327             case 0x4: /* SHSUB, UHSUB */
10328             {
10329                 static NeonGenTwoOpFn * const fns[3][2] = {
10330                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
10331                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
10332                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
10333                 };
10334                 genfn = fns[size][u];
10335                 break;
10336             }
10337             case 0x5: /* SQSUB, UQSUB */
10338             {
10339                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
10340                     { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
10341                     { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
10342                     { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
10343                 };
10344                 genenvfn = fns[size][u];
10345                 break;
10346             }
10347             case 0x8: /* SSHL, USHL */
10348             {
10349                 static NeonGenTwoOpFn * const fns[3][2] = {
10350                     { gen_helper_neon_shl_s8, gen_helper_neon_shl_u8 },
10351                     { gen_helper_neon_shl_s16, gen_helper_neon_shl_u16 },
10352                     { gen_helper_neon_shl_s32, gen_helper_neon_shl_u32 },
10353                 };
10354                 genfn = fns[size][u];
10355                 break;
10356             }
10357             case 0x9: /* SQSHL, UQSHL */
10358             {
10359                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
10360                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
10361                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
10362                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
10363                 };
10364                 genenvfn = fns[size][u];
10365                 break;
10366             }
10367             case 0xa: /* SRSHL, URSHL */
10368             {
10369                 static NeonGenTwoOpFn * const fns[3][2] = {
10370                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
10371                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
10372                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
10373                 };
10374                 genfn = fns[size][u];
10375                 break;
10376             }
10377             case 0xb: /* SQRSHL, UQRSHL */
10378             {
10379                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
10380                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
10381                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
10382                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
10383                 };
10384                 genenvfn = fns[size][u];
10385                 break;
10386             }
10387             case 0xc: /* SMAX, UMAX */
10388             {
10389                 static NeonGenTwoOpFn * const fns[3][2] = {
10390                     { gen_helper_neon_max_s8, gen_helper_neon_max_u8 },
10391                     { gen_helper_neon_max_s16, gen_helper_neon_max_u16 },
10392                     { gen_max_s32, gen_max_u32 },
10393                 };
10394                 genfn = fns[size][u];
10395                 break;
10396             }
10397
10398             case 0xd: /* SMIN, UMIN */
10399             {
10400                 static NeonGenTwoOpFn * const fns[3][2] = {
10401                     { gen_helper_neon_min_s8, gen_helper_neon_min_u8 },
10402                     { gen_helper_neon_min_s16, gen_helper_neon_min_u16 },
10403                     { gen_min_s32, gen_min_u32 },
10404                 };
10405                 genfn = fns[size][u];
10406                 break;
10407             }
10408             case 0xe: /* SABD, UABD */
10409             case 0xf: /* SABA, UABA */
10410             {
10411                 static NeonGenTwoOpFn * const fns[3][2] = {
10412                     { gen_helper_neon_abd_s8, gen_helper_neon_abd_u8 },
10413                     { gen_helper_neon_abd_s16, gen_helper_neon_abd_u16 },
10414                     { gen_helper_neon_abd_s32, gen_helper_neon_abd_u32 },
10415                 };
10416                 genfn = fns[size][u];
10417                 break;
10418             }
10419             case 0x13: /* MUL, PMUL */
10420                 assert(u); /* PMUL */
10421                 assert(size == 0);
10422                 genfn = gen_helper_neon_mul_p8;
10423                 break;
10424             case 0x16: /* SQDMULH, SQRDMULH */
10425             {
10426                 static NeonGenTwoOpEnvFn * const fns[2][2] = {
10427                     { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
10428                     { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
10429                 };
10430                 assert(size == 1 || size == 2);
10431                 genenvfn = fns[size - 1][u];
10432                 break;
10433             }
10434             default:
10435                 g_assert_not_reached();
10436             }
10437
10438             if (genenvfn) {
10439                 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
10440             } else {
10441                 genfn(tcg_res, tcg_op1, tcg_op2);
10442             }
10443
10444             if (opcode == 0xf) {
10445                 /* SABA, UABA: accumulating ops */
10446                 static NeonGenTwoOpFn * const fns[3] = {
10447                     gen_helper_neon_add_u8,
10448                     gen_helper_neon_add_u16,
10449                     tcg_gen_add_i32,
10450                 };
10451
10452                 read_vec_element_i32(s, tcg_op1, rd, pass, MO_32);
10453                 fns[size](tcg_res, tcg_op1, tcg_res);
10454             }
10455
10456             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10457
10458             tcg_temp_free_i32(tcg_res);
10459             tcg_temp_free_i32(tcg_op1);
10460             tcg_temp_free_i32(tcg_op2);
10461         }
10462     }
10463     clear_vec_high(s, is_q, rd);
10464 }
10465
10466 /* AdvSIMD three same
10467  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
10468  * +---+---+---+-----------+------+---+------+--------+---+------+------+
10469  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
10470  * +---+---+---+-----------+------+---+------+--------+---+------+------+
10471  */
10472 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
10473 {
10474     int opcode = extract32(insn, 11, 5);
10475
10476     switch (opcode) {
10477     case 0x3: /* logic ops */
10478         disas_simd_3same_logic(s, insn);
10479         break;
10480     case 0x17: /* ADDP */
10481     case 0x14: /* SMAXP, UMAXP */
10482     case 0x15: /* SMINP, UMINP */
10483     {
10484         /* Pairwise operations */
10485         int is_q = extract32(insn, 30, 1);
10486         int u = extract32(insn, 29, 1);
10487         int size = extract32(insn, 22, 2);
10488         int rm = extract32(insn, 16, 5);
10489         int rn = extract32(insn, 5, 5);
10490         int rd = extract32(insn, 0, 5);
10491         if (opcode == 0x17) {
10492             if (u || (size == 3 && !is_q)) {
10493                 unallocated_encoding(s);
10494                 return;
10495             }
10496         } else {
10497             if (size == 3) {
10498                 unallocated_encoding(s);
10499                 return;
10500             }
10501         }
10502         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
10503         break;
10504     }
10505     case 0x18 ... 0x31:
10506         /* floating point ops, sz[1] and U are part of opcode */
10507         disas_simd_3same_float(s, insn);
10508         break;
10509     default:
10510         disas_simd_3same_int(s, insn);
10511         break;
10512     }
10513 }
10514
10515 /*
10516  * Advanced SIMD three same (ARMv8.2 FP16 variants)
10517  *
10518  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
10519  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
10520  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
10521  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
10522  *
10523  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
10524  * (register), FACGE, FABD, FCMGT (register) and FACGT.
10525  *
10526  */
10527 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
10528 {
10529     int opcode, fpopcode;
10530     int is_q, u, a, rm, rn, rd;
10531     int datasize, elements;
10532     int pass;
10533     TCGv_ptr fpst;
10534     bool pairwise = false;
10535
10536     if (!arm_dc_feature(s, ARM_FEATURE_V8_FP16)) {
10537         unallocated_encoding(s);
10538         return;
10539     }
10540
10541     if (!fp_access_check(s)) {
10542         return;
10543     }
10544
10545     /* For these floating point ops, the U, a and opcode bits
10546      * together indicate the operation.
10547      */
10548     opcode = extract32(insn, 11, 3);
10549     u = extract32(insn, 29, 1);
10550     a = extract32(insn, 23, 1);
10551     is_q = extract32(insn, 30, 1);
10552     rm = extract32(insn, 16, 5);
10553     rn = extract32(insn, 5, 5);
10554     rd = extract32(insn, 0, 5);
10555
10556     fpopcode = opcode | (a << 3) |  (u << 4);
10557     datasize = is_q ? 128 : 64;
10558     elements = datasize / 16;
10559
10560     switch (fpopcode) {
10561     case 0x10: /* FMAXNMP */
10562     case 0x12: /* FADDP */
10563     case 0x16: /* FMAXP */
10564     case 0x18: /* FMINNMP */
10565     case 0x1e: /* FMINP */
10566         pairwise = true;
10567         break;
10568     }
10569
10570     fpst = get_fpstatus_ptr(true);
10571
10572     if (pairwise) {
10573         int maxpass = is_q ? 8 : 4;
10574         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10575         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10576         TCGv_i32 tcg_res[8];
10577
10578         for (pass = 0; pass < maxpass; pass++) {
10579             int passreg = pass < (maxpass / 2) ? rn : rm;
10580             int passelt = (pass << 1) & (maxpass - 1);
10581
10582             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
10583             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
10584             tcg_res[pass] = tcg_temp_new_i32();
10585
10586             switch (fpopcode) {
10587             case 0x10: /* FMAXNMP */
10588                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
10589                                            fpst);
10590                 break;
10591             case 0x12: /* FADDP */
10592                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10593                 break;
10594             case 0x16: /* FMAXP */
10595                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10596                 break;
10597             case 0x18: /* FMINNMP */
10598                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
10599                                            fpst);
10600                 break;
10601             case 0x1e: /* FMINP */
10602                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10603                 break;
10604             default:
10605                 g_assert_not_reached();
10606             }
10607         }
10608
10609         for (pass = 0; pass < maxpass; pass++) {
10610             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
10611             tcg_temp_free_i32(tcg_res[pass]);
10612         }
10613
10614         tcg_temp_free_i32(tcg_op1);
10615         tcg_temp_free_i32(tcg_op2);
10616
10617     } else {
10618         for (pass = 0; pass < elements; pass++) {
10619             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10620             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10621             TCGv_i32 tcg_res = tcg_temp_new_i32();
10622
10623             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
10624             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
10625
10626             switch (fpopcode) {
10627             case 0x0: /* FMAXNM */
10628                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
10629                 break;
10630             case 0x1: /* FMLA */
10631                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
10632                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
10633                                            fpst);
10634                 break;
10635             case 0x2: /* FADD */
10636                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
10637                 break;
10638             case 0x3: /* FMULX */
10639                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
10640                 break;
10641             case 0x4: /* FCMEQ */
10642                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
10643                 break;
10644             case 0x6: /* FMAX */
10645                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
10646                 break;
10647             case 0x7: /* FRECPS */
10648                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
10649                 break;
10650             case 0x8: /* FMINNM */
10651                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
10652                 break;
10653             case 0x9: /* FMLS */
10654                 /* As usual for ARM, separate negation for fused multiply-add */
10655                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
10656                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
10657                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
10658                                            fpst);
10659                 break;
10660             case 0xa: /* FSUB */
10661                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
10662                 break;
10663             case 0xe: /* FMIN */
10664                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
10665                 break;
10666             case 0xf: /* FRSQRTS */
10667                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
10668                 break;
10669             case 0x13: /* FMUL */
10670                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
10671                 break;
10672             case 0x14: /* FCMGE */
10673                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
10674                 break;
10675             case 0x15: /* FACGE */
10676                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
10677                 break;
10678             case 0x17: /* FDIV */
10679                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
10680                 break;
10681             case 0x1a: /* FABD */
10682                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
10683                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
10684                 break;
10685             case 0x1c: /* FCMGT */
10686                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
10687                 break;
10688             case 0x1d: /* FACGT */
10689                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
10690                 break;
10691             default:
10692                 fprintf(stderr, "%s: insn %#04x, fpop %#2x @ %#" PRIx64 "\n",
10693                         __func__, insn, fpopcode, s->pc);
10694                 g_assert_not_reached();
10695             }
10696
10697             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
10698             tcg_temp_free_i32(tcg_res);
10699             tcg_temp_free_i32(tcg_op1);
10700             tcg_temp_free_i32(tcg_op2);
10701         }
10702     }
10703
10704     tcg_temp_free_ptr(fpst);
10705
10706     clear_vec_high(s, is_q, rd);
10707 }
10708
10709 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
10710                                   int size, int rn, int rd)
10711 {
10712     /* Handle 2-reg-misc ops which are widening (so each size element
10713      * in the source becomes a 2*size element in the destination.
10714      * The only instruction like this is FCVTL.
10715      */
10716     int pass;
10717
10718     if (size == 3) {
10719         /* 32 -> 64 bit fp conversion */
10720         TCGv_i64 tcg_res[2];
10721         int srcelt = is_q ? 2 : 0;
10722
10723         for (pass = 0; pass < 2; pass++) {
10724             TCGv_i32 tcg_op = tcg_temp_new_i32();
10725             tcg_res[pass] = tcg_temp_new_i64();
10726
10727             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
10728             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
10729             tcg_temp_free_i32(tcg_op);
10730         }
10731         for (pass = 0; pass < 2; pass++) {
10732             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10733             tcg_temp_free_i64(tcg_res[pass]);
10734         }
10735     } else {
10736         /* 16 -> 32 bit fp conversion */
10737         int srcelt = is_q ? 4 : 0;
10738         TCGv_i32 tcg_res[4];
10739
10740         for (pass = 0; pass < 4; pass++) {
10741             tcg_res[pass] = tcg_temp_new_i32();
10742
10743             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
10744             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
10745                                            cpu_env);
10746         }
10747         for (pass = 0; pass < 4; pass++) {
10748             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
10749             tcg_temp_free_i32(tcg_res[pass]);
10750         }
10751     }
10752 }
10753
10754 static void handle_rev(DisasContext *s, int opcode, bool u,
10755                        bool is_q, int size, int rn, int rd)
10756 {
10757     int op = (opcode << 1) | u;
10758     int opsz = op + size;
10759     int grp_size = 3 - opsz;
10760     int dsize = is_q ? 128 : 64;
10761     int i;
10762
10763     if (opsz >= 3) {
10764         unallocated_encoding(s);
10765         return;
10766     }
10767
10768     if (!fp_access_check(s)) {
10769         return;
10770     }
10771
10772     if (size == 0) {
10773         /* Special case bytes, use bswap op on each group of elements */
10774         int groups = dsize / (8 << grp_size);
10775
10776         for (i = 0; i < groups; i++) {
10777             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
10778
10779             read_vec_element(s, tcg_tmp, rn, i, grp_size);
10780             switch (grp_size) {
10781             case MO_16:
10782                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
10783                 break;
10784             case MO_32:
10785                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
10786                 break;
10787             case MO_64:
10788                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
10789                 break;
10790             default:
10791                 g_assert_not_reached();
10792             }
10793             write_vec_element(s, tcg_tmp, rd, i, grp_size);
10794             tcg_temp_free_i64(tcg_tmp);
10795         }
10796         clear_vec_high(s, is_q, rd);
10797     } else {
10798         int revmask = (1 << grp_size) - 1;
10799         int esize = 8 << size;
10800         int elements = dsize / esize;
10801         TCGv_i64 tcg_rn = tcg_temp_new_i64();
10802         TCGv_i64 tcg_rd = tcg_const_i64(0);
10803         TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
10804
10805         for (i = 0; i < elements; i++) {
10806             int e_rev = (i & 0xf) ^ revmask;
10807             int off = e_rev * esize;
10808             read_vec_element(s, tcg_rn, rn, i, size);
10809             if (off >= 64) {
10810                 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
10811                                     tcg_rn, off - 64, esize);
10812             } else {
10813                 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
10814             }
10815         }
10816         write_vec_element(s, tcg_rd, rd, 0, MO_64);
10817         write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
10818
10819         tcg_temp_free_i64(tcg_rd_hi);
10820         tcg_temp_free_i64(tcg_rd);
10821         tcg_temp_free_i64(tcg_rn);
10822     }
10823 }
10824
10825 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
10826                                   bool is_q, int size, int rn, int rd)
10827 {
10828     /* Implement the pairwise operations from 2-misc:
10829      * SADDLP, UADDLP, SADALP, UADALP.
10830      * These all add pairs of elements in the input to produce a
10831      * double-width result element in the output (possibly accumulating).
10832      */
10833     bool accum = (opcode == 0x6);
10834     int maxpass = is_q ? 2 : 1;
10835     int pass;
10836     TCGv_i64 tcg_res[2];
10837
10838     if (size == 2) {
10839         /* 32 + 32 -> 64 op */
10840         TCGMemOp memop = size + (u ? 0 : MO_SIGN);
10841
10842         for (pass = 0; pass < maxpass; pass++) {
10843             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10844             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10845
10846             tcg_res[pass] = tcg_temp_new_i64();
10847
10848             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
10849             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
10850             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
10851             if (accum) {
10852                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
10853                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
10854             }
10855
10856             tcg_temp_free_i64(tcg_op1);
10857             tcg_temp_free_i64(tcg_op2);
10858         }
10859     } else {
10860         for (pass = 0; pass < maxpass; pass++) {
10861             TCGv_i64 tcg_op = tcg_temp_new_i64();
10862             NeonGenOneOpFn *genfn;
10863             static NeonGenOneOpFn * const fns[2][2] = {
10864                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
10865                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
10866             };
10867
10868             genfn = fns[size][u];
10869
10870             tcg_res[pass] = tcg_temp_new_i64();
10871
10872             read_vec_element(s, tcg_op, rn, pass, MO_64);
10873             genfn(tcg_res[pass], tcg_op);
10874
10875             if (accum) {
10876                 read_vec_element(s, tcg_op, rd, pass, MO_64);
10877                 if (size == 0) {
10878                     gen_helper_neon_addl_u16(tcg_res[pass],
10879                                              tcg_res[pass], tcg_op);
10880                 } else {
10881                     gen_helper_neon_addl_u32(tcg_res[pass],
10882                                              tcg_res[pass], tcg_op);
10883                 }
10884             }
10885             tcg_temp_free_i64(tcg_op);
10886         }
10887     }
10888     if (!is_q) {
10889         tcg_res[1] = tcg_const_i64(0);
10890     }
10891     for (pass = 0; pass < 2; pass++) {
10892         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10893         tcg_temp_free_i64(tcg_res[pass]);
10894     }
10895 }
10896
10897 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
10898 {
10899     /* Implement SHLL and SHLL2 */
10900     int pass;
10901     int part = is_q ? 2 : 0;
10902     TCGv_i64 tcg_res[2];
10903
10904     for (pass = 0; pass < 2; pass++) {
10905         static NeonGenWidenFn * const widenfns[3] = {
10906             gen_helper_neon_widen_u8,
10907             gen_helper_neon_widen_u16,
10908             tcg_gen_extu_i32_i64,
10909         };
10910         NeonGenWidenFn *widenfn = widenfns[size];
10911         TCGv_i32 tcg_op = tcg_temp_new_i32();
10912
10913         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
10914         tcg_res[pass] = tcg_temp_new_i64();
10915         widenfn(tcg_res[pass], tcg_op);
10916         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
10917
10918         tcg_temp_free_i32(tcg_op);
10919     }
10920
10921     for (pass = 0; pass < 2; pass++) {
10922         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10923         tcg_temp_free_i64(tcg_res[pass]);
10924     }
10925 }
10926
10927 /* AdvSIMD two reg misc
10928  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
10929  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
10930  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
10931  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
10932  */
10933 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
10934 {
10935     int size = extract32(insn, 22, 2);
10936     int opcode = extract32(insn, 12, 5);
10937     bool u = extract32(insn, 29, 1);
10938     bool is_q = extract32(insn, 30, 1);
10939     int rn = extract32(insn, 5, 5);
10940     int rd = extract32(insn, 0, 5);
10941     bool need_fpstatus = false;
10942     bool need_rmode = false;
10943     int rmode = -1;
10944     TCGv_i32 tcg_rmode;
10945     TCGv_ptr tcg_fpstatus;
10946
10947     switch (opcode) {
10948     case 0x0: /* REV64, REV32 */
10949     case 0x1: /* REV16 */
10950         handle_rev(s, opcode, u, is_q, size, rn, rd);
10951         return;
10952     case 0x5: /* CNT, NOT, RBIT */
10953         if (u && size == 0) {
10954             /* NOT */
10955             break;
10956         } else if (u && size == 1) {
10957             /* RBIT */
10958             break;
10959         } else if (!u && size == 0) {
10960             /* CNT */
10961             break;
10962         }
10963         unallocated_encoding(s);
10964         return;
10965     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
10966     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
10967         if (size == 3) {
10968             unallocated_encoding(s);
10969             return;
10970         }
10971         if (!fp_access_check(s)) {
10972             return;
10973         }
10974
10975         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
10976         return;
10977     case 0x4: /* CLS, CLZ */
10978         if (size == 3) {
10979             unallocated_encoding(s);
10980             return;
10981         }
10982         break;
10983     case 0x2: /* SADDLP, UADDLP */
10984     case 0x6: /* SADALP, UADALP */
10985         if (size == 3) {
10986             unallocated_encoding(s);
10987             return;
10988         }
10989         if (!fp_access_check(s)) {
10990             return;
10991         }
10992         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
10993         return;
10994     case 0x13: /* SHLL, SHLL2 */
10995         if (u == 0 || size == 3) {
10996             unallocated_encoding(s);
10997             return;
10998         }
10999         if (!fp_access_check(s)) {
11000             return;
11001         }
11002         handle_shll(s, is_q, size, rn, rd);
11003         return;
11004     case 0xa: /* CMLT */
11005         if (u == 1) {
11006             unallocated_encoding(s);
11007             return;
11008         }
11009         /* fall through */
11010     case 0x8: /* CMGT, CMGE */
11011     case 0x9: /* CMEQ, CMLE */
11012     case 0xb: /* ABS, NEG */
11013         if (size == 3 && !is_q) {
11014             unallocated_encoding(s);
11015             return;
11016         }
11017         break;
11018     case 0x3: /* SUQADD, USQADD */
11019         if (size == 3 && !is_q) {
11020             unallocated_encoding(s);
11021             return;
11022         }
11023         if (!fp_access_check(s)) {
11024             return;
11025         }
11026         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
11027         return;
11028     case 0x7: /* SQABS, SQNEG */
11029         if (size == 3 && !is_q) {
11030             unallocated_encoding(s);
11031             return;
11032         }
11033         break;
11034     case 0xc ... 0xf:
11035     case 0x16 ... 0x1d:
11036     case 0x1f:
11037     {
11038         /* Floating point: U, size[1] and opcode indicate operation;
11039          * size[0] indicates single or double precision.
11040          */
11041         int is_double = extract32(size, 0, 1);
11042         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
11043         size = is_double ? 3 : 2;
11044         switch (opcode) {
11045         case 0x2f: /* FABS */
11046         case 0x6f: /* FNEG */
11047             if (size == 3 && !is_q) {
11048                 unallocated_encoding(s);
11049                 return;
11050             }
11051             break;
11052         case 0x1d: /* SCVTF */
11053         case 0x5d: /* UCVTF */
11054         {
11055             bool is_signed = (opcode == 0x1d) ? true : false;
11056             int elements = is_double ? 2 : is_q ? 4 : 2;
11057             if (is_double && !is_q) {
11058                 unallocated_encoding(s);
11059                 return;
11060             }
11061             if (!fp_access_check(s)) {
11062                 return;
11063             }
11064             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
11065             return;
11066         }
11067         case 0x2c: /* FCMGT (zero) */
11068         case 0x2d: /* FCMEQ (zero) */
11069         case 0x2e: /* FCMLT (zero) */
11070         case 0x6c: /* FCMGE (zero) */
11071         case 0x6d: /* FCMLE (zero) */
11072             if (size == 3 && !is_q) {
11073                 unallocated_encoding(s);
11074                 return;
11075             }
11076             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
11077             return;
11078         case 0x7f: /* FSQRT */
11079             if (size == 3 && !is_q) {
11080                 unallocated_encoding(s);
11081                 return;
11082             }
11083             break;
11084         case 0x1a: /* FCVTNS */
11085         case 0x1b: /* FCVTMS */
11086         case 0x3a: /* FCVTPS */
11087         case 0x3b: /* FCVTZS */
11088         case 0x5a: /* FCVTNU */
11089         case 0x5b: /* FCVTMU */
11090         case 0x7a: /* FCVTPU */
11091         case 0x7b: /* FCVTZU */
11092             need_fpstatus = true;
11093             need_rmode = true;
11094             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
11095             if (size == 3 && !is_q) {
11096                 unallocated_encoding(s);
11097                 return;
11098             }
11099             break;
11100         case 0x5c: /* FCVTAU */
11101         case 0x1c: /* FCVTAS */
11102             need_fpstatus = true;
11103             need_rmode = true;
11104             rmode = FPROUNDING_TIEAWAY;
11105             if (size == 3 && !is_q) {
11106                 unallocated_encoding(s);
11107                 return;
11108             }
11109             break;
11110         case 0x3c: /* URECPE */
11111             if (size == 3) {
11112                 unallocated_encoding(s);
11113                 return;
11114             }
11115             /* fall through */
11116         case 0x3d: /* FRECPE */
11117         case 0x7d: /* FRSQRTE */
11118             if (size == 3 && !is_q) {
11119                 unallocated_encoding(s);
11120                 return;
11121             }
11122             if (!fp_access_check(s)) {
11123                 return;
11124             }
11125             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
11126             return;
11127         case 0x56: /* FCVTXN, FCVTXN2 */
11128             if (size == 2) {
11129                 unallocated_encoding(s);
11130                 return;
11131             }
11132             /* fall through */
11133         case 0x16: /* FCVTN, FCVTN2 */
11134             /* handle_2misc_narrow does a 2*size -> size operation, but these
11135              * instructions encode the source size rather than dest size.
11136              */
11137             if (!fp_access_check(s)) {
11138                 return;
11139             }
11140             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
11141             return;
11142         case 0x17: /* FCVTL, FCVTL2 */
11143             if (!fp_access_check(s)) {
11144                 return;
11145             }
11146             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
11147             return;
11148         case 0x18: /* FRINTN */
11149         case 0x19: /* FRINTM */
11150         case 0x38: /* FRINTP */
11151         case 0x39: /* FRINTZ */
11152             need_rmode = true;
11153             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
11154             /* fall through */
11155         case 0x59: /* FRINTX */
11156         case 0x79: /* FRINTI */
11157             need_fpstatus = true;
11158             if (size == 3 && !is_q) {
11159                 unallocated_encoding(s);
11160                 return;
11161             }
11162             break;
11163         case 0x58: /* FRINTA */
11164             need_rmode = true;
11165             rmode = FPROUNDING_TIEAWAY;
11166             need_fpstatus = true;
11167             if (size == 3 && !is_q) {
11168                 unallocated_encoding(s);
11169                 return;
11170             }
11171             break;
11172         case 0x7c: /* URSQRTE */
11173             if (size == 3) {
11174                 unallocated_encoding(s);
11175                 return;
11176             }
11177             need_fpstatus = true;
11178             break;
11179         default:
11180             unallocated_encoding(s);
11181             return;
11182         }
11183         break;
11184     }
11185     default:
11186         unallocated_encoding(s);
11187         return;
11188     }
11189
11190     if (!fp_access_check(s)) {
11191         return;
11192     }
11193
11194     if (need_fpstatus || need_rmode) {
11195         tcg_fpstatus = get_fpstatus_ptr(false);
11196     } else {
11197         tcg_fpstatus = NULL;
11198     }
11199     if (need_rmode) {
11200         tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
11201         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
11202     } else {
11203         tcg_rmode = NULL;
11204     }
11205
11206     switch (opcode) {
11207     case 0x5:
11208         if (u && size == 0) { /* NOT */
11209             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
11210             return;
11211         }
11212         break;
11213     case 0xb:
11214         if (u) { /* NEG */
11215             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
11216             return;
11217         }
11218         break;
11219     }
11220
11221     if (size == 3) {
11222         /* All 64-bit element operations can be shared with scalar 2misc */
11223         int pass;
11224
11225         for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
11226             TCGv_i64 tcg_op = tcg_temp_new_i64();
11227             TCGv_i64 tcg_res = tcg_temp_new_i64();
11228
11229             read_vec_element(s, tcg_op, rn, pass, MO_64);
11230
11231             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
11232                             tcg_rmode, tcg_fpstatus);
11233
11234             write_vec_element(s, tcg_res, rd, pass, MO_64);
11235
11236             tcg_temp_free_i64(tcg_res);
11237             tcg_temp_free_i64(tcg_op);
11238         }
11239     } else {
11240         int pass;
11241
11242         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11243             TCGv_i32 tcg_op = tcg_temp_new_i32();
11244             TCGv_i32 tcg_res = tcg_temp_new_i32();
11245             TCGCond cond;
11246
11247             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
11248
11249             if (size == 2) {
11250                 /* Special cases for 32 bit elements */
11251                 switch (opcode) {
11252                 case 0xa: /* CMLT */
11253                     /* 32 bit integer comparison against zero, result is
11254                      * test ? (2^32 - 1) : 0. We implement via setcond(test)
11255                      * and inverting.
11256                      */
11257                     cond = TCG_COND_LT;
11258                 do_cmop:
11259                     tcg_gen_setcondi_i32(cond, tcg_res, tcg_op, 0);
11260                     tcg_gen_neg_i32(tcg_res, tcg_res);
11261                     break;
11262                 case 0x8: /* CMGT, CMGE */
11263                     cond = u ? TCG_COND_GE : TCG_COND_GT;
11264                     goto do_cmop;
11265                 case 0x9: /* CMEQ, CMLE */
11266                     cond = u ? TCG_COND_LE : TCG_COND_EQ;
11267                     goto do_cmop;
11268                 case 0x4: /* CLS */
11269                     if (u) {
11270                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
11271                     } else {
11272                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
11273                     }
11274                     break;
11275                 case 0x7: /* SQABS, SQNEG */
11276                     if (u) {
11277                         gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
11278                     } else {
11279                         gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
11280                     }
11281                     break;
11282                 case 0xb: /* ABS, NEG */
11283                     if (u) {
11284                         tcg_gen_neg_i32(tcg_res, tcg_op);
11285                     } else {
11286                         TCGv_i32 tcg_zero = tcg_const_i32(0);
11287                         tcg_gen_neg_i32(tcg_res, tcg_op);
11288                         tcg_gen_movcond_i32(TCG_COND_GT, tcg_res, tcg_op,
11289                                             tcg_zero, tcg_op, tcg_res);
11290                         tcg_temp_free_i32(tcg_zero);
11291                     }
11292                     break;
11293                 case 0x2f: /* FABS */
11294                     gen_helper_vfp_abss(tcg_res, tcg_op);
11295                     break;
11296                 case 0x6f: /* FNEG */
11297                     gen_helper_vfp_negs(tcg_res, tcg_op);
11298                     break;
11299                 case 0x7f: /* FSQRT */
11300                     gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
11301                     break;
11302                 case 0x1a: /* FCVTNS */
11303                 case 0x1b: /* FCVTMS */
11304                 case 0x1c: /* FCVTAS */
11305                 case 0x3a: /* FCVTPS */
11306                 case 0x3b: /* FCVTZS */
11307                 {
11308                     TCGv_i32 tcg_shift = tcg_const_i32(0);
11309                     gen_helper_vfp_tosls(tcg_res, tcg_op,
11310                                          tcg_shift, tcg_fpstatus);
11311                     tcg_temp_free_i32(tcg_shift);
11312                     break;
11313                 }
11314                 case 0x5a: /* FCVTNU */
11315                 case 0x5b: /* FCVTMU */
11316                 case 0x5c: /* FCVTAU */
11317                 case 0x7a: /* FCVTPU */
11318                 case 0x7b: /* FCVTZU */
11319                 {
11320                     TCGv_i32 tcg_shift = tcg_const_i32(0);
11321                     gen_helper_vfp_touls(tcg_res, tcg_op,
11322                                          tcg_shift, tcg_fpstatus);
11323                     tcg_temp_free_i32(tcg_shift);
11324                     break;
11325                 }
11326                 case 0x18: /* FRINTN */
11327                 case 0x19: /* FRINTM */
11328                 case 0x38: /* FRINTP */
11329                 case 0x39: /* FRINTZ */
11330                 case 0x58: /* FRINTA */
11331                 case 0x79: /* FRINTI */
11332                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
11333                     break;
11334                 case 0x59: /* FRINTX */
11335                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
11336                     break;
11337                 case 0x7c: /* URSQRTE */
11338                     gen_helper_rsqrte_u32(tcg_res, tcg_op, tcg_fpstatus);
11339                     break;
11340                 default:
11341                     g_assert_not_reached();
11342                 }
11343             } else {
11344                 /* Use helpers for 8 and 16 bit elements */
11345                 switch (opcode) {
11346                 case 0x5: /* CNT, RBIT */
11347                     /* For these two insns size is part of the opcode specifier
11348                      * (handled earlier); they always operate on byte elements.
11349                      */
11350                     if (u) {
11351                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
11352                     } else {
11353                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
11354                     }
11355                     break;
11356                 case 0x7: /* SQABS, SQNEG */
11357                 {
11358                     NeonGenOneOpEnvFn *genfn;
11359                     static NeonGenOneOpEnvFn * const fns[2][2] = {
11360                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
11361                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
11362                     };
11363                     genfn = fns[size][u];
11364                     genfn(tcg_res, cpu_env, tcg_op);
11365                     break;
11366                 }
11367                 case 0x8: /* CMGT, CMGE */
11368                 case 0x9: /* CMEQ, CMLE */
11369                 case 0xa: /* CMLT */
11370                 {
11371                     static NeonGenTwoOpFn * const fns[3][2] = {
11372                         { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_s16 },
11373                         { gen_helper_neon_cge_s8, gen_helper_neon_cge_s16 },
11374                         { gen_helper_neon_ceq_u8, gen_helper_neon_ceq_u16 },
11375                     };
11376                     NeonGenTwoOpFn *genfn;
11377                     int comp;
11378                     bool reverse;
11379                     TCGv_i32 tcg_zero = tcg_const_i32(0);
11380
11381                     /* comp = index into [CMGT, CMGE, CMEQ, CMLE, CMLT] */
11382                     comp = (opcode - 0x8) * 2 + u;
11383                     /* ...but LE, LT are implemented as reverse GE, GT */
11384                     reverse = (comp > 2);
11385                     if (reverse) {
11386                         comp = 4 - comp;
11387                     }
11388                     genfn = fns[comp][size];
11389                     if (reverse) {
11390                         genfn(tcg_res, tcg_zero, tcg_op);
11391                     } else {
11392                         genfn(tcg_res, tcg_op, tcg_zero);
11393                     }
11394                     tcg_temp_free_i32(tcg_zero);
11395                     break;
11396                 }
11397                 case 0xb: /* ABS, NEG */
11398                     if (u) {
11399                         TCGv_i32 tcg_zero = tcg_const_i32(0);
11400                         if (size) {
11401                             gen_helper_neon_sub_u16(tcg_res, tcg_zero, tcg_op);
11402                         } else {
11403                             gen_helper_neon_sub_u8(tcg_res, tcg_zero, tcg_op);
11404                         }
11405                         tcg_temp_free_i32(tcg_zero);
11406                     } else {
11407                         if (size) {
11408                             gen_helper_neon_abs_s16(tcg_res, tcg_op);
11409                         } else {
11410                             gen_helper_neon_abs_s8(tcg_res, tcg_op);
11411                         }
11412                     }
11413                     break;
11414                 case 0x4: /* CLS, CLZ */
11415                     if (u) {
11416                         if (size == 0) {
11417                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
11418                         } else {
11419                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
11420                         }
11421                     } else {
11422                         if (size == 0) {
11423                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
11424                         } else {
11425                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
11426                         }
11427                     }
11428                     break;
11429                 default:
11430                     g_assert_not_reached();
11431                 }
11432             }
11433
11434             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11435
11436             tcg_temp_free_i32(tcg_res);
11437             tcg_temp_free_i32(tcg_op);
11438         }
11439     }
11440     clear_vec_high(s, is_q, rd);
11441
11442     if (need_rmode) {
11443         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
11444         tcg_temp_free_i32(tcg_rmode);
11445     }
11446     if (need_fpstatus) {
11447         tcg_temp_free_ptr(tcg_fpstatus);
11448     }
11449 }
11450
11451 /* AdvSIMD [scalar] two register miscellaneous (FP16)
11452  *
11453  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
11454  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
11455  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
11456  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
11457  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
11458  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
11459  *
11460  * This actually covers two groups where scalar access is governed by
11461  * bit 28. A bunch of the instructions (float to integral) only exist
11462  * in the vector form and are un-allocated for the scalar decode. Also
11463  * in the scalar decode Q is always 1.
11464  */
11465 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
11466 {
11467     int fpop, opcode, a, u;
11468     int rn, rd;
11469     bool is_q;
11470     bool is_scalar;
11471     bool only_in_vector = false;
11472
11473     int pass;
11474     TCGv_i32 tcg_rmode = NULL;
11475     TCGv_ptr tcg_fpstatus = NULL;
11476     bool need_rmode = false;
11477     bool need_fpst = true;
11478     int rmode;
11479
11480     if (!arm_dc_feature(s, ARM_FEATURE_V8_FP16)) {
11481         unallocated_encoding(s);
11482         return;
11483     }
11484
11485     rd = extract32(insn, 0, 5);
11486     rn = extract32(insn, 5, 5);
11487
11488     a = extract32(insn, 23, 1);
11489     u = extract32(insn, 29, 1);
11490     is_scalar = extract32(insn, 28, 1);
11491     is_q = extract32(insn, 30, 1);
11492
11493     opcode = extract32(insn, 12, 5);
11494     fpop = deposit32(opcode, 5, 1, a);
11495     fpop = deposit32(fpop, 6, 1, u);
11496
11497     rd = extract32(insn, 0, 5);
11498     rn = extract32(insn, 5, 5);
11499
11500     switch (fpop) {
11501     case 0x1d: /* SCVTF */
11502     case 0x5d: /* UCVTF */
11503     {
11504         int elements;
11505
11506         if (is_scalar) {
11507             elements = 1;
11508         } else {
11509             elements = (is_q ? 8 : 4);
11510         }
11511
11512         if (!fp_access_check(s)) {
11513             return;
11514         }
11515         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
11516         return;
11517     }
11518     break;
11519     case 0x2c: /* FCMGT (zero) */
11520     case 0x2d: /* FCMEQ (zero) */
11521     case 0x2e: /* FCMLT (zero) */
11522     case 0x6c: /* FCMGE (zero) */
11523     case 0x6d: /* FCMLE (zero) */
11524         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
11525         return;
11526     case 0x3d: /* FRECPE */
11527     case 0x3f: /* FRECPX */
11528         break;
11529     case 0x18: /* FRINTN */
11530         need_rmode = true;
11531         only_in_vector = true;
11532         rmode = FPROUNDING_TIEEVEN;
11533         break;
11534     case 0x19: /* FRINTM */
11535         need_rmode = true;
11536         only_in_vector = true;
11537         rmode = FPROUNDING_NEGINF;
11538         break;
11539     case 0x38: /* FRINTP */
11540         need_rmode = true;
11541         only_in_vector = true;
11542         rmode = FPROUNDING_POSINF;
11543         break;
11544     case 0x39: /* FRINTZ */
11545         need_rmode = true;
11546         only_in_vector = true;
11547         rmode = FPROUNDING_ZERO;
11548         break;
11549     case 0x58: /* FRINTA */
11550         need_rmode = true;
11551         only_in_vector = true;
11552         rmode = FPROUNDING_TIEAWAY;
11553         break;
11554     case 0x59: /* FRINTX */
11555     case 0x79: /* FRINTI */
11556         only_in_vector = true;
11557         /* current rounding mode */
11558         break;
11559     case 0x1a: /* FCVTNS */
11560         need_rmode = true;
11561         rmode = FPROUNDING_TIEEVEN;
11562         break;
11563     case 0x1b: /* FCVTMS */
11564         need_rmode = true;
11565         rmode = FPROUNDING_NEGINF;
11566         break;
11567     case 0x1c: /* FCVTAS */
11568         need_rmode = true;
11569         rmode = FPROUNDING_TIEAWAY;
11570         break;
11571     case 0x3a: /* FCVTPS */
11572         need_rmode = true;
11573         rmode = FPROUNDING_POSINF;
11574         break;
11575     case 0x3b: /* FCVTZS */
11576         need_rmode = true;
11577         rmode = FPROUNDING_ZERO;
11578         break;
11579     case 0x5a: /* FCVTNU */
11580         need_rmode = true;
11581         rmode = FPROUNDING_TIEEVEN;
11582         break;
11583     case 0x5b: /* FCVTMU */
11584         need_rmode = true;
11585         rmode = FPROUNDING_NEGINF;
11586         break;
11587     case 0x5c: /* FCVTAU */
11588         need_rmode = true;
11589         rmode = FPROUNDING_TIEAWAY;
11590         break;
11591     case 0x7a: /* FCVTPU */
11592         need_rmode = true;
11593         rmode = FPROUNDING_POSINF;
11594         break;
11595     case 0x7b: /* FCVTZU */
11596         need_rmode = true;
11597         rmode = FPROUNDING_ZERO;
11598         break;
11599     case 0x2f: /* FABS */
11600     case 0x6f: /* FNEG */
11601         need_fpst = false;
11602         break;
11603     case 0x7d: /* FRSQRTE */
11604     case 0x7f: /* FSQRT (vector) */
11605         break;
11606     default:
11607         fprintf(stderr, "%s: insn %#04x fpop %#2x\n", __func__, insn, fpop);
11608         g_assert_not_reached();
11609     }
11610
11611
11612     /* Check additional constraints for the scalar encoding */
11613     if (is_scalar) {
11614         if (!is_q) {
11615             unallocated_encoding(s);
11616             return;
11617         }
11618         /* FRINTxx is only in the vector form */
11619         if (only_in_vector) {
11620             unallocated_encoding(s);
11621             return;
11622         }
11623     }
11624
11625     if (!fp_access_check(s)) {
11626         return;
11627     }
11628
11629     if (need_rmode || need_fpst) {
11630         tcg_fpstatus = get_fpstatus_ptr(true);
11631     }
11632
11633     if (need_rmode) {
11634         tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
11635         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
11636     }
11637
11638     if (is_scalar) {
11639         TCGv_i32 tcg_op = tcg_temp_new_i32();
11640         TCGv_i32 tcg_res = tcg_temp_new_i32();
11641
11642         read_vec_element_i32(s, tcg_op, rn, 0, MO_16);
11643
11644         switch (fpop) {
11645         case 0x1a: /* FCVTNS */
11646         case 0x1b: /* FCVTMS */
11647         case 0x1c: /* FCVTAS */
11648         case 0x3a: /* FCVTPS */
11649         case 0x3b: /* FCVTZS */
11650             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
11651             break;
11652         case 0x3d: /* FRECPE */
11653             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
11654             break;
11655         case 0x3f: /* FRECPX */
11656             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
11657             break;
11658         case 0x5a: /* FCVTNU */
11659         case 0x5b: /* FCVTMU */
11660         case 0x5c: /* FCVTAU */
11661         case 0x7a: /* FCVTPU */
11662         case 0x7b: /* FCVTZU */
11663             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
11664             break;
11665         case 0x6f: /* FNEG */
11666             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
11667             break;
11668         case 0x7d: /* FRSQRTE */
11669             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
11670             break;
11671         default:
11672             g_assert_not_reached();
11673         }
11674
11675         /* limit any sign extension going on */
11676         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
11677         write_fp_sreg(s, rd, tcg_res);
11678
11679         tcg_temp_free_i32(tcg_res);
11680         tcg_temp_free_i32(tcg_op);
11681     } else {
11682         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
11683             TCGv_i32 tcg_op = tcg_temp_new_i32();
11684             TCGv_i32 tcg_res = tcg_temp_new_i32();
11685
11686             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
11687
11688             switch (fpop) {
11689             case 0x1a: /* FCVTNS */
11690             case 0x1b: /* FCVTMS */
11691             case 0x1c: /* FCVTAS */
11692             case 0x3a: /* FCVTPS */
11693             case 0x3b: /* FCVTZS */
11694                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
11695                 break;
11696             case 0x3d: /* FRECPE */
11697                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
11698                 break;
11699             case 0x5a: /* FCVTNU */
11700             case 0x5b: /* FCVTMU */
11701             case 0x5c: /* FCVTAU */
11702             case 0x7a: /* FCVTPU */
11703             case 0x7b: /* FCVTZU */
11704                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
11705                 break;
11706             case 0x18: /* FRINTN */
11707             case 0x19: /* FRINTM */
11708             case 0x38: /* FRINTP */
11709             case 0x39: /* FRINTZ */
11710             case 0x58: /* FRINTA */
11711             case 0x79: /* FRINTI */
11712                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
11713                 break;
11714             case 0x59: /* FRINTX */
11715                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
11716                 break;
11717             case 0x2f: /* FABS */
11718                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
11719                 break;
11720             case 0x6f: /* FNEG */
11721                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
11722                 break;
11723             case 0x7d: /* FRSQRTE */
11724                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
11725                 break;
11726             case 0x7f: /* FSQRT */
11727                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
11728                 break;
11729             default:
11730                 g_assert_not_reached();
11731             }
11732
11733             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11734
11735             tcg_temp_free_i32(tcg_res);
11736             tcg_temp_free_i32(tcg_op);
11737         }
11738
11739         clear_vec_high(s, is_q, rd);
11740     }
11741
11742     if (tcg_rmode) {
11743         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
11744         tcg_temp_free_i32(tcg_rmode);
11745     }
11746
11747     if (tcg_fpstatus) {
11748         tcg_temp_free_ptr(tcg_fpstatus);
11749     }
11750 }
11751
11752 /* AdvSIMD scalar x indexed element
11753  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
11754  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
11755  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
11756  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
11757  * AdvSIMD vector x indexed element
11758  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
11759  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
11760  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
11761  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
11762  */
11763 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
11764 {
11765     /* This encoding has two kinds of instruction:
11766      *  normal, where we perform elt x idxelt => elt for each
11767      *     element in the vector
11768      *  long, where we perform elt x idxelt and generate a result of
11769      *     double the width of the input element
11770      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
11771      */
11772     bool is_scalar = extract32(insn, 28, 1);
11773     bool is_q = extract32(insn, 30, 1);
11774     bool u = extract32(insn, 29, 1);
11775     int size = extract32(insn, 22, 2);
11776     int l = extract32(insn, 21, 1);
11777     int m = extract32(insn, 20, 1);
11778     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
11779     int rm = extract32(insn, 16, 4);
11780     int opcode = extract32(insn, 12, 4);
11781     int h = extract32(insn, 11, 1);
11782     int rn = extract32(insn, 5, 5);
11783     int rd = extract32(insn, 0, 5);
11784     bool is_long = false;
11785     bool is_fp = false;
11786     bool is_fp16 = false;
11787     int index;
11788     TCGv_ptr fpst;
11789
11790     switch (opcode) {
11791     case 0x0: /* MLA */
11792     case 0x4: /* MLS */
11793         if (!u || is_scalar) {
11794             unallocated_encoding(s);
11795             return;
11796         }
11797         break;
11798     case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
11799     case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
11800     case 0xa: /* SMULL, SMULL2, UMULL, UMULL2 */
11801         if (is_scalar) {
11802             unallocated_encoding(s);
11803             return;
11804         }
11805         is_long = true;
11806         break;
11807     case 0x3: /* SQDMLAL, SQDMLAL2 */
11808     case 0x7: /* SQDMLSL, SQDMLSL2 */
11809     case 0xb: /* SQDMULL, SQDMULL2 */
11810         is_long = true;
11811         /* fall through */
11812     case 0xc: /* SQDMULH */
11813     case 0xd: /* SQRDMULH */
11814         if (u) {
11815             unallocated_encoding(s);
11816             return;
11817         }
11818         break;
11819     case 0x8: /* MUL */
11820         if (u || is_scalar) {
11821             unallocated_encoding(s);
11822             return;
11823         }
11824         break;
11825     case 0x1: /* FMLA */
11826     case 0x5: /* FMLS */
11827         if (u) {
11828             unallocated_encoding(s);
11829             return;
11830         }
11831         /* fall through */
11832     case 0x9: /* FMUL, FMULX */
11833         if (size == 1) {
11834             unallocated_encoding(s);
11835             return;
11836         }
11837         is_fp = true;
11838         break;
11839     default:
11840         unallocated_encoding(s);
11841         return;
11842     }
11843
11844     if (is_fp) {
11845         /* convert insn encoded size to TCGMemOp size */
11846         switch (size) {
11847         case 2: /* single precision */
11848             size = MO_32;
11849             index = h << 1 | l;
11850             rm |= (m << 4);
11851             break;
11852         case 3: /* double precision */
11853             size = MO_64;
11854             if (l || !is_q) {
11855                 unallocated_encoding(s);
11856                 return;
11857             }
11858             index = h;
11859             rm |= (m << 4);
11860             break;
11861         case 0: /* half precision */
11862             size = MO_16;
11863             index = h << 2 | l << 1 | m;
11864             is_fp16 = true;
11865             if (arm_dc_feature(s, ARM_FEATURE_V8_FP16)) {
11866                 break;
11867             }
11868             /* fallthru */
11869         default: /* unallocated */
11870             unallocated_encoding(s);
11871             return;
11872         }
11873     } else {
11874         switch (size) {
11875         case 1:
11876             index = h << 2 | l << 1 | m;
11877             break;
11878         case 2:
11879             index = h << 1 | l;
11880             rm |= (m << 4);
11881             break;
11882         default:
11883             unallocated_encoding(s);
11884             return;
11885         }
11886     }
11887
11888     if (!fp_access_check(s)) {
11889         return;
11890     }
11891
11892     if (is_fp) {
11893         fpst = get_fpstatus_ptr(is_fp16);
11894     } else {
11895         fpst = NULL;
11896     }
11897
11898     if (size == 3) {
11899         TCGv_i64 tcg_idx = tcg_temp_new_i64();
11900         int pass;
11901
11902         assert(is_fp && is_q && !is_long);
11903
11904         read_vec_element(s, tcg_idx, rm, index, MO_64);
11905
11906         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
11907             TCGv_i64 tcg_op = tcg_temp_new_i64();
11908             TCGv_i64 tcg_res = tcg_temp_new_i64();
11909
11910             read_vec_element(s, tcg_op, rn, pass, MO_64);
11911
11912             switch (opcode) {
11913             case 0x5: /* FMLS */
11914                 /* As usual for ARM, separate negation for fused multiply-add */
11915                 gen_helper_vfp_negd(tcg_op, tcg_op);
11916                 /* fall through */
11917             case 0x1: /* FMLA */
11918                 read_vec_element(s, tcg_res, rd, pass, MO_64);
11919                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
11920                 break;
11921             case 0x9: /* FMUL, FMULX */
11922                 if (u) {
11923                     gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
11924                 } else {
11925                     gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
11926                 }
11927                 break;
11928             default:
11929                 g_assert_not_reached();
11930             }
11931
11932             write_vec_element(s, tcg_res, rd, pass, MO_64);
11933             tcg_temp_free_i64(tcg_op);
11934             tcg_temp_free_i64(tcg_res);
11935         }
11936
11937         tcg_temp_free_i64(tcg_idx);
11938         clear_vec_high(s, !is_scalar, rd);
11939     } else if (!is_long) {
11940         /* 32 bit floating point, or 16 or 32 bit integer.
11941          * For the 16 bit scalar case we use the usual Neon helpers and
11942          * rely on the fact that 0 op 0 == 0 with no side effects.
11943          */
11944         TCGv_i32 tcg_idx = tcg_temp_new_i32();
11945         int pass, maxpasses;
11946
11947         if (is_scalar) {
11948             maxpasses = 1;
11949         } else {
11950             maxpasses = is_q ? 4 : 2;
11951         }
11952
11953         read_vec_element_i32(s, tcg_idx, rm, index, size);
11954
11955         if (size == 1 && !is_scalar) {
11956             /* The simplest way to handle the 16x16 indexed ops is to duplicate
11957              * the index into both halves of the 32 bit tcg_idx and then use
11958              * the usual Neon helpers.
11959              */
11960             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
11961         }
11962
11963         for (pass = 0; pass < maxpasses; pass++) {
11964             TCGv_i32 tcg_op = tcg_temp_new_i32();
11965             TCGv_i32 tcg_res = tcg_temp_new_i32();
11966
11967             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
11968
11969             switch (opcode) {
11970             case 0x0: /* MLA */
11971             case 0x4: /* MLS */
11972             case 0x8: /* MUL */
11973             {
11974                 static NeonGenTwoOpFn * const fns[2][2] = {
11975                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
11976                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
11977                 };
11978                 NeonGenTwoOpFn *genfn;
11979                 bool is_sub = opcode == 0x4;
11980
11981                 if (size == 1) {
11982                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
11983                 } else {
11984                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
11985                 }
11986                 if (opcode == 0x8) {
11987                     break;
11988                 }
11989                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
11990                 genfn = fns[size - 1][is_sub];
11991                 genfn(tcg_res, tcg_op, tcg_res);
11992                 break;
11993             }
11994             case 0x5: /* FMLS */
11995             case 0x1: /* FMLA */
11996                 read_vec_element_i32(s, tcg_res, rd, pass,
11997                                      is_scalar ? size : MO_32);
11998                 switch (size) {
11999                 case 1:
12000                     if (opcode == 0x5) {
12001                         /* As usual for ARM, separate negation for fused
12002                          * multiply-add */
12003                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
12004                     }
12005                     if (is_scalar) {
12006                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
12007                                                    tcg_res, fpst);
12008                     } else {
12009                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
12010                                                     tcg_res, fpst);
12011                     }
12012                     break;
12013                 case 2:
12014                     if (opcode == 0x5) {
12015                         /* As usual for ARM, separate negation for
12016                          * fused multiply-add */
12017                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
12018                     }
12019                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
12020                                            tcg_res, fpst);
12021                     break;
12022                 default:
12023                     g_assert_not_reached();
12024                 }
12025                 break;
12026             case 0x9: /* FMUL, FMULX */
12027                 switch (size) {
12028                 case 1:
12029                     if (u) {
12030                         if (is_scalar) {
12031                             gen_helper_advsimd_mulxh(tcg_res, tcg_op,
12032                                                      tcg_idx, fpst);
12033                         } else {
12034                             gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
12035                                                       tcg_idx, fpst);
12036                         }
12037                     } else {
12038                         if (is_scalar) {
12039                             gen_helper_advsimd_mulh(tcg_res, tcg_op,
12040                                                     tcg_idx, fpst);
12041                         } else {
12042                             gen_helper_advsimd_mul2h(tcg_res, tcg_op,
12043                                                      tcg_idx, fpst);
12044                         }
12045                     }
12046                     break;
12047                 case 2:
12048                     if (u) {
12049                         gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
12050                     } else {
12051                         gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
12052                     }
12053                     break;
12054                 default:
12055                     g_assert_not_reached();
12056                 }
12057                 break;
12058             case 0xc: /* SQDMULH */
12059                 if (size == 1) {
12060                     gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
12061                                                tcg_op, tcg_idx);
12062                 } else {
12063                     gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
12064                                                tcg_op, tcg_idx);
12065                 }
12066                 break;
12067             case 0xd: /* SQRDMULH */
12068                 if (size == 1) {
12069                     gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
12070                                                 tcg_op, tcg_idx);
12071                 } else {
12072                     gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
12073                                                 tcg_op, tcg_idx);
12074                 }
12075                 break;
12076             default:
12077                 g_assert_not_reached();
12078             }
12079
12080             if (is_scalar) {
12081                 write_fp_sreg(s, rd, tcg_res);
12082             } else {
12083                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12084             }
12085
12086             tcg_temp_free_i32(tcg_op);
12087             tcg_temp_free_i32(tcg_res);
12088         }
12089
12090         tcg_temp_free_i32(tcg_idx);
12091         clear_vec_high(s, is_q, rd);
12092     } else {
12093         /* long ops: 16x16->32 or 32x32->64 */
12094         TCGv_i64 tcg_res[2];
12095         int pass;
12096         bool satop = extract32(opcode, 0, 1);
12097         TCGMemOp memop = MO_32;
12098
12099         if (satop || !u) {
12100             memop |= MO_SIGN;
12101         }
12102
12103         if (size == 2) {
12104             TCGv_i64 tcg_idx = tcg_temp_new_i64();
12105
12106             read_vec_element(s, tcg_idx, rm, index, memop);
12107
12108             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
12109                 TCGv_i64 tcg_op = tcg_temp_new_i64();
12110                 TCGv_i64 tcg_passres;
12111                 int passelt;
12112
12113                 if (is_scalar) {
12114                     passelt = 0;
12115                 } else {
12116                     passelt = pass + (is_q * 2);
12117                 }
12118
12119                 read_vec_element(s, tcg_op, rn, passelt, memop);
12120
12121                 tcg_res[pass] = tcg_temp_new_i64();
12122
12123                 if (opcode == 0xa || opcode == 0xb) {
12124                     /* Non-accumulating ops */
12125                     tcg_passres = tcg_res[pass];
12126                 } else {
12127                     tcg_passres = tcg_temp_new_i64();
12128                 }
12129
12130                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
12131                 tcg_temp_free_i64(tcg_op);
12132
12133                 if (satop) {
12134                     /* saturating, doubling */
12135                     gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
12136                                                       tcg_passres, tcg_passres);
12137                 }
12138
12139                 if (opcode == 0xa || opcode == 0xb) {
12140                     continue;
12141                 }
12142
12143                 /* Accumulating op: handle accumulate step */
12144                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12145
12146                 switch (opcode) {
12147                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
12148                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
12149                     break;
12150                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
12151                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
12152                     break;
12153                 case 0x7: /* SQDMLSL, SQDMLSL2 */
12154                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
12155                     /* fall through */
12156                 case 0x3: /* SQDMLAL, SQDMLAL2 */
12157                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
12158                                                       tcg_res[pass],
12159                                                       tcg_passres);
12160                     break;
12161                 default:
12162                     g_assert_not_reached();
12163                 }
12164                 tcg_temp_free_i64(tcg_passres);
12165             }
12166             tcg_temp_free_i64(tcg_idx);
12167
12168             clear_vec_high(s, !is_scalar, rd);
12169         } else {
12170             TCGv_i32 tcg_idx = tcg_temp_new_i32();
12171
12172             assert(size == 1);
12173             read_vec_element_i32(s, tcg_idx, rm, index, size);
12174
12175             if (!is_scalar) {
12176                 /* The simplest way to handle the 16x16 indexed ops is to
12177                  * duplicate the index into both halves of the 32 bit tcg_idx
12178                  * and then use the usual Neon helpers.
12179                  */
12180                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
12181             }
12182
12183             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
12184                 TCGv_i32 tcg_op = tcg_temp_new_i32();
12185                 TCGv_i64 tcg_passres;
12186
12187                 if (is_scalar) {
12188                     read_vec_element_i32(s, tcg_op, rn, pass, size);
12189                 } else {
12190                     read_vec_element_i32(s, tcg_op, rn,
12191                                          pass + (is_q * 2), MO_32);
12192                 }
12193
12194                 tcg_res[pass] = tcg_temp_new_i64();
12195
12196                 if (opcode == 0xa || opcode == 0xb) {
12197                     /* Non-accumulating ops */
12198                     tcg_passres = tcg_res[pass];
12199                 } else {
12200                     tcg_passres = tcg_temp_new_i64();
12201                 }
12202
12203                 if (memop & MO_SIGN) {
12204                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
12205                 } else {
12206                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
12207                 }
12208                 if (satop) {
12209                     gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
12210                                                       tcg_passres, tcg_passres);
12211                 }
12212                 tcg_temp_free_i32(tcg_op);
12213
12214                 if (opcode == 0xa || opcode == 0xb) {
12215                     continue;
12216                 }
12217
12218                 /* Accumulating op: handle accumulate step */
12219                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12220
12221                 switch (opcode) {
12222                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
12223                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
12224                                              tcg_passres);
12225                     break;
12226                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
12227                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
12228                                              tcg_passres);
12229                     break;
12230                 case 0x7: /* SQDMLSL, SQDMLSL2 */
12231                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
12232                     /* fall through */
12233                 case 0x3: /* SQDMLAL, SQDMLAL2 */
12234                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
12235                                                       tcg_res[pass],
12236                                                       tcg_passres);
12237                     break;
12238                 default:
12239                     g_assert_not_reached();
12240                 }
12241                 tcg_temp_free_i64(tcg_passres);
12242             }
12243             tcg_temp_free_i32(tcg_idx);
12244
12245             if (is_scalar) {
12246                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
12247             }
12248         }
12249
12250         if (is_scalar) {
12251             tcg_res[1] = tcg_const_i64(0);
12252         }
12253
12254         for (pass = 0; pass < 2; pass++) {
12255             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12256             tcg_temp_free_i64(tcg_res[pass]);
12257         }
12258     }
12259
12260     if (fpst) {
12261         tcg_temp_free_ptr(fpst);
12262     }
12263 }
12264
12265 /* Crypto AES
12266  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
12267  * +-----------------+------+-----------+--------+-----+------+------+
12268  * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12269  * +-----------------+------+-----------+--------+-----+------+------+
12270  */
12271 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
12272 {
12273     int size = extract32(insn, 22, 2);
12274     int opcode = extract32(insn, 12, 5);
12275     int rn = extract32(insn, 5, 5);
12276     int rd = extract32(insn, 0, 5);
12277     int decrypt;
12278     TCGv_ptr tcg_rd_ptr, tcg_rn_ptr;
12279     TCGv_i32 tcg_decrypt;
12280     CryptoThreeOpIntFn *genfn;
12281
12282     if (!arm_dc_feature(s, ARM_FEATURE_V8_AES)
12283         || size != 0) {
12284         unallocated_encoding(s);
12285         return;
12286     }
12287
12288     switch (opcode) {
12289     case 0x4: /* AESE */
12290         decrypt = 0;
12291         genfn = gen_helper_crypto_aese;
12292         break;
12293     case 0x6: /* AESMC */
12294         decrypt = 0;
12295         genfn = gen_helper_crypto_aesmc;
12296         break;
12297     case 0x5: /* AESD */
12298         decrypt = 1;
12299         genfn = gen_helper_crypto_aese;
12300         break;
12301     case 0x7: /* AESIMC */
12302         decrypt = 1;
12303         genfn = gen_helper_crypto_aesmc;
12304         break;
12305     default:
12306         unallocated_encoding(s);
12307         return;
12308     }
12309
12310     if (!fp_access_check(s)) {
12311         return;
12312     }
12313
12314     tcg_rd_ptr = vec_full_reg_ptr(s, rd);
12315     tcg_rn_ptr = vec_full_reg_ptr(s, rn);
12316     tcg_decrypt = tcg_const_i32(decrypt);
12317
12318     genfn(tcg_rd_ptr, tcg_rn_ptr, tcg_decrypt);
12319
12320     tcg_temp_free_ptr(tcg_rd_ptr);
12321     tcg_temp_free_ptr(tcg_rn_ptr);
12322     tcg_temp_free_i32(tcg_decrypt);
12323 }
12324
12325 /* Crypto three-reg SHA
12326  *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
12327  * +-----------------+------+---+------+---+--------+-----+------+------+
12328  * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
12329  * +-----------------+------+---+------+---+--------+-----+------+------+
12330  */
12331 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
12332 {
12333     int size = extract32(insn, 22, 2);
12334     int opcode = extract32(insn, 12, 3);
12335     int rm = extract32(insn, 16, 5);
12336     int rn = extract32(insn, 5, 5);
12337     int rd = extract32(insn, 0, 5);
12338     CryptoThreeOpFn *genfn;
12339     TCGv_ptr tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr;
12340     int feature = ARM_FEATURE_V8_SHA256;
12341
12342     if (size != 0) {
12343         unallocated_encoding(s);
12344         return;
12345     }
12346
12347     switch (opcode) {
12348     case 0: /* SHA1C */
12349     case 1: /* SHA1P */
12350     case 2: /* SHA1M */
12351     case 3: /* SHA1SU0 */
12352         genfn = NULL;
12353         feature = ARM_FEATURE_V8_SHA1;
12354         break;
12355     case 4: /* SHA256H */
12356         genfn = gen_helper_crypto_sha256h;
12357         break;
12358     case 5: /* SHA256H2 */
12359         genfn = gen_helper_crypto_sha256h2;
12360         break;
12361     case 6: /* SHA256SU1 */
12362         genfn = gen_helper_crypto_sha256su1;
12363         break;
12364     default:
12365         unallocated_encoding(s);
12366         return;
12367     }
12368
12369     if (!arm_dc_feature(s, feature)) {
12370         unallocated_encoding(s);
12371         return;
12372     }
12373
12374     if (!fp_access_check(s)) {
12375         return;
12376     }
12377
12378     tcg_rd_ptr = vec_full_reg_ptr(s, rd);
12379     tcg_rn_ptr = vec_full_reg_ptr(s, rn);
12380     tcg_rm_ptr = vec_full_reg_ptr(s, rm);
12381
12382     if (genfn) {
12383         genfn(tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr);
12384     } else {
12385         TCGv_i32 tcg_opcode = tcg_const_i32(opcode);
12386
12387         gen_helper_crypto_sha1_3reg(tcg_rd_ptr, tcg_rn_ptr,
12388                                     tcg_rm_ptr, tcg_opcode);
12389         tcg_temp_free_i32(tcg_opcode);
12390     }
12391
12392     tcg_temp_free_ptr(tcg_rd_ptr);
12393     tcg_temp_free_ptr(tcg_rn_ptr);
12394     tcg_temp_free_ptr(tcg_rm_ptr);
12395 }
12396
12397 /* Crypto two-reg SHA
12398  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
12399  * +-----------------+------+-----------+--------+-----+------+------+
12400  * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12401  * +-----------------+------+-----------+--------+-----+------+------+
12402  */
12403 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
12404 {
12405     int size = extract32(insn, 22, 2);
12406     int opcode = extract32(insn, 12, 5);
12407     int rn = extract32(insn, 5, 5);
12408     int rd = extract32(insn, 0, 5);
12409     CryptoTwoOpFn *genfn;
12410     int feature;
12411     TCGv_ptr tcg_rd_ptr, tcg_rn_ptr;
12412
12413     if (size != 0) {
12414         unallocated_encoding(s);
12415         return;
12416     }
12417
12418     switch (opcode) {
12419     case 0: /* SHA1H */
12420         feature = ARM_FEATURE_V8_SHA1;
12421         genfn = gen_helper_crypto_sha1h;
12422         break;
12423     case 1: /* SHA1SU1 */
12424         feature = ARM_FEATURE_V8_SHA1;
12425         genfn = gen_helper_crypto_sha1su1;
12426         break;
12427     case 2: /* SHA256SU0 */
12428         feature = ARM_FEATURE_V8_SHA256;
12429         genfn = gen_helper_crypto_sha256su0;
12430         break;
12431     default:
12432         unallocated_encoding(s);
12433         return;
12434     }
12435
12436     if (!arm_dc_feature(s, feature)) {
12437         unallocated_encoding(s);
12438         return;
12439     }
12440
12441     if (!fp_access_check(s)) {
12442         return;
12443     }
12444
12445     tcg_rd_ptr = vec_full_reg_ptr(s, rd);
12446     tcg_rn_ptr = vec_full_reg_ptr(s, rn);
12447
12448     genfn(tcg_rd_ptr, tcg_rn_ptr);
12449
12450     tcg_temp_free_ptr(tcg_rd_ptr);
12451     tcg_temp_free_ptr(tcg_rn_ptr);
12452 }
12453
12454 /* Crypto three-reg SHA512
12455  *  31                   21 20  16 15  14  13 12  11  10  9    5 4    0
12456  * +-----------------------+------+---+---+-----+--------+------+------+
12457  * | 1 1 0 0 1 1 1 0 0 1 1 |  Rm  | 1 | O | 0 0 | opcode |  Rn  |  Rd  |
12458  * +-----------------------+------+---+---+-----+--------+------+------+
12459  */
12460 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
12461 {
12462     int opcode = extract32(insn, 10, 2);
12463     int o =  extract32(insn, 14, 1);
12464     int rm = extract32(insn, 16, 5);
12465     int rn = extract32(insn, 5, 5);
12466     int rd = extract32(insn, 0, 5);
12467     int feature;
12468     CryptoThreeOpFn *genfn;
12469
12470     if (o == 0) {
12471         switch (opcode) {
12472         case 0: /* SHA512H */
12473             feature = ARM_FEATURE_V8_SHA512;
12474             genfn = gen_helper_crypto_sha512h;
12475             break;
12476         case 1: /* SHA512H2 */
12477             feature = ARM_FEATURE_V8_SHA512;
12478             genfn = gen_helper_crypto_sha512h2;
12479             break;
12480         case 2: /* SHA512SU1 */
12481             feature = ARM_FEATURE_V8_SHA512;
12482             genfn = gen_helper_crypto_sha512su1;
12483             break;
12484         case 3: /* RAX1 */
12485             feature = ARM_FEATURE_V8_SHA3;
12486             genfn = NULL;
12487             break;
12488         }
12489     } else {
12490         switch (opcode) {
12491         case 0: /* SM3PARTW1 */
12492             feature = ARM_FEATURE_V8_SM3;
12493             genfn = gen_helper_crypto_sm3partw1;
12494             break;
12495         case 1: /* SM3PARTW2 */
12496             feature = ARM_FEATURE_V8_SM3;
12497             genfn = gen_helper_crypto_sm3partw2;
12498             break;
12499         case 2: /* SM4EKEY */
12500             feature = ARM_FEATURE_V8_SM4;
12501             genfn = gen_helper_crypto_sm4ekey;
12502             break;
12503         default:
12504             unallocated_encoding(s);
12505             return;
12506         }
12507     }
12508
12509     if (!arm_dc_feature(s, feature)) {
12510         unallocated_encoding(s);
12511         return;
12512     }
12513
12514     if (!fp_access_check(s)) {
12515         return;
12516     }
12517
12518     if (genfn) {
12519         TCGv_ptr tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr;
12520
12521         tcg_rd_ptr = vec_full_reg_ptr(s, rd);
12522         tcg_rn_ptr = vec_full_reg_ptr(s, rn);
12523         tcg_rm_ptr = vec_full_reg_ptr(s, rm);
12524
12525         genfn(tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr);
12526
12527         tcg_temp_free_ptr(tcg_rd_ptr);
12528         tcg_temp_free_ptr(tcg_rn_ptr);
12529         tcg_temp_free_ptr(tcg_rm_ptr);
12530     } else {
12531         TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
12532         int pass;
12533
12534         tcg_op1 = tcg_temp_new_i64();
12535         tcg_op2 = tcg_temp_new_i64();
12536         tcg_res[0] = tcg_temp_new_i64();
12537         tcg_res[1] = tcg_temp_new_i64();
12538
12539         for (pass = 0; pass < 2; pass++) {
12540             read_vec_element(s, tcg_op1, rn, pass, MO_64);
12541             read_vec_element(s, tcg_op2, rm, pass, MO_64);
12542
12543             tcg_gen_rotli_i64(tcg_res[pass], tcg_op2, 1);
12544             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12545         }
12546         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
12547         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
12548
12549         tcg_temp_free_i64(tcg_op1);
12550         tcg_temp_free_i64(tcg_op2);
12551         tcg_temp_free_i64(tcg_res[0]);
12552         tcg_temp_free_i64(tcg_res[1]);
12553     }
12554 }
12555
12556 /* Crypto two-reg SHA512
12557  *  31                                     12  11  10  9    5 4    0
12558  * +-----------------------------------------+--------+------+------+
12559  * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode |  Rn  |  Rd  |
12560  * +-----------------------------------------+--------+------+------+
12561  */
12562 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
12563 {
12564     int opcode = extract32(insn, 10, 2);
12565     int rn = extract32(insn, 5, 5);
12566     int rd = extract32(insn, 0, 5);
12567     TCGv_ptr tcg_rd_ptr, tcg_rn_ptr;
12568     int feature;
12569     CryptoTwoOpFn *genfn;
12570
12571     switch (opcode) {
12572     case 0: /* SHA512SU0 */
12573         feature = ARM_FEATURE_V8_SHA512;
12574         genfn = gen_helper_crypto_sha512su0;
12575         break;
12576     case 1: /* SM4E */
12577         feature = ARM_FEATURE_V8_SM4;
12578         genfn = gen_helper_crypto_sm4e;
12579         break;
12580     default:
12581         unallocated_encoding(s);
12582         return;
12583     }
12584
12585     if (!arm_dc_feature(s, feature)) {
12586         unallocated_encoding(s);
12587         return;
12588     }
12589
12590     if (!fp_access_check(s)) {
12591         return;
12592     }
12593
12594     tcg_rd_ptr = vec_full_reg_ptr(s, rd);
12595     tcg_rn_ptr = vec_full_reg_ptr(s, rn);
12596
12597     genfn(tcg_rd_ptr, tcg_rn_ptr);
12598
12599     tcg_temp_free_ptr(tcg_rd_ptr);
12600     tcg_temp_free_ptr(tcg_rn_ptr);
12601 }
12602
12603 /* Crypto four-register
12604  *  31               23 22 21 20  16 15  14  10 9    5 4    0
12605  * +-------------------+-----+------+---+------+------+------+
12606  * | 1 1 0 0 1 1 1 0 0 | Op0 |  Rm  | 0 |  Ra  |  Rn  |  Rd  |
12607  * +-------------------+-----+------+---+------+------+------+
12608  */
12609 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
12610 {
12611     int op0 = extract32(insn, 21, 2);
12612     int rm = extract32(insn, 16, 5);
12613     int ra = extract32(insn, 10, 5);
12614     int rn = extract32(insn, 5, 5);
12615     int rd = extract32(insn, 0, 5);
12616     int feature;
12617
12618     switch (op0) {
12619     case 0: /* EOR3 */
12620     case 1: /* BCAX */
12621         feature = ARM_FEATURE_V8_SHA3;
12622         break;
12623     case 2: /* SM3SS1 */
12624         feature = ARM_FEATURE_V8_SM3;
12625         break;
12626     default:
12627         unallocated_encoding(s);
12628         return;
12629     }
12630
12631     if (!arm_dc_feature(s, feature)) {
12632         unallocated_encoding(s);
12633         return;
12634     }
12635
12636     if (!fp_access_check(s)) {
12637         return;
12638     }
12639
12640     if (op0 < 2) {
12641         TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
12642         int pass;
12643
12644         tcg_op1 = tcg_temp_new_i64();
12645         tcg_op2 = tcg_temp_new_i64();
12646         tcg_op3 = tcg_temp_new_i64();
12647         tcg_res[0] = tcg_temp_new_i64();
12648         tcg_res[1] = tcg_temp_new_i64();
12649
12650         for (pass = 0; pass < 2; pass++) {
12651             read_vec_element(s, tcg_op1, rn, pass, MO_64);
12652             read_vec_element(s, tcg_op2, rm, pass, MO_64);
12653             read_vec_element(s, tcg_op3, ra, pass, MO_64);
12654
12655             if (op0 == 0) {
12656                 /* EOR3 */
12657                 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
12658             } else {
12659                 /* BCAX */
12660                 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
12661             }
12662             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12663         }
12664         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
12665         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
12666
12667         tcg_temp_free_i64(tcg_op1);
12668         tcg_temp_free_i64(tcg_op2);
12669         tcg_temp_free_i64(tcg_op3);
12670         tcg_temp_free_i64(tcg_res[0]);
12671         tcg_temp_free_i64(tcg_res[1]);
12672     } else {
12673         TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
12674
12675         tcg_op1 = tcg_temp_new_i32();
12676         tcg_op2 = tcg_temp_new_i32();
12677         tcg_op3 = tcg_temp_new_i32();
12678         tcg_res = tcg_temp_new_i32();
12679         tcg_zero = tcg_const_i32(0);
12680
12681         read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
12682         read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
12683         read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
12684
12685         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
12686         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
12687         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
12688         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
12689
12690         write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
12691         write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
12692         write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
12693         write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
12694
12695         tcg_temp_free_i32(tcg_op1);
12696         tcg_temp_free_i32(tcg_op2);
12697         tcg_temp_free_i32(tcg_op3);
12698         tcg_temp_free_i32(tcg_res);
12699         tcg_temp_free_i32(tcg_zero);
12700     }
12701 }
12702
12703 /* Crypto XAR
12704  *  31                   21 20  16 15    10 9    5 4    0
12705  * +-----------------------+------+--------+------+------+
12706  * | 1 1 0 0 1 1 1 0 1 0 0 |  Rm  |  imm6  |  Rn  |  Rd  |
12707  * +-----------------------+------+--------+------+------+
12708  */
12709 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
12710 {
12711     int rm = extract32(insn, 16, 5);
12712     int imm6 = extract32(insn, 10, 6);
12713     int rn = extract32(insn, 5, 5);
12714     int rd = extract32(insn, 0, 5);
12715     TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
12716     int pass;
12717
12718     if (!arm_dc_feature(s, ARM_FEATURE_V8_SHA3)) {
12719         unallocated_encoding(s);
12720         return;
12721     }
12722
12723     if (!fp_access_check(s)) {
12724         return;
12725     }
12726
12727     tcg_op1 = tcg_temp_new_i64();
12728     tcg_op2 = tcg_temp_new_i64();
12729     tcg_res[0] = tcg_temp_new_i64();
12730     tcg_res[1] = tcg_temp_new_i64();
12731
12732     for (pass = 0; pass < 2; pass++) {
12733         read_vec_element(s, tcg_op1, rn, pass, MO_64);
12734         read_vec_element(s, tcg_op2, rm, pass, MO_64);
12735
12736         tcg_gen_xor_i64(tcg_res[pass], tcg_op1, tcg_op2);
12737         tcg_gen_rotri_i64(tcg_res[pass], tcg_res[pass], imm6);
12738     }
12739     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
12740     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
12741
12742     tcg_temp_free_i64(tcg_op1);
12743     tcg_temp_free_i64(tcg_op2);
12744     tcg_temp_free_i64(tcg_res[0]);
12745     tcg_temp_free_i64(tcg_res[1]);
12746 }
12747
12748 /* Crypto three-reg imm2
12749  *  31                   21 20  16 15  14 13 12  11  10  9    5 4    0
12750  * +-----------------------+------+-----+------+--------+------+------+
12751  * | 1 1 0 0 1 1 1 0 0 1 0 |  Rm  | 1 0 | imm2 | opcode |  Rn  |  Rd  |
12752  * +-----------------------+------+-----+------+--------+------+------+
12753  */
12754 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
12755 {
12756     int opcode = extract32(insn, 10, 2);
12757     int imm2 = extract32(insn, 12, 2);
12758     int rm = extract32(insn, 16, 5);
12759     int rn = extract32(insn, 5, 5);
12760     int rd = extract32(insn, 0, 5);
12761     TCGv_ptr tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr;
12762     TCGv_i32 tcg_imm2, tcg_opcode;
12763
12764     if (!arm_dc_feature(s, ARM_FEATURE_V8_SM3)) {
12765         unallocated_encoding(s);
12766         return;
12767     }
12768
12769     if (!fp_access_check(s)) {
12770         return;
12771     }
12772
12773     tcg_rd_ptr = vec_full_reg_ptr(s, rd);
12774     tcg_rn_ptr = vec_full_reg_ptr(s, rn);
12775     tcg_rm_ptr = vec_full_reg_ptr(s, rm);
12776     tcg_imm2   = tcg_const_i32(imm2);
12777     tcg_opcode = tcg_const_i32(opcode);
12778
12779     gen_helper_crypto_sm3tt(tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr, tcg_imm2,
12780                             tcg_opcode);
12781
12782     tcg_temp_free_ptr(tcg_rd_ptr);
12783     tcg_temp_free_ptr(tcg_rn_ptr);
12784     tcg_temp_free_ptr(tcg_rm_ptr);
12785     tcg_temp_free_i32(tcg_imm2);
12786     tcg_temp_free_i32(tcg_opcode);
12787 }
12788
12789 /* C3.6 Data processing - SIMD, inc Crypto
12790  *
12791  * As the decode gets a little complex we are using a table based
12792  * approach for this part of the decode.
12793  */
12794 static const AArch64DecodeTable data_proc_simd[] = {
12795     /* pattern  ,  mask     ,  fn                        */
12796     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
12797     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
12798     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
12799     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
12800     { 0x0e000400, 0x9fe08400, disas_simd_copy },
12801     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
12802     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
12803     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
12804     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
12805     { 0x0e000000, 0xbf208c00, disas_simd_tb },
12806     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
12807     { 0x2e000000, 0xbf208400, disas_simd_ext },
12808     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
12809     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
12810     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
12811     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
12812     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
12813     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
12814     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
12815     { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
12816     { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
12817     { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
12818     { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
12819     { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
12820     { 0xce000000, 0xff808000, disas_crypto_four_reg },
12821     { 0xce800000, 0xffe00000, disas_crypto_xar },
12822     { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
12823     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
12824     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
12825     { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
12826     { 0x00000000, 0x00000000, NULL }
12827 };
12828
12829 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
12830 {
12831     /* Note that this is called with all non-FP cases from
12832      * table C3-6 so it must UNDEF for entries not specifically
12833      * allocated to instructions in that table.
12834      */
12835     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
12836     if (fn) {
12837         fn(s, insn);
12838     } else {
12839         unallocated_encoding(s);
12840     }
12841 }
12842
12843 /* C3.6 Data processing - SIMD and floating point */
12844 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
12845 {
12846     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
12847         disas_data_proc_fp(s, insn);
12848     } else {
12849         /* SIMD, including crypto */
12850         disas_data_proc_simd(s, insn);
12851     }
12852 }
12853
12854 /* C3.1 A64 instruction index by encoding */
12855 static void disas_a64_insn(CPUARMState *env, DisasContext *s)
12856 {
12857     uint32_t insn;
12858
12859     insn = arm_ldl_code(env, s->pc, s->sctlr_b);
12860     s->insn = insn;
12861     s->pc += 4;
12862
12863     s->fp_access_checked = false;
12864
12865     switch (extract32(insn, 25, 4)) {
12866     case 0x0: case 0x1: case 0x2: case 0x3: /* UNALLOCATED */
12867         unallocated_encoding(s);
12868         break;
12869     case 0x8: case 0x9: /* Data processing - immediate */
12870         disas_data_proc_imm(s, insn);
12871         break;
12872     case 0xa: case 0xb: /* Branch, exception generation and system insns */
12873         disas_b_exc_sys(s, insn);
12874         break;
12875     case 0x4:
12876     case 0x6:
12877     case 0xc:
12878     case 0xe:      /* Loads and stores */
12879         disas_ldst(s, insn);
12880         break;
12881     case 0x5:
12882     case 0xd:      /* Data processing - register */
12883         disas_data_proc_reg(s, insn);
12884         break;
12885     case 0x7:
12886     case 0xf:      /* Data processing - SIMD and floating point */
12887         disas_data_proc_simd_fp(s, insn);
12888         break;
12889     default:
12890         assert(FALSE); /* all 15 cases should be handled above */
12891         break;
12892     }
12893
12894     /* if we allocated any temporaries, free them here */
12895     free_tmp_a64(s);
12896 }
12897
12898 static int aarch64_tr_init_disas_context(DisasContextBase *dcbase,
12899                                          CPUState *cpu, int max_insns)
12900 {
12901     DisasContext *dc = container_of(dcbase, DisasContext, base);
12902     CPUARMState *env = cpu->env_ptr;
12903     ARMCPU *arm_cpu = arm_env_get_cpu(env);
12904     int bound;
12905
12906     dc->pc = dc->base.pc_first;
12907     dc->condjmp = 0;
12908
12909     dc->aarch64 = 1;
12910     /* If we are coming from secure EL0 in a system with a 32-bit EL3, then
12911      * there is no secure EL1, so we route exceptions to EL3.
12912      */
12913     dc->secure_routed_to_el3 = arm_feature(env, ARM_FEATURE_EL3) &&
12914                                !arm_el_is_aa64(env, 3);
12915     dc->thumb = 0;
12916     dc->sctlr_b = 0;
12917     dc->be_data = ARM_TBFLAG_BE_DATA(dc->base.tb->flags) ? MO_BE : MO_LE;
12918     dc->condexec_mask = 0;
12919     dc->condexec_cond = 0;
12920     dc->mmu_idx = core_to_arm_mmu_idx(env, ARM_TBFLAG_MMUIDX(dc->base.tb->flags));
12921     dc->tbi0 = ARM_TBFLAG_TBI0(dc->base.tb->flags);
12922     dc->tbi1 = ARM_TBFLAG_TBI1(dc->base.tb->flags);
12923     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
12924 #if !defined(CONFIG_USER_ONLY)
12925     dc->user = (dc->current_el == 0);
12926 #endif
12927     dc->fp_excp_el = ARM_TBFLAG_FPEXC_EL(dc->base.tb->flags);
12928     dc->sve_excp_el = ARM_TBFLAG_SVEEXC_EL(dc->base.tb->flags);
12929     dc->sve_len = (ARM_TBFLAG_ZCR_LEN(dc->base.tb->flags) + 1) * 16;
12930     dc->vec_len = 0;
12931     dc->vec_stride = 0;
12932     dc->cp_regs = arm_cpu->cp_regs;
12933     dc->features = env->features;
12934
12935     /* Single step state. The code-generation logic here is:
12936      *  SS_ACTIVE == 0:
12937      *   generate code with no special handling for single-stepping (except
12938      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
12939      *   this happens anyway because those changes are all system register or
12940      *   PSTATE writes).
12941      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
12942      *   emit code for one insn
12943      *   emit code to clear PSTATE.SS
12944      *   emit code to generate software step exception for completed step
12945      *   end TB (as usual for having generated an exception)
12946      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
12947      *   emit code to generate a software step exception
12948      *   end the TB
12949      */
12950     dc->ss_active = ARM_TBFLAG_SS_ACTIVE(dc->base.tb->flags);
12951     dc->pstate_ss = ARM_TBFLAG_PSTATE_SS(dc->base.tb->flags);
12952     dc->is_ldex = false;
12953     dc->ss_same_el = (arm_debug_target_el(env) == dc->current_el);
12954
12955     /* Bound the number of insns to execute to those left on the page.  */
12956     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
12957
12958     /* If architectural single step active, limit to 1.  */
12959     if (dc->ss_active) {
12960         bound = 1;
12961     }
12962     max_insns = MIN(max_insns, bound);
12963
12964     init_tmp_a64_array(dc);
12965
12966     return max_insns;
12967 }
12968
12969 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
12970 {
12971     tcg_clear_temp_count();
12972 }
12973
12974 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
12975 {
12976     DisasContext *dc = container_of(dcbase, DisasContext, base);
12977
12978     tcg_gen_insn_start(dc->pc, 0, 0);
12979     dc->insn_start = tcg_last_op();
12980 }
12981
12982 static bool aarch64_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,
12983                                         const CPUBreakpoint *bp)
12984 {
12985     DisasContext *dc = container_of(dcbase, DisasContext, base);
12986
12987     if (bp->flags & BP_CPU) {
12988         gen_a64_set_pc_im(dc->pc);
12989         gen_helper_check_breakpoints(cpu_env);
12990         /* End the TB early; it likely won't be executed */
12991         dc->base.is_jmp = DISAS_TOO_MANY;
12992     } else {
12993         gen_exception_internal_insn(dc, 0, EXCP_DEBUG);
12994         /* The address covered by the breakpoint must be
12995            included in [tb->pc, tb->pc + tb->size) in order
12996            to for it to be properly cleared -- thus we
12997            increment the PC here so that the logic setting
12998            tb->size below does the right thing.  */
12999         dc->pc += 4;
13000         dc->base.is_jmp = DISAS_NORETURN;
13001     }
13002
13003     return true;
13004 }
13005
13006 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
13007 {
13008     DisasContext *dc = container_of(dcbase, DisasContext, base);
13009     CPUARMState *env = cpu->env_ptr;
13010
13011     if (dc->ss_active && !dc->pstate_ss) {
13012         /* Singlestep state is Active-pending.
13013          * If we're in this state at the start of a TB then either
13014          *  a) we just took an exception to an EL which is being debugged
13015          *     and this is the first insn in the exception handler
13016          *  b) debug exceptions were masked and we just unmasked them
13017          *     without changing EL (eg by clearing PSTATE.D)
13018          * In either case we're going to take a swstep exception in the
13019          * "did not step an insn" case, and so the syndrome ISV and EX
13020          * bits should be zero.
13021          */
13022         assert(dc->base.num_insns == 1);
13023         gen_exception(EXCP_UDEF, syn_swstep(dc->ss_same_el, 0, 0),
13024                       default_exception_el(dc));
13025         dc->base.is_jmp = DISAS_NORETURN;
13026     } else {
13027         disas_a64_insn(env, dc);
13028     }
13029
13030     dc->base.pc_next = dc->pc;
13031     translator_loop_temp_check(&dc->base);
13032 }
13033
13034 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
13035 {
13036     DisasContext *dc = container_of(dcbase, DisasContext, base);
13037
13038     if (unlikely(dc->base.singlestep_enabled || dc->ss_active)) {
13039         /* Note that this means single stepping WFI doesn't halt the CPU.
13040          * For conditional branch insns this is harmless unreachable code as
13041          * gen_goto_tb() has already handled emitting the debug exception
13042          * (and thus a tb-jump is not possible when singlestepping).
13043          */
13044         switch (dc->base.is_jmp) {
13045         default:
13046             gen_a64_set_pc_im(dc->pc);
13047             /* fall through */
13048         case DISAS_EXIT:
13049         case DISAS_JUMP:
13050             if (dc->base.singlestep_enabled) {
13051                 gen_exception_internal(EXCP_DEBUG);
13052             } else {
13053                 gen_step_complete_exception(dc);
13054             }
13055             break;
13056         case DISAS_NORETURN:
13057             break;
13058         }
13059     } else {
13060         switch (dc->base.is_jmp) {
13061         case DISAS_NEXT:
13062         case DISAS_TOO_MANY:
13063             gen_goto_tb(dc, 1, dc->pc);
13064             break;
13065         default:
13066         case DISAS_UPDATE:
13067             gen_a64_set_pc_im(dc->pc);
13068             /* fall through */
13069         case DISAS_JUMP:
13070             tcg_gen_lookup_and_goto_ptr();
13071             break;
13072         case DISAS_EXIT:
13073             tcg_gen_exit_tb(0);
13074             break;
13075         case DISAS_NORETURN:
13076         case DISAS_SWI:
13077             break;
13078         case DISAS_WFE:
13079             gen_a64_set_pc_im(dc->pc);
13080             gen_helper_wfe(cpu_env);
13081             break;
13082         case DISAS_YIELD:
13083             gen_a64_set_pc_im(dc->pc);
13084             gen_helper_yield(cpu_env);
13085             break;
13086         case DISAS_WFI:
13087         {
13088             /* This is a special case because we don't want to just halt the CPU
13089              * if trying to debug across a WFI.
13090              */
13091             TCGv_i32 tmp = tcg_const_i32(4);
13092
13093             gen_a64_set_pc_im(dc->pc);
13094             gen_helper_wfi(cpu_env, tmp);
13095             tcg_temp_free_i32(tmp);
13096             /* The helper doesn't necessarily throw an exception, but we
13097              * must go back to the main loop to check for interrupts anyway.
13098              */
13099             tcg_gen_exit_tb(0);
13100             break;
13101         }
13102         }
13103     }
13104
13105     /* Functions above can change dc->pc, so re-align db->pc_next */
13106     dc->base.pc_next = dc->pc;
13107 }
13108
13109 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
13110                                       CPUState *cpu)
13111 {
13112     DisasContext *dc = container_of(dcbase, DisasContext, base);
13113
13114     qemu_log("IN: %s\n", lookup_symbol(dc->base.pc_first));
13115     log_target_disas(cpu, dc->base.pc_first, dc->base.tb->size);
13116 }
13117
13118 const TranslatorOps aarch64_translator_ops = {
13119     .init_disas_context = aarch64_tr_init_disas_context,
13120     .tb_start           = aarch64_tr_tb_start,
13121     .insn_start         = aarch64_tr_insn_start,
13122     .breakpoint_check   = aarch64_tr_breakpoint_check,
13123     .translate_insn     = aarch64_tr_translate_insn,
13124     .tb_stop            = aarch64_tr_tb_stop,
13125     .disas_log          = aarch64_tr_disas_log,
13126 };
This page took 0.734007 seconds and 4 git commands to generate.