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"
24 #include "tcg-op-gvec.h"
27 #include "translate.h"
28 #include "internals.h"
29 #include "qemu/host-utils.h"
31 #include "exec/semihost.h"
32 #include "exec/gen-icount.h"
34 #include "exec/helper-proto.h"
35 #include "exec/helper-gen.h"
38 #include "trace-tcg.h"
40 static TCGv_i64 cpu_X[32];
41 static TCGv_i64 cpu_pc;
43 /* Load/store exclusive handling */
44 static TCGv_i64 cpu_exclusive_high;
45 static TCGv_i64 cpu_reg(DisasContext *s, int reg);
47 static const char *regnames[] = {
48 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
49 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
50 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
51 "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
55 A64_SHIFT_TYPE_LSL = 0,
56 A64_SHIFT_TYPE_LSR = 1,
57 A64_SHIFT_TYPE_ASR = 2,
58 A64_SHIFT_TYPE_ROR = 3
61 /* Table based decoder typedefs - used when the relevant bits for decode
62 * are too awkwardly scattered across the instruction (eg SIMD).
64 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
66 typedef struct AArch64DecodeTable {
69 AArch64DecodeFn *disas_fn;
72 /* Function prototype for gen_ functions for calling Neon helpers */
73 typedef void NeonGenOneOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32);
74 typedef void NeonGenTwoOpFn(TCGv_i32, TCGv_i32, TCGv_i32);
75 typedef void NeonGenTwoOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32, TCGv_i32);
76 typedef void NeonGenTwo64OpFn(TCGv_i64, TCGv_i64, TCGv_i64);
77 typedef void NeonGenTwo64OpEnvFn(TCGv_i64, TCGv_ptr, TCGv_i64, TCGv_i64);
78 typedef void NeonGenNarrowFn(TCGv_i32, TCGv_i64);
79 typedef void NeonGenNarrowEnvFn(TCGv_i32, TCGv_ptr, TCGv_i64);
80 typedef void NeonGenWidenFn(TCGv_i64, TCGv_i32);
81 typedef void NeonGenTwoSingleOPFn(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
82 typedef void NeonGenTwoDoubleOPFn(TCGv_i64, TCGv_i64, TCGv_i64, TCGv_ptr);
83 typedef void NeonGenOneOpFn(TCGv_i64, TCGv_i64);
84 typedef void CryptoTwoOpFn(TCGv_ptr, TCGv_ptr);
85 typedef void CryptoThreeOpIntFn(TCGv_ptr, TCGv_ptr, TCGv_i32);
86 typedef void CryptoThreeOpFn(TCGv_ptr, TCGv_ptr, TCGv_ptr);
88 /* Note that the gvec expanders operate on offsets + sizes. */
89 typedef void GVecGen2Fn(unsigned, uint32_t, uint32_t, uint32_t, uint32_t);
90 typedef void GVecGen2iFn(unsigned, uint32_t, uint32_t, int64_t,
92 typedef void GVecGen3Fn(unsigned, uint32_t, uint32_t,
93 uint32_t, uint32_t, uint32_t);
95 /* initialize TCG globals. */
96 void a64_translate_init(void)
100 cpu_pc = tcg_global_mem_new_i64(cpu_env,
101 offsetof(CPUARMState, pc),
103 for (i = 0; i < 32; i++) {
104 cpu_X[i] = tcg_global_mem_new_i64(cpu_env,
105 offsetof(CPUARMState, xregs[i]),
109 cpu_exclusive_high = tcg_global_mem_new_i64(cpu_env,
110 offsetof(CPUARMState, exclusive_high), "exclusive_high");
113 static inline int get_a64_user_mem_index(DisasContext *s)
115 /* Return the core mmu_idx to use for A64 "unprivileged load/store" insns:
116 * if EL1, access as if EL0; otherwise access at current EL
120 switch (s->mmu_idx) {
121 case ARMMMUIdx_S12NSE1:
122 useridx = ARMMMUIdx_S12NSE0;
124 case ARMMMUIdx_S1SE1:
125 useridx = ARMMMUIdx_S1SE0;
128 g_assert_not_reached();
130 useridx = s->mmu_idx;
133 return arm_to_core_mmu_idx(useridx);
136 void aarch64_cpu_dump_state(CPUState *cs, FILE *f,
137 fprintf_function cpu_fprintf, int flags)
139 ARMCPU *cpu = ARM_CPU(cs);
140 CPUARMState *env = &cpu->env;
141 uint32_t psr = pstate_read(env);
143 int el = arm_current_el(env);
144 const char *ns_status;
146 cpu_fprintf(f, "PC=%016"PRIx64" SP=%016"PRIx64"\n",
147 env->pc, env->xregs[31]);
148 for (i = 0; i < 31; i++) {
149 cpu_fprintf(f, "X%02d=%016"PRIx64, i, env->xregs[i]);
151 cpu_fprintf(f, "\n");
157 if (arm_feature(env, ARM_FEATURE_EL3) && el != 3) {
158 ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
163 cpu_fprintf(f, "\nPSTATE=%08x %c%c%c%c %sEL%d%c\n",
165 psr & PSTATE_N ? 'N' : '-',
166 psr & PSTATE_Z ? 'Z' : '-',
167 psr & PSTATE_C ? 'C' : '-',
168 psr & PSTATE_V ? 'V' : '-',
171 psr & PSTATE_SP ? 'h' : 't');
173 if (flags & CPU_DUMP_FPU) {
175 for (i = 0; i < numvfpregs; i++) {
176 uint64_t *q = aa64_vfp_qreg(env, i);
179 cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 "%c",
180 i, vhi, vlo, (i & 1 ? '\n' : ' '));
182 cpu_fprintf(f, "FPCR: %08x FPSR: %08x\n",
183 vfp_get_fpcr(env), vfp_get_fpsr(env));
187 void gen_a64_set_pc_im(uint64_t val)
189 tcg_gen_movi_i64(cpu_pc, val);
192 /* Load the PC from a generic TCG variable.
194 * If address tagging is enabled via the TCR TBI bits, then loading
195 * an address into the PC will clear out any tag in the it:
196 * + for EL2 and EL3 there is only one TBI bit, and if it is set
197 * then the address is zero-extended, clearing bits [63:56]
198 * + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
199 * and TBI1 controls addressses with bit 55 == 1.
200 * If the appropriate TBI bit is set for the address then
201 * the address is sign-extended from bit 55 into bits [63:56]
203 * We can avoid doing this for relative-branches, because the
204 * PC + offset can never overflow into the tag bits (assuming
205 * that virtual addresses are less than 56 bits wide, as they
206 * are currently), but we must handle it for branch-to-register.
208 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
211 if (s->current_el <= 1) {
212 /* Test if NEITHER or BOTH TBI values are set. If so, no need to
213 * examine bit 55 of address, can just generate code.
214 * If mixed, then test via generated code
216 if (s->tbi0 && s->tbi1) {
217 TCGv_i64 tmp_reg = tcg_temp_new_i64();
218 /* Both bits set, sign extension from bit 55 into [63:56] will
221 tcg_gen_shli_i64(tmp_reg, src, 8);
222 tcg_gen_sari_i64(cpu_pc, tmp_reg, 8);
223 tcg_temp_free_i64(tmp_reg);
224 } else if (!s->tbi0 && !s->tbi1) {
225 /* Neither bit set, just load it as-is */
226 tcg_gen_mov_i64(cpu_pc, src);
228 TCGv_i64 tcg_tmpval = tcg_temp_new_i64();
229 TCGv_i64 tcg_bit55 = tcg_temp_new_i64();
230 TCGv_i64 tcg_zero = tcg_const_i64(0);
232 tcg_gen_andi_i64(tcg_bit55, src, (1ull << 55));
235 /* tbi0==1, tbi1==0, so 0-fill upper byte if bit 55 = 0 */
236 tcg_gen_andi_i64(tcg_tmpval, src,
237 0x00FFFFFFFFFFFFFFull);
238 tcg_gen_movcond_i64(TCG_COND_EQ, cpu_pc, tcg_bit55, tcg_zero,
241 /* tbi0==0, tbi1==1, so 1-fill upper byte if bit 55 = 1 */
242 tcg_gen_ori_i64(tcg_tmpval, src,
243 0xFF00000000000000ull);
244 tcg_gen_movcond_i64(TCG_COND_NE, cpu_pc, tcg_bit55, tcg_zero,
247 tcg_temp_free_i64(tcg_zero);
248 tcg_temp_free_i64(tcg_bit55);
249 tcg_temp_free_i64(tcg_tmpval);
251 } else { /* EL > 1 */
253 /* Force tag byte to all zero */
254 tcg_gen_andi_i64(cpu_pc, src, 0x00FFFFFFFFFFFFFFull);
256 /* Load unmodified address */
257 tcg_gen_mov_i64(cpu_pc, src);
262 typedef struct DisasCompare64 {
267 static void a64_test_cc(DisasCompare64 *c64, int cc)
271 arm_test_cc(&c32, cc);
273 /* Sign-extend the 32-bit value so that the GE/LT comparisons work
274 * properly. The NE/EQ comparisons are also fine with this choice. */
275 c64->cond = c32.cond;
276 c64->value = tcg_temp_new_i64();
277 tcg_gen_ext_i32_i64(c64->value, c32.value);
282 static void a64_free_cc(DisasCompare64 *c64)
284 tcg_temp_free_i64(c64->value);
287 static void gen_exception_internal(int excp)
289 TCGv_i32 tcg_excp = tcg_const_i32(excp);
291 assert(excp_is_internal(excp));
292 gen_helper_exception_internal(cpu_env, tcg_excp);
293 tcg_temp_free_i32(tcg_excp);
296 static void gen_exception(int excp, uint32_t syndrome, uint32_t target_el)
298 TCGv_i32 tcg_excp = tcg_const_i32(excp);
299 TCGv_i32 tcg_syn = tcg_const_i32(syndrome);
300 TCGv_i32 tcg_el = tcg_const_i32(target_el);
302 gen_helper_exception_with_syndrome(cpu_env, tcg_excp,
304 tcg_temp_free_i32(tcg_el);
305 tcg_temp_free_i32(tcg_syn);
306 tcg_temp_free_i32(tcg_excp);
309 static void gen_exception_internal_insn(DisasContext *s, int offset, int excp)
311 gen_a64_set_pc_im(s->pc - offset);
312 gen_exception_internal(excp);
313 s->base.is_jmp = DISAS_NORETURN;
316 static void gen_exception_insn(DisasContext *s, int offset, int excp,
317 uint32_t syndrome, uint32_t target_el)
319 gen_a64_set_pc_im(s->pc - offset);
320 gen_exception(excp, syndrome, target_el);
321 s->base.is_jmp = DISAS_NORETURN;
324 static void gen_ss_advance(DisasContext *s)
326 /* If the singlestep state is Active-not-pending, advance to
331 gen_helper_clear_pstate_ss(cpu_env);
335 static void gen_step_complete_exception(DisasContext *s)
337 /* We just completed step of an insn. Move from Active-not-pending
338 * to Active-pending, and then also take the swstep exception.
339 * This corresponds to making the (IMPDEF) choice to prioritize
340 * swstep exceptions over asynchronous exceptions taken to an exception
341 * level where debug is disabled. This choice has the advantage that
342 * we do not need to maintain internal state corresponding to the
343 * ISV/EX syndrome bits between completion of the step and generation
344 * of the exception, and our syndrome information is always correct.
347 gen_exception(EXCP_UDEF, syn_swstep(s->ss_same_el, 1, s->is_ldex),
348 default_exception_el(s));
349 s->base.is_jmp = DISAS_NORETURN;
352 static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
354 /* No direct tb linking with singlestep (either QEMU's or the ARM
355 * debug architecture kind) or deterministic io
357 if (s->base.singlestep_enabled || s->ss_active ||
358 (tb_cflags(s->base.tb) & CF_LAST_IO)) {
362 #ifndef CONFIG_USER_ONLY
363 /* Only link tbs from inside the same guest page */
364 if ((s->base.tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
372 static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
374 TranslationBlock *tb;
377 if (use_goto_tb(s, n, dest)) {
379 gen_a64_set_pc_im(dest);
380 tcg_gen_exit_tb((intptr_t)tb + n);
381 s->base.is_jmp = DISAS_NORETURN;
383 gen_a64_set_pc_im(dest);
385 gen_step_complete_exception(s);
386 } else if (s->base.singlestep_enabled) {
387 gen_exception_internal(EXCP_DEBUG);
389 tcg_gen_lookup_and_goto_ptr();
390 s->base.is_jmp = DISAS_NORETURN;
395 static void unallocated_encoding(DisasContext *s)
397 /* Unallocated and reserved encodings are uncategorized */
398 gen_exception_insn(s, 4, EXCP_UDEF, syn_uncategorized(),
399 default_exception_el(s));
402 #define unsupported_encoding(s, insn) \
404 qemu_log_mask(LOG_UNIMP, \
405 "%s:%d: unsupported instruction encoding 0x%08x " \
406 "at pc=%016" PRIx64 "\n", \
407 __FILE__, __LINE__, insn, s->pc - 4); \
408 unallocated_encoding(s); \
411 static void init_tmp_a64_array(DisasContext *s)
413 #ifdef CONFIG_DEBUG_TCG
414 memset(s->tmp_a64, 0, sizeof(s->tmp_a64));
416 s->tmp_a64_count = 0;
419 static void free_tmp_a64(DisasContext *s)
422 for (i = 0; i < s->tmp_a64_count; i++) {
423 tcg_temp_free_i64(s->tmp_a64[i]);
425 init_tmp_a64_array(s);
428 static TCGv_i64 new_tmp_a64(DisasContext *s)
430 assert(s->tmp_a64_count < TMP_A64_MAX);
431 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
434 static TCGv_i64 new_tmp_a64_zero(DisasContext *s)
436 TCGv_i64 t = new_tmp_a64(s);
437 tcg_gen_movi_i64(t, 0);
442 * Register access functions
444 * These functions are used for directly accessing a register in where
445 * changes to the final register value are likely to be made. If you
446 * need to use a register for temporary calculation (e.g. index type
447 * operations) use the read_* form.
449 * B1.2.1 Register mappings
451 * In instruction register encoding 31 can refer to ZR (zero register) or
452 * the SP (stack pointer) depending on context. In QEMU's case we map SP
453 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
454 * This is the point of the _sp forms.
456 static TCGv_i64 cpu_reg(DisasContext *s, int reg)
459 return new_tmp_a64_zero(s);
465 /* register access for when 31 == SP */
466 static TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
471 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
472 * representing the register contents. This TCGv is an auto-freed
473 * temporary so it need not be explicitly freed, and may be modified.
475 static TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
477 TCGv_i64 v = new_tmp_a64(s);
480 tcg_gen_mov_i64(v, cpu_X[reg]);
482 tcg_gen_ext32u_i64(v, cpu_X[reg]);
485 tcg_gen_movi_i64(v, 0);
490 static TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
492 TCGv_i64 v = new_tmp_a64(s);
494 tcg_gen_mov_i64(v, cpu_X[reg]);
496 tcg_gen_ext32u_i64(v, cpu_X[reg]);
501 /* We should have at some point before trying to access an FP register
502 * done the necessary access check, so assert that
503 * (a) we did the check and
504 * (b) we didn't then just plough ahead anyway if it failed.
505 * Print the instruction pattern in the abort message so we can figure
506 * out what we need to fix if a user encounters this problem in the wild.
508 static inline void assert_fp_access_checked(DisasContext *s)
510 #ifdef CONFIG_DEBUG_TCG
511 if (unlikely(!s->fp_access_checked || s->fp_excp_el)) {
512 fprintf(stderr, "target-arm: FP access check missing for "
513 "instruction 0x%08x\n", s->insn);
519 /* Return the offset into CPUARMState of an element of specified
520 * size, 'element' places in from the least significant end of
521 * the FP/vector register Qn.
523 static inline int vec_reg_offset(DisasContext *s, int regno,
524 int element, TCGMemOp size)
527 #ifdef HOST_WORDS_BIGENDIAN
528 /* This is complicated slightly because vfp.zregs[n].d[0] is
529 * still the low half and vfp.zregs[n].d[1] the high half
530 * of the 128 bit vector, even on big endian systems.
531 * Calculate the offset assuming a fully bigendian 128 bits,
532 * then XOR to account for the order of the two 64 bit halves.
534 offs += (16 - ((element + 1) * (1 << size)));
537 offs += element * (1 << size);
539 offs += offsetof(CPUARMState, vfp.zregs[regno]);
540 assert_fp_access_checked(s);
544 /* Return the offset info CPUARMState of the "whole" vector register Qn. */
545 static inline int vec_full_reg_offset(DisasContext *s, int regno)
547 assert_fp_access_checked(s);
548 return offsetof(CPUARMState, vfp.zregs[regno]);
551 /* Return a newly allocated pointer to the vector register. */
552 static TCGv_ptr vec_full_reg_ptr(DisasContext *s, int regno)
554 TCGv_ptr ret = tcg_temp_new_ptr();
555 tcg_gen_addi_ptr(ret, cpu_env, vec_full_reg_offset(s, regno));
559 /* Return the byte size of the "whole" vector register, VL / 8. */
560 static inline int vec_full_reg_size(DisasContext *s)
562 /* FIXME SVE: We should put the composite ZCR_EL* value into tb->flags.
563 In the meantime this is just the AdvSIMD length of 128. */
567 /* Return the offset into CPUARMState of a slice (from
568 * the least significant end) of FP register Qn (ie
570 * (Note that this is not the same mapping as for A32; see cpu.h)
572 static inline int fp_reg_offset(DisasContext *s, int regno, TCGMemOp size)
574 return vec_reg_offset(s, regno, 0, size);
577 /* Offset of the high half of the 128 bit vector Qn */
578 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
580 return vec_reg_offset(s, regno, 1, MO_64);
583 /* Convenience accessors for reading and writing single and double
584 * FP registers. Writing clears the upper parts of the associated
585 * 128 bit vector register, as required by the architecture.
586 * Note that unlike the GP register accessors, the values returned
587 * by the read functions must be manually freed.
589 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
591 TCGv_i64 v = tcg_temp_new_i64();
593 tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
597 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
599 TCGv_i32 v = tcg_temp_new_i32();
601 tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
605 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
606 * If SVE is not enabled, then there are only 128 bits in the vector.
608 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
610 unsigned ofs = fp_reg_offset(s, rd, MO_64);
611 unsigned vsz = vec_full_reg_size(s);
614 TCGv_i64 tcg_zero = tcg_const_i64(0);
615 tcg_gen_st_i64(tcg_zero, cpu_env, ofs + 8);
616 tcg_temp_free_i64(tcg_zero);
619 tcg_gen_gvec_dup8i(ofs + 16, vsz - 16, vsz - 16, 0);
623 static void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
625 unsigned ofs = fp_reg_offset(s, reg, MO_64);
627 tcg_gen_st_i64(v, cpu_env, ofs);
628 clear_vec_high(s, false, reg);
631 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
633 TCGv_i64 tmp = tcg_temp_new_i64();
635 tcg_gen_extu_i32_i64(tmp, v);
636 write_fp_dreg(s, reg, tmp);
637 tcg_temp_free_i64(tmp);
640 static TCGv_ptr get_fpstatus_ptr(bool is_f16)
642 TCGv_ptr statusptr = tcg_temp_new_ptr();
645 /* In A64 all instructions (both FP and Neon) use the FPCR; there
646 * is no equivalent of the A32 Neon "standard FPSCR value".
647 * However half-precision operations operate under a different
648 * FZ16 flag and use vfp.fp_status_f16 instead of vfp.fp_status.
651 offset = offsetof(CPUARMState, vfp.fp_status_f16);
653 offset = offsetof(CPUARMState, vfp.fp_status);
655 tcg_gen_addi_ptr(statusptr, cpu_env, offset);
659 /* Expand a 2-operand AdvSIMD vector operation using an expander function. */
660 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
661 GVecGen2Fn *gvec_fn, int vece)
663 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
664 is_q ? 16 : 8, vec_full_reg_size(s));
667 /* Expand a 2-operand + immediate AdvSIMD vector operation using
668 * an expander function.
670 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
671 int64_t imm, GVecGen2iFn *gvec_fn, int vece)
673 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
674 imm, is_q ? 16 : 8, vec_full_reg_size(s));
677 /* Expand a 3-operand AdvSIMD vector operation using an expander function. */
678 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
679 GVecGen3Fn *gvec_fn, int vece)
681 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
682 vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
685 /* Expand a 2-operand + immediate AdvSIMD vector operation using
688 static void gen_gvec_op2i(DisasContext *s, bool is_q, int rd,
689 int rn, int64_t imm, const GVecGen2i *gvec_op)
691 tcg_gen_gvec_2i(vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
692 is_q ? 16 : 8, vec_full_reg_size(s), imm, gvec_op);
695 /* Expand a 3-operand AdvSIMD vector operation using an op descriptor. */
696 static void gen_gvec_op3(DisasContext *s, bool is_q, int rd,
697 int rn, int rm, const GVecGen3 *gvec_op)
699 tcg_gen_gvec_3(vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
700 vec_full_reg_offset(s, rm), is_q ? 16 : 8,
701 vec_full_reg_size(s), gvec_op);
704 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
705 * than the 32 bit equivalent.
707 static inline void gen_set_NZ64(TCGv_i64 result)
709 tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
710 tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
713 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
714 static inline void gen_logic_CC(int sf, TCGv_i64 result)
717 gen_set_NZ64(result);
719 tcg_gen_extrl_i64_i32(cpu_ZF, result);
720 tcg_gen_mov_i32(cpu_NF, cpu_ZF);
722 tcg_gen_movi_i32(cpu_CF, 0);
723 tcg_gen_movi_i32(cpu_VF, 0);
726 /* dest = T0 + T1; compute C, N, V and Z flags */
727 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
730 TCGv_i64 result, flag, tmp;
731 result = tcg_temp_new_i64();
732 flag = tcg_temp_new_i64();
733 tmp = tcg_temp_new_i64();
735 tcg_gen_movi_i64(tmp, 0);
736 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
738 tcg_gen_extrl_i64_i32(cpu_CF, flag);
740 gen_set_NZ64(result);
742 tcg_gen_xor_i64(flag, result, t0);
743 tcg_gen_xor_i64(tmp, t0, t1);
744 tcg_gen_andc_i64(flag, flag, tmp);
745 tcg_temp_free_i64(tmp);
746 tcg_gen_extrh_i64_i32(cpu_VF, flag);
748 tcg_gen_mov_i64(dest, result);
749 tcg_temp_free_i64(result);
750 tcg_temp_free_i64(flag);
752 /* 32 bit arithmetic */
753 TCGv_i32 t0_32 = tcg_temp_new_i32();
754 TCGv_i32 t1_32 = tcg_temp_new_i32();
755 TCGv_i32 tmp = tcg_temp_new_i32();
757 tcg_gen_movi_i32(tmp, 0);
758 tcg_gen_extrl_i64_i32(t0_32, t0);
759 tcg_gen_extrl_i64_i32(t1_32, t1);
760 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
761 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
762 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
763 tcg_gen_xor_i32(tmp, t0_32, t1_32);
764 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
765 tcg_gen_extu_i32_i64(dest, cpu_NF);
767 tcg_temp_free_i32(tmp);
768 tcg_temp_free_i32(t0_32);
769 tcg_temp_free_i32(t1_32);
773 /* dest = T0 - T1; compute C, N, V and Z flags */
774 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
777 /* 64 bit arithmetic */
778 TCGv_i64 result, flag, tmp;
780 result = tcg_temp_new_i64();
781 flag = tcg_temp_new_i64();
782 tcg_gen_sub_i64(result, t0, t1);
784 gen_set_NZ64(result);
786 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
787 tcg_gen_extrl_i64_i32(cpu_CF, flag);
789 tcg_gen_xor_i64(flag, result, t0);
790 tmp = tcg_temp_new_i64();
791 tcg_gen_xor_i64(tmp, t0, t1);
792 tcg_gen_and_i64(flag, flag, tmp);
793 tcg_temp_free_i64(tmp);
794 tcg_gen_extrh_i64_i32(cpu_VF, flag);
795 tcg_gen_mov_i64(dest, result);
796 tcg_temp_free_i64(flag);
797 tcg_temp_free_i64(result);
799 /* 32 bit arithmetic */
800 TCGv_i32 t0_32 = tcg_temp_new_i32();
801 TCGv_i32 t1_32 = tcg_temp_new_i32();
804 tcg_gen_extrl_i64_i32(t0_32, t0);
805 tcg_gen_extrl_i64_i32(t1_32, t1);
806 tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
807 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
808 tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
809 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
810 tmp = tcg_temp_new_i32();
811 tcg_gen_xor_i32(tmp, t0_32, t1_32);
812 tcg_temp_free_i32(t0_32);
813 tcg_temp_free_i32(t1_32);
814 tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
815 tcg_temp_free_i32(tmp);
816 tcg_gen_extu_i32_i64(dest, cpu_NF);
820 /* dest = T0 + T1 + CF; do not compute flags. */
821 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
823 TCGv_i64 flag = tcg_temp_new_i64();
824 tcg_gen_extu_i32_i64(flag, cpu_CF);
825 tcg_gen_add_i64(dest, t0, t1);
826 tcg_gen_add_i64(dest, dest, flag);
827 tcg_temp_free_i64(flag);
830 tcg_gen_ext32u_i64(dest, dest);
834 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
835 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
838 TCGv_i64 result, cf_64, vf_64, tmp;
839 result = tcg_temp_new_i64();
840 cf_64 = tcg_temp_new_i64();
841 vf_64 = tcg_temp_new_i64();
842 tmp = tcg_const_i64(0);
844 tcg_gen_extu_i32_i64(cf_64, cpu_CF);
845 tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
846 tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
847 tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
848 gen_set_NZ64(result);
850 tcg_gen_xor_i64(vf_64, result, t0);
851 tcg_gen_xor_i64(tmp, t0, t1);
852 tcg_gen_andc_i64(vf_64, vf_64, tmp);
853 tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
855 tcg_gen_mov_i64(dest, result);
857 tcg_temp_free_i64(tmp);
858 tcg_temp_free_i64(vf_64);
859 tcg_temp_free_i64(cf_64);
860 tcg_temp_free_i64(result);
862 TCGv_i32 t0_32, t1_32, tmp;
863 t0_32 = tcg_temp_new_i32();
864 t1_32 = tcg_temp_new_i32();
865 tmp = tcg_const_i32(0);
867 tcg_gen_extrl_i64_i32(t0_32, t0);
868 tcg_gen_extrl_i64_i32(t1_32, t1);
869 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
870 tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
872 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
873 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
874 tcg_gen_xor_i32(tmp, t0_32, t1_32);
875 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
876 tcg_gen_extu_i32_i64(dest, cpu_NF);
878 tcg_temp_free_i32(tmp);
879 tcg_temp_free_i32(t1_32);
880 tcg_temp_free_i32(t0_32);
885 * Load/Store generators
889 * Store from GPR register to memory.
891 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
892 TCGv_i64 tcg_addr, int size, int memidx,
894 unsigned int iss_srt,
895 bool iss_sf, bool iss_ar)
898 tcg_gen_qemu_st_i64(source, tcg_addr, memidx, s->be_data + size);
903 syn = syn_data_abort_with_iss(0,
909 0, 0, 0, 0, 0, false);
910 disas_set_insn_syndrome(s, syn);
914 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
915 TCGv_i64 tcg_addr, int size,
917 unsigned int iss_srt,
918 bool iss_sf, bool iss_ar)
920 do_gpr_st_memidx(s, source, tcg_addr, size, get_mem_index(s),
921 iss_valid, iss_srt, iss_sf, iss_ar);
925 * Load from memory to GPR register
927 static void do_gpr_ld_memidx(DisasContext *s,
928 TCGv_i64 dest, TCGv_i64 tcg_addr,
929 int size, bool is_signed,
930 bool extend, int memidx,
931 bool iss_valid, unsigned int iss_srt,
932 bool iss_sf, bool iss_ar)
934 TCGMemOp memop = s->be_data + size;
942 tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
944 if (extend && is_signed) {
946 tcg_gen_ext32u_i64(dest, dest);
952 syn = syn_data_abort_with_iss(0,
958 0, 0, 0, 0, 0, false);
959 disas_set_insn_syndrome(s, syn);
963 static void do_gpr_ld(DisasContext *s,
964 TCGv_i64 dest, TCGv_i64 tcg_addr,
965 int size, bool is_signed, bool extend,
966 bool iss_valid, unsigned int iss_srt,
967 bool iss_sf, bool iss_ar)
969 do_gpr_ld_memidx(s, dest, tcg_addr, size, is_signed, extend,
971 iss_valid, iss_srt, iss_sf, iss_ar);
975 * Store from FP register to memory
977 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
979 /* This writes the bottom N bits of a 128 bit wide vector to memory */
980 TCGv_i64 tmp = tcg_temp_new_i64();
981 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_offset(s, srcidx, MO_64));
983 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s),
986 bool be = s->be_data == MO_BE;
987 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
989 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
990 tcg_gen_qemu_st_i64(tmp, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
992 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_hi_offset(s, srcidx));
993 tcg_gen_qemu_st_i64(tmp, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
995 tcg_temp_free_i64(tcg_hiaddr);
998 tcg_temp_free_i64(tmp);
1002 * Load from memory to FP register
1004 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
1006 /* This always zero-extends and writes to a full 128 bit wide vector */
1007 TCGv_i64 tmplo = tcg_temp_new_i64();
1011 TCGMemOp memop = s->be_data + size;
1012 tmphi = tcg_const_i64(0);
1013 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
1015 bool be = s->be_data == MO_BE;
1016 TCGv_i64 tcg_hiaddr;
1018 tmphi = tcg_temp_new_i64();
1019 tcg_hiaddr = tcg_temp_new_i64();
1021 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
1022 tcg_gen_qemu_ld_i64(tmplo, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
1024 tcg_gen_qemu_ld_i64(tmphi, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
1026 tcg_temp_free_i64(tcg_hiaddr);
1029 tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
1030 tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
1032 tcg_temp_free_i64(tmplo);
1033 tcg_temp_free_i64(tmphi);
1035 clear_vec_high(s, true, destidx);
1039 * Vector load/store helpers.
1041 * The principal difference between this and a FP load is that we don't
1042 * zero extend as we are filling a partial chunk of the vector register.
1043 * These functions don't support 128 bit loads/stores, which would be
1044 * normal load/store operations.
1046 * The _i32 versions are useful when operating on 32 bit quantities
1047 * (eg for floating point single or using Neon helper functions).
1050 /* Get value of an element within a vector register */
1051 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
1052 int element, TCGMemOp memop)
1054 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1057 tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
1060 tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
1063 tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
1066 tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
1069 tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
1072 tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
1076 tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
1079 g_assert_not_reached();
1083 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1084 int element, TCGMemOp memop)
1086 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1089 tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1092 tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1095 tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1098 tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1102 tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1105 g_assert_not_reached();
1109 /* Set value of an element within a vector register */
1110 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1111 int element, TCGMemOp memop)
1113 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1116 tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1119 tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1122 tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1125 tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1128 g_assert_not_reached();
1132 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1133 int destidx, int element, TCGMemOp memop)
1135 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1138 tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1141 tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1144 tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1147 g_assert_not_reached();
1151 /* Store from vector register to memory */
1152 static void do_vec_st(DisasContext *s, int srcidx, int element,
1153 TCGv_i64 tcg_addr, int size)
1155 TCGMemOp memop = s->be_data + size;
1156 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1158 read_vec_element(s, tcg_tmp, srcidx, element, size);
1159 tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
1161 tcg_temp_free_i64(tcg_tmp);
1164 /* Load from memory to vector register */
1165 static void do_vec_ld(DisasContext *s, int destidx, int element,
1166 TCGv_i64 tcg_addr, int size)
1168 TCGMemOp memop = s->be_data + size;
1169 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1171 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
1172 write_vec_element(s, tcg_tmp, destidx, element, size);
1174 tcg_temp_free_i64(tcg_tmp);
1177 /* Check that FP/Neon access is enabled. If it is, return
1178 * true. If not, emit code to generate an appropriate exception,
1179 * and return false; the caller should not emit any code for
1180 * the instruction. Note that this check must happen after all
1181 * unallocated-encoding checks (otherwise the syndrome information
1182 * for the resulting exception will be incorrect).
1184 static inline bool fp_access_check(DisasContext *s)
1186 assert(!s->fp_access_checked);
1187 s->fp_access_checked = true;
1189 if (!s->fp_excp_el) {
1193 gen_exception_insn(s, 4, EXCP_UDEF, syn_fp_access_trap(1, 0xe, false),
1198 /* Check that SVE access is enabled. If it is, return true.
1199 * If not, emit code to generate an appropriate exception and return false.
1201 static inline bool sve_access_check(DisasContext *s)
1203 if (s->sve_excp_el) {
1204 gen_exception_insn(s, 4, EXCP_UDEF, syn_sve_access_trap(),
1212 * This utility function is for doing register extension with an
1213 * optional shift. You will likely want to pass a temporary for the
1214 * destination register. See DecodeRegExtend() in the ARM ARM.
1216 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1217 int option, unsigned int shift)
1219 int extsize = extract32(option, 0, 2);
1220 bool is_signed = extract32(option, 2, 1);
1225 tcg_gen_ext8s_i64(tcg_out, tcg_in);
1228 tcg_gen_ext16s_i64(tcg_out, tcg_in);
1231 tcg_gen_ext32s_i64(tcg_out, tcg_in);
1234 tcg_gen_mov_i64(tcg_out, tcg_in);
1240 tcg_gen_ext8u_i64(tcg_out, tcg_in);
1243 tcg_gen_ext16u_i64(tcg_out, tcg_in);
1246 tcg_gen_ext32u_i64(tcg_out, tcg_in);
1249 tcg_gen_mov_i64(tcg_out, tcg_in);
1255 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1259 static inline void gen_check_sp_alignment(DisasContext *s)
1261 /* The AArch64 architecture mandates that (if enabled via PSTATE
1262 * or SCTLR bits) there is a check that SP is 16-aligned on every
1263 * SP-relative load or store (with an exception generated if it is not).
1264 * In line with general QEMU practice regarding misaligned accesses,
1265 * we omit these checks for the sake of guest program performance.
1266 * This function is provided as a hook so we can more easily add these
1267 * checks in future (possibly as a "favour catching guest program bugs
1268 * over speed" user selectable option).
1273 * This provides a simple table based table lookup decoder. It is
1274 * intended to be used when the relevant bits for decode are too
1275 * awkwardly placed and switch/if based logic would be confusing and
1276 * deeply nested. Since it's a linear search through the table, tables
1277 * should be kept small.
1279 * It returns the first handler where insn & mask == pattern, or
1280 * NULL if there is no match.
1281 * The table is terminated by an empty mask (i.e. 0)
1283 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1286 const AArch64DecodeTable *tptr = table;
1288 while (tptr->mask) {
1289 if ((insn & tptr->mask) == tptr->pattern) {
1290 return tptr->disas_fn;
1298 * The instruction disassembly implemented here matches
1299 * the instruction encoding classifications in chapter C4
1300 * of the ARM Architecture Reference Manual (DDI0487B_a);
1301 * classification names and decode diagrams here should generally
1302 * match up with those in the manual.
1305 /* Unconditional branch (immediate)
1307 * +----+-----------+-------------------------------------+
1308 * | op | 0 0 1 0 1 | imm26 |
1309 * +----+-----------+-------------------------------------+
1311 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
1313 uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
1315 if (insn & (1U << 31)) {
1316 /* BL Branch with link */
1317 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1320 /* B Branch / BL Branch with link */
1321 gen_goto_tb(s, 0, addr);
1324 /* Compare and branch (immediate)
1325 * 31 30 25 24 23 5 4 0
1326 * +----+-------------+----+---------------------+--------+
1327 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
1328 * +----+-------------+----+---------------------+--------+
1330 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
1332 unsigned int sf, op, rt;
1334 TCGLabel *label_match;
1337 sf = extract32(insn, 31, 1);
1338 op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
1339 rt = extract32(insn, 0, 5);
1340 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1342 tcg_cmp = read_cpu_reg(s, rt, sf);
1343 label_match = gen_new_label();
1345 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1346 tcg_cmp, 0, label_match);
1348 gen_goto_tb(s, 0, s->pc);
1349 gen_set_label(label_match);
1350 gen_goto_tb(s, 1, addr);
1353 /* Test and branch (immediate)
1354 * 31 30 25 24 23 19 18 5 4 0
1355 * +----+-------------+----+-------+-------------+------+
1356 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
1357 * +----+-------------+----+-------+-------------+------+
1359 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1361 unsigned int bit_pos, op, rt;
1363 TCGLabel *label_match;
1366 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1367 op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1368 addr = s->pc + sextract32(insn, 5, 14) * 4 - 4;
1369 rt = extract32(insn, 0, 5);
1371 tcg_cmp = tcg_temp_new_i64();
1372 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1373 label_match = gen_new_label();
1374 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1375 tcg_cmp, 0, label_match);
1376 tcg_temp_free_i64(tcg_cmp);
1377 gen_goto_tb(s, 0, s->pc);
1378 gen_set_label(label_match);
1379 gen_goto_tb(s, 1, addr);
1382 /* Conditional branch (immediate)
1383 * 31 25 24 23 5 4 3 0
1384 * +---------------+----+---------------------+----+------+
1385 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
1386 * +---------------+----+---------------------+----+------+
1388 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1393 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1394 unallocated_encoding(s);
1397 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1398 cond = extract32(insn, 0, 4);
1401 /* genuinely conditional branches */
1402 TCGLabel *label_match = gen_new_label();
1403 arm_gen_test_cc(cond, label_match);
1404 gen_goto_tb(s, 0, s->pc);
1405 gen_set_label(label_match);
1406 gen_goto_tb(s, 1, addr);
1408 /* 0xe and 0xf are both "always" conditions */
1409 gen_goto_tb(s, 0, addr);
1413 /* HINT instruction group, including various allocated HINTs */
1414 static void handle_hint(DisasContext *s, uint32_t insn,
1415 unsigned int op1, unsigned int op2, unsigned int crm)
1417 unsigned int selector = crm << 3 | op2;
1420 unallocated_encoding(s);
1428 s->base.is_jmp = DISAS_WFI;
1430 /* When running in MTTCG we don't generate jumps to the yield and
1431 * WFE helpers as it won't affect the scheduling of other vCPUs.
1432 * If we wanted to more completely model WFE/SEV so we don't busy
1433 * spin unnecessarily we would need to do something more involved.
1436 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1437 s->base.is_jmp = DISAS_YIELD;
1441 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1442 s->base.is_jmp = DISAS_WFE;
1447 /* we treat all as NOP at least for now */
1450 /* default specified as NOP equivalent */
1455 static void gen_clrex(DisasContext *s, uint32_t insn)
1457 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1460 /* CLREX, DSB, DMB, ISB */
1461 static void handle_sync(DisasContext *s, uint32_t insn,
1462 unsigned int op1, unsigned int op2, unsigned int crm)
1467 unallocated_encoding(s);
1478 case 1: /* MBReqTypes_Reads */
1479 bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1481 case 2: /* MBReqTypes_Writes */
1482 bar = TCG_BAR_SC | TCG_MO_ST_ST;
1484 default: /* MBReqTypes_All */
1485 bar = TCG_BAR_SC | TCG_MO_ALL;
1491 /* We need to break the TB after this insn to execute
1492 * a self-modified code correctly and also to take
1493 * any pending interrupts immediately.
1495 gen_goto_tb(s, 0, s->pc);
1498 unallocated_encoding(s);
1503 /* MSR (immediate) - move immediate to processor state field */
1504 static void handle_msr_i(DisasContext *s, uint32_t insn,
1505 unsigned int op1, unsigned int op2, unsigned int crm)
1507 int op = op1 << 3 | op2;
1509 case 0x05: /* SPSel */
1510 if (s->current_el == 0) {
1511 unallocated_encoding(s);
1515 case 0x1e: /* DAIFSet */
1516 case 0x1f: /* DAIFClear */
1518 TCGv_i32 tcg_imm = tcg_const_i32(crm);
1519 TCGv_i32 tcg_op = tcg_const_i32(op);
1520 gen_a64_set_pc_im(s->pc - 4);
1521 gen_helper_msr_i_pstate(cpu_env, tcg_op, tcg_imm);
1522 tcg_temp_free_i32(tcg_imm);
1523 tcg_temp_free_i32(tcg_op);
1524 /* For DAIFClear, exit the cpu loop to re-evaluate pending IRQs. */
1525 gen_a64_set_pc_im(s->pc);
1526 s->base.is_jmp = (op == 0x1f ? DISAS_EXIT : DISAS_JUMP);
1530 unallocated_encoding(s);
1535 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1537 TCGv_i32 tmp = tcg_temp_new_i32();
1538 TCGv_i32 nzcv = tcg_temp_new_i32();
1540 /* build bit 31, N */
1541 tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
1542 /* build bit 30, Z */
1543 tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1544 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1545 /* build bit 29, C */
1546 tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1547 /* build bit 28, V */
1548 tcg_gen_shri_i32(tmp, cpu_VF, 31);
1549 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1550 /* generate result */
1551 tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1553 tcg_temp_free_i32(nzcv);
1554 tcg_temp_free_i32(tmp);
1557 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1560 TCGv_i32 nzcv = tcg_temp_new_i32();
1562 /* take NZCV from R[t] */
1563 tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
1566 tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
1568 tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1569 tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1571 tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1572 tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1574 tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1575 tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1576 tcg_temp_free_i32(nzcv);
1579 /* MRS - move from system register
1580 * MSR (register) - move to system register
1583 * These are all essentially the same insn in 'read' and 'write'
1584 * versions, with varying op0 fields.
1586 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1587 unsigned int op0, unsigned int op1, unsigned int op2,
1588 unsigned int crn, unsigned int crm, unsigned int rt)
1590 const ARMCPRegInfo *ri;
1593 ri = get_arm_cp_reginfo(s->cp_regs,
1594 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1595 crn, crm, op0, op1, op2));
1598 /* Unknown register; this might be a guest error or a QEMU
1599 * unimplemented feature.
1601 qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1602 "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1603 isread ? "read" : "write", op0, op1, crn, crm, op2);
1604 unallocated_encoding(s);
1608 /* Check access permissions */
1609 if (!cp_access_ok(s->current_el, ri, isread)) {
1610 unallocated_encoding(s);
1615 /* Emit code to perform further access permissions checks at
1616 * runtime; this may result in an exception.
1619 TCGv_i32 tcg_syn, tcg_isread;
1622 gen_a64_set_pc_im(s->pc - 4);
1623 tmpptr = tcg_const_ptr(ri);
1624 syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1625 tcg_syn = tcg_const_i32(syndrome);
1626 tcg_isread = tcg_const_i32(isread);
1627 gen_helper_access_check_cp_reg(cpu_env, tmpptr, tcg_syn, tcg_isread);
1628 tcg_temp_free_ptr(tmpptr);
1629 tcg_temp_free_i32(tcg_syn);
1630 tcg_temp_free_i32(tcg_isread);
1633 /* Handle special cases first */
1634 switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
1638 tcg_rt = cpu_reg(s, rt);
1640 gen_get_nzcv(tcg_rt);
1642 gen_set_nzcv(tcg_rt);
1645 case ARM_CP_CURRENTEL:
1646 /* Reads as current EL value from pstate, which is
1647 * guaranteed to be constant by the tb flags.
1649 tcg_rt = cpu_reg(s, rt);
1650 tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
1653 /* Writes clear the aligned block of memory which rt points into. */
1654 tcg_rt = cpu_reg(s, rt);
1655 gen_helper_dc_zva(cpu_env, tcg_rt);
1660 if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
1663 if ((ri->type & ARM_CP_FPU) && !fp_access_check(s)) {
1667 if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1671 tcg_rt = cpu_reg(s, rt);
1674 if (ri->type & ARM_CP_CONST) {
1675 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1676 } else if (ri->readfn) {
1678 tmpptr = tcg_const_ptr(ri);
1679 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1680 tcg_temp_free_ptr(tmpptr);
1682 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1685 if (ri->type & ARM_CP_CONST) {
1686 /* If not forbidden by access permissions, treat as WI */
1688 } else if (ri->writefn) {
1690 tmpptr = tcg_const_ptr(ri);
1691 gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1692 tcg_temp_free_ptr(tmpptr);
1694 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
1698 if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1699 /* I/O operations must end the TB here (whether read or write) */
1701 s->base.is_jmp = DISAS_UPDATE;
1702 } else if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
1703 /* We default to ending the TB on a coprocessor register write,
1704 * but allow this to be suppressed by the register definition
1705 * (usually only necessary to work around guest bugs).
1707 s->base.is_jmp = DISAS_UPDATE;
1712 * 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0
1713 * +---------------------+---+-----+-----+-------+-------+-----+------+
1714 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 | CRn | CRm | op2 | Rt |
1715 * +---------------------+---+-----+-----+-------+-------+-----+------+
1717 static void disas_system(DisasContext *s, uint32_t insn)
1719 unsigned int l, op0, op1, crn, crm, op2, rt;
1720 l = extract32(insn, 21, 1);
1721 op0 = extract32(insn, 19, 2);
1722 op1 = extract32(insn, 16, 3);
1723 crn = extract32(insn, 12, 4);
1724 crm = extract32(insn, 8, 4);
1725 op2 = extract32(insn, 5, 3);
1726 rt = extract32(insn, 0, 5);
1729 if (l || rt != 31) {
1730 unallocated_encoding(s);
1734 case 2: /* HINT (including allocated hints like NOP, YIELD, etc) */
1735 handle_hint(s, insn, op1, op2, crm);
1737 case 3: /* CLREX, DSB, DMB, ISB */
1738 handle_sync(s, insn, op1, op2, crm);
1740 case 4: /* MSR (immediate) */
1741 handle_msr_i(s, insn, op1, op2, crm);
1744 unallocated_encoding(s);
1749 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
1752 /* Exception generation
1754 * 31 24 23 21 20 5 4 2 1 0
1755 * +-----------------+-----+------------------------+-----+----+
1756 * | 1 1 0 1 0 1 0 0 | opc | imm16 | op2 | LL |
1757 * +-----------------------+------------------------+----------+
1759 static void disas_exc(DisasContext *s, uint32_t insn)
1761 int opc = extract32(insn, 21, 3);
1762 int op2_ll = extract32(insn, 0, 5);
1763 int imm16 = extract32(insn, 5, 16);
1768 /* For SVC, HVC and SMC we advance the single-step state
1769 * machine before taking the exception. This is architecturally
1770 * mandated, to ensure that single-stepping a system call
1771 * instruction works properly.
1776 gen_exception_insn(s, 0, EXCP_SWI, syn_aa64_svc(imm16),
1777 default_exception_el(s));
1780 if (s->current_el == 0) {
1781 unallocated_encoding(s);
1784 /* The pre HVC helper handles cases when HVC gets trapped
1785 * as an undefined insn by runtime configuration.
1787 gen_a64_set_pc_im(s->pc - 4);
1788 gen_helper_pre_hvc(cpu_env);
1790 gen_exception_insn(s, 0, EXCP_HVC, syn_aa64_hvc(imm16), 2);
1793 if (s->current_el == 0) {
1794 unallocated_encoding(s);
1797 gen_a64_set_pc_im(s->pc - 4);
1798 tmp = tcg_const_i32(syn_aa64_smc(imm16));
1799 gen_helper_pre_smc(cpu_env, tmp);
1800 tcg_temp_free_i32(tmp);
1802 gen_exception_insn(s, 0, EXCP_SMC, syn_aa64_smc(imm16), 3);
1805 unallocated_encoding(s);
1811 unallocated_encoding(s);
1815 gen_exception_insn(s, 4, EXCP_BKPT, syn_aa64_bkpt(imm16),
1816 default_exception_el(s));
1820 unallocated_encoding(s);
1823 /* HLT. This has two purposes.
1824 * Architecturally, it is an external halting debug instruction.
1825 * Since QEMU doesn't implement external debug, we treat this as
1826 * it is required for halting debug disabled: it will UNDEF.
1827 * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
1829 if (semihosting_enabled() && imm16 == 0xf000) {
1830 #ifndef CONFIG_USER_ONLY
1831 /* In system mode, don't allow userspace access to semihosting,
1832 * to provide some semblance of security (and for consistency
1833 * with our 32-bit semihosting).
1835 if (s->current_el == 0) {
1836 unsupported_encoding(s, insn);
1840 gen_exception_internal_insn(s, 0, EXCP_SEMIHOST);
1842 unsupported_encoding(s, insn);
1846 if (op2_ll < 1 || op2_ll > 3) {
1847 unallocated_encoding(s);
1850 /* DCPS1, DCPS2, DCPS3 */
1851 unsupported_encoding(s, insn);
1854 unallocated_encoding(s);
1859 /* Unconditional branch (register)
1860 * 31 25 24 21 20 16 15 10 9 5 4 0
1861 * +---------------+-------+-------+-------+------+-------+
1862 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
1863 * +---------------+-------+-------+-------+------+-------+
1865 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
1867 unsigned int opc, op2, op3, rn, op4;
1869 opc = extract32(insn, 21, 4);
1870 op2 = extract32(insn, 16, 5);
1871 op3 = extract32(insn, 10, 6);
1872 rn = extract32(insn, 5, 5);
1873 op4 = extract32(insn, 0, 5);
1875 if (op4 != 0x0 || op3 != 0x0 || op2 != 0x1f) {
1876 unallocated_encoding(s);
1884 gen_a64_set_pc(s, cpu_reg(s, rn));
1885 /* BLR also needs to load return address */
1887 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1891 if (s->current_el == 0) {
1892 unallocated_encoding(s);
1895 gen_helper_exception_return(cpu_env);
1896 /* Must exit loop to check un-masked IRQs */
1897 s->base.is_jmp = DISAS_EXIT;
1901 unallocated_encoding(s);
1903 unsupported_encoding(s, insn);
1907 unallocated_encoding(s);
1911 s->base.is_jmp = DISAS_JUMP;
1914 /* Branches, exception generating and system instructions */
1915 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
1917 switch (extract32(insn, 25, 7)) {
1918 case 0x0a: case 0x0b:
1919 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
1920 disas_uncond_b_imm(s, insn);
1922 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
1923 disas_comp_b_imm(s, insn);
1925 case 0x1b: case 0x5b: /* Test & branch (immediate) */
1926 disas_test_b_imm(s, insn);
1928 case 0x2a: /* Conditional branch (immediate) */
1929 disas_cond_b_imm(s, insn);
1931 case 0x6a: /* Exception generation / System */
1932 if (insn & (1 << 24)) {
1933 disas_system(s, insn);
1938 case 0x6b: /* Unconditional branch (register) */
1939 disas_uncond_b_reg(s, insn);
1942 unallocated_encoding(s);
1948 * Load/Store exclusive instructions are implemented by remembering
1949 * the value/address loaded, and seeing if these are the same
1950 * when the store is performed. This is not actually the architecturally
1951 * mandated semantics, but it works for typical guest code sequences
1952 * and avoids having to monitor regular stores.
1954 * The store exclusive uses the atomic cmpxchg primitives to avoid
1955 * races in multi-threaded linux-user and when MTTCG softmmu is
1958 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
1959 TCGv_i64 addr, int size, bool is_pair)
1961 int idx = get_mem_index(s);
1962 TCGMemOp memop = s->be_data;
1964 g_assert(size <= 3);
1966 g_assert(size >= 2);
1968 /* The pair must be single-copy atomic for the doubleword. */
1969 memop |= MO_64 | MO_ALIGN;
1970 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
1971 if (s->be_data == MO_LE) {
1972 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
1973 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
1975 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
1976 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
1979 /* The pair must be single-copy atomic for *each* doubleword, not
1980 the entire quadword, however it must be quadword aligned. */
1982 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx,
1983 memop | MO_ALIGN_16);
1985 TCGv_i64 addr2 = tcg_temp_new_i64();
1986 tcg_gen_addi_i64(addr2, addr, 8);
1987 tcg_gen_qemu_ld_i64(cpu_exclusive_high, addr2, idx, memop);
1988 tcg_temp_free_i64(addr2);
1990 tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
1991 tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
1994 memop |= size | MO_ALIGN;
1995 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
1996 tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
1998 tcg_gen_mov_i64(cpu_exclusive_addr, addr);
2001 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2002 TCGv_i64 addr, int size, int is_pair)
2004 /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2005 * && (!is_pair || env->exclusive_high == [addr + datasize])) {
2008 * [addr + datasize] = {Rt2};
2014 * env->exclusive_addr = -1;
2016 TCGLabel *fail_label = gen_new_label();
2017 TCGLabel *done_label = gen_new_label();
2020 tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
2022 tmp = tcg_temp_new_i64();
2025 if (s->be_data == MO_LE) {
2026 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2028 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2030 tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2031 cpu_exclusive_val, tmp,
2033 MO_64 | MO_ALIGN | s->be_data);
2034 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2035 } else if (s->be_data == MO_LE) {
2036 if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2037 gen_helper_paired_cmpxchg64_le_parallel(tmp, cpu_env,
2042 gen_helper_paired_cmpxchg64_le(tmp, cpu_env, cpu_exclusive_addr,
2043 cpu_reg(s, rt), cpu_reg(s, rt2));
2046 if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2047 gen_helper_paired_cmpxchg64_be_parallel(tmp, cpu_env,
2052 gen_helper_paired_cmpxchg64_be(tmp, cpu_env, cpu_exclusive_addr,
2053 cpu_reg(s, rt), cpu_reg(s, rt2));
2057 tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2058 cpu_reg(s, rt), get_mem_index(s),
2059 size | MO_ALIGN | s->be_data);
2060 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2062 tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2063 tcg_temp_free_i64(tmp);
2064 tcg_gen_br(done_label);
2066 gen_set_label(fail_label);
2067 tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2068 gen_set_label(done_label);
2069 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2072 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
2073 * from the ARMv8 specs for LDR (Shared decode for all encodings).
2075 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2077 int opc0 = extract32(opc, 0, 1);
2081 regsize = opc0 ? 32 : 64;
2083 regsize = size == 3 ? 64 : 32;
2085 return regsize == 64;
2088 /* Load/store exclusive
2090 * 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0
2091 * +-----+-------------+----+---+----+------+----+-------+------+------+
2092 * | sz | 0 0 1 0 0 0 | o2 | L | o1 | Rs | o0 | Rt2 | Rn | Rt |
2093 * +-----+-------------+----+---+----+------+----+-------+------+------+
2095 * sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2096 * L: 0 -> store, 1 -> load
2097 * o2: 0 -> exclusive, 1 -> not
2098 * o1: 0 -> single register, 1 -> register pair
2099 * o0: 1 -> load-acquire/store-release, 0 -> not
2101 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2103 int rt = extract32(insn, 0, 5);
2104 int rn = extract32(insn, 5, 5);
2105 int rt2 = extract32(insn, 10, 5);
2106 int is_lasr = extract32(insn, 15, 1);
2107 int rs = extract32(insn, 16, 5);
2108 int is_pair = extract32(insn, 21, 1);
2109 int is_store = !extract32(insn, 22, 1);
2110 int is_excl = !extract32(insn, 23, 1);
2111 int size = extract32(insn, 30, 2);
2114 if ((!is_excl && !is_pair && !is_lasr) ||
2115 (!is_excl && is_pair) ||
2116 (is_pair && size < 2)) {
2117 unallocated_encoding(s);
2122 gen_check_sp_alignment(s);
2124 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2126 /* Note that since TCG is single threaded load-acquire/store-release
2127 * semantics require no extra if (is_lasr) { ... } handling.
2133 gen_load_exclusive(s, rt, rt2, tcg_addr, size, is_pair);
2135 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2139 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2141 gen_store_exclusive(s, rs, rt, rt2, tcg_addr, size, is_pair);
2144 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2145 bool iss_sf = disas_ldst_compute_iss_sf(size, false, 0);
2147 /* Generate ISS for non-exclusive accesses including LASR. */
2150 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2152 do_gpr_st(s, tcg_rt, tcg_addr, size,
2153 true, rt, iss_sf, is_lasr);
2155 do_gpr_ld(s, tcg_rt, tcg_addr, size, false, false,
2156 true, rt, iss_sf, is_lasr);
2158 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2165 * Load register (literal)
2167 * 31 30 29 27 26 25 24 23 5 4 0
2168 * +-----+-------+---+-----+-------------------+-------+
2169 * | opc | 0 1 1 | V | 0 0 | imm19 | Rt |
2170 * +-----+-------+---+-----+-------------------+-------+
2172 * V: 1 -> vector (simd/fp)
2173 * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2174 * 10-> 32 bit signed, 11 -> prefetch
2175 * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2177 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2179 int rt = extract32(insn, 0, 5);
2180 int64_t imm = sextract32(insn, 5, 19) << 2;
2181 bool is_vector = extract32(insn, 26, 1);
2182 int opc = extract32(insn, 30, 2);
2183 bool is_signed = false;
2185 TCGv_i64 tcg_rt, tcg_addr;
2189 unallocated_encoding(s);
2193 if (!fp_access_check(s)) {
2198 /* PRFM (literal) : prefetch */
2201 size = 2 + extract32(opc, 0, 1);
2202 is_signed = extract32(opc, 1, 1);
2205 tcg_rt = cpu_reg(s, rt);
2207 tcg_addr = tcg_const_i64((s->pc - 4) + imm);
2209 do_fp_ld(s, rt, tcg_addr, size);
2211 /* Only unsigned 32bit loads target 32bit registers. */
2212 bool iss_sf = opc != 0;
2214 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false,
2215 true, rt, iss_sf, false);
2217 tcg_temp_free_i64(tcg_addr);
2221 * LDNP (Load Pair - non-temporal hint)
2222 * LDP (Load Pair - non vector)
2223 * LDPSW (Load Pair Signed Word - non vector)
2224 * STNP (Store Pair - non-temporal hint)
2225 * STP (Store Pair - non vector)
2226 * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2227 * LDP (Load Pair of SIMD&FP)
2228 * STNP (Store Pair of SIMD&FP - non-temporal hint)
2229 * STP (Store Pair of SIMD&FP)
2231 * 31 30 29 27 26 25 24 23 22 21 15 14 10 9 5 4 0
2232 * +-----+-------+---+---+-------+---+-----------------------------+
2233 * | opc | 1 0 1 | V | 0 | index | L | imm7 | Rt2 | Rn | Rt |
2234 * +-----+-------+---+---+-------+---+-------+-------+------+------+
2236 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
2238 * LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2239 * V: 0 -> GPR, 1 -> Vector
2240 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2241 * 10 -> signed offset, 11 -> pre-index
2242 * L: 0 -> Store 1 -> Load
2244 * Rt, Rt2 = GPR or SIMD registers to be stored
2245 * Rn = general purpose register containing address
2246 * imm7 = signed offset (multiple of 4 or 8 depending on size)
2248 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2250 int rt = extract32(insn, 0, 5);
2251 int rn = extract32(insn, 5, 5);
2252 int rt2 = extract32(insn, 10, 5);
2253 uint64_t offset = sextract64(insn, 15, 7);
2254 int index = extract32(insn, 23, 2);
2255 bool is_vector = extract32(insn, 26, 1);
2256 bool is_load = extract32(insn, 22, 1);
2257 int opc = extract32(insn, 30, 2);
2259 bool is_signed = false;
2260 bool postindex = false;
2263 TCGv_i64 tcg_addr; /* calculated address */
2267 unallocated_encoding(s);
2274 size = 2 + extract32(opc, 1, 1);
2275 is_signed = extract32(opc, 0, 1);
2276 if (!is_load && is_signed) {
2277 unallocated_encoding(s);
2283 case 1: /* post-index */
2288 /* signed offset with "non-temporal" hint. Since we don't emulate
2289 * caches we don't care about hints to the cache system about
2290 * data access patterns, and handle this identically to plain
2294 /* There is no non-temporal-hint version of LDPSW */
2295 unallocated_encoding(s);
2300 case 2: /* signed offset, rn not updated */
2303 case 3: /* pre-index */
2309 if (is_vector && !fp_access_check(s)) {
2316 gen_check_sp_alignment(s);
2319 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2322 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
2327 do_fp_ld(s, rt, tcg_addr, size);
2329 do_fp_st(s, rt, tcg_addr, size);
2331 tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
2333 do_fp_ld(s, rt2, tcg_addr, size);
2335 do_fp_st(s, rt2, tcg_addr, size);
2338 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2339 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
2342 TCGv_i64 tmp = tcg_temp_new_i64();
2344 /* Do not modify tcg_rt before recognizing any exception
2345 * from the second load.
2347 do_gpr_ld(s, tmp, tcg_addr, size, is_signed, false,
2348 false, 0, false, false);
2349 tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
2350 do_gpr_ld(s, tcg_rt2, tcg_addr, size, is_signed, false,
2351 false, 0, false, false);
2353 tcg_gen_mov_i64(tcg_rt, tmp);
2354 tcg_temp_free_i64(tmp);
2356 do_gpr_st(s, tcg_rt, tcg_addr, size,
2357 false, 0, false, false);
2358 tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
2359 do_gpr_st(s, tcg_rt2, tcg_addr, size,
2360 false, 0, false, false);
2366 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset - (1 << size));
2368 tcg_gen_subi_i64(tcg_addr, tcg_addr, 1 << size);
2370 tcg_gen_mov_i64(cpu_reg_sp(s, rn), tcg_addr);
2375 * Load/store (immediate post-indexed)
2376 * Load/store (immediate pre-indexed)
2377 * Load/store (unscaled immediate)
2379 * 31 30 29 27 26 25 24 23 22 21 20 12 11 10 9 5 4 0
2380 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2381 * |size| 1 1 1 | V | 0 0 | opc | 0 | imm9 | idx | Rn | Rt |
2382 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2384 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
2386 * V = 0 -> non-vector
2387 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
2388 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2390 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
2396 int rn = extract32(insn, 5, 5);
2397 int imm9 = sextract32(insn, 12, 9);
2398 int idx = extract32(insn, 10, 2);
2399 bool is_signed = false;
2400 bool is_store = false;
2401 bool is_extended = false;
2402 bool is_unpriv = (idx == 2);
2403 bool iss_valid = !is_vector;
2410 size |= (opc & 2) << 1;
2411 if (size > 4 || is_unpriv) {
2412 unallocated_encoding(s);
2415 is_store = ((opc & 1) == 0);
2416 if (!fp_access_check(s)) {
2420 if (size == 3 && opc == 2) {
2421 /* PRFM - prefetch */
2423 unallocated_encoding(s);
2428 if (opc == 3 && size > 1) {
2429 unallocated_encoding(s);
2432 is_store = (opc == 0);
2433 is_signed = extract32(opc, 1, 1);
2434 is_extended = (size < 3) && extract32(opc, 0, 1);
2452 g_assert_not_reached();
2456 gen_check_sp_alignment(s);
2458 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2461 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
2466 do_fp_st(s, rt, tcg_addr, size);
2468 do_fp_ld(s, rt, tcg_addr, size);
2471 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2472 int memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
2473 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2476 do_gpr_st_memidx(s, tcg_rt, tcg_addr, size, memidx,
2477 iss_valid, rt, iss_sf, false);
2479 do_gpr_ld_memidx(s, tcg_rt, tcg_addr, size,
2480 is_signed, is_extended, memidx,
2481 iss_valid, rt, iss_sf, false);
2486 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2488 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
2490 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2495 * Load/store (register offset)
2497 * 31 30 29 27 26 25 24 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2498 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2499 * |size| 1 1 1 | V | 0 0 | opc | 1 | Rm | opt | S| 1 0 | Rn | Rt |
2500 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2503 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2504 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2506 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2507 * opc<0>: 0 -> store, 1 -> load
2508 * V: 1 -> vector/simd
2509 * opt: extend encoding (see DecodeRegExtend)
2510 * S: if S=1 then scale (essentially index by sizeof(size))
2511 * Rt: register to transfer into/out of
2512 * Rn: address register or SP for base
2513 * Rm: offset register or ZR for offset
2515 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
2521 int rn = extract32(insn, 5, 5);
2522 int shift = extract32(insn, 12, 1);
2523 int rm = extract32(insn, 16, 5);
2524 int opt = extract32(insn, 13, 3);
2525 bool is_signed = false;
2526 bool is_store = false;
2527 bool is_extended = false;
2532 if (extract32(opt, 1, 1) == 0) {
2533 unallocated_encoding(s);
2538 size |= (opc & 2) << 1;
2540 unallocated_encoding(s);
2543 is_store = !extract32(opc, 0, 1);
2544 if (!fp_access_check(s)) {
2548 if (size == 3 && opc == 2) {
2549 /* PRFM - prefetch */
2552 if (opc == 3 && size > 1) {
2553 unallocated_encoding(s);
2556 is_store = (opc == 0);
2557 is_signed = extract32(opc, 1, 1);
2558 is_extended = (size < 3) && extract32(opc, 0, 1);
2562 gen_check_sp_alignment(s);
2564 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2566 tcg_rm = read_cpu_reg(s, rm, 1);
2567 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
2569 tcg_gen_add_i64(tcg_addr, tcg_addr, tcg_rm);
2573 do_fp_st(s, rt, tcg_addr, size);
2575 do_fp_ld(s, rt, tcg_addr, size);
2578 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2579 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2581 do_gpr_st(s, tcg_rt, tcg_addr, size,
2582 true, rt, iss_sf, false);
2584 do_gpr_ld(s, tcg_rt, tcg_addr, size,
2585 is_signed, is_extended,
2586 true, rt, iss_sf, false);
2592 * Load/store (unsigned immediate)
2594 * 31 30 29 27 26 25 24 23 22 21 10 9 5
2595 * +----+-------+---+-----+-----+------------+-------+------+
2596 * |size| 1 1 1 | V | 0 1 | opc | imm12 | Rn | Rt |
2597 * +----+-------+---+-----+-----+------------+-------+------+
2600 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2601 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2603 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2604 * opc<0>: 0 -> store, 1 -> load
2605 * Rn: base address register (inc SP)
2606 * Rt: target register
2608 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
2614 int rn = extract32(insn, 5, 5);
2615 unsigned int imm12 = extract32(insn, 10, 12);
2616 unsigned int offset;
2621 bool is_signed = false;
2622 bool is_extended = false;
2625 size |= (opc & 2) << 1;
2627 unallocated_encoding(s);
2630 is_store = !extract32(opc, 0, 1);
2631 if (!fp_access_check(s)) {
2635 if (size == 3 && opc == 2) {
2636 /* PRFM - prefetch */
2639 if (opc == 3 && size > 1) {
2640 unallocated_encoding(s);
2643 is_store = (opc == 0);
2644 is_signed = extract32(opc, 1, 1);
2645 is_extended = (size < 3) && extract32(opc, 0, 1);
2649 gen_check_sp_alignment(s);
2651 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2652 offset = imm12 << size;
2653 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
2657 do_fp_st(s, rt, tcg_addr, size);
2659 do_fp_ld(s, rt, tcg_addr, size);
2662 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2663 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2665 do_gpr_st(s, tcg_rt, tcg_addr, size,
2666 true, rt, iss_sf, false);
2668 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended,
2669 true, rt, iss_sf, false);
2674 /* Load/store register (all forms) */
2675 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
2677 int rt = extract32(insn, 0, 5);
2678 int opc = extract32(insn, 22, 2);
2679 bool is_vector = extract32(insn, 26, 1);
2680 int size = extract32(insn, 30, 2);
2682 switch (extract32(insn, 24, 2)) {
2684 if (extract32(insn, 21, 1) == 1 && extract32(insn, 10, 2) == 2) {
2685 disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
2687 /* Load/store register (unscaled immediate)
2688 * Load/store immediate pre/post-indexed
2689 * Load/store register unprivileged
2691 disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
2695 disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
2698 unallocated_encoding(s);
2703 /* AdvSIMD load/store multiple structures
2705 * 31 30 29 23 22 21 16 15 12 11 10 9 5 4 0
2706 * +---+---+---------------+---+-------------+--------+------+------+------+
2707 * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size | Rn | Rt |
2708 * +---+---+---------------+---+-------------+--------+------+------+------+
2710 * AdvSIMD load/store multiple structures (post-indexed)
2712 * 31 30 29 23 22 21 20 16 15 12 11 10 9 5 4 0
2713 * +---+---+---------------+---+---+---------+--------+------+------+------+
2714 * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 | Rm | opcode | size | Rn | Rt |
2715 * +---+---+---------------+---+---+---------+--------+------+------+------+
2717 * Rt: first (or only) SIMD&FP register to be transferred
2718 * Rn: base address or SP
2719 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2721 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
2723 int rt = extract32(insn, 0, 5);
2724 int rn = extract32(insn, 5, 5);
2725 int size = extract32(insn, 10, 2);
2726 int opcode = extract32(insn, 12, 4);
2727 bool is_store = !extract32(insn, 22, 1);
2728 bool is_postidx = extract32(insn, 23, 1);
2729 bool is_q = extract32(insn, 30, 1);
2730 TCGv_i64 tcg_addr, tcg_rn;
2732 int ebytes = 1 << size;
2733 int elements = (is_q ? 128 : 64) / (8 << size);
2734 int rpt; /* num iterations */
2735 int selem; /* structure elements */
2738 if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
2739 unallocated_encoding(s);
2743 /* From the shared decode logic */
2774 unallocated_encoding(s);
2778 if (size == 3 && !is_q && selem != 1) {
2780 unallocated_encoding(s);
2784 if (!fp_access_check(s)) {
2789 gen_check_sp_alignment(s);
2792 tcg_rn = cpu_reg_sp(s, rn);
2793 tcg_addr = tcg_temp_new_i64();
2794 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2796 for (r = 0; r < rpt; r++) {
2798 for (e = 0; e < elements; e++) {
2799 int tt = (rt + r) % 32;
2801 for (xs = 0; xs < selem; xs++) {
2803 do_vec_st(s, tt, e, tcg_addr, size);
2805 do_vec_ld(s, tt, e, tcg_addr, size);
2807 /* For non-quad operations, setting a slice of the low
2808 * 64 bits of the register clears the high 64 bits (in
2809 * the ARM ARM pseudocode this is implicit in the fact
2810 * that 'rval' is a 64 bit wide variable).
2811 * For quad operations, we might still need to zero the
2812 * high bits of SVE. We optimize by noticing that we only
2813 * need to do this the first time we touch a register.
2815 if (e == 0 && (r == 0 || xs == selem - 1)) {
2816 clear_vec_high(s, is_q, tt);
2819 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2826 int rm = extract32(insn, 16, 5);
2828 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2830 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2833 tcg_temp_free_i64(tcg_addr);
2836 /* AdvSIMD load/store single structure
2838 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2839 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2840 * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size | Rn | Rt |
2841 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2843 * AdvSIMD load/store single structure (post-indexed)
2845 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2846 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2847 * | 0 | Q | 0 0 1 1 0 1 1 | L R | Rm | opc | S | size | Rn | Rt |
2848 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2850 * Rt: first (or only) SIMD&FP register to be transferred
2851 * Rn: base address or SP
2852 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2853 * index = encoded in Q:S:size dependent on size
2855 * lane_size = encoded in R, opc
2856 * transfer width = encoded in opc, S, size
2858 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
2860 int rt = extract32(insn, 0, 5);
2861 int rn = extract32(insn, 5, 5);
2862 int size = extract32(insn, 10, 2);
2863 int S = extract32(insn, 12, 1);
2864 int opc = extract32(insn, 13, 3);
2865 int R = extract32(insn, 21, 1);
2866 int is_load = extract32(insn, 22, 1);
2867 int is_postidx = extract32(insn, 23, 1);
2868 int is_q = extract32(insn, 30, 1);
2870 int scale = extract32(opc, 1, 2);
2871 int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
2872 bool replicate = false;
2873 int index = is_q << 3 | S << 2 | size;
2875 TCGv_i64 tcg_addr, tcg_rn;
2879 if (!is_load || S) {
2880 unallocated_encoding(s);
2889 if (extract32(size, 0, 1)) {
2890 unallocated_encoding(s);
2896 if (extract32(size, 1, 1)) {
2897 unallocated_encoding(s);
2900 if (!extract32(size, 0, 1)) {
2904 unallocated_encoding(s);
2912 g_assert_not_reached();
2915 if (!fp_access_check(s)) {
2919 ebytes = 1 << scale;
2922 gen_check_sp_alignment(s);
2925 tcg_rn = cpu_reg_sp(s, rn);
2926 tcg_addr = tcg_temp_new_i64();
2927 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2929 for (xs = 0; xs < selem; xs++) {
2931 /* Load and replicate to all elements */
2933 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
2935 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr,
2936 get_mem_index(s), s->be_data + scale);
2939 mulconst = 0x0101010101010101ULL;
2942 mulconst = 0x0001000100010001ULL;
2945 mulconst = 0x0000000100000001ULL;
2951 g_assert_not_reached();
2954 tcg_gen_muli_i64(tcg_tmp, tcg_tmp, mulconst);
2956 write_vec_element(s, tcg_tmp, rt, 0, MO_64);
2958 write_vec_element(s, tcg_tmp, rt, 1, MO_64);
2960 tcg_temp_free_i64(tcg_tmp);
2961 clear_vec_high(s, is_q, rt);
2963 /* Load/store one element per register */
2965 do_vec_ld(s, rt, index, tcg_addr, scale);
2967 do_vec_st(s, rt, index, tcg_addr, scale);
2970 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2975 int rm = extract32(insn, 16, 5);
2977 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2979 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2982 tcg_temp_free_i64(tcg_addr);
2985 /* Loads and stores */
2986 static void disas_ldst(DisasContext *s, uint32_t insn)
2988 switch (extract32(insn, 24, 6)) {
2989 case 0x08: /* Load/store exclusive */
2990 disas_ldst_excl(s, insn);
2992 case 0x18: case 0x1c: /* Load register (literal) */
2993 disas_ld_lit(s, insn);
2995 case 0x28: case 0x29:
2996 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
2997 disas_ldst_pair(s, insn);
2999 case 0x38: case 0x39:
3000 case 0x3c: case 0x3d: /* Load/store register (all forms) */
3001 disas_ldst_reg(s, insn);
3003 case 0x0c: /* AdvSIMD load/store multiple structures */
3004 disas_ldst_multiple_struct(s, insn);
3006 case 0x0d: /* AdvSIMD load/store single structure */
3007 disas_ldst_single_struct(s, insn);
3010 unallocated_encoding(s);
3015 /* PC-rel. addressing
3016 * 31 30 29 28 24 23 5 4 0
3017 * +----+-------+-----------+-------------------+------+
3018 * | op | immlo | 1 0 0 0 0 | immhi | Rd |
3019 * +----+-------+-----------+-------------------+------+
3021 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
3023 unsigned int page, rd;
3027 page = extract32(insn, 31, 1);
3028 /* SignExtend(immhi:immlo) -> offset */
3029 offset = sextract64(insn, 5, 19);
3030 offset = offset << 2 | extract32(insn, 29, 2);
3031 rd = extract32(insn, 0, 5);
3035 /* ADRP (page based) */
3040 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
3044 * Add/subtract (immediate)
3046 * 31 30 29 28 24 23 22 21 10 9 5 4 0
3047 * +--+--+--+-----------+-----+-------------+-----+-----+
3048 * |sf|op| S| 1 0 0 0 1 |shift| imm12 | Rn | Rd |
3049 * +--+--+--+-----------+-----+-------------+-----+-----+
3051 * sf: 0 -> 32bit, 1 -> 64bit
3052 * op: 0 -> add , 1 -> sub
3054 * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
3056 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
3058 int rd = extract32(insn, 0, 5);
3059 int rn = extract32(insn, 5, 5);
3060 uint64_t imm = extract32(insn, 10, 12);
3061 int shift = extract32(insn, 22, 2);
3062 bool setflags = extract32(insn, 29, 1);
3063 bool sub_op = extract32(insn, 30, 1);
3064 bool is_64bit = extract32(insn, 31, 1);
3066 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3067 TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
3068 TCGv_i64 tcg_result;
3077 unallocated_encoding(s);
3081 tcg_result = tcg_temp_new_i64();
3084 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
3086 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
3089 TCGv_i64 tcg_imm = tcg_const_i64(imm);
3091 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
3093 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
3095 tcg_temp_free_i64(tcg_imm);
3099 tcg_gen_mov_i64(tcg_rd, tcg_result);
3101 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3104 tcg_temp_free_i64(tcg_result);
3107 /* The input should be a value in the bottom e bits (with higher
3108 * bits zero); returns that value replicated into every element
3109 * of size e in a 64 bit integer.
3111 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
3121 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
3122 static inline uint64_t bitmask64(unsigned int length)
3124 assert(length > 0 && length <= 64);
3125 return ~0ULL >> (64 - length);
3128 /* Simplified variant of pseudocode DecodeBitMasks() for the case where we
3129 * only require the wmask. Returns false if the imms/immr/immn are a reserved
3130 * value (ie should cause a guest UNDEF exception), and true if they are
3131 * valid, in which case the decoded bit pattern is written to result.
3133 static bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
3134 unsigned int imms, unsigned int immr)
3137 unsigned e, levels, s, r;
3140 assert(immn < 2 && imms < 64 && immr < 64);
3142 /* The bit patterns we create here are 64 bit patterns which
3143 * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
3144 * 64 bits each. Each element contains the same value: a run
3145 * of between 1 and e-1 non-zero bits, rotated within the
3146 * element by between 0 and e-1 bits.
3148 * The element size and run length are encoded into immn (1 bit)
3149 * and imms (6 bits) as follows:
3150 * 64 bit elements: immn = 1, imms = <length of run - 1>
3151 * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
3152 * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
3153 * 8 bit elements: immn = 0, imms = 110 : <length of run - 1>
3154 * 4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
3155 * 2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
3156 * Notice that immn = 0, imms = 11111x is the only combination
3157 * not covered by one of the above options; this is reserved.
3158 * Further, <length of run - 1> all-ones is a reserved pattern.
3160 * In all cases the rotation is by immr % e (and immr is 6 bits).
3163 /* First determine the element size */
3164 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
3166 /* This is the immn == 0, imms == 0x11111x case */
3176 /* <length of run - 1> mustn't be all-ones. */
3180 /* Create the value of one element: s+1 set bits rotated
3181 * by r within the element (which is e bits wide)...
3183 mask = bitmask64(s + 1);
3185 mask = (mask >> r) | (mask << (e - r));
3186 mask &= bitmask64(e);
3188 /* ...then replicate the element over the whole 64 bit value */
3189 mask = bitfield_replicate(mask, e);
3194 /* Logical (immediate)
3195 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
3196 * +----+-----+-------------+---+------+------+------+------+
3197 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms | Rn | Rd |
3198 * +----+-----+-------------+---+------+------+------+------+
3200 static void disas_logic_imm(DisasContext *s, uint32_t insn)
3202 unsigned int sf, opc, is_n, immr, imms, rn, rd;
3203 TCGv_i64 tcg_rd, tcg_rn;
3205 bool is_and = false;
3207 sf = extract32(insn, 31, 1);
3208 opc = extract32(insn, 29, 2);
3209 is_n = extract32(insn, 22, 1);
3210 immr = extract32(insn, 16, 6);
3211 imms = extract32(insn, 10, 6);
3212 rn = extract32(insn, 5, 5);
3213 rd = extract32(insn, 0, 5);
3216 unallocated_encoding(s);
3220 if (opc == 0x3) { /* ANDS */
3221 tcg_rd = cpu_reg(s, rd);
3223 tcg_rd = cpu_reg_sp(s, rd);
3225 tcg_rn = cpu_reg(s, rn);
3227 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
3228 /* some immediate field values are reserved */
3229 unallocated_encoding(s);
3234 wmask &= 0xffffffff;
3238 case 0x3: /* ANDS */
3240 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
3244 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
3247 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
3250 assert(FALSE); /* must handle all above */
3254 if (!sf && !is_and) {
3255 /* zero extend final result; we know we can skip this for AND
3256 * since the immediate had the high 32 bits clear.
3258 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3261 if (opc == 3) { /* ANDS */
3262 gen_logic_CC(sf, tcg_rd);
3267 * Move wide (immediate)
3269 * 31 30 29 28 23 22 21 20 5 4 0
3270 * +--+-----+-------------+-----+----------------+------+
3271 * |sf| opc | 1 0 0 1 0 1 | hw | imm16 | Rd |
3272 * +--+-----+-------------+-----+----------------+------+
3274 * sf: 0 -> 32 bit, 1 -> 64 bit
3275 * opc: 00 -> N, 10 -> Z, 11 -> K
3276 * hw: shift/16 (0,16, and sf only 32, 48)
3278 static void disas_movw_imm(DisasContext *s, uint32_t insn)
3280 int rd = extract32(insn, 0, 5);
3281 uint64_t imm = extract32(insn, 5, 16);
3282 int sf = extract32(insn, 31, 1);
3283 int opc = extract32(insn, 29, 2);
3284 int pos = extract32(insn, 21, 2) << 4;
3285 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3288 if (!sf && (pos >= 32)) {
3289 unallocated_encoding(s);
3303 tcg_gen_movi_i64(tcg_rd, imm);
3306 tcg_imm = tcg_const_i64(imm);
3307 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
3308 tcg_temp_free_i64(tcg_imm);
3310 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3314 unallocated_encoding(s);
3320 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
3321 * +----+-----+-------------+---+------+------+------+------+
3322 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms | Rn | Rd |
3323 * +----+-----+-------------+---+------+------+------+------+
3325 static void disas_bitfield(DisasContext *s, uint32_t insn)
3327 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
3328 TCGv_i64 tcg_rd, tcg_tmp;
3330 sf = extract32(insn, 31, 1);
3331 opc = extract32(insn, 29, 2);
3332 n = extract32(insn, 22, 1);
3333 ri = extract32(insn, 16, 6);
3334 si = extract32(insn, 10, 6);
3335 rn = extract32(insn, 5, 5);
3336 rd = extract32(insn, 0, 5);
3337 bitsize = sf ? 64 : 32;
3339 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
3340 unallocated_encoding(s);
3344 tcg_rd = cpu_reg(s, rd);
3346 /* Suppress the zero-extend for !sf. Since RI and SI are constrained
3347 to be smaller than bitsize, we'll never reference data outside the
3348 low 32-bits anyway. */
3349 tcg_tmp = read_cpu_reg(s, rn, 1);
3351 /* Recognize simple(r) extractions. */
3353 /* Wd<s-r:0> = Wn<s:r> */
3354 len = (si - ri) + 1;
3355 if (opc == 0) { /* SBFM: ASR, SBFX, SXTB, SXTH, SXTW */
3356 tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
3358 } else if (opc == 2) { /* UBFM: UBFX, LSR, UXTB, UXTH */
3359 tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
3362 /* opc == 1, BXFIL fall through to deposit */
3363 tcg_gen_extract_i64(tcg_tmp, tcg_tmp, ri, len);
3366 /* Handle the ri > si case with a deposit
3367 * Wd<32+s-r,32-r> = Wn<s:0>
3370 pos = (bitsize - ri) & (bitsize - 1);
3373 if (opc == 0 && len < ri) {
3374 /* SBFM: sign extend the destination field from len to fill
3375 the balance of the word. Let the deposit below insert all
3376 of those sign bits. */
3377 tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
3381 if (opc == 1) { /* BFM, BXFIL */
3382 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
3384 /* SBFM or UBFM: We start with zero, and we haven't modified
3385 any bits outside bitsize, therefore the zero-extension
3386 below is unneeded. */
3387 tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
3392 if (!sf) { /* zero extend final result */
3393 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3398 * 31 30 29 28 23 22 21 20 16 15 10 9 5 4 0
3399 * +----+------+-------------+---+----+------+--------+------+------+
3400 * | sf | op21 | 1 0 0 1 1 1 | N | o0 | Rm | imms | Rn | Rd |
3401 * +----+------+-------------+---+----+------+--------+------+------+
3403 static void disas_extract(DisasContext *s, uint32_t insn)
3405 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
3407 sf = extract32(insn, 31, 1);
3408 n = extract32(insn, 22, 1);
3409 rm = extract32(insn, 16, 5);
3410 imm = extract32(insn, 10, 6);
3411 rn = extract32(insn, 5, 5);
3412 rd = extract32(insn, 0, 5);
3413 op21 = extract32(insn, 29, 2);
3414 op0 = extract32(insn, 21, 1);
3415 bitsize = sf ? 64 : 32;
3417 if (sf != n || op21 || op0 || imm >= bitsize) {
3418 unallocated_encoding(s);
3420 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
3422 tcg_rd = cpu_reg(s, rd);
3424 if (unlikely(imm == 0)) {
3425 /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
3426 * so an extract from bit 0 is a special case.
3429 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
3431 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
3433 } else if (rm == rn) { /* ROR */
3434 tcg_rm = cpu_reg(s, rm);
3436 tcg_gen_rotri_i64(tcg_rd, tcg_rm, imm);
3438 TCGv_i32 tmp = tcg_temp_new_i32();
3439 tcg_gen_extrl_i64_i32(tmp, tcg_rm);
3440 tcg_gen_rotri_i32(tmp, tmp, imm);
3441 tcg_gen_extu_i32_i64(tcg_rd, tmp);
3442 tcg_temp_free_i32(tmp);
3445 tcg_rm = read_cpu_reg(s, rm, sf);
3446 tcg_rn = read_cpu_reg(s, rn, sf);
3447 tcg_gen_shri_i64(tcg_rm, tcg_rm, imm);
3448 tcg_gen_shli_i64(tcg_rn, tcg_rn, bitsize - imm);
3449 tcg_gen_or_i64(tcg_rd, tcg_rm, tcg_rn);
3451 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3457 /* Data processing - immediate */
3458 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
3460 switch (extract32(insn, 23, 6)) {
3461 case 0x20: case 0x21: /* PC-rel. addressing */
3462 disas_pc_rel_adr(s, insn);
3464 case 0x22: case 0x23: /* Add/subtract (immediate) */
3465 disas_add_sub_imm(s, insn);
3467 case 0x24: /* Logical (immediate) */
3468 disas_logic_imm(s, insn);
3470 case 0x25: /* Move wide (immediate) */
3471 disas_movw_imm(s, insn);
3473 case 0x26: /* Bitfield */
3474 disas_bitfield(s, insn);
3476 case 0x27: /* Extract */
3477 disas_extract(s, insn);
3480 unallocated_encoding(s);
3485 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
3486 * Note that it is the caller's responsibility to ensure that the
3487 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
3488 * mandated semantics for out of range shifts.
3490 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
3491 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
3493 switch (shift_type) {
3494 case A64_SHIFT_TYPE_LSL:
3495 tcg_gen_shl_i64(dst, src, shift_amount);
3497 case A64_SHIFT_TYPE_LSR:
3498 tcg_gen_shr_i64(dst, src, shift_amount);
3500 case A64_SHIFT_TYPE_ASR:
3502 tcg_gen_ext32s_i64(dst, src);
3504 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
3506 case A64_SHIFT_TYPE_ROR:
3508 tcg_gen_rotr_i64(dst, src, shift_amount);
3511 t0 = tcg_temp_new_i32();
3512 t1 = tcg_temp_new_i32();
3513 tcg_gen_extrl_i64_i32(t0, src);
3514 tcg_gen_extrl_i64_i32(t1, shift_amount);
3515 tcg_gen_rotr_i32(t0, t0, t1);
3516 tcg_gen_extu_i32_i64(dst, t0);
3517 tcg_temp_free_i32(t0);
3518 tcg_temp_free_i32(t1);
3522 assert(FALSE); /* all shift types should be handled */
3526 if (!sf) { /* zero extend final result */
3527 tcg_gen_ext32u_i64(dst, dst);
3531 /* Shift a TCGv src by immediate, put result in dst.
3532 * The shift amount must be in range (this should always be true as the
3533 * relevant instructions will UNDEF on bad shift immediates).
3535 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
3536 enum a64_shift_type shift_type, unsigned int shift_i)
3538 assert(shift_i < (sf ? 64 : 32));
3541 tcg_gen_mov_i64(dst, src);
3543 TCGv_i64 shift_const;
3545 shift_const = tcg_const_i64(shift_i);
3546 shift_reg(dst, src, sf, shift_type, shift_const);
3547 tcg_temp_free_i64(shift_const);
3551 /* Logical (shifted register)
3552 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
3553 * +----+-----+-----------+-------+---+------+--------+------+------+
3554 * | sf | opc | 0 1 0 1 0 | shift | N | Rm | imm6 | Rn | Rd |
3555 * +----+-----+-----------+-------+---+------+--------+------+------+
3557 static void disas_logic_reg(DisasContext *s, uint32_t insn)
3559 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
3560 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
3562 sf = extract32(insn, 31, 1);
3563 opc = extract32(insn, 29, 2);
3564 shift_type = extract32(insn, 22, 2);
3565 invert = extract32(insn, 21, 1);
3566 rm = extract32(insn, 16, 5);
3567 shift_amount = extract32(insn, 10, 6);
3568 rn = extract32(insn, 5, 5);
3569 rd = extract32(insn, 0, 5);
3571 if (!sf && (shift_amount & (1 << 5))) {
3572 unallocated_encoding(s);
3576 tcg_rd = cpu_reg(s, rd);
3578 if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
3579 /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
3580 * register-register MOV and MVN, so it is worth special casing.
3582 tcg_rm = cpu_reg(s, rm);
3584 tcg_gen_not_i64(tcg_rd, tcg_rm);
3586 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3590 tcg_gen_mov_i64(tcg_rd, tcg_rm);
3592 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
3598 tcg_rm = read_cpu_reg(s, rm, sf);
3601 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
3604 tcg_rn = cpu_reg(s, rn);
3606 switch (opc | (invert << 2)) {
3609 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
3612 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
3615 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
3619 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
3622 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
3625 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
3633 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3637 gen_logic_CC(sf, tcg_rd);
3642 * Add/subtract (extended register)
3644 * 31|30|29|28 24|23 22|21|20 16|15 13|12 10|9 5|4 0|
3645 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3646 * |sf|op| S| 0 1 0 1 1 | opt | 1| Rm |option| imm3 | Rn | Rd |
3647 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3649 * sf: 0 -> 32bit, 1 -> 64bit
3650 * op: 0 -> add , 1 -> sub
3653 * option: extension type (see DecodeRegExtend)
3654 * imm3: optional shift to Rm
3656 * Rd = Rn + LSL(extend(Rm), amount)
3658 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
3660 int rd = extract32(insn, 0, 5);
3661 int rn = extract32(insn, 5, 5);
3662 int imm3 = extract32(insn, 10, 3);
3663 int option = extract32(insn, 13, 3);
3664 int rm = extract32(insn, 16, 5);
3665 bool setflags = extract32(insn, 29, 1);
3666 bool sub_op = extract32(insn, 30, 1);
3667 bool sf = extract32(insn, 31, 1);
3669 TCGv_i64 tcg_rm, tcg_rn; /* temps */
3671 TCGv_i64 tcg_result;
3674 unallocated_encoding(s);
3678 /* non-flag setting ops may use SP */
3680 tcg_rd = cpu_reg_sp(s, rd);
3682 tcg_rd = cpu_reg(s, rd);
3684 tcg_rn = read_cpu_reg_sp(s, rn, sf);
3686 tcg_rm = read_cpu_reg(s, rm, sf);
3687 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
3689 tcg_result = tcg_temp_new_i64();
3693 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3695 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3699 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3701 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3706 tcg_gen_mov_i64(tcg_rd, tcg_result);
3708 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3711 tcg_temp_free_i64(tcg_result);
3715 * Add/subtract (shifted register)
3717 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
3718 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3719 * |sf|op| S| 0 1 0 1 1 |shift| 0| Rm | imm6 | Rn | Rd |
3720 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3722 * sf: 0 -> 32bit, 1 -> 64bit
3723 * op: 0 -> add , 1 -> sub
3725 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
3726 * imm6: Shift amount to apply to Rm before the add/sub
3728 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
3730 int rd = extract32(insn, 0, 5);
3731 int rn = extract32(insn, 5, 5);
3732 int imm6 = extract32(insn, 10, 6);
3733 int rm = extract32(insn, 16, 5);
3734 int shift_type = extract32(insn, 22, 2);
3735 bool setflags = extract32(insn, 29, 1);
3736 bool sub_op = extract32(insn, 30, 1);
3737 bool sf = extract32(insn, 31, 1);
3739 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3740 TCGv_i64 tcg_rn, tcg_rm;
3741 TCGv_i64 tcg_result;
3743 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
3744 unallocated_encoding(s);
3748 tcg_rn = read_cpu_reg(s, rn, sf);
3749 tcg_rm = read_cpu_reg(s, rm, sf);
3751 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
3753 tcg_result = tcg_temp_new_i64();
3757 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3759 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3763 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3765 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3770 tcg_gen_mov_i64(tcg_rd, tcg_result);
3772 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3775 tcg_temp_free_i64(tcg_result);
3778 /* Data-processing (3 source)
3780 * 31 30 29 28 24 23 21 20 16 15 14 10 9 5 4 0
3781 * +--+------+-----------+------+------+----+------+------+------+
3782 * |sf| op54 | 1 1 0 1 1 | op31 | Rm | o0 | Ra | Rn | Rd |
3783 * +--+------+-----------+------+------+----+------+------+------+
3785 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
3787 int rd = extract32(insn, 0, 5);
3788 int rn = extract32(insn, 5, 5);
3789 int ra = extract32(insn, 10, 5);
3790 int rm = extract32(insn, 16, 5);
3791 int op_id = (extract32(insn, 29, 3) << 4) |
3792 (extract32(insn, 21, 3) << 1) |
3793 extract32(insn, 15, 1);
3794 bool sf = extract32(insn, 31, 1);
3795 bool is_sub = extract32(op_id, 0, 1);
3796 bool is_high = extract32(op_id, 2, 1);
3797 bool is_signed = false;
3802 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
3804 case 0x42: /* SMADDL */
3805 case 0x43: /* SMSUBL */
3806 case 0x44: /* SMULH */
3809 case 0x0: /* MADD (32bit) */
3810 case 0x1: /* MSUB (32bit) */
3811 case 0x40: /* MADD (64bit) */
3812 case 0x41: /* MSUB (64bit) */
3813 case 0x4a: /* UMADDL */
3814 case 0x4b: /* UMSUBL */
3815 case 0x4c: /* UMULH */
3818 unallocated_encoding(s);
3823 TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
3824 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3825 TCGv_i64 tcg_rn = cpu_reg(s, rn);
3826 TCGv_i64 tcg_rm = cpu_reg(s, rm);
3829 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3831 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3834 tcg_temp_free_i64(low_bits);
3838 tcg_op1 = tcg_temp_new_i64();
3839 tcg_op2 = tcg_temp_new_i64();
3840 tcg_tmp = tcg_temp_new_i64();
3843 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
3844 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
3847 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
3848 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
3850 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
3851 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
3855 if (ra == 31 && !is_sub) {
3856 /* Special-case MADD with rA == XZR; it is the standard MUL alias */
3857 tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
3859 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
3861 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3863 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3868 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
3871 tcg_temp_free_i64(tcg_op1);
3872 tcg_temp_free_i64(tcg_op2);
3873 tcg_temp_free_i64(tcg_tmp);
3876 /* Add/subtract (with carry)
3877 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
3878 * +--+--+--+------------------------+------+---------+------+-----+
3879 * |sf|op| S| 1 1 0 1 0 0 0 0 | rm | opcode2 | Rn | Rd |
3880 * +--+--+--+------------------------+------+---------+------+-----+
3884 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
3886 unsigned int sf, op, setflags, rm, rn, rd;
3887 TCGv_i64 tcg_y, tcg_rn, tcg_rd;
3889 if (extract32(insn, 10, 6) != 0) {
3890 unallocated_encoding(s);
3894 sf = extract32(insn, 31, 1);
3895 op = extract32(insn, 30, 1);
3896 setflags = extract32(insn, 29, 1);
3897 rm = extract32(insn, 16, 5);
3898 rn = extract32(insn, 5, 5);
3899 rd = extract32(insn, 0, 5);
3901 tcg_rd = cpu_reg(s, rd);
3902 tcg_rn = cpu_reg(s, rn);
3905 tcg_y = new_tmp_a64(s);
3906 tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
3908 tcg_y = cpu_reg(s, rm);
3912 gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
3914 gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
3918 /* Conditional compare (immediate / register)
3919 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
3920 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3921 * |sf|op| S| 1 1 0 1 0 0 1 0 |imm5/rm | cond |i/r |o2| Rn |o3|nzcv |
3922 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3925 static void disas_cc(DisasContext *s, uint32_t insn)
3927 unsigned int sf, op, y, cond, rn, nzcv, is_imm;
3928 TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
3929 TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
3932 if (!extract32(insn, 29, 1)) {
3933 unallocated_encoding(s);
3936 if (insn & (1 << 10 | 1 << 4)) {
3937 unallocated_encoding(s);
3940 sf = extract32(insn, 31, 1);
3941 op = extract32(insn, 30, 1);
3942 is_imm = extract32(insn, 11, 1);
3943 y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
3944 cond = extract32(insn, 12, 4);
3945 rn = extract32(insn, 5, 5);
3946 nzcv = extract32(insn, 0, 4);
3948 /* Set T0 = !COND. */
3949 tcg_t0 = tcg_temp_new_i32();
3950 arm_test_cc(&c, cond);
3951 tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
3954 /* Load the arguments for the new comparison. */
3956 tcg_y = new_tmp_a64(s);
3957 tcg_gen_movi_i64(tcg_y, y);
3959 tcg_y = cpu_reg(s, y);
3961 tcg_rn = cpu_reg(s, rn);
3963 /* Set the flags for the new comparison. */
3964 tcg_tmp = tcg_temp_new_i64();
3966 gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3968 gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3970 tcg_temp_free_i64(tcg_tmp);
3972 /* If COND was false, force the flags to #nzcv. Compute two masks
3973 * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
3974 * For tcg hosts that support ANDC, we can make do with just T1.
3975 * In either case, allow the tcg optimizer to delete any unused mask.
3977 tcg_t1 = tcg_temp_new_i32();
3978 tcg_t2 = tcg_temp_new_i32();
3979 tcg_gen_neg_i32(tcg_t1, tcg_t0);
3980 tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
3982 if (nzcv & 8) { /* N */
3983 tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
3985 if (TCG_TARGET_HAS_andc_i32) {
3986 tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
3988 tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
3991 if (nzcv & 4) { /* Z */
3992 if (TCG_TARGET_HAS_andc_i32) {
3993 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
3995 tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
3998 tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
4000 if (nzcv & 2) { /* C */
4001 tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
4003 if (TCG_TARGET_HAS_andc_i32) {
4004 tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
4006 tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
4009 if (nzcv & 1) { /* V */
4010 tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
4012 if (TCG_TARGET_HAS_andc_i32) {
4013 tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
4015 tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
4018 tcg_temp_free_i32(tcg_t0);
4019 tcg_temp_free_i32(tcg_t1);
4020 tcg_temp_free_i32(tcg_t2);
4023 /* Conditional select
4024 * 31 30 29 28 21 20 16 15 12 11 10 9 5 4 0
4025 * +----+----+---+-----------------+------+------+-----+------+------+
4026 * | sf | op | S | 1 1 0 1 0 1 0 0 | Rm | cond | op2 | Rn | Rd |
4027 * +----+----+---+-----------------+------+------+-----+------+------+
4029 static void disas_cond_select(DisasContext *s, uint32_t insn)
4031 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
4032 TCGv_i64 tcg_rd, zero;
4035 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
4036 /* S == 1 or op2<1> == 1 */
4037 unallocated_encoding(s);
4040 sf = extract32(insn, 31, 1);
4041 else_inv = extract32(insn, 30, 1);
4042 rm = extract32(insn, 16, 5);
4043 cond = extract32(insn, 12, 4);
4044 else_inc = extract32(insn, 10, 1);
4045 rn = extract32(insn, 5, 5);
4046 rd = extract32(insn, 0, 5);
4048 tcg_rd = cpu_reg(s, rd);
4050 a64_test_cc(&c, cond);
4051 zero = tcg_const_i64(0);
4053 if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
4055 tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
4057 tcg_gen_neg_i64(tcg_rd, tcg_rd);
4060 TCGv_i64 t_true = cpu_reg(s, rn);
4061 TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
4062 if (else_inv && else_inc) {
4063 tcg_gen_neg_i64(t_false, t_false);
4064 } else if (else_inv) {
4065 tcg_gen_not_i64(t_false, t_false);
4066 } else if (else_inc) {
4067 tcg_gen_addi_i64(t_false, t_false, 1);
4069 tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
4072 tcg_temp_free_i64(zero);
4076 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4080 static void handle_clz(DisasContext *s, unsigned int sf,
4081 unsigned int rn, unsigned int rd)
4083 TCGv_i64 tcg_rd, tcg_rn;
4084 tcg_rd = cpu_reg(s, rd);
4085 tcg_rn = cpu_reg(s, rn);
4088 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
4090 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4091 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4092 tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
4093 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4094 tcg_temp_free_i32(tcg_tmp32);
4098 static void handle_cls(DisasContext *s, unsigned int sf,
4099 unsigned int rn, unsigned int rd)
4101 TCGv_i64 tcg_rd, tcg_rn;
4102 tcg_rd = cpu_reg(s, rd);
4103 tcg_rn = cpu_reg(s, rn);
4106 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
4108 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4109 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4110 tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
4111 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4112 tcg_temp_free_i32(tcg_tmp32);
4116 static void handle_rbit(DisasContext *s, unsigned int sf,
4117 unsigned int rn, unsigned int rd)
4119 TCGv_i64 tcg_rd, tcg_rn;
4120 tcg_rd = cpu_reg(s, rd);
4121 tcg_rn = cpu_reg(s, rn);
4124 gen_helper_rbit64(tcg_rd, tcg_rn);
4126 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4127 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4128 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
4129 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4130 tcg_temp_free_i32(tcg_tmp32);
4134 /* REV with sf==1, opcode==3 ("REV64") */
4135 static void handle_rev64(DisasContext *s, unsigned int sf,
4136 unsigned int rn, unsigned int rd)
4139 unallocated_encoding(s);
4142 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
4145 /* REV with sf==0, opcode==2
4146 * REV32 (sf==1, opcode==2)
4148 static void handle_rev32(DisasContext *s, unsigned int sf,
4149 unsigned int rn, unsigned int rd)
4151 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4154 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4155 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4157 /* bswap32_i64 requires zero high word */
4158 tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
4159 tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
4160 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
4161 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
4162 tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
4164 tcg_temp_free_i64(tcg_tmp);
4166 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
4167 tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
4171 /* REV16 (opcode==1) */
4172 static void handle_rev16(DisasContext *s, unsigned int sf,
4173 unsigned int rn, unsigned int rd)
4175 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4176 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4177 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4178 TCGv_i64 mask = tcg_const_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
4180 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
4181 tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
4182 tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
4183 tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
4184 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
4186 tcg_temp_free_i64(mask);
4187 tcg_temp_free_i64(tcg_tmp);
4190 /* Data-processing (1 source)
4191 * 31 30 29 28 21 20 16 15 10 9 5 4 0
4192 * +----+---+---+-----------------+---------+--------+------+------+
4193 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode | Rn | Rd |
4194 * +----+---+---+-----------------+---------+--------+------+------+
4196 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
4198 unsigned int sf, opcode, rn, rd;
4200 if (extract32(insn, 29, 1) || extract32(insn, 16, 5)) {
4201 unallocated_encoding(s);
4205 sf = extract32(insn, 31, 1);
4206 opcode = extract32(insn, 10, 6);
4207 rn = extract32(insn, 5, 5);
4208 rd = extract32(insn, 0, 5);
4212 handle_rbit(s, sf, rn, rd);
4215 handle_rev16(s, sf, rn, rd);
4218 handle_rev32(s, sf, rn, rd);
4221 handle_rev64(s, sf, rn, rd);
4224 handle_clz(s, sf, rn, rd);
4227 handle_cls(s, sf, rn, rd);
4232 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
4233 unsigned int rm, unsigned int rn, unsigned int rd)
4235 TCGv_i64 tcg_n, tcg_m, tcg_rd;
4236 tcg_rd = cpu_reg(s, rd);
4238 if (!sf && is_signed) {
4239 tcg_n = new_tmp_a64(s);
4240 tcg_m = new_tmp_a64(s);
4241 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
4242 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
4244 tcg_n = read_cpu_reg(s, rn, sf);
4245 tcg_m = read_cpu_reg(s, rm, sf);
4249 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
4251 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
4254 if (!sf) { /* zero extend final result */
4255 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4259 /* LSLV, LSRV, ASRV, RORV */
4260 static void handle_shift_reg(DisasContext *s,
4261 enum a64_shift_type shift_type, unsigned int sf,
4262 unsigned int rm, unsigned int rn, unsigned int rd)
4264 TCGv_i64 tcg_shift = tcg_temp_new_i64();
4265 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4266 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4268 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
4269 shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
4270 tcg_temp_free_i64(tcg_shift);
4273 /* CRC32[BHWX], CRC32C[BHWX] */
4274 static void handle_crc32(DisasContext *s,
4275 unsigned int sf, unsigned int sz, bool crc32c,
4276 unsigned int rm, unsigned int rn, unsigned int rd)
4278 TCGv_i64 tcg_acc, tcg_val;
4281 if (!arm_dc_feature(s, ARM_FEATURE_CRC)
4282 || (sf == 1 && sz != 3)
4283 || (sf == 0 && sz == 3)) {
4284 unallocated_encoding(s);
4289 tcg_val = cpu_reg(s, rm);
4303 g_assert_not_reached();
4305 tcg_val = new_tmp_a64(s);
4306 tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
4309 tcg_acc = cpu_reg(s, rn);
4310 tcg_bytes = tcg_const_i32(1 << sz);
4313 gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
4315 gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
4318 tcg_temp_free_i32(tcg_bytes);
4321 /* Data-processing (2 source)
4322 * 31 30 29 28 21 20 16 15 10 9 5 4 0
4323 * +----+---+---+-----------------+------+--------+------+------+
4324 * | sf | 0 | S | 1 1 0 1 0 1 1 0 | Rm | opcode | Rn | Rd |
4325 * +----+---+---+-----------------+------+--------+------+------+
4327 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
4329 unsigned int sf, rm, opcode, rn, rd;
4330 sf = extract32(insn, 31, 1);
4331 rm = extract32(insn, 16, 5);
4332 opcode = extract32(insn, 10, 6);
4333 rn = extract32(insn, 5, 5);
4334 rd = extract32(insn, 0, 5);
4336 if (extract32(insn, 29, 1)) {
4337 unallocated_encoding(s);
4343 handle_div(s, false, sf, rm, rn, rd);
4346 handle_div(s, true, sf, rm, rn, rd);
4349 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
4352 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
4355 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
4358 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
4367 case 23: /* CRC32 */
4369 int sz = extract32(opcode, 0, 2);
4370 bool crc32c = extract32(opcode, 2, 1);
4371 handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
4375 unallocated_encoding(s);
4380 /* Data processing - register */
4381 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
4383 switch (extract32(insn, 24, 5)) {
4384 case 0x0a: /* Logical (shifted register) */
4385 disas_logic_reg(s, insn);
4387 case 0x0b: /* Add/subtract */
4388 if (insn & (1 << 21)) { /* (extended register) */
4389 disas_add_sub_ext_reg(s, insn);
4391 disas_add_sub_reg(s, insn);
4394 case 0x1b: /* Data-processing (3 source) */
4395 disas_data_proc_3src(s, insn);
4398 switch (extract32(insn, 21, 3)) {
4399 case 0x0: /* Add/subtract (with carry) */
4400 disas_adc_sbc(s, insn);
4402 case 0x2: /* Conditional compare */
4403 disas_cc(s, insn); /* both imm and reg forms */
4405 case 0x4: /* Conditional select */
4406 disas_cond_select(s, insn);
4408 case 0x6: /* Data-processing */
4409 if (insn & (1 << 30)) { /* (1 source) */
4410 disas_data_proc_1src(s, insn);
4411 } else { /* (2 source) */
4412 disas_data_proc_2src(s, insn);
4416 unallocated_encoding(s);
4421 unallocated_encoding(s);
4426 static void handle_fp_compare(DisasContext *s, bool is_double,
4427 unsigned int rn, unsigned int rm,
4428 bool cmp_with_zero, bool signal_all_nans)
4430 TCGv_i64 tcg_flags = tcg_temp_new_i64();
4431 TCGv_ptr fpst = get_fpstatus_ptr(false);
4434 TCGv_i64 tcg_vn, tcg_vm;
4436 tcg_vn = read_fp_dreg(s, rn);
4437 if (cmp_with_zero) {
4438 tcg_vm = tcg_const_i64(0);
4440 tcg_vm = read_fp_dreg(s, rm);
4442 if (signal_all_nans) {
4443 gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4445 gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4447 tcg_temp_free_i64(tcg_vn);
4448 tcg_temp_free_i64(tcg_vm);
4450 TCGv_i32 tcg_vn, tcg_vm;
4452 tcg_vn = read_fp_sreg(s, rn);
4453 if (cmp_with_zero) {
4454 tcg_vm = tcg_const_i32(0);
4456 tcg_vm = read_fp_sreg(s, rm);
4458 if (signal_all_nans) {
4459 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4461 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4463 tcg_temp_free_i32(tcg_vn);
4464 tcg_temp_free_i32(tcg_vm);
4467 tcg_temp_free_ptr(fpst);
4469 gen_set_nzcv(tcg_flags);
4471 tcg_temp_free_i64(tcg_flags);
4474 /* Floating point compare
4475 * 31 30 29 28 24 23 22 21 20 16 15 14 13 10 9 5 4 0
4476 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
4477 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | op | 1 0 0 0 | Rn | op2 |
4478 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
4480 static void disas_fp_compare(DisasContext *s, uint32_t insn)
4482 unsigned int mos, type, rm, op, rn, opc, op2r;
4484 mos = extract32(insn, 29, 3);
4485 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4486 rm = extract32(insn, 16, 5);
4487 op = extract32(insn, 14, 2);
4488 rn = extract32(insn, 5, 5);
4489 opc = extract32(insn, 3, 2);
4490 op2r = extract32(insn, 0, 3);
4492 if (mos || op || op2r || type > 1) {
4493 unallocated_encoding(s);
4497 if (!fp_access_check(s)) {
4501 handle_fp_compare(s, type, rn, rm, opc & 1, opc & 2);
4504 /* Floating point conditional compare
4505 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
4506 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
4507 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 0 1 | Rn | op | nzcv |
4508 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
4510 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
4512 unsigned int mos, type, rm, cond, rn, op, nzcv;
4514 TCGLabel *label_continue = NULL;
4516 mos = extract32(insn, 29, 3);
4517 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4518 rm = extract32(insn, 16, 5);
4519 cond = extract32(insn, 12, 4);
4520 rn = extract32(insn, 5, 5);
4521 op = extract32(insn, 4, 1);
4522 nzcv = extract32(insn, 0, 4);
4524 if (mos || type > 1) {
4525 unallocated_encoding(s);
4529 if (!fp_access_check(s)) {
4533 if (cond < 0x0e) { /* not always */
4534 TCGLabel *label_match = gen_new_label();
4535 label_continue = gen_new_label();
4536 arm_gen_test_cc(cond, label_match);
4538 tcg_flags = tcg_const_i64(nzcv << 28);
4539 gen_set_nzcv(tcg_flags);
4540 tcg_temp_free_i64(tcg_flags);
4541 tcg_gen_br(label_continue);
4542 gen_set_label(label_match);
4545 handle_fp_compare(s, type, rn, rm, false, op);
4548 gen_set_label(label_continue);
4552 /* Floating point conditional select
4553 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
4554 * +---+---+---+-----------+------+---+------+------+-----+------+------+
4555 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 1 1 | Rn | Rd |
4556 * +---+---+---+-----------+------+---+------+------+-----+------+------+
4558 static void disas_fp_csel(DisasContext *s, uint32_t insn)
4560 unsigned int mos, type, rm, cond, rn, rd;
4561 TCGv_i64 t_true, t_false, t_zero;
4564 mos = extract32(insn, 29, 3);
4565 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4566 rm = extract32(insn, 16, 5);
4567 cond = extract32(insn, 12, 4);
4568 rn = extract32(insn, 5, 5);
4569 rd = extract32(insn, 0, 5);
4571 if (mos || type > 1) {
4572 unallocated_encoding(s);
4576 if (!fp_access_check(s)) {
4580 /* Zero extend sreg inputs to 64 bits now. */
4581 t_true = tcg_temp_new_i64();
4582 t_false = tcg_temp_new_i64();
4583 read_vec_element(s, t_true, rn, 0, type ? MO_64 : MO_32);
4584 read_vec_element(s, t_false, rm, 0, type ? MO_64 : MO_32);
4586 a64_test_cc(&c, cond);
4587 t_zero = tcg_const_i64(0);
4588 tcg_gen_movcond_i64(c.cond, t_true, c.value, t_zero, t_true, t_false);
4589 tcg_temp_free_i64(t_zero);
4590 tcg_temp_free_i64(t_false);
4593 /* Note that sregs write back zeros to the high bits,
4594 and we've already done the zero-extension. */
4595 write_fp_dreg(s, rd, t_true);
4596 tcg_temp_free_i64(t_true);
4599 /* Floating-point data-processing (1 source) - single precision */
4600 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
4606 fpst = get_fpstatus_ptr(false);
4607 tcg_op = read_fp_sreg(s, rn);
4608 tcg_res = tcg_temp_new_i32();
4611 case 0x0: /* FMOV */
4612 tcg_gen_mov_i32(tcg_res, tcg_op);
4614 case 0x1: /* FABS */
4615 gen_helper_vfp_abss(tcg_res, tcg_op);
4617 case 0x2: /* FNEG */
4618 gen_helper_vfp_negs(tcg_res, tcg_op);
4620 case 0x3: /* FSQRT */
4621 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
4623 case 0x8: /* FRINTN */
4624 case 0x9: /* FRINTP */
4625 case 0xa: /* FRINTM */
4626 case 0xb: /* FRINTZ */
4627 case 0xc: /* FRINTA */
4629 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4631 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
4632 gen_helper_rints(tcg_res, tcg_op, fpst);
4634 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
4635 tcg_temp_free_i32(tcg_rmode);
4638 case 0xe: /* FRINTX */
4639 gen_helper_rints_exact(tcg_res, tcg_op, fpst);
4641 case 0xf: /* FRINTI */
4642 gen_helper_rints(tcg_res, tcg_op, fpst);
4648 write_fp_sreg(s, rd, tcg_res);
4650 tcg_temp_free_ptr(fpst);
4651 tcg_temp_free_i32(tcg_op);
4652 tcg_temp_free_i32(tcg_res);
4655 /* Floating-point data-processing (1 source) - double precision */
4656 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
4663 case 0x0: /* FMOV */
4664 gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
4668 fpst = get_fpstatus_ptr(false);
4669 tcg_op = read_fp_dreg(s, rn);
4670 tcg_res = tcg_temp_new_i64();
4673 case 0x1: /* FABS */
4674 gen_helper_vfp_absd(tcg_res, tcg_op);
4676 case 0x2: /* FNEG */
4677 gen_helper_vfp_negd(tcg_res, tcg_op);
4679 case 0x3: /* FSQRT */
4680 gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
4682 case 0x8: /* FRINTN */
4683 case 0x9: /* FRINTP */
4684 case 0xa: /* FRINTM */
4685 case 0xb: /* FRINTZ */
4686 case 0xc: /* FRINTA */
4688 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4690 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
4691 gen_helper_rintd(tcg_res, tcg_op, fpst);
4693 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
4694 tcg_temp_free_i32(tcg_rmode);
4697 case 0xe: /* FRINTX */
4698 gen_helper_rintd_exact(tcg_res, tcg_op, fpst);
4700 case 0xf: /* FRINTI */
4701 gen_helper_rintd(tcg_res, tcg_op, fpst);
4707 write_fp_dreg(s, rd, tcg_res);
4709 tcg_temp_free_ptr(fpst);
4710 tcg_temp_free_i64(tcg_op);
4711 tcg_temp_free_i64(tcg_res);
4714 static void handle_fp_fcvt(DisasContext *s, int opcode,
4715 int rd, int rn, int dtype, int ntype)
4720 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4722 /* Single to double */
4723 TCGv_i64 tcg_rd = tcg_temp_new_i64();
4724 gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
4725 write_fp_dreg(s, rd, tcg_rd);
4726 tcg_temp_free_i64(tcg_rd);
4728 /* Single to half */
4729 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4730 gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, cpu_env);
4731 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4732 write_fp_sreg(s, rd, tcg_rd);
4733 tcg_temp_free_i32(tcg_rd);
4735 tcg_temp_free_i32(tcg_rn);
4740 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
4741 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4743 /* Double to single */
4744 gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
4746 /* Double to half */
4747 gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, cpu_env);
4748 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4750 write_fp_sreg(s, rd, tcg_rd);
4751 tcg_temp_free_i32(tcg_rd);
4752 tcg_temp_free_i64(tcg_rn);
4757 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4758 tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
4760 /* Half to single */
4761 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4762 gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, cpu_env);
4763 write_fp_sreg(s, rd, tcg_rd);
4764 tcg_temp_free_i32(tcg_rd);
4766 /* Half to double */
4767 TCGv_i64 tcg_rd = tcg_temp_new_i64();
4768 gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, cpu_env);
4769 write_fp_dreg(s, rd, tcg_rd);
4770 tcg_temp_free_i64(tcg_rd);
4772 tcg_temp_free_i32(tcg_rn);
4780 /* Floating point data-processing (1 source)
4781 * 31 30 29 28 24 23 22 21 20 15 14 10 9 5 4 0
4782 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4783 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 | Rn | Rd |
4784 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4786 static void disas_fp_1src(DisasContext *s, uint32_t insn)
4788 int type = extract32(insn, 22, 2);
4789 int opcode = extract32(insn, 15, 6);
4790 int rn = extract32(insn, 5, 5);
4791 int rd = extract32(insn, 0, 5);
4794 case 0x4: case 0x5: case 0x7:
4796 /* FCVT between half, single and double precision */
4797 int dtype = extract32(opcode, 0, 2);
4798 if (type == 2 || dtype == type) {
4799 unallocated_encoding(s);
4802 if (!fp_access_check(s)) {
4806 handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
4812 /* 32-to-32 and 64-to-64 ops */
4815 if (!fp_access_check(s)) {
4819 handle_fp_1src_single(s, opcode, rd, rn);
4822 if (!fp_access_check(s)) {
4826 handle_fp_1src_double(s, opcode, rd, rn);
4829 unallocated_encoding(s);
4833 unallocated_encoding(s);
4838 /* Floating-point data-processing (2 source) - single precision */
4839 static void handle_fp_2src_single(DisasContext *s, int opcode,
4840 int rd, int rn, int rm)
4847 tcg_res = tcg_temp_new_i32();
4848 fpst = get_fpstatus_ptr(false);
4849 tcg_op1 = read_fp_sreg(s, rn);
4850 tcg_op2 = read_fp_sreg(s, rm);
4853 case 0x0: /* FMUL */
4854 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4856 case 0x1: /* FDIV */
4857 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
4859 case 0x2: /* FADD */
4860 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
4862 case 0x3: /* FSUB */
4863 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
4865 case 0x4: /* FMAX */
4866 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
4868 case 0x5: /* FMIN */
4869 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
4871 case 0x6: /* FMAXNM */
4872 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
4874 case 0x7: /* FMINNM */
4875 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
4877 case 0x8: /* FNMUL */
4878 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4879 gen_helper_vfp_negs(tcg_res, tcg_res);
4883 write_fp_sreg(s, rd, tcg_res);
4885 tcg_temp_free_ptr(fpst);
4886 tcg_temp_free_i32(tcg_op1);
4887 tcg_temp_free_i32(tcg_op2);
4888 tcg_temp_free_i32(tcg_res);
4891 /* Floating-point data-processing (2 source) - double precision */
4892 static void handle_fp_2src_double(DisasContext *s, int opcode,
4893 int rd, int rn, int rm)
4900 tcg_res = tcg_temp_new_i64();
4901 fpst = get_fpstatus_ptr(false);
4902 tcg_op1 = read_fp_dreg(s, rn);
4903 tcg_op2 = read_fp_dreg(s, rm);
4906 case 0x0: /* FMUL */
4907 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4909 case 0x1: /* FDIV */
4910 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
4912 case 0x2: /* FADD */
4913 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
4915 case 0x3: /* FSUB */
4916 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
4918 case 0x4: /* FMAX */
4919 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
4921 case 0x5: /* FMIN */
4922 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
4924 case 0x6: /* FMAXNM */
4925 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4927 case 0x7: /* FMINNM */
4928 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4930 case 0x8: /* FNMUL */
4931 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4932 gen_helper_vfp_negd(tcg_res, tcg_res);
4936 write_fp_dreg(s, rd, tcg_res);
4938 tcg_temp_free_ptr(fpst);
4939 tcg_temp_free_i64(tcg_op1);
4940 tcg_temp_free_i64(tcg_op2);
4941 tcg_temp_free_i64(tcg_res);
4944 /* Floating point data-processing (2 source)
4945 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
4946 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4947 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | opcode | 1 0 | Rn | Rd |
4948 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4950 static void disas_fp_2src(DisasContext *s, uint32_t insn)
4952 int type = extract32(insn, 22, 2);
4953 int rd = extract32(insn, 0, 5);
4954 int rn = extract32(insn, 5, 5);
4955 int rm = extract32(insn, 16, 5);
4956 int opcode = extract32(insn, 12, 4);
4959 unallocated_encoding(s);
4965 if (!fp_access_check(s)) {
4968 handle_fp_2src_single(s, opcode, rd, rn, rm);
4971 if (!fp_access_check(s)) {
4974 handle_fp_2src_double(s, opcode, rd, rn, rm);
4977 unallocated_encoding(s);
4981 /* Floating-point data-processing (3 source) - single precision */
4982 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
4983 int rd, int rn, int rm, int ra)
4985 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
4986 TCGv_i32 tcg_res = tcg_temp_new_i32();
4987 TCGv_ptr fpst = get_fpstatus_ptr(false);
4989 tcg_op1 = read_fp_sreg(s, rn);
4990 tcg_op2 = read_fp_sreg(s, rm);
4991 tcg_op3 = read_fp_sreg(s, ra);
4993 /* These are fused multiply-add, and must be done as one
4994 * floating point operation with no rounding between the
4995 * multiplication and addition steps.
4996 * NB that doing the negations here as separate steps is
4997 * correct : an input NaN should come out with its sign bit
4998 * flipped if it is a negated-input.
5001 gen_helper_vfp_negs(tcg_op3, tcg_op3);
5005 gen_helper_vfp_negs(tcg_op1, tcg_op1);
5008 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
5010 write_fp_sreg(s, rd, tcg_res);
5012 tcg_temp_free_ptr(fpst);
5013 tcg_temp_free_i32(tcg_op1);
5014 tcg_temp_free_i32(tcg_op2);
5015 tcg_temp_free_i32(tcg_op3);
5016 tcg_temp_free_i32(tcg_res);
5019 /* Floating-point data-processing (3 source) - double precision */
5020 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
5021 int rd, int rn, int rm, int ra)
5023 TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
5024 TCGv_i64 tcg_res = tcg_temp_new_i64();
5025 TCGv_ptr fpst = get_fpstatus_ptr(false);
5027 tcg_op1 = read_fp_dreg(s, rn);
5028 tcg_op2 = read_fp_dreg(s, rm);
5029 tcg_op3 = read_fp_dreg(s, ra);
5031 /* These are fused multiply-add, and must be done as one
5032 * floating point operation with no rounding between the
5033 * multiplication and addition steps.
5034 * NB that doing the negations here as separate steps is
5035 * correct : an input NaN should come out with its sign bit
5036 * flipped if it is a negated-input.
5039 gen_helper_vfp_negd(tcg_op3, tcg_op3);
5043 gen_helper_vfp_negd(tcg_op1, tcg_op1);
5046 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
5048 write_fp_dreg(s, rd, tcg_res);
5050 tcg_temp_free_ptr(fpst);
5051 tcg_temp_free_i64(tcg_op1);
5052 tcg_temp_free_i64(tcg_op2);
5053 tcg_temp_free_i64(tcg_op3);
5054 tcg_temp_free_i64(tcg_res);
5057 /* Floating point data-processing (3 source)
5058 * 31 30 29 28 24 23 22 21 20 16 15 14 10 9 5 4 0
5059 * +---+---+---+-----------+------+----+------+----+------+------+------+
5060 * | M | 0 | S | 1 1 1 1 1 | type | o1 | Rm | o0 | Ra | Rn | Rd |
5061 * +---+---+---+-----------+------+----+------+----+------+------+------+
5063 static void disas_fp_3src(DisasContext *s, uint32_t insn)
5065 int type = extract32(insn, 22, 2);
5066 int rd = extract32(insn, 0, 5);
5067 int rn = extract32(insn, 5, 5);
5068 int ra = extract32(insn, 10, 5);
5069 int rm = extract32(insn, 16, 5);
5070 bool o0 = extract32(insn, 15, 1);
5071 bool o1 = extract32(insn, 21, 1);
5075 if (!fp_access_check(s)) {
5078 handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
5081 if (!fp_access_check(s)) {
5084 handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
5087 unallocated_encoding(s);
5091 /* The imm8 encodes the sign bit, enough bits to represent an exponent in
5092 * the range 01....1xx to 10....0xx, and the most significant 4 bits of
5093 * the mantissa; see VFPExpandImm() in the v8 ARM ARM.
5095 static uint64_t vfp_expand_imm(int size, uint8_t imm8)
5101 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
5102 (extract32(imm8, 6, 1) ? 0x3fc0 : 0x4000) |
5103 extract32(imm8, 0, 6);
5107 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
5108 (extract32(imm8, 6, 1) ? 0x3e00 : 0x4000) |
5109 (extract32(imm8, 0, 6) << 3);
5113 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
5114 (extract32(imm8, 6, 1) ? 0x3000 : 0x4000) |
5115 (extract32(imm8, 0, 6) << 6);
5118 g_assert_not_reached();
5123 /* Floating point immediate
5124 * 31 30 29 28 24 23 22 21 20 13 12 10 9 5 4 0
5125 * +---+---+---+-----------+------+---+------------+-------+------+------+
5126 * | M | 0 | S | 1 1 1 1 0 | type | 1 | imm8 | 1 0 0 | imm5 | Rd |
5127 * +---+---+---+-----------+------+---+------------+-------+------+------+
5129 static void disas_fp_imm(DisasContext *s, uint32_t insn)
5131 int rd = extract32(insn, 0, 5);
5132 int imm8 = extract32(insn, 13, 8);
5133 int is_double = extract32(insn, 22, 2);
5137 if (is_double > 1) {
5138 unallocated_encoding(s);
5142 if (!fp_access_check(s)) {
5146 imm = vfp_expand_imm(MO_32 + is_double, imm8);
5148 tcg_res = tcg_const_i64(imm);
5149 write_fp_dreg(s, rd, tcg_res);
5150 tcg_temp_free_i64(tcg_res);
5153 /* Handle floating point <=> fixed point conversions. Note that we can
5154 * also deal with fp <=> integer conversions as a special case (scale == 64)
5155 * OPTME: consider handling that special case specially or at least skipping
5156 * the call to scalbn in the helpers for zero shifts.
5158 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
5159 bool itof, int rmode, int scale, int sf, int type)
5161 bool is_signed = !(opcode & 1);
5162 bool is_double = type;
5163 TCGv_ptr tcg_fpstatus;
5166 tcg_fpstatus = get_fpstatus_ptr(false);
5168 tcg_shift = tcg_const_i32(64 - scale);
5171 TCGv_i64 tcg_int = cpu_reg(s, rn);
5173 TCGv_i64 tcg_extend = new_tmp_a64(s);
5176 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
5178 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
5181 tcg_int = tcg_extend;
5185 TCGv_i64 tcg_double = tcg_temp_new_i64();
5187 gen_helper_vfp_sqtod(tcg_double, tcg_int,
5188 tcg_shift, tcg_fpstatus);
5190 gen_helper_vfp_uqtod(tcg_double, tcg_int,
5191 tcg_shift, tcg_fpstatus);
5193 write_fp_dreg(s, rd, tcg_double);
5194 tcg_temp_free_i64(tcg_double);
5196 TCGv_i32 tcg_single = tcg_temp_new_i32();
5198 gen_helper_vfp_sqtos(tcg_single, tcg_int,
5199 tcg_shift, tcg_fpstatus);
5201 gen_helper_vfp_uqtos(tcg_single, tcg_int,
5202 tcg_shift, tcg_fpstatus);
5204 write_fp_sreg(s, rd, tcg_single);
5205 tcg_temp_free_i32(tcg_single);
5208 TCGv_i64 tcg_int = cpu_reg(s, rd);
5211 if (extract32(opcode, 2, 1)) {
5212 /* There are too many rounding modes to all fit into rmode,
5213 * so FCVTA[US] is a special case.
5215 rmode = FPROUNDING_TIEAWAY;
5218 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
5220 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
5223 TCGv_i64 tcg_double = read_fp_dreg(s, rn);
5226 gen_helper_vfp_tosld(tcg_int, tcg_double,
5227 tcg_shift, tcg_fpstatus);
5229 gen_helper_vfp_tosqd(tcg_int, tcg_double,
5230 tcg_shift, tcg_fpstatus);
5234 gen_helper_vfp_tould(tcg_int, tcg_double,
5235 tcg_shift, tcg_fpstatus);
5237 gen_helper_vfp_touqd(tcg_int, tcg_double,
5238 tcg_shift, tcg_fpstatus);
5241 tcg_temp_free_i64(tcg_double);
5243 TCGv_i32 tcg_single = read_fp_sreg(s, rn);
5246 gen_helper_vfp_tosqs(tcg_int, tcg_single,
5247 tcg_shift, tcg_fpstatus);
5249 gen_helper_vfp_touqs(tcg_int, tcg_single,
5250 tcg_shift, tcg_fpstatus);
5253 TCGv_i32 tcg_dest = tcg_temp_new_i32();
5255 gen_helper_vfp_tosls(tcg_dest, tcg_single,
5256 tcg_shift, tcg_fpstatus);
5258 gen_helper_vfp_touls(tcg_dest, tcg_single,
5259 tcg_shift, tcg_fpstatus);
5261 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
5262 tcg_temp_free_i32(tcg_dest);
5264 tcg_temp_free_i32(tcg_single);
5267 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
5268 tcg_temp_free_i32(tcg_rmode);
5271 tcg_gen_ext32u_i64(tcg_int, tcg_int);
5275 tcg_temp_free_ptr(tcg_fpstatus);
5276 tcg_temp_free_i32(tcg_shift);
5279 /* Floating point <-> fixed point conversions
5280 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
5281 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
5282 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale | Rn | Rd |
5283 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
5285 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
5287 int rd = extract32(insn, 0, 5);
5288 int rn = extract32(insn, 5, 5);
5289 int scale = extract32(insn, 10, 6);
5290 int opcode = extract32(insn, 16, 3);
5291 int rmode = extract32(insn, 19, 2);
5292 int type = extract32(insn, 22, 2);
5293 bool sbit = extract32(insn, 29, 1);
5294 bool sf = extract32(insn, 31, 1);
5297 if (sbit || (type > 1)
5298 || (!sf && scale < 32)) {
5299 unallocated_encoding(s);
5303 switch ((rmode << 3) | opcode) {
5304 case 0x2: /* SCVTF */
5305 case 0x3: /* UCVTF */
5308 case 0x18: /* FCVTZS */
5309 case 0x19: /* FCVTZU */
5313 unallocated_encoding(s);
5317 if (!fp_access_check(s)) {
5321 handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
5324 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
5326 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
5327 * without conversion.
5331 TCGv_i64 tcg_rn = cpu_reg(s, rn);
5337 TCGv_i64 tmp = tcg_temp_new_i64();
5338 tcg_gen_ext32u_i64(tmp, tcg_rn);
5339 tcg_gen_st_i64(tmp, cpu_env, fp_reg_offset(s, rd, MO_64));
5340 tcg_gen_movi_i64(tmp, 0);
5341 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(s, rd));
5342 tcg_temp_free_i64(tmp);
5348 TCGv_i64 tmp = tcg_const_i64(0);
5349 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_offset(s, rd, MO_64));
5350 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(s, rd));
5351 tcg_temp_free_i64(tmp);
5355 /* 64 bit to top half. */
5356 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
5360 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5365 tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
5369 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
5372 /* 64 bits from top half */
5373 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
5379 /* Floating point <-> integer conversions
5380 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
5381 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
5382 * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
5383 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
5385 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
5387 int rd = extract32(insn, 0, 5);
5388 int rn = extract32(insn, 5, 5);
5389 int opcode = extract32(insn, 16, 3);
5390 int rmode = extract32(insn, 19, 2);
5391 int type = extract32(insn, 22, 2);
5392 bool sbit = extract32(insn, 29, 1);
5393 bool sf = extract32(insn, 31, 1);
5396 unallocated_encoding(s);
5402 bool itof = opcode & 1;
5405 unallocated_encoding(s);
5409 switch (sf << 3 | type << 1 | rmode) {
5410 case 0x0: /* 32 bit */
5411 case 0xa: /* 64 bit */
5412 case 0xd: /* 64 bit to top half of quad */
5415 /* all other sf/type/rmode combinations are invalid */
5416 unallocated_encoding(s);
5420 if (!fp_access_check(s)) {
5423 handle_fmov(s, rd, rn, type, itof);
5425 /* actual FP conversions */
5426 bool itof = extract32(opcode, 1, 1);
5428 if (type > 1 || (rmode != 0 && opcode > 1)) {
5429 unallocated_encoding(s);
5433 if (!fp_access_check(s)) {
5436 handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
5440 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
5441 * 31 30 29 28 25 24 0
5442 * +---+---+---+---------+-----------------------------+
5443 * | | 0 | | 1 1 1 1 | |
5444 * +---+---+---+---------+-----------------------------+
5446 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
5448 if (extract32(insn, 24, 1)) {
5449 /* Floating point data-processing (3 source) */
5450 disas_fp_3src(s, insn);
5451 } else if (extract32(insn, 21, 1) == 0) {
5452 /* Floating point to fixed point conversions */
5453 disas_fp_fixed_conv(s, insn);
5455 switch (extract32(insn, 10, 2)) {
5457 /* Floating point conditional compare */
5458 disas_fp_ccomp(s, insn);
5461 /* Floating point data-processing (2 source) */
5462 disas_fp_2src(s, insn);
5465 /* Floating point conditional select */
5466 disas_fp_csel(s, insn);
5469 switch (ctz32(extract32(insn, 12, 4))) {
5470 case 0: /* [15:12] == xxx1 */
5471 /* Floating point immediate */
5472 disas_fp_imm(s, insn);
5474 case 1: /* [15:12] == xx10 */
5475 /* Floating point compare */
5476 disas_fp_compare(s, insn);
5478 case 2: /* [15:12] == x100 */
5479 /* Floating point data-processing (1 source) */
5480 disas_fp_1src(s, insn);
5482 case 3: /* [15:12] == 1000 */
5483 unallocated_encoding(s);
5485 default: /* [15:12] == 0000 */
5486 /* Floating point <-> integer conversions */
5487 disas_fp_int_conv(s, insn);
5495 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
5498 /* Extract 64 bits from the middle of two concatenated 64 bit
5499 * vector register slices left:right. The extracted bits start
5500 * at 'pos' bits into the right (least significant) side.
5501 * We return the result in tcg_right, and guarantee not to
5504 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5505 assert(pos > 0 && pos < 64);
5507 tcg_gen_shri_i64(tcg_right, tcg_right, pos);
5508 tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
5509 tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
5511 tcg_temp_free_i64(tcg_tmp);
5515 * 31 30 29 24 23 22 21 20 16 15 14 11 10 9 5 4 0
5516 * +---+---+-------------+-----+---+------+---+------+---+------+------+
5517 * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 | Rm | 0 | imm4 | 0 | Rn | Rd |
5518 * +---+---+-------------+-----+---+------+---+------+---+------+------+
5520 static void disas_simd_ext(DisasContext *s, uint32_t insn)
5522 int is_q = extract32(insn, 30, 1);
5523 int op2 = extract32(insn, 22, 2);
5524 int imm4 = extract32(insn, 11, 4);
5525 int rm = extract32(insn, 16, 5);
5526 int rn = extract32(insn, 5, 5);
5527 int rd = extract32(insn, 0, 5);
5528 int pos = imm4 << 3;
5529 TCGv_i64 tcg_resl, tcg_resh;
5531 if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
5532 unallocated_encoding(s);
5536 if (!fp_access_check(s)) {
5540 tcg_resh = tcg_temp_new_i64();
5541 tcg_resl = tcg_temp_new_i64();
5543 /* Vd gets bits starting at pos bits into Vm:Vn. This is
5544 * either extracting 128 bits from a 128:128 concatenation, or
5545 * extracting 64 bits from a 64:64 concatenation.
5548 read_vec_element(s, tcg_resl, rn, 0, MO_64);
5550 read_vec_element(s, tcg_resh, rm, 0, MO_64);
5551 do_ext64(s, tcg_resh, tcg_resl, pos);
5553 tcg_gen_movi_i64(tcg_resh, 0);
5560 EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
5561 EltPosns *elt = eltposns;
5568 read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
5570 read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
5573 do_ext64(s, tcg_resh, tcg_resl, pos);
5574 tcg_hh = tcg_temp_new_i64();
5575 read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
5576 do_ext64(s, tcg_hh, tcg_resh, pos);
5577 tcg_temp_free_i64(tcg_hh);
5581 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5582 tcg_temp_free_i64(tcg_resl);
5583 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5584 tcg_temp_free_i64(tcg_resh);
5588 * 31 30 29 24 23 22 21 20 16 15 14 13 12 11 10 9 5 4 0
5589 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
5590 * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 | Rm | 0 | len | op | 0 0 | Rn | Rd |
5591 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
5593 static void disas_simd_tb(DisasContext *s, uint32_t insn)
5595 int op2 = extract32(insn, 22, 2);
5596 int is_q = extract32(insn, 30, 1);
5597 int rm = extract32(insn, 16, 5);
5598 int rn = extract32(insn, 5, 5);
5599 int rd = extract32(insn, 0, 5);
5600 int is_tblx = extract32(insn, 12, 1);
5601 int len = extract32(insn, 13, 2);
5602 TCGv_i64 tcg_resl, tcg_resh, tcg_idx;
5603 TCGv_i32 tcg_regno, tcg_numregs;
5606 unallocated_encoding(s);
5610 if (!fp_access_check(s)) {
5614 /* This does a table lookup: for every byte element in the input
5615 * we index into a table formed from up to four vector registers,
5616 * and then the output is the result of the lookups. Our helper
5617 * function does the lookup operation for a single 64 bit part of
5620 tcg_resl = tcg_temp_new_i64();
5621 tcg_resh = tcg_temp_new_i64();
5624 read_vec_element(s, tcg_resl, rd, 0, MO_64);
5626 tcg_gen_movi_i64(tcg_resl, 0);
5628 if (is_tblx && is_q) {
5629 read_vec_element(s, tcg_resh, rd, 1, MO_64);
5631 tcg_gen_movi_i64(tcg_resh, 0);
5634 tcg_idx = tcg_temp_new_i64();
5635 tcg_regno = tcg_const_i32(rn);
5636 tcg_numregs = tcg_const_i32(len + 1);
5637 read_vec_element(s, tcg_idx, rm, 0, MO_64);
5638 gen_helper_simd_tbl(tcg_resl, cpu_env, tcg_resl, tcg_idx,
5639 tcg_regno, tcg_numregs);
5641 read_vec_element(s, tcg_idx, rm, 1, MO_64);
5642 gen_helper_simd_tbl(tcg_resh, cpu_env, tcg_resh, tcg_idx,
5643 tcg_regno, tcg_numregs);
5645 tcg_temp_free_i64(tcg_idx);
5646 tcg_temp_free_i32(tcg_regno);
5647 tcg_temp_free_i32(tcg_numregs);
5649 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5650 tcg_temp_free_i64(tcg_resl);
5651 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5652 tcg_temp_free_i64(tcg_resh);
5656 * 31 30 29 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
5657 * +---+---+-------------+------+---+------+---+------------------+------+
5658 * | 0 | Q | 0 0 1 1 1 0 | size | 0 | Rm | 0 | opc | 1 0 | Rn | Rd |
5659 * +---+---+-------------+------+---+------+---+------------------+------+
5661 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
5663 int rd = extract32(insn, 0, 5);
5664 int rn = extract32(insn, 5, 5);
5665 int rm = extract32(insn, 16, 5);
5666 int size = extract32(insn, 22, 2);
5667 /* opc field bits [1:0] indicate ZIP/UZP/TRN;
5668 * bit 2 indicates 1 vs 2 variant of the insn.
5670 int opcode = extract32(insn, 12, 2);
5671 bool part = extract32(insn, 14, 1);
5672 bool is_q = extract32(insn, 30, 1);
5673 int esize = 8 << size;
5675 int datasize = is_q ? 128 : 64;
5676 int elements = datasize / esize;
5677 TCGv_i64 tcg_res, tcg_resl, tcg_resh;
5679 if (opcode == 0 || (size == 3 && !is_q)) {
5680 unallocated_encoding(s);
5684 if (!fp_access_check(s)) {
5688 tcg_resl = tcg_const_i64(0);
5689 tcg_resh = tcg_const_i64(0);
5690 tcg_res = tcg_temp_new_i64();
5692 for (i = 0; i < elements; i++) {
5694 case 1: /* UZP1/2 */
5696 int midpoint = elements / 2;
5698 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
5700 read_vec_element(s, tcg_res, rm,
5701 2 * (i - midpoint) + part, size);
5705 case 2: /* TRN1/2 */
5707 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
5709 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
5712 case 3: /* ZIP1/2 */
5714 int base = part * elements / 2;
5716 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
5718 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
5723 g_assert_not_reached();
5728 tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
5729 tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
5731 tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
5732 tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
5736 tcg_temp_free_i64(tcg_res);
5738 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5739 tcg_temp_free_i64(tcg_resl);
5740 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5741 tcg_temp_free_i64(tcg_resh);
5745 * do_reduction_op helper
5747 * This mirrors the Reduce() pseudocode in the ARM ARM. It is
5748 * important for correct NaN propagation that we do these
5749 * operations in exactly the order specified by the pseudocode.
5751 * This is a recursive function, TCG temps should be freed by the
5752 * calling function once it is done with the values.
5754 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
5755 int esize, int size, int vmap, TCGv_ptr fpst)
5757 if (esize == size) {
5759 TCGMemOp msize = esize == 16 ? MO_16 : MO_32;
5762 /* We should have one register left here */
5763 assert(ctpop8(vmap) == 1);
5764 element = ctz32(vmap);
5765 assert(element < 8);
5767 tcg_elem = tcg_temp_new_i32();
5768 read_vec_element_i32(s, tcg_elem, rn, element, msize);
5771 int bits = size / 2;
5772 int shift = ctpop8(vmap) / 2;
5773 int vmap_lo = (vmap >> shift) & vmap;
5774 int vmap_hi = (vmap & ~vmap_lo);
5775 TCGv_i32 tcg_hi, tcg_lo, tcg_res;
5777 tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
5778 tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
5779 tcg_res = tcg_temp_new_i32();
5782 case 0x0c: /* fmaxnmv half-precision */
5783 gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
5785 case 0x0f: /* fmaxv half-precision */
5786 gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
5788 case 0x1c: /* fminnmv half-precision */
5789 gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
5791 case 0x1f: /* fminv half-precision */
5792 gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
5794 case 0x2c: /* fmaxnmv */
5795 gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
5797 case 0x2f: /* fmaxv */
5798 gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
5800 case 0x3c: /* fminnmv */
5801 gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
5803 case 0x3f: /* fminv */
5804 gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
5807 g_assert_not_reached();
5810 tcg_temp_free_i32(tcg_hi);
5811 tcg_temp_free_i32(tcg_lo);
5816 /* AdvSIMD across lanes
5817 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
5818 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5819 * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
5820 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5822 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
5824 int rd = extract32(insn, 0, 5);
5825 int rn = extract32(insn, 5, 5);
5826 int size = extract32(insn, 22, 2);
5827 int opcode = extract32(insn, 12, 5);
5828 bool is_q = extract32(insn, 30, 1);
5829 bool is_u = extract32(insn, 29, 1);
5831 bool is_min = false;
5835 TCGv_i64 tcg_res, tcg_elt;
5838 case 0x1b: /* ADDV */
5840 unallocated_encoding(s);
5844 case 0x3: /* SADDLV, UADDLV */
5845 case 0xa: /* SMAXV, UMAXV */
5846 case 0x1a: /* SMINV, UMINV */
5847 if (size == 3 || (size == 2 && !is_q)) {
5848 unallocated_encoding(s);
5852 case 0xc: /* FMAXNMV, FMINNMV */
5853 case 0xf: /* FMAXV, FMINV */
5854 /* Bit 1 of size field encodes min vs max and the actual size
5855 * depends on the encoding of the U bit. If not set (and FP16
5856 * enabled) then we do half-precision float instead of single
5859 is_min = extract32(size, 1, 1);
5861 if (!is_u && arm_dc_feature(s, ARM_FEATURE_V8_FP16)) {
5863 } else if (!is_u || !is_q || extract32(size, 0, 1)) {
5864 unallocated_encoding(s);
5871 unallocated_encoding(s);
5875 if (!fp_access_check(s)) {
5880 elements = (is_q ? 128 : 64) / esize;
5882 tcg_res = tcg_temp_new_i64();
5883 tcg_elt = tcg_temp_new_i64();
5885 /* These instructions operate across all lanes of a vector
5886 * to produce a single result. We can guarantee that a 64
5887 * bit intermediate is sufficient:
5888 * + for [US]ADDLV the maximum element size is 32 bits, and
5889 * the result type is 64 bits
5890 * + for FMAX*V, FMIN*V, ADDV the intermediate type is the
5891 * same as the element size, which is 32 bits at most
5892 * For the integer operations we can choose to work at 64
5893 * or 32 bits and truncate at the end; for simplicity
5894 * we use 64 bits always. The floating point
5895 * ops do require 32 bit intermediates, though.
5898 read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
5900 for (i = 1; i < elements; i++) {
5901 read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
5904 case 0x03: /* SADDLV / UADDLV */
5905 case 0x1b: /* ADDV */
5906 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
5908 case 0x0a: /* SMAXV / UMAXV */
5909 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
5911 tcg_res, tcg_elt, tcg_res, tcg_elt);
5913 case 0x1a: /* SMINV / UMINV */
5914 tcg_gen_movcond_i64(is_u ? TCG_COND_LEU : TCG_COND_LE,
5916 tcg_res, tcg_elt, tcg_res, tcg_elt);
5920 g_assert_not_reached();
5925 /* Floating point vector reduction ops which work across 32
5926 * bit (single) or 16 bit (half-precision) intermediates.
5927 * Note that correct NaN propagation requires that we do these
5928 * operations in exactly the order specified by the pseudocode.
5930 TCGv_ptr fpst = get_fpstatus_ptr(size == MO_16);
5931 int fpopcode = opcode | is_min << 4 | is_u << 5;
5932 int vmap = (1 << elements) - 1;
5933 TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
5934 (is_q ? 128 : 64), vmap, fpst);
5935 tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
5936 tcg_temp_free_i32(tcg_res32);
5937 tcg_temp_free_ptr(fpst);
5940 tcg_temp_free_i64(tcg_elt);
5942 /* Now truncate the result to the width required for the final output */
5943 if (opcode == 0x03) {
5944 /* SADDLV, UADDLV: result is 2*esize */
5950 tcg_gen_ext8u_i64(tcg_res, tcg_res);
5953 tcg_gen_ext16u_i64(tcg_res, tcg_res);
5956 tcg_gen_ext32u_i64(tcg_res, tcg_res);
5961 g_assert_not_reached();
5964 write_fp_dreg(s, rd, tcg_res);
5965 tcg_temp_free_i64(tcg_res);
5968 /* DUP (Element, Vector)
5970 * 31 30 29 21 20 16 15 10 9 5 4 0
5971 * +---+---+-------------------+--------+-------------+------+------+
5972 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
5973 * +---+---+-------------------+--------+-------------+------+------+
5975 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5977 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
5980 int size = ctz32(imm5);
5981 int index = imm5 >> (size + 1);
5983 if (size > 3 || (size == 3 && !is_q)) {
5984 unallocated_encoding(s);
5988 if (!fp_access_check(s)) {
5992 tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
5993 vec_reg_offset(s, rn, index, size),
5994 is_q ? 16 : 8, vec_full_reg_size(s));
5997 /* DUP (element, scalar)
5998 * 31 21 20 16 15 10 9 5 4 0
5999 * +-----------------------+--------+-------------+------+------+
6000 * | 0 1 0 1 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
6001 * +-----------------------+--------+-------------+------+------+
6003 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
6006 int size = ctz32(imm5);
6011 unallocated_encoding(s);
6015 if (!fp_access_check(s)) {
6019 index = imm5 >> (size + 1);
6021 /* This instruction just extracts the specified element and
6022 * zero-extends it into the bottom of the destination register.
6024 tmp = tcg_temp_new_i64();
6025 read_vec_element(s, tmp, rn, index, size);
6026 write_fp_dreg(s, rd, tmp);
6027 tcg_temp_free_i64(tmp);
6032 * 31 30 29 21 20 16 15 10 9 5 4 0
6033 * +---+---+-------------------+--------+-------------+------+------+
6034 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 1 1 | Rn | Rd |
6035 * +---+---+-------------------+--------+-------------+------+------+
6037 * size: encoded in imm5 (see ARM ARM LowestSetBit())
6039 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
6042 int size = ctz32(imm5);
6043 uint32_t dofs, oprsz, maxsz;
6045 if (size > 3 || ((size == 3) && !is_q)) {
6046 unallocated_encoding(s);
6050 if (!fp_access_check(s)) {
6054 dofs = vec_full_reg_offset(s, rd);
6055 oprsz = is_q ? 16 : 8;
6056 maxsz = vec_full_reg_size(s);
6058 tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
6063 * 31 21 20 16 15 14 11 10 9 5 4 0
6064 * +-----------------------+--------+------------+---+------+------+
6065 * | 0 1 1 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
6066 * +-----------------------+--------+------------+---+------+------+
6068 * size: encoded in imm5 (see ARM ARM LowestSetBit())
6069 * index: encoded in imm5<4:size+1>
6071 static void handle_simd_inse(DisasContext *s, int rd, int rn,
6074 int size = ctz32(imm5);
6075 int src_index, dst_index;
6079 unallocated_encoding(s);
6083 if (!fp_access_check(s)) {
6087 dst_index = extract32(imm5, 1+size, 5);
6088 src_index = extract32(imm4, size, 4);
6090 tmp = tcg_temp_new_i64();
6092 read_vec_element(s, tmp, rn, src_index, size);
6093 write_vec_element(s, tmp, rd, dst_index, size);
6095 tcg_temp_free_i64(tmp);
6101 * 31 21 20 16 15 10 9 5 4 0
6102 * +-----------------------+--------+-------------+------+------+
6103 * | 0 1 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 1 1 1 | Rn | Rd |
6104 * +-----------------------+--------+-------------+------+------+
6106 * size: encoded in imm5 (see ARM ARM LowestSetBit())
6107 * index: encoded in imm5<4:size+1>
6109 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
6111 int size = ctz32(imm5);
6115 unallocated_encoding(s);
6119 if (!fp_access_check(s)) {
6123 idx = extract32(imm5, 1 + size, 4 - size);
6124 write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
6131 * 31 30 29 21 20 16 15 12 10 9 5 4 0
6132 * +---+---+-------------------+--------+-------------+------+------+
6133 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 1 U 1 1 | Rn | Rd |
6134 * +---+---+-------------------+--------+-------------+------+------+
6136 * U: unsigned when set
6137 * size: encoded in imm5 (see ARM ARM LowestSetBit())
6139 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
6140 int rn, int rd, int imm5)
6142 int size = ctz32(imm5);
6146 /* Check for UnallocatedEncodings */
6148 if (size > 2 || (size == 2 && !is_q)) {
6149 unallocated_encoding(s);
6154 || (size < 3 && is_q)
6155 || (size == 3 && !is_q)) {
6156 unallocated_encoding(s);
6161 if (!fp_access_check(s)) {
6165 element = extract32(imm5, 1+size, 4);
6167 tcg_rd = cpu_reg(s, rd);
6168 read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
6169 if (is_signed && !is_q) {
6170 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
6175 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
6176 * +---+---+----+-----------------+------+---+------+---+------+------+
6177 * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
6178 * +---+---+----+-----------------+------+---+------+---+------+------+
6180 static void disas_simd_copy(DisasContext *s, uint32_t insn)
6182 int rd = extract32(insn, 0, 5);
6183 int rn = extract32(insn, 5, 5);
6184 int imm4 = extract32(insn, 11, 4);
6185 int op = extract32(insn, 29, 1);
6186 int is_q = extract32(insn, 30, 1);
6187 int imm5 = extract32(insn, 16, 5);
6192 handle_simd_inse(s, rd, rn, imm4, imm5);
6194 unallocated_encoding(s);
6199 /* DUP (element - vector) */
6200 handle_simd_dupe(s, is_q, rd, rn, imm5);
6204 handle_simd_dupg(s, is_q, rd, rn, imm5);
6209 handle_simd_insg(s, rd, rn, imm5);
6211 unallocated_encoding(s);
6216 /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
6217 handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
6220 unallocated_encoding(s);
6226 /* AdvSIMD modified immediate
6227 * 31 30 29 28 19 18 16 15 12 11 10 9 5 4 0
6228 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
6229 * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh | Rd |
6230 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
6232 * There are a number of operations that can be carried out here:
6233 * MOVI - move (shifted) imm into register
6234 * MVNI - move inverted (shifted) imm into register
6235 * ORR - bitwise OR of (shifted) imm with register
6236 * BIC - bitwise clear of (shifted) imm with register
6238 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
6240 int rd = extract32(insn, 0, 5);
6241 int cmode = extract32(insn, 12, 4);
6242 int cmode_3_1 = extract32(cmode, 1, 3);
6243 int cmode_0 = extract32(cmode, 0, 1);
6244 int o2 = extract32(insn, 11, 1);
6245 uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
6246 bool is_neg = extract32(insn, 29, 1);
6247 bool is_q = extract32(insn, 30, 1);
6250 if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
6251 unallocated_encoding(s);
6255 if (!fp_access_check(s)) {
6259 /* See AdvSIMDExpandImm() in ARM ARM */
6260 switch (cmode_3_1) {
6261 case 0: /* Replicate(Zeros(24):imm8, 2) */
6262 case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
6263 case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
6264 case 3: /* Replicate(imm8:Zeros(24), 2) */
6266 int shift = cmode_3_1 * 8;
6267 imm = bitfield_replicate(abcdefgh << shift, 32);
6270 case 4: /* Replicate(Zeros(8):imm8, 4) */
6271 case 5: /* Replicate(imm8:Zeros(8), 4) */
6273 int shift = (cmode_3_1 & 0x1) * 8;
6274 imm = bitfield_replicate(abcdefgh << shift, 16);
6279 /* Replicate(Zeros(8):imm8:Ones(16), 2) */
6280 imm = (abcdefgh << 16) | 0xffff;
6282 /* Replicate(Zeros(16):imm8:Ones(8), 2) */
6283 imm = (abcdefgh << 8) | 0xff;
6285 imm = bitfield_replicate(imm, 32);
6288 if (!cmode_0 && !is_neg) {
6289 imm = bitfield_replicate(abcdefgh, 8);
6290 } else if (!cmode_0 && is_neg) {
6293 for (i = 0; i < 8; i++) {
6294 if ((abcdefgh) & (1 << i)) {
6295 imm |= 0xffULL << (i * 8);
6298 } else if (cmode_0) {
6300 imm = (abcdefgh & 0x3f) << 48;
6301 if (abcdefgh & 0x80) {
6302 imm |= 0x8000000000000000ULL;
6304 if (abcdefgh & 0x40) {
6305 imm |= 0x3fc0000000000000ULL;
6307 imm |= 0x4000000000000000ULL;
6310 imm = (abcdefgh & 0x3f) << 19;
6311 if (abcdefgh & 0x80) {
6314 if (abcdefgh & 0x40) {
6325 if (cmode_3_1 != 7 && is_neg) {
6329 if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
6330 /* MOVI or MVNI, with MVNI negation handled above. */
6331 tcg_gen_gvec_dup64i(vec_full_reg_offset(s, rd), is_q ? 16 : 8,
6332 vec_full_reg_size(s), imm);
6334 /* ORR or BIC, with BIC negation to AND handled above. */
6336 gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
6338 gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
6343 /* AdvSIMD scalar copy
6344 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
6345 * +-----+----+-----------------+------+---+------+---+------+------+
6346 * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
6347 * +-----+----+-----------------+------+---+------+---+------+------+
6349 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
6351 int rd = extract32(insn, 0, 5);
6352 int rn = extract32(insn, 5, 5);
6353 int imm4 = extract32(insn, 11, 4);
6354 int imm5 = extract32(insn, 16, 5);
6355 int op = extract32(insn, 29, 1);
6357 if (op != 0 || imm4 != 0) {
6358 unallocated_encoding(s);
6362 /* DUP (element, scalar) */
6363 handle_simd_dupes(s, rd, rn, imm5);
6366 /* AdvSIMD scalar pairwise
6367 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
6368 * +-----+---+-----------+------+-----------+--------+-----+------+------+
6369 * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
6370 * +-----+---+-----------+------+-----------+--------+-----+------+------+
6372 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
6374 int u = extract32(insn, 29, 1);
6375 int size = extract32(insn, 22, 2);
6376 int opcode = extract32(insn, 12, 5);
6377 int rn = extract32(insn, 5, 5);
6378 int rd = extract32(insn, 0, 5);
6381 /* For some ops (the FP ones), size[1] is part of the encoding.
6382 * For ADDP strictly it is not but size[1] is always 1 for valid
6385 opcode |= (extract32(size, 1, 1) << 5);
6388 case 0x3b: /* ADDP */
6389 if (u || size != 3) {
6390 unallocated_encoding(s);
6393 if (!fp_access_check(s)) {
6399 case 0xc: /* FMAXNMP */
6400 case 0xd: /* FADDP */
6401 case 0xf: /* FMAXP */
6402 case 0x2c: /* FMINNMP */
6403 case 0x2f: /* FMINP */
6404 /* FP op, size[0] is 32 or 64 bit */
6406 unallocated_encoding(s);
6409 if (!fp_access_check(s)) {
6413 size = extract32(size, 0, 1) ? 3 : 2;
6414 fpst = get_fpstatus_ptr(false);
6417 unallocated_encoding(s);
6422 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6423 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6424 TCGv_i64 tcg_res = tcg_temp_new_i64();
6426 read_vec_element(s, tcg_op1, rn, 0, MO_64);
6427 read_vec_element(s, tcg_op2, rn, 1, MO_64);
6430 case 0x3b: /* ADDP */
6431 tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
6433 case 0xc: /* FMAXNMP */
6434 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6436 case 0xd: /* FADDP */
6437 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6439 case 0xf: /* FMAXP */
6440 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6442 case 0x2c: /* FMINNMP */
6443 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6445 case 0x2f: /* FMINP */
6446 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6449 g_assert_not_reached();
6452 write_fp_dreg(s, rd, tcg_res);
6454 tcg_temp_free_i64(tcg_op1);
6455 tcg_temp_free_i64(tcg_op2);
6456 tcg_temp_free_i64(tcg_res);
6458 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6459 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6460 TCGv_i32 tcg_res = tcg_temp_new_i32();
6462 read_vec_element_i32(s, tcg_op1, rn, 0, MO_32);
6463 read_vec_element_i32(s, tcg_op2, rn, 1, MO_32);
6466 case 0xc: /* FMAXNMP */
6467 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6469 case 0xd: /* FADDP */
6470 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6472 case 0xf: /* FMAXP */
6473 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6475 case 0x2c: /* FMINNMP */
6476 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6478 case 0x2f: /* FMINP */
6479 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6482 g_assert_not_reached();
6485 write_fp_sreg(s, rd, tcg_res);
6487 tcg_temp_free_i32(tcg_op1);
6488 tcg_temp_free_i32(tcg_op2);
6489 tcg_temp_free_i32(tcg_res);
6493 tcg_temp_free_ptr(fpst);
6498 * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
6500 * This code is handles the common shifting code and is used by both
6501 * the vector and scalar code.
6503 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6504 TCGv_i64 tcg_rnd, bool accumulate,
6505 bool is_u, int size, int shift)
6507 bool extended_result = false;
6508 bool round = tcg_rnd != NULL;
6510 TCGv_i64 tcg_src_hi;
6512 if (round && size == 3) {
6513 extended_result = true;
6514 ext_lshift = 64 - shift;
6515 tcg_src_hi = tcg_temp_new_i64();
6516 } else if (shift == 64) {
6517 if (!accumulate && is_u) {
6518 /* result is zero */
6519 tcg_gen_movi_i64(tcg_res, 0);
6524 /* Deal with the rounding step */
6526 if (extended_result) {
6527 TCGv_i64 tcg_zero = tcg_const_i64(0);
6529 /* take care of sign extending tcg_res */
6530 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
6531 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
6532 tcg_src, tcg_src_hi,
6535 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
6539 tcg_temp_free_i64(tcg_zero);
6541 tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
6545 /* Now do the shift right */
6546 if (round && extended_result) {
6547 /* extended case, >64 bit precision required */
6548 if (ext_lshift == 0) {
6549 /* special case, only high bits matter */
6550 tcg_gen_mov_i64(tcg_src, tcg_src_hi);
6552 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6553 tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
6554 tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
6559 /* essentially shifting in 64 zeros */
6560 tcg_gen_movi_i64(tcg_src, 0);
6562 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6566 /* effectively extending the sign-bit */
6567 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
6569 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
6575 tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
6577 tcg_gen_mov_i64(tcg_res, tcg_src);
6580 if (extended_result) {
6581 tcg_temp_free_i64(tcg_src_hi);
6585 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
6586 static void handle_scalar_simd_shri(DisasContext *s,
6587 bool is_u, int immh, int immb,
6588 int opcode, int rn, int rd)
6591 int immhb = immh << 3 | immb;
6592 int shift = 2 * (8 << size) - immhb;
6593 bool accumulate = false;
6595 bool insert = false;
6600 if (!extract32(immh, 3, 1)) {
6601 unallocated_encoding(s);
6605 if (!fp_access_check(s)) {
6610 case 0x02: /* SSRA / USRA (accumulate) */
6613 case 0x04: /* SRSHR / URSHR (rounding) */
6616 case 0x06: /* SRSRA / URSRA (accum + rounding) */
6617 accumulate = round = true;
6619 case 0x08: /* SRI */
6625 uint64_t round_const = 1ULL << (shift - 1);
6626 tcg_round = tcg_const_i64(round_const);
6631 tcg_rn = read_fp_dreg(s, rn);
6632 tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6635 /* shift count same as element size is valid but does nothing;
6636 * special case to avoid potential shift by 64.
6638 int esize = 8 << size;
6639 if (shift != esize) {
6640 tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
6641 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
6644 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6645 accumulate, is_u, size, shift);
6648 write_fp_dreg(s, rd, tcg_rd);
6650 tcg_temp_free_i64(tcg_rn);
6651 tcg_temp_free_i64(tcg_rd);
6653 tcg_temp_free_i64(tcg_round);
6657 /* SHL/SLI - Scalar shift left */
6658 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
6659 int immh, int immb, int opcode,
6662 int size = 32 - clz32(immh) - 1;
6663 int immhb = immh << 3 | immb;
6664 int shift = immhb - (8 << size);
6665 TCGv_i64 tcg_rn = new_tmp_a64(s);
6666 TCGv_i64 tcg_rd = new_tmp_a64(s);
6668 if (!extract32(immh, 3, 1)) {
6669 unallocated_encoding(s);
6673 if (!fp_access_check(s)) {
6677 tcg_rn = read_fp_dreg(s, rn);
6678 tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6681 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
6683 tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
6686 write_fp_dreg(s, rd, tcg_rd);
6688 tcg_temp_free_i64(tcg_rn);
6689 tcg_temp_free_i64(tcg_rd);
6692 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
6693 * (signed/unsigned) narrowing */
6694 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
6695 bool is_u_shift, bool is_u_narrow,
6696 int immh, int immb, int opcode,
6699 int immhb = immh << 3 | immb;
6700 int size = 32 - clz32(immh) - 1;
6701 int esize = 8 << size;
6702 int shift = (2 * esize) - immhb;
6703 int elements = is_scalar ? 1 : (64 / esize);
6704 bool round = extract32(opcode, 0, 1);
6705 TCGMemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
6706 TCGv_i64 tcg_rn, tcg_rd, tcg_round;
6707 TCGv_i32 tcg_rd_narrowed;
6710 static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
6711 { gen_helper_neon_narrow_sat_s8,
6712 gen_helper_neon_unarrow_sat8 },
6713 { gen_helper_neon_narrow_sat_s16,
6714 gen_helper_neon_unarrow_sat16 },
6715 { gen_helper_neon_narrow_sat_s32,
6716 gen_helper_neon_unarrow_sat32 },
6719 static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
6720 gen_helper_neon_narrow_sat_u8,
6721 gen_helper_neon_narrow_sat_u16,
6722 gen_helper_neon_narrow_sat_u32,
6725 NeonGenNarrowEnvFn *narrowfn;
6731 if (extract32(immh, 3, 1)) {
6732 unallocated_encoding(s);
6736 if (!fp_access_check(s)) {
6741 narrowfn = unsigned_narrow_fns[size];
6743 narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
6746 tcg_rn = tcg_temp_new_i64();
6747 tcg_rd = tcg_temp_new_i64();
6748 tcg_rd_narrowed = tcg_temp_new_i32();
6749 tcg_final = tcg_const_i64(0);
6752 uint64_t round_const = 1ULL << (shift - 1);
6753 tcg_round = tcg_const_i64(round_const);
6758 for (i = 0; i < elements; i++) {
6759 read_vec_element(s, tcg_rn, rn, i, ldop);
6760 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6761 false, is_u_shift, size+1, shift);
6762 narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
6763 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
6764 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
6768 write_vec_element(s, tcg_final, rd, 0, MO_64);
6770 write_vec_element(s, tcg_final, rd, 1, MO_64);
6774 tcg_temp_free_i64(tcg_round);
6776 tcg_temp_free_i64(tcg_rn);
6777 tcg_temp_free_i64(tcg_rd);
6778 tcg_temp_free_i32(tcg_rd_narrowed);
6779 tcg_temp_free_i64(tcg_final);
6781 clear_vec_high(s, is_q, rd);
6784 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
6785 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
6786 bool src_unsigned, bool dst_unsigned,
6787 int immh, int immb, int rn, int rd)
6789 int immhb = immh << 3 | immb;
6790 int size = 32 - clz32(immh) - 1;
6791 int shift = immhb - (8 << size);
6795 assert(!(scalar && is_q));
6798 if (!is_q && extract32(immh, 3, 1)) {
6799 unallocated_encoding(s);
6803 /* Since we use the variable-shift helpers we must
6804 * replicate the shift count into each element of
6805 * the tcg_shift value.
6809 shift |= shift << 8;
6812 shift |= shift << 16;
6818 g_assert_not_reached();
6822 if (!fp_access_check(s)) {
6827 TCGv_i64 tcg_shift = tcg_const_i64(shift);
6828 static NeonGenTwo64OpEnvFn * const fns[2][2] = {
6829 { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
6830 { NULL, gen_helper_neon_qshl_u64 },
6832 NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
6833 int maxpass = is_q ? 2 : 1;
6835 for (pass = 0; pass < maxpass; pass++) {
6836 TCGv_i64 tcg_op = tcg_temp_new_i64();
6838 read_vec_element(s, tcg_op, rn, pass, MO_64);
6839 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6840 write_vec_element(s, tcg_op, rd, pass, MO_64);
6842 tcg_temp_free_i64(tcg_op);
6844 tcg_temp_free_i64(tcg_shift);
6845 clear_vec_high(s, is_q, rd);
6847 TCGv_i32 tcg_shift = tcg_const_i32(shift);
6848 static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
6850 { gen_helper_neon_qshl_s8,
6851 gen_helper_neon_qshl_s16,
6852 gen_helper_neon_qshl_s32 },
6853 { gen_helper_neon_qshlu_s8,
6854 gen_helper_neon_qshlu_s16,
6855 gen_helper_neon_qshlu_s32 }
6857 { NULL, NULL, NULL },
6858 { gen_helper_neon_qshl_u8,
6859 gen_helper_neon_qshl_u16,
6860 gen_helper_neon_qshl_u32 }
6863 NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
6864 TCGMemOp memop = scalar ? size : MO_32;
6865 int maxpass = scalar ? 1 : is_q ? 4 : 2;
6867 for (pass = 0; pass < maxpass; pass++) {
6868 TCGv_i32 tcg_op = tcg_temp_new_i32();
6870 read_vec_element_i32(s, tcg_op, rn, pass, memop);
6871 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6875 tcg_gen_ext8u_i32(tcg_op, tcg_op);
6878 tcg_gen_ext16u_i32(tcg_op, tcg_op);
6883 g_assert_not_reached();
6885 write_fp_sreg(s, rd, tcg_op);
6887 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
6890 tcg_temp_free_i32(tcg_op);
6892 tcg_temp_free_i32(tcg_shift);
6895 clear_vec_high(s, is_q, rd);
6900 /* Common vector code for handling integer to FP conversion */
6901 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
6902 int elements, int is_signed,
6903 int fracbits, int size)
6905 TCGv_ptr tcg_fpst = get_fpstatus_ptr(size == MO_16);
6906 TCGv_i32 tcg_shift = NULL;
6908 TCGMemOp mop = size | (is_signed ? MO_SIGN : 0);
6911 if (fracbits || size == MO_64) {
6912 tcg_shift = tcg_const_i32(fracbits);
6915 if (size == MO_64) {
6916 TCGv_i64 tcg_int64 = tcg_temp_new_i64();
6917 TCGv_i64 tcg_double = tcg_temp_new_i64();
6919 for (pass = 0; pass < elements; pass++) {
6920 read_vec_element(s, tcg_int64, rn, pass, mop);
6923 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
6924 tcg_shift, tcg_fpst);
6926 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
6927 tcg_shift, tcg_fpst);
6929 if (elements == 1) {
6930 write_fp_dreg(s, rd, tcg_double);
6932 write_vec_element(s, tcg_double, rd, pass, MO_64);
6936 tcg_temp_free_i64(tcg_int64);
6937 tcg_temp_free_i64(tcg_double);
6940 TCGv_i32 tcg_int32 = tcg_temp_new_i32();
6941 TCGv_i32 tcg_float = tcg_temp_new_i32();
6943 for (pass = 0; pass < elements; pass++) {
6944 read_vec_element_i32(s, tcg_int32, rn, pass, mop);
6950 gen_helper_vfp_sltos(tcg_float, tcg_int32,
6951 tcg_shift, tcg_fpst);
6953 gen_helper_vfp_ultos(tcg_float, tcg_int32,
6954 tcg_shift, tcg_fpst);
6958 gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
6960 gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
6967 gen_helper_vfp_sltoh(tcg_float, tcg_int32,
6968 tcg_shift, tcg_fpst);
6970 gen_helper_vfp_ultoh(tcg_float, tcg_int32,
6971 tcg_shift, tcg_fpst);
6975 gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
6977 gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
6982 g_assert_not_reached();
6985 if (elements == 1) {
6986 write_fp_sreg(s, rd, tcg_float);
6988 write_vec_element_i32(s, tcg_float, rd, pass, size);
6992 tcg_temp_free_i32(tcg_int32);
6993 tcg_temp_free_i32(tcg_float);
6996 tcg_temp_free_ptr(tcg_fpst);
6998 tcg_temp_free_i32(tcg_shift);
7001 clear_vec_high(s, elements << size == 16, rd);
7004 /* UCVTF/SCVTF - Integer to FP conversion */
7005 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
7006 bool is_q, bool is_u,
7007 int immh, int immb, int opcode,
7010 bool is_double = extract32(immh, 3, 1);
7011 int size = is_double ? MO_64 : MO_32;
7013 int immhb = immh << 3 | immb;
7014 int fracbits = (is_double ? 128 : 64) - immhb;
7016 if (!extract32(immh, 2, 2)) {
7017 unallocated_encoding(s);
7024 elements = is_double ? 2 : is_q ? 4 : 2;
7025 if (is_double && !is_q) {
7026 unallocated_encoding(s);
7031 if (!fp_access_check(s)) {
7035 /* immh == 0 would be a failure of the decode logic */
7038 handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
7041 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
7042 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
7043 bool is_q, bool is_u,
7044 int immh, int immb, int rn, int rd)
7046 bool is_double = extract32(immh, 3, 1);
7047 int immhb = immh << 3 | immb;
7048 int fracbits = (is_double ? 128 : 64) - immhb;
7050 TCGv_ptr tcg_fpstatus;
7051 TCGv_i32 tcg_rmode, tcg_shift;
7053 if (!extract32(immh, 2, 2)) {
7054 unallocated_encoding(s);
7058 if (!is_scalar && !is_q && is_double) {
7059 unallocated_encoding(s);
7063 if (!fp_access_check(s)) {
7067 assert(!(is_scalar && is_q));
7069 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(FPROUNDING_ZERO));
7070 tcg_fpstatus = get_fpstatus_ptr(false);
7071 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
7072 tcg_shift = tcg_const_i32(fracbits);
7075 int maxpass = is_scalar ? 1 : 2;
7077 for (pass = 0; pass < maxpass; pass++) {
7078 TCGv_i64 tcg_op = tcg_temp_new_i64();
7080 read_vec_element(s, tcg_op, rn, pass, MO_64);
7082 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
7084 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
7086 write_vec_element(s, tcg_op, rd, pass, MO_64);
7087 tcg_temp_free_i64(tcg_op);
7089 clear_vec_high(s, is_q, rd);
7091 int maxpass = is_scalar ? 1 : is_q ? 4 : 2;
7092 for (pass = 0; pass < maxpass; pass++) {
7093 TCGv_i32 tcg_op = tcg_temp_new_i32();
7095 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7097 gen_helper_vfp_touls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
7099 gen_helper_vfp_tosls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
7102 write_fp_sreg(s, rd, tcg_op);
7104 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
7106 tcg_temp_free_i32(tcg_op);
7109 clear_vec_high(s, is_q, rd);
7113 tcg_temp_free_ptr(tcg_fpstatus);
7114 tcg_temp_free_i32(tcg_shift);
7115 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
7116 tcg_temp_free_i32(tcg_rmode);
7119 /* AdvSIMD scalar shift by immediate
7120 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
7121 * +-----+---+-------------+------+------+--------+---+------+------+
7122 * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
7123 * +-----+---+-------------+------+------+--------+---+------+------+
7125 * This is the scalar version so it works on a fixed sized registers
7127 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
7129 int rd = extract32(insn, 0, 5);
7130 int rn = extract32(insn, 5, 5);
7131 int opcode = extract32(insn, 11, 5);
7132 int immb = extract32(insn, 16, 3);
7133 int immh = extract32(insn, 19, 4);
7134 bool is_u = extract32(insn, 29, 1);
7137 unallocated_encoding(s);
7142 case 0x08: /* SRI */
7144 unallocated_encoding(s);
7148 case 0x00: /* SSHR / USHR */
7149 case 0x02: /* SSRA / USRA */
7150 case 0x04: /* SRSHR / URSHR */
7151 case 0x06: /* SRSRA / URSRA */
7152 handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
7154 case 0x0a: /* SHL / SLI */
7155 handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
7157 case 0x1c: /* SCVTF, UCVTF */
7158 handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
7161 case 0x10: /* SQSHRUN, SQSHRUN2 */
7162 case 0x11: /* SQRSHRUN, SQRSHRUN2 */
7164 unallocated_encoding(s);
7167 handle_vec_simd_sqshrn(s, true, false, false, true,
7168 immh, immb, opcode, rn, rd);
7170 case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
7171 case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
7172 handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
7173 immh, immb, opcode, rn, rd);
7175 case 0xc: /* SQSHLU */
7177 unallocated_encoding(s);
7180 handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
7182 case 0xe: /* SQSHL, UQSHL */
7183 handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
7185 case 0x1f: /* FCVTZS, FCVTZU */
7186 handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
7189 unallocated_encoding(s);
7194 /* AdvSIMD scalar three different
7195 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
7196 * +-----+---+-----------+------+---+------+--------+-----+------+------+
7197 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
7198 * +-----+---+-----------+------+---+------+--------+-----+------+------+
7200 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
7202 bool is_u = extract32(insn, 29, 1);
7203 int size = extract32(insn, 22, 2);
7204 int opcode = extract32(insn, 12, 4);
7205 int rm = extract32(insn, 16, 5);
7206 int rn = extract32(insn, 5, 5);
7207 int rd = extract32(insn, 0, 5);
7210 unallocated_encoding(s);
7215 case 0x9: /* SQDMLAL, SQDMLAL2 */
7216 case 0xb: /* SQDMLSL, SQDMLSL2 */
7217 case 0xd: /* SQDMULL, SQDMULL2 */
7218 if (size == 0 || size == 3) {
7219 unallocated_encoding(s);
7224 unallocated_encoding(s);
7228 if (!fp_access_check(s)) {
7233 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7234 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7235 TCGv_i64 tcg_res = tcg_temp_new_i64();
7237 read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
7238 read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
7240 tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
7241 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
7244 case 0xd: /* SQDMULL, SQDMULL2 */
7246 case 0xb: /* SQDMLSL, SQDMLSL2 */
7247 tcg_gen_neg_i64(tcg_res, tcg_res);
7249 case 0x9: /* SQDMLAL, SQDMLAL2 */
7250 read_vec_element(s, tcg_op1, rd, 0, MO_64);
7251 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
7255 g_assert_not_reached();
7258 write_fp_dreg(s, rd, tcg_res);
7260 tcg_temp_free_i64(tcg_op1);
7261 tcg_temp_free_i64(tcg_op2);
7262 tcg_temp_free_i64(tcg_res);
7264 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7265 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7266 TCGv_i64 tcg_res = tcg_temp_new_i64();
7268 read_vec_element_i32(s, tcg_op1, rn, 0, MO_16);
7269 read_vec_element_i32(s, tcg_op2, rm, 0, MO_16);
7271 gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
7272 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
7275 case 0xd: /* SQDMULL, SQDMULL2 */
7277 case 0xb: /* SQDMLSL, SQDMLSL2 */
7278 gen_helper_neon_negl_u32(tcg_res, tcg_res);
7280 case 0x9: /* SQDMLAL, SQDMLAL2 */
7282 TCGv_i64 tcg_op3 = tcg_temp_new_i64();
7283 read_vec_element(s, tcg_op3, rd, 0, MO_32);
7284 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
7286 tcg_temp_free_i64(tcg_op3);
7290 g_assert_not_reached();
7293 tcg_gen_ext32u_i64(tcg_res, tcg_res);
7294 write_fp_dreg(s, rd, tcg_res);
7296 tcg_temp_free_i32(tcg_op1);
7297 tcg_temp_free_i32(tcg_op2);
7298 tcg_temp_free_i64(tcg_res);
7302 /* CMTST : test is "if (X & Y != 0)". */
7303 static void gen_cmtst_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
7305 tcg_gen_and_i32(d, a, b);
7306 tcg_gen_setcondi_i32(TCG_COND_NE, d, d, 0);
7307 tcg_gen_neg_i32(d, d);
7310 static void gen_cmtst_i64(TCGv_i64 d, TCGv_i64 a, TCGv_i64 b)
7312 tcg_gen_and_i64(d, a, b);
7313 tcg_gen_setcondi_i64(TCG_COND_NE, d, d, 0);
7314 tcg_gen_neg_i64(d, d);
7317 static void gen_cmtst_vec(unsigned vece, TCGv_vec d, TCGv_vec a, TCGv_vec b)
7319 tcg_gen_and_vec(vece, d, a, b);
7320 tcg_gen_dupi_vec(vece, a, 0);
7321 tcg_gen_cmp_vec(TCG_COND_NE, vece, d, d, a);
7324 static void handle_3same_64(DisasContext *s, int opcode, bool u,
7325 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
7327 /* Handle 64x64->64 opcodes which are shared between the scalar
7328 * and vector 3-same groups. We cover every opcode where size == 3
7329 * is valid in either the three-reg-same (integer, not pairwise)
7330 * or scalar-three-reg-same groups.
7335 case 0x1: /* SQADD */
7337 gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7339 gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7342 case 0x5: /* SQSUB */
7344 gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7346 gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7349 case 0x6: /* CMGT, CMHI */
7350 /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
7351 * We implement this using setcond (test) and then negating.
7353 cond = u ? TCG_COND_GTU : TCG_COND_GT;
7355 tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
7356 tcg_gen_neg_i64(tcg_rd, tcg_rd);
7358 case 0x7: /* CMGE, CMHS */
7359 cond = u ? TCG_COND_GEU : TCG_COND_GE;
7361 case 0x11: /* CMTST, CMEQ */
7366 gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
7368 case 0x8: /* SSHL, USHL */
7370 gen_helper_neon_shl_u64(tcg_rd, tcg_rn, tcg_rm);
7372 gen_helper_neon_shl_s64(tcg_rd, tcg_rn, tcg_rm);
7375 case 0x9: /* SQSHL, UQSHL */
7377 gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7379 gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7382 case 0xa: /* SRSHL, URSHL */
7384 gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
7386 gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
7389 case 0xb: /* SQRSHL, UQRSHL */
7391 gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7393 gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7396 case 0x10: /* ADD, SUB */
7398 tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
7400 tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
7404 g_assert_not_reached();
7408 /* Handle the 3-same-operands float operations; shared by the scalar
7409 * and vector encodings. The caller must filter out any encodings
7410 * not allocated for the encoding it is dealing with.
7412 static void handle_3same_float(DisasContext *s, int size, int elements,
7413 int fpopcode, int rd, int rn, int rm)
7416 TCGv_ptr fpst = get_fpstatus_ptr(false);
7418 for (pass = 0; pass < elements; pass++) {
7421 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7422 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7423 TCGv_i64 tcg_res = tcg_temp_new_i64();
7425 read_vec_element(s, tcg_op1, rn, pass, MO_64);
7426 read_vec_element(s, tcg_op2, rm, pass, MO_64);
7429 case 0x39: /* FMLS */
7430 /* As usual for ARM, separate negation for fused multiply-add */
7431 gen_helper_vfp_negd(tcg_op1, tcg_op1);
7433 case 0x19: /* FMLA */
7434 read_vec_element(s, tcg_res, rd, pass, MO_64);
7435 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
7438 case 0x18: /* FMAXNM */
7439 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7441 case 0x1a: /* FADD */
7442 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
7444 case 0x1b: /* FMULX */
7445 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
7447 case 0x1c: /* FCMEQ */
7448 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7450 case 0x1e: /* FMAX */
7451 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
7453 case 0x1f: /* FRECPS */
7454 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7456 case 0x38: /* FMINNM */
7457 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7459 case 0x3a: /* FSUB */
7460 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
7462 case 0x3e: /* FMIN */
7463 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
7465 case 0x3f: /* FRSQRTS */
7466 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7468 case 0x5b: /* FMUL */
7469 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
7471 case 0x5c: /* FCMGE */
7472 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7474 case 0x5d: /* FACGE */
7475 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7477 case 0x5f: /* FDIV */
7478 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
7480 case 0x7a: /* FABD */
7481 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
7482 gen_helper_vfp_absd(tcg_res, tcg_res);
7484 case 0x7c: /* FCMGT */
7485 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7487 case 0x7d: /* FACGT */
7488 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7491 g_assert_not_reached();
7494 write_vec_element(s, tcg_res, rd, pass, MO_64);
7496 tcg_temp_free_i64(tcg_res);
7497 tcg_temp_free_i64(tcg_op1);
7498 tcg_temp_free_i64(tcg_op2);
7501 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7502 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7503 TCGv_i32 tcg_res = tcg_temp_new_i32();
7505 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
7506 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
7509 case 0x39: /* FMLS */
7510 /* As usual for ARM, separate negation for fused multiply-add */
7511 gen_helper_vfp_negs(tcg_op1, tcg_op1);
7513 case 0x19: /* FMLA */
7514 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7515 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
7518 case 0x1a: /* FADD */
7519 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
7521 case 0x1b: /* FMULX */
7522 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
7524 case 0x1c: /* FCMEQ */
7525 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7527 case 0x1e: /* FMAX */
7528 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
7530 case 0x1f: /* FRECPS */
7531 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7533 case 0x18: /* FMAXNM */
7534 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
7536 case 0x38: /* FMINNM */
7537 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
7539 case 0x3a: /* FSUB */
7540 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
7542 case 0x3e: /* FMIN */
7543 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
7545 case 0x3f: /* FRSQRTS */
7546 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7548 case 0x5b: /* FMUL */
7549 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
7551 case 0x5c: /* FCMGE */
7552 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7554 case 0x5d: /* FACGE */
7555 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7557 case 0x5f: /* FDIV */
7558 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
7560 case 0x7a: /* FABD */
7561 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
7562 gen_helper_vfp_abss(tcg_res, tcg_res);
7564 case 0x7c: /* FCMGT */
7565 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7567 case 0x7d: /* FACGT */
7568 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7571 g_assert_not_reached();
7574 if (elements == 1) {
7575 /* scalar single so clear high part */
7576 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7578 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
7579 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
7580 tcg_temp_free_i64(tcg_tmp);
7582 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7585 tcg_temp_free_i32(tcg_res);
7586 tcg_temp_free_i32(tcg_op1);
7587 tcg_temp_free_i32(tcg_op2);
7591 tcg_temp_free_ptr(fpst);
7593 clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
7596 /* AdvSIMD scalar three same
7597 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
7598 * +-----+---+-----------+------+---+------+--------+---+------+------+
7599 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
7600 * +-----+---+-----------+------+---+------+--------+---+------+------+
7602 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
7604 int rd = extract32(insn, 0, 5);
7605 int rn = extract32(insn, 5, 5);
7606 int opcode = extract32(insn, 11, 5);
7607 int rm = extract32(insn, 16, 5);
7608 int size = extract32(insn, 22, 2);
7609 bool u = extract32(insn, 29, 1);
7612 if (opcode >= 0x18) {
7613 /* Floating point: U, size[1] and opcode indicate operation */
7614 int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
7616 case 0x1b: /* FMULX */
7617 case 0x1f: /* FRECPS */
7618 case 0x3f: /* FRSQRTS */
7619 case 0x5d: /* FACGE */
7620 case 0x7d: /* FACGT */
7621 case 0x1c: /* FCMEQ */
7622 case 0x5c: /* FCMGE */
7623 case 0x7c: /* FCMGT */
7624 case 0x7a: /* FABD */
7627 unallocated_encoding(s);
7631 if (!fp_access_check(s)) {
7635 handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
7640 case 0x1: /* SQADD, UQADD */
7641 case 0x5: /* SQSUB, UQSUB */
7642 case 0x9: /* SQSHL, UQSHL */
7643 case 0xb: /* SQRSHL, UQRSHL */
7645 case 0x8: /* SSHL, USHL */
7646 case 0xa: /* SRSHL, URSHL */
7647 case 0x6: /* CMGT, CMHI */
7648 case 0x7: /* CMGE, CMHS */
7649 case 0x11: /* CMTST, CMEQ */
7650 case 0x10: /* ADD, SUB (vector) */
7652 unallocated_encoding(s);
7656 case 0x16: /* SQDMULH, SQRDMULH (vector) */
7657 if (size != 1 && size != 2) {
7658 unallocated_encoding(s);
7663 unallocated_encoding(s);
7667 if (!fp_access_check(s)) {
7671 tcg_rd = tcg_temp_new_i64();
7674 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
7675 TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
7677 handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
7678 tcg_temp_free_i64(tcg_rn);
7679 tcg_temp_free_i64(tcg_rm);
7681 /* Do a single operation on the lowest element in the vector.
7682 * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
7683 * no side effects for all these operations.
7684 * OPTME: special-purpose helpers would avoid doing some
7685 * unnecessary work in the helper for the 8 and 16 bit cases.
7687 NeonGenTwoOpEnvFn *genenvfn;
7688 TCGv_i32 tcg_rn = tcg_temp_new_i32();
7689 TCGv_i32 tcg_rm = tcg_temp_new_i32();
7690 TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
7692 read_vec_element_i32(s, tcg_rn, rn, 0, size);
7693 read_vec_element_i32(s, tcg_rm, rm, 0, size);
7696 case 0x1: /* SQADD, UQADD */
7698 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7699 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
7700 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
7701 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
7703 genenvfn = fns[size][u];
7706 case 0x5: /* SQSUB, UQSUB */
7708 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7709 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
7710 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
7711 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
7713 genenvfn = fns[size][u];
7716 case 0x9: /* SQSHL, UQSHL */
7718 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7719 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
7720 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
7721 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
7723 genenvfn = fns[size][u];
7726 case 0xb: /* SQRSHL, UQRSHL */
7728 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7729 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
7730 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
7731 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
7733 genenvfn = fns[size][u];
7736 case 0x16: /* SQDMULH, SQRDMULH */
7738 static NeonGenTwoOpEnvFn * const fns[2][2] = {
7739 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
7740 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
7742 assert(size == 1 || size == 2);
7743 genenvfn = fns[size - 1][u];
7747 g_assert_not_reached();
7750 genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
7751 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
7752 tcg_temp_free_i32(tcg_rd32);
7753 tcg_temp_free_i32(tcg_rn);
7754 tcg_temp_free_i32(tcg_rm);
7757 write_fp_dreg(s, rd, tcg_rd);
7759 tcg_temp_free_i64(tcg_rd);
7762 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
7763 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
7764 TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
7766 /* Handle 64->64 opcodes which are shared between the scalar and
7767 * vector 2-reg-misc groups. We cover every integer opcode where size == 3
7768 * is valid in either group and also the double-precision fp ops.
7769 * The caller only need provide tcg_rmode and tcg_fpstatus if the op
7775 case 0x4: /* CLS, CLZ */
7777 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
7779 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
7783 /* This opcode is shared with CNT and RBIT but we have earlier
7784 * enforced that size == 3 if and only if this is the NOT insn.
7786 tcg_gen_not_i64(tcg_rd, tcg_rn);
7788 case 0x7: /* SQABS, SQNEG */
7790 gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
7792 gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
7795 case 0xa: /* CMLT */
7796 /* 64 bit integer comparison against zero, result is
7797 * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
7802 tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
7803 tcg_gen_neg_i64(tcg_rd, tcg_rd);
7805 case 0x8: /* CMGT, CMGE */
7806 cond = u ? TCG_COND_GE : TCG_COND_GT;
7808 case 0x9: /* CMEQ, CMLE */
7809 cond = u ? TCG_COND_LE : TCG_COND_EQ;
7811 case 0xb: /* ABS, NEG */
7813 tcg_gen_neg_i64(tcg_rd, tcg_rn);
7815 TCGv_i64 tcg_zero = tcg_const_i64(0);
7816 tcg_gen_neg_i64(tcg_rd, tcg_rn);
7817 tcg_gen_movcond_i64(TCG_COND_GT, tcg_rd, tcg_rn, tcg_zero,
7819 tcg_temp_free_i64(tcg_zero);
7822 case 0x2f: /* FABS */
7823 gen_helper_vfp_absd(tcg_rd, tcg_rn);
7825 case 0x6f: /* FNEG */
7826 gen_helper_vfp_negd(tcg_rd, tcg_rn);
7828 case 0x7f: /* FSQRT */
7829 gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
7831 case 0x1a: /* FCVTNS */
7832 case 0x1b: /* FCVTMS */
7833 case 0x1c: /* FCVTAS */
7834 case 0x3a: /* FCVTPS */
7835 case 0x3b: /* FCVTZS */
7837 TCGv_i32 tcg_shift = tcg_const_i32(0);
7838 gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7839 tcg_temp_free_i32(tcg_shift);
7842 case 0x5a: /* FCVTNU */
7843 case 0x5b: /* FCVTMU */
7844 case 0x5c: /* FCVTAU */
7845 case 0x7a: /* FCVTPU */
7846 case 0x7b: /* FCVTZU */
7848 TCGv_i32 tcg_shift = tcg_const_i32(0);
7849 gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7850 tcg_temp_free_i32(tcg_shift);
7853 case 0x18: /* FRINTN */
7854 case 0x19: /* FRINTM */
7855 case 0x38: /* FRINTP */
7856 case 0x39: /* FRINTZ */
7857 case 0x58: /* FRINTA */
7858 case 0x79: /* FRINTI */
7859 gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
7861 case 0x59: /* FRINTX */
7862 gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
7865 g_assert_not_reached();
7869 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
7870 bool is_scalar, bool is_u, bool is_q,
7871 int size, int rn, int rd)
7873 bool is_double = (size == MO_64);
7876 if (!fp_access_check(s)) {
7880 fpst = get_fpstatus_ptr(size == MO_16);
7883 TCGv_i64 tcg_op = tcg_temp_new_i64();
7884 TCGv_i64 tcg_zero = tcg_const_i64(0);
7885 TCGv_i64 tcg_res = tcg_temp_new_i64();
7886 NeonGenTwoDoubleOPFn *genfn;
7891 case 0x2e: /* FCMLT (zero) */
7894 case 0x2c: /* FCMGT (zero) */
7895 genfn = gen_helper_neon_cgt_f64;
7897 case 0x2d: /* FCMEQ (zero) */
7898 genfn = gen_helper_neon_ceq_f64;
7900 case 0x6d: /* FCMLE (zero) */
7903 case 0x6c: /* FCMGE (zero) */
7904 genfn = gen_helper_neon_cge_f64;
7907 g_assert_not_reached();
7910 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7911 read_vec_element(s, tcg_op, rn, pass, MO_64);
7913 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7915 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7917 write_vec_element(s, tcg_res, rd, pass, MO_64);
7919 tcg_temp_free_i64(tcg_res);
7920 tcg_temp_free_i64(tcg_zero);
7921 tcg_temp_free_i64(tcg_op);
7923 clear_vec_high(s, !is_scalar, rd);
7925 TCGv_i32 tcg_op = tcg_temp_new_i32();
7926 TCGv_i32 tcg_zero = tcg_const_i32(0);
7927 TCGv_i32 tcg_res = tcg_temp_new_i32();
7928 NeonGenTwoSingleOPFn *genfn;
7930 int pass, maxpasses;
7932 if (size == MO_16) {
7934 case 0x2e: /* FCMLT (zero) */
7937 case 0x2c: /* FCMGT (zero) */
7938 genfn = gen_helper_advsimd_cgt_f16;
7940 case 0x2d: /* FCMEQ (zero) */
7941 genfn = gen_helper_advsimd_ceq_f16;
7943 case 0x6d: /* FCMLE (zero) */
7946 case 0x6c: /* FCMGE (zero) */
7947 genfn = gen_helper_advsimd_cge_f16;
7950 g_assert_not_reached();
7954 case 0x2e: /* FCMLT (zero) */
7957 case 0x2c: /* FCMGT (zero) */
7958 genfn = gen_helper_neon_cgt_f32;
7960 case 0x2d: /* FCMEQ (zero) */
7961 genfn = gen_helper_neon_ceq_f32;
7963 case 0x6d: /* FCMLE (zero) */
7966 case 0x6c: /* FCMGE (zero) */
7967 genfn = gen_helper_neon_cge_f32;
7970 g_assert_not_reached();
7977 int vector_size = 8 << is_q;
7978 maxpasses = vector_size >> size;
7981 for (pass = 0; pass < maxpasses; pass++) {
7982 read_vec_element_i32(s, tcg_op, rn, pass, size);
7984 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7986 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7989 write_fp_sreg(s, rd, tcg_res);
7991 write_vec_element_i32(s, tcg_res, rd, pass, size);
7994 tcg_temp_free_i32(tcg_res);
7995 tcg_temp_free_i32(tcg_zero);
7996 tcg_temp_free_i32(tcg_op);
7998 clear_vec_high(s, is_q, rd);
8002 tcg_temp_free_ptr(fpst);
8005 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
8006 bool is_scalar, bool is_u, bool is_q,
8007 int size, int rn, int rd)
8009 bool is_double = (size == 3);
8010 TCGv_ptr fpst = get_fpstatus_ptr(false);
8013 TCGv_i64 tcg_op = tcg_temp_new_i64();
8014 TCGv_i64 tcg_res = tcg_temp_new_i64();
8017 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
8018 read_vec_element(s, tcg_op, rn, pass, MO_64);
8020 case 0x3d: /* FRECPE */
8021 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
8023 case 0x3f: /* FRECPX */
8024 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
8026 case 0x7d: /* FRSQRTE */
8027 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
8030 g_assert_not_reached();
8032 write_vec_element(s, tcg_res, rd, pass, MO_64);
8034 tcg_temp_free_i64(tcg_res);
8035 tcg_temp_free_i64(tcg_op);
8036 clear_vec_high(s, !is_scalar, rd);
8038 TCGv_i32 tcg_op = tcg_temp_new_i32();
8039 TCGv_i32 tcg_res = tcg_temp_new_i32();
8040 int pass, maxpasses;
8045 maxpasses = is_q ? 4 : 2;
8048 for (pass = 0; pass < maxpasses; pass++) {
8049 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
8052 case 0x3c: /* URECPE */
8053 gen_helper_recpe_u32(tcg_res, tcg_op, fpst);
8055 case 0x3d: /* FRECPE */
8056 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
8058 case 0x3f: /* FRECPX */
8059 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
8061 case 0x7d: /* FRSQRTE */
8062 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
8065 g_assert_not_reached();
8069 write_fp_sreg(s, rd, tcg_res);
8071 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
8074 tcg_temp_free_i32(tcg_res);
8075 tcg_temp_free_i32(tcg_op);
8077 clear_vec_high(s, is_q, rd);
8080 tcg_temp_free_ptr(fpst);
8083 static void handle_2misc_narrow(DisasContext *s, bool scalar,
8084 int opcode, bool u, bool is_q,
8085 int size, int rn, int rd)
8087 /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
8088 * in the source becomes a size element in the destination).
8091 TCGv_i32 tcg_res[2];
8092 int destelt = is_q ? 2 : 0;
8093 int passes = scalar ? 1 : 2;
8096 tcg_res[1] = tcg_const_i32(0);
8099 for (pass = 0; pass < passes; pass++) {
8100 TCGv_i64 tcg_op = tcg_temp_new_i64();
8101 NeonGenNarrowFn *genfn = NULL;
8102 NeonGenNarrowEnvFn *genenvfn = NULL;
8105 read_vec_element(s, tcg_op, rn, pass, size + 1);
8107 read_vec_element(s, tcg_op, rn, pass, MO_64);
8109 tcg_res[pass] = tcg_temp_new_i32();
8112 case 0x12: /* XTN, SQXTUN */
8114 static NeonGenNarrowFn * const xtnfns[3] = {
8115 gen_helper_neon_narrow_u8,
8116 gen_helper_neon_narrow_u16,
8117 tcg_gen_extrl_i64_i32,
8119 static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
8120 gen_helper_neon_unarrow_sat8,
8121 gen_helper_neon_unarrow_sat16,
8122 gen_helper_neon_unarrow_sat32,
8125 genenvfn = sqxtunfns[size];
8127 genfn = xtnfns[size];
8131 case 0x14: /* SQXTN, UQXTN */
8133 static NeonGenNarrowEnvFn * const fns[3][2] = {
8134 { gen_helper_neon_narrow_sat_s8,
8135 gen_helper_neon_narrow_sat_u8 },
8136 { gen_helper_neon_narrow_sat_s16,
8137 gen_helper_neon_narrow_sat_u16 },
8138 { gen_helper_neon_narrow_sat_s32,
8139 gen_helper_neon_narrow_sat_u32 },
8141 genenvfn = fns[size][u];
8144 case 0x16: /* FCVTN, FCVTN2 */
8145 /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
8147 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
8149 TCGv_i32 tcg_lo = tcg_temp_new_i32();
8150 TCGv_i32 tcg_hi = tcg_temp_new_i32();
8151 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
8152 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, cpu_env);
8153 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, cpu_env);
8154 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
8155 tcg_temp_free_i32(tcg_lo);
8156 tcg_temp_free_i32(tcg_hi);
8159 case 0x56: /* FCVTXN, FCVTXN2 */
8160 /* 64 bit to 32 bit float conversion
8161 * with von Neumann rounding (round to odd)
8164 gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
8167 g_assert_not_reached();
8171 genfn(tcg_res[pass], tcg_op);
8172 } else if (genenvfn) {
8173 genenvfn(tcg_res[pass], cpu_env, tcg_op);
8176 tcg_temp_free_i64(tcg_op);
8179 for (pass = 0; pass < 2; pass++) {
8180 write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
8181 tcg_temp_free_i32(tcg_res[pass]);
8183 clear_vec_high(s, is_q, rd);
8186 /* Remaining saturating accumulating ops */
8187 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
8188 bool is_q, int size, int rn, int rd)
8190 bool is_double = (size == 3);
8193 TCGv_i64 tcg_rn = tcg_temp_new_i64();
8194 TCGv_i64 tcg_rd = tcg_temp_new_i64();
8197 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
8198 read_vec_element(s, tcg_rn, rn, pass, MO_64);
8199 read_vec_element(s, tcg_rd, rd, pass, MO_64);
8201 if (is_u) { /* USQADD */
8202 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8203 } else { /* SUQADD */
8204 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8206 write_vec_element(s, tcg_rd, rd, pass, MO_64);
8208 tcg_temp_free_i64(tcg_rd);
8209 tcg_temp_free_i64(tcg_rn);
8210 clear_vec_high(s, !is_scalar, rd);
8212 TCGv_i32 tcg_rn = tcg_temp_new_i32();
8213 TCGv_i32 tcg_rd = tcg_temp_new_i32();
8214 int pass, maxpasses;
8219 maxpasses = is_q ? 4 : 2;
8222 for (pass = 0; pass < maxpasses; pass++) {
8224 read_vec_element_i32(s, tcg_rn, rn, pass, size);
8225 read_vec_element_i32(s, tcg_rd, rd, pass, size);
8227 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
8228 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
8231 if (is_u) { /* USQADD */
8234 gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8237 gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8240 gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8243 g_assert_not_reached();
8245 } else { /* SUQADD */
8248 gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8251 gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8254 gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8257 g_assert_not_reached();
8262 TCGv_i64 tcg_zero = tcg_const_i64(0);
8263 write_vec_element(s, tcg_zero, rd, 0, MO_64);
8264 tcg_temp_free_i64(tcg_zero);
8266 write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
8268 tcg_temp_free_i32(tcg_rd);
8269 tcg_temp_free_i32(tcg_rn);
8270 clear_vec_high(s, is_q, rd);
8274 /* AdvSIMD scalar two reg misc
8275 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
8276 * +-----+---+-----------+------+-----------+--------+-----+------+------+
8277 * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
8278 * +-----+---+-----------+------+-----------+--------+-----+------+------+
8280 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
8282 int rd = extract32(insn, 0, 5);
8283 int rn = extract32(insn, 5, 5);
8284 int opcode = extract32(insn, 12, 5);
8285 int size = extract32(insn, 22, 2);
8286 bool u = extract32(insn, 29, 1);
8287 bool is_fcvt = false;
8290 TCGv_ptr tcg_fpstatus;
8293 case 0x3: /* USQADD / SUQADD*/
8294 if (!fp_access_check(s)) {
8297 handle_2misc_satacc(s, true, u, false, size, rn, rd);
8299 case 0x7: /* SQABS / SQNEG */
8301 case 0xa: /* CMLT */
8303 unallocated_encoding(s);
8307 case 0x8: /* CMGT, CMGE */
8308 case 0x9: /* CMEQ, CMLE */
8309 case 0xb: /* ABS, NEG */
8311 unallocated_encoding(s);
8315 case 0x12: /* SQXTUN */
8317 unallocated_encoding(s);
8321 case 0x14: /* SQXTN, UQXTN */
8323 unallocated_encoding(s);
8326 if (!fp_access_check(s)) {
8329 handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
8334 /* Floating point: U, size[1] and opcode indicate operation;
8335 * size[0] indicates single or double precision.
8337 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
8338 size = extract32(size, 0, 1) ? 3 : 2;
8340 case 0x2c: /* FCMGT (zero) */
8341 case 0x2d: /* FCMEQ (zero) */
8342 case 0x2e: /* FCMLT (zero) */
8343 case 0x6c: /* FCMGE (zero) */
8344 case 0x6d: /* FCMLE (zero) */
8345 handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
8347 case 0x1d: /* SCVTF */
8348 case 0x5d: /* UCVTF */
8350 bool is_signed = (opcode == 0x1d);
8351 if (!fp_access_check(s)) {
8354 handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
8357 case 0x3d: /* FRECPE */
8358 case 0x3f: /* FRECPX */
8359 case 0x7d: /* FRSQRTE */
8360 if (!fp_access_check(s)) {
8363 handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
8365 case 0x1a: /* FCVTNS */
8366 case 0x1b: /* FCVTMS */
8367 case 0x3a: /* FCVTPS */
8368 case 0x3b: /* FCVTZS */
8369 case 0x5a: /* FCVTNU */
8370 case 0x5b: /* FCVTMU */
8371 case 0x7a: /* FCVTPU */
8372 case 0x7b: /* FCVTZU */
8374 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
8376 case 0x1c: /* FCVTAS */
8377 case 0x5c: /* FCVTAU */
8378 /* TIEAWAY doesn't fit in the usual rounding mode encoding */
8380 rmode = FPROUNDING_TIEAWAY;
8382 case 0x56: /* FCVTXN, FCVTXN2 */
8384 unallocated_encoding(s);
8387 if (!fp_access_check(s)) {
8390 handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
8393 unallocated_encoding(s);
8398 unallocated_encoding(s);
8402 if (!fp_access_check(s)) {
8407 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
8408 tcg_fpstatus = get_fpstatus_ptr(false);
8409 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
8412 tcg_fpstatus = NULL;
8416 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
8417 TCGv_i64 tcg_rd = tcg_temp_new_i64();
8419 handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
8420 write_fp_dreg(s, rd, tcg_rd);
8421 tcg_temp_free_i64(tcg_rd);
8422 tcg_temp_free_i64(tcg_rn);
8424 TCGv_i32 tcg_rn = tcg_temp_new_i32();
8425 TCGv_i32 tcg_rd = tcg_temp_new_i32();
8427 read_vec_element_i32(s, tcg_rn, rn, 0, size);
8430 case 0x7: /* SQABS, SQNEG */
8432 NeonGenOneOpEnvFn *genfn;
8433 static NeonGenOneOpEnvFn * const fns[3][2] = {
8434 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
8435 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
8436 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
8438 genfn = fns[size][u];
8439 genfn(tcg_rd, cpu_env, tcg_rn);
8442 case 0x1a: /* FCVTNS */
8443 case 0x1b: /* FCVTMS */
8444 case 0x1c: /* FCVTAS */
8445 case 0x3a: /* FCVTPS */
8446 case 0x3b: /* FCVTZS */
8448 TCGv_i32 tcg_shift = tcg_const_i32(0);
8449 gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
8450 tcg_temp_free_i32(tcg_shift);
8453 case 0x5a: /* FCVTNU */
8454 case 0x5b: /* FCVTMU */
8455 case 0x5c: /* FCVTAU */
8456 case 0x7a: /* FCVTPU */
8457 case 0x7b: /* FCVTZU */
8459 TCGv_i32 tcg_shift = tcg_const_i32(0);
8460 gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
8461 tcg_temp_free_i32(tcg_shift);
8465 g_assert_not_reached();
8468 write_fp_sreg(s, rd, tcg_rd);
8469 tcg_temp_free_i32(tcg_rd);
8470 tcg_temp_free_i32(tcg_rn);
8474 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
8475 tcg_temp_free_i32(tcg_rmode);
8476 tcg_temp_free_ptr(tcg_fpstatus);
8480 static void gen_ssra8_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8482 tcg_gen_vec_sar8i_i64(a, a, shift);
8483 tcg_gen_vec_add8_i64(d, d, a);
8486 static void gen_ssra16_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8488 tcg_gen_vec_sar16i_i64(a, a, shift);
8489 tcg_gen_vec_add16_i64(d, d, a);
8492 static void gen_ssra32_i32(TCGv_i32 d, TCGv_i32 a, int32_t shift)
8494 tcg_gen_sari_i32(a, a, shift);
8495 tcg_gen_add_i32(d, d, a);
8498 static void gen_ssra64_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8500 tcg_gen_sari_i64(a, a, shift);
8501 tcg_gen_add_i64(d, d, a);
8504 static void gen_ssra_vec(unsigned vece, TCGv_vec d, TCGv_vec a, int64_t sh)
8506 tcg_gen_sari_vec(vece, a, a, sh);
8507 tcg_gen_add_vec(vece, d, d, a);
8510 static void gen_usra8_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8512 tcg_gen_vec_shr8i_i64(a, a, shift);
8513 tcg_gen_vec_add8_i64(d, d, a);
8516 static void gen_usra16_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8518 tcg_gen_vec_shr16i_i64(a, a, shift);
8519 tcg_gen_vec_add16_i64(d, d, a);
8522 static void gen_usra32_i32(TCGv_i32 d, TCGv_i32 a, int32_t shift)
8524 tcg_gen_shri_i32(a, a, shift);
8525 tcg_gen_add_i32(d, d, a);
8528 static void gen_usra64_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8530 tcg_gen_shri_i64(a, a, shift);
8531 tcg_gen_add_i64(d, d, a);
8534 static void gen_usra_vec(unsigned vece, TCGv_vec d, TCGv_vec a, int64_t sh)
8536 tcg_gen_shri_vec(vece, a, a, sh);
8537 tcg_gen_add_vec(vece, d, d, a);
8540 static void gen_shr8_ins_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8542 uint64_t mask = dup_const(MO_8, 0xff >> shift);
8543 TCGv_i64 t = tcg_temp_new_i64();
8545 tcg_gen_shri_i64(t, a, shift);
8546 tcg_gen_andi_i64(t, t, mask);
8547 tcg_gen_andi_i64(d, d, ~mask);
8548 tcg_gen_or_i64(d, d, t);
8549 tcg_temp_free_i64(t);
8552 static void gen_shr16_ins_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8554 uint64_t mask = dup_const(MO_16, 0xffff >> shift);
8555 TCGv_i64 t = tcg_temp_new_i64();
8557 tcg_gen_shri_i64(t, a, shift);
8558 tcg_gen_andi_i64(t, t, mask);
8559 tcg_gen_andi_i64(d, d, ~mask);
8560 tcg_gen_or_i64(d, d, t);
8561 tcg_temp_free_i64(t);
8564 static void gen_shr32_ins_i32(TCGv_i32 d, TCGv_i32 a, int32_t shift)
8566 tcg_gen_shri_i32(a, a, shift);
8567 tcg_gen_deposit_i32(d, d, a, 0, 32 - shift);
8570 static void gen_shr64_ins_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8572 tcg_gen_shri_i64(a, a, shift);
8573 tcg_gen_deposit_i64(d, d, a, 0, 64 - shift);
8576 static void gen_shr_ins_vec(unsigned vece, TCGv_vec d, TCGv_vec a, int64_t sh)
8578 uint64_t mask = (2ull << ((8 << vece) - 1)) - 1;
8579 TCGv_vec t = tcg_temp_new_vec_matching(d);
8580 TCGv_vec m = tcg_temp_new_vec_matching(d);
8582 tcg_gen_dupi_vec(vece, m, mask ^ (mask >> sh));
8583 tcg_gen_shri_vec(vece, t, a, sh);
8584 tcg_gen_and_vec(vece, d, d, m);
8585 tcg_gen_or_vec(vece, d, d, t);
8587 tcg_temp_free_vec(t);
8588 tcg_temp_free_vec(m);
8591 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
8592 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
8593 int immh, int immb, int opcode, int rn, int rd)
8595 static const GVecGen2i ssra_op[4] = {
8596 { .fni8 = gen_ssra8_i64,
8597 .fniv = gen_ssra_vec,
8599 .opc = INDEX_op_sari_vec,
8601 { .fni8 = gen_ssra16_i64,
8602 .fniv = gen_ssra_vec,
8604 .opc = INDEX_op_sari_vec,
8606 { .fni4 = gen_ssra32_i32,
8607 .fniv = gen_ssra_vec,
8609 .opc = INDEX_op_sari_vec,
8611 { .fni8 = gen_ssra64_i64,
8612 .fniv = gen_ssra_vec,
8613 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
8615 .opc = INDEX_op_sari_vec,
8618 static const GVecGen2i usra_op[4] = {
8619 { .fni8 = gen_usra8_i64,
8620 .fniv = gen_usra_vec,
8622 .opc = INDEX_op_shri_vec,
8624 { .fni8 = gen_usra16_i64,
8625 .fniv = gen_usra_vec,
8627 .opc = INDEX_op_shri_vec,
8629 { .fni4 = gen_usra32_i32,
8630 .fniv = gen_usra_vec,
8632 .opc = INDEX_op_shri_vec,
8634 { .fni8 = gen_usra64_i64,
8635 .fniv = gen_usra_vec,
8636 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
8638 .opc = INDEX_op_shri_vec,
8641 static const GVecGen2i sri_op[4] = {
8642 { .fni8 = gen_shr8_ins_i64,
8643 .fniv = gen_shr_ins_vec,
8645 .opc = INDEX_op_shri_vec,
8647 { .fni8 = gen_shr16_ins_i64,
8648 .fniv = gen_shr_ins_vec,
8650 .opc = INDEX_op_shri_vec,
8652 { .fni4 = gen_shr32_ins_i32,
8653 .fniv = gen_shr_ins_vec,
8655 .opc = INDEX_op_shri_vec,
8657 { .fni8 = gen_shr64_ins_i64,
8658 .fniv = gen_shr_ins_vec,
8659 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
8661 .opc = INDEX_op_shri_vec,
8665 int size = 32 - clz32(immh) - 1;
8666 int immhb = immh << 3 | immb;
8667 int shift = 2 * (8 << size) - immhb;
8668 bool accumulate = false;
8669 int dsize = is_q ? 128 : 64;
8670 int esize = 8 << size;
8671 int elements = dsize/esize;
8672 TCGMemOp memop = size | (is_u ? 0 : MO_SIGN);
8673 TCGv_i64 tcg_rn = new_tmp_a64(s);
8674 TCGv_i64 tcg_rd = new_tmp_a64(s);
8676 uint64_t round_const;
8679 if (extract32(immh, 3, 1) && !is_q) {
8680 unallocated_encoding(s);
8684 if (size > 3 && !is_q) {
8685 unallocated_encoding(s);
8689 if (!fp_access_check(s)) {
8694 case 0x02: /* SSRA / USRA (accumulate) */
8696 /* Shift count same as element size produces zero to add. */
8697 if (shift == 8 << size) {
8700 gen_gvec_op2i(s, is_q, rd, rn, shift, &usra_op[size]);
8702 /* Shift count same as element size produces all sign to add. */
8703 if (shift == 8 << size) {
8706 gen_gvec_op2i(s, is_q, rd, rn, shift, &ssra_op[size]);
8709 case 0x08: /* SRI */
8710 /* Shift count same as element size is valid but does nothing. */
8711 if (shift == 8 << size) {
8714 gen_gvec_op2i(s, is_q, rd, rn, shift, &sri_op[size]);
8717 case 0x00: /* SSHR / USHR */
8719 if (shift == 8 << size) {
8720 /* Shift count the same size as element size produces zero. */
8721 tcg_gen_gvec_dup8i(vec_full_reg_offset(s, rd),
8722 is_q ? 16 : 8, vec_full_reg_size(s), 0);
8724 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shri, size);
8727 /* Shift count the same size as element size produces all sign. */
8728 if (shift == 8 << size) {
8731 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_sari, size);
8735 case 0x04: /* SRSHR / URSHR (rounding) */
8737 case 0x06: /* SRSRA / URSRA (accum + rounding) */
8741 g_assert_not_reached();
8744 round_const = 1ULL << (shift - 1);
8745 tcg_round = tcg_const_i64(round_const);
8747 for (i = 0; i < elements; i++) {
8748 read_vec_element(s, tcg_rn, rn, i, memop);
8750 read_vec_element(s, tcg_rd, rd, i, memop);
8753 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8754 accumulate, is_u, size, shift);
8756 write_vec_element(s, tcg_rd, rd, i, size);
8758 tcg_temp_free_i64(tcg_round);
8761 clear_vec_high(s, is_q, rd);
8764 static void gen_shl8_ins_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8766 uint64_t mask = dup_const(MO_8, 0xff << shift);
8767 TCGv_i64 t = tcg_temp_new_i64();
8769 tcg_gen_shli_i64(t, a, shift);
8770 tcg_gen_andi_i64(t, t, mask);
8771 tcg_gen_andi_i64(d, d, ~mask);
8772 tcg_gen_or_i64(d, d, t);
8773 tcg_temp_free_i64(t);
8776 static void gen_shl16_ins_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8778 uint64_t mask = dup_const(MO_16, 0xffff << shift);
8779 TCGv_i64 t = tcg_temp_new_i64();
8781 tcg_gen_shli_i64(t, a, shift);
8782 tcg_gen_andi_i64(t, t, mask);
8783 tcg_gen_andi_i64(d, d, ~mask);
8784 tcg_gen_or_i64(d, d, t);
8785 tcg_temp_free_i64(t);
8788 static void gen_shl32_ins_i32(TCGv_i32 d, TCGv_i32 a, int32_t shift)
8790 tcg_gen_deposit_i32(d, d, a, shift, 32 - shift);
8793 static void gen_shl64_ins_i64(TCGv_i64 d, TCGv_i64 a, int64_t shift)
8795 tcg_gen_deposit_i64(d, d, a, shift, 64 - shift);
8798 static void gen_shl_ins_vec(unsigned vece, TCGv_vec d, TCGv_vec a, int64_t sh)
8800 uint64_t mask = (1ull << sh) - 1;
8801 TCGv_vec t = tcg_temp_new_vec_matching(d);
8802 TCGv_vec m = tcg_temp_new_vec_matching(d);
8804 tcg_gen_dupi_vec(vece, m, mask);
8805 tcg_gen_shli_vec(vece, t, a, sh);
8806 tcg_gen_and_vec(vece, d, d, m);
8807 tcg_gen_or_vec(vece, d, d, t);
8809 tcg_temp_free_vec(t);
8810 tcg_temp_free_vec(m);
8813 /* SHL/SLI - Vector shift left */
8814 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
8815 int immh, int immb, int opcode, int rn, int rd)
8817 static const GVecGen2i shi_op[4] = {
8818 { .fni8 = gen_shl8_ins_i64,
8819 .fniv = gen_shl_ins_vec,
8820 .opc = INDEX_op_shli_vec,
8821 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
8824 { .fni8 = gen_shl16_ins_i64,
8825 .fniv = gen_shl_ins_vec,
8826 .opc = INDEX_op_shli_vec,
8827 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
8830 { .fni4 = gen_shl32_ins_i32,
8831 .fniv = gen_shl_ins_vec,
8832 .opc = INDEX_op_shli_vec,
8833 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
8836 { .fni8 = gen_shl64_ins_i64,
8837 .fniv = gen_shl_ins_vec,
8838 .opc = INDEX_op_shli_vec,
8839 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
8843 int size = 32 - clz32(immh) - 1;
8844 int immhb = immh << 3 | immb;
8845 int shift = immhb - (8 << size);
8847 if (extract32(immh, 3, 1) && !is_q) {
8848 unallocated_encoding(s);
8852 if (size > 3 && !is_q) {
8853 unallocated_encoding(s);
8857 if (!fp_access_check(s)) {
8862 gen_gvec_op2i(s, is_q, rd, rn, shift, &shi_op[size]);
8864 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
8868 /* USHLL/SHLL - Vector shift left with widening */
8869 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
8870 int immh, int immb, int opcode, int rn, int rd)
8872 int size = 32 - clz32(immh) - 1;
8873 int immhb = immh << 3 | immb;
8874 int shift = immhb - (8 << size);
8876 int esize = 8 << size;
8877 int elements = dsize/esize;
8878 TCGv_i64 tcg_rn = new_tmp_a64(s);
8879 TCGv_i64 tcg_rd = new_tmp_a64(s);
8883 unallocated_encoding(s);
8887 if (!fp_access_check(s)) {
8891 /* For the LL variants the store is larger than the load,
8892 * so if rd == rn we would overwrite parts of our input.
8893 * So load everything right now and use shifts in the main loop.
8895 read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
8897 for (i = 0; i < elements; i++) {
8898 tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
8899 ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
8900 tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
8901 write_vec_element(s, tcg_rd, rd, i, size + 1);
8905 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
8906 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
8907 int immh, int immb, int opcode, int rn, int rd)
8909 int immhb = immh << 3 | immb;
8910 int size = 32 - clz32(immh) - 1;
8912 int esize = 8 << size;
8913 int elements = dsize/esize;
8914 int shift = (2 * esize) - immhb;
8915 bool round = extract32(opcode, 0, 1);
8916 TCGv_i64 tcg_rn, tcg_rd, tcg_final;
8920 if (extract32(immh, 3, 1)) {
8921 unallocated_encoding(s);
8925 if (!fp_access_check(s)) {
8929 tcg_rn = tcg_temp_new_i64();
8930 tcg_rd = tcg_temp_new_i64();
8931 tcg_final = tcg_temp_new_i64();
8932 read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
8935 uint64_t round_const = 1ULL << (shift - 1);
8936 tcg_round = tcg_const_i64(round_const);
8941 for (i = 0; i < elements; i++) {
8942 read_vec_element(s, tcg_rn, rn, i, size+1);
8943 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8944 false, true, size+1, shift);
8946 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8950 write_vec_element(s, tcg_final, rd, 0, MO_64);
8952 write_vec_element(s, tcg_final, rd, 1, MO_64);
8955 tcg_temp_free_i64(tcg_round);
8957 tcg_temp_free_i64(tcg_rn);
8958 tcg_temp_free_i64(tcg_rd);
8959 tcg_temp_free_i64(tcg_final);
8961 clear_vec_high(s, is_q, rd);
8965 /* AdvSIMD shift by immediate
8966 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
8967 * +---+---+---+-------------+------+------+--------+---+------+------+
8968 * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
8969 * +---+---+---+-------------+------+------+--------+---+------+------+
8971 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
8973 int rd = extract32(insn, 0, 5);
8974 int rn = extract32(insn, 5, 5);
8975 int opcode = extract32(insn, 11, 5);
8976 int immb = extract32(insn, 16, 3);
8977 int immh = extract32(insn, 19, 4);
8978 bool is_u = extract32(insn, 29, 1);
8979 bool is_q = extract32(insn, 30, 1);
8982 case 0x08: /* SRI */
8984 unallocated_encoding(s);
8988 case 0x00: /* SSHR / USHR */
8989 case 0x02: /* SSRA / USRA (accumulate) */
8990 case 0x04: /* SRSHR / URSHR (rounding) */
8991 case 0x06: /* SRSRA / URSRA (accum + rounding) */
8992 handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
8994 case 0x0a: /* SHL / SLI */
8995 handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
8997 case 0x10: /* SHRN */
8998 case 0x11: /* RSHRN / SQRSHRUN */
9000 handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
9003 handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
9006 case 0x12: /* SQSHRN / UQSHRN */
9007 case 0x13: /* SQRSHRN / UQRSHRN */
9008 handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
9011 case 0x14: /* SSHLL / USHLL */
9012 handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
9014 case 0x1c: /* SCVTF / UCVTF */
9015 handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
9018 case 0xc: /* SQSHLU */
9020 unallocated_encoding(s);
9023 handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
9025 case 0xe: /* SQSHL, UQSHL */
9026 handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
9028 case 0x1f: /* FCVTZS/ FCVTZU */
9029 handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
9032 unallocated_encoding(s);
9037 /* Generate code to do a "long" addition or subtraction, ie one done in
9038 * TCGv_i64 on vector lanes twice the width specified by size.
9040 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
9041 TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
9043 static NeonGenTwo64OpFn * const fns[3][2] = {
9044 { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
9045 { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
9046 { tcg_gen_add_i64, tcg_gen_sub_i64 },
9048 NeonGenTwo64OpFn *genfn;
9051 genfn = fns[size][is_sub];
9052 genfn(tcg_res, tcg_op1, tcg_op2);
9055 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
9056 int opcode, int rd, int rn, int rm)
9058 /* 3-reg-different widening insns: 64 x 64 -> 128 */
9059 TCGv_i64 tcg_res[2];
9062 tcg_res[0] = tcg_temp_new_i64();
9063 tcg_res[1] = tcg_temp_new_i64();
9065 /* Does this op do an adding accumulate, a subtracting accumulate,
9066 * or no accumulate at all?
9084 read_vec_element(s, tcg_res[0], rd, 0, MO_64);
9085 read_vec_element(s, tcg_res[1], rd, 1, MO_64);
9088 /* size == 2 means two 32x32->64 operations; this is worth special
9089 * casing because we can generally handle it inline.
9092 for (pass = 0; pass < 2; pass++) {
9093 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9094 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9095 TCGv_i64 tcg_passres;
9096 TCGMemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
9098 int elt = pass + is_q * 2;
9100 read_vec_element(s, tcg_op1, rn, elt, memop);
9101 read_vec_element(s, tcg_op2, rm, elt, memop);
9104 tcg_passres = tcg_res[pass];
9106 tcg_passres = tcg_temp_new_i64();
9110 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
9111 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
9113 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
9114 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
9116 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
9117 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
9119 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
9120 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
9122 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
9123 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
9124 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
9126 tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
9127 tcg_temp_free_i64(tcg_tmp1);
9128 tcg_temp_free_i64(tcg_tmp2);
9131 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
9132 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
9133 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
9134 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
9136 case 9: /* SQDMLAL, SQDMLAL2 */
9137 case 11: /* SQDMLSL, SQDMLSL2 */
9138 case 13: /* SQDMULL, SQDMULL2 */
9139 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
9140 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
9141 tcg_passres, tcg_passres);
9144 g_assert_not_reached();
9147 if (opcode == 9 || opcode == 11) {
9148 /* saturating accumulate ops */
9150 tcg_gen_neg_i64(tcg_passres, tcg_passres);
9152 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
9153 tcg_res[pass], tcg_passres);
9154 } else if (accop > 0) {
9155 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
9156 } else if (accop < 0) {
9157 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
9161 tcg_temp_free_i64(tcg_passres);
9164 tcg_temp_free_i64(tcg_op1);
9165 tcg_temp_free_i64(tcg_op2);
9168 /* size 0 or 1, generally helper functions */
9169 for (pass = 0; pass < 2; pass++) {
9170 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9171 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9172 TCGv_i64 tcg_passres;
9173 int elt = pass + is_q * 2;
9175 read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
9176 read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
9179 tcg_passres = tcg_res[pass];
9181 tcg_passres = tcg_temp_new_i64();
9185 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
9186 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
9188 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
9189 static NeonGenWidenFn * const widenfns[2][2] = {
9190 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
9191 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
9193 NeonGenWidenFn *widenfn = widenfns[size][is_u];
9195 widenfn(tcg_op2_64, tcg_op2);
9196 widenfn(tcg_passres, tcg_op1);
9197 gen_neon_addl(size, (opcode == 2), tcg_passres,
9198 tcg_passres, tcg_op2_64);
9199 tcg_temp_free_i64(tcg_op2_64);
9202 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
9203 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
9206 gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
9208 gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
9212 gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
9214 gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
9218 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
9219 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
9220 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
9223 gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
9225 gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
9229 gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
9231 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
9235 case 9: /* SQDMLAL, SQDMLAL2 */
9236 case 11: /* SQDMLSL, SQDMLSL2 */
9237 case 13: /* SQDMULL, SQDMULL2 */
9239 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
9240 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
9241 tcg_passres, tcg_passres);
9243 case 14: /* PMULL */
9245 gen_helper_neon_mull_p8(tcg_passres, tcg_op1, tcg_op2);
9248 g_assert_not_reached();
9250 tcg_temp_free_i32(tcg_op1);
9251 tcg_temp_free_i32(tcg_op2);
9254 if (opcode == 9 || opcode == 11) {
9255 /* saturating accumulate ops */
9257 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
9259 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
9263 gen_neon_addl(size, (accop < 0), tcg_res[pass],
9264 tcg_res[pass], tcg_passres);
9266 tcg_temp_free_i64(tcg_passres);
9271 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
9272 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
9273 tcg_temp_free_i64(tcg_res[0]);
9274 tcg_temp_free_i64(tcg_res[1]);
9277 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
9278 int opcode, int rd, int rn, int rm)
9280 TCGv_i64 tcg_res[2];
9281 int part = is_q ? 2 : 0;
9284 for (pass = 0; pass < 2; pass++) {
9285 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9286 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9287 TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
9288 static NeonGenWidenFn * const widenfns[3][2] = {
9289 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
9290 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
9291 { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
9293 NeonGenWidenFn *widenfn = widenfns[size][is_u];
9295 read_vec_element(s, tcg_op1, rn, pass, MO_64);
9296 read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
9297 widenfn(tcg_op2_wide, tcg_op2);
9298 tcg_temp_free_i32(tcg_op2);
9299 tcg_res[pass] = tcg_temp_new_i64();
9300 gen_neon_addl(size, (opcode == 3),
9301 tcg_res[pass], tcg_op1, tcg_op2_wide);
9302 tcg_temp_free_i64(tcg_op1);
9303 tcg_temp_free_i64(tcg_op2_wide);
9306 for (pass = 0; pass < 2; pass++) {
9307 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9308 tcg_temp_free_i64(tcg_res[pass]);
9312 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
9314 tcg_gen_addi_i64(in, in, 1U << 31);
9315 tcg_gen_extrh_i64_i32(res, in);
9318 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
9319 int opcode, int rd, int rn, int rm)
9321 TCGv_i32 tcg_res[2];
9322 int part = is_q ? 2 : 0;
9325 for (pass = 0; pass < 2; pass++) {
9326 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9327 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9328 TCGv_i64 tcg_wideres = tcg_temp_new_i64();
9329 static NeonGenNarrowFn * const narrowfns[3][2] = {
9330 { gen_helper_neon_narrow_high_u8,
9331 gen_helper_neon_narrow_round_high_u8 },
9332 { gen_helper_neon_narrow_high_u16,
9333 gen_helper_neon_narrow_round_high_u16 },
9334 { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
9336 NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
9338 read_vec_element(s, tcg_op1, rn, pass, MO_64);
9339 read_vec_element(s, tcg_op2, rm, pass, MO_64);
9341 gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
9343 tcg_temp_free_i64(tcg_op1);
9344 tcg_temp_free_i64(tcg_op2);
9346 tcg_res[pass] = tcg_temp_new_i32();
9347 gennarrow(tcg_res[pass], tcg_wideres);
9348 tcg_temp_free_i64(tcg_wideres);
9351 for (pass = 0; pass < 2; pass++) {
9352 write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
9353 tcg_temp_free_i32(tcg_res[pass]);
9355 clear_vec_high(s, is_q, rd);
9358 static void handle_pmull_64(DisasContext *s, int is_q, int rd, int rn, int rm)
9360 /* PMULL of 64 x 64 -> 128 is an odd special case because it
9361 * is the only three-reg-diff instruction which produces a
9362 * 128-bit wide result from a single operation. However since
9363 * it's possible to calculate the two halves more or less
9364 * separately we just use two helper calls.
9366 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9367 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9368 TCGv_i64 tcg_res = tcg_temp_new_i64();
9370 read_vec_element(s, tcg_op1, rn, is_q, MO_64);
9371 read_vec_element(s, tcg_op2, rm, is_q, MO_64);
9372 gen_helper_neon_pmull_64_lo(tcg_res, tcg_op1, tcg_op2);
9373 write_vec_element(s, tcg_res, rd, 0, MO_64);
9374 gen_helper_neon_pmull_64_hi(tcg_res, tcg_op1, tcg_op2);
9375 write_vec_element(s, tcg_res, rd, 1, MO_64);
9377 tcg_temp_free_i64(tcg_op1);
9378 tcg_temp_free_i64(tcg_op2);
9379 tcg_temp_free_i64(tcg_res);
9382 /* AdvSIMD three different
9383 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
9384 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
9385 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
9386 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
9388 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
9390 /* Instructions in this group fall into three basic classes
9391 * (in each case with the operation working on each element in
9392 * the input vectors):
9393 * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
9395 * (2) wide 64 x 128 -> 128
9396 * (3) narrowing 128 x 128 -> 64
9397 * Here we do initial decode, catch unallocated cases and
9398 * dispatch to separate functions for each class.
9400 int is_q = extract32(insn, 30, 1);
9401 int is_u = extract32(insn, 29, 1);
9402 int size = extract32(insn, 22, 2);
9403 int opcode = extract32(insn, 12, 4);
9404 int rm = extract32(insn, 16, 5);
9405 int rn = extract32(insn, 5, 5);
9406 int rd = extract32(insn, 0, 5);
9409 case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
9410 case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
9411 /* 64 x 128 -> 128 */
9413 unallocated_encoding(s);
9416 if (!fp_access_check(s)) {
9419 handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
9421 case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
9422 case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
9423 /* 128 x 128 -> 64 */
9425 unallocated_encoding(s);
9428 if (!fp_access_check(s)) {
9431 handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
9433 case 14: /* PMULL, PMULL2 */
9434 if (is_u || size == 1 || size == 2) {
9435 unallocated_encoding(s);
9439 if (!arm_dc_feature(s, ARM_FEATURE_V8_PMULL)) {
9440 unallocated_encoding(s);
9443 if (!fp_access_check(s)) {
9446 handle_pmull_64(s, is_q, rd, rn, rm);
9450 case 9: /* SQDMLAL, SQDMLAL2 */
9451 case 11: /* SQDMLSL, SQDMLSL2 */
9452 case 13: /* SQDMULL, SQDMULL2 */
9453 if (is_u || size == 0) {
9454 unallocated_encoding(s);
9458 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
9459 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
9460 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
9461 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
9462 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
9463 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
9464 case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
9465 /* 64 x 64 -> 128 */
9467 unallocated_encoding(s);
9471 if (!fp_access_check(s)) {
9475 handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
9478 /* opcode 15 not allocated */
9479 unallocated_encoding(s);
9484 static void gen_bsl_i64(TCGv_i64 rd, TCGv_i64 rn, TCGv_i64 rm)
9486 tcg_gen_xor_i64(rn, rn, rm);
9487 tcg_gen_and_i64(rn, rn, rd);
9488 tcg_gen_xor_i64(rd, rm, rn);
9491 static void gen_bit_i64(TCGv_i64 rd, TCGv_i64 rn, TCGv_i64 rm)
9493 tcg_gen_xor_i64(rn, rn, rd);
9494 tcg_gen_and_i64(rn, rn, rm);
9495 tcg_gen_xor_i64(rd, rd, rn);
9498 static void gen_bif_i64(TCGv_i64 rd, TCGv_i64 rn, TCGv_i64 rm)
9500 tcg_gen_xor_i64(rn, rn, rd);
9501 tcg_gen_andc_i64(rn, rn, rm);
9502 tcg_gen_xor_i64(rd, rd, rn);
9505 static void gen_bsl_vec(unsigned vece, TCGv_vec rd, TCGv_vec rn, TCGv_vec rm)
9507 tcg_gen_xor_vec(vece, rn, rn, rm);
9508 tcg_gen_and_vec(vece, rn, rn, rd);
9509 tcg_gen_xor_vec(vece, rd, rm, rn);
9512 static void gen_bit_vec(unsigned vece, TCGv_vec rd, TCGv_vec rn, TCGv_vec rm)
9514 tcg_gen_xor_vec(vece, rn, rn, rd);
9515 tcg_gen_and_vec(vece, rn, rn, rm);
9516 tcg_gen_xor_vec(vece, rd, rd, rn);
9519 static void gen_bif_vec(unsigned vece, TCGv_vec rd, TCGv_vec rn, TCGv_vec rm)
9521 tcg_gen_xor_vec(vece, rn, rn, rd);
9522 tcg_gen_andc_vec(vece, rn, rn, rm);
9523 tcg_gen_xor_vec(vece, rd, rd, rn);
9526 /* Logic op (opcode == 3) subgroup of C3.6.16. */
9527 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
9529 static const GVecGen3 bsl_op = {
9530 .fni8 = gen_bsl_i64,
9531 .fniv = gen_bsl_vec,
9532 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
9535 static const GVecGen3 bit_op = {
9536 .fni8 = gen_bit_i64,
9537 .fniv = gen_bit_vec,
9538 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
9541 static const GVecGen3 bif_op = {
9542 .fni8 = gen_bif_i64,
9543 .fniv = gen_bif_vec,
9544 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
9548 int rd = extract32(insn, 0, 5);
9549 int rn = extract32(insn, 5, 5);
9550 int rm = extract32(insn, 16, 5);
9551 int size = extract32(insn, 22, 2);
9552 bool is_u = extract32(insn, 29, 1);
9553 bool is_q = extract32(insn, 30, 1);
9555 if (!fp_access_check(s)) {
9559 switch (size + 4 * is_u) {
9561 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
9564 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
9567 if (rn == rm) { /* MOV */
9568 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_mov, 0);
9570 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
9574 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
9577 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
9580 case 5: /* BSL bitwise select */
9581 gen_gvec_op3(s, is_q, rd, rn, rm, &bsl_op);
9583 case 6: /* BIT, bitwise insert if true */
9584 gen_gvec_op3(s, is_q, rd, rn, rm, &bit_op);
9586 case 7: /* BIF, bitwise insert if false */
9587 gen_gvec_op3(s, is_q, rd, rn, rm, &bif_op);
9591 g_assert_not_reached();
9595 /* Helper functions for 32 bit comparisons */
9596 static void gen_max_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9598 tcg_gen_movcond_i32(TCG_COND_GE, res, op1, op2, op1, op2);
9601 static void gen_max_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9603 tcg_gen_movcond_i32(TCG_COND_GEU, res, op1, op2, op1, op2);
9606 static void gen_min_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9608 tcg_gen_movcond_i32(TCG_COND_LE, res, op1, op2, op1, op2);
9611 static void gen_min_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9613 tcg_gen_movcond_i32(TCG_COND_LEU, res, op1, op2, op1, op2);
9616 /* Pairwise op subgroup of C3.6.16.
9618 * This is called directly or via the handle_3same_float for float pairwise
9619 * operations where the opcode and size are calculated differently.
9621 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
9622 int size, int rn, int rm, int rd)
9627 /* Floating point operations need fpst */
9628 if (opcode >= 0x58) {
9629 fpst = get_fpstatus_ptr(false);
9634 if (!fp_access_check(s)) {
9638 /* These operations work on the concatenated rm:rn, with each pair of
9639 * adjacent elements being operated on to produce an element in the result.
9642 TCGv_i64 tcg_res[2];
9644 for (pass = 0; pass < 2; pass++) {
9645 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9646 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9647 int passreg = (pass == 0) ? rn : rm;
9649 read_vec_element(s, tcg_op1, passreg, 0, MO_64);
9650 read_vec_element(s, tcg_op2, passreg, 1, MO_64);
9651 tcg_res[pass] = tcg_temp_new_i64();
9654 case 0x17: /* ADDP */
9655 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
9657 case 0x58: /* FMAXNMP */
9658 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9660 case 0x5a: /* FADDP */
9661 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9663 case 0x5e: /* FMAXP */
9664 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9666 case 0x78: /* FMINNMP */
9667 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9669 case 0x7e: /* FMINP */
9670 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9673 g_assert_not_reached();
9676 tcg_temp_free_i64(tcg_op1);
9677 tcg_temp_free_i64(tcg_op2);
9680 for (pass = 0; pass < 2; pass++) {
9681 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9682 tcg_temp_free_i64(tcg_res[pass]);
9685 int maxpass = is_q ? 4 : 2;
9686 TCGv_i32 tcg_res[4];
9688 for (pass = 0; pass < maxpass; pass++) {
9689 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9690 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9691 NeonGenTwoOpFn *genfn = NULL;
9692 int passreg = pass < (maxpass / 2) ? rn : rm;
9693 int passelt = (is_q && (pass & 1)) ? 2 : 0;
9695 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
9696 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
9697 tcg_res[pass] = tcg_temp_new_i32();
9700 case 0x17: /* ADDP */
9702 static NeonGenTwoOpFn * const fns[3] = {
9703 gen_helper_neon_padd_u8,
9704 gen_helper_neon_padd_u16,
9710 case 0x14: /* SMAXP, UMAXP */
9712 static NeonGenTwoOpFn * const fns[3][2] = {
9713 { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
9714 { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
9715 { gen_max_s32, gen_max_u32 },
9717 genfn = fns[size][u];
9720 case 0x15: /* SMINP, UMINP */
9722 static NeonGenTwoOpFn * const fns[3][2] = {
9723 { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
9724 { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
9725 { gen_min_s32, gen_min_u32 },
9727 genfn = fns[size][u];
9730 /* The FP operations are all on single floats (32 bit) */
9731 case 0x58: /* FMAXNMP */
9732 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9734 case 0x5a: /* FADDP */
9735 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9737 case 0x5e: /* FMAXP */
9738 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9740 case 0x78: /* FMINNMP */
9741 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9743 case 0x7e: /* FMINP */
9744 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9747 g_assert_not_reached();
9750 /* FP ops called directly, otherwise call now */
9752 genfn(tcg_res[pass], tcg_op1, tcg_op2);
9755 tcg_temp_free_i32(tcg_op1);
9756 tcg_temp_free_i32(tcg_op2);
9759 for (pass = 0; pass < maxpass; pass++) {
9760 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
9761 tcg_temp_free_i32(tcg_res[pass]);
9763 clear_vec_high(s, is_q, rd);
9767 tcg_temp_free_ptr(fpst);
9771 /* Floating point op subgroup of C3.6.16. */
9772 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
9774 /* For floating point ops, the U, size[1] and opcode bits
9775 * together indicate the operation. size[0] indicates single
9778 int fpopcode = extract32(insn, 11, 5)
9779 | (extract32(insn, 23, 1) << 5)
9780 | (extract32(insn, 29, 1) << 6);
9781 int is_q = extract32(insn, 30, 1);
9782 int size = extract32(insn, 22, 1);
9783 int rm = extract32(insn, 16, 5);
9784 int rn = extract32(insn, 5, 5);
9785 int rd = extract32(insn, 0, 5);
9787 int datasize = is_q ? 128 : 64;
9788 int esize = 32 << size;
9789 int elements = datasize / esize;
9791 if (size == 1 && !is_q) {
9792 unallocated_encoding(s);
9797 case 0x58: /* FMAXNMP */
9798 case 0x5a: /* FADDP */
9799 case 0x5e: /* FMAXP */
9800 case 0x78: /* FMINNMP */
9801 case 0x7e: /* FMINP */
9802 if (size && !is_q) {
9803 unallocated_encoding(s);
9806 handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
9809 case 0x1b: /* FMULX */
9810 case 0x1f: /* FRECPS */
9811 case 0x3f: /* FRSQRTS */
9812 case 0x5d: /* FACGE */
9813 case 0x7d: /* FACGT */
9814 case 0x19: /* FMLA */
9815 case 0x39: /* FMLS */
9816 case 0x18: /* FMAXNM */
9817 case 0x1a: /* FADD */
9818 case 0x1c: /* FCMEQ */
9819 case 0x1e: /* FMAX */
9820 case 0x38: /* FMINNM */
9821 case 0x3a: /* FSUB */
9822 case 0x3e: /* FMIN */
9823 case 0x5b: /* FMUL */
9824 case 0x5c: /* FCMGE */
9825 case 0x5f: /* FDIV */
9826 case 0x7a: /* FABD */
9827 case 0x7c: /* FCMGT */
9828 if (!fp_access_check(s)) {
9832 handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
9835 unallocated_encoding(s);
9840 static void gen_mla8_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
9842 gen_helper_neon_mul_u8(a, a, b);
9843 gen_helper_neon_add_u8(d, d, a);
9846 static void gen_mla16_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
9848 gen_helper_neon_mul_u16(a, a, b);
9849 gen_helper_neon_add_u16(d, d, a);
9852 static void gen_mla32_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
9854 tcg_gen_mul_i32(a, a, b);
9855 tcg_gen_add_i32(d, d, a);
9858 static void gen_mla64_i64(TCGv_i64 d, TCGv_i64 a, TCGv_i64 b)
9860 tcg_gen_mul_i64(a, a, b);
9861 tcg_gen_add_i64(d, d, a);
9864 static void gen_mla_vec(unsigned vece, TCGv_vec d, TCGv_vec a, TCGv_vec b)
9866 tcg_gen_mul_vec(vece, a, a, b);
9867 tcg_gen_add_vec(vece, d, d, a);
9870 static void gen_mls8_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
9872 gen_helper_neon_mul_u8(a, a, b);
9873 gen_helper_neon_sub_u8(d, d, a);
9876 static void gen_mls16_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
9878 gen_helper_neon_mul_u16(a, a, b);
9879 gen_helper_neon_sub_u16(d, d, a);
9882 static void gen_mls32_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
9884 tcg_gen_mul_i32(a, a, b);
9885 tcg_gen_sub_i32(d, d, a);
9888 static void gen_mls64_i64(TCGv_i64 d, TCGv_i64 a, TCGv_i64 b)
9890 tcg_gen_mul_i64(a, a, b);
9891 tcg_gen_sub_i64(d, d, a);
9894 static void gen_mls_vec(unsigned vece, TCGv_vec d, TCGv_vec a, TCGv_vec b)
9896 tcg_gen_mul_vec(vece, a, a, b);
9897 tcg_gen_sub_vec(vece, d, d, a);
9900 /* Integer op subgroup of C3.6.16. */
9901 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
9903 static const GVecGen3 cmtst_op[4] = {
9904 { .fni4 = gen_helper_neon_tst_u8,
9905 .fniv = gen_cmtst_vec,
9907 { .fni4 = gen_helper_neon_tst_u16,
9908 .fniv = gen_cmtst_vec,
9910 { .fni4 = gen_cmtst_i32,
9911 .fniv = gen_cmtst_vec,
9913 { .fni8 = gen_cmtst_i64,
9914 .fniv = gen_cmtst_vec,
9915 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
9918 static const GVecGen3 mla_op[4] = {
9919 { .fni4 = gen_mla8_i32,
9920 .fniv = gen_mla_vec,
9921 .opc = INDEX_op_mul_vec,
9924 { .fni4 = gen_mla16_i32,
9925 .fniv = gen_mla_vec,
9926 .opc = INDEX_op_mul_vec,
9929 { .fni4 = gen_mla32_i32,
9930 .fniv = gen_mla_vec,
9931 .opc = INDEX_op_mul_vec,
9934 { .fni8 = gen_mla64_i64,
9935 .fniv = gen_mla_vec,
9936 .opc = INDEX_op_mul_vec,
9937 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
9941 static const GVecGen3 mls_op[4] = {
9942 { .fni4 = gen_mls8_i32,
9943 .fniv = gen_mls_vec,
9944 .opc = INDEX_op_mul_vec,
9947 { .fni4 = gen_mls16_i32,
9948 .fniv = gen_mls_vec,
9949 .opc = INDEX_op_mul_vec,
9952 { .fni4 = gen_mls32_i32,
9953 .fniv = gen_mls_vec,
9954 .opc = INDEX_op_mul_vec,
9957 { .fni8 = gen_mls64_i64,
9958 .fniv = gen_mls_vec,
9959 .opc = INDEX_op_mul_vec,
9960 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
9965 int is_q = extract32(insn, 30, 1);
9966 int u = extract32(insn, 29, 1);
9967 int size = extract32(insn, 22, 2);
9968 int opcode = extract32(insn, 11, 5);
9969 int rm = extract32(insn, 16, 5);
9970 int rn = extract32(insn, 5, 5);
9971 int rd = extract32(insn, 0, 5);
9976 case 0x13: /* MUL, PMUL */
9977 if (u && size != 0) {
9978 unallocated_encoding(s);
9982 case 0x0: /* SHADD, UHADD */
9983 case 0x2: /* SRHADD, URHADD */
9984 case 0x4: /* SHSUB, UHSUB */
9985 case 0xc: /* SMAX, UMAX */
9986 case 0xd: /* SMIN, UMIN */
9987 case 0xe: /* SABD, UABD */
9988 case 0xf: /* SABA, UABA */
9989 case 0x12: /* MLA, MLS */
9991 unallocated_encoding(s);
9995 case 0x16: /* SQDMULH, SQRDMULH */
9996 if (size == 0 || size == 3) {
9997 unallocated_encoding(s);
10002 if (size == 3 && !is_q) {
10003 unallocated_encoding(s);
10009 if (!fp_access_check(s)) {
10014 case 0x10: /* ADD, SUB */
10016 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
10018 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
10021 case 0x13: /* MUL, PMUL */
10022 if (!u) { /* MUL */
10023 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
10027 case 0x12: /* MLA, MLS */
10029 gen_gvec_op3(s, is_q, rd, rn, rm, &mls_op[size]);
10031 gen_gvec_op3(s, is_q, rd, rn, rm, &mla_op[size]);
10035 if (!u) { /* CMTST */
10036 gen_gvec_op3(s, is_q, rd, rn, rm, &cmtst_op[size]);
10040 cond = TCG_COND_EQ;
10042 case 0x06: /* CMGT, CMHI */
10043 cond = u ? TCG_COND_GTU : TCG_COND_GT;
10045 case 0x07: /* CMGE, CMHS */
10046 cond = u ? TCG_COND_GEU : TCG_COND_GE;
10048 tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
10049 vec_full_reg_offset(s, rn),
10050 vec_full_reg_offset(s, rm),
10051 is_q ? 16 : 8, vec_full_reg_size(s));
10057 for (pass = 0; pass < 2; pass++) {
10058 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10059 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10060 TCGv_i64 tcg_res = tcg_temp_new_i64();
10062 read_vec_element(s, tcg_op1, rn, pass, MO_64);
10063 read_vec_element(s, tcg_op2, rm, pass, MO_64);
10065 handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
10067 write_vec_element(s, tcg_res, rd, pass, MO_64);
10069 tcg_temp_free_i64(tcg_res);
10070 tcg_temp_free_i64(tcg_op1);
10071 tcg_temp_free_i64(tcg_op2);
10074 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
10075 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10076 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10077 TCGv_i32 tcg_res = tcg_temp_new_i32();
10078 NeonGenTwoOpFn *genfn = NULL;
10079 NeonGenTwoOpEnvFn *genenvfn = NULL;
10081 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
10082 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
10085 case 0x0: /* SHADD, UHADD */
10087 static NeonGenTwoOpFn * const fns[3][2] = {
10088 { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
10089 { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
10090 { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
10092 genfn = fns[size][u];
10095 case 0x1: /* SQADD, UQADD */
10097 static NeonGenTwoOpEnvFn * const fns[3][2] = {
10098 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
10099 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
10100 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
10102 genenvfn = fns[size][u];
10105 case 0x2: /* SRHADD, URHADD */
10107 static NeonGenTwoOpFn * const fns[3][2] = {
10108 { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
10109 { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
10110 { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
10112 genfn = fns[size][u];
10115 case 0x4: /* SHSUB, UHSUB */
10117 static NeonGenTwoOpFn * const fns[3][2] = {
10118 { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
10119 { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
10120 { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
10122 genfn = fns[size][u];
10125 case 0x5: /* SQSUB, UQSUB */
10127 static NeonGenTwoOpEnvFn * const fns[3][2] = {
10128 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
10129 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
10130 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
10132 genenvfn = fns[size][u];
10135 case 0x8: /* SSHL, USHL */
10137 static NeonGenTwoOpFn * const fns[3][2] = {
10138 { gen_helper_neon_shl_s8, gen_helper_neon_shl_u8 },
10139 { gen_helper_neon_shl_s16, gen_helper_neon_shl_u16 },
10140 { gen_helper_neon_shl_s32, gen_helper_neon_shl_u32 },
10142 genfn = fns[size][u];
10145 case 0x9: /* SQSHL, UQSHL */
10147 static NeonGenTwoOpEnvFn * const fns[3][2] = {
10148 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
10149 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
10150 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
10152 genenvfn = fns[size][u];
10155 case 0xa: /* SRSHL, URSHL */
10157 static NeonGenTwoOpFn * const fns[3][2] = {
10158 { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
10159 { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
10160 { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
10162 genfn = fns[size][u];
10165 case 0xb: /* SQRSHL, UQRSHL */
10167 static NeonGenTwoOpEnvFn * const fns[3][2] = {
10168 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
10169 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
10170 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
10172 genenvfn = fns[size][u];
10175 case 0xc: /* SMAX, UMAX */
10177 static NeonGenTwoOpFn * const fns[3][2] = {
10178 { gen_helper_neon_max_s8, gen_helper_neon_max_u8 },
10179 { gen_helper_neon_max_s16, gen_helper_neon_max_u16 },
10180 { gen_max_s32, gen_max_u32 },
10182 genfn = fns[size][u];
10186 case 0xd: /* SMIN, UMIN */
10188 static NeonGenTwoOpFn * const fns[3][2] = {
10189 { gen_helper_neon_min_s8, gen_helper_neon_min_u8 },
10190 { gen_helper_neon_min_s16, gen_helper_neon_min_u16 },
10191 { gen_min_s32, gen_min_u32 },
10193 genfn = fns[size][u];
10196 case 0xe: /* SABD, UABD */
10197 case 0xf: /* SABA, UABA */
10199 static NeonGenTwoOpFn * const fns[3][2] = {
10200 { gen_helper_neon_abd_s8, gen_helper_neon_abd_u8 },
10201 { gen_helper_neon_abd_s16, gen_helper_neon_abd_u16 },
10202 { gen_helper_neon_abd_s32, gen_helper_neon_abd_u32 },
10204 genfn = fns[size][u];
10207 case 0x13: /* MUL, PMUL */
10208 assert(u); /* PMUL */
10210 genfn = gen_helper_neon_mul_p8;
10212 case 0x16: /* SQDMULH, SQRDMULH */
10214 static NeonGenTwoOpEnvFn * const fns[2][2] = {
10215 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
10216 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
10218 assert(size == 1 || size == 2);
10219 genenvfn = fns[size - 1][u];
10223 g_assert_not_reached();
10227 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
10229 genfn(tcg_res, tcg_op1, tcg_op2);
10232 if (opcode == 0xf) {
10233 /* SABA, UABA: accumulating ops */
10234 static NeonGenTwoOpFn * const fns[3] = {
10235 gen_helper_neon_add_u8,
10236 gen_helper_neon_add_u16,
10240 read_vec_element_i32(s, tcg_op1, rd, pass, MO_32);
10241 fns[size](tcg_res, tcg_op1, tcg_res);
10244 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10246 tcg_temp_free_i32(tcg_res);
10247 tcg_temp_free_i32(tcg_op1);
10248 tcg_temp_free_i32(tcg_op2);
10251 clear_vec_high(s, is_q, rd);
10254 /* AdvSIMD three same
10255 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
10256 * +---+---+---+-----------+------+---+------+--------+---+------+------+
10257 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
10258 * +---+---+---+-----------+------+---+------+--------+---+------+------+
10260 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
10262 int opcode = extract32(insn, 11, 5);
10265 case 0x3: /* logic ops */
10266 disas_simd_3same_logic(s, insn);
10268 case 0x17: /* ADDP */
10269 case 0x14: /* SMAXP, UMAXP */
10270 case 0x15: /* SMINP, UMINP */
10272 /* Pairwise operations */
10273 int is_q = extract32(insn, 30, 1);
10274 int u = extract32(insn, 29, 1);
10275 int size = extract32(insn, 22, 2);
10276 int rm = extract32(insn, 16, 5);
10277 int rn = extract32(insn, 5, 5);
10278 int rd = extract32(insn, 0, 5);
10279 if (opcode == 0x17) {
10280 if (u || (size == 3 && !is_q)) {
10281 unallocated_encoding(s);
10286 unallocated_encoding(s);
10290 handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
10293 case 0x18 ... 0x31:
10294 /* floating point ops, sz[1] and U are part of opcode */
10295 disas_simd_3same_float(s, insn);
10298 disas_simd_3same_int(s, insn);
10304 * Advanced SIMD three same (ARMv8.2 FP16 variants)
10306 * 31 30 29 28 24 23 22 21 20 16 15 14 13 11 10 9 5 4 0
10307 * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
10308 * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 | Rm | 0 0 | opcode | 1 | Rn | Rd |
10309 * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
10311 * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
10312 * (register), FACGE, FABD, FCMGT (register) and FACGT.
10315 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
10317 int opcode, fpopcode;
10318 int is_q, u, a, rm, rn, rd;
10319 int datasize, elements;
10322 bool pairwise = false;
10324 if (!arm_dc_feature(s, ARM_FEATURE_V8_FP16)) {
10325 unallocated_encoding(s);
10329 if (!fp_access_check(s)) {
10333 /* For these floating point ops, the U, a and opcode bits
10334 * together indicate the operation.
10336 opcode = extract32(insn, 11, 3);
10337 u = extract32(insn, 29, 1);
10338 a = extract32(insn, 23, 1);
10339 is_q = extract32(insn, 30, 1);
10340 rm = extract32(insn, 16, 5);
10341 rn = extract32(insn, 5, 5);
10342 rd = extract32(insn, 0, 5);
10344 fpopcode = opcode | (a << 3) | (u << 4);
10345 datasize = is_q ? 128 : 64;
10346 elements = datasize / 16;
10348 switch (fpopcode) {
10349 case 0x10: /* FMAXNMP */
10350 case 0x12: /* FADDP */
10351 case 0x16: /* FMAXP */
10352 case 0x18: /* FMINNMP */
10353 case 0x1e: /* FMINP */
10358 fpst = get_fpstatus_ptr(true);
10361 int maxpass = is_q ? 8 : 4;
10362 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10363 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10364 TCGv_i32 tcg_res[8];
10366 for (pass = 0; pass < maxpass; pass++) {
10367 int passreg = pass < (maxpass / 2) ? rn : rm;
10368 int passelt = (pass << 1) & (maxpass - 1);
10370 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
10371 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
10372 tcg_res[pass] = tcg_temp_new_i32();
10374 switch (fpopcode) {
10375 case 0x10: /* FMAXNMP */
10376 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
10379 case 0x12: /* FADDP */
10380 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10382 case 0x16: /* FMAXP */
10383 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10385 case 0x18: /* FMINNMP */
10386 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
10389 case 0x1e: /* FMINP */
10390 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10393 g_assert_not_reached();
10397 for (pass = 0; pass < maxpass; pass++) {
10398 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
10399 tcg_temp_free_i32(tcg_res[pass]);
10402 tcg_temp_free_i32(tcg_op1);
10403 tcg_temp_free_i32(tcg_op2);
10406 for (pass = 0; pass < elements; pass++) {
10407 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10408 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10409 TCGv_i32 tcg_res = tcg_temp_new_i32();
10411 read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
10412 read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
10414 switch (fpopcode) {
10415 case 0x0: /* FMAXNM */
10416 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
10418 case 0x1: /* FMLA */
10419 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
10420 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
10423 case 0x2: /* FADD */
10424 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
10426 case 0x3: /* FMULX */
10427 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
10429 case 0x4: /* FCMEQ */
10430 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
10432 case 0x6: /* FMAX */
10433 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
10435 case 0x7: /* FRECPS */
10436 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
10438 case 0x8: /* FMINNM */
10439 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
10441 case 0x9: /* FMLS */
10442 /* As usual for ARM, separate negation for fused multiply-add */
10443 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
10444 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
10445 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
10448 case 0xa: /* FSUB */
10449 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
10451 case 0xe: /* FMIN */
10452 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
10454 case 0xf: /* FRSQRTS */
10455 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
10457 case 0x13: /* FMUL */
10458 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
10460 case 0x14: /* FCMGE */
10461 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
10463 case 0x15: /* FACGE */
10464 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
10466 case 0x17: /* FDIV */
10467 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
10469 case 0x1a: /* FABD */
10470 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
10471 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
10473 case 0x1c: /* FCMGT */
10474 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
10476 case 0x1d: /* FACGT */
10477 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
10480 fprintf(stderr, "%s: insn %#04x, fpop %#2x @ %#" PRIx64 "\n",
10481 __func__, insn, fpopcode, s->pc);
10482 g_assert_not_reached();
10485 write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
10486 tcg_temp_free_i32(tcg_res);
10487 tcg_temp_free_i32(tcg_op1);
10488 tcg_temp_free_i32(tcg_op2);
10492 tcg_temp_free_ptr(fpst);
10494 clear_vec_high(s, is_q, rd);
10497 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
10498 int size, int rn, int rd)
10500 /* Handle 2-reg-misc ops which are widening (so each size element
10501 * in the source becomes a 2*size element in the destination.
10502 * The only instruction like this is FCVTL.
10507 /* 32 -> 64 bit fp conversion */
10508 TCGv_i64 tcg_res[2];
10509 int srcelt = is_q ? 2 : 0;
10511 for (pass = 0; pass < 2; pass++) {
10512 TCGv_i32 tcg_op = tcg_temp_new_i32();
10513 tcg_res[pass] = tcg_temp_new_i64();
10515 read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
10516 gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
10517 tcg_temp_free_i32(tcg_op);
10519 for (pass = 0; pass < 2; pass++) {
10520 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10521 tcg_temp_free_i64(tcg_res[pass]);
10524 /* 16 -> 32 bit fp conversion */
10525 int srcelt = is_q ? 4 : 0;
10526 TCGv_i32 tcg_res[4];
10528 for (pass = 0; pass < 4; pass++) {
10529 tcg_res[pass] = tcg_temp_new_i32();
10531 read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
10532 gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
10535 for (pass = 0; pass < 4; pass++) {
10536 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
10537 tcg_temp_free_i32(tcg_res[pass]);
10542 static void handle_rev(DisasContext *s, int opcode, bool u,
10543 bool is_q, int size, int rn, int rd)
10545 int op = (opcode << 1) | u;
10546 int opsz = op + size;
10547 int grp_size = 3 - opsz;
10548 int dsize = is_q ? 128 : 64;
10552 unallocated_encoding(s);
10556 if (!fp_access_check(s)) {
10561 /* Special case bytes, use bswap op on each group of elements */
10562 int groups = dsize / (8 << grp_size);
10564 for (i = 0; i < groups; i++) {
10565 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
10567 read_vec_element(s, tcg_tmp, rn, i, grp_size);
10568 switch (grp_size) {
10570 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
10573 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
10576 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
10579 g_assert_not_reached();
10581 write_vec_element(s, tcg_tmp, rd, i, grp_size);
10582 tcg_temp_free_i64(tcg_tmp);
10584 clear_vec_high(s, is_q, rd);
10586 int revmask = (1 << grp_size) - 1;
10587 int esize = 8 << size;
10588 int elements = dsize / esize;
10589 TCGv_i64 tcg_rn = tcg_temp_new_i64();
10590 TCGv_i64 tcg_rd = tcg_const_i64(0);
10591 TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
10593 for (i = 0; i < elements; i++) {
10594 int e_rev = (i & 0xf) ^ revmask;
10595 int off = e_rev * esize;
10596 read_vec_element(s, tcg_rn, rn, i, size);
10598 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
10599 tcg_rn, off - 64, esize);
10601 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
10604 write_vec_element(s, tcg_rd, rd, 0, MO_64);
10605 write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
10607 tcg_temp_free_i64(tcg_rd_hi);
10608 tcg_temp_free_i64(tcg_rd);
10609 tcg_temp_free_i64(tcg_rn);
10613 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
10614 bool is_q, int size, int rn, int rd)
10616 /* Implement the pairwise operations from 2-misc:
10617 * SADDLP, UADDLP, SADALP, UADALP.
10618 * These all add pairs of elements in the input to produce a
10619 * double-width result element in the output (possibly accumulating).
10621 bool accum = (opcode == 0x6);
10622 int maxpass = is_q ? 2 : 1;
10624 TCGv_i64 tcg_res[2];
10627 /* 32 + 32 -> 64 op */
10628 TCGMemOp memop = size + (u ? 0 : MO_SIGN);
10630 for (pass = 0; pass < maxpass; pass++) {
10631 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10632 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10634 tcg_res[pass] = tcg_temp_new_i64();
10636 read_vec_element(s, tcg_op1, rn, pass * 2, memop);
10637 read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
10638 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
10640 read_vec_element(s, tcg_op1, rd, pass, MO_64);
10641 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
10644 tcg_temp_free_i64(tcg_op1);
10645 tcg_temp_free_i64(tcg_op2);
10648 for (pass = 0; pass < maxpass; pass++) {
10649 TCGv_i64 tcg_op = tcg_temp_new_i64();
10650 NeonGenOneOpFn *genfn;
10651 static NeonGenOneOpFn * const fns[2][2] = {
10652 { gen_helper_neon_addlp_s8, gen_helper_neon_addlp_u8 },
10653 { gen_helper_neon_addlp_s16, gen_helper_neon_addlp_u16 },
10656 genfn = fns[size][u];
10658 tcg_res[pass] = tcg_temp_new_i64();
10660 read_vec_element(s, tcg_op, rn, pass, MO_64);
10661 genfn(tcg_res[pass], tcg_op);
10664 read_vec_element(s, tcg_op, rd, pass, MO_64);
10666 gen_helper_neon_addl_u16(tcg_res[pass],
10667 tcg_res[pass], tcg_op);
10669 gen_helper_neon_addl_u32(tcg_res[pass],
10670 tcg_res[pass], tcg_op);
10673 tcg_temp_free_i64(tcg_op);
10677 tcg_res[1] = tcg_const_i64(0);
10679 for (pass = 0; pass < 2; pass++) {
10680 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10681 tcg_temp_free_i64(tcg_res[pass]);
10685 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
10687 /* Implement SHLL and SHLL2 */
10689 int part = is_q ? 2 : 0;
10690 TCGv_i64 tcg_res[2];
10692 for (pass = 0; pass < 2; pass++) {
10693 static NeonGenWidenFn * const widenfns[3] = {
10694 gen_helper_neon_widen_u8,
10695 gen_helper_neon_widen_u16,
10696 tcg_gen_extu_i32_i64,
10698 NeonGenWidenFn *widenfn = widenfns[size];
10699 TCGv_i32 tcg_op = tcg_temp_new_i32();
10701 read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
10702 tcg_res[pass] = tcg_temp_new_i64();
10703 widenfn(tcg_res[pass], tcg_op);
10704 tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
10706 tcg_temp_free_i32(tcg_op);
10709 for (pass = 0; pass < 2; pass++) {
10710 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10711 tcg_temp_free_i64(tcg_res[pass]);
10715 /* AdvSIMD two reg misc
10716 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
10717 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
10718 * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
10719 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
10721 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
10723 int size = extract32(insn, 22, 2);
10724 int opcode = extract32(insn, 12, 5);
10725 bool u = extract32(insn, 29, 1);
10726 bool is_q = extract32(insn, 30, 1);
10727 int rn = extract32(insn, 5, 5);
10728 int rd = extract32(insn, 0, 5);
10729 bool need_fpstatus = false;
10730 bool need_rmode = false;
10732 TCGv_i32 tcg_rmode;
10733 TCGv_ptr tcg_fpstatus;
10736 case 0x0: /* REV64, REV32 */
10737 case 0x1: /* REV16 */
10738 handle_rev(s, opcode, u, is_q, size, rn, rd);
10740 case 0x5: /* CNT, NOT, RBIT */
10741 if (u && size == 0) {
10744 } else if (u && size == 1) {
10747 } else if (!u && size == 0) {
10751 unallocated_encoding(s);
10753 case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
10754 case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
10756 unallocated_encoding(s);
10759 if (!fp_access_check(s)) {
10763 handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
10765 case 0x4: /* CLS, CLZ */
10767 unallocated_encoding(s);
10771 case 0x2: /* SADDLP, UADDLP */
10772 case 0x6: /* SADALP, UADALP */
10774 unallocated_encoding(s);
10777 if (!fp_access_check(s)) {
10780 handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
10782 case 0x13: /* SHLL, SHLL2 */
10783 if (u == 0 || size == 3) {
10784 unallocated_encoding(s);
10787 if (!fp_access_check(s)) {
10790 handle_shll(s, is_q, size, rn, rd);
10792 case 0xa: /* CMLT */
10794 unallocated_encoding(s);
10798 case 0x8: /* CMGT, CMGE */
10799 case 0x9: /* CMEQ, CMLE */
10800 case 0xb: /* ABS, NEG */
10801 if (size == 3 && !is_q) {
10802 unallocated_encoding(s);
10806 case 0x3: /* SUQADD, USQADD */
10807 if (size == 3 && !is_q) {
10808 unallocated_encoding(s);
10811 if (!fp_access_check(s)) {
10814 handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
10816 case 0x7: /* SQABS, SQNEG */
10817 if (size == 3 && !is_q) {
10818 unallocated_encoding(s);
10823 case 0x16 ... 0x1d:
10826 /* Floating point: U, size[1] and opcode indicate operation;
10827 * size[0] indicates single or double precision.
10829 int is_double = extract32(size, 0, 1);
10830 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10831 size = is_double ? 3 : 2;
10833 case 0x2f: /* FABS */
10834 case 0x6f: /* FNEG */
10835 if (size == 3 && !is_q) {
10836 unallocated_encoding(s);
10840 case 0x1d: /* SCVTF */
10841 case 0x5d: /* UCVTF */
10843 bool is_signed = (opcode == 0x1d) ? true : false;
10844 int elements = is_double ? 2 : is_q ? 4 : 2;
10845 if (is_double && !is_q) {
10846 unallocated_encoding(s);
10849 if (!fp_access_check(s)) {
10852 handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
10855 case 0x2c: /* FCMGT (zero) */
10856 case 0x2d: /* FCMEQ (zero) */
10857 case 0x2e: /* FCMLT (zero) */
10858 case 0x6c: /* FCMGE (zero) */
10859 case 0x6d: /* FCMLE (zero) */
10860 if (size == 3 && !is_q) {
10861 unallocated_encoding(s);
10864 handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
10866 case 0x7f: /* FSQRT */
10867 if (size == 3 && !is_q) {
10868 unallocated_encoding(s);
10872 case 0x1a: /* FCVTNS */
10873 case 0x1b: /* FCVTMS */
10874 case 0x3a: /* FCVTPS */
10875 case 0x3b: /* FCVTZS */
10876 case 0x5a: /* FCVTNU */
10877 case 0x5b: /* FCVTMU */
10878 case 0x7a: /* FCVTPU */
10879 case 0x7b: /* FCVTZU */
10880 need_fpstatus = true;
10882 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10883 if (size == 3 && !is_q) {
10884 unallocated_encoding(s);
10888 case 0x5c: /* FCVTAU */
10889 case 0x1c: /* FCVTAS */
10890 need_fpstatus = true;
10892 rmode = FPROUNDING_TIEAWAY;
10893 if (size == 3 && !is_q) {
10894 unallocated_encoding(s);
10898 case 0x3c: /* URECPE */
10900 unallocated_encoding(s);
10904 case 0x3d: /* FRECPE */
10905 case 0x7d: /* FRSQRTE */
10906 if (size == 3 && !is_q) {
10907 unallocated_encoding(s);
10910 if (!fp_access_check(s)) {
10913 handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
10915 case 0x56: /* FCVTXN, FCVTXN2 */
10917 unallocated_encoding(s);
10921 case 0x16: /* FCVTN, FCVTN2 */
10922 /* handle_2misc_narrow does a 2*size -> size operation, but these
10923 * instructions encode the source size rather than dest size.
10925 if (!fp_access_check(s)) {
10928 handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
10930 case 0x17: /* FCVTL, FCVTL2 */
10931 if (!fp_access_check(s)) {
10934 handle_2misc_widening(s, opcode, is_q, size, rn, rd);
10936 case 0x18: /* FRINTN */
10937 case 0x19: /* FRINTM */
10938 case 0x38: /* FRINTP */
10939 case 0x39: /* FRINTZ */
10941 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10943 case 0x59: /* FRINTX */
10944 case 0x79: /* FRINTI */
10945 need_fpstatus = true;
10946 if (size == 3 && !is_q) {
10947 unallocated_encoding(s);
10951 case 0x58: /* FRINTA */
10953 rmode = FPROUNDING_TIEAWAY;
10954 need_fpstatus = true;
10955 if (size == 3 && !is_q) {
10956 unallocated_encoding(s);
10960 case 0x7c: /* URSQRTE */
10962 unallocated_encoding(s);
10965 need_fpstatus = true;
10968 unallocated_encoding(s);
10974 unallocated_encoding(s);
10978 if (!fp_access_check(s)) {
10982 if (need_fpstatus || need_rmode) {
10983 tcg_fpstatus = get_fpstatus_ptr(false);
10985 tcg_fpstatus = NULL;
10988 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
10989 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
10996 if (u && size == 0) { /* NOT */
10997 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
11003 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
11010 /* All 64-bit element operations can be shared with scalar 2misc */
11013 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
11014 TCGv_i64 tcg_op = tcg_temp_new_i64();
11015 TCGv_i64 tcg_res = tcg_temp_new_i64();
11017 read_vec_element(s, tcg_op, rn, pass, MO_64);
11019 handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
11020 tcg_rmode, tcg_fpstatus);
11022 write_vec_element(s, tcg_res, rd, pass, MO_64);
11024 tcg_temp_free_i64(tcg_res);
11025 tcg_temp_free_i64(tcg_op);
11030 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11031 TCGv_i32 tcg_op = tcg_temp_new_i32();
11032 TCGv_i32 tcg_res = tcg_temp_new_i32();
11035 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
11038 /* Special cases for 32 bit elements */
11040 case 0xa: /* CMLT */
11041 /* 32 bit integer comparison against zero, result is
11042 * test ? (2^32 - 1) : 0. We implement via setcond(test)
11045 cond = TCG_COND_LT;
11047 tcg_gen_setcondi_i32(cond, tcg_res, tcg_op, 0);
11048 tcg_gen_neg_i32(tcg_res, tcg_res);
11050 case 0x8: /* CMGT, CMGE */
11051 cond = u ? TCG_COND_GE : TCG_COND_GT;
11053 case 0x9: /* CMEQ, CMLE */
11054 cond = u ? TCG_COND_LE : TCG_COND_EQ;
11056 case 0x4: /* CLS */
11058 tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
11060 tcg_gen_clrsb_i32(tcg_res, tcg_op);
11063 case 0x7: /* SQABS, SQNEG */
11065 gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
11067 gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
11070 case 0xb: /* ABS, NEG */
11072 tcg_gen_neg_i32(tcg_res, tcg_op);
11074 TCGv_i32 tcg_zero = tcg_const_i32(0);
11075 tcg_gen_neg_i32(tcg_res, tcg_op);
11076 tcg_gen_movcond_i32(TCG_COND_GT, tcg_res, tcg_op,
11077 tcg_zero, tcg_op, tcg_res);
11078 tcg_temp_free_i32(tcg_zero);
11081 case 0x2f: /* FABS */
11082 gen_helper_vfp_abss(tcg_res, tcg_op);
11084 case 0x6f: /* FNEG */
11085 gen_helper_vfp_negs(tcg_res, tcg_op);
11087 case 0x7f: /* FSQRT */
11088 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
11090 case 0x1a: /* FCVTNS */
11091 case 0x1b: /* FCVTMS */
11092 case 0x1c: /* FCVTAS */
11093 case 0x3a: /* FCVTPS */
11094 case 0x3b: /* FCVTZS */
11096 TCGv_i32 tcg_shift = tcg_const_i32(0);
11097 gen_helper_vfp_tosls(tcg_res, tcg_op,
11098 tcg_shift, tcg_fpstatus);
11099 tcg_temp_free_i32(tcg_shift);
11102 case 0x5a: /* FCVTNU */
11103 case 0x5b: /* FCVTMU */
11104 case 0x5c: /* FCVTAU */
11105 case 0x7a: /* FCVTPU */
11106 case 0x7b: /* FCVTZU */
11108 TCGv_i32 tcg_shift = tcg_const_i32(0);
11109 gen_helper_vfp_touls(tcg_res, tcg_op,
11110 tcg_shift, tcg_fpstatus);
11111 tcg_temp_free_i32(tcg_shift);
11114 case 0x18: /* FRINTN */
11115 case 0x19: /* FRINTM */
11116 case 0x38: /* FRINTP */
11117 case 0x39: /* FRINTZ */
11118 case 0x58: /* FRINTA */
11119 case 0x79: /* FRINTI */
11120 gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
11122 case 0x59: /* FRINTX */
11123 gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
11125 case 0x7c: /* URSQRTE */
11126 gen_helper_rsqrte_u32(tcg_res, tcg_op, tcg_fpstatus);
11129 g_assert_not_reached();
11132 /* Use helpers for 8 and 16 bit elements */
11134 case 0x5: /* CNT, RBIT */
11135 /* For these two insns size is part of the opcode specifier
11136 * (handled earlier); they always operate on byte elements.
11139 gen_helper_neon_rbit_u8(tcg_res, tcg_op);
11141 gen_helper_neon_cnt_u8(tcg_res, tcg_op);
11144 case 0x7: /* SQABS, SQNEG */
11146 NeonGenOneOpEnvFn *genfn;
11147 static NeonGenOneOpEnvFn * const fns[2][2] = {
11148 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
11149 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
11151 genfn = fns[size][u];
11152 genfn(tcg_res, cpu_env, tcg_op);
11155 case 0x8: /* CMGT, CMGE */
11156 case 0x9: /* CMEQ, CMLE */
11157 case 0xa: /* CMLT */
11159 static NeonGenTwoOpFn * const fns[3][2] = {
11160 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_s16 },
11161 { gen_helper_neon_cge_s8, gen_helper_neon_cge_s16 },
11162 { gen_helper_neon_ceq_u8, gen_helper_neon_ceq_u16 },
11164 NeonGenTwoOpFn *genfn;
11167 TCGv_i32 tcg_zero = tcg_const_i32(0);
11169 /* comp = index into [CMGT, CMGE, CMEQ, CMLE, CMLT] */
11170 comp = (opcode - 0x8) * 2 + u;
11171 /* ...but LE, LT are implemented as reverse GE, GT */
11172 reverse = (comp > 2);
11176 genfn = fns[comp][size];
11178 genfn(tcg_res, tcg_zero, tcg_op);
11180 genfn(tcg_res, tcg_op, tcg_zero);
11182 tcg_temp_free_i32(tcg_zero);
11185 case 0xb: /* ABS, NEG */
11187 TCGv_i32 tcg_zero = tcg_const_i32(0);
11189 gen_helper_neon_sub_u16(tcg_res, tcg_zero, tcg_op);
11191 gen_helper_neon_sub_u8(tcg_res, tcg_zero, tcg_op);
11193 tcg_temp_free_i32(tcg_zero);
11196 gen_helper_neon_abs_s16(tcg_res, tcg_op);
11198 gen_helper_neon_abs_s8(tcg_res, tcg_op);
11202 case 0x4: /* CLS, CLZ */
11205 gen_helper_neon_clz_u8(tcg_res, tcg_op);
11207 gen_helper_neon_clz_u16(tcg_res, tcg_op);
11211 gen_helper_neon_cls_s8(tcg_res, tcg_op);
11213 gen_helper_neon_cls_s16(tcg_res, tcg_op);
11218 g_assert_not_reached();
11222 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11224 tcg_temp_free_i32(tcg_res);
11225 tcg_temp_free_i32(tcg_op);
11228 clear_vec_high(s, is_q, rd);
11231 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
11232 tcg_temp_free_i32(tcg_rmode);
11234 if (need_fpstatus) {
11235 tcg_temp_free_ptr(tcg_fpstatus);
11239 /* AdvSIMD [scalar] two register miscellaneous (FP16)
11241 * 31 30 29 28 27 24 23 22 21 17 16 12 11 10 9 5 4 0
11242 * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
11243 * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 | Rn | Rd |
11244 * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
11245 * mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
11246 * val: 0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
11248 * This actually covers two groups where scalar access is governed by
11249 * bit 28. A bunch of the instructions (float to integral) only exist
11250 * in the vector form and are un-allocated for the scalar decode. Also
11251 * in the scalar decode Q is always 1.
11253 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
11255 int fpop, opcode, a, u;
11259 bool only_in_vector = false;
11262 TCGv_i32 tcg_rmode = NULL;
11263 TCGv_ptr tcg_fpstatus = NULL;
11264 bool need_rmode = false;
11265 bool need_fpst = true;
11268 if (!arm_dc_feature(s, ARM_FEATURE_V8_FP16)) {
11269 unallocated_encoding(s);
11273 rd = extract32(insn, 0, 5);
11274 rn = extract32(insn, 5, 5);
11276 a = extract32(insn, 23, 1);
11277 u = extract32(insn, 29, 1);
11278 is_scalar = extract32(insn, 28, 1);
11279 is_q = extract32(insn, 30, 1);
11281 opcode = extract32(insn, 12, 5);
11282 fpop = deposit32(opcode, 5, 1, a);
11283 fpop = deposit32(fpop, 6, 1, u);
11285 rd = extract32(insn, 0, 5);
11286 rn = extract32(insn, 5, 5);
11289 case 0x1d: /* SCVTF */
11290 case 0x5d: /* UCVTF */
11297 elements = (is_q ? 8 : 4);
11300 if (!fp_access_check(s)) {
11303 handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
11307 case 0x2c: /* FCMGT (zero) */
11308 case 0x2d: /* FCMEQ (zero) */
11309 case 0x2e: /* FCMLT (zero) */
11310 case 0x6c: /* FCMGE (zero) */
11311 case 0x6d: /* FCMLE (zero) */
11312 handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
11314 case 0x3d: /* FRECPE */
11315 case 0x3f: /* FRECPX */
11317 case 0x18: /* FRINTN */
11319 only_in_vector = true;
11320 rmode = FPROUNDING_TIEEVEN;
11322 case 0x19: /* FRINTM */
11324 only_in_vector = true;
11325 rmode = FPROUNDING_NEGINF;
11327 case 0x38: /* FRINTP */
11329 only_in_vector = true;
11330 rmode = FPROUNDING_POSINF;
11332 case 0x39: /* FRINTZ */
11334 only_in_vector = true;
11335 rmode = FPROUNDING_ZERO;
11337 case 0x58: /* FRINTA */
11339 only_in_vector = true;
11340 rmode = FPROUNDING_TIEAWAY;
11342 case 0x59: /* FRINTX */
11343 case 0x79: /* FRINTI */
11344 only_in_vector = true;
11345 /* current rounding mode */
11347 case 0x1a: /* FCVTNS */
11349 rmode = FPROUNDING_TIEEVEN;
11351 case 0x1b: /* FCVTMS */
11353 rmode = FPROUNDING_NEGINF;
11355 case 0x1c: /* FCVTAS */
11357 rmode = FPROUNDING_TIEAWAY;
11359 case 0x3a: /* FCVTPS */
11361 rmode = FPROUNDING_POSINF;
11363 case 0x3b: /* FCVTZS */
11365 rmode = FPROUNDING_ZERO;
11367 case 0x5a: /* FCVTNU */
11369 rmode = FPROUNDING_TIEEVEN;
11371 case 0x5b: /* FCVTMU */
11373 rmode = FPROUNDING_NEGINF;
11375 case 0x5c: /* FCVTAU */
11377 rmode = FPROUNDING_TIEAWAY;
11379 case 0x7a: /* FCVTPU */
11381 rmode = FPROUNDING_POSINF;
11383 case 0x7b: /* FCVTZU */
11385 rmode = FPROUNDING_ZERO;
11387 case 0x2f: /* FABS */
11388 case 0x6f: /* FNEG */
11391 case 0x7d: /* FRSQRTE */
11392 case 0x7f: /* FSQRT (vector) */
11395 fprintf(stderr, "%s: insn %#04x fpop %#2x\n", __func__, insn, fpop);
11396 g_assert_not_reached();
11400 /* Check additional constraints for the scalar encoding */
11403 unallocated_encoding(s);
11406 /* FRINTxx is only in the vector form */
11407 if (only_in_vector) {
11408 unallocated_encoding(s);
11413 if (!fp_access_check(s)) {
11417 if (need_rmode || need_fpst) {
11418 tcg_fpstatus = get_fpstatus_ptr(true);
11422 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
11423 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
11427 TCGv_i32 tcg_op = tcg_temp_new_i32();
11428 TCGv_i32 tcg_res = tcg_temp_new_i32();
11430 read_vec_element_i32(s, tcg_op, rn, 0, MO_16);
11433 case 0x1a: /* FCVTNS */
11434 case 0x1b: /* FCVTMS */
11435 case 0x1c: /* FCVTAS */
11436 case 0x3a: /* FCVTPS */
11437 case 0x3b: /* FCVTZS */
11438 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
11440 case 0x3d: /* FRECPE */
11441 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
11443 case 0x3f: /* FRECPX */
11444 gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
11446 case 0x5a: /* FCVTNU */
11447 case 0x5b: /* FCVTMU */
11448 case 0x5c: /* FCVTAU */
11449 case 0x7a: /* FCVTPU */
11450 case 0x7b: /* FCVTZU */
11451 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
11453 case 0x6f: /* FNEG */
11454 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
11456 case 0x7d: /* FRSQRTE */
11457 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
11460 g_assert_not_reached();
11463 /* limit any sign extension going on */
11464 tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
11465 write_fp_sreg(s, rd, tcg_res);
11467 tcg_temp_free_i32(tcg_res);
11468 tcg_temp_free_i32(tcg_op);
11470 for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
11471 TCGv_i32 tcg_op = tcg_temp_new_i32();
11472 TCGv_i32 tcg_res = tcg_temp_new_i32();
11474 read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
11477 case 0x1a: /* FCVTNS */
11478 case 0x1b: /* FCVTMS */
11479 case 0x1c: /* FCVTAS */
11480 case 0x3a: /* FCVTPS */
11481 case 0x3b: /* FCVTZS */
11482 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
11484 case 0x3d: /* FRECPE */
11485 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
11487 case 0x5a: /* FCVTNU */
11488 case 0x5b: /* FCVTMU */
11489 case 0x5c: /* FCVTAU */
11490 case 0x7a: /* FCVTPU */
11491 case 0x7b: /* FCVTZU */
11492 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
11494 case 0x18: /* FRINTN */
11495 case 0x19: /* FRINTM */
11496 case 0x38: /* FRINTP */
11497 case 0x39: /* FRINTZ */
11498 case 0x58: /* FRINTA */
11499 case 0x79: /* FRINTI */
11500 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
11502 case 0x59: /* FRINTX */
11503 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
11505 case 0x2f: /* FABS */
11506 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
11508 case 0x6f: /* FNEG */
11509 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
11511 case 0x7d: /* FRSQRTE */
11512 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
11514 case 0x7f: /* FSQRT */
11515 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
11518 g_assert_not_reached();
11521 write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11523 tcg_temp_free_i32(tcg_res);
11524 tcg_temp_free_i32(tcg_op);
11527 clear_vec_high(s, is_q, rd);
11531 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
11532 tcg_temp_free_i32(tcg_rmode);
11535 if (tcg_fpstatus) {
11536 tcg_temp_free_ptr(tcg_fpstatus);
11540 /* AdvSIMD scalar x indexed element
11541 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
11542 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
11543 * | 0 1 | U | 1 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
11544 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
11545 * AdvSIMD vector x indexed element
11546 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
11547 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
11548 * | 0 | Q | U | 0 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
11549 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
11551 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
11553 /* This encoding has two kinds of instruction:
11554 * normal, where we perform elt x idxelt => elt for each
11555 * element in the vector
11556 * long, where we perform elt x idxelt and generate a result of
11557 * double the width of the input element
11558 * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
11560 bool is_scalar = extract32(insn, 28, 1);
11561 bool is_q = extract32(insn, 30, 1);
11562 bool u = extract32(insn, 29, 1);
11563 int size = extract32(insn, 22, 2);
11564 int l = extract32(insn, 21, 1);
11565 int m = extract32(insn, 20, 1);
11566 /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
11567 int rm = extract32(insn, 16, 4);
11568 int opcode = extract32(insn, 12, 4);
11569 int h = extract32(insn, 11, 1);
11570 int rn = extract32(insn, 5, 5);
11571 int rd = extract32(insn, 0, 5);
11572 bool is_long = false;
11573 bool is_fp = false;
11574 bool is_fp16 = false;
11579 case 0x0: /* MLA */
11580 case 0x4: /* MLS */
11581 if (!u || is_scalar) {
11582 unallocated_encoding(s);
11586 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
11587 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
11588 case 0xa: /* SMULL, SMULL2, UMULL, UMULL2 */
11590 unallocated_encoding(s);
11595 case 0x3: /* SQDMLAL, SQDMLAL2 */
11596 case 0x7: /* SQDMLSL, SQDMLSL2 */
11597 case 0xb: /* SQDMULL, SQDMULL2 */
11600 case 0xc: /* SQDMULH */
11601 case 0xd: /* SQRDMULH */
11603 unallocated_encoding(s);
11607 case 0x8: /* MUL */
11608 if (u || is_scalar) {
11609 unallocated_encoding(s);
11613 case 0x1: /* FMLA */
11614 case 0x5: /* FMLS */
11616 unallocated_encoding(s);
11620 case 0x9: /* FMUL, FMULX */
11622 unallocated_encoding(s);
11628 unallocated_encoding(s);
11633 /* convert insn encoded size to TCGMemOp size */
11635 case 2: /* single precision */
11637 index = h << 1 | l;
11640 case 3: /* double precision */
11643 unallocated_encoding(s);
11649 case 0: /* half precision */
11651 index = h << 2 | l << 1 | m;
11653 if (arm_dc_feature(s, ARM_FEATURE_V8_FP16)) {
11657 default: /* unallocated */
11658 unallocated_encoding(s);
11664 index = h << 2 | l << 1 | m;
11667 index = h << 1 | l;
11671 unallocated_encoding(s);
11676 if (!fp_access_check(s)) {
11681 fpst = get_fpstatus_ptr(is_fp16);
11687 TCGv_i64 tcg_idx = tcg_temp_new_i64();
11690 assert(is_fp && is_q && !is_long);
11692 read_vec_element(s, tcg_idx, rm, index, MO_64);
11694 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
11695 TCGv_i64 tcg_op = tcg_temp_new_i64();
11696 TCGv_i64 tcg_res = tcg_temp_new_i64();
11698 read_vec_element(s, tcg_op, rn, pass, MO_64);
11701 case 0x5: /* FMLS */
11702 /* As usual for ARM, separate negation for fused multiply-add */
11703 gen_helper_vfp_negd(tcg_op, tcg_op);
11705 case 0x1: /* FMLA */
11706 read_vec_element(s, tcg_res, rd, pass, MO_64);
11707 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
11709 case 0x9: /* FMUL, FMULX */
11711 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
11713 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
11717 g_assert_not_reached();
11720 write_vec_element(s, tcg_res, rd, pass, MO_64);
11721 tcg_temp_free_i64(tcg_op);
11722 tcg_temp_free_i64(tcg_res);
11725 tcg_temp_free_i64(tcg_idx);
11726 clear_vec_high(s, !is_scalar, rd);
11727 } else if (!is_long) {
11728 /* 32 bit floating point, or 16 or 32 bit integer.
11729 * For the 16 bit scalar case we use the usual Neon helpers and
11730 * rely on the fact that 0 op 0 == 0 with no side effects.
11732 TCGv_i32 tcg_idx = tcg_temp_new_i32();
11733 int pass, maxpasses;
11738 maxpasses = is_q ? 4 : 2;
11741 read_vec_element_i32(s, tcg_idx, rm, index, size);
11743 if (size == 1 && !is_scalar) {
11744 /* The simplest way to handle the 16x16 indexed ops is to duplicate
11745 * the index into both halves of the 32 bit tcg_idx and then use
11746 * the usual Neon helpers.
11748 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
11751 for (pass = 0; pass < maxpasses; pass++) {
11752 TCGv_i32 tcg_op = tcg_temp_new_i32();
11753 TCGv_i32 tcg_res = tcg_temp_new_i32();
11755 read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
11758 case 0x0: /* MLA */
11759 case 0x4: /* MLS */
11760 case 0x8: /* MUL */
11762 static NeonGenTwoOpFn * const fns[2][2] = {
11763 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
11764 { tcg_gen_add_i32, tcg_gen_sub_i32 },
11766 NeonGenTwoOpFn *genfn;
11767 bool is_sub = opcode == 0x4;
11770 gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
11772 tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
11774 if (opcode == 0x8) {
11777 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
11778 genfn = fns[size - 1][is_sub];
11779 genfn(tcg_res, tcg_op, tcg_res);
11782 case 0x5: /* FMLS */
11783 case 0x1: /* FMLA */
11784 read_vec_element_i32(s, tcg_res, rd, pass,
11785 is_scalar ? size : MO_32);
11788 if (opcode == 0x5) {
11789 /* As usual for ARM, separate negation for fused
11791 tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
11794 gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
11797 gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
11802 if (opcode == 0x5) {
11803 /* As usual for ARM, separate negation for
11804 * fused multiply-add */
11805 tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
11807 gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
11811 g_assert_not_reached();
11814 case 0x9: /* FMUL, FMULX */
11819 gen_helper_advsimd_mulxh(tcg_res, tcg_op,
11822 gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
11827 gen_helper_advsimd_mulh(tcg_res, tcg_op,
11830 gen_helper_advsimd_mul2h(tcg_res, tcg_op,
11837 gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
11839 gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
11843 g_assert_not_reached();
11846 case 0xc: /* SQDMULH */
11848 gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
11851 gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
11855 case 0xd: /* SQRDMULH */
11857 gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
11860 gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
11865 g_assert_not_reached();
11869 write_fp_sreg(s, rd, tcg_res);
11871 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11874 tcg_temp_free_i32(tcg_op);
11875 tcg_temp_free_i32(tcg_res);
11878 tcg_temp_free_i32(tcg_idx);
11879 clear_vec_high(s, is_q, rd);
11881 /* long ops: 16x16->32 or 32x32->64 */
11882 TCGv_i64 tcg_res[2];
11884 bool satop = extract32(opcode, 0, 1);
11885 TCGMemOp memop = MO_32;
11892 TCGv_i64 tcg_idx = tcg_temp_new_i64();
11894 read_vec_element(s, tcg_idx, rm, index, memop);
11896 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
11897 TCGv_i64 tcg_op = tcg_temp_new_i64();
11898 TCGv_i64 tcg_passres;
11904 passelt = pass + (is_q * 2);
11907 read_vec_element(s, tcg_op, rn, passelt, memop);
11909 tcg_res[pass] = tcg_temp_new_i64();
11911 if (opcode == 0xa || opcode == 0xb) {
11912 /* Non-accumulating ops */
11913 tcg_passres = tcg_res[pass];
11915 tcg_passres = tcg_temp_new_i64();
11918 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
11919 tcg_temp_free_i64(tcg_op);
11922 /* saturating, doubling */
11923 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
11924 tcg_passres, tcg_passres);
11927 if (opcode == 0xa || opcode == 0xb) {
11931 /* Accumulating op: handle accumulate step */
11932 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11935 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
11936 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
11938 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
11939 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
11941 case 0x7: /* SQDMLSL, SQDMLSL2 */
11942 tcg_gen_neg_i64(tcg_passres, tcg_passres);
11944 case 0x3: /* SQDMLAL, SQDMLAL2 */
11945 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
11950 g_assert_not_reached();
11952 tcg_temp_free_i64(tcg_passres);
11954 tcg_temp_free_i64(tcg_idx);
11956 clear_vec_high(s, !is_scalar, rd);
11958 TCGv_i32 tcg_idx = tcg_temp_new_i32();
11961 read_vec_element_i32(s, tcg_idx, rm, index, size);
11964 /* The simplest way to handle the 16x16 indexed ops is to
11965 * duplicate the index into both halves of the 32 bit tcg_idx
11966 * and then use the usual Neon helpers.
11968 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
11971 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
11972 TCGv_i32 tcg_op = tcg_temp_new_i32();
11973 TCGv_i64 tcg_passres;
11976 read_vec_element_i32(s, tcg_op, rn, pass, size);
11978 read_vec_element_i32(s, tcg_op, rn,
11979 pass + (is_q * 2), MO_32);
11982 tcg_res[pass] = tcg_temp_new_i64();
11984 if (opcode == 0xa || opcode == 0xb) {
11985 /* Non-accumulating ops */
11986 tcg_passres = tcg_res[pass];
11988 tcg_passres = tcg_temp_new_i64();
11991 if (memop & MO_SIGN) {
11992 gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
11994 gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
11997 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
11998 tcg_passres, tcg_passres);
12000 tcg_temp_free_i32(tcg_op);
12002 if (opcode == 0xa || opcode == 0xb) {
12006 /* Accumulating op: handle accumulate step */
12007 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12010 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
12011 gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
12014 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
12015 gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
12018 case 0x7: /* SQDMLSL, SQDMLSL2 */
12019 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
12021 case 0x3: /* SQDMLAL, SQDMLAL2 */
12022 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
12027 g_assert_not_reached();
12029 tcg_temp_free_i64(tcg_passres);
12031 tcg_temp_free_i32(tcg_idx);
12034 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
12039 tcg_res[1] = tcg_const_i64(0);
12042 for (pass = 0; pass < 2; pass++) {
12043 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12044 tcg_temp_free_i64(tcg_res[pass]);
12049 tcg_temp_free_ptr(fpst);
12054 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
12055 * +-----------------+------+-----------+--------+-----+------+------+
12056 * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
12057 * +-----------------+------+-----------+--------+-----+------+------+
12059 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
12061 int size = extract32(insn, 22, 2);
12062 int opcode = extract32(insn, 12, 5);
12063 int rn = extract32(insn, 5, 5);
12064 int rd = extract32(insn, 0, 5);
12066 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr;
12067 TCGv_i32 tcg_decrypt;
12068 CryptoThreeOpIntFn *genfn;
12070 if (!arm_dc_feature(s, ARM_FEATURE_V8_AES)
12072 unallocated_encoding(s);
12077 case 0x4: /* AESE */
12079 genfn = gen_helper_crypto_aese;
12081 case 0x6: /* AESMC */
12083 genfn = gen_helper_crypto_aesmc;
12085 case 0x5: /* AESD */
12087 genfn = gen_helper_crypto_aese;
12089 case 0x7: /* AESIMC */
12091 genfn = gen_helper_crypto_aesmc;
12094 unallocated_encoding(s);
12098 if (!fp_access_check(s)) {
12102 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
12103 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
12104 tcg_decrypt = tcg_const_i32(decrypt);
12106 genfn(tcg_rd_ptr, tcg_rn_ptr, tcg_decrypt);
12108 tcg_temp_free_ptr(tcg_rd_ptr);
12109 tcg_temp_free_ptr(tcg_rn_ptr);
12110 tcg_temp_free_i32(tcg_decrypt);
12113 /* Crypto three-reg SHA
12114 * 31 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
12115 * +-----------------+------+---+------+---+--------+-----+------+------+
12116 * | 0 1 0 1 1 1 1 0 | size | 0 | Rm | 0 | opcode | 0 0 | Rn | Rd |
12117 * +-----------------+------+---+------+---+--------+-----+------+------+
12119 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
12121 int size = extract32(insn, 22, 2);
12122 int opcode = extract32(insn, 12, 3);
12123 int rm = extract32(insn, 16, 5);
12124 int rn = extract32(insn, 5, 5);
12125 int rd = extract32(insn, 0, 5);
12126 CryptoThreeOpFn *genfn;
12127 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr;
12128 int feature = ARM_FEATURE_V8_SHA256;
12131 unallocated_encoding(s);
12136 case 0: /* SHA1C */
12137 case 1: /* SHA1P */
12138 case 2: /* SHA1M */
12139 case 3: /* SHA1SU0 */
12141 feature = ARM_FEATURE_V8_SHA1;
12143 case 4: /* SHA256H */
12144 genfn = gen_helper_crypto_sha256h;
12146 case 5: /* SHA256H2 */
12147 genfn = gen_helper_crypto_sha256h2;
12149 case 6: /* SHA256SU1 */
12150 genfn = gen_helper_crypto_sha256su1;
12153 unallocated_encoding(s);
12157 if (!arm_dc_feature(s, feature)) {
12158 unallocated_encoding(s);
12162 if (!fp_access_check(s)) {
12166 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
12167 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
12168 tcg_rm_ptr = vec_full_reg_ptr(s, rm);
12171 genfn(tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr);
12173 TCGv_i32 tcg_opcode = tcg_const_i32(opcode);
12175 gen_helper_crypto_sha1_3reg(tcg_rd_ptr, tcg_rn_ptr,
12176 tcg_rm_ptr, tcg_opcode);
12177 tcg_temp_free_i32(tcg_opcode);
12180 tcg_temp_free_ptr(tcg_rd_ptr);
12181 tcg_temp_free_ptr(tcg_rn_ptr);
12182 tcg_temp_free_ptr(tcg_rm_ptr);
12185 /* Crypto two-reg SHA
12186 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
12187 * +-----------------+------+-----------+--------+-----+------+------+
12188 * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
12189 * +-----------------+------+-----------+--------+-----+------+------+
12191 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
12193 int size = extract32(insn, 22, 2);
12194 int opcode = extract32(insn, 12, 5);
12195 int rn = extract32(insn, 5, 5);
12196 int rd = extract32(insn, 0, 5);
12197 CryptoTwoOpFn *genfn;
12199 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr;
12202 unallocated_encoding(s);
12207 case 0: /* SHA1H */
12208 feature = ARM_FEATURE_V8_SHA1;
12209 genfn = gen_helper_crypto_sha1h;
12211 case 1: /* SHA1SU1 */
12212 feature = ARM_FEATURE_V8_SHA1;
12213 genfn = gen_helper_crypto_sha1su1;
12215 case 2: /* SHA256SU0 */
12216 feature = ARM_FEATURE_V8_SHA256;
12217 genfn = gen_helper_crypto_sha256su0;
12220 unallocated_encoding(s);
12224 if (!arm_dc_feature(s, feature)) {
12225 unallocated_encoding(s);
12229 if (!fp_access_check(s)) {
12233 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
12234 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
12236 genfn(tcg_rd_ptr, tcg_rn_ptr);
12238 tcg_temp_free_ptr(tcg_rd_ptr);
12239 tcg_temp_free_ptr(tcg_rn_ptr);
12242 /* Crypto three-reg SHA512
12243 * 31 21 20 16 15 14 13 12 11 10 9 5 4 0
12244 * +-----------------------+------+---+---+-----+--------+------+------+
12245 * | 1 1 0 0 1 1 1 0 0 1 1 | Rm | 1 | O | 0 0 | opcode | Rn | Rd |
12246 * +-----------------------+------+---+---+-----+--------+------+------+
12248 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
12250 int opcode = extract32(insn, 10, 2);
12251 int o = extract32(insn, 14, 1);
12252 int rm = extract32(insn, 16, 5);
12253 int rn = extract32(insn, 5, 5);
12254 int rd = extract32(insn, 0, 5);
12256 CryptoThreeOpFn *genfn;
12260 case 0: /* SHA512H */
12261 feature = ARM_FEATURE_V8_SHA512;
12262 genfn = gen_helper_crypto_sha512h;
12264 case 1: /* SHA512H2 */
12265 feature = ARM_FEATURE_V8_SHA512;
12266 genfn = gen_helper_crypto_sha512h2;
12268 case 2: /* SHA512SU1 */
12269 feature = ARM_FEATURE_V8_SHA512;
12270 genfn = gen_helper_crypto_sha512su1;
12273 feature = ARM_FEATURE_V8_SHA3;
12279 case 0: /* SM3PARTW1 */
12280 feature = ARM_FEATURE_V8_SM3;
12281 genfn = gen_helper_crypto_sm3partw1;
12283 case 1: /* SM3PARTW2 */
12284 feature = ARM_FEATURE_V8_SM3;
12285 genfn = gen_helper_crypto_sm3partw2;
12287 case 2: /* SM4EKEY */
12288 feature = ARM_FEATURE_V8_SM4;
12289 genfn = gen_helper_crypto_sm4ekey;
12292 unallocated_encoding(s);
12297 if (!arm_dc_feature(s, feature)) {
12298 unallocated_encoding(s);
12302 if (!fp_access_check(s)) {
12307 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr;
12309 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
12310 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
12311 tcg_rm_ptr = vec_full_reg_ptr(s, rm);
12313 genfn(tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr);
12315 tcg_temp_free_ptr(tcg_rd_ptr);
12316 tcg_temp_free_ptr(tcg_rn_ptr);
12317 tcg_temp_free_ptr(tcg_rm_ptr);
12319 TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
12322 tcg_op1 = tcg_temp_new_i64();
12323 tcg_op2 = tcg_temp_new_i64();
12324 tcg_res[0] = tcg_temp_new_i64();
12325 tcg_res[1] = tcg_temp_new_i64();
12327 for (pass = 0; pass < 2; pass++) {
12328 read_vec_element(s, tcg_op1, rn, pass, MO_64);
12329 read_vec_element(s, tcg_op2, rm, pass, MO_64);
12331 tcg_gen_rotli_i64(tcg_res[pass], tcg_op2, 1);
12332 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12334 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
12335 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
12337 tcg_temp_free_i64(tcg_op1);
12338 tcg_temp_free_i64(tcg_op2);
12339 tcg_temp_free_i64(tcg_res[0]);
12340 tcg_temp_free_i64(tcg_res[1]);
12344 /* Crypto two-reg SHA512
12345 * 31 12 11 10 9 5 4 0
12346 * +-----------------------------------------+--------+------+------+
12347 * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode | Rn | Rd |
12348 * +-----------------------------------------+--------+------+------+
12350 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
12352 int opcode = extract32(insn, 10, 2);
12353 int rn = extract32(insn, 5, 5);
12354 int rd = extract32(insn, 0, 5);
12355 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr;
12357 CryptoTwoOpFn *genfn;
12360 case 0: /* SHA512SU0 */
12361 feature = ARM_FEATURE_V8_SHA512;
12362 genfn = gen_helper_crypto_sha512su0;
12365 feature = ARM_FEATURE_V8_SM4;
12366 genfn = gen_helper_crypto_sm4e;
12369 unallocated_encoding(s);
12373 if (!arm_dc_feature(s, feature)) {
12374 unallocated_encoding(s);
12378 if (!fp_access_check(s)) {
12382 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
12383 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
12385 genfn(tcg_rd_ptr, tcg_rn_ptr);
12387 tcg_temp_free_ptr(tcg_rd_ptr);
12388 tcg_temp_free_ptr(tcg_rn_ptr);
12391 /* Crypto four-register
12392 * 31 23 22 21 20 16 15 14 10 9 5 4 0
12393 * +-------------------+-----+------+---+------+------+------+
12394 * | 1 1 0 0 1 1 1 0 0 | Op0 | Rm | 0 | Ra | Rn | Rd |
12395 * +-------------------+-----+------+---+------+------+------+
12397 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
12399 int op0 = extract32(insn, 21, 2);
12400 int rm = extract32(insn, 16, 5);
12401 int ra = extract32(insn, 10, 5);
12402 int rn = extract32(insn, 5, 5);
12403 int rd = extract32(insn, 0, 5);
12409 feature = ARM_FEATURE_V8_SHA3;
12411 case 2: /* SM3SS1 */
12412 feature = ARM_FEATURE_V8_SM3;
12415 unallocated_encoding(s);
12419 if (!arm_dc_feature(s, feature)) {
12420 unallocated_encoding(s);
12424 if (!fp_access_check(s)) {
12429 TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
12432 tcg_op1 = tcg_temp_new_i64();
12433 tcg_op2 = tcg_temp_new_i64();
12434 tcg_op3 = tcg_temp_new_i64();
12435 tcg_res[0] = tcg_temp_new_i64();
12436 tcg_res[1] = tcg_temp_new_i64();
12438 for (pass = 0; pass < 2; pass++) {
12439 read_vec_element(s, tcg_op1, rn, pass, MO_64);
12440 read_vec_element(s, tcg_op2, rm, pass, MO_64);
12441 read_vec_element(s, tcg_op3, ra, pass, MO_64);
12445 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
12448 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
12450 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12452 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
12453 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
12455 tcg_temp_free_i64(tcg_op1);
12456 tcg_temp_free_i64(tcg_op2);
12457 tcg_temp_free_i64(tcg_op3);
12458 tcg_temp_free_i64(tcg_res[0]);
12459 tcg_temp_free_i64(tcg_res[1]);
12461 TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
12463 tcg_op1 = tcg_temp_new_i32();
12464 tcg_op2 = tcg_temp_new_i32();
12465 tcg_op3 = tcg_temp_new_i32();
12466 tcg_res = tcg_temp_new_i32();
12467 tcg_zero = tcg_const_i32(0);
12469 read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
12470 read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
12471 read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
12473 tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
12474 tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
12475 tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
12476 tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
12478 write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
12479 write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
12480 write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
12481 write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
12483 tcg_temp_free_i32(tcg_op1);
12484 tcg_temp_free_i32(tcg_op2);
12485 tcg_temp_free_i32(tcg_op3);
12486 tcg_temp_free_i32(tcg_res);
12487 tcg_temp_free_i32(tcg_zero);
12492 * 31 21 20 16 15 10 9 5 4 0
12493 * +-----------------------+------+--------+------+------+
12494 * | 1 1 0 0 1 1 1 0 1 0 0 | Rm | imm6 | Rn | Rd |
12495 * +-----------------------+------+--------+------+------+
12497 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
12499 int rm = extract32(insn, 16, 5);
12500 int imm6 = extract32(insn, 10, 6);
12501 int rn = extract32(insn, 5, 5);
12502 int rd = extract32(insn, 0, 5);
12503 TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
12506 if (!arm_dc_feature(s, ARM_FEATURE_V8_SHA3)) {
12507 unallocated_encoding(s);
12511 if (!fp_access_check(s)) {
12515 tcg_op1 = tcg_temp_new_i64();
12516 tcg_op2 = tcg_temp_new_i64();
12517 tcg_res[0] = tcg_temp_new_i64();
12518 tcg_res[1] = tcg_temp_new_i64();
12520 for (pass = 0; pass < 2; pass++) {
12521 read_vec_element(s, tcg_op1, rn, pass, MO_64);
12522 read_vec_element(s, tcg_op2, rm, pass, MO_64);
12524 tcg_gen_xor_i64(tcg_res[pass], tcg_op1, tcg_op2);
12525 tcg_gen_rotri_i64(tcg_res[pass], tcg_res[pass], imm6);
12527 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
12528 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
12530 tcg_temp_free_i64(tcg_op1);
12531 tcg_temp_free_i64(tcg_op2);
12532 tcg_temp_free_i64(tcg_res[0]);
12533 tcg_temp_free_i64(tcg_res[1]);
12536 /* Crypto three-reg imm2
12537 * 31 21 20 16 15 14 13 12 11 10 9 5 4 0
12538 * +-----------------------+------+-----+------+--------+------+------+
12539 * | 1 1 0 0 1 1 1 0 0 1 0 | Rm | 1 0 | imm2 | opcode | Rn | Rd |
12540 * +-----------------------+------+-----+------+--------+------+------+
12542 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
12544 int opcode = extract32(insn, 10, 2);
12545 int imm2 = extract32(insn, 12, 2);
12546 int rm = extract32(insn, 16, 5);
12547 int rn = extract32(insn, 5, 5);
12548 int rd = extract32(insn, 0, 5);
12549 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr;
12550 TCGv_i32 tcg_imm2, tcg_opcode;
12552 if (!arm_dc_feature(s, ARM_FEATURE_V8_SM3)) {
12553 unallocated_encoding(s);
12557 if (!fp_access_check(s)) {
12561 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
12562 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
12563 tcg_rm_ptr = vec_full_reg_ptr(s, rm);
12564 tcg_imm2 = tcg_const_i32(imm2);
12565 tcg_opcode = tcg_const_i32(opcode);
12567 gen_helper_crypto_sm3tt(tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr, tcg_imm2,
12570 tcg_temp_free_ptr(tcg_rd_ptr);
12571 tcg_temp_free_ptr(tcg_rn_ptr);
12572 tcg_temp_free_ptr(tcg_rm_ptr);
12573 tcg_temp_free_i32(tcg_imm2);
12574 tcg_temp_free_i32(tcg_opcode);
12577 /* C3.6 Data processing - SIMD, inc Crypto
12579 * As the decode gets a little complex we are using a table based
12580 * approach for this part of the decode.
12582 static const AArch64DecodeTable data_proc_simd[] = {
12583 /* pattern , mask , fn */
12584 { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
12585 { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
12586 { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
12587 { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
12588 { 0x0e000400, 0x9fe08400, disas_simd_copy },
12589 { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
12590 /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
12591 { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
12592 { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
12593 { 0x0e000000, 0xbf208c00, disas_simd_tb },
12594 { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
12595 { 0x2e000000, 0xbf208400, disas_simd_ext },
12596 { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
12597 { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
12598 { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
12599 { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
12600 { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
12601 { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
12602 { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
12603 { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
12604 { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
12605 { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
12606 { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
12607 { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
12608 { 0xce000000, 0xff808000, disas_crypto_four_reg },
12609 { 0xce800000, 0xffe00000, disas_crypto_xar },
12610 { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
12611 { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
12612 { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
12613 { 0x00000000, 0x00000000, NULL }
12616 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
12618 /* Note that this is called with all non-FP cases from
12619 * table C3-6 so it must UNDEF for entries not specifically
12620 * allocated to instructions in that table.
12622 AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
12626 unallocated_encoding(s);
12630 /* C3.6 Data processing - SIMD and floating point */
12631 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
12633 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
12634 disas_data_proc_fp(s, insn);
12636 /* SIMD, including crypto */
12637 disas_data_proc_simd(s, insn);
12641 /* C3.1 A64 instruction index by encoding */
12642 static void disas_a64_insn(CPUARMState *env, DisasContext *s)
12646 insn = arm_ldl_code(env, s->pc, s->sctlr_b);
12650 s->fp_access_checked = false;
12652 switch (extract32(insn, 25, 4)) {
12653 case 0x0: case 0x1: case 0x2: case 0x3: /* UNALLOCATED */
12654 unallocated_encoding(s);
12656 case 0x8: case 0x9: /* Data processing - immediate */
12657 disas_data_proc_imm(s, insn);
12659 case 0xa: case 0xb: /* Branch, exception generation and system insns */
12660 disas_b_exc_sys(s, insn);
12665 case 0xe: /* Loads and stores */
12666 disas_ldst(s, insn);
12669 case 0xd: /* Data processing - register */
12670 disas_data_proc_reg(s, insn);
12673 case 0xf: /* Data processing - SIMD and floating point */
12674 disas_data_proc_simd_fp(s, insn);
12677 assert(FALSE); /* all 15 cases should be handled above */
12681 /* if we allocated any temporaries, free them here */
12685 static int aarch64_tr_init_disas_context(DisasContextBase *dcbase,
12686 CPUState *cpu, int max_insns)
12688 DisasContext *dc = container_of(dcbase, DisasContext, base);
12689 CPUARMState *env = cpu->env_ptr;
12690 ARMCPU *arm_cpu = arm_env_get_cpu(env);
12693 dc->pc = dc->base.pc_first;
12697 /* If we are coming from secure EL0 in a system with a 32-bit EL3, then
12698 * there is no secure EL1, so we route exceptions to EL3.
12700 dc->secure_routed_to_el3 = arm_feature(env, ARM_FEATURE_EL3) &&
12701 !arm_el_is_aa64(env, 3);
12704 dc->be_data = ARM_TBFLAG_BE_DATA(dc->base.tb->flags) ? MO_BE : MO_LE;
12705 dc->condexec_mask = 0;
12706 dc->condexec_cond = 0;
12707 dc->mmu_idx = core_to_arm_mmu_idx(env, ARM_TBFLAG_MMUIDX(dc->base.tb->flags));
12708 dc->tbi0 = ARM_TBFLAG_TBI0(dc->base.tb->flags);
12709 dc->tbi1 = ARM_TBFLAG_TBI1(dc->base.tb->flags);
12710 dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
12711 #if !defined(CONFIG_USER_ONLY)
12712 dc->user = (dc->current_el == 0);
12714 dc->fp_excp_el = ARM_TBFLAG_FPEXC_EL(dc->base.tb->flags);
12715 dc->sve_excp_el = ARM_TBFLAG_SVEEXC_EL(dc->base.tb->flags);
12716 dc->sve_len = (ARM_TBFLAG_ZCR_LEN(dc->base.tb->flags) + 1) * 16;
12718 dc->vec_stride = 0;
12719 dc->cp_regs = arm_cpu->cp_regs;
12720 dc->features = env->features;
12722 /* Single step state. The code-generation logic here is:
12724 * generate code with no special handling for single-stepping (except
12725 * that anything that can make us go to SS_ACTIVE == 1 must end the TB;
12726 * this happens anyway because those changes are all system register or
12728 * SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
12729 * emit code for one insn
12730 * emit code to clear PSTATE.SS
12731 * emit code to generate software step exception for completed step
12732 * end TB (as usual for having generated an exception)
12733 * SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
12734 * emit code to generate a software step exception
12737 dc->ss_active = ARM_TBFLAG_SS_ACTIVE(dc->base.tb->flags);
12738 dc->pstate_ss = ARM_TBFLAG_PSTATE_SS(dc->base.tb->flags);
12739 dc->is_ldex = false;
12740 dc->ss_same_el = (arm_debug_target_el(env) == dc->current_el);
12742 /* Bound the number of insns to execute to those left on the page. */
12743 bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
12745 /* If architectural single step active, limit to 1. */
12746 if (dc->ss_active) {
12749 max_insns = MIN(max_insns, bound);
12751 init_tmp_a64_array(dc);
12756 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
12758 tcg_clear_temp_count();
12761 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
12763 DisasContext *dc = container_of(dcbase, DisasContext, base);
12765 tcg_gen_insn_start(dc->pc, 0, 0);
12766 dc->insn_start = tcg_last_op();
12769 static bool aarch64_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,
12770 const CPUBreakpoint *bp)
12772 DisasContext *dc = container_of(dcbase, DisasContext, base);
12774 if (bp->flags & BP_CPU) {
12775 gen_a64_set_pc_im(dc->pc);
12776 gen_helper_check_breakpoints(cpu_env);
12777 /* End the TB early; it likely won't be executed */
12778 dc->base.is_jmp = DISAS_TOO_MANY;
12780 gen_exception_internal_insn(dc, 0, EXCP_DEBUG);
12781 /* The address covered by the breakpoint must be
12782 included in [tb->pc, tb->pc + tb->size) in order
12783 to for it to be properly cleared -- thus we
12784 increment the PC here so that the logic setting
12785 tb->size below does the right thing. */
12787 dc->base.is_jmp = DISAS_NORETURN;
12793 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
12795 DisasContext *dc = container_of(dcbase, DisasContext, base);
12796 CPUARMState *env = cpu->env_ptr;
12798 if (dc->ss_active && !dc->pstate_ss) {
12799 /* Singlestep state is Active-pending.
12800 * If we're in this state at the start of a TB then either
12801 * a) we just took an exception to an EL which is being debugged
12802 * and this is the first insn in the exception handler
12803 * b) debug exceptions were masked and we just unmasked them
12804 * without changing EL (eg by clearing PSTATE.D)
12805 * In either case we're going to take a swstep exception in the
12806 * "did not step an insn" case, and so the syndrome ISV and EX
12807 * bits should be zero.
12809 assert(dc->base.num_insns == 1);
12810 gen_exception(EXCP_UDEF, syn_swstep(dc->ss_same_el, 0, 0),
12811 default_exception_el(dc));
12812 dc->base.is_jmp = DISAS_NORETURN;
12814 disas_a64_insn(env, dc);
12817 dc->base.pc_next = dc->pc;
12818 translator_loop_temp_check(&dc->base);
12821 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
12823 DisasContext *dc = container_of(dcbase, DisasContext, base);
12825 if (unlikely(dc->base.singlestep_enabled || dc->ss_active)) {
12826 /* Note that this means single stepping WFI doesn't halt the CPU.
12827 * For conditional branch insns this is harmless unreachable code as
12828 * gen_goto_tb() has already handled emitting the debug exception
12829 * (and thus a tb-jump is not possible when singlestepping).
12831 switch (dc->base.is_jmp) {
12833 gen_a64_set_pc_im(dc->pc);
12837 if (dc->base.singlestep_enabled) {
12838 gen_exception_internal(EXCP_DEBUG);
12840 gen_step_complete_exception(dc);
12843 case DISAS_NORETURN:
12847 switch (dc->base.is_jmp) {
12849 case DISAS_TOO_MANY:
12850 gen_goto_tb(dc, 1, dc->pc);
12854 gen_a64_set_pc_im(dc->pc);
12857 tcg_gen_lookup_and_goto_ptr();
12860 tcg_gen_exit_tb(0);
12862 case DISAS_NORETURN:
12866 gen_a64_set_pc_im(dc->pc);
12867 gen_helper_wfe(cpu_env);
12870 gen_a64_set_pc_im(dc->pc);
12871 gen_helper_yield(cpu_env);
12875 /* This is a special case because we don't want to just halt the CPU
12876 * if trying to debug across a WFI.
12878 TCGv_i32 tmp = tcg_const_i32(4);
12880 gen_a64_set_pc_im(dc->pc);
12881 gen_helper_wfi(cpu_env, tmp);
12882 tcg_temp_free_i32(tmp);
12883 /* The helper doesn't necessarily throw an exception, but we
12884 * must go back to the main loop to check for interrupts anyway.
12886 tcg_gen_exit_tb(0);
12892 /* Functions above can change dc->pc, so re-align db->pc_next */
12893 dc->base.pc_next = dc->pc;
12896 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
12899 DisasContext *dc = container_of(dcbase, DisasContext, base);
12901 qemu_log("IN: %s\n", lookup_symbol(dc->base.pc_first));
12902 log_target_disas(cpu, dc->base.pc_first, dc->base.tb->size);
12905 const TranslatorOps aarch64_translator_ops = {
12906 .init_disas_context = aarch64_tr_init_disas_context,
12907 .tb_start = aarch64_tr_tb_start,
12908 .insn_start = aarch64_tr_insn_start,
12909 .breakpoint_check = aarch64_tr_breakpoint_check,
12910 .translate_insn = aarch64_tr_translate_insn,
12911 .tb_stop = aarch64_tr_tb_stop,
12912 .disas_log = aarch64_tr_disas_log,