]> Git Repo - qemu.git/blob - target/arm/translate-a64.c
arm/translate-a64: add FP16 FRSQRTE to simd_two_reg_misc_fp16
[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) - single precision */
4600 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
4601 {
4602     TCGv_ptr fpst;
4603     TCGv_i32 tcg_op;
4604     TCGv_i32 tcg_res;
4605
4606     fpst = get_fpstatus_ptr(false);
4607     tcg_op = read_fp_sreg(s, rn);
4608     tcg_res = tcg_temp_new_i32();
4609
4610     switch (opcode) {
4611     case 0x0: /* FMOV */
4612         tcg_gen_mov_i32(tcg_res, tcg_op);
4613         break;
4614     case 0x1: /* FABS */
4615         gen_helper_vfp_abss(tcg_res, tcg_op);
4616         break;
4617     case 0x2: /* FNEG */
4618         gen_helper_vfp_negs(tcg_res, tcg_op);
4619         break;
4620     case 0x3: /* FSQRT */
4621         gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
4622         break;
4623     case 0x8: /* FRINTN */
4624     case 0x9: /* FRINTP */
4625     case 0xa: /* FRINTM */
4626     case 0xb: /* FRINTZ */
4627     case 0xc: /* FRINTA */
4628     {
4629         TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4630
4631         gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
4632         gen_helper_rints(tcg_res, tcg_op, fpst);
4633
4634         gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
4635         tcg_temp_free_i32(tcg_rmode);
4636         break;
4637     }
4638     case 0xe: /* FRINTX */
4639         gen_helper_rints_exact(tcg_res, tcg_op, fpst);
4640         break;
4641     case 0xf: /* FRINTI */
4642         gen_helper_rints(tcg_res, tcg_op, fpst);
4643         break;
4644     default:
4645         abort();
4646     }
4647
4648     write_fp_sreg(s, rd, tcg_res);
4649
4650     tcg_temp_free_ptr(fpst);
4651     tcg_temp_free_i32(tcg_op);
4652     tcg_temp_free_i32(tcg_res);
4653 }
4654
4655 /* Floating-point data-processing (1 source) - double precision */
4656 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
4657 {
4658     TCGv_ptr fpst;
4659     TCGv_i64 tcg_op;
4660     TCGv_i64 tcg_res;
4661
4662     switch (opcode) {
4663     case 0x0: /* FMOV */
4664         gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
4665         return;
4666     }
4667
4668     fpst = get_fpstatus_ptr(false);
4669     tcg_op = read_fp_dreg(s, rn);
4670     tcg_res = tcg_temp_new_i64();
4671
4672     switch (opcode) {
4673     case 0x1: /* FABS */
4674         gen_helper_vfp_absd(tcg_res, tcg_op);
4675         break;
4676     case 0x2: /* FNEG */
4677         gen_helper_vfp_negd(tcg_res, tcg_op);
4678         break;
4679     case 0x3: /* FSQRT */
4680         gen_helper_vfp_sqrtd(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_rintd(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_rintd_exact(tcg_res, tcg_op, fpst);
4699         break;
4700     case 0xf: /* FRINTI */
4701         gen_helper_rintd(tcg_res, tcg_op, fpst);
4702         break;
4703     default:
4704         abort();
4705     }
4706
4707     write_fp_dreg(s, rd, tcg_res);
4708
4709     tcg_temp_free_ptr(fpst);
4710     tcg_temp_free_i64(tcg_op);
4711     tcg_temp_free_i64(tcg_res);
4712 }
4713
4714 static void handle_fp_fcvt(DisasContext *s, int opcode,
4715                            int rd, int rn, int dtype, int ntype)
4716 {
4717     switch (ntype) {
4718     case 0x0:
4719     {
4720         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4721         if (dtype == 1) {
4722             /* Single to double */
4723             TCGv_i64 tcg_rd = tcg_temp_new_i64();
4724             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
4725             write_fp_dreg(s, rd, tcg_rd);
4726             tcg_temp_free_i64(tcg_rd);
4727         } else {
4728             /* Single to half */
4729             TCGv_i32 tcg_rd = tcg_temp_new_i32();
4730             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, cpu_env);
4731             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4732             write_fp_sreg(s, rd, tcg_rd);
4733             tcg_temp_free_i32(tcg_rd);
4734         }
4735         tcg_temp_free_i32(tcg_rn);
4736         break;
4737     }
4738     case 0x1:
4739     {
4740         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
4741         TCGv_i32 tcg_rd = tcg_temp_new_i32();
4742         if (dtype == 0) {
4743             /* Double to single */
4744             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
4745         } else {
4746             /* Double to half */
4747             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, cpu_env);
4748             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4749         }
4750         write_fp_sreg(s, rd, tcg_rd);
4751         tcg_temp_free_i32(tcg_rd);
4752         tcg_temp_free_i64(tcg_rn);
4753         break;
4754     }
4755     case 0x3:
4756     {
4757         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4758         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
4759         if (dtype == 0) {
4760             /* Half to single */
4761             TCGv_i32 tcg_rd = tcg_temp_new_i32();
4762             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, cpu_env);
4763             write_fp_sreg(s, rd, tcg_rd);
4764             tcg_temp_free_i32(tcg_rd);
4765         } else {
4766             /* Half to double */
4767             TCGv_i64 tcg_rd = tcg_temp_new_i64();
4768             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, cpu_env);
4769             write_fp_dreg(s, rd, tcg_rd);
4770             tcg_temp_free_i64(tcg_rd);
4771         }
4772         tcg_temp_free_i32(tcg_rn);
4773         break;
4774     }
4775     default:
4776         abort();
4777     }
4778 }
4779
4780 /* Floating point data-processing (1 source)
4781  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
4782  * +---+---+---+-----------+------+---+--------+-----------+------+------+
4783  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
4784  * +---+---+---+-----------+------+---+--------+-----------+------+------+
4785  */
4786 static void disas_fp_1src(DisasContext *s, uint32_t insn)
4787 {
4788     int type = extract32(insn, 22, 2);
4789     int opcode = extract32(insn, 15, 6);
4790     int rn = extract32(insn, 5, 5);
4791     int rd = extract32(insn, 0, 5);
4792
4793     switch (opcode) {
4794     case 0x4: case 0x5: case 0x7:
4795     {
4796         /* FCVT between half, single and double precision */
4797         int dtype = extract32(opcode, 0, 2);
4798         if (type == 2 || dtype == type) {
4799             unallocated_encoding(s);
4800             return;
4801         }
4802         if (!fp_access_check(s)) {
4803             return;
4804         }
4805
4806         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
4807         break;
4808     }
4809     case 0x0 ... 0x3:
4810     case 0x8 ... 0xc:
4811     case 0xe ... 0xf:
4812         /* 32-to-32 and 64-to-64 ops */
4813         switch (type) {
4814         case 0:
4815             if (!fp_access_check(s)) {
4816                 return;
4817             }
4818
4819             handle_fp_1src_single(s, opcode, rd, rn);
4820             break;
4821         case 1:
4822             if (!fp_access_check(s)) {
4823                 return;
4824             }
4825
4826             handle_fp_1src_double(s, opcode, rd, rn);
4827             break;
4828         default:
4829             unallocated_encoding(s);
4830         }
4831         break;
4832     default:
4833         unallocated_encoding(s);
4834         break;
4835     }
4836 }
4837
4838 /* Floating-point data-processing (2 source) - single precision */
4839 static void handle_fp_2src_single(DisasContext *s, int opcode,
4840                                   int rd, int rn, int rm)
4841 {
4842     TCGv_i32 tcg_op1;
4843     TCGv_i32 tcg_op2;
4844     TCGv_i32 tcg_res;
4845     TCGv_ptr fpst;
4846
4847     tcg_res = tcg_temp_new_i32();
4848     fpst = get_fpstatus_ptr(false);
4849     tcg_op1 = read_fp_sreg(s, rn);
4850     tcg_op2 = read_fp_sreg(s, rm);
4851
4852     switch (opcode) {
4853     case 0x0: /* FMUL */
4854         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4855         break;
4856     case 0x1: /* FDIV */
4857         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
4858         break;
4859     case 0x2: /* FADD */
4860         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
4861         break;
4862     case 0x3: /* FSUB */
4863         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
4864         break;
4865     case 0x4: /* FMAX */
4866         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
4867         break;
4868     case 0x5: /* FMIN */
4869         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
4870         break;
4871     case 0x6: /* FMAXNM */
4872         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
4873         break;
4874     case 0x7: /* FMINNM */
4875         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
4876         break;
4877     case 0x8: /* FNMUL */
4878         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4879         gen_helper_vfp_negs(tcg_res, tcg_res);
4880         break;
4881     }
4882
4883     write_fp_sreg(s, rd, tcg_res);
4884
4885     tcg_temp_free_ptr(fpst);
4886     tcg_temp_free_i32(tcg_op1);
4887     tcg_temp_free_i32(tcg_op2);
4888     tcg_temp_free_i32(tcg_res);
4889 }
4890
4891 /* Floating-point data-processing (2 source) - double precision */
4892 static void handle_fp_2src_double(DisasContext *s, int opcode,
4893                                   int rd, int rn, int rm)
4894 {
4895     TCGv_i64 tcg_op1;
4896     TCGv_i64 tcg_op2;
4897     TCGv_i64 tcg_res;
4898     TCGv_ptr fpst;
4899
4900     tcg_res = tcg_temp_new_i64();
4901     fpst = get_fpstatus_ptr(false);
4902     tcg_op1 = read_fp_dreg(s, rn);
4903     tcg_op2 = read_fp_dreg(s, rm);
4904
4905     switch (opcode) {
4906     case 0x0: /* FMUL */
4907         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4908         break;
4909     case 0x1: /* FDIV */
4910         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
4911         break;
4912     case 0x2: /* FADD */
4913         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
4914         break;
4915     case 0x3: /* FSUB */
4916         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
4917         break;
4918     case 0x4: /* FMAX */
4919         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
4920         break;
4921     case 0x5: /* FMIN */
4922         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
4923         break;
4924     case 0x6: /* FMAXNM */
4925         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4926         break;
4927     case 0x7: /* FMINNM */
4928         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4929         break;
4930     case 0x8: /* FNMUL */
4931         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4932         gen_helper_vfp_negd(tcg_res, tcg_res);
4933         break;
4934     }
4935
4936     write_fp_dreg(s, rd, tcg_res);
4937
4938     tcg_temp_free_ptr(fpst);
4939     tcg_temp_free_i64(tcg_op1);
4940     tcg_temp_free_i64(tcg_op2);
4941     tcg_temp_free_i64(tcg_res);
4942 }
4943
4944 /* Floating point data-processing (2 source)
4945  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
4946  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4947  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
4948  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4949  */
4950 static void disas_fp_2src(DisasContext *s, uint32_t insn)
4951 {
4952     int type = extract32(insn, 22, 2);
4953     int rd = extract32(insn, 0, 5);
4954     int rn = extract32(insn, 5, 5);
4955     int rm = extract32(insn, 16, 5);
4956     int opcode = extract32(insn, 12, 4);
4957
4958     if (opcode > 8) {
4959         unallocated_encoding(s);
4960         return;
4961     }
4962
4963     switch (type) {
4964     case 0:
4965         if (!fp_access_check(s)) {
4966             return;
4967         }
4968         handle_fp_2src_single(s, opcode, rd, rn, rm);
4969         break;
4970     case 1:
4971         if (!fp_access_check(s)) {
4972             return;
4973         }
4974         handle_fp_2src_double(s, opcode, rd, rn, rm);
4975         break;
4976     default:
4977         unallocated_encoding(s);
4978     }
4979 }
4980
4981 /* Floating-point data-processing (3 source) - single precision */
4982 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
4983                                   int rd, int rn, int rm, int ra)
4984 {
4985     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
4986     TCGv_i32 tcg_res = tcg_temp_new_i32();
4987     TCGv_ptr fpst = get_fpstatus_ptr(false);
4988
4989     tcg_op1 = read_fp_sreg(s, rn);
4990     tcg_op2 = read_fp_sreg(s, rm);
4991     tcg_op3 = read_fp_sreg(s, ra);
4992
4993     /* These are fused multiply-add, and must be done as one
4994      * floating point operation with no rounding between the
4995      * multiplication and addition steps.
4996      * NB that doing the negations here as separate steps is
4997      * correct : an input NaN should come out with its sign bit
4998      * flipped if it is a negated-input.
4999      */
5000     if (o1 == true) {
5001         gen_helper_vfp_negs(tcg_op3, tcg_op3);
5002     }
5003
5004     if (o0 != o1) {
5005         gen_helper_vfp_negs(tcg_op1, tcg_op1);
5006     }
5007
5008     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
5009
5010     write_fp_sreg(s, rd, tcg_res);
5011
5012     tcg_temp_free_ptr(fpst);
5013     tcg_temp_free_i32(tcg_op1);
5014     tcg_temp_free_i32(tcg_op2);
5015     tcg_temp_free_i32(tcg_op3);
5016     tcg_temp_free_i32(tcg_res);
5017 }
5018
5019 /* Floating-point data-processing (3 source) - double precision */
5020 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
5021                                   int rd, int rn, int rm, int ra)
5022 {
5023     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
5024     TCGv_i64 tcg_res = tcg_temp_new_i64();
5025     TCGv_ptr fpst = get_fpstatus_ptr(false);
5026
5027     tcg_op1 = read_fp_dreg(s, rn);
5028     tcg_op2 = read_fp_dreg(s, rm);
5029     tcg_op3 = read_fp_dreg(s, ra);
5030
5031     /* These are fused multiply-add, and must be done as one
5032      * floating point operation with no rounding between the
5033      * multiplication and addition steps.
5034      * NB that doing the negations here as separate steps is
5035      * correct : an input NaN should come out with its sign bit
5036      * flipped if it is a negated-input.
5037      */
5038     if (o1 == true) {
5039         gen_helper_vfp_negd(tcg_op3, tcg_op3);
5040     }
5041
5042     if (o0 != o1) {
5043         gen_helper_vfp_negd(tcg_op1, tcg_op1);
5044     }
5045
5046     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
5047
5048     write_fp_dreg(s, rd, tcg_res);
5049
5050     tcg_temp_free_ptr(fpst);
5051     tcg_temp_free_i64(tcg_op1);
5052     tcg_temp_free_i64(tcg_op2);
5053     tcg_temp_free_i64(tcg_op3);
5054     tcg_temp_free_i64(tcg_res);
5055 }
5056
5057 /* Floating point data-processing (3 source)
5058  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
5059  * +---+---+---+-----------+------+----+------+----+------+------+------+
5060  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
5061  * +---+---+---+-----------+------+----+------+----+------+------+------+
5062  */
5063 static void disas_fp_3src(DisasContext *s, uint32_t insn)
5064 {
5065     int type = extract32(insn, 22, 2);
5066     int rd = extract32(insn, 0, 5);
5067     int rn = extract32(insn, 5, 5);
5068     int ra = extract32(insn, 10, 5);
5069     int rm = extract32(insn, 16, 5);
5070     bool o0 = extract32(insn, 15, 1);
5071     bool o1 = extract32(insn, 21, 1);
5072
5073     switch (type) {
5074     case 0:
5075         if (!fp_access_check(s)) {
5076             return;
5077         }
5078         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
5079         break;
5080     case 1:
5081         if (!fp_access_check(s)) {
5082             return;
5083         }
5084         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
5085         break;
5086     default:
5087         unallocated_encoding(s);
5088     }
5089 }
5090
5091 /* The imm8 encodes the sign bit, enough bits to represent an exponent in
5092  * the range 01....1xx to 10....0xx, and the most significant 4 bits of
5093  * the mantissa; see VFPExpandImm() in the v8 ARM ARM.
5094  */
5095 static uint64_t vfp_expand_imm(int size, uint8_t imm8)
5096 {
5097     uint64_t imm;
5098
5099     switch (size) {
5100     case MO_64:
5101         imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
5102             (extract32(imm8, 6, 1) ? 0x3fc0 : 0x4000) |
5103             extract32(imm8, 0, 6);
5104         imm <<= 48;
5105         break;
5106     case MO_32:
5107         imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
5108             (extract32(imm8, 6, 1) ? 0x3e00 : 0x4000) |
5109             (extract32(imm8, 0, 6) << 3);
5110         imm <<= 16;
5111         break;
5112     case MO_16:
5113         imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
5114             (extract32(imm8, 6, 1) ? 0x3000 : 0x4000) |
5115             (extract32(imm8, 0, 6) << 6);
5116         break;
5117     default:
5118         g_assert_not_reached();
5119     }
5120     return imm;
5121 }
5122
5123 /* Floating point immediate
5124  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
5125  * +---+---+---+-----------+------+---+------------+-------+------+------+
5126  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
5127  * +---+---+---+-----------+------+---+------------+-------+------+------+
5128  */
5129 static void disas_fp_imm(DisasContext *s, uint32_t insn)
5130 {
5131     int rd = extract32(insn, 0, 5);
5132     int imm8 = extract32(insn, 13, 8);
5133     int is_double = extract32(insn, 22, 2);
5134     uint64_t imm;
5135     TCGv_i64 tcg_res;
5136
5137     if (is_double > 1) {
5138         unallocated_encoding(s);
5139         return;
5140     }
5141
5142     if (!fp_access_check(s)) {
5143         return;
5144     }
5145
5146     imm = vfp_expand_imm(MO_32 + is_double, imm8);
5147
5148     tcg_res = tcg_const_i64(imm);
5149     write_fp_dreg(s, rd, tcg_res);
5150     tcg_temp_free_i64(tcg_res);
5151 }
5152
5153 /* Handle floating point <=> fixed point conversions. Note that we can
5154  * also deal with fp <=> integer conversions as a special case (scale == 64)
5155  * OPTME: consider handling that special case specially or at least skipping
5156  * the call to scalbn in the helpers for zero shifts.
5157  */
5158 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
5159                            bool itof, int rmode, int scale, int sf, int type)
5160 {
5161     bool is_signed = !(opcode & 1);
5162     bool is_double = type;
5163     TCGv_ptr tcg_fpstatus;
5164     TCGv_i32 tcg_shift;
5165
5166     tcg_fpstatus = get_fpstatus_ptr(false);
5167
5168     tcg_shift = tcg_const_i32(64 - scale);
5169
5170     if (itof) {
5171         TCGv_i64 tcg_int = cpu_reg(s, rn);
5172         if (!sf) {
5173             TCGv_i64 tcg_extend = new_tmp_a64(s);
5174
5175             if (is_signed) {
5176                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
5177             } else {
5178                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
5179             }
5180
5181             tcg_int = tcg_extend;
5182         }
5183
5184         if (is_double) {
5185             TCGv_i64 tcg_double = tcg_temp_new_i64();
5186             if (is_signed) {
5187                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
5188                                      tcg_shift, tcg_fpstatus);
5189             } else {
5190                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
5191                                      tcg_shift, tcg_fpstatus);
5192             }
5193             write_fp_dreg(s, rd, tcg_double);
5194             tcg_temp_free_i64(tcg_double);
5195         } else {
5196             TCGv_i32 tcg_single = tcg_temp_new_i32();
5197             if (is_signed) {
5198                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
5199                                      tcg_shift, tcg_fpstatus);
5200             } else {
5201                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
5202                                      tcg_shift, tcg_fpstatus);
5203             }
5204             write_fp_sreg(s, rd, tcg_single);
5205             tcg_temp_free_i32(tcg_single);
5206         }
5207     } else {
5208         TCGv_i64 tcg_int = cpu_reg(s, rd);
5209         TCGv_i32 tcg_rmode;
5210
5211         if (extract32(opcode, 2, 1)) {
5212             /* There are too many rounding modes to all fit into rmode,
5213              * so FCVTA[US] is a special case.
5214              */
5215             rmode = FPROUNDING_TIEAWAY;
5216         }
5217
5218         tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
5219
5220         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
5221
5222         if (is_double) {
5223             TCGv_i64 tcg_double = read_fp_dreg(s, rn);
5224             if (is_signed) {
5225                 if (!sf) {
5226                     gen_helper_vfp_tosld(tcg_int, tcg_double,
5227                                          tcg_shift, tcg_fpstatus);
5228                 } else {
5229                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
5230                                          tcg_shift, tcg_fpstatus);
5231                 }
5232             } else {
5233                 if (!sf) {
5234                     gen_helper_vfp_tould(tcg_int, tcg_double,
5235                                          tcg_shift, tcg_fpstatus);
5236                 } else {
5237                     gen_helper_vfp_touqd(tcg_int, tcg_double,
5238                                          tcg_shift, tcg_fpstatus);
5239                 }
5240             }
5241             tcg_temp_free_i64(tcg_double);
5242         } else {
5243             TCGv_i32 tcg_single = read_fp_sreg(s, rn);
5244             if (sf) {
5245                 if (is_signed) {
5246                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
5247                                          tcg_shift, tcg_fpstatus);
5248                 } else {
5249                     gen_helper_vfp_touqs(tcg_int, tcg_single,
5250                                          tcg_shift, tcg_fpstatus);
5251                 }
5252             } else {
5253                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
5254                 if (is_signed) {
5255                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
5256                                          tcg_shift, tcg_fpstatus);
5257                 } else {
5258                     gen_helper_vfp_touls(tcg_dest, tcg_single,
5259                                          tcg_shift, tcg_fpstatus);
5260                 }
5261                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
5262                 tcg_temp_free_i32(tcg_dest);
5263             }
5264             tcg_temp_free_i32(tcg_single);
5265         }
5266
5267         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
5268         tcg_temp_free_i32(tcg_rmode);
5269
5270         if (!sf) {
5271             tcg_gen_ext32u_i64(tcg_int, tcg_int);
5272         }
5273     }
5274
5275     tcg_temp_free_ptr(tcg_fpstatus);
5276     tcg_temp_free_i32(tcg_shift);
5277 }
5278
5279 /* Floating point <-> fixed point conversions
5280  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
5281  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
5282  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
5283  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
5284  */
5285 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
5286 {
5287     int rd = extract32(insn, 0, 5);
5288     int rn = extract32(insn, 5, 5);
5289     int scale = extract32(insn, 10, 6);
5290     int opcode = extract32(insn, 16, 3);
5291     int rmode = extract32(insn, 19, 2);
5292     int type = extract32(insn, 22, 2);
5293     bool sbit = extract32(insn, 29, 1);
5294     bool sf = extract32(insn, 31, 1);
5295     bool itof;
5296
5297     if (sbit || (type > 1)
5298         || (!sf && scale < 32)) {
5299         unallocated_encoding(s);
5300         return;
5301     }
5302
5303     switch ((rmode << 3) | opcode) {
5304     case 0x2: /* SCVTF */
5305     case 0x3: /* UCVTF */
5306         itof = true;
5307         break;
5308     case 0x18: /* FCVTZS */
5309     case 0x19: /* FCVTZU */
5310         itof = false;
5311         break;
5312     default:
5313         unallocated_encoding(s);
5314         return;
5315     }
5316
5317     if (!fp_access_check(s)) {
5318         return;
5319     }
5320
5321     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
5322 }
5323
5324 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
5325 {
5326     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
5327      * without conversion.
5328      */
5329
5330     if (itof) {
5331         TCGv_i64 tcg_rn = cpu_reg(s, rn);
5332
5333         switch (type) {
5334         case 0:
5335         {
5336             /* 32 bit */
5337             TCGv_i64 tmp = tcg_temp_new_i64();
5338             tcg_gen_ext32u_i64(tmp, tcg_rn);
5339             tcg_gen_st_i64(tmp, cpu_env, fp_reg_offset(s, rd, MO_64));
5340             tcg_gen_movi_i64(tmp, 0);
5341             tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(s, rd));
5342             tcg_temp_free_i64(tmp);
5343             break;
5344         }
5345         case 1:
5346         {
5347             /* 64 bit */
5348             TCGv_i64 tmp = tcg_const_i64(0);
5349             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_offset(s, rd, MO_64));
5350             tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(s, rd));
5351             tcg_temp_free_i64(tmp);
5352             break;
5353         }
5354         case 2:
5355             /* 64 bit to top half. */
5356             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
5357             break;
5358         }
5359     } else {
5360         TCGv_i64 tcg_rd = cpu_reg(s, rd);
5361
5362         switch (type) {
5363         case 0:
5364             /* 32 bit */
5365             tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
5366             break;
5367         case 1:
5368             /* 64 bit */
5369             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
5370             break;
5371         case 2:
5372             /* 64 bits from top half */
5373             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
5374             break;
5375         }
5376     }
5377 }
5378
5379 /* Floating point <-> integer conversions
5380  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
5381  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
5382  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
5383  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
5384  */
5385 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
5386 {
5387     int rd = extract32(insn, 0, 5);
5388     int rn = extract32(insn, 5, 5);
5389     int opcode = extract32(insn, 16, 3);
5390     int rmode = extract32(insn, 19, 2);
5391     int type = extract32(insn, 22, 2);
5392     bool sbit = extract32(insn, 29, 1);
5393     bool sf = extract32(insn, 31, 1);
5394
5395     if (sbit) {
5396         unallocated_encoding(s);
5397         return;
5398     }
5399
5400     if (opcode > 5) {
5401         /* FMOV */
5402         bool itof = opcode & 1;
5403
5404         if (rmode >= 2) {
5405             unallocated_encoding(s);
5406             return;
5407         }
5408
5409         switch (sf << 3 | type << 1 | rmode) {
5410         case 0x0: /* 32 bit */
5411         case 0xa: /* 64 bit */
5412         case 0xd: /* 64 bit to top half of quad */
5413             break;
5414         default:
5415             /* all other sf/type/rmode combinations are invalid */
5416             unallocated_encoding(s);
5417             break;
5418         }
5419
5420         if (!fp_access_check(s)) {
5421             return;
5422         }
5423         handle_fmov(s, rd, rn, type, itof);
5424     } else {
5425         /* actual FP conversions */
5426         bool itof = extract32(opcode, 1, 1);
5427
5428         if (type > 1 || (rmode != 0 && opcode > 1)) {
5429             unallocated_encoding(s);
5430             return;
5431         }
5432
5433         if (!fp_access_check(s)) {
5434             return;
5435         }
5436         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
5437     }
5438 }
5439
5440 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
5441  *   31  30  29 28     25 24                          0
5442  * +---+---+---+---------+-----------------------------+
5443  * |   | 0 |   | 1 1 1 1 |                             |
5444  * +---+---+---+---------+-----------------------------+
5445  */
5446 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
5447 {
5448     if (extract32(insn, 24, 1)) {
5449         /* Floating point data-processing (3 source) */
5450         disas_fp_3src(s, insn);
5451     } else if (extract32(insn, 21, 1) == 0) {
5452         /* Floating point to fixed point conversions */
5453         disas_fp_fixed_conv(s, insn);
5454     } else {
5455         switch (extract32(insn, 10, 2)) {
5456         case 1:
5457             /* Floating point conditional compare */
5458             disas_fp_ccomp(s, insn);
5459             break;
5460         case 2:
5461             /* Floating point data-processing (2 source) */
5462             disas_fp_2src(s, insn);
5463             break;
5464         case 3:
5465             /* Floating point conditional select */
5466             disas_fp_csel(s, insn);
5467             break;
5468         case 0:
5469             switch (ctz32(extract32(insn, 12, 4))) {
5470             case 0: /* [15:12] == xxx1 */
5471                 /* Floating point immediate */
5472                 disas_fp_imm(s, insn);
5473                 break;
5474             case 1: /* [15:12] == xx10 */
5475                 /* Floating point compare */
5476                 disas_fp_compare(s, insn);
5477                 break;
5478             case 2: /* [15:12] == x100 */
5479                 /* Floating point data-processing (1 source) */
5480                 disas_fp_1src(s, insn);
5481                 break;
5482             case 3: /* [15:12] == 1000 */
5483                 unallocated_encoding(s);
5484                 break;
5485             default: /* [15:12] == 0000 */
5486                 /* Floating point <-> integer conversions */
5487                 disas_fp_int_conv(s, insn);
5488                 break;
5489             }
5490             break;
5491         }
5492     }
5493 }
5494
5495 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
5496                      int pos)
5497 {
5498     /* Extract 64 bits from the middle of two concatenated 64 bit
5499      * vector register slices left:right. The extracted bits start
5500      * at 'pos' bits into the right (least significant) side.
5501      * We return the result in tcg_right, and guarantee not to
5502      * trash tcg_left.
5503      */
5504     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5505     assert(pos > 0 && pos < 64);
5506
5507     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
5508     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
5509     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
5510
5511     tcg_temp_free_i64(tcg_tmp);
5512 }
5513
5514 /* EXT
5515  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
5516  * +---+---+-------------+-----+---+------+---+------+---+------+------+
5517  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
5518  * +---+---+-------------+-----+---+------+---+------+---+------+------+
5519  */
5520 static void disas_simd_ext(DisasContext *s, uint32_t insn)
5521 {
5522     int is_q = extract32(insn, 30, 1);
5523     int op2 = extract32(insn, 22, 2);
5524     int imm4 = extract32(insn, 11, 4);
5525     int rm = extract32(insn, 16, 5);
5526     int rn = extract32(insn, 5, 5);
5527     int rd = extract32(insn, 0, 5);
5528     int pos = imm4 << 3;
5529     TCGv_i64 tcg_resl, tcg_resh;
5530
5531     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
5532         unallocated_encoding(s);
5533         return;
5534     }
5535
5536     if (!fp_access_check(s)) {
5537         return;
5538     }
5539
5540     tcg_resh = tcg_temp_new_i64();
5541     tcg_resl = tcg_temp_new_i64();
5542
5543     /* Vd gets bits starting at pos bits into Vm:Vn. This is
5544      * either extracting 128 bits from a 128:128 concatenation, or
5545      * extracting 64 bits from a 64:64 concatenation.
5546      */
5547     if (!is_q) {
5548         read_vec_element(s, tcg_resl, rn, 0, MO_64);
5549         if (pos != 0) {
5550             read_vec_element(s, tcg_resh, rm, 0, MO_64);
5551             do_ext64(s, tcg_resh, tcg_resl, pos);
5552         }
5553         tcg_gen_movi_i64(tcg_resh, 0);
5554     } else {
5555         TCGv_i64 tcg_hh;
5556         typedef struct {
5557             int reg;
5558             int elt;
5559         } EltPosns;
5560         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
5561         EltPosns *elt = eltposns;
5562
5563         if (pos >= 64) {
5564             elt++;
5565             pos -= 64;
5566         }
5567
5568         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
5569         elt++;
5570         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
5571         elt++;
5572         if (pos != 0) {
5573             do_ext64(s, tcg_resh, tcg_resl, pos);
5574             tcg_hh = tcg_temp_new_i64();
5575             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
5576             do_ext64(s, tcg_hh, tcg_resh, pos);
5577             tcg_temp_free_i64(tcg_hh);
5578         }
5579     }
5580
5581     write_vec_element(s, tcg_resl, rd, 0, MO_64);
5582     tcg_temp_free_i64(tcg_resl);
5583     write_vec_element(s, tcg_resh, rd, 1, MO_64);
5584     tcg_temp_free_i64(tcg_resh);
5585 }
5586
5587 /* TBL/TBX
5588  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
5589  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
5590  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
5591  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
5592  */
5593 static void disas_simd_tb(DisasContext *s, uint32_t insn)
5594 {
5595     int op2 = extract32(insn, 22, 2);
5596     int is_q = extract32(insn, 30, 1);
5597     int rm = extract32(insn, 16, 5);
5598     int rn = extract32(insn, 5, 5);
5599     int rd = extract32(insn, 0, 5);
5600     int is_tblx = extract32(insn, 12, 1);
5601     int len = extract32(insn, 13, 2);
5602     TCGv_i64 tcg_resl, tcg_resh, tcg_idx;
5603     TCGv_i32 tcg_regno, tcg_numregs;
5604
5605     if (op2 != 0) {
5606         unallocated_encoding(s);
5607         return;
5608     }
5609
5610     if (!fp_access_check(s)) {
5611         return;
5612     }
5613
5614     /* This does a table lookup: for every byte element in the input
5615      * we index into a table formed from up to four vector registers,
5616      * and then the output is the result of the lookups. Our helper
5617      * function does the lookup operation for a single 64 bit part of
5618      * the input.
5619      */
5620     tcg_resl = tcg_temp_new_i64();
5621     tcg_resh = tcg_temp_new_i64();
5622
5623     if (is_tblx) {
5624         read_vec_element(s, tcg_resl, rd, 0, MO_64);
5625     } else {
5626         tcg_gen_movi_i64(tcg_resl, 0);
5627     }
5628     if (is_tblx && is_q) {
5629         read_vec_element(s, tcg_resh, rd, 1, MO_64);
5630     } else {
5631         tcg_gen_movi_i64(tcg_resh, 0);
5632     }
5633
5634     tcg_idx = tcg_temp_new_i64();
5635     tcg_regno = tcg_const_i32(rn);
5636     tcg_numregs = tcg_const_i32(len + 1);
5637     read_vec_element(s, tcg_idx, rm, 0, MO_64);
5638     gen_helper_simd_tbl(tcg_resl, cpu_env, tcg_resl, tcg_idx,
5639                         tcg_regno, tcg_numregs);
5640     if (is_q) {
5641         read_vec_element(s, tcg_idx, rm, 1, MO_64);
5642         gen_helper_simd_tbl(tcg_resh, cpu_env, tcg_resh, tcg_idx,
5643                             tcg_regno, tcg_numregs);
5644     }
5645     tcg_temp_free_i64(tcg_idx);
5646     tcg_temp_free_i32(tcg_regno);
5647     tcg_temp_free_i32(tcg_numregs);
5648
5649     write_vec_element(s, tcg_resl, rd, 0, MO_64);
5650     tcg_temp_free_i64(tcg_resl);
5651     write_vec_element(s, tcg_resh, rd, 1, MO_64);
5652     tcg_temp_free_i64(tcg_resh);
5653 }
5654
5655 /* ZIP/UZP/TRN
5656  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
5657  * +---+---+-------------+------+---+------+---+------------------+------+
5658  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
5659  * +---+---+-------------+------+---+------+---+------------------+------+
5660  */
5661 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
5662 {
5663     int rd = extract32(insn, 0, 5);
5664     int rn = extract32(insn, 5, 5);
5665     int rm = extract32(insn, 16, 5);
5666     int size = extract32(insn, 22, 2);
5667     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
5668      * bit 2 indicates 1 vs 2 variant of the insn.
5669      */
5670     int opcode = extract32(insn, 12, 2);
5671     bool part = extract32(insn, 14, 1);
5672     bool is_q = extract32(insn, 30, 1);
5673     int esize = 8 << size;
5674     int i, ofs;
5675     int datasize = is_q ? 128 : 64;
5676     int elements = datasize / esize;
5677     TCGv_i64 tcg_res, tcg_resl, tcg_resh;
5678
5679     if (opcode == 0 || (size == 3 && !is_q)) {
5680         unallocated_encoding(s);
5681         return;
5682     }
5683
5684     if (!fp_access_check(s)) {
5685         return;
5686     }
5687
5688     tcg_resl = tcg_const_i64(0);
5689     tcg_resh = tcg_const_i64(0);
5690     tcg_res = tcg_temp_new_i64();
5691
5692     for (i = 0; i < elements; i++) {
5693         switch (opcode) {
5694         case 1: /* UZP1/2 */
5695         {
5696             int midpoint = elements / 2;
5697             if (i < midpoint) {
5698                 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
5699             } else {
5700                 read_vec_element(s, tcg_res, rm,
5701                                  2 * (i - midpoint) + part, size);
5702             }
5703             break;
5704         }
5705         case 2: /* TRN1/2 */
5706             if (i & 1) {
5707                 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
5708             } else {
5709                 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
5710             }
5711             break;
5712         case 3: /* ZIP1/2 */
5713         {
5714             int base = part * elements / 2;
5715             if (i & 1) {
5716                 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
5717             } else {
5718                 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
5719             }
5720             break;
5721         }
5722         default:
5723             g_assert_not_reached();
5724         }
5725
5726         ofs = i * esize;
5727         if (ofs < 64) {
5728             tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
5729             tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
5730         } else {
5731             tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
5732             tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
5733         }
5734     }
5735
5736     tcg_temp_free_i64(tcg_res);
5737
5738     write_vec_element(s, tcg_resl, rd, 0, MO_64);
5739     tcg_temp_free_i64(tcg_resl);
5740     write_vec_element(s, tcg_resh, rd, 1, MO_64);
5741     tcg_temp_free_i64(tcg_resh);
5742 }
5743
5744 /*
5745  * do_reduction_op helper
5746  *
5747  * This mirrors the Reduce() pseudocode in the ARM ARM. It is
5748  * important for correct NaN propagation that we do these
5749  * operations in exactly the order specified by the pseudocode.
5750  *
5751  * This is a recursive function, TCG temps should be freed by the
5752  * calling function once it is done with the values.
5753  */
5754 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
5755                                 int esize, int size, int vmap, TCGv_ptr fpst)
5756 {
5757     if (esize == size) {
5758         int element;
5759         TCGMemOp msize = esize == 16 ? MO_16 : MO_32;
5760         TCGv_i32 tcg_elem;
5761
5762         /* We should have one register left here */
5763         assert(ctpop8(vmap) == 1);
5764         element = ctz32(vmap);
5765         assert(element < 8);
5766
5767         tcg_elem = tcg_temp_new_i32();
5768         read_vec_element_i32(s, tcg_elem, rn, element, msize);
5769         return tcg_elem;
5770     } else {
5771         int bits = size / 2;
5772         int shift = ctpop8(vmap) / 2;
5773         int vmap_lo = (vmap >> shift) & vmap;
5774         int vmap_hi = (vmap & ~vmap_lo);
5775         TCGv_i32 tcg_hi, tcg_lo, tcg_res;
5776
5777         tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
5778         tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
5779         tcg_res = tcg_temp_new_i32();
5780
5781         switch (fpopcode) {
5782         case 0x0c: /* fmaxnmv half-precision */
5783             gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
5784             break;
5785         case 0x0f: /* fmaxv half-precision */
5786             gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
5787             break;
5788         case 0x1c: /* fminnmv half-precision */
5789             gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
5790             break;
5791         case 0x1f: /* fminv half-precision */
5792             gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
5793             break;
5794         case 0x2c: /* fmaxnmv */
5795             gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
5796             break;
5797         case 0x2f: /* fmaxv */
5798             gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
5799             break;
5800         case 0x3c: /* fminnmv */
5801             gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
5802             break;
5803         case 0x3f: /* fminv */
5804             gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
5805             break;
5806         default:
5807             g_assert_not_reached();
5808         }
5809
5810         tcg_temp_free_i32(tcg_hi);
5811         tcg_temp_free_i32(tcg_lo);
5812         return tcg_res;
5813     }
5814 }
5815
5816 /* AdvSIMD across lanes
5817  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
5818  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5819  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
5820  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5821  */
5822 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
5823 {
5824     int rd = extract32(insn, 0, 5);
5825     int rn = extract32(insn, 5, 5);
5826     int size = extract32(insn, 22, 2);
5827     int opcode = extract32(insn, 12, 5);
5828     bool is_q = extract32(insn, 30, 1);
5829     bool is_u = extract32(insn, 29, 1);
5830     bool is_fp = false;
5831     bool is_min = false;
5832     int esize;
5833     int elements;
5834     int i;
5835     TCGv_i64 tcg_res, tcg_elt;
5836
5837     switch (opcode) {
5838     case 0x1b: /* ADDV */
5839         if (is_u) {
5840             unallocated_encoding(s);
5841             return;
5842         }
5843         /* fall through */
5844     case 0x3: /* SADDLV, UADDLV */
5845     case 0xa: /* SMAXV, UMAXV */
5846     case 0x1a: /* SMINV, UMINV */
5847         if (size == 3 || (size == 2 && !is_q)) {
5848             unallocated_encoding(s);
5849             return;
5850         }
5851         break;
5852     case 0xc: /* FMAXNMV, FMINNMV */
5853     case 0xf: /* FMAXV, FMINV */
5854         /* Bit 1 of size field encodes min vs max and the actual size
5855          * depends on the encoding of the U bit. If not set (and FP16
5856          * enabled) then we do half-precision float instead of single
5857          * precision.
5858          */
5859         is_min = extract32(size, 1, 1);
5860         is_fp = true;
5861         if (!is_u && arm_dc_feature(s, ARM_FEATURE_V8_FP16)) {
5862             size = 1;
5863         } else if (!is_u || !is_q || extract32(size, 0, 1)) {
5864             unallocated_encoding(s);
5865             return;
5866         } else {
5867             size = 2;
5868         }
5869         break;
5870     default:
5871         unallocated_encoding(s);
5872         return;
5873     }
5874
5875     if (!fp_access_check(s)) {
5876         return;
5877     }
5878
5879     esize = 8 << size;
5880     elements = (is_q ? 128 : 64) / esize;
5881
5882     tcg_res = tcg_temp_new_i64();
5883     tcg_elt = tcg_temp_new_i64();
5884
5885     /* These instructions operate across all lanes of a vector
5886      * to produce a single result. We can guarantee that a 64
5887      * bit intermediate is sufficient:
5888      *  + for [US]ADDLV the maximum element size is 32 bits, and
5889      *    the result type is 64 bits
5890      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
5891      *    same as the element size, which is 32 bits at most
5892      * For the integer operations we can choose to work at 64
5893      * or 32 bits and truncate at the end; for simplicity
5894      * we use 64 bits always. The floating point
5895      * ops do require 32 bit intermediates, though.
5896      */
5897     if (!is_fp) {
5898         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
5899
5900         for (i = 1; i < elements; i++) {
5901             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
5902
5903             switch (opcode) {
5904             case 0x03: /* SADDLV / UADDLV */
5905             case 0x1b: /* ADDV */
5906                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
5907                 break;
5908             case 0x0a: /* SMAXV / UMAXV */
5909                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
5910                                     tcg_res,
5911                                     tcg_res, tcg_elt, tcg_res, tcg_elt);
5912                 break;
5913             case 0x1a: /* SMINV / UMINV */
5914                 tcg_gen_movcond_i64(is_u ? TCG_COND_LEU : TCG_COND_LE,
5915                                     tcg_res,
5916                                     tcg_res, tcg_elt, tcg_res, tcg_elt);
5917                 break;
5918                 break;
5919             default:
5920                 g_assert_not_reached();
5921             }
5922
5923         }
5924     } else {
5925         /* Floating point vector reduction ops which work across 32
5926          * bit (single) or 16 bit (half-precision) intermediates.
5927          * Note that correct NaN propagation requires that we do these
5928          * operations in exactly the order specified by the pseudocode.
5929          */
5930         TCGv_ptr fpst = get_fpstatus_ptr(size == MO_16);
5931         int fpopcode = opcode | is_min << 4 | is_u << 5;
5932         int vmap = (1 << elements) - 1;
5933         TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
5934                                              (is_q ? 128 : 64), vmap, fpst);
5935         tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
5936         tcg_temp_free_i32(tcg_res32);
5937         tcg_temp_free_ptr(fpst);
5938     }
5939
5940     tcg_temp_free_i64(tcg_elt);
5941
5942     /* Now truncate the result to the width required for the final output */
5943     if (opcode == 0x03) {
5944         /* SADDLV, UADDLV: result is 2*esize */
5945         size++;
5946     }
5947
5948     switch (size) {
5949     case 0:
5950         tcg_gen_ext8u_i64(tcg_res, tcg_res);
5951         break;
5952     case 1:
5953         tcg_gen_ext16u_i64(tcg_res, tcg_res);
5954         break;
5955     case 2:
5956         tcg_gen_ext32u_i64(tcg_res, tcg_res);
5957         break;
5958     case 3:
5959         break;
5960     default:
5961         g_assert_not_reached();
5962     }
5963
5964     write_fp_dreg(s, rd, tcg_res);
5965     tcg_temp_free_i64(tcg_res);
5966 }
5967
5968 /* DUP (Element, Vector)
5969  *
5970  *  31  30   29              21 20    16 15        10  9    5 4    0
5971  * +---+---+-------------------+--------+-------------+------+------+
5972  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
5973  * +---+---+-------------------+--------+-------------+------+------+
5974  *
5975  * size: encoded in imm5 (see ARM ARM LowestSetBit())
5976  */
5977 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
5978                              int imm5)
5979 {
5980     int size = ctz32(imm5);
5981     int index = imm5 >> (size + 1);
5982
5983     if (size > 3 || (size == 3 && !is_q)) {
5984         unallocated_encoding(s);
5985         return;
5986     }
5987
5988     if (!fp_access_check(s)) {
5989         return;
5990     }
5991
5992     tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
5993                          vec_reg_offset(s, rn, index, size),
5994                          is_q ? 16 : 8, vec_full_reg_size(s));
5995 }
5996
5997 /* DUP (element, scalar)
5998  *  31                   21 20    16 15        10  9    5 4    0
5999  * +-----------------------+--------+-------------+------+------+
6000  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
6001  * +-----------------------+--------+-------------+------+------+
6002  */
6003 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
6004                               int imm5)
6005 {
6006     int size = ctz32(imm5);
6007     int index;
6008     TCGv_i64 tmp;
6009
6010     if (size > 3) {
6011         unallocated_encoding(s);
6012         return;
6013     }
6014
6015     if (!fp_access_check(s)) {
6016         return;
6017     }
6018
6019     index = imm5 >> (size + 1);
6020
6021     /* This instruction just extracts the specified element and
6022      * zero-extends it into the bottom of the destination register.
6023      */
6024     tmp = tcg_temp_new_i64();
6025     read_vec_element(s, tmp, rn, index, size);
6026     write_fp_dreg(s, rd, tmp);
6027     tcg_temp_free_i64(tmp);
6028 }
6029
6030 /* DUP (General)
6031  *
6032  *  31  30   29              21 20    16 15        10  9    5 4    0
6033  * +---+---+-------------------+--------+-------------+------+------+
6034  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
6035  * +---+---+-------------------+--------+-------------+------+------+
6036  *
6037  * size: encoded in imm5 (see ARM ARM LowestSetBit())
6038  */
6039 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
6040                              int imm5)
6041 {
6042     int size = ctz32(imm5);
6043     uint32_t dofs, oprsz, maxsz;
6044
6045     if (size > 3 || ((size == 3) && !is_q)) {
6046         unallocated_encoding(s);
6047         return;
6048     }
6049
6050     if (!fp_access_check(s)) {
6051         return;
6052     }
6053
6054     dofs = vec_full_reg_offset(s, rd);
6055     oprsz = is_q ? 16 : 8;
6056     maxsz = vec_full_reg_size(s);
6057
6058     tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
6059 }
6060
6061 /* INS (Element)
6062  *
6063  *  31                   21 20    16 15  14    11  10 9    5 4    0
6064  * +-----------------------+--------+------------+---+------+------+
6065  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
6066  * +-----------------------+--------+------------+---+------+------+
6067  *
6068  * size: encoded in imm5 (see ARM ARM LowestSetBit())
6069  * index: encoded in imm5<4:size+1>
6070  */
6071 static void handle_simd_inse(DisasContext *s, int rd, int rn,
6072                              int imm4, int imm5)
6073 {
6074     int size = ctz32(imm5);
6075     int src_index, dst_index;
6076     TCGv_i64 tmp;
6077
6078     if (size > 3) {
6079         unallocated_encoding(s);
6080         return;
6081     }
6082
6083     if (!fp_access_check(s)) {
6084         return;
6085     }
6086
6087     dst_index = extract32(imm5, 1+size, 5);
6088     src_index = extract32(imm4, size, 4);
6089
6090     tmp = tcg_temp_new_i64();
6091
6092     read_vec_element(s, tmp, rn, src_index, size);
6093     write_vec_element(s, tmp, rd, dst_index, size);
6094
6095     tcg_temp_free_i64(tmp);
6096 }
6097
6098
6099 /* INS (General)
6100  *
6101  *  31                   21 20    16 15        10  9    5 4    0
6102  * +-----------------------+--------+-------------+------+------+
6103  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
6104  * +-----------------------+--------+-------------+------+------+
6105  *
6106  * size: encoded in imm5 (see ARM ARM LowestSetBit())
6107  * index: encoded in imm5<4:size+1>
6108  */
6109 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
6110 {
6111     int size = ctz32(imm5);
6112     int idx;
6113
6114     if (size > 3) {
6115         unallocated_encoding(s);
6116         return;
6117     }
6118
6119     if (!fp_access_check(s)) {
6120         return;
6121     }
6122
6123     idx = extract32(imm5, 1 + size, 4 - size);
6124     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
6125 }
6126
6127 /*
6128  * UMOV (General)
6129  * SMOV (General)
6130  *
6131  *  31  30   29              21 20    16 15    12   10 9    5 4    0
6132  * +---+---+-------------------+--------+-------------+------+------+
6133  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
6134  * +---+---+-------------------+--------+-------------+------+------+
6135  *
6136  * U: unsigned when set
6137  * size: encoded in imm5 (see ARM ARM LowestSetBit())
6138  */
6139 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
6140                                   int rn, int rd, int imm5)
6141 {
6142     int size = ctz32(imm5);
6143     int element;
6144     TCGv_i64 tcg_rd;
6145
6146     /* Check for UnallocatedEncodings */
6147     if (is_signed) {
6148         if (size > 2 || (size == 2 && !is_q)) {
6149             unallocated_encoding(s);
6150             return;
6151         }
6152     } else {
6153         if (size > 3
6154             || (size < 3 && is_q)
6155             || (size == 3 && !is_q)) {
6156             unallocated_encoding(s);
6157             return;
6158         }
6159     }
6160
6161     if (!fp_access_check(s)) {
6162         return;
6163     }
6164
6165     element = extract32(imm5, 1+size, 4);
6166
6167     tcg_rd = cpu_reg(s, rd);
6168     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
6169     if (is_signed && !is_q) {
6170         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
6171     }
6172 }
6173
6174 /* AdvSIMD copy
6175  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
6176  * +---+---+----+-----------------+------+---+------+---+------+------+
6177  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
6178  * +---+---+----+-----------------+------+---+------+---+------+------+
6179  */
6180 static void disas_simd_copy(DisasContext *s, uint32_t insn)
6181 {
6182     int rd = extract32(insn, 0, 5);
6183     int rn = extract32(insn, 5, 5);
6184     int imm4 = extract32(insn, 11, 4);
6185     int op = extract32(insn, 29, 1);
6186     int is_q = extract32(insn, 30, 1);
6187     int imm5 = extract32(insn, 16, 5);
6188
6189     if (op) {
6190         if (is_q) {
6191             /* INS (element) */
6192             handle_simd_inse(s, rd, rn, imm4, imm5);
6193         } else {
6194             unallocated_encoding(s);
6195         }
6196     } else {
6197         switch (imm4) {
6198         case 0:
6199             /* DUP (element - vector) */
6200             handle_simd_dupe(s, is_q, rd, rn, imm5);
6201             break;
6202         case 1:
6203             /* DUP (general) */
6204             handle_simd_dupg(s, is_q, rd, rn, imm5);
6205             break;
6206         case 3:
6207             if (is_q) {
6208                 /* INS (general) */
6209                 handle_simd_insg(s, rd, rn, imm5);
6210             } else {
6211                 unallocated_encoding(s);
6212             }
6213             break;
6214         case 5:
6215         case 7:
6216             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
6217             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
6218             break;
6219         default:
6220             unallocated_encoding(s);
6221             break;
6222         }
6223     }
6224 }
6225
6226 /* AdvSIMD modified immediate
6227  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
6228  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
6229  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
6230  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
6231  *
6232  * There are a number of operations that can be carried out here:
6233  *   MOVI - move (shifted) imm into register
6234  *   MVNI - move inverted (shifted) imm into register
6235  *   ORR  - bitwise OR of (shifted) imm with register
6236  *   BIC  - bitwise clear of (shifted) imm with register
6237  */
6238 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
6239 {
6240     int rd = extract32(insn, 0, 5);
6241     int cmode = extract32(insn, 12, 4);
6242     int cmode_3_1 = extract32(cmode, 1, 3);
6243     int cmode_0 = extract32(cmode, 0, 1);
6244     int o2 = extract32(insn, 11, 1);
6245     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
6246     bool is_neg = extract32(insn, 29, 1);
6247     bool is_q = extract32(insn, 30, 1);
6248     uint64_t imm = 0;
6249
6250     if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
6251         unallocated_encoding(s);
6252         return;
6253     }
6254
6255     if (!fp_access_check(s)) {
6256         return;
6257     }
6258
6259     /* See AdvSIMDExpandImm() in ARM ARM */
6260     switch (cmode_3_1) {
6261     case 0: /* Replicate(Zeros(24):imm8, 2) */
6262     case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
6263     case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
6264     case 3: /* Replicate(imm8:Zeros(24), 2) */
6265     {
6266         int shift = cmode_3_1 * 8;
6267         imm = bitfield_replicate(abcdefgh << shift, 32);
6268         break;
6269     }
6270     case 4: /* Replicate(Zeros(8):imm8, 4) */
6271     case 5: /* Replicate(imm8:Zeros(8), 4) */
6272     {
6273         int shift = (cmode_3_1 & 0x1) * 8;
6274         imm = bitfield_replicate(abcdefgh << shift, 16);
6275         break;
6276     }
6277     case 6:
6278         if (cmode_0) {
6279             /* Replicate(Zeros(8):imm8:Ones(16), 2) */
6280             imm = (abcdefgh << 16) | 0xffff;
6281         } else {
6282             /* Replicate(Zeros(16):imm8:Ones(8), 2) */
6283             imm = (abcdefgh << 8) | 0xff;
6284         }
6285         imm = bitfield_replicate(imm, 32);
6286         break;
6287     case 7:
6288         if (!cmode_0 && !is_neg) {
6289             imm = bitfield_replicate(abcdefgh, 8);
6290         } else if (!cmode_0 && is_neg) {
6291             int i;
6292             imm = 0;
6293             for (i = 0; i < 8; i++) {
6294                 if ((abcdefgh) & (1 << i)) {
6295                     imm |= 0xffULL << (i * 8);
6296                 }
6297             }
6298         } else if (cmode_0) {
6299             if (is_neg) {
6300                 imm = (abcdefgh & 0x3f) << 48;
6301                 if (abcdefgh & 0x80) {
6302                     imm |= 0x8000000000000000ULL;
6303                 }
6304                 if (abcdefgh & 0x40) {
6305                     imm |= 0x3fc0000000000000ULL;
6306                 } else {
6307                     imm |= 0x4000000000000000ULL;
6308                 }
6309             } else {
6310                 imm = (abcdefgh & 0x3f) << 19;
6311                 if (abcdefgh & 0x80) {
6312                     imm |= 0x80000000;
6313                 }
6314                 if (abcdefgh & 0x40) {
6315                     imm |= 0x3e000000;
6316                 } else {
6317                     imm |= 0x40000000;
6318                 }
6319                 imm |= (imm << 32);
6320             }
6321         }
6322         break;
6323     }
6324
6325     if (cmode_3_1 != 7 && is_neg) {
6326         imm = ~imm;
6327     }
6328
6329     if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
6330         /* MOVI or MVNI, with MVNI negation handled above.  */
6331         tcg_gen_gvec_dup64i(vec_full_reg_offset(s, rd), is_q ? 16 : 8,
6332                             vec_full_reg_size(s), imm);
6333     } else {
6334         /* ORR or BIC, with BIC negation to AND handled above.  */
6335         if (is_neg) {
6336             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
6337         } else {
6338             gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
6339         }
6340     }
6341 }
6342
6343 /* AdvSIMD scalar copy
6344  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
6345  * +-----+----+-----------------+------+---+------+---+------+------+
6346  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
6347  * +-----+----+-----------------+------+---+------+---+------+------+
6348  */
6349 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
6350 {
6351     int rd = extract32(insn, 0, 5);
6352     int rn = extract32(insn, 5, 5);
6353     int imm4 = extract32(insn, 11, 4);
6354     int imm5 = extract32(insn, 16, 5);
6355     int op = extract32(insn, 29, 1);
6356
6357     if (op != 0 || imm4 != 0) {
6358         unallocated_encoding(s);
6359         return;
6360     }
6361
6362     /* DUP (element, scalar) */
6363     handle_simd_dupes(s, rd, rn, imm5);
6364 }
6365
6366 /* AdvSIMD scalar pairwise
6367  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
6368  * +-----+---+-----------+------+-----------+--------+-----+------+------+
6369  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
6370  * +-----+---+-----------+------+-----------+--------+-----+------+------+
6371  */
6372 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
6373 {
6374     int u = extract32(insn, 29, 1);
6375     int size = extract32(insn, 22, 2);
6376     int opcode = extract32(insn, 12, 5);
6377     int rn = extract32(insn, 5, 5);
6378     int rd = extract32(insn, 0, 5);
6379     TCGv_ptr fpst;
6380
6381     /* For some ops (the FP ones), size[1] is part of the encoding.
6382      * For ADDP strictly it is not but size[1] is always 1 for valid
6383      * encodings.
6384      */
6385     opcode |= (extract32(size, 1, 1) << 5);
6386
6387     switch (opcode) {
6388     case 0x3b: /* ADDP */
6389         if (u || size != 3) {
6390             unallocated_encoding(s);
6391             return;
6392         }
6393         if (!fp_access_check(s)) {
6394             return;
6395         }
6396
6397         fpst = NULL;
6398         break;
6399     case 0xc: /* FMAXNMP */
6400     case 0xd: /* FADDP */
6401     case 0xf: /* FMAXP */
6402     case 0x2c: /* FMINNMP */
6403     case 0x2f: /* FMINP */
6404         /* FP op, size[0] is 32 or 64 bit */
6405         if (!u) {
6406             unallocated_encoding(s);
6407             return;
6408         }
6409         if (!fp_access_check(s)) {
6410             return;
6411         }
6412
6413         size = extract32(size, 0, 1) ? 3 : 2;
6414         fpst = get_fpstatus_ptr(false);
6415         break;
6416     default:
6417         unallocated_encoding(s);
6418         return;
6419     }
6420
6421     if (size == 3) {
6422         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6423         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6424         TCGv_i64 tcg_res = tcg_temp_new_i64();
6425
6426         read_vec_element(s, tcg_op1, rn, 0, MO_64);
6427         read_vec_element(s, tcg_op2, rn, 1, MO_64);
6428
6429         switch (opcode) {
6430         case 0x3b: /* ADDP */
6431             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
6432             break;
6433         case 0xc: /* FMAXNMP */
6434             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6435             break;
6436         case 0xd: /* FADDP */
6437             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6438             break;
6439         case 0xf: /* FMAXP */
6440             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6441             break;
6442         case 0x2c: /* FMINNMP */
6443             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6444             break;
6445         case 0x2f: /* FMINP */
6446             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6447             break;
6448         default:
6449             g_assert_not_reached();
6450         }
6451
6452         write_fp_dreg(s, rd, tcg_res);
6453
6454         tcg_temp_free_i64(tcg_op1);
6455         tcg_temp_free_i64(tcg_op2);
6456         tcg_temp_free_i64(tcg_res);
6457     } else {
6458         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6459         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6460         TCGv_i32 tcg_res = tcg_temp_new_i32();
6461
6462         read_vec_element_i32(s, tcg_op1, rn, 0, MO_32);
6463         read_vec_element_i32(s, tcg_op2, rn, 1, MO_32);
6464
6465         switch (opcode) {
6466         case 0xc: /* FMAXNMP */
6467             gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6468             break;
6469         case 0xd: /* FADDP */
6470             gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6471             break;
6472         case 0xf: /* FMAXP */
6473             gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6474             break;
6475         case 0x2c: /* FMINNMP */
6476             gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6477             break;
6478         case 0x2f: /* FMINP */
6479             gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6480             break;
6481         default:
6482             g_assert_not_reached();
6483         }
6484
6485         write_fp_sreg(s, rd, tcg_res);
6486
6487         tcg_temp_free_i32(tcg_op1);
6488         tcg_temp_free_i32(tcg_op2);
6489         tcg_temp_free_i32(tcg_res);
6490     }
6491
6492     if (fpst) {
6493         tcg_temp_free_ptr(fpst);
6494     }
6495 }
6496
6497 /*
6498  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
6499  *
6500  * This code is handles the common shifting code and is used by both
6501  * the vector and scalar code.
6502  */
6503 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6504                                     TCGv_i64 tcg_rnd, bool accumulate,
6505                                     bool is_u, int size, int shift)
6506 {
6507     bool extended_result = false;
6508     bool round = tcg_rnd != NULL;
6509     int ext_lshift = 0;
6510     TCGv_i64 tcg_src_hi;
6511
6512     if (round && size == 3) {
6513         extended_result = true;
6514         ext_lshift = 64 - shift;
6515         tcg_src_hi = tcg_temp_new_i64();
6516     } else if (shift == 64) {
6517         if (!accumulate && is_u) {
6518             /* result is zero */
6519             tcg_gen_movi_i64(tcg_res, 0);
6520             return;
6521         }
6522     }
6523
6524     /* Deal with the rounding step */
6525     if (round) {
6526         if (extended_result) {
6527             TCGv_i64 tcg_zero = tcg_const_i64(0);
6528             if (!is_u) {
6529                 /* take care of sign extending tcg_res */
6530                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
6531                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
6532                                  tcg_src, tcg_src_hi,
6533                                  tcg_rnd, tcg_zero);
6534             } else {
6535                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
6536                                  tcg_src, tcg_zero,
6537                                  tcg_rnd, tcg_zero);
6538             }
6539             tcg_temp_free_i64(tcg_zero);
6540         } else {
6541             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
6542         }
6543     }
6544
6545     /* Now do the shift right */
6546     if (round && extended_result) {
6547         /* extended case, >64 bit precision required */
6548         if (ext_lshift == 0) {
6549             /* special case, only high bits matter */
6550             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
6551         } else {
6552             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6553             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
6554             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
6555         }
6556     } else {
6557         if (is_u) {
6558             if (shift == 64) {
6559                 /* essentially shifting in 64 zeros */
6560                 tcg_gen_movi_i64(tcg_src, 0);
6561             } else {
6562                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6563             }
6564         } else {
6565             if (shift == 64) {
6566                 /* effectively extending the sign-bit */
6567                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
6568             } else {
6569                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
6570             }
6571         }
6572     }
6573
6574     if (accumulate) {
6575         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
6576     } else {
6577         tcg_gen_mov_i64(tcg_res, tcg_src);
6578     }
6579
6580     if (extended_result) {
6581         tcg_temp_free_i64(tcg_src_hi);
6582     }
6583 }
6584
6585 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
6586 static void handle_scalar_simd_shri(DisasContext *s,
6587                                     bool is_u, int immh, int immb,
6588                                     int opcode, int rn, int rd)
6589 {
6590     const int size = 3;
6591     int immhb = immh << 3 | immb;
6592     int shift = 2 * (8 << size) - immhb;
6593     bool accumulate = false;
6594     bool round = false;
6595     bool insert = false;
6596     TCGv_i64 tcg_rn;
6597     TCGv_i64 tcg_rd;
6598     TCGv_i64 tcg_round;
6599
6600     if (!extract32(immh, 3, 1)) {
6601         unallocated_encoding(s);
6602         return;
6603     }
6604
6605     if (!fp_access_check(s)) {
6606         return;
6607     }
6608
6609     switch (opcode) {
6610     case 0x02: /* SSRA / USRA (accumulate) */
6611         accumulate = true;
6612         break;
6613     case 0x04: /* SRSHR / URSHR (rounding) */
6614         round = true;
6615         break;
6616     case 0x06: /* SRSRA / URSRA (accum + rounding) */
6617         accumulate = round = true;
6618         break;
6619     case 0x08: /* SRI */
6620         insert = true;
6621         break;
6622     }
6623
6624     if (round) {
6625         uint64_t round_const = 1ULL << (shift - 1);
6626         tcg_round = tcg_const_i64(round_const);
6627     } else {
6628         tcg_round = NULL;
6629     }
6630
6631     tcg_rn = read_fp_dreg(s, rn);
6632     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6633
6634     if (insert) {
6635         /* shift count same as element size is valid but does nothing;
6636          * special case to avoid potential shift by 64.
6637          */
6638         int esize = 8 << size;
6639         if (shift != esize) {
6640             tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
6641             tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
6642         }
6643     } else {
6644         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6645                                 accumulate, is_u, size, shift);
6646     }
6647
6648     write_fp_dreg(s, rd, tcg_rd);
6649
6650     tcg_temp_free_i64(tcg_rn);
6651     tcg_temp_free_i64(tcg_rd);
6652     if (round) {
6653         tcg_temp_free_i64(tcg_round);
6654     }
6655 }
6656
6657 /* SHL/SLI - Scalar shift left */
6658 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
6659                                     int immh, int immb, int opcode,
6660                                     int rn, int rd)
6661 {
6662     int size = 32 - clz32(immh) - 1;
6663     int immhb = immh << 3 | immb;
6664     int shift = immhb - (8 << size);
6665     TCGv_i64 tcg_rn = new_tmp_a64(s);
6666     TCGv_i64 tcg_rd = new_tmp_a64(s);
6667
6668     if (!extract32(immh, 3, 1)) {
6669         unallocated_encoding(s);
6670         return;
6671     }
6672
6673     if (!fp_access_check(s)) {
6674         return;
6675     }
6676
6677     tcg_rn = read_fp_dreg(s, rn);
6678     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6679
6680     if (insert) {
6681         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
6682     } else {
6683         tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
6684     }
6685
6686     write_fp_dreg(s, rd, tcg_rd);
6687
6688     tcg_temp_free_i64(tcg_rn);
6689     tcg_temp_free_i64(tcg_rd);
6690 }
6691
6692 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
6693  * (signed/unsigned) narrowing */
6694 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
6695                                    bool is_u_shift, bool is_u_narrow,
6696                                    int immh, int immb, int opcode,
6697                                    int rn, int rd)
6698 {
6699     int immhb = immh << 3 | immb;
6700     int size = 32 - clz32(immh) - 1;
6701     int esize = 8 << size;
6702     int shift = (2 * esize) - immhb;
6703     int elements = is_scalar ? 1 : (64 / esize);
6704     bool round = extract32(opcode, 0, 1);
6705     TCGMemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
6706     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
6707     TCGv_i32 tcg_rd_narrowed;
6708     TCGv_i64 tcg_final;
6709
6710     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
6711         { gen_helper_neon_narrow_sat_s8,
6712           gen_helper_neon_unarrow_sat8 },
6713         { gen_helper_neon_narrow_sat_s16,
6714           gen_helper_neon_unarrow_sat16 },
6715         { gen_helper_neon_narrow_sat_s32,
6716           gen_helper_neon_unarrow_sat32 },
6717         { NULL, NULL },
6718     };
6719     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
6720         gen_helper_neon_narrow_sat_u8,
6721         gen_helper_neon_narrow_sat_u16,
6722         gen_helper_neon_narrow_sat_u32,
6723         NULL
6724     };
6725     NeonGenNarrowEnvFn *narrowfn;
6726
6727     int i;
6728
6729     assert(size < 4);
6730
6731     if (extract32(immh, 3, 1)) {
6732         unallocated_encoding(s);
6733         return;
6734     }
6735
6736     if (!fp_access_check(s)) {
6737         return;
6738     }
6739
6740     if (is_u_shift) {
6741         narrowfn = unsigned_narrow_fns[size];
6742     } else {
6743         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
6744     }
6745
6746     tcg_rn = tcg_temp_new_i64();
6747     tcg_rd = tcg_temp_new_i64();
6748     tcg_rd_narrowed = tcg_temp_new_i32();
6749     tcg_final = tcg_const_i64(0);
6750
6751     if (round) {
6752         uint64_t round_const = 1ULL << (shift - 1);
6753         tcg_round = tcg_const_i64(round_const);
6754     } else {
6755         tcg_round = NULL;
6756     }
6757
6758     for (i = 0; i < elements; i++) {
6759         read_vec_element(s, tcg_rn, rn, i, ldop);
6760         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6761                                 false, is_u_shift, size+1, shift);
6762         narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
6763         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
6764         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
6765     }
6766
6767     if (!is_q) {
6768         write_vec_element(s, tcg_final, rd, 0, MO_64);
6769     } else {
6770         write_vec_element(s, tcg_final, rd, 1, MO_64);
6771     }
6772
6773     if (round) {
6774         tcg_temp_free_i64(tcg_round);
6775     }
6776     tcg_temp_free_i64(tcg_rn);
6777     tcg_temp_free_i64(tcg_rd);
6778     tcg_temp_free_i32(tcg_rd_narrowed);
6779     tcg_temp_free_i64(tcg_final);
6780
6781     clear_vec_high(s, is_q, rd);
6782 }
6783
6784 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
6785 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
6786                              bool src_unsigned, bool dst_unsigned,
6787                              int immh, int immb, int rn, int rd)
6788 {
6789     int immhb = immh << 3 | immb;
6790     int size = 32 - clz32(immh) - 1;
6791     int shift = immhb - (8 << size);
6792     int pass;
6793
6794     assert(immh != 0);
6795     assert(!(scalar && is_q));
6796
6797     if (!scalar) {
6798         if (!is_q && extract32(immh, 3, 1)) {
6799             unallocated_encoding(s);
6800             return;
6801         }
6802
6803         /* Since we use the variable-shift helpers we must
6804          * replicate the shift count into each element of
6805          * the tcg_shift value.
6806          */
6807         switch (size) {
6808         case 0:
6809             shift |= shift << 8;
6810             /* fall through */
6811         case 1:
6812             shift |= shift << 16;
6813             break;
6814         case 2:
6815         case 3:
6816             break;
6817         default:
6818             g_assert_not_reached();
6819         }
6820     }
6821
6822     if (!fp_access_check(s)) {
6823         return;
6824     }
6825
6826     if (size == 3) {
6827         TCGv_i64 tcg_shift = tcg_const_i64(shift);
6828         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
6829             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
6830             { NULL, gen_helper_neon_qshl_u64 },
6831         };
6832         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
6833         int maxpass = is_q ? 2 : 1;
6834
6835         for (pass = 0; pass < maxpass; pass++) {
6836             TCGv_i64 tcg_op = tcg_temp_new_i64();
6837
6838             read_vec_element(s, tcg_op, rn, pass, MO_64);
6839             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6840             write_vec_element(s, tcg_op, rd, pass, MO_64);
6841
6842             tcg_temp_free_i64(tcg_op);
6843         }
6844         tcg_temp_free_i64(tcg_shift);
6845         clear_vec_high(s, is_q, rd);
6846     } else {
6847         TCGv_i32 tcg_shift = tcg_const_i32(shift);
6848         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
6849             {
6850                 { gen_helper_neon_qshl_s8,
6851                   gen_helper_neon_qshl_s16,
6852                   gen_helper_neon_qshl_s32 },
6853                 { gen_helper_neon_qshlu_s8,
6854                   gen_helper_neon_qshlu_s16,
6855                   gen_helper_neon_qshlu_s32 }
6856             }, {
6857                 { NULL, NULL, NULL },
6858                 { gen_helper_neon_qshl_u8,
6859                   gen_helper_neon_qshl_u16,
6860                   gen_helper_neon_qshl_u32 }
6861             }
6862         };
6863         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
6864         TCGMemOp memop = scalar ? size : MO_32;
6865         int maxpass = scalar ? 1 : is_q ? 4 : 2;
6866
6867         for (pass = 0; pass < maxpass; pass++) {
6868             TCGv_i32 tcg_op = tcg_temp_new_i32();
6869
6870             read_vec_element_i32(s, tcg_op, rn, pass, memop);
6871             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6872             if (scalar) {
6873                 switch (size) {
6874                 case 0:
6875                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
6876                     break;
6877                 case 1:
6878                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
6879                     break;
6880                 case 2:
6881                     break;
6882                 default:
6883                     g_assert_not_reached();
6884                 }
6885                 write_fp_sreg(s, rd, tcg_op);
6886             } else {
6887                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
6888             }
6889
6890             tcg_temp_free_i32(tcg_op);
6891         }
6892         tcg_temp_free_i32(tcg_shift);
6893
6894         if (!scalar) {
6895             clear_vec_high(s, is_q, rd);
6896         }
6897     }
6898 }
6899
6900 /* Common vector code for handling integer to FP conversion */
6901 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
6902                                    int elements, int is_signed,
6903                                    int fracbits, int size)
6904 {
6905     TCGv_ptr tcg_fpst = get_fpstatus_ptr(size == MO_16);
6906     TCGv_i32 tcg_shift = NULL;
6907
6908     TCGMemOp mop = size | (is_signed ? MO_SIGN : 0);
6909     int pass;
6910
6911     if (fracbits || size == MO_64) {
6912         tcg_shift = tcg_const_i32(fracbits);
6913     }
6914
6915     if (size == MO_64) {
6916         TCGv_i64 tcg_int64 = tcg_temp_new_i64();
6917         TCGv_i64 tcg_double = tcg_temp_new_i64();
6918
6919         for (pass = 0; pass < elements; pass++) {
6920             read_vec_element(s, tcg_int64, rn, pass, mop);
6921
6922             if (is_signed) {
6923                 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
6924                                      tcg_shift, tcg_fpst);
6925             } else {
6926                 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
6927                                      tcg_shift, tcg_fpst);
6928             }
6929             if (elements == 1) {
6930                 write_fp_dreg(s, rd, tcg_double);
6931             } else {
6932                 write_vec_element(s, tcg_double, rd, pass, MO_64);
6933             }
6934         }
6935
6936         tcg_temp_free_i64(tcg_int64);
6937         tcg_temp_free_i64(tcg_double);
6938
6939     } else {
6940         TCGv_i32 tcg_int32 = tcg_temp_new_i32();
6941         TCGv_i32 tcg_float = tcg_temp_new_i32();
6942
6943         for (pass = 0; pass < elements; pass++) {
6944             read_vec_element_i32(s, tcg_int32, rn, pass, mop);
6945
6946             switch (size) {
6947             case MO_32:
6948                 if (fracbits) {
6949                     if (is_signed) {
6950                         gen_helper_vfp_sltos(tcg_float, tcg_int32,
6951                                              tcg_shift, tcg_fpst);
6952                     } else {
6953                         gen_helper_vfp_ultos(tcg_float, tcg_int32,
6954                                              tcg_shift, tcg_fpst);
6955                     }
6956                 } else {
6957                     if (is_signed) {
6958                         gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
6959                     } else {
6960                         gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
6961                     }
6962                 }
6963                 break;
6964             case MO_16:
6965                 if (fracbits) {
6966                     if (is_signed) {
6967                         gen_helper_vfp_sltoh(tcg_float, tcg_int32,
6968                                              tcg_shift, tcg_fpst);
6969                     } else {
6970                         gen_helper_vfp_ultoh(tcg_float, tcg_int32,
6971                                              tcg_shift, tcg_fpst);
6972                     }
6973                 } else {
6974                     if (is_signed) {
6975                         gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
6976                     } else {
6977                         gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
6978                     }
6979                 }
6980                 break;
6981             default:
6982                 g_assert_not_reached();
6983             }
6984
6985             if (elements == 1) {
6986                 write_fp_sreg(s, rd, tcg_float);
6987             } else {
6988                 write_vec_element_i32(s, tcg_float, rd, pass, size);
6989             }
6990         }
6991
6992         tcg_temp_free_i32(tcg_int32);
6993         tcg_temp_free_i32(tcg_float);
6994     }
6995
6996     tcg_temp_free_ptr(tcg_fpst);
6997     if (tcg_shift) {
6998         tcg_temp_free_i32(tcg_shift);
6999     }
7000
7001     clear_vec_high(s, elements << size == 16, rd);
7002 }
7003
7004 /* UCVTF/SCVTF - Integer to FP conversion */
7005 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
7006                                          bool is_q, bool is_u,
7007                                          int immh, int immb, int opcode,
7008                                          int rn, int rd)
7009 {
7010     bool is_double = extract32(immh, 3, 1);
7011     int size = is_double ? MO_64 : MO_32;
7012     int elements;
7013     int immhb = immh << 3 | immb;
7014     int fracbits = (is_double ? 128 : 64) - immhb;
7015
7016     if (!extract32(immh, 2, 2)) {
7017         unallocated_encoding(s);
7018         return;
7019     }
7020
7021     if (is_scalar) {
7022         elements = 1;
7023     } else {
7024         elements = is_double ? 2 : is_q ? 4 : 2;
7025         if (is_double && !is_q) {
7026             unallocated_encoding(s);
7027             return;
7028         }
7029     }
7030
7031     if (!fp_access_check(s)) {
7032         return;
7033     }
7034
7035     /* immh == 0 would be a failure of the decode logic */
7036     g_assert(immh);
7037
7038     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
7039 }
7040
7041 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
7042 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
7043                                          bool is_q, bool is_u,
7044                                          int immh, int immb, int rn, int rd)
7045 {
7046     bool is_double = extract32(immh, 3, 1);
7047     int immhb = immh << 3 | immb;
7048     int fracbits = (is_double ? 128 : 64) - immhb;
7049     int pass;
7050     TCGv_ptr tcg_fpstatus;
7051     TCGv_i32 tcg_rmode, tcg_shift;
7052
7053     if (!extract32(immh, 2, 2)) {
7054         unallocated_encoding(s);
7055         return;
7056     }
7057
7058     if (!is_scalar && !is_q && is_double) {
7059         unallocated_encoding(s);
7060         return;
7061     }
7062
7063     if (!fp_access_check(s)) {
7064         return;
7065     }
7066
7067     assert(!(is_scalar && is_q));
7068
7069     tcg_rmode = tcg_const_i32(arm_rmode_to_sf(FPROUNDING_ZERO));
7070     tcg_fpstatus = get_fpstatus_ptr(false);
7071     gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
7072     tcg_shift = tcg_const_i32(fracbits);
7073
7074     if (is_double) {
7075         int maxpass = is_scalar ? 1 : 2;
7076
7077         for (pass = 0; pass < maxpass; pass++) {
7078             TCGv_i64 tcg_op = tcg_temp_new_i64();
7079
7080             read_vec_element(s, tcg_op, rn, pass, MO_64);
7081             if (is_u) {
7082                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
7083             } else {
7084                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
7085             }
7086             write_vec_element(s, tcg_op, rd, pass, MO_64);
7087             tcg_temp_free_i64(tcg_op);
7088         }
7089         clear_vec_high(s, is_q, rd);
7090     } else {
7091         int maxpass = is_scalar ? 1 : is_q ? 4 : 2;
7092         for (pass = 0; pass < maxpass; pass++) {
7093             TCGv_i32 tcg_op = tcg_temp_new_i32();
7094
7095             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7096             if (is_u) {
7097                 gen_helper_vfp_touls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
7098             } else {
7099                 gen_helper_vfp_tosls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
7100             }
7101             if (is_scalar) {
7102                 write_fp_sreg(s, rd, tcg_op);
7103             } else {
7104                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
7105             }
7106             tcg_temp_free_i32(tcg_op);
7107         }
7108         if (!is_scalar) {
7109             clear_vec_high(s, is_q, rd);
7110         }
7111     }
7112
7113     tcg_temp_free_ptr(tcg_fpstatus);
7114     tcg_temp_free_i32(tcg_shift);
7115     gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
7116     tcg_temp_free_i32(tcg_rmode);
7117 }
7118
7119 /* AdvSIMD scalar shift by immediate
7120  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
7121  * +-----+---+-------------+------+------+--------+---+------+------+
7122  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
7123  * +-----+---+-------------+------+------+--------+---+------+------+
7124  *
7125  * This is the scalar version so it works on a fixed sized registers
7126  */
7127 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
7128 {
7129     int rd = extract32(insn, 0, 5);
7130     int rn = extract32(insn, 5, 5);
7131     int opcode = extract32(insn, 11, 5);
7132     int immb = extract32(insn, 16, 3);
7133     int immh = extract32(insn, 19, 4);
7134     bool is_u = extract32(insn, 29, 1);
7135
7136     if (immh == 0) {
7137         unallocated_encoding(s);
7138         return;
7139     }
7140
7141     switch (opcode) {
7142     case 0x08: /* SRI */
7143         if (!is_u) {
7144             unallocated_encoding(s);
7145             return;
7146         }
7147         /* fall through */
7148     case 0x00: /* SSHR / USHR */
7149     case 0x02: /* SSRA / USRA */
7150     case 0x04: /* SRSHR / URSHR */
7151     case 0x06: /* SRSRA / URSRA */
7152         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
7153         break;
7154     case 0x0a: /* SHL / SLI */
7155         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
7156         break;
7157     case 0x1c: /* SCVTF, UCVTF */
7158         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
7159                                      opcode, rn, rd);
7160         break;
7161     case 0x10: /* SQSHRUN, SQSHRUN2 */
7162     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
7163         if (!is_u) {
7164             unallocated_encoding(s);
7165             return;
7166         }
7167         handle_vec_simd_sqshrn(s, true, false, false, true,
7168                                immh, immb, opcode, rn, rd);
7169         break;
7170     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
7171     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
7172         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
7173                                immh, immb, opcode, rn, rd);
7174         break;
7175     case 0xc: /* SQSHLU */
7176         if (!is_u) {
7177             unallocated_encoding(s);
7178             return;
7179         }
7180         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
7181         break;
7182     case 0xe: /* SQSHL, UQSHL */
7183         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
7184         break;
7185     case 0x1f: /* FCVTZS, FCVTZU */
7186         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
7187         break;
7188     default:
7189         unallocated_encoding(s);
7190         break;
7191     }
7192 }
7193
7194 /* AdvSIMD scalar three different
7195  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
7196  * +-----+---+-----------+------+---+------+--------+-----+------+------+
7197  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
7198  * +-----+---+-----------+------+---+------+--------+-----+------+------+
7199  */
7200 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
7201 {
7202     bool is_u = extract32(insn, 29, 1);
7203     int size = extract32(insn, 22, 2);
7204     int opcode = extract32(insn, 12, 4);
7205     int rm = extract32(insn, 16, 5);
7206     int rn = extract32(insn, 5, 5);
7207     int rd = extract32(insn, 0, 5);
7208
7209     if (is_u) {
7210         unallocated_encoding(s);
7211         return;
7212     }
7213
7214     switch (opcode) {
7215     case 0x9: /* SQDMLAL, SQDMLAL2 */
7216     case 0xb: /* SQDMLSL, SQDMLSL2 */
7217     case 0xd: /* SQDMULL, SQDMULL2 */
7218         if (size == 0 || size == 3) {
7219             unallocated_encoding(s);
7220             return;
7221         }
7222         break;
7223     default:
7224         unallocated_encoding(s);
7225         return;
7226     }
7227
7228     if (!fp_access_check(s)) {
7229         return;
7230     }
7231
7232     if (size == 2) {
7233         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7234         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7235         TCGv_i64 tcg_res = tcg_temp_new_i64();
7236
7237         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
7238         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
7239
7240         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
7241         gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
7242
7243         switch (opcode) {
7244         case 0xd: /* SQDMULL, SQDMULL2 */
7245             break;
7246         case 0xb: /* SQDMLSL, SQDMLSL2 */
7247             tcg_gen_neg_i64(tcg_res, tcg_res);
7248             /* fall through */
7249         case 0x9: /* SQDMLAL, SQDMLAL2 */
7250             read_vec_element(s, tcg_op1, rd, 0, MO_64);
7251             gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
7252                                               tcg_res, tcg_op1);
7253             break;
7254         default:
7255             g_assert_not_reached();
7256         }
7257
7258         write_fp_dreg(s, rd, tcg_res);
7259
7260         tcg_temp_free_i64(tcg_op1);
7261         tcg_temp_free_i64(tcg_op2);
7262         tcg_temp_free_i64(tcg_res);
7263     } else {
7264         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7265         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7266         TCGv_i64 tcg_res = tcg_temp_new_i64();
7267
7268         read_vec_element_i32(s, tcg_op1, rn, 0, MO_16);
7269         read_vec_element_i32(s, tcg_op2, rm, 0, MO_16);
7270
7271         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
7272         gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
7273
7274         switch (opcode) {
7275         case 0xd: /* SQDMULL, SQDMULL2 */
7276             break;
7277         case 0xb: /* SQDMLSL, SQDMLSL2 */
7278             gen_helper_neon_negl_u32(tcg_res, tcg_res);
7279             /* fall through */
7280         case 0x9: /* SQDMLAL, SQDMLAL2 */
7281         {
7282             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
7283             read_vec_element(s, tcg_op3, rd, 0, MO_32);
7284             gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
7285                                               tcg_res, tcg_op3);
7286             tcg_temp_free_i64(tcg_op3);
7287             break;
7288         }
7289         default:
7290             g_assert_not_reached();
7291         }
7292
7293         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7294         write_fp_dreg(s, rd, tcg_res);
7295
7296         tcg_temp_free_i32(tcg_op1);
7297         tcg_temp_free_i32(tcg_op2);
7298         tcg_temp_free_i64(tcg_res);
7299     }
7300 }
7301
7302 /* CMTST : test is "if (X & Y != 0)". */
7303 static void gen_cmtst_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
7304 {
7305     tcg_gen_and_i32(d, a, b);
7306     tcg_gen_setcondi_i32(TCG_COND_NE, d, d, 0);
7307     tcg_gen_neg_i32(d, d);
7308 }
7309
7310 static void gen_cmtst_i64(TCGv_i64 d, TCGv_i64 a, TCGv_i64 b)
7311 {
7312     tcg_gen_and_i64(d, a, b);
7313     tcg_gen_setcondi_i64(TCG_COND_NE, d, d, 0);
7314     tcg_gen_neg_i64(d, d);
7315 }
7316
7317 static void gen_cmtst_vec(unsigned vece, TCGv_vec d, TCGv_vec a, TCGv_vec b)
7318 {
7319     tcg_gen_and_vec(vece, d, a, b);
7320     tcg_gen_dupi_vec(vece, a, 0);
7321     tcg_gen_cmp_vec(TCG_COND_NE, vece, d, d, a);
7322 }
7323
7324 static void handle_3same_64(DisasContext *s, int opcode, bool u,
7325                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
7326 {
7327     /* Handle 64x64->64 opcodes which are shared between the scalar
7328      * and vector 3-same groups. We cover every opcode where size == 3
7329      * is valid in either the three-reg-same (integer, not pairwise)
7330      * or scalar-three-reg-same groups.
7331      */
7332     TCGCond cond;
7333
7334     switch (opcode) {
7335     case 0x1: /* SQADD */
7336         if (u) {
7337             gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7338         } else {
7339             gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7340         }
7341         break;
7342     case 0x5: /* SQSUB */
7343         if (u) {
7344             gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7345         } else {
7346             gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7347         }
7348         break;
7349     case 0x6: /* CMGT, CMHI */
7350         /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
7351          * We implement this using setcond (test) and then negating.
7352          */
7353         cond = u ? TCG_COND_GTU : TCG_COND_GT;
7354     do_cmop:
7355         tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
7356         tcg_gen_neg_i64(tcg_rd, tcg_rd);
7357         break;
7358     case 0x7: /* CMGE, CMHS */
7359         cond = u ? TCG_COND_GEU : TCG_COND_GE;
7360         goto do_cmop;
7361     case 0x11: /* CMTST, CMEQ */
7362         if (u) {
7363             cond = TCG_COND_EQ;
7364             goto do_cmop;
7365         }
7366         gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
7367         break;
7368     case 0x8: /* SSHL, USHL */
7369         if (u) {
7370             gen_helper_neon_shl_u64(tcg_rd, tcg_rn, tcg_rm);
7371         } else {
7372             gen_helper_neon_shl_s64(tcg_rd, tcg_rn, tcg_rm);
7373         }
7374         break;
7375     case 0x9: /* SQSHL, UQSHL */
7376         if (u) {
7377             gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7378         } else {
7379             gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7380         }
7381         break;
7382     case 0xa: /* SRSHL, URSHL */
7383         if (u) {
7384             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
7385         } else {
7386             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
7387         }
7388         break;
7389     case 0xb: /* SQRSHL, UQRSHL */
7390         if (u) {
7391             gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7392         } else {
7393             gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7394         }
7395         break;
7396     case 0x10: /* ADD, SUB */
7397         if (u) {
7398             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
7399         } else {
7400             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
7401         }
7402         break;
7403     default:
7404         g_assert_not_reached();
7405     }
7406 }
7407
7408 /* Handle the 3-same-operands float operations; shared by the scalar
7409  * and vector encodings. The caller must filter out any encodings
7410  * not allocated for the encoding it is dealing with.
7411  */
7412 static void handle_3same_float(DisasContext *s, int size, int elements,
7413                                int fpopcode, int rd, int rn, int rm)
7414 {
7415     int pass;
7416     TCGv_ptr fpst = get_fpstatus_ptr(false);
7417
7418     for (pass = 0; pass < elements; pass++) {
7419         if (size) {
7420             /* Double */
7421             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7422             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7423             TCGv_i64 tcg_res = tcg_temp_new_i64();
7424
7425             read_vec_element(s, tcg_op1, rn, pass, MO_64);
7426             read_vec_element(s, tcg_op2, rm, pass, MO_64);
7427
7428             switch (fpopcode) {
7429             case 0x39: /* FMLS */
7430                 /* As usual for ARM, separate negation for fused multiply-add */
7431                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
7432                 /* fall through */
7433             case 0x19: /* FMLA */
7434                 read_vec_element(s, tcg_res, rd, pass, MO_64);
7435                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
7436                                        tcg_res, fpst);
7437                 break;
7438             case 0x18: /* FMAXNM */
7439                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7440                 break;
7441             case 0x1a: /* FADD */
7442                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
7443                 break;
7444             case 0x1b: /* FMULX */
7445                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
7446                 break;
7447             case 0x1c: /* FCMEQ */
7448                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7449                 break;
7450             case 0x1e: /* FMAX */
7451                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
7452                 break;
7453             case 0x1f: /* FRECPS */
7454                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7455                 break;
7456             case 0x38: /* FMINNM */
7457                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7458                 break;
7459             case 0x3a: /* FSUB */
7460                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
7461                 break;
7462             case 0x3e: /* FMIN */
7463                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
7464                 break;
7465             case 0x3f: /* FRSQRTS */
7466                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7467                 break;
7468             case 0x5b: /* FMUL */
7469                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
7470                 break;
7471             case 0x5c: /* FCMGE */
7472                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7473                 break;
7474             case 0x5d: /* FACGE */
7475                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7476                 break;
7477             case 0x5f: /* FDIV */
7478                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
7479                 break;
7480             case 0x7a: /* FABD */
7481                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
7482                 gen_helper_vfp_absd(tcg_res, tcg_res);
7483                 break;
7484             case 0x7c: /* FCMGT */
7485                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7486                 break;
7487             case 0x7d: /* FACGT */
7488                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7489                 break;
7490             default:
7491                 g_assert_not_reached();
7492             }
7493
7494             write_vec_element(s, tcg_res, rd, pass, MO_64);
7495
7496             tcg_temp_free_i64(tcg_res);
7497             tcg_temp_free_i64(tcg_op1);
7498             tcg_temp_free_i64(tcg_op2);
7499         } else {
7500             /* Single */
7501             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7502             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7503             TCGv_i32 tcg_res = tcg_temp_new_i32();
7504
7505             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
7506             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
7507
7508             switch (fpopcode) {
7509             case 0x39: /* FMLS */
7510                 /* As usual for ARM, separate negation for fused multiply-add */
7511                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
7512                 /* fall through */
7513             case 0x19: /* FMLA */
7514                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7515                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
7516                                        tcg_res, fpst);
7517                 break;
7518             case 0x1a: /* FADD */
7519                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
7520                 break;
7521             case 0x1b: /* FMULX */
7522                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
7523                 break;
7524             case 0x1c: /* FCMEQ */
7525                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7526                 break;
7527             case 0x1e: /* FMAX */
7528                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
7529                 break;
7530             case 0x1f: /* FRECPS */
7531                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7532                 break;
7533             case 0x18: /* FMAXNM */
7534                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
7535                 break;
7536             case 0x38: /* FMINNM */
7537                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
7538                 break;
7539             case 0x3a: /* FSUB */
7540                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
7541                 break;
7542             case 0x3e: /* FMIN */
7543                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
7544                 break;
7545             case 0x3f: /* FRSQRTS */
7546                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7547                 break;
7548             case 0x5b: /* FMUL */
7549                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
7550                 break;
7551             case 0x5c: /* FCMGE */
7552                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7553                 break;
7554             case 0x5d: /* FACGE */
7555                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7556                 break;
7557             case 0x5f: /* FDIV */
7558                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
7559                 break;
7560             case 0x7a: /* FABD */
7561                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
7562                 gen_helper_vfp_abss(tcg_res, tcg_res);
7563                 break;
7564             case 0x7c: /* FCMGT */
7565                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7566                 break;
7567             case 0x7d: /* FACGT */
7568                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7569                 break;
7570             default:
7571                 g_assert_not_reached();
7572             }
7573
7574             if (elements == 1) {
7575                 /* scalar single so clear high part */
7576                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7577
7578                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
7579                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
7580                 tcg_temp_free_i64(tcg_tmp);
7581             } else {
7582                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7583             }
7584
7585             tcg_temp_free_i32(tcg_res);
7586             tcg_temp_free_i32(tcg_op1);
7587             tcg_temp_free_i32(tcg_op2);
7588         }
7589     }
7590
7591     tcg_temp_free_ptr(fpst);
7592
7593     clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
7594 }
7595
7596 /* AdvSIMD scalar three same
7597  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
7598  * +-----+---+-----------+------+---+------+--------+---+------+------+
7599  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
7600  * +-----+---+-----------+------+---+------+--------+---+------+------+
7601  */
7602 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
7603 {
7604     int rd = extract32(insn, 0, 5);
7605     int rn = extract32(insn, 5, 5);
7606     int opcode = extract32(insn, 11, 5);
7607     int rm = extract32(insn, 16, 5);
7608     int size = extract32(insn, 22, 2);
7609     bool u = extract32(insn, 29, 1);
7610     TCGv_i64 tcg_rd;
7611
7612     if (opcode >= 0x18) {
7613         /* Floating point: U, size[1] and opcode indicate operation */
7614         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
7615         switch (fpopcode) {
7616         case 0x1b: /* FMULX */
7617         case 0x1f: /* FRECPS */
7618         case 0x3f: /* FRSQRTS */
7619         case 0x5d: /* FACGE */
7620         case 0x7d: /* FACGT */
7621         case 0x1c: /* FCMEQ */
7622         case 0x5c: /* FCMGE */
7623         case 0x7c: /* FCMGT */
7624         case 0x7a: /* FABD */
7625             break;
7626         default:
7627             unallocated_encoding(s);
7628             return;
7629         }
7630
7631         if (!fp_access_check(s)) {
7632             return;
7633         }
7634
7635         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
7636         return;
7637     }
7638
7639     switch (opcode) {
7640     case 0x1: /* SQADD, UQADD */
7641     case 0x5: /* SQSUB, UQSUB */
7642     case 0x9: /* SQSHL, UQSHL */
7643     case 0xb: /* SQRSHL, UQRSHL */
7644         break;
7645     case 0x8: /* SSHL, USHL */
7646     case 0xa: /* SRSHL, URSHL */
7647     case 0x6: /* CMGT, CMHI */
7648     case 0x7: /* CMGE, CMHS */
7649     case 0x11: /* CMTST, CMEQ */
7650     case 0x10: /* ADD, SUB (vector) */
7651         if (size != 3) {
7652             unallocated_encoding(s);
7653             return;
7654         }
7655         break;
7656     case 0x16: /* SQDMULH, SQRDMULH (vector) */
7657         if (size != 1 && size != 2) {
7658             unallocated_encoding(s);
7659             return;
7660         }
7661         break;
7662     default:
7663         unallocated_encoding(s);
7664         return;
7665     }
7666
7667     if (!fp_access_check(s)) {
7668         return;
7669     }
7670
7671     tcg_rd = tcg_temp_new_i64();
7672
7673     if (size == 3) {
7674         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
7675         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
7676
7677         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
7678         tcg_temp_free_i64(tcg_rn);
7679         tcg_temp_free_i64(tcg_rm);
7680     } else {
7681         /* Do a single operation on the lowest element in the vector.
7682          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
7683          * no side effects for all these operations.
7684          * OPTME: special-purpose helpers would avoid doing some
7685          * unnecessary work in the helper for the 8 and 16 bit cases.
7686          */
7687         NeonGenTwoOpEnvFn *genenvfn;
7688         TCGv_i32 tcg_rn = tcg_temp_new_i32();
7689         TCGv_i32 tcg_rm = tcg_temp_new_i32();
7690         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
7691
7692         read_vec_element_i32(s, tcg_rn, rn, 0, size);
7693         read_vec_element_i32(s, tcg_rm, rm, 0, size);
7694
7695         switch (opcode) {
7696         case 0x1: /* SQADD, UQADD */
7697         {
7698             static NeonGenTwoOpEnvFn * const fns[3][2] = {
7699                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
7700                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
7701                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
7702             };
7703             genenvfn = fns[size][u];
7704             break;
7705         }
7706         case 0x5: /* SQSUB, UQSUB */
7707         {
7708             static NeonGenTwoOpEnvFn * const fns[3][2] = {
7709                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
7710                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
7711                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
7712             };
7713             genenvfn = fns[size][u];
7714             break;
7715         }
7716         case 0x9: /* SQSHL, UQSHL */
7717         {
7718             static NeonGenTwoOpEnvFn * const fns[3][2] = {
7719                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
7720                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
7721                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
7722             };
7723             genenvfn = fns[size][u];
7724             break;
7725         }
7726         case 0xb: /* SQRSHL, UQRSHL */
7727         {
7728             static NeonGenTwoOpEnvFn * const fns[3][2] = {
7729                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
7730                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
7731                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
7732             };
7733             genenvfn = fns[size][u];
7734             break;
7735         }
7736         case 0x16: /* SQDMULH, SQRDMULH */
7737         {
7738             static NeonGenTwoOpEnvFn * const fns[2][2] = {
7739                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
7740                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
7741             };
7742             assert(size == 1 || size == 2);
7743             genenvfn = fns[size - 1][u];
7744             break;
7745         }
7746         default:
7747             g_assert_not_reached();
7748         }
7749
7750         genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
7751         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
7752         tcg_temp_free_i32(tcg_rd32);
7753         tcg_temp_free_i32(tcg_rn);
7754         tcg_temp_free_i32(tcg_rm);
7755     }
7756
7757     write_fp_dreg(s, rd, tcg_rd);
7758
7759     tcg_temp_free_i64(tcg_rd);
7760 }
7761
7762 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
7763                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
7764                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
7765 {
7766     /* Handle 64->64 opcodes which are shared between the scalar and
7767      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
7768      * is valid in either group and also the double-precision fp ops.
7769      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
7770      * requires them.
7771      */
7772     TCGCond cond;
7773
7774     switch (opcode) {
7775     case 0x4: /* CLS, CLZ */
7776         if (u) {
7777             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
7778         } else {
7779             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
7780         }
7781         break;
7782     case 0x5: /* NOT */
7783         /* This opcode is shared with CNT and RBIT but we have earlier
7784          * enforced that size == 3 if and only if this is the NOT insn.
7785          */
7786         tcg_gen_not_i64(tcg_rd, tcg_rn);
7787         break;
7788     case 0x7: /* SQABS, SQNEG */
7789         if (u) {
7790             gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
7791         } else {
7792             gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
7793         }
7794         break;
7795     case 0xa: /* CMLT */
7796         /* 64 bit integer comparison against zero, result is
7797          * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
7798          * subtracting 1.
7799          */
7800         cond = TCG_COND_LT;
7801     do_cmop:
7802         tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
7803         tcg_gen_neg_i64(tcg_rd, tcg_rd);
7804         break;
7805     case 0x8: /* CMGT, CMGE */
7806         cond = u ? TCG_COND_GE : TCG_COND_GT;
7807         goto do_cmop;
7808     case 0x9: /* CMEQ, CMLE */
7809         cond = u ? TCG_COND_LE : TCG_COND_EQ;
7810         goto do_cmop;
7811     case 0xb: /* ABS, NEG */
7812         if (u) {
7813             tcg_gen_neg_i64(tcg_rd, tcg_rn);
7814         } else {
7815             TCGv_i64 tcg_zero = tcg_const_i64(0);
7816             tcg_gen_neg_i64(tcg_rd, tcg_rn);
7817             tcg_gen_movcond_i64(TCG_COND_GT, tcg_rd, tcg_rn, tcg_zero,
7818                                 tcg_rn, tcg_rd);
7819             tcg_temp_free_i64(tcg_zero);
7820         }
7821         break;
7822     case 0x2f: /* FABS */
7823         gen_helper_vfp_absd(tcg_rd, tcg_rn);
7824         break;
7825     case 0x6f: /* FNEG */
7826         gen_helper_vfp_negd(tcg_rd, tcg_rn);
7827         break;
7828     case 0x7f: /* FSQRT */
7829         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
7830         break;
7831     case 0x1a: /* FCVTNS */
7832     case 0x1b: /* FCVTMS */
7833     case 0x1c: /* FCVTAS */
7834     case 0x3a: /* FCVTPS */
7835     case 0x3b: /* FCVTZS */
7836     {
7837         TCGv_i32 tcg_shift = tcg_const_i32(0);
7838         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7839         tcg_temp_free_i32(tcg_shift);
7840         break;
7841     }
7842     case 0x5a: /* FCVTNU */
7843     case 0x5b: /* FCVTMU */
7844     case 0x5c: /* FCVTAU */
7845     case 0x7a: /* FCVTPU */
7846     case 0x7b: /* FCVTZU */
7847     {
7848         TCGv_i32 tcg_shift = tcg_const_i32(0);
7849         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7850         tcg_temp_free_i32(tcg_shift);
7851         break;
7852     }
7853     case 0x18: /* FRINTN */
7854     case 0x19: /* FRINTM */
7855     case 0x38: /* FRINTP */
7856     case 0x39: /* FRINTZ */
7857     case 0x58: /* FRINTA */
7858     case 0x79: /* FRINTI */
7859         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
7860         break;
7861     case 0x59: /* FRINTX */
7862         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
7863         break;
7864     default:
7865         g_assert_not_reached();
7866     }
7867 }
7868
7869 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
7870                                    bool is_scalar, bool is_u, bool is_q,
7871                                    int size, int rn, int rd)
7872 {
7873     bool is_double = (size == MO_64);
7874     TCGv_ptr fpst;
7875
7876     if (!fp_access_check(s)) {
7877         return;
7878     }
7879
7880     fpst = get_fpstatus_ptr(size == MO_16);
7881
7882     if (is_double) {
7883         TCGv_i64 tcg_op = tcg_temp_new_i64();
7884         TCGv_i64 tcg_zero = tcg_const_i64(0);
7885         TCGv_i64 tcg_res = tcg_temp_new_i64();
7886         NeonGenTwoDoubleOPFn *genfn;
7887         bool swap = false;
7888         int pass;
7889
7890         switch (opcode) {
7891         case 0x2e: /* FCMLT (zero) */
7892             swap = true;
7893             /* fallthrough */
7894         case 0x2c: /* FCMGT (zero) */
7895             genfn = gen_helper_neon_cgt_f64;
7896             break;
7897         case 0x2d: /* FCMEQ (zero) */
7898             genfn = gen_helper_neon_ceq_f64;
7899             break;
7900         case 0x6d: /* FCMLE (zero) */
7901             swap = true;
7902             /* fall through */
7903         case 0x6c: /* FCMGE (zero) */
7904             genfn = gen_helper_neon_cge_f64;
7905             break;
7906         default:
7907             g_assert_not_reached();
7908         }
7909
7910         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7911             read_vec_element(s, tcg_op, rn, pass, MO_64);
7912             if (swap) {
7913                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7914             } else {
7915                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7916             }
7917             write_vec_element(s, tcg_res, rd, pass, MO_64);
7918         }
7919         tcg_temp_free_i64(tcg_res);
7920         tcg_temp_free_i64(tcg_zero);
7921         tcg_temp_free_i64(tcg_op);
7922
7923         clear_vec_high(s, !is_scalar, rd);
7924     } else {
7925         TCGv_i32 tcg_op = tcg_temp_new_i32();
7926         TCGv_i32 tcg_zero = tcg_const_i32(0);
7927         TCGv_i32 tcg_res = tcg_temp_new_i32();
7928         NeonGenTwoSingleOPFn *genfn;
7929         bool swap = false;
7930         int pass, maxpasses;
7931
7932         if (size == MO_16) {
7933             switch (opcode) {
7934             case 0x2e: /* FCMLT (zero) */
7935                 swap = true;
7936                 /* fall through */
7937             case 0x2c: /* FCMGT (zero) */
7938                 genfn = gen_helper_advsimd_cgt_f16;
7939                 break;
7940             case 0x2d: /* FCMEQ (zero) */
7941                 genfn = gen_helper_advsimd_ceq_f16;
7942                 break;
7943             case 0x6d: /* FCMLE (zero) */
7944                 swap = true;
7945                 /* fall through */
7946             case 0x6c: /* FCMGE (zero) */
7947                 genfn = gen_helper_advsimd_cge_f16;
7948                 break;
7949             default:
7950                 g_assert_not_reached();
7951             }
7952         } else {
7953             switch (opcode) {
7954             case 0x2e: /* FCMLT (zero) */
7955                 swap = true;
7956                 /* fall through */
7957             case 0x2c: /* FCMGT (zero) */
7958                 genfn = gen_helper_neon_cgt_f32;
7959                 break;
7960             case 0x2d: /* FCMEQ (zero) */
7961                 genfn = gen_helper_neon_ceq_f32;
7962                 break;
7963             case 0x6d: /* FCMLE (zero) */
7964                 swap = true;
7965                 /* fall through */
7966             case 0x6c: /* FCMGE (zero) */
7967                 genfn = gen_helper_neon_cge_f32;
7968                 break;
7969             default:
7970                 g_assert_not_reached();
7971             }
7972         }
7973
7974         if (is_scalar) {
7975             maxpasses = 1;
7976         } else {
7977             int vector_size = 8 << is_q;
7978             maxpasses = vector_size >> size;
7979         }
7980
7981         for (pass = 0; pass < maxpasses; pass++) {
7982             read_vec_element_i32(s, tcg_op, rn, pass, size);
7983             if (swap) {
7984                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7985             } else {
7986                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7987             }
7988             if (is_scalar) {
7989                 write_fp_sreg(s, rd, tcg_res);
7990             } else {
7991                 write_vec_element_i32(s, tcg_res, rd, pass, size);
7992             }
7993         }
7994         tcg_temp_free_i32(tcg_res);
7995         tcg_temp_free_i32(tcg_zero);
7996         tcg_temp_free_i32(tcg_op);
7997         if (!is_scalar) {
7998             clear_vec_high(s, is_q, rd);
7999         }
8000     }
8001
8002     tcg_temp_free_ptr(fpst);
8003 }
8004
8005 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
8006                                     bool is_scalar, bool is_u, bool is_q,
8007                                     int size, int rn, int rd)
8008 {
8009     bool is_double = (size == 3);
8010     TCGv_ptr fpst = get_fpstatus_ptr(false);
8011
8012     if (is_double) {
8013         TCGv_i64 tcg_op = tcg_temp_new_i64();
8014         TCGv_i64 tcg_res = tcg_temp_new_i64();
8015         int pass;
8016
8017         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
8018             read_vec_element(s, tcg_op, rn, pass, MO_64);
8019             switch (opcode) {
8020             case 0x3d: /* FRECPE */
8021                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
8022                 break;
8023             case 0x3f: /* FRECPX */
8024                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
8025                 break;
8026             case 0x7d: /* FRSQRTE */
8027                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
8028                 break;
8029             default:
8030                 g_assert_not_reached();
8031             }
8032             write_vec_element(s, tcg_res, rd, pass, MO_64);
8033         }
8034         tcg_temp_free_i64(tcg_res);
8035         tcg_temp_free_i64(tcg_op);
8036         clear_vec_high(s, !is_scalar, rd);
8037     } else {
8038         TCGv_i32 tcg_op = tcg_temp_new_i32();
8039         TCGv_i32 tcg_res = tcg_temp_new_i32();
8040         int pass, maxpasses;
8041
8042         if (is_scalar) {
8043             maxpasses = 1;
8044         } else {
8045             maxpasses = is_q ? 4 : 2;
8046         }
8047
8048         for (pass = 0; pass < maxpasses; pass++) {
8049             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
8050
8051             switch (opcode) {
8052             case 0x3c: /* URECPE */
8053                 gen_helper_recpe_u32(tcg_res, tcg_op, fpst);
8054                 break;
8055             case 0x3d: /* FRECPE */
8056                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
8057                 break;
8058             case 0x3f: /* FRECPX */
8059                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
8060                 break;
8061             case 0x7d: /* FRSQRTE */
8062                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
8063                 break;
8064             default:
8065                 g_assert_not_reached();
8066             }
8067
8068             if (is_scalar) {
8069                 write_fp_sreg(s, rd, tcg_res);
8070             } else {
8071                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
8072             }
8073         }
8074         tcg_temp_free_i32(tcg_res);
8075         tcg_temp_free_i32(tcg_op);
8076         if (!is_scalar) {
8077             clear_vec_high(s, is_q, rd);
8078         }
8079     }
8080     tcg_temp_free_ptr(fpst);
8081 }
8082
8083 static void handle_2misc_narrow(DisasContext *s, bool scalar,
8084                                 int opcode, bool u, bool is_q,
8085                                 int size, int rn, int rd)
8086 {
8087     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
8088      * in the source becomes a size element in the destination).
8089      */
8090     int pass;
8091     TCGv_i32 tcg_res[2];
8092     int destelt = is_q ? 2 : 0;
8093     int passes = scalar ? 1 : 2;
8094
8095     if (scalar) {
8096         tcg_res[1] = tcg_const_i32(0);
8097     }
8098
8099     for (pass = 0; pass < passes; pass++) {
8100         TCGv_i64 tcg_op = tcg_temp_new_i64();
8101         NeonGenNarrowFn *genfn = NULL;
8102         NeonGenNarrowEnvFn *genenvfn = NULL;
8103
8104         if (scalar) {
8105             read_vec_element(s, tcg_op, rn, pass, size + 1);
8106         } else {
8107             read_vec_element(s, tcg_op, rn, pass, MO_64);
8108         }
8109         tcg_res[pass] = tcg_temp_new_i32();
8110
8111         switch (opcode) {
8112         case 0x12: /* XTN, SQXTUN */
8113         {
8114             static NeonGenNarrowFn * const xtnfns[3] = {
8115                 gen_helper_neon_narrow_u8,
8116                 gen_helper_neon_narrow_u16,
8117                 tcg_gen_extrl_i64_i32,
8118             };
8119             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
8120                 gen_helper_neon_unarrow_sat8,
8121                 gen_helper_neon_unarrow_sat16,
8122                 gen_helper_neon_unarrow_sat32,
8123             };
8124             if (u) {
8125                 genenvfn = sqxtunfns[size];
8126             } else {
8127                 genfn = xtnfns[size];
8128             }
8129             break;
8130         }
8131         case 0x14: /* SQXTN, UQXTN */
8132         {
8133             static NeonGenNarrowEnvFn * const fns[3][2] = {
8134                 { gen_helper_neon_narrow_sat_s8,
8135                   gen_helper_neon_narrow_sat_u8 },
8136                 { gen_helper_neon_narrow_sat_s16,
8137                   gen_helper_neon_narrow_sat_u16 },
8138                 { gen_helper_neon_narrow_sat_s32,
8139                   gen_helper_neon_narrow_sat_u32 },
8140             };
8141             genenvfn = fns[size][u];
8142             break;
8143         }
8144         case 0x16: /* FCVTN, FCVTN2 */
8145             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
8146             if (size == 2) {
8147                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
8148             } else {
8149                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
8150                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
8151                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
8152                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, cpu_env);
8153                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, cpu_env);
8154                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
8155                 tcg_temp_free_i32(tcg_lo);
8156                 tcg_temp_free_i32(tcg_hi);
8157             }
8158             break;
8159         case 0x56:  /* FCVTXN, FCVTXN2 */
8160             /* 64 bit to 32 bit float conversion
8161              * with von Neumann rounding (round to odd)
8162              */
8163             assert(size == 2);
8164             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
8165             break;
8166         default:
8167             g_assert_not_reached();
8168         }
8169
8170         if (genfn) {
8171             genfn(tcg_res[pass], tcg_op);
8172         } else if (genenvfn) {
8173             genenvfn(tcg_res[pass], cpu_env, tcg_op);
8174         }
8175
8176         tcg_temp_free_i64(tcg_op);
8177     }
8178
8179     for (pass = 0; pass < 2; pass++) {
8180         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
8181         tcg_temp_free_i32(tcg_res[pass]);
8182     }
8183     clear_vec_high(s, is_q, rd);
8184 }
8185
8186 /* Remaining saturating accumulating ops */
8187 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
8188                                 bool is_q, int size, int rn, int rd)
8189 {
8190     bool is_double = (size == 3);
8191
8192     if (is_double) {
8193         TCGv_i64 tcg_rn = tcg_temp_new_i64();
8194         TCGv_i64 tcg_rd = tcg_temp_new_i64();
8195         int pass;
8196
8197         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
8198             read_vec_element(s, tcg_rn, rn, pass, MO_64);
8199             read_vec_element(s, tcg_rd, rd, pass, MO_64);
8200
8201             if (is_u) { /* USQADD */
8202                 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8203             } else { /* SUQADD */
8204                 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8205             }
8206             write_vec_element(s, tcg_rd, rd, pass, MO_64);
8207         }
8208         tcg_temp_free_i64(tcg_rd);
8209         tcg_temp_free_i64(tcg_rn);
8210         clear_vec_high(s, !is_scalar, rd);
8211     } else {
8212         TCGv_i32 tcg_rn = tcg_temp_new_i32();
8213         TCGv_i32 tcg_rd = tcg_temp_new_i32();
8214         int pass, maxpasses;
8215
8216         if (is_scalar) {
8217             maxpasses = 1;
8218         } else {
8219             maxpasses = is_q ? 4 : 2;
8220         }
8221
8222         for (pass = 0; pass < maxpasses; pass++) {
8223             if (is_scalar) {
8224                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
8225                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
8226             } else {
8227                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
8228                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
8229             }
8230
8231             if (is_u) { /* USQADD */
8232                 switch (size) {
8233                 case 0:
8234                     gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8235                     break;
8236                 case 1:
8237                     gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8238                     break;
8239                 case 2:
8240                     gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8241                     break;
8242                 default:
8243                     g_assert_not_reached();
8244                 }
8245             } else { /* SUQADD */
8246                 switch (size) {
8247                 case 0:
8248                     gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8249                     break;
8250                 case 1:
8251                     gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8252                     break;
8253                 case 2:
8254                     gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8255                     break;
8256                 default:
8257                     g_assert_not_reached();
8258                 }
8259             }
8260
8261             if (is_scalar) {
8262                 TCGv_i64 tcg_zero = tcg_const_i64(0);
8263                 write_vec_element(s, tcg_zero, rd, 0, MO_64);
8264                 tcg_temp_free_i64(tcg_zero);
8265             }
8266             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
8267         }
8268         tcg_temp_free_i32(tcg_rd);
8269         tcg_temp_free_i32(tcg_rn);
8270         clear_vec_high(s, is_q, rd);
8271     }
8272 }
8273
8274 /* AdvSIMD scalar two reg misc
8275  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
8276  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8277  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
8278  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8279  */
8280 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
8281 {
8282     int rd = extract32(insn, 0, 5);
8283     int rn = extract32(insn, 5, 5);
8284     int opcode = extract32(insn, 12, 5);
8285     int size = extract32(insn, 22, 2);
8286     bool u = extract32(insn, 29, 1);
8287     bool is_fcvt = false;
8288     int rmode;
8289     TCGv_i32 tcg_rmode;
8290     TCGv_ptr tcg_fpstatus;
8291
8292     switch (opcode) {
8293     case 0x3: /* USQADD / SUQADD*/
8294         if (!fp_access_check(s)) {
8295             return;
8296         }
8297         handle_2misc_satacc(s, true, u, false, size, rn, rd);
8298         return;
8299     case 0x7: /* SQABS / SQNEG */
8300         break;
8301     case 0xa: /* CMLT */
8302         if (u) {
8303             unallocated_encoding(s);
8304             return;
8305         }
8306         /* fall through */
8307     case 0x8: /* CMGT, CMGE */
8308     case 0x9: /* CMEQ, CMLE */
8309     case 0xb: /* ABS, NEG */
8310         if (size != 3) {
8311             unallocated_encoding(s);
8312             return;
8313         }
8314         break;
8315     case 0x12: /* SQXTUN */
8316         if (!u) {
8317             unallocated_encoding(s);
8318             return;
8319         }
8320         /* fall through */
8321     case 0x14: /* SQXTN, UQXTN */
8322         if (size == 3) {
8323             unallocated_encoding(s);
8324             return;
8325         }
8326         if (!fp_access_check(s)) {
8327             return;
8328         }
8329         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
8330         return;
8331     case 0xc ... 0xf:
8332     case 0x16 ... 0x1d:
8333     case 0x1f:
8334         /* Floating point: U, size[1] and opcode indicate operation;
8335          * size[0] indicates single or double precision.
8336          */
8337         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
8338         size = extract32(size, 0, 1) ? 3 : 2;
8339         switch (opcode) {
8340         case 0x2c: /* FCMGT (zero) */
8341         case 0x2d: /* FCMEQ (zero) */
8342         case 0x2e: /* FCMLT (zero) */
8343         case 0x6c: /* FCMGE (zero) */
8344         case 0x6d: /* FCMLE (zero) */
8345             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
8346             return;
8347         case 0x1d: /* SCVTF */
8348         case 0x5d: /* UCVTF */
8349         {
8350             bool is_signed = (opcode == 0x1d);
8351             if (!fp_access_check(s)) {
8352                 return;
8353             }
8354             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
8355             return;
8356         }
8357         case 0x3d: /* FRECPE */
8358         case 0x3f: /* FRECPX */
8359         case 0x7d: /* FRSQRTE */
8360             if (!fp_access_check(s)) {
8361                 return;
8362             }
8363             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
8364             return;
8365         case 0x1a: /* FCVTNS */
8366         case 0x1b: /* FCVTMS */
8367         case 0x3a: /* FCVTPS */
8368         case 0x3b: /* FCVTZS */
8369         case 0x5a: /* FCVTNU */
8370         case 0x5b: /* FCVTMU */
8371         case 0x7a: /* FCVTPU */
8372         case 0x7b: /* FCVTZU */
8373             is_fcvt = true;
8374             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
8375             break;
8376         case 0x1c: /* FCVTAS */
8377         case 0x5c: /* FCVTAU */
8378             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
8379             is_fcvt = true;
8380             rmode = FPROUNDING_TIEAWAY;
8381             break;
8382         case 0x56: /* FCVTXN, FCVTXN2 */
8383             if (size == 2) {
8384                 unallocated_encoding(s);
8385                 return;
8386             }
8387             if (!fp_access_check(s)) {
8388                 return;
8389             }
8390             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
8391             return;
8392         default:
8393             unallocated_encoding(s);
8394             return;
8395         }
8396         break;
8397     default:
8398         unallocated_encoding(s);
8399         return;
8400     }
8401
8402     if (!fp_access_check(s)) {
8403         return;
8404     }
8405
8406     if (is_fcvt) {
8407         tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
8408         tcg_fpstatus = get_fpstatus_ptr(false);
8409         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
8410     } else {
8411         tcg_rmode = NULL;
8412         tcg_fpstatus = NULL;
8413     }
8414
8415     if (size == 3) {
8416         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
8417         TCGv_i64 tcg_rd = tcg_temp_new_i64();
8418
8419         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
8420         write_fp_dreg(s, rd, tcg_rd);
8421         tcg_temp_free_i64(tcg_rd);
8422         tcg_temp_free_i64(tcg_rn);
8423     } else {
8424         TCGv_i32 tcg_rn = tcg_temp_new_i32();
8425         TCGv_i32 tcg_rd = tcg_temp_new_i32();
8426
8427         read_vec_element_i32(s, tcg_rn, rn, 0, size);
8428
8429         switch (opcode) {
8430         case 0x7: /* SQABS, SQNEG */
8431         {
8432             NeonGenOneOpEnvFn *genfn;
8433             static NeonGenOneOpEnvFn * const fns[3][2] = {
8434                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
8435                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
8436                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
8437             };
8438             genfn = fns[size][u];
8439             genfn(tcg_rd, cpu_env, tcg_rn);
8440             break;
8441         }
8442         case 0x1a: /* FCVTNS */
8443         case 0x1b: /* FCVTMS */
8444         case 0x1c: /* FCVTAS */
8445         case 0x3a: /* FCVTPS */
8446         case 0x3b: /* FCVTZS */
8447         {
8448             TCGv_i32 tcg_shift = tcg_const_i32(0);
8449             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
8450             tcg_temp_free_i32(tcg_shift);
8451             break;
8452         }
8453         case 0x5a: /* FCVTNU */
8454         case 0x5b: /* FCVTMU */
8455         case 0x5c: /* FCVTAU */
8456         case 0x7a: /* FCVTPU */
8457         case 0x7b: /* FCVTZU */
8458         {
8459             TCGv_i32 tcg_shift = tcg_const_i32(0);
8460             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
8461             tcg_temp_free_i32(tcg_shift);
8462             break;
8463         }
8464         default:
8465             g_assert_not_reached();
8466         }
8467
8468         write_fp_sreg(s, rd, tcg_rd);
8469         tcg_temp_free_i32(tcg_rd);
8470         tcg_temp_free_i32(tcg_rn);
8471     }
8472
8473     if (is_fcvt) {
8474         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
8475         tcg_temp_free_i32(tcg_rmode);
8476         tcg_temp_free_ptr(tcg_fpstatus);
8477     }
8478 }
8479
8480 static void gen_ssra8_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8481 {
8482     tcg_gen_vec_sar8i_i64(a, a, shift);
8483     tcg_gen_vec_add8_i64(d, d, a);
8484 }
8485
8486 static void gen_ssra16_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8487 {
8488     tcg_gen_vec_sar16i_i64(a, a, shift);
8489     tcg_gen_vec_add16_i64(d, d, a);
8490 }
8491
8492 static void gen_ssra32_i32(TCGv_i32 d, TCGv_i32 a, int32_t shift)
8493 {
8494     tcg_gen_sari_i32(a, a, shift);
8495     tcg_gen_add_i32(d, d, a);
8496 }
8497
8498 static void gen_ssra64_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8499 {
8500     tcg_gen_sari_i64(a, a, shift);
8501     tcg_gen_add_i64(d, d, a);
8502 }
8503
8504 static void gen_ssra_vec(unsigned vece, TCGv_vec d, TCGv_vec a, int64_t sh)
8505 {
8506     tcg_gen_sari_vec(vece, a, a, sh);
8507     tcg_gen_add_vec(vece, d, d, a);
8508 }
8509
8510 static void gen_usra8_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8511 {
8512     tcg_gen_vec_shr8i_i64(a, a, shift);
8513     tcg_gen_vec_add8_i64(d, d, a);
8514 }
8515
8516 static void gen_usra16_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8517 {
8518     tcg_gen_vec_shr16i_i64(a, a, shift);
8519     tcg_gen_vec_add16_i64(d, d, a);
8520 }
8521
8522 static void gen_usra32_i32(TCGv_i32 d, TCGv_i32 a, int32_t shift)
8523 {
8524     tcg_gen_shri_i32(a, a, shift);
8525     tcg_gen_add_i32(d, d, a);
8526 }
8527
8528 static void gen_usra64_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8529 {
8530     tcg_gen_shri_i64(a, a, shift);
8531     tcg_gen_add_i64(d, d, a);
8532 }
8533
8534 static void gen_usra_vec(unsigned vece, TCGv_vec d, TCGv_vec a, int64_t sh)
8535 {
8536     tcg_gen_shri_vec(vece, a, a, sh);
8537     tcg_gen_add_vec(vece, d, d, a);
8538 }
8539
8540 static void gen_shr8_ins_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8541 {
8542     uint64_t mask = dup_const(MO_8, 0xff >> shift);
8543     TCGv_i64 t = tcg_temp_new_i64();
8544
8545     tcg_gen_shri_i64(t, a, shift);
8546     tcg_gen_andi_i64(t, t, mask);
8547     tcg_gen_andi_i64(d, d, ~mask);
8548     tcg_gen_or_i64(d, d, t);
8549     tcg_temp_free_i64(t);
8550 }
8551
8552 static void gen_shr16_ins_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8553 {
8554     uint64_t mask = dup_const(MO_16, 0xffff >> shift);
8555     TCGv_i64 t = tcg_temp_new_i64();
8556
8557     tcg_gen_shri_i64(t, a, shift);
8558     tcg_gen_andi_i64(t, t, mask);
8559     tcg_gen_andi_i64(d, d, ~mask);
8560     tcg_gen_or_i64(d, d, t);
8561     tcg_temp_free_i64(t);
8562 }
8563
8564 static void gen_shr32_ins_i32(TCGv_i32 d, TCGv_i32 a, int32_t shift)
8565 {
8566     tcg_gen_shri_i32(a, a, shift);
8567     tcg_gen_deposit_i32(d, d, a, 0, 32 - shift);
8568 }
8569
8570 static void gen_shr64_ins_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8571 {
8572     tcg_gen_shri_i64(a, a, shift);
8573     tcg_gen_deposit_i64(d, d, a, 0, 64 - shift);
8574 }
8575
8576 static void gen_shr_ins_vec(unsigned vece, TCGv_vec d, TCGv_vec a, int64_t sh)
8577 {
8578     uint64_t mask = (2ull << ((8 << vece) - 1)) - 1;
8579     TCGv_vec t = tcg_temp_new_vec_matching(d);
8580     TCGv_vec m = tcg_temp_new_vec_matching(d);
8581
8582     tcg_gen_dupi_vec(vece, m, mask ^ (mask >> sh));
8583     tcg_gen_shri_vec(vece, t, a, sh);
8584     tcg_gen_and_vec(vece, d, d, m);
8585     tcg_gen_or_vec(vece, d, d, t);
8586
8587     tcg_temp_free_vec(t);
8588     tcg_temp_free_vec(m);
8589 }
8590
8591 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
8592 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
8593                                  int immh, int immb, int opcode, int rn, int rd)
8594 {
8595     static const GVecGen2i ssra_op[4] = {
8596         { .fni8 = gen_ssra8_i64,
8597           .fniv = gen_ssra_vec,
8598           .load_dest = true,
8599           .opc = INDEX_op_sari_vec,
8600           .vece = MO_8 },
8601         { .fni8 = gen_ssra16_i64,
8602           .fniv = gen_ssra_vec,
8603           .load_dest = true,
8604           .opc = INDEX_op_sari_vec,
8605           .vece = MO_16 },
8606         { .fni4 = gen_ssra32_i32,
8607           .fniv = gen_ssra_vec,
8608           .load_dest = true,
8609           .opc = INDEX_op_sari_vec,
8610           .vece = MO_32 },
8611         { .fni8 = gen_ssra64_i64,
8612           .fniv = gen_ssra_vec,
8613           .prefer_i64 = TCG_TARGET_REG_BITS == 64,
8614           .load_dest = true,
8615           .opc = INDEX_op_sari_vec,
8616           .vece = MO_64 },
8617     };
8618     static const GVecGen2i usra_op[4] = {
8619         { .fni8 = gen_usra8_i64,
8620           .fniv = gen_usra_vec,
8621           .load_dest = true,
8622           .opc = INDEX_op_shri_vec,
8623           .vece = MO_8, },
8624         { .fni8 = gen_usra16_i64,
8625           .fniv = gen_usra_vec,
8626           .load_dest = true,
8627           .opc = INDEX_op_shri_vec,
8628           .vece = MO_16, },
8629         { .fni4 = gen_usra32_i32,
8630           .fniv = gen_usra_vec,
8631           .load_dest = true,
8632           .opc = INDEX_op_shri_vec,
8633           .vece = MO_32, },
8634         { .fni8 = gen_usra64_i64,
8635           .fniv = gen_usra_vec,
8636           .prefer_i64 = TCG_TARGET_REG_BITS == 64,
8637           .load_dest = true,
8638           .opc = INDEX_op_shri_vec,
8639           .vece = MO_64, },
8640     };
8641     static const GVecGen2i sri_op[4] = {
8642         { .fni8 = gen_shr8_ins_i64,
8643           .fniv = gen_shr_ins_vec,
8644           .load_dest = true,
8645           .opc = INDEX_op_shri_vec,
8646           .vece = MO_8 },
8647         { .fni8 = gen_shr16_ins_i64,
8648           .fniv = gen_shr_ins_vec,
8649           .load_dest = true,
8650           .opc = INDEX_op_shri_vec,
8651           .vece = MO_16 },
8652         { .fni4 = gen_shr32_ins_i32,
8653           .fniv = gen_shr_ins_vec,
8654           .load_dest = true,
8655           .opc = INDEX_op_shri_vec,
8656           .vece = MO_32 },
8657         { .fni8 = gen_shr64_ins_i64,
8658           .fniv = gen_shr_ins_vec,
8659           .prefer_i64 = TCG_TARGET_REG_BITS == 64,
8660           .load_dest = true,
8661           .opc = INDEX_op_shri_vec,
8662           .vece = MO_64 },
8663     };
8664
8665     int size = 32 - clz32(immh) - 1;
8666     int immhb = immh << 3 | immb;
8667     int shift = 2 * (8 << size) - immhb;
8668     bool accumulate = false;
8669     int dsize = is_q ? 128 : 64;
8670     int esize = 8 << size;
8671     int elements = dsize/esize;
8672     TCGMemOp memop = size | (is_u ? 0 : MO_SIGN);
8673     TCGv_i64 tcg_rn = new_tmp_a64(s);
8674     TCGv_i64 tcg_rd = new_tmp_a64(s);
8675     TCGv_i64 tcg_round;
8676     uint64_t round_const;
8677     int i;
8678
8679     if (extract32(immh, 3, 1) && !is_q) {
8680         unallocated_encoding(s);
8681         return;
8682     }
8683
8684     if (size > 3 && !is_q) {
8685         unallocated_encoding(s);
8686         return;
8687     }
8688
8689     if (!fp_access_check(s)) {
8690         return;
8691     }
8692
8693     switch (opcode) {
8694     case 0x02: /* SSRA / USRA (accumulate) */
8695         if (is_u) {
8696             /* Shift count same as element size produces zero to add.  */
8697             if (shift == 8 << size) {
8698                 goto done;
8699             }
8700             gen_gvec_op2i(s, is_q, rd, rn, shift, &usra_op[size]);
8701         } else {
8702             /* Shift count same as element size produces all sign to add.  */
8703             if (shift == 8 << size) {
8704                 shift -= 1;
8705             }
8706             gen_gvec_op2i(s, is_q, rd, rn, shift, &ssra_op[size]);
8707         }
8708         return;
8709     case 0x08: /* SRI */
8710         /* Shift count same as element size is valid but does nothing.  */
8711         if (shift == 8 << size) {
8712             goto done;
8713         }
8714         gen_gvec_op2i(s, is_q, rd, rn, shift, &sri_op[size]);
8715         return;
8716
8717     case 0x00: /* SSHR / USHR */
8718         if (is_u) {
8719             if (shift == 8 << size) {
8720                 /* Shift count the same size as element size produces zero.  */
8721                 tcg_gen_gvec_dup8i(vec_full_reg_offset(s, rd),
8722                                    is_q ? 16 : 8, vec_full_reg_size(s), 0);
8723             } else {
8724                 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shri, size);
8725             }
8726         } else {
8727             /* Shift count the same size as element size produces all sign.  */
8728             if (shift == 8 << size) {
8729                 shift -= 1;
8730             }
8731             gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_sari, size);
8732         }
8733         return;
8734
8735     case 0x04: /* SRSHR / URSHR (rounding) */
8736         break;
8737     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8738         accumulate = true;
8739         break;
8740     default:
8741         g_assert_not_reached();
8742     }
8743
8744     round_const = 1ULL << (shift - 1);
8745     tcg_round = tcg_const_i64(round_const);
8746
8747     for (i = 0; i < elements; i++) {
8748         read_vec_element(s, tcg_rn, rn, i, memop);
8749         if (accumulate) {
8750             read_vec_element(s, tcg_rd, rd, i, memop);
8751         }
8752
8753         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8754                                 accumulate, is_u, size, shift);
8755
8756         write_vec_element(s, tcg_rd, rd, i, size);
8757     }
8758     tcg_temp_free_i64(tcg_round);
8759
8760  done:
8761     clear_vec_high(s, is_q, rd);
8762 }
8763
8764 static void gen_shl8_ins_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8765 {
8766     uint64_t mask = dup_const(MO_8, 0xff << shift);
8767     TCGv_i64 t = tcg_temp_new_i64();
8768
8769     tcg_gen_shli_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_shl16_ins_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8777 {
8778     uint64_t mask = dup_const(MO_16, 0xffff << shift);
8779     TCGv_i64 t = tcg_temp_new_i64();
8780
8781     tcg_gen_shli_i64(t, a, shift);
8782     tcg_gen_andi_i64(t, t, mask);
8783     tcg_gen_andi_i64(d, d, ~mask);
8784     tcg_gen_or_i64(d, d, t);
8785     tcg_temp_free_i64(t);
8786 }
8787
8788 static void gen_shl32_ins_i32(TCGv_i32 d, TCGv_i32 a, int32_t shift)
8789 {
8790     tcg_gen_deposit_i32(d, d, a, shift, 32 - shift);
8791 }
8792
8793 static void gen_shl64_ins_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8794 {
8795     tcg_gen_deposit_i64(d, d, a, shift, 64 - shift);
8796 }
8797
8798 static void gen_shl_ins_vec(unsigned vece, TCGv_vec d, TCGv_vec a, int64_t sh)
8799 {
8800     uint64_t mask = (1ull << sh) - 1;
8801     TCGv_vec t = tcg_temp_new_vec_matching(d);
8802     TCGv_vec m = tcg_temp_new_vec_matching(d);
8803
8804     tcg_gen_dupi_vec(vece, m, mask);
8805     tcg_gen_shli_vec(vece, t, a, sh);
8806     tcg_gen_and_vec(vece, d, d, m);
8807     tcg_gen_or_vec(vece, d, d, t);
8808
8809     tcg_temp_free_vec(t);
8810     tcg_temp_free_vec(m);
8811 }
8812
8813 /* SHL/SLI - Vector shift left */
8814 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
8815                                  int immh, int immb, int opcode, int rn, int rd)
8816 {
8817     static const GVecGen2i shi_op[4] = {
8818         { .fni8 = gen_shl8_ins_i64,
8819           .fniv = gen_shl_ins_vec,
8820           .opc = INDEX_op_shli_vec,
8821           .prefer_i64 = TCG_TARGET_REG_BITS == 64,
8822           .load_dest = true,
8823           .vece = MO_8 },
8824         { .fni8 = gen_shl16_ins_i64,
8825           .fniv = gen_shl_ins_vec,
8826           .opc = INDEX_op_shli_vec,
8827           .prefer_i64 = TCG_TARGET_REG_BITS == 64,
8828           .load_dest = true,
8829           .vece = MO_16 },
8830         { .fni4 = gen_shl32_ins_i32,
8831           .fniv = gen_shl_ins_vec,
8832           .opc = INDEX_op_shli_vec,
8833           .prefer_i64 = TCG_TARGET_REG_BITS == 64,
8834           .load_dest = true,
8835           .vece = MO_32 },
8836         { .fni8 = gen_shl64_ins_i64,
8837           .fniv = gen_shl_ins_vec,
8838           .opc = INDEX_op_shli_vec,
8839           .prefer_i64 = TCG_TARGET_REG_BITS == 64,
8840           .load_dest = true,
8841           .vece = MO_64 },
8842     };
8843     int size = 32 - clz32(immh) - 1;
8844     int immhb = immh << 3 | immb;
8845     int shift = immhb - (8 << size);
8846
8847     if (extract32(immh, 3, 1) && !is_q) {
8848         unallocated_encoding(s);
8849         return;
8850     }
8851
8852     if (size > 3 && !is_q) {
8853         unallocated_encoding(s);
8854         return;
8855     }
8856
8857     if (!fp_access_check(s)) {
8858         return;
8859     }
8860
8861     if (insert) {
8862         gen_gvec_op2i(s, is_q, rd, rn, shift, &shi_op[size]);
8863     } else {
8864         gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
8865     }
8866 }
8867
8868 /* USHLL/SHLL - Vector shift left with widening */
8869 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
8870                                  int immh, int immb, int opcode, int rn, int rd)
8871 {
8872     int size = 32 - clz32(immh) - 1;
8873     int immhb = immh << 3 | immb;
8874     int shift = immhb - (8 << size);
8875     int dsize = 64;
8876     int esize = 8 << size;
8877     int elements = dsize/esize;
8878     TCGv_i64 tcg_rn = new_tmp_a64(s);
8879     TCGv_i64 tcg_rd = new_tmp_a64(s);
8880     int i;
8881
8882     if (size >= 3) {
8883         unallocated_encoding(s);
8884         return;
8885     }
8886
8887     if (!fp_access_check(s)) {
8888         return;
8889     }
8890
8891     /* For the LL variants the store is larger than the load,
8892      * so if rd == rn we would overwrite parts of our input.
8893      * So load everything right now and use shifts in the main loop.
8894      */
8895     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
8896
8897     for (i = 0; i < elements; i++) {
8898         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
8899         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
8900         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
8901         write_vec_element(s, tcg_rd, rd, i, size + 1);
8902     }
8903 }
8904
8905 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
8906 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
8907                                  int immh, int immb, int opcode, int rn, int rd)
8908 {
8909     int immhb = immh << 3 | immb;
8910     int size = 32 - clz32(immh) - 1;
8911     int dsize = 64;
8912     int esize = 8 << size;
8913     int elements = dsize/esize;
8914     int shift = (2 * esize) - immhb;
8915     bool round = extract32(opcode, 0, 1);
8916     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
8917     TCGv_i64 tcg_round;
8918     int i;
8919
8920     if (extract32(immh, 3, 1)) {
8921         unallocated_encoding(s);
8922         return;
8923     }
8924
8925     if (!fp_access_check(s)) {
8926         return;
8927     }
8928
8929     tcg_rn = tcg_temp_new_i64();
8930     tcg_rd = tcg_temp_new_i64();
8931     tcg_final = tcg_temp_new_i64();
8932     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
8933
8934     if (round) {
8935         uint64_t round_const = 1ULL << (shift - 1);
8936         tcg_round = tcg_const_i64(round_const);
8937     } else {
8938         tcg_round = NULL;
8939     }
8940
8941     for (i = 0; i < elements; i++) {
8942         read_vec_element(s, tcg_rn, rn, i, size+1);
8943         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8944                                 false, true, size+1, shift);
8945
8946         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8947     }
8948
8949     if (!is_q) {
8950         write_vec_element(s, tcg_final, rd, 0, MO_64);
8951     } else {
8952         write_vec_element(s, tcg_final, rd, 1, MO_64);
8953     }
8954     if (round) {
8955         tcg_temp_free_i64(tcg_round);
8956     }
8957     tcg_temp_free_i64(tcg_rn);
8958     tcg_temp_free_i64(tcg_rd);
8959     tcg_temp_free_i64(tcg_final);
8960
8961     clear_vec_high(s, is_q, rd);
8962 }
8963
8964
8965 /* AdvSIMD shift by immediate
8966  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
8967  * +---+---+---+-------------+------+------+--------+---+------+------+
8968  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8969  * +---+---+---+-------------+------+------+--------+---+------+------+
8970  */
8971 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
8972 {
8973     int rd = extract32(insn, 0, 5);
8974     int rn = extract32(insn, 5, 5);
8975     int opcode = extract32(insn, 11, 5);
8976     int immb = extract32(insn, 16, 3);
8977     int immh = extract32(insn, 19, 4);
8978     bool is_u = extract32(insn, 29, 1);
8979     bool is_q = extract32(insn, 30, 1);
8980
8981     switch (opcode) {
8982     case 0x08: /* SRI */
8983         if (!is_u) {
8984             unallocated_encoding(s);
8985             return;
8986         }
8987         /* fall through */
8988     case 0x00: /* SSHR / USHR */
8989     case 0x02: /* SSRA / USRA (accumulate) */
8990     case 0x04: /* SRSHR / URSHR (rounding) */
8991     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8992         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
8993         break;
8994     case 0x0a: /* SHL / SLI */
8995         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
8996         break;
8997     case 0x10: /* SHRN */
8998     case 0x11: /* RSHRN / SQRSHRUN */
8999         if (is_u) {
9000             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
9001                                    opcode, rn, rd);
9002         } else {
9003             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
9004         }
9005         break;
9006     case 0x12: /* SQSHRN / UQSHRN */
9007     case 0x13: /* SQRSHRN / UQRSHRN */
9008         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
9009                                opcode, rn, rd);
9010         break;
9011     case 0x14: /* SSHLL / USHLL */
9012         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
9013         break;
9014     case 0x1c: /* SCVTF / UCVTF */
9015         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
9016                                      opcode, rn, rd);
9017         break;
9018     case 0xc: /* SQSHLU */
9019         if (!is_u) {
9020             unallocated_encoding(s);
9021             return;
9022         }
9023         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
9024         break;
9025     case 0xe: /* SQSHL, UQSHL */
9026         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
9027         break;
9028     case 0x1f: /* FCVTZS/ FCVTZU */
9029         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
9030         return;
9031     default:
9032         unallocated_encoding(s);
9033         return;
9034     }
9035 }
9036
9037 /* Generate code to do a "long" addition or subtraction, ie one done in
9038  * TCGv_i64 on vector lanes twice the width specified by size.
9039  */
9040 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
9041                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
9042 {
9043     static NeonGenTwo64OpFn * const fns[3][2] = {
9044         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
9045         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
9046         { tcg_gen_add_i64, tcg_gen_sub_i64 },
9047     };
9048     NeonGenTwo64OpFn *genfn;
9049     assert(size < 3);
9050
9051     genfn = fns[size][is_sub];
9052     genfn(tcg_res, tcg_op1, tcg_op2);
9053 }
9054
9055 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
9056                                 int opcode, int rd, int rn, int rm)
9057 {
9058     /* 3-reg-different widening insns: 64 x 64 -> 128 */
9059     TCGv_i64 tcg_res[2];
9060     int pass, accop;
9061
9062     tcg_res[0] = tcg_temp_new_i64();
9063     tcg_res[1] = tcg_temp_new_i64();
9064
9065     /* Does this op do an adding accumulate, a subtracting accumulate,
9066      * or no accumulate at all?
9067      */
9068     switch (opcode) {
9069     case 5:
9070     case 8:
9071     case 9:
9072         accop = 1;
9073         break;
9074     case 10:
9075     case 11:
9076         accop = -1;
9077         break;
9078     default:
9079         accop = 0;
9080         break;
9081     }
9082
9083     if (accop != 0) {
9084         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
9085         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
9086     }
9087
9088     /* size == 2 means two 32x32->64 operations; this is worth special
9089      * casing because we can generally handle it inline.
9090      */
9091     if (size == 2) {
9092         for (pass = 0; pass < 2; pass++) {
9093             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9094             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9095             TCGv_i64 tcg_passres;
9096             TCGMemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
9097
9098             int elt = pass + is_q * 2;
9099
9100             read_vec_element(s, tcg_op1, rn, elt, memop);
9101             read_vec_element(s, tcg_op2, rm, elt, memop);
9102
9103             if (accop == 0) {
9104                 tcg_passres = tcg_res[pass];
9105             } else {
9106                 tcg_passres = tcg_temp_new_i64();
9107             }
9108
9109             switch (opcode) {
9110             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
9111                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
9112                 break;
9113             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
9114                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
9115                 break;
9116             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
9117             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
9118             {
9119                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
9120                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
9121
9122                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
9123                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
9124                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
9125                                     tcg_passres,
9126                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
9127                 tcg_temp_free_i64(tcg_tmp1);
9128                 tcg_temp_free_i64(tcg_tmp2);
9129                 break;
9130             }
9131             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
9132             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
9133             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
9134                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
9135                 break;
9136             case 9: /* SQDMLAL, SQDMLAL2 */
9137             case 11: /* SQDMLSL, SQDMLSL2 */
9138             case 13: /* SQDMULL, SQDMULL2 */
9139                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
9140                 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
9141                                                   tcg_passres, tcg_passres);
9142                 break;
9143             default:
9144                 g_assert_not_reached();
9145             }
9146
9147             if (opcode == 9 || opcode == 11) {
9148                 /* saturating accumulate ops */
9149                 if (accop < 0) {
9150                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
9151                 }
9152                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
9153                                                   tcg_res[pass], tcg_passres);
9154             } else if (accop > 0) {
9155                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
9156             } else if (accop < 0) {
9157                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
9158             }
9159
9160             if (accop != 0) {
9161                 tcg_temp_free_i64(tcg_passres);
9162             }
9163
9164             tcg_temp_free_i64(tcg_op1);
9165             tcg_temp_free_i64(tcg_op2);
9166         }
9167     } else {
9168         /* size 0 or 1, generally helper functions */
9169         for (pass = 0; pass < 2; pass++) {
9170             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9171             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9172             TCGv_i64 tcg_passres;
9173             int elt = pass + is_q * 2;
9174
9175             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
9176             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
9177
9178             if (accop == 0) {
9179                 tcg_passres = tcg_res[pass];
9180             } else {
9181                 tcg_passres = tcg_temp_new_i64();
9182             }
9183
9184             switch (opcode) {
9185             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
9186             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
9187             {
9188                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
9189                 static NeonGenWidenFn * const widenfns[2][2] = {
9190                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
9191                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
9192                 };
9193                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
9194
9195                 widenfn(tcg_op2_64, tcg_op2);
9196                 widenfn(tcg_passres, tcg_op1);
9197                 gen_neon_addl(size, (opcode == 2), tcg_passres,
9198                               tcg_passres, tcg_op2_64);
9199                 tcg_temp_free_i64(tcg_op2_64);
9200                 break;
9201             }
9202             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
9203             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
9204                 if (size == 0) {
9205                     if (is_u) {
9206                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
9207                     } else {
9208                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
9209                     }
9210                 } else {
9211                     if (is_u) {
9212                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
9213                     } else {
9214                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
9215                     }
9216                 }
9217                 break;
9218             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
9219             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
9220             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
9221                 if (size == 0) {
9222                     if (is_u) {
9223                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
9224                     } else {
9225                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
9226                     }
9227                 } else {
9228                     if (is_u) {
9229                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
9230                     } else {
9231                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
9232                     }
9233                 }
9234                 break;
9235             case 9: /* SQDMLAL, SQDMLAL2 */
9236             case 11: /* SQDMLSL, SQDMLSL2 */
9237             case 13: /* SQDMULL, SQDMULL2 */
9238                 assert(size == 1);
9239                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
9240                 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
9241                                                   tcg_passres, tcg_passres);
9242                 break;
9243             case 14: /* PMULL */
9244                 assert(size == 0);
9245                 gen_helper_neon_mull_p8(tcg_passres, tcg_op1, tcg_op2);
9246                 break;
9247             default:
9248                 g_assert_not_reached();
9249             }
9250             tcg_temp_free_i32(tcg_op1);
9251             tcg_temp_free_i32(tcg_op2);
9252
9253             if (accop != 0) {
9254                 if (opcode == 9 || opcode == 11) {
9255                     /* saturating accumulate ops */
9256                     if (accop < 0) {
9257                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
9258                     }
9259                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
9260                                                       tcg_res[pass],
9261                                                       tcg_passres);
9262                 } else {
9263                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
9264                                   tcg_res[pass], tcg_passres);
9265                 }
9266                 tcg_temp_free_i64(tcg_passres);
9267             }
9268         }
9269     }
9270
9271     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
9272     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
9273     tcg_temp_free_i64(tcg_res[0]);
9274     tcg_temp_free_i64(tcg_res[1]);
9275 }
9276
9277 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
9278                             int opcode, int rd, int rn, int rm)
9279 {
9280     TCGv_i64 tcg_res[2];
9281     int part = is_q ? 2 : 0;
9282     int pass;
9283
9284     for (pass = 0; pass < 2; pass++) {
9285         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9286         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9287         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
9288         static NeonGenWidenFn * const widenfns[3][2] = {
9289             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
9290             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
9291             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
9292         };
9293         NeonGenWidenFn *widenfn = widenfns[size][is_u];
9294
9295         read_vec_element(s, tcg_op1, rn, pass, MO_64);
9296         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
9297         widenfn(tcg_op2_wide, tcg_op2);
9298         tcg_temp_free_i32(tcg_op2);
9299         tcg_res[pass] = tcg_temp_new_i64();
9300         gen_neon_addl(size, (opcode == 3),
9301                       tcg_res[pass], tcg_op1, tcg_op2_wide);
9302         tcg_temp_free_i64(tcg_op1);
9303         tcg_temp_free_i64(tcg_op2_wide);
9304     }
9305
9306     for (pass = 0; pass < 2; pass++) {
9307         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9308         tcg_temp_free_i64(tcg_res[pass]);
9309     }
9310 }
9311
9312 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
9313 {
9314     tcg_gen_addi_i64(in, in, 1U << 31);
9315     tcg_gen_extrh_i64_i32(res, in);
9316 }
9317
9318 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
9319                                  int opcode, int rd, int rn, int rm)
9320 {
9321     TCGv_i32 tcg_res[2];
9322     int part = is_q ? 2 : 0;
9323     int pass;
9324
9325     for (pass = 0; pass < 2; pass++) {
9326         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9327         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9328         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
9329         static NeonGenNarrowFn * const narrowfns[3][2] = {
9330             { gen_helper_neon_narrow_high_u8,
9331               gen_helper_neon_narrow_round_high_u8 },
9332             { gen_helper_neon_narrow_high_u16,
9333               gen_helper_neon_narrow_round_high_u16 },
9334             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
9335         };
9336         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
9337
9338         read_vec_element(s, tcg_op1, rn, pass, MO_64);
9339         read_vec_element(s, tcg_op2, rm, pass, MO_64);
9340
9341         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
9342
9343         tcg_temp_free_i64(tcg_op1);
9344         tcg_temp_free_i64(tcg_op2);
9345
9346         tcg_res[pass] = tcg_temp_new_i32();
9347         gennarrow(tcg_res[pass], tcg_wideres);
9348         tcg_temp_free_i64(tcg_wideres);
9349     }
9350
9351     for (pass = 0; pass < 2; pass++) {
9352         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
9353         tcg_temp_free_i32(tcg_res[pass]);
9354     }
9355     clear_vec_high(s, is_q, rd);
9356 }
9357
9358 static void handle_pmull_64(DisasContext *s, int is_q, int rd, int rn, int rm)
9359 {
9360     /* PMULL of 64 x 64 -> 128 is an odd special case because it
9361      * is the only three-reg-diff instruction which produces a
9362      * 128-bit wide result from a single operation. However since
9363      * it's possible to calculate the two halves more or less
9364      * separately we just use two helper calls.
9365      */
9366     TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9367     TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9368     TCGv_i64 tcg_res = tcg_temp_new_i64();
9369
9370     read_vec_element(s, tcg_op1, rn, is_q, MO_64);
9371     read_vec_element(s, tcg_op2, rm, is_q, MO_64);
9372     gen_helper_neon_pmull_64_lo(tcg_res, tcg_op1, tcg_op2);
9373     write_vec_element(s, tcg_res, rd, 0, MO_64);
9374     gen_helper_neon_pmull_64_hi(tcg_res, tcg_op1, tcg_op2);
9375     write_vec_element(s, tcg_res, rd, 1, MO_64);
9376
9377     tcg_temp_free_i64(tcg_op1);
9378     tcg_temp_free_i64(tcg_op2);
9379     tcg_temp_free_i64(tcg_res);
9380 }
9381
9382 /* AdvSIMD three different
9383  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
9384  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
9385  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
9386  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
9387  */
9388 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
9389 {
9390     /* Instructions in this group fall into three basic classes
9391      * (in each case with the operation working on each element in
9392      * the input vectors):
9393      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
9394      *     128 bit input)
9395      * (2) wide 64 x 128 -> 128
9396      * (3) narrowing 128 x 128 -> 64
9397      * Here we do initial decode, catch unallocated cases and
9398      * dispatch to separate functions for each class.
9399      */
9400     int is_q = extract32(insn, 30, 1);
9401     int is_u = extract32(insn, 29, 1);
9402     int size = extract32(insn, 22, 2);
9403     int opcode = extract32(insn, 12, 4);
9404     int rm = extract32(insn, 16, 5);
9405     int rn = extract32(insn, 5, 5);
9406     int rd = extract32(insn, 0, 5);
9407
9408     switch (opcode) {
9409     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
9410     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
9411         /* 64 x 128 -> 128 */
9412         if (size == 3) {
9413             unallocated_encoding(s);
9414             return;
9415         }
9416         if (!fp_access_check(s)) {
9417             return;
9418         }
9419         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
9420         break;
9421     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
9422     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
9423         /* 128 x 128 -> 64 */
9424         if (size == 3) {
9425             unallocated_encoding(s);
9426             return;
9427         }
9428         if (!fp_access_check(s)) {
9429             return;
9430         }
9431         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
9432         break;
9433     case 14: /* PMULL, PMULL2 */
9434         if (is_u || size == 1 || size == 2) {
9435             unallocated_encoding(s);
9436             return;
9437         }
9438         if (size == 3) {
9439             if (!arm_dc_feature(s, ARM_FEATURE_V8_PMULL)) {
9440                 unallocated_encoding(s);
9441                 return;
9442             }
9443             if (!fp_access_check(s)) {
9444                 return;
9445             }
9446             handle_pmull_64(s, is_q, rd, rn, rm);
9447             return;
9448         }
9449         goto is_widening;
9450     case 9: /* SQDMLAL, SQDMLAL2 */
9451     case 11: /* SQDMLSL, SQDMLSL2 */
9452     case 13: /* SQDMULL, SQDMULL2 */
9453         if (is_u || size == 0) {
9454             unallocated_encoding(s);
9455             return;
9456         }
9457         /* fall through */
9458     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
9459     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
9460     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
9461     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
9462     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
9463     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
9464     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
9465         /* 64 x 64 -> 128 */
9466         if (size == 3) {
9467             unallocated_encoding(s);
9468             return;
9469         }
9470     is_widening:
9471         if (!fp_access_check(s)) {
9472             return;
9473         }
9474
9475         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
9476         break;
9477     default:
9478         /* opcode 15 not allocated */
9479         unallocated_encoding(s);
9480         break;
9481     }
9482 }
9483
9484 static void gen_bsl_i64(TCGv_i64 rd, TCGv_i64 rn, TCGv_i64 rm)
9485 {
9486     tcg_gen_xor_i64(rn, rn, rm);
9487     tcg_gen_and_i64(rn, rn, rd);
9488     tcg_gen_xor_i64(rd, rm, rn);
9489 }
9490
9491 static void gen_bit_i64(TCGv_i64 rd, TCGv_i64 rn, TCGv_i64 rm)
9492 {
9493     tcg_gen_xor_i64(rn, rn, rd);
9494     tcg_gen_and_i64(rn, rn, rm);
9495     tcg_gen_xor_i64(rd, rd, rn);
9496 }
9497
9498 static void gen_bif_i64(TCGv_i64 rd, TCGv_i64 rn, TCGv_i64 rm)
9499 {
9500     tcg_gen_xor_i64(rn, rn, rd);
9501     tcg_gen_andc_i64(rn, rn, rm);
9502     tcg_gen_xor_i64(rd, rd, rn);
9503 }
9504
9505 static void gen_bsl_vec(unsigned vece, TCGv_vec rd, TCGv_vec rn, TCGv_vec rm)
9506 {
9507     tcg_gen_xor_vec(vece, rn, rn, rm);
9508     tcg_gen_and_vec(vece, rn, rn, rd);
9509     tcg_gen_xor_vec(vece, rd, rm, rn);
9510 }
9511
9512 static void gen_bit_vec(unsigned vece, TCGv_vec rd, TCGv_vec rn, TCGv_vec rm)
9513 {
9514     tcg_gen_xor_vec(vece, rn, rn, rd);
9515     tcg_gen_and_vec(vece, rn, rn, rm);
9516     tcg_gen_xor_vec(vece, rd, rd, rn);
9517 }
9518
9519 static void gen_bif_vec(unsigned vece, TCGv_vec rd, TCGv_vec rn, TCGv_vec rm)
9520 {
9521     tcg_gen_xor_vec(vece, rn, rn, rd);
9522     tcg_gen_andc_vec(vece, rn, rn, rm);
9523     tcg_gen_xor_vec(vece, rd, rd, rn);
9524 }
9525
9526 /* Logic op (opcode == 3) subgroup of C3.6.16. */
9527 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
9528 {
9529     static const GVecGen3 bsl_op = {
9530         .fni8 = gen_bsl_i64,
9531         .fniv = gen_bsl_vec,
9532         .prefer_i64 = TCG_TARGET_REG_BITS == 64,
9533         .load_dest = true
9534     };
9535     static const GVecGen3 bit_op = {
9536         .fni8 = gen_bit_i64,
9537         .fniv = gen_bit_vec,
9538         .prefer_i64 = TCG_TARGET_REG_BITS == 64,
9539         .load_dest = true
9540     };
9541     static const GVecGen3 bif_op = {
9542         .fni8 = gen_bif_i64,
9543         .fniv = gen_bif_vec,
9544         .prefer_i64 = TCG_TARGET_REG_BITS == 64,
9545         .load_dest = true
9546     };
9547
9548     int rd = extract32(insn, 0, 5);
9549     int rn = extract32(insn, 5, 5);
9550     int rm = extract32(insn, 16, 5);
9551     int size = extract32(insn, 22, 2);
9552     bool is_u = extract32(insn, 29, 1);
9553     bool is_q = extract32(insn, 30, 1);
9554
9555     if (!fp_access_check(s)) {
9556         return;
9557     }
9558
9559     switch (size + 4 * is_u) {
9560     case 0: /* AND */
9561         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
9562         return;
9563     case 1: /* BIC */
9564         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
9565         return;
9566     case 2: /* ORR */
9567         if (rn == rm) { /* MOV */
9568             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_mov, 0);
9569         } else {
9570             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
9571         }
9572         return;
9573     case 3: /* ORN */
9574         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
9575         return;
9576     case 4: /* EOR */
9577         gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
9578         return;
9579
9580     case 5: /* BSL bitwise select */
9581         gen_gvec_op3(s, is_q, rd, rn, rm, &bsl_op);
9582         return;
9583     case 6: /* BIT, bitwise insert if true */
9584         gen_gvec_op3(s, is_q, rd, rn, rm, &bit_op);
9585         return;
9586     case 7: /* BIF, bitwise insert if false */
9587         gen_gvec_op3(s, is_q, rd, rn, rm, &bif_op);
9588         return;
9589
9590     default:
9591         g_assert_not_reached();
9592     }
9593 }
9594
9595 /* Helper functions for 32 bit comparisons */
9596 static void gen_max_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9597 {
9598     tcg_gen_movcond_i32(TCG_COND_GE, res, op1, op2, op1, op2);
9599 }
9600
9601 static void gen_max_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9602 {
9603     tcg_gen_movcond_i32(TCG_COND_GEU, res, op1, op2, op1, op2);
9604 }
9605
9606 static void gen_min_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9607 {
9608     tcg_gen_movcond_i32(TCG_COND_LE, res, op1, op2, op1, op2);
9609 }
9610
9611 static void gen_min_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9612 {
9613     tcg_gen_movcond_i32(TCG_COND_LEU, res, op1, op2, op1, op2);
9614 }
9615
9616 /* Pairwise op subgroup of C3.6.16.
9617  *
9618  * This is called directly or via the handle_3same_float for float pairwise
9619  * operations where the opcode and size are calculated differently.
9620  */
9621 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
9622                                    int size, int rn, int rm, int rd)
9623 {
9624     TCGv_ptr fpst;
9625     int pass;
9626
9627     /* Floating point operations need fpst */
9628     if (opcode >= 0x58) {
9629         fpst = get_fpstatus_ptr(false);
9630     } else {
9631         fpst = NULL;
9632     }
9633
9634     if (!fp_access_check(s)) {
9635         return;
9636     }
9637
9638     /* These operations work on the concatenated rm:rn, with each pair of
9639      * adjacent elements being operated on to produce an element in the result.
9640      */
9641     if (size == 3) {
9642         TCGv_i64 tcg_res[2];
9643
9644         for (pass = 0; pass < 2; pass++) {
9645             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9646             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9647             int passreg = (pass == 0) ? rn : rm;
9648
9649             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
9650             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
9651             tcg_res[pass] = tcg_temp_new_i64();
9652
9653             switch (opcode) {
9654             case 0x17: /* ADDP */
9655                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
9656                 break;
9657             case 0x58: /* FMAXNMP */
9658                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9659                 break;
9660             case 0x5a: /* FADDP */
9661                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9662                 break;
9663             case 0x5e: /* FMAXP */
9664                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9665                 break;
9666             case 0x78: /* FMINNMP */
9667                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9668                 break;
9669             case 0x7e: /* FMINP */
9670                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9671                 break;
9672             default:
9673                 g_assert_not_reached();
9674             }
9675
9676             tcg_temp_free_i64(tcg_op1);
9677             tcg_temp_free_i64(tcg_op2);
9678         }
9679
9680         for (pass = 0; pass < 2; pass++) {
9681             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9682             tcg_temp_free_i64(tcg_res[pass]);
9683         }
9684     } else {
9685         int maxpass = is_q ? 4 : 2;
9686         TCGv_i32 tcg_res[4];
9687
9688         for (pass = 0; pass < maxpass; pass++) {
9689             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9690             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9691             NeonGenTwoOpFn *genfn = NULL;
9692             int passreg = pass < (maxpass / 2) ? rn : rm;
9693             int passelt = (is_q && (pass & 1)) ? 2 : 0;
9694
9695             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
9696             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
9697             tcg_res[pass] = tcg_temp_new_i32();
9698
9699             switch (opcode) {
9700             case 0x17: /* ADDP */
9701             {
9702                 static NeonGenTwoOpFn * const fns[3] = {
9703                     gen_helper_neon_padd_u8,
9704                     gen_helper_neon_padd_u16,
9705                     tcg_gen_add_i32,
9706                 };
9707                 genfn = fns[size];
9708                 break;
9709             }
9710             case 0x14: /* SMAXP, UMAXP */
9711             {
9712                 static NeonGenTwoOpFn * const fns[3][2] = {
9713                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
9714                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
9715                     { gen_max_s32, gen_max_u32 },
9716                 };
9717                 genfn = fns[size][u];
9718                 break;
9719             }
9720             case 0x15: /* SMINP, UMINP */
9721             {
9722                 static NeonGenTwoOpFn * const fns[3][2] = {
9723                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
9724                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
9725                     { gen_min_s32, gen_min_u32 },
9726                 };
9727                 genfn = fns[size][u];
9728                 break;
9729             }
9730             /* The FP operations are all on single floats (32 bit) */
9731             case 0x58: /* FMAXNMP */
9732                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9733                 break;
9734             case 0x5a: /* FADDP */
9735                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9736                 break;
9737             case 0x5e: /* FMAXP */
9738                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9739                 break;
9740             case 0x78: /* FMINNMP */
9741                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9742                 break;
9743             case 0x7e: /* FMINP */
9744                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9745                 break;
9746             default:
9747                 g_assert_not_reached();
9748             }
9749
9750             /* FP ops called directly, otherwise call now */
9751             if (genfn) {
9752                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
9753             }
9754
9755             tcg_temp_free_i32(tcg_op1);
9756             tcg_temp_free_i32(tcg_op2);
9757         }
9758
9759         for (pass = 0; pass < maxpass; pass++) {
9760             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
9761             tcg_temp_free_i32(tcg_res[pass]);
9762         }
9763         clear_vec_high(s, is_q, rd);
9764     }
9765
9766     if (fpst) {
9767         tcg_temp_free_ptr(fpst);
9768     }
9769 }
9770
9771 /* Floating point op subgroup of C3.6.16. */
9772 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
9773 {
9774     /* For floating point ops, the U, size[1] and opcode bits
9775      * together indicate the operation. size[0] indicates single
9776      * or double.
9777      */
9778     int fpopcode = extract32(insn, 11, 5)
9779         | (extract32(insn, 23, 1) << 5)
9780         | (extract32(insn, 29, 1) << 6);
9781     int is_q = extract32(insn, 30, 1);
9782     int size = extract32(insn, 22, 1);
9783     int rm = extract32(insn, 16, 5);
9784     int rn = extract32(insn, 5, 5);
9785     int rd = extract32(insn, 0, 5);
9786
9787     int datasize = is_q ? 128 : 64;
9788     int esize = 32 << size;
9789     int elements = datasize / esize;
9790
9791     if (size == 1 && !is_q) {
9792         unallocated_encoding(s);
9793         return;
9794     }
9795
9796     switch (fpopcode) {
9797     case 0x58: /* FMAXNMP */
9798     case 0x5a: /* FADDP */
9799     case 0x5e: /* FMAXP */
9800     case 0x78: /* FMINNMP */
9801     case 0x7e: /* FMINP */
9802         if (size && !is_q) {
9803             unallocated_encoding(s);
9804             return;
9805         }
9806         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
9807                                rn, rm, rd);
9808         return;
9809     case 0x1b: /* FMULX */
9810     case 0x1f: /* FRECPS */
9811     case 0x3f: /* FRSQRTS */
9812     case 0x5d: /* FACGE */
9813     case 0x7d: /* FACGT */
9814     case 0x19: /* FMLA */
9815     case 0x39: /* FMLS */
9816     case 0x18: /* FMAXNM */
9817     case 0x1a: /* FADD */
9818     case 0x1c: /* FCMEQ */
9819     case 0x1e: /* FMAX */
9820     case 0x38: /* FMINNM */
9821     case 0x3a: /* FSUB */
9822     case 0x3e: /* FMIN */
9823     case 0x5b: /* FMUL */
9824     case 0x5c: /* FCMGE */
9825     case 0x5f: /* FDIV */
9826     case 0x7a: /* FABD */
9827     case 0x7c: /* FCMGT */
9828         if (!fp_access_check(s)) {
9829             return;
9830         }
9831
9832         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
9833         return;
9834     default:
9835         unallocated_encoding(s);
9836         return;
9837     }
9838 }
9839
9840 static void gen_mla8_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
9841 {
9842     gen_helper_neon_mul_u8(a, a, b);
9843     gen_helper_neon_add_u8(d, d, a);
9844 }
9845
9846 static void gen_mla16_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
9847 {
9848     gen_helper_neon_mul_u16(a, a, b);
9849     gen_helper_neon_add_u16(d, d, a);
9850 }
9851
9852 static void gen_mla32_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
9853 {
9854     tcg_gen_mul_i32(a, a, b);
9855     tcg_gen_add_i32(d, d, a);
9856 }
9857
9858 static void gen_mla64_i64(TCGv_i64 d, TCGv_i64 a, TCGv_i64 b)
9859 {
9860     tcg_gen_mul_i64(a, a, b);
9861     tcg_gen_add_i64(d, d, a);
9862 }
9863
9864 static void gen_mla_vec(unsigned vece, TCGv_vec d, TCGv_vec a, TCGv_vec b)
9865 {
9866     tcg_gen_mul_vec(vece, a, a, b);
9867     tcg_gen_add_vec(vece, d, d, a);
9868 }
9869
9870 static void gen_mls8_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
9871 {
9872     gen_helper_neon_mul_u8(a, a, b);
9873     gen_helper_neon_sub_u8(d, d, a);
9874 }
9875
9876 static void gen_mls16_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
9877 {
9878     gen_helper_neon_mul_u16(a, a, b);
9879     gen_helper_neon_sub_u16(d, d, a);
9880 }
9881
9882 static void gen_mls32_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
9883 {
9884     tcg_gen_mul_i32(a, a, b);
9885     tcg_gen_sub_i32(d, d, a);
9886 }
9887
9888 static void gen_mls64_i64(TCGv_i64 d, TCGv_i64 a, TCGv_i64 b)
9889 {
9890     tcg_gen_mul_i64(a, a, b);
9891     tcg_gen_sub_i64(d, d, a);
9892 }
9893
9894 static void gen_mls_vec(unsigned vece, TCGv_vec d, TCGv_vec a, TCGv_vec b)
9895 {
9896     tcg_gen_mul_vec(vece, a, a, b);
9897     tcg_gen_sub_vec(vece, d, d, a);
9898 }
9899
9900 /* Integer op subgroup of C3.6.16. */
9901 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
9902 {
9903     static const GVecGen3 cmtst_op[4] = {
9904         { .fni4 = gen_helper_neon_tst_u8,
9905           .fniv = gen_cmtst_vec,
9906           .vece = MO_8 },
9907         { .fni4 = gen_helper_neon_tst_u16,
9908           .fniv = gen_cmtst_vec,
9909           .vece = MO_16 },
9910         { .fni4 = gen_cmtst_i32,
9911           .fniv = gen_cmtst_vec,
9912           .vece = MO_32 },
9913         { .fni8 = gen_cmtst_i64,
9914           .fniv = gen_cmtst_vec,
9915           .prefer_i64 = TCG_TARGET_REG_BITS == 64,
9916           .vece = MO_64 },
9917     };
9918     static const GVecGen3 mla_op[4] = {
9919         { .fni4 = gen_mla8_i32,
9920           .fniv = gen_mla_vec,
9921           .opc = INDEX_op_mul_vec,
9922           .load_dest = true,
9923           .vece = MO_8 },
9924         { .fni4 = gen_mla16_i32,
9925           .fniv = gen_mla_vec,
9926           .opc = INDEX_op_mul_vec,
9927           .load_dest = true,
9928           .vece = MO_16 },
9929         { .fni4 = gen_mla32_i32,
9930           .fniv = gen_mla_vec,
9931           .opc = INDEX_op_mul_vec,
9932           .load_dest = true,
9933           .vece = MO_32 },
9934         { .fni8 = gen_mla64_i64,
9935           .fniv = gen_mla_vec,
9936           .opc = INDEX_op_mul_vec,
9937           .prefer_i64 = TCG_TARGET_REG_BITS == 64,
9938           .load_dest = true,
9939           .vece = MO_64 },
9940     };
9941     static const GVecGen3 mls_op[4] = {
9942         { .fni4 = gen_mls8_i32,
9943           .fniv = gen_mls_vec,
9944           .opc = INDEX_op_mul_vec,
9945           .load_dest = true,
9946           .vece = MO_8 },
9947         { .fni4 = gen_mls16_i32,
9948           .fniv = gen_mls_vec,
9949           .opc = INDEX_op_mul_vec,
9950           .load_dest = true,
9951           .vece = MO_16 },
9952         { .fni4 = gen_mls32_i32,
9953           .fniv = gen_mls_vec,
9954           .opc = INDEX_op_mul_vec,
9955           .load_dest = true,
9956           .vece = MO_32 },
9957         { .fni8 = gen_mls64_i64,
9958           .fniv = gen_mls_vec,
9959           .opc = INDEX_op_mul_vec,
9960           .prefer_i64 = TCG_TARGET_REG_BITS == 64,
9961           .load_dest = true,
9962           .vece = MO_64 },
9963     };
9964
9965     int is_q = extract32(insn, 30, 1);
9966     int u = extract32(insn, 29, 1);
9967     int size = extract32(insn, 22, 2);
9968     int opcode = extract32(insn, 11, 5);
9969     int rm = extract32(insn, 16, 5);
9970     int rn = extract32(insn, 5, 5);
9971     int rd = extract32(insn, 0, 5);
9972     int pass;
9973     TCGCond cond;
9974
9975     switch (opcode) {
9976     case 0x13: /* MUL, PMUL */
9977         if (u && size != 0) {
9978             unallocated_encoding(s);
9979             return;
9980         }
9981         /* fall through */
9982     case 0x0: /* SHADD, UHADD */
9983     case 0x2: /* SRHADD, URHADD */
9984     case 0x4: /* SHSUB, UHSUB */
9985     case 0xc: /* SMAX, UMAX */
9986     case 0xd: /* SMIN, UMIN */
9987     case 0xe: /* SABD, UABD */
9988     case 0xf: /* SABA, UABA */
9989     case 0x12: /* MLA, MLS */
9990         if (size == 3) {
9991             unallocated_encoding(s);
9992             return;
9993         }
9994         break;
9995     case 0x16: /* SQDMULH, SQRDMULH */
9996         if (size == 0 || size == 3) {
9997             unallocated_encoding(s);
9998             return;
9999         }
10000         break;
10001     default:
10002         if (size == 3 && !is_q) {
10003             unallocated_encoding(s);
10004             return;
10005         }
10006         break;
10007     }
10008
10009     if (!fp_access_check(s)) {
10010         return;
10011     }
10012
10013     switch (opcode) {
10014     case 0x10: /* ADD, SUB */
10015         if (u) {
10016             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
10017         } else {
10018             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
10019         }
10020         return;
10021     case 0x13: /* MUL, PMUL */
10022         if (!u) { /* MUL */
10023             gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
10024             return;
10025         }
10026         break;
10027     case 0x12: /* MLA, MLS */
10028         if (u) {
10029             gen_gvec_op3(s, is_q, rd, rn, rm, &mls_op[size]);
10030         } else {
10031             gen_gvec_op3(s, is_q, rd, rn, rm, &mla_op[size]);
10032         }
10033         return;
10034     case 0x11:
10035         if (!u) { /* CMTST */
10036             gen_gvec_op3(s, is_q, rd, rn, rm, &cmtst_op[size]);
10037             return;
10038         }
10039         /* else CMEQ */
10040         cond = TCG_COND_EQ;
10041         goto do_gvec_cmp;
10042     case 0x06: /* CMGT, CMHI */
10043         cond = u ? TCG_COND_GTU : TCG_COND_GT;
10044         goto do_gvec_cmp;
10045     case 0x07: /* CMGE, CMHS */
10046         cond = u ? TCG_COND_GEU : TCG_COND_GE;
10047     do_gvec_cmp:
10048         tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
10049                          vec_full_reg_offset(s, rn),
10050                          vec_full_reg_offset(s, rm),
10051                          is_q ? 16 : 8, vec_full_reg_size(s));
10052         return;
10053     }
10054
10055     if (size == 3) {
10056         assert(is_q);
10057         for (pass = 0; pass < 2; pass++) {
10058             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10059             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10060             TCGv_i64 tcg_res = tcg_temp_new_i64();
10061
10062             read_vec_element(s, tcg_op1, rn, pass, MO_64);
10063             read_vec_element(s, tcg_op2, rm, pass, MO_64);
10064
10065             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
10066
10067             write_vec_element(s, tcg_res, rd, pass, MO_64);
10068
10069             tcg_temp_free_i64(tcg_res);
10070             tcg_temp_free_i64(tcg_op1);
10071             tcg_temp_free_i64(tcg_op2);
10072         }
10073     } else {
10074         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
10075             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10076             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10077             TCGv_i32 tcg_res = tcg_temp_new_i32();
10078             NeonGenTwoOpFn *genfn = NULL;
10079             NeonGenTwoOpEnvFn *genenvfn = NULL;
10080
10081             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
10082             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
10083
10084             switch (opcode) {
10085             case 0x0: /* SHADD, UHADD */
10086             {
10087                 static NeonGenTwoOpFn * const fns[3][2] = {
10088                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
10089                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
10090                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
10091                 };
10092                 genfn = fns[size][u];
10093                 break;
10094             }
10095             case 0x1: /* SQADD, UQADD */
10096             {
10097                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
10098                     { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
10099                     { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
10100                     { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
10101                 };
10102                 genenvfn = fns[size][u];
10103                 break;
10104             }
10105             case 0x2: /* SRHADD, URHADD */
10106             {
10107                 static NeonGenTwoOpFn * const fns[3][2] = {
10108                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
10109                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
10110                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
10111                 };
10112                 genfn = fns[size][u];
10113                 break;
10114             }
10115             case 0x4: /* SHSUB, UHSUB */
10116             {
10117                 static NeonGenTwoOpFn * const fns[3][2] = {
10118                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
10119                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
10120                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
10121                 };
10122                 genfn = fns[size][u];
10123                 break;
10124             }
10125             case 0x5: /* SQSUB, UQSUB */
10126             {
10127                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
10128                     { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
10129                     { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
10130                     { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
10131                 };
10132                 genenvfn = fns[size][u];
10133                 break;
10134             }
10135             case 0x8: /* SSHL, USHL */
10136             {
10137                 static NeonGenTwoOpFn * const fns[3][2] = {
10138                     { gen_helper_neon_shl_s8, gen_helper_neon_shl_u8 },
10139                     { gen_helper_neon_shl_s16, gen_helper_neon_shl_u16 },
10140                     { gen_helper_neon_shl_s32, gen_helper_neon_shl_u32 },
10141                 };
10142                 genfn = fns[size][u];
10143                 break;
10144             }
10145             case 0x9: /* SQSHL, UQSHL */
10146             {
10147                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
10148                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
10149                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
10150                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
10151                 };
10152                 genenvfn = fns[size][u];
10153                 break;
10154             }
10155             case 0xa: /* SRSHL, URSHL */
10156             {
10157                 static NeonGenTwoOpFn * const fns[3][2] = {
10158                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
10159                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
10160                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
10161                 };
10162                 genfn = fns[size][u];
10163                 break;
10164             }
10165             case 0xb: /* SQRSHL, UQRSHL */
10166             {
10167                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
10168                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
10169                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
10170                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
10171                 };
10172                 genenvfn = fns[size][u];
10173                 break;
10174             }
10175             case 0xc: /* SMAX, UMAX */
10176             {
10177                 static NeonGenTwoOpFn * const fns[3][2] = {
10178                     { gen_helper_neon_max_s8, gen_helper_neon_max_u8 },
10179                     { gen_helper_neon_max_s16, gen_helper_neon_max_u16 },
10180                     { gen_max_s32, gen_max_u32 },
10181                 };
10182                 genfn = fns[size][u];
10183                 break;
10184             }
10185
10186             case 0xd: /* SMIN, UMIN */
10187             {
10188                 static NeonGenTwoOpFn * const fns[3][2] = {
10189                     { gen_helper_neon_min_s8, gen_helper_neon_min_u8 },
10190                     { gen_helper_neon_min_s16, gen_helper_neon_min_u16 },
10191                     { gen_min_s32, gen_min_u32 },
10192                 };
10193                 genfn = fns[size][u];
10194                 break;
10195             }
10196             case 0xe: /* SABD, UABD */
10197             case 0xf: /* SABA, UABA */
10198             {
10199                 static NeonGenTwoOpFn * const fns[3][2] = {
10200                     { gen_helper_neon_abd_s8, gen_helper_neon_abd_u8 },
10201                     { gen_helper_neon_abd_s16, gen_helper_neon_abd_u16 },
10202                     { gen_helper_neon_abd_s32, gen_helper_neon_abd_u32 },
10203                 };
10204                 genfn = fns[size][u];
10205                 break;
10206             }
10207             case 0x13: /* MUL, PMUL */
10208                 assert(u); /* PMUL */
10209                 assert(size == 0);
10210                 genfn = gen_helper_neon_mul_p8;
10211                 break;
10212             case 0x16: /* SQDMULH, SQRDMULH */
10213             {
10214                 static NeonGenTwoOpEnvFn * const fns[2][2] = {
10215                     { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
10216                     { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
10217                 };
10218                 assert(size == 1 || size == 2);
10219                 genenvfn = fns[size - 1][u];
10220                 break;
10221             }
10222             default:
10223                 g_assert_not_reached();
10224             }
10225
10226             if (genenvfn) {
10227                 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
10228             } else {
10229                 genfn(tcg_res, tcg_op1, tcg_op2);
10230             }
10231
10232             if (opcode == 0xf) {
10233                 /* SABA, UABA: accumulating ops */
10234                 static NeonGenTwoOpFn * const fns[3] = {
10235                     gen_helper_neon_add_u8,
10236                     gen_helper_neon_add_u16,
10237                     tcg_gen_add_i32,
10238                 };
10239
10240                 read_vec_element_i32(s, tcg_op1, rd, pass, MO_32);
10241                 fns[size](tcg_res, tcg_op1, tcg_res);
10242             }
10243
10244             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10245
10246             tcg_temp_free_i32(tcg_res);
10247             tcg_temp_free_i32(tcg_op1);
10248             tcg_temp_free_i32(tcg_op2);
10249         }
10250     }
10251     clear_vec_high(s, is_q, rd);
10252 }
10253
10254 /* AdvSIMD three same
10255  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
10256  * +---+---+---+-----------+------+---+------+--------+---+------+------+
10257  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
10258  * +---+---+---+-----------+------+---+------+--------+---+------+------+
10259  */
10260 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
10261 {
10262     int opcode = extract32(insn, 11, 5);
10263
10264     switch (opcode) {
10265     case 0x3: /* logic ops */
10266         disas_simd_3same_logic(s, insn);
10267         break;
10268     case 0x17: /* ADDP */
10269     case 0x14: /* SMAXP, UMAXP */
10270     case 0x15: /* SMINP, UMINP */
10271     {
10272         /* Pairwise operations */
10273         int is_q = extract32(insn, 30, 1);
10274         int u = extract32(insn, 29, 1);
10275         int size = extract32(insn, 22, 2);
10276         int rm = extract32(insn, 16, 5);
10277         int rn = extract32(insn, 5, 5);
10278         int rd = extract32(insn, 0, 5);
10279         if (opcode == 0x17) {
10280             if (u || (size == 3 && !is_q)) {
10281                 unallocated_encoding(s);
10282                 return;
10283             }
10284         } else {
10285             if (size == 3) {
10286                 unallocated_encoding(s);
10287                 return;
10288             }
10289         }
10290         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
10291         break;
10292     }
10293     case 0x18 ... 0x31:
10294         /* floating point ops, sz[1] and U are part of opcode */
10295         disas_simd_3same_float(s, insn);
10296         break;
10297     default:
10298         disas_simd_3same_int(s, insn);
10299         break;
10300     }
10301 }
10302
10303 /*
10304  * Advanced SIMD three same (ARMv8.2 FP16 variants)
10305  *
10306  *  31  30  29  28       24 23  22 21 20  16 15 14 13    11 10  9    5 4    0
10307  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
10308  * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 |  Rm  | 0 0 | opcode | 1 |  Rn  |  Rd  |
10309  * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
10310  *
10311  * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
10312  * (register), FACGE, FABD, FCMGT (register) and FACGT.
10313  *
10314  */
10315 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
10316 {
10317     int opcode, fpopcode;
10318     int is_q, u, a, rm, rn, rd;
10319     int datasize, elements;
10320     int pass;
10321     TCGv_ptr fpst;
10322     bool pairwise = false;
10323
10324     if (!arm_dc_feature(s, ARM_FEATURE_V8_FP16)) {
10325         unallocated_encoding(s);
10326         return;
10327     }
10328
10329     if (!fp_access_check(s)) {
10330         return;
10331     }
10332
10333     /* For these floating point ops, the U, a and opcode bits
10334      * together indicate the operation.
10335      */
10336     opcode = extract32(insn, 11, 3);
10337     u = extract32(insn, 29, 1);
10338     a = extract32(insn, 23, 1);
10339     is_q = extract32(insn, 30, 1);
10340     rm = extract32(insn, 16, 5);
10341     rn = extract32(insn, 5, 5);
10342     rd = extract32(insn, 0, 5);
10343
10344     fpopcode = opcode | (a << 3) |  (u << 4);
10345     datasize = is_q ? 128 : 64;
10346     elements = datasize / 16;
10347
10348     switch (fpopcode) {
10349     case 0x10: /* FMAXNMP */
10350     case 0x12: /* FADDP */
10351     case 0x16: /* FMAXP */
10352     case 0x18: /* FMINNMP */
10353     case 0x1e: /* FMINP */
10354         pairwise = true;
10355         break;
10356     }
10357
10358     fpst = get_fpstatus_ptr(true);
10359
10360     if (pairwise) {
10361         int maxpass = is_q ? 8 : 4;
10362         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10363         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10364         TCGv_i32 tcg_res[8];
10365
10366         for (pass = 0; pass < maxpass; pass++) {
10367             int passreg = pass < (maxpass / 2) ? rn : rm;
10368             int passelt = (pass << 1) & (maxpass - 1);
10369
10370             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
10371             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
10372             tcg_res[pass] = tcg_temp_new_i32();
10373
10374             switch (fpopcode) {
10375             case 0x10: /* FMAXNMP */
10376                 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
10377                                            fpst);
10378                 break;
10379             case 0x12: /* FADDP */
10380                 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10381                 break;
10382             case 0x16: /* FMAXP */
10383                 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10384                 break;
10385             case 0x18: /* FMINNMP */
10386                 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
10387                                            fpst);
10388                 break;
10389             case 0x1e: /* FMINP */
10390                 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10391                 break;
10392             default:
10393                 g_assert_not_reached();
10394             }
10395         }
10396
10397         for (pass = 0; pass < maxpass; pass++) {
10398             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
10399             tcg_temp_free_i32(tcg_res[pass]);
10400         }
10401
10402         tcg_temp_free_i32(tcg_op1);
10403         tcg_temp_free_i32(tcg_op2);
10404
10405     } else {
10406         for (pass = 0; pass < elements; pass++) {
10407             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10408             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10409             TCGv_i32 tcg_res = tcg_temp_new_i32();
10410
10411             read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
10412             read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
10413
10414             switch (fpopcode) {
10415             case 0x0: /* FMAXNM */
10416                 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
10417                 break;
10418             case 0x1: /* FMLA */
10419                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
10420                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
10421                                            fpst);
10422                 break;
10423             case 0x2: /* FADD */
10424                 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
10425                 break;
10426             case 0x3: /* FMULX */
10427                 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
10428                 break;
10429             case 0x4: /* FCMEQ */
10430                 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
10431                 break;
10432             case 0x6: /* FMAX */
10433                 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
10434                 break;
10435             case 0x7: /* FRECPS */
10436                 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
10437                 break;
10438             case 0x8: /* FMINNM */
10439                 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
10440                 break;
10441             case 0x9: /* FMLS */
10442                 /* As usual for ARM, separate negation for fused multiply-add */
10443                 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
10444                 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
10445                 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
10446                                            fpst);
10447                 break;
10448             case 0xa: /* FSUB */
10449                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
10450                 break;
10451             case 0xe: /* FMIN */
10452                 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
10453                 break;
10454             case 0xf: /* FRSQRTS */
10455                 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
10456                 break;
10457             case 0x13: /* FMUL */
10458                 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
10459                 break;
10460             case 0x14: /* FCMGE */
10461                 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
10462                 break;
10463             case 0x15: /* FACGE */
10464                 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
10465                 break;
10466             case 0x17: /* FDIV */
10467                 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
10468                 break;
10469             case 0x1a: /* FABD */
10470                 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
10471                 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
10472                 break;
10473             case 0x1c: /* FCMGT */
10474                 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
10475                 break;
10476             case 0x1d: /* FACGT */
10477                 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
10478                 break;
10479             default:
10480                 fprintf(stderr, "%s: insn %#04x, fpop %#2x @ %#" PRIx64 "\n",
10481                         __func__, insn, fpopcode, s->pc);
10482                 g_assert_not_reached();
10483             }
10484
10485             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
10486             tcg_temp_free_i32(tcg_res);
10487             tcg_temp_free_i32(tcg_op1);
10488             tcg_temp_free_i32(tcg_op2);
10489         }
10490     }
10491
10492     tcg_temp_free_ptr(fpst);
10493
10494     clear_vec_high(s, is_q, rd);
10495 }
10496
10497 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
10498                                   int size, int rn, int rd)
10499 {
10500     /* Handle 2-reg-misc ops which are widening (so each size element
10501      * in the source becomes a 2*size element in the destination.
10502      * The only instruction like this is FCVTL.
10503      */
10504     int pass;
10505
10506     if (size == 3) {
10507         /* 32 -> 64 bit fp conversion */
10508         TCGv_i64 tcg_res[2];
10509         int srcelt = is_q ? 2 : 0;
10510
10511         for (pass = 0; pass < 2; pass++) {
10512             TCGv_i32 tcg_op = tcg_temp_new_i32();
10513             tcg_res[pass] = tcg_temp_new_i64();
10514
10515             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
10516             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
10517             tcg_temp_free_i32(tcg_op);
10518         }
10519         for (pass = 0; pass < 2; pass++) {
10520             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10521             tcg_temp_free_i64(tcg_res[pass]);
10522         }
10523     } else {
10524         /* 16 -> 32 bit fp conversion */
10525         int srcelt = is_q ? 4 : 0;
10526         TCGv_i32 tcg_res[4];
10527
10528         for (pass = 0; pass < 4; pass++) {
10529             tcg_res[pass] = tcg_temp_new_i32();
10530
10531             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
10532             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
10533                                            cpu_env);
10534         }
10535         for (pass = 0; pass < 4; pass++) {
10536             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
10537             tcg_temp_free_i32(tcg_res[pass]);
10538         }
10539     }
10540 }
10541
10542 static void handle_rev(DisasContext *s, int opcode, bool u,
10543                        bool is_q, int size, int rn, int rd)
10544 {
10545     int op = (opcode << 1) | u;
10546     int opsz = op + size;
10547     int grp_size = 3 - opsz;
10548     int dsize = is_q ? 128 : 64;
10549     int i;
10550
10551     if (opsz >= 3) {
10552         unallocated_encoding(s);
10553         return;
10554     }
10555
10556     if (!fp_access_check(s)) {
10557         return;
10558     }
10559
10560     if (size == 0) {
10561         /* Special case bytes, use bswap op on each group of elements */
10562         int groups = dsize / (8 << grp_size);
10563
10564         for (i = 0; i < groups; i++) {
10565             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
10566
10567             read_vec_element(s, tcg_tmp, rn, i, grp_size);
10568             switch (grp_size) {
10569             case MO_16:
10570                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
10571                 break;
10572             case MO_32:
10573                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
10574                 break;
10575             case MO_64:
10576                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
10577                 break;
10578             default:
10579                 g_assert_not_reached();
10580             }
10581             write_vec_element(s, tcg_tmp, rd, i, grp_size);
10582             tcg_temp_free_i64(tcg_tmp);
10583         }
10584         clear_vec_high(s, is_q, rd);
10585     } else {
10586         int revmask = (1 << grp_size) - 1;
10587         int esize = 8 << size;
10588         int elements = dsize / esize;
10589         TCGv_i64 tcg_rn = tcg_temp_new_i64();
10590         TCGv_i64 tcg_rd = tcg_const_i64(0);
10591         TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
10592
10593         for (i = 0; i < elements; i++) {
10594             int e_rev = (i & 0xf) ^ revmask;
10595             int off = e_rev * esize;
10596             read_vec_element(s, tcg_rn, rn, i, size);
10597             if (off >= 64) {
10598                 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
10599                                     tcg_rn, off - 64, esize);
10600             } else {
10601                 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
10602             }
10603         }
10604         write_vec_element(s, tcg_rd, rd, 0, MO_64);
10605         write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
10606
10607         tcg_temp_free_i64(tcg_rd_hi);
10608         tcg_temp_free_i64(tcg_rd);
10609         tcg_temp_free_i64(tcg_rn);
10610     }
10611 }
10612
10613 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
10614                                   bool is_q, int size, int rn, int rd)
10615 {
10616     /* Implement the pairwise operations from 2-misc:
10617      * SADDLP, UADDLP, SADALP, UADALP.
10618      * These all add pairs of elements in the input to produce a
10619      * double-width result element in the output (possibly accumulating).
10620      */
10621     bool accum = (opcode == 0x6);
10622     int maxpass = is_q ? 2 : 1;
10623     int pass;
10624     TCGv_i64 tcg_res[2];
10625
10626     if (size == 2) {
10627         /* 32 + 32 -> 64 op */
10628         TCGMemOp memop = size + (u ? 0 : MO_SIGN);
10629
10630         for (pass = 0; pass < maxpass; pass++) {
10631             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10632             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10633
10634             tcg_res[pass] = tcg_temp_new_i64();
10635
10636             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
10637             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
10638             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
10639             if (accum) {
10640                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
10641                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
10642             }
10643
10644             tcg_temp_free_i64(tcg_op1);
10645             tcg_temp_free_i64(tcg_op2);
10646         }
10647     } else {
10648         for (pass = 0; pass < maxpass; pass++) {
10649             TCGv_i64 tcg_op = tcg_temp_new_i64();
10650             NeonGenOneOpFn *genfn;
10651             static NeonGenOneOpFn * const fns[2][2] = {
10652                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
10653                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
10654             };
10655
10656             genfn = fns[size][u];
10657
10658             tcg_res[pass] = tcg_temp_new_i64();
10659
10660             read_vec_element(s, tcg_op, rn, pass, MO_64);
10661             genfn(tcg_res[pass], tcg_op);
10662
10663             if (accum) {
10664                 read_vec_element(s, tcg_op, rd, pass, MO_64);
10665                 if (size == 0) {
10666                     gen_helper_neon_addl_u16(tcg_res[pass],
10667                                              tcg_res[pass], tcg_op);
10668                 } else {
10669                     gen_helper_neon_addl_u32(tcg_res[pass],
10670                                              tcg_res[pass], tcg_op);
10671                 }
10672             }
10673             tcg_temp_free_i64(tcg_op);
10674         }
10675     }
10676     if (!is_q) {
10677         tcg_res[1] = tcg_const_i64(0);
10678     }
10679     for (pass = 0; pass < 2; pass++) {
10680         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10681         tcg_temp_free_i64(tcg_res[pass]);
10682     }
10683 }
10684
10685 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
10686 {
10687     /* Implement SHLL and SHLL2 */
10688     int pass;
10689     int part = is_q ? 2 : 0;
10690     TCGv_i64 tcg_res[2];
10691
10692     for (pass = 0; pass < 2; pass++) {
10693         static NeonGenWidenFn * const widenfns[3] = {
10694             gen_helper_neon_widen_u8,
10695             gen_helper_neon_widen_u16,
10696             tcg_gen_extu_i32_i64,
10697         };
10698         NeonGenWidenFn *widenfn = widenfns[size];
10699         TCGv_i32 tcg_op = tcg_temp_new_i32();
10700
10701         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
10702         tcg_res[pass] = tcg_temp_new_i64();
10703         widenfn(tcg_res[pass], tcg_op);
10704         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
10705
10706         tcg_temp_free_i32(tcg_op);
10707     }
10708
10709     for (pass = 0; pass < 2; pass++) {
10710         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10711         tcg_temp_free_i64(tcg_res[pass]);
10712     }
10713 }
10714
10715 /* AdvSIMD two reg misc
10716  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
10717  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
10718  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
10719  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
10720  */
10721 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
10722 {
10723     int size = extract32(insn, 22, 2);
10724     int opcode = extract32(insn, 12, 5);
10725     bool u = extract32(insn, 29, 1);
10726     bool is_q = extract32(insn, 30, 1);
10727     int rn = extract32(insn, 5, 5);
10728     int rd = extract32(insn, 0, 5);
10729     bool need_fpstatus = false;
10730     bool need_rmode = false;
10731     int rmode = -1;
10732     TCGv_i32 tcg_rmode;
10733     TCGv_ptr tcg_fpstatus;
10734
10735     switch (opcode) {
10736     case 0x0: /* REV64, REV32 */
10737     case 0x1: /* REV16 */
10738         handle_rev(s, opcode, u, is_q, size, rn, rd);
10739         return;
10740     case 0x5: /* CNT, NOT, RBIT */
10741         if (u && size == 0) {
10742             /* NOT */
10743             break;
10744         } else if (u && size == 1) {
10745             /* RBIT */
10746             break;
10747         } else if (!u && size == 0) {
10748             /* CNT */
10749             break;
10750         }
10751         unallocated_encoding(s);
10752         return;
10753     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
10754     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
10755         if (size == 3) {
10756             unallocated_encoding(s);
10757             return;
10758         }
10759         if (!fp_access_check(s)) {
10760             return;
10761         }
10762
10763         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
10764         return;
10765     case 0x4: /* CLS, CLZ */
10766         if (size == 3) {
10767             unallocated_encoding(s);
10768             return;
10769         }
10770         break;
10771     case 0x2: /* SADDLP, UADDLP */
10772     case 0x6: /* SADALP, UADALP */
10773         if (size == 3) {
10774             unallocated_encoding(s);
10775             return;
10776         }
10777         if (!fp_access_check(s)) {
10778             return;
10779         }
10780         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
10781         return;
10782     case 0x13: /* SHLL, SHLL2 */
10783         if (u == 0 || size == 3) {
10784             unallocated_encoding(s);
10785             return;
10786         }
10787         if (!fp_access_check(s)) {
10788             return;
10789         }
10790         handle_shll(s, is_q, size, rn, rd);
10791         return;
10792     case 0xa: /* CMLT */
10793         if (u == 1) {
10794             unallocated_encoding(s);
10795             return;
10796         }
10797         /* fall through */
10798     case 0x8: /* CMGT, CMGE */
10799     case 0x9: /* CMEQ, CMLE */
10800     case 0xb: /* ABS, NEG */
10801         if (size == 3 && !is_q) {
10802             unallocated_encoding(s);
10803             return;
10804         }
10805         break;
10806     case 0x3: /* SUQADD, USQADD */
10807         if (size == 3 && !is_q) {
10808             unallocated_encoding(s);
10809             return;
10810         }
10811         if (!fp_access_check(s)) {
10812             return;
10813         }
10814         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
10815         return;
10816     case 0x7: /* SQABS, SQNEG */
10817         if (size == 3 && !is_q) {
10818             unallocated_encoding(s);
10819             return;
10820         }
10821         break;
10822     case 0xc ... 0xf:
10823     case 0x16 ... 0x1d:
10824     case 0x1f:
10825     {
10826         /* Floating point: U, size[1] and opcode indicate operation;
10827          * size[0] indicates single or double precision.
10828          */
10829         int is_double = extract32(size, 0, 1);
10830         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10831         size = is_double ? 3 : 2;
10832         switch (opcode) {
10833         case 0x2f: /* FABS */
10834         case 0x6f: /* FNEG */
10835             if (size == 3 && !is_q) {
10836                 unallocated_encoding(s);
10837                 return;
10838             }
10839             break;
10840         case 0x1d: /* SCVTF */
10841         case 0x5d: /* UCVTF */
10842         {
10843             bool is_signed = (opcode == 0x1d) ? true : false;
10844             int elements = is_double ? 2 : is_q ? 4 : 2;
10845             if (is_double && !is_q) {
10846                 unallocated_encoding(s);
10847                 return;
10848             }
10849             if (!fp_access_check(s)) {
10850                 return;
10851             }
10852             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
10853             return;
10854         }
10855         case 0x2c: /* FCMGT (zero) */
10856         case 0x2d: /* FCMEQ (zero) */
10857         case 0x2e: /* FCMLT (zero) */
10858         case 0x6c: /* FCMGE (zero) */
10859         case 0x6d: /* FCMLE (zero) */
10860             if (size == 3 && !is_q) {
10861                 unallocated_encoding(s);
10862                 return;
10863             }
10864             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
10865             return;
10866         case 0x7f: /* FSQRT */
10867             if (size == 3 && !is_q) {
10868                 unallocated_encoding(s);
10869                 return;
10870             }
10871             break;
10872         case 0x1a: /* FCVTNS */
10873         case 0x1b: /* FCVTMS */
10874         case 0x3a: /* FCVTPS */
10875         case 0x3b: /* FCVTZS */
10876         case 0x5a: /* FCVTNU */
10877         case 0x5b: /* FCVTMU */
10878         case 0x7a: /* FCVTPU */
10879         case 0x7b: /* FCVTZU */
10880             need_fpstatus = true;
10881             need_rmode = true;
10882             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10883             if (size == 3 && !is_q) {
10884                 unallocated_encoding(s);
10885                 return;
10886             }
10887             break;
10888         case 0x5c: /* FCVTAU */
10889         case 0x1c: /* FCVTAS */
10890             need_fpstatus = true;
10891             need_rmode = true;
10892             rmode = FPROUNDING_TIEAWAY;
10893             if (size == 3 && !is_q) {
10894                 unallocated_encoding(s);
10895                 return;
10896             }
10897             break;
10898         case 0x3c: /* URECPE */
10899             if (size == 3) {
10900                 unallocated_encoding(s);
10901                 return;
10902             }
10903             /* fall through */
10904         case 0x3d: /* FRECPE */
10905         case 0x7d: /* FRSQRTE */
10906             if (size == 3 && !is_q) {
10907                 unallocated_encoding(s);
10908                 return;
10909             }
10910             if (!fp_access_check(s)) {
10911                 return;
10912             }
10913             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
10914             return;
10915         case 0x56: /* FCVTXN, FCVTXN2 */
10916             if (size == 2) {
10917                 unallocated_encoding(s);
10918                 return;
10919             }
10920             /* fall through */
10921         case 0x16: /* FCVTN, FCVTN2 */
10922             /* handle_2misc_narrow does a 2*size -> size operation, but these
10923              * instructions encode the source size rather than dest size.
10924              */
10925             if (!fp_access_check(s)) {
10926                 return;
10927             }
10928             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
10929             return;
10930         case 0x17: /* FCVTL, FCVTL2 */
10931             if (!fp_access_check(s)) {
10932                 return;
10933             }
10934             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
10935             return;
10936         case 0x18: /* FRINTN */
10937         case 0x19: /* FRINTM */
10938         case 0x38: /* FRINTP */
10939         case 0x39: /* FRINTZ */
10940             need_rmode = true;
10941             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10942             /* fall through */
10943         case 0x59: /* FRINTX */
10944         case 0x79: /* FRINTI */
10945             need_fpstatus = true;
10946             if (size == 3 && !is_q) {
10947                 unallocated_encoding(s);
10948                 return;
10949             }
10950             break;
10951         case 0x58: /* FRINTA */
10952             need_rmode = true;
10953             rmode = FPROUNDING_TIEAWAY;
10954             need_fpstatus = true;
10955             if (size == 3 && !is_q) {
10956                 unallocated_encoding(s);
10957                 return;
10958             }
10959             break;
10960         case 0x7c: /* URSQRTE */
10961             if (size == 3) {
10962                 unallocated_encoding(s);
10963                 return;
10964             }
10965             need_fpstatus = true;
10966             break;
10967         default:
10968             unallocated_encoding(s);
10969             return;
10970         }
10971         break;
10972     }
10973     default:
10974         unallocated_encoding(s);
10975         return;
10976     }
10977
10978     if (!fp_access_check(s)) {
10979         return;
10980     }
10981
10982     if (need_fpstatus || need_rmode) {
10983         tcg_fpstatus = get_fpstatus_ptr(false);
10984     } else {
10985         tcg_fpstatus = NULL;
10986     }
10987     if (need_rmode) {
10988         tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
10989         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
10990     } else {
10991         tcg_rmode = NULL;
10992     }
10993
10994     switch (opcode) {
10995     case 0x5:
10996         if (u && size == 0) { /* NOT */
10997             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
10998             return;
10999         }
11000         break;
11001     case 0xb:
11002         if (u) { /* NEG */
11003             gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
11004             return;
11005         }
11006         break;
11007     }
11008
11009     if (size == 3) {
11010         /* All 64-bit element operations can be shared with scalar 2misc */
11011         int pass;
11012
11013         for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
11014             TCGv_i64 tcg_op = tcg_temp_new_i64();
11015             TCGv_i64 tcg_res = tcg_temp_new_i64();
11016
11017             read_vec_element(s, tcg_op, rn, pass, MO_64);
11018
11019             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
11020                             tcg_rmode, tcg_fpstatus);
11021
11022             write_vec_element(s, tcg_res, rd, pass, MO_64);
11023
11024             tcg_temp_free_i64(tcg_res);
11025             tcg_temp_free_i64(tcg_op);
11026         }
11027     } else {
11028         int pass;
11029
11030         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11031             TCGv_i32 tcg_op = tcg_temp_new_i32();
11032             TCGv_i32 tcg_res = tcg_temp_new_i32();
11033             TCGCond cond;
11034
11035             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
11036
11037             if (size == 2) {
11038                 /* Special cases for 32 bit elements */
11039                 switch (opcode) {
11040                 case 0xa: /* CMLT */
11041                     /* 32 bit integer comparison against zero, result is
11042                      * test ? (2^32 - 1) : 0. We implement via setcond(test)
11043                      * and inverting.
11044                      */
11045                     cond = TCG_COND_LT;
11046                 do_cmop:
11047                     tcg_gen_setcondi_i32(cond, tcg_res, tcg_op, 0);
11048                     tcg_gen_neg_i32(tcg_res, tcg_res);
11049                     break;
11050                 case 0x8: /* CMGT, CMGE */
11051                     cond = u ? TCG_COND_GE : TCG_COND_GT;
11052                     goto do_cmop;
11053                 case 0x9: /* CMEQ, CMLE */
11054                     cond = u ? TCG_COND_LE : TCG_COND_EQ;
11055                     goto do_cmop;
11056                 case 0x4: /* CLS */
11057                     if (u) {
11058                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
11059                     } else {
11060                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
11061                     }
11062                     break;
11063                 case 0x7: /* SQABS, SQNEG */
11064                     if (u) {
11065                         gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
11066                     } else {
11067                         gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
11068                     }
11069                     break;
11070                 case 0xb: /* ABS, NEG */
11071                     if (u) {
11072                         tcg_gen_neg_i32(tcg_res, tcg_op);
11073                     } else {
11074                         TCGv_i32 tcg_zero = tcg_const_i32(0);
11075                         tcg_gen_neg_i32(tcg_res, tcg_op);
11076                         tcg_gen_movcond_i32(TCG_COND_GT, tcg_res, tcg_op,
11077                                             tcg_zero, tcg_op, tcg_res);
11078                         tcg_temp_free_i32(tcg_zero);
11079                     }
11080                     break;
11081                 case 0x2f: /* FABS */
11082                     gen_helper_vfp_abss(tcg_res, tcg_op);
11083                     break;
11084                 case 0x6f: /* FNEG */
11085                     gen_helper_vfp_negs(tcg_res, tcg_op);
11086                     break;
11087                 case 0x7f: /* FSQRT */
11088                     gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
11089                     break;
11090                 case 0x1a: /* FCVTNS */
11091                 case 0x1b: /* FCVTMS */
11092                 case 0x1c: /* FCVTAS */
11093                 case 0x3a: /* FCVTPS */
11094                 case 0x3b: /* FCVTZS */
11095                 {
11096                     TCGv_i32 tcg_shift = tcg_const_i32(0);
11097                     gen_helper_vfp_tosls(tcg_res, tcg_op,
11098                                          tcg_shift, tcg_fpstatus);
11099                     tcg_temp_free_i32(tcg_shift);
11100                     break;
11101                 }
11102                 case 0x5a: /* FCVTNU */
11103                 case 0x5b: /* FCVTMU */
11104                 case 0x5c: /* FCVTAU */
11105                 case 0x7a: /* FCVTPU */
11106                 case 0x7b: /* FCVTZU */
11107                 {
11108                     TCGv_i32 tcg_shift = tcg_const_i32(0);
11109                     gen_helper_vfp_touls(tcg_res, tcg_op,
11110                                          tcg_shift, tcg_fpstatus);
11111                     tcg_temp_free_i32(tcg_shift);
11112                     break;
11113                 }
11114                 case 0x18: /* FRINTN */
11115                 case 0x19: /* FRINTM */
11116                 case 0x38: /* FRINTP */
11117                 case 0x39: /* FRINTZ */
11118                 case 0x58: /* FRINTA */
11119                 case 0x79: /* FRINTI */
11120                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
11121                     break;
11122                 case 0x59: /* FRINTX */
11123                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
11124                     break;
11125                 case 0x7c: /* URSQRTE */
11126                     gen_helper_rsqrte_u32(tcg_res, tcg_op, tcg_fpstatus);
11127                     break;
11128                 default:
11129                     g_assert_not_reached();
11130                 }
11131             } else {
11132                 /* Use helpers for 8 and 16 bit elements */
11133                 switch (opcode) {
11134                 case 0x5: /* CNT, RBIT */
11135                     /* For these two insns size is part of the opcode specifier
11136                      * (handled earlier); they always operate on byte elements.
11137                      */
11138                     if (u) {
11139                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
11140                     } else {
11141                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
11142                     }
11143                     break;
11144                 case 0x7: /* SQABS, SQNEG */
11145                 {
11146                     NeonGenOneOpEnvFn *genfn;
11147                     static NeonGenOneOpEnvFn * const fns[2][2] = {
11148                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
11149                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
11150                     };
11151                     genfn = fns[size][u];
11152                     genfn(tcg_res, cpu_env, tcg_op);
11153                     break;
11154                 }
11155                 case 0x8: /* CMGT, CMGE */
11156                 case 0x9: /* CMEQ, CMLE */
11157                 case 0xa: /* CMLT */
11158                 {
11159                     static NeonGenTwoOpFn * const fns[3][2] = {
11160                         { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_s16 },
11161                         { gen_helper_neon_cge_s8, gen_helper_neon_cge_s16 },
11162                         { gen_helper_neon_ceq_u8, gen_helper_neon_ceq_u16 },
11163                     };
11164                     NeonGenTwoOpFn *genfn;
11165                     int comp;
11166                     bool reverse;
11167                     TCGv_i32 tcg_zero = tcg_const_i32(0);
11168
11169                     /* comp = index into [CMGT, CMGE, CMEQ, CMLE, CMLT] */
11170                     comp = (opcode - 0x8) * 2 + u;
11171                     /* ...but LE, LT are implemented as reverse GE, GT */
11172                     reverse = (comp > 2);
11173                     if (reverse) {
11174                         comp = 4 - comp;
11175                     }
11176                     genfn = fns[comp][size];
11177                     if (reverse) {
11178                         genfn(tcg_res, tcg_zero, tcg_op);
11179                     } else {
11180                         genfn(tcg_res, tcg_op, tcg_zero);
11181                     }
11182                     tcg_temp_free_i32(tcg_zero);
11183                     break;
11184                 }
11185                 case 0xb: /* ABS, NEG */
11186                     if (u) {
11187                         TCGv_i32 tcg_zero = tcg_const_i32(0);
11188                         if (size) {
11189                             gen_helper_neon_sub_u16(tcg_res, tcg_zero, tcg_op);
11190                         } else {
11191                             gen_helper_neon_sub_u8(tcg_res, tcg_zero, tcg_op);
11192                         }
11193                         tcg_temp_free_i32(tcg_zero);
11194                     } else {
11195                         if (size) {
11196                             gen_helper_neon_abs_s16(tcg_res, tcg_op);
11197                         } else {
11198                             gen_helper_neon_abs_s8(tcg_res, tcg_op);
11199                         }
11200                     }
11201                     break;
11202                 case 0x4: /* CLS, CLZ */
11203                     if (u) {
11204                         if (size == 0) {
11205                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
11206                         } else {
11207                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
11208                         }
11209                     } else {
11210                         if (size == 0) {
11211                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
11212                         } else {
11213                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
11214                         }
11215                     }
11216                     break;
11217                 default:
11218                     g_assert_not_reached();
11219                 }
11220             }
11221
11222             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11223
11224             tcg_temp_free_i32(tcg_res);
11225             tcg_temp_free_i32(tcg_op);
11226         }
11227     }
11228     clear_vec_high(s, is_q, rd);
11229
11230     if (need_rmode) {
11231         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
11232         tcg_temp_free_i32(tcg_rmode);
11233     }
11234     if (need_fpstatus) {
11235         tcg_temp_free_ptr(tcg_fpstatus);
11236     }
11237 }
11238
11239 /* AdvSIMD [scalar] two register miscellaneous (FP16)
11240  *
11241  *   31  30  29 28  27     24  23 22 21       17 16    12 11 10 9    5 4    0
11242  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
11243  * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
11244  * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
11245  *   mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
11246  *   val:  0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
11247  *
11248  * This actually covers two groups where scalar access is governed by
11249  * bit 28. A bunch of the instructions (float to integral) only exist
11250  * in the vector form and are un-allocated for the scalar decode. Also
11251  * in the scalar decode Q is always 1.
11252  */
11253 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
11254 {
11255     int fpop, opcode, a, u;
11256     int rn, rd;
11257     bool is_q;
11258     bool is_scalar;
11259     bool only_in_vector = false;
11260
11261     int pass;
11262     TCGv_i32 tcg_rmode = NULL;
11263     TCGv_ptr tcg_fpstatus = NULL;
11264     bool need_rmode = false;
11265     bool need_fpst = true;
11266     int rmode;
11267
11268     if (!arm_dc_feature(s, ARM_FEATURE_V8_FP16)) {
11269         unallocated_encoding(s);
11270         return;
11271     }
11272
11273     rd = extract32(insn, 0, 5);
11274     rn = extract32(insn, 5, 5);
11275
11276     a = extract32(insn, 23, 1);
11277     u = extract32(insn, 29, 1);
11278     is_scalar = extract32(insn, 28, 1);
11279     is_q = extract32(insn, 30, 1);
11280
11281     opcode = extract32(insn, 12, 5);
11282     fpop = deposit32(opcode, 5, 1, a);
11283     fpop = deposit32(fpop, 6, 1, u);
11284
11285     rd = extract32(insn, 0, 5);
11286     rn = extract32(insn, 5, 5);
11287
11288     switch (fpop) {
11289     case 0x1d: /* SCVTF */
11290     case 0x5d: /* UCVTF */
11291     {
11292         int elements;
11293
11294         if (is_scalar) {
11295             elements = 1;
11296         } else {
11297             elements = (is_q ? 8 : 4);
11298         }
11299
11300         if (!fp_access_check(s)) {
11301             return;
11302         }
11303         handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
11304         return;
11305     }
11306     break;
11307     case 0x2c: /* FCMGT (zero) */
11308     case 0x2d: /* FCMEQ (zero) */
11309     case 0x2e: /* FCMLT (zero) */
11310     case 0x6c: /* FCMGE (zero) */
11311     case 0x6d: /* FCMLE (zero) */
11312         handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
11313         return;
11314     case 0x3d: /* FRECPE */
11315     case 0x3f: /* FRECPX */
11316         break;
11317     case 0x18: /* FRINTN */
11318         need_rmode = true;
11319         only_in_vector = true;
11320         rmode = FPROUNDING_TIEEVEN;
11321         break;
11322     case 0x19: /* FRINTM */
11323         need_rmode = true;
11324         only_in_vector = true;
11325         rmode = FPROUNDING_NEGINF;
11326         break;
11327     case 0x38: /* FRINTP */
11328         need_rmode = true;
11329         only_in_vector = true;
11330         rmode = FPROUNDING_POSINF;
11331         break;
11332     case 0x39: /* FRINTZ */
11333         need_rmode = true;
11334         only_in_vector = true;
11335         rmode = FPROUNDING_ZERO;
11336         break;
11337     case 0x58: /* FRINTA */
11338         need_rmode = true;
11339         only_in_vector = true;
11340         rmode = FPROUNDING_TIEAWAY;
11341         break;
11342     case 0x59: /* FRINTX */
11343     case 0x79: /* FRINTI */
11344         only_in_vector = true;
11345         /* current rounding mode */
11346         break;
11347     case 0x1a: /* FCVTNS */
11348         need_rmode = true;
11349         rmode = FPROUNDING_TIEEVEN;
11350         break;
11351     case 0x1b: /* FCVTMS */
11352         need_rmode = true;
11353         rmode = FPROUNDING_NEGINF;
11354         break;
11355     case 0x1c: /* FCVTAS */
11356         need_rmode = true;
11357         rmode = FPROUNDING_TIEAWAY;
11358         break;
11359     case 0x3a: /* FCVTPS */
11360         need_rmode = true;
11361         rmode = FPROUNDING_POSINF;
11362         break;
11363     case 0x3b: /* FCVTZS */
11364         need_rmode = true;
11365         rmode = FPROUNDING_ZERO;
11366         break;
11367     case 0x5a: /* FCVTNU */
11368         need_rmode = true;
11369         rmode = FPROUNDING_TIEEVEN;
11370         break;
11371     case 0x5b: /* FCVTMU */
11372         need_rmode = true;
11373         rmode = FPROUNDING_NEGINF;
11374         break;
11375     case 0x5c: /* FCVTAU */
11376         need_rmode = true;
11377         rmode = FPROUNDING_TIEAWAY;
11378         break;
11379     case 0x7a: /* FCVTPU */
11380         need_rmode = true;
11381         rmode = FPROUNDING_POSINF;
11382         break;
11383     case 0x7b: /* FCVTZU */
11384         need_rmode = true;
11385         rmode = FPROUNDING_ZERO;
11386         break;
11387     case 0x2f: /* FABS */
11388     case 0x6f: /* FNEG */
11389         need_fpst = false;
11390         break;
11391     case 0x7d: /* FRSQRTE */
11392     case 0x7f: /* FSQRT (vector) */
11393         break;
11394     default:
11395         fprintf(stderr, "%s: insn %#04x fpop %#2x\n", __func__, insn, fpop);
11396         g_assert_not_reached();
11397     }
11398
11399
11400     /* Check additional constraints for the scalar encoding */
11401     if (is_scalar) {
11402         if (!is_q) {
11403             unallocated_encoding(s);
11404             return;
11405         }
11406         /* FRINTxx is only in the vector form */
11407         if (only_in_vector) {
11408             unallocated_encoding(s);
11409             return;
11410         }
11411     }
11412
11413     if (!fp_access_check(s)) {
11414         return;
11415     }
11416
11417     if (need_rmode || need_fpst) {
11418         tcg_fpstatus = get_fpstatus_ptr(true);
11419     }
11420
11421     if (need_rmode) {
11422         tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
11423         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
11424     }
11425
11426     if (is_scalar) {
11427         TCGv_i32 tcg_op = tcg_temp_new_i32();
11428         TCGv_i32 tcg_res = tcg_temp_new_i32();
11429
11430         read_vec_element_i32(s, tcg_op, rn, 0, MO_16);
11431
11432         switch (fpop) {
11433         case 0x1a: /* FCVTNS */
11434         case 0x1b: /* FCVTMS */
11435         case 0x1c: /* FCVTAS */
11436         case 0x3a: /* FCVTPS */
11437         case 0x3b: /* FCVTZS */
11438             gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
11439             break;
11440         case 0x3d: /* FRECPE */
11441             gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
11442             break;
11443         case 0x3f: /* FRECPX */
11444             gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
11445             break;
11446         case 0x5a: /* FCVTNU */
11447         case 0x5b: /* FCVTMU */
11448         case 0x5c: /* FCVTAU */
11449         case 0x7a: /* FCVTPU */
11450         case 0x7b: /* FCVTZU */
11451             gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
11452             break;
11453         case 0x6f: /* FNEG */
11454             tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
11455             break;
11456         case 0x7d: /* FRSQRTE */
11457             gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
11458             break;
11459         default:
11460             g_assert_not_reached();
11461         }
11462
11463         /* limit any sign extension going on */
11464         tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
11465         write_fp_sreg(s, rd, tcg_res);
11466
11467         tcg_temp_free_i32(tcg_res);
11468         tcg_temp_free_i32(tcg_op);
11469     } else {
11470         for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
11471             TCGv_i32 tcg_op = tcg_temp_new_i32();
11472             TCGv_i32 tcg_res = tcg_temp_new_i32();
11473
11474             read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
11475
11476             switch (fpop) {
11477             case 0x1a: /* FCVTNS */
11478             case 0x1b: /* FCVTMS */
11479             case 0x1c: /* FCVTAS */
11480             case 0x3a: /* FCVTPS */
11481             case 0x3b: /* FCVTZS */
11482                 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
11483                 break;
11484             case 0x3d: /* FRECPE */
11485                 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
11486                 break;
11487             case 0x5a: /* FCVTNU */
11488             case 0x5b: /* FCVTMU */
11489             case 0x5c: /* FCVTAU */
11490             case 0x7a: /* FCVTPU */
11491             case 0x7b: /* FCVTZU */
11492                 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
11493                 break;
11494             case 0x18: /* FRINTN */
11495             case 0x19: /* FRINTM */
11496             case 0x38: /* FRINTP */
11497             case 0x39: /* FRINTZ */
11498             case 0x58: /* FRINTA */
11499             case 0x79: /* FRINTI */
11500                 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
11501                 break;
11502             case 0x59: /* FRINTX */
11503                 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
11504                 break;
11505             case 0x2f: /* FABS */
11506                 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
11507                 break;
11508             case 0x6f: /* FNEG */
11509                 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
11510                 break;
11511             case 0x7d: /* FRSQRTE */
11512                 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
11513                 break;
11514             case 0x7f: /* FSQRT */
11515                 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
11516                 break;
11517             default:
11518                 g_assert_not_reached();
11519             }
11520
11521             write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11522
11523             tcg_temp_free_i32(tcg_res);
11524             tcg_temp_free_i32(tcg_op);
11525         }
11526
11527         clear_vec_high(s, is_q, rd);
11528     }
11529
11530     if (tcg_rmode) {
11531         gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
11532         tcg_temp_free_i32(tcg_rmode);
11533     }
11534
11535     if (tcg_fpstatus) {
11536         tcg_temp_free_ptr(tcg_fpstatus);
11537     }
11538 }
11539
11540 /* AdvSIMD scalar x indexed element
11541  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
11542  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
11543  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
11544  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
11545  * AdvSIMD vector x indexed element
11546  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
11547  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
11548  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
11549  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
11550  */
11551 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
11552 {
11553     /* This encoding has two kinds of instruction:
11554      *  normal, where we perform elt x idxelt => elt for each
11555      *     element in the vector
11556      *  long, where we perform elt x idxelt and generate a result of
11557      *     double the width of the input element
11558      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
11559      */
11560     bool is_scalar = extract32(insn, 28, 1);
11561     bool is_q = extract32(insn, 30, 1);
11562     bool u = extract32(insn, 29, 1);
11563     int size = extract32(insn, 22, 2);
11564     int l = extract32(insn, 21, 1);
11565     int m = extract32(insn, 20, 1);
11566     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
11567     int rm = extract32(insn, 16, 4);
11568     int opcode = extract32(insn, 12, 4);
11569     int h = extract32(insn, 11, 1);
11570     int rn = extract32(insn, 5, 5);
11571     int rd = extract32(insn, 0, 5);
11572     bool is_long = false;
11573     bool is_fp = false;
11574     bool is_fp16 = false;
11575     int index;
11576     TCGv_ptr fpst;
11577
11578     switch (opcode) {
11579     case 0x0: /* MLA */
11580     case 0x4: /* MLS */
11581         if (!u || is_scalar) {
11582             unallocated_encoding(s);
11583             return;
11584         }
11585         break;
11586     case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
11587     case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
11588     case 0xa: /* SMULL, SMULL2, UMULL, UMULL2 */
11589         if (is_scalar) {
11590             unallocated_encoding(s);
11591             return;
11592         }
11593         is_long = true;
11594         break;
11595     case 0x3: /* SQDMLAL, SQDMLAL2 */
11596     case 0x7: /* SQDMLSL, SQDMLSL2 */
11597     case 0xb: /* SQDMULL, SQDMULL2 */
11598         is_long = true;
11599         /* fall through */
11600     case 0xc: /* SQDMULH */
11601     case 0xd: /* SQRDMULH */
11602         if (u) {
11603             unallocated_encoding(s);
11604             return;
11605         }
11606         break;
11607     case 0x8: /* MUL */
11608         if (u || is_scalar) {
11609             unallocated_encoding(s);
11610             return;
11611         }
11612         break;
11613     case 0x1: /* FMLA */
11614     case 0x5: /* FMLS */
11615         if (u) {
11616             unallocated_encoding(s);
11617             return;
11618         }
11619         /* fall through */
11620     case 0x9: /* FMUL, FMULX */
11621         if (size == 1) {
11622             unallocated_encoding(s);
11623             return;
11624         }
11625         is_fp = true;
11626         break;
11627     default:
11628         unallocated_encoding(s);
11629         return;
11630     }
11631
11632     if (is_fp) {
11633         /* convert insn encoded size to TCGMemOp size */
11634         switch (size) {
11635         case 2: /* single precision */
11636             size = MO_32;
11637             index = h << 1 | l;
11638             rm |= (m << 4);
11639             break;
11640         case 3: /* double precision */
11641             size = MO_64;
11642             if (l || !is_q) {
11643                 unallocated_encoding(s);
11644                 return;
11645             }
11646             index = h;
11647             rm |= (m << 4);
11648             break;
11649         case 0: /* half precision */
11650             size = MO_16;
11651             index = h << 2 | l << 1 | m;
11652             is_fp16 = true;
11653             if (arm_dc_feature(s, ARM_FEATURE_V8_FP16)) {
11654                 break;
11655             }
11656             /* fallthru */
11657         default: /* unallocated */
11658             unallocated_encoding(s);
11659             return;
11660         }
11661     } else {
11662         switch (size) {
11663         case 1:
11664             index = h << 2 | l << 1 | m;
11665             break;
11666         case 2:
11667             index = h << 1 | l;
11668             rm |= (m << 4);
11669             break;
11670         default:
11671             unallocated_encoding(s);
11672             return;
11673         }
11674     }
11675
11676     if (!fp_access_check(s)) {
11677         return;
11678     }
11679
11680     if (is_fp) {
11681         fpst = get_fpstatus_ptr(is_fp16);
11682     } else {
11683         fpst = NULL;
11684     }
11685
11686     if (size == 3) {
11687         TCGv_i64 tcg_idx = tcg_temp_new_i64();
11688         int pass;
11689
11690         assert(is_fp && is_q && !is_long);
11691
11692         read_vec_element(s, tcg_idx, rm, index, MO_64);
11693
11694         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
11695             TCGv_i64 tcg_op = tcg_temp_new_i64();
11696             TCGv_i64 tcg_res = tcg_temp_new_i64();
11697
11698             read_vec_element(s, tcg_op, rn, pass, MO_64);
11699
11700             switch (opcode) {
11701             case 0x5: /* FMLS */
11702                 /* As usual for ARM, separate negation for fused multiply-add */
11703                 gen_helper_vfp_negd(tcg_op, tcg_op);
11704                 /* fall through */
11705             case 0x1: /* FMLA */
11706                 read_vec_element(s, tcg_res, rd, pass, MO_64);
11707                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
11708                 break;
11709             case 0x9: /* FMUL, FMULX */
11710                 if (u) {
11711                     gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
11712                 } else {
11713                     gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
11714                 }
11715                 break;
11716             default:
11717                 g_assert_not_reached();
11718             }
11719
11720             write_vec_element(s, tcg_res, rd, pass, MO_64);
11721             tcg_temp_free_i64(tcg_op);
11722             tcg_temp_free_i64(tcg_res);
11723         }
11724
11725         tcg_temp_free_i64(tcg_idx);
11726         clear_vec_high(s, !is_scalar, rd);
11727     } else if (!is_long) {
11728         /* 32 bit floating point, or 16 or 32 bit integer.
11729          * For the 16 bit scalar case we use the usual Neon helpers and
11730          * rely on the fact that 0 op 0 == 0 with no side effects.
11731          */
11732         TCGv_i32 tcg_idx = tcg_temp_new_i32();
11733         int pass, maxpasses;
11734
11735         if (is_scalar) {
11736             maxpasses = 1;
11737         } else {
11738             maxpasses = is_q ? 4 : 2;
11739         }
11740
11741         read_vec_element_i32(s, tcg_idx, rm, index, size);
11742
11743         if (size == 1 && !is_scalar) {
11744             /* The simplest way to handle the 16x16 indexed ops is to duplicate
11745              * the index into both halves of the 32 bit tcg_idx and then use
11746              * the usual Neon helpers.
11747              */
11748             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
11749         }
11750
11751         for (pass = 0; pass < maxpasses; pass++) {
11752             TCGv_i32 tcg_op = tcg_temp_new_i32();
11753             TCGv_i32 tcg_res = tcg_temp_new_i32();
11754
11755             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
11756
11757             switch (opcode) {
11758             case 0x0: /* MLA */
11759             case 0x4: /* MLS */
11760             case 0x8: /* MUL */
11761             {
11762                 static NeonGenTwoOpFn * const fns[2][2] = {
11763                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
11764                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
11765                 };
11766                 NeonGenTwoOpFn *genfn;
11767                 bool is_sub = opcode == 0x4;
11768
11769                 if (size == 1) {
11770                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
11771                 } else {
11772                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
11773                 }
11774                 if (opcode == 0x8) {
11775                     break;
11776                 }
11777                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
11778                 genfn = fns[size - 1][is_sub];
11779                 genfn(tcg_res, tcg_op, tcg_res);
11780                 break;
11781             }
11782             case 0x5: /* FMLS */
11783             case 0x1: /* FMLA */
11784                 read_vec_element_i32(s, tcg_res, rd, pass,
11785                                      is_scalar ? size : MO_32);
11786                 switch (size) {
11787                 case 1:
11788                     if (opcode == 0x5) {
11789                         /* As usual for ARM, separate negation for fused
11790                          * multiply-add */
11791                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
11792                     }
11793                     if (is_scalar) {
11794                         gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
11795                                                    tcg_res, fpst);
11796                     } else {
11797                         gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
11798                                                     tcg_res, fpst);
11799                     }
11800                     break;
11801                 case 2:
11802                     if (opcode == 0x5) {
11803                         /* As usual for ARM, separate negation for
11804                          * fused multiply-add */
11805                         tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
11806                     }
11807                     gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
11808                                            tcg_res, fpst);
11809                     break;
11810                 default:
11811                     g_assert_not_reached();
11812                 }
11813                 break;
11814             case 0x9: /* FMUL, FMULX */
11815                 switch (size) {
11816                 case 1:
11817                     if (u) {
11818                         if (is_scalar) {
11819                             gen_helper_advsimd_mulxh(tcg_res, tcg_op,
11820                                                      tcg_idx, fpst);
11821                         } else {
11822                             gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
11823                                                       tcg_idx, fpst);
11824                         }
11825                     } else {
11826                         if (is_scalar) {
11827                             gen_helper_advsimd_mulh(tcg_res, tcg_op,
11828                                                     tcg_idx, fpst);
11829                         } else {
11830                             gen_helper_advsimd_mul2h(tcg_res, tcg_op,
11831                                                      tcg_idx, fpst);
11832                         }
11833                     }
11834                     break;
11835                 case 2:
11836                     if (u) {
11837                         gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
11838                     } else {
11839                         gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
11840                     }
11841                     break;
11842                 default:
11843                     g_assert_not_reached();
11844                 }
11845                 break;
11846             case 0xc: /* SQDMULH */
11847                 if (size == 1) {
11848                     gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
11849                                                tcg_op, tcg_idx);
11850                 } else {
11851                     gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
11852                                                tcg_op, tcg_idx);
11853                 }
11854                 break;
11855             case 0xd: /* SQRDMULH */
11856                 if (size == 1) {
11857                     gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
11858                                                 tcg_op, tcg_idx);
11859                 } else {
11860                     gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
11861                                                 tcg_op, tcg_idx);
11862                 }
11863                 break;
11864             default:
11865                 g_assert_not_reached();
11866             }
11867
11868             if (is_scalar) {
11869                 write_fp_sreg(s, rd, tcg_res);
11870             } else {
11871                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11872             }
11873
11874             tcg_temp_free_i32(tcg_op);
11875             tcg_temp_free_i32(tcg_res);
11876         }
11877
11878         tcg_temp_free_i32(tcg_idx);
11879         clear_vec_high(s, is_q, rd);
11880     } else {
11881         /* long ops: 16x16->32 or 32x32->64 */
11882         TCGv_i64 tcg_res[2];
11883         int pass;
11884         bool satop = extract32(opcode, 0, 1);
11885         TCGMemOp memop = MO_32;
11886
11887         if (satop || !u) {
11888             memop |= MO_SIGN;
11889         }
11890
11891         if (size == 2) {
11892             TCGv_i64 tcg_idx = tcg_temp_new_i64();
11893
11894             read_vec_element(s, tcg_idx, rm, index, memop);
11895
11896             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
11897                 TCGv_i64 tcg_op = tcg_temp_new_i64();
11898                 TCGv_i64 tcg_passres;
11899                 int passelt;
11900
11901                 if (is_scalar) {
11902                     passelt = 0;
11903                 } else {
11904                     passelt = pass + (is_q * 2);
11905                 }
11906
11907                 read_vec_element(s, tcg_op, rn, passelt, memop);
11908
11909                 tcg_res[pass] = tcg_temp_new_i64();
11910
11911                 if (opcode == 0xa || opcode == 0xb) {
11912                     /* Non-accumulating ops */
11913                     tcg_passres = tcg_res[pass];
11914                 } else {
11915                     tcg_passres = tcg_temp_new_i64();
11916                 }
11917
11918                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
11919                 tcg_temp_free_i64(tcg_op);
11920
11921                 if (satop) {
11922                     /* saturating, doubling */
11923                     gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
11924                                                       tcg_passres, tcg_passres);
11925                 }
11926
11927                 if (opcode == 0xa || opcode == 0xb) {
11928                     continue;
11929                 }
11930
11931                 /* Accumulating op: handle accumulate step */
11932                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11933
11934                 switch (opcode) {
11935                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
11936                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
11937                     break;
11938                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
11939                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
11940                     break;
11941                 case 0x7: /* SQDMLSL, SQDMLSL2 */
11942                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
11943                     /* fall through */
11944                 case 0x3: /* SQDMLAL, SQDMLAL2 */
11945                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
11946                                                       tcg_res[pass],
11947                                                       tcg_passres);
11948                     break;
11949                 default:
11950                     g_assert_not_reached();
11951                 }
11952                 tcg_temp_free_i64(tcg_passres);
11953             }
11954             tcg_temp_free_i64(tcg_idx);
11955
11956             clear_vec_high(s, !is_scalar, rd);
11957         } else {
11958             TCGv_i32 tcg_idx = tcg_temp_new_i32();
11959
11960             assert(size == 1);
11961             read_vec_element_i32(s, tcg_idx, rm, index, size);
11962
11963             if (!is_scalar) {
11964                 /* The simplest way to handle the 16x16 indexed ops is to
11965                  * duplicate the index into both halves of the 32 bit tcg_idx
11966                  * and then use the usual Neon helpers.
11967                  */
11968                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
11969             }
11970
11971             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
11972                 TCGv_i32 tcg_op = tcg_temp_new_i32();
11973                 TCGv_i64 tcg_passres;
11974
11975                 if (is_scalar) {
11976                     read_vec_element_i32(s, tcg_op, rn, pass, size);
11977                 } else {
11978                     read_vec_element_i32(s, tcg_op, rn,
11979                                          pass + (is_q * 2), MO_32);
11980                 }
11981
11982                 tcg_res[pass] = tcg_temp_new_i64();
11983
11984                 if (opcode == 0xa || opcode == 0xb) {
11985                     /* Non-accumulating ops */
11986                     tcg_passres = tcg_res[pass];
11987                 } else {
11988                     tcg_passres = tcg_temp_new_i64();
11989                 }
11990
11991                 if (memop & MO_SIGN) {
11992                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
11993                 } else {
11994                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
11995                 }
11996                 if (satop) {
11997                     gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
11998                                                       tcg_passres, tcg_passres);
11999                 }
12000                 tcg_temp_free_i32(tcg_op);
12001
12002                 if (opcode == 0xa || opcode == 0xb) {
12003                     continue;
12004                 }
12005
12006                 /* Accumulating op: handle accumulate step */
12007                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12008
12009                 switch (opcode) {
12010                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
12011                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
12012                                              tcg_passres);
12013                     break;
12014                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
12015                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
12016                                              tcg_passres);
12017                     break;
12018                 case 0x7: /* SQDMLSL, SQDMLSL2 */
12019                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
12020                     /* fall through */
12021                 case 0x3: /* SQDMLAL, SQDMLAL2 */
12022                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
12023                                                       tcg_res[pass],
12024                                                       tcg_passres);
12025                     break;
12026                 default:
12027                     g_assert_not_reached();
12028                 }
12029                 tcg_temp_free_i64(tcg_passres);
12030             }
12031             tcg_temp_free_i32(tcg_idx);
12032
12033             if (is_scalar) {
12034                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
12035             }
12036         }
12037
12038         if (is_scalar) {
12039             tcg_res[1] = tcg_const_i64(0);
12040         }
12041
12042         for (pass = 0; pass < 2; pass++) {
12043             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12044             tcg_temp_free_i64(tcg_res[pass]);
12045         }
12046     }
12047
12048     if (fpst) {
12049         tcg_temp_free_ptr(fpst);
12050     }
12051 }
12052
12053 /* Crypto AES
12054  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
12055  * +-----------------+------+-----------+--------+-----+------+------+
12056  * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12057  * +-----------------+------+-----------+--------+-----+------+------+
12058  */
12059 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
12060 {
12061     int size = extract32(insn, 22, 2);
12062     int opcode = extract32(insn, 12, 5);
12063     int rn = extract32(insn, 5, 5);
12064     int rd = extract32(insn, 0, 5);
12065     int decrypt;
12066     TCGv_ptr tcg_rd_ptr, tcg_rn_ptr;
12067     TCGv_i32 tcg_decrypt;
12068     CryptoThreeOpIntFn *genfn;
12069
12070     if (!arm_dc_feature(s, ARM_FEATURE_V8_AES)
12071         || size != 0) {
12072         unallocated_encoding(s);
12073         return;
12074     }
12075
12076     switch (opcode) {
12077     case 0x4: /* AESE */
12078         decrypt = 0;
12079         genfn = gen_helper_crypto_aese;
12080         break;
12081     case 0x6: /* AESMC */
12082         decrypt = 0;
12083         genfn = gen_helper_crypto_aesmc;
12084         break;
12085     case 0x5: /* AESD */
12086         decrypt = 1;
12087         genfn = gen_helper_crypto_aese;
12088         break;
12089     case 0x7: /* AESIMC */
12090         decrypt = 1;
12091         genfn = gen_helper_crypto_aesmc;
12092         break;
12093     default:
12094         unallocated_encoding(s);
12095         return;
12096     }
12097
12098     if (!fp_access_check(s)) {
12099         return;
12100     }
12101
12102     tcg_rd_ptr = vec_full_reg_ptr(s, rd);
12103     tcg_rn_ptr = vec_full_reg_ptr(s, rn);
12104     tcg_decrypt = tcg_const_i32(decrypt);
12105
12106     genfn(tcg_rd_ptr, tcg_rn_ptr, tcg_decrypt);
12107
12108     tcg_temp_free_ptr(tcg_rd_ptr);
12109     tcg_temp_free_ptr(tcg_rn_ptr);
12110     tcg_temp_free_i32(tcg_decrypt);
12111 }
12112
12113 /* Crypto three-reg SHA
12114  *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
12115  * +-----------------+------+---+------+---+--------+-----+------+------+
12116  * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
12117  * +-----------------+------+---+------+---+--------+-----+------+------+
12118  */
12119 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
12120 {
12121     int size = extract32(insn, 22, 2);
12122     int opcode = extract32(insn, 12, 3);
12123     int rm = extract32(insn, 16, 5);
12124     int rn = extract32(insn, 5, 5);
12125     int rd = extract32(insn, 0, 5);
12126     CryptoThreeOpFn *genfn;
12127     TCGv_ptr tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr;
12128     int feature = ARM_FEATURE_V8_SHA256;
12129
12130     if (size != 0) {
12131         unallocated_encoding(s);
12132         return;
12133     }
12134
12135     switch (opcode) {
12136     case 0: /* SHA1C */
12137     case 1: /* SHA1P */
12138     case 2: /* SHA1M */
12139     case 3: /* SHA1SU0 */
12140         genfn = NULL;
12141         feature = ARM_FEATURE_V8_SHA1;
12142         break;
12143     case 4: /* SHA256H */
12144         genfn = gen_helper_crypto_sha256h;
12145         break;
12146     case 5: /* SHA256H2 */
12147         genfn = gen_helper_crypto_sha256h2;
12148         break;
12149     case 6: /* SHA256SU1 */
12150         genfn = gen_helper_crypto_sha256su1;
12151         break;
12152     default:
12153         unallocated_encoding(s);
12154         return;
12155     }
12156
12157     if (!arm_dc_feature(s, feature)) {
12158         unallocated_encoding(s);
12159         return;
12160     }
12161
12162     if (!fp_access_check(s)) {
12163         return;
12164     }
12165
12166     tcg_rd_ptr = vec_full_reg_ptr(s, rd);
12167     tcg_rn_ptr = vec_full_reg_ptr(s, rn);
12168     tcg_rm_ptr = vec_full_reg_ptr(s, rm);
12169
12170     if (genfn) {
12171         genfn(tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr);
12172     } else {
12173         TCGv_i32 tcg_opcode = tcg_const_i32(opcode);
12174
12175         gen_helper_crypto_sha1_3reg(tcg_rd_ptr, tcg_rn_ptr,
12176                                     tcg_rm_ptr, tcg_opcode);
12177         tcg_temp_free_i32(tcg_opcode);
12178     }
12179
12180     tcg_temp_free_ptr(tcg_rd_ptr);
12181     tcg_temp_free_ptr(tcg_rn_ptr);
12182     tcg_temp_free_ptr(tcg_rm_ptr);
12183 }
12184
12185 /* Crypto two-reg SHA
12186  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
12187  * +-----------------+------+-----------+--------+-----+------+------+
12188  * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
12189  * +-----------------+------+-----------+--------+-----+------+------+
12190  */
12191 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
12192 {
12193     int size = extract32(insn, 22, 2);
12194     int opcode = extract32(insn, 12, 5);
12195     int rn = extract32(insn, 5, 5);
12196     int rd = extract32(insn, 0, 5);
12197     CryptoTwoOpFn *genfn;
12198     int feature;
12199     TCGv_ptr tcg_rd_ptr, tcg_rn_ptr;
12200
12201     if (size != 0) {
12202         unallocated_encoding(s);
12203         return;
12204     }
12205
12206     switch (opcode) {
12207     case 0: /* SHA1H */
12208         feature = ARM_FEATURE_V8_SHA1;
12209         genfn = gen_helper_crypto_sha1h;
12210         break;
12211     case 1: /* SHA1SU1 */
12212         feature = ARM_FEATURE_V8_SHA1;
12213         genfn = gen_helper_crypto_sha1su1;
12214         break;
12215     case 2: /* SHA256SU0 */
12216         feature = ARM_FEATURE_V8_SHA256;
12217         genfn = gen_helper_crypto_sha256su0;
12218         break;
12219     default:
12220         unallocated_encoding(s);
12221         return;
12222     }
12223
12224     if (!arm_dc_feature(s, feature)) {
12225         unallocated_encoding(s);
12226         return;
12227     }
12228
12229     if (!fp_access_check(s)) {
12230         return;
12231     }
12232
12233     tcg_rd_ptr = vec_full_reg_ptr(s, rd);
12234     tcg_rn_ptr = vec_full_reg_ptr(s, rn);
12235
12236     genfn(tcg_rd_ptr, tcg_rn_ptr);
12237
12238     tcg_temp_free_ptr(tcg_rd_ptr);
12239     tcg_temp_free_ptr(tcg_rn_ptr);
12240 }
12241
12242 /* Crypto three-reg SHA512
12243  *  31                   21 20  16 15  14  13 12  11  10  9    5 4    0
12244  * +-----------------------+------+---+---+-----+--------+------+------+
12245  * | 1 1 0 0 1 1 1 0 0 1 1 |  Rm  | 1 | O | 0 0 | opcode |  Rn  |  Rd  |
12246  * +-----------------------+------+---+---+-----+--------+------+------+
12247  */
12248 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
12249 {
12250     int opcode = extract32(insn, 10, 2);
12251     int o =  extract32(insn, 14, 1);
12252     int rm = extract32(insn, 16, 5);
12253     int rn = extract32(insn, 5, 5);
12254     int rd = extract32(insn, 0, 5);
12255     int feature;
12256     CryptoThreeOpFn *genfn;
12257
12258     if (o == 0) {
12259         switch (opcode) {
12260         case 0: /* SHA512H */
12261             feature = ARM_FEATURE_V8_SHA512;
12262             genfn = gen_helper_crypto_sha512h;
12263             break;
12264         case 1: /* SHA512H2 */
12265             feature = ARM_FEATURE_V8_SHA512;
12266             genfn = gen_helper_crypto_sha512h2;
12267             break;
12268         case 2: /* SHA512SU1 */
12269             feature = ARM_FEATURE_V8_SHA512;
12270             genfn = gen_helper_crypto_sha512su1;
12271             break;
12272         case 3: /* RAX1 */
12273             feature = ARM_FEATURE_V8_SHA3;
12274             genfn = NULL;
12275             break;
12276         }
12277     } else {
12278         switch (opcode) {
12279         case 0: /* SM3PARTW1 */
12280             feature = ARM_FEATURE_V8_SM3;
12281             genfn = gen_helper_crypto_sm3partw1;
12282             break;
12283         case 1: /* SM3PARTW2 */
12284             feature = ARM_FEATURE_V8_SM3;
12285             genfn = gen_helper_crypto_sm3partw2;
12286             break;
12287         case 2: /* SM4EKEY */
12288             feature = ARM_FEATURE_V8_SM4;
12289             genfn = gen_helper_crypto_sm4ekey;
12290             break;
12291         default:
12292             unallocated_encoding(s);
12293             return;
12294         }
12295     }
12296
12297     if (!arm_dc_feature(s, feature)) {
12298         unallocated_encoding(s);
12299         return;
12300     }
12301
12302     if (!fp_access_check(s)) {
12303         return;
12304     }
12305
12306     if (genfn) {
12307         TCGv_ptr tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr;
12308
12309         tcg_rd_ptr = vec_full_reg_ptr(s, rd);
12310         tcg_rn_ptr = vec_full_reg_ptr(s, rn);
12311         tcg_rm_ptr = vec_full_reg_ptr(s, rm);
12312
12313         genfn(tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr);
12314
12315         tcg_temp_free_ptr(tcg_rd_ptr);
12316         tcg_temp_free_ptr(tcg_rn_ptr);
12317         tcg_temp_free_ptr(tcg_rm_ptr);
12318     } else {
12319         TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
12320         int pass;
12321
12322         tcg_op1 = tcg_temp_new_i64();
12323         tcg_op2 = tcg_temp_new_i64();
12324         tcg_res[0] = tcg_temp_new_i64();
12325         tcg_res[1] = tcg_temp_new_i64();
12326
12327         for (pass = 0; pass < 2; pass++) {
12328             read_vec_element(s, tcg_op1, rn, pass, MO_64);
12329             read_vec_element(s, tcg_op2, rm, pass, MO_64);
12330
12331             tcg_gen_rotli_i64(tcg_res[pass], tcg_op2, 1);
12332             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12333         }
12334         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
12335         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
12336
12337         tcg_temp_free_i64(tcg_op1);
12338         tcg_temp_free_i64(tcg_op2);
12339         tcg_temp_free_i64(tcg_res[0]);
12340         tcg_temp_free_i64(tcg_res[1]);
12341     }
12342 }
12343
12344 /* Crypto two-reg SHA512
12345  *  31                                     12  11  10  9    5 4    0
12346  * +-----------------------------------------+--------+------+------+
12347  * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode |  Rn  |  Rd  |
12348  * +-----------------------------------------+--------+------+------+
12349  */
12350 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
12351 {
12352     int opcode = extract32(insn, 10, 2);
12353     int rn = extract32(insn, 5, 5);
12354     int rd = extract32(insn, 0, 5);
12355     TCGv_ptr tcg_rd_ptr, tcg_rn_ptr;
12356     int feature;
12357     CryptoTwoOpFn *genfn;
12358
12359     switch (opcode) {
12360     case 0: /* SHA512SU0 */
12361         feature = ARM_FEATURE_V8_SHA512;
12362         genfn = gen_helper_crypto_sha512su0;
12363         break;
12364     case 1: /* SM4E */
12365         feature = ARM_FEATURE_V8_SM4;
12366         genfn = gen_helper_crypto_sm4e;
12367         break;
12368     default:
12369         unallocated_encoding(s);
12370         return;
12371     }
12372
12373     if (!arm_dc_feature(s, feature)) {
12374         unallocated_encoding(s);
12375         return;
12376     }
12377
12378     if (!fp_access_check(s)) {
12379         return;
12380     }
12381
12382     tcg_rd_ptr = vec_full_reg_ptr(s, rd);
12383     tcg_rn_ptr = vec_full_reg_ptr(s, rn);
12384
12385     genfn(tcg_rd_ptr, tcg_rn_ptr);
12386
12387     tcg_temp_free_ptr(tcg_rd_ptr);
12388     tcg_temp_free_ptr(tcg_rn_ptr);
12389 }
12390
12391 /* Crypto four-register
12392  *  31               23 22 21 20  16 15  14  10 9    5 4    0
12393  * +-------------------+-----+------+---+------+------+------+
12394  * | 1 1 0 0 1 1 1 0 0 | Op0 |  Rm  | 0 |  Ra  |  Rn  |  Rd  |
12395  * +-------------------+-----+------+---+------+------+------+
12396  */
12397 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
12398 {
12399     int op0 = extract32(insn, 21, 2);
12400     int rm = extract32(insn, 16, 5);
12401     int ra = extract32(insn, 10, 5);
12402     int rn = extract32(insn, 5, 5);
12403     int rd = extract32(insn, 0, 5);
12404     int feature;
12405
12406     switch (op0) {
12407     case 0: /* EOR3 */
12408     case 1: /* BCAX */
12409         feature = ARM_FEATURE_V8_SHA3;
12410         break;
12411     case 2: /* SM3SS1 */
12412         feature = ARM_FEATURE_V8_SM3;
12413         break;
12414     default:
12415         unallocated_encoding(s);
12416         return;
12417     }
12418
12419     if (!arm_dc_feature(s, feature)) {
12420         unallocated_encoding(s);
12421         return;
12422     }
12423
12424     if (!fp_access_check(s)) {
12425         return;
12426     }
12427
12428     if (op0 < 2) {
12429         TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
12430         int pass;
12431
12432         tcg_op1 = tcg_temp_new_i64();
12433         tcg_op2 = tcg_temp_new_i64();
12434         tcg_op3 = tcg_temp_new_i64();
12435         tcg_res[0] = tcg_temp_new_i64();
12436         tcg_res[1] = tcg_temp_new_i64();
12437
12438         for (pass = 0; pass < 2; pass++) {
12439             read_vec_element(s, tcg_op1, rn, pass, MO_64);
12440             read_vec_element(s, tcg_op2, rm, pass, MO_64);
12441             read_vec_element(s, tcg_op3, ra, pass, MO_64);
12442
12443             if (op0 == 0) {
12444                 /* EOR3 */
12445                 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
12446             } else {
12447                 /* BCAX */
12448                 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
12449             }
12450             tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12451         }
12452         write_vec_element(s, tcg_res[0], rd, 0, MO_64);
12453         write_vec_element(s, tcg_res[1], rd, 1, MO_64);
12454
12455         tcg_temp_free_i64(tcg_op1);
12456         tcg_temp_free_i64(tcg_op2);
12457         tcg_temp_free_i64(tcg_op3);
12458         tcg_temp_free_i64(tcg_res[0]);
12459         tcg_temp_free_i64(tcg_res[1]);
12460     } else {
12461         TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
12462
12463         tcg_op1 = tcg_temp_new_i32();
12464         tcg_op2 = tcg_temp_new_i32();
12465         tcg_op3 = tcg_temp_new_i32();
12466         tcg_res = tcg_temp_new_i32();
12467         tcg_zero = tcg_const_i32(0);
12468
12469         read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
12470         read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
12471         read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
12472
12473         tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
12474         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
12475         tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
12476         tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
12477
12478         write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
12479         write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
12480         write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
12481         write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
12482
12483         tcg_temp_free_i32(tcg_op1);
12484         tcg_temp_free_i32(tcg_op2);
12485         tcg_temp_free_i32(tcg_op3);
12486         tcg_temp_free_i32(tcg_res);
12487         tcg_temp_free_i32(tcg_zero);
12488     }
12489 }
12490
12491 /* Crypto XAR
12492  *  31                   21 20  16 15    10 9    5 4    0
12493  * +-----------------------+------+--------+------+------+
12494  * | 1 1 0 0 1 1 1 0 1 0 0 |  Rm  |  imm6  |  Rn  |  Rd  |
12495  * +-----------------------+------+--------+------+------+
12496  */
12497 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
12498 {
12499     int rm = extract32(insn, 16, 5);
12500     int imm6 = extract32(insn, 10, 6);
12501     int rn = extract32(insn, 5, 5);
12502     int rd = extract32(insn, 0, 5);
12503     TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
12504     int pass;
12505
12506     if (!arm_dc_feature(s, ARM_FEATURE_V8_SHA3)) {
12507         unallocated_encoding(s);
12508         return;
12509     }
12510
12511     if (!fp_access_check(s)) {
12512         return;
12513     }
12514
12515     tcg_op1 = tcg_temp_new_i64();
12516     tcg_op2 = tcg_temp_new_i64();
12517     tcg_res[0] = tcg_temp_new_i64();
12518     tcg_res[1] = tcg_temp_new_i64();
12519
12520     for (pass = 0; pass < 2; pass++) {
12521         read_vec_element(s, tcg_op1, rn, pass, MO_64);
12522         read_vec_element(s, tcg_op2, rm, pass, MO_64);
12523
12524         tcg_gen_xor_i64(tcg_res[pass], tcg_op1, tcg_op2);
12525         tcg_gen_rotri_i64(tcg_res[pass], tcg_res[pass], imm6);
12526     }
12527     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
12528     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
12529
12530     tcg_temp_free_i64(tcg_op1);
12531     tcg_temp_free_i64(tcg_op2);
12532     tcg_temp_free_i64(tcg_res[0]);
12533     tcg_temp_free_i64(tcg_res[1]);
12534 }
12535
12536 /* Crypto three-reg imm2
12537  *  31                   21 20  16 15  14 13 12  11  10  9    5 4    0
12538  * +-----------------------+------+-----+------+--------+------+------+
12539  * | 1 1 0 0 1 1 1 0 0 1 0 |  Rm  | 1 0 | imm2 | opcode |  Rn  |  Rd  |
12540  * +-----------------------+------+-----+------+--------+------+------+
12541  */
12542 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
12543 {
12544     int opcode = extract32(insn, 10, 2);
12545     int imm2 = extract32(insn, 12, 2);
12546     int rm = extract32(insn, 16, 5);
12547     int rn = extract32(insn, 5, 5);
12548     int rd = extract32(insn, 0, 5);
12549     TCGv_ptr tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr;
12550     TCGv_i32 tcg_imm2, tcg_opcode;
12551
12552     if (!arm_dc_feature(s, ARM_FEATURE_V8_SM3)) {
12553         unallocated_encoding(s);
12554         return;
12555     }
12556
12557     if (!fp_access_check(s)) {
12558         return;
12559     }
12560
12561     tcg_rd_ptr = vec_full_reg_ptr(s, rd);
12562     tcg_rn_ptr = vec_full_reg_ptr(s, rn);
12563     tcg_rm_ptr = vec_full_reg_ptr(s, rm);
12564     tcg_imm2   = tcg_const_i32(imm2);
12565     tcg_opcode = tcg_const_i32(opcode);
12566
12567     gen_helper_crypto_sm3tt(tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr, tcg_imm2,
12568                             tcg_opcode);
12569
12570     tcg_temp_free_ptr(tcg_rd_ptr);
12571     tcg_temp_free_ptr(tcg_rn_ptr);
12572     tcg_temp_free_ptr(tcg_rm_ptr);
12573     tcg_temp_free_i32(tcg_imm2);
12574     tcg_temp_free_i32(tcg_opcode);
12575 }
12576
12577 /* C3.6 Data processing - SIMD, inc Crypto
12578  *
12579  * As the decode gets a little complex we are using a table based
12580  * approach for this part of the decode.
12581  */
12582 static const AArch64DecodeTable data_proc_simd[] = {
12583     /* pattern  ,  mask     ,  fn                        */
12584     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
12585     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
12586     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
12587     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
12588     { 0x0e000400, 0x9fe08400, disas_simd_copy },
12589     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
12590     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
12591     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
12592     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
12593     { 0x0e000000, 0xbf208c00, disas_simd_tb },
12594     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
12595     { 0x2e000000, 0xbf208400, disas_simd_ext },
12596     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
12597     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
12598     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
12599     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
12600     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
12601     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
12602     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
12603     { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
12604     { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
12605     { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
12606     { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
12607     { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
12608     { 0xce000000, 0xff808000, disas_crypto_four_reg },
12609     { 0xce800000, 0xffe00000, disas_crypto_xar },
12610     { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
12611     { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
12612     { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
12613     { 0x00000000, 0x00000000, NULL }
12614 };
12615
12616 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
12617 {
12618     /* Note that this is called with all non-FP cases from
12619      * table C3-6 so it must UNDEF for entries not specifically
12620      * allocated to instructions in that table.
12621      */
12622     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
12623     if (fn) {
12624         fn(s, insn);
12625     } else {
12626         unallocated_encoding(s);
12627     }
12628 }
12629
12630 /* C3.6 Data processing - SIMD and floating point */
12631 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
12632 {
12633     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
12634         disas_data_proc_fp(s, insn);
12635     } else {
12636         /* SIMD, including crypto */
12637         disas_data_proc_simd(s, insn);
12638     }
12639 }
12640
12641 /* C3.1 A64 instruction index by encoding */
12642 static void disas_a64_insn(CPUARMState *env, DisasContext *s)
12643 {
12644     uint32_t insn;
12645
12646     insn = arm_ldl_code(env, s->pc, s->sctlr_b);
12647     s->insn = insn;
12648     s->pc += 4;
12649
12650     s->fp_access_checked = false;
12651
12652     switch (extract32(insn, 25, 4)) {
12653     case 0x0: case 0x1: case 0x2: case 0x3: /* UNALLOCATED */
12654         unallocated_encoding(s);
12655         break;
12656     case 0x8: case 0x9: /* Data processing - immediate */
12657         disas_data_proc_imm(s, insn);
12658         break;
12659     case 0xa: case 0xb: /* Branch, exception generation and system insns */
12660         disas_b_exc_sys(s, insn);
12661         break;
12662     case 0x4:
12663     case 0x6:
12664     case 0xc:
12665     case 0xe:      /* Loads and stores */
12666         disas_ldst(s, insn);
12667         break;
12668     case 0x5:
12669     case 0xd:      /* Data processing - register */
12670         disas_data_proc_reg(s, insn);
12671         break;
12672     case 0x7:
12673     case 0xf:      /* Data processing - SIMD and floating point */
12674         disas_data_proc_simd_fp(s, insn);
12675         break;
12676     default:
12677         assert(FALSE); /* all 15 cases should be handled above */
12678         break;
12679     }
12680
12681     /* if we allocated any temporaries, free them here */
12682     free_tmp_a64(s);
12683 }
12684
12685 static int aarch64_tr_init_disas_context(DisasContextBase *dcbase,
12686                                          CPUState *cpu, int max_insns)
12687 {
12688     DisasContext *dc = container_of(dcbase, DisasContext, base);
12689     CPUARMState *env = cpu->env_ptr;
12690     ARMCPU *arm_cpu = arm_env_get_cpu(env);
12691     int bound;
12692
12693     dc->pc = dc->base.pc_first;
12694     dc->condjmp = 0;
12695
12696     dc->aarch64 = 1;
12697     /* If we are coming from secure EL0 in a system with a 32-bit EL3, then
12698      * there is no secure EL1, so we route exceptions to EL3.
12699      */
12700     dc->secure_routed_to_el3 = arm_feature(env, ARM_FEATURE_EL3) &&
12701                                !arm_el_is_aa64(env, 3);
12702     dc->thumb = 0;
12703     dc->sctlr_b = 0;
12704     dc->be_data = ARM_TBFLAG_BE_DATA(dc->base.tb->flags) ? MO_BE : MO_LE;
12705     dc->condexec_mask = 0;
12706     dc->condexec_cond = 0;
12707     dc->mmu_idx = core_to_arm_mmu_idx(env, ARM_TBFLAG_MMUIDX(dc->base.tb->flags));
12708     dc->tbi0 = ARM_TBFLAG_TBI0(dc->base.tb->flags);
12709     dc->tbi1 = ARM_TBFLAG_TBI1(dc->base.tb->flags);
12710     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
12711 #if !defined(CONFIG_USER_ONLY)
12712     dc->user = (dc->current_el == 0);
12713 #endif
12714     dc->fp_excp_el = ARM_TBFLAG_FPEXC_EL(dc->base.tb->flags);
12715     dc->sve_excp_el = ARM_TBFLAG_SVEEXC_EL(dc->base.tb->flags);
12716     dc->sve_len = (ARM_TBFLAG_ZCR_LEN(dc->base.tb->flags) + 1) * 16;
12717     dc->vec_len = 0;
12718     dc->vec_stride = 0;
12719     dc->cp_regs = arm_cpu->cp_regs;
12720     dc->features = env->features;
12721
12722     /* Single step state. The code-generation logic here is:
12723      *  SS_ACTIVE == 0:
12724      *   generate code with no special handling for single-stepping (except
12725      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
12726      *   this happens anyway because those changes are all system register or
12727      *   PSTATE writes).
12728      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
12729      *   emit code for one insn
12730      *   emit code to clear PSTATE.SS
12731      *   emit code to generate software step exception for completed step
12732      *   end TB (as usual for having generated an exception)
12733      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
12734      *   emit code to generate a software step exception
12735      *   end the TB
12736      */
12737     dc->ss_active = ARM_TBFLAG_SS_ACTIVE(dc->base.tb->flags);
12738     dc->pstate_ss = ARM_TBFLAG_PSTATE_SS(dc->base.tb->flags);
12739     dc->is_ldex = false;
12740     dc->ss_same_el = (arm_debug_target_el(env) == dc->current_el);
12741
12742     /* Bound the number of insns to execute to those left on the page.  */
12743     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
12744
12745     /* If architectural single step active, limit to 1.  */
12746     if (dc->ss_active) {
12747         bound = 1;
12748     }
12749     max_insns = MIN(max_insns, bound);
12750
12751     init_tmp_a64_array(dc);
12752
12753     return max_insns;
12754 }
12755
12756 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
12757 {
12758     tcg_clear_temp_count();
12759 }
12760
12761 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
12762 {
12763     DisasContext *dc = container_of(dcbase, DisasContext, base);
12764
12765     tcg_gen_insn_start(dc->pc, 0, 0);
12766     dc->insn_start = tcg_last_op();
12767 }
12768
12769 static bool aarch64_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,
12770                                         const CPUBreakpoint *bp)
12771 {
12772     DisasContext *dc = container_of(dcbase, DisasContext, base);
12773
12774     if (bp->flags & BP_CPU) {
12775         gen_a64_set_pc_im(dc->pc);
12776         gen_helper_check_breakpoints(cpu_env);
12777         /* End the TB early; it likely won't be executed */
12778         dc->base.is_jmp = DISAS_TOO_MANY;
12779     } else {
12780         gen_exception_internal_insn(dc, 0, EXCP_DEBUG);
12781         /* The address covered by the breakpoint must be
12782            included in [tb->pc, tb->pc + tb->size) in order
12783            to for it to be properly cleared -- thus we
12784            increment the PC here so that the logic setting
12785            tb->size below does the right thing.  */
12786         dc->pc += 4;
12787         dc->base.is_jmp = DISAS_NORETURN;
12788     }
12789
12790     return true;
12791 }
12792
12793 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
12794 {
12795     DisasContext *dc = container_of(dcbase, DisasContext, base);
12796     CPUARMState *env = cpu->env_ptr;
12797
12798     if (dc->ss_active && !dc->pstate_ss) {
12799         /* Singlestep state is Active-pending.
12800          * If we're in this state at the start of a TB then either
12801          *  a) we just took an exception to an EL which is being debugged
12802          *     and this is the first insn in the exception handler
12803          *  b) debug exceptions were masked and we just unmasked them
12804          *     without changing EL (eg by clearing PSTATE.D)
12805          * In either case we're going to take a swstep exception in the
12806          * "did not step an insn" case, and so the syndrome ISV and EX
12807          * bits should be zero.
12808          */
12809         assert(dc->base.num_insns == 1);
12810         gen_exception(EXCP_UDEF, syn_swstep(dc->ss_same_el, 0, 0),
12811                       default_exception_el(dc));
12812         dc->base.is_jmp = DISAS_NORETURN;
12813     } else {
12814         disas_a64_insn(env, dc);
12815     }
12816
12817     dc->base.pc_next = dc->pc;
12818     translator_loop_temp_check(&dc->base);
12819 }
12820
12821 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
12822 {
12823     DisasContext *dc = container_of(dcbase, DisasContext, base);
12824
12825     if (unlikely(dc->base.singlestep_enabled || dc->ss_active)) {
12826         /* Note that this means single stepping WFI doesn't halt the CPU.
12827          * For conditional branch insns this is harmless unreachable code as
12828          * gen_goto_tb() has already handled emitting the debug exception
12829          * (and thus a tb-jump is not possible when singlestepping).
12830          */
12831         switch (dc->base.is_jmp) {
12832         default:
12833             gen_a64_set_pc_im(dc->pc);
12834             /* fall through */
12835         case DISAS_EXIT:
12836         case DISAS_JUMP:
12837             if (dc->base.singlestep_enabled) {
12838                 gen_exception_internal(EXCP_DEBUG);
12839             } else {
12840                 gen_step_complete_exception(dc);
12841             }
12842             break;
12843         case DISAS_NORETURN:
12844             break;
12845         }
12846     } else {
12847         switch (dc->base.is_jmp) {
12848         case DISAS_NEXT:
12849         case DISAS_TOO_MANY:
12850             gen_goto_tb(dc, 1, dc->pc);
12851             break;
12852         default:
12853         case DISAS_UPDATE:
12854             gen_a64_set_pc_im(dc->pc);
12855             /* fall through */
12856         case DISAS_JUMP:
12857             tcg_gen_lookup_and_goto_ptr();
12858             break;
12859         case DISAS_EXIT:
12860             tcg_gen_exit_tb(0);
12861             break;
12862         case DISAS_NORETURN:
12863         case DISAS_SWI:
12864             break;
12865         case DISAS_WFE:
12866             gen_a64_set_pc_im(dc->pc);
12867             gen_helper_wfe(cpu_env);
12868             break;
12869         case DISAS_YIELD:
12870             gen_a64_set_pc_im(dc->pc);
12871             gen_helper_yield(cpu_env);
12872             break;
12873         case DISAS_WFI:
12874         {
12875             /* This is a special case because we don't want to just halt the CPU
12876              * if trying to debug across a WFI.
12877              */
12878             TCGv_i32 tmp = tcg_const_i32(4);
12879
12880             gen_a64_set_pc_im(dc->pc);
12881             gen_helper_wfi(cpu_env, tmp);
12882             tcg_temp_free_i32(tmp);
12883             /* The helper doesn't necessarily throw an exception, but we
12884              * must go back to the main loop to check for interrupts anyway.
12885              */
12886             tcg_gen_exit_tb(0);
12887             break;
12888         }
12889         }
12890     }
12891
12892     /* Functions above can change dc->pc, so re-align db->pc_next */
12893     dc->base.pc_next = dc->pc;
12894 }
12895
12896 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
12897                                       CPUState *cpu)
12898 {
12899     DisasContext *dc = container_of(dcbase, DisasContext, base);
12900
12901     qemu_log("IN: %s\n", lookup_symbol(dc->base.pc_first));
12902     log_target_disas(cpu, dc->base.pc_first, dc->base.tb->size);
12903 }
12904
12905 const TranslatorOps aarch64_translator_ops = {
12906     .init_disas_context = aarch64_tr_init_disas_context,
12907     .tb_start           = aarch64_tr_tb_start,
12908     .insn_start         = aarch64_tr_insn_start,
12909     .breakpoint_check   = aarch64_tr_breakpoint_check,
12910     .translate_insn     = aarch64_tr_translate_insn,
12911     .tb_stop            = aarch64_tr_tb_stop,
12912     .disas_log          = aarch64_tr_disas_log,
12913 };
This page took 0.704136 seconds and 4 git commands to generate.