block/iscsi: avoid potential overflow of acb->task->cdb
[qemu.git] / target-arm / translate-a64.c
blobce8141a4429b3f3d8aef7a66d7bf3a68384e69ed
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;
45 static const char *regnames[] = {
46 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
47 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
48 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
49 "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
52 enum a64_shift_type {
53 A64_SHIFT_TYPE_LSL = 0,
54 A64_SHIFT_TYPE_LSR = 1,
55 A64_SHIFT_TYPE_ASR = 2,
56 A64_SHIFT_TYPE_ROR = 3
59 /* Table based decoder typedefs - used when the relevant bits for decode
60 * are too awkwardly scattered across the instruction (eg SIMD).
62 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
64 typedef struct AArch64DecodeTable {
65 uint32_t pattern;
66 uint32_t mask;
67 AArch64DecodeFn *disas_fn;
68 } AArch64DecodeTable;
70 /* Function prototype for gen_ functions for calling Neon helpers */
71 typedef void NeonGenOneOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32);
72 typedef void NeonGenTwoOpFn(TCGv_i32, TCGv_i32, TCGv_i32);
73 typedef void NeonGenTwoOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32, TCGv_i32);
74 typedef void NeonGenTwo64OpFn(TCGv_i64, TCGv_i64, TCGv_i64);
75 typedef void NeonGenTwo64OpEnvFn(TCGv_i64, TCGv_ptr, TCGv_i64, TCGv_i64);
76 typedef void NeonGenNarrowFn(TCGv_i32, TCGv_i64);
77 typedef void NeonGenNarrowEnvFn(TCGv_i32, TCGv_ptr, TCGv_i64);
78 typedef void NeonGenWidenFn(TCGv_i64, TCGv_i32);
79 typedef void NeonGenTwoSingleOPFn(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
80 typedef void NeonGenTwoDoubleOPFn(TCGv_i64, TCGv_i64, TCGv_i64, TCGv_ptr);
81 typedef void NeonGenOneOpFn(TCGv_i64, TCGv_i64);
82 typedef void CryptoTwoOpEnvFn(TCGv_ptr, TCGv_i32, TCGv_i32);
83 typedef void CryptoThreeOpEnvFn(TCGv_ptr, TCGv_i32, TCGv_i32, TCGv_i32);
85 /* initialize TCG globals. */
86 void a64_translate_init(void)
88 int i;
90 cpu_pc = tcg_global_mem_new_i64(cpu_env,
91 offsetof(CPUARMState, pc),
92 "pc");
93 for (i = 0; i < 32; i++) {
94 cpu_X[i] = tcg_global_mem_new_i64(cpu_env,
95 offsetof(CPUARMState, xregs[i]),
96 regnames[i]);
99 cpu_exclusive_high = tcg_global_mem_new_i64(cpu_env,
100 offsetof(CPUARMState, exclusive_high), "exclusive_high");
103 static inline ARMMMUIdx get_a64_user_mem_index(DisasContext *s)
105 /* Return the mmu_idx to use for A64 "unprivileged load/store" insns:
106 * if EL1, access as if EL0; otherwise access at current EL
108 switch (s->mmu_idx) {
109 case ARMMMUIdx_S12NSE1:
110 return ARMMMUIdx_S12NSE0;
111 case ARMMMUIdx_S1SE1:
112 return ARMMMUIdx_S1SE0;
113 case ARMMMUIdx_S2NS:
114 g_assert_not_reached();
115 default:
116 return s->mmu_idx;
120 void aarch64_cpu_dump_state(CPUState *cs, FILE *f,
121 fprintf_function cpu_fprintf, int flags)
123 ARMCPU *cpu = ARM_CPU(cs);
124 CPUARMState *env = &cpu->env;
125 uint32_t psr = pstate_read(env);
126 int i;
127 int el = arm_current_el(env);
128 const char *ns_status;
130 cpu_fprintf(f, "PC=%016"PRIx64" SP=%016"PRIx64"\n",
131 env->pc, env->xregs[31]);
132 for (i = 0; i < 31; i++) {
133 cpu_fprintf(f, "X%02d=%016"PRIx64, i, env->xregs[i]);
134 if ((i % 4) == 3) {
135 cpu_fprintf(f, "\n");
136 } else {
137 cpu_fprintf(f, " ");
141 if (arm_feature(env, ARM_FEATURE_EL3) && el != 3) {
142 ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
143 } else {
144 ns_status = "";
147 cpu_fprintf(f, "\nPSTATE=%08x %c%c%c%c %sEL%d%c\n",
148 psr,
149 psr & PSTATE_N ? 'N' : '-',
150 psr & PSTATE_Z ? 'Z' : '-',
151 psr & PSTATE_C ? 'C' : '-',
152 psr & PSTATE_V ? 'V' : '-',
153 ns_status,
155 psr & PSTATE_SP ? 'h' : 't');
157 if (flags & CPU_DUMP_FPU) {
158 int numvfpregs = 32;
159 for (i = 0; i < numvfpregs; i += 2) {
160 uint64_t vlo = float64_val(env->vfp.regs[i * 2]);
161 uint64_t vhi = float64_val(env->vfp.regs[(i * 2) + 1]);
162 cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 " ",
163 i, vhi, vlo);
164 vlo = float64_val(env->vfp.regs[(i + 1) * 2]);
165 vhi = float64_val(env->vfp.regs[((i + 1) * 2) + 1]);
166 cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 "\n",
167 i + 1, vhi, vlo);
169 cpu_fprintf(f, "FPCR: %08x FPSR: %08x\n",
170 vfp_get_fpcr(env), vfp_get_fpsr(env));
174 void gen_a64_set_pc_im(uint64_t val)
176 tcg_gen_movi_i64(cpu_pc, val);
179 typedef struct DisasCompare64 {
180 TCGCond cond;
181 TCGv_i64 value;
182 } DisasCompare64;
184 static void a64_test_cc(DisasCompare64 *c64, int cc)
186 DisasCompare c32;
188 arm_test_cc(&c32, cc);
190 /* Sign-extend the 32-bit value so that the GE/LT comparisons work
191 * properly. The NE/EQ comparisons are also fine with this choice. */
192 c64->cond = c32.cond;
193 c64->value = tcg_temp_new_i64();
194 tcg_gen_ext_i32_i64(c64->value, c32.value);
196 arm_free_cc(&c32);
199 static void a64_free_cc(DisasCompare64 *c64)
201 tcg_temp_free_i64(c64->value);
204 static void gen_exception_internal(int excp)
206 TCGv_i32 tcg_excp = tcg_const_i32(excp);
208 assert(excp_is_internal(excp));
209 gen_helper_exception_internal(cpu_env, tcg_excp);
210 tcg_temp_free_i32(tcg_excp);
213 static void gen_exception(int excp, uint32_t syndrome, uint32_t target_el)
215 TCGv_i32 tcg_excp = tcg_const_i32(excp);
216 TCGv_i32 tcg_syn = tcg_const_i32(syndrome);
217 TCGv_i32 tcg_el = tcg_const_i32(target_el);
219 gen_helper_exception_with_syndrome(cpu_env, tcg_excp,
220 tcg_syn, tcg_el);
221 tcg_temp_free_i32(tcg_el);
222 tcg_temp_free_i32(tcg_syn);
223 tcg_temp_free_i32(tcg_excp);
226 static void gen_exception_internal_insn(DisasContext *s, int offset, int excp)
228 gen_a64_set_pc_im(s->pc - offset);
229 gen_exception_internal(excp);
230 s->is_jmp = DISAS_EXC;
233 static void gen_exception_insn(DisasContext *s, int offset, int excp,
234 uint32_t syndrome, uint32_t target_el)
236 gen_a64_set_pc_im(s->pc - offset);
237 gen_exception(excp, syndrome, target_el);
238 s->is_jmp = DISAS_EXC;
241 static void gen_ss_advance(DisasContext *s)
243 /* If the singlestep state is Active-not-pending, advance to
244 * Active-pending.
246 if (s->ss_active) {
247 s->pstate_ss = 0;
248 gen_helper_clear_pstate_ss(cpu_env);
252 static void gen_step_complete_exception(DisasContext *s)
254 /* We just completed step of an insn. Move from Active-not-pending
255 * to Active-pending, and then also take the swstep exception.
256 * This corresponds to making the (IMPDEF) choice to prioritize
257 * swstep exceptions over asynchronous exceptions taken to an exception
258 * level where debug is disabled. This choice has the advantage that
259 * we do not need to maintain internal state corresponding to the
260 * ISV/EX syndrome bits between completion of the step and generation
261 * of the exception, and our syndrome information is always correct.
263 gen_ss_advance(s);
264 gen_exception(EXCP_UDEF, syn_swstep(s->ss_same_el, 1, s->is_ldex),
265 default_exception_el(s));
266 s->is_jmp = DISAS_EXC;
269 static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
271 /* No direct tb linking with singlestep (either QEMU's or the ARM
272 * debug architecture kind) or deterministic io
274 if (s->singlestep_enabled || s->ss_active || (s->tb->cflags & CF_LAST_IO)) {
275 return false;
278 #ifndef CONFIG_USER_ONLY
279 /* Only link tbs from inside the same guest page */
280 if ((s->tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
281 return false;
283 #endif
285 return true;
288 static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
290 TranslationBlock *tb;
292 tb = s->tb;
293 if (use_goto_tb(s, n, dest)) {
294 tcg_gen_goto_tb(n);
295 gen_a64_set_pc_im(dest);
296 tcg_gen_exit_tb((intptr_t)tb + n);
297 s->is_jmp = DISAS_TB_JUMP;
298 } else {
299 gen_a64_set_pc_im(dest);
300 if (s->ss_active) {
301 gen_step_complete_exception(s);
302 } else if (s->singlestep_enabled) {
303 gen_exception_internal(EXCP_DEBUG);
304 } else {
305 tcg_gen_exit_tb(0);
306 s->is_jmp = DISAS_TB_JUMP;
311 static void unallocated_encoding(DisasContext *s)
313 /* Unallocated and reserved encodings are uncategorized */
314 gen_exception_insn(s, 4, EXCP_UDEF, syn_uncategorized(),
315 default_exception_el(s));
318 #define unsupported_encoding(s, insn) \
319 do { \
320 qemu_log_mask(LOG_UNIMP, \
321 "%s:%d: unsupported instruction encoding 0x%08x " \
322 "at pc=%016" PRIx64 "\n", \
323 __FILE__, __LINE__, insn, s->pc - 4); \
324 unallocated_encoding(s); \
325 } while (0);
327 static void init_tmp_a64_array(DisasContext *s)
329 #ifdef CONFIG_DEBUG_TCG
330 int i;
331 for (i = 0; i < ARRAY_SIZE(s->tmp_a64); i++) {
332 TCGV_UNUSED_I64(s->tmp_a64[i]);
334 #endif
335 s->tmp_a64_count = 0;
338 static void free_tmp_a64(DisasContext *s)
340 int i;
341 for (i = 0; i < s->tmp_a64_count; i++) {
342 tcg_temp_free_i64(s->tmp_a64[i]);
344 init_tmp_a64_array(s);
347 static TCGv_i64 new_tmp_a64(DisasContext *s)
349 assert(s->tmp_a64_count < TMP_A64_MAX);
350 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
353 static TCGv_i64 new_tmp_a64_zero(DisasContext *s)
355 TCGv_i64 t = new_tmp_a64(s);
356 tcg_gen_movi_i64(t, 0);
357 return t;
361 * Register access functions
363 * These functions are used for directly accessing a register in where
364 * changes to the final register value are likely to be made. If you
365 * need to use a register for temporary calculation (e.g. index type
366 * operations) use the read_* form.
368 * B1.2.1 Register mappings
370 * In instruction register encoding 31 can refer to ZR (zero register) or
371 * the SP (stack pointer) depending on context. In QEMU's case we map SP
372 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
373 * This is the point of the _sp forms.
375 static TCGv_i64 cpu_reg(DisasContext *s, int reg)
377 if (reg == 31) {
378 return new_tmp_a64_zero(s);
379 } else {
380 return cpu_X[reg];
384 /* register access for when 31 == SP */
385 static TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
387 return cpu_X[reg];
390 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
391 * representing the register contents. This TCGv is an auto-freed
392 * temporary so it need not be explicitly freed, and may be modified.
394 static TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
396 TCGv_i64 v = new_tmp_a64(s);
397 if (reg != 31) {
398 if (sf) {
399 tcg_gen_mov_i64(v, cpu_X[reg]);
400 } else {
401 tcg_gen_ext32u_i64(v, cpu_X[reg]);
403 } else {
404 tcg_gen_movi_i64(v, 0);
406 return v;
409 static TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
411 TCGv_i64 v = new_tmp_a64(s);
412 if (sf) {
413 tcg_gen_mov_i64(v, cpu_X[reg]);
414 } else {
415 tcg_gen_ext32u_i64(v, cpu_X[reg]);
417 return v;
420 /* We should have at some point before trying to access an FP register
421 * done the necessary access check, so assert that
422 * (a) we did the check and
423 * (b) we didn't then just plough ahead anyway if it failed.
424 * Print the instruction pattern in the abort message so we can figure
425 * out what we need to fix if a user encounters this problem in the wild.
427 static inline void assert_fp_access_checked(DisasContext *s)
429 #ifdef CONFIG_DEBUG_TCG
430 if (unlikely(!s->fp_access_checked || s->fp_excp_el)) {
431 fprintf(stderr, "target-arm: FP access check missing for "
432 "instruction 0x%08x\n", s->insn);
433 abort();
435 #endif
438 /* Return the offset into CPUARMState of an element of specified
439 * size, 'element' places in from the least significant end of
440 * the FP/vector register Qn.
442 static inline int vec_reg_offset(DisasContext *s, int regno,
443 int element, TCGMemOp size)
445 int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
446 #ifdef HOST_WORDS_BIGENDIAN
447 /* This is complicated slightly because vfp.regs[2n] is
448 * still the low half and vfp.regs[2n+1] the high half
449 * of the 128 bit vector, even on big endian systems.
450 * Calculate the offset assuming a fully bigendian 128 bits,
451 * then XOR to account for the order of the two 64 bit halves.
453 offs += (16 - ((element + 1) * (1 << size)));
454 offs ^= 8;
455 #else
456 offs += element * (1 << size);
457 #endif
458 assert_fp_access_checked(s);
459 return offs;
462 /* Return the offset into CPUARMState of a slice (from
463 * the least significant end) of FP register Qn (ie
464 * Dn, Sn, Hn or Bn).
465 * (Note that this is not the same mapping as for A32; see cpu.h)
467 static inline int fp_reg_offset(DisasContext *s, int regno, TCGMemOp size)
469 int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
470 #ifdef HOST_WORDS_BIGENDIAN
471 offs += (8 - (1 << size));
472 #endif
473 assert_fp_access_checked(s);
474 return offs;
477 /* Offset of the high half of the 128 bit vector Qn */
478 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
480 assert_fp_access_checked(s);
481 return offsetof(CPUARMState, vfp.regs[regno * 2 + 1]);
484 /* Convenience accessors for reading and writing single and double
485 * FP registers. Writing clears the upper parts of the associated
486 * 128 bit vector register, as required by the architecture.
487 * Note that unlike the GP register accessors, the values returned
488 * by the read functions must be manually freed.
490 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
492 TCGv_i64 v = tcg_temp_new_i64();
494 tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
495 return v;
498 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
500 TCGv_i32 v = tcg_temp_new_i32();
502 tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
503 return v;
506 static void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
508 TCGv_i64 tcg_zero = tcg_const_i64(0);
510 tcg_gen_st_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
511 tcg_gen_st_i64(tcg_zero, cpu_env, fp_reg_hi_offset(s, reg));
512 tcg_temp_free_i64(tcg_zero);
515 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
517 TCGv_i64 tmp = tcg_temp_new_i64();
519 tcg_gen_extu_i32_i64(tmp, v);
520 write_fp_dreg(s, reg, tmp);
521 tcg_temp_free_i64(tmp);
524 static TCGv_ptr get_fpstatus_ptr(void)
526 TCGv_ptr statusptr = tcg_temp_new_ptr();
527 int offset;
529 /* In A64 all instructions (both FP and Neon) use the FPCR;
530 * there is no equivalent of the A32 Neon "standard FPSCR value"
531 * and all operations use vfp.fp_status.
533 offset = offsetof(CPUARMState, vfp.fp_status);
534 tcg_gen_addi_ptr(statusptr, cpu_env, offset);
535 return statusptr;
538 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
539 * than the 32 bit equivalent.
541 static inline void gen_set_NZ64(TCGv_i64 result)
543 tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
544 tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
547 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
548 static inline void gen_logic_CC(int sf, TCGv_i64 result)
550 if (sf) {
551 gen_set_NZ64(result);
552 } else {
553 tcg_gen_extrl_i64_i32(cpu_ZF, result);
554 tcg_gen_mov_i32(cpu_NF, cpu_ZF);
556 tcg_gen_movi_i32(cpu_CF, 0);
557 tcg_gen_movi_i32(cpu_VF, 0);
560 /* dest = T0 + T1; compute C, N, V and Z flags */
561 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
563 if (sf) {
564 TCGv_i64 result, flag, tmp;
565 result = tcg_temp_new_i64();
566 flag = tcg_temp_new_i64();
567 tmp = tcg_temp_new_i64();
569 tcg_gen_movi_i64(tmp, 0);
570 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
572 tcg_gen_extrl_i64_i32(cpu_CF, flag);
574 gen_set_NZ64(result);
576 tcg_gen_xor_i64(flag, result, t0);
577 tcg_gen_xor_i64(tmp, t0, t1);
578 tcg_gen_andc_i64(flag, flag, tmp);
579 tcg_temp_free_i64(tmp);
580 tcg_gen_extrh_i64_i32(cpu_VF, flag);
582 tcg_gen_mov_i64(dest, result);
583 tcg_temp_free_i64(result);
584 tcg_temp_free_i64(flag);
585 } else {
586 /* 32 bit arithmetic */
587 TCGv_i32 t0_32 = tcg_temp_new_i32();
588 TCGv_i32 t1_32 = tcg_temp_new_i32();
589 TCGv_i32 tmp = tcg_temp_new_i32();
591 tcg_gen_movi_i32(tmp, 0);
592 tcg_gen_extrl_i64_i32(t0_32, t0);
593 tcg_gen_extrl_i64_i32(t1_32, t1);
594 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
595 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
596 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
597 tcg_gen_xor_i32(tmp, t0_32, t1_32);
598 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
599 tcg_gen_extu_i32_i64(dest, cpu_NF);
601 tcg_temp_free_i32(tmp);
602 tcg_temp_free_i32(t0_32);
603 tcg_temp_free_i32(t1_32);
607 /* dest = T0 - T1; compute C, N, V and Z flags */
608 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
610 if (sf) {
611 /* 64 bit arithmetic */
612 TCGv_i64 result, flag, tmp;
614 result = tcg_temp_new_i64();
615 flag = tcg_temp_new_i64();
616 tcg_gen_sub_i64(result, t0, t1);
618 gen_set_NZ64(result);
620 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
621 tcg_gen_extrl_i64_i32(cpu_CF, flag);
623 tcg_gen_xor_i64(flag, result, t0);
624 tmp = tcg_temp_new_i64();
625 tcg_gen_xor_i64(tmp, t0, t1);
626 tcg_gen_and_i64(flag, flag, tmp);
627 tcg_temp_free_i64(tmp);
628 tcg_gen_extrh_i64_i32(cpu_VF, flag);
629 tcg_gen_mov_i64(dest, result);
630 tcg_temp_free_i64(flag);
631 tcg_temp_free_i64(result);
632 } else {
633 /* 32 bit arithmetic */
634 TCGv_i32 t0_32 = tcg_temp_new_i32();
635 TCGv_i32 t1_32 = tcg_temp_new_i32();
636 TCGv_i32 tmp;
638 tcg_gen_extrl_i64_i32(t0_32, t0);
639 tcg_gen_extrl_i64_i32(t1_32, t1);
640 tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
641 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
642 tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
643 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
644 tmp = tcg_temp_new_i32();
645 tcg_gen_xor_i32(tmp, t0_32, t1_32);
646 tcg_temp_free_i32(t0_32);
647 tcg_temp_free_i32(t1_32);
648 tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
649 tcg_temp_free_i32(tmp);
650 tcg_gen_extu_i32_i64(dest, cpu_NF);
654 /* dest = T0 + T1 + CF; do not compute flags. */
655 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
657 TCGv_i64 flag = tcg_temp_new_i64();
658 tcg_gen_extu_i32_i64(flag, cpu_CF);
659 tcg_gen_add_i64(dest, t0, t1);
660 tcg_gen_add_i64(dest, dest, flag);
661 tcg_temp_free_i64(flag);
663 if (!sf) {
664 tcg_gen_ext32u_i64(dest, dest);
668 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
669 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
671 if (sf) {
672 TCGv_i64 result, cf_64, vf_64, tmp;
673 result = tcg_temp_new_i64();
674 cf_64 = tcg_temp_new_i64();
675 vf_64 = tcg_temp_new_i64();
676 tmp = tcg_const_i64(0);
678 tcg_gen_extu_i32_i64(cf_64, cpu_CF);
679 tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
680 tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
681 tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
682 gen_set_NZ64(result);
684 tcg_gen_xor_i64(vf_64, result, t0);
685 tcg_gen_xor_i64(tmp, t0, t1);
686 tcg_gen_andc_i64(vf_64, vf_64, tmp);
687 tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
689 tcg_gen_mov_i64(dest, result);
691 tcg_temp_free_i64(tmp);
692 tcg_temp_free_i64(vf_64);
693 tcg_temp_free_i64(cf_64);
694 tcg_temp_free_i64(result);
695 } else {
696 TCGv_i32 t0_32, t1_32, tmp;
697 t0_32 = tcg_temp_new_i32();
698 t1_32 = tcg_temp_new_i32();
699 tmp = tcg_const_i32(0);
701 tcg_gen_extrl_i64_i32(t0_32, t0);
702 tcg_gen_extrl_i64_i32(t1_32, t1);
703 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
704 tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
706 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
707 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
708 tcg_gen_xor_i32(tmp, t0_32, t1_32);
709 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
710 tcg_gen_extu_i32_i64(dest, cpu_NF);
712 tcg_temp_free_i32(tmp);
713 tcg_temp_free_i32(t1_32);
714 tcg_temp_free_i32(t0_32);
719 * Load/Store generators
723 * Store from GPR register to memory.
725 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
726 TCGv_i64 tcg_addr, int size, int memidx)
728 g_assert(size <= 3);
729 tcg_gen_qemu_st_i64(source, tcg_addr, memidx, s->be_data + size);
732 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
733 TCGv_i64 tcg_addr, int size)
735 do_gpr_st_memidx(s, source, tcg_addr, size, get_mem_index(s));
739 * Load from memory to GPR register
741 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
742 int size, bool is_signed, bool extend, int memidx)
744 TCGMemOp memop = s->be_data + size;
746 g_assert(size <= 3);
748 if (is_signed) {
749 memop += MO_SIGN;
752 tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
754 if (extend && is_signed) {
755 g_assert(size < 3);
756 tcg_gen_ext32u_i64(dest, dest);
760 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
761 int size, bool is_signed, bool extend)
763 do_gpr_ld_memidx(s, dest, tcg_addr, size, is_signed, extend,
764 get_mem_index(s));
768 * Store from FP register to memory
770 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
772 /* This writes the bottom N bits of a 128 bit wide vector to memory */
773 TCGv_i64 tmp = tcg_temp_new_i64();
774 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_offset(s, srcidx, MO_64));
775 if (size < 4) {
776 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s),
777 s->be_data + size);
778 } else {
779 bool be = s->be_data == MO_BE;
780 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
782 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
783 tcg_gen_qemu_st_i64(tmp, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
784 s->be_data | MO_Q);
785 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_hi_offset(s, srcidx));
786 tcg_gen_qemu_st_i64(tmp, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
787 s->be_data | MO_Q);
788 tcg_temp_free_i64(tcg_hiaddr);
791 tcg_temp_free_i64(tmp);
795 * Load from memory to FP register
797 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
799 /* This always zero-extends and writes to a full 128 bit wide vector */
800 TCGv_i64 tmplo = tcg_temp_new_i64();
801 TCGv_i64 tmphi;
803 if (size < 4) {
804 TCGMemOp memop = s->be_data + size;
805 tmphi = tcg_const_i64(0);
806 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
807 } else {
808 bool be = s->be_data == MO_BE;
809 TCGv_i64 tcg_hiaddr;
811 tmphi = tcg_temp_new_i64();
812 tcg_hiaddr = tcg_temp_new_i64();
814 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
815 tcg_gen_qemu_ld_i64(tmplo, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
816 s->be_data | MO_Q);
817 tcg_gen_qemu_ld_i64(tmphi, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
818 s->be_data | MO_Q);
819 tcg_temp_free_i64(tcg_hiaddr);
822 tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
823 tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
825 tcg_temp_free_i64(tmplo);
826 tcg_temp_free_i64(tmphi);
830 * Vector load/store helpers.
832 * The principal difference between this and a FP load is that we don't
833 * zero extend as we are filling a partial chunk of the vector register.
834 * These functions don't support 128 bit loads/stores, which would be
835 * normal load/store operations.
837 * The _i32 versions are useful when operating on 32 bit quantities
838 * (eg for floating point single or using Neon helper functions).
841 /* Get value of an element within a vector register */
842 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
843 int element, TCGMemOp memop)
845 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
846 switch (memop) {
847 case MO_8:
848 tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
849 break;
850 case MO_16:
851 tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
852 break;
853 case MO_32:
854 tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
855 break;
856 case MO_8|MO_SIGN:
857 tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
858 break;
859 case MO_16|MO_SIGN:
860 tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
861 break;
862 case MO_32|MO_SIGN:
863 tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
864 break;
865 case MO_64:
866 case MO_64|MO_SIGN:
867 tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
868 break;
869 default:
870 g_assert_not_reached();
874 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
875 int element, TCGMemOp memop)
877 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
878 switch (memop) {
879 case MO_8:
880 tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
881 break;
882 case MO_16:
883 tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
884 break;
885 case MO_8|MO_SIGN:
886 tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
887 break;
888 case MO_16|MO_SIGN:
889 tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
890 break;
891 case MO_32:
892 case MO_32|MO_SIGN:
893 tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
894 break;
895 default:
896 g_assert_not_reached();
900 /* Set value of an element within a vector register */
901 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
902 int element, TCGMemOp memop)
904 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
905 switch (memop) {
906 case MO_8:
907 tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
908 break;
909 case MO_16:
910 tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
911 break;
912 case MO_32:
913 tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
914 break;
915 case MO_64:
916 tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
917 break;
918 default:
919 g_assert_not_reached();
923 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
924 int destidx, int element, TCGMemOp memop)
926 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
927 switch (memop) {
928 case MO_8:
929 tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
930 break;
931 case MO_16:
932 tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
933 break;
934 case MO_32:
935 tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
936 break;
937 default:
938 g_assert_not_reached();
942 /* Clear the high 64 bits of a 128 bit vector (in general non-quad
943 * vector ops all need to do this).
945 static void clear_vec_high(DisasContext *s, int rd)
947 TCGv_i64 tcg_zero = tcg_const_i64(0);
949 write_vec_element(s, tcg_zero, rd, 1, MO_64);
950 tcg_temp_free_i64(tcg_zero);
953 /* Store from vector register to memory */
954 static void do_vec_st(DisasContext *s, int srcidx, int element,
955 TCGv_i64 tcg_addr, int size)
957 TCGMemOp memop = s->be_data + size;
958 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
960 read_vec_element(s, tcg_tmp, srcidx, element, size);
961 tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
963 tcg_temp_free_i64(tcg_tmp);
966 /* Load from memory to vector register */
967 static void do_vec_ld(DisasContext *s, int destidx, int element,
968 TCGv_i64 tcg_addr, int size)
970 TCGMemOp memop = s->be_data + size;
971 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
973 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
974 write_vec_element(s, tcg_tmp, destidx, element, size);
976 tcg_temp_free_i64(tcg_tmp);
979 /* Check that FP/Neon access is enabled. If it is, return
980 * true. If not, emit code to generate an appropriate exception,
981 * and return false; the caller should not emit any code for
982 * the instruction. Note that this check must happen after all
983 * unallocated-encoding checks (otherwise the syndrome information
984 * for the resulting exception will be incorrect).
986 static inline bool fp_access_check(DisasContext *s)
988 assert(!s->fp_access_checked);
989 s->fp_access_checked = true;
991 if (!s->fp_excp_el) {
992 return true;
995 gen_exception_insn(s, 4, EXCP_UDEF, syn_fp_access_trap(1, 0xe, false),
996 s->fp_excp_el);
997 return false;
1001 * This utility function is for doing register extension with an
1002 * optional shift. You will likely want to pass a temporary for the
1003 * destination register. See DecodeRegExtend() in the ARM ARM.
1005 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1006 int option, unsigned int shift)
1008 int extsize = extract32(option, 0, 2);
1009 bool is_signed = extract32(option, 2, 1);
1011 if (is_signed) {
1012 switch (extsize) {
1013 case 0:
1014 tcg_gen_ext8s_i64(tcg_out, tcg_in);
1015 break;
1016 case 1:
1017 tcg_gen_ext16s_i64(tcg_out, tcg_in);
1018 break;
1019 case 2:
1020 tcg_gen_ext32s_i64(tcg_out, tcg_in);
1021 break;
1022 case 3:
1023 tcg_gen_mov_i64(tcg_out, tcg_in);
1024 break;
1026 } else {
1027 switch (extsize) {
1028 case 0:
1029 tcg_gen_ext8u_i64(tcg_out, tcg_in);
1030 break;
1031 case 1:
1032 tcg_gen_ext16u_i64(tcg_out, tcg_in);
1033 break;
1034 case 2:
1035 tcg_gen_ext32u_i64(tcg_out, tcg_in);
1036 break;
1037 case 3:
1038 tcg_gen_mov_i64(tcg_out, tcg_in);
1039 break;
1043 if (shift) {
1044 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1048 static inline void gen_check_sp_alignment(DisasContext *s)
1050 /* The AArch64 architecture mandates that (if enabled via PSTATE
1051 * or SCTLR bits) there is a check that SP is 16-aligned on every
1052 * SP-relative load or store (with an exception generated if it is not).
1053 * In line with general QEMU practice regarding misaligned accesses,
1054 * we omit these checks for the sake of guest program performance.
1055 * This function is provided as a hook so we can more easily add these
1056 * checks in future (possibly as a "favour catching guest program bugs
1057 * over speed" user selectable option).
1062 * This provides a simple table based table lookup decoder. It is
1063 * intended to be used when the relevant bits for decode are too
1064 * awkwardly placed and switch/if based logic would be confusing and
1065 * deeply nested. Since it's a linear search through the table, tables
1066 * should be kept small.
1068 * It returns the first handler where insn & mask == pattern, or
1069 * NULL if there is no match.
1070 * The table is terminated by an empty mask (i.e. 0)
1072 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1073 uint32_t insn)
1075 const AArch64DecodeTable *tptr = table;
1077 while (tptr->mask) {
1078 if ((insn & tptr->mask) == tptr->pattern) {
1079 return tptr->disas_fn;
1081 tptr++;
1083 return NULL;
1087 * the instruction disassembly implemented here matches
1088 * the instruction encoding classifications in chapter 3 (C3)
1089 * of the ARM Architecture Reference Manual (DDI0487A_a)
1092 /* C3.2.7 Unconditional branch (immediate)
1093 * 31 30 26 25 0
1094 * +----+-----------+-------------------------------------+
1095 * | op | 0 0 1 0 1 | imm26 |
1096 * +----+-----------+-------------------------------------+
1098 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
1100 uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
1102 if (insn & (1U << 31)) {
1103 /* C5.6.26 BL Branch with link */
1104 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1107 /* C5.6.20 B Branch / C5.6.26 BL Branch with link */
1108 gen_goto_tb(s, 0, addr);
1111 /* C3.2.1 Compare & branch (immediate)
1112 * 31 30 25 24 23 5 4 0
1113 * +----+-------------+----+---------------------+--------+
1114 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
1115 * +----+-------------+----+---------------------+--------+
1117 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
1119 unsigned int sf, op, rt;
1120 uint64_t addr;
1121 TCGLabel *label_match;
1122 TCGv_i64 tcg_cmp;
1124 sf = extract32(insn, 31, 1);
1125 op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
1126 rt = extract32(insn, 0, 5);
1127 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1129 tcg_cmp = read_cpu_reg(s, rt, sf);
1130 label_match = gen_new_label();
1132 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1133 tcg_cmp, 0, label_match);
1135 gen_goto_tb(s, 0, s->pc);
1136 gen_set_label(label_match);
1137 gen_goto_tb(s, 1, addr);
1140 /* C3.2.5 Test & branch (immediate)
1141 * 31 30 25 24 23 19 18 5 4 0
1142 * +----+-------------+----+-------+-------------+------+
1143 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
1144 * +----+-------------+----+-------+-------------+------+
1146 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1148 unsigned int bit_pos, op, rt;
1149 uint64_t addr;
1150 TCGLabel *label_match;
1151 TCGv_i64 tcg_cmp;
1153 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1154 op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1155 addr = s->pc + sextract32(insn, 5, 14) * 4 - 4;
1156 rt = extract32(insn, 0, 5);
1158 tcg_cmp = tcg_temp_new_i64();
1159 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1160 label_match = gen_new_label();
1161 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1162 tcg_cmp, 0, label_match);
1163 tcg_temp_free_i64(tcg_cmp);
1164 gen_goto_tb(s, 0, s->pc);
1165 gen_set_label(label_match);
1166 gen_goto_tb(s, 1, addr);
1169 /* C3.2.2 / C5.6.19 Conditional branch (immediate)
1170 * 31 25 24 23 5 4 3 0
1171 * +---------------+----+---------------------+----+------+
1172 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
1173 * +---------------+----+---------------------+----+------+
1175 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1177 unsigned int cond;
1178 uint64_t addr;
1180 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1181 unallocated_encoding(s);
1182 return;
1184 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1185 cond = extract32(insn, 0, 4);
1187 if (cond < 0x0e) {
1188 /* genuinely conditional branches */
1189 TCGLabel *label_match = gen_new_label();
1190 arm_gen_test_cc(cond, label_match);
1191 gen_goto_tb(s, 0, s->pc);
1192 gen_set_label(label_match);
1193 gen_goto_tb(s, 1, addr);
1194 } else {
1195 /* 0xe and 0xf are both "always" conditions */
1196 gen_goto_tb(s, 0, addr);
1200 /* C5.6.68 HINT */
1201 static void handle_hint(DisasContext *s, uint32_t insn,
1202 unsigned int op1, unsigned int op2, unsigned int crm)
1204 unsigned int selector = crm << 3 | op2;
1206 if (op1 != 3) {
1207 unallocated_encoding(s);
1208 return;
1211 switch (selector) {
1212 case 0: /* NOP */
1213 return;
1214 case 3: /* WFI */
1215 s->is_jmp = DISAS_WFI;
1216 return;
1217 case 1: /* YIELD */
1218 s->is_jmp = DISAS_YIELD;
1219 return;
1220 case 2: /* WFE */
1221 s->is_jmp = DISAS_WFE;
1222 return;
1223 case 4: /* SEV */
1224 case 5: /* SEVL */
1225 /* we treat all as NOP at least for now */
1226 return;
1227 default:
1228 /* default specified as NOP equivalent */
1229 return;
1233 static void gen_clrex(DisasContext *s, uint32_t insn)
1235 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1238 /* CLREX, DSB, DMB, ISB */
1239 static void handle_sync(DisasContext *s, uint32_t insn,
1240 unsigned int op1, unsigned int op2, unsigned int crm)
1242 if (op1 != 3) {
1243 unallocated_encoding(s);
1244 return;
1247 switch (op2) {
1248 case 2: /* CLREX */
1249 gen_clrex(s, insn);
1250 return;
1251 case 4: /* DSB */
1252 case 5: /* DMB */
1253 /* We don't emulate caches so barriers are no-ops */
1254 return;
1255 case 6: /* ISB */
1256 /* We need to break the TB after this insn to execute
1257 * a self-modified code correctly and also to take
1258 * any pending interrupts immediately.
1260 s->is_jmp = DISAS_UPDATE;
1261 return;
1262 default:
1263 unallocated_encoding(s);
1264 return;
1268 /* C5.6.130 MSR (immediate) - move immediate to processor state field */
1269 static void handle_msr_i(DisasContext *s, uint32_t insn,
1270 unsigned int op1, unsigned int op2, unsigned int crm)
1272 int op = op1 << 3 | op2;
1273 switch (op) {
1274 case 0x05: /* SPSel */
1275 if (s->current_el == 0) {
1276 unallocated_encoding(s);
1277 return;
1279 /* fall through */
1280 case 0x1e: /* DAIFSet */
1281 case 0x1f: /* DAIFClear */
1283 TCGv_i32 tcg_imm = tcg_const_i32(crm);
1284 TCGv_i32 tcg_op = tcg_const_i32(op);
1285 gen_a64_set_pc_im(s->pc - 4);
1286 gen_helper_msr_i_pstate(cpu_env, tcg_op, tcg_imm);
1287 tcg_temp_free_i32(tcg_imm);
1288 tcg_temp_free_i32(tcg_op);
1289 s->is_jmp = DISAS_UPDATE;
1290 break;
1292 default:
1293 unallocated_encoding(s);
1294 return;
1298 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1300 TCGv_i32 tmp = tcg_temp_new_i32();
1301 TCGv_i32 nzcv = tcg_temp_new_i32();
1303 /* build bit 31, N */
1304 tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
1305 /* build bit 30, Z */
1306 tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1307 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1308 /* build bit 29, C */
1309 tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1310 /* build bit 28, V */
1311 tcg_gen_shri_i32(tmp, cpu_VF, 31);
1312 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1313 /* generate result */
1314 tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1316 tcg_temp_free_i32(nzcv);
1317 tcg_temp_free_i32(tmp);
1320 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1323 TCGv_i32 nzcv = tcg_temp_new_i32();
1325 /* take NZCV from R[t] */
1326 tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
1328 /* bit 31, N */
1329 tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
1330 /* bit 30, Z */
1331 tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1332 tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1333 /* bit 29, C */
1334 tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1335 tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1336 /* bit 28, V */
1337 tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1338 tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1339 tcg_temp_free_i32(nzcv);
1342 /* C5.6.129 MRS - move from system register
1343 * C5.6.131 MSR (register) - move to system register
1344 * C5.6.204 SYS
1345 * C5.6.205 SYSL
1346 * These are all essentially the same insn in 'read' and 'write'
1347 * versions, with varying op0 fields.
1349 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1350 unsigned int op0, unsigned int op1, unsigned int op2,
1351 unsigned int crn, unsigned int crm, unsigned int rt)
1353 const ARMCPRegInfo *ri;
1354 TCGv_i64 tcg_rt;
1356 ri = get_arm_cp_reginfo(s->cp_regs,
1357 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1358 crn, crm, op0, op1, op2));
1360 if (!ri) {
1361 /* Unknown register; this might be a guest error or a QEMU
1362 * unimplemented feature.
1364 qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1365 "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1366 isread ? "read" : "write", op0, op1, crn, crm, op2);
1367 unallocated_encoding(s);
1368 return;
1371 /* Check access permissions */
1372 if (!cp_access_ok(s->current_el, ri, isread)) {
1373 unallocated_encoding(s);
1374 return;
1377 if (ri->accessfn) {
1378 /* Emit code to perform further access permissions checks at
1379 * runtime; this may result in an exception.
1381 TCGv_ptr tmpptr;
1382 TCGv_i32 tcg_syn, tcg_isread;
1383 uint32_t syndrome;
1385 gen_a64_set_pc_im(s->pc - 4);
1386 tmpptr = tcg_const_ptr(ri);
1387 syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1388 tcg_syn = tcg_const_i32(syndrome);
1389 tcg_isread = tcg_const_i32(isread);
1390 gen_helper_access_check_cp_reg(cpu_env, tmpptr, tcg_syn, tcg_isread);
1391 tcg_temp_free_ptr(tmpptr);
1392 tcg_temp_free_i32(tcg_syn);
1393 tcg_temp_free_i32(tcg_isread);
1396 /* Handle special cases first */
1397 switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
1398 case ARM_CP_NOP:
1399 return;
1400 case ARM_CP_NZCV:
1401 tcg_rt = cpu_reg(s, rt);
1402 if (isread) {
1403 gen_get_nzcv(tcg_rt);
1404 } else {
1405 gen_set_nzcv(tcg_rt);
1407 return;
1408 case ARM_CP_CURRENTEL:
1409 /* Reads as current EL value from pstate, which is
1410 * guaranteed to be constant by the tb flags.
1412 tcg_rt = cpu_reg(s, rt);
1413 tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
1414 return;
1415 case ARM_CP_DC_ZVA:
1416 /* Writes clear the aligned block of memory which rt points into. */
1417 tcg_rt = cpu_reg(s, rt);
1418 gen_helper_dc_zva(cpu_env, tcg_rt);
1419 return;
1420 default:
1421 break;
1424 if ((s->tb->cflags & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1425 gen_io_start();
1428 tcg_rt = cpu_reg(s, rt);
1430 if (isread) {
1431 if (ri->type & ARM_CP_CONST) {
1432 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1433 } else if (ri->readfn) {
1434 TCGv_ptr tmpptr;
1435 tmpptr = tcg_const_ptr(ri);
1436 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1437 tcg_temp_free_ptr(tmpptr);
1438 } else {
1439 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1441 } else {
1442 if (ri->type & ARM_CP_CONST) {
1443 /* If not forbidden by access permissions, treat as WI */
1444 return;
1445 } else if (ri->writefn) {
1446 TCGv_ptr tmpptr;
1447 tmpptr = tcg_const_ptr(ri);
1448 gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1449 tcg_temp_free_ptr(tmpptr);
1450 } else {
1451 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
1455 if ((s->tb->cflags & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1456 /* I/O operations must end the TB here (whether read or write) */
1457 gen_io_end();
1458 s->is_jmp = DISAS_UPDATE;
1459 } else if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
1460 /* We default to ending the TB on a coprocessor register write,
1461 * but allow this to be suppressed by the register definition
1462 * (usually only necessary to work around guest bugs).
1464 s->is_jmp = DISAS_UPDATE;
1468 /* C3.2.4 System
1469 * 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0
1470 * +---------------------+---+-----+-----+-------+-------+-----+------+
1471 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 | CRn | CRm | op2 | Rt |
1472 * +---------------------+---+-----+-----+-------+-------+-----+------+
1474 static void disas_system(DisasContext *s, uint32_t insn)
1476 unsigned int l, op0, op1, crn, crm, op2, rt;
1477 l = extract32(insn, 21, 1);
1478 op0 = extract32(insn, 19, 2);
1479 op1 = extract32(insn, 16, 3);
1480 crn = extract32(insn, 12, 4);
1481 crm = extract32(insn, 8, 4);
1482 op2 = extract32(insn, 5, 3);
1483 rt = extract32(insn, 0, 5);
1485 if (op0 == 0) {
1486 if (l || rt != 31) {
1487 unallocated_encoding(s);
1488 return;
1490 switch (crn) {
1491 case 2: /* C5.6.68 HINT */
1492 handle_hint(s, insn, op1, op2, crm);
1493 break;
1494 case 3: /* CLREX, DSB, DMB, ISB */
1495 handle_sync(s, insn, op1, op2, crm);
1496 break;
1497 case 4: /* C5.6.130 MSR (immediate) */
1498 handle_msr_i(s, insn, op1, op2, crm);
1499 break;
1500 default:
1501 unallocated_encoding(s);
1502 break;
1504 return;
1506 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
1509 /* C3.2.3 Exception generation
1511 * 31 24 23 21 20 5 4 2 1 0
1512 * +-----------------+-----+------------------------+-----+----+
1513 * | 1 1 0 1 0 1 0 0 | opc | imm16 | op2 | LL |
1514 * +-----------------------+------------------------+----------+
1516 static void disas_exc(DisasContext *s, uint32_t insn)
1518 int opc = extract32(insn, 21, 3);
1519 int op2_ll = extract32(insn, 0, 5);
1520 int imm16 = extract32(insn, 5, 16);
1521 TCGv_i32 tmp;
1523 switch (opc) {
1524 case 0:
1525 /* For SVC, HVC and SMC we advance the single-step state
1526 * machine before taking the exception. This is architecturally
1527 * mandated, to ensure that single-stepping a system call
1528 * instruction works properly.
1530 switch (op2_ll) {
1531 case 1:
1532 gen_ss_advance(s);
1533 gen_exception_insn(s, 0, EXCP_SWI, syn_aa64_svc(imm16),
1534 default_exception_el(s));
1535 break;
1536 case 2:
1537 if (s->current_el == 0) {
1538 unallocated_encoding(s);
1539 break;
1541 /* The pre HVC helper handles cases when HVC gets trapped
1542 * as an undefined insn by runtime configuration.
1544 gen_a64_set_pc_im(s->pc - 4);
1545 gen_helper_pre_hvc(cpu_env);
1546 gen_ss_advance(s);
1547 gen_exception_insn(s, 0, EXCP_HVC, syn_aa64_hvc(imm16), 2);
1548 break;
1549 case 3:
1550 if (s->current_el == 0) {
1551 unallocated_encoding(s);
1552 break;
1554 gen_a64_set_pc_im(s->pc - 4);
1555 tmp = tcg_const_i32(syn_aa64_smc(imm16));
1556 gen_helper_pre_smc(cpu_env, tmp);
1557 tcg_temp_free_i32(tmp);
1558 gen_ss_advance(s);
1559 gen_exception_insn(s, 0, EXCP_SMC, syn_aa64_smc(imm16), 3);
1560 break;
1561 default:
1562 unallocated_encoding(s);
1563 break;
1565 break;
1566 case 1:
1567 if (op2_ll != 0) {
1568 unallocated_encoding(s);
1569 break;
1571 /* BRK */
1572 gen_exception_insn(s, 4, EXCP_BKPT, syn_aa64_bkpt(imm16),
1573 default_exception_el(s));
1574 break;
1575 case 2:
1576 if (op2_ll != 0) {
1577 unallocated_encoding(s);
1578 break;
1580 /* HLT. This has two purposes.
1581 * Architecturally, it is an external halting debug instruction.
1582 * Since QEMU doesn't implement external debug, we treat this as
1583 * it is required for halting debug disabled: it will UNDEF.
1584 * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
1586 if (semihosting_enabled() && imm16 == 0xf000) {
1587 #ifndef CONFIG_USER_ONLY
1588 /* In system mode, don't allow userspace access to semihosting,
1589 * to provide some semblance of security (and for consistency
1590 * with our 32-bit semihosting).
1592 if (s->current_el == 0) {
1593 unsupported_encoding(s, insn);
1594 break;
1596 #endif
1597 gen_exception_internal_insn(s, 0, EXCP_SEMIHOST);
1598 } else {
1599 unsupported_encoding(s, insn);
1601 break;
1602 case 5:
1603 if (op2_ll < 1 || op2_ll > 3) {
1604 unallocated_encoding(s);
1605 break;
1607 /* DCPS1, DCPS2, DCPS3 */
1608 unsupported_encoding(s, insn);
1609 break;
1610 default:
1611 unallocated_encoding(s);
1612 break;
1616 /* C3.2.7 Unconditional branch (register)
1617 * 31 25 24 21 20 16 15 10 9 5 4 0
1618 * +---------------+-------+-------+-------+------+-------+
1619 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
1620 * +---------------+-------+-------+-------+------+-------+
1622 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
1624 unsigned int opc, op2, op3, rn, op4;
1626 opc = extract32(insn, 21, 4);
1627 op2 = extract32(insn, 16, 5);
1628 op3 = extract32(insn, 10, 6);
1629 rn = extract32(insn, 5, 5);
1630 op4 = extract32(insn, 0, 5);
1632 if (op4 != 0x0 || op3 != 0x0 || op2 != 0x1f) {
1633 unallocated_encoding(s);
1634 return;
1637 switch (opc) {
1638 case 0: /* BR */
1639 case 2: /* RET */
1640 tcg_gen_mov_i64(cpu_pc, cpu_reg(s, rn));
1641 break;
1642 case 1: /* BLR */
1643 tcg_gen_mov_i64(cpu_pc, cpu_reg(s, rn));
1644 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1645 break;
1646 case 4: /* ERET */
1647 if (s->current_el == 0) {
1648 unallocated_encoding(s);
1649 return;
1651 gen_helper_exception_return(cpu_env);
1652 s->is_jmp = DISAS_JUMP;
1653 return;
1654 case 5: /* DRPS */
1655 if (rn != 0x1f) {
1656 unallocated_encoding(s);
1657 } else {
1658 unsupported_encoding(s, insn);
1660 return;
1661 default:
1662 unallocated_encoding(s);
1663 return;
1666 s->is_jmp = DISAS_JUMP;
1669 /* C3.2 Branches, exception generating and system instructions */
1670 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
1672 switch (extract32(insn, 25, 7)) {
1673 case 0x0a: case 0x0b:
1674 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
1675 disas_uncond_b_imm(s, insn);
1676 break;
1677 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
1678 disas_comp_b_imm(s, insn);
1679 break;
1680 case 0x1b: case 0x5b: /* Test & branch (immediate) */
1681 disas_test_b_imm(s, insn);
1682 break;
1683 case 0x2a: /* Conditional branch (immediate) */
1684 disas_cond_b_imm(s, insn);
1685 break;
1686 case 0x6a: /* Exception generation / System */
1687 if (insn & (1 << 24)) {
1688 disas_system(s, insn);
1689 } else {
1690 disas_exc(s, insn);
1692 break;
1693 case 0x6b: /* Unconditional branch (register) */
1694 disas_uncond_b_reg(s, insn);
1695 break;
1696 default:
1697 unallocated_encoding(s);
1698 break;
1703 * Load/Store exclusive instructions are implemented by remembering
1704 * the value/address loaded, and seeing if these are the same
1705 * when the store is performed. This is not actually the architecturally
1706 * mandated semantics, but it works for typical guest code sequences
1707 * and avoids having to monitor regular stores.
1709 * In system emulation mode only one CPU will be running at once, so
1710 * this sequence is effectively atomic. In user emulation mode we
1711 * throw an exception and handle the atomic operation elsewhere.
1713 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
1714 TCGv_i64 addr, int size, bool is_pair)
1716 TCGv_i64 tmp = tcg_temp_new_i64();
1717 TCGMemOp memop = s->be_data + size;
1719 g_assert(size <= 3);
1720 tcg_gen_qemu_ld_i64(tmp, addr, get_mem_index(s), memop);
1722 if (is_pair) {
1723 TCGv_i64 addr2 = tcg_temp_new_i64();
1724 TCGv_i64 hitmp = tcg_temp_new_i64();
1726 g_assert(size >= 2);
1727 tcg_gen_addi_i64(addr2, addr, 1 << size);
1728 tcg_gen_qemu_ld_i64(hitmp, addr2, get_mem_index(s), memop);
1729 tcg_temp_free_i64(addr2);
1730 tcg_gen_mov_i64(cpu_exclusive_high, hitmp);
1731 tcg_gen_mov_i64(cpu_reg(s, rt2), hitmp);
1732 tcg_temp_free_i64(hitmp);
1735 tcg_gen_mov_i64(cpu_exclusive_val, tmp);
1736 tcg_gen_mov_i64(cpu_reg(s, rt), tmp);
1738 tcg_temp_free_i64(tmp);
1739 tcg_gen_mov_i64(cpu_exclusive_addr, addr);
1742 #ifdef CONFIG_USER_ONLY
1743 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
1744 TCGv_i64 addr, int size, int is_pair)
1746 tcg_gen_mov_i64(cpu_exclusive_test, addr);
1747 tcg_gen_movi_i32(cpu_exclusive_info,
1748 size | is_pair << 2 | (rd << 4) | (rt << 9) | (rt2 << 14));
1749 gen_exception_internal_insn(s, 4, EXCP_STREX);
1751 #else
1752 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
1753 TCGv_i64 inaddr, int size, int is_pair)
1755 /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
1756 * && (!is_pair || env->exclusive_high == [addr + datasize])) {
1757 * [addr] = {Rt};
1758 * if (is_pair) {
1759 * [addr + datasize] = {Rt2};
1761 * {Rd} = 0;
1762 * } else {
1763 * {Rd} = 1;
1765 * env->exclusive_addr = -1;
1767 TCGLabel *fail_label = gen_new_label();
1768 TCGLabel *done_label = gen_new_label();
1769 TCGv_i64 addr = tcg_temp_local_new_i64();
1770 TCGv_i64 tmp;
1772 /* Copy input into a local temp so it is not trashed when the
1773 * basic block ends at the branch insn.
1775 tcg_gen_mov_i64(addr, inaddr);
1776 tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
1778 tmp = tcg_temp_new_i64();
1779 tcg_gen_qemu_ld_i64(tmp, addr, get_mem_index(s), s->be_data + size);
1780 tcg_gen_brcond_i64(TCG_COND_NE, tmp, cpu_exclusive_val, fail_label);
1781 tcg_temp_free_i64(tmp);
1783 if (is_pair) {
1784 TCGv_i64 addrhi = tcg_temp_new_i64();
1785 TCGv_i64 tmphi = tcg_temp_new_i64();
1787 tcg_gen_addi_i64(addrhi, addr, 1 << size);
1788 tcg_gen_qemu_ld_i64(tmphi, addrhi, get_mem_index(s),
1789 s->be_data + size);
1790 tcg_gen_brcond_i64(TCG_COND_NE, tmphi, cpu_exclusive_high, fail_label);
1792 tcg_temp_free_i64(tmphi);
1793 tcg_temp_free_i64(addrhi);
1796 /* We seem to still have the exclusive monitor, so do the store */
1797 tcg_gen_qemu_st_i64(cpu_reg(s, rt), addr, get_mem_index(s),
1798 s->be_data + size);
1799 if (is_pair) {
1800 TCGv_i64 addrhi = tcg_temp_new_i64();
1802 tcg_gen_addi_i64(addrhi, addr, 1 << size);
1803 tcg_gen_qemu_st_i64(cpu_reg(s, rt2), addrhi,
1804 get_mem_index(s), s->be_data + size);
1805 tcg_temp_free_i64(addrhi);
1808 tcg_temp_free_i64(addr);
1810 tcg_gen_movi_i64(cpu_reg(s, rd), 0);
1811 tcg_gen_br(done_label);
1812 gen_set_label(fail_label);
1813 tcg_gen_movi_i64(cpu_reg(s, rd), 1);
1814 gen_set_label(done_label);
1815 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1818 #endif
1820 /* C3.3.6 Load/store exclusive
1822 * 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0
1823 * +-----+-------------+----+---+----+------+----+-------+------+------+
1824 * | sz | 0 0 1 0 0 0 | o2 | L | o1 | Rs | o0 | Rt2 | Rn | Rt |
1825 * +-----+-------------+----+---+----+------+----+-------+------+------+
1827 * sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
1828 * L: 0 -> store, 1 -> load
1829 * o2: 0 -> exclusive, 1 -> not
1830 * o1: 0 -> single register, 1 -> register pair
1831 * o0: 1 -> load-acquire/store-release, 0 -> not
1833 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
1835 int rt = extract32(insn, 0, 5);
1836 int rn = extract32(insn, 5, 5);
1837 int rt2 = extract32(insn, 10, 5);
1838 int is_lasr = extract32(insn, 15, 1);
1839 int rs = extract32(insn, 16, 5);
1840 int is_pair = extract32(insn, 21, 1);
1841 int is_store = !extract32(insn, 22, 1);
1842 int is_excl = !extract32(insn, 23, 1);
1843 int size = extract32(insn, 30, 2);
1844 TCGv_i64 tcg_addr;
1846 if ((!is_excl && !is_pair && !is_lasr) ||
1847 (!is_excl && is_pair) ||
1848 (is_pair && size < 2)) {
1849 unallocated_encoding(s);
1850 return;
1853 if (rn == 31) {
1854 gen_check_sp_alignment(s);
1856 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1858 /* Note that since TCG is single threaded load-acquire/store-release
1859 * semantics require no extra if (is_lasr) { ... } handling.
1862 if (is_excl) {
1863 if (!is_store) {
1864 s->is_ldex = true;
1865 gen_load_exclusive(s, rt, rt2, tcg_addr, size, is_pair);
1866 } else {
1867 gen_store_exclusive(s, rs, rt, rt2, tcg_addr, size, is_pair);
1869 } else {
1870 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1871 if (is_store) {
1872 do_gpr_st(s, tcg_rt, tcg_addr, size);
1873 } else {
1874 do_gpr_ld(s, tcg_rt, tcg_addr, size, false, false);
1880 * C3.3.5 Load register (literal)
1882 * 31 30 29 27 26 25 24 23 5 4 0
1883 * +-----+-------+---+-----+-------------------+-------+
1884 * | opc | 0 1 1 | V | 0 0 | imm19 | Rt |
1885 * +-----+-------+---+-----+-------------------+-------+
1887 * V: 1 -> vector (simd/fp)
1888 * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
1889 * 10-> 32 bit signed, 11 -> prefetch
1890 * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
1892 static void disas_ld_lit(DisasContext *s, uint32_t insn)
1894 int rt = extract32(insn, 0, 5);
1895 int64_t imm = sextract32(insn, 5, 19) << 2;
1896 bool is_vector = extract32(insn, 26, 1);
1897 int opc = extract32(insn, 30, 2);
1898 bool is_signed = false;
1899 int size = 2;
1900 TCGv_i64 tcg_rt, tcg_addr;
1902 if (is_vector) {
1903 if (opc == 3) {
1904 unallocated_encoding(s);
1905 return;
1907 size = 2 + opc;
1908 if (!fp_access_check(s)) {
1909 return;
1911 } else {
1912 if (opc == 3) {
1913 /* PRFM (literal) : prefetch */
1914 return;
1916 size = 2 + extract32(opc, 0, 1);
1917 is_signed = extract32(opc, 1, 1);
1920 tcg_rt = cpu_reg(s, rt);
1922 tcg_addr = tcg_const_i64((s->pc - 4) + imm);
1923 if (is_vector) {
1924 do_fp_ld(s, rt, tcg_addr, size);
1925 } else {
1926 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false);
1928 tcg_temp_free_i64(tcg_addr);
1932 * C5.6.80 LDNP (Load Pair - non-temporal hint)
1933 * C5.6.81 LDP (Load Pair - non vector)
1934 * C5.6.82 LDPSW (Load Pair Signed Word - non vector)
1935 * C5.6.176 STNP (Store Pair - non-temporal hint)
1936 * C5.6.177 STP (Store Pair - non vector)
1937 * C6.3.165 LDNP (Load Pair of SIMD&FP - non-temporal hint)
1938 * C6.3.165 LDP (Load Pair of SIMD&FP)
1939 * C6.3.284 STNP (Store Pair of SIMD&FP - non-temporal hint)
1940 * C6.3.284 STP (Store Pair of SIMD&FP)
1942 * 31 30 29 27 26 25 24 23 22 21 15 14 10 9 5 4 0
1943 * +-----+-------+---+---+-------+---+-----------------------------+
1944 * | opc | 1 0 1 | V | 0 | index | L | imm7 | Rt2 | Rn | Rt |
1945 * +-----+-------+---+---+-------+---+-------+-------+------+------+
1947 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
1948 * LDPSW 01
1949 * LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
1950 * V: 0 -> GPR, 1 -> Vector
1951 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
1952 * 10 -> signed offset, 11 -> pre-index
1953 * L: 0 -> Store 1 -> Load
1955 * Rt, Rt2 = GPR or SIMD registers to be stored
1956 * Rn = general purpose register containing address
1957 * imm7 = signed offset (multiple of 4 or 8 depending on size)
1959 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
1961 int rt = extract32(insn, 0, 5);
1962 int rn = extract32(insn, 5, 5);
1963 int rt2 = extract32(insn, 10, 5);
1964 uint64_t offset = sextract64(insn, 15, 7);
1965 int index = extract32(insn, 23, 2);
1966 bool is_vector = extract32(insn, 26, 1);
1967 bool is_load = extract32(insn, 22, 1);
1968 int opc = extract32(insn, 30, 2);
1970 bool is_signed = false;
1971 bool postindex = false;
1972 bool wback = false;
1974 TCGv_i64 tcg_addr; /* calculated address */
1975 int size;
1977 if (opc == 3) {
1978 unallocated_encoding(s);
1979 return;
1982 if (is_vector) {
1983 size = 2 + opc;
1984 } else {
1985 size = 2 + extract32(opc, 1, 1);
1986 is_signed = extract32(opc, 0, 1);
1987 if (!is_load && is_signed) {
1988 unallocated_encoding(s);
1989 return;
1993 switch (index) {
1994 case 1: /* post-index */
1995 postindex = true;
1996 wback = true;
1997 break;
1998 case 0:
1999 /* signed offset with "non-temporal" hint. Since we don't emulate
2000 * caches we don't care about hints to the cache system about
2001 * data access patterns, and handle this identically to plain
2002 * signed offset.
2004 if (is_signed) {
2005 /* There is no non-temporal-hint version of LDPSW */
2006 unallocated_encoding(s);
2007 return;
2009 postindex = false;
2010 break;
2011 case 2: /* signed offset, rn not updated */
2012 postindex = false;
2013 break;
2014 case 3: /* pre-index */
2015 postindex = false;
2016 wback = true;
2017 break;
2020 if (is_vector && !fp_access_check(s)) {
2021 return;
2024 offset <<= size;
2026 if (rn == 31) {
2027 gen_check_sp_alignment(s);
2030 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2032 if (!postindex) {
2033 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
2036 if (is_vector) {
2037 if (is_load) {
2038 do_fp_ld(s, rt, tcg_addr, size);
2039 } else {
2040 do_fp_st(s, rt, tcg_addr, size);
2042 } else {
2043 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2044 if (is_load) {
2045 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false);
2046 } else {
2047 do_gpr_st(s, tcg_rt, tcg_addr, size);
2050 tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
2051 if (is_vector) {
2052 if (is_load) {
2053 do_fp_ld(s, rt2, tcg_addr, size);
2054 } else {
2055 do_fp_st(s, rt2, tcg_addr, size);
2057 } else {
2058 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
2059 if (is_load) {
2060 do_gpr_ld(s, tcg_rt2, tcg_addr, size, is_signed, false);
2061 } else {
2062 do_gpr_st(s, tcg_rt2, tcg_addr, size);
2066 if (wback) {
2067 if (postindex) {
2068 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset - (1 << size));
2069 } else {
2070 tcg_gen_subi_i64(tcg_addr, tcg_addr, 1 << size);
2072 tcg_gen_mov_i64(cpu_reg_sp(s, rn), tcg_addr);
2077 * C3.3.8 Load/store (immediate post-indexed)
2078 * C3.3.9 Load/store (immediate pre-indexed)
2079 * C3.3.12 Load/store (unscaled immediate)
2081 * 31 30 29 27 26 25 24 23 22 21 20 12 11 10 9 5 4 0
2082 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2083 * |size| 1 1 1 | V | 0 0 | opc | 0 | imm9 | idx | Rn | Rt |
2084 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2086 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
2087 10 -> unprivileged
2088 * V = 0 -> non-vector
2089 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
2090 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2092 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
2093 int opc,
2094 int size,
2095 int rt,
2096 bool is_vector)
2098 int rn = extract32(insn, 5, 5);
2099 int imm9 = sextract32(insn, 12, 9);
2100 int idx = extract32(insn, 10, 2);
2101 bool is_signed = false;
2102 bool is_store = false;
2103 bool is_extended = false;
2104 bool is_unpriv = (idx == 2);
2105 bool post_index;
2106 bool writeback;
2108 TCGv_i64 tcg_addr;
2110 if (is_vector) {
2111 size |= (opc & 2) << 1;
2112 if (size > 4 || is_unpriv) {
2113 unallocated_encoding(s);
2114 return;
2116 is_store = ((opc & 1) == 0);
2117 if (!fp_access_check(s)) {
2118 return;
2120 } else {
2121 if (size == 3 && opc == 2) {
2122 /* PRFM - prefetch */
2123 if (is_unpriv) {
2124 unallocated_encoding(s);
2125 return;
2127 return;
2129 if (opc == 3 && size > 1) {
2130 unallocated_encoding(s);
2131 return;
2133 is_store = (opc == 0);
2134 is_signed = extract32(opc, 1, 1);
2135 is_extended = (size < 3) && extract32(opc, 0, 1);
2138 switch (idx) {
2139 case 0:
2140 case 2:
2141 post_index = false;
2142 writeback = false;
2143 break;
2144 case 1:
2145 post_index = true;
2146 writeback = true;
2147 break;
2148 case 3:
2149 post_index = false;
2150 writeback = true;
2151 break;
2154 if (rn == 31) {
2155 gen_check_sp_alignment(s);
2157 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2159 if (!post_index) {
2160 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
2163 if (is_vector) {
2164 if (is_store) {
2165 do_fp_st(s, rt, tcg_addr, size);
2166 } else {
2167 do_fp_ld(s, rt, tcg_addr, size);
2169 } else {
2170 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2171 int memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
2173 if (is_store) {
2174 do_gpr_st_memidx(s, tcg_rt, tcg_addr, size, memidx);
2175 } else {
2176 do_gpr_ld_memidx(s, tcg_rt, tcg_addr, size,
2177 is_signed, is_extended, memidx);
2181 if (writeback) {
2182 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2183 if (post_index) {
2184 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
2186 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2191 * C3.3.10 Load/store (register offset)
2193 * 31 30 29 27 26 25 24 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2194 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2195 * |size| 1 1 1 | V | 0 0 | opc | 1 | Rm | opt | S| 1 0 | Rn | Rt |
2196 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2198 * For non-vector:
2199 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2200 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2201 * For vector:
2202 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2203 * opc<0>: 0 -> store, 1 -> load
2204 * V: 1 -> vector/simd
2205 * opt: extend encoding (see DecodeRegExtend)
2206 * S: if S=1 then scale (essentially index by sizeof(size))
2207 * Rt: register to transfer into/out of
2208 * Rn: address register or SP for base
2209 * Rm: offset register or ZR for offset
2211 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
2212 int opc,
2213 int size,
2214 int rt,
2215 bool is_vector)
2217 int rn = extract32(insn, 5, 5);
2218 int shift = extract32(insn, 12, 1);
2219 int rm = extract32(insn, 16, 5);
2220 int opt = extract32(insn, 13, 3);
2221 bool is_signed = false;
2222 bool is_store = false;
2223 bool is_extended = false;
2225 TCGv_i64 tcg_rm;
2226 TCGv_i64 tcg_addr;
2228 if (extract32(opt, 1, 1) == 0) {
2229 unallocated_encoding(s);
2230 return;
2233 if (is_vector) {
2234 size |= (opc & 2) << 1;
2235 if (size > 4) {
2236 unallocated_encoding(s);
2237 return;
2239 is_store = !extract32(opc, 0, 1);
2240 if (!fp_access_check(s)) {
2241 return;
2243 } else {
2244 if (size == 3 && opc == 2) {
2245 /* PRFM - prefetch */
2246 return;
2248 if (opc == 3 && size > 1) {
2249 unallocated_encoding(s);
2250 return;
2252 is_store = (opc == 0);
2253 is_signed = extract32(opc, 1, 1);
2254 is_extended = (size < 3) && extract32(opc, 0, 1);
2257 if (rn == 31) {
2258 gen_check_sp_alignment(s);
2260 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2262 tcg_rm = read_cpu_reg(s, rm, 1);
2263 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
2265 tcg_gen_add_i64(tcg_addr, tcg_addr, tcg_rm);
2267 if (is_vector) {
2268 if (is_store) {
2269 do_fp_st(s, rt, tcg_addr, size);
2270 } else {
2271 do_fp_ld(s, rt, tcg_addr, size);
2273 } else {
2274 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2275 if (is_store) {
2276 do_gpr_st(s, tcg_rt, tcg_addr, size);
2277 } else {
2278 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
2284 * C3.3.13 Load/store (unsigned immediate)
2286 * 31 30 29 27 26 25 24 23 22 21 10 9 5
2287 * +----+-------+---+-----+-----+------------+-------+------+
2288 * |size| 1 1 1 | V | 0 1 | opc | imm12 | Rn | Rt |
2289 * +----+-------+---+-----+-----+------------+-------+------+
2291 * For non-vector:
2292 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2293 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2294 * For vector:
2295 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2296 * opc<0>: 0 -> store, 1 -> load
2297 * Rn: base address register (inc SP)
2298 * Rt: target register
2300 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
2301 int opc,
2302 int size,
2303 int rt,
2304 bool is_vector)
2306 int rn = extract32(insn, 5, 5);
2307 unsigned int imm12 = extract32(insn, 10, 12);
2308 unsigned int offset;
2310 TCGv_i64 tcg_addr;
2312 bool is_store;
2313 bool is_signed = false;
2314 bool is_extended = false;
2316 if (is_vector) {
2317 size |= (opc & 2) << 1;
2318 if (size > 4) {
2319 unallocated_encoding(s);
2320 return;
2322 is_store = !extract32(opc, 0, 1);
2323 if (!fp_access_check(s)) {
2324 return;
2326 } else {
2327 if (size == 3 && opc == 2) {
2328 /* PRFM - prefetch */
2329 return;
2331 if (opc == 3 && size > 1) {
2332 unallocated_encoding(s);
2333 return;
2335 is_store = (opc == 0);
2336 is_signed = extract32(opc, 1, 1);
2337 is_extended = (size < 3) && extract32(opc, 0, 1);
2340 if (rn == 31) {
2341 gen_check_sp_alignment(s);
2343 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2344 offset = imm12 << size;
2345 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
2347 if (is_vector) {
2348 if (is_store) {
2349 do_fp_st(s, rt, tcg_addr, size);
2350 } else {
2351 do_fp_ld(s, rt, tcg_addr, size);
2353 } else {
2354 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2355 if (is_store) {
2356 do_gpr_st(s, tcg_rt, tcg_addr, size);
2357 } else {
2358 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
2363 /* Load/store register (all forms) */
2364 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
2366 int rt = extract32(insn, 0, 5);
2367 int opc = extract32(insn, 22, 2);
2368 bool is_vector = extract32(insn, 26, 1);
2369 int size = extract32(insn, 30, 2);
2371 switch (extract32(insn, 24, 2)) {
2372 case 0:
2373 if (extract32(insn, 21, 1) == 1 && extract32(insn, 10, 2) == 2) {
2374 disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
2375 } else {
2376 /* Load/store register (unscaled immediate)
2377 * Load/store immediate pre/post-indexed
2378 * Load/store register unprivileged
2380 disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
2382 break;
2383 case 1:
2384 disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
2385 break;
2386 default:
2387 unallocated_encoding(s);
2388 break;
2392 /* C3.3.1 AdvSIMD load/store multiple structures
2394 * 31 30 29 23 22 21 16 15 12 11 10 9 5 4 0
2395 * +---+---+---------------+---+-------------+--------+------+------+------+
2396 * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size | Rn | Rt |
2397 * +---+---+---------------+---+-------------+--------+------+------+------+
2399 * C3.3.2 AdvSIMD load/store multiple structures (post-indexed)
2401 * 31 30 29 23 22 21 20 16 15 12 11 10 9 5 4 0
2402 * +---+---+---------------+---+---+---------+--------+------+------+------+
2403 * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 | Rm | opcode | size | Rn | Rt |
2404 * +---+---+---------------+---+---+---------+--------+------+------+------+
2406 * Rt: first (or only) SIMD&FP register to be transferred
2407 * Rn: base address or SP
2408 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2410 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
2412 int rt = extract32(insn, 0, 5);
2413 int rn = extract32(insn, 5, 5);
2414 int size = extract32(insn, 10, 2);
2415 int opcode = extract32(insn, 12, 4);
2416 bool is_store = !extract32(insn, 22, 1);
2417 bool is_postidx = extract32(insn, 23, 1);
2418 bool is_q = extract32(insn, 30, 1);
2419 TCGv_i64 tcg_addr, tcg_rn;
2421 int ebytes = 1 << size;
2422 int elements = (is_q ? 128 : 64) / (8 << size);
2423 int rpt; /* num iterations */
2424 int selem; /* structure elements */
2425 int r;
2427 if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
2428 unallocated_encoding(s);
2429 return;
2432 /* From the shared decode logic */
2433 switch (opcode) {
2434 case 0x0:
2435 rpt = 1;
2436 selem = 4;
2437 break;
2438 case 0x2:
2439 rpt = 4;
2440 selem = 1;
2441 break;
2442 case 0x4:
2443 rpt = 1;
2444 selem = 3;
2445 break;
2446 case 0x6:
2447 rpt = 3;
2448 selem = 1;
2449 break;
2450 case 0x7:
2451 rpt = 1;
2452 selem = 1;
2453 break;
2454 case 0x8:
2455 rpt = 1;
2456 selem = 2;
2457 break;
2458 case 0xa:
2459 rpt = 2;
2460 selem = 1;
2461 break;
2462 default:
2463 unallocated_encoding(s);
2464 return;
2467 if (size == 3 && !is_q && selem != 1) {
2468 /* reserved */
2469 unallocated_encoding(s);
2470 return;
2473 if (!fp_access_check(s)) {
2474 return;
2477 if (rn == 31) {
2478 gen_check_sp_alignment(s);
2481 tcg_rn = cpu_reg_sp(s, rn);
2482 tcg_addr = tcg_temp_new_i64();
2483 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2485 for (r = 0; r < rpt; r++) {
2486 int e;
2487 for (e = 0; e < elements; e++) {
2488 int tt = (rt + r) % 32;
2489 int xs;
2490 for (xs = 0; xs < selem; xs++) {
2491 if (is_store) {
2492 do_vec_st(s, tt, e, tcg_addr, size);
2493 } else {
2494 do_vec_ld(s, tt, e, tcg_addr, size);
2496 /* For non-quad operations, setting a slice of the low
2497 * 64 bits of the register clears the high 64 bits (in
2498 * the ARM ARM pseudocode this is implicit in the fact
2499 * that 'rval' is a 64 bit wide variable). We optimize
2500 * by noticing that we only need to do this the first
2501 * time we touch a register.
2503 if (!is_q && e == 0 && (r == 0 || xs == selem - 1)) {
2504 clear_vec_high(s, tt);
2507 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2508 tt = (tt + 1) % 32;
2513 if (is_postidx) {
2514 int rm = extract32(insn, 16, 5);
2515 if (rm == 31) {
2516 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2517 } else {
2518 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2521 tcg_temp_free_i64(tcg_addr);
2524 /* C3.3.3 AdvSIMD load/store single structure
2526 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2527 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2528 * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size | Rn | Rt |
2529 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2531 * C3.3.4 AdvSIMD load/store single structure (post-indexed)
2533 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2534 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2535 * | 0 | Q | 0 0 1 1 0 1 1 | L R | Rm | opc | S | size | Rn | Rt |
2536 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2538 * Rt: first (or only) SIMD&FP register to be transferred
2539 * Rn: base address or SP
2540 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2541 * index = encoded in Q:S:size dependent on size
2543 * lane_size = encoded in R, opc
2544 * transfer width = encoded in opc, S, size
2546 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
2548 int rt = extract32(insn, 0, 5);
2549 int rn = extract32(insn, 5, 5);
2550 int size = extract32(insn, 10, 2);
2551 int S = extract32(insn, 12, 1);
2552 int opc = extract32(insn, 13, 3);
2553 int R = extract32(insn, 21, 1);
2554 int is_load = extract32(insn, 22, 1);
2555 int is_postidx = extract32(insn, 23, 1);
2556 int is_q = extract32(insn, 30, 1);
2558 int scale = extract32(opc, 1, 2);
2559 int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
2560 bool replicate = false;
2561 int index = is_q << 3 | S << 2 | size;
2562 int ebytes, xs;
2563 TCGv_i64 tcg_addr, tcg_rn;
2565 switch (scale) {
2566 case 3:
2567 if (!is_load || S) {
2568 unallocated_encoding(s);
2569 return;
2571 scale = size;
2572 replicate = true;
2573 break;
2574 case 0:
2575 break;
2576 case 1:
2577 if (extract32(size, 0, 1)) {
2578 unallocated_encoding(s);
2579 return;
2581 index >>= 1;
2582 break;
2583 case 2:
2584 if (extract32(size, 1, 1)) {
2585 unallocated_encoding(s);
2586 return;
2588 if (!extract32(size, 0, 1)) {
2589 index >>= 2;
2590 } else {
2591 if (S) {
2592 unallocated_encoding(s);
2593 return;
2595 index >>= 3;
2596 scale = 3;
2598 break;
2599 default:
2600 g_assert_not_reached();
2603 if (!fp_access_check(s)) {
2604 return;
2607 ebytes = 1 << scale;
2609 if (rn == 31) {
2610 gen_check_sp_alignment(s);
2613 tcg_rn = cpu_reg_sp(s, rn);
2614 tcg_addr = tcg_temp_new_i64();
2615 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2617 for (xs = 0; xs < selem; xs++) {
2618 if (replicate) {
2619 /* Load and replicate to all elements */
2620 uint64_t mulconst;
2621 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
2623 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr,
2624 get_mem_index(s), s->be_data + scale);
2625 switch (scale) {
2626 case 0:
2627 mulconst = 0x0101010101010101ULL;
2628 break;
2629 case 1:
2630 mulconst = 0x0001000100010001ULL;
2631 break;
2632 case 2:
2633 mulconst = 0x0000000100000001ULL;
2634 break;
2635 case 3:
2636 mulconst = 0;
2637 break;
2638 default:
2639 g_assert_not_reached();
2641 if (mulconst) {
2642 tcg_gen_muli_i64(tcg_tmp, tcg_tmp, mulconst);
2644 write_vec_element(s, tcg_tmp, rt, 0, MO_64);
2645 if (is_q) {
2646 write_vec_element(s, tcg_tmp, rt, 1, MO_64);
2647 } else {
2648 clear_vec_high(s, rt);
2650 tcg_temp_free_i64(tcg_tmp);
2651 } else {
2652 /* Load/store one element per register */
2653 if (is_load) {
2654 do_vec_ld(s, rt, index, tcg_addr, s->be_data + scale);
2655 } else {
2656 do_vec_st(s, rt, index, tcg_addr, s->be_data + scale);
2659 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2660 rt = (rt + 1) % 32;
2663 if (is_postidx) {
2664 int rm = extract32(insn, 16, 5);
2665 if (rm == 31) {
2666 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2667 } else {
2668 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2671 tcg_temp_free_i64(tcg_addr);
2674 /* C3.3 Loads and stores */
2675 static void disas_ldst(DisasContext *s, uint32_t insn)
2677 switch (extract32(insn, 24, 6)) {
2678 case 0x08: /* Load/store exclusive */
2679 disas_ldst_excl(s, insn);
2680 break;
2681 case 0x18: case 0x1c: /* Load register (literal) */
2682 disas_ld_lit(s, insn);
2683 break;
2684 case 0x28: case 0x29:
2685 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
2686 disas_ldst_pair(s, insn);
2687 break;
2688 case 0x38: case 0x39:
2689 case 0x3c: case 0x3d: /* Load/store register (all forms) */
2690 disas_ldst_reg(s, insn);
2691 break;
2692 case 0x0c: /* AdvSIMD load/store multiple structures */
2693 disas_ldst_multiple_struct(s, insn);
2694 break;
2695 case 0x0d: /* AdvSIMD load/store single structure */
2696 disas_ldst_single_struct(s, insn);
2697 break;
2698 default:
2699 unallocated_encoding(s);
2700 break;
2704 /* C3.4.6 PC-rel. addressing
2705 * 31 30 29 28 24 23 5 4 0
2706 * +----+-------+-----------+-------------------+------+
2707 * | op | immlo | 1 0 0 0 0 | immhi | Rd |
2708 * +----+-------+-----------+-------------------+------+
2710 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
2712 unsigned int page, rd;
2713 uint64_t base;
2714 uint64_t offset;
2716 page = extract32(insn, 31, 1);
2717 /* SignExtend(immhi:immlo) -> offset */
2718 offset = sextract64(insn, 5, 19);
2719 offset = offset << 2 | extract32(insn, 29, 2);
2720 rd = extract32(insn, 0, 5);
2721 base = s->pc - 4;
2723 if (page) {
2724 /* ADRP (page based) */
2725 base &= ~0xfff;
2726 offset <<= 12;
2729 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
2733 * C3.4.1 Add/subtract (immediate)
2735 * 31 30 29 28 24 23 22 21 10 9 5 4 0
2736 * +--+--+--+-----------+-----+-------------+-----+-----+
2737 * |sf|op| S| 1 0 0 0 1 |shift| imm12 | Rn | Rd |
2738 * +--+--+--+-----------+-----+-------------+-----+-----+
2740 * sf: 0 -> 32bit, 1 -> 64bit
2741 * op: 0 -> add , 1 -> sub
2742 * S: 1 -> set flags
2743 * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
2745 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
2747 int rd = extract32(insn, 0, 5);
2748 int rn = extract32(insn, 5, 5);
2749 uint64_t imm = extract32(insn, 10, 12);
2750 int shift = extract32(insn, 22, 2);
2751 bool setflags = extract32(insn, 29, 1);
2752 bool sub_op = extract32(insn, 30, 1);
2753 bool is_64bit = extract32(insn, 31, 1);
2755 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2756 TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
2757 TCGv_i64 tcg_result;
2759 switch (shift) {
2760 case 0x0:
2761 break;
2762 case 0x1:
2763 imm <<= 12;
2764 break;
2765 default:
2766 unallocated_encoding(s);
2767 return;
2770 tcg_result = tcg_temp_new_i64();
2771 if (!setflags) {
2772 if (sub_op) {
2773 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
2774 } else {
2775 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
2777 } else {
2778 TCGv_i64 tcg_imm = tcg_const_i64(imm);
2779 if (sub_op) {
2780 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2781 } else {
2782 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2784 tcg_temp_free_i64(tcg_imm);
2787 if (is_64bit) {
2788 tcg_gen_mov_i64(tcg_rd, tcg_result);
2789 } else {
2790 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
2793 tcg_temp_free_i64(tcg_result);
2796 /* The input should be a value in the bottom e bits (with higher
2797 * bits zero); returns that value replicated into every element
2798 * of size e in a 64 bit integer.
2800 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
2802 assert(e != 0);
2803 while (e < 64) {
2804 mask |= mask << e;
2805 e *= 2;
2807 return mask;
2810 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
2811 static inline uint64_t bitmask64(unsigned int length)
2813 assert(length > 0 && length <= 64);
2814 return ~0ULL >> (64 - length);
2817 /* Simplified variant of pseudocode DecodeBitMasks() for the case where we
2818 * only require the wmask. Returns false if the imms/immr/immn are a reserved
2819 * value (ie should cause a guest UNDEF exception), and true if they are
2820 * valid, in which case the decoded bit pattern is written to result.
2822 static bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
2823 unsigned int imms, unsigned int immr)
2825 uint64_t mask;
2826 unsigned e, levels, s, r;
2827 int len;
2829 assert(immn < 2 && imms < 64 && immr < 64);
2831 /* The bit patterns we create here are 64 bit patterns which
2832 * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
2833 * 64 bits each. Each element contains the same value: a run
2834 * of between 1 and e-1 non-zero bits, rotated within the
2835 * element by between 0 and e-1 bits.
2837 * The element size and run length are encoded into immn (1 bit)
2838 * and imms (6 bits) as follows:
2839 * 64 bit elements: immn = 1, imms = <length of run - 1>
2840 * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
2841 * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
2842 * 8 bit elements: immn = 0, imms = 110 : <length of run - 1>
2843 * 4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
2844 * 2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
2845 * Notice that immn = 0, imms = 11111x is the only combination
2846 * not covered by one of the above options; this is reserved.
2847 * Further, <length of run - 1> all-ones is a reserved pattern.
2849 * In all cases the rotation is by immr % e (and immr is 6 bits).
2852 /* First determine the element size */
2853 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
2854 if (len < 1) {
2855 /* This is the immn == 0, imms == 0x11111x case */
2856 return false;
2858 e = 1 << len;
2860 levels = e - 1;
2861 s = imms & levels;
2862 r = immr & levels;
2864 if (s == levels) {
2865 /* <length of run - 1> mustn't be all-ones. */
2866 return false;
2869 /* Create the value of one element: s+1 set bits rotated
2870 * by r within the element (which is e bits wide)...
2872 mask = bitmask64(s + 1);
2873 if (r) {
2874 mask = (mask >> r) | (mask << (e - r));
2875 mask &= bitmask64(e);
2877 /* ...then replicate the element over the whole 64 bit value */
2878 mask = bitfield_replicate(mask, e);
2879 *result = mask;
2880 return true;
2883 /* C3.4.4 Logical (immediate)
2884 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
2885 * +----+-----+-------------+---+------+------+------+------+
2886 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms | Rn | Rd |
2887 * +----+-----+-------------+---+------+------+------+------+
2889 static void disas_logic_imm(DisasContext *s, uint32_t insn)
2891 unsigned int sf, opc, is_n, immr, imms, rn, rd;
2892 TCGv_i64 tcg_rd, tcg_rn;
2893 uint64_t wmask;
2894 bool is_and = false;
2896 sf = extract32(insn, 31, 1);
2897 opc = extract32(insn, 29, 2);
2898 is_n = extract32(insn, 22, 1);
2899 immr = extract32(insn, 16, 6);
2900 imms = extract32(insn, 10, 6);
2901 rn = extract32(insn, 5, 5);
2902 rd = extract32(insn, 0, 5);
2904 if (!sf && is_n) {
2905 unallocated_encoding(s);
2906 return;
2909 if (opc == 0x3) { /* ANDS */
2910 tcg_rd = cpu_reg(s, rd);
2911 } else {
2912 tcg_rd = cpu_reg_sp(s, rd);
2914 tcg_rn = cpu_reg(s, rn);
2916 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
2917 /* some immediate field values are reserved */
2918 unallocated_encoding(s);
2919 return;
2922 if (!sf) {
2923 wmask &= 0xffffffff;
2926 switch (opc) {
2927 case 0x3: /* ANDS */
2928 case 0x0: /* AND */
2929 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
2930 is_and = true;
2931 break;
2932 case 0x1: /* ORR */
2933 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
2934 break;
2935 case 0x2: /* EOR */
2936 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
2937 break;
2938 default:
2939 assert(FALSE); /* must handle all above */
2940 break;
2943 if (!sf && !is_and) {
2944 /* zero extend final result; we know we can skip this for AND
2945 * since the immediate had the high 32 bits clear.
2947 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2950 if (opc == 3) { /* ANDS */
2951 gen_logic_CC(sf, tcg_rd);
2956 * C3.4.5 Move wide (immediate)
2958 * 31 30 29 28 23 22 21 20 5 4 0
2959 * +--+-----+-------------+-----+----------------+------+
2960 * |sf| opc | 1 0 0 1 0 1 | hw | imm16 | Rd |
2961 * +--+-----+-------------+-----+----------------+------+
2963 * sf: 0 -> 32 bit, 1 -> 64 bit
2964 * opc: 00 -> N, 10 -> Z, 11 -> K
2965 * hw: shift/16 (0,16, and sf only 32, 48)
2967 static void disas_movw_imm(DisasContext *s, uint32_t insn)
2969 int rd = extract32(insn, 0, 5);
2970 uint64_t imm = extract32(insn, 5, 16);
2971 int sf = extract32(insn, 31, 1);
2972 int opc = extract32(insn, 29, 2);
2973 int pos = extract32(insn, 21, 2) << 4;
2974 TCGv_i64 tcg_rd = cpu_reg(s, rd);
2975 TCGv_i64 tcg_imm;
2977 if (!sf && (pos >= 32)) {
2978 unallocated_encoding(s);
2979 return;
2982 switch (opc) {
2983 case 0: /* MOVN */
2984 case 2: /* MOVZ */
2985 imm <<= pos;
2986 if (opc == 0) {
2987 imm = ~imm;
2989 if (!sf) {
2990 imm &= 0xffffffffu;
2992 tcg_gen_movi_i64(tcg_rd, imm);
2993 break;
2994 case 3: /* MOVK */
2995 tcg_imm = tcg_const_i64(imm);
2996 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
2997 tcg_temp_free_i64(tcg_imm);
2998 if (!sf) {
2999 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3001 break;
3002 default:
3003 unallocated_encoding(s);
3004 break;
3008 /* C3.4.2 Bitfield
3009 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
3010 * +----+-----+-------------+---+------+------+------+------+
3011 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms | Rn | Rd |
3012 * +----+-----+-------------+---+------+------+------+------+
3014 static void disas_bitfield(DisasContext *s, uint32_t insn)
3016 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
3017 TCGv_i64 tcg_rd, tcg_tmp;
3019 sf = extract32(insn, 31, 1);
3020 opc = extract32(insn, 29, 2);
3021 n = extract32(insn, 22, 1);
3022 ri = extract32(insn, 16, 6);
3023 si = extract32(insn, 10, 6);
3024 rn = extract32(insn, 5, 5);
3025 rd = extract32(insn, 0, 5);
3026 bitsize = sf ? 64 : 32;
3028 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
3029 unallocated_encoding(s);
3030 return;
3033 tcg_rd = cpu_reg(s, rd);
3035 /* Suppress the zero-extend for !sf. Since RI and SI are constrained
3036 to be smaller than bitsize, we'll never reference data outside the
3037 low 32-bits anyway. */
3038 tcg_tmp = read_cpu_reg(s, rn, 1);
3040 /* Recognize the common aliases. */
3041 if (opc == 0) { /* SBFM */
3042 if (ri == 0) {
3043 if (si == 7) { /* SXTB */
3044 tcg_gen_ext8s_i64(tcg_rd, tcg_tmp);
3045 goto done;
3046 } else if (si == 15) { /* SXTH */
3047 tcg_gen_ext16s_i64(tcg_rd, tcg_tmp);
3048 goto done;
3049 } else if (si == 31) { /* SXTW */
3050 tcg_gen_ext32s_i64(tcg_rd, tcg_tmp);
3051 goto done;
3054 if (si == 63 || (si == 31 && ri <= si)) { /* ASR */
3055 if (si == 31) {
3056 tcg_gen_ext32s_i64(tcg_tmp, tcg_tmp);
3058 tcg_gen_sari_i64(tcg_rd, tcg_tmp, ri);
3059 goto done;
3061 } else if (opc == 2) { /* UBFM */
3062 if (ri == 0) { /* UXTB, UXTH, plus non-canonical AND */
3063 tcg_gen_andi_i64(tcg_rd, tcg_tmp, bitmask64(si + 1));
3064 return;
3066 if (si == 63 || (si == 31 && ri <= si)) { /* LSR */
3067 if (si == 31) {
3068 tcg_gen_ext32u_i64(tcg_tmp, tcg_tmp);
3070 tcg_gen_shri_i64(tcg_rd, tcg_tmp, ri);
3071 return;
3073 if (si + 1 == ri && si != bitsize - 1) { /* LSL */
3074 int shift = bitsize - 1 - si;
3075 tcg_gen_shli_i64(tcg_rd, tcg_tmp, shift);
3076 goto done;
3080 if (opc != 1) { /* SBFM or UBFM */
3081 tcg_gen_movi_i64(tcg_rd, 0);
3084 /* do the bit move operation */
3085 if (si >= ri) {
3086 /* Wd<s-r:0> = Wn<s:r> */
3087 tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
3088 pos = 0;
3089 len = (si - ri) + 1;
3090 } else {
3091 /* Wd<32+s-r,32-r> = Wn<s:0> */
3092 pos = bitsize - ri;
3093 len = si + 1;
3096 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
3098 if (opc == 0) { /* SBFM - sign extend the destination field */
3099 tcg_gen_shli_i64(tcg_rd, tcg_rd, 64 - (pos + len));
3100 tcg_gen_sari_i64(tcg_rd, tcg_rd, 64 - (pos + len));
3103 done:
3104 if (!sf) { /* zero extend final result */
3105 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3109 /* C3.4.3 Extract
3110 * 31 30 29 28 23 22 21 20 16 15 10 9 5 4 0
3111 * +----+------+-------------+---+----+------+--------+------+------+
3112 * | sf | op21 | 1 0 0 1 1 1 | N | o0 | Rm | imms | Rn | Rd |
3113 * +----+------+-------------+---+----+------+--------+------+------+
3115 static void disas_extract(DisasContext *s, uint32_t insn)
3117 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
3119 sf = extract32(insn, 31, 1);
3120 n = extract32(insn, 22, 1);
3121 rm = extract32(insn, 16, 5);
3122 imm = extract32(insn, 10, 6);
3123 rn = extract32(insn, 5, 5);
3124 rd = extract32(insn, 0, 5);
3125 op21 = extract32(insn, 29, 2);
3126 op0 = extract32(insn, 21, 1);
3127 bitsize = sf ? 64 : 32;
3129 if (sf != n || op21 || op0 || imm >= bitsize) {
3130 unallocated_encoding(s);
3131 } else {
3132 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
3134 tcg_rd = cpu_reg(s, rd);
3136 if (unlikely(imm == 0)) {
3137 /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
3138 * so an extract from bit 0 is a special case.
3140 if (sf) {
3141 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
3142 } else {
3143 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
3145 } else if (rm == rn) { /* ROR */
3146 tcg_rm = cpu_reg(s, rm);
3147 if (sf) {
3148 tcg_gen_rotri_i64(tcg_rd, tcg_rm, imm);
3149 } else {
3150 TCGv_i32 tmp = tcg_temp_new_i32();
3151 tcg_gen_extrl_i64_i32(tmp, tcg_rm);
3152 tcg_gen_rotri_i32(tmp, tmp, imm);
3153 tcg_gen_extu_i32_i64(tcg_rd, tmp);
3154 tcg_temp_free_i32(tmp);
3156 } else {
3157 tcg_rm = read_cpu_reg(s, rm, sf);
3158 tcg_rn = read_cpu_reg(s, rn, sf);
3159 tcg_gen_shri_i64(tcg_rm, tcg_rm, imm);
3160 tcg_gen_shli_i64(tcg_rn, tcg_rn, bitsize - imm);
3161 tcg_gen_or_i64(tcg_rd, tcg_rm, tcg_rn);
3162 if (!sf) {
3163 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3169 /* C3.4 Data processing - immediate */
3170 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
3172 switch (extract32(insn, 23, 6)) {
3173 case 0x20: case 0x21: /* PC-rel. addressing */
3174 disas_pc_rel_adr(s, insn);
3175 break;
3176 case 0x22: case 0x23: /* Add/subtract (immediate) */
3177 disas_add_sub_imm(s, insn);
3178 break;
3179 case 0x24: /* Logical (immediate) */
3180 disas_logic_imm(s, insn);
3181 break;
3182 case 0x25: /* Move wide (immediate) */
3183 disas_movw_imm(s, insn);
3184 break;
3185 case 0x26: /* Bitfield */
3186 disas_bitfield(s, insn);
3187 break;
3188 case 0x27: /* Extract */
3189 disas_extract(s, insn);
3190 break;
3191 default:
3192 unallocated_encoding(s);
3193 break;
3197 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
3198 * Note that it is the caller's responsibility to ensure that the
3199 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
3200 * mandated semantics for out of range shifts.
3202 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
3203 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
3205 switch (shift_type) {
3206 case A64_SHIFT_TYPE_LSL:
3207 tcg_gen_shl_i64(dst, src, shift_amount);
3208 break;
3209 case A64_SHIFT_TYPE_LSR:
3210 tcg_gen_shr_i64(dst, src, shift_amount);
3211 break;
3212 case A64_SHIFT_TYPE_ASR:
3213 if (!sf) {
3214 tcg_gen_ext32s_i64(dst, src);
3216 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
3217 break;
3218 case A64_SHIFT_TYPE_ROR:
3219 if (sf) {
3220 tcg_gen_rotr_i64(dst, src, shift_amount);
3221 } else {
3222 TCGv_i32 t0, t1;
3223 t0 = tcg_temp_new_i32();
3224 t1 = tcg_temp_new_i32();
3225 tcg_gen_extrl_i64_i32(t0, src);
3226 tcg_gen_extrl_i64_i32(t1, shift_amount);
3227 tcg_gen_rotr_i32(t0, t0, t1);
3228 tcg_gen_extu_i32_i64(dst, t0);
3229 tcg_temp_free_i32(t0);
3230 tcg_temp_free_i32(t1);
3232 break;
3233 default:
3234 assert(FALSE); /* all shift types should be handled */
3235 break;
3238 if (!sf) { /* zero extend final result */
3239 tcg_gen_ext32u_i64(dst, dst);
3243 /* Shift a TCGv src by immediate, put result in dst.
3244 * The shift amount must be in range (this should always be true as the
3245 * relevant instructions will UNDEF on bad shift immediates).
3247 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
3248 enum a64_shift_type shift_type, unsigned int shift_i)
3250 assert(shift_i < (sf ? 64 : 32));
3252 if (shift_i == 0) {
3253 tcg_gen_mov_i64(dst, src);
3254 } else {
3255 TCGv_i64 shift_const;
3257 shift_const = tcg_const_i64(shift_i);
3258 shift_reg(dst, src, sf, shift_type, shift_const);
3259 tcg_temp_free_i64(shift_const);
3263 /* C3.5.10 Logical (shifted register)
3264 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
3265 * +----+-----+-----------+-------+---+------+--------+------+------+
3266 * | sf | opc | 0 1 0 1 0 | shift | N | Rm | imm6 | Rn | Rd |
3267 * +----+-----+-----------+-------+---+------+--------+------+------+
3269 static void disas_logic_reg(DisasContext *s, uint32_t insn)
3271 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
3272 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
3274 sf = extract32(insn, 31, 1);
3275 opc = extract32(insn, 29, 2);
3276 shift_type = extract32(insn, 22, 2);
3277 invert = extract32(insn, 21, 1);
3278 rm = extract32(insn, 16, 5);
3279 shift_amount = extract32(insn, 10, 6);
3280 rn = extract32(insn, 5, 5);
3281 rd = extract32(insn, 0, 5);
3283 if (!sf && (shift_amount & (1 << 5))) {
3284 unallocated_encoding(s);
3285 return;
3288 tcg_rd = cpu_reg(s, rd);
3290 if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
3291 /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
3292 * register-register MOV and MVN, so it is worth special casing.
3294 tcg_rm = cpu_reg(s, rm);
3295 if (invert) {
3296 tcg_gen_not_i64(tcg_rd, tcg_rm);
3297 if (!sf) {
3298 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3300 } else {
3301 if (sf) {
3302 tcg_gen_mov_i64(tcg_rd, tcg_rm);
3303 } else {
3304 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
3307 return;
3310 tcg_rm = read_cpu_reg(s, rm, sf);
3312 if (shift_amount) {
3313 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
3316 tcg_rn = cpu_reg(s, rn);
3318 switch (opc | (invert << 2)) {
3319 case 0: /* AND */
3320 case 3: /* ANDS */
3321 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
3322 break;
3323 case 1: /* ORR */
3324 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
3325 break;
3326 case 2: /* EOR */
3327 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
3328 break;
3329 case 4: /* BIC */
3330 case 7: /* BICS */
3331 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
3332 break;
3333 case 5: /* ORN */
3334 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
3335 break;
3336 case 6: /* EON */
3337 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
3338 break;
3339 default:
3340 assert(FALSE);
3341 break;
3344 if (!sf) {
3345 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3348 if (opc == 3) {
3349 gen_logic_CC(sf, tcg_rd);
3354 * C3.5.1 Add/subtract (extended register)
3356 * 31|30|29|28 24|23 22|21|20 16|15 13|12 10|9 5|4 0|
3357 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3358 * |sf|op| S| 0 1 0 1 1 | opt | 1| Rm |option| imm3 | Rn | Rd |
3359 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3361 * sf: 0 -> 32bit, 1 -> 64bit
3362 * op: 0 -> add , 1 -> sub
3363 * S: 1 -> set flags
3364 * opt: 00
3365 * option: extension type (see DecodeRegExtend)
3366 * imm3: optional shift to Rm
3368 * Rd = Rn + LSL(extend(Rm), amount)
3370 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
3372 int rd = extract32(insn, 0, 5);
3373 int rn = extract32(insn, 5, 5);
3374 int imm3 = extract32(insn, 10, 3);
3375 int option = extract32(insn, 13, 3);
3376 int rm = extract32(insn, 16, 5);
3377 bool setflags = extract32(insn, 29, 1);
3378 bool sub_op = extract32(insn, 30, 1);
3379 bool sf = extract32(insn, 31, 1);
3381 TCGv_i64 tcg_rm, tcg_rn; /* temps */
3382 TCGv_i64 tcg_rd;
3383 TCGv_i64 tcg_result;
3385 if (imm3 > 4) {
3386 unallocated_encoding(s);
3387 return;
3390 /* non-flag setting ops may use SP */
3391 if (!setflags) {
3392 tcg_rd = cpu_reg_sp(s, rd);
3393 } else {
3394 tcg_rd = cpu_reg(s, rd);
3396 tcg_rn = read_cpu_reg_sp(s, rn, sf);
3398 tcg_rm = read_cpu_reg(s, rm, sf);
3399 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
3401 tcg_result = tcg_temp_new_i64();
3403 if (!setflags) {
3404 if (sub_op) {
3405 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3406 } else {
3407 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3409 } else {
3410 if (sub_op) {
3411 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3412 } else {
3413 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3417 if (sf) {
3418 tcg_gen_mov_i64(tcg_rd, tcg_result);
3419 } else {
3420 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3423 tcg_temp_free_i64(tcg_result);
3427 * C3.5.2 Add/subtract (shifted register)
3429 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
3430 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3431 * |sf|op| S| 0 1 0 1 1 |shift| 0| Rm | imm6 | Rn | Rd |
3432 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3434 * sf: 0 -> 32bit, 1 -> 64bit
3435 * op: 0 -> add , 1 -> sub
3436 * S: 1 -> set flags
3437 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
3438 * imm6: Shift amount to apply to Rm before the add/sub
3440 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
3442 int rd = extract32(insn, 0, 5);
3443 int rn = extract32(insn, 5, 5);
3444 int imm6 = extract32(insn, 10, 6);
3445 int rm = extract32(insn, 16, 5);
3446 int shift_type = extract32(insn, 22, 2);
3447 bool setflags = extract32(insn, 29, 1);
3448 bool sub_op = extract32(insn, 30, 1);
3449 bool sf = extract32(insn, 31, 1);
3451 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3452 TCGv_i64 tcg_rn, tcg_rm;
3453 TCGv_i64 tcg_result;
3455 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
3456 unallocated_encoding(s);
3457 return;
3460 tcg_rn = read_cpu_reg(s, rn, sf);
3461 tcg_rm = read_cpu_reg(s, rm, sf);
3463 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
3465 tcg_result = tcg_temp_new_i64();
3467 if (!setflags) {
3468 if (sub_op) {
3469 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3470 } else {
3471 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3473 } else {
3474 if (sub_op) {
3475 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3476 } else {
3477 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3481 if (sf) {
3482 tcg_gen_mov_i64(tcg_rd, tcg_result);
3483 } else {
3484 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3487 tcg_temp_free_i64(tcg_result);
3490 /* C3.5.9 Data-processing (3 source)
3492 31 30 29 28 24 23 21 20 16 15 14 10 9 5 4 0
3493 +--+------+-----------+------+------+----+------+------+------+
3494 |sf| op54 | 1 1 0 1 1 | op31 | Rm | o0 | Ra | Rn | Rd |
3495 +--+------+-----------+------+------+----+------+------+------+
3498 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
3500 int rd = extract32(insn, 0, 5);
3501 int rn = extract32(insn, 5, 5);
3502 int ra = extract32(insn, 10, 5);
3503 int rm = extract32(insn, 16, 5);
3504 int op_id = (extract32(insn, 29, 3) << 4) |
3505 (extract32(insn, 21, 3) << 1) |
3506 extract32(insn, 15, 1);
3507 bool sf = extract32(insn, 31, 1);
3508 bool is_sub = extract32(op_id, 0, 1);
3509 bool is_high = extract32(op_id, 2, 1);
3510 bool is_signed = false;
3511 TCGv_i64 tcg_op1;
3512 TCGv_i64 tcg_op2;
3513 TCGv_i64 tcg_tmp;
3515 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
3516 switch (op_id) {
3517 case 0x42: /* SMADDL */
3518 case 0x43: /* SMSUBL */
3519 case 0x44: /* SMULH */
3520 is_signed = true;
3521 break;
3522 case 0x0: /* MADD (32bit) */
3523 case 0x1: /* MSUB (32bit) */
3524 case 0x40: /* MADD (64bit) */
3525 case 0x41: /* MSUB (64bit) */
3526 case 0x4a: /* UMADDL */
3527 case 0x4b: /* UMSUBL */
3528 case 0x4c: /* UMULH */
3529 break;
3530 default:
3531 unallocated_encoding(s);
3532 return;
3535 if (is_high) {
3536 TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
3537 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3538 TCGv_i64 tcg_rn = cpu_reg(s, rn);
3539 TCGv_i64 tcg_rm = cpu_reg(s, rm);
3541 if (is_signed) {
3542 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3543 } else {
3544 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3547 tcg_temp_free_i64(low_bits);
3548 return;
3551 tcg_op1 = tcg_temp_new_i64();
3552 tcg_op2 = tcg_temp_new_i64();
3553 tcg_tmp = tcg_temp_new_i64();
3555 if (op_id < 0x42) {
3556 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
3557 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
3558 } else {
3559 if (is_signed) {
3560 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
3561 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
3562 } else {
3563 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
3564 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
3568 if (ra == 31 && !is_sub) {
3569 /* Special-case MADD with rA == XZR; it is the standard MUL alias */
3570 tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
3571 } else {
3572 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
3573 if (is_sub) {
3574 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3575 } else {
3576 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3580 if (!sf) {
3581 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
3584 tcg_temp_free_i64(tcg_op1);
3585 tcg_temp_free_i64(tcg_op2);
3586 tcg_temp_free_i64(tcg_tmp);
3589 /* C3.5.3 - Add/subtract (with carry)
3590 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
3591 * +--+--+--+------------------------+------+---------+------+-----+
3592 * |sf|op| S| 1 1 0 1 0 0 0 0 | rm | opcode2 | Rn | Rd |
3593 * +--+--+--+------------------------+------+---------+------+-----+
3594 * [000000]
3597 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
3599 unsigned int sf, op, setflags, rm, rn, rd;
3600 TCGv_i64 tcg_y, tcg_rn, tcg_rd;
3602 if (extract32(insn, 10, 6) != 0) {
3603 unallocated_encoding(s);
3604 return;
3607 sf = extract32(insn, 31, 1);
3608 op = extract32(insn, 30, 1);
3609 setflags = extract32(insn, 29, 1);
3610 rm = extract32(insn, 16, 5);
3611 rn = extract32(insn, 5, 5);
3612 rd = extract32(insn, 0, 5);
3614 tcg_rd = cpu_reg(s, rd);
3615 tcg_rn = cpu_reg(s, rn);
3617 if (op) {
3618 tcg_y = new_tmp_a64(s);
3619 tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
3620 } else {
3621 tcg_y = cpu_reg(s, rm);
3624 if (setflags) {
3625 gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
3626 } else {
3627 gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
3631 /* C3.5.4 - C3.5.5 Conditional compare (immediate / register)
3632 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
3633 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3634 * |sf|op| S| 1 1 0 1 0 0 1 0 |imm5/rm | cond |i/r |o2| Rn |o3|nzcv |
3635 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3636 * [1] y [0] [0]
3638 static void disas_cc(DisasContext *s, uint32_t insn)
3640 unsigned int sf, op, y, cond, rn, nzcv, is_imm;
3641 TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
3642 TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
3643 DisasCompare c;
3645 if (!extract32(insn, 29, 1)) {
3646 unallocated_encoding(s);
3647 return;
3649 if (insn & (1 << 10 | 1 << 4)) {
3650 unallocated_encoding(s);
3651 return;
3653 sf = extract32(insn, 31, 1);
3654 op = extract32(insn, 30, 1);
3655 is_imm = extract32(insn, 11, 1);
3656 y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
3657 cond = extract32(insn, 12, 4);
3658 rn = extract32(insn, 5, 5);
3659 nzcv = extract32(insn, 0, 4);
3661 /* Set T0 = !COND. */
3662 tcg_t0 = tcg_temp_new_i32();
3663 arm_test_cc(&c, cond);
3664 tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
3665 arm_free_cc(&c);
3667 /* Load the arguments for the new comparison. */
3668 if (is_imm) {
3669 tcg_y = new_tmp_a64(s);
3670 tcg_gen_movi_i64(tcg_y, y);
3671 } else {
3672 tcg_y = cpu_reg(s, y);
3674 tcg_rn = cpu_reg(s, rn);
3676 /* Set the flags for the new comparison. */
3677 tcg_tmp = tcg_temp_new_i64();
3678 if (op) {
3679 gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3680 } else {
3681 gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3683 tcg_temp_free_i64(tcg_tmp);
3685 /* If COND was false, force the flags to #nzcv. Compute two masks
3686 * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
3687 * For tcg hosts that support ANDC, we can make do with just T1.
3688 * In either case, allow the tcg optimizer to delete any unused mask.
3690 tcg_t1 = tcg_temp_new_i32();
3691 tcg_t2 = tcg_temp_new_i32();
3692 tcg_gen_neg_i32(tcg_t1, tcg_t0);
3693 tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
3695 if (nzcv & 8) { /* N */
3696 tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
3697 } else {
3698 if (TCG_TARGET_HAS_andc_i32) {
3699 tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
3700 } else {
3701 tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
3704 if (nzcv & 4) { /* Z */
3705 if (TCG_TARGET_HAS_andc_i32) {
3706 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
3707 } else {
3708 tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
3710 } else {
3711 tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
3713 if (nzcv & 2) { /* C */
3714 tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
3715 } else {
3716 if (TCG_TARGET_HAS_andc_i32) {
3717 tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
3718 } else {
3719 tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
3722 if (nzcv & 1) { /* V */
3723 tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
3724 } else {
3725 if (TCG_TARGET_HAS_andc_i32) {
3726 tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
3727 } else {
3728 tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
3731 tcg_temp_free_i32(tcg_t0);
3732 tcg_temp_free_i32(tcg_t1);
3733 tcg_temp_free_i32(tcg_t2);
3736 /* C3.5.6 Conditional select
3737 * 31 30 29 28 21 20 16 15 12 11 10 9 5 4 0
3738 * +----+----+---+-----------------+------+------+-----+------+------+
3739 * | sf | op | S | 1 1 0 1 0 1 0 0 | Rm | cond | op2 | Rn | Rd |
3740 * +----+----+---+-----------------+------+------+-----+------+------+
3742 static void disas_cond_select(DisasContext *s, uint32_t insn)
3744 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
3745 TCGv_i64 tcg_rd, zero;
3746 DisasCompare64 c;
3748 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
3749 /* S == 1 or op2<1> == 1 */
3750 unallocated_encoding(s);
3751 return;
3753 sf = extract32(insn, 31, 1);
3754 else_inv = extract32(insn, 30, 1);
3755 rm = extract32(insn, 16, 5);
3756 cond = extract32(insn, 12, 4);
3757 else_inc = extract32(insn, 10, 1);
3758 rn = extract32(insn, 5, 5);
3759 rd = extract32(insn, 0, 5);
3761 tcg_rd = cpu_reg(s, rd);
3763 a64_test_cc(&c, cond);
3764 zero = tcg_const_i64(0);
3766 if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
3767 /* CSET & CSETM. */
3768 tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
3769 if (else_inv) {
3770 tcg_gen_neg_i64(tcg_rd, tcg_rd);
3772 } else {
3773 TCGv_i64 t_true = cpu_reg(s, rn);
3774 TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
3775 if (else_inv && else_inc) {
3776 tcg_gen_neg_i64(t_false, t_false);
3777 } else if (else_inv) {
3778 tcg_gen_not_i64(t_false, t_false);
3779 } else if (else_inc) {
3780 tcg_gen_addi_i64(t_false, t_false, 1);
3782 tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
3785 tcg_temp_free_i64(zero);
3786 a64_free_cc(&c);
3788 if (!sf) {
3789 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3793 static void handle_clz(DisasContext *s, unsigned int sf,
3794 unsigned int rn, unsigned int rd)
3796 TCGv_i64 tcg_rd, tcg_rn;
3797 tcg_rd = cpu_reg(s, rd);
3798 tcg_rn = cpu_reg(s, rn);
3800 if (sf) {
3801 gen_helper_clz64(tcg_rd, tcg_rn);
3802 } else {
3803 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3804 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
3805 gen_helper_clz(tcg_tmp32, tcg_tmp32);
3806 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3807 tcg_temp_free_i32(tcg_tmp32);
3811 static void handle_cls(DisasContext *s, unsigned int sf,
3812 unsigned int rn, unsigned int rd)
3814 TCGv_i64 tcg_rd, tcg_rn;
3815 tcg_rd = cpu_reg(s, rd);
3816 tcg_rn = cpu_reg(s, rn);
3818 if (sf) {
3819 gen_helper_cls64(tcg_rd, tcg_rn);
3820 } else {
3821 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3822 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
3823 gen_helper_cls32(tcg_tmp32, tcg_tmp32);
3824 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3825 tcg_temp_free_i32(tcg_tmp32);
3829 static void handle_rbit(DisasContext *s, unsigned int sf,
3830 unsigned int rn, unsigned int rd)
3832 TCGv_i64 tcg_rd, tcg_rn;
3833 tcg_rd = cpu_reg(s, rd);
3834 tcg_rn = cpu_reg(s, rn);
3836 if (sf) {
3837 gen_helper_rbit64(tcg_rd, tcg_rn);
3838 } else {
3839 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3840 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
3841 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
3842 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3843 tcg_temp_free_i32(tcg_tmp32);
3847 /* C5.6.149 REV with sf==1, opcode==3 ("REV64") */
3848 static void handle_rev64(DisasContext *s, unsigned int sf,
3849 unsigned int rn, unsigned int rd)
3851 if (!sf) {
3852 unallocated_encoding(s);
3853 return;
3855 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
3858 /* C5.6.149 REV with sf==0, opcode==2
3859 * C5.6.151 REV32 (sf==1, opcode==2)
3861 static void handle_rev32(DisasContext *s, unsigned int sf,
3862 unsigned int rn, unsigned int rd)
3864 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3866 if (sf) {
3867 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3868 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3870 /* bswap32_i64 requires zero high word */
3871 tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
3872 tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
3873 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
3874 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
3875 tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
3877 tcg_temp_free_i64(tcg_tmp);
3878 } else {
3879 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
3880 tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
3884 /* C5.6.150 REV16 (opcode==1) */
3885 static void handle_rev16(DisasContext *s, unsigned int sf,
3886 unsigned int rn, unsigned int rd)
3888 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3889 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3890 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3892 tcg_gen_andi_i64(tcg_tmp, tcg_rn, 0xffff);
3893 tcg_gen_bswap16_i64(tcg_rd, tcg_tmp);
3895 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 16);
3896 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
3897 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
3898 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 16, 16);
3900 if (sf) {
3901 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
3902 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
3903 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
3904 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 32, 16);
3906 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 48);
3907 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
3908 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 48, 16);
3911 tcg_temp_free_i64(tcg_tmp);
3914 /* C3.5.7 Data-processing (1 source)
3915 * 31 30 29 28 21 20 16 15 10 9 5 4 0
3916 * +----+---+---+-----------------+---------+--------+------+------+
3917 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode | Rn | Rd |
3918 * +----+---+---+-----------------+---------+--------+------+------+
3920 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
3922 unsigned int sf, opcode, rn, rd;
3924 if (extract32(insn, 29, 1) || extract32(insn, 16, 5)) {
3925 unallocated_encoding(s);
3926 return;
3929 sf = extract32(insn, 31, 1);
3930 opcode = extract32(insn, 10, 6);
3931 rn = extract32(insn, 5, 5);
3932 rd = extract32(insn, 0, 5);
3934 switch (opcode) {
3935 case 0: /* RBIT */
3936 handle_rbit(s, sf, rn, rd);
3937 break;
3938 case 1: /* REV16 */
3939 handle_rev16(s, sf, rn, rd);
3940 break;
3941 case 2: /* REV32 */
3942 handle_rev32(s, sf, rn, rd);
3943 break;
3944 case 3: /* REV64 */
3945 handle_rev64(s, sf, rn, rd);
3946 break;
3947 case 4: /* CLZ */
3948 handle_clz(s, sf, rn, rd);
3949 break;
3950 case 5: /* CLS */
3951 handle_cls(s, sf, rn, rd);
3952 break;
3956 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
3957 unsigned int rm, unsigned int rn, unsigned int rd)
3959 TCGv_i64 tcg_n, tcg_m, tcg_rd;
3960 tcg_rd = cpu_reg(s, rd);
3962 if (!sf && is_signed) {
3963 tcg_n = new_tmp_a64(s);
3964 tcg_m = new_tmp_a64(s);
3965 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
3966 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
3967 } else {
3968 tcg_n = read_cpu_reg(s, rn, sf);
3969 tcg_m = read_cpu_reg(s, rm, sf);
3972 if (is_signed) {
3973 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
3974 } else {
3975 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
3978 if (!sf) { /* zero extend final result */
3979 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3983 /* C5.6.115 LSLV, C5.6.118 LSRV, C5.6.17 ASRV, C5.6.154 RORV */
3984 static void handle_shift_reg(DisasContext *s,
3985 enum a64_shift_type shift_type, unsigned int sf,
3986 unsigned int rm, unsigned int rn, unsigned int rd)
3988 TCGv_i64 tcg_shift = tcg_temp_new_i64();
3989 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3990 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3992 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
3993 shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
3994 tcg_temp_free_i64(tcg_shift);
3997 /* CRC32[BHWX], CRC32C[BHWX] */
3998 static void handle_crc32(DisasContext *s,
3999 unsigned int sf, unsigned int sz, bool crc32c,
4000 unsigned int rm, unsigned int rn, unsigned int rd)
4002 TCGv_i64 tcg_acc, tcg_val;
4003 TCGv_i32 tcg_bytes;
4005 if (!arm_dc_feature(s, ARM_FEATURE_CRC)
4006 || (sf == 1 && sz != 3)
4007 || (sf == 0 && sz == 3)) {
4008 unallocated_encoding(s);
4009 return;
4012 if (sz == 3) {
4013 tcg_val = cpu_reg(s, rm);
4014 } else {
4015 uint64_t mask;
4016 switch (sz) {
4017 case 0:
4018 mask = 0xFF;
4019 break;
4020 case 1:
4021 mask = 0xFFFF;
4022 break;
4023 case 2:
4024 mask = 0xFFFFFFFF;
4025 break;
4026 default:
4027 g_assert_not_reached();
4029 tcg_val = new_tmp_a64(s);
4030 tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
4033 tcg_acc = cpu_reg(s, rn);
4034 tcg_bytes = tcg_const_i32(1 << sz);
4036 if (crc32c) {
4037 gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
4038 } else {
4039 gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
4042 tcg_temp_free_i32(tcg_bytes);
4045 /* C3.5.8 Data-processing (2 source)
4046 * 31 30 29 28 21 20 16 15 10 9 5 4 0
4047 * +----+---+---+-----------------+------+--------+------+------+
4048 * | sf | 0 | S | 1 1 0 1 0 1 1 0 | Rm | opcode | Rn | Rd |
4049 * +----+---+---+-----------------+------+--------+------+------+
4051 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
4053 unsigned int sf, rm, opcode, rn, rd;
4054 sf = extract32(insn, 31, 1);
4055 rm = extract32(insn, 16, 5);
4056 opcode = extract32(insn, 10, 6);
4057 rn = extract32(insn, 5, 5);
4058 rd = extract32(insn, 0, 5);
4060 if (extract32(insn, 29, 1)) {
4061 unallocated_encoding(s);
4062 return;
4065 switch (opcode) {
4066 case 2: /* UDIV */
4067 handle_div(s, false, sf, rm, rn, rd);
4068 break;
4069 case 3: /* SDIV */
4070 handle_div(s, true, sf, rm, rn, rd);
4071 break;
4072 case 8: /* LSLV */
4073 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
4074 break;
4075 case 9: /* LSRV */
4076 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
4077 break;
4078 case 10: /* ASRV */
4079 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
4080 break;
4081 case 11: /* RORV */
4082 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
4083 break;
4084 case 16:
4085 case 17:
4086 case 18:
4087 case 19:
4088 case 20:
4089 case 21:
4090 case 22:
4091 case 23: /* CRC32 */
4093 int sz = extract32(opcode, 0, 2);
4094 bool crc32c = extract32(opcode, 2, 1);
4095 handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
4096 break;
4098 default:
4099 unallocated_encoding(s);
4100 break;
4104 /* C3.5 Data processing - register */
4105 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
4107 switch (extract32(insn, 24, 5)) {
4108 case 0x0a: /* Logical (shifted register) */
4109 disas_logic_reg(s, insn);
4110 break;
4111 case 0x0b: /* Add/subtract */
4112 if (insn & (1 << 21)) { /* (extended register) */
4113 disas_add_sub_ext_reg(s, insn);
4114 } else {
4115 disas_add_sub_reg(s, insn);
4117 break;
4118 case 0x1b: /* Data-processing (3 source) */
4119 disas_data_proc_3src(s, insn);
4120 break;
4121 case 0x1a:
4122 switch (extract32(insn, 21, 3)) {
4123 case 0x0: /* Add/subtract (with carry) */
4124 disas_adc_sbc(s, insn);
4125 break;
4126 case 0x2: /* Conditional compare */
4127 disas_cc(s, insn); /* both imm and reg forms */
4128 break;
4129 case 0x4: /* Conditional select */
4130 disas_cond_select(s, insn);
4131 break;
4132 case 0x6: /* Data-processing */
4133 if (insn & (1 << 30)) { /* (1 source) */
4134 disas_data_proc_1src(s, insn);
4135 } else { /* (2 source) */
4136 disas_data_proc_2src(s, insn);
4138 break;
4139 default:
4140 unallocated_encoding(s);
4141 break;
4143 break;
4144 default:
4145 unallocated_encoding(s);
4146 break;
4150 static void handle_fp_compare(DisasContext *s, bool is_double,
4151 unsigned int rn, unsigned int rm,
4152 bool cmp_with_zero, bool signal_all_nans)
4154 TCGv_i64 tcg_flags = tcg_temp_new_i64();
4155 TCGv_ptr fpst = get_fpstatus_ptr();
4157 if (is_double) {
4158 TCGv_i64 tcg_vn, tcg_vm;
4160 tcg_vn = read_fp_dreg(s, rn);
4161 if (cmp_with_zero) {
4162 tcg_vm = tcg_const_i64(0);
4163 } else {
4164 tcg_vm = read_fp_dreg(s, rm);
4166 if (signal_all_nans) {
4167 gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4168 } else {
4169 gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4171 tcg_temp_free_i64(tcg_vn);
4172 tcg_temp_free_i64(tcg_vm);
4173 } else {
4174 TCGv_i32 tcg_vn, tcg_vm;
4176 tcg_vn = read_fp_sreg(s, rn);
4177 if (cmp_with_zero) {
4178 tcg_vm = tcg_const_i32(0);
4179 } else {
4180 tcg_vm = read_fp_sreg(s, rm);
4182 if (signal_all_nans) {
4183 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4184 } else {
4185 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4187 tcg_temp_free_i32(tcg_vn);
4188 tcg_temp_free_i32(tcg_vm);
4191 tcg_temp_free_ptr(fpst);
4193 gen_set_nzcv(tcg_flags);
4195 tcg_temp_free_i64(tcg_flags);
4198 /* C3.6.22 Floating point compare
4199 * 31 30 29 28 24 23 22 21 20 16 15 14 13 10 9 5 4 0
4200 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
4201 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | op | 1 0 0 0 | Rn | op2 |
4202 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
4204 static void disas_fp_compare(DisasContext *s, uint32_t insn)
4206 unsigned int mos, type, rm, op, rn, opc, op2r;
4208 mos = extract32(insn, 29, 3);
4209 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4210 rm = extract32(insn, 16, 5);
4211 op = extract32(insn, 14, 2);
4212 rn = extract32(insn, 5, 5);
4213 opc = extract32(insn, 3, 2);
4214 op2r = extract32(insn, 0, 3);
4216 if (mos || op || op2r || type > 1) {
4217 unallocated_encoding(s);
4218 return;
4221 if (!fp_access_check(s)) {
4222 return;
4225 handle_fp_compare(s, type, rn, rm, opc & 1, opc & 2);
4228 /* C3.6.23 Floating point conditional compare
4229 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
4230 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
4231 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 0 1 | Rn | op | nzcv |
4232 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
4234 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
4236 unsigned int mos, type, rm, cond, rn, op, nzcv;
4237 TCGv_i64 tcg_flags;
4238 TCGLabel *label_continue = NULL;
4240 mos = extract32(insn, 29, 3);
4241 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4242 rm = extract32(insn, 16, 5);
4243 cond = extract32(insn, 12, 4);
4244 rn = extract32(insn, 5, 5);
4245 op = extract32(insn, 4, 1);
4246 nzcv = extract32(insn, 0, 4);
4248 if (mos || type > 1) {
4249 unallocated_encoding(s);
4250 return;
4253 if (!fp_access_check(s)) {
4254 return;
4257 if (cond < 0x0e) { /* not always */
4258 TCGLabel *label_match = gen_new_label();
4259 label_continue = gen_new_label();
4260 arm_gen_test_cc(cond, label_match);
4261 /* nomatch: */
4262 tcg_flags = tcg_const_i64(nzcv << 28);
4263 gen_set_nzcv(tcg_flags);
4264 tcg_temp_free_i64(tcg_flags);
4265 tcg_gen_br(label_continue);
4266 gen_set_label(label_match);
4269 handle_fp_compare(s, type, rn, rm, false, op);
4271 if (cond < 0x0e) {
4272 gen_set_label(label_continue);
4276 /* C3.6.24 Floating point conditional select
4277 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
4278 * +---+---+---+-----------+------+---+------+------+-----+------+------+
4279 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 1 1 | Rn | Rd |
4280 * +---+---+---+-----------+------+---+------+------+-----+------+------+
4282 static void disas_fp_csel(DisasContext *s, uint32_t insn)
4284 unsigned int mos, type, rm, cond, rn, rd;
4285 TCGv_i64 t_true, t_false, t_zero;
4286 DisasCompare64 c;
4288 mos = extract32(insn, 29, 3);
4289 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4290 rm = extract32(insn, 16, 5);
4291 cond = extract32(insn, 12, 4);
4292 rn = extract32(insn, 5, 5);
4293 rd = extract32(insn, 0, 5);
4295 if (mos || type > 1) {
4296 unallocated_encoding(s);
4297 return;
4300 if (!fp_access_check(s)) {
4301 return;
4304 /* Zero extend sreg inputs to 64 bits now. */
4305 t_true = tcg_temp_new_i64();
4306 t_false = tcg_temp_new_i64();
4307 read_vec_element(s, t_true, rn, 0, type ? MO_64 : MO_32);
4308 read_vec_element(s, t_false, rm, 0, type ? MO_64 : MO_32);
4310 a64_test_cc(&c, cond);
4311 t_zero = tcg_const_i64(0);
4312 tcg_gen_movcond_i64(c.cond, t_true, c.value, t_zero, t_true, t_false);
4313 tcg_temp_free_i64(t_zero);
4314 tcg_temp_free_i64(t_false);
4315 a64_free_cc(&c);
4317 /* Note that sregs write back zeros to the high bits,
4318 and we've already done the zero-extension. */
4319 write_fp_dreg(s, rd, t_true);
4320 tcg_temp_free_i64(t_true);
4323 /* C3.6.25 Floating-point data-processing (1 source) - single precision */
4324 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
4326 TCGv_ptr fpst;
4327 TCGv_i32 tcg_op;
4328 TCGv_i32 tcg_res;
4330 fpst = get_fpstatus_ptr();
4331 tcg_op = read_fp_sreg(s, rn);
4332 tcg_res = tcg_temp_new_i32();
4334 switch (opcode) {
4335 case 0x0: /* FMOV */
4336 tcg_gen_mov_i32(tcg_res, tcg_op);
4337 break;
4338 case 0x1: /* FABS */
4339 gen_helper_vfp_abss(tcg_res, tcg_op);
4340 break;
4341 case 0x2: /* FNEG */
4342 gen_helper_vfp_negs(tcg_res, tcg_op);
4343 break;
4344 case 0x3: /* FSQRT */
4345 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
4346 break;
4347 case 0x8: /* FRINTN */
4348 case 0x9: /* FRINTP */
4349 case 0xa: /* FRINTM */
4350 case 0xb: /* FRINTZ */
4351 case 0xc: /* FRINTA */
4353 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4355 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4356 gen_helper_rints(tcg_res, tcg_op, fpst);
4358 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4359 tcg_temp_free_i32(tcg_rmode);
4360 break;
4362 case 0xe: /* FRINTX */
4363 gen_helper_rints_exact(tcg_res, tcg_op, fpst);
4364 break;
4365 case 0xf: /* FRINTI */
4366 gen_helper_rints(tcg_res, tcg_op, fpst);
4367 break;
4368 default:
4369 abort();
4372 write_fp_sreg(s, rd, tcg_res);
4374 tcg_temp_free_ptr(fpst);
4375 tcg_temp_free_i32(tcg_op);
4376 tcg_temp_free_i32(tcg_res);
4379 /* C3.6.25 Floating-point data-processing (1 source) - double precision */
4380 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
4382 TCGv_ptr fpst;
4383 TCGv_i64 tcg_op;
4384 TCGv_i64 tcg_res;
4386 fpst = get_fpstatus_ptr();
4387 tcg_op = read_fp_dreg(s, rn);
4388 tcg_res = tcg_temp_new_i64();
4390 switch (opcode) {
4391 case 0x0: /* FMOV */
4392 tcg_gen_mov_i64(tcg_res, tcg_op);
4393 break;
4394 case 0x1: /* FABS */
4395 gen_helper_vfp_absd(tcg_res, tcg_op);
4396 break;
4397 case 0x2: /* FNEG */
4398 gen_helper_vfp_negd(tcg_res, tcg_op);
4399 break;
4400 case 0x3: /* FSQRT */
4401 gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
4402 break;
4403 case 0x8: /* FRINTN */
4404 case 0x9: /* FRINTP */
4405 case 0xa: /* FRINTM */
4406 case 0xb: /* FRINTZ */
4407 case 0xc: /* FRINTA */
4409 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4411 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4412 gen_helper_rintd(tcg_res, tcg_op, fpst);
4414 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4415 tcg_temp_free_i32(tcg_rmode);
4416 break;
4418 case 0xe: /* FRINTX */
4419 gen_helper_rintd_exact(tcg_res, tcg_op, fpst);
4420 break;
4421 case 0xf: /* FRINTI */
4422 gen_helper_rintd(tcg_res, tcg_op, fpst);
4423 break;
4424 default:
4425 abort();
4428 write_fp_dreg(s, rd, tcg_res);
4430 tcg_temp_free_ptr(fpst);
4431 tcg_temp_free_i64(tcg_op);
4432 tcg_temp_free_i64(tcg_res);
4435 static void handle_fp_fcvt(DisasContext *s, int opcode,
4436 int rd, int rn, int dtype, int ntype)
4438 switch (ntype) {
4439 case 0x0:
4441 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4442 if (dtype == 1) {
4443 /* Single to double */
4444 TCGv_i64 tcg_rd = tcg_temp_new_i64();
4445 gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
4446 write_fp_dreg(s, rd, tcg_rd);
4447 tcg_temp_free_i64(tcg_rd);
4448 } else {
4449 /* Single to half */
4450 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4451 gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, cpu_env);
4452 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4453 write_fp_sreg(s, rd, tcg_rd);
4454 tcg_temp_free_i32(tcg_rd);
4456 tcg_temp_free_i32(tcg_rn);
4457 break;
4459 case 0x1:
4461 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
4462 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4463 if (dtype == 0) {
4464 /* Double to single */
4465 gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
4466 } else {
4467 /* Double to half */
4468 gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, cpu_env);
4469 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4471 write_fp_sreg(s, rd, tcg_rd);
4472 tcg_temp_free_i32(tcg_rd);
4473 tcg_temp_free_i64(tcg_rn);
4474 break;
4476 case 0x3:
4478 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4479 tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
4480 if (dtype == 0) {
4481 /* Half to single */
4482 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4483 gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, cpu_env);
4484 write_fp_sreg(s, rd, tcg_rd);
4485 tcg_temp_free_i32(tcg_rd);
4486 } else {
4487 /* Half to double */
4488 TCGv_i64 tcg_rd = tcg_temp_new_i64();
4489 gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, cpu_env);
4490 write_fp_dreg(s, rd, tcg_rd);
4491 tcg_temp_free_i64(tcg_rd);
4493 tcg_temp_free_i32(tcg_rn);
4494 break;
4496 default:
4497 abort();
4501 /* C3.6.25 Floating point data-processing (1 source)
4502 * 31 30 29 28 24 23 22 21 20 15 14 10 9 5 4 0
4503 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4504 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 | Rn | Rd |
4505 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4507 static void disas_fp_1src(DisasContext *s, uint32_t insn)
4509 int type = extract32(insn, 22, 2);
4510 int opcode = extract32(insn, 15, 6);
4511 int rn = extract32(insn, 5, 5);
4512 int rd = extract32(insn, 0, 5);
4514 switch (opcode) {
4515 case 0x4: case 0x5: case 0x7:
4517 /* FCVT between half, single and double precision */
4518 int dtype = extract32(opcode, 0, 2);
4519 if (type == 2 || dtype == type) {
4520 unallocated_encoding(s);
4521 return;
4523 if (!fp_access_check(s)) {
4524 return;
4527 handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
4528 break;
4530 case 0x0 ... 0x3:
4531 case 0x8 ... 0xc:
4532 case 0xe ... 0xf:
4533 /* 32-to-32 and 64-to-64 ops */
4534 switch (type) {
4535 case 0:
4536 if (!fp_access_check(s)) {
4537 return;
4540 handle_fp_1src_single(s, opcode, rd, rn);
4541 break;
4542 case 1:
4543 if (!fp_access_check(s)) {
4544 return;
4547 handle_fp_1src_double(s, opcode, rd, rn);
4548 break;
4549 default:
4550 unallocated_encoding(s);
4552 break;
4553 default:
4554 unallocated_encoding(s);
4555 break;
4559 /* C3.6.26 Floating-point data-processing (2 source) - single precision */
4560 static void handle_fp_2src_single(DisasContext *s, int opcode,
4561 int rd, int rn, int rm)
4563 TCGv_i32 tcg_op1;
4564 TCGv_i32 tcg_op2;
4565 TCGv_i32 tcg_res;
4566 TCGv_ptr fpst;
4568 tcg_res = tcg_temp_new_i32();
4569 fpst = get_fpstatus_ptr();
4570 tcg_op1 = read_fp_sreg(s, rn);
4571 tcg_op2 = read_fp_sreg(s, rm);
4573 switch (opcode) {
4574 case 0x0: /* FMUL */
4575 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4576 break;
4577 case 0x1: /* FDIV */
4578 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
4579 break;
4580 case 0x2: /* FADD */
4581 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
4582 break;
4583 case 0x3: /* FSUB */
4584 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
4585 break;
4586 case 0x4: /* FMAX */
4587 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
4588 break;
4589 case 0x5: /* FMIN */
4590 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
4591 break;
4592 case 0x6: /* FMAXNM */
4593 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
4594 break;
4595 case 0x7: /* FMINNM */
4596 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
4597 break;
4598 case 0x8: /* FNMUL */
4599 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4600 gen_helper_vfp_negs(tcg_res, tcg_res);
4601 break;
4604 write_fp_sreg(s, rd, tcg_res);
4606 tcg_temp_free_ptr(fpst);
4607 tcg_temp_free_i32(tcg_op1);
4608 tcg_temp_free_i32(tcg_op2);
4609 tcg_temp_free_i32(tcg_res);
4612 /* C3.6.26 Floating-point data-processing (2 source) - double precision */
4613 static void handle_fp_2src_double(DisasContext *s, int opcode,
4614 int rd, int rn, int rm)
4616 TCGv_i64 tcg_op1;
4617 TCGv_i64 tcg_op2;
4618 TCGv_i64 tcg_res;
4619 TCGv_ptr fpst;
4621 tcg_res = tcg_temp_new_i64();
4622 fpst = get_fpstatus_ptr();
4623 tcg_op1 = read_fp_dreg(s, rn);
4624 tcg_op2 = read_fp_dreg(s, rm);
4626 switch (opcode) {
4627 case 0x0: /* FMUL */
4628 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4629 break;
4630 case 0x1: /* FDIV */
4631 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
4632 break;
4633 case 0x2: /* FADD */
4634 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
4635 break;
4636 case 0x3: /* FSUB */
4637 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
4638 break;
4639 case 0x4: /* FMAX */
4640 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
4641 break;
4642 case 0x5: /* FMIN */
4643 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
4644 break;
4645 case 0x6: /* FMAXNM */
4646 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4647 break;
4648 case 0x7: /* FMINNM */
4649 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4650 break;
4651 case 0x8: /* FNMUL */
4652 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4653 gen_helper_vfp_negd(tcg_res, tcg_res);
4654 break;
4657 write_fp_dreg(s, rd, tcg_res);
4659 tcg_temp_free_ptr(fpst);
4660 tcg_temp_free_i64(tcg_op1);
4661 tcg_temp_free_i64(tcg_op2);
4662 tcg_temp_free_i64(tcg_res);
4665 /* C3.6.26 Floating point data-processing (2 source)
4666 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
4667 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4668 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | opcode | 1 0 | Rn | Rd |
4669 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4671 static void disas_fp_2src(DisasContext *s, uint32_t insn)
4673 int type = extract32(insn, 22, 2);
4674 int rd = extract32(insn, 0, 5);
4675 int rn = extract32(insn, 5, 5);
4676 int rm = extract32(insn, 16, 5);
4677 int opcode = extract32(insn, 12, 4);
4679 if (opcode > 8) {
4680 unallocated_encoding(s);
4681 return;
4684 switch (type) {
4685 case 0:
4686 if (!fp_access_check(s)) {
4687 return;
4689 handle_fp_2src_single(s, opcode, rd, rn, rm);
4690 break;
4691 case 1:
4692 if (!fp_access_check(s)) {
4693 return;
4695 handle_fp_2src_double(s, opcode, rd, rn, rm);
4696 break;
4697 default:
4698 unallocated_encoding(s);
4702 /* C3.6.27 Floating-point data-processing (3 source) - single precision */
4703 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
4704 int rd, int rn, int rm, int ra)
4706 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
4707 TCGv_i32 tcg_res = tcg_temp_new_i32();
4708 TCGv_ptr fpst = get_fpstatus_ptr();
4710 tcg_op1 = read_fp_sreg(s, rn);
4711 tcg_op2 = read_fp_sreg(s, rm);
4712 tcg_op3 = read_fp_sreg(s, ra);
4714 /* These are fused multiply-add, and must be done as one
4715 * floating point operation with no rounding between the
4716 * multiplication and addition steps.
4717 * NB that doing the negations here as separate steps is
4718 * correct : an input NaN should come out with its sign bit
4719 * flipped if it is a negated-input.
4721 if (o1 == true) {
4722 gen_helper_vfp_negs(tcg_op3, tcg_op3);
4725 if (o0 != o1) {
4726 gen_helper_vfp_negs(tcg_op1, tcg_op1);
4729 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4731 write_fp_sreg(s, rd, tcg_res);
4733 tcg_temp_free_ptr(fpst);
4734 tcg_temp_free_i32(tcg_op1);
4735 tcg_temp_free_i32(tcg_op2);
4736 tcg_temp_free_i32(tcg_op3);
4737 tcg_temp_free_i32(tcg_res);
4740 /* C3.6.27 Floating-point data-processing (3 source) - double precision */
4741 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
4742 int rd, int rn, int rm, int ra)
4744 TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
4745 TCGv_i64 tcg_res = tcg_temp_new_i64();
4746 TCGv_ptr fpst = get_fpstatus_ptr();
4748 tcg_op1 = read_fp_dreg(s, rn);
4749 tcg_op2 = read_fp_dreg(s, rm);
4750 tcg_op3 = read_fp_dreg(s, ra);
4752 /* These are fused multiply-add, and must be done as one
4753 * floating point operation with no rounding between the
4754 * multiplication and addition steps.
4755 * NB that doing the negations here as separate steps is
4756 * correct : an input NaN should come out with its sign bit
4757 * flipped if it is a negated-input.
4759 if (o1 == true) {
4760 gen_helper_vfp_negd(tcg_op3, tcg_op3);
4763 if (o0 != o1) {
4764 gen_helper_vfp_negd(tcg_op1, tcg_op1);
4767 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4769 write_fp_dreg(s, rd, tcg_res);
4771 tcg_temp_free_ptr(fpst);
4772 tcg_temp_free_i64(tcg_op1);
4773 tcg_temp_free_i64(tcg_op2);
4774 tcg_temp_free_i64(tcg_op3);
4775 tcg_temp_free_i64(tcg_res);
4778 /* C3.6.27 Floating point data-processing (3 source)
4779 * 31 30 29 28 24 23 22 21 20 16 15 14 10 9 5 4 0
4780 * +---+---+---+-----------+------+----+------+----+------+------+------+
4781 * | M | 0 | S | 1 1 1 1 1 | type | o1 | Rm | o0 | Ra | Rn | Rd |
4782 * +---+---+---+-----------+------+----+------+----+------+------+------+
4784 static void disas_fp_3src(DisasContext *s, uint32_t insn)
4786 int type = extract32(insn, 22, 2);
4787 int rd = extract32(insn, 0, 5);
4788 int rn = extract32(insn, 5, 5);
4789 int ra = extract32(insn, 10, 5);
4790 int rm = extract32(insn, 16, 5);
4791 bool o0 = extract32(insn, 15, 1);
4792 bool o1 = extract32(insn, 21, 1);
4794 switch (type) {
4795 case 0:
4796 if (!fp_access_check(s)) {
4797 return;
4799 handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
4800 break;
4801 case 1:
4802 if (!fp_access_check(s)) {
4803 return;
4805 handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
4806 break;
4807 default:
4808 unallocated_encoding(s);
4812 /* C3.6.28 Floating point immediate
4813 * 31 30 29 28 24 23 22 21 20 13 12 10 9 5 4 0
4814 * +---+---+---+-----------+------+---+------------+-------+------+------+
4815 * | M | 0 | S | 1 1 1 1 0 | type | 1 | imm8 | 1 0 0 | imm5 | Rd |
4816 * +---+---+---+-----------+------+---+------------+-------+------+------+
4818 static void disas_fp_imm(DisasContext *s, uint32_t insn)
4820 int rd = extract32(insn, 0, 5);
4821 int imm8 = extract32(insn, 13, 8);
4822 int is_double = extract32(insn, 22, 2);
4823 uint64_t imm;
4824 TCGv_i64 tcg_res;
4826 if (is_double > 1) {
4827 unallocated_encoding(s);
4828 return;
4831 if (!fp_access_check(s)) {
4832 return;
4835 /* The imm8 encodes the sign bit, enough bits to represent
4836 * an exponent in the range 01....1xx to 10....0xx,
4837 * and the most significant 4 bits of the mantissa; see
4838 * VFPExpandImm() in the v8 ARM ARM.
4840 if (is_double) {
4841 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
4842 (extract32(imm8, 6, 1) ? 0x3fc0 : 0x4000) |
4843 extract32(imm8, 0, 6);
4844 imm <<= 48;
4845 } else {
4846 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
4847 (extract32(imm8, 6, 1) ? 0x3e00 : 0x4000) |
4848 (extract32(imm8, 0, 6) << 3);
4849 imm <<= 16;
4852 tcg_res = tcg_const_i64(imm);
4853 write_fp_dreg(s, rd, tcg_res);
4854 tcg_temp_free_i64(tcg_res);
4857 /* Handle floating point <=> fixed point conversions. Note that we can
4858 * also deal with fp <=> integer conversions as a special case (scale == 64)
4859 * OPTME: consider handling that special case specially or at least skipping
4860 * the call to scalbn in the helpers for zero shifts.
4862 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
4863 bool itof, int rmode, int scale, int sf, int type)
4865 bool is_signed = !(opcode & 1);
4866 bool is_double = type;
4867 TCGv_ptr tcg_fpstatus;
4868 TCGv_i32 tcg_shift;
4870 tcg_fpstatus = get_fpstatus_ptr();
4872 tcg_shift = tcg_const_i32(64 - scale);
4874 if (itof) {
4875 TCGv_i64 tcg_int = cpu_reg(s, rn);
4876 if (!sf) {
4877 TCGv_i64 tcg_extend = new_tmp_a64(s);
4879 if (is_signed) {
4880 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
4881 } else {
4882 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
4885 tcg_int = tcg_extend;
4888 if (is_double) {
4889 TCGv_i64 tcg_double = tcg_temp_new_i64();
4890 if (is_signed) {
4891 gen_helper_vfp_sqtod(tcg_double, tcg_int,
4892 tcg_shift, tcg_fpstatus);
4893 } else {
4894 gen_helper_vfp_uqtod(tcg_double, tcg_int,
4895 tcg_shift, tcg_fpstatus);
4897 write_fp_dreg(s, rd, tcg_double);
4898 tcg_temp_free_i64(tcg_double);
4899 } else {
4900 TCGv_i32 tcg_single = tcg_temp_new_i32();
4901 if (is_signed) {
4902 gen_helper_vfp_sqtos(tcg_single, tcg_int,
4903 tcg_shift, tcg_fpstatus);
4904 } else {
4905 gen_helper_vfp_uqtos(tcg_single, tcg_int,
4906 tcg_shift, tcg_fpstatus);
4908 write_fp_sreg(s, rd, tcg_single);
4909 tcg_temp_free_i32(tcg_single);
4911 } else {
4912 TCGv_i64 tcg_int = cpu_reg(s, rd);
4913 TCGv_i32 tcg_rmode;
4915 if (extract32(opcode, 2, 1)) {
4916 /* There are too many rounding modes to all fit into rmode,
4917 * so FCVTA[US] is a special case.
4919 rmode = FPROUNDING_TIEAWAY;
4922 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
4924 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4926 if (is_double) {
4927 TCGv_i64 tcg_double = read_fp_dreg(s, rn);
4928 if (is_signed) {
4929 if (!sf) {
4930 gen_helper_vfp_tosld(tcg_int, tcg_double,
4931 tcg_shift, tcg_fpstatus);
4932 } else {
4933 gen_helper_vfp_tosqd(tcg_int, tcg_double,
4934 tcg_shift, tcg_fpstatus);
4936 } else {
4937 if (!sf) {
4938 gen_helper_vfp_tould(tcg_int, tcg_double,
4939 tcg_shift, tcg_fpstatus);
4940 } else {
4941 gen_helper_vfp_touqd(tcg_int, tcg_double,
4942 tcg_shift, tcg_fpstatus);
4945 tcg_temp_free_i64(tcg_double);
4946 } else {
4947 TCGv_i32 tcg_single = read_fp_sreg(s, rn);
4948 if (sf) {
4949 if (is_signed) {
4950 gen_helper_vfp_tosqs(tcg_int, tcg_single,
4951 tcg_shift, tcg_fpstatus);
4952 } else {
4953 gen_helper_vfp_touqs(tcg_int, tcg_single,
4954 tcg_shift, tcg_fpstatus);
4956 } else {
4957 TCGv_i32 tcg_dest = tcg_temp_new_i32();
4958 if (is_signed) {
4959 gen_helper_vfp_tosls(tcg_dest, tcg_single,
4960 tcg_shift, tcg_fpstatus);
4961 } else {
4962 gen_helper_vfp_touls(tcg_dest, tcg_single,
4963 tcg_shift, tcg_fpstatus);
4965 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
4966 tcg_temp_free_i32(tcg_dest);
4968 tcg_temp_free_i32(tcg_single);
4971 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4972 tcg_temp_free_i32(tcg_rmode);
4974 if (!sf) {
4975 tcg_gen_ext32u_i64(tcg_int, tcg_int);
4979 tcg_temp_free_ptr(tcg_fpstatus);
4980 tcg_temp_free_i32(tcg_shift);
4983 /* C3.6.29 Floating point <-> fixed point conversions
4984 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
4985 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
4986 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale | Rn | Rd |
4987 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
4989 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
4991 int rd = extract32(insn, 0, 5);
4992 int rn = extract32(insn, 5, 5);
4993 int scale = extract32(insn, 10, 6);
4994 int opcode = extract32(insn, 16, 3);
4995 int rmode = extract32(insn, 19, 2);
4996 int type = extract32(insn, 22, 2);
4997 bool sbit = extract32(insn, 29, 1);
4998 bool sf = extract32(insn, 31, 1);
4999 bool itof;
5001 if (sbit || (type > 1)
5002 || (!sf && scale < 32)) {
5003 unallocated_encoding(s);
5004 return;
5007 switch ((rmode << 3) | opcode) {
5008 case 0x2: /* SCVTF */
5009 case 0x3: /* UCVTF */
5010 itof = true;
5011 break;
5012 case 0x18: /* FCVTZS */
5013 case 0x19: /* FCVTZU */
5014 itof = false;
5015 break;
5016 default:
5017 unallocated_encoding(s);
5018 return;
5021 if (!fp_access_check(s)) {
5022 return;
5025 handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
5028 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
5030 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
5031 * without conversion.
5034 if (itof) {
5035 TCGv_i64 tcg_rn = cpu_reg(s, rn);
5037 switch (type) {
5038 case 0:
5040 /* 32 bit */
5041 TCGv_i64 tmp = tcg_temp_new_i64();
5042 tcg_gen_ext32u_i64(tmp, tcg_rn);
5043 tcg_gen_st_i64(tmp, cpu_env, fp_reg_offset(s, rd, MO_64));
5044 tcg_gen_movi_i64(tmp, 0);
5045 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(s, rd));
5046 tcg_temp_free_i64(tmp);
5047 break;
5049 case 1:
5051 /* 64 bit */
5052 TCGv_i64 tmp = tcg_const_i64(0);
5053 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_offset(s, rd, MO_64));
5054 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(s, rd));
5055 tcg_temp_free_i64(tmp);
5056 break;
5058 case 2:
5059 /* 64 bit to top half. */
5060 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
5061 break;
5063 } else {
5064 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5066 switch (type) {
5067 case 0:
5068 /* 32 bit */
5069 tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
5070 break;
5071 case 1:
5072 /* 64 bit */
5073 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
5074 break;
5075 case 2:
5076 /* 64 bits from top half */
5077 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
5078 break;
5083 /* C3.6.30 Floating point <-> integer conversions
5084 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
5085 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
5086 * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
5087 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
5089 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
5091 int rd = extract32(insn, 0, 5);
5092 int rn = extract32(insn, 5, 5);
5093 int opcode = extract32(insn, 16, 3);
5094 int rmode = extract32(insn, 19, 2);
5095 int type = extract32(insn, 22, 2);
5096 bool sbit = extract32(insn, 29, 1);
5097 bool sf = extract32(insn, 31, 1);
5099 if (sbit) {
5100 unallocated_encoding(s);
5101 return;
5104 if (opcode > 5) {
5105 /* FMOV */
5106 bool itof = opcode & 1;
5108 if (rmode >= 2) {
5109 unallocated_encoding(s);
5110 return;
5113 switch (sf << 3 | type << 1 | rmode) {
5114 case 0x0: /* 32 bit */
5115 case 0xa: /* 64 bit */
5116 case 0xd: /* 64 bit to top half of quad */
5117 break;
5118 default:
5119 /* all other sf/type/rmode combinations are invalid */
5120 unallocated_encoding(s);
5121 break;
5124 if (!fp_access_check(s)) {
5125 return;
5127 handle_fmov(s, rd, rn, type, itof);
5128 } else {
5129 /* actual FP conversions */
5130 bool itof = extract32(opcode, 1, 1);
5132 if (type > 1 || (rmode != 0 && opcode > 1)) {
5133 unallocated_encoding(s);
5134 return;
5137 if (!fp_access_check(s)) {
5138 return;
5140 handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
5144 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
5145 * 31 30 29 28 25 24 0
5146 * +---+---+---+---------+-----------------------------+
5147 * | | 0 | | 1 1 1 1 | |
5148 * +---+---+---+---------+-----------------------------+
5150 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
5152 if (extract32(insn, 24, 1)) {
5153 /* Floating point data-processing (3 source) */
5154 disas_fp_3src(s, insn);
5155 } else if (extract32(insn, 21, 1) == 0) {
5156 /* Floating point to fixed point conversions */
5157 disas_fp_fixed_conv(s, insn);
5158 } else {
5159 switch (extract32(insn, 10, 2)) {
5160 case 1:
5161 /* Floating point conditional compare */
5162 disas_fp_ccomp(s, insn);
5163 break;
5164 case 2:
5165 /* Floating point data-processing (2 source) */
5166 disas_fp_2src(s, insn);
5167 break;
5168 case 3:
5169 /* Floating point conditional select */
5170 disas_fp_csel(s, insn);
5171 break;
5172 case 0:
5173 switch (ctz32(extract32(insn, 12, 4))) {
5174 case 0: /* [15:12] == xxx1 */
5175 /* Floating point immediate */
5176 disas_fp_imm(s, insn);
5177 break;
5178 case 1: /* [15:12] == xx10 */
5179 /* Floating point compare */
5180 disas_fp_compare(s, insn);
5181 break;
5182 case 2: /* [15:12] == x100 */
5183 /* Floating point data-processing (1 source) */
5184 disas_fp_1src(s, insn);
5185 break;
5186 case 3: /* [15:12] == 1000 */
5187 unallocated_encoding(s);
5188 break;
5189 default: /* [15:12] == 0000 */
5190 /* Floating point <-> integer conversions */
5191 disas_fp_int_conv(s, insn);
5192 break;
5194 break;
5199 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
5200 int pos)
5202 /* Extract 64 bits from the middle of two concatenated 64 bit
5203 * vector register slices left:right. The extracted bits start
5204 * at 'pos' bits into the right (least significant) side.
5205 * We return the result in tcg_right, and guarantee not to
5206 * trash tcg_left.
5208 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5209 assert(pos > 0 && pos < 64);
5211 tcg_gen_shri_i64(tcg_right, tcg_right, pos);
5212 tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
5213 tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
5215 tcg_temp_free_i64(tcg_tmp);
5218 /* C3.6.1 EXT
5219 * 31 30 29 24 23 22 21 20 16 15 14 11 10 9 5 4 0
5220 * +---+---+-------------+-----+---+------+---+------+---+------+------+
5221 * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 | Rm | 0 | imm4 | 0 | Rn | Rd |
5222 * +---+---+-------------+-----+---+------+---+------+---+------+------+
5224 static void disas_simd_ext(DisasContext *s, uint32_t insn)
5226 int is_q = extract32(insn, 30, 1);
5227 int op2 = extract32(insn, 22, 2);
5228 int imm4 = extract32(insn, 11, 4);
5229 int rm = extract32(insn, 16, 5);
5230 int rn = extract32(insn, 5, 5);
5231 int rd = extract32(insn, 0, 5);
5232 int pos = imm4 << 3;
5233 TCGv_i64 tcg_resl, tcg_resh;
5235 if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
5236 unallocated_encoding(s);
5237 return;
5240 if (!fp_access_check(s)) {
5241 return;
5244 tcg_resh = tcg_temp_new_i64();
5245 tcg_resl = tcg_temp_new_i64();
5247 /* Vd gets bits starting at pos bits into Vm:Vn. This is
5248 * either extracting 128 bits from a 128:128 concatenation, or
5249 * extracting 64 bits from a 64:64 concatenation.
5251 if (!is_q) {
5252 read_vec_element(s, tcg_resl, rn, 0, MO_64);
5253 if (pos != 0) {
5254 read_vec_element(s, tcg_resh, rm, 0, MO_64);
5255 do_ext64(s, tcg_resh, tcg_resl, pos);
5257 tcg_gen_movi_i64(tcg_resh, 0);
5258 } else {
5259 TCGv_i64 tcg_hh;
5260 typedef struct {
5261 int reg;
5262 int elt;
5263 } EltPosns;
5264 EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
5265 EltPosns *elt = eltposns;
5267 if (pos >= 64) {
5268 elt++;
5269 pos -= 64;
5272 read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
5273 elt++;
5274 read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
5275 elt++;
5276 if (pos != 0) {
5277 do_ext64(s, tcg_resh, tcg_resl, pos);
5278 tcg_hh = tcg_temp_new_i64();
5279 read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
5280 do_ext64(s, tcg_hh, tcg_resh, pos);
5281 tcg_temp_free_i64(tcg_hh);
5285 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5286 tcg_temp_free_i64(tcg_resl);
5287 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5288 tcg_temp_free_i64(tcg_resh);
5291 /* C3.6.2 TBL/TBX
5292 * 31 30 29 24 23 22 21 20 16 15 14 13 12 11 10 9 5 4 0
5293 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
5294 * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 | Rm | 0 | len | op | 0 0 | Rn | Rd |
5295 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
5297 static void disas_simd_tb(DisasContext *s, uint32_t insn)
5299 int op2 = extract32(insn, 22, 2);
5300 int is_q = extract32(insn, 30, 1);
5301 int rm = extract32(insn, 16, 5);
5302 int rn = extract32(insn, 5, 5);
5303 int rd = extract32(insn, 0, 5);
5304 int is_tblx = extract32(insn, 12, 1);
5305 int len = extract32(insn, 13, 2);
5306 TCGv_i64 tcg_resl, tcg_resh, tcg_idx;
5307 TCGv_i32 tcg_regno, tcg_numregs;
5309 if (op2 != 0) {
5310 unallocated_encoding(s);
5311 return;
5314 if (!fp_access_check(s)) {
5315 return;
5318 /* This does a table lookup: for every byte element in the input
5319 * we index into a table formed from up to four vector registers,
5320 * and then the output is the result of the lookups. Our helper
5321 * function does the lookup operation for a single 64 bit part of
5322 * the input.
5324 tcg_resl = tcg_temp_new_i64();
5325 tcg_resh = tcg_temp_new_i64();
5327 if (is_tblx) {
5328 read_vec_element(s, tcg_resl, rd, 0, MO_64);
5329 } else {
5330 tcg_gen_movi_i64(tcg_resl, 0);
5332 if (is_tblx && is_q) {
5333 read_vec_element(s, tcg_resh, rd, 1, MO_64);
5334 } else {
5335 tcg_gen_movi_i64(tcg_resh, 0);
5338 tcg_idx = tcg_temp_new_i64();
5339 tcg_regno = tcg_const_i32(rn);
5340 tcg_numregs = tcg_const_i32(len + 1);
5341 read_vec_element(s, tcg_idx, rm, 0, MO_64);
5342 gen_helper_simd_tbl(tcg_resl, cpu_env, tcg_resl, tcg_idx,
5343 tcg_regno, tcg_numregs);
5344 if (is_q) {
5345 read_vec_element(s, tcg_idx, rm, 1, MO_64);
5346 gen_helper_simd_tbl(tcg_resh, cpu_env, tcg_resh, tcg_idx,
5347 tcg_regno, tcg_numregs);
5349 tcg_temp_free_i64(tcg_idx);
5350 tcg_temp_free_i32(tcg_regno);
5351 tcg_temp_free_i32(tcg_numregs);
5353 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5354 tcg_temp_free_i64(tcg_resl);
5355 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5356 tcg_temp_free_i64(tcg_resh);
5359 /* C3.6.3 ZIP/UZP/TRN
5360 * 31 30 29 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
5361 * +---+---+-------------+------+---+------+---+------------------+------+
5362 * | 0 | Q | 0 0 1 1 1 0 | size | 0 | Rm | 0 | opc | 1 0 | Rn | Rd |
5363 * +---+---+-------------+------+---+------+---+------------------+------+
5365 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
5367 int rd = extract32(insn, 0, 5);
5368 int rn = extract32(insn, 5, 5);
5369 int rm = extract32(insn, 16, 5);
5370 int size = extract32(insn, 22, 2);
5371 /* opc field bits [1:0] indicate ZIP/UZP/TRN;
5372 * bit 2 indicates 1 vs 2 variant of the insn.
5374 int opcode = extract32(insn, 12, 2);
5375 bool part = extract32(insn, 14, 1);
5376 bool is_q = extract32(insn, 30, 1);
5377 int esize = 8 << size;
5378 int i, ofs;
5379 int datasize = is_q ? 128 : 64;
5380 int elements = datasize / esize;
5381 TCGv_i64 tcg_res, tcg_resl, tcg_resh;
5383 if (opcode == 0 || (size == 3 && !is_q)) {
5384 unallocated_encoding(s);
5385 return;
5388 if (!fp_access_check(s)) {
5389 return;
5392 tcg_resl = tcg_const_i64(0);
5393 tcg_resh = tcg_const_i64(0);
5394 tcg_res = tcg_temp_new_i64();
5396 for (i = 0; i < elements; i++) {
5397 switch (opcode) {
5398 case 1: /* UZP1/2 */
5400 int midpoint = elements / 2;
5401 if (i < midpoint) {
5402 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
5403 } else {
5404 read_vec_element(s, tcg_res, rm,
5405 2 * (i - midpoint) + part, size);
5407 break;
5409 case 2: /* TRN1/2 */
5410 if (i & 1) {
5411 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
5412 } else {
5413 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
5415 break;
5416 case 3: /* ZIP1/2 */
5418 int base = part * elements / 2;
5419 if (i & 1) {
5420 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
5421 } else {
5422 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
5424 break;
5426 default:
5427 g_assert_not_reached();
5430 ofs = i * esize;
5431 if (ofs < 64) {
5432 tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
5433 tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
5434 } else {
5435 tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
5436 tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
5440 tcg_temp_free_i64(tcg_res);
5442 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5443 tcg_temp_free_i64(tcg_resl);
5444 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5445 tcg_temp_free_i64(tcg_resh);
5448 static void do_minmaxop(DisasContext *s, TCGv_i32 tcg_elt1, TCGv_i32 tcg_elt2,
5449 int opc, bool is_min, TCGv_ptr fpst)
5451 /* Helper function for disas_simd_across_lanes: do a single precision
5452 * min/max operation on the specified two inputs,
5453 * and return the result in tcg_elt1.
5455 if (opc == 0xc) {
5456 if (is_min) {
5457 gen_helper_vfp_minnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5458 } else {
5459 gen_helper_vfp_maxnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5461 } else {
5462 assert(opc == 0xf);
5463 if (is_min) {
5464 gen_helper_vfp_mins(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5465 } else {
5466 gen_helper_vfp_maxs(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5471 /* C3.6.4 AdvSIMD across lanes
5472 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
5473 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5474 * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
5475 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5477 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
5479 int rd = extract32(insn, 0, 5);
5480 int rn = extract32(insn, 5, 5);
5481 int size = extract32(insn, 22, 2);
5482 int opcode = extract32(insn, 12, 5);
5483 bool is_q = extract32(insn, 30, 1);
5484 bool is_u = extract32(insn, 29, 1);
5485 bool is_fp = false;
5486 bool is_min = false;
5487 int esize;
5488 int elements;
5489 int i;
5490 TCGv_i64 tcg_res, tcg_elt;
5492 switch (opcode) {
5493 case 0x1b: /* ADDV */
5494 if (is_u) {
5495 unallocated_encoding(s);
5496 return;
5498 /* fall through */
5499 case 0x3: /* SADDLV, UADDLV */
5500 case 0xa: /* SMAXV, UMAXV */
5501 case 0x1a: /* SMINV, UMINV */
5502 if (size == 3 || (size == 2 && !is_q)) {
5503 unallocated_encoding(s);
5504 return;
5506 break;
5507 case 0xc: /* FMAXNMV, FMINNMV */
5508 case 0xf: /* FMAXV, FMINV */
5509 if (!is_u || !is_q || extract32(size, 0, 1)) {
5510 unallocated_encoding(s);
5511 return;
5513 /* Bit 1 of size field encodes min vs max, and actual size is always
5514 * 32 bits: adjust the size variable so following code can rely on it
5516 is_min = extract32(size, 1, 1);
5517 is_fp = true;
5518 size = 2;
5519 break;
5520 default:
5521 unallocated_encoding(s);
5522 return;
5525 if (!fp_access_check(s)) {
5526 return;
5529 esize = 8 << size;
5530 elements = (is_q ? 128 : 64) / esize;
5532 tcg_res = tcg_temp_new_i64();
5533 tcg_elt = tcg_temp_new_i64();
5535 /* These instructions operate across all lanes of a vector
5536 * to produce a single result. We can guarantee that a 64
5537 * bit intermediate is sufficient:
5538 * + for [US]ADDLV the maximum element size is 32 bits, and
5539 * the result type is 64 bits
5540 * + for FMAX*V, FMIN*V, ADDV the intermediate type is the
5541 * same as the element size, which is 32 bits at most
5542 * For the integer operations we can choose to work at 64
5543 * or 32 bits and truncate at the end; for simplicity
5544 * we use 64 bits always. The floating point
5545 * ops do require 32 bit intermediates, though.
5547 if (!is_fp) {
5548 read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
5550 for (i = 1; i < elements; i++) {
5551 read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
5553 switch (opcode) {
5554 case 0x03: /* SADDLV / UADDLV */
5555 case 0x1b: /* ADDV */
5556 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
5557 break;
5558 case 0x0a: /* SMAXV / UMAXV */
5559 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
5560 tcg_res,
5561 tcg_res, tcg_elt, tcg_res, tcg_elt);
5562 break;
5563 case 0x1a: /* SMINV / UMINV */
5564 tcg_gen_movcond_i64(is_u ? TCG_COND_LEU : TCG_COND_LE,
5565 tcg_res,
5566 tcg_res, tcg_elt, tcg_res, tcg_elt);
5567 break;
5568 break;
5569 default:
5570 g_assert_not_reached();
5574 } else {
5575 /* Floating point ops which work on 32 bit (single) intermediates.
5576 * Note that correct NaN propagation requires that we do these
5577 * operations in exactly the order specified by the pseudocode.
5579 TCGv_i32 tcg_elt1 = tcg_temp_new_i32();
5580 TCGv_i32 tcg_elt2 = tcg_temp_new_i32();
5581 TCGv_i32 tcg_elt3 = tcg_temp_new_i32();
5582 TCGv_ptr fpst = get_fpstatus_ptr();
5584 assert(esize == 32);
5585 assert(elements == 4);
5587 read_vec_element(s, tcg_elt, rn, 0, MO_32);
5588 tcg_gen_extrl_i64_i32(tcg_elt1, tcg_elt);
5589 read_vec_element(s, tcg_elt, rn, 1, MO_32);
5590 tcg_gen_extrl_i64_i32(tcg_elt2, tcg_elt);
5592 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5594 read_vec_element(s, tcg_elt, rn, 2, MO_32);
5595 tcg_gen_extrl_i64_i32(tcg_elt2, tcg_elt);
5596 read_vec_element(s, tcg_elt, rn, 3, MO_32);
5597 tcg_gen_extrl_i64_i32(tcg_elt3, tcg_elt);
5599 do_minmaxop(s, tcg_elt2, tcg_elt3, opcode, is_min, fpst);
5601 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5603 tcg_gen_extu_i32_i64(tcg_res, tcg_elt1);
5604 tcg_temp_free_i32(tcg_elt1);
5605 tcg_temp_free_i32(tcg_elt2);
5606 tcg_temp_free_i32(tcg_elt3);
5607 tcg_temp_free_ptr(fpst);
5610 tcg_temp_free_i64(tcg_elt);
5612 /* Now truncate the result to the width required for the final output */
5613 if (opcode == 0x03) {
5614 /* SADDLV, UADDLV: result is 2*esize */
5615 size++;
5618 switch (size) {
5619 case 0:
5620 tcg_gen_ext8u_i64(tcg_res, tcg_res);
5621 break;
5622 case 1:
5623 tcg_gen_ext16u_i64(tcg_res, tcg_res);
5624 break;
5625 case 2:
5626 tcg_gen_ext32u_i64(tcg_res, tcg_res);
5627 break;
5628 case 3:
5629 break;
5630 default:
5631 g_assert_not_reached();
5634 write_fp_dreg(s, rd, tcg_res);
5635 tcg_temp_free_i64(tcg_res);
5638 /* C6.3.31 DUP (Element, Vector)
5640 * 31 30 29 21 20 16 15 10 9 5 4 0
5641 * +---+---+-------------------+--------+-------------+------+------+
5642 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
5643 * +---+---+-------------------+--------+-------------+------+------+
5645 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5647 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
5648 int imm5)
5650 int size = ctz32(imm5);
5651 int esize = 8 << size;
5652 int elements = (is_q ? 128 : 64) / esize;
5653 int index, i;
5654 TCGv_i64 tmp;
5656 if (size > 3 || (size == 3 && !is_q)) {
5657 unallocated_encoding(s);
5658 return;
5661 if (!fp_access_check(s)) {
5662 return;
5665 index = imm5 >> (size + 1);
5667 tmp = tcg_temp_new_i64();
5668 read_vec_element(s, tmp, rn, index, size);
5670 for (i = 0; i < elements; i++) {
5671 write_vec_element(s, tmp, rd, i, size);
5674 if (!is_q) {
5675 clear_vec_high(s, rd);
5678 tcg_temp_free_i64(tmp);
5681 /* C6.3.31 DUP (element, scalar)
5682 * 31 21 20 16 15 10 9 5 4 0
5683 * +-----------------------+--------+-------------+------+------+
5684 * | 0 1 0 1 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
5685 * +-----------------------+--------+-------------+------+------+
5687 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
5688 int imm5)
5690 int size = ctz32(imm5);
5691 int index;
5692 TCGv_i64 tmp;
5694 if (size > 3) {
5695 unallocated_encoding(s);
5696 return;
5699 if (!fp_access_check(s)) {
5700 return;
5703 index = imm5 >> (size + 1);
5705 /* This instruction just extracts the specified element and
5706 * zero-extends it into the bottom of the destination register.
5708 tmp = tcg_temp_new_i64();
5709 read_vec_element(s, tmp, rn, index, size);
5710 write_fp_dreg(s, rd, tmp);
5711 tcg_temp_free_i64(tmp);
5714 /* C6.3.32 DUP (General)
5716 * 31 30 29 21 20 16 15 10 9 5 4 0
5717 * +---+---+-------------------+--------+-------------+------+------+
5718 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 1 1 | Rn | Rd |
5719 * +---+---+-------------------+--------+-------------+------+------+
5721 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5723 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
5724 int imm5)
5726 int size = ctz32(imm5);
5727 int esize = 8 << size;
5728 int elements = (is_q ? 128 : 64)/esize;
5729 int i = 0;
5731 if (size > 3 || ((size == 3) && !is_q)) {
5732 unallocated_encoding(s);
5733 return;
5736 if (!fp_access_check(s)) {
5737 return;
5740 for (i = 0; i < elements; i++) {
5741 write_vec_element(s, cpu_reg(s, rn), rd, i, size);
5743 if (!is_q) {
5744 clear_vec_high(s, rd);
5748 /* C6.3.150 INS (Element)
5750 * 31 21 20 16 15 14 11 10 9 5 4 0
5751 * +-----------------------+--------+------------+---+------+------+
5752 * | 0 1 1 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5753 * +-----------------------+--------+------------+---+------+------+
5755 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5756 * index: encoded in imm5<4:size+1>
5758 static void handle_simd_inse(DisasContext *s, int rd, int rn,
5759 int imm4, int imm5)
5761 int size = ctz32(imm5);
5762 int src_index, dst_index;
5763 TCGv_i64 tmp;
5765 if (size > 3) {
5766 unallocated_encoding(s);
5767 return;
5770 if (!fp_access_check(s)) {
5771 return;
5774 dst_index = extract32(imm5, 1+size, 5);
5775 src_index = extract32(imm4, size, 4);
5777 tmp = tcg_temp_new_i64();
5779 read_vec_element(s, tmp, rn, src_index, size);
5780 write_vec_element(s, tmp, rd, dst_index, size);
5782 tcg_temp_free_i64(tmp);
5786 /* C6.3.151 INS (General)
5788 * 31 21 20 16 15 10 9 5 4 0
5789 * +-----------------------+--------+-------------+------+------+
5790 * | 0 1 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 1 1 1 | Rn | Rd |
5791 * +-----------------------+--------+-------------+------+------+
5793 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5794 * index: encoded in imm5<4:size+1>
5796 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
5798 int size = ctz32(imm5);
5799 int idx;
5801 if (size > 3) {
5802 unallocated_encoding(s);
5803 return;
5806 if (!fp_access_check(s)) {
5807 return;
5810 idx = extract32(imm5, 1 + size, 4 - size);
5811 write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
5815 * C6.3.321 UMOV (General)
5816 * C6.3.237 SMOV (General)
5818 * 31 30 29 21 20 16 15 12 10 9 5 4 0
5819 * +---+---+-------------------+--------+-------------+------+------+
5820 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 1 U 1 1 | Rn | Rd |
5821 * +---+---+-------------------+--------+-------------+------+------+
5823 * U: unsigned when set
5824 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5826 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
5827 int rn, int rd, int imm5)
5829 int size = ctz32(imm5);
5830 int element;
5831 TCGv_i64 tcg_rd;
5833 /* Check for UnallocatedEncodings */
5834 if (is_signed) {
5835 if (size > 2 || (size == 2 && !is_q)) {
5836 unallocated_encoding(s);
5837 return;
5839 } else {
5840 if (size > 3
5841 || (size < 3 && is_q)
5842 || (size == 3 && !is_q)) {
5843 unallocated_encoding(s);
5844 return;
5848 if (!fp_access_check(s)) {
5849 return;
5852 element = extract32(imm5, 1+size, 4);
5854 tcg_rd = cpu_reg(s, rd);
5855 read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
5856 if (is_signed && !is_q) {
5857 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5861 /* C3.6.5 AdvSIMD copy
5862 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
5863 * +---+---+----+-----------------+------+---+------+---+------+------+
5864 * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5865 * +---+---+----+-----------------+------+---+------+---+------+------+
5867 static void disas_simd_copy(DisasContext *s, uint32_t insn)
5869 int rd = extract32(insn, 0, 5);
5870 int rn = extract32(insn, 5, 5);
5871 int imm4 = extract32(insn, 11, 4);
5872 int op = extract32(insn, 29, 1);
5873 int is_q = extract32(insn, 30, 1);
5874 int imm5 = extract32(insn, 16, 5);
5876 if (op) {
5877 if (is_q) {
5878 /* INS (element) */
5879 handle_simd_inse(s, rd, rn, imm4, imm5);
5880 } else {
5881 unallocated_encoding(s);
5883 } else {
5884 switch (imm4) {
5885 case 0:
5886 /* DUP (element - vector) */
5887 handle_simd_dupe(s, is_q, rd, rn, imm5);
5888 break;
5889 case 1:
5890 /* DUP (general) */
5891 handle_simd_dupg(s, is_q, rd, rn, imm5);
5892 break;
5893 case 3:
5894 if (is_q) {
5895 /* INS (general) */
5896 handle_simd_insg(s, rd, rn, imm5);
5897 } else {
5898 unallocated_encoding(s);
5900 break;
5901 case 5:
5902 case 7:
5903 /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
5904 handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
5905 break;
5906 default:
5907 unallocated_encoding(s);
5908 break;
5913 /* C3.6.6 AdvSIMD modified immediate
5914 * 31 30 29 28 19 18 16 15 12 11 10 9 5 4 0
5915 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
5916 * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh | Rd |
5917 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
5919 * There are a number of operations that can be carried out here:
5920 * MOVI - move (shifted) imm into register
5921 * MVNI - move inverted (shifted) imm into register
5922 * ORR - bitwise OR of (shifted) imm with register
5923 * BIC - bitwise clear of (shifted) imm with register
5925 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
5927 int rd = extract32(insn, 0, 5);
5928 int cmode = extract32(insn, 12, 4);
5929 int cmode_3_1 = extract32(cmode, 1, 3);
5930 int cmode_0 = extract32(cmode, 0, 1);
5931 int o2 = extract32(insn, 11, 1);
5932 uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
5933 bool is_neg = extract32(insn, 29, 1);
5934 bool is_q = extract32(insn, 30, 1);
5935 uint64_t imm = 0;
5936 TCGv_i64 tcg_rd, tcg_imm;
5937 int i;
5939 if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
5940 unallocated_encoding(s);
5941 return;
5944 if (!fp_access_check(s)) {
5945 return;
5948 /* See AdvSIMDExpandImm() in ARM ARM */
5949 switch (cmode_3_1) {
5950 case 0: /* Replicate(Zeros(24):imm8, 2) */
5951 case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
5952 case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
5953 case 3: /* Replicate(imm8:Zeros(24), 2) */
5955 int shift = cmode_3_1 * 8;
5956 imm = bitfield_replicate(abcdefgh << shift, 32);
5957 break;
5959 case 4: /* Replicate(Zeros(8):imm8, 4) */
5960 case 5: /* Replicate(imm8:Zeros(8), 4) */
5962 int shift = (cmode_3_1 & 0x1) * 8;
5963 imm = bitfield_replicate(abcdefgh << shift, 16);
5964 break;
5966 case 6:
5967 if (cmode_0) {
5968 /* Replicate(Zeros(8):imm8:Ones(16), 2) */
5969 imm = (abcdefgh << 16) | 0xffff;
5970 } else {
5971 /* Replicate(Zeros(16):imm8:Ones(8), 2) */
5972 imm = (abcdefgh << 8) | 0xff;
5974 imm = bitfield_replicate(imm, 32);
5975 break;
5976 case 7:
5977 if (!cmode_0 && !is_neg) {
5978 imm = bitfield_replicate(abcdefgh, 8);
5979 } else if (!cmode_0 && is_neg) {
5980 int i;
5981 imm = 0;
5982 for (i = 0; i < 8; i++) {
5983 if ((abcdefgh) & (1 << i)) {
5984 imm |= 0xffULL << (i * 8);
5987 } else if (cmode_0) {
5988 if (is_neg) {
5989 imm = (abcdefgh & 0x3f) << 48;
5990 if (abcdefgh & 0x80) {
5991 imm |= 0x8000000000000000ULL;
5993 if (abcdefgh & 0x40) {
5994 imm |= 0x3fc0000000000000ULL;
5995 } else {
5996 imm |= 0x4000000000000000ULL;
5998 } else {
5999 imm = (abcdefgh & 0x3f) << 19;
6000 if (abcdefgh & 0x80) {
6001 imm |= 0x80000000;
6003 if (abcdefgh & 0x40) {
6004 imm |= 0x3e000000;
6005 } else {
6006 imm |= 0x40000000;
6008 imm |= (imm << 32);
6011 break;
6014 if (cmode_3_1 != 7 && is_neg) {
6015 imm = ~imm;
6018 tcg_imm = tcg_const_i64(imm);
6019 tcg_rd = new_tmp_a64(s);
6021 for (i = 0; i < 2; i++) {
6022 int foffs = i ? fp_reg_hi_offset(s, rd) : fp_reg_offset(s, rd, MO_64);
6024 if (i == 1 && !is_q) {
6025 /* non-quad ops clear high half of vector */
6026 tcg_gen_movi_i64(tcg_rd, 0);
6027 } else if ((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9) {
6028 tcg_gen_ld_i64(tcg_rd, cpu_env, foffs);
6029 if (is_neg) {
6030 /* AND (BIC) */
6031 tcg_gen_and_i64(tcg_rd, tcg_rd, tcg_imm);
6032 } else {
6033 /* ORR */
6034 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_imm);
6036 } else {
6037 /* MOVI */
6038 tcg_gen_mov_i64(tcg_rd, tcg_imm);
6040 tcg_gen_st_i64(tcg_rd, cpu_env, foffs);
6043 tcg_temp_free_i64(tcg_imm);
6046 /* C3.6.7 AdvSIMD scalar copy
6047 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
6048 * +-----+----+-----------------+------+---+------+---+------+------+
6049 * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
6050 * +-----+----+-----------------+------+---+------+---+------+------+
6052 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
6054 int rd = extract32(insn, 0, 5);
6055 int rn = extract32(insn, 5, 5);
6056 int imm4 = extract32(insn, 11, 4);
6057 int imm5 = extract32(insn, 16, 5);
6058 int op = extract32(insn, 29, 1);
6060 if (op != 0 || imm4 != 0) {
6061 unallocated_encoding(s);
6062 return;
6065 /* DUP (element, scalar) */
6066 handle_simd_dupes(s, rd, rn, imm5);
6069 /* C3.6.8 AdvSIMD scalar pairwise
6070 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
6071 * +-----+---+-----------+------+-----------+--------+-----+------+------+
6072 * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
6073 * +-----+---+-----------+------+-----------+--------+-----+------+------+
6075 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
6077 int u = extract32(insn, 29, 1);
6078 int size = extract32(insn, 22, 2);
6079 int opcode = extract32(insn, 12, 5);
6080 int rn = extract32(insn, 5, 5);
6081 int rd = extract32(insn, 0, 5);
6082 TCGv_ptr fpst;
6084 /* For some ops (the FP ones), size[1] is part of the encoding.
6085 * For ADDP strictly it is not but size[1] is always 1 for valid
6086 * encodings.
6088 opcode |= (extract32(size, 1, 1) << 5);
6090 switch (opcode) {
6091 case 0x3b: /* ADDP */
6092 if (u || size != 3) {
6093 unallocated_encoding(s);
6094 return;
6096 if (!fp_access_check(s)) {
6097 return;
6100 TCGV_UNUSED_PTR(fpst);
6101 break;
6102 case 0xc: /* FMAXNMP */
6103 case 0xd: /* FADDP */
6104 case 0xf: /* FMAXP */
6105 case 0x2c: /* FMINNMP */
6106 case 0x2f: /* FMINP */
6107 /* FP op, size[0] is 32 or 64 bit */
6108 if (!u) {
6109 unallocated_encoding(s);
6110 return;
6112 if (!fp_access_check(s)) {
6113 return;
6116 size = extract32(size, 0, 1) ? 3 : 2;
6117 fpst = get_fpstatus_ptr();
6118 break;
6119 default:
6120 unallocated_encoding(s);
6121 return;
6124 if (size == 3) {
6125 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6126 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6127 TCGv_i64 tcg_res = tcg_temp_new_i64();
6129 read_vec_element(s, tcg_op1, rn, 0, MO_64);
6130 read_vec_element(s, tcg_op2, rn, 1, MO_64);
6132 switch (opcode) {
6133 case 0x3b: /* ADDP */
6134 tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
6135 break;
6136 case 0xc: /* FMAXNMP */
6137 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6138 break;
6139 case 0xd: /* FADDP */
6140 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6141 break;
6142 case 0xf: /* FMAXP */
6143 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6144 break;
6145 case 0x2c: /* FMINNMP */
6146 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6147 break;
6148 case 0x2f: /* FMINP */
6149 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6150 break;
6151 default:
6152 g_assert_not_reached();
6155 write_fp_dreg(s, rd, tcg_res);
6157 tcg_temp_free_i64(tcg_op1);
6158 tcg_temp_free_i64(tcg_op2);
6159 tcg_temp_free_i64(tcg_res);
6160 } else {
6161 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6162 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6163 TCGv_i32 tcg_res = tcg_temp_new_i32();
6165 read_vec_element_i32(s, tcg_op1, rn, 0, MO_32);
6166 read_vec_element_i32(s, tcg_op2, rn, 1, MO_32);
6168 switch (opcode) {
6169 case 0xc: /* FMAXNMP */
6170 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6171 break;
6172 case 0xd: /* FADDP */
6173 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6174 break;
6175 case 0xf: /* FMAXP */
6176 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6177 break;
6178 case 0x2c: /* FMINNMP */
6179 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6180 break;
6181 case 0x2f: /* FMINP */
6182 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6183 break;
6184 default:
6185 g_assert_not_reached();
6188 write_fp_sreg(s, rd, tcg_res);
6190 tcg_temp_free_i32(tcg_op1);
6191 tcg_temp_free_i32(tcg_op2);
6192 tcg_temp_free_i32(tcg_res);
6195 if (!TCGV_IS_UNUSED_PTR(fpst)) {
6196 tcg_temp_free_ptr(fpst);
6201 * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
6203 * This code is handles the common shifting code and is used by both
6204 * the vector and scalar code.
6206 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6207 TCGv_i64 tcg_rnd, bool accumulate,
6208 bool is_u, int size, int shift)
6210 bool extended_result = false;
6211 bool round = !TCGV_IS_UNUSED_I64(tcg_rnd);
6212 int ext_lshift = 0;
6213 TCGv_i64 tcg_src_hi;
6215 if (round && size == 3) {
6216 extended_result = true;
6217 ext_lshift = 64 - shift;
6218 tcg_src_hi = tcg_temp_new_i64();
6219 } else if (shift == 64) {
6220 if (!accumulate && is_u) {
6221 /* result is zero */
6222 tcg_gen_movi_i64(tcg_res, 0);
6223 return;
6227 /* Deal with the rounding step */
6228 if (round) {
6229 if (extended_result) {
6230 TCGv_i64 tcg_zero = tcg_const_i64(0);
6231 if (!is_u) {
6232 /* take care of sign extending tcg_res */
6233 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
6234 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
6235 tcg_src, tcg_src_hi,
6236 tcg_rnd, tcg_zero);
6237 } else {
6238 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
6239 tcg_src, tcg_zero,
6240 tcg_rnd, tcg_zero);
6242 tcg_temp_free_i64(tcg_zero);
6243 } else {
6244 tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
6248 /* Now do the shift right */
6249 if (round && extended_result) {
6250 /* extended case, >64 bit precision required */
6251 if (ext_lshift == 0) {
6252 /* special case, only high bits matter */
6253 tcg_gen_mov_i64(tcg_src, tcg_src_hi);
6254 } else {
6255 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6256 tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
6257 tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
6259 } else {
6260 if (is_u) {
6261 if (shift == 64) {
6262 /* essentially shifting in 64 zeros */
6263 tcg_gen_movi_i64(tcg_src, 0);
6264 } else {
6265 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6267 } else {
6268 if (shift == 64) {
6269 /* effectively extending the sign-bit */
6270 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
6271 } else {
6272 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
6277 if (accumulate) {
6278 tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
6279 } else {
6280 tcg_gen_mov_i64(tcg_res, tcg_src);
6283 if (extended_result) {
6284 tcg_temp_free_i64(tcg_src_hi);
6288 /* Common SHL/SLI - Shift left with an optional insert */
6289 static void handle_shli_with_ins(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6290 bool insert, int shift)
6292 if (insert) { /* SLI */
6293 tcg_gen_deposit_i64(tcg_res, tcg_res, tcg_src, shift, 64 - shift);
6294 } else { /* SHL */
6295 tcg_gen_shli_i64(tcg_res, tcg_src, shift);
6299 /* SRI: shift right with insert */
6300 static void handle_shri_with_ins(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6301 int size, int shift)
6303 int esize = 8 << size;
6305 /* shift count same as element size is valid but does nothing;
6306 * special case to avoid potential shift by 64.
6308 if (shift != esize) {
6309 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6310 tcg_gen_deposit_i64(tcg_res, tcg_res, tcg_src, 0, esize - shift);
6314 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
6315 static void handle_scalar_simd_shri(DisasContext *s,
6316 bool is_u, int immh, int immb,
6317 int opcode, int rn, int rd)
6319 const int size = 3;
6320 int immhb = immh << 3 | immb;
6321 int shift = 2 * (8 << size) - immhb;
6322 bool accumulate = false;
6323 bool round = false;
6324 bool insert = false;
6325 TCGv_i64 tcg_rn;
6326 TCGv_i64 tcg_rd;
6327 TCGv_i64 tcg_round;
6329 if (!extract32(immh, 3, 1)) {
6330 unallocated_encoding(s);
6331 return;
6334 if (!fp_access_check(s)) {
6335 return;
6338 switch (opcode) {
6339 case 0x02: /* SSRA / USRA (accumulate) */
6340 accumulate = true;
6341 break;
6342 case 0x04: /* SRSHR / URSHR (rounding) */
6343 round = true;
6344 break;
6345 case 0x06: /* SRSRA / URSRA (accum + rounding) */
6346 accumulate = round = true;
6347 break;
6348 case 0x08: /* SRI */
6349 insert = true;
6350 break;
6353 if (round) {
6354 uint64_t round_const = 1ULL << (shift - 1);
6355 tcg_round = tcg_const_i64(round_const);
6356 } else {
6357 TCGV_UNUSED_I64(tcg_round);
6360 tcg_rn = read_fp_dreg(s, rn);
6361 tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6363 if (insert) {
6364 handle_shri_with_ins(tcg_rd, tcg_rn, size, shift);
6365 } else {
6366 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6367 accumulate, is_u, size, shift);
6370 write_fp_dreg(s, rd, tcg_rd);
6372 tcg_temp_free_i64(tcg_rn);
6373 tcg_temp_free_i64(tcg_rd);
6374 if (round) {
6375 tcg_temp_free_i64(tcg_round);
6379 /* SHL/SLI - Scalar shift left */
6380 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
6381 int immh, int immb, int opcode,
6382 int rn, int rd)
6384 int size = 32 - clz32(immh) - 1;
6385 int immhb = immh << 3 | immb;
6386 int shift = immhb - (8 << size);
6387 TCGv_i64 tcg_rn = new_tmp_a64(s);
6388 TCGv_i64 tcg_rd = new_tmp_a64(s);
6390 if (!extract32(immh, 3, 1)) {
6391 unallocated_encoding(s);
6392 return;
6395 if (!fp_access_check(s)) {
6396 return;
6399 tcg_rn = read_fp_dreg(s, rn);
6400 tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6402 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
6404 write_fp_dreg(s, rd, tcg_rd);
6406 tcg_temp_free_i64(tcg_rn);
6407 tcg_temp_free_i64(tcg_rd);
6410 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
6411 * (signed/unsigned) narrowing */
6412 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
6413 bool is_u_shift, bool is_u_narrow,
6414 int immh, int immb, int opcode,
6415 int rn, int rd)
6417 int immhb = immh << 3 | immb;
6418 int size = 32 - clz32(immh) - 1;
6419 int esize = 8 << size;
6420 int shift = (2 * esize) - immhb;
6421 int elements = is_scalar ? 1 : (64 / esize);
6422 bool round = extract32(opcode, 0, 1);
6423 TCGMemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
6424 TCGv_i64 tcg_rn, tcg_rd, tcg_round;
6425 TCGv_i32 tcg_rd_narrowed;
6426 TCGv_i64 tcg_final;
6428 static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
6429 { gen_helper_neon_narrow_sat_s8,
6430 gen_helper_neon_unarrow_sat8 },
6431 { gen_helper_neon_narrow_sat_s16,
6432 gen_helper_neon_unarrow_sat16 },
6433 { gen_helper_neon_narrow_sat_s32,
6434 gen_helper_neon_unarrow_sat32 },
6435 { NULL, NULL },
6437 static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
6438 gen_helper_neon_narrow_sat_u8,
6439 gen_helper_neon_narrow_sat_u16,
6440 gen_helper_neon_narrow_sat_u32,
6441 NULL
6443 NeonGenNarrowEnvFn *narrowfn;
6445 int i;
6447 assert(size < 4);
6449 if (extract32(immh, 3, 1)) {
6450 unallocated_encoding(s);
6451 return;
6454 if (!fp_access_check(s)) {
6455 return;
6458 if (is_u_shift) {
6459 narrowfn = unsigned_narrow_fns[size];
6460 } else {
6461 narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
6464 tcg_rn = tcg_temp_new_i64();
6465 tcg_rd = tcg_temp_new_i64();
6466 tcg_rd_narrowed = tcg_temp_new_i32();
6467 tcg_final = tcg_const_i64(0);
6469 if (round) {
6470 uint64_t round_const = 1ULL << (shift - 1);
6471 tcg_round = tcg_const_i64(round_const);
6472 } else {
6473 TCGV_UNUSED_I64(tcg_round);
6476 for (i = 0; i < elements; i++) {
6477 read_vec_element(s, tcg_rn, rn, i, ldop);
6478 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6479 false, is_u_shift, size+1, shift);
6480 narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
6481 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
6482 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
6485 if (!is_q) {
6486 clear_vec_high(s, rd);
6487 write_vec_element(s, tcg_final, rd, 0, MO_64);
6488 } else {
6489 write_vec_element(s, tcg_final, rd, 1, MO_64);
6492 if (round) {
6493 tcg_temp_free_i64(tcg_round);
6495 tcg_temp_free_i64(tcg_rn);
6496 tcg_temp_free_i64(tcg_rd);
6497 tcg_temp_free_i32(tcg_rd_narrowed);
6498 tcg_temp_free_i64(tcg_final);
6499 return;
6502 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
6503 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
6504 bool src_unsigned, bool dst_unsigned,
6505 int immh, int immb, int rn, int rd)
6507 int immhb = immh << 3 | immb;
6508 int size = 32 - clz32(immh) - 1;
6509 int shift = immhb - (8 << size);
6510 int pass;
6512 assert(immh != 0);
6513 assert(!(scalar && is_q));
6515 if (!scalar) {
6516 if (!is_q && extract32(immh, 3, 1)) {
6517 unallocated_encoding(s);
6518 return;
6521 /* Since we use the variable-shift helpers we must
6522 * replicate the shift count into each element of
6523 * the tcg_shift value.
6525 switch (size) {
6526 case 0:
6527 shift |= shift << 8;
6528 /* fall through */
6529 case 1:
6530 shift |= shift << 16;
6531 break;
6532 case 2:
6533 case 3:
6534 break;
6535 default:
6536 g_assert_not_reached();
6540 if (!fp_access_check(s)) {
6541 return;
6544 if (size == 3) {
6545 TCGv_i64 tcg_shift = tcg_const_i64(shift);
6546 static NeonGenTwo64OpEnvFn * const fns[2][2] = {
6547 { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
6548 { NULL, gen_helper_neon_qshl_u64 },
6550 NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
6551 int maxpass = is_q ? 2 : 1;
6553 for (pass = 0; pass < maxpass; pass++) {
6554 TCGv_i64 tcg_op = tcg_temp_new_i64();
6556 read_vec_element(s, tcg_op, rn, pass, MO_64);
6557 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6558 write_vec_element(s, tcg_op, rd, pass, MO_64);
6560 tcg_temp_free_i64(tcg_op);
6562 tcg_temp_free_i64(tcg_shift);
6564 if (!is_q) {
6565 clear_vec_high(s, rd);
6567 } else {
6568 TCGv_i32 tcg_shift = tcg_const_i32(shift);
6569 static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
6571 { gen_helper_neon_qshl_s8,
6572 gen_helper_neon_qshl_s16,
6573 gen_helper_neon_qshl_s32 },
6574 { gen_helper_neon_qshlu_s8,
6575 gen_helper_neon_qshlu_s16,
6576 gen_helper_neon_qshlu_s32 }
6577 }, {
6578 { NULL, NULL, NULL },
6579 { gen_helper_neon_qshl_u8,
6580 gen_helper_neon_qshl_u16,
6581 gen_helper_neon_qshl_u32 }
6584 NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
6585 TCGMemOp memop = scalar ? size : MO_32;
6586 int maxpass = scalar ? 1 : is_q ? 4 : 2;
6588 for (pass = 0; pass < maxpass; pass++) {
6589 TCGv_i32 tcg_op = tcg_temp_new_i32();
6591 read_vec_element_i32(s, tcg_op, rn, pass, memop);
6592 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6593 if (scalar) {
6594 switch (size) {
6595 case 0:
6596 tcg_gen_ext8u_i32(tcg_op, tcg_op);
6597 break;
6598 case 1:
6599 tcg_gen_ext16u_i32(tcg_op, tcg_op);
6600 break;
6601 case 2:
6602 break;
6603 default:
6604 g_assert_not_reached();
6606 write_fp_sreg(s, rd, tcg_op);
6607 } else {
6608 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
6611 tcg_temp_free_i32(tcg_op);
6613 tcg_temp_free_i32(tcg_shift);
6615 if (!is_q && !scalar) {
6616 clear_vec_high(s, rd);
6621 /* Common vector code for handling integer to FP conversion */
6622 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
6623 int elements, int is_signed,
6624 int fracbits, int size)
6626 bool is_double = size == 3 ? true : false;
6627 TCGv_ptr tcg_fpst = get_fpstatus_ptr();
6628 TCGv_i32 tcg_shift = tcg_const_i32(fracbits);
6629 TCGv_i64 tcg_int = tcg_temp_new_i64();
6630 TCGMemOp mop = size | (is_signed ? MO_SIGN : 0);
6631 int pass;
6633 for (pass = 0; pass < elements; pass++) {
6634 read_vec_element(s, tcg_int, rn, pass, mop);
6636 if (is_double) {
6637 TCGv_i64 tcg_double = tcg_temp_new_i64();
6638 if (is_signed) {
6639 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6640 tcg_shift, tcg_fpst);
6641 } else {
6642 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6643 tcg_shift, tcg_fpst);
6645 if (elements == 1) {
6646 write_fp_dreg(s, rd, tcg_double);
6647 } else {
6648 write_vec_element(s, tcg_double, rd, pass, MO_64);
6650 tcg_temp_free_i64(tcg_double);
6651 } else {
6652 TCGv_i32 tcg_single = tcg_temp_new_i32();
6653 if (is_signed) {
6654 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6655 tcg_shift, tcg_fpst);
6656 } else {
6657 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6658 tcg_shift, tcg_fpst);
6660 if (elements == 1) {
6661 write_fp_sreg(s, rd, tcg_single);
6662 } else {
6663 write_vec_element_i32(s, tcg_single, rd, pass, MO_32);
6665 tcg_temp_free_i32(tcg_single);
6669 if (!is_double && elements == 2) {
6670 clear_vec_high(s, rd);
6673 tcg_temp_free_i64(tcg_int);
6674 tcg_temp_free_ptr(tcg_fpst);
6675 tcg_temp_free_i32(tcg_shift);
6678 /* UCVTF/SCVTF - Integer to FP conversion */
6679 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
6680 bool is_q, bool is_u,
6681 int immh, int immb, int opcode,
6682 int rn, int rd)
6684 bool is_double = extract32(immh, 3, 1);
6685 int size = is_double ? MO_64 : MO_32;
6686 int elements;
6687 int immhb = immh << 3 | immb;
6688 int fracbits = (is_double ? 128 : 64) - immhb;
6690 if (!extract32(immh, 2, 2)) {
6691 unallocated_encoding(s);
6692 return;
6695 if (is_scalar) {
6696 elements = 1;
6697 } else {
6698 elements = is_double ? 2 : is_q ? 4 : 2;
6699 if (is_double && !is_q) {
6700 unallocated_encoding(s);
6701 return;
6705 if (!fp_access_check(s)) {
6706 return;
6709 /* immh == 0 would be a failure of the decode logic */
6710 g_assert(immh);
6712 handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
6715 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
6716 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
6717 bool is_q, bool is_u,
6718 int immh, int immb, int rn, int rd)
6720 bool is_double = extract32(immh, 3, 1);
6721 int immhb = immh << 3 | immb;
6722 int fracbits = (is_double ? 128 : 64) - immhb;
6723 int pass;
6724 TCGv_ptr tcg_fpstatus;
6725 TCGv_i32 tcg_rmode, tcg_shift;
6727 if (!extract32(immh, 2, 2)) {
6728 unallocated_encoding(s);
6729 return;
6732 if (!is_scalar && !is_q && is_double) {
6733 unallocated_encoding(s);
6734 return;
6737 if (!fp_access_check(s)) {
6738 return;
6741 assert(!(is_scalar && is_q));
6743 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(FPROUNDING_ZERO));
6744 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
6745 tcg_fpstatus = get_fpstatus_ptr();
6746 tcg_shift = tcg_const_i32(fracbits);
6748 if (is_double) {
6749 int maxpass = is_scalar ? 1 : 2;
6751 for (pass = 0; pass < maxpass; pass++) {
6752 TCGv_i64 tcg_op = tcg_temp_new_i64();
6754 read_vec_element(s, tcg_op, rn, pass, MO_64);
6755 if (is_u) {
6756 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6757 } else {
6758 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6760 write_vec_element(s, tcg_op, rd, pass, MO_64);
6761 tcg_temp_free_i64(tcg_op);
6763 if (!is_q) {
6764 clear_vec_high(s, rd);
6766 } else {
6767 int maxpass = is_scalar ? 1 : is_q ? 4 : 2;
6768 for (pass = 0; pass < maxpass; pass++) {
6769 TCGv_i32 tcg_op = tcg_temp_new_i32();
6771 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
6772 if (is_u) {
6773 gen_helper_vfp_touls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6774 } else {
6775 gen_helper_vfp_tosls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6777 if (is_scalar) {
6778 write_fp_sreg(s, rd, tcg_op);
6779 } else {
6780 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
6782 tcg_temp_free_i32(tcg_op);
6784 if (!is_q && !is_scalar) {
6785 clear_vec_high(s, rd);
6789 tcg_temp_free_ptr(tcg_fpstatus);
6790 tcg_temp_free_i32(tcg_shift);
6791 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
6792 tcg_temp_free_i32(tcg_rmode);
6795 /* C3.6.9 AdvSIMD scalar shift by immediate
6796 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
6797 * +-----+---+-------------+------+------+--------+---+------+------+
6798 * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
6799 * +-----+---+-------------+------+------+--------+---+------+------+
6801 * This is the scalar version so it works on a fixed sized registers
6803 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
6805 int rd = extract32(insn, 0, 5);
6806 int rn = extract32(insn, 5, 5);
6807 int opcode = extract32(insn, 11, 5);
6808 int immb = extract32(insn, 16, 3);
6809 int immh = extract32(insn, 19, 4);
6810 bool is_u = extract32(insn, 29, 1);
6812 if (immh == 0) {
6813 unallocated_encoding(s);
6814 return;
6817 switch (opcode) {
6818 case 0x08: /* SRI */
6819 if (!is_u) {
6820 unallocated_encoding(s);
6821 return;
6823 /* fall through */
6824 case 0x00: /* SSHR / USHR */
6825 case 0x02: /* SSRA / USRA */
6826 case 0x04: /* SRSHR / URSHR */
6827 case 0x06: /* SRSRA / URSRA */
6828 handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
6829 break;
6830 case 0x0a: /* SHL / SLI */
6831 handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
6832 break;
6833 case 0x1c: /* SCVTF, UCVTF */
6834 handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
6835 opcode, rn, rd);
6836 break;
6837 case 0x10: /* SQSHRUN, SQSHRUN2 */
6838 case 0x11: /* SQRSHRUN, SQRSHRUN2 */
6839 if (!is_u) {
6840 unallocated_encoding(s);
6841 return;
6843 handle_vec_simd_sqshrn(s, true, false, false, true,
6844 immh, immb, opcode, rn, rd);
6845 break;
6846 case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
6847 case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
6848 handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
6849 immh, immb, opcode, rn, rd);
6850 break;
6851 case 0xc: /* SQSHLU */
6852 if (!is_u) {
6853 unallocated_encoding(s);
6854 return;
6856 handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
6857 break;
6858 case 0xe: /* SQSHL, UQSHL */
6859 handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
6860 break;
6861 case 0x1f: /* FCVTZS, FCVTZU */
6862 handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
6863 break;
6864 default:
6865 unallocated_encoding(s);
6866 break;
6870 /* C3.6.10 AdvSIMD scalar three different
6871 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
6872 * +-----+---+-----------+------+---+------+--------+-----+------+------+
6873 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
6874 * +-----+---+-----------+------+---+------+--------+-----+------+------+
6876 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
6878 bool is_u = extract32(insn, 29, 1);
6879 int size = extract32(insn, 22, 2);
6880 int opcode = extract32(insn, 12, 4);
6881 int rm = extract32(insn, 16, 5);
6882 int rn = extract32(insn, 5, 5);
6883 int rd = extract32(insn, 0, 5);
6885 if (is_u) {
6886 unallocated_encoding(s);
6887 return;
6890 switch (opcode) {
6891 case 0x9: /* SQDMLAL, SQDMLAL2 */
6892 case 0xb: /* SQDMLSL, SQDMLSL2 */
6893 case 0xd: /* SQDMULL, SQDMULL2 */
6894 if (size == 0 || size == 3) {
6895 unallocated_encoding(s);
6896 return;
6898 break;
6899 default:
6900 unallocated_encoding(s);
6901 return;
6904 if (!fp_access_check(s)) {
6905 return;
6908 if (size == 2) {
6909 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6910 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6911 TCGv_i64 tcg_res = tcg_temp_new_i64();
6913 read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
6914 read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
6916 tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
6917 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
6919 switch (opcode) {
6920 case 0xd: /* SQDMULL, SQDMULL2 */
6921 break;
6922 case 0xb: /* SQDMLSL, SQDMLSL2 */
6923 tcg_gen_neg_i64(tcg_res, tcg_res);
6924 /* fall through */
6925 case 0x9: /* SQDMLAL, SQDMLAL2 */
6926 read_vec_element(s, tcg_op1, rd, 0, MO_64);
6927 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
6928 tcg_res, tcg_op1);
6929 break;
6930 default:
6931 g_assert_not_reached();
6934 write_fp_dreg(s, rd, tcg_res);
6936 tcg_temp_free_i64(tcg_op1);
6937 tcg_temp_free_i64(tcg_op2);
6938 tcg_temp_free_i64(tcg_res);
6939 } else {
6940 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6941 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6942 TCGv_i64 tcg_res = tcg_temp_new_i64();
6944 read_vec_element_i32(s, tcg_op1, rn, 0, MO_16);
6945 read_vec_element_i32(s, tcg_op2, rm, 0, MO_16);
6947 gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
6948 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
6950 switch (opcode) {
6951 case 0xd: /* SQDMULL, SQDMULL2 */
6952 break;
6953 case 0xb: /* SQDMLSL, SQDMLSL2 */
6954 gen_helper_neon_negl_u32(tcg_res, tcg_res);
6955 /* fall through */
6956 case 0x9: /* SQDMLAL, SQDMLAL2 */
6958 TCGv_i64 tcg_op3 = tcg_temp_new_i64();
6959 read_vec_element(s, tcg_op3, rd, 0, MO_32);
6960 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
6961 tcg_res, tcg_op3);
6962 tcg_temp_free_i64(tcg_op3);
6963 break;
6965 default:
6966 g_assert_not_reached();
6969 tcg_gen_ext32u_i64(tcg_res, tcg_res);
6970 write_fp_dreg(s, rd, tcg_res);
6972 tcg_temp_free_i32(tcg_op1);
6973 tcg_temp_free_i32(tcg_op2);
6974 tcg_temp_free_i64(tcg_res);
6978 static void handle_3same_64(DisasContext *s, int opcode, bool u,
6979 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
6981 /* Handle 64x64->64 opcodes which are shared between the scalar
6982 * and vector 3-same groups. We cover every opcode where size == 3
6983 * is valid in either the three-reg-same (integer, not pairwise)
6984 * or scalar-three-reg-same groups. (Some opcodes are not yet
6985 * implemented.)
6987 TCGCond cond;
6989 switch (opcode) {
6990 case 0x1: /* SQADD */
6991 if (u) {
6992 gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6993 } else {
6994 gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6996 break;
6997 case 0x5: /* SQSUB */
6998 if (u) {
6999 gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7000 } else {
7001 gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7003 break;
7004 case 0x6: /* CMGT, CMHI */
7005 /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
7006 * We implement this using setcond (test) and then negating.
7008 cond = u ? TCG_COND_GTU : TCG_COND_GT;
7009 do_cmop:
7010 tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
7011 tcg_gen_neg_i64(tcg_rd, tcg_rd);
7012 break;
7013 case 0x7: /* CMGE, CMHS */
7014 cond = u ? TCG_COND_GEU : TCG_COND_GE;
7015 goto do_cmop;
7016 case 0x11: /* CMTST, CMEQ */
7017 if (u) {
7018 cond = TCG_COND_EQ;
7019 goto do_cmop;
7021 /* CMTST : test is "if (X & Y != 0)". */
7022 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
7023 tcg_gen_setcondi_i64(TCG_COND_NE, tcg_rd, tcg_rd, 0);
7024 tcg_gen_neg_i64(tcg_rd, tcg_rd);
7025 break;
7026 case 0x8: /* SSHL, USHL */
7027 if (u) {
7028 gen_helper_neon_shl_u64(tcg_rd, tcg_rn, tcg_rm);
7029 } else {
7030 gen_helper_neon_shl_s64(tcg_rd, tcg_rn, tcg_rm);
7032 break;
7033 case 0x9: /* SQSHL, UQSHL */
7034 if (u) {
7035 gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7036 } else {
7037 gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7039 break;
7040 case 0xa: /* SRSHL, URSHL */
7041 if (u) {
7042 gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
7043 } else {
7044 gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
7046 break;
7047 case 0xb: /* SQRSHL, UQRSHL */
7048 if (u) {
7049 gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7050 } else {
7051 gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7053 break;
7054 case 0x10: /* ADD, SUB */
7055 if (u) {
7056 tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
7057 } else {
7058 tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
7060 break;
7061 default:
7062 g_assert_not_reached();
7066 /* Handle the 3-same-operands float operations; shared by the scalar
7067 * and vector encodings. The caller must filter out any encodings
7068 * not allocated for the encoding it is dealing with.
7070 static void handle_3same_float(DisasContext *s, int size, int elements,
7071 int fpopcode, int rd, int rn, int rm)
7073 int pass;
7074 TCGv_ptr fpst = get_fpstatus_ptr();
7076 for (pass = 0; pass < elements; pass++) {
7077 if (size) {
7078 /* Double */
7079 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7080 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7081 TCGv_i64 tcg_res = tcg_temp_new_i64();
7083 read_vec_element(s, tcg_op1, rn, pass, MO_64);
7084 read_vec_element(s, tcg_op2, rm, pass, MO_64);
7086 switch (fpopcode) {
7087 case 0x39: /* FMLS */
7088 /* As usual for ARM, separate negation for fused multiply-add */
7089 gen_helper_vfp_negd(tcg_op1, tcg_op1);
7090 /* fall through */
7091 case 0x19: /* FMLA */
7092 read_vec_element(s, tcg_res, rd, pass, MO_64);
7093 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
7094 tcg_res, fpst);
7095 break;
7096 case 0x18: /* FMAXNM */
7097 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7098 break;
7099 case 0x1a: /* FADD */
7100 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
7101 break;
7102 case 0x1b: /* FMULX */
7103 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
7104 break;
7105 case 0x1c: /* FCMEQ */
7106 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7107 break;
7108 case 0x1e: /* FMAX */
7109 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
7110 break;
7111 case 0x1f: /* FRECPS */
7112 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7113 break;
7114 case 0x38: /* FMINNM */
7115 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7116 break;
7117 case 0x3a: /* FSUB */
7118 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
7119 break;
7120 case 0x3e: /* FMIN */
7121 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
7122 break;
7123 case 0x3f: /* FRSQRTS */
7124 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7125 break;
7126 case 0x5b: /* FMUL */
7127 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
7128 break;
7129 case 0x5c: /* FCMGE */
7130 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7131 break;
7132 case 0x5d: /* FACGE */
7133 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7134 break;
7135 case 0x5f: /* FDIV */
7136 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
7137 break;
7138 case 0x7a: /* FABD */
7139 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
7140 gen_helper_vfp_absd(tcg_res, tcg_res);
7141 break;
7142 case 0x7c: /* FCMGT */
7143 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7144 break;
7145 case 0x7d: /* FACGT */
7146 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7147 break;
7148 default:
7149 g_assert_not_reached();
7152 write_vec_element(s, tcg_res, rd, pass, MO_64);
7154 tcg_temp_free_i64(tcg_res);
7155 tcg_temp_free_i64(tcg_op1);
7156 tcg_temp_free_i64(tcg_op2);
7157 } else {
7158 /* Single */
7159 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7160 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7161 TCGv_i32 tcg_res = tcg_temp_new_i32();
7163 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
7164 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
7166 switch (fpopcode) {
7167 case 0x39: /* FMLS */
7168 /* As usual for ARM, separate negation for fused multiply-add */
7169 gen_helper_vfp_negs(tcg_op1, tcg_op1);
7170 /* fall through */
7171 case 0x19: /* FMLA */
7172 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7173 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
7174 tcg_res, fpst);
7175 break;
7176 case 0x1a: /* FADD */
7177 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
7178 break;
7179 case 0x1b: /* FMULX */
7180 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
7181 break;
7182 case 0x1c: /* FCMEQ */
7183 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7184 break;
7185 case 0x1e: /* FMAX */
7186 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
7187 break;
7188 case 0x1f: /* FRECPS */
7189 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7190 break;
7191 case 0x18: /* FMAXNM */
7192 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
7193 break;
7194 case 0x38: /* FMINNM */
7195 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
7196 break;
7197 case 0x3a: /* FSUB */
7198 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
7199 break;
7200 case 0x3e: /* FMIN */
7201 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
7202 break;
7203 case 0x3f: /* FRSQRTS */
7204 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7205 break;
7206 case 0x5b: /* FMUL */
7207 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
7208 break;
7209 case 0x5c: /* FCMGE */
7210 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7211 break;
7212 case 0x5d: /* FACGE */
7213 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7214 break;
7215 case 0x5f: /* FDIV */
7216 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
7217 break;
7218 case 0x7a: /* FABD */
7219 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
7220 gen_helper_vfp_abss(tcg_res, tcg_res);
7221 break;
7222 case 0x7c: /* FCMGT */
7223 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7224 break;
7225 case 0x7d: /* FACGT */
7226 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7227 break;
7228 default:
7229 g_assert_not_reached();
7232 if (elements == 1) {
7233 /* scalar single so clear high part */
7234 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7236 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
7237 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
7238 tcg_temp_free_i64(tcg_tmp);
7239 } else {
7240 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7243 tcg_temp_free_i32(tcg_res);
7244 tcg_temp_free_i32(tcg_op1);
7245 tcg_temp_free_i32(tcg_op2);
7249 tcg_temp_free_ptr(fpst);
7251 if ((elements << size) < 4) {
7252 /* scalar, or non-quad vector op */
7253 clear_vec_high(s, rd);
7257 /* C3.6.11 AdvSIMD scalar three same
7258 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
7259 * +-----+---+-----------+------+---+------+--------+---+------+------+
7260 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
7261 * +-----+---+-----------+------+---+------+--------+---+------+------+
7263 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
7265 int rd = extract32(insn, 0, 5);
7266 int rn = extract32(insn, 5, 5);
7267 int opcode = extract32(insn, 11, 5);
7268 int rm = extract32(insn, 16, 5);
7269 int size = extract32(insn, 22, 2);
7270 bool u = extract32(insn, 29, 1);
7271 TCGv_i64 tcg_rd;
7273 if (opcode >= 0x18) {
7274 /* Floating point: U, size[1] and opcode indicate operation */
7275 int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
7276 switch (fpopcode) {
7277 case 0x1b: /* FMULX */
7278 case 0x1f: /* FRECPS */
7279 case 0x3f: /* FRSQRTS */
7280 case 0x5d: /* FACGE */
7281 case 0x7d: /* FACGT */
7282 case 0x1c: /* FCMEQ */
7283 case 0x5c: /* FCMGE */
7284 case 0x7c: /* FCMGT */
7285 case 0x7a: /* FABD */
7286 break;
7287 default:
7288 unallocated_encoding(s);
7289 return;
7292 if (!fp_access_check(s)) {
7293 return;
7296 handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
7297 return;
7300 switch (opcode) {
7301 case 0x1: /* SQADD, UQADD */
7302 case 0x5: /* SQSUB, UQSUB */
7303 case 0x9: /* SQSHL, UQSHL */
7304 case 0xb: /* SQRSHL, UQRSHL */
7305 break;
7306 case 0x8: /* SSHL, USHL */
7307 case 0xa: /* SRSHL, URSHL */
7308 case 0x6: /* CMGT, CMHI */
7309 case 0x7: /* CMGE, CMHS */
7310 case 0x11: /* CMTST, CMEQ */
7311 case 0x10: /* ADD, SUB (vector) */
7312 if (size != 3) {
7313 unallocated_encoding(s);
7314 return;
7316 break;
7317 case 0x16: /* SQDMULH, SQRDMULH (vector) */
7318 if (size != 1 && size != 2) {
7319 unallocated_encoding(s);
7320 return;
7322 break;
7323 default:
7324 unallocated_encoding(s);
7325 return;
7328 if (!fp_access_check(s)) {
7329 return;
7332 tcg_rd = tcg_temp_new_i64();
7334 if (size == 3) {
7335 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
7336 TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
7338 handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
7339 tcg_temp_free_i64(tcg_rn);
7340 tcg_temp_free_i64(tcg_rm);
7341 } else {
7342 /* Do a single operation on the lowest element in the vector.
7343 * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
7344 * no side effects for all these operations.
7345 * OPTME: special-purpose helpers would avoid doing some
7346 * unnecessary work in the helper for the 8 and 16 bit cases.
7348 NeonGenTwoOpEnvFn *genenvfn;
7349 TCGv_i32 tcg_rn = tcg_temp_new_i32();
7350 TCGv_i32 tcg_rm = tcg_temp_new_i32();
7351 TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
7353 read_vec_element_i32(s, tcg_rn, rn, 0, size);
7354 read_vec_element_i32(s, tcg_rm, rm, 0, size);
7356 switch (opcode) {
7357 case 0x1: /* SQADD, UQADD */
7359 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7360 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
7361 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
7362 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
7364 genenvfn = fns[size][u];
7365 break;
7367 case 0x5: /* SQSUB, UQSUB */
7369 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7370 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
7371 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
7372 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
7374 genenvfn = fns[size][u];
7375 break;
7377 case 0x9: /* SQSHL, UQSHL */
7379 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7380 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
7381 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
7382 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
7384 genenvfn = fns[size][u];
7385 break;
7387 case 0xb: /* SQRSHL, UQRSHL */
7389 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7390 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
7391 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
7392 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
7394 genenvfn = fns[size][u];
7395 break;
7397 case 0x16: /* SQDMULH, SQRDMULH */
7399 static NeonGenTwoOpEnvFn * const fns[2][2] = {
7400 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
7401 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
7403 assert(size == 1 || size == 2);
7404 genenvfn = fns[size - 1][u];
7405 break;
7407 default:
7408 g_assert_not_reached();
7411 genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
7412 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
7413 tcg_temp_free_i32(tcg_rd32);
7414 tcg_temp_free_i32(tcg_rn);
7415 tcg_temp_free_i32(tcg_rm);
7418 write_fp_dreg(s, rd, tcg_rd);
7420 tcg_temp_free_i64(tcg_rd);
7423 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
7424 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
7425 TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
7427 /* Handle 64->64 opcodes which are shared between the scalar and
7428 * vector 2-reg-misc groups. We cover every integer opcode where size == 3
7429 * is valid in either group and also the double-precision fp ops.
7430 * The caller only need provide tcg_rmode and tcg_fpstatus if the op
7431 * requires them.
7433 TCGCond cond;
7435 switch (opcode) {
7436 case 0x4: /* CLS, CLZ */
7437 if (u) {
7438 gen_helper_clz64(tcg_rd, tcg_rn);
7439 } else {
7440 gen_helper_cls64(tcg_rd, tcg_rn);
7442 break;
7443 case 0x5: /* NOT */
7444 /* This opcode is shared with CNT and RBIT but we have earlier
7445 * enforced that size == 3 if and only if this is the NOT insn.
7447 tcg_gen_not_i64(tcg_rd, tcg_rn);
7448 break;
7449 case 0x7: /* SQABS, SQNEG */
7450 if (u) {
7451 gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
7452 } else {
7453 gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
7455 break;
7456 case 0xa: /* CMLT */
7457 /* 64 bit integer comparison against zero, result is
7458 * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
7459 * subtracting 1.
7461 cond = TCG_COND_LT;
7462 do_cmop:
7463 tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
7464 tcg_gen_neg_i64(tcg_rd, tcg_rd);
7465 break;
7466 case 0x8: /* CMGT, CMGE */
7467 cond = u ? TCG_COND_GE : TCG_COND_GT;
7468 goto do_cmop;
7469 case 0x9: /* CMEQ, CMLE */
7470 cond = u ? TCG_COND_LE : TCG_COND_EQ;
7471 goto do_cmop;
7472 case 0xb: /* ABS, NEG */
7473 if (u) {
7474 tcg_gen_neg_i64(tcg_rd, tcg_rn);
7475 } else {
7476 TCGv_i64 tcg_zero = tcg_const_i64(0);
7477 tcg_gen_neg_i64(tcg_rd, tcg_rn);
7478 tcg_gen_movcond_i64(TCG_COND_GT, tcg_rd, tcg_rn, tcg_zero,
7479 tcg_rn, tcg_rd);
7480 tcg_temp_free_i64(tcg_zero);
7482 break;
7483 case 0x2f: /* FABS */
7484 gen_helper_vfp_absd(tcg_rd, tcg_rn);
7485 break;
7486 case 0x6f: /* FNEG */
7487 gen_helper_vfp_negd(tcg_rd, tcg_rn);
7488 break;
7489 case 0x7f: /* FSQRT */
7490 gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
7491 break;
7492 case 0x1a: /* FCVTNS */
7493 case 0x1b: /* FCVTMS */
7494 case 0x1c: /* FCVTAS */
7495 case 0x3a: /* FCVTPS */
7496 case 0x3b: /* FCVTZS */
7498 TCGv_i32 tcg_shift = tcg_const_i32(0);
7499 gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7500 tcg_temp_free_i32(tcg_shift);
7501 break;
7503 case 0x5a: /* FCVTNU */
7504 case 0x5b: /* FCVTMU */
7505 case 0x5c: /* FCVTAU */
7506 case 0x7a: /* FCVTPU */
7507 case 0x7b: /* FCVTZU */
7509 TCGv_i32 tcg_shift = tcg_const_i32(0);
7510 gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7511 tcg_temp_free_i32(tcg_shift);
7512 break;
7514 case 0x18: /* FRINTN */
7515 case 0x19: /* FRINTM */
7516 case 0x38: /* FRINTP */
7517 case 0x39: /* FRINTZ */
7518 case 0x58: /* FRINTA */
7519 case 0x79: /* FRINTI */
7520 gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
7521 break;
7522 case 0x59: /* FRINTX */
7523 gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
7524 break;
7525 default:
7526 g_assert_not_reached();
7530 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
7531 bool is_scalar, bool is_u, bool is_q,
7532 int size, int rn, int rd)
7534 bool is_double = (size == 3);
7535 TCGv_ptr fpst;
7537 if (!fp_access_check(s)) {
7538 return;
7541 fpst = get_fpstatus_ptr();
7543 if (is_double) {
7544 TCGv_i64 tcg_op = tcg_temp_new_i64();
7545 TCGv_i64 tcg_zero = tcg_const_i64(0);
7546 TCGv_i64 tcg_res = tcg_temp_new_i64();
7547 NeonGenTwoDoubleOPFn *genfn;
7548 bool swap = false;
7549 int pass;
7551 switch (opcode) {
7552 case 0x2e: /* FCMLT (zero) */
7553 swap = true;
7554 /* fallthrough */
7555 case 0x2c: /* FCMGT (zero) */
7556 genfn = gen_helper_neon_cgt_f64;
7557 break;
7558 case 0x2d: /* FCMEQ (zero) */
7559 genfn = gen_helper_neon_ceq_f64;
7560 break;
7561 case 0x6d: /* FCMLE (zero) */
7562 swap = true;
7563 /* fall through */
7564 case 0x6c: /* FCMGE (zero) */
7565 genfn = gen_helper_neon_cge_f64;
7566 break;
7567 default:
7568 g_assert_not_reached();
7571 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7572 read_vec_element(s, tcg_op, rn, pass, MO_64);
7573 if (swap) {
7574 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7575 } else {
7576 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7578 write_vec_element(s, tcg_res, rd, pass, MO_64);
7580 if (is_scalar) {
7581 clear_vec_high(s, rd);
7584 tcg_temp_free_i64(tcg_res);
7585 tcg_temp_free_i64(tcg_zero);
7586 tcg_temp_free_i64(tcg_op);
7587 } else {
7588 TCGv_i32 tcg_op = tcg_temp_new_i32();
7589 TCGv_i32 tcg_zero = tcg_const_i32(0);
7590 TCGv_i32 tcg_res = tcg_temp_new_i32();
7591 NeonGenTwoSingleOPFn *genfn;
7592 bool swap = false;
7593 int pass, maxpasses;
7595 switch (opcode) {
7596 case 0x2e: /* FCMLT (zero) */
7597 swap = true;
7598 /* fall through */
7599 case 0x2c: /* FCMGT (zero) */
7600 genfn = gen_helper_neon_cgt_f32;
7601 break;
7602 case 0x2d: /* FCMEQ (zero) */
7603 genfn = gen_helper_neon_ceq_f32;
7604 break;
7605 case 0x6d: /* FCMLE (zero) */
7606 swap = true;
7607 /* fall through */
7608 case 0x6c: /* FCMGE (zero) */
7609 genfn = gen_helper_neon_cge_f32;
7610 break;
7611 default:
7612 g_assert_not_reached();
7615 if (is_scalar) {
7616 maxpasses = 1;
7617 } else {
7618 maxpasses = is_q ? 4 : 2;
7621 for (pass = 0; pass < maxpasses; pass++) {
7622 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7623 if (swap) {
7624 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7625 } else {
7626 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7628 if (is_scalar) {
7629 write_fp_sreg(s, rd, tcg_res);
7630 } else {
7631 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7634 tcg_temp_free_i32(tcg_res);
7635 tcg_temp_free_i32(tcg_zero);
7636 tcg_temp_free_i32(tcg_op);
7637 if (!is_q && !is_scalar) {
7638 clear_vec_high(s, rd);
7642 tcg_temp_free_ptr(fpst);
7645 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
7646 bool is_scalar, bool is_u, bool is_q,
7647 int size, int rn, int rd)
7649 bool is_double = (size == 3);
7650 TCGv_ptr fpst = get_fpstatus_ptr();
7652 if (is_double) {
7653 TCGv_i64 tcg_op = tcg_temp_new_i64();
7654 TCGv_i64 tcg_res = tcg_temp_new_i64();
7655 int pass;
7657 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7658 read_vec_element(s, tcg_op, rn, pass, MO_64);
7659 switch (opcode) {
7660 case 0x3d: /* FRECPE */
7661 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
7662 break;
7663 case 0x3f: /* FRECPX */
7664 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
7665 break;
7666 case 0x7d: /* FRSQRTE */
7667 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
7668 break;
7669 default:
7670 g_assert_not_reached();
7672 write_vec_element(s, tcg_res, rd, pass, MO_64);
7674 if (is_scalar) {
7675 clear_vec_high(s, rd);
7678 tcg_temp_free_i64(tcg_res);
7679 tcg_temp_free_i64(tcg_op);
7680 } else {
7681 TCGv_i32 tcg_op = tcg_temp_new_i32();
7682 TCGv_i32 tcg_res = tcg_temp_new_i32();
7683 int pass, maxpasses;
7685 if (is_scalar) {
7686 maxpasses = 1;
7687 } else {
7688 maxpasses = is_q ? 4 : 2;
7691 for (pass = 0; pass < maxpasses; pass++) {
7692 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7694 switch (opcode) {
7695 case 0x3c: /* URECPE */
7696 gen_helper_recpe_u32(tcg_res, tcg_op, fpst);
7697 break;
7698 case 0x3d: /* FRECPE */
7699 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
7700 break;
7701 case 0x3f: /* FRECPX */
7702 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
7703 break;
7704 case 0x7d: /* FRSQRTE */
7705 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
7706 break;
7707 default:
7708 g_assert_not_reached();
7711 if (is_scalar) {
7712 write_fp_sreg(s, rd, tcg_res);
7713 } else {
7714 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7717 tcg_temp_free_i32(tcg_res);
7718 tcg_temp_free_i32(tcg_op);
7719 if (!is_q && !is_scalar) {
7720 clear_vec_high(s, rd);
7723 tcg_temp_free_ptr(fpst);
7726 static void handle_2misc_narrow(DisasContext *s, bool scalar,
7727 int opcode, bool u, bool is_q,
7728 int size, int rn, int rd)
7730 /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
7731 * in the source becomes a size element in the destination).
7733 int pass;
7734 TCGv_i32 tcg_res[2];
7735 int destelt = is_q ? 2 : 0;
7736 int passes = scalar ? 1 : 2;
7738 if (scalar) {
7739 tcg_res[1] = tcg_const_i32(0);
7742 for (pass = 0; pass < passes; pass++) {
7743 TCGv_i64 tcg_op = tcg_temp_new_i64();
7744 NeonGenNarrowFn *genfn = NULL;
7745 NeonGenNarrowEnvFn *genenvfn = NULL;
7747 if (scalar) {
7748 read_vec_element(s, tcg_op, rn, pass, size + 1);
7749 } else {
7750 read_vec_element(s, tcg_op, rn, pass, MO_64);
7752 tcg_res[pass] = tcg_temp_new_i32();
7754 switch (opcode) {
7755 case 0x12: /* XTN, SQXTUN */
7757 static NeonGenNarrowFn * const xtnfns[3] = {
7758 gen_helper_neon_narrow_u8,
7759 gen_helper_neon_narrow_u16,
7760 tcg_gen_extrl_i64_i32,
7762 static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
7763 gen_helper_neon_unarrow_sat8,
7764 gen_helper_neon_unarrow_sat16,
7765 gen_helper_neon_unarrow_sat32,
7767 if (u) {
7768 genenvfn = sqxtunfns[size];
7769 } else {
7770 genfn = xtnfns[size];
7772 break;
7774 case 0x14: /* SQXTN, UQXTN */
7776 static NeonGenNarrowEnvFn * const fns[3][2] = {
7777 { gen_helper_neon_narrow_sat_s8,
7778 gen_helper_neon_narrow_sat_u8 },
7779 { gen_helper_neon_narrow_sat_s16,
7780 gen_helper_neon_narrow_sat_u16 },
7781 { gen_helper_neon_narrow_sat_s32,
7782 gen_helper_neon_narrow_sat_u32 },
7784 genenvfn = fns[size][u];
7785 break;
7787 case 0x16: /* FCVTN, FCVTN2 */
7788 /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
7789 if (size == 2) {
7790 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
7791 } else {
7792 TCGv_i32 tcg_lo = tcg_temp_new_i32();
7793 TCGv_i32 tcg_hi = tcg_temp_new_i32();
7794 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
7795 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, cpu_env);
7796 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, cpu_env);
7797 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
7798 tcg_temp_free_i32(tcg_lo);
7799 tcg_temp_free_i32(tcg_hi);
7801 break;
7802 case 0x56: /* FCVTXN, FCVTXN2 */
7803 /* 64 bit to 32 bit float conversion
7804 * with von Neumann rounding (round to odd)
7806 assert(size == 2);
7807 gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
7808 break;
7809 default:
7810 g_assert_not_reached();
7813 if (genfn) {
7814 genfn(tcg_res[pass], tcg_op);
7815 } else if (genenvfn) {
7816 genenvfn(tcg_res[pass], cpu_env, tcg_op);
7819 tcg_temp_free_i64(tcg_op);
7822 for (pass = 0; pass < 2; pass++) {
7823 write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
7824 tcg_temp_free_i32(tcg_res[pass]);
7826 if (!is_q) {
7827 clear_vec_high(s, rd);
7831 /* Remaining saturating accumulating ops */
7832 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
7833 bool is_q, int size, int rn, int rd)
7835 bool is_double = (size == 3);
7837 if (is_double) {
7838 TCGv_i64 tcg_rn = tcg_temp_new_i64();
7839 TCGv_i64 tcg_rd = tcg_temp_new_i64();
7840 int pass;
7842 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7843 read_vec_element(s, tcg_rn, rn, pass, MO_64);
7844 read_vec_element(s, tcg_rd, rd, pass, MO_64);
7846 if (is_u) { /* USQADD */
7847 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7848 } else { /* SUQADD */
7849 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7851 write_vec_element(s, tcg_rd, rd, pass, MO_64);
7853 if (is_scalar) {
7854 clear_vec_high(s, rd);
7857 tcg_temp_free_i64(tcg_rd);
7858 tcg_temp_free_i64(tcg_rn);
7859 } else {
7860 TCGv_i32 tcg_rn = tcg_temp_new_i32();
7861 TCGv_i32 tcg_rd = tcg_temp_new_i32();
7862 int pass, maxpasses;
7864 if (is_scalar) {
7865 maxpasses = 1;
7866 } else {
7867 maxpasses = is_q ? 4 : 2;
7870 for (pass = 0; pass < maxpasses; pass++) {
7871 if (is_scalar) {
7872 read_vec_element_i32(s, tcg_rn, rn, pass, size);
7873 read_vec_element_i32(s, tcg_rd, rd, pass, size);
7874 } else {
7875 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
7876 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
7879 if (is_u) { /* USQADD */
7880 switch (size) {
7881 case 0:
7882 gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7883 break;
7884 case 1:
7885 gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7886 break;
7887 case 2:
7888 gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7889 break;
7890 default:
7891 g_assert_not_reached();
7893 } else { /* SUQADD */
7894 switch (size) {
7895 case 0:
7896 gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7897 break;
7898 case 1:
7899 gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7900 break;
7901 case 2:
7902 gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7903 break;
7904 default:
7905 g_assert_not_reached();
7909 if (is_scalar) {
7910 TCGv_i64 tcg_zero = tcg_const_i64(0);
7911 write_vec_element(s, tcg_zero, rd, 0, MO_64);
7912 tcg_temp_free_i64(tcg_zero);
7914 write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
7917 if (!is_q) {
7918 clear_vec_high(s, rd);
7921 tcg_temp_free_i32(tcg_rd);
7922 tcg_temp_free_i32(tcg_rn);
7926 /* C3.6.12 AdvSIMD scalar two reg misc
7927 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
7928 * +-----+---+-----------+------+-----------+--------+-----+------+------+
7929 * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
7930 * +-----+---+-----------+------+-----------+--------+-----+------+------+
7932 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
7934 int rd = extract32(insn, 0, 5);
7935 int rn = extract32(insn, 5, 5);
7936 int opcode = extract32(insn, 12, 5);
7937 int size = extract32(insn, 22, 2);
7938 bool u = extract32(insn, 29, 1);
7939 bool is_fcvt = false;
7940 int rmode;
7941 TCGv_i32 tcg_rmode;
7942 TCGv_ptr tcg_fpstatus;
7944 switch (opcode) {
7945 case 0x3: /* USQADD / SUQADD*/
7946 if (!fp_access_check(s)) {
7947 return;
7949 handle_2misc_satacc(s, true, u, false, size, rn, rd);
7950 return;
7951 case 0x7: /* SQABS / SQNEG */
7952 break;
7953 case 0xa: /* CMLT */
7954 if (u) {
7955 unallocated_encoding(s);
7956 return;
7958 /* fall through */
7959 case 0x8: /* CMGT, CMGE */
7960 case 0x9: /* CMEQ, CMLE */
7961 case 0xb: /* ABS, NEG */
7962 if (size != 3) {
7963 unallocated_encoding(s);
7964 return;
7966 break;
7967 case 0x12: /* SQXTUN */
7968 if (!u) {
7969 unallocated_encoding(s);
7970 return;
7972 /* fall through */
7973 case 0x14: /* SQXTN, UQXTN */
7974 if (size == 3) {
7975 unallocated_encoding(s);
7976 return;
7978 if (!fp_access_check(s)) {
7979 return;
7981 handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
7982 return;
7983 case 0xc ... 0xf:
7984 case 0x16 ... 0x1d:
7985 case 0x1f:
7986 /* Floating point: U, size[1] and opcode indicate operation;
7987 * size[0] indicates single or double precision.
7989 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
7990 size = extract32(size, 0, 1) ? 3 : 2;
7991 switch (opcode) {
7992 case 0x2c: /* FCMGT (zero) */
7993 case 0x2d: /* FCMEQ (zero) */
7994 case 0x2e: /* FCMLT (zero) */
7995 case 0x6c: /* FCMGE (zero) */
7996 case 0x6d: /* FCMLE (zero) */
7997 handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
7998 return;
7999 case 0x1d: /* SCVTF */
8000 case 0x5d: /* UCVTF */
8002 bool is_signed = (opcode == 0x1d);
8003 if (!fp_access_check(s)) {
8004 return;
8006 handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
8007 return;
8009 case 0x3d: /* FRECPE */
8010 case 0x3f: /* FRECPX */
8011 case 0x7d: /* FRSQRTE */
8012 if (!fp_access_check(s)) {
8013 return;
8015 handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
8016 return;
8017 case 0x1a: /* FCVTNS */
8018 case 0x1b: /* FCVTMS */
8019 case 0x3a: /* FCVTPS */
8020 case 0x3b: /* FCVTZS */
8021 case 0x5a: /* FCVTNU */
8022 case 0x5b: /* FCVTMU */
8023 case 0x7a: /* FCVTPU */
8024 case 0x7b: /* FCVTZU */
8025 is_fcvt = true;
8026 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
8027 break;
8028 case 0x1c: /* FCVTAS */
8029 case 0x5c: /* FCVTAU */
8030 /* TIEAWAY doesn't fit in the usual rounding mode encoding */
8031 is_fcvt = true;
8032 rmode = FPROUNDING_TIEAWAY;
8033 break;
8034 case 0x56: /* FCVTXN, FCVTXN2 */
8035 if (size == 2) {
8036 unallocated_encoding(s);
8037 return;
8039 if (!fp_access_check(s)) {
8040 return;
8042 handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
8043 return;
8044 default:
8045 unallocated_encoding(s);
8046 return;
8048 break;
8049 default:
8050 unallocated_encoding(s);
8051 return;
8054 if (!fp_access_check(s)) {
8055 return;
8058 if (is_fcvt) {
8059 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
8060 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
8061 tcg_fpstatus = get_fpstatus_ptr();
8062 } else {
8063 TCGV_UNUSED_I32(tcg_rmode);
8064 TCGV_UNUSED_PTR(tcg_fpstatus);
8067 if (size == 3) {
8068 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
8069 TCGv_i64 tcg_rd = tcg_temp_new_i64();
8071 handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
8072 write_fp_dreg(s, rd, tcg_rd);
8073 tcg_temp_free_i64(tcg_rd);
8074 tcg_temp_free_i64(tcg_rn);
8075 } else {
8076 TCGv_i32 tcg_rn = tcg_temp_new_i32();
8077 TCGv_i32 tcg_rd = tcg_temp_new_i32();
8079 read_vec_element_i32(s, tcg_rn, rn, 0, size);
8081 switch (opcode) {
8082 case 0x7: /* SQABS, SQNEG */
8084 NeonGenOneOpEnvFn *genfn;
8085 static NeonGenOneOpEnvFn * const fns[3][2] = {
8086 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
8087 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
8088 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
8090 genfn = fns[size][u];
8091 genfn(tcg_rd, cpu_env, tcg_rn);
8092 break;
8094 case 0x1a: /* FCVTNS */
8095 case 0x1b: /* FCVTMS */
8096 case 0x1c: /* FCVTAS */
8097 case 0x3a: /* FCVTPS */
8098 case 0x3b: /* FCVTZS */
8100 TCGv_i32 tcg_shift = tcg_const_i32(0);
8101 gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
8102 tcg_temp_free_i32(tcg_shift);
8103 break;
8105 case 0x5a: /* FCVTNU */
8106 case 0x5b: /* FCVTMU */
8107 case 0x5c: /* FCVTAU */
8108 case 0x7a: /* FCVTPU */
8109 case 0x7b: /* FCVTZU */
8111 TCGv_i32 tcg_shift = tcg_const_i32(0);
8112 gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
8113 tcg_temp_free_i32(tcg_shift);
8114 break;
8116 default:
8117 g_assert_not_reached();
8120 write_fp_sreg(s, rd, tcg_rd);
8121 tcg_temp_free_i32(tcg_rd);
8122 tcg_temp_free_i32(tcg_rn);
8125 if (is_fcvt) {
8126 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
8127 tcg_temp_free_i32(tcg_rmode);
8128 tcg_temp_free_ptr(tcg_fpstatus);
8132 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
8133 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
8134 int immh, int immb, int opcode, int rn, int rd)
8136 int size = 32 - clz32(immh) - 1;
8137 int immhb = immh << 3 | immb;
8138 int shift = 2 * (8 << size) - immhb;
8139 bool accumulate = false;
8140 bool round = false;
8141 bool insert = false;
8142 int dsize = is_q ? 128 : 64;
8143 int esize = 8 << size;
8144 int elements = dsize/esize;
8145 TCGMemOp memop = size | (is_u ? 0 : MO_SIGN);
8146 TCGv_i64 tcg_rn = new_tmp_a64(s);
8147 TCGv_i64 tcg_rd = new_tmp_a64(s);
8148 TCGv_i64 tcg_round;
8149 int i;
8151 if (extract32(immh, 3, 1) && !is_q) {
8152 unallocated_encoding(s);
8153 return;
8156 if (size > 3 && !is_q) {
8157 unallocated_encoding(s);
8158 return;
8161 if (!fp_access_check(s)) {
8162 return;
8165 switch (opcode) {
8166 case 0x02: /* SSRA / USRA (accumulate) */
8167 accumulate = true;
8168 break;
8169 case 0x04: /* SRSHR / URSHR (rounding) */
8170 round = true;
8171 break;
8172 case 0x06: /* SRSRA / URSRA (accum + rounding) */
8173 accumulate = round = true;
8174 break;
8175 case 0x08: /* SRI */
8176 insert = true;
8177 break;
8180 if (round) {
8181 uint64_t round_const = 1ULL << (shift - 1);
8182 tcg_round = tcg_const_i64(round_const);
8183 } else {
8184 TCGV_UNUSED_I64(tcg_round);
8187 for (i = 0; i < elements; i++) {
8188 read_vec_element(s, tcg_rn, rn, i, memop);
8189 if (accumulate || insert) {
8190 read_vec_element(s, tcg_rd, rd, i, memop);
8193 if (insert) {
8194 handle_shri_with_ins(tcg_rd, tcg_rn, size, shift);
8195 } else {
8196 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8197 accumulate, is_u, size, shift);
8200 write_vec_element(s, tcg_rd, rd, i, size);
8203 if (!is_q) {
8204 clear_vec_high(s, rd);
8207 if (round) {
8208 tcg_temp_free_i64(tcg_round);
8212 /* SHL/SLI - Vector shift left */
8213 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
8214 int immh, int immb, int opcode, int rn, int rd)
8216 int size = 32 - clz32(immh) - 1;
8217 int immhb = immh << 3 | immb;
8218 int shift = immhb - (8 << size);
8219 int dsize = is_q ? 128 : 64;
8220 int esize = 8 << size;
8221 int elements = dsize/esize;
8222 TCGv_i64 tcg_rn = new_tmp_a64(s);
8223 TCGv_i64 tcg_rd = new_tmp_a64(s);
8224 int i;
8226 if (extract32(immh, 3, 1) && !is_q) {
8227 unallocated_encoding(s);
8228 return;
8231 if (size > 3 && !is_q) {
8232 unallocated_encoding(s);
8233 return;
8236 if (!fp_access_check(s)) {
8237 return;
8240 for (i = 0; i < elements; i++) {
8241 read_vec_element(s, tcg_rn, rn, i, size);
8242 if (insert) {
8243 read_vec_element(s, tcg_rd, rd, i, size);
8246 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
8248 write_vec_element(s, tcg_rd, rd, i, size);
8251 if (!is_q) {
8252 clear_vec_high(s, rd);
8256 /* USHLL/SHLL - Vector shift left with widening */
8257 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
8258 int immh, int immb, int opcode, int rn, int rd)
8260 int size = 32 - clz32(immh) - 1;
8261 int immhb = immh << 3 | immb;
8262 int shift = immhb - (8 << size);
8263 int dsize = 64;
8264 int esize = 8 << size;
8265 int elements = dsize/esize;
8266 TCGv_i64 tcg_rn = new_tmp_a64(s);
8267 TCGv_i64 tcg_rd = new_tmp_a64(s);
8268 int i;
8270 if (size >= 3) {
8271 unallocated_encoding(s);
8272 return;
8275 if (!fp_access_check(s)) {
8276 return;
8279 /* For the LL variants the store is larger than the load,
8280 * so if rd == rn we would overwrite parts of our input.
8281 * So load everything right now and use shifts in the main loop.
8283 read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
8285 for (i = 0; i < elements; i++) {
8286 tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
8287 ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
8288 tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
8289 write_vec_element(s, tcg_rd, rd, i, size + 1);
8293 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
8294 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
8295 int immh, int immb, int opcode, int rn, int rd)
8297 int immhb = immh << 3 | immb;
8298 int size = 32 - clz32(immh) - 1;
8299 int dsize = 64;
8300 int esize = 8 << size;
8301 int elements = dsize/esize;
8302 int shift = (2 * esize) - immhb;
8303 bool round = extract32(opcode, 0, 1);
8304 TCGv_i64 tcg_rn, tcg_rd, tcg_final;
8305 TCGv_i64 tcg_round;
8306 int i;
8308 if (extract32(immh, 3, 1)) {
8309 unallocated_encoding(s);
8310 return;
8313 if (!fp_access_check(s)) {
8314 return;
8317 tcg_rn = tcg_temp_new_i64();
8318 tcg_rd = tcg_temp_new_i64();
8319 tcg_final = tcg_temp_new_i64();
8320 read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
8322 if (round) {
8323 uint64_t round_const = 1ULL << (shift - 1);
8324 tcg_round = tcg_const_i64(round_const);
8325 } else {
8326 TCGV_UNUSED_I64(tcg_round);
8329 for (i = 0; i < elements; i++) {
8330 read_vec_element(s, tcg_rn, rn, i, size+1);
8331 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8332 false, true, size+1, shift);
8334 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8337 if (!is_q) {
8338 clear_vec_high(s, rd);
8339 write_vec_element(s, tcg_final, rd, 0, MO_64);
8340 } else {
8341 write_vec_element(s, tcg_final, rd, 1, MO_64);
8344 if (round) {
8345 tcg_temp_free_i64(tcg_round);
8347 tcg_temp_free_i64(tcg_rn);
8348 tcg_temp_free_i64(tcg_rd);
8349 tcg_temp_free_i64(tcg_final);
8350 return;
8354 /* C3.6.14 AdvSIMD shift by immediate
8355 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
8356 * +---+---+---+-------------+------+------+--------+---+------+------+
8357 * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
8358 * +---+---+---+-------------+------+------+--------+---+------+------+
8360 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
8362 int rd = extract32(insn, 0, 5);
8363 int rn = extract32(insn, 5, 5);
8364 int opcode = extract32(insn, 11, 5);
8365 int immb = extract32(insn, 16, 3);
8366 int immh = extract32(insn, 19, 4);
8367 bool is_u = extract32(insn, 29, 1);
8368 bool is_q = extract32(insn, 30, 1);
8370 switch (opcode) {
8371 case 0x08: /* SRI */
8372 if (!is_u) {
8373 unallocated_encoding(s);
8374 return;
8376 /* fall through */
8377 case 0x00: /* SSHR / USHR */
8378 case 0x02: /* SSRA / USRA (accumulate) */
8379 case 0x04: /* SRSHR / URSHR (rounding) */
8380 case 0x06: /* SRSRA / URSRA (accum + rounding) */
8381 handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
8382 break;
8383 case 0x0a: /* SHL / SLI */
8384 handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
8385 break;
8386 case 0x10: /* SHRN */
8387 case 0x11: /* RSHRN / SQRSHRUN */
8388 if (is_u) {
8389 handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
8390 opcode, rn, rd);
8391 } else {
8392 handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
8394 break;
8395 case 0x12: /* SQSHRN / UQSHRN */
8396 case 0x13: /* SQRSHRN / UQRSHRN */
8397 handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
8398 opcode, rn, rd);
8399 break;
8400 case 0x14: /* SSHLL / USHLL */
8401 handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
8402 break;
8403 case 0x1c: /* SCVTF / UCVTF */
8404 handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
8405 opcode, rn, rd);
8406 break;
8407 case 0xc: /* SQSHLU */
8408 if (!is_u) {
8409 unallocated_encoding(s);
8410 return;
8412 handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
8413 break;
8414 case 0xe: /* SQSHL, UQSHL */
8415 handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
8416 break;
8417 case 0x1f: /* FCVTZS/ FCVTZU */
8418 handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
8419 return;
8420 default:
8421 unallocated_encoding(s);
8422 return;
8426 /* Generate code to do a "long" addition or subtraction, ie one done in
8427 * TCGv_i64 on vector lanes twice the width specified by size.
8429 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
8430 TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
8432 static NeonGenTwo64OpFn * const fns[3][2] = {
8433 { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
8434 { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
8435 { tcg_gen_add_i64, tcg_gen_sub_i64 },
8437 NeonGenTwo64OpFn *genfn;
8438 assert(size < 3);
8440 genfn = fns[size][is_sub];
8441 genfn(tcg_res, tcg_op1, tcg_op2);
8444 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
8445 int opcode, int rd, int rn, int rm)
8447 /* 3-reg-different widening insns: 64 x 64 -> 128 */
8448 TCGv_i64 tcg_res[2];
8449 int pass, accop;
8451 tcg_res[0] = tcg_temp_new_i64();
8452 tcg_res[1] = tcg_temp_new_i64();
8454 /* Does this op do an adding accumulate, a subtracting accumulate,
8455 * or no accumulate at all?
8457 switch (opcode) {
8458 case 5:
8459 case 8:
8460 case 9:
8461 accop = 1;
8462 break;
8463 case 10:
8464 case 11:
8465 accop = -1;
8466 break;
8467 default:
8468 accop = 0;
8469 break;
8472 if (accop != 0) {
8473 read_vec_element(s, tcg_res[0], rd, 0, MO_64);
8474 read_vec_element(s, tcg_res[1], rd, 1, MO_64);
8477 /* size == 2 means two 32x32->64 operations; this is worth special
8478 * casing because we can generally handle it inline.
8480 if (size == 2) {
8481 for (pass = 0; pass < 2; pass++) {
8482 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8483 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8484 TCGv_i64 tcg_passres;
8485 TCGMemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
8487 int elt = pass + is_q * 2;
8489 read_vec_element(s, tcg_op1, rn, elt, memop);
8490 read_vec_element(s, tcg_op2, rm, elt, memop);
8492 if (accop == 0) {
8493 tcg_passres = tcg_res[pass];
8494 } else {
8495 tcg_passres = tcg_temp_new_i64();
8498 switch (opcode) {
8499 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8500 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
8501 break;
8502 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8503 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
8504 break;
8505 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8506 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8508 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
8509 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
8511 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
8512 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
8513 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
8514 tcg_passres,
8515 tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
8516 tcg_temp_free_i64(tcg_tmp1);
8517 tcg_temp_free_i64(tcg_tmp2);
8518 break;
8520 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
8521 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
8522 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
8523 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
8524 break;
8525 case 9: /* SQDMLAL, SQDMLAL2 */
8526 case 11: /* SQDMLSL, SQDMLSL2 */
8527 case 13: /* SQDMULL, SQDMULL2 */
8528 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
8529 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
8530 tcg_passres, tcg_passres);
8531 break;
8532 default:
8533 g_assert_not_reached();
8536 if (opcode == 9 || opcode == 11) {
8537 /* saturating accumulate ops */
8538 if (accop < 0) {
8539 tcg_gen_neg_i64(tcg_passres, tcg_passres);
8541 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
8542 tcg_res[pass], tcg_passres);
8543 } else if (accop > 0) {
8544 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
8545 } else if (accop < 0) {
8546 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
8549 if (accop != 0) {
8550 tcg_temp_free_i64(tcg_passres);
8553 tcg_temp_free_i64(tcg_op1);
8554 tcg_temp_free_i64(tcg_op2);
8556 } else {
8557 /* size 0 or 1, generally helper functions */
8558 for (pass = 0; pass < 2; pass++) {
8559 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8560 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8561 TCGv_i64 tcg_passres;
8562 int elt = pass + is_q * 2;
8564 read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
8565 read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
8567 if (accop == 0) {
8568 tcg_passres = tcg_res[pass];
8569 } else {
8570 tcg_passres = tcg_temp_new_i64();
8573 switch (opcode) {
8574 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8575 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8577 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
8578 static NeonGenWidenFn * const widenfns[2][2] = {
8579 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
8580 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
8582 NeonGenWidenFn *widenfn = widenfns[size][is_u];
8584 widenfn(tcg_op2_64, tcg_op2);
8585 widenfn(tcg_passres, tcg_op1);
8586 gen_neon_addl(size, (opcode == 2), tcg_passres,
8587 tcg_passres, tcg_op2_64);
8588 tcg_temp_free_i64(tcg_op2_64);
8589 break;
8591 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8592 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8593 if (size == 0) {
8594 if (is_u) {
8595 gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
8596 } else {
8597 gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
8599 } else {
8600 if (is_u) {
8601 gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
8602 } else {
8603 gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
8606 break;
8607 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
8608 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
8609 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
8610 if (size == 0) {
8611 if (is_u) {
8612 gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
8613 } else {
8614 gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
8616 } else {
8617 if (is_u) {
8618 gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
8619 } else {
8620 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
8623 break;
8624 case 9: /* SQDMLAL, SQDMLAL2 */
8625 case 11: /* SQDMLSL, SQDMLSL2 */
8626 case 13: /* SQDMULL, SQDMULL2 */
8627 assert(size == 1);
8628 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
8629 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
8630 tcg_passres, tcg_passres);
8631 break;
8632 case 14: /* PMULL */
8633 assert(size == 0);
8634 gen_helper_neon_mull_p8(tcg_passres, tcg_op1, tcg_op2);
8635 break;
8636 default:
8637 g_assert_not_reached();
8639 tcg_temp_free_i32(tcg_op1);
8640 tcg_temp_free_i32(tcg_op2);
8642 if (accop != 0) {
8643 if (opcode == 9 || opcode == 11) {
8644 /* saturating accumulate ops */
8645 if (accop < 0) {
8646 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
8648 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
8649 tcg_res[pass],
8650 tcg_passres);
8651 } else {
8652 gen_neon_addl(size, (accop < 0), tcg_res[pass],
8653 tcg_res[pass], tcg_passres);
8655 tcg_temp_free_i64(tcg_passres);
8660 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
8661 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
8662 tcg_temp_free_i64(tcg_res[0]);
8663 tcg_temp_free_i64(tcg_res[1]);
8666 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
8667 int opcode, int rd, int rn, int rm)
8669 TCGv_i64 tcg_res[2];
8670 int part = is_q ? 2 : 0;
8671 int pass;
8673 for (pass = 0; pass < 2; pass++) {
8674 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8675 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8676 TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
8677 static NeonGenWidenFn * const widenfns[3][2] = {
8678 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
8679 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
8680 { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
8682 NeonGenWidenFn *widenfn = widenfns[size][is_u];
8684 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8685 read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
8686 widenfn(tcg_op2_wide, tcg_op2);
8687 tcg_temp_free_i32(tcg_op2);
8688 tcg_res[pass] = tcg_temp_new_i64();
8689 gen_neon_addl(size, (opcode == 3),
8690 tcg_res[pass], tcg_op1, tcg_op2_wide);
8691 tcg_temp_free_i64(tcg_op1);
8692 tcg_temp_free_i64(tcg_op2_wide);
8695 for (pass = 0; pass < 2; pass++) {
8696 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
8697 tcg_temp_free_i64(tcg_res[pass]);
8701 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
8703 tcg_gen_addi_i64(in, in, 1U << 31);
8704 tcg_gen_extrh_i64_i32(res, in);
8707 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
8708 int opcode, int rd, int rn, int rm)
8710 TCGv_i32 tcg_res[2];
8711 int part = is_q ? 2 : 0;
8712 int pass;
8714 for (pass = 0; pass < 2; pass++) {
8715 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8716 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8717 TCGv_i64 tcg_wideres = tcg_temp_new_i64();
8718 static NeonGenNarrowFn * const narrowfns[3][2] = {
8719 { gen_helper_neon_narrow_high_u8,
8720 gen_helper_neon_narrow_round_high_u8 },
8721 { gen_helper_neon_narrow_high_u16,
8722 gen_helper_neon_narrow_round_high_u16 },
8723 { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
8725 NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
8727 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8728 read_vec_element(s, tcg_op2, rm, pass, MO_64);
8730 gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
8732 tcg_temp_free_i64(tcg_op1);
8733 tcg_temp_free_i64(tcg_op2);
8735 tcg_res[pass] = tcg_temp_new_i32();
8736 gennarrow(tcg_res[pass], tcg_wideres);
8737 tcg_temp_free_i64(tcg_wideres);
8740 for (pass = 0; pass < 2; pass++) {
8741 write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
8742 tcg_temp_free_i32(tcg_res[pass]);
8744 if (!is_q) {
8745 clear_vec_high(s, rd);
8749 static void handle_pmull_64(DisasContext *s, int is_q, int rd, int rn, int rm)
8751 /* PMULL of 64 x 64 -> 128 is an odd special case because it
8752 * is the only three-reg-diff instruction which produces a
8753 * 128-bit wide result from a single operation. However since
8754 * it's possible to calculate the two halves more or less
8755 * separately we just use two helper calls.
8757 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8758 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8759 TCGv_i64 tcg_res = tcg_temp_new_i64();
8761 read_vec_element(s, tcg_op1, rn, is_q, MO_64);
8762 read_vec_element(s, tcg_op2, rm, is_q, MO_64);
8763 gen_helper_neon_pmull_64_lo(tcg_res, tcg_op1, tcg_op2);
8764 write_vec_element(s, tcg_res, rd, 0, MO_64);
8765 gen_helper_neon_pmull_64_hi(tcg_res, tcg_op1, tcg_op2);
8766 write_vec_element(s, tcg_res, rd, 1, MO_64);
8768 tcg_temp_free_i64(tcg_op1);
8769 tcg_temp_free_i64(tcg_op2);
8770 tcg_temp_free_i64(tcg_res);
8773 /* C3.6.15 AdvSIMD three different
8774 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
8775 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
8776 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
8777 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
8779 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
8781 /* Instructions in this group fall into three basic classes
8782 * (in each case with the operation working on each element in
8783 * the input vectors):
8784 * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
8785 * 128 bit input)
8786 * (2) wide 64 x 128 -> 128
8787 * (3) narrowing 128 x 128 -> 64
8788 * Here we do initial decode, catch unallocated cases and
8789 * dispatch to separate functions for each class.
8791 int is_q = extract32(insn, 30, 1);
8792 int is_u = extract32(insn, 29, 1);
8793 int size = extract32(insn, 22, 2);
8794 int opcode = extract32(insn, 12, 4);
8795 int rm = extract32(insn, 16, 5);
8796 int rn = extract32(insn, 5, 5);
8797 int rd = extract32(insn, 0, 5);
8799 switch (opcode) {
8800 case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
8801 case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
8802 /* 64 x 128 -> 128 */
8803 if (size == 3) {
8804 unallocated_encoding(s);
8805 return;
8807 if (!fp_access_check(s)) {
8808 return;
8810 handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
8811 break;
8812 case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
8813 case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
8814 /* 128 x 128 -> 64 */
8815 if (size == 3) {
8816 unallocated_encoding(s);
8817 return;
8819 if (!fp_access_check(s)) {
8820 return;
8822 handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
8823 break;
8824 case 14: /* PMULL, PMULL2 */
8825 if (is_u || size == 1 || size == 2) {
8826 unallocated_encoding(s);
8827 return;
8829 if (size == 3) {
8830 if (!arm_dc_feature(s, ARM_FEATURE_V8_PMULL)) {
8831 unallocated_encoding(s);
8832 return;
8834 if (!fp_access_check(s)) {
8835 return;
8837 handle_pmull_64(s, is_q, rd, rn, rm);
8838 return;
8840 goto is_widening;
8841 case 9: /* SQDMLAL, SQDMLAL2 */
8842 case 11: /* SQDMLSL, SQDMLSL2 */
8843 case 13: /* SQDMULL, SQDMULL2 */
8844 if (is_u || size == 0) {
8845 unallocated_encoding(s);
8846 return;
8848 /* fall through */
8849 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8850 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8851 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8852 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8853 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
8854 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
8855 case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
8856 /* 64 x 64 -> 128 */
8857 if (size == 3) {
8858 unallocated_encoding(s);
8859 return;
8861 is_widening:
8862 if (!fp_access_check(s)) {
8863 return;
8866 handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
8867 break;
8868 default:
8869 /* opcode 15 not allocated */
8870 unallocated_encoding(s);
8871 break;
8875 /* Logic op (opcode == 3) subgroup of C3.6.16. */
8876 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
8878 int rd = extract32(insn, 0, 5);
8879 int rn = extract32(insn, 5, 5);
8880 int rm = extract32(insn, 16, 5);
8881 int size = extract32(insn, 22, 2);
8882 bool is_u = extract32(insn, 29, 1);
8883 bool is_q = extract32(insn, 30, 1);
8884 TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
8885 int pass;
8887 if (!fp_access_check(s)) {
8888 return;
8891 tcg_op1 = tcg_temp_new_i64();
8892 tcg_op2 = tcg_temp_new_i64();
8893 tcg_res[0] = tcg_temp_new_i64();
8894 tcg_res[1] = tcg_temp_new_i64();
8896 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
8897 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8898 read_vec_element(s, tcg_op2, rm, pass, MO_64);
8900 if (!is_u) {
8901 switch (size) {
8902 case 0: /* AND */
8903 tcg_gen_and_i64(tcg_res[pass], tcg_op1, tcg_op2);
8904 break;
8905 case 1: /* BIC */
8906 tcg_gen_andc_i64(tcg_res[pass], tcg_op1, tcg_op2);
8907 break;
8908 case 2: /* ORR */
8909 tcg_gen_or_i64(tcg_res[pass], tcg_op1, tcg_op2);
8910 break;
8911 case 3: /* ORN */
8912 tcg_gen_orc_i64(tcg_res[pass], tcg_op1, tcg_op2);
8913 break;
8915 } else {
8916 if (size != 0) {
8917 /* B* ops need res loaded to operate on */
8918 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
8921 switch (size) {
8922 case 0: /* EOR */
8923 tcg_gen_xor_i64(tcg_res[pass], tcg_op1, tcg_op2);
8924 break;
8925 case 1: /* BSL bitwise select */
8926 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_op2);
8927 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_res[pass]);
8928 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op1);
8929 break;
8930 case 2: /* BIT, bitwise insert if true */
8931 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
8932 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_op2);
8933 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
8934 break;
8935 case 3: /* BIF, bitwise insert if false */
8936 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
8937 tcg_gen_andc_i64(tcg_op1, tcg_op1, tcg_op2);
8938 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
8939 break;
8944 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
8945 if (!is_q) {
8946 tcg_gen_movi_i64(tcg_res[1], 0);
8948 write_vec_element(s, tcg_res[1], 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[0]);
8953 tcg_temp_free_i64(tcg_res[1]);
8956 /* Helper functions for 32 bit comparisons */
8957 static void gen_max_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
8959 tcg_gen_movcond_i32(TCG_COND_GE, res, op1, op2, op1, op2);
8962 static void gen_max_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
8964 tcg_gen_movcond_i32(TCG_COND_GEU, res, op1, op2, op1, op2);
8967 static void gen_min_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
8969 tcg_gen_movcond_i32(TCG_COND_LE, res, op1, op2, op1, op2);
8972 static void gen_min_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
8974 tcg_gen_movcond_i32(TCG_COND_LEU, res, op1, op2, op1, op2);
8977 /* Pairwise op subgroup of C3.6.16.
8979 * This is called directly or via the handle_3same_float for float pairwise
8980 * operations where the opcode and size are calculated differently.
8982 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
8983 int size, int rn, int rm, int rd)
8985 TCGv_ptr fpst;
8986 int pass;
8988 /* Floating point operations need fpst */
8989 if (opcode >= 0x58) {
8990 fpst = get_fpstatus_ptr();
8991 } else {
8992 TCGV_UNUSED_PTR(fpst);
8995 if (!fp_access_check(s)) {
8996 return;
8999 /* These operations work on the concatenated rm:rn, with each pair of
9000 * adjacent elements being operated on to produce an element in the result.
9002 if (size == 3) {
9003 TCGv_i64 tcg_res[2];
9005 for (pass = 0; pass < 2; pass++) {
9006 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9007 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9008 int passreg = (pass == 0) ? rn : rm;
9010 read_vec_element(s, tcg_op1, passreg, 0, MO_64);
9011 read_vec_element(s, tcg_op2, passreg, 1, MO_64);
9012 tcg_res[pass] = tcg_temp_new_i64();
9014 switch (opcode) {
9015 case 0x17: /* ADDP */
9016 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
9017 break;
9018 case 0x58: /* FMAXNMP */
9019 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9020 break;
9021 case 0x5a: /* FADDP */
9022 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9023 break;
9024 case 0x5e: /* FMAXP */
9025 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9026 break;
9027 case 0x78: /* FMINNMP */
9028 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9029 break;
9030 case 0x7e: /* FMINP */
9031 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9032 break;
9033 default:
9034 g_assert_not_reached();
9037 tcg_temp_free_i64(tcg_op1);
9038 tcg_temp_free_i64(tcg_op2);
9041 for (pass = 0; pass < 2; pass++) {
9042 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9043 tcg_temp_free_i64(tcg_res[pass]);
9045 } else {
9046 int maxpass = is_q ? 4 : 2;
9047 TCGv_i32 tcg_res[4];
9049 for (pass = 0; pass < maxpass; pass++) {
9050 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9051 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9052 NeonGenTwoOpFn *genfn = NULL;
9053 int passreg = pass < (maxpass / 2) ? rn : rm;
9054 int passelt = (is_q && (pass & 1)) ? 2 : 0;
9056 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
9057 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
9058 tcg_res[pass] = tcg_temp_new_i32();
9060 switch (opcode) {
9061 case 0x17: /* ADDP */
9063 static NeonGenTwoOpFn * const fns[3] = {
9064 gen_helper_neon_padd_u8,
9065 gen_helper_neon_padd_u16,
9066 tcg_gen_add_i32,
9068 genfn = fns[size];
9069 break;
9071 case 0x14: /* SMAXP, UMAXP */
9073 static NeonGenTwoOpFn * const fns[3][2] = {
9074 { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
9075 { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
9076 { gen_max_s32, gen_max_u32 },
9078 genfn = fns[size][u];
9079 break;
9081 case 0x15: /* SMINP, UMINP */
9083 static NeonGenTwoOpFn * const fns[3][2] = {
9084 { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
9085 { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
9086 { gen_min_s32, gen_min_u32 },
9088 genfn = fns[size][u];
9089 break;
9091 /* The FP operations are all on single floats (32 bit) */
9092 case 0x58: /* FMAXNMP */
9093 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9094 break;
9095 case 0x5a: /* FADDP */
9096 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9097 break;
9098 case 0x5e: /* FMAXP */
9099 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9100 break;
9101 case 0x78: /* FMINNMP */
9102 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9103 break;
9104 case 0x7e: /* FMINP */
9105 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9106 break;
9107 default:
9108 g_assert_not_reached();
9111 /* FP ops called directly, otherwise call now */
9112 if (genfn) {
9113 genfn(tcg_res[pass], tcg_op1, tcg_op2);
9116 tcg_temp_free_i32(tcg_op1);
9117 tcg_temp_free_i32(tcg_op2);
9120 for (pass = 0; pass < maxpass; pass++) {
9121 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
9122 tcg_temp_free_i32(tcg_res[pass]);
9124 if (!is_q) {
9125 clear_vec_high(s, rd);
9129 if (!TCGV_IS_UNUSED_PTR(fpst)) {
9130 tcg_temp_free_ptr(fpst);
9134 /* Floating point op subgroup of C3.6.16. */
9135 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
9137 /* For floating point ops, the U, size[1] and opcode bits
9138 * together indicate the operation. size[0] indicates single
9139 * or double.
9141 int fpopcode = extract32(insn, 11, 5)
9142 | (extract32(insn, 23, 1) << 5)
9143 | (extract32(insn, 29, 1) << 6);
9144 int is_q = extract32(insn, 30, 1);
9145 int size = extract32(insn, 22, 1);
9146 int rm = extract32(insn, 16, 5);
9147 int rn = extract32(insn, 5, 5);
9148 int rd = extract32(insn, 0, 5);
9150 int datasize = is_q ? 128 : 64;
9151 int esize = 32 << size;
9152 int elements = datasize / esize;
9154 if (size == 1 && !is_q) {
9155 unallocated_encoding(s);
9156 return;
9159 switch (fpopcode) {
9160 case 0x58: /* FMAXNMP */
9161 case 0x5a: /* FADDP */
9162 case 0x5e: /* FMAXP */
9163 case 0x78: /* FMINNMP */
9164 case 0x7e: /* FMINP */
9165 if (size && !is_q) {
9166 unallocated_encoding(s);
9167 return;
9169 handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
9170 rn, rm, rd);
9171 return;
9172 case 0x1b: /* FMULX */
9173 case 0x1f: /* FRECPS */
9174 case 0x3f: /* FRSQRTS */
9175 case 0x5d: /* FACGE */
9176 case 0x7d: /* FACGT */
9177 case 0x19: /* FMLA */
9178 case 0x39: /* FMLS */
9179 case 0x18: /* FMAXNM */
9180 case 0x1a: /* FADD */
9181 case 0x1c: /* FCMEQ */
9182 case 0x1e: /* FMAX */
9183 case 0x38: /* FMINNM */
9184 case 0x3a: /* FSUB */
9185 case 0x3e: /* FMIN */
9186 case 0x5b: /* FMUL */
9187 case 0x5c: /* FCMGE */
9188 case 0x5f: /* FDIV */
9189 case 0x7a: /* FABD */
9190 case 0x7c: /* FCMGT */
9191 if (!fp_access_check(s)) {
9192 return;
9195 handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
9196 return;
9197 default:
9198 unallocated_encoding(s);
9199 return;
9203 /* Integer op subgroup of C3.6.16. */
9204 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
9206 int is_q = extract32(insn, 30, 1);
9207 int u = extract32(insn, 29, 1);
9208 int size = extract32(insn, 22, 2);
9209 int opcode = extract32(insn, 11, 5);
9210 int rm = extract32(insn, 16, 5);
9211 int rn = extract32(insn, 5, 5);
9212 int rd = extract32(insn, 0, 5);
9213 int pass;
9215 switch (opcode) {
9216 case 0x13: /* MUL, PMUL */
9217 if (u && size != 0) {
9218 unallocated_encoding(s);
9219 return;
9221 /* fall through */
9222 case 0x0: /* SHADD, UHADD */
9223 case 0x2: /* SRHADD, URHADD */
9224 case 0x4: /* SHSUB, UHSUB */
9225 case 0xc: /* SMAX, UMAX */
9226 case 0xd: /* SMIN, UMIN */
9227 case 0xe: /* SABD, UABD */
9228 case 0xf: /* SABA, UABA */
9229 case 0x12: /* MLA, MLS */
9230 if (size == 3) {
9231 unallocated_encoding(s);
9232 return;
9234 break;
9235 case 0x16: /* SQDMULH, SQRDMULH */
9236 if (size == 0 || size == 3) {
9237 unallocated_encoding(s);
9238 return;
9240 break;
9241 default:
9242 if (size == 3 && !is_q) {
9243 unallocated_encoding(s);
9244 return;
9246 break;
9249 if (!fp_access_check(s)) {
9250 return;
9253 if (size == 3) {
9254 assert(is_q);
9255 for (pass = 0; pass < 2; pass++) {
9256 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9257 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9258 TCGv_i64 tcg_res = tcg_temp_new_i64();
9260 read_vec_element(s, tcg_op1, rn, pass, MO_64);
9261 read_vec_element(s, tcg_op2, rm, pass, MO_64);
9263 handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
9265 write_vec_element(s, tcg_res, rd, pass, MO_64);
9267 tcg_temp_free_i64(tcg_res);
9268 tcg_temp_free_i64(tcg_op1);
9269 tcg_temp_free_i64(tcg_op2);
9271 } else {
9272 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
9273 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9274 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9275 TCGv_i32 tcg_res = tcg_temp_new_i32();
9276 NeonGenTwoOpFn *genfn = NULL;
9277 NeonGenTwoOpEnvFn *genenvfn = NULL;
9279 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9280 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9282 switch (opcode) {
9283 case 0x0: /* SHADD, UHADD */
9285 static NeonGenTwoOpFn * const fns[3][2] = {
9286 { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
9287 { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
9288 { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
9290 genfn = fns[size][u];
9291 break;
9293 case 0x1: /* SQADD, UQADD */
9295 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9296 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9297 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9298 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9300 genenvfn = fns[size][u];
9301 break;
9303 case 0x2: /* SRHADD, URHADD */
9305 static NeonGenTwoOpFn * const fns[3][2] = {
9306 { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
9307 { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
9308 { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
9310 genfn = fns[size][u];
9311 break;
9313 case 0x4: /* SHSUB, UHSUB */
9315 static NeonGenTwoOpFn * const fns[3][2] = {
9316 { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
9317 { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
9318 { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
9320 genfn = fns[size][u];
9321 break;
9323 case 0x5: /* SQSUB, UQSUB */
9325 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9326 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9327 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9328 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9330 genenvfn = fns[size][u];
9331 break;
9333 case 0x6: /* CMGT, CMHI */
9335 static NeonGenTwoOpFn * const fns[3][2] = {
9336 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_u8 },
9337 { gen_helper_neon_cgt_s16, gen_helper_neon_cgt_u16 },
9338 { gen_helper_neon_cgt_s32, gen_helper_neon_cgt_u32 },
9340 genfn = fns[size][u];
9341 break;
9343 case 0x7: /* CMGE, CMHS */
9345 static NeonGenTwoOpFn * const fns[3][2] = {
9346 { gen_helper_neon_cge_s8, gen_helper_neon_cge_u8 },
9347 { gen_helper_neon_cge_s16, gen_helper_neon_cge_u16 },
9348 { gen_helper_neon_cge_s32, gen_helper_neon_cge_u32 },
9350 genfn = fns[size][u];
9351 break;
9353 case 0x8: /* SSHL, USHL */
9355 static NeonGenTwoOpFn * const fns[3][2] = {
9356 { gen_helper_neon_shl_s8, gen_helper_neon_shl_u8 },
9357 { gen_helper_neon_shl_s16, gen_helper_neon_shl_u16 },
9358 { gen_helper_neon_shl_s32, gen_helper_neon_shl_u32 },
9360 genfn = fns[size][u];
9361 break;
9363 case 0x9: /* SQSHL, UQSHL */
9365 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9366 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9367 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9368 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9370 genenvfn = fns[size][u];
9371 break;
9373 case 0xa: /* SRSHL, URSHL */
9375 static NeonGenTwoOpFn * const fns[3][2] = {
9376 { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
9377 { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
9378 { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
9380 genfn = fns[size][u];
9381 break;
9383 case 0xb: /* SQRSHL, UQRSHL */
9385 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9386 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9387 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9388 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9390 genenvfn = fns[size][u];
9391 break;
9393 case 0xc: /* SMAX, UMAX */
9395 static NeonGenTwoOpFn * const fns[3][2] = {
9396 { gen_helper_neon_max_s8, gen_helper_neon_max_u8 },
9397 { gen_helper_neon_max_s16, gen_helper_neon_max_u16 },
9398 { gen_max_s32, gen_max_u32 },
9400 genfn = fns[size][u];
9401 break;
9404 case 0xd: /* SMIN, UMIN */
9406 static NeonGenTwoOpFn * const fns[3][2] = {
9407 { gen_helper_neon_min_s8, gen_helper_neon_min_u8 },
9408 { gen_helper_neon_min_s16, gen_helper_neon_min_u16 },
9409 { gen_min_s32, gen_min_u32 },
9411 genfn = fns[size][u];
9412 break;
9414 case 0xe: /* SABD, UABD */
9415 case 0xf: /* SABA, UABA */
9417 static NeonGenTwoOpFn * const fns[3][2] = {
9418 { gen_helper_neon_abd_s8, gen_helper_neon_abd_u8 },
9419 { gen_helper_neon_abd_s16, gen_helper_neon_abd_u16 },
9420 { gen_helper_neon_abd_s32, gen_helper_neon_abd_u32 },
9422 genfn = fns[size][u];
9423 break;
9425 case 0x10: /* ADD, SUB */
9427 static NeonGenTwoOpFn * const fns[3][2] = {
9428 { gen_helper_neon_add_u8, gen_helper_neon_sub_u8 },
9429 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
9430 { tcg_gen_add_i32, tcg_gen_sub_i32 },
9432 genfn = fns[size][u];
9433 break;
9435 case 0x11: /* CMTST, CMEQ */
9437 static NeonGenTwoOpFn * const fns[3][2] = {
9438 { gen_helper_neon_tst_u8, gen_helper_neon_ceq_u8 },
9439 { gen_helper_neon_tst_u16, gen_helper_neon_ceq_u16 },
9440 { gen_helper_neon_tst_u32, gen_helper_neon_ceq_u32 },
9442 genfn = fns[size][u];
9443 break;
9445 case 0x13: /* MUL, PMUL */
9446 if (u) {
9447 /* PMUL */
9448 assert(size == 0);
9449 genfn = gen_helper_neon_mul_p8;
9450 break;
9452 /* fall through : MUL */
9453 case 0x12: /* MLA, MLS */
9455 static NeonGenTwoOpFn * const fns[3] = {
9456 gen_helper_neon_mul_u8,
9457 gen_helper_neon_mul_u16,
9458 tcg_gen_mul_i32,
9460 genfn = fns[size];
9461 break;
9463 case 0x16: /* SQDMULH, SQRDMULH */
9465 static NeonGenTwoOpEnvFn * const fns[2][2] = {
9466 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9467 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9469 assert(size == 1 || size == 2);
9470 genenvfn = fns[size - 1][u];
9471 break;
9473 default:
9474 g_assert_not_reached();
9477 if (genenvfn) {
9478 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
9479 } else {
9480 genfn(tcg_res, tcg_op1, tcg_op2);
9483 if (opcode == 0xf || opcode == 0x12) {
9484 /* SABA, UABA, MLA, MLS: accumulating ops */
9485 static NeonGenTwoOpFn * const fns[3][2] = {
9486 { gen_helper_neon_add_u8, gen_helper_neon_sub_u8 },
9487 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
9488 { tcg_gen_add_i32, tcg_gen_sub_i32 },
9490 bool is_sub = (opcode == 0x12 && u); /* MLS */
9492 genfn = fns[size][is_sub];
9493 read_vec_element_i32(s, tcg_op1, rd, pass, MO_32);
9494 genfn(tcg_res, tcg_op1, tcg_res);
9497 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9499 tcg_temp_free_i32(tcg_res);
9500 tcg_temp_free_i32(tcg_op1);
9501 tcg_temp_free_i32(tcg_op2);
9505 if (!is_q) {
9506 clear_vec_high(s, rd);
9510 /* C3.6.16 AdvSIMD three same
9511 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
9512 * +---+---+---+-----------+------+---+------+--------+---+------+------+
9513 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
9514 * +---+---+---+-----------+------+---+------+--------+---+------+------+
9516 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
9518 int opcode = extract32(insn, 11, 5);
9520 switch (opcode) {
9521 case 0x3: /* logic ops */
9522 disas_simd_3same_logic(s, insn);
9523 break;
9524 case 0x17: /* ADDP */
9525 case 0x14: /* SMAXP, UMAXP */
9526 case 0x15: /* SMINP, UMINP */
9528 /* Pairwise operations */
9529 int is_q = extract32(insn, 30, 1);
9530 int u = extract32(insn, 29, 1);
9531 int size = extract32(insn, 22, 2);
9532 int rm = extract32(insn, 16, 5);
9533 int rn = extract32(insn, 5, 5);
9534 int rd = extract32(insn, 0, 5);
9535 if (opcode == 0x17) {
9536 if (u || (size == 3 && !is_q)) {
9537 unallocated_encoding(s);
9538 return;
9540 } else {
9541 if (size == 3) {
9542 unallocated_encoding(s);
9543 return;
9546 handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
9547 break;
9549 case 0x18 ... 0x31:
9550 /* floating point ops, sz[1] and U are part of opcode */
9551 disas_simd_3same_float(s, insn);
9552 break;
9553 default:
9554 disas_simd_3same_int(s, insn);
9555 break;
9559 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
9560 int size, int rn, int rd)
9562 /* Handle 2-reg-misc ops which are widening (so each size element
9563 * in the source becomes a 2*size element in the destination.
9564 * The only instruction like this is FCVTL.
9566 int pass;
9568 if (size == 3) {
9569 /* 32 -> 64 bit fp conversion */
9570 TCGv_i64 tcg_res[2];
9571 int srcelt = is_q ? 2 : 0;
9573 for (pass = 0; pass < 2; pass++) {
9574 TCGv_i32 tcg_op = tcg_temp_new_i32();
9575 tcg_res[pass] = tcg_temp_new_i64();
9577 read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
9578 gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
9579 tcg_temp_free_i32(tcg_op);
9581 for (pass = 0; pass < 2; pass++) {
9582 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9583 tcg_temp_free_i64(tcg_res[pass]);
9585 } else {
9586 /* 16 -> 32 bit fp conversion */
9587 int srcelt = is_q ? 4 : 0;
9588 TCGv_i32 tcg_res[4];
9590 for (pass = 0; pass < 4; pass++) {
9591 tcg_res[pass] = tcg_temp_new_i32();
9593 read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
9594 gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
9595 cpu_env);
9597 for (pass = 0; pass < 4; pass++) {
9598 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
9599 tcg_temp_free_i32(tcg_res[pass]);
9604 static void handle_rev(DisasContext *s, int opcode, bool u,
9605 bool is_q, int size, int rn, int rd)
9607 int op = (opcode << 1) | u;
9608 int opsz = op + size;
9609 int grp_size = 3 - opsz;
9610 int dsize = is_q ? 128 : 64;
9611 int i;
9613 if (opsz >= 3) {
9614 unallocated_encoding(s);
9615 return;
9618 if (!fp_access_check(s)) {
9619 return;
9622 if (size == 0) {
9623 /* Special case bytes, use bswap op on each group of elements */
9624 int groups = dsize / (8 << grp_size);
9626 for (i = 0; i < groups; i++) {
9627 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9629 read_vec_element(s, tcg_tmp, rn, i, grp_size);
9630 switch (grp_size) {
9631 case MO_16:
9632 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
9633 break;
9634 case MO_32:
9635 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
9636 break;
9637 case MO_64:
9638 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
9639 break;
9640 default:
9641 g_assert_not_reached();
9643 write_vec_element(s, tcg_tmp, rd, i, grp_size);
9644 tcg_temp_free_i64(tcg_tmp);
9646 if (!is_q) {
9647 clear_vec_high(s, rd);
9649 } else {
9650 int revmask = (1 << grp_size) - 1;
9651 int esize = 8 << size;
9652 int elements = dsize / esize;
9653 TCGv_i64 tcg_rn = tcg_temp_new_i64();
9654 TCGv_i64 tcg_rd = tcg_const_i64(0);
9655 TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
9657 for (i = 0; i < elements; i++) {
9658 int e_rev = (i & 0xf) ^ revmask;
9659 int off = e_rev * esize;
9660 read_vec_element(s, tcg_rn, rn, i, size);
9661 if (off >= 64) {
9662 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
9663 tcg_rn, off - 64, esize);
9664 } else {
9665 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
9668 write_vec_element(s, tcg_rd, rd, 0, MO_64);
9669 write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
9671 tcg_temp_free_i64(tcg_rd_hi);
9672 tcg_temp_free_i64(tcg_rd);
9673 tcg_temp_free_i64(tcg_rn);
9677 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
9678 bool is_q, int size, int rn, int rd)
9680 /* Implement the pairwise operations from 2-misc:
9681 * SADDLP, UADDLP, SADALP, UADALP.
9682 * These all add pairs of elements in the input to produce a
9683 * double-width result element in the output (possibly accumulating).
9685 bool accum = (opcode == 0x6);
9686 int maxpass = is_q ? 2 : 1;
9687 int pass;
9688 TCGv_i64 tcg_res[2];
9690 if (size == 2) {
9691 /* 32 + 32 -> 64 op */
9692 TCGMemOp memop = size + (u ? 0 : MO_SIGN);
9694 for (pass = 0; pass < maxpass; pass++) {
9695 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9696 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9698 tcg_res[pass] = tcg_temp_new_i64();
9700 read_vec_element(s, tcg_op1, rn, pass * 2, memop);
9701 read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
9702 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
9703 if (accum) {
9704 read_vec_element(s, tcg_op1, rd, pass, MO_64);
9705 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
9708 tcg_temp_free_i64(tcg_op1);
9709 tcg_temp_free_i64(tcg_op2);
9711 } else {
9712 for (pass = 0; pass < maxpass; pass++) {
9713 TCGv_i64 tcg_op = tcg_temp_new_i64();
9714 NeonGenOneOpFn *genfn;
9715 static NeonGenOneOpFn * const fns[2][2] = {
9716 { gen_helper_neon_addlp_s8, gen_helper_neon_addlp_u8 },
9717 { gen_helper_neon_addlp_s16, gen_helper_neon_addlp_u16 },
9720 genfn = fns[size][u];
9722 tcg_res[pass] = tcg_temp_new_i64();
9724 read_vec_element(s, tcg_op, rn, pass, MO_64);
9725 genfn(tcg_res[pass], tcg_op);
9727 if (accum) {
9728 read_vec_element(s, tcg_op, rd, pass, MO_64);
9729 if (size == 0) {
9730 gen_helper_neon_addl_u16(tcg_res[pass],
9731 tcg_res[pass], tcg_op);
9732 } else {
9733 gen_helper_neon_addl_u32(tcg_res[pass],
9734 tcg_res[pass], tcg_op);
9737 tcg_temp_free_i64(tcg_op);
9740 if (!is_q) {
9741 tcg_res[1] = tcg_const_i64(0);
9743 for (pass = 0; pass < 2; pass++) {
9744 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9745 tcg_temp_free_i64(tcg_res[pass]);
9749 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
9751 /* Implement SHLL and SHLL2 */
9752 int pass;
9753 int part = is_q ? 2 : 0;
9754 TCGv_i64 tcg_res[2];
9756 for (pass = 0; pass < 2; pass++) {
9757 static NeonGenWidenFn * const widenfns[3] = {
9758 gen_helper_neon_widen_u8,
9759 gen_helper_neon_widen_u16,
9760 tcg_gen_extu_i32_i64,
9762 NeonGenWidenFn *widenfn = widenfns[size];
9763 TCGv_i32 tcg_op = tcg_temp_new_i32();
9765 read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
9766 tcg_res[pass] = tcg_temp_new_i64();
9767 widenfn(tcg_res[pass], tcg_op);
9768 tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
9770 tcg_temp_free_i32(tcg_op);
9773 for (pass = 0; pass < 2; pass++) {
9774 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9775 tcg_temp_free_i64(tcg_res[pass]);
9779 /* C3.6.17 AdvSIMD two reg misc
9780 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
9781 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
9782 * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
9783 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
9785 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
9787 int size = extract32(insn, 22, 2);
9788 int opcode = extract32(insn, 12, 5);
9789 bool u = extract32(insn, 29, 1);
9790 bool is_q = extract32(insn, 30, 1);
9791 int rn = extract32(insn, 5, 5);
9792 int rd = extract32(insn, 0, 5);
9793 bool need_fpstatus = false;
9794 bool need_rmode = false;
9795 int rmode = -1;
9796 TCGv_i32 tcg_rmode;
9797 TCGv_ptr tcg_fpstatus;
9799 switch (opcode) {
9800 case 0x0: /* REV64, REV32 */
9801 case 0x1: /* REV16 */
9802 handle_rev(s, opcode, u, is_q, size, rn, rd);
9803 return;
9804 case 0x5: /* CNT, NOT, RBIT */
9805 if (u && size == 0) {
9806 /* NOT: adjust size so we can use the 64-bits-at-a-time loop. */
9807 size = 3;
9808 break;
9809 } else if (u && size == 1) {
9810 /* RBIT */
9811 break;
9812 } else if (!u && size == 0) {
9813 /* CNT */
9814 break;
9816 unallocated_encoding(s);
9817 return;
9818 case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
9819 case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
9820 if (size == 3) {
9821 unallocated_encoding(s);
9822 return;
9824 if (!fp_access_check(s)) {
9825 return;
9828 handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
9829 return;
9830 case 0x4: /* CLS, CLZ */
9831 if (size == 3) {
9832 unallocated_encoding(s);
9833 return;
9835 break;
9836 case 0x2: /* SADDLP, UADDLP */
9837 case 0x6: /* SADALP, UADALP */
9838 if (size == 3) {
9839 unallocated_encoding(s);
9840 return;
9842 if (!fp_access_check(s)) {
9843 return;
9845 handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
9846 return;
9847 case 0x13: /* SHLL, SHLL2 */
9848 if (u == 0 || size == 3) {
9849 unallocated_encoding(s);
9850 return;
9852 if (!fp_access_check(s)) {
9853 return;
9855 handle_shll(s, is_q, size, rn, rd);
9856 return;
9857 case 0xa: /* CMLT */
9858 if (u == 1) {
9859 unallocated_encoding(s);
9860 return;
9862 /* fall through */
9863 case 0x8: /* CMGT, CMGE */
9864 case 0x9: /* CMEQ, CMLE */
9865 case 0xb: /* ABS, NEG */
9866 if (size == 3 && !is_q) {
9867 unallocated_encoding(s);
9868 return;
9870 break;
9871 case 0x3: /* SUQADD, USQADD */
9872 if (size == 3 && !is_q) {
9873 unallocated_encoding(s);
9874 return;
9876 if (!fp_access_check(s)) {
9877 return;
9879 handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
9880 return;
9881 case 0x7: /* SQABS, SQNEG */
9882 if (size == 3 && !is_q) {
9883 unallocated_encoding(s);
9884 return;
9886 break;
9887 case 0xc ... 0xf:
9888 case 0x16 ... 0x1d:
9889 case 0x1f:
9891 /* Floating point: U, size[1] and opcode indicate operation;
9892 * size[0] indicates single or double precision.
9894 int is_double = extract32(size, 0, 1);
9895 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
9896 size = is_double ? 3 : 2;
9897 switch (opcode) {
9898 case 0x2f: /* FABS */
9899 case 0x6f: /* FNEG */
9900 if (size == 3 && !is_q) {
9901 unallocated_encoding(s);
9902 return;
9904 break;
9905 case 0x1d: /* SCVTF */
9906 case 0x5d: /* UCVTF */
9908 bool is_signed = (opcode == 0x1d) ? true : false;
9909 int elements = is_double ? 2 : is_q ? 4 : 2;
9910 if (is_double && !is_q) {
9911 unallocated_encoding(s);
9912 return;
9914 if (!fp_access_check(s)) {
9915 return;
9917 handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
9918 return;
9920 case 0x2c: /* FCMGT (zero) */
9921 case 0x2d: /* FCMEQ (zero) */
9922 case 0x2e: /* FCMLT (zero) */
9923 case 0x6c: /* FCMGE (zero) */
9924 case 0x6d: /* FCMLE (zero) */
9925 if (size == 3 && !is_q) {
9926 unallocated_encoding(s);
9927 return;
9929 handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
9930 return;
9931 case 0x7f: /* FSQRT */
9932 if (size == 3 && !is_q) {
9933 unallocated_encoding(s);
9934 return;
9936 break;
9937 case 0x1a: /* FCVTNS */
9938 case 0x1b: /* FCVTMS */
9939 case 0x3a: /* FCVTPS */
9940 case 0x3b: /* FCVTZS */
9941 case 0x5a: /* FCVTNU */
9942 case 0x5b: /* FCVTMU */
9943 case 0x7a: /* FCVTPU */
9944 case 0x7b: /* FCVTZU */
9945 need_fpstatus = true;
9946 need_rmode = true;
9947 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
9948 if (size == 3 && !is_q) {
9949 unallocated_encoding(s);
9950 return;
9952 break;
9953 case 0x5c: /* FCVTAU */
9954 case 0x1c: /* FCVTAS */
9955 need_fpstatus = true;
9956 need_rmode = true;
9957 rmode = FPROUNDING_TIEAWAY;
9958 if (size == 3 && !is_q) {
9959 unallocated_encoding(s);
9960 return;
9962 break;
9963 case 0x3c: /* URECPE */
9964 if (size == 3) {
9965 unallocated_encoding(s);
9966 return;
9968 /* fall through */
9969 case 0x3d: /* FRECPE */
9970 case 0x7d: /* FRSQRTE */
9971 if (size == 3 && !is_q) {
9972 unallocated_encoding(s);
9973 return;
9975 if (!fp_access_check(s)) {
9976 return;
9978 handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
9979 return;
9980 case 0x56: /* FCVTXN, FCVTXN2 */
9981 if (size == 2) {
9982 unallocated_encoding(s);
9983 return;
9985 /* fall through */
9986 case 0x16: /* FCVTN, FCVTN2 */
9987 /* handle_2misc_narrow does a 2*size -> size operation, but these
9988 * instructions encode the source size rather than dest size.
9990 if (!fp_access_check(s)) {
9991 return;
9993 handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
9994 return;
9995 case 0x17: /* FCVTL, FCVTL2 */
9996 if (!fp_access_check(s)) {
9997 return;
9999 handle_2misc_widening(s, opcode, is_q, size, rn, rd);
10000 return;
10001 case 0x18: /* FRINTN */
10002 case 0x19: /* FRINTM */
10003 case 0x38: /* FRINTP */
10004 case 0x39: /* FRINTZ */
10005 need_rmode = true;
10006 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10007 /* fall through */
10008 case 0x59: /* FRINTX */
10009 case 0x79: /* FRINTI */
10010 need_fpstatus = true;
10011 if (size == 3 && !is_q) {
10012 unallocated_encoding(s);
10013 return;
10015 break;
10016 case 0x58: /* FRINTA */
10017 need_rmode = true;
10018 rmode = FPROUNDING_TIEAWAY;
10019 need_fpstatus = true;
10020 if (size == 3 && !is_q) {
10021 unallocated_encoding(s);
10022 return;
10024 break;
10025 case 0x7c: /* URSQRTE */
10026 if (size == 3) {
10027 unallocated_encoding(s);
10028 return;
10030 need_fpstatus = true;
10031 break;
10032 default:
10033 unallocated_encoding(s);
10034 return;
10036 break;
10038 default:
10039 unallocated_encoding(s);
10040 return;
10043 if (!fp_access_check(s)) {
10044 return;
10047 if (need_fpstatus) {
10048 tcg_fpstatus = get_fpstatus_ptr();
10049 } else {
10050 TCGV_UNUSED_PTR(tcg_fpstatus);
10052 if (need_rmode) {
10053 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
10054 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
10055 } else {
10056 TCGV_UNUSED_I32(tcg_rmode);
10059 if (size == 3) {
10060 /* All 64-bit element operations can be shared with scalar 2misc */
10061 int pass;
10063 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
10064 TCGv_i64 tcg_op = tcg_temp_new_i64();
10065 TCGv_i64 tcg_res = tcg_temp_new_i64();
10067 read_vec_element(s, tcg_op, rn, pass, MO_64);
10069 handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
10070 tcg_rmode, tcg_fpstatus);
10072 write_vec_element(s, tcg_res, rd, pass, MO_64);
10074 tcg_temp_free_i64(tcg_res);
10075 tcg_temp_free_i64(tcg_op);
10077 } else {
10078 int pass;
10080 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
10081 TCGv_i32 tcg_op = tcg_temp_new_i32();
10082 TCGv_i32 tcg_res = tcg_temp_new_i32();
10083 TCGCond cond;
10085 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
10087 if (size == 2) {
10088 /* Special cases for 32 bit elements */
10089 switch (opcode) {
10090 case 0xa: /* CMLT */
10091 /* 32 bit integer comparison against zero, result is
10092 * test ? (2^32 - 1) : 0. We implement via setcond(test)
10093 * and inverting.
10095 cond = TCG_COND_LT;
10096 do_cmop:
10097 tcg_gen_setcondi_i32(cond, tcg_res, tcg_op, 0);
10098 tcg_gen_neg_i32(tcg_res, tcg_res);
10099 break;
10100 case 0x8: /* CMGT, CMGE */
10101 cond = u ? TCG_COND_GE : TCG_COND_GT;
10102 goto do_cmop;
10103 case 0x9: /* CMEQ, CMLE */
10104 cond = u ? TCG_COND_LE : TCG_COND_EQ;
10105 goto do_cmop;
10106 case 0x4: /* CLS */
10107 if (u) {
10108 gen_helper_clz32(tcg_res, tcg_op);
10109 } else {
10110 gen_helper_cls32(tcg_res, tcg_op);
10112 break;
10113 case 0x7: /* SQABS, SQNEG */
10114 if (u) {
10115 gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
10116 } else {
10117 gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
10119 break;
10120 case 0xb: /* ABS, NEG */
10121 if (u) {
10122 tcg_gen_neg_i32(tcg_res, tcg_op);
10123 } else {
10124 TCGv_i32 tcg_zero = tcg_const_i32(0);
10125 tcg_gen_neg_i32(tcg_res, tcg_op);
10126 tcg_gen_movcond_i32(TCG_COND_GT, tcg_res, tcg_op,
10127 tcg_zero, tcg_op, tcg_res);
10128 tcg_temp_free_i32(tcg_zero);
10130 break;
10131 case 0x2f: /* FABS */
10132 gen_helper_vfp_abss(tcg_res, tcg_op);
10133 break;
10134 case 0x6f: /* FNEG */
10135 gen_helper_vfp_negs(tcg_res, tcg_op);
10136 break;
10137 case 0x7f: /* FSQRT */
10138 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
10139 break;
10140 case 0x1a: /* FCVTNS */
10141 case 0x1b: /* FCVTMS */
10142 case 0x1c: /* FCVTAS */
10143 case 0x3a: /* FCVTPS */
10144 case 0x3b: /* FCVTZS */
10146 TCGv_i32 tcg_shift = tcg_const_i32(0);
10147 gen_helper_vfp_tosls(tcg_res, tcg_op,
10148 tcg_shift, tcg_fpstatus);
10149 tcg_temp_free_i32(tcg_shift);
10150 break;
10152 case 0x5a: /* FCVTNU */
10153 case 0x5b: /* FCVTMU */
10154 case 0x5c: /* FCVTAU */
10155 case 0x7a: /* FCVTPU */
10156 case 0x7b: /* FCVTZU */
10158 TCGv_i32 tcg_shift = tcg_const_i32(0);
10159 gen_helper_vfp_touls(tcg_res, tcg_op,
10160 tcg_shift, tcg_fpstatus);
10161 tcg_temp_free_i32(tcg_shift);
10162 break;
10164 case 0x18: /* FRINTN */
10165 case 0x19: /* FRINTM */
10166 case 0x38: /* FRINTP */
10167 case 0x39: /* FRINTZ */
10168 case 0x58: /* FRINTA */
10169 case 0x79: /* FRINTI */
10170 gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
10171 break;
10172 case 0x59: /* FRINTX */
10173 gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
10174 break;
10175 case 0x7c: /* URSQRTE */
10176 gen_helper_rsqrte_u32(tcg_res, tcg_op, tcg_fpstatus);
10177 break;
10178 default:
10179 g_assert_not_reached();
10181 } else {
10182 /* Use helpers for 8 and 16 bit elements */
10183 switch (opcode) {
10184 case 0x5: /* CNT, RBIT */
10185 /* For these two insns size is part of the opcode specifier
10186 * (handled earlier); they always operate on byte elements.
10188 if (u) {
10189 gen_helper_neon_rbit_u8(tcg_res, tcg_op);
10190 } else {
10191 gen_helper_neon_cnt_u8(tcg_res, tcg_op);
10193 break;
10194 case 0x7: /* SQABS, SQNEG */
10196 NeonGenOneOpEnvFn *genfn;
10197 static NeonGenOneOpEnvFn * const fns[2][2] = {
10198 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10199 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10201 genfn = fns[size][u];
10202 genfn(tcg_res, cpu_env, tcg_op);
10203 break;
10205 case 0x8: /* CMGT, CMGE */
10206 case 0x9: /* CMEQ, CMLE */
10207 case 0xa: /* CMLT */
10209 static NeonGenTwoOpFn * const fns[3][2] = {
10210 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_s16 },
10211 { gen_helper_neon_cge_s8, gen_helper_neon_cge_s16 },
10212 { gen_helper_neon_ceq_u8, gen_helper_neon_ceq_u16 },
10214 NeonGenTwoOpFn *genfn;
10215 int comp;
10216 bool reverse;
10217 TCGv_i32 tcg_zero = tcg_const_i32(0);
10219 /* comp = index into [CMGT, CMGE, CMEQ, CMLE, CMLT] */
10220 comp = (opcode - 0x8) * 2 + u;
10221 /* ...but LE, LT are implemented as reverse GE, GT */
10222 reverse = (comp > 2);
10223 if (reverse) {
10224 comp = 4 - comp;
10226 genfn = fns[comp][size];
10227 if (reverse) {
10228 genfn(tcg_res, tcg_zero, tcg_op);
10229 } else {
10230 genfn(tcg_res, tcg_op, tcg_zero);
10232 tcg_temp_free_i32(tcg_zero);
10233 break;
10235 case 0xb: /* ABS, NEG */
10236 if (u) {
10237 TCGv_i32 tcg_zero = tcg_const_i32(0);
10238 if (size) {
10239 gen_helper_neon_sub_u16(tcg_res, tcg_zero, tcg_op);
10240 } else {
10241 gen_helper_neon_sub_u8(tcg_res, tcg_zero, tcg_op);
10243 tcg_temp_free_i32(tcg_zero);
10244 } else {
10245 if (size) {
10246 gen_helper_neon_abs_s16(tcg_res, tcg_op);
10247 } else {
10248 gen_helper_neon_abs_s8(tcg_res, tcg_op);
10251 break;
10252 case 0x4: /* CLS, CLZ */
10253 if (u) {
10254 if (size == 0) {
10255 gen_helper_neon_clz_u8(tcg_res, tcg_op);
10256 } else {
10257 gen_helper_neon_clz_u16(tcg_res, tcg_op);
10259 } else {
10260 if (size == 0) {
10261 gen_helper_neon_cls_s8(tcg_res, tcg_op);
10262 } else {
10263 gen_helper_neon_cls_s16(tcg_res, tcg_op);
10266 break;
10267 default:
10268 g_assert_not_reached();
10272 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10274 tcg_temp_free_i32(tcg_res);
10275 tcg_temp_free_i32(tcg_op);
10278 if (!is_q) {
10279 clear_vec_high(s, rd);
10282 if (need_rmode) {
10283 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
10284 tcg_temp_free_i32(tcg_rmode);
10286 if (need_fpstatus) {
10287 tcg_temp_free_ptr(tcg_fpstatus);
10291 /* C3.6.13 AdvSIMD scalar x indexed element
10292 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
10293 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
10294 * | 0 1 | U | 1 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
10295 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
10296 * C3.6.18 AdvSIMD vector x indexed element
10297 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
10298 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
10299 * | 0 | Q | U | 0 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
10300 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
10302 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
10304 /* This encoding has two kinds of instruction:
10305 * normal, where we perform elt x idxelt => elt for each
10306 * element in the vector
10307 * long, where we perform elt x idxelt and generate a result of
10308 * double the width of the input element
10309 * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
10311 bool is_scalar = extract32(insn, 28, 1);
10312 bool is_q = extract32(insn, 30, 1);
10313 bool u = extract32(insn, 29, 1);
10314 int size = extract32(insn, 22, 2);
10315 int l = extract32(insn, 21, 1);
10316 int m = extract32(insn, 20, 1);
10317 /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
10318 int rm = extract32(insn, 16, 4);
10319 int opcode = extract32(insn, 12, 4);
10320 int h = extract32(insn, 11, 1);
10321 int rn = extract32(insn, 5, 5);
10322 int rd = extract32(insn, 0, 5);
10323 bool is_long = false;
10324 bool is_fp = false;
10325 int index;
10326 TCGv_ptr fpst;
10328 switch (opcode) {
10329 case 0x0: /* MLA */
10330 case 0x4: /* MLS */
10331 if (!u || is_scalar) {
10332 unallocated_encoding(s);
10333 return;
10335 break;
10336 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10337 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10338 case 0xa: /* SMULL, SMULL2, UMULL, UMULL2 */
10339 if (is_scalar) {
10340 unallocated_encoding(s);
10341 return;
10343 is_long = true;
10344 break;
10345 case 0x3: /* SQDMLAL, SQDMLAL2 */
10346 case 0x7: /* SQDMLSL, SQDMLSL2 */
10347 case 0xb: /* SQDMULL, SQDMULL2 */
10348 is_long = true;
10349 /* fall through */
10350 case 0xc: /* SQDMULH */
10351 case 0xd: /* SQRDMULH */
10352 if (u) {
10353 unallocated_encoding(s);
10354 return;
10356 break;
10357 case 0x8: /* MUL */
10358 if (u || is_scalar) {
10359 unallocated_encoding(s);
10360 return;
10362 break;
10363 case 0x1: /* FMLA */
10364 case 0x5: /* FMLS */
10365 if (u) {
10366 unallocated_encoding(s);
10367 return;
10369 /* fall through */
10370 case 0x9: /* FMUL, FMULX */
10371 if (!extract32(size, 1, 1)) {
10372 unallocated_encoding(s);
10373 return;
10375 is_fp = true;
10376 break;
10377 default:
10378 unallocated_encoding(s);
10379 return;
10382 if (is_fp) {
10383 /* low bit of size indicates single/double */
10384 size = extract32(size, 0, 1) ? 3 : 2;
10385 if (size == 2) {
10386 index = h << 1 | l;
10387 } else {
10388 if (l || !is_q) {
10389 unallocated_encoding(s);
10390 return;
10392 index = h;
10394 rm |= (m << 4);
10395 } else {
10396 switch (size) {
10397 case 1:
10398 index = h << 2 | l << 1 | m;
10399 break;
10400 case 2:
10401 index = h << 1 | l;
10402 rm |= (m << 4);
10403 break;
10404 default:
10405 unallocated_encoding(s);
10406 return;
10410 if (!fp_access_check(s)) {
10411 return;
10414 if (is_fp) {
10415 fpst = get_fpstatus_ptr();
10416 } else {
10417 TCGV_UNUSED_PTR(fpst);
10420 if (size == 3) {
10421 TCGv_i64 tcg_idx = tcg_temp_new_i64();
10422 int pass;
10424 assert(is_fp && is_q && !is_long);
10426 read_vec_element(s, tcg_idx, rm, index, MO_64);
10428 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10429 TCGv_i64 tcg_op = tcg_temp_new_i64();
10430 TCGv_i64 tcg_res = tcg_temp_new_i64();
10432 read_vec_element(s, tcg_op, rn, pass, MO_64);
10434 switch (opcode) {
10435 case 0x5: /* FMLS */
10436 /* As usual for ARM, separate negation for fused multiply-add */
10437 gen_helper_vfp_negd(tcg_op, tcg_op);
10438 /* fall through */
10439 case 0x1: /* FMLA */
10440 read_vec_element(s, tcg_res, rd, pass, MO_64);
10441 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
10442 break;
10443 case 0x9: /* FMUL, FMULX */
10444 if (u) {
10445 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
10446 } else {
10447 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
10449 break;
10450 default:
10451 g_assert_not_reached();
10454 write_vec_element(s, tcg_res, rd, pass, MO_64);
10455 tcg_temp_free_i64(tcg_op);
10456 tcg_temp_free_i64(tcg_res);
10459 if (is_scalar) {
10460 clear_vec_high(s, rd);
10463 tcg_temp_free_i64(tcg_idx);
10464 } else if (!is_long) {
10465 /* 32 bit floating point, or 16 or 32 bit integer.
10466 * For the 16 bit scalar case we use the usual Neon helpers and
10467 * rely on the fact that 0 op 0 == 0 with no side effects.
10469 TCGv_i32 tcg_idx = tcg_temp_new_i32();
10470 int pass, maxpasses;
10472 if (is_scalar) {
10473 maxpasses = 1;
10474 } else {
10475 maxpasses = is_q ? 4 : 2;
10478 read_vec_element_i32(s, tcg_idx, rm, index, size);
10480 if (size == 1 && !is_scalar) {
10481 /* The simplest way to handle the 16x16 indexed ops is to duplicate
10482 * the index into both halves of the 32 bit tcg_idx and then use
10483 * the usual Neon helpers.
10485 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
10488 for (pass = 0; pass < maxpasses; pass++) {
10489 TCGv_i32 tcg_op = tcg_temp_new_i32();
10490 TCGv_i32 tcg_res = tcg_temp_new_i32();
10492 read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
10494 switch (opcode) {
10495 case 0x0: /* MLA */
10496 case 0x4: /* MLS */
10497 case 0x8: /* MUL */
10499 static NeonGenTwoOpFn * const fns[2][2] = {
10500 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
10501 { tcg_gen_add_i32, tcg_gen_sub_i32 },
10503 NeonGenTwoOpFn *genfn;
10504 bool is_sub = opcode == 0x4;
10506 if (size == 1) {
10507 gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
10508 } else {
10509 tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
10511 if (opcode == 0x8) {
10512 break;
10514 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
10515 genfn = fns[size - 1][is_sub];
10516 genfn(tcg_res, tcg_op, tcg_res);
10517 break;
10519 case 0x5: /* FMLS */
10520 /* As usual for ARM, separate negation for fused multiply-add */
10521 gen_helper_vfp_negs(tcg_op, tcg_op);
10522 /* fall through */
10523 case 0x1: /* FMLA */
10524 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10525 gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
10526 break;
10527 case 0x9: /* FMUL, FMULX */
10528 if (u) {
10529 gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
10530 } else {
10531 gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
10533 break;
10534 case 0xc: /* SQDMULH */
10535 if (size == 1) {
10536 gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
10537 tcg_op, tcg_idx);
10538 } else {
10539 gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
10540 tcg_op, tcg_idx);
10542 break;
10543 case 0xd: /* SQRDMULH */
10544 if (size == 1) {
10545 gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
10546 tcg_op, tcg_idx);
10547 } else {
10548 gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
10549 tcg_op, tcg_idx);
10551 break;
10552 default:
10553 g_assert_not_reached();
10556 if (is_scalar) {
10557 write_fp_sreg(s, rd, tcg_res);
10558 } else {
10559 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10562 tcg_temp_free_i32(tcg_op);
10563 tcg_temp_free_i32(tcg_res);
10566 tcg_temp_free_i32(tcg_idx);
10568 if (!is_q) {
10569 clear_vec_high(s, rd);
10571 } else {
10572 /* long ops: 16x16->32 or 32x32->64 */
10573 TCGv_i64 tcg_res[2];
10574 int pass;
10575 bool satop = extract32(opcode, 0, 1);
10576 TCGMemOp memop = MO_32;
10578 if (satop || !u) {
10579 memop |= MO_SIGN;
10582 if (size == 2) {
10583 TCGv_i64 tcg_idx = tcg_temp_new_i64();
10585 read_vec_element(s, tcg_idx, rm, index, memop);
10587 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10588 TCGv_i64 tcg_op = tcg_temp_new_i64();
10589 TCGv_i64 tcg_passres;
10590 int passelt;
10592 if (is_scalar) {
10593 passelt = 0;
10594 } else {
10595 passelt = pass + (is_q * 2);
10598 read_vec_element(s, tcg_op, rn, passelt, memop);
10600 tcg_res[pass] = tcg_temp_new_i64();
10602 if (opcode == 0xa || opcode == 0xb) {
10603 /* Non-accumulating ops */
10604 tcg_passres = tcg_res[pass];
10605 } else {
10606 tcg_passres = tcg_temp_new_i64();
10609 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
10610 tcg_temp_free_i64(tcg_op);
10612 if (satop) {
10613 /* saturating, doubling */
10614 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10615 tcg_passres, tcg_passres);
10618 if (opcode == 0xa || opcode == 0xb) {
10619 continue;
10622 /* Accumulating op: handle accumulate step */
10623 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10625 switch (opcode) {
10626 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10627 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10628 break;
10629 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10630 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10631 break;
10632 case 0x7: /* SQDMLSL, SQDMLSL2 */
10633 tcg_gen_neg_i64(tcg_passres, tcg_passres);
10634 /* fall through */
10635 case 0x3: /* SQDMLAL, SQDMLAL2 */
10636 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10637 tcg_res[pass],
10638 tcg_passres);
10639 break;
10640 default:
10641 g_assert_not_reached();
10643 tcg_temp_free_i64(tcg_passres);
10645 tcg_temp_free_i64(tcg_idx);
10647 if (is_scalar) {
10648 clear_vec_high(s, rd);
10650 } else {
10651 TCGv_i32 tcg_idx = tcg_temp_new_i32();
10653 assert(size == 1);
10654 read_vec_element_i32(s, tcg_idx, rm, index, size);
10656 if (!is_scalar) {
10657 /* The simplest way to handle the 16x16 indexed ops is to
10658 * duplicate the index into both halves of the 32 bit tcg_idx
10659 * and then use the usual Neon helpers.
10661 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
10664 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10665 TCGv_i32 tcg_op = tcg_temp_new_i32();
10666 TCGv_i64 tcg_passres;
10668 if (is_scalar) {
10669 read_vec_element_i32(s, tcg_op, rn, pass, size);
10670 } else {
10671 read_vec_element_i32(s, tcg_op, rn,
10672 pass + (is_q * 2), MO_32);
10675 tcg_res[pass] = tcg_temp_new_i64();
10677 if (opcode == 0xa || opcode == 0xb) {
10678 /* Non-accumulating ops */
10679 tcg_passres = tcg_res[pass];
10680 } else {
10681 tcg_passres = tcg_temp_new_i64();
10684 if (memop & MO_SIGN) {
10685 gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
10686 } else {
10687 gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
10689 if (satop) {
10690 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10691 tcg_passres, tcg_passres);
10693 tcg_temp_free_i32(tcg_op);
10695 if (opcode == 0xa || opcode == 0xb) {
10696 continue;
10699 /* Accumulating op: handle accumulate step */
10700 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10702 switch (opcode) {
10703 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10704 gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
10705 tcg_passres);
10706 break;
10707 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10708 gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
10709 tcg_passres);
10710 break;
10711 case 0x7: /* SQDMLSL, SQDMLSL2 */
10712 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10713 /* fall through */
10714 case 0x3: /* SQDMLAL, SQDMLAL2 */
10715 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10716 tcg_res[pass],
10717 tcg_passres);
10718 break;
10719 default:
10720 g_assert_not_reached();
10722 tcg_temp_free_i64(tcg_passres);
10724 tcg_temp_free_i32(tcg_idx);
10726 if (is_scalar) {
10727 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
10731 if (is_scalar) {
10732 tcg_res[1] = tcg_const_i64(0);
10735 for (pass = 0; pass < 2; pass++) {
10736 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10737 tcg_temp_free_i64(tcg_res[pass]);
10741 if (!TCGV_IS_UNUSED_PTR(fpst)) {
10742 tcg_temp_free_ptr(fpst);
10746 /* C3.6.19 Crypto AES
10747 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
10748 * +-----------------+------+-----------+--------+-----+------+------+
10749 * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
10750 * +-----------------+------+-----------+--------+-----+------+------+
10752 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
10754 int size = extract32(insn, 22, 2);
10755 int opcode = extract32(insn, 12, 5);
10756 int rn = extract32(insn, 5, 5);
10757 int rd = extract32(insn, 0, 5);
10758 int decrypt;
10759 TCGv_i32 tcg_rd_regno, tcg_rn_regno, tcg_decrypt;
10760 CryptoThreeOpEnvFn *genfn;
10762 if (!arm_dc_feature(s, ARM_FEATURE_V8_AES)
10763 || size != 0) {
10764 unallocated_encoding(s);
10765 return;
10768 switch (opcode) {
10769 case 0x4: /* AESE */
10770 decrypt = 0;
10771 genfn = gen_helper_crypto_aese;
10772 break;
10773 case 0x6: /* AESMC */
10774 decrypt = 0;
10775 genfn = gen_helper_crypto_aesmc;
10776 break;
10777 case 0x5: /* AESD */
10778 decrypt = 1;
10779 genfn = gen_helper_crypto_aese;
10780 break;
10781 case 0x7: /* AESIMC */
10782 decrypt = 1;
10783 genfn = gen_helper_crypto_aesmc;
10784 break;
10785 default:
10786 unallocated_encoding(s);
10787 return;
10790 /* Note that we convert the Vx register indexes into the
10791 * index within the vfp.regs[] array, so we can share the
10792 * helper with the AArch32 instructions.
10794 tcg_rd_regno = tcg_const_i32(rd << 1);
10795 tcg_rn_regno = tcg_const_i32(rn << 1);
10796 tcg_decrypt = tcg_const_i32(decrypt);
10798 genfn(cpu_env, tcg_rd_regno, tcg_rn_regno, tcg_decrypt);
10800 tcg_temp_free_i32(tcg_rd_regno);
10801 tcg_temp_free_i32(tcg_rn_regno);
10802 tcg_temp_free_i32(tcg_decrypt);
10805 /* C3.6.20 Crypto three-reg SHA
10806 * 31 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
10807 * +-----------------+------+---+------+---+--------+-----+------+------+
10808 * | 0 1 0 1 1 1 1 0 | size | 0 | Rm | 0 | opcode | 0 0 | Rn | Rd |
10809 * +-----------------+------+---+------+---+--------+-----+------+------+
10811 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
10813 int size = extract32(insn, 22, 2);
10814 int opcode = extract32(insn, 12, 3);
10815 int rm = extract32(insn, 16, 5);
10816 int rn = extract32(insn, 5, 5);
10817 int rd = extract32(insn, 0, 5);
10818 CryptoThreeOpEnvFn *genfn;
10819 TCGv_i32 tcg_rd_regno, tcg_rn_regno, tcg_rm_regno;
10820 int feature = ARM_FEATURE_V8_SHA256;
10822 if (size != 0) {
10823 unallocated_encoding(s);
10824 return;
10827 switch (opcode) {
10828 case 0: /* SHA1C */
10829 case 1: /* SHA1P */
10830 case 2: /* SHA1M */
10831 case 3: /* SHA1SU0 */
10832 genfn = NULL;
10833 feature = ARM_FEATURE_V8_SHA1;
10834 break;
10835 case 4: /* SHA256H */
10836 genfn = gen_helper_crypto_sha256h;
10837 break;
10838 case 5: /* SHA256H2 */
10839 genfn = gen_helper_crypto_sha256h2;
10840 break;
10841 case 6: /* SHA256SU1 */
10842 genfn = gen_helper_crypto_sha256su1;
10843 break;
10844 default:
10845 unallocated_encoding(s);
10846 return;
10849 if (!arm_dc_feature(s, feature)) {
10850 unallocated_encoding(s);
10851 return;
10854 tcg_rd_regno = tcg_const_i32(rd << 1);
10855 tcg_rn_regno = tcg_const_i32(rn << 1);
10856 tcg_rm_regno = tcg_const_i32(rm << 1);
10858 if (genfn) {
10859 genfn(cpu_env, tcg_rd_regno, tcg_rn_regno, tcg_rm_regno);
10860 } else {
10861 TCGv_i32 tcg_opcode = tcg_const_i32(opcode);
10863 gen_helper_crypto_sha1_3reg(cpu_env, tcg_rd_regno,
10864 tcg_rn_regno, tcg_rm_regno, tcg_opcode);
10865 tcg_temp_free_i32(tcg_opcode);
10868 tcg_temp_free_i32(tcg_rd_regno);
10869 tcg_temp_free_i32(tcg_rn_regno);
10870 tcg_temp_free_i32(tcg_rm_regno);
10873 /* C3.6.21 Crypto two-reg SHA
10874 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
10875 * +-----------------+------+-----------+--------+-----+------+------+
10876 * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
10877 * +-----------------+------+-----------+--------+-----+------+------+
10879 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
10881 int size = extract32(insn, 22, 2);
10882 int opcode = extract32(insn, 12, 5);
10883 int rn = extract32(insn, 5, 5);
10884 int rd = extract32(insn, 0, 5);
10885 CryptoTwoOpEnvFn *genfn;
10886 int feature;
10887 TCGv_i32 tcg_rd_regno, tcg_rn_regno;
10889 if (size != 0) {
10890 unallocated_encoding(s);
10891 return;
10894 switch (opcode) {
10895 case 0: /* SHA1H */
10896 feature = ARM_FEATURE_V8_SHA1;
10897 genfn = gen_helper_crypto_sha1h;
10898 break;
10899 case 1: /* SHA1SU1 */
10900 feature = ARM_FEATURE_V8_SHA1;
10901 genfn = gen_helper_crypto_sha1su1;
10902 break;
10903 case 2: /* SHA256SU0 */
10904 feature = ARM_FEATURE_V8_SHA256;
10905 genfn = gen_helper_crypto_sha256su0;
10906 break;
10907 default:
10908 unallocated_encoding(s);
10909 return;
10912 if (!arm_dc_feature(s, feature)) {
10913 unallocated_encoding(s);
10914 return;
10917 tcg_rd_regno = tcg_const_i32(rd << 1);
10918 tcg_rn_regno = tcg_const_i32(rn << 1);
10920 genfn(cpu_env, tcg_rd_regno, tcg_rn_regno);
10922 tcg_temp_free_i32(tcg_rd_regno);
10923 tcg_temp_free_i32(tcg_rn_regno);
10926 /* C3.6 Data processing - SIMD, inc Crypto
10928 * As the decode gets a little complex we are using a table based
10929 * approach for this part of the decode.
10931 static const AArch64DecodeTable data_proc_simd[] = {
10932 /* pattern , mask , fn */
10933 { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
10934 { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
10935 { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
10936 { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
10937 { 0x0e000400, 0x9fe08400, disas_simd_copy },
10938 { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
10939 /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
10940 { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
10941 { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
10942 { 0x0e000000, 0xbf208c00, disas_simd_tb },
10943 { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
10944 { 0x2e000000, 0xbf208400, disas_simd_ext },
10945 { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
10946 { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
10947 { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
10948 { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
10949 { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
10950 { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
10951 { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
10952 { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
10953 { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
10954 { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
10955 { 0x00000000, 0x00000000, NULL }
10958 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
10960 /* Note that this is called with all non-FP cases from
10961 * table C3-6 so it must UNDEF for entries not specifically
10962 * allocated to instructions in that table.
10964 AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
10965 if (fn) {
10966 fn(s, insn);
10967 } else {
10968 unallocated_encoding(s);
10972 /* C3.6 Data processing - SIMD and floating point */
10973 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
10975 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
10976 disas_data_proc_fp(s, insn);
10977 } else {
10978 /* SIMD, including crypto */
10979 disas_data_proc_simd(s, insn);
10983 /* C3.1 A64 instruction index by encoding */
10984 static void disas_a64_insn(CPUARMState *env, DisasContext *s)
10986 uint32_t insn;
10988 insn = arm_ldl_code(env, s->pc, s->sctlr_b);
10989 s->insn = insn;
10990 s->pc += 4;
10992 s->fp_access_checked = false;
10994 switch (extract32(insn, 25, 4)) {
10995 case 0x0: case 0x1: case 0x2: case 0x3: /* UNALLOCATED */
10996 unallocated_encoding(s);
10997 break;
10998 case 0x8: case 0x9: /* Data processing - immediate */
10999 disas_data_proc_imm(s, insn);
11000 break;
11001 case 0xa: case 0xb: /* Branch, exception generation and system insns */
11002 disas_b_exc_sys(s, insn);
11003 break;
11004 case 0x4:
11005 case 0x6:
11006 case 0xc:
11007 case 0xe: /* Loads and stores */
11008 disas_ldst(s, insn);
11009 break;
11010 case 0x5:
11011 case 0xd: /* Data processing - register */
11012 disas_data_proc_reg(s, insn);
11013 break;
11014 case 0x7:
11015 case 0xf: /* Data processing - SIMD and floating point */
11016 disas_data_proc_simd_fp(s, insn);
11017 break;
11018 default:
11019 assert(FALSE); /* all 15 cases should be handled above */
11020 break;
11023 /* if we allocated any temporaries, free them here */
11024 free_tmp_a64(s);
11027 void gen_intermediate_code_a64(ARMCPU *cpu, TranslationBlock *tb)
11029 CPUState *cs = CPU(cpu);
11030 CPUARMState *env = &cpu->env;
11031 DisasContext dc1, *dc = &dc1;
11032 target_ulong pc_start;
11033 target_ulong next_page_start;
11034 int num_insns;
11035 int max_insns;
11037 pc_start = tb->pc;
11039 dc->tb = tb;
11041 dc->is_jmp = DISAS_NEXT;
11042 dc->pc = pc_start;
11043 dc->singlestep_enabled = cs->singlestep_enabled;
11044 dc->condjmp = 0;
11046 dc->aarch64 = 1;
11047 /* If we are coming from secure EL0 in a system with a 32-bit EL3, then
11048 * there is no secure EL1, so we route exceptions to EL3.
11050 dc->secure_routed_to_el3 = arm_feature(env, ARM_FEATURE_EL3) &&
11051 !arm_el_is_aa64(env, 3);
11052 dc->thumb = 0;
11053 dc->sctlr_b = 0;
11054 dc->be_data = ARM_TBFLAG_BE_DATA(tb->flags) ? MO_BE : MO_LE;
11055 dc->condexec_mask = 0;
11056 dc->condexec_cond = 0;
11057 dc->mmu_idx = ARM_TBFLAG_MMUIDX(tb->flags);
11058 dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
11059 #if !defined(CONFIG_USER_ONLY)
11060 dc->user = (dc->current_el == 0);
11061 #endif
11062 dc->fp_excp_el = ARM_TBFLAG_FPEXC_EL(tb->flags);
11063 dc->vec_len = 0;
11064 dc->vec_stride = 0;
11065 dc->cp_regs = cpu->cp_regs;
11066 dc->features = env->features;
11068 /* Single step state. The code-generation logic here is:
11069 * SS_ACTIVE == 0:
11070 * generate code with no special handling for single-stepping (except
11071 * that anything that can make us go to SS_ACTIVE == 1 must end the TB;
11072 * this happens anyway because those changes are all system register or
11073 * PSTATE writes).
11074 * SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
11075 * emit code for one insn
11076 * emit code to clear PSTATE.SS
11077 * emit code to generate software step exception for completed step
11078 * end TB (as usual for having generated an exception)
11079 * SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
11080 * emit code to generate a software step exception
11081 * end the TB
11083 dc->ss_active = ARM_TBFLAG_SS_ACTIVE(tb->flags);
11084 dc->pstate_ss = ARM_TBFLAG_PSTATE_SS(tb->flags);
11085 dc->is_ldex = false;
11086 dc->ss_same_el = (arm_debug_target_el(env) == dc->current_el);
11088 init_tmp_a64_array(dc);
11090 next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
11091 num_insns = 0;
11092 max_insns = tb->cflags & CF_COUNT_MASK;
11093 if (max_insns == 0) {
11094 max_insns = CF_COUNT_MASK;
11096 if (max_insns > TCG_MAX_INSNS) {
11097 max_insns = TCG_MAX_INSNS;
11100 gen_tb_start(tb);
11102 tcg_clear_temp_count();
11104 do {
11105 tcg_gen_insn_start(dc->pc, 0);
11106 num_insns++;
11108 if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) {
11109 CPUBreakpoint *bp;
11110 QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
11111 if (bp->pc == dc->pc) {
11112 if (bp->flags & BP_CPU) {
11113 gen_a64_set_pc_im(dc->pc);
11114 gen_helper_check_breakpoints(cpu_env);
11115 /* End the TB early; it likely won't be executed */
11116 dc->is_jmp = DISAS_UPDATE;
11117 } else {
11118 gen_exception_internal_insn(dc, 0, EXCP_DEBUG);
11119 /* The address covered by the breakpoint must be
11120 included in [tb->pc, tb->pc + tb->size) in order
11121 to for it to be properly cleared -- thus we
11122 increment the PC here so that the logic setting
11123 tb->size below does the right thing. */
11124 dc->pc += 4;
11125 goto done_generating;
11127 break;
11132 if (num_insns == max_insns && (tb->cflags & CF_LAST_IO)) {
11133 gen_io_start();
11136 if (dc->ss_active && !dc->pstate_ss) {
11137 /* Singlestep state is Active-pending.
11138 * If we're in this state at the start of a TB then either
11139 * a) we just took an exception to an EL which is being debugged
11140 * and this is the first insn in the exception handler
11141 * b) debug exceptions were masked and we just unmasked them
11142 * without changing EL (eg by clearing PSTATE.D)
11143 * In either case we're going to take a swstep exception in the
11144 * "did not step an insn" case, and so the syndrome ISV and EX
11145 * bits should be zero.
11147 assert(num_insns == 1);
11148 gen_exception(EXCP_UDEF, syn_swstep(dc->ss_same_el, 0, 0),
11149 default_exception_el(dc));
11150 dc->is_jmp = DISAS_EXC;
11151 break;
11154 disas_a64_insn(env, dc);
11156 if (tcg_check_temp_count()) {
11157 fprintf(stderr, "TCG temporary leak before "TARGET_FMT_lx"\n",
11158 dc->pc);
11161 /* Translation stops when a conditional branch is encountered.
11162 * Otherwise the subsequent code could get translated several times.
11163 * Also stop translation when a page boundary is reached. This
11164 * ensures prefetch aborts occur at the right place.
11166 } while (!dc->is_jmp && !tcg_op_buf_full() &&
11167 !cs->singlestep_enabled &&
11168 !singlestep &&
11169 !dc->ss_active &&
11170 dc->pc < next_page_start &&
11171 num_insns < max_insns);
11173 if (tb->cflags & CF_LAST_IO) {
11174 gen_io_end();
11177 if (unlikely(cs->singlestep_enabled || dc->ss_active)
11178 && dc->is_jmp != DISAS_EXC) {
11179 /* Note that this means single stepping WFI doesn't halt the CPU.
11180 * For conditional branch insns this is harmless unreachable code as
11181 * gen_goto_tb() has already handled emitting the debug exception
11182 * (and thus a tb-jump is not possible when singlestepping).
11184 assert(dc->is_jmp != DISAS_TB_JUMP);
11185 if (dc->is_jmp != DISAS_JUMP) {
11186 gen_a64_set_pc_im(dc->pc);
11188 if (cs->singlestep_enabled) {
11189 gen_exception_internal(EXCP_DEBUG);
11190 } else {
11191 gen_step_complete_exception(dc);
11193 } else {
11194 switch (dc->is_jmp) {
11195 case DISAS_NEXT:
11196 gen_goto_tb(dc, 1, dc->pc);
11197 break;
11198 default:
11199 case DISAS_UPDATE:
11200 gen_a64_set_pc_im(dc->pc);
11201 /* fall through */
11202 case DISAS_JUMP:
11203 /* indicate that the hash table must be used to find the next TB */
11204 tcg_gen_exit_tb(0);
11205 break;
11206 case DISAS_TB_JUMP:
11207 case DISAS_EXC:
11208 case DISAS_SWI:
11209 break;
11210 case DISAS_WFE:
11211 gen_a64_set_pc_im(dc->pc);
11212 gen_helper_wfe(cpu_env);
11213 break;
11214 case DISAS_YIELD:
11215 gen_a64_set_pc_im(dc->pc);
11216 gen_helper_yield(cpu_env);
11217 break;
11218 case DISAS_WFI:
11219 /* This is a special case because we don't want to just halt the CPU
11220 * if trying to debug across a WFI.
11222 gen_a64_set_pc_im(dc->pc);
11223 gen_helper_wfi(cpu_env);
11224 /* The helper doesn't necessarily throw an exception, but we
11225 * must go back to the main loop to check for interrupts anyway.
11227 tcg_gen_exit_tb(0);
11228 break;
11232 done_generating:
11233 gen_tb_end(tb, num_insns);
11235 #ifdef DEBUG_DISAS
11236 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) &&
11237 qemu_log_in_addr_range(pc_start)) {
11238 qemu_log("----------------\n");
11239 qemu_log("IN: %s\n", lookup_symbol(pc_start));
11240 log_target_disas(cs, pc_start, dc->pc - pc_start,
11241 4 | (bswap_code(dc->sctlr_b) ? 2 : 0));
11242 qemu_log("\n");
11244 #endif
11245 tb->size = dc->pc - pc_start;
11246 tb->icount = num_insns;