arm: Don't decode MRS(banked) or MSR(banked) for M profile
[qemu/ar7.git] / target / arm / translate-a64.c
blob24de30d92cca7774230bb21ca493691d586c94d7
1 /*
2 * AArch64 translation
4 * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
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"
21 #include "cpu.h"
22 #include "exec/exec-all.h"
23 #include "tcg-op.h"
24 #include "qemu/log.h"
25 #include "arm_ldst.h"
26 #include "translate.h"
27 #include "internals.h"
28 #include "qemu/host-utils.h"
30 #include "exec/semihost.h"
31 #include "exec/gen-icount.h"
33 #include "exec/helper-proto.h"
34 #include "exec/helper-gen.h"
35 #include "exec/log.h"
37 #include "trace-tcg.h"
39 static TCGv_i64 cpu_X[32];
40 static TCGv_i64 cpu_pc;
42 /* Load/store exclusive handling */
43 static TCGv_i64 cpu_exclusive_high;
44 static TCGv_i64 cpu_reg(DisasContext *s, int reg);
46 static const char *regnames[] = {
47 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
48 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
49 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
50 "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
53 enum a64_shift_type {
54 A64_SHIFT_TYPE_LSL = 0,
55 A64_SHIFT_TYPE_LSR = 1,
56 A64_SHIFT_TYPE_ASR = 2,
57 A64_SHIFT_TYPE_ROR = 3
60 /* Table based decoder typedefs - used when the relevant bits for decode
61 * are too awkwardly scattered across the instruction (eg SIMD).
63 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
65 typedef struct AArch64DecodeTable {
66 uint32_t pattern;
67 uint32_t mask;
68 AArch64DecodeFn *disas_fn;
69 } AArch64DecodeTable;
71 /* Function prototype for gen_ functions for calling Neon helpers */
72 typedef void NeonGenOneOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32);
73 typedef void NeonGenTwoOpFn(TCGv_i32, TCGv_i32, TCGv_i32);
74 typedef void NeonGenTwoOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32, TCGv_i32);
75 typedef void NeonGenTwo64OpFn(TCGv_i64, TCGv_i64, TCGv_i64);
76 typedef void NeonGenTwo64OpEnvFn(TCGv_i64, TCGv_ptr, TCGv_i64, TCGv_i64);
77 typedef void NeonGenNarrowFn(TCGv_i32, TCGv_i64);
78 typedef void NeonGenNarrowEnvFn(TCGv_i32, TCGv_ptr, TCGv_i64);
79 typedef void NeonGenWidenFn(TCGv_i64, TCGv_i32);
80 typedef void NeonGenTwoSingleOPFn(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
81 typedef void NeonGenTwoDoubleOPFn(TCGv_i64, TCGv_i64, TCGv_i64, TCGv_ptr);
82 typedef void NeonGenOneOpFn(TCGv_i64, TCGv_i64);
83 typedef void CryptoTwoOpEnvFn(TCGv_ptr, TCGv_i32, TCGv_i32);
84 typedef void CryptoThreeOpEnvFn(TCGv_ptr, TCGv_i32, TCGv_i32, TCGv_i32);
86 /* initialize TCG globals. */
87 void a64_translate_init(void)
89 int i;
91 cpu_pc = tcg_global_mem_new_i64(cpu_env,
92 offsetof(CPUARMState, pc),
93 "pc");
94 for (i = 0; i < 32; i++) {
95 cpu_X[i] = tcg_global_mem_new_i64(cpu_env,
96 offsetof(CPUARMState, xregs[i]),
97 regnames[i]);
100 cpu_exclusive_high = tcg_global_mem_new_i64(cpu_env,
101 offsetof(CPUARMState, exclusive_high), "exclusive_high");
104 static inline ARMMMUIdx get_a64_user_mem_index(DisasContext *s)
106 /* Return the mmu_idx to use for A64 "unprivileged load/store" insns:
107 * if EL1, access as if EL0; otherwise access at current EL
109 switch (s->mmu_idx) {
110 case ARMMMUIdx_S12NSE1:
111 return ARMMMUIdx_S12NSE0;
112 case ARMMMUIdx_S1SE1:
113 return ARMMMUIdx_S1SE0;
114 case ARMMMUIdx_S2NS:
115 g_assert_not_reached();
116 default:
117 return s->mmu_idx;
121 void aarch64_cpu_dump_state(CPUState *cs, FILE *f,
122 fprintf_function cpu_fprintf, int flags)
124 ARMCPU *cpu = ARM_CPU(cs);
125 CPUARMState *env = &cpu->env;
126 uint32_t psr = pstate_read(env);
127 int i;
128 int el = arm_current_el(env);
129 const char *ns_status;
131 cpu_fprintf(f, "PC=%016"PRIx64" SP=%016"PRIx64"\n",
132 env->pc, env->xregs[31]);
133 for (i = 0; i < 31; i++) {
134 cpu_fprintf(f, "X%02d=%016"PRIx64, i, env->xregs[i]);
135 if ((i % 4) == 3) {
136 cpu_fprintf(f, "\n");
137 } else {
138 cpu_fprintf(f, " ");
142 if (arm_feature(env, ARM_FEATURE_EL3) && el != 3) {
143 ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
144 } else {
145 ns_status = "";
148 cpu_fprintf(f, "\nPSTATE=%08x %c%c%c%c %sEL%d%c\n",
149 psr,
150 psr & PSTATE_N ? 'N' : '-',
151 psr & PSTATE_Z ? 'Z' : '-',
152 psr & PSTATE_C ? 'C' : '-',
153 psr & PSTATE_V ? 'V' : '-',
154 ns_status,
156 psr & PSTATE_SP ? 'h' : 't');
158 if (flags & CPU_DUMP_FPU) {
159 int numvfpregs = 32;
160 for (i = 0; i < numvfpregs; i += 2) {
161 uint64_t vlo = float64_val(env->vfp.regs[i * 2]);
162 uint64_t vhi = float64_val(env->vfp.regs[(i * 2) + 1]);
163 cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 " ",
164 i, vhi, vlo);
165 vlo = float64_val(env->vfp.regs[(i + 1) * 2]);
166 vhi = float64_val(env->vfp.regs[((i + 1) * 2) + 1]);
167 cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 "\n",
168 i + 1, vhi, vlo);
170 cpu_fprintf(f, "FPCR: %08x FPSR: %08x\n",
171 vfp_get_fpcr(env), vfp_get_fpsr(env));
175 void gen_a64_set_pc_im(uint64_t val)
177 tcg_gen_movi_i64(cpu_pc, val);
180 /* Load the PC from a generic TCG variable.
182 * If address tagging is enabled via the TCR TBI bits, then loading
183 * an address into the PC will clear out any tag in the it:
184 * + for EL2 and EL3 there is only one TBI bit, and if it is set
185 * then the address is zero-extended, clearing bits [63:56]
186 * + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
187 * and TBI1 controls addressses with bit 55 == 1.
188 * If the appropriate TBI bit is set for the address then
189 * the address is sign-extended from bit 55 into bits [63:56]
191 * We can avoid doing this for relative-branches, because the
192 * PC + offset can never overflow into the tag bits (assuming
193 * that virtual addresses are less than 56 bits wide, as they
194 * are currently), but we must handle it for branch-to-register.
196 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
199 if (s->current_el <= 1) {
200 /* Test if NEITHER or BOTH TBI values are set. If so, no need to
201 * examine bit 55 of address, can just generate code.
202 * If mixed, then test via generated code
204 if (s->tbi0 && s->tbi1) {
205 TCGv_i64 tmp_reg = tcg_temp_new_i64();
206 /* Both bits set, sign extension from bit 55 into [63:56] will
207 * cover both cases
209 tcg_gen_shli_i64(tmp_reg, src, 8);
210 tcg_gen_sari_i64(cpu_pc, tmp_reg, 8);
211 tcg_temp_free_i64(tmp_reg);
212 } else if (!s->tbi0 && !s->tbi1) {
213 /* Neither bit set, just load it as-is */
214 tcg_gen_mov_i64(cpu_pc, src);
215 } else {
216 TCGv_i64 tcg_tmpval = tcg_temp_new_i64();
217 TCGv_i64 tcg_bit55 = tcg_temp_new_i64();
218 TCGv_i64 tcg_zero = tcg_const_i64(0);
220 tcg_gen_andi_i64(tcg_bit55, src, (1ull << 55));
222 if (s->tbi0) {
223 /* tbi0==1, tbi1==0, so 0-fill upper byte if bit 55 = 0 */
224 tcg_gen_andi_i64(tcg_tmpval, src,
225 0x00FFFFFFFFFFFFFFull);
226 tcg_gen_movcond_i64(TCG_COND_EQ, cpu_pc, tcg_bit55, tcg_zero,
227 tcg_tmpval, src);
228 } else {
229 /* tbi0==0, tbi1==1, so 1-fill upper byte if bit 55 = 1 */
230 tcg_gen_ori_i64(tcg_tmpval, src,
231 0xFF00000000000000ull);
232 tcg_gen_movcond_i64(TCG_COND_NE, cpu_pc, tcg_bit55, tcg_zero,
233 tcg_tmpval, src);
235 tcg_temp_free_i64(tcg_zero);
236 tcg_temp_free_i64(tcg_bit55);
237 tcg_temp_free_i64(tcg_tmpval);
239 } else { /* EL > 1 */
240 if (s->tbi0) {
241 /* Force tag byte to all zero */
242 tcg_gen_andi_i64(cpu_pc, src, 0x00FFFFFFFFFFFFFFull);
243 } else {
244 /* Load unmodified address */
245 tcg_gen_mov_i64(cpu_pc, src);
250 typedef struct DisasCompare64 {
251 TCGCond cond;
252 TCGv_i64 value;
253 } DisasCompare64;
255 static void a64_test_cc(DisasCompare64 *c64, int cc)
257 DisasCompare c32;
259 arm_test_cc(&c32, cc);
261 /* Sign-extend the 32-bit value so that the GE/LT comparisons work
262 * properly. The NE/EQ comparisons are also fine with this choice. */
263 c64->cond = c32.cond;
264 c64->value = tcg_temp_new_i64();
265 tcg_gen_ext_i32_i64(c64->value, c32.value);
267 arm_free_cc(&c32);
270 static void a64_free_cc(DisasCompare64 *c64)
272 tcg_temp_free_i64(c64->value);
275 static void gen_exception_internal(int excp)
277 TCGv_i32 tcg_excp = tcg_const_i32(excp);
279 assert(excp_is_internal(excp));
280 gen_helper_exception_internal(cpu_env, tcg_excp);
281 tcg_temp_free_i32(tcg_excp);
284 static void gen_exception(int excp, uint32_t syndrome, uint32_t target_el)
286 TCGv_i32 tcg_excp = tcg_const_i32(excp);
287 TCGv_i32 tcg_syn = tcg_const_i32(syndrome);
288 TCGv_i32 tcg_el = tcg_const_i32(target_el);
290 gen_helper_exception_with_syndrome(cpu_env, tcg_excp,
291 tcg_syn, tcg_el);
292 tcg_temp_free_i32(tcg_el);
293 tcg_temp_free_i32(tcg_syn);
294 tcg_temp_free_i32(tcg_excp);
297 static void gen_exception_internal_insn(DisasContext *s, int offset, int excp)
299 gen_a64_set_pc_im(s->pc - offset);
300 gen_exception_internal(excp);
301 s->is_jmp = DISAS_EXC;
304 static void gen_exception_insn(DisasContext *s, int offset, int excp,
305 uint32_t syndrome, uint32_t target_el)
307 gen_a64_set_pc_im(s->pc - offset);
308 gen_exception(excp, syndrome, target_el);
309 s->is_jmp = DISAS_EXC;
312 static void gen_ss_advance(DisasContext *s)
314 /* If the singlestep state is Active-not-pending, advance to
315 * Active-pending.
317 if (s->ss_active) {
318 s->pstate_ss = 0;
319 gen_helper_clear_pstate_ss(cpu_env);
323 static void gen_step_complete_exception(DisasContext *s)
325 /* We just completed step of an insn. Move from Active-not-pending
326 * to Active-pending, and then also take the swstep exception.
327 * This corresponds to making the (IMPDEF) choice to prioritize
328 * swstep exceptions over asynchronous exceptions taken to an exception
329 * level where debug is disabled. This choice has the advantage that
330 * we do not need to maintain internal state corresponding to the
331 * ISV/EX syndrome bits between completion of the step and generation
332 * of the exception, and our syndrome information is always correct.
334 gen_ss_advance(s);
335 gen_exception(EXCP_UDEF, syn_swstep(s->ss_same_el, 1, s->is_ldex),
336 default_exception_el(s));
337 s->is_jmp = DISAS_EXC;
340 static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
342 /* No direct tb linking with singlestep (either QEMU's or the ARM
343 * debug architecture kind) or deterministic io
345 if (s->singlestep_enabled || s->ss_active || (s->tb->cflags & CF_LAST_IO)) {
346 return false;
349 #ifndef CONFIG_USER_ONLY
350 /* Only link tbs from inside the same guest page */
351 if ((s->tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
352 return false;
354 #endif
356 return true;
359 static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
361 TranslationBlock *tb;
363 tb = s->tb;
364 if (use_goto_tb(s, n, dest)) {
365 tcg_gen_goto_tb(n);
366 gen_a64_set_pc_im(dest);
367 tcg_gen_exit_tb((intptr_t)tb + n);
368 s->is_jmp = DISAS_TB_JUMP;
369 } else {
370 gen_a64_set_pc_im(dest);
371 if (s->ss_active) {
372 gen_step_complete_exception(s);
373 } else if (s->singlestep_enabled) {
374 gen_exception_internal(EXCP_DEBUG);
375 } else {
376 tcg_gen_exit_tb(0);
377 s->is_jmp = DISAS_TB_JUMP;
382 static void unallocated_encoding(DisasContext *s)
384 /* Unallocated and reserved encodings are uncategorized */
385 gen_exception_insn(s, 4, EXCP_UDEF, syn_uncategorized(),
386 default_exception_el(s));
389 #define unsupported_encoding(s, insn) \
390 do { \
391 qemu_log_mask(LOG_UNIMP, \
392 "%s:%d: unsupported instruction encoding 0x%08x " \
393 "at pc=%016" PRIx64 "\n", \
394 __FILE__, __LINE__, insn, s->pc - 4); \
395 unallocated_encoding(s); \
396 } while (0);
398 static void init_tmp_a64_array(DisasContext *s)
400 #ifdef CONFIG_DEBUG_TCG
401 int i;
402 for (i = 0; i < ARRAY_SIZE(s->tmp_a64); i++) {
403 TCGV_UNUSED_I64(s->tmp_a64[i]);
405 #endif
406 s->tmp_a64_count = 0;
409 static void free_tmp_a64(DisasContext *s)
411 int i;
412 for (i = 0; i < s->tmp_a64_count; i++) {
413 tcg_temp_free_i64(s->tmp_a64[i]);
415 init_tmp_a64_array(s);
418 static TCGv_i64 new_tmp_a64(DisasContext *s)
420 assert(s->tmp_a64_count < TMP_A64_MAX);
421 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
424 static TCGv_i64 new_tmp_a64_zero(DisasContext *s)
426 TCGv_i64 t = new_tmp_a64(s);
427 tcg_gen_movi_i64(t, 0);
428 return t;
432 * Register access functions
434 * These functions are used for directly accessing a register in where
435 * changes to the final register value are likely to be made. If you
436 * need to use a register for temporary calculation (e.g. index type
437 * operations) use the read_* form.
439 * B1.2.1 Register mappings
441 * In instruction register encoding 31 can refer to ZR (zero register) or
442 * the SP (stack pointer) depending on context. In QEMU's case we map SP
443 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
444 * This is the point of the _sp forms.
446 static TCGv_i64 cpu_reg(DisasContext *s, int reg)
448 if (reg == 31) {
449 return new_tmp_a64_zero(s);
450 } else {
451 return cpu_X[reg];
455 /* register access for when 31 == SP */
456 static TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
458 return cpu_X[reg];
461 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
462 * representing the register contents. This TCGv is an auto-freed
463 * temporary so it need not be explicitly freed, and may be modified.
465 static TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
467 TCGv_i64 v = new_tmp_a64(s);
468 if (reg != 31) {
469 if (sf) {
470 tcg_gen_mov_i64(v, cpu_X[reg]);
471 } else {
472 tcg_gen_ext32u_i64(v, cpu_X[reg]);
474 } else {
475 tcg_gen_movi_i64(v, 0);
477 return v;
480 static TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
482 TCGv_i64 v = new_tmp_a64(s);
483 if (sf) {
484 tcg_gen_mov_i64(v, cpu_X[reg]);
485 } else {
486 tcg_gen_ext32u_i64(v, cpu_X[reg]);
488 return v;
491 /* We should have at some point before trying to access an FP register
492 * done the necessary access check, so assert that
493 * (a) we did the check and
494 * (b) we didn't then just plough ahead anyway if it failed.
495 * Print the instruction pattern in the abort message so we can figure
496 * out what we need to fix if a user encounters this problem in the wild.
498 static inline void assert_fp_access_checked(DisasContext *s)
500 #ifdef CONFIG_DEBUG_TCG
501 if (unlikely(!s->fp_access_checked || s->fp_excp_el)) {
502 fprintf(stderr, "target-arm: FP access check missing for "
503 "instruction 0x%08x\n", s->insn);
504 abort();
506 #endif
509 /* Return the offset into CPUARMState of an element of specified
510 * size, 'element' places in from the least significant end of
511 * the FP/vector register Qn.
513 static inline int vec_reg_offset(DisasContext *s, int regno,
514 int element, TCGMemOp size)
516 int offs = 0;
517 #ifdef HOST_WORDS_BIGENDIAN
518 /* This is complicated slightly because vfp.regs[2n] is
519 * still the low half and vfp.regs[2n+1] the high half
520 * of the 128 bit vector, even on big endian systems.
521 * Calculate the offset assuming a fully bigendian 128 bits,
522 * then XOR to account for the order of the two 64 bit halves.
524 offs += (16 - ((element + 1) * (1 << size)));
525 offs ^= 8;
526 #else
527 offs += element * (1 << size);
528 #endif
529 offs += offsetof(CPUARMState, vfp.regs[regno * 2]);
530 assert_fp_access_checked(s);
531 return offs;
534 /* Return the offset into CPUARMState of a slice (from
535 * the least significant end) of FP register Qn (ie
536 * Dn, Sn, Hn or Bn).
537 * (Note that this is not the same mapping as for A32; see cpu.h)
539 static inline int fp_reg_offset(DisasContext *s, int regno, TCGMemOp size)
541 int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
542 #ifdef HOST_WORDS_BIGENDIAN
543 offs += (8 - (1 << size));
544 #endif
545 assert_fp_access_checked(s);
546 return offs;
549 /* Offset of the high half of the 128 bit vector Qn */
550 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
552 assert_fp_access_checked(s);
553 return offsetof(CPUARMState, vfp.regs[regno * 2 + 1]);
556 /* Convenience accessors for reading and writing single and double
557 * FP registers. Writing clears the upper parts of the associated
558 * 128 bit vector register, as required by the architecture.
559 * Note that unlike the GP register accessors, the values returned
560 * by the read functions must be manually freed.
562 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
564 TCGv_i64 v = tcg_temp_new_i64();
566 tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
567 return v;
570 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
572 TCGv_i32 v = tcg_temp_new_i32();
574 tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
575 return v;
578 static void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
580 TCGv_i64 tcg_zero = tcg_const_i64(0);
582 tcg_gen_st_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
583 tcg_gen_st_i64(tcg_zero, cpu_env, fp_reg_hi_offset(s, reg));
584 tcg_temp_free_i64(tcg_zero);
587 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
589 TCGv_i64 tmp = tcg_temp_new_i64();
591 tcg_gen_extu_i32_i64(tmp, v);
592 write_fp_dreg(s, reg, tmp);
593 tcg_temp_free_i64(tmp);
596 static TCGv_ptr get_fpstatus_ptr(void)
598 TCGv_ptr statusptr = tcg_temp_new_ptr();
599 int offset;
601 /* In A64 all instructions (both FP and Neon) use the FPCR;
602 * there is no equivalent of the A32 Neon "standard FPSCR value"
603 * and all operations use vfp.fp_status.
605 offset = offsetof(CPUARMState, vfp.fp_status);
606 tcg_gen_addi_ptr(statusptr, cpu_env, offset);
607 return statusptr;
610 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
611 * than the 32 bit equivalent.
613 static inline void gen_set_NZ64(TCGv_i64 result)
615 tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
616 tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
619 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
620 static inline void gen_logic_CC(int sf, TCGv_i64 result)
622 if (sf) {
623 gen_set_NZ64(result);
624 } else {
625 tcg_gen_extrl_i64_i32(cpu_ZF, result);
626 tcg_gen_mov_i32(cpu_NF, cpu_ZF);
628 tcg_gen_movi_i32(cpu_CF, 0);
629 tcg_gen_movi_i32(cpu_VF, 0);
632 /* dest = T0 + T1; compute C, N, V and Z flags */
633 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
635 if (sf) {
636 TCGv_i64 result, flag, tmp;
637 result = tcg_temp_new_i64();
638 flag = tcg_temp_new_i64();
639 tmp = tcg_temp_new_i64();
641 tcg_gen_movi_i64(tmp, 0);
642 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
644 tcg_gen_extrl_i64_i32(cpu_CF, flag);
646 gen_set_NZ64(result);
648 tcg_gen_xor_i64(flag, result, t0);
649 tcg_gen_xor_i64(tmp, t0, t1);
650 tcg_gen_andc_i64(flag, flag, tmp);
651 tcg_temp_free_i64(tmp);
652 tcg_gen_extrh_i64_i32(cpu_VF, flag);
654 tcg_gen_mov_i64(dest, result);
655 tcg_temp_free_i64(result);
656 tcg_temp_free_i64(flag);
657 } else {
658 /* 32 bit arithmetic */
659 TCGv_i32 t0_32 = tcg_temp_new_i32();
660 TCGv_i32 t1_32 = tcg_temp_new_i32();
661 TCGv_i32 tmp = tcg_temp_new_i32();
663 tcg_gen_movi_i32(tmp, 0);
664 tcg_gen_extrl_i64_i32(t0_32, t0);
665 tcg_gen_extrl_i64_i32(t1_32, t1);
666 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
667 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
668 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
669 tcg_gen_xor_i32(tmp, t0_32, t1_32);
670 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
671 tcg_gen_extu_i32_i64(dest, cpu_NF);
673 tcg_temp_free_i32(tmp);
674 tcg_temp_free_i32(t0_32);
675 tcg_temp_free_i32(t1_32);
679 /* dest = T0 - T1; compute C, N, V and Z flags */
680 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
682 if (sf) {
683 /* 64 bit arithmetic */
684 TCGv_i64 result, flag, tmp;
686 result = tcg_temp_new_i64();
687 flag = tcg_temp_new_i64();
688 tcg_gen_sub_i64(result, t0, t1);
690 gen_set_NZ64(result);
692 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
693 tcg_gen_extrl_i64_i32(cpu_CF, flag);
695 tcg_gen_xor_i64(flag, result, t0);
696 tmp = tcg_temp_new_i64();
697 tcg_gen_xor_i64(tmp, t0, t1);
698 tcg_gen_and_i64(flag, flag, tmp);
699 tcg_temp_free_i64(tmp);
700 tcg_gen_extrh_i64_i32(cpu_VF, flag);
701 tcg_gen_mov_i64(dest, result);
702 tcg_temp_free_i64(flag);
703 tcg_temp_free_i64(result);
704 } else {
705 /* 32 bit arithmetic */
706 TCGv_i32 t0_32 = tcg_temp_new_i32();
707 TCGv_i32 t1_32 = tcg_temp_new_i32();
708 TCGv_i32 tmp;
710 tcg_gen_extrl_i64_i32(t0_32, t0);
711 tcg_gen_extrl_i64_i32(t1_32, t1);
712 tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
713 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
714 tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
715 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
716 tmp = tcg_temp_new_i32();
717 tcg_gen_xor_i32(tmp, t0_32, t1_32);
718 tcg_temp_free_i32(t0_32);
719 tcg_temp_free_i32(t1_32);
720 tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
721 tcg_temp_free_i32(tmp);
722 tcg_gen_extu_i32_i64(dest, cpu_NF);
726 /* dest = T0 + T1 + CF; do not compute flags. */
727 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
729 TCGv_i64 flag = tcg_temp_new_i64();
730 tcg_gen_extu_i32_i64(flag, cpu_CF);
731 tcg_gen_add_i64(dest, t0, t1);
732 tcg_gen_add_i64(dest, dest, flag);
733 tcg_temp_free_i64(flag);
735 if (!sf) {
736 tcg_gen_ext32u_i64(dest, dest);
740 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
741 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
743 if (sf) {
744 TCGv_i64 result, cf_64, vf_64, tmp;
745 result = tcg_temp_new_i64();
746 cf_64 = tcg_temp_new_i64();
747 vf_64 = tcg_temp_new_i64();
748 tmp = tcg_const_i64(0);
750 tcg_gen_extu_i32_i64(cf_64, cpu_CF);
751 tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
752 tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
753 tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
754 gen_set_NZ64(result);
756 tcg_gen_xor_i64(vf_64, result, t0);
757 tcg_gen_xor_i64(tmp, t0, t1);
758 tcg_gen_andc_i64(vf_64, vf_64, tmp);
759 tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
761 tcg_gen_mov_i64(dest, result);
763 tcg_temp_free_i64(tmp);
764 tcg_temp_free_i64(vf_64);
765 tcg_temp_free_i64(cf_64);
766 tcg_temp_free_i64(result);
767 } else {
768 TCGv_i32 t0_32, t1_32, tmp;
769 t0_32 = tcg_temp_new_i32();
770 t1_32 = tcg_temp_new_i32();
771 tmp = tcg_const_i32(0);
773 tcg_gen_extrl_i64_i32(t0_32, t0);
774 tcg_gen_extrl_i64_i32(t1_32, t1);
775 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
776 tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
778 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
779 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
780 tcg_gen_xor_i32(tmp, t0_32, t1_32);
781 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
782 tcg_gen_extu_i32_i64(dest, cpu_NF);
784 tcg_temp_free_i32(tmp);
785 tcg_temp_free_i32(t1_32);
786 tcg_temp_free_i32(t0_32);
791 * Load/Store generators
795 * Store from GPR register to memory.
797 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
798 TCGv_i64 tcg_addr, int size, int memidx,
799 bool iss_valid,
800 unsigned int iss_srt,
801 bool iss_sf, bool iss_ar)
803 g_assert(size <= 3);
804 tcg_gen_qemu_st_i64(source, tcg_addr, memidx, s->be_data + size);
806 if (iss_valid) {
807 uint32_t syn;
809 syn = syn_data_abort_with_iss(0,
810 size,
811 false,
812 iss_srt,
813 iss_sf,
814 iss_ar,
815 0, 0, 0, 0, 0, false);
816 disas_set_insn_syndrome(s, syn);
820 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
821 TCGv_i64 tcg_addr, int size,
822 bool iss_valid,
823 unsigned int iss_srt,
824 bool iss_sf, bool iss_ar)
826 do_gpr_st_memidx(s, source, tcg_addr, size, get_mem_index(s),
827 iss_valid, iss_srt, iss_sf, iss_ar);
831 * Load from memory to GPR register
833 static void do_gpr_ld_memidx(DisasContext *s,
834 TCGv_i64 dest, TCGv_i64 tcg_addr,
835 int size, bool is_signed,
836 bool extend, int memidx,
837 bool iss_valid, unsigned int iss_srt,
838 bool iss_sf, bool iss_ar)
840 TCGMemOp memop = s->be_data + size;
842 g_assert(size <= 3);
844 if (is_signed) {
845 memop += MO_SIGN;
848 tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
850 if (extend && is_signed) {
851 g_assert(size < 3);
852 tcg_gen_ext32u_i64(dest, dest);
855 if (iss_valid) {
856 uint32_t syn;
858 syn = syn_data_abort_with_iss(0,
859 size,
860 is_signed,
861 iss_srt,
862 iss_sf,
863 iss_ar,
864 0, 0, 0, 0, 0, false);
865 disas_set_insn_syndrome(s, syn);
869 static void do_gpr_ld(DisasContext *s,
870 TCGv_i64 dest, TCGv_i64 tcg_addr,
871 int size, bool is_signed, bool extend,
872 bool iss_valid, unsigned int iss_srt,
873 bool iss_sf, bool iss_ar)
875 do_gpr_ld_memidx(s, dest, tcg_addr, size, is_signed, extend,
876 get_mem_index(s),
877 iss_valid, iss_srt, iss_sf, iss_ar);
881 * Store from FP register to memory
883 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
885 /* This writes the bottom N bits of a 128 bit wide vector to memory */
886 TCGv_i64 tmp = tcg_temp_new_i64();
887 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_offset(s, srcidx, MO_64));
888 if (size < 4) {
889 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s),
890 s->be_data + size);
891 } else {
892 bool be = s->be_data == MO_BE;
893 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
895 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
896 tcg_gen_qemu_st_i64(tmp, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
897 s->be_data | MO_Q);
898 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_hi_offset(s, srcidx));
899 tcg_gen_qemu_st_i64(tmp, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
900 s->be_data | MO_Q);
901 tcg_temp_free_i64(tcg_hiaddr);
904 tcg_temp_free_i64(tmp);
908 * Load from memory to FP register
910 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
912 /* This always zero-extends and writes to a full 128 bit wide vector */
913 TCGv_i64 tmplo = tcg_temp_new_i64();
914 TCGv_i64 tmphi;
916 if (size < 4) {
917 TCGMemOp memop = s->be_data + size;
918 tmphi = tcg_const_i64(0);
919 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
920 } else {
921 bool be = s->be_data == MO_BE;
922 TCGv_i64 tcg_hiaddr;
924 tmphi = tcg_temp_new_i64();
925 tcg_hiaddr = tcg_temp_new_i64();
927 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
928 tcg_gen_qemu_ld_i64(tmplo, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
929 s->be_data | MO_Q);
930 tcg_gen_qemu_ld_i64(tmphi, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
931 s->be_data | MO_Q);
932 tcg_temp_free_i64(tcg_hiaddr);
935 tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
936 tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
938 tcg_temp_free_i64(tmplo);
939 tcg_temp_free_i64(tmphi);
943 * Vector load/store helpers.
945 * The principal difference between this and a FP load is that we don't
946 * zero extend as we are filling a partial chunk of the vector register.
947 * These functions don't support 128 bit loads/stores, which would be
948 * normal load/store operations.
950 * The _i32 versions are useful when operating on 32 bit quantities
951 * (eg for floating point single or using Neon helper functions).
954 /* Get value of an element within a vector register */
955 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
956 int element, TCGMemOp memop)
958 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
959 switch (memop) {
960 case MO_8:
961 tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
962 break;
963 case MO_16:
964 tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
965 break;
966 case MO_32:
967 tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
968 break;
969 case MO_8|MO_SIGN:
970 tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
971 break;
972 case MO_16|MO_SIGN:
973 tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
974 break;
975 case MO_32|MO_SIGN:
976 tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
977 break;
978 case MO_64:
979 case MO_64|MO_SIGN:
980 tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
981 break;
982 default:
983 g_assert_not_reached();
987 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
988 int element, TCGMemOp memop)
990 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
991 switch (memop) {
992 case MO_8:
993 tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
994 break;
995 case MO_16:
996 tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
997 break;
998 case MO_8|MO_SIGN:
999 tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1000 break;
1001 case MO_16|MO_SIGN:
1002 tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1003 break;
1004 case MO_32:
1005 case MO_32|MO_SIGN:
1006 tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1007 break;
1008 default:
1009 g_assert_not_reached();
1013 /* Set value of an element within a vector register */
1014 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1015 int element, TCGMemOp memop)
1017 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1018 switch (memop) {
1019 case MO_8:
1020 tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1021 break;
1022 case MO_16:
1023 tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1024 break;
1025 case MO_32:
1026 tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1027 break;
1028 case MO_64:
1029 tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1030 break;
1031 default:
1032 g_assert_not_reached();
1036 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1037 int destidx, int element, TCGMemOp memop)
1039 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1040 switch (memop) {
1041 case MO_8:
1042 tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1043 break;
1044 case MO_16:
1045 tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1046 break;
1047 case MO_32:
1048 tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1049 break;
1050 default:
1051 g_assert_not_reached();
1055 /* Clear the high 64 bits of a 128 bit vector (in general non-quad
1056 * vector ops all need to do this).
1058 static void clear_vec_high(DisasContext *s, int rd)
1060 TCGv_i64 tcg_zero = tcg_const_i64(0);
1062 write_vec_element(s, tcg_zero, rd, 1, MO_64);
1063 tcg_temp_free_i64(tcg_zero);
1066 /* Store from vector register to memory */
1067 static void do_vec_st(DisasContext *s, int srcidx, int element,
1068 TCGv_i64 tcg_addr, int size)
1070 TCGMemOp memop = s->be_data + size;
1071 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1073 read_vec_element(s, tcg_tmp, srcidx, element, size);
1074 tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
1076 tcg_temp_free_i64(tcg_tmp);
1079 /* Load from memory to vector register */
1080 static void do_vec_ld(DisasContext *s, int destidx, int element,
1081 TCGv_i64 tcg_addr, int size)
1083 TCGMemOp memop = s->be_data + size;
1084 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1086 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
1087 write_vec_element(s, tcg_tmp, destidx, element, size);
1089 tcg_temp_free_i64(tcg_tmp);
1092 /* Check that FP/Neon access is enabled. If it is, return
1093 * true. If not, emit code to generate an appropriate exception,
1094 * and return false; the caller should not emit any code for
1095 * the instruction. Note that this check must happen after all
1096 * unallocated-encoding checks (otherwise the syndrome information
1097 * for the resulting exception will be incorrect).
1099 static inline bool fp_access_check(DisasContext *s)
1101 assert(!s->fp_access_checked);
1102 s->fp_access_checked = true;
1104 if (!s->fp_excp_el) {
1105 return true;
1108 gen_exception_insn(s, 4, EXCP_UDEF, syn_fp_access_trap(1, 0xe, false),
1109 s->fp_excp_el);
1110 return false;
1114 * This utility function is for doing register extension with an
1115 * optional shift. You will likely want to pass a temporary for the
1116 * destination register. See DecodeRegExtend() in the ARM ARM.
1118 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1119 int option, unsigned int shift)
1121 int extsize = extract32(option, 0, 2);
1122 bool is_signed = extract32(option, 2, 1);
1124 if (is_signed) {
1125 switch (extsize) {
1126 case 0:
1127 tcg_gen_ext8s_i64(tcg_out, tcg_in);
1128 break;
1129 case 1:
1130 tcg_gen_ext16s_i64(tcg_out, tcg_in);
1131 break;
1132 case 2:
1133 tcg_gen_ext32s_i64(tcg_out, tcg_in);
1134 break;
1135 case 3:
1136 tcg_gen_mov_i64(tcg_out, tcg_in);
1137 break;
1139 } else {
1140 switch (extsize) {
1141 case 0:
1142 tcg_gen_ext8u_i64(tcg_out, tcg_in);
1143 break;
1144 case 1:
1145 tcg_gen_ext16u_i64(tcg_out, tcg_in);
1146 break;
1147 case 2:
1148 tcg_gen_ext32u_i64(tcg_out, tcg_in);
1149 break;
1150 case 3:
1151 tcg_gen_mov_i64(tcg_out, tcg_in);
1152 break;
1156 if (shift) {
1157 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1161 static inline void gen_check_sp_alignment(DisasContext *s)
1163 /* The AArch64 architecture mandates that (if enabled via PSTATE
1164 * or SCTLR bits) there is a check that SP is 16-aligned on every
1165 * SP-relative load or store (with an exception generated if it is not).
1166 * In line with general QEMU practice regarding misaligned accesses,
1167 * we omit these checks for the sake of guest program performance.
1168 * This function is provided as a hook so we can more easily add these
1169 * checks in future (possibly as a "favour catching guest program bugs
1170 * over speed" user selectable option).
1175 * This provides a simple table based table lookup decoder. It is
1176 * intended to be used when the relevant bits for decode are too
1177 * awkwardly placed and switch/if based logic would be confusing and
1178 * deeply nested. Since it's a linear search through the table, tables
1179 * should be kept small.
1181 * It returns the first handler where insn & mask == pattern, or
1182 * NULL if there is no match.
1183 * The table is terminated by an empty mask (i.e. 0)
1185 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1186 uint32_t insn)
1188 const AArch64DecodeTable *tptr = table;
1190 while (tptr->mask) {
1191 if ((insn & tptr->mask) == tptr->pattern) {
1192 return tptr->disas_fn;
1194 tptr++;
1196 return NULL;
1200 * the instruction disassembly implemented here matches
1201 * the instruction encoding classifications in chapter 3 (C3)
1202 * of the ARM Architecture Reference Manual (DDI0487A_a)
1205 /* C3.2.7 Unconditional branch (immediate)
1206 * 31 30 26 25 0
1207 * +----+-----------+-------------------------------------+
1208 * | op | 0 0 1 0 1 | imm26 |
1209 * +----+-----------+-------------------------------------+
1211 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
1213 uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
1215 if (insn & (1U << 31)) {
1216 /* C5.6.26 BL Branch with link */
1217 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1220 /* C5.6.20 B Branch / C5.6.26 BL Branch with link */
1221 gen_goto_tb(s, 0, addr);
1224 /* C3.2.1 Compare & branch (immediate)
1225 * 31 30 25 24 23 5 4 0
1226 * +----+-------------+----+---------------------+--------+
1227 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
1228 * +----+-------------+----+---------------------+--------+
1230 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
1232 unsigned int sf, op, rt;
1233 uint64_t addr;
1234 TCGLabel *label_match;
1235 TCGv_i64 tcg_cmp;
1237 sf = extract32(insn, 31, 1);
1238 op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
1239 rt = extract32(insn, 0, 5);
1240 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1242 tcg_cmp = read_cpu_reg(s, rt, sf);
1243 label_match = gen_new_label();
1245 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1246 tcg_cmp, 0, label_match);
1248 gen_goto_tb(s, 0, s->pc);
1249 gen_set_label(label_match);
1250 gen_goto_tb(s, 1, addr);
1253 /* C3.2.5 Test & branch (immediate)
1254 * 31 30 25 24 23 19 18 5 4 0
1255 * +----+-------------+----+-------+-------------+------+
1256 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
1257 * +----+-------------+----+-------+-------------+------+
1259 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1261 unsigned int bit_pos, op, rt;
1262 uint64_t addr;
1263 TCGLabel *label_match;
1264 TCGv_i64 tcg_cmp;
1266 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1267 op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1268 addr = s->pc + sextract32(insn, 5, 14) * 4 - 4;
1269 rt = extract32(insn, 0, 5);
1271 tcg_cmp = tcg_temp_new_i64();
1272 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1273 label_match = gen_new_label();
1274 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1275 tcg_cmp, 0, label_match);
1276 tcg_temp_free_i64(tcg_cmp);
1277 gen_goto_tb(s, 0, s->pc);
1278 gen_set_label(label_match);
1279 gen_goto_tb(s, 1, addr);
1282 /* C3.2.2 / C5.6.19 Conditional branch (immediate)
1283 * 31 25 24 23 5 4 3 0
1284 * +---------------+----+---------------------+----+------+
1285 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
1286 * +---------------+----+---------------------+----+------+
1288 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1290 unsigned int cond;
1291 uint64_t addr;
1293 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1294 unallocated_encoding(s);
1295 return;
1297 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1298 cond = extract32(insn, 0, 4);
1300 if (cond < 0x0e) {
1301 /* genuinely conditional branches */
1302 TCGLabel *label_match = gen_new_label();
1303 arm_gen_test_cc(cond, label_match);
1304 gen_goto_tb(s, 0, s->pc);
1305 gen_set_label(label_match);
1306 gen_goto_tb(s, 1, addr);
1307 } else {
1308 /* 0xe and 0xf are both "always" conditions */
1309 gen_goto_tb(s, 0, addr);
1313 /* C5.6.68 HINT */
1314 static void handle_hint(DisasContext *s, uint32_t insn,
1315 unsigned int op1, unsigned int op2, unsigned int crm)
1317 unsigned int selector = crm << 3 | op2;
1319 if (op1 != 3) {
1320 unallocated_encoding(s);
1321 return;
1324 switch (selector) {
1325 case 0: /* NOP */
1326 return;
1327 case 3: /* WFI */
1328 s->is_jmp = DISAS_WFI;
1329 return;
1330 case 1: /* YIELD */
1331 if (!parallel_cpus) {
1332 s->is_jmp = DISAS_YIELD;
1334 return;
1335 case 2: /* WFE */
1336 if (!parallel_cpus) {
1337 s->is_jmp = DISAS_WFE;
1339 return;
1340 case 4: /* SEV */
1341 case 5: /* SEVL */
1342 /* we treat all as NOP at least for now */
1343 return;
1344 default:
1345 /* default specified as NOP equivalent */
1346 return;
1350 static void gen_clrex(DisasContext *s, uint32_t insn)
1352 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1355 /* CLREX, DSB, DMB, ISB */
1356 static void handle_sync(DisasContext *s, uint32_t insn,
1357 unsigned int op1, unsigned int op2, unsigned int crm)
1359 TCGBar bar;
1361 if (op1 != 3) {
1362 unallocated_encoding(s);
1363 return;
1366 switch (op2) {
1367 case 2: /* CLREX */
1368 gen_clrex(s, insn);
1369 return;
1370 case 4: /* DSB */
1371 case 5: /* DMB */
1372 switch (crm & 3) {
1373 case 1: /* MBReqTypes_Reads */
1374 bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1375 break;
1376 case 2: /* MBReqTypes_Writes */
1377 bar = TCG_BAR_SC | TCG_MO_ST_ST;
1378 break;
1379 default: /* MBReqTypes_All */
1380 bar = TCG_BAR_SC | TCG_MO_ALL;
1381 break;
1383 tcg_gen_mb(bar);
1384 return;
1385 case 6: /* ISB */
1386 /* We need to break the TB after this insn to execute
1387 * a self-modified code correctly and also to take
1388 * any pending interrupts immediately.
1390 s->is_jmp = DISAS_UPDATE;
1391 return;
1392 default:
1393 unallocated_encoding(s);
1394 return;
1398 /* C5.6.130 MSR (immediate) - move immediate to processor state field */
1399 static void handle_msr_i(DisasContext *s, uint32_t insn,
1400 unsigned int op1, unsigned int op2, unsigned int crm)
1402 int op = op1 << 3 | op2;
1403 switch (op) {
1404 case 0x05: /* SPSel */
1405 if (s->current_el == 0) {
1406 unallocated_encoding(s);
1407 return;
1409 /* fall through */
1410 case 0x1e: /* DAIFSet */
1411 case 0x1f: /* DAIFClear */
1413 TCGv_i32 tcg_imm = tcg_const_i32(crm);
1414 TCGv_i32 tcg_op = tcg_const_i32(op);
1415 gen_a64_set_pc_im(s->pc - 4);
1416 gen_helper_msr_i_pstate(cpu_env, tcg_op, tcg_imm);
1417 tcg_temp_free_i32(tcg_imm);
1418 tcg_temp_free_i32(tcg_op);
1419 s->is_jmp = DISAS_UPDATE;
1420 break;
1422 default:
1423 unallocated_encoding(s);
1424 return;
1428 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1430 TCGv_i32 tmp = tcg_temp_new_i32();
1431 TCGv_i32 nzcv = tcg_temp_new_i32();
1433 /* build bit 31, N */
1434 tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
1435 /* build bit 30, Z */
1436 tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1437 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1438 /* build bit 29, C */
1439 tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1440 /* build bit 28, V */
1441 tcg_gen_shri_i32(tmp, cpu_VF, 31);
1442 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1443 /* generate result */
1444 tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1446 tcg_temp_free_i32(nzcv);
1447 tcg_temp_free_i32(tmp);
1450 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1453 TCGv_i32 nzcv = tcg_temp_new_i32();
1455 /* take NZCV from R[t] */
1456 tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
1458 /* bit 31, N */
1459 tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
1460 /* bit 30, Z */
1461 tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1462 tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1463 /* bit 29, C */
1464 tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1465 tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1466 /* bit 28, V */
1467 tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1468 tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1469 tcg_temp_free_i32(nzcv);
1472 /* C5.6.129 MRS - move from system register
1473 * C5.6.131 MSR (register) - move to system register
1474 * C5.6.204 SYS
1475 * C5.6.205 SYSL
1476 * These are all essentially the same insn in 'read' and 'write'
1477 * versions, with varying op0 fields.
1479 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1480 unsigned int op0, unsigned int op1, unsigned int op2,
1481 unsigned int crn, unsigned int crm, unsigned int rt)
1483 const ARMCPRegInfo *ri;
1484 TCGv_i64 tcg_rt;
1486 ri = get_arm_cp_reginfo(s->cp_regs,
1487 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1488 crn, crm, op0, op1, op2));
1490 if (!ri) {
1491 /* Unknown register; this might be a guest error or a QEMU
1492 * unimplemented feature.
1494 qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1495 "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1496 isread ? "read" : "write", op0, op1, crn, crm, op2);
1497 unallocated_encoding(s);
1498 return;
1501 /* Check access permissions */
1502 if (!cp_access_ok(s->current_el, ri, isread)) {
1503 unallocated_encoding(s);
1504 return;
1507 if (ri->accessfn) {
1508 /* Emit code to perform further access permissions checks at
1509 * runtime; this may result in an exception.
1511 TCGv_ptr tmpptr;
1512 TCGv_i32 tcg_syn, tcg_isread;
1513 uint32_t syndrome;
1515 gen_a64_set_pc_im(s->pc - 4);
1516 tmpptr = tcg_const_ptr(ri);
1517 syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1518 tcg_syn = tcg_const_i32(syndrome);
1519 tcg_isread = tcg_const_i32(isread);
1520 gen_helper_access_check_cp_reg(cpu_env, tmpptr, tcg_syn, tcg_isread);
1521 tcg_temp_free_ptr(tmpptr);
1522 tcg_temp_free_i32(tcg_syn);
1523 tcg_temp_free_i32(tcg_isread);
1526 /* Handle special cases first */
1527 switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
1528 case ARM_CP_NOP:
1529 return;
1530 case ARM_CP_NZCV:
1531 tcg_rt = cpu_reg(s, rt);
1532 if (isread) {
1533 gen_get_nzcv(tcg_rt);
1534 } else {
1535 gen_set_nzcv(tcg_rt);
1537 return;
1538 case ARM_CP_CURRENTEL:
1539 /* Reads as current EL value from pstate, which is
1540 * guaranteed to be constant by the tb flags.
1542 tcg_rt = cpu_reg(s, rt);
1543 tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
1544 return;
1545 case ARM_CP_DC_ZVA:
1546 /* Writes clear the aligned block of memory which rt points into. */
1547 tcg_rt = cpu_reg(s, rt);
1548 gen_helper_dc_zva(cpu_env, tcg_rt);
1549 return;
1550 default:
1551 break;
1554 if ((s->tb->cflags & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1555 gen_io_start();
1558 tcg_rt = cpu_reg(s, rt);
1560 if (isread) {
1561 if (ri->type & ARM_CP_CONST) {
1562 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1563 } else if (ri->readfn) {
1564 TCGv_ptr tmpptr;
1565 tmpptr = tcg_const_ptr(ri);
1566 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1567 tcg_temp_free_ptr(tmpptr);
1568 } else {
1569 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1571 } else {
1572 if (ri->type & ARM_CP_CONST) {
1573 /* If not forbidden by access permissions, treat as WI */
1574 return;
1575 } else if (ri->writefn) {
1576 TCGv_ptr tmpptr;
1577 tmpptr = tcg_const_ptr(ri);
1578 gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1579 tcg_temp_free_ptr(tmpptr);
1580 } else {
1581 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
1585 if ((s->tb->cflags & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1586 /* I/O operations must end the TB here (whether read or write) */
1587 gen_io_end();
1588 s->is_jmp = DISAS_UPDATE;
1589 } else if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
1590 /* We default to ending the TB on a coprocessor register write,
1591 * but allow this to be suppressed by the register definition
1592 * (usually only necessary to work around guest bugs).
1594 s->is_jmp = DISAS_UPDATE;
1598 /* C3.2.4 System
1599 * 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0
1600 * +---------------------+---+-----+-----+-------+-------+-----+------+
1601 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 | CRn | CRm | op2 | Rt |
1602 * +---------------------+---+-----+-----+-------+-------+-----+------+
1604 static void disas_system(DisasContext *s, uint32_t insn)
1606 unsigned int l, op0, op1, crn, crm, op2, rt;
1607 l = extract32(insn, 21, 1);
1608 op0 = extract32(insn, 19, 2);
1609 op1 = extract32(insn, 16, 3);
1610 crn = extract32(insn, 12, 4);
1611 crm = extract32(insn, 8, 4);
1612 op2 = extract32(insn, 5, 3);
1613 rt = extract32(insn, 0, 5);
1615 if (op0 == 0) {
1616 if (l || rt != 31) {
1617 unallocated_encoding(s);
1618 return;
1620 switch (crn) {
1621 case 2: /* C5.6.68 HINT */
1622 handle_hint(s, insn, op1, op2, crm);
1623 break;
1624 case 3: /* CLREX, DSB, DMB, ISB */
1625 handle_sync(s, insn, op1, op2, crm);
1626 break;
1627 case 4: /* C5.6.130 MSR (immediate) */
1628 handle_msr_i(s, insn, op1, op2, crm);
1629 break;
1630 default:
1631 unallocated_encoding(s);
1632 break;
1634 return;
1636 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
1639 /* C3.2.3 Exception generation
1641 * 31 24 23 21 20 5 4 2 1 0
1642 * +-----------------+-----+------------------------+-----+----+
1643 * | 1 1 0 1 0 1 0 0 | opc | imm16 | op2 | LL |
1644 * +-----------------------+------------------------+----------+
1646 static void disas_exc(DisasContext *s, uint32_t insn)
1648 int opc = extract32(insn, 21, 3);
1649 int op2_ll = extract32(insn, 0, 5);
1650 int imm16 = extract32(insn, 5, 16);
1651 TCGv_i32 tmp;
1653 switch (opc) {
1654 case 0:
1655 /* For SVC, HVC and SMC we advance the single-step state
1656 * machine before taking the exception. This is architecturally
1657 * mandated, to ensure that single-stepping a system call
1658 * instruction works properly.
1660 switch (op2_ll) {
1661 case 1: /* SVC */
1662 gen_ss_advance(s);
1663 gen_exception_insn(s, 0, EXCP_SWI, syn_aa64_svc(imm16),
1664 default_exception_el(s));
1665 break;
1666 case 2: /* HVC */
1667 if (s->current_el == 0) {
1668 unallocated_encoding(s);
1669 break;
1671 /* The pre HVC helper handles cases when HVC gets trapped
1672 * as an undefined insn by runtime configuration.
1674 gen_a64_set_pc_im(s->pc - 4);
1675 gen_helper_pre_hvc(cpu_env);
1676 gen_ss_advance(s);
1677 gen_exception_insn(s, 0, EXCP_HVC, syn_aa64_hvc(imm16), 2);
1678 break;
1679 case 3: /* SMC */
1680 if (s->current_el == 0) {
1681 unallocated_encoding(s);
1682 break;
1684 gen_a64_set_pc_im(s->pc - 4);
1685 tmp = tcg_const_i32(syn_aa64_smc(imm16));
1686 gen_helper_pre_smc(cpu_env, tmp);
1687 tcg_temp_free_i32(tmp);
1688 gen_ss_advance(s);
1689 gen_exception_insn(s, 0, EXCP_SMC, syn_aa64_smc(imm16), 3);
1690 break;
1691 default:
1692 unallocated_encoding(s);
1693 break;
1695 break;
1696 case 1:
1697 if (op2_ll != 0) {
1698 unallocated_encoding(s);
1699 break;
1701 /* BRK */
1702 gen_exception_insn(s, 4, EXCP_BKPT, syn_aa64_bkpt(imm16),
1703 default_exception_el(s));
1704 break;
1705 case 2:
1706 if (op2_ll != 0) {
1707 unallocated_encoding(s);
1708 break;
1710 /* HLT. This has two purposes.
1711 * Architecturally, it is an external halting debug instruction.
1712 * Since QEMU doesn't implement external debug, we treat this as
1713 * it is required for halting debug disabled: it will UNDEF.
1714 * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
1716 if (semihosting_enabled() && imm16 == 0xf000) {
1717 #ifndef CONFIG_USER_ONLY
1718 /* In system mode, don't allow userspace access to semihosting,
1719 * to provide some semblance of security (and for consistency
1720 * with our 32-bit semihosting).
1722 if (s->current_el == 0) {
1723 unsupported_encoding(s, insn);
1724 break;
1726 #endif
1727 gen_exception_internal_insn(s, 0, EXCP_SEMIHOST);
1728 } else {
1729 unsupported_encoding(s, insn);
1731 break;
1732 case 5:
1733 if (op2_ll < 1 || op2_ll > 3) {
1734 unallocated_encoding(s);
1735 break;
1737 /* DCPS1, DCPS2, DCPS3 */
1738 unsupported_encoding(s, insn);
1739 break;
1740 default:
1741 unallocated_encoding(s);
1742 break;
1746 /* C3.2.7 Unconditional branch (register)
1747 * 31 25 24 21 20 16 15 10 9 5 4 0
1748 * +---------------+-------+-------+-------+------+-------+
1749 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
1750 * +---------------+-------+-------+-------+------+-------+
1752 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
1754 unsigned int opc, op2, op3, rn, op4;
1756 opc = extract32(insn, 21, 4);
1757 op2 = extract32(insn, 16, 5);
1758 op3 = extract32(insn, 10, 6);
1759 rn = extract32(insn, 5, 5);
1760 op4 = extract32(insn, 0, 5);
1762 if (op4 != 0x0 || op3 != 0x0 || op2 != 0x1f) {
1763 unallocated_encoding(s);
1764 return;
1767 switch (opc) {
1768 case 0: /* BR */
1769 case 1: /* BLR */
1770 case 2: /* RET */
1771 gen_a64_set_pc(s, cpu_reg(s, rn));
1772 /* BLR also needs to load return address */
1773 if (opc == 1) {
1774 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1776 break;
1777 case 4: /* ERET */
1778 if (s->current_el == 0) {
1779 unallocated_encoding(s);
1780 return;
1782 gen_helper_exception_return(cpu_env);
1783 s->is_jmp = DISAS_JUMP;
1784 return;
1785 case 5: /* DRPS */
1786 if (rn != 0x1f) {
1787 unallocated_encoding(s);
1788 } else {
1789 unsupported_encoding(s, insn);
1791 return;
1792 default:
1793 unallocated_encoding(s);
1794 return;
1797 s->is_jmp = DISAS_JUMP;
1800 /* C3.2 Branches, exception generating and system instructions */
1801 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
1803 switch (extract32(insn, 25, 7)) {
1804 case 0x0a: case 0x0b:
1805 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
1806 disas_uncond_b_imm(s, insn);
1807 break;
1808 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
1809 disas_comp_b_imm(s, insn);
1810 break;
1811 case 0x1b: case 0x5b: /* Test & branch (immediate) */
1812 disas_test_b_imm(s, insn);
1813 break;
1814 case 0x2a: /* Conditional branch (immediate) */
1815 disas_cond_b_imm(s, insn);
1816 break;
1817 case 0x6a: /* Exception generation / System */
1818 if (insn & (1 << 24)) {
1819 disas_system(s, insn);
1820 } else {
1821 disas_exc(s, insn);
1823 break;
1824 case 0x6b: /* Unconditional branch (register) */
1825 disas_uncond_b_reg(s, insn);
1826 break;
1827 default:
1828 unallocated_encoding(s);
1829 break;
1834 * Load/Store exclusive instructions are implemented by remembering
1835 * the value/address loaded, and seeing if these are the same
1836 * when the store is performed. This is not actually the architecturally
1837 * mandated semantics, but it works for typical guest code sequences
1838 * and avoids having to monitor regular stores.
1840 * The store exclusive uses the atomic cmpxchg primitives to avoid
1841 * races in multi-threaded linux-user and when MTTCG softmmu is
1842 * enabled.
1844 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
1845 TCGv_i64 addr, int size, bool is_pair)
1847 TCGv_i64 tmp = tcg_temp_new_i64();
1848 TCGMemOp memop = s->be_data + size;
1850 g_assert(size <= 3);
1851 tcg_gen_qemu_ld_i64(tmp, addr, get_mem_index(s), memop);
1853 if (is_pair) {
1854 TCGv_i64 addr2 = tcg_temp_new_i64();
1855 TCGv_i64 hitmp = tcg_temp_new_i64();
1857 g_assert(size >= 2);
1858 tcg_gen_addi_i64(addr2, addr, 1 << size);
1859 tcg_gen_qemu_ld_i64(hitmp, addr2, get_mem_index(s), memop);
1860 tcg_temp_free_i64(addr2);
1861 tcg_gen_mov_i64(cpu_exclusive_high, hitmp);
1862 tcg_gen_mov_i64(cpu_reg(s, rt2), hitmp);
1863 tcg_temp_free_i64(hitmp);
1866 tcg_gen_mov_i64(cpu_exclusive_val, tmp);
1867 tcg_gen_mov_i64(cpu_reg(s, rt), tmp);
1869 tcg_temp_free_i64(tmp);
1870 tcg_gen_mov_i64(cpu_exclusive_addr, addr);
1873 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
1874 TCGv_i64 inaddr, int size, int is_pair)
1876 /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
1877 * && (!is_pair || env->exclusive_high == [addr + datasize])) {
1878 * [addr] = {Rt};
1879 * if (is_pair) {
1880 * [addr + datasize] = {Rt2};
1882 * {Rd} = 0;
1883 * } else {
1884 * {Rd} = 1;
1886 * env->exclusive_addr = -1;
1888 TCGLabel *fail_label = gen_new_label();
1889 TCGLabel *done_label = gen_new_label();
1890 TCGv_i64 addr = tcg_temp_local_new_i64();
1891 TCGv_i64 tmp;
1893 /* Copy input into a local temp so it is not trashed when the
1894 * basic block ends at the branch insn.
1896 tcg_gen_mov_i64(addr, inaddr);
1897 tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
1899 tmp = tcg_temp_new_i64();
1900 if (is_pair) {
1901 if (size == 2) {
1902 TCGv_i64 val = tcg_temp_new_i64();
1903 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
1904 tcg_gen_concat32_i64(val, cpu_exclusive_val, cpu_exclusive_high);
1905 tcg_gen_atomic_cmpxchg_i64(tmp, addr, val, tmp,
1906 get_mem_index(s),
1907 size | MO_ALIGN | s->be_data);
1908 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, val);
1909 tcg_temp_free_i64(val);
1910 } else if (s->be_data == MO_LE) {
1911 gen_helper_paired_cmpxchg64_le(tmp, cpu_env, addr, cpu_reg(s, rt),
1912 cpu_reg(s, rt2));
1913 } else {
1914 gen_helper_paired_cmpxchg64_be(tmp, cpu_env, addr, cpu_reg(s, rt),
1915 cpu_reg(s, rt2));
1917 } else {
1918 TCGv_i64 val = cpu_reg(s, rt);
1919 tcg_gen_atomic_cmpxchg_i64(tmp, addr, cpu_exclusive_val, val,
1920 get_mem_index(s),
1921 size | MO_ALIGN | s->be_data);
1922 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
1925 tcg_temp_free_i64(addr);
1927 tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
1928 tcg_temp_free_i64(tmp);
1929 tcg_gen_br(done_label);
1931 gen_set_label(fail_label);
1932 tcg_gen_movi_i64(cpu_reg(s, rd), 1);
1933 gen_set_label(done_label);
1934 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1937 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
1938 * from the ARMv8 specs for LDR (Shared decode for all encodings).
1940 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
1942 int opc0 = extract32(opc, 0, 1);
1943 int regsize;
1945 if (is_signed) {
1946 regsize = opc0 ? 32 : 64;
1947 } else {
1948 regsize = size == 3 ? 64 : 32;
1950 return regsize == 64;
1953 /* C3.3.6 Load/store exclusive
1955 * 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0
1956 * +-----+-------------+----+---+----+------+----+-------+------+------+
1957 * | sz | 0 0 1 0 0 0 | o2 | L | o1 | Rs | o0 | Rt2 | Rn | Rt |
1958 * +-----+-------------+----+---+----+------+----+-------+------+------+
1960 * sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
1961 * L: 0 -> store, 1 -> load
1962 * o2: 0 -> exclusive, 1 -> not
1963 * o1: 0 -> single register, 1 -> register pair
1964 * o0: 1 -> load-acquire/store-release, 0 -> not
1966 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
1968 int rt = extract32(insn, 0, 5);
1969 int rn = extract32(insn, 5, 5);
1970 int rt2 = extract32(insn, 10, 5);
1971 int is_lasr = extract32(insn, 15, 1);
1972 int rs = extract32(insn, 16, 5);
1973 int is_pair = extract32(insn, 21, 1);
1974 int is_store = !extract32(insn, 22, 1);
1975 int is_excl = !extract32(insn, 23, 1);
1976 int size = extract32(insn, 30, 2);
1977 TCGv_i64 tcg_addr;
1979 if ((!is_excl && !is_pair && !is_lasr) ||
1980 (!is_excl && is_pair) ||
1981 (is_pair && size < 2)) {
1982 unallocated_encoding(s);
1983 return;
1986 if (rn == 31) {
1987 gen_check_sp_alignment(s);
1989 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1991 /* Note that since TCG is single threaded load-acquire/store-release
1992 * semantics require no extra if (is_lasr) { ... } handling.
1995 if (is_excl) {
1996 if (!is_store) {
1997 s->is_ldex = true;
1998 gen_load_exclusive(s, rt, rt2, tcg_addr, size, is_pair);
1999 if (is_lasr) {
2000 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2002 } else {
2003 if (is_lasr) {
2004 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2006 gen_store_exclusive(s, rs, rt, rt2, tcg_addr, size, is_pair);
2008 } else {
2009 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2010 bool iss_sf = disas_ldst_compute_iss_sf(size, false, 0);
2012 /* Generate ISS for non-exclusive accesses including LASR. */
2013 if (is_store) {
2014 if (is_lasr) {
2015 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2017 do_gpr_st(s, tcg_rt, tcg_addr, size,
2018 true, rt, iss_sf, is_lasr);
2019 } else {
2020 do_gpr_ld(s, tcg_rt, tcg_addr, size, false, false,
2021 true, rt, iss_sf, is_lasr);
2022 if (is_lasr) {
2023 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2030 * C3.3.5 Load register (literal)
2032 * 31 30 29 27 26 25 24 23 5 4 0
2033 * +-----+-------+---+-----+-------------------+-------+
2034 * | opc | 0 1 1 | V | 0 0 | imm19 | Rt |
2035 * +-----+-------+---+-----+-------------------+-------+
2037 * V: 1 -> vector (simd/fp)
2038 * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2039 * 10-> 32 bit signed, 11 -> prefetch
2040 * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2042 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2044 int rt = extract32(insn, 0, 5);
2045 int64_t imm = sextract32(insn, 5, 19) << 2;
2046 bool is_vector = extract32(insn, 26, 1);
2047 int opc = extract32(insn, 30, 2);
2048 bool is_signed = false;
2049 int size = 2;
2050 TCGv_i64 tcg_rt, tcg_addr;
2052 if (is_vector) {
2053 if (opc == 3) {
2054 unallocated_encoding(s);
2055 return;
2057 size = 2 + opc;
2058 if (!fp_access_check(s)) {
2059 return;
2061 } else {
2062 if (opc == 3) {
2063 /* PRFM (literal) : prefetch */
2064 return;
2066 size = 2 + extract32(opc, 0, 1);
2067 is_signed = extract32(opc, 1, 1);
2070 tcg_rt = cpu_reg(s, rt);
2072 tcg_addr = tcg_const_i64((s->pc - 4) + imm);
2073 if (is_vector) {
2074 do_fp_ld(s, rt, tcg_addr, size);
2075 } else {
2076 /* Only unsigned 32bit loads target 32bit registers. */
2077 bool iss_sf = opc != 0;
2079 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false,
2080 true, rt, iss_sf, false);
2082 tcg_temp_free_i64(tcg_addr);
2086 * C5.6.80 LDNP (Load Pair - non-temporal hint)
2087 * C5.6.81 LDP (Load Pair - non vector)
2088 * C5.6.82 LDPSW (Load Pair Signed Word - non vector)
2089 * C5.6.176 STNP (Store Pair - non-temporal hint)
2090 * C5.6.177 STP (Store Pair - non vector)
2091 * C6.3.165 LDNP (Load Pair of SIMD&FP - non-temporal hint)
2092 * C6.3.165 LDP (Load Pair of SIMD&FP)
2093 * C6.3.284 STNP (Store Pair of SIMD&FP - non-temporal hint)
2094 * C6.3.284 STP (Store Pair of SIMD&FP)
2096 * 31 30 29 27 26 25 24 23 22 21 15 14 10 9 5 4 0
2097 * +-----+-------+---+---+-------+---+-----------------------------+
2098 * | opc | 1 0 1 | V | 0 | index | L | imm7 | Rt2 | Rn | Rt |
2099 * +-----+-------+---+---+-------+---+-------+-------+------+------+
2101 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
2102 * LDPSW 01
2103 * LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2104 * V: 0 -> GPR, 1 -> Vector
2105 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2106 * 10 -> signed offset, 11 -> pre-index
2107 * L: 0 -> Store 1 -> Load
2109 * Rt, Rt2 = GPR or SIMD registers to be stored
2110 * Rn = general purpose register containing address
2111 * imm7 = signed offset (multiple of 4 or 8 depending on size)
2113 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2115 int rt = extract32(insn, 0, 5);
2116 int rn = extract32(insn, 5, 5);
2117 int rt2 = extract32(insn, 10, 5);
2118 uint64_t offset = sextract64(insn, 15, 7);
2119 int index = extract32(insn, 23, 2);
2120 bool is_vector = extract32(insn, 26, 1);
2121 bool is_load = extract32(insn, 22, 1);
2122 int opc = extract32(insn, 30, 2);
2124 bool is_signed = false;
2125 bool postindex = false;
2126 bool wback = false;
2128 TCGv_i64 tcg_addr; /* calculated address */
2129 int size;
2131 if (opc == 3) {
2132 unallocated_encoding(s);
2133 return;
2136 if (is_vector) {
2137 size = 2 + opc;
2138 } else {
2139 size = 2 + extract32(opc, 1, 1);
2140 is_signed = extract32(opc, 0, 1);
2141 if (!is_load && is_signed) {
2142 unallocated_encoding(s);
2143 return;
2147 switch (index) {
2148 case 1: /* post-index */
2149 postindex = true;
2150 wback = true;
2151 break;
2152 case 0:
2153 /* signed offset with "non-temporal" hint. Since we don't emulate
2154 * caches we don't care about hints to the cache system about
2155 * data access patterns, and handle this identically to plain
2156 * signed offset.
2158 if (is_signed) {
2159 /* There is no non-temporal-hint version of LDPSW */
2160 unallocated_encoding(s);
2161 return;
2163 postindex = false;
2164 break;
2165 case 2: /* signed offset, rn not updated */
2166 postindex = false;
2167 break;
2168 case 3: /* pre-index */
2169 postindex = false;
2170 wback = true;
2171 break;
2174 if (is_vector && !fp_access_check(s)) {
2175 return;
2178 offset <<= size;
2180 if (rn == 31) {
2181 gen_check_sp_alignment(s);
2184 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2186 if (!postindex) {
2187 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
2190 if (is_vector) {
2191 if (is_load) {
2192 do_fp_ld(s, rt, tcg_addr, size);
2193 } else {
2194 do_fp_st(s, rt, tcg_addr, size);
2196 } else {
2197 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2198 if (is_load) {
2199 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false,
2200 false, 0, false, false);
2201 } else {
2202 do_gpr_st(s, tcg_rt, tcg_addr, size,
2203 false, 0, false, false);
2206 tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
2207 if (is_vector) {
2208 if (is_load) {
2209 do_fp_ld(s, rt2, tcg_addr, size);
2210 } else {
2211 do_fp_st(s, rt2, tcg_addr, size);
2213 } else {
2214 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
2215 if (is_load) {
2216 do_gpr_ld(s, tcg_rt2, tcg_addr, size, is_signed, false,
2217 false, 0, false, false);
2218 } else {
2219 do_gpr_st(s, tcg_rt2, tcg_addr, size,
2220 false, 0, false, false);
2224 if (wback) {
2225 if (postindex) {
2226 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset - (1 << size));
2227 } else {
2228 tcg_gen_subi_i64(tcg_addr, tcg_addr, 1 << size);
2230 tcg_gen_mov_i64(cpu_reg_sp(s, rn), tcg_addr);
2235 * C3.3.8 Load/store (immediate post-indexed)
2236 * C3.3.9 Load/store (immediate pre-indexed)
2237 * C3.3.12 Load/store (unscaled immediate)
2239 * 31 30 29 27 26 25 24 23 22 21 20 12 11 10 9 5 4 0
2240 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2241 * |size| 1 1 1 | V | 0 0 | opc | 0 | imm9 | idx | Rn | Rt |
2242 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2244 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
2245 10 -> unprivileged
2246 * V = 0 -> non-vector
2247 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
2248 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2250 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
2251 int opc,
2252 int size,
2253 int rt,
2254 bool is_vector)
2256 int rn = extract32(insn, 5, 5);
2257 int imm9 = sextract32(insn, 12, 9);
2258 int idx = extract32(insn, 10, 2);
2259 bool is_signed = false;
2260 bool is_store = false;
2261 bool is_extended = false;
2262 bool is_unpriv = (idx == 2);
2263 bool iss_valid = !is_vector;
2264 bool post_index;
2265 bool writeback;
2267 TCGv_i64 tcg_addr;
2269 if (is_vector) {
2270 size |= (opc & 2) << 1;
2271 if (size > 4 || is_unpriv) {
2272 unallocated_encoding(s);
2273 return;
2275 is_store = ((opc & 1) == 0);
2276 if (!fp_access_check(s)) {
2277 return;
2279 } else {
2280 if (size == 3 && opc == 2) {
2281 /* PRFM - prefetch */
2282 if (is_unpriv) {
2283 unallocated_encoding(s);
2284 return;
2286 return;
2288 if (opc == 3 && size > 1) {
2289 unallocated_encoding(s);
2290 return;
2292 is_store = (opc == 0);
2293 is_signed = extract32(opc, 1, 1);
2294 is_extended = (size < 3) && extract32(opc, 0, 1);
2297 switch (idx) {
2298 case 0:
2299 case 2:
2300 post_index = false;
2301 writeback = false;
2302 break;
2303 case 1:
2304 post_index = true;
2305 writeback = true;
2306 break;
2307 case 3:
2308 post_index = false;
2309 writeback = true;
2310 break;
2313 if (rn == 31) {
2314 gen_check_sp_alignment(s);
2316 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2318 if (!post_index) {
2319 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
2322 if (is_vector) {
2323 if (is_store) {
2324 do_fp_st(s, rt, tcg_addr, size);
2325 } else {
2326 do_fp_ld(s, rt, tcg_addr, size);
2328 } else {
2329 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2330 int memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
2331 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2333 if (is_store) {
2334 do_gpr_st_memidx(s, tcg_rt, tcg_addr, size, memidx,
2335 iss_valid, rt, iss_sf, false);
2336 } else {
2337 do_gpr_ld_memidx(s, tcg_rt, tcg_addr, size,
2338 is_signed, is_extended, memidx,
2339 iss_valid, rt, iss_sf, false);
2343 if (writeback) {
2344 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2345 if (post_index) {
2346 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
2348 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2353 * C3.3.10 Load/store (register offset)
2355 * 31 30 29 27 26 25 24 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2356 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2357 * |size| 1 1 1 | V | 0 0 | opc | 1 | Rm | opt | S| 1 0 | Rn | Rt |
2358 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2360 * For non-vector:
2361 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2362 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2363 * For vector:
2364 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2365 * opc<0>: 0 -> store, 1 -> load
2366 * V: 1 -> vector/simd
2367 * opt: extend encoding (see DecodeRegExtend)
2368 * S: if S=1 then scale (essentially index by sizeof(size))
2369 * Rt: register to transfer into/out of
2370 * Rn: address register or SP for base
2371 * Rm: offset register or ZR for offset
2373 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
2374 int opc,
2375 int size,
2376 int rt,
2377 bool is_vector)
2379 int rn = extract32(insn, 5, 5);
2380 int shift = extract32(insn, 12, 1);
2381 int rm = extract32(insn, 16, 5);
2382 int opt = extract32(insn, 13, 3);
2383 bool is_signed = false;
2384 bool is_store = false;
2385 bool is_extended = false;
2387 TCGv_i64 tcg_rm;
2388 TCGv_i64 tcg_addr;
2390 if (extract32(opt, 1, 1) == 0) {
2391 unallocated_encoding(s);
2392 return;
2395 if (is_vector) {
2396 size |= (opc & 2) << 1;
2397 if (size > 4) {
2398 unallocated_encoding(s);
2399 return;
2401 is_store = !extract32(opc, 0, 1);
2402 if (!fp_access_check(s)) {
2403 return;
2405 } else {
2406 if (size == 3 && opc == 2) {
2407 /* PRFM - prefetch */
2408 return;
2410 if (opc == 3 && size > 1) {
2411 unallocated_encoding(s);
2412 return;
2414 is_store = (opc == 0);
2415 is_signed = extract32(opc, 1, 1);
2416 is_extended = (size < 3) && extract32(opc, 0, 1);
2419 if (rn == 31) {
2420 gen_check_sp_alignment(s);
2422 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2424 tcg_rm = read_cpu_reg(s, rm, 1);
2425 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
2427 tcg_gen_add_i64(tcg_addr, tcg_addr, tcg_rm);
2429 if (is_vector) {
2430 if (is_store) {
2431 do_fp_st(s, rt, tcg_addr, size);
2432 } else {
2433 do_fp_ld(s, rt, tcg_addr, size);
2435 } else {
2436 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2437 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2438 if (is_store) {
2439 do_gpr_st(s, tcg_rt, tcg_addr, size,
2440 true, rt, iss_sf, false);
2441 } else {
2442 do_gpr_ld(s, tcg_rt, tcg_addr, size,
2443 is_signed, is_extended,
2444 true, rt, iss_sf, false);
2450 * C3.3.13 Load/store (unsigned immediate)
2452 * 31 30 29 27 26 25 24 23 22 21 10 9 5
2453 * +----+-------+---+-----+-----+------------+-------+------+
2454 * |size| 1 1 1 | V | 0 1 | opc | imm12 | Rn | Rt |
2455 * +----+-------+---+-----+-----+------------+-------+------+
2457 * For non-vector:
2458 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2459 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2460 * For vector:
2461 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2462 * opc<0>: 0 -> store, 1 -> load
2463 * Rn: base address register (inc SP)
2464 * Rt: target register
2466 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
2467 int opc,
2468 int size,
2469 int rt,
2470 bool is_vector)
2472 int rn = extract32(insn, 5, 5);
2473 unsigned int imm12 = extract32(insn, 10, 12);
2474 unsigned int offset;
2476 TCGv_i64 tcg_addr;
2478 bool is_store;
2479 bool is_signed = false;
2480 bool is_extended = false;
2482 if (is_vector) {
2483 size |= (opc & 2) << 1;
2484 if (size > 4) {
2485 unallocated_encoding(s);
2486 return;
2488 is_store = !extract32(opc, 0, 1);
2489 if (!fp_access_check(s)) {
2490 return;
2492 } else {
2493 if (size == 3 && opc == 2) {
2494 /* PRFM - prefetch */
2495 return;
2497 if (opc == 3 && size > 1) {
2498 unallocated_encoding(s);
2499 return;
2501 is_store = (opc == 0);
2502 is_signed = extract32(opc, 1, 1);
2503 is_extended = (size < 3) && extract32(opc, 0, 1);
2506 if (rn == 31) {
2507 gen_check_sp_alignment(s);
2509 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2510 offset = imm12 << size;
2511 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
2513 if (is_vector) {
2514 if (is_store) {
2515 do_fp_st(s, rt, tcg_addr, size);
2516 } else {
2517 do_fp_ld(s, rt, tcg_addr, size);
2519 } else {
2520 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2521 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2522 if (is_store) {
2523 do_gpr_st(s, tcg_rt, tcg_addr, size,
2524 true, rt, iss_sf, false);
2525 } else {
2526 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended,
2527 true, rt, iss_sf, false);
2532 /* Load/store register (all forms) */
2533 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
2535 int rt = extract32(insn, 0, 5);
2536 int opc = extract32(insn, 22, 2);
2537 bool is_vector = extract32(insn, 26, 1);
2538 int size = extract32(insn, 30, 2);
2540 switch (extract32(insn, 24, 2)) {
2541 case 0:
2542 if (extract32(insn, 21, 1) == 1 && extract32(insn, 10, 2) == 2) {
2543 disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
2544 } else {
2545 /* Load/store register (unscaled immediate)
2546 * Load/store immediate pre/post-indexed
2547 * Load/store register unprivileged
2549 disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
2551 break;
2552 case 1:
2553 disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
2554 break;
2555 default:
2556 unallocated_encoding(s);
2557 break;
2561 /* C3.3.1 AdvSIMD load/store multiple structures
2563 * 31 30 29 23 22 21 16 15 12 11 10 9 5 4 0
2564 * +---+---+---------------+---+-------------+--------+------+------+------+
2565 * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size | Rn | Rt |
2566 * +---+---+---------------+---+-------------+--------+------+------+------+
2568 * C3.3.2 AdvSIMD load/store multiple structures (post-indexed)
2570 * 31 30 29 23 22 21 20 16 15 12 11 10 9 5 4 0
2571 * +---+---+---------------+---+---+---------+--------+------+------+------+
2572 * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 | Rm | opcode | size | Rn | Rt |
2573 * +---+---+---------------+---+---+---------+--------+------+------+------+
2575 * Rt: first (or only) SIMD&FP register to be transferred
2576 * Rn: base address or SP
2577 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2579 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
2581 int rt = extract32(insn, 0, 5);
2582 int rn = extract32(insn, 5, 5);
2583 int size = extract32(insn, 10, 2);
2584 int opcode = extract32(insn, 12, 4);
2585 bool is_store = !extract32(insn, 22, 1);
2586 bool is_postidx = extract32(insn, 23, 1);
2587 bool is_q = extract32(insn, 30, 1);
2588 TCGv_i64 tcg_addr, tcg_rn;
2590 int ebytes = 1 << size;
2591 int elements = (is_q ? 128 : 64) / (8 << size);
2592 int rpt; /* num iterations */
2593 int selem; /* structure elements */
2594 int r;
2596 if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
2597 unallocated_encoding(s);
2598 return;
2601 /* From the shared decode logic */
2602 switch (opcode) {
2603 case 0x0:
2604 rpt = 1;
2605 selem = 4;
2606 break;
2607 case 0x2:
2608 rpt = 4;
2609 selem = 1;
2610 break;
2611 case 0x4:
2612 rpt = 1;
2613 selem = 3;
2614 break;
2615 case 0x6:
2616 rpt = 3;
2617 selem = 1;
2618 break;
2619 case 0x7:
2620 rpt = 1;
2621 selem = 1;
2622 break;
2623 case 0x8:
2624 rpt = 1;
2625 selem = 2;
2626 break;
2627 case 0xa:
2628 rpt = 2;
2629 selem = 1;
2630 break;
2631 default:
2632 unallocated_encoding(s);
2633 return;
2636 if (size == 3 && !is_q && selem != 1) {
2637 /* reserved */
2638 unallocated_encoding(s);
2639 return;
2642 if (!fp_access_check(s)) {
2643 return;
2646 if (rn == 31) {
2647 gen_check_sp_alignment(s);
2650 tcg_rn = cpu_reg_sp(s, rn);
2651 tcg_addr = tcg_temp_new_i64();
2652 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2654 for (r = 0; r < rpt; r++) {
2655 int e;
2656 for (e = 0; e < elements; e++) {
2657 int tt = (rt + r) % 32;
2658 int xs;
2659 for (xs = 0; xs < selem; xs++) {
2660 if (is_store) {
2661 do_vec_st(s, tt, e, tcg_addr, size);
2662 } else {
2663 do_vec_ld(s, tt, e, tcg_addr, size);
2665 /* For non-quad operations, setting a slice of the low
2666 * 64 bits of the register clears the high 64 bits (in
2667 * the ARM ARM pseudocode this is implicit in the fact
2668 * that 'rval' is a 64 bit wide variable). We optimize
2669 * by noticing that we only need to do this the first
2670 * time we touch a register.
2672 if (!is_q && e == 0 && (r == 0 || xs == selem - 1)) {
2673 clear_vec_high(s, tt);
2676 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2677 tt = (tt + 1) % 32;
2682 if (is_postidx) {
2683 int rm = extract32(insn, 16, 5);
2684 if (rm == 31) {
2685 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2686 } else {
2687 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2690 tcg_temp_free_i64(tcg_addr);
2693 /* C3.3.3 AdvSIMD load/store single structure
2695 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2696 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2697 * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size | Rn | Rt |
2698 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2700 * C3.3.4 AdvSIMD load/store single structure (post-indexed)
2702 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2703 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2704 * | 0 | Q | 0 0 1 1 0 1 1 | L R | Rm | opc | S | size | Rn | Rt |
2705 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2707 * Rt: first (or only) SIMD&FP register to be transferred
2708 * Rn: base address or SP
2709 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2710 * index = encoded in Q:S:size dependent on size
2712 * lane_size = encoded in R, opc
2713 * transfer width = encoded in opc, S, size
2715 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
2717 int rt = extract32(insn, 0, 5);
2718 int rn = extract32(insn, 5, 5);
2719 int size = extract32(insn, 10, 2);
2720 int S = extract32(insn, 12, 1);
2721 int opc = extract32(insn, 13, 3);
2722 int R = extract32(insn, 21, 1);
2723 int is_load = extract32(insn, 22, 1);
2724 int is_postidx = extract32(insn, 23, 1);
2725 int is_q = extract32(insn, 30, 1);
2727 int scale = extract32(opc, 1, 2);
2728 int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
2729 bool replicate = false;
2730 int index = is_q << 3 | S << 2 | size;
2731 int ebytes, xs;
2732 TCGv_i64 tcg_addr, tcg_rn;
2734 switch (scale) {
2735 case 3:
2736 if (!is_load || S) {
2737 unallocated_encoding(s);
2738 return;
2740 scale = size;
2741 replicate = true;
2742 break;
2743 case 0:
2744 break;
2745 case 1:
2746 if (extract32(size, 0, 1)) {
2747 unallocated_encoding(s);
2748 return;
2750 index >>= 1;
2751 break;
2752 case 2:
2753 if (extract32(size, 1, 1)) {
2754 unallocated_encoding(s);
2755 return;
2757 if (!extract32(size, 0, 1)) {
2758 index >>= 2;
2759 } else {
2760 if (S) {
2761 unallocated_encoding(s);
2762 return;
2764 index >>= 3;
2765 scale = 3;
2767 break;
2768 default:
2769 g_assert_not_reached();
2772 if (!fp_access_check(s)) {
2773 return;
2776 ebytes = 1 << scale;
2778 if (rn == 31) {
2779 gen_check_sp_alignment(s);
2782 tcg_rn = cpu_reg_sp(s, rn);
2783 tcg_addr = tcg_temp_new_i64();
2784 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2786 for (xs = 0; xs < selem; xs++) {
2787 if (replicate) {
2788 /* Load and replicate to all elements */
2789 uint64_t mulconst;
2790 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
2792 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr,
2793 get_mem_index(s), s->be_data + scale);
2794 switch (scale) {
2795 case 0:
2796 mulconst = 0x0101010101010101ULL;
2797 break;
2798 case 1:
2799 mulconst = 0x0001000100010001ULL;
2800 break;
2801 case 2:
2802 mulconst = 0x0000000100000001ULL;
2803 break;
2804 case 3:
2805 mulconst = 0;
2806 break;
2807 default:
2808 g_assert_not_reached();
2810 if (mulconst) {
2811 tcg_gen_muli_i64(tcg_tmp, tcg_tmp, mulconst);
2813 write_vec_element(s, tcg_tmp, rt, 0, MO_64);
2814 if (is_q) {
2815 write_vec_element(s, tcg_tmp, rt, 1, MO_64);
2816 } else {
2817 clear_vec_high(s, rt);
2819 tcg_temp_free_i64(tcg_tmp);
2820 } else {
2821 /* Load/store one element per register */
2822 if (is_load) {
2823 do_vec_ld(s, rt, index, tcg_addr, scale);
2824 } else {
2825 do_vec_st(s, rt, index, tcg_addr, scale);
2828 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2829 rt = (rt + 1) % 32;
2832 if (is_postidx) {
2833 int rm = extract32(insn, 16, 5);
2834 if (rm == 31) {
2835 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2836 } else {
2837 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2840 tcg_temp_free_i64(tcg_addr);
2843 /* C3.3 Loads and stores */
2844 static void disas_ldst(DisasContext *s, uint32_t insn)
2846 switch (extract32(insn, 24, 6)) {
2847 case 0x08: /* Load/store exclusive */
2848 disas_ldst_excl(s, insn);
2849 break;
2850 case 0x18: case 0x1c: /* Load register (literal) */
2851 disas_ld_lit(s, insn);
2852 break;
2853 case 0x28: case 0x29:
2854 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
2855 disas_ldst_pair(s, insn);
2856 break;
2857 case 0x38: case 0x39:
2858 case 0x3c: case 0x3d: /* Load/store register (all forms) */
2859 disas_ldst_reg(s, insn);
2860 break;
2861 case 0x0c: /* AdvSIMD load/store multiple structures */
2862 disas_ldst_multiple_struct(s, insn);
2863 break;
2864 case 0x0d: /* AdvSIMD load/store single structure */
2865 disas_ldst_single_struct(s, insn);
2866 break;
2867 default:
2868 unallocated_encoding(s);
2869 break;
2873 /* C3.4.6 PC-rel. addressing
2874 * 31 30 29 28 24 23 5 4 0
2875 * +----+-------+-----------+-------------------+------+
2876 * | op | immlo | 1 0 0 0 0 | immhi | Rd |
2877 * +----+-------+-----------+-------------------+------+
2879 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
2881 unsigned int page, rd;
2882 uint64_t base;
2883 uint64_t offset;
2885 page = extract32(insn, 31, 1);
2886 /* SignExtend(immhi:immlo) -> offset */
2887 offset = sextract64(insn, 5, 19);
2888 offset = offset << 2 | extract32(insn, 29, 2);
2889 rd = extract32(insn, 0, 5);
2890 base = s->pc - 4;
2892 if (page) {
2893 /* ADRP (page based) */
2894 base &= ~0xfff;
2895 offset <<= 12;
2898 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
2902 * C3.4.1 Add/subtract (immediate)
2904 * 31 30 29 28 24 23 22 21 10 9 5 4 0
2905 * +--+--+--+-----------+-----+-------------+-----+-----+
2906 * |sf|op| S| 1 0 0 0 1 |shift| imm12 | Rn | Rd |
2907 * +--+--+--+-----------+-----+-------------+-----+-----+
2909 * sf: 0 -> 32bit, 1 -> 64bit
2910 * op: 0 -> add , 1 -> sub
2911 * S: 1 -> set flags
2912 * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
2914 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
2916 int rd = extract32(insn, 0, 5);
2917 int rn = extract32(insn, 5, 5);
2918 uint64_t imm = extract32(insn, 10, 12);
2919 int shift = extract32(insn, 22, 2);
2920 bool setflags = extract32(insn, 29, 1);
2921 bool sub_op = extract32(insn, 30, 1);
2922 bool is_64bit = extract32(insn, 31, 1);
2924 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2925 TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
2926 TCGv_i64 tcg_result;
2928 switch (shift) {
2929 case 0x0:
2930 break;
2931 case 0x1:
2932 imm <<= 12;
2933 break;
2934 default:
2935 unallocated_encoding(s);
2936 return;
2939 tcg_result = tcg_temp_new_i64();
2940 if (!setflags) {
2941 if (sub_op) {
2942 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
2943 } else {
2944 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
2946 } else {
2947 TCGv_i64 tcg_imm = tcg_const_i64(imm);
2948 if (sub_op) {
2949 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2950 } else {
2951 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2953 tcg_temp_free_i64(tcg_imm);
2956 if (is_64bit) {
2957 tcg_gen_mov_i64(tcg_rd, tcg_result);
2958 } else {
2959 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
2962 tcg_temp_free_i64(tcg_result);
2965 /* The input should be a value in the bottom e bits (with higher
2966 * bits zero); returns that value replicated into every element
2967 * of size e in a 64 bit integer.
2969 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
2971 assert(e != 0);
2972 while (e < 64) {
2973 mask |= mask << e;
2974 e *= 2;
2976 return mask;
2979 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
2980 static inline uint64_t bitmask64(unsigned int length)
2982 assert(length > 0 && length <= 64);
2983 return ~0ULL >> (64 - length);
2986 /* Simplified variant of pseudocode DecodeBitMasks() for the case where we
2987 * only require the wmask. Returns false if the imms/immr/immn are a reserved
2988 * value (ie should cause a guest UNDEF exception), and true if they are
2989 * valid, in which case the decoded bit pattern is written to result.
2991 static bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
2992 unsigned int imms, unsigned int immr)
2994 uint64_t mask;
2995 unsigned e, levels, s, r;
2996 int len;
2998 assert(immn < 2 && imms < 64 && immr < 64);
3000 /* The bit patterns we create here are 64 bit patterns which
3001 * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
3002 * 64 bits each. Each element contains the same value: a run
3003 * of between 1 and e-1 non-zero bits, rotated within the
3004 * element by between 0 and e-1 bits.
3006 * The element size and run length are encoded into immn (1 bit)
3007 * and imms (6 bits) as follows:
3008 * 64 bit elements: immn = 1, imms = <length of run - 1>
3009 * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
3010 * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
3011 * 8 bit elements: immn = 0, imms = 110 : <length of run - 1>
3012 * 4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
3013 * 2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
3014 * Notice that immn = 0, imms = 11111x is the only combination
3015 * not covered by one of the above options; this is reserved.
3016 * Further, <length of run - 1> all-ones is a reserved pattern.
3018 * In all cases the rotation is by immr % e (and immr is 6 bits).
3021 /* First determine the element size */
3022 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
3023 if (len < 1) {
3024 /* This is the immn == 0, imms == 0x11111x case */
3025 return false;
3027 e = 1 << len;
3029 levels = e - 1;
3030 s = imms & levels;
3031 r = immr & levels;
3033 if (s == levels) {
3034 /* <length of run - 1> mustn't be all-ones. */
3035 return false;
3038 /* Create the value of one element: s+1 set bits rotated
3039 * by r within the element (which is e bits wide)...
3041 mask = bitmask64(s + 1);
3042 if (r) {
3043 mask = (mask >> r) | (mask << (e - r));
3044 mask &= bitmask64(e);
3046 /* ...then replicate the element over the whole 64 bit value */
3047 mask = bitfield_replicate(mask, e);
3048 *result = mask;
3049 return true;
3052 /* C3.4.4 Logical (immediate)
3053 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
3054 * +----+-----+-------------+---+------+------+------+------+
3055 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms | Rn | Rd |
3056 * +----+-----+-------------+---+------+------+------+------+
3058 static void disas_logic_imm(DisasContext *s, uint32_t insn)
3060 unsigned int sf, opc, is_n, immr, imms, rn, rd;
3061 TCGv_i64 tcg_rd, tcg_rn;
3062 uint64_t wmask;
3063 bool is_and = false;
3065 sf = extract32(insn, 31, 1);
3066 opc = extract32(insn, 29, 2);
3067 is_n = extract32(insn, 22, 1);
3068 immr = extract32(insn, 16, 6);
3069 imms = extract32(insn, 10, 6);
3070 rn = extract32(insn, 5, 5);
3071 rd = extract32(insn, 0, 5);
3073 if (!sf && is_n) {
3074 unallocated_encoding(s);
3075 return;
3078 if (opc == 0x3) { /* ANDS */
3079 tcg_rd = cpu_reg(s, rd);
3080 } else {
3081 tcg_rd = cpu_reg_sp(s, rd);
3083 tcg_rn = cpu_reg(s, rn);
3085 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
3086 /* some immediate field values are reserved */
3087 unallocated_encoding(s);
3088 return;
3091 if (!sf) {
3092 wmask &= 0xffffffff;
3095 switch (opc) {
3096 case 0x3: /* ANDS */
3097 case 0x0: /* AND */
3098 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
3099 is_and = true;
3100 break;
3101 case 0x1: /* ORR */
3102 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
3103 break;
3104 case 0x2: /* EOR */
3105 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
3106 break;
3107 default:
3108 assert(FALSE); /* must handle all above */
3109 break;
3112 if (!sf && !is_and) {
3113 /* zero extend final result; we know we can skip this for AND
3114 * since the immediate had the high 32 bits clear.
3116 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3119 if (opc == 3) { /* ANDS */
3120 gen_logic_CC(sf, tcg_rd);
3125 * C3.4.5 Move wide (immediate)
3127 * 31 30 29 28 23 22 21 20 5 4 0
3128 * +--+-----+-------------+-----+----------------+------+
3129 * |sf| opc | 1 0 0 1 0 1 | hw | imm16 | Rd |
3130 * +--+-----+-------------+-----+----------------+------+
3132 * sf: 0 -> 32 bit, 1 -> 64 bit
3133 * opc: 00 -> N, 10 -> Z, 11 -> K
3134 * hw: shift/16 (0,16, and sf only 32, 48)
3136 static void disas_movw_imm(DisasContext *s, uint32_t insn)
3138 int rd = extract32(insn, 0, 5);
3139 uint64_t imm = extract32(insn, 5, 16);
3140 int sf = extract32(insn, 31, 1);
3141 int opc = extract32(insn, 29, 2);
3142 int pos = extract32(insn, 21, 2) << 4;
3143 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3144 TCGv_i64 tcg_imm;
3146 if (!sf && (pos >= 32)) {
3147 unallocated_encoding(s);
3148 return;
3151 switch (opc) {
3152 case 0: /* MOVN */
3153 case 2: /* MOVZ */
3154 imm <<= pos;
3155 if (opc == 0) {
3156 imm = ~imm;
3158 if (!sf) {
3159 imm &= 0xffffffffu;
3161 tcg_gen_movi_i64(tcg_rd, imm);
3162 break;
3163 case 3: /* MOVK */
3164 tcg_imm = tcg_const_i64(imm);
3165 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
3166 tcg_temp_free_i64(tcg_imm);
3167 if (!sf) {
3168 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3170 break;
3171 default:
3172 unallocated_encoding(s);
3173 break;
3177 /* C3.4.2 Bitfield
3178 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
3179 * +----+-----+-------------+---+------+------+------+------+
3180 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms | Rn | Rd |
3181 * +----+-----+-------------+---+------+------+------+------+
3183 static void disas_bitfield(DisasContext *s, uint32_t insn)
3185 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
3186 TCGv_i64 tcg_rd, tcg_tmp;
3188 sf = extract32(insn, 31, 1);
3189 opc = extract32(insn, 29, 2);
3190 n = extract32(insn, 22, 1);
3191 ri = extract32(insn, 16, 6);
3192 si = extract32(insn, 10, 6);
3193 rn = extract32(insn, 5, 5);
3194 rd = extract32(insn, 0, 5);
3195 bitsize = sf ? 64 : 32;
3197 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
3198 unallocated_encoding(s);
3199 return;
3202 tcg_rd = cpu_reg(s, rd);
3204 /* Suppress the zero-extend for !sf. Since RI and SI are constrained
3205 to be smaller than bitsize, we'll never reference data outside the
3206 low 32-bits anyway. */
3207 tcg_tmp = read_cpu_reg(s, rn, 1);
3209 /* Recognize simple(r) extractions. */
3210 if (si >= ri) {
3211 /* Wd<s-r:0> = Wn<s:r> */
3212 len = (si - ri) + 1;
3213 if (opc == 0) { /* SBFM: ASR, SBFX, SXTB, SXTH, SXTW */
3214 tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
3215 goto done;
3216 } else if (opc == 2) { /* UBFM: UBFX, LSR, UXTB, UXTH */
3217 tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
3218 return;
3220 /* opc == 1, BXFIL fall through to deposit */
3221 tcg_gen_extract_i64(tcg_tmp, tcg_tmp, ri, len);
3222 pos = 0;
3223 } else {
3224 /* Handle the ri > si case with a deposit
3225 * Wd<32+s-r,32-r> = Wn<s:0>
3227 len = si + 1;
3228 pos = (bitsize - ri) & (bitsize - 1);
3231 if (opc == 0 && len < ri) {
3232 /* SBFM: sign extend the destination field from len to fill
3233 the balance of the word. Let the deposit below insert all
3234 of those sign bits. */
3235 tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
3236 len = ri;
3239 if (opc == 1) { /* BFM, BXFIL */
3240 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
3241 } else {
3242 /* SBFM or UBFM: We start with zero, and we haven't modified
3243 any bits outside bitsize, therefore the zero-extension
3244 below is unneeded. */
3245 tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
3246 return;
3249 done:
3250 if (!sf) { /* zero extend final result */
3251 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3255 /* C3.4.3 Extract
3256 * 31 30 29 28 23 22 21 20 16 15 10 9 5 4 0
3257 * +----+------+-------------+---+----+------+--------+------+------+
3258 * | sf | op21 | 1 0 0 1 1 1 | N | o0 | Rm | imms | Rn | Rd |
3259 * +----+------+-------------+---+----+------+--------+------+------+
3261 static void disas_extract(DisasContext *s, uint32_t insn)
3263 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
3265 sf = extract32(insn, 31, 1);
3266 n = extract32(insn, 22, 1);
3267 rm = extract32(insn, 16, 5);
3268 imm = extract32(insn, 10, 6);
3269 rn = extract32(insn, 5, 5);
3270 rd = extract32(insn, 0, 5);
3271 op21 = extract32(insn, 29, 2);
3272 op0 = extract32(insn, 21, 1);
3273 bitsize = sf ? 64 : 32;
3275 if (sf != n || op21 || op0 || imm >= bitsize) {
3276 unallocated_encoding(s);
3277 } else {
3278 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
3280 tcg_rd = cpu_reg(s, rd);
3282 if (unlikely(imm == 0)) {
3283 /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
3284 * so an extract from bit 0 is a special case.
3286 if (sf) {
3287 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
3288 } else {
3289 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
3291 } else if (rm == rn) { /* ROR */
3292 tcg_rm = cpu_reg(s, rm);
3293 if (sf) {
3294 tcg_gen_rotri_i64(tcg_rd, tcg_rm, imm);
3295 } else {
3296 TCGv_i32 tmp = tcg_temp_new_i32();
3297 tcg_gen_extrl_i64_i32(tmp, tcg_rm);
3298 tcg_gen_rotri_i32(tmp, tmp, imm);
3299 tcg_gen_extu_i32_i64(tcg_rd, tmp);
3300 tcg_temp_free_i32(tmp);
3302 } else {
3303 tcg_rm = read_cpu_reg(s, rm, sf);
3304 tcg_rn = read_cpu_reg(s, rn, sf);
3305 tcg_gen_shri_i64(tcg_rm, tcg_rm, imm);
3306 tcg_gen_shli_i64(tcg_rn, tcg_rn, bitsize - imm);
3307 tcg_gen_or_i64(tcg_rd, tcg_rm, tcg_rn);
3308 if (!sf) {
3309 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3315 /* C3.4 Data processing - immediate */
3316 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
3318 switch (extract32(insn, 23, 6)) {
3319 case 0x20: case 0x21: /* PC-rel. addressing */
3320 disas_pc_rel_adr(s, insn);
3321 break;
3322 case 0x22: case 0x23: /* Add/subtract (immediate) */
3323 disas_add_sub_imm(s, insn);
3324 break;
3325 case 0x24: /* Logical (immediate) */
3326 disas_logic_imm(s, insn);
3327 break;
3328 case 0x25: /* Move wide (immediate) */
3329 disas_movw_imm(s, insn);
3330 break;
3331 case 0x26: /* Bitfield */
3332 disas_bitfield(s, insn);
3333 break;
3334 case 0x27: /* Extract */
3335 disas_extract(s, insn);
3336 break;
3337 default:
3338 unallocated_encoding(s);
3339 break;
3343 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
3344 * Note that it is the caller's responsibility to ensure that the
3345 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
3346 * mandated semantics for out of range shifts.
3348 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
3349 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
3351 switch (shift_type) {
3352 case A64_SHIFT_TYPE_LSL:
3353 tcg_gen_shl_i64(dst, src, shift_amount);
3354 break;
3355 case A64_SHIFT_TYPE_LSR:
3356 tcg_gen_shr_i64(dst, src, shift_amount);
3357 break;
3358 case A64_SHIFT_TYPE_ASR:
3359 if (!sf) {
3360 tcg_gen_ext32s_i64(dst, src);
3362 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
3363 break;
3364 case A64_SHIFT_TYPE_ROR:
3365 if (sf) {
3366 tcg_gen_rotr_i64(dst, src, shift_amount);
3367 } else {
3368 TCGv_i32 t0, t1;
3369 t0 = tcg_temp_new_i32();
3370 t1 = tcg_temp_new_i32();
3371 tcg_gen_extrl_i64_i32(t0, src);
3372 tcg_gen_extrl_i64_i32(t1, shift_amount);
3373 tcg_gen_rotr_i32(t0, t0, t1);
3374 tcg_gen_extu_i32_i64(dst, t0);
3375 tcg_temp_free_i32(t0);
3376 tcg_temp_free_i32(t1);
3378 break;
3379 default:
3380 assert(FALSE); /* all shift types should be handled */
3381 break;
3384 if (!sf) { /* zero extend final result */
3385 tcg_gen_ext32u_i64(dst, dst);
3389 /* Shift a TCGv src by immediate, put result in dst.
3390 * The shift amount must be in range (this should always be true as the
3391 * relevant instructions will UNDEF on bad shift immediates).
3393 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
3394 enum a64_shift_type shift_type, unsigned int shift_i)
3396 assert(shift_i < (sf ? 64 : 32));
3398 if (shift_i == 0) {
3399 tcg_gen_mov_i64(dst, src);
3400 } else {
3401 TCGv_i64 shift_const;
3403 shift_const = tcg_const_i64(shift_i);
3404 shift_reg(dst, src, sf, shift_type, shift_const);
3405 tcg_temp_free_i64(shift_const);
3409 /* C3.5.10 Logical (shifted register)
3410 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
3411 * +----+-----+-----------+-------+---+------+--------+------+------+
3412 * | sf | opc | 0 1 0 1 0 | shift | N | Rm | imm6 | Rn | Rd |
3413 * +----+-----+-----------+-------+---+------+--------+------+------+
3415 static void disas_logic_reg(DisasContext *s, uint32_t insn)
3417 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
3418 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
3420 sf = extract32(insn, 31, 1);
3421 opc = extract32(insn, 29, 2);
3422 shift_type = extract32(insn, 22, 2);
3423 invert = extract32(insn, 21, 1);
3424 rm = extract32(insn, 16, 5);
3425 shift_amount = extract32(insn, 10, 6);
3426 rn = extract32(insn, 5, 5);
3427 rd = extract32(insn, 0, 5);
3429 if (!sf && (shift_amount & (1 << 5))) {
3430 unallocated_encoding(s);
3431 return;
3434 tcg_rd = cpu_reg(s, rd);
3436 if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
3437 /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
3438 * register-register MOV and MVN, so it is worth special casing.
3440 tcg_rm = cpu_reg(s, rm);
3441 if (invert) {
3442 tcg_gen_not_i64(tcg_rd, tcg_rm);
3443 if (!sf) {
3444 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3446 } else {
3447 if (sf) {
3448 tcg_gen_mov_i64(tcg_rd, tcg_rm);
3449 } else {
3450 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
3453 return;
3456 tcg_rm = read_cpu_reg(s, rm, sf);
3458 if (shift_amount) {
3459 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
3462 tcg_rn = cpu_reg(s, rn);
3464 switch (opc | (invert << 2)) {
3465 case 0: /* AND */
3466 case 3: /* ANDS */
3467 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
3468 break;
3469 case 1: /* ORR */
3470 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
3471 break;
3472 case 2: /* EOR */
3473 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
3474 break;
3475 case 4: /* BIC */
3476 case 7: /* BICS */
3477 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
3478 break;
3479 case 5: /* ORN */
3480 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
3481 break;
3482 case 6: /* EON */
3483 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
3484 break;
3485 default:
3486 assert(FALSE);
3487 break;
3490 if (!sf) {
3491 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3494 if (opc == 3) {
3495 gen_logic_CC(sf, tcg_rd);
3500 * C3.5.1 Add/subtract (extended register)
3502 * 31|30|29|28 24|23 22|21|20 16|15 13|12 10|9 5|4 0|
3503 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3504 * |sf|op| S| 0 1 0 1 1 | opt | 1| Rm |option| imm3 | Rn | Rd |
3505 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3507 * sf: 0 -> 32bit, 1 -> 64bit
3508 * op: 0 -> add , 1 -> sub
3509 * S: 1 -> set flags
3510 * opt: 00
3511 * option: extension type (see DecodeRegExtend)
3512 * imm3: optional shift to Rm
3514 * Rd = Rn + LSL(extend(Rm), amount)
3516 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
3518 int rd = extract32(insn, 0, 5);
3519 int rn = extract32(insn, 5, 5);
3520 int imm3 = extract32(insn, 10, 3);
3521 int option = extract32(insn, 13, 3);
3522 int rm = extract32(insn, 16, 5);
3523 bool setflags = extract32(insn, 29, 1);
3524 bool sub_op = extract32(insn, 30, 1);
3525 bool sf = extract32(insn, 31, 1);
3527 TCGv_i64 tcg_rm, tcg_rn; /* temps */
3528 TCGv_i64 tcg_rd;
3529 TCGv_i64 tcg_result;
3531 if (imm3 > 4) {
3532 unallocated_encoding(s);
3533 return;
3536 /* non-flag setting ops may use SP */
3537 if (!setflags) {
3538 tcg_rd = cpu_reg_sp(s, rd);
3539 } else {
3540 tcg_rd = cpu_reg(s, rd);
3542 tcg_rn = read_cpu_reg_sp(s, rn, sf);
3544 tcg_rm = read_cpu_reg(s, rm, sf);
3545 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
3547 tcg_result = tcg_temp_new_i64();
3549 if (!setflags) {
3550 if (sub_op) {
3551 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3552 } else {
3553 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3555 } else {
3556 if (sub_op) {
3557 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3558 } else {
3559 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3563 if (sf) {
3564 tcg_gen_mov_i64(tcg_rd, tcg_result);
3565 } else {
3566 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3569 tcg_temp_free_i64(tcg_result);
3573 * C3.5.2 Add/subtract (shifted register)
3575 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
3576 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3577 * |sf|op| S| 0 1 0 1 1 |shift| 0| Rm | imm6 | Rn | Rd |
3578 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3580 * sf: 0 -> 32bit, 1 -> 64bit
3581 * op: 0 -> add , 1 -> sub
3582 * S: 1 -> set flags
3583 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
3584 * imm6: Shift amount to apply to Rm before the add/sub
3586 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
3588 int rd = extract32(insn, 0, 5);
3589 int rn = extract32(insn, 5, 5);
3590 int imm6 = extract32(insn, 10, 6);
3591 int rm = extract32(insn, 16, 5);
3592 int shift_type = extract32(insn, 22, 2);
3593 bool setflags = extract32(insn, 29, 1);
3594 bool sub_op = extract32(insn, 30, 1);
3595 bool sf = extract32(insn, 31, 1);
3597 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3598 TCGv_i64 tcg_rn, tcg_rm;
3599 TCGv_i64 tcg_result;
3601 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
3602 unallocated_encoding(s);
3603 return;
3606 tcg_rn = read_cpu_reg(s, rn, sf);
3607 tcg_rm = read_cpu_reg(s, rm, sf);
3609 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
3611 tcg_result = tcg_temp_new_i64();
3613 if (!setflags) {
3614 if (sub_op) {
3615 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3616 } else {
3617 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3619 } else {
3620 if (sub_op) {
3621 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3622 } else {
3623 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3627 if (sf) {
3628 tcg_gen_mov_i64(tcg_rd, tcg_result);
3629 } else {
3630 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3633 tcg_temp_free_i64(tcg_result);
3636 /* C3.5.9 Data-processing (3 source)
3638 31 30 29 28 24 23 21 20 16 15 14 10 9 5 4 0
3639 +--+------+-----------+------+------+----+------+------+------+
3640 |sf| op54 | 1 1 0 1 1 | op31 | Rm | o0 | Ra | Rn | Rd |
3641 +--+------+-----------+------+------+----+------+------+------+
3644 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
3646 int rd = extract32(insn, 0, 5);
3647 int rn = extract32(insn, 5, 5);
3648 int ra = extract32(insn, 10, 5);
3649 int rm = extract32(insn, 16, 5);
3650 int op_id = (extract32(insn, 29, 3) << 4) |
3651 (extract32(insn, 21, 3) << 1) |
3652 extract32(insn, 15, 1);
3653 bool sf = extract32(insn, 31, 1);
3654 bool is_sub = extract32(op_id, 0, 1);
3655 bool is_high = extract32(op_id, 2, 1);
3656 bool is_signed = false;
3657 TCGv_i64 tcg_op1;
3658 TCGv_i64 tcg_op2;
3659 TCGv_i64 tcg_tmp;
3661 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
3662 switch (op_id) {
3663 case 0x42: /* SMADDL */
3664 case 0x43: /* SMSUBL */
3665 case 0x44: /* SMULH */
3666 is_signed = true;
3667 break;
3668 case 0x0: /* MADD (32bit) */
3669 case 0x1: /* MSUB (32bit) */
3670 case 0x40: /* MADD (64bit) */
3671 case 0x41: /* MSUB (64bit) */
3672 case 0x4a: /* UMADDL */
3673 case 0x4b: /* UMSUBL */
3674 case 0x4c: /* UMULH */
3675 break;
3676 default:
3677 unallocated_encoding(s);
3678 return;
3681 if (is_high) {
3682 TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
3683 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3684 TCGv_i64 tcg_rn = cpu_reg(s, rn);
3685 TCGv_i64 tcg_rm = cpu_reg(s, rm);
3687 if (is_signed) {
3688 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3689 } else {
3690 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3693 tcg_temp_free_i64(low_bits);
3694 return;
3697 tcg_op1 = tcg_temp_new_i64();
3698 tcg_op2 = tcg_temp_new_i64();
3699 tcg_tmp = tcg_temp_new_i64();
3701 if (op_id < 0x42) {
3702 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
3703 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
3704 } else {
3705 if (is_signed) {
3706 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
3707 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
3708 } else {
3709 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
3710 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
3714 if (ra == 31 && !is_sub) {
3715 /* Special-case MADD with rA == XZR; it is the standard MUL alias */
3716 tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
3717 } else {
3718 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
3719 if (is_sub) {
3720 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3721 } else {
3722 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3726 if (!sf) {
3727 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
3730 tcg_temp_free_i64(tcg_op1);
3731 tcg_temp_free_i64(tcg_op2);
3732 tcg_temp_free_i64(tcg_tmp);
3735 /* C3.5.3 - Add/subtract (with carry)
3736 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
3737 * +--+--+--+------------------------+------+---------+------+-----+
3738 * |sf|op| S| 1 1 0 1 0 0 0 0 | rm | opcode2 | Rn | Rd |
3739 * +--+--+--+------------------------+------+---------+------+-----+
3740 * [000000]
3743 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
3745 unsigned int sf, op, setflags, rm, rn, rd;
3746 TCGv_i64 tcg_y, tcg_rn, tcg_rd;
3748 if (extract32(insn, 10, 6) != 0) {
3749 unallocated_encoding(s);
3750 return;
3753 sf = extract32(insn, 31, 1);
3754 op = extract32(insn, 30, 1);
3755 setflags = extract32(insn, 29, 1);
3756 rm = extract32(insn, 16, 5);
3757 rn = extract32(insn, 5, 5);
3758 rd = extract32(insn, 0, 5);
3760 tcg_rd = cpu_reg(s, rd);
3761 tcg_rn = cpu_reg(s, rn);
3763 if (op) {
3764 tcg_y = new_tmp_a64(s);
3765 tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
3766 } else {
3767 tcg_y = cpu_reg(s, rm);
3770 if (setflags) {
3771 gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
3772 } else {
3773 gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
3777 /* C3.5.4 - C3.5.5 Conditional compare (immediate / register)
3778 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
3779 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3780 * |sf|op| S| 1 1 0 1 0 0 1 0 |imm5/rm | cond |i/r |o2| Rn |o3|nzcv |
3781 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3782 * [1] y [0] [0]
3784 static void disas_cc(DisasContext *s, uint32_t insn)
3786 unsigned int sf, op, y, cond, rn, nzcv, is_imm;
3787 TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
3788 TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
3789 DisasCompare c;
3791 if (!extract32(insn, 29, 1)) {
3792 unallocated_encoding(s);
3793 return;
3795 if (insn & (1 << 10 | 1 << 4)) {
3796 unallocated_encoding(s);
3797 return;
3799 sf = extract32(insn, 31, 1);
3800 op = extract32(insn, 30, 1);
3801 is_imm = extract32(insn, 11, 1);
3802 y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
3803 cond = extract32(insn, 12, 4);
3804 rn = extract32(insn, 5, 5);
3805 nzcv = extract32(insn, 0, 4);
3807 /* Set T0 = !COND. */
3808 tcg_t0 = tcg_temp_new_i32();
3809 arm_test_cc(&c, cond);
3810 tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
3811 arm_free_cc(&c);
3813 /* Load the arguments for the new comparison. */
3814 if (is_imm) {
3815 tcg_y = new_tmp_a64(s);
3816 tcg_gen_movi_i64(tcg_y, y);
3817 } else {
3818 tcg_y = cpu_reg(s, y);
3820 tcg_rn = cpu_reg(s, rn);
3822 /* Set the flags for the new comparison. */
3823 tcg_tmp = tcg_temp_new_i64();
3824 if (op) {
3825 gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3826 } else {
3827 gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3829 tcg_temp_free_i64(tcg_tmp);
3831 /* If COND was false, force the flags to #nzcv. Compute two masks
3832 * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
3833 * For tcg hosts that support ANDC, we can make do with just T1.
3834 * In either case, allow the tcg optimizer to delete any unused mask.
3836 tcg_t1 = tcg_temp_new_i32();
3837 tcg_t2 = tcg_temp_new_i32();
3838 tcg_gen_neg_i32(tcg_t1, tcg_t0);
3839 tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
3841 if (nzcv & 8) { /* N */
3842 tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
3843 } else {
3844 if (TCG_TARGET_HAS_andc_i32) {
3845 tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
3846 } else {
3847 tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
3850 if (nzcv & 4) { /* Z */
3851 if (TCG_TARGET_HAS_andc_i32) {
3852 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
3853 } else {
3854 tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
3856 } else {
3857 tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
3859 if (nzcv & 2) { /* C */
3860 tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
3861 } else {
3862 if (TCG_TARGET_HAS_andc_i32) {
3863 tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
3864 } else {
3865 tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
3868 if (nzcv & 1) { /* V */
3869 tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
3870 } else {
3871 if (TCG_TARGET_HAS_andc_i32) {
3872 tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
3873 } else {
3874 tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
3877 tcg_temp_free_i32(tcg_t0);
3878 tcg_temp_free_i32(tcg_t1);
3879 tcg_temp_free_i32(tcg_t2);
3882 /* C3.5.6 Conditional select
3883 * 31 30 29 28 21 20 16 15 12 11 10 9 5 4 0
3884 * +----+----+---+-----------------+------+------+-----+------+------+
3885 * | sf | op | S | 1 1 0 1 0 1 0 0 | Rm | cond | op2 | Rn | Rd |
3886 * +----+----+---+-----------------+------+------+-----+------+------+
3888 static void disas_cond_select(DisasContext *s, uint32_t insn)
3890 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
3891 TCGv_i64 tcg_rd, zero;
3892 DisasCompare64 c;
3894 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
3895 /* S == 1 or op2<1> == 1 */
3896 unallocated_encoding(s);
3897 return;
3899 sf = extract32(insn, 31, 1);
3900 else_inv = extract32(insn, 30, 1);
3901 rm = extract32(insn, 16, 5);
3902 cond = extract32(insn, 12, 4);
3903 else_inc = extract32(insn, 10, 1);
3904 rn = extract32(insn, 5, 5);
3905 rd = extract32(insn, 0, 5);
3907 tcg_rd = cpu_reg(s, rd);
3909 a64_test_cc(&c, cond);
3910 zero = tcg_const_i64(0);
3912 if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
3913 /* CSET & CSETM. */
3914 tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
3915 if (else_inv) {
3916 tcg_gen_neg_i64(tcg_rd, tcg_rd);
3918 } else {
3919 TCGv_i64 t_true = cpu_reg(s, rn);
3920 TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
3921 if (else_inv && else_inc) {
3922 tcg_gen_neg_i64(t_false, t_false);
3923 } else if (else_inv) {
3924 tcg_gen_not_i64(t_false, t_false);
3925 } else if (else_inc) {
3926 tcg_gen_addi_i64(t_false, t_false, 1);
3928 tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
3931 tcg_temp_free_i64(zero);
3932 a64_free_cc(&c);
3934 if (!sf) {
3935 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3939 static void handle_clz(DisasContext *s, unsigned int sf,
3940 unsigned int rn, unsigned int rd)
3942 TCGv_i64 tcg_rd, tcg_rn;
3943 tcg_rd = cpu_reg(s, rd);
3944 tcg_rn = cpu_reg(s, rn);
3946 if (sf) {
3947 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
3948 } else {
3949 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3950 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
3951 tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
3952 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3953 tcg_temp_free_i32(tcg_tmp32);
3957 static void handle_cls(DisasContext *s, unsigned int sf,
3958 unsigned int rn, unsigned int rd)
3960 TCGv_i64 tcg_rd, tcg_rn;
3961 tcg_rd = cpu_reg(s, rd);
3962 tcg_rn = cpu_reg(s, rn);
3964 if (sf) {
3965 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
3966 } else {
3967 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3968 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
3969 tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
3970 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3971 tcg_temp_free_i32(tcg_tmp32);
3975 static void handle_rbit(DisasContext *s, unsigned int sf,
3976 unsigned int rn, unsigned int rd)
3978 TCGv_i64 tcg_rd, tcg_rn;
3979 tcg_rd = cpu_reg(s, rd);
3980 tcg_rn = cpu_reg(s, rn);
3982 if (sf) {
3983 gen_helper_rbit64(tcg_rd, tcg_rn);
3984 } else {
3985 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3986 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
3987 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
3988 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3989 tcg_temp_free_i32(tcg_tmp32);
3993 /* C5.6.149 REV with sf==1, opcode==3 ("REV64") */
3994 static void handle_rev64(DisasContext *s, unsigned int sf,
3995 unsigned int rn, unsigned int rd)
3997 if (!sf) {
3998 unallocated_encoding(s);
3999 return;
4001 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
4004 /* C5.6.149 REV with sf==0, opcode==2
4005 * C5.6.151 REV32 (sf==1, opcode==2)
4007 static void handle_rev32(DisasContext *s, unsigned int sf,
4008 unsigned int rn, unsigned int rd)
4010 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4012 if (sf) {
4013 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4014 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4016 /* bswap32_i64 requires zero high word */
4017 tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
4018 tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
4019 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
4020 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
4021 tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
4023 tcg_temp_free_i64(tcg_tmp);
4024 } else {
4025 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
4026 tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
4030 /* C5.6.150 REV16 (opcode==1) */
4031 static void handle_rev16(DisasContext *s, unsigned int sf,
4032 unsigned int rn, unsigned int rd)
4034 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4035 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4036 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4038 tcg_gen_andi_i64(tcg_tmp, tcg_rn, 0xffff);
4039 tcg_gen_bswap16_i64(tcg_rd, tcg_tmp);
4041 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 16);
4042 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
4043 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
4044 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 16, 16);
4046 if (sf) {
4047 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
4048 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
4049 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
4050 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 32, 16);
4052 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 48);
4053 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
4054 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 48, 16);
4057 tcg_temp_free_i64(tcg_tmp);
4060 /* C3.5.7 Data-processing (1 source)
4061 * 31 30 29 28 21 20 16 15 10 9 5 4 0
4062 * +----+---+---+-----------------+---------+--------+------+------+
4063 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode | Rn | Rd |
4064 * +----+---+---+-----------------+---------+--------+------+------+
4066 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
4068 unsigned int sf, opcode, rn, rd;
4070 if (extract32(insn, 29, 1) || extract32(insn, 16, 5)) {
4071 unallocated_encoding(s);
4072 return;
4075 sf = extract32(insn, 31, 1);
4076 opcode = extract32(insn, 10, 6);
4077 rn = extract32(insn, 5, 5);
4078 rd = extract32(insn, 0, 5);
4080 switch (opcode) {
4081 case 0: /* RBIT */
4082 handle_rbit(s, sf, rn, rd);
4083 break;
4084 case 1: /* REV16 */
4085 handle_rev16(s, sf, rn, rd);
4086 break;
4087 case 2: /* REV32 */
4088 handle_rev32(s, sf, rn, rd);
4089 break;
4090 case 3: /* REV64 */
4091 handle_rev64(s, sf, rn, rd);
4092 break;
4093 case 4: /* CLZ */
4094 handle_clz(s, sf, rn, rd);
4095 break;
4096 case 5: /* CLS */
4097 handle_cls(s, sf, rn, rd);
4098 break;
4102 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
4103 unsigned int rm, unsigned int rn, unsigned int rd)
4105 TCGv_i64 tcg_n, tcg_m, tcg_rd;
4106 tcg_rd = cpu_reg(s, rd);
4108 if (!sf && is_signed) {
4109 tcg_n = new_tmp_a64(s);
4110 tcg_m = new_tmp_a64(s);
4111 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
4112 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
4113 } else {
4114 tcg_n = read_cpu_reg(s, rn, sf);
4115 tcg_m = read_cpu_reg(s, rm, sf);
4118 if (is_signed) {
4119 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
4120 } else {
4121 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
4124 if (!sf) { /* zero extend final result */
4125 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4129 /* C5.6.115 LSLV, C5.6.118 LSRV, C5.6.17 ASRV, C5.6.154 RORV */
4130 static void handle_shift_reg(DisasContext *s,
4131 enum a64_shift_type shift_type, unsigned int sf,
4132 unsigned int rm, unsigned int rn, unsigned int rd)
4134 TCGv_i64 tcg_shift = tcg_temp_new_i64();
4135 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4136 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4138 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
4139 shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
4140 tcg_temp_free_i64(tcg_shift);
4143 /* CRC32[BHWX], CRC32C[BHWX] */
4144 static void handle_crc32(DisasContext *s,
4145 unsigned int sf, unsigned int sz, bool crc32c,
4146 unsigned int rm, unsigned int rn, unsigned int rd)
4148 TCGv_i64 tcg_acc, tcg_val;
4149 TCGv_i32 tcg_bytes;
4151 if (!arm_dc_feature(s, ARM_FEATURE_CRC)
4152 || (sf == 1 && sz != 3)
4153 || (sf == 0 && sz == 3)) {
4154 unallocated_encoding(s);
4155 return;
4158 if (sz == 3) {
4159 tcg_val = cpu_reg(s, rm);
4160 } else {
4161 uint64_t mask;
4162 switch (sz) {
4163 case 0:
4164 mask = 0xFF;
4165 break;
4166 case 1:
4167 mask = 0xFFFF;
4168 break;
4169 case 2:
4170 mask = 0xFFFFFFFF;
4171 break;
4172 default:
4173 g_assert_not_reached();
4175 tcg_val = new_tmp_a64(s);
4176 tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
4179 tcg_acc = cpu_reg(s, rn);
4180 tcg_bytes = tcg_const_i32(1 << sz);
4182 if (crc32c) {
4183 gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
4184 } else {
4185 gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
4188 tcg_temp_free_i32(tcg_bytes);
4191 /* C3.5.8 Data-processing (2 source)
4192 * 31 30 29 28 21 20 16 15 10 9 5 4 0
4193 * +----+---+---+-----------------+------+--------+------+------+
4194 * | sf | 0 | S | 1 1 0 1 0 1 1 0 | Rm | opcode | Rn | Rd |
4195 * +----+---+---+-----------------+------+--------+------+------+
4197 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
4199 unsigned int sf, rm, opcode, rn, rd;
4200 sf = extract32(insn, 31, 1);
4201 rm = extract32(insn, 16, 5);
4202 opcode = extract32(insn, 10, 6);
4203 rn = extract32(insn, 5, 5);
4204 rd = extract32(insn, 0, 5);
4206 if (extract32(insn, 29, 1)) {
4207 unallocated_encoding(s);
4208 return;
4211 switch (opcode) {
4212 case 2: /* UDIV */
4213 handle_div(s, false, sf, rm, rn, rd);
4214 break;
4215 case 3: /* SDIV */
4216 handle_div(s, true, sf, rm, rn, rd);
4217 break;
4218 case 8: /* LSLV */
4219 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
4220 break;
4221 case 9: /* LSRV */
4222 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
4223 break;
4224 case 10: /* ASRV */
4225 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
4226 break;
4227 case 11: /* RORV */
4228 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
4229 break;
4230 case 16:
4231 case 17:
4232 case 18:
4233 case 19:
4234 case 20:
4235 case 21:
4236 case 22:
4237 case 23: /* CRC32 */
4239 int sz = extract32(opcode, 0, 2);
4240 bool crc32c = extract32(opcode, 2, 1);
4241 handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
4242 break;
4244 default:
4245 unallocated_encoding(s);
4246 break;
4250 /* C3.5 Data processing - register */
4251 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
4253 switch (extract32(insn, 24, 5)) {
4254 case 0x0a: /* Logical (shifted register) */
4255 disas_logic_reg(s, insn);
4256 break;
4257 case 0x0b: /* Add/subtract */
4258 if (insn & (1 << 21)) { /* (extended register) */
4259 disas_add_sub_ext_reg(s, insn);
4260 } else {
4261 disas_add_sub_reg(s, insn);
4263 break;
4264 case 0x1b: /* Data-processing (3 source) */
4265 disas_data_proc_3src(s, insn);
4266 break;
4267 case 0x1a:
4268 switch (extract32(insn, 21, 3)) {
4269 case 0x0: /* Add/subtract (with carry) */
4270 disas_adc_sbc(s, insn);
4271 break;
4272 case 0x2: /* Conditional compare */
4273 disas_cc(s, insn); /* both imm and reg forms */
4274 break;
4275 case 0x4: /* Conditional select */
4276 disas_cond_select(s, insn);
4277 break;
4278 case 0x6: /* Data-processing */
4279 if (insn & (1 << 30)) { /* (1 source) */
4280 disas_data_proc_1src(s, insn);
4281 } else { /* (2 source) */
4282 disas_data_proc_2src(s, insn);
4284 break;
4285 default:
4286 unallocated_encoding(s);
4287 break;
4289 break;
4290 default:
4291 unallocated_encoding(s);
4292 break;
4296 static void handle_fp_compare(DisasContext *s, bool is_double,
4297 unsigned int rn, unsigned int rm,
4298 bool cmp_with_zero, bool signal_all_nans)
4300 TCGv_i64 tcg_flags = tcg_temp_new_i64();
4301 TCGv_ptr fpst = get_fpstatus_ptr();
4303 if (is_double) {
4304 TCGv_i64 tcg_vn, tcg_vm;
4306 tcg_vn = read_fp_dreg(s, rn);
4307 if (cmp_with_zero) {
4308 tcg_vm = tcg_const_i64(0);
4309 } else {
4310 tcg_vm = read_fp_dreg(s, rm);
4312 if (signal_all_nans) {
4313 gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4314 } else {
4315 gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4317 tcg_temp_free_i64(tcg_vn);
4318 tcg_temp_free_i64(tcg_vm);
4319 } else {
4320 TCGv_i32 tcg_vn, tcg_vm;
4322 tcg_vn = read_fp_sreg(s, rn);
4323 if (cmp_with_zero) {
4324 tcg_vm = tcg_const_i32(0);
4325 } else {
4326 tcg_vm = read_fp_sreg(s, rm);
4328 if (signal_all_nans) {
4329 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4330 } else {
4331 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4333 tcg_temp_free_i32(tcg_vn);
4334 tcg_temp_free_i32(tcg_vm);
4337 tcg_temp_free_ptr(fpst);
4339 gen_set_nzcv(tcg_flags);
4341 tcg_temp_free_i64(tcg_flags);
4344 /* C3.6.22 Floating point compare
4345 * 31 30 29 28 24 23 22 21 20 16 15 14 13 10 9 5 4 0
4346 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
4347 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | op | 1 0 0 0 | Rn | op2 |
4348 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
4350 static void disas_fp_compare(DisasContext *s, uint32_t insn)
4352 unsigned int mos, type, rm, op, rn, opc, op2r;
4354 mos = extract32(insn, 29, 3);
4355 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4356 rm = extract32(insn, 16, 5);
4357 op = extract32(insn, 14, 2);
4358 rn = extract32(insn, 5, 5);
4359 opc = extract32(insn, 3, 2);
4360 op2r = extract32(insn, 0, 3);
4362 if (mos || op || op2r || type > 1) {
4363 unallocated_encoding(s);
4364 return;
4367 if (!fp_access_check(s)) {
4368 return;
4371 handle_fp_compare(s, type, rn, rm, opc & 1, opc & 2);
4374 /* C3.6.23 Floating point conditional compare
4375 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
4376 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
4377 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 0 1 | Rn | op | nzcv |
4378 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
4380 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
4382 unsigned int mos, type, rm, cond, rn, op, nzcv;
4383 TCGv_i64 tcg_flags;
4384 TCGLabel *label_continue = NULL;
4386 mos = extract32(insn, 29, 3);
4387 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4388 rm = extract32(insn, 16, 5);
4389 cond = extract32(insn, 12, 4);
4390 rn = extract32(insn, 5, 5);
4391 op = extract32(insn, 4, 1);
4392 nzcv = extract32(insn, 0, 4);
4394 if (mos || type > 1) {
4395 unallocated_encoding(s);
4396 return;
4399 if (!fp_access_check(s)) {
4400 return;
4403 if (cond < 0x0e) { /* not always */
4404 TCGLabel *label_match = gen_new_label();
4405 label_continue = gen_new_label();
4406 arm_gen_test_cc(cond, label_match);
4407 /* nomatch: */
4408 tcg_flags = tcg_const_i64(nzcv << 28);
4409 gen_set_nzcv(tcg_flags);
4410 tcg_temp_free_i64(tcg_flags);
4411 tcg_gen_br(label_continue);
4412 gen_set_label(label_match);
4415 handle_fp_compare(s, type, rn, rm, false, op);
4417 if (cond < 0x0e) {
4418 gen_set_label(label_continue);
4422 /* C3.6.24 Floating point conditional select
4423 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
4424 * +---+---+---+-----------+------+---+------+------+-----+------+------+
4425 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 1 1 | Rn | Rd |
4426 * +---+---+---+-----------+------+---+------+------+-----+------+------+
4428 static void disas_fp_csel(DisasContext *s, uint32_t insn)
4430 unsigned int mos, type, rm, cond, rn, rd;
4431 TCGv_i64 t_true, t_false, t_zero;
4432 DisasCompare64 c;
4434 mos = extract32(insn, 29, 3);
4435 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4436 rm = extract32(insn, 16, 5);
4437 cond = extract32(insn, 12, 4);
4438 rn = extract32(insn, 5, 5);
4439 rd = extract32(insn, 0, 5);
4441 if (mos || type > 1) {
4442 unallocated_encoding(s);
4443 return;
4446 if (!fp_access_check(s)) {
4447 return;
4450 /* Zero extend sreg inputs to 64 bits now. */
4451 t_true = tcg_temp_new_i64();
4452 t_false = tcg_temp_new_i64();
4453 read_vec_element(s, t_true, rn, 0, type ? MO_64 : MO_32);
4454 read_vec_element(s, t_false, rm, 0, type ? MO_64 : MO_32);
4456 a64_test_cc(&c, cond);
4457 t_zero = tcg_const_i64(0);
4458 tcg_gen_movcond_i64(c.cond, t_true, c.value, t_zero, t_true, t_false);
4459 tcg_temp_free_i64(t_zero);
4460 tcg_temp_free_i64(t_false);
4461 a64_free_cc(&c);
4463 /* Note that sregs write back zeros to the high bits,
4464 and we've already done the zero-extension. */
4465 write_fp_dreg(s, rd, t_true);
4466 tcg_temp_free_i64(t_true);
4469 /* C3.6.25 Floating-point data-processing (1 source) - single precision */
4470 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
4472 TCGv_ptr fpst;
4473 TCGv_i32 tcg_op;
4474 TCGv_i32 tcg_res;
4476 fpst = get_fpstatus_ptr();
4477 tcg_op = read_fp_sreg(s, rn);
4478 tcg_res = tcg_temp_new_i32();
4480 switch (opcode) {
4481 case 0x0: /* FMOV */
4482 tcg_gen_mov_i32(tcg_res, tcg_op);
4483 break;
4484 case 0x1: /* FABS */
4485 gen_helper_vfp_abss(tcg_res, tcg_op);
4486 break;
4487 case 0x2: /* FNEG */
4488 gen_helper_vfp_negs(tcg_res, tcg_op);
4489 break;
4490 case 0x3: /* FSQRT */
4491 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
4492 break;
4493 case 0x8: /* FRINTN */
4494 case 0x9: /* FRINTP */
4495 case 0xa: /* FRINTM */
4496 case 0xb: /* FRINTZ */
4497 case 0xc: /* FRINTA */
4499 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4501 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4502 gen_helper_rints(tcg_res, tcg_op, fpst);
4504 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4505 tcg_temp_free_i32(tcg_rmode);
4506 break;
4508 case 0xe: /* FRINTX */
4509 gen_helper_rints_exact(tcg_res, tcg_op, fpst);
4510 break;
4511 case 0xf: /* FRINTI */
4512 gen_helper_rints(tcg_res, tcg_op, fpst);
4513 break;
4514 default:
4515 abort();
4518 write_fp_sreg(s, rd, tcg_res);
4520 tcg_temp_free_ptr(fpst);
4521 tcg_temp_free_i32(tcg_op);
4522 tcg_temp_free_i32(tcg_res);
4525 /* C3.6.25 Floating-point data-processing (1 source) - double precision */
4526 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
4528 TCGv_ptr fpst;
4529 TCGv_i64 tcg_op;
4530 TCGv_i64 tcg_res;
4532 fpst = get_fpstatus_ptr();
4533 tcg_op = read_fp_dreg(s, rn);
4534 tcg_res = tcg_temp_new_i64();
4536 switch (opcode) {
4537 case 0x0: /* FMOV */
4538 tcg_gen_mov_i64(tcg_res, tcg_op);
4539 break;
4540 case 0x1: /* FABS */
4541 gen_helper_vfp_absd(tcg_res, tcg_op);
4542 break;
4543 case 0x2: /* FNEG */
4544 gen_helper_vfp_negd(tcg_res, tcg_op);
4545 break;
4546 case 0x3: /* FSQRT */
4547 gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
4548 break;
4549 case 0x8: /* FRINTN */
4550 case 0x9: /* FRINTP */
4551 case 0xa: /* FRINTM */
4552 case 0xb: /* FRINTZ */
4553 case 0xc: /* FRINTA */
4555 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4557 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4558 gen_helper_rintd(tcg_res, tcg_op, fpst);
4560 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4561 tcg_temp_free_i32(tcg_rmode);
4562 break;
4564 case 0xe: /* FRINTX */
4565 gen_helper_rintd_exact(tcg_res, tcg_op, fpst);
4566 break;
4567 case 0xf: /* FRINTI */
4568 gen_helper_rintd(tcg_res, tcg_op, fpst);
4569 break;
4570 default:
4571 abort();
4574 write_fp_dreg(s, rd, tcg_res);
4576 tcg_temp_free_ptr(fpst);
4577 tcg_temp_free_i64(tcg_op);
4578 tcg_temp_free_i64(tcg_res);
4581 static void handle_fp_fcvt(DisasContext *s, int opcode,
4582 int rd, int rn, int dtype, int ntype)
4584 switch (ntype) {
4585 case 0x0:
4587 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4588 if (dtype == 1) {
4589 /* Single to double */
4590 TCGv_i64 tcg_rd = tcg_temp_new_i64();
4591 gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
4592 write_fp_dreg(s, rd, tcg_rd);
4593 tcg_temp_free_i64(tcg_rd);
4594 } else {
4595 /* Single to half */
4596 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4597 gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, cpu_env);
4598 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4599 write_fp_sreg(s, rd, tcg_rd);
4600 tcg_temp_free_i32(tcg_rd);
4602 tcg_temp_free_i32(tcg_rn);
4603 break;
4605 case 0x1:
4607 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
4608 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4609 if (dtype == 0) {
4610 /* Double to single */
4611 gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
4612 } else {
4613 /* Double to half */
4614 gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, cpu_env);
4615 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4617 write_fp_sreg(s, rd, tcg_rd);
4618 tcg_temp_free_i32(tcg_rd);
4619 tcg_temp_free_i64(tcg_rn);
4620 break;
4622 case 0x3:
4624 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4625 tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
4626 if (dtype == 0) {
4627 /* Half to single */
4628 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4629 gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, cpu_env);
4630 write_fp_sreg(s, rd, tcg_rd);
4631 tcg_temp_free_i32(tcg_rd);
4632 } else {
4633 /* Half to double */
4634 TCGv_i64 tcg_rd = tcg_temp_new_i64();
4635 gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, cpu_env);
4636 write_fp_dreg(s, rd, tcg_rd);
4637 tcg_temp_free_i64(tcg_rd);
4639 tcg_temp_free_i32(tcg_rn);
4640 break;
4642 default:
4643 abort();
4647 /* C3.6.25 Floating point data-processing (1 source)
4648 * 31 30 29 28 24 23 22 21 20 15 14 10 9 5 4 0
4649 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4650 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 | Rn | Rd |
4651 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4653 static void disas_fp_1src(DisasContext *s, uint32_t insn)
4655 int type = extract32(insn, 22, 2);
4656 int opcode = extract32(insn, 15, 6);
4657 int rn = extract32(insn, 5, 5);
4658 int rd = extract32(insn, 0, 5);
4660 switch (opcode) {
4661 case 0x4: case 0x5: case 0x7:
4663 /* FCVT between half, single and double precision */
4664 int dtype = extract32(opcode, 0, 2);
4665 if (type == 2 || dtype == type) {
4666 unallocated_encoding(s);
4667 return;
4669 if (!fp_access_check(s)) {
4670 return;
4673 handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
4674 break;
4676 case 0x0 ... 0x3:
4677 case 0x8 ... 0xc:
4678 case 0xe ... 0xf:
4679 /* 32-to-32 and 64-to-64 ops */
4680 switch (type) {
4681 case 0:
4682 if (!fp_access_check(s)) {
4683 return;
4686 handle_fp_1src_single(s, opcode, rd, rn);
4687 break;
4688 case 1:
4689 if (!fp_access_check(s)) {
4690 return;
4693 handle_fp_1src_double(s, opcode, rd, rn);
4694 break;
4695 default:
4696 unallocated_encoding(s);
4698 break;
4699 default:
4700 unallocated_encoding(s);
4701 break;
4705 /* C3.6.26 Floating-point data-processing (2 source) - single precision */
4706 static void handle_fp_2src_single(DisasContext *s, int opcode,
4707 int rd, int rn, int rm)
4709 TCGv_i32 tcg_op1;
4710 TCGv_i32 tcg_op2;
4711 TCGv_i32 tcg_res;
4712 TCGv_ptr fpst;
4714 tcg_res = tcg_temp_new_i32();
4715 fpst = get_fpstatus_ptr();
4716 tcg_op1 = read_fp_sreg(s, rn);
4717 tcg_op2 = read_fp_sreg(s, rm);
4719 switch (opcode) {
4720 case 0x0: /* FMUL */
4721 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4722 break;
4723 case 0x1: /* FDIV */
4724 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
4725 break;
4726 case 0x2: /* FADD */
4727 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
4728 break;
4729 case 0x3: /* FSUB */
4730 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
4731 break;
4732 case 0x4: /* FMAX */
4733 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
4734 break;
4735 case 0x5: /* FMIN */
4736 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
4737 break;
4738 case 0x6: /* FMAXNM */
4739 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
4740 break;
4741 case 0x7: /* FMINNM */
4742 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
4743 break;
4744 case 0x8: /* FNMUL */
4745 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4746 gen_helper_vfp_negs(tcg_res, tcg_res);
4747 break;
4750 write_fp_sreg(s, rd, tcg_res);
4752 tcg_temp_free_ptr(fpst);
4753 tcg_temp_free_i32(tcg_op1);
4754 tcg_temp_free_i32(tcg_op2);
4755 tcg_temp_free_i32(tcg_res);
4758 /* C3.6.26 Floating-point data-processing (2 source) - double precision */
4759 static void handle_fp_2src_double(DisasContext *s, int opcode,
4760 int rd, int rn, int rm)
4762 TCGv_i64 tcg_op1;
4763 TCGv_i64 tcg_op2;
4764 TCGv_i64 tcg_res;
4765 TCGv_ptr fpst;
4767 tcg_res = tcg_temp_new_i64();
4768 fpst = get_fpstatus_ptr();
4769 tcg_op1 = read_fp_dreg(s, rn);
4770 tcg_op2 = read_fp_dreg(s, rm);
4772 switch (opcode) {
4773 case 0x0: /* FMUL */
4774 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4775 break;
4776 case 0x1: /* FDIV */
4777 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
4778 break;
4779 case 0x2: /* FADD */
4780 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
4781 break;
4782 case 0x3: /* FSUB */
4783 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
4784 break;
4785 case 0x4: /* FMAX */
4786 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
4787 break;
4788 case 0x5: /* FMIN */
4789 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
4790 break;
4791 case 0x6: /* FMAXNM */
4792 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4793 break;
4794 case 0x7: /* FMINNM */
4795 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4796 break;
4797 case 0x8: /* FNMUL */
4798 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4799 gen_helper_vfp_negd(tcg_res, tcg_res);
4800 break;
4803 write_fp_dreg(s, rd, tcg_res);
4805 tcg_temp_free_ptr(fpst);
4806 tcg_temp_free_i64(tcg_op1);
4807 tcg_temp_free_i64(tcg_op2);
4808 tcg_temp_free_i64(tcg_res);
4811 /* C3.6.26 Floating point data-processing (2 source)
4812 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
4813 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4814 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | opcode | 1 0 | Rn | Rd |
4815 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4817 static void disas_fp_2src(DisasContext *s, uint32_t insn)
4819 int type = extract32(insn, 22, 2);
4820 int rd = extract32(insn, 0, 5);
4821 int rn = extract32(insn, 5, 5);
4822 int rm = extract32(insn, 16, 5);
4823 int opcode = extract32(insn, 12, 4);
4825 if (opcode > 8) {
4826 unallocated_encoding(s);
4827 return;
4830 switch (type) {
4831 case 0:
4832 if (!fp_access_check(s)) {
4833 return;
4835 handle_fp_2src_single(s, opcode, rd, rn, rm);
4836 break;
4837 case 1:
4838 if (!fp_access_check(s)) {
4839 return;
4841 handle_fp_2src_double(s, opcode, rd, rn, rm);
4842 break;
4843 default:
4844 unallocated_encoding(s);
4848 /* C3.6.27 Floating-point data-processing (3 source) - single precision */
4849 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
4850 int rd, int rn, int rm, int ra)
4852 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
4853 TCGv_i32 tcg_res = tcg_temp_new_i32();
4854 TCGv_ptr fpst = get_fpstatus_ptr();
4856 tcg_op1 = read_fp_sreg(s, rn);
4857 tcg_op2 = read_fp_sreg(s, rm);
4858 tcg_op3 = read_fp_sreg(s, ra);
4860 /* These are fused multiply-add, and must be done as one
4861 * floating point operation with no rounding between the
4862 * multiplication and addition steps.
4863 * NB that doing the negations here as separate steps is
4864 * correct : an input NaN should come out with its sign bit
4865 * flipped if it is a negated-input.
4867 if (o1 == true) {
4868 gen_helper_vfp_negs(tcg_op3, tcg_op3);
4871 if (o0 != o1) {
4872 gen_helper_vfp_negs(tcg_op1, tcg_op1);
4875 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4877 write_fp_sreg(s, rd, tcg_res);
4879 tcg_temp_free_ptr(fpst);
4880 tcg_temp_free_i32(tcg_op1);
4881 tcg_temp_free_i32(tcg_op2);
4882 tcg_temp_free_i32(tcg_op3);
4883 tcg_temp_free_i32(tcg_res);
4886 /* C3.6.27 Floating-point data-processing (3 source) - double precision */
4887 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
4888 int rd, int rn, int rm, int ra)
4890 TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
4891 TCGv_i64 tcg_res = tcg_temp_new_i64();
4892 TCGv_ptr fpst = get_fpstatus_ptr();
4894 tcg_op1 = read_fp_dreg(s, rn);
4895 tcg_op2 = read_fp_dreg(s, rm);
4896 tcg_op3 = read_fp_dreg(s, ra);
4898 /* These are fused multiply-add, and must be done as one
4899 * floating point operation with no rounding between the
4900 * multiplication and addition steps.
4901 * NB that doing the negations here as separate steps is
4902 * correct : an input NaN should come out with its sign bit
4903 * flipped if it is a negated-input.
4905 if (o1 == true) {
4906 gen_helper_vfp_negd(tcg_op3, tcg_op3);
4909 if (o0 != o1) {
4910 gen_helper_vfp_negd(tcg_op1, tcg_op1);
4913 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4915 write_fp_dreg(s, rd, tcg_res);
4917 tcg_temp_free_ptr(fpst);
4918 tcg_temp_free_i64(tcg_op1);
4919 tcg_temp_free_i64(tcg_op2);
4920 tcg_temp_free_i64(tcg_op3);
4921 tcg_temp_free_i64(tcg_res);
4924 /* C3.6.27 Floating point data-processing (3 source)
4925 * 31 30 29 28 24 23 22 21 20 16 15 14 10 9 5 4 0
4926 * +---+---+---+-----------+------+----+------+----+------+------+------+
4927 * | M | 0 | S | 1 1 1 1 1 | type | o1 | Rm | o0 | Ra | Rn | Rd |
4928 * +---+---+---+-----------+------+----+------+----+------+------+------+
4930 static void disas_fp_3src(DisasContext *s, uint32_t insn)
4932 int type = extract32(insn, 22, 2);
4933 int rd = extract32(insn, 0, 5);
4934 int rn = extract32(insn, 5, 5);
4935 int ra = extract32(insn, 10, 5);
4936 int rm = extract32(insn, 16, 5);
4937 bool o0 = extract32(insn, 15, 1);
4938 bool o1 = extract32(insn, 21, 1);
4940 switch (type) {
4941 case 0:
4942 if (!fp_access_check(s)) {
4943 return;
4945 handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
4946 break;
4947 case 1:
4948 if (!fp_access_check(s)) {
4949 return;
4951 handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
4952 break;
4953 default:
4954 unallocated_encoding(s);
4958 /* C3.6.28 Floating point immediate
4959 * 31 30 29 28 24 23 22 21 20 13 12 10 9 5 4 0
4960 * +---+---+---+-----------+------+---+------------+-------+------+------+
4961 * | M | 0 | S | 1 1 1 1 0 | type | 1 | imm8 | 1 0 0 | imm5 | Rd |
4962 * +---+---+---+-----------+------+---+------------+-------+------+------+
4964 static void disas_fp_imm(DisasContext *s, uint32_t insn)
4966 int rd = extract32(insn, 0, 5);
4967 int imm8 = extract32(insn, 13, 8);
4968 int is_double = extract32(insn, 22, 2);
4969 uint64_t imm;
4970 TCGv_i64 tcg_res;
4972 if (is_double > 1) {
4973 unallocated_encoding(s);
4974 return;
4977 if (!fp_access_check(s)) {
4978 return;
4981 /* The imm8 encodes the sign bit, enough bits to represent
4982 * an exponent in the range 01....1xx to 10....0xx,
4983 * and the most significant 4 bits of the mantissa; see
4984 * VFPExpandImm() in the v8 ARM ARM.
4986 if (is_double) {
4987 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
4988 (extract32(imm8, 6, 1) ? 0x3fc0 : 0x4000) |
4989 extract32(imm8, 0, 6);
4990 imm <<= 48;
4991 } else {
4992 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
4993 (extract32(imm8, 6, 1) ? 0x3e00 : 0x4000) |
4994 (extract32(imm8, 0, 6) << 3);
4995 imm <<= 16;
4998 tcg_res = tcg_const_i64(imm);
4999 write_fp_dreg(s, rd, tcg_res);
5000 tcg_temp_free_i64(tcg_res);
5003 /* Handle floating point <=> fixed point conversions. Note that we can
5004 * also deal with fp <=> integer conversions as a special case (scale == 64)
5005 * OPTME: consider handling that special case specially or at least skipping
5006 * the call to scalbn in the helpers for zero shifts.
5008 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
5009 bool itof, int rmode, int scale, int sf, int type)
5011 bool is_signed = !(opcode & 1);
5012 bool is_double = type;
5013 TCGv_ptr tcg_fpstatus;
5014 TCGv_i32 tcg_shift;
5016 tcg_fpstatus = get_fpstatus_ptr();
5018 tcg_shift = tcg_const_i32(64 - scale);
5020 if (itof) {
5021 TCGv_i64 tcg_int = cpu_reg(s, rn);
5022 if (!sf) {
5023 TCGv_i64 tcg_extend = new_tmp_a64(s);
5025 if (is_signed) {
5026 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
5027 } else {
5028 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
5031 tcg_int = tcg_extend;
5034 if (is_double) {
5035 TCGv_i64 tcg_double = tcg_temp_new_i64();
5036 if (is_signed) {
5037 gen_helper_vfp_sqtod(tcg_double, tcg_int,
5038 tcg_shift, tcg_fpstatus);
5039 } else {
5040 gen_helper_vfp_uqtod(tcg_double, tcg_int,
5041 tcg_shift, tcg_fpstatus);
5043 write_fp_dreg(s, rd, tcg_double);
5044 tcg_temp_free_i64(tcg_double);
5045 } else {
5046 TCGv_i32 tcg_single = tcg_temp_new_i32();
5047 if (is_signed) {
5048 gen_helper_vfp_sqtos(tcg_single, tcg_int,
5049 tcg_shift, tcg_fpstatus);
5050 } else {
5051 gen_helper_vfp_uqtos(tcg_single, tcg_int,
5052 tcg_shift, tcg_fpstatus);
5054 write_fp_sreg(s, rd, tcg_single);
5055 tcg_temp_free_i32(tcg_single);
5057 } else {
5058 TCGv_i64 tcg_int = cpu_reg(s, rd);
5059 TCGv_i32 tcg_rmode;
5061 if (extract32(opcode, 2, 1)) {
5062 /* There are too many rounding modes to all fit into rmode,
5063 * so FCVTA[US] is a special case.
5065 rmode = FPROUNDING_TIEAWAY;
5068 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
5070 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
5072 if (is_double) {
5073 TCGv_i64 tcg_double = read_fp_dreg(s, rn);
5074 if (is_signed) {
5075 if (!sf) {
5076 gen_helper_vfp_tosld(tcg_int, tcg_double,
5077 tcg_shift, tcg_fpstatus);
5078 } else {
5079 gen_helper_vfp_tosqd(tcg_int, tcg_double,
5080 tcg_shift, tcg_fpstatus);
5082 } else {
5083 if (!sf) {
5084 gen_helper_vfp_tould(tcg_int, tcg_double,
5085 tcg_shift, tcg_fpstatus);
5086 } else {
5087 gen_helper_vfp_touqd(tcg_int, tcg_double,
5088 tcg_shift, tcg_fpstatus);
5091 tcg_temp_free_i64(tcg_double);
5092 } else {
5093 TCGv_i32 tcg_single = read_fp_sreg(s, rn);
5094 if (sf) {
5095 if (is_signed) {
5096 gen_helper_vfp_tosqs(tcg_int, tcg_single,
5097 tcg_shift, tcg_fpstatus);
5098 } else {
5099 gen_helper_vfp_touqs(tcg_int, tcg_single,
5100 tcg_shift, tcg_fpstatus);
5102 } else {
5103 TCGv_i32 tcg_dest = tcg_temp_new_i32();
5104 if (is_signed) {
5105 gen_helper_vfp_tosls(tcg_dest, tcg_single,
5106 tcg_shift, tcg_fpstatus);
5107 } else {
5108 gen_helper_vfp_touls(tcg_dest, tcg_single,
5109 tcg_shift, tcg_fpstatus);
5111 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
5112 tcg_temp_free_i32(tcg_dest);
5114 tcg_temp_free_i32(tcg_single);
5117 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
5118 tcg_temp_free_i32(tcg_rmode);
5120 if (!sf) {
5121 tcg_gen_ext32u_i64(tcg_int, tcg_int);
5125 tcg_temp_free_ptr(tcg_fpstatus);
5126 tcg_temp_free_i32(tcg_shift);
5129 /* C3.6.29 Floating point <-> fixed point conversions
5130 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
5131 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
5132 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale | Rn | Rd |
5133 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
5135 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
5137 int rd = extract32(insn, 0, 5);
5138 int rn = extract32(insn, 5, 5);
5139 int scale = extract32(insn, 10, 6);
5140 int opcode = extract32(insn, 16, 3);
5141 int rmode = extract32(insn, 19, 2);
5142 int type = extract32(insn, 22, 2);
5143 bool sbit = extract32(insn, 29, 1);
5144 bool sf = extract32(insn, 31, 1);
5145 bool itof;
5147 if (sbit || (type > 1)
5148 || (!sf && scale < 32)) {
5149 unallocated_encoding(s);
5150 return;
5153 switch ((rmode << 3) | opcode) {
5154 case 0x2: /* SCVTF */
5155 case 0x3: /* UCVTF */
5156 itof = true;
5157 break;
5158 case 0x18: /* FCVTZS */
5159 case 0x19: /* FCVTZU */
5160 itof = false;
5161 break;
5162 default:
5163 unallocated_encoding(s);
5164 return;
5167 if (!fp_access_check(s)) {
5168 return;
5171 handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
5174 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
5176 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
5177 * without conversion.
5180 if (itof) {
5181 TCGv_i64 tcg_rn = cpu_reg(s, rn);
5183 switch (type) {
5184 case 0:
5186 /* 32 bit */
5187 TCGv_i64 tmp = tcg_temp_new_i64();
5188 tcg_gen_ext32u_i64(tmp, tcg_rn);
5189 tcg_gen_st_i64(tmp, cpu_env, fp_reg_offset(s, rd, MO_64));
5190 tcg_gen_movi_i64(tmp, 0);
5191 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(s, rd));
5192 tcg_temp_free_i64(tmp);
5193 break;
5195 case 1:
5197 /* 64 bit */
5198 TCGv_i64 tmp = tcg_const_i64(0);
5199 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_offset(s, rd, MO_64));
5200 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(s, rd));
5201 tcg_temp_free_i64(tmp);
5202 break;
5204 case 2:
5205 /* 64 bit to top half. */
5206 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
5207 break;
5209 } else {
5210 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5212 switch (type) {
5213 case 0:
5214 /* 32 bit */
5215 tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
5216 break;
5217 case 1:
5218 /* 64 bit */
5219 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
5220 break;
5221 case 2:
5222 /* 64 bits from top half */
5223 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
5224 break;
5229 /* C3.6.30 Floating point <-> integer conversions
5230 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
5231 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
5232 * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
5233 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
5235 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
5237 int rd = extract32(insn, 0, 5);
5238 int rn = extract32(insn, 5, 5);
5239 int opcode = extract32(insn, 16, 3);
5240 int rmode = extract32(insn, 19, 2);
5241 int type = extract32(insn, 22, 2);
5242 bool sbit = extract32(insn, 29, 1);
5243 bool sf = extract32(insn, 31, 1);
5245 if (sbit) {
5246 unallocated_encoding(s);
5247 return;
5250 if (opcode > 5) {
5251 /* FMOV */
5252 bool itof = opcode & 1;
5254 if (rmode >= 2) {
5255 unallocated_encoding(s);
5256 return;
5259 switch (sf << 3 | type << 1 | rmode) {
5260 case 0x0: /* 32 bit */
5261 case 0xa: /* 64 bit */
5262 case 0xd: /* 64 bit to top half of quad */
5263 break;
5264 default:
5265 /* all other sf/type/rmode combinations are invalid */
5266 unallocated_encoding(s);
5267 break;
5270 if (!fp_access_check(s)) {
5271 return;
5273 handle_fmov(s, rd, rn, type, itof);
5274 } else {
5275 /* actual FP conversions */
5276 bool itof = extract32(opcode, 1, 1);
5278 if (type > 1 || (rmode != 0 && opcode > 1)) {
5279 unallocated_encoding(s);
5280 return;
5283 if (!fp_access_check(s)) {
5284 return;
5286 handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
5290 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
5291 * 31 30 29 28 25 24 0
5292 * +---+---+---+---------+-----------------------------+
5293 * | | 0 | | 1 1 1 1 | |
5294 * +---+---+---+---------+-----------------------------+
5296 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
5298 if (extract32(insn, 24, 1)) {
5299 /* Floating point data-processing (3 source) */
5300 disas_fp_3src(s, insn);
5301 } else if (extract32(insn, 21, 1) == 0) {
5302 /* Floating point to fixed point conversions */
5303 disas_fp_fixed_conv(s, insn);
5304 } else {
5305 switch (extract32(insn, 10, 2)) {
5306 case 1:
5307 /* Floating point conditional compare */
5308 disas_fp_ccomp(s, insn);
5309 break;
5310 case 2:
5311 /* Floating point data-processing (2 source) */
5312 disas_fp_2src(s, insn);
5313 break;
5314 case 3:
5315 /* Floating point conditional select */
5316 disas_fp_csel(s, insn);
5317 break;
5318 case 0:
5319 switch (ctz32(extract32(insn, 12, 4))) {
5320 case 0: /* [15:12] == xxx1 */
5321 /* Floating point immediate */
5322 disas_fp_imm(s, insn);
5323 break;
5324 case 1: /* [15:12] == xx10 */
5325 /* Floating point compare */
5326 disas_fp_compare(s, insn);
5327 break;
5328 case 2: /* [15:12] == x100 */
5329 /* Floating point data-processing (1 source) */
5330 disas_fp_1src(s, insn);
5331 break;
5332 case 3: /* [15:12] == 1000 */
5333 unallocated_encoding(s);
5334 break;
5335 default: /* [15:12] == 0000 */
5336 /* Floating point <-> integer conversions */
5337 disas_fp_int_conv(s, insn);
5338 break;
5340 break;
5345 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
5346 int pos)
5348 /* Extract 64 bits from the middle of two concatenated 64 bit
5349 * vector register slices left:right. The extracted bits start
5350 * at 'pos' bits into the right (least significant) side.
5351 * We return the result in tcg_right, and guarantee not to
5352 * trash tcg_left.
5354 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5355 assert(pos > 0 && pos < 64);
5357 tcg_gen_shri_i64(tcg_right, tcg_right, pos);
5358 tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
5359 tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
5361 tcg_temp_free_i64(tcg_tmp);
5364 /* C3.6.1 EXT
5365 * 31 30 29 24 23 22 21 20 16 15 14 11 10 9 5 4 0
5366 * +---+---+-------------+-----+---+------+---+------+---+------+------+
5367 * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 | Rm | 0 | imm4 | 0 | Rn | Rd |
5368 * +---+---+-------------+-----+---+------+---+------+---+------+------+
5370 static void disas_simd_ext(DisasContext *s, uint32_t insn)
5372 int is_q = extract32(insn, 30, 1);
5373 int op2 = extract32(insn, 22, 2);
5374 int imm4 = extract32(insn, 11, 4);
5375 int rm = extract32(insn, 16, 5);
5376 int rn = extract32(insn, 5, 5);
5377 int rd = extract32(insn, 0, 5);
5378 int pos = imm4 << 3;
5379 TCGv_i64 tcg_resl, tcg_resh;
5381 if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
5382 unallocated_encoding(s);
5383 return;
5386 if (!fp_access_check(s)) {
5387 return;
5390 tcg_resh = tcg_temp_new_i64();
5391 tcg_resl = tcg_temp_new_i64();
5393 /* Vd gets bits starting at pos bits into Vm:Vn. This is
5394 * either extracting 128 bits from a 128:128 concatenation, or
5395 * extracting 64 bits from a 64:64 concatenation.
5397 if (!is_q) {
5398 read_vec_element(s, tcg_resl, rn, 0, MO_64);
5399 if (pos != 0) {
5400 read_vec_element(s, tcg_resh, rm, 0, MO_64);
5401 do_ext64(s, tcg_resh, tcg_resl, pos);
5403 tcg_gen_movi_i64(tcg_resh, 0);
5404 } else {
5405 TCGv_i64 tcg_hh;
5406 typedef struct {
5407 int reg;
5408 int elt;
5409 } EltPosns;
5410 EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
5411 EltPosns *elt = eltposns;
5413 if (pos >= 64) {
5414 elt++;
5415 pos -= 64;
5418 read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
5419 elt++;
5420 read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
5421 elt++;
5422 if (pos != 0) {
5423 do_ext64(s, tcg_resh, tcg_resl, pos);
5424 tcg_hh = tcg_temp_new_i64();
5425 read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
5426 do_ext64(s, tcg_hh, tcg_resh, pos);
5427 tcg_temp_free_i64(tcg_hh);
5431 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5432 tcg_temp_free_i64(tcg_resl);
5433 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5434 tcg_temp_free_i64(tcg_resh);
5437 /* C3.6.2 TBL/TBX
5438 * 31 30 29 24 23 22 21 20 16 15 14 13 12 11 10 9 5 4 0
5439 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
5440 * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 | Rm | 0 | len | op | 0 0 | Rn | Rd |
5441 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
5443 static void disas_simd_tb(DisasContext *s, uint32_t insn)
5445 int op2 = extract32(insn, 22, 2);
5446 int is_q = extract32(insn, 30, 1);
5447 int rm = extract32(insn, 16, 5);
5448 int rn = extract32(insn, 5, 5);
5449 int rd = extract32(insn, 0, 5);
5450 int is_tblx = extract32(insn, 12, 1);
5451 int len = extract32(insn, 13, 2);
5452 TCGv_i64 tcg_resl, tcg_resh, tcg_idx;
5453 TCGv_i32 tcg_regno, tcg_numregs;
5455 if (op2 != 0) {
5456 unallocated_encoding(s);
5457 return;
5460 if (!fp_access_check(s)) {
5461 return;
5464 /* This does a table lookup: for every byte element in the input
5465 * we index into a table formed from up to four vector registers,
5466 * and then the output is the result of the lookups. Our helper
5467 * function does the lookup operation for a single 64 bit part of
5468 * the input.
5470 tcg_resl = tcg_temp_new_i64();
5471 tcg_resh = tcg_temp_new_i64();
5473 if (is_tblx) {
5474 read_vec_element(s, tcg_resl, rd, 0, MO_64);
5475 } else {
5476 tcg_gen_movi_i64(tcg_resl, 0);
5478 if (is_tblx && is_q) {
5479 read_vec_element(s, tcg_resh, rd, 1, MO_64);
5480 } else {
5481 tcg_gen_movi_i64(tcg_resh, 0);
5484 tcg_idx = tcg_temp_new_i64();
5485 tcg_regno = tcg_const_i32(rn);
5486 tcg_numregs = tcg_const_i32(len + 1);
5487 read_vec_element(s, tcg_idx, rm, 0, MO_64);
5488 gen_helper_simd_tbl(tcg_resl, cpu_env, tcg_resl, tcg_idx,
5489 tcg_regno, tcg_numregs);
5490 if (is_q) {
5491 read_vec_element(s, tcg_idx, rm, 1, MO_64);
5492 gen_helper_simd_tbl(tcg_resh, cpu_env, tcg_resh, tcg_idx,
5493 tcg_regno, tcg_numregs);
5495 tcg_temp_free_i64(tcg_idx);
5496 tcg_temp_free_i32(tcg_regno);
5497 tcg_temp_free_i32(tcg_numregs);
5499 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5500 tcg_temp_free_i64(tcg_resl);
5501 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5502 tcg_temp_free_i64(tcg_resh);
5505 /* C3.6.3 ZIP/UZP/TRN
5506 * 31 30 29 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
5507 * +---+---+-------------+------+---+------+---+------------------+------+
5508 * | 0 | Q | 0 0 1 1 1 0 | size | 0 | Rm | 0 | opc | 1 0 | Rn | Rd |
5509 * +---+---+-------------+------+---+------+---+------------------+------+
5511 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
5513 int rd = extract32(insn, 0, 5);
5514 int rn = extract32(insn, 5, 5);
5515 int rm = extract32(insn, 16, 5);
5516 int size = extract32(insn, 22, 2);
5517 /* opc field bits [1:0] indicate ZIP/UZP/TRN;
5518 * bit 2 indicates 1 vs 2 variant of the insn.
5520 int opcode = extract32(insn, 12, 2);
5521 bool part = extract32(insn, 14, 1);
5522 bool is_q = extract32(insn, 30, 1);
5523 int esize = 8 << size;
5524 int i, ofs;
5525 int datasize = is_q ? 128 : 64;
5526 int elements = datasize / esize;
5527 TCGv_i64 tcg_res, tcg_resl, tcg_resh;
5529 if (opcode == 0 || (size == 3 && !is_q)) {
5530 unallocated_encoding(s);
5531 return;
5534 if (!fp_access_check(s)) {
5535 return;
5538 tcg_resl = tcg_const_i64(0);
5539 tcg_resh = tcg_const_i64(0);
5540 tcg_res = tcg_temp_new_i64();
5542 for (i = 0; i < elements; i++) {
5543 switch (opcode) {
5544 case 1: /* UZP1/2 */
5546 int midpoint = elements / 2;
5547 if (i < midpoint) {
5548 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
5549 } else {
5550 read_vec_element(s, tcg_res, rm,
5551 2 * (i - midpoint) + part, size);
5553 break;
5555 case 2: /* TRN1/2 */
5556 if (i & 1) {
5557 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
5558 } else {
5559 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
5561 break;
5562 case 3: /* ZIP1/2 */
5564 int base = part * elements / 2;
5565 if (i & 1) {
5566 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
5567 } else {
5568 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
5570 break;
5572 default:
5573 g_assert_not_reached();
5576 ofs = i * esize;
5577 if (ofs < 64) {
5578 tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
5579 tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
5580 } else {
5581 tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
5582 tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
5586 tcg_temp_free_i64(tcg_res);
5588 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5589 tcg_temp_free_i64(tcg_resl);
5590 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5591 tcg_temp_free_i64(tcg_resh);
5594 static void do_minmaxop(DisasContext *s, TCGv_i32 tcg_elt1, TCGv_i32 tcg_elt2,
5595 int opc, bool is_min, TCGv_ptr fpst)
5597 /* Helper function for disas_simd_across_lanes: do a single precision
5598 * min/max operation on the specified two inputs,
5599 * and return the result in tcg_elt1.
5601 if (opc == 0xc) {
5602 if (is_min) {
5603 gen_helper_vfp_minnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5604 } else {
5605 gen_helper_vfp_maxnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5607 } else {
5608 assert(opc == 0xf);
5609 if (is_min) {
5610 gen_helper_vfp_mins(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5611 } else {
5612 gen_helper_vfp_maxs(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5617 /* C3.6.4 AdvSIMD across lanes
5618 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
5619 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5620 * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
5621 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5623 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
5625 int rd = extract32(insn, 0, 5);
5626 int rn = extract32(insn, 5, 5);
5627 int size = extract32(insn, 22, 2);
5628 int opcode = extract32(insn, 12, 5);
5629 bool is_q = extract32(insn, 30, 1);
5630 bool is_u = extract32(insn, 29, 1);
5631 bool is_fp = false;
5632 bool is_min = false;
5633 int esize;
5634 int elements;
5635 int i;
5636 TCGv_i64 tcg_res, tcg_elt;
5638 switch (opcode) {
5639 case 0x1b: /* ADDV */
5640 if (is_u) {
5641 unallocated_encoding(s);
5642 return;
5644 /* fall through */
5645 case 0x3: /* SADDLV, UADDLV */
5646 case 0xa: /* SMAXV, UMAXV */
5647 case 0x1a: /* SMINV, UMINV */
5648 if (size == 3 || (size == 2 && !is_q)) {
5649 unallocated_encoding(s);
5650 return;
5652 break;
5653 case 0xc: /* FMAXNMV, FMINNMV */
5654 case 0xf: /* FMAXV, FMINV */
5655 if (!is_u || !is_q || extract32(size, 0, 1)) {
5656 unallocated_encoding(s);
5657 return;
5659 /* Bit 1 of size field encodes min vs max, and actual size is always
5660 * 32 bits: adjust the size variable so following code can rely on it
5662 is_min = extract32(size, 1, 1);
5663 is_fp = true;
5664 size = 2;
5665 break;
5666 default:
5667 unallocated_encoding(s);
5668 return;
5671 if (!fp_access_check(s)) {
5672 return;
5675 esize = 8 << size;
5676 elements = (is_q ? 128 : 64) / esize;
5678 tcg_res = tcg_temp_new_i64();
5679 tcg_elt = tcg_temp_new_i64();
5681 /* These instructions operate across all lanes of a vector
5682 * to produce a single result. We can guarantee that a 64
5683 * bit intermediate is sufficient:
5684 * + for [US]ADDLV the maximum element size is 32 bits, and
5685 * the result type is 64 bits
5686 * + for FMAX*V, FMIN*V, ADDV the intermediate type is the
5687 * same as the element size, which is 32 bits at most
5688 * For the integer operations we can choose to work at 64
5689 * or 32 bits and truncate at the end; for simplicity
5690 * we use 64 bits always. The floating point
5691 * ops do require 32 bit intermediates, though.
5693 if (!is_fp) {
5694 read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
5696 for (i = 1; i < elements; i++) {
5697 read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
5699 switch (opcode) {
5700 case 0x03: /* SADDLV / UADDLV */
5701 case 0x1b: /* ADDV */
5702 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
5703 break;
5704 case 0x0a: /* SMAXV / UMAXV */
5705 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
5706 tcg_res,
5707 tcg_res, tcg_elt, tcg_res, tcg_elt);
5708 break;
5709 case 0x1a: /* SMINV / UMINV */
5710 tcg_gen_movcond_i64(is_u ? TCG_COND_LEU : TCG_COND_LE,
5711 tcg_res,
5712 tcg_res, tcg_elt, tcg_res, tcg_elt);
5713 break;
5714 break;
5715 default:
5716 g_assert_not_reached();
5720 } else {
5721 /* Floating point ops which work on 32 bit (single) intermediates.
5722 * Note that correct NaN propagation requires that we do these
5723 * operations in exactly the order specified by the pseudocode.
5725 TCGv_i32 tcg_elt1 = tcg_temp_new_i32();
5726 TCGv_i32 tcg_elt2 = tcg_temp_new_i32();
5727 TCGv_i32 tcg_elt3 = tcg_temp_new_i32();
5728 TCGv_ptr fpst = get_fpstatus_ptr();
5730 assert(esize == 32);
5731 assert(elements == 4);
5733 read_vec_element(s, tcg_elt, rn, 0, MO_32);
5734 tcg_gen_extrl_i64_i32(tcg_elt1, tcg_elt);
5735 read_vec_element(s, tcg_elt, rn, 1, MO_32);
5736 tcg_gen_extrl_i64_i32(tcg_elt2, tcg_elt);
5738 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5740 read_vec_element(s, tcg_elt, rn, 2, MO_32);
5741 tcg_gen_extrl_i64_i32(tcg_elt2, tcg_elt);
5742 read_vec_element(s, tcg_elt, rn, 3, MO_32);
5743 tcg_gen_extrl_i64_i32(tcg_elt3, tcg_elt);
5745 do_minmaxop(s, tcg_elt2, tcg_elt3, opcode, is_min, fpst);
5747 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5749 tcg_gen_extu_i32_i64(tcg_res, tcg_elt1);
5750 tcg_temp_free_i32(tcg_elt1);
5751 tcg_temp_free_i32(tcg_elt2);
5752 tcg_temp_free_i32(tcg_elt3);
5753 tcg_temp_free_ptr(fpst);
5756 tcg_temp_free_i64(tcg_elt);
5758 /* Now truncate the result to the width required for the final output */
5759 if (opcode == 0x03) {
5760 /* SADDLV, UADDLV: result is 2*esize */
5761 size++;
5764 switch (size) {
5765 case 0:
5766 tcg_gen_ext8u_i64(tcg_res, tcg_res);
5767 break;
5768 case 1:
5769 tcg_gen_ext16u_i64(tcg_res, tcg_res);
5770 break;
5771 case 2:
5772 tcg_gen_ext32u_i64(tcg_res, tcg_res);
5773 break;
5774 case 3:
5775 break;
5776 default:
5777 g_assert_not_reached();
5780 write_fp_dreg(s, rd, tcg_res);
5781 tcg_temp_free_i64(tcg_res);
5784 /* C6.3.31 DUP (Element, Vector)
5786 * 31 30 29 21 20 16 15 10 9 5 4 0
5787 * +---+---+-------------------+--------+-------------+------+------+
5788 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
5789 * +---+---+-------------------+--------+-------------+------+------+
5791 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5793 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
5794 int imm5)
5796 int size = ctz32(imm5);
5797 int esize = 8 << size;
5798 int elements = (is_q ? 128 : 64) / esize;
5799 int index, i;
5800 TCGv_i64 tmp;
5802 if (size > 3 || (size == 3 && !is_q)) {
5803 unallocated_encoding(s);
5804 return;
5807 if (!fp_access_check(s)) {
5808 return;
5811 index = imm5 >> (size + 1);
5813 tmp = tcg_temp_new_i64();
5814 read_vec_element(s, tmp, rn, index, size);
5816 for (i = 0; i < elements; i++) {
5817 write_vec_element(s, tmp, rd, i, size);
5820 if (!is_q) {
5821 clear_vec_high(s, rd);
5824 tcg_temp_free_i64(tmp);
5827 /* C6.3.31 DUP (element, scalar)
5828 * 31 21 20 16 15 10 9 5 4 0
5829 * +-----------------------+--------+-------------+------+------+
5830 * | 0 1 0 1 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
5831 * +-----------------------+--------+-------------+------+------+
5833 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
5834 int imm5)
5836 int size = ctz32(imm5);
5837 int index;
5838 TCGv_i64 tmp;
5840 if (size > 3) {
5841 unallocated_encoding(s);
5842 return;
5845 if (!fp_access_check(s)) {
5846 return;
5849 index = imm5 >> (size + 1);
5851 /* This instruction just extracts the specified element and
5852 * zero-extends it into the bottom of the destination register.
5854 tmp = tcg_temp_new_i64();
5855 read_vec_element(s, tmp, rn, index, size);
5856 write_fp_dreg(s, rd, tmp);
5857 tcg_temp_free_i64(tmp);
5860 /* C6.3.32 DUP (General)
5862 * 31 30 29 21 20 16 15 10 9 5 4 0
5863 * +---+---+-------------------+--------+-------------+------+------+
5864 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 1 1 | Rn | Rd |
5865 * +---+---+-------------------+--------+-------------+------+------+
5867 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5869 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
5870 int imm5)
5872 int size = ctz32(imm5);
5873 int esize = 8 << size;
5874 int elements = (is_q ? 128 : 64)/esize;
5875 int i = 0;
5877 if (size > 3 || ((size == 3) && !is_q)) {
5878 unallocated_encoding(s);
5879 return;
5882 if (!fp_access_check(s)) {
5883 return;
5886 for (i = 0; i < elements; i++) {
5887 write_vec_element(s, cpu_reg(s, rn), rd, i, size);
5889 if (!is_q) {
5890 clear_vec_high(s, rd);
5894 /* C6.3.150 INS (Element)
5896 * 31 21 20 16 15 14 11 10 9 5 4 0
5897 * +-----------------------+--------+------------+---+------+------+
5898 * | 0 1 1 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5899 * +-----------------------+--------+------------+---+------+------+
5901 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5902 * index: encoded in imm5<4:size+1>
5904 static void handle_simd_inse(DisasContext *s, int rd, int rn,
5905 int imm4, int imm5)
5907 int size = ctz32(imm5);
5908 int src_index, dst_index;
5909 TCGv_i64 tmp;
5911 if (size > 3) {
5912 unallocated_encoding(s);
5913 return;
5916 if (!fp_access_check(s)) {
5917 return;
5920 dst_index = extract32(imm5, 1+size, 5);
5921 src_index = extract32(imm4, size, 4);
5923 tmp = tcg_temp_new_i64();
5925 read_vec_element(s, tmp, rn, src_index, size);
5926 write_vec_element(s, tmp, rd, dst_index, size);
5928 tcg_temp_free_i64(tmp);
5932 /* C6.3.151 INS (General)
5934 * 31 21 20 16 15 10 9 5 4 0
5935 * +-----------------------+--------+-------------+------+------+
5936 * | 0 1 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 1 1 1 | Rn | Rd |
5937 * +-----------------------+--------+-------------+------+------+
5939 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5940 * index: encoded in imm5<4:size+1>
5942 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
5944 int size = ctz32(imm5);
5945 int idx;
5947 if (size > 3) {
5948 unallocated_encoding(s);
5949 return;
5952 if (!fp_access_check(s)) {
5953 return;
5956 idx = extract32(imm5, 1 + size, 4 - size);
5957 write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
5961 * C6.3.321 UMOV (General)
5962 * C6.3.237 SMOV (General)
5964 * 31 30 29 21 20 16 15 12 10 9 5 4 0
5965 * +---+---+-------------------+--------+-------------+------+------+
5966 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 1 U 1 1 | Rn | Rd |
5967 * +---+---+-------------------+--------+-------------+------+------+
5969 * U: unsigned when set
5970 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5972 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
5973 int rn, int rd, int imm5)
5975 int size = ctz32(imm5);
5976 int element;
5977 TCGv_i64 tcg_rd;
5979 /* Check for UnallocatedEncodings */
5980 if (is_signed) {
5981 if (size > 2 || (size == 2 && !is_q)) {
5982 unallocated_encoding(s);
5983 return;
5985 } else {
5986 if (size > 3
5987 || (size < 3 && is_q)
5988 || (size == 3 && !is_q)) {
5989 unallocated_encoding(s);
5990 return;
5994 if (!fp_access_check(s)) {
5995 return;
5998 element = extract32(imm5, 1+size, 4);
6000 tcg_rd = cpu_reg(s, rd);
6001 read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
6002 if (is_signed && !is_q) {
6003 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
6007 /* C3.6.5 AdvSIMD copy
6008 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
6009 * +---+---+----+-----------------+------+---+------+---+------+------+
6010 * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
6011 * +---+---+----+-----------------+------+---+------+---+------+------+
6013 static void disas_simd_copy(DisasContext *s, uint32_t insn)
6015 int rd = extract32(insn, 0, 5);
6016 int rn = extract32(insn, 5, 5);
6017 int imm4 = extract32(insn, 11, 4);
6018 int op = extract32(insn, 29, 1);
6019 int is_q = extract32(insn, 30, 1);
6020 int imm5 = extract32(insn, 16, 5);
6022 if (op) {
6023 if (is_q) {
6024 /* INS (element) */
6025 handle_simd_inse(s, rd, rn, imm4, imm5);
6026 } else {
6027 unallocated_encoding(s);
6029 } else {
6030 switch (imm4) {
6031 case 0:
6032 /* DUP (element - vector) */
6033 handle_simd_dupe(s, is_q, rd, rn, imm5);
6034 break;
6035 case 1:
6036 /* DUP (general) */
6037 handle_simd_dupg(s, is_q, rd, rn, imm5);
6038 break;
6039 case 3:
6040 if (is_q) {
6041 /* INS (general) */
6042 handle_simd_insg(s, rd, rn, imm5);
6043 } else {
6044 unallocated_encoding(s);
6046 break;
6047 case 5:
6048 case 7:
6049 /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
6050 handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
6051 break;
6052 default:
6053 unallocated_encoding(s);
6054 break;
6059 /* C3.6.6 AdvSIMD modified immediate
6060 * 31 30 29 28 19 18 16 15 12 11 10 9 5 4 0
6061 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
6062 * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh | Rd |
6063 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
6065 * There are a number of operations that can be carried out here:
6066 * MOVI - move (shifted) imm into register
6067 * MVNI - move inverted (shifted) imm into register
6068 * ORR - bitwise OR of (shifted) imm with register
6069 * BIC - bitwise clear of (shifted) imm with register
6071 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
6073 int rd = extract32(insn, 0, 5);
6074 int cmode = extract32(insn, 12, 4);
6075 int cmode_3_1 = extract32(cmode, 1, 3);
6076 int cmode_0 = extract32(cmode, 0, 1);
6077 int o2 = extract32(insn, 11, 1);
6078 uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
6079 bool is_neg = extract32(insn, 29, 1);
6080 bool is_q = extract32(insn, 30, 1);
6081 uint64_t imm = 0;
6082 TCGv_i64 tcg_rd, tcg_imm;
6083 int i;
6085 if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
6086 unallocated_encoding(s);
6087 return;
6090 if (!fp_access_check(s)) {
6091 return;
6094 /* See AdvSIMDExpandImm() in ARM ARM */
6095 switch (cmode_3_1) {
6096 case 0: /* Replicate(Zeros(24):imm8, 2) */
6097 case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
6098 case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
6099 case 3: /* Replicate(imm8:Zeros(24), 2) */
6101 int shift = cmode_3_1 * 8;
6102 imm = bitfield_replicate(abcdefgh << shift, 32);
6103 break;
6105 case 4: /* Replicate(Zeros(8):imm8, 4) */
6106 case 5: /* Replicate(imm8:Zeros(8), 4) */
6108 int shift = (cmode_3_1 & 0x1) * 8;
6109 imm = bitfield_replicate(abcdefgh << shift, 16);
6110 break;
6112 case 6:
6113 if (cmode_0) {
6114 /* Replicate(Zeros(8):imm8:Ones(16), 2) */
6115 imm = (abcdefgh << 16) | 0xffff;
6116 } else {
6117 /* Replicate(Zeros(16):imm8:Ones(8), 2) */
6118 imm = (abcdefgh << 8) | 0xff;
6120 imm = bitfield_replicate(imm, 32);
6121 break;
6122 case 7:
6123 if (!cmode_0 && !is_neg) {
6124 imm = bitfield_replicate(abcdefgh, 8);
6125 } else if (!cmode_0 && is_neg) {
6126 int i;
6127 imm = 0;
6128 for (i = 0; i < 8; i++) {
6129 if ((abcdefgh) & (1 << i)) {
6130 imm |= 0xffULL << (i * 8);
6133 } else if (cmode_0) {
6134 if (is_neg) {
6135 imm = (abcdefgh & 0x3f) << 48;
6136 if (abcdefgh & 0x80) {
6137 imm |= 0x8000000000000000ULL;
6139 if (abcdefgh & 0x40) {
6140 imm |= 0x3fc0000000000000ULL;
6141 } else {
6142 imm |= 0x4000000000000000ULL;
6144 } else {
6145 imm = (abcdefgh & 0x3f) << 19;
6146 if (abcdefgh & 0x80) {
6147 imm |= 0x80000000;
6149 if (abcdefgh & 0x40) {
6150 imm |= 0x3e000000;
6151 } else {
6152 imm |= 0x40000000;
6154 imm |= (imm << 32);
6157 break;
6160 if (cmode_3_1 != 7 && is_neg) {
6161 imm = ~imm;
6164 tcg_imm = tcg_const_i64(imm);
6165 tcg_rd = new_tmp_a64(s);
6167 for (i = 0; i < 2; i++) {
6168 int foffs = i ? fp_reg_hi_offset(s, rd) : fp_reg_offset(s, rd, MO_64);
6170 if (i == 1 && !is_q) {
6171 /* non-quad ops clear high half of vector */
6172 tcg_gen_movi_i64(tcg_rd, 0);
6173 } else if ((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9) {
6174 tcg_gen_ld_i64(tcg_rd, cpu_env, foffs);
6175 if (is_neg) {
6176 /* AND (BIC) */
6177 tcg_gen_and_i64(tcg_rd, tcg_rd, tcg_imm);
6178 } else {
6179 /* ORR */
6180 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_imm);
6182 } else {
6183 /* MOVI */
6184 tcg_gen_mov_i64(tcg_rd, tcg_imm);
6186 tcg_gen_st_i64(tcg_rd, cpu_env, foffs);
6189 tcg_temp_free_i64(tcg_imm);
6192 /* C3.6.7 AdvSIMD scalar copy
6193 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
6194 * +-----+----+-----------------+------+---+------+---+------+------+
6195 * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
6196 * +-----+----+-----------------+------+---+------+---+------+------+
6198 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
6200 int rd = extract32(insn, 0, 5);
6201 int rn = extract32(insn, 5, 5);
6202 int imm4 = extract32(insn, 11, 4);
6203 int imm5 = extract32(insn, 16, 5);
6204 int op = extract32(insn, 29, 1);
6206 if (op != 0 || imm4 != 0) {
6207 unallocated_encoding(s);
6208 return;
6211 /* DUP (element, scalar) */
6212 handle_simd_dupes(s, rd, rn, imm5);
6215 /* C3.6.8 AdvSIMD scalar pairwise
6216 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
6217 * +-----+---+-----------+------+-----------+--------+-----+------+------+
6218 * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
6219 * +-----+---+-----------+------+-----------+--------+-----+------+------+
6221 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
6223 int u = extract32(insn, 29, 1);
6224 int size = extract32(insn, 22, 2);
6225 int opcode = extract32(insn, 12, 5);
6226 int rn = extract32(insn, 5, 5);
6227 int rd = extract32(insn, 0, 5);
6228 TCGv_ptr fpst;
6230 /* For some ops (the FP ones), size[1] is part of the encoding.
6231 * For ADDP strictly it is not but size[1] is always 1 for valid
6232 * encodings.
6234 opcode |= (extract32(size, 1, 1) << 5);
6236 switch (opcode) {
6237 case 0x3b: /* ADDP */
6238 if (u || size != 3) {
6239 unallocated_encoding(s);
6240 return;
6242 if (!fp_access_check(s)) {
6243 return;
6246 TCGV_UNUSED_PTR(fpst);
6247 break;
6248 case 0xc: /* FMAXNMP */
6249 case 0xd: /* FADDP */
6250 case 0xf: /* FMAXP */
6251 case 0x2c: /* FMINNMP */
6252 case 0x2f: /* FMINP */
6253 /* FP op, size[0] is 32 or 64 bit */
6254 if (!u) {
6255 unallocated_encoding(s);
6256 return;
6258 if (!fp_access_check(s)) {
6259 return;
6262 size = extract32(size, 0, 1) ? 3 : 2;
6263 fpst = get_fpstatus_ptr();
6264 break;
6265 default:
6266 unallocated_encoding(s);
6267 return;
6270 if (size == 3) {
6271 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6272 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6273 TCGv_i64 tcg_res = tcg_temp_new_i64();
6275 read_vec_element(s, tcg_op1, rn, 0, MO_64);
6276 read_vec_element(s, tcg_op2, rn, 1, MO_64);
6278 switch (opcode) {
6279 case 0x3b: /* ADDP */
6280 tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
6281 break;
6282 case 0xc: /* FMAXNMP */
6283 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6284 break;
6285 case 0xd: /* FADDP */
6286 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6287 break;
6288 case 0xf: /* FMAXP */
6289 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6290 break;
6291 case 0x2c: /* FMINNMP */
6292 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6293 break;
6294 case 0x2f: /* FMINP */
6295 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6296 break;
6297 default:
6298 g_assert_not_reached();
6301 write_fp_dreg(s, rd, tcg_res);
6303 tcg_temp_free_i64(tcg_op1);
6304 tcg_temp_free_i64(tcg_op2);
6305 tcg_temp_free_i64(tcg_res);
6306 } else {
6307 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6308 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6309 TCGv_i32 tcg_res = tcg_temp_new_i32();
6311 read_vec_element_i32(s, tcg_op1, rn, 0, MO_32);
6312 read_vec_element_i32(s, tcg_op2, rn, 1, MO_32);
6314 switch (opcode) {
6315 case 0xc: /* FMAXNMP */
6316 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6317 break;
6318 case 0xd: /* FADDP */
6319 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6320 break;
6321 case 0xf: /* FMAXP */
6322 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6323 break;
6324 case 0x2c: /* FMINNMP */
6325 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6326 break;
6327 case 0x2f: /* FMINP */
6328 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6329 break;
6330 default:
6331 g_assert_not_reached();
6334 write_fp_sreg(s, rd, tcg_res);
6336 tcg_temp_free_i32(tcg_op1);
6337 tcg_temp_free_i32(tcg_op2);
6338 tcg_temp_free_i32(tcg_res);
6341 if (!TCGV_IS_UNUSED_PTR(fpst)) {
6342 tcg_temp_free_ptr(fpst);
6347 * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
6349 * This code is handles the common shifting code and is used by both
6350 * the vector and scalar code.
6352 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6353 TCGv_i64 tcg_rnd, bool accumulate,
6354 bool is_u, int size, int shift)
6356 bool extended_result = false;
6357 bool round = !TCGV_IS_UNUSED_I64(tcg_rnd);
6358 int ext_lshift = 0;
6359 TCGv_i64 tcg_src_hi;
6361 if (round && size == 3) {
6362 extended_result = true;
6363 ext_lshift = 64 - shift;
6364 tcg_src_hi = tcg_temp_new_i64();
6365 } else if (shift == 64) {
6366 if (!accumulate && is_u) {
6367 /* result is zero */
6368 tcg_gen_movi_i64(tcg_res, 0);
6369 return;
6373 /* Deal with the rounding step */
6374 if (round) {
6375 if (extended_result) {
6376 TCGv_i64 tcg_zero = tcg_const_i64(0);
6377 if (!is_u) {
6378 /* take care of sign extending tcg_res */
6379 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
6380 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
6381 tcg_src, tcg_src_hi,
6382 tcg_rnd, tcg_zero);
6383 } else {
6384 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
6385 tcg_src, tcg_zero,
6386 tcg_rnd, tcg_zero);
6388 tcg_temp_free_i64(tcg_zero);
6389 } else {
6390 tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
6394 /* Now do the shift right */
6395 if (round && extended_result) {
6396 /* extended case, >64 bit precision required */
6397 if (ext_lshift == 0) {
6398 /* special case, only high bits matter */
6399 tcg_gen_mov_i64(tcg_src, tcg_src_hi);
6400 } else {
6401 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6402 tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
6403 tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
6405 } else {
6406 if (is_u) {
6407 if (shift == 64) {
6408 /* essentially shifting in 64 zeros */
6409 tcg_gen_movi_i64(tcg_src, 0);
6410 } else {
6411 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6413 } else {
6414 if (shift == 64) {
6415 /* effectively extending the sign-bit */
6416 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
6417 } else {
6418 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
6423 if (accumulate) {
6424 tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
6425 } else {
6426 tcg_gen_mov_i64(tcg_res, tcg_src);
6429 if (extended_result) {
6430 tcg_temp_free_i64(tcg_src_hi);
6434 /* Common SHL/SLI - Shift left with an optional insert */
6435 static void handle_shli_with_ins(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6436 bool insert, int shift)
6438 if (insert) { /* SLI */
6439 tcg_gen_deposit_i64(tcg_res, tcg_res, tcg_src, shift, 64 - shift);
6440 } else { /* SHL */
6441 tcg_gen_shli_i64(tcg_res, tcg_src, shift);
6445 /* SRI: shift right with insert */
6446 static void handle_shri_with_ins(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6447 int size, int shift)
6449 int esize = 8 << size;
6451 /* shift count same as element size is valid but does nothing;
6452 * special case to avoid potential shift by 64.
6454 if (shift != esize) {
6455 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6456 tcg_gen_deposit_i64(tcg_res, tcg_res, tcg_src, 0, esize - shift);
6460 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
6461 static void handle_scalar_simd_shri(DisasContext *s,
6462 bool is_u, int immh, int immb,
6463 int opcode, int rn, int rd)
6465 const int size = 3;
6466 int immhb = immh << 3 | immb;
6467 int shift = 2 * (8 << size) - immhb;
6468 bool accumulate = false;
6469 bool round = false;
6470 bool insert = false;
6471 TCGv_i64 tcg_rn;
6472 TCGv_i64 tcg_rd;
6473 TCGv_i64 tcg_round;
6475 if (!extract32(immh, 3, 1)) {
6476 unallocated_encoding(s);
6477 return;
6480 if (!fp_access_check(s)) {
6481 return;
6484 switch (opcode) {
6485 case 0x02: /* SSRA / USRA (accumulate) */
6486 accumulate = true;
6487 break;
6488 case 0x04: /* SRSHR / URSHR (rounding) */
6489 round = true;
6490 break;
6491 case 0x06: /* SRSRA / URSRA (accum + rounding) */
6492 accumulate = round = true;
6493 break;
6494 case 0x08: /* SRI */
6495 insert = true;
6496 break;
6499 if (round) {
6500 uint64_t round_const = 1ULL << (shift - 1);
6501 tcg_round = tcg_const_i64(round_const);
6502 } else {
6503 TCGV_UNUSED_I64(tcg_round);
6506 tcg_rn = read_fp_dreg(s, rn);
6507 tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6509 if (insert) {
6510 handle_shri_with_ins(tcg_rd, tcg_rn, size, shift);
6511 } else {
6512 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6513 accumulate, is_u, size, shift);
6516 write_fp_dreg(s, rd, tcg_rd);
6518 tcg_temp_free_i64(tcg_rn);
6519 tcg_temp_free_i64(tcg_rd);
6520 if (round) {
6521 tcg_temp_free_i64(tcg_round);
6525 /* SHL/SLI - Scalar shift left */
6526 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
6527 int immh, int immb, int opcode,
6528 int rn, int rd)
6530 int size = 32 - clz32(immh) - 1;
6531 int immhb = immh << 3 | immb;
6532 int shift = immhb - (8 << size);
6533 TCGv_i64 tcg_rn = new_tmp_a64(s);
6534 TCGv_i64 tcg_rd = new_tmp_a64(s);
6536 if (!extract32(immh, 3, 1)) {
6537 unallocated_encoding(s);
6538 return;
6541 if (!fp_access_check(s)) {
6542 return;
6545 tcg_rn = read_fp_dreg(s, rn);
6546 tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6548 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
6550 write_fp_dreg(s, rd, tcg_rd);
6552 tcg_temp_free_i64(tcg_rn);
6553 tcg_temp_free_i64(tcg_rd);
6556 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
6557 * (signed/unsigned) narrowing */
6558 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
6559 bool is_u_shift, bool is_u_narrow,
6560 int immh, int immb, int opcode,
6561 int rn, int rd)
6563 int immhb = immh << 3 | immb;
6564 int size = 32 - clz32(immh) - 1;
6565 int esize = 8 << size;
6566 int shift = (2 * esize) - immhb;
6567 int elements = is_scalar ? 1 : (64 / esize);
6568 bool round = extract32(opcode, 0, 1);
6569 TCGMemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
6570 TCGv_i64 tcg_rn, tcg_rd, tcg_round;
6571 TCGv_i32 tcg_rd_narrowed;
6572 TCGv_i64 tcg_final;
6574 static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
6575 { gen_helper_neon_narrow_sat_s8,
6576 gen_helper_neon_unarrow_sat8 },
6577 { gen_helper_neon_narrow_sat_s16,
6578 gen_helper_neon_unarrow_sat16 },
6579 { gen_helper_neon_narrow_sat_s32,
6580 gen_helper_neon_unarrow_sat32 },
6581 { NULL, NULL },
6583 static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
6584 gen_helper_neon_narrow_sat_u8,
6585 gen_helper_neon_narrow_sat_u16,
6586 gen_helper_neon_narrow_sat_u32,
6587 NULL
6589 NeonGenNarrowEnvFn *narrowfn;
6591 int i;
6593 assert(size < 4);
6595 if (extract32(immh, 3, 1)) {
6596 unallocated_encoding(s);
6597 return;
6600 if (!fp_access_check(s)) {
6601 return;
6604 if (is_u_shift) {
6605 narrowfn = unsigned_narrow_fns[size];
6606 } else {
6607 narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
6610 tcg_rn = tcg_temp_new_i64();
6611 tcg_rd = tcg_temp_new_i64();
6612 tcg_rd_narrowed = tcg_temp_new_i32();
6613 tcg_final = tcg_const_i64(0);
6615 if (round) {
6616 uint64_t round_const = 1ULL << (shift - 1);
6617 tcg_round = tcg_const_i64(round_const);
6618 } else {
6619 TCGV_UNUSED_I64(tcg_round);
6622 for (i = 0; i < elements; i++) {
6623 read_vec_element(s, tcg_rn, rn, i, ldop);
6624 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6625 false, is_u_shift, size+1, shift);
6626 narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
6627 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
6628 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
6631 if (!is_q) {
6632 clear_vec_high(s, rd);
6633 write_vec_element(s, tcg_final, rd, 0, MO_64);
6634 } else {
6635 write_vec_element(s, tcg_final, rd, 1, MO_64);
6638 if (round) {
6639 tcg_temp_free_i64(tcg_round);
6641 tcg_temp_free_i64(tcg_rn);
6642 tcg_temp_free_i64(tcg_rd);
6643 tcg_temp_free_i32(tcg_rd_narrowed);
6644 tcg_temp_free_i64(tcg_final);
6645 return;
6648 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
6649 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
6650 bool src_unsigned, bool dst_unsigned,
6651 int immh, int immb, int rn, int rd)
6653 int immhb = immh << 3 | immb;
6654 int size = 32 - clz32(immh) - 1;
6655 int shift = immhb - (8 << size);
6656 int pass;
6658 assert(immh != 0);
6659 assert(!(scalar && is_q));
6661 if (!scalar) {
6662 if (!is_q && extract32(immh, 3, 1)) {
6663 unallocated_encoding(s);
6664 return;
6667 /* Since we use the variable-shift helpers we must
6668 * replicate the shift count into each element of
6669 * the tcg_shift value.
6671 switch (size) {
6672 case 0:
6673 shift |= shift << 8;
6674 /* fall through */
6675 case 1:
6676 shift |= shift << 16;
6677 break;
6678 case 2:
6679 case 3:
6680 break;
6681 default:
6682 g_assert_not_reached();
6686 if (!fp_access_check(s)) {
6687 return;
6690 if (size == 3) {
6691 TCGv_i64 tcg_shift = tcg_const_i64(shift);
6692 static NeonGenTwo64OpEnvFn * const fns[2][2] = {
6693 { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
6694 { NULL, gen_helper_neon_qshl_u64 },
6696 NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
6697 int maxpass = is_q ? 2 : 1;
6699 for (pass = 0; pass < maxpass; pass++) {
6700 TCGv_i64 tcg_op = tcg_temp_new_i64();
6702 read_vec_element(s, tcg_op, rn, pass, MO_64);
6703 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6704 write_vec_element(s, tcg_op, rd, pass, MO_64);
6706 tcg_temp_free_i64(tcg_op);
6708 tcg_temp_free_i64(tcg_shift);
6710 if (!is_q) {
6711 clear_vec_high(s, rd);
6713 } else {
6714 TCGv_i32 tcg_shift = tcg_const_i32(shift);
6715 static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
6717 { gen_helper_neon_qshl_s8,
6718 gen_helper_neon_qshl_s16,
6719 gen_helper_neon_qshl_s32 },
6720 { gen_helper_neon_qshlu_s8,
6721 gen_helper_neon_qshlu_s16,
6722 gen_helper_neon_qshlu_s32 }
6723 }, {
6724 { NULL, NULL, NULL },
6725 { gen_helper_neon_qshl_u8,
6726 gen_helper_neon_qshl_u16,
6727 gen_helper_neon_qshl_u32 }
6730 NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
6731 TCGMemOp memop = scalar ? size : MO_32;
6732 int maxpass = scalar ? 1 : is_q ? 4 : 2;
6734 for (pass = 0; pass < maxpass; pass++) {
6735 TCGv_i32 tcg_op = tcg_temp_new_i32();
6737 read_vec_element_i32(s, tcg_op, rn, pass, memop);
6738 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6739 if (scalar) {
6740 switch (size) {
6741 case 0:
6742 tcg_gen_ext8u_i32(tcg_op, tcg_op);
6743 break;
6744 case 1:
6745 tcg_gen_ext16u_i32(tcg_op, tcg_op);
6746 break;
6747 case 2:
6748 break;
6749 default:
6750 g_assert_not_reached();
6752 write_fp_sreg(s, rd, tcg_op);
6753 } else {
6754 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
6757 tcg_temp_free_i32(tcg_op);
6759 tcg_temp_free_i32(tcg_shift);
6761 if (!is_q && !scalar) {
6762 clear_vec_high(s, rd);
6767 /* Common vector code for handling integer to FP conversion */
6768 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
6769 int elements, int is_signed,
6770 int fracbits, int size)
6772 bool is_double = size == 3 ? true : false;
6773 TCGv_ptr tcg_fpst = get_fpstatus_ptr();
6774 TCGv_i32 tcg_shift = tcg_const_i32(fracbits);
6775 TCGv_i64 tcg_int = tcg_temp_new_i64();
6776 TCGMemOp mop = size | (is_signed ? MO_SIGN : 0);
6777 int pass;
6779 for (pass = 0; pass < elements; pass++) {
6780 read_vec_element(s, tcg_int, rn, pass, mop);
6782 if (is_double) {
6783 TCGv_i64 tcg_double = tcg_temp_new_i64();
6784 if (is_signed) {
6785 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6786 tcg_shift, tcg_fpst);
6787 } else {
6788 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6789 tcg_shift, tcg_fpst);
6791 if (elements == 1) {
6792 write_fp_dreg(s, rd, tcg_double);
6793 } else {
6794 write_vec_element(s, tcg_double, rd, pass, MO_64);
6796 tcg_temp_free_i64(tcg_double);
6797 } else {
6798 TCGv_i32 tcg_single = tcg_temp_new_i32();
6799 if (is_signed) {
6800 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6801 tcg_shift, tcg_fpst);
6802 } else {
6803 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6804 tcg_shift, tcg_fpst);
6806 if (elements == 1) {
6807 write_fp_sreg(s, rd, tcg_single);
6808 } else {
6809 write_vec_element_i32(s, tcg_single, rd, pass, MO_32);
6811 tcg_temp_free_i32(tcg_single);
6815 if (!is_double && elements == 2) {
6816 clear_vec_high(s, rd);
6819 tcg_temp_free_i64(tcg_int);
6820 tcg_temp_free_ptr(tcg_fpst);
6821 tcg_temp_free_i32(tcg_shift);
6824 /* UCVTF/SCVTF - Integer to FP conversion */
6825 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
6826 bool is_q, bool is_u,
6827 int immh, int immb, int opcode,
6828 int rn, int rd)
6830 bool is_double = extract32(immh, 3, 1);
6831 int size = is_double ? MO_64 : MO_32;
6832 int elements;
6833 int immhb = immh << 3 | immb;
6834 int fracbits = (is_double ? 128 : 64) - immhb;
6836 if (!extract32(immh, 2, 2)) {
6837 unallocated_encoding(s);
6838 return;
6841 if (is_scalar) {
6842 elements = 1;
6843 } else {
6844 elements = is_double ? 2 : is_q ? 4 : 2;
6845 if (is_double && !is_q) {
6846 unallocated_encoding(s);
6847 return;
6851 if (!fp_access_check(s)) {
6852 return;
6855 /* immh == 0 would be a failure of the decode logic */
6856 g_assert(immh);
6858 handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
6861 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
6862 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
6863 bool is_q, bool is_u,
6864 int immh, int immb, int rn, int rd)
6866 bool is_double = extract32(immh, 3, 1);
6867 int immhb = immh << 3 | immb;
6868 int fracbits = (is_double ? 128 : 64) - immhb;
6869 int pass;
6870 TCGv_ptr tcg_fpstatus;
6871 TCGv_i32 tcg_rmode, tcg_shift;
6873 if (!extract32(immh, 2, 2)) {
6874 unallocated_encoding(s);
6875 return;
6878 if (!is_scalar && !is_q && is_double) {
6879 unallocated_encoding(s);
6880 return;
6883 if (!fp_access_check(s)) {
6884 return;
6887 assert(!(is_scalar && is_q));
6889 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(FPROUNDING_ZERO));
6890 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
6891 tcg_fpstatus = get_fpstatus_ptr();
6892 tcg_shift = tcg_const_i32(fracbits);
6894 if (is_double) {
6895 int maxpass = is_scalar ? 1 : 2;
6897 for (pass = 0; pass < maxpass; pass++) {
6898 TCGv_i64 tcg_op = tcg_temp_new_i64();
6900 read_vec_element(s, tcg_op, rn, pass, MO_64);
6901 if (is_u) {
6902 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6903 } else {
6904 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6906 write_vec_element(s, tcg_op, rd, pass, MO_64);
6907 tcg_temp_free_i64(tcg_op);
6909 if (!is_q) {
6910 clear_vec_high(s, rd);
6912 } else {
6913 int maxpass = is_scalar ? 1 : is_q ? 4 : 2;
6914 for (pass = 0; pass < maxpass; pass++) {
6915 TCGv_i32 tcg_op = tcg_temp_new_i32();
6917 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
6918 if (is_u) {
6919 gen_helper_vfp_touls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6920 } else {
6921 gen_helper_vfp_tosls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6923 if (is_scalar) {
6924 write_fp_sreg(s, rd, tcg_op);
6925 } else {
6926 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
6928 tcg_temp_free_i32(tcg_op);
6930 if (!is_q && !is_scalar) {
6931 clear_vec_high(s, rd);
6935 tcg_temp_free_ptr(tcg_fpstatus);
6936 tcg_temp_free_i32(tcg_shift);
6937 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
6938 tcg_temp_free_i32(tcg_rmode);
6941 /* C3.6.9 AdvSIMD scalar shift by immediate
6942 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
6943 * +-----+---+-------------+------+------+--------+---+------+------+
6944 * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
6945 * +-----+---+-------------+------+------+--------+---+------+------+
6947 * This is the scalar version so it works on a fixed sized registers
6949 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
6951 int rd = extract32(insn, 0, 5);
6952 int rn = extract32(insn, 5, 5);
6953 int opcode = extract32(insn, 11, 5);
6954 int immb = extract32(insn, 16, 3);
6955 int immh = extract32(insn, 19, 4);
6956 bool is_u = extract32(insn, 29, 1);
6958 if (immh == 0) {
6959 unallocated_encoding(s);
6960 return;
6963 switch (opcode) {
6964 case 0x08: /* SRI */
6965 if (!is_u) {
6966 unallocated_encoding(s);
6967 return;
6969 /* fall through */
6970 case 0x00: /* SSHR / USHR */
6971 case 0x02: /* SSRA / USRA */
6972 case 0x04: /* SRSHR / URSHR */
6973 case 0x06: /* SRSRA / URSRA */
6974 handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
6975 break;
6976 case 0x0a: /* SHL / SLI */
6977 handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
6978 break;
6979 case 0x1c: /* SCVTF, UCVTF */
6980 handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
6981 opcode, rn, rd);
6982 break;
6983 case 0x10: /* SQSHRUN, SQSHRUN2 */
6984 case 0x11: /* SQRSHRUN, SQRSHRUN2 */
6985 if (!is_u) {
6986 unallocated_encoding(s);
6987 return;
6989 handle_vec_simd_sqshrn(s, true, false, false, true,
6990 immh, immb, opcode, rn, rd);
6991 break;
6992 case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
6993 case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
6994 handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
6995 immh, immb, opcode, rn, rd);
6996 break;
6997 case 0xc: /* SQSHLU */
6998 if (!is_u) {
6999 unallocated_encoding(s);
7000 return;
7002 handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
7003 break;
7004 case 0xe: /* SQSHL, UQSHL */
7005 handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
7006 break;
7007 case 0x1f: /* FCVTZS, FCVTZU */
7008 handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
7009 break;
7010 default:
7011 unallocated_encoding(s);
7012 break;
7016 /* C3.6.10 AdvSIMD scalar three different
7017 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
7018 * +-----+---+-----------+------+---+------+--------+-----+------+------+
7019 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
7020 * +-----+---+-----------+------+---+------+--------+-----+------+------+
7022 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
7024 bool is_u = extract32(insn, 29, 1);
7025 int size = extract32(insn, 22, 2);
7026 int opcode = extract32(insn, 12, 4);
7027 int rm = extract32(insn, 16, 5);
7028 int rn = extract32(insn, 5, 5);
7029 int rd = extract32(insn, 0, 5);
7031 if (is_u) {
7032 unallocated_encoding(s);
7033 return;
7036 switch (opcode) {
7037 case 0x9: /* SQDMLAL, SQDMLAL2 */
7038 case 0xb: /* SQDMLSL, SQDMLSL2 */
7039 case 0xd: /* SQDMULL, SQDMULL2 */
7040 if (size == 0 || size == 3) {
7041 unallocated_encoding(s);
7042 return;
7044 break;
7045 default:
7046 unallocated_encoding(s);
7047 return;
7050 if (!fp_access_check(s)) {
7051 return;
7054 if (size == 2) {
7055 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7056 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7057 TCGv_i64 tcg_res = tcg_temp_new_i64();
7059 read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
7060 read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
7062 tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
7063 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
7065 switch (opcode) {
7066 case 0xd: /* SQDMULL, SQDMULL2 */
7067 break;
7068 case 0xb: /* SQDMLSL, SQDMLSL2 */
7069 tcg_gen_neg_i64(tcg_res, tcg_res);
7070 /* fall through */
7071 case 0x9: /* SQDMLAL, SQDMLAL2 */
7072 read_vec_element(s, tcg_op1, rd, 0, MO_64);
7073 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
7074 tcg_res, tcg_op1);
7075 break;
7076 default:
7077 g_assert_not_reached();
7080 write_fp_dreg(s, rd, tcg_res);
7082 tcg_temp_free_i64(tcg_op1);
7083 tcg_temp_free_i64(tcg_op2);
7084 tcg_temp_free_i64(tcg_res);
7085 } else {
7086 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7087 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7088 TCGv_i64 tcg_res = tcg_temp_new_i64();
7090 read_vec_element_i32(s, tcg_op1, rn, 0, MO_16);
7091 read_vec_element_i32(s, tcg_op2, rm, 0, MO_16);
7093 gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
7094 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
7096 switch (opcode) {
7097 case 0xd: /* SQDMULL, SQDMULL2 */
7098 break;
7099 case 0xb: /* SQDMLSL, SQDMLSL2 */
7100 gen_helper_neon_negl_u32(tcg_res, tcg_res);
7101 /* fall through */
7102 case 0x9: /* SQDMLAL, SQDMLAL2 */
7104 TCGv_i64 tcg_op3 = tcg_temp_new_i64();
7105 read_vec_element(s, tcg_op3, rd, 0, MO_32);
7106 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
7107 tcg_res, tcg_op3);
7108 tcg_temp_free_i64(tcg_op3);
7109 break;
7111 default:
7112 g_assert_not_reached();
7115 tcg_gen_ext32u_i64(tcg_res, tcg_res);
7116 write_fp_dreg(s, rd, tcg_res);
7118 tcg_temp_free_i32(tcg_op1);
7119 tcg_temp_free_i32(tcg_op2);
7120 tcg_temp_free_i64(tcg_res);
7124 static void handle_3same_64(DisasContext *s, int opcode, bool u,
7125 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
7127 /* Handle 64x64->64 opcodes which are shared between the scalar
7128 * and vector 3-same groups. We cover every opcode where size == 3
7129 * is valid in either the three-reg-same (integer, not pairwise)
7130 * or scalar-three-reg-same groups. (Some opcodes are not yet
7131 * implemented.)
7133 TCGCond cond;
7135 switch (opcode) {
7136 case 0x1: /* SQADD */
7137 if (u) {
7138 gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7139 } else {
7140 gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7142 break;
7143 case 0x5: /* SQSUB */
7144 if (u) {
7145 gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7146 } else {
7147 gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7149 break;
7150 case 0x6: /* CMGT, CMHI */
7151 /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
7152 * We implement this using setcond (test) and then negating.
7154 cond = u ? TCG_COND_GTU : TCG_COND_GT;
7155 do_cmop:
7156 tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
7157 tcg_gen_neg_i64(tcg_rd, tcg_rd);
7158 break;
7159 case 0x7: /* CMGE, CMHS */
7160 cond = u ? TCG_COND_GEU : TCG_COND_GE;
7161 goto do_cmop;
7162 case 0x11: /* CMTST, CMEQ */
7163 if (u) {
7164 cond = TCG_COND_EQ;
7165 goto do_cmop;
7167 /* CMTST : test is "if (X & Y != 0)". */
7168 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
7169 tcg_gen_setcondi_i64(TCG_COND_NE, tcg_rd, tcg_rd, 0);
7170 tcg_gen_neg_i64(tcg_rd, tcg_rd);
7171 break;
7172 case 0x8: /* SSHL, USHL */
7173 if (u) {
7174 gen_helper_neon_shl_u64(tcg_rd, tcg_rn, tcg_rm);
7175 } else {
7176 gen_helper_neon_shl_s64(tcg_rd, tcg_rn, tcg_rm);
7178 break;
7179 case 0x9: /* SQSHL, UQSHL */
7180 if (u) {
7181 gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7182 } else {
7183 gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7185 break;
7186 case 0xa: /* SRSHL, URSHL */
7187 if (u) {
7188 gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
7189 } else {
7190 gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
7192 break;
7193 case 0xb: /* SQRSHL, UQRSHL */
7194 if (u) {
7195 gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7196 } else {
7197 gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7199 break;
7200 case 0x10: /* ADD, SUB */
7201 if (u) {
7202 tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
7203 } else {
7204 tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
7206 break;
7207 default:
7208 g_assert_not_reached();
7212 /* Handle the 3-same-operands float operations; shared by the scalar
7213 * and vector encodings. The caller must filter out any encodings
7214 * not allocated for the encoding it is dealing with.
7216 static void handle_3same_float(DisasContext *s, int size, int elements,
7217 int fpopcode, int rd, int rn, int rm)
7219 int pass;
7220 TCGv_ptr fpst = get_fpstatus_ptr();
7222 for (pass = 0; pass < elements; pass++) {
7223 if (size) {
7224 /* Double */
7225 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7226 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7227 TCGv_i64 tcg_res = tcg_temp_new_i64();
7229 read_vec_element(s, tcg_op1, rn, pass, MO_64);
7230 read_vec_element(s, tcg_op2, rm, pass, MO_64);
7232 switch (fpopcode) {
7233 case 0x39: /* FMLS */
7234 /* As usual for ARM, separate negation for fused multiply-add */
7235 gen_helper_vfp_negd(tcg_op1, tcg_op1);
7236 /* fall through */
7237 case 0x19: /* FMLA */
7238 read_vec_element(s, tcg_res, rd, pass, MO_64);
7239 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
7240 tcg_res, fpst);
7241 break;
7242 case 0x18: /* FMAXNM */
7243 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7244 break;
7245 case 0x1a: /* FADD */
7246 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
7247 break;
7248 case 0x1b: /* FMULX */
7249 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
7250 break;
7251 case 0x1c: /* FCMEQ */
7252 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7253 break;
7254 case 0x1e: /* FMAX */
7255 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
7256 break;
7257 case 0x1f: /* FRECPS */
7258 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7259 break;
7260 case 0x38: /* FMINNM */
7261 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7262 break;
7263 case 0x3a: /* FSUB */
7264 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
7265 break;
7266 case 0x3e: /* FMIN */
7267 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
7268 break;
7269 case 0x3f: /* FRSQRTS */
7270 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7271 break;
7272 case 0x5b: /* FMUL */
7273 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
7274 break;
7275 case 0x5c: /* FCMGE */
7276 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7277 break;
7278 case 0x5d: /* FACGE */
7279 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7280 break;
7281 case 0x5f: /* FDIV */
7282 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
7283 break;
7284 case 0x7a: /* FABD */
7285 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
7286 gen_helper_vfp_absd(tcg_res, tcg_res);
7287 break;
7288 case 0x7c: /* FCMGT */
7289 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7290 break;
7291 case 0x7d: /* FACGT */
7292 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7293 break;
7294 default:
7295 g_assert_not_reached();
7298 write_vec_element(s, tcg_res, rd, pass, MO_64);
7300 tcg_temp_free_i64(tcg_res);
7301 tcg_temp_free_i64(tcg_op1);
7302 tcg_temp_free_i64(tcg_op2);
7303 } else {
7304 /* Single */
7305 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7306 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7307 TCGv_i32 tcg_res = tcg_temp_new_i32();
7309 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
7310 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
7312 switch (fpopcode) {
7313 case 0x39: /* FMLS */
7314 /* As usual for ARM, separate negation for fused multiply-add */
7315 gen_helper_vfp_negs(tcg_op1, tcg_op1);
7316 /* fall through */
7317 case 0x19: /* FMLA */
7318 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7319 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
7320 tcg_res, fpst);
7321 break;
7322 case 0x1a: /* FADD */
7323 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
7324 break;
7325 case 0x1b: /* FMULX */
7326 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
7327 break;
7328 case 0x1c: /* FCMEQ */
7329 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7330 break;
7331 case 0x1e: /* FMAX */
7332 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
7333 break;
7334 case 0x1f: /* FRECPS */
7335 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7336 break;
7337 case 0x18: /* FMAXNM */
7338 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
7339 break;
7340 case 0x38: /* FMINNM */
7341 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
7342 break;
7343 case 0x3a: /* FSUB */
7344 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
7345 break;
7346 case 0x3e: /* FMIN */
7347 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
7348 break;
7349 case 0x3f: /* FRSQRTS */
7350 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7351 break;
7352 case 0x5b: /* FMUL */
7353 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
7354 break;
7355 case 0x5c: /* FCMGE */
7356 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7357 break;
7358 case 0x5d: /* FACGE */
7359 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7360 break;
7361 case 0x5f: /* FDIV */
7362 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
7363 break;
7364 case 0x7a: /* FABD */
7365 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
7366 gen_helper_vfp_abss(tcg_res, tcg_res);
7367 break;
7368 case 0x7c: /* FCMGT */
7369 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7370 break;
7371 case 0x7d: /* FACGT */
7372 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7373 break;
7374 default:
7375 g_assert_not_reached();
7378 if (elements == 1) {
7379 /* scalar single so clear high part */
7380 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7382 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
7383 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
7384 tcg_temp_free_i64(tcg_tmp);
7385 } else {
7386 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7389 tcg_temp_free_i32(tcg_res);
7390 tcg_temp_free_i32(tcg_op1);
7391 tcg_temp_free_i32(tcg_op2);
7395 tcg_temp_free_ptr(fpst);
7397 if ((elements << size) < 4) {
7398 /* scalar, or non-quad vector op */
7399 clear_vec_high(s, rd);
7403 /* C3.6.11 AdvSIMD scalar three same
7404 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
7405 * +-----+---+-----------+------+---+------+--------+---+------+------+
7406 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
7407 * +-----+---+-----------+------+---+------+--------+---+------+------+
7409 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
7411 int rd = extract32(insn, 0, 5);
7412 int rn = extract32(insn, 5, 5);
7413 int opcode = extract32(insn, 11, 5);
7414 int rm = extract32(insn, 16, 5);
7415 int size = extract32(insn, 22, 2);
7416 bool u = extract32(insn, 29, 1);
7417 TCGv_i64 tcg_rd;
7419 if (opcode >= 0x18) {
7420 /* Floating point: U, size[1] and opcode indicate operation */
7421 int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
7422 switch (fpopcode) {
7423 case 0x1b: /* FMULX */
7424 case 0x1f: /* FRECPS */
7425 case 0x3f: /* FRSQRTS */
7426 case 0x5d: /* FACGE */
7427 case 0x7d: /* FACGT */
7428 case 0x1c: /* FCMEQ */
7429 case 0x5c: /* FCMGE */
7430 case 0x7c: /* FCMGT */
7431 case 0x7a: /* FABD */
7432 break;
7433 default:
7434 unallocated_encoding(s);
7435 return;
7438 if (!fp_access_check(s)) {
7439 return;
7442 handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
7443 return;
7446 switch (opcode) {
7447 case 0x1: /* SQADD, UQADD */
7448 case 0x5: /* SQSUB, UQSUB */
7449 case 0x9: /* SQSHL, UQSHL */
7450 case 0xb: /* SQRSHL, UQRSHL */
7451 break;
7452 case 0x8: /* SSHL, USHL */
7453 case 0xa: /* SRSHL, URSHL */
7454 case 0x6: /* CMGT, CMHI */
7455 case 0x7: /* CMGE, CMHS */
7456 case 0x11: /* CMTST, CMEQ */
7457 case 0x10: /* ADD, SUB (vector) */
7458 if (size != 3) {
7459 unallocated_encoding(s);
7460 return;
7462 break;
7463 case 0x16: /* SQDMULH, SQRDMULH (vector) */
7464 if (size != 1 && size != 2) {
7465 unallocated_encoding(s);
7466 return;
7468 break;
7469 default:
7470 unallocated_encoding(s);
7471 return;
7474 if (!fp_access_check(s)) {
7475 return;
7478 tcg_rd = tcg_temp_new_i64();
7480 if (size == 3) {
7481 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
7482 TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
7484 handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
7485 tcg_temp_free_i64(tcg_rn);
7486 tcg_temp_free_i64(tcg_rm);
7487 } else {
7488 /* Do a single operation on the lowest element in the vector.
7489 * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
7490 * no side effects for all these operations.
7491 * OPTME: special-purpose helpers would avoid doing some
7492 * unnecessary work in the helper for the 8 and 16 bit cases.
7494 NeonGenTwoOpEnvFn *genenvfn;
7495 TCGv_i32 tcg_rn = tcg_temp_new_i32();
7496 TCGv_i32 tcg_rm = tcg_temp_new_i32();
7497 TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
7499 read_vec_element_i32(s, tcg_rn, rn, 0, size);
7500 read_vec_element_i32(s, tcg_rm, rm, 0, size);
7502 switch (opcode) {
7503 case 0x1: /* SQADD, UQADD */
7505 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7506 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
7507 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
7508 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
7510 genenvfn = fns[size][u];
7511 break;
7513 case 0x5: /* SQSUB, UQSUB */
7515 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7516 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
7517 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
7518 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
7520 genenvfn = fns[size][u];
7521 break;
7523 case 0x9: /* SQSHL, UQSHL */
7525 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7526 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
7527 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
7528 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
7530 genenvfn = fns[size][u];
7531 break;
7533 case 0xb: /* SQRSHL, UQRSHL */
7535 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7536 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
7537 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
7538 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
7540 genenvfn = fns[size][u];
7541 break;
7543 case 0x16: /* SQDMULH, SQRDMULH */
7545 static NeonGenTwoOpEnvFn * const fns[2][2] = {
7546 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
7547 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
7549 assert(size == 1 || size == 2);
7550 genenvfn = fns[size - 1][u];
7551 break;
7553 default:
7554 g_assert_not_reached();
7557 genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
7558 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
7559 tcg_temp_free_i32(tcg_rd32);
7560 tcg_temp_free_i32(tcg_rn);
7561 tcg_temp_free_i32(tcg_rm);
7564 write_fp_dreg(s, rd, tcg_rd);
7566 tcg_temp_free_i64(tcg_rd);
7569 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
7570 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
7571 TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
7573 /* Handle 64->64 opcodes which are shared between the scalar and
7574 * vector 2-reg-misc groups. We cover every integer opcode where size == 3
7575 * is valid in either group and also the double-precision fp ops.
7576 * The caller only need provide tcg_rmode and tcg_fpstatus if the op
7577 * requires them.
7579 TCGCond cond;
7581 switch (opcode) {
7582 case 0x4: /* CLS, CLZ */
7583 if (u) {
7584 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
7585 } else {
7586 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
7588 break;
7589 case 0x5: /* NOT */
7590 /* This opcode is shared with CNT and RBIT but we have earlier
7591 * enforced that size == 3 if and only if this is the NOT insn.
7593 tcg_gen_not_i64(tcg_rd, tcg_rn);
7594 break;
7595 case 0x7: /* SQABS, SQNEG */
7596 if (u) {
7597 gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
7598 } else {
7599 gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
7601 break;
7602 case 0xa: /* CMLT */
7603 /* 64 bit integer comparison against zero, result is
7604 * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
7605 * subtracting 1.
7607 cond = TCG_COND_LT;
7608 do_cmop:
7609 tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
7610 tcg_gen_neg_i64(tcg_rd, tcg_rd);
7611 break;
7612 case 0x8: /* CMGT, CMGE */
7613 cond = u ? TCG_COND_GE : TCG_COND_GT;
7614 goto do_cmop;
7615 case 0x9: /* CMEQ, CMLE */
7616 cond = u ? TCG_COND_LE : TCG_COND_EQ;
7617 goto do_cmop;
7618 case 0xb: /* ABS, NEG */
7619 if (u) {
7620 tcg_gen_neg_i64(tcg_rd, tcg_rn);
7621 } else {
7622 TCGv_i64 tcg_zero = tcg_const_i64(0);
7623 tcg_gen_neg_i64(tcg_rd, tcg_rn);
7624 tcg_gen_movcond_i64(TCG_COND_GT, tcg_rd, tcg_rn, tcg_zero,
7625 tcg_rn, tcg_rd);
7626 tcg_temp_free_i64(tcg_zero);
7628 break;
7629 case 0x2f: /* FABS */
7630 gen_helper_vfp_absd(tcg_rd, tcg_rn);
7631 break;
7632 case 0x6f: /* FNEG */
7633 gen_helper_vfp_negd(tcg_rd, tcg_rn);
7634 break;
7635 case 0x7f: /* FSQRT */
7636 gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
7637 break;
7638 case 0x1a: /* FCVTNS */
7639 case 0x1b: /* FCVTMS */
7640 case 0x1c: /* FCVTAS */
7641 case 0x3a: /* FCVTPS */
7642 case 0x3b: /* FCVTZS */
7644 TCGv_i32 tcg_shift = tcg_const_i32(0);
7645 gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7646 tcg_temp_free_i32(tcg_shift);
7647 break;
7649 case 0x5a: /* FCVTNU */
7650 case 0x5b: /* FCVTMU */
7651 case 0x5c: /* FCVTAU */
7652 case 0x7a: /* FCVTPU */
7653 case 0x7b: /* FCVTZU */
7655 TCGv_i32 tcg_shift = tcg_const_i32(0);
7656 gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7657 tcg_temp_free_i32(tcg_shift);
7658 break;
7660 case 0x18: /* FRINTN */
7661 case 0x19: /* FRINTM */
7662 case 0x38: /* FRINTP */
7663 case 0x39: /* FRINTZ */
7664 case 0x58: /* FRINTA */
7665 case 0x79: /* FRINTI */
7666 gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
7667 break;
7668 case 0x59: /* FRINTX */
7669 gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
7670 break;
7671 default:
7672 g_assert_not_reached();
7676 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
7677 bool is_scalar, bool is_u, bool is_q,
7678 int size, int rn, int rd)
7680 bool is_double = (size == 3);
7681 TCGv_ptr fpst;
7683 if (!fp_access_check(s)) {
7684 return;
7687 fpst = get_fpstatus_ptr();
7689 if (is_double) {
7690 TCGv_i64 tcg_op = tcg_temp_new_i64();
7691 TCGv_i64 tcg_zero = tcg_const_i64(0);
7692 TCGv_i64 tcg_res = tcg_temp_new_i64();
7693 NeonGenTwoDoubleOPFn *genfn;
7694 bool swap = false;
7695 int pass;
7697 switch (opcode) {
7698 case 0x2e: /* FCMLT (zero) */
7699 swap = true;
7700 /* fallthrough */
7701 case 0x2c: /* FCMGT (zero) */
7702 genfn = gen_helper_neon_cgt_f64;
7703 break;
7704 case 0x2d: /* FCMEQ (zero) */
7705 genfn = gen_helper_neon_ceq_f64;
7706 break;
7707 case 0x6d: /* FCMLE (zero) */
7708 swap = true;
7709 /* fall through */
7710 case 0x6c: /* FCMGE (zero) */
7711 genfn = gen_helper_neon_cge_f64;
7712 break;
7713 default:
7714 g_assert_not_reached();
7717 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7718 read_vec_element(s, tcg_op, rn, pass, MO_64);
7719 if (swap) {
7720 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7721 } else {
7722 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7724 write_vec_element(s, tcg_res, rd, pass, MO_64);
7726 if (is_scalar) {
7727 clear_vec_high(s, rd);
7730 tcg_temp_free_i64(tcg_res);
7731 tcg_temp_free_i64(tcg_zero);
7732 tcg_temp_free_i64(tcg_op);
7733 } else {
7734 TCGv_i32 tcg_op = tcg_temp_new_i32();
7735 TCGv_i32 tcg_zero = tcg_const_i32(0);
7736 TCGv_i32 tcg_res = tcg_temp_new_i32();
7737 NeonGenTwoSingleOPFn *genfn;
7738 bool swap = false;
7739 int pass, maxpasses;
7741 switch (opcode) {
7742 case 0x2e: /* FCMLT (zero) */
7743 swap = true;
7744 /* fall through */
7745 case 0x2c: /* FCMGT (zero) */
7746 genfn = gen_helper_neon_cgt_f32;
7747 break;
7748 case 0x2d: /* FCMEQ (zero) */
7749 genfn = gen_helper_neon_ceq_f32;
7750 break;
7751 case 0x6d: /* FCMLE (zero) */
7752 swap = true;
7753 /* fall through */
7754 case 0x6c: /* FCMGE (zero) */
7755 genfn = gen_helper_neon_cge_f32;
7756 break;
7757 default:
7758 g_assert_not_reached();
7761 if (is_scalar) {
7762 maxpasses = 1;
7763 } else {
7764 maxpasses = is_q ? 4 : 2;
7767 for (pass = 0; pass < maxpasses; pass++) {
7768 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7769 if (swap) {
7770 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7771 } else {
7772 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7774 if (is_scalar) {
7775 write_fp_sreg(s, rd, tcg_res);
7776 } else {
7777 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7780 tcg_temp_free_i32(tcg_res);
7781 tcg_temp_free_i32(tcg_zero);
7782 tcg_temp_free_i32(tcg_op);
7783 if (!is_q && !is_scalar) {
7784 clear_vec_high(s, rd);
7788 tcg_temp_free_ptr(fpst);
7791 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
7792 bool is_scalar, bool is_u, bool is_q,
7793 int size, int rn, int rd)
7795 bool is_double = (size == 3);
7796 TCGv_ptr fpst = get_fpstatus_ptr();
7798 if (is_double) {
7799 TCGv_i64 tcg_op = tcg_temp_new_i64();
7800 TCGv_i64 tcg_res = tcg_temp_new_i64();
7801 int pass;
7803 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7804 read_vec_element(s, tcg_op, rn, pass, MO_64);
7805 switch (opcode) {
7806 case 0x3d: /* FRECPE */
7807 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
7808 break;
7809 case 0x3f: /* FRECPX */
7810 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
7811 break;
7812 case 0x7d: /* FRSQRTE */
7813 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
7814 break;
7815 default:
7816 g_assert_not_reached();
7818 write_vec_element(s, tcg_res, rd, pass, MO_64);
7820 if (is_scalar) {
7821 clear_vec_high(s, rd);
7824 tcg_temp_free_i64(tcg_res);
7825 tcg_temp_free_i64(tcg_op);
7826 } else {
7827 TCGv_i32 tcg_op = tcg_temp_new_i32();
7828 TCGv_i32 tcg_res = tcg_temp_new_i32();
7829 int pass, maxpasses;
7831 if (is_scalar) {
7832 maxpasses = 1;
7833 } else {
7834 maxpasses = is_q ? 4 : 2;
7837 for (pass = 0; pass < maxpasses; pass++) {
7838 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7840 switch (opcode) {
7841 case 0x3c: /* URECPE */
7842 gen_helper_recpe_u32(tcg_res, tcg_op, fpst);
7843 break;
7844 case 0x3d: /* FRECPE */
7845 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
7846 break;
7847 case 0x3f: /* FRECPX */
7848 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
7849 break;
7850 case 0x7d: /* FRSQRTE */
7851 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
7852 break;
7853 default:
7854 g_assert_not_reached();
7857 if (is_scalar) {
7858 write_fp_sreg(s, rd, tcg_res);
7859 } else {
7860 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7863 tcg_temp_free_i32(tcg_res);
7864 tcg_temp_free_i32(tcg_op);
7865 if (!is_q && !is_scalar) {
7866 clear_vec_high(s, rd);
7869 tcg_temp_free_ptr(fpst);
7872 static void handle_2misc_narrow(DisasContext *s, bool scalar,
7873 int opcode, bool u, bool is_q,
7874 int size, int rn, int rd)
7876 /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
7877 * in the source becomes a size element in the destination).
7879 int pass;
7880 TCGv_i32 tcg_res[2];
7881 int destelt = is_q ? 2 : 0;
7882 int passes = scalar ? 1 : 2;
7884 if (scalar) {
7885 tcg_res[1] = tcg_const_i32(0);
7888 for (pass = 0; pass < passes; pass++) {
7889 TCGv_i64 tcg_op = tcg_temp_new_i64();
7890 NeonGenNarrowFn *genfn = NULL;
7891 NeonGenNarrowEnvFn *genenvfn = NULL;
7893 if (scalar) {
7894 read_vec_element(s, tcg_op, rn, pass, size + 1);
7895 } else {
7896 read_vec_element(s, tcg_op, rn, pass, MO_64);
7898 tcg_res[pass] = tcg_temp_new_i32();
7900 switch (opcode) {
7901 case 0x12: /* XTN, SQXTUN */
7903 static NeonGenNarrowFn * const xtnfns[3] = {
7904 gen_helper_neon_narrow_u8,
7905 gen_helper_neon_narrow_u16,
7906 tcg_gen_extrl_i64_i32,
7908 static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
7909 gen_helper_neon_unarrow_sat8,
7910 gen_helper_neon_unarrow_sat16,
7911 gen_helper_neon_unarrow_sat32,
7913 if (u) {
7914 genenvfn = sqxtunfns[size];
7915 } else {
7916 genfn = xtnfns[size];
7918 break;
7920 case 0x14: /* SQXTN, UQXTN */
7922 static NeonGenNarrowEnvFn * const fns[3][2] = {
7923 { gen_helper_neon_narrow_sat_s8,
7924 gen_helper_neon_narrow_sat_u8 },
7925 { gen_helper_neon_narrow_sat_s16,
7926 gen_helper_neon_narrow_sat_u16 },
7927 { gen_helper_neon_narrow_sat_s32,
7928 gen_helper_neon_narrow_sat_u32 },
7930 genenvfn = fns[size][u];
7931 break;
7933 case 0x16: /* FCVTN, FCVTN2 */
7934 /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
7935 if (size == 2) {
7936 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
7937 } else {
7938 TCGv_i32 tcg_lo = tcg_temp_new_i32();
7939 TCGv_i32 tcg_hi = tcg_temp_new_i32();
7940 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
7941 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, cpu_env);
7942 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, cpu_env);
7943 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
7944 tcg_temp_free_i32(tcg_lo);
7945 tcg_temp_free_i32(tcg_hi);
7947 break;
7948 case 0x56: /* FCVTXN, FCVTXN2 */
7949 /* 64 bit to 32 bit float conversion
7950 * with von Neumann rounding (round to odd)
7952 assert(size == 2);
7953 gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
7954 break;
7955 default:
7956 g_assert_not_reached();
7959 if (genfn) {
7960 genfn(tcg_res[pass], tcg_op);
7961 } else if (genenvfn) {
7962 genenvfn(tcg_res[pass], cpu_env, tcg_op);
7965 tcg_temp_free_i64(tcg_op);
7968 for (pass = 0; pass < 2; pass++) {
7969 write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
7970 tcg_temp_free_i32(tcg_res[pass]);
7972 if (!is_q) {
7973 clear_vec_high(s, rd);
7977 /* Remaining saturating accumulating ops */
7978 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
7979 bool is_q, int size, int rn, int rd)
7981 bool is_double = (size == 3);
7983 if (is_double) {
7984 TCGv_i64 tcg_rn = tcg_temp_new_i64();
7985 TCGv_i64 tcg_rd = tcg_temp_new_i64();
7986 int pass;
7988 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7989 read_vec_element(s, tcg_rn, rn, pass, MO_64);
7990 read_vec_element(s, tcg_rd, rd, pass, MO_64);
7992 if (is_u) { /* USQADD */
7993 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7994 } else { /* SUQADD */
7995 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7997 write_vec_element(s, tcg_rd, rd, pass, MO_64);
7999 if (is_scalar) {
8000 clear_vec_high(s, rd);
8003 tcg_temp_free_i64(tcg_rd);
8004 tcg_temp_free_i64(tcg_rn);
8005 } else {
8006 TCGv_i32 tcg_rn = tcg_temp_new_i32();
8007 TCGv_i32 tcg_rd = tcg_temp_new_i32();
8008 int pass, maxpasses;
8010 if (is_scalar) {
8011 maxpasses = 1;
8012 } else {
8013 maxpasses = is_q ? 4 : 2;
8016 for (pass = 0; pass < maxpasses; pass++) {
8017 if (is_scalar) {
8018 read_vec_element_i32(s, tcg_rn, rn, pass, size);
8019 read_vec_element_i32(s, tcg_rd, rd, pass, size);
8020 } else {
8021 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
8022 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
8025 if (is_u) { /* USQADD */
8026 switch (size) {
8027 case 0:
8028 gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8029 break;
8030 case 1:
8031 gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8032 break;
8033 case 2:
8034 gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8035 break;
8036 default:
8037 g_assert_not_reached();
8039 } else { /* SUQADD */
8040 switch (size) {
8041 case 0:
8042 gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8043 break;
8044 case 1:
8045 gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8046 break;
8047 case 2:
8048 gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8049 break;
8050 default:
8051 g_assert_not_reached();
8055 if (is_scalar) {
8056 TCGv_i64 tcg_zero = tcg_const_i64(0);
8057 write_vec_element(s, tcg_zero, rd, 0, MO_64);
8058 tcg_temp_free_i64(tcg_zero);
8060 write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
8063 if (!is_q) {
8064 clear_vec_high(s, rd);
8067 tcg_temp_free_i32(tcg_rd);
8068 tcg_temp_free_i32(tcg_rn);
8072 /* C3.6.12 AdvSIMD scalar two reg misc
8073 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
8074 * +-----+---+-----------+------+-----------+--------+-----+------+------+
8075 * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
8076 * +-----+---+-----------+------+-----------+--------+-----+------+------+
8078 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
8080 int rd = extract32(insn, 0, 5);
8081 int rn = extract32(insn, 5, 5);
8082 int opcode = extract32(insn, 12, 5);
8083 int size = extract32(insn, 22, 2);
8084 bool u = extract32(insn, 29, 1);
8085 bool is_fcvt = false;
8086 int rmode;
8087 TCGv_i32 tcg_rmode;
8088 TCGv_ptr tcg_fpstatus;
8090 switch (opcode) {
8091 case 0x3: /* USQADD / SUQADD*/
8092 if (!fp_access_check(s)) {
8093 return;
8095 handle_2misc_satacc(s, true, u, false, size, rn, rd);
8096 return;
8097 case 0x7: /* SQABS / SQNEG */
8098 break;
8099 case 0xa: /* CMLT */
8100 if (u) {
8101 unallocated_encoding(s);
8102 return;
8104 /* fall through */
8105 case 0x8: /* CMGT, CMGE */
8106 case 0x9: /* CMEQ, CMLE */
8107 case 0xb: /* ABS, NEG */
8108 if (size != 3) {
8109 unallocated_encoding(s);
8110 return;
8112 break;
8113 case 0x12: /* SQXTUN */
8114 if (!u) {
8115 unallocated_encoding(s);
8116 return;
8118 /* fall through */
8119 case 0x14: /* SQXTN, UQXTN */
8120 if (size == 3) {
8121 unallocated_encoding(s);
8122 return;
8124 if (!fp_access_check(s)) {
8125 return;
8127 handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
8128 return;
8129 case 0xc ... 0xf:
8130 case 0x16 ... 0x1d:
8131 case 0x1f:
8132 /* Floating point: U, size[1] and opcode indicate operation;
8133 * size[0] indicates single or double precision.
8135 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
8136 size = extract32(size, 0, 1) ? 3 : 2;
8137 switch (opcode) {
8138 case 0x2c: /* FCMGT (zero) */
8139 case 0x2d: /* FCMEQ (zero) */
8140 case 0x2e: /* FCMLT (zero) */
8141 case 0x6c: /* FCMGE (zero) */
8142 case 0x6d: /* FCMLE (zero) */
8143 handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
8144 return;
8145 case 0x1d: /* SCVTF */
8146 case 0x5d: /* UCVTF */
8148 bool is_signed = (opcode == 0x1d);
8149 if (!fp_access_check(s)) {
8150 return;
8152 handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
8153 return;
8155 case 0x3d: /* FRECPE */
8156 case 0x3f: /* FRECPX */
8157 case 0x7d: /* FRSQRTE */
8158 if (!fp_access_check(s)) {
8159 return;
8161 handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
8162 return;
8163 case 0x1a: /* FCVTNS */
8164 case 0x1b: /* FCVTMS */
8165 case 0x3a: /* FCVTPS */
8166 case 0x3b: /* FCVTZS */
8167 case 0x5a: /* FCVTNU */
8168 case 0x5b: /* FCVTMU */
8169 case 0x7a: /* FCVTPU */
8170 case 0x7b: /* FCVTZU */
8171 is_fcvt = true;
8172 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
8173 break;
8174 case 0x1c: /* FCVTAS */
8175 case 0x5c: /* FCVTAU */
8176 /* TIEAWAY doesn't fit in the usual rounding mode encoding */
8177 is_fcvt = true;
8178 rmode = FPROUNDING_TIEAWAY;
8179 break;
8180 case 0x56: /* FCVTXN, FCVTXN2 */
8181 if (size == 2) {
8182 unallocated_encoding(s);
8183 return;
8185 if (!fp_access_check(s)) {
8186 return;
8188 handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
8189 return;
8190 default:
8191 unallocated_encoding(s);
8192 return;
8194 break;
8195 default:
8196 unallocated_encoding(s);
8197 return;
8200 if (!fp_access_check(s)) {
8201 return;
8204 if (is_fcvt) {
8205 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
8206 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
8207 tcg_fpstatus = get_fpstatus_ptr();
8208 } else {
8209 TCGV_UNUSED_I32(tcg_rmode);
8210 TCGV_UNUSED_PTR(tcg_fpstatus);
8213 if (size == 3) {
8214 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
8215 TCGv_i64 tcg_rd = tcg_temp_new_i64();
8217 handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
8218 write_fp_dreg(s, rd, tcg_rd);
8219 tcg_temp_free_i64(tcg_rd);
8220 tcg_temp_free_i64(tcg_rn);
8221 } else {
8222 TCGv_i32 tcg_rn = tcg_temp_new_i32();
8223 TCGv_i32 tcg_rd = tcg_temp_new_i32();
8225 read_vec_element_i32(s, tcg_rn, rn, 0, size);
8227 switch (opcode) {
8228 case 0x7: /* SQABS, SQNEG */
8230 NeonGenOneOpEnvFn *genfn;
8231 static NeonGenOneOpEnvFn * const fns[3][2] = {
8232 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
8233 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
8234 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
8236 genfn = fns[size][u];
8237 genfn(tcg_rd, cpu_env, tcg_rn);
8238 break;
8240 case 0x1a: /* FCVTNS */
8241 case 0x1b: /* FCVTMS */
8242 case 0x1c: /* FCVTAS */
8243 case 0x3a: /* FCVTPS */
8244 case 0x3b: /* FCVTZS */
8246 TCGv_i32 tcg_shift = tcg_const_i32(0);
8247 gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
8248 tcg_temp_free_i32(tcg_shift);
8249 break;
8251 case 0x5a: /* FCVTNU */
8252 case 0x5b: /* FCVTMU */
8253 case 0x5c: /* FCVTAU */
8254 case 0x7a: /* FCVTPU */
8255 case 0x7b: /* FCVTZU */
8257 TCGv_i32 tcg_shift = tcg_const_i32(0);
8258 gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
8259 tcg_temp_free_i32(tcg_shift);
8260 break;
8262 default:
8263 g_assert_not_reached();
8266 write_fp_sreg(s, rd, tcg_rd);
8267 tcg_temp_free_i32(tcg_rd);
8268 tcg_temp_free_i32(tcg_rn);
8271 if (is_fcvt) {
8272 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
8273 tcg_temp_free_i32(tcg_rmode);
8274 tcg_temp_free_ptr(tcg_fpstatus);
8278 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
8279 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
8280 int immh, int immb, int opcode, int rn, int rd)
8282 int size = 32 - clz32(immh) - 1;
8283 int immhb = immh << 3 | immb;
8284 int shift = 2 * (8 << size) - immhb;
8285 bool accumulate = false;
8286 bool round = false;
8287 bool insert = false;
8288 int dsize = is_q ? 128 : 64;
8289 int esize = 8 << size;
8290 int elements = dsize/esize;
8291 TCGMemOp memop = size | (is_u ? 0 : MO_SIGN);
8292 TCGv_i64 tcg_rn = new_tmp_a64(s);
8293 TCGv_i64 tcg_rd = new_tmp_a64(s);
8294 TCGv_i64 tcg_round;
8295 int i;
8297 if (extract32(immh, 3, 1) && !is_q) {
8298 unallocated_encoding(s);
8299 return;
8302 if (size > 3 && !is_q) {
8303 unallocated_encoding(s);
8304 return;
8307 if (!fp_access_check(s)) {
8308 return;
8311 switch (opcode) {
8312 case 0x02: /* SSRA / USRA (accumulate) */
8313 accumulate = true;
8314 break;
8315 case 0x04: /* SRSHR / URSHR (rounding) */
8316 round = true;
8317 break;
8318 case 0x06: /* SRSRA / URSRA (accum + rounding) */
8319 accumulate = round = true;
8320 break;
8321 case 0x08: /* SRI */
8322 insert = true;
8323 break;
8326 if (round) {
8327 uint64_t round_const = 1ULL << (shift - 1);
8328 tcg_round = tcg_const_i64(round_const);
8329 } else {
8330 TCGV_UNUSED_I64(tcg_round);
8333 for (i = 0; i < elements; i++) {
8334 read_vec_element(s, tcg_rn, rn, i, memop);
8335 if (accumulate || insert) {
8336 read_vec_element(s, tcg_rd, rd, i, memop);
8339 if (insert) {
8340 handle_shri_with_ins(tcg_rd, tcg_rn, size, shift);
8341 } else {
8342 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8343 accumulate, is_u, size, shift);
8346 write_vec_element(s, tcg_rd, rd, i, size);
8349 if (!is_q) {
8350 clear_vec_high(s, rd);
8353 if (round) {
8354 tcg_temp_free_i64(tcg_round);
8358 /* SHL/SLI - Vector shift left */
8359 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
8360 int immh, int immb, int opcode, int rn, int rd)
8362 int size = 32 - clz32(immh) - 1;
8363 int immhb = immh << 3 | immb;
8364 int shift = immhb - (8 << size);
8365 int dsize = is_q ? 128 : 64;
8366 int esize = 8 << size;
8367 int elements = dsize/esize;
8368 TCGv_i64 tcg_rn = new_tmp_a64(s);
8369 TCGv_i64 tcg_rd = new_tmp_a64(s);
8370 int i;
8372 if (extract32(immh, 3, 1) && !is_q) {
8373 unallocated_encoding(s);
8374 return;
8377 if (size > 3 && !is_q) {
8378 unallocated_encoding(s);
8379 return;
8382 if (!fp_access_check(s)) {
8383 return;
8386 for (i = 0; i < elements; i++) {
8387 read_vec_element(s, tcg_rn, rn, i, size);
8388 if (insert) {
8389 read_vec_element(s, tcg_rd, rd, i, size);
8392 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
8394 write_vec_element(s, tcg_rd, rd, i, size);
8397 if (!is_q) {
8398 clear_vec_high(s, rd);
8402 /* USHLL/SHLL - Vector shift left with widening */
8403 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
8404 int immh, int immb, int opcode, int rn, int rd)
8406 int size = 32 - clz32(immh) - 1;
8407 int immhb = immh << 3 | immb;
8408 int shift = immhb - (8 << size);
8409 int dsize = 64;
8410 int esize = 8 << size;
8411 int elements = dsize/esize;
8412 TCGv_i64 tcg_rn = new_tmp_a64(s);
8413 TCGv_i64 tcg_rd = new_tmp_a64(s);
8414 int i;
8416 if (size >= 3) {
8417 unallocated_encoding(s);
8418 return;
8421 if (!fp_access_check(s)) {
8422 return;
8425 /* For the LL variants the store is larger than the load,
8426 * so if rd == rn we would overwrite parts of our input.
8427 * So load everything right now and use shifts in the main loop.
8429 read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
8431 for (i = 0; i < elements; i++) {
8432 tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
8433 ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
8434 tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
8435 write_vec_element(s, tcg_rd, rd, i, size + 1);
8439 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
8440 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
8441 int immh, int immb, int opcode, int rn, int rd)
8443 int immhb = immh << 3 | immb;
8444 int size = 32 - clz32(immh) - 1;
8445 int dsize = 64;
8446 int esize = 8 << size;
8447 int elements = dsize/esize;
8448 int shift = (2 * esize) - immhb;
8449 bool round = extract32(opcode, 0, 1);
8450 TCGv_i64 tcg_rn, tcg_rd, tcg_final;
8451 TCGv_i64 tcg_round;
8452 int i;
8454 if (extract32(immh, 3, 1)) {
8455 unallocated_encoding(s);
8456 return;
8459 if (!fp_access_check(s)) {
8460 return;
8463 tcg_rn = tcg_temp_new_i64();
8464 tcg_rd = tcg_temp_new_i64();
8465 tcg_final = tcg_temp_new_i64();
8466 read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
8468 if (round) {
8469 uint64_t round_const = 1ULL << (shift - 1);
8470 tcg_round = tcg_const_i64(round_const);
8471 } else {
8472 TCGV_UNUSED_I64(tcg_round);
8475 for (i = 0; i < elements; i++) {
8476 read_vec_element(s, tcg_rn, rn, i, size+1);
8477 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8478 false, true, size+1, shift);
8480 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8483 if (!is_q) {
8484 clear_vec_high(s, rd);
8485 write_vec_element(s, tcg_final, rd, 0, MO_64);
8486 } else {
8487 write_vec_element(s, tcg_final, rd, 1, MO_64);
8490 if (round) {
8491 tcg_temp_free_i64(tcg_round);
8493 tcg_temp_free_i64(tcg_rn);
8494 tcg_temp_free_i64(tcg_rd);
8495 tcg_temp_free_i64(tcg_final);
8496 return;
8500 /* C3.6.14 AdvSIMD shift by immediate
8501 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
8502 * +---+---+---+-------------+------+------+--------+---+------+------+
8503 * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
8504 * +---+---+---+-------------+------+------+--------+---+------+------+
8506 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
8508 int rd = extract32(insn, 0, 5);
8509 int rn = extract32(insn, 5, 5);
8510 int opcode = extract32(insn, 11, 5);
8511 int immb = extract32(insn, 16, 3);
8512 int immh = extract32(insn, 19, 4);
8513 bool is_u = extract32(insn, 29, 1);
8514 bool is_q = extract32(insn, 30, 1);
8516 switch (opcode) {
8517 case 0x08: /* SRI */
8518 if (!is_u) {
8519 unallocated_encoding(s);
8520 return;
8522 /* fall through */
8523 case 0x00: /* SSHR / USHR */
8524 case 0x02: /* SSRA / USRA (accumulate) */
8525 case 0x04: /* SRSHR / URSHR (rounding) */
8526 case 0x06: /* SRSRA / URSRA (accum + rounding) */
8527 handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
8528 break;
8529 case 0x0a: /* SHL / SLI */
8530 handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
8531 break;
8532 case 0x10: /* SHRN */
8533 case 0x11: /* RSHRN / SQRSHRUN */
8534 if (is_u) {
8535 handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
8536 opcode, rn, rd);
8537 } else {
8538 handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
8540 break;
8541 case 0x12: /* SQSHRN / UQSHRN */
8542 case 0x13: /* SQRSHRN / UQRSHRN */
8543 handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
8544 opcode, rn, rd);
8545 break;
8546 case 0x14: /* SSHLL / USHLL */
8547 handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
8548 break;
8549 case 0x1c: /* SCVTF / UCVTF */
8550 handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
8551 opcode, rn, rd);
8552 break;
8553 case 0xc: /* SQSHLU */
8554 if (!is_u) {
8555 unallocated_encoding(s);
8556 return;
8558 handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
8559 break;
8560 case 0xe: /* SQSHL, UQSHL */
8561 handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
8562 break;
8563 case 0x1f: /* FCVTZS/ FCVTZU */
8564 handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
8565 return;
8566 default:
8567 unallocated_encoding(s);
8568 return;
8572 /* Generate code to do a "long" addition or subtraction, ie one done in
8573 * TCGv_i64 on vector lanes twice the width specified by size.
8575 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
8576 TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
8578 static NeonGenTwo64OpFn * const fns[3][2] = {
8579 { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
8580 { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
8581 { tcg_gen_add_i64, tcg_gen_sub_i64 },
8583 NeonGenTwo64OpFn *genfn;
8584 assert(size < 3);
8586 genfn = fns[size][is_sub];
8587 genfn(tcg_res, tcg_op1, tcg_op2);
8590 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
8591 int opcode, int rd, int rn, int rm)
8593 /* 3-reg-different widening insns: 64 x 64 -> 128 */
8594 TCGv_i64 tcg_res[2];
8595 int pass, accop;
8597 tcg_res[0] = tcg_temp_new_i64();
8598 tcg_res[1] = tcg_temp_new_i64();
8600 /* Does this op do an adding accumulate, a subtracting accumulate,
8601 * or no accumulate at all?
8603 switch (opcode) {
8604 case 5:
8605 case 8:
8606 case 9:
8607 accop = 1;
8608 break;
8609 case 10:
8610 case 11:
8611 accop = -1;
8612 break;
8613 default:
8614 accop = 0;
8615 break;
8618 if (accop != 0) {
8619 read_vec_element(s, tcg_res[0], rd, 0, MO_64);
8620 read_vec_element(s, tcg_res[1], rd, 1, MO_64);
8623 /* size == 2 means two 32x32->64 operations; this is worth special
8624 * casing because we can generally handle it inline.
8626 if (size == 2) {
8627 for (pass = 0; pass < 2; pass++) {
8628 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8629 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8630 TCGv_i64 tcg_passres;
8631 TCGMemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
8633 int elt = pass + is_q * 2;
8635 read_vec_element(s, tcg_op1, rn, elt, memop);
8636 read_vec_element(s, tcg_op2, rm, elt, memop);
8638 if (accop == 0) {
8639 tcg_passres = tcg_res[pass];
8640 } else {
8641 tcg_passres = tcg_temp_new_i64();
8644 switch (opcode) {
8645 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8646 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
8647 break;
8648 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8649 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
8650 break;
8651 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8652 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8654 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
8655 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
8657 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
8658 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
8659 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
8660 tcg_passres,
8661 tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
8662 tcg_temp_free_i64(tcg_tmp1);
8663 tcg_temp_free_i64(tcg_tmp2);
8664 break;
8666 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
8667 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
8668 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
8669 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
8670 break;
8671 case 9: /* SQDMLAL, SQDMLAL2 */
8672 case 11: /* SQDMLSL, SQDMLSL2 */
8673 case 13: /* SQDMULL, SQDMULL2 */
8674 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
8675 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
8676 tcg_passres, tcg_passres);
8677 break;
8678 default:
8679 g_assert_not_reached();
8682 if (opcode == 9 || opcode == 11) {
8683 /* saturating accumulate ops */
8684 if (accop < 0) {
8685 tcg_gen_neg_i64(tcg_passres, tcg_passres);
8687 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
8688 tcg_res[pass], tcg_passres);
8689 } else if (accop > 0) {
8690 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
8691 } else if (accop < 0) {
8692 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
8695 if (accop != 0) {
8696 tcg_temp_free_i64(tcg_passres);
8699 tcg_temp_free_i64(tcg_op1);
8700 tcg_temp_free_i64(tcg_op2);
8702 } else {
8703 /* size 0 or 1, generally helper functions */
8704 for (pass = 0; pass < 2; pass++) {
8705 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8706 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8707 TCGv_i64 tcg_passres;
8708 int elt = pass + is_q * 2;
8710 read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
8711 read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
8713 if (accop == 0) {
8714 tcg_passres = tcg_res[pass];
8715 } else {
8716 tcg_passres = tcg_temp_new_i64();
8719 switch (opcode) {
8720 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8721 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8723 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
8724 static NeonGenWidenFn * const widenfns[2][2] = {
8725 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
8726 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
8728 NeonGenWidenFn *widenfn = widenfns[size][is_u];
8730 widenfn(tcg_op2_64, tcg_op2);
8731 widenfn(tcg_passres, tcg_op1);
8732 gen_neon_addl(size, (opcode == 2), tcg_passres,
8733 tcg_passres, tcg_op2_64);
8734 tcg_temp_free_i64(tcg_op2_64);
8735 break;
8737 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8738 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8739 if (size == 0) {
8740 if (is_u) {
8741 gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
8742 } else {
8743 gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
8745 } else {
8746 if (is_u) {
8747 gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
8748 } else {
8749 gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
8752 break;
8753 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
8754 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
8755 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
8756 if (size == 0) {
8757 if (is_u) {
8758 gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
8759 } else {
8760 gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
8762 } else {
8763 if (is_u) {
8764 gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
8765 } else {
8766 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
8769 break;
8770 case 9: /* SQDMLAL, SQDMLAL2 */
8771 case 11: /* SQDMLSL, SQDMLSL2 */
8772 case 13: /* SQDMULL, SQDMULL2 */
8773 assert(size == 1);
8774 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
8775 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
8776 tcg_passres, tcg_passres);
8777 break;
8778 case 14: /* PMULL */
8779 assert(size == 0);
8780 gen_helper_neon_mull_p8(tcg_passres, tcg_op1, tcg_op2);
8781 break;
8782 default:
8783 g_assert_not_reached();
8785 tcg_temp_free_i32(tcg_op1);
8786 tcg_temp_free_i32(tcg_op2);
8788 if (accop != 0) {
8789 if (opcode == 9 || opcode == 11) {
8790 /* saturating accumulate ops */
8791 if (accop < 0) {
8792 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
8794 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
8795 tcg_res[pass],
8796 tcg_passres);
8797 } else {
8798 gen_neon_addl(size, (accop < 0), tcg_res[pass],
8799 tcg_res[pass], tcg_passres);
8801 tcg_temp_free_i64(tcg_passres);
8806 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
8807 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
8808 tcg_temp_free_i64(tcg_res[0]);
8809 tcg_temp_free_i64(tcg_res[1]);
8812 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
8813 int opcode, int rd, int rn, int rm)
8815 TCGv_i64 tcg_res[2];
8816 int part = is_q ? 2 : 0;
8817 int pass;
8819 for (pass = 0; pass < 2; pass++) {
8820 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8821 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8822 TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
8823 static NeonGenWidenFn * const widenfns[3][2] = {
8824 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
8825 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
8826 { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
8828 NeonGenWidenFn *widenfn = widenfns[size][is_u];
8830 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8831 read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
8832 widenfn(tcg_op2_wide, tcg_op2);
8833 tcg_temp_free_i32(tcg_op2);
8834 tcg_res[pass] = tcg_temp_new_i64();
8835 gen_neon_addl(size, (opcode == 3),
8836 tcg_res[pass], tcg_op1, tcg_op2_wide);
8837 tcg_temp_free_i64(tcg_op1);
8838 tcg_temp_free_i64(tcg_op2_wide);
8841 for (pass = 0; pass < 2; pass++) {
8842 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
8843 tcg_temp_free_i64(tcg_res[pass]);
8847 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
8849 tcg_gen_addi_i64(in, in, 1U << 31);
8850 tcg_gen_extrh_i64_i32(res, in);
8853 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
8854 int opcode, int rd, int rn, int rm)
8856 TCGv_i32 tcg_res[2];
8857 int part = is_q ? 2 : 0;
8858 int pass;
8860 for (pass = 0; pass < 2; pass++) {
8861 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8862 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8863 TCGv_i64 tcg_wideres = tcg_temp_new_i64();
8864 static NeonGenNarrowFn * const narrowfns[3][2] = {
8865 { gen_helper_neon_narrow_high_u8,
8866 gen_helper_neon_narrow_round_high_u8 },
8867 { gen_helper_neon_narrow_high_u16,
8868 gen_helper_neon_narrow_round_high_u16 },
8869 { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
8871 NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
8873 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8874 read_vec_element(s, tcg_op2, rm, pass, MO_64);
8876 gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
8878 tcg_temp_free_i64(tcg_op1);
8879 tcg_temp_free_i64(tcg_op2);
8881 tcg_res[pass] = tcg_temp_new_i32();
8882 gennarrow(tcg_res[pass], tcg_wideres);
8883 tcg_temp_free_i64(tcg_wideres);
8886 for (pass = 0; pass < 2; pass++) {
8887 write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
8888 tcg_temp_free_i32(tcg_res[pass]);
8890 if (!is_q) {
8891 clear_vec_high(s, rd);
8895 static void handle_pmull_64(DisasContext *s, int is_q, int rd, int rn, int rm)
8897 /* PMULL of 64 x 64 -> 128 is an odd special case because it
8898 * is the only three-reg-diff instruction which produces a
8899 * 128-bit wide result from a single operation. However since
8900 * it's possible to calculate the two halves more or less
8901 * separately we just use two helper calls.
8903 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8904 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8905 TCGv_i64 tcg_res = tcg_temp_new_i64();
8907 read_vec_element(s, tcg_op1, rn, is_q, MO_64);
8908 read_vec_element(s, tcg_op2, rm, is_q, MO_64);
8909 gen_helper_neon_pmull_64_lo(tcg_res, tcg_op1, tcg_op2);
8910 write_vec_element(s, tcg_res, rd, 0, MO_64);
8911 gen_helper_neon_pmull_64_hi(tcg_res, tcg_op1, tcg_op2);
8912 write_vec_element(s, tcg_res, rd, 1, MO_64);
8914 tcg_temp_free_i64(tcg_op1);
8915 tcg_temp_free_i64(tcg_op2);
8916 tcg_temp_free_i64(tcg_res);
8919 /* C3.6.15 AdvSIMD three different
8920 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
8921 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
8922 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
8923 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
8925 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
8927 /* Instructions in this group fall into three basic classes
8928 * (in each case with the operation working on each element in
8929 * the input vectors):
8930 * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
8931 * 128 bit input)
8932 * (2) wide 64 x 128 -> 128
8933 * (3) narrowing 128 x 128 -> 64
8934 * Here we do initial decode, catch unallocated cases and
8935 * dispatch to separate functions for each class.
8937 int is_q = extract32(insn, 30, 1);
8938 int is_u = extract32(insn, 29, 1);
8939 int size = extract32(insn, 22, 2);
8940 int opcode = extract32(insn, 12, 4);
8941 int rm = extract32(insn, 16, 5);
8942 int rn = extract32(insn, 5, 5);
8943 int rd = extract32(insn, 0, 5);
8945 switch (opcode) {
8946 case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
8947 case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
8948 /* 64 x 128 -> 128 */
8949 if (size == 3) {
8950 unallocated_encoding(s);
8951 return;
8953 if (!fp_access_check(s)) {
8954 return;
8956 handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
8957 break;
8958 case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
8959 case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
8960 /* 128 x 128 -> 64 */
8961 if (size == 3) {
8962 unallocated_encoding(s);
8963 return;
8965 if (!fp_access_check(s)) {
8966 return;
8968 handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
8969 break;
8970 case 14: /* PMULL, PMULL2 */
8971 if (is_u || size == 1 || size == 2) {
8972 unallocated_encoding(s);
8973 return;
8975 if (size == 3) {
8976 if (!arm_dc_feature(s, ARM_FEATURE_V8_PMULL)) {
8977 unallocated_encoding(s);
8978 return;
8980 if (!fp_access_check(s)) {
8981 return;
8983 handle_pmull_64(s, is_q, rd, rn, rm);
8984 return;
8986 goto is_widening;
8987 case 9: /* SQDMLAL, SQDMLAL2 */
8988 case 11: /* SQDMLSL, SQDMLSL2 */
8989 case 13: /* SQDMULL, SQDMULL2 */
8990 if (is_u || size == 0) {
8991 unallocated_encoding(s);
8992 return;
8994 /* fall through */
8995 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8996 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8997 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8998 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8999 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
9000 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
9001 case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
9002 /* 64 x 64 -> 128 */
9003 if (size == 3) {
9004 unallocated_encoding(s);
9005 return;
9007 is_widening:
9008 if (!fp_access_check(s)) {
9009 return;
9012 handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
9013 break;
9014 default:
9015 /* opcode 15 not allocated */
9016 unallocated_encoding(s);
9017 break;
9021 /* Logic op (opcode == 3) subgroup of C3.6.16. */
9022 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
9024 int rd = extract32(insn, 0, 5);
9025 int rn = extract32(insn, 5, 5);
9026 int rm = extract32(insn, 16, 5);
9027 int size = extract32(insn, 22, 2);
9028 bool is_u = extract32(insn, 29, 1);
9029 bool is_q = extract32(insn, 30, 1);
9030 TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
9031 int pass;
9033 if (!fp_access_check(s)) {
9034 return;
9037 tcg_op1 = tcg_temp_new_i64();
9038 tcg_op2 = tcg_temp_new_i64();
9039 tcg_res[0] = tcg_temp_new_i64();
9040 tcg_res[1] = tcg_temp_new_i64();
9042 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
9043 read_vec_element(s, tcg_op1, rn, pass, MO_64);
9044 read_vec_element(s, tcg_op2, rm, pass, MO_64);
9046 if (!is_u) {
9047 switch (size) {
9048 case 0: /* AND */
9049 tcg_gen_and_i64(tcg_res[pass], tcg_op1, tcg_op2);
9050 break;
9051 case 1: /* BIC */
9052 tcg_gen_andc_i64(tcg_res[pass], tcg_op1, tcg_op2);
9053 break;
9054 case 2: /* ORR */
9055 tcg_gen_or_i64(tcg_res[pass], tcg_op1, tcg_op2);
9056 break;
9057 case 3: /* ORN */
9058 tcg_gen_orc_i64(tcg_res[pass], tcg_op1, tcg_op2);
9059 break;
9061 } else {
9062 if (size != 0) {
9063 /* B* ops need res loaded to operate on */
9064 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9067 switch (size) {
9068 case 0: /* EOR */
9069 tcg_gen_xor_i64(tcg_res[pass], tcg_op1, tcg_op2);
9070 break;
9071 case 1: /* BSL bitwise select */
9072 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_op2);
9073 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_res[pass]);
9074 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op1);
9075 break;
9076 case 2: /* BIT, bitwise insert if true */
9077 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
9078 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_op2);
9079 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
9080 break;
9081 case 3: /* BIF, bitwise insert if false */
9082 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
9083 tcg_gen_andc_i64(tcg_op1, tcg_op1, tcg_op2);
9084 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
9085 break;
9090 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
9091 if (!is_q) {
9092 tcg_gen_movi_i64(tcg_res[1], 0);
9094 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
9096 tcg_temp_free_i64(tcg_op1);
9097 tcg_temp_free_i64(tcg_op2);
9098 tcg_temp_free_i64(tcg_res[0]);
9099 tcg_temp_free_i64(tcg_res[1]);
9102 /* Helper functions for 32 bit comparisons */
9103 static void gen_max_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9105 tcg_gen_movcond_i32(TCG_COND_GE, res, op1, op2, op1, op2);
9108 static void gen_max_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9110 tcg_gen_movcond_i32(TCG_COND_GEU, res, op1, op2, op1, op2);
9113 static void gen_min_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9115 tcg_gen_movcond_i32(TCG_COND_LE, res, op1, op2, op1, op2);
9118 static void gen_min_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9120 tcg_gen_movcond_i32(TCG_COND_LEU, res, op1, op2, op1, op2);
9123 /* Pairwise op subgroup of C3.6.16.
9125 * This is called directly or via the handle_3same_float for float pairwise
9126 * operations where the opcode and size are calculated differently.
9128 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
9129 int size, int rn, int rm, int rd)
9131 TCGv_ptr fpst;
9132 int pass;
9134 /* Floating point operations need fpst */
9135 if (opcode >= 0x58) {
9136 fpst = get_fpstatus_ptr();
9137 } else {
9138 TCGV_UNUSED_PTR(fpst);
9141 if (!fp_access_check(s)) {
9142 return;
9145 /* These operations work on the concatenated rm:rn, with each pair of
9146 * adjacent elements being operated on to produce an element in the result.
9148 if (size == 3) {
9149 TCGv_i64 tcg_res[2];
9151 for (pass = 0; pass < 2; pass++) {
9152 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9153 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9154 int passreg = (pass == 0) ? rn : rm;
9156 read_vec_element(s, tcg_op1, passreg, 0, MO_64);
9157 read_vec_element(s, tcg_op2, passreg, 1, MO_64);
9158 tcg_res[pass] = tcg_temp_new_i64();
9160 switch (opcode) {
9161 case 0x17: /* ADDP */
9162 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
9163 break;
9164 case 0x58: /* FMAXNMP */
9165 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9166 break;
9167 case 0x5a: /* FADDP */
9168 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9169 break;
9170 case 0x5e: /* FMAXP */
9171 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9172 break;
9173 case 0x78: /* FMINNMP */
9174 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9175 break;
9176 case 0x7e: /* FMINP */
9177 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9178 break;
9179 default:
9180 g_assert_not_reached();
9183 tcg_temp_free_i64(tcg_op1);
9184 tcg_temp_free_i64(tcg_op2);
9187 for (pass = 0; pass < 2; pass++) {
9188 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9189 tcg_temp_free_i64(tcg_res[pass]);
9191 } else {
9192 int maxpass = is_q ? 4 : 2;
9193 TCGv_i32 tcg_res[4];
9195 for (pass = 0; pass < maxpass; pass++) {
9196 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9197 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9198 NeonGenTwoOpFn *genfn = NULL;
9199 int passreg = pass < (maxpass / 2) ? rn : rm;
9200 int passelt = (is_q && (pass & 1)) ? 2 : 0;
9202 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
9203 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
9204 tcg_res[pass] = tcg_temp_new_i32();
9206 switch (opcode) {
9207 case 0x17: /* ADDP */
9209 static NeonGenTwoOpFn * const fns[3] = {
9210 gen_helper_neon_padd_u8,
9211 gen_helper_neon_padd_u16,
9212 tcg_gen_add_i32,
9214 genfn = fns[size];
9215 break;
9217 case 0x14: /* SMAXP, UMAXP */
9219 static NeonGenTwoOpFn * const fns[3][2] = {
9220 { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
9221 { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
9222 { gen_max_s32, gen_max_u32 },
9224 genfn = fns[size][u];
9225 break;
9227 case 0x15: /* SMINP, UMINP */
9229 static NeonGenTwoOpFn * const fns[3][2] = {
9230 { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
9231 { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
9232 { gen_min_s32, gen_min_u32 },
9234 genfn = fns[size][u];
9235 break;
9237 /* The FP operations are all on single floats (32 bit) */
9238 case 0x58: /* FMAXNMP */
9239 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9240 break;
9241 case 0x5a: /* FADDP */
9242 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9243 break;
9244 case 0x5e: /* FMAXP */
9245 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9246 break;
9247 case 0x78: /* FMINNMP */
9248 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9249 break;
9250 case 0x7e: /* FMINP */
9251 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9252 break;
9253 default:
9254 g_assert_not_reached();
9257 /* FP ops called directly, otherwise call now */
9258 if (genfn) {
9259 genfn(tcg_res[pass], tcg_op1, tcg_op2);
9262 tcg_temp_free_i32(tcg_op1);
9263 tcg_temp_free_i32(tcg_op2);
9266 for (pass = 0; pass < maxpass; pass++) {
9267 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
9268 tcg_temp_free_i32(tcg_res[pass]);
9270 if (!is_q) {
9271 clear_vec_high(s, rd);
9275 if (!TCGV_IS_UNUSED_PTR(fpst)) {
9276 tcg_temp_free_ptr(fpst);
9280 /* Floating point op subgroup of C3.6.16. */
9281 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
9283 /* For floating point ops, the U, size[1] and opcode bits
9284 * together indicate the operation. size[0] indicates single
9285 * or double.
9287 int fpopcode = extract32(insn, 11, 5)
9288 | (extract32(insn, 23, 1) << 5)
9289 | (extract32(insn, 29, 1) << 6);
9290 int is_q = extract32(insn, 30, 1);
9291 int size = extract32(insn, 22, 1);
9292 int rm = extract32(insn, 16, 5);
9293 int rn = extract32(insn, 5, 5);
9294 int rd = extract32(insn, 0, 5);
9296 int datasize = is_q ? 128 : 64;
9297 int esize = 32 << size;
9298 int elements = datasize / esize;
9300 if (size == 1 && !is_q) {
9301 unallocated_encoding(s);
9302 return;
9305 switch (fpopcode) {
9306 case 0x58: /* FMAXNMP */
9307 case 0x5a: /* FADDP */
9308 case 0x5e: /* FMAXP */
9309 case 0x78: /* FMINNMP */
9310 case 0x7e: /* FMINP */
9311 if (size && !is_q) {
9312 unallocated_encoding(s);
9313 return;
9315 handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
9316 rn, rm, rd);
9317 return;
9318 case 0x1b: /* FMULX */
9319 case 0x1f: /* FRECPS */
9320 case 0x3f: /* FRSQRTS */
9321 case 0x5d: /* FACGE */
9322 case 0x7d: /* FACGT */
9323 case 0x19: /* FMLA */
9324 case 0x39: /* FMLS */
9325 case 0x18: /* FMAXNM */
9326 case 0x1a: /* FADD */
9327 case 0x1c: /* FCMEQ */
9328 case 0x1e: /* FMAX */
9329 case 0x38: /* FMINNM */
9330 case 0x3a: /* FSUB */
9331 case 0x3e: /* FMIN */
9332 case 0x5b: /* FMUL */
9333 case 0x5c: /* FCMGE */
9334 case 0x5f: /* FDIV */
9335 case 0x7a: /* FABD */
9336 case 0x7c: /* FCMGT */
9337 if (!fp_access_check(s)) {
9338 return;
9341 handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
9342 return;
9343 default:
9344 unallocated_encoding(s);
9345 return;
9349 /* Integer op subgroup of C3.6.16. */
9350 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
9352 int is_q = extract32(insn, 30, 1);
9353 int u = extract32(insn, 29, 1);
9354 int size = extract32(insn, 22, 2);
9355 int opcode = extract32(insn, 11, 5);
9356 int rm = extract32(insn, 16, 5);
9357 int rn = extract32(insn, 5, 5);
9358 int rd = extract32(insn, 0, 5);
9359 int pass;
9361 switch (opcode) {
9362 case 0x13: /* MUL, PMUL */
9363 if (u && size != 0) {
9364 unallocated_encoding(s);
9365 return;
9367 /* fall through */
9368 case 0x0: /* SHADD, UHADD */
9369 case 0x2: /* SRHADD, URHADD */
9370 case 0x4: /* SHSUB, UHSUB */
9371 case 0xc: /* SMAX, UMAX */
9372 case 0xd: /* SMIN, UMIN */
9373 case 0xe: /* SABD, UABD */
9374 case 0xf: /* SABA, UABA */
9375 case 0x12: /* MLA, MLS */
9376 if (size == 3) {
9377 unallocated_encoding(s);
9378 return;
9380 break;
9381 case 0x16: /* SQDMULH, SQRDMULH */
9382 if (size == 0 || size == 3) {
9383 unallocated_encoding(s);
9384 return;
9386 break;
9387 default:
9388 if (size == 3 && !is_q) {
9389 unallocated_encoding(s);
9390 return;
9392 break;
9395 if (!fp_access_check(s)) {
9396 return;
9399 if (size == 3) {
9400 assert(is_q);
9401 for (pass = 0; pass < 2; pass++) {
9402 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9403 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9404 TCGv_i64 tcg_res = tcg_temp_new_i64();
9406 read_vec_element(s, tcg_op1, rn, pass, MO_64);
9407 read_vec_element(s, tcg_op2, rm, pass, MO_64);
9409 handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
9411 write_vec_element(s, tcg_res, rd, pass, MO_64);
9413 tcg_temp_free_i64(tcg_res);
9414 tcg_temp_free_i64(tcg_op1);
9415 tcg_temp_free_i64(tcg_op2);
9417 } else {
9418 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
9419 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9420 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9421 TCGv_i32 tcg_res = tcg_temp_new_i32();
9422 NeonGenTwoOpFn *genfn = NULL;
9423 NeonGenTwoOpEnvFn *genenvfn = NULL;
9425 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9426 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9428 switch (opcode) {
9429 case 0x0: /* SHADD, UHADD */
9431 static NeonGenTwoOpFn * const fns[3][2] = {
9432 { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
9433 { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
9434 { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
9436 genfn = fns[size][u];
9437 break;
9439 case 0x1: /* SQADD, UQADD */
9441 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9442 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9443 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9444 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9446 genenvfn = fns[size][u];
9447 break;
9449 case 0x2: /* SRHADD, URHADD */
9451 static NeonGenTwoOpFn * const fns[3][2] = {
9452 { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
9453 { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
9454 { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
9456 genfn = fns[size][u];
9457 break;
9459 case 0x4: /* SHSUB, UHSUB */
9461 static NeonGenTwoOpFn * const fns[3][2] = {
9462 { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
9463 { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
9464 { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
9466 genfn = fns[size][u];
9467 break;
9469 case 0x5: /* SQSUB, UQSUB */
9471 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9472 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9473 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9474 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9476 genenvfn = fns[size][u];
9477 break;
9479 case 0x6: /* CMGT, CMHI */
9481 static NeonGenTwoOpFn * const fns[3][2] = {
9482 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_u8 },
9483 { gen_helper_neon_cgt_s16, gen_helper_neon_cgt_u16 },
9484 { gen_helper_neon_cgt_s32, gen_helper_neon_cgt_u32 },
9486 genfn = fns[size][u];
9487 break;
9489 case 0x7: /* CMGE, CMHS */
9491 static NeonGenTwoOpFn * const fns[3][2] = {
9492 { gen_helper_neon_cge_s8, gen_helper_neon_cge_u8 },
9493 { gen_helper_neon_cge_s16, gen_helper_neon_cge_u16 },
9494 { gen_helper_neon_cge_s32, gen_helper_neon_cge_u32 },
9496 genfn = fns[size][u];
9497 break;
9499 case 0x8: /* SSHL, USHL */
9501 static NeonGenTwoOpFn * const fns[3][2] = {
9502 { gen_helper_neon_shl_s8, gen_helper_neon_shl_u8 },
9503 { gen_helper_neon_shl_s16, gen_helper_neon_shl_u16 },
9504 { gen_helper_neon_shl_s32, gen_helper_neon_shl_u32 },
9506 genfn = fns[size][u];
9507 break;
9509 case 0x9: /* SQSHL, UQSHL */
9511 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9512 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9513 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9514 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9516 genenvfn = fns[size][u];
9517 break;
9519 case 0xa: /* SRSHL, URSHL */
9521 static NeonGenTwoOpFn * const fns[3][2] = {
9522 { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
9523 { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
9524 { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
9526 genfn = fns[size][u];
9527 break;
9529 case 0xb: /* SQRSHL, UQRSHL */
9531 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9532 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9533 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9534 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9536 genenvfn = fns[size][u];
9537 break;
9539 case 0xc: /* SMAX, UMAX */
9541 static NeonGenTwoOpFn * const fns[3][2] = {
9542 { gen_helper_neon_max_s8, gen_helper_neon_max_u8 },
9543 { gen_helper_neon_max_s16, gen_helper_neon_max_u16 },
9544 { gen_max_s32, gen_max_u32 },
9546 genfn = fns[size][u];
9547 break;
9550 case 0xd: /* SMIN, UMIN */
9552 static NeonGenTwoOpFn * const fns[3][2] = {
9553 { gen_helper_neon_min_s8, gen_helper_neon_min_u8 },
9554 { gen_helper_neon_min_s16, gen_helper_neon_min_u16 },
9555 { gen_min_s32, gen_min_u32 },
9557 genfn = fns[size][u];
9558 break;
9560 case 0xe: /* SABD, UABD */
9561 case 0xf: /* SABA, UABA */
9563 static NeonGenTwoOpFn * const fns[3][2] = {
9564 { gen_helper_neon_abd_s8, gen_helper_neon_abd_u8 },
9565 { gen_helper_neon_abd_s16, gen_helper_neon_abd_u16 },
9566 { gen_helper_neon_abd_s32, gen_helper_neon_abd_u32 },
9568 genfn = fns[size][u];
9569 break;
9571 case 0x10: /* ADD, SUB */
9573 static NeonGenTwoOpFn * const fns[3][2] = {
9574 { gen_helper_neon_add_u8, gen_helper_neon_sub_u8 },
9575 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
9576 { tcg_gen_add_i32, tcg_gen_sub_i32 },
9578 genfn = fns[size][u];
9579 break;
9581 case 0x11: /* CMTST, CMEQ */
9583 static NeonGenTwoOpFn * const fns[3][2] = {
9584 { gen_helper_neon_tst_u8, gen_helper_neon_ceq_u8 },
9585 { gen_helper_neon_tst_u16, gen_helper_neon_ceq_u16 },
9586 { gen_helper_neon_tst_u32, gen_helper_neon_ceq_u32 },
9588 genfn = fns[size][u];
9589 break;
9591 case 0x13: /* MUL, PMUL */
9592 if (u) {
9593 /* PMUL */
9594 assert(size == 0);
9595 genfn = gen_helper_neon_mul_p8;
9596 break;
9598 /* fall through : MUL */
9599 case 0x12: /* MLA, MLS */
9601 static NeonGenTwoOpFn * const fns[3] = {
9602 gen_helper_neon_mul_u8,
9603 gen_helper_neon_mul_u16,
9604 tcg_gen_mul_i32,
9606 genfn = fns[size];
9607 break;
9609 case 0x16: /* SQDMULH, SQRDMULH */
9611 static NeonGenTwoOpEnvFn * const fns[2][2] = {
9612 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9613 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9615 assert(size == 1 || size == 2);
9616 genenvfn = fns[size - 1][u];
9617 break;
9619 default:
9620 g_assert_not_reached();
9623 if (genenvfn) {
9624 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
9625 } else {
9626 genfn(tcg_res, tcg_op1, tcg_op2);
9629 if (opcode == 0xf || opcode == 0x12) {
9630 /* SABA, UABA, MLA, MLS: accumulating ops */
9631 static NeonGenTwoOpFn * const fns[3][2] = {
9632 { gen_helper_neon_add_u8, gen_helper_neon_sub_u8 },
9633 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
9634 { tcg_gen_add_i32, tcg_gen_sub_i32 },
9636 bool is_sub = (opcode == 0x12 && u); /* MLS */
9638 genfn = fns[size][is_sub];
9639 read_vec_element_i32(s, tcg_op1, rd, pass, MO_32);
9640 genfn(tcg_res, tcg_op1, tcg_res);
9643 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9645 tcg_temp_free_i32(tcg_res);
9646 tcg_temp_free_i32(tcg_op1);
9647 tcg_temp_free_i32(tcg_op2);
9651 if (!is_q) {
9652 clear_vec_high(s, rd);
9656 /* C3.6.16 AdvSIMD three same
9657 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
9658 * +---+---+---+-----------+------+---+------+--------+---+------+------+
9659 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
9660 * +---+---+---+-----------+------+---+------+--------+---+------+------+
9662 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
9664 int opcode = extract32(insn, 11, 5);
9666 switch (opcode) {
9667 case 0x3: /* logic ops */
9668 disas_simd_3same_logic(s, insn);
9669 break;
9670 case 0x17: /* ADDP */
9671 case 0x14: /* SMAXP, UMAXP */
9672 case 0x15: /* SMINP, UMINP */
9674 /* Pairwise operations */
9675 int is_q = extract32(insn, 30, 1);
9676 int u = extract32(insn, 29, 1);
9677 int size = extract32(insn, 22, 2);
9678 int rm = extract32(insn, 16, 5);
9679 int rn = extract32(insn, 5, 5);
9680 int rd = extract32(insn, 0, 5);
9681 if (opcode == 0x17) {
9682 if (u || (size == 3 && !is_q)) {
9683 unallocated_encoding(s);
9684 return;
9686 } else {
9687 if (size == 3) {
9688 unallocated_encoding(s);
9689 return;
9692 handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
9693 break;
9695 case 0x18 ... 0x31:
9696 /* floating point ops, sz[1] and U are part of opcode */
9697 disas_simd_3same_float(s, insn);
9698 break;
9699 default:
9700 disas_simd_3same_int(s, insn);
9701 break;
9705 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
9706 int size, int rn, int rd)
9708 /* Handle 2-reg-misc ops which are widening (so each size element
9709 * in the source becomes a 2*size element in the destination.
9710 * The only instruction like this is FCVTL.
9712 int pass;
9714 if (size == 3) {
9715 /* 32 -> 64 bit fp conversion */
9716 TCGv_i64 tcg_res[2];
9717 int srcelt = is_q ? 2 : 0;
9719 for (pass = 0; pass < 2; pass++) {
9720 TCGv_i32 tcg_op = tcg_temp_new_i32();
9721 tcg_res[pass] = tcg_temp_new_i64();
9723 read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
9724 gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
9725 tcg_temp_free_i32(tcg_op);
9727 for (pass = 0; pass < 2; pass++) {
9728 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9729 tcg_temp_free_i64(tcg_res[pass]);
9731 } else {
9732 /* 16 -> 32 bit fp conversion */
9733 int srcelt = is_q ? 4 : 0;
9734 TCGv_i32 tcg_res[4];
9736 for (pass = 0; pass < 4; pass++) {
9737 tcg_res[pass] = tcg_temp_new_i32();
9739 read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
9740 gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
9741 cpu_env);
9743 for (pass = 0; pass < 4; pass++) {
9744 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
9745 tcg_temp_free_i32(tcg_res[pass]);
9750 static void handle_rev(DisasContext *s, int opcode, bool u,
9751 bool is_q, int size, int rn, int rd)
9753 int op = (opcode << 1) | u;
9754 int opsz = op + size;
9755 int grp_size = 3 - opsz;
9756 int dsize = is_q ? 128 : 64;
9757 int i;
9759 if (opsz >= 3) {
9760 unallocated_encoding(s);
9761 return;
9764 if (!fp_access_check(s)) {
9765 return;
9768 if (size == 0) {
9769 /* Special case bytes, use bswap op on each group of elements */
9770 int groups = dsize / (8 << grp_size);
9772 for (i = 0; i < groups; i++) {
9773 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9775 read_vec_element(s, tcg_tmp, rn, i, grp_size);
9776 switch (grp_size) {
9777 case MO_16:
9778 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
9779 break;
9780 case MO_32:
9781 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
9782 break;
9783 case MO_64:
9784 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
9785 break;
9786 default:
9787 g_assert_not_reached();
9789 write_vec_element(s, tcg_tmp, rd, i, grp_size);
9790 tcg_temp_free_i64(tcg_tmp);
9792 if (!is_q) {
9793 clear_vec_high(s, rd);
9795 } else {
9796 int revmask = (1 << grp_size) - 1;
9797 int esize = 8 << size;
9798 int elements = dsize / esize;
9799 TCGv_i64 tcg_rn = tcg_temp_new_i64();
9800 TCGv_i64 tcg_rd = tcg_const_i64(0);
9801 TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
9803 for (i = 0; i < elements; i++) {
9804 int e_rev = (i & 0xf) ^ revmask;
9805 int off = e_rev * esize;
9806 read_vec_element(s, tcg_rn, rn, i, size);
9807 if (off >= 64) {
9808 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
9809 tcg_rn, off - 64, esize);
9810 } else {
9811 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
9814 write_vec_element(s, tcg_rd, rd, 0, MO_64);
9815 write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
9817 tcg_temp_free_i64(tcg_rd_hi);
9818 tcg_temp_free_i64(tcg_rd);
9819 tcg_temp_free_i64(tcg_rn);
9823 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
9824 bool is_q, int size, int rn, int rd)
9826 /* Implement the pairwise operations from 2-misc:
9827 * SADDLP, UADDLP, SADALP, UADALP.
9828 * These all add pairs of elements in the input to produce a
9829 * double-width result element in the output (possibly accumulating).
9831 bool accum = (opcode == 0x6);
9832 int maxpass = is_q ? 2 : 1;
9833 int pass;
9834 TCGv_i64 tcg_res[2];
9836 if (size == 2) {
9837 /* 32 + 32 -> 64 op */
9838 TCGMemOp memop = size + (u ? 0 : MO_SIGN);
9840 for (pass = 0; pass < maxpass; pass++) {
9841 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9842 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9844 tcg_res[pass] = tcg_temp_new_i64();
9846 read_vec_element(s, tcg_op1, rn, pass * 2, memop);
9847 read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
9848 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
9849 if (accum) {
9850 read_vec_element(s, tcg_op1, rd, pass, MO_64);
9851 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
9854 tcg_temp_free_i64(tcg_op1);
9855 tcg_temp_free_i64(tcg_op2);
9857 } else {
9858 for (pass = 0; pass < maxpass; pass++) {
9859 TCGv_i64 tcg_op = tcg_temp_new_i64();
9860 NeonGenOneOpFn *genfn;
9861 static NeonGenOneOpFn * const fns[2][2] = {
9862 { gen_helper_neon_addlp_s8, gen_helper_neon_addlp_u8 },
9863 { gen_helper_neon_addlp_s16, gen_helper_neon_addlp_u16 },
9866 genfn = fns[size][u];
9868 tcg_res[pass] = tcg_temp_new_i64();
9870 read_vec_element(s, tcg_op, rn, pass, MO_64);
9871 genfn(tcg_res[pass], tcg_op);
9873 if (accum) {
9874 read_vec_element(s, tcg_op, rd, pass, MO_64);
9875 if (size == 0) {
9876 gen_helper_neon_addl_u16(tcg_res[pass],
9877 tcg_res[pass], tcg_op);
9878 } else {
9879 gen_helper_neon_addl_u32(tcg_res[pass],
9880 tcg_res[pass], tcg_op);
9883 tcg_temp_free_i64(tcg_op);
9886 if (!is_q) {
9887 tcg_res[1] = tcg_const_i64(0);
9889 for (pass = 0; pass < 2; pass++) {
9890 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9891 tcg_temp_free_i64(tcg_res[pass]);
9895 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
9897 /* Implement SHLL and SHLL2 */
9898 int pass;
9899 int part = is_q ? 2 : 0;
9900 TCGv_i64 tcg_res[2];
9902 for (pass = 0; pass < 2; pass++) {
9903 static NeonGenWidenFn * const widenfns[3] = {
9904 gen_helper_neon_widen_u8,
9905 gen_helper_neon_widen_u16,
9906 tcg_gen_extu_i32_i64,
9908 NeonGenWidenFn *widenfn = widenfns[size];
9909 TCGv_i32 tcg_op = tcg_temp_new_i32();
9911 read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
9912 tcg_res[pass] = tcg_temp_new_i64();
9913 widenfn(tcg_res[pass], tcg_op);
9914 tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
9916 tcg_temp_free_i32(tcg_op);
9919 for (pass = 0; pass < 2; pass++) {
9920 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9921 tcg_temp_free_i64(tcg_res[pass]);
9925 /* C3.6.17 AdvSIMD two reg misc
9926 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
9927 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
9928 * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
9929 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
9931 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
9933 int size = extract32(insn, 22, 2);
9934 int opcode = extract32(insn, 12, 5);
9935 bool u = extract32(insn, 29, 1);
9936 bool is_q = extract32(insn, 30, 1);
9937 int rn = extract32(insn, 5, 5);
9938 int rd = extract32(insn, 0, 5);
9939 bool need_fpstatus = false;
9940 bool need_rmode = false;
9941 int rmode = -1;
9942 TCGv_i32 tcg_rmode;
9943 TCGv_ptr tcg_fpstatus;
9945 switch (opcode) {
9946 case 0x0: /* REV64, REV32 */
9947 case 0x1: /* REV16 */
9948 handle_rev(s, opcode, u, is_q, size, rn, rd);
9949 return;
9950 case 0x5: /* CNT, NOT, RBIT */
9951 if (u && size == 0) {
9952 /* NOT: adjust size so we can use the 64-bits-at-a-time loop. */
9953 size = 3;
9954 break;
9955 } else if (u && size == 1) {
9956 /* RBIT */
9957 break;
9958 } else if (!u && size == 0) {
9959 /* CNT */
9960 break;
9962 unallocated_encoding(s);
9963 return;
9964 case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
9965 case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
9966 if (size == 3) {
9967 unallocated_encoding(s);
9968 return;
9970 if (!fp_access_check(s)) {
9971 return;
9974 handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
9975 return;
9976 case 0x4: /* CLS, CLZ */
9977 if (size == 3) {
9978 unallocated_encoding(s);
9979 return;
9981 break;
9982 case 0x2: /* SADDLP, UADDLP */
9983 case 0x6: /* SADALP, UADALP */
9984 if (size == 3) {
9985 unallocated_encoding(s);
9986 return;
9988 if (!fp_access_check(s)) {
9989 return;
9991 handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
9992 return;
9993 case 0x13: /* SHLL, SHLL2 */
9994 if (u == 0 || size == 3) {
9995 unallocated_encoding(s);
9996 return;
9998 if (!fp_access_check(s)) {
9999 return;
10001 handle_shll(s, is_q, size, rn, rd);
10002 return;
10003 case 0xa: /* CMLT */
10004 if (u == 1) {
10005 unallocated_encoding(s);
10006 return;
10008 /* fall through */
10009 case 0x8: /* CMGT, CMGE */
10010 case 0x9: /* CMEQ, CMLE */
10011 case 0xb: /* ABS, NEG */
10012 if (size == 3 && !is_q) {
10013 unallocated_encoding(s);
10014 return;
10016 break;
10017 case 0x3: /* SUQADD, USQADD */
10018 if (size == 3 && !is_q) {
10019 unallocated_encoding(s);
10020 return;
10022 if (!fp_access_check(s)) {
10023 return;
10025 handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
10026 return;
10027 case 0x7: /* SQABS, SQNEG */
10028 if (size == 3 && !is_q) {
10029 unallocated_encoding(s);
10030 return;
10032 break;
10033 case 0xc ... 0xf:
10034 case 0x16 ... 0x1d:
10035 case 0x1f:
10037 /* Floating point: U, size[1] and opcode indicate operation;
10038 * size[0] indicates single or double precision.
10040 int is_double = extract32(size, 0, 1);
10041 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10042 size = is_double ? 3 : 2;
10043 switch (opcode) {
10044 case 0x2f: /* FABS */
10045 case 0x6f: /* FNEG */
10046 if (size == 3 && !is_q) {
10047 unallocated_encoding(s);
10048 return;
10050 break;
10051 case 0x1d: /* SCVTF */
10052 case 0x5d: /* UCVTF */
10054 bool is_signed = (opcode == 0x1d) ? true : false;
10055 int elements = is_double ? 2 : is_q ? 4 : 2;
10056 if (is_double && !is_q) {
10057 unallocated_encoding(s);
10058 return;
10060 if (!fp_access_check(s)) {
10061 return;
10063 handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
10064 return;
10066 case 0x2c: /* FCMGT (zero) */
10067 case 0x2d: /* FCMEQ (zero) */
10068 case 0x2e: /* FCMLT (zero) */
10069 case 0x6c: /* FCMGE (zero) */
10070 case 0x6d: /* FCMLE (zero) */
10071 if (size == 3 && !is_q) {
10072 unallocated_encoding(s);
10073 return;
10075 handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
10076 return;
10077 case 0x7f: /* FSQRT */
10078 if (size == 3 && !is_q) {
10079 unallocated_encoding(s);
10080 return;
10082 break;
10083 case 0x1a: /* FCVTNS */
10084 case 0x1b: /* FCVTMS */
10085 case 0x3a: /* FCVTPS */
10086 case 0x3b: /* FCVTZS */
10087 case 0x5a: /* FCVTNU */
10088 case 0x5b: /* FCVTMU */
10089 case 0x7a: /* FCVTPU */
10090 case 0x7b: /* FCVTZU */
10091 need_fpstatus = true;
10092 need_rmode = true;
10093 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10094 if (size == 3 && !is_q) {
10095 unallocated_encoding(s);
10096 return;
10098 break;
10099 case 0x5c: /* FCVTAU */
10100 case 0x1c: /* FCVTAS */
10101 need_fpstatus = true;
10102 need_rmode = true;
10103 rmode = FPROUNDING_TIEAWAY;
10104 if (size == 3 && !is_q) {
10105 unallocated_encoding(s);
10106 return;
10108 break;
10109 case 0x3c: /* URECPE */
10110 if (size == 3) {
10111 unallocated_encoding(s);
10112 return;
10114 /* fall through */
10115 case 0x3d: /* FRECPE */
10116 case 0x7d: /* FRSQRTE */
10117 if (size == 3 && !is_q) {
10118 unallocated_encoding(s);
10119 return;
10121 if (!fp_access_check(s)) {
10122 return;
10124 handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
10125 return;
10126 case 0x56: /* FCVTXN, FCVTXN2 */
10127 if (size == 2) {
10128 unallocated_encoding(s);
10129 return;
10131 /* fall through */
10132 case 0x16: /* FCVTN, FCVTN2 */
10133 /* handle_2misc_narrow does a 2*size -> size operation, but these
10134 * instructions encode the source size rather than dest size.
10136 if (!fp_access_check(s)) {
10137 return;
10139 handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
10140 return;
10141 case 0x17: /* FCVTL, FCVTL2 */
10142 if (!fp_access_check(s)) {
10143 return;
10145 handle_2misc_widening(s, opcode, is_q, size, rn, rd);
10146 return;
10147 case 0x18: /* FRINTN */
10148 case 0x19: /* FRINTM */
10149 case 0x38: /* FRINTP */
10150 case 0x39: /* FRINTZ */
10151 need_rmode = true;
10152 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10153 /* fall through */
10154 case 0x59: /* FRINTX */
10155 case 0x79: /* FRINTI */
10156 need_fpstatus = true;
10157 if (size == 3 && !is_q) {
10158 unallocated_encoding(s);
10159 return;
10161 break;
10162 case 0x58: /* FRINTA */
10163 need_rmode = true;
10164 rmode = FPROUNDING_TIEAWAY;
10165 need_fpstatus = true;
10166 if (size == 3 && !is_q) {
10167 unallocated_encoding(s);
10168 return;
10170 break;
10171 case 0x7c: /* URSQRTE */
10172 if (size == 3) {
10173 unallocated_encoding(s);
10174 return;
10176 need_fpstatus = true;
10177 break;
10178 default:
10179 unallocated_encoding(s);
10180 return;
10182 break;
10184 default:
10185 unallocated_encoding(s);
10186 return;
10189 if (!fp_access_check(s)) {
10190 return;
10193 if (need_fpstatus) {
10194 tcg_fpstatus = get_fpstatus_ptr();
10195 } else {
10196 TCGV_UNUSED_PTR(tcg_fpstatus);
10198 if (need_rmode) {
10199 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
10200 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
10201 } else {
10202 TCGV_UNUSED_I32(tcg_rmode);
10205 if (size == 3) {
10206 /* All 64-bit element operations can be shared with scalar 2misc */
10207 int pass;
10209 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
10210 TCGv_i64 tcg_op = tcg_temp_new_i64();
10211 TCGv_i64 tcg_res = tcg_temp_new_i64();
10213 read_vec_element(s, tcg_op, rn, pass, MO_64);
10215 handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
10216 tcg_rmode, tcg_fpstatus);
10218 write_vec_element(s, tcg_res, rd, pass, MO_64);
10220 tcg_temp_free_i64(tcg_res);
10221 tcg_temp_free_i64(tcg_op);
10223 } else {
10224 int pass;
10226 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
10227 TCGv_i32 tcg_op = tcg_temp_new_i32();
10228 TCGv_i32 tcg_res = tcg_temp_new_i32();
10229 TCGCond cond;
10231 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
10233 if (size == 2) {
10234 /* Special cases for 32 bit elements */
10235 switch (opcode) {
10236 case 0xa: /* CMLT */
10237 /* 32 bit integer comparison against zero, result is
10238 * test ? (2^32 - 1) : 0. We implement via setcond(test)
10239 * and inverting.
10241 cond = TCG_COND_LT;
10242 do_cmop:
10243 tcg_gen_setcondi_i32(cond, tcg_res, tcg_op, 0);
10244 tcg_gen_neg_i32(tcg_res, tcg_res);
10245 break;
10246 case 0x8: /* CMGT, CMGE */
10247 cond = u ? TCG_COND_GE : TCG_COND_GT;
10248 goto do_cmop;
10249 case 0x9: /* CMEQ, CMLE */
10250 cond = u ? TCG_COND_LE : TCG_COND_EQ;
10251 goto do_cmop;
10252 case 0x4: /* CLS */
10253 if (u) {
10254 tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
10255 } else {
10256 tcg_gen_clrsb_i32(tcg_res, tcg_op);
10258 break;
10259 case 0x7: /* SQABS, SQNEG */
10260 if (u) {
10261 gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
10262 } else {
10263 gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
10265 break;
10266 case 0xb: /* ABS, NEG */
10267 if (u) {
10268 tcg_gen_neg_i32(tcg_res, tcg_op);
10269 } else {
10270 TCGv_i32 tcg_zero = tcg_const_i32(0);
10271 tcg_gen_neg_i32(tcg_res, tcg_op);
10272 tcg_gen_movcond_i32(TCG_COND_GT, tcg_res, tcg_op,
10273 tcg_zero, tcg_op, tcg_res);
10274 tcg_temp_free_i32(tcg_zero);
10276 break;
10277 case 0x2f: /* FABS */
10278 gen_helper_vfp_abss(tcg_res, tcg_op);
10279 break;
10280 case 0x6f: /* FNEG */
10281 gen_helper_vfp_negs(tcg_res, tcg_op);
10282 break;
10283 case 0x7f: /* FSQRT */
10284 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
10285 break;
10286 case 0x1a: /* FCVTNS */
10287 case 0x1b: /* FCVTMS */
10288 case 0x1c: /* FCVTAS */
10289 case 0x3a: /* FCVTPS */
10290 case 0x3b: /* FCVTZS */
10292 TCGv_i32 tcg_shift = tcg_const_i32(0);
10293 gen_helper_vfp_tosls(tcg_res, tcg_op,
10294 tcg_shift, tcg_fpstatus);
10295 tcg_temp_free_i32(tcg_shift);
10296 break;
10298 case 0x5a: /* FCVTNU */
10299 case 0x5b: /* FCVTMU */
10300 case 0x5c: /* FCVTAU */
10301 case 0x7a: /* FCVTPU */
10302 case 0x7b: /* FCVTZU */
10304 TCGv_i32 tcg_shift = tcg_const_i32(0);
10305 gen_helper_vfp_touls(tcg_res, tcg_op,
10306 tcg_shift, tcg_fpstatus);
10307 tcg_temp_free_i32(tcg_shift);
10308 break;
10310 case 0x18: /* FRINTN */
10311 case 0x19: /* FRINTM */
10312 case 0x38: /* FRINTP */
10313 case 0x39: /* FRINTZ */
10314 case 0x58: /* FRINTA */
10315 case 0x79: /* FRINTI */
10316 gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
10317 break;
10318 case 0x59: /* FRINTX */
10319 gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
10320 break;
10321 case 0x7c: /* URSQRTE */
10322 gen_helper_rsqrte_u32(tcg_res, tcg_op, tcg_fpstatus);
10323 break;
10324 default:
10325 g_assert_not_reached();
10327 } else {
10328 /* Use helpers for 8 and 16 bit elements */
10329 switch (opcode) {
10330 case 0x5: /* CNT, RBIT */
10331 /* For these two insns size is part of the opcode specifier
10332 * (handled earlier); they always operate on byte elements.
10334 if (u) {
10335 gen_helper_neon_rbit_u8(tcg_res, tcg_op);
10336 } else {
10337 gen_helper_neon_cnt_u8(tcg_res, tcg_op);
10339 break;
10340 case 0x7: /* SQABS, SQNEG */
10342 NeonGenOneOpEnvFn *genfn;
10343 static NeonGenOneOpEnvFn * const fns[2][2] = {
10344 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10345 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10347 genfn = fns[size][u];
10348 genfn(tcg_res, cpu_env, tcg_op);
10349 break;
10351 case 0x8: /* CMGT, CMGE */
10352 case 0x9: /* CMEQ, CMLE */
10353 case 0xa: /* CMLT */
10355 static NeonGenTwoOpFn * const fns[3][2] = {
10356 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_s16 },
10357 { gen_helper_neon_cge_s8, gen_helper_neon_cge_s16 },
10358 { gen_helper_neon_ceq_u8, gen_helper_neon_ceq_u16 },
10360 NeonGenTwoOpFn *genfn;
10361 int comp;
10362 bool reverse;
10363 TCGv_i32 tcg_zero = tcg_const_i32(0);
10365 /* comp = index into [CMGT, CMGE, CMEQ, CMLE, CMLT] */
10366 comp = (opcode - 0x8) * 2 + u;
10367 /* ...but LE, LT are implemented as reverse GE, GT */
10368 reverse = (comp > 2);
10369 if (reverse) {
10370 comp = 4 - comp;
10372 genfn = fns[comp][size];
10373 if (reverse) {
10374 genfn(tcg_res, tcg_zero, tcg_op);
10375 } else {
10376 genfn(tcg_res, tcg_op, tcg_zero);
10378 tcg_temp_free_i32(tcg_zero);
10379 break;
10381 case 0xb: /* ABS, NEG */
10382 if (u) {
10383 TCGv_i32 tcg_zero = tcg_const_i32(0);
10384 if (size) {
10385 gen_helper_neon_sub_u16(tcg_res, tcg_zero, tcg_op);
10386 } else {
10387 gen_helper_neon_sub_u8(tcg_res, tcg_zero, tcg_op);
10389 tcg_temp_free_i32(tcg_zero);
10390 } else {
10391 if (size) {
10392 gen_helper_neon_abs_s16(tcg_res, tcg_op);
10393 } else {
10394 gen_helper_neon_abs_s8(tcg_res, tcg_op);
10397 break;
10398 case 0x4: /* CLS, CLZ */
10399 if (u) {
10400 if (size == 0) {
10401 gen_helper_neon_clz_u8(tcg_res, tcg_op);
10402 } else {
10403 gen_helper_neon_clz_u16(tcg_res, tcg_op);
10405 } else {
10406 if (size == 0) {
10407 gen_helper_neon_cls_s8(tcg_res, tcg_op);
10408 } else {
10409 gen_helper_neon_cls_s16(tcg_res, tcg_op);
10412 break;
10413 default:
10414 g_assert_not_reached();
10418 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10420 tcg_temp_free_i32(tcg_res);
10421 tcg_temp_free_i32(tcg_op);
10424 if (!is_q) {
10425 clear_vec_high(s, rd);
10428 if (need_rmode) {
10429 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
10430 tcg_temp_free_i32(tcg_rmode);
10432 if (need_fpstatus) {
10433 tcg_temp_free_ptr(tcg_fpstatus);
10437 /* C3.6.13 AdvSIMD scalar x indexed element
10438 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
10439 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
10440 * | 0 1 | U | 1 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
10441 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
10442 * C3.6.18 AdvSIMD vector x indexed element
10443 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
10444 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
10445 * | 0 | Q | U | 0 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
10446 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
10448 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
10450 /* This encoding has two kinds of instruction:
10451 * normal, where we perform elt x idxelt => elt for each
10452 * element in the vector
10453 * long, where we perform elt x idxelt and generate a result of
10454 * double the width of the input element
10455 * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
10457 bool is_scalar = extract32(insn, 28, 1);
10458 bool is_q = extract32(insn, 30, 1);
10459 bool u = extract32(insn, 29, 1);
10460 int size = extract32(insn, 22, 2);
10461 int l = extract32(insn, 21, 1);
10462 int m = extract32(insn, 20, 1);
10463 /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
10464 int rm = extract32(insn, 16, 4);
10465 int opcode = extract32(insn, 12, 4);
10466 int h = extract32(insn, 11, 1);
10467 int rn = extract32(insn, 5, 5);
10468 int rd = extract32(insn, 0, 5);
10469 bool is_long = false;
10470 bool is_fp = false;
10471 int index;
10472 TCGv_ptr fpst;
10474 switch (opcode) {
10475 case 0x0: /* MLA */
10476 case 0x4: /* MLS */
10477 if (!u || is_scalar) {
10478 unallocated_encoding(s);
10479 return;
10481 break;
10482 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10483 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10484 case 0xa: /* SMULL, SMULL2, UMULL, UMULL2 */
10485 if (is_scalar) {
10486 unallocated_encoding(s);
10487 return;
10489 is_long = true;
10490 break;
10491 case 0x3: /* SQDMLAL, SQDMLAL2 */
10492 case 0x7: /* SQDMLSL, SQDMLSL2 */
10493 case 0xb: /* SQDMULL, SQDMULL2 */
10494 is_long = true;
10495 /* fall through */
10496 case 0xc: /* SQDMULH */
10497 case 0xd: /* SQRDMULH */
10498 if (u) {
10499 unallocated_encoding(s);
10500 return;
10502 break;
10503 case 0x8: /* MUL */
10504 if (u || is_scalar) {
10505 unallocated_encoding(s);
10506 return;
10508 break;
10509 case 0x1: /* FMLA */
10510 case 0x5: /* FMLS */
10511 if (u) {
10512 unallocated_encoding(s);
10513 return;
10515 /* fall through */
10516 case 0x9: /* FMUL, FMULX */
10517 if (!extract32(size, 1, 1)) {
10518 unallocated_encoding(s);
10519 return;
10521 is_fp = true;
10522 break;
10523 default:
10524 unallocated_encoding(s);
10525 return;
10528 if (is_fp) {
10529 /* low bit of size indicates single/double */
10530 size = extract32(size, 0, 1) ? 3 : 2;
10531 if (size == 2) {
10532 index = h << 1 | l;
10533 } else {
10534 if (l || !is_q) {
10535 unallocated_encoding(s);
10536 return;
10538 index = h;
10540 rm |= (m << 4);
10541 } else {
10542 switch (size) {
10543 case 1:
10544 index = h << 2 | l << 1 | m;
10545 break;
10546 case 2:
10547 index = h << 1 | l;
10548 rm |= (m << 4);
10549 break;
10550 default:
10551 unallocated_encoding(s);
10552 return;
10556 if (!fp_access_check(s)) {
10557 return;
10560 if (is_fp) {
10561 fpst = get_fpstatus_ptr();
10562 } else {
10563 TCGV_UNUSED_PTR(fpst);
10566 if (size == 3) {
10567 TCGv_i64 tcg_idx = tcg_temp_new_i64();
10568 int pass;
10570 assert(is_fp && is_q && !is_long);
10572 read_vec_element(s, tcg_idx, rm, index, MO_64);
10574 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10575 TCGv_i64 tcg_op = tcg_temp_new_i64();
10576 TCGv_i64 tcg_res = tcg_temp_new_i64();
10578 read_vec_element(s, tcg_op, rn, pass, MO_64);
10580 switch (opcode) {
10581 case 0x5: /* FMLS */
10582 /* As usual for ARM, separate negation for fused multiply-add */
10583 gen_helper_vfp_negd(tcg_op, tcg_op);
10584 /* fall through */
10585 case 0x1: /* FMLA */
10586 read_vec_element(s, tcg_res, rd, pass, MO_64);
10587 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
10588 break;
10589 case 0x9: /* FMUL, FMULX */
10590 if (u) {
10591 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
10592 } else {
10593 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
10595 break;
10596 default:
10597 g_assert_not_reached();
10600 write_vec_element(s, tcg_res, rd, pass, MO_64);
10601 tcg_temp_free_i64(tcg_op);
10602 tcg_temp_free_i64(tcg_res);
10605 if (is_scalar) {
10606 clear_vec_high(s, rd);
10609 tcg_temp_free_i64(tcg_idx);
10610 } else if (!is_long) {
10611 /* 32 bit floating point, or 16 or 32 bit integer.
10612 * For the 16 bit scalar case we use the usual Neon helpers and
10613 * rely on the fact that 0 op 0 == 0 with no side effects.
10615 TCGv_i32 tcg_idx = tcg_temp_new_i32();
10616 int pass, maxpasses;
10618 if (is_scalar) {
10619 maxpasses = 1;
10620 } else {
10621 maxpasses = is_q ? 4 : 2;
10624 read_vec_element_i32(s, tcg_idx, rm, index, size);
10626 if (size == 1 && !is_scalar) {
10627 /* The simplest way to handle the 16x16 indexed ops is to duplicate
10628 * the index into both halves of the 32 bit tcg_idx and then use
10629 * the usual Neon helpers.
10631 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
10634 for (pass = 0; pass < maxpasses; pass++) {
10635 TCGv_i32 tcg_op = tcg_temp_new_i32();
10636 TCGv_i32 tcg_res = tcg_temp_new_i32();
10638 read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
10640 switch (opcode) {
10641 case 0x0: /* MLA */
10642 case 0x4: /* MLS */
10643 case 0x8: /* MUL */
10645 static NeonGenTwoOpFn * const fns[2][2] = {
10646 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
10647 { tcg_gen_add_i32, tcg_gen_sub_i32 },
10649 NeonGenTwoOpFn *genfn;
10650 bool is_sub = opcode == 0x4;
10652 if (size == 1) {
10653 gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
10654 } else {
10655 tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
10657 if (opcode == 0x8) {
10658 break;
10660 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
10661 genfn = fns[size - 1][is_sub];
10662 genfn(tcg_res, tcg_op, tcg_res);
10663 break;
10665 case 0x5: /* FMLS */
10666 /* As usual for ARM, separate negation for fused multiply-add */
10667 gen_helper_vfp_negs(tcg_op, tcg_op);
10668 /* fall through */
10669 case 0x1: /* FMLA */
10670 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10671 gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
10672 break;
10673 case 0x9: /* FMUL, FMULX */
10674 if (u) {
10675 gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
10676 } else {
10677 gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
10679 break;
10680 case 0xc: /* SQDMULH */
10681 if (size == 1) {
10682 gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
10683 tcg_op, tcg_idx);
10684 } else {
10685 gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
10686 tcg_op, tcg_idx);
10688 break;
10689 case 0xd: /* SQRDMULH */
10690 if (size == 1) {
10691 gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
10692 tcg_op, tcg_idx);
10693 } else {
10694 gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
10695 tcg_op, tcg_idx);
10697 break;
10698 default:
10699 g_assert_not_reached();
10702 if (is_scalar) {
10703 write_fp_sreg(s, rd, tcg_res);
10704 } else {
10705 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10708 tcg_temp_free_i32(tcg_op);
10709 tcg_temp_free_i32(tcg_res);
10712 tcg_temp_free_i32(tcg_idx);
10714 if (!is_q) {
10715 clear_vec_high(s, rd);
10717 } else {
10718 /* long ops: 16x16->32 or 32x32->64 */
10719 TCGv_i64 tcg_res[2];
10720 int pass;
10721 bool satop = extract32(opcode, 0, 1);
10722 TCGMemOp memop = MO_32;
10724 if (satop || !u) {
10725 memop |= MO_SIGN;
10728 if (size == 2) {
10729 TCGv_i64 tcg_idx = tcg_temp_new_i64();
10731 read_vec_element(s, tcg_idx, rm, index, memop);
10733 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10734 TCGv_i64 tcg_op = tcg_temp_new_i64();
10735 TCGv_i64 tcg_passres;
10736 int passelt;
10738 if (is_scalar) {
10739 passelt = 0;
10740 } else {
10741 passelt = pass + (is_q * 2);
10744 read_vec_element(s, tcg_op, rn, passelt, memop);
10746 tcg_res[pass] = tcg_temp_new_i64();
10748 if (opcode == 0xa || opcode == 0xb) {
10749 /* Non-accumulating ops */
10750 tcg_passres = tcg_res[pass];
10751 } else {
10752 tcg_passres = tcg_temp_new_i64();
10755 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
10756 tcg_temp_free_i64(tcg_op);
10758 if (satop) {
10759 /* saturating, doubling */
10760 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10761 tcg_passres, tcg_passres);
10764 if (opcode == 0xa || opcode == 0xb) {
10765 continue;
10768 /* Accumulating op: handle accumulate step */
10769 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10771 switch (opcode) {
10772 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10773 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10774 break;
10775 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10776 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10777 break;
10778 case 0x7: /* SQDMLSL, SQDMLSL2 */
10779 tcg_gen_neg_i64(tcg_passres, tcg_passres);
10780 /* fall through */
10781 case 0x3: /* SQDMLAL, SQDMLAL2 */
10782 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10783 tcg_res[pass],
10784 tcg_passres);
10785 break;
10786 default:
10787 g_assert_not_reached();
10789 tcg_temp_free_i64(tcg_passres);
10791 tcg_temp_free_i64(tcg_idx);
10793 if (is_scalar) {
10794 clear_vec_high(s, rd);
10796 } else {
10797 TCGv_i32 tcg_idx = tcg_temp_new_i32();
10799 assert(size == 1);
10800 read_vec_element_i32(s, tcg_idx, rm, index, size);
10802 if (!is_scalar) {
10803 /* The simplest way to handle the 16x16 indexed ops is to
10804 * duplicate the index into both halves of the 32 bit tcg_idx
10805 * and then use the usual Neon helpers.
10807 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
10810 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10811 TCGv_i32 tcg_op = tcg_temp_new_i32();
10812 TCGv_i64 tcg_passres;
10814 if (is_scalar) {
10815 read_vec_element_i32(s, tcg_op, rn, pass, size);
10816 } else {
10817 read_vec_element_i32(s, tcg_op, rn,
10818 pass + (is_q * 2), MO_32);
10821 tcg_res[pass] = tcg_temp_new_i64();
10823 if (opcode == 0xa || opcode == 0xb) {
10824 /* Non-accumulating ops */
10825 tcg_passres = tcg_res[pass];
10826 } else {
10827 tcg_passres = tcg_temp_new_i64();
10830 if (memop & MO_SIGN) {
10831 gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
10832 } else {
10833 gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
10835 if (satop) {
10836 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10837 tcg_passres, tcg_passres);
10839 tcg_temp_free_i32(tcg_op);
10841 if (opcode == 0xa || opcode == 0xb) {
10842 continue;
10845 /* Accumulating op: handle accumulate step */
10846 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10848 switch (opcode) {
10849 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10850 gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
10851 tcg_passres);
10852 break;
10853 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10854 gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
10855 tcg_passres);
10856 break;
10857 case 0x7: /* SQDMLSL, SQDMLSL2 */
10858 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10859 /* fall through */
10860 case 0x3: /* SQDMLAL, SQDMLAL2 */
10861 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10862 tcg_res[pass],
10863 tcg_passres);
10864 break;
10865 default:
10866 g_assert_not_reached();
10868 tcg_temp_free_i64(tcg_passres);
10870 tcg_temp_free_i32(tcg_idx);
10872 if (is_scalar) {
10873 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
10877 if (is_scalar) {
10878 tcg_res[1] = tcg_const_i64(0);
10881 for (pass = 0; pass < 2; pass++) {
10882 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10883 tcg_temp_free_i64(tcg_res[pass]);
10887 if (!TCGV_IS_UNUSED_PTR(fpst)) {
10888 tcg_temp_free_ptr(fpst);
10892 /* C3.6.19 Crypto AES
10893 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
10894 * +-----------------+------+-----------+--------+-----+------+------+
10895 * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
10896 * +-----------------+------+-----------+--------+-----+------+------+
10898 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
10900 int size = extract32(insn, 22, 2);
10901 int opcode = extract32(insn, 12, 5);
10902 int rn = extract32(insn, 5, 5);
10903 int rd = extract32(insn, 0, 5);
10904 int decrypt;
10905 TCGv_i32 tcg_rd_regno, tcg_rn_regno, tcg_decrypt;
10906 CryptoThreeOpEnvFn *genfn;
10908 if (!arm_dc_feature(s, ARM_FEATURE_V8_AES)
10909 || size != 0) {
10910 unallocated_encoding(s);
10911 return;
10914 switch (opcode) {
10915 case 0x4: /* AESE */
10916 decrypt = 0;
10917 genfn = gen_helper_crypto_aese;
10918 break;
10919 case 0x6: /* AESMC */
10920 decrypt = 0;
10921 genfn = gen_helper_crypto_aesmc;
10922 break;
10923 case 0x5: /* AESD */
10924 decrypt = 1;
10925 genfn = gen_helper_crypto_aese;
10926 break;
10927 case 0x7: /* AESIMC */
10928 decrypt = 1;
10929 genfn = gen_helper_crypto_aesmc;
10930 break;
10931 default:
10932 unallocated_encoding(s);
10933 return;
10936 if (!fp_access_check(s)) {
10937 return;
10940 /* Note that we convert the Vx register indexes into the
10941 * index within the vfp.regs[] array, so we can share the
10942 * helper with the AArch32 instructions.
10944 tcg_rd_regno = tcg_const_i32(rd << 1);
10945 tcg_rn_regno = tcg_const_i32(rn << 1);
10946 tcg_decrypt = tcg_const_i32(decrypt);
10948 genfn(cpu_env, tcg_rd_regno, tcg_rn_regno, tcg_decrypt);
10950 tcg_temp_free_i32(tcg_rd_regno);
10951 tcg_temp_free_i32(tcg_rn_regno);
10952 tcg_temp_free_i32(tcg_decrypt);
10955 /* C3.6.20 Crypto three-reg SHA
10956 * 31 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
10957 * +-----------------+------+---+------+---+--------+-----+------+------+
10958 * | 0 1 0 1 1 1 1 0 | size | 0 | Rm | 0 | opcode | 0 0 | Rn | Rd |
10959 * +-----------------+------+---+------+---+--------+-----+------+------+
10961 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
10963 int size = extract32(insn, 22, 2);
10964 int opcode = extract32(insn, 12, 3);
10965 int rm = extract32(insn, 16, 5);
10966 int rn = extract32(insn, 5, 5);
10967 int rd = extract32(insn, 0, 5);
10968 CryptoThreeOpEnvFn *genfn;
10969 TCGv_i32 tcg_rd_regno, tcg_rn_regno, tcg_rm_regno;
10970 int feature = ARM_FEATURE_V8_SHA256;
10972 if (size != 0) {
10973 unallocated_encoding(s);
10974 return;
10977 switch (opcode) {
10978 case 0: /* SHA1C */
10979 case 1: /* SHA1P */
10980 case 2: /* SHA1M */
10981 case 3: /* SHA1SU0 */
10982 genfn = NULL;
10983 feature = ARM_FEATURE_V8_SHA1;
10984 break;
10985 case 4: /* SHA256H */
10986 genfn = gen_helper_crypto_sha256h;
10987 break;
10988 case 5: /* SHA256H2 */
10989 genfn = gen_helper_crypto_sha256h2;
10990 break;
10991 case 6: /* SHA256SU1 */
10992 genfn = gen_helper_crypto_sha256su1;
10993 break;
10994 default:
10995 unallocated_encoding(s);
10996 return;
10999 if (!arm_dc_feature(s, feature)) {
11000 unallocated_encoding(s);
11001 return;
11004 if (!fp_access_check(s)) {
11005 return;
11008 tcg_rd_regno = tcg_const_i32(rd << 1);
11009 tcg_rn_regno = tcg_const_i32(rn << 1);
11010 tcg_rm_regno = tcg_const_i32(rm << 1);
11012 if (genfn) {
11013 genfn(cpu_env, tcg_rd_regno, tcg_rn_regno, tcg_rm_regno);
11014 } else {
11015 TCGv_i32 tcg_opcode = tcg_const_i32(opcode);
11017 gen_helper_crypto_sha1_3reg(cpu_env, tcg_rd_regno,
11018 tcg_rn_regno, tcg_rm_regno, tcg_opcode);
11019 tcg_temp_free_i32(tcg_opcode);
11022 tcg_temp_free_i32(tcg_rd_regno);
11023 tcg_temp_free_i32(tcg_rn_regno);
11024 tcg_temp_free_i32(tcg_rm_regno);
11027 /* C3.6.21 Crypto two-reg SHA
11028 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
11029 * +-----------------+------+-----------+--------+-----+------+------+
11030 * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
11031 * +-----------------+------+-----------+--------+-----+------+------+
11033 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
11035 int size = extract32(insn, 22, 2);
11036 int opcode = extract32(insn, 12, 5);
11037 int rn = extract32(insn, 5, 5);
11038 int rd = extract32(insn, 0, 5);
11039 CryptoTwoOpEnvFn *genfn;
11040 int feature;
11041 TCGv_i32 tcg_rd_regno, tcg_rn_regno;
11043 if (size != 0) {
11044 unallocated_encoding(s);
11045 return;
11048 switch (opcode) {
11049 case 0: /* SHA1H */
11050 feature = ARM_FEATURE_V8_SHA1;
11051 genfn = gen_helper_crypto_sha1h;
11052 break;
11053 case 1: /* SHA1SU1 */
11054 feature = ARM_FEATURE_V8_SHA1;
11055 genfn = gen_helper_crypto_sha1su1;
11056 break;
11057 case 2: /* SHA256SU0 */
11058 feature = ARM_FEATURE_V8_SHA256;
11059 genfn = gen_helper_crypto_sha256su0;
11060 break;
11061 default:
11062 unallocated_encoding(s);
11063 return;
11066 if (!arm_dc_feature(s, feature)) {
11067 unallocated_encoding(s);
11068 return;
11071 if (!fp_access_check(s)) {
11072 return;
11075 tcg_rd_regno = tcg_const_i32(rd << 1);
11076 tcg_rn_regno = tcg_const_i32(rn << 1);
11078 genfn(cpu_env, tcg_rd_regno, tcg_rn_regno);
11080 tcg_temp_free_i32(tcg_rd_regno);
11081 tcg_temp_free_i32(tcg_rn_regno);
11084 /* C3.6 Data processing - SIMD, inc Crypto
11086 * As the decode gets a little complex we are using a table based
11087 * approach for this part of the decode.
11089 static const AArch64DecodeTable data_proc_simd[] = {
11090 /* pattern , mask , fn */
11091 { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
11092 { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
11093 { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
11094 { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
11095 { 0x0e000400, 0x9fe08400, disas_simd_copy },
11096 { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
11097 /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
11098 { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
11099 { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
11100 { 0x0e000000, 0xbf208c00, disas_simd_tb },
11101 { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
11102 { 0x2e000000, 0xbf208400, disas_simd_ext },
11103 { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
11104 { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
11105 { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
11106 { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
11107 { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
11108 { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
11109 { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
11110 { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
11111 { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
11112 { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
11113 { 0x00000000, 0x00000000, NULL }
11116 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
11118 /* Note that this is called with all non-FP cases from
11119 * table C3-6 so it must UNDEF for entries not specifically
11120 * allocated to instructions in that table.
11122 AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
11123 if (fn) {
11124 fn(s, insn);
11125 } else {
11126 unallocated_encoding(s);
11130 /* C3.6 Data processing - SIMD and floating point */
11131 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
11133 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
11134 disas_data_proc_fp(s, insn);
11135 } else {
11136 /* SIMD, including crypto */
11137 disas_data_proc_simd(s, insn);
11141 /* C3.1 A64 instruction index by encoding */
11142 static void disas_a64_insn(CPUARMState *env, DisasContext *s)
11144 uint32_t insn;
11146 insn = arm_ldl_code(env, s->pc, s->sctlr_b);
11147 s->insn = insn;
11148 s->pc += 4;
11150 s->fp_access_checked = false;
11152 switch (extract32(insn, 25, 4)) {
11153 case 0x0: case 0x1: case 0x2: case 0x3: /* UNALLOCATED */
11154 unallocated_encoding(s);
11155 break;
11156 case 0x8: case 0x9: /* Data processing - immediate */
11157 disas_data_proc_imm(s, insn);
11158 break;
11159 case 0xa: case 0xb: /* Branch, exception generation and system insns */
11160 disas_b_exc_sys(s, insn);
11161 break;
11162 case 0x4:
11163 case 0x6:
11164 case 0xc:
11165 case 0xe: /* Loads and stores */
11166 disas_ldst(s, insn);
11167 break;
11168 case 0x5:
11169 case 0xd: /* Data processing - register */
11170 disas_data_proc_reg(s, insn);
11171 break;
11172 case 0x7:
11173 case 0xf: /* Data processing - SIMD and floating point */
11174 disas_data_proc_simd_fp(s, insn);
11175 break;
11176 default:
11177 assert(FALSE); /* all 15 cases should be handled above */
11178 break;
11181 /* if we allocated any temporaries, free them here */
11182 free_tmp_a64(s);
11185 void gen_intermediate_code_a64(ARMCPU *cpu, TranslationBlock *tb)
11187 CPUState *cs = CPU(cpu);
11188 CPUARMState *env = &cpu->env;
11189 DisasContext dc1, *dc = &dc1;
11190 target_ulong pc_start;
11191 target_ulong next_page_start;
11192 int num_insns;
11193 int max_insns;
11195 pc_start = tb->pc;
11197 dc->tb = tb;
11199 dc->is_jmp = DISAS_NEXT;
11200 dc->pc = pc_start;
11201 dc->singlestep_enabled = cs->singlestep_enabled;
11202 dc->condjmp = 0;
11204 dc->aarch64 = 1;
11205 /* If we are coming from secure EL0 in a system with a 32-bit EL3, then
11206 * there is no secure EL1, so we route exceptions to EL3.
11208 dc->secure_routed_to_el3 = arm_feature(env, ARM_FEATURE_EL3) &&
11209 !arm_el_is_aa64(env, 3);
11210 dc->thumb = 0;
11211 dc->sctlr_b = 0;
11212 dc->be_data = ARM_TBFLAG_BE_DATA(tb->flags) ? MO_BE : MO_LE;
11213 dc->condexec_mask = 0;
11214 dc->condexec_cond = 0;
11215 dc->mmu_idx = ARM_TBFLAG_MMUIDX(tb->flags);
11216 dc->tbi0 = ARM_TBFLAG_TBI0(tb->flags);
11217 dc->tbi1 = ARM_TBFLAG_TBI1(tb->flags);
11218 dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
11219 #if !defined(CONFIG_USER_ONLY)
11220 dc->user = (dc->current_el == 0);
11221 #endif
11222 dc->fp_excp_el = ARM_TBFLAG_FPEXC_EL(tb->flags);
11223 dc->vec_len = 0;
11224 dc->vec_stride = 0;
11225 dc->cp_regs = cpu->cp_regs;
11226 dc->features = env->features;
11228 /* Single step state. The code-generation logic here is:
11229 * SS_ACTIVE == 0:
11230 * generate code with no special handling for single-stepping (except
11231 * that anything that can make us go to SS_ACTIVE == 1 must end the TB;
11232 * this happens anyway because those changes are all system register or
11233 * PSTATE writes).
11234 * SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
11235 * emit code for one insn
11236 * emit code to clear PSTATE.SS
11237 * emit code to generate software step exception for completed step
11238 * end TB (as usual for having generated an exception)
11239 * SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
11240 * emit code to generate a software step exception
11241 * end the TB
11243 dc->ss_active = ARM_TBFLAG_SS_ACTIVE(tb->flags);
11244 dc->pstate_ss = ARM_TBFLAG_PSTATE_SS(tb->flags);
11245 dc->is_ldex = false;
11246 dc->ss_same_el = (arm_debug_target_el(env) == dc->current_el);
11248 init_tmp_a64_array(dc);
11250 next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
11251 num_insns = 0;
11252 max_insns = tb->cflags & CF_COUNT_MASK;
11253 if (max_insns == 0) {
11254 max_insns = CF_COUNT_MASK;
11256 if (max_insns > TCG_MAX_INSNS) {
11257 max_insns = TCG_MAX_INSNS;
11260 gen_tb_start(tb);
11262 tcg_clear_temp_count();
11264 do {
11265 dc->insn_start_idx = tcg_op_buf_count();
11266 tcg_gen_insn_start(dc->pc, 0, 0);
11267 num_insns++;
11269 if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) {
11270 CPUBreakpoint *bp;
11271 QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
11272 if (bp->pc == dc->pc) {
11273 if (bp->flags & BP_CPU) {
11274 gen_a64_set_pc_im(dc->pc);
11275 gen_helper_check_breakpoints(cpu_env);
11276 /* End the TB early; it likely won't be executed */
11277 dc->is_jmp = DISAS_UPDATE;
11278 } else {
11279 gen_exception_internal_insn(dc, 0, EXCP_DEBUG);
11280 /* The address covered by the breakpoint must be
11281 included in [tb->pc, tb->pc + tb->size) in order
11282 to for it to be properly cleared -- thus we
11283 increment the PC here so that the logic setting
11284 tb->size below does the right thing. */
11285 dc->pc += 4;
11286 goto done_generating;
11288 break;
11293 if (num_insns == max_insns && (tb->cflags & CF_LAST_IO)) {
11294 gen_io_start();
11297 if (dc->ss_active && !dc->pstate_ss) {
11298 /* Singlestep state is Active-pending.
11299 * If we're in this state at the start of a TB then either
11300 * a) we just took an exception to an EL which is being debugged
11301 * and this is the first insn in the exception handler
11302 * b) debug exceptions were masked and we just unmasked them
11303 * without changing EL (eg by clearing PSTATE.D)
11304 * In either case we're going to take a swstep exception in the
11305 * "did not step an insn" case, and so the syndrome ISV and EX
11306 * bits should be zero.
11308 assert(num_insns == 1);
11309 gen_exception(EXCP_UDEF, syn_swstep(dc->ss_same_el, 0, 0),
11310 default_exception_el(dc));
11311 dc->is_jmp = DISAS_EXC;
11312 break;
11315 disas_a64_insn(env, dc);
11317 if (tcg_check_temp_count()) {
11318 fprintf(stderr, "TCG temporary leak before "TARGET_FMT_lx"\n",
11319 dc->pc);
11322 /* Translation stops when a conditional branch is encountered.
11323 * Otherwise the subsequent code could get translated several times.
11324 * Also stop translation when a page boundary is reached. This
11325 * ensures prefetch aborts occur at the right place.
11327 } while (!dc->is_jmp && !tcg_op_buf_full() &&
11328 !cs->singlestep_enabled &&
11329 !singlestep &&
11330 !dc->ss_active &&
11331 dc->pc < next_page_start &&
11332 num_insns < max_insns);
11334 if (tb->cflags & CF_LAST_IO) {
11335 gen_io_end();
11338 if (unlikely(cs->singlestep_enabled || dc->ss_active)
11339 && dc->is_jmp != DISAS_EXC) {
11340 /* Note that this means single stepping WFI doesn't halt the CPU.
11341 * For conditional branch insns this is harmless unreachable code as
11342 * gen_goto_tb() has already handled emitting the debug exception
11343 * (and thus a tb-jump is not possible when singlestepping).
11345 assert(dc->is_jmp != DISAS_TB_JUMP);
11346 if (dc->is_jmp != DISAS_JUMP) {
11347 gen_a64_set_pc_im(dc->pc);
11349 if (cs->singlestep_enabled) {
11350 gen_exception_internal(EXCP_DEBUG);
11351 } else {
11352 gen_step_complete_exception(dc);
11354 } else {
11355 switch (dc->is_jmp) {
11356 case DISAS_NEXT:
11357 gen_goto_tb(dc, 1, dc->pc);
11358 break;
11359 default:
11360 case DISAS_UPDATE:
11361 gen_a64_set_pc_im(dc->pc);
11362 /* fall through */
11363 case DISAS_JUMP:
11364 /* indicate that the hash table must be used to find the next TB */
11365 tcg_gen_exit_tb(0);
11366 break;
11367 case DISAS_TB_JUMP:
11368 case DISAS_EXC:
11369 case DISAS_SWI:
11370 break;
11371 case DISAS_WFE:
11372 gen_a64_set_pc_im(dc->pc);
11373 gen_helper_wfe(cpu_env);
11374 break;
11375 case DISAS_YIELD:
11376 gen_a64_set_pc_im(dc->pc);
11377 gen_helper_yield(cpu_env);
11378 break;
11379 case DISAS_WFI:
11380 /* This is a special case because we don't want to just halt the CPU
11381 * if trying to debug across a WFI.
11383 gen_a64_set_pc_im(dc->pc);
11384 gen_helper_wfi(cpu_env);
11385 /* The helper doesn't necessarily throw an exception, but we
11386 * must go back to the main loop to check for interrupts anyway.
11388 tcg_gen_exit_tb(0);
11389 break;
11393 done_generating:
11394 gen_tb_end(tb, num_insns);
11396 #ifdef DEBUG_DISAS
11397 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) &&
11398 qemu_log_in_addr_range(pc_start)) {
11399 qemu_log_lock();
11400 qemu_log("----------------\n");
11401 qemu_log("IN: %s\n", lookup_symbol(pc_start));
11402 log_target_disas(cs, pc_start, dc->pc - pc_start,
11403 4 | (bswap_code(dc->sctlr_b) ? 2 : 0));
11404 qemu_log("\n");
11405 qemu_log_unlock();
11407 #endif
11408 tb->size = dc->pc - pc_start;
11409 tb->icount = num_insns;