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.
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.
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/>.
19 #include "qemu/osdep.h"
22 #include "exec/exec-all.h"
26 #include "translate.h"
27 #include "internals.h"
28 #include "qemu/host-utils.h"
30 #include "exec/semihost.h"
31 #include "exec/gen-icount.h"
33 #include "exec/helper-proto.h"
34 #include "exec/helper-gen.h"
37 #include "trace-tcg.h"
39 static TCGv_i64 cpu_X[32];
40 static TCGv_i64 cpu_pc;
42 /* Load/store exclusive handling */
43 static TCGv_i64 cpu_exclusive_high;
44 static TCGv_i64 cpu_reg(DisasContext *s, int reg);
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"
54 A64_SHIFT_TYPE_LSL = 0,
55 A64_SHIFT_TYPE_LSR = 1,
56 A64_SHIFT_TYPE_ASR = 2,
57 A64_SHIFT_TYPE_ROR = 3
60 /* Table based decoder typedefs - used when the relevant bits for decode
61 * are too awkwardly scattered across the instruction (eg SIMD).
63 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
65 typedef struct AArch64DecodeTable {
68 AArch64DecodeFn *disas_fn;
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);
86 /* initialize TCG globals. */
87 void a64_translate_init(void)
91 cpu_pc = tcg_global_mem_new_i64(cpu_env,
92 offsetof(CPUARMState, pc),
94 for (i = 0; i < 32; i++) {
95 cpu_X[i] = tcg_global_mem_new_i64(cpu_env,
96 offsetof(CPUARMState, xregs[i]),
100 cpu_exclusive_high = tcg_global_mem_new_i64(cpu_env,
101 offsetof(CPUARMState, exclusive_high), "exclusive_high");
104 static inline int get_a64_user_mem_index(DisasContext *s)
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
111 switch (s->mmu_idx) {
112 case ARMMMUIdx_S12NSE1:
113 useridx = ARMMMUIdx_S12NSE0;
115 case ARMMMUIdx_S1SE1:
116 useridx = ARMMMUIdx_S1SE0;
119 g_assert_not_reached();
121 useridx = s->mmu_idx;
124 return arm_to_core_mmu_idx(useridx);
127 void aarch64_cpu_dump_state(CPUState *cs, FILE *f,
128 fprintf_function cpu_fprintf, int flags)
130 ARMCPU *cpu = ARM_CPU(cs);
131 CPUARMState *env = &cpu->env;
132 uint32_t psr = pstate_read(env);
134 int el = arm_current_el(env);
135 const char *ns_status;
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]);
142 cpu_fprintf(f, "\n");
148 if (arm_feature(env, ARM_FEATURE_EL3) && el != 3) {
149 ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
154 cpu_fprintf(f, "\nPSTATE=%08x %c%c%c%c %sEL%d%c\n",
156 psr & PSTATE_N ? 'N' : '-',
157 psr & PSTATE_Z ? 'Z' : '-',
158 psr & PSTATE_C ? 'C' : '-',
159 psr & PSTATE_V ? 'V' : '-',
162 psr & PSTATE_SP ? 'h' : 't');
164 if (flags & CPU_DUMP_FPU) {
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 " ",
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",
176 cpu_fprintf(f, "FPCR: %08x FPSR: %08x\n",
177 vfp_get_fpcr(env), vfp_get_fpsr(env));
181 void gen_a64_set_pc_im(uint64_t val)
183 tcg_gen_movi_i64(cpu_pc, val);
186 /* Load the PC from a generic TCG variable.
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]
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.
202 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
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
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
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);
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);
226 tcg_gen_andi_i64(tcg_bit55, src, (1ull << 55));
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,
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,
241 tcg_temp_free_i64(tcg_zero);
242 tcg_temp_free_i64(tcg_bit55);
243 tcg_temp_free_i64(tcg_tmpval);
245 } else { /* EL > 1 */
247 /* Force tag byte to all zero */
248 tcg_gen_andi_i64(cpu_pc, src, 0x00FFFFFFFFFFFFFFull);
250 /* Load unmodified address */
251 tcg_gen_mov_i64(cpu_pc, src);
256 typedef struct DisasCompare64 {
261 static void a64_test_cc(DisasCompare64 *c64, int cc)
265 arm_test_cc(&c32, cc);
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);
276 static void a64_free_cc(DisasCompare64 *c64)
278 tcg_temp_free_i64(c64->value);
281 static void gen_exception_internal(int excp)
283 TCGv_i32 tcg_excp = tcg_const_i32(excp);
285 assert(excp_is_internal(excp));
286 gen_helper_exception_internal(cpu_env, tcg_excp);
287 tcg_temp_free_i32(tcg_excp);
290 static void gen_exception(int excp, uint32_t syndrome, uint32_t target_el)
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);
296 gen_helper_exception_with_syndrome(cpu_env, tcg_excp,
298 tcg_temp_free_i32(tcg_el);
299 tcg_temp_free_i32(tcg_syn);
300 tcg_temp_free_i32(tcg_excp);
303 static void gen_exception_internal_insn(DisasContext *s, int offset, int excp)
305 gen_a64_set_pc_im(s->pc - offset);
306 gen_exception_internal(excp);
307 s->base.is_jmp = DISAS_NORETURN;
310 static void gen_exception_insn(DisasContext *s, int offset, int excp,
311 uint32_t syndrome, uint32_t target_el)
313 gen_a64_set_pc_im(s->pc - offset);
314 gen_exception(excp, syndrome, target_el);
315 s->base.is_jmp = DISAS_NORETURN;
318 static void gen_ss_advance(DisasContext *s)
320 /* If the singlestep state is Active-not-pending, advance to
325 gen_helper_clear_pstate_ss(cpu_env);
329 static void gen_step_complete_exception(DisasContext *s)
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.
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;
346 static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
348 /* No direct tb linking with singlestep (either QEMU's or the ARM
349 * debug architecture kind) or deterministic io
351 if (s->base.singlestep_enabled || s->ss_active ||
352 (tb_cflags(s->base.tb) & CF_LAST_IO)) {
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)) {
366 static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
368 TranslationBlock *tb;
371 if (use_goto_tb(s, n, dest)) {
373 gen_a64_set_pc_im(dest);
374 tcg_gen_exit_tb((intptr_t)tb + n);
375 s->base.is_jmp = DISAS_NORETURN;
377 gen_a64_set_pc_im(dest);
379 gen_step_complete_exception(s);
380 } else if (s->base.singlestep_enabled) {
381 gen_exception_internal(EXCP_DEBUG);
383 tcg_gen_lookup_and_goto_ptr();
384 s->base.is_jmp = DISAS_NORETURN;
389 static void unallocated_encoding(DisasContext *s)
391 /* Unallocated and reserved encodings are uncategorized */
392 gen_exception_insn(s, 4, EXCP_UDEF, syn_uncategorized(),
393 default_exception_el(s));
396 #define unsupported_encoding(s, insn) \
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); \
405 static void init_tmp_a64_array(DisasContext *s)
407 #ifdef CONFIG_DEBUG_TCG
409 for (i = 0; i < ARRAY_SIZE(s->tmp_a64); i++) {
410 TCGV_UNUSED_I64(s->tmp_a64[i]);
413 s->tmp_a64_count = 0;
416 static void free_tmp_a64(DisasContext *s)
419 for (i = 0; i < s->tmp_a64_count; i++) {
420 tcg_temp_free_i64(s->tmp_a64[i]);
422 init_tmp_a64_array(s);
425 static TCGv_i64 new_tmp_a64(DisasContext *s)
427 assert(s->tmp_a64_count < TMP_A64_MAX);
428 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
431 static TCGv_i64 new_tmp_a64_zero(DisasContext *s)
433 TCGv_i64 t = new_tmp_a64(s);
434 tcg_gen_movi_i64(t, 0);
439 * Register access functions
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.
446 * B1.2.1 Register mappings
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.
453 static TCGv_i64 cpu_reg(DisasContext *s, int reg)
456 return new_tmp_a64_zero(s);
462 /* register access for when 31 == SP */
463 static TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
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.
472 static TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
474 TCGv_i64 v = new_tmp_a64(s);
477 tcg_gen_mov_i64(v, cpu_X[reg]);
479 tcg_gen_ext32u_i64(v, cpu_X[reg]);
482 tcg_gen_movi_i64(v, 0);
487 static TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
489 TCGv_i64 v = new_tmp_a64(s);
491 tcg_gen_mov_i64(v, cpu_X[reg]);
493 tcg_gen_ext32u_i64(v, cpu_X[reg]);
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.
505 static inline void assert_fp_access_checked(DisasContext *s)
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);
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.
520 static inline int vec_reg_offset(DisasContext *s, int regno,
521 int element, TCGMemOp size)
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.
531 offs += (16 - ((element + 1) * (1 << size)));
534 offs += element * (1 << size);
536 offs += offsetof(CPUARMState, vfp.regs[regno * 2]);
537 assert_fp_access_checked(s);
541 /* Return the offset into CPUARMState of a slice (from
542 * the least significant end) of FP register Qn (ie
544 * (Note that this is not the same mapping as for A32; see cpu.h)
546 static inline int fp_reg_offset(DisasContext *s, int regno, TCGMemOp size)
548 int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
549 #ifdef HOST_WORDS_BIGENDIAN
550 offs += (8 - (1 << size));
552 assert_fp_access_checked(s);
556 /* Offset of the high half of the 128 bit vector Qn */
557 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
559 assert_fp_access_checked(s);
560 return offsetof(CPUARMState, vfp.regs[regno * 2 + 1]);
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.
569 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
571 TCGv_i64 v = tcg_temp_new_i64();
573 tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
577 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
579 TCGv_i32 v = tcg_temp_new_i32();
581 tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
585 static void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
587 TCGv_i64 tcg_zero = tcg_const_i64(0);
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);
594 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
596 TCGv_i64 tmp = tcg_temp_new_i64();
598 tcg_gen_extu_i32_i64(tmp, v);
599 write_fp_dreg(s, reg, tmp);
600 tcg_temp_free_i64(tmp);
603 static TCGv_ptr get_fpstatus_ptr(void)
605 TCGv_ptr statusptr = tcg_temp_new_ptr();
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.
612 offset = offsetof(CPUARMState, vfp.fp_status);
613 tcg_gen_addi_ptr(statusptr, cpu_env, offset);
617 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
618 * than the 32 bit equivalent.
620 static inline void gen_set_NZ64(TCGv_i64 result)
622 tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
623 tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
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)
630 gen_set_NZ64(result);
632 tcg_gen_extrl_i64_i32(cpu_ZF, result);
633 tcg_gen_mov_i32(cpu_NF, cpu_ZF);
635 tcg_gen_movi_i32(cpu_CF, 0);
636 tcg_gen_movi_i32(cpu_VF, 0);
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)
643 TCGv_i64 result, flag, tmp;
644 result = tcg_temp_new_i64();
645 flag = tcg_temp_new_i64();
646 tmp = tcg_temp_new_i64();
648 tcg_gen_movi_i64(tmp, 0);
649 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
651 tcg_gen_extrl_i64_i32(cpu_CF, flag);
653 gen_set_NZ64(result);
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);
661 tcg_gen_mov_i64(dest, result);
662 tcg_temp_free_i64(result);
663 tcg_temp_free_i64(flag);
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();
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);
680 tcg_temp_free_i32(tmp);
681 tcg_temp_free_i32(t0_32);
682 tcg_temp_free_i32(t1_32);
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)
690 /* 64 bit arithmetic */
691 TCGv_i64 result, flag, tmp;
693 result = tcg_temp_new_i64();
694 flag = tcg_temp_new_i64();
695 tcg_gen_sub_i64(result, t0, t1);
697 gen_set_NZ64(result);
699 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
700 tcg_gen_extrl_i64_i32(cpu_CF, flag);
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);
712 /* 32 bit arithmetic */
713 TCGv_i32 t0_32 = tcg_temp_new_i32();
714 TCGv_i32 t1_32 = tcg_temp_new_i32();
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);
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)
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);
743 tcg_gen_ext32u_i64(dest, dest);
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)
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);
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);
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);
768 tcg_gen_mov_i64(dest, result);
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);
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);
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);
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);
791 tcg_temp_free_i32(tmp);
792 tcg_temp_free_i32(t1_32);
793 tcg_temp_free_i32(t0_32);
798 * Load/Store generators
802 * Store from GPR register to memory.
804 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
805 TCGv_i64 tcg_addr, int size, int memidx,
807 unsigned int iss_srt,
808 bool iss_sf, bool iss_ar)
811 tcg_gen_qemu_st_i64(source, tcg_addr, memidx, s->be_data + size);
816 syn = syn_data_abort_with_iss(0,
822 0, 0, 0, 0, 0, false);
823 disas_set_insn_syndrome(s, syn);
827 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
828 TCGv_i64 tcg_addr, int size,
830 unsigned int iss_srt,
831 bool iss_sf, bool iss_ar)
833 do_gpr_st_memidx(s, source, tcg_addr, size, get_mem_index(s),
834 iss_valid, iss_srt, iss_sf, iss_ar);
838 * Load from memory to GPR register
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)
847 TCGMemOp memop = s->be_data + size;
855 tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
857 if (extend && is_signed) {
859 tcg_gen_ext32u_i64(dest, dest);
865 syn = syn_data_abort_with_iss(0,
871 0, 0, 0, 0, 0, false);
872 disas_set_insn_syndrome(s, syn);
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)
882 do_gpr_ld_memidx(s, dest, tcg_addr, size, is_signed, extend,
884 iss_valid, iss_srt, iss_sf, iss_ar);
888 * Store from FP register to memory
890 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
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));
896 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s),
899 bool be = s->be_data == MO_BE;
900 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
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),
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),
908 tcg_temp_free_i64(tcg_hiaddr);
911 tcg_temp_free_i64(tmp);
915 * Load from memory to FP register
917 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
919 /* This always zero-extends and writes to a full 128 bit wide vector */
920 TCGv_i64 tmplo = tcg_temp_new_i64();
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);
928 bool be = s->be_data == MO_BE;
931 tmphi = tcg_temp_new_i64();
932 tcg_hiaddr = tcg_temp_new_i64();
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),
937 tcg_gen_qemu_ld_i64(tmphi, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
939 tcg_temp_free_i64(tcg_hiaddr);
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));
945 tcg_temp_free_i64(tmplo);
946 tcg_temp_free_i64(tmphi);
950 * Vector load/store helpers.
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.
957 * The _i32 versions are useful when operating on 32 bit quantities
958 * (eg for floating point single or using Neon helper functions).
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)
965 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
968 tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
971 tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
974 tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
977 tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
980 tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
983 tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
987 tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
990 g_assert_not_reached();
994 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
995 int element, TCGMemOp memop)
997 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1000 tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1003 tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1006 tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1009 tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1013 tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1016 g_assert_not_reached();
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)
1024 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1027 tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1030 tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1033 tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1036 tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1039 g_assert_not_reached();
1043 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1044 int destidx, int element, TCGMemOp memop)
1046 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1049 tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1052 tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1055 tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1058 g_assert_not_reached();
1062 /* Clear the high 64 bits of a 128 bit vector (in general non-quad
1063 * vector ops all need to do this).
1065 static void clear_vec_high(DisasContext *s, int rd)
1067 TCGv_i64 tcg_zero = tcg_const_i64(0);
1069 write_vec_element(s, tcg_zero, rd, 1, MO_64);
1070 tcg_temp_free_i64(tcg_zero);
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)
1077 TCGMemOp memop = s->be_data + size;
1078 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
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);
1083 tcg_temp_free_i64(tcg_tmp);
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)
1090 TCGMemOp memop = s->be_data + size;
1091 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
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);
1096 tcg_temp_free_i64(tcg_tmp);
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).
1106 static inline bool fp_access_check(DisasContext *s)
1108 assert(!s->fp_access_checked);
1109 s->fp_access_checked = true;
1111 if (!s->fp_excp_el) {
1115 gen_exception_insn(s, 4, EXCP_UDEF, syn_fp_access_trap(1, 0xe, false),
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.
1125 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1126 int option, unsigned int shift)
1128 int extsize = extract32(option, 0, 2);
1129 bool is_signed = extract32(option, 2, 1);
1134 tcg_gen_ext8s_i64(tcg_out, tcg_in);
1137 tcg_gen_ext16s_i64(tcg_out, tcg_in);
1140 tcg_gen_ext32s_i64(tcg_out, tcg_in);
1143 tcg_gen_mov_i64(tcg_out, tcg_in);
1149 tcg_gen_ext8u_i64(tcg_out, tcg_in);
1152 tcg_gen_ext16u_i64(tcg_out, tcg_in);
1155 tcg_gen_ext32u_i64(tcg_out, tcg_in);
1158 tcg_gen_mov_i64(tcg_out, tcg_in);
1164 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1168 static inline void gen_check_sp_alignment(DisasContext *s)
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).
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.
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)
1192 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1195 const AArch64DecodeTable *tptr = table;
1197 while (tptr->mask) {
1198 if ((insn & tptr->mask) == tptr->pattern) {
1199 return tptr->disas_fn;
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.
1214 /* Unconditional branch (immediate)
1216 * +----+-----------+-------------------------------------+
1217 * | op | 0 0 1 0 1 | imm26 |
1218 * +----+-----------+-------------------------------------+
1220 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
1222 uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
1224 if (insn & (1U << 31)) {
1225 /* BL Branch with link */
1226 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1229 /* B Branch / BL Branch with link */
1230 gen_goto_tb(s, 0, addr);
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 * +----+-------------+----+---------------------+--------+
1239 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
1241 unsigned int sf, op, rt;
1243 TCGLabel *label_match;
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;
1251 tcg_cmp = read_cpu_reg(s, rt, sf);
1252 label_match = gen_new_label();
1254 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1255 tcg_cmp, 0, label_match);
1257 gen_goto_tb(s, 0, s->pc);
1258 gen_set_label(label_match);
1259 gen_goto_tb(s, 1, addr);
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 * +----+-------------+----+-------+-------------+------+
1268 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1270 unsigned int bit_pos, op, rt;
1272 TCGLabel *label_match;
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);
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);
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 * +---------------+----+---------------------+----+------+
1297 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1302 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1303 unallocated_encoding(s);
1306 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1307 cond = extract32(insn, 0, 4);
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);
1317 /* 0xe and 0xf are both "always" conditions */
1318 gen_goto_tb(s, 0, addr);
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)
1326 unsigned int selector = crm << 3 | op2;
1329 unallocated_encoding(s);
1337 s->base.is_jmp = DISAS_WFI;
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.
1345 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1346 s->base.is_jmp = DISAS_YIELD;
1350 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1351 s->base.is_jmp = DISAS_WFE;
1356 /* we treat all as NOP at least for now */
1359 /* default specified as NOP equivalent */
1364 static void gen_clrex(DisasContext *s, uint32_t insn)
1366 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
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)
1376 unallocated_encoding(s);
1387 case 1: /* MBReqTypes_Reads */
1388 bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1390 case 2: /* MBReqTypes_Writes */
1391 bar = TCG_BAR_SC | TCG_MO_ST_ST;
1393 default: /* MBReqTypes_All */
1394 bar = TCG_BAR_SC | TCG_MO_ALL;
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.
1404 gen_goto_tb(s, 0, s->pc);
1407 unallocated_encoding(s);
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)
1416 int op = op1 << 3 | op2;
1418 case 0x05: /* SPSel */
1419 if (s->current_el == 0) {
1420 unallocated_encoding(s);
1424 case 0x1e: /* DAIFSet */
1425 case 0x1f: /* DAIFClear */
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);
1439 unallocated_encoding(s);
1444 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1446 TCGv_i32 tmp = tcg_temp_new_i32();
1447 TCGv_i32 nzcv = tcg_temp_new_i32();
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);
1462 tcg_temp_free_i32(nzcv);
1463 tcg_temp_free_i32(tmp);
1466 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1469 TCGv_i32 nzcv = tcg_temp_new_i32();
1471 /* take NZCV from R[t] */
1472 tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
1475 tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
1477 tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1478 tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1480 tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1481 tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
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);
1488 /* MRS - move from system register
1489 * MSR (register) - move to system register
1492 * These are all essentially the same insn in 'read' and 'write'
1493 * versions, with varying op0 fields.
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)
1499 const ARMCPRegInfo *ri;
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));
1507 /* Unknown register; this might be a guest error or a QEMU
1508 * unimplemented feature.
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);
1517 /* Check access permissions */
1518 if (!cp_access_ok(s->current_el, ri, isread)) {
1519 unallocated_encoding(s);
1524 /* Emit code to perform further access permissions checks at
1525 * runtime; this may result in an exception.
1528 TCGv_i32 tcg_syn, tcg_isread;
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);
1542 /* Handle special cases first */
1543 switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
1547 tcg_rt = cpu_reg(s, rt);
1549 gen_get_nzcv(tcg_rt);
1551 gen_set_nzcv(tcg_rt);
1554 case ARM_CP_CURRENTEL:
1555 /* Reads as current EL value from pstate, which is
1556 * guaranteed to be constant by the tb flags.
1558 tcg_rt = cpu_reg(s, rt);
1559 tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
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);
1570 if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1574 tcg_rt = cpu_reg(s, rt);
1577 if (ri->type & ARM_CP_CONST) {
1578 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1579 } else if (ri->readfn) {
1581 tmpptr = tcg_const_ptr(ri);
1582 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1583 tcg_temp_free_ptr(tmpptr);
1585 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1588 if (ri->type & ARM_CP_CONST) {
1589 /* If not forbidden by access permissions, treat as WI */
1591 } else if (ri->writefn) {
1593 tmpptr = tcg_const_ptr(ri);
1594 gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1595 tcg_temp_free_ptr(tmpptr);
1597 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
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) */
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).
1610 s->base.is_jmp = DISAS_UPDATE;
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 * +---------------------+---+-----+-----+-------+-------+-----+------+
1620 static void disas_system(DisasContext *s, uint32_t insn)
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);
1632 if (l || rt != 31) {
1633 unallocated_encoding(s);
1637 case 2: /* HINT (including allocated hints like NOP, YIELD, etc) */
1638 handle_hint(s, insn, op1, op2, crm);
1640 case 3: /* CLREX, DSB, DMB, ISB */
1641 handle_sync(s, insn, op1, op2, crm);
1643 case 4: /* MSR (immediate) */
1644 handle_msr_i(s, insn, op1, op2, crm);
1647 unallocated_encoding(s);
1652 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
1655 /* Exception generation
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 * +-----------------------+------------------------+----------+
1662 static void disas_exc(DisasContext *s, uint32_t insn)
1664 int opc = extract32(insn, 21, 3);
1665 int op2_ll = extract32(insn, 0, 5);
1666 int imm16 = extract32(insn, 5, 16);
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.
1679 gen_exception_insn(s, 0, EXCP_SWI, syn_aa64_svc(imm16),
1680 default_exception_el(s));
1683 if (s->current_el == 0) {
1684 unallocated_encoding(s);
1687 /* The pre HVC helper handles cases when HVC gets trapped
1688 * as an undefined insn by runtime configuration.
1690 gen_a64_set_pc_im(s->pc - 4);
1691 gen_helper_pre_hvc(cpu_env);
1693 gen_exception_insn(s, 0, EXCP_HVC, syn_aa64_hvc(imm16), 2);
1696 if (s->current_el == 0) {
1697 unallocated_encoding(s);
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);
1705 gen_exception_insn(s, 0, EXCP_SMC, syn_aa64_smc(imm16), 3);
1708 unallocated_encoding(s);
1714 unallocated_encoding(s);
1718 gen_exception_insn(s, 4, EXCP_BKPT, syn_aa64_bkpt(imm16),
1719 default_exception_el(s));
1723 unallocated_encoding(s);
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.
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).
1738 if (s->current_el == 0) {
1739 unsupported_encoding(s, insn);
1743 gen_exception_internal_insn(s, 0, EXCP_SEMIHOST);
1745 unsupported_encoding(s, insn);
1749 if (op2_ll < 1 || op2_ll > 3) {
1750 unallocated_encoding(s);
1753 /* DCPS1, DCPS2, DCPS3 */
1754 unsupported_encoding(s, insn);
1757 unallocated_encoding(s);
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 * +---------------+-------+-------+-------+------+-------+
1768 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
1770 unsigned int opc, op2, op3, rn, op4;
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);
1778 if (op4 != 0x0 || op3 != 0x0 || op2 != 0x1f) {
1779 unallocated_encoding(s);
1787 gen_a64_set_pc(s, cpu_reg(s, rn));
1788 /* BLR also needs to load return address */
1790 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1794 if (s->current_el == 0) {
1795 unallocated_encoding(s);
1798 gen_helper_exception_return(cpu_env);
1799 /* Must exit loop to check un-masked IRQs */
1800 s->base.is_jmp = DISAS_EXIT;
1804 unallocated_encoding(s);
1806 unsupported_encoding(s, insn);
1810 unallocated_encoding(s);
1814 s->base.is_jmp = DISAS_JUMP;
1817 /* Branches, exception generating and system instructions */
1818 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
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);
1825 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
1826 disas_comp_b_imm(s, insn);
1828 case 0x1b: case 0x5b: /* Test & branch (immediate) */
1829 disas_test_b_imm(s, insn);
1831 case 0x2a: /* Conditional branch (immediate) */
1832 disas_cond_b_imm(s, insn);
1834 case 0x6a: /* Exception generation / System */
1835 if (insn & (1 << 24)) {
1836 disas_system(s, insn);
1841 case 0x6b: /* Unconditional branch (register) */
1842 disas_uncond_b_reg(s, insn);
1845 unallocated_encoding(s);
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.
1857 * The store exclusive uses the atomic cmpxchg primitives to avoid
1858 * races in multi-threaded linux-user and when MTTCG softmmu is
1861 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
1862 TCGv_i64 addr, int size, bool is_pair)
1864 int idx = get_mem_index(s);
1865 TCGMemOp memop = s->be_data;
1867 g_assert(size <= 3);
1869 g_assert(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);
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);
1882 /* The pair must be single-copy atomic for *each* doubleword, not
1883 the entire quadword, however it must be quadword aligned. */
1885 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx,
1886 memop | MO_ALIGN_16);
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);
1893 tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
1894 tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
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);
1901 tcg_gen_mov_i64(cpu_exclusive_addr, addr);
1904 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
1905 TCGv_i64 addr, int size, int is_pair)
1907 /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
1908 * && (!is_pair || env->exclusive_high == [addr + datasize])) {
1911 * [addr + datasize] = {Rt2};
1917 * env->exclusive_addr = -1;
1919 TCGLabel *fail_label = gen_new_label();
1920 TCGLabel *done_label = gen_new_label();
1923 tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
1925 tmp = tcg_temp_new_i64();
1928 if (s->be_data == MO_LE) {
1929 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
1931 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
1933 tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
1934 cpu_exclusive_val, tmp,
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,
1945 gen_helper_paired_cmpxchg64_le(tmp, cpu_env, cpu_exclusive_addr,
1946 cpu_reg(s, rt), cpu_reg(s, rt2));
1949 if (tb_cflags(s->base.tb) & CF_PARALLEL) {
1950 gen_helper_paired_cmpxchg64_be_parallel(tmp, cpu_env,
1955 gen_helper_paired_cmpxchg64_be(tmp, cpu_env, cpu_exclusive_addr,
1956 cpu_reg(s, rt), cpu_reg(s, rt2));
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);
1965 tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
1966 tcg_temp_free_i64(tmp);
1967 tcg_gen_br(done_label);
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);
1975 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
1976 * from the ARMv8 specs for LDR (Shared decode for all encodings).
1978 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
1980 int opc0 = extract32(opc, 0, 1);
1984 regsize = opc0 ? 32 : 64;
1986 regsize = size == 3 ? 64 : 32;
1988 return regsize == 64;
1991 /* Load/store exclusive
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 * +-----+-------------+----+---+----+------+----+-------+------+------+
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
2004 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
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);
2017 if ((!is_excl && !is_pair && !is_lasr) ||
2018 (!is_excl && is_pair) ||
2019 (is_pair && size < 2)) {
2020 unallocated_encoding(s);
2025 gen_check_sp_alignment(s);
2027 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2029 /* Note that since TCG is single threaded load-acquire/store-release
2030 * semantics require no extra if (is_lasr) { ... } handling.
2036 gen_load_exclusive(s, rt, rt2, tcg_addr, size, is_pair);
2038 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2042 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2044 gen_store_exclusive(s, rs, rt, rt2, tcg_addr, size, is_pair);
2047 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2048 bool iss_sf = disas_ldst_compute_iss_sf(size, false, 0);
2050 /* Generate ISS for non-exclusive accesses including LASR. */
2053 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2055 do_gpr_st(s, tcg_rt, tcg_addr, size,
2056 true, rt, iss_sf, is_lasr);
2058 do_gpr_ld(s, tcg_rt, tcg_addr, size, false, false,
2059 true, rt, iss_sf, is_lasr);
2061 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2068 * Load register (literal)
2070 * 31 30 29 27 26 25 24 23 5 4 0
2071 * +-----+-------+---+-----+-------------------+-------+
2072 * | opc | 0 1 1 | V | 0 0 | imm19 | Rt |
2073 * +-----+-------+---+-----+-------------------+-------+
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)
2080 static void disas_ld_lit(DisasContext *s, uint32_t insn)
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;
2088 TCGv_i64 tcg_rt, tcg_addr;
2092 unallocated_encoding(s);
2096 if (!fp_access_check(s)) {
2101 /* PRFM (literal) : prefetch */
2104 size = 2 + extract32(opc, 0, 1);
2105 is_signed = extract32(opc, 1, 1);
2108 tcg_rt = cpu_reg(s, rt);
2110 tcg_addr = tcg_const_i64((s->pc - 4) + imm);
2112 do_fp_ld(s, rt, tcg_addr, size);
2114 /* Only unsigned 32bit loads target 32bit registers. */
2115 bool iss_sf = opc != 0;
2117 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false,
2118 true, rt, iss_sf, false);
2120 tcg_temp_free_i64(tcg_addr);
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)
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 * +-----+-------+---+---+-------+---+-------+-------+------+------+
2139 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
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
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)
2151 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
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);
2162 bool is_signed = false;
2163 bool postindex = false;
2166 TCGv_i64 tcg_addr; /* calculated address */
2170 unallocated_encoding(s);
2177 size = 2 + extract32(opc, 1, 1);
2178 is_signed = extract32(opc, 0, 1);
2179 if (!is_load && is_signed) {
2180 unallocated_encoding(s);
2186 case 1: /* post-index */
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
2197 /* There is no non-temporal-hint version of LDPSW */
2198 unallocated_encoding(s);
2203 case 2: /* signed offset, rn not updated */
2206 case 3: /* pre-index */
2212 if (is_vector && !fp_access_check(s)) {
2219 gen_check_sp_alignment(s);
2222 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2225 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
2230 do_fp_ld(s, rt, tcg_addr, size);
2232 do_fp_st(s, rt, tcg_addr, size);
2234 tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
2236 do_fp_ld(s, rt2, tcg_addr, size);
2238 do_fp_st(s, rt2, tcg_addr, size);
2241 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2242 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
2245 TCGv_i64 tmp = tcg_temp_new_i64();
2247 /* Do not modify tcg_rt before recognizing any exception
2248 * from the second load.
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);
2256 tcg_gen_mov_i64(tcg_rt, tmp);
2257 tcg_temp_free_i64(tmp);
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);
2269 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset - (1 << size));
2271 tcg_gen_subi_i64(tcg_addr, tcg_addr, 1 << size);
2273 tcg_gen_mov_i64(cpu_reg_sp(s, rn), tcg_addr);
2278 * Load/store (immediate post-indexed)
2279 * Load/store (immediate pre-indexed)
2280 * Load/store (unscaled immediate)
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 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2287 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
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
2293 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
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;
2313 size |= (opc & 2) << 1;
2314 if (size > 4 || is_unpriv) {
2315 unallocated_encoding(s);
2318 is_store = ((opc & 1) == 0);
2319 if (!fp_access_check(s)) {
2323 if (size == 3 && opc == 2) {
2324 /* PRFM - prefetch */
2326 unallocated_encoding(s);
2331 if (opc == 3 && size > 1) {
2332 unallocated_encoding(s);
2335 is_store = (opc == 0);
2336 is_signed = extract32(opc, 1, 1);
2337 is_extended = (size < 3) && extract32(opc, 0, 1);
2357 gen_check_sp_alignment(s);
2359 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2362 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
2367 do_fp_st(s, rt, tcg_addr, size);
2369 do_fp_ld(s, rt, tcg_addr, size);
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);
2377 do_gpr_st_memidx(s, tcg_rt, tcg_addr, size, memidx,
2378 iss_valid, rt, iss_sf, false);
2380 do_gpr_ld_memidx(s, tcg_rt, tcg_addr, size,
2381 is_signed, is_extended, memidx,
2382 iss_valid, rt, iss_sf, false);
2387 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2389 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
2391 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2396 * Load/store (register offset)
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 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2404 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2405 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
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
2416 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
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;
2433 if (extract32(opt, 1, 1) == 0) {
2434 unallocated_encoding(s);
2439 size |= (opc & 2) << 1;
2441 unallocated_encoding(s);
2444 is_store = !extract32(opc, 0, 1);
2445 if (!fp_access_check(s)) {
2449 if (size == 3 && opc == 2) {
2450 /* PRFM - prefetch */
2453 if (opc == 3 && size > 1) {
2454 unallocated_encoding(s);
2457 is_store = (opc == 0);
2458 is_signed = extract32(opc, 1, 1);
2459 is_extended = (size < 3) && extract32(opc, 0, 1);
2463 gen_check_sp_alignment(s);
2465 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2467 tcg_rm = read_cpu_reg(s, rm, 1);
2468 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
2470 tcg_gen_add_i64(tcg_addr, tcg_addr, tcg_rm);
2474 do_fp_st(s, rt, tcg_addr, size);
2476 do_fp_ld(s, rt, tcg_addr, size);
2479 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2480 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2482 do_gpr_st(s, tcg_rt, tcg_addr, size,
2483 true, rt, iss_sf, false);
2485 do_gpr_ld(s, tcg_rt, tcg_addr, size,
2486 is_signed, is_extended,
2487 true, rt, iss_sf, false);
2493 * Load/store (unsigned immediate)
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 * +----+-------+---+-----+-----+------------+-------+------+
2501 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2502 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
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
2509 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
2515 int rn = extract32(insn, 5, 5);
2516 unsigned int imm12 = extract32(insn, 10, 12);
2517 unsigned int offset;
2522 bool is_signed = false;
2523 bool is_extended = false;
2526 size |= (opc & 2) << 1;
2528 unallocated_encoding(s);
2531 is_store = !extract32(opc, 0, 1);
2532 if (!fp_access_check(s)) {
2536 if (size == 3 && opc == 2) {
2537 /* PRFM - prefetch */
2540 if (opc == 3 && size > 1) {
2541 unallocated_encoding(s);
2544 is_store = (opc == 0);
2545 is_signed = extract32(opc, 1, 1);
2546 is_extended = (size < 3) && extract32(opc, 0, 1);
2550 gen_check_sp_alignment(s);
2552 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2553 offset = imm12 << size;
2554 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
2558 do_fp_st(s, rt, tcg_addr, size);
2560 do_fp_ld(s, rt, tcg_addr, size);
2563 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2564 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2566 do_gpr_st(s, tcg_rt, tcg_addr, size,
2567 true, rt, iss_sf, false);
2569 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended,
2570 true, rt, iss_sf, false);
2575 /* Load/store register (all forms) */
2576 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
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);
2583 switch (extract32(insn, 24, 2)) {
2585 if (extract32(insn, 21, 1) == 1 && extract32(insn, 10, 2) == 2) {
2586 disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
2588 /* Load/store register (unscaled immediate)
2589 * Load/store immediate pre/post-indexed
2590 * Load/store register unprivileged
2592 disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
2596 disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
2599 unallocated_encoding(s);
2604 /* AdvSIMD load/store multiple structures
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 * +---+---+---------------+---+-------------+--------+------+------+------+
2611 * AdvSIMD load/store multiple structures (post-indexed)
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 * +---+---+---------------+---+---+---------+--------+------+------+------+
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
2622 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
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;
2633 int ebytes = 1 << size;
2634 int elements = (is_q ? 128 : 64) / (8 << size);
2635 int rpt; /* num iterations */
2636 int selem; /* structure elements */
2639 if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
2640 unallocated_encoding(s);
2644 /* From the shared decode logic */
2675 unallocated_encoding(s);
2679 if (size == 3 && !is_q && selem != 1) {
2681 unallocated_encoding(s);
2685 if (!fp_access_check(s)) {
2690 gen_check_sp_alignment(s);
2693 tcg_rn = cpu_reg_sp(s, rn);
2694 tcg_addr = tcg_temp_new_i64();
2695 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2697 for (r = 0; r < rpt; r++) {
2699 for (e = 0; e < elements; e++) {
2700 int tt = (rt + r) % 32;
2702 for (xs = 0; xs < selem; xs++) {
2704 do_vec_st(s, tt, e, tcg_addr, size);
2706 do_vec_ld(s, tt, e, tcg_addr, size);
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.
2715 if (!is_q && e == 0 && (r == 0 || xs == selem - 1)) {
2716 clear_vec_high(s, tt);
2719 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2726 int rm = extract32(insn, 16, 5);
2728 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2730 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2733 tcg_temp_free_i64(tcg_addr);
2736 /* AdvSIMD load/store single structure
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 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2743 * AdvSIMD load/store single structure (post-indexed)
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 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
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
2755 * lane_size = encoded in R, opc
2756 * transfer width = encoded in opc, S, size
2758 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
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);
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;
2775 TCGv_i64 tcg_addr, tcg_rn;
2779 if (!is_load || S) {
2780 unallocated_encoding(s);
2789 if (extract32(size, 0, 1)) {
2790 unallocated_encoding(s);
2796 if (extract32(size, 1, 1)) {
2797 unallocated_encoding(s);
2800 if (!extract32(size, 0, 1)) {
2804 unallocated_encoding(s);
2812 g_assert_not_reached();
2815 if (!fp_access_check(s)) {
2819 ebytes = 1 << scale;
2822 gen_check_sp_alignment(s);
2825 tcg_rn = cpu_reg_sp(s, rn);
2826 tcg_addr = tcg_temp_new_i64();
2827 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2829 for (xs = 0; xs < selem; xs++) {
2831 /* Load and replicate to all elements */
2833 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
2835 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr,
2836 get_mem_index(s), s->be_data + scale);
2839 mulconst = 0x0101010101010101ULL;
2842 mulconst = 0x0001000100010001ULL;
2845 mulconst = 0x0000000100000001ULL;
2851 g_assert_not_reached();
2854 tcg_gen_muli_i64(tcg_tmp, tcg_tmp, mulconst);
2856 write_vec_element(s, tcg_tmp, rt, 0, MO_64);
2858 write_vec_element(s, tcg_tmp, rt, 1, MO_64);
2860 clear_vec_high(s, rt);
2862 tcg_temp_free_i64(tcg_tmp);
2864 /* Load/store one element per register */
2866 do_vec_ld(s, rt, index, tcg_addr, scale);
2868 do_vec_st(s, rt, index, tcg_addr, scale);
2871 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2876 int rm = extract32(insn, 16, 5);
2878 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2880 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2883 tcg_temp_free_i64(tcg_addr);
2886 /* Loads and stores */
2887 static void disas_ldst(DisasContext *s, uint32_t insn)
2889 switch (extract32(insn, 24, 6)) {
2890 case 0x08: /* Load/store exclusive */
2891 disas_ldst_excl(s, insn);
2893 case 0x18: case 0x1c: /* Load register (literal) */
2894 disas_ld_lit(s, insn);
2896 case 0x28: case 0x29:
2897 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
2898 disas_ldst_pair(s, insn);
2900 case 0x38: case 0x39:
2901 case 0x3c: case 0x3d: /* Load/store register (all forms) */
2902 disas_ldst_reg(s, insn);
2904 case 0x0c: /* AdvSIMD load/store multiple structures */
2905 disas_ldst_multiple_struct(s, insn);
2907 case 0x0d: /* AdvSIMD load/store single structure */
2908 disas_ldst_single_struct(s, insn);
2911 unallocated_encoding(s);
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 * +----+-------+-----------+-------------------+------+
2922 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
2924 unsigned int page, rd;
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);
2936 /* ADRP (page based) */
2941 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
2945 * Add/subtract (immediate)
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 * +--+--+--+-----------+-----+-------------+-----+-----+
2952 * sf: 0 -> 32bit, 1 -> 64bit
2953 * op: 0 -> add , 1 -> sub
2955 * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
2957 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
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);
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;
2978 unallocated_encoding(s);
2982 tcg_result = tcg_temp_new_i64();
2985 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
2987 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
2990 TCGv_i64 tcg_imm = tcg_const_i64(imm);
2992 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2994 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2996 tcg_temp_free_i64(tcg_imm);
3000 tcg_gen_mov_i64(tcg_rd, tcg_result);
3002 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3005 tcg_temp_free_i64(tcg_result);
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.
3012 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
3022 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
3023 static inline uint64_t bitmask64(unsigned int length)
3025 assert(length > 0 && length <= 64);
3026 return ~0ULL >> (64 - length);
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.
3034 static bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
3035 unsigned int imms, unsigned int immr)
3038 unsigned e, levels, s, r;
3041 assert(immn < 2 && imms < 64 && immr < 64);
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.
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.
3061 * In all cases the rotation is by immr % e (and immr is 6 bits).
3064 /* First determine the element size */
3065 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
3067 /* This is the immn == 0, imms == 0x11111x case */
3077 /* <length of run - 1> mustn't be all-ones. */
3081 /* Create the value of one element: s+1 set bits rotated
3082 * by r within the element (which is e bits wide)...
3084 mask = bitmask64(s + 1);
3086 mask = (mask >> r) | (mask << (e - r));
3087 mask &= bitmask64(e);
3089 /* ...then replicate the element over the whole 64 bit value */
3090 mask = bitfield_replicate(mask, e);
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 * +----+-----+-------------+---+------+------+------+------+
3101 static void disas_logic_imm(DisasContext *s, uint32_t insn)
3103 unsigned int sf, opc, is_n, immr, imms, rn, rd;
3104 TCGv_i64 tcg_rd, tcg_rn;
3106 bool is_and = false;
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);
3117 unallocated_encoding(s);
3121 if (opc == 0x3) { /* ANDS */
3122 tcg_rd = cpu_reg(s, rd);
3124 tcg_rd = cpu_reg_sp(s, rd);
3126 tcg_rn = cpu_reg(s, rn);
3128 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
3129 /* some immediate field values are reserved */
3130 unallocated_encoding(s);
3135 wmask &= 0xffffffff;
3139 case 0x3: /* ANDS */
3141 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
3145 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
3148 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
3151 assert(FALSE); /* must handle all above */
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.
3159 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3162 if (opc == 3) { /* ANDS */
3163 gen_logic_CC(sf, tcg_rd);
3168 * Move wide (immediate)
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 * +--+-----+-------------+-----+----------------+------+
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)
3179 static void disas_movw_imm(DisasContext *s, uint32_t insn)
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);
3189 if (!sf && (pos >= 32)) {
3190 unallocated_encoding(s);
3204 tcg_gen_movi_i64(tcg_rd, imm);
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);
3211 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3215 unallocated_encoding(s);
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 * +----+-----+-------------+---+------+------+------+------+
3226 static void disas_bitfield(DisasContext *s, uint32_t insn)
3228 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
3229 TCGv_i64 tcg_rd, tcg_tmp;
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;
3240 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
3241 unallocated_encoding(s);
3245 tcg_rd = cpu_reg(s, rd);
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);
3252 /* Recognize simple(r) extractions. */
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);
3259 } else if (opc == 2) { /* UBFM: UBFX, LSR, UXTB, UXTH */
3260 tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
3263 /* opc == 1, BXFIL fall through to deposit */
3264 tcg_gen_extract_i64(tcg_tmp, tcg_tmp, ri, len);
3267 /* Handle the ri > si case with a deposit
3268 * Wd<32+s-r,32-r> = Wn<s:0>
3271 pos = (bitsize - ri) & (bitsize - 1);
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);
3282 if (opc == 1) { /* BFM, BXFIL */
3283 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
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);
3293 if (!sf) { /* zero extend final result */
3294 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
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 * +----+------+-------------+---+----+------+--------+------+------+
3304 static void disas_extract(DisasContext *s, uint32_t insn)
3306 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
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;
3318 if (sf != n || op21 || op0 || imm >= bitsize) {
3319 unallocated_encoding(s);
3321 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
3323 tcg_rd = cpu_reg(s, rd);
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.
3330 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
3332 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
3334 } else if (rm == rn) { /* ROR */
3335 tcg_rm = cpu_reg(s, rm);
3337 tcg_gen_rotri_i64(tcg_rd, tcg_rm, imm);
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);
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);
3352 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3358 /* Data processing - immediate */
3359 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
3361 switch (extract32(insn, 23, 6)) {
3362 case 0x20: case 0x21: /* PC-rel. addressing */
3363 disas_pc_rel_adr(s, insn);
3365 case 0x22: case 0x23: /* Add/subtract (immediate) */
3366 disas_add_sub_imm(s, insn);
3368 case 0x24: /* Logical (immediate) */
3369 disas_logic_imm(s, insn);
3371 case 0x25: /* Move wide (immediate) */
3372 disas_movw_imm(s, insn);
3374 case 0x26: /* Bitfield */
3375 disas_bitfield(s, insn);
3377 case 0x27: /* Extract */
3378 disas_extract(s, insn);
3381 unallocated_encoding(s);
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.
3391 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
3392 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
3394 switch (shift_type) {
3395 case A64_SHIFT_TYPE_LSL:
3396 tcg_gen_shl_i64(dst, src, shift_amount);
3398 case A64_SHIFT_TYPE_LSR:
3399 tcg_gen_shr_i64(dst, src, shift_amount);
3401 case A64_SHIFT_TYPE_ASR:
3403 tcg_gen_ext32s_i64(dst, src);
3405 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
3407 case A64_SHIFT_TYPE_ROR:
3409 tcg_gen_rotr_i64(dst, src, shift_amount);
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);
3423 assert(FALSE); /* all shift types should be handled */
3427 if (!sf) { /* zero extend final result */
3428 tcg_gen_ext32u_i64(dst, dst);
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).
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)
3439 assert(shift_i < (sf ? 64 : 32));
3442 tcg_gen_mov_i64(dst, src);
3444 TCGv_i64 shift_const;
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);
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 * +----+-----+-----------+-------+---+------+--------+------+------+
3458 static void disas_logic_reg(DisasContext *s, uint32_t insn)
3460 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
3461 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
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);
3472 if (!sf && (shift_amount & (1 << 5))) {
3473 unallocated_encoding(s);
3477 tcg_rd = cpu_reg(s, rd);
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.
3483 tcg_rm = cpu_reg(s, rm);
3485 tcg_gen_not_i64(tcg_rd, tcg_rm);
3487 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3491 tcg_gen_mov_i64(tcg_rd, tcg_rm);
3493 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
3499 tcg_rm = read_cpu_reg(s, rm, sf);
3502 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
3505 tcg_rn = cpu_reg(s, rn);
3507 switch (opc | (invert << 2)) {
3510 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
3513 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
3516 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
3520 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
3523 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
3526 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
3534 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3538 gen_logic_CC(sf, tcg_rd);
3543 * Add/subtract (extended register)
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 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3550 * sf: 0 -> 32bit, 1 -> 64bit
3551 * op: 0 -> add , 1 -> sub
3554 * option: extension type (see DecodeRegExtend)
3555 * imm3: optional shift to Rm
3557 * Rd = Rn + LSL(extend(Rm), amount)
3559 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
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);
3570 TCGv_i64 tcg_rm, tcg_rn; /* temps */
3572 TCGv_i64 tcg_result;
3575 unallocated_encoding(s);
3579 /* non-flag setting ops may use SP */
3581 tcg_rd = cpu_reg_sp(s, rd);
3583 tcg_rd = cpu_reg(s, rd);
3585 tcg_rn = read_cpu_reg_sp(s, rn, sf);
3587 tcg_rm = read_cpu_reg(s, rm, sf);
3588 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
3590 tcg_result = tcg_temp_new_i64();
3594 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3596 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3600 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3602 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3607 tcg_gen_mov_i64(tcg_rd, tcg_result);
3609 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3612 tcg_temp_free_i64(tcg_result);
3616 * Add/subtract (shifted register)
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 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3623 * sf: 0 -> 32bit, 1 -> 64bit
3624 * op: 0 -> add , 1 -> sub
3626 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
3627 * imm6: Shift amount to apply to Rm before the add/sub
3629 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
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);
3640 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3641 TCGv_i64 tcg_rn, tcg_rm;
3642 TCGv_i64 tcg_result;
3644 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
3645 unallocated_encoding(s);
3649 tcg_rn = read_cpu_reg(s, rn, sf);
3650 tcg_rm = read_cpu_reg(s, rm, sf);
3652 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
3654 tcg_result = tcg_temp_new_i64();
3658 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3660 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3664 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3666 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3671 tcg_gen_mov_i64(tcg_rd, tcg_result);
3673 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3676 tcg_temp_free_i64(tcg_result);
3679 /* Data-processing (3 source)
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 * +--+------+-----------+------+------+----+------+------+------+
3686 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
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;
3703 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
3705 case 0x42: /* SMADDL */
3706 case 0x43: /* SMSUBL */
3707 case 0x44: /* SMULH */
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 */
3719 unallocated_encoding(s);
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);
3730 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3732 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3735 tcg_temp_free_i64(low_bits);
3739 tcg_op1 = tcg_temp_new_i64();
3740 tcg_op2 = tcg_temp_new_i64();
3741 tcg_tmp = tcg_temp_new_i64();
3744 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
3745 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
3748 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
3749 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
3751 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
3752 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
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);
3760 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
3762 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3764 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3769 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
3772 tcg_temp_free_i64(tcg_op1);
3773 tcg_temp_free_i64(tcg_op2);
3774 tcg_temp_free_i64(tcg_tmp);
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 * +--+--+--+------------------------+------+---------+------+-----+
3785 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
3787 unsigned int sf, op, setflags, rm, rn, rd;
3788 TCGv_i64 tcg_y, tcg_rn, tcg_rd;
3790 if (extract32(insn, 10, 6) != 0) {
3791 unallocated_encoding(s);
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);
3802 tcg_rd = cpu_reg(s, rd);
3803 tcg_rn = cpu_reg(s, rn);
3806 tcg_y = new_tmp_a64(s);
3807 tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
3809 tcg_y = cpu_reg(s, rm);
3813 gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
3815 gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
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 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3826 static void disas_cc(DisasContext *s, uint32_t insn)
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;
3833 if (!extract32(insn, 29, 1)) {
3834 unallocated_encoding(s);
3837 if (insn & (1 << 10 | 1 << 4)) {
3838 unallocated_encoding(s);
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);
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);
3855 /* Load the arguments for the new comparison. */
3857 tcg_y = new_tmp_a64(s);
3858 tcg_gen_movi_i64(tcg_y, y);
3860 tcg_y = cpu_reg(s, y);
3862 tcg_rn = cpu_reg(s, rn);
3864 /* Set the flags for the new comparison. */
3865 tcg_tmp = tcg_temp_new_i64();
3867 gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3869 gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3871 tcg_temp_free_i64(tcg_tmp);
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.
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);
3883 if (nzcv & 8) { /* N */
3884 tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
3886 if (TCG_TARGET_HAS_andc_i32) {
3887 tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
3889 tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
3892 if (nzcv & 4) { /* Z */
3893 if (TCG_TARGET_HAS_andc_i32) {
3894 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
3896 tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
3899 tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
3901 if (nzcv & 2) { /* C */
3902 tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
3904 if (TCG_TARGET_HAS_andc_i32) {
3905 tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
3907 tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
3910 if (nzcv & 1) { /* V */
3911 tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
3913 if (TCG_TARGET_HAS_andc_i32) {
3914 tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
3916 tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
3919 tcg_temp_free_i32(tcg_t0);
3920 tcg_temp_free_i32(tcg_t1);
3921 tcg_temp_free_i32(tcg_t2);
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 * +----+----+---+-----------------+------+------+-----+------+------+
3930 static void disas_cond_select(DisasContext *s, uint32_t insn)
3932 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
3933 TCGv_i64 tcg_rd, zero;
3936 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
3937 /* S == 1 or op2<1> == 1 */
3938 unallocated_encoding(s);
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);
3949 tcg_rd = cpu_reg(s, rd);
3951 a64_test_cc(&c, cond);
3952 zero = tcg_const_i64(0);
3954 if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
3956 tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
3958 tcg_gen_neg_i64(tcg_rd, tcg_rd);
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);
3970 tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
3973 tcg_temp_free_i64(zero);
3977 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3981 static void handle_clz(DisasContext *s, unsigned int sf,
3982 unsigned int rn, unsigned int rd)
3984 TCGv_i64 tcg_rd, tcg_rn;
3985 tcg_rd = cpu_reg(s, rd);
3986 tcg_rn = cpu_reg(s, rn);
3989 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
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);
3999 static void handle_cls(DisasContext *s, unsigned int sf,
4000 unsigned int rn, unsigned int rd)
4002 TCGv_i64 tcg_rd, tcg_rn;
4003 tcg_rd = cpu_reg(s, rd);
4004 tcg_rn = cpu_reg(s, rn);
4007 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
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);
4017 static void handle_rbit(DisasContext *s, unsigned int sf,
4018 unsigned int rn, unsigned int rd)
4020 TCGv_i64 tcg_rd, tcg_rn;
4021 tcg_rd = cpu_reg(s, rd);
4022 tcg_rn = cpu_reg(s, rn);
4025 gen_helper_rbit64(tcg_rd, tcg_rn);
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);
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)
4040 unallocated_encoding(s);
4043 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
4046 /* REV with sf==0, opcode==2
4047 * REV32 (sf==1, opcode==2)
4049 static void handle_rev32(DisasContext *s, unsigned int sf,
4050 unsigned int rn, unsigned int rd)
4052 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4055 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4056 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
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);
4065 tcg_temp_free_i64(tcg_tmp);
4067 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
4068 tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
4072 /* REV16 (opcode==1) */
4073 static void handle_rev16(DisasContext *s, unsigned int sf,
4074 unsigned int rn, unsigned int rd)
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);
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);
4087 tcg_temp_free_i64(mask);
4088 tcg_temp_free_i64(tcg_tmp);
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 * +----+---+---+-----------------+---------+--------+------+------+
4097 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
4099 unsigned int sf, opcode, rn, rd;
4101 if (extract32(insn, 29, 1) || extract32(insn, 16, 5)) {
4102 unallocated_encoding(s);
4106 sf = extract32(insn, 31, 1);
4107 opcode = extract32(insn, 10, 6);
4108 rn = extract32(insn, 5, 5);
4109 rd = extract32(insn, 0, 5);
4113 handle_rbit(s, sf, rn, rd);
4116 handle_rev16(s, sf, rn, rd);
4119 handle_rev32(s, sf, rn, rd);
4122 handle_rev64(s, sf, rn, rd);
4125 handle_clz(s, sf, rn, rd);
4128 handle_cls(s, sf, rn, rd);
4133 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
4134 unsigned int rm, unsigned int rn, unsigned int rd)
4136 TCGv_i64 tcg_n, tcg_m, tcg_rd;
4137 tcg_rd = cpu_reg(s, rd);
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));
4145 tcg_n = read_cpu_reg(s, rn, sf);
4146 tcg_m = read_cpu_reg(s, rm, sf);
4150 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
4152 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
4155 if (!sf) { /* zero extend final result */
4156 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
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)
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);
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);
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)
4179 TCGv_i64 tcg_acc, tcg_val;
4182 if (!arm_dc_feature(s, ARM_FEATURE_CRC)
4183 || (sf == 1 && sz != 3)
4184 || (sf == 0 && sz == 3)) {
4185 unallocated_encoding(s);
4190 tcg_val = cpu_reg(s, rm);
4204 g_assert_not_reached();
4206 tcg_val = new_tmp_a64(s);
4207 tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
4210 tcg_acc = cpu_reg(s, rn);
4211 tcg_bytes = tcg_const_i32(1 << sz);
4214 gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
4216 gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
4219 tcg_temp_free_i32(tcg_bytes);
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 * +----+---+---+-----------------+------+--------+------+------+
4228 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
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);
4237 if (extract32(insn, 29, 1)) {
4238 unallocated_encoding(s);
4244 handle_div(s, false, sf, rm, rn, rd);
4247 handle_div(s, true, sf, rm, rn, rd);
4250 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
4253 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
4256 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
4259 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
4268 case 23: /* CRC32 */
4270 int sz = extract32(opcode, 0, 2);
4271 bool crc32c = extract32(opcode, 2, 1);
4272 handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
4276 unallocated_encoding(s);
4281 /* Data processing - register */
4282 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
4284 switch (extract32(insn, 24, 5)) {
4285 case 0x0a: /* Logical (shifted register) */
4286 disas_logic_reg(s, insn);
4288 case 0x0b: /* Add/subtract */
4289 if (insn & (1 << 21)) { /* (extended register) */
4290 disas_add_sub_ext_reg(s, insn);
4292 disas_add_sub_reg(s, insn);
4295 case 0x1b: /* Data-processing (3 source) */
4296 disas_data_proc_3src(s, insn);
4299 switch (extract32(insn, 21, 3)) {
4300 case 0x0: /* Add/subtract (with carry) */
4301 disas_adc_sbc(s, insn);
4303 case 0x2: /* Conditional compare */
4304 disas_cc(s, insn); /* both imm and reg forms */
4306 case 0x4: /* Conditional select */
4307 disas_cond_select(s, insn);
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);
4317 unallocated_encoding(s);
4322 unallocated_encoding(s);
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)
4331 TCGv_i64 tcg_flags = tcg_temp_new_i64();
4332 TCGv_ptr fpst = get_fpstatus_ptr();
4335 TCGv_i64 tcg_vn, tcg_vm;
4337 tcg_vn = read_fp_dreg(s, rn);
4338 if (cmp_with_zero) {
4339 tcg_vm = tcg_const_i64(0);
4341 tcg_vm = read_fp_dreg(s, rm);
4343 if (signal_all_nans) {
4344 gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4346 gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4348 tcg_temp_free_i64(tcg_vn);
4349 tcg_temp_free_i64(tcg_vm);
4351 TCGv_i32 tcg_vn, tcg_vm;
4353 tcg_vn = read_fp_sreg(s, rn);
4354 if (cmp_with_zero) {
4355 tcg_vm = tcg_const_i32(0);
4357 tcg_vm = read_fp_sreg(s, rm);
4359 if (signal_all_nans) {
4360 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4362 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4364 tcg_temp_free_i32(tcg_vn);
4365 tcg_temp_free_i32(tcg_vm);
4368 tcg_temp_free_ptr(fpst);
4370 gen_set_nzcv(tcg_flags);
4372 tcg_temp_free_i64(tcg_flags);
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 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
4381 static void disas_fp_compare(DisasContext *s, uint32_t insn)
4383 unsigned int mos, type, rm, op, rn, opc, op2r;
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);
4393 if (mos || op || op2r || type > 1) {
4394 unallocated_encoding(s);
4398 if (!fp_access_check(s)) {
4402 handle_fp_compare(s, type, rn, rm, opc & 1, opc & 2);
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 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
4411 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
4413 unsigned int mos, type, rm, cond, rn, op, nzcv;
4415 TCGLabel *label_continue = NULL;
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);
4425 if (mos || type > 1) {
4426 unallocated_encoding(s);
4430 if (!fp_access_check(s)) {
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);
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);
4446 handle_fp_compare(s, type, rn, rm, false, op);
4449 gen_set_label(label_continue);
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 * +---+---+---+-----------+------+---+------+------+-----+------+------+
4459 static void disas_fp_csel(DisasContext *s, uint32_t insn)
4461 unsigned int mos, type, rm, cond, rn, rd;
4462 TCGv_i64 t_true, t_false, t_zero;
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);
4472 if (mos || type > 1) {
4473 unallocated_encoding(s);
4477 if (!fp_access_check(s)) {
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);
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);
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);
4500 /* Floating-point data-processing (1 source) - single precision */
4501 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
4507 fpst = get_fpstatus_ptr();
4508 tcg_op = read_fp_sreg(s, rn);
4509 tcg_res = tcg_temp_new_i32();
4512 case 0x0: /* FMOV */
4513 tcg_gen_mov_i32(tcg_res, tcg_op);
4515 case 0x1: /* FABS */
4516 gen_helper_vfp_abss(tcg_res, tcg_op);
4518 case 0x2: /* FNEG */
4519 gen_helper_vfp_negs(tcg_res, tcg_op);
4521 case 0x3: /* FSQRT */
4522 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
4524 case 0x8: /* FRINTN */
4525 case 0x9: /* FRINTP */
4526 case 0xa: /* FRINTM */
4527 case 0xb: /* FRINTZ */
4528 case 0xc: /* FRINTA */
4530 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4532 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4533 gen_helper_rints(tcg_res, tcg_op, fpst);
4535 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4536 tcg_temp_free_i32(tcg_rmode);
4539 case 0xe: /* FRINTX */
4540 gen_helper_rints_exact(tcg_res, tcg_op, fpst);
4542 case 0xf: /* FRINTI */
4543 gen_helper_rints(tcg_res, tcg_op, fpst);
4549 write_fp_sreg(s, rd, tcg_res);
4551 tcg_temp_free_ptr(fpst);
4552 tcg_temp_free_i32(tcg_op);
4553 tcg_temp_free_i32(tcg_res);
4556 /* Floating-point data-processing (1 source) - double precision */
4557 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
4563 fpst = get_fpstatus_ptr();
4564 tcg_op = read_fp_dreg(s, rn);
4565 tcg_res = tcg_temp_new_i64();
4568 case 0x0: /* FMOV */
4569 tcg_gen_mov_i64(tcg_res, tcg_op);
4571 case 0x1: /* FABS */
4572 gen_helper_vfp_absd(tcg_res, tcg_op);
4574 case 0x2: /* FNEG */
4575 gen_helper_vfp_negd(tcg_res, tcg_op);
4577 case 0x3: /* FSQRT */
4578 gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
4580 case 0x8: /* FRINTN */
4581 case 0x9: /* FRINTP */
4582 case 0xa: /* FRINTM */
4583 case 0xb: /* FRINTZ */
4584 case 0xc: /* FRINTA */
4586 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4588 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4589 gen_helper_rintd(tcg_res, tcg_op, fpst);
4591 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4592 tcg_temp_free_i32(tcg_rmode);
4595 case 0xe: /* FRINTX */
4596 gen_helper_rintd_exact(tcg_res, tcg_op, fpst);
4598 case 0xf: /* FRINTI */
4599 gen_helper_rintd(tcg_res, tcg_op, fpst);
4605 write_fp_dreg(s, rd, tcg_res);
4607 tcg_temp_free_ptr(fpst);
4608 tcg_temp_free_i64(tcg_op);
4609 tcg_temp_free_i64(tcg_res);
4612 static void handle_fp_fcvt(DisasContext *s, int opcode,
4613 int rd, int rn, int dtype, int ntype)
4618 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
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);
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);
4633 tcg_temp_free_i32(tcg_rn);
4638 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
4639 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4641 /* Double to single */
4642 gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
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 */
4648 write_fp_sreg(s, rd, tcg_rd);
4649 tcg_temp_free_i32(tcg_rd);
4650 tcg_temp_free_i64(tcg_rn);
4655 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4656 tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
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);
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);
4670 tcg_temp_free_i32(tcg_rn);
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 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4684 static void disas_fp_1src(DisasContext *s, uint32_t insn)
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);
4692 case 0x4: case 0x5: case 0x7:
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);
4700 if (!fp_access_check(s)) {
4704 handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
4710 /* 32-to-32 and 64-to-64 ops */
4713 if (!fp_access_check(s)) {
4717 handle_fp_1src_single(s, opcode, rd, rn);
4720 if (!fp_access_check(s)) {
4724 handle_fp_1src_double(s, opcode, rd, rn);
4727 unallocated_encoding(s);
4731 unallocated_encoding(s);
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)
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);
4751 case 0x0: /* FMUL */
4752 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4754 case 0x1: /* FDIV */
4755 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
4757 case 0x2: /* FADD */
4758 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
4760 case 0x3: /* FSUB */
4761 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
4763 case 0x4: /* FMAX */
4764 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
4766 case 0x5: /* FMIN */
4767 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
4769 case 0x6: /* FMAXNM */
4770 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
4772 case 0x7: /* FMINNM */
4773 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
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);
4781 write_fp_sreg(s, rd, tcg_res);
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);
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)
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);
4804 case 0x0: /* FMUL */
4805 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4807 case 0x1: /* FDIV */
4808 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
4810 case 0x2: /* FADD */
4811 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
4813 case 0x3: /* FSUB */
4814 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
4816 case 0x4: /* FMAX */
4817 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
4819 case 0x5: /* FMIN */
4820 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
4822 case 0x6: /* FMAXNM */
4823 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4825 case 0x7: /* FMINNM */
4826 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
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);
4834 write_fp_dreg(s, rd, tcg_res);
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);
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 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4848 static void disas_fp_2src(DisasContext *s, uint32_t insn)
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);
4857 unallocated_encoding(s);
4863 if (!fp_access_check(s)) {
4866 handle_fp_2src_single(s, opcode, rd, rn, rm);
4869 if (!fp_access_check(s)) {
4872 handle_fp_2src_double(s, opcode, rd, rn, rm);
4875 unallocated_encoding(s);
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)
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();
4887 tcg_op1 = read_fp_sreg(s, rn);
4888 tcg_op2 = read_fp_sreg(s, rm);
4889 tcg_op3 = read_fp_sreg(s, ra);
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.
4899 gen_helper_vfp_negs(tcg_op3, tcg_op3);
4903 gen_helper_vfp_negs(tcg_op1, tcg_op1);
4906 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4908 write_fp_sreg(s, rd, tcg_res);
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);
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)
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();
4925 tcg_op1 = read_fp_dreg(s, rn);
4926 tcg_op2 = read_fp_dreg(s, rm);
4927 tcg_op3 = read_fp_dreg(s, ra);
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.
4937 gen_helper_vfp_negd(tcg_op3, tcg_op3);
4941 gen_helper_vfp_negd(tcg_op1, tcg_op1);
4944 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4946 write_fp_dreg(s, rd, tcg_res);
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);
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 * +---+---+---+-----------+------+----+------+----+------+------+------+
4961 static void disas_fp_3src(DisasContext *s, uint32_t insn)
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);
4973 if (!fp_access_check(s)) {
4976 handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
4979 if (!fp_access_check(s)) {
4982 handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
4985 unallocated_encoding(s);
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 * +---+---+---+-----------+------+---+------------+-------+------+------+
4995 static void disas_fp_imm(DisasContext *s, uint32_t insn)
4997 int rd = extract32(insn, 0, 5);
4998 int imm8 = extract32(insn, 13, 8);
4999 int is_double = extract32(insn, 22, 2);
5003 if (is_double > 1) {
5004 unallocated_encoding(s);
5008 if (!fp_access_check(s)) {
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.
5018 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
5019 (extract32(imm8, 6, 1) ? 0x3fc0 : 0x4000) |
5020 extract32(imm8, 0, 6);
5023 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
5024 (extract32(imm8, 6, 1) ? 0x3e00 : 0x4000) |
5025 (extract32(imm8, 0, 6) << 3);
5029 tcg_res = tcg_const_i64(imm);
5030 write_fp_dreg(s, rd, tcg_res);
5031 tcg_temp_free_i64(tcg_res);
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.
5039 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
5040 bool itof, int rmode, int scale, int sf, int type)
5042 bool is_signed = !(opcode & 1);
5043 bool is_double = type;
5044 TCGv_ptr tcg_fpstatus;
5047 tcg_fpstatus = get_fpstatus_ptr();
5049 tcg_shift = tcg_const_i32(64 - scale);
5052 TCGv_i64 tcg_int = cpu_reg(s, rn);
5054 TCGv_i64 tcg_extend = new_tmp_a64(s);
5057 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
5059 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
5062 tcg_int = tcg_extend;
5066 TCGv_i64 tcg_double = tcg_temp_new_i64();
5068 gen_helper_vfp_sqtod(tcg_double, tcg_int,
5069 tcg_shift, tcg_fpstatus);
5071 gen_helper_vfp_uqtod(tcg_double, tcg_int,
5072 tcg_shift, tcg_fpstatus);
5074 write_fp_dreg(s, rd, tcg_double);
5075 tcg_temp_free_i64(tcg_double);
5077 TCGv_i32 tcg_single = tcg_temp_new_i32();
5079 gen_helper_vfp_sqtos(tcg_single, tcg_int,
5080 tcg_shift, tcg_fpstatus);
5082 gen_helper_vfp_uqtos(tcg_single, tcg_int,
5083 tcg_shift, tcg_fpstatus);
5085 write_fp_sreg(s, rd, tcg_single);
5086 tcg_temp_free_i32(tcg_single);
5089 TCGv_i64 tcg_int = cpu_reg(s, rd);
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.
5096 rmode = FPROUNDING_TIEAWAY;
5099 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
5101 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
5104 TCGv_i64 tcg_double = read_fp_dreg(s, rn);
5107 gen_helper_vfp_tosld(tcg_int, tcg_double,
5108 tcg_shift, tcg_fpstatus);
5110 gen_helper_vfp_tosqd(tcg_int, tcg_double,
5111 tcg_shift, tcg_fpstatus);
5115 gen_helper_vfp_tould(tcg_int, tcg_double,
5116 tcg_shift, tcg_fpstatus);
5118 gen_helper_vfp_touqd(tcg_int, tcg_double,
5119 tcg_shift, tcg_fpstatus);
5122 tcg_temp_free_i64(tcg_double);
5124 TCGv_i32 tcg_single = read_fp_sreg(s, rn);
5127 gen_helper_vfp_tosqs(tcg_int, tcg_single,
5128 tcg_shift, tcg_fpstatus);
5130 gen_helper_vfp_touqs(tcg_int, tcg_single,
5131 tcg_shift, tcg_fpstatus);
5134 TCGv_i32 tcg_dest = tcg_temp_new_i32();
5136 gen_helper_vfp_tosls(tcg_dest, tcg_single,
5137 tcg_shift, tcg_fpstatus);
5139 gen_helper_vfp_touls(tcg_dest, tcg_single,
5140 tcg_shift, tcg_fpstatus);
5142 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
5143 tcg_temp_free_i32(tcg_dest);
5145 tcg_temp_free_i32(tcg_single);
5148 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
5149 tcg_temp_free_i32(tcg_rmode);
5152 tcg_gen_ext32u_i64(tcg_int, tcg_int);
5156 tcg_temp_free_ptr(tcg_fpstatus);
5157 tcg_temp_free_i32(tcg_shift);
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 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
5166 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
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);
5178 if (sbit || (type > 1)
5179 || (!sf && scale < 32)) {
5180 unallocated_encoding(s);
5184 switch ((rmode << 3) | opcode) {
5185 case 0x2: /* SCVTF */
5186 case 0x3: /* UCVTF */
5189 case 0x18: /* FCVTZS */
5190 case 0x19: /* FCVTZU */
5194 unallocated_encoding(s);
5198 if (!fp_access_check(s)) {
5202 handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
5205 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
5207 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
5208 * without conversion.
5212 TCGv_i64 tcg_rn = cpu_reg(s, rn);
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);
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);
5236 /* 64 bit to top half. */
5237 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
5241 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5246 tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
5250 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
5253 /* 64 bits from top half */
5254 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
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 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
5266 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
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);
5277 unallocated_encoding(s);
5283 bool itof = opcode & 1;
5286 unallocated_encoding(s);
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 */
5296 /* all other sf/type/rmode combinations are invalid */
5297 unallocated_encoding(s);
5301 if (!fp_access_check(s)) {
5304 handle_fmov(s, rd, rn, type, itof);
5306 /* actual FP conversions */
5307 bool itof = extract32(opcode, 1, 1);
5309 if (type > 1 || (rmode != 0 && opcode > 1)) {
5310 unallocated_encoding(s);
5314 if (!fp_access_check(s)) {
5317 handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
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 * +---+---+---+---------+-----------------------------+
5327 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
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);
5336 switch (extract32(insn, 10, 2)) {
5338 /* Floating point conditional compare */
5339 disas_fp_ccomp(s, insn);
5342 /* Floating point data-processing (2 source) */
5343 disas_fp_2src(s, insn);
5346 /* Floating point conditional select */
5347 disas_fp_csel(s, insn);
5350 switch (ctz32(extract32(insn, 12, 4))) {
5351 case 0: /* [15:12] == xxx1 */
5352 /* Floating point immediate */
5353 disas_fp_imm(s, insn);
5355 case 1: /* [15:12] == xx10 */
5356 /* Floating point compare */
5357 disas_fp_compare(s, insn);
5359 case 2: /* [15:12] == x100 */
5360 /* Floating point data-processing (1 source) */
5361 disas_fp_1src(s, insn);
5363 case 3: /* [15:12] == 1000 */
5364 unallocated_encoding(s);
5366 default: /* [15:12] == 0000 */
5367 /* Floating point <-> integer conversions */
5368 disas_fp_int_conv(s, insn);
5376 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
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
5385 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5386 assert(pos > 0 && pos < 64);
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);
5392 tcg_temp_free_i64(tcg_tmp);
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 * +---+---+-------------+-----+---+------+---+------+---+------+------+
5401 static void disas_simd_ext(DisasContext *s, uint32_t insn)
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;
5412 if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
5413 unallocated_encoding(s);
5417 if (!fp_access_check(s)) {
5421 tcg_resh = tcg_temp_new_i64();
5422 tcg_resl = tcg_temp_new_i64();
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.
5429 read_vec_element(s, tcg_resl, rn, 0, MO_64);
5431 read_vec_element(s, tcg_resh, rm, 0, MO_64);
5432 do_ext64(s, tcg_resh, tcg_resl, pos);
5434 tcg_gen_movi_i64(tcg_resh, 0);
5441 EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
5442 EltPosns *elt = eltposns;
5449 read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
5451 read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
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);
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);
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 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
5474 static void disas_simd_tb(DisasContext *s, uint32_t insn)
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;
5487 unallocated_encoding(s);
5491 if (!fp_access_check(s)) {
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
5501 tcg_resl = tcg_temp_new_i64();
5502 tcg_resh = tcg_temp_new_i64();
5505 read_vec_element(s, tcg_resl, rd, 0, MO_64);
5507 tcg_gen_movi_i64(tcg_resl, 0);
5509 if (is_tblx && is_q) {
5510 read_vec_element(s, tcg_resh, rd, 1, MO_64);
5512 tcg_gen_movi_i64(tcg_resh, 0);
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);
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);
5526 tcg_temp_free_i64(tcg_idx);
5527 tcg_temp_free_i32(tcg_regno);
5528 tcg_temp_free_i32(tcg_numregs);
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);
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 * +---+---+-------------+------+---+------+---+------------------+------+
5542 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
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.
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;
5556 int datasize = is_q ? 128 : 64;
5557 int elements = datasize / esize;
5558 TCGv_i64 tcg_res, tcg_resl, tcg_resh;
5560 if (opcode == 0 || (size == 3 && !is_q)) {
5561 unallocated_encoding(s);
5565 if (!fp_access_check(s)) {
5569 tcg_resl = tcg_const_i64(0);
5570 tcg_resh = tcg_const_i64(0);
5571 tcg_res = tcg_temp_new_i64();
5573 for (i = 0; i < elements; i++) {
5575 case 1: /* UZP1/2 */
5577 int midpoint = elements / 2;
5579 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
5581 read_vec_element(s, tcg_res, rm,
5582 2 * (i - midpoint) + part, size);
5586 case 2: /* TRN1/2 */
5588 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
5590 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
5593 case 3: /* ZIP1/2 */
5595 int base = part * elements / 2;
5597 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
5599 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
5604 g_assert_not_reached();
5609 tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
5610 tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
5612 tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
5613 tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
5617 tcg_temp_free_i64(tcg_res);
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);
5625 static void do_minmaxop(DisasContext *s, TCGv_i32 tcg_elt1, TCGv_i32 tcg_elt2,
5626 int opc, bool is_min, TCGv_ptr fpst)
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.
5634 gen_helper_vfp_minnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5636 gen_helper_vfp_maxnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5641 gen_helper_vfp_mins(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5643 gen_helper_vfp_maxs(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
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 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5654 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
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);
5663 bool is_min = false;
5667 TCGv_i64 tcg_res, tcg_elt;
5670 case 0x1b: /* ADDV */
5672 unallocated_encoding(s);
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);
5684 case 0xc: /* FMAXNMV, FMINNMV */
5685 case 0xf: /* FMAXV, FMINV */
5686 if (!is_u || !is_q || extract32(size, 0, 1)) {
5687 unallocated_encoding(s);
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
5693 is_min = extract32(size, 1, 1);
5698 unallocated_encoding(s);
5702 if (!fp_access_check(s)) {
5707 elements = (is_q ? 128 : 64) / esize;
5709 tcg_res = tcg_temp_new_i64();
5710 tcg_elt = tcg_temp_new_i64();
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.
5725 read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
5727 for (i = 1; i < elements; i++) {
5728 read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
5731 case 0x03: /* SADDLV / UADDLV */
5732 case 0x1b: /* ADDV */
5733 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
5735 case 0x0a: /* SMAXV / UMAXV */
5736 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
5738 tcg_res, tcg_elt, tcg_res, tcg_elt);
5740 case 0x1a: /* SMINV / UMINV */
5741 tcg_gen_movcond_i64(is_u ? TCG_COND_LEU : TCG_COND_LE,
5743 tcg_res, tcg_elt, tcg_res, tcg_elt);
5747 g_assert_not_reached();
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.
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();
5761 assert(esize == 32);
5762 assert(elements == 4);
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);
5769 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
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);
5776 do_minmaxop(s, tcg_elt2, tcg_elt3, opcode, is_min, fpst);
5778 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
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);
5787 tcg_temp_free_i64(tcg_elt);
5789 /* Now truncate the result to the width required for the final output */
5790 if (opcode == 0x03) {
5791 /* SADDLV, UADDLV: result is 2*esize */
5797 tcg_gen_ext8u_i64(tcg_res, tcg_res);
5800 tcg_gen_ext16u_i64(tcg_res, tcg_res);
5803 tcg_gen_ext32u_i64(tcg_res, tcg_res);
5808 g_assert_not_reached();
5811 write_fp_dreg(s, rd, tcg_res);
5812 tcg_temp_free_i64(tcg_res);
5815 /* DUP (Element, Vector)
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 * +---+---+-------------------+--------+-------------+------+------+
5822 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5824 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
5827 int size = ctz32(imm5);
5828 int esize = 8 << size;
5829 int elements = (is_q ? 128 : 64) / esize;
5833 if (size > 3 || (size == 3 && !is_q)) {
5834 unallocated_encoding(s);
5838 if (!fp_access_check(s)) {
5842 index = imm5 >> (size + 1);
5844 tmp = tcg_temp_new_i64();
5845 read_vec_element(s, tmp, rn, index, size);
5847 for (i = 0; i < elements; i++) {
5848 write_vec_element(s, tmp, rd, i, size);
5852 clear_vec_high(s, rd);
5855 tcg_temp_free_i64(tmp);
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 * +-----------------------+--------+-------------+------+------+
5864 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
5867 int size = ctz32(imm5);
5872 unallocated_encoding(s);
5876 if (!fp_access_check(s)) {
5880 index = imm5 >> (size + 1);
5882 /* This instruction just extracts the specified element and
5883 * zero-extends it into the bottom of the destination register.
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);
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 * +---+---+-------------------+--------+-------------+------+------+
5898 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5900 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
5903 int size = ctz32(imm5);
5904 int esize = 8 << size;
5905 int elements = (is_q ? 128 : 64)/esize;
5908 if (size > 3 || ((size == 3) && !is_q)) {
5909 unallocated_encoding(s);
5913 if (!fp_access_check(s)) {
5917 for (i = 0; i < elements; i++) {
5918 write_vec_element(s, cpu_reg(s, rn), rd, i, size);
5921 clear_vec_high(s, rd);
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 * +-----------------------+--------+------------+---+------+------+
5932 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5933 * index: encoded in imm5<4:size+1>
5935 static void handle_simd_inse(DisasContext *s, int rd, int rn,
5938 int size = ctz32(imm5);
5939 int src_index, dst_index;
5943 unallocated_encoding(s);
5947 if (!fp_access_check(s)) {
5951 dst_index = extract32(imm5, 1+size, 5);
5952 src_index = extract32(imm4, size, 4);
5954 tmp = tcg_temp_new_i64();
5956 read_vec_element(s, tmp, rn, src_index, size);
5957 write_vec_element(s, tmp, rd, dst_index, size);
5959 tcg_temp_free_i64(tmp);
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 * +-----------------------+--------+-------------+------+------+
5970 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5971 * index: encoded in imm5<4:size+1>
5973 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
5975 int size = ctz32(imm5);
5979 unallocated_encoding(s);
5983 if (!fp_access_check(s)) {
5987 idx = extract32(imm5, 1 + size, 4 - size);
5988 write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
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 * +---+---+-------------------+--------+-------------+------+------+
6000 * U: unsigned when set
6001 * size: encoded in imm5 (see ARM ARM LowestSetBit())
6003 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
6004 int rn, int rd, int imm5)
6006 int size = ctz32(imm5);
6010 /* Check for UnallocatedEncodings */
6012 if (size > 2 || (size == 2 && !is_q)) {
6013 unallocated_encoding(s);
6018 || (size < 3 && is_q)
6019 || (size == 3 && !is_q)) {
6020 unallocated_encoding(s);
6025 if (!fp_access_check(s)) {
6029 element = extract32(imm5, 1+size, 4);
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);
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 * +---+---+----+-----------------+------+---+------+---+------+------+
6044 static void disas_simd_copy(DisasContext *s, uint32_t insn)
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);
6056 handle_simd_inse(s, rd, rn, imm4, imm5);
6058 unallocated_encoding(s);
6063 /* DUP (element - vector) */
6064 handle_simd_dupe(s, is_q, rd, rn, imm5);
6068 handle_simd_dupg(s, is_q, rd, rn, imm5);
6073 handle_simd_insg(s, rd, rn, imm5);
6075 unallocated_encoding(s);
6080 /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
6081 handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
6084 unallocated_encoding(s);
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 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
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
6102 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
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);
6113 TCGv_i64 tcg_rd, tcg_imm;
6116 if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
6117 unallocated_encoding(s);
6121 if (!fp_access_check(s)) {
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) */
6132 int shift = cmode_3_1 * 8;
6133 imm = bitfield_replicate(abcdefgh << shift, 32);
6136 case 4: /* Replicate(Zeros(8):imm8, 4) */
6137 case 5: /* Replicate(imm8:Zeros(8), 4) */
6139 int shift = (cmode_3_1 & 0x1) * 8;
6140 imm = bitfield_replicate(abcdefgh << shift, 16);
6145 /* Replicate(Zeros(8):imm8:Ones(16), 2) */
6146 imm = (abcdefgh << 16) | 0xffff;
6148 /* Replicate(Zeros(16):imm8:Ones(8), 2) */
6149 imm = (abcdefgh << 8) | 0xff;
6151 imm = bitfield_replicate(imm, 32);
6154 if (!cmode_0 && !is_neg) {
6155 imm = bitfield_replicate(abcdefgh, 8);
6156 } else if (!cmode_0 && is_neg) {
6159 for (i = 0; i < 8; i++) {
6160 if ((abcdefgh) & (1 << i)) {
6161 imm |= 0xffULL << (i * 8);
6164 } else if (cmode_0) {
6166 imm = (abcdefgh & 0x3f) << 48;
6167 if (abcdefgh & 0x80) {
6168 imm |= 0x8000000000000000ULL;
6170 if (abcdefgh & 0x40) {
6171 imm |= 0x3fc0000000000000ULL;
6173 imm |= 0x4000000000000000ULL;
6176 imm = (abcdefgh & 0x3f) << 19;
6177 if (abcdefgh & 0x80) {
6180 if (abcdefgh & 0x40) {
6191 if (cmode_3_1 != 7 && is_neg) {
6195 tcg_imm = tcg_const_i64(imm);
6196 tcg_rd = new_tmp_a64(s);
6198 for (i = 0; i < 2; i++) {
6199 int foffs = i ? fp_reg_hi_offset(s, rd) : fp_reg_offset(s, rd, MO_64);
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);
6208 tcg_gen_and_i64(tcg_rd, tcg_rd, tcg_imm);
6211 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_imm);
6215 tcg_gen_mov_i64(tcg_rd, tcg_imm);
6217 tcg_gen_st_i64(tcg_rd, cpu_env, foffs);
6220 tcg_temp_free_i64(tcg_imm);
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 * +-----+----+-----------------+------+---+------+---+------+------+
6229 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
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);
6237 if (op != 0 || imm4 != 0) {
6238 unallocated_encoding(s);
6242 /* DUP (element, scalar) */
6243 handle_simd_dupes(s, rd, rn, imm5);
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 * +-----+---+-----------+------+-----------+--------+-----+------+------+
6252 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
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);
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
6265 opcode |= (extract32(size, 1, 1) << 5);
6268 case 0x3b: /* ADDP */
6269 if (u || size != 3) {
6270 unallocated_encoding(s);
6273 if (!fp_access_check(s)) {
6277 TCGV_UNUSED_PTR(fpst);
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 */
6286 unallocated_encoding(s);
6289 if (!fp_access_check(s)) {
6293 size = extract32(size, 0, 1) ? 3 : 2;
6294 fpst = get_fpstatus_ptr();
6297 unallocated_encoding(s);
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();
6306 read_vec_element(s, tcg_op1, rn, 0, MO_64);
6307 read_vec_element(s, tcg_op2, rn, 1, MO_64);
6310 case 0x3b: /* ADDP */
6311 tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
6313 case 0xc: /* FMAXNMP */
6314 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6316 case 0xd: /* FADDP */
6317 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6319 case 0xf: /* FMAXP */
6320 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6322 case 0x2c: /* FMINNMP */
6323 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6325 case 0x2f: /* FMINP */
6326 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6329 g_assert_not_reached();
6332 write_fp_dreg(s, rd, tcg_res);
6334 tcg_temp_free_i64(tcg_op1);
6335 tcg_temp_free_i64(tcg_op2);
6336 tcg_temp_free_i64(tcg_res);
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();
6342 read_vec_element_i32(s, tcg_op1, rn, 0, MO_32);
6343 read_vec_element_i32(s, tcg_op2, rn, 1, MO_32);
6346 case 0xc: /* FMAXNMP */
6347 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6349 case 0xd: /* FADDP */
6350 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6352 case 0xf: /* FMAXP */
6353 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6355 case 0x2c: /* FMINNMP */
6356 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6358 case 0x2f: /* FMINP */
6359 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6362 g_assert_not_reached();
6365 write_fp_sreg(s, rd, tcg_res);
6367 tcg_temp_free_i32(tcg_op1);
6368 tcg_temp_free_i32(tcg_op2);
6369 tcg_temp_free_i32(tcg_res);
6372 if (!TCGV_IS_UNUSED_PTR(fpst)) {
6373 tcg_temp_free_ptr(fpst);
6378 * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
6380 * This code is handles the common shifting code and is used by both
6381 * the vector and scalar code.
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)
6387 bool extended_result = false;
6388 bool round = !TCGV_IS_UNUSED_I64(tcg_rnd);
6390 TCGv_i64 tcg_src_hi;
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);
6404 /* Deal with the rounding step */
6406 if (extended_result) {
6407 TCGv_i64 tcg_zero = tcg_const_i64(0);
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,
6415 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
6419 tcg_temp_free_i64(tcg_zero);
6421 tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
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);
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);
6439 /* essentially shifting in 64 zeros */
6440 tcg_gen_movi_i64(tcg_src, 0);
6442 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6446 /* effectively extending the sign-bit */
6447 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
6449 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
6455 tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
6457 tcg_gen_mov_i64(tcg_res, tcg_src);
6460 if (extended_result) {
6461 tcg_temp_free_i64(tcg_src_hi);
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)
6469 if (insert) { /* SLI */
6470 tcg_gen_deposit_i64(tcg_res, tcg_res, tcg_src, shift, 64 - shift);
6472 tcg_gen_shli_i64(tcg_res, tcg_src, shift);
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)
6480 int esize = 8 << size;
6482 /* shift count same as element size is valid but does nothing;
6483 * special case to avoid potential shift by 64.
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);
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)
6497 int immhb = immh << 3 | immb;
6498 int shift = 2 * (8 << size) - immhb;
6499 bool accumulate = false;
6501 bool insert = false;
6506 if (!extract32(immh, 3, 1)) {
6507 unallocated_encoding(s);
6511 if (!fp_access_check(s)) {
6516 case 0x02: /* SSRA / USRA (accumulate) */
6519 case 0x04: /* SRSHR / URSHR (rounding) */
6522 case 0x06: /* SRSRA / URSRA (accum + rounding) */
6523 accumulate = round = true;
6525 case 0x08: /* SRI */
6531 uint64_t round_const = 1ULL << (shift - 1);
6532 tcg_round = tcg_const_i64(round_const);
6534 TCGV_UNUSED_I64(tcg_round);
6537 tcg_rn = read_fp_dreg(s, rn);
6538 tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6541 handle_shri_with_ins(tcg_rd, tcg_rn, size, shift);
6543 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6544 accumulate, is_u, size, shift);
6547 write_fp_dreg(s, rd, tcg_rd);
6549 tcg_temp_free_i64(tcg_rn);
6550 tcg_temp_free_i64(tcg_rd);
6552 tcg_temp_free_i64(tcg_round);
6556 /* SHL/SLI - Scalar shift left */
6557 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
6558 int immh, int immb, int opcode,
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);
6567 if (!extract32(immh, 3, 1)) {
6568 unallocated_encoding(s);
6572 if (!fp_access_check(s)) {
6576 tcg_rn = read_fp_dreg(s, rn);
6577 tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6579 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
6581 write_fp_dreg(s, rd, tcg_rd);
6583 tcg_temp_free_i64(tcg_rn);
6584 tcg_temp_free_i64(tcg_rd);
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,
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;
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 },
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,
6620 NeonGenNarrowEnvFn *narrowfn;
6626 if (extract32(immh, 3, 1)) {
6627 unallocated_encoding(s);
6631 if (!fp_access_check(s)) {
6636 narrowfn = unsigned_narrow_fns[size];
6638 narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
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);
6647 uint64_t round_const = 1ULL << (shift - 1);
6648 tcg_round = tcg_const_i64(round_const);
6650 TCGV_UNUSED_I64(tcg_round);
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);
6663 clear_vec_high(s, rd);
6664 write_vec_element(s, tcg_final, rd, 0, MO_64);
6666 write_vec_element(s, tcg_final, rd, 1, MO_64);
6670 tcg_temp_free_i64(tcg_round);
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);
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)
6684 int immhb = immh << 3 | immb;
6685 int size = 32 - clz32(immh) - 1;
6686 int shift = immhb - (8 << size);
6690 assert(!(scalar && is_q));
6693 if (!is_q && extract32(immh, 3, 1)) {
6694 unallocated_encoding(s);
6698 /* Since we use the variable-shift helpers we must
6699 * replicate the shift count into each element of
6700 * the tcg_shift value.
6704 shift |= shift << 8;
6707 shift |= shift << 16;
6713 g_assert_not_reached();
6717 if (!fp_access_check(s)) {
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 },
6727 NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
6728 int maxpass = is_q ? 2 : 1;
6730 for (pass = 0; pass < maxpass; pass++) {
6731 TCGv_i64 tcg_op = tcg_temp_new_i64();
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);
6737 tcg_temp_free_i64(tcg_op);
6739 tcg_temp_free_i64(tcg_shift);
6742 clear_vec_high(s, rd);
6745 TCGv_i32 tcg_shift = tcg_const_i32(shift);
6746 static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
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 }
6755 { NULL, NULL, NULL },
6756 { gen_helper_neon_qshl_u8,
6757 gen_helper_neon_qshl_u16,
6758 gen_helper_neon_qshl_u32 }
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;
6765 for (pass = 0; pass < maxpass; pass++) {
6766 TCGv_i32 tcg_op = tcg_temp_new_i32();
6768 read_vec_element_i32(s, tcg_op, rn, pass, memop);
6769 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6773 tcg_gen_ext8u_i32(tcg_op, tcg_op);
6776 tcg_gen_ext16u_i32(tcg_op, tcg_op);
6781 g_assert_not_reached();
6783 write_fp_sreg(s, rd, tcg_op);
6785 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
6788 tcg_temp_free_i32(tcg_op);
6790 tcg_temp_free_i32(tcg_shift);
6792 if (!is_q && !scalar) {
6793 clear_vec_high(s, rd);
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)
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);
6810 for (pass = 0; pass < elements; pass++) {
6811 read_vec_element(s, tcg_int, rn, pass, mop);
6814 TCGv_i64 tcg_double = tcg_temp_new_i64();
6816 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6817 tcg_shift, tcg_fpst);
6819 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6820 tcg_shift, tcg_fpst);
6822 if (elements == 1) {
6823 write_fp_dreg(s, rd, tcg_double);
6825 write_vec_element(s, tcg_double, rd, pass, MO_64);
6827 tcg_temp_free_i64(tcg_double);
6829 TCGv_i32 tcg_single = tcg_temp_new_i32();
6831 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6832 tcg_shift, tcg_fpst);
6834 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6835 tcg_shift, tcg_fpst);
6837 if (elements == 1) {
6838 write_fp_sreg(s, rd, tcg_single);
6840 write_vec_element_i32(s, tcg_single, rd, pass, MO_32);
6842 tcg_temp_free_i32(tcg_single);
6846 if (!is_double && elements == 2) {
6847 clear_vec_high(s, rd);
6850 tcg_temp_free_i64(tcg_int);
6851 tcg_temp_free_ptr(tcg_fpst);
6852 tcg_temp_free_i32(tcg_shift);
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,
6861 bool is_double = extract32(immh, 3, 1);
6862 int size = is_double ? MO_64 : MO_32;
6864 int immhb = immh << 3 | immb;
6865 int fracbits = (is_double ? 128 : 64) - immhb;
6867 if (!extract32(immh, 2, 2)) {
6868 unallocated_encoding(s);
6875 elements = is_double ? 2 : is_q ? 4 : 2;
6876 if (is_double && !is_q) {
6877 unallocated_encoding(s);
6882 if (!fp_access_check(s)) {
6886 /* immh == 0 would be a failure of the decode logic */
6889 handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
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)
6897 bool is_double = extract32(immh, 3, 1);
6898 int immhb = immh << 3 | immb;
6899 int fracbits = (is_double ? 128 : 64) - immhb;
6901 TCGv_ptr tcg_fpstatus;
6902 TCGv_i32 tcg_rmode, tcg_shift;
6904 if (!extract32(immh, 2, 2)) {
6905 unallocated_encoding(s);
6909 if (!is_scalar && !is_q && is_double) {
6910 unallocated_encoding(s);
6914 if (!fp_access_check(s)) {
6918 assert(!(is_scalar && is_q));
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);
6926 int maxpass = is_scalar ? 1 : 2;
6928 for (pass = 0; pass < maxpass; pass++) {
6929 TCGv_i64 tcg_op = tcg_temp_new_i64();
6931 read_vec_element(s, tcg_op, rn, pass, MO_64);
6933 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6935 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6937 write_vec_element(s, tcg_op, rd, pass, MO_64);
6938 tcg_temp_free_i64(tcg_op);
6941 clear_vec_high(s, rd);
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();
6948 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
6950 gen_helper_vfp_touls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6952 gen_helper_vfp_tosls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6955 write_fp_sreg(s, rd, tcg_op);
6957 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
6959 tcg_temp_free_i32(tcg_op);
6961 if (!is_q && !is_scalar) {
6962 clear_vec_high(s, rd);
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);
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 * +-----+---+-------------+------+------+--------+---+------+------+
6978 * This is the scalar version so it works on a fixed sized registers
6980 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
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);
6990 unallocated_encoding(s);
6995 case 0x08: /* SRI */
6997 unallocated_encoding(s);
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);
7007 case 0x0a: /* SHL / SLI */
7008 handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
7010 case 0x1c: /* SCVTF, UCVTF */
7011 handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
7014 case 0x10: /* SQSHRUN, SQSHRUN2 */
7015 case 0x11: /* SQRSHRUN, SQRSHRUN2 */
7017 unallocated_encoding(s);
7020 handle_vec_simd_sqshrn(s, true, false, false, true,
7021 immh, immb, opcode, rn, rd);
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);
7028 case 0xc: /* SQSHLU */
7030 unallocated_encoding(s);
7033 handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
7035 case 0xe: /* SQSHL, UQSHL */
7036 handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
7038 case 0x1f: /* FCVTZS, FCVTZU */
7039 handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
7042 unallocated_encoding(s);
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 * +-----+---+-----------+------+---+------+--------+-----+------+------+
7053 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
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);
7063 unallocated_encoding(s);
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);
7077 unallocated_encoding(s);
7081 if (!fp_access_check(s)) {
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();
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);
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);
7097 case 0xd: /* SQDMULL, SQDMULL2 */
7099 case 0xb: /* SQDMLSL, SQDMLSL2 */
7100 tcg_gen_neg_i64(tcg_res, tcg_res);
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,
7108 g_assert_not_reached();
7111 write_fp_dreg(s, rd, tcg_res);
7113 tcg_temp_free_i64(tcg_op1);
7114 tcg_temp_free_i64(tcg_op2);
7115 tcg_temp_free_i64(tcg_res);
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();
7121 read_vec_element_i32(s, tcg_op1, rn, 0, MO_16);
7122 read_vec_element_i32(s, tcg_op2, rm, 0, MO_16);
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);
7128 case 0xd: /* SQDMULL, SQDMULL2 */
7130 case 0xb: /* SQDMLSL, SQDMLSL2 */
7131 gen_helper_neon_negl_u32(tcg_res, tcg_res);
7133 case 0x9: /* SQDMLAL, SQDMLAL2 */
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,
7139 tcg_temp_free_i64(tcg_op3);
7143 g_assert_not_reached();
7146 tcg_gen_ext32u_i64(tcg_res, tcg_res);
7147 write_fp_dreg(s, rd, tcg_res);
7149 tcg_temp_free_i32(tcg_op1);
7150 tcg_temp_free_i32(tcg_op2);
7151 tcg_temp_free_i64(tcg_res);
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)
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
7167 case 0x1: /* SQADD */
7169 gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7171 gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7174 case 0x5: /* SQSUB */
7176 gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7178 gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
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.
7185 cond = u ? TCG_COND_GTU : TCG_COND_GT;
7187 tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
7188 tcg_gen_neg_i64(tcg_rd, tcg_rd);
7190 case 0x7: /* CMGE, CMHS */
7191 cond = u ? TCG_COND_GEU : TCG_COND_GE;
7193 case 0x11: /* CMTST, CMEQ */
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);
7203 case 0x8: /* SSHL, USHL */
7205 gen_helper_neon_shl_u64(tcg_rd, tcg_rn, tcg_rm);
7207 gen_helper_neon_shl_s64(tcg_rd, tcg_rn, tcg_rm);
7210 case 0x9: /* SQSHL, UQSHL */
7212 gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7214 gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7217 case 0xa: /* SRSHL, URSHL */
7219 gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
7221 gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
7224 case 0xb: /* SQRSHL, UQRSHL */
7226 gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7228 gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7231 case 0x10: /* ADD, SUB */
7233 tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
7235 tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
7239 g_assert_not_reached();
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.
7247 static void handle_3same_float(DisasContext *s, int size, int elements,
7248 int fpopcode, int rd, int rn, int rm)
7251 TCGv_ptr fpst = get_fpstatus_ptr();
7253 for (pass = 0; pass < elements; pass++) {
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();
7260 read_vec_element(s, tcg_op1, rn, pass, MO_64);
7261 read_vec_element(s, tcg_op2, rm, pass, MO_64);
7264 case 0x39: /* FMLS */
7265 /* As usual for ARM, separate negation for fused multiply-add */
7266 gen_helper_vfp_negd(tcg_op1, tcg_op1);
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,
7273 case 0x18: /* FMAXNM */
7274 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7276 case 0x1a: /* FADD */
7277 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
7279 case 0x1b: /* FMULX */
7280 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
7282 case 0x1c: /* FCMEQ */
7283 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7285 case 0x1e: /* FMAX */
7286 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
7288 case 0x1f: /* FRECPS */
7289 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7291 case 0x38: /* FMINNM */
7292 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7294 case 0x3a: /* FSUB */
7295 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
7297 case 0x3e: /* FMIN */
7298 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
7300 case 0x3f: /* FRSQRTS */
7301 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7303 case 0x5b: /* FMUL */
7304 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
7306 case 0x5c: /* FCMGE */
7307 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7309 case 0x5d: /* FACGE */
7310 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7312 case 0x5f: /* FDIV */
7313 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
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);
7319 case 0x7c: /* FCMGT */
7320 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7322 case 0x7d: /* FACGT */
7323 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7326 g_assert_not_reached();
7329 write_vec_element(s, tcg_res, rd, pass, MO_64);
7331 tcg_temp_free_i64(tcg_res);
7332 tcg_temp_free_i64(tcg_op1);
7333 tcg_temp_free_i64(tcg_op2);
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();
7340 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
7341 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
7344 case 0x39: /* FMLS */
7345 /* As usual for ARM, separate negation for fused multiply-add */
7346 gen_helper_vfp_negs(tcg_op1, tcg_op1);
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,
7353 case 0x1a: /* FADD */
7354 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
7356 case 0x1b: /* FMULX */
7357 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
7359 case 0x1c: /* FCMEQ */
7360 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7362 case 0x1e: /* FMAX */
7363 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
7365 case 0x1f: /* FRECPS */
7366 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7368 case 0x18: /* FMAXNM */
7369 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
7371 case 0x38: /* FMINNM */
7372 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
7374 case 0x3a: /* FSUB */
7375 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
7377 case 0x3e: /* FMIN */
7378 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
7380 case 0x3f: /* FRSQRTS */
7381 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7383 case 0x5b: /* FMUL */
7384 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
7386 case 0x5c: /* FCMGE */
7387 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7389 case 0x5d: /* FACGE */
7390 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7392 case 0x5f: /* FDIV */
7393 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
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);
7399 case 0x7c: /* FCMGT */
7400 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7402 case 0x7d: /* FACGT */
7403 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7406 g_assert_not_reached();
7409 if (elements == 1) {
7410 /* scalar single so clear high part */
7411 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
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);
7417 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7420 tcg_temp_free_i32(tcg_res);
7421 tcg_temp_free_i32(tcg_op1);
7422 tcg_temp_free_i32(tcg_op2);
7426 tcg_temp_free_ptr(fpst);
7428 if ((elements << size) < 4) {
7429 /* scalar, or non-quad vector op */
7430 clear_vec_high(s, rd);
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 * +-----+---+-----------+------+---+------+--------+---+------+------+
7440 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
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);
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);
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 */
7465 unallocated_encoding(s);
7469 if (!fp_access_check(s)) {
7473 handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
7478 case 0x1: /* SQADD, UQADD */
7479 case 0x5: /* SQSUB, UQSUB */
7480 case 0x9: /* SQSHL, UQSHL */
7481 case 0xb: /* SQRSHL, UQRSHL */
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) */
7490 unallocated_encoding(s);
7494 case 0x16: /* SQDMULH, SQRDMULH (vector) */
7495 if (size != 1 && size != 2) {
7496 unallocated_encoding(s);
7501 unallocated_encoding(s);
7505 if (!fp_access_check(s)) {
7509 tcg_rd = tcg_temp_new_i64();
7512 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
7513 TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
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);
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.
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();
7530 read_vec_element_i32(s, tcg_rn, rn, 0, size);
7531 read_vec_element_i32(s, tcg_rm, rm, 0, size);
7534 case 0x1: /* SQADD, UQADD */
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 },
7541 genenvfn = fns[size][u];
7544 case 0x5: /* SQSUB, UQSUB */
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 },
7551 genenvfn = fns[size][u];
7554 case 0x9: /* SQSHL, UQSHL */
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 },
7561 genenvfn = fns[size][u];
7564 case 0xb: /* SQRSHL, UQRSHL */
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 },
7571 genenvfn = fns[size][u];
7574 case 0x16: /* SQDMULH, SQRDMULH */
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 },
7580 assert(size == 1 || size == 2);
7581 genenvfn = fns[size - 1][u];
7585 g_assert_not_reached();
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);
7595 write_fp_dreg(s, rd, tcg_rd);
7597 tcg_temp_free_i64(tcg_rd);
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)
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
7613 case 0x4: /* CLS, CLZ */
7615 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
7617 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
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.
7624 tcg_gen_not_i64(tcg_rd, tcg_rn);
7626 case 0x7: /* SQABS, SQNEG */
7628 gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
7630 gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
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
7640 tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
7641 tcg_gen_neg_i64(tcg_rd, tcg_rd);
7643 case 0x8: /* CMGT, CMGE */
7644 cond = u ? TCG_COND_GE : TCG_COND_GT;
7646 case 0x9: /* CMEQ, CMLE */
7647 cond = u ? TCG_COND_LE : TCG_COND_EQ;
7649 case 0xb: /* ABS, NEG */
7651 tcg_gen_neg_i64(tcg_rd, tcg_rn);
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,
7657 tcg_temp_free_i64(tcg_zero);
7660 case 0x2f: /* FABS */
7661 gen_helper_vfp_absd(tcg_rd, tcg_rn);
7663 case 0x6f: /* FNEG */
7664 gen_helper_vfp_negd(tcg_rd, tcg_rn);
7666 case 0x7f: /* FSQRT */
7667 gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
7669 case 0x1a: /* FCVTNS */
7670 case 0x1b: /* FCVTMS */
7671 case 0x1c: /* FCVTAS */
7672 case 0x3a: /* FCVTPS */
7673 case 0x3b: /* FCVTZS */
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);
7680 case 0x5a: /* FCVTNU */
7681 case 0x5b: /* FCVTMU */
7682 case 0x5c: /* FCVTAU */
7683 case 0x7a: /* FCVTPU */
7684 case 0x7b: /* FCVTZU */
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);
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);
7699 case 0x59: /* FRINTX */
7700 gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
7703 g_assert_not_reached();
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)
7711 bool is_double = (size == 3);
7714 if (!fp_access_check(s)) {
7718 fpst = get_fpstatus_ptr();
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;
7729 case 0x2e: /* FCMLT (zero) */
7732 case 0x2c: /* FCMGT (zero) */
7733 genfn = gen_helper_neon_cgt_f64;
7735 case 0x2d: /* FCMEQ (zero) */
7736 genfn = gen_helper_neon_ceq_f64;
7738 case 0x6d: /* FCMLE (zero) */
7741 case 0x6c: /* FCMGE (zero) */
7742 genfn = gen_helper_neon_cge_f64;
7745 g_assert_not_reached();
7748 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7749 read_vec_element(s, tcg_op, rn, pass, MO_64);
7751 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7753 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7755 write_vec_element(s, tcg_res, rd, pass, MO_64);
7758 clear_vec_high(s, rd);
7761 tcg_temp_free_i64(tcg_res);
7762 tcg_temp_free_i64(tcg_zero);
7763 tcg_temp_free_i64(tcg_op);
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;
7770 int pass, maxpasses;
7773 case 0x2e: /* FCMLT (zero) */
7776 case 0x2c: /* FCMGT (zero) */
7777 genfn = gen_helper_neon_cgt_f32;
7779 case 0x2d: /* FCMEQ (zero) */
7780 genfn = gen_helper_neon_ceq_f32;
7782 case 0x6d: /* FCMLE (zero) */
7785 case 0x6c: /* FCMGE (zero) */
7786 genfn = gen_helper_neon_cge_f32;
7789 g_assert_not_reached();
7795 maxpasses = is_q ? 4 : 2;
7798 for (pass = 0; pass < maxpasses; pass++) {
7799 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7801 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7803 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7806 write_fp_sreg(s, rd, tcg_res);
7808 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
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);
7819 tcg_temp_free_ptr(fpst);
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)
7826 bool is_double = (size == 3);
7827 TCGv_ptr fpst = get_fpstatus_ptr();
7830 TCGv_i64 tcg_op = tcg_temp_new_i64();
7831 TCGv_i64 tcg_res = tcg_temp_new_i64();
7834 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7835 read_vec_element(s, tcg_op, rn, pass, MO_64);
7837 case 0x3d: /* FRECPE */
7838 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
7840 case 0x3f: /* FRECPX */
7841 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
7843 case 0x7d: /* FRSQRTE */
7844 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
7847 g_assert_not_reached();
7849 write_vec_element(s, tcg_res, rd, pass, MO_64);
7852 clear_vec_high(s, rd);
7855 tcg_temp_free_i64(tcg_res);
7856 tcg_temp_free_i64(tcg_op);
7858 TCGv_i32 tcg_op = tcg_temp_new_i32();
7859 TCGv_i32 tcg_res = tcg_temp_new_i32();
7860 int pass, maxpasses;
7865 maxpasses = is_q ? 4 : 2;
7868 for (pass = 0; pass < maxpasses; pass++) {
7869 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7872 case 0x3c: /* URECPE */
7873 gen_helper_recpe_u32(tcg_res, tcg_op, fpst);
7875 case 0x3d: /* FRECPE */
7876 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
7878 case 0x3f: /* FRECPX */
7879 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
7881 case 0x7d: /* FRSQRTE */
7882 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
7885 g_assert_not_reached();
7889 write_fp_sreg(s, rd, tcg_res);
7891 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
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);
7900 tcg_temp_free_ptr(fpst);
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)
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).
7911 TCGv_i32 tcg_res[2];
7912 int destelt = is_q ? 2 : 0;
7913 int passes = scalar ? 1 : 2;
7916 tcg_res[1] = tcg_const_i32(0);
7919 for (pass = 0; pass < passes; pass++) {
7920 TCGv_i64 tcg_op = tcg_temp_new_i64();
7921 NeonGenNarrowFn *genfn = NULL;
7922 NeonGenNarrowEnvFn *genenvfn = NULL;
7925 read_vec_element(s, tcg_op, rn, pass, size + 1);
7927 read_vec_element(s, tcg_op, rn, pass, MO_64);
7929 tcg_res[pass] = tcg_temp_new_i32();
7932 case 0x12: /* XTN, SQXTUN */
7934 static NeonGenNarrowFn * const xtnfns[3] = {
7935 gen_helper_neon_narrow_u8,
7936 gen_helper_neon_narrow_u16,
7937 tcg_gen_extrl_i64_i32,
7939 static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
7940 gen_helper_neon_unarrow_sat8,
7941 gen_helper_neon_unarrow_sat16,
7942 gen_helper_neon_unarrow_sat32,
7945 genenvfn = sqxtunfns[size];
7947 genfn = xtnfns[size];
7951 case 0x14: /* SQXTN, UQXTN */
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 },
7961 genenvfn = fns[size][u];
7964 case 0x16: /* FCVTN, FCVTN2 */
7965 /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
7967 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
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);
7979 case 0x56: /* FCVTXN, FCVTXN2 */
7980 /* 64 bit to 32 bit float conversion
7981 * with von Neumann rounding (round to odd)
7984 gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
7987 g_assert_not_reached();
7991 genfn(tcg_res[pass], tcg_op);
7992 } else if (genenvfn) {
7993 genenvfn(tcg_res[pass], cpu_env, tcg_op);
7996 tcg_temp_free_i64(tcg_op);
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]);
8004 clear_vec_high(s, rd);
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)
8012 bool is_double = (size == 3);
8015 TCGv_i64 tcg_rn = tcg_temp_new_i64();
8016 TCGv_i64 tcg_rd = tcg_temp_new_i64();
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);
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);
8028 write_vec_element(s, tcg_rd, rd, pass, MO_64);
8031 clear_vec_high(s, rd);
8034 tcg_temp_free_i64(tcg_rd);
8035 tcg_temp_free_i64(tcg_rn);
8037 TCGv_i32 tcg_rn = tcg_temp_new_i32();
8038 TCGv_i32 tcg_rd = tcg_temp_new_i32();
8039 int pass, maxpasses;
8044 maxpasses = is_q ? 4 : 2;
8047 for (pass = 0; pass < maxpasses; pass++) {
8049 read_vec_element_i32(s, tcg_rn, rn, pass, size);
8050 read_vec_element_i32(s, tcg_rd, rd, pass, size);
8052 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
8053 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
8056 if (is_u) { /* USQADD */
8059 gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8062 gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8065 gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8068 g_assert_not_reached();
8070 } else { /* SUQADD */
8073 gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8076 gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8079 gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8082 g_assert_not_reached();
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);
8091 write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
8095 clear_vec_high(s, rd);
8098 tcg_temp_free_i32(tcg_rd);
8099 tcg_temp_free_i32(tcg_rn);
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 * +-----+---+-----------+------+-----------+--------+-----+------+------+
8109 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
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;
8119 TCGv_ptr tcg_fpstatus;
8122 case 0x3: /* USQADD / SUQADD*/
8123 if (!fp_access_check(s)) {
8126 handle_2misc_satacc(s, true, u, false, size, rn, rd);
8128 case 0x7: /* SQABS / SQNEG */
8130 case 0xa: /* CMLT */
8132 unallocated_encoding(s);
8136 case 0x8: /* CMGT, CMGE */
8137 case 0x9: /* CMEQ, CMLE */
8138 case 0xb: /* ABS, NEG */
8140 unallocated_encoding(s);
8144 case 0x12: /* SQXTUN */
8146 unallocated_encoding(s);
8150 case 0x14: /* SQXTN, UQXTN */
8152 unallocated_encoding(s);
8155 if (!fp_access_check(s)) {
8158 handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
8163 /* Floating point: U, size[1] and opcode indicate operation;
8164 * size[0] indicates single or double precision.
8166 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
8167 size = extract32(size, 0, 1) ? 3 : 2;
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);
8176 case 0x1d: /* SCVTF */
8177 case 0x5d: /* UCVTF */
8179 bool is_signed = (opcode == 0x1d);
8180 if (!fp_access_check(s)) {
8183 handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
8186 case 0x3d: /* FRECPE */
8187 case 0x3f: /* FRECPX */
8188 case 0x7d: /* FRSQRTE */
8189 if (!fp_access_check(s)) {
8192 handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
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 */
8203 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
8205 case 0x1c: /* FCVTAS */
8206 case 0x5c: /* FCVTAU */
8207 /* TIEAWAY doesn't fit in the usual rounding mode encoding */
8209 rmode = FPROUNDING_TIEAWAY;
8211 case 0x56: /* FCVTXN, FCVTXN2 */
8213 unallocated_encoding(s);
8216 if (!fp_access_check(s)) {
8219 handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
8222 unallocated_encoding(s);
8227 unallocated_encoding(s);
8231 if (!fp_access_check(s)) {
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();
8240 TCGV_UNUSED_I32(tcg_rmode);
8241 TCGV_UNUSED_PTR(tcg_fpstatus);
8245 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
8246 TCGv_i64 tcg_rd = tcg_temp_new_i64();
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);
8253 TCGv_i32 tcg_rn = tcg_temp_new_i32();
8254 TCGv_i32 tcg_rd = tcg_temp_new_i32();
8256 read_vec_element_i32(s, tcg_rn, rn, 0, size);
8259 case 0x7: /* SQABS, SQNEG */
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 },
8267 genfn = fns[size][u];
8268 genfn(tcg_rd, cpu_env, tcg_rn);
8271 case 0x1a: /* FCVTNS */
8272 case 0x1b: /* FCVTMS */
8273 case 0x1c: /* FCVTAS */
8274 case 0x3a: /* FCVTPS */
8275 case 0x3b: /* FCVTZS */
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);
8282 case 0x5a: /* FCVTNU */
8283 case 0x5b: /* FCVTMU */
8284 case 0x5c: /* FCVTAU */
8285 case 0x7a: /* FCVTPU */
8286 case 0x7b: /* FCVTZU */
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);
8294 g_assert_not_reached();
8297 write_fp_sreg(s, rd, tcg_rd);
8298 tcg_temp_free_i32(tcg_rd);
8299 tcg_temp_free_i32(tcg_rn);
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);
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)
8313 int size = 32 - clz32(immh) - 1;
8314 int immhb = immh << 3 | immb;
8315 int shift = 2 * (8 << size) - immhb;
8316 bool accumulate = 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);
8328 if (extract32(immh, 3, 1) && !is_q) {
8329 unallocated_encoding(s);
8333 if (size > 3 && !is_q) {
8334 unallocated_encoding(s);
8338 if (!fp_access_check(s)) {
8343 case 0x02: /* SSRA / USRA (accumulate) */
8346 case 0x04: /* SRSHR / URSHR (rounding) */
8349 case 0x06: /* SRSRA / URSRA (accum + rounding) */
8350 accumulate = round = true;
8352 case 0x08: /* SRI */
8358 uint64_t round_const = 1ULL << (shift - 1);
8359 tcg_round = tcg_const_i64(round_const);
8361 TCGV_UNUSED_I64(tcg_round);
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);
8371 handle_shri_with_ins(tcg_rd, tcg_rn, size, shift);
8373 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8374 accumulate, is_u, size, shift);
8377 write_vec_element(s, tcg_rd, rd, i, size);
8381 clear_vec_high(s, rd);
8385 tcg_temp_free_i64(tcg_round);
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)
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);
8403 if (extract32(immh, 3, 1) && !is_q) {
8404 unallocated_encoding(s);
8408 if (size > 3 && !is_q) {
8409 unallocated_encoding(s);
8413 if (!fp_access_check(s)) {
8417 for (i = 0; i < elements; i++) {
8418 read_vec_element(s, tcg_rn, rn, i, size);
8420 read_vec_element(s, tcg_rd, rd, i, size);
8423 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
8425 write_vec_element(s, tcg_rd, rd, i, size);
8429 clear_vec_high(s, rd);
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)
8437 int size = 32 - clz32(immh) - 1;
8438 int immhb = immh << 3 | immb;
8439 int shift = immhb - (8 << size);
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);
8448 unallocated_encoding(s);
8452 if (!fp_access_check(s)) {
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.
8460 read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
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);
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)
8474 int immhb = immh << 3 | immb;
8475 int size = 32 - clz32(immh) - 1;
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;
8485 if (extract32(immh, 3, 1)) {
8486 unallocated_encoding(s);
8490 if (!fp_access_check(s)) {
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);
8500 uint64_t round_const = 1ULL << (shift - 1);
8501 tcg_round = tcg_const_i64(round_const);
8503 TCGV_UNUSED_I64(tcg_round);
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);
8511 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8515 clear_vec_high(s, rd);
8516 write_vec_element(s, tcg_final, rd, 0, MO_64);
8518 write_vec_element(s, tcg_final, rd, 1, MO_64);
8522 tcg_temp_free_i64(tcg_round);
8524 tcg_temp_free_i64(tcg_rn);
8525 tcg_temp_free_i64(tcg_rd);
8526 tcg_temp_free_i64(tcg_final);
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 * +---+---+---+-------------+------+------+--------+---+------+------+
8537 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
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);
8548 case 0x08: /* SRI */
8550 unallocated_encoding(s);
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);
8560 case 0x0a: /* SHL / SLI */
8561 handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
8563 case 0x10: /* SHRN */
8564 case 0x11: /* RSHRN / SQRSHRUN */
8566 handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
8569 handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
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,
8577 case 0x14: /* SSHLL / USHLL */
8578 handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
8580 case 0x1c: /* SCVTF / UCVTF */
8581 handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
8584 case 0xc: /* SQSHLU */
8586 unallocated_encoding(s);
8589 handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
8591 case 0xe: /* SQSHL, UQSHL */
8592 handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
8594 case 0x1f: /* FCVTZS/ FCVTZU */
8595 handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
8598 unallocated_encoding(s);
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.
8606 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
8607 TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
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 },
8614 NeonGenTwo64OpFn *genfn;
8617 genfn = fns[size][is_sub];
8618 genfn(tcg_res, tcg_op1, tcg_op2);
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)
8624 /* 3-reg-different widening insns: 64 x 64 -> 128 */
8625 TCGv_i64 tcg_res[2];
8628 tcg_res[0] = tcg_temp_new_i64();
8629 tcg_res[1] = tcg_temp_new_i64();
8631 /* Does this op do an adding accumulate, a subtracting accumulate,
8632 * or no accumulate at all?
8650 read_vec_element(s, tcg_res[0], rd, 0, MO_64);
8651 read_vec_element(s, tcg_res[1], rd, 1, MO_64);
8654 /* size == 2 means two 32x32->64 operations; this is worth special
8655 * casing because we can generally handle it inline.
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);
8664 int elt = pass + is_q * 2;
8666 read_vec_element(s, tcg_op1, rn, elt, memop);
8667 read_vec_element(s, tcg_op2, rm, elt, memop);
8670 tcg_passres = tcg_res[pass];
8672 tcg_passres = tcg_temp_new_i64();
8676 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8677 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
8679 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8680 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
8682 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8683 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8685 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
8686 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
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,
8692 tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
8693 tcg_temp_free_i64(tcg_tmp1);
8694 tcg_temp_free_i64(tcg_tmp2);
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);
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);
8710 g_assert_not_reached();
8713 if (opcode == 9 || opcode == 11) {
8714 /* saturating accumulate ops */
8716 tcg_gen_neg_i64(tcg_passres, tcg_passres);
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);
8727 tcg_temp_free_i64(tcg_passres);
8730 tcg_temp_free_i64(tcg_op1);
8731 tcg_temp_free_i64(tcg_op2);
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;
8741 read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
8742 read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
8745 tcg_passres = tcg_res[pass];
8747 tcg_passres = tcg_temp_new_i64();
8751 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8752 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
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 },
8759 NeonGenWidenFn *widenfn = widenfns[size][is_u];
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);
8768 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8769 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8772 gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
8774 gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
8778 gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
8780 gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
8784 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
8785 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
8786 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
8789 gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
8791 gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
8795 gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
8797 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
8801 case 9: /* SQDMLAL, SQDMLAL2 */
8802 case 11: /* SQDMLSL, SQDMLSL2 */
8803 case 13: /* SQDMULL, SQDMULL2 */
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);
8809 case 14: /* PMULL */
8811 gen_helper_neon_mull_p8(tcg_passres, tcg_op1, tcg_op2);
8814 g_assert_not_reached();
8816 tcg_temp_free_i32(tcg_op1);
8817 tcg_temp_free_i32(tcg_op2);
8820 if (opcode == 9 || opcode == 11) {
8821 /* saturating accumulate ops */
8823 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
8825 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
8829 gen_neon_addl(size, (accop < 0), tcg_res[pass],
8830 tcg_res[pass], tcg_passres);
8832 tcg_temp_free_i64(tcg_passres);
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]);
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)
8846 TCGv_i64 tcg_res[2];
8847 int part = is_q ? 2 : 0;
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 },
8859 NeonGenWidenFn *widenfn = widenfns[size][is_u];
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);
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]);
8878 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
8880 tcg_gen_addi_i64(in, in, 1U << 31);
8881 tcg_gen_extrh_i64_i32(res, in);
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)
8887 TCGv_i32 tcg_res[2];
8888 int part = is_q ? 2 : 0;
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 },
8902 NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
8904 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8905 read_vec_element(s, tcg_op2, rm, pass, MO_64);
8907 gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
8909 tcg_temp_free_i64(tcg_op1);
8910 tcg_temp_free_i64(tcg_op2);
8912 tcg_res[pass] = tcg_temp_new_i32();
8913 gennarrow(tcg_res[pass], tcg_wideres);
8914 tcg_temp_free_i64(tcg_wideres);
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]);
8922 clear_vec_high(s, rd);
8926 static void handle_pmull_64(DisasContext *s, int is_q, int rd, int rn, int rm)
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.
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();
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);
8945 tcg_temp_free_i64(tcg_op1);
8946 tcg_temp_free_i64(tcg_op2);
8947 tcg_temp_free_i64(tcg_res);
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 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
8956 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
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
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.
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);
8977 case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
8978 case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
8979 /* 64 x 128 -> 128 */
8981 unallocated_encoding(s);
8984 if (!fp_access_check(s)) {
8987 handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
8989 case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
8990 case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
8991 /* 128 x 128 -> 64 */
8993 unallocated_encoding(s);
8996 if (!fp_access_check(s)) {
8999 handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
9001 case 14: /* PMULL, PMULL2 */
9002 if (is_u || size == 1 || size == 2) {
9003 unallocated_encoding(s);
9007 if (!arm_dc_feature(s, ARM_FEATURE_V8_PMULL)) {
9008 unallocated_encoding(s);
9011 if (!fp_access_check(s)) {
9014 handle_pmull_64(s, is_q, rd, rn, rm);
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);
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 */
9035 unallocated_encoding(s);
9039 if (!fp_access_check(s)) {
9043 handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
9046 /* opcode 15 not allocated */
9047 unallocated_encoding(s);
9052 /* Logic op (opcode == 3) subgroup of C3.6.16. */
9053 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
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];
9064 if (!fp_access_check(s)) {
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();
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);
9080 tcg_gen_and_i64(tcg_res[pass], tcg_op1, tcg_op2);
9083 tcg_gen_andc_i64(tcg_res[pass], tcg_op1, tcg_op2);
9086 tcg_gen_or_i64(tcg_res[pass], tcg_op1, tcg_op2);
9089 tcg_gen_orc_i64(tcg_res[pass], tcg_op1, tcg_op2);
9094 /* B* ops need res loaded to operate on */
9095 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9100 tcg_gen_xor_i64(tcg_res[pass], tcg_op1, tcg_op2);
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);
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);
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);
9121 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
9123 tcg_gen_movi_i64(tcg_res[1], 0);
9125 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
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]);
9133 /* Helper functions for 32 bit comparisons */
9134 static void gen_max_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9136 tcg_gen_movcond_i32(TCG_COND_GE, res, op1, op2, op1, op2);
9139 static void gen_max_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9141 tcg_gen_movcond_i32(TCG_COND_GEU, res, op1, op2, op1, op2);
9144 static void gen_min_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9146 tcg_gen_movcond_i32(TCG_COND_LE, res, op1, op2, op1, op2);
9149 static void gen_min_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9151 tcg_gen_movcond_i32(TCG_COND_LEU, res, op1, op2, op1, op2);
9154 /* Pairwise op subgroup of C3.6.16.
9156 * This is called directly or via the handle_3same_float for float pairwise
9157 * operations where the opcode and size are calculated differently.
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)
9165 /* Floating point operations need fpst */
9166 if (opcode >= 0x58) {
9167 fpst = get_fpstatus_ptr();
9169 TCGV_UNUSED_PTR(fpst);
9172 if (!fp_access_check(s)) {
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.
9180 TCGv_i64 tcg_res[2];
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;
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();
9192 case 0x17: /* ADDP */
9193 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
9195 case 0x58: /* FMAXNMP */
9196 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9198 case 0x5a: /* FADDP */
9199 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9201 case 0x5e: /* FMAXP */
9202 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9204 case 0x78: /* FMINNMP */
9205 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9207 case 0x7e: /* FMINP */
9208 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9211 g_assert_not_reached();
9214 tcg_temp_free_i64(tcg_op1);
9215 tcg_temp_free_i64(tcg_op2);
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]);
9223 int maxpass = is_q ? 4 : 2;
9224 TCGv_i32 tcg_res[4];
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;
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();
9238 case 0x17: /* ADDP */
9240 static NeonGenTwoOpFn * const fns[3] = {
9241 gen_helper_neon_padd_u8,
9242 gen_helper_neon_padd_u16,
9248 case 0x14: /* SMAXP, UMAXP */
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 },
9255 genfn = fns[size][u];
9258 case 0x15: /* SMINP, UMINP */
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 },
9265 genfn = fns[size][u];
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);
9272 case 0x5a: /* FADDP */
9273 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9275 case 0x5e: /* FMAXP */
9276 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9278 case 0x78: /* FMINNMP */
9279 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9281 case 0x7e: /* FMINP */
9282 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9285 g_assert_not_reached();
9288 /* FP ops called directly, otherwise call now */
9290 genfn(tcg_res[pass], tcg_op1, tcg_op2);
9293 tcg_temp_free_i32(tcg_op1);
9294 tcg_temp_free_i32(tcg_op2);
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]);
9302 clear_vec_high(s, rd);
9306 if (!TCGV_IS_UNUSED_PTR(fpst)) {
9307 tcg_temp_free_ptr(fpst);
9311 /* Floating point op subgroup of C3.6.16. */
9312 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
9314 /* For floating point ops, the U, size[1] and opcode bits
9315 * together indicate the operation. size[0] indicates single
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);
9327 int datasize = is_q ? 128 : 64;
9328 int esize = 32 << size;
9329 int elements = datasize / esize;
9331 if (size == 1 && !is_q) {
9332 unallocated_encoding(s);
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);
9346 handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
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)) {
9372 handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
9375 unallocated_encoding(s);
9380 /* Integer op subgroup of C3.6.16. */
9381 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
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);
9393 case 0x13: /* MUL, PMUL */
9394 if (u && size != 0) {
9395 unallocated_encoding(s);
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 */
9408 unallocated_encoding(s);
9412 case 0x16: /* SQDMULH, SQRDMULH */
9413 if (size == 0 || size == 3) {
9414 unallocated_encoding(s);
9419 if (size == 3 && !is_q) {
9420 unallocated_encoding(s);
9426 if (!fp_access_check(s)) {
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();
9437 read_vec_element(s, tcg_op1, rn, pass, MO_64);
9438 read_vec_element(s, tcg_op2, rm, pass, MO_64);
9440 handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
9442 write_vec_element(s, tcg_res, rd, pass, MO_64);
9444 tcg_temp_free_i64(tcg_res);
9445 tcg_temp_free_i64(tcg_op1);
9446 tcg_temp_free_i64(tcg_op2);
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;
9456 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9457 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9460 case 0x0: /* SHADD, UHADD */
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 },
9467 genfn = fns[size][u];
9470 case 0x1: /* SQADD, UQADD */
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 },
9477 genenvfn = fns[size][u];
9480 case 0x2: /* SRHADD, URHADD */
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 },
9487 genfn = fns[size][u];
9490 case 0x4: /* SHSUB, UHSUB */
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 },
9497 genfn = fns[size][u];
9500 case 0x5: /* SQSUB, UQSUB */
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 },
9507 genenvfn = fns[size][u];
9510 case 0x6: /* CMGT, CMHI */
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 },
9517 genfn = fns[size][u];
9520 case 0x7: /* CMGE, CMHS */
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 },
9527 genfn = fns[size][u];
9530 case 0x8: /* SSHL, USHL */
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 },
9537 genfn = fns[size][u];
9540 case 0x9: /* SQSHL, UQSHL */
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 },
9547 genenvfn = fns[size][u];
9550 case 0xa: /* SRSHL, URSHL */
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 },
9557 genfn = fns[size][u];
9560 case 0xb: /* SQRSHL, UQRSHL */
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 },
9567 genenvfn = fns[size][u];
9570 case 0xc: /* SMAX, UMAX */
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 },
9577 genfn = fns[size][u];
9581 case 0xd: /* SMIN, UMIN */
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 },
9588 genfn = fns[size][u];
9591 case 0xe: /* SABD, UABD */
9592 case 0xf: /* SABA, UABA */
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 },
9599 genfn = fns[size][u];
9602 case 0x10: /* ADD, SUB */
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 },
9609 genfn = fns[size][u];
9612 case 0x11: /* CMTST, CMEQ */
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 },
9619 genfn = fns[size][u];
9622 case 0x13: /* MUL, PMUL */
9626 genfn = gen_helper_neon_mul_p8;
9629 /* fall through : MUL */
9630 case 0x12: /* MLA, MLS */
9632 static NeonGenTwoOpFn * const fns[3] = {
9633 gen_helper_neon_mul_u8,
9634 gen_helper_neon_mul_u16,
9640 case 0x16: /* SQDMULH, SQRDMULH */
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 },
9646 assert(size == 1 || size == 2);
9647 genenvfn = fns[size - 1][u];
9651 g_assert_not_reached();
9655 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
9657 genfn(tcg_res, tcg_op1, tcg_op2);
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 },
9667 bool is_sub = (opcode == 0x12 && u); /* MLS */
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);
9674 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9676 tcg_temp_free_i32(tcg_res);
9677 tcg_temp_free_i32(tcg_op1);
9678 tcg_temp_free_i32(tcg_op2);
9683 clear_vec_high(s, rd);
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 * +---+---+---+-----------+------+---+------+--------+---+------+------+
9693 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
9695 int opcode = extract32(insn, 11, 5);
9698 case 0x3: /* logic ops */
9699 disas_simd_3same_logic(s, insn);
9701 case 0x17: /* ADDP */
9702 case 0x14: /* SMAXP, UMAXP */
9703 case 0x15: /* SMINP, UMINP */
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);
9719 unallocated_encoding(s);
9723 handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
9727 /* floating point ops, sz[1] and U are part of opcode */
9728 disas_simd_3same_float(s, insn);
9731 disas_simd_3same_int(s, insn);
9736 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
9737 int size, int rn, int rd)
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.
9746 /* 32 -> 64 bit fp conversion */
9747 TCGv_i64 tcg_res[2];
9748 int srcelt = is_q ? 2 : 0;
9750 for (pass = 0; pass < 2; pass++) {
9751 TCGv_i32 tcg_op = tcg_temp_new_i32();
9752 tcg_res[pass] = tcg_temp_new_i64();
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);
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]);
9763 /* 16 -> 32 bit fp conversion */
9764 int srcelt = is_q ? 4 : 0;
9765 TCGv_i32 tcg_res[4];
9767 for (pass = 0; pass < 4; pass++) {
9768 tcg_res[pass] = tcg_temp_new_i32();
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],
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]);
9781 static void handle_rev(DisasContext *s, int opcode, bool u,
9782 bool is_q, int size, int rn, int rd)
9784 int op = (opcode << 1) | u;
9785 int opsz = op + size;
9786 int grp_size = 3 - opsz;
9787 int dsize = is_q ? 128 : 64;
9791 unallocated_encoding(s);
9795 if (!fp_access_check(s)) {
9800 /* Special case bytes, use bswap op on each group of elements */
9801 int groups = dsize / (8 << grp_size);
9803 for (i = 0; i < groups; i++) {
9804 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9806 read_vec_element(s, tcg_tmp, rn, i, grp_size);
9809 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
9812 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
9815 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
9818 g_assert_not_reached();
9820 write_vec_element(s, tcg_tmp, rd, i, grp_size);
9821 tcg_temp_free_i64(tcg_tmp);
9824 clear_vec_high(s, rd);
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);
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);
9839 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
9840 tcg_rn, off - 64, esize);
9842 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
9845 write_vec_element(s, tcg_rd, rd, 0, MO_64);
9846 write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
9848 tcg_temp_free_i64(tcg_rd_hi);
9849 tcg_temp_free_i64(tcg_rd);
9850 tcg_temp_free_i64(tcg_rn);
9854 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
9855 bool is_q, int size, int rn, int rd)
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).
9862 bool accum = (opcode == 0x6);
9863 int maxpass = is_q ? 2 : 1;
9865 TCGv_i64 tcg_res[2];
9868 /* 32 + 32 -> 64 op */
9869 TCGMemOp memop = size + (u ? 0 : MO_SIGN);
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();
9875 tcg_res[pass] = tcg_temp_new_i64();
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);
9881 read_vec_element(s, tcg_op1, rd, pass, MO_64);
9882 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
9885 tcg_temp_free_i64(tcg_op1);
9886 tcg_temp_free_i64(tcg_op2);
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 },
9897 genfn = fns[size][u];
9899 tcg_res[pass] = tcg_temp_new_i64();
9901 read_vec_element(s, tcg_op, rn, pass, MO_64);
9902 genfn(tcg_res[pass], tcg_op);
9905 read_vec_element(s, tcg_op, rd, pass, MO_64);
9907 gen_helper_neon_addl_u16(tcg_res[pass],
9908 tcg_res[pass], tcg_op);
9910 gen_helper_neon_addl_u32(tcg_res[pass],
9911 tcg_res[pass], tcg_op);
9914 tcg_temp_free_i64(tcg_op);
9918 tcg_res[1] = tcg_const_i64(0);
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]);
9926 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
9928 /* Implement SHLL and SHLL2 */
9930 int part = is_q ? 2 : 0;
9931 TCGv_i64 tcg_res[2];
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,
9939 NeonGenWidenFn *widenfn = widenfns[size];
9940 TCGv_i32 tcg_op = tcg_temp_new_i32();
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);
9947 tcg_temp_free_i32(tcg_op);
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]);
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 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
9962 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
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;
9974 TCGv_ptr tcg_fpstatus;
9977 case 0x0: /* REV64, REV32 */
9978 case 0x1: /* REV16 */
9979 handle_rev(s, opcode, u, is_q, size, rn, rd);
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. */
9986 } else if (u && size == 1) {
9989 } else if (!u && size == 0) {
9993 unallocated_encoding(s);
9995 case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
9996 case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
9998 unallocated_encoding(s);
10001 if (!fp_access_check(s)) {
10005 handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
10007 case 0x4: /* CLS, CLZ */
10009 unallocated_encoding(s);
10013 case 0x2: /* SADDLP, UADDLP */
10014 case 0x6: /* SADALP, UADALP */
10016 unallocated_encoding(s);
10019 if (!fp_access_check(s)) {
10022 handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
10024 case 0x13: /* SHLL, SHLL2 */
10025 if (u == 0 || size == 3) {
10026 unallocated_encoding(s);
10029 if (!fp_access_check(s)) {
10032 handle_shll(s, is_q, size, rn, rd);
10034 case 0xa: /* CMLT */
10036 unallocated_encoding(s);
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);
10048 case 0x3: /* SUQADD, USQADD */
10049 if (size == 3 && !is_q) {
10050 unallocated_encoding(s);
10053 if (!fp_access_check(s)) {
10056 handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
10058 case 0x7: /* SQABS, SQNEG */
10059 if (size == 3 && !is_q) {
10060 unallocated_encoding(s);
10065 case 0x16 ... 0x1d:
10068 /* Floating point: U, size[1] and opcode indicate operation;
10069 * size[0] indicates single or double precision.
10071 int is_double = extract32(size, 0, 1);
10072 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10073 size = is_double ? 3 : 2;
10075 case 0x2f: /* FABS */
10076 case 0x6f: /* FNEG */
10077 if (size == 3 && !is_q) {
10078 unallocated_encoding(s);
10082 case 0x1d: /* SCVTF */
10083 case 0x5d: /* UCVTF */
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);
10091 if (!fp_access_check(s)) {
10094 handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
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);
10106 handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
10108 case 0x7f: /* FSQRT */
10109 if (size == 3 && !is_q) {
10110 unallocated_encoding(s);
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;
10124 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10125 if (size == 3 && !is_q) {
10126 unallocated_encoding(s);
10130 case 0x5c: /* FCVTAU */
10131 case 0x1c: /* FCVTAS */
10132 need_fpstatus = true;
10134 rmode = FPROUNDING_TIEAWAY;
10135 if (size == 3 && !is_q) {
10136 unallocated_encoding(s);
10140 case 0x3c: /* URECPE */
10142 unallocated_encoding(s);
10146 case 0x3d: /* FRECPE */
10147 case 0x7d: /* FRSQRTE */
10148 if (size == 3 && !is_q) {
10149 unallocated_encoding(s);
10152 if (!fp_access_check(s)) {
10155 handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
10157 case 0x56: /* FCVTXN, FCVTXN2 */
10159 unallocated_encoding(s);
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.
10167 if (!fp_access_check(s)) {
10170 handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
10172 case 0x17: /* FCVTL, FCVTL2 */
10173 if (!fp_access_check(s)) {
10176 handle_2misc_widening(s, opcode, is_q, size, rn, rd);
10178 case 0x18: /* FRINTN */
10179 case 0x19: /* FRINTM */
10180 case 0x38: /* FRINTP */
10181 case 0x39: /* FRINTZ */
10183 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10185 case 0x59: /* FRINTX */
10186 case 0x79: /* FRINTI */
10187 need_fpstatus = true;
10188 if (size == 3 && !is_q) {
10189 unallocated_encoding(s);
10193 case 0x58: /* FRINTA */
10195 rmode = FPROUNDING_TIEAWAY;
10196 need_fpstatus = true;
10197 if (size == 3 && !is_q) {
10198 unallocated_encoding(s);
10202 case 0x7c: /* URSQRTE */
10204 unallocated_encoding(s);
10207 need_fpstatus = true;
10210 unallocated_encoding(s);
10216 unallocated_encoding(s);
10220 if (!fp_access_check(s)) {
10224 if (need_fpstatus) {
10225 tcg_fpstatus = get_fpstatus_ptr();
10227 TCGV_UNUSED_PTR(tcg_fpstatus);
10230 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
10231 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
10233 TCGV_UNUSED_I32(tcg_rmode);
10237 /* All 64-bit element operations can be shared with scalar 2misc */
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();
10244 read_vec_element(s, tcg_op, rn, pass, MO_64);
10246 handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
10247 tcg_rmode, tcg_fpstatus);
10249 write_vec_element(s, tcg_res, rd, pass, MO_64);
10251 tcg_temp_free_i64(tcg_res);
10252 tcg_temp_free_i64(tcg_op);
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();
10262 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
10265 /* Special cases for 32 bit elements */
10267 case 0xa: /* CMLT */
10268 /* 32 bit integer comparison against zero, result is
10269 * test ? (2^32 - 1) : 0. We implement via setcond(test)
10272 cond = TCG_COND_LT;
10274 tcg_gen_setcondi_i32(cond, tcg_res, tcg_op, 0);
10275 tcg_gen_neg_i32(tcg_res, tcg_res);
10277 case 0x8: /* CMGT, CMGE */
10278 cond = u ? TCG_COND_GE : TCG_COND_GT;
10280 case 0x9: /* CMEQ, CMLE */
10281 cond = u ? TCG_COND_LE : TCG_COND_EQ;
10283 case 0x4: /* CLS */
10285 tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
10287 tcg_gen_clrsb_i32(tcg_res, tcg_op);
10290 case 0x7: /* SQABS, SQNEG */
10292 gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
10294 gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
10297 case 0xb: /* ABS, NEG */
10299 tcg_gen_neg_i32(tcg_res, tcg_op);
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);
10308 case 0x2f: /* FABS */
10309 gen_helper_vfp_abss(tcg_res, tcg_op);
10311 case 0x6f: /* FNEG */
10312 gen_helper_vfp_negs(tcg_res, tcg_op);
10314 case 0x7f: /* FSQRT */
10315 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
10317 case 0x1a: /* FCVTNS */
10318 case 0x1b: /* FCVTMS */
10319 case 0x1c: /* FCVTAS */
10320 case 0x3a: /* FCVTPS */
10321 case 0x3b: /* FCVTZS */
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);
10329 case 0x5a: /* FCVTNU */
10330 case 0x5b: /* FCVTMU */
10331 case 0x5c: /* FCVTAU */
10332 case 0x7a: /* FCVTPU */
10333 case 0x7b: /* FCVTZU */
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);
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);
10349 case 0x59: /* FRINTX */
10350 gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
10352 case 0x7c: /* URSQRTE */
10353 gen_helper_rsqrte_u32(tcg_res, tcg_op, tcg_fpstatus);
10356 g_assert_not_reached();
10359 /* Use helpers for 8 and 16 bit elements */
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.
10366 gen_helper_neon_rbit_u8(tcg_res, tcg_op);
10368 gen_helper_neon_cnt_u8(tcg_res, tcg_op);
10371 case 0x7: /* SQABS, SQNEG */
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 },
10378 genfn = fns[size][u];
10379 genfn(tcg_res, cpu_env, tcg_op);
10382 case 0x8: /* CMGT, CMGE */
10383 case 0x9: /* CMEQ, CMLE */
10384 case 0xa: /* CMLT */
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 },
10391 NeonGenTwoOpFn *genfn;
10394 TCGv_i32 tcg_zero = tcg_const_i32(0);
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);
10403 genfn = fns[comp][size];
10405 genfn(tcg_res, tcg_zero, tcg_op);
10407 genfn(tcg_res, tcg_op, tcg_zero);
10409 tcg_temp_free_i32(tcg_zero);
10412 case 0xb: /* ABS, NEG */
10414 TCGv_i32 tcg_zero = tcg_const_i32(0);
10416 gen_helper_neon_sub_u16(tcg_res, tcg_zero, tcg_op);
10418 gen_helper_neon_sub_u8(tcg_res, tcg_zero, tcg_op);
10420 tcg_temp_free_i32(tcg_zero);
10423 gen_helper_neon_abs_s16(tcg_res, tcg_op);
10425 gen_helper_neon_abs_s8(tcg_res, tcg_op);
10429 case 0x4: /* CLS, CLZ */
10432 gen_helper_neon_clz_u8(tcg_res, tcg_op);
10434 gen_helper_neon_clz_u16(tcg_res, tcg_op);
10438 gen_helper_neon_cls_s8(tcg_res, tcg_op);
10440 gen_helper_neon_cls_s16(tcg_res, tcg_op);
10445 g_assert_not_reached();
10449 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10451 tcg_temp_free_i32(tcg_res);
10452 tcg_temp_free_i32(tcg_op);
10456 clear_vec_high(s, rd);
10460 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
10461 tcg_temp_free_i32(tcg_rmode);
10463 if (need_fpstatus) {
10464 tcg_temp_free_ptr(tcg_fpstatus);
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 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
10479 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
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).
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;
10506 case 0x0: /* MLA */
10507 case 0x4: /* MLS */
10508 if (!u || is_scalar) {
10509 unallocated_encoding(s);
10513 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10514 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10515 case 0xa: /* SMULL, SMULL2, UMULL, UMULL2 */
10517 unallocated_encoding(s);
10522 case 0x3: /* SQDMLAL, SQDMLAL2 */
10523 case 0x7: /* SQDMLSL, SQDMLSL2 */
10524 case 0xb: /* SQDMULL, SQDMULL2 */
10527 case 0xc: /* SQDMULH */
10528 case 0xd: /* SQRDMULH */
10530 unallocated_encoding(s);
10534 case 0x8: /* MUL */
10535 if (u || is_scalar) {
10536 unallocated_encoding(s);
10540 case 0x1: /* FMLA */
10541 case 0x5: /* FMLS */
10543 unallocated_encoding(s);
10547 case 0x9: /* FMUL, FMULX */
10548 if (!extract32(size, 1, 1)) {
10549 unallocated_encoding(s);
10555 unallocated_encoding(s);
10560 /* low bit of size indicates single/double */
10561 size = extract32(size, 0, 1) ? 3 : 2;
10563 index = h << 1 | l;
10566 unallocated_encoding(s);
10575 index = h << 2 | l << 1 | m;
10578 index = h << 1 | l;
10582 unallocated_encoding(s);
10587 if (!fp_access_check(s)) {
10592 fpst = get_fpstatus_ptr();
10594 TCGV_UNUSED_PTR(fpst);
10598 TCGv_i64 tcg_idx = tcg_temp_new_i64();
10601 assert(is_fp && is_q && !is_long);
10603 read_vec_element(s, tcg_idx, rm, index, MO_64);
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();
10609 read_vec_element(s, tcg_op, rn, pass, MO_64);
10612 case 0x5: /* FMLS */
10613 /* As usual for ARM, separate negation for fused multiply-add */
10614 gen_helper_vfp_negd(tcg_op, tcg_op);
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);
10620 case 0x9: /* FMUL, FMULX */
10622 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
10624 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
10628 g_assert_not_reached();
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);
10637 clear_vec_high(s, rd);
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.
10646 TCGv_i32 tcg_idx = tcg_temp_new_i32();
10647 int pass, maxpasses;
10652 maxpasses = is_q ? 4 : 2;
10655 read_vec_element_i32(s, tcg_idx, rm, index, size);
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.
10662 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
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();
10669 read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
10672 case 0x0: /* MLA */
10673 case 0x4: /* MLS */
10674 case 0x8: /* MUL */
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 },
10680 NeonGenTwoOpFn *genfn;
10681 bool is_sub = opcode == 0x4;
10684 gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
10686 tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
10688 if (opcode == 0x8) {
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);
10696 case 0x5: /* FMLS */
10697 /* As usual for ARM, separate negation for fused multiply-add */
10698 gen_helper_vfp_negs(tcg_op, tcg_op);
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);
10704 case 0x9: /* FMUL, FMULX */
10706 gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
10708 gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
10711 case 0xc: /* SQDMULH */
10713 gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
10716 gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
10720 case 0xd: /* SQRDMULH */
10722 gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
10725 gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
10730 g_assert_not_reached();
10734 write_fp_sreg(s, rd, tcg_res);
10736 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10739 tcg_temp_free_i32(tcg_op);
10740 tcg_temp_free_i32(tcg_res);
10743 tcg_temp_free_i32(tcg_idx);
10746 clear_vec_high(s, rd);
10749 /* long ops: 16x16->32 or 32x32->64 */
10750 TCGv_i64 tcg_res[2];
10752 bool satop = extract32(opcode, 0, 1);
10753 TCGMemOp memop = MO_32;
10760 TCGv_i64 tcg_idx = tcg_temp_new_i64();
10762 read_vec_element(s, tcg_idx, rm, index, memop);
10764 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10765 TCGv_i64 tcg_op = tcg_temp_new_i64();
10766 TCGv_i64 tcg_passres;
10772 passelt = pass + (is_q * 2);
10775 read_vec_element(s, tcg_op, rn, passelt, memop);
10777 tcg_res[pass] = tcg_temp_new_i64();
10779 if (opcode == 0xa || opcode == 0xb) {
10780 /* Non-accumulating ops */
10781 tcg_passres = tcg_res[pass];
10783 tcg_passres = tcg_temp_new_i64();
10786 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
10787 tcg_temp_free_i64(tcg_op);
10790 /* saturating, doubling */
10791 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10792 tcg_passres, tcg_passres);
10795 if (opcode == 0xa || opcode == 0xb) {
10799 /* Accumulating op: handle accumulate step */
10800 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10803 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10804 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10806 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10807 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10809 case 0x7: /* SQDMLSL, SQDMLSL2 */
10810 tcg_gen_neg_i64(tcg_passres, tcg_passres);
10812 case 0x3: /* SQDMLAL, SQDMLAL2 */
10813 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10818 g_assert_not_reached();
10820 tcg_temp_free_i64(tcg_passres);
10822 tcg_temp_free_i64(tcg_idx);
10825 clear_vec_high(s, rd);
10828 TCGv_i32 tcg_idx = tcg_temp_new_i32();
10831 read_vec_element_i32(s, tcg_idx, rm, index, size);
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.
10838 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
10841 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10842 TCGv_i32 tcg_op = tcg_temp_new_i32();
10843 TCGv_i64 tcg_passres;
10846 read_vec_element_i32(s, tcg_op, rn, pass, size);
10848 read_vec_element_i32(s, tcg_op, rn,
10849 pass + (is_q * 2), MO_32);
10852 tcg_res[pass] = tcg_temp_new_i64();
10854 if (opcode == 0xa || opcode == 0xb) {
10855 /* Non-accumulating ops */
10856 tcg_passres = tcg_res[pass];
10858 tcg_passres = tcg_temp_new_i64();
10861 if (memop & MO_SIGN) {
10862 gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
10864 gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
10867 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10868 tcg_passres, tcg_passres);
10870 tcg_temp_free_i32(tcg_op);
10872 if (opcode == 0xa || opcode == 0xb) {
10876 /* Accumulating op: handle accumulate step */
10877 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10880 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10881 gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
10884 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10885 gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
10888 case 0x7: /* SQDMLSL, SQDMLSL2 */
10889 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10891 case 0x3: /* SQDMLAL, SQDMLAL2 */
10892 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10897 g_assert_not_reached();
10899 tcg_temp_free_i64(tcg_passres);
10901 tcg_temp_free_i32(tcg_idx);
10904 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
10909 tcg_res[1] = tcg_const_i64(0);
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]);
10918 if (!TCGV_IS_UNUSED_PTR(fpst)) {
10919 tcg_temp_free_ptr(fpst);
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 * +-----------------+------+-----------+--------+-----+------+------+
10929 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
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);
10936 TCGv_i32 tcg_rd_regno, tcg_rn_regno, tcg_decrypt;
10937 CryptoThreeOpEnvFn *genfn;
10939 if (!arm_dc_feature(s, ARM_FEATURE_V8_AES)
10941 unallocated_encoding(s);
10946 case 0x4: /* AESE */
10948 genfn = gen_helper_crypto_aese;
10950 case 0x6: /* AESMC */
10952 genfn = gen_helper_crypto_aesmc;
10954 case 0x5: /* AESD */
10956 genfn = gen_helper_crypto_aese;
10958 case 0x7: /* AESIMC */
10960 genfn = gen_helper_crypto_aesmc;
10963 unallocated_encoding(s);
10967 if (!fp_access_check(s)) {
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.
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);
10979 genfn(cpu_env, tcg_rd_regno, tcg_rn_regno, tcg_decrypt);
10981 tcg_temp_free_i32(tcg_rd_regno);
10982 tcg_temp_free_i32(tcg_rn_regno);
10983 tcg_temp_free_i32(tcg_decrypt);
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 * +-----------------+------+---+------+---+--------+-----+------+------+
10992 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
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;
11004 unallocated_encoding(s);
11009 case 0: /* SHA1C */
11010 case 1: /* SHA1P */
11011 case 2: /* SHA1M */
11012 case 3: /* SHA1SU0 */
11014 feature = ARM_FEATURE_V8_SHA1;
11016 case 4: /* SHA256H */
11017 genfn = gen_helper_crypto_sha256h;
11019 case 5: /* SHA256H2 */
11020 genfn = gen_helper_crypto_sha256h2;
11022 case 6: /* SHA256SU1 */
11023 genfn = gen_helper_crypto_sha256su1;
11026 unallocated_encoding(s);
11030 if (!arm_dc_feature(s, feature)) {
11031 unallocated_encoding(s);
11035 if (!fp_access_check(s)) {
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);
11044 genfn(cpu_env, tcg_rd_regno, tcg_rn_regno, tcg_rm_regno);
11046 TCGv_i32 tcg_opcode = tcg_const_i32(opcode);
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);
11053 tcg_temp_free_i32(tcg_rd_regno);
11054 tcg_temp_free_i32(tcg_rn_regno);
11055 tcg_temp_free_i32(tcg_rm_regno);
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 * +-----------------+------+-----------+--------+-----+------+------+
11064 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
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;
11072 TCGv_i32 tcg_rd_regno, tcg_rn_regno;
11075 unallocated_encoding(s);
11080 case 0: /* SHA1H */
11081 feature = ARM_FEATURE_V8_SHA1;
11082 genfn = gen_helper_crypto_sha1h;
11084 case 1: /* SHA1SU1 */
11085 feature = ARM_FEATURE_V8_SHA1;
11086 genfn = gen_helper_crypto_sha1su1;
11088 case 2: /* SHA256SU0 */
11089 feature = ARM_FEATURE_V8_SHA256;
11090 genfn = gen_helper_crypto_sha256su0;
11093 unallocated_encoding(s);
11097 if (!arm_dc_feature(s, feature)) {
11098 unallocated_encoding(s);
11102 if (!fp_access_check(s)) {
11106 tcg_rd_regno = tcg_const_i32(rd << 1);
11107 tcg_rn_regno = tcg_const_i32(rn << 1);
11109 genfn(cpu_env, tcg_rd_regno, tcg_rn_regno);
11111 tcg_temp_free_i32(tcg_rd_regno);
11112 tcg_temp_free_i32(tcg_rn_regno);
11115 /* C3.6 Data processing - SIMD, inc Crypto
11117 * As the decode gets a little complex we are using a table based
11118 * approach for this part of the decode.
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 }
11147 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
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.
11153 AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
11157 unallocated_encoding(s);
11161 /* C3.6 Data processing - SIMD and floating point */
11162 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
11164 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
11165 disas_data_proc_fp(s, insn);
11167 /* SIMD, including crypto */
11168 disas_data_proc_simd(s, insn);
11172 /* C3.1 A64 instruction index by encoding */
11173 static void disas_a64_insn(CPUARMState *env, DisasContext *s)
11177 insn = arm_ldl_code(env, s->pc, s->sctlr_b);
11181 s->fp_access_checked = false;
11183 switch (extract32(insn, 25, 4)) {
11184 case 0x0: case 0x1: case 0x2: case 0x3: /* UNALLOCATED */
11185 unallocated_encoding(s);
11187 case 0x8: case 0x9: /* Data processing - immediate */
11188 disas_data_proc_imm(s, insn);
11190 case 0xa: case 0xb: /* Branch, exception generation and system insns */
11191 disas_b_exc_sys(s, insn);
11196 case 0xe: /* Loads and stores */
11197 disas_ldst(s, insn);
11200 case 0xd: /* Data processing - register */
11201 disas_data_proc_reg(s, insn);
11204 case 0xf: /* Data processing - SIMD and floating point */
11205 disas_data_proc_simd_fp(s, insn);
11208 assert(FALSE); /* all 15 cases should be handled above */
11212 /* if we allocated any temporaries, free them here */
11216 static int aarch64_tr_init_disas_context(DisasContextBase *dcbase,
11217 CPUState *cpu, int max_insns)
11219 DisasContext *dc = container_of(dcbase, DisasContext, base);
11220 CPUARMState *env = cpu->env_ptr;
11221 ARMCPU *arm_cpu = arm_env_get_cpu(env);
11224 dc->pc = dc->base.pc_first;
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.
11231 dc->secure_routed_to_el3 = arm_feature(env, ARM_FEATURE_EL3) &&
11232 !arm_el_is_aa64(env, 3);
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);
11245 dc->fp_excp_el = ARM_TBFLAG_FPEXC_EL(dc->base.tb->flags);
11247 dc->vec_stride = 0;
11248 dc->cp_regs = arm_cpu->cp_regs;
11249 dc->features = env->features;
11251 /* Single step state. The code-generation logic here is:
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
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
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);
11271 /* Bound the number of insns to execute to those left on the page. */
11272 bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
11274 /* If architectural single step active, limit to 1. */
11275 if (dc->ss_active) {
11278 max_insns = MIN(max_insns, bound);
11280 init_tmp_a64_array(dc);
11285 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
11287 tcg_clear_temp_count();
11290 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
11292 DisasContext *dc = container_of(dcbase, DisasContext, base);
11294 dc->insn_start_idx = tcg_op_buf_count();
11295 tcg_gen_insn_start(dc->pc, 0, 0);
11298 static bool aarch64_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,
11299 const CPUBreakpoint *bp)
11301 DisasContext *dc = container_of(dcbase, DisasContext, base);
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;
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. */
11316 dc->base.is_jmp = DISAS_NORETURN;
11322 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
11324 DisasContext *dc = container_of(dcbase, DisasContext, base);
11325 CPUARMState *env = cpu->env_ptr;
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.
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;
11343 disas_a64_insn(env, dc);
11346 dc->base.pc_next = dc->pc;
11347 translator_loop_temp_check(&dc->base);
11350 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
11352 DisasContext *dc = container_of(dcbase, DisasContext, base);
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).
11360 switch (dc->base.is_jmp) {
11362 gen_a64_set_pc_im(dc->pc);
11366 if (dc->base.singlestep_enabled) {
11367 gen_exception_internal(EXCP_DEBUG);
11369 gen_step_complete_exception(dc);
11372 case DISAS_NORETURN:
11376 switch (dc->base.is_jmp) {
11378 case DISAS_TOO_MANY:
11379 gen_goto_tb(dc, 1, dc->pc);
11383 gen_a64_set_pc_im(dc->pc);
11386 tcg_gen_lookup_and_goto_ptr();
11389 tcg_gen_exit_tb(0);
11391 case DISAS_NORETURN:
11395 gen_a64_set_pc_im(dc->pc);
11396 gen_helper_wfe(cpu_env);
11399 gen_a64_set_pc_im(dc->pc);
11400 gen_helper_yield(cpu_env);
11404 /* This is a special case because we don't want to just halt the CPU
11405 * if trying to debug across a WFI.
11407 TCGv_i32 tmp = tcg_const_i32(4);
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.
11415 tcg_gen_exit_tb(0);
11421 /* Functions above can change dc->pc, so re-align db->pc_next */
11422 dc->base.pc_next = dc->pc;
11425 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
11428 DisasContext *dc = container_of(dcbase, DisasContext, base);
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);
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,