Merge remote-tracking branch 'fam/tags/for-upstream' into staging
[qemu/kevin.git] / target-arm / translate-a64.c
blobde48747376b5960e995a29610bb440f7325ee27b
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 disas_set_insn_syndrome(DisasContext *s, uint32_t syn)
384 /* We don't need to save all of the syndrome so we mask and shift
385 * out uneeded bits to help the sleb128 encoder do a better job.
387 syn &= ARM_INSN_START_WORD2_MASK;
388 syn >>= ARM_INSN_START_WORD2_SHIFT;
390 /* We check and clear insn_start_idx to catch multiple updates. */
391 assert(s->insn_start_idx != 0);
392 tcg_set_insn_param(s->insn_start_idx, 2, syn);
393 s->insn_start_idx = 0;
396 static void unallocated_encoding(DisasContext *s)
398 /* Unallocated and reserved encodings are uncategorized */
399 gen_exception_insn(s, 4, EXCP_UDEF, syn_uncategorized(),
400 default_exception_el(s));
403 #define unsupported_encoding(s, insn) \
404 do { \
405 qemu_log_mask(LOG_UNIMP, \
406 "%s:%d: unsupported instruction encoding 0x%08x " \
407 "at pc=%016" PRIx64 "\n", \
408 __FILE__, __LINE__, insn, s->pc - 4); \
409 unallocated_encoding(s); \
410 } while (0);
412 static void init_tmp_a64_array(DisasContext *s)
414 #ifdef CONFIG_DEBUG_TCG
415 int i;
416 for (i = 0; i < ARRAY_SIZE(s->tmp_a64); i++) {
417 TCGV_UNUSED_I64(s->tmp_a64[i]);
419 #endif
420 s->tmp_a64_count = 0;
423 static void free_tmp_a64(DisasContext *s)
425 int i;
426 for (i = 0; i < s->tmp_a64_count; i++) {
427 tcg_temp_free_i64(s->tmp_a64[i]);
429 init_tmp_a64_array(s);
432 static TCGv_i64 new_tmp_a64(DisasContext *s)
434 assert(s->tmp_a64_count < TMP_A64_MAX);
435 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
438 static TCGv_i64 new_tmp_a64_zero(DisasContext *s)
440 TCGv_i64 t = new_tmp_a64(s);
441 tcg_gen_movi_i64(t, 0);
442 return t;
446 * Register access functions
448 * These functions are used for directly accessing a register in where
449 * changes to the final register value are likely to be made. If you
450 * need to use a register for temporary calculation (e.g. index type
451 * operations) use the read_* form.
453 * B1.2.1 Register mappings
455 * In instruction register encoding 31 can refer to ZR (zero register) or
456 * the SP (stack pointer) depending on context. In QEMU's case we map SP
457 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
458 * This is the point of the _sp forms.
460 static TCGv_i64 cpu_reg(DisasContext *s, int reg)
462 if (reg == 31) {
463 return new_tmp_a64_zero(s);
464 } else {
465 return cpu_X[reg];
469 /* register access for when 31 == SP */
470 static TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
472 return cpu_X[reg];
475 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
476 * representing the register contents. This TCGv is an auto-freed
477 * temporary so it need not be explicitly freed, and may be modified.
479 static TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
481 TCGv_i64 v = new_tmp_a64(s);
482 if (reg != 31) {
483 if (sf) {
484 tcg_gen_mov_i64(v, cpu_X[reg]);
485 } else {
486 tcg_gen_ext32u_i64(v, cpu_X[reg]);
488 } else {
489 tcg_gen_movi_i64(v, 0);
491 return v;
494 static TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
496 TCGv_i64 v = new_tmp_a64(s);
497 if (sf) {
498 tcg_gen_mov_i64(v, cpu_X[reg]);
499 } else {
500 tcg_gen_ext32u_i64(v, cpu_X[reg]);
502 return v;
505 /* We should have at some point before trying to access an FP register
506 * done the necessary access check, so assert that
507 * (a) we did the check and
508 * (b) we didn't then just plough ahead anyway if it failed.
509 * Print the instruction pattern in the abort message so we can figure
510 * out what we need to fix if a user encounters this problem in the wild.
512 static inline void assert_fp_access_checked(DisasContext *s)
514 #ifdef CONFIG_DEBUG_TCG
515 if (unlikely(!s->fp_access_checked || s->fp_excp_el)) {
516 fprintf(stderr, "target-arm: FP access check missing for "
517 "instruction 0x%08x\n", s->insn);
518 abort();
520 #endif
523 /* Return the offset into CPUARMState of an element of specified
524 * size, 'element' places in from the least significant end of
525 * the FP/vector register Qn.
527 static inline int vec_reg_offset(DisasContext *s, int regno,
528 int element, TCGMemOp size)
530 int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
531 #ifdef HOST_WORDS_BIGENDIAN
532 /* This is complicated slightly because vfp.regs[2n] is
533 * still the low half and vfp.regs[2n+1] the high half
534 * of the 128 bit vector, even on big endian systems.
535 * Calculate the offset assuming a fully bigendian 128 bits,
536 * then XOR to account for the order of the two 64 bit halves.
538 offs += (16 - ((element + 1) * (1 << size)));
539 offs ^= 8;
540 #else
541 offs += element * (1 << size);
542 #endif
543 assert_fp_access_checked(s);
544 return offs;
547 /* Return the offset into CPUARMState of a slice (from
548 * the least significant end) of FP register Qn (ie
549 * Dn, Sn, Hn or Bn).
550 * (Note that this is not the same mapping as for A32; see cpu.h)
552 static inline int fp_reg_offset(DisasContext *s, int regno, TCGMemOp size)
554 int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
555 #ifdef HOST_WORDS_BIGENDIAN
556 offs += (8 - (1 << size));
557 #endif
558 assert_fp_access_checked(s);
559 return offs;
562 /* Offset of the high half of the 128 bit vector Qn */
563 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
565 assert_fp_access_checked(s);
566 return offsetof(CPUARMState, vfp.regs[regno * 2 + 1]);
569 /* Convenience accessors for reading and writing single and double
570 * FP registers. Writing clears the upper parts of the associated
571 * 128 bit vector register, as required by the architecture.
572 * Note that unlike the GP register accessors, the values returned
573 * by the read functions must be manually freed.
575 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
577 TCGv_i64 v = tcg_temp_new_i64();
579 tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
580 return v;
583 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
585 TCGv_i32 v = tcg_temp_new_i32();
587 tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
588 return v;
591 static void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
593 TCGv_i64 tcg_zero = tcg_const_i64(0);
595 tcg_gen_st_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
596 tcg_gen_st_i64(tcg_zero, cpu_env, fp_reg_hi_offset(s, reg));
597 tcg_temp_free_i64(tcg_zero);
600 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
602 TCGv_i64 tmp = tcg_temp_new_i64();
604 tcg_gen_extu_i32_i64(tmp, v);
605 write_fp_dreg(s, reg, tmp);
606 tcg_temp_free_i64(tmp);
609 static TCGv_ptr get_fpstatus_ptr(void)
611 TCGv_ptr statusptr = tcg_temp_new_ptr();
612 int offset;
614 /* In A64 all instructions (both FP and Neon) use the FPCR;
615 * there is no equivalent of the A32 Neon "standard FPSCR value"
616 * and all operations use vfp.fp_status.
618 offset = offsetof(CPUARMState, vfp.fp_status);
619 tcg_gen_addi_ptr(statusptr, cpu_env, offset);
620 return statusptr;
623 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
624 * than the 32 bit equivalent.
626 static inline void gen_set_NZ64(TCGv_i64 result)
628 tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
629 tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
632 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
633 static inline void gen_logic_CC(int sf, TCGv_i64 result)
635 if (sf) {
636 gen_set_NZ64(result);
637 } else {
638 tcg_gen_extrl_i64_i32(cpu_ZF, result);
639 tcg_gen_mov_i32(cpu_NF, cpu_ZF);
641 tcg_gen_movi_i32(cpu_CF, 0);
642 tcg_gen_movi_i32(cpu_VF, 0);
645 /* dest = T0 + T1; compute C, N, V and Z flags */
646 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
648 if (sf) {
649 TCGv_i64 result, flag, tmp;
650 result = tcg_temp_new_i64();
651 flag = tcg_temp_new_i64();
652 tmp = tcg_temp_new_i64();
654 tcg_gen_movi_i64(tmp, 0);
655 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
657 tcg_gen_extrl_i64_i32(cpu_CF, flag);
659 gen_set_NZ64(result);
661 tcg_gen_xor_i64(flag, result, t0);
662 tcg_gen_xor_i64(tmp, t0, t1);
663 tcg_gen_andc_i64(flag, flag, tmp);
664 tcg_temp_free_i64(tmp);
665 tcg_gen_extrh_i64_i32(cpu_VF, flag);
667 tcg_gen_mov_i64(dest, result);
668 tcg_temp_free_i64(result);
669 tcg_temp_free_i64(flag);
670 } else {
671 /* 32 bit arithmetic */
672 TCGv_i32 t0_32 = tcg_temp_new_i32();
673 TCGv_i32 t1_32 = tcg_temp_new_i32();
674 TCGv_i32 tmp = tcg_temp_new_i32();
676 tcg_gen_movi_i32(tmp, 0);
677 tcg_gen_extrl_i64_i32(t0_32, t0);
678 tcg_gen_extrl_i64_i32(t1_32, t1);
679 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
680 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
681 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
682 tcg_gen_xor_i32(tmp, t0_32, t1_32);
683 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
684 tcg_gen_extu_i32_i64(dest, cpu_NF);
686 tcg_temp_free_i32(tmp);
687 tcg_temp_free_i32(t0_32);
688 tcg_temp_free_i32(t1_32);
692 /* dest = T0 - T1; compute C, N, V and Z flags */
693 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
695 if (sf) {
696 /* 64 bit arithmetic */
697 TCGv_i64 result, flag, tmp;
699 result = tcg_temp_new_i64();
700 flag = tcg_temp_new_i64();
701 tcg_gen_sub_i64(result, t0, t1);
703 gen_set_NZ64(result);
705 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
706 tcg_gen_extrl_i64_i32(cpu_CF, flag);
708 tcg_gen_xor_i64(flag, result, t0);
709 tmp = tcg_temp_new_i64();
710 tcg_gen_xor_i64(tmp, t0, t1);
711 tcg_gen_and_i64(flag, flag, tmp);
712 tcg_temp_free_i64(tmp);
713 tcg_gen_extrh_i64_i32(cpu_VF, flag);
714 tcg_gen_mov_i64(dest, result);
715 tcg_temp_free_i64(flag);
716 tcg_temp_free_i64(result);
717 } else {
718 /* 32 bit arithmetic */
719 TCGv_i32 t0_32 = tcg_temp_new_i32();
720 TCGv_i32 t1_32 = tcg_temp_new_i32();
721 TCGv_i32 tmp;
723 tcg_gen_extrl_i64_i32(t0_32, t0);
724 tcg_gen_extrl_i64_i32(t1_32, t1);
725 tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
726 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
727 tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
728 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
729 tmp = tcg_temp_new_i32();
730 tcg_gen_xor_i32(tmp, t0_32, t1_32);
731 tcg_temp_free_i32(t0_32);
732 tcg_temp_free_i32(t1_32);
733 tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
734 tcg_temp_free_i32(tmp);
735 tcg_gen_extu_i32_i64(dest, cpu_NF);
739 /* dest = T0 + T1 + CF; do not compute flags. */
740 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
742 TCGv_i64 flag = tcg_temp_new_i64();
743 tcg_gen_extu_i32_i64(flag, cpu_CF);
744 tcg_gen_add_i64(dest, t0, t1);
745 tcg_gen_add_i64(dest, dest, flag);
746 tcg_temp_free_i64(flag);
748 if (!sf) {
749 tcg_gen_ext32u_i64(dest, dest);
753 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
754 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
756 if (sf) {
757 TCGv_i64 result, cf_64, vf_64, tmp;
758 result = tcg_temp_new_i64();
759 cf_64 = tcg_temp_new_i64();
760 vf_64 = tcg_temp_new_i64();
761 tmp = tcg_const_i64(0);
763 tcg_gen_extu_i32_i64(cf_64, cpu_CF);
764 tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
765 tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
766 tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
767 gen_set_NZ64(result);
769 tcg_gen_xor_i64(vf_64, result, t0);
770 tcg_gen_xor_i64(tmp, t0, t1);
771 tcg_gen_andc_i64(vf_64, vf_64, tmp);
772 tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
774 tcg_gen_mov_i64(dest, result);
776 tcg_temp_free_i64(tmp);
777 tcg_temp_free_i64(vf_64);
778 tcg_temp_free_i64(cf_64);
779 tcg_temp_free_i64(result);
780 } else {
781 TCGv_i32 t0_32, t1_32, tmp;
782 t0_32 = tcg_temp_new_i32();
783 t1_32 = tcg_temp_new_i32();
784 tmp = tcg_const_i32(0);
786 tcg_gen_extrl_i64_i32(t0_32, t0);
787 tcg_gen_extrl_i64_i32(t1_32, t1);
788 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
789 tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
791 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
792 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
793 tcg_gen_xor_i32(tmp, t0_32, t1_32);
794 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
795 tcg_gen_extu_i32_i64(dest, cpu_NF);
797 tcg_temp_free_i32(tmp);
798 tcg_temp_free_i32(t1_32);
799 tcg_temp_free_i32(t0_32);
804 * Load/Store generators
808 * Store from GPR register to memory.
810 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
811 TCGv_i64 tcg_addr, int size, int memidx,
812 bool iss_valid,
813 unsigned int iss_srt,
814 bool iss_sf, bool iss_ar)
816 g_assert(size <= 3);
817 tcg_gen_qemu_st_i64(source, tcg_addr, memidx, s->be_data + size);
819 if (iss_valid) {
820 uint32_t syn;
822 syn = syn_data_abort_with_iss(0,
823 size,
824 false,
825 iss_srt,
826 iss_sf,
827 iss_ar,
828 0, 0, 0, 0, 0, false);
829 disas_set_insn_syndrome(s, syn);
833 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
834 TCGv_i64 tcg_addr, int size,
835 bool iss_valid,
836 unsigned int iss_srt,
837 bool iss_sf, bool iss_ar)
839 do_gpr_st_memidx(s, source, tcg_addr, size, get_mem_index(s),
840 iss_valid, iss_srt, iss_sf, iss_ar);
844 * Load from memory to GPR register
846 static void do_gpr_ld_memidx(DisasContext *s,
847 TCGv_i64 dest, TCGv_i64 tcg_addr,
848 int size, bool is_signed,
849 bool extend, int memidx,
850 bool iss_valid, unsigned int iss_srt,
851 bool iss_sf, bool iss_ar)
853 TCGMemOp memop = s->be_data + size;
855 g_assert(size <= 3);
857 if (is_signed) {
858 memop += MO_SIGN;
861 tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
863 if (extend && is_signed) {
864 g_assert(size < 3);
865 tcg_gen_ext32u_i64(dest, dest);
868 if (iss_valid) {
869 uint32_t syn;
871 syn = syn_data_abort_with_iss(0,
872 size,
873 is_signed,
874 iss_srt,
875 iss_sf,
876 iss_ar,
877 0, 0, 0, 0, 0, false);
878 disas_set_insn_syndrome(s, syn);
882 static void do_gpr_ld(DisasContext *s,
883 TCGv_i64 dest, TCGv_i64 tcg_addr,
884 int size, bool is_signed, bool extend,
885 bool iss_valid, unsigned int iss_srt,
886 bool iss_sf, bool iss_ar)
888 do_gpr_ld_memidx(s, dest, tcg_addr, size, is_signed, extend,
889 get_mem_index(s),
890 iss_valid, iss_srt, iss_sf, iss_ar);
894 * Store from FP register to memory
896 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
898 /* This writes the bottom N bits of a 128 bit wide vector to memory */
899 TCGv_i64 tmp = tcg_temp_new_i64();
900 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_offset(s, srcidx, MO_64));
901 if (size < 4) {
902 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s),
903 s->be_data + size);
904 } else {
905 bool be = s->be_data == MO_BE;
906 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
908 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
909 tcg_gen_qemu_st_i64(tmp, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
910 s->be_data | MO_Q);
911 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_hi_offset(s, srcidx));
912 tcg_gen_qemu_st_i64(tmp, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
913 s->be_data | MO_Q);
914 tcg_temp_free_i64(tcg_hiaddr);
917 tcg_temp_free_i64(tmp);
921 * Load from memory to FP register
923 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
925 /* This always zero-extends and writes to a full 128 bit wide vector */
926 TCGv_i64 tmplo = tcg_temp_new_i64();
927 TCGv_i64 tmphi;
929 if (size < 4) {
930 TCGMemOp memop = s->be_data + size;
931 tmphi = tcg_const_i64(0);
932 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
933 } else {
934 bool be = s->be_data == MO_BE;
935 TCGv_i64 tcg_hiaddr;
937 tmphi = tcg_temp_new_i64();
938 tcg_hiaddr = tcg_temp_new_i64();
940 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
941 tcg_gen_qemu_ld_i64(tmplo, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
942 s->be_data | MO_Q);
943 tcg_gen_qemu_ld_i64(tmphi, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
944 s->be_data | MO_Q);
945 tcg_temp_free_i64(tcg_hiaddr);
948 tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
949 tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
951 tcg_temp_free_i64(tmplo);
952 tcg_temp_free_i64(tmphi);
956 * Vector load/store helpers.
958 * The principal difference between this and a FP load is that we don't
959 * zero extend as we are filling a partial chunk of the vector register.
960 * These functions don't support 128 bit loads/stores, which would be
961 * normal load/store operations.
963 * The _i32 versions are useful when operating on 32 bit quantities
964 * (eg for floating point single or using Neon helper functions).
967 /* Get value of an element within a vector register */
968 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
969 int element, TCGMemOp memop)
971 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
972 switch (memop) {
973 case MO_8:
974 tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
975 break;
976 case MO_16:
977 tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
978 break;
979 case MO_32:
980 tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
981 break;
982 case MO_8|MO_SIGN:
983 tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
984 break;
985 case MO_16|MO_SIGN:
986 tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
987 break;
988 case MO_32|MO_SIGN:
989 tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
990 break;
991 case MO_64:
992 case MO_64|MO_SIGN:
993 tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
994 break;
995 default:
996 g_assert_not_reached();
1000 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1001 int element, TCGMemOp memop)
1003 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1004 switch (memop) {
1005 case MO_8:
1006 tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1007 break;
1008 case MO_16:
1009 tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1010 break;
1011 case MO_8|MO_SIGN:
1012 tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1013 break;
1014 case MO_16|MO_SIGN:
1015 tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1016 break;
1017 case MO_32:
1018 case MO_32|MO_SIGN:
1019 tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1020 break;
1021 default:
1022 g_assert_not_reached();
1026 /* Set value of an element within a vector register */
1027 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1028 int element, TCGMemOp memop)
1030 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1031 switch (memop) {
1032 case MO_8:
1033 tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1034 break;
1035 case MO_16:
1036 tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1037 break;
1038 case MO_32:
1039 tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1040 break;
1041 case MO_64:
1042 tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1043 break;
1044 default:
1045 g_assert_not_reached();
1049 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1050 int destidx, int element, TCGMemOp memop)
1052 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1053 switch (memop) {
1054 case MO_8:
1055 tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1056 break;
1057 case MO_16:
1058 tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1059 break;
1060 case MO_32:
1061 tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1062 break;
1063 default:
1064 g_assert_not_reached();
1068 /* Clear the high 64 bits of a 128 bit vector (in general non-quad
1069 * vector ops all need to do this).
1071 static void clear_vec_high(DisasContext *s, int rd)
1073 TCGv_i64 tcg_zero = tcg_const_i64(0);
1075 write_vec_element(s, tcg_zero, rd, 1, MO_64);
1076 tcg_temp_free_i64(tcg_zero);
1079 /* Store from vector register to memory */
1080 static void do_vec_st(DisasContext *s, int srcidx, 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 read_vec_element(s, tcg_tmp, srcidx, element, size);
1087 tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
1089 tcg_temp_free_i64(tcg_tmp);
1092 /* Load from memory to vector register */
1093 static void do_vec_ld(DisasContext *s, int destidx, int element,
1094 TCGv_i64 tcg_addr, int size)
1096 TCGMemOp memop = s->be_data + size;
1097 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1099 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
1100 write_vec_element(s, tcg_tmp, destidx, element, size);
1102 tcg_temp_free_i64(tcg_tmp);
1105 /* Check that FP/Neon access is enabled. If it is, return
1106 * true. If not, emit code to generate an appropriate exception,
1107 * and return false; the caller should not emit any code for
1108 * the instruction. Note that this check must happen after all
1109 * unallocated-encoding checks (otherwise the syndrome information
1110 * for the resulting exception will be incorrect).
1112 static inline bool fp_access_check(DisasContext *s)
1114 assert(!s->fp_access_checked);
1115 s->fp_access_checked = true;
1117 if (!s->fp_excp_el) {
1118 return true;
1121 gen_exception_insn(s, 4, EXCP_UDEF, syn_fp_access_trap(1, 0xe, false),
1122 s->fp_excp_el);
1123 return false;
1127 * This utility function is for doing register extension with an
1128 * optional shift. You will likely want to pass a temporary for the
1129 * destination register. See DecodeRegExtend() in the ARM ARM.
1131 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1132 int option, unsigned int shift)
1134 int extsize = extract32(option, 0, 2);
1135 bool is_signed = extract32(option, 2, 1);
1137 if (is_signed) {
1138 switch (extsize) {
1139 case 0:
1140 tcg_gen_ext8s_i64(tcg_out, tcg_in);
1141 break;
1142 case 1:
1143 tcg_gen_ext16s_i64(tcg_out, tcg_in);
1144 break;
1145 case 2:
1146 tcg_gen_ext32s_i64(tcg_out, tcg_in);
1147 break;
1148 case 3:
1149 tcg_gen_mov_i64(tcg_out, tcg_in);
1150 break;
1152 } else {
1153 switch (extsize) {
1154 case 0:
1155 tcg_gen_ext8u_i64(tcg_out, tcg_in);
1156 break;
1157 case 1:
1158 tcg_gen_ext16u_i64(tcg_out, tcg_in);
1159 break;
1160 case 2:
1161 tcg_gen_ext32u_i64(tcg_out, tcg_in);
1162 break;
1163 case 3:
1164 tcg_gen_mov_i64(tcg_out, tcg_in);
1165 break;
1169 if (shift) {
1170 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1174 static inline void gen_check_sp_alignment(DisasContext *s)
1176 /* The AArch64 architecture mandates that (if enabled via PSTATE
1177 * or SCTLR bits) there is a check that SP is 16-aligned on every
1178 * SP-relative load or store (with an exception generated if it is not).
1179 * In line with general QEMU practice regarding misaligned accesses,
1180 * we omit these checks for the sake of guest program performance.
1181 * This function is provided as a hook so we can more easily add these
1182 * checks in future (possibly as a "favour catching guest program bugs
1183 * over speed" user selectable option).
1188 * This provides a simple table based table lookup decoder. It is
1189 * intended to be used when the relevant bits for decode are too
1190 * awkwardly placed and switch/if based logic would be confusing and
1191 * deeply nested. Since it's a linear search through the table, tables
1192 * should be kept small.
1194 * It returns the first handler where insn & mask == pattern, or
1195 * NULL if there is no match.
1196 * The table is terminated by an empty mask (i.e. 0)
1198 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1199 uint32_t insn)
1201 const AArch64DecodeTable *tptr = table;
1203 while (tptr->mask) {
1204 if ((insn & tptr->mask) == tptr->pattern) {
1205 return tptr->disas_fn;
1207 tptr++;
1209 return NULL;
1213 * the instruction disassembly implemented here matches
1214 * the instruction encoding classifications in chapter 3 (C3)
1215 * of the ARM Architecture Reference Manual (DDI0487A_a)
1218 /* C3.2.7 Unconditional branch (immediate)
1219 * 31 30 26 25 0
1220 * +----+-----------+-------------------------------------+
1221 * | op | 0 0 1 0 1 | imm26 |
1222 * +----+-----------+-------------------------------------+
1224 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
1226 uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
1228 if (insn & (1U << 31)) {
1229 /* C5.6.26 BL Branch with link */
1230 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1233 /* C5.6.20 B Branch / C5.6.26 BL Branch with link */
1234 gen_goto_tb(s, 0, addr);
1237 /* C3.2.1 Compare & branch (immediate)
1238 * 31 30 25 24 23 5 4 0
1239 * +----+-------------+----+---------------------+--------+
1240 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
1241 * +----+-------------+----+---------------------+--------+
1243 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
1245 unsigned int sf, op, rt;
1246 uint64_t addr;
1247 TCGLabel *label_match;
1248 TCGv_i64 tcg_cmp;
1250 sf = extract32(insn, 31, 1);
1251 op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
1252 rt = extract32(insn, 0, 5);
1253 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1255 tcg_cmp = read_cpu_reg(s, rt, sf);
1256 label_match = gen_new_label();
1258 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1259 tcg_cmp, 0, label_match);
1261 gen_goto_tb(s, 0, s->pc);
1262 gen_set_label(label_match);
1263 gen_goto_tb(s, 1, addr);
1266 /* C3.2.5 Test & branch (immediate)
1267 * 31 30 25 24 23 19 18 5 4 0
1268 * +----+-------------+----+-------+-------------+------+
1269 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
1270 * +----+-------------+----+-------+-------------+------+
1272 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1274 unsigned int bit_pos, op, rt;
1275 uint64_t addr;
1276 TCGLabel *label_match;
1277 TCGv_i64 tcg_cmp;
1279 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1280 op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1281 addr = s->pc + sextract32(insn, 5, 14) * 4 - 4;
1282 rt = extract32(insn, 0, 5);
1284 tcg_cmp = tcg_temp_new_i64();
1285 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1286 label_match = gen_new_label();
1287 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1288 tcg_cmp, 0, label_match);
1289 tcg_temp_free_i64(tcg_cmp);
1290 gen_goto_tb(s, 0, s->pc);
1291 gen_set_label(label_match);
1292 gen_goto_tb(s, 1, addr);
1295 /* C3.2.2 / C5.6.19 Conditional branch (immediate)
1296 * 31 25 24 23 5 4 3 0
1297 * +---------------+----+---------------------+----+------+
1298 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
1299 * +---------------+----+---------------------+----+------+
1301 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1303 unsigned int cond;
1304 uint64_t addr;
1306 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1307 unallocated_encoding(s);
1308 return;
1310 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1311 cond = extract32(insn, 0, 4);
1313 if (cond < 0x0e) {
1314 /* genuinely conditional branches */
1315 TCGLabel *label_match = gen_new_label();
1316 arm_gen_test_cc(cond, label_match);
1317 gen_goto_tb(s, 0, s->pc);
1318 gen_set_label(label_match);
1319 gen_goto_tb(s, 1, addr);
1320 } else {
1321 /* 0xe and 0xf are both "always" conditions */
1322 gen_goto_tb(s, 0, addr);
1326 /* C5.6.68 HINT */
1327 static void handle_hint(DisasContext *s, uint32_t insn,
1328 unsigned int op1, unsigned int op2, unsigned int crm)
1330 unsigned int selector = crm << 3 | op2;
1332 if (op1 != 3) {
1333 unallocated_encoding(s);
1334 return;
1337 switch (selector) {
1338 case 0: /* NOP */
1339 return;
1340 case 3: /* WFI */
1341 s->is_jmp = DISAS_WFI;
1342 return;
1343 case 1: /* YIELD */
1344 s->is_jmp = DISAS_YIELD;
1345 return;
1346 case 2: /* WFE */
1347 s->is_jmp = DISAS_WFE;
1348 return;
1349 case 4: /* SEV */
1350 case 5: /* SEVL */
1351 /* we treat all as NOP at least for now */
1352 return;
1353 default:
1354 /* default specified as NOP equivalent */
1355 return;
1359 static void gen_clrex(DisasContext *s, uint32_t insn)
1361 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1364 /* CLREX, DSB, DMB, ISB */
1365 static void handle_sync(DisasContext *s, uint32_t insn,
1366 unsigned int op1, unsigned int op2, unsigned int crm)
1368 TCGBar bar;
1370 if (op1 != 3) {
1371 unallocated_encoding(s);
1372 return;
1375 switch (op2) {
1376 case 2: /* CLREX */
1377 gen_clrex(s, insn);
1378 return;
1379 case 4: /* DSB */
1380 case 5: /* DMB */
1381 switch (crm & 3) {
1382 case 1: /* MBReqTypes_Reads */
1383 bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1384 break;
1385 case 2: /* MBReqTypes_Writes */
1386 bar = TCG_BAR_SC | TCG_MO_ST_ST;
1387 break;
1388 default: /* MBReqTypes_All */
1389 bar = TCG_BAR_SC | TCG_MO_ALL;
1390 break;
1392 tcg_gen_mb(bar);
1393 return;
1394 case 6: /* ISB */
1395 /* We need to break the TB after this insn to execute
1396 * a self-modified code correctly and also to take
1397 * any pending interrupts immediately.
1399 s->is_jmp = DISAS_UPDATE;
1400 return;
1401 default:
1402 unallocated_encoding(s);
1403 return;
1407 /* C5.6.130 MSR (immediate) - move immediate to processor state field */
1408 static void handle_msr_i(DisasContext *s, uint32_t insn,
1409 unsigned int op1, unsigned int op2, unsigned int crm)
1411 int op = op1 << 3 | op2;
1412 switch (op) {
1413 case 0x05: /* SPSel */
1414 if (s->current_el == 0) {
1415 unallocated_encoding(s);
1416 return;
1418 /* fall through */
1419 case 0x1e: /* DAIFSet */
1420 case 0x1f: /* DAIFClear */
1422 TCGv_i32 tcg_imm = tcg_const_i32(crm);
1423 TCGv_i32 tcg_op = tcg_const_i32(op);
1424 gen_a64_set_pc_im(s->pc - 4);
1425 gen_helper_msr_i_pstate(cpu_env, tcg_op, tcg_imm);
1426 tcg_temp_free_i32(tcg_imm);
1427 tcg_temp_free_i32(tcg_op);
1428 s->is_jmp = DISAS_UPDATE;
1429 break;
1431 default:
1432 unallocated_encoding(s);
1433 return;
1437 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1439 TCGv_i32 tmp = tcg_temp_new_i32();
1440 TCGv_i32 nzcv = tcg_temp_new_i32();
1442 /* build bit 31, N */
1443 tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
1444 /* build bit 30, Z */
1445 tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1446 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1447 /* build bit 29, C */
1448 tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1449 /* build bit 28, V */
1450 tcg_gen_shri_i32(tmp, cpu_VF, 31);
1451 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1452 /* generate result */
1453 tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1455 tcg_temp_free_i32(nzcv);
1456 tcg_temp_free_i32(tmp);
1459 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1462 TCGv_i32 nzcv = tcg_temp_new_i32();
1464 /* take NZCV from R[t] */
1465 tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
1467 /* bit 31, N */
1468 tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
1469 /* bit 30, Z */
1470 tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1471 tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1472 /* bit 29, C */
1473 tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1474 tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1475 /* bit 28, V */
1476 tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1477 tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1478 tcg_temp_free_i32(nzcv);
1481 /* C5.6.129 MRS - move from system register
1482 * C5.6.131 MSR (register) - move to system register
1483 * C5.6.204 SYS
1484 * C5.6.205 SYSL
1485 * These are all essentially the same insn in 'read' and 'write'
1486 * versions, with varying op0 fields.
1488 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1489 unsigned int op0, unsigned int op1, unsigned int op2,
1490 unsigned int crn, unsigned int crm, unsigned int rt)
1492 const ARMCPRegInfo *ri;
1493 TCGv_i64 tcg_rt;
1495 ri = get_arm_cp_reginfo(s->cp_regs,
1496 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1497 crn, crm, op0, op1, op2));
1499 if (!ri) {
1500 /* Unknown register; this might be a guest error or a QEMU
1501 * unimplemented feature.
1503 qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1504 "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1505 isread ? "read" : "write", op0, op1, crn, crm, op2);
1506 unallocated_encoding(s);
1507 return;
1510 /* Check access permissions */
1511 if (!cp_access_ok(s->current_el, ri, isread)) {
1512 unallocated_encoding(s);
1513 return;
1516 if (ri->accessfn) {
1517 /* Emit code to perform further access permissions checks at
1518 * runtime; this may result in an exception.
1520 TCGv_ptr tmpptr;
1521 TCGv_i32 tcg_syn, tcg_isread;
1522 uint32_t syndrome;
1524 gen_a64_set_pc_im(s->pc - 4);
1525 tmpptr = tcg_const_ptr(ri);
1526 syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1527 tcg_syn = tcg_const_i32(syndrome);
1528 tcg_isread = tcg_const_i32(isread);
1529 gen_helper_access_check_cp_reg(cpu_env, tmpptr, tcg_syn, tcg_isread);
1530 tcg_temp_free_ptr(tmpptr);
1531 tcg_temp_free_i32(tcg_syn);
1532 tcg_temp_free_i32(tcg_isread);
1535 /* Handle special cases first */
1536 switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
1537 case ARM_CP_NOP:
1538 return;
1539 case ARM_CP_NZCV:
1540 tcg_rt = cpu_reg(s, rt);
1541 if (isread) {
1542 gen_get_nzcv(tcg_rt);
1543 } else {
1544 gen_set_nzcv(tcg_rt);
1546 return;
1547 case ARM_CP_CURRENTEL:
1548 /* Reads as current EL value from pstate, which is
1549 * guaranteed to be constant by the tb flags.
1551 tcg_rt = cpu_reg(s, rt);
1552 tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
1553 return;
1554 case ARM_CP_DC_ZVA:
1555 /* Writes clear the aligned block of memory which rt points into. */
1556 tcg_rt = cpu_reg(s, rt);
1557 gen_helper_dc_zva(cpu_env, tcg_rt);
1558 return;
1559 default:
1560 break;
1563 if ((s->tb->cflags & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1564 gen_io_start();
1567 tcg_rt = cpu_reg(s, rt);
1569 if (isread) {
1570 if (ri->type & ARM_CP_CONST) {
1571 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1572 } else if (ri->readfn) {
1573 TCGv_ptr tmpptr;
1574 tmpptr = tcg_const_ptr(ri);
1575 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1576 tcg_temp_free_ptr(tmpptr);
1577 } else {
1578 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1580 } else {
1581 if (ri->type & ARM_CP_CONST) {
1582 /* If not forbidden by access permissions, treat as WI */
1583 return;
1584 } else if (ri->writefn) {
1585 TCGv_ptr tmpptr;
1586 tmpptr = tcg_const_ptr(ri);
1587 gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1588 tcg_temp_free_ptr(tmpptr);
1589 } else {
1590 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
1594 if ((s->tb->cflags & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1595 /* I/O operations must end the TB here (whether read or write) */
1596 gen_io_end();
1597 s->is_jmp = DISAS_UPDATE;
1598 } else if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
1599 /* We default to ending the TB on a coprocessor register write,
1600 * but allow this to be suppressed by the register definition
1601 * (usually only necessary to work around guest bugs).
1603 s->is_jmp = DISAS_UPDATE;
1607 /* C3.2.4 System
1608 * 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0
1609 * +---------------------+---+-----+-----+-------+-------+-----+------+
1610 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 | CRn | CRm | op2 | Rt |
1611 * +---------------------+---+-----+-----+-------+-------+-----+------+
1613 static void disas_system(DisasContext *s, uint32_t insn)
1615 unsigned int l, op0, op1, crn, crm, op2, rt;
1616 l = extract32(insn, 21, 1);
1617 op0 = extract32(insn, 19, 2);
1618 op1 = extract32(insn, 16, 3);
1619 crn = extract32(insn, 12, 4);
1620 crm = extract32(insn, 8, 4);
1621 op2 = extract32(insn, 5, 3);
1622 rt = extract32(insn, 0, 5);
1624 if (op0 == 0) {
1625 if (l || rt != 31) {
1626 unallocated_encoding(s);
1627 return;
1629 switch (crn) {
1630 case 2: /* C5.6.68 HINT */
1631 handle_hint(s, insn, op1, op2, crm);
1632 break;
1633 case 3: /* CLREX, DSB, DMB, ISB */
1634 handle_sync(s, insn, op1, op2, crm);
1635 break;
1636 case 4: /* C5.6.130 MSR (immediate) */
1637 handle_msr_i(s, insn, op1, op2, crm);
1638 break;
1639 default:
1640 unallocated_encoding(s);
1641 break;
1643 return;
1645 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
1648 /* C3.2.3 Exception generation
1650 * 31 24 23 21 20 5 4 2 1 0
1651 * +-----------------+-----+------------------------+-----+----+
1652 * | 1 1 0 1 0 1 0 0 | opc | imm16 | op2 | LL |
1653 * +-----------------------+------------------------+----------+
1655 static void disas_exc(DisasContext *s, uint32_t insn)
1657 int opc = extract32(insn, 21, 3);
1658 int op2_ll = extract32(insn, 0, 5);
1659 int imm16 = extract32(insn, 5, 16);
1660 TCGv_i32 tmp;
1662 switch (opc) {
1663 case 0:
1664 /* For SVC, HVC and SMC we advance the single-step state
1665 * machine before taking the exception. This is architecturally
1666 * mandated, to ensure that single-stepping a system call
1667 * instruction works properly.
1669 switch (op2_ll) {
1670 case 1: /* SVC */
1671 gen_ss_advance(s);
1672 gen_exception_insn(s, 0, EXCP_SWI, syn_aa64_svc(imm16),
1673 default_exception_el(s));
1674 break;
1675 case 2: /* HVC */
1676 if (s->current_el == 0) {
1677 unallocated_encoding(s);
1678 break;
1680 /* The pre HVC helper handles cases when HVC gets trapped
1681 * as an undefined insn by runtime configuration.
1683 gen_a64_set_pc_im(s->pc - 4);
1684 gen_helper_pre_hvc(cpu_env);
1685 gen_ss_advance(s);
1686 gen_exception_insn(s, 0, EXCP_HVC, syn_aa64_hvc(imm16), 2);
1687 break;
1688 case 3: /* SMC */
1689 if (s->current_el == 0) {
1690 unallocated_encoding(s);
1691 break;
1693 gen_a64_set_pc_im(s->pc - 4);
1694 tmp = tcg_const_i32(syn_aa64_smc(imm16));
1695 gen_helper_pre_smc(cpu_env, tmp);
1696 tcg_temp_free_i32(tmp);
1697 gen_ss_advance(s);
1698 gen_exception_insn(s, 0, EXCP_SMC, syn_aa64_smc(imm16), 3);
1699 break;
1700 default:
1701 unallocated_encoding(s);
1702 break;
1704 break;
1705 case 1:
1706 if (op2_ll != 0) {
1707 unallocated_encoding(s);
1708 break;
1710 /* BRK */
1711 gen_exception_insn(s, 4, EXCP_BKPT, syn_aa64_bkpt(imm16),
1712 default_exception_el(s));
1713 break;
1714 case 2:
1715 if (op2_ll != 0) {
1716 unallocated_encoding(s);
1717 break;
1719 /* HLT. This has two purposes.
1720 * Architecturally, it is an external halting debug instruction.
1721 * Since QEMU doesn't implement external debug, we treat this as
1722 * it is required for halting debug disabled: it will UNDEF.
1723 * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
1725 if (semihosting_enabled() && imm16 == 0xf000) {
1726 #ifndef CONFIG_USER_ONLY
1727 /* In system mode, don't allow userspace access to semihosting,
1728 * to provide some semblance of security (and for consistency
1729 * with our 32-bit semihosting).
1731 if (s->current_el == 0) {
1732 unsupported_encoding(s, insn);
1733 break;
1735 #endif
1736 gen_exception_internal_insn(s, 0, EXCP_SEMIHOST);
1737 } else {
1738 unsupported_encoding(s, insn);
1740 break;
1741 case 5:
1742 if (op2_ll < 1 || op2_ll > 3) {
1743 unallocated_encoding(s);
1744 break;
1746 /* DCPS1, DCPS2, DCPS3 */
1747 unsupported_encoding(s, insn);
1748 break;
1749 default:
1750 unallocated_encoding(s);
1751 break;
1755 /* C3.2.7 Unconditional branch (register)
1756 * 31 25 24 21 20 16 15 10 9 5 4 0
1757 * +---------------+-------+-------+-------+------+-------+
1758 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
1759 * +---------------+-------+-------+-------+------+-------+
1761 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
1763 unsigned int opc, op2, op3, rn, op4;
1765 opc = extract32(insn, 21, 4);
1766 op2 = extract32(insn, 16, 5);
1767 op3 = extract32(insn, 10, 6);
1768 rn = extract32(insn, 5, 5);
1769 op4 = extract32(insn, 0, 5);
1771 if (op4 != 0x0 || op3 != 0x0 || op2 != 0x1f) {
1772 unallocated_encoding(s);
1773 return;
1776 switch (opc) {
1777 case 0: /* BR */
1778 case 1: /* BLR */
1779 case 2: /* RET */
1780 gen_a64_set_pc(s, cpu_reg(s, rn));
1781 /* BLR also needs to load return address */
1782 if (opc == 1) {
1783 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1785 break;
1786 case 4: /* ERET */
1787 if (s->current_el == 0) {
1788 unallocated_encoding(s);
1789 return;
1791 gen_helper_exception_return(cpu_env);
1792 s->is_jmp = DISAS_JUMP;
1793 return;
1794 case 5: /* DRPS */
1795 if (rn != 0x1f) {
1796 unallocated_encoding(s);
1797 } else {
1798 unsupported_encoding(s, insn);
1800 return;
1801 default:
1802 unallocated_encoding(s);
1803 return;
1806 s->is_jmp = DISAS_JUMP;
1809 /* C3.2 Branches, exception generating and system instructions */
1810 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
1812 switch (extract32(insn, 25, 7)) {
1813 case 0x0a: case 0x0b:
1814 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
1815 disas_uncond_b_imm(s, insn);
1816 break;
1817 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
1818 disas_comp_b_imm(s, insn);
1819 break;
1820 case 0x1b: case 0x5b: /* Test & branch (immediate) */
1821 disas_test_b_imm(s, insn);
1822 break;
1823 case 0x2a: /* Conditional branch (immediate) */
1824 disas_cond_b_imm(s, insn);
1825 break;
1826 case 0x6a: /* Exception generation / System */
1827 if (insn & (1 << 24)) {
1828 disas_system(s, insn);
1829 } else {
1830 disas_exc(s, insn);
1832 break;
1833 case 0x6b: /* Unconditional branch (register) */
1834 disas_uncond_b_reg(s, insn);
1835 break;
1836 default:
1837 unallocated_encoding(s);
1838 break;
1842 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
1843 TCGv_i64 addr, int size, bool is_pair)
1845 TCGv_i64 tmp = tcg_temp_new_i64();
1846 TCGMemOp be = s->be_data;
1848 g_assert(size <= 3);
1849 if (is_pair) {
1850 TCGv_i64 hitmp = tcg_temp_new_i64();
1852 if (size == 3) {
1853 TCGv_i64 addr2 = tcg_temp_new_i64();
1855 tcg_gen_qemu_ld_i64(tmp, addr, get_mem_index(s),
1856 MO_64 | MO_ALIGN_16 | be);
1857 tcg_gen_addi_i64(addr2, addr, 8);
1858 tcg_gen_qemu_ld_i64(hitmp, addr2, get_mem_index(s),
1859 MO_64 | MO_ALIGN | be);
1860 tcg_temp_free_i64(addr2);
1861 } else {
1862 g_assert(size == 2);
1863 tcg_gen_qemu_ld_i64(tmp, addr, get_mem_index(s),
1864 MO_64 | MO_ALIGN | be);
1865 if (be == MO_LE) {
1866 tcg_gen_extr32_i64(tmp, hitmp, tmp);
1867 } else {
1868 tcg_gen_extr32_i64(hitmp, tmp, tmp);
1872 tcg_gen_mov_i64(cpu_exclusive_high, hitmp);
1873 tcg_gen_mov_i64(cpu_reg(s, rt2), hitmp);
1874 tcg_temp_free_i64(hitmp);
1875 } else {
1876 tcg_gen_qemu_ld_i64(tmp, addr, get_mem_index(s), size | MO_ALIGN | be);
1879 tcg_gen_mov_i64(cpu_exclusive_val, tmp);
1880 tcg_gen_mov_i64(cpu_reg(s, rt), tmp);
1882 tcg_temp_free_i64(tmp);
1883 tcg_gen_mov_i64(cpu_exclusive_addr, addr);
1886 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
1887 TCGv_i64 inaddr, int size, int is_pair)
1889 /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
1890 * && (!is_pair || env->exclusive_high == [addr + datasize])) {
1891 * [addr] = {Rt};
1892 * if (is_pair) {
1893 * [addr + datasize] = {Rt2};
1895 * {Rd} = 0;
1896 * } else {
1897 * {Rd} = 1;
1899 * env->exclusive_addr = -1;
1901 TCGLabel *fail_label = gen_new_label();
1902 TCGLabel *done_label = gen_new_label();
1903 TCGv_i64 addr = tcg_temp_local_new_i64();
1904 TCGv_i64 tmp;
1906 /* Copy input into a local temp so it is not trashed when the
1907 * basic block ends at the branch insn.
1909 tcg_gen_mov_i64(addr, inaddr);
1910 tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
1912 tmp = tcg_temp_new_i64();
1913 if (is_pair) {
1914 if (size == 2) {
1915 TCGv_i64 val = tcg_temp_new_i64();
1916 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
1917 tcg_gen_concat32_i64(val, cpu_exclusive_val, cpu_exclusive_high);
1918 tcg_gen_atomic_cmpxchg_i64(tmp, addr, val, tmp,
1919 get_mem_index(s),
1920 size | MO_ALIGN | s->be_data);
1921 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, val);
1922 tcg_temp_free_i64(val);
1923 } else if (s->be_data == MO_LE) {
1924 gen_helper_paired_cmpxchg64_le(tmp, cpu_env, addr, cpu_reg(s, rt),
1925 cpu_reg(s, rt2));
1926 } else {
1927 gen_helper_paired_cmpxchg64_be(tmp, cpu_env, addr, cpu_reg(s, rt),
1928 cpu_reg(s, rt2));
1930 } else {
1931 TCGv_i64 val = cpu_reg(s, rt);
1932 tcg_gen_atomic_cmpxchg_i64(tmp, addr, cpu_exclusive_val, val,
1933 get_mem_index(s),
1934 size | MO_ALIGN | s->be_data);
1935 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
1938 tcg_temp_free_i64(addr);
1940 tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
1941 tcg_temp_free_i64(tmp);
1942 tcg_gen_br(done_label);
1944 gen_set_label(fail_label);
1945 tcg_gen_movi_i64(cpu_reg(s, rd), 1);
1946 gen_set_label(done_label);
1947 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1950 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
1951 * from the ARMv8 specs for LDR (Shared decode for all encodings).
1953 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
1955 int opc0 = extract32(opc, 0, 1);
1956 int regsize;
1958 if (is_signed) {
1959 regsize = opc0 ? 32 : 64;
1960 } else {
1961 regsize = size == 3 ? 64 : 32;
1963 return regsize == 64;
1966 /* C3.3.6 Load/store exclusive
1968 * 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0
1969 * +-----+-------------+----+---+----+------+----+-------+------+------+
1970 * | sz | 0 0 1 0 0 0 | o2 | L | o1 | Rs | o0 | Rt2 | Rn | Rt |
1971 * +-----+-------------+----+---+----+------+----+-------+------+------+
1973 * sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
1974 * L: 0 -> store, 1 -> load
1975 * o2: 0 -> exclusive, 1 -> not
1976 * o1: 0 -> single register, 1 -> register pair
1977 * o0: 1 -> load-acquire/store-release, 0 -> not
1979 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
1981 int rt = extract32(insn, 0, 5);
1982 int rn = extract32(insn, 5, 5);
1983 int rt2 = extract32(insn, 10, 5);
1984 int is_lasr = extract32(insn, 15, 1);
1985 int rs = extract32(insn, 16, 5);
1986 int is_pair = extract32(insn, 21, 1);
1987 int is_store = !extract32(insn, 22, 1);
1988 int is_excl = !extract32(insn, 23, 1);
1989 int size = extract32(insn, 30, 2);
1990 TCGv_i64 tcg_addr;
1992 if ((!is_excl && !is_pair && !is_lasr) ||
1993 (!is_excl && is_pair) ||
1994 (is_pair && size < 2)) {
1995 unallocated_encoding(s);
1996 return;
1999 if (rn == 31) {
2000 gen_check_sp_alignment(s);
2002 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2004 /* Note that since TCG is single threaded load-acquire/store-release
2005 * semantics require no extra if (is_lasr) { ... } handling.
2008 if (is_excl) {
2009 if (!is_store) {
2010 s->is_ldex = true;
2011 gen_load_exclusive(s, rt, rt2, tcg_addr, size, is_pair);
2012 if (is_lasr) {
2013 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2015 } else {
2016 if (is_lasr) {
2017 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2019 gen_store_exclusive(s, rs, rt, rt2, tcg_addr, size, is_pair);
2021 } else {
2022 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2023 bool iss_sf = disas_ldst_compute_iss_sf(size, false, 0);
2025 /* Generate ISS for non-exclusive accesses including LASR. */
2026 if (is_store) {
2027 if (is_lasr) {
2028 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2030 do_gpr_st(s, tcg_rt, tcg_addr, size,
2031 true, rt, iss_sf, is_lasr);
2032 } else {
2033 do_gpr_ld(s, tcg_rt, tcg_addr, size, false, false,
2034 true, rt, iss_sf, is_lasr);
2035 if (is_lasr) {
2036 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2043 * C3.3.5 Load register (literal)
2045 * 31 30 29 27 26 25 24 23 5 4 0
2046 * +-----+-------+---+-----+-------------------+-------+
2047 * | opc | 0 1 1 | V | 0 0 | imm19 | Rt |
2048 * +-----+-------+---+-----+-------------------+-------+
2050 * V: 1 -> vector (simd/fp)
2051 * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2052 * 10-> 32 bit signed, 11 -> prefetch
2053 * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2055 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2057 int rt = extract32(insn, 0, 5);
2058 int64_t imm = sextract32(insn, 5, 19) << 2;
2059 bool is_vector = extract32(insn, 26, 1);
2060 int opc = extract32(insn, 30, 2);
2061 bool is_signed = false;
2062 int size = 2;
2063 TCGv_i64 tcg_rt, tcg_addr;
2065 if (is_vector) {
2066 if (opc == 3) {
2067 unallocated_encoding(s);
2068 return;
2070 size = 2 + opc;
2071 if (!fp_access_check(s)) {
2072 return;
2074 } else {
2075 if (opc == 3) {
2076 /* PRFM (literal) : prefetch */
2077 return;
2079 size = 2 + extract32(opc, 0, 1);
2080 is_signed = extract32(opc, 1, 1);
2083 tcg_rt = cpu_reg(s, rt);
2085 tcg_addr = tcg_const_i64((s->pc - 4) + imm);
2086 if (is_vector) {
2087 do_fp_ld(s, rt, tcg_addr, size);
2088 } else {
2089 /* Only unsigned 32bit loads target 32bit registers. */
2090 bool iss_sf = opc != 0;
2092 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false,
2093 true, rt, iss_sf, false);
2095 tcg_temp_free_i64(tcg_addr);
2099 * C5.6.80 LDNP (Load Pair - non-temporal hint)
2100 * C5.6.81 LDP (Load Pair - non vector)
2101 * C5.6.82 LDPSW (Load Pair Signed Word - non vector)
2102 * C5.6.176 STNP (Store Pair - non-temporal hint)
2103 * C5.6.177 STP (Store Pair - non vector)
2104 * C6.3.165 LDNP (Load Pair of SIMD&FP - non-temporal hint)
2105 * C6.3.165 LDP (Load Pair of SIMD&FP)
2106 * C6.3.284 STNP (Store Pair of SIMD&FP - non-temporal hint)
2107 * C6.3.284 STP (Store Pair of SIMD&FP)
2109 * 31 30 29 27 26 25 24 23 22 21 15 14 10 9 5 4 0
2110 * +-----+-------+---+---+-------+---+-----------------------------+
2111 * | opc | 1 0 1 | V | 0 | index | L | imm7 | Rt2 | Rn | Rt |
2112 * +-----+-------+---+---+-------+---+-------+-------+------+------+
2114 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
2115 * LDPSW 01
2116 * LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2117 * V: 0 -> GPR, 1 -> Vector
2118 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2119 * 10 -> signed offset, 11 -> pre-index
2120 * L: 0 -> Store 1 -> Load
2122 * Rt, Rt2 = GPR or SIMD registers to be stored
2123 * Rn = general purpose register containing address
2124 * imm7 = signed offset (multiple of 4 or 8 depending on size)
2126 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2128 int rt = extract32(insn, 0, 5);
2129 int rn = extract32(insn, 5, 5);
2130 int rt2 = extract32(insn, 10, 5);
2131 uint64_t offset = sextract64(insn, 15, 7);
2132 int index = extract32(insn, 23, 2);
2133 bool is_vector = extract32(insn, 26, 1);
2134 bool is_load = extract32(insn, 22, 1);
2135 int opc = extract32(insn, 30, 2);
2137 bool is_signed = false;
2138 bool postindex = false;
2139 bool wback = false;
2141 TCGv_i64 tcg_addr; /* calculated address */
2142 int size;
2144 if (opc == 3) {
2145 unallocated_encoding(s);
2146 return;
2149 if (is_vector) {
2150 size = 2 + opc;
2151 } else {
2152 size = 2 + extract32(opc, 1, 1);
2153 is_signed = extract32(opc, 0, 1);
2154 if (!is_load && is_signed) {
2155 unallocated_encoding(s);
2156 return;
2160 switch (index) {
2161 case 1: /* post-index */
2162 postindex = true;
2163 wback = true;
2164 break;
2165 case 0:
2166 /* signed offset with "non-temporal" hint. Since we don't emulate
2167 * caches we don't care about hints to the cache system about
2168 * data access patterns, and handle this identically to plain
2169 * signed offset.
2171 if (is_signed) {
2172 /* There is no non-temporal-hint version of LDPSW */
2173 unallocated_encoding(s);
2174 return;
2176 postindex = false;
2177 break;
2178 case 2: /* signed offset, rn not updated */
2179 postindex = false;
2180 break;
2181 case 3: /* pre-index */
2182 postindex = false;
2183 wback = true;
2184 break;
2187 if (is_vector && !fp_access_check(s)) {
2188 return;
2191 offset <<= size;
2193 if (rn == 31) {
2194 gen_check_sp_alignment(s);
2197 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2199 if (!postindex) {
2200 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
2203 if (is_vector) {
2204 if (is_load) {
2205 do_fp_ld(s, rt, tcg_addr, size);
2206 } else {
2207 do_fp_st(s, rt, tcg_addr, size);
2209 } else {
2210 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2211 if (is_load) {
2212 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false,
2213 false, 0, false, false);
2214 } else {
2215 do_gpr_st(s, tcg_rt, tcg_addr, size,
2216 false, 0, false, false);
2219 tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
2220 if (is_vector) {
2221 if (is_load) {
2222 do_fp_ld(s, rt2, tcg_addr, size);
2223 } else {
2224 do_fp_st(s, rt2, tcg_addr, size);
2226 } else {
2227 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
2228 if (is_load) {
2229 do_gpr_ld(s, tcg_rt2, tcg_addr, size, is_signed, false,
2230 false, 0, false, false);
2231 } else {
2232 do_gpr_st(s, tcg_rt2, tcg_addr, size,
2233 false, 0, false, false);
2237 if (wback) {
2238 if (postindex) {
2239 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset - (1 << size));
2240 } else {
2241 tcg_gen_subi_i64(tcg_addr, tcg_addr, 1 << size);
2243 tcg_gen_mov_i64(cpu_reg_sp(s, rn), tcg_addr);
2248 * C3.3.8 Load/store (immediate post-indexed)
2249 * C3.3.9 Load/store (immediate pre-indexed)
2250 * C3.3.12 Load/store (unscaled immediate)
2252 * 31 30 29 27 26 25 24 23 22 21 20 12 11 10 9 5 4 0
2253 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2254 * |size| 1 1 1 | V | 0 0 | opc | 0 | imm9 | idx | Rn | Rt |
2255 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2257 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
2258 10 -> unprivileged
2259 * V = 0 -> non-vector
2260 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
2261 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2263 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
2264 int opc,
2265 int size,
2266 int rt,
2267 bool is_vector)
2269 int rn = extract32(insn, 5, 5);
2270 int imm9 = sextract32(insn, 12, 9);
2271 int idx = extract32(insn, 10, 2);
2272 bool is_signed = false;
2273 bool is_store = false;
2274 bool is_extended = false;
2275 bool is_unpriv = (idx == 2);
2276 bool iss_valid = !is_vector;
2277 bool post_index;
2278 bool writeback;
2280 TCGv_i64 tcg_addr;
2282 if (is_vector) {
2283 size |= (opc & 2) << 1;
2284 if (size > 4 || is_unpriv) {
2285 unallocated_encoding(s);
2286 return;
2288 is_store = ((opc & 1) == 0);
2289 if (!fp_access_check(s)) {
2290 return;
2292 } else {
2293 if (size == 3 && opc == 2) {
2294 /* PRFM - prefetch */
2295 if (is_unpriv) {
2296 unallocated_encoding(s);
2297 return;
2299 return;
2301 if (opc == 3 && size > 1) {
2302 unallocated_encoding(s);
2303 return;
2305 is_store = (opc == 0);
2306 is_signed = extract32(opc, 1, 1);
2307 is_extended = (size < 3) && extract32(opc, 0, 1);
2310 switch (idx) {
2311 case 0:
2312 case 2:
2313 post_index = false;
2314 writeback = false;
2315 break;
2316 case 1:
2317 post_index = true;
2318 writeback = true;
2319 break;
2320 case 3:
2321 post_index = false;
2322 writeback = true;
2323 break;
2326 if (rn == 31) {
2327 gen_check_sp_alignment(s);
2329 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2331 if (!post_index) {
2332 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
2335 if (is_vector) {
2336 if (is_store) {
2337 do_fp_st(s, rt, tcg_addr, size);
2338 } else {
2339 do_fp_ld(s, rt, tcg_addr, size);
2341 } else {
2342 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2343 int memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
2344 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2346 if (is_store) {
2347 do_gpr_st_memidx(s, tcg_rt, tcg_addr, size, memidx,
2348 iss_valid, rt, iss_sf, false);
2349 } else {
2350 do_gpr_ld_memidx(s, tcg_rt, tcg_addr, size,
2351 is_signed, is_extended, memidx,
2352 iss_valid, rt, iss_sf, false);
2356 if (writeback) {
2357 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2358 if (post_index) {
2359 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
2361 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2366 * C3.3.10 Load/store (register offset)
2368 * 31 30 29 27 26 25 24 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2369 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2370 * |size| 1 1 1 | V | 0 0 | opc | 1 | Rm | opt | S| 1 0 | Rn | Rt |
2371 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2373 * For non-vector:
2374 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2375 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2376 * For vector:
2377 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2378 * opc<0>: 0 -> store, 1 -> load
2379 * V: 1 -> vector/simd
2380 * opt: extend encoding (see DecodeRegExtend)
2381 * S: if S=1 then scale (essentially index by sizeof(size))
2382 * Rt: register to transfer into/out of
2383 * Rn: address register or SP for base
2384 * Rm: offset register or ZR for offset
2386 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
2387 int opc,
2388 int size,
2389 int rt,
2390 bool is_vector)
2392 int rn = extract32(insn, 5, 5);
2393 int shift = extract32(insn, 12, 1);
2394 int rm = extract32(insn, 16, 5);
2395 int opt = extract32(insn, 13, 3);
2396 bool is_signed = false;
2397 bool is_store = false;
2398 bool is_extended = false;
2400 TCGv_i64 tcg_rm;
2401 TCGv_i64 tcg_addr;
2403 if (extract32(opt, 1, 1) == 0) {
2404 unallocated_encoding(s);
2405 return;
2408 if (is_vector) {
2409 size |= (opc & 2) << 1;
2410 if (size > 4) {
2411 unallocated_encoding(s);
2412 return;
2414 is_store = !extract32(opc, 0, 1);
2415 if (!fp_access_check(s)) {
2416 return;
2418 } else {
2419 if (size == 3 && opc == 2) {
2420 /* PRFM - prefetch */
2421 return;
2423 if (opc == 3 && size > 1) {
2424 unallocated_encoding(s);
2425 return;
2427 is_store = (opc == 0);
2428 is_signed = extract32(opc, 1, 1);
2429 is_extended = (size < 3) && extract32(opc, 0, 1);
2432 if (rn == 31) {
2433 gen_check_sp_alignment(s);
2435 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2437 tcg_rm = read_cpu_reg(s, rm, 1);
2438 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
2440 tcg_gen_add_i64(tcg_addr, tcg_addr, tcg_rm);
2442 if (is_vector) {
2443 if (is_store) {
2444 do_fp_st(s, rt, tcg_addr, size);
2445 } else {
2446 do_fp_ld(s, rt, tcg_addr, size);
2448 } else {
2449 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2450 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2451 if (is_store) {
2452 do_gpr_st(s, tcg_rt, tcg_addr, size,
2453 true, rt, iss_sf, false);
2454 } else {
2455 do_gpr_ld(s, tcg_rt, tcg_addr, size,
2456 is_signed, is_extended,
2457 true, rt, iss_sf, false);
2463 * C3.3.13 Load/store (unsigned immediate)
2465 * 31 30 29 27 26 25 24 23 22 21 10 9 5
2466 * +----+-------+---+-----+-----+------------+-------+------+
2467 * |size| 1 1 1 | V | 0 1 | opc | imm12 | Rn | Rt |
2468 * +----+-------+---+-----+-----+------------+-------+------+
2470 * For non-vector:
2471 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2472 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2473 * For vector:
2474 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2475 * opc<0>: 0 -> store, 1 -> load
2476 * Rn: base address register (inc SP)
2477 * Rt: target register
2479 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
2480 int opc,
2481 int size,
2482 int rt,
2483 bool is_vector)
2485 int rn = extract32(insn, 5, 5);
2486 unsigned int imm12 = extract32(insn, 10, 12);
2487 unsigned int offset;
2489 TCGv_i64 tcg_addr;
2491 bool is_store;
2492 bool is_signed = false;
2493 bool is_extended = false;
2495 if (is_vector) {
2496 size |= (opc & 2) << 1;
2497 if (size > 4) {
2498 unallocated_encoding(s);
2499 return;
2501 is_store = !extract32(opc, 0, 1);
2502 if (!fp_access_check(s)) {
2503 return;
2505 } else {
2506 if (size == 3 && opc == 2) {
2507 /* PRFM - prefetch */
2508 return;
2510 if (opc == 3 && size > 1) {
2511 unallocated_encoding(s);
2512 return;
2514 is_store = (opc == 0);
2515 is_signed = extract32(opc, 1, 1);
2516 is_extended = (size < 3) && extract32(opc, 0, 1);
2519 if (rn == 31) {
2520 gen_check_sp_alignment(s);
2522 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2523 offset = imm12 << size;
2524 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
2526 if (is_vector) {
2527 if (is_store) {
2528 do_fp_st(s, rt, tcg_addr, size);
2529 } else {
2530 do_fp_ld(s, rt, tcg_addr, size);
2532 } else {
2533 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2534 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2535 if (is_store) {
2536 do_gpr_st(s, tcg_rt, tcg_addr, size,
2537 true, rt, iss_sf, false);
2538 } else {
2539 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended,
2540 true, rt, iss_sf, false);
2545 /* Load/store register (all forms) */
2546 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
2548 int rt = extract32(insn, 0, 5);
2549 int opc = extract32(insn, 22, 2);
2550 bool is_vector = extract32(insn, 26, 1);
2551 int size = extract32(insn, 30, 2);
2553 switch (extract32(insn, 24, 2)) {
2554 case 0:
2555 if (extract32(insn, 21, 1) == 1 && extract32(insn, 10, 2) == 2) {
2556 disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
2557 } else {
2558 /* Load/store register (unscaled immediate)
2559 * Load/store immediate pre/post-indexed
2560 * Load/store register unprivileged
2562 disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
2564 break;
2565 case 1:
2566 disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
2567 break;
2568 default:
2569 unallocated_encoding(s);
2570 break;
2574 /* C3.3.1 AdvSIMD load/store multiple structures
2576 * 31 30 29 23 22 21 16 15 12 11 10 9 5 4 0
2577 * +---+---+---------------+---+-------------+--------+------+------+------+
2578 * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size | Rn | Rt |
2579 * +---+---+---------------+---+-------------+--------+------+------+------+
2581 * C3.3.2 AdvSIMD load/store multiple structures (post-indexed)
2583 * 31 30 29 23 22 21 20 16 15 12 11 10 9 5 4 0
2584 * +---+---+---------------+---+---+---------+--------+------+------+------+
2585 * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 | Rm | opcode | size | Rn | Rt |
2586 * +---+---+---------------+---+---+---------+--------+------+------+------+
2588 * Rt: first (or only) SIMD&FP register to be transferred
2589 * Rn: base address or SP
2590 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2592 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
2594 int rt = extract32(insn, 0, 5);
2595 int rn = extract32(insn, 5, 5);
2596 int size = extract32(insn, 10, 2);
2597 int opcode = extract32(insn, 12, 4);
2598 bool is_store = !extract32(insn, 22, 1);
2599 bool is_postidx = extract32(insn, 23, 1);
2600 bool is_q = extract32(insn, 30, 1);
2601 TCGv_i64 tcg_addr, tcg_rn;
2603 int ebytes = 1 << size;
2604 int elements = (is_q ? 128 : 64) / (8 << size);
2605 int rpt; /* num iterations */
2606 int selem; /* structure elements */
2607 int r;
2609 if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
2610 unallocated_encoding(s);
2611 return;
2614 /* From the shared decode logic */
2615 switch (opcode) {
2616 case 0x0:
2617 rpt = 1;
2618 selem = 4;
2619 break;
2620 case 0x2:
2621 rpt = 4;
2622 selem = 1;
2623 break;
2624 case 0x4:
2625 rpt = 1;
2626 selem = 3;
2627 break;
2628 case 0x6:
2629 rpt = 3;
2630 selem = 1;
2631 break;
2632 case 0x7:
2633 rpt = 1;
2634 selem = 1;
2635 break;
2636 case 0x8:
2637 rpt = 1;
2638 selem = 2;
2639 break;
2640 case 0xa:
2641 rpt = 2;
2642 selem = 1;
2643 break;
2644 default:
2645 unallocated_encoding(s);
2646 return;
2649 if (size == 3 && !is_q && selem != 1) {
2650 /* reserved */
2651 unallocated_encoding(s);
2652 return;
2655 if (!fp_access_check(s)) {
2656 return;
2659 if (rn == 31) {
2660 gen_check_sp_alignment(s);
2663 tcg_rn = cpu_reg_sp(s, rn);
2664 tcg_addr = tcg_temp_new_i64();
2665 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2667 for (r = 0; r < rpt; r++) {
2668 int e;
2669 for (e = 0; e < elements; e++) {
2670 int tt = (rt + r) % 32;
2671 int xs;
2672 for (xs = 0; xs < selem; xs++) {
2673 if (is_store) {
2674 do_vec_st(s, tt, e, tcg_addr, size);
2675 } else {
2676 do_vec_ld(s, tt, e, tcg_addr, size);
2678 /* For non-quad operations, setting a slice of the low
2679 * 64 bits of the register clears the high 64 bits (in
2680 * the ARM ARM pseudocode this is implicit in the fact
2681 * that 'rval' is a 64 bit wide variable). We optimize
2682 * by noticing that we only need to do this the first
2683 * time we touch a register.
2685 if (!is_q && e == 0 && (r == 0 || xs == selem - 1)) {
2686 clear_vec_high(s, tt);
2689 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2690 tt = (tt + 1) % 32;
2695 if (is_postidx) {
2696 int rm = extract32(insn, 16, 5);
2697 if (rm == 31) {
2698 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2699 } else {
2700 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2703 tcg_temp_free_i64(tcg_addr);
2706 /* C3.3.3 AdvSIMD load/store single structure
2708 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2709 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2710 * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size | Rn | Rt |
2711 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2713 * C3.3.4 AdvSIMD load/store single structure (post-indexed)
2715 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2716 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2717 * | 0 | Q | 0 0 1 1 0 1 1 | L R | Rm | opc | S | size | Rn | Rt |
2718 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2720 * Rt: first (or only) SIMD&FP register to be transferred
2721 * Rn: base address or SP
2722 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2723 * index = encoded in Q:S:size dependent on size
2725 * lane_size = encoded in R, opc
2726 * transfer width = encoded in opc, S, size
2728 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
2730 int rt = extract32(insn, 0, 5);
2731 int rn = extract32(insn, 5, 5);
2732 int size = extract32(insn, 10, 2);
2733 int S = extract32(insn, 12, 1);
2734 int opc = extract32(insn, 13, 3);
2735 int R = extract32(insn, 21, 1);
2736 int is_load = extract32(insn, 22, 1);
2737 int is_postidx = extract32(insn, 23, 1);
2738 int is_q = extract32(insn, 30, 1);
2740 int scale = extract32(opc, 1, 2);
2741 int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
2742 bool replicate = false;
2743 int index = is_q << 3 | S << 2 | size;
2744 int ebytes, xs;
2745 TCGv_i64 tcg_addr, tcg_rn;
2747 switch (scale) {
2748 case 3:
2749 if (!is_load || S) {
2750 unallocated_encoding(s);
2751 return;
2753 scale = size;
2754 replicate = true;
2755 break;
2756 case 0:
2757 break;
2758 case 1:
2759 if (extract32(size, 0, 1)) {
2760 unallocated_encoding(s);
2761 return;
2763 index >>= 1;
2764 break;
2765 case 2:
2766 if (extract32(size, 1, 1)) {
2767 unallocated_encoding(s);
2768 return;
2770 if (!extract32(size, 0, 1)) {
2771 index >>= 2;
2772 } else {
2773 if (S) {
2774 unallocated_encoding(s);
2775 return;
2777 index >>= 3;
2778 scale = 3;
2780 break;
2781 default:
2782 g_assert_not_reached();
2785 if (!fp_access_check(s)) {
2786 return;
2789 ebytes = 1 << scale;
2791 if (rn == 31) {
2792 gen_check_sp_alignment(s);
2795 tcg_rn = cpu_reg_sp(s, rn);
2796 tcg_addr = tcg_temp_new_i64();
2797 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2799 for (xs = 0; xs < selem; xs++) {
2800 if (replicate) {
2801 /* Load and replicate to all elements */
2802 uint64_t mulconst;
2803 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
2805 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr,
2806 get_mem_index(s), s->be_data + scale);
2807 switch (scale) {
2808 case 0:
2809 mulconst = 0x0101010101010101ULL;
2810 break;
2811 case 1:
2812 mulconst = 0x0001000100010001ULL;
2813 break;
2814 case 2:
2815 mulconst = 0x0000000100000001ULL;
2816 break;
2817 case 3:
2818 mulconst = 0;
2819 break;
2820 default:
2821 g_assert_not_reached();
2823 if (mulconst) {
2824 tcg_gen_muli_i64(tcg_tmp, tcg_tmp, mulconst);
2826 write_vec_element(s, tcg_tmp, rt, 0, MO_64);
2827 if (is_q) {
2828 write_vec_element(s, tcg_tmp, rt, 1, MO_64);
2829 } else {
2830 clear_vec_high(s, rt);
2832 tcg_temp_free_i64(tcg_tmp);
2833 } else {
2834 /* Load/store one element per register */
2835 if (is_load) {
2836 do_vec_ld(s, rt, index, tcg_addr, s->be_data + scale);
2837 } else {
2838 do_vec_st(s, rt, index, tcg_addr, s->be_data + scale);
2841 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2842 rt = (rt + 1) % 32;
2845 if (is_postidx) {
2846 int rm = extract32(insn, 16, 5);
2847 if (rm == 31) {
2848 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2849 } else {
2850 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2853 tcg_temp_free_i64(tcg_addr);
2856 /* C3.3 Loads and stores */
2857 static void disas_ldst(DisasContext *s, uint32_t insn)
2859 switch (extract32(insn, 24, 6)) {
2860 case 0x08: /* Load/store exclusive */
2861 disas_ldst_excl(s, insn);
2862 break;
2863 case 0x18: case 0x1c: /* Load register (literal) */
2864 disas_ld_lit(s, insn);
2865 break;
2866 case 0x28: case 0x29:
2867 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
2868 disas_ldst_pair(s, insn);
2869 break;
2870 case 0x38: case 0x39:
2871 case 0x3c: case 0x3d: /* Load/store register (all forms) */
2872 disas_ldst_reg(s, insn);
2873 break;
2874 case 0x0c: /* AdvSIMD load/store multiple structures */
2875 disas_ldst_multiple_struct(s, insn);
2876 break;
2877 case 0x0d: /* AdvSIMD load/store single structure */
2878 disas_ldst_single_struct(s, insn);
2879 break;
2880 default:
2881 unallocated_encoding(s);
2882 break;
2886 /* C3.4.6 PC-rel. addressing
2887 * 31 30 29 28 24 23 5 4 0
2888 * +----+-------+-----------+-------------------+------+
2889 * | op | immlo | 1 0 0 0 0 | immhi | Rd |
2890 * +----+-------+-----------+-------------------+------+
2892 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
2894 unsigned int page, rd;
2895 uint64_t base;
2896 uint64_t offset;
2898 page = extract32(insn, 31, 1);
2899 /* SignExtend(immhi:immlo) -> offset */
2900 offset = sextract64(insn, 5, 19);
2901 offset = offset << 2 | extract32(insn, 29, 2);
2902 rd = extract32(insn, 0, 5);
2903 base = s->pc - 4;
2905 if (page) {
2906 /* ADRP (page based) */
2907 base &= ~0xfff;
2908 offset <<= 12;
2911 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
2915 * C3.4.1 Add/subtract (immediate)
2917 * 31 30 29 28 24 23 22 21 10 9 5 4 0
2918 * +--+--+--+-----------+-----+-------------+-----+-----+
2919 * |sf|op| S| 1 0 0 0 1 |shift| imm12 | Rn | Rd |
2920 * +--+--+--+-----------+-----+-------------+-----+-----+
2922 * sf: 0 -> 32bit, 1 -> 64bit
2923 * op: 0 -> add , 1 -> sub
2924 * S: 1 -> set flags
2925 * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
2927 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
2929 int rd = extract32(insn, 0, 5);
2930 int rn = extract32(insn, 5, 5);
2931 uint64_t imm = extract32(insn, 10, 12);
2932 int shift = extract32(insn, 22, 2);
2933 bool setflags = extract32(insn, 29, 1);
2934 bool sub_op = extract32(insn, 30, 1);
2935 bool is_64bit = extract32(insn, 31, 1);
2937 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2938 TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
2939 TCGv_i64 tcg_result;
2941 switch (shift) {
2942 case 0x0:
2943 break;
2944 case 0x1:
2945 imm <<= 12;
2946 break;
2947 default:
2948 unallocated_encoding(s);
2949 return;
2952 tcg_result = tcg_temp_new_i64();
2953 if (!setflags) {
2954 if (sub_op) {
2955 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
2956 } else {
2957 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
2959 } else {
2960 TCGv_i64 tcg_imm = tcg_const_i64(imm);
2961 if (sub_op) {
2962 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2963 } else {
2964 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2966 tcg_temp_free_i64(tcg_imm);
2969 if (is_64bit) {
2970 tcg_gen_mov_i64(tcg_rd, tcg_result);
2971 } else {
2972 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
2975 tcg_temp_free_i64(tcg_result);
2978 /* The input should be a value in the bottom e bits (with higher
2979 * bits zero); returns that value replicated into every element
2980 * of size e in a 64 bit integer.
2982 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
2984 assert(e != 0);
2985 while (e < 64) {
2986 mask |= mask << e;
2987 e *= 2;
2989 return mask;
2992 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
2993 static inline uint64_t bitmask64(unsigned int length)
2995 assert(length > 0 && length <= 64);
2996 return ~0ULL >> (64 - length);
2999 /* Simplified variant of pseudocode DecodeBitMasks() for the case where we
3000 * only require the wmask. Returns false if the imms/immr/immn are a reserved
3001 * value (ie should cause a guest UNDEF exception), and true if they are
3002 * valid, in which case the decoded bit pattern is written to result.
3004 static bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
3005 unsigned int imms, unsigned int immr)
3007 uint64_t mask;
3008 unsigned e, levels, s, r;
3009 int len;
3011 assert(immn < 2 && imms < 64 && immr < 64);
3013 /* The bit patterns we create here are 64 bit patterns which
3014 * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
3015 * 64 bits each. Each element contains the same value: a run
3016 * of between 1 and e-1 non-zero bits, rotated within the
3017 * element by between 0 and e-1 bits.
3019 * The element size and run length are encoded into immn (1 bit)
3020 * and imms (6 bits) as follows:
3021 * 64 bit elements: immn = 1, imms = <length of run - 1>
3022 * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
3023 * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
3024 * 8 bit elements: immn = 0, imms = 110 : <length of run - 1>
3025 * 4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
3026 * 2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
3027 * Notice that immn = 0, imms = 11111x is the only combination
3028 * not covered by one of the above options; this is reserved.
3029 * Further, <length of run - 1> all-ones is a reserved pattern.
3031 * In all cases the rotation is by immr % e (and immr is 6 bits).
3034 /* First determine the element size */
3035 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
3036 if (len < 1) {
3037 /* This is the immn == 0, imms == 0x11111x case */
3038 return false;
3040 e = 1 << len;
3042 levels = e - 1;
3043 s = imms & levels;
3044 r = immr & levels;
3046 if (s == levels) {
3047 /* <length of run - 1> mustn't be all-ones. */
3048 return false;
3051 /* Create the value of one element: s+1 set bits rotated
3052 * by r within the element (which is e bits wide)...
3054 mask = bitmask64(s + 1);
3055 if (r) {
3056 mask = (mask >> r) | (mask << (e - r));
3057 mask &= bitmask64(e);
3059 /* ...then replicate the element over the whole 64 bit value */
3060 mask = bitfield_replicate(mask, e);
3061 *result = mask;
3062 return true;
3065 /* C3.4.4 Logical (immediate)
3066 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
3067 * +----+-----+-------------+---+------+------+------+------+
3068 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms | Rn | Rd |
3069 * +----+-----+-------------+---+------+------+------+------+
3071 static void disas_logic_imm(DisasContext *s, uint32_t insn)
3073 unsigned int sf, opc, is_n, immr, imms, rn, rd;
3074 TCGv_i64 tcg_rd, tcg_rn;
3075 uint64_t wmask;
3076 bool is_and = false;
3078 sf = extract32(insn, 31, 1);
3079 opc = extract32(insn, 29, 2);
3080 is_n = extract32(insn, 22, 1);
3081 immr = extract32(insn, 16, 6);
3082 imms = extract32(insn, 10, 6);
3083 rn = extract32(insn, 5, 5);
3084 rd = extract32(insn, 0, 5);
3086 if (!sf && is_n) {
3087 unallocated_encoding(s);
3088 return;
3091 if (opc == 0x3) { /* ANDS */
3092 tcg_rd = cpu_reg(s, rd);
3093 } else {
3094 tcg_rd = cpu_reg_sp(s, rd);
3096 tcg_rn = cpu_reg(s, rn);
3098 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
3099 /* some immediate field values are reserved */
3100 unallocated_encoding(s);
3101 return;
3104 if (!sf) {
3105 wmask &= 0xffffffff;
3108 switch (opc) {
3109 case 0x3: /* ANDS */
3110 case 0x0: /* AND */
3111 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
3112 is_and = true;
3113 break;
3114 case 0x1: /* ORR */
3115 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
3116 break;
3117 case 0x2: /* EOR */
3118 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
3119 break;
3120 default:
3121 assert(FALSE); /* must handle all above */
3122 break;
3125 if (!sf && !is_and) {
3126 /* zero extend final result; we know we can skip this for AND
3127 * since the immediate had the high 32 bits clear.
3129 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3132 if (opc == 3) { /* ANDS */
3133 gen_logic_CC(sf, tcg_rd);
3138 * C3.4.5 Move wide (immediate)
3140 * 31 30 29 28 23 22 21 20 5 4 0
3141 * +--+-----+-------------+-----+----------------+------+
3142 * |sf| opc | 1 0 0 1 0 1 | hw | imm16 | Rd |
3143 * +--+-----+-------------+-----+----------------+------+
3145 * sf: 0 -> 32 bit, 1 -> 64 bit
3146 * opc: 00 -> N, 10 -> Z, 11 -> K
3147 * hw: shift/16 (0,16, and sf only 32, 48)
3149 static void disas_movw_imm(DisasContext *s, uint32_t insn)
3151 int rd = extract32(insn, 0, 5);
3152 uint64_t imm = extract32(insn, 5, 16);
3153 int sf = extract32(insn, 31, 1);
3154 int opc = extract32(insn, 29, 2);
3155 int pos = extract32(insn, 21, 2) << 4;
3156 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3157 TCGv_i64 tcg_imm;
3159 if (!sf && (pos >= 32)) {
3160 unallocated_encoding(s);
3161 return;
3164 switch (opc) {
3165 case 0: /* MOVN */
3166 case 2: /* MOVZ */
3167 imm <<= pos;
3168 if (opc == 0) {
3169 imm = ~imm;
3171 if (!sf) {
3172 imm &= 0xffffffffu;
3174 tcg_gen_movi_i64(tcg_rd, imm);
3175 break;
3176 case 3: /* MOVK */
3177 tcg_imm = tcg_const_i64(imm);
3178 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
3179 tcg_temp_free_i64(tcg_imm);
3180 if (!sf) {
3181 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3183 break;
3184 default:
3185 unallocated_encoding(s);
3186 break;
3190 /* C3.4.2 Bitfield
3191 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
3192 * +----+-----+-------------+---+------+------+------+------+
3193 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms | Rn | Rd |
3194 * +----+-----+-------------+---+------+------+------+------+
3196 static void disas_bitfield(DisasContext *s, uint32_t insn)
3198 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
3199 TCGv_i64 tcg_rd, tcg_tmp;
3201 sf = extract32(insn, 31, 1);
3202 opc = extract32(insn, 29, 2);
3203 n = extract32(insn, 22, 1);
3204 ri = extract32(insn, 16, 6);
3205 si = extract32(insn, 10, 6);
3206 rn = extract32(insn, 5, 5);
3207 rd = extract32(insn, 0, 5);
3208 bitsize = sf ? 64 : 32;
3210 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
3211 unallocated_encoding(s);
3212 return;
3215 tcg_rd = cpu_reg(s, rd);
3217 /* Suppress the zero-extend for !sf. Since RI and SI are constrained
3218 to be smaller than bitsize, we'll never reference data outside the
3219 low 32-bits anyway. */
3220 tcg_tmp = read_cpu_reg(s, rn, 1);
3222 /* Recognize the common aliases. */
3223 if (opc == 0) { /* SBFM */
3224 if (ri == 0) {
3225 if (si == 7) { /* SXTB */
3226 tcg_gen_ext8s_i64(tcg_rd, tcg_tmp);
3227 goto done;
3228 } else if (si == 15) { /* SXTH */
3229 tcg_gen_ext16s_i64(tcg_rd, tcg_tmp);
3230 goto done;
3231 } else if (si == 31) { /* SXTW */
3232 tcg_gen_ext32s_i64(tcg_rd, tcg_tmp);
3233 goto done;
3236 if (si == 63 || (si == 31 && ri <= si)) { /* ASR */
3237 if (si == 31) {
3238 tcg_gen_ext32s_i64(tcg_tmp, tcg_tmp);
3240 tcg_gen_sari_i64(tcg_rd, tcg_tmp, ri);
3241 goto done;
3243 } else if (opc == 2) { /* UBFM */
3244 if (ri == 0) { /* UXTB, UXTH, plus non-canonical AND */
3245 tcg_gen_andi_i64(tcg_rd, tcg_tmp, bitmask64(si + 1));
3246 return;
3248 if (si == 63 || (si == 31 && ri <= si)) { /* LSR */
3249 if (si == 31) {
3250 tcg_gen_ext32u_i64(tcg_tmp, tcg_tmp);
3252 tcg_gen_shri_i64(tcg_rd, tcg_tmp, ri);
3253 return;
3255 if (si + 1 == ri && si != bitsize - 1) { /* LSL */
3256 int shift = bitsize - 1 - si;
3257 tcg_gen_shli_i64(tcg_rd, tcg_tmp, shift);
3258 goto done;
3262 if (opc != 1) { /* SBFM or UBFM */
3263 tcg_gen_movi_i64(tcg_rd, 0);
3266 /* do the bit move operation */
3267 if (si >= ri) {
3268 /* Wd<s-r:0> = Wn<s:r> */
3269 tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
3270 pos = 0;
3271 len = (si - ri) + 1;
3272 } else {
3273 /* Wd<32+s-r,32-r> = Wn<s:0> */
3274 pos = bitsize - ri;
3275 len = si + 1;
3278 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
3280 if (opc == 0) { /* SBFM - sign extend the destination field */
3281 tcg_gen_shli_i64(tcg_rd, tcg_rd, 64 - (pos + len));
3282 tcg_gen_sari_i64(tcg_rd, tcg_rd, 64 - (pos + len));
3285 done:
3286 if (!sf) { /* zero extend final result */
3287 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3291 /* C3.4.3 Extract
3292 * 31 30 29 28 23 22 21 20 16 15 10 9 5 4 0
3293 * +----+------+-------------+---+----+------+--------+------+------+
3294 * | sf | op21 | 1 0 0 1 1 1 | N | o0 | Rm | imms | Rn | Rd |
3295 * +----+------+-------------+---+----+------+--------+------+------+
3297 static void disas_extract(DisasContext *s, uint32_t insn)
3299 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
3301 sf = extract32(insn, 31, 1);
3302 n = extract32(insn, 22, 1);
3303 rm = extract32(insn, 16, 5);
3304 imm = extract32(insn, 10, 6);
3305 rn = extract32(insn, 5, 5);
3306 rd = extract32(insn, 0, 5);
3307 op21 = extract32(insn, 29, 2);
3308 op0 = extract32(insn, 21, 1);
3309 bitsize = sf ? 64 : 32;
3311 if (sf != n || op21 || op0 || imm >= bitsize) {
3312 unallocated_encoding(s);
3313 } else {
3314 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
3316 tcg_rd = cpu_reg(s, rd);
3318 if (unlikely(imm == 0)) {
3319 /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
3320 * so an extract from bit 0 is a special case.
3322 if (sf) {
3323 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
3324 } else {
3325 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
3327 } else if (rm == rn) { /* ROR */
3328 tcg_rm = cpu_reg(s, rm);
3329 if (sf) {
3330 tcg_gen_rotri_i64(tcg_rd, tcg_rm, imm);
3331 } else {
3332 TCGv_i32 tmp = tcg_temp_new_i32();
3333 tcg_gen_extrl_i64_i32(tmp, tcg_rm);
3334 tcg_gen_rotri_i32(tmp, tmp, imm);
3335 tcg_gen_extu_i32_i64(tcg_rd, tmp);
3336 tcg_temp_free_i32(tmp);
3338 } else {
3339 tcg_rm = read_cpu_reg(s, rm, sf);
3340 tcg_rn = read_cpu_reg(s, rn, sf);
3341 tcg_gen_shri_i64(tcg_rm, tcg_rm, imm);
3342 tcg_gen_shli_i64(tcg_rn, tcg_rn, bitsize - imm);
3343 tcg_gen_or_i64(tcg_rd, tcg_rm, tcg_rn);
3344 if (!sf) {
3345 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3351 /* C3.4 Data processing - immediate */
3352 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
3354 switch (extract32(insn, 23, 6)) {
3355 case 0x20: case 0x21: /* PC-rel. addressing */
3356 disas_pc_rel_adr(s, insn);
3357 break;
3358 case 0x22: case 0x23: /* Add/subtract (immediate) */
3359 disas_add_sub_imm(s, insn);
3360 break;
3361 case 0x24: /* Logical (immediate) */
3362 disas_logic_imm(s, insn);
3363 break;
3364 case 0x25: /* Move wide (immediate) */
3365 disas_movw_imm(s, insn);
3366 break;
3367 case 0x26: /* Bitfield */
3368 disas_bitfield(s, insn);
3369 break;
3370 case 0x27: /* Extract */
3371 disas_extract(s, insn);
3372 break;
3373 default:
3374 unallocated_encoding(s);
3375 break;
3379 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
3380 * Note that it is the caller's responsibility to ensure that the
3381 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
3382 * mandated semantics for out of range shifts.
3384 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
3385 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
3387 switch (shift_type) {
3388 case A64_SHIFT_TYPE_LSL:
3389 tcg_gen_shl_i64(dst, src, shift_amount);
3390 break;
3391 case A64_SHIFT_TYPE_LSR:
3392 tcg_gen_shr_i64(dst, src, shift_amount);
3393 break;
3394 case A64_SHIFT_TYPE_ASR:
3395 if (!sf) {
3396 tcg_gen_ext32s_i64(dst, src);
3398 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
3399 break;
3400 case A64_SHIFT_TYPE_ROR:
3401 if (sf) {
3402 tcg_gen_rotr_i64(dst, src, shift_amount);
3403 } else {
3404 TCGv_i32 t0, t1;
3405 t0 = tcg_temp_new_i32();
3406 t1 = tcg_temp_new_i32();
3407 tcg_gen_extrl_i64_i32(t0, src);
3408 tcg_gen_extrl_i64_i32(t1, shift_amount);
3409 tcg_gen_rotr_i32(t0, t0, t1);
3410 tcg_gen_extu_i32_i64(dst, t0);
3411 tcg_temp_free_i32(t0);
3412 tcg_temp_free_i32(t1);
3414 break;
3415 default:
3416 assert(FALSE); /* all shift types should be handled */
3417 break;
3420 if (!sf) { /* zero extend final result */
3421 tcg_gen_ext32u_i64(dst, dst);
3425 /* Shift a TCGv src by immediate, put result in dst.
3426 * The shift amount must be in range (this should always be true as the
3427 * relevant instructions will UNDEF on bad shift immediates).
3429 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
3430 enum a64_shift_type shift_type, unsigned int shift_i)
3432 assert(shift_i < (sf ? 64 : 32));
3434 if (shift_i == 0) {
3435 tcg_gen_mov_i64(dst, src);
3436 } else {
3437 TCGv_i64 shift_const;
3439 shift_const = tcg_const_i64(shift_i);
3440 shift_reg(dst, src, sf, shift_type, shift_const);
3441 tcg_temp_free_i64(shift_const);
3445 /* C3.5.10 Logical (shifted register)
3446 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
3447 * +----+-----+-----------+-------+---+------+--------+------+------+
3448 * | sf | opc | 0 1 0 1 0 | shift | N | Rm | imm6 | Rn | Rd |
3449 * +----+-----+-----------+-------+---+------+--------+------+------+
3451 static void disas_logic_reg(DisasContext *s, uint32_t insn)
3453 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
3454 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
3456 sf = extract32(insn, 31, 1);
3457 opc = extract32(insn, 29, 2);
3458 shift_type = extract32(insn, 22, 2);
3459 invert = extract32(insn, 21, 1);
3460 rm = extract32(insn, 16, 5);
3461 shift_amount = extract32(insn, 10, 6);
3462 rn = extract32(insn, 5, 5);
3463 rd = extract32(insn, 0, 5);
3465 if (!sf && (shift_amount & (1 << 5))) {
3466 unallocated_encoding(s);
3467 return;
3470 tcg_rd = cpu_reg(s, rd);
3472 if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
3473 /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
3474 * register-register MOV and MVN, so it is worth special casing.
3476 tcg_rm = cpu_reg(s, rm);
3477 if (invert) {
3478 tcg_gen_not_i64(tcg_rd, tcg_rm);
3479 if (!sf) {
3480 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3482 } else {
3483 if (sf) {
3484 tcg_gen_mov_i64(tcg_rd, tcg_rm);
3485 } else {
3486 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
3489 return;
3492 tcg_rm = read_cpu_reg(s, rm, sf);
3494 if (shift_amount) {
3495 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
3498 tcg_rn = cpu_reg(s, rn);
3500 switch (opc | (invert << 2)) {
3501 case 0: /* AND */
3502 case 3: /* ANDS */
3503 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
3504 break;
3505 case 1: /* ORR */
3506 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
3507 break;
3508 case 2: /* EOR */
3509 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
3510 break;
3511 case 4: /* BIC */
3512 case 7: /* BICS */
3513 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
3514 break;
3515 case 5: /* ORN */
3516 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
3517 break;
3518 case 6: /* EON */
3519 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
3520 break;
3521 default:
3522 assert(FALSE);
3523 break;
3526 if (!sf) {
3527 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3530 if (opc == 3) {
3531 gen_logic_CC(sf, tcg_rd);
3536 * C3.5.1 Add/subtract (extended register)
3538 * 31|30|29|28 24|23 22|21|20 16|15 13|12 10|9 5|4 0|
3539 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3540 * |sf|op| S| 0 1 0 1 1 | opt | 1| Rm |option| imm3 | Rn | Rd |
3541 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3543 * sf: 0 -> 32bit, 1 -> 64bit
3544 * op: 0 -> add , 1 -> sub
3545 * S: 1 -> set flags
3546 * opt: 00
3547 * option: extension type (see DecodeRegExtend)
3548 * imm3: optional shift to Rm
3550 * Rd = Rn + LSL(extend(Rm), amount)
3552 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
3554 int rd = extract32(insn, 0, 5);
3555 int rn = extract32(insn, 5, 5);
3556 int imm3 = extract32(insn, 10, 3);
3557 int option = extract32(insn, 13, 3);
3558 int rm = extract32(insn, 16, 5);
3559 bool setflags = extract32(insn, 29, 1);
3560 bool sub_op = extract32(insn, 30, 1);
3561 bool sf = extract32(insn, 31, 1);
3563 TCGv_i64 tcg_rm, tcg_rn; /* temps */
3564 TCGv_i64 tcg_rd;
3565 TCGv_i64 tcg_result;
3567 if (imm3 > 4) {
3568 unallocated_encoding(s);
3569 return;
3572 /* non-flag setting ops may use SP */
3573 if (!setflags) {
3574 tcg_rd = cpu_reg_sp(s, rd);
3575 } else {
3576 tcg_rd = cpu_reg(s, rd);
3578 tcg_rn = read_cpu_reg_sp(s, rn, sf);
3580 tcg_rm = read_cpu_reg(s, rm, sf);
3581 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
3583 tcg_result = tcg_temp_new_i64();
3585 if (!setflags) {
3586 if (sub_op) {
3587 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3588 } else {
3589 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3591 } else {
3592 if (sub_op) {
3593 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3594 } else {
3595 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3599 if (sf) {
3600 tcg_gen_mov_i64(tcg_rd, tcg_result);
3601 } else {
3602 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3605 tcg_temp_free_i64(tcg_result);
3609 * C3.5.2 Add/subtract (shifted register)
3611 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
3612 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3613 * |sf|op| S| 0 1 0 1 1 |shift| 0| Rm | imm6 | Rn | Rd |
3614 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3616 * sf: 0 -> 32bit, 1 -> 64bit
3617 * op: 0 -> add , 1 -> sub
3618 * S: 1 -> set flags
3619 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
3620 * imm6: Shift amount to apply to Rm before the add/sub
3622 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
3624 int rd = extract32(insn, 0, 5);
3625 int rn = extract32(insn, 5, 5);
3626 int imm6 = extract32(insn, 10, 6);
3627 int rm = extract32(insn, 16, 5);
3628 int shift_type = extract32(insn, 22, 2);
3629 bool setflags = extract32(insn, 29, 1);
3630 bool sub_op = extract32(insn, 30, 1);
3631 bool sf = extract32(insn, 31, 1);
3633 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3634 TCGv_i64 tcg_rn, tcg_rm;
3635 TCGv_i64 tcg_result;
3637 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
3638 unallocated_encoding(s);
3639 return;
3642 tcg_rn = read_cpu_reg(s, rn, sf);
3643 tcg_rm = read_cpu_reg(s, rm, sf);
3645 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
3647 tcg_result = tcg_temp_new_i64();
3649 if (!setflags) {
3650 if (sub_op) {
3651 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3652 } else {
3653 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3655 } else {
3656 if (sub_op) {
3657 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3658 } else {
3659 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3663 if (sf) {
3664 tcg_gen_mov_i64(tcg_rd, tcg_result);
3665 } else {
3666 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3669 tcg_temp_free_i64(tcg_result);
3672 /* C3.5.9 Data-processing (3 source)
3674 31 30 29 28 24 23 21 20 16 15 14 10 9 5 4 0
3675 +--+------+-----------+------+------+----+------+------+------+
3676 |sf| op54 | 1 1 0 1 1 | op31 | Rm | o0 | Ra | Rn | Rd |
3677 +--+------+-----------+------+------+----+------+------+------+
3680 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
3682 int rd = extract32(insn, 0, 5);
3683 int rn = extract32(insn, 5, 5);
3684 int ra = extract32(insn, 10, 5);
3685 int rm = extract32(insn, 16, 5);
3686 int op_id = (extract32(insn, 29, 3) << 4) |
3687 (extract32(insn, 21, 3) << 1) |
3688 extract32(insn, 15, 1);
3689 bool sf = extract32(insn, 31, 1);
3690 bool is_sub = extract32(op_id, 0, 1);
3691 bool is_high = extract32(op_id, 2, 1);
3692 bool is_signed = false;
3693 TCGv_i64 tcg_op1;
3694 TCGv_i64 tcg_op2;
3695 TCGv_i64 tcg_tmp;
3697 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
3698 switch (op_id) {
3699 case 0x42: /* SMADDL */
3700 case 0x43: /* SMSUBL */
3701 case 0x44: /* SMULH */
3702 is_signed = true;
3703 break;
3704 case 0x0: /* MADD (32bit) */
3705 case 0x1: /* MSUB (32bit) */
3706 case 0x40: /* MADD (64bit) */
3707 case 0x41: /* MSUB (64bit) */
3708 case 0x4a: /* UMADDL */
3709 case 0x4b: /* UMSUBL */
3710 case 0x4c: /* UMULH */
3711 break;
3712 default:
3713 unallocated_encoding(s);
3714 return;
3717 if (is_high) {
3718 TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
3719 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3720 TCGv_i64 tcg_rn = cpu_reg(s, rn);
3721 TCGv_i64 tcg_rm = cpu_reg(s, rm);
3723 if (is_signed) {
3724 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3725 } else {
3726 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3729 tcg_temp_free_i64(low_bits);
3730 return;
3733 tcg_op1 = tcg_temp_new_i64();
3734 tcg_op2 = tcg_temp_new_i64();
3735 tcg_tmp = tcg_temp_new_i64();
3737 if (op_id < 0x42) {
3738 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
3739 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
3740 } else {
3741 if (is_signed) {
3742 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
3743 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
3744 } else {
3745 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
3746 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
3750 if (ra == 31 && !is_sub) {
3751 /* Special-case MADD with rA == XZR; it is the standard MUL alias */
3752 tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
3753 } else {
3754 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
3755 if (is_sub) {
3756 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3757 } else {
3758 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3762 if (!sf) {
3763 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
3766 tcg_temp_free_i64(tcg_op1);
3767 tcg_temp_free_i64(tcg_op2);
3768 tcg_temp_free_i64(tcg_tmp);
3771 /* C3.5.3 - Add/subtract (with carry)
3772 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
3773 * +--+--+--+------------------------+------+---------+------+-----+
3774 * |sf|op| S| 1 1 0 1 0 0 0 0 | rm | opcode2 | Rn | Rd |
3775 * +--+--+--+------------------------+------+---------+------+-----+
3776 * [000000]
3779 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
3781 unsigned int sf, op, setflags, rm, rn, rd;
3782 TCGv_i64 tcg_y, tcg_rn, tcg_rd;
3784 if (extract32(insn, 10, 6) != 0) {
3785 unallocated_encoding(s);
3786 return;
3789 sf = extract32(insn, 31, 1);
3790 op = extract32(insn, 30, 1);
3791 setflags = extract32(insn, 29, 1);
3792 rm = extract32(insn, 16, 5);
3793 rn = extract32(insn, 5, 5);
3794 rd = extract32(insn, 0, 5);
3796 tcg_rd = cpu_reg(s, rd);
3797 tcg_rn = cpu_reg(s, rn);
3799 if (op) {
3800 tcg_y = new_tmp_a64(s);
3801 tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
3802 } else {
3803 tcg_y = cpu_reg(s, rm);
3806 if (setflags) {
3807 gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
3808 } else {
3809 gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
3813 /* C3.5.4 - C3.5.5 Conditional compare (immediate / register)
3814 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
3815 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3816 * |sf|op| S| 1 1 0 1 0 0 1 0 |imm5/rm | cond |i/r |o2| Rn |o3|nzcv |
3817 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3818 * [1] y [0] [0]
3820 static void disas_cc(DisasContext *s, uint32_t insn)
3822 unsigned int sf, op, y, cond, rn, nzcv, is_imm;
3823 TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
3824 TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
3825 DisasCompare c;
3827 if (!extract32(insn, 29, 1)) {
3828 unallocated_encoding(s);
3829 return;
3831 if (insn & (1 << 10 | 1 << 4)) {
3832 unallocated_encoding(s);
3833 return;
3835 sf = extract32(insn, 31, 1);
3836 op = extract32(insn, 30, 1);
3837 is_imm = extract32(insn, 11, 1);
3838 y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
3839 cond = extract32(insn, 12, 4);
3840 rn = extract32(insn, 5, 5);
3841 nzcv = extract32(insn, 0, 4);
3843 /* Set T0 = !COND. */
3844 tcg_t0 = tcg_temp_new_i32();
3845 arm_test_cc(&c, cond);
3846 tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
3847 arm_free_cc(&c);
3849 /* Load the arguments for the new comparison. */
3850 if (is_imm) {
3851 tcg_y = new_tmp_a64(s);
3852 tcg_gen_movi_i64(tcg_y, y);
3853 } else {
3854 tcg_y = cpu_reg(s, y);
3856 tcg_rn = cpu_reg(s, rn);
3858 /* Set the flags for the new comparison. */
3859 tcg_tmp = tcg_temp_new_i64();
3860 if (op) {
3861 gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3862 } else {
3863 gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3865 tcg_temp_free_i64(tcg_tmp);
3867 /* If COND was false, force the flags to #nzcv. Compute two masks
3868 * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
3869 * For tcg hosts that support ANDC, we can make do with just T1.
3870 * In either case, allow the tcg optimizer to delete any unused mask.
3872 tcg_t1 = tcg_temp_new_i32();
3873 tcg_t2 = tcg_temp_new_i32();
3874 tcg_gen_neg_i32(tcg_t1, tcg_t0);
3875 tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
3877 if (nzcv & 8) { /* N */
3878 tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
3879 } else {
3880 if (TCG_TARGET_HAS_andc_i32) {
3881 tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
3882 } else {
3883 tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
3886 if (nzcv & 4) { /* Z */
3887 if (TCG_TARGET_HAS_andc_i32) {
3888 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
3889 } else {
3890 tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
3892 } else {
3893 tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
3895 if (nzcv & 2) { /* C */
3896 tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
3897 } else {
3898 if (TCG_TARGET_HAS_andc_i32) {
3899 tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
3900 } else {
3901 tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
3904 if (nzcv & 1) { /* V */
3905 tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
3906 } else {
3907 if (TCG_TARGET_HAS_andc_i32) {
3908 tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
3909 } else {
3910 tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
3913 tcg_temp_free_i32(tcg_t0);
3914 tcg_temp_free_i32(tcg_t1);
3915 tcg_temp_free_i32(tcg_t2);
3918 /* C3.5.6 Conditional select
3919 * 31 30 29 28 21 20 16 15 12 11 10 9 5 4 0
3920 * +----+----+---+-----------------+------+------+-----+------+------+
3921 * | sf | op | S | 1 1 0 1 0 1 0 0 | Rm | cond | op2 | Rn | Rd |
3922 * +----+----+---+-----------------+------+------+-----+------+------+
3924 static void disas_cond_select(DisasContext *s, uint32_t insn)
3926 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
3927 TCGv_i64 tcg_rd, zero;
3928 DisasCompare64 c;
3930 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
3931 /* S == 1 or op2<1> == 1 */
3932 unallocated_encoding(s);
3933 return;
3935 sf = extract32(insn, 31, 1);
3936 else_inv = extract32(insn, 30, 1);
3937 rm = extract32(insn, 16, 5);
3938 cond = extract32(insn, 12, 4);
3939 else_inc = extract32(insn, 10, 1);
3940 rn = extract32(insn, 5, 5);
3941 rd = extract32(insn, 0, 5);
3943 tcg_rd = cpu_reg(s, rd);
3945 a64_test_cc(&c, cond);
3946 zero = tcg_const_i64(0);
3948 if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
3949 /* CSET & CSETM. */
3950 tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
3951 if (else_inv) {
3952 tcg_gen_neg_i64(tcg_rd, tcg_rd);
3954 } else {
3955 TCGv_i64 t_true = cpu_reg(s, rn);
3956 TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
3957 if (else_inv && else_inc) {
3958 tcg_gen_neg_i64(t_false, t_false);
3959 } else if (else_inv) {
3960 tcg_gen_not_i64(t_false, t_false);
3961 } else if (else_inc) {
3962 tcg_gen_addi_i64(t_false, t_false, 1);
3964 tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
3967 tcg_temp_free_i64(zero);
3968 a64_free_cc(&c);
3970 if (!sf) {
3971 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3975 static void handle_clz(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_clz64(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_clz(tcg_tmp32, tcg_tmp32);
3988 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3989 tcg_temp_free_i32(tcg_tmp32);
3993 static void handle_cls(DisasContext *s, unsigned int sf,
3994 unsigned int rn, unsigned int rd)
3996 TCGv_i64 tcg_rd, tcg_rn;
3997 tcg_rd = cpu_reg(s, rd);
3998 tcg_rn = cpu_reg(s, rn);
4000 if (sf) {
4001 gen_helper_cls64(tcg_rd, tcg_rn);
4002 } else {
4003 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4004 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4005 gen_helper_cls32(tcg_tmp32, tcg_tmp32);
4006 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4007 tcg_temp_free_i32(tcg_tmp32);
4011 static void handle_rbit(DisasContext *s, unsigned int sf,
4012 unsigned int rn, unsigned int rd)
4014 TCGv_i64 tcg_rd, tcg_rn;
4015 tcg_rd = cpu_reg(s, rd);
4016 tcg_rn = cpu_reg(s, rn);
4018 if (sf) {
4019 gen_helper_rbit64(tcg_rd, tcg_rn);
4020 } else {
4021 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4022 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4023 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
4024 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4025 tcg_temp_free_i32(tcg_tmp32);
4029 /* C5.6.149 REV with sf==1, opcode==3 ("REV64") */
4030 static void handle_rev64(DisasContext *s, unsigned int sf,
4031 unsigned int rn, unsigned int rd)
4033 if (!sf) {
4034 unallocated_encoding(s);
4035 return;
4037 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
4040 /* C5.6.149 REV with sf==0, opcode==2
4041 * C5.6.151 REV32 (sf==1, opcode==2)
4043 static void handle_rev32(DisasContext *s, unsigned int sf,
4044 unsigned int rn, unsigned int rd)
4046 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4048 if (sf) {
4049 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4050 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4052 /* bswap32_i64 requires zero high word */
4053 tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
4054 tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
4055 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
4056 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
4057 tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
4059 tcg_temp_free_i64(tcg_tmp);
4060 } else {
4061 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
4062 tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
4066 /* C5.6.150 REV16 (opcode==1) */
4067 static void handle_rev16(DisasContext *s, unsigned int sf,
4068 unsigned int rn, unsigned int rd)
4070 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4071 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4072 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4074 tcg_gen_andi_i64(tcg_tmp, tcg_rn, 0xffff);
4075 tcg_gen_bswap16_i64(tcg_rd, tcg_tmp);
4077 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 16);
4078 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
4079 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
4080 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 16, 16);
4082 if (sf) {
4083 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
4084 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
4085 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
4086 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 32, 16);
4088 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 48);
4089 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
4090 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 48, 16);
4093 tcg_temp_free_i64(tcg_tmp);
4096 /* C3.5.7 Data-processing (1 source)
4097 * 31 30 29 28 21 20 16 15 10 9 5 4 0
4098 * +----+---+---+-----------------+---------+--------+------+------+
4099 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode | Rn | Rd |
4100 * +----+---+---+-----------------+---------+--------+------+------+
4102 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
4104 unsigned int sf, opcode, rn, rd;
4106 if (extract32(insn, 29, 1) || extract32(insn, 16, 5)) {
4107 unallocated_encoding(s);
4108 return;
4111 sf = extract32(insn, 31, 1);
4112 opcode = extract32(insn, 10, 6);
4113 rn = extract32(insn, 5, 5);
4114 rd = extract32(insn, 0, 5);
4116 switch (opcode) {
4117 case 0: /* RBIT */
4118 handle_rbit(s, sf, rn, rd);
4119 break;
4120 case 1: /* REV16 */
4121 handle_rev16(s, sf, rn, rd);
4122 break;
4123 case 2: /* REV32 */
4124 handle_rev32(s, sf, rn, rd);
4125 break;
4126 case 3: /* REV64 */
4127 handle_rev64(s, sf, rn, rd);
4128 break;
4129 case 4: /* CLZ */
4130 handle_clz(s, sf, rn, rd);
4131 break;
4132 case 5: /* CLS */
4133 handle_cls(s, sf, rn, rd);
4134 break;
4138 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
4139 unsigned int rm, unsigned int rn, unsigned int rd)
4141 TCGv_i64 tcg_n, tcg_m, tcg_rd;
4142 tcg_rd = cpu_reg(s, rd);
4144 if (!sf && is_signed) {
4145 tcg_n = new_tmp_a64(s);
4146 tcg_m = new_tmp_a64(s);
4147 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
4148 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
4149 } else {
4150 tcg_n = read_cpu_reg(s, rn, sf);
4151 tcg_m = read_cpu_reg(s, rm, sf);
4154 if (is_signed) {
4155 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
4156 } else {
4157 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
4160 if (!sf) { /* zero extend final result */
4161 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4165 /* C5.6.115 LSLV, C5.6.118 LSRV, C5.6.17 ASRV, C5.6.154 RORV */
4166 static void handle_shift_reg(DisasContext *s,
4167 enum a64_shift_type shift_type, unsigned int sf,
4168 unsigned int rm, unsigned int rn, unsigned int rd)
4170 TCGv_i64 tcg_shift = tcg_temp_new_i64();
4171 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4172 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4174 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
4175 shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
4176 tcg_temp_free_i64(tcg_shift);
4179 /* CRC32[BHWX], CRC32C[BHWX] */
4180 static void handle_crc32(DisasContext *s,
4181 unsigned int sf, unsigned int sz, bool crc32c,
4182 unsigned int rm, unsigned int rn, unsigned int rd)
4184 TCGv_i64 tcg_acc, tcg_val;
4185 TCGv_i32 tcg_bytes;
4187 if (!arm_dc_feature(s, ARM_FEATURE_CRC)
4188 || (sf == 1 && sz != 3)
4189 || (sf == 0 && sz == 3)) {
4190 unallocated_encoding(s);
4191 return;
4194 if (sz == 3) {
4195 tcg_val = cpu_reg(s, rm);
4196 } else {
4197 uint64_t mask;
4198 switch (sz) {
4199 case 0:
4200 mask = 0xFF;
4201 break;
4202 case 1:
4203 mask = 0xFFFF;
4204 break;
4205 case 2:
4206 mask = 0xFFFFFFFF;
4207 break;
4208 default:
4209 g_assert_not_reached();
4211 tcg_val = new_tmp_a64(s);
4212 tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
4215 tcg_acc = cpu_reg(s, rn);
4216 tcg_bytes = tcg_const_i32(1 << sz);
4218 if (crc32c) {
4219 gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
4220 } else {
4221 gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
4224 tcg_temp_free_i32(tcg_bytes);
4227 /* C3.5.8 Data-processing (2 source)
4228 * 31 30 29 28 21 20 16 15 10 9 5 4 0
4229 * +----+---+---+-----------------+------+--------+------+------+
4230 * | sf | 0 | S | 1 1 0 1 0 1 1 0 | Rm | opcode | Rn | Rd |
4231 * +----+---+---+-----------------+------+--------+------+------+
4233 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
4235 unsigned int sf, rm, opcode, rn, rd;
4236 sf = extract32(insn, 31, 1);
4237 rm = extract32(insn, 16, 5);
4238 opcode = extract32(insn, 10, 6);
4239 rn = extract32(insn, 5, 5);
4240 rd = extract32(insn, 0, 5);
4242 if (extract32(insn, 29, 1)) {
4243 unallocated_encoding(s);
4244 return;
4247 switch (opcode) {
4248 case 2: /* UDIV */
4249 handle_div(s, false, sf, rm, rn, rd);
4250 break;
4251 case 3: /* SDIV */
4252 handle_div(s, true, sf, rm, rn, rd);
4253 break;
4254 case 8: /* LSLV */
4255 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
4256 break;
4257 case 9: /* LSRV */
4258 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
4259 break;
4260 case 10: /* ASRV */
4261 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
4262 break;
4263 case 11: /* RORV */
4264 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
4265 break;
4266 case 16:
4267 case 17:
4268 case 18:
4269 case 19:
4270 case 20:
4271 case 21:
4272 case 22:
4273 case 23: /* CRC32 */
4275 int sz = extract32(opcode, 0, 2);
4276 bool crc32c = extract32(opcode, 2, 1);
4277 handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
4278 break;
4280 default:
4281 unallocated_encoding(s);
4282 break;
4286 /* C3.5 Data processing - register */
4287 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
4289 switch (extract32(insn, 24, 5)) {
4290 case 0x0a: /* Logical (shifted register) */
4291 disas_logic_reg(s, insn);
4292 break;
4293 case 0x0b: /* Add/subtract */
4294 if (insn & (1 << 21)) { /* (extended register) */
4295 disas_add_sub_ext_reg(s, insn);
4296 } else {
4297 disas_add_sub_reg(s, insn);
4299 break;
4300 case 0x1b: /* Data-processing (3 source) */
4301 disas_data_proc_3src(s, insn);
4302 break;
4303 case 0x1a:
4304 switch (extract32(insn, 21, 3)) {
4305 case 0x0: /* Add/subtract (with carry) */
4306 disas_adc_sbc(s, insn);
4307 break;
4308 case 0x2: /* Conditional compare */
4309 disas_cc(s, insn); /* both imm and reg forms */
4310 break;
4311 case 0x4: /* Conditional select */
4312 disas_cond_select(s, insn);
4313 break;
4314 case 0x6: /* Data-processing */
4315 if (insn & (1 << 30)) { /* (1 source) */
4316 disas_data_proc_1src(s, insn);
4317 } else { /* (2 source) */
4318 disas_data_proc_2src(s, insn);
4320 break;
4321 default:
4322 unallocated_encoding(s);
4323 break;
4325 break;
4326 default:
4327 unallocated_encoding(s);
4328 break;
4332 static void handle_fp_compare(DisasContext *s, bool is_double,
4333 unsigned int rn, unsigned int rm,
4334 bool cmp_with_zero, bool signal_all_nans)
4336 TCGv_i64 tcg_flags = tcg_temp_new_i64();
4337 TCGv_ptr fpst = get_fpstatus_ptr();
4339 if (is_double) {
4340 TCGv_i64 tcg_vn, tcg_vm;
4342 tcg_vn = read_fp_dreg(s, rn);
4343 if (cmp_with_zero) {
4344 tcg_vm = tcg_const_i64(0);
4345 } else {
4346 tcg_vm = read_fp_dreg(s, rm);
4348 if (signal_all_nans) {
4349 gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4350 } else {
4351 gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4353 tcg_temp_free_i64(tcg_vn);
4354 tcg_temp_free_i64(tcg_vm);
4355 } else {
4356 TCGv_i32 tcg_vn, tcg_vm;
4358 tcg_vn = read_fp_sreg(s, rn);
4359 if (cmp_with_zero) {
4360 tcg_vm = tcg_const_i32(0);
4361 } else {
4362 tcg_vm = read_fp_sreg(s, rm);
4364 if (signal_all_nans) {
4365 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4366 } else {
4367 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4369 tcg_temp_free_i32(tcg_vn);
4370 tcg_temp_free_i32(tcg_vm);
4373 tcg_temp_free_ptr(fpst);
4375 gen_set_nzcv(tcg_flags);
4377 tcg_temp_free_i64(tcg_flags);
4380 /* C3.6.22 Floating point compare
4381 * 31 30 29 28 24 23 22 21 20 16 15 14 13 10 9 5 4 0
4382 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
4383 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | op | 1 0 0 0 | Rn | op2 |
4384 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
4386 static void disas_fp_compare(DisasContext *s, uint32_t insn)
4388 unsigned int mos, type, rm, op, rn, opc, op2r;
4390 mos = extract32(insn, 29, 3);
4391 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4392 rm = extract32(insn, 16, 5);
4393 op = extract32(insn, 14, 2);
4394 rn = extract32(insn, 5, 5);
4395 opc = extract32(insn, 3, 2);
4396 op2r = extract32(insn, 0, 3);
4398 if (mos || op || op2r || type > 1) {
4399 unallocated_encoding(s);
4400 return;
4403 if (!fp_access_check(s)) {
4404 return;
4407 handle_fp_compare(s, type, rn, rm, opc & 1, opc & 2);
4410 /* C3.6.23 Floating point conditional compare
4411 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
4412 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
4413 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 0 1 | Rn | op | nzcv |
4414 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
4416 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
4418 unsigned int mos, type, rm, cond, rn, op, nzcv;
4419 TCGv_i64 tcg_flags;
4420 TCGLabel *label_continue = NULL;
4422 mos = extract32(insn, 29, 3);
4423 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4424 rm = extract32(insn, 16, 5);
4425 cond = extract32(insn, 12, 4);
4426 rn = extract32(insn, 5, 5);
4427 op = extract32(insn, 4, 1);
4428 nzcv = extract32(insn, 0, 4);
4430 if (mos || type > 1) {
4431 unallocated_encoding(s);
4432 return;
4435 if (!fp_access_check(s)) {
4436 return;
4439 if (cond < 0x0e) { /* not always */
4440 TCGLabel *label_match = gen_new_label();
4441 label_continue = gen_new_label();
4442 arm_gen_test_cc(cond, label_match);
4443 /* nomatch: */
4444 tcg_flags = tcg_const_i64(nzcv << 28);
4445 gen_set_nzcv(tcg_flags);
4446 tcg_temp_free_i64(tcg_flags);
4447 tcg_gen_br(label_continue);
4448 gen_set_label(label_match);
4451 handle_fp_compare(s, type, rn, rm, false, op);
4453 if (cond < 0x0e) {
4454 gen_set_label(label_continue);
4458 /* C3.6.24 Floating point conditional select
4459 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
4460 * +---+---+---+-----------+------+---+------+------+-----+------+------+
4461 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 1 1 | Rn | Rd |
4462 * +---+---+---+-----------+------+---+------+------+-----+------+------+
4464 static void disas_fp_csel(DisasContext *s, uint32_t insn)
4466 unsigned int mos, type, rm, cond, rn, rd;
4467 TCGv_i64 t_true, t_false, t_zero;
4468 DisasCompare64 c;
4470 mos = extract32(insn, 29, 3);
4471 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4472 rm = extract32(insn, 16, 5);
4473 cond = extract32(insn, 12, 4);
4474 rn = extract32(insn, 5, 5);
4475 rd = extract32(insn, 0, 5);
4477 if (mos || type > 1) {
4478 unallocated_encoding(s);
4479 return;
4482 if (!fp_access_check(s)) {
4483 return;
4486 /* Zero extend sreg inputs to 64 bits now. */
4487 t_true = tcg_temp_new_i64();
4488 t_false = tcg_temp_new_i64();
4489 read_vec_element(s, t_true, rn, 0, type ? MO_64 : MO_32);
4490 read_vec_element(s, t_false, rm, 0, type ? MO_64 : MO_32);
4492 a64_test_cc(&c, cond);
4493 t_zero = tcg_const_i64(0);
4494 tcg_gen_movcond_i64(c.cond, t_true, c.value, t_zero, t_true, t_false);
4495 tcg_temp_free_i64(t_zero);
4496 tcg_temp_free_i64(t_false);
4497 a64_free_cc(&c);
4499 /* Note that sregs write back zeros to the high bits,
4500 and we've already done the zero-extension. */
4501 write_fp_dreg(s, rd, t_true);
4502 tcg_temp_free_i64(t_true);
4505 /* C3.6.25 Floating-point data-processing (1 source) - single precision */
4506 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
4508 TCGv_ptr fpst;
4509 TCGv_i32 tcg_op;
4510 TCGv_i32 tcg_res;
4512 fpst = get_fpstatus_ptr();
4513 tcg_op = read_fp_sreg(s, rn);
4514 tcg_res = tcg_temp_new_i32();
4516 switch (opcode) {
4517 case 0x0: /* FMOV */
4518 tcg_gen_mov_i32(tcg_res, tcg_op);
4519 break;
4520 case 0x1: /* FABS */
4521 gen_helper_vfp_abss(tcg_res, tcg_op);
4522 break;
4523 case 0x2: /* FNEG */
4524 gen_helper_vfp_negs(tcg_res, tcg_op);
4525 break;
4526 case 0x3: /* FSQRT */
4527 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
4528 break;
4529 case 0x8: /* FRINTN */
4530 case 0x9: /* FRINTP */
4531 case 0xa: /* FRINTM */
4532 case 0xb: /* FRINTZ */
4533 case 0xc: /* FRINTA */
4535 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4537 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4538 gen_helper_rints(tcg_res, tcg_op, fpst);
4540 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4541 tcg_temp_free_i32(tcg_rmode);
4542 break;
4544 case 0xe: /* FRINTX */
4545 gen_helper_rints_exact(tcg_res, tcg_op, fpst);
4546 break;
4547 case 0xf: /* FRINTI */
4548 gen_helper_rints(tcg_res, tcg_op, fpst);
4549 break;
4550 default:
4551 abort();
4554 write_fp_sreg(s, rd, tcg_res);
4556 tcg_temp_free_ptr(fpst);
4557 tcg_temp_free_i32(tcg_op);
4558 tcg_temp_free_i32(tcg_res);
4561 /* C3.6.25 Floating-point data-processing (1 source) - double precision */
4562 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
4564 TCGv_ptr fpst;
4565 TCGv_i64 tcg_op;
4566 TCGv_i64 tcg_res;
4568 fpst = get_fpstatus_ptr();
4569 tcg_op = read_fp_dreg(s, rn);
4570 tcg_res = tcg_temp_new_i64();
4572 switch (opcode) {
4573 case 0x0: /* FMOV */
4574 tcg_gen_mov_i64(tcg_res, tcg_op);
4575 break;
4576 case 0x1: /* FABS */
4577 gen_helper_vfp_absd(tcg_res, tcg_op);
4578 break;
4579 case 0x2: /* FNEG */
4580 gen_helper_vfp_negd(tcg_res, tcg_op);
4581 break;
4582 case 0x3: /* FSQRT */
4583 gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
4584 break;
4585 case 0x8: /* FRINTN */
4586 case 0x9: /* FRINTP */
4587 case 0xa: /* FRINTM */
4588 case 0xb: /* FRINTZ */
4589 case 0xc: /* FRINTA */
4591 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4593 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4594 gen_helper_rintd(tcg_res, tcg_op, fpst);
4596 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4597 tcg_temp_free_i32(tcg_rmode);
4598 break;
4600 case 0xe: /* FRINTX */
4601 gen_helper_rintd_exact(tcg_res, tcg_op, fpst);
4602 break;
4603 case 0xf: /* FRINTI */
4604 gen_helper_rintd(tcg_res, tcg_op, fpst);
4605 break;
4606 default:
4607 abort();
4610 write_fp_dreg(s, rd, tcg_res);
4612 tcg_temp_free_ptr(fpst);
4613 tcg_temp_free_i64(tcg_op);
4614 tcg_temp_free_i64(tcg_res);
4617 static void handle_fp_fcvt(DisasContext *s, int opcode,
4618 int rd, int rn, int dtype, int ntype)
4620 switch (ntype) {
4621 case 0x0:
4623 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4624 if (dtype == 1) {
4625 /* Single to double */
4626 TCGv_i64 tcg_rd = tcg_temp_new_i64();
4627 gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
4628 write_fp_dreg(s, rd, tcg_rd);
4629 tcg_temp_free_i64(tcg_rd);
4630 } else {
4631 /* Single to half */
4632 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4633 gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, cpu_env);
4634 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4635 write_fp_sreg(s, rd, tcg_rd);
4636 tcg_temp_free_i32(tcg_rd);
4638 tcg_temp_free_i32(tcg_rn);
4639 break;
4641 case 0x1:
4643 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
4644 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4645 if (dtype == 0) {
4646 /* Double to single */
4647 gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
4648 } else {
4649 /* Double to half */
4650 gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, cpu_env);
4651 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4653 write_fp_sreg(s, rd, tcg_rd);
4654 tcg_temp_free_i32(tcg_rd);
4655 tcg_temp_free_i64(tcg_rn);
4656 break;
4658 case 0x3:
4660 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4661 tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
4662 if (dtype == 0) {
4663 /* Half to single */
4664 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4665 gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, cpu_env);
4666 write_fp_sreg(s, rd, tcg_rd);
4667 tcg_temp_free_i32(tcg_rd);
4668 } else {
4669 /* Half to double */
4670 TCGv_i64 tcg_rd = tcg_temp_new_i64();
4671 gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, cpu_env);
4672 write_fp_dreg(s, rd, tcg_rd);
4673 tcg_temp_free_i64(tcg_rd);
4675 tcg_temp_free_i32(tcg_rn);
4676 break;
4678 default:
4679 abort();
4683 /* C3.6.25 Floating point data-processing (1 source)
4684 * 31 30 29 28 24 23 22 21 20 15 14 10 9 5 4 0
4685 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4686 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 | Rn | Rd |
4687 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4689 static void disas_fp_1src(DisasContext *s, uint32_t insn)
4691 int type = extract32(insn, 22, 2);
4692 int opcode = extract32(insn, 15, 6);
4693 int rn = extract32(insn, 5, 5);
4694 int rd = extract32(insn, 0, 5);
4696 switch (opcode) {
4697 case 0x4: case 0x5: case 0x7:
4699 /* FCVT between half, single and double precision */
4700 int dtype = extract32(opcode, 0, 2);
4701 if (type == 2 || dtype == type) {
4702 unallocated_encoding(s);
4703 return;
4705 if (!fp_access_check(s)) {
4706 return;
4709 handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
4710 break;
4712 case 0x0 ... 0x3:
4713 case 0x8 ... 0xc:
4714 case 0xe ... 0xf:
4715 /* 32-to-32 and 64-to-64 ops */
4716 switch (type) {
4717 case 0:
4718 if (!fp_access_check(s)) {
4719 return;
4722 handle_fp_1src_single(s, opcode, rd, rn);
4723 break;
4724 case 1:
4725 if (!fp_access_check(s)) {
4726 return;
4729 handle_fp_1src_double(s, opcode, rd, rn);
4730 break;
4731 default:
4732 unallocated_encoding(s);
4734 break;
4735 default:
4736 unallocated_encoding(s);
4737 break;
4741 /* C3.6.26 Floating-point data-processing (2 source) - single precision */
4742 static void handle_fp_2src_single(DisasContext *s, int opcode,
4743 int rd, int rn, int rm)
4745 TCGv_i32 tcg_op1;
4746 TCGv_i32 tcg_op2;
4747 TCGv_i32 tcg_res;
4748 TCGv_ptr fpst;
4750 tcg_res = tcg_temp_new_i32();
4751 fpst = get_fpstatus_ptr();
4752 tcg_op1 = read_fp_sreg(s, rn);
4753 tcg_op2 = read_fp_sreg(s, rm);
4755 switch (opcode) {
4756 case 0x0: /* FMUL */
4757 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4758 break;
4759 case 0x1: /* FDIV */
4760 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
4761 break;
4762 case 0x2: /* FADD */
4763 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
4764 break;
4765 case 0x3: /* FSUB */
4766 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
4767 break;
4768 case 0x4: /* FMAX */
4769 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
4770 break;
4771 case 0x5: /* FMIN */
4772 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
4773 break;
4774 case 0x6: /* FMAXNM */
4775 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
4776 break;
4777 case 0x7: /* FMINNM */
4778 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
4779 break;
4780 case 0x8: /* FNMUL */
4781 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4782 gen_helper_vfp_negs(tcg_res, tcg_res);
4783 break;
4786 write_fp_sreg(s, rd, tcg_res);
4788 tcg_temp_free_ptr(fpst);
4789 tcg_temp_free_i32(tcg_op1);
4790 tcg_temp_free_i32(tcg_op2);
4791 tcg_temp_free_i32(tcg_res);
4794 /* C3.6.26 Floating-point data-processing (2 source) - double precision */
4795 static void handle_fp_2src_double(DisasContext *s, int opcode,
4796 int rd, int rn, int rm)
4798 TCGv_i64 tcg_op1;
4799 TCGv_i64 tcg_op2;
4800 TCGv_i64 tcg_res;
4801 TCGv_ptr fpst;
4803 tcg_res = tcg_temp_new_i64();
4804 fpst = get_fpstatus_ptr();
4805 tcg_op1 = read_fp_dreg(s, rn);
4806 tcg_op2 = read_fp_dreg(s, rm);
4808 switch (opcode) {
4809 case 0x0: /* FMUL */
4810 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4811 break;
4812 case 0x1: /* FDIV */
4813 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
4814 break;
4815 case 0x2: /* FADD */
4816 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
4817 break;
4818 case 0x3: /* FSUB */
4819 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
4820 break;
4821 case 0x4: /* FMAX */
4822 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
4823 break;
4824 case 0x5: /* FMIN */
4825 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
4826 break;
4827 case 0x6: /* FMAXNM */
4828 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4829 break;
4830 case 0x7: /* FMINNM */
4831 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4832 break;
4833 case 0x8: /* FNMUL */
4834 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4835 gen_helper_vfp_negd(tcg_res, tcg_res);
4836 break;
4839 write_fp_dreg(s, rd, tcg_res);
4841 tcg_temp_free_ptr(fpst);
4842 tcg_temp_free_i64(tcg_op1);
4843 tcg_temp_free_i64(tcg_op2);
4844 tcg_temp_free_i64(tcg_res);
4847 /* C3.6.26 Floating point data-processing (2 source)
4848 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
4849 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4850 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | opcode | 1 0 | Rn | Rd |
4851 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4853 static void disas_fp_2src(DisasContext *s, uint32_t insn)
4855 int type = extract32(insn, 22, 2);
4856 int rd = extract32(insn, 0, 5);
4857 int rn = extract32(insn, 5, 5);
4858 int rm = extract32(insn, 16, 5);
4859 int opcode = extract32(insn, 12, 4);
4861 if (opcode > 8) {
4862 unallocated_encoding(s);
4863 return;
4866 switch (type) {
4867 case 0:
4868 if (!fp_access_check(s)) {
4869 return;
4871 handle_fp_2src_single(s, opcode, rd, rn, rm);
4872 break;
4873 case 1:
4874 if (!fp_access_check(s)) {
4875 return;
4877 handle_fp_2src_double(s, opcode, rd, rn, rm);
4878 break;
4879 default:
4880 unallocated_encoding(s);
4884 /* C3.6.27 Floating-point data-processing (3 source) - single precision */
4885 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
4886 int rd, int rn, int rm, int ra)
4888 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
4889 TCGv_i32 tcg_res = tcg_temp_new_i32();
4890 TCGv_ptr fpst = get_fpstatus_ptr();
4892 tcg_op1 = read_fp_sreg(s, rn);
4893 tcg_op2 = read_fp_sreg(s, rm);
4894 tcg_op3 = read_fp_sreg(s, ra);
4896 /* These are fused multiply-add, and must be done as one
4897 * floating point operation with no rounding between the
4898 * multiplication and addition steps.
4899 * NB that doing the negations here as separate steps is
4900 * correct : an input NaN should come out with its sign bit
4901 * flipped if it is a negated-input.
4903 if (o1 == true) {
4904 gen_helper_vfp_negs(tcg_op3, tcg_op3);
4907 if (o0 != o1) {
4908 gen_helper_vfp_negs(tcg_op1, tcg_op1);
4911 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4913 write_fp_sreg(s, rd, tcg_res);
4915 tcg_temp_free_ptr(fpst);
4916 tcg_temp_free_i32(tcg_op1);
4917 tcg_temp_free_i32(tcg_op2);
4918 tcg_temp_free_i32(tcg_op3);
4919 tcg_temp_free_i32(tcg_res);
4922 /* C3.6.27 Floating-point data-processing (3 source) - double precision */
4923 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
4924 int rd, int rn, int rm, int ra)
4926 TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
4927 TCGv_i64 tcg_res = tcg_temp_new_i64();
4928 TCGv_ptr fpst = get_fpstatus_ptr();
4930 tcg_op1 = read_fp_dreg(s, rn);
4931 tcg_op2 = read_fp_dreg(s, rm);
4932 tcg_op3 = read_fp_dreg(s, ra);
4934 /* These are fused multiply-add, and must be done as one
4935 * floating point operation with no rounding between the
4936 * multiplication and addition steps.
4937 * NB that doing the negations here as separate steps is
4938 * correct : an input NaN should come out with its sign bit
4939 * flipped if it is a negated-input.
4941 if (o1 == true) {
4942 gen_helper_vfp_negd(tcg_op3, tcg_op3);
4945 if (o0 != o1) {
4946 gen_helper_vfp_negd(tcg_op1, tcg_op1);
4949 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4951 write_fp_dreg(s, rd, tcg_res);
4953 tcg_temp_free_ptr(fpst);
4954 tcg_temp_free_i64(tcg_op1);
4955 tcg_temp_free_i64(tcg_op2);
4956 tcg_temp_free_i64(tcg_op3);
4957 tcg_temp_free_i64(tcg_res);
4960 /* C3.6.27 Floating point data-processing (3 source)
4961 * 31 30 29 28 24 23 22 21 20 16 15 14 10 9 5 4 0
4962 * +---+---+---+-----------+------+----+------+----+------+------+------+
4963 * | M | 0 | S | 1 1 1 1 1 | type | o1 | Rm | o0 | Ra | Rn | Rd |
4964 * +---+---+---+-----------+------+----+------+----+------+------+------+
4966 static void disas_fp_3src(DisasContext *s, uint32_t insn)
4968 int type = extract32(insn, 22, 2);
4969 int rd = extract32(insn, 0, 5);
4970 int rn = extract32(insn, 5, 5);
4971 int ra = extract32(insn, 10, 5);
4972 int rm = extract32(insn, 16, 5);
4973 bool o0 = extract32(insn, 15, 1);
4974 bool o1 = extract32(insn, 21, 1);
4976 switch (type) {
4977 case 0:
4978 if (!fp_access_check(s)) {
4979 return;
4981 handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
4982 break;
4983 case 1:
4984 if (!fp_access_check(s)) {
4985 return;
4987 handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
4988 break;
4989 default:
4990 unallocated_encoding(s);
4994 /* C3.6.28 Floating point immediate
4995 * 31 30 29 28 24 23 22 21 20 13 12 10 9 5 4 0
4996 * +---+---+---+-----------+------+---+------------+-------+------+------+
4997 * | M | 0 | S | 1 1 1 1 0 | type | 1 | imm8 | 1 0 0 | imm5 | Rd |
4998 * +---+---+---+-----------+------+---+------------+-------+------+------+
5000 static void disas_fp_imm(DisasContext *s, uint32_t insn)
5002 int rd = extract32(insn, 0, 5);
5003 int imm8 = extract32(insn, 13, 8);
5004 int is_double = extract32(insn, 22, 2);
5005 uint64_t imm;
5006 TCGv_i64 tcg_res;
5008 if (is_double > 1) {
5009 unallocated_encoding(s);
5010 return;
5013 if (!fp_access_check(s)) {
5014 return;
5017 /* The imm8 encodes the sign bit, enough bits to represent
5018 * an exponent in the range 01....1xx to 10....0xx,
5019 * and the most significant 4 bits of the mantissa; see
5020 * VFPExpandImm() in the v8 ARM ARM.
5022 if (is_double) {
5023 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
5024 (extract32(imm8, 6, 1) ? 0x3fc0 : 0x4000) |
5025 extract32(imm8, 0, 6);
5026 imm <<= 48;
5027 } else {
5028 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
5029 (extract32(imm8, 6, 1) ? 0x3e00 : 0x4000) |
5030 (extract32(imm8, 0, 6) << 3);
5031 imm <<= 16;
5034 tcg_res = tcg_const_i64(imm);
5035 write_fp_dreg(s, rd, tcg_res);
5036 tcg_temp_free_i64(tcg_res);
5039 /* Handle floating point <=> fixed point conversions. Note that we can
5040 * also deal with fp <=> integer conversions as a special case (scale == 64)
5041 * OPTME: consider handling that special case specially or at least skipping
5042 * the call to scalbn in the helpers for zero shifts.
5044 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
5045 bool itof, int rmode, int scale, int sf, int type)
5047 bool is_signed = !(opcode & 1);
5048 bool is_double = type;
5049 TCGv_ptr tcg_fpstatus;
5050 TCGv_i32 tcg_shift;
5052 tcg_fpstatus = get_fpstatus_ptr();
5054 tcg_shift = tcg_const_i32(64 - scale);
5056 if (itof) {
5057 TCGv_i64 tcg_int = cpu_reg(s, rn);
5058 if (!sf) {
5059 TCGv_i64 tcg_extend = new_tmp_a64(s);
5061 if (is_signed) {
5062 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
5063 } else {
5064 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
5067 tcg_int = tcg_extend;
5070 if (is_double) {
5071 TCGv_i64 tcg_double = tcg_temp_new_i64();
5072 if (is_signed) {
5073 gen_helper_vfp_sqtod(tcg_double, tcg_int,
5074 tcg_shift, tcg_fpstatus);
5075 } else {
5076 gen_helper_vfp_uqtod(tcg_double, tcg_int,
5077 tcg_shift, tcg_fpstatus);
5079 write_fp_dreg(s, rd, tcg_double);
5080 tcg_temp_free_i64(tcg_double);
5081 } else {
5082 TCGv_i32 tcg_single = tcg_temp_new_i32();
5083 if (is_signed) {
5084 gen_helper_vfp_sqtos(tcg_single, tcg_int,
5085 tcg_shift, tcg_fpstatus);
5086 } else {
5087 gen_helper_vfp_uqtos(tcg_single, tcg_int,
5088 tcg_shift, tcg_fpstatus);
5090 write_fp_sreg(s, rd, tcg_single);
5091 tcg_temp_free_i32(tcg_single);
5093 } else {
5094 TCGv_i64 tcg_int = cpu_reg(s, rd);
5095 TCGv_i32 tcg_rmode;
5097 if (extract32(opcode, 2, 1)) {
5098 /* There are too many rounding modes to all fit into rmode,
5099 * so FCVTA[US] is a special case.
5101 rmode = FPROUNDING_TIEAWAY;
5104 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
5106 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
5108 if (is_double) {
5109 TCGv_i64 tcg_double = read_fp_dreg(s, rn);
5110 if (is_signed) {
5111 if (!sf) {
5112 gen_helper_vfp_tosld(tcg_int, tcg_double,
5113 tcg_shift, tcg_fpstatus);
5114 } else {
5115 gen_helper_vfp_tosqd(tcg_int, tcg_double,
5116 tcg_shift, tcg_fpstatus);
5118 } else {
5119 if (!sf) {
5120 gen_helper_vfp_tould(tcg_int, tcg_double,
5121 tcg_shift, tcg_fpstatus);
5122 } else {
5123 gen_helper_vfp_touqd(tcg_int, tcg_double,
5124 tcg_shift, tcg_fpstatus);
5127 tcg_temp_free_i64(tcg_double);
5128 } else {
5129 TCGv_i32 tcg_single = read_fp_sreg(s, rn);
5130 if (sf) {
5131 if (is_signed) {
5132 gen_helper_vfp_tosqs(tcg_int, tcg_single,
5133 tcg_shift, tcg_fpstatus);
5134 } else {
5135 gen_helper_vfp_touqs(tcg_int, tcg_single,
5136 tcg_shift, tcg_fpstatus);
5138 } else {
5139 TCGv_i32 tcg_dest = tcg_temp_new_i32();
5140 if (is_signed) {
5141 gen_helper_vfp_tosls(tcg_dest, tcg_single,
5142 tcg_shift, tcg_fpstatus);
5143 } else {
5144 gen_helper_vfp_touls(tcg_dest, tcg_single,
5145 tcg_shift, tcg_fpstatus);
5147 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
5148 tcg_temp_free_i32(tcg_dest);
5150 tcg_temp_free_i32(tcg_single);
5153 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
5154 tcg_temp_free_i32(tcg_rmode);
5156 if (!sf) {
5157 tcg_gen_ext32u_i64(tcg_int, tcg_int);
5161 tcg_temp_free_ptr(tcg_fpstatus);
5162 tcg_temp_free_i32(tcg_shift);
5165 /* C3.6.29 Floating point <-> fixed point conversions
5166 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
5167 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
5168 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale | Rn | Rd |
5169 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
5171 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
5173 int rd = extract32(insn, 0, 5);
5174 int rn = extract32(insn, 5, 5);
5175 int scale = extract32(insn, 10, 6);
5176 int opcode = extract32(insn, 16, 3);
5177 int rmode = extract32(insn, 19, 2);
5178 int type = extract32(insn, 22, 2);
5179 bool sbit = extract32(insn, 29, 1);
5180 bool sf = extract32(insn, 31, 1);
5181 bool itof;
5183 if (sbit || (type > 1)
5184 || (!sf && scale < 32)) {
5185 unallocated_encoding(s);
5186 return;
5189 switch ((rmode << 3) | opcode) {
5190 case 0x2: /* SCVTF */
5191 case 0x3: /* UCVTF */
5192 itof = true;
5193 break;
5194 case 0x18: /* FCVTZS */
5195 case 0x19: /* FCVTZU */
5196 itof = false;
5197 break;
5198 default:
5199 unallocated_encoding(s);
5200 return;
5203 if (!fp_access_check(s)) {
5204 return;
5207 handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
5210 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
5212 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
5213 * without conversion.
5216 if (itof) {
5217 TCGv_i64 tcg_rn = cpu_reg(s, rn);
5219 switch (type) {
5220 case 0:
5222 /* 32 bit */
5223 TCGv_i64 tmp = tcg_temp_new_i64();
5224 tcg_gen_ext32u_i64(tmp, tcg_rn);
5225 tcg_gen_st_i64(tmp, cpu_env, fp_reg_offset(s, rd, MO_64));
5226 tcg_gen_movi_i64(tmp, 0);
5227 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(s, rd));
5228 tcg_temp_free_i64(tmp);
5229 break;
5231 case 1:
5233 /* 64 bit */
5234 TCGv_i64 tmp = tcg_const_i64(0);
5235 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_offset(s, rd, MO_64));
5236 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(s, rd));
5237 tcg_temp_free_i64(tmp);
5238 break;
5240 case 2:
5241 /* 64 bit to top half. */
5242 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
5243 break;
5245 } else {
5246 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5248 switch (type) {
5249 case 0:
5250 /* 32 bit */
5251 tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
5252 break;
5253 case 1:
5254 /* 64 bit */
5255 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
5256 break;
5257 case 2:
5258 /* 64 bits from top half */
5259 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
5260 break;
5265 /* C3.6.30 Floating point <-> integer conversions
5266 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
5267 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
5268 * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
5269 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
5271 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
5273 int rd = extract32(insn, 0, 5);
5274 int rn = extract32(insn, 5, 5);
5275 int opcode = extract32(insn, 16, 3);
5276 int rmode = extract32(insn, 19, 2);
5277 int type = extract32(insn, 22, 2);
5278 bool sbit = extract32(insn, 29, 1);
5279 bool sf = extract32(insn, 31, 1);
5281 if (sbit) {
5282 unallocated_encoding(s);
5283 return;
5286 if (opcode > 5) {
5287 /* FMOV */
5288 bool itof = opcode & 1;
5290 if (rmode >= 2) {
5291 unallocated_encoding(s);
5292 return;
5295 switch (sf << 3 | type << 1 | rmode) {
5296 case 0x0: /* 32 bit */
5297 case 0xa: /* 64 bit */
5298 case 0xd: /* 64 bit to top half of quad */
5299 break;
5300 default:
5301 /* all other sf/type/rmode combinations are invalid */
5302 unallocated_encoding(s);
5303 break;
5306 if (!fp_access_check(s)) {
5307 return;
5309 handle_fmov(s, rd, rn, type, itof);
5310 } else {
5311 /* actual FP conversions */
5312 bool itof = extract32(opcode, 1, 1);
5314 if (type > 1 || (rmode != 0 && opcode > 1)) {
5315 unallocated_encoding(s);
5316 return;
5319 if (!fp_access_check(s)) {
5320 return;
5322 handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
5326 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
5327 * 31 30 29 28 25 24 0
5328 * +---+---+---+---------+-----------------------------+
5329 * | | 0 | | 1 1 1 1 | |
5330 * +---+---+---+---------+-----------------------------+
5332 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
5334 if (extract32(insn, 24, 1)) {
5335 /* Floating point data-processing (3 source) */
5336 disas_fp_3src(s, insn);
5337 } else if (extract32(insn, 21, 1) == 0) {
5338 /* Floating point to fixed point conversions */
5339 disas_fp_fixed_conv(s, insn);
5340 } else {
5341 switch (extract32(insn, 10, 2)) {
5342 case 1:
5343 /* Floating point conditional compare */
5344 disas_fp_ccomp(s, insn);
5345 break;
5346 case 2:
5347 /* Floating point data-processing (2 source) */
5348 disas_fp_2src(s, insn);
5349 break;
5350 case 3:
5351 /* Floating point conditional select */
5352 disas_fp_csel(s, insn);
5353 break;
5354 case 0:
5355 switch (ctz32(extract32(insn, 12, 4))) {
5356 case 0: /* [15:12] == xxx1 */
5357 /* Floating point immediate */
5358 disas_fp_imm(s, insn);
5359 break;
5360 case 1: /* [15:12] == xx10 */
5361 /* Floating point compare */
5362 disas_fp_compare(s, insn);
5363 break;
5364 case 2: /* [15:12] == x100 */
5365 /* Floating point data-processing (1 source) */
5366 disas_fp_1src(s, insn);
5367 break;
5368 case 3: /* [15:12] == 1000 */
5369 unallocated_encoding(s);
5370 break;
5371 default: /* [15:12] == 0000 */
5372 /* Floating point <-> integer conversions */
5373 disas_fp_int_conv(s, insn);
5374 break;
5376 break;
5381 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
5382 int pos)
5384 /* Extract 64 bits from the middle of two concatenated 64 bit
5385 * vector register slices left:right. The extracted bits start
5386 * at 'pos' bits into the right (least significant) side.
5387 * We return the result in tcg_right, and guarantee not to
5388 * trash tcg_left.
5390 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5391 assert(pos > 0 && pos < 64);
5393 tcg_gen_shri_i64(tcg_right, tcg_right, pos);
5394 tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
5395 tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
5397 tcg_temp_free_i64(tcg_tmp);
5400 /* C3.6.1 EXT
5401 * 31 30 29 24 23 22 21 20 16 15 14 11 10 9 5 4 0
5402 * +---+---+-------------+-----+---+------+---+------+---+------+------+
5403 * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 | Rm | 0 | imm4 | 0 | Rn | Rd |
5404 * +---+---+-------------+-----+---+------+---+------+---+------+------+
5406 static void disas_simd_ext(DisasContext *s, uint32_t insn)
5408 int is_q = extract32(insn, 30, 1);
5409 int op2 = extract32(insn, 22, 2);
5410 int imm4 = extract32(insn, 11, 4);
5411 int rm = extract32(insn, 16, 5);
5412 int rn = extract32(insn, 5, 5);
5413 int rd = extract32(insn, 0, 5);
5414 int pos = imm4 << 3;
5415 TCGv_i64 tcg_resl, tcg_resh;
5417 if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
5418 unallocated_encoding(s);
5419 return;
5422 if (!fp_access_check(s)) {
5423 return;
5426 tcg_resh = tcg_temp_new_i64();
5427 tcg_resl = tcg_temp_new_i64();
5429 /* Vd gets bits starting at pos bits into Vm:Vn. This is
5430 * either extracting 128 bits from a 128:128 concatenation, or
5431 * extracting 64 bits from a 64:64 concatenation.
5433 if (!is_q) {
5434 read_vec_element(s, tcg_resl, rn, 0, MO_64);
5435 if (pos != 0) {
5436 read_vec_element(s, tcg_resh, rm, 0, MO_64);
5437 do_ext64(s, tcg_resh, tcg_resl, pos);
5439 tcg_gen_movi_i64(tcg_resh, 0);
5440 } else {
5441 TCGv_i64 tcg_hh;
5442 typedef struct {
5443 int reg;
5444 int elt;
5445 } EltPosns;
5446 EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
5447 EltPosns *elt = eltposns;
5449 if (pos >= 64) {
5450 elt++;
5451 pos -= 64;
5454 read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
5455 elt++;
5456 read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
5457 elt++;
5458 if (pos != 0) {
5459 do_ext64(s, tcg_resh, tcg_resl, pos);
5460 tcg_hh = tcg_temp_new_i64();
5461 read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
5462 do_ext64(s, tcg_hh, tcg_resh, pos);
5463 tcg_temp_free_i64(tcg_hh);
5467 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5468 tcg_temp_free_i64(tcg_resl);
5469 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5470 tcg_temp_free_i64(tcg_resh);
5473 /* C3.6.2 TBL/TBX
5474 * 31 30 29 24 23 22 21 20 16 15 14 13 12 11 10 9 5 4 0
5475 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
5476 * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 | Rm | 0 | len | op | 0 0 | Rn | Rd |
5477 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
5479 static void disas_simd_tb(DisasContext *s, uint32_t insn)
5481 int op2 = extract32(insn, 22, 2);
5482 int is_q = extract32(insn, 30, 1);
5483 int rm = extract32(insn, 16, 5);
5484 int rn = extract32(insn, 5, 5);
5485 int rd = extract32(insn, 0, 5);
5486 int is_tblx = extract32(insn, 12, 1);
5487 int len = extract32(insn, 13, 2);
5488 TCGv_i64 tcg_resl, tcg_resh, tcg_idx;
5489 TCGv_i32 tcg_regno, tcg_numregs;
5491 if (op2 != 0) {
5492 unallocated_encoding(s);
5493 return;
5496 if (!fp_access_check(s)) {
5497 return;
5500 /* This does a table lookup: for every byte element in the input
5501 * we index into a table formed from up to four vector registers,
5502 * and then the output is the result of the lookups. Our helper
5503 * function does the lookup operation for a single 64 bit part of
5504 * the input.
5506 tcg_resl = tcg_temp_new_i64();
5507 tcg_resh = tcg_temp_new_i64();
5509 if (is_tblx) {
5510 read_vec_element(s, tcg_resl, rd, 0, MO_64);
5511 } else {
5512 tcg_gen_movi_i64(tcg_resl, 0);
5514 if (is_tblx && is_q) {
5515 read_vec_element(s, tcg_resh, rd, 1, MO_64);
5516 } else {
5517 tcg_gen_movi_i64(tcg_resh, 0);
5520 tcg_idx = tcg_temp_new_i64();
5521 tcg_regno = tcg_const_i32(rn);
5522 tcg_numregs = tcg_const_i32(len + 1);
5523 read_vec_element(s, tcg_idx, rm, 0, MO_64);
5524 gen_helper_simd_tbl(tcg_resl, cpu_env, tcg_resl, tcg_idx,
5525 tcg_regno, tcg_numregs);
5526 if (is_q) {
5527 read_vec_element(s, tcg_idx, rm, 1, MO_64);
5528 gen_helper_simd_tbl(tcg_resh, cpu_env, tcg_resh, tcg_idx,
5529 tcg_regno, tcg_numregs);
5531 tcg_temp_free_i64(tcg_idx);
5532 tcg_temp_free_i32(tcg_regno);
5533 tcg_temp_free_i32(tcg_numregs);
5535 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5536 tcg_temp_free_i64(tcg_resl);
5537 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5538 tcg_temp_free_i64(tcg_resh);
5541 /* C3.6.3 ZIP/UZP/TRN
5542 * 31 30 29 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
5543 * +---+---+-------------+------+---+------+---+------------------+------+
5544 * | 0 | Q | 0 0 1 1 1 0 | size | 0 | Rm | 0 | opc | 1 0 | Rn | Rd |
5545 * +---+---+-------------+------+---+------+---+------------------+------+
5547 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
5549 int rd = extract32(insn, 0, 5);
5550 int rn = extract32(insn, 5, 5);
5551 int rm = extract32(insn, 16, 5);
5552 int size = extract32(insn, 22, 2);
5553 /* opc field bits [1:0] indicate ZIP/UZP/TRN;
5554 * bit 2 indicates 1 vs 2 variant of the insn.
5556 int opcode = extract32(insn, 12, 2);
5557 bool part = extract32(insn, 14, 1);
5558 bool is_q = extract32(insn, 30, 1);
5559 int esize = 8 << size;
5560 int i, ofs;
5561 int datasize = is_q ? 128 : 64;
5562 int elements = datasize / esize;
5563 TCGv_i64 tcg_res, tcg_resl, tcg_resh;
5565 if (opcode == 0 || (size == 3 && !is_q)) {
5566 unallocated_encoding(s);
5567 return;
5570 if (!fp_access_check(s)) {
5571 return;
5574 tcg_resl = tcg_const_i64(0);
5575 tcg_resh = tcg_const_i64(0);
5576 tcg_res = tcg_temp_new_i64();
5578 for (i = 0; i < elements; i++) {
5579 switch (opcode) {
5580 case 1: /* UZP1/2 */
5582 int midpoint = elements / 2;
5583 if (i < midpoint) {
5584 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
5585 } else {
5586 read_vec_element(s, tcg_res, rm,
5587 2 * (i - midpoint) + part, size);
5589 break;
5591 case 2: /* TRN1/2 */
5592 if (i & 1) {
5593 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
5594 } else {
5595 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
5597 break;
5598 case 3: /* ZIP1/2 */
5600 int base = part * elements / 2;
5601 if (i & 1) {
5602 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
5603 } else {
5604 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
5606 break;
5608 default:
5609 g_assert_not_reached();
5612 ofs = i * esize;
5613 if (ofs < 64) {
5614 tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
5615 tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
5616 } else {
5617 tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
5618 tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
5622 tcg_temp_free_i64(tcg_res);
5624 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5625 tcg_temp_free_i64(tcg_resl);
5626 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5627 tcg_temp_free_i64(tcg_resh);
5630 static void do_minmaxop(DisasContext *s, TCGv_i32 tcg_elt1, TCGv_i32 tcg_elt2,
5631 int opc, bool is_min, TCGv_ptr fpst)
5633 /* Helper function for disas_simd_across_lanes: do a single precision
5634 * min/max operation on the specified two inputs,
5635 * and return the result in tcg_elt1.
5637 if (opc == 0xc) {
5638 if (is_min) {
5639 gen_helper_vfp_minnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5640 } else {
5641 gen_helper_vfp_maxnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5643 } else {
5644 assert(opc == 0xf);
5645 if (is_min) {
5646 gen_helper_vfp_mins(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5647 } else {
5648 gen_helper_vfp_maxs(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5653 /* C3.6.4 AdvSIMD across lanes
5654 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
5655 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5656 * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
5657 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5659 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
5661 int rd = extract32(insn, 0, 5);
5662 int rn = extract32(insn, 5, 5);
5663 int size = extract32(insn, 22, 2);
5664 int opcode = extract32(insn, 12, 5);
5665 bool is_q = extract32(insn, 30, 1);
5666 bool is_u = extract32(insn, 29, 1);
5667 bool is_fp = false;
5668 bool is_min = false;
5669 int esize;
5670 int elements;
5671 int i;
5672 TCGv_i64 tcg_res, tcg_elt;
5674 switch (opcode) {
5675 case 0x1b: /* ADDV */
5676 if (is_u) {
5677 unallocated_encoding(s);
5678 return;
5680 /* fall through */
5681 case 0x3: /* SADDLV, UADDLV */
5682 case 0xa: /* SMAXV, UMAXV */
5683 case 0x1a: /* SMINV, UMINV */
5684 if (size == 3 || (size == 2 && !is_q)) {
5685 unallocated_encoding(s);
5686 return;
5688 break;
5689 case 0xc: /* FMAXNMV, FMINNMV */
5690 case 0xf: /* FMAXV, FMINV */
5691 if (!is_u || !is_q || extract32(size, 0, 1)) {
5692 unallocated_encoding(s);
5693 return;
5695 /* Bit 1 of size field encodes min vs max, and actual size is always
5696 * 32 bits: adjust the size variable so following code can rely on it
5698 is_min = extract32(size, 1, 1);
5699 is_fp = true;
5700 size = 2;
5701 break;
5702 default:
5703 unallocated_encoding(s);
5704 return;
5707 if (!fp_access_check(s)) {
5708 return;
5711 esize = 8 << size;
5712 elements = (is_q ? 128 : 64) / esize;
5714 tcg_res = tcg_temp_new_i64();
5715 tcg_elt = tcg_temp_new_i64();
5717 /* These instructions operate across all lanes of a vector
5718 * to produce a single result. We can guarantee that a 64
5719 * bit intermediate is sufficient:
5720 * + for [US]ADDLV the maximum element size is 32 bits, and
5721 * the result type is 64 bits
5722 * + for FMAX*V, FMIN*V, ADDV the intermediate type is the
5723 * same as the element size, which is 32 bits at most
5724 * For the integer operations we can choose to work at 64
5725 * or 32 bits and truncate at the end; for simplicity
5726 * we use 64 bits always. The floating point
5727 * ops do require 32 bit intermediates, though.
5729 if (!is_fp) {
5730 read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
5732 for (i = 1; i < elements; i++) {
5733 read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
5735 switch (opcode) {
5736 case 0x03: /* SADDLV / UADDLV */
5737 case 0x1b: /* ADDV */
5738 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
5739 break;
5740 case 0x0a: /* SMAXV / UMAXV */
5741 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
5742 tcg_res,
5743 tcg_res, tcg_elt, tcg_res, tcg_elt);
5744 break;
5745 case 0x1a: /* SMINV / UMINV */
5746 tcg_gen_movcond_i64(is_u ? TCG_COND_LEU : TCG_COND_LE,
5747 tcg_res,
5748 tcg_res, tcg_elt, tcg_res, tcg_elt);
5749 break;
5750 break;
5751 default:
5752 g_assert_not_reached();
5756 } else {
5757 /* Floating point ops which work on 32 bit (single) intermediates.
5758 * Note that correct NaN propagation requires that we do these
5759 * operations in exactly the order specified by the pseudocode.
5761 TCGv_i32 tcg_elt1 = tcg_temp_new_i32();
5762 TCGv_i32 tcg_elt2 = tcg_temp_new_i32();
5763 TCGv_i32 tcg_elt3 = tcg_temp_new_i32();
5764 TCGv_ptr fpst = get_fpstatus_ptr();
5766 assert(esize == 32);
5767 assert(elements == 4);
5769 read_vec_element(s, tcg_elt, rn, 0, MO_32);
5770 tcg_gen_extrl_i64_i32(tcg_elt1, tcg_elt);
5771 read_vec_element(s, tcg_elt, rn, 1, MO_32);
5772 tcg_gen_extrl_i64_i32(tcg_elt2, tcg_elt);
5774 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5776 read_vec_element(s, tcg_elt, rn, 2, MO_32);
5777 tcg_gen_extrl_i64_i32(tcg_elt2, tcg_elt);
5778 read_vec_element(s, tcg_elt, rn, 3, MO_32);
5779 tcg_gen_extrl_i64_i32(tcg_elt3, tcg_elt);
5781 do_minmaxop(s, tcg_elt2, tcg_elt3, opcode, is_min, fpst);
5783 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5785 tcg_gen_extu_i32_i64(tcg_res, tcg_elt1);
5786 tcg_temp_free_i32(tcg_elt1);
5787 tcg_temp_free_i32(tcg_elt2);
5788 tcg_temp_free_i32(tcg_elt3);
5789 tcg_temp_free_ptr(fpst);
5792 tcg_temp_free_i64(tcg_elt);
5794 /* Now truncate the result to the width required for the final output */
5795 if (opcode == 0x03) {
5796 /* SADDLV, UADDLV: result is 2*esize */
5797 size++;
5800 switch (size) {
5801 case 0:
5802 tcg_gen_ext8u_i64(tcg_res, tcg_res);
5803 break;
5804 case 1:
5805 tcg_gen_ext16u_i64(tcg_res, tcg_res);
5806 break;
5807 case 2:
5808 tcg_gen_ext32u_i64(tcg_res, tcg_res);
5809 break;
5810 case 3:
5811 break;
5812 default:
5813 g_assert_not_reached();
5816 write_fp_dreg(s, rd, tcg_res);
5817 tcg_temp_free_i64(tcg_res);
5820 /* C6.3.31 DUP (Element, Vector)
5822 * 31 30 29 21 20 16 15 10 9 5 4 0
5823 * +---+---+-------------------+--------+-------------+------+------+
5824 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
5825 * +---+---+-------------------+--------+-------------+------+------+
5827 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5829 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
5830 int imm5)
5832 int size = ctz32(imm5);
5833 int esize = 8 << size;
5834 int elements = (is_q ? 128 : 64) / esize;
5835 int index, i;
5836 TCGv_i64 tmp;
5838 if (size > 3 || (size == 3 && !is_q)) {
5839 unallocated_encoding(s);
5840 return;
5843 if (!fp_access_check(s)) {
5844 return;
5847 index = imm5 >> (size + 1);
5849 tmp = tcg_temp_new_i64();
5850 read_vec_element(s, tmp, rn, index, size);
5852 for (i = 0; i < elements; i++) {
5853 write_vec_element(s, tmp, rd, i, size);
5856 if (!is_q) {
5857 clear_vec_high(s, rd);
5860 tcg_temp_free_i64(tmp);
5863 /* C6.3.31 DUP (element, scalar)
5864 * 31 21 20 16 15 10 9 5 4 0
5865 * +-----------------------+--------+-------------+------+------+
5866 * | 0 1 0 1 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
5867 * +-----------------------+--------+-------------+------+------+
5869 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
5870 int imm5)
5872 int size = ctz32(imm5);
5873 int index;
5874 TCGv_i64 tmp;
5876 if (size > 3) {
5877 unallocated_encoding(s);
5878 return;
5881 if (!fp_access_check(s)) {
5882 return;
5885 index = imm5 >> (size + 1);
5887 /* This instruction just extracts the specified element and
5888 * zero-extends it into the bottom of the destination register.
5890 tmp = tcg_temp_new_i64();
5891 read_vec_element(s, tmp, rn, index, size);
5892 write_fp_dreg(s, rd, tmp);
5893 tcg_temp_free_i64(tmp);
5896 /* C6.3.32 DUP (General)
5898 * 31 30 29 21 20 16 15 10 9 5 4 0
5899 * +---+---+-------------------+--------+-------------+------+------+
5900 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 1 1 | Rn | Rd |
5901 * +---+---+-------------------+--------+-------------+------+------+
5903 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5905 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
5906 int imm5)
5908 int size = ctz32(imm5);
5909 int esize = 8 << size;
5910 int elements = (is_q ? 128 : 64)/esize;
5911 int i = 0;
5913 if (size > 3 || ((size == 3) && !is_q)) {
5914 unallocated_encoding(s);
5915 return;
5918 if (!fp_access_check(s)) {
5919 return;
5922 for (i = 0; i < elements; i++) {
5923 write_vec_element(s, cpu_reg(s, rn), rd, i, size);
5925 if (!is_q) {
5926 clear_vec_high(s, rd);
5930 /* C6.3.150 INS (Element)
5932 * 31 21 20 16 15 14 11 10 9 5 4 0
5933 * +-----------------------+--------+------------+---+------+------+
5934 * | 0 1 1 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5935 * +-----------------------+--------+------------+---+------+------+
5937 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5938 * index: encoded in imm5<4:size+1>
5940 static void handle_simd_inse(DisasContext *s, int rd, int rn,
5941 int imm4, int imm5)
5943 int size = ctz32(imm5);
5944 int src_index, dst_index;
5945 TCGv_i64 tmp;
5947 if (size > 3) {
5948 unallocated_encoding(s);
5949 return;
5952 if (!fp_access_check(s)) {
5953 return;
5956 dst_index = extract32(imm5, 1+size, 5);
5957 src_index = extract32(imm4, size, 4);
5959 tmp = tcg_temp_new_i64();
5961 read_vec_element(s, tmp, rn, src_index, size);
5962 write_vec_element(s, tmp, rd, dst_index, size);
5964 tcg_temp_free_i64(tmp);
5968 /* C6.3.151 INS (General)
5970 * 31 21 20 16 15 10 9 5 4 0
5971 * +-----------------------+--------+-------------+------+------+
5972 * | 0 1 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 1 1 1 | Rn | Rd |
5973 * +-----------------------+--------+-------------+------+------+
5975 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5976 * index: encoded in imm5<4:size+1>
5978 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
5980 int size = ctz32(imm5);
5981 int idx;
5983 if (size > 3) {
5984 unallocated_encoding(s);
5985 return;
5988 if (!fp_access_check(s)) {
5989 return;
5992 idx = extract32(imm5, 1 + size, 4 - size);
5993 write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
5997 * C6.3.321 UMOV (General)
5998 * C6.3.237 SMOV (General)
6000 * 31 30 29 21 20 16 15 12 10 9 5 4 0
6001 * +---+---+-------------------+--------+-------------+------+------+
6002 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 1 U 1 1 | Rn | Rd |
6003 * +---+---+-------------------+--------+-------------+------+------+
6005 * U: unsigned when set
6006 * size: encoded in imm5 (see ARM ARM LowestSetBit())
6008 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
6009 int rn, int rd, int imm5)
6011 int size = ctz32(imm5);
6012 int element;
6013 TCGv_i64 tcg_rd;
6015 /* Check for UnallocatedEncodings */
6016 if (is_signed) {
6017 if (size > 2 || (size == 2 && !is_q)) {
6018 unallocated_encoding(s);
6019 return;
6021 } else {
6022 if (size > 3
6023 || (size < 3 && is_q)
6024 || (size == 3 && !is_q)) {
6025 unallocated_encoding(s);
6026 return;
6030 if (!fp_access_check(s)) {
6031 return;
6034 element = extract32(imm5, 1+size, 4);
6036 tcg_rd = cpu_reg(s, rd);
6037 read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
6038 if (is_signed && !is_q) {
6039 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
6043 /* C3.6.5 AdvSIMD copy
6044 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
6045 * +---+---+----+-----------------+------+---+------+---+------+------+
6046 * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
6047 * +---+---+----+-----------------+------+---+------+---+------+------+
6049 static void disas_simd_copy(DisasContext *s, uint32_t insn)
6051 int rd = extract32(insn, 0, 5);
6052 int rn = extract32(insn, 5, 5);
6053 int imm4 = extract32(insn, 11, 4);
6054 int op = extract32(insn, 29, 1);
6055 int is_q = extract32(insn, 30, 1);
6056 int imm5 = extract32(insn, 16, 5);
6058 if (op) {
6059 if (is_q) {
6060 /* INS (element) */
6061 handle_simd_inse(s, rd, rn, imm4, imm5);
6062 } else {
6063 unallocated_encoding(s);
6065 } else {
6066 switch (imm4) {
6067 case 0:
6068 /* DUP (element - vector) */
6069 handle_simd_dupe(s, is_q, rd, rn, imm5);
6070 break;
6071 case 1:
6072 /* DUP (general) */
6073 handle_simd_dupg(s, is_q, rd, rn, imm5);
6074 break;
6075 case 3:
6076 if (is_q) {
6077 /* INS (general) */
6078 handle_simd_insg(s, rd, rn, imm5);
6079 } else {
6080 unallocated_encoding(s);
6082 break;
6083 case 5:
6084 case 7:
6085 /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
6086 handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
6087 break;
6088 default:
6089 unallocated_encoding(s);
6090 break;
6095 /* C3.6.6 AdvSIMD modified immediate
6096 * 31 30 29 28 19 18 16 15 12 11 10 9 5 4 0
6097 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
6098 * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh | Rd |
6099 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
6101 * There are a number of operations that can be carried out here:
6102 * MOVI - move (shifted) imm into register
6103 * MVNI - move inverted (shifted) imm into register
6104 * ORR - bitwise OR of (shifted) imm with register
6105 * BIC - bitwise clear of (shifted) imm with register
6107 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
6109 int rd = extract32(insn, 0, 5);
6110 int cmode = extract32(insn, 12, 4);
6111 int cmode_3_1 = extract32(cmode, 1, 3);
6112 int cmode_0 = extract32(cmode, 0, 1);
6113 int o2 = extract32(insn, 11, 1);
6114 uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
6115 bool is_neg = extract32(insn, 29, 1);
6116 bool is_q = extract32(insn, 30, 1);
6117 uint64_t imm = 0;
6118 TCGv_i64 tcg_rd, tcg_imm;
6119 int i;
6121 if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
6122 unallocated_encoding(s);
6123 return;
6126 if (!fp_access_check(s)) {
6127 return;
6130 /* See AdvSIMDExpandImm() in ARM ARM */
6131 switch (cmode_3_1) {
6132 case 0: /* Replicate(Zeros(24):imm8, 2) */
6133 case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
6134 case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
6135 case 3: /* Replicate(imm8:Zeros(24), 2) */
6137 int shift = cmode_3_1 * 8;
6138 imm = bitfield_replicate(abcdefgh << shift, 32);
6139 break;
6141 case 4: /* Replicate(Zeros(8):imm8, 4) */
6142 case 5: /* Replicate(imm8:Zeros(8), 4) */
6144 int shift = (cmode_3_1 & 0x1) * 8;
6145 imm = bitfield_replicate(abcdefgh << shift, 16);
6146 break;
6148 case 6:
6149 if (cmode_0) {
6150 /* Replicate(Zeros(8):imm8:Ones(16), 2) */
6151 imm = (abcdefgh << 16) | 0xffff;
6152 } else {
6153 /* Replicate(Zeros(16):imm8:Ones(8), 2) */
6154 imm = (abcdefgh << 8) | 0xff;
6156 imm = bitfield_replicate(imm, 32);
6157 break;
6158 case 7:
6159 if (!cmode_0 && !is_neg) {
6160 imm = bitfield_replicate(abcdefgh, 8);
6161 } else if (!cmode_0 && is_neg) {
6162 int i;
6163 imm = 0;
6164 for (i = 0; i < 8; i++) {
6165 if ((abcdefgh) & (1 << i)) {
6166 imm |= 0xffULL << (i * 8);
6169 } else if (cmode_0) {
6170 if (is_neg) {
6171 imm = (abcdefgh & 0x3f) << 48;
6172 if (abcdefgh & 0x80) {
6173 imm |= 0x8000000000000000ULL;
6175 if (abcdefgh & 0x40) {
6176 imm |= 0x3fc0000000000000ULL;
6177 } else {
6178 imm |= 0x4000000000000000ULL;
6180 } else {
6181 imm = (abcdefgh & 0x3f) << 19;
6182 if (abcdefgh & 0x80) {
6183 imm |= 0x80000000;
6185 if (abcdefgh & 0x40) {
6186 imm |= 0x3e000000;
6187 } else {
6188 imm |= 0x40000000;
6190 imm |= (imm << 32);
6193 break;
6196 if (cmode_3_1 != 7 && is_neg) {
6197 imm = ~imm;
6200 tcg_imm = tcg_const_i64(imm);
6201 tcg_rd = new_tmp_a64(s);
6203 for (i = 0; i < 2; i++) {
6204 int foffs = i ? fp_reg_hi_offset(s, rd) : fp_reg_offset(s, rd, MO_64);
6206 if (i == 1 && !is_q) {
6207 /* non-quad ops clear high half of vector */
6208 tcg_gen_movi_i64(tcg_rd, 0);
6209 } else if ((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9) {
6210 tcg_gen_ld_i64(tcg_rd, cpu_env, foffs);
6211 if (is_neg) {
6212 /* AND (BIC) */
6213 tcg_gen_and_i64(tcg_rd, tcg_rd, tcg_imm);
6214 } else {
6215 /* ORR */
6216 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_imm);
6218 } else {
6219 /* MOVI */
6220 tcg_gen_mov_i64(tcg_rd, tcg_imm);
6222 tcg_gen_st_i64(tcg_rd, cpu_env, foffs);
6225 tcg_temp_free_i64(tcg_imm);
6228 /* C3.6.7 AdvSIMD scalar copy
6229 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
6230 * +-----+----+-----------------+------+---+------+---+------+------+
6231 * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
6232 * +-----+----+-----------------+------+---+------+---+------+------+
6234 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
6236 int rd = extract32(insn, 0, 5);
6237 int rn = extract32(insn, 5, 5);
6238 int imm4 = extract32(insn, 11, 4);
6239 int imm5 = extract32(insn, 16, 5);
6240 int op = extract32(insn, 29, 1);
6242 if (op != 0 || imm4 != 0) {
6243 unallocated_encoding(s);
6244 return;
6247 /* DUP (element, scalar) */
6248 handle_simd_dupes(s, rd, rn, imm5);
6251 /* C3.6.8 AdvSIMD scalar pairwise
6252 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
6253 * +-----+---+-----------+------+-----------+--------+-----+------+------+
6254 * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
6255 * +-----+---+-----------+------+-----------+--------+-----+------+------+
6257 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
6259 int u = extract32(insn, 29, 1);
6260 int size = extract32(insn, 22, 2);
6261 int opcode = extract32(insn, 12, 5);
6262 int rn = extract32(insn, 5, 5);
6263 int rd = extract32(insn, 0, 5);
6264 TCGv_ptr fpst;
6266 /* For some ops (the FP ones), size[1] is part of the encoding.
6267 * For ADDP strictly it is not but size[1] is always 1 for valid
6268 * encodings.
6270 opcode |= (extract32(size, 1, 1) << 5);
6272 switch (opcode) {
6273 case 0x3b: /* ADDP */
6274 if (u || size != 3) {
6275 unallocated_encoding(s);
6276 return;
6278 if (!fp_access_check(s)) {
6279 return;
6282 TCGV_UNUSED_PTR(fpst);
6283 break;
6284 case 0xc: /* FMAXNMP */
6285 case 0xd: /* FADDP */
6286 case 0xf: /* FMAXP */
6287 case 0x2c: /* FMINNMP */
6288 case 0x2f: /* FMINP */
6289 /* FP op, size[0] is 32 or 64 bit */
6290 if (!u) {
6291 unallocated_encoding(s);
6292 return;
6294 if (!fp_access_check(s)) {
6295 return;
6298 size = extract32(size, 0, 1) ? 3 : 2;
6299 fpst = get_fpstatus_ptr();
6300 break;
6301 default:
6302 unallocated_encoding(s);
6303 return;
6306 if (size == 3) {
6307 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6308 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6309 TCGv_i64 tcg_res = tcg_temp_new_i64();
6311 read_vec_element(s, tcg_op1, rn, 0, MO_64);
6312 read_vec_element(s, tcg_op2, rn, 1, MO_64);
6314 switch (opcode) {
6315 case 0x3b: /* ADDP */
6316 tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
6317 break;
6318 case 0xc: /* FMAXNMP */
6319 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6320 break;
6321 case 0xd: /* FADDP */
6322 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6323 break;
6324 case 0xf: /* FMAXP */
6325 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6326 break;
6327 case 0x2c: /* FMINNMP */
6328 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6329 break;
6330 case 0x2f: /* FMINP */
6331 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6332 break;
6333 default:
6334 g_assert_not_reached();
6337 write_fp_dreg(s, rd, tcg_res);
6339 tcg_temp_free_i64(tcg_op1);
6340 tcg_temp_free_i64(tcg_op2);
6341 tcg_temp_free_i64(tcg_res);
6342 } else {
6343 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6344 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6345 TCGv_i32 tcg_res = tcg_temp_new_i32();
6347 read_vec_element_i32(s, tcg_op1, rn, 0, MO_32);
6348 read_vec_element_i32(s, tcg_op2, rn, 1, MO_32);
6350 switch (opcode) {
6351 case 0xc: /* FMAXNMP */
6352 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6353 break;
6354 case 0xd: /* FADDP */
6355 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6356 break;
6357 case 0xf: /* FMAXP */
6358 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6359 break;
6360 case 0x2c: /* FMINNMP */
6361 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6362 break;
6363 case 0x2f: /* FMINP */
6364 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6365 break;
6366 default:
6367 g_assert_not_reached();
6370 write_fp_sreg(s, rd, tcg_res);
6372 tcg_temp_free_i32(tcg_op1);
6373 tcg_temp_free_i32(tcg_op2);
6374 tcg_temp_free_i32(tcg_res);
6377 if (!TCGV_IS_UNUSED_PTR(fpst)) {
6378 tcg_temp_free_ptr(fpst);
6383 * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
6385 * This code is handles the common shifting code and is used by both
6386 * the vector and scalar code.
6388 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6389 TCGv_i64 tcg_rnd, bool accumulate,
6390 bool is_u, int size, int shift)
6392 bool extended_result = false;
6393 bool round = !TCGV_IS_UNUSED_I64(tcg_rnd);
6394 int ext_lshift = 0;
6395 TCGv_i64 tcg_src_hi;
6397 if (round && size == 3) {
6398 extended_result = true;
6399 ext_lshift = 64 - shift;
6400 tcg_src_hi = tcg_temp_new_i64();
6401 } else if (shift == 64) {
6402 if (!accumulate && is_u) {
6403 /* result is zero */
6404 tcg_gen_movi_i64(tcg_res, 0);
6405 return;
6409 /* Deal with the rounding step */
6410 if (round) {
6411 if (extended_result) {
6412 TCGv_i64 tcg_zero = tcg_const_i64(0);
6413 if (!is_u) {
6414 /* take care of sign extending tcg_res */
6415 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
6416 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
6417 tcg_src, tcg_src_hi,
6418 tcg_rnd, tcg_zero);
6419 } else {
6420 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
6421 tcg_src, tcg_zero,
6422 tcg_rnd, tcg_zero);
6424 tcg_temp_free_i64(tcg_zero);
6425 } else {
6426 tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
6430 /* Now do the shift right */
6431 if (round && extended_result) {
6432 /* extended case, >64 bit precision required */
6433 if (ext_lshift == 0) {
6434 /* special case, only high bits matter */
6435 tcg_gen_mov_i64(tcg_src, tcg_src_hi);
6436 } else {
6437 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6438 tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
6439 tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
6441 } else {
6442 if (is_u) {
6443 if (shift == 64) {
6444 /* essentially shifting in 64 zeros */
6445 tcg_gen_movi_i64(tcg_src, 0);
6446 } else {
6447 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6449 } else {
6450 if (shift == 64) {
6451 /* effectively extending the sign-bit */
6452 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
6453 } else {
6454 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
6459 if (accumulate) {
6460 tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
6461 } else {
6462 tcg_gen_mov_i64(tcg_res, tcg_src);
6465 if (extended_result) {
6466 tcg_temp_free_i64(tcg_src_hi);
6470 /* Common SHL/SLI - Shift left with an optional insert */
6471 static void handle_shli_with_ins(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6472 bool insert, int shift)
6474 if (insert) { /* SLI */
6475 tcg_gen_deposit_i64(tcg_res, tcg_res, tcg_src, shift, 64 - shift);
6476 } else { /* SHL */
6477 tcg_gen_shli_i64(tcg_res, tcg_src, shift);
6481 /* SRI: shift right with insert */
6482 static void handle_shri_with_ins(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6483 int size, int shift)
6485 int esize = 8 << size;
6487 /* shift count same as element size is valid but does nothing;
6488 * special case to avoid potential shift by 64.
6490 if (shift != esize) {
6491 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6492 tcg_gen_deposit_i64(tcg_res, tcg_res, tcg_src, 0, esize - shift);
6496 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
6497 static void handle_scalar_simd_shri(DisasContext *s,
6498 bool is_u, int immh, int immb,
6499 int opcode, int rn, int rd)
6501 const int size = 3;
6502 int immhb = immh << 3 | immb;
6503 int shift = 2 * (8 << size) - immhb;
6504 bool accumulate = false;
6505 bool round = false;
6506 bool insert = false;
6507 TCGv_i64 tcg_rn;
6508 TCGv_i64 tcg_rd;
6509 TCGv_i64 tcg_round;
6511 if (!extract32(immh, 3, 1)) {
6512 unallocated_encoding(s);
6513 return;
6516 if (!fp_access_check(s)) {
6517 return;
6520 switch (opcode) {
6521 case 0x02: /* SSRA / USRA (accumulate) */
6522 accumulate = true;
6523 break;
6524 case 0x04: /* SRSHR / URSHR (rounding) */
6525 round = true;
6526 break;
6527 case 0x06: /* SRSRA / URSRA (accum + rounding) */
6528 accumulate = round = true;
6529 break;
6530 case 0x08: /* SRI */
6531 insert = true;
6532 break;
6535 if (round) {
6536 uint64_t round_const = 1ULL << (shift - 1);
6537 tcg_round = tcg_const_i64(round_const);
6538 } else {
6539 TCGV_UNUSED_I64(tcg_round);
6542 tcg_rn = read_fp_dreg(s, rn);
6543 tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6545 if (insert) {
6546 handle_shri_with_ins(tcg_rd, tcg_rn, size, shift);
6547 } else {
6548 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6549 accumulate, is_u, size, shift);
6552 write_fp_dreg(s, rd, tcg_rd);
6554 tcg_temp_free_i64(tcg_rn);
6555 tcg_temp_free_i64(tcg_rd);
6556 if (round) {
6557 tcg_temp_free_i64(tcg_round);
6561 /* SHL/SLI - Scalar shift left */
6562 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
6563 int immh, int immb, int opcode,
6564 int rn, int rd)
6566 int size = 32 - clz32(immh) - 1;
6567 int immhb = immh << 3 | immb;
6568 int shift = immhb - (8 << size);
6569 TCGv_i64 tcg_rn = new_tmp_a64(s);
6570 TCGv_i64 tcg_rd = new_tmp_a64(s);
6572 if (!extract32(immh, 3, 1)) {
6573 unallocated_encoding(s);
6574 return;
6577 if (!fp_access_check(s)) {
6578 return;
6581 tcg_rn = read_fp_dreg(s, rn);
6582 tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6584 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
6586 write_fp_dreg(s, rd, tcg_rd);
6588 tcg_temp_free_i64(tcg_rn);
6589 tcg_temp_free_i64(tcg_rd);
6592 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
6593 * (signed/unsigned) narrowing */
6594 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
6595 bool is_u_shift, bool is_u_narrow,
6596 int immh, int immb, int opcode,
6597 int rn, int rd)
6599 int immhb = immh << 3 | immb;
6600 int size = 32 - clz32(immh) - 1;
6601 int esize = 8 << size;
6602 int shift = (2 * esize) - immhb;
6603 int elements = is_scalar ? 1 : (64 / esize);
6604 bool round = extract32(opcode, 0, 1);
6605 TCGMemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
6606 TCGv_i64 tcg_rn, tcg_rd, tcg_round;
6607 TCGv_i32 tcg_rd_narrowed;
6608 TCGv_i64 tcg_final;
6610 static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
6611 { gen_helper_neon_narrow_sat_s8,
6612 gen_helper_neon_unarrow_sat8 },
6613 { gen_helper_neon_narrow_sat_s16,
6614 gen_helper_neon_unarrow_sat16 },
6615 { gen_helper_neon_narrow_sat_s32,
6616 gen_helper_neon_unarrow_sat32 },
6617 { NULL, NULL },
6619 static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
6620 gen_helper_neon_narrow_sat_u8,
6621 gen_helper_neon_narrow_sat_u16,
6622 gen_helper_neon_narrow_sat_u32,
6623 NULL
6625 NeonGenNarrowEnvFn *narrowfn;
6627 int i;
6629 assert(size < 4);
6631 if (extract32(immh, 3, 1)) {
6632 unallocated_encoding(s);
6633 return;
6636 if (!fp_access_check(s)) {
6637 return;
6640 if (is_u_shift) {
6641 narrowfn = unsigned_narrow_fns[size];
6642 } else {
6643 narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
6646 tcg_rn = tcg_temp_new_i64();
6647 tcg_rd = tcg_temp_new_i64();
6648 tcg_rd_narrowed = tcg_temp_new_i32();
6649 tcg_final = tcg_const_i64(0);
6651 if (round) {
6652 uint64_t round_const = 1ULL << (shift - 1);
6653 tcg_round = tcg_const_i64(round_const);
6654 } else {
6655 TCGV_UNUSED_I64(tcg_round);
6658 for (i = 0; i < elements; i++) {
6659 read_vec_element(s, tcg_rn, rn, i, ldop);
6660 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6661 false, is_u_shift, size+1, shift);
6662 narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
6663 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
6664 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
6667 if (!is_q) {
6668 clear_vec_high(s, rd);
6669 write_vec_element(s, tcg_final, rd, 0, MO_64);
6670 } else {
6671 write_vec_element(s, tcg_final, rd, 1, MO_64);
6674 if (round) {
6675 tcg_temp_free_i64(tcg_round);
6677 tcg_temp_free_i64(tcg_rn);
6678 tcg_temp_free_i64(tcg_rd);
6679 tcg_temp_free_i32(tcg_rd_narrowed);
6680 tcg_temp_free_i64(tcg_final);
6681 return;
6684 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
6685 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
6686 bool src_unsigned, bool dst_unsigned,
6687 int immh, int immb, int rn, int rd)
6689 int immhb = immh << 3 | immb;
6690 int size = 32 - clz32(immh) - 1;
6691 int shift = immhb - (8 << size);
6692 int pass;
6694 assert(immh != 0);
6695 assert(!(scalar && is_q));
6697 if (!scalar) {
6698 if (!is_q && extract32(immh, 3, 1)) {
6699 unallocated_encoding(s);
6700 return;
6703 /* Since we use the variable-shift helpers we must
6704 * replicate the shift count into each element of
6705 * the tcg_shift value.
6707 switch (size) {
6708 case 0:
6709 shift |= shift << 8;
6710 /* fall through */
6711 case 1:
6712 shift |= shift << 16;
6713 break;
6714 case 2:
6715 case 3:
6716 break;
6717 default:
6718 g_assert_not_reached();
6722 if (!fp_access_check(s)) {
6723 return;
6726 if (size == 3) {
6727 TCGv_i64 tcg_shift = tcg_const_i64(shift);
6728 static NeonGenTwo64OpEnvFn * const fns[2][2] = {
6729 { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
6730 { NULL, gen_helper_neon_qshl_u64 },
6732 NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
6733 int maxpass = is_q ? 2 : 1;
6735 for (pass = 0; pass < maxpass; pass++) {
6736 TCGv_i64 tcg_op = tcg_temp_new_i64();
6738 read_vec_element(s, tcg_op, rn, pass, MO_64);
6739 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6740 write_vec_element(s, tcg_op, rd, pass, MO_64);
6742 tcg_temp_free_i64(tcg_op);
6744 tcg_temp_free_i64(tcg_shift);
6746 if (!is_q) {
6747 clear_vec_high(s, rd);
6749 } else {
6750 TCGv_i32 tcg_shift = tcg_const_i32(shift);
6751 static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
6753 { gen_helper_neon_qshl_s8,
6754 gen_helper_neon_qshl_s16,
6755 gen_helper_neon_qshl_s32 },
6756 { gen_helper_neon_qshlu_s8,
6757 gen_helper_neon_qshlu_s16,
6758 gen_helper_neon_qshlu_s32 }
6759 }, {
6760 { NULL, NULL, NULL },
6761 { gen_helper_neon_qshl_u8,
6762 gen_helper_neon_qshl_u16,
6763 gen_helper_neon_qshl_u32 }
6766 NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
6767 TCGMemOp memop = scalar ? size : MO_32;
6768 int maxpass = scalar ? 1 : is_q ? 4 : 2;
6770 for (pass = 0; pass < maxpass; pass++) {
6771 TCGv_i32 tcg_op = tcg_temp_new_i32();
6773 read_vec_element_i32(s, tcg_op, rn, pass, memop);
6774 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6775 if (scalar) {
6776 switch (size) {
6777 case 0:
6778 tcg_gen_ext8u_i32(tcg_op, tcg_op);
6779 break;
6780 case 1:
6781 tcg_gen_ext16u_i32(tcg_op, tcg_op);
6782 break;
6783 case 2:
6784 break;
6785 default:
6786 g_assert_not_reached();
6788 write_fp_sreg(s, rd, tcg_op);
6789 } else {
6790 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
6793 tcg_temp_free_i32(tcg_op);
6795 tcg_temp_free_i32(tcg_shift);
6797 if (!is_q && !scalar) {
6798 clear_vec_high(s, rd);
6803 /* Common vector code for handling integer to FP conversion */
6804 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
6805 int elements, int is_signed,
6806 int fracbits, int size)
6808 bool is_double = size == 3 ? true : false;
6809 TCGv_ptr tcg_fpst = get_fpstatus_ptr();
6810 TCGv_i32 tcg_shift = tcg_const_i32(fracbits);
6811 TCGv_i64 tcg_int = tcg_temp_new_i64();
6812 TCGMemOp mop = size | (is_signed ? MO_SIGN : 0);
6813 int pass;
6815 for (pass = 0; pass < elements; pass++) {
6816 read_vec_element(s, tcg_int, rn, pass, mop);
6818 if (is_double) {
6819 TCGv_i64 tcg_double = tcg_temp_new_i64();
6820 if (is_signed) {
6821 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6822 tcg_shift, tcg_fpst);
6823 } else {
6824 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6825 tcg_shift, tcg_fpst);
6827 if (elements == 1) {
6828 write_fp_dreg(s, rd, tcg_double);
6829 } else {
6830 write_vec_element(s, tcg_double, rd, pass, MO_64);
6832 tcg_temp_free_i64(tcg_double);
6833 } else {
6834 TCGv_i32 tcg_single = tcg_temp_new_i32();
6835 if (is_signed) {
6836 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6837 tcg_shift, tcg_fpst);
6838 } else {
6839 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6840 tcg_shift, tcg_fpst);
6842 if (elements == 1) {
6843 write_fp_sreg(s, rd, tcg_single);
6844 } else {
6845 write_vec_element_i32(s, tcg_single, rd, pass, MO_32);
6847 tcg_temp_free_i32(tcg_single);
6851 if (!is_double && elements == 2) {
6852 clear_vec_high(s, rd);
6855 tcg_temp_free_i64(tcg_int);
6856 tcg_temp_free_ptr(tcg_fpst);
6857 tcg_temp_free_i32(tcg_shift);
6860 /* UCVTF/SCVTF - Integer to FP conversion */
6861 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
6862 bool is_q, bool is_u,
6863 int immh, int immb, int opcode,
6864 int rn, int rd)
6866 bool is_double = extract32(immh, 3, 1);
6867 int size = is_double ? MO_64 : MO_32;
6868 int elements;
6869 int immhb = immh << 3 | immb;
6870 int fracbits = (is_double ? 128 : 64) - immhb;
6872 if (!extract32(immh, 2, 2)) {
6873 unallocated_encoding(s);
6874 return;
6877 if (is_scalar) {
6878 elements = 1;
6879 } else {
6880 elements = is_double ? 2 : is_q ? 4 : 2;
6881 if (is_double && !is_q) {
6882 unallocated_encoding(s);
6883 return;
6887 if (!fp_access_check(s)) {
6888 return;
6891 /* immh == 0 would be a failure of the decode logic */
6892 g_assert(immh);
6894 handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
6897 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
6898 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
6899 bool is_q, bool is_u,
6900 int immh, int immb, int rn, int rd)
6902 bool is_double = extract32(immh, 3, 1);
6903 int immhb = immh << 3 | immb;
6904 int fracbits = (is_double ? 128 : 64) - immhb;
6905 int pass;
6906 TCGv_ptr tcg_fpstatus;
6907 TCGv_i32 tcg_rmode, tcg_shift;
6909 if (!extract32(immh, 2, 2)) {
6910 unallocated_encoding(s);
6911 return;
6914 if (!is_scalar && !is_q && is_double) {
6915 unallocated_encoding(s);
6916 return;
6919 if (!fp_access_check(s)) {
6920 return;
6923 assert(!(is_scalar && is_q));
6925 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(FPROUNDING_ZERO));
6926 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
6927 tcg_fpstatus = get_fpstatus_ptr();
6928 tcg_shift = tcg_const_i32(fracbits);
6930 if (is_double) {
6931 int maxpass = is_scalar ? 1 : 2;
6933 for (pass = 0; pass < maxpass; pass++) {
6934 TCGv_i64 tcg_op = tcg_temp_new_i64();
6936 read_vec_element(s, tcg_op, rn, pass, MO_64);
6937 if (is_u) {
6938 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6939 } else {
6940 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6942 write_vec_element(s, tcg_op, rd, pass, MO_64);
6943 tcg_temp_free_i64(tcg_op);
6945 if (!is_q) {
6946 clear_vec_high(s, rd);
6948 } else {
6949 int maxpass = is_scalar ? 1 : is_q ? 4 : 2;
6950 for (pass = 0; pass < maxpass; pass++) {
6951 TCGv_i32 tcg_op = tcg_temp_new_i32();
6953 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
6954 if (is_u) {
6955 gen_helper_vfp_touls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6956 } else {
6957 gen_helper_vfp_tosls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6959 if (is_scalar) {
6960 write_fp_sreg(s, rd, tcg_op);
6961 } else {
6962 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
6964 tcg_temp_free_i32(tcg_op);
6966 if (!is_q && !is_scalar) {
6967 clear_vec_high(s, rd);
6971 tcg_temp_free_ptr(tcg_fpstatus);
6972 tcg_temp_free_i32(tcg_shift);
6973 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
6974 tcg_temp_free_i32(tcg_rmode);
6977 /* C3.6.9 AdvSIMD scalar shift by immediate
6978 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
6979 * +-----+---+-------------+------+------+--------+---+------+------+
6980 * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
6981 * +-----+---+-------------+------+------+--------+---+------+------+
6983 * This is the scalar version so it works on a fixed sized registers
6985 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
6987 int rd = extract32(insn, 0, 5);
6988 int rn = extract32(insn, 5, 5);
6989 int opcode = extract32(insn, 11, 5);
6990 int immb = extract32(insn, 16, 3);
6991 int immh = extract32(insn, 19, 4);
6992 bool is_u = extract32(insn, 29, 1);
6994 if (immh == 0) {
6995 unallocated_encoding(s);
6996 return;
6999 switch (opcode) {
7000 case 0x08: /* SRI */
7001 if (!is_u) {
7002 unallocated_encoding(s);
7003 return;
7005 /* fall through */
7006 case 0x00: /* SSHR / USHR */
7007 case 0x02: /* SSRA / USRA */
7008 case 0x04: /* SRSHR / URSHR */
7009 case 0x06: /* SRSRA / URSRA */
7010 handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
7011 break;
7012 case 0x0a: /* SHL / SLI */
7013 handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
7014 break;
7015 case 0x1c: /* SCVTF, UCVTF */
7016 handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
7017 opcode, rn, rd);
7018 break;
7019 case 0x10: /* SQSHRUN, SQSHRUN2 */
7020 case 0x11: /* SQRSHRUN, SQRSHRUN2 */
7021 if (!is_u) {
7022 unallocated_encoding(s);
7023 return;
7025 handle_vec_simd_sqshrn(s, true, false, false, true,
7026 immh, immb, opcode, rn, rd);
7027 break;
7028 case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
7029 case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
7030 handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
7031 immh, immb, opcode, rn, rd);
7032 break;
7033 case 0xc: /* SQSHLU */
7034 if (!is_u) {
7035 unallocated_encoding(s);
7036 return;
7038 handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
7039 break;
7040 case 0xe: /* SQSHL, UQSHL */
7041 handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
7042 break;
7043 case 0x1f: /* FCVTZS, FCVTZU */
7044 handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
7045 break;
7046 default:
7047 unallocated_encoding(s);
7048 break;
7052 /* C3.6.10 AdvSIMD scalar three different
7053 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
7054 * +-----+---+-----------+------+---+------+--------+-----+------+------+
7055 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
7056 * +-----+---+-----------+------+---+------+--------+-----+------+------+
7058 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
7060 bool is_u = extract32(insn, 29, 1);
7061 int size = extract32(insn, 22, 2);
7062 int opcode = extract32(insn, 12, 4);
7063 int rm = extract32(insn, 16, 5);
7064 int rn = extract32(insn, 5, 5);
7065 int rd = extract32(insn, 0, 5);
7067 if (is_u) {
7068 unallocated_encoding(s);
7069 return;
7072 switch (opcode) {
7073 case 0x9: /* SQDMLAL, SQDMLAL2 */
7074 case 0xb: /* SQDMLSL, SQDMLSL2 */
7075 case 0xd: /* SQDMULL, SQDMULL2 */
7076 if (size == 0 || size == 3) {
7077 unallocated_encoding(s);
7078 return;
7080 break;
7081 default:
7082 unallocated_encoding(s);
7083 return;
7086 if (!fp_access_check(s)) {
7087 return;
7090 if (size == 2) {
7091 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7092 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7093 TCGv_i64 tcg_res = tcg_temp_new_i64();
7095 read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
7096 read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
7098 tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
7099 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
7101 switch (opcode) {
7102 case 0xd: /* SQDMULL, SQDMULL2 */
7103 break;
7104 case 0xb: /* SQDMLSL, SQDMLSL2 */
7105 tcg_gen_neg_i64(tcg_res, tcg_res);
7106 /* fall through */
7107 case 0x9: /* SQDMLAL, SQDMLAL2 */
7108 read_vec_element(s, tcg_op1, rd, 0, MO_64);
7109 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
7110 tcg_res, tcg_op1);
7111 break;
7112 default:
7113 g_assert_not_reached();
7116 write_fp_dreg(s, rd, tcg_res);
7118 tcg_temp_free_i64(tcg_op1);
7119 tcg_temp_free_i64(tcg_op2);
7120 tcg_temp_free_i64(tcg_res);
7121 } else {
7122 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7123 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7124 TCGv_i64 tcg_res = tcg_temp_new_i64();
7126 read_vec_element_i32(s, tcg_op1, rn, 0, MO_16);
7127 read_vec_element_i32(s, tcg_op2, rm, 0, MO_16);
7129 gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
7130 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
7132 switch (opcode) {
7133 case 0xd: /* SQDMULL, SQDMULL2 */
7134 break;
7135 case 0xb: /* SQDMLSL, SQDMLSL2 */
7136 gen_helper_neon_negl_u32(tcg_res, tcg_res);
7137 /* fall through */
7138 case 0x9: /* SQDMLAL, SQDMLAL2 */
7140 TCGv_i64 tcg_op3 = tcg_temp_new_i64();
7141 read_vec_element(s, tcg_op3, rd, 0, MO_32);
7142 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
7143 tcg_res, tcg_op3);
7144 tcg_temp_free_i64(tcg_op3);
7145 break;
7147 default:
7148 g_assert_not_reached();
7151 tcg_gen_ext32u_i64(tcg_res, tcg_res);
7152 write_fp_dreg(s, rd, tcg_res);
7154 tcg_temp_free_i32(tcg_op1);
7155 tcg_temp_free_i32(tcg_op2);
7156 tcg_temp_free_i64(tcg_res);
7160 static void handle_3same_64(DisasContext *s, int opcode, bool u,
7161 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
7163 /* Handle 64x64->64 opcodes which are shared between the scalar
7164 * and vector 3-same groups. We cover every opcode where size == 3
7165 * is valid in either the three-reg-same (integer, not pairwise)
7166 * or scalar-three-reg-same groups. (Some opcodes are not yet
7167 * implemented.)
7169 TCGCond cond;
7171 switch (opcode) {
7172 case 0x1: /* SQADD */
7173 if (u) {
7174 gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7175 } else {
7176 gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7178 break;
7179 case 0x5: /* SQSUB */
7180 if (u) {
7181 gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7182 } else {
7183 gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7185 break;
7186 case 0x6: /* CMGT, CMHI */
7187 /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
7188 * We implement this using setcond (test) and then negating.
7190 cond = u ? TCG_COND_GTU : TCG_COND_GT;
7191 do_cmop:
7192 tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
7193 tcg_gen_neg_i64(tcg_rd, tcg_rd);
7194 break;
7195 case 0x7: /* CMGE, CMHS */
7196 cond = u ? TCG_COND_GEU : TCG_COND_GE;
7197 goto do_cmop;
7198 case 0x11: /* CMTST, CMEQ */
7199 if (u) {
7200 cond = TCG_COND_EQ;
7201 goto do_cmop;
7203 /* CMTST : test is "if (X & Y != 0)". */
7204 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
7205 tcg_gen_setcondi_i64(TCG_COND_NE, tcg_rd, tcg_rd, 0);
7206 tcg_gen_neg_i64(tcg_rd, tcg_rd);
7207 break;
7208 case 0x8: /* SSHL, USHL */
7209 if (u) {
7210 gen_helper_neon_shl_u64(tcg_rd, tcg_rn, tcg_rm);
7211 } else {
7212 gen_helper_neon_shl_s64(tcg_rd, tcg_rn, tcg_rm);
7214 break;
7215 case 0x9: /* SQSHL, UQSHL */
7216 if (u) {
7217 gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7218 } else {
7219 gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7221 break;
7222 case 0xa: /* SRSHL, URSHL */
7223 if (u) {
7224 gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
7225 } else {
7226 gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
7228 break;
7229 case 0xb: /* SQRSHL, UQRSHL */
7230 if (u) {
7231 gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7232 } else {
7233 gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7235 break;
7236 case 0x10: /* ADD, SUB */
7237 if (u) {
7238 tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
7239 } else {
7240 tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
7242 break;
7243 default:
7244 g_assert_not_reached();
7248 /* Handle the 3-same-operands float operations; shared by the scalar
7249 * and vector encodings. The caller must filter out any encodings
7250 * not allocated for the encoding it is dealing with.
7252 static void handle_3same_float(DisasContext *s, int size, int elements,
7253 int fpopcode, int rd, int rn, int rm)
7255 int pass;
7256 TCGv_ptr fpst = get_fpstatus_ptr();
7258 for (pass = 0; pass < elements; pass++) {
7259 if (size) {
7260 /* Double */
7261 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7262 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7263 TCGv_i64 tcg_res = tcg_temp_new_i64();
7265 read_vec_element(s, tcg_op1, rn, pass, MO_64);
7266 read_vec_element(s, tcg_op2, rm, pass, MO_64);
7268 switch (fpopcode) {
7269 case 0x39: /* FMLS */
7270 /* As usual for ARM, separate negation for fused multiply-add */
7271 gen_helper_vfp_negd(tcg_op1, tcg_op1);
7272 /* fall through */
7273 case 0x19: /* FMLA */
7274 read_vec_element(s, tcg_res, rd, pass, MO_64);
7275 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
7276 tcg_res, fpst);
7277 break;
7278 case 0x18: /* FMAXNM */
7279 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7280 break;
7281 case 0x1a: /* FADD */
7282 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
7283 break;
7284 case 0x1b: /* FMULX */
7285 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
7286 break;
7287 case 0x1c: /* FCMEQ */
7288 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7289 break;
7290 case 0x1e: /* FMAX */
7291 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
7292 break;
7293 case 0x1f: /* FRECPS */
7294 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7295 break;
7296 case 0x38: /* FMINNM */
7297 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7298 break;
7299 case 0x3a: /* FSUB */
7300 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
7301 break;
7302 case 0x3e: /* FMIN */
7303 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
7304 break;
7305 case 0x3f: /* FRSQRTS */
7306 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7307 break;
7308 case 0x5b: /* FMUL */
7309 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
7310 break;
7311 case 0x5c: /* FCMGE */
7312 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7313 break;
7314 case 0x5d: /* FACGE */
7315 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7316 break;
7317 case 0x5f: /* FDIV */
7318 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
7319 break;
7320 case 0x7a: /* FABD */
7321 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
7322 gen_helper_vfp_absd(tcg_res, tcg_res);
7323 break;
7324 case 0x7c: /* FCMGT */
7325 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7326 break;
7327 case 0x7d: /* FACGT */
7328 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7329 break;
7330 default:
7331 g_assert_not_reached();
7334 write_vec_element(s, tcg_res, rd, pass, MO_64);
7336 tcg_temp_free_i64(tcg_res);
7337 tcg_temp_free_i64(tcg_op1);
7338 tcg_temp_free_i64(tcg_op2);
7339 } else {
7340 /* Single */
7341 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7342 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7343 TCGv_i32 tcg_res = tcg_temp_new_i32();
7345 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
7346 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
7348 switch (fpopcode) {
7349 case 0x39: /* FMLS */
7350 /* As usual for ARM, separate negation for fused multiply-add */
7351 gen_helper_vfp_negs(tcg_op1, tcg_op1);
7352 /* fall through */
7353 case 0x19: /* FMLA */
7354 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7355 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
7356 tcg_res, fpst);
7357 break;
7358 case 0x1a: /* FADD */
7359 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
7360 break;
7361 case 0x1b: /* FMULX */
7362 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
7363 break;
7364 case 0x1c: /* FCMEQ */
7365 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7366 break;
7367 case 0x1e: /* FMAX */
7368 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
7369 break;
7370 case 0x1f: /* FRECPS */
7371 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7372 break;
7373 case 0x18: /* FMAXNM */
7374 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
7375 break;
7376 case 0x38: /* FMINNM */
7377 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
7378 break;
7379 case 0x3a: /* FSUB */
7380 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
7381 break;
7382 case 0x3e: /* FMIN */
7383 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
7384 break;
7385 case 0x3f: /* FRSQRTS */
7386 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7387 break;
7388 case 0x5b: /* FMUL */
7389 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
7390 break;
7391 case 0x5c: /* FCMGE */
7392 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7393 break;
7394 case 0x5d: /* FACGE */
7395 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7396 break;
7397 case 0x5f: /* FDIV */
7398 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
7399 break;
7400 case 0x7a: /* FABD */
7401 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
7402 gen_helper_vfp_abss(tcg_res, tcg_res);
7403 break;
7404 case 0x7c: /* FCMGT */
7405 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7406 break;
7407 case 0x7d: /* FACGT */
7408 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7409 break;
7410 default:
7411 g_assert_not_reached();
7414 if (elements == 1) {
7415 /* scalar single so clear high part */
7416 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7418 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
7419 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
7420 tcg_temp_free_i64(tcg_tmp);
7421 } else {
7422 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7425 tcg_temp_free_i32(tcg_res);
7426 tcg_temp_free_i32(tcg_op1);
7427 tcg_temp_free_i32(tcg_op2);
7431 tcg_temp_free_ptr(fpst);
7433 if ((elements << size) < 4) {
7434 /* scalar, or non-quad vector op */
7435 clear_vec_high(s, rd);
7439 /* C3.6.11 AdvSIMD scalar three same
7440 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
7441 * +-----+---+-----------+------+---+------+--------+---+------+------+
7442 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
7443 * +-----+---+-----------+------+---+------+--------+---+------+------+
7445 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
7447 int rd = extract32(insn, 0, 5);
7448 int rn = extract32(insn, 5, 5);
7449 int opcode = extract32(insn, 11, 5);
7450 int rm = extract32(insn, 16, 5);
7451 int size = extract32(insn, 22, 2);
7452 bool u = extract32(insn, 29, 1);
7453 TCGv_i64 tcg_rd;
7455 if (opcode >= 0x18) {
7456 /* Floating point: U, size[1] and opcode indicate operation */
7457 int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
7458 switch (fpopcode) {
7459 case 0x1b: /* FMULX */
7460 case 0x1f: /* FRECPS */
7461 case 0x3f: /* FRSQRTS */
7462 case 0x5d: /* FACGE */
7463 case 0x7d: /* FACGT */
7464 case 0x1c: /* FCMEQ */
7465 case 0x5c: /* FCMGE */
7466 case 0x7c: /* FCMGT */
7467 case 0x7a: /* FABD */
7468 break;
7469 default:
7470 unallocated_encoding(s);
7471 return;
7474 if (!fp_access_check(s)) {
7475 return;
7478 handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
7479 return;
7482 switch (opcode) {
7483 case 0x1: /* SQADD, UQADD */
7484 case 0x5: /* SQSUB, UQSUB */
7485 case 0x9: /* SQSHL, UQSHL */
7486 case 0xb: /* SQRSHL, UQRSHL */
7487 break;
7488 case 0x8: /* SSHL, USHL */
7489 case 0xa: /* SRSHL, URSHL */
7490 case 0x6: /* CMGT, CMHI */
7491 case 0x7: /* CMGE, CMHS */
7492 case 0x11: /* CMTST, CMEQ */
7493 case 0x10: /* ADD, SUB (vector) */
7494 if (size != 3) {
7495 unallocated_encoding(s);
7496 return;
7498 break;
7499 case 0x16: /* SQDMULH, SQRDMULH (vector) */
7500 if (size != 1 && size != 2) {
7501 unallocated_encoding(s);
7502 return;
7504 break;
7505 default:
7506 unallocated_encoding(s);
7507 return;
7510 if (!fp_access_check(s)) {
7511 return;
7514 tcg_rd = tcg_temp_new_i64();
7516 if (size == 3) {
7517 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
7518 TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
7520 handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
7521 tcg_temp_free_i64(tcg_rn);
7522 tcg_temp_free_i64(tcg_rm);
7523 } else {
7524 /* Do a single operation on the lowest element in the vector.
7525 * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
7526 * no side effects for all these operations.
7527 * OPTME: special-purpose helpers would avoid doing some
7528 * unnecessary work in the helper for the 8 and 16 bit cases.
7530 NeonGenTwoOpEnvFn *genenvfn;
7531 TCGv_i32 tcg_rn = tcg_temp_new_i32();
7532 TCGv_i32 tcg_rm = tcg_temp_new_i32();
7533 TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
7535 read_vec_element_i32(s, tcg_rn, rn, 0, size);
7536 read_vec_element_i32(s, tcg_rm, rm, 0, size);
7538 switch (opcode) {
7539 case 0x1: /* SQADD, UQADD */
7541 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7542 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
7543 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
7544 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
7546 genenvfn = fns[size][u];
7547 break;
7549 case 0x5: /* SQSUB, UQSUB */
7551 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7552 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
7553 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
7554 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
7556 genenvfn = fns[size][u];
7557 break;
7559 case 0x9: /* SQSHL, UQSHL */
7561 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7562 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
7563 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
7564 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
7566 genenvfn = fns[size][u];
7567 break;
7569 case 0xb: /* SQRSHL, UQRSHL */
7571 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7572 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
7573 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
7574 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
7576 genenvfn = fns[size][u];
7577 break;
7579 case 0x16: /* SQDMULH, SQRDMULH */
7581 static NeonGenTwoOpEnvFn * const fns[2][2] = {
7582 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
7583 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
7585 assert(size == 1 || size == 2);
7586 genenvfn = fns[size - 1][u];
7587 break;
7589 default:
7590 g_assert_not_reached();
7593 genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
7594 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
7595 tcg_temp_free_i32(tcg_rd32);
7596 tcg_temp_free_i32(tcg_rn);
7597 tcg_temp_free_i32(tcg_rm);
7600 write_fp_dreg(s, rd, tcg_rd);
7602 tcg_temp_free_i64(tcg_rd);
7605 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
7606 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
7607 TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
7609 /* Handle 64->64 opcodes which are shared between the scalar and
7610 * vector 2-reg-misc groups. We cover every integer opcode where size == 3
7611 * is valid in either group and also the double-precision fp ops.
7612 * The caller only need provide tcg_rmode and tcg_fpstatus if the op
7613 * requires them.
7615 TCGCond cond;
7617 switch (opcode) {
7618 case 0x4: /* CLS, CLZ */
7619 if (u) {
7620 gen_helper_clz64(tcg_rd, tcg_rn);
7621 } else {
7622 gen_helper_cls64(tcg_rd, tcg_rn);
7624 break;
7625 case 0x5: /* NOT */
7626 /* This opcode is shared with CNT and RBIT but we have earlier
7627 * enforced that size == 3 if and only if this is the NOT insn.
7629 tcg_gen_not_i64(tcg_rd, tcg_rn);
7630 break;
7631 case 0x7: /* SQABS, SQNEG */
7632 if (u) {
7633 gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
7634 } else {
7635 gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
7637 break;
7638 case 0xa: /* CMLT */
7639 /* 64 bit integer comparison against zero, result is
7640 * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
7641 * subtracting 1.
7643 cond = TCG_COND_LT;
7644 do_cmop:
7645 tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
7646 tcg_gen_neg_i64(tcg_rd, tcg_rd);
7647 break;
7648 case 0x8: /* CMGT, CMGE */
7649 cond = u ? TCG_COND_GE : TCG_COND_GT;
7650 goto do_cmop;
7651 case 0x9: /* CMEQ, CMLE */
7652 cond = u ? TCG_COND_LE : TCG_COND_EQ;
7653 goto do_cmop;
7654 case 0xb: /* ABS, NEG */
7655 if (u) {
7656 tcg_gen_neg_i64(tcg_rd, tcg_rn);
7657 } else {
7658 TCGv_i64 tcg_zero = tcg_const_i64(0);
7659 tcg_gen_neg_i64(tcg_rd, tcg_rn);
7660 tcg_gen_movcond_i64(TCG_COND_GT, tcg_rd, tcg_rn, tcg_zero,
7661 tcg_rn, tcg_rd);
7662 tcg_temp_free_i64(tcg_zero);
7664 break;
7665 case 0x2f: /* FABS */
7666 gen_helper_vfp_absd(tcg_rd, tcg_rn);
7667 break;
7668 case 0x6f: /* FNEG */
7669 gen_helper_vfp_negd(tcg_rd, tcg_rn);
7670 break;
7671 case 0x7f: /* FSQRT */
7672 gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
7673 break;
7674 case 0x1a: /* FCVTNS */
7675 case 0x1b: /* FCVTMS */
7676 case 0x1c: /* FCVTAS */
7677 case 0x3a: /* FCVTPS */
7678 case 0x3b: /* FCVTZS */
7680 TCGv_i32 tcg_shift = tcg_const_i32(0);
7681 gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7682 tcg_temp_free_i32(tcg_shift);
7683 break;
7685 case 0x5a: /* FCVTNU */
7686 case 0x5b: /* FCVTMU */
7687 case 0x5c: /* FCVTAU */
7688 case 0x7a: /* FCVTPU */
7689 case 0x7b: /* FCVTZU */
7691 TCGv_i32 tcg_shift = tcg_const_i32(0);
7692 gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7693 tcg_temp_free_i32(tcg_shift);
7694 break;
7696 case 0x18: /* FRINTN */
7697 case 0x19: /* FRINTM */
7698 case 0x38: /* FRINTP */
7699 case 0x39: /* FRINTZ */
7700 case 0x58: /* FRINTA */
7701 case 0x79: /* FRINTI */
7702 gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
7703 break;
7704 case 0x59: /* FRINTX */
7705 gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
7706 break;
7707 default:
7708 g_assert_not_reached();
7712 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
7713 bool is_scalar, bool is_u, bool is_q,
7714 int size, int rn, int rd)
7716 bool is_double = (size == 3);
7717 TCGv_ptr fpst;
7719 if (!fp_access_check(s)) {
7720 return;
7723 fpst = get_fpstatus_ptr();
7725 if (is_double) {
7726 TCGv_i64 tcg_op = tcg_temp_new_i64();
7727 TCGv_i64 tcg_zero = tcg_const_i64(0);
7728 TCGv_i64 tcg_res = tcg_temp_new_i64();
7729 NeonGenTwoDoubleOPFn *genfn;
7730 bool swap = false;
7731 int pass;
7733 switch (opcode) {
7734 case 0x2e: /* FCMLT (zero) */
7735 swap = true;
7736 /* fallthrough */
7737 case 0x2c: /* FCMGT (zero) */
7738 genfn = gen_helper_neon_cgt_f64;
7739 break;
7740 case 0x2d: /* FCMEQ (zero) */
7741 genfn = gen_helper_neon_ceq_f64;
7742 break;
7743 case 0x6d: /* FCMLE (zero) */
7744 swap = true;
7745 /* fall through */
7746 case 0x6c: /* FCMGE (zero) */
7747 genfn = gen_helper_neon_cge_f64;
7748 break;
7749 default:
7750 g_assert_not_reached();
7753 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7754 read_vec_element(s, tcg_op, rn, pass, MO_64);
7755 if (swap) {
7756 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7757 } else {
7758 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7760 write_vec_element(s, tcg_res, rd, pass, MO_64);
7762 if (is_scalar) {
7763 clear_vec_high(s, rd);
7766 tcg_temp_free_i64(tcg_res);
7767 tcg_temp_free_i64(tcg_zero);
7768 tcg_temp_free_i64(tcg_op);
7769 } else {
7770 TCGv_i32 tcg_op = tcg_temp_new_i32();
7771 TCGv_i32 tcg_zero = tcg_const_i32(0);
7772 TCGv_i32 tcg_res = tcg_temp_new_i32();
7773 NeonGenTwoSingleOPFn *genfn;
7774 bool swap = false;
7775 int pass, maxpasses;
7777 switch (opcode) {
7778 case 0x2e: /* FCMLT (zero) */
7779 swap = true;
7780 /* fall through */
7781 case 0x2c: /* FCMGT (zero) */
7782 genfn = gen_helper_neon_cgt_f32;
7783 break;
7784 case 0x2d: /* FCMEQ (zero) */
7785 genfn = gen_helper_neon_ceq_f32;
7786 break;
7787 case 0x6d: /* FCMLE (zero) */
7788 swap = true;
7789 /* fall through */
7790 case 0x6c: /* FCMGE (zero) */
7791 genfn = gen_helper_neon_cge_f32;
7792 break;
7793 default:
7794 g_assert_not_reached();
7797 if (is_scalar) {
7798 maxpasses = 1;
7799 } else {
7800 maxpasses = is_q ? 4 : 2;
7803 for (pass = 0; pass < maxpasses; pass++) {
7804 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7805 if (swap) {
7806 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7807 } else {
7808 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7810 if (is_scalar) {
7811 write_fp_sreg(s, rd, tcg_res);
7812 } else {
7813 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7816 tcg_temp_free_i32(tcg_res);
7817 tcg_temp_free_i32(tcg_zero);
7818 tcg_temp_free_i32(tcg_op);
7819 if (!is_q && !is_scalar) {
7820 clear_vec_high(s, rd);
7824 tcg_temp_free_ptr(fpst);
7827 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
7828 bool is_scalar, bool is_u, bool is_q,
7829 int size, int rn, int rd)
7831 bool is_double = (size == 3);
7832 TCGv_ptr fpst = get_fpstatus_ptr();
7834 if (is_double) {
7835 TCGv_i64 tcg_op = tcg_temp_new_i64();
7836 TCGv_i64 tcg_res = tcg_temp_new_i64();
7837 int pass;
7839 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7840 read_vec_element(s, tcg_op, rn, pass, MO_64);
7841 switch (opcode) {
7842 case 0x3d: /* FRECPE */
7843 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
7844 break;
7845 case 0x3f: /* FRECPX */
7846 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
7847 break;
7848 case 0x7d: /* FRSQRTE */
7849 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
7850 break;
7851 default:
7852 g_assert_not_reached();
7854 write_vec_element(s, tcg_res, rd, pass, MO_64);
7856 if (is_scalar) {
7857 clear_vec_high(s, rd);
7860 tcg_temp_free_i64(tcg_res);
7861 tcg_temp_free_i64(tcg_op);
7862 } else {
7863 TCGv_i32 tcg_op = tcg_temp_new_i32();
7864 TCGv_i32 tcg_res = tcg_temp_new_i32();
7865 int pass, maxpasses;
7867 if (is_scalar) {
7868 maxpasses = 1;
7869 } else {
7870 maxpasses = is_q ? 4 : 2;
7873 for (pass = 0; pass < maxpasses; pass++) {
7874 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7876 switch (opcode) {
7877 case 0x3c: /* URECPE */
7878 gen_helper_recpe_u32(tcg_res, tcg_op, fpst);
7879 break;
7880 case 0x3d: /* FRECPE */
7881 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
7882 break;
7883 case 0x3f: /* FRECPX */
7884 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
7885 break;
7886 case 0x7d: /* FRSQRTE */
7887 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
7888 break;
7889 default:
7890 g_assert_not_reached();
7893 if (is_scalar) {
7894 write_fp_sreg(s, rd, tcg_res);
7895 } else {
7896 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7899 tcg_temp_free_i32(tcg_res);
7900 tcg_temp_free_i32(tcg_op);
7901 if (!is_q && !is_scalar) {
7902 clear_vec_high(s, rd);
7905 tcg_temp_free_ptr(fpst);
7908 static void handle_2misc_narrow(DisasContext *s, bool scalar,
7909 int opcode, bool u, bool is_q,
7910 int size, int rn, int rd)
7912 /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
7913 * in the source becomes a size element in the destination).
7915 int pass;
7916 TCGv_i32 tcg_res[2];
7917 int destelt = is_q ? 2 : 0;
7918 int passes = scalar ? 1 : 2;
7920 if (scalar) {
7921 tcg_res[1] = tcg_const_i32(0);
7924 for (pass = 0; pass < passes; pass++) {
7925 TCGv_i64 tcg_op = tcg_temp_new_i64();
7926 NeonGenNarrowFn *genfn = NULL;
7927 NeonGenNarrowEnvFn *genenvfn = NULL;
7929 if (scalar) {
7930 read_vec_element(s, tcg_op, rn, pass, size + 1);
7931 } else {
7932 read_vec_element(s, tcg_op, rn, pass, MO_64);
7934 tcg_res[pass] = tcg_temp_new_i32();
7936 switch (opcode) {
7937 case 0x12: /* XTN, SQXTUN */
7939 static NeonGenNarrowFn * const xtnfns[3] = {
7940 gen_helper_neon_narrow_u8,
7941 gen_helper_neon_narrow_u16,
7942 tcg_gen_extrl_i64_i32,
7944 static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
7945 gen_helper_neon_unarrow_sat8,
7946 gen_helper_neon_unarrow_sat16,
7947 gen_helper_neon_unarrow_sat32,
7949 if (u) {
7950 genenvfn = sqxtunfns[size];
7951 } else {
7952 genfn = xtnfns[size];
7954 break;
7956 case 0x14: /* SQXTN, UQXTN */
7958 static NeonGenNarrowEnvFn * const fns[3][2] = {
7959 { gen_helper_neon_narrow_sat_s8,
7960 gen_helper_neon_narrow_sat_u8 },
7961 { gen_helper_neon_narrow_sat_s16,
7962 gen_helper_neon_narrow_sat_u16 },
7963 { gen_helper_neon_narrow_sat_s32,
7964 gen_helper_neon_narrow_sat_u32 },
7966 genenvfn = fns[size][u];
7967 break;
7969 case 0x16: /* FCVTN, FCVTN2 */
7970 /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
7971 if (size == 2) {
7972 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
7973 } else {
7974 TCGv_i32 tcg_lo = tcg_temp_new_i32();
7975 TCGv_i32 tcg_hi = tcg_temp_new_i32();
7976 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
7977 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, cpu_env);
7978 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, cpu_env);
7979 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
7980 tcg_temp_free_i32(tcg_lo);
7981 tcg_temp_free_i32(tcg_hi);
7983 break;
7984 case 0x56: /* FCVTXN, FCVTXN2 */
7985 /* 64 bit to 32 bit float conversion
7986 * with von Neumann rounding (round to odd)
7988 assert(size == 2);
7989 gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
7990 break;
7991 default:
7992 g_assert_not_reached();
7995 if (genfn) {
7996 genfn(tcg_res[pass], tcg_op);
7997 } else if (genenvfn) {
7998 genenvfn(tcg_res[pass], cpu_env, tcg_op);
8001 tcg_temp_free_i64(tcg_op);
8004 for (pass = 0; pass < 2; pass++) {
8005 write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
8006 tcg_temp_free_i32(tcg_res[pass]);
8008 if (!is_q) {
8009 clear_vec_high(s, rd);
8013 /* Remaining saturating accumulating ops */
8014 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
8015 bool is_q, int size, int rn, int rd)
8017 bool is_double = (size == 3);
8019 if (is_double) {
8020 TCGv_i64 tcg_rn = tcg_temp_new_i64();
8021 TCGv_i64 tcg_rd = tcg_temp_new_i64();
8022 int pass;
8024 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
8025 read_vec_element(s, tcg_rn, rn, pass, MO_64);
8026 read_vec_element(s, tcg_rd, rd, pass, MO_64);
8028 if (is_u) { /* USQADD */
8029 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8030 } else { /* SUQADD */
8031 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8033 write_vec_element(s, tcg_rd, rd, pass, MO_64);
8035 if (is_scalar) {
8036 clear_vec_high(s, rd);
8039 tcg_temp_free_i64(tcg_rd);
8040 tcg_temp_free_i64(tcg_rn);
8041 } else {
8042 TCGv_i32 tcg_rn = tcg_temp_new_i32();
8043 TCGv_i32 tcg_rd = tcg_temp_new_i32();
8044 int pass, maxpasses;
8046 if (is_scalar) {
8047 maxpasses = 1;
8048 } else {
8049 maxpasses = is_q ? 4 : 2;
8052 for (pass = 0; pass < maxpasses; pass++) {
8053 if (is_scalar) {
8054 read_vec_element_i32(s, tcg_rn, rn, pass, size);
8055 read_vec_element_i32(s, tcg_rd, rd, pass, size);
8056 } else {
8057 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
8058 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
8061 if (is_u) { /* USQADD */
8062 switch (size) {
8063 case 0:
8064 gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8065 break;
8066 case 1:
8067 gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8068 break;
8069 case 2:
8070 gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8071 break;
8072 default:
8073 g_assert_not_reached();
8075 } else { /* SUQADD */
8076 switch (size) {
8077 case 0:
8078 gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8079 break;
8080 case 1:
8081 gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8082 break;
8083 case 2:
8084 gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8085 break;
8086 default:
8087 g_assert_not_reached();
8091 if (is_scalar) {
8092 TCGv_i64 tcg_zero = tcg_const_i64(0);
8093 write_vec_element(s, tcg_zero, rd, 0, MO_64);
8094 tcg_temp_free_i64(tcg_zero);
8096 write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
8099 if (!is_q) {
8100 clear_vec_high(s, rd);
8103 tcg_temp_free_i32(tcg_rd);
8104 tcg_temp_free_i32(tcg_rn);
8108 /* C3.6.12 AdvSIMD scalar two reg misc
8109 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
8110 * +-----+---+-----------+------+-----------+--------+-----+------+------+
8111 * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
8112 * +-----+---+-----------+------+-----------+--------+-----+------+------+
8114 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
8116 int rd = extract32(insn, 0, 5);
8117 int rn = extract32(insn, 5, 5);
8118 int opcode = extract32(insn, 12, 5);
8119 int size = extract32(insn, 22, 2);
8120 bool u = extract32(insn, 29, 1);
8121 bool is_fcvt = false;
8122 int rmode;
8123 TCGv_i32 tcg_rmode;
8124 TCGv_ptr tcg_fpstatus;
8126 switch (opcode) {
8127 case 0x3: /* USQADD / SUQADD*/
8128 if (!fp_access_check(s)) {
8129 return;
8131 handle_2misc_satacc(s, true, u, false, size, rn, rd);
8132 return;
8133 case 0x7: /* SQABS / SQNEG */
8134 break;
8135 case 0xa: /* CMLT */
8136 if (u) {
8137 unallocated_encoding(s);
8138 return;
8140 /* fall through */
8141 case 0x8: /* CMGT, CMGE */
8142 case 0x9: /* CMEQ, CMLE */
8143 case 0xb: /* ABS, NEG */
8144 if (size != 3) {
8145 unallocated_encoding(s);
8146 return;
8148 break;
8149 case 0x12: /* SQXTUN */
8150 if (!u) {
8151 unallocated_encoding(s);
8152 return;
8154 /* fall through */
8155 case 0x14: /* SQXTN, UQXTN */
8156 if (size == 3) {
8157 unallocated_encoding(s);
8158 return;
8160 if (!fp_access_check(s)) {
8161 return;
8163 handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
8164 return;
8165 case 0xc ... 0xf:
8166 case 0x16 ... 0x1d:
8167 case 0x1f:
8168 /* Floating point: U, size[1] and opcode indicate operation;
8169 * size[0] indicates single or double precision.
8171 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
8172 size = extract32(size, 0, 1) ? 3 : 2;
8173 switch (opcode) {
8174 case 0x2c: /* FCMGT (zero) */
8175 case 0x2d: /* FCMEQ (zero) */
8176 case 0x2e: /* FCMLT (zero) */
8177 case 0x6c: /* FCMGE (zero) */
8178 case 0x6d: /* FCMLE (zero) */
8179 handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
8180 return;
8181 case 0x1d: /* SCVTF */
8182 case 0x5d: /* UCVTF */
8184 bool is_signed = (opcode == 0x1d);
8185 if (!fp_access_check(s)) {
8186 return;
8188 handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
8189 return;
8191 case 0x3d: /* FRECPE */
8192 case 0x3f: /* FRECPX */
8193 case 0x7d: /* FRSQRTE */
8194 if (!fp_access_check(s)) {
8195 return;
8197 handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
8198 return;
8199 case 0x1a: /* FCVTNS */
8200 case 0x1b: /* FCVTMS */
8201 case 0x3a: /* FCVTPS */
8202 case 0x3b: /* FCVTZS */
8203 case 0x5a: /* FCVTNU */
8204 case 0x5b: /* FCVTMU */
8205 case 0x7a: /* FCVTPU */
8206 case 0x7b: /* FCVTZU */
8207 is_fcvt = true;
8208 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
8209 break;
8210 case 0x1c: /* FCVTAS */
8211 case 0x5c: /* FCVTAU */
8212 /* TIEAWAY doesn't fit in the usual rounding mode encoding */
8213 is_fcvt = true;
8214 rmode = FPROUNDING_TIEAWAY;
8215 break;
8216 case 0x56: /* FCVTXN, FCVTXN2 */
8217 if (size == 2) {
8218 unallocated_encoding(s);
8219 return;
8221 if (!fp_access_check(s)) {
8222 return;
8224 handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
8225 return;
8226 default:
8227 unallocated_encoding(s);
8228 return;
8230 break;
8231 default:
8232 unallocated_encoding(s);
8233 return;
8236 if (!fp_access_check(s)) {
8237 return;
8240 if (is_fcvt) {
8241 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
8242 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
8243 tcg_fpstatus = get_fpstatus_ptr();
8244 } else {
8245 TCGV_UNUSED_I32(tcg_rmode);
8246 TCGV_UNUSED_PTR(tcg_fpstatus);
8249 if (size == 3) {
8250 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
8251 TCGv_i64 tcg_rd = tcg_temp_new_i64();
8253 handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
8254 write_fp_dreg(s, rd, tcg_rd);
8255 tcg_temp_free_i64(tcg_rd);
8256 tcg_temp_free_i64(tcg_rn);
8257 } else {
8258 TCGv_i32 tcg_rn = tcg_temp_new_i32();
8259 TCGv_i32 tcg_rd = tcg_temp_new_i32();
8261 read_vec_element_i32(s, tcg_rn, rn, 0, size);
8263 switch (opcode) {
8264 case 0x7: /* SQABS, SQNEG */
8266 NeonGenOneOpEnvFn *genfn;
8267 static NeonGenOneOpEnvFn * const fns[3][2] = {
8268 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
8269 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
8270 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
8272 genfn = fns[size][u];
8273 genfn(tcg_rd, cpu_env, tcg_rn);
8274 break;
8276 case 0x1a: /* FCVTNS */
8277 case 0x1b: /* FCVTMS */
8278 case 0x1c: /* FCVTAS */
8279 case 0x3a: /* FCVTPS */
8280 case 0x3b: /* FCVTZS */
8282 TCGv_i32 tcg_shift = tcg_const_i32(0);
8283 gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
8284 tcg_temp_free_i32(tcg_shift);
8285 break;
8287 case 0x5a: /* FCVTNU */
8288 case 0x5b: /* FCVTMU */
8289 case 0x5c: /* FCVTAU */
8290 case 0x7a: /* FCVTPU */
8291 case 0x7b: /* FCVTZU */
8293 TCGv_i32 tcg_shift = tcg_const_i32(0);
8294 gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
8295 tcg_temp_free_i32(tcg_shift);
8296 break;
8298 default:
8299 g_assert_not_reached();
8302 write_fp_sreg(s, rd, tcg_rd);
8303 tcg_temp_free_i32(tcg_rd);
8304 tcg_temp_free_i32(tcg_rn);
8307 if (is_fcvt) {
8308 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
8309 tcg_temp_free_i32(tcg_rmode);
8310 tcg_temp_free_ptr(tcg_fpstatus);
8314 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
8315 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
8316 int immh, int immb, int opcode, int rn, int rd)
8318 int size = 32 - clz32(immh) - 1;
8319 int immhb = immh << 3 | immb;
8320 int shift = 2 * (8 << size) - immhb;
8321 bool accumulate = false;
8322 bool round = false;
8323 bool insert = false;
8324 int dsize = is_q ? 128 : 64;
8325 int esize = 8 << size;
8326 int elements = dsize/esize;
8327 TCGMemOp memop = size | (is_u ? 0 : MO_SIGN);
8328 TCGv_i64 tcg_rn = new_tmp_a64(s);
8329 TCGv_i64 tcg_rd = new_tmp_a64(s);
8330 TCGv_i64 tcg_round;
8331 int i;
8333 if (extract32(immh, 3, 1) && !is_q) {
8334 unallocated_encoding(s);
8335 return;
8338 if (size > 3 && !is_q) {
8339 unallocated_encoding(s);
8340 return;
8343 if (!fp_access_check(s)) {
8344 return;
8347 switch (opcode) {
8348 case 0x02: /* SSRA / USRA (accumulate) */
8349 accumulate = true;
8350 break;
8351 case 0x04: /* SRSHR / URSHR (rounding) */
8352 round = true;
8353 break;
8354 case 0x06: /* SRSRA / URSRA (accum + rounding) */
8355 accumulate = round = true;
8356 break;
8357 case 0x08: /* SRI */
8358 insert = true;
8359 break;
8362 if (round) {
8363 uint64_t round_const = 1ULL << (shift - 1);
8364 tcg_round = tcg_const_i64(round_const);
8365 } else {
8366 TCGV_UNUSED_I64(tcg_round);
8369 for (i = 0; i < elements; i++) {
8370 read_vec_element(s, tcg_rn, rn, i, memop);
8371 if (accumulate || insert) {
8372 read_vec_element(s, tcg_rd, rd, i, memop);
8375 if (insert) {
8376 handle_shri_with_ins(tcg_rd, tcg_rn, size, shift);
8377 } else {
8378 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8379 accumulate, is_u, size, shift);
8382 write_vec_element(s, tcg_rd, rd, i, size);
8385 if (!is_q) {
8386 clear_vec_high(s, rd);
8389 if (round) {
8390 tcg_temp_free_i64(tcg_round);
8394 /* SHL/SLI - Vector shift left */
8395 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
8396 int immh, int immb, int opcode, int rn, int rd)
8398 int size = 32 - clz32(immh) - 1;
8399 int immhb = immh << 3 | immb;
8400 int shift = immhb - (8 << size);
8401 int dsize = is_q ? 128 : 64;
8402 int esize = 8 << size;
8403 int elements = dsize/esize;
8404 TCGv_i64 tcg_rn = new_tmp_a64(s);
8405 TCGv_i64 tcg_rd = new_tmp_a64(s);
8406 int i;
8408 if (extract32(immh, 3, 1) && !is_q) {
8409 unallocated_encoding(s);
8410 return;
8413 if (size > 3 && !is_q) {
8414 unallocated_encoding(s);
8415 return;
8418 if (!fp_access_check(s)) {
8419 return;
8422 for (i = 0; i < elements; i++) {
8423 read_vec_element(s, tcg_rn, rn, i, size);
8424 if (insert) {
8425 read_vec_element(s, tcg_rd, rd, i, size);
8428 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
8430 write_vec_element(s, tcg_rd, rd, i, size);
8433 if (!is_q) {
8434 clear_vec_high(s, rd);
8438 /* USHLL/SHLL - Vector shift left with widening */
8439 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
8440 int immh, int immb, int opcode, int rn, int rd)
8442 int size = 32 - clz32(immh) - 1;
8443 int immhb = immh << 3 | immb;
8444 int shift = immhb - (8 << size);
8445 int dsize = 64;
8446 int esize = 8 << size;
8447 int elements = dsize/esize;
8448 TCGv_i64 tcg_rn = new_tmp_a64(s);
8449 TCGv_i64 tcg_rd = new_tmp_a64(s);
8450 int i;
8452 if (size >= 3) {
8453 unallocated_encoding(s);
8454 return;
8457 if (!fp_access_check(s)) {
8458 return;
8461 /* For the LL variants the store is larger than the load,
8462 * so if rd == rn we would overwrite parts of our input.
8463 * So load everything right now and use shifts in the main loop.
8465 read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
8467 for (i = 0; i < elements; i++) {
8468 tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
8469 ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
8470 tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
8471 write_vec_element(s, tcg_rd, rd, i, size + 1);
8475 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
8476 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
8477 int immh, int immb, int opcode, int rn, int rd)
8479 int immhb = immh << 3 | immb;
8480 int size = 32 - clz32(immh) - 1;
8481 int dsize = 64;
8482 int esize = 8 << size;
8483 int elements = dsize/esize;
8484 int shift = (2 * esize) - immhb;
8485 bool round = extract32(opcode, 0, 1);
8486 TCGv_i64 tcg_rn, tcg_rd, tcg_final;
8487 TCGv_i64 tcg_round;
8488 int i;
8490 if (extract32(immh, 3, 1)) {
8491 unallocated_encoding(s);
8492 return;
8495 if (!fp_access_check(s)) {
8496 return;
8499 tcg_rn = tcg_temp_new_i64();
8500 tcg_rd = tcg_temp_new_i64();
8501 tcg_final = tcg_temp_new_i64();
8502 read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
8504 if (round) {
8505 uint64_t round_const = 1ULL << (shift - 1);
8506 tcg_round = tcg_const_i64(round_const);
8507 } else {
8508 TCGV_UNUSED_I64(tcg_round);
8511 for (i = 0; i < elements; i++) {
8512 read_vec_element(s, tcg_rn, rn, i, size+1);
8513 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8514 false, true, size+1, shift);
8516 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8519 if (!is_q) {
8520 clear_vec_high(s, rd);
8521 write_vec_element(s, tcg_final, rd, 0, MO_64);
8522 } else {
8523 write_vec_element(s, tcg_final, rd, 1, MO_64);
8526 if (round) {
8527 tcg_temp_free_i64(tcg_round);
8529 tcg_temp_free_i64(tcg_rn);
8530 tcg_temp_free_i64(tcg_rd);
8531 tcg_temp_free_i64(tcg_final);
8532 return;
8536 /* C3.6.14 AdvSIMD shift by immediate
8537 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
8538 * +---+---+---+-------------+------+------+--------+---+------+------+
8539 * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
8540 * +---+---+---+-------------+------+------+--------+---+------+------+
8542 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
8544 int rd = extract32(insn, 0, 5);
8545 int rn = extract32(insn, 5, 5);
8546 int opcode = extract32(insn, 11, 5);
8547 int immb = extract32(insn, 16, 3);
8548 int immh = extract32(insn, 19, 4);
8549 bool is_u = extract32(insn, 29, 1);
8550 bool is_q = extract32(insn, 30, 1);
8552 switch (opcode) {
8553 case 0x08: /* SRI */
8554 if (!is_u) {
8555 unallocated_encoding(s);
8556 return;
8558 /* fall through */
8559 case 0x00: /* SSHR / USHR */
8560 case 0x02: /* SSRA / USRA (accumulate) */
8561 case 0x04: /* SRSHR / URSHR (rounding) */
8562 case 0x06: /* SRSRA / URSRA (accum + rounding) */
8563 handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
8564 break;
8565 case 0x0a: /* SHL / SLI */
8566 handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
8567 break;
8568 case 0x10: /* SHRN */
8569 case 0x11: /* RSHRN / SQRSHRUN */
8570 if (is_u) {
8571 handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
8572 opcode, rn, rd);
8573 } else {
8574 handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
8576 break;
8577 case 0x12: /* SQSHRN / UQSHRN */
8578 case 0x13: /* SQRSHRN / UQRSHRN */
8579 handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
8580 opcode, rn, rd);
8581 break;
8582 case 0x14: /* SSHLL / USHLL */
8583 handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
8584 break;
8585 case 0x1c: /* SCVTF / UCVTF */
8586 handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
8587 opcode, rn, rd);
8588 break;
8589 case 0xc: /* SQSHLU */
8590 if (!is_u) {
8591 unallocated_encoding(s);
8592 return;
8594 handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
8595 break;
8596 case 0xe: /* SQSHL, UQSHL */
8597 handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
8598 break;
8599 case 0x1f: /* FCVTZS/ FCVTZU */
8600 handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
8601 return;
8602 default:
8603 unallocated_encoding(s);
8604 return;
8608 /* Generate code to do a "long" addition or subtraction, ie one done in
8609 * TCGv_i64 on vector lanes twice the width specified by size.
8611 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
8612 TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
8614 static NeonGenTwo64OpFn * const fns[3][2] = {
8615 { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
8616 { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
8617 { tcg_gen_add_i64, tcg_gen_sub_i64 },
8619 NeonGenTwo64OpFn *genfn;
8620 assert(size < 3);
8622 genfn = fns[size][is_sub];
8623 genfn(tcg_res, tcg_op1, tcg_op2);
8626 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
8627 int opcode, int rd, int rn, int rm)
8629 /* 3-reg-different widening insns: 64 x 64 -> 128 */
8630 TCGv_i64 tcg_res[2];
8631 int pass, accop;
8633 tcg_res[0] = tcg_temp_new_i64();
8634 tcg_res[1] = tcg_temp_new_i64();
8636 /* Does this op do an adding accumulate, a subtracting accumulate,
8637 * or no accumulate at all?
8639 switch (opcode) {
8640 case 5:
8641 case 8:
8642 case 9:
8643 accop = 1;
8644 break;
8645 case 10:
8646 case 11:
8647 accop = -1;
8648 break;
8649 default:
8650 accop = 0;
8651 break;
8654 if (accop != 0) {
8655 read_vec_element(s, tcg_res[0], rd, 0, MO_64);
8656 read_vec_element(s, tcg_res[1], rd, 1, MO_64);
8659 /* size == 2 means two 32x32->64 operations; this is worth special
8660 * casing because we can generally handle it inline.
8662 if (size == 2) {
8663 for (pass = 0; pass < 2; pass++) {
8664 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8665 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8666 TCGv_i64 tcg_passres;
8667 TCGMemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
8669 int elt = pass + is_q * 2;
8671 read_vec_element(s, tcg_op1, rn, elt, memop);
8672 read_vec_element(s, tcg_op2, rm, elt, memop);
8674 if (accop == 0) {
8675 tcg_passres = tcg_res[pass];
8676 } else {
8677 tcg_passres = tcg_temp_new_i64();
8680 switch (opcode) {
8681 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8682 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
8683 break;
8684 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8685 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
8686 break;
8687 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8688 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8690 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
8691 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
8693 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
8694 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
8695 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
8696 tcg_passres,
8697 tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
8698 tcg_temp_free_i64(tcg_tmp1);
8699 tcg_temp_free_i64(tcg_tmp2);
8700 break;
8702 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
8703 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
8704 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
8705 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
8706 break;
8707 case 9: /* SQDMLAL, SQDMLAL2 */
8708 case 11: /* SQDMLSL, SQDMLSL2 */
8709 case 13: /* SQDMULL, SQDMULL2 */
8710 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
8711 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
8712 tcg_passres, tcg_passres);
8713 break;
8714 default:
8715 g_assert_not_reached();
8718 if (opcode == 9 || opcode == 11) {
8719 /* saturating accumulate ops */
8720 if (accop < 0) {
8721 tcg_gen_neg_i64(tcg_passres, tcg_passres);
8723 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
8724 tcg_res[pass], tcg_passres);
8725 } else if (accop > 0) {
8726 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
8727 } else if (accop < 0) {
8728 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
8731 if (accop != 0) {
8732 tcg_temp_free_i64(tcg_passres);
8735 tcg_temp_free_i64(tcg_op1);
8736 tcg_temp_free_i64(tcg_op2);
8738 } else {
8739 /* size 0 or 1, generally helper functions */
8740 for (pass = 0; pass < 2; pass++) {
8741 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8742 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8743 TCGv_i64 tcg_passres;
8744 int elt = pass + is_q * 2;
8746 read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
8747 read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
8749 if (accop == 0) {
8750 tcg_passres = tcg_res[pass];
8751 } else {
8752 tcg_passres = tcg_temp_new_i64();
8755 switch (opcode) {
8756 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8757 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8759 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
8760 static NeonGenWidenFn * const widenfns[2][2] = {
8761 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
8762 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
8764 NeonGenWidenFn *widenfn = widenfns[size][is_u];
8766 widenfn(tcg_op2_64, tcg_op2);
8767 widenfn(tcg_passres, tcg_op1);
8768 gen_neon_addl(size, (opcode == 2), tcg_passres,
8769 tcg_passres, tcg_op2_64);
8770 tcg_temp_free_i64(tcg_op2_64);
8771 break;
8773 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8774 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8775 if (size == 0) {
8776 if (is_u) {
8777 gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
8778 } else {
8779 gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
8781 } else {
8782 if (is_u) {
8783 gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
8784 } else {
8785 gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
8788 break;
8789 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
8790 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
8791 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
8792 if (size == 0) {
8793 if (is_u) {
8794 gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
8795 } else {
8796 gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
8798 } else {
8799 if (is_u) {
8800 gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
8801 } else {
8802 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
8805 break;
8806 case 9: /* SQDMLAL, SQDMLAL2 */
8807 case 11: /* SQDMLSL, SQDMLSL2 */
8808 case 13: /* SQDMULL, SQDMULL2 */
8809 assert(size == 1);
8810 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
8811 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
8812 tcg_passres, tcg_passres);
8813 break;
8814 case 14: /* PMULL */
8815 assert(size == 0);
8816 gen_helper_neon_mull_p8(tcg_passres, tcg_op1, tcg_op2);
8817 break;
8818 default:
8819 g_assert_not_reached();
8821 tcg_temp_free_i32(tcg_op1);
8822 tcg_temp_free_i32(tcg_op2);
8824 if (accop != 0) {
8825 if (opcode == 9 || opcode == 11) {
8826 /* saturating accumulate ops */
8827 if (accop < 0) {
8828 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
8830 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
8831 tcg_res[pass],
8832 tcg_passres);
8833 } else {
8834 gen_neon_addl(size, (accop < 0), tcg_res[pass],
8835 tcg_res[pass], tcg_passres);
8837 tcg_temp_free_i64(tcg_passres);
8842 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
8843 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
8844 tcg_temp_free_i64(tcg_res[0]);
8845 tcg_temp_free_i64(tcg_res[1]);
8848 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
8849 int opcode, int rd, int rn, int rm)
8851 TCGv_i64 tcg_res[2];
8852 int part = is_q ? 2 : 0;
8853 int pass;
8855 for (pass = 0; pass < 2; pass++) {
8856 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8857 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8858 TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
8859 static NeonGenWidenFn * const widenfns[3][2] = {
8860 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
8861 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
8862 { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
8864 NeonGenWidenFn *widenfn = widenfns[size][is_u];
8866 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8867 read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
8868 widenfn(tcg_op2_wide, tcg_op2);
8869 tcg_temp_free_i32(tcg_op2);
8870 tcg_res[pass] = tcg_temp_new_i64();
8871 gen_neon_addl(size, (opcode == 3),
8872 tcg_res[pass], tcg_op1, tcg_op2_wide);
8873 tcg_temp_free_i64(tcg_op1);
8874 tcg_temp_free_i64(tcg_op2_wide);
8877 for (pass = 0; pass < 2; pass++) {
8878 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
8879 tcg_temp_free_i64(tcg_res[pass]);
8883 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
8885 tcg_gen_addi_i64(in, in, 1U << 31);
8886 tcg_gen_extrh_i64_i32(res, in);
8889 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
8890 int opcode, int rd, int rn, int rm)
8892 TCGv_i32 tcg_res[2];
8893 int part = is_q ? 2 : 0;
8894 int pass;
8896 for (pass = 0; pass < 2; pass++) {
8897 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8898 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8899 TCGv_i64 tcg_wideres = tcg_temp_new_i64();
8900 static NeonGenNarrowFn * const narrowfns[3][2] = {
8901 { gen_helper_neon_narrow_high_u8,
8902 gen_helper_neon_narrow_round_high_u8 },
8903 { gen_helper_neon_narrow_high_u16,
8904 gen_helper_neon_narrow_round_high_u16 },
8905 { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
8907 NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
8909 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8910 read_vec_element(s, tcg_op2, rm, pass, MO_64);
8912 gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
8914 tcg_temp_free_i64(tcg_op1);
8915 tcg_temp_free_i64(tcg_op2);
8917 tcg_res[pass] = tcg_temp_new_i32();
8918 gennarrow(tcg_res[pass], tcg_wideres);
8919 tcg_temp_free_i64(tcg_wideres);
8922 for (pass = 0; pass < 2; pass++) {
8923 write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
8924 tcg_temp_free_i32(tcg_res[pass]);
8926 if (!is_q) {
8927 clear_vec_high(s, rd);
8931 static void handle_pmull_64(DisasContext *s, int is_q, int rd, int rn, int rm)
8933 /* PMULL of 64 x 64 -> 128 is an odd special case because it
8934 * is the only three-reg-diff instruction which produces a
8935 * 128-bit wide result from a single operation. However since
8936 * it's possible to calculate the two halves more or less
8937 * separately we just use two helper calls.
8939 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8940 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8941 TCGv_i64 tcg_res = tcg_temp_new_i64();
8943 read_vec_element(s, tcg_op1, rn, is_q, MO_64);
8944 read_vec_element(s, tcg_op2, rm, is_q, MO_64);
8945 gen_helper_neon_pmull_64_lo(tcg_res, tcg_op1, tcg_op2);
8946 write_vec_element(s, tcg_res, rd, 0, MO_64);
8947 gen_helper_neon_pmull_64_hi(tcg_res, tcg_op1, tcg_op2);
8948 write_vec_element(s, tcg_res, rd, 1, MO_64);
8950 tcg_temp_free_i64(tcg_op1);
8951 tcg_temp_free_i64(tcg_op2);
8952 tcg_temp_free_i64(tcg_res);
8955 /* C3.6.15 AdvSIMD three different
8956 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
8957 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
8958 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
8959 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
8961 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
8963 /* Instructions in this group fall into three basic classes
8964 * (in each case with the operation working on each element in
8965 * the input vectors):
8966 * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
8967 * 128 bit input)
8968 * (2) wide 64 x 128 -> 128
8969 * (3) narrowing 128 x 128 -> 64
8970 * Here we do initial decode, catch unallocated cases and
8971 * dispatch to separate functions for each class.
8973 int is_q = extract32(insn, 30, 1);
8974 int is_u = extract32(insn, 29, 1);
8975 int size = extract32(insn, 22, 2);
8976 int opcode = extract32(insn, 12, 4);
8977 int rm = extract32(insn, 16, 5);
8978 int rn = extract32(insn, 5, 5);
8979 int rd = extract32(insn, 0, 5);
8981 switch (opcode) {
8982 case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
8983 case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
8984 /* 64 x 128 -> 128 */
8985 if (size == 3) {
8986 unallocated_encoding(s);
8987 return;
8989 if (!fp_access_check(s)) {
8990 return;
8992 handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
8993 break;
8994 case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
8995 case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
8996 /* 128 x 128 -> 64 */
8997 if (size == 3) {
8998 unallocated_encoding(s);
8999 return;
9001 if (!fp_access_check(s)) {
9002 return;
9004 handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
9005 break;
9006 case 14: /* PMULL, PMULL2 */
9007 if (is_u || size == 1 || size == 2) {
9008 unallocated_encoding(s);
9009 return;
9011 if (size == 3) {
9012 if (!arm_dc_feature(s, ARM_FEATURE_V8_PMULL)) {
9013 unallocated_encoding(s);
9014 return;
9016 if (!fp_access_check(s)) {
9017 return;
9019 handle_pmull_64(s, is_q, rd, rn, rm);
9020 return;
9022 goto is_widening;
9023 case 9: /* SQDMLAL, SQDMLAL2 */
9024 case 11: /* SQDMLSL, SQDMLSL2 */
9025 case 13: /* SQDMULL, SQDMULL2 */
9026 if (is_u || size == 0) {
9027 unallocated_encoding(s);
9028 return;
9030 /* fall through */
9031 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
9032 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
9033 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
9034 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
9035 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
9036 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
9037 case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
9038 /* 64 x 64 -> 128 */
9039 if (size == 3) {
9040 unallocated_encoding(s);
9041 return;
9043 is_widening:
9044 if (!fp_access_check(s)) {
9045 return;
9048 handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
9049 break;
9050 default:
9051 /* opcode 15 not allocated */
9052 unallocated_encoding(s);
9053 break;
9057 /* Logic op (opcode == 3) subgroup of C3.6.16. */
9058 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
9060 int rd = extract32(insn, 0, 5);
9061 int rn = extract32(insn, 5, 5);
9062 int rm = extract32(insn, 16, 5);
9063 int size = extract32(insn, 22, 2);
9064 bool is_u = extract32(insn, 29, 1);
9065 bool is_q = extract32(insn, 30, 1);
9066 TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
9067 int pass;
9069 if (!fp_access_check(s)) {
9070 return;
9073 tcg_op1 = tcg_temp_new_i64();
9074 tcg_op2 = tcg_temp_new_i64();
9075 tcg_res[0] = tcg_temp_new_i64();
9076 tcg_res[1] = tcg_temp_new_i64();
9078 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
9079 read_vec_element(s, tcg_op1, rn, pass, MO_64);
9080 read_vec_element(s, tcg_op2, rm, pass, MO_64);
9082 if (!is_u) {
9083 switch (size) {
9084 case 0: /* AND */
9085 tcg_gen_and_i64(tcg_res[pass], tcg_op1, tcg_op2);
9086 break;
9087 case 1: /* BIC */
9088 tcg_gen_andc_i64(tcg_res[pass], tcg_op1, tcg_op2);
9089 break;
9090 case 2: /* ORR */
9091 tcg_gen_or_i64(tcg_res[pass], tcg_op1, tcg_op2);
9092 break;
9093 case 3: /* ORN */
9094 tcg_gen_orc_i64(tcg_res[pass], tcg_op1, tcg_op2);
9095 break;
9097 } else {
9098 if (size != 0) {
9099 /* B* ops need res loaded to operate on */
9100 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9103 switch (size) {
9104 case 0: /* EOR */
9105 tcg_gen_xor_i64(tcg_res[pass], tcg_op1, tcg_op2);
9106 break;
9107 case 1: /* BSL bitwise select */
9108 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_op2);
9109 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_res[pass]);
9110 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op1);
9111 break;
9112 case 2: /* BIT, bitwise insert if true */
9113 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
9114 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_op2);
9115 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
9116 break;
9117 case 3: /* BIF, bitwise insert if false */
9118 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
9119 tcg_gen_andc_i64(tcg_op1, tcg_op1, tcg_op2);
9120 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
9121 break;
9126 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
9127 if (!is_q) {
9128 tcg_gen_movi_i64(tcg_res[1], 0);
9130 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
9132 tcg_temp_free_i64(tcg_op1);
9133 tcg_temp_free_i64(tcg_op2);
9134 tcg_temp_free_i64(tcg_res[0]);
9135 tcg_temp_free_i64(tcg_res[1]);
9138 /* Helper functions for 32 bit comparisons */
9139 static void gen_max_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9141 tcg_gen_movcond_i32(TCG_COND_GE, res, op1, op2, op1, op2);
9144 static void gen_max_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9146 tcg_gen_movcond_i32(TCG_COND_GEU, res, op1, op2, op1, op2);
9149 static void gen_min_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9151 tcg_gen_movcond_i32(TCG_COND_LE, res, op1, op2, op1, op2);
9154 static void gen_min_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9156 tcg_gen_movcond_i32(TCG_COND_LEU, res, op1, op2, op1, op2);
9159 /* Pairwise op subgroup of C3.6.16.
9161 * This is called directly or via the handle_3same_float for float pairwise
9162 * operations where the opcode and size are calculated differently.
9164 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
9165 int size, int rn, int rm, int rd)
9167 TCGv_ptr fpst;
9168 int pass;
9170 /* Floating point operations need fpst */
9171 if (opcode >= 0x58) {
9172 fpst = get_fpstatus_ptr();
9173 } else {
9174 TCGV_UNUSED_PTR(fpst);
9177 if (!fp_access_check(s)) {
9178 return;
9181 /* These operations work on the concatenated rm:rn, with each pair of
9182 * adjacent elements being operated on to produce an element in the result.
9184 if (size == 3) {
9185 TCGv_i64 tcg_res[2];
9187 for (pass = 0; pass < 2; pass++) {
9188 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9189 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9190 int passreg = (pass == 0) ? rn : rm;
9192 read_vec_element(s, tcg_op1, passreg, 0, MO_64);
9193 read_vec_element(s, tcg_op2, passreg, 1, MO_64);
9194 tcg_res[pass] = tcg_temp_new_i64();
9196 switch (opcode) {
9197 case 0x17: /* ADDP */
9198 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
9199 break;
9200 case 0x58: /* FMAXNMP */
9201 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9202 break;
9203 case 0x5a: /* FADDP */
9204 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9205 break;
9206 case 0x5e: /* FMAXP */
9207 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9208 break;
9209 case 0x78: /* FMINNMP */
9210 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9211 break;
9212 case 0x7e: /* FMINP */
9213 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9214 break;
9215 default:
9216 g_assert_not_reached();
9219 tcg_temp_free_i64(tcg_op1);
9220 tcg_temp_free_i64(tcg_op2);
9223 for (pass = 0; pass < 2; pass++) {
9224 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9225 tcg_temp_free_i64(tcg_res[pass]);
9227 } else {
9228 int maxpass = is_q ? 4 : 2;
9229 TCGv_i32 tcg_res[4];
9231 for (pass = 0; pass < maxpass; pass++) {
9232 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9233 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9234 NeonGenTwoOpFn *genfn = NULL;
9235 int passreg = pass < (maxpass / 2) ? rn : rm;
9236 int passelt = (is_q && (pass & 1)) ? 2 : 0;
9238 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
9239 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
9240 tcg_res[pass] = tcg_temp_new_i32();
9242 switch (opcode) {
9243 case 0x17: /* ADDP */
9245 static NeonGenTwoOpFn * const fns[3] = {
9246 gen_helper_neon_padd_u8,
9247 gen_helper_neon_padd_u16,
9248 tcg_gen_add_i32,
9250 genfn = fns[size];
9251 break;
9253 case 0x14: /* SMAXP, UMAXP */
9255 static NeonGenTwoOpFn * const fns[3][2] = {
9256 { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
9257 { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
9258 { gen_max_s32, gen_max_u32 },
9260 genfn = fns[size][u];
9261 break;
9263 case 0x15: /* SMINP, UMINP */
9265 static NeonGenTwoOpFn * const fns[3][2] = {
9266 { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
9267 { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
9268 { gen_min_s32, gen_min_u32 },
9270 genfn = fns[size][u];
9271 break;
9273 /* The FP operations are all on single floats (32 bit) */
9274 case 0x58: /* FMAXNMP */
9275 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9276 break;
9277 case 0x5a: /* FADDP */
9278 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9279 break;
9280 case 0x5e: /* FMAXP */
9281 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9282 break;
9283 case 0x78: /* FMINNMP */
9284 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9285 break;
9286 case 0x7e: /* FMINP */
9287 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9288 break;
9289 default:
9290 g_assert_not_reached();
9293 /* FP ops called directly, otherwise call now */
9294 if (genfn) {
9295 genfn(tcg_res[pass], tcg_op1, tcg_op2);
9298 tcg_temp_free_i32(tcg_op1);
9299 tcg_temp_free_i32(tcg_op2);
9302 for (pass = 0; pass < maxpass; pass++) {
9303 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
9304 tcg_temp_free_i32(tcg_res[pass]);
9306 if (!is_q) {
9307 clear_vec_high(s, rd);
9311 if (!TCGV_IS_UNUSED_PTR(fpst)) {
9312 tcg_temp_free_ptr(fpst);
9316 /* Floating point op subgroup of C3.6.16. */
9317 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
9319 /* For floating point ops, the U, size[1] and opcode bits
9320 * together indicate the operation. size[0] indicates single
9321 * or double.
9323 int fpopcode = extract32(insn, 11, 5)
9324 | (extract32(insn, 23, 1) << 5)
9325 | (extract32(insn, 29, 1) << 6);
9326 int is_q = extract32(insn, 30, 1);
9327 int size = extract32(insn, 22, 1);
9328 int rm = extract32(insn, 16, 5);
9329 int rn = extract32(insn, 5, 5);
9330 int rd = extract32(insn, 0, 5);
9332 int datasize = is_q ? 128 : 64;
9333 int esize = 32 << size;
9334 int elements = datasize / esize;
9336 if (size == 1 && !is_q) {
9337 unallocated_encoding(s);
9338 return;
9341 switch (fpopcode) {
9342 case 0x58: /* FMAXNMP */
9343 case 0x5a: /* FADDP */
9344 case 0x5e: /* FMAXP */
9345 case 0x78: /* FMINNMP */
9346 case 0x7e: /* FMINP */
9347 if (size && !is_q) {
9348 unallocated_encoding(s);
9349 return;
9351 handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
9352 rn, rm, rd);
9353 return;
9354 case 0x1b: /* FMULX */
9355 case 0x1f: /* FRECPS */
9356 case 0x3f: /* FRSQRTS */
9357 case 0x5d: /* FACGE */
9358 case 0x7d: /* FACGT */
9359 case 0x19: /* FMLA */
9360 case 0x39: /* FMLS */
9361 case 0x18: /* FMAXNM */
9362 case 0x1a: /* FADD */
9363 case 0x1c: /* FCMEQ */
9364 case 0x1e: /* FMAX */
9365 case 0x38: /* FMINNM */
9366 case 0x3a: /* FSUB */
9367 case 0x3e: /* FMIN */
9368 case 0x5b: /* FMUL */
9369 case 0x5c: /* FCMGE */
9370 case 0x5f: /* FDIV */
9371 case 0x7a: /* FABD */
9372 case 0x7c: /* FCMGT */
9373 if (!fp_access_check(s)) {
9374 return;
9377 handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
9378 return;
9379 default:
9380 unallocated_encoding(s);
9381 return;
9385 /* Integer op subgroup of C3.6.16. */
9386 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
9388 int is_q = extract32(insn, 30, 1);
9389 int u = extract32(insn, 29, 1);
9390 int size = extract32(insn, 22, 2);
9391 int opcode = extract32(insn, 11, 5);
9392 int rm = extract32(insn, 16, 5);
9393 int rn = extract32(insn, 5, 5);
9394 int rd = extract32(insn, 0, 5);
9395 int pass;
9397 switch (opcode) {
9398 case 0x13: /* MUL, PMUL */
9399 if (u && size != 0) {
9400 unallocated_encoding(s);
9401 return;
9403 /* fall through */
9404 case 0x0: /* SHADD, UHADD */
9405 case 0x2: /* SRHADD, URHADD */
9406 case 0x4: /* SHSUB, UHSUB */
9407 case 0xc: /* SMAX, UMAX */
9408 case 0xd: /* SMIN, UMIN */
9409 case 0xe: /* SABD, UABD */
9410 case 0xf: /* SABA, UABA */
9411 case 0x12: /* MLA, MLS */
9412 if (size == 3) {
9413 unallocated_encoding(s);
9414 return;
9416 break;
9417 case 0x16: /* SQDMULH, SQRDMULH */
9418 if (size == 0 || size == 3) {
9419 unallocated_encoding(s);
9420 return;
9422 break;
9423 default:
9424 if (size == 3 && !is_q) {
9425 unallocated_encoding(s);
9426 return;
9428 break;
9431 if (!fp_access_check(s)) {
9432 return;
9435 if (size == 3) {
9436 assert(is_q);
9437 for (pass = 0; pass < 2; pass++) {
9438 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9439 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9440 TCGv_i64 tcg_res = tcg_temp_new_i64();
9442 read_vec_element(s, tcg_op1, rn, pass, MO_64);
9443 read_vec_element(s, tcg_op2, rm, pass, MO_64);
9445 handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
9447 write_vec_element(s, tcg_res, rd, pass, MO_64);
9449 tcg_temp_free_i64(tcg_res);
9450 tcg_temp_free_i64(tcg_op1);
9451 tcg_temp_free_i64(tcg_op2);
9453 } else {
9454 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
9455 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9456 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9457 TCGv_i32 tcg_res = tcg_temp_new_i32();
9458 NeonGenTwoOpFn *genfn = NULL;
9459 NeonGenTwoOpEnvFn *genenvfn = NULL;
9461 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9462 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9464 switch (opcode) {
9465 case 0x0: /* SHADD, UHADD */
9467 static NeonGenTwoOpFn * const fns[3][2] = {
9468 { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
9469 { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
9470 { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
9472 genfn = fns[size][u];
9473 break;
9475 case 0x1: /* SQADD, UQADD */
9477 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9478 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9479 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9480 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9482 genenvfn = fns[size][u];
9483 break;
9485 case 0x2: /* SRHADD, URHADD */
9487 static NeonGenTwoOpFn * const fns[3][2] = {
9488 { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
9489 { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
9490 { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
9492 genfn = fns[size][u];
9493 break;
9495 case 0x4: /* SHSUB, UHSUB */
9497 static NeonGenTwoOpFn * const fns[3][2] = {
9498 { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
9499 { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
9500 { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
9502 genfn = fns[size][u];
9503 break;
9505 case 0x5: /* SQSUB, UQSUB */
9507 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9508 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9509 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9510 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9512 genenvfn = fns[size][u];
9513 break;
9515 case 0x6: /* CMGT, CMHI */
9517 static NeonGenTwoOpFn * const fns[3][2] = {
9518 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_u8 },
9519 { gen_helper_neon_cgt_s16, gen_helper_neon_cgt_u16 },
9520 { gen_helper_neon_cgt_s32, gen_helper_neon_cgt_u32 },
9522 genfn = fns[size][u];
9523 break;
9525 case 0x7: /* CMGE, CMHS */
9527 static NeonGenTwoOpFn * const fns[3][2] = {
9528 { gen_helper_neon_cge_s8, gen_helper_neon_cge_u8 },
9529 { gen_helper_neon_cge_s16, gen_helper_neon_cge_u16 },
9530 { gen_helper_neon_cge_s32, gen_helper_neon_cge_u32 },
9532 genfn = fns[size][u];
9533 break;
9535 case 0x8: /* SSHL, USHL */
9537 static NeonGenTwoOpFn * const fns[3][2] = {
9538 { gen_helper_neon_shl_s8, gen_helper_neon_shl_u8 },
9539 { gen_helper_neon_shl_s16, gen_helper_neon_shl_u16 },
9540 { gen_helper_neon_shl_s32, gen_helper_neon_shl_u32 },
9542 genfn = fns[size][u];
9543 break;
9545 case 0x9: /* SQSHL, UQSHL */
9547 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9548 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9549 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9550 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9552 genenvfn = fns[size][u];
9553 break;
9555 case 0xa: /* SRSHL, URSHL */
9557 static NeonGenTwoOpFn * const fns[3][2] = {
9558 { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
9559 { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
9560 { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
9562 genfn = fns[size][u];
9563 break;
9565 case 0xb: /* SQRSHL, UQRSHL */
9567 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9568 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9569 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9570 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9572 genenvfn = fns[size][u];
9573 break;
9575 case 0xc: /* SMAX, UMAX */
9577 static NeonGenTwoOpFn * const fns[3][2] = {
9578 { gen_helper_neon_max_s8, gen_helper_neon_max_u8 },
9579 { gen_helper_neon_max_s16, gen_helper_neon_max_u16 },
9580 { gen_max_s32, gen_max_u32 },
9582 genfn = fns[size][u];
9583 break;
9586 case 0xd: /* SMIN, UMIN */
9588 static NeonGenTwoOpFn * const fns[3][2] = {
9589 { gen_helper_neon_min_s8, gen_helper_neon_min_u8 },
9590 { gen_helper_neon_min_s16, gen_helper_neon_min_u16 },
9591 { gen_min_s32, gen_min_u32 },
9593 genfn = fns[size][u];
9594 break;
9596 case 0xe: /* SABD, UABD */
9597 case 0xf: /* SABA, UABA */
9599 static NeonGenTwoOpFn * const fns[3][2] = {
9600 { gen_helper_neon_abd_s8, gen_helper_neon_abd_u8 },
9601 { gen_helper_neon_abd_s16, gen_helper_neon_abd_u16 },
9602 { gen_helper_neon_abd_s32, gen_helper_neon_abd_u32 },
9604 genfn = fns[size][u];
9605 break;
9607 case 0x10: /* ADD, SUB */
9609 static NeonGenTwoOpFn * const fns[3][2] = {
9610 { gen_helper_neon_add_u8, gen_helper_neon_sub_u8 },
9611 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
9612 { tcg_gen_add_i32, tcg_gen_sub_i32 },
9614 genfn = fns[size][u];
9615 break;
9617 case 0x11: /* CMTST, CMEQ */
9619 static NeonGenTwoOpFn * const fns[3][2] = {
9620 { gen_helper_neon_tst_u8, gen_helper_neon_ceq_u8 },
9621 { gen_helper_neon_tst_u16, gen_helper_neon_ceq_u16 },
9622 { gen_helper_neon_tst_u32, gen_helper_neon_ceq_u32 },
9624 genfn = fns[size][u];
9625 break;
9627 case 0x13: /* MUL, PMUL */
9628 if (u) {
9629 /* PMUL */
9630 assert(size == 0);
9631 genfn = gen_helper_neon_mul_p8;
9632 break;
9634 /* fall through : MUL */
9635 case 0x12: /* MLA, MLS */
9637 static NeonGenTwoOpFn * const fns[3] = {
9638 gen_helper_neon_mul_u8,
9639 gen_helper_neon_mul_u16,
9640 tcg_gen_mul_i32,
9642 genfn = fns[size];
9643 break;
9645 case 0x16: /* SQDMULH, SQRDMULH */
9647 static NeonGenTwoOpEnvFn * const fns[2][2] = {
9648 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9649 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9651 assert(size == 1 || size == 2);
9652 genenvfn = fns[size - 1][u];
9653 break;
9655 default:
9656 g_assert_not_reached();
9659 if (genenvfn) {
9660 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
9661 } else {
9662 genfn(tcg_res, tcg_op1, tcg_op2);
9665 if (opcode == 0xf || opcode == 0x12) {
9666 /* SABA, UABA, MLA, MLS: accumulating ops */
9667 static NeonGenTwoOpFn * const fns[3][2] = {
9668 { gen_helper_neon_add_u8, gen_helper_neon_sub_u8 },
9669 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
9670 { tcg_gen_add_i32, tcg_gen_sub_i32 },
9672 bool is_sub = (opcode == 0x12 && u); /* MLS */
9674 genfn = fns[size][is_sub];
9675 read_vec_element_i32(s, tcg_op1, rd, pass, MO_32);
9676 genfn(tcg_res, tcg_op1, tcg_res);
9679 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9681 tcg_temp_free_i32(tcg_res);
9682 tcg_temp_free_i32(tcg_op1);
9683 tcg_temp_free_i32(tcg_op2);
9687 if (!is_q) {
9688 clear_vec_high(s, rd);
9692 /* C3.6.16 AdvSIMD three same
9693 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
9694 * +---+---+---+-----------+------+---+------+--------+---+------+------+
9695 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
9696 * +---+---+---+-----------+------+---+------+--------+---+------+------+
9698 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
9700 int opcode = extract32(insn, 11, 5);
9702 switch (opcode) {
9703 case 0x3: /* logic ops */
9704 disas_simd_3same_logic(s, insn);
9705 break;
9706 case 0x17: /* ADDP */
9707 case 0x14: /* SMAXP, UMAXP */
9708 case 0x15: /* SMINP, UMINP */
9710 /* Pairwise operations */
9711 int is_q = extract32(insn, 30, 1);
9712 int u = extract32(insn, 29, 1);
9713 int size = extract32(insn, 22, 2);
9714 int rm = extract32(insn, 16, 5);
9715 int rn = extract32(insn, 5, 5);
9716 int rd = extract32(insn, 0, 5);
9717 if (opcode == 0x17) {
9718 if (u || (size == 3 && !is_q)) {
9719 unallocated_encoding(s);
9720 return;
9722 } else {
9723 if (size == 3) {
9724 unallocated_encoding(s);
9725 return;
9728 handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
9729 break;
9731 case 0x18 ... 0x31:
9732 /* floating point ops, sz[1] and U are part of opcode */
9733 disas_simd_3same_float(s, insn);
9734 break;
9735 default:
9736 disas_simd_3same_int(s, insn);
9737 break;
9741 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
9742 int size, int rn, int rd)
9744 /* Handle 2-reg-misc ops which are widening (so each size element
9745 * in the source becomes a 2*size element in the destination.
9746 * The only instruction like this is FCVTL.
9748 int pass;
9750 if (size == 3) {
9751 /* 32 -> 64 bit fp conversion */
9752 TCGv_i64 tcg_res[2];
9753 int srcelt = is_q ? 2 : 0;
9755 for (pass = 0; pass < 2; pass++) {
9756 TCGv_i32 tcg_op = tcg_temp_new_i32();
9757 tcg_res[pass] = tcg_temp_new_i64();
9759 read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
9760 gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
9761 tcg_temp_free_i32(tcg_op);
9763 for (pass = 0; pass < 2; pass++) {
9764 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9765 tcg_temp_free_i64(tcg_res[pass]);
9767 } else {
9768 /* 16 -> 32 bit fp conversion */
9769 int srcelt = is_q ? 4 : 0;
9770 TCGv_i32 tcg_res[4];
9772 for (pass = 0; pass < 4; pass++) {
9773 tcg_res[pass] = tcg_temp_new_i32();
9775 read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
9776 gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
9777 cpu_env);
9779 for (pass = 0; pass < 4; pass++) {
9780 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
9781 tcg_temp_free_i32(tcg_res[pass]);
9786 static void handle_rev(DisasContext *s, int opcode, bool u,
9787 bool is_q, int size, int rn, int rd)
9789 int op = (opcode << 1) | u;
9790 int opsz = op + size;
9791 int grp_size = 3 - opsz;
9792 int dsize = is_q ? 128 : 64;
9793 int i;
9795 if (opsz >= 3) {
9796 unallocated_encoding(s);
9797 return;
9800 if (!fp_access_check(s)) {
9801 return;
9804 if (size == 0) {
9805 /* Special case bytes, use bswap op on each group of elements */
9806 int groups = dsize / (8 << grp_size);
9808 for (i = 0; i < groups; i++) {
9809 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9811 read_vec_element(s, tcg_tmp, rn, i, grp_size);
9812 switch (grp_size) {
9813 case MO_16:
9814 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
9815 break;
9816 case MO_32:
9817 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
9818 break;
9819 case MO_64:
9820 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
9821 break;
9822 default:
9823 g_assert_not_reached();
9825 write_vec_element(s, tcg_tmp, rd, i, grp_size);
9826 tcg_temp_free_i64(tcg_tmp);
9828 if (!is_q) {
9829 clear_vec_high(s, rd);
9831 } else {
9832 int revmask = (1 << grp_size) - 1;
9833 int esize = 8 << size;
9834 int elements = dsize / esize;
9835 TCGv_i64 tcg_rn = tcg_temp_new_i64();
9836 TCGv_i64 tcg_rd = tcg_const_i64(0);
9837 TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
9839 for (i = 0; i < elements; i++) {
9840 int e_rev = (i & 0xf) ^ revmask;
9841 int off = e_rev * esize;
9842 read_vec_element(s, tcg_rn, rn, i, size);
9843 if (off >= 64) {
9844 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
9845 tcg_rn, off - 64, esize);
9846 } else {
9847 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
9850 write_vec_element(s, tcg_rd, rd, 0, MO_64);
9851 write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
9853 tcg_temp_free_i64(tcg_rd_hi);
9854 tcg_temp_free_i64(tcg_rd);
9855 tcg_temp_free_i64(tcg_rn);
9859 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
9860 bool is_q, int size, int rn, int rd)
9862 /* Implement the pairwise operations from 2-misc:
9863 * SADDLP, UADDLP, SADALP, UADALP.
9864 * These all add pairs of elements in the input to produce a
9865 * double-width result element in the output (possibly accumulating).
9867 bool accum = (opcode == 0x6);
9868 int maxpass = is_q ? 2 : 1;
9869 int pass;
9870 TCGv_i64 tcg_res[2];
9872 if (size == 2) {
9873 /* 32 + 32 -> 64 op */
9874 TCGMemOp memop = size + (u ? 0 : MO_SIGN);
9876 for (pass = 0; pass < maxpass; pass++) {
9877 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9878 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9880 tcg_res[pass] = tcg_temp_new_i64();
9882 read_vec_element(s, tcg_op1, rn, pass * 2, memop);
9883 read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
9884 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
9885 if (accum) {
9886 read_vec_element(s, tcg_op1, rd, pass, MO_64);
9887 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
9890 tcg_temp_free_i64(tcg_op1);
9891 tcg_temp_free_i64(tcg_op2);
9893 } else {
9894 for (pass = 0; pass < maxpass; pass++) {
9895 TCGv_i64 tcg_op = tcg_temp_new_i64();
9896 NeonGenOneOpFn *genfn;
9897 static NeonGenOneOpFn * const fns[2][2] = {
9898 { gen_helper_neon_addlp_s8, gen_helper_neon_addlp_u8 },
9899 { gen_helper_neon_addlp_s16, gen_helper_neon_addlp_u16 },
9902 genfn = fns[size][u];
9904 tcg_res[pass] = tcg_temp_new_i64();
9906 read_vec_element(s, tcg_op, rn, pass, MO_64);
9907 genfn(tcg_res[pass], tcg_op);
9909 if (accum) {
9910 read_vec_element(s, tcg_op, rd, pass, MO_64);
9911 if (size == 0) {
9912 gen_helper_neon_addl_u16(tcg_res[pass],
9913 tcg_res[pass], tcg_op);
9914 } else {
9915 gen_helper_neon_addl_u32(tcg_res[pass],
9916 tcg_res[pass], tcg_op);
9919 tcg_temp_free_i64(tcg_op);
9922 if (!is_q) {
9923 tcg_res[1] = tcg_const_i64(0);
9925 for (pass = 0; pass < 2; pass++) {
9926 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9927 tcg_temp_free_i64(tcg_res[pass]);
9931 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
9933 /* Implement SHLL and SHLL2 */
9934 int pass;
9935 int part = is_q ? 2 : 0;
9936 TCGv_i64 tcg_res[2];
9938 for (pass = 0; pass < 2; pass++) {
9939 static NeonGenWidenFn * const widenfns[3] = {
9940 gen_helper_neon_widen_u8,
9941 gen_helper_neon_widen_u16,
9942 tcg_gen_extu_i32_i64,
9944 NeonGenWidenFn *widenfn = widenfns[size];
9945 TCGv_i32 tcg_op = tcg_temp_new_i32();
9947 read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
9948 tcg_res[pass] = tcg_temp_new_i64();
9949 widenfn(tcg_res[pass], tcg_op);
9950 tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
9952 tcg_temp_free_i32(tcg_op);
9955 for (pass = 0; pass < 2; pass++) {
9956 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9957 tcg_temp_free_i64(tcg_res[pass]);
9961 /* C3.6.17 AdvSIMD two reg misc
9962 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
9963 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
9964 * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
9965 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
9967 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
9969 int size = extract32(insn, 22, 2);
9970 int opcode = extract32(insn, 12, 5);
9971 bool u = extract32(insn, 29, 1);
9972 bool is_q = extract32(insn, 30, 1);
9973 int rn = extract32(insn, 5, 5);
9974 int rd = extract32(insn, 0, 5);
9975 bool need_fpstatus = false;
9976 bool need_rmode = false;
9977 int rmode = -1;
9978 TCGv_i32 tcg_rmode;
9979 TCGv_ptr tcg_fpstatus;
9981 switch (opcode) {
9982 case 0x0: /* REV64, REV32 */
9983 case 0x1: /* REV16 */
9984 handle_rev(s, opcode, u, is_q, size, rn, rd);
9985 return;
9986 case 0x5: /* CNT, NOT, RBIT */
9987 if (u && size == 0) {
9988 /* NOT: adjust size so we can use the 64-bits-at-a-time loop. */
9989 size = 3;
9990 break;
9991 } else if (u && size == 1) {
9992 /* RBIT */
9993 break;
9994 } else if (!u && size == 0) {
9995 /* CNT */
9996 break;
9998 unallocated_encoding(s);
9999 return;
10000 case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
10001 case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
10002 if (size == 3) {
10003 unallocated_encoding(s);
10004 return;
10006 if (!fp_access_check(s)) {
10007 return;
10010 handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
10011 return;
10012 case 0x4: /* CLS, CLZ */
10013 if (size == 3) {
10014 unallocated_encoding(s);
10015 return;
10017 break;
10018 case 0x2: /* SADDLP, UADDLP */
10019 case 0x6: /* SADALP, UADALP */
10020 if (size == 3) {
10021 unallocated_encoding(s);
10022 return;
10024 if (!fp_access_check(s)) {
10025 return;
10027 handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
10028 return;
10029 case 0x13: /* SHLL, SHLL2 */
10030 if (u == 0 || size == 3) {
10031 unallocated_encoding(s);
10032 return;
10034 if (!fp_access_check(s)) {
10035 return;
10037 handle_shll(s, is_q, size, rn, rd);
10038 return;
10039 case 0xa: /* CMLT */
10040 if (u == 1) {
10041 unallocated_encoding(s);
10042 return;
10044 /* fall through */
10045 case 0x8: /* CMGT, CMGE */
10046 case 0x9: /* CMEQ, CMLE */
10047 case 0xb: /* ABS, NEG */
10048 if (size == 3 && !is_q) {
10049 unallocated_encoding(s);
10050 return;
10052 break;
10053 case 0x3: /* SUQADD, USQADD */
10054 if (size == 3 && !is_q) {
10055 unallocated_encoding(s);
10056 return;
10058 if (!fp_access_check(s)) {
10059 return;
10061 handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
10062 return;
10063 case 0x7: /* SQABS, SQNEG */
10064 if (size == 3 && !is_q) {
10065 unallocated_encoding(s);
10066 return;
10068 break;
10069 case 0xc ... 0xf:
10070 case 0x16 ... 0x1d:
10071 case 0x1f:
10073 /* Floating point: U, size[1] and opcode indicate operation;
10074 * size[0] indicates single or double precision.
10076 int is_double = extract32(size, 0, 1);
10077 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10078 size = is_double ? 3 : 2;
10079 switch (opcode) {
10080 case 0x2f: /* FABS */
10081 case 0x6f: /* FNEG */
10082 if (size == 3 && !is_q) {
10083 unallocated_encoding(s);
10084 return;
10086 break;
10087 case 0x1d: /* SCVTF */
10088 case 0x5d: /* UCVTF */
10090 bool is_signed = (opcode == 0x1d) ? true : false;
10091 int elements = is_double ? 2 : is_q ? 4 : 2;
10092 if (is_double && !is_q) {
10093 unallocated_encoding(s);
10094 return;
10096 if (!fp_access_check(s)) {
10097 return;
10099 handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
10100 return;
10102 case 0x2c: /* FCMGT (zero) */
10103 case 0x2d: /* FCMEQ (zero) */
10104 case 0x2e: /* FCMLT (zero) */
10105 case 0x6c: /* FCMGE (zero) */
10106 case 0x6d: /* FCMLE (zero) */
10107 if (size == 3 && !is_q) {
10108 unallocated_encoding(s);
10109 return;
10111 handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
10112 return;
10113 case 0x7f: /* FSQRT */
10114 if (size == 3 && !is_q) {
10115 unallocated_encoding(s);
10116 return;
10118 break;
10119 case 0x1a: /* FCVTNS */
10120 case 0x1b: /* FCVTMS */
10121 case 0x3a: /* FCVTPS */
10122 case 0x3b: /* FCVTZS */
10123 case 0x5a: /* FCVTNU */
10124 case 0x5b: /* FCVTMU */
10125 case 0x7a: /* FCVTPU */
10126 case 0x7b: /* FCVTZU */
10127 need_fpstatus = true;
10128 need_rmode = true;
10129 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10130 if (size == 3 && !is_q) {
10131 unallocated_encoding(s);
10132 return;
10134 break;
10135 case 0x5c: /* FCVTAU */
10136 case 0x1c: /* FCVTAS */
10137 need_fpstatus = true;
10138 need_rmode = true;
10139 rmode = FPROUNDING_TIEAWAY;
10140 if (size == 3 && !is_q) {
10141 unallocated_encoding(s);
10142 return;
10144 break;
10145 case 0x3c: /* URECPE */
10146 if (size == 3) {
10147 unallocated_encoding(s);
10148 return;
10150 /* fall through */
10151 case 0x3d: /* FRECPE */
10152 case 0x7d: /* FRSQRTE */
10153 if (size == 3 && !is_q) {
10154 unallocated_encoding(s);
10155 return;
10157 if (!fp_access_check(s)) {
10158 return;
10160 handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
10161 return;
10162 case 0x56: /* FCVTXN, FCVTXN2 */
10163 if (size == 2) {
10164 unallocated_encoding(s);
10165 return;
10167 /* fall through */
10168 case 0x16: /* FCVTN, FCVTN2 */
10169 /* handle_2misc_narrow does a 2*size -> size operation, but these
10170 * instructions encode the source size rather than dest size.
10172 if (!fp_access_check(s)) {
10173 return;
10175 handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
10176 return;
10177 case 0x17: /* FCVTL, FCVTL2 */
10178 if (!fp_access_check(s)) {
10179 return;
10181 handle_2misc_widening(s, opcode, is_q, size, rn, rd);
10182 return;
10183 case 0x18: /* FRINTN */
10184 case 0x19: /* FRINTM */
10185 case 0x38: /* FRINTP */
10186 case 0x39: /* FRINTZ */
10187 need_rmode = true;
10188 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10189 /* fall through */
10190 case 0x59: /* FRINTX */
10191 case 0x79: /* FRINTI */
10192 need_fpstatus = true;
10193 if (size == 3 && !is_q) {
10194 unallocated_encoding(s);
10195 return;
10197 break;
10198 case 0x58: /* FRINTA */
10199 need_rmode = true;
10200 rmode = FPROUNDING_TIEAWAY;
10201 need_fpstatus = true;
10202 if (size == 3 && !is_q) {
10203 unallocated_encoding(s);
10204 return;
10206 break;
10207 case 0x7c: /* URSQRTE */
10208 if (size == 3) {
10209 unallocated_encoding(s);
10210 return;
10212 need_fpstatus = true;
10213 break;
10214 default:
10215 unallocated_encoding(s);
10216 return;
10218 break;
10220 default:
10221 unallocated_encoding(s);
10222 return;
10225 if (!fp_access_check(s)) {
10226 return;
10229 if (need_fpstatus) {
10230 tcg_fpstatus = get_fpstatus_ptr();
10231 } else {
10232 TCGV_UNUSED_PTR(tcg_fpstatus);
10234 if (need_rmode) {
10235 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
10236 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
10237 } else {
10238 TCGV_UNUSED_I32(tcg_rmode);
10241 if (size == 3) {
10242 /* All 64-bit element operations can be shared with scalar 2misc */
10243 int pass;
10245 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
10246 TCGv_i64 tcg_op = tcg_temp_new_i64();
10247 TCGv_i64 tcg_res = tcg_temp_new_i64();
10249 read_vec_element(s, tcg_op, rn, pass, MO_64);
10251 handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
10252 tcg_rmode, tcg_fpstatus);
10254 write_vec_element(s, tcg_res, rd, pass, MO_64);
10256 tcg_temp_free_i64(tcg_res);
10257 tcg_temp_free_i64(tcg_op);
10259 } else {
10260 int pass;
10262 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
10263 TCGv_i32 tcg_op = tcg_temp_new_i32();
10264 TCGv_i32 tcg_res = tcg_temp_new_i32();
10265 TCGCond cond;
10267 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
10269 if (size == 2) {
10270 /* Special cases for 32 bit elements */
10271 switch (opcode) {
10272 case 0xa: /* CMLT */
10273 /* 32 bit integer comparison against zero, result is
10274 * test ? (2^32 - 1) : 0. We implement via setcond(test)
10275 * and inverting.
10277 cond = TCG_COND_LT;
10278 do_cmop:
10279 tcg_gen_setcondi_i32(cond, tcg_res, tcg_op, 0);
10280 tcg_gen_neg_i32(tcg_res, tcg_res);
10281 break;
10282 case 0x8: /* CMGT, CMGE */
10283 cond = u ? TCG_COND_GE : TCG_COND_GT;
10284 goto do_cmop;
10285 case 0x9: /* CMEQ, CMLE */
10286 cond = u ? TCG_COND_LE : TCG_COND_EQ;
10287 goto do_cmop;
10288 case 0x4: /* CLS */
10289 if (u) {
10290 gen_helper_clz32(tcg_res, tcg_op);
10291 } else {
10292 gen_helper_cls32(tcg_res, tcg_op);
10294 break;
10295 case 0x7: /* SQABS, SQNEG */
10296 if (u) {
10297 gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
10298 } else {
10299 gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
10301 break;
10302 case 0xb: /* ABS, NEG */
10303 if (u) {
10304 tcg_gen_neg_i32(tcg_res, tcg_op);
10305 } else {
10306 TCGv_i32 tcg_zero = tcg_const_i32(0);
10307 tcg_gen_neg_i32(tcg_res, tcg_op);
10308 tcg_gen_movcond_i32(TCG_COND_GT, tcg_res, tcg_op,
10309 tcg_zero, tcg_op, tcg_res);
10310 tcg_temp_free_i32(tcg_zero);
10312 break;
10313 case 0x2f: /* FABS */
10314 gen_helper_vfp_abss(tcg_res, tcg_op);
10315 break;
10316 case 0x6f: /* FNEG */
10317 gen_helper_vfp_negs(tcg_res, tcg_op);
10318 break;
10319 case 0x7f: /* FSQRT */
10320 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
10321 break;
10322 case 0x1a: /* FCVTNS */
10323 case 0x1b: /* FCVTMS */
10324 case 0x1c: /* FCVTAS */
10325 case 0x3a: /* FCVTPS */
10326 case 0x3b: /* FCVTZS */
10328 TCGv_i32 tcg_shift = tcg_const_i32(0);
10329 gen_helper_vfp_tosls(tcg_res, tcg_op,
10330 tcg_shift, tcg_fpstatus);
10331 tcg_temp_free_i32(tcg_shift);
10332 break;
10334 case 0x5a: /* FCVTNU */
10335 case 0x5b: /* FCVTMU */
10336 case 0x5c: /* FCVTAU */
10337 case 0x7a: /* FCVTPU */
10338 case 0x7b: /* FCVTZU */
10340 TCGv_i32 tcg_shift = tcg_const_i32(0);
10341 gen_helper_vfp_touls(tcg_res, tcg_op,
10342 tcg_shift, tcg_fpstatus);
10343 tcg_temp_free_i32(tcg_shift);
10344 break;
10346 case 0x18: /* FRINTN */
10347 case 0x19: /* FRINTM */
10348 case 0x38: /* FRINTP */
10349 case 0x39: /* FRINTZ */
10350 case 0x58: /* FRINTA */
10351 case 0x79: /* FRINTI */
10352 gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
10353 break;
10354 case 0x59: /* FRINTX */
10355 gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
10356 break;
10357 case 0x7c: /* URSQRTE */
10358 gen_helper_rsqrte_u32(tcg_res, tcg_op, tcg_fpstatus);
10359 break;
10360 default:
10361 g_assert_not_reached();
10363 } else {
10364 /* Use helpers for 8 and 16 bit elements */
10365 switch (opcode) {
10366 case 0x5: /* CNT, RBIT */
10367 /* For these two insns size is part of the opcode specifier
10368 * (handled earlier); they always operate on byte elements.
10370 if (u) {
10371 gen_helper_neon_rbit_u8(tcg_res, tcg_op);
10372 } else {
10373 gen_helper_neon_cnt_u8(tcg_res, tcg_op);
10375 break;
10376 case 0x7: /* SQABS, SQNEG */
10378 NeonGenOneOpEnvFn *genfn;
10379 static NeonGenOneOpEnvFn * const fns[2][2] = {
10380 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10381 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10383 genfn = fns[size][u];
10384 genfn(tcg_res, cpu_env, tcg_op);
10385 break;
10387 case 0x8: /* CMGT, CMGE */
10388 case 0x9: /* CMEQ, CMLE */
10389 case 0xa: /* CMLT */
10391 static NeonGenTwoOpFn * const fns[3][2] = {
10392 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_s16 },
10393 { gen_helper_neon_cge_s8, gen_helper_neon_cge_s16 },
10394 { gen_helper_neon_ceq_u8, gen_helper_neon_ceq_u16 },
10396 NeonGenTwoOpFn *genfn;
10397 int comp;
10398 bool reverse;
10399 TCGv_i32 tcg_zero = tcg_const_i32(0);
10401 /* comp = index into [CMGT, CMGE, CMEQ, CMLE, CMLT] */
10402 comp = (opcode - 0x8) * 2 + u;
10403 /* ...but LE, LT are implemented as reverse GE, GT */
10404 reverse = (comp > 2);
10405 if (reverse) {
10406 comp = 4 - comp;
10408 genfn = fns[comp][size];
10409 if (reverse) {
10410 genfn(tcg_res, tcg_zero, tcg_op);
10411 } else {
10412 genfn(tcg_res, tcg_op, tcg_zero);
10414 tcg_temp_free_i32(tcg_zero);
10415 break;
10417 case 0xb: /* ABS, NEG */
10418 if (u) {
10419 TCGv_i32 tcg_zero = tcg_const_i32(0);
10420 if (size) {
10421 gen_helper_neon_sub_u16(tcg_res, tcg_zero, tcg_op);
10422 } else {
10423 gen_helper_neon_sub_u8(tcg_res, tcg_zero, tcg_op);
10425 tcg_temp_free_i32(tcg_zero);
10426 } else {
10427 if (size) {
10428 gen_helper_neon_abs_s16(tcg_res, tcg_op);
10429 } else {
10430 gen_helper_neon_abs_s8(tcg_res, tcg_op);
10433 break;
10434 case 0x4: /* CLS, CLZ */
10435 if (u) {
10436 if (size == 0) {
10437 gen_helper_neon_clz_u8(tcg_res, tcg_op);
10438 } else {
10439 gen_helper_neon_clz_u16(tcg_res, tcg_op);
10441 } else {
10442 if (size == 0) {
10443 gen_helper_neon_cls_s8(tcg_res, tcg_op);
10444 } else {
10445 gen_helper_neon_cls_s16(tcg_res, tcg_op);
10448 break;
10449 default:
10450 g_assert_not_reached();
10454 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10456 tcg_temp_free_i32(tcg_res);
10457 tcg_temp_free_i32(tcg_op);
10460 if (!is_q) {
10461 clear_vec_high(s, rd);
10464 if (need_rmode) {
10465 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
10466 tcg_temp_free_i32(tcg_rmode);
10468 if (need_fpstatus) {
10469 tcg_temp_free_ptr(tcg_fpstatus);
10473 /* C3.6.13 AdvSIMD scalar x indexed element
10474 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
10475 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
10476 * | 0 1 | U | 1 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
10477 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
10478 * C3.6.18 AdvSIMD vector x indexed element
10479 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
10480 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
10481 * | 0 | Q | U | 0 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
10482 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
10484 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
10486 /* This encoding has two kinds of instruction:
10487 * normal, where we perform elt x idxelt => elt for each
10488 * element in the vector
10489 * long, where we perform elt x idxelt and generate a result of
10490 * double the width of the input element
10491 * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
10493 bool is_scalar = extract32(insn, 28, 1);
10494 bool is_q = extract32(insn, 30, 1);
10495 bool u = extract32(insn, 29, 1);
10496 int size = extract32(insn, 22, 2);
10497 int l = extract32(insn, 21, 1);
10498 int m = extract32(insn, 20, 1);
10499 /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
10500 int rm = extract32(insn, 16, 4);
10501 int opcode = extract32(insn, 12, 4);
10502 int h = extract32(insn, 11, 1);
10503 int rn = extract32(insn, 5, 5);
10504 int rd = extract32(insn, 0, 5);
10505 bool is_long = false;
10506 bool is_fp = false;
10507 int index;
10508 TCGv_ptr fpst;
10510 switch (opcode) {
10511 case 0x0: /* MLA */
10512 case 0x4: /* MLS */
10513 if (!u || is_scalar) {
10514 unallocated_encoding(s);
10515 return;
10517 break;
10518 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10519 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10520 case 0xa: /* SMULL, SMULL2, UMULL, UMULL2 */
10521 if (is_scalar) {
10522 unallocated_encoding(s);
10523 return;
10525 is_long = true;
10526 break;
10527 case 0x3: /* SQDMLAL, SQDMLAL2 */
10528 case 0x7: /* SQDMLSL, SQDMLSL2 */
10529 case 0xb: /* SQDMULL, SQDMULL2 */
10530 is_long = true;
10531 /* fall through */
10532 case 0xc: /* SQDMULH */
10533 case 0xd: /* SQRDMULH */
10534 if (u) {
10535 unallocated_encoding(s);
10536 return;
10538 break;
10539 case 0x8: /* MUL */
10540 if (u || is_scalar) {
10541 unallocated_encoding(s);
10542 return;
10544 break;
10545 case 0x1: /* FMLA */
10546 case 0x5: /* FMLS */
10547 if (u) {
10548 unallocated_encoding(s);
10549 return;
10551 /* fall through */
10552 case 0x9: /* FMUL, FMULX */
10553 if (!extract32(size, 1, 1)) {
10554 unallocated_encoding(s);
10555 return;
10557 is_fp = true;
10558 break;
10559 default:
10560 unallocated_encoding(s);
10561 return;
10564 if (is_fp) {
10565 /* low bit of size indicates single/double */
10566 size = extract32(size, 0, 1) ? 3 : 2;
10567 if (size == 2) {
10568 index = h << 1 | l;
10569 } else {
10570 if (l || !is_q) {
10571 unallocated_encoding(s);
10572 return;
10574 index = h;
10576 rm |= (m << 4);
10577 } else {
10578 switch (size) {
10579 case 1:
10580 index = h << 2 | l << 1 | m;
10581 break;
10582 case 2:
10583 index = h << 1 | l;
10584 rm |= (m << 4);
10585 break;
10586 default:
10587 unallocated_encoding(s);
10588 return;
10592 if (!fp_access_check(s)) {
10593 return;
10596 if (is_fp) {
10597 fpst = get_fpstatus_ptr();
10598 } else {
10599 TCGV_UNUSED_PTR(fpst);
10602 if (size == 3) {
10603 TCGv_i64 tcg_idx = tcg_temp_new_i64();
10604 int pass;
10606 assert(is_fp && is_q && !is_long);
10608 read_vec_element(s, tcg_idx, rm, index, MO_64);
10610 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10611 TCGv_i64 tcg_op = tcg_temp_new_i64();
10612 TCGv_i64 tcg_res = tcg_temp_new_i64();
10614 read_vec_element(s, tcg_op, rn, pass, MO_64);
10616 switch (opcode) {
10617 case 0x5: /* FMLS */
10618 /* As usual for ARM, separate negation for fused multiply-add */
10619 gen_helper_vfp_negd(tcg_op, tcg_op);
10620 /* fall through */
10621 case 0x1: /* FMLA */
10622 read_vec_element(s, tcg_res, rd, pass, MO_64);
10623 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
10624 break;
10625 case 0x9: /* FMUL, FMULX */
10626 if (u) {
10627 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
10628 } else {
10629 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
10631 break;
10632 default:
10633 g_assert_not_reached();
10636 write_vec_element(s, tcg_res, rd, pass, MO_64);
10637 tcg_temp_free_i64(tcg_op);
10638 tcg_temp_free_i64(tcg_res);
10641 if (is_scalar) {
10642 clear_vec_high(s, rd);
10645 tcg_temp_free_i64(tcg_idx);
10646 } else if (!is_long) {
10647 /* 32 bit floating point, or 16 or 32 bit integer.
10648 * For the 16 bit scalar case we use the usual Neon helpers and
10649 * rely on the fact that 0 op 0 == 0 with no side effects.
10651 TCGv_i32 tcg_idx = tcg_temp_new_i32();
10652 int pass, maxpasses;
10654 if (is_scalar) {
10655 maxpasses = 1;
10656 } else {
10657 maxpasses = is_q ? 4 : 2;
10660 read_vec_element_i32(s, tcg_idx, rm, index, size);
10662 if (size == 1 && !is_scalar) {
10663 /* The simplest way to handle the 16x16 indexed ops is to duplicate
10664 * the index into both halves of the 32 bit tcg_idx and then use
10665 * the usual Neon helpers.
10667 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
10670 for (pass = 0; pass < maxpasses; pass++) {
10671 TCGv_i32 tcg_op = tcg_temp_new_i32();
10672 TCGv_i32 tcg_res = tcg_temp_new_i32();
10674 read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
10676 switch (opcode) {
10677 case 0x0: /* MLA */
10678 case 0x4: /* MLS */
10679 case 0x8: /* MUL */
10681 static NeonGenTwoOpFn * const fns[2][2] = {
10682 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
10683 { tcg_gen_add_i32, tcg_gen_sub_i32 },
10685 NeonGenTwoOpFn *genfn;
10686 bool is_sub = opcode == 0x4;
10688 if (size == 1) {
10689 gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
10690 } else {
10691 tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
10693 if (opcode == 0x8) {
10694 break;
10696 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
10697 genfn = fns[size - 1][is_sub];
10698 genfn(tcg_res, tcg_op, tcg_res);
10699 break;
10701 case 0x5: /* FMLS */
10702 /* As usual for ARM, separate negation for fused multiply-add */
10703 gen_helper_vfp_negs(tcg_op, tcg_op);
10704 /* fall through */
10705 case 0x1: /* FMLA */
10706 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10707 gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
10708 break;
10709 case 0x9: /* FMUL, FMULX */
10710 if (u) {
10711 gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
10712 } else {
10713 gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
10715 break;
10716 case 0xc: /* SQDMULH */
10717 if (size == 1) {
10718 gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
10719 tcg_op, tcg_idx);
10720 } else {
10721 gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
10722 tcg_op, tcg_idx);
10724 break;
10725 case 0xd: /* SQRDMULH */
10726 if (size == 1) {
10727 gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
10728 tcg_op, tcg_idx);
10729 } else {
10730 gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
10731 tcg_op, tcg_idx);
10733 break;
10734 default:
10735 g_assert_not_reached();
10738 if (is_scalar) {
10739 write_fp_sreg(s, rd, tcg_res);
10740 } else {
10741 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10744 tcg_temp_free_i32(tcg_op);
10745 tcg_temp_free_i32(tcg_res);
10748 tcg_temp_free_i32(tcg_idx);
10750 if (!is_q) {
10751 clear_vec_high(s, rd);
10753 } else {
10754 /* long ops: 16x16->32 or 32x32->64 */
10755 TCGv_i64 tcg_res[2];
10756 int pass;
10757 bool satop = extract32(opcode, 0, 1);
10758 TCGMemOp memop = MO_32;
10760 if (satop || !u) {
10761 memop |= MO_SIGN;
10764 if (size == 2) {
10765 TCGv_i64 tcg_idx = tcg_temp_new_i64();
10767 read_vec_element(s, tcg_idx, rm, index, memop);
10769 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10770 TCGv_i64 tcg_op = tcg_temp_new_i64();
10771 TCGv_i64 tcg_passres;
10772 int passelt;
10774 if (is_scalar) {
10775 passelt = 0;
10776 } else {
10777 passelt = pass + (is_q * 2);
10780 read_vec_element(s, tcg_op, rn, passelt, memop);
10782 tcg_res[pass] = tcg_temp_new_i64();
10784 if (opcode == 0xa || opcode == 0xb) {
10785 /* Non-accumulating ops */
10786 tcg_passres = tcg_res[pass];
10787 } else {
10788 tcg_passres = tcg_temp_new_i64();
10791 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
10792 tcg_temp_free_i64(tcg_op);
10794 if (satop) {
10795 /* saturating, doubling */
10796 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10797 tcg_passres, tcg_passres);
10800 if (opcode == 0xa || opcode == 0xb) {
10801 continue;
10804 /* Accumulating op: handle accumulate step */
10805 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10807 switch (opcode) {
10808 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10809 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10810 break;
10811 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10812 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10813 break;
10814 case 0x7: /* SQDMLSL, SQDMLSL2 */
10815 tcg_gen_neg_i64(tcg_passres, tcg_passres);
10816 /* fall through */
10817 case 0x3: /* SQDMLAL, SQDMLAL2 */
10818 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10819 tcg_res[pass],
10820 tcg_passres);
10821 break;
10822 default:
10823 g_assert_not_reached();
10825 tcg_temp_free_i64(tcg_passres);
10827 tcg_temp_free_i64(tcg_idx);
10829 if (is_scalar) {
10830 clear_vec_high(s, rd);
10832 } else {
10833 TCGv_i32 tcg_idx = tcg_temp_new_i32();
10835 assert(size == 1);
10836 read_vec_element_i32(s, tcg_idx, rm, index, size);
10838 if (!is_scalar) {
10839 /* The simplest way to handle the 16x16 indexed ops is to
10840 * duplicate the index into both halves of the 32 bit tcg_idx
10841 * and then use the usual Neon helpers.
10843 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
10846 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10847 TCGv_i32 tcg_op = tcg_temp_new_i32();
10848 TCGv_i64 tcg_passres;
10850 if (is_scalar) {
10851 read_vec_element_i32(s, tcg_op, rn, pass, size);
10852 } else {
10853 read_vec_element_i32(s, tcg_op, rn,
10854 pass + (is_q * 2), MO_32);
10857 tcg_res[pass] = tcg_temp_new_i64();
10859 if (opcode == 0xa || opcode == 0xb) {
10860 /* Non-accumulating ops */
10861 tcg_passres = tcg_res[pass];
10862 } else {
10863 tcg_passres = tcg_temp_new_i64();
10866 if (memop & MO_SIGN) {
10867 gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
10868 } else {
10869 gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
10871 if (satop) {
10872 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10873 tcg_passres, tcg_passres);
10875 tcg_temp_free_i32(tcg_op);
10877 if (opcode == 0xa || opcode == 0xb) {
10878 continue;
10881 /* Accumulating op: handle accumulate step */
10882 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10884 switch (opcode) {
10885 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10886 gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
10887 tcg_passres);
10888 break;
10889 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10890 gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
10891 tcg_passres);
10892 break;
10893 case 0x7: /* SQDMLSL, SQDMLSL2 */
10894 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10895 /* fall through */
10896 case 0x3: /* SQDMLAL, SQDMLAL2 */
10897 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10898 tcg_res[pass],
10899 tcg_passres);
10900 break;
10901 default:
10902 g_assert_not_reached();
10904 tcg_temp_free_i64(tcg_passres);
10906 tcg_temp_free_i32(tcg_idx);
10908 if (is_scalar) {
10909 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
10913 if (is_scalar) {
10914 tcg_res[1] = tcg_const_i64(0);
10917 for (pass = 0; pass < 2; pass++) {
10918 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10919 tcg_temp_free_i64(tcg_res[pass]);
10923 if (!TCGV_IS_UNUSED_PTR(fpst)) {
10924 tcg_temp_free_ptr(fpst);
10928 /* C3.6.19 Crypto AES
10929 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
10930 * +-----------------+------+-----------+--------+-----+------+------+
10931 * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
10932 * +-----------------+------+-----------+--------+-----+------+------+
10934 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
10936 int size = extract32(insn, 22, 2);
10937 int opcode = extract32(insn, 12, 5);
10938 int rn = extract32(insn, 5, 5);
10939 int rd = extract32(insn, 0, 5);
10940 int decrypt;
10941 TCGv_i32 tcg_rd_regno, tcg_rn_regno, tcg_decrypt;
10942 CryptoThreeOpEnvFn *genfn;
10944 if (!arm_dc_feature(s, ARM_FEATURE_V8_AES)
10945 || size != 0) {
10946 unallocated_encoding(s);
10947 return;
10950 switch (opcode) {
10951 case 0x4: /* AESE */
10952 decrypt = 0;
10953 genfn = gen_helper_crypto_aese;
10954 break;
10955 case 0x6: /* AESMC */
10956 decrypt = 0;
10957 genfn = gen_helper_crypto_aesmc;
10958 break;
10959 case 0x5: /* AESD */
10960 decrypt = 1;
10961 genfn = gen_helper_crypto_aese;
10962 break;
10963 case 0x7: /* AESIMC */
10964 decrypt = 1;
10965 genfn = gen_helper_crypto_aesmc;
10966 break;
10967 default:
10968 unallocated_encoding(s);
10969 return;
10972 /* Note that we convert the Vx register indexes into the
10973 * index within the vfp.regs[] array, so we can share the
10974 * helper with the AArch32 instructions.
10976 tcg_rd_regno = tcg_const_i32(rd << 1);
10977 tcg_rn_regno = tcg_const_i32(rn << 1);
10978 tcg_decrypt = tcg_const_i32(decrypt);
10980 genfn(cpu_env, tcg_rd_regno, tcg_rn_regno, tcg_decrypt);
10982 tcg_temp_free_i32(tcg_rd_regno);
10983 tcg_temp_free_i32(tcg_rn_regno);
10984 tcg_temp_free_i32(tcg_decrypt);
10987 /* C3.6.20 Crypto three-reg SHA
10988 * 31 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
10989 * +-----------------+------+---+------+---+--------+-----+------+------+
10990 * | 0 1 0 1 1 1 1 0 | size | 0 | Rm | 0 | opcode | 0 0 | Rn | Rd |
10991 * +-----------------+------+---+------+---+--------+-----+------+------+
10993 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
10995 int size = extract32(insn, 22, 2);
10996 int opcode = extract32(insn, 12, 3);
10997 int rm = extract32(insn, 16, 5);
10998 int rn = extract32(insn, 5, 5);
10999 int rd = extract32(insn, 0, 5);
11000 CryptoThreeOpEnvFn *genfn;
11001 TCGv_i32 tcg_rd_regno, tcg_rn_regno, tcg_rm_regno;
11002 int feature = ARM_FEATURE_V8_SHA256;
11004 if (size != 0) {
11005 unallocated_encoding(s);
11006 return;
11009 switch (opcode) {
11010 case 0: /* SHA1C */
11011 case 1: /* SHA1P */
11012 case 2: /* SHA1M */
11013 case 3: /* SHA1SU0 */
11014 genfn = NULL;
11015 feature = ARM_FEATURE_V8_SHA1;
11016 break;
11017 case 4: /* SHA256H */
11018 genfn = gen_helper_crypto_sha256h;
11019 break;
11020 case 5: /* SHA256H2 */
11021 genfn = gen_helper_crypto_sha256h2;
11022 break;
11023 case 6: /* SHA256SU1 */
11024 genfn = gen_helper_crypto_sha256su1;
11025 break;
11026 default:
11027 unallocated_encoding(s);
11028 return;
11031 if (!arm_dc_feature(s, feature)) {
11032 unallocated_encoding(s);
11033 return;
11036 tcg_rd_regno = tcg_const_i32(rd << 1);
11037 tcg_rn_regno = tcg_const_i32(rn << 1);
11038 tcg_rm_regno = tcg_const_i32(rm << 1);
11040 if (genfn) {
11041 genfn(cpu_env, tcg_rd_regno, tcg_rn_regno, tcg_rm_regno);
11042 } else {
11043 TCGv_i32 tcg_opcode = tcg_const_i32(opcode);
11045 gen_helper_crypto_sha1_3reg(cpu_env, tcg_rd_regno,
11046 tcg_rn_regno, tcg_rm_regno, tcg_opcode);
11047 tcg_temp_free_i32(tcg_opcode);
11050 tcg_temp_free_i32(tcg_rd_regno);
11051 tcg_temp_free_i32(tcg_rn_regno);
11052 tcg_temp_free_i32(tcg_rm_regno);
11055 /* C3.6.21 Crypto two-reg SHA
11056 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
11057 * +-----------------+------+-----------+--------+-----+------+------+
11058 * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
11059 * +-----------------+------+-----------+--------+-----+------+------+
11061 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
11063 int size = extract32(insn, 22, 2);
11064 int opcode = extract32(insn, 12, 5);
11065 int rn = extract32(insn, 5, 5);
11066 int rd = extract32(insn, 0, 5);
11067 CryptoTwoOpEnvFn *genfn;
11068 int feature;
11069 TCGv_i32 tcg_rd_regno, tcg_rn_regno;
11071 if (size != 0) {
11072 unallocated_encoding(s);
11073 return;
11076 switch (opcode) {
11077 case 0: /* SHA1H */
11078 feature = ARM_FEATURE_V8_SHA1;
11079 genfn = gen_helper_crypto_sha1h;
11080 break;
11081 case 1: /* SHA1SU1 */
11082 feature = ARM_FEATURE_V8_SHA1;
11083 genfn = gen_helper_crypto_sha1su1;
11084 break;
11085 case 2: /* SHA256SU0 */
11086 feature = ARM_FEATURE_V8_SHA256;
11087 genfn = gen_helper_crypto_sha256su0;
11088 break;
11089 default:
11090 unallocated_encoding(s);
11091 return;
11094 if (!arm_dc_feature(s, feature)) {
11095 unallocated_encoding(s);
11096 return;
11099 tcg_rd_regno = tcg_const_i32(rd << 1);
11100 tcg_rn_regno = tcg_const_i32(rn << 1);
11102 genfn(cpu_env, tcg_rd_regno, tcg_rn_regno);
11104 tcg_temp_free_i32(tcg_rd_regno);
11105 tcg_temp_free_i32(tcg_rn_regno);
11108 /* C3.6 Data processing - SIMD, inc Crypto
11110 * As the decode gets a little complex we are using a table based
11111 * approach for this part of the decode.
11113 static const AArch64DecodeTable data_proc_simd[] = {
11114 /* pattern , mask , fn */
11115 { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
11116 { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
11117 { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
11118 { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
11119 { 0x0e000400, 0x9fe08400, disas_simd_copy },
11120 { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
11121 /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
11122 { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
11123 { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
11124 { 0x0e000000, 0xbf208c00, disas_simd_tb },
11125 { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
11126 { 0x2e000000, 0xbf208400, disas_simd_ext },
11127 { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
11128 { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
11129 { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
11130 { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
11131 { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
11132 { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
11133 { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
11134 { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
11135 { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
11136 { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
11137 { 0x00000000, 0x00000000, NULL }
11140 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
11142 /* Note that this is called with all non-FP cases from
11143 * table C3-6 so it must UNDEF for entries not specifically
11144 * allocated to instructions in that table.
11146 AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
11147 if (fn) {
11148 fn(s, insn);
11149 } else {
11150 unallocated_encoding(s);
11154 /* C3.6 Data processing - SIMD and floating point */
11155 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
11157 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
11158 disas_data_proc_fp(s, insn);
11159 } else {
11160 /* SIMD, including crypto */
11161 disas_data_proc_simd(s, insn);
11165 /* C3.1 A64 instruction index by encoding */
11166 static void disas_a64_insn(CPUARMState *env, DisasContext *s)
11168 uint32_t insn;
11170 insn = arm_ldl_code(env, s->pc, s->sctlr_b);
11171 s->insn = insn;
11172 s->pc += 4;
11174 s->fp_access_checked = false;
11176 switch (extract32(insn, 25, 4)) {
11177 case 0x0: case 0x1: case 0x2: case 0x3: /* UNALLOCATED */
11178 unallocated_encoding(s);
11179 break;
11180 case 0x8: case 0x9: /* Data processing - immediate */
11181 disas_data_proc_imm(s, insn);
11182 break;
11183 case 0xa: case 0xb: /* Branch, exception generation and system insns */
11184 disas_b_exc_sys(s, insn);
11185 break;
11186 case 0x4:
11187 case 0x6:
11188 case 0xc:
11189 case 0xe: /* Loads and stores */
11190 disas_ldst(s, insn);
11191 break;
11192 case 0x5:
11193 case 0xd: /* Data processing - register */
11194 disas_data_proc_reg(s, insn);
11195 break;
11196 case 0x7:
11197 case 0xf: /* Data processing - SIMD and floating point */
11198 disas_data_proc_simd_fp(s, insn);
11199 break;
11200 default:
11201 assert(FALSE); /* all 15 cases should be handled above */
11202 break;
11205 /* if we allocated any temporaries, free them here */
11206 free_tmp_a64(s);
11209 void gen_intermediate_code_a64(ARMCPU *cpu, TranslationBlock *tb)
11211 CPUState *cs = CPU(cpu);
11212 CPUARMState *env = &cpu->env;
11213 DisasContext dc1, *dc = &dc1;
11214 target_ulong pc_start;
11215 target_ulong next_page_start;
11216 int num_insns;
11217 int max_insns;
11219 pc_start = tb->pc;
11221 dc->tb = tb;
11223 dc->is_jmp = DISAS_NEXT;
11224 dc->pc = pc_start;
11225 dc->singlestep_enabled = cs->singlestep_enabled;
11226 dc->condjmp = 0;
11228 dc->aarch64 = 1;
11229 /* If we are coming from secure EL0 in a system with a 32-bit EL3, then
11230 * there is no secure EL1, so we route exceptions to EL3.
11232 dc->secure_routed_to_el3 = arm_feature(env, ARM_FEATURE_EL3) &&
11233 !arm_el_is_aa64(env, 3);
11234 dc->thumb = 0;
11235 dc->sctlr_b = 0;
11236 dc->be_data = ARM_TBFLAG_BE_DATA(tb->flags) ? MO_BE : MO_LE;
11237 dc->condexec_mask = 0;
11238 dc->condexec_cond = 0;
11239 dc->mmu_idx = ARM_TBFLAG_MMUIDX(tb->flags);
11240 dc->tbi0 = ARM_TBFLAG_TBI0(tb->flags);
11241 dc->tbi1 = ARM_TBFLAG_TBI1(tb->flags);
11242 dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
11243 #if !defined(CONFIG_USER_ONLY)
11244 dc->user = (dc->current_el == 0);
11245 #endif
11246 dc->fp_excp_el = ARM_TBFLAG_FPEXC_EL(tb->flags);
11247 dc->vec_len = 0;
11248 dc->vec_stride = 0;
11249 dc->cp_regs = cpu->cp_regs;
11250 dc->features = env->features;
11252 /* Single step state. The code-generation logic here is:
11253 * SS_ACTIVE == 0:
11254 * generate code with no special handling for single-stepping (except
11255 * that anything that can make us go to SS_ACTIVE == 1 must end the TB;
11256 * this happens anyway because those changes are all system register or
11257 * PSTATE writes).
11258 * SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
11259 * emit code for one insn
11260 * emit code to clear PSTATE.SS
11261 * emit code to generate software step exception for completed step
11262 * end TB (as usual for having generated an exception)
11263 * SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
11264 * emit code to generate a software step exception
11265 * end the TB
11267 dc->ss_active = ARM_TBFLAG_SS_ACTIVE(tb->flags);
11268 dc->pstate_ss = ARM_TBFLAG_PSTATE_SS(tb->flags);
11269 dc->is_ldex = false;
11270 dc->ss_same_el = (arm_debug_target_el(env) == dc->current_el);
11272 init_tmp_a64_array(dc);
11274 next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
11275 num_insns = 0;
11276 max_insns = tb->cflags & CF_COUNT_MASK;
11277 if (max_insns == 0) {
11278 max_insns = CF_COUNT_MASK;
11280 if (max_insns > TCG_MAX_INSNS) {
11281 max_insns = TCG_MAX_INSNS;
11284 gen_tb_start(tb);
11286 tcg_clear_temp_count();
11288 do {
11289 dc->insn_start_idx = tcg_op_buf_count();
11290 tcg_gen_insn_start(dc->pc, 0, 0);
11291 num_insns++;
11293 if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) {
11294 CPUBreakpoint *bp;
11295 QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
11296 if (bp->pc == dc->pc) {
11297 if (bp->flags & BP_CPU) {
11298 gen_a64_set_pc_im(dc->pc);
11299 gen_helper_check_breakpoints(cpu_env);
11300 /* End the TB early; it likely won't be executed */
11301 dc->is_jmp = DISAS_UPDATE;
11302 } else {
11303 gen_exception_internal_insn(dc, 0, EXCP_DEBUG);
11304 /* The address covered by the breakpoint must be
11305 included in [tb->pc, tb->pc + tb->size) in order
11306 to for it to be properly cleared -- thus we
11307 increment the PC here so that the logic setting
11308 tb->size below does the right thing. */
11309 dc->pc += 4;
11310 goto done_generating;
11312 break;
11317 if (num_insns == max_insns && (tb->cflags & CF_LAST_IO)) {
11318 gen_io_start();
11321 if (dc->ss_active && !dc->pstate_ss) {
11322 /* Singlestep state is Active-pending.
11323 * If we're in this state at the start of a TB then either
11324 * a) we just took an exception to an EL which is being debugged
11325 * and this is the first insn in the exception handler
11326 * b) debug exceptions were masked and we just unmasked them
11327 * without changing EL (eg by clearing PSTATE.D)
11328 * In either case we're going to take a swstep exception in the
11329 * "did not step an insn" case, and so the syndrome ISV and EX
11330 * bits should be zero.
11332 assert(num_insns == 1);
11333 gen_exception(EXCP_UDEF, syn_swstep(dc->ss_same_el, 0, 0),
11334 default_exception_el(dc));
11335 dc->is_jmp = DISAS_EXC;
11336 break;
11339 disas_a64_insn(env, dc);
11341 if (tcg_check_temp_count()) {
11342 fprintf(stderr, "TCG temporary leak before "TARGET_FMT_lx"\n",
11343 dc->pc);
11346 /* Translation stops when a conditional branch is encountered.
11347 * Otherwise the subsequent code could get translated several times.
11348 * Also stop translation when a page boundary is reached. This
11349 * ensures prefetch aborts occur at the right place.
11351 } while (!dc->is_jmp && !tcg_op_buf_full() &&
11352 !cs->singlestep_enabled &&
11353 !singlestep &&
11354 !dc->ss_active &&
11355 dc->pc < next_page_start &&
11356 num_insns < max_insns);
11358 if (tb->cflags & CF_LAST_IO) {
11359 gen_io_end();
11362 if (unlikely(cs->singlestep_enabled || dc->ss_active)
11363 && dc->is_jmp != DISAS_EXC) {
11364 /* Note that this means single stepping WFI doesn't halt the CPU.
11365 * For conditional branch insns this is harmless unreachable code as
11366 * gen_goto_tb() has already handled emitting the debug exception
11367 * (and thus a tb-jump is not possible when singlestepping).
11369 assert(dc->is_jmp != DISAS_TB_JUMP);
11370 if (dc->is_jmp != DISAS_JUMP) {
11371 gen_a64_set_pc_im(dc->pc);
11373 if (cs->singlestep_enabled) {
11374 gen_exception_internal(EXCP_DEBUG);
11375 } else {
11376 gen_step_complete_exception(dc);
11378 } else {
11379 switch (dc->is_jmp) {
11380 case DISAS_NEXT:
11381 gen_goto_tb(dc, 1, dc->pc);
11382 break;
11383 default:
11384 case DISAS_UPDATE:
11385 gen_a64_set_pc_im(dc->pc);
11386 /* fall through */
11387 case DISAS_JUMP:
11388 /* indicate that the hash table must be used to find the next TB */
11389 tcg_gen_exit_tb(0);
11390 break;
11391 case DISAS_TB_JUMP:
11392 case DISAS_EXC:
11393 case DISAS_SWI:
11394 break;
11395 case DISAS_WFE:
11396 gen_a64_set_pc_im(dc->pc);
11397 gen_helper_wfe(cpu_env);
11398 break;
11399 case DISAS_YIELD:
11400 gen_a64_set_pc_im(dc->pc);
11401 gen_helper_yield(cpu_env);
11402 break;
11403 case DISAS_WFI:
11404 /* This is a special case because we don't want to just halt the CPU
11405 * if trying to debug across a WFI.
11407 gen_a64_set_pc_im(dc->pc);
11408 gen_helper_wfi(cpu_env);
11409 /* The helper doesn't necessarily throw an exception, but we
11410 * must go back to the main loop to check for interrupts anyway.
11412 tcg_gen_exit_tb(0);
11413 break;
11417 done_generating:
11418 gen_tb_end(tb, num_insns);
11420 #ifdef DEBUG_DISAS
11421 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) &&
11422 qemu_log_in_addr_range(pc_start)) {
11423 qemu_log_lock();
11424 qemu_log("----------------\n");
11425 qemu_log("IN: %s\n", lookup_symbol(pc_start));
11426 log_target_disas(cs, pc_start, dc->pc - pc_start,
11427 4 | (bswap_code(dc->sctlr_b) ? 2 : 0));
11428 qemu_log("\n");
11429 qemu_log_unlock();
11431 #endif
11432 tb->size = dc->pc - pc_start;
11433 tb->icount = num_insns;