]> Git Repo - qemu.git/blob - target/arm/translate-a64.c
Merge remote-tracking branch 'remotes/cohuck/tags/s390x-20171109' into staging
[qemu.git] / target / arm / translate-a64.c
1 /*
2  *  AArch64 translation
3  *
4  *  Copyright (c) 2013 Alexander Graf <[email protected]>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "qemu/osdep.h"
20
21 #include "cpu.h"
22 #include "exec/exec-all.h"
23 #include "tcg-op.h"
24 #include "qemu/log.h"
25 #include "arm_ldst.h"
26 #include "translate.h"
27 #include "internals.h"
28 #include "qemu/host-utils.h"
29
30 #include "exec/semihost.h"
31 #include "exec/gen-icount.h"
32
33 #include "exec/helper-proto.h"
34 #include "exec/helper-gen.h"
35 #include "exec/log.h"
36
37 #include "trace-tcg.h"
38
39 static TCGv_i64 cpu_X[32];
40 static TCGv_i64 cpu_pc;
41
42 /* Load/store exclusive handling */
43 static TCGv_i64 cpu_exclusive_high;
44 static TCGv_i64 cpu_reg(DisasContext *s, int reg);
45
46 static const char *regnames[] = {
47     "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
48     "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
49     "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
50     "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
51 };
52
53 enum a64_shift_type {
54     A64_SHIFT_TYPE_LSL = 0,
55     A64_SHIFT_TYPE_LSR = 1,
56     A64_SHIFT_TYPE_ASR = 2,
57     A64_SHIFT_TYPE_ROR = 3
58 };
59
60 /* Table based decoder typedefs - used when the relevant bits for decode
61  * are too awkwardly scattered across the instruction (eg SIMD).
62  */
63 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
64
65 typedef struct AArch64DecodeTable {
66     uint32_t pattern;
67     uint32_t mask;
68     AArch64DecodeFn *disas_fn;
69 } AArch64DecodeTable;
70
71 /* Function prototype for gen_ functions for calling Neon helpers */
72 typedef void NeonGenOneOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32);
73 typedef void NeonGenTwoOpFn(TCGv_i32, TCGv_i32, TCGv_i32);
74 typedef void NeonGenTwoOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32, TCGv_i32);
75 typedef void NeonGenTwo64OpFn(TCGv_i64, TCGv_i64, TCGv_i64);
76 typedef void NeonGenTwo64OpEnvFn(TCGv_i64, TCGv_ptr, TCGv_i64, TCGv_i64);
77 typedef void NeonGenNarrowFn(TCGv_i32, TCGv_i64);
78 typedef void NeonGenNarrowEnvFn(TCGv_i32, TCGv_ptr, TCGv_i64);
79 typedef void NeonGenWidenFn(TCGv_i64, TCGv_i32);
80 typedef void NeonGenTwoSingleOPFn(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
81 typedef void NeonGenTwoDoubleOPFn(TCGv_i64, TCGv_i64, TCGv_i64, TCGv_ptr);
82 typedef void NeonGenOneOpFn(TCGv_i64, TCGv_i64);
83 typedef void CryptoTwoOpEnvFn(TCGv_ptr, TCGv_i32, TCGv_i32);
84 typedef void CryptoThreeOpEnvFn(TCGv_ptr, TCGv_i32, TCGv_i32, TCGv_i32);
85
86 /* initialize TCG globals.  */
87 void a64_translate_init(void)
88 {
89     int i;
90
91     cpu_pc = tcg_global_mem_new_i64(cpu_env,
92                                     offsetof(CPUARMState, pc),
93                                     "pc");
94     for (i = 0; i < 32; i++) {
95         cpu_X[i] = tcg_global_mem_new_i64(cpu_env,
96                                           offsetof(CPUARMState, xregs[i]),
97                                           regnames[i]);
98     }
99
100     cpu_exclusive_high = tcg_global_mem_new_i64(cpu_env,
101         offsetof(CPUARMState, exclusive_high), "exclusive_high");
102 }
103
104 static inline int get_a64_user_mem_index(DisasContext *s)
105 {
106     /* Return the core mmu_idx to use for A64 "unprivileged load/store" insns:
107      *  if EL1, access as if EL0; otherwise access at current EL
108      */
109     ARMMMUIdx useridx;
110
111     switch (s->mmu_idx) {
112     case ARMMMUIdx_S12NSE1:
113         useridx = ARMMMUIdx_S12NSE0;
114         break;
115     case ARMMMUIdx_S1SE1:
116         useridx = ARMMMUIdx_S1SE0;
117         break;
118     case ARMMMUIdx_S2NS:
119         g_assert_not_reached();
120     default:
121         useridx = s->mmu_idx;
122         break;
123     }
124     return arm_to_core_mmu_idx(useridx);
125 }
126
127 void aarch64_cpu_dump_state(CPUState *cs, FILE *f,
128                             fprintf_function cpu_fprintf, int flags)
129 {
130     ARMCPU *cpu = ARM_CPU(cs);
131     CPUARMState *env = &cpu->env;
132     uint32_t psr = pstate_read(env);
133     int i;
134     int el = arm_current_el(env);
135     const char *ns_status;
136
137     cpu_fprintf(f, "PC=%016"PRIx64"  SP=%016"PRIx64"\n",
138             env->pc, env->xregs[31]);
139     for (i = 0; i < 31; i++) {
140         cpu_fprintf(f, "X%02d=%016"PRIx64, i, env->xregs[i]);
141         if ((i % 4) == 3) {
142             cpu_fprintf(f, "\n");
143         } else {
144             cpu_fprintf(f, " ");
145         }
146     }
147
148     if (arm_feature(env, ARM_FEATURE_EL3) && el != 3) {
149         ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
150     } else {
151         ns_status = "";
152     }
153
154     cpu_fprintf(f, "\nPSTATE=%08x %c%c%c%c %sEL%d%c\n",
155                 psr,
156                 psr & PSTATE_N ? 'N' : '-',
157                 psr & PSTATE_Z ? 'Z' : '-',
158                 psr & PSTATE_C ? 'C' : '-',
159                 psr & PSTATE_V ? 'V' : '-',
160                 ns_status,
161                 el,
162                 psr & PSTATE_SP ? 'h' : 't');
163
164     if (flags & CPU_DUMP_FPU) {
165         int numvfpregs = 32;
166         for (i = 0; i < numvfpregs; i += 2) {
167             uint64_t vlo = float64_val(env->vfp.regs[i * 2]);
168             uint64_t vhi = float64_val(env->vfp.regs[(i * 2) + 1]);
169             cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 " ",
170                         i, vhi, vlo);
171             vlo = float64_val(env->vfp.regs[(i + 1) * 2]);
172             vhi = float64_val(env->vfp.regs[((i + 1) * 2) + 1]);
173             cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 "\n",
174                         i + 1, vhi, vlo);
175         }
176         cpu_fprintf(f, "FPCR: %08x  FPSR: %08x\n",
177                     vfp_get_fpcr(env), vfp_get_fpsr(env));
178     }
179 }
180
181 void gen_a64_set_pc_im(uint64_t val)
182 {
183     tcg_gen_movi_i64(cpu_pc, val);
184 }
185
186 /* Load the PC from a generic TCG variable.
187  *
188  * If address tagging is enabled via the TCR TBI bits, then loading
189  * an address into the PC will clear out any tag in the it:
190  *  + for EL2 and EL3 there is only one TBI bit, and if it is set
191  *    then the address is zero-extended, clearing bits [63:56]
192  *  + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
193  *    and TBI1 controls addressses with bit 55 == 1.
194  *    If the appropriate TBI bit is set for the address then
195  *    the address is sign-extended from bit 55 into bits [63:56]
196  *
197  * We can avoid doing this for relative-branches, because the
198  * PC + offset can never overflow into the tag bits (assuming
199  * that virtual addresses are less than 56 bits wide, as they
200  * are currently), but we must handle it for branch-to-register.
201  */
202 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
203 {
204
205     if (s->current_el <= 1) {
206         /* Test if NEITHER or BOTH TBI values are set.  If so, no need to
207          * examine bit 55 of address, can just generate code.
208          * If mixed, then test via generated code
209          */
210         if (s->tbi0 && s->tbi1) {
211             TCGv_i64 tmp_reg = tcg_temp_new_i64();
212             /* Both bits set, sign extension from bit 55 into [63:56] will
213              * cover both cases
214              */
215             tcg_gen_shli_i64(tmp_reg, src, 8);
216             tcg_gen_sari_i64(cpu_pc, tmp_reg, 8);
217             tcg_temp_free_i64(tmp_reg);
218         } else if (!s->tbi0 && !s->tbi1) {
219             /* Neither bit set, just load it as-is */
220             tcg_gen_mov_i64(cpu_pc, src);
221         } else {
222             TCGv_i64 tcg_tmpval = tcg_temp_new_i64();
223             TCGv_i64 tcg_bit55  = tcg_temp_new_i64();
224             TCGv_i64 tcg_zero   = tcg_const_i64(0);
225
226             tcg_gen_andi_i64(tcg_bit55, src, (1ull << 55));
227
228             if (s->tbi0) {
229                 /* tbi0==1, tbi1==0, so 0-fill upper byte if bit 55 = 0 */
230                 tcg_gen_andi_i64(tcg_tmpval, src,
231                                  0x00FFFFFFFFFFFFFFull);
232                 tcg_gen_movcond_i64(TCG_COND_EQ, cpu_pc, tcg_bit55, tcg_zero,
233                                     tcg_tmpval, src);
234             } else {
235                 /* tbi0==0, tbi1==1, so 1-fill upper byte if bit 55 = 1 */
236                 tcg_gen_ori_i64(tcg_tmpval, src,
237                                 0xFF00000000000000ull);
238                 tcg_gen_movcond_i64(TCG_COND_NE, cpu_pc, tcg_bit55, tcg_zero,
239                                     tcg_tmpval, src);
240             }
241             tcg_temp_free_i64(tcg_zero);
242             tcg_temp_free_i64(tcg_bit55);
243             tcg_temp_free_i64(tcg_tmpval);
244         }
245     } else {  /* EL > 1 */
246         if (s->tbi0) {
247             /* Force tag byte to all zero */
248             tcg_gen_andi_i64(cpu_pc, src, 0x00FFFFFFFFFFFFFFull);
249         } else {
250             /* Load unmodified address */
251             tcg_gen_mov_i64(cpu_pc, src);
252         }
253     }
254 }
255
256 typedef struct DisasCompare64 {
257     TCGCond cond;
258     TCGv_i64 value;
259 } DisasCompare64;
260
261 static void a64_test_cc(DisasCompare64 *c64, int cc)
262 {
263     DisasCompare c32;
264
265     arm_test_cc(&c32, cc);
266
267     /* Sign-extend the 32-bit value so that the GE/LT comparisons work
268        * properly.  The NE/EQ comparisons are also fine with this choice.  */
269     c64->cond = c32.cond;
270     c64->value = tcg_temp_new_i64();
271     tcg_gen_ext_i32_i64(c64->value, c32.value);
272
273     arm_free_cc(&c32);
274 }
275
276 static void a64_free_cc(DisasCompare64 *c64)
277 {
278     tcg_temp_free_i64(c64->value);
279 }
280
281 static void gen_exception_internal(int excp)
282 {
283     TCGv_i32 tcg_excp = tcg_const_i32(excp);
284
285     assert(excp_is_internal(excp));
286     gen_helper_exception_internal(cpu_env, tcg_excp);
287     tcg_temp_free_i32(tcg_excp);
288 }
289
290 static void gen_exception(int excp, uint32_t syndrome, uint32_t target_el)
291 {
292     TCGv_i32 tcg_excp = tcg_const_i32(excp);
293     TCGv_i32 tcg_syn = tcg_const_i32(syndrome);
294     TCGv_i32 tcg_el = tcg_const_i32(target_el);
295
296     gen_helper_exception_with_syndrome(cpu_env, tcg_excp,
297                                        tcg_syn, tcg_el);
298     tcg_temp_free_i32(tcg_el);
299     tcg_temp_free_i32(tcg_syn);
300     tcg_temp_free_i32(tcg_excp);
301 }
302
303 static void gen_exception_internal_insn(DisasContext *s, int offset, int excp)
304 {
305     gen_a64_set_pc_im(s->pc - offset);
306     gen_exception_internal(excp);
307     s->base.is_jmp = DISAS_NORETURN;
308 }
309
310 static void gen_exception_insn(DisasContext *s, int offset, int excp,
311                                uint32_t syndrome, uint32_t target_el)
312 {
313     gen_a64_set_pc_im(s->pc - offset);
314     gen_exception(excp, syndrome, target_el);
315     s->base.is_jmp = DISAS_NORETURN;
316 }
317
318 static void gen_ss_advance(DisasContext *s)
319 {
320     /* If the singlestep state is Active-not-pending, advance to
321      * Active-pending.
322      */
323     if (s->ss_active) {
324         s->pstate_ss = 0;
325         gen_helper_clear_pstate_ss(cpu_env);
326     }
327 }
328
329 static void gen_step_complete_exception(DisasContext *s)
330 {
331     /* We just completed step of an insn. Move from Active-not-pending
332      * to Active-pending, and then also take the swstep exception.
333      * This corresponds to making the (IMPDEF) choice to prioritize
334      * swstep exceptions over asynchronous exceptions taken to an exception
335      * level where debug is disabled. This choice has the advantage that
336      * we do not need to maintain internal state corresponding to the
337      * ISV/EX syndrome bits between completion of the step and generation
338      * of the exception, and our syndrome information is always correct.
339      */
340     gen_ss_advance(s);
341     gen_exception(EXCP_UDEF, syn_swstep(s->ss_same_el, 1, s->is_ldex),
342                   default_exception_el(s));
343     s->base.is_jmp = DISAS_NORETURN;
344 }
345
346 static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
347 {
348     /* No direct tb linking with singlestep (either QEMU's or the ARM
349      * debug architecture kind) or deterministic io
350      */
351     if (s->base.singlestep_enabled || s->ss_active ||
352         (tb_cflags(s->base.tb) & CF_LAST_IO)) {
353         return false;
354     }
355
356 #ifndef CONFIG_USER_ONLY
357     /* Only link tbs from inside the same guest page */
358     if ((s->base.tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
359         return false;
360     }
361 #endif
362
363     return true;
364 }
365
366 static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
367 {
368     TranslationBlock *tb;
369
370     tb = s->base.tb;
371     if (use_goto_tb(s, n, dest)) {
372         tcg_gen_goto_tb(n);
373         gen_a64_set_pc_im(dest);
374         tcg_gen_exit_tb((intptr_t)tb + n);
375         s->base.is_jmp = DISAS_NORETURN;
376     } else {
377         gen_a64_set_pc_im(dest);
378         if (s->ss_active) {
379             gen_step_complete_exception(s);
380         } else if (s->base.singlestep_enabled) {
381             gen_exception_internal(EXCP_DEBUG);
382         } else {
383             tcg_gen_lookup_and_goto_ptr();
384             s->base.is_jmp = DISAS_NORETURN;
385         }
386     }
387 }
388
389 static void unallocated_encoding(DisasContext *s)
390 {
391     /* Unallocated and reserved encodings are uncategorized */
392     gen_exception_insn(s, 4, EXCP_UDEF, syn_uncategorized(),
393                        default_exception_el(s));
394 }
395
396 #define unsupported_encoding(s, insn)                                    \
397     do {                                                                 \
398         qemu_log_mask(LOG_UNIMP,                                         \
399                       "%s:%d: unsupported instruction encoding 0x%08x "  \
400                       "at pc=%016" PRIx64 "\n",                          \
401                       __FILE__, __LINE__, insn, s->pc - 4);              \
402         unallocated_encoding(s);                                         \
403     } while (0);
404
405 static void init_tmp_a64_array(DisasContext *s)
406 {
407 #ifdef CONFIG_DEBUG_TCG
408     int i;
409     for (i = 0; i < ARRAY_SIZE(s->tmp_a64); i++) {
410         TCGV_UNUSED_I64(s->tmp_a64[i]);
411     }
412 #endif
413     s->tmp_a64_count = 0;
414 }
415
416 static void free_tmp_a64(DisasContext *s)
417 {
418     int i;
419     for (i = 0; i < s->tmp_a64_count; i++) {
420         tcg_temp_free_i64(s->tmp_a64[i]);
421     }
422     init_tmp_a64_array(s);
423 }
424
425 static TCGv_i64 new_tmp_a64(DisasContext *s)
426 {
427     assert(s->tmp_a64_count < TMP_A64_MAX);
428     return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
429 }
430
431 static TCGv_i64 new_tmp_a64_zero(DisasContext *s)
432 {
433     TCGv_i64 t = new_tmp_a64(s);
434     tcg_gen_movi_i64(t, 0);
435     return t;
436 }
437
438 /*
439  * Register access functions
440  *
441  * These functions are used for directly accessing a register in where
442  * changes to the final register value are likely to be made. If you
443  * need to use a register for temporary calculation (e.g. index type
444  * operations) use the read_* form.
445  *
446  * B1.2.1 Register mappings
447  *
448  * In instruction register encoding 31 can refer to ZR (zero register) or
449  * the SP (stack pointer) depending on context. In QEMU's case we map SP
450  * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
451  * This is the point of the _sp forms.
452  */
453 static TCGv_i64 cpu_reg(DisasContext *s, int reg)
454 {
455     if (reg == 31) {
456         return new_tmp_a64_zero(s);
457     } else {
458         return cpu_X[reg];
459     }
460 }
461
462 /* register access for when 31 == SP */
463 static TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
464 {
465     return cpu_X[reg];
466 }
467
468 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
469  * representing the register contents. This TCGv is an auto-freed
470  * temporary so it need not be explicitly freed, and may be modified.
471  */
472 static TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
473 {
474     TCGv_i64 v = new_tmp_a64(s);
475     if (reg != 31) {
476         if (sf) {
477             tcg_gen_mov_i64(v, cpu_X[reg]);
478         } else {
479             tcg_gen_ext32u_i64(v, cpu_X[reg]);
480         }
481     } else {
482         tcg_gen_movi_i64(v, 0);
483     }
484     return v;
485 }
486
487 static TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
488 {
489     TCGv_i64 v = new_tmp_a64(s);
490     if (sf) {
491         tcg_gen_mov_i64(v, cpu_X[reg]);
492     } else {
493         tcg_gen_ext32u_i64(v, cpu_X[reg]);
494     }
495     return v;
496 }
497
498 /* We should have at some point before trying to access an FP register
499  * done the necessary access check, so assert that
500  * (a) we did the check and
501  * (b) we didn't then just plough ahead anyway if it failed.
502  * Print the instruction pattern in the abort message so we can figure
503  * out what we need to fix if a user encounters this problem in the wild.
504  */
505 static inline void assert_fp_access_checked(DisasContext *s)
506 {
507 #ifdef CONFIG_DEBUG_TCG
508     if (unlikely(!s->fp_access_checked || s->fp_excp_el)) {
509         fprintf(stderr, "target-arm: FP access check missing for "
510                 "instruction 0x%08x\n", s->insn);
511         abort();
512     }
513 #endif
514 }
515
516 /* Return the offset into CPUARMState of an element of specified
517  * size, 'element' places in from the least significant end of
518  * the FP/vector register Qn.
519  */
520 static inline int vec_reg_offset(DisasContext *s, int regno,
521                                  int element, TCGMemOp size)
522 {
523     int offs = 0;
524 #ifdef HOST_WORDS_BIGENDIAN
525     /* This is complicated slightly because vfp.regs[2n] is
526      * still the low half and  vfp.regs[2n+1] the high half
527      * of the 128 bit vector, even on big endian systems.
528      * Calculate the offset assuming a fully bigendian 128 bits,
529      * then XOR to account for the order of the two 64 bit halves.
530      */
531     offs += (16 - ((element + 1) * (1 << size)));
532     offs ^= 8;
533 #else
534     offs += element * (1 << size);
535 #endif
536     offs += offsetof(CPUARMState, vfp.regs[regno * 2]);
537     assert_fp_access_checked(s);
538     return offs;
539 }
540
541 /* Return the offset into CPUARMState of a slice (from
542  * the least significant end) of FP register Qn (ie
543  * Dn, Sn, Hn or Bn).
544  * (Note that this is not the same mapping as for A32; see cpu.h)
545  */
546 static inline int fp_reg_offset(DisasContext *s, int regno, TCGMemOp size)
547 {
548     int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
549 #ifdef HOST_WORDS_BIGENDIAN
550     offs += (8 - (1 << size));
551 #endif
552     assert_fp_access_checked(s);
553     return offs;
554 }
555
556 /* Offset of the high half of the 128 bit vector Qn */
557 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
558 {
559     assert_fp_access_checked(s);
560     return offsetof(CPUARMState, vfp.regs[regno * 2 + 1]);
561 }
562
563 /* Convenience accessors for reading and writing single and double
564  * FP registers. Writing clears the upper parts of the associated
565  * 128 bit vector register, as required by the architecture.
566  * Note that unlike the GP register accessors, the values returned
567  * by the read functions must be manually freed.
568  */
569 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
570 {
571     TCGv_i64 v = tcg_temp_new_i64();
572
573     tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
574     return v;
575 }
576
577 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
578 {
579     TCGv_i32 v = tcg_temp_new_i32();
580
581     tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
582     return v;
583 }
584
585 static void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
586 {
587     TCGv_i64 tcg_zero = tcg_const_i64(0);
588
589     tcg_gen_st_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
590     tcg_gen_st_i64(tcg_zero, cpu_env, fp_reg_hi_offset(s, reg));
591     tcg_temp_free_i64(tcg_zero);
592 }
593
594 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
595 {
596     TCGv_i64 tmp = tcg_temp_new_i64();
597
598     tcg_gen_extu_i32_i64(tmp, v);
599     write_fp_dreg(s, reg, tmp);
600     tcg_temp_free_i64(tmp);
601 }
602
603 static TCGv_ptr get_fpstatus_ptr(void)
604 {
605     TCGv_ptr statusptr = tcg_temp_new_ptr();
606     int offset;
607
608     /* In A64 all instructions (both FP and Neon) use the FPCR;
609      * there is no equivalent of the A32 Neon "standard FPSCR value"
610      * and all operations use vfp.fp_status.
611      */
612     offset = offsetof(CPUARMState, vfp.fp_status);
613     tcg_gen_addi_ptr(statusptr, cpu_env, offset);
614     return statusptr;
615 }
616
617 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
618  * than the 32 bit equivalent.
619  */
620 static inline void gen_set_NZ64(TCGv_i64 result)
621 {
622     tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
623     tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
624 }
625
626 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
627 static inline void gen_logic_CC(int sf, TCGv_i64 result)
628 {
629     if (sf) {
630         gen_set_NZ64(result);
631     } else {
632         tcg_gen_extrl_i64_i32(cpu_ZF, result);
633         tcg_gen_mov_i32(cpu_NF, cpu_ZF);
634     }
635     tcg_gen_movi_i32(cpu_CF, 0);
636     tcg_gen_movi_i32(cpu_VF, 0);
637 }
638
639 /* dest = T0 + T1; compute C, N, V and Z flags */
640 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
641 {
642     if (sf) {
643         TCGv_i64 result, flag, tmp;
644         result = tcg_temp_new_i64();
645         flag = tcg_temp_new_i64();
646         tmp = tcg_temp_new_i64();
647
648         tcg_gen_movi_i64(tmp, 0);
649         tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
650
651         tcg_gen_extrl_i64_i32(cpu_CF, flag);
652
653         gen_set_NZ64(result);
654
655         tcg_gen_xor_i64(flag, result, t0);
656         tcg_gen_xor_i64(tmp, t0, t1);
657         tcg_gen_andc_i64(flag, flag, tmp);
658         tcg_temp_free_i64(tmp);
659         tcg_gen_extrh_i64_i32(cpu_VF, flag);
660
661         tcg_gen_mov_i64(dest, result);
662         tcg_temp_free_i64(result);
663         tcg_temp_free_i64(flag);
664     } else {
665         /* 32 bit arithmetic */
666         TCGv_i32 t0_32 = tcg_temp_new_i32();
667         TCGv_i32 t1_32 = tcg_temp_new_i32();
668         TCGv_i32 tmp = tcg_temp_new_i32();
669
670         tcg_gen_movi_i32(tmp, 0);
671         tcg_gen_extrl_i64_i32(t0_32, t0);
672         tcg_gen_extrl_i64_i32(t1_32, t1);
673         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
674         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
675         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
676         tcg_gen_xor_i32(tmp, t0_32, t1_32);
677         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
678         tcg_gen_extu_i32_i64(dest, cpu_NF);
679
680         tcg_temp_free_i32(tmp);
681         tcg_temp_free_i32(t0_32);
682         tcg_temp_free_i32(t1_32);
683     }
684 }
685
686 /* dest = T0 - T1; compute C, N, V and Z flags */
687 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
688 {
689     if (sf) {
690         /* 64 bit arithmetic */
691         TCGv_i64 result, flag, tmp;
692
693         result = tcg_temp_new_i64();
694         flag = tcg_temp_new_i64();
695         tcg_gen_sub_i64(result, t0, t1);
696
697         gen_set_NZ64(result);
698
699         tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
700         tcg_gen_extrl_i64_i32(cpu_CF, flag);
701
702         tcg_gen_xor_i64(flag, result, t0);
703         tmp = tcg_temp_new_i64();
704         tcg_gen_xor_i64(tmp, t0, t1);
705         tcg_gen_and_i64(flag, flag, tmp);
706         tcg_temp_free_i64(tmp);
707         tcg_gen_extrh_i64_i32(cpu_VF, flag);
708         tcg_gen_mov_i64(dest, result);
709         tcg_temp_free_i64(flag);
710         tcg_temp_free_i64(result);
711     } else {
712         /* 32 bit arithmetic */
713         TCGv_i32 t0_32 = tcg_temp_new_i32();
714         TCGv_i32 t1_32 = tcg_temp_new_i32();
715         TCGv_i32 tmp;
716
717         tcg_gen_extrl_i64_i32(t0_32, t0);
718         tcg_gen_extrl_i64_i32(t1_32, t1);
719         tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
720         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
721         tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
722         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
723         tmp = tcg_temp_new_i32();
724         tcg_gen_xor_i32(tmp, t0_32, t1_32);
725         tcg_temp_free_i32(t0_32);
726         tcg_temp_free_i32(t1_32);
727         tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
728         tcg_temp_free_i32(tmp);
729         tcg_gen_extu_i32_i64(dest, cpu_NF);
730     }
731 }
732
733 /* dest = T0 + T1 + CF; do not compute flags. */
734 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
735 {
736     TCGv_i64 flag = tcg_temp_new_i64();
737     tcg_gen_extu_i32_i64(flag, cpu_CF);
738     tcg_gen_add_i64(dest, t0, t1);
739     tcg_gen_add_i64(dest, dest, flag);
740     tcg_temp_free_i64(flag);
741
742     if (!sf) {
743         tcg_gen_ext32u_i64(dest, dest);
744     }
745 }
746
747 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
748 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
749 {
750     if (sf) {
751         TCGv_i64 result, cf_64, vf_64, tmp;
752         result = tcg_temp_new_i64();
753         cf_64 = tcg_temp_new_i64();
754         vf_64 = tcg_temp_new_i64();
755         tmp = tcg_const_i64(0);
756
757         tcg_gen_extu_i32_i64(cf_64, cpu_CF);
758         tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
759         tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
760         tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
761         gen_set_NZ64(result);
762
763         tcg_gen_xor_i64(vf_64, result, t0);
764         tcg_gen_xor_i64(tmp, t0, t1);
765         tcg_gen_andc_i64(vf_64, vf_64, tmp);
766         tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
767
768         tcg_gen_mov_i64(dest, result);
769
770         tcg_temp_free_i64(tmp);
771         tcg_temp_free_i64(vf_64);
772         tcg_temp_free_i64(cf_64);
773         tcg_temp_free_i64(result);
774     } else {
775         TCGv_i32 t0_32, t1_32, tmp;
776         t0_32 = tcg_temp_new_i32();
777         t1_32 = tcg_temp_new_i32();
778         tmp = tcg_const_i32(0);
779
780         tcg_gen_extrl_i64_i32(t0_32, t0);
781         tcg_gen_extrl_i64_i32(t1_32, t1);
782         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
783         tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
784
785         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
786         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
787         tcg_gen_xor_i32(tmp, t0_32, t1_32);
788         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
789         tcg_gen_extu_i32_i64(dest, cpu_NF);
790
791         tcg_temp_free_i32(tmp);
792         tcg_temp_free_i32(t1_32);
793         tcg_temp_free_i32(t0_32);
794     }
795 }
796
797 /*
798  * Load/Store generators
799  */
800
801 /*
802  * Store from GPR register to memory.
803  */
804 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
805                              TCGv_i64 tcg_addr, int size, int memidx,
806                              bool iss_valid,
807                              unsigned int iss_srt,
808                              bool iss_sf, bool iss_ar)
809 {
810     g_assert(size <= 3);
811     tcg_gen_qemu_st_i64(source, tcg_addr, memidx, s->be_data + size);
812
813     if (iss_valid) {
814         uint32_t syn;
815
816         syn = syn_data_abort_with_iss(0,
817                                       size,
818                                       false,
819                                       iss_srt,
820                                       iss_sf,
821                                       iss_ar,
822                                       0, 0, 0, 0, 0, false);
823         disas_set_insn_syndrome(s, syn);
824     }
825 }
826
827 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
828                       TCGv_i64 tcg_addr, int size,
829                       bool iss_valid,
830                       unsigned int iss_srt,
831                       bool iss_sf, bool iss_ar)
832 {
833     do_gpr_st_memidx(s, source, tcg_addr, size, get_mem_index(s),
834                      iss_valid, iss_srt, iss_sf, iss_ar);
835 }
836
837 /*
838  * Load from memory to GPR register
839  */
840 static void do_gpr_ld_memidx(DisasContext *s,
841                              TCGv_i64 dest, TCGv_i64 tcg_addr,
842                              int size, bool is_signed,
843                              bool extend, int memidx,
844                              bool iss_valid, unsigned int iss_srt,
845                              bool iss_sf, bool iss_ar)
846 {
847     TCGMemOp memop = s->be_data + size;
848
849     g_assert(size <= 3);
850
851     if (is_signed) {
852         memop += MO_SIGN;
853     }
854
855     tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
856
857     if (extend && is_signed) {
858         g_assert(size < 3);
859         tcg_gen_ext32u_i64(dest, dest);
860     }
861
862     if (iss_valid) {
863         uint32_t syn;
864
865         syn = syn_data_abort_with_iss(0,
866                                       size,
867                                       is_signed,
868                                       iss_srt,
869                                       iss_sf,
870                                       iss_ar,
871                                       0, 0, 0, 0, 0, false);
872         disas_set_insn_syndrome(s, syn);
873     }
874 }
875
876 static void do_gpr_ld(DisasContext *s,
877                       TCGv_i64 dest, TCGv_i64 tcg_addr,
878                       int size, bool is_signed, bool extend,
879                       bool iss_valid, unsigned int iss_srt,
880                       bool iss_sf, bool iss_ar)
881 {
882     do_gpr_ld_memidx(s, dest, tcg_addr, size, is_signed, extend,
883                      get_mem_index(s),
884                      iss_valid, iss_srt, iss_sf, iss_ar);
885 }
886
887 /*
888  * Store from FP register to memory
889  */
890 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
891 {
892     /* This writes the bottom N bits of a 128 bit wide vector to memory */
893     TCGv_i64 tmp = tcg_temp_new_i64();
894     tcg_gen_ld_i64(tmp, cpu_env, fp_reg_offset(s, srcidx, MO_64));
895     if (size < 4) {
896         tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s),
897                             s->be_data + size);
898     } else {
899         bool be = s->be_data == MO_BE;
900         TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
901
902         tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
903         tcg_gen_qemu_st_i64(tmp, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
904                             s->be_data | MO_Q);
905         tcg_gen_ld_i64(tmp, cpu_env, fp_reg_hi_offset(s, srcidx));
906         tcg_gen_qemu_st_i64(tmp, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
907                             s->be_data | MO_Q);
908         tcg_temp_free_i64(tcg_hiaddr);
909     }
910
911     tcg_temp_free_i64(tmp);
912 }
913
914 /*
915  * Load from memory to FP register
916  */
917 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
918 {
919     /* This always zero-extends and writes to a full 128 bit wide vector */
920     TCGv_i64 tmplo = tcg_temp_new_i64();
921     TCGv_i64 tmphi;
922
923     if (size < 4) {
924         TCGMemOp memop = s->be_data + size;
925         tmphi = tcg_const_i64(0);
926         tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
927     } else {
928         bool be = s->be_data == MO_BE;
929         TCGv_i64 tcg_hiaddr;
930
931         tmphi = tcg_temp_new_i64();
932         tcg_hiaddr = tcg_temp_new_i64();
933
934         tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
935         tcg_gen_qemu_ld_i64(tmplo, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
936                             s->be_data | MO_Q);
937         tcg_gen_qemu_ld_i64(tmphi, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
938                             s->be_data | MO_Q);
939         tcg_temp_free_i64(tcg_hiaddr);
940     }
941
942     tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
943     tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
944
945     tcg_temp_free_i64(tmplo);
946     tcg_temp_free_i64(tmphi);
947 }
948
949 /*
950  * Vector load/store helpers.
951  *
952  * The principal difference between this and a FP load is that we don't
953  * zero extend as we are filling a partial chunk of the vector register.
954  * These functions don't support 128 bit loads/stores, which would be
955  * normal load/store operations.
956  *
957  * The _i32 versions are useful when operating on 32 bit quantities
958  * (eg for floating point single or using Neon helper functions).
959  */
960
961 /* Get value of an element within a vector register */
962 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
963                              int element, TCGMemOp memop)
964 {
965     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
966     switch (memop) {
967     case MO_8:
968         tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
969         break;
970     case MO_16:
971         tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
972         break;
973     case MO_32:
974         tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
975         break;
976     case MO_8|MO_SIGN:
977         tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
978         break;
979     case MO_16|MO_SIGN:
980         tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
981         break;
982     case MO_32|MO_SIGN:
983         tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
984         break;
985     case MO_64:
986     case MO_64|MO_SIGN:
987         tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
988         break;
989     default:
990         g_assert_not_reached();
991     }
992 }
993
994 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
995                                  int element, TCGMemOp memop)
996 {
997     int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
998     switch (memop) {
999     case MO_8:
1000         tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1001         break;
1002     case MO_16:
1003         tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1004         break;
1005     case MO_8|MO_SIGN:
1006         tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1007         break;
1008     case MO_16|MO_SIGN:
1009         tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1010         break;
1011     case MO_32:
1012     case MO_32|MO_SIGN:
1013         tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1014         break;
1015     default:
1016         g_assert_not_reached();
1017     }
1018 }
1019
1020 /* Set value of an element within a vector register */
1021 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1022                               int element, TCGMemOp memop)
1023 {
1024     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1025     switch (memop) {
1026     case MO_8:
1027         tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1028         break;
1029     case MO_16:
1030         tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1031         break;
1032     case MO_32:
1033         tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1034         break;
1035     case MO_64:
1036         tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1037         break;
1038     default:
1039         g_assert_not_reached();
1040     }
1041 }
1042
1043 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1044                                   int destidx, int element, TCGMemOp memop)
1045 {
1046     int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1047     switch (memop) {
1048     case MO_8:
1049         tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1050         break;
1051     case MO_16:
1052         tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1053         break;
1054     case MO_32:
1055         tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1056         break;
1057     default:
1058         g_assert_not_reached();
1059     }
1060 }
1061
1062 /* Clear the high 64 bits of a 128 bit vector (in general non-quad
1063  * vector ops all need to do this).
1064  */
1065 static void clear_vec_high(DisasContext *s, int rd)
1066 {
1067     TCGv_i64 tcg_zero = tcg_const_i64(0);
1068
1069     write_vec_element(s, tcg_zero, rd, 1, MO_64);
1070     tcg_temp_free_i64(tcg_zero);
1071 }
1072
1073 /* Store from vector register to memory */
1074 static void do_vec_st(DisasContext *s, int srcidx, int element,
1075                       TCGv_i64 tcg_addr, int size)
1076 {
1077     TCGMemOp memop = s->be_data + size;
1078     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1079
1080     read_vec_element(s, tcg_tmp, srcidx, element, size);
1081     tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
1082
1083     tcg_temp_free_i64(tcg_tmp);
1084 }
1085
1086 /* Load from memory to vector register */
1087 static void do_vec_ld(DisasContext *s, int destidx, int element,
1088                       TCGv_i64 tcg_addr, int size)
1089 {
1090     TCGMemOp memop = s->be_data + size;
1091     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1092
1093     tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
1094     write_vec_element(s, tcg_tmp, destidx, element, size);
1095
1096     tcg_temp_free_i64(tcg_tmp);
1097 }
1098
1099 /* Check that FP/Neon access is enabled. If it is, return
1100  * true. If not, emit code to generate an appropriate exception,
1101  * and return false; the caller should not emit any code for
1102  * the instruction. Note that this check must happen after all
1103  * unallocated-encoding checks (otherwise the syndrome information
1104  * for the resulting exception will be incorrect).
1105  */
1106 static inline bool fp_access_check(DisasContext *s)
1107 {
1108     assert(!s->fp_access_checked);
1109     s->fp_access_checked = true;
1110
1111     if (!s->fp_excp_el) {
1112         return true;
1113     }
1114
1115     gen_exception_insn(s, 4, EXCP_UDEF, syn_fp_access_trap(1, 0xe, false),
1116                        s->fp_excp_el);
1117     return false;
1118 }
1119
1120 /*
1121  * This utility function is for doing register extension with an
1122  * optional shift. You will likely want to pass a temporary for the
1123  * destination register. See DecodeRegExtend() in the ARM ARM.
1124  */
1125 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1126                               int option, unsigned int shift)
1127 {
1128     int extsize = extract32(option, 0, 2);
1129     bool is_signed = extract32(option, 2, 1);
1130
1131     if (is_signed) {
1132         switch (extsize) {
1133         case 0:
1134             tcg_gen_ext8s_i64(tcg_out, tcg_in);
1135             break;
1136         case 1:
1137             tcg_gen_ext16s_i64(tcg_out, tcg_in);
1138             break;
1139         case 2:
1140             tcg_gen_ext32s_i64(tcg_out, tcg_in);
1141             break;
1142         case 3:
1143             tcg_gen_mov_i64(tcg_out, tcg_in);
1144             break;
1145         }
1146     } else {
1147         switch (extsize) {
1148         case 0:
1149             tcg_gen_ext8u_i64(tcg_out, tcg_in);
1150             break;
1151         case 1:
1152             tcg_gen_ext16u_i64(tcg_out, tcg_in);
1153             break;
1154         case 2:
1155             tcg_gen_ext32u_i64(tcg_out, tcg_in);
1156             break;
1157         case 3:
1158             tcg_gen_mov_i64(tcg_out, tcg_in);
1159             break;
1160         }
1161     }
1162
1163     if (shift) {
1164         tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1165     }
1166 }
1167
1168 static inline void gen_check_sp_alignment(DisasContext *s)
1169 {
1170     /* The AArch64 architecture mandates that (if enabled via PSTATE
1171      * or SCTLR bits) there is a check that SP is 16-aligned on every
1172      * SP-relative load or store (with an exception generated if it is not).
1173      * In line with general QEMU practice regarding misaligned accesses,
1174      * we omit these checks for the sake of guest program performance.
1175      * This function is provided as a hook so we can more easily add these
1176      * checks in future (possibly as a "favour catching guest program bugs
1177      * over speed" user selectable option).
1178      */
1179 }
1180
1181 /*
1182  * This provides a simple table based table lookup decoder. It is
1183  * intended to be used when the relevant bits for decode are too
1184  * awkwardly placed and switch/if based logic would be confusing and
1185  * deeply nested. Since it's a linear search through the table, tables
1186  * should be kept small.
1187  *
1188  * It returns the first handler where insn & mask == pattern, or
1189  * NULL if there is no match.
1190  * The table is terminated by an empty mask (i.e. 0)
1191  */
1192 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1193                                                uint32_t insn)
1194 {
1195     const AArch64DecodeTable *tptr = table;
1196
1197     while (tptr->mask) {
1198         if ((insn & tptr->mask) == tptr->pattern) {
1199             return tptr->disas_fn;
1200         }
1201         tptr++;
1202     }
1203     return NULL;
1204 }
1205
1206 /*
1207  * The instruction disassembly implemented here matches
1208  * the instruction encoding classifications in chapter C4
1209  * of the ARM Architecture Reference Manual (DDI0487B_a);
1210  * classification names and decode diagrams here should generally
1211  * match up with those in the manual.
1212  */
1213
1214 /* Unconditional branch (immediate)
1215  *   31  30       26 25                                  0
1216  * +----+-----------+-------------------------------------+
1217  * | op | 0 0 1 0 1 |                 imm26               |
1218  * +----+-----------+-------------------------------------+
1219  */
1220 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
1221 {
1222     uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
1223
1224     if (insn & (1U << 31)) {
1225         /* BL Branch with link */
1226         tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1227     }
1228
1229     /* B Branch / BL Branch with link */
1230     gen_goto_tb(s, 0, addr);
1231 }
1232
1233 /* Compare and branch (immediate)
1234  *   31  30         25  24  23                  5 4      0
1235  * +----+-------------+----+---------------------+--------+
1236  * | sf | 0 1 1 0 1 0 | op |         imm19       |   Rt   |
1237  * +----+-------------+----+---------------------+--------+
1238  */
1239 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
1240 {
1241     unsigned int sf, op, rt;
1242     uint64_t addr;
1243     TCGLabel *label_match;
1244     TCGv_i64 tcg_cmp;
1245
1246     sf = extract32(insn, 31, 1);
1247     op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
1248     rt = extract32(insn, 0, 5);
1249     addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1250
1251     tcg_cmp = read_cpu_reg(s, rt, sf);
1252     label_match = gen_new_label();
1253
1254     tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1255                         tcg_cmp, 0, label_match);
1256
1257     gen_goto_tb(s, 0, s->pc);
1258     gen_set_label(label_match);
1259     gen_goto_tb(s, 1, addr);
1260 }
1261
1262 /* Test and branch (immediate)
1263  *   31  30         25  24  23   19 18          5 4    0
1264  * +----+-------------+----+-------+-------------+------+
1265  * | b5 | 0 1 1 0 1 1 | op |  b40  |    imm14    |  Rt  |
1266  * +----+-------------+----+-------+-------------+------+
1267  */
1268 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1269 {
1270     unsigned int bit_pos, op, rt;
1271     uint64_t addr;
1272     TCGLabel *label_match;
1273     TCGv_i64 tcg_cmp;
1274
1275     bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1276     op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1277     addr = s->pc + sextract32(insn, 5, 14) * 4 - 4;
1278     rt = extract32(insn, 0, 5);
1279
1280     tcg_cmp = tcg_temp_new_i64();
1281     tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1282     label_match = gen_new_label();
1283     tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1284                         tcg_cmp, 0, label_match);
1285     tcg_temp_free_i64(tcg_cmp);
1286     gen_goto_tb(s, 0, s->pc);
1287     gen_set_label(label_match);
1288     gen_goto_tb(s, 1, addr);
1289 }
1290
1291 /* Conditional branch (immediate)
1292  *  31           25  24  23                  5   4  3    0
1293  * +---------------+----+---------------------+----+------+
1294  * | 0 1 0 1 0 1 0 | o1 |         imm19       | o0 | cond |
1295  * +---------------+----+---------------------+----+------+
1296  */
1297 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1298 {
1299     unsigned int cond;
1300     uint64_t addr;
1301
1302     if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1303         unallocated_encoding(s);
1304         return;
1305     }
1306     addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1307     cond = extract32(insn, 0, 4);
1308
1309     if (cond < 0x0e) {
1310         /* genuinely conditional branches */
1311         TCGLabel *label_match = gen_new_label();
1312         arm_gen_test_cc(cond, label_match);
1313         gen_goto_tb(s, 0, s->pc);
1314         gen_set_label(label_match);
1315         gen_goto_tb(s, 1, addr);
1316     } else {
1317         /* 0xe and 0xf are both "always" conditions */
1318         gen_goto_tb(s, 0, addr);
1319     }
1320 }
1321
1322 /* HINT instruction group, including various allocated HINTs */
1323 static void handle_hint(DisasContext *s, uint32_t insn,
1324                         unsigned int op1, unsigned int op2, unsigned int crm)
1325 {
1326     unsigned int selector = crm << 3 | op2;
1327
1328     if (op1 != 3) {
1329         unallocated_encoding(s);
1330         return;
1331     }
1332
1333     switch (selector) {
1334     case 0: /* NOP */
1335         return;
1336     case 3: /* WFI */
1337         s->base.is_jmp = DISAS_WFI;
1338         return;
1339         /* When running in MTTCG we don't generate jumps to the yield and
1340          * WFE helpers as it won't affect the scheduling of other vCPUs.
1341          * If we wanted to more completely model WFE/SEV so we don't busy
1342          * spin unnecessarily we would need to do something more involved.
1343          */
1344     case 1: /* YIELD */
1345         if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1346             s->base.is_jmp = DISAS_YIELD;
1347         }
1348         return;
1349     case 2: /* WFE */
1350         if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1351             s->base.is_jmp = DISAS_WFE;
1352         }
1353         return;
1354     case 4: /* SEV */
1355     case 5: /* SEVL */
1356         /* we treat all as NOP at least for now */
1357         return;
1358     default:
1359         /* default specified as NOP equivalent */
1360         return;
1361     }
1362 }
1363
1364 static void gen_clrex(DisasContext *s, uint32_t insn)
1365 {
1366     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1367 }
1368
1369 /* CLREX, DSB, DMB, ISB */
1370 static void handle_sync(DisasContext *s, uint32_t insn,
1371                         unsigned int op1, unsigned int op2, unsigned int crm)
1372 {
1373     TCGBar bar;
1374
1375     if (op1 != 3) {
1376         unallocated_encoding(s);
1377         return;
1378     }
1379
1380     switch (op2) {
1381     case 2: /* CLREX */
1382         gen_clrex(s, insn);
1383         return;
1384     case 4: /* DSB */
1385     case 5: /* DMB */
1386         switch (crm & 3) {
1387         case 1: /* MBReqTypes_Reads */
1388             bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1389             break;
1390         case 2: /* MBReqTypes_Writes */
1391             bar = TCG_BAR_SC | TCG_MO_ST_ST;
1392             break;
1393         default: /* MBReqTypes_All */
1394             bar = TCG_BAR_SC | TCG_MO_ALL;
1395             break;
1396         }
1397         tcg_gen_mb(bar);
1398         return;
1399     case 6: /* ISB */
1400         /* We need to break the TB after this insn to execute
1401          * a self-modified code correctly and also to take
1402          * any pending interrupts immediately.
1403          */
1404         gen_goto_tb(s, 0, s->pc);
1405         return;
1406     default:
1407         unallocated_encoding(s);
1408         return;
1409     }
1410 }
1411
1412 /* MSR (immediate) - move immediate to processor state field */
1413 static void handle_msr_i(DisasContext *s, uint32_t insn,
1414                          unsigned int op1, unsigned int op2, unsigned int crm)
1415 {
1416     int op = op1 << 3 | op2;
1417     switch (op) {
1418     case 0x05: /* SPSel */
1419         if (s->current_el == 0) {
1420             unallocated_encoding(s);
1421             return;
1422         }
1423         /* fall through */
1424     case 0x1e: /* DAIFSet */
1425     case 0x1f: /* DAIFClear */
1426     {
1427         TCGv_i32 tcg_imm = tcg_const_i32(crm);
1428         TCGv_i32 tcg_op = tcg_const_i32(op);
1429         gen_a64_set_pc_im(s->pc - 4);
1430         gen_helper_msr_i_pstate(cpu_env, tcg_op, tcg_imm);
1431         tcg_temp_free_i32(tcg_imm);
1432         tcg_temp_free_i32(tcg_op);
1433         /* For DAIFClear, exit the cpu loop to re-evaluate pending IRQs.  */
1434         gen_a64_set_pc_im(s->pc);
1435         s->base.is_jmp = (op == 0x1f ? DISAS_EXIT : DISAS_JUMP);
1436         break;
1437     }
1438     default:
1439         unallocated_encoding(s);
1440         return;
1441     }
1442 }
1443
1444 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1445 {
1446     TCGv_i32 tmp = tcg_temp_new_i32();
1447     TCGv_i32 nzcv = tcg_temp_new_i32();
1448
1449     /* build bit 31, N */
1450     tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
1451     /* build bit 30, Z */
1452     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1453     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1454     /* build bit 29, C */
1455     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1456     /* build bit 28, V */
1457     tcg_gen_shri_i32(tmp, cpu_VF, 31);
1458     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1459     /* generate result */
1460     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1461
1462     tcg_temp_free_i32(nzcv);
1463     tcg_temp_free_i32(tmp);
1464 }
1465
1466 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1467
1468 {
1469     TCGv_i32 nzcv = tcg_temp_new_i32();
1470
1471     /* take NZCV from R[t] */
1472     tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
1473
1474     /* bit 31, N */
1475     tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
1476     /* bit 30, Z */
1477     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1478     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1479     /* bit 29, C */
1480     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1481     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1482     /* bit 28, V */
1483     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1484     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1485     tcg_temp_free_i32(nzcv);
1486 }
1487
1488 /* MRS - move from system register
1489  * MSR (register) - move to system register
1490  * SYS
1491  * SYSL
1492  * These are all essentially the same insn in 'read' and 'write'
1493  * versions, with varying op0 fields.
1494  */
1495 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1496                        unsigned int op0, unsigned int op1, unsigned int op2,
1497                        unsigned int crn, unsigned int crm, unsigned int rt)
1498 {
1499     const ARMCPRegInfo *ri;
1500     TCGv_i64 tcg_rt;
1501
1502     ri = get_arm_cp_reginfo(s->cp_regs,
1503                             ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1504                                                crn, crm, op0, op1, op2));
1505
1506     if (!ri) {
1507         /* Unknown register; this might be a guest error or a QEMU
1508          * unimplemented feature.
1509          */
1510         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1511                       "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1512                       isread ? "read" : "write", op0, op1, crn, crm, op2);
1513         unallocated_encoding(s);
1514         return;
1515     }
1516
1517     /* Check access permissions */
1518     if (!cp_access_ok(s->current_el, ri, isread)) {
1519         unallocated_encoding(s);
1520         return;
1521     }
1522
1523     if (ri->accessfn) {
1524         /* Emit code to perform further access permissions checks at
1525          * runtime; this may result in an exception.
1526          */
1527         TCGv_ptr tmpptr;
1528         TCGv_i32 tcg_syn, tcg_isread;
1529         uint32_t syndrome;
1530
1531         gen_a64_set_pc_im(s->pc - 4);
1532         tmpptr = tcg_const_ptr(ri);
1533         syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1534         tcg_syn = tcg_const_i32(syndrome);
1535         tcg_isread = tcg_const_i32(isread);
1536         gen_helper_access_check_cp_reg(cpu_env, tmpptr, tcg_syn, tcg_isread);
1537         tcg_temp_free_ptr(tmpptr);
1538         tcg_temp_free_i32(tcg_syn);
1539         tcg_temp_free_i32(tcg_isread);
1540     }
1541
1542     /* Handle special cases first */
1543     switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
1544     case ARM_CP_NOP:
1545         return;
1546     case ARM_CP_NZCV:
1547         tcg_rt = cpu_reg(s, rt);
1548         if (isread) {
1549             gen_get_nzcv(tcg_rt);
1550         } else {
1551             gen_set_nzcv(tcg_rt);
1552         }
1553         return;
1554     case ARM_CP_CURRENTEL:
1555         /* Reads as current EL value from pstate, which is
1556          * guaranteed to be constant by the tb flags.
1557          */
1558         tcg_rt = cpu_reg(s, rt);
1559         tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
1560         return;
1561     case ARM_CP_DC_ZVA:
1562         /* Writes clear the aligned block of memory which rt points into. */
1563         tcg_rt = cpu_reg(s, rt);
1564         gen_helper_dc_zva(cpu_env, tcg_rt);
1565         return;
1566     default:
1567         break;
1568     }
1569
1570     if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1571         gen_io_start();
1572     }
1573
1574     tcg_rt = cpu_reg(s, rt);
1575
1576     if (isread) {
1577         if (ri->type & ARM_CP_CONST) {
1578             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1579         } else if (ri->readfn) {
1580             TCGv_ptr tmpptr;
1581             tmpptr = tcg_const_ptr(ri);
1582             gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1583             tcg_temp_free_ptr(tmpptr);
1584         } else {
1585             tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1586         }
1587     } else {
1588         if (ri->type & ARM_CP_CONST) {
1589             /* If not forbidden by access permissions, treat as WI */
1590             return;
1591         } else if (ri->writefn) {
1592             TCGv_ptr tmpptr;
1593             tmpptr = tcg_const_ptr(ri);
1594             gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1595             tcg_temp_free_ptr(tmpptr);
1596         } else {
1597             tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
1598         }
1599     }
1600
1601     if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1602         /* I/O operations must end the TB here (whether read or write) */
1603         gen_io_end();
1604         s->base.is_jmp = DISAS_UPDATE;
1605     } else if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
1606         /* We default to ending the TB on a coprocessor register write,
1607          * but allow this to be suppressed by the register definition
1608          * (usually only necessary to work around guest bugs).
1609          */
1610         s->base.is_jmp = DISAS_UPDATE;
1611     }
1612 }
1613
1614 /* System
1615  *  31                 22 21  20 19 18 16 15   12 11    8 7   5 4    0
1616  * +---------------------+---+-----+-----+-------+-------+-----+------+
1617  * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 |  CRn  |  CRm  | op2 |  Rt  |
1618  * +---------------------+---+-----+-----+-------+-------+-----+------+
1619  */
1620 static void disas_system(DisasContext *s, uint32_t insn)
1621 {
1622     unsigned int l, op0, op1, crn, crm, op2, rt;
1623     l = extract32(insn, 21, 1);
1624     op0 = extract32(insn, 19, 2);
1625     op1 = extract32(insn, 16, 3);
1626     crn = extract32(insn, 12, 4);
1627     crm = extract32(insn, 8, 4);
1628     op2 = extract32(insn, 5, 3);
1629     rt = extract32(insn, 0, 5);
1630
1631     if (op0 == 0) {
1632         if (l || rt != 31) {
1633             unallocated_encoding(s);
1634             return;
1635         }
1636         switch (crn) {
1637         case 2: /* HINT (including allocated hints like NOP, YIELD, etc) */
1638             handle_hint(s, insn, op1, op2, crm);
1639             break;
1640         case 3: /* CLREX, DSB, DMB, ISB */
1641             handle_sync(s, insn, op1, op2, crm);
1642             break;
1643         case 4: /* MSR (immediate) */
1644             handle_msr_i(s, insn, op1, op2, crm);
1645             break;
1646         default:
1647             unallocated_encoding(s);
1648             break;
1649         }
1650         return;
1651     }
1652     handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
1653 }
1654
1655 /* Exception generation
1656  *
1657  *  31             24 23 21 20                     5 4   2 1  0
1658  * +-----------------+-----+------------------------+-----+----+
1659  * | 1 1 0 1 0 1 0 0 | opc |          imm16         | op2 | LL |
1660  * +-----------------------+------------------------+----------+
1661  */
1662 static void disas_exc(DisasContext *s, uint32_t insn)
1663 {
1664     int opc = extract32(insn, 21, 3);
1665     int op2_ll = extract32(insn, 0, 5);
1666     int imm16 = extract32(insn, 5, 16);
1667     TCGv_i32 tmp;
1668
1669     switch (opc) {
1670     case 0:
1671         /* For SVC, HVC and SMC we advance the single-step state
1672          * machine before taking the exception. This is architecturally
1673          * mandated, to ensure that single-stepping a system call
1674          * instruction works properly.
1675          */
1676         switch (op2_ll) {
1677         case 1:                                                     /* SVC */
1678             gen_ss_advance(s);
1679             gen_exception_insn(s, 0, EXCP_SWI, syn_aa64_svc(imm16),
1680                                default_exception_el(s));
1681             break;
1682         case 2:                                                     /* HVC */
1683             if (s->current_el == 0) {
1684                 unallocated_encoding(s);
1685                 break;
1686             }
1687             /* The pre HVC helper handles cases when HVC gets trapped
1688              * as an undefined insn by runtime configuration.
1689              */
1690             gen_a64_set_pc_im(s->pc - 4);
1691             gen_helper_pre_hvc(cpu_env);
1692             gen_ss_advance(s);
1693             gen_exception_insn(s, 0, EXCP_HVC, syn_aa64_hvc(imm16), 2);
1694             break;
1695         case 3:                                                     /* SMC */
1696             if (s->current_el == 0) {
1697                 unallocated_encoding(s);
1698                 break;
1699             }
1700             gen_a64_set_pc_im(s->pc - 4);
1701             tmp = tcg_const_i32(syn_aa64_smc(imm16));
1702             gen_helper_pre_smc(cpu_env, tmp);
1703             tcg_temp_free_i32(tmp);
1704             gen_ss_advance(s);
1705             gen_exception_insn(s, 0, EXCP_SMC, syn_aa64_smc(imm16), 3);
1706             break;
1707         default:
1708             unallocated_encoding(s);
1709             break;
1710         }
1711         break;
1712     case 1:
1713         if (op2_ll != 0) {
1714             unallocated_encoding(s);
1715             break;
1716         }
1717         /* BRK */
1718         gen_exception_insn(s, 4, EXCP_BKPT, syn_aa64_bkpt(imm16),
1719                            default_exception_el(s));
1720         break;
1721     case 2:
1722         if (op2_ll != 0) {
1723             unallocated_encoding(s);
1724             break;
1725         }
1726         /* HLT. This has two purposes.
1727          * Architecturally, it is an external halting debug instruction.
1728          * Since QEMU doesn't implement external debug, we treat this as
1729          * it is required for halting debug disabled: it will UNDEF.
1730          * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
1731          */
1732         if (semihosting_enabled() && imm16 == 0xf000) {
1733 #ifndef CONFIG_USER_ONLY
1734             /* In system mode, don't allow userspace access to semihosting,
1735              * to provide some semblance of security (and for consistency
1736              * with our 32-bit semihosting).
1737              */
1738             if (s->current_el == 0) {
1739                 unsupported_encoding(s, insn);
1740                 break;
1741             }
1742 #endif
1743             gen_exception_internal_insn(s, 0, EXCP_SEMIHOST);
1744         } else {
1745             unsupported_encoding(s, insn);
1746         }
1747         break;
1748     case 5:
1749         if (op2_ll < 1 || op2_ll > 3) {
1750             unallocated_encoding(s);
1751             break;
1752         }
1753         /* DCPS1, DCPS2, DCPS3 */
1754         unsupported_encoding(s, insn);
1755         break;
1756     default:
1757         unallocated_encoding(s);
1758         break;
1759     }
1760 }
1761
1762 /* Unconditional branch (register)
1763  *  31           25 24   21 20   16 15   10 9    5 4     0
1764  * +---------------+-------+-------+-------+------+-------+
1765  * | 1 1 0 1 0 1 1 |  opc  |  op2  |  op3  |  Rn  |  op4  |
1766  * +---------------+-------+-------+-------+------+-------+
1767  */
1768 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
1769 {
1770     unsigned int opc, op2, op3, rn, op4;
1771
1772     opc = extract32(insn, 21, 4);
1773     op2 = extract32(insn, 16, 5);
1774     op3 = extract32(insn, 10, 6);
1775     rn = extract32(insn, 5, 5);
1776     op4 = extract32(insn, 0, 5);
1777
1778     if (op4 != 0x0 || op3 != 0x0 || op2 != 0x1f) {
1779         unallocated_encoding(s);
1780         return;
1781     }
1782
1783     switch (opc) {
1784     case 0: /* BR */
1785     case 1: /* BLR */
1786     case 2: /* RET */
1787         gen_a64_set_pc(s, cpu_reg(s, rn));
1788         /* BLR also needs to load return address */
1789         if (opc == 1) {
1790             tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1791         }
1792         break;
1793     case 4: /* ERET */
1794         if (s->current_el == 0) {
1795             unallocated_encoding(s);
1796             return;
1797         }
1798         gen_helper_exception_return(cpu_env);
1799         /* Must exit loop to check un-masked IRQs */
1800         s->base.is_jmp = DISAS_EXIT;
1801         return;
1802     case 5: /* DRPS */
1803         if (rn != 0x1f) {
1804             unallocated_encoding(s);
1805         } else {
1806             unsupported_encoding(s, insn);
1807         }
1808         return;
1809     default:
1810         unallocated_encoding(s);
1811         return;
1812     }
1813
1814     s->base.is_jmp = DISAS_JUMP;
1815 }
1816
1817 /* Branches, exception generating and system instructions */
1818 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
1819 {
1820     switch (extract32(insn, 25, 7)) {
1821     case 0x0a: case 0x0b:
1822     case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
1823         disas_uncond_b_imm(s, insn);
1824         break;
1825     case 0x1a: case 0x5a: /* Compare & branch (immediate) */
1826         disas_comp_b_imm(s, insn);
1827         break;
1828     case 0x1b: case 0x5b: /* Test & branch (immediate) */
1829         disas_test_b_imm(s, insn);
1830         break;
1831     case 0x2a: /* Conditional branch (immediate) */
1832         disas_cond_b_imm(s, insn);
1833         break;
1834     case 0x6a: /* Exception generation / System */
1835         if (insn & (1 << 24)) {
1836             disas_system(s, insn);
1837         } else {
1838             disas_exc(s, insn);
1839         }
1840         break;
1841     case 0x6b: /* Unconditional branch (register) */
1842         disas_uncond_b_reg(s, insn);
1843         break;
1844     default:
1845         unallocated_encoding(s);
1846         break;
1847     }
1848 }
1849
1850 /*
1851  * Load/Store exclusive instructions are implemented by remembering
1852  * the value/address loaded, and seeing if these are the same
1853  * when the store is performed. This is not actually the architecturally
1854  * mandated semantics, but it works for typical guest code sequences
1855  * and avoids having to monitor regular stores.
1856  *
1857  * The store exclusive uses the atomic cmpxchg primitives to avoid
1858  * races in multi-threaded linux-user and when MTTCG softmmu is
1859  * enabled.
1860  */
1861 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
1862                                TCGv_i64 addr, int size, bool is_pair)
1863 {
1864     int idx = get_mem_index(s);
1865     TCGMemOp memop = s->be_data;
1866
1867     g_assert(size <= 3);
1868     if (is_pair) {
1869         g_assert(size >= 2);
1870         if (size == 2) {
1871             /* The pair must be single-copy atomic for the doubleword.  */
1872             memop |= MO_64 | MO_ALIGN;
1873             tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
1874             if (s->be_data == MO_LE) {
1875                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
1876                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
1877             } else {
1878                 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
1879                 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
1880             }
1881         } else {
1882             /* The pair must be single-copy atomic for *each* doubleword, not
1883                the entire quadword, however it must be quadword aligned.  */
1884             memop |= MO_64;
1885             tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx,
1886                                 memop | MO_ALIGN_16);
1887
1888             TCGv_i64 addr2 = tcg_temp_new_i64();
1889             tcg_gen_addi_i64(addr2, addr, 8);
1890             tcg_gen_qemu_ld_i64(cpu_exclusive_high, addr2, idx, memop);
1891             tcg_temp_free_i64(addr2);
1892
1893             tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
1894             tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
1895         }
1896     } else {
1897         memop |= size | MO_ALIGN;
1898         tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
1899         tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
1900     }
1901     tcg_gen_mov_i64(cpu_exclusive_addr, addr);
1902 }
1903
1904 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
1905                                 TCGv_i64 addr, int size, int is_pair)
1906 {
1907     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
1908      *     && (!is_pair || env->exclusive_high == [addr + datasize])) {
1909      *     [addr] = {Rt};
1910      *     if (is_pair) {
1911      *         [addr + datasize] = {Rt2};
1912      *     }
1913      *     {Rd} = 0;
1914      * } else {
1915      *     {Rd} = 1;
1916      * }
1917      * env->exclusive_addr = -1;
1918      */
1919     TCGLabel *fail_label = gen_new_label();
1920     TCGLabel *done_label = gen_new_label();
1921     TCGv_i64 tmp;
1922
1923     tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
1924
1925     tmp = tcg_temp_new_i64();
1926     if (is_pair) {
1927         if (size == 2) {
1928             if (s->be_data == MO_LE) {
1929                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
1930             } else {
1931                 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
1932             }
1933             tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
1934                                        cpu_exclusive_val, tmp,
1935                                        get_mem_index(s),
1936                                        MO_64 | MO_ALIGN | s->be_data);
1937             tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
1938         } else if (s->be_data == MO_LE) {
1939             if (tb_cflags(s->base.tb) & CF_PARALLEL) {
1940                 gen_helper_paired_cmpxchg64_le_parallel(tmp, cpu_env,
1941                                                         cpu_exclusive_addr,
1942                                                         cpu_reg(s, rt),
1943                                                         cpu_reg(s, rt2));
1944             } else {
1945                 gen_helper_paired_cmpxchg64_le(tmp, cpu_env, cpu_exclusive_addr,
1946                                                cpu_reg(s, rt), cpu_reg(s, rt2));
1947             }
1948         } else {
1949             if (tb_cflags(s->base.tb) & CF_PARALLEL) {
1950                 gen_helper_paired_cmpxchg64_be_parallel(tmp, cpu_env,
1951                                                         cpu_exclusive_addr,
1952                                                         cpu_reg(s, rt),
1953                                                         cpu_reg(s, rt2));
1954             } else {
1955                 gen_helper_paired_cmpxchg64_be(tmp, cpu_env, cpu_exclusive_addr,
1956                                                cpu_reg(s, rt), cpu_reg(s, rt2));
1957             }
1958         }
1959     } else {
1960         tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
1961                                    cpu_reg(s, rt), get_mem_index(s),
1962                                    size | MO_ALIGN | s->be_data);
1963         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
1964     }
1965     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
1966     tcg_temp_free_i64(tmp);
1967     tcg_gen_br(done_label);
1968
1969     gen_set_label(fail_label);
1970     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
1971     gen_set_label(done_label);
1972     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1973 }
1974
1975 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
1976  * from the ARMv8 specs for LDR (Shared decode for all encodings).
1977  */
1978 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
1979 {
1980     int opc0 = extract32(opc, 0, 1);
1981     int regsize;
1982
1983     if (is_signed) {
1984         regsize = opc0 ? 32 : 64;
1985     } else {
1986         regsize = size == 3 ? 64 : 32;
1987     }
1988     return regsize == 64;
1989 }
1990
1991 /* Load/store exclusive
1992  *
1993  *  31 30 29         24  23  22   21  20  16  15  14   10 9    5 4    0
1994  * +-----+-------------+----+---+----+------+----+-------+------+------+
1995  * | sz  | 0 0 1 0 0 0 | o2 | L | o1 |  Rs  | o0 |  Rt2  |  Rn  | Rt   |
1996  * +-----+-------------+----+---+----+------+----+-------+------+------+
1997  *
1998  *  sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
1999  *   L: 0 -> store, 1 -> load
2000  *  o2: 0 -> exclusive, 1 -> not
2001  *  o1: 0 -> single register, 1 -> register pair
2002  *  o0: 1 -> load-acquire/store-release, 0 -> not
2003  */
2004 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2005 {
2006     int rt = extract32(insn, 0, 5);
2007     int rn = extract32(insn, 5, 5);
2008     int rt2 = extract32(insn, 10, 5);
2009     int is_lasr = extract32(insn, 15, 1);
2010     int rs = extract32(insn, 16, 5);
2011     int is_pair = extract32(insn, 21, 1);
2012     int is_store = !extract32(insn, 22, 1);
2013     int is_excl = !extract32(insn, 23, 1);
2014     int size = extract32(insn, 30, 2);
2015     TCGv_i64 tcg_addr;
2016
2017     if ((!is_excl && !is_pair && !is_lasr) ||
2018         (!is_excl && is_pair) ||
2019         (is_pair && size < 2)) {
2020         unallocated_encoding(s);
2021         return;
2022     }
2023
2024     if (rn == 31) {
2025         gen_check_sp_alignment(s);
2026     }
2027     tcg_addr = read_cpu_reg_sp(s, rn, 1);
2028
2029     /* Note that since TCG is single threaded load-acquire/store-release
2030      * semantics require no extra if (is_lasr) { ... } handling.
2031      */
2032
2033     if (is_excl) {
2034         if (!is_store) {
2035             s->is_ldex = true;
2036             gen_load_exclusive(s, rt, rt2, tcg_addr, size, is_pair);
2037             if (is_lasr) {
2038                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2039             }
2040         } else {
2041             if (is_lasr) {
2042                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2043             }
2044             gen_store_exclusive(s, rs, rt, rt2, tcg_addr, size, is_pair);
2045         }
2046     } else {
2047         TCGv_i64 tcg_rt = cpu_reg(s, rt);
2048         bool iss_sf = disas_ldst_compute_iss_sf(size, false, 0);
2049
2050         /* Generate ISS for non-exclusive accesses including LASR.  */
2051         if (is_store) {
2052             if (is_lasr) {
2053                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2054             }
2055             do_gpr_st(s, tcg_rt, tcg_addr, size,
2056                       true, rt, iss_sf, is_lasr);
2057         } else {
2058             do_gpr_ld(s, tcg_rt, tcg_addr, size, false, false,
2059                       true, rt, iss_sf, is_lasr);
2060             if (is_lasr) {
2061                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2062             }
2063         }
2064     }
2065 }
2066
2067 /*
2068  * Load register (literal)
2069  *
2070  *  31 30 29   27  26 25 24 23                5 4     0
2071  * +-----+-------+---+-----+-------------------+-------+
2072  * | opc | 0 1 1 | V | 0 0 |     imm19         |  Rt   |
2073  * +-----+-------+---+-----+-------------------+-------+
2074  *
2075  * V: 1 -> vector (simd/fp)
2076  * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2077  *                   10-> 32 bit signed, 11 -> prefetch
2078  * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2079  */
2080 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2081 {
2082     int rt = extract32(insn, 0, 5);
2083     int64_t imm = sextract32(insn, 5, 19) << 2;
2084     bool is_vector = extract32(insn, 26, 1);
2085     int opc = extract32(insn, 30, 2);
2086     bool is_signed = false;
2087     int size = 2;
2088     TCGv_i64 tcg_rt, tcg_addr;
2089
2090     if (is_vector) {
2091         if (opc == 3) {
2092             unallocated_encoding(s);
2093             return;
2094         }
2095         size = 2 + opc;
2096         if (!fp_access_check(s)) {
2097             return;
2098         }
2099     } else {
2100         if (opc == 3) {
2101             /* PRFM (literal) : prefetch */
2102             return;
2103         }
2104         size = 2 + extract32(opc, 0, 1);
2105         is_signed = extract32(opc, 1, 1);
2106     }
2107
2108     tcg_rt = cpu_reg(s, rt);
2109
2110     tcg_addr = tcg_const_i64((s->pc - 4) + imm);
2111     if (is_vector) {
2112         do_fp_ld(s, rt, tcg_addr, size);
2113     } else {
2114         /* Only unsigned 32bit loads target 32bit registers.  */
2115         bool iss_sf = opc != 0;
2116
2117         do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false,
2118                   true, rt, iss_sf, false);
2119     }
2120     tcg_temp_free_i64(tcg_addr);
2121 }
2122
2123 /*
2124  * LDNP (Load Pair - non-temporal hint)
2125  * LDP (Load Pair - non vector)
2126  * LDPSW (Load Pair Signed Word - non vector)
2127  * STNP (Store Pair - non-temporal hint)
2128  * STP (Store Pair - non vector)
2129  * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2130  * LDP (Load Pair of SIMD&FP)
2131  * STNP (Store Pair of SIMD&FP - non-temporal hint)
2132  * STP (Store Pair of SIMD&FP)
2133  *
2134  *  31 30 29   27  26  25 24   23  22 21   15 14   10 9    5 4    0
2135  * +-----+-------+---+---+-------+---+-----------------------------+
2136  * | opc | 1 0 1 | V | 0 | index | L |  imm7 |  Rt2  |  Rn  | Rt   |
2137  * +-----+-------+---+---+-------+---+-------+-------+------+------+
2138  *
2139  * opc: LDP/STP/LDNP/STNP        00 -> 32 bit, 10 -> 64 bit
2140  *      LDPSW                    01
2141  *      LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2142  *   V: 0 -> GPR, 1 -> Vector
2143  * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2144  *      10 -> signed offset, 11 -> pre-index
2145  *   L: 0 -> Store 1 -> Load
2146  *
2147  * Rt, Rt2 = GPR or SIMD registers to be stored
2148  * Rn = general purpose register containing address
2149  * imm7 = signed offset (multiple of 4 or 8 depending on size)
2150  */
2151 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2152 {
2153     int rt = extract32(insn, 0, 5);
2154     int rn = extract32(insn, 5, 5);
2155     int rt2 = extract32(insn, 10, 5);
2156     uint64_t offset = sextract64(insn, 15, 7);
2157     int index = extract32(insn, 23, 2);
2158     bool is_vector = extract32(insn, 26, 1);
2159     bool is_load = extract32(insn, 22, 1);
2160     int opc = extract32(insn, 30, 2);
2161
2162     bool is_signed = false;
2163     bool postindex = false;
2164     bool wback = false;
2165
2166     TCGv_i64 tcg_addr; /* calculated address */
2167     int size;
2168
2169     if (opc == 3) {
2170         unallocated_encoding(s);
2171         return;
2172     }
2173
2174     if (is_vector) {
2175         size = 2 + opc;
2176     } else {
2177         size = 2 + extract32(opc, 1, 1);
2178         is_signed = extract32(opc, 0, 1);
2179         if (!is_load && is_signed) {
2180             unallocated_encoding(s);
2181             return;
2182         }
2183     }
2184
2185     switch (index) {
2186     case 1: /* post-index */
2187         postindex = true;
2188         wback = true;
2189         break;
2190     case 0:
2191         /* signed offset with "non-temporal" hint. Since we don't emulate
2192          * caches we don't care about hints to the cache system about
2193          * data access patterns, and handle this identically to plain
2194          * signed offset.
2195          */
2196         if (is_signed) {
2197             /* There is no non-temporal-hint version of LDPSW */
2198             unallocated_encoding(s);
2199             return;
2200         }
2201         postindex = false;
2202         break;
2203     case 2: /* signed offset, rn not updated */
2204         postindex = false;
2205         break;
2206     case 3: /* pre-index */
2207         postindex = false;
2208         wback = true;
2209         break;
2210     }
2211
2212     if (is_vector && !fp_access_check(s)) {
2213         return;
2214     }
2215
2216     offset <<= size;
2217
2218     if (rn == 31) {
2219         gen_check_sp_alignment(s);
2220     }
2221
2222     tcg_addr = read_cpu_reg_sp(s, rn, 1);
2223
2224     if (!postindex) {
2225         tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
2226     }
2227
2228     if (is_vector) {
2229         if (is_load) {
2230             do_fp_ld(s, rt, tcg_addr, size);
2231         } else {
2232             do_fp_st(s, rt, tcg_addr, size);
2233         }
2234         tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
2235         if (is_load) {
2236             do_fp_ld(s, rt2, tcg_addr, size);
2237         } else {
2238             do_fp_st(s, rt2, tcg_addr, size);
2239         }
2240     } else {
2241         TCGv_i64 tcg_rt = cpu_reg(s, rt);
2242         TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
2243
2244         if (is_load) {
2245             TCGv_i64 tmp = tcg_temp_new_i64();
2246
2247             /* Do not modify tcg_rt before recognizing any exception
2248              * from the second load.
2249              */
2250             do_gpr_ld(s, tmp, tcg_addr, size, is_signed, false,
2251                       false, 0, false, false);
2252             tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
2253             do_gpr_ld(s, tcg_rt2, tcg_addr, size, is_signed, false,
2254                       false, 0, false, false);
2255
2256             tcg_gen_mov_i64(tcg_rt, tmp);
2257             tcg_temp_free_i64(tmp);
2258         } else {
2259             do_gpr_st(s, tcg_rt, tcg_addr, size,
2260                       false, 0, false, false);
2261             tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
2262             do_gpr_st(s, tcg_rt2, tcg_addr, size,
2263                       false, 0, false, false);
2264         }
2265     }
2266
2267     if (wback) {
2268         if (postindex) {
2269             tcg_gen_addi_i64(tcg_addr, tcg_addr, offset - (1 << size));
2270         } else {
2271             tcg_gen_subi_i64(tcg_addr, tcg_addr, 1 << size);
2272         }
2273         tcg_gen_mov_i64(cpu_reg_sp(s, rn), tcg_addr);
2274     }
2275 }
2276
2277 /*
2278  * Load/store (immediate post-indexed)
2279  * Load/store (immediate pre-indexed)
2280  * Load/store (unscaled immediate)
2281  *
2282  * 31 30 29   27  26 25 24 23 22 21  20    12 11 10 9    5 4    0
2283  * +----+-------+---+-----+-----+---+--------+-----+------+------+
2284  * |size| 1 1 1 | V | 0 0 | opc | 0 |  imm9  | idx |  Rn  |  Rt  |
2285  * +----+-------+---+-----+-----+---+--------+-----+------+------+
2286  *
2287  * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
2288          10 -> unprivileged
2289  * V = 0 -> non-vector
2290  * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
2291  * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2292  */
2293 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
2294                                 int opc,
2295                                 int size,
2296                                 int rt,
2297                                 bool is_vector)
2298 {
2299     int rn = extract32(insn, 5, 5);
2300     int imm9 = sextract32(insn, 12, 9);
2301     int idx = extract32(insn, 10, 2);
2302     bool is_signed = false;
2303     bool is_store = false;
2304     bool is_extended = false;
2305     bool is_unpriv = (idx == 2);
2306     bool iss_valid = !is_vector;
2307     bool post_index;
2308     bool writeback;
2309
2310     TCGv_i64 tcg_addr;
2311
2312     if (is_vector) {
2313         size |= (opc & 2) << 1;
2314         if (size > 4 || is_unpriv) {
2315             unallocated_encoding(s);
2316             return;
2317         }
2318         is_store = ((opc & 1) == 0);
2319         if (!fp_access_check(s)) {
2320             return;
2321         }
2322     } else {
2323         if (size == 3 && opc == 2) {
2324             /* PRFM - prefetch */
2325             if (is_unpriv) {
2326                 unallocated_encoding(s);
2327                 return;
2328             }
2329             return;
2330         }
2331         if (opc == 3 && size > 1) {
2332             unallocated_encoding(s);
2333             return;
2334         }
2335         is_store = (opc == 0);
2336         is_signed = extract32(opc, 1, 1);
2337         is_extended = (size < 3) && extract32(opc, 0, 1);
2338     }
2339
2340     switch (idx) {
2341     case 0:
2342     case 2:
2343         post_index = false;
2344         writeback = false;
2345         break;
2346     case 1:
2347         post_index = true;
2348         writeback = true;
2349         break;
2350     case 3:
2351         post_index = false;
2352         writeback = true;
2353         break;
2354     }
2355
2356     if (rn == 31) {
2357         gen_check_sp_alignment(s);
2358     }
2359     tcg_addr = read_cpu_reg_sp(s, rn, 1);
2360
2361     if (!post_index) {
2362         tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
2363     }
2364
2365     if (is_vector) {
2366         if (is_store) {
2367             do_fp_st(s, rt, tcg_addr, size);
2368         } else {
2369             do_fp_ld(s, rt, tcg_addr, size);
2370         }
2371     } else {
2372         TCGv_i64 tcg_rt = cpu_reg(s, rt);
2373         int memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
2374         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2375
2376         if (is_store) {
2377             do_gpr_st_memidx(s, tcg_rt, tcg_addr, size, memidx,
2378                              iss_valid, rt, iss_sf, false);
2379         } else {
2380             do_gpr_ld_memidx(s, tcg_rt, tcg_addr, size,
2381                              is_signed, is_extended, memidx,
2382                              iss_valid, rt, iss_sf, false);
2383         }
2384     }
2385
2386     if (writeback) {
2387         TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2388         if (post_index) {
2389             tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
2390         }
2391         tcg_gen_mov_i64(tcg_rn, tcg_addr);
2392     }
2393 }
2394
2395 /*
2396  * Load/store (register offset)
2397  *
2398  * 31 30 29   27  26 25 24 23 22 21  20  16 15 13 12 11 10 9  5 4  0
2399  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2400  * |size| 1 1 1 | V | 0 0 | opc | 1 |  Rm  | opt | S| 1 0 | Rn | Rt |
2401  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2402  *
2403  * For non-vector:
2404  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2405  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2406  * For vector:
2407  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2408  *   opc<0>: 0 -> store, 1 -> load
2409  * V: 1 -> vector/simd
2410  * opt: extend encoding (see DecodeRegExtend)
2411  * S: if S=1 then scale (essentially index by sizeof(size))
2412  * Rt: register to transfer into/out of
2413  * Rn: address register or SP for base
2414  * Rm: offset register or ZR for offset
2415  */
2416 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
2417                                    int opc,
2418                                    int size,
2419                                    int rt,
2420                                    bool is_vector)
2421 {
2422     int rn = extract32(insn, 5, 5);
2423     int shift = extract32(insn, 12, 1);
2424     int rm = extract32(insn, 16, 5);
2425     int opt = extract32(insn, 13, 3);
2426     bool is_signed = false;
2427     bool is_store = false;
2428     bool is_extended = false;
2429
2430     TCGv_i64 tcg_rm;
2431     TCGv_i64 tcg_addr;
2432
2433     if (extract32(opt, 1, 1) == 0) {
2434         unallocated_encoding(s);
2435         return;
2436     }
2437
2438     if (is_vector) {
2439         size |= (opc & 2) << 1;
2440         if (size > 4) {
2441             unallocated_encoding(s);
2442             return;
2443         }
2444         is_store = !extract32(opc, 0, 1);
2445         if (!fp_access_check(s)) {
2446             return;
2447         }
2448     } else {
2449         if (size == 3 && opc == 2) {
2450             /* PRFM - prefetch */
2451             return;
2452         }
2453         if (opc == 3 && size > 1) {
2454             unallocated_encoding(s);
2455             return;
2456         }
2457         is_store = (opc == 0);
2458         is_signed = extract32(opc, 1, 1);
2459         is_extended = (size < 3) && extract32(opc, 0, 1);
2460     }
2461
2462     if (rn == 31) {
2463         gen_check_sp_alignment(s);
2464     }
2465     tcg_addr = read_cpu_reg_sp(s, rn, 1);
2466
2467     tcg_rm = read_cpu_reg(s, rm, 1);
2468     ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
2469
2470     tcg_gen_add_i64(tcg_addr, tcg_addr, tcg_rm);
2471
2472     if (is_vector) {
2473         if (is_store) {
2474             do_fp_st(s, rt, tcg_addr, size);
2475         } else {
2476             do_fp_ld(s, rt, tcg_addr, size);
2477         }
2478     } else {
2479         TCGv_i64 tcg_rt = cpu_reg(s, rt);
2480         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2481         if (is_store) {
2482             do_gpr_st(s, tcg_rt, tcg_addr, size,
2483                       true, rt, iss_sf, false);
2484         } else {
2485             do_gpr_ld(s, tcg_rt, tcg_addr, size,
2486                       is_signed, is_extended,
2487                       true, rt, iss_sf, false);
2488         }
2489     }
2490 }
2491
2492 /*
2493  * Load/store (unsigned immediate)
2494  *
2495  * 31 30 29   27  26 25 24 23 22 21        10 9     5
2496  * +----+-------+---+-----+-----+------------+-------+------+
2497  * |size| 1 1 1 | V | 0 1 | opc |   imm12    |  Rn   |  Rt  |
2498  * +----+-------+---+-----+-----+------------+-------+------+
2499  *
2500  * For non-vector:
2501  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2502  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2503  * For vector:
2504  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2505  *   opc<0>: 0 -> store, 1 -> load
2506  * Rn: base address register (inc SP)
2507  * Rt: target register
2508  */
2509 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
2510                                         int opc,
2511                                         int size,
2512                                         int rt,
2513                                         bool is_vector)
2514 {
2515     int rn = extract32(insn, 5, 5);
2516     unsigned int imm12 = extract32(insn, 10, 12);
2517     unsigned int offset;
2518
2519     TCGv_i64 tcg_addr;
2520
2521     bool is_store;
2522     bool is_signed = false;
2523     bool is_extended = false;
2524
2525     if (is_vector) {
2526         size |= (opc & 2) << 1;
2527         if (size > 4) {
2528             unallocated_encoding(s);
2529             return;
2530         }
2531         is_store = !extract32(opc, 0, 1);
2532         if (!fp_access_check(s)) {
2533             return;
2534         }
2535     } else {
2536         if (size == 3 && opc == 2) {
2537             /* PRFM - prefetch */
2538             return;
2539         }
2540         if (opc == 3 && size > 1) {
2541             unallocated_encoding(s);
2542             return;
2543         }
2544         is_store = (opc == 0);
2545         is_signed = extract32(opc, 1, 1);
2546         is_extended = (size < 3) && extract32(opc, 0, 1);
2547     }
2548
2549     if (rn == 31) {
2550         gen_check_sp_alignment(s);
2551     }
2552     tcg_addr = read_cpu_reg_sp(s, rn, 1);
2553     offset = imm12 << size;
2554     tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
2555
2556     if (is_vector) {
2557         if (is_store) {
2558             do_fp_st(s, rt, tcg_addr, size);
2559         } else {
2560             do_fp_ld(s, rt, tcg_addr, size);
2561         }
2562     } else {
2563         TCGv_i64 tcg_rt = cpu_reg(s, rt);
2564         bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2565         if (is_store) {
2566             do_gpr_st(s, tcg_rt, tcg_addr, size,
2567                       true, rt, iss_sf, false);
2568         } else {
2569             do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended,
2570                       true, rt, iss_sf, false);
2571         }
2572     }
2573 }
2574
2575 /* Load/store register (all forms) */
2576 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
2577 {
2578     int rt = extract32(insn, 0, 5);
2579     int opc = extract32(insn, 22, 2);
2580     bool is_vector = extract32(insn, 26, 1);
2581     int size = extract32(insn, 30, 2);
2582
2583     switch (extract32(insn, 24, 2)) {
2584     case 0:
2585         if (extract32(insn, 21, 1) == 1 && extract32(insn, 10, 2) == 2) {
2586             disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
2587         } else {
2588             /* Load/store register (unscaled immediate)
2589              * Load/store immediate pre/post-indexed
2590              * Load/store register unprivileged
2591              */
2592             disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
2593         }
2594         break;
2595     case 1:
2596         disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
2597         break;
2598     default:
2599         unallocated_encoding(s);
2600         break;
2601     }
2602 }
2603
2604 /* AdvSIMD load/store multiple structures
2605  *
2606  *  31  30  29           23 22  21         16 15    12 11  10 9    5 4    0
2607  * +---+---+---------------+---+-------------+--------+------+------+------+
2608  * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size |  Rn  |  Rt  |
2609  * +---+---+---------------+---+-------------+--------+------+------+------+
2610  *
2611  * AdvSIMD load/store multiple structures (post-indexed)
2612  *
2613  *  31  30  29           23 22  21  20     16 15    12 11  10 9    5 4    0
2614  * +---+---+---------------+---+---+---------+--------+------+------+------+
2615  * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 |   Rm    | opcode | size |  Rn  |  Rt  |
2616  * +---+---+---------------+---+---+---------+--------+------+------+------+
2617  *
2618  * Rt: first (or only) SIMD&FP register to be transferred
2619  * Rn: base address or SP
2620  * Rm (post-index only): post-index register (when !31) or size dependent #imm
2621  */
2622 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
2623 {
2624     int rt = extract32(insn, 0, 5);
2625     int rn = extract32(insn, 5, 5);
2626     int size = extract32(insn, 10, 2);
2627     int opcode = extract32(insn, 12, 4);
2628     bool is_store = !extract32(insn, 22, 1);
2629     bool is_postidx = extract32(insn, 23, 1);
2630     bool is_q = extract32(insn, 30, 1);
2631     TCGv_i64 tcg_addr, tcg_rn;
2632
2633     int ebytes = 1 << size;
2634     int elements = (is_q ? 128 : 64) / (8 << size);
2635     int rpt;    /* num iterations */
2636     int selem;  /* structure elements */
2637     int r;
2638
2639     if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
2640         unallocated_encoding(s);
2641         return;
2642     }
2643
2644     /* From the shared decode logic */
2645     switch (opcode) {
2646     case 0x0:
2647         rpt = 1;
2648         selem = 4;
2649         break;
2650     case 0x2:
2651         rpt = 4;
2652         selem = 1;
2653         break;
2654     case 0x4:
2655         rpt = 1;
2656         selem = 3;
2657         break;
2658     case 0x6:
2659         rpt = 3;
2660         selem = 1;
2661         break;
2662     case 0x7:
2663         rpt = 1;
2664         selem = 1;
2665         break;
2666     case 0x8:
2667         rpt = 1;
2668         selem = 2;
2669         break;
2670     case 0xa:
2671         rpt = 2;
2672         selem = 1;
2673         break;
2674     default:
2675         unallocated_encoding(s);
2676         return;
2677     }
2678
2679     if (size == 3 && !is_q && selem != 1) {
2680         /* reserved */
2681         unallocated_encoding(s);
2682         return;
2683     }
2684
2685     if (!fp_access_check(s)) {
2686         return;
2687     }
2688
2689     if (rn == 31) {
2690         gen_check_sp_alignment(s);
2691     }
2692
2693     tcg_rn = cpu_reg_sp(s, rn);
2694     tcg_addr = tcg_temp_new_i64();
2695     tcg_gen_mov_i64(tcg_addr, tcg_rn);
2696
2697     for (r = 0; r < rpt; r++) {
2698         int e;
2699         for (e = 0; e < elements; e++) {
2700             int tt = (rt + r) % 32;
2701             int xs;
2702             for (xs = 0; xs < selem; xs++) {
2703                 if (is_store) {
2704                     do_vec_st(s, tt, e, tcg_addr, size);
2705                 } else {
2706                     do_vec_ld(s, tt, e, tcg_addr, size);
2707
2708                     /* For non-quad operations, setting a slice of the low
2709                      * 64 bits of the register clears the high 64 bits (in
2710                      * the ARM ARM pseudocode this is implicit in the fact
2711                      * that 'rval' is a 64 bit wide variable). We optimize
2712                      * by noticing that we only need to do this the first
2713                      * time we touch a register.
2714                      */
2715                     if (!is_q && e == 0 && (r == 0 || xs == selem - 1)) {
2716                         clear_vec_high(s, tt);
2717                     }
2718                 }
2719                 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2720                 tt = (tt + 1) % 32;
2721             }
2722         }
2723     }
2724
2725     if (is_postidx) {
2726         int rm = extract32(insn, 16, 5);
2727         if (rm == 31) {
2728             tcg_gen_mov_i64(tcg_rn, tcg_addr);
2729         } else {
2730             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2731         }
2732     }
2733     tcg_temp_free_i64(tcg_addr);
2734 }
2735
2736 /* AdvSIMD load/store single structure
2737  *
2738  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
2739  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2740  * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size |  Rn  |  Rt  |
2741  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2742  *
2743  * AdvSIMD load/store single structure (post-indexed)
2744  *
2745  *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
2746  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2747  * | 0 | Q | 0 0 1 1 0 1 1 | L R |     Rm    | opc | S | size |  Rn  |  Rt  |
2748  * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2749  *
2750  * Rt: first (or only) SIMD&FP register to be transferred
2751  * Rn: base address or SP
2752  * Rm (post-index only): post-index register (when !31) or size dependent #imm
2753  * index = encoded in Q:S:size dependent on size
2754  *
2755  * lane_size = encoded in R, opc
2756  * transfer width = encoded in opc, S, size
2757  */
2758 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
2759 {
2760     int rt = extract32(insn, 0, 5);
2761     int rn = extract32(insn, 5, 5);
2762     int size = extract32(insn, 10, 2);
2763     int S = extract32(insn, 12, 1);
2764     int opc = extract32(insn, 13, 3);
2765     int R = extract32(insn, 21, 1);
2766     int is_load = extract32(insn, 22, 1);
2767     int is_postidx = extract32(insn, 23, 1);
2768     int is_q = extract32(insn, 30, 1);
2769
2770     int scale = extract32(opc, 1, 2);
2771     int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
2772     bool replicate = false;
2773     int index = is_q << 3 | S << 2 | size;
2774     int ebytes, xs;
2775     TCGv_i64 tcg_addr, tcg_rn;
2776
2777     switch (scale) {
2778     case 3:
2779         if (!is_load || S) {
2780             unallocated_encoding(s);
2781             return;
2782         }
2783         scale = size;
2784         replicate = true;
2785         break;
2786     case 0:
2787         break;
2788     case 1:
2789         if (extract32(size, 0, 1)) {
2790             unallocated_encoding(s);
2791             return;
2792         }
2793         index >>= 1;
2794         break;
2795     case 2:
2796         if (extract32(size, 1, 1)) {
2797             unallocated_encoding(s);
2798             return;
2799         }
2800         if (!extract32(size, 0, 1)) {
2801             index >>= 2;
2802         } else {
2803             if (S) {
2804                 unallocated_encoding(s);
2805                 return;
2806             }
2807             index >>= 3;
2808             scale = 3;
2809         }
2810         break;
2811     default:
2812         g_assert_not_reached();
2813     }
2814
2815     if (!fp_access_check(s)) {
2816         return;
2817     }
2818
2819     ebytes = 1 << scale;
2820
2821     if (rn == 31) {
2822         gen_check_sp_alignment(s);
2823     }
2824
2825     tcg_rn = cpu_reg_sp(s, rn);
2826     tcg_addr = tcg_temp_new_i64();
2827     tcg_gen_mov_i64(tcg_addr, tcg_rn);
2828
2829     for (xs = 0; xs < selem; xs++) {
2830         if (replicate) {
2831             /* Load and replicate to all elements */
2832             uint64_t mulconst;
2833             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
2834
2835             tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr,
2836                                 get_mem_index(s), s->be_data + scale);
2837             switch (scale) {
2838             case 0:
2839                 mulconst = 0x0101010101010101ULL;
2840                 break;
2841             case 1:
2842                 mulconst = 0x0001000100010001ULL;
2843                 break;
2844             case 2:
2845                 mulconst = 0x0000000100000001ULL;
2846                 break;
2847             case 3:
2848                 mulconst = 0;
2849                 break;
2850             default:
2851                 g_assert_not_reached();
2852             }
2853             if (mulconst) {
2854                 tcg_gen_muli_i64(tcg_tmp, tcg_tmp, mulconst);
2855             }
2856             write_vec_element(s, tcg_tmp, rt, 0, MO_64);
2857             if (is_q) {
2858                 write_vec_element(s, tcg_tmp, rt, 1, MO_64);
2859             } else {
2860                 clear_vec_high(s, rt);
2861             }
2862             tcg_temp_free_i64(tcg_tmp);
2863         } else {
2864             /* Load/store one element per register */
2865             if (is_load) {
2866                 do_vec_ld(s, rt, index, tcg_addr, scale);
2867             } else {
2868                 do_vec_st(s, rt, index, tcg_addr, scale);
2869             }
2870         }
2871         tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2872         rt = (rt + 1) % 32;
2873     }
2874
2875     if (is_postidx) {
2876         int rm = extract32(insn, 16, 5);
2877         if (rm == 31) {
2878             tcg_gen_mov_i64(tcg_rn, tcg_addr);
2879         } else {
2880             tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2881         }
2882     }
2883     tcg_temp_free_i64(tcg_addr);
2884 }
2885
2886 /* Loads and stores */
2887 static void disas_ldst(DisasContext *s, uint32_t insn)
2888 {
2889     switch (extract32(insn, 24, 6)) {
2890     case 0x08: /* Load/store exclusive */
2891         disas_ldst_excl(s, insn);
2892         break;
2893     case 0x18: case 0x1c: /* Load register (literal) */
2894         disas_ld_lit(s, insn);
2895         break;
2896     case 0x28: case 0x29:
2897     case 0x2c: case 0x2d: /* Load/store pair (all forms) */
2898         disas_ldst_pair(s, insn);
2899         break;
2900     case 0x38: case 0x39:
2901     case 0x3c: case 0x3d: /* Load/store register (all forms) */
2902         disas_ldst_reg(s, insn);
2903         break;
2904     case 0x0c: /* AdvSIMD load/store multiple structures */
2905         disas_ldst_multiple_struct(s, insn);
2906         break;
2907     case 0x0d: /* AdvSIMD load/store single structure */
2908         disas_ldst_single_struct(s, insn);
2909         break;
2910     default:
2911         unallocated_encoding(s);
2912         break;
2913     }
2914 }
2915
2916 /* PC-rel. addressing
2917  *   31  30   29 28       24 23                5 4    0
2918  * +----+-------+-----------+-------------------+------+
2919  * | op | immlo | 1 0 0 0 0 |       immhi       |  Rd  |
2920  * +----+-------+-----------+-------------------+------+
2921  */
2922 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
2923 {
2924     unsigned int page, rd;
2925     uint64_t base;
2926     uint64_t offset;
2927
2928     page = extract32(insn, 31, 1);
2929     /* SignExtend(immhi:immlo) -> offset */
2930     offset = sextract64(insn, 5, 19);
2931     offset = offset << 2 | extract32(insn, 29, 2);
2932     rd = extract32(insn, 0, 5);
2933     base = s->pc - 4;
2934
2935     if (page) {
2936         /* ADRP (page based) */
2937         base &= ~0xfff;
2938         offset <<= 12;
2939     }
2940
2941     tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
2942 }
2943
2944 /*
2945  * Add/subtract (immediate)
2946  *
2947  *  31 30 29 28       24 23 22 21         10 9   5 4   0
2948  * +--+--+--+-----------+-----+-------------+-----+-----+
2949  * |sf|op| S| 1 0 0 0 1 |shift|    imm12    |  Rn | Rd  |
2950  * +--+--+--+-----------+-----+-------------+-----+-----+
2951  *
2952  *    sf: 0 -> 32bit, 1 -> 64bit
2953  *    op: 0 -> add  , 1 -> sub
2954  *     S: 1 -> set flags
2955  * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
2956  */
2957 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
2958 {
2959     int rd = extract32(insn, 0, 5);
2960     int rn = extract32(insn, 5, 5);
2961     uint64_t imm = extract32(insn, 10, 12);
2962     int shift = extract32(insn, 22, 2);
2963     bool setflags = extract32(insn, 29, 1);
2964     bool sub_op = extract32(insn, 30, 1);
2965     bool is_64bit = extract32(insn, 31, 1);
2966
2967     TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2968     TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
2969     TCGv_i64 tcg_result;
2970
2971     switch (shift) {
2972     case 0x0:
2973         break;
2974     case 0x1:
2975         imm <<= 12;
2976         break;
2977     default:
2978         unallocated_encoding(s);
2979         return;
2980     }
2981
2982     tcg_result = tcg_temp_new_i64();
2983     if (!setflags) {
2984         if (sub_op) {
2985             tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
2986         } else {
2987             tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
2988         }
2989     } else {
2990         TCGv_i64 tcg_imm = tcg_const_i64(imm);
2991         if (sub_op) {
2992             gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2993         } else {
2994             gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2995         }
2996         tcg_temp_free_i64(tcg_imm);
2997     }
2998
2999     if (is_64bit) {
3000         tcg_gen_mov_i64(tcg_rd, tcg_result);
3001     } else {
3002         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3003     }
3004
3005     tcg_temp_free_i64(tcg_result);
3006 }
3007
3008 /* The input should be a value in the bottom e bits (with higher
3009  * bits zero); returns that value replicated into every element
3010  * of size e in a 64 bit integer.
3011  */
3012 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
3013 {
3014     assert(e != 0);
3015     while (e < 64) {
3016         mask |= mask << e;
3017         e *= 2;
3018     }
3019     return mask;
3020 }
3021
3022 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
3023 static inline uint64_t bitmask64(unsigned int length)
3024 {
3025     assert(length > 0 && length <= 64);
3026     return ~0ULL >> (64 - length);
3027 }
3028
3029 /* Simplified variant of pseudocode DecodeBitMasks() for the case where we
3030  * only require the wmask. Returns false if the imms/immr/immn are a reserved
3031  * value (ie should cause a guest UNDEF exception), and true if they are
3032  * valid, in which case the decoded bit pattern is written to result.
3033  */
3034 static bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
3035                                    unsigned int imms, unsigned int immr)
3036 {
3037     uint64_t mask;
3038     unsigned e, levels, s, r;
3039     int len;
3040
3041     assert(immn < 2 && imms < 64 && immr < 64);
3042
3043     /* The bit patterns we create here are 64 bit patterns which
3044      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
3045      * 64 bits each. Each element contains the same value: a run
3046      * of between 1 and e-1 non-zero bits, rotated within the
3047      * element by between 0 and e-1 bits.
3048      *
3049      * The element size and run length are encoded into immn (1 bit)
3050      * and imms (6 bits) as follows:
3051      * 64 bit elements: immn = 1, imms = <length of run - 1>
3052      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
3053      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
3054      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
3055      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
3056      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
3057      * Notice that immn = 0, imms = 11111x is the only combination
3058      * not covered by one of the above options; this is reserved.
3059      * Further, <length of run - 1> all-ones is a reserved pattern.
3060      *
3061      * In all cases the rotation is by immr % e (and immr is 6 bits).
3062      */
3063
3064     /* First determine the element size */
3065     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
3066     if (len < 1) {
3067         /* This is the immn == 0, imms == 0x11111x case */
3068         return false;
3069     }
3070     e = 1 << len;
3071
3072     levels = e - 1;
3073     s = imms & levels;
3074     r = immr & levels;
3075
3076     if (s == levels) {
3077         /* <length of run - 1> mustn't be all-ones. */
3078         return false;
3079     }
3080
3081     /* Create the value of one element: s+1 set bits rotated
3082      * by r within the element (which is e bits wide)...
3083      */
3084     mask = bitmask64(s + 1);
3085     if (r) {
3086         mask = (mask >> r) | (mask << (e - r));
3087         mask &= bitmask64(e);
3088     }
3089     /* ...then replicate the element over the whole 64 bit value */
3090     mask = bitfield_replicate(mask, e);
3091     *result = mask;
3092     return true;
3093 }
3094
3095 /* Logical (immediate)
3096  *   31  30 29 28         23 22  21  16 15  10 9    5 4    0
3097  * +----+-----+-------------+---+------+------+------+------+
3098  * | sf | opc | 1 0 0 1 0 0 | N | immr | imms |  Rn  |  Rd  |
3099  * +----+-----+-------------+---+------+------+------+------+
3100  */
3101 static void disas_logic_imm(DisasContext *s, uint32_t insn)
3102 {
3103     unsigned int sf, opc, is_n, immr, imms, rn, rd;
3104     TCGv_i64 tcg_rd, tcg_rn;
3105     uint64_t wmask;
3106     bool is_and = false;
3107
3108     sf = extract32(insn, 31, 1);
3109     opc = extract32(insn, 29, 2);
3110     is_n = extract32(insn, 22, 1);
3111     immr = extract32(insn, 16, 6);
3112     imms = extract32(insn, 10, 6);
3113     rn = extract32(insn, 5, 5);
3114     rd = extract32(insn, 0, 5);
3115
3116     if (!sf && is_n) {
3117         unallocated_encoding(s);
3118         return;
3119     }
3120
3121     if (opc == 0x3) { /* ANDS */
3122         tcg_rd = cpu_reg(s, rd);
3123     } else {
3124         tcg_rd = cpu_reg_sp(s, rd);
3125     }
3126     tcg_rn = cpu_reg(s, rn);
3127
3128     if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
3129         /* some immediate field values are reserved */
3130         unallocated_encoding(s);
3131         return;
3132     }
3133
3134     if (!sf) {
3135         wmask &= 0xffffffff;
3136     }
3137
3138     switch (opc) {
3139     case 0x3: /* ANDS */
3140     case 0x0: /* AND */
3141         tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
3142         is_and = true;
3143         break;
3144     case 0x1: /* ORR */
3145         tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
3146         break;
3147     case 0x2: /* EOR */
3148         tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
3149         break;
3150     default:
3151         assert(FALSE); /* must handle all above */
3152         break;
3153     }
3154
3155     if (!sf && !is_and) {
3156         /* zero extend final result; we know we can skip this for AND
3157          * since the immediate had the high 32 bits clear.
3158          */
3159         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3160     }
3161
3162     if (opc == 3) { /* ANDS */
3163         gen_logic_CC(sf, tcg_rd);
3164     }
3165 }
3166
3167 /*
3168  * Move wide (immediate)
3169  *
3170  *  31 30 29 28         23 22 21 20             5 4    0
3171  * +--+-----+-------------+-----+----------------+------+
3172  * |sf| opc | 1 0 0 1 0 1 |  hw |  imm16         |  Rd  |
3173  * +--+-----+-------------+-----+----------------+------+
3174  *
3175  * sf: 0 -> 32 bit, 1 -> 64 bit
3176  * opc: 00 -> N, 10 -> Z, 11 -> K
3177  * hw: shift/16 (0,16, and sf only 32, 48)
3178  */
3179 static void disas_movw_imm(DisasContext *s, uint32_t insn)
3180 {
3181     int rd = extract32(insn, 0, 5);
3182     uint64_t imm = extract32(insn, 5, 16);
3183     int sf = extract32(insn, 31, 1);
3184     int opc = extract32(insn, 29, 2);
3185     int pos = extract32(insn, 21, 2) << 4;
3186     TCGv_i64 tcg_rd = cpu_reg(s, rd);
3187     TCGv_i64 tcg_imm;
3188
3189     if (!sf && (pos >= 32)) {
3190         unallocated_encoding(s);
3191         return;
3192     }
3193
3194     switch (opc) {
3195     case 0: /* MOVN */
3196     case 2: /* MOVZ */
3197         imm <<= pos;
3198         if (opc == 0) {
3199             imm = ~imm;
3200         }
3201         if (!sf) {
3202             imm &= 0xffffffffu;
3203         }
3204         tcg_gen_movi_i64(tcg_rd, imm);
3205         break;
3206     case 3: /* MOVK */
3207         tcg_imm = tcg_const_i64(imm);
3208         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
3209         tcg_temp_free_i64(tcg_imm);
3210         if (!sf) {
3211             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3212         }
3213         break;
3214     default:
3215         unallocated_encoding(s);
3216         break;
3217     }
3218 }
3219
3220 /* Bitfield
3221  *   31  30 29 28         23 22  21  16 15  10 9    5 4    0
3222  * +----+-----+-------------+---+------+------+------+------+
3223  * | sf | opc | 1 0 0 1 1 0 | N | immr | imms |  Rn  |  Rd  |
3224  * +----+-----+-------------+---+------+------+------+------+
3225  */
3226 static void disas_bitfield(DisasContext *s, uint32_t insn)
3227 {
3228     unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
3229     TCGv_i64 tcg_rd, tcg_tmp;
3230
3231     sf = extract32(insn, 31, 1);
3232     opc = extract32(insn, 29, 2);
3233     n = extract32(insn, 22, 1);
3234     ri = extract32(insn, 16, 6);
3235     si = extract32(insn, 10, 6);
3236     rn = extract32(insn, 5, 5);
3237     rd = extract32(insn, 0, 5);
3238     bitsize = sf ? 64 : 32;
3239
3240     if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
3241         unallocated_encoding(s);
3242         return;
3243     }
3244
3245     tcg_rd = cpu_reg(s, rd);
3246
3247     /* Suppress the zero-extend for !sf.  Since RI and SI are constrained
3248        to be smaller than bitsize, we'll never reference data outside the
3249        low 32-bits anyway.  */
3250     tcg_tmp = read_cpu_reg(s, rn, 1);
3251
3252     /* Recognize simple(r) extractions.  */
3253     if (si >= ri) {
3254         /* Wd<s-r:0> = Wn<s:r> */
3255         len = (si - ri) + 1;
3256         if (opc == 0) { /* SBFM: ASR, SBFX, SXTB, SXTH, SXTW */
3257             tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
3258             goto done;
3259         } else if (opc == 2) { /* UBFM: UBFX, LSR, UXTB, UXTH */
3260             tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
3261             return;
3262         }
3263         /* opc == 1, BXFIL fall through to deposit */
3264         tcg_gen_extract_i64(tcg_tmp, tcg_tmp, ri, len);
3265         pos = 0;
3266     } else {
3267         /* Handle the ri > si case with a deposit
3268          * Wd<32+s-r,32-r> = Wn<s:0>
3269          */
3270         len = si + 1;
3271         pos = (bitsize - ri) & (bitsize - 1);
3272     }
3273
3274     if (opc == 0 && len < ri) {
3275         /* SBFM: sign extend the destination field from len to fill
3276            the balance of the word.  Let the deposit below insert all
3277            of those sign bits.  */
3278         tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
3279         len = ri;
3280     }
3281
3282     if (opc == 1) { /* BFM, BXFIL */
3283         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
3284     } else {
3285         /* SBFM or UBFM: We start with zero, and we haven't modified
3286            any bits outside bitsize, therefore the zero-extension
3287            below is unneeded.  */
3288         tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
3289         return;
3290     }
3291
3292  done:
3293     if (!sf) { /* zero extend final result */
3294         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3295     }
3296 }
3297
3298 /* Extract
3299  *   31  30  29 28         23 22   21  20  16 15    10 9    5 4    0
3300  * +----+------+-------------+---+----+------+--------+------+------+
3301  * | sf | op21 | 1 0 0 1 1 1 | N | o0 |  Rm  |  imms  |  Rn  |  Rd  |
3302  * +----+------+-------------+---+----+------+--------+------+------+
3303  */
3304 static void disas_extract(DisasContext *s, uint32_t insn)
3305 {
3306     unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
3307
3308     sf = extract32(insn, 31, 1);
3309     n = extract32(insn, 22, 1);
3310     rm = extract32(insn, 16, 5);
3311     imm = extract32(insn, 10, 6);
3312     rn = extract32(insn, 5, 5);
3313     rd = extract32(insn, 0, 5);
3314     op21 = extract32(insn, 29, 2);
3315     op0 = extract32(insn, 21, 1);
3316     bitsize = sf ? 64 : 32;
3317
3318     if (sf != n || op21 || op0 || imm >= bitsize) {
3319         unallocated_encoding(s);
3320     } else {
3321         TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
3322
3323         tcg_rd = cpu_reg(s, rd);
3324
3325         if (unlikely(imm == 0)) {
3326             /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
3327              * so an extract from bit 0 is a special case.
3328              */
3329             if (sf) {
3330                 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
3331             } else {
3332                 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
3333             }
3334         } else if (rm == rn) { /* ROR */
3335             tcg_rm = cpu_reg(s, rm);
3336             if (sf) {
3337                 tcg_gen_rotri_i64(tcg_rd, tcg_rm, imm);
3338             } else {
3339                 TCGv_i32 tmp = tcg_temp_new_i32();
3340                 tcg_gen_extrl_i64_i32(tmp, tcg_rm);
3341                 tcg_gen_rotri_i32(tmp, tmp, imm);
3342                 tcg_gen_extu_i32_i64(tcg_rd, tmp);
3343                 tcg_temp_free_i32(tmp);
3344             }
3345         } else {
3346             tcg_rm = read_cpu_reg(s, rm, sf);
3347             tcg_rn = read_cpu_reg(s, rn, sf);
3348             tcg_gen_shri_i64(tcg_rm, tcg_rm, imm);
3349             tcg_gen_shli_i64(tcg_rn, tcg_rn, bitsize - imm);
3350             tcg_gen_or_i64(tcg_rd, tcg_rm, tcg_rn);
3351             if (!sf) {
3352                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3353             }
3354         }
3355     }
3356 }
3357
3358 /* Data processing - immediate */
3359 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
3360 {
3361     switch (extract32(insn, 23, 6)) {
3362     case 0x20: case 0x21: /* PC-rel. addressing */
3363         disas_pc_rel_adr(s, insn);
3364         break;
3365     case 0x22: case 0x23: /* Add/subtract (immediate) */
3366         disas_add_sub_imm(s, insn);
3367         break;
3368     case 0x24: /* Logical (immediate) */
3369         disas_logic_imm(s, insn);
3370         break;
3371     case 0x25: /* Move wide (immediate) */
3372         disas_movw_imm(s, insn);
3373         break;
3374     case 0x26: /* Bitfield */
3375         disas_bitfield(s, insn);
3376         break;
3377     case 0x27: /* Extract */
3378         disas_extract(s, insn);
3379         break;
3380     default:
3381         unallocated_encoding(s);
3382         break;
3383     }
3384 }
3385
3386 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
3387  * Note that it is the caller's responsibility to ensure that the
3388  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
3389  * mandated semantics for out of range shifts.
3390  */
3391 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
3392                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
3393 {
3394     switch (shift_type) {
3395     case A64_SHIFT_TYPE_LSL:
3396         tcg_gen_shl_i64(dst, src, shift_amount);
3397         break;
3398     case A64_SHIFT_TYPE_LSR:
3399         tcg_gen_shr_i64(dst, src, shift_amount);
3400         break;
3401     case A64_SHIFT_TYPE_ASR:
3402         if (!sf) {
3403             tcg_gen_ext32s_i64(dst, src);
3404         }
3405         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
3406         break;
3407     case A64_SHIFT_TYPE_ROR:
3408         if (sf) {
3409             tcg_gen_rotr_i64(dst, src, shift_amount);
3410         } else {
3411             TCGv_i32 t0, t1;
3412             t0 = tcg_temp_new_i32();
3413             t1 = tcg_temp_new_i32();
3414             tcg_gen_extrl_i64_i32(t0, src);
3415             tcg_gen_extrl_i64_i32(t1, shift_amount);
3416             tcg_gen_rotr_i32(t0, t0, t1);
3417             tcg_gen_extu_i32_i64(dst, t0);
3418             tcg_temp_free_i32(t0);
3419             tcg_temp_free_i32(t1);
3420         }
3421         break;
3422     default:
3423         assert(FALSE); /* all shift types should be handled */
3424         break;
3425     }
3426
3427     if (!sf) { /* zero extend final result */
3428         tcg_gen_ext32u_i64(dst, dst);
3429     }
3430 }
3431
3432 /* Shift a TCGv src by immediate, put result in dst.
3433  * The shift amount must be in range (this should always be true as the
3434  * relevant instructions will UNDEF on bad shift immediates).
3435  */
3436 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
3437                           enum a64_shift_type shift_type, unsigned int shift_i)
3438 {
3439     assert(shift_i < (sf ? 64 : 32));
3440
3441     if (shift_i == 0) {
3442         tcg_gen_mov_i64(dst, src);
3443     } else {
3444         TCGv_i64 shift_const;
3445
3446         shift_const = tcg_const_i64(shift_i);
3447         shift_reg(dst, src, sf, shift_type, shift_const);
3448         tcg_temp_free_i64(shift_const);
3449     }
3450 }
3451
3452 /* Logical (shifted register)
3453  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
3454  * +----+-----+-----------+-------+---+------+--------+------+------+
3455  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
3456  * +----+-----+-----------+-------+---+------+--------+------+------+
3457  */
3458 static void disas_logic_reg(DisasContext *s, uint32_t insn)
3459 {
3460     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
3461     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
3462
3463     sf = extract32(insn, 31, 1);
3464     opc = extract32(insn, 29, 2);
3465     shift_type = extract32(insn, 22, 2);
3466     invert = extract32(insn, 21, 1);
3467     rm = extract32(insn, 16, 5);
3468     shift_amount = extract32(insn, 10, 6);
3469     rn = extract32(insn, 5, 5);
3470     rd = extract32(insn, 0, 5);
3471
3472     if (!sf && (shift_amount & (1 << 5))) {
3473         unallocated_encoding(s);
3474         return;
3475     }
3476
3477     tcg_rd = cpu_reg(s, rd);
3478
3479     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
3480         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
3481          * register-register MOV and MVN, so it is worth special casing.
3482          */
3483         tcg_rm = cpu_reg(s, rm);
3484         if (invert) {
3485             tcg_gen_not_i64(tcg_rd, tcg_rm);
3486             if (!sf) {
3487                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3488             }
3489         } else {
3490             if (sf) {
3491                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
3492             } else {
3493                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
3494             }
3495         }
3496         return;
3497     }
3498
3499     tcg_rm = read_cpu_reg(s, rm, sf);
3500
3501     if (shift_amount) {
3502         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
3503     }
3504
3505     tcg_rn = cpu_reg(s, rn);
3506
3507     switch (opc | (invert << 2)) {
3508     case 0: /* AND */
3509     case 3: /* ANDS */
3510         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
3511         break;
3512     case 1: /* ORR */
3513         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
3514         break;
3515     case 2: /* EOR */
3516         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
3517         break;
3518     case 4: /* BIC */
3519     case 7: /* BICS */
3520         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
3521         break;
3522     case 5: /* ORN */
3523         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
3524         break;
3525     case 6: /* EON */
3526         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
3527         break;
3528     default:
3529         assert(FALSE);
3530         break;
3531     }
3532
3533     if (!sf) {
3534         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3535     }
3536
3537     if (opc == 3) {
3538         gen_logic_CC(sf, tcg_rd);
3539     }
3540 }
3541
3542 /*
3543  * Add/subtract (extended register)
3544  *
3545  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
3546  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3547  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
3548  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3549  *
3550  *  sf: 0 -> 32bit, 1 -> 64bit
3551  *  op: 0 -> add  , 1 -> sub
3552  *   S: 1 -> set flags
3553  * opt: 00
3554  * option: extension type (see DecodeRegExtend)
3555  * imm3: optional shift to Rm
3556  *
3557  * Rd = Rn + LSL(extend(Rm), amount)
3558  */
3559 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
3560 {
3561     int rd = extract32(insn, 0, 5);
3562     int rn = extract32(insn, 5, 5);
3563     int imm3 = extract32(insn, 10, 3);
3564     int option = extract32(insn, 13, 3);
3565     int rm = extract32(insn, 16, 5);
3566     bool setflags = extract32(insn, 29, 1);
3567     bool sub_op = extract32(insn, 30, 1);
3568     bool sf = extract32(insn, 31, 1);
3569
3570     TCGv_i64 tcg_rm, tcg_rn; /* temps */
3571     TCGv_i64 tcg_rd;
3572     TCGv_i64 tcg_result;
3573
3574     if (imm3 > 4) {
3575         unallocated_encoding(s);
3576         return;
3577     }
3578
3579     /* non-flag setting ops may use SP */
3580     if (!setflags) {
3581         tcg_rd = cpu_reg_sp(s, rd);
3582     } else {
3583         tcg_rd = cpu_reg(s, rd);
3584     }
3585     tcg_rn = read_cpu_reg_sp(s, rn, sf);
3586
3587     tcg_rm = read_cpu_reg(s, rm, sf);
3588     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
3589
3590     tcg_result = tcg_temp_new_i64();
3591
3592     if (!setflags) {
3593         if (sub_op) {
3594             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3595         } else {
3596             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3597         }
3598     } else {
3599         if (sub_op) {
3600             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3601         } else {
3602             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3603         }
3604     }
3605
3606     if (sf) {
3607         tcg_gen_mov_i64(tcg_rd, tcg_result);
3608     } else {
3609         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3610     }
3611
3612     tcg_temp_free_i64(tcg_result);
3613 }
3614
3615 /*
3616  * Add/subtract (shifted register)
3617  *
3618  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
3619  * +--+--+--+-----------+-----+--+-------+---------+------+------+
3620  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
3621  * +--+--+--+-----------+-----+--+-------+---------+------+------+
3622  *
3623  *    sf: 0 -> 32bit, 1 -> 64bit
3624  *    op: 0 -> add  , 1 -> sub
3625  *     S: 1 -> set flags
3626  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
3627  *  imm6: Shift amount to apply to Rm before the add/sub
3628  */
3629 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
3630 {
3631     int rd = extract32(insn, 0, 5);
3632     int rn = extract32(insn, 5, 5);
3633     int imm6 = extract32(insn, 10, 6);
3634     int rm = extract32(insn, 16, 5);
3635     int shift_type = extract32(insn, 22, 2);
3636     bool setflags = extract32(insn, 29, 1);
3637     bool sub_op = extract32(insn, 30, 1);
3638     bool sf = extract32(insn, 31, 1);
3639
3640     TCGv_i64 tcg_rd = cpu_reg(s, rd);
3641     TCGv_i64 tcg_rn, tcg_rm;
3642     TCGv_i64 tcg_result;
3643
3644     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
3645         unallocated_encoding(s);
3646         return;
3647     }
3648
3649     tcg_rn = read_cpu_reg(s, rn, sf);
3650     tcg_rm = read_cpu_reg(s, rm, sf);
3651
3652     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
3653
3654     tcg_result = tcg_temp_new_i64();
3655
3656     if (!setflags) {
3657         if (sub_op) {
3658             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3659         } else {
3660             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3661         }
3662     } else {
3663         if (sub_op) {
3664             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3665         } else {
3666             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3667         }
3668     }
3669
3670     if (sf) {
3671         tcg_gen_mov_i64(tcg_rd, tcg_result);
3672     } else {
3673         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3674     }
3675
3676     tcg_temp_free_i64(tcg_result);
3677 }
3678
3679 /* Data-processing (3 source)
3680  *
3681  *    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
3682  *  +--+------+-----------+------+------+----+------+------+------+
3683  *  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
3684  *  +--+------+-----------+------+------+----+------+------+------+
3685  */
3686 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
3687 {
3688     int rd = extract32(insn, 0, 5);
3689     int rn = extract32(insn, 5, 5);
3690     int ra = extract32(insn, 10, 5);
3691     int rm = extract32(insn, 16, 5);
3692     int op_id = (extract32(insn, 29, 3) << 4) |
3693         (extract32(insn, 21, 3) << 1) |
3694         extract32(insn, 15, 1);
3695     bool sf = extract32(insn, 31, 1);
3696     bool is_sub = extract32(op_id, 0, 1);
3697     bool is_high = extract32(op_id, 2, 1);
3698     bool is_signed = false;
3699     TCGv_i64 tcg_op1;
3700     TCGv_i64 tcg_op2;
3701     TCGv_i64 tcg_tmp;
3702
3703     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
3704     switch (op_id) {
3705     case 0x42: /* SMADDL */
3706     case 0x43: /* SMSUBL */
3707     case 0x44: /* SMULH */
3708         is_signed = true;
3709         break;
3710     case 0x0: /* MADD (32bit) */
3711     case 0x1: /* MSUB (32bit) */
3712     case 0x40: /* MADD (64bit) */
3713     case 0x41: /* MSUB (64bit) */
3714     case 0x4a: /* UMADDL */
3715     case 0x4b: /* UMSUBL */
3716     case 0x4c: /* UMULH */
3717         break;
3718     default:
3719         unallocated_encoding(s);
3720         return;
3721     }
3722
3723     if (is_high) {
3724         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
3725         TCGv_i64 tcg_rd = cpu_reg(s, rd);
3726         TCGv_i64 tcg_rn = cpu_reg(s, rn);
3727         TCGv_i64 tcg_rm = cpu_reg(s, rm);
3728
3729         if (is_signed) {
3730             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3731         } else {
3732             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3733         }
3734
3735         tcg_temp_free_i64(low_bits);
3736         return;
3737     }
3738
3739     tcg_op1 = tcg_temp_new_i64();
3740     tcg_op2 = tcg_temp_new_i64();
3741     tcg_tmp = tcg_temp_new_i64();
3742
3743     if (op_id < 0x42) {
3744         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
3745         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
3746     } else {
3747         if (is_signed) {
3748             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
3749             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
3750         } else {
3751             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
3752             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
3753         }
3754     }
3755
3756     if (ra == 31 && !is_sub) {
3757         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
3758         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
3759     } else {
3760         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
3761         if (is_sub) {
3762             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3763         } else {
3764             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3765         }
3766     }
3767
3768     if (!sf) {
3769         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
3770     }
3771
3772     tcg_temp_free_i64(tcg_op1);
3773     tcg_temp_free_i64(tcg_op2);
3774     tcg_temp_free_i64(tcg_tmp);
3775 }
3776
3777 /* Add/subtract (with carry)
3778  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15   10  9    5 4   0
3779  * +--+--+--+------------------------+------+---------+------+-----+
3780  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | opcode2 |  Rn  |  Rd |
3781  * +--+--+--+------------------------+------+---------+------+-----+
3782  *                                            [000000]
3783  */
3784
3785 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
3786 {
3787     unsigned int sf, op, setflags, rm, rn, rd;
3788     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
3789
3790     if (extract32(insn, 10, 6) != 0) {
3791         unallocated_encoding(s);
3792         return;
3793     }
3794
3795     sf = extract32(insn, 31, 1);
3796     op = extract32(insn, 30, 1);
3797     setflags = extract32(insn, 29, 1);
3798     rm = extract32(insn, 16, 5);
3799     rn = extract32(insn, 5, 5);
3800     rd = extract32(insn, 0, 5);
3801
3802     tcg_rd = cpu_reg(s, rd);
3803     tcg_rn = cpu_reg(s, rn);
3804
3805     if (op) {
3806         tcg_y = new_tmp_a64(s);
3807         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
3808     } else {
3809         tcg_y = cpu_reg(s, rm);
3810     }
3811
3812     if (setflags) {
3813         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
3814     } else {
3815         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
3816     }
3817 }
3818
3819 /* Conditional compare (immediate / register)
3820  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
3821  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3822  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
3823  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3824  *        [1]                             y                [0]       [0]
3825  */
3826 static void disas_cc(DisasContext *s, uint32_t insn)
3827 {
3828     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
3829     TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
3830     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
3831     DisasCompare c;
3832
3833     if (!extract32(insn, 29, 1)) {
3834         unallocated_encoding(s);
3835         return;
3836     }
3837     if (insn & (1 << 10 | 1 << 4)) {
3838         unallocated_encoding(s);
3839         return;
3840     }
3841     sf = extract32(insn, 31, 1);
3842     op = extract32(insn, 30, 1);
3843     is_imm = extract32(insn, 11, 1);
3844     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
3845     cond = extract32(insn, 12, 4);
3846     rn = extract32(insn, 5, 5);
3847     nzcv = extract32(insn, 0, 4);
3848
3849     /* Set T0 = !COND.  */
3850     tcg_t0 = tcg_temp_new_i32();
3851     arm_test_cc(&c, cond);
3852     tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
3853     arm_free_cc(&c);
3854
3855     /* Load the arguments for the new comparison.  */
3856     if (is_imm) {
3857         tcg_y = new_tmp_a64(s);
3858         tcg_gen_movi_i64(tcg_y, y);
3859     } else {
3860         tcg_y = cpu_reg(s, y);
3861     }
3862     tcg_rn = cpu_reg(s, rn);
3863
3864     /* Set the flags for the new comparison.  */
3865     tcg_tmp = tcg_temp_new_i64();
3866     if (op) {
3867         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3868     } else {
3869         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3870     }
3871     tcg_temp_free_i64(tcg_tmp);
3872
3873     /* If COND was false, force the flags to #nzcv.  Compute two masks
3874      * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
3875      * For tcg hosts that support ANDC, we can make do with just T1.
3876      * In either case, allow the tcg optimizer to delete any unused mask.
3877      */
3878     tcg_t1 = tcg_temp_new_i32();
3879     tcg_t2 = tcg_temp_new_i32();
3880     tcg_gen_neg_i32(tcg_t1, tcg_t0);
3881     tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
3882
3883     if (nzcv & 8) { /* N */
3884         tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
3885     } else {
3886         if (TCG_TARGET_HAS_andc_i32) {
3887             tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
3888         } else {
3889             tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
3890         }
3891     }
3892     if (nzcv & 4) { /* Z */
3893         if (TCG_TARGET_HAS_andc_i32) {
3894             tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
3895         } else {
3896             tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
3897         }
3898     } else {
3899         tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
3900     }
3901     if (nzcv & 2) { /* C */
3902         tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
3903     } else {
3904         if (TCG_TARGET_HAS_andc_i32) {
3905             tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
3906         } else {
3907             tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
3908         }
3909     }
3910     if (nzcv & 1) { /* V */
3911         tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
3912     } else {
3913         if (TCG_TARGET_HAS_andc_i32) {
3914             tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
3915         } else {
3916             tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
3917         }
3918     }
3919     tcg_temp_free_i32(tcg_t0);
3920     tcg_temp_free_i32(tcg_t1);
3921     tcg_temp_free_i32(tcg_t2);
3922 }
3923
3924 /* Conditional select
3925  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
3926  * +----+----+---+-----------------+------+------+-----+------+------+
3927  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
3928  * +----+----+---+-----------------+------+------+-----+------+------+
3929  */
3930 static void disas_cond_select(DisasContext *s, uint32_t insn)
3931 {
3932     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
3933     TCGv_i64 tcg_rd, zero;
3934     DisasCompare64 c;
3935
3936     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
3937         /* S == 1 or op2<1> == 1 */
3938         unallocated_encoding(s);
3939         return;
3940     }
3941     sf = extract32(insn, 31, 1);
3942     else_inv = extract32(insn, 30, 1);
3943     rm = extract32(insn, 16, 5);
3944     cond = extract32(insn, 12, 4);
3945     else_inc = extract32(insn, 10, 1);
3946     rn = extract32(insn, 5, 5);
3947     rd = extract32(insn, 0, 5);
3948
3949     tcg_rd = cpu_reg(s, rd);
3950
3951     a64_test_cc(&c, cond);
3952     zero = tcg_const_i64(0);
3953
3954     if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
3955         /* CSET & CSETM.  */
3956         tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
3957         if (else_inv) {
3958             tcg_gen_neg_i64(tcg_rd, tcg_rd);
3959         }
3960     } else {
3961         TCGv_i64 t_true = cpu_reg(s, rn);
3962         TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
3963         if (else_inv && else_inc) {
3964             tcg_gen_neg_i64(t_false, t_false);
3965         } else if (else_inv) {
3966             tcg_gen_not_i64(t_false, t_false);
3967         } else if (else_inc) {
3968             tcg_gen_addi_i64(t_false, t_false, 1);
3969         }
3970         tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
3971     }
3972
3973     tcg_temp_free_i64(zero);
3974     a64_free_cc(&c);
3975
3976     if (!sf) {
3977         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3978     }
3979 }
3980
3981 static void handle_clz(DisasContext *s, unsigned int sf,
3982                        unsigned int rn, unsigned int rd)
3983 {
3984     TCGv_i64 tcg_rd, tcg_rn;
3985     tcg_rd = cpu_reg(s, rd);
3986     tcg_rn = cpu_reg(s, rn);
3987
3988     if (sf) {
3989         tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
3990     } else {
3991         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3992         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
3993         tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
3994         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3995         tcg_temp_free_i32(tcg_tmp32);
3996     }
3997 }
3998
3999 static void handle_cls(DisasContext *s, unsigned int sf,
4000                        unsigned int rn, unsigned int rd)
4001 {
4002     TCGv_i64 tcg_rd, tcg_rn;
4003     tcg_rd = cpu_reg(s, rd);
4004     tcg_rn = cpu_reg(s, rn);
4005
4006     if (sf) {
4007         tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
4008     } else {
4009         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4010         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4011         tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
4012         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4013         tcg_temp_free_i32(tcg_tmp32);
4014     }
4015 }
4016
4017 static void handle_rbit(DisasContext *s, unsigned int sf,
4018                         unsigned int rn, unsigned int rd)
4019 {
4020     TCGv_i64 tcg_rd, tcg_rn;
4021     tcg_rd = cpu_reg(s, rd);
4022     tcg_rn = cpu_reg(s, rn);
4023
4024     if (sf) {
4025         gen_helper_rbit64(tcg_rd, tcg_rn);
4026     } else {
4027         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4028         tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4029         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
4030         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4031         tcg_temp_free_i32(tcg_tmp32);
4032     }
4033 }
4034
4035 /* REV with sf==1, opcode==3 ("REV64") */
4036 static void handle_rev64(DisasContext *s, unsigned int sf,
4037                          unsigned int rn, unsigned int rd)
4038 {
4039     if (!sf) {
4040         unallocated_encoding(s);
4041         return;
4042     }
4043     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
4044 }
4045
4046 /* REV with sf==0, opcode==2
4047  * REV32 (sf==1, opcode==2)
4048  */
4049 static void handle_rev32(DisasContext *s, unsigned int sf,
4050                          unsigned int rn, unsigned int rd)
4051 {
4052     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4053
4054     if (sf) {
4055         TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4056         TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4057
4058         /* bswap32_i64 requires zero high word */
4059         tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
4060         tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
4061         tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
4062         tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
4063         tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
4064
4065         tcg_temp_free_i64(tcg_tmp);
4066     } else {
4067         tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
4068         tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
4069     }
4070 }
4071
4072 /* REV16 (opcode==1) */
4073 static void handle_rev16(DisasContext *s, unsigned int sf,
4074                          unsigned int rn, unsigned int rd)
4075 {
4076     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4077     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4078     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4079     TCGv_i64 mask = tcg_const_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
4080
4081     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
4082     tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
4083     tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
4084     tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
4085     tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
4086
4087     tcg_temp_free_i64(mask);
4088     tcg_temp_free_i64(tcg_tmp);
4089 }
4090
4091 /* Data-processing (1 source)
4092  *   31  30  29  28             21 20     16 15    10 9    5 4    0
4093  * +----+---+---+-----------------+---------+--------+------+------+
4094  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
4095  * +----+---+---+-----------------+---------+--------+------+------+
4096  */
4097 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
4098 {
4099     unsigned int sf, opcode, rn, rd;
4100
4101     if (extract32(insn, 29, 1) || extract32(insn, 16, 5)) {
4102         unallocated_encoding(s);
4103         return;
4104     }
4105
4106     sf = extract32(insn, 31, 1);
4107     opcode = extract32(insn, 10, 6);
4108     rn = extract32(insn, 5, 5);
4109     rd = extract32(insn, 0, 5);
4110
4111     switch (opcode) {
4112     case 0: /* RBIT */
4113         handle_rbit(s, sf, rn, rd);
4114         break;
4115     case 1: /* REV16 */
4116         handle_rev16(s, sf, rn, rd);
4117         break;
4118     case 2: /* REV32 */
4119         handle_rev32(s, sf, rn, rd);
4120         break;
4121     case 3: /* REV64 */
4122         handle_rev64(s, sf, rn, rd);
4123         break;
4124     case 4: /* CLZ */
4125         handle_clz(s, sf, rn, rd);
4126         break;
4127     case 5: /* CLS */
4128         handle_cls(s, sf, rn, rd);
4129         break;
4130     }
4131 }
4132
4133 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
4134                        unsigned int rm, unsigned int rn, unsigned int rd)
4135 {
4136     TCGv_i64 tcg_n, tcg_m, tcg_rd;
4137     tcg_rd = cpu_reg(s, rd);
4138
4139     if (!sf && is_signed) {
4140         tcg_n = new_tmp_a64(s);
4141         tcg_m = new_tmp_a64(s);
4142         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
4143         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
4144     } else {
4145         tcg_n = read_cpu_reg(s, rn, sf);
4146         tcg_m = read_cpu_reg(s, rm, sf);
4147     }
4148
4149     if (is_signed) {
4150         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
4151     } else {
4152         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
4153     }
4154
4155     if (!sf) { /* zero extend final result */
4156         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4157     }
4158 }
4159
4160 /* LSLV, LSRV, ASRV, RORV */
4161 static void handle_shift_reg(DisasContext *s,
4162                              enum a64_shift_type shift_type, unsigned int sf,
4163                              unsigned int rm, unsigned int rn, unsigned int rd)
4164 {
4165     TCGv_i64 tcg_shift = tcg_temp_new_i64();
4166     TCGv_i64 tcg_rd = cpu_reg(s, rd);
4167     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4168
4169     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
4170     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
4171     tcg_temp_free_i64(tcg_shift);
4172 }
4173
4174 /* CRC32[BHWX], CRC32C[BHWX] */
4175 static void handle_crc32(DisasContext *s,
4176                          unsigned int sf, unsigned int sz, bool crc32c,
4177                          unsigned int rm, unsigned int rn, unsigned int rd)
4178 {
4179     TCGv_i64 tcg_acc, tcg_val;
4180     TCGv_i32 tcg_bytes;
4181
4182     if (!arm_dc_feature(s, ARM_FEATURE_CRC)
4183         || (sf == 1 && sz != 3)
4184         || (sf == 0 && sz == 3)) {
4185         unallocated_encoding(s);
4186         return;
4187     }
4188
4189     if (sz == 3) {
4190         tcg_val = cpu_reg(s, rm);
4191     } else {
4192         uint64_t mask;
4193         switch (sz) {
4194         case 0:
4195             mask = 0xFF;
4196             break;
4197         case 1:
4198             mask = 0xFFFF;
4199             break;
4200         case 2:
4201             mask = 0xFFFFFFFF;
4202             break;
4203         default:
4204             g_assert_not_reached();
4205         }
4206         tcg_val = new_tmp_a64(s);
4207         tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
4208     }
4209
4210     tcg_acc = cpu_reg(s, rn);
4211     tcg_bytes = tcg_const_i32(1 << sz);
4212
4213     if (crc32c) {
4214         gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
4215     } else {
4216         gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
4217     }
4218
4219     tcg_temp_free_i32(tcg_bytes);
4220 }
4221
4222 /* Data-processing (2 source)
4223  *   31   30  29 28             21 20  16 15    10 9    5 4    0
4224  * +----+---+---+-----------------+------+--------+------+------+
4225  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
4226  * +----+---+---+-----------------+------+--------+------+------+
4227  */
4228 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
4229 {
4230     unsigned int sf, rm, opcode, rn, rd;
4231     sf = extract32(insn, 31, 1);
4232     rm = extract32(insn, 16, 5);
4233     opcode = extract32(insn, 10, 6);
4234     rn = extract32(insn, 5, 5);
4235     rd = extract32(insn, 0, 5);
4236
4237     if (extract32(insn, 29, 1)) {
4238         unallocated_encoding(s);
4239         return;
4240     }
4241
4242     switch (opcode) {
4243     case 2: /* UDIV */
4244         handle_div(s, false, sf, rm, rn, rd);
4245         break;
4246     case 3: /* SDIV */
4247         handle_div(s, true, sf, rm, rn, rd);
4248         break;
4249     case 8: /* LSLV */
4250         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
4251         break;
4252     case 9: /* LSRV */
4253         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
4254         break;
4255     case 10: /* ASRV */
4256         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
4257         break;
4258     case 11: /* RORV */
4259         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
4260         break;
4261     case 16:
4262     case 17:
4263     case 18:
4264     case 19:
4265     case 20:
4266     case 21:
4267     case 22:
4268     case 23: /* CRC32 */
4269     {
4270         int sz = extract32(opcode, 0, 2);
4271         bool crc32c = extract32(opcode, 2, 1);
4272         handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
4273         break;
4274     }
4275     default:
4276         unallocated_encoding(s);
4277         break;
4278     }
4279 }
4280
4281 /* Data processing - register */
4282 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
4283 {
4284     switch (extract32(insn, 24, 5)) {
4285     case 0x0a: /* Logical (shifted register) */
4286         disas_logic_reg(s, insn);
4287         break;
4288     case 0x0b: /* Add/subtract */
4289         if (insn & (1 << 21)) { /* (extended register) */
4290             disas_add_sub_ext_reg(s, insn);
4291         } else {
4292             disas_add_sub_reg(s, insn);
4293         }
4294         break;
4295     case 0x1b: /* Data-processing (3 source) */
4296         disas_data_proc_3src(s, insn);
4297         break;
4298     case 0x1a:
4299         switch (extract32(insn, 21, 3)) {
4300         case 0x0: /* Add/subtract (with carry) */
4301             disas_adc_sbc(s, insn);
4302             break;
4303         case 0x2: /* Conditional compare */
4304             disas_cc(s, insn); /* both imm and reg forms */
4305             break;
4306         case 0x4: /* Conditional select */
4307             disas_cond_select(s, insn);
4308             break;
4309         case 0x6: /* Data-processing */
4310             if (insn & (1 << 30)) { /* (1 source) */
4311                 disas_data_proc_1src(s, insn);
4312             } else {            /* (2 source) */
4313                 disas_data_proc_2src(s, insn);
4314             }
4315             break;
4316         default:
4317             unallocated_encoding(s);
4318             break;
4319         }
4320         break;
4321     default:
4322         unallocated_encoding(s);
4323         break;
4324     }
4325 }
4326
4327 static void handle_fp_compare(DisasContext *s, bool is_double,
4328                               unsigned int rn, unsigned int rm,
4329                               bool cmp_with_zero, bool signal_all_nans)
4330 {
4331     TCGv_i64 tcg_flags = tcg_temp_new_i64();
4332     TCGv_ptr fpst = get_fpstatus_ptr();
4333
4334     if (is_double) {
4335         TCGv_i64 tcg_vn, tcg_vm;
4336
4337         tcg_vn = read_fp_dreg(s, rn);
4338         if (cmp_with_zero) {
4339             tcg_vm = tcg_const_i64(0);
4340         } else {
4341             tcg_vm = read_fp_dreg(s, rm);
4342         }
4343         if (signal_all_nans) {
4344             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4345         } else {
4346             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4347         }
4348         tcg_temp_free_i64(tcg_vn);
4349         tcg_temp_free_i64(tcg_vm);
4350     } else {
4351         TCGv_i32 tcg_vn, tcg_vm;
4352
4353         tcg_vn = read_fp_sreg(s, rn);
4354         if (cmp_with_zero) {
4355             tcg_vm = tcg_const_i32(0);
4356         } else {
4357             tcg_vm = read_fp_sreg(s, rm);
4358         }
4359         if (signal_all_nans) {
4360             gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4361         } else {
4362             gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4363         }
4364         tcg_temp_free_i32(tcg_vn);
4365         tcg_temp_free_i32(tcg_vm);
4366     }
4367
4368     tcg_temp_free_ptr(fpst);
4369
4370     gen_set_nzcv(tcg_flags);
4371
4372     tcg_temp_free_i64(tcg_flags);
4373 }
4374
4375 /* Floating point compare
4376  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
4377  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
4378  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
4379  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
4380  */
4381 static void disas_fp_compare(DisasContext *s, uint32_t insn)
4382 {
4383     unsigned int mos, type, rm, op, rn, opc, op2r;
4384
4385     mos = extract32(insn, 29, 3);
4386     type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4387     rm = extract32(insn, 16, 5);
4388     op = extract32(insn, 14, 2);
4389     rn = extract32(insn, 5, 5);
4390     opc = extract32(insn, 3, 2);
4391     op2r = extract32(insn, 0, 3);
4392
4393     if (mos || op || op2r || type > 1) {
4394         unallocated_encoding(s);
4395         return;
4396     }
4397
4398     if (!fp_access_check(s)) {
4399         return;
4400     }
4401
4402     handle_fp_compare(s, type, rn, rm, opc & 1, opc & 2);
4403 }
4404
4405 /* Floating point conditional compare
4406  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
4407  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
4408  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
4409  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
4410  */
4411 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
4412 {
4413     unsigned int mos, type, rm, cond, rn, op, nzcv;
4414     TCGv_i64 tcg_flags;
4415     TCGLabel *label_continue = NULL;
4416
4417     mos = extract32(insn, 29, 3);
4418     type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4419     rm = extract32(insn, 16, 5);
4420     cond = extract32(insn, 12, 4);
4421     rn = extract32(insn, 5, 5);
4422     op = extract32(insn, 4, 1);
4423     nzcv = extract32(insn, 0, 4);
4424
4425     if (mos || type > 1) {
4426         unallocated_encoding(s);
4427         return;
4428     }
4429
4430     if (!fp_access_check(s)) {
4431         return;
4432     }
4433
4434     if (cond < 0x0e) { /* not always */
4435         TCGLabel *label_match = gen_new_label();
4436         label_continue = gen_new_label();
4437         arm_gen_test_cc(cond, label_match);
4438         /* nomatch: */
4439         tcg_flags = tcg_const_i64(nzcv << 28);
4440         gen_set_nzcv(tcg_flags);
4441         tcg_temp_free_i64(tcg_flags);
4442         tcg_gen_br(label_continue);
4443         gen_set_label(label_match);
4444     }
4445
4446     handle_fp_compare(s, type, rn, rm, false, op);
4447
4448     if (cond < 0x0e) {
4449         gen_set_label(label_continue);
4450     }
4451 }
4452
4453 /* Floating point conditional select
4454  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
4455  * +---+---+---+-----------+------+---+------+------+-----+------+------+
4456  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
4457  * +---+---+---+-----------+------+---+------+------+-----+------+------+
4458  */
4459 static void disas_fp_csel(DisasContext *s, uint32_t insn)
4460 {
4461     unsigned int mos, type, rm, cond, rn, rd;
4462     TCGv_i64 t_true, t_false, t_zero;
4463     DisasCompare64 c;
4464
4465     mos = extract32(insn, 29, 3);
4466     type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4467     rm = extract32(insn, 16, 5);
4468     cond = extract32(insn, 12, 4);
4469     rn = extract32(insn, 5, 5);
4470     rd = extract32(insn, 0, 5);
4471
4472     if (mos || type > 1) {
4473         unallocated_encoding(s);
4474         return;
4475     }
4476
4477     if (!fp_access_check(s)) {
4478         return;
4479     }
4480
4481     /* Zero extend sreg inputs to 64 bits now.  */
4482     t_true = tcg_temp_new_i64();
4483     t_false = tcg_temp_new_i64();
4484     read_vec_element(s, t_true, rn, 0, type ? MO_64 : MO_32);
4485     read_vec_element(s, t_false, rm, 0, type ? MO_64 : MO_32);
4486
4487     a64_test_cc(&c, cond);
4488     t_zero = tcg_const_i64(0);
4489     tcg_gen_movcond_i64(c.cond, t_true, c.value, t_zero, t_true, t_false);
4490     tcg_temp_free_i64(t_zero);
4491     tcg_temp_free_i64(t_false);
4492     a64_free_cc(&c);
4493
4494     /* Note that sregs write back zeros to the high bits,
4495        and we've already done the zero-extension.  */
4496     write_fp_dreg(s, rd, t_true);
4497     tcg_temp_free_i64(t_true);
4498 }
4499
4500 /* Floating-point data-processing (1 source) - single precision */
4501 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
4502 {
4503     TCGv_ptr fpst;
4504     TCGv_i32 tcg_op;
4505     TCGv_i32 tcg_res;
4506
4507     fpst = get_fpstatus_ptr();
4508     tcg_op = read_fp_sreg(s, rn);
4509     tcg_res = tcg_temp_new_i32();
4510
4511     switch (opcode) {
4512     case 0x0: /* FMOV */
4513         tcg_gen_mov_i32(tcg_res, tcg_op);
4514         break;
4515     case 0x1: /* FABS */
4516         gen_helper_vfp_abss(tcg_res, tcg_op);
4517         break;
4518     case 0x2: /* FNEG */
4519         gen_helper_vfp_negs(tcg_res, tcg_op);
4520         break;
4521     case 0x3: /* FSQRT */
4522         gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
4523         break;
4524     case 0x8: /* FRINTN */
4525     case 0x9: /* FRINTP */
4526     case 0xa: /* FRINTM */
4527     case 0xb: /* FRINTZ */
4528     case 0xc: /* FRINTA */
4529     {
4530         TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4531
4532         gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4533         gen_helper_rints(tcg_res, tcg_op, fpst);
4534
4535         gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4536         tcg_temp_free_i32(tcg_rmode);
4537         break;
4538     }
4539     case 0xe: /* FRINTX */
4540         gen_helper_rints_exact(tcg_res, tcg_op, fpst);
4541         break;
4542     case 0xf: /* FRINTI */
4543         gen_helper_rints(tcg_res, tcg_op, fpst);
4544         break;
4545     default:
4546         abort();
4547     }
4548
4549     write_fp_sreg(s, rd, tcg_res);
4550
4551     tcg_temp_free_ptr(fpst);
4552     tcg_temp_free_i32(tcg_op);
4553     tcg_temp_free_i32(tcg_res);
4554 }
4555
4556 /* Floating-point data-processing (1 source) - double precision */
4557 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
4558 {
4559     TCGv_ptr fpst;
4560     TCGv_i64 tcg_op;
4561     TCGv_i64 tcg_res;
4562
4563     fpst = get_fpstatus_ptr();
4564     tcg_op = read_fp_dreg(s, rn);
4565     tcg_res = tcg_temp_new_i64();
4566
4567     switch (opcode) {
4568     case 0x0: /* FMOV */
4569         tcg_gen_mov_i64(tcg_res, tcg_op);
4570         break;
4571     case 0x1: /* FABS */
4572         gen_helper_vfp_absd(tcg_res, tcg_op);
4573         break;
4574     case 0x2: /* FNEG */
4575         gen_helper_vfp_negd(tcg_res, tcg_op);
4576         break;
4577     case 0x3: /* FSQRT */
4578         gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
4579         break;
4580     case 0x8: /* FRINTN */
4581     case 0x9: /* FRINTP */
4582     case 0xa: /* FRINTM */
4583     case 0xb: /* FRINTZ */
4584     case 0xc: /* FRINTA */
4585     {
4586         TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4587
4588         gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4589         gen_helper_rintd(tcg_res, tcg_op, fpst);
4590
4591         gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4592         tcg_temp_free_i32(tcg_rmode);
4593         break;
4594     }
4595     case 0xe: /* FRINTX */
4596         gen_helper_rintd_exact(tcg_res, tcg_op, fpst);
4597         break;
4598     case 0xf: /* FRINTI */
4599         gen_helper_rintd(tcg_res, tcg_op, fpst);
4600         break;
4601     default:
4602         abort();
4603     }
4604
4605     write_fp_dreg(s, rd, tcg_res);
4606
4607     tcg_temp_free_ptr(fpst);
4608     tcg_temp_free_i64(tcg_op);
4609     tcg_temp_free_i64(tcg_res);
4610 }
4611
4612 static void handle_fp_fcvt(DisasContext *s, int opcode,
4613                            int rd, int rn, int dtype, int ntype)
4614 {
4615     switch (ntype) {
4616     case 0x0:
4617     {
4618         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4619         if (dtype == 1) {
4620             /* Single to double */
4621             TCGv_i64 tcg_rd = tcg_temp_new_i64();
4622             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
4623             write_fp_dreg(s, rd, tcg_rd);
4624             tcg_temp_free_i64(tcg_rd);
4625         } else {
4626             /* Single to half */
4627             TCGv_i32 tcg_rd = tcg_temp_new_i32();
4628             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, cpu_env);
4629             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4630             write_fp_sreg(s, rd, tcg_rd);
4631             tcg_temp_free_i32(tcg_rd);
4632         }
4633         tcg_temp_free_i32(tcg_rn);
4634         break;
4635     }
4636     case 0x1:
4637     {
4638         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
4639         TCGv_i32 tcg_rd = tcg_temp_new_i32();
4640         if (dtype == 0) {
4641             /* Double to single */
4642             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
4643         } else {
4644             /* Double to half */
4645             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, cpu_env);
4646             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4647         }
4648         write_fp_sreg(s, rd, tcg_rd);
4649         tcg_temp_free_i32(tcg_rd);
4650         tcg_temp_free_i64(tcg_rn);
4651         break;
4652     }
4653     case 0x3:
4654     {
4655         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4656         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
4657         if (dtype == 0) {
4658             /* Half to single */
4659             TCGv_i32 tcg_rd = tcg_temp_new_i32();
4660             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, cpu_env);
4661             write_fp_sreg(s, rd, tcg_rd);
4662             tcg_temp_free_i32(tcg_rd);
4663         } else {
4664             /* Half to double */
4665             TCGv_i64 tcg_rd = tcg_temp_new_i64();
4666             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, cpu_env);
4667             write_fp_dreg(s, rd, tcg_rd);
4668             tcg_temp_free_i64(tcg_rd);
4669         }
4670         tcg_temp_free_i32(tcg_rn);
4671         break;
4672     }
4673     default:
4674         abort();
4675     }
4676 }
4677
4678 /* Floating point data-processing (1 source)
4679  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
4680  * +---+---+---+-----------+------+---+--------+-----------+------+------+
4681  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
4682  * +---+---+---+-----------+------+---+--------+-----------+------+------+
4683  */
4684 static void disas_fp_1src(DisasContext *s, uint32_t insn)
4685 {
4686     int type = extract32(insn, 22, 2);
4687     int opcode = extract32(insn, 15, 6);
4688     int rn = extract32(insn, 5, 5);
4689     int rd = extract32(insn, 0, 5);
4690
4691     switch (opcode) {
4692     case 0x4: case 0x5: case 0x7:
4693     {
4694         /* FCVT between half, single and double precision */
4695         int dtype = extract32(opcode, 0, 2);
4696         if (type == 2 || dtype == type) {
4697             unallocated_encoding(s);
4698             return;
4699         }
4700         if (!fp_access_check(s)) {
4701             return;
4702         }
4703
4704         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
4705         break;
4706     }
4707     case 0x0 ... 0x3:
4708     case 0x8 ... 0xc:
4709     case 0xe ... 0xf:
4710         /* 32-to-32 and 64-to-64 ops */
4711         switch (type) {
4712         case 0:
4713             if (!fp_access_check(s)) {
4714                 return;
4715             }
4716
4717             handle_fp_1src_single(s, opcode, rd, rn);
4718             break;
4719         case 1:
4720             if (!fp_access_check(s)) {
4721                 return;
4722             }
4723
4724             handle_fp_1src_double(s, opcode, rd, rn);
4725             break;
4726         default:
4727             unallocated_encoding(s);
4728         }
4729         break;
4730     default:
4731         unallocated_encoding(s);
4732         break;
4733     }
4734 }
4735
4736 /* Floating-point data-processing (2 source) - single precision */
4737 static void handle_fp_2src_single(DisasContext *s, int opcode,
4738                                   int rd, int rn, int rm)
4739 {
4740     TCGv_i32 tcg_op1;
4741     TCGv_i32 tcg_op2;
4742     TCGv_i32 tcg_res;
4743     TCGv_ptr fpst;
4744
4745     tcg_res = tcg_temp_new_i32();
4746     fpst = get_fpstatus_ptr();
4747     tcg_op1 = read_fp_sreg(s, rn);
4748     tcg_op2 = read_fp_sreg(s, rm);
4749
4750     switch (opcode) {
4751     case 0x0: /* FMUL */
4752         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4753         break;
4754     case 0x1: /* FDIV */
4755         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
4756         break;
4757     case 0x2: /* FADD */
4758         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
4759         break;
4760     case 0x3: /* FSUB */
4761         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
4762         break;
4763     case 0x4: /* FMAX */
4764         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
4765         break;
4766     case 0x5: /* FMIN */
4767         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
4768         break;
4769     case 0x6: /* FMAXNM */
4770         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
4771         break;
4772     case 0x7: /* FMINNM */
4773         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
4774         break;
4775     case 0x8: /* FNMUL */
4776         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4777         gen_helper_vfp_negs(tcg_res, tcg_res);
4778         break;
4779     }
4780
4781     write_fp_sreg(s, rd, tcg_res);
4782
4783     tcg_temp_free_ptr(fpst);
4784     tcg_temp_free_i32(tcg_op1);
4785     tcg_temp_free_i32(tcg_op2);
4786     tcg_temp_free_i32(tcg_res);
4787 }
4788
4789 /* Floating-point data-processing (2 source) - double precision */
4790 static void handle_fp_2src_double(DisasContext *s, int opcode,
4791                                   int rd, int rn, int rm)
4792 {
4793     TCGv_i64 tcg_op1;
4794     TCGv_i64 tcg_op2;
4795     TCGv_i64 tcg_res;
4796     TCGv_ptr fpst;
4797
4798     tcg_res = tcg_temp_new_i64();
4799     fpst = get_fpstatus_ptr();
4800     tcg_op1 = read_fp_dreg(s, rn);
4801     tcg_op2 = read_fp_dreg(s, rm);
4802
4803     switch (opcode) {
4804     case 0x0: /* FMUL */
4805         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4806         break;
4807     case 0x1: /* FDIV */
4808         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
4809         break;
4810     case 0x2: /* FADD */
4811         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
4812         break;
4813     case 0x3: /* FSUB */
4814         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
4815         break;
4816     case 0x4: /* FMAX */
4817         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
4818         break;
4819     case 0x5: /* FMIN */
4820         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
4821         break;
4822     case 0x6: /* FMAXNM */
4823         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4824         break;
4825     case 0x7: /* FMINNM */
4826         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4827         break;
4828     case 0x8: /* FNMUL */
4829         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4830         gen_helper_vfp_negd(tcg_res, tcg_res);
4831         break;
4832     }
4833
4834     write_fp_dreg(s, rd, tcg_res);
4835
4836     tcg_temp_free_ptr(fpst);
4837     tcg_temp_free_i64(tcg_op1);
4838     tcg_temp_free_i64(tcg_op2);
4839     tcg_temp_free_i64(tcg_res);
4840 }
4841
4842 /* Floating point data-processing (2 source)
4843  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
4844  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4845  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
4846  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4847  */
4848 static void disas_fp_2src(DisasContext *s, uint32_t insn)
4849 {
4850     int type = extract32(insn, 22, 2);
4851     int rd = extract32(insn, 0, 5);
4852     int rn = extract32(insn, 5, 5);
4853     int rm = extract32(insn, 16, 5);
4854     int opcode = extract32(insn, 12, 4);
4855
4856     if (opcode > 8) {
4857         unallocated_encoding(s);
4858         return;
4859     }
4860
4861     switch (type) {
4862     case 0:
4863         if (!fp_access_check(s)) {
4864             return;
4865         }
4866         handle_fp_2src_single(s, opcode, rd, rn, rm);
4867         break;
4868     case 1:
4869         if (!fp_access_check(s)) {
4870             return;
4871         }
4872         handle_fp_2src_double(s, opcode, rd, rn, rm);
4873         break;
4874     default:
4875         unallocated_encoding(s);
4876     }
4877 }
4878
4879 /* Floating-point data-processing (3 source) - single precision */
4880 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
4881                                   int rd, int rn, int rm, int ra)
4882 {
4883     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
4884     TCGv_i32 tcg_res = tcg_temp_new_i32();
4885     TCGv_ptr fpst = get_fpstatus_ptr();
4886
4887     tcg_op1 = read_fp_sreg(s, rn);
4888     tcg_op2 = read_fp_sreg(s, rm);
4889     tcg_op3 = read_fp_sreg(s, ra);
4890
4891     /* These are fused multiply-add, and must be done as one
4892      * floating point operation with no rounding between the
4893      * multiplication and addition steps.
4894      * NB that doing the negations here as separate steps is
4895      * correct : an input NaN should come out with its sign bit
4896      * flipped if it is a negated-input.
4897      */
4898     if (o1 == true) {
4899         gen_helper_vfp_negs(tcg_op3, tcg_op3);
4900     }
4901
4902     if (o0 != o1) {
4903         gen_helper_vfp_negs(tcg_op1, tcg_op1);
4904     }
4905
4906     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4907
4908     write_fp_sreg(s, rd, tcg_res);
4909
4910     tcg_temp_free_ptr(fpst);
4911     tcg_temp_free_i32(tcg_op1);
4912     tcg_temp_free_i32(tcg_op2);
4913     tcg_temp_free_i32(tcg_op3);
4914     tcg_temp_free_i32(tcg_res);
4915 }
4916
4917 /* Floating-point data-processing (3 source) - double precision */
4918 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
4919                                   int rd, int rn, int rm, int ra)
4920 {
4921     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
4922     TCGv_i64 tcg_res = tcg_temp_new_i64();
4923     TCGv_ptr fpst = get_fpstatus_ptr();
4924
4925     tcg_op1 = read_fp_dreg(s, rn);
4926     tcg_op2 = read_fp_dreg(s, rm);
4927     tcg_op3 = read_fp_dreg(s, ra);
4928
4929     /* These are fused multiply-add, and must be done as one
4930      * floating point operation with no rounding between the
4931      * multiplication and addition steps.
4932      * NB that doing the negations here as separate steps is
4933      * correct : an input NaN should come out with its sign bit
4934      * flipped if it is a negated-input.
4935      */
4936     if (o1 == true) {
4937         gen_helper_vfp_negd(tcg_op3, tcg_op3);
4938     }
4939
4940     if (o0 != o1) {
4941         gen_helper_vfp_negd(tcg_op1, tcg_op1);
4942     }
4943
4944     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4945
4946     write_fp_dreg(s, rd, tcg_res);
4947
4948     tcg_temp_free_ptr(fpst);
4949     tcg_temp_free_i64(tcg_op1);
4950     tcg_temp_free_i64(tcg_op2);
4951     tcg_temp_free_i64(tcg_op3);
4952     tcg_temp_free_i64(tcg_res);
4953 }
4954
4955 /* Floating point data-processing (3 source)
4956  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
4957  * +---+---+---+-----------+------+----+------+----+------+------+------+
4958  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
4959  * +---+---+---+-----------+------+----+------+----+------+------+------+
4960  */
4961 static void disas_fp_3src(DisasContext *s, uint32_t insn)
4962 {
4963     int type = extract32(insn, 22, 2);
4964     int rd = extract32(insn, 0, 5);
4965     int rn = extract32(insn, 5, 5);
4966     int ra = extract32(insn, 10, 5);
4967     int rm = extract32(insn, 16, 5);
4968     bool o0 = extract32(insn, 15, 1);
4969     bool o1 = extract32(insn, 21, 1);
4970
4971     switch (type) {
4972     case 0:
4973         if (!fp_access_check(s)) {
4974             return;
4975         }
4976         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
4977         break;
4978     case 1:
4979         if (!fp_access_check(s)) {
4980             return;
4981         }
4982         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
4983         break;
4984     default:
4985         unallocated_encoding(s);
4986     }
4987 }
4988
4989 /* Floating point immediate
4990  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
4991  * +---+---+---+-----------+------+---+------------+-------+------+------+
4992  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
4993  * +---+---+---+-----------+------+---+------------+-------+------+------+
4994  */
4995 static void disas_fp_imm(DisasContext *s, uint32_t insn)
4996 {
4997     int rd = extract32(insn, 0, 5);
4998     int imm8 = extract32(insn, 13, 8);
4999     int is_double = extract32(insn, 22, 2);
5000     uint64_t imm;
5001     TCGv_i64 tcg_res;
5002
5003     if (is_double > 1) {
5004         unallocated_encoding(s);
5005         return;
5006     }
5007
5008     if (!fp_access_check(s)) {
5009         return;
5010     }
5011
5012     /* The imm8 encodes the sign bit, enough bits to represent
5013      * an exponent in the range 01....1xx to 10....0xx,
5014      * and the most significant 4 bits of the mantissa; see
5015      * VFPExpandImm() in the v8 ARM ARM.
5016      */
5017     if (is_double) {
5018         imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
5019             (extract32(imm8, 6, 1) ? 0x3fc0 : 0x4000) |
5020             extract32(imm8, 0, 6);
5021         imm <<= 48;
5022     } else {
5023         imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
5024             (extract32(imm8, 6, 1) ? 0x3e00 : 0x4000) |
5025             (extract32(imm8, 0, 6) << 3);
5026         imm <<= 16;
5027     }
5028
5029     tcg_res = tcg_const_i64(imm);
5030     write_fp_dreg(s, rd, tcg_res);
5031     tcg_temp_free_i64(tcg_res);
5032 }
5033
5034 /* Handle floating point <=> fixed point conversions. Note that we can
5035  * also deal with fp <=> integer conversions as a special case (scale == 64)
5036  * OPTME: consider handling that special case specially or at least skipping
5037  * the call to scalbn in the helpers for zero shifts.
5038  */
5039 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
5040                            bool itof, int rmode, int scale, int sf, int type)
5041 {
5042     bool is_signed = !(opcode & 1);
5043     bool is_double = type;
5044     TCGv_ptr tcg_fpstatus;
5045     TCGv_i32 tcg_shift;
5046
5047     tcg_fpstatus = get_fpstatus_ptr();
5048
5049     tcg_shift = tcg_const_i32(64 - scale);
5050
5051     if (itof) {
5052         TCGv_i64 tcg_int = cpu_reg(s, rn);
5053         if (!sf) {
5054             TCGv_i64 tcg_extend = new_tmp_a64(s);
5055
5056             if (is_signed) {
5057                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
5058             } else {
5059                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
5060             }
5061
5062             tcg_int = tcg_extend;
5063         }
5064
5065         if (is_double) {
5066             TCGv_i64 tcg_double = tcg_temp_new_i64();
5067             if (is_signed) {
5068                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
5069                                      tcg_shift, tcg_fpstatus);
5070             } else {
5071                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
5072                                      tcg_shift, tcg_fpstatus);
5073             }
5074             write_fp_dreg(s, rd, tcg_double);
5075             tcg_temp_free_i64(tcg_double);
5076         } else {
5077             TCGv_i32 tcg_single = tcg_temp_new_i32();
5078             if (is_signed) {
5079                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
5080                                      tcg_shift, tcg_fpstatus);
5081             } else {
5082                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
5083                                      tcg_shift, tcg_fpstatus);
5084             }
5085             write_fp_sreg(s, rd, tcg_single);
5086             tcg_temp_free_i32(tcg_single);
5087         }
5088     } else {
5089         TCGv_i64 tcg_int = cpu_reg(s, rd);
5090         TCGv_i32 tcg_rmode;
5091
5092         if (extract32(opcode, 2, 1)) {
5093             /* There are too many rounding modes to all fit into rmode,
5094              * so FCVTA[US] is a special case.
5095              */
5096             rmode = FPROUNDING_TIEAWAY;
5097         }
5098
5099         tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
5100
5101         gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
5102
5103         if (is_double) {
5104             TCGv_i64 tcg_double = read_fp_dreg(s, rn);
5105             if (is_signed) {
5106                 if (!sf) {
5107                     gen_helper_vfp_tosld(tcg_int, tcg_double,
5108                                          tcg_shift, tcg_fpstatus);
5109                 } else {
5110                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
5111                                          tcg_shift, tcg_fpstatus);
5112                 }
5113             } else {
5114                 if (!sf) {
5115                     gen_helper_vfp_tould(tcg_int, tcg_double,
5116                                          tcg_shift, tcg_fpstatus);
5117                 } else {
5118                     gen_helper_vfp_touqd(tcg_int, tcg_double,
5119                                          tcg_shift, tcg_fpstatus);
5120                 }
5121             }
5122             tcg_temp_free_i64(tcg_double);
5123         } else {
5124             TCGv_i32 tcg_single = read_fp_sreg(s, rn);
5125             if (sf) {
5126                 if (is_signed) {
5127                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
5128                                          tcg_shift, tcg_fpstatus);
5129                 } else {
5130                     gen_helper_vfp_touqs(tcg_int, tcg_single,
5131                                          tcg_shift, tcg_fpstatus);
5132                 }
5133             } else {
5134                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
5135                 if (is_signed) {
5136                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
5137                                          tcg_shift, tcg_fpstatus);
5138                 } else {
5139                     gen_helper_vfp_touls(tcg_dest, tcg_single,
5140                                          tcg_shift, tcg_fpstatus);
5141                 }
5142                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
5143                 tcg_temp_free_i32(tcg_dest);
5144             }
5145             tcg_temp_free_i32(tcg_single);
5146         }
5147
5148         gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
5149         tcg_temp_free_i32(tcg_rmode);
5150
5151         if (!sf) {
5152             tcg_gen_ext32u_i64(tcg_int, tcg_int);
5153         }
5154     }
5155
5156     tcg_temp_free_ptr(tcg_fpstatus);
5157     tcg_temp_free_i32(tcg_shift);
5158 }
5159
5160 /* Floating point <-> fixed point conversions
5161  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
5162  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
5163  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
5164  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
5165  */
5166 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
5167 {
5168     int rd = extract32(insn, 0, 5);
5169     int rn = extract32(insn, 5, 5);
5170     int scale = extract32(insn, 10, 6);
5171     int opcode = extract32(insn, 16, 3);
5172     int rmode = extract32(insn, 19, 2);
5173     int type = extract32(insn, 22, 2);
5174     bool sbit = extract32(insn, 29, 1);
5175     bool sf = extract32(insn, 31, 1);
5176     bool itof;
5177
5178     if (sbit || (type > 1)
5179         || (!sf && scale < 32)) {
5180         unallocated_encoding(s);
5181         return;
5182     }
5183
5184     switch ((rmode << 3) | opcode) {
5185     case 0x2: /* SCVTF */
5186     case 0x3: /* UCVTF */
5187         itof = true;
5188         break;
5189     case 0x18: /* FCVTZS */
5190     case 0x19: /* FCVTZU */
5191         itof = false;
5192         break;
5193     default:
5194         unallocated_encoding(s);
5195         return;
5196     }
5197
5198     if (!fp_access_check(s)) {
5199         return;
5200     }
5201
5202     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
5203 }
5204
5205 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
5206 {
5207     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
5208      * without conversion.
5209      */
5210
5211     if (itof) {
5212         TCGv_i64 tcg_rn = cpu_reg(s, rn);
5213
5214         switch (type) {
5215         case 0:
5216         {
5217             /* 32 bit */
5218             TCGv_i64 tmp = tcg_temp_new_i64();
5219             tcg_gen_ext32u_i64(tmp, tcg_rn);
5220             tcg_gen_st_i64(tmp, cpu_env, fp_reg_offset(s, rd, MO_64));
5221             tcg_gen_movi_i64(tmp, 0);
5222             tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(s, rd));
5223             tcg_temp_free_i64(tmp);
5224             break;
5225         }
5226         case 1:
5227         {
5228             /* 64 bit */
5229             TCGv_i64 tmp = tcg_const_i64(0);
5230             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_offset(s, rd, MO_64));
5231             tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(s, rd));
5232             tcg_temp_free_i64(tmp);
5233             break;
5234         }
5235         case 2:
5236             /* 64 bit to top half. */
5237             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
5238             break;
5239         }
5240     } else {
5241         TCGv_i64 tcg_rd = cpu_reg(s, rd);
5242
5243         switch (type) {
5244         case 0:
5245             /* 32 bit */
5246             tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
5247             break;
5248         case 1:
5249             /* 64 bit */
5250             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
5251             break;
5252         case 2:
5253             /* 64 bits from top half */
5254             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
5255             break;
5256         }
5257     }
5258 }
5259
5260 /* Floating point <-> integer conversions
5261  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
5262  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
5263  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
5264  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
5265  */
5266 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
5267 {
5268     int rd = extract32(insn, 0, 5);
5269     int rn = extract32(insn, 5, 5);
5270     int opcode = extract32(insn, 16, 3);
5271     int rmode = extract32(insn, 19, 2);
5272     int type = extract32(insn, 22, 2);
5273     bool sbit = extract32(insn, 29, 1);
5274     bool sf = extract32(insn, 31, 1);
5275
5276     if (sbit) {
5277         unallocated_encoding(s);
5278         return;
5279     }
5280
5281     if (opcode > 5) {
5282         /* FMOV */
5283         bool itof = opcode & 1;
5284
5285         if (rmode >= 2) {
5286             unallocated_encoding(s);
5287             return;
5288         }
5289
5290         switch (sf << 3 | type << 1 | rmode) {
5291         case 0x0: /* 32 bit */
5292         case 0xa: /* 64 bit */
5293         case 0xd: /* 64 bit to top half of quad */
5294             break;
5295         default:
5296             /* all other sf/type/rmode combinations are invalid */
5297             unallocated_encoding(s);
5298             break;
5299         }
5300
5301         if (!fp_access_check(s)) {
5302             return;
5303         }
5304         handle_fmov(s, rd, rn, type, itof);
5305     } else {
5306         /* actual FP conversions */
5307         bool itof = extract32(opcode, 1, 1);
5308
5309         if (type > 1 || (rmode != 0 && opcode > 1)) {
5310             unallocated_encoding(s);
5311             return;
5312         }
5313
5314         if (!fp_access_check(s)) {
5315             return;
5316         }
5317         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
5318     }
5319 }
5320
5321 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
5322  *   31  30  29 28     25 24                          0
5323  * +---+---+---+---------+-----------------------------+
5324  * |   | 0 |   | 1 1 1 1 |                             |
5325  * +---+---+---+---------+-----------------------------+
5326  */
5327 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
5328 {
5329     if (extract32(insn, 24, 1)) {
5330         /* Floating point data-processing (3 source) */
5331         disas_fp_3src(s, insn);
5332     } else if (extract32(insn, 21, 1) == 0) {
5333         /* Floating point to fixed point conversions */
5334         disas_fp_fixed_conv(s, insn);
5335     } else {
5336         switch (extract32(insn, 10, 2)) {
5337         case 1:
5338             /* Floating point conditional compare */
5339             disas_fp_ccomp(s, insn);
5340             break;
5341         case 2:
5342             /* Floating point data-processing (2 source) */
5343             disas_fp_2src(s, insn);
5344             break;
5345         case 3:
5346             /* Floating point conditional select */
5347             disas_fp_csel(s, insn);
5348             break;
5349         case 0:
5350             switch (ctz32(extract32(insn, 12, 4))) {
5351             case 0: /* [15:12] == xxx1 */
5352                 /* Floating point immediate */
5353                 disas_fp_imm(s, insn);
5354                 break;
5355             case 1: /* [15:12] == xx10 */
5356                 /* Floating point compare */
5357                 disas_fp_compare(s, insn);
5358                 break;
5359             case 2: /* [15:12] == x100 */
5360                 /* Floating point data-processing (1 source) */
5361                 disas_fp_1src(s, insn);
5362                 break;
5363             case 3: /* [15:12] == 1000 */
5364                 unallocated_encoding(s);
5365                 break;
5366             default: /* [15:12] == 0000 */
5367                 /* Floating point <-> integer conversions */
5368                 disas_fp_int_conv(s, insn);
5369                 break;
5370             }
5371             break;
5372         }
5373     }
5374 }
5375
5376 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
5377                      int pos)
5378 {
5379     /* Extract 64 bits from the middle of two concatenated 64 bit
5380      * vector register slices left:right. The extracted bits start
5381      * at 'pos' bits into the right (least significant) side.
5382      * We return the result in tcg_right, and guarantee not to
5383      * trash tcg_left.
5384      */
5385     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5386     assert(pos > 0 && pos < 64);
5387
5388     tcg_gen_shri_i64(tcg_right, tcg_right, pos);
5389     tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
5390     tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
5391
5392     tcg_temp_free_i64(tcg_tmp);
5393 }
5394
5395 /* EXT
5396  *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
5397  * +---+---+-------------+-----+---+------+---+------+---+------+------+
5398  * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
5399  * +---+---+-------------+-----+---+------+---+------+---+------+------+
5400  */
5401 static void disas_simd_ext(DisasContext *s, uint32_t insn)
5402 {
5403     int is_q = extract32(insn, 30, 1);
5404     int op2 = extract32(insn, 22, 2);
5405     int imm4 = extract32(insn, 11, 4);
5406     int rm = extract32(insn, 16, 5);
5407     int rn = extract32(insn, 5, 5);
5408     int rd = extract32(insn, 0, 5);
5409     int pos = imm4 << 3;
5410     TCGv_i64 tcg_resl, tcg_resh;
5411
5412     if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
5413         unallocated_encoding(s);
5414         return;
5415     }
5416
5417     if (!fp_access_check(s)) {
5418         return;
5419     }
5420
5421     tcg_resh = tcg_temp_new_i64();
5422     tcg_resl = tcg_temp_new_i64();
5423
5424     /* Vd gets bits starting at pos bits into Vm:Vn. This is
5425      * either extracting 128 bits from a 128:128 concatenation, or
5426      * extracting 64 bits from a 64:64 concatenation.
5427      */
5428     if (!is_q) {
5429         read_vec_element(s, tcg_resl, rn, 0, MO_64);
5430         if (pos != 0) {
5431             read_vec_element(s, tcg_resh, rm, 0, MO_64);
5432             do_ext64(s, tcg_resh, tcg_resl, pos);
5433         }
5434         tcg_gen_movi_i64(tcg_resh, 0);
5435     } else {
5436         TCGv_i64 tcg_hh;
5437         typedef struct {
5438             int reg;
5439             int elt;
5440         } EltPosns;
5441         EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
5442         EltPosns *elt = eltposns;
5443
5444         if (pos >= 64) {
5445             elt++;
5446             pos -= 64;
5447         }
5448
5449         read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
5450         elt++;
5451         read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
5452         elt++;
5453         if (pos != 0) {
5454             do_ext64(s, tcg_resh, tcg_resl, pos);
5455             tcg_hh = tcg_temp_new_i64();
5456             read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
5457             do_ext64(s, tcg_hh, tcg_resh, pos);
5458             tcg_temp_free_i64(tcg_hh);
5459         }
5460     }
5461
5462     write_vec_element(s, tcg_resl, rd, 0, MO_64);
5463     tcg_temp_free_i64(tcg_resl);
5464     write_vec_element(s, tcg_resh, rd, 1, MO_64);
5465     tcg_temp_free_i64(tcg_resh);
5466 }
5467
5468 /* TBL/TBX
5469  *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
5470  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
5471  * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
5472  * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
5473  */
5474 static void disas_simd_tb(DisasContext *s, uint32_t insn)
5475 {
5476     int op2 = extract32(insn, 22, 2);
5477     int is_q = extract32(insn, 30, 1);
5478     int rm = extract32(insn, 16, 5);
5479     int rn = extract32(insn, 5, 5);
5480     int rd = extract32(insn, 0, 5);
5481     int is_tblx = extract32(insn, 12, 1);
5482     int len = extract32(insn, 13, 2);
5483     TCGv_i64 tcg_resl, tcg_resh, tcg_idx;
5484     TCGv_i32 tcg_regno, tcg_numregs;
5485
5486     if (op2 != 0) {
5487         unallocated_encoding(s);
5488         return;
5489     }
5490
5491     if (!fp_access_check(s)) {
5492         return;
5493     }
5494
5495     /* This does a table lookup: for every byte element in the input
5496      * we index into a table formed from up to four vector registers,
5497      * and then the output is the result of the lookups. Our helper
5498      * function does the lookup operation for a single 64 bit part of
5499      * the input.
5500      */
5501     tcg_resl = tcg_temp_new_i64();
5502     tcg_resh = tcg_temp_new_i64();
5503
5504     if (is_tblx) {
5505         read_vec_element(s, tcg_resl, rd, 0, MO_64);
5506     } else {
5507         tcg_gen_movi_i64(tcg_resl, 0);
5508     }
5509     if (is_tblx && is_q) {
5510         read_vec_element(s, tcg_resh, rd, 1, MO_64);
5511     } else {
5512         tcg_gen_movi_i64(tcg_resh, 0);
5513     }
5514
5515     tcg_idx = tcg_temp_new_i64();
5516     tcg_regno = tcg_const_i32(rn);
5517     tcg_numregs = tcg_const_i32(len + 1);
5518     read_vec_element(s, tcg_idx, rm, 0, MO_64);
5519     gen_helper_simd_tbl(tcg_resl, cpu_env, tcg_resl, tcg_idx,
5520                         tcg_regno, tcg_numregs);
5521     if (is_q) {
5522         read_vec_element(s, tcg_idx, rm, 1, MO_64);
5523         gen_helper_simd_tbl(tcg_resh, cpu_env, tcg_resh, tcg_idx,
5524                             tcg_regno, tcg_numregs);
5525     }
5526     tcg_temp_free_i64(tcg_idx);
5527     tcg_temp_free_i32(tcg_regno);
5528     tcg_temp_free_i32(tcg_numregs);
5529
5530     write_vec_element(s, tcg_resl, rd, 0, MO_64);
5531     tcg_temp_free_i64(tcg_resl);
5532     write_vec_element(s, tcg_resh, rd, 1, MO_64);
5533     tcg_temp_free_i64(tcg_resh);
5534 }
5535
5536 /* ZIP/UZP/TRN
5537  *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
5538  * +---+---+-------------+------+---+------+---+------------------+------+
5539  * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
5540  * +---+---+-------------+------+---+------+---+------------------+------+
5541  */
5542 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
5543 {
5544     int rd = extract32(insn, 0, 5);
5545     int rn = extract32(insn, 5, 5);
5546     int rm = extract32(insn, 16, 5);
5547     int size = extract32(insn, 22, 2);
5548     /* opc field bits [1:0] indicate ZIP/UZP/TRN;
5549      * bit 2 indicates 1 vs 2 variant of the insn.
5550      */
5551     int opcode = extract32(insn, 12, 2);
5552     bool part = extract32(insn, 14, 1);
5553     bool is_q = extract32(insn, 30, 1);
5554     int esize = 8 << size;
5555     int i, ofs;
5556     int datasize = is_q ? 128 : 64;
5557     int elements = datasize / esize;
5558     TCGv_i64 tcg_res, tcg_resl, tcg_resh;
5559
5560     if (opcode == 0 || (size == 3 && !is_q)) {
5561         unallocated_encoding(s);
5562         return;
5563     }
5564
5565     if (!fp_access_check(s)) {
5566         return;
5567     }
5568
5569     tcg_resl = tcg_const_i64(0);
5570     tcg_resh = tcg_const_i64(0);
5571     tcg_res = tcg_temp_new_i64();
5572
5573     for (i = 0; i < elements; i++) {
5574         switch (opcode) {
5575         case 1: /* UZP1/2 */
5576         {
5577             int midpoint = elements / 2;
5578             if (i < midpoint) {
5579                 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
5580             } else {
5581                 read_vec_element(s, tcg_res, rm,
5582                                  2 * (i - midpoint) + part, size);
5583             }
5584             break;
5585         }
5586         case 2: /* TRN1/2 */
5587             if (i & 1) {
5588                 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
5589             } else {
5590                 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
5591             }
5592             break;
5593         case 3: /* ZIP1/2 */
5594         {
5595             int base = part * elements / 2;
5596             if (i & 1) {
5597                 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
5598             } else {
5599                 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
5600             }
5601             break;
5602         }
5603         default:
5604             g_assert_not_reached();
5605         }
5606
5607         ofs = i * esize;
5608         if (ofs < 64) {
5609             tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
5610             tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
5611         } else {
5612             tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
5613             tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
5614         }
5615     }
5616
5617     tcg_temp_free_i64(tcg_res);
5618
5619     write_vec_element(s, tcg_resl, rd, 0, MO_64);
5620     tcg_temp_free_i64(tcg_resl);
5621     write_vec_element(s, tcg_resh, rd, 1, MO_64);
5622     tcg_temp_free_i64(tcg_resh);
5623 }
5624
5625 static void do_minmaxop(DisasContext *s, TCGv_i32 tcg_elt1, TCGv_i32 tcg_elt2,
5626                         int opc, bool is_min, TCGv_ptr fpst)
5627 {
5628     /* Helper function for disas_simd_across_lanes: do a single precision
5629      * min/max operation on the specified two inputs,
5630      * and return the result in tcg_elt1.
5631      */
5632     if (opc == 0xc) {
5633         if (is_min) {
5634             gen_helper_vfp_minnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5635         } else {
5636             gen_helper_vfp_maxnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5637         }
5638     } else {
5639         assert(opc == 0xf);
5640         if (is_min) {
5641             gen_helper_vfp_mins(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5642         } else {
5643             gen_helper_vfp_maxs(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5644         }
5645     }
5646 }
5647
5648 /* AdvSIMD across lanes
5649  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
5650  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5651  * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
5652  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5653  */
5654 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
5655 {
5656     int rd = extract32(insn, 0, 5);
5657     int rn = extract32(insn, 5, 5);
5658     int size = extract32(insn, 22, 2);
5659     int opcode = extract32(insn, 12, 5);
5660     bool is_q = extract32(insn, 30, 1);
5661     bool is_u = extract32(insn, 29, 1);
5662     bool is_fp = false;
5663     bool is_min = false;
5664     int esize;
5665     int elements;
5666     int i;
5667     TCGv_i64 tcg_res, tcg_elt;
5668
5669     switch (opcode) {
5670     case 0x1b: /* ADDV */
5671         if (is_u) {
5672             unallocated_encoding(s);
5673             return;
5674         }
5675         /* fall through */
5676     case 0x3: /* SADDLV, UADDLV */
5677     case 0xa: /* SMAXV, UMAXV */
5678     case 0x1a: /* SMINV, UMINV */
5679         if (size == 3 || (size == 2 && !is_q)) {
5680             unallocated_encoding(s);
5681             return;
5682         }
5683         break;
5684     case 0xc: /* FMAXNMV, FMINNMV */
5685     case 0xf: /* FMAXV, FMINV */
5686         if (!is_u || !is_q || extract32(size, 0, 1)) {
5687             unallocated_encoding(s);
5688             return;
5689         }
5690         /* Bit 1 of size field encodes min vs max, and actual size is always
5691          * 32 bits: adjust the size variable so following code can rely on it
5692          */
5693         is_min = extract32(size, 1, 1);
5694         is_fp = true;
5695         size = 2;
5696         break;
5697     default:
5698         unallocated_encoding(s);
5699         return;
5700     }
5701
5702     if (!fp_access_check(s)) {
5703         return;
5704     }
5705
5706     esize = 8 << size;
5707     elements = (is_q ? 128 : 64) / esize;
5708
5709     tcg_res = tcg_temp_new_i64();
5710     tcg_elt = tcg_temp_new_i64();
5711
5712     /* These instructions operate across all lanes of a vector
5713      * to produce a single result. We can guarantee that a 64
5714      * bit intermediate is sufficient:
5715      *  + for [US]ADDLV the maximum element size is 32 bits, and
5716      *    the result type is 64 bits
5717      *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
5718      *    same as the element size, which is 32 bits at most
5719      * For the integer operations we can choose to work at 64
5720      * or 32 bits and truncate at the end; for simplicity
5721      * we use 64 bits always. The floating point
5722      * ops do require 32 bit intermediates, though.
5723      */
5724     if (!is_fp) {
5725         read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
5726
5727         for (i = 1; i < elements; i++) {
5728             read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
5729
5730             switch (opcode) {
5731             case 0x03: /* SADDLV / UADDLV */
5732             case 0x1b: /* ADDV */
5733                 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
5734                 break;
5735             case 0x0a: /* SMAXV / UMAXV */
5736                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
5737                                     tcg_res,
5738                                     tcg_res, tcg_elt, tcg_res, tcg_elt);
5739                 break;
5740             case 0x1a: /* SMINV / UMINV */
5741                 tcg_gen_movcond_i64(is_u ? TCG_COND_LEU : TCG_COND_LE,
5742                                     tcg_res,
5743                                     tcg_res, tcg_elt, tcg_res, tcg_elt);
5744                 break;
5745                 break;
5746             default:
5747                 g_assert_not_reached();
5748             }
5749
5750         }
5751     } else {
5752         /* Floating point ops which work on 32 bit (single) intermediates.
5753          * Note that correct NaN propagation requires that we do these
5754          * operations in exactly the order specified by the pseudocode.
5755          */
5756         TCGv_i32 tcg_elt1 = tcg_temp_new_i32();
5757         TCGv_i32 tcg_elt2 = tcg_temp_new_i32();
5758         TCGv_i32 tcg_elt3 = tcg_temp_new_i32();
5759         TCGv_ptr fpst = get_fpstatus_ptr();
5760
5761         assert(esize == 32);
5762         assert(elements == 4);
5763
5764         read_vec_element(s, tcg_elt, rn, 0, MO_32);
5765         tcg_gen_extrl_i64_i32(tcg_elt1, tcg_elt);
5766         read_vec_element(s, tcg_elt, rn, 1, MO_32);
5767         tcg_gen_extrl_i64_i32(tcg_elt2, tcg_elt);
5768
5769         do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5770
5771         read_vec_element(s, tcg_elt, rn, 2, MO_32);
5772         tcg_gen_extrl_i64_i32(tcg_elt2, tcg_elt);
5773         read_vec_element(s, tcg_elt, rn, 3, MO_32);
5774         tcg_gen_extrl_i64_i32(tcg_elt3, tcg_elt);
5775
5776         do_minmaxop(s, tcg_elt2, tcg_elt3, opcode, is_min, fpst);
5777
5778         do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5779
5780         tcg_gen_extu_i32_i64(tcg_res, tcg_elt1);
5781         tcg_temp_free_i32(tcg_elt1);
5782         tcg_temp_free_i32(tcg_elt2);
5783         tcg_temp_free_i32(tcg_elt3);
5784         tcg_temp_free_ptr(fpst);
5785     }
5786
5787     tcg_temp_free_i64(tcg_elt);
5788
5789     /* Now truncate the result to the width required for the final output */
5790     if (opcode == 0x03) {
5791         /* SADDLV, UADDLV: result is 2*esize */
5792         size++;
5793     }
5794
5795     switch (size) {
5796     case 0:
5797         tcg_gen_ext8u_i64(tcg_res, tcg_res);
5798         break;
5799     case 1:
5800         tcg_gen_ext16u_i64(tcg_res, tcg_res);
5801         break;
5802     case 2:
5803         tcg_gen_ext32u_i64(tcg_res, tcg_res);
5804         break;
5805     case 3:
5806         break;
5807     default:
5808         g_assert_not_reached();
5809     }
5810
5811     write_fp_dreg(s, rd, tcg_res);
5812     tcg_temp_free_i64(tcg_res);
5813 }
5814
5815 /* DUP (Element, Vector)
5816  *
5817  *  31  30   29              21 20    16 15        10  9    5 4    0
5818  * +---+---+-------------------+--------+-------------+------+------+
5819  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
5820  * +---+---+-------------------+--------+-------------+------+------+
5821  *
5822  * size: encoded in imm5 (see ARM ARM LowestSetBit())
5823  */
5824 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
5825                              int imm5)
5826 {
5827     int size = ctz32(imm5);
5828     int esize = 8 << size;
5829     int elements = (is_q ? 128 : 64) / esize;
5830     int index, i;
5831     TCGv_i64 tmp;
5832
5833     if (size > 3 || (size == 3 && !is_q)) {
5834         unallocated_encoding(s);
5835         return;
5836     }
5837
5838     if (!fp_access_check(s)) {
5839         return;
5840     }
5841
5842     index = imm5 >> (size + 1);
5843
5844     tmp = tcg_temp_new_i64();
5845     read_vec_element(s, tmp, rn, index, size);
5846
5847     for (i = 0; i < elements; i++) {
5848         write_vec_element(s, tmp, rd, i, size);
5849     }
5850
5851     if (!is_q) {
5852         clear_vec_high(s, rd);
5853     }
5854
5855     tcg_temp_free_i64(tmp);
5856 }
5857
5858 /* DUP (element, scalar)
5859  *  31                   21 20    16 15        10  9    5 4    0
5860  * +-----------------------+--------+-------------+------+------+
5861  * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
5862  * +-----------------------+--------+-------------+------+------+
5863  */
5864 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
5865                               int imm5)
5866 {
5867     int size = ctz32(imm5);
5868     int index;
5869     TCGv_i64 tmp;
5870
5871     if (size > 3) {
5872         unallocated_encoding(s);
5873         return;
5874     }
5875
5876     if (!fp_access_check(s)) {
5877         return;
5878     }
5879
5880     index = imm5 >> (size + 1);
5881
5882     /* This instruction just extracts the specified element and
5883      * zero-extends it into the bottom of the destination register.
5884      */
5885     tmp = tcg_temp_new_i64();
5886     read_vec_element(s, tmp, rn, index, size);
5887     write_fp_dreg(s, rd, tmp);
5888     tcg_temp_free_i64(tmp);
5889 }
5890
5891 /* DUP (General)
5892  *
5893  *  31  30   29              21 20    16 15        10  9    5 4    0
5894  * +---+---+-------------------+--------+-------------+------+------+
5895  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
5896  * +---+---+-------------------+--------+-------------+------+------+
5897  *
5898  * size: encoded in imm5 (see ARM ARM LowestSetBit())
5899  */
5900 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
5901                              int imm5)
5902 {
5903     int size = ctz32(imm5);
5904     int esize = 8 << size;
5905     int elements = (is_q ? 128 : 64)/esize;
5906     int i = 0;
5907
5908     if (size > 3 || ((size == 3) && !is_q)) {
5909         unallocated_encoding(s);
5910         return;
5911     }
5912
5913     if (!fp_access_check(s)) {
5914         return;
5915     }
5916
5917     for (i = 0; i < elements; i++) {
5918         write_vec_element(s, cpu_reg(s, rn), rd, i, size);
5919     }
5920     if (!is_q) {
5921         clear_vec_high(s, rd);
5922     }
5923 }
5924
5925 /* INS (Element)
5926  *
5927  *  31                   21 20    16 15  14    11  10 9    5 4    0
5928  * +-----------------------+--------+------------+---+------+------+
5929  * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
5930  * +-----------------------+--------+------------+---+------+------+
5931  *
5932  * size: encoded in imm5 (see ARM ARM LowestSetBit())
5933  * index: encoded in imm5<4:size+1>
5934  */
5935 static void handle_simd_inse(DisasContext *s, int rd, int rn,
5936                              int imm4, int imm5)
5937 {
5938     int size = ctz32(imm5);
5939     int src_index, dst_index;
5940     TCGv_i64 tmp;
5941
5942     if (size > 3) {
5943         unallocated_encoding(s);
5944         return;
5945     }
5946
5947     if (!fp_access_check(s)) {
5948         return;
5949     }
5950
5951     dst_index = extract32(imm5, 1+size, 5);
5952     src_index = extract32(imm4, size, 4);
5953
5954     tmp = tcg_temp_new_i64();
5955
5956     read_vec_element(s, tmp, rn, src_index, size);
5957     write_vec_element(s, tmp, rd, dst_index, size);
5958
5959     tcg_temp_free_i64(tmp);
5960 }
5961
5962
5963 /* INS (General)
5964  *
5965  *  31                   21 20    16 15        10  9    5 4    0
5966  * +-----------------------+--------+-------------+------+------+
5967  * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
5968  * +-----------------------+--------+-------------+------+------+
5969  *
5970  * size: encoded in imm5 (see ARM ARM LowestSetBit())
5971  * index: encoded in imm5<4:size+1>
5972  */
5973 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
5974 {
5975     int size = ctz32(imm5);
5976     int idx;
5977
5978     if (size > 3) {
5979         unallocated_encoding(s);
5980         return;
5981     }
5982
5983     if (!fp_access_check(s)) {
5984         return;
5985     }
5986
5987     idx = extract32(imm5, 1 + size, 4 - size);
5988     write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
5989 }
5990
5991 /*
5992  * UMOV (General)
5993  * SMOV (General)
5994  *
5995  *  31  30   29              21 20    16 15    12   10 9    5 4    0
5996  * +---+---+-------------------+--------+-------------+------+------+
5997  * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
5998  * +---+---+-------------------+--------+-------------+------+------+
5999  *
6000  * U: unsigned when set
6001  * size: encoded in imm5 (see ARM ARM LowestSetBit())
6002  */
6003 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
6004                                   int rn, int rd, int imm5)
6005 {
6006     int size = ctz32(imm5);
6007     int element;
6008     TCGv_i64 tcg_rd;
6009
6010     /* Check for UnallocatedEncodings */
6011     if (is_signed) {
6012         if (size > 2 || (size == 2 && !is_q)) {
6013             unallocated_encoding(s);
6014             return;
6015         }
6016     } else {
6017         if (size > 3
6018             || (size < 3 && is_q)
6019             || (size == 3 && !is_q)) {
6020             unallocated_encoding(s);
6021             return;
6022         }
6023     }
6024
6025     if (!fp_access_check(s)) {
6026         return;
6027     }
6028
6029     element = extract32(imm5, 1+size, 4);
6030
6031     tcg_rd = cpu_reg(s, rd);
6032     read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
6033     if (is_signed && !is_q) {
6034         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
6035     }
6036 }
6037
6038 /* AdvSIMD copy
6039  *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
6040  * +---+---+----+-----------------+------+---+------+---+------+------+
6041  * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
6042  * +---+---+----+-----------------+------+---+------+---+------+------+
6043  */
6044 static void disas_simd_copy(DisasContext *s, uint32_t insn)
6045 {
6046     int rd = extract32(insn, 0, 5);
6047     int rn = extract32(insn, 5, 5);
6048     int imm4 = extract32(insn, 11, 4);
6049     int op = extract32(insn, 29, 1);
6050     int is_q = extract32(insn, 30, 1);
6051     int imm5 = extract32(insn, 16, 5);
6052
6053     if (op) {
6054         if (is_q) {
6055             /* INS (element) */
6056             handle_simd_inse(s, rd, rn, imm4, imm5);
6057         } else {
6058             unallocated_encoding(s);
6059         }
6060     } else {
6061         switch (imm4) {
6062         case 0:
6063             /* DUP (element - vector) */
6064             handle_simd_dupe(s, is_q, rd, rn, imm5);
6065             break;
6066         case 1:
6067             /* DUP (general) */
6068             handle_simd_dupg(s, is_q, rd, rn, imm5);
6069             break;
6070         case 3:
6071             if (is_q) {
6072                 /* INS (general) */
6073                 handle_simd_insg(s, rd, rn, imm5);
6074             } else {
6075                 unallocated_encoding(s);
6076             }
6077             break;
6078         case 5:
6079         case 7:
6080             /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
6081             handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
6082             break;
6083         default:
6084             unallocated_encoding(s);
6085             break;
6086         }
6087     }
6088 }
6089
6090 /* AdvSIMD modified immediate
6091  *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
6092  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
6093  * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
6094  * +---+---+----+---------------------+-----+-------+----+---+-------+------+
6095  *
6096  * There are a number of operations that can be carried out here:
6097  *   MOVI - move (shifted) imm into register
6098  *   MVNI - move inverted (shifted) imm into register
6099  *   ORR  - bitwise OR of (shifted) imm with register
6100  *   BIC  - bitwise clear of (shifted) imm with register
6101  */
6102 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
6103 {
6104     int rd = extract32(insn, 0, 5);
6105     int cmode = extract32(insn, 12, 4);
6106     int cmode_3_1 = extract32(cmode, 1, 3);
6107     int cmode_0 = extract32(cmode, 0, 1);
6108     int o2 = extract32(insn, 11, 1);
6109     uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
6110     bool is_neg = extract32(insn, 29, 1);
6111     bool is_q = extract32(insn, 30, 1);
6112     uint64_t imm = 0;
6113     TCGv_i64 tcg_rd, tcg_imm;
6114     int i;
6115
6116     if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
6117         unallocated_encoding(s);
6118         return;
6119     }
6120
6121     if (!fp_access_check(s)) {
6122         return;
6123     }
6124
6125     /* See AdvSIMDExpandImm() in ARM ARM */
6126     switch (cmode_3_1) {
6127     case 0: /* Replicate(Zeros(24):imm8, 2) */
6128     case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
6129     case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
6130     case 3: /* Replicate(imm8:Zeros(24), 2) */
6131     {
6132         int shift = cmode_3_1 * 8;
6133         imm = bitfield_replicate(abcdefgh << shift, 32);
6134         break;
6135     }
6136     case 4: /* Replicate(Zeros(8):imm8, 4) */
6137     case 5: /* Replicate(imm8:Zeros(8), 4) */
6138     {
6139         int shift = (cmode_3_1 & 0x1) * 8;
6140         imm = bitfield_replicate(abcdefgh << shift, 16);
6141         break;
6142     }
6143     case 6:
6144         if (cmode_0) {
6145             /* Replicate(Zeros(8):imm8:Ones(16), 2) */
6146             imm = (abcdefgh << 16) | 0xffff;
6147         } else {
6148             /* Replicate(Zeros(16):imm8:Ones(8), 2) */
6149             imm = (abcdefgh << 8) | 0xff;
6150         }
6151         imm = bitfield_replicate(imm, 32);
6152         break;
6153     case 7:
6154         if (!cmode_0 && !is_neg) {
6155             imm = bitfield_replicate(abcdefgh, 8);
6156         } else if (!cmode_0 && is_neg) {
6157             int i;
6158             imm = 0;
6159             for (i = 0; i < 8; i++) {
6160                 if ((abcdefgh) & (1 << i)) {
6161                     imm |= 0xffULL << (i * 8);
6162                 }
6163             }
6164         } else if (cmode_0) {
6165             if (is_neg) {
6166                 imm = (abcdefgh & 0x3f) << 48;
6167                 if (abcdefgh & 0x80) {
6168                     imm |= 0x8000000000000000ULL;
6169                 }
6170                 if (abcdefgh & 0x40) {
6171                     imm |= 0x3fc0000000000000ULL;
6172                 } else {
6173                     imm |= 0x4000000000000000ULL;
6174                 }
6175             } else {
6176                 imm = (abcdefgh & 0x3f) << 19;
6177                 if (abcdefgh & 0x80) {
6178                     imm |= 0x80000000;
6179                 }
6180                 if (abcdefgh & 0x40) {
6181                     imm |= 0x3e000000;
6182                 } else {
6183                     imm |= 0x40000000;
6184                 }
6185                 imm |= (imm << 32);
6186             }
6187         }
6188         break;
6189     }
6190
6191     if (cmode_3_1 != 7 && is_neg) {
6192         imm = ~imm;
6193     }
6194
6195     tcg_imm = tcg_const_i64(imm);
6196     tcg_rd = new_tmp_a64(s);
6197
6198     for (i = 0; i < 2; i++) {
6199         int foffs = i ? fp_reg_hi_offset(s, rd) : fp_reg_offset(s, rd, MO_64);
6200
6201         if (i == 1 && !is_q) {
6202             /* non-quad ops clear high half of vector */
6203             tcg_gen_movi_i64(tcg_rd, 0);
6204         } else if ((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9) {
6205             tcg_gen_ld_i64(tcg_rd, cpu_env, foffs);
6206             if (is_neg) {
6207                 /* AND (BIC) */
6208                 tcg_gen_and_i64(tcg_rd, tcg_rd, tcg_imm);
6209             } else {
6210                 /* ORR */
6211                 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_imm);
6212             }
6213         } else {
6214             /* MOVI */
6215             tcg_gen_mov_i64(tcg_rd, tcg_imm);
6216         }
6217         tcg_gen_st_i64(tcg_rd, cpu_env, foffs);
6218     }
6219
6220     tcg_temp_free_i64(tcg_imm);
6221 }
6222
6223 /* AdvSIMD scalar copy
6224  *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
6225  * +-----+----+-----------------+------+---+------+---+------+------+
6226  * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
6227  * +-----+----+-----------------+------+---+------+---+------+------+
6228  */
6229 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
6230 {
6231     int rd = extract32(insn, 0, 5);
6232     int rn = extract32(insn, 5, 5);
6233     int imm4 = extract32(insn, 11, 4);
6234     int imm5 = extract32(insn, 16, 5);
6235     int op = extract32(insn, 29, 1);
6236
6237     if (op != 0 || imm4 != 0) {
6238         unallocated_encoding(s);
6239         return;
6240     }
6241
6242     /* DUP (element, scalar) */
6243     handle_simd_dupes(s, rd, rn, imm5);
6244 }
6245
6246 /* AdvSIMD scalar pairwise
6247  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
6248  * +-----+---+-----------+------+-----------+--------+-----+------+------+
6249  * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
6250  * +-----+---+-----------+------+-----------+--------+-----+------+------+
6251  */
6252 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
6253 {
6254     int u = extract32(insn, 29, 1);
6255     int size = extract32(insn, 22, 2);
6256     int opcode = extract32(insn, 12, 5);
6257     int rn = extract32(insn, 5, 5);
6258     int rd = extract32(insn, 0, 5);
6259     TCGv_ptr fpst;
6260
6261     /* For some ops (the FP ones), size[1] is part of the encoding.
6262      * For ADDP strictly it is not but size[1] is always 1 for valid
6263      * encodings.
6264      */
6265     opcode |= (extract32(size, 1, 1) << 5);
6266
6267     switch (opcode) {
6268     case 0x3b: /* ADDP */
6269         if (u || size != 3) {
6270             unallocated_encoding(s);
6271             return;
6272         }
6273         if (!fp_access_check(s)) {
6274             return;
6275         }
6276
6277         TCGV_UNUSED_PTR(fpst);
6278         break;
6279     case 0xc: /* FMAXNMP */
6280     case 0xd: /* FADDP */
6281     case 0xf: /* FMAXP */
6282     case 0x2c: /* FMINNMP */
6283     case 0x2f: /* FMINP */
6284         /* FP op, size[0] is 32 or 64 bit */
6285         if (!u) {
6286             unallocated_encoding(s);
6287             return;
6288         }
6289         if (!fp_access_check(s)) {
6290             return;
6291         }
6292
6293         size = extract32(size, 0, 1) ? 3 : 2;
6294         fpst = get_fpstatus_ptr();
6295         break;
6296     default:
6297         unallocated_encoding(s);
6298         return;
6299     }
6300
6301     if (size == 3) {
6302         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6303         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6304         TCGv_i64 tcg_res = tcg_temp_new_i64();
6305
6306         read_vec_element(s, tcg_op1, rn, 0, MO_64);
6307         read_vec_element(s, tcg_op2, rn, 1, MO_64);
6308
6309         switch (opcode) {
6310         case 0x3b: /* ADDP */
6311             tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
6312             break;
6313         case 0xc: /* FMAXNMP */
6314             gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6315             break;
6316         case 0xd: /* FADDP */
6317             gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6318             break;
6319         case 0xf: /* FMAXP */
6320             gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6321             break;
6322         case 0x2c: /* FMINNMP */
6323             gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6324             break;
6325         case 0x2f: /* FMINP */
6326             gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6327             break;
6328         default:
6329             g_assert_not_reached();
6330         }
6331
6332         write_fp_dreg(s, rd, tcg_res);
6333
6334         tcg_temp_free_i64(tcg_op1);
6335         tcg_temp_free_i64(tcg_op2);
6336         tcg_temp_free_i64(tcg_res);
6337     } else {
6338         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6339         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6340         TCGv_i32 tcg_res = tcg_temp_new_i32();
6341
6342         read_vec_element_i32(s, tcg_op1, rn, 0, MO_32);
6343         read_vec_element_i32(s, tcg_op2, rn, 1, MO_32);
6344
6345         switch (opcode) {
6346         case 0xc: /* FMAXNMP */
6347             gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6348             break;
6349         case 0xd: /* FADDP */
6350             gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6351             break;
6352         case 0xf: /* FMAXP */
6353             gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6354             break;
6355         case 0x2c: /* FMINNMP */
6356             gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6357             break;
6358         case 0x2f: /* FMINP */
6359             gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6360             break;
6361         default:
6362             g_assert_not_reached();
6363         }
6364
6365         write_fp_sreg(s, rd, tcg_res);
6366
6367         tcg_temp_free_i32(tcg_op1);
6368         tcg_temp_free_i32(tcg_op2);
6369         tcg_temp_free_i32(tcg_res);
6370     }
6371
6372     if (!TCGV_IS_UNUSED_PTR(fpst)) {
6373         tcg_temp_free_ptr(fpst);
6374     }
6375 }
6376
6377 /*
6378  * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
6379  *
6380  * This code is handles the common shifting code and is used by both
6381  * the vector and scalar code.
6382  */
6383 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6384                                     TCGv_i64 tcg_rnd, bool accumulate,
6385                                     bool is_u, int size, int shift)
6386 {
6387     bool extended_result = false;
6388     bool round = !TCGV_IS_UNUSED_I64(tcg_rnd);
6389     int ext_lshift = 0;
6390     TCGv_i64 tcg_src_hi;
6391
6392     if (round && size == 3) {
6393         extended_result = true;
6394         ext_lshift = 64 - shift;
6395         tcg_src_hi = tcg_temp_new_i64();
6396     } else if (shift == 64) {
6397         if (!accumulate && is_u) {
6398             /* result is zero */
6399             tcg_gen_movi_i64(tcg_res, 0);
6400             return;
6401         }
6402     }
6403
6404     /* Deal with the rounding step */
6405     if (round) {
6406         if (extended_result) {
6407             TCGv_i64 tcg_zero = tcg_const_i64(0);
6408             if (!is_u) {
6409                 /* take care of sign extending tcg_res */
6410                 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
6411                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
6412                                  tcg_src, tcg_src_hi,
6413                                  tcg_rnd, tcg_zero);
6414             } else {
6415                 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
6416                                  tcg_src, tcg_zero,
6417                                  tcg_rnd, tcg_zero);
6418             }
6419             tcg_temp_free_i64(tcg_zero);
6420         } else {
6421             tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
6422         }
6423     }
6424
6425     /* Now do the shift right */
6426     if (round && extended_result) {
6427         /* extended case, >64 bit precision required */
6428         if (ext_lshift == 0) {
6429             /* special case, only high bits matter */
6430             tcg_gen_mov_i64(tcg_src, tcg_src_hi);
6431         } else {
6432             tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6433             tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
6434             tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
6435         }
6436     } else {
6437         if (is_u) {
6438             if (shift == 64) {
6439                 /* essentially shifting in 64 zeros */
6440                 tcg_gen_movi_i64(tcg_src, 0);
6441             } else {
6442                 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6443             }
6444         } else {
6445             if (shift == 64) {
6446                 /* effectively extending the sign-bit */
6447                 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
6448             } else {
6449                 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
6450             }
6451         }
6452     }
6453
6454     if (accumulate) {
6455         tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
6456     } else {
6457         tcg_gen_mov_i64(tcg_res, tcg_src);
6458     }
6459
6460     if (extended_result) {
6461         tcg_temp_free_i64(tcg_src_hi);
6462     }
6463 }
6464
6465 /* Common SHL/SLI - Shift left with an optional insert */
6466 static void handle_shli_with_ins(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6467                                  bool insert, int shift)
6468 {
6469     if (insert) { /* SLI */
6470         tcg_gen_deposit_i64(tcg_res, tcg_res, tcg_src, shift, 64 - shift);
6471     } else { /* SHL */
6472         tcg_gen_shli_i64(tcg_res, tcg_src, shift);
6473     }
6474 }
6475
6476 /* SRI: shift right with insert */
6477 static void handle_shri_with_ins(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6478                                  int size, int shift)
6479 {
6480     int esize = 8 << size;
6481
6482     /* shift count same as element size is valid but does nothing;
6483      * special case to avoid potential shift by 64.
6484      */
6485     if (shift != esize) {
6486         tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6487         tcg_gen_deposit_i64(tcg_res, tcg_res, tcg_src, 0, esize - shift);
6488     }
6489 }
6490
6491 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
6492 static void handle_scalar_simd_shri(DisasContext *s,
6493                                     bool is_u, int immh, int immb,
6494                                     int opcode, int rn, int rd)
6495 {
6496     const int size = 3;
6497     int immhb = immh << 3 | immb;
6498     int shift = 2 * (8 << size) - immhb;
6499     bool accumulate = false;
6500     bool round = false;
6501     bool insert = false;
6502     TCGv_i64 tcg_rn;
6503     TCGv_i64 tcg_rd;
6504     TCGv_i64 tcg_round;
6505
6506     if (!extract32(immh, 3, 1)) {
6507         unallocated_encoding(s);
6508         return;
6509     }
6510
6511     if (!fp_access_check(s)) {
6512         return;
6513     }
6514
6515     switch (opcode) {
6516     case 0x02: /* SSRA / USRA (accumulate) */
6517         accumulate = true;
6518         break;
6519     case 0x04: /* SRSHR / URSHR (rounding) */
6520         round = true;
6521         break;
6522     case 0x06: /* SRSRA / URSRA (accum + rounding) */
6523         accumulate = round = true;
6524         break;
6525     case 0x08: /* SRI */
6526         insert = true;
6527         break;
6528     }
6529
6530     if (round) {
6531         uint64_t round_const = 1ULL << (shift - 1);
6532         tcg_round = tcg_const_i64(round_const);
6533     } else {
6534         TCGV_UNUSED_I64(tcg_round);
6535     }
6536
6537     tcg_rn = read_fp_dreg(s, rn);
6538     tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6539
6540     if (insert) {
6541         handle_shri_with_ins(tcg_rd, tcg_rn, size, shift);
6542     } else {
6543         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6544                                 accumulate, is_u, size, shift);
6545     }
6546
6547     write_fp_dreg(s, rd, tcg_rd);
6548
6549     tcg_temp_free_i64(tcg_rn);
6550     tcg_temp_free_i64(tcg_rd);
6551     if (round) {
6552         tcg_temp_free_i64(tcg_round);
6553     }
6554 }
6555
6556 /* SHL/SLI - Scalar shift left */
6557 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
6558                                     int immh, int immb, int opcode,
6559                                     int rn, int rd)
6560 {
6561     int size = 32 - clz32(immh) - 1;
6562     int immhb = immh << 3 | immb;
6563     int shift = immhb - (8 << size);
6564     TCGv_i64 tcg_rn = new_tmp_a64(s);
6565     TCGv_i64 tcg_rd = new_tmp_a64(s);
6566
6567     if (!extract32(immh, 3, 1)) {
6568         unallocated_encoding(s);
6569         return;
6570     }
6571
6572     if (!fp_access_check(s)) {
6573         return;
6574     }
6575
6576     tcg_rn = read_fp_dreg(s, rn);
6577     tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6578
6579     handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
6580
6581     write_fp_dreg(s, rd, tcg_rd);
6582
6583     tcg_temp_free_i64(tcg_rn);
6584     tcg_temp_free_i64(tcg_rd);
6585 }
6586
6587 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
6588  * (signed/unsigned) narrowing */
6589 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
6590                                    bool is_u_shift, bool is_u_narrow,
6591                                    int immh, int immb, int opcode,
6592                                    int rn, int rd)
6593 {
6594     int immhb = immh << 3 | immb;
6595     int size = 32 - clz32(immh) - 1;
6596     int esize = 8 << size;
6597     int shift = (2 * esize) - immhb;
6598     int elements = is_scalar ? 1 : (64 / esize);
6599     bool round = extract32(opcode, 0, 1);
6600     TCGMemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
6601     TCGv_i64 tcg_rn, tcg_rd, tcg_round;
6602     TCGv_i32 tcg_rd_narrowed;
6603     TCGv_i64 tcg_final;
6604
6605     static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
6606         { gen_helper_neon_narrow_sat_s8,
6607           gen_helper_neon_unarrow_sat8 },
6608         { gen_helper_neon_narrow_sat_s16,
6609           gen_helper_neon_unarrow_sat16 },
6610         { gen_helper_neon_narrow_sat_s32,
6611           gen_helper_neon_unarrow_sat32 },
6612         { NULL, NULL },
6613     };
6614     static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
6615         gen_helper_neon_narrow_sat_u8,
6616         gen_helper_neon_narrow_sat_u16,
6617         gen_helper_neon_narrow_sat_u32,
6618         NULL
6619     };
6620     NeonGenNarrowEnvFn *narrowfn;
6621
6622     int i;
6623
6624     assert(size < 4);
6625
6626     if (extract32(immh, 3, 1)) {
6627         unallocated_encoding(s);
6628         return;
6629     }
6630
6631     if (!fp_access_check(s)) {
6632         return;
6633     }
6634
6635     if (is_u_shift) {
6636         narrowfn = unsigned_narrow_fns[size];
6637     } else {
6638         narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
6639     }
6640
6641     tcg_rn = tcg_temp_new_i64();
6642     tcg_rd = tcg_temp_new_i64();
6643     tcg_rd_narrowed = tcg_temp_new_i32();
6644     tcg_final = tcg_const_i64(0);
6645
6646     if (round) {
6647         uint64_t round_const = 1ULL << (shift - 1);
6648         tcg_round = tcg_const_i64(round_const);
6649     } else {
6650         TCGV_UNUSED_I64(tcg_round);
6651     }
6652
6653     for (i = 0; i < elements; i++) {
6654         read_vec_element(s, tcg_rn, rn, i, ldop);
6655         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6656                                 false, is_u_shift, size+1, shift);
6657         narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
6658         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
6659         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
6660     }
6661
6662     if (!is_q) {
6663         clear_vec_high(s, rd);
6664         write_vec_element(s, tcg_final, rd, 0, MO_64);
6665     } else {
6666         write_vec_element(s, tcg_final, rd, 1, MO_64);
6667     }
6668
6669     if (round) {
6670         tcg_temp_free_i64(tcg_round);
6671     }
6672     tcg_temp_free_i64(tcg_rn);
6673     tcg_temp_free_i64(tcg_rd);
6674     tcg_temp_free_i32(tcg_rd_narrowed);
6675     tcg_temp_free_i64(tcg_final);
6676     return;
6677 }
6678
6679 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
6680 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
6681                              bool src_unsigned, bool dst_unsigned,
6682                              int immh, int immb, int rn, int rd)
6683 {
6684     int immhb = immh << 3 | immb;
6685     int size = 32 - clz32(immh) - 1;
6686     int shift = immhb - (8 << size);
6687     int pass;
6688
6689     assert(immh != 0);
6690     assert(!(scalar && is_q));
6691
6692     if (!scalar) {
6693         if (!is_q && extract32(immh, 3, 1)) {
6694             unallocated_encoding(s);
6695             return;
6696         }
6697
6698         /* Since we use the variable-shift helpers we must
6699          * replicate the shift count into each element of
6700          * the tcg_shift value.
6701          */
6702         switch (size) {
6703         case 0:
6704             shift |= shift << 8;
6705             /* fall through */
6706         case 1:
6707             shift |= shift << 16;
6708             break;
6709         case 2:
6710         case 3:
6711             break;
6712         default:
6713             g_assert_not_reached();
6714         }
6715     }
6716
6717     if (!fp_access_check(s)) {
6718         return;
6719     }
6720
6721     if (size == 3) {
6722         TCGv_i64 tcg_shift = tcg_const_i64(shift);
6723         static NeonGenTwo64OpEnvFn * const fns[2][2] = {
6724             { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
6725             { NULL, gen_helper_neon_qshl_u64 },
6726         };
6727         NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
6728         int maxpass = is_q ? 2 : 1;
6729
6730         for (pass = 0; pass < maxpass; pass++) {
6731             TCGv_i64 tcg_op = tcg_temp_new_i64();
6732
6733             read_vec_element(s, tcg_op, rn, pass, MO_64);
6734             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6735             write_vec_element(s, tcg_op, rd, pass, MO_64);
6736
6737             tcg_temp_free_i64(tcg_op);
6738         }
6739         tcg_temp_free_i64(tcg_shift);
6740
6741         if (!is_q) {
6742             clear_vec_high(s, rd);
6743         }
6744     } else {
6745         TCGv_i32 tcg_shift = tcg_const_i32(shift);
6746         static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
6747             {
6748                 { gen_helper_neon_qshl_s8,
6749                   gen_helper_neon_qshl_s16,
6750                   gen_helper_neon_qshl_s32 },
6751                 { gen_helper_neon_qshlu_s8,
6752                   gen_helper_neon_qshlu_s16,
6753                   gen_helper_neon_qshlu_s32 }
6754             }, {
6755                 { NULL, NULL, NULL },
6756                 { gen_helper_neon_qshl_u8,
6757                   gen_helper_neon_qshl_u16,
6758                   gen_helper_neon_qshl_u32 }
6759             }
6760         };
6761         NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
6762         TCGMemOp memop = scalar ? size : MO_32;
6763         int maxpass = scalar ? 1 : is_q ? 4 : 2;
6764
6765         for (pass = 0; pass < maxpass; pass++) {
6766             TCGv_i32 tcg_op = tcg_temp_new_i32();
6767
6768             read_vec_element_i32(s, tcg_op, rn, pass, memop);
6769             genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6770             if (scalar) {
6771                 switch (size) {
6772                 case 0:
6773                     tcg_gen_ext8u_i32(tcg_op, tcg_op);
6774                     break;
6775                 case 1:
6776                     tcg_gen_ext16u_i32(tcg_op, tcg_op);
6777                     break;
6778                 case 2:
6779                     break;
6780                 default:
6781                     g_assert_not_reached();
6782                 }
6783                 write_fp_sreg(s, rd, tcg_op);
6784             } else {
6785                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
6786             }
6787
6788             tcg_temp_free_i32(tcg_op);
6789         }
6790         tcg_temp_free_i32(tcg_shift);
6791
6792         if (!is_q && !scalar) {
6793             clear_vec_high(s, rd);
6794         }
6795     }
6796 }
6797
6798 /* Common vector code for handling integer to FP conversion */
6799 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
6800                                    int elements, int is_signed,
6801                                    int fracbits, int size)
6802 {
6803     bool is_double = size == 3 ? true : false;
6804     TCGv_ptr tcg_fpst = get_fpstatus_ptr();
6805     TCGv_i32 tcg_shift = tcg_const_i32(fracbits);
6806     TCGv_i64 tcg_int = tcg_temp_new_i64();
6807     TCGMemOp mop = size | (is_signed ? MO_SIGN : 0);
6808     int pass;
6809
6810     for (pass = 0; pass < elements; pass++) {
6811         read_vec_element(s, tcg_int, rn, pass, mop);
6812
6813         if (is_double) {
6814             TCGv_i64 tcg_double = tcg_temp_new_i64();
6815             if (is_signed) {
6816                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6817                                      tcg_shift, tcg_fpst);
6818             } else {
6819                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6820                                      tcg_shift, tcg_fpst);
6821             }
6822             if (elements == 1) {
6823                 write_fp_dreg(s, rd, tcg_double);
6824             } else {
6825                 write_vec_element(s, tcg_double, rd, pass, MO_64);
6826             }
6827             tcg_temp_free_i64(tcg_double);
6828         } else {
6829             TCGv_i32 tcg_single = tcg_temp_new_i32();
6830             if (is_signed) {
6831                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6832                                      tcg_shift, tcg_fpst);
6833             } else {
6834                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6835                                      tcg_shift, tcg_fpst);
6836             }
6837             if (elements == 1) {
6838                 write_fp_sreg(s, rd, tcg_single);
6839             } else {
6840                 write_vec_element_i32(s, tcg_single, rd, pass, MO_32);
6841             }
6842             tcg_temp_free_i32(tcg_single);
6843         }
6844     }
6845
6846     if (!is_double && elements == 2) {
6847         clear_vec_high(s, rd);
6848     }
6849
6850     tcg_temp_free_i64(tcg_int);
6851     tcg_temp_free_ptr(tcg_fpst);
6852     tcg_temp_free_i32(tcg_shift);
6853 }
6854
6855 /* UCVTF/SCVTF - Integer to FP conversion */
6856 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
6857                                          bool is_q, bool is_u,
6858                                          int immh, int immb, int opcode,
6859                                          int rn, int rd)
6860 {
6861     bool is_double = extract32(immh, 3, 1);
6862     int size = is_double ? MO_64 : MO_32;
6863     int elements;
6864     int immhb = immh << 3 | immb;
6865     int fracbits = (is_double ? 128 : 64) - immhb;
6866
6867     if (!extract32(immh, 2, 2)) {
6868         unallocated_encoding(s);
6869         return;
6870     }
6871
6872     if (is_scalar) {
6873         elements = 1;
6874     } else {
6875         elements = is_double ? 2 : is_q ? 4 : 2;
6876         if (is_double && !is_q) {
6877             unallocated_encoding(s);
6878             return;
6879         }
6880     }
6881
6882     if (!fp_access_check(s)) {
6883         return;
6884     }
6885
6886     /* immh == 0 would be a failure of the decode logic */
6887     g_assert(immh);
6888
6889     handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
6890 }
6891
6892 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
6893 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
6894                                          bool is_q, bool is_u,
6895                                          int immh, int immb, int rn, int rd)
6896 {
6897     bool is_double = extract32(immh, 3, 1);
6898     int immhb = immh << 3 | immb;
6899     int fracbits = (is_double ? 128 : 64) - immhb;
6900     int pass;
6901     TCGv_ptr tcg_fpstatus;
6902     TCGv_i32 tcg_rmode, tcg_shift;
6903
6904     if (!extract32(immh, 2, 2)) {
6905         unallocated_encoding(s);
6906         return;
6907     }
6908
6909     if (!is_scalar && !is_q && is_double) {
6910         unallocated_encoding(s);
6911         return;
6912     }
6913
6914     if (!fp_access_check(s)) {
6915         return;
6916     }
6917
6918     assert(!(is_scalar && is_q));
6919
6920     tcg_rmode = tcg_const_i32(arm_rmode_to_sf(FPROUNDING_ZERO));
6921     gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
6922     tcg_fpstatus = get_fpstatus_ptr();
6923     tcg_shift = tcg_const_i32(fracbits);
6924
6925     if (is_double) {
6926         int maxpass = is_scalar ? 1 : 2;
6927
6928         for (pass = 0; pass < maxpass; pass++) {
6929             TCGv_i64 tcg_op = tcg_temp_new_i64();
6930
6931             read_vec_element(s, tcg_op, rn, pass, MO_64);
6932             if (is_u) {
6933                 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6934             } else {
6935                 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6936             }
6937             write_vec_element(s, tcg_op, rd, pass, MO_64);
6938             tcg_temp_free_i64(tcg_op);
6939         }
6940         if (!is_q) {
6941             clear_vec_high(s, rd);
6942         }
6943     } else {
6944         int maxpass = is_scalar ? 1 : is_q ? 4 : 2;
6945         for (pass = 0; pass < maxpass; pass++) {
6946             TCGv_i32 tcg_op = tcg_temp_new_i32();
6947
6948             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
6949             if (is_u) {
6950                 gen_helper_vfp_touls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6951             } else {
6952                 gen_helper_vfp_tosls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6953             }
6954             if (is_scalar) {
6955                 write_fp_sreg(s, rd, tcg_op);
6956             } else {
6957                 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
6958             }
6959             tcg_temp_free_i32(tcg_op);
6960         }
6961         if (!is_q && !is_scalar) {
6962             clear_vec_high(s, rd);
6963         }
6964     }
6965
6966     tcg_temp_free_ptr(tcg_fpstatus);
6967     tcg_temp_free_i32(tcg_shift);
6968     gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
6969     tcg_temp_free_i32(tcg_rmode);
6970 }
6971
6972 /* AdvSIMD scalar shift by immediate
6973  *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
6974  * +-----+---+-------------+------+------+--------+---+------+------+
6975  * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
6976  * +-----+---+-------------+------+------+--------+---+------+------+
6977  *
6978  * This is the scalar version so it works on a fixed sized registers
6979  */
6980 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
6981 {
6982     int rd = extract32(insn, 0, 5);
6983     int rn = extract32(insn, 5, 5);
6984     int opcode = extract32(insn, 11, 5);
6985     int immb = extract32(insn, 16, 3);
6986     int immh = extract32(insn, 19, 4);
6987     bool is_u = extract32(insn, 29, 1);
6988
6989     if (immh == 0) {
6990         unallocated_encoding(s);
6991         return;
6992     }
6993
6994     switch (opcode) {
6995     case 0x08: /* SRI */
6996         if (!is_u) {
6997             unallocated_encoding(s);
6998             return;
6999         }
7000         /* fall through */
7001     case 0x00: /* SSHR / USHR */
7002     case 0x02: /* SSRA / USRA */
7003     case 0x04: /* SRSHR / URSHR */
7004     case 0x06: /* SRSRA / URSRA */
7005         handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
7006         break;
7007     case 0x0a: /* SHL / SLI */
7008         handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
7009         break;
7010     case 0x1c: /* SCVTF, UCVTF */
7011         handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
7012                                      opcode, rn, rd);
7013         break;
7014     case 0x10: /* SQSHRUN, SQSHRUN2 */
7015     case 0x11: /* SQRSHRUN, SQRSHRUN2 */
7016         if (!is_u) {
7017             unallocated_encoding(s);
7018             return;
7019         }
7020         handle_vec_simd_sqshrn(s, true, false, false, true,
7021                                immh, immb, opcode, rn, rd);
7022         break;
7023     case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
7024     case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
7025         handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
7026                                immh, immb, opcode, rn, rd);
7027         break;
7028     case 0xc: /* SQSHLU */
7029         if (!is_u) {
7030             unallocated_encoding(s);
7031             return;
7032         }
7033         handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
7034         break;
7035     case 0xe: /* SQSHL, UQSHL */
7036         handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
7037         break;
7038     case 0x1f: /* FCVTZS, FCVTZU */
7039         handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
7040         break;
7041     default:
7042         unallocated_encoding(s);
7043         break;
7044     }
7045 }
7046
7047 /* AdvSIMD scalar three different
7048  *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
7049  * +-----+---+-----------+------+---+------+--------+-----+------+------+
7050  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
7051  * +-----+---+-----------+------+---+------+--------+-----+------+------+
7052  */
7053 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
7054 {
7055     bool is_u = extract32(insn, 29, 1);
7056     int size = extract32(insn, 22, 2);
7057     int opcode = extract32(insn, 12, 4);
7058     int rm = extract32(insn, 16, 5);
7059     int rn = extract32(insn, 5, 5);
7060     int rd = extract32(insn, 0, 5);
7061
7062     if (is_u) {
7063         unallocated_encoding(s);
7064         return;
7065     }
7066
7067     switch (opcode) {
7068     case 0x9: /* SQDMLAL, SQDMLAL2 */
7069     case 0xb: /* SQDMLSL, SQDMLSL2 */
7070     case 0xd: /* SQDMULL, SQDMULL2 */
7071         if (size == 0 || size == 3) {
7072             unallocated_encoding(s);
7073             return;
7074         }
7075         break;
7076     default:
7077         unallocated_encoding(s);
7078         return;
7079     }
7080
7081     if (!fp_access_check(s)) {
7082         return;
7083     }
7084
7085     if (size == 2) {
7086         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7087         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7088         TCGv_i64 tcg_res = tcg_temp_new_i64();
7089
7090         read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
7091         read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
7092
7093         tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
7094         gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
7095
7096         switch (opcode) {
7097         case 0xd: /* SQDMULL, SQDMULL2 */
7098             break;
7099         case 0xb: /* SQDMLSL, SQDMLSL2 */
7100             tcg_gen_neg_i64(tcg_res, tcg_res);
7101             /* fall through */
7102         case 0x9: /* SQDMLAL, SQDMLAL2 */
7103             read_vec_element(s, tcg_op1, rd, 0, MO_64);
7104             gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
7105                                               tcg_res, tcg_op1);
7106             break;
7107         default:
7108             g_assert_not_reached();
7109         }
7110
7111         write_fp_dreg(s, rd, tcg_res);
7112
7113         tcg_temp_free_i64(tcg_op1);
7114         tcg_temp_free_i64(tcg_op2);
7115         tcg_temp_free_i64(tcg_res);
7116     } else {
7117         TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7118         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7119         TCGv_i64 tcg_res = tcg_temp_new_i64();
7120
7121         read_vec_element_i32(s, tcg_op1, rn, 0, MO_16);
7122         read_vec_element_i32(s, tcg_op2, rm, 0, MO_16);
7123
7124         gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
7125         gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
7126
7127         switch (opcode) {
7128         case 0xd: /* SQDMULL, SQDMULL2 */
7129             break;
7130         case 0xb: /* SQDMLSL, SQDMLSL2 */
7131             gen_helper_neon_negl_u32(tcg_res, tcg_res);
7132             /* fall through */
7133         case 0x9: /* SQDMLAL, SQDMLAL2 */
7134         {
7135             TCGv_i64 tcg_op3 = tcg_temp_new_i64();
7136             read_vec_element(s, tcg_op3, rd, 0, MO_32);
7137             gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
7138                                               tcg_res, tcg_op3);
7139             tcg_temp_free_i64(tcg_op3);
7140             break;
7141         }
7142         default:
7143             g_assert_not_reached();
7144         }
7145
7146         tcg_gen_ext32u_i64(tcg_res, tcg_res);
7147         write_fp_dreg(s, rd, tcg_res);
7148
7149         tcg_temp_free_i32(tcg_op1);
7150         tcg_temp_free_i32(tcg_op2);
7151         tcg_temp_free_i64(tcg_res);
7152     }
7153 }
7154
7155 static void handle_3same_64(DisasContext *s, int opcode, bool u,
7156                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
7157 {
7158     /* Handle 64x64->64 opcodes which are shared between the scalar
7159      * and vector 3-same groups. We cover every opcode where size == 3
7160      * is valid in either the three-reg-same (integer, not pairwise)
7161      * or scalar-three-reg-same groups. (Some opcodes are not yet
7162      * implemented.)
7163      */
7164     TCGCond cond;
7165
7166     switch (opcode) {
7167     case 0x1: /* SQADD */
7168         if (u) {
7169             gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7170         } else {
7171             gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7172         }
7173         break;
7174     case 0x5: /* SQSUB */
7175         if (u) {
7176             gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7177         } else {
7178             gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7179         }
7180         break;
7181     case 0x6: /* CMGT, CMHI */
7182         /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
7183          * We implement this using setcond (test) and then negating.
7184          */
7185         cond = u ? TCG_COND_GTU : TCG_COND_GT;
7186     do_cmop:
7187         tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
7188         tcg_gen_neg_i64(tcg_rd, tcg_rd);
7189         break;
7190     case 0x7: /* CMGE, CMHS */
7191         cond = u ? TCG_COND_GEU : TCG_COND_GE;
7192         goto do_cmop;
7193     case 0x11: /* CMTST, CMEQ */
7194         if (u) {
7195             cond = TCG_COND_EQ;
7196             goto do_cmop;
7197         }
7198         /* CMTST : test is "if (X & Y != 0)". */
7199         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
7200         tcg_gen_setcondi_i64(TCG_COND_NE, tcg_rd, tcg_rd, 0);
7201         tcg_gen_neg_i64(tcg_rd, tcg_rd);
7202         break;
7203     case 0x8: /* SSHL, USHL */
7204         if (u) {
7205             gen_helper_neon_shl_u64(tcg_rd, tcg_rn, tcg_rm);
7206         } else {
7207             gen_helper_neon_shl_s64(tcg_rd, tcg_rn, tcg_rm);
7208         }
7209         break;
7210     case 0x9: /* SQSHL, UQSHL */
7211         if (u) {
7212             gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7213         } else {
7214             gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7215         }
7216         break;
7217     case 0xa: /* SRSHL, URSHL */
7218         if (u) {
7219             gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
7220         } else {
7221             gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
7222         }
7223         break;
7224     case 0xb: /* SQRSHL, UQRSHL */
7225         if (u) {
7226             gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7227         } else {
7228             gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7229         }
7230         break;
7231     case 0x10: /* ADD, SUB */
7232         if (u) {
7233             tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
7234         } else {
7235             tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
7236         }
7237         break;
7238     default:
7239         g_assert_not_reached();
7240     }
7241 }
7242
7243 /* Handle the 3-same-operands float operations; shared by the scalar
7244  * and vector encodings. The caller must filter out any encodings
7245  * not allocated for the encoding it is dealing with.
7246  */
7247 static void handle_3same_float(DisasContext *s, int size, int elements,
7248                                int fpopcode, int rd, int rn, int rm)
7249 {
7250     int pass;
7251     TCGv_ptr fpst = get_fpstatus_ptr();
7252
7253     for (pass = 0; pass < elements; pass++) {
7254         if (size) {
7255             /* Double */
7256             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7257             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7258             TCGv_i64 tcg_res = tcg_temp_new_i64();
7259
7260             read_vec_element(s, tcg_op1, rn, pass, MO_64);
7261             read_vec_element(s, tcg_op2, rm, pass, MO_64);
7262
7263             switch (fpopcode) {
7264             case 0x39: /* FMLS */
7265                 /* As usual for ARM, separate negation for fused multiply-add */
7266                 gen_helper_vfp_negd(tcg_op1, tcg_op1);
7267                 /* fall through */
7268             case 0x19: /* FMLA */
7269                 read_vec_element(s, tcg_res, rd, pass, MO_64);
7270                 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
7271                                        tcg_res, fpst);
7272                 break;
7273             case 0x18: /* FMAXNM */
7274                 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7275                 break;
7276             case 0x1a: /* FADD */
7277                 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
7278                 break;
7279             case 0x1b: /* FMULX */
7280                 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
7281                 break;
7282             case 0x1c: /* FCMEQ */
7283                 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7284                 break;
7285             case 0x1e: /* FMAX */
7286                 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
7287                 break;
7288             case 0x1f: /* FRECPS */
7289                 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7290                 break;
7291             case 0x38: /* FMINNM */
7292                 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7293                 break;
7294             case 0x3a: /* FSUB */
7295                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
7296                 break;
7297             case 0x3e: /* FMIN */
7298                 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
7299                 break;
7300             case 0x3f: /* FRSQRTS */
7301                 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7302                 break;
7303             case 0x5b: /* FMUL */
7304                 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
7305                 break;
7306             case 0x5c: /* FCMGE */
7307                 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7308                 break;
7309             case 0x5d: /* FACGE */
7310                 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7311                 break;
7312             case 0x5f: /* FDIV */
7313                 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
7314                 break;
7315             case 0x7a: /* FABD */
7316                 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
7317                 gen_helper_vfp_absd(tcg_res, tcg_res);
7318                 break;
7319             case 0x7c: /* FCMGT */
7320                 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7321                 break;
7322             case 0x7d: /* FACGT */
7323                 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7324                 break;
7325             default:
7326                 g_assert_not_reached();
7327             }
7328
7329             write_vec_element(s, tcg_res, rd, pass, MO_64);
7330
7331             tcg_temp_free_i64(tcg_res);
7332             tcg_temp_free_i64(tcg_op1);
7333             tcg_temp_free_i64(tcg_op2);
7334         } else {
7335             /* Single */
7336             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7337             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7338             TCGv_i32 tcg_res = tcg_temp_new_i32();
7339
7340             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
7341             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
7342
7343             switch (fpopcode) {
7344             case 0x39: /* FMLS */
7345                 /* As usual for ARM, separate negation for fused multiply-add */
7346                 gen_helper_vfp_negs(tcg_op1, tcg_op1);
7347                 /* fall through */
7348             case 0x19: /* FMLA */
7349                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7350                 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
7351                                        tcg_res, fpst);
7352                 break;
7353             case 0x1a: /* FADD */
7354                 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
7355                 break;
7356             case 0x1b: /* FMULX */
7357                 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
7358                 break;
7359             case 0x1c: /* FCMEQ */
7360                 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7361                 break;
7362             case 0x1e: /* FMAX */
7363                 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
7364                 break;
7365             case 0x1f: /* FRECPS */
7366                 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7367                 break;
7368             case 0x18: /* FMAXNM */
7369                 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
7370                 break;
7371             case 0x38: /* FMINNM */
7372                 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
7373                 break;
7374             case 0x3a: /* FSUB */
7375                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
7376                 break;
7377             case 0x3e: /* FMIN */
7378                 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
7379                 break;
7380             case 0x3f: /* FRSQRTS */
7381                 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7382                 break;
7383             case 0x5b: /* FMUL */
7384                 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
7385                 break;
7386             case 0x5c: /* FCMGE */
7387                 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7388                 break;
7389             case 0x5d: /* FACGE */
7390                 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7391                 break;
7392             case 0x5f: /* FDIV */
7393                 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
7394                 break;
7395             case 0x7a: /* FABD */
7396                 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
7397                 gen_helper_vfp_abss(tcg_res, tcg_res);
7398                 break;
7399             case 0x7c: /* FCMGT */
7400                 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7401                 break;
7402             case 0x7d: /* FACGT */
7403                 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7404                 break;
7405             default:
7406                 g_assert_not_reached();
7407             }
7408
7409             if (elements == 1) {
7410                 /* scalar single so clear high part */
7411                 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7412
7413                 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
7414                 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
7415                 tcg_temp_free_i64(tcg_tmp);
7416             } else {
7417                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7418             }
7419
7420             tcg_temp_free_i32(tcg_res);
7421             tcg_temp_free_i32(tcg_op1);
7422             tcg_temp_free_i32(tcg_op2);
7423         }
7424     }
7425
7426     tcg_temp_free_ptr(fpst);
7427
7428     if ((elements << size) < 4) {
7429         /* scalar, or non-quad vector op */
7430         clear_vec_high(s, rd);
7431     }
7432 }
7433
7434 /* AdvSIMD scalar three same
7435  *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
7436  * +-----+---+-----------+------+---+------+--------+---+------+------+
7437  * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
7438  * +-----+---+-----------+------+---+------+--------+---+------+------+
7439  */
7440 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
7441 {
7442     int rd = extract32(insn, 0, 5);
7443     int rn = extract32(insn, 5, 5);
7444     int opcode = extract32(insn, 11, 5);
7445     int rm = extract32(insn, 16, 5);
7446     int size = extract32(insn, 22, 2);
7447     bool u = extract32(insn, 29, 1);
7448     TCGv_i64 tcg_rd;
7449
7450     if (opcode >= 0x18) {
7451         /* Floating point: U, size[1] and opcode indicate operation */
7452         int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
7453         switch (fpopcode) {
7454         case 0x1b: /* FMULX */
7455         case 0x1f: /* FRECPS */
7456         case 0x3f: /* FRSQRTS */
7457         case 0x5d: /* FACGE */
7458         case 0x7d: /* FACGT */
7459         case 0x1c: /* FCMEQ */
7460         case 0x5c: /* FCMGE */
7461         case 0x7c: /* FCMGT */
7462         case 0x7a: /* FABD */
7463             break;
7464         default:
7465             unallocated_encoding(s);
7466             return;
7467         }
7468
7469         if (!fp_access_check(s)) {
7470             return;
7471         }
7472
7473         handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
7474         return;
7475     }
7476
7477     switch (opcode) {
7478     case 0x1: /* SQADD, UQADD */
7479     case 0x5: /* SQSUB, UQSUB */
7480     case 0x9: /* SQSHL, UQSHL */
7481     case 0xb: /* SQRSHL, UQRSHL */
7482         break;
7483     case 0x8: /* SSHL, USHL */
7484     case 0xa: /* SRSHL, URSHL */
7485     case 0x6: /* CMGT, CMHI */
7486     case 0x7: /* CMGE, CMHS */
7487     case 0x11: /* CMTST, CMEQ */
7488     case 0x10: /* ADD, SUB (vector) */
7489         if (size != 3) {
7490             unallocated_encoding(s);
7491             return;
7492         }
7493         break;
7494     case 0x16: /* SQDMULH, SQRDMULH (vector) */
7495         if (size != 1 && size != 2) {
7496             unallocated_encoding(s);
7497             return;
7498         }
7499         break;
7500     default:
7501         unallocated_encoding(s);
7502         return;
7503     }
7504
7505     if (!fp_access_check(s)) {
7506         return;
7507     }
7508
7509     tcg_rd = tcg_temp_new_i64();
7510
7511     if (size == 3) {
7512         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
7513         TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
7514
7515         handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
7516         tcg_temp_free_i64(tcg_rn);
7517         tcg_temp_free_i64(tcg_rm);
7518     } else {
7519         /* Do a single operation on the lowest element in the vector.
7520          * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
7521          * no side effects for all these operations.
7522          * OPTME: special-purpose helpers would avoid doing some
7523          * unnecessary work in the helper for the 8 and 16 bit cases.
7524          */
7525         NeonGenTwoOpEnvFn *genenvfn;
7526         TCGv_i32 tcg_rn = tcg_temp_new_i32();
7527         TCGv_i32 tcg_rm = tcg_temp_new_i32();
7528         TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
7529
7530         read_vec_element_i32(s, tcg_rn, rn, 0, size);
7531         read_vec_element_i32(s, tcg_rm, rm, 0, size);
7532
7533         switch (opcode) {
7534         case 0x1: /* SQADD, UQADD */
7535         {
7536             static NeonGenTwoOpEnvFn * const fns[3][2] = {
7537                 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
7538                 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
7539                 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
7540             };
7541             genenvfn = fns[size][u];
7542             break;
7543         }
7544         case 0x5: /* SQSUB, UQSUB */
7545         {
7546             static NeonGenTwoOpEnvFn * const fns[3][2] = {
7547                 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
7548                 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
7549                 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
7550             };
7551             genenvfn = fns[size][u];
7552             break;
7553         }
7554         case 0x9: /* SQSHL, UQSHL */
7555         {
7556             static NeonGenTwoOpEnvFn * const fns[3][2] = {
7557                 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
7558                 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
7559                 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
7560             };
7561             genenvfn = fns[size][u];
7562             break;
7563         }
7564         case 0xb: /* SQRSHL, UQRSHL */
7565         {
7566             static NeonGenTwoOpEnvFn * const fns[3][2] = {
7567                 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
7568                 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
7569                 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
7570             };
7571             genenvfn = fns[size][u];
7572             break;
7573         }
7574         case 0x16: /* SQDMULH, SQRDMULH */
7575         {
7576             static NeonGenTwoOpEnvFn * const fns[2][2] = {
7577                 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
7578                 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
7579             };
7580             assert(size == 1 || size == 2);
7581             genenvfn = fns[size - 1][u];
7582             break;
7583         }
7584         default:
7585             g_assert_not_reached();
7586         }
7587
7588         genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
7589         tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
7590         tcg_temp_free_i32(tcg_rd32);
7591         tcg_temp_free_i32(tcg_rn);
7592         tcg_temp_free_i32(tcg_rm);
7593     }
7594
7595     write_fp_dreg(s, rd, tcg_rd);
7596
7597     tcg_temp_free_i64(tcg_rd);
7598 }
7599
7600 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
7601                             TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
7602                             TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
7603 {
7604     /* Handle 64->64 opcodes which are shared between the scalar and
7605      * vector 2-reg-misc groups. We cover every integer opcode where size == 3
7606      * is valid in either group and also the double-precision fp ops.
7607      * The caller only need provide tcg_rmode and tcg_fpstatus if the op
7608      * requires them.
7609      */
7610     TCGCond cond;
7611
7612     switch (opcode) {
7613     case 0x4: /* CLS, CLZ */
7614         if (u) {
7615             tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
7616         } else {
7617             tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
7618         }
7619         break;
7620     case 0x5: /* NOT */
7621         /* This opcode is shared with CNT and RBIT but we have earlier
7622          * enforced that size == 3 if and only if this is the NOT insn.
7623          */
7624         tcg_gen_not_i64(tcg_rd, tcg_rn);
7625         break;
7626     case 0x7: /* SQABS, SQNEG */
7627         if (u) {
7628             gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
7629         } else {
7630             gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
7631         }
7632         break;
7633     case 0xa: /* CMLT */
7634         /* 64 bit integer comparison against zero, result is
7635          * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
7636          * subtracting 1.
7637          */
7638         cond = TCG_COND_LT;
7639     do_cmop:
7640         tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
7641         tcg_gen_neg_i64(tcg_rd, tcg_rd);
7642         break;
7643     case 0x8: /* CMGT, CMGE */
7644         cond = u ? TCG_COND_GE : TCG_COND_GT;
7645         goto do_cmop;
7646     case 0x9: /* CMEQ, CMLE */
7647         cond = u ? TCG_COND_LE : TCG_COND_EQ;
7648         goto do_cmop;
7649     case 0xb: /* ABS, NEG */
7650         if (u) {
7651             tcg_gen_neg_i64(tcg_rd, tcg_rn);
7652         } else {
7653             TCGv_i64 tcg_zero = tcg_const_i64(0);
7654             tcg_gen_neg_i64(tcg_rd, tcg_rn);
7655             tcg_gen_movcond_i64(TCG_COND_GT, tcg_rd, tcg_rn, tcg_zero,
7656                                 tcg_rn, tcg_rd);
7657             tcg_temp_free_i64(tcg_zero);
7658         }
7659         break;
7660     case 0x2f: /* FABS */
7661         gen_helper_vfp_absd(tcg_rd, tcg_rn);
7662         break;
7663     case 0x6f: /* FNEG */
7664         gen_helper_vfp_negd(tcg_rd, tcg_rn);
7665         break;
7666     case 0x7f: /* FSQRT */
7667         gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
7668         break;
7669     case 0x1a: /* FCVTNS */
7670     case 0x1b: /* FCVTMS */
7671     case 0x1c: /* FCVTAS */
7672     case 0x3a: /* FCVTPS */
7673     case 0x3b: /* FCVTZS */
7674     {
7675         TCGv_i32 tcg_shift = tcg_const_i32(0);
7676         gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7677         tcg_temp_free_i32(tcg_shift);
7678         break;
7679     }
7680     case 0x5a: /* FCVTNU */
7681     case 0x5b: /* FCVTMU */
7682     case 0x5c: /* FCVTAU */
7683     case 0x7a: /* FCVTPU */
7684     case 0x7b: /* FCVTZU */
7685     {
7686         TCGv_i32 tcg_shift = tcg_const_i32(0);
7687         gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7688         tcg_temp_free_i32(tcg_shift);
7689         break;
7690     }
7691     case 0x18: /* FRINTN */
7692     case 0x19: /* FRINTM */
7693     case 0x38: /* FRINTP */
7694     case 0x39: /* FRINTZ */
7695     case 0x58: /* FRINTA */
7696     case 0x79: /* FRINTI */
7697         gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
7698         break;
7699     case 0x59: /* FRINTX */
7700         gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
7701         break;
7702     default:
7703         g_assert_not_reached();
7704     }
7705 }
7706
7707 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
7708                                    bool is_scalar, bool is_u, bool is_q,
7709                                    int size, int rn, int rd)
7710 {
7711     bool is_double = (size == 3);
7712     TCGv_ptr fpst;
7713
7714     if (!fp_access_check(s)) {
7715         return;
7716     }
7717
7718     fpst = get_fpstatus_ptr();
7719
7720     if (is_double) {
7721         TCGv_i64 tcg_op = tcg_temp_new_i64();
7722         TCGv_i64 tcg_zero = tcg_const_i64(0);
7723         TCGv_i64 tcg_res = tcg_temp_new_i64();
7724         NeonGenTwoDoubleOPFn *genfn;
7725         bool swap = false;
7726         int pass;
7727
7728         switch (opcode) {
7729         case 0x2e: /* FCMLT (zero) */
7730             swap = true;
7731             /* fallthrough */
7732         case 0x2c: /* FCMGT (zero) */
7733             genfn = gen_helper_neon_cgt_f64;
7734             break;
7735         case 0x2d: /* FCMEQ (zero) */
7736             genfn = gen_helper_neon_ceq_f64;
7737             break;
7738         case 0x6d: /* FCMLE (zero) */
7739             swap = true;
7740             /* fall through */
7741         case 0x6c: /* FCMGE (zero) */
7742             genfn = gen_helper_neon_cge_f64;
7743             break;
7744         default:
7745             g_assert_not_reached();
7746         }
7747
7748         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7749             read_vec_element(s, tcg_op, rn, pass, MO_64);
7750             if (swap) {
7751                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7752             } else {
7753                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7754             }
7755             write_vec_element(s, tcg_res, rd, pass, MO_64);
7756         }
7757         if (is_scalar) {
7758             clear_vec_high(s, rd);
7759         }
7760
7761         tcg_temp_free_i64(tcg_res);
7762         tcg_temp_free_i64(tcg_zero);
7763         tcg_temp_free_i64(tcg_op);
7764     } else {
7765         TCGv_i32 tcg_op = tcg_temp_new_i32();
7766         TCGv_i32 tcg_zero = tcg_const_i32(0);
7767         TCGv_i32 tcg_res = tcg_temp_new_i32();
7768         NeonGenTwoSingleOPFn *genfn;
7769         bool swap = false;
7770         int pass, maxpasses;
7771
7772         switch (opcode) {
7773         case 0x2e: /* FCMLT (zero) */
7774             swap = true;
7775             /* fall through */
7776         case 0x2c: /* FCMGT (zero) */
7777             genfn = gen_helper_neon_cgt_f32;
7778             break;
7779         case 0x2d: /* FCMEQ (zero) */
7780             genfn = gen_helper_neon_ceq_f32;
7781             break;
7782         case 0x6d: /* FCMLE (zero) */
7783             swap = true;
7784             /* fall through */
7785         case 0x6c: /* FCMGE (zero) */
7786             genfn = gen_helper_neon_cge_f32;
7787             break;
7788         default:
7789             g_assert_not_reached();
7790         }
7791
7792         if (is_scalar) {
7793             maxpasses = 1;
7794         } else {
7795             maxpasses = is_q ? 4 : 2;
7796         }
7797
7798         for (pass = 0; pass < maxpasses; pass++) {
7799             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7800             if (swap) {
7801                 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7802             } else {
7803                 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7804             }
7805             if (is_scalar) {
7806                 write_fp_sreg(s, rd, tcg_res);
7807             } else {
7808                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7809             }
7810         }
7811         tcg_temp_free_i32(tcg_res);
7812         tcg_temp_free_i32(tcg_zero);
7813         tcg_temp_free_i32(tcg_op);
7814         if (!is_q && !is_scalar) {
7815             clear_vec_high(s, rd);
7816         }
7817     }
7818
7819     tcg_temp_free_ptr(fpst);
7820 }
7821
7822 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
7823                                     bool is_scalar, bool is_u, bool is_q,
7824                                     int size, int rn, int rd)
7825 {
7826     bool is_double = (size == 3);
7827     TCGv_ptr fpst = get_fpstatus_ptr();
7828
7829     if (is_double) {
7830         TCGv_i64 tcg_op = tcg_temp_new_i64();
7831         TCGv_i64 tcg_res = tcg_temp_new_i64();
7832         int pass;
7833
7834         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7835             read_vec_element(s, tcg_op, rn, pass, MO_64);
7836             switch (opcode) {
7837             case 0x3d: /* FRECPE */
7838                 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
7839                 break;
7840             case 0x3f: /* FRECPX */
7841                 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
7842                 break;
7843             case 0x7d: /* FRSQRTE */
7844                 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
7845                 break;
7846             default:
7847                 g_assert_not_reached();
7848             }
7849             write_vec_element(s, tcg_res, rd, pass, MO_64);
7850         }
7851         if (is_scalar) {
7852             clear_vec_high(s, rd);
7853         }
7854
7855         tcg_temp_free_i64(tcg_res);
7856         tcg_temp_free_i64(tcg_op);
7857     } else {
7858         TCGv_i32 tcg_op = tcg_temp_new_i32();
7859         TCGv_i32 tcg_res = tcg_temp_new_i32();
7860         int pass, maxpasses;
7861
7862         if (is_scalar) {
7863             maxpasses = 1;
7864         } else {
7865             maxpasses = is_q ? 4 : 2;
7866         }
7867
7868         for (pass = 0; pass < maxpasses; pass++) {
7869             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7870
7871             switch (opcode) {
7872             case 0x3c: /* URECPE */
7873                 gen_helper_recpe_u32(tcg_res, tcg_op, fpst);
7874                 break;
7875             case 0x3d: /* FRECPE */
7876                 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
7877                 break;
7878             case 0x3f: /* FRECPX */
7879                 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
7880                 break;
7881             case 0x7d: /* FRSQRTE */
7882                 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
7883                 break;
7884             default:
7885                 g_assert_not_reached();
7886             }
7887
7888             if (is_scalar) {
7889                 write_fp_sreg(s, rd, tcg_res);
7890             } else {
7891                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7892             }
7893         }
7894         tcg_temp_free_i32(tcg_res);
7895         tcg_temp_free_i32(tcg_op);
7896         if (!is_q && !is_scalar) {
7897             clear_vec_high(s, rd);
7898         }
7899     }
7900     tcg_temp_free_ptr(fpst);
7901 }
7902
7903 static void handle_2misc_narrow(DisasContext *s, bool scalar,
7904                                 int opcode, bool u, bool is_q,
7905                                 int size, int rn, int rd)
7906 {
7907     /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
7908      * in the source becomes a size element in the destination).
7909      */
7910     int pass;
7911     TCGv_i32 tcg_res[2];
7912     int destelt = is_q ? 2 : 0;
7913     int passes = scalar ? 1 : 2;
7914
7915     if (scalar) {
7916         tcg_res[1] = tcg_const_i32(0);
7917     }
7918
7919     for (pass = 0; pass < passes; pass++) {
7920         TCGv_i64 tcg_op = tcg_temp_new_i64();
7921         NeonGenNarrowFn *genfn = NULL;
7922         NeonGenNarrowEnvFn *genenvfn = NULL;
7923
7924         if (scalar) {
7925             read_vec_element(s, tcg_op, rn, pass, size + 1);
7926         } else {
7927             read_vec_element(s, tcg_op, rn, pass, MO_64);
7928         }
7929         tcg_res[pass] = tcg_temp_new_i32();
7930
7931         switch (opcode) {
7932         case 0x12: /* XTN, SQXTUN */
7933         {
7934             static NeonGenNarrowFn * const xtnfns[3] = {
7935                 gen_helper_neon_narrow_u8,
7936                 gen_helper_neon_narrow_u16,
7937                 tcg_gen_extrl_i64_i32,
7938             };
7939             static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
7940                 gen_helper_neon_unarrow_sat8,
7941                 gen_helper_neon_unarrow_sat16,
7942                 gen_helper_neon_unarrow_sat32,
7943             };
7944             if (u) {
7945                 genenvfn = sqxtunfns[size];
7946             } else {
7947                 genfn = xtnfns[size];
7948             }
7949             break;
7950         }
7951         case 0x14: /* SQXTN, UQXTN */
7952         {
7953             static NeonGenNarrowEnvFn * const fns[3][2] = {
7954                 { gen_helper_neon_narrow_sat_s8,
7955                   gen_helper_neon_narrow_sat_u8 },
7956                 { gen_helper_neon_narrow_sat_s16,
7957                   gen_helper_neon_narrow_sat_u16 },
7958                 { gen_helper_neon_narrow_sat_s32,
7959                   gen_helper_neon_narrow_sat_u32 },
7960             };
7961             genenvfn = fns[size][u];
7962             break;
7963         }
7964         case 0x16: /* FCVTN, FCVTN2 */
7965             /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
7966             if (size == 2) {
7967                 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
7968             } else {
7969                 TCGv_i32 tcg_lo = tcg_temp_new_i32();
7970                 TCGv_i32 tcg_hi = tcg_temp_new_i32();
7971                 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
7972                 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, cpu_env);
7973                 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, cpu_env);
7974                 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
7975                 tcg_temp_free_i32(tcg_lo);
7976                 tcg_temp_free_i32(tcg_hi);
7977             }
7978             break;
7979         case 0x56:  /* FCVTXN, FCVTXN2 */
7980             /* 64 bit to 32 bit float conversion
7981              * with von Neumann rounding (round to odd)
7982              */
7983             assert(size == 2);
7984             gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
7985             break;
7986         default:
7987             g_assert_not_reached();
7988         }
7989
7990         if (genfn) {
7991             genfn(tcg_res[pass], tcg_op);
7992         } else if (genenvfn) {
7993             genenvfn(tcg_res[pass], cpu_env, tcg_op);
7994         }
7995
7996         tcg_temp_free_i64(tcg_op);
7997     }
7998
7999     for (pass = 0; pass < 2; pass++) {
8000         write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
8001         tcg_temp_free_i32(tcg_res[pass]);
8002     }
8003     if (!is_q) {
8004         clear_vec_high(s, rd);
8005     }
8006 }
8007
8008 /* Remaining saturating accumulating ops */
8009 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
8010                                 bool is_q, int size, int rn, int rd)
8011 {
8012     bool is_double = (size == 3);
8013
8014     if (is_double) {
8015         TCGv_i64 tcg_rn = tcg_temp_new_i64();
8016         TCGv_i64 tcg_rd = tcg_temp_new_i64();
8017         int pass;
8018
8019         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
8020             read_vec_element(s, tcg_rn, rn, pass, MO_64);
8021             read_vec_element(s, tcg_rd, rd, pass, MO_64);
8022
8023             if (is_u) { /* USQADD */
8024                 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8025             } else { /* SUQADD */
8026                 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8027             }
8028             write_vec_element(s, tcg_rd, rd, pass, MO_64);
8029         }
8030         if (is_scalar) {
8031             clear_vec_high(s, rd);
8032         }
8033
8034         tcg_temp_free_i64(tcg_rd);
8035         tcg_temp_free_i64(tcg_rn);
8036     } else {
8037         TCGv_i32 tcg_rn = tcg_temp_new_i32();
8038         TCGv_i32 tcg_rd = tcg_temp_new_i32();
8039         int pass, maxpasses;
8040
8041         if (is_scalar) {
8042             maxpasses = 1;
8043         } else {
8044             maxpasses = is_q ? 4 : 2;
8045         }
8046
8047         for (pass = 0; pass < maxpasses; pass++) {
8048             if (is_scalar) {
8049                 read_vec_element_i32(s, tcg_rn, rn, pass, size);
8050                 read_vec_element_i32(s, tcg_rd, rd, pass, size);
8051             } else {
8052                 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
8053                 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
8054             }
8055
8056             if (is_u) { /* USQADD */
8057                 switch (size) {
8058                 case 0:
8059                     gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8060                     break;
8061                 case 1:
8062                     gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8063                     break;
8064                 case 2:
8065                     gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8066                     break;
8067                 default:
8068                     g_assert_not_reached();
8069                 }
8070             } else { /* SUQADD */
8071                 switch (size) {
8072                 case 0:
8073                     gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8074                     break;
8075                 case 1:
8076                     gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8077                     break;
8078                 case 2:
8079                     gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8080                     break;
8081                 default:
8082                     g_assert_not_reached();
8083                 }
8084             }
8085
8086             if (is_scalar) {
8087                 TCGv_i64 tcg_zero = tcg_const_i64(0);
8088                 write_vec_element(s, tcg_zero, rd, 0, MO_64);
8089                 tcg_temp_free_i64(tcg_zero);
8090             }
8091             write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
8092         }
8093
8094         if (!is_q) {
8095             clear_vec_high(s, rd);
8096         }
8097
8098         tcg_temp_free_i32(tcg_rd);
8099         tcg_temp_free_i32(tcg_rn);
8100     }
8101 }
8102
8103 /* AdvSIMD scalar two reg misc
8104  *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
8105  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8106  * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
8107  * +-----+---+-----------+------+-----------+--------+-----+------+------+
8108  */
8109 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
8110 {
8111     int rd = extract32(insn, 0, 5);
8112     int rn = extract32(insn, 5, 5);
8113     int opcode = extract32(insn, 12, 5);
8114     int size = extract32(insn, 22, 2);
8115     bool u = extract32(insn, 29, 1);
8116     bool is_fcvt = false;
8117     int rmode;
8118     TCGv_i32 tcg_rmode;
8119     TCGv_ptr tcg_fpstatus;
8120
8121     switch (opcode) {
8122     case 0x3: /* USQADD / SUQADD*/
8123         if (!fp_access_check(s)) {
8124             return;
8125         }
8126         handle_2misc_satacc(s, true, u, false, size, rn, rd);
8127         return;
8128     case 0x7: /* SQABS / SQNEG */
8129         break;
8130     case 0xa: /* CMLT */
8131         if (u) {
8132             unallocated_encoding(s);
8133             return;
8134         }
8135         /* fall through */
8136     case 0x8: /* CMGT, CMGE */
8137     case 0x9: /* CMEQ, CMLE */
8138     case 0xb: /* ABS, NEG */
8139         if (size != 3) {
8140             unallocated_encoding(s);
8141             return;
8142         }
8143         break;
8144     case 0x12: /* SQXTUN */
8145         if (!u) {
8146             unallocated_encoding(s);
8147             return;
8148         }
8149         /* fall through */
8150     case 0x14: /* SQXTN, UQXTN */
8151         if (size == 3) {
8152             unallocated_encoding(s);
8153             return;
8154         }
8155         if (!fp_access_check(s)) {
8156             return;
8157         }
8158         handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
8159         return;
8160     case 0xc ... 0xf:
8161     case 0x16 ... 0x1d:
8162     case 0x1f:
8163         /* Floating point: U, size[1] and opcode indicate operation;
8164          * size[0] indicates single or double precision.
8165          */
8166         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
8167         size = extract32(size, 0, 1) ? 3 : 2;
8168         switch (opcode) {
8169         case 0x2c: /* FCMGT (zero) */
8170         case 0x2d: /* FCMEQ (zero) */
8171         case 0x2e: /* FCMLT (zero) */
8172         case 0x6c: /* FCMGE (zero) */
8173         case 0x6d: /* FCMLE (zero) */
8174             handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
8175             return;
8176         case 0x1d: /* SCVTF */
8177         case 0x5d: /* UCVTF */
8178         {
8179             bool is_signed = (opcode == 0x1d);
8180             if (!fp_access_check(s)) {
8181                 return;
8182             }
8183             handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
8184             return;
8185         }
8186         case 0x3d: /* FRECPE */
8187         case 0x3f: /* FRECPX */
8188         case 0x7d: /* FRSQRTE */
8189             if (!fp_access_check(s)) {
8190                 return;
8191             }
8192             handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
8193             return;
8194         case 0x1a: /* FCVTNS */
8195         case 0x1b: /* FCVTMS */
8196         case 0x3a: /* FCVTPS */
8197         case 0x3b: /* FCVTZS */
8198         case 0x5a: /* FCVTNU */
8199         case 0x5b: /* FCVTMU */
8200         case 0x7a: /* FCVTPU */
8201         case 0x7b: /* FCVTZU */
8202             is_fcvt = true;
8203             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
8204             break;
8205         case 0x1c: /* FCVTAS */
8206         case 0x5c: /* FCVTAU */
8207             /* TIEAWAY doesn't fit in the usual rounding mode encoding */
8208             is_fcvt = true;
8209             rmode = FPROUNDING_TIEAWAY;
8210             break;
8211         case 0x56: /* FCVTXN, FCVTXN2 */
8212             if (size == 2) {
8213                 unallocated_encoding(s);
8214                 return;
8215             }
8216             if (!fp_access_check(s)) {
8217                 return;
8218             }
8219             handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
8220             return;
8221         default:
8222             unallocated_encoding(s);
8223             return;
8224         }
8225         break;
8226     default:
8227         unallocated_encoding(s);
8228         return;
8229     }
8230
8231     if (!fp_access_check(s)) {
8232         return;
8233     }
8234
8235     if (is_fcvt) {
8236         tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
8237         gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
8238         tcg_fpstatus = get_fpstatus_ptr();
8239     } else {
8240         TCGV_UNUSED_I32(tcg_rmode);
8241         TCGV_UNUSED_PTR(tcg_fpstatus);
8242     }
8243
8244     if (size == 3) {
8245         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
8246         TCGv_i64 tcg_rd = tcg_temp_new_i64();
8247
8248         handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
8249         write_fp_dreg(s, rd, tcg_rd);
8250         tcg_temp_free_i64(tcg_rd);
8251         tcg_temp_free_i64(tcg_rn);
8252     } else {
8253         TCGv_i32 tcg_rn = tcg_temp_new_i32();
8254         TCGv_i32 tcg_rd = tcg_temp_new_i32();
8255
8256         read_vec_element_i32(s, tcg_rn, rn, 0, size);
8257
8258         switch (opcode) {
8259         case 0x7: /* SQABS, SQNEG */
8260         {
8261             NeonGenOneOpEnvFn *genfn;
8262             static NeonGenOneOpEnvFn * const fns[3][2] = {
8263                 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
8264                 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
8265                 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
8266             };
8267             genfn = fns[size][u];
8268             genfn(tcg_rd, cpu_env, tcg_rn);
8269             break;
8270         }
8271         case 0x1a: /* FCVTNS */
8272         case 0x1b: /* FCVTMS */
8273         case 0x1c: /* FCVTAS */
8274         case 0x3a: /* FCVTPS */
8275         case 0x3b: /* FCVTZS */
8276         {
8277             TCGv_i32 tcg_shift = tcg_const_i32(0);
8278             gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
8279             tcg_temp_free_i32(tcg_shift);
8280             break;
8281         }
8282         case 0x5a: /* FCVTNU */
8283         case 0x5b: /* FCVTMU */
8284         case 0x5c: /* FCVTAU */
8285         case 0x7a: /* FCVTPU */
8286         case 0x7b: /* FCVTZU */
8287         {
8288             TCGv_i32 tcg_shift = tcg_const_i32(0);
8289             gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
8290             tcg_temp_free_i32(tcg_shift);
8291             break;
8292         }
8293         default:
8294             g_assert_not_reached();
8295         }
8296
8297         write_fp_sreg(s, rd, tcg_rd);
8298         tcg_temp_free_i32(tcg_rd);
8299         tcg_temp_free_i32(tcg_rn);
8300     }
8301
8302     if (is_fcvt) {
8303         gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
8304         tcg_temp_free_i32(tcg_rmode);
8305         tcg_temp_free_ptr(tcg_fpstatus);
8306     }
8307 }
8308
8309 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
8310 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
8311                                  int immh, int immb, int opcode, int rn, int rd)
8312 {
8313     int size = 32 - clz32(immh) - 1;
8314     int immhb = immh << 3 | immb;
8315     int shift = 2 * (8 << size) - immhb;
8316     bool accumulate = false;
8317     bool round = false;
8318     bool insert = false;
8319     int dsize = is_q ? 128 : 64;
8320     int esize = 8 << size;
8321     int elements = dsize/esize;
8322     TCGMemOp memop = size | (is_u ? 0 : MO_SIGN);
8323     TCGv_i64 tcg_rn = new_tmp_a64(s);
8324     TCGv_i64 tcg_rd = new_tmp_a64(s);
8325     TCGv_i64 tcg_round;
8326     int i;
8327
8328     if (extract32(immh, 3, 1) && !is_q) {
8329         unallocated_encoding(s);
8330         return;
8331     }
8332
8333     if (size > 3 && !is_q) {
8334         unallocated_encoding(s);
8335         return;
8336     }
8337
8338     if (!fp_access_check(s)) {
8339         return;
8340     }
8341
8342     switch (opcode) {
8343     case 0x02: /* SSRA / USRA (accumulate) */
8344         accumulate = true;
8345         break;
8346     case 0x04: /* SRSHR / URSHR (rounding) */
8347         round = true;
8348         break;
8349     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8350         accumulate = round = true;
8351         break;
8352     case 0x08: /* SRI */
8353         insert = true;
8354         break;
8355     }
8356
8357     if (round) {
8358         uint64_t round_const = 1ULL << (shift - 1);
8359         tcg_round = tcg_const_i64(round_const);
8360     } else {
8361         TCGV_UNUSED_I64(tcg_round);
8362     }
8363
8364     for (i = 0; i < elements; i++) {
8365         read_vec_element(s, tcg_rn, rn, i, memop);
8366         if (accumulate || insert) {
8367             read_vec_element(s, tcg_rd, rd, i, memop);
8368         }
8369
8370         if (insert) {
8371             handle_shri_with_ins(tcg_rd, tcg_rn, size, shift);
8372         } else {
8373             handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8374                                     accumulate, is_u, size, shift);
8375         }
8376
8377         write_vec_element(s, tcg_rd, rd, i, size);
8378     }
8379
8380     if (!is_q) {
8381         clear_vec_high(s, rd);
8382     }
8383
8384     if (round) {
8385         tcg_temp_free_i64(tcg_round);
8386     }
8387 }
8388
8389 /* SHL/SLI - Vector shift left */
8390 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
8391                                 int immh, int immb, int opcode, int rn, int rd)
8392 {
8393     int size = 32 - clz32(immh) - 1;
8394     int immhb = immh << 3 | immb;
8395     int shift = immhb - (8 << size);
8396     int dsize = is_q ? 128 : 64;
8397     int esize = 8 << size;
8398     int elements = dsize/esize;
8399     TCGv_i64 tcg_rn = new_tmp_a64(s);
8400     TCGv_i64 tcg_rd = new_tmp_a64(s);
8401     int i;
8402
8403     if (extract32(immh, 3, 1) && !is_q) {
8404         unallocated_encoding(s);
8405         return;
8406     }
8407
8408     if (size > 3 && !is_q) {
8409         unallocated_encoding(s);
8410         return;
8411     }
8412
8413     if (!fp_access_check(s)) {
8414         return;
8415     }
8416
8417     for (i = 0; i < elements; i++) {
8418         read_vec_element(s, tcg_rn, rn, i, size);
8419         if (insert) {
8420             read_vec_element(s, tcg_rd, rd, i, size);
8421         }
8422
8423         handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
8424
8425         write_vec_element(s, tcg_rd, rd, i, size);
8426     }
8427
8428     if (!is_q) {
8429         clear_vec_high(s, rd);
8430     }
8431 }
8432
8433 /* USHLL/SHLL - Vector shift left with widening */
8434 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
8435                                  int immh, int immb, int opcode, int rn, int rd)
8436 {
8437     int size = 32 - clz32(immh) - 1;
8438     int immhb = immh << 3 | immb;
8439     int shift = immhb - (8 << size);
8440     int dsize = 64;
8441     int esize = 8 << size;
8442     int elements = dsize/esize;
8443     TCGv_i64 tcg_rn = new_tmp_a64(s);
8444     TCGv_i64 tcg_rd = new_tmp_a64(s);
8445     int i;
8446
8447     if (size >= 3) {
8448         unallocated_encoding(s);
8449         return;
8450     }
8451
8452     if (!fp_access_check(s)) {
8453         return;
8454     }
8455
8456     /* For the LL variants the store is larger than the load,
8457      * so if rd == rn we would overwrite parts of our input.
8458      * So load everything right now and use shifts in the main loop.
8459      */
8460     read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
8461
8462     for (i = 0; i < elements; i++) {
8463         tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
8464         ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
8465         tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
8466         write_vec_element(s, tcg_rd, rd, i, size + 1);
8467     }
8468 }
8469
8470 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
8471 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
8472                                  int immh, int immb, int opcode, int rn, int rd)
8473 {
8474     int immhb = immh << 3 | immb;
8475     int size = 32 - clz32(immh) - 1;
8476     int dsize = 64;
8477     int esize = 8 << size;
8478     int elements = dsize/esize;
8479     int shift = (2 * esize) - immhb;
8480     bool round = extract32(opcode, 0, 1);
8481     TCGv_i64 tcg_rn, tcg_rd, tcg_final;
8482     TCGv_i64 tcg_round;
8483     int i;
8484
8485     if (extract32(immh, 3, 1)) {
8486         unallocated_encoding(s);
8487         return;
8488     }
8489
8490     if (!fp_access_check(s)) {
8491         return;
8492     }
8493
8494     tcg_rn = tcg_temp_new_i64();
8495     tcg_rd = tcg_temp_new_i64();
8496     tcg_final = tcg_temp_new_i64();
8497     read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
8498
8499     if (round) {
8500         uint64_t round_const = 1ULL << (shift - 1);
8501         tcg_round = tcg_const_i64(round_const);
8502     } else {
8503         TCGV_UNUSED_I64(tcg_round);
8504     }
8505
8506     for (i = 0; i < elements; i++) {
8507         read_vec_element(s, tcg_rn, rn, i, size+1);
8508         handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8509                                 false, true, size+1, shift);
8510
8511         tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8512     }
8513
8514     if (!is_q) {
8515         clear_vec_high(s, rd);
8516         write_vec_element(s, tcg_final, rd, 0, MO_64);
8517     } else {
8518         write_vec_element(s, tcg_final, rd, 1, MO_64);
8519     }
8520
8521     if (round) {
8522         tcg_temp_free_i64(tcg_round);
8523     }
8524     tcg_temp_free_i64(tcg_rn);
8525     tcg_temp_free_i64(tcg_rd);
8526     tcg_temp_free_i64(tcg_final);
8527     return;
8528 }
8529
8530
8531 /* AdvSIMD shift by immediate
8532  *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
8533  * +---+---+---+-------------+------+------+--------+---+------+------+
8534  * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
8535  * +---+---+---+-------------+------+------+--------+---+------+------+
8536  */
8537 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
8538 {
8539     int rd = extract32(insn, 0, 5);
8540     int rn = extract32(insn, 5, 5);
8541     int opcode = extract32(insn, 11, 5);
8542     int immb = extract32(insn, 16, 3);
8543     int immh = extract32(insn, 19, 4);
8544     bool is_u = extract32(insn, 29, 1);
8545     bool is_q = extract32(insn, 30, 1);
8546
8547     switch (opcode) {
8548     case 0x08: /* SRI */
8549         if (!is_u) {
8550             unallocated_encoding(s);
8551             return;
8552         }
8553         /* fall through */
8554     case 0x00: /* SSHR / USHR */
8555     case 0x02: /* SSRA / USRA (accumulate) */
8556     case 0x04: /* SRSHR / URSHR (rounding) */
8557     case 0x06: /* SRSRA / URSRA (accum + rounding) */
8558         handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
8559         break;
8560     case 0x0a: /* SHL / SLI */
8561         handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
8562         break;
8563     case 0x10: /* SHRN */
8564     case 0x11: /* RSHRN / SQRSHRUN */
8565         if (is_u) {
8566             handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
8567                                    opcode, rn, rd);
8568         } else {
8569             handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
8570         }
8571         break;
8572     case 0x12: /* SQSHRN / UQSHRN */
8573     case 0x13: /* SQRSHRN / UQRSHRN */
8574         handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
8575                                opcode, rn, rd);
8576         break;
8577     case 0x14: /* SSHLL / USHLL */
8578         handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
8579         break;
8580     case 0x1c: /* SCVTF / UCVTF */
8581         handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
8582                                      opcode, rn, rd);
8583         break;
8584     case 0xc: /* SQSHLU */
8585         if (!is_u) {
8586             unallocated_encoding(s);
8587             return;
8588         }
8589         handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
8590         break;
8591     case 0xe: /* SQSHL, UQSHL */
8592         handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
8593         break;
8594     case 0x1f: /* FCVTZS/ FCVTZU */
8595         handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
8596         return;
8597     default:
8598         unallocated_encoding(s);
8599         return;
8600     }
8601 }
8602
8603 /* Generate code to do a "long" addition or subtraction, ie one done in
8604  * TCGv_i64 on vector lanes twice the width specified by size.
8605  */
8606 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
8607                           TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
8608 {
8609     static NeonGenTwo64OpFn * const fns[3][2] = {
8610         { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
8611         { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
8612         { tcg_gen_add_i64, tcg_gen_sub_i64 },
8613     };
8614     NeonGenTwo64OpFn *genfn;
8615     assert(size < 3);
8616
8617     genfn = fns[size][is_sub];
8618     genfn(tcg_res, tcg_op1, tcg_op2);
8619 }
8620
8621 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
8622                                 int opcode, int rd, int rn, int rm)
8623 {
8624     /* 3-reg-different widening insns: 64 x 64 -> 128 */
8625     TCGv_i64 tcg_res[2];
8626     int pass, accop;
8627
8628     tcg_res[0] = tcg_temp_new_i64();
8629     tcg_res[1] = tcg_temp_new_i64();
8630
8631     /* Does this op do an adding accumulate, a subtracting accumulate,
8632      * or no accumulate at all?
8633      */
8634     switch (opcode) {
8635     case 5:
8636     case 8:
8637     case 9:
8638         accop = 1;
8639         break;
8640     case 10:
8641     case 11:
8642         accop = -1;
8643         break;
8644     default:
8645         accop = 0;
8646         break;
8647     }
8648
8649     if (accop != 0) {
8650         read_vec_element(s, tcg_res[0], rd, 0, MO_64);
8651         read_vec_element(s, tcg_res[1], rd, 1, MO_64);
8652     }
8653
8654     /* size == 2 means two 32x32->64 operations; this is worth special
8655      * casing because we can generally handle it inline.
8656      */
8657     if (size == 2) {
8658         for (pass = 0; pass < 2; pass++) {
8659             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8660             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8661             TCGv_i64 tcg_passres;
8662             TCGMemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
8663
8664             int elt = pass + is_q * 2;
8665
8666             read_vec_element(s, tcg_op1, rn, elt, memop);
8667             read_vec_element(s, tcg_op2, rm, elt, memop);
8668
8669             if (accop == 0) {
8670                 tcg_passres = tcg_res[pass];
8671             } else {
8672                 tcg_passres = tcg_temp_new_i64();
8673             }
8674
8675             switch (opcode) {
8676             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8677                 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
8678                 break;
8679             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8680                 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
8681                 break;
8682             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8683             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8684             {
8685                 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
8686                 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
8687
8688                 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
8689                 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
8690                 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
8691                                     tcg_passres,
8692                                     tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
8693                 tcg_temp_free_i64(tcg_tmp1);
8694                 tcg_temp_free_i64(tcg_tmp2);
8695                 break;
8696             }
8697             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
8698             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
8699             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
8700                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
8701                 break;
8702             case 9: /* SQDMLAL, SQDMLAL2 */
8703             case 11: /* SQDMLSL, SQDMLSL2 */
8704             case 13: /* SQDMULL, SQDMULL2 */
8705                 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
8706                 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
8707                                                   tcg_passres, tcg_passres);
8708                 break;
8709             default:
8710                 g_assert_not_reached();
8711             }
8712
8713             if (opcode == 9 || opcode == 11) {
8714                 /* saturating accumulate ops */
8715                 if (accop < 0) {
8716                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
8717                 }
8718                 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
8719                                                   tcg_res[pass], tcg_passres);
8720             } else if (accop > 0) {
8721                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
8722             } else if (accop < 0) {
8723                 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
8724             }
8725
8726             if (accop != 0) {
8727                 tcg_temp_free_i64(tcg_passres);
8728             }
8729
8730             tcg_temp_free_i64(tcg_op1);
8731             tcg_temp_free_i64(tcg_op2);
8732         }
8733     } else {
8734         /* size 0 or 1, generally helper functions */
8735         for (pass = 0; pass < 2; pass++) {
8736             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8737             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8738             TCGv_i64 tcg_passres;
8739             int elt = pass + is_q * 2;
8740
8741             read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
8742             read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
8743
8744             if (accop == 0) {
8745                 tcg_passres = tcg_res[pass];
8746             } else {
8747                 tcg_passres = tcg_temp_new_i64();
8748             }
8749
8750             switch (opcode) {
8751             case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8752             case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8753             {
8754                 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
8755                 static NeonGenWidenFn * const widenfns[2][2] = {
8756                     { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
8757                     { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
8758                 };
8759                 NeonGenWidenFn *widenfn = widenfns[size][is_u];
8760
8761                 widenfn(tcg_op2_64, tcg_op2);
8762                 widenfn(tcg_passres, tcg_op1);
8763                 gen_neon_addl(size, (opcode == 2), tcg_passres,
8764                               tcg_passres, tcg_op2_64);
8765                 tcg_temp_free_i64(tcg_op2_64);
8766                 break;
8767             }
8768             case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8769             case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8770                 if (size == 0) {
8771                     if (is_u) {
8772                         gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
8773                     } else {
8774                         gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
8775                     }
8776                 } else {
8777                     if (is_u) {
8778                         gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
8779                     } else {
8780                         gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
8781                     }
8782                 }
8783                 break;
8784             case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
8785             case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
8786             case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
8787                 if (size == 0) {
8788                     if (is_u) {
8789                         gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
8790                     } else {
8791                         gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
8792                     }
8793                 } else {
8794                     if (is_u) {
8795                         gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
8796                     } else {
8797                         gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
8798                     }
8799                 }
8800                 break;
8801             case 9: /* SQDMLAL, SQDMLAL2 */
8802             case 11: /* SQDMLSL, SQDMLSL2 */
8803             case 13: /* SQDMULL, SQDMULL2 */
8804                 assert(size == 1);
8805                 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
8806                 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
8807                                                   tcg_passres, tcg_passres);
8808                 break;
8809             case 14: /* PMULL */
8810                 assert(size == 0);
8811                 gen_helper_neon_mull_p8(tcg_passres, tcg_op1, tcg_op2);
8812                 break;
8813             default:
8814                 g_assert_not_reached();
8815             }
8816             tcg_temp_free_i32(tcg_op1);
8817             tcg_temp_free_i32(tcg_op2);
8818
8819             if (accop != 0) {
8820                 if (opcode == 9 || opcode == 11) {
8821                     /* saturating accumulate ops */
8822                     if (accop < 0) {
8823                         gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
8824                     }
8825                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
8826                                                       tcg_res[pass],
8827                                                       tcg_passres);
8828                 } else {
8829                     gen_neon_addl(size, (accop < 0), tcg_res[pass],
8830                                   tcg_res[pass], tcg_passres);
8831                 }
8832                 tcg_temp_free_i64(tcg_passres);
8833             }
8834         }
8835     }
8836
8837     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
8838     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
8839     tcg_temp_free_i64(tcg_res[0]);
8840     tcg_temp_free_i64(tcg_res[1]);
8841 }
8842
8843 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
8844                             int opcode, int rd, int rn, int rm)
8845 {
8846     TCGv_i64 tcg_res[2];
8847     int part = is_q ? 2 : 0;
8848     int pass;
8849
8850     for (pass = 0; pass < 2; pass++) {
8851         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8852         TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8853         TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
8854         static NeonGenWidenFn * const widenfns[3][2] = {
8855             { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
8856             { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
8857             { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
8858         };
8859         NeonGenWidenFn *widenfn = widenfns[size][is_u];
8860
8861         read_vec_element(s, tcg_op1, rn, pass, MO_64);
8862         read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
8863         widenfn(tcg_op2_wide, tcg_op2);
8864         tcg_temp_free_i32(tcg_op2);
8865         tcg_res[pass] = tcg_temp_new_i64();
8866         gen_neon_addl(size, (opcode == 3),
8867                       tcg_res[pass], tcg_op1, tcg_op2_wide);
8868         tcg_temp_free_i64(tcg_op1);
8869         tcg_temp_free_i64(tcg_op2_wide);
8870     }
8871
8872     for (pass = 0; pass < 2; pass++) {
8873         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
8874         tcg_temp_free_i64(tcg_res[pass]);
8875     }
8876 }
8877
8878 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
8879 {
8880     tcg_gen_addi_i64(in, in, 1U << 31);
8881     tcg_gen_extrh_i64_i32(res, in);
8882 }
8883
8884 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
8885                                  int opcode, int rd, int rn, int rm)
8886 {
8887     TCGv_i32 tcg_res[2];
8888     int part = is_q ? 2 : 0;
8889     int pass;
8890
8891     for (pass = 0; pass < 2; pass++) {
8892         TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8893         TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8894         TCGv_i64 tcg_wideres = tcg_temp_new_i64();
8895         static NeonGenNarrowFn * const narrowfns[3][2] = {
8896             { gen_helper_neon_narrow_high_u8,
8897               gen_helper_neon_narrow_round_high_u8 },
8898             { gen_helper_neon_narrow_high_u16,
8899               gen_helper_neon_narrow_round_high_u16 },
8900             { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
8901         };
8902         NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
8903
8904         read_vec_element(s, tcg_op1, rn, pass, MO_64);
8905         read_vec_element(s, tcg_op2, rm, pass, MO_64);
8906
8907         gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
8908
8909         tcg_temp_free_i64(tcg_op1);
8910         tcg_temp_free_i64(tcg_op2);
8911
8912         tcg_res[pass] = tcg_temp_new_i32();
8913         gennarrow(tcg_res[pass], tcg_wideres);
8914         tcg_temp_free_i64(tcg_wideres);
8915     }
8916
8917     for (pass = 0; pass < 2; pass++) {
8918         write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
8919         tcg_temp_free_i32(tcg_res[pass]);
8920     }
8921     if (!is_q) {
8922         clear_vec_high(s, rd);
8923     }
8924 }
8925
8926 static void handle_pmull_64(DisasContext *s, int is_q, int rd, int rn, int rm)
8927 {
8928     /* PMULL of 64 x 64 -> 128 is an odd special case because it
8929      * is the only three-reg-diff instruction which produces a
8930      * 128-bit wide result from a single operation. However since
8931      * it's possible to calculate the two halves more or less
8932      * separately we just use two helper calls.
8933      */
8934     TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8935     TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8936     TCGv_i64 tcg_res = tcg_temp_new_i64();
8937
8938     read_vec_element(s, tcg_op1, rn, is_q, MO_64);
8939     read_vec_element(s, tcg_op2, rm, is_q, MO_64);
8940     gen_helper_neon_pmull_64_lo(tcg_res, tcg_op1, tcg_op2);
8941     write_vec_element(s, tcg_res, rd, 0, MO_64);
8942     gen_helper_neon_pmull_64_hi(tcg_res, tcg_op1, tcg_op2);
8943     write_vec_element(s, tcg_res, rd, 1, MO_64);
8944
8945     tcg_temp_free_i64(tcg_op1);
8946     tcg_temp_free_i64(tcg_op2);
8947     tcg_temp_free_i64(tcg_res);
8948 }
8949
8950 /* AdvSIMD three different
8951  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
8952  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
8953  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
8954  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
8955  */
8956 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
8957 {
8958     /* Instructions in this group fall into three basic classes
8959      * (in each case with the operation working on each element in
8960      * the input vectors):
8961      * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
8962      *     128 bit input)
8963      * (2) wide 64 x 128 -> 128
8964      * (3) narrowing 128 x 128 -> 64
8965      * Here we do initial decode, catch unallocated cases and
8966      * dispatch to separate functions for each class.
8967      */
8968     int is_q = extract32(insn, 30, 1);
8969     int is_u = extract32(insn, 29, 1);
8970     int size = extract32(insn, 22, 2);
8971     int opcode = extract32(insn, 12, 4);
8972     int rm = extract32(insn, 16, 5);
8973     int rn = extract32(insn, 5, 5);
8974     int rd = extract32(insn, 0, 5);
8975
8976     switch (opcode) {
8977     case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
8978     case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
8979         /* 64 x 128 -> 128 */
8980         if (size == 3) {
8981             unallocated_encoding(s);
8982             return;
8983         }
8984         if (!fp_access_check(s)) {
8985             return;
8986         }
8987         handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
8988         break;
8989     case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
8990     case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
8991         /* 128 x 128 -> 64 */
8992         if (size == 3) {
8993             unallocated_encoding(s);
8994             return;
8995         }
8996         if (!fp_access_check(s)) {
8997             return;
8998         }
8999         handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
9000         break;
9001     case 14: /* PMULL, PMULL2 */
9002         if (is_u || size == 1 || size == 2) {
9003             unallocated_encoding(s);
9004             return;
9005         }
9006         if (size == 3) {
9007             if (!arm_dc_feature(s, ARM_FEATURE_V8_PMULL)) {
9008                 unallocated_encoding(s);
9009                 return;
9010             }
9011             if (!fp_access_check(s)) {
9012                 return;
9013             }
9014             handle_pmull_64(s, is_q, rd, rn, rm);
9015             return;
9016         }
9017         goto is_widening;
9018     case 9: /* SQDMLAL, SQDMLAL2 */
9019     case 11: /* SQDMLSL, SQDMLSL2 */
9020     case 13: /* SQDMULL, SQDMULL2 */
9021         if (is_u || size == 0) {
9022             unallocated_encoding(s);
9023             return;
9024         }
9025         /* fall through */
9026     case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
9027     case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
9028     case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
9029     case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
9030     case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
9031     case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
9032     case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
9033         /* 64 x 64 -> 128 */
9034         if (size == 3) {
9035             unallocated_encoding(s);
9036             return;
9037         }
9038     is_widening:
9039         if (!fp_access_check(s)) {
9040             return;
9041         }
9042
9043         handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
9044         break;
9045     default:
9046         /* opcode 15 not allocated */
9047         unallocated_encoding(s);
9048         break;
9049     }
9050 }
9051
9052 /* Logic op (opcode == 3) subgroup of C3.6.16. */
9053 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
9054 {
9055     int rd = extract32(insn, 0, 5);
9056     int rn = extract32(insn, 5, 5);
9057     int rm = extract32(insn, 16, 5);
9058     int size = extract32(insn, 22, 2);
9059     bool is_u = extract32(insn, 29, 1);
9060     bool is_q = extract32(insn, 30, 1);
9061     TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
9062     int pass;
9063
9064     if (!fp_access_check(s)) {
9065         return;
9066     }
9067
9068     tcg_op1 = tcg_temp_new_i64();
9069     tcg_op2 = tcg_temp_new_i64();
9070     tcg_res[0] = tcg_temp_new_i64();
9071     tcg_res[1] = tcg_temp_new_i64();
9072
9073     for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
9074         read_vec_element(s, tcg_op1, rn, pass, MO_64);
9075         read_vec_element(s, tcg_op2, rm, pass, MO_64);
9076
9077         if (!is_u) {
9078             switch (size) {
9079             case 0: /* AND */
9080                 tcg_gen_and_i64(tcg_res[pass], tcg_op1, tcg_op2);
9081                 break;
9082             case 1: /* BIC */
9083                 tcg_gen_andc_i64(tcg_res[pass], tcg_op1, tcg_op2);
9084                 break;
9085             case 2: /* ORR */
9086                 tcg_gen_or_i64(tcg_res[pass], tcg_op1, tcg_op2);
9087                 break;
9088             case 3: /* ORN */
9089                 tcg_gen_orc_i64(tcg_res[pass], tcg_op1, tcg_op2);
9090                 break;
9091             }
9092         } else {
9093             if (size != 0) {
9094                 /* B* ops need res loaded to operate on */
9095                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9096             }
9097
9098             switch (size) {
9099             case 0: /* EOR */
9100                 tcg_gen_xor_i64(tcg_res[pass], tcg_op1, tcg_op2);
9101                 break;
9102             case 1: /* BSL bitwise select */
9103                 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_op2);
9104                 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_res[pass]);
9105                 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op1);
9106                 break;
9107             case 2: /* BIT, bitwise insert if true */
9108                 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
9109                 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_op2);
9110                 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
9111                 break;
9112             case 3: /* BIF, bitwise insert if false */
9113                 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
9114                 tcg_gen_andc_i64(tcg_op1, tcg_op1, tcg_op2);
9115                 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
9116                 break;
9117             }
9118         }
9119     }
9120
9121     write_vec_element(s, tcg_res[0], rd, 0, MO_64);
9122     if (!is_q) {
9123         tcg_gen_movi_i64(tcg_res[1], 0);
9124     }
9125     write_vec_element(s, tcg_res[1], rd, 1, MO_64);
9126
9127     tcg_temp_free_i64(tcg_op1);
9128     tcg_temp_free_i64(tcg_op2);
9129     tcg_temp_free_i64(tcg_res[0]);
9130     tcg_temp_free_i64(tcg_res[1]);
9131 }
9132
9133 /* Helper functions for 32 bit comparisons */
9134 static void gen_max_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9135 {
9136     tcg_gen_movcond_i32(TCG_COND_GE, res, op1, op2, op1, op2);
9137 }
9138
9139 static void gen_max_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9140 {
9141     tcg_gen_movcond_i32(TCG_COND_GEU, res, op1, op2, op1, op2);
9142 }
9143
9144 static void gen_min_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9145 {
9146     tcg_gen_movcond_i32(TCG_COND_LE, res, op1, op2, op1, op2);
9147 }
9148
9149 static void gen_min_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9150 {
9151     tcg_gen_movcond_i32(TCG_COND_LEU, res, op1, op2, op1, op2);
9152 }
9153
9154 /* Pairwise op subgroup of C3.6.16.
9155  *
9156  * This is called directly or via the handle_3same_float for float pairwise
9157  * operations where the opcode and size are calculated differently.
9158  */
9159 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
9160                                    int size, int rn, int rm, int rd)
9161 {
9162     TCGv_ptr fpst;
9163     int pass;
9164
9165     /* Floating point operations need fpst */
9166     if (opcode >= 0x58) {
9167         fpst = get_fpstatus_ptr();
9168     } else {
9169         TCGV_UNUSED_PTR(fpst);
9170     }
9171
9172     if (!fp_access_check(s)) {
9173         return;
9174     }
9175
9176     /* These operations work on the concatenated rm:rn, with each pair of
9177      * adjacent elements being operated on to produce an element in the result.
9178      */
9179     if (size == 3) {
9180         TCGv_i64 tcg_res[2];
9181
9182         for (pass = 0; pass < 2; pass++) {
9183             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9184             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9185             int passreg = (pass == 0) ? rn : rm;
9186
9187             read_vec_element(s, tcg_op1, passreg, 0, MO_64);
9188             read_vec_element(s, tcg_op2, passreg, 1, MO_64);
9189             tcg_res[pass] = tcg_temp_new_i64();
9190
9191             switch (opcode) {
9192             case 0x17: /* ADDP */
9193                 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
9194                 break;
9195             case 0x58: /* FMAXNMP */
9196                 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9197                 break;
9198             case 0x5a: /* FADDP */
9199                 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9200                 break;
9201             case 0x5e: /* FMAXP */
9202                 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9203                 break;
9204             case 0x78: /* FMINNMP */
9205                 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9206                 break;
9207             case 0x7e: /* FMINP */
9208                 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9209                 break;
9210             default:
9211                 g_assert_not_reached();
9212             }
9213
9214             tcg_temp_free_i64(tcg_op1);
9215             tcg_temp_free_i64(tcg_op2);
9216         }
9217
9218         for (pass = 0; pass < 2; pass++) {
9219             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9220             tcg_temp_free_i64(tcg_res[pass]);
9221         }
9222     } else {
9223         int maxpass = is_q ? 4 : 2;
9224         TCGv_i32 tcg_res[4];
9225
9226         for (pass = 0; pass < maxpass; pass++) {
9227             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9228             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9229             NeonGenTwoOpFn *genfn = NULL;
9230             int passreg = pass < (maxpass / 2) ? rn : rm;
9231             int passelt = (is_q && (pass & 1)) ? 2 : 0;
9232
9233             read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
9234             read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
9235             tcg_res[pass] = tcg_temp_new_i32();
9236
9237             switch (opcode) {
9238             case 0x17: /* ADDP */
9239             {
9240                 static NeonGenTwoOpFn * const fns[3] = {
9241                     gen_helper_neon_padd_u8,
9242                     gen_helper_neon_padd_u16,
9243                     tcg_gen_add_i32,
9244                 };
9245                 genfn = fns[size];
9246                 break;
9247             }
9248             case 0x14: /* SMAXP, UMAXP */
9249             {
9250                 static NeonGenTwoOpFn * const fns[3][2] = {
9251                     { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
9252                     { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
9253                     { gen_max_s32, gen_max_u32 },
9254                 };
9255                 genfn = fns[size][u];
9256                 break;
9257             }
9258             case 0x15: /* SMINP, UMINP */
9259             {
9260                 static NeonGenTwoOpFn * const fns[3][2] = {
9261                     { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
9262                     { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
9263                     { gen_min_s32, gen_min_u32 },
9264                 };
9265                 genfn = fns[size][u];
9266                 break;
9267             }
9268             /* The FP operations are all on single floats (32 bit) */
9269             case 0x58: /* FMAXNMP */
9270                 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9271                 break;
9272             case 0x5a: /* FADDP */
9273                 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9274                 break;
9275             case 0x5e: /* FMAXP */
9276                 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9277                 break;
9278             case 0x78: /* FMINNMP */
9279                 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9280                 break;
9281             case 0x7e: /* FMINP */
9282                 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9283                 break;
9284             default:
9285                 g_assert_not_reached();
9286             }
9287
9288             /* FP ops called directly, otherwise call now */
9289             if (genfn) {
9290                 genfn(tcg_res[pass], tcg_op1, tcg_op2);
9291             }
9292
9293             tcg_temp_free_i32(tcg_op1);
9294             tcg_temp_free_i32(tcg_op2);
9295         }
9296
9297         for (pass = 0; pass < maxpass; pass++) {
9298             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
9299             tcg_temp_free_i32(tcg_res[pass]);
9300         }
9301         if (!is_q) {
9302             clear_vec_high(s, rd);
9303         }
9304     }
9305
9306     if (!TCGV_IS_UNUSED_PTR(fpst)) {
9307         tcg_temp_free_ptr(fpst);
9308     }
9309 }
9310
9311 /* Floating point op subgroup of C3.6.16. */
9312 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
9313 {
9314     /* For floating point ops, the U, size[1] and opcode bits
9315      * together indicate the operation. size[0] indicates single
9316      * or double.
9317      */
9318     int fpopcode = extract32(insn, 11, 5)
9319         | (extract32(insn, 23, 1) << 5)
9320         | (extract32(insn, 29, 1) << 6);
9321     int is_q = extract32(insn, 30, 1);
9322     int size = extract32(insn, 22, 1);
9323     int rm = extract32(insn, 16, 5);
9324     int rn = extract32(insn, 5, 5);
9325     int rd = extract32(insn, 0, 5);
9326
9327     int datasize = is_q ? 128 : 64;
9328     int esize = 32 << size;
9329     int elements = datasize / esize;
9330
9331     if (size == 1 && !is_q) {
9332         unallocated_encoding(s);
9333         return;
9334     }
9335
9336     switch (fpopcode) {
9337     case 0x58: /* FMAXNMP */
9338     case 0x5a: /* FADDP */
9339     case 0x5e: /* FMAXP */
9340     case 0x78: /* FMINNMP */
9341     case 0x7e: /* FMINP */
9342         if (size && !is_q) {
9343             unallocated_encoding(s);
9344             return;
9345         }
9346         handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
9347                                rn, rm, rd);
9348         return;
9349     case 0x1b: /* FMULX */
9350     case 0x1f: /* FRECPS */
9351     case 0x3f: /* FRSQRTS */
9352     case 0x5d: /* FACGE */
9353     case 0x7d: /* FACGT */
9354     case 0x19: /* FMLA */
9355     case 0x39: /* FMLS */
9356     case 0x18: /* FMAXNM */
9357     case 0x1a: /* FADD */
9358     case 0x1c: /* FCMEQ */
9359     case 0x1e: /* FMAX */
9360     case 0x38: /* FMINNM */
9361     case 0x3a: /* FSUB */
9362     case 0x3e: /* FMIN */
9363     case 0x5b: /* FMUL */
9364     case 0x5c: /* FCMGE */
9365     case 0x5f: /* FDIV */
9366     case 0x7a: /* FABD */
9367     case 0x7c: /* FCMGT */
9368         if (!fp_access_check(s)) {
9369             return;
9370         }
9371
9372         handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
9373         return;
9374     default:
9375         unallocated_encoding(s);
9376         return;
9377     }
9378 }
9379
9380 /* Integer op subgroup of C3.6.16. */
9381 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
9382 {
9383     int is_q = extract32(insn, 30, 1);
9384     int u = extract32(insn, 29, 1);
9385     int size = extract32(insn, 22, 2);
9386     int opcode = extract32(insn, 11, 5);
9387     int rm = extract32(insn, 16, 5);
9388     int rn = extract32(insn, 5, 5);
9389     int rd = extract32(insn, 0, 5);
9390     int pass;
9391
9392     switch (opcode) {
9393     case 0x13: /* MUL, PMUL */
9394         if (u && size != 0) {
9395             unallocated_encoding(s);
9396             return;
9397         }
9398         /* fall through */
9399     case 0x0: /* SHADD, UHADD */
9400     case 0x2: /* SRHADD, URHADD */
9401     case 0x4: /* SHSUB, UHSUB */
9402     case 0xc: /* SMAX, UMAX */
9403     case 0xd: /* SMIN, UMIN */
9404     case 0xe: /* SABD, UABD */
9405     case 0xf: /* SABA, UABA */
9406     case 0x12: /* MLA, MLS */
9407         if (size == 3) {
9408             unallocated_encoding(s);
9409             return;
9410         }
9411         break;
9412     case 0x16: /* SQDMULH, SQRDMULH */
9413         if (size == 0 || size == 3) {
9414             unallocated_encoding(s);
9415             return;
9416         }
9417         break;
9418     default:
9419         if (size == 3 && !is_q) {
9420             unallocated_encoding(s);
9421             return;
9422         }
9423         break;
9424     }
9425
9426     if (!fp_access_check(s)) {
9427         return;
9428     }
9429
9430     if (size == 3) {
9431         assert(is_q);
9432         for (pass = 0; pass < 2; pass++) {
9433             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9434             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9435             TCGv_i64 tcg_res = tcg_temp_new_i64();
9436
9437             read_vec_element(s, tcg_op1, rn, pass, MO_64);
9438             read_vec_element(s, tcg_op2, rm, pass, MO_64);
9439
9440             handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
9441
9442             write_vec_element(s, tcg_res, rd, pass, MO_64);
9443
9444             tcg_temp_free_i64(tcg_res);
9445             tcg_temp_free_i64(tcg_op1);
9446             tcg_temp_free_i64(tcg_op2);
9447         }
9448     } else {
9449         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
9450             TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9451             TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9452             TCGv_i32 tcg_res = tcg_temp_new_i32();
9453             NeonGenTwoOpFn *genfn = NULL;
9454             NeonGenTwoOpEnvFn *genenvfn = NULL;
9455
9456             read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9457             read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9458
9459             switch (opcode) {
9460             case 0x0: /* SHADD, UHADD */
9461             {
9462                 static NeonGenTwoOpFn * const fns[3][2] = {
9463                     { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
9464                     { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
9465                     { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
9466                 };
9467                 genfn = fns[size][u];
9468                 break;
9469             }
9470             case 0x1: /* SQADD, UQADD */
9471             {
9472                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9473                     { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9474                     { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9475                     { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9476                 };
9477                 genenvfn = fns[size][u];
9478                 break;
9479             }
9480             case 0x2: /* SRHADD, URHADD */
9481             {
9482                 static NeonGenTwoOpFn * const fns[3][2] = {
9483                     { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
9484                     { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
9485                     { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
9486                 };
9487                 genfn = fns[size][u];
9488                 break;
9489             }
9490             case 0x4: /* SHSUB, UHSUB */
9491             {
9492                 static NeonGenTwoOpFn * const fns[3][2] = {
9493                     { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
9494                     { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
9495                     { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
9496                 };
9497                 genfn = fns[size][u];
9498                 break;
9499             }
9500             case 0x5: /* SQSUB, UQSUB */
9501             {
9502                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9503                     { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9504                     { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9505                     { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9506                 };
9507                 genenvfn = fns[size][u];
9508                 break;
9509             }
9510             case 0x6: /* CMGT, CMHI */
9511             {
9512                 static NeonGenTwoOpFn * const fns[3][2] = {
9513                     { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_u8 },
9514                     { gen_helper_neon_cgt_s16, gen_helper_neon_cgt_u16 },
9515                     { gen_helper_neon_cgt_s32, gen_helper_neon_cgt_u32 },
9516                 };
9517                 genfn = fns[size][u];
9518                 break;
9519             }
9520             case 0x7: /* CMGE, CMHS */
9521             {
9522                 static NeonGenTwoOpFn * const fns[3][2] = {
9523                     { gen_helper_neon_cge_s8, gen_helper_neon_cge_u8 },
9524                     { gen_helper_neon_cge_s16, gen_helper_neon_cge_u16 },
9525                     { gen_helper_neon_cge_s32, gen_helper_neon_cge_u32 },
9526                 };
9527                 genfn = fns[size][u];
9528                 break;
9529             }
9530             case 0x8: /* SSHL, USHL */
9531             {
9532                 static NeonGenTwoOpFn * const fns[3][2] = {
9533                     { gen_helper_neon_shl_s8, gen_helper_neon_shl_u8 },
9534                     { gen_helper_neon_shl_s16, gen_helper_neon_shl_u16 },
9535                     { gen_helper_neon_shl_s32, gen_helper_neon_shl_u32 },
9536                 };
9537                 genfn = fns[size][u];
9538                 break;
9539             }
9540             case 0x9: /* SQSHL, UQSHL */
9541             {
9542                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9543                     { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9544                     { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9545                     { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9546                 };
9547                 genenvfn = fns[size][u];
9548                 break;
9549             }
9550             case 0xa: /* SRSHL, URSHL */
9551             {
9552                 static NeonGenTwoOpFn * const fns[3][2] = {
9553                     { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
9554                     { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
9555                     { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
9556                 };
9557                 genfn = fns[size][u];
9558                 break;
9559             }
9560             case 0xb: /* SQRSHL, UQRSHL */
9561             {
9562                 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9563                     { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9564                     { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9565                     { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9566                 };
9567                 genenvfn = fns[size][u];
9568                 break;
9569             }
9570             case 0xc: /* SMAX, UMAX */
9571             {
9572                 static NeonGenTwoOpFn * const fns[3][2] = {
9573                     { gen_helper_neon_max_s8, gen_helper_neon_max_u8 },
9574                     { gen_helper_neon_max_s16, gen_helper_neon_max_u16 },
9575                     { gen_max_s32, gen_max_u32 },
9576                 };
9577                 genfn = fns[size][u];
9578                 break;
9579             }
9580
9581             case 0xd: /* SMIN, UMIN */
9582             {
9583                 static NeonGenTwoOpFn * const fns[3][2] = {
9584                     { gen_helper_neon_min_s8, gen_helper_neon_min_u8 },
9585                     { gen_helper_neon_min_s16, gen_helper_neon_min_u16 },
9586                     { gen_min_s32, gen_min_u32 },
9587                 };
9588                 genfn = fns[size][u];
9589                 break;
9590             }
9591             case 0xe: /* SABD, UABD */
9592             case 0xf: /* SABA, UABA */
9593             {
9594                 static NeonGenTwoOpFn * const fns[3][2] = {
9595                     { gen_helper_neon_abd_s8, gen_helper_neon_abd_u8 },
9596                     { gen_helper_neon_abd_s16, gen_helper_neon_abd_u16 },
9597                     { gen_helper_neon_abd_s32, gen_helper_neon_abd_u32 },
9598                 };
9599                 genfn = fns[size][u];
9600                 break;
9601             }
9602             case 0x10: /* ADD, SUB */
9603             {
9604                 static NeonGenTwoOpFn * const fns[3][2] = {
9605                     { gen_helper_neon_add_u8, gen_helper_neon_sub_u8 },
9606                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
9607                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
9608                 };
9609                 genfn = fns[size][u];
9610                 break;
9611             }
9612             case 0x11: /* CMTST, CMEQ */
9613             {
9614                 static NeonGenTwoOpFn * const fns[3][2] = {
9615                     { gen_helper_neon_tst_u8, gen_helper_neon_ceq_u8 },
9616                     { gen_helper_neon_tst_u16, gen_helper_neon_ceq_u16 },
9617                     { gen_helper_neon_tst_u32, gen_helper_neon_ceq_u32 },
9618                 };
9619                 genfn = fns[size][u];
9620                 break;
9621             }
9622             case 0x13: /* MUL, PMUL */
9623                 if (u) {
9624                     /* PMUL */
9625                     assert(size == 0);
9626                     genfn = gen_helper_neon_mul_p8;
9627                     break;
9628                 }
9629                 /* fall through : MUL */
9630             case 0x12: /* MLA, MLS */
9631             {
9632                 static NeonGenTwoOpFn * const fns[3] = {
9633                     gen_helper_neon_mul_u8,
9634                     gen_helper_neon_mul_u16,
9635                     tcg_gen_mul_i32,
9636                 };
9637                 genfn = fns[size];
9638                 break;
9639             }
9640             case 0x16: /* SQDMULH, SQRDMULH */
9641             {
9642                 static NeonGenTwoOpEnvFn * const fns[2][2] = {
9643                     { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9644                     { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9645                 };
9646                 assert(size == 1 || size == 2);
9647                 genenvfn = fns[size - 1][u];
9648                 break;
9649             }
9650             default:
9651                 g_assert_not_reached();
9652             }
9653
9654             if (genenvfn) {
9655                 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
9656             } else {
9657                 genfn(tcg_res, tcg_op1, tcg_op2);
9658             }
9659
9660             if (opcode == 0xf || opcode == 0x12) {
9661                 /* SABA, UABA, MLA, MLS: accumulating ops */
9662                 static NeonGenTwoOpFn * const fns[3][2] = {
9663                     { gen_helper_neon_add_u8, gen_helper_neon_sub_u8 },
9664                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
9665                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
9666                 };
9667                 bool is_sub = (opcode == 0x12 && u); /* MLS */
9668
9669                 genfn = fns[size][is_sub];
9670                 read_vec_element_i32(s, tcg_op1, rd, pass, MO_32);
9671                 genfn(tcg_res, tcg_op1, tcg_res);
9672             }
9673
9674             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9675
9676             tcg_temp_free_i32(tcg_res);
9677             tcg_temp_free_i32(tcg_op1);
9678             tcg_temp_free_i32(tcg_op2);
9679         }
9680     }
9681
9682     if (!is_q) {
9683         clear_vec_high(s, rd);
9684     }
9685 }
9686
9687 /* AdvSIMD three same
9688  *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
9689  * +---+---+---+-----------+------+---+------+--------+---+------+------+
9690  * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
9691  * +---+---+---+-----------+------+---+------+--------+---+------+------+
9692  */
9693 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
9694 {
9695     int opcode = extract32(insn, 11, 5);
9696
9697     switch (opcode) {
9698     case 0x3: /* logic ops */
9699         disas_simd_3same_logic(s, insn);
9700         break;
9701     case 0x17: /* ADDP */
9702     case 0x14: /* SMAXP, UMAXP */
9703     case 0x15: /* SMINP, UMINP */
9704     {
9705         /* Pairwise operations */
9706         int is_q = extract32(insn, 30, 1);
9707         int u = extract32(insn, 29, 1);
9708         int size = extract32(insn, 22, 2);
9709         int rm = extract32(insn, 16, 5);
9710         int rn = extract32(insn, 5, 5);
9711         int rd = extract32(insn, 0, 5);
9712         if (opcode == 0x17) {
9713             if (u || (size == 3 && !is_q)) {
9714                 unallocated_encoding(s);
9715                 return;
9716             }
9717         } else {
9718             if (size == 3) {
9719                 unallocated_encoding(s);
9720                 return;
9721             }
9722         }
9723         handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
9724         break;
9725     }
9726     case 0x18 ... 0x31:
9727         /* floating point ops, sz[1] and U are part of opcode */
9728         disas_simd_3same_float(s, insn);
9729         break;
9730     default:
9731         disas_simd_3same_int(s, insn);
9732         break;
9733     }
9734 }
9735
9736 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
9737                                   int size, int rn, int rd)
9738 {
9739     /* Handle 2-reg-misc ops which are widening (so each size element
9740      * in the source becomes a 2*size element in the destination.
9741      * The only instruction like this is FCVTL.
9742      */
9743     int pass;
9744
9745     if (size == 3) {
9746         /* 32 -> 64 bit fp conversion */
9747         TCGv_i64 tcg_res[2];
9748         int srcelt = is_q ? 2 : 0;
9749
9750         for (pass = 0; pass < 2; pass++) {
9751             TCGv_i32 tcg_op = tcg_temp_new_i32();
9752             tcg_res[pass] = tcg_temp_new_i64();
9753
9754             read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
9755             gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
9756             tcg_temp_free_i32(tcg_op);
9757         }
9758         for (pass = 0; pass < 2; pass++) {
9759             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9760             tcg_temp_free_i64(tcg_res[pass]);
9761         }
9762     } else {
9763         /* 16 -> 32 bit fp conversion */
9764         int srcelt = is_q ? 4 : 0;
9765         TCGv_i32 tcg_res[4];
9766
9767         for (pass = 0; pass < 4; pass++) {
9768             tcg_res[pass] = tcg_temp_new_i32();
9769
9770             read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
9771             gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
9772                                            cpu_env);
9773         }
9774         for (pass = 0; pass < 4; pass++) {
9775             write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
9776             tcg_temp_free_i32(tcg_res[pass]);
9777         }
9778     }
9779 }
9780
9781 static void handle_rev(DisasContext *s, int opcode, bool u,
9782                        bool is_q, int size, int rn, int rd)
9783 {
9784     int op = (opcode << 1) | u;
9785     int opsz = op + size;
9786     int grp_size = 3 - opsz;
9787     int dsize = is_q ? 128 : 64;
9788     int i;
9789
9790     if (opsz >= 3) {
9791         unallocated_encoding(s);
9792         return;
9793     }
9794
9795     if (!fp_access_check(s)) {
9796         return;
9797     }
9798
9799     if (size == 0) {
9800         /* Special case bytes, use bswap op on each group of elements */
9801         int groups = dsize / (8 << grp_size);
9802
9803         for (i = 0; i < groups; i++) {
9804             TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9805
9806             read_vec_element(s, tcg_tmp, rn, i, grp_size);
9807             switch (grp_size) {
9808             case MO_16:
9809                 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
9810                 break;
9811             case MO_32:
9812                 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
9813                 break;
9814             case MO_64:
9815                 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
9816                 break;
9817             default:
9818                 g_assert_not_reached();
9819             }
9820             write_vec_element(s, tcg_tmp, rd, i, grp_size);
9821             tcg_temp_free_i64(tcg_tmp);
9822         }
9823         if (!is_q) {
9824             clear_vec_high(s, rd);
9825         }
9826     } else {
9827         int revmask = (1 << grp_size) - 1;
9828         int esize = 8 << size;
9829         int elements = dsize / esize;
9830         TCGv_i64 tcg_rn = tcg_temp_new_i64();
9831         TCGv_i64 tcg_rd = tcg_const_i64(0);
9832         TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
9833
9834         for (i = 0; i < elements; i++) {
9835             int e_rev = (i & 0xf) ^ revmask;
9836             int off = e_rev * esize;
9837             read_vec_element(s, tcg_rn, rn, i, size);
9838             if (off >= 64) {
9839                 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
9840                                     tcg_rn, off - 64, esize);
9841             } else {
9842                 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
9843             }
9844         }
9845         write_vec_element(s, tcg_rd, rd, 0, MO_64);
9846         write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
9847
9848         tcg_temp_free_i64(tcg_rd_hi);
9849         tcg_temp_free_i64(tcg_rd);
9850         tcg_temp_free_i64(tcg_rn);
9851     }
9852 }
9853
9854 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
9855                                   bool is_q, int size, int rn, int rd)
9856 {
9857     /* Implement the pairwise operations from 2-misc:
9858      * SADDLP, UADDLP, SADALP, UADALP.
9859      * These all add pairs of elements in the input to produce a
9860      * double-width result element in the output (possibly accumulating).
9861      */
9862     bool accum = (opcode == 0x6);
9863     int maxpass = is_q ? 2 : 1;
9864     int pass;
9865     TCGv_i64 tcg_res[2];
9866
9867     if (size == 2) {
9868         /* 32 + 32 -> 64 op */
9869         TCGMemOp memop = size + (u ? 0 : MO_SIGN);
9870
9871         for (pass = 0; pass < maxpass; pass++) {
9872             TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9873             TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9874
9875             tcg_res[pass] = tcg_temp_new_i64();
9876
9877             read_vec_element(s, tcg_op1, rn, pass * 2, memop);
9878             read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
9879             tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
9880             if (accum) {
9881                 read_vec_element(s, tcg_op1, rd, pass, MO_64);
9882                 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
9883             }
9884
9885             tcg_temp_free_i64(tcg_op1);
9886             tcg_temp_free_i64(tcg_op2);
9887         }
9888     } else {
9889         for (pass = 0; pass < maxpass; pass++) {
9890             TCGv_i64 tcg_op = tcg_temp_new_i64();
9891             NeonGenOneOpFn *genfn;
9892             static NeonGenOneOpFn * const fns[2][2] = {
9893                 { gen_helper_neon_addlp_s8,  gen_helper_neon_addlp_u8 },
9894                 { gen_helper_neon_addlp_s16,  gen_helper_neon_addlp_u16 },
9895             };
9896
9897             genfn = fns[size][u];
9898
9899             tcg_res[pass] = tcg_temp_new_i64();
9900
9901             read_vec_element(s, tcg_op, rn, pass, MO_64);
9902             genfn(tcg_res[pass], tcg_op);
9903
9904             if (accum) {
9905                 read_vec_element(s, tcg_op, rd, pass, MO_64);
9906                 if (size == 0) {
9907                     gen_helper_neon_addl_u16(tcg_res[pass],
9908                                              tcg_res[pass], tcg_op);
9909                 } else {
9910                     gen_helper_neon_addl_u32(tcg_res[pass],
9911                                              tcg_res[pass], tcg_op);
9912                 }
9913             }
9914             tcg_temp_free_i64(tcg_op);
9915         }
9916     }
9917     if (!is_q) {
9918         tcg_res[1] = tcg_const_i64(0);
9919     }
9920     for (pass = 0; pass < 2; pass++) {
9921         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9922         tcg_temp_free_i64(tcg_res[pass]);
9923     }
9924 }
9925
9926 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
9927 {
9928     /* Implement SHLL and SHLL2 */
9929     int pass;
9930     int part = is_q ? 2 : 0;
9931     TCGv_i64 tcg_res[2];
9932
9933     for (pass = 0; pass < 2; pass++) {
9934         static NeonGenWidenFn * const widenfns[3] = {
9935             gen_helper_neon_widen_u8,
9936             gen_helper_neon_widen_u16,
9937             tcg_gen_extu_i32_i64,
9938         };
9939         NeonGenWidenFn *widenfn = widenfns[size];
9940         TCGv_i32 tcg_op = tcg_temp_new_i32();
9941
9942         read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
9943         tcg_res[pass] = tcg_temp_new_i64();
9944         widenfn(tcg_res[pass], tcg_op);
9945         tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
9946
9947         tcg_temp_free_i32(tcg_op);
9948     }
9949
9950     for (pass = 0; pass < 2; pass++) {
9951         write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9952         tcg_temp_free_i64(tcg_res[pass]);
9953     }
9954 }
9955
9956 /* AdvSIMD two reg misc
9957  *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
9958  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
9959  * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
9960  * +---+---+---+-----------+------+-----------+--------+-----+------+------+
9961  */
9962 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
9963 {
9964     int size = extract32(insn, 22, 2);
9965     int opcode = extract32(insn, 12, 5);
9966     bool u = extract32(insn, 29, 1);
9967     bool is_q = extract32(insn, 30, 1);
9968     int rn = extract32(insn, 5, 5);
9969     int rd = extract32(insn, 0, 5);
9970     bool need_fpstatus = false;
9971     bool need_rmode = false;
9972     int rmode = -1;
9973     TCGv_i32 tcg_rmode;
9974     TCGv_ptr tcg_fpstatus;
9975
9976     switch (opcode) {
9977     case 0x0: /* REV64, REV32 */
9978     case 0x1: /* REV16 */
9979         handle_rev(s, opcode, u, is_q, size, rn, rd);
9980         return;
9981     case 0x5: /* CNT, NOT, RBIT */
9982         if (u && size == 0) {
9983             /* NOT: adjust size so we can use the 64-bits-at-a-time loop. */
9984             size = 3;
9985             break;
9986         } else if (u && size == 1) {
9987             /* RBIT */
9988             break;
9989         } else if (!u && size == 0) {
9990             /* CNT */
9991             break;
9992         }
9993         unallocated_encoding(s);
9994         return;
9995     case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
9996     case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
9997         if (size == 3) {
9998             unallocated_encoding(s);
9999             return;
10000         }
10001         if (!fp_access_check(s)) {
10002             return;
10003         }
10004
10005         handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
10006         return;
10007     case 0x4: /* CLS, CLZ */
10008         if (size == 3) {
10009             unallocated_encoding(s);
10010             return;
10011         }
10012         break;
10013     case 0x2: /* SADDLP, UADDLP */
10014     case 0x6: /* SADALP, UADALP */
10015         if (size == 3) {
10016             unallocated_encoding(s);
10017             return;
10018         }
10019         if (!fp_access_check(s)) {
10020             return;
10021         }
10022         handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
10023         return;
10024     case 0x13: /* SHLL, SHLL2 */
10025         if (u == 0 || size == 3) {
10026             unallocated_encoding(s);
10027             return;
10028         }
10029         if (!fp_access_check(s)) {
10030             return;
10031         }
10032         handle_shll(s, is_q, size, rn, rd);
10033         return;
10034     case 0xa: /* CMLT */
10035         if (u == 1) {
10036             unallocated_encoding(s);
10037             return;
10038         }
10039         /* fall through */
10040     case 0x8: /* CMGT, CMGE */
10041     case 0x9: /* CMEQ, CMLE */
10042     case 0xb: /* ABS, NEG */
10043         if (size == 3 && !is_q) {
10044             unallocated_encoding(s);
10045             return;
10046         }
10047         break;
10048     case 0x3: /* SUQADD, USQADD */
10049         if (size == 3 && !is_q) {
10050             unallocated_encoding(s);
10051             return;
10052         }
10053         if (!fp_access_check(s)) {
10054             return;
10055         }
10056         handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
10057         return;
10058     case 0x7: /* SQABS, SQNEG */
10059         if (size == 3 && !is_q) {
10060             unallocated_encoding(s);
10061             return;
10062         }
10063         break;
10064     case 0xc ... 0xf:
10065     case 0x16 ... 0x1d:
10066     case 0x1f:
10067     {
10068         /* Floating point: U, size[1] and opcode indicate operation;
10069          * size[0] indicates single or double precision.
10070          */
10071         int is_double = extract32(size, 0, 1);
10072         opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10073         size = is_double ? 3 : 2;
10074         switch (opcode) {
10075         case 0x2f: /* FABS */
10076         case 0x6f: /* FNEG */
10077             if (size == 3 && !is_q) {
10078                 unallocated_encoding(s);
10079                 return;
10080             }
10081             break;
10082         case 0x1d: /* SCVTF */
10083         case 0x5d: /* UCVTF */
10084         {
10085             bool is_signed = (opcode == 0x1d) ? true : false;
10086             int elements = is_double ? 2 : is_q ? 4 : 2;
10087             if (is_double && !is_q) {
10088                 unallocated_encoding(s);
10089                 return;
10090             }
10091             if (!fp_access_check(s)) {
10092                 return;
10093             }
10094             handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
10095             return;
10096         }
10097         case 0x2c: /* FCMGT (zero) */
10098         case 0x2d: /* FCMEQ (zero) */
10099         case 0x2e: /* FCMLT (zero) */
10100         case 0x6c: /* FCMGE (zero) */
10101         case 0x6d: /* FCMLE (zero) */
10102             if (size == 3 && !is_q) {
10103                 unallocated_encoding(s);
10104                 return;
10105             }
10106             handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
10107             return;
10108         case 0x7f: /* FSQRT */
10109             if (size == 3 && !is_q) {
10110                 unallocated_encoding(s);
10111                 return;
10112             }
10113             break;
10114         case 0x1a: /* FCVTNS */
10115         case 0x1b: /* FCVTMS */
10116         case 0x3a: /* FCVTPS */
10117         case 0x3b: /* FCVTZS */
10118         case 0x5a: /* FCVTNU */
10119         case 0x5b: /* FCVTMU */
10120         case 0x7a: /* FCVTPU */
10121         case 0x7b: /* FCVTZU */
10122             need_fpstatus = true;
10123             need_rmode = true;
10124             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10125             if (size == 3 && !is_q) {
10126                 unallocated_encoding(s);
10127                 return;
10128             }
10129             break;
10130         case 0x5c: /* FCVTAU */
10131         case 0x1c: /* FCVTAS */
10132             need_fpstatus = true;
10133             need_rmode = true;
10134             rmode = FPROUNDING_TIEAWAY;
10135             if (size == 3 && !is_q) {
10136                 unallocated_encoding(s);
10137                 return;
10138             }
10139             break;
10140         case 0x3c: /* URECPE */
10141             if (size == 3) {
10142                 unallocated_encoding(s);
10143                 return;
10144             }
10145             /* fall through */
10146         case 0x3d: /* FRECPE */
10147         case 0x7d: /* FRSQRTE */
10148             if (size == 3 && !is_q) {
10149                 unallocated_encoding(s);
10150                 return;
10151             }
10152             if (!fp_access_check(s)) {
10153                 return;
10154             }
10155             handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
10156             return;
10157         case 0x56: /* FCVTXN, FCVTXN2 */
10158             if (size == 2) {
10159                 unallocated_encoding(s);
10160                 return;
10161             }
10162             /* fall through */
10163         case 0x16: /* FCVTN, FCVTN2 */
10164             /* handle_2misc_narrow does a 2*size -> size operation, but these
10165              * instructions encode the source size rather than dest size.
10166              */
10167             if (!fp_access_check(s)) {
10168                 return;
10169             }
10170             handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
10171             return;
10172         case 0x17: /* FCVTL, FCVTL2 */
10173             if (!fp_access_check(s)) {
10174                 return;
10175             }
10176             handle_2misc_widening(s, opcode, is_q, size, rn, rd);
10177             return;
10178         case 0x18: /* FRINTN */
10179         case 0x19: /* FRINTM */
10180         case 0x38: /* FRINTP */
10181         case 0x39: /* FRINTZ */
10182             need_rmode = true;
10183             rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10184             /* fall through */
10185         case 0x59: /* FRINTX */
10186         case 0x79: /* FRINTI */
10187             need_fpstatus = true;
10188             if (size == 3 && !is_q) {
10189                 unallocated_encoding(s);
10190                 return;
10191             }
10192             break;
10193         case 0x58: /* FRINTA */
10194             need_rmode = true;
10195             rmode = FPROUNDING_TIEAWAY;
10196             need_fpstatus = true;
10197             if (size == 3 && !is_q) {
10198                 unallocated_encoding(s);
10199                 return;
10200             }
10201             break;
10202         case 0x7c: /* URSQRTE */
10203             if (size == 3) {
10204                 unallocated_encoding(s);
10205                 return;
10206             }
10207             need_fpstatus = true;
10208             break;
10209         default:
10210             unallocated_encoding(s);
10211             return;
10212         }
10213         break;
10214     }
10215     default:
10216         unallocated_encoding(s);
10217         return;
10218     }
10219
10220     if (!fp_access_check(s)) {
10221         return;
10222     }
10223
10224     if (need_fpstatus) {
10225         tcg_fpstatus = get_fpstatus_ptr();
10226     } else {
10227         TCGV_UNUSED_PTR(tcg_fpstatus);
10228     }
10229     if (need_rmode) {
10230         tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
10231         gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
10232     } else {
10233         TCGV_UNUSED_I32(tcg_rmode);
10234     }
10235
10236     if (size == 3) {
10237         /* All 64-bit element operations can be shared with scalar 2misc */
10238         int pass;
10239
10240         for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
10241             TCGv_i64 tcg_op = tcg_temp_new_i64();
10242             TCGv_i64 tcg_res = tcg_temp_new_i64();
10243
10244             read_vec_element(s, tcg_op, rn, pass, MO_64);
10245
10246             handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
10247                             tcg_rmode, tcg_fpstatus);
10248
10249             write_vec_element(s, tcg_res, rd, pass, MO_64);
10250
10251             tcg_temp_free_i64(tcg_res);
10252             tcg_temp_free_i64(tcg_op);
10253         }
10254     } else {
10255         int pass;
10256
10257         for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
10258             TCGv_i32 tcg_op = tcg_temp_new_i32();
10259             TCGv_i32 tcg_res = tcg_temp_new_i32();
10260             TCGCond cond;
10261
10262             read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
10263
10264             if (size == 2) {
10265                 /* Special cases for 32 bit elements */
10266                 switch (opcode) {
10267                 case 0xa: /* CMLT */
10268                     /* 32 bit integer comparison against zero, result is
10269                      * test ? (2^32 - 1) : 0. We implement via setcond(test)
10270                      * and inverting.
10271                      */
10272                     cond = TCG_COND_LT;
10273                 do_cmop:
10274                     tcg_gen_setcondi_i32(cond, tcg_res, tcg_op, 0);
10275                     tcg_gen_neg_i32(tcg_res, tcg_res);
10276                     break;
10277                 case 0x8: /* CMGT, CMGE */
10278                     cond = u ? TCG_COND_GE : TCG_COND_GT;
10279                     goto do_cmop;
10280                 case 0x9: /* CMEQ, CMLE */
10281                     cond = u ? TCG_COND_LE : TCG_COND_EQ;
10282                     goto do_cmop;
10283                 case 0x4: /* CLS */
10284                     if (u) {
10285                         tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
10286                     } else {
10287                         tcg_gen_clrsb_i32(tcg_res, tcg_op);
10288                     }
10289                     break;
10290                 case 0x7: /* SQABS, SQNEG */
10291                     if (u) {
10292                         gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
10293                     } else {
10294                         gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
10295                     }
10296                     break;
10297                 case 0xb: /* ABS, NEG */
10298                     if (u) {
10299                         tcg_gen_neg_i32(tcg_res, tcg_op);
10300                     } else {
10301                         TCGv_i32 tcg_zero = tcg_const_i32(0);
10302                         tcg_gen_neg_i32(tcg_res, tcg_op);
10303                         tcg_gen_movcond_i32(TCG_COND_GT, tcg_res, tcg_op,
10304                                             tcg_zero, tcg_op, tcg_res);
10305                         tcg_temp_free_i32(tcg_zero);
10306                     }
10307                     break;
10308                 case 0x2f: /* FABS */
10309                     gen_helper_vfp_abss(tcg_res, tcg_op);
10310                     break;
10311                 case 0x6f: /* FNEG */
10312                     gen_helper_vfp_negs(tcg_res, tcg_op);
10313                     break;
10314                 case 0x7f: /* FSQRT */
10315                     gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
10316                     break;
10317                 case 0x1a: /* FCVTNS */
10318                 case 0x1b: /* FCVTMS */
10319                 case 0x1c: /* FCVTAS */
10320                 case 0x3a: /* FCVTPS */
10321                 case 0x3b: /* FCVTZS */
10322                 {
10323                     TCGv_i32 tcg_shift = tcg_const_i32(0);
10324                     gen_helper_vfp_tosls(tcg_res, tcg_op,
10325                                          tcg_shift, tcg_fpstatus);
10326                     tcg_temp_free_i32(tcg_shift);
10327                     break;
10328                 }
10329                 case 0x5a: /* FCVTNU */
10330                 case 0x5b: /* FCVTMU */
10331                 case 0x5c: /* FCVTAU */
10332                 case 0x7a: /* FCVTPU */
10333                 case 0x7b: /* FCVTZU */
10334                 {
10335                     TCGv_i32 tcg_shift = tcg_const_i32(0);
10336                     gen_helper_vfp_touls(tcg_res, tcg_op,
10337                                          tcg_shift, tcg_fpstatus);
10338                     tcg_temp_free_i32(tcg_shift);
10339                     break;
10340                 }
10341                 case 0x18: /* FRINTN */
10342                 case 0x19: /* FRINTM */
10343                 case 0x38: /* FRINTP */
10344                 case 0x39: /* FRINTZ */
10345                 case 0x58: /* FRINTA */
10346                 case 0x79: /* FRINTI */
10347                     gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
10348                     break;
10349                 case 0x59: /* FRINTX */
10350                     gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
10351                     break;
10352                 case 0x7c: /* URSQRTE */
10353                     gen_helper_rsqrte_u32(tcg_res, tcg_op, tcg_fpstatus);
10354                     break;
10355                 default:
10356                     g_assert_not_reached();
10357                 }
10358             } else {
10359                 /* Use helpers for 8 and 16 bit elements */
10360                 switch (opcode) {
10361                 case 0x5: /* CNT, RBIT */
10362                     /* For these two insns size is part of the opcode specifier
10363                      * (handled earlier); they always operate on byte elements.
10364                      */
10365                     if (u) {
10366                         gen_helper_neon_rbit_u8(tcg_res, tcg_op);
10367                     } else {
10368                         gen_helper_neon_cnt_u8(tcg_res, tcg_op);
10369                     }
10370                     break;
10371                 case 0x7: /* SQABS, SQNEG */
10372                 {
10373                     NeonGenOneOpEnvFn *genfn;
10374                     static NeonGenOneOpEnvFn * const fns[2][2] = {
10375                         { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10376                         { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10377                     };
10378                     genfn = fns[size][u];
10379                     genfn(tcg_res, cpu_env, tcg_op);
10380                     break;
10381                 }
10382                 case 0x8: /* CMGT, CMGE */
10383                 case 0x9: /* CMEQ, CMLE */
10384                 case 0xa: /* CMLT */
10385                 {
10386                     static NeonGenTwoOpFn * const fns[3][2] = {
10387                         { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_s16 },
10388                         { gen_helper_neon_cge_s8, gen_helper_neon_cge_s16 },
10389                         { gen_helper_neon_ceq_u8, gen_helper_neon_ceq_u16 },
10390                     };
10391                     NeonGenTwoOpFn *genfn;
10392                     int comp;
10393                     bool reverse;
10394                     TCGv_i32 tcg_zero = tcg_const_i32(0);
10395
10396                     /* comp = index into [CMGT, CMGE, CMEQ, CMLE, CMLT] */
10397                     comp = (opcode - 0x8) * 2 + u;
10398                     /* ...but LE, LT are implemented as reverse GE, GT */
10399                     reverse = (comp > 2);
10400                     if (reverse) {
10401                         comp = 4 - comp;
10402                     }
10403                     genfn = fns[comp][size];
10404                     if (reverse) {
10405                         genfn(tcg_res, tcg_zero, tcg_op);
10406                     } else {
10407                         genfn(tcg_res, tcg_op, tcg_zero);
10408                     }
10409                     tcg_temp_free_i32(tcg_zero);
10410                     break;
10411                 }
10412                 case 0xb: /* ABS, NEG */
10413                     if (u) {
10414                         TCGv_i32 tcg_zero = tcg_const_i32(0);
10415                         if (size) {
10416                             gen_helper_neon_sub_u16(tcg_res, tcg_zero, tcg_op);
10417                         } else {
10418                             gen_helper_neon_sub_u8(tcg_res, tcg_zero, tcg_op);
10419                         }
10420                         tcg_temp_free_i32(tcg_zero);
10421                     } else {
10422                         if (size) {
10423                             gen_helper_neon_abs_s16(tcg_res, tcg_op);
10424                         } else {
10425                             gen_helper_neon_abs_s8(tcg_res, tcg_op);
10426                         }
10427                     }
10428                     break;
10429                 case 0x4: /* CLS, CLZ */
10430                     if (u) {
10431                         if (size == 0) {
10432                             gen_helper_neon_clz_u8(tcg_res, tcg_op);
10433                         } else {
10434                             gen_helper_neon_clz_u16(tcg_res, tcg_op);
10435                         }
10436                     } else {
10437                         if (size == 0) {
10438                             gen_helper_neon_cls_s8(tcg_res, tcg_op);
10439                         } else {
10440                             gen_helper_neon_cls_s16(tcg_res, tcg_op);
10441                         }
10442                     }
10443                     break;
10444                 default:
10445                     g_assert_not_reached();
10446                 }
10447             }
10448
10449             write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10450
10451             tcg_temp_free_i32(tcg_res);
10452             tcg_temp_free_i32(tcg_op);
10453         }
10454     }
10455     if (!is_q) {
10456         clear_vec_high(s, rd);
10457     }
10458
10459     if (need_rmode) {
10460         gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
10461         tcg_temp_free_i32(tcg_rmode);
10462     }
10463     if (need_fpstatus) {
10464         tcg_temp_free_ptr(tcg_fpstatus);
10465     }
10466 }
10467
10468 /* AdvSIMD scalar x indexed element
10469  *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
10470  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
10471  * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
10472  * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
10473  * AdvSIMD vector x indexed element
10474  *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
10475  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
10476  * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
10477  * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
10478  */
10479 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
10480 {
10481     /* This encoding has two kinds of instruction:
10482      *  normal, where we perform elt x idxelt => elt for each
10483      *     element in the vector
10484      *  long, where we perform elt x idxelt and generate a result of
10485      *     double the width of the input element
10486      * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
10487      */
10488     bool is_scalar = extract32(insn, 28, 1);
10489     bool is_q = extract32(insn, 30, 1);
10490     bool u = extract32(insn, 29, 1);
10491     int size = extract32(insn, 22, 2);
10492     int l = extract32(insn, 21, 1);
10493     int m = extract32(insn, 20, 1);
10494     /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
10495     int rm = extract32(insn, 16, 4);
10496     int opcode = extract32(insn, 12, 4);
10497     int h = extract32(insn, 11, 1);
10498     int rn = extract32(insn, 5, 5);
10499     int rd = extract32(insn, 0, 5);
10500     bool is_long = false;
10501     bool is_fp = false;
10502     int index;
10503     TCGv_ptr fpst;
10504
10505     switch (opcode) {
10506     case 0x0: /* MLA */
10507     case 0x4: /* MLS */
10508         if (!u || is_scalar) {
10509             unallocated_encoding(s);
10510             return;
10511         }
10512         break;
10513     case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10514     case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10515     case 0xa: /* SMULL, SMULL2, UMULL, UMULL2 */
10516         if (is_scalar) {
10517             unallocated_encoding(s);
10518             return;
10519         }
10520         is_long = true;
10521         break;
10522     case 0x3: /* SQDMLAL, SQDMLAL2 */
10523     case 0x7: /* SQDMLSL, SQDMLSL2 */
10524     case 0xb: /* SQDMULL, SQDMULL2 */
10525         is_long = true;
10526         /* fall through */
10527     case 0xc: /* SQDMULH */
10528     case 0xd: /* SQRDMULH */
10529         if (u) {
10530             unallocated_encoding(s);
10531             return;
10532         }
10533         break;
10534     case 0x8: /* MUL */
10535         if (u || is_scalar) {
10536             unallocated_encoding(s);
10537             return;
10538         }
10539         break;
10540     case 0x1: /* FMLA */
10541     case 0x5: /* FMLS */
10542         if (u) {
10543             unallocated_encoding(s);
10544             return;
10545         }
10546         /* fall through */
10547     case 0x9: /* FMUL, FMULX */
10548         if (!extract32(size, 1, 1)) {
10549             unallocated_encoding(s);
10550             return;
10551         }
10552         is_fp = true;
10553         break;
10554     default:
10555         unallocated_encoding(s);
10556         return;
10557     }
10558
10559     if (is_fp) {
10560         /* low bit of size indicates single/double */
10561         size = extract32(size, 0, 1) ? 3 : 2;
10562         if (size == 2) {
10563             index = h << 1 | l;
10564         } else {
10565             if (l || !is_q) {
10566                 unallocated_encoding(s);
10567                 return;
10568             }
10569             index = h;
10570         }
10571         rm |= (m << 4);
10572     } else {
10573         switch (size) {
10574         case 1:
10575             index = h << 2 | l << 1 | m;
10576             break;
10577         case 2:
10578             index = h << 1 | l;
10579             rm |= (m << 4);
10580             break;
10581         default:
10582             unallocated_encoding(s);
10583             return;
10584         }
10585     }
10586
10587     if (!fp_access_check(s)) {
10588         return;
10589     }
10590
10591     if (is_fp) {
10592         fpst = get_fpstatus_ptr();
10593     } else {
10594         TCGV_UNUSED_PTR(fpst);
10595     }
10596
10597     if (size == 3) {
10598         TCGv_i64 tcg_idx = tcg_temp_new_i64();
10599         int pass;
10600
10601         assert(is_fp && is_q && !is_long);
10602
10603         read_vec_element(s, tcg_idx, rm, index, MO_64);
10604
10605         for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10606             TCGv_i64 tcg_op = tcg_temp_new_i64();
10607             TCGv_i64 tcg_res = tcg_temp_new_i64();
10608
10609             read_vec_element(s, tcg_op, rn, pass, MO_64);
10610
10611             switch (opcode) {
10612             case 0x5: /* FMLS */
10613                 /* As usual for ARM, separate negation for fused multiply-add */
10614                 gen_helper_vfp_negd(tcg_op, tcg_op);
10615                 /* fall through */
10616             case 0x1: /* FMLA */
10617                 read_vec_element(s, tcg_res, rd, pass, MO_64);
10618                 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
10619                 break;
10620             case 0x9: /* FMUL, FMULX */
10621                 if (u) {
10622                     gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
10623                 } else {
10624                     gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
10625                 }
10626                 break;
10627             default:
10628                 g_assert_not_reached();
10629             }
10630
10631             write_vec_element(s, tcg_res, rd, pass, MO_64);
10632             tcg_temp_free_i64(tcg_op);
10633             tcg_temp_free_i64(tcg_res);
10634         }
10635
10636         if (is_scalar) {
10637             clear_vec_high(s, rd);
10638         }
10639
10640         tcg_temp_free_i64(tcg_idx);
10641     } else if (!is_long) {
10642         /* 32 bit floating point, or 16 or 32 bit integer.
10643          * For the 16 bit scalar case we use the usual Neon helpers and
10644          * rely on the fact that 0 op 0 == 0 with no side effects.
10645          */
10646         TCGv_i32 tcg_idx = tcg_temp_new_i32();
10647         int pass, maxpasses;
10648
10649         if (is_scalar) {
10650             maxpasses = 1;
10651         } else {
10652             maxpasses = is_q ? 4 : 2;
10653         }
10654
10655         read_vec_element_i32(s, tcg_idx, rm, index, size);
10656
10657         if (size == 1 && !is_scalar) {
10658             /* The simplest way to handle the 16x16 indexed ops is to duplicate
10659              * the index into both halves of the 32 bit tcg_idx and then use
10660              * the usual Neon helpers.
10661              */
10662             tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
10663         }
10664
10665         for (pass = 0; pass < maxpasses; pass++) {
10666             TCGv_i32 tcg_op = tcg_temp_new_i32();
10667             TCGv_i32 tcg_res = tcg_temp_new_i32();
10668
10669             read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
10670
10671             switch (opcode) {
10672             case 0x0: /* MLA */
10673             case 0x4: /* MLS */
10674             case 0x8: /* MUL */
10675             {
10676                 static NeonGenTwoOpFn * const fns[2][2] = {
10677                     { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
10678                     { tcg_gen_add_i32, tcg_gen_sub_i32 },
10679                 };
10680                 NeonGenTwoOpFn *genfn;
10681                 bool is_sub = opcode == 0x4;
10682
10683                 if (size == 1) {
10684                     gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
10685                 } else {
10686                     tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
10687                 }
10688                 if (opcode == 0x8) {
10689                     break;
10690                 }
10691                 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
10692                 genfn = fns[size - 1][is_sub];
10693                 genfn(tcg_res, tcg_op, tcg_res);
10694                 break;
10695             }
10696             case 0x5: /* FMLS */
10697                 /* As usual for ARM, separate negation for fused multiply-add */
10698                 gen_helper_vfp_negs(tcg_op, tcg_op);
10699                 /* fall through */
10700             case 0x1: /* FMLA */
10701                 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10702                 gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
10703                 break;
10704             case 0x9: /* FMUL, FMULX */
10705                 if (u) {
10706                     gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
10707                 } else {
10708                     gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
10709                 }
10710                 break;
10711             case 0xc: /* SQDMULH */
10712                 if (size == 1) {
10713                     gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
10714                                                tcg_op, tcg_idx);
10715                 } else {
10716                     gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
10717                                                tcg_op, tcg_idx);
10718                 }
10719                 break;
10720             case 0xd: /* SQRDMULH */
10721                 if (size == 1) {
10722                     gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
10723                                                 tcg_op, tcg_idx);
10724                 } else {
10725                     gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
10726                                                 tcg_op, tcg_idx);
10727                 }
10728                 break;
10729             default:
10730                 g_assert_not_reached();
10731             }
10732
10733             if (is_scalar) {
10734                 write_fp_sreg(s, rd, tcg_res);
10735             } else {
10736                 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10737             }
10738
10739             tcg_temp_free_i32(tcg_op);
10740             tcg_temp_free_i32(tcg_res);
10741         }
10742
10743         tcg_temp_free_i32(tcg_idx);
10744
10745         if (!is_q) {
10746             clear_vec_high(s, rd);
10747         }
10748     } else {
10749         /* long ops: 16x16->32 or 32x32->64 */
10750         TCGv_i64 tcg_res[2];
10751         int pass;
10752         bool satop = extract32(opcode, 0, 1);
10753         TCGMemOp memop = MO_32;
10754
10755         if (satop || !u) {
10756             memop |= MO_SIGN;
10757         }
10758
10759         if (size == 2) {
10760             TCGv_i64 tcg_idx = tcg_temp_new_i64();
10761
10762             read_vec_element(s, tcg_idx, rm, index, memop);
10763
10764             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10765                 TCGv_i64 tcg_op = tcg_temp_new_i64();
10766                 TCGv_i64 tcg_passres;
10767                 int passelt;
10768
10769                 if (is_scalar) {
10770                     passelt = 0;
10771                 } else {
10772                     passelt = pass + (is_q * 2);
10773                 }
10774
10775                 read_vec_element(s, tcg_op, rn, passelt, memop);
10776
10777                 tcg_res[pass] = tcg_temp_new_i64();
10778
10779                 if (opcode == 0xa || opcode == 0xb) {
10780                     /* Non-accumulating ops */
10781                     tcg_passres = tcg_res[pass];
10782                 } else {
10783                     tcg_passres = tcg_temp_new_i64();
10784                 }
10785
10786                 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
10787                 tcg_temp_free_i64(tcg_op);
10788
10789                 if (satop) {
10790                     /* saturating, doubling */
10791                     gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10792                                                       tcg_passres, tcg_passres);
10793                 }
10794
10795                 if (opcode == 0xa || opcode == 0xb) {
10796                     continue;
10797                 }
10798
10799                 /* Accumulating op: handle accumulate step */
10800                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10801
10802                 switch (opcode) {
10803                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10804                     tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10805                     break;
10806                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10807                     tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10808                     break;
10809                 case 0x7: /* SQDMLSL, SQDMLSL2 */
10810                     tcg_gen_neg_i64(tcg_passres, tcg_passres);
10811                     /* fall through */
10812                 case 0x3: /* SQDMLAL, SQDMLAL2 */
10813                     gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10814                                                       tcg_res[pass],
10815                                                       tcg_passres);
10816                     break;
10817                 default:
10818                     g_assert_not_reached();
10819                 }
10820                 tcg_temp_free_i64(tcg_passres);
10821             }
10822             tcg_temp_free_i64(tcg_idx);
10823
10824             if (is_scalar) {
10825                 clear_vec_high(s, rd);
10826             }
10827         } else {
10828             TCGv_i32 tcg_idx = tcg_temp_new_i32();
10829
10830             assert(size == 1);
10831             read_vec_element_i32(s, tcg_idx, rm, index, size);
10832
10833             if (!is_scalar) {
10834                 /* The simplest way to handle the 16x16 indexed ops is to
10835                  * duplicate the index into both halves of the 32 bit tcg_idx
10836                  * and then use the usual Neon helpers.
10837                  */
10838                 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
10839             }
10840
10841             for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10842                 TCGv_i32 tcg_op = tcg_temp_new_i32();
10843                 TCGv_i64 tcg_passres;
10844
10845                 if (is_scalar) {
10846                     read_vec_element_i32(s, tcg_op, rn, pass, size);
10847                 } else {
10848                     read_vec_element_i32(s, tcg_op, rn,
10849                                          pass + (is_q * 2), MO_32);
10850                 }
10851
10852                 tcg_res[pass] = tcg_temp_new_i64();
10853
10854                 if (opcode == 0xa || opcode == 0xb) {
10855                     /* Non-accumulating ops */
10856                     tcg_passres = tcg_res[pass];
10857                 } else {
10858                     tcg_passres = tcg_temp_new_i64();
10859                 }
10860
10861                 if (memop & MO_SIGN) {
10862                     gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
10863                 } else {
10864                     gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
10865                 }
10866                 if (satop) {
10867                     gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10868                                                       tcg_passres, tcg_passres);
10869                 }
10870                 tcg_temp_free_i32(tcg_op);
10871
10872                 if (opcode == 0xa || opcode == 0xb) {
10873                     continue;
10874                 }
10875
10876                 /* Accumulating op: handle accumulate step */
10877                 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10878
10879                 switch (opcode) {
10880                 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10881                     gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
10882                                              tcg_passres);
10883                     break;
10884                 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10885                     gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
10886                                              tcg_passres);
10887                     break;
10888                 case 0x7: /* SQDMLSL, SQDMLSL2 */
10889                     gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10890                     /* fall through */
10891                 case 0x3: /* SQDMLAL, SQDMLAL2 */
10892                     gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10893                                                       tcg_res[pass],
10894                                                       tcg_passres);
10895                     break;
10896                 default:
10897                     g_assert_not_reached();
10898                 }
10899                 tcg_temp_free_i64(tcg_passres);
10900             }
10901             tcg_temp_free_i32(tcg_idx);
10902
10903             if (is_scalar) {
10904                 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
10905             }
10906         }
10907
10908         if (is_scalar) {
10909             tcg_res[1] = tcg_const_i64(0);
10910         }
10911
10912         for (pass = 0; pass < 2; pass++) {
10913             write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10914             tcg_temp_free_i64(tcg_res[pass]);
10915         }
10916     }
10917
10918     if (!TCGV_IS_UNUSED_PTR(fpst)) {
10919         tcg_temp_free_ptr(fpst);
10920     }
10921 }
10922
10923 /* Crypto AES
10924  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
10925  * +-----------------+------+-----------+--------+-----+------+------+
10926  * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
10927  * +-----------------+------+-----------+--------+-----+------+------+
10928  */
10929 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
10930 {
10931     int size = extract32(insn, 22, 2);
10932     int opcode = extract32(insn, 12, 5);
10933     int rn = extract32(insn, 5, 5);
10934     int rd = extract32(insn, 0, 5);
10935     int decrypt;
10936     TCGv_i32 tcg_rd_regno, tcg_rn_regno, tcg_decrypt;
10937     CryptoThreeOpEnvFn *genfn;
10938
10939     if (!arm_dc_feature(s, ARM_FEATURE_V8_AES)
10940         || size != 0) {
10941         unallocated_encoding(s);
10942         return;
10943     }
10944
10945     switch (opcode) {
10946     case 0x4: /* AESE */
10947         decrypt = 0;
10948         genfn = gen_helper_crypto_aese;
10949         break;
10950     case 0x6: /* AESMC */
10951         decrypt = 0;
10952         genfn = gen_helper_crypto_aesmc;
10953         break;
10954     case 0x5: /* AESD */
10955         decrypt = 1;
10956         genfn = gen_helper_crypto_aese;
10957         break;
10958     case 0x7: /* AESIMC */
10959         decrypt = 1;
10960         genfn = gen_helper_crypto_aesmc;
10961         break;
10962     default:
10963         unallocated_encoding(s);
10964         return;
10965     }
10966
10967     if (!fp_access_check(s)) {
10968         return;
10969     }
10970
10971     /* Note that we convert the Vx register indexes into the
10972      * index within the vfp.regs[] array, so we can share the
10973      * helper with the AArch32 instructions.
10974      */
10975     tcg_rd_regno = tcg_const_i32(rd << 1);
10976     tcg_rn_regno = tcg_const_i32(rn << 1);
10977     tcg_decrypt = tcg_const_i32(decrypt);
10978
10979     genfn(cpu_env, tcg_rd_regno, tcg_rn_regno, tcg_decrypt);
10980
10981     tcg_temp_free_i32(tcg_rd_regno);
10982     tcg_temp_free_i32(tcg_rn_regno);
10983     tcg_temp_free_i32(tcg_decrypt);
10984 }
10985
10986 /* Crypto three-reg SHA
10987  *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
10988  * +-----------------+------+---+------+---+--------+-----+------+------+
10989  * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
10990  * +-----------------+------+---+------+---+--------+-----+------+------+
10991  */
10992 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
10993 {
10994     int size = extract32(insn, 22, 2);
10995     int opcode = extract32(insn, 12, 3);
10996     int rm = extract32(insn, 16, 5);
10997     int rn = extract32(insn, 5, 5);
10998     int rd = extract32(insn, 0, 5);
10999     CryptoThreeOpEnvFn *genfn;
11000     TCGv_i32 tcg_rd_regno, tcg_rn_regno, tcg_rm_regno;
11001     int feature = ARM_FEATURE_V8_SHA256;
11002
11003     if (size != 0) {
11004         unallocated_encoding(s);
11005         return;
11006     }
11007
11008     switch (opcode) {
11009     case 0: /* SHA1C */
11010     case 1: /* SHA1P */
11011     case 2: /* SHA1M */
11012     case 3: /* SHA1SU0 */
11013         genfn = NULL;
11014         feature = ARM_FEATURE_V8_SHA1;
11015         break;
11016     case 4: /* SHA256H */
11017         genfn = gen_helper_crypto_sha256h;
11018         break;
11019     case 5: /* SHA256H2 */
11020         genfn = gen_helper_crypto_sha256h2;
11021         break;
11022     case 6: /* SHA256SU1 */
11023         genfn = gen_helper_crypto_sha256su1;
11024         break;
11025     default:
11026         unallocated_encoding(s);
11027         return;
11028     }
11029
11030     if (!arm_dc_feature(s, feature)) {
11031         unallocated_encoding(s);
11032         return;
11033     }
11034
11035     if (!fp_access_check(s)) {
11036         return;
11037     }
11038
11039     tcg_rd_regno = tcg_const_i32(rd << 1);
11040     tcg_rn_regno = tcg_const_i32(rn << 1);
11041     tcg_rm_regno = tcg_const_i32(rm << 1);
11042
11043     if (genfn) {
11044         genfn(cpu_env, tcg_rd_regno, tcg_rn_regno, tcg_rm_regno);
11045     } else {
11046         TCGv_i32 tcg_opcode = tcg_const_i32(opcode);
11047
11048         gen_helper_crypto_sha1_3reg(cpu_env, tcg_rd_regno,
11049                                     tcg_rn_regno, tcg_rm_regno, tcg_opcode);
11050         tcg_temp_free_i32(tcg_opcode);
11051     }
11052
11053     tcg_temp_free_i32(tcg_rd_regno);
11054     tcg_temp_free_i32(tcg_rn_regno);
11055     tcg_temp_free_i32(tcg_rm_regno);
11056 }
11057
11058 /* Crypto two-reg SHA
11059  *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
11060  * +-----------------+------+-----------+--------+-----+------+------+
11061  * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
11062  * +-----------------+------+-----------+--------+-----+------+------+
11063  */
11064 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
11065 {
11066     int size = extract32(insn, 22, 2);
11067     int opcode = extract32(insn, 12, 5);
11068     int rn = extract32(insn, 5, 5);
11069     int rd = extract32(insn, 0, 5);
11070     CryptoTwoOpEnvFn *genfn;
11071     int feature;
11072     TCGv_i32 tcg_rd_regno, tcg_rn_regno;
11073
11074     if (size != 0) {
11075         unallocated_encoding(s);
11076         return;
11077     }
11078
11079     switch (opcode) {
11080     case 0: /* SHA1H */
11081         feature = ARM_FEATURE_V8_SHA1;
11082         genfn = gen_helper_crypto_sha1h;
11083         break;
11084     case 1: /* SHA1SU1 */
11085         feature = ARM_FEATURE_V8_SHA1;
11086         genfn = gen_helper_crypto_sha1su1;
11087         break;
11088     case 2: /* SHA256SU0 */
11089         feature = ARM_FEATURE_V8_SHA256;
11090         genfn = gen_helper_crypto_sha256su0;
11091         break;
11092     default:
11093         unallocated_encoding(s);
11094         return;
11095     }
11096
11097     if (!arm_dc_feature(s, feature)) {
11098         unallocated_encoding(s);
11099         return;
11100     }
11101
11102     if (!fp_access_check(s)) {
11103         return;
11104     }
11105
11106     tcg_rd_regno = tcg_const_i32(rd << 1);
11107     tcg_rn_regno = tcg_const_i32(rn << 1);
11108
11109     genfn(cpu_env, tcg_rd_regno, tcg_rn_regno);
11110
11111     tcg_temp_free_i32(tcg_rd_regno);
11112     tcg_temp_free_i32(tcg_rn_regno);
11113 }
11114
11115 /* C3.6 Data processing - SIMD, inc Crypto
11116  *
11117  * As the decode gets a little complex we are using a table based
11118  * approach for this part of the decode.
11119  */
11120 static const AArch64DecodeTable data_proc_simd[] = {
11121     /* pattern  ,  mask     ,  fn                        */
11122     { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
11123     { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
11124     { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
11125     { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
11126     { 0x0e000400, 0x9fe08400, disas_simd_copy },
11127     { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
11128     /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
11129     { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
11130     { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
11131     { 0x0e000000, 0xbf208c00, disas_simd_tb },
11132     { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
11133     { 0x2e000000, 0xbf208400, disas_simd_ext },
11134     { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
11135     { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
11136     { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
11137     { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
11138     { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
11139     { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
11140     { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
11141     { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
11142     { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
11143     { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
11144     { 0x00000000, 0x00000000, NULL }
11145 };
11146
11147 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
11148 {
11149     /* Note that this is called with all non-FP cases from
11150      * table C3-6 so it must UNDEF for entries not specifically
11151      * allocated to instructions in that table.
11152      */
11153     AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
11154     if (fn) {
11155         fn(s, insn);
11156     } else {
11157         unallocated_encoding(s);
11158     }
11159 }
11160
11161 /* C3.6 Data processing - SIMD and floating point */
11162 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
11163 {
11164     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
11165         disas_data_proc_fp(s, insn);
11166     } else {
11167         /* SIMD, including crypto */
11168         disas_data_proc_simd(s, insn);
11169     }
11170 }
11171
11172 /* C3.1 A64 instruction index by encoding */
11173 static void disas_a64_insn(CPUARMState *env, DisasContext *s)
11174 {
11175     uint32_t insn;
11176
11177     insn = arm_ldl_code(env, s->pc, s->sctlr_b);
11178     s->insn = insn;
11179     s->pc += 4;
11180
11181     s->fp_access_checked = false;
11182
11183     switch (extract32(insn, 25, 4)) {
11184     case 0x0: case 0x1: case 0x2: case 0x3: /* UNALLOCATED */
11185         unallocated_encoding(s);
11186         break;
11187     case 0x8: case 0x9: /* Data processing - immediate */
11188         disas_data_proc_imm(s, insn);
11189         break;
11190     case 0xa: case 0xb: /* Branch, exception generation and system insns */
11191         disas_b_exc_sys(s, insn);
11192         break;
11193     case 0x4:
11194     case 0x6:
11195     case 0xc:
11196     case 0xe:      /* Loads and stores */
11197         disas_ldst(s, insn);
11198         break;
11199     case 0x5:
11200     case 0xd:      /* Data processing - register */
11201         disas_data_proc_reg(s, insn);
11202         break;
11203     case 0x7:
11204     case 0xf:      /* Data processing - SIMD and floating point */
11205         disas_data_proc_simd_fp(s, insn);
11206         break;
11207     default:
11208         assert(FALSE); /* all 15 cases should be handled above */
11209         break;
11210     }
11211
11212     /* if we allocated any temporaries, free them here */
11213     free_tmp_a64(s);
11214 }
11215
11216 static int aarch64_tr_init_disas_context(DisasContextBase *dcbase,
11217                                          CPUState *cpu, int max_insns)
11218 {
11219     DisasContext *dc = container_of(dcbase, DisasContext, base);
11220     CPUARMState *env = cpu->env_ptr;
11221     ARMCPU *arm_cpu = arm_env_get_cpu(env);
11222     int bound;
11223
11224     dc->pc = dc->base.pc_first;
11225     dc->condjmp = 0;
11226
11227     dc->aarch64 = 1;
11228     /* If we are coming from secure EL0 in a system with a 32-bit EL3, then
11229      * there is no secure EL1, so we route exceptions to EL3.
11230      */
11231     dc->secure_routed_to_el3 = arm_feature(env, ARM_FEATURE_EL3) &&
11232                                !arm_el_is_aa64(env, 3);
11233     dc->thumb = 0;
11234     dc->sctlr_b = 0;
11235     dc->be_data = ARM_TBFLAG_BE_DATA(dc->base.tb->flags) ? MO_BE : MO_LE;
11236     dc->condexec_mask = 0;
11237     dc->condexec_cond = 0;
11238     dc->mmu_idx = core_to_arm_mmu_idx(env, ARM_TBFLAG_MMUIDX(dc->base.tb->flags));
11239     dc->tbi0 = ARM_TBFLAG_TBI0(dc->base.tb->flags);
11240     dc->tbi1 = ARM_TBFLAG_TBI1(dc->base.tb->flags);
11241     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
11242 #if !defined(CONFIG_USER_ONLY)
11243     dc->user = (dc->current_el == 0);
11244 #endif
11245     dc->fp_excp_el = ARM_TBFLAG_FPEXC_EL(dc->base.tb->flags);
11246     dc->vec_len = 0;
11247     dc->vec_stride = 0;
11248     dc->cp_regs = arm_cpu->cp_regs;
11249     dc->features = env->features;
11250
11251     /* Single step state. The code-generation logic here is:
11252      *  SS_ACTIVE == 0:
11253      *   generate code with no special handling for single-stepping (except
11254      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
11255      *   this happens anyway because those changes are all system register or
11256      *   PSTATE writes).
11257      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
11258      *   emit code for one insn
11259      *   emit code to clear PSTATE.SS
11260      *   emit code to generate software step exception for completed step
11261      *   end TB (as usual for having generated an exception)
11262      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
11263      *   emit code to generate a software step exception
11264      *   end the TB
11265      */
11266     dc->ss_active = ARM_TBFLAG_SS_ACTIVE(dc->base.tb->flags);
11267     dc->pstate_ss = ARM_TBFLAG_PSTATE_SS(dc->base.tb->flags);
11268     dc->is_ldex = false;
11269     dc->ss_same_el = (arm_debug_target_el(env) == dc->current_el);
11270
11271     /* Bound the number of insns to execute to those left on the page.  */
11272     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
11273
11274     /* If architectural single step active, limit to 1.  */
11275     if (dc->ss_active) {
11276         bound = 1;
11277     }
11278     max_insns = MIN(max_insns, bound);
11279
11280     init_tmp_a64_array(dc);
11281
11282     return max_insns;
11283 }
11284
11285 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
11286 {
11287     tcg_clear_temp_count();
11288 }
11289
11290 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
11291 {
11292     DisasContext *dc = container_of(dcbase, DisasContext, base);
11293
11294     dc->insn_start_idx = tcg_op_buf_count();
11295     tcg_gen_insn_start(dc->pc, 0, 0);
11296 }
11297
11298 static bool aarch64_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,
11299                                         const CPUBreakpoint *bp)
11300 {
11301     DisasContext *dc = container_of(dcbase, DisasContext, base);
11302
11303     if (bp->flags & BP_CPU) {
11304         gen_a64_set_pc_im(dc->pc);
11305         gen_helper_check_breakpoints(cpu_env);
11306         /* End the TB early; it likely won't be executed */
11307         dc->base.is_jmp = DISAS_TOO_MANY;
11308     } else {
11309         gen_exception_internal_insn(dc, 0, EXCP_DEBUG);
11310         /* The address covered by the breakpoint must be
11311            included in [tb->pc, tb->pc + tb->size) in order
11312            to for it to be properly cleared -- thus we
11313            increment the PC here so that the logic setting
11314            tb->size below does the right thing.  */
11315         dc->pc += 4;
11316         dc->base.is_jmp = DISAS_NORETURN;
11317     }
11318
11319     return true;
11320 }
11321
11322 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
11323 {
11324     DisasContext *dc = container_of(dcbase, DisasContext, base);
11325     CPUARMState *env = cpu->env_ptr;
11326
11327     if (dc->ss_active && !dc->pstate_ss) {
11328         /* Singlestep state is Active-pending.
11329          * If we're in this state at the start of a TB then either
11330          *  a) we just took an exception to an EL which is being debugged
11331          *     and this is the first insn in the exception handler
11332          *  b) debug exceptions were masked and we just unmasked them
11333          *     without changing EL (eg by clearing PSTATE.D)
11334          * In either case we're going to take a swstep exception in the
11335          * "did not step an insn" case, and so the syndrome ISV and EX
11336          * bits should be zero.
11337          */
11338         assert(dc->base.num_insns == 1);
11339         gen_exception(EXCP_UDEF, syn_swstep(dc->ss_same_el, 0, 0),
11340                       default_exception_el(dc));
11341         dc->base.is_jmp = DISAS_NORETURN;
11342     } else {
11343         disas_a64_insn(env, dc);
11344     }
11345
11346     dc->base.pc_next = dc->pc;
11347     translator_loop_temp_check(&dc->base);
11348 }
11349
11350 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
11351 {
11352     DisasContext *dc = container_of(dcbase, DisasContext, base);
11353
11354     if (unlikely(dc->base.singlestep_enabled || dc->ss_active)) {
11355         /* Note that this means single stepping WFI doesn't halt the CPU.
11356          * For conditional branch insns this is harmless unreachable code as
11357          * gen_goto_tb() has already handled emitting the debug exception
11358          * (and thus a tb-jump is not possible when singlestepping).
11359          */
11360         switch (dc->base.is_jmp) {
11361         default:
11362             gen_a64_set_pc_im(dc->pc);
11363             /* fall through */
11364         case DISAS_EXIT:
11365         case DISAS_JUMP:
11366             if (dc->base.singlestep_enabled) {
11367                 gen_exception_internal(EXCP_DEBUG);
11368             } else {
11369                 gen_step_complete_exception(dc);
11370             }
11371             break;
11372         case DISAS_NORETURN:
11373             break;
11374         }
11375     } else {
11376         switch (dc->base.is_jmp) {
11377         case DISAS_NEXT:
11378         case DISAS_TOO_MANY:
11379             gen_goto_tb(dc, 1, dc->pc);
11380             break;
11381         default:
11382         case DISAS_UPDATE:
11383             gen_a64_set_pc_im(dc->pc);
11384             /* fall through */
11385         case DISAS_JUMP:
11386             tcg_gen_lookup_and_goto_ptr();
11387             break;
11388         case DISAS_EXIT:
11389             tcg_gen_exit_tb(0);
11390             break;
11391         case DISAS_NORETURN:
11392         case DISAS_SWI:
11393             break;
11394         case DISAS_WFE:
11395             gen_a64_set_pc_im(dc->pc);
11396             gen_helper_wfe(cpu_env);
11397             break;
11398         case DISAS_YIELD:
11399             gen_a64_set_pc_im(dc->pc);
11400             gen_helper_yield(cpu_env);
11401             break;
11402         case DISAS_WFI:
11403         {
11404             /* This is a special case because we don't want to just halt the CPU
11405              * if trying to debug across a WFI.
11406              */
11407             TCGv_i32 tmp = tcg_const_i32(4);
11408
11409             gen_a64_set_pc_im(dc->pc);
11410             gen_helper_wfi(cpu_env, tmp);
11411             tcg_temp_free_i32(tmp);
11412             /* The helper doesn't necessarily throw an exception, but we
11413              * must go back to the main loop to check for interrupts anyway.
11414              */
11415             tcg_gen_exit_tb(0);
11416             break;
11417         }
11418         }
11419     }
11420
11421     /* Functions above can change dc->pc, so re-align db->pc_next */
11422     dc->base.pc_next = dc->pc;
11423 }
11424
11425 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
11426                                       CPUState *cpu)
11427 {
11428     DisasContext *dc = container_of(dcbase, DisasContext, base);
11429
11430     qemu_log("IN: %s\n", lookup_symbol(dc->base.pc_first));
11431     log_target_disas(cpu, dc->base.pc_first, dc->base.tb->size);
11432 }
11433
11434 const TranslatorOps aarch64_translator_ops = {
11435     .init_disas_context = aarch64_tr_init_disas_context,
11436     .tb_start           = aarch64_tr_tb_start,
11437     .insn_start         = aarch64_tr_insn_start,
11438     .breakpoint_check   = aarch64_tr_breakpoint_check,
11439     .translate_insn     = aarch64_tr_translate_insn,
11440     .tb_stop            = aarch64_tr_tb_stop,
11441     .disas_log          = aarch64_tr_disas_log,
11442 };
This page took 0.644272 seconds and 4 git commands to generate.