target/arm: Implement VFMAL and VFMSL for aarch32
[qemu/ar7.git] / target / arm / translate-a64.c
blobd3c8eaf08933575ca39affe1a0833bb383f41610
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 "tcg-op-gvec.h"
25 #include "qemu/log.h"
26 #include "arm_ldst.h"
27 #include "translate.h"
28 #include "internals.h"
29 #include "qemu/host-utils.h"
31 #include "exec/semihost.h"
32 #include "exec/gen-icount.h"
34 #include "exec/helper-proto.h"
35 #include "exec/helper-gen.h"
36 #include "exec/log.h"
38 #include "trace-tcg.h"
39 #include "translate-a64.h"
40 #include "qemu/atomic128.h"
42 static TCGv_i64 cpu_X[32];
43 static TCGv_i64 cpu_pc;
45 /* Load/store exclusive handling */
46 static TCGv_i64 cpu_exclusive_high;
48 static const char *regnames[] = {
49 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
50 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
51 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
52 "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
55 enum a64_shift_type {
56 A64_SHIFT_TYPE_LSL = 0,
57 A64_SHIFT_TYPE_LSR = 1,
58 A64_SHIFT_TYPE_ASR = 2,
59 A64_SHIFT_TYPE_ROR = 3
62 /* Table based decoder typedefs - used when the relevant bits for decode
63 * are too awkwardly scattered across the instruction (eg SIMD).
65 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
67 typedef struct AArch64DecodeTable {
68 uint32_t pattern;
69 uint32_t mask;
70 AArch64DecodeFn *disas_fn;
71 } AArch64DecodeTable;
73 /* Function prototype for gen_ functions for calling Neon helpers */
74 typedef void NeonGenOneOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32);
75 typedef void NeonGenTwoOpFn(TCGv_i32, TCGv_i32, TCGv_i32);
76 typedef void NeonGenTwoOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32, TCGv_i32);
77 typedef void NeonGenTwo64OpFn(TCGv_i64, TCGv_i64, TCGv_i64);
78 typedef void NeonGenTwo64OpEnvFn(TCGv_i64, TCGv_ptr, TCGv_i64, TCGv_i64);
79 typedef void NeonGenNarrowFn(TCGv_i32, TCGv_i64);
80 typedef void NeonGenNarrowEnvFn(TCGv_i32, TCGv_ptr, TCGv_i64);
81 typedef void NeonGenWidenFn(TCGv_i64, TCGv_i32);
82 typedef void NeonGenTwoSingleOPFn(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
83 typedef void NeonGenTwoDoubleOPFn(TCGv_i64, TCGv_i64, TCGv_i64, TCGv_ptr);
84 typedef void NeonGenOneOpFn(TCGv_i64, TCGv_i64);
85 typedef void CryptoTwoOpFn(TCGv_ptr, TCGv_ptr);
86 typedef void CryptoThreeOpIntFn(TCGv_ptr, TCGv_ptr, TCGv_i32);
87 typedef void CryptoThreeOpFn(TCGv_ptr, TCGv_ptr, TCGv_ptr);
88 typedef void AtomicThreeOpFn(TCGv_i64, TCGv_i64, TCGv_i64, TCGArg, TCGMemOp);
90 /* initialize TCG globals. */
91 void a64_translate_init(void)
93 int i;
95 cpu_pc = tcg_global_mem_new_i64(cpu_env,
96 offsetof(CPUARMState, pc),
97 "pc");
98 for (i = 0; i < 32; i++) {
99 cpu_X[i] = tcg_global_mem_new_i64(cpu_env,
100 offsetof(CPUARMState, xregs[i]),
101 regnames[i]);
104 cpu_exclusive_high = tcg_global_mem_new_i64(cpu_env,
105 offsetof(CPUARMState, exclusive_high), "exclusive_high");
108 static inline int get_a64_user_mem_index(DisasContext *s)
110 /* Return the core mmu_idx to use for A64 "unprivileged load/store" insns:
111 * if EL1, access as if EL0; otherwise access at current EL
113 ARMMMUIdx useridx;
115 switch (s->mmu_idx) {
116 case ARMMMUIdx_S12NSE1:
117 useridx = ARMMMUIdx_S12NSE0;
118 break;
119 case ARMMMUIdx_S1SE1:
120 useridx = ARMMMUIdx_S1SE0;
121 break;
122 case ARMMMUIdx_S2NS:
123 g_assert_not_reached();
124 default:
125 useridx = s->mmu_idx;
126 break;
128 return arm_to_core_mmu_idx(useridx);
131 static void reset_btype(DisasContext *s)
133 if (s->btype != 0) {
134 TCGv_i32 zero = tcg_const_i32(0);
135 tcg_gen_st_i32(zero, cpu_env, offsetof(CPUARMState, btype));
136 tcg_temp_free_i32(zero);
137 s->btype = 0;
141 static void set_btype(DisasContext *s, int val)
143 TCGv_i32 tcg_val;
145 /* BTYPE is a 2-bit field, and 0 should be done with reset_btype. */
146 tcg_debug_assert(val >= 1 && val <= 3);
148 tcg_val = tcg_const_i32(val);
149 tcg_gen_st_i32(tcg_val, cpu_env, offsetof(CPUARMState, btype));
150 tcg_temp_free_i32(tcg_val);
151 s->btype = -1;
154 void aarch64_cpu_dump_state(CPUState *cs, FILE *f,
155 fprintf_function cpu_fprintf, int flags)
157 ARMCPU *cpu = ARM_CPU(cs);
158 CPUARMState *env = &cpu->env;
159 uint32_t psr = pstate_read(env);
160 int i;
161 int el = arm_current_el(env);
162 const char *ns_status;
164 cpu_fprintf(f, " PC=%016" PRIx64 " ", env->pc);
165 for (i = 0; i < 32; i++) {
166 if (i == 31) {
167 cpu_fprintf(f, " SP=%016" PRIx64 "\n", env->xregs[i]);
168 } else {
169 cpu_fprintf(f, "X%02d=%016" PRIx64 "%s", i, env->xregs[i],
170 (i + 2) % 3 ? " " : "\n");
174 if (arm_feature(env, ARM_FEATURE_EL3) && el != 3) {
175 ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
176 } else {
177 ns_status = "";
179 cpu_fprintf(f, "PSTATE=%08x %c%c%c%c %sEL%d%c",
180 psr,
181 psr & PSTATE_N ? 'N' : '-',
182 psr & PSTATE_Z ? 'Z' : '-',
183 psr & PSTATE_C ? 'C' : '-',
184 psr & PSTATE_V ? 'V' : '-',
185 ns_status,
187 psr & PSTATE_SP ? 'h' : 't');
189 if (cpu_isar_feature(aa64_bti, cpu)) {
190 cpu_fprintf(f, " BTYPE=%d", (psr & PSTATE_BTYPE) >> 10);
192 if (!(flags & CPU_DUMP_FPU)) {
193 cpu_fprintf(f, "\n");
194 return;
196 if (fp_exception_el(env, el) != 0) {
197 cpu_fprintf(f, " FPU disabled\n");
198 return;
200 cpu_fprintf(f, " FPCR=%08x FPSR=%08x\n",
201 vfp_get_fpcr(env), vfp_get_fpsr(env));
203 if (cpu_isar_feature(aa64_sve, cpu) && sve_exception_el(env, el) == 0) {
204 int j, zcr_len = sve_zcr_len_for_el(env, el);
206 for (i = 0; i <= FFR_PRED_NUM; i++) {
207 bool eol;
208 if (i == FFR_PRED_NUM) {
209 cpu_fprintf(f, "FFR=");
210 /* It's last, so end the line. */
211 eol = true;
212 } else {
213 cpu_fprintf(f, "P%02d=", i);
214 switch (zcr_len) {
215 case 0:
216 eol = i % 8 == 7;
217 break;
218 case 1:
219 eol = i % 6 == 5;
220 break;
221 case 2:
222 case 3:
223 eol = i % 3 == 2;
224 break;
225 default:
226 /* More than one quadword per predicate. */
227 eol = true;
228 break;
231 for (j = zcr_len / 4; j >= 0; j--) {
232 int digits;
233 if (j * 4 + 4 <= zcr_len + 1) {
234 digits = 16;
235 } else {
236 digits = (zcr_len % 4 + 1) * 4;
238 cpu_fprintf(f, "%0*" PRIx64 "%s", digits,
239 env->vfp.pregs[i].p[j],
240 j ? ":" : eol ? "\n" : " ");
244 for (i = 0; i < 32; i++) {
245 if (zcr_len == 0) {
246 cpu_fprintf(f, "Z%02d=%016" PRIx64 ":%016" PRIx64 "%s",
247 i, env->vfp.zregs[i].d[1],
248 env->vfp.zregs[i].d[0], i & 1 ? "\n" : " ");
249 } else if (zcr_len == 1) {
250 cpu_fprintf(f, "Z%02d=%016" PRIx64 ":%016" PRIx64
251 ":%016" PRIx64 ":%016" PRIx64 "\n",
252 i, env->vfp.zregs[i].d[3], env->vfp.zregs[i].d[2],
253 env->vfp.zregs[i].d[1], env->vfp.zregs[i].d[0]);
254 } else {
255 for (j = zcr_len; j >= 0; j--) {
256 bool odd = (zcr_len - j) % 2 != 0;
257 if (j == zcr_len) {
258 cpu_fprintf(f, "Z%02d[%x-%x]=", i, j, j - 1);
259 } else if (!odd) {
260 if (j > 0) {
261 cpu_fprintf(f, " [%x-%x]=", j, j - 1);
262 } else {
263 cpu_fprintf(f, " [%x]=", j);
266 cpu_fprintf(f, "%016" PRIx64 ":%016" PRIx64 "%s",
267 env->vfp.zregs[i].d[j * 2 + 1],
268 env->vfp.zregs[i].d[j * 2],
269 odd || j == 0 ? "\n" : ":");
273 } else {
274 for (i = 0; i < 32; i++) {
275 uint64_t *q = aa64_vfp_qreg(env, i);
276 cpu_fprintf(f, "Q%02d=%016" PRIx64 ":%016" PRIx64 "%s",
277 i, q[1], q[0], (i & 1 ? "\n" : " "));
282 void gen_a64_set_pc_im(uint64_t val)
284 tcg_gen_movi_i64(cpu_pc, val);
288 * Handle Top Byte Ignore (TBI) bits.
290 * If address tagging is enabled via the TCR TBI bits:
291 * + for EL2 and EL3 there is only one TBI bit, and if it is set
292 * then the address is zero-extended, clearing bits [63:56]
293 * + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
294 * and TBI1 controls addressses with bit 55 == 1.
295 * If the appropriate TBI bit is set for the address then
296 * the address is sign-extended from bit 55 into bits [63:56]
298 * Here We have concatenated TBI{1,0} into tbi.
300 static void gen_top_byte_ignore(DisasContext *s, TCGv_i64 dst,
301 TCGv_i64 src, int tbi)
303 if (tbi == 0) {
304 /* Load unmodified address */
305 tcg_gen_mov_i64(dst, src);
306 } else if (s->current_el >= 2) {
307 /* FIXME: ARMv8.1-VHE S2 translation regime. */
308 /* Force tag byte to all zero */
309 tcg_gen_extract_i64(dst, src, 0, 56);
310 } else {
311 /* Sign-extend from bit 55. */
312 tcg_gen_sextract_i64(dst, src, 0, 56);
314 if (tbi != 3) {
315 TCGv_i64 tcg_zero = tcg_const_i64(0);
318 * The two TBI bits differ.
319 * If tbi0, then !tbi1: only use the extension if positive.
320 * if !tbi0, then tbi1: only use the extension if negative.
322 tcg_gen_movcond_i64(tbi == 1 ? TCG_COND_GE : TCG_COND_LT,
323 dst, dst, tcg_zero, dst, src);
324 tcg_temp_free_i64(tcg_zero);
329 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
332 * If address tagging is enabled for instructions via the TCR TBI bits,
333 * then loading an address into the PC will clear out any tag.
335 gen_top_byte_ignore(s, cpu_pc, src, s->tbii);
339 * Return a "clean" address for ADDR according to TBID.
340 * This is always a fresh temporary, as we need to be able to
341 * increment this independently of a dirty write-back address.
343 static TCGv_i64 clean_data_tbi(DisasContext *s, TCGv_i64 addr)
345 TCGv_i64 clean = new_tmp_a64(s);
346 gen_top_byte_ignore(s, clean, addr, s->tbid);
347 return clean;
350 typedef struct DisasCompare64 {
351 TCGCond cond;
352 TCGv_i64 value;
353 } DisasCompare64;
355 static void a64_test_cc(DisasCompare64 *c64, int cc)
357 DisasCompare c32;
359 arm_test_cc(&c32, cc);
361 /* Sign-extend the 32-bit value so that the GE/LT comparisons work
362 * properly. The NE/EQ comparisons are also fine with this choice. */
363 c64->cond = c32.cond;
364 c64->value = tcg_temp_new_i64();
365 tcg_gen_ext_i32_i64(c64->value, c32.value);
367 arm_free_cc(&c32);
370 static void a64_free_cc(DisasCompare64 *c64)
372 tcg_temp_free_i64(c64->value);
375 static void gen_exception_internal(int excp)
377 TCGv_i32 tcg_excp = tcg_const_i32(excp);
379 assert(excp_is_internal(excp));
380 gen_helper_exception_internal(cpu_env, tcg_excp);
381 tcg_temp_free_i32(tcg_excp);
384 static void gen_exception(int excp, uint32_t syndrome, uint32_t target_el)
386 TCGv_i32 tcg_excp = tcg_const_i32(excp);
387 TCGv_i32 tcg_syn = tcg_const_i32(syndrome);
388 TCGv_i32 tcg_el = tcg_const_i32(target_el);
390 gen_helper_exception_with_syndrome(cpu_env, tcg_excp,
391 tcg_syn, tcg_el);
392 tcg_temp_free_i32(tcg_el);
393 tcg_temp_free_i32(tcg_syn);
394 tcg_temp_free_i32(tcg_excp);
397 static void gen_exception_internal_insn(DisasContext *s, int offset, int excp)
399 gen_a64_set_pc_im(s->pc - offset);
400 gen_exception_internal(excp);
401 s->base.is_jmp = DISAS_NORETURN;
404 static void gen_exception_insn(DisasContext *s, int offset, int excp,
405 uint32_t syndrome, uint32_t target_el)
407 gen_a64_set_pc_im(s->pc - offset);
408 gen_exception(excp, syndrome, target_el);
409 s->base.is_jmp = DISAS_NORETURN;
412 static void gen_exception_bkpt_insn(DisasContext *s, int offset,
413 uint32_t syndrome)
415 TCGv_i32 tcg_syn;
417 gen_a64_set_pc_im(s->pc - offset);
418 tcg_syn = tcg_const_i32(syndrome);
419 gen_helper_exception_bkpt_insn(cpu_env, tcg_syn);
420 tcg_temp_free_i32(tcg_syn);
421 s->base.is_jmp = DISAS_NORETURN;
424 static void gen_ss_advance(DisasContext *s)
426 /* If the singlestep state is Active-not-pending, advance to
427 * Active-pending.
429 if (s->ss_active) {
430 s->pstate_ss = 0;
431 gen_helper_clear_pstate_ss(cpu_env);
435 static void gen_step_complete_exception(DisasContext *s)
437 /* We just completed step of an insn. Move from Active-not-pending
438 * to Active-pending, and then also take the swstep exception.
439 * This corresponds to making the (IMPDEF) choice to prioritize
440 * swstep exceptions over asynchronous exceptions taken to an exception
441 * level where debug is disabled. This choice has the advantage that
442 * we do not need to maintain internal state corresponding to the
443 * ISV/EX syndrome bits between completion of the step and generation
444 * of the exception, and our syndrome information is always correct.
446 gen_ss_advance(s);
447 gen_exception(EXCP_UDEF, syn_swstep(s->ss_same_el, 1, s->is_ldex),
448 default_exception_el(s));
449 s->base.is_jmp = DISAS_NORETURN;
452 static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
454 /* No direct tb linking with singlestep (either QEMU's or the ARM
455 * debug architecture kind) or deterministic io
457 if (s->base.singlestep_enabled || s->ss_active ||
458 (tb_cflags(s->base.tb) & CF_LAST_IO)) {
459 return false;
462 #ifndef CONFIG_USER_ONLY
463 /* Only link tbs from inside the same guest page */
464 if ((s->base.tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
465 return false;
467 #endif
469 return true;
472 static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
474 TranslationBlock *tb;
476 tb = s->base.tb;
477 if (use_goto_tb(s, n, dest)) {
478 tcg_gen_goto_tb(n);
479 gen_a64_set_pc_im(dest);
480 tcg_gen_exit_tb(tb, n);
481 s->base.is_jmp = DISAS_NORETURN;
482 } else {
483 gen_a64_set_pc_im(dest);
484 if (s->ss_active) {
485 gen_step_complete_exception(s);
486 } else if (s->base.singlestep_enabled) {
487 gen_exception_internal(EXCP_DEBUG);
488 } else {
489 tcg_gen_lookup_and_goto_ptr();
490 s->base.is_jmp = DISAS_NORETURN;
495 void unallocated_encoding(DisasContext *s)
497 /* Unallocated and reserved encodings are uncategorized */
498 gen_exception_insn(s, 4, EXCP_UDEF, syn_uncategorized(),
499 default_exception_el(s));
502 static void init_tmp_a64_array(DisasContext *s)
504 #ifdef CONFIG_DEBUG_TCG
505 memset(s->tmp_a64, 0, sizeof(s->tmp_a64));
506 #endif
507 s->tmp_a64_count = 0;
510 static void free_tmp_a64(DisasContext *s)
512 int i;
513 for (i = 0; i < s->tmp_a64_count; i++) {
514 tcg_temp_free_i64(s->tmp_a64[i]);
516 init_tmp_a64_array(s);
519 TCGv_i64 new_tmp_a64(DisasContext *s)
521 assert(s->tmp_a64_count < TMP_A64_MAX);
522 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
525 TCGv_i64 new_tmp_a64_zero(DisasContext *s)
527 TCGv_i64 t = new_tmp_a64(s);
528 tcg_gen_movi_i64(t, 0);
529 return t;
533 * Register access functions
535 * These functions are used for directly accessing a register in where
536 * changes to the final register value are likely to be made. If you
537 * need to use a register for temporary calculation (e.g. index type
538 * operations) use the read_* form.
540 * B1.2.1 Register mappings
542 * In instruction register encoding 31 can refer to ZR (zero register) or
543 * the SP (stack pointer) depending on context. In QEMU's case we map SP
544 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
545 * This is the point of the _sp forms.
547 TCGv_i64 cpu_reg(DisasContext *s, int reg)
549 if (reg == 31) {
550 return new_tmp_a64_zero(s);
551 } else {
552 return cpu_X[reg];
556 /* register access for when 31 == SP */
557 TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
559 return cpu_X[reg];
562 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
563 * representing the register contents. This TCGv is an auto-freed
564 * temporary so it need not be explicitly freed, and may be modified.
566 TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
568 TCGv_i64 v = new_tmp_a64(s);
569 if (reg != 31) {
570 if (sf) {
571 tcg_gen_mov_i64(v, cpu_X[reg]);
572 } else {
573 tcg_gen_ext32u_i64(v, cpu_X[reg]);
575 } else {
576 tcg_gen_movi_i64(v, 0);
578 return v;
581 TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
583 TCGv_i64 v = new_tmp_a64(s);
584 if (sf) {
585 tcg_gen_mov_i64(v, cpu_X[reg]);
586 } else {
587 tcg_gen_ext32u_i64(v, cpu_X[reg]);
589 return v;
592 /* Return the offset into CPUARMState of a slice (from
593 * the least significant end) of FP register Qn (ie
594 * Dn, Sn, Hn or Bn).
595 * (Note that this is not the same mapping as for A32; see cpu.h)
597 static inline int fp_reg_offset(DisasContext *s, int regno, TCGMemOp size)
599 return vec_reg_offset(s, regno, 0, size);
602 /* Offset of the high half of the 128 bit vector Qn */
603 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
605 return vec_reg_offset(s, regno, 1, MO_64);
608 /* Convenience accessors for reading and writing single and double
609 * FP registers. Writing clears the upper parts of the associated
610 * 128 bit vector register, as required by the architecture.
611 * Note that unlike the GP register accessors, the values returned
612 * by the read functions must be manually freed.
614 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
616 TCGv_i64 v = tcg_temp_new_i64();
618 tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
619 return v;
622 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
624 TCGv_i32 v = tcg_temp_new_i32();
626 tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
627 return v;
630 static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
632 TCGv_i32 v = tcg_temp_new_i32();
634 tcg_gen_ld16u_i32(v, cpu_env, fp_reg_offset(s, reg, MO_16));
635 return v;
638 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
639 * If SVE is not enabled, then there are only 128 bits in the vector.
641 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
643 unsigned ofs = fp_reg_offset(s, rd, MO_64);
644 unsigned vsz = vec_full_reg_size(s);
646 if (!is_q) {
647 TCGv_i64 tcg_zero = tcg_const_i64(0);
648 tcg_gen_st_i64(tcg_zero, cpu_env, ofs + 8);
649 tcg_temp_free_i64(tcg_zero);
651 if (vsz > 16) {
652 tcg_gen_gvec_dup8i(ofs + 16, vsz - 16, vsz - 16, 0);
656 void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
658 unsigned ofs = fp_reg_offset(s, reg, MO_64);
660 tcg_gen_st_i64(v, cpu_env, ofs);
661 clear_vec_high(s, false, reg);
664 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
666 TCGv_i64 tmp = tcg_temp_new_i64();
668 tcg_gen_extu_i32_i64(tmp, v);
669 write_fp_dreg(s, reg, tmp);
670 tcg_temp_free_i64(tmp);
673 TCGv_ptr get_fpstatus_ptr(bool is_f16)
675 TCGv_ptr statusptr = tcg_temp_new_ptr();
676 int offset;
678 /* In A64 all instructions (both FP and Neon) use the FPCR; there
679 * is no equivalent of the A32 Neon "standard FPSCR value".
680 * However half-precision operations operate under a different
681 * FZ16 flag and use vfp.fp_status_f16 instead of vfp.fp_status.
683 if (is_f16) {
684 offset = offsetof(CPUARMState, vfp.fp_status_f16);
685 } else {
686 offset = offsetof(CPUARMState, vfp.fp_status);
688 tcg_gen_addi_ptr(statusptr, cpu_env, offset);
689 return statusptr;
692 /* Expand a 2-operand AdvSIMD vector operation using an expander function. */
693 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
694 GVecGen2Fn *gvec_fn, int vece)
696 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
697 is_q ? 16 : 8, vec_full_reg_size(s));
700 /* Expand a 2-operand + immediate AdvSIMD vector operation using
701 * an expander function.
703 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
704 int64_t imm, GVecGen2iFn *gvec_fn, int vece)
706 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
707 imm, is_q ? 16 : 8, vec_full_reg_size(s));
710 /* Expand a 3-operand AdvSIMD vector operation using an expander function. */
711 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
712 GVecGen3Fn *gvec_fn, int vece)
714 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
715 vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
718 /* Expand a 2-operand + immediate AdvSIMD vector operation using
719 * an op descriptor.
721 static void gen_gvec_op2i(DisasContext *s, bool is_q, int rd,
722 int rn, int64_t imm, const GVecGen2i *gvec_op)
724 tcg_gen_gvec_2i(vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
725 is_q ? 16 : 8, vec_full_reg_size(s), imm, gvec_op);
728 /* Expand a 3-operand AdvSIMD vector operation using an op descriptor. */
729 static void gen_gvec_op3(DisasContext *s, bool is_q, int rd,
730 int rn, int rm, const GVecGen3 *gvec_op)
732 tcg_gen_gvec_3(vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
733 vec_full_reg_offset(s, rm), is_q ? 16 : 8,
734 vec_full_reg_size(s), gvec_op);
737 /* Expand a 3-operand operation using an out-of-line helper. */
738 static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
739 int rn, int rm, int data, gen_helper_gvec_3 *fn)
741 tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
742 vec_full_reg_offset(s, rn),
743 vec_full_reg_offset(s, rm),
744 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
747 /* Expand a 3-operand + env pointer operation using
748 * an out-of-line helper.
750 static void gen_gvec_op3_env(DisasContext *s, bool is_q, int rd,
751 int rn, int rm, gen_helper_gvec_3_ptr *fn)
753 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
754 vec_full_reg_offset(s, rn),
755 vec_full_reg_offset(s, rm), cpu_env,
756 is_q ? 16 : 8, vec_full_reg_size(s), 0, fn);
759 /* Expand a 3-operand + fpstatus pointer + simd data value operation using
760 * an out-of-line helper.
762 static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
763 int rm, bool is_fp16, int data,
764 gen_helper_gvec_3_ptr *fn)
766 TCGv_ptr fpst = get_fpstatus_ptr(is_fp16);
767 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
768 vec_full_reg_offset(s, rn),
769 vec_full_reg_offset(s, rm), fpst,
770 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
771 tcg_temp_free_ptr(fpst);
774 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
775 * than the 32 bit equivalent.
777 static inline void gen_set_NZ64(TCGv_i64 result)
779 tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
780 tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
783 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
784 static inline void gen_logic_CC(int sf, TCGv_i64 result)
786 if (sf) {
787 gen_set_NZ64(result);
788 } else {
789 tcg_gen_extrl_i64_i32(cpu_ZF, result);
790 tcg_gen_mov_i32(cpu_NF, cpu_ZF);
792 tcg_gen_movi_i32(cpu_CF, 0);
793 tcg_gen_movi_i32(cpu_VF, 0);
796 /* dest = T0 + T1; compute C, N, V and Z flags */
797 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
799 if (sf) {
800 TCGv_i64 result, flag, tmp;
801 result = tcg_temp_new_i64();
802 flag = tcg_temp_new_i64();
803 tmp = tcg_temp_new_i64();
805 tcg_gen_movi_i64(tmp, 0);
806 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
808 tcg_gen_extrl_i64_i32(cpu_CF, flag);
810 gen_set_NZ64(result);
812 tcg_gen_xor_i64(flag, result, t0);
813 tcg_gen_xor_i64(tmp, t0, t1);
814 tcg_gen_andc_i64(flag, flag, tmp);
815 tcg_temp_free_i64(tmp);
816 tcg_gen_extrh_i64_i32(cpu_VF, flag);
818 tcg_gen_mov_i64(dest, result);
819 tcg_temp_free_i64(result);
820 tcg_temp_free_i64(flag);
821 } else {
822 /* 32 bit arithmetic */
823 TCGv_i32 t0_32 = tcg_temp_new_i32();
824 TCGv_i32 t1_32 = tcg_temp_new_i32();
825 TCGv_i32 tmp = tcg_temp_new_i32();
827 tcg_gen_movi_i32(tmp, 0);
828 tcg_gen_extrl_i64_i32(t0_32, t0);
829 tcg_gen_extrl_i64_i32(t1_32, t1);
830 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
831 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
832 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
833 tcg_gen_xor_i32(tmp, t0_32, t1_32);
834 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
835 tcg_gen_extu_i32_i64(dest, cpu_NF);
837 tcg_temp_free_i32(tmp);
838 tcg_temp_free_i32(t0_32);
839 tcg_temp_free_i32(t1_32);
843 /* dest = T0 - T1; compute C, N, V and Z flags */
844 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
846 if (sf) {
847 /* 64 bit arithmetic */
848 TCGv_i64 result, flag, tmp;
850 result = tcg_temp_new_i64();
851 flag = tcg_temp_new_i64();
852 tcg_gen_sub_i64(result, t0, t1);
854 gen_set_NZ64(result);
856 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
857 tcg_gen_extrl_i64_i32(cpu_CF, flag);
859 tcg_gen_xor_i64(flag, result, t0);
860 tmp = tcg_temp_new_i64();
861 tcg_gen_xor_i64(tmp, t0, t1);
862 tcg_gen_and_i64(flag, flag, tmp);
863 tcg_temp_free_i64(tmp);
864 tcg_gen_extrh_i64_i32(cpu_VF, flag);
865 tcg_gen_mov_i64(dest, result);
866 tcg_temp_free_i64(flag);
867 tcg_temp_free_i64(result);
868 } else {
869 /* 32 bit arithmetic */
870 TCGv_i32 t0_32 = tcg_temp_new_i32();
871 TCGv_i32 t1_32 = tcg_temp_new_i32();
872 TCGv_i32 tmp;
874 tcg_gen_extrl_i64_i32(t0_32, t0);
875 tcg_gen_extrl_i64_i32(t1_32, t1);
876 tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
877 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
878 tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
879 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
880 tmp = tcg_temp_new_i32();
881 tcg_gen_xor_i32(tmp, t0_32, t1_32);
882 tcg_temp_free_i32(t0_32);
883 tcg_temp_free_i32(t1_32);
884 tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
885 tcg_temp_free_i32(tmp);
886 tcg_gen_extu_i32_i64(dest, cpu_NF);
890 /* dest = T0 + T1 + CF; do not compute flags. */
891 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
893 TCGv_i64 flag = tcg_temp_new_i64();
894 tcg_gen_extu_i32_i64(flag, cpu_CF);
895 tcg_gen_add_i64(dest, t0, t1);
896 tcg_gen_add_i64(dest, dest, flag);
897 tcg_temp_free_i64(flag);
899 if (!sf) {
900 tcg_gen_ext32u_i64(dest, dest);
904 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
905 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
907 if (sf) {
908 TCGv_i64 result, cf_64, vf_64, tmp;
909 result = tcg_temp_new_i64();
910 cf_64 = tcg_temp_new_i64();
911 vf_64 = tcg_temp_new_i64();
912 tmp = tcg_const_i64(0);
914 tcg_gen_extu_i32_i64(cf_64, cpu_CF);
915 tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
916 tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
917 tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
918 gen_set_NZ64(result);
920 tcg_gen_xor_i64(vf_64, result, t0);
921 tcg_gen_xor_i64(tmp, t0, t1);
922 tcg_gen_andc_i64(vf_64, vf_64, tmp);
923 tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
925 tcg_gen_mov_i64(dest, result);
927 tcg_temp_free_i64(tmp);
928 tcg_temp_free_i64(vf_64);
929 tcg_temp_free_i64(cf_64);
930 tcg_temp_free_i64(result);
931 } else {
932 TCGv_i32 t0_32, t1_32, tmp;
933 t0_32 = tcg_temp_new_i32();
934 t1_32 = tcg_temp_new_i32();
935 tmp = tcg_const_i32(0);
937 tcg_gen_extrl_i64_i32(t0_32, t0);
938 tcg_gen_extrl_i64_i32(t1_32, t1);
939 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
940 tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
942 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
943 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
944 tcg_gen_xor_i32(tmp, t0_32, t1_32);
945 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
946 tcg_gen_extu_i32_i64(dest, cpu_NF);
948 tcg_temp_free_i32(tmp);
949 tcg_temp_free_i32(t1_32);
950 tcg_temp_free_i32(t0_32);
955 * Load/Store generators
959 * Store from GPR register to memory.
961 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
962 TCGv_i64 tcg_addr, int size, int memidx,
963 bool iss_valid,
964 unsigned int iss_srt,
965 bool iss_sf, bool iss_ar)
967 g_assert(size <= 3);
968 tcg_gen_qemu_st_i64(source, tcg_addr, memidx, s->be_data + size);
970 if (iss_valid) {
971 uint32_t syn;
973 syn = syn_data_abort_with_iss(0,
974 size,
975 false,
976 iss_srt,
977 iss_sf,
978 iss_ar,
979 0, 0, 0, 0, 0, false);
980 disas_set_insn_syndrome(s, syn);
984 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
985 TCGv_i64 tcg_addr, int size,
986 bool iss_valid,
987 unsigned int iss_srt,
988 bool iss_sf, bool iss_ar)
990 do_gpr_st_memidx(s, source, tcg_addr, size, get_mem_index(s),
991 iss_valid, iss_srt, iss_sf, iss_ar);
995 * Load from memory to GPR register
997 static void do_gpr_ld_memidx(DisasContext *s,
998 TCGv_i64 dest, TCGv_i64 tcg_addr,
999 int size, bool is_signed,
1000 bool extend, int memidx,
1001 bool iss_valid, unsigned int iss_srt,
1002 bool iss_sf, bool iss_ar)
1004 TCGMemOp memop = s->be_data + size;
1006 g_assert(size <= 3);
1008 if (is_signed) {
1009 memop += MO_SIGN;
1012 tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
1014 if (extend && is_signed) {
1015 g_assert(size < 3);
1016 tcg_gen_ext32u_i64(dest, dest);
1019 if (iss_valid) {
1020 uint32_t syn;
1022 syn = syn_data_abort_with_iss(0,
1023 size,
1024 is_signed,
1025 iss_srt,
1026 iss_sf,
1027 iss_ar,
1028 0, 0, 0, 0, 0, false);
1029 disas_set_insn_syndrome(s, syn);
1033 static void do_gpr_ld(DisasContext *s,
1034 TCGv_i64 dest, TCGv_i64 tcg_addr,
1035 int size, bool is_signed, bool extend,
1036 bool iss_valid, unsigned int iss_srt,
1037 bool iss_sf, bool iss_ar)
1039 do_gpr_ld_memidx(s, dest, tcg_addr, size, is_signed, extend,
1040 get_mem_index(s),
1041 iss_valid, iss_srt, iss_sf, iss_ar);
1045 * Store from FP register to memory
1047 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
1049 /* This writes the bottom N bits of a 128 bit wide vector to memory */
1050 TCGv_i64 tmp = tcg_temp_new_i64();
1051 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_offset(s, srcidx, MO_64));
1052 if (size < 4) {
1053 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s),
1054 s->be_data + size);
1055 } else {
1056 bool be = s->be_data == MO_BE;
1057 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
1059 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
1060 tcg_gen_qemu_st_i64(tmp, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
1061 s->be_data | MO_Q);
1062 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_hi_offset(s, srcidx));
1063 tcg_gen_qemu_st_i64(tmp, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
1064 s->be_data | MO_Q);
1065 tcg_temp_free_i64(tcg_hiaddr);
1068 tcg_temp_free_i64(tmp);
1072 * Load from memory to FP register
1074 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
1076 /* This always zero-extends and writes to a full 128 bit wide vector */
1077 TCGv_i64 tmplo = tcg_temp_new_i64();
1078 TCGv_i64 tmphi;
1080 if (size < 4) {
1081 TCGMemOp memop = s->be_data + size;
1082 tmphi = tcg_const_i64(0);
1083 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
1084 } else {
1085 bool be = s->be_data == MO_BE;
1086 TCGv_i64 tcg_hiaddr;
1088 tmphi = tcg_temp_new_i64();
1089 tcg_hiaddr = tcg_temp_new_i64();
1091 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
1092 tcg_gen_qemu_ld_i64(tmplo, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
1093 s->be_data | MO_Q);
1094 tcg_gen_qemu_ld_i64(tmphi, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
1095 s->be_data | MO_Q);
1096 tcg_temp_free_i64(tcg_hiaddr);
1099 tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
1100 tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
1102 tcg_temp_free_i64(tmplo);
1103 tcg_temp_free_i64(tmphi);
1105 clear_vec_high(s, true, destidx);
1109 * Vector load/store helpers.
1111 * The principal difference between this and a FP load is that we don't
1112 * zero extend as we are filling a partial chunk of the vector register.
1113 * These functions don't support 128 bit loads/stores, which would be
1114 * normal load/store operations.
1116 * The _i32 versions are useful when operating on 32 bit quantities
1117 * (eg for floating point single or using Neon helper functions).
1120 /* Get value of an element within a vector register */
1121 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
1122 int element, TCGMemOp memop)
1124 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1125 switch (memop) {
1126 case MO_8:
1127 tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
1128 break;
1129 case MO_16:
1130 tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
1131 break;
1132 case MO_32:
1133 tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
1134 break;
1135 case MO_8|MO_SIGN:
1136 tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
1137 break;
1138 case MO_16|MO_SIGN:
1139 tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
1140 break;
1141 case MO_32|MO_SIGN:
1142 tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
1143 break;
1144 case MO_64:
1145 case MO_64|MO_SIGN:
1146 tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
1147 break;
1148 default:
1149 g_assert_not_reached();
1153 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1154 int element, TCGMemOp memop)
1156 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1157 switch (memop) {
1158 case MO_8:
1159 tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1160 break;
1161 case MO_16:
1162 tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1163 break;
1164 case MO_8|MO_SIGN:
1165 tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1166 break;
1167 case MO_16|MO_SIGN:
1168 tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1169 break;
1170 case MO_32:
1171 case MO_32|MO_SIGN:
1172 tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1173 break;
1174 default:
1175 g_assert_not_reached();
1179 /* Set value of an element within a vector register */
1180 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1181 int element, TCGMemOp memop)
1183 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1184 switch (memop) {
1185 case MO_8:
1186 tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1187 break;
1188 case MO_16:
1189 tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1190 break;
1191 case MO_32:
1192 tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1193 break;
1194 case MO_64:
1195 tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1196 break;
1197 default:
1198 g_assert_not_reached();
1202 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1203 int destidx, int element, TCGMemOp memop)
1205 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1206 switch (memop) {
1207 case MO_8:
1208 tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1209 break;
1210 case MO_16:
1211 tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1212 break;
1213 case MO_32:
1214 tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1215 break;
1216 default:
1217 g_assert_not_reached();
1221 /* Store from vector register to memory */
1222 static void do_vec_st(DisasContext *s, int srcidx, int element,
1223 TCGv_i64 tcg_addr, int size, TCGMemOp endian)
1225 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1227 read_vec_element(s, tcg_tmp, srcidx, element, size);
1228 tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), endian | size);
1230 tcg_temp_free_i64(tcg_tmp);
1233 /* Load from memory to vector register */
1234 static void do_vec_ld(DisasContext *s, int destidx, int element,
1235 TCGv_i64 tcg_addr, int size, TCGMemOp endian)
1237 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1239 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), endian | size);
1240 write_vec_element(s, tcg_tmp, destidx, element, size);
1242 tcg_temp_free_i64(tcg_tmp);
1245 /* Check that FP/Neon access is enabled. If it is, return
1246 * true. If not, emit code to generate an appropriate exception,
1247 * and return false; the caller should not emit any code for
1248 * the instruction. Note that this check must happen after all
1249 * unallocated-encoding checks (otherwise the syndrome information
1250 * for the resulting exception will be incorrect).
1252 static inline bool fp_access_check(DisasContext *s)
1254 assert(!s->fp_access_checked);
1255 s->fp_access_checked = true;
1257 if (!s->fp_excp_el) {
1258 return true;
1261 gen_exception_insn(s, 4, EXCP_UDEF, syn_fp_access_trap(1, 0xe, false),
1262 s->fp_excp_el);
1263 return false;
1266 /* Check that SVE access is enabled. If it is, return true.
1267 * If not, emit code to generate an appropriate exception and return false.
1269 bool sve_access_check(DisasContext *s)
1271 if (s->sve_excp_el) {
1272 gen_exception_insn(s, 4, EXCP_UDEF, syn_sve_access_trap(),
1273 s->sve_excp_el);
1274 return false;
1276 return fp_access_check(s);
1280 * This utility function is for doing register extension with an
1281 * optional shift. You will likely want to pass a temporary for the
1282 * destination register. See DecodeRegExtend() in the ARM ARM.
1284 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1285 int option, unsigned int shift)
1287 int extsize = extract32(option, 0, 2);
1288 bool is_signed = extract32(option, 2, 1);
1290 if (is_signed) {
1291 switch (extsize) {
1292 case 0:
1293 tcg_gen_ext8s_i64(tcg_out, tcg_in);
1294 break;
1295 case 1:
1296 tcg_gen_ext16s_i64(tcg_out, tcg_in);
1297 break;
1298 case 2:
1299 tcg_gen_ext32s_i64(tcg_out, tcg_in);
1300 break;
1301 case 3:
1302 tcg_gen_mov_i64(tcg_out, tcg_in);
1303 break;
1305 } else {
1306 switch (extsize) {
1307 case 0:
1308 tcg_gen_ext8u_i64(tcg_out, tcg_in);
1309 break;
1310 case 1:
1311 tcg_gen_ext16u_i64(tcg_out, tcg_in);
1312 break;
1313 case 2:
1314 tcg_gen_ext32u_i64(tcg_out, tcg_in);
1315 break;
1316 case 3:
1317 tcg_gen_mov_i64(tcg_out, tcg_in);
1318 break;
1322 if (shift) {
1323 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1327 static inline void gen_check_sp_alignment(DisasContext *s)
1329 /* The AArch64 architecture mandates that (if enabled via PSTATE
1330 * or SCTLR bits) there is a check that SP is 16-aligned on every
1331 * SP-relative load or store (with an exception generated if it is not).
1332 * In line with general QEMU practice regarding misaligned accesses,
1333 * we omit these checks for the sake of guest program performance.
1334 * This function is provided as a hook so we can more easily add these
1335 * checks in future (possibly as a "favour catching guest program bugs
1336 * over speed" user selectable option).
1341 * This provides a simple table based table lookup decoder. It is
1342 * intended to be used when the relevant bits for decode are too
1343 * awkwardly placed and switch/if based logic would be confusing and
1344 * deeply nested. Since it's a linear search through the table, tables
1345 * should be kept small.
1347 * It returns the first handler where insn & mask == pattern, or
1348 * NULL if there is no match.
1349 * The table is terminated by an empty mask (i.e. 0)
1351 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1352 uint32_t insn)
1354 const AArch64DecodeTable *tptr = table;
1356 while (tptr->mask) {
1357 if ((insn & tptr->mask) == tptr->pattern) {
1358 return tptr->disas_fn;
1360 tptr++;
1362 return NULL;
1366 * The instruction disassembly implemented here matches
1367 * the instruction encoding classifications in chapter C4
1368 * of the ARM Architecture Reference Manual (DDI0487B_a);
1369 * classification names and decode diagrams here should generally
1370 * match up with those in the manual.
1373 /* Unconditional branch (immediate)
1374 * 31 30 26 25 0
1375 * +----+-----------+-------------------------------------+
1376 * | op | 0 0 1 0 1 | imm26 |
1377 * +----+-----------+-------------------------------------+
1379 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
1381 uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
1383 if (insn & (1U << 31)) {
1384 /* BL Branch with link */
1385 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1388 /* B Branch / BL Branch with link */
1389 reset_btype(s);
1390 gen_goto_tb(s, 0, addr);
1393 /* Compare and branch (immediate)
1394 * 31 30 25 24 23 5 4 0
1395 * +----+-------------+----+---------------------+--------+
1396 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
1397 * +----+-------------+----+---------------------+--------+
1399 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
1401 unsigned int sf, op, rt;
1402 uint64_t addr;
1403 TCGLabel *label_match;
1404 TCGv_i64 tcg_cmp;
1406 sf = extract32(insn, 31, 1);
1407 op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
1408 rt = extract32(insn, 0, 5);
1409 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1411 tcg_cmp = read_cpu_reg(s, rt, sf);
1412 label_match = gen_new_label();
1414 reset_btype(s);
1415 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1416 tcg_cmp, 0, label_match);
1418 gen_goto_tb(s, 0, s->pc);
1419 gen_set_label(label_match);
1420 gen_goto_tb(s, 1, addr);
1423 /* Test and branch (immediate)
1424 * 31 30 25 24 23 19 18 5 4 0
1425 * +----+-------------+----+-------+-------------+------+
1426 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
1427 * +----+-------------+----+-------+-------------+------+
1429 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1431 unsigned int bit_pos, op, rt;
1432 uint64_t addr;
1433 TCGLabel *label_match;
1434 TCGv_i64 tcg_cmp;
1436 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1437 op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1438 addr = s->pc + sextract32(insn, 5, 14) * 4 - 4;
1439 rt = extract32(insn, 0, 5);
1441 tcg_cmp = tcg_temp_new_i64();
1442 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1443 label_match = gen_new_label();
1445 reset_btype(s);
1446 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1447 tcg_cmp, 0, label_match);
1448 tcg_temp_free_i64(tcg_cmp);
1449 gen_goto_tb(s, 0, s->pc);
1450 gen_set_label(label_match);
1451 gen_goto_tb(s, 1, addr);
1454 /* Conditional branch (immediate)
1455 * 31 25 24 23 5 4 3 0
1456 * +---------------+----+---------------------+----+------+
1457 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
1458 * +---------------+----+---------------------+----+------+
1460 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1462 unsigned int cond;
1463 uint64_t addr;
1465 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1466 unallocated_encoding(s);
1467 return;
1469 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1470 cond = extract32(insn, 0, 4);
1472 reset_btype(s);
1473 if (cond < 0x0e) {
1474 /* genuinely conditional branches */
1475 TCGLabel *label_match = gen_new_label();
1476 arm_gen_test_cc(cond, label_match);
1477 gen_goto_tb(s, 0, s->pc);
1478 gen_set_label(label_match);
1479 gen_goto_tb(s, 1, addr);
1480 } else {
1481 /* 0xe and 0xf are both "always" conditions */
1482 gen_goto_tb(s, 0, addr);
1486 /* HINT instruction group, including various allocated HINTs */
1487 static void handle_hint(DisasContext *s, uint32_t insn,
1488 unsigned int op1, unsigned int op2, unsigned int crm)
1490 unsigned int selector = crm << 3 | op2;
1492 if (op1 != 3) {
1493 unallocated_encoding(s);
1494 return;
1497 switch (selector) {
1498 case 0b00000: /* NOP */
1499 break;
1500 case 0b00011: /* WFI */
1501 s->base.is_jmp = DISAS_WFI;
1502 break;
1503 case 0b00001: /* YIELD */
1504 /* When running in MTTCG we don't generate jumps to the yield and
1505 * WFE helpers as it won't affect the scheduling of other vCPUs.
1506 * If we wanted to more completely model WFE/SEV so we don't busy
1507 * spin unnecessarily we would need to do something more involved.
1509 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1510 s->base.is_jmp = DISAS_YIELD;
1512 break;
1513 case 0b00010: /* WFE */
1514 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1515 s->base.is_jmp = DISAS_WFE;
1517 break;
1518 case 0b00100: /* SEV */
1519 case 0b00101: /* SEVL */
1520 /* we treat all as NOP at least for now */
1521 break;
1522 case 0b00111: /* XPACLRI */
1523 if (s->pauth_active) {
1524 gen_helper_xpaci(cpu_X[30], cpu_env, cpu_X[30]);
1526 break;
1527 case 0b01000: /* PACIA1716 */
1528 if (s->pauth_active) {
1529 gen_helper_pacia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1531 break;
1532 case 0b01010: /* PACIB1716 */
1533 if (s->pauth_active) {
1534 gen_helper_pacib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1536 break;
1537 case 0b01100: /* AUTIA1716 */
1538 if (s->pauth_active) {
1539 gen_helper_autia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1541 break;
1542 case 0b01110: /* AUTIB1716 */
1543 if (s->pauth_active) {
1544 gen_helper_autib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1546 break;
1547 case 0b11000: /* PACIAZ */
1548 if (s->pauth_active) {
1549 gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30],
1550 new_tmp_a64_zero(s));
1552 break;
1553 case 0b11001: /* PACIASP */
1554 if (s->pauth_active) {
1555 gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1557 break;
1558 case 0b11010: /* PACIBZ */
1559 if (s->pauth_active) {
1560 gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30],
1561 new_tmp_a64_zero(s));
1563 break;
1564 case 0b11011: /* PACIBSP */
1565 if (s->pauth_active) {
1566 gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1568 break;
1569 case 0b11100: /* AUTIAZ */
1570 if (s->pauth_active) {
1571 gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30],
1572 new_tmp_a64_zero(s));
1574 break;
1575 case 0b11101: /* AUTIASP */
1576 if (s->pauth_active) {
1577 gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1579 break;
1580 case 0b11110: /* AUTIBZ */
1581 if (s->pauth_active) {
1582 gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30],
1583 new_tmp_a64_zero(s));
1585 break;
1586 case 0b11111: /* AUTIBSP */
1587 if (s->pauth_active) {
1588 gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1590 break;
1591 default:
1592 /* default specified as NOP equivalent */
1593 break;
1597 static void gen_clrex(DisasContext *s, uint32_t insn)
1599 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1602 /* CLREX, DSB, DMB, ISB */
1603 static void handle_sync(DisasContext *s, uint32_t insn,
1604 unsigned int op1, unsigned int op2, unsigned int crm)
1606 TCGBar bar;
1608 if (op1 != 3) {
1609 unallocated_encoding(s);
1610 return;
1613 switch (op2) {
1614 case 2: /* CLREX */
1615 gen_clrex(s, insn);
1616 return;
1617 case 4: /* DSB */
1618 case 5: /* DMB */
1619 switch (crm & 3) {
1620 case 1: /* MBReqTypes_Reads */
1621 bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1622 break;
1623 case 2: /* MBReqTypes_Writes */
1624 bar = TCG_BAR_SC | TCG_MO_ST_ST;
1625 break;
1626 default: /* MBReqTypes_All */
1627 bar = TCG_BAR_SC | TCG_MO_ALL;
1628 break;
1630 tcg_gen_mb(bar);
1631 return;
1632 case 6: /* ISB */
1633 /* We need to break the TB after this insn to execute
1634 * a self-modified code correctly and also to take
1635 * any pending interrupts immediately.
1637 reset_btype(s);
1638 gen_goto_tb(s, 0, s->pc);
1639 return;
1640 default:
1641 unallocated_encoding(s);
1642 return;
1646 /* MSR (immediate) - move immediate to processor state field */
1647 static void handle_msr_i(DisasContext *s, uint32_t insn,
1648 unsigned int op1, unsigned int op2, unsigned int crm)
1650 int op = op1 << 3 | op2;
1651 switch (op) {
1652 case 0x05: /* SPSel */
1653 if (s->current_el == 0) {
1654 unallocated_encoding(s);
1655 return;
1657 /* fall through */
1658 case 0x1e: /* DAIFSet */
1659 case 0x1f: /* DAIFClear */
1661 TCGv_i32 tcg_imm = tcg_const_i32(crm);
1662 TCGv_i32 tcg_op = tcg_const_i32(op);
1663 gen_a64_set_pc_im(s->pc - 4);
1664 gen_helper_msr_i_pstate(cpu_env, tcg_op, tcg_imm);
1665 tcg_temp_free_i32(tcg_imm);
1666 tcg_temp_free_i32(tcg_op);
1667 /* For DAIFClear, exit the cpu loop to re-evaluate pending IRQs. */
1668 gen_a64_set_pc_im(s->pc);
1669 s->base.is_jmp = (op == 0x1f ? DISAS_EXIT : DISAS_JUMP);
1670 break;
1672 default:
1673 unallocated_encoding(s);
1674 return;
1678 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1680 TCGv_i32 tmp = tcg_temp_new_i32();
1681 TCGv_i32 nzcv = tcg_temp_new_i32();
1683 /* build bit 31, N */
1684 tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
1685 /* build bit 30, Z */
1686 tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1687 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1688 /* build bit 29, C */
1689 tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1690 /* build bit 28, V */
1691 tcg_gen_shri_i32(tmp, cpu_VF, 31);
1692 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1693 /* generate result */
1694 tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1696 tcg_temp_free_i32(nzcv);
1697 tcg_temp_free_i32(tmp);
1700 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1703 TCGv_i32 nzcv = tcg_temp_new_i32();
1705 /* take NZCV from R[t] */
1706 tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
1708 /* bit 31, N */
1709 tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
1710 /* bit 30, Z */
1711 tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1712 tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1713 /* bit 29, C */
1714 tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1715 tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1716 /* bit 28, V */
1717 tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1718 tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1719 tcg_temp_free_i32(nzcv);
1722 /* MRS - move from system register
1723 * MSR (register) - move to system register
1724 * SYS
1725 * SYSL
1726 * These are all essentially the same insn in 'read' and 'write'
1727 * versions, with varying op0 fields.
1729 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1730 unsigned int op0, unsigned int op1, unsigned int op2,
1731 unsigned int crn, unsigned int crm, unsigned int rt)
1733 const ARMCPRegInfo *ri;
1734 TCGv_i64 tcg_rt;
1736 ri = get_arm_cp_reginfo(s->cp_regs,
1737 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1738 crn, crm, op0, op1, op2));
1740 if (!ri) {
1741 /* Unknown register; this might be a guest error or a QEMU
1742 * unimplemented feature.
1744 qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1745 "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1746 isread ? "read" : "write", op0, op1, crn, crm, op2);
1747 unallocated_encoding(s);
1748 return;
1751 /* Check access permissions */
1752 if (!cp_access_ok(s->current_el, ri, isread)) {
1753 unallocated_encoding(s);
1754 return;
1757 if (ri->accessfn) {
1758 /* Emit code to perform further access permissions checks at
1759 * runtime; this may result in an exception.
1761 TCGv_ptr tmpptr;
1762 TCGv_i32 tcg_syn, tcg_isread;
1763 uint32_t syndrome;
1765 gen_a64_set_pc_im(s->pc - 4);
1766 tmpptr = tcg_const_ptr(ri);
1767 syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1768 tcg_syn = tcg_const_i32(syndrome);
1769 tcg_isread = tcg_const_i32(isread);
1770 gen_helper_access_check_cp_reg(cpu_env, tmpptr, tcg_syn, tcg_isread);
1771 tcg_temp_free_ptr(tmpptr);
1772 tcg_temp_free_i32(tcg_syn);
1773 tcg_temp_free_i32(tcg_isread);
1776 /* Handle special cases first */
1777 switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
1778 case ARM_CP_NOP:
1779 return;
1780 case ARM_CP_NZCV:
1781 tcg_rt = cpu_reg(s, rt);
1782 if (isread) {
1783 gen_get_nzcv(tcg_rt);
1784 } else {
1785 gen_set_nzcv(tcg_rt);
1787 return;
1788 case ARM_CP_CURRENTEL:
1789 /* Reads as current EL value from pstate, which is
1790 * guaranteed to be constant by the tb flags.
1792 tcg_rt = cpu_reg(s, rt);
1793 tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
1794 return;
1795 case ARM_CP_DC_ZVA:
1796 /* Writes clear the aligned block of memory which rt points into. */
1797 tcg_rt = cpu_reg(s, rt);
1798 gen_helper_dc_zva(cpu_env, tcg_rt);
1799 return;
1800 default:
1801 break;
1803 if ((ri->type & ARM_CP_FPU) && !fp_access_check(s)) {
1804 return;
1805 } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
1806 return;
1809 if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1810 gen_io_start();
1813 tcg_rt = cpu_reg(s, rt);
1815 if (isread) {
1816 if (ri->type & ARM_CP_CONST) {
1817 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1818 } else if (ri->readfn) {
1819 TCGv_ptr tmpptr;
1820 tmpptr = tcg_const_ptr(ri);
1821 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1822 tcg_temp_free_ptr(tmpptr);
1823 } else {
1824 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1826 } else {
1827 if (ri->type & ARM_CP_CONST) {
1828 /* If not forbidden by access permissions, treat as WI */
1829 return;
1830 } else if (ri->writefn) {
1831 TCGv_ptr tmpptr;
1832 tmpptr = tcg_const_ptr(ri);
1833 gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1834 tcg_temp_free_ptr(tmpptr);
1835 } else {
1836 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
1840 if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1841 /* I/O operations must end the TB here (whether read or write) */
1842 gen_io_end();
1843 s->base.is_jmp = DISAS_UPDATE;
1844 } else if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
1845 /* We default to ending the TB on a coprocessor register write,
1846 * but allow this to be suppressed by the register definition
1847 * (usually only necessary to work around guest bugs).
1849 s->base.is_jmp = DISAS_UPDATE;
1853 /* System
1854 * 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0
1855 * +---------------------+---+-----+-----+-------+-------+-----+------+
1856 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 | CRn | CRm | op2 | Rt |
1857 * +---------------------+---+-----+-----+-------+-------+-----+------+
1859 static void disas_system(DisasContext *s, uint32_t insn)
1861 unsigned int l, op0, op1, crn, crm, op2, rt;
1862 l = extract32(insn, 21, 1);
1863 op0 = extract32(insn, 19, 2);
1864 op1 = extract32(insn, 16, 3);
1865 crn = extract32(insn, 12, 4);
1866 crm = extract32(insn, 8, 4);
1867 op2 = extract32(insn, 5, 3);
1868 rt = extract32(insn, 0, 5);
1870 if (op0 == 0) {
1871 if (l || rt != 31) {
1872 unallocated_encoding(s);
1873 return;
1875 switch (crn) {
1876 case 2: /* HINT (including allocated hints like NOP, YIELD, etc) */
1877 handle_hint(s, insn, op1, op2, crm);
1878 break;
1879 case 3: /* CLREX, DSB, DMB, ISB */
1880 handle_sync(s, insn, op1, op2, crm);
1881 break;
1882 case 4: /* MSR (immediate) */
1883 handle_msr_i(s, insn, op1, op2, crm);
1884 break;
1885 default:
1886 unallocated_encoding(s);
1887 break;
1889 return;
1891 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
1894 /* Exception generation
1896 * 31 24 23 21 20 5 4 2 1 0
1897 * +-----------------+-----+------------------------+-----+----+
1898 * | 1 1 0 1 0 1 0 0 | opc | imm16 | op2 | LL |
1899 * +-----------------------+------------------------+----------+
1901 static void disas_exc(DisasContext *s, uint32_t insn)
1903 int opc = extract32(insn, 21, 3);
1904 int op2_ll = extract32(insn, 0, 5);
1905 int imm16 = extract32(insn, 5, 16);
1906 TCGv_i32 tmp;
1908 switch (opc) {
1909 case 0:
1910 /* For SVC, HVC and SMC we advance the single-step state
1911 * machine before taking the exception. This is architecturally
1912 * mandated, to ensure that single-stepping a system call
1913 * instruction works properly.
1915 switch (op2_ll) {
1916 case 1: /* SVC */
1917 gen_ss_advance(s);
1918 gen_exception_insn(s, 0, EXCP_SWI, syn_aa64_svc(imm16),
1919 default_exception_el(s));
1920 break;
1921 case 2: /* HVC */
1922 if (s->current_el == 0) {
1923 unallocated_encoding(s);
1924 break;
1926 /* The pre HVC helper handles cases when HVC gets trapped
1927 * as an undefined insn by runtime configuration.
1929 gen_a64_set_pc_im(s->pc - 4);
1930 gen_helper_pre_hvc(cpu_env);
1931 gen_ss_advance(s);
1932 gen_exception_insn(s, 0, EXCP_HVC, syn_aa64_hvc(imm16), 2);
1933 break;
1934 case 3: /* SMC */
1935 if (s->current_el == 0) {
1936 unallocated_encoding(s);
1937 break;
1939 gen_a64_set_pc_im(s->pc - 4);
1940 tmp = tcg_const_i32(syn_aa64_smc(imm16));
1941 gen_helper_pre_smc(cpu_env, tmp);
1942 tcg_temp_free_i32(tmp);
1943 gen_ss_advance(s);
1944 gen_exception_insn(s, 0, EXCP_SMC, syn_aa64_smc(imm16), 3);
1945 break;
1946 default:
1947 unallocated_encoding(s);
1948 break;
1950 break;
1951 case 1:
1952 if (op2_ll != 0) {
1953 unallocated_encoding(s);
1954 break;
1956 /* BRK */
1957 gen_exception_bkpt_insn(s, 4, syn_aa64_bkpt(imm16));
1958 break;
1959 case 2:
1960 if (op2_ll != 0) {
1961 unallocated_encoding(s);
1962 break;
1964 /* HLT. This has two purposes.
1965 * Architecturally, it is an external halting debug instruction.
1966 * Since QEMU doesn't implement external debug, we treat this as
1967 * it is required for halting debug disabled: it will UNDEF.
1968 * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
1970 if (semihosting_enabled() && imm16 == 0xf000) {
1971 #ifndef CONFIG_USER_ONLY
1972 /* In system mode, don't allow userspace access to semihosting,
1973 * to provide some semblance of security (and for consistency
1974 * with our 32-bit semihosting).
1976 if (s->current_el == 0) {
1977 unsupported_encoding(s, insn);
1978 break;
1980 #endif
1981 gen_exception_internal_insn(s, 0, EXCP_SEMIHOST);
1982 } else {
1983 unsupported_encoding(s, insn);
1985 break;
1986 case 5:
1987 if (op2_ll < 1 || op2_ll > 3) {
1988 unallocated_encoding(s);
1989 break;
1991 /* DCPS1, DCPS2, DCPS3 */
1992 unsupported_encoding(s, insn);
1993 break;
1994 default:
1995 unallocated_encoding(s);
1996 break;
2000 /* Unconditional branch (register)
2001 * 31 25 24 21 20 16 15 10 9 5 4 0
2002 * +---------------+-------+-------+-------+------+-------+
2003 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
2004 * +---------------+-------+-------+-------+------+-------+
2006 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
2008 unsigned int opc, op2, op3, rn, op4;
2009 unsigned btype_mod = 2; /* 0: BR, 1: BLR, 2: other */
2010 TCGv_i64 dst;
2011 TCGv_i64 modifier;
2013 opc = extract32(insn, 21, 4);
2014 op2 = extract32(insn, 16, 5);
2015 op3 = extract32(insn, 10, 6);
2016 rn = extract32(insn, 5, 5);
2017 op4 = extract32(insn, 0, 5);
2019 if (op2 != 0x1f) {
2020 goto do_unallocated;
2023 switch (opc) {
2024 case 0: /* BR */
2025 case 1: /* BLR */
2026 case 2: /* RET */
2027 btype_mod = opc;
2028 switch (op3) {
2029 case 0:
2030 /* BR, BLR, RET */
2031 if (op4 != 0) {
2032 goto do_unallocated;
2034 dst = cpu_reg(s, rn);
2035 break;
2037 case 2:
2038 case 3:
2039 if (!dc_isar_feature(aa64_pauth, s)) {
2040 goto do_unallocated;
2042 if (opc == 2) {
2043 /* RETAA, RETAB */
2044 if (rn != 0x1f || op4 != 0x1f) {
2045 goto do_unallocated;
2047 rn = 30;
2048 modifier = cpu_X[31];
2049 } else {
2050 /* BRAAZ, BRABZ, BLRAAZ, BLRABZ */
2051 if (op4 != 0x1f) {
2052 goto do_unallocated;
2054 modifier = new_tmp_a64_zero(s);
2056 if (s->pauth_active) {
2057 dst = new_tmp_a64(s);
2058 if (op3 == 2) {
2059 gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
2060 } else {
2061 gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2063 } else {
2064 dst = cpu_reg(s, rn);
2066 break;
2068 default:
2069 goto do_unallocated;
2071 gen_a64_set_pc(s, dst);
2072 /* BLR also needs to load return address */
2073 if (opc == 1) {
2074 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
2076 break;
2078 case 8: /* BRAA */
2079 case 9: /* BLRAA */
2080 if (!dc_isar_feature(aa64_pauth, s)) {
2081 goto do_unallocated;
2083 if ((op3 & ~1) != 2) {
2084 goto do_unallocated;
2086 btype_mod = opc & 1;
2087 if (s->pauth_active) {
2088 dst = new_tmp_a64(s);
2089 modifier = cpu_reg_sp(s, op4);
2090 if (op3 == 2) {
2091 gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
2092 } else {
2093 gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2095 } else {
2096 dst = cpu_reg(s, rn);
2098 gen_a64_set_pc(s, dst);
2099 /* BLRAA also needs to load return address */
2100 if (opc == 9) {
2101 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
2103 break;
2105 case 4: /* ERET */
2106 if (s->current_el == 0) {
2107 goto do_unallocated;
2109 switch (op3) {
2110 case 0: /* ERET */
2111 if (op4 != 0) {
2112 goto do_unallocated;
2114 dst = tcg_temp_new_i64();
2115 tcg_gen_ld_i64(dst, cpu_env,
2116 offsetof(CPUARMState, elr_el[s->current_el]));
2117 break;
2119 case 2: /* ERETAA */
2120 case 3: /* ERETAB */
2121 if (!dc_isar_feature(aa64_pauth, s)) {
2122 goto do_unallocated;
2124 if (rn != 0x1f || op4 != 0x1f) {
2125 goto do_unallocated;
2127 dst = tcg_temp_new_i64();
2128 tcg_gen_ld_i64(dst, cpu_env,
2129 offsetof(CPUARMState, elr_el[s->current_el]));
2130 if (s->pauth_active) {
2131 modifier = cpu_X[31];
2132 if (op3 == 2) {
2133 gen_helper_autia(dst, cpu_env, dst, modifier);
2134 } else {
2135 gen_helper_autib(dst, cpu_env, dst, modifier);
2138 break;
2140 default:
2141 goto do_unallocated;
2143 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2144 gen_io_start();
2147 gen_helper_exception_return(cpu_env, dst);
2148 tcg_temp_free_i64(dst);
2149 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2150 gen_io_end();
2152 /* Must exit loop to check un-masked IRQs */
2153 s->base.is_jmp = DISAS_EXIT;
2154 return;
2156 case 5: /* DRPS */
2157 if (op3 != 0 || op4 != 0 || rn != 0x1f) {
2158 goto do_unallocated;
2159 } else {
2160 unsupported_encoding(s, insn);
2162 return;
2164 default:
2165 do_unallocated:
2166 unallocated_encoding(s);
2167 return;
2170 switch (btype_mod) {
2171 case 0: /* BR */
2172 if (dc_isar_feature(aa64_bti, s)) {
2173 /* BR to {x16,x17} or !guard -> 1, else 3. */
2174 set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
2176 break;
2178 case 1: /* BLR */
2179 if (dc_isar_feature(aa64_bti, s)) {
2180 /* BLR sets BTYPE to 2, regardless of source guarded page. */
2181 set_btype(s, 2);
2183 break;
2185 default: /* RET or none of the above. */
2186 /* BTYPE will be set to 0 by normal end-of-insn processing. */
2187 break;
2190 s->base.is_jmp = DISAS_JUMP;
2193 /* Branches, exception generating and system instructions */
2194 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
2196 switch (extract32(insn, 25, 7)) {
2197 case 0x0a: case 0x0b:
2198 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
2199 disas_uncond_b_imm(s, insn);
2200 break;
2201 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
2202 disas_comp_b_imm(s, insn);
2203 break;
2204 case 0x1b: case 0x5b: /* Test & branch (immediate) */
2205 disas_test_b_imm(s, insn);
2206 break;
2207 case 0x2a: /* Conditional branch (immediate) */
2208 disas_cond_b_imm(s, insn);
2209 break;
2210 case 0x6a: /* Exception generation / System */
2211 if (insn & (1 << 24)) {
2212 if (extract32(insn, 22, 2) == 0) {
2213 disas_system(s, insn);
2214 } else {
2215 unallocated_encoding(s);
2217 } else {
2218 disas_exc(s, insn);
2220 break;
2221 case 0x6b: /* Unconditional branch (register) */
2222 disas_uncond_b_reg(s, insn);
2223 break;
2224 default:
2225 unallocated_encoding(s);
2226 break;
2231 * Load/Store exclusive instructions are implemented by remembering
2232 * the value/address loaded, and seeing if these are the same
2233 * when the store is performed. This is not actually the architecturally
2234 * mandated semantics, but it works for typical guest code sequences
2235 * and avoids having to monitor regular stores.
2237 * The store exclusive uses the atomic cmpxchg primitives to avoid
2238 * races in multi-threaded linux-user and when MTTCG softmmu is
2239 * enabled.
2241 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
2242 TCGv_i64 addr, int size, bool is_pair)
2244 int idx = get_mem_index(s);
2245 TCGMemOp memop = s->be_data;
2247 g_assert(size <= 3);
2248 if (is_pair) {
2249 g_assert(size >= 2);
2250 if (size == 2) {
2251 /* The pair must be single-copy atomic for the doubleword. */
2252 memop |= MO_64 | MO_ALIGN;
2253 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2254 if (s->be_data == MO_LE) {
2255 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2256 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2257 } else {
2258 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2259 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2261 } else {
2262 /* The pair must be single-copy atomic for *each* doubleword, not
2263 the entire quadword, however it must be quadword aligned. */
2264 memop |= MO_64;
2265 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx,
2266 memop | MO_ALIGN_16);
2268 TCGv_i64 addr2 = tcg_temp_new_i64();
2269 tcg_gen_addi_i64(addr2, addr, 8);
2270 tcg_gen_qemu_ld_i64(cpu_exclusive_high, addr2, idx, memop);
2271 tcg_temp_free_i64(addr2);
2273 tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2274 tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2276 } else {
2277 memop |= size | MO_ALIGN;
2278 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2279 tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2281 tcg_gen_mov_i64(cpu_exclusive_addr, addr);
2284 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2285 TCGv_i64 addr, int size, int is_pair)
2287 /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2288 * && (!is_pair || env->exclusive_high == [addr + datasize])) {
2289 * [addr] = {Rt};
2290 * if (is_pair) {
2291 * [addr + datasize] = {Rt2};
2293 * {Rd} = 0;
2294 * } else {
2295 * {Rd} = 1;
2297 * env->exclusive_addr = -1;
2299 TCGLabel *fail_label = gen_new_label();
2300 TCGLabel *done_label = gen_new_label();
2301 TCGv_i64 tmp;
2303 tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
2305 tmp = tcg_temp_new_i64();
2306 if (is_pair) {
2307 if (size == 2) {
2308 if (s->be_data == MO_LE) {
2309 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2310 } else {
2311 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2313 tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2314 cpu_exclusive_val, tmp,
2315 get_mem_index(s),
2316 MO_64 | MO_ALIGN | s->be_data);
2317 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2318 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2319 if (!HAVE_CMPXCHG128) {
2320 gen_helper_exit_atomic(cpu_env);
2321 s->base.is_jmp = DISAS_NORETURN;
2322 } else if (s->be_data == MO_LE) {
2323 gen_helper_paired_cmpxchg64_le_parallel(tmp, cpu_env,
2324 cpu_exclusive_addr,
2325 cpu_reg(s, rt),
2326 cpu_reg(s, rt2));
2327 } else {
2328 gen_helper_paired_cmpxchg64_be_parallel(tmp, cpu_env,
2329 cpu_exclusive_addr,
2330 cpu_reg(s, rt),
2331 cpu_reg(s, rt2));
2333 } else if (s->be_data == MO_LE) {
2334 gen_helper_paired_cmpxchg64_le(tmp, cpu_env, cpu_exclusive_addr,
2335 cpu_reg(s, rt), cpu_reg(s, rt2));
2336 } else {
2337 gen_helper_paired_cmpxchg64_be(tmp, cpu_env, cpu_exclusive_addr,
2338 cpu_reg(s, rt), cpu_reg(s, rt2));
2340 } else {
2341 tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2342 cpu_reg(s, rt), get_mem_index(s),
2343 size | MO_ALIGN | s->be_data);
2344 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2346 tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2347 tcg_temp_free_i64(tmp);
2348 tcg_gen_br(done_label);
2350 gen_set_label(fail_label);
2351 tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2352 gen_set_label(done_label);
2353 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2356 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2357 int rn, int size)
2359 TCGv_i64 tcg_rs = cpu_reg(s, rs);
2360 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2361 int memidx = get_mem_index(s);
2362 TCGv_i64 clean_addr;
2364 if (rn == 31) {
2365 gen_check_sp_alignment(s);
2367 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2368 tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt, memidx,
2369 size | MO_ALIGN | s->be_data);
2372 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2373 int rn, int size)
2375 TCGv_i64 s1 = cpu_reg(s, rs);
2376 TCGv_i64 s2 = cpu_reg(s, rs + 1);
2377 TCGv_i64 t1 = cpu_reg(s, rt);
2378 TCGv_i64 t2 = cpu_reg(s, rt + 1);
2379 TCGv_i64 clean_addr;
2380 int memidx = get_mem_index(s);
2382 if (rn == 31) {
2383 gen_check_sp_alignment(s);
2385 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2387 if (size == 2) {
2388 TCGv_i64 cmp = tcg_temp_new_i64();
2389 TCGv_i64 val = tcg_temp_new_i64();
2391 if (s->be_data == MO_LE) {
2392 tcg_gen_concat32_i64(val, t1, t2);
2393 tcg_gen_concat32_i64(cmp, s1, s2);
2394 } else {
2395 tcg_gen_concat32_i64(val, t2, t1);
2396 tcg_gen_concat32_i64(cmp, s2, s1);
2399 tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx,
2400 MO_64 | MO_ALIGN | s->be_data);
2401 tcg_temp_free_i64(val);
2403 if (s->be_data == MO_LE) {
2404 tcg_gen_extr32_i64(s1, s2, cmp);
2405 } else {
2406 tcg_gen_extr32_i64(s2, s1, cmp);
2408 tcg_temp_free_i64(cmp);
2409 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2410 if (HAVE_CMPXCHG128) {
2411 TCGv_i32 tcg_rs = tcg_const_i32(rs);
2412 if (s->be_data == MO_LE) {
2413 gen_helper_casp_le_parallel(cpu_env, tcg_rs,
2414 clean_addr, t1, t2);
2415 } else {
2416 gen_helper_casp_be_parallel(cpu_env, tcg_rs,
2417 clean_addr, t1, t2);
2419 tcg_temp_free_i32(tcg_rs);
2420 } else {
2421 gen_helper_exit_atomic(cpu_env);
2422 s->base.is_jmp = DISAS_NORETURN;
2424 } else {
2425 TCGv_i64 d1 = tcg_temp_new_i64();
2426 TCGv_i64 d2 = tcg_temp_new_i64();
2427 TCGv_i64 a2 = tcg_temp_new_i64();
2428 TCGv_i64 c1 = tcg_temp_new_i64();
2429 TCGv_i64 c2 = tcg_temp_new_i64();
2430 TCGv_i64 zero = tcg_const_i64(0);
2432 /* Load the two words, in memory order. */
2433 tcg_gen_qemu_ld_i64(d1, clean_addr, memidx,
2434 MO_64 | MO_ALIGN_16 | s->be_data);
2435 tcg_gen_addi_i64(a2, clean_addr, 8);
2436 tcg_gen_qemu_ld_i64(d2, clean_addr, memidx, MO_64 | s->be_data);
2438 /* Compare the two words, also in memory order. */
2439 tcg_gen_setcond_i64(TCG_COND_EQ, c1, d1, s1);
2440 tcg_gen_setcond_i64(TCG_COND_EQ, c2, d2, s2);
2441 tcg_gen_and_i64(c2, c2, c1);
2443 /* If compare equal, write back new data, else write back old data. */
2444 tcg_gen_movcond_i64(TCG_COND_NE, c1, c2, zero, t1, d1);
2445 tcg_gen_movcond_i64(TCG_COND_NE, c2, c2, zero, t2, d2);
2446 tcg_gen_qemu_st_i64(c1, clean_addr, memidx, MO_64 | s->be_data);
2447 tcg_gen_qemu_st_i64(c2, a2, memidx, MO_64 | s->be_data);
2448 tcg_temp_free_i64(a2);
2449 tcg_temp_free_i64(c1);
2450 tcg_temp_free_i64(c2);
2451 tcg_temp_free_i64(zero);
2453 /* Write back the data from memory to Rs. */
2454 tcg_gen_mov_i64(s1, d1);
2455 tcg_gen_mov_i64(s2, d2);
2456 tcg_temp_free_i64(d1);
2457 tcg_temp_free_i64(d2);
2461 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
2462 * from the ARMv8 specs for LDR (Shared decode for all encodings).
2464 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2466 int opc0 = extract32(opc, 0, 1);
2467 int regsize;
2469 if (is_signed) {
2470 regsize = opc0 ? 32 : 64;
2471 } else {
2472 regsize = size == 3 ? 64 : 32;
2474 return regsize == 64;
2477 /* Load/store exclusive
2479 * 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0
2480 * +-----+-------------+----+---+----+------+----+-------+------+------+
2481 * | sz | 0 0 1 0 0 0 | o2 | L | o1 | Rs | o0 | Rt2 | Rn | Rt |
2482 * +-----+-------------+----+---+----+------+----+-------+------+------+
2484 * sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2485 * L: 0 -> store, 1 -> load
2486 * o2: 0 -> exclusive, 1 -> not
2487 * o1: 0 -> single register, 1 -> register pair
2488 * o0: 1 -> load-acquire/store-release, 0 -> not
2490 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2492 int rt = extract32(insn, 0, 5);
2493 int rn = extract32(insn, 5, 5);
2494 int rt2 = extract32(insn, 10, 5);
2495 int rs = extract32(insn, 16, 5);
2496 int is_lasr = extract32(insn, 15, 1);
2497 int o2_L_o1_o0 = extract32(insn, 21, 3) * 2 | is_lasr;
2498 int size = extract32(insn, 30, 2);
2499 TCGv_i64 clean_addr;
2501 switch (o2_L_o1_o0) {
2502 case 0x0: /* STXR */
2503 case 0x1: /* STLXR */
2504 if (rn == 31) {
2505 gen_check_sp_alignment(s);
2507 if (is_lasr) {
2508 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2510 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2511 gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, false);
2512 return;
2514 case 0x4: /* LDXR */
2515 case 0x5: /* LDAXR */
2516 if (rn == 31) {
2517 gen_check_sp_alignment(s);
2519 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2520 s->is_ldex = true;
2521 gen_load_exclusive(s, rt, rt2, clean_addr, size, false);
2522 if (is_lasr) {
2523 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2525 return;
2527 case 0x8: /* STLLR */
2528 if (!dc_isar_feature(aa64_lor, s)) {
2529 break;
2531 /* StoreLORelease is the same as Store-Release for QEMU. */
2532 /* fall through */
2533 case 0x9: /* STLR */
2534 /* Generate ISS for non-exclusive accesses including LASR. */
2535 if (rn == 31) {
2536 gen_check_sp_alignment(s);
2538 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2539 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2540 do_gpr_st(s, cpu_reg(s, rt), clean_addr, size, true, rt,
2541 disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2542 return;
2544 case 0xc: /* LDLAR */
2545 if (!dc_isar_feature(aa64_lor, s)) {
2546 break;
2548 /* LoadLOAcquire is the same as Load-Acquire for QEMU. */
2549 /* fall through */
2550 case 0xd: /* LDAR */
2551 /* Generate ISS for non-exclusive accesses including LASR. */
2552 if (rn == 31) {
2553 gen_check_sp_alignment(s);
2555 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2556 do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size, false, false, true, rt,
2557 disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2558 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2559 return;
2561 case 0x2: case 0x3: /* CASP / STXP */
2562 if (size & 2) { /* STXP / STLXP */
2563 if (rn == 31) {
2564 gen_check_sp_alignment(s);
2566 if (is_lasr) {
2567 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2569 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2570 gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, true);
2571 return;
2573 if (rt2 == 31
2574 && ((rt | rs) & 1) == 0
2575 && dc_isar_feature(aa64_atomics, s)) {
2576 /* CASP / CASPL */
2577 gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2578 return;
2580 break;
2582 case 0x6: case 0x7: /* CASPA / LDXP */
2583 if (size & 2) { /* LDXP / LDAXP */
2584 if (rn == 31) {
2585 gen_check_sp_alignment(s);
2587 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2588 s->is_ldex = true;
2589 gen_load_exclusive(s, rt, rt2, clean_addr, size, true);
2590 if (is_lasr) {
2591 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2593 return;
2595 if (rt2 == 31
2596 && ((rt | rs) & 1) == 0
2597 && dc_isar_feature(aa64_atomics, s)) {
2598 /* CASPA / CASPAL */
2599 gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2600 return;
2602 break;
2604 case 0xa: /* CAS */
2605 case 0xb: /* CASL */
2606 case 0xe: /* CASA */
2607 case 0xf: /* CASAL */
2608 if (rt2 == 31 && dc_isar_feature(aa64_atomics, s)) {
2609 gen_compare_and_swap(s, rs, rt, rn, size);
2610 return;
2612 break;
2614 unallocated_encoding(s);
2618 * Load register (literal)
2620 * 31 30 29 27 26 25 24 23 5 4 0
2621 * +-----+-------+---+-----+-------------------+-------+
2622 * | opc | 0 1 1 | V | 0 0 | imm19 | Rt |
2623 * +-----+-------+---+-----+-------------------+-------+
2625 * V: 1 -> vector (simd/fp)
2626 * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2627 * 10-> 32 bit signed, 11 -> prefetch
2628 * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2630 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2632 int rt = extract32(insn, 0, 5);
2633 int64_t imm = sextract32(insn, 5, 19) << 2;
2634 bool is_vector = extract32(insn, 26, 1);
2635 int opc = extract32(insn, 30, 2);
2636 bool is_signed = false;
2637 int size = 2;
2638 TCGv_i64 tcg_rt, clean_addr;
2640 if (is_vector) {
2641 if (opc == 3) {
2642 unallocated_encoding(s);
2643 return;
2645 size = 2 + opc;
2646 if (!fp_access_check(s)) {
2647 return;
2649 } else {
2650 if (opc == 3) {
2651 /* PRFM (literal) : prefetch */
2652 return;
2654 size = 2 + extract32(opc, 0, 1);
2655 is_signed = extract32(opc, 1, 1);
2658 tcg_rt = cpu_reg(s, rt);
2660 clean_addr = tcg_const_i64((s->pc - 4) + imm);
2661 if (is_vector) {
2662 do_fp_ld(s, rt, clean_addr, size);
2663 } else {
2664 /* Only unsigned 32bit loads target 32bit registers. */
2665 bool iss_sf = opc != 0;
2667 do_gpr_ld(s, tcg_rt, clean_addr, size, is_signed, false,
2668 true, rt, iss_sf, false);
2670 tcg_temp_free_i64(clean_addr);
2674 * LDNP (Load Pair - non-temporal hint)
2675 * LDP (Load Pair - non vector)
2676 * LDPSW (Load Pair Signed Word - non vector)
2677 * STNP (Store Pair - non-temporal hint)
2678 * STP (Store Pair - non vector)
2679 * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2680 * LDP (Load Pair of SIMD&FP)
2681 * STNP (Store Pair of SIMD&FP - non-temporal hint)
2682 * STP (Store Pair of SIMD&FP)
2684 * 31 30 29 27 26 25 24 23 22 21 15 14 10 9 5 4 0
2685 * +-----+-------+---+---+-------+---+-----------------------------+
2686 * | opc | 1 0 1 | V | 0 | index | L | imm7 | Rt2 | Rn | Rt |
2687 * +-----+-------+---+---+-------+---+-------+-------+------+------+
2689 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
2690 * LDPSW 01
2691 * LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2692 * V: 0 -> GPR, 1 -> Vector
2693 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2694 * 10 -> signed offset, 11 -> pre-index
2695 * L: 0 -> Store 1 -> Load
2697 * Rt, Rt2 = GPR or SIMD registers to be stored
2698 * Rn = general purpose register containing address
2699 * imm7 = signed offset (multiple of 4 or 8 depending on size)
2701 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2703 int rt = extract32(insn, 0, 5);
2704 int rn = extract32(insn, 5, 5);
2705 int rt2 = extract32(insn, 10, 5);
2706 uint64_t offset = sextract64(insn, 15, 7);
2707 int index = extract32(insn, 23, 2);
2708 bool is_vector = extract32(insn, 26, 1);
2709 bool is_load = extract32(insn, 22, 1);
2710 int opc = extract32(insn, 30, 2);
2712 bool is_signed = false;
2713 bool postindex = false;
2714 bool wback = false;
2716 TCGv_i64 clean_addr, dirty_addr;
2718 int size;
2720 if (opc == 3) {
2721 unallocated_encoding(s);
2722 return;
2725 if (is_vector) {
2726 size = 2 + opc;
2727 } else {
2728 size = 2 + extract32(opc, 1, 1);
2729 is_signed = extract32(opc, 0, 1);
2730 if (!is_load && is_signed) {
2731 unallocated_encoding(s);
2732 return;
2736 switch (index) {
2737 case 1: /* post-index */
2738 postindex = true;
2739 wback = true;
2740 break;
2741 case 0:
2742 /* signed offset with "non-temporal" hint. Since we don't emulate
2743 * caches we don't care about hints to the cache system about
2744 * data access patterns, and handle this identically to plain
2745 * signed offset.
2747 if (is_signed) {
2748 /* There is no non-temporal-hint version of LDPSW */
2749 unallocated_encoding(s);
2750 return;
2752 postindex = false;
2753 break;
2754 case 2: /* signed offset, rn not updated */
2755 postindex = false;
2756 break;
2757 case 3: /* pre-index */
2758 postindex = false;
2759 wback = true;
2760 break;
2763 if (is_vector && !fp_access_check(s)) {
2764 return;
2767 offset <<= size;
2769 if (rn == 31) {
2770 gen_check_sp_alignment(s);
2773 dirty_addr = read_cpu_reg_sp(s, rn, 1);
2774 if (!postindex) {
2775 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2777 clean_addr = clean_data_tbi(s, dirty_addr);
2779 if (is_vector) {
2780 if (is_load) {
2781 do_fp_ld(s, rt, clean_addr, size);
2782 } else {
2783 do_fp_st(s, rt, clean_addr, size);
2785 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2786 if (is_load) {
2787 do_fp_ld(s, rt2, clean_addr, size);
2788 } else {
2789 do_fp_st(s, rt2, clean_addr, size);
2791 } else {
2792 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2793 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
2795 if (is_load) {
2796 TCGv_i64 tmp = tcg_temp_new_i64();
2798 /* Do not modify tcg_rt before recognizing any exception
2799 * from the second load.
2801 do_gpr_ld(s, tmp, clean_addr, size, is_signed, false,
2802 false, 0, false, false);
2803 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2804 do_gpr_ld(s, tcg_rt2, clean_addr, size, is_signed, false,
2805 false, 0, false, false);
2807 tcg_gen_mov_i64(tcg_rt, tmp);
2808 tcg_temp_free_i64(tmp);
2809 } else {
2810 do_gpr_st(s, tcg_rt, clean_addr, size,
2811 false, 0, false, false);
2812 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2813 do_gpr_st(s, tcg_rt2, clean_addr, size,
2814 false, 0, false, false);
2818 if (wback) {
2819 if (postindex) {
2820 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2822 tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
2827 * Load/store (immediate post-indexed)
2828 * Load/store (immediate pre-indexed)
2829 * Load/store (unscaled immediate)
2831 * 31 30 29 27 26 25 24 23 22 21 20 12 11 10 9 5 4 0
2832 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2833 * |size| 1 1 1 | V | 0 0 | opc | 0 | imm9 | idx | Rn | Rt |
2834 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2836 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
2837 10 -> unprivileged
2838 * V = 0 -> non-vector
2839 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
2840 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2842 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
2843 int opc,
2844 int size,
2845 int rt,
2846 bool is_vector)
2848 int rn = extract32(insn, 5, 5);
2849 int imm9 = sextract32(insn, 12, 9);
2850 int idx = extract32(insn, 10, 2);
2851 bool is_signed = false;
2852 bool is_store = false;
2853 bool is_extended = false;
2854 bool is_unpriv = (idx == 2);
2855 bool iss_valid = !is_vector;
2856 bool post_index;
2857 bool writeback;
2859 TCGv_i64 clean_addr, dirty_addr;
2861 if (is_vector) {
2862 size |= (opc & 2) << 1;
2863 if (size > 4 || is_unpriv) {
2864 unallocated_encoding(s);
2865 return;
2867 is_store = ((opc & 1) == 0);
2868 if (!fp_access_check(s)) {
2869 return;
2871 } else {
2872 if (size == 3 && opc == 2) {
2873 /* PRFM - prefetch */
2874 if (idx != 0) {
2875 unallocated_encoding(s);
2876 return;
2878 return;
2880 if (opc == 3 && size > 1) {
2881 unallocated_encoding(s);
2882 return;
2884 is_store = (opc == 0);
2885 is_signed = extract32(opc, 1, 1);
2886 is_extended = (size < 3) && extract32(opc, 0, 1);
2889 switch (idx) {
2890 case 0:
2891 case 2:
2892 post_index = false;
2893 writeback = false;
2894 break;
2895 case 1:
2896 post_index = true;
2897 writeback = true;
2898 break;
2899 case 3:
2900 post_index = false;
2901 writeback = true;
2902 break;
2903 default:
2904 g_assert_not_reached();
2907 if (rn == 31) {
2908 gen_check_sp_alignment(s);
2911 dirty_addr = read_cpu_reg_sp(s, rn, 1);
2912 if (!post_index) {
2913 tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
2915 clean_addr = clean_data_tbi(s, dirty_addr);
2917 if (is_vector) {
2918 if (is_store) {
2919 do_fp_st(s, rt, clean_addr, size);
2920 } else {
2921 do_fp_ld(s, rt, clean_addr, size);
2923 } else {
2924 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2925 int memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
2926 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2928 if (is_store) {
2929 do_gpr_st_memidx(s, tcg_rt, clean_addr, size, memidx,
2930 iss_valid, rt, iss_sf, false);
2931 } else {
2932 do_gpr_ld_memidx(s, tcg_rt, clean_addr, size,
2933 is_signed, is_extended, memidx,
2934 iss_valid, rt, iss_sf, false);
2938 if (writeback) {
2939 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2940 if (post_index) {
2941 tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
2943 tcg_gen_mov_i64(tcg_rn, dirty_addr);
2948 * Load/store (register offset)
2950 * 31 30 29 27 26 25 24 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2951 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2952 * |size| 1 1 1 | V | 0 0 | opc | 1 | Rm | opt | S| 1 0 | Rn | Rt |
2953 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2955 * For non-vector:
2956 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2957 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2958 * For vector:
2959 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2960 * opc<0>: 0 -> store, 1 -> load
2961 * V: 1 -> vector/simd
2962 * opt: extend encoding (see DecodeRegExtend)
2963 * S: if S=1 then scale (essentially index by sizeof(size))
2964 * Rt: register to transfer into/out of
2965 * Rn: address register or SP for base
2966 * Rm: offset register or ZR for offset
2968 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
2969 int opc,
2970 int size,
2971 int rt,
2972 bool is_vector)
2974 int rn = extract32(insn, 5, 5);
2975 int shift = extract32(insn, 12, 1);
2976 int rm = extract32(insn, 16, 5);
2977 int opt = extract32(insn, 13, 3);
2978 bool is_signed = false;
2979 bool is_store = false;
2980 bool is_extended = false;
2982 TCGv_i64 tcg_rm, clean_addr, dirty_addr;
2984 if (extract32(opt, 1, 1) == 0) {
2985 unallocated_encoding(s);
2986 return;
2989 if (is_vector) {
2990 size |= (opc & 2) << 1;
2991 if (size > 4) {
2992 unallocated_encoding(s);
2993 return;
2995 is_store = !extract32(opc, 0, 1);
2996 if (!fp_access_check(s)) {
2997 return;
2999 } else {
3000 if (size == 3 && opc == 2) {
3001 /* PRFM - prefetch */
3002 return;
3004 if (opc == 3 && size > 1) {
3005 unallocated_encoding(s);
3006 return;
3008 is_store = (opc == 0);
3009 is_signed = extract32(opc, 1, 1);
3010 is_extended = (size < 3) && extract32(opc, 0, 1);
3013 if (rn == 31) {
3014 gen_check_sp_alignment(s);
3016 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3018 tcg_rm = read_cpu_reg(s, rm, 1);
3019 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
3021 tcg_gen_add_i64(dirty_addr, dirty_addr, tcg_rm);
3022 clean_addr = clean_data_tbi(s, dirty_addr);
3024 if (is_vector) {
3025 if (is_store) {
3026 do_fp_st(s, rt, clean_addr, size);
3027 } else {
3028 do_fp_ld(s, rt, clean_addr, size);
3030 } else {
3031 TCGv_i64 tcg_rt = cpu_reg(s, rt);
3032 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3033 if (is_store) {
3034 do_gpr_st(s, tcg_rt, clean_addr, size,
3035 true, rt, iss_sf, false);
3036 } else {
3037 do_gpr_ld(s, tcg_rt, clean_addr, size,
3038 is_signed, is_extended,
3039 true, rt, iss_sf, false);
3045 * Load/store (unsigned immediate)
3047 * 31 30 29 27 26 25 24 23 22 21 10 9 5
3048 * +----+-------+---+-----+-----+------------+-------+------+
3049 * |size| 1 1 1 | V | 0 1 | opc | imm12 | Rn | Rt |
3050 * +----+-------+---+-----+-----+------------+-------+------+
3052 * For non-vector:
3053 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3054 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3055 * For vector:
3056 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3057 * opc<0>: 0 -> store, 1 -> load
3058 * Rn: base address register (inc SP)
3059 * Rt: target register
3061 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
3062 int opc,
3063 int size,
3064 int rt,
3065 bool is_vector)
3067 int rn = extract32(insn, 5, 5);
3068 unsigned int imm12 = extract32(insn, 10, 12);
3069 unsigned int offset;
3071 TCGv_i64 clean_addr, dirty_addr;
3073 bool is_store;
3074 bool is_signed = false;
3075 bool is_extended = false;
3077 if (is_vector) {
3078 size |= (opc & 2) << 1;
3079 if (size > 4) {
3080 unallocated_encoding(s);
3081 return;
3083 is_store = !extract32(opc, 0, 1);
3084 if (!fp_access_check(s)) {
3085 return;
3087 } else {
3088 if (size == 3 && opc == 2) {
3089 /* PRFM - prefetch */
3090 return;
3092 if (opc == 3 && size > 1) {
3093 unallocated_encoding(s);
3094 return;
3096 is_store = (opc == 0);
3097 is_signed = extract32(opc, 1, 1);
3098 is_extended = (size < 3) && extract32(opc, 0, 1);
3101 if (rn == 31) {
3102 gen_check_sp_alignment(s);
3104 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3105 offset = imm12 << size;
3106 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3107 clean_addr = clean_data_tbi(s, dirty_addr);
3109 if (is_vector) {
3110 if (is_store) {
3111 do_fp_st(s, rt, clean_addr, size);
3112 } else {
3113 do_fp_ld(s, rt, clean_addr, size);
3115 } else {
3116 TCGv_i64 tcg_rt = cpu_reg(s, rt);
3117 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3118 if (is_store) {
3119 do_gpr_st(s, tcg_rt, clean_addr, size,
3120 true, rt, iss_sf, false);
3121 } else {
3122 do_gpr_ld(s, tcg_rt, clean_addr, size, is_signed, is_extended,
3123 true, rt, iss_sf, false);
3128 /* Atomic memory operations
3130 * 31 30 27 26 24 22 21 16 15 12 10 5 0
3131 * +------+-------+---+-----+-----+---+----+----+-----+-----+----+-----+
3132 * | size | 1 1 1 | V | 0 0 | A R | 1 | Rs | o3 | opc | 0 0 | Rn | Rt |
3133 * +------+-------+---+-----+-----+--------+----+-----+-----+----+-----+
3135 * Rt: the result register
3136 * Rn: base address or SP
3137 * Rs: the source register for the operation
3138 * V: vector flag (always 0 as of v8.3)
3139 * A: acquire flag
3140 * R: release flag
3142 static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
3143 int size, int rt, bool is_vector)
3145 int rs = extract32(insn, 16, 5);
3146 int rn = extract32(insn, 5, 5);
3147 int o3_opc = extract32(insn, 12, 4);
3148 TCGv_i64 tcg_rs, clean_addr;
3149 AtomicThreeOpFn *fn;
3151 if (is_vector || !dc_isar_feature(aa64_atomics, s)) {
3152 unallocated_encoding(s);
3153 return;
3155 switch (o3_opc) {
3156 case 000: /* LDADD */
3157 fn = tcg_gen_atomic_fetch_add_i64;
3158 break;
3159 case 001: /* LDCLR */
3160 fn = tcg_gen_atomic_fetch_and_i64;
3161 break;
3162 case 002: /* LDEOR */
3163 fn = tcg_gen_atomic_fetch_xor_i64;
3164 break;
3165 case 003: /* LDSET */
3166 fn = tcg_gen_atomic_fetch_or_i64;
3167 break;
3168 case 004: /* LDSMAX */
3169 fn = tcg_gen_atomic_fetch_smax_i64;
3170 break;
3171 case 005: /* LDSMIN */
3172 fn = tcg_gen_atomic_fetch_smin_i64;
3173 break;
3174 case 006: /* LDUMAX */
3175 fn = tcg_gen_atomic_fetch_umax_i64;
3176 break;
3177 case 007: /* LDUMIN */
3178 fn = tcg_gen_atomic_fetch_umin_i64;
3179 break;
3180 case 010: /* SWP */
3181 fn = tcg_gen_atomic_xchg_i64;
3182 break;
3183 default:
3184 unallocated_encoding(s);
3185 return;
3188 if (rn == 31) {
3189 gen_check_sp_alignment(s);
3191 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
3192 tcg_rs = read_cpu_reg(s, rs, true);
3194 if (o3_opc == 1) { /* LDCLR */
3195 tcg_gen_not_i64(tcg_rs, tcg_rs);
3198 /* The tcg atomic primitives are all full barriers. Therefore we
3199 * can ignore the Acquire and Release bits of this instruction.
3201 fn(cpu_reg(s, rt), clean_addr, tcg_rs, get_mem_index(s),
3202 s->be_data | size | MO_ALIGN);
3206 * PAC memory operations
3208 * 31 30 27 26 24 22 21 12 11 10 5 0
3209 * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3210 * | size | 1 1 1 | V | 0 0 | M S | 1 | imm9 | W | 1 | Rn | Rt |
3211 * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3213 * Rt: the result register
3214 * Rn: base address or SP
3215 * V: vector flag (always 0 as of v8.3)
3216 * M: clear for key DA, set for key DB
3217 * W: pre-indexing flag
3218 * S: sign for imm9.
3220 static void disas_ldst_pac(DisasContext *s, uint32_t insn,
3221 int size, int rt, bool is_vector)
3223 int rn = extract32(insn, 5, 5);
3224 bool is_wback = extract32(insn, 11, 1);
3225 bool use_key_a = !extract32(insn, 23, 1);
3226 int offset;
3227 TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3229 if (size != 3 || is_vector || !dc_isar_feature(aa64_pauth, s)) {
3230 unallocated_encoding(s);
3231 return;
3234 if (rn == 31) {
3235 gen_check_sp_alignment(s);
3237 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3239 if (s->pauth_active) {
3240 if (use_key_a) {
3241 gen_helper_autda(dirty_addr, cpu_env, dirty_addr, cpu_X[31]);
3242 } else {
3243 gen_helper_autdb(dirty_addr, cpu_env, dirty_addr, cpu_X[31]);
3247 /* Form the 10-bit signed, scaled offset. */
3248 offset = (extract32(insn, 22, 1) << 9) | extract32(insn, 12, 9);
3249 offset = sextract32(offset << size, 0, 10 + size);
3250 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3252 /* Note that "clean" and "dirty" here refer to TBI not PAC. */
3253 clean_addr = clean_data_tbi(s, dirty_addr);
3255 tcg_rt = cpu_reg(s, rt);
3256 do_gpr_ld(s, tcg_rt, clean_addr, size, /* is_signed */ false,
3257 /* extend */ false, /* iss_valid */ !is_wback,
3258 /* iss_srt */ rt, /* iss_sf */ true, /* iss_ar */ false);
3260 if (is_wback) {
3261 tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3265 /* Load/store register (all forms) */
3266 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
3268 int rt = extract32(insn, 0, 5);
3269 int opc = extract32(insn, 22, 2);
3270 bool is_vector = extract32(insn, 26, 1);
3271 int size = extract32(insn, 30, 2);
3273 switch (extract32(insn, 24, 2)) {
3274 case 0:
3275 if (extract32(insn, 21, 1) == 0) {
3276 /* Load/store register (unscaled immediate)
3277 * Load/store immediate pre/post-indexed
3278 * Load/store register unprivileged
3280 disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
3281 return;
3283 switch (extract32(insn, 10, 2)) {
3284 case 0:
3285 disas_ldst_atomic(s, insn, size, rt, is_vector);
3286 return;
3287 case 2:
3288 disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
3289 return;
3290 default:
3291 disas_ldst_pac(s, insn, size, rt, is_vector);
3292 return;
3294 break;
3295 case 1:
3296 disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
3297 return;
3299 unallocated_encoding(s);
3302 /* AdvSIMD load/store multiple structures
3304 * 31 30 29 23 22 21 16 15 12 11 10 9 5 4 0
3305 * +---+---+---------------+---+-------------+--------+------+------+------+
3306 * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size | Rn | Rt |
3307 * +---+---+---------------+---+-------------+--------+------+------+------+
3309 * AdvSIMD load/store multiple structures (post-indexed)
3311 * 31 30 29 23 22 21 20 16 15 12 11 10 9 5 4 0
3312 * +---+---+---------------+---+---+---------+--------+------+------+------+
3313 * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 | Rm | opcode | size | Rn | Rt |
3314 * +---+---+---------------+---+---+---------+--------+------+------+------+
3316 * Rt: first (or only) SIMD&FP register to be transferred
3317 * Rn: base address or SP
3318 * Rm (post-index only): post-index register (when !31) or size dependent #imm
3320 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
3322 int rt = extract32(insn, 0, 5);
3323 int rn = extract32(insn, 5, 5);
3324 int rm = extract32(insn, 16, 5);
3325 int size = extract32(insn, 10, 2);
3326 int opcode = extract32(insn, 12, 4);
3327 bool is_store = !extract32(insn, 22, 1);
3328 bool is_postidx = extract32(insn, 23, 1);
3329 bool is_q = extract32(insn, 30, 1);
3330 TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3331 TCGMemOp endian = s->be_data;
3333 int ebytes; /* bytes per element */
3334 int elements; /* elements per vector */
3335 int rpt; /* num iterations */
3336 int selem; /* structure elements */
3337 int r;
3339 if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
3340 unallocated_encoding(s);
3341 return;
3344 if (!is_postidx && rm != 0) {
3345 unallocated_encoding(s);
3346 return;
3349 /* From the shared decode logic */
3350 switch (opcode) {
3351 case 0x0:
3352 rpt = 1;
3353 selem = 4;
3354 break;
3355 case 0x2:
3356 rpt = 4;
3357 selem = 1;
3358 break;
3359 case 0x4:
3360 rpt = 1;
3361 selem = 3;
3362 break;
3363 case 0x6:
3364 rpt = 3;
3365 selem = 1;
3366 break;
3367 case 0x7:
3368 rpt = 1;
3369 selem = 1;
3370 break;
3371 case 0x8:
3372 rpt = 1;
3373 selem = 2;
3374 break;
3375 case 0xa:
3376 rpt = 2;
3377 selem = 1;
3378 break;
3379 default:
3380 unallocated_encoding(s);
3381 return;
3384 if (size == 3 && !is_q && selem != 1) {
3385 /* reserved */
3386 unallocated_encoding(s);
3387 return;
3390 if (!fp_access_check(s)) {
3391 return;
3394 if (rn == 31) {
3395 gen_check_sp_alignment(s);
3398 /* For our purposes, bytes are always little-endian. */
3399 if (size == 0) {
3400 endian = MO_LE;
3403 /* Consecutive little-endian elements from a single register
3404 * can be promoted to a larger little-endian operation.
3406 if (selem == 1 && endian == MO_LE) {
3407 size = 3;
3409 ebytes = 1 << size;
3410 elements = (is_q ? 16 : 8) / ebytes;
3412 tcg_rn = cpu_reg_sp(s, rn);
3413 clean_addr = clean_data_tbi(s, tcg_rn);
3414 tcg_ebytes = tcg_const_i64(ebytes);
3416 for (r = 0; r < rpt; r++) {
3417 int e;
3418 for (e = 0; e < elements; e++) {
3419 int xs;
3420 for (xs = 0; xs < selem; xs++) {
3421 int tt = (rt + r + xs) % 32;
3422 if (is_store) {
3423 do_vec_st(s, tt, e, clean_addr, size, endian);
3424 } else {
3425 do_vec_ld(s, tt, e, clean_addr, size, endian);
3427 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3431 tcg_temp_free_i64(tcg_ebytes);
3433 if (!is_store) {
3434 /* For non-quad operations, setting a slice of the low
3435 * 64 bits of the register clears the high 64 bits (in
3436 * the ARM ARM pseudocode this is implicit in the fact
3437 * that 'rval' is a 64 bit wide variable).
3438 * For quad operations, we might still need to zero the
3439 * high bits of SVE.
3441 for (r = 0; r < rpt * selem; r++) {
3442 int tt = (rt + r) % 32;
3443 clear_vec_high(s, is_q, tt);
3447 if (is_postidx) {
3448 if (rm == 31) {
3449 tcg_gen_addi_i64(tcg_rn, tcg_rn, rpt * elements * selem * ebytes);
3450 } else {
3451 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3456 /* AdvSIMD load/store single structure
3458 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3459 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3460 * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size | Rn | Rt |
3461 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3463 * AdvSIMD load/store single structure (post-indexed)
3465 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3466 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3467 * | 0 | Q | 0 0 1 1 0 1 1 | L R | Rm | opc | S | size | Rn | Rt |
3468 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3470 * Rt: first (or only) SIMD&FP register to be transferred
3471 * Rn: base address or SP
3472 * Rm (post-index only): post-index register (when !31) or size dependent #imm
3473 * index = encoded in Q:S:size dependent on size
3475 * lane_size = encoded in R, opc
3476 * transfer width = encoded in opc, S, size
3478 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
3480 int rt = extract32(insn, 0, 5);
3481 int rn = extract32(insn, 5, 5);
3482 int rm = extract32(insn, 16, 5);
3483 int size = extract32(insn, 10, 2);
3484 int S = extract32(insn, 12, 1);
3485 int opc = extract32(insn, 13, 3);
3486 int R = extract32(insn, 21, 1);
3487 int is_load = extract32(insn, 22, 1);
3488 int is_postidx = extract32(insn, 23, 1);
3489 int is_q = extract32(insn, 30, 1);
3491 int scale = extract32(opc, 1, 2);
3492 int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
3493 bool replicate = false;
3494 int index = is_q << 3 | S << 2 | size;
3495 int ebytes, xs;
3496 TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3498 if (extract32(insn, 31, 1)) {
3499 unallocated_encoding(s);
3500 return;
3502 if (!is_postidx && rm != 0) {
3503 unallocated_encoding(s);
3504 return;
3507 switch (scale) {
3508 case 3:
3509 if (!is_load || S) {
3510 unallocated_encoding(s);
3511 return;
3513 scale = size;
3514 replicate = true;
3515 break;
3516 case 0:
3517 break;
3518 case 1:
3519 if (extract32(size, 0, 1)) {
3520 unallocated_encoding(s);
3521 return;
3523 index >>= 1;
3524 break;
3525 case 2:
3526 if (extract32(size, 1, 1)) {
3527 unallocated_encoding(s);
3528 return;
3530 if (!extract32(size, 0, 1)) {
3531 index >>= 2;
3532 } else {
3533 if (S) {
3534 unallocated_encoding(s);
3535 return;
3537 index >>= 3;
3538 scale = 3;
3540 break;
3541 default:
3542 g_assert_not_reached();
3545 if (!fp_access_check(s)) {
3546 return;
3549 ebytes = 1 << scale;
3551 if (rn == 31) {
3552 gen_check_sp_alignment(s);
3555 tcg_rn = cpu_reg_sp(s, rn);
3556 clean_addr = clean_data_tbi(s, tcg_rn);
3557 tcg_ebytes = tcg_const_i64(ebytes);
3559 for (xs = 0; xs < selem; xs++) {
3560 if (replicate) {
3561 /* Load and replicate to all elements */
3562 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3564 tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr,
3565 get_mem_index(s), s->be_data + scale);
3566 tcg_gen_gvec_dup_i64(scale, vec_full_reg_offset(s, rt),
3567 (is_q + 1) * 8, vec_full_reg_size(s),
3568 tcg_tmp);
3569 tcg_temp_free_i64(tcg_tmp);
3570 } else {
3571 /* Load/store one element per register */
3572 if (is_load) {
3573 do_vec_ld(s, rt, index, clean_addr, scale, s->be_data);
3574 } else {
3575 do_vec_st(s, rt, index, clean_addr, scale, s->be_data);
3578 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3579 rt = (rt + 1) % 32;
3581 tcg_temp_free_i64(tcg_ebytes);
3583 if (is_postidx) {
3584 if (rm == 31) {
3585 tcg_gen_addi_i64(tcg_rn, tcg_rn, selem * ebytes);
3586 } else {
3587 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3592 /* Loads and stores */
3593 static void disas_ldst(DisasContext *s, uint32_t insn)
3595 switch (extract32(insn, 24, 6)) {
3596 case 0x08: /* Load/store exclusive */
3597 disas_ldst_excl(s, insn);
3598 break;
3599 case 0x18: case 0x1c: /* Load register (literal) */
3600 disas_ld_lit(s, insn);
3601 break;
3602 case 0x28: case 0x29:
3603 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
3604 disas_ldst_pair(s, insn);
3605 break;
3606 case 0x38: case 0x39:
3607 case 0x3c: case 0x3d: /* Load/store register (all forms) */
3608 disas_ldst_reg(s, insn);
3609 break;
3610 case 0x0c: /* AdvSIMD load/store multiple structures */
3611 disas_ldst_multiple_struct(s, insn);
3612 break;
3613 case 0x0d: /* AdvSIMD load/store single structure */
3614 disas_ldst_single_struct(s, insn);
3615 break;
3616 default:
3617 unallocated_encoding(s);
3618 break;
3622 /* PC-rel. addressing
3623 * 31 30 29 28 24 23 5 4 0
3624 * +----+-------+-----------+-------------------+------+
3625 * | op | immlo | 1 0 0 0 0 | immhi | Rd |
3626 * +----+-------+-----------+-------------------+------+
3628 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
3630 unsigned int page, rd;
3631 uint64_t base;
3632 uint64_t offset;
3634 page = extract32(insn, 31, 1);
3635 /* SignExtend(immhi:immlo) -> offset */
3636 offset = sextract64(insn, 5, 19);
3637 offset = offset << 2 | extract32(insn, 29, 2);
3638 rd = extract32(insn, 0, 5);
3639 base = s->pc - 4;
3641 if (page) {
3642 /* ADRP (page based) */
3643 base &= ~0xfff;
3644 offset <<= 12;
3647 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
3651 * Add/subtract (immediate)
3653 * 31 30 29 28 24 23 22 21 10 9 5 4 0
3654 * +--+--+--+-----------+-----+-------------+-----+-----+
3655 * |sf|op| S| 1 0 0 0 1 |shift| imm12 | Rn | Rd |
3656 * +--+--+--+-----------+-----+-------------+-----+-----+
3658 * sf: 0 -> 32bit, 1 -> 64bit
3659 * op: 0 -> add , 1 -> sub
3660 * S: 1 -> set flags
3661 * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
3663 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
3665 int rd = extract32(insn, 0, 5);
3666 int rn = extract32(insn, 5, 5);
3667 uint64_t imm = extract32(insn, 10, 12);
3668 int shift = extract32(insn, 22, 2);
3669 bool setflags = extract32(insn, 29, 1);
3670 bool sub_op = extract32(insn, 30, 1);
3671 bool is_64bit = extract32(insn, 31, 1);
3673 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3674 TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
3675 TCGv_i64 tcg_result;
3677 switch (shift) {
3678 case 0x0:
3679 break;
3680 case 0x1:
3681 imm <<= 12;
3682 break;
3683 default:
3684 unallocated_encoding(s);
3685 return;
3688 tcg_result = tcg_temp_new_i64();
3689 if (!setflags) {
3690 if (sub_op) {
3691 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
3692 } else {
3693 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
3695 } else {
3696 TCGv_i64 tcg_imm = tcg_const_i64(imm);
3697 if (sub_op) {
3698 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
3699 } else {
3700 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
3702 tcg_temp_free_i64(tcg_imm);
3705 if (is_64bit) {
3706 tcg_gen_mov_i64(tcg_rd, tcg_result);
3707 } else {
3708 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3711 tcg_temp_free_i64(tcg_result);
3714 /* The input should be a value in the bottom e bits (with higher
3715 * bits zero); returns that value replicated into every element
3716 * of size e in a 64 bit integer.
3718 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
3720 assert(e != 0);
3721 while (e < 64) {
3722 mask |= mask << e;
3723 e *= 2;
3725 return mask;
3728 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
3729 static inline uint64_t bitmask64(unsigned int length)
3731 assert(length > 0 && length <= 64);
3732 return ~0ULL >> (64 - length);
3735 /* Simplified variant of pseudocode DecodeBitMasks() for the case where we
3736 * only require the wmask. Returns false if the imms/immr/immn are a reserved
3737 * value (ie should cause a guest UNDEF exception), and true if they are
3738 * valid, in which case the decoded bit pattern is written to result.
3740 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
3741 unsigned int imms, unsigned int immr)
3743 uint64_t mask;
3744 unsigned e, levels, s, r;
3745 int len;
3747 assert(immn < 2 && imms < 64 && immr < 64);
3749 /* The bit patterns we create here are 64 bit patterns which
3750 * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
3751 * 64 bits each. Each element contains the same value: a run
3752 * of between 1 and e-1 non-zero bits, rotated within the
3753 * element by between 0 and e-1 bits.
3755 * The element size and run length are encoded into immn (1 bit)
3756 * and imms (6 bits) as follows:
3757 * 64 bit elements: immn = 1, imms = <length of run - 1>
3758 * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
3759 * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
3760 * 8 bit elements: immn = 0, imms = 110 : <length of run - 1>
3761 * 4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
3762 * 2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
3763 * Notice that immn = 0, imms = 11111x is the only combination
3764 * not covered by one of the above options; this is reserved.
3765 * Further, <length of run - 1> all-ones is a reserved pattern.
3767 * In all cases the rotation is by immr % e (and immr is 6 bits).
3770 /* First determine the element size */
3771 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
3772 if (len < 1) {
3773 /* This is the immn == 0, imms == 0x11111x case */
3774 return false;
3776 e = 1 << len;
3778 levels = e - 1;
3779 s = imms & levels;
3780 r = immr & levels;
3782 if (s == levels) {
3783 /* <length of run - 1> mustn't be all-ones. */
3784 return false;
3787 /* Create the value of one element: s+1 set bits rotated
3788 * by r within the element (which is e bits wide)...
3790 mask = bitmask64(s + 1);
3791 if (r) {
3792 mask = (mask >> r) | (mask << (e - r));
3793 mask &= bitmask64(e);
3795 /* ...then replicate the element over the whole 64 bit value */
3796 mask = bitfield_replicate(mask, e);
3797 *result = mask;
3798 return true;
3801 /* Logical (immediate)
3802 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
3803 * +----+-----+-------------+---+------+------+------+------+
3804 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms | Rn | Rd |
3805 * +----+-----+-------------+---+------+------+------+------+
3807 static void disas_logic_imm(DisasContext *s, uint32_t insn)
3809 unsigned int sf, opc, is_n, immr, imms, rn, rd;
3810 TCGv_i64 tcg_rd, tcg_rn;
3811 uint64_t wmask;
3812 bool is_and = false;
3814 sf = extract32(insn, 31, 1);
3815 opc = extract32(insn, 29, 2);
3816 is_n = extract32(insn, 22, 1);
3817 immr = extract32(insn, 16, 6);
3818 imms = extract32(insn, 10, 6);
3819 rn = extract32(insn, 5, 5);
3820 rd = extract32(insn, 0, 5);
3822 if (!sf && is_n) {
3823 unallocated_encoding(s);
3824 return;
3827 if (opc == 0x3) { /* ANDS */
3828 tcg_rd = cpu_reg(s, rd);
3829 } else {
3830 tcg_rd = cpu_reg_sp(s, rd);
3832 tcg_rn = cpu_reg(s, rn);
3834 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
3835 /* some immediate field values are reserved */
3836 unallocated_encoding(s);
3837 return;
3840 if (!sf) {
3841 wmask &= 0xffffffff;
3844 switch (opc) {
3845 case 0x3: /* ANDS */
3846 case 0x0: /* AND */
3847 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
3848 is_and = true;
3849 break;
3850 case 0x1: /* ORR */
3851 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
3852 break;
3853 case 0x2: /* EOR */
3854 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
3855 break;
3856 default:
3857 assert(FALSE); /* must handle all above */
3858 break;
3861 if (!sf && !is_and) {
3862 /* zero extend final result; we know we can skip this for AND
3863 * since the immediate had the high 32 bits clear.
3865 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3868 if (opc == 3) { /* ANDS */
3869 gen_logic_CC(sf, tcg_rd);
3874 * Move wide (immediate)
3876 * 31 30 29 28 23 22 21 20 5 4 0
3877 * +--+-----+-------------+-----+----------------+------+
3878 * |sf| opc | 1 0 0 1 0 1 | hw | imm16 | Rd |
3879 * +--+-----+-------------+-----+----------------+------+
3881 * sf: 0 -> 32 bit, 1 -> 64 bit
3882 * opc: 00 -> N, 10 -> Z, 11 -> K
3883 * hw: shift/16 (0,16, and sf only 32, 48)
3885 static void disas_movw_imm(DisasContext *s, uint32_t insn)
3887 int rd = extract32(insn, 0, 5);
3888 uint64_t imm = extract32(insn, 5, 16);
3889 int sf = extract32(insn, 31, 1);
3890 int opc = extract32(insn, 29, 2);
3891 int pos = extract32(insn, 21, 2) << 4;
3892 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3893 TCGv_i64 tcg_imm;
3895 if (!sf && (pos >= 32)) {
3896 unallocated_encoding(s);
3897 return;
3900 switch (opc) {
3901 case 0: /* MOVN */
3902 case 2: /* MOVZ */
3903 imm <<= pos;
3904 if (opc == 0) {
3905 imm = ~imm;
3907 if (!sf) {
3908 imm &= 0xffffffffu;
3910 tcg_gen_movi_i64(tcg_rd, imm);
3911 break;
3912 case 3: /* MOVK */
3913 tcg_imm = tcg_const_i64(imm);
3914 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
3915 tcg_temp_free_i64(tcg_imm);
3916 if (!sf) {
3917 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3919 break;
3920 default:
3921 unallocated_encoding(s);
3922 break;
3926 /* Bitfield
3927 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
3928 * +----+-----+-------------+---+------+------+------+------+
3929 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms | Rn | Rd |
3930 * +----+-----+-------------+---+------+------+------+------+
3932 static void disas_bitfield(DisasContext *s, uint32_t insn)
3934 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
3935 TCGv_i64 tcg_rd, tcg_tmp;
3937 sf = extract32(insn, 31, 1);
3938 opc = extract32(insn, 29, 2);
3939 n = extract32(insn, 22, 1);
3940 ri = extract32(insn, 16, 6);
3941 si = extract32(insn, 10, 6);
3942 rn = extract32(insn, 5, 5);
3943 rd = extract32(insn, 0, 5);
3944 bitsize = sf ? 64 : 32;
3946 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
3947 unallocated_encoding(s);
3948 return;
3951 tcg_rd = cpu_reg(s, rd);
3953 /* Suppress the zero-extend for !sf. Since RI and SI are constrained
3954 to be smaller than bitsize, we'll never reference data outside the
3955 low 32-bits anyway. */
3956 tcg_tmp = read_cpu_reg(s, rn, 1);
3958 /* Recognize simple(r) extractions. */
3959 if (si >= ri) {
3960 /* Wd<s-r:0> = Wn<s:r> */
3961 len = (si - ri) + 1;
3962 if (opc == 0) { /* SBFM: ASR, SBFX, SXTB, SXTH, SXTW */
3963 tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
3964 goto done;
3965 } else if (opc == 2) { /* UBFM: UBFX, LSR, UXTB, UXTH */
3966 tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
3967 return;
3969 /* opc == 1, BXFIL fall through to deposit */
3970 tcg_gen_extract_i64(tcg_tmp, tcg_tmp, ri, len);
3971 pos = 0;
3972 } else {
3973 /* Handle the ri > si case with a deposit
3974 * Wd<32+s-r,32-r> = Wn<s:0>
3976 len = si + 1;
3977 pos = (bitsize - ri) & (bitsize - 1);
3980 if (opc == 0 && len < ri) {
3981 /* SBFM: sign extend the destination field from len to fill
3982 the balance of the word. Let the deposit below insert all
3983 of those sign bits. */
3984 tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
3985 len = ri;
3988 if (opc == 1) { /* BFM, BXFIL */
3989 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
3990 } else {
3991 /* SBFM or UBFM: We start with zero, and we haven't modified
3992 any bits outside bitsize, therefore the zero-extension
3993 below is unneeded. */
3994 tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
3995 return;
3998 done:
3999 if (!sf) { /* zero extend final result */
4000 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4004 /* Extract
4005 * 31 30 29 28 23 22 21 20 16 15 10 9 5 4 0
4006 * +----+------+-------------+---+----+------+--------+------+------+
4007 * | sf | op21 | 1 0 0 1 1 1 | N | o0 | Rm | imms | Rn | Rd |
4008 * +----+------+-------------+---+----+------+--------+------+------+
4010 static void disas_extract(DisasContext *s, uint32_t insn)
4012 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
4014 sf = extract32(insn, 31, 1);
4015 n = extract32(insn, 22, 1);
4016 rm = extract32(insn, 16, 5);
4017 imm = extract32(insn, 10, 6);
4018 rn = extract32(insn, 5, 5);
4019 rd = extract32(insn, 0, 5);
4020 op21 = extract32(insn, 29, 2);
4021 op0 = extract32(insn, 21, 1);
4022 bitsize = sf ? 64 : 32;
4024 if (sf != n || op21 || op0 || imm >= bitsize) {
4025 unallocated_encoding(s);
4026 } else {
4027 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4029 tcg_rd = cpu_reg(s, rd);
4031 if (unlikely(imm == 0)) {
4032 /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4033 * so an extract from bit 0 is a special case.
4035 if (sf) {
4036 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
4037 } else {
4038 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
4040 } else if (rm == rn) { /* ROR */
4041 tcg_rm = cpu_reg(s, rm);
4042 if (sf) {
4043 tcg_gen_rotri_i64(tcg_rd, tcg_rm, imm);
4044 } else {
4045 TCGv_i32 tmp = tcg_temp_new_i32();
4046 tcg_gen_extrl_i64_i32(tmp, tcg_rm);
4047 tcg_gen_rotri_i32(tmp, tmp, imm);
4048 tcg_gen_extu_i32_i64(tcg_rd, tmp);
4049 tcg_temp_free_i32(tmp);
4051 } else {
4052 tcg_rm = read_cpu_reg(s, rm, sf);
4053 tcg_rn = read_cpu_reg(s, rn, sf);
4054 tcg_gen_shri_i64(tcg_rm, tcg_rm, imm);
4055 tcg_gen_shli_i64(tcg_rn, tcg_rn, bitsize - imm);
4056 tcg_gen_or_i64(tcg_rd, tcg_rm, tcg_rn);
4057 if (!sf) {
4058 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4064 /* Data processing - immediate */
4065 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
4067 switch (extract32(insn, 23, 6)) {
4068 case 0x20: case 0x21: /* PC-rel. addressing */
4069 disas_pc_rel_adr(s, insn);
4070 break;
4071 case 0x22: case 0x23: /* Add/subtract (immediate) */
4072 disas_add_sub_imm(s, insn);
4073 break;
4074 case 0x24: /* Logical (immediate) */
4075 disas_logic_imm(s, insn);
4076 break;
4077 case 0x25: /* Move wide (immediate) */
4078 disas_movw_imm(s, insn);
4079 break;
4080 case 0x26: /* Bitfield */
4081 disas_bitfield(s, insn);
4082 break;
4083 case 0x27: /* Extract */
4084 disas_extract(s, insn);
4085 break;
4086 default:
4087 unallocated_encoding(s);
4088 break;
4092 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4093 * Note that it is the caller's responsibility to ensure that the
4094 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4095 * mandated semantics for out of range shifts.
4097 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4098 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4100 switch (shift_type) {
4101 case A64_SHIFT_TYPE_LSL:
4102 tcg_gen_shl_i64(dst, src, shift_amount);
4103 break;
4104 case A64_SHIFT_TYPE_LSR:
4105 tcg_gen_shr_i64(dst, src, shift_amount);
4106 break;
4107 case A64_SHIFT_TYPE_ASR:
4108 if (!sf) {
4109 tcg_gen_ext32s_i64(dst, src);
4111 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4112 break;
4113 case A64_SHIFT_TYPE_ROR:
4114 if (sf) {
4115 tcg_gen_rotr_i64(dst, src, shift_amount);
4116 } else {
4117 TCGv_i32 t0, t1;
4118 t0 = tcg_temp_new_i32();
4119 t1 = tcg_temp_new_i32();
4120 tcg_gen_extrl_i64_i32(t0, src);
4121 tcg_gen_extrl_i64_i32(t1, shift_amount);
4122 tcg_gen_rotr_i32(t0, t0, t1);
4123 tcg_gen_extu_i32_i64(dst, t0);
4124 tcg_temp_free_i32(t0);
4125 tcg_temp_free_i32(t1);
4127 break;
4128 default:
4129 assert(FALSE); /* all shift types should be handled */
4130 break;
4133 if (!sf) { /* zero extend final result */
4134 tcg_gen_ext32u_i64(dst, dst);
4138 /* Shift a TCGv src by immediate, put result in dst.
4139 * The shift amount must be in range (this should always be true as the
4140 * relevant instructions will UNDEF on bad shift immediates).
4142 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4143 enum a64_shift_type shift_type, unsigned int shift_i)
4145 assert(shift_i < (sf ? 64 : 32));
4147 if (shift_i == 0) {
4148 tcg_gen_mov_i64(dst, src);
4149 } else {
4150 TCGv_i64 shift_const;
4152 shift_const = tcg_const_i64(shift_i);
4153 shift_reg(dst, src, sf, shift_type, shift_const);
4154 tcg_temp_free_i64(shift_const);
4158 /* Logical (shifted register)
4159 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
4160 * +----+-----+-----------+-------+---+------+--------+------+------+
4161 * | sf | opc | 0 1 0 1 0 | shift | N | Rm | imm6 | Rn | Rd |
4162 * +----+-----+-----------+-------+---+------+--------+------+------+
4164 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4166 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4167 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4169 sf = extract32(insn, 31, 1);
4170 opc = extract32(insn, 29, 2);
4171 shift_type = extract32(insn, 22, 2);
4172 invert = extract32(insn, 21, 1);
4173 rm = extract32(insn, 16, 5);
4174 shift_amount = extract32(insn, 10, 6);
4175 rn = extract32(insn, 5, 5);
4176 rd = extract32(insn, 0, 5);
4178 if (!sf && (shift_amount & (1 << 5))) {
4179 unallocated_encoding(s);
4180 return;
4183 tcg_rd = cpu_reg(s, rd);
4185 if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4186 /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4187 * register-register MOV and MVN, so it is worth special casing.
4189 tcg_rm = cpu_reg(s, rm);
4190 if (invert) {
4191 tcg_gen_not_i64(tcg_rd, tcg_rm);
4192 if (!sf) {
4193 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4195 } else {
4196 if (sf) {
4197 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4198 } else {
4199 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4202 return;
4205 tcg_rm = read_cpu_reg(s, rm, sf);
4207 if (shift_amount) {
4208 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4211 tcg_rn = cpu_reg(s, rn);
4213 switch (opc | (invert << 2)) {
4214 case 0: /* AND */
4215 case 3: /* ANDS */
4216 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4217 break;
4218 case 1: /* ORR */
4219 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4220 break;
4221 case 2: /* EOR */
4222 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4223 break;
4224 case 4: /* BIC */
4225 case 7: /* BICS */
4226 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4227 break;
4228 case 5: /* ORN */
4229 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4230 break;
4231 case 6: /* EON */
4232 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4233 break;
4234 default:
4235 assert(FALSE);
4236 break;
4239 if (!sf) {
4240 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4243 if (opc == 3) {
4244 gen_logic_CC(sf, tcg_rd);
4249 * Add/subtract (extended register)
4251 * 31|30|29|28 24|23 22|21|20 16|15 13|12 10|9 5|4 0|
4252 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4253 * |sf|op| S| 0 1 0 1 1 | opt | 1| Rm |option| imm3 | Rn | Rd |
4254 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4256 * sf: 0 -> 32bit, 1 -> 64bit
4257 * op: 0 -> add , 1 -> sub
4258 * S: 1 -> set flags
4259 * opt: 00
4260 * option: extension type (see DecodeRegExtend)
4261 * imm3: optional shift to Rm
4263 * Rd = Rn + LSL(extend(Rm), amount)
4265 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4267 int rd = extract32(insn, 0, 5);
4268 int rn = extract32(insn, 5, 5);
4269 int imm3 = extract32(insn, 10, 3);
4270 int option = extract32(insn, 13, 3);
4271 int rm = extract32(insn, 16, 5);
4272 int opt = extract32(insn, 22, 2);
4273 bool setflags = extract32(insn, 29, 1);
4274 bool sub_op = extract32(insn, 30, 1);
4275 bool sf = extract32(insn, 31, 1);
4277 TCGv_i64 tcg_rm, tcg_rn; /* temps */
4278 TCGv_i64 tcg_rd;
4279 TCGv_i64 tcg_result;
4281 if (imm3 > 4 || opt != 0) {
4282 unallocated_encoding(s);
4283 return;
4286 /* non-flag setting ops may use SP */
4287 if (!setflags) {
4288 tcg_rd = cpu_reg_sp(s, rd);
4289 } else {
4290 tcg_rd = cpu_reg(s, rd);
4292 tcg_rn = read_cpu_reg_sp(s, rn, sf);
4294 tcg_rm = read_cpu_reg(s, rm, sf);
4295 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4297 tcg_result = tcg_temp_new_i64();
4299 if (!setflags) {
4300 if (sub_op) {
4301 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4302 } else {
4303 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4305 } else {
4306 if (sub_op) {
4307 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4308 } else {
4309 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4313 if (sf) {
4314 tcg_gen_mov_i64(tcg_rd, tcg_result);
4315 } else {
4316 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4319 tcg_temp_free_i64(tcg_result);
4323 * Add/subtract (shifted register)
4325 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
4326 * +--+--+--+-----------+-----+--+-------+---------+------+------+
4327 * |sf|op| S| 0 1 0 1 1 |shift| 0| Rm | imm6 | Rn | Rd |
4328 * +--+--+--+-----------+-----+--+-------+---------+------+------+
4330 * sf: 0 -> 32bit, 1 -> 64bit
4331 * op: 0 -> add , 1 -> sub
4332 * S: 1 -> set flags
4333 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4334 * imm6: Shift amount to apply to Rm before the add/sub
4336 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4338 int rd = extract32(insn, 0, 5);
4339 int rn = extract32(insn, 5, 5);
4340 int imm6 = extract32(insn, 10, 6);
4341 int rm = extract32(insn, 16, 5);
4342 int shift_type = extract32(insn, 22, 2);
4343 bool setflags = extract32(insn, 29, 1);
4344 bool sub_op = extract32(insn, 30, 1);
4345 bool sf = extract32(insn, 31, 1);
4347 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4348 TCGv_i64 tcg_rn, tcg_rm;
4349 TCGv_i64 tcg_result;
4351 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4352 unallocated_encoding(s);
4353 return;
4356 tcg_rn = read_cpu_reg(s, rn, sf);
4357 tcg_rm = read_cpu_reg(s, rm, sf);
4359 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4361 tcg_result = tcg_temp_new_i64();
4363 if (!setflags) {
4364 if (sub_op) {
4365 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4366 } else {
4367 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4369 } else {
4370 if (sub_op) {
4371 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4372 } else {
4373 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4377 if (sf) {
4378 tcg_gen_mov_i64(tcg_rd, tcg_result);
4379 } else {
4380 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4383 tcg_temp_free_i64(tcg_result);
4386 /* Data-processing (3 source)
4388 * 31 30 29 28 24 23 21 20 16 15 14 10 9 5 4 0
4389 * +--+------+-----------+------+------+----+------+------+------+
4390 * |sf| op54 | 1 1 0 1 1 | op31 | Rm | o0 | Ra | Rn | Rd |
4391 * +--+------+-----------+------+------+----+------+------+------+
4393 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4395 int rd = extract32(insn, 0, 5);
4396 int rn = extract32(insn, 5, 5);
4397 int ra = extract32(insn, 10, 5);
4398 int rm = extract32(insn, 16, 5);
4399 int op_id = (extract32(insn, 29, 3) << 4) |
4400 (extract32(insn, 21, 3) << 1) |
4401 extract32(insn, 15, 1);
4402 bool sf = extract32(insn, 31, 1);
4403 bool is_sub = extract32(op_id, 0, 1);
4404 bool is_high = extract32(op_id, 2, 1);
4405 bool is_signed = false;
4406 TCGv_i64 tcg_op1;
4407 TCGv_i64 tcg_op2;
4408 TCGv_i64 tcg_tmp;
4410 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
4411 switch (op_id) {
4412 case 0x42: /* SMADDL */
4413 case 0x43: /* SMSUBL */
4414 case 0x44: /* SMULH */
4415 is_signed = true;
4416 break;
4417 case 0x0: /* MADD (32bit) */
4418 case 0x1: /* MSUB (32bit) */
4419 case 0x40: /* MADD (64bit) */
4420 case 0x41: /* MSUB (64bit) */
4421 case 0x4a: /* UMADDL */
4422 case 0x4b: /* UMSUBL */
4423 case 0x4c: /* UMULH */
4424 break;
4425 default:
4426 unallocated_encoding(s);
4427 return;
4430 if (is_high) {
4431 TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
4432 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4433 TCGv_i64 tcg_rn = cpu_reg(s, rn);
4434 TCGv_i64 tcg_rm = cpu_reg(s, rm);
4436 if (is_signed) {
4437 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4438 } else {
4439 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4442 tcg_temp_free_i64(low_bits);
4443 return;
4446 tcg_op1 = tcg_temp_new_i64();
4447 tcg_op2 = tcg_temp_new_i64();
4448 tcg_tmp = tcg_temp_new_i64();
4450 if (op_id < 0x42) {
4451 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
4452 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
4453 } else {
4454 if (is_signed) {
4455 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
4456 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
4457 } else {
4458 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
4459 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
4463 if (ra == 31 && !is_sub) {
4464 /* Special-case MADD with rA == XZR; it is the standard MUL alias */
4465 tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
4466 } else {
4467 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
4468 if (is_sub) {
4469 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4470 } else {
4471 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4475 if (!sf) {
4476 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
4479 tcg_temp_free_i64(tcg_op1);
4480 tcg_temp_free_i64(tcg_op2);
4481 tcg_temp_free_i64(tcg_tmp);
4484 /* Add/subtract (with carry)
4485 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
4486 * +--+--+--+------------------------+------+---------+------+-----+
4487 * |sf|op| S| 1 1 0 1 0 0 0 0 | rm | opcode2 | Rn | Rd |
4488 * +--+--+--+------------------------+------+---------+------+-----+
4489 * [000000]
4492 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
4494 unsigned int sf, op, setflags, rm, rn, rd;
4495 TCGv_i64 tcg_y, tcg_rn, tcg_rd;
4497 if (extract32(insn, 10, 6) != 0) {
4498 unallocated_encoding(s);
4499 return;
4502 sf = extract32(insn, 31, 1);
4503 op = extract32(insn, 30, 1);
4504 setflags = extract32(insn, 29, 1);
4505 rm = extract32(insn, 16, 5);
4506 rn = extract32(insn, 5, 5);
4507 rd = extract32(insn, 0, 5);
4509 tcg_rd = cpu_reg(s, rd);
4510 tcg_rn = cpu_reg(s, rn);
4512 if (op) {
4513 tcg_y = new_tmp_a64(s);
4514 tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
4515 } else {
4516 tcg_y = cpu_reg(s, rm);
4519 if (setflags) {
4520 gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
4521 } else {
4522 gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
4526 /* Conditional compare (immediate / register)
4527 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
4528 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
4529 * |sf|op| S| 1 1 0 1 0 0 1 0 |imm5/rm | cond |i/r |o2| Rn |o3|nzcv |
4530 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
4531 * [1] y [0] [0]
4533 static void disas_cc(DisasContext *s, uint32_t insn)
4535 unsigned int sf, op, y, cond, rn, nzcv, is_imm;
4536 TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
4537 TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
4538 DisasCompare c;
4540 if (!extract32(insn, 29, 1)) {
4541 unallocated_encoding(s);
4542 return;
4544 if (insn & (1 << 10 | 1 << 4)) {
4545 unallocated_encoding(s);
4546 return;
4548 sf = extract32(insn, 31, 1);
4549 op = extract32(insn, 30, 1);
4550 is_imm = extract32(insn, 11, 1);
4551 y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
4552 cond = extract32(insn, 12, 4);
4553 rn = extract32(insn, 5, 5);
4554 nzcv = extract32(insn, 0, 4);
4556 /* Set T0 = !COND. */
4557 tcg_t0 = tcg_temp_new_i32();
4558 arm_test_cc(&c, cond);
4559 tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
4560 arm_free_cc(&c);
4562 /* Load the arguments for the new comparison. */
4563 if (is_imm) {
4564 tcg_y = new_tmp_a64(s);
4565 tcg_gen_movi_i64(tcg_y, y);
4566 } else {
4567 tcg_y = cpu_reg(s, y);
4569 tcg_rn = cpu_reg(s, rn);
4571 /* Set the flags for the new comparison. */
4572 tcg_tmp = tcg_temp_new_i64();
4573 if (op) {
4574 gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
4575 } else {
4576 gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
4578 tcg_temp_free_i64(tcg_tmp);
4580 /* If COND was false, force the flags to #nzcv. Compute two masks
4581 * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
4582 * For tcg hosts that support ANDC, we can make do with just T1.
4583 * In either case, allow the tcg optimizer to delete any unused mask.
4585 tcg_t1 = tcg_temp_new_i32();
4586 tcg_t2 = tcg_temp_new_i32();
4587 tcg_gen_neg_i32(tcg_t1, tcg_t0);
4588 tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
4590 if (nzcv & 8) { /* N */
4591 tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
4592 } else {
4593 if (TCG_TARGET_HAS_andc_i32) {
4594 tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
4595 } else {
4596 tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
4599 if (nzcv & 4) { /* Z */
4600 if (TCG_TARGET_HAS_andc_i32) {
4601 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
4602 } else {
4603 tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
4605 } else {
4606 tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
4608 if (nzcv & 2) { /* C */
4609 tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
4610 } else {
4611 if (TCG_TARGET_HAS_andc_i32) {
4612 tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
4613 } else {
4614 tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
4617 if (nzcv & 1) { /* V */
4618 tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
4619 } else {
4620 if (TCG_TARGET_HAS_andc_i32) {
4621 tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
4622 } else {
4623 tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
4626 tcg_temp_free_i32(tcg_t0);
4627 tcg_temp_free_i32(tcg_t1);
4628 tcg_temp_free_i32(tcg_t2);
4631 /* Conditional select
4632 * 31 30 29 28 21 20 16 15 12 11 10 9 5 4 0
4633 * +----+----+---+-----------------+------+------+-----+------+------+
4634 * | sf | op | S | 1 1 0 1 0 1 0 0 | Rm | cond | op2 | Rn | Rd |
4635 * +----+----+---+-----------------+------+------+-----+------+------+
4637 static void disas_cond_select(DisasContext *s, uint32_t insn)
4639 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
4640 TCGv_i64 tcg_rd, zero;
4641 DisasCompare64 c;
4643 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
4644 /* S == 1 or op2<1> == 1 */
4645 unallocated_encoding(s);
4646 return;
4648 sf = extract32(insn, 31, 1);
4649 else_inv = extract32(insn, 30, 1);
4650 rm = extract32(insn, 16, 5);
4651 cond = extract32(insn, 12, 4);
4652 else_inc = extract32(insn, 10, 1);
4653 rn = extract32(insn, 5, 5);
4654 rd = extract32(insn, 0, 5);
4656 tcg_rd = cpu_reg(s, rd);
4658 a64_test_cc(&c, cond);
4659 zero = tcg_const_i64(0);
4661 if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
4662 /* CSET & CSETM. */
4663 tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
4664 if (else_inv) {
4665 tcg_gen_neg_i64(tcg_rd, tcg_rd);
4667 } else {
4668 TCGv_i64 t_true = cpu_reg(s, rn);
4669 TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
4670 if (else_inv && else_inc) {
4671 tcg_gen_neg_i64(t_false, t_false);
4672 } else if (else_inv) {
4673 tcg_gen_not_i64(t_false, t_false);
4674 } else if (else_inc) {
4675 tcg_gen_addi_i64(t_false, t_false, 1);
4677 tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
4680 tcg_temp_free_i64(zero);
4681 a64_free_cc(&c);
4683 if (!sf) {
4684 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4688 static void handle_clz(DisasContext *s, unsigned int sf,
4689 unsigned int rn, unsigned int rd)
4691 TCGv_i64 tcg_rd, tcg_rn;
4692 tcg_rd = cpu_reg(s, rd);
4693 tcg_rn = cpu_reg(s, rn);
4695 if (sf) {
4696 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
4697 } else {
4698 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4699 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4700 tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
4701 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4702 tcg_temp_free_i32(tcg_tmp32);
4706 static void handle_cls(DisasContext *s, unsigned int sf,
4707 unsigned int rn, unsigned int rd)
4709 TCGv_i64 tcg_rd, tcg_rn;
4710 tcg_rd = cpu_reg(s, rd);
4711 tcg_rn = cpu_reg(s, rn);
4713 if (sf) {
4714 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
4715 } else {
4716 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4717 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4718 tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
4719 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4720 tcg_temp_free_i32(tcg_tmp32);
4724 static void handle_rbit(DisasContext *s, unsigned int sf,
4725 unsigned int rn, unsigned int rd)
4727 TCGv_i64 tcg_rd, tcg_rn;
4728 tcg_rd = cpu_reg(s, rd);
4729 tcg_rn = cpu_reg(s, rn);
4731 if (sf) {
4732 gen_helper_rbit64(tcg_rd, tcg_rn);
4733 } else {
4734 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4735 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4736 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
4737 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4738 tcg_temp_free_i32(tcg_tmp32);
4742 /* REV with sf==1, opcode==3 ("REV64") */
4743 static void handle_rev64(DisasContext *s, unsigned int sf,
4744 unsigned int rn, unsigned int rd)
4746 if (!sf) {
4747 unallocated_encoding(s);
4748 return;
4750 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
4753 /* REV with sf==0, opcode==2
4754 * REV32 (sf==1, opcode==2)
4756 static void handle_rev32(DisasContext *s, unsigned int sf,
4757 unsigned int rn, unsigned int rd)
4759 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4761 if (sf) {
4762 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4763 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4765 /* bswap32_i64 requires zero high word */
4766 tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
4767 tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
4768 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
4769 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
4770 tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
4772 tcg_temp_free_i64(tcg_tmp);
4773 } else {
4774 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
4775 tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
4779 /* REV16 (opcode==1) */
4780 static void handle_rev16(DisasContext *s, unsigned int sf,
4781 unsigned int rn, unsigned int rd)
4783 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4784 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4785 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4786 TCGv_i64 mask = tcg_const_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
4788 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
4789 tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
4790 tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
4791 tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
4792 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
4794 tcg_temp_free_i64(mask);
4795 tcg_temp_free_i64(tcg_tmp);
4798 /* Data-processing (1 source)
4799 * 31 30 29 28 21 20 16 15 10 9 5 4 0
4800 * +----+---+---+-----------------+---------+--------+------+------+
4801 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode | Rn | Rd |
4802 * +----+---+---+-----------------+---------+--------+------+------+
4804 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
4806 unsigned int sf, opcode, opcode2, rn, rd;
4807 TCGv_i64 tcg_rd;
4809 if (extract32(insn, 29, 1)) {
4810 unallocated_encoding(s);
4811 return;
4814 sf = extract32(insn, 31, 1);
4815 opcode = extract32(insn, 10, 6);
4816 opcode2 = extract32(insn, 16, 5);
4817 rn = extract32(insn, 5, 5);
4818 rd = extract32(insn, 0, 5);
4820 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
4822 switch (MAP(sf, opcode2, opcode)) {
4823 case MAP(0, 0x00, 0x00): /* RBIT */
4824 case MAP(1, 0x00, 0x00):
4825 handle_rbit(s, sf, rn, rd);
4826 break;
4827 case MAP(0, 0x00, 0x01): /* REV16 */
4828 case MAP(1, 0x00, 0x01):
4829 handle_rev16(s, sf, rn, rd);
4830 break;
4831 case MAP(0, 0x00, 0x02): /* REV/REV32 */
4832 case MAP(1, 0x00, 0x02):
4833 handle_rev32(s, sf, rn, rd);
4834 break;
4835 case MAP(1, 0x00, 0x03): /* REV64 */
4836 handle_rev64(s, sf, rn, rd);
4837 break;
4838 case MAP(0, 0x00, 0x04): /* CLZ */
4839 case MAP(1, 0x00, 0x04):
4840 handle_clz(s, sf, rn, rd);
4841 break;
4842 case MAP(0, 0x00, 0x05): /* CLS */
4843 case MAP(1, 0x00, 0x05):
4844 handle_cls(s, sf, rn, rd);
4845 break;
4846 case MAP(1, 0x01, 0x00): /* PACIA */
4847 if (s->pauth_active) {
4848 tcg_rd = cpu_reg(s, rd);
4849 gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4850 } else if (!dc_isar_feature(aa64_pauth, s)) {
4851 goto do_unallocated;
4853 break;
4854 case MAP(1, 0x01, 0x01): /* PACIB */
4855 if (s->pauth_active) {
4856 tcg_rd = cpu_reg(s, rd);
4857 gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4858 } else if (!dc_isar_feature(aa64_pauth, s)) {
4859 goto do_unallocated;
4861 break;
4862 case MAP(1, 0x01, 0x02): /* PACDA */
4863 if (s->pauth_active) {
4864 tcg_rd = cpu_reg(s, rd);
4865 gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4866 } else if (!dc_isar_feature(aa64_pauth, s)) {
4867 goto do_unallocated;
4869 break;
4870 case MAP(1, 0x01, 0x03): /* PACDB */
4871 if (s->pauth_active) {
4872 tcg_rd = cpu_reg(s, rd);
4873 gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4874 } else if (!dc_isar_feature(aa64_pauth, s)) {
4875 goto do_unallocated;
4877 break;
4878 case MAP(1, 0x01, 0x04): /* AUTIA */
4879 if (s->pauth_active) {
4880 tcg_rd = cpu_reg(s, rd);
4881 gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4882 } else if (!dc_isar_feature(aa64_pauth, s)) {
4883 goto do_unallocated;
4885 break;
4886 case MAP(1, 0x01, 0x05): /* AUTIB */
4887 if (s->pauth_active) {
4888 tcg_rd = cpu_reg(s, rd);
4889 gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4890 } else if (!dc_isar_feature(aa64_pauth, s)) {
4891 goto do_unallocated;
4893 break;
4894 case MAP(1, 0x01, 0x06): /* AUTDA */
4895 if (s->pauth_active) {
4896 tcg_rd = cpu_reg(s, rd);
4897 gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4898 } else if (!dc_isar_feature(aa64_pauth, s)) {
4899 goto do_unallocated;
4901 break;
4902 case MAP(1, 0x01, 0x07): /* AUTDB */
4903 if (s->pauth_active) {
4904 tcg_rd = cpu_reg(s, rd);
4905 gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4906 } else if (!dc_isar_feature(aa64_pauth, s)) {
4907 goto do_unallocated;
4909 break;
4910 case MAP(1, 0x01, 0x08): /* PACIZA */
4911 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
4912 goto do_unallocated;
4913 } else if (s->pauth_active) {
4914 tcg_rd = cpu_reg(s, rd);
4915 gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
4917 break;
4918 case MAP(1, 0x01, 0x09): /* PACIZB */
4919 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
4920 goto do_unallocated;
4921 } else if (s->pauth_active) {
4922 tcg_rd = cpu_reg(s, rd);
4923 gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
4925 break;
4926 case MAP(1, 0x01, 0x0a): /* PACDZA */
4927 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
4928 goto do_unallocated;
4929 } else if (s->pauth_active) {
4930 tcg_rd = cpu_reg(s, rd);
4931 gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
4933 break;
4934 case MAP(1, 0x01, 0x0b): /* PACDZB */
4935 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
4936 goto do_unallocated;
4937 } else if (s->pauth_active) {
4938 tcg_rd = cpu_reg(s, rd);
4939 gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
4941 break;
4942 case MAP(1, 0x01, 0x0c): /* AUTIZA */
4943 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
4944 goto do_unallocated;
4945 } else if (s->pauth_active) {
4946 tcg_rd = cpu_reg(s, rd);
4947 gen_helper_autia(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
4949 break;
4950 case MAP(1, 0x01, 0x0d): /* AUTIZB */
4951 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
4952 goto do_unallocated;
4953 } else if (s->pauth_active) {
4954 tcg_rd = cpu_reg(s, rd);
4955 gen_helper_autib(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
4957 break;
4958 case MAP(1, 0x01, 0x0e): /* AUTDZA */
4959 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
4960 goto do_unallocated;
4961 } else if (s->pauth_active) {
4962 tcg_rd = cpu_reg(s, rd);
4963 gen_helper_autda(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
4965 break;
4966 case MAP(1, 0x01, 0x0f): /* AUTDZB */
4967 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
4968 goto do_unallocated;
4969 } else if (s->pauth_active) {
4970 tcg_rd = cpu_reg(s, rd);
4971 gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
4973 break;
4974 case MAP(1, 0x01, 0x10): /* XPACI */
4975 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
4976 goto do_unallocated;
4977 } else if (s->pauth_active) {
4978 tcg_rd = cpu_reg(s, rd);
4979 gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
4981 break;
4982 case MAP(1, 0x01, 0x11): /* XPACD */
4983 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
4984 goto do_unallocated;
4985 } else if (s->pauth_active) {
4986 tcg_rd = cpu_reg(s, rd);
4987 gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
4989 break;
4990 default:
4991 do_unallocated:
4992 unallocated_encoding(s);
4993 break;
4996 #undef MAP
4999 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5000 unsigned int rm, unsigned int rn, unsigned int rd)
5002 TCGv_i64 tcg_n, tcg_m, tcg_rd;
5003 tcg_rd = cpu_reg(s, rd);
5005 if (!sf && is_signed) {
5006 tcg_n = new_tmp_a64(s);
5007 tcg_m = new_tmp_a64(s);
5008 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5009 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5010 } else {
5011 tcg_n = read_cpu_reg(s, rn, sf);
5012 tcg_m = read_cpu_reg(s, rm, sf);
5015 if (is_signed) {
5016 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5017 } else {
5018 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5021 if (!sf) { /* zero extend final result */
5022 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5026 /* LSLV, LSRV, ASRV, RORV */
5027 static void handle_shift_reg(DisasContext *s,
5028 enum a64_shift_type shift_type, unsigned int sf,
5029 unsigned int rm, unsigned int rn, unsigned int rd)
5031 TCGv_i64 tcg_shift = tcg_temp_new_i64();
5032 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5033 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5035 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5036 shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5037 tcg_temp_free_i64(tcg_shift);
5040 /* CRC32[BHWX], CRC32C[BHWX] */
5041 static void handle_crc32(DisasContext *s,
5042 unsigned int sf, unsigned int sz, bool crc32c,
5043 unsigned int rm, unsigned int rn, unsigned int rd)
5045 TCGv_i64 tcg_acc, tcg_val;
5046 TCGv_i32 tcg_bytes;
5048 if (!dc_isar_feature(aa64_crc32, s)
5049 || (sf == 1 && sz != 3)
5050 || (sf == 0 && sz == 3)) {
5051 unallocated_encoding(s);
5052 return;
5055 if (sz == 3) {
5056 tcg_val = cpu_reg(s, rm);
5057 } else {
5058 uint64_t mask;
5059 switch (sz) {
5060 case 0:
5061 mask = 0xFF;
5062 break;
5063 case 1:
5064 mask = 0xFFFF;
5065 break;
5066 case 2:
5067 mask = 0xFFFFFFFF;
5068 break;
5069 default:
5070 g_assert_not_reached();
5072 tcg_val = new_tmp_a64(s);
5073 tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5076 tcg_acc = cpu_reg(s, rn);
5077 tcg_bytes = tcg_const_i32(1 << sz);
5079 if (crc32c) {
5080 gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5081 } else {
5082 gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5085 tcg_temp_free_i32(tcg_bytes);
5088 /* Data-processing (2 source)
5089 * 31 30 29 28 21 20 16 15 10 9 5 4 0
5090 * +----+---+---+-----------------+------+--------+------+------+
5091 * | sf | 0 | S | 1 1 0 1 0 1 1 0 | Rm | opcode | Rn | Rd |
5092 * +----+---+---+-----------------+------+--------+------+------+
5094 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5096 unsigned int sf, rm, opcode, rn, rd;
5097 sf = extract32(insn, 31, 1);
5098 rm = extract32(insn, 16, 5);
5099 opcode = extract32(insn, 10, 6);
5100 rn = extract32(insn, 5, 5);
5101 rd = extract32(insn, 0, 5);
5103 if (extract32(insn, 29, 1)) {
5104 unallocated_encoding(s);
5105 return;
5108 switch (opcode) {
5109 case 2: /* UDIV */
5110 handle_div(s, false, sf, rm, rn, rd);
5111 break;
5112 case 3: /* SDIV */
5113 handle_div(s, true, sf, rm, rn, rd);
5114 break;
5115 case 8: /* LSLV */
5116 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5117 break;
5118 case 9: /* LSRV */
5119 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5120 break;
5121 case 10: /* ASRV */
5122 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5123 break;
5124 case 11: /* RORV */
5125 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5126 break;
5127 case 12: /* PACGA */
5128 if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5129 goto do_unallocated;
5131 gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5132 cpu_reg(s, rn), cpu_reg_sp(s, rm));
5133 break;
5134 case 16:
5135 case 17:
5136 case 18:
5137 case 19:
5138 case 20:
5139 case 21:
5140 case 22:
5141 case 23: /* CRC32 */
5143 int sz = extract32(opcode, 0, 2);
5144 bool crc32c = extract32(opcode, 2, 1);
5145 handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5146 break;
5148 default:
5149 do_unallocated:
5150 unallocated_encoding(s);
5151 break;
5155 /* Data processing - register */
5156 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5158 switch (extract32(insn, 24, 5)) {
5159 case 0x0a: /* Logical (shifted register) */
5160 disas_logic_reg(s, insn);
5161 break;
5162 case 0x0b: /* Add/subtract */
5163 if (insn & (1 << 21)) { /* (extended register) */
5164 disas_add_sub_ext_reg(s, insn);
5165 } else {
5166 disas_add_sub_reg(s, insn);
5168 break;
5169 case 0x1b: /* Data-processing (3 source) */
5170 disas_data_proc_3src(s, insn);
5171 break;
5172 case 0x1a:
5173 switch (extract32(insn, 21, 3)) {
5174 case 0x0: /* Add/subtract (with carry) */
5175 disas_adc_sbc(s, insn);
5176 break;
5177 case 0x2: /* Conditional compare */
5178 disas_cc(s, insn); /* both imm and reg forms */
5179 break;
5180 case 0x4: /* Conditional select */
5181 disas_cond_select(s, insn);
5182 break;
5183 case 0x6: /* Data-processing */
5184 if (insn & (1 << 30)) { /* (1 source) */
5185 disas_data_proc_1src(s, insn);
5186 } else { /* (2 source) */
5187 disas_data_proc_2src(s, insn);
5189 break;
5190 default:
5191 unallocated_encoding(s);
5192 break;
5194 break;
5195 default:
5196 unallocated_encoding(s);
5197 break;
5201 static void handle_fp_compare(DisasContext *s, int size,
5202 unsigned int rn, unsigned int rm,
5203 bool cmp_with_zero, bool signal_all_nans)
5205 TCGv_i64 tcg_flags = tcg_temp_new_i64();
5206 TCGv_ptr fpst = get_fpstatus_ptr(size == MO_16);
5208 if (size == MO_64) {
5209 TCGv_i64 tcg_vn, tcg_vm;
5211 tcg_vn = read_fp_dreg(s, rn);
5212 if (cmp_with_zero) {
5213 tcg_vm = tcg_const_i64(0);
5214 } else {
5215 tcg_vm = read_fp_dreg(s, rm);
5217 if (signal_all_nans) {
5218 gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5219 } else {
5220 gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5222 tcg_temp_free_i64(tcg_vn);
5223 tcg_temp_free_i64(tcg_vm);
5224 } else {
5225 TCGv_i32 tcg_vn = tcg_temp_new_i32();
5226 TCGv_i32 tcg_vm = tcg_temp_new_i32();
5228 read_vec_element_i32(s, tcg_vn, rn, 0, size);
5229 if (cmp_with_zero) {
5230 tcg_gen_movi_i32(tcg_vm, 0);
5231 } else {
5232 read_vec_element_i32(s, tcg_vm, rm, 0, size);
5235 switch (size) {
5236 case MO_32:
5237 if (signal_all_nans) {
5238 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5239 } else {
5240 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5242 break;
5243 case MO_16:
5244 if (signal_all_nans) {
5245 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5246 } else {
5247 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5249 break;
5250 default:
5251 g_assert_not_reached();
5254 tcg_temp_free_i32(tcg_vn);
5255 tcg_temp_free_i32(tcg_vm);
5258 tcg_temp_free_ptr(fpst);
5260 gen_set_nzcv(tcg_flags);
5262 tcg_temp_free_i64(tcg_flags);
5265 /* Floating point compare
5266 * 31 30 29 28 24 23 22 21 20 16 15 14 13 10 9 5 4 0
5267 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5268 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | op | 1 0 0 0 | Rn | op2 |
5269 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5271 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5273 unsigned int mos, type, rm, op, rn, opc, op2r;
5274 int size;
5276 mos = extract32(insn, 29, 3);
5277 type = extract32(insn, 22, 2);
5278 rm = extract32(insn, 16, 5);
5279 op = extract32(insn, 14, 2);
5280 rn = extract32(insn, 5, 5);
5281 opc = extract32(insn, 3, 2);
5282 op2r = extract32(insn, 0, 3);
5284 if (mos || op || op2r) {
5285 unallocated_encoding(s);
5286 return;
5289 switch (type) {
5290 case 0:
5291 size = MO_32;
5292 break;
5293 case 1:
5294 size = MO_64;
5295 break;
5296 case 3:
5297 size = MO_16;
5298 if (dc_isar_feature(aa64_fp16, s)) {
5299 break;
5301 /* fallthru */
5302 default:
5303 unallocated_encoding(s);
5304 return;
5307 if (!fp_access_check(s)) {
5308 return;
5311 handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
5314 /* Floating point conditional compare
5315 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
5316 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5317 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 0 1 | Rn | op | nzcv |
5318 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5320 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
5322 unsigned int mos, type, rm, cond, rn, op, nzcv;
5323 TCGv_i64 tcg_flags;
5324 TCGLabel *label_continue = NULL;
5325 int size;
5327 mos = extract32(insn, 29, 3);
5328 type = extract32(insn, 22, 2);
5329 rm = extract32(insn, 16, 5);
5330 cond = extract32(insn, 12, 4);
5331 rn = extract32(insn, 5, 5);
5332 op = extract32(insn, 4, 1);
5333 nzcv = extract32(insn, 0, 4);
5335 if (mos) {
5336 unallocated_encoding(s);
5337 return;
5340 switch (type) {
5341 case 0:
5342 size = MO_32;
5343 break;
5344 case 1:
5345 size = MO_64;
5346 break;
5347 case 3:
5348 size = MO_16;
5349 if (dc_isar_feature(aa64_fp16, s)) {
5350 break;
5352 /* fallthru */
5353 default:
5354 unallocated_encoding(s);
5355 return;
5358 if (!fp_access_check(s)) {
5359 return;
5362 if (cond < 0x0e) { /* not always */
5363 TCGLabel *label_match = gen_new_label();
5364 label_continue = gen_new_label();
5365 arm_gen_test_cc(cond, label_match);
5366 /* nomatch: */
5367 tcg_flags = tcg_const_i64(nzcv << 28);
5368 gen_set_nzcv(tcg_flags);
5369 tcg_temp_free_i64(tcg_flags);
5370 tcg_gen_br(label_continue);
5371 gen_set_label(label_match);
5374 handle_fp_compare(s, size, rn, rm, false, op);
5376 if (cond < 0x0e) {
5377 gen_set_label(label_continue);
5381 /* Floating point conditional select
5382 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
5383 * +---+---+---+-----------+------+---+------+------+-----+------+------+
5384 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 1 1 | Rn | Rd |
5385 * +---+---+---+-----------+------+---+------+------+-----+------+------+
5387 static void disas_fp_csel(DisasContext *s, uint32_t insn)
5389 unsigned int mos, type, rm, cond, rn, rd;
5390 TCGv_i64 t_true, t_false, t_zero;
5391 DisasCompare64 c;
5392 TCGMemOp sz;
5394 mos = extract32(insn, 29, 3);
5395 type = extract32(insn, 22, 2);
5396 rm = extract32(insn, 16, 5);
5397 cond = extract32(insn, 12, 4);
5398 rn = extract32(insn, 5, 5);
5399 rd = extract32(insn, 0, 5);
5401 if (mos) {
5402 unallocated_encoding(s);
5403 return;
5406 switch (type) {
5407 case 0:
5408 sz = MO_32;
5409 break;
5410 case 1:
5411 sz = MO_64;
5412 break;
5413 case 3:
5414 sz = MO_16;
5415 if (dc_isar_feature(aa64_fp16, s)) {
5416 break;
5418 /* fallthru */
5419 default:
5420 unallocated_encoding(s);
5421 return;
5424 if (!fp_access_check(s)) {
5425 return;
5428 /* Zero extend sreg & hreg inputs to 64 bits now. */
5429 t_true = tcg_temp_new_i64();
5430 t_false = tcg_temp_new_i64();
5431 read_vec_element(s, t_true, rn, 0, sz);
5432 read_vec_element(s, t_false, rm, 0, sz);
5434 a64_test_cc(&c, cond);
5435 t_zero = tcg_const_i64(0);
5436 tcg_gen_movcond_i64(c.cond, t_true, c.value, t_zero, t_true, t_false);
5437 tcg_temp_free_i64(t_zero);
5438 tcg_temp_free_i64(t_false);
5439 a64_free_cc(&c);
5441 /* Note that sregs & hregs write back zeros to the high bits,
5442 and we've already done the zero-extension. */
5443 write_fp_dreg(s, rd, t_true);
5444 tcg_temp_free_i64(t_true);
5447 /* Floating-point data-processing (1 source) - half precision */
5448 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
5450 TCGv_ptr fpst = NULL;
5451 TCGv_i32 tcg_op = read_fp_hreg(s, rn);
5452 TCGv_i32 tcg_res = tcg_temp_new_i32();
5454 switch (opcode) {
5455 case 0x0: /* FMOV */
5456 tcg_gen_mov_i32(tcg_res, tcg_op);
5457 break;
5458 case 0x1: /* FABS */
5459 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
5460 break;
5461 case 0x2: /* FNEG */
5462 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
5463 break;
5464 case 0x3: /* FSQRT */
5465 fpst = get_fpstatus_ptr(true);
5466 gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
5467 break;
5468 case 0x8: /* FRINTN */
5469 case 0x9: /* FRINTP */
5470 case 0xa: /* FRINTM */
5471 case 0xb: /* FRINTZ */
5472 case 0xc: /* FRINTA */
5474 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
5475 fpst = get_fpstatus_ptr(true);
5477 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5478 gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
5480 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5481 tcg_temp_free_i32(tcg_rmode);
5482 break;
5484 case 0xe: /* FRINTX */
5485 fpst = get_fpstatus_ptr(true);
5486 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
5487 break;
5488 case 0xf: /* FRINTI */
5489 fpst = get_fpstatus_ptr(true);
5490 gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
5491 break;
5492 default:
5493 abort();
5496 write_fp_sreg(s, rd, tcg_res);
5498 if (fpst) {
5499 tcg_temp_free_ptr(fpst);
5501 tcg_temp_free_i32(tcg_op);
5502 tcg_temp_free_i32(tcg_res);
5505 /* Floating-point data-processing (1 source) - single precision */
5506 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
5508 TCGv_ptr fpst;
5509 TCGv_i32 tcg_op;
5510 TCGv_i32 tcg_res;
5512 fpst = get_fpstatus_ptr(false);
5513 tcg_op = read_fp_sreg(s, rn);
5514 tcg_res = tcg_temp_new_i32();
5516 switch (opcode) {
5517 case 0x0: /* FMOV */
5518 tcg_gen_mov_i32(tcg_res, tcg_op);
5519 break;
5520 case 0x1: /* FABS */
5521 gen_helper_vfp_abss(tcg_res, tcg_op);
5522 break;
5523 case 0x2: /* FNEG */
5524 gen_helper_vfp_negs(tcg_res, tcg_op);
5525 break;
5526 case 0x3: /* FSQRT */
5527 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
5528 break;
5529 case 0x8: /* FRINTN */
5530 case 0x9: /* FRINTP */
5531 case 0xa: /* FRINTM */
5532 case 0xb: /* FRINTZ */
5533 case 0xc: /* FRINTA */
5535 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
5537 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5538 gen_helper_rints(tcg_res, tcg_op, fpst);
5540 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5541 tcg_temp_free_i32(tcg_rmode);
5542 break;
5544 case 0xe: /* FRINTX */
5545 gen_helper_rints_exact(tcg_res, tcg_op, fpst);
5546 break;
5547 case 0xf: /* FRINTI */
5548 gen_helper_rints(tcg_res, tcg_op, fpst);
5549 break;
5550 default:
5551 abort();
5554 write_fp_sreg(s, rd, tcg_res);
5556 tcg_temp_free_ptr(fpst);
5557 tcg_temp_free_i32(tcg_op);
5558 tcg_temp_free_i32(tcg_res);
5561 /* Floating-point data-processing (1 source) - double precision */
5562 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
5564 TCGv_ptr fpst;
5565 TCGv_i64 tcg_op;
5566 TCGv_i64 tcg_res;
5568 switch (opcode) {
5569 case 0x0: /* FMOV */
5570 gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
5571 return;
5574 fpst = get_fpstatus_ptr(false);
5575 tcg_op = read_fp_dreg(s, rn);
5576 tcg_res = tcg_temp_new_i64();
5578 switch (opcode) {
5579 case 0x1: /* FABS */
5580 gen_helper_vfp_absd(tcg_res, tcg_op);
5581 break;
5582 case 0x2: /* FNEG */
5583 gen_helper_vfp_negd(tcg_res, tcg_op);
5584 break;
5585 case 0x3: /* FSQRT */
5586 gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
5587 break;
5588 case 0x8: /* FRINTN */
5589 case 0x9: /* FRINTP */
5590 case 0xa: /* FRINTM */
5591 case 0xb: /* FRINTZ */
5592 case 0xc: /* FRINTA */
5594 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
5596 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5597 gen_helper_rintd(tcg_res, tcg_op, fpst);
5599 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5600 tcg_temp_free_i32(tcg_rmode);
5601 break;
5603 case 0xe: /* FRINTX */
5604 gen_helper_rintd_exact(tcg_res, tcg_op, fpst);
5605 break;
5606 case 0xf: /* FRINTI */
5607 gen_helper_rintd(tcg_res, tcg_op, fpst);
5608 break;
5609 default:
5610 abort();
5613 write_fp_dreg(s, rd, tcg_res);
5615 tcg_temp_free_ptr(fpst);
5616 tcg_temp_free_i64(tcg_op);
5617 tcg_temp_free_i64(tcg_res);
5620 static void handle_fp_fcvt(DisasContext *s, int opcode,
5621 int rd, int rn, int dtype, int ntype)
5623 switch (ntype) {
5624 case 0x0:
5626 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
5627 if (dtype == 1) {
5628 /* Single to double */
5629 TCGv_i64 tcg_rd = tcg_temp_new_i64();
5630 gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
5631 write_fp_dreg(s, rd, tcg_rd);
5632 tcg_temp_free_i64(tcg_rd);
5633 } else {
5634 /* Single to half */
5635 TCGv_i32 tcg_rd = tcg_temp_new_i32();
5636 TCGv_i32 ahp = get_ahp_flag();
5637 TCGv_ptr fpst = get_fpstatus_ptr(false);
5639 gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
5640 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
5641 write_fp_sreg(s, rd, tcg_rd);
5642 tcg_temp_free_i32(tcg_rd);
5643 tcg_temp_free_i32(ahp);
5644 tcg_temp_free_ptr(fpst);
5646 tcg_temp_free_i32(tcg_rn);
5647 break;
5649 case 0x1:
5651 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
5652 TCGv_i32 tcg_rd = tcg_temp_new_i32();
5653 if (dtype == 0) {
5654 /* Double to single */
5655 gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
5656 } else {
5657 TCGv_ptr fpst = get_fpstatus_ptr(false);
5658 TCGv_i32 ahp = get_ahp_flag();
5659 /* Double to half */
5660 gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
5661 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
5662 tcg_temp_free_ptr(fpst);
5663 tcg_temp_free_i32(ahp);
5665 write_fp_sreg(s, rd, tcg_rd);
5666 tcg_temp_free_i32(tcg_rd);
5667 tcg_temp_free_i64(tcg_rn);
5668 break;
5670 case 0x3:
5672 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
5673 TCGv_ptr tcg_fpst = get_fpstatus_ptr(false);
5674 TCGv_i32 tcg_ahp = get_ahp_flag();
5675 tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
5676 if (dtype == 0) {
5677 /* Half to single */
5678 TCGv_i32 tcg_rd = tcg_temp_new_i32();
5679 gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
5680 write_fp_sreg(s, rd, tcg_rd);
5681 tcg_temp_free_ptr(tcg_fpst);
5682 tcg_temp_free_i32(tcg_ahp);
5683 tcg_temp_free_i32(tcg_rd);
5684 } else {
5685 /* Half to double */
5686 TCGv_i64 tcg_rd = tcg_temp_new_i64();
5687 gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
5688 write_fp_dreg(s, rd, tcg_rd);
5689 tcg_temp_free_i64(tcg_rd);
5691 tcg_temp_free_i32(tcg_rn);
5692 break;
5694 default:
5695 abort();
5699 /* Floating point data-processing (1 source)
5700 * 31 30 29 28 24 23 22 21 20 15 14 10 9 5 4 0
5701 * +---+---+---+-----------+------+---+--------+-----------+------+------+
5702 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 | Rn | Rd |
5703 * +---+---+---+-----------+------+---+--------+-----------+------+------+
5705 static void disas_fp_1src(DisasContext *s, uint32_t insn)
5707 int mos = extract32(insn, 29, 3);
5708 int type = extract32(insn, 22, 2);
5709 int opcode = extract32(insn, 15, 6);
5710 int rn = extract32(insn, 5, 5);
5711 int rd = extract32(insn, 0, 5);
5713 if (mos) {
5714 unallocated_encoding(s);
5715 return;
5718 switch (opcode) {
5719 case 0x4: case 0x5: case 0x7:
5721 /* FCVT between half, single and double precision */
5722 int dtype = extract32(opcode, 0, 2);
5723 if (type == 2 || dtype == type) {
5724 unallocated_encoding(s);
5725 return;
5727 if (!fp_access_check(s)) {
5728 return;
5731 handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
5732 break;
5734 case 0x0 ... 0x3:
5735 case 0x8 ... 0xc:
5736 case 0xe ... 0xf:
5737 /* 32-to-32 and 64-to-64 ops */
5738 switch (type) {
5739 case 0:
5740 if (!fp_access_check(s)) {
5741 return;
5744 handle_fp_1src_single(s, opcode, rd, rn);
5745 break;
5746 case 1:
5747 if (!fp_access_check(s)) {
5748 return;
5751 handle_fp_1src_double(s, opcode, rd, rn);
5752 break;
5753 case 3:
5754 if (!dc_isar_feature(aa64_fp16, s)) {
5755 unallocated_encoding(s);
5756 return;
5759 if (!fp_access_check(s)) {
5760 return;
5763 handle_fp_1src_half(s, opcode, rd, rn);
5764 break;
5765 default:
5766 unallocated_encoding(s);
5768 break;
5769 default:
5770 unallocated_encoding(s);
5771 break;
5775 /* Floating-point data-processing (2 source) - single precision */
5776 static void handle_fp_2src_single(DisasContext *s, int opcode,
5777 int rd, int rn, int rm)
5779 TCGv_i32 tcg_op1;
5780 TCGv_i32 tcg_op2;
5781 TCGv_i32 tcg_res;
5782 TCGv_ptr fpst;
5784 tcg_res = tcg_temp_new_i32();
5785 fpst = get_fpstatus_ptr(false);
5786 tcg_op1 = read_fp_sreg(s, rn);
5787 tcg_op2 = read_fp_sreg(s, rm);
5789 switch (opcode) {
5790 case 0x0: /* FMUL */
5791 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
5792 break;
5793 case 0x1: /* FDIV */
5794 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
5795 break;
5796 case 0x2: /* FADD */
5797 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
5798 break;
5799 case 0x3: /* FSUB */
5800 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
5801 break;
5802 case 0x4: /* FMAX */
5803 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
5804 break;
5805 case 0x5: /* FMIN */
5806 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
5807 break;
5808 case 0x6: /* FMAXNM */
5809 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
5810 break;
5811 case 0x7: /* FMINNM */
5812 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
5813 break;
5814 case 0x8: /* FNMUL */
5815 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
5816 gen_helper_vfp_negs(tcg_res, tcg_res);
5817 break;
5820 write_fp_sreg(s, rd, tcg_res);
5822 tcg_temp_free_ptr(fpst);
5823 tcg_temp_free_i32(tcg_op1);
5824 tcg_temp_free_i32(tcg_op2);
5825 tcg_temp_free_i32(tcg_res);
5828 /* Floating-point data-processing (2 source) - double precision */
5829 static void handle_fp_2src_double(DisasContext *s, int opcode,
5830 int rd, int rn, int rm)
5832 TCGv_i64 tcg_op1;
5833 TCGv_i64 tcg_op2;
5834 TCGv_i64 tcg_res;
5835 TCGv_ptr fpst;
5837 tcg_res = tcg_temp_new_i64();
5838 fpst = get_fpstatus_ptr(false);
5839 tcg_op1 = read_fp_dreg(s, rn);
5840 tcg_op2 = read_fp_dreg(s, rm);
5842 switch (opcode) {
5843 case 0x0: /* FMUL */
5844 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
5845 break;
5846 case 0x1: /* FDIV */
5847 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
5848 break;
5849 case 0x2: /* FADD */
5850 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
5851 break;
5852 case 0x3: /* FSUB */
5853 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
5854 break;
5855 case 0x4: /* FMAX */
5856 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
5857 break;
5858 case 0x5: /* FMIN */
5859 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
5860 break;
5861 case 0x6: /* FMAXNM */
5862 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
5863 break;
5864 case 0x7: /* FMINNM */
5865 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
5866 break;
5867 case 0x8: /* FNMUL */
5868 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
5869 gen_helper_vfp_negd(tcg_res, tcg_res);
5870 break;
5873 write_fp_dreg(s, rd, tcg_res);
5875 tcg_temp_free_ptr(fpst);
5876 tcg_temp_free_i64(tcg_op1);
5877 tcg_temp_free_i64(tcg_op2);
5878 tcg_temp_free_i64(tcg_res);
5881 /* Floating-point data-processing (2 source) - half precision */
5882 static void handle_fp_2src_half(DisasContext *s, int opcode,
5883 int rd, int rn, int rm)
5885 TCGv_i32 tcg_op1;
5886 TCGv_i32 tcg_op2;
5887 TCGv_i32 tcg_res;
5888 TCGv_ptr fpst;
5890 tcg_res = tcg_temp_new_i32();
5891 fpst = get_fpstatus_ptr(true);
5892 tcg_op1 = read_fp_hreg(s, rn);
5893 tcg_op2 = read_fp_hreg(s, rm);
5895 switch (opcode) {
5896 case 0x0: /* FMUL */
5897 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
5898 break;
5899 case 0x1: /* FDIV */
5900 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
5901 break;
5902 case 0x2: /* FADD */
5903 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
5904 break;
5905 case 0x3: /* FSUB */
5906 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
5907 break;
5908 case 0x4: /* FMAX */
5909 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
5910 break;
5911 case 0x5: /* FMIN */
5912 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
5913 break;
5914 case 0x6: /* FMAXNM */
5915 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
5916 break;
5917 case 0x7: /* FMINNM */
5918 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
5919 break;
5920 case 0x8: /* FNMUL */
5921 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
5922 tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
5923 break;
5924 default:
5925 g_assert_not_reached();
5928 write_fp_sreg(s, rd, tcg_res);
5930 tcg_temp_free_ptr(fpst);
5931 tcg_temp_free_i32(tcg_op1);
5932 tcg_temp_free_i32(tcg_op2);
5933 tcg_temp_free_i32(tcg_res);
5936 /* Floating point data-processing (2 source)
5937 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
5938 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
5939 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | opcode | 1 0 | Rn | Rd |
5940 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
5942 static void disas_fp_2src(DisasContext *s, uint32_t insn)
5944 int mos = extract32(insn, 29, 3);
5945 int type = extract32(insn, 22, 2);
5946 int rd = extract32(insn, 0, 5);
5947 int rn = extract32(insn, 5, 5);
5948 int rm = extract32(insn, 16, 5);
5949 int opcode = extract32(insn, 12, 4);
5951 if (opcode > 8 || mos) {
5952 unallocated_encoding(s);
5953 return;
5956 switch (type) {
5957 case 0:
5958 if (!fp_access_check(s)) {
5959 return;
5961 handle_fp_2src_single(s, opcode, rd, rn, rm);
5962 break;
5963 case 1:
5964 if (!fp_access_check(s)) {
5965 return;
5967 handle_fp_2src_double(s, opcode, rd, rn, rm);
5968 break;
5969 case 3:
5970 if (!dc_isar_feature(aa64_fp16, s)) {
5971 unallocated_encoding(s);
5972 return;
5974 if (!fp_access_check(s)) {
5975 return;
5977 handle_fp_2src_half(s, opcode, rd, rn, rm);
5978 break;
5979 default:
5980 unallocated_encoding(s);
5984 /* Floating-point data-processing (3 source) - single precision */
5985 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
5986 int rd, int rn, int rm, int ra)
5988 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
5989 TCGv_i32 tcg_res = tcg_temp_new_i32();
5990 TCGv_ptr fpst = get_fpstatus_ptr(false);
5992 tcg_op1 = read_fp_sreg(s, rn);
5993 tcg_op2 = read_fp_sreg(s, rm);
5994 tcg_op3 = read_fp_sreg(s, ra);
5996 /* These are fused multiply-add, and must be done as one
5997 * floating point operation with no rounding between the
5998 * multiplication and addition steps.
5999 * NB that doing the negations here as separate steps is
6000 * correct : an input NaN should come out with its sign bit
6001 * flipped if it is a negated-input.
6003 if (o1 == true) {
6004 gen_helper_vfp_negs(tcg_op3, tcg_op3);
6007 if (o0 != o1) {
6008 gen_helper_vfp_negs(tcg_op1, tcg_op1);
6011 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6013 write_fp_sreg(s, rd, tcg_res);
6015 tcg_temp_free_ptr(fpst);
6016 tcg_temp_free_i32(tcg_op1);
6017 tcg_temp_free_i32(tcg_op2);
6018 tcg_temp_free_i32(tcg_op3);
6019 tcg_temp_free_i32(tcg_res);
6022 /* Floating-point data-processing (3 source) - double precision */
6023 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6024 int rd, int rn, int rm, int ra)
6026 TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6027 TCGv_i64 tcg_res = tcg_temp_new_i64();
6028 TCGv_ptr fpst = get_fpstatus_ptr(false);
6030 tcg_op1 = read_fp_dreg(s, rn);
6031 tcg_op2 = read_fp_dreg(s, rm);
6032 tcg_op3 = read_fp_dreg(s, ra);
6034 /* These are fused multiply-add, and must be done as one
6035 * floating point operation with no rounding between the
6036 * multiplication and addition steps.
6037 * NB that doing the negations here as separate steps is
6038 * correct : an input NaN should come out with its sign bit
6039 * flipped if it is a negated-input.
6041 if (o1 == true) {
6042 gen_helper_vfp_negd(tcg_op3, tcg_op3);
6045 if (o0 != o1) {
6046 gen_helper_vfp_negd(tcg_op1, tcg_op1);
6049 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6051 write_fp_dreg(s, rd, tcg_res);
6053 tcg_temp_free_ptr(fpst);
6054 tcg_temp_free_i64(tcg_op1);
6055 tcg_temp_free_i64(tcg_op2);
6056 tcg_temp_free_i64(tcg_op3);
6057 tcg_temp_free_i64(tcg_res);
6060 /* Floating-point data-processing (3 source) - half precision */
6061 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6062 int rd, int rn, int rm, int ra)
6064 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6065 TCGv_i32 tcg_res = tcg_temp_new_i32();
6066 TCGv_ptr fpst = get_fpstatus_ptr(true);
6068 tcg_op1 = read_fp_hreg(s, rn);
6069 tcg_op2 = read_fp_hreg(s, rm);
6070 tcg_op3 = read_fp_hreg(s, ra);
6072 /* These are fused multiply-add, and must be done as one
6073 * floating point operation with no rounding between the
6074 * multiplication and addition steps.
6075 * NB that doing the negations here as separate steps is
6076 * correct : an input NaN should come out with its sign bit
6077 * flipped if it is a negated-input.
6079 if (o1 == true) {
6080 tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6083 if (o0 != o1) {
6084 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6087 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6089 write_fp_sreg(s, rd, tcg_res);
6091 tcg_temp_free_ptr(fpst);
6092 tcg_temp_free_i32(tcg_op1);
6093 tcg_temp_free_i32(tcg_op2);
6094 tcg_temp_free_i32(tcg_op3);
6095 tcg_temp_free_i32(tcg_res);
6098 /* Floating point data-processing (3 source)
6099 * 31 30 29 28 24 23 22 21 20 16 15 14 10 9 5 4 0
6100 * +---+---+---+-----------+------+----+------+----+------+------+------+
6101 * | M | 0 | S | 1 1 1 1 1 | type | o1 | Rm | o0 | Ra | Rn | Rd |
6102 * +---+---+---+-----------+------+----+------+----+------+------+------+
6104 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6106 int mos = extract32(insn, 29, 3);
6107 int type = extract32(insn, 22, 2);
6108 int rd = extract32(insn, 0, 5);
6109 int rn = extract32(insn, 5, 5);
6110 int ra = extract32(insn, 10, 5);
6111 int rm = extract32(insn, 16, 5);
6112 bool o0 = extract32(insn, 15, 1);
6113 bool o1 = extract32(insn, 21, 1);
6115 if (mos) {
6116 unallocated_encoding(s);
6117 return;
6120 switch (type) {
6121 case 0:
6122 if (!fp_access_check(s)) {
6123 return;
6125 handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6126 break;
6127 case 1:
6128 if (!fp_access_check(s)) {
6129 return;
6131 handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6132 break;
6133 case 3:
6134 if (!dc_isar_feature(aa64_fp16, s)) {
6135 unallocated_encoding(s);
6136 return;
6138 if (!fp_access_check(s)) {
6139 return;
6141 handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6142 break;
6143 default:
6144 unallocated_encoding(s);
6148 /* The imm8 encodes the sign bit, enough bits to represent an exponent in
6149 * the range 01....1xx to 10....0xx, and the most significant 4 bits of
6150 * the mantissa; see VFPExpandImm() in the v8 ARM ARM.
6152 uint64_t vfp_expand_imm(int size, uint8_t imm8)
6154 uint64_t imm;
6156 switch (size) {
6157 case MO_64:
6158 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
6159 (extract32(imm8, 6, 1) ? 0x3fc0 : 0x4000) |
6160 extract32(imm8, 0, 6);
6161 imm <<= 48;
6162 break;
6163 case MO_32:
6164 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
6165 (extract32(imm8, 6, 1) ? 0x3e00 : 0x4000) |
6166 (extract32(imm8, 0, 6) << 3);
6167 imm <<= 16;
6168 break;
6169 case MO_16:
6170 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
6171 (extract32(imm8, 6, 1) ? 0x3000 : 0x4000) |
6172 (extract32(imm8, 0, 6) << 6);
6173 break;
6174 default:
6175 g_assert_not_reached();
6177 return imm;
6180 /* Floating point immediate
6181 * 31 30 29 28 24 23 22 21 20 13 12 10 9 5 4 0
6182 * +---+---+---+-----------+------+---+------------+-------+------+------+
6183 * | M | 0 | S | 1 1 1 1 0 | type | 1 | imm8 | 1 0 0 | imm5 | Rd |
6184 * +---+---+---+-----------+------+---+------------+-------+------+------+
6186 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6188 int rd = extract32(insn, 0, 5);
6189 int imm5 = extract32(insn, 5, 5);
6190 int imm8 = extract32(insn, 13, 8);
6191 int type = extract32(insn, 22, 2);
6192 int mos = extract32(insn, 29, 3);
6193 uint64_t imm;
6194 TCGv_i64 tcg_res;
6195 TCGMemOp sz;
6197 if (mos || imm5) {
6198 unallocated_encoding(s);
6199 return;
6202 switch (type) {
6203 case 0:
6204 sz = MO_32;
6205 break;
6206 case 1:
6207 sz = MO_64;
6208 break;
6209 case 3:
6210 sz = MO_16;
6211 if (dc_isar_feature(aa64_fp16, s)) {
6212 break;
6214 /* fallthru */
6215 default:
6216 unallocated_encoding(s);
6217 return;
6220 if (!fp_access_check(s)) {
6221 return;
6224 imm = vfp_expand_imm(sz, imm8);
6226 tcg_res = tcg_const_i64(imm);
6227 write_fp_dreg(s, rd, tcg_res);
6228 tcg_temp_free_i64(tcg_res);
6231 /* Handle floating point <=> fixed point conversions. Note that we can
6232 * also deal with fp <=> integer conversions as a special case (scale == 64)
6233 * OPTME: consider handling that special case specially or at least skipping
6234 * the call to scalbn in the helpers for zero shifts.
6236 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6237 bool itof, int rmode, int scale, int sf, int type)
6239 bool is_signed = !(opcode & 1);
6240 TCGv_ptr tcg_fpstatus;
6241 TCGv_i32 tcg_shift, tcg_single;
6242 TCGv_i64 tcg_double;
6244 tcg_fpstatus = get_fpstatus_ptr(type == 3);
6246 tcg_shift = tcg_const_i32(64 - scale);
6248 if (itof) {
6249 TCGv_i64 tcg_int = cpu_reg(s, rn);
6250 if (!sf) {
6251 TCGv_i64 tcg_extend = new_tmp_a64(s);
6253 if (is_signed) {
6254 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6255 } else {
6256 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6259 tcg_int = tcg_extend;
6262 switch (type) {
6263 case 1: /* float64 */
6264 tcg_double = tcg_temp_new_i64();
6265 if (is_signed) {
6266 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6267 tcg_shift, tcg_fpstatus);
6268 } else {
6269 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6270 tcg_shift, tcg_fpstatus);
6272 write_fp_dreg(s, rd, tcg_double);
6273 tcg_temp_free_i64(tcg_double);
6274 break;
6276 case 0: /* float32 */
6277 tcg_single = tcg_temp_new_i32();
6278 if (is_signed) {
6279 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6280 tcg_shift, tcg_fpstatus);
6281 } else {
6282 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6283 tcg_shift, tcg_fpstatus);
6285 write_fp_sreg(s, rd, tcg_single);
6286 tcg_temp_free_i32(tcg_single);
6287 break;
6289 case 3: /* float16 */
6290 tcg_single = tcg_temp_new_i32();
6291 if (is_signed) {
6292 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6293 tcg_shift, tcg_fpstatus);
6294 } else {
6295 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6296 tcg_shift, tcg_fpstatus);
6298 write_fp_sreg(s, rd, tcg_single);
6299 tcg_temp_free_i32(tcg_single);
6300 break;
6302 default:
6303 g_assert_not_reached();
6305 } else {
6306 TCGv_i64 tcg_int = cpu_reg(s, rd);
6307 TCGv_i32 tcg_rmode;
6309 if (extract32(opcode, 2, 1)) {
6310 /* There are too many rounding modes to all fit into rmode,
6311 * so FCVTA[US] is a special case.
6313 rmode = FPROUNDING_TIEAWAY;
6316 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
6318 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
6320 switch (type) {
6321 case 1: /* float64 */
6322 tcg_double = read_fp_dreg(s, rn);
6323 if (is_signed) {
6324 if (!sf) {
6325 gen_helper_vfp_tosld(tcg_int, tcg_double,
6326 tcg_shift, tcg_fpstatus);
6327 } else {
6328 gen_helper_vfp_tosqd(tcg_int, tcg_double,
6329 tcg_shift, tcg_fpstatus);
6331 } else {
6332 if (!sf) {
6333 gen_helper_vfp_tould(tcg_int, tcg_double,
6334 tcg_shift, tcg_fpstatus);
6335 } else {
6336 gen_helper_vfp_touqd(tcg_int, tcg_double,
6337 tcg_shift, tcg_fpstatus);
6340 if (!sf) {
6341 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6343 tcg_temp_free_i64(tcg_double);
6344 break;
6346 case 0: /* float32 */
6347 tcg_single = read_fp_sreg(s, rn);
6348 if (sf) {
6349 if (is_signed) {
6350 gen_helper_vfp_tosqs(tcg_int, tcg_single,
6351 tcg_shift, tcg_fpstatus);
6352 } else {
6353 gen_helper_vfp_touqs(tcg_int, tcg_single,
6354 tcg_shift, tcg_fpstatus);
6356 } else {
6357 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6358 if (is_signed) {
6359 gen_helper_vfp_tosls(tcg_dest, tcg_single,
6360 tcg_shift, tcg_fpstatus);
6361 } else {
6362 gen_helper_vfp_touls(tcg_dest, tcg_single,
6363 tcg_shift, tcg_fpstatus);
6365 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6366 tcg_temp_free_i32(tcg_dest);
6368 tcg_temp_free_i32(tcg_single);
6369 break;
6371 case 3: /* float16 */
6372 tcg_single = read_fp_sreg(s, rn);
6373 if (sf) {
6374 if (is_signed) {
6375 gen_helper_vfp_tosqh(tcg_int, tcg_single,
6376 tcg_shift, tcg_fpstatus);
6377 } else {
6378 gen_helper_vfp_touqh(tcg_int, tcg_single,
6379 tcg_shift, tcg_fpstatus);
6381 } else {
6382 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6383 if (is_signed) {
6384 gen_helper_vfp_toslh(tcg_dest, tcg_single,
6385 tcg_shift, tcg_fpstatus);
6386 } else {
6387 gen_helper_vfp_toulh(tcg_dest, tcg_single,
6388 tcg_shift, tcg_fpstatus);
6390 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6391 tcg_temp_free_i32(tcg_dest);
6393 tcg_temp_free_i32(tcg_single);
6394 break;
6396 default:
6397 g_assert_not_reached();
6400 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
6401 tcg_temp_free_i32(tcg_rmode);
6404 tcg_temp_free_ptr(tcg_fpstatus);
6405 tcg_temp_free_i32(tcg_shift);
6408 /* Floating point <-> fixed point conversions
6409 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
6410 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6411 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale | Rn | Rd |
6412 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6414 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
6416 int rd = extract32(insn, 0, 5);
6417 int rn = extract32(insn, 5, 5);
6418 int scale = extract32(insn, 10, 6);
6419 int opcode = extract32(insn, 16, 3);
6420 int rmode = extract32(insn, 19, 2);
6421 int type = extract32(insn, 22, 2);
6422 bool sbit = extract32(insn, 29, 1);
6423 bool sf = extract32(insn, 31, 1);
6424 bool itof;
6426 if (sbit || (!sf && scale < 32)) {
6427 unallocated_encoding(s);
6428 return;
6431 switch (type) {
6432 case 0: /* float32 */
6433 case 1: /* float64 */
6434 break;
6435 case 3: /* float16 */
6436 if (dc_isar_feature(aa64_fp16, s)) {
6437 break;
6439 /* fallthru */
6440 default:
6441 unallocated_encoding(s);
6442 return;
6445 switch ((rmode << 3) | opcode) {
6446 case 0x2: /* SCVTF */
6447 case 0x3: /* UCVTF */
6448 itof = true;
6449 break;
6450 case 0x18: /* FCVTZS */
6451 case 0x19: /* FCVTZU */
6452 itof = false;
6453 break;
6454 default:
6455 unallocated_encoding(s);
6456 return;
6459 if (!fp_access_check(s)) {
6460 return;
6463 handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
6466 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
6468 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
6469 * without conversion.
6472 if (itof) {
6473 TCGv_i64 tcg_rn = cpu_reg(s, rn);
6474 TCGv_i64 tmp;
6476 switch (type) {
6477 case 0:
6478 /* 32 bit */
6479 tmp = tcg_temp_new_i64();
6480 tcg_gen_ext32u_i64(tmp, tcg_rn);
6481 write_fp_dreg(s, rd, tmp);
6482 tcg_temp_free_i64(tmp);
6483 break;
6484 case 1:
6485 /* 64 bit */
6486 write_fp_dreg(s, rd, tcg_rn);
6487 break;
6488 case 2:
6489 /* 64 bit to top half. */
6490 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
6491 clear_vec_high(s, true, rd);
6492 break;
6493 case 3:
6494 /* 16 bit */
6495 tmp = tcg_temp_new_i64();
6496 tcg_gen_ext16u_i64(tmp, tcg_rn);
6497 write_fp_dreg(s, rd, tmp);
6498 tcg_temp_free_i64(tmp);
6499 break;
6500 default:
6501 g_assert_not_reached();
6503 } else {
6504 TCGv_i64 tcg_rd = cpu_reg(s, rd);
6506 switch (type) {
6507 case 0:
6508 /* 32 bit */
6509 tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
6510 break;
6511 case 1:
6512 /* 64 bit */
6513 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
6514 break;
6515 case 2:
6516 /* 64 bits from top half */
6517 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
6518 break;
6519 case 3:
6520 /* 16 bit */
6521 tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
6522 break;
6523 default:
6524 g_assert_not_reached();
6529 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
6531 TCGv_i64 t = read_fp_dreg(s, rn);
6532 TCGv_ptr fpstatus = get_fpstatus_ptr(false);
6534 gen_helper_fjcvtzs(t, t, fpstatus);
6536 tcg_temp_free_ptr(fpstatus);
6538 tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
6539 tcg_gen_extrh_i64_i32(cpu_ZF, t);
6540 tcg_gen_movi_i32(cpu_CF, 0);
6541 tcg_gen_movi_i32(cpu_NF, 0);
6542 tcg_gen_movi_i32(cpu_VF, 0);
6544 tcg_temp_free_i64(t);
6547 /* Floating point <-> integer conversions
6548 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
6549 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
6550 * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
6551 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
6553 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
6555 int rd = extract32(insn, 0, 5);
6556 int rn = extract32(insn, 5, 5);
6557 int opcode = extract32(insn, 16, 3);
6558 int rmode = extract32(insn, 19, 2);
6559 int type = extract32(insn, 22, 2);
6560 bool sbit = extract32(insn, 29, 1);
6561 bool sf = extract32(insn, 31, 1);
6562 bool itof = false;
6564 if (sbit) {
6565 goto do_unallocated;
6568 switch (opcode) {
6569 case 2: /* SCVTF */
6570 case 3: /* UCVTF */
6571 itof = true;
6572 /* fallthru */
6573 case 4: /* FCVTAS */
6574 case 5: /* FCVTAU */
6575 if (rmode != 0) {
6576 goto do_unallocated;
6578 /* fallthru */
6579 case 0: /* FCVT[NPMZ]S */
6580 case 1: /* FCVT[NPMZ]U */
6581 switch (type) {
6582 case 0: /* float32 */
6583 case 1: /* float64 */
6584 break;
6585 case 3: /* float16 */
6586 if (!dc_isar_feature(aa64_fp16, s)) {
6587 goto do_unallocated;
6589 break;
6590 default:
6591 goto do_unallocated;
6593 if (!fp_access_check(s)) {
6594 return;
6596 handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
6597 break;
6599 default:
6600 switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
6601 case 0b01100110: /* FMOV half <-> 32-bit int */
6602 case 0b01100111:
6603 case 0b11100110: /* FMOV half <-> 64-bit int */
6604 case 0b11100111:
6605 if (!dc_isar_feature(aa64_fp16, s)) {
6606 goto do_unallocated;
6608 /* fallthru */
6609 case 0b00000110: /* FMOV 32-bit */
6610 case 0b00000111:
6611 case 0b10100110: /* FMOV 64-bit */
6612 case 0b10100111:
6613 case 0b11001110: /* FMOV top half of 128-bit */
6614 case 0b11001111:
6615 if (!fp_access_check(s)) {
6616 return;
6618 itof = opcode & 1;
6619 handle_fmov(s, rd, rn, type, itof);
6620 break;
6622 case 0b00111110: /* FJCVTZS */
6623 if (!dc_isar_feature(aa64_jscvt, s)) {
6624 goto do_unallocated;
6625 } else if (fp_access_check(s)) {
6626 handle_fjcvtzs(s, rd, rn);
6628 break;
6630 default:
6631 do_unallocated:
6632 unallocated_encoding(s);
6633 return;
6635 break;
6639 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
6640 * 31 30 29 28 25 24 0
6641 * +---+---+---+---------+-----------------------------+
6642 * | | 0 | | 1 1 1 1 | |
6643 * +---+---+---+---------+-----------------------------+
6645 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
6647 if (extract32(insn, 24, 1)) {
6648 /* Floating point data-processing (3 source) */
6649 disas_fp_3src(s, insn);
6650 } else if (extract32(insn, 21, 1) == 0) {
6651 /* Floating point to fixed point conversions */
6652 disas_fp_fixed_conv(s, insn);
6653 } else {
6654 switch (extract32(insn, 10, 2)) {
6655 case 1:
6656 /* Floating point conditional compare */
6657 disas_fp_ccomp(s, insn);
6658 break;
6659 case 2:
6660 /* Floating point data-processing (2 source) */
6661 disas_fp_2src(s, insn);
6662 break;
6663 case 3:
6664 /* Floating point conditional select */
6665 disas_fp_csel(s, insn);
6666 break;
6667 case 0:
6668 switch (ctz32(extract32(insn, 12, 4))) {
6669 case 0: /* [15:12] == xxx1 */
6670 /* Floating point immediate */
6671 disas_fp_imm(s, insn);
6672 break;
6673 case 1: /* [15:12] == xx10 */
6674 /* Floating point compare */
6675 disas_fp_compare(s, insn);
6676 break;
6677 case 2: /* [15:12] == x100 */
6678 /* Floating point data-processing (1 source) */
6679 disas_fp_1src(s, insn);
6680 break;
6681 case 3: /* [15:12] == 1000 */
6682 unallocated_encoding(s);
6683 break;
6684 default: /* [15:12] == 0000 */
6685 /* Floating point <-> integer conversions */
6686 disas_fp_int_conv(s, insn);
6687 break;
6689 break;
6694 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
6695 int pos)
6697 /* Extract 64 bits from the middle of two concatenated 64 bit
6698 * vector register slices left:right. The extracted bits start
6699 * at 'pos' bits into the right (least significant) side.
6700 * We return the result in tcg_right, and guarantee not to
6701 * trash tcg_left.
6703 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
6704 assert(pos > 0 && pos < 64);
6706 tcg_gen_shri_i64(tcg_right, tcg_right, pos);
6707 tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
6708 tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
6710 tcg_temp_free_i64(tcg_tmp);
6713 /* EXT
6714 * 31 30 29 24 23 22 21 20 16 15 14 11 10 9 5 4 0
6715 * +---+---+-------------+-----+---+------+---+------+---+------+------+
6716 * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 | Rm | 0 | imm4 | 0 | Rn | Rd |
6717 * +---+---+-------------+-----+---+------+---+------+---+------+------+
6719 static void disas_simd_ext(DisasContext *s, uint32_t insn)
6721 int is_q = extract32(insn, 30, 1);
6722 int op2 = extract32(insn, 22, 2);
6723 int imm4 = extract32(insn, 11, 4);
6724 int rm = extract32(insn, 16, 5);
6725 int rn = extract32(insn, 5, 5);
6726 int rd = extract32(insn, 0, 5);
6727 int pos = imm4 << 3;
6728 TCGv_i64 tcg_resl, tcg_resh;
6730 if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
6731 unallocated_encoding(s);
6732 return;
6735 if (!fp_access_check(s)) {
6736 return;
6739 tcg_resh = tcg_temp_new_i64();
6740 tcg_resl = tcg_temp_new_i64();
6742 /* Vd gets bits starting at pos bits into Vm:Vn. This is
6743 * either extracting 128 bits from a 128:128 concatenation, or
6744 * extracting 64 bits from a 64:64 concatenation.
6746 if (!is_q) {
6747 read_vec_element(s, tcg_resl, rn, 0, MO_64);
6748 if (pos != 0) {
6749 read_vec_element(s, tcg_resh, rm, 0, MO_64);
6750 do_ext64(s, tcg_resh, tcg_resl, pos);
6752 tcg_gen_movi_i64(tcg_resh, 0);
6753 } else {
6754 TCGv_i64 tcg_hh;
6755 typedef struct {
6756 int reg;
6757 int elt;
6758 } EltPosns;
6759 EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
6760 EltPosns *elt = eltposns;
6762 if (pos >= 64) {
6763 elt++;
6764 pos -= 64;
6767 read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
6768 elt++;
6769 read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
6770 elt++;
6771 if (pos != 0) {
6772 do_ext64(s, tcg_resh, tcg_resl, pos);
6773 tcg_hh = tcg_temp_new_i64();
6774 read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
6775 do_ext64(s, tcg_hh, tcg_resh, pos);
6776 tcg_temp_free_i64(tcg_hh);
6780 write_vec_element(s, tcg_resl, rd, 0, MO_64);
6781 tcg_temp_free_i64(tcg_resl);
6782 write_vec_element(s, tcg_resh, rd, 1, MO_64);
6783 tcg_temp_free_i64(tcg_resh);
6786 /* TBL/TBX
6787 * 31 30 29 24 23 22 21 20 16 15 14 13 12 11 10 9 5 4 0
6788 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
6789 * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 | Rm | 0 | len | op | 0 0 | Rn | Rd |
6790 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
6792 static void disas_simd_tb(DisasContext *s, uint32_t insn)
6794 int op2 = extract32(insn, 22, 2);
6795 int is_q = extract32(insn, 30, 1);
6796 int rm = extract32(insn, 16, 5);
6797 int rn = extract32(insn, 5, 5);
6798 int rd = extract32(insn, 0, 5);
6799 int is_tblx = extract32(insn, 12, 1);
6800 int len = extract32(insn, 13, 2);
6801 TCGv_i64 tcg_resl, tcg_resh, tcg_idx;
6802 TCGv_i32 tcg_regno, tcg_numregs;
6804 if (op2 != 0) {
6805 unallocated_encoding(s);
6806 return;
6809 if (!fp_access_check(s)) {
6810 return;
6813 /* This does a table lookup: for every byte element in the input
6814 * we index into a table formed from up to four vector registers,
6815 * and then the output is the result of the lookups. Our helper
6816 * function does the lookup operation for a single 64 bit part of
6817 * the input.
6819 tcg_resl = tcg_temp_new_i64();
6820 tcg_resh = tcg_temp_new_i64();
6822 if (is_tblx) {
6823 read_vec_element(s, tcg_resl, rd, 0, MO_64);
6824 } else {
6825 tcg_gen_movi_i64(tcg_resl, 0);
6827 if (is_tblx && is_q) {
6828 read_vec_element(s, tcg_resh, rd, 1, MO_64);
6829 } else {
6830 tcg_gen_movi_i64(tcg_resh, 0);
6833 tcg_idx = tcg_temp_new_i64();
6834 tcg_regno = tcg_const_i32(rn);
6835 tcg_numregs = tcg_const_i32(len + 1);
6836 read_vec_element(s, tcg_idx, rm, 0, MO_64);
6837 gen_helper_simd_tbl(tcg_resl, cpu_env, tcg_resl, tcg_idx,
6838 tcg_regno, tcg_numregs);
6839 if (is_q) {
6840 read_vec_element(s, tcg_idx, rm, 1, MO_64);
6841 gen_helper_simd_tbl(tcg_resh, cpu_env, tcg_resh, tcg_idx,
6842 tcg_regno, tcg_numregs);
6844 tcg_temp_free_i64(tcg_idx);
6845 tcg_temp_free_i32(tcg_regno);
6846 tcg_temp_free_i32(tcg_numregs);
6848 write_vec_element(s, tcg_resl, rd, 0, MO_64);
6849 tcg_temp_free_i64(tcg_resl);
6850 write_vec_element(s, tcg_resh, rd, 1, MO_64);
6851 tcg_temp_free_i64(tcg_resh);
6854 /* ZIP/UZP/TRN
6855 * 31 30 29 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
6856 * +---+---+-------------+------+---+------+---+------------------+------+
6857 * | 0 | Q | 0 0 1 1 1 0 | size | 0 | Rm | 0 | opc | 1 0 | Rn | Rd |
6858 * +---+---+-------------+------+---+------+---+------------------+------+
6860 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
6862 int rd = extract32(insn, 0, 5);
6863 int rn = extract32(insn, 5, 5);
6864 int rm = extract32(insn, 16, 5);
6865 int size = extract32(insn, 22, 2);
6866 /* opc field bits [1:0] indicate ZIP/UZP/TRN;
6867 * bit 2 indicates 1 vs 2 variant of the insn.
6869 int opcode = extract32(insn, 12, 2);
6870 bool part = extract32(insn, 14, 1);
6871 bool is_q = extract32(insn, 30, 1);
6872 int esize = 8 << size;
6873 int i, ofs;
6874 int datasize = is_q ? 128 : 64;
6875 int elements = datasize / esize;
6876 TCGv_i64 tcg_res, tcg_resl, tcg_resh;
6878 if (opcode == 0 || (size == 3 && !is_q)) {
6879 unallocated_encoding(s);
6880 return;
6883 if (!fp_access_check(s)) {
6884 return;
6887 tcg_resl = tcg_const_i64(0);
6888 tcg_resh = tcg_const_i64(0);
6889 tcg_res = tcg_temp_new_i64();
6891 for (i = 0; i < elements; i++) {
6892 switch (opcode) {
6893 case 1: /* UZP1/2 */
6895 int midpoint = elements / 2;
6896 if (i < midpoint) {
6897 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
6898 } else {
6899 read_vec_element(s, tcg_res, rm,
6900 2 * (i - midpoint) + part, size);
6902 break;
6904 case 2: /* TRN1/2 */
6905 if (i & 1) {
6906 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
6907 } else {
6908 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
6910 break;
6911 case 3: /* ZIP1/2 */
6913 int base = part * elements / 2;
6914 if (i & 1) {
6915 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
6916 } else {
6917 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
6919 break;
6921 default:
6922 g_assert_not_reached();
6925 ofs = i * esize;
6926 if (ofs < 64) {
6927 tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
6928 tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
6929 } else {
6930 tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
6931 tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
6935 tcg_temp_free_i64(tcg_res);
6937 write_vec_element(s, tcg_resl, rd, 0, MO_64);
6938 tcg_temp_free_i64(tcg_resl);
6939 write_vec_element(s, tcg_resh, rd, 1, MO_64);
6940 tcg_temp_free_i64(tcg_resh);
6944 * do_reduction_op helper
6946 * This mirrors the Reduce() pseudocode in the ARM ARM. It is
6947 * important for correct NaN propagation that we do these
6948 * operations in exactly the order specified by the pseudocode.
6950 * This is a recursive function, TCG temps should be freed by the
6951 * calling function once it is done with the values.
6953 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
6954 int esize, int size, int vmap, TCGv_ptr fpst)
6956 if (esize == size) {
6957 int element;
6958 TCGMemOp msize = esize == 16 ? MO_16 : MO_32;
6959 TCGv_i32 tcg_elem;
6961 /* We should have one register left here */
6962 assert(ctpop8(vmap) == 1);
6963 element = ctz32(vmap);
6964 assert(element < 8);
6966 tcg_elem = tcg_temp_new_i32();
6967 read_vec_element_i32(s, tcg_elem, rn, element, msize);
6968 return tcg_elem;
6969 } else {
6970 int bits = size / 2;
6971 int shift = ctpop8(vmap) / 2;
6972 int vmap_lo = (vmap >> shift) & vmap;
6973 int vmap_hi = (vmap & ~vmap_lo);
6974 TCGv_i32 tcg_hi, tcg_lo, tcg_res;
6976 tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
6977 tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
6978 tcg_res = tcg_temp_new_i32();
6980 switch (fpopcode) {
6981 case 0x0c: /* fmaxnmv half-precision */
6982 gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
6983 break;
6984 case 0x0f: /* fmaxv half-precision */
6985 gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
6986 break;
6987 case 0x1c: /* fminnmv half-precision */
6988 gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
6989 break;
6990 case 0x1f: /* fminv half-precision */
6991 gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
6992 break;
6993 case 0x2c: /* fmaxnmv */
6994 gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
6995 break;
6996 case 0x2f: /* fmaxv */
6997 gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
6998 break;
6999 case 0x3c: /* fminnmv */
7000 gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7001 break;
7002 case 0x3f: /* fminv */
7003 gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7004 break;
7005 default:
7006 g_assert_not_reached();
7009 tcg_temp_free_i32(tcg_hi);
7010 tcg_temp_free_i32(tcg_lo);
7011 return tcg_res;
7015 /* AdvSIMD across lanes
7016 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
7017 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7018 * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
7019 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7021 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7023 int rd = extract32(insn, 0, 5);
7024 int rn = extract32(insn, 5, 5);
7025 int size = extract32(insn, 22, 2);
7026 int opcode = extract32(insn, 12, 5);
7027 bool is_q = extract32(insn, 30, 1);
7028 bool is_u = extract32(insn, 29, 1);
7029 bool is_fp = false;
7030 bool is_min = false;
7031 int esize;
7032 int elements;
7033 int i;
7034 TCGv_i64 tcg_res, tcg_elt;
7036 switch (opcode) {
7037 case 0x1b: /* ADDV */
7038 if (is_u) {
7039 unallocated_encoding(s);
7040 return;
7042 /* fall through */
7043 case 0x3: /* SADDLV, UADDLV */
7044 case 0xa: /* SMAXV, UMAXV */
7045 case 0x1a: /* SMINV, UMINV */
7046 if (size == 3 || (size == 2 && !is_q)) {
7047 unallocated_encoding(s);
7048 return;
7050 break;
7051 case 0xc: /* FMAXNMV, FMINNMV */
7052 case 0xf: /* FMAXV, FMINV */
7053 /* Bit 1 of size field encodes min vs max and the actual size
7054 * depends on the encoding of the U bit. If not set (and FP16
7055 * enabled) then we do half-precision float instead of single
7056 * precision.
7058 is_min = extract32(size, 1, 1);
7059 is_fp = true;
7060 if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7061 size = 1;
7062 } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7063 unallocated_encoding(s);
7064 return;
7065 } else {
7066 size = 2;
7068 break;
7069 default:
7070 unallocated_encoding(s);
7071 return;
7074 if (!fp_access_check(s)) {
7075 return;
7078 esize = 8 << size;
7079 elements = (is_q ? 128 : 64) / esize;
7081 tcg_res = tcg_temp_new_i64();
7082 tcg_elt = tcg_temp_new_i64();
7084 /* These instructions operate across all lanes of a vector
7085 * to produce a single result. We can guarantee that a 64
7086 * bit intermediate is sufficient:
7087 * + for [US]ADDLV the maximum element size is 32 bits, and
7088 * the result type is 64 bits
7089 * + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7090 * same as the element size, which is 32 bits at most
7091 * For the integer operations we can choose to work at 64
7092 * or 32 bits and truncate at the end; for simplicity
7093 * we use 64 bits always. The floating point
7094 * ops do require 32 bit intermediates, though.
7096 if (!is_fp) {
7097 read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7099 for (i = 1; i < elements; i++) {
7100 read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7102 switch (opcode) {
7103 case 0x03: /* SADDLV / UADDLV */
7104 case 0x1b: /* ADDV */
7105 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7106 break;
7107 case 0x0a: /* SMAXV / UMAXV */
7108 if (is_u) {
7109 tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7110 } else {
7111 tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7113 break;
7114 case 0x1a: /* SMINV / UMINV */
7115 if (is_u) {
7116 tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7117 } else {
7118 tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7120 break;
7121 default:
7122 g_assert_not_reached();
7126 } else {
7127 /* Floating point vector reduction ops which work across 32
7128 * bit (single) or 16 bit (half-precision) intermediates.
7129 * Note that correct NaN propagation requires that we do these
7130 * operations in exactly the order specified by the pseudocode.
7132 TCGv_ptr fpst = get_fpstatus_ptr(size == MO_16);
7133 int fpopcode = opcode | is_min << 4 | is_u << 5;
7134 int vmap = (1 << elements) - 1;
7135 TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7136 (is_q ? 128 : 64), vmap, fpst);
7137 tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7138 tcg_temp_free_i32(tcg_res32);
7139 tcg_temp_free_ptr(fpst);
7142 tcg_temp_free_i64(tcg_elt);
7144 /* Now truncate the result to the width required for the final output */
7145 if (opcode == 0x03) {
7146 /* SADDLV, UADDLV: result is 2*esize */
7147 size++;
7150 switch (size) {
7151 case 0:
7152 tcg_gen_ext8u_i64(tcg_res, tcg_res);
7153 break;
7154 case 1:
7155 tcg_gen_ext16u_i64(tcg_res, tcg_res);
7156 break;
7157 case 2:
7158 tcg_gen_ext32u_i64(tcg_res, tcg_res);
7159 break;
7160 case 3:
7161 break;
7162 default:
7163 g_assert_not_reached();
7166 write_fp_dreg(s, rd, tcg_res);
7167 tcg_temp_free_i64(tcg_res);
7170 /* DUP (Element, Vector)
7172 * 31 30 29 21 20 16 15 10 9 5 4 0
7173 * +---+---+-------------------+--------+-------------+------+------+
7174 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
7175 * +---+---+-------------------+--------+-------------+------+------+
7177 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7179 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7180 int imm5)
7182 int size = ctz32(imm5);
7183 int index = imm5 >> (size + 1);
7185 if (size > 3 || (size == 3 && !is_q)) {
7186 unallocated_encoding(s);
7187 return;
7190 if (!fp_access_check(s)) {
7191 return;
7194 tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7195 vec_reg_offset(s, rn, index, size),
7196 is_q ? 16 : 8, vec_full_reg_size(s));
7199 /* DUP (element, scalar)
7200 * 31 21 20 16 15 10 9 5 4 0
7201 * +-----------------------+--------+-------------+------+------+
7202 * | 0 1 0 1 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
7203 * +-----------------------+--------+-------------+------+------+
7205 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7206 int imm5)
7208 int size = ctz32(imm5);
7209 int index;
7210 TCGv_i64 tmp;
7212 if (size > 3) {
7213 unallocated_encoding(s);
7214 return;
7217 if (!fp_access_check(s)) {
7218 return;
7221 index = imm5 >> (size + 1);
7223 /* This instruction just extracts the specified element and
7224 * zero-extends it into the bottom of the destination register.
7226 tmp = tcg_temp_new_i64();
7227 read_vec_element(s, tmp, rn, index, size);
7228 write_fp_dreg(s, rd, tmp);
7229 tcg_temp_free_i64(tmp);
7232 /* DUP (General)
7234 * 31 30 29 21 20 16 15 10 9 5 4 0
7235 * +---+---+-------------------+--------+-------------+------+------+
7236 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 1 1 | Rn | Rd |
7237 * +---+---+-------------------+--------+-------------+------+------+
7239 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7241 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7242 int imm5)
7244 int size = ctz32(imm5);
7245 uint32_t dofs, oprsz, maxsz;
7247 if (size > 3 || ((size == 3) && !is_q)) {
7248 unallocated_encoding(s);
7249 return;
7252 if (!fp_access_check(s)) {
7253 return;
7256 dofs = vec_full_reg_offset(s, rd);
7257 oprsz = is_q ? 16 : 8;
7258 maxsz = vec_full_reg_size(s);
7260 tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7263 /* INS (Element)
7265 * 31 21 20 16 15 14 11 10 9 5 4 0
7266 * +-----------------------+--------+------------+---+------+------+
7267 * | 0 1 1 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
7268 * +-----------------------+--------+------------+---+------+------+
7270 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7271 * index: encoded in imm5<4:size+1>
7273 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7274 int imm4, int imm5)
7276 int size = ctz32(imm5);
7277 int src_index, dst_index;
7278 TCGv_i64 tmp;
7280 if (size > 3) {
7281 unallocated_encoding(s);
7282 return;
7285 if (!fp_access_check(s)) {
7286 return;
7289 dst_index = extract32(imm5, 1+size, 5);
7290 src_index = extract32(imm4, size, 4);
7292 tmp = tcg_temp_new_i64();
7294 read_vec_element(s, tmp, rn, src_index, size);
7295 write_vec_element(s, tmp, rd, dst_index, size);
7297 tcg_temp_free_i64(tmp);
7301 /* INS (General)
7303 * 31 21 20 16 15 10 9 5 4 0
7304 * +-----------------------+--------+-------------+------+------+
7305 * | 0 1 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 1 1 1 | Rn | Rd |
7306 * +-----------------------+--------+-------------+------+------+
7308 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7309 * index: encoded in imm5<4:size+1>
7311 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7313 int size = ctz32(imm5);
7314 int idx;
7316 if (size > 3) {
7317 unallocated_encoding(s);
7318 return;
7321 if (!fp_access_check(s)) {
7322 return;
7325 idx = extract32(imm5, 1 + size, 4 - size);
7326 write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7330 * UMOV (General)
7331 * SMOV (General)
7333 * 31 30 29 21 20 16 15 12 10 9 5 4 0
7334 * +---+---+-------------------+--------+-------------+------+------+
7335 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 1 U 1 1 | Rn | Rd |
7336 * +---+---+-------------------+--------+-------------+------+------+
7338 * U: unsigned when set
7339 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7341 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7342 int rn, int rd, int imm5)
7344 int size = ctz32(imm5);
7345 int element;
7346 TCGv_i64 tcg_rd;
7348 /* Check for UnallocatedEncodings */
7349 if (is_signed) {
7350 if (size > 2 || (size == 2 && !is_q)) {
7351 unallocated_encoding(s);
7352 return;
7354 } else {
7355 if (size > 3
7356 || (size < 3 && is_q)
7357 || (size == 3 && !is_q)) {
7358 unallocated_encoding(s);
7359 return;
7363 if (!fp_access_check(s)) {
7364 return;
7367 element = extract32(imm5, 1+size, 4);
7369 tcg_rd = cpu_reg(s, rd);
7370 read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7371 if (is_signed && !is_q) {
7372 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7376 /* AdvSIMD copy
7377 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
7378 * +---+---+----+-----------------+------+---+------+---+------+------+
7379 * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
7380 * +---+---+----+-----------------+------+---+------+---+------+------+
7382 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7384 int rd = extract32(insn, 0, 5);
7385 int rn = extract32(insn, 5, 5);
7386 int imm4 = extract32(insn, 11, 4);
7387 int op = extract32(insn, 29, 1);
7388 int is_q = extract32(insn, 30, 1);
7389 int imm5 = extract32(insn, 16, 5);
7391 if (op) {
7392 if (is_q) {
7393 /* INS (element) */
7394 handle_simd_inse(s, rd, rn, imm4, imm5);
7395 } else {
7396 unallocated_encoding(s);
7398 } else {
7399 switch (imm4) {
7400 case 0:
7401 /* DUP (element - vector) */
7402 handle_simd_dupe(s, is_q, rd, rn, imm5);
7403 break;
7404 case 1:
7405 /* DUP (general) */
7406 handle_simd_dupg(s, is_q, rd, rn, imm5);
7407 break;
7408 case 3:
7409 if (is_q) {
7410 /* INS (general) */
7411 handle_simd_insg(s, rd, rn, imm5);
7412 } else {
7413 unallocated_encoding(s);
7415 break;
7416 case 5:
7417 case 7:
7418 /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
7419 handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
7420 break;
7421 default:
7422 unallocated_encoding(s);
7423 break;
7428 /* AdvSIMD modified immediate
7429 * 31 30 29 28 19 18 16 15 12 11 10 9 5 4 0
7430 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7431 * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh | Rd |
7432 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7434 * There are a number of operations that can be carried out here:
7435 * MOVI - move (shifted) imm into register
7436 * MVNI - move inverted (shifted) imm into register
7437 * ORR - bitwise OR of (shifted) imm with register
7438 * BIC - bitwise clear of (shifted) imm with register
7439 * With ARMv8.2 we also have:
7440 * FMOV half-precision
7442 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
7444 int rd = extract32(insn, 0, 5);
7445 int cmode = extract32(insn, 12, 4);
7446 int cmode_3_1 = extract32(cmode, 1, 3);
7447 int cmode_0 = extract32(cmode, 0, 1);
7448 int o2 = extract32(insn, 11, 1);
7449 uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
7450 bool is_neg = extract32(insn, 29, 1);
7451 bool is_q = extract32(insn, 30, 1);
7452 uint64_t imm = 0;
7454 if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
7455 /* Check for FMOV (vector, immediate) - half-precision */
7456 if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
7457 unallocated_encoding(s);
7458 return;
7462 if (!fp_access_check(s)) {
7463 return;
7466 /* See AdvSIMDExpandImm() in ARM ARM */
7467 switch (cmode_3_1) {
7468 case 0: /* Replicate(Zeros(24):imm8, 2) */
7469 case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
7470 case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
7471 case 3: /* Replicate(imm8:Zeros(24), 2) */
7473 int shift = cmode_3_1 * 8;
7474 imm = bitfield_replicate(abcdefgh << shift, 32);
7475 break;
7477 case 4: /* Replicate(Zeros(8):imm8, 4) */
7478 case 5: /* Replicate(imm8:Zeros(8), 4) */
7480 int shift = (cmode_3_1 & 0x1) * 8;
7481 imm = bitfield_replicate(abcdefgh << shift, 16);
7482 break;
7484 case 6:
7485 if (cmode_0) {
7486 /* Replicate(Zeros(8):imm8:Ones(16), 2) */
7487 imm = (abcdefgh << 16) | 0xffff;
7488 } else {
7489 /* Replicate(Zeros(16):imm8:Ones(8), 2) */
7490 imm = (abcdefgh << 8) | 0xff;
7492 imm = bitfield_replicate(imm, 32);
7493 break;
7494 case 7:
7495 if (!cmode_0 && !is_neg) {
7496 imm = bitfield_replicate(abcdefgh, 8);
7497 } else if (!cmode_0 && is_neg) {
7498 int i;
7499 imm = 0;
7500 for (i = 0; i < 8; i++) {
7501 if ((abcdefgh) & (1 << i)) {
7502 imm |= 0xffULL << (i * 8);
7505 } else if (cmode_0) {
7506 if (is_neg) {
7507 imm = (abcdefgh & 0x3f) << 48;
7508 if (abcdefgh & 0x80) {
7509 imm |= 0x8000000000000000ULL;
7511 if (abcdefgh & 0x40) {
7512 imm |= 0x3fc0000000000000ULL;
7513 } else {
7514 imm |= 0x4000000000000000ULL;
7516 } else {
7517 if (o2) {
7518 /* FMOV (vector, immediate) - half-precision */
7519 imm = vfp_expand_imm(MO_16, abcdefgh);
7520 /* now duplicate across the lanes */
7521 imm = bitfield_replicate(imm, 16);
7522 } else {
7523 imm = (abcdefgh & 0x3f) << 19;
7524 if (abcdefgh & 0x80) {
7525 imm |= 0x80000000;
7527 if (abcdefgh & 0x40) {
7528 imm |= 0x3e000000;
7529 } else {
7530 imm |= 0x40000000;
7532 imm |= (imm << 32);
7536 break;
7537 default:
7538 fprintf(stderr, "%s: cmode_3_1: %x\n", __func__, cmode_3_1);
7539 g_assert_not_reached();
7542 if (cmode_3_1 != 7 && is_neg) {
7543 imm = ~imm;
7546 if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
7547 /* MOVI or MVNI, with MVNI negation handled above. */
7548 tcg_gen_gvec_dup64i(vec_full_reg_offset(s, rd), is_q ? 16 : 8,
7549 vec_full_reg_size(s), imm);
7550 } else {
7551 /* ORR or BIC, with BIC negation to AND handled above. */
7552 if (is_neg) {
7553 gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
7554 } else {
7555 gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
7560 /* AdvSIMD scalar copy
7561 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
7562 * +-----+----+-----------------+------+---+------+---+------+------+
7563 * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
7564 * +-----+----+-----------------+------+---+------+---+------+------+
7566 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
7568 int rd = extract32(insn, 0, 5);
7569 int rn = extract32(insn, 5, 5);
7570 int imm4 = extract32(insn, 11, 4);
7571 int imm5 = extract32(insn, 16, 5);
7572 int op = extract32(insn, 29, 1);
7574 if (op != 0 || imm4 != 0) {
7575 unallocated_encoding(s);
7576 return;
7579 /* DUP (element, scalar) */
7580 handle_simd_dupes(s, rd, rn, imm5);
7583 /* AdvSIMD scalar pairwise
7584 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
7585 * +-----+---+-----------+------+-----------+--------+-----+------+------+
7586 * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
7587 * +-----+---+-----------+------+-----------+--------+-----+------+------+
7589 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
7591 int u = extract32(insn, 29, 1);
7592 int size = extract32(insn, 22, 2);
7593 int opcode = extract32(insn, 12, 5);
7594 int rn = extract32(insn, 5, 5);
7595 int rd = extract32(insn, 0, 5);
7596 TCGv_ptr fpst;
7598 /* For some ops (the FP ones), size[1] is part of the encoding.
7599 * For ADDP strictly it is not but size[1] is always 1 for valid
7600 * encodings.
7602 opcode |= (extract32(size, 1, 1) << 5);
7604 switch (opcode) {
7605 case 0x3b: /* ADDP */
7606 if (u || size != 3) {
7607 unallocated_encoding(s);
7608 return;
7610 if (!fp_access_check(s)) {
7611 return;
7614 fpst = NULL;
7615 break;
7616 case 0xc: /* FMAXNMP */
7617 case 0xd: /* FADDP */
7618 case 0xf: /* FMAXP */
7619 case 0x2c: /* FMINNMP */
7620 case 0x2f: /* FMINP */
7621 /* FP op, size[0] is 32 or 64 bit*/
7622 if (!u) {
7623 if (!dc_isar_feature(aa64_fp16, s)) {
7624 unallocated_encoding(s);
7625 return;
7626 } else {
7627 size = MO_16;
7629 } else {
7630 size = extract32(size, 0, 1) ? MO_64 : MO_32;
7633 if (!fp_access_check(s)) {
7634 return;
7637 fpst = get_fpstatus_ptr(size == MO_16);
7638 break;
7639 default:
7640 unallocated_encoding(s);
7641 return;
7644 if (size == MO_64) {
7645 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7646 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7647 TCGv_i64 tcg_res = tcg_temp_new_i64();
7649 read_vec_element(s, tcg_op1, rn, 0, MO_64);
7650 read_vec_element(s, tcg_op2, rn, 1, MO_64);
7652 switch (opcode) {
7653 case 0x3b: /* ADDP */
7654 tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
7655 break;
7656 case 0xc: /* FMAXNMP */
7657 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7658 break;
7659 case 0xd: /* FADDP */
7660 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
7661 break;
7662 case 0xf: /* FMAXP */
7663 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
7664 break;
7665 case 0x2c: /* FMINNMP */
7666 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7667 break;
7668 case 0x2f: /* FMINP */
7669 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
7670 break;
7671 default:
7672 g_assert_not_reached();
7675 write_fp_dreg(s, rd, tcg_res);
7677 tcg_temp_free_i64(tcg_op1);
7678 tcg_temp_free_i64(tcg_op2);
7679 tcg_temp_free_i64(tcg_res);
7680 } else {
7681 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7682 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7683 TCGv_i32 tcg_res = tcg_temp_new_i32();
7685 read_vec_element_i32(s, tcg_op1, rn, 0, size);
7686 read_vec_element_i32(s, tcg_op2, rn, 1, size);
7688 if (size == MO_16) {
7689 switch (opcode) {
7690 case 0xc: /* FMAXNMP */
7691 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
7692 break;
7693 case 0xd: /* FADDP */
7694 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
7695 break;
7696 case 0xf: /* FMAXP */
7697 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
7698 break;
7699 case 0x2c: /* FMINNMP */
7700 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
7701 break;
7702 case 0x2f: /* FMINP */
7703 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
7704 break;
7705 default:
7706 g_assert_not_reached();
7708 } else {
7709 switch (opcode) {
7710 case 0xc: /* FMAXNMP */
7711 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
7712 break;
7713 case 0xd: /* FADDP */
7714 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
7715 break;
7716 case 0xf: /* FMAXP */
7717 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
7718 break;
7719 case 0x2c: /* FMINNMP */
7720 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
7721 break;
7722 case 0x2f: /* FMINP */
7723 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
7724 break;
7725 default:
7726 g_assert_not_reached();
7730 write_fp_sreg(s, rd, tcg_res);
7732 tcg_temp_free_i32(tcg_op1);
7733 tcg_temp_free_i32(tcg_op2);
7734 tcg_temp_free_i32(tcg_res);
7737 if (fpst) {
7738 tcg_temp_free_ptr(fpst);
7743 * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
7745 * This code is handles the common shifting code and is used by both
7746 * the vector and scalar code.
7748 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
7749 TCGv_i64 tcg_rnd, bool accumulate,
7750 bool is_u, int size, int shift)
7752 bool extended_result = false;
7753 bool round = tcg_rnd != NULL;
7754 int ext_lshift = 0;
7755 TCGv_i64 tcg_src_hi;
7757 if (round && size == 3) {
7758 extended_result = true;
7759 ext_lshift = 64 - shift;
7760 tcg_src_hi = tcg_temp_new_i64();
7761 } else if (shift == 64) {
7762 if (!accumulate && is_u) {
7763 /* result is zero */
7764 tcg_gen_movi_i64(tcg_res, 0);
7765 return;
7769 /* Deal with the rounding step */
7770 if (round) {
7771 if (extended_result) {
7772 TCGv_i64 tcg_zero = tcg_const_i64(0);
7773 if (!is_u) {
7774 /* take care of sign extending tcg_res */
7775 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
7776 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
7777 tcg_src, tcg_src_hi,
7778 tcg_rnd, tcg_zero);
7779 } else {
7780 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
7781 tcg_src, tcg_zero,
7782 tcg_rnd, tcg_zero);
7784 tcg_temp_free_i64(tcg_zero);
7785 } else {
7786 tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
7790 /* Now do the shift right */
7791 if (round && extended_result) {
7792 /* extended case, >64 bit precision required */
7793 if (ext_lshift == 0) {
7794 /* special case, only high bits matter */
7795 tcg_gen_mov_i64(tcg_src, tcg_src_hi);
7796 } else {
7797 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
7798 tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
7799 tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
7801 } else {
7802 if (is_u) {
7803 if (shift == 64) {
7804 /* essentially shifting in 64 zeros */
7805 tcg_gen_movi_i64(tcg_src, 0);
7806 } else {
7807 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
7809 } else {
7810 if (shift == 64) {
7811 /* effectively extending the sign-bit */
7812 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
7813 } else {
7814 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
7819 if (accumulate) {
7820 tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
7821 } else {
7822 tcg_gen_mov_i64(tcg_res, tcg_src);
7825 if (extended_result) {
7826 tcg_temp_free_i64(tcg_src_hi);
7830 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
7831 static void handle_scalar_simd_shri(DisasContext *s,
7832 bool is_u, int immh, int immb,
7833 int opcode, int rn, int rd)
7835 const int size = 3;
7836 int immhb = immh << 3 | immb;
7837 int shift = 2 * (8 << size) - immhb;
7838 bool accumulate = false;
7839 bool round = false;
7840 bool insert = false;
7841 TCGv_i64 tcg_rn;
7842 TCGv_i64 tcg_rd;
7843 TCGv_i64 tcg_round;
7845 if (!extract32(immh, 3, 1)) {
7846 unallocated_encoding(s);
7847 return;
7850 if (!fp_access_check(s)) {
7851 return;
7854 switch (opcode) {
7855 case 0x02: /* SSRA / USRA (accumulate) */
7856 accumulate = true;
7857 break;
7858 case 0x04: /* SRSHR / URSHR (rounding) */
7859 round = true;
7860 break;
7861 case 0x06: /* SRSRA / URSRA (accum + rounding) */
7862 accumulate = round = true;
7863 break;
7864 case 0x08: /* SRI */
7865 insert = true;
7866 break;
7869 if (round) {
7870 uint64_t round_const = 1ULL << (shift - 1);
7871 tcg_round = tcg_const_i64(round_const);
7872 } else {
7873 tcg_round = NULL;
7876 tcg_rn = read_fp_dreg(s, rn);
7877 tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
7879 if (insert) {
7880 /* shift count same as element size is valid but does nothing;
7881 * special case to avoid potential shift by 64.
7883 int esize = 8 << size;
7884 if (shift != esize) {
7885 tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
7886 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
7888 } else {
7889 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
7890 accumulate, is_u, size, shift);
7893 write_fp_dreg(s, rd, tcg_rd);
7895 tcg_temp_free_i64(tcg_rn);
7896 tcg_temp_free_i64(tcg_rd);
7897 if (round) {
7898 tcg_temp_free_i64(tcg_round);
7902 /* SHL/SLI - Scalar shift left */
7903 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
7904 int immh, int immb, int opcode,
7905 int rn, int rd)
7907 int size = 32 - clz32(immh) - 1;
7908 int immhb = immh << 3 | immb;
7909 int shift = immhb - (8 << size);
7910 TCGv_i64 tcg_rn = new_tmp_a64(s);
7911 TCGv_i64 tcg_rd = new_tmp_a64(s);
7913 if (!extract32(immh, 3, 1)) {
7914 unallocated_encoding(s);
7915 return;
7918 if (!fp_access_check(s)) {
7919 return;
7922 tcg_rn = read_fp_dreg(s, rn);
7923 tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
7925 if (insert) {
7926 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
7927 } else {
7928 tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
7931 write_fp_dreg(s, rd, tcg_rd);
7933 tcg_temp_free_i64(tcg_rn);
7934 tcg_temp_free_i64(tcg_rd);
7937 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
7938 * (signed/unsigned) narrowing */
7939 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
7940 bool is_u_shift, bool is_u_narrow,
7941 int immh, int immb, int opcode,
7942 int rn, int rd)
7944 int immhb = immh << 3 | immb;
7945 int size = 32 - clz32(immh) - 1;
7946 int esize = 8 << size;
7947 int shift = (2 * esize) - immhb;
7948 int elements = is_scalar ? 1 : (64 / esize);
7949 bool round = extract32(opcode, 0, 1);
7950 TCGMemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
7951 TCGv_i64 tcg_rn, tcg_rd, tcg_round;
7952 TCGv_i32 tcg_rd_narrowed;
7953 TCGv_i64 tcg_final;
7955 static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
7956 { gen_helper_neon_narrow_sat_s8,
7957 gen_helper_neon_unarrow_sat8 },
7958 { gen_helper_neon_narrow_sat_s16,
7959 gen_helper_neon_unarrow_sat16 },
7960 { gen_helper_neon_narrow_sat_s32,
7961 gen_helper_neon_unarrow_sat32 },
7962 { NULL, NULL },
7964 static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
7965 gen_helper_neon_narrow_sat_u8,
7966 gen_helper_neon_narrow_sat_u16,
7967 gen_helper_neon_narrow_sat_u32,
7968 NULL
7970 NeonGenNarrowEnvFn *narrowfn;
7972 int i;
7974 assert(size < 4);
7976 if (extract32(immh, 3, 1)) {
7977 unallocated_encoding(s);
7978 return;
7981 if (!fp_access_check(s)) {
7982 return;
7985 if (is_u_shift) {
7986 narrowfn = unsigned_narrow_fns[size];
7987 } else {
7988 narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
7991 tcg_rn = tcg_temp_new_i64();
7992 tcg_rd = tcg_temp_new_i64();
7993 tcg_rd_narrowed = tcg_temp_new_i32();
7994 tcg_final = tcg_const_i64(0);
7996 if (round) {
7997 uint64_t round_const = 1ULL << (shift - 1);
7998 tcg_round = tcg_const_i64(round_const);
7999 } else {
8000 tcg_round = NULL;
8003 for (i = 0; i < elements; i++) {
8004 read_vec_element(s, tcg_rn, rn, i, ldop);
8005 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8006 false, is_u_shift, size+1, shift);
8007 narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8008 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8009 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8012 if (!is_q) {
8013 write_vec_element(s, tcg_final, rd, 0, MO_64);
8014 } else {
8015 write_vec_element(s, tcg_final, rd, 1, MO_64);
8018 if (round) {
8019 tcg_temp_free_i64(tcg_round);
8021 tcg_temp_free_i64(tcg_rn);
8022 tcg_temp_free_i64(tcg_rd);
8023 tcg_temp_free_i32(tcg_rd_narrowed);
8024 tcg_temp_free_i64(tcg_final);
8026 clear_vec_high(s, is_q, rd);
8029 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8030 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8031 bool src_unsigned, bool dst_unsigned,
8032 int immh, int immb, int rn, int rd)
8034 int immhb = immh << 3 | immb;
8035 int size = 32 - clz32(immh) - 1;
8036 int shift = immhb - (8 << size);
8037 int pass;
8039 assert(immh != 0);
8040 assert(!(scalar && is_q));
8042 if (!scalar) {
8043 if (!is_q && extract32(immh, 3, 1)) {
8044 unallocated_encoding(s);
8045 return;
8048 /* Since we use the variable-shift helpers we must
8049 * replicate the shift count into each element of
8050 * the tcg_shift value.
8052 switch (size) {
8053 case 0:
8054 shift |= shift << 8;
8055 /* fall through */
8056 case 1:
8057 shift |= shift << 16;
8058 break;
8059 case 2:
8060 case 3:
8061 break;
8062 default:
8063 g_assert_not_reached();
8067 if (!fp_access_check(s)) {
8068 return;
8071 if (size == 3) {
8072 TCGv_i64 tcg_shift = tcg_const_i64(shift);
8073 static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8074 { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8075 { NULL, gen_helper_neon_qshl_u64 },
8077 NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8078 int maxpass = is_q ? 2 : 1;
8080 for (pass = 0; pass < maxpass; pass++) {
8081 TCGv_i64 tcg_op = tcg_temp_new_i64();
8083 read_vec_element(s, tcg_op, rn, pass, MO_64);
8084 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8085 write_vec_element(s, tcg_op, rd, pass, MO_64);
8087 tcg_temp_free_i64(tcg_op);
8089 tcg_temp_free_i64(tcg_shift);
8090 clear_vec_high(s, is_q, rd);
8091 } else {
8092 TCGv_i32 tcg_shift = tcg_const_i32(shift);
8093 static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8095 { gen_helper_neon_qshl_s8,
8096 gen_helper_neon_qshl_s16,
8097 gen_helper_neon_qshl_s32 },
8098 { gen_helper_neon_qshlu_s8,
8099 gen_helper_neon_qshlu_s16,
8100 gen_helper_neon_qshlu_s32 }
8101 }, {
8102 { NULL, NULL, NULL },
8103 { gen_helper_neon_qshl_u8,
8104 gen_helper_neon_qshl_u16,
8105 gen_helper_neon_qshl_u32 }
8108 NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8109 TCGMemOp memop = scalar ? size : MO_32;
8110 int maxpass = scalar ? 1 : is_q ? 4 : 2;
8112 for (pass = 0; pass < maxpass; pass++) {
8113 TCGv_i32 tcg_op = tcg_temp_new_i32();
8115 read_vec_element_i32(s, tcg_op, rn, pass, memop);
8116 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8117 if (scalar) {
8118 switch (size) {
8119 case 0:
8120 tcg_gen_ext8u_i32(tcg_op, tcg_op);
8121 break;
8122 case 1:
8123 tcg_gen_ext16u_i32(tcg_op, tcg_op);
8124 break;
8125 case 2:
8126 break;
8127 default:
8128 g_assert_not_reached();
8130 write_fp_sreg(s, rd, tcg_op);
8131 } else {
8132 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8135 tcg_temp_free_i32(tcg_op);
8137 tcg_temp_free_i32(tcg_shift);
8139 if (!scalar) {
8140 clear_vec_high(s, is_q, rd);
8145 /* Common vector code for handling integer to FP conversion */
8146 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8147 int elements, int is_signed,
8148 int fracbits, int size)
8150 TCGv_ptr tcg_fpst = get_fpstatus_ptr(size == MO_16);
8151 TCGv_i32 tcg_shift = NULL;
8153 TCGMemOp mop = size | (is_signed ? MO_SIGN : 0);
8154 int pass;
8156 if (fracbits || size == MO_64) {
8157 tcg_shift = tcg_const_i32(fracbits);
8160 if (size == MO_64) {
8161 TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8162 TCGv_i64 tcg_double = tcg_temp_new_i64();
8164 for (pass = 0; pass < elements; pass++) {
8165 read_vec_element(s, tcg_int64, rn, pass, mop);
8167 if (is_signed) {
8168 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8169 tcg_shift, tcg_fpst);
8170 } else {
8171 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8172 tcg_shift, tcg_fpst);
8174 if (elements == 1) {
8175 write_fp_dreg(s, rd, tcg_double);
8176 } else {
8177 write_vec_element(s, tcg_double, rd, pass, MO_64);
8181 tcg_temp_free_i64(tcg_int64);
8182 tcg_temp_free_i64(tcg_double);
8184 } else {
8185 TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8186 TCGv_i32 tcg_float = tcg_temp_new_i32();
8188 for (pass = 0; pass < elements; pass++) {
8189 read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8191 switch (size) {
8192 case MO_32:
8193 if (fracbits) {
8194 if (is_signed) {
8195 gen_helper_vfp_sltos(tcg_float, tcg_int32,
8196 tcg_shift, tcg_fpst);
8197 } else {
8198 gen_helper_vfp_ultos(tcg_float, tcg_int32,
8199 tcg_shift, tcg_fpst);
8201 } else {
8202 if (is_signed) {
8203 gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8204 } else {
8205 gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8208 break;
8209 case MO_16:
8210 if (fracbits) {
8211 if (is_signed) {
8212 gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8213 tcg_shift, tcg_fpst);
8214 } else {
8215 gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8216 tcg_shift, tcg_fpst);
8218 } else {
8219 if (is_signed) {
8220 gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8221 } else {
8222 gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8225 break;
8226 default:
8227 g_assert_not_reached();
8230 if (elements == 1) {
8231 write_fp_sreg(s, rd, tcg_float);
8232 } else {
8233 write_vec_element_i32(s, tcg_float, rd, pass, size);
8237 tcg_temp_free_i32(tcg_int32);
8238 tcg_temp_free_i32(tcg_float);
8241 tcg_temp_free_ptr(tcg_fpst);
8242 if (tcg_shift) {
8243 tcg_temp_free_i32(tcg_shift);
8246 clear_vec_high(s, elements << size == 16, rd);
8249 /* UCVTF/SCVTF - Integer to FP conversion */
8250 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8251 bool is_q, bool is_u,
8252 int immh, int immb, int opcode,
8253 int rn, int rd)
8255 int size, elements, fracbits;
8256 int immhb = immh << 3 | immb;
8258 if (immh & 8) {
8259 size = MO_64;
8260 if (!is_scalar && !is_q) {
8261 unallocated_encoding(s);
8262 return;
8264 } else if (immh & 4) {
8265 size = MO_32;
8266 } else if (immh & 2) {
8267 size = MO_16;
8268 if (!dc_isar_feature(aa64_fp16, s)) {
8269 unallocated_encoding(s);
8270 return;
8272 } else {
8273 /* immh == 0 would be a failure of the decode logic */
8274 g_assert(immh == 1);
8275 unallocated_encoding(s);
8276 return;
8279 if (is_scalar) {
8280 elements = 1;
8281 } else {
8282 elements = (8 << is_q) >> size;
8284 fracbits = (16 << size) - immhb;
8286 if (!fp_access_check(s)) {
8287 return;
8290 handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8293 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8294 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8295 bool is_q, bool is_u,
8296 int immh, int immb, int rn, int rd)
8298 int immhb = immh << 3 | immb;
8299 int pass, size, fracbits;
8300 TCGv_ptr tcg_fpstatus;
8301 TCGv_i32 tcg_rmode, tcg_shift;
8303 if (immh & 0x8) {
8304 size = MO_64;
8305 if (!is_scalar && !is_q) {
8306 unallocated_encoding(s);
8307 return;
8309 } else if (immh & 0x4) {
8310 size = MO_32;
8311 } else if (immh & 0x2) {
8312 size = MO_16;
8313 if (!dc_isar_feature(aa64_fp16, s)) {
8314 unallocated_encoding(s);
8315 return;
8317 } else {
8318 /* Should have split out AdvSIMD modified immediate earlier. */
8319 assert(immh == 1);
8320 unallocated_encoding(s);
8321 return;
8324 if (!fp_access_check(s)) {
8325 return;
8328 assert(!(is_scalar && is_q));
8330 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(FPROUNDING_ZERO));
8331 tcg_fpstatus = get_fpstatus_ptr(size == MO_16);
8332 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
8333 fracbits = (16 << size) - immhb;
8334 tcg_shift = tcg_const_i32(fracbits);
8336 if (size == MO_64) {
8337 int maxpass = is_scalar ? 1 : 2;
8339 for (pass = 0; pass < maxpass; pass++) {
8340 TCGv_i64 tcg_op = tcg_temp_new_i64();
8342 read_vec_element(s, tcg_op, rn, pass, MO_64);
8343 if (is_u) {
8344 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8345 } else {
8346 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8348 write_vec_element(s, tcg_op, rd, pass, MO_64);
8349 tcg_temp_free_i64(tcg_op);
8351 clear_vec_high(s, is_q, rd);
8352 } else {
8353 void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8354 int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8356 switch (size) {
8357 case MO_16:
8358 if (is_u) {
8359 fn = gen_helper_vfp_touhh;
8360 } else {
8361 fn = gen_helper_vfp_toshh;
8363 break;
8364 case MO_32:
8365 if (is_u) {
8366 fn = gen_helper_vfp_touls;
8367 } else {
8368 fn = gen_helper_vfp_tosls;
8370 break;
8371 default:
8372 g_assert_not_reached();
8375 for (pass = 0; pass < maxpass; pass++) {
8376 TCGv_i32 tcg_op = tcg_temp_new_i32();
8378 read_vec_element_i32(s, tcg_op, rn, pass, size);
8379 fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8380 if (is_scalar) {
8381 write_fp_sreg(s, rd, tcg_op);
8382 } else {
8383 write_vec_element_i32(s, tcg_op, rd, pass, size);
8385 tcg_temp_free_i32(tcg_op);
8387 if (!is_scalar) {
8388 clear_vec_high(s, is_q, rd);
8392 tcg_temp_free_ptr(tcg_fpstatus);
8393 tcg_temp_free_i32(tcg_shift);
8394 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
8395 tcg_temp_free_i32(tcg_rmode);
8398 /* AdvSIMD scalar shift by immediate
8399 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
8400 * +-----+---+-------------+------+------+--------+---+------+------+
8401 * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
8402 * +-----+---+-------------+------+------+--------+---+------+------+
8404 * This is the scalar version so it works on a fixed sized registers
8406 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8408 int rd = extract32(insn, 0, 5);
8409 int rn = extract32(insn, 5, 5);
8410 int opcode = extract32(insn, 11, 5);
8411 int immb = extract32(insn, 16, 3);
8412 int immh = extract32(insn, 19, 4);
8413 bool is_u = extract32(insn, 29, 1);
8415 if (immh == 0) {
8416 unallocated_encoding(s);
8417 return;
8420 switch (opcode) {
8421 case 0x08: /* SRI */
8422 if (!is_u) {
8423 unallocated_encoding(s);
8424 return;
8426 /* fall through */
8427 case 0x00: /* SSHR / USHR */
8428 case 0x02: /* SSRA / USRA */
8429 case 0x04: /* SRSHR / URSHR */
8430 case 0x06: /* SRSRA / URSRA */
8431 handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8432 break;
8433 case 0x0a: /* SHL / SLI */
8434 handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8435 break;
8436 case 0x1c: /* SCVTF, UCVTF */
8437 handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8438 opcode, rn, rd);
8439 break;
8440 case 0x10: /* SQSHRUN, SQSHRUN2 */
8441 case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8442 if (!is_u) {
8443 unallocated_encoding(s);
8444 return;
8446 handle_vec_simd_sqshrn(s, true, false, false, true,
8447 immh, immb, opcode, rn, rd);
8448 break;
8449 case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8450 case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8451 handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8452 immh, immb, opcode, rn, rd);
8453 break;
8454 case 0xc: /* SQSHLU */
8455 if (!is_u) {
8456 unallocated_encoding(s);
8457 return;
8459 handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8460 break;
8461 case 0xe: /* SQSHL, UQSHL */
8462 handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8463 break;
8464 case 0x1f: /* FCVTZS, FCVTZU */
8465 handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8466 break;
8467 default:
8468 unallocated_encoding(s);
8469 break;
8473 /* AdvSIMD scalar three different
8474 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
8475 * +-----+---+-----------+------+---+------+--------+-----+------+------+
8476 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
8477 * +-----+---+-----------+------+---+------+--------+-----+------+------+
8479 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8481 bool is_u = extract32(insn, 29, 1);
8482 int size = extract32(insn, 22, 2);
8483 int opcode = extract32(insn, 12, 4);
8484 int rm = extract32(insn, 16, 5);
8485 int rn = extract32(insn, 5, 5);
8486 int rd = extract32(insn, 0, 5);
8488 if (is_u) {
8489 unallocated_encoding(s);
8490 return;
8493 switch (opcode) {
8494 case 0x9: /* SQDMLAL, SQDMLAL2 */
8495 case 0xb: /* SQDMLSL, SQDMLSL2 */
8496 case 0xd: /* SQDMULL, SQDMULL2 */
8497 if (size == 0 || size == 3) {
8498 unallocated_encoding(s);
8499 return;
8501 break;
8502 default:
8503 unallocated_encoding(s);
8504 return;
8507 if (!fp_access_check(s)) {
8508 return;
8511 if (size == 2) {
8512 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8513 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8514 TCGv_i64 tcg_res = tcg_temp_new_i64();
8516 read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8517 read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8519 tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8520 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
8522 switch (opcode) {
8523 case 0xd: /* SQDMULL, SQDMULL2 */
8524 break;
8525 case 0xb: /* SQDMLSL, SQDMLSL2 */
8526 tcg_gen_neg_i64(tcg_res, tcg_res);
8527 /* fall through */
8528 case 0x9: /* SQDMLAL, SQDMLAL2 */
8529 read_vec_element(s, tcg_op1, rd, 0, MO_64);
8530 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
8531 tcg_res, tcg_op1);
8532 break;
8533 default:
8534 g_assert_not_reached();
8537 write_fp_dreg(s, rd, tcg_res);
8539 tcg_temp_free_i64(tcg_op1);
8540 tcg_temp_free_i64(tcg_op2);
8541 tcg_temp_free_i64(tcg_res);
8542 } else {
8543 TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8544 TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8545 TCGv_i64 tcg_res = tcg_temp_new_i64();
8547 gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8548 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
8550 switch (opcode) {
8551 case 0xd: /* SQDMULL, SQDMULL2 */
8552 break;
8553 case 0xb: /* SQDMLSL, SQDMLSL2 */
8554 gen_helper_neon_negl_u32(tcg_res, tcg_res);
8555 /* fall through */
8556 case 0x9: /* SQDMLAL, SQDMLAL2 */
8558 TCGv_i64 tcg_op3 = tcg_temp_new_i64();
8559 read_vec_element(s, tcg_op3, rd, 0, MO_32);
8560 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
8561 tcg_res, tcg_op3);
8562 tcg_temp_free_i64(tcg_op3);
8563 break;
8565 default:
8566 g_assert_not_reached();
8569 tcg_gen_ext32u_i64(tcg_res, tcg_res);
8570 write_fp_dreg(s, rd, tcg_res);
8572 tcg_temp_free_i32(tcg_op1);
8573 tcg_temp_free_i32(tcg_op2);
8574 tcg_temp_free_i64(tcg_res);
8578 static void handle_3same_64(DisasContext *s, int opcode, bool u,
8579 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
8581 /* Handle 64x64->64 opcodes which are shared between the scalar
8582 * and vector 3-same groups. We cover every opcode where size == 3
8583 * is valid in either the three-reg-same (integer, not pairwise)
8584 * or scalar-three-reg-same groups.
8586 TCGCond cond;
8588 switch (opcode) {
8589 case 0x1: /* SQADD */
8590 if (u) {
8591 gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8592 } else {
8593 gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8595 break;
8596 case 0x5: /* SQSUB */
8597 if (u) {
8598 gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8599 } else {
8600 gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8602 break;
8603 case 0x6: /* CMGT, CMHI */
8604 /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
8605 * We implement this using setcond (test) and then negating.
8607 cond = u ? TCG_COND_GTU : TCG_COND_GT;
8608 do_cmop:
8609 tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
8610 tcg_gen_neg_i64(tcg_rd, tcg_rd);
8611 break;
8612 case 0x7: /* CMGE, CMHS */
8613 cond = u ? TCG_COND_GEU : TCG_COND_GE;
8614 goto do_cmop;
8615 case 0x11: /* CMTST, CMEQ */
8616 if (u) {
8617 cond = TCG_COND_EQ;
8618 goto do_cmop;
8620 gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
8621 break;
8622 case 0x8: /* SSHL, USHL */
8623 if (u) {
8624 gen_helper_neon_shl_u64(tcg_rd, tcg_rn, tcg_rm);
8625 } else {
8626 gen_helper_neon_shl_s64(tcg_rd, tcg_rn, tcg_rm);
8628 break;
8629 case 0x9: /* SQSHL, UQSHL */
8630 if (u) {
8631 gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8632 } else {
8633 gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8635 break;
8636 case 0xa: /* SRSHL, URSHL */
8637 if (u) {
8638 gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
8639 } else {
8640 gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
8642 break;
8643 case 0xb: /* SQRSHL, UQRSHL */
8644 if (u) {
8645 gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8646 } else {
8647 gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8649 break;
8650 case 0x10: /* ADD, SUB */
8651 if (u) {
8652 tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
8653 } else {
8654 tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
8656 break;
8657 default:
8658 g_assert_not_reached();
8662 /* Handle the 3-same-operands float operations; shared by the scalar
8663 * and vector encodings. The caller must filter out any encodings
8664 * not allocated for the encoding it is dealing with.
8666 static void handle_3same_float(DisasContext *s, int size, int elements,
8667 int fpopcode, int rd, int rn, int rm)
8669 int pass;
8670 TCGv_ptr fpst = get_fpstatus_ptr(false);
8672 for (pass = 0; pass < elements; pass++) {
8673 if (size) {
8674 /* Double */
8675 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8676 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8677 TCGv_i64 tcg_res = tcg_temp_new_i64();
8679 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8680 read_vec_element(s, tcg_op2, rm, pass, MO_64);
8682 switch (fpopcode) {
8683 case 0x39: /* FMLS */
8684 /* As usual for ARM, separate negation for fused multiply-add */
8685 gen_helper_vfp_negd(tcg_op1, tcg_op1);
8686 /* fall through */
8687 case 0x19: /* FMLA */
8688 read_vec_element(s, tcg_res, rd, pass, MO_64);
8689 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
8690 tcg_res, fpst);
8691 break;
8692 case 0x18: /* FMAXNM */
8693 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8694 break;
8695 case 0x1a: /* FADD */
8696 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8697 break;
8698 case 0x1b: /* FMULX */
8699 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
8700 break;
8701 case 0x1c: /* FCMEQ */
8702 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8703 break;
8704 case 0x1e: /* FMAX */
8705 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8706 break;
8707 case 0x1f: /* FRECPS */
8708 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8709 break;
8710 case 0x38: /* FMINNM */
8711 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8712 break;
8713 case 0x3a: /* FSUB */
8714 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
8715 break;
8716 case 0x3e: /* FMIN */
8717 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8718 break;
8719 case 0x3f: /* FRSQRTS */
8720 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8721 break;
8722 case 0x5b: /* FMUL */
8723 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
8724 break;
8725 case 0x5c: /* FCMGE */
8726 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8727 break;
8728 case 0x5d: /* FACGE */
8729 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8730 break;
8731 case 0x5f: /* FDIV */
8732 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
8733 break;
8734 case 0x7a: /* FABD */
8735 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
8736 gen_helper_vfp_absd(tcg_res, tcg_res);
8737 break;
8738 case 0x7c: /* FCMGT */
8739 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8740 break;
8741 case 0x7d: /* FACGT */
8742 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8743 break;
8744 default:
8745 g_assert_not_reached();
8748 write_vec_element(s, tcg_res, rd, pass, MO_64);
8750 tcg_temp_free_i64(tcg_res);
8751 tcg_temp_free_i64(tcg_op1);
8752 tcg_temp_free_i64(tcg_op2);
8753 } else {
8754 /* Single */
8755 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8756 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8757 TCGv_i32 tcg_res = tcg_temp_new_i32();
8759 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
8760 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
8762 switch (fpopcode) {
8763 case 0x39: /* FMLS */
8764 /* As usual for ARM, separate negation for fused multiply-add */
8765 gen_helper_vfp_negs(tcg_op1, tcg_op1);
8766 /* fall through */
8767 case 0x19: /* FMLA */
8768 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
8769 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
8770 tcg_res, fpst);
8771 break;
8772 case 0x1a: /* FADD */
8773 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8774 break;
8775 case 0x1b: /* FMULX */
8776 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
8777 break;
8778 case 0x1c: /* FCMEQ */
8779 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8780 break;
8781 case 0x1e: /* FMAX */
8782 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8783 break;
8784 case 0x1f: /* FRECPS */
8785 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8786 break;
8787 case 0x18: /* FMAXNM */
8788 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8789 break;
8790 case 0x38: /* FMINNM */
8791 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8792 break;
8793 case 0x3a: /* FSUB */
8794 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
8795 break;
8796 case 0x3e: /* FMIN */
8797 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8798 break;
8799 case 0x3f: /* FRSQRTS */
8800 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8801 break;
8802 case 0x5b: /* FMUL */
8803 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
8804 break;
8805 case 0x5c: /* FCMGE */
8806 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8807 break;
8808 case 0x5d: /* FACGE */
8809 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8810 break;
8811 case 0x5f: /* FDIV */
8812 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
8813 break;
8814 case 0x7a: /* FABD */
8815 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
8816 gen_helper_vfp_abss(tcg_res, tcg_res);
8817 break;
8818 case 0x7c: /* FCMGT */
8819 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8820 break;
8821 case 0x7d: /* FACGT */
8822 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8823 break;
8824 default:
8825 g_assert_not_reached();
8828 if (elements == 1) {
8829 /* scalar single so clear high part */
8830 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
8832 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
8833 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
8834 tcg_temp_free_i64(tcg_tmp);
8835 } else {
8836 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
8839 tcg_temp_free_i32(tcg_res);
8840 tcg_temp_free_i32(tcg_op1);
8841 tcg_temp_free_i32(tcg_op2);
8845 tcg_temp_free_ptr(fpst);
8847 clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
8850 /* AdvSIMD scalar three same
8851 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
8852 * +-----+---+-----------+------+---+------+--------+---+------+------+
8853 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
8854 * +-----+---+-----------+------+---+------+--------+---+------+------+
8856 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
8858 int rd = extract32(insn, 0, 5);
8859 int rn = extract32(insn, 5, 5);
8860 int opcode = extract32(insn, 11, 5);
8861 int rm = extract32(insn, 16, 5);
8862 int size = extract32(insn, 22, 2);
8863 bool u = extract32(insn, 29, 1);
8864 TCGv_i64 tcg_rd;
8866 if (opcode >= 0x18) {
8867 /* Floating point: U, size[1] and opcode indicate operation */
8868 int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
8869 switch (fpopcode) {
8870 case 0x1b: /* FMULX */
8871 case 0x1f: /* FRECPS */
8872 case 0x3f: /* FRSQRTS */
8873 case 0x5d: /* FACGE */
8874 case 0x7d: /* FACGT */
8875 case 0x1c: /* FCMEQ */
8876 case 0x5c: /* FCMGE */
8877 case 0x7c: /* FCMGT */
8878 case 0x7a: /* FABD */
8879 break;
8880 default:
8881 unallocated_encoding(s);
8882 return;
8885 if (!fp_access_check(s)) {
8886 return;
8889 handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
8890 return;
8893 switch (opcode) {
8894 case 0x1: /* SQADD, UQADD */
8895 case 0x5: /* SQSUB, UQSUB */
8896 case 0x9: /* SQSHL, UQSHL */
8897 case 0xb: /* SQRSHL, UQRSHL */
8898 break;
8899 case 0x8: /* SSHL, USHL */
8900 case 0xa: /* SRSHL, URSHL */
8901 case 0x6: /* CMGT, CMHI */
8902 case 0x7: /* CMGE, CMHS */
8903 case 0x11: /* CMTST, CMEQ */
8904 case 0x10: /* ADD, SUB (vector) */
8905 if (size != 3) {
8906 unallocated_encoding(s);
8907 return;
8909 break;
8910 case 0x16: /* SQDMULH, SQRDMULH (vector) */
8911 if (size != 1 && size != 2) {
8912 unallocated_encoding(s);
8913 return;
8915 break;
8916 default:
8917 unallocated_encoding(s);
8918 return;
8921 if (!fp_access_check(s)) {
8922 return;
8925 tcg_rd = tcg_temp_new_i64();
8927 if (size == 3) {
8928 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
8929 TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
8931 handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
8932 tcg_temp_free_i64(tcg_rn);
8933 tcg_temp_free_i64(tcg_rm);
8934 } else {
8935 /* Do a single operation on the lowest element in the vector.
8936 * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
8937 * no side effects for all these operations.
8938 * OPTME: special-purpose helpers would avoid doing some
8939 * unnecessary work in the helper for the 8 and 16 bit cases.
8941 NeonGenTwoOpEnvFn *genenvfn;
8942 TCGv_i32 tcg_rn = tcg_temp_new_i32();
8943 TCGv_i32 tcg_rm = tcg_temp_new_i32();
8944 TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
8946 read_vec_element_i32(s, tcg_rn, rn, 0, size);
8947 read_vec_element_i32(s, tcg_rm, rm, 0, size);
8949 switch (opcode) {
8950 case 0x1: /* SQADD, UQADD */
8952 static NeonGenTwoOpEnvFn * const fns[3][2] = {
8953 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
8954 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
8955 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
8957 genenvfn = fns[size][u];
8958 break;
8960 case 0x5: /* SQSUB, UQSUB */
8962 static NeonGenTwoOpEnvFn * const fns[3][2] = {
8963 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
8964 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
8965 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
8967 genenvfn = fns[size][u];
8968 break;
8970 case 0x9: /* SQSHL, UQSHL */
8972 static NeonGenTwoOpEnvFn * const fns[3][2] = {
8973 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
8974 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
8975 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
8977 genenvfn = fns[size][u];
8978 break;
8980 case 0xb: /* SQRSHL, UQRSHL */
8982 static NeonGenTwoOpEnvFn * const fns[3][2] = {
8983 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
8984 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
8985 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
8987 genenvfn = fns[size][u];
8988 break;
8990 case 0x16: /* SQDMULH, SQRDMULH */
8992 static NeonGenTwoOpEnvFn * const fns[2][2] = {
8993 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
8994 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
8996 assert(size == 1 || size == 2);
8997 genenvfn = fns[size - 1][u];
8998 break;
9000 default:
9001 g_assert_not_reached();
9004 genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9005 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9006 tcg_temp_free_i32(tcg_rd32);
9007 tcg_temp_free_i32(tcg_rn);
9008 tcg_temp_free_i32(tcg_rm);
9011 write_fp_dreg(s, rd, tcg_rd);
9013 tcg_temp_free_i64(tcg_rd);
9016 /* AdvSIMD scalar three same FP16
9017 * 31 30 29 28 24 23 22 21 20 16 15 14 13 11 10 9 5 4 0
9018 * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9019 * | 0 1 | U | 1 1 1 1 0 | a | 1 0 | Rm | 0 0 | opcode | 1 | Rn | Rd |
9020 * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9021 * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9022 * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9024 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9025 uint32_t insn)
9027 int rd = extract32(insn, 0, 5);
9028 int rn = extract32(insn, 5, 5);
9029 int opcode = extract32(insn, 11, 3);
9030 int rm = extract32(insn, 16, 5);
9031 bool u = extract32(insn, 29, 1);
9032 bool a = extract32(insn, 23, 1);
9033 int fpopcode = opcode | (a << 3) | (u << 4);
9034 TCGv_ptr fpst;
9035 TCGv_i32 tcg_op1;
9036 TCGv_i32 tcg_op2;
9037 TCGv_i32 tcg_res;
9039 switch (fpopcode) {
9040 case 0x03: /* FMULX */
9041 case 0x04: /* FCMEQ (reg) */
9042 case 0x07: /* FRECPS */
9043 case 0x0f: /* FRSQRTS */
9044 case 0x14: /* FCMGE (reg) */
9045 case 0x15: /* FACGE */
9046 case 0x1a: /* FABD */
9047 case 0x1c: /* FCMGT (reg) */
9048 case 0x1d: /* FACGT */
9049 break;
9050 default:
9051 unallocated_encoding(s);
9052 return;
9055 if (!dc_isar_feature(aa64_fp16, s)) {
9056 unallocated_encoding(s);
9059 if (!fp_access_check(s)) {
9060 return;
9063 fpst = get_fpstatus_ptr(true);
9065 tcg_op1 = read_fp_hreg(s, rn);
9066 tcg_op2 = read_fp_hreg(s, rm);
9067 tcg_res = tcg_temp_new_i32();
9069 switch (fpopcode) {
9070 case 0x03: /* FMULX */
9071 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9072 break;
9073 case 0x04: /* FCMEQ (reg) */
9074 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9075 break;
9076 case 0x07: /* FRECPS */
9077 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9078 break;
9079 case 0x0f: /* FRSQRTS */
9080 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9081 break;
9082 case 0x14: /* FCMGE (reg) */
9083 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9084 break;
9085 case 0x15: /* FACGE */
9086 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9087 break;
9088 case 0x1a: /* FABD */
9089 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9090 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9091 break;
9092 case 0x1c: /* FCMGT (reg) */
9093 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9094 break;
9095 case 0x1d: /* FACGT */
9096 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9097 break;
9098 default:
9099 g_assert_not_reached();
9102 write_fp_sreg(s, rd, tcg_res);
9105 tcg_temp_free_i32(tcg_res);
9106 tcg_temp_free_i32(tcg_op1);
9107 tcg_temp_free_i32(tcg_op2);
9108 tcg_temp_free_ptr(fpst);
9111 /* AdvSIMD scalar three same extra
9112 * 31 30 29 28 24 23 22 21 20 16 15 14 11 10 9 5 4 0
9113 * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9114 * | 0 1 | U | 1 1 1 1 0 | size | 0 | Rm | 1 | opcode | 1 | Rn | Rd |
9115 * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9117 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9118 uint32_t insn)
9120 int rd = extract32(insn, 0, 5);
9121 int rn = extract32(insn, 5, 5);
9122 int opcode = extract32(insn, 11, 4);
9123 int rm = extract32(insn, 16, 5);
9124 int size = extract32(insn, 22, 2);
9125 bool u = extract32(insn, 29, 1);
9126 TCGv_i32 ele1, ele2, ele3;
9127 TCGv_i64 res;
9128 bool feature;
9130 switch (u * 16 + opcode) {
9131 case 0x10: /* SQRDMLAH (vector) */
9132 case 0x11: /* SQRDMLSH (vector) */
9133 if (size != 1 && size != 2) {
9134 unallocated_encoding(s);
9135 return;
9137 feature = dc_isar_feature(aa64_rdm, s);
9138 break;
9139 default:
9140 unallocated_encoding(s);
9141 return;
9143 if (!feature) {
9144 unallocated_encoding(s);
9145 return;
9147 if (!fp_access_check(s)) {
9148 return;
9151 /* Do a single operation on the lowest element in the vector.
9152 * We use the standard Neon helpers and rely on 0 OP 0 == 0
9153 * with no side effects for all these operations.
9154 * OPTME: special-purpose helpers would avoid doing some
9155 * unnecessary work in the helper for the 16 bit cases.
9157 ele1 = tcg_temp_new_i32();
9158 ele2 = tcg_temp_new_i32();
9159 ele3 = tcg_temp_new_i32();
9161 read_vec_element_i32(s, ele1, rn, 0, size);
9162 read_vec_element_i32(s, ele2, rm, 0, size);
9163 read_vec_element_i32(s, ele3, rd, 0, size);
9165 switch (opcode) {
9166 case 0x0: /* SQRDMLAH */
9167 if (size == 1) {
9168 gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9169 } else {
9170 gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9172 break;
9173 case 0x1: /* SQRDMLSH */
9174 if (size == 1) {
9175 gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9176 } else {
9177 gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9179 break;
9180 default:
9181 g_assert_not_reached();
9183 tcg_temp_free_i32(ele1);
9184 tcg_temp_free_i32(ele2);
9186 res = tcg_temp_new_i64();
9187 tcg_gen_extu_i32_i64(res, ele3);
9188 tcg_temp_free_i32(ele3);
9190 write_fp_dreg(s, rd, res);
9191 tcg_temp_free_i64(res);
9194 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9195 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9196 TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9198 /* Handle 64->64 opcodes which are shared between the scalar and
9199 * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9200 * is valid in either group and also the double-precision fp ops.
9201 * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9202 * requires them.
9204 TCGCond cond;
9206 switch (opcode) {
9207 case 0x4: /* CLS, CLZ */
9208 if (u) {
9209 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9210 } else {
9211 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9213 break;
9214 case 0x5: /* NOT */
9215 /* This opcode is shared with CNT and RBIT but we have earlier
9216 * enforced that size == 3 if and only if this is the NOT insn.
9218 tcg_gen_not_i64(tcg_rd, tcg_rn);
9219 break;
9220 case 0x7: /* SQABS, SQNEG */
9221 if (u) {
9222 gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9223 } else {
9224 gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9226 break;
9227 case 0xa: /* CMLT */
9228 /* 64 bit integer comparison against zero, result is
9229 * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9230 * subtracting 1.
9232 cond = TCG_COND_LT;
9233 do_cmop:
9234 tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9235 tcg_gen_neg_i64(tcg_rd, tcg_rd);
9236 break;
9237 case 0x8: /* CMGT, CMGE */
9238 cond = u ? TCG_COND_GE : TCG_COND_GT;
9239 goto do_cmop;
9240 case 0x9: /* CMEQ, CMLE */
9241 cond = u ? TCG_COND_LE : TCG_COND_EQ;
9242 goto do_cmop;
9243 case 0xb: /* ABS, NEG */
9244 if (u) {
9245 tcg_gen_neg_i64(tcg_rd, tcg_rn);
9246 } else {
9247 TCGv_i64 tcg_zero = tcg_const_i64(0);
9248 tcg_gen_neg_i64(tcg_rd, tcg_rn);
9249 tcg_gen_movcond_i64(TCG_COND_GT, tcg_rd, tcg_rn, tcg_zero,
9250 tcg_rn, tcg_rd);
9251 tcg_temp_free_i64(tcg_zero);
9253 break;
9254 case 0x2f: /* FABS */
9255 gen_helper_vfp_absd(tcg_rd, tcg_rn);
9256 break;
9257 case 0x6f: /* FNEG */
9258 gen_helper_vfp_negd(tcg_rd, tcg_rn);
9259 break;
9260 case 0x7f: /* FSQRT */
9261 gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9262 break;
9263 case 0x1a: /* FCVTNS */
9264 case 0x1b: /* FCVTMS */
9265 case 0x1c: /* FCVTAS */
9266 case 0x3a: /* FCVTPS */
9267 case 0x3b: /* FCVTZS */
9269 TCGv_i32 tcg_shift = tcg_const_i32(0);
9270 gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
9271 tcg_temp_free_i32(tcg_shift);
9272 break;
9274 case 0x5a: /* FCVTNU */
9275 case 0x5b: /* FCVTMU */
9276 case 0x5c: /* FCVTAU */
9277 case 0x7a: /* FCVTPU */
9278 case 0x7b: /* FCVTZU */
9280 TCGv_i32 tcg_shift = tcg_const_i32(0);
9281 gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
9282 tcg_temp_free_i32(tcg_shift);
9283 break;
9285 case 0x18: /* FRINTN */
9286 case 0x19: /* FRINTM */
9287 case 0x38: /* FRINTP */
9288 case 0x39: /* FRINTZ */
9289 case 0x58: /* FRINTA */
9290 case 0x79: /* FRINTI */
9291 gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9292 break;
9293 case 0x59: /* FRINTX */
9294 gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9295 break;
9296 default:
9297 g_assert_not_reached();
9301 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9302 bool is_scalar, bool is_u, bool is_q,
9303 int size, int rn, int rd)
9305 bool is_double = (size == MO_64);
9306 TCGv_ptr fpst;
9308 if (!fp_access_check(s)) {
9309 return;
9312 fpst = get_fpstatus_ptr(size == MO_16);
9314 if (is_double) {
9315 TCGv_i64 tcg_op = tcg_temp_new_i64();
9316 TCGv_i64 tcg_zero = tcg_const_i64(0);
9317 TCGv_i64 tcg_res = tcg_temp_new_i64();
9318 NeonGenTwoDoubleOPFn *genfn;
9319 bool swap = false;
9320 int pass;
9322 switch (opcode) {
9323 case 0x2e: /* FCMLT (zero) */
9324 swap = true;
9325 /* fallthrough */
9326 case 0x2c: /* FCMGT (zero) */
9327 genfn = gen_helper_neon_cgt_f64;
9328 break;
9329 case 0x2d: /* FCMEQ (zero) */
9330 genfn = gen_helper_neon_ceq_f64;
9331 break;
9332 case 0x6d: /* FCMLE (zero) */
9333 swap = true;
9334 /* fall through */
9335 case 0x6c: /* FCMGE (zero) */
9336 genfn = gen_helper_neon_cge_f64;
9337 break;
9338 default:
9339 g_assert_not_reached();
9342 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9343 read_vec_element(s, tcg_op, rn, pass, MO_64);
9344 if (swap) {
9345 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9346 } else {
9347 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9349 write_vec_element(s, tcg_res, rd, pass, MO_64);
9351 tcg_temp_free_i64(tcg_res);
9352 tcg_temp_free_i64(tcg_zero);
9353 tcg_temp_free_i64(tcg_op);
9355 clear_vec_high(s, !is_scalar, rd);
9356 } else {
9357 TCGv_i32 tcg_op = tcg_temp_new_i32();
9358 TCGv_i32 tcg_zero = tcg_const_i32(0);
9359 TCGv_i32 tcg_res = tcg_temp_new_i32();
9360 NeonGenTwoSingleOPFn *genfn;
9361 bool swap = false;
9362 int pass, maxpasses;
9364 if (size == MO_16) {
9365 switch (opcode) {
9366 case 0x2e: /* FCMLT (zero) */
9367 swap = true;
9368 /* fall through */
9369 case 0x2c: /* FCMGT (zero) */
9370 genfn = gen_helper_advsimd_cgt_f16;
9371 break;
9372 case 0x2d: /* FCMEQ (zero) */
9373 genfn = gen_helper_advsimd_ceq_f16;
9374 break;
9375 case 0x6d: /* FCMLE (zero) */
9376 swap = true;
9377 /* fall through */
9378 case 0x6c: /* FCMGE (zero) */
9379 genfn = gen_helper_advsimd_cge_f16;
9380 break;
9381 default:
9382 g_assert_not_reached();
9384 } else {
9385 switch (opcode) {
9386 case 0x2e: /* FCMLT (zero) */
9387 swap = true;
9388 /* fall through */
9389 case 0x2c: /* FCMGT (zero) */
9390 genfn = gen_helper_neon_cgt_f32;
9391 break;
9392 case 0x2d: /* FCMEQ (zero) */
9393 genfn = gen_helper_neon_ceq_f32;
9394 break;
9395 case 0x6d: /* FCMLE (zero) */
9396 swap = true;
9397 /* fall through */
9398 case 0x6c: /* FCMGE (zero) */
9399 genfn = gen_helper_neon_cge_f32;
9400 break;
9401 default:
9402 g_assert_not_reached();
9406 if (is_scalar) {
9407 maxpasses = 1;
9408 } else {
9409 int vector_size = 8 << is_q;
9410 maxpasses = vector_size >> size;
9413 for (pass = 0; pass < maxpasses; pass++) {
9414 read_vec_element_i32(s, tcg_op, rn, pass, size);
9415 if (swap) {
9416 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9417 } else {
9418 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9420 if (is_scalar) {
9421 write_fp_sreg(s, rd, tcg_res);
9422 } else {
9423 write_vec_element_i32(s, tcg_res, rd, pass, size);
9426 tcg_temp_free_i32(tcg_res);
9427 tcg_temp_free_i32(tcg_zero);
9428 tcg_temp_free_i32(tcg_op);
9429 if (!is_scalar) {
9430 clear_vec_high(s, is_q, rd);
9434 tcg_temp_free_ptr(fpst);
9437 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9438 bool is_scalar, bool is_u, bool is_q,
9439 int size, int rn, int rd)
9441 bool is_double = (size == 3);
9442 TCGv_ptr fpst = get_fpstatus_ptr(false);
9444 if (is_double) {
9445 TCGv_i64 tcg_op = tcg_temp_new_i64();
9446 TCGv_i64 tcg_res = tcg_temp_new_i64();
9447 int pass;
9449 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9450 read_vec_element(s, tcg_op, rn, pass, MO_64);
9451 switch (opcode) {
9452 case 0x3d: /* FRECPE */
9453 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9454 break;
9455 case 0x3f: /* FRECPX */
9456 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9457 break;
9458 case 0x7d: /* FRSQRTE */
9459 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9460 break;
9461 default:
9462 g_assert_not_reached();
9464 write_vec_element(s, tcg_res, rd, pass, MO_64);
9466 tcg_temp_free_i64(tcg_res);
9467 tcg_temp_free_i64(tcg_op);
9468 clear_vec_high(s, !is_scalar, rd);
9469 } else {
9470 TCGv_i32 tcg_op = tcg_temp_new_i32();
9471 TCGv_i32 tcg_res = tcg_temp_new_i32();
9472 int pass, maxpasses;
9474 if (is_scalar) {
9475 maxpasses = 1;
9476 } else {
9477 maxpasses = is_q ? 4 : 2;
9480 for (pass = 0; pass < maxpasses; pass++) {
9481 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9483 switch (opcode) {
9484 case 0x3c: /* URECPE */
9485 gen_helper_recpe_u32(tcg_res, tcg_op, fpst);
9486 break;
9487 case 0x3d: /* FRECPE */
9488 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9489 break;
9490 case 0x3f: /* FRECPX */
9491 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9492 break;
9493 case 0x7d: /* FRSQRTE */
9494 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9495 break;
9496 default:
9497 g_assert_not_reached();
9500 if (is_scalar) {
9501 write_fp_sreg(s, rd, tcg_res);
9502 } else {
9503 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9506 tcg_temp_free_i32(tcg_res);
9507 tcg_temp_free_i32(tcg_op);
9508 if (!is_scalar) {
9509 clear_vec_high(s, is_q, rd);
9512 tcg_temp_free_ptr(fpst);
9515 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9516 int opcode, bool u, bool is_q,
9517 int size, int rn, int rd)
9519 /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9520 * in the source becomes a size element in the destination).
9522 int pass;
9523 TCGv_i32 tcg_res[2];
9524 int destelt = is_q ? 2 : 0;
9525 int passes = scalar ? 1 : 2;
9527 if (scalar) {
9528 tcg_res[1] = tcg_const_i32(0);
9531 for (pass = 0; pass < passes; pass++) {
9532 TCGv_i64 tcg_op = tcg_temp_new_i64();
9533 NeonGenNarrowFn *genfn = NULL;
9534 NeonGenNarrowEnvFn *genenvfn = NULL;
9536 if (scalar) {
9537 read_vec_element(s, tcg_op, rn, pass, size + 1);
9538 } else {
9539 read_vec_element(s, tcg_op, rn, pass, MO_64);
9541 tcg_res[pass] = tcg_temp_new_i32();
9543 switch (opcode) {
9544 case 0x12: /* XTN, SQXTUN */
9546 static NeonGenNarrowFn * const xtnfns[3] = {
9547 gen_helper_neon_narrow_u8,
9548 gen_helper_neon_narrow_u16,
9549 tcg_gen_extrl_i64_i32,
9551 static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9552 gen_helper_neon_unarrow_sat8,
9553 gen_helper_neon_unarrow_sat16,
9554 gen_helper_neon_unarrow_sat32,
9556 if (u) {
9557 genenvfn = sqxtunfns[size];
9558 } else {
9559 genfn = xtnfns[size];
9561 break;
9563 case 0x14: /* SQXTN, UQXTN */
9565 static NeonGenNarrowEnvFn * const fns[3][2] = {
9566 { gen_helper_neon_narrow_sat_s8,
9567 gen_helper_neon_narrow_sat_u8 },
9568 { gen_helper_neon_narrow_sat_s16,
9569 gen_helper_neon_narrow_sat_u16 },
9570 { gen_helper_neon_narrow_sat_s32,
9571 gen_helper_neon_narrow_sat_u32 },
9573 genenvfn = fns[size][u];
9574 break;
9576 case 0x16: /* FCVTN, FCVTN2 */
9577 /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9578 if (size == 2) {
9579 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
9580 } else {
9581 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9582 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9583 TCGv_ptr fpst = get_fpstatus_ptr(false);
9584 TCGv_i32 ahp = get_ahp_flag();
9586 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9587 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9588 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9589 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9590 tcg_temp_free_i32(tcg_lo);
9591 tcg_temp_free_i32(tcg_hi);
9592 tcg_temp_free_ptr(fpst);
9593 tcg_temp_free_i32(ahp);
9595 break;
9596 case 0x56: /* FCVTXN, FCVTXN2 */
9597 /* 64 bit to 32 bit float conversion
9598 * with von Neumann rounding (round to odd)
9600 assert(size == 2);
9601 gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
9602 break;
9603 default:
9604 g_assert_not_reached();
9607 if (genfn) {
9608 genfn(tcg_res[pass], tcg_op);
9609 } else if (genenvfn) {
9610 genenvfn(tcg_res[pass], cpu_env, tcg_op);
9613 tcg_temp_free_i64(tcg_op);
9616 for (pass = 0; pass < 2; pass++) {
9617 write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
9618 tcg_temp_free_i32(tcg_res[pass]);
9620 clear_vec_high(s, is_q, rd);
9623 /* Remaining saturating accumulating ops */
9624 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
9625 bool is_q, int size, int rn, int rd)
9627 bool is_double = (size == 3);
9629 if (is_double) {
9630 TCGv_i64 tcg_rn = tcg_temp_new_i64();
9631 TCGv_i64 tcg_rd = tcg_temp_new_i64();
9632 int pass;
9634 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9635 read_vec_element(s, tcg_rn, rn, pass, MO_64);
9636 read_vec_element(s, tcg_rd, rd, pass, MO_64);
9638 if (is_u) { /* USQADD */
9639 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9640 } else { /* SUQADD */
9641 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9643 write_vec_element(s, tcg_rd, rd, pass, MO_64);
9645 tcg_temp_free_i64(tcg_rd);
9646 tcg_temp_free_i64(tcg_rn);
9647 clear_vec_high(s, !is_scalar, rd);
9648 } else {
9649 TCGv_i32 tcg_rn = tcg_temp_new_i32();
9650 TCGv_i32 tcg_rd = tcg_temp_new_i32();
9651 int pass, maxpasses;
9653 if (is_scalar) {
9654 maxpasses = 1;
9655 } else {
9656 maxpasses = is_q ? 4 : 2;
9659 for (pass = 0; pass < maxpasses; pass++) {
9660 if (is_scalar) {
9661 read_vec_element_i32(s, tcg_rn, rn, pass, size);
9662 read_vec_element_i32(s, tcg_rd, rd, pass, size);
9663 } else {
9664 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
9665 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9668 if (is_u) { /* USQADD */
9669 switch (size) {
9670 case 0:
9671 gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9672 break;
9673 case 1:
9674 gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9675 break;
9676 case 2:
9677 gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9678 break;
9679 default:
9680 g_assert_not_reached();
9682 } else { /* SUQADD */
9683 switch (size) {
9684 case 0:
9685 gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9686 break;
9687 case 1:
9688 gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9689 break;
9690 case 2:
9691 gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9692 break;
9693 default:
9694 g_assert_not_reached();
9698 if (is_scalar) {
9699 TCGv_i64 tcg_zero = tcg_const_i64(0);
9700 write_vec_element(s, tcg_zero, rd, 0, MO_64);
9701 tcg_temp_free_i64(tcg_zero);
9703 write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9705 tcg_temp_free_i32(tcg_rd);
9706 tcg_temp_free_i32(tcg_rn);
9707 clear_vec_high(s, is_q, rd);
9711 /* AdvSIMD scalar two reg misc
9712 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
9713 * +-----+---+-----------+------+-----------+--------+-----+------+------+
9714 * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
9715 * +-----+---+-----------+------+-----------+--------+-----+------+------+
9717 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
9719 int rd = extract32(insn, 0, 5);
9720 int rn = extract32(insn, 5, 5);
9721 int opcode = extract32(insn, 12, 5);
9722 int size = extract32(insn, 22, 2);
9723 bool u = extract32(insn, 29, 1);
9724 bool is_fcvt = false;
9725 int rmode;
9726 TCGv_i32 tcg_rmode;
9727 TCGv_ptr tcg_fpstatus;
9729 switch (opcode) {
9730 case 0x3: /* USQADD / SUQADD*/
9731 if (!fp_access_check(s)) {
9732 return;
9734 handle_2misc_satacc(s, true, u, false, size, rn, rd);
9735 return;
9736 case 0x7: /* SQABS / SQNEG */
9737 break;
9738 case 0xa: /* CMLT */
9739 if (u) {
9740 unallocated_encoding(s);
9741 return;
9743 /* fall through */
9744 case 0x8: /* CMGT, CMGE */
9745 case 0x9: /* CMEQ, CMLE */
9746 case 0xb: /* ABS, NEG */
9747 if (size != 3) {
9748 unallocated_encoding(s);
9749 return;
9751 break;
9752 case 0x12: /* SQXTUN */
9753 if (!u) {
9754 unallocated_encoding(s);
9755 return;
9757 /* fall through */
9758 case 0x14: /* SQXTN, UQXTN */
9759 if (size == 3) {
9760 unallocated_encoding(s);
9761 return;
9763 if (!fp_access_check(s)) {
9764 return;
9766 handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
9767 return;
9768 case 0xc ... 0xf:
9769 case 0x16 ... 0x1d:
9770 case 0x1f:
9771 /* Floating point: U, size[1] and opcode indicate operation;
9772 * size[0] indicates single or double precision.
9774 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
9775 size = extract32(size, 0, 1) ? 3 : 2;
9776 switch (opcode) {
9777 case 0x2c: /* FCMGT (zero) */
9778 case 0x2d: /* FCMEQ (zero) */
9779 case 0x2e: /* FCMLT (zero) */
9780 case 0x6c: /* FCMGE (zero) */
9781 case 0x6d: /* FCMLE (zero) */
9782 handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
9783 return;
9784 case 0x1d: /* SCVTF */
9785 case 0x5d: /* UCVTF */
9787 bool is_signed = (opcode == 0x1d);
9788 if (!fp_access_check(s)) {
9789 return;
9791 handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
9792 return;
9794 case 0x3d: /* FRECPE */
9795 case 0x3f: /* FRECPX */
9796 case 0x7d: /* FRSQRTE */
9797 if (!fp_access_check(s)) {
9798 return;
9800 handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
9801 return;
9802 case 0x1a: /* FCVTNS */
9803 case 0x1b: /* FCVTMS */
9804 case 0x3a: /* FCVTPS */
9805 case 0x3b: /* FCVTZS */
9806 case 0x5a: /* FCVTNU */
9807 case 0x5b: /* FCVTMU */
9808 case 0x7a: /* FCVTPU */
9809 case 0x7b: /* FCVTZU */
9810 is_fcvt = true;
9811 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
9812 break;
9813 case 0x1c: /* FCVTAS */
9814 case 0x5c: /* FCVTAU */
9815 /* TIEAWAY doesn't fit in the usual rounding mode encoding */
9816 is_fcvt = true;
9817 rmode = FPROUNDING_TIEAWAY;
9818 break;
9819 case 0x56: /* FCVTXN, FCVTXN2 */
9820 if (size == 2) {
9821 unallocated_encoding(s);
9822 return;
9824 if (!fp_access_check(s)) {
9825 return;
9827 handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
9828 return;
9829 default:
9830 unallocated_encoding(s);
9831 return;
9833 break;
9834 default:
9835 unallocated_encoding(s);
9836 return;
9839 if (!fp_access_check(s)) {
9840 return;
9843 if (is_fcvt) {
9844 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
9845 tcg_fpstatus = get_fpstatus_ptr(false);
9846 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
9847 } else {
9848 tcg_rmode = NULL;
9849 tcg_fpstatus = NULL;
9852 if (size == 3) {
9853 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9854 TCGv_i64 tcg_rd = tcg_temp_new_i64();
9856 handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
9857 write_fp_dreg(s, rd, tcg_rd);
9858 tcg_temp_free_i64(tcg_rd);
9859 tcg_temp_free_i64(tcg_rn);
9860 } else {
9861 TCGv_i32 tcg_rn = tcg_temp_new_i32();
9862 TCGv_i32 tcg_rd = tcg_temp_new_i32();
9864 read_vec_element_i32(s, tcg_rn, rn, 0, size);
9866 switch (opcode) {
9867 case 0x7: /* SQABS, SQNEG */
9869 NeonGenOneOpEnvFn *genfn;
9870 static NeonGenOneOpEnvFn * const fns[3][2] = {
9871 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
9872 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
9873 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
9875 genfn = fns[size][u];
9876 genfn(tcg_rd, cpu_env, tcg_rn);
9877 break;
9879 case 0x1a: /* FCVTNS */
9880 case 0x1b: /* FCVTMS */
9881 case 0x1c: /* FCVTAS */
9882 case 0x3a: /* FCVTPS */
9883 case 0x3b: /* FCVTZS */
9885 TCGv_i32 tcg_shift = tcg_const_i32(0);
9886 gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
9887 tcg_temp_free_i32(tcg_shift);
9888 break;
9890 case 0x5a: /* FCVTNU */
9891 case 0x5b: /* FCVTMU */
9892 case 0x5c: /* FCVTAU */
9893 case 0x7a: /* FCVTPU */
9894 case 0x7b: /* FCVTZU */
9896 TCGv_i32 tcg_shift = tcg_const_i32(0);
9897 gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
9898 tcg_temp_free_i32(tcg_shift);
9899 break;
9901 default:
9902 g_assert_not_reached();
9905 write_fp_sreg(s, rd, tcg_rd);
9906 tcg_temp_free_i32(tcg_rd);
9907 tcg_temp_free_i32(tcg_rn);
9910 if (is_fcvt) {
9911 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
9912 tcg_temp_free_i32(tcg_rmode);
9913 tcg_temp_free_ptr(tcg_fpstatus);
9917 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
9918 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
9919 int immh, int immb, int opcode, int rn, int rd)
9921 int size = 32 - clz32(immh) - 1;
9922 int immhb = immh << 3 | immb;
9923 int shift = 2 * (8 << size) - immhb;
9924 bool accumulate = false;
9925 int dsize = is_q ? 128 : 64;
9926 int esize = 8 << size;
9927 int elements = dsize/esize;
9928 TCGMemOp memop = size | (is_u ? 0 : MO_SIGN);
9929 TCGv_i64 tcg_rn = new_tmp_a64(s);
9930 TCGv_i64 tcg_rd = new_tmp_a64(s);
9931 TCGv_i64 tcg_round;
9932 uint64_t round_const;
9933 int i;
9935 if (extract32(immh, 3, 1) && !is_q) {
9936 unallocated_encoding(s);
9937 return;
9939 tcg_debug_assert(size <= 3);
9941 if (!fp_access_check(s)) {
9942 return;
9945 switch (opcode) {
9946 case 0x02: /* SSRA / USRA (accumulate) */
9947 if (is_u) {
9948 /* Shift count same as element size produces zero to add. */
9949 if (shift == 8 << size) {
9950 goto done;
9952 gen_gvec_op2i(s, is_q, rd, rn, shift, &usra_op[size]);
9953 } else {
9954 /* Shift count same as element size produces all sign to add. */
9955 if (shift == 8 << size) {
9956 shift -= 1;
9958 gen_gvec_op2i(s, is_q, rd, rn, shift, &ssra_op[size]);
9960 return;
9961 case 0x08: /* SRI */
9962 /* Shift count same as element size is valid but does nothing. */
9963 if (shift == 8 << size) {
9964 goto done;
9966 gen_gvec_op2i(s, is_q, rd, rn, shift, &sri_op[size]);
9967 return;
9969 case 0x00: /* SSHR / USHR */
9970 if (is_u) {
9971 if (shift == 8 << size) {
9972 /* Shift count the same size as element size produces zero. */
9973 tcg_gen_gvec_dup8i(vec_full_reg_offset(s, rd),
9974 is_q ? 16 : 8, vec_full_reg_size(s), 0);
9975 } else {
9976 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shri, size);
9978 } else {
9979 /* Shift count the same size as element size produces all sign. */
9980 if (shift == 8 << size) {
9981 shift -= 1;
9983 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_sari, size);
9985 return;
9987 case 0x04: /* SRSHR / URSHR (rounding) */
9988 break;
9989 case 0x06: /* SRSRA / URSRA (accum + rounding) */
9990 accumulate = true;
9991 break;
9992 default:
9993 g_assert_not_reached();
9996 round_const = 1ULL << (shift - 1);
9997 tcg_round = tcg_const_i64(round_const);
9999 for (i = 0; i < elements; i++) {
10000 read_vec_element(s, tcg_rn, rn, i, memop);
10001 if (accumulate) {
10002 read_vec_element(s, tcg_rd, rd, i, memop);
10005 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10006 accumulate, is_u, size, shift);
10008 write_vec_element(s, tcg_rd, rd, i, size);
10010 tcg_temp_free_i64(tcg_round);
10012 done:
10013 clear_vec_high(s, is_q, rd);
10016 /* SHL/SLI - Vector shift left */
10017 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10018 int immh, int immb, int opcode, int rn, int rd)
10020 int size = 32 - clz32(immh) - 1;
10021 int immhb = immh << 3 | immb;
10022 int shift = immhb - (8 << size);
10024 /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10025 assert(size >= 0 && size <= 3);
10027 if (extract32(immh, 3, 1) && !is_q) {
10028 unallocated_encoding(s);
10029 return;
10032 if (!fp_access_check(s)) {
10033 return;
10036 if (insert) {
10037 gen_gvec_op2i(s, is_q, rd, rn, shift, &sli_op[size]);
10038 } else {
10039 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10043 /* USHLL/SHLL - Vector shift left with widening */
10044 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10045 int immh, int immb, int opcode, int rn, int rd)
10047 int size = 32 - clz32(immh) - 1;
10048 int immhb = immh << 3 | immb;
10049 int shift = immhb - (8 << size);
10050 int dsize = 64;
10051 int esize = 8 << size;
10052 int elements = dsize/esize;
10053 TCGv_i64 tcg_rn = new_tmp_a64(s);
10054 TCGv_i64 tcg_rd = new_tmp_a64(s);
10055 int i;
10057 if (size >= 3) {
10058 unallocated_encoding(s);
10059 return;
10062 if (!fp_access_check(s)) {
10063 return;
10066 /* For the LL variants the store is larger than the load,
10067 * so if rd == rn we would overwrite parts of our input.
10068 * So load everything right now and use shifts in the main loop.
10070 read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10072 for (i = 0; i < elements; i++) {
10073 tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10074 ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10075 tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10076 write_vec_element(s, tcg_rd, rd, i, size + 1);
10080 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10081 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10082 int immh, int immb, int opcode, int rn, int rd)
10084 int immhb = immh << 3 | immb;
10085 int size = 32 - clz32(immh) - 1;
10086 int dsize = 64;
10087 int esize = 8 << size;
10088 int elements = dsize/esize;
10089 int shift = (2 * esize) - immhb;
10090 bool round = extract32(opcode, 0, 1);
10091 TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10092 TCGv_i64 tcg_round;
10093 int i;
10095 if (extract32(immh, 3, 1)) {
10096 unallocated_encoding(s);
10097 return;
10100 if (!fp_access_check(s)) {
10101 return;
10104 tcg_rn = tcg_temp_new_i64();
10105 tcg_rd = tcg_temp_new_i64();
10106 tcg_final = tcg_temp_new_i64();
10107 read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10109 if (round) {
10110 uint64_t round_const = 1ULL << (shift - 1);
10111 tcg_round = tcg_const_i64(round_const);
10112 } else {
10113 tcg_round = NULL;
10116 for (i = 0; i < elements; i++) {
10117 read_vec_element(s, tcg_rn, rn, i, size+1);
10118 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10119 false, true, size+1, shift);
10121 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10124 if (!is_q) {
10125 write_vec_element(s, tcg_final, rd, 0, MO_64);
10126 } else {
10127 write_vec_element(s, tcg_final, rd, 1, MO_64);
10129 if (round) {
10130 tcg_temp_free_i64(tcg_round);
10132 tcg_temp_free_i64(tcg_rn);
10133 tcg_temp_free_i64(tcg_rd);
10134 tcg_temp_free_i64(tcg_final);
10136 clear_vec_high(s, is_q, rd);
10140 /* AdvSIMD shift by immediate
10141 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
10142 * +---+---+---+-------------+------+------+--------+---+------+------+
10143 * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
10144 * +---+---+---+-------------+------+------+--------+---+------+------+
10146 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10148 int rd = extract32(insn, 0, 5);
10149 int rn = extract32(insn, 5, 5);
10150 int opcode = extract32(insn, 11, 5);
10151 int immb = extract32(insn, 16, 3);
10152 int immh = extract32(insn, 19, 4);
10153 bool is_u = extract32(insn, 29, 1);
10154 bool is_q = extract32(insn, 30, 1);
10156 switch (opcode) {
10157 case 0x08: /* SRI */
10158 if (!is_u) {
10159 unallocated_encoding(s);
10160 return;
10162 /* fall through */
10163 case 0x00: /* SSHR / USHR */
10164 case 0x02: /* SSRA / USRA (accumulate) */
10165 case 0x04: /* SRSHR / URSHR (rounding) */
10166 case 0x06: /* SRSRA / URSRA (accum + rounding) */
10167 handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10168 break;
10169 case 0x0a: /* SHL / SLI */
10170 handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10171 break;
10172 case 0x10: /* SHRN */
10173 case 0x11: /* RSHRN / SQRSHRUN */
10174 if (is_u) {
10175 handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10176 opcode, rn, rd);
10177 } else {
10178 handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10180 break;
10181 case 0x12: /* SQSHRN / UQSHRN */
10182 case 0x13: /* SQRSHRN / UQRSHRN */
10183 handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10184 opcode, rn, rd);
10185 break;
10186 case 0x14: /* SSHLL / USHLL */
10187 handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10188 break;
10189 case 0x1c: /* SCVTF / UCVTF */
10190 handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10191 opcode, rn, rd);
10192 break;
10193 case 0xc: /* SQSHLU */
10194 if (!is_u) {
10195 unallocated_encoding(s);
10196 return;
10198 handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10199 break;
10200 case 0xe: /* SQSHL, UQSHL */
10201 handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10202 break;
10203 case 0x1f: /* FCVTZS/ FCVTZU */
10204 handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10205 return;
10206 default:
10207 unallocated_encoding(s);
10208 return;
10212 /* Generate code to do a "long" addition or subtraction, ie one done in
10213 * TCGv_i64 on vector lanes twice the width specified by size.
10215 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10216 TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10218 static NeonGenTwo64OpFn * const fns[3][2] = {
10219 { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10220 { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10221 { tcg_gen_add_i64, tcg_gen_sub_i64 },
10223 NeonGenTwo64OpFn *genfn;
10224 assert(size < 3);
10226 genfn = fns[size][is_sub];
10227 genfn(tcg_res, tcg_op1, tcg_op2);
10230 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10231 int opcode, int rd, int rn, int rm)
10233 /* 3-reg-different widening insns: 64 x 64 -> 128 */
10234 TCGv_i64 tcg_res[2];
10235 int pass, accop;
10237 tcg_res[0] = tcg_temp_new_i64();
10238 tcg_res[1] = tcg_temp_new_i64();
10240 /* Does this op do an adding accumulate, a subtracting accumulate,
10241 * or no accumulate at all?
10243 switch (opcode) {
10244 case 5:
10245 case 8:
10246 case 9:
10247 accop = 1;
10248 break;
10249 case 10:
10250 case 11:
10251 accop = -1;
10252 break;
10253 default:
10254 accop = 0;
10255 break;
10258 if (accop != 0) {
10259 read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10260 read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10263 /* size == 2 means two 32x32->64 operations; this is worth special
10264 * casing because we can generally handle it inline.
10266 if (size == 2) {
10267 for (pass = 0; pass < 2; pass++) {
10268 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10269 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10270 TCGv_i64 tcg_passres;
10271 TCGMemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10273 int elt = pass + is_q * 2;
10275 read_vec_element(s, tcg_op1, rn, elt, memop);
10276 read_vec_element(s, tcg_op2, rm, elt, memop);
10278 if (accop == 0) {
10279 tcg_passres = tcg_res[pass];
10280 } else {
10281 tcg_passres = tcg_temp_new_i64();
10284 switch (opcode) {
10285 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10286 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10287 break;
10288 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10289 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10290 break;
10291 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10292 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10294 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10295 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10297 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10298 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10299 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10300 tcg_passres,
10301 tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10302 tcg_temp_free_i64(tcg_tmp1);
10303 tcg_temp_free_i64(tcg_tmp2);
10304 break;
10306 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10307 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10308 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10309 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10310 break;
10311 case 9: /* SQDMLAL, SQDMLAL2 */
10312 case 11: /* SQDMLSL, SQDMLSL2 */
10313 case 13: /* SQDMULL, SQDMULL2 */
10314 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10315 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10316 tcg_passres, tcg_passres);
10317 break;
10318 default:
10319 g_assert_not_reached();
10322 if (opcode == 9 || opcode == 11) {
10323 /* saturating accumulate ops */
10324 if (accop < 0) {
10325 tcg_gen_neg_i64(tcg_passres, tcg_passres);
10327 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10328 tcg_res[pass], tcg_passres);
10329 } else if (accop > 0) {
10330 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10331 } else if (accop < 0) {
10332 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10335 if (accop != 0) {
10336 tcg_temp_free_i64(tcg_passres);
10339 tcg_temp_free_i64(tcg_op1);
10340 tcg_temp_free_i64(tcg_op2);
10342 } else {
10343 /* size 0 or 1, generally helper functions */
10344 for (pass = 0; pass < 2; pass++) {
10345 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10346 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10347 TCGv_i64 tcg_passres;
10348 int elt = pass + is_q * 2;
10350 read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10351 read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10353 if (accop == 0) {
10354 tcg_passres = tcg_res[pass];
10355 } else {
10356 tcg_passres = tcg_temp_new_i64();
10359 switch (opcode) {
10360 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10361 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10363 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10364 static NeonGenWidenFn * const widenfns[2][2] = {
10365 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10366 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10368 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10370 widenfn(tcg_op2_64, tcg_op2);
10371 widenfn(tcg_passres, tcg_op1);
10372 gen_neon_addl(size, (opcode == 2), tcg_passres,
10373 tcg_passres, tcg_op2_64);
10374 tcg_temp_free_i64(tcg_op2_64);
10375 break;
10377 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10378 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10379 if (size == 0) {
10380 if (is_u) {
10381 gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10382 } else {
10383 gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10385 } else {
10386 if (is_u) {
10387 gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10388 } else {
10389 gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10392 break;
10393 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10394 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10395 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10396 if (size == 0) {
10397 if (is_u) {
10398 gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10399 } else {
10400 gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10402 } else {
10403 if (is_u) {
10404 gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10405 } else {
10406 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10409 break;
10410 case 9: /* SQDMLAL, SQDMLAL2 */
10411 case 11: /* SQDMLSL, SQDMLSL2 */
10412 case 13: /* SQDMULL, SQDMULL2 */
10413 assert(size == 1);
10414 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10415 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10416 tcg_passres, tcg_passres);
10417 break;
10418 case 14: /* PMULL */
10419 assert(size == 0);
10420 gen_helper_neon_mull_p8(tcg_passres, tcg_op1, tcg_op2);
10421 break;
10422 default:
10423 g_assert_not_reached();
10425 tcg_temp_free_i32(tcg_op1);
10426 tcg_temp_free_i32(tcg_op2);
10428 if (accop != 0) {
10429 if (opcode == 9 || opcode == 11) {
10430 /* saturating accumulate ops */
10431 if (accop < 0) {
10432 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10434 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10435 tcg_res[pass],
10436 tcg_passres);
10437 } else {
10438 gen_neon_addl(size, (accop < 0), tcg_res[pass],
10439 tcg_res[pass], tcg_passres);
10441 tcg_temp_free_i64(tcg_passres);
10446 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10447 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10448 tcg_temp_free_i64(tcg_res[0]);
10449 tcg_temp_free_i64(tcg_res[1]);
10452 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10453 int opcode, int rd, int rn, int rm)
10455 TCGv_i64 tcg_res[2];
10456 int part = is_q ? 2 : 0;
10457 int pass;
10459 for (pass = 0; pass < 2; pass++) {
10460 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10461 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10462 TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10463 static NeonGenWidenFn * const widenfns[3][2] = {
10464 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10465 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10466 { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10468 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10470 read_vec_element(s, tcg_op1, rn, pass, MO_64);
10471 read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10472 widenfn(tcg_op2_wide, tcg_op2);
10473 tcg_temp_free_i32(tcg_op2);
10474 tcg_res[pass] = tcg_temp_new_i64();
10475 gen_neon_addl(size, (opcode == 3),
10476 tcg_res[pass], tcg_op1, tcg_op2_wide);
10477 tcg_temp_free_i64(tcg_op1);
10478 tcg_temp_free_i64(tcg_op2_wide);
10481 for (pass = 0; pass < 2; pass++) {
10482 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10483 tcg_temp_free_i64(tcg_res[pass]);
10487 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10489 tcg_gen_addi_i64(in, in, 1U << 31);
10490 tcg_gen_extrh_i64_i32(res, in);
10493 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10494 int opcode, int rd, int rn, int rm)
10496 TCGv_i32 tcg_res[2];
10497 int part = is_q ? 2 : 0;
10498 int pass;
10500 for (pass = 0; pass < 2; pass++) {
10501 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10502 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10503 TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10504 static NeonGenNarrowFn * const narrowfns[3][2] = {
10505 { gen_helper_neon_narrow_high_u8,
10506 gen_helper_neon_narrow_round_high_u8 },
10507 { gen_helper_neon_narrow_high_u16,
10508 gen_helper_neon_narrow_round_high_u16 },
10509 { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10511 NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10513 read_vec_element(s, tcg_op1, rn, pass, MO_64);
10514 read_vec_element(s, tcg_op2, rm, pass, MO_64);
10516 gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10518 tcg_temp_free_i64(tcg_op1);
10519 tcg_temp_free_i64(tcg_op2);
10521 tcg_res[pass] = tcg_temp_new_i32();
10522 gennarrow(tcg_res[pass], tcg_wideres);
10523 tcg_temp_free_i64(tcg_wideres);
10526 for (pass = 0; pass < 2; pass++) {
10527 write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10528 tcg_temp_free_i32(tcg_res[pass]);
10530 clear_vec_high(s, is_q, rd);
10533 static void handle_pmull_64(DisasContext *s, int is_q, int rd, int rn, int rm)
10535 /* PMULL of 64 x 64 -> 128 is an odd special case because it
10536 * is the only three-reg-diff instruction which produces a
10537 * 128-bit wide result from a single operation. However since
10538 * it's possible to calculate the two halves more or less
10539 * separately we just use two helper calls.
10541 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10542 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10543 TCGv_i64 tcg_res = tcg_temp_new_i64();
10545 read_vec_element(s, tcg_op1, rn, is_q, MO_64);
10546 read_vec_element(s, tcg_op2, rm, is_q, MO_64);
10547 gen_helper_neon_pmull_64_lo(tcg_res, tcg_op1, tcg_op2);
10548 write_vec_element(s, tcg_res, rd, 0, MO_64);
10549 gen_helper_neon_pmull_64_hi(tcg_res, tcg_op1, tcg_op2);
10550 write_vec_element(s, tcg_res, rd, 1, MO_64);
10552 tcg_temp_free_i64(tcg_op1);
10553 tcg_temp_free_i64(tcg_op2);
10554 tcg_temp_free_i64(tcg_res);
10557 /* AdvSIMD three different
10558 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
10559 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10560 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
10561 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10563 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10565 /* Instructions in this group fall into three basic classes
10566 * (in each case with the operation working on each element in
10567 * the input vectors):
10568 * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10569 * 128 bit input)
10570 * (2) wide 64 x 128 -> 128
10571 * (3) narrowing 128 x 128 -> 64
10572 * Here we do initial decode, catch unallocated cases and
10573 * dispatch to separate functions for each class.
10575 int is_q = extract32(insn, 30, 1);
10576 int is_u = extract32(insn, 29, 1);
10577 int size = extract32(insn, 22, 2);
10578 int opcode = extract32(insn, 12, 4);
10579 int rm = extract32(insn, 16, 5);
10580 int rn = extract32(insn, 5, 5);
10581 int rd = extract32(insn, 0, 5);
10583 switch (opcode) {
10584 case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10585 case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10586 /* 64 x 128 -> 128 */
10587 if (size == 3) {
10588 unallocated_encoding(s);
10589 return;
10591 if (!fp_access_check(s)) {
10592 return;
10594 handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10595 break;
10596 case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10597 case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10598 /* 128 x 128 -> 64 */
10599 if (size == 3) {
10600 unallocated_encoding(s);
10601 return;
10603 if (!fp_access_check(s)) {
10604 return;
10606 handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10607 break;
10608 case 14: /* PMULL, PMULL2 */
10609 if (is_u || size == 1 || size == 2) {
10610 unallocated_encoding(s);
10611 return;
10613 if (size == 3) {
10614 if (!dc_isar_feature(aa64_pmull, s)) {
10615 unallocated_encoding(s);
10616 return;
10618 if (!fp_access_check(s)) {
10619 return;
10621 handle_pmull_64(s, is_q, rd, rn, rm);
10622 return;
10624 goto is_widening;
10625 case 9: /* SQDMLAL, SQDMLAL2 */
10626 case 11: /* SQDMLSL, SQDMLSL2 */
10627 case 13: /* SQDMULL, SQDMULL2 */
10628 if (is_u || size == 0) {
10629 unallocated_encoding(s);
10630 return;
10632 /* fall through */
10633 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10634 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10635 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10636 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10637 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10638 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10639 case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10640 /* 64 x 64 -> 128 */
10641 if (size == 3) {
10642 unallocated_encoding(s);
10643 return;
10645 is_widening:
10646 if (!fp_access_check(s)) {
10647 return;
10650 handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10651 break;
10652 default:
10653 /* opcode 15 not allocated */
10654 unallocated_encoding(s);
10655 break;
10659 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10660 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10662 int rd = extract32(insn, 0, 5);
10663 int rn = extract32(insn, 5, 5);
10664 int rm = extract32(insn, 16, 5);
10665 int size = extract32(insn, 22, 2);
10666 bool is_u = extract32(insn, 29, 1);
10667 bool is_q = extract32(insn, 30, 1);
10669 if (!fp_access_check(s)) {
10670 return;
10673 switch (size + 4 * is_u) {
10674 case 0: /* AND */
10675 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10676 return;
10677 case 1: /* BIC */
10678 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10679 return;
10680 case 2: /* ORR */
10681 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10682 return;
10683 case 3: /* ORN */
10684 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10685 return;
10686 case 4: /* EOR */
10687 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10688 return;
10690 case 5: /* BSL bitwise select */
10691 gen_gvec_op3(s, is_q, rd, rn, rm, &bsl_op);
10692 return;
10693 case 6: /* BIT, bitwise insert if true */
10694 gen_gvec_op3(s, is_q, rd, rn, rm, &bit_op);
10695 return;
10696 case 7: /* BIF, bitwise insert if false */
10697 gen_gvec_op3(s, is_q, rd, rn, rm, &bif_op);
10698 return;
10700 default:
10701 g_assert_not_reached();
10705 /* Pairwise op subgroup of C3.6.16.
10707 * This is called directly or via the handle_3same_float for float pairwise
10708 * operations where the opcode and size are calculated differently.
10710 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
10711 int size, int rn, int rm, int rd)
10713 TCGv_ptr fpst;
10714 int pass;
10716 /* Floating point operations need fpst */
10717 if (opcode >= 0x58) {
10718 fpst = get_fpstatus_ptr(false);
10719 } else {
10720 fpst = NULL;
10723 if (!fp_access_check(s)) {
10724 return;
10727 /* These operations work on the concatenated rm:rn, with each pair of
10728 * adjacent elements being operated on to produce an element in the result.
10730 if (size == 3) {
10731 TCGv_i64 tcg_res[2];
10733 for (pass = 0; pass < 2; pass++) {
10734 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10735 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10736 int passreg = (pass == 0) ? rn : rm;
10738 read_vec_element(s, tcg_op1, passreg, 0, MO_64);
10739 read_vec_element(s, tcg_op2, passreg, 1, MO_64);
10740 tcg_res[pass] = tcg_temp_new_i64();
10742 switch (opcode) {
10743 case 0x17: /* ADDP */
10744 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
10745 break;
10746 case 0x58: /* FMAXNMP */
10747 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10748 break;
10749 case 0x5a: /* FADDP */
10750 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10751 break;
10752 case 0x5e: /* FMAXP */
10753 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10754 break;
10755 case 0x78: /* FMINNMP */
10756 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10757 break;
10758 case 0x7e: /* FMINP */
10759 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10760 break;
10761 default:
10762 g_assert_not_reached();
10765 tcg_temp_free_i64(tcg_op1);
10766 tcg_temp_free_i64(tcg_op2);
10769 for (pass = 0; pass < 2; pass++) {
10770 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10771 tcg_temp_free_i64(tcg_res[pass]);
10773 } else {
10774 int maxpass = is_q ? 4 : 2;
10775 TCGv_i32 tcg_res[4];
10777 for (pass = 0; pass < maxpass; pass++) {
10778 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10779 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10780 NeonGenTwoOpFn *genfn = NULL;
10781 int passreg = pass < (maxpass / 2) ? rn : rm;
10782 int passelt = (is_q && (pass & 1)) ? 2 : 0;
10784 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
10785 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
10786 tcg_res[pass] = tcg_temp_new_i32();
10788 switch (opcode) {
10789 case 0x17: /* ADDP */
10791 static NeonGenTwoOpFn * const fns[3] = {
10792 gen_helper_neon_padd_u8,
10793 gen_helper_neon_padd_u16,
10794 tcg_gen_add_i32,
10796 genfn = fns[size];
10797 break;
10799 case 0x14: /* SMAXP, UMAXP */
10801 static NeonGenTwoOpFn * const fns[3][2] = {
10802 { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
10803 { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
10804 { tcg_gen_smax_i32, tcg_gen_umax_i32 },
10806 genfn = fns[size][u];
10807 break;
10809 case 0x15: /* SMINP, UMINP */
10811 static NeonGenTwoOpFn * const fns[3][2] = {
10812 { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
10813 { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
10814 { tcg_gen_smin_i32, tcg_gen_umin_i32 },
10816 genfn = fns[size][u];
10817 break;
10819 /* The FP operations are all on single floats (32 bit) */
10820 case 0x58: /* FMAXNMP */
10821 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10822 break;
10823 case 0x5a: /* FADDP */
10824 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10825 break;
10826 case 0x5e: /* FMAXP */
10827 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10828 break;
10829 case 0x78: /* FMINNMP */
10830 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10831 break;
10832 case 0x7e: /* FMINP */
10833 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10834 break;
10835 default:
10836 g_assert_not_reached();
10839 /* FP ops called directly, otherwise call now */
10840 if (genfn) {
10841 genfn(tcg_res[pass], tcg_op1, tcg_op2);
10844 tcg_temp_free_i32(tcg_op1);
10845 tcg_temp_free_i32(tcg_op2);
10848 for (pass = 0; pass < maxpass; pass++) {
10849 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
10850 tcg_temp_free_i32(tcg_res[pass]);
10852 clear_vec_high(s, is_q, rd);
10855 if (fpst) {
10856 tcg_temp_free_ptr(fpst);
10860 /* Floating point op subgroup of C3.6.16. */
10861 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
10863 /* For floating point ops, the U, size[1] and opcode bits
10864 * together indicate the operation. size[0] indicates single
10865 * or double.
10867 int fpopcode = extract32(insn, 11, 5)
10868 | (extract32(insn, 23, 1) << 5)
10869 | (extract32(insn, 29, 1) << 6);
10870 int is_q = extract32(insn, 30, 1);
10871 int size = extract32(insn, 22, 1);
10872 int rm = extract32(insn, 16, 5);
10873 int rn = extract32(insn, 5, 5);
10874 int rd = extract32(insn, 0, 5);
10876 int datasize = is_q ? 128 : 64;
10877 int esize = 32 << size;
10878 int elements = datasize / esize;
10880 if (size == 1 && !is_q) {
10881 unallocated_encoding(s);
10882 return;
10885 switch (fpopcode) {
10886 case 0x58: /* FMAXNMP */
10887 case 0x5a: /* FADDP */
10888 case 0x5e: /* FMAXP */
10889 case 0x78: /* FMINNMP */
10890 case 0x7e: /* FMINP */
10891 if (size && !is_q) {
10892 unallocated_encoding(s);
10893 return;
10895 handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
10896 rn, rm, rd);
10897 return;
10898 case 0x1b: /* FMULX */
10899 case 0x1f: /* FRECPS */
10900 case 0x3f: /* FRSQRTS */
10901 case 0x5d: /* FACGE */
10902 case 0x7d: /* FACGT */
10903 case 0x19: /* FMLA */
10904 case 0x39: /* FMLS */
10905 case 0x18: /* FMAXNM */
10906 case 0x1a: /* FADD */
10907 case 0x1c: /* FCMEQ */
10908 case 0x1e: /* FMAX */
10909 case 0x38: /* FMINNM */
10910 case 0x3a: /* FSUB */
10911 case 0x3e: /* FMIN */
10912 case 0x5b: /* FMUL */
10913 case 0x5c: /* FCMGE */
10914 case 0x5f: /* FDIV */
10915 case 0x7a: /* FABD */
10916 case 0x7c: /* FCMGT */
10917 if (!fp_access_check(s)) {
10918 return;
10920 handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
10921 return;
10923 case 0x1d: /* FMLAL */
10924 case 0x3d: /* FMLSL */
10925 case 0x59: /* FMLAL2 */
10926 case 0x79: /* FMLSL2 */
10927 if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
10928 unallocated_encoding(s);
10929 return;
10931 if (fp_access_check(s)) {
10932 int is_s = extract32(insn, 23, 1);
10933 int is_2 = extract32(insn, 29, 1);
10934 int data = (is_2 << 1) | is_s;
10935 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
10936 vec_full_reg_offset(s, rn),
10937 vec_full_reg_offset(s, rm), cpu_env,
10938 is_q ? 16 : 8, vec_full_reg_size(s),
10939 data, gen_helper_gvec_fmlal_a64);
10941 return;
10943 default:
10944 unallocated_encoding(s);
10945 return;
10949 /* Integer op subgroup of C3.6.16. */
10950 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
10952 int is_q = extract32(insn, 30, 1);
10953 int u = extract32(insn, 29, 1);
10954 int size = extract32(insn, 22, 2);
10955 int opcode = extract32(insn, 11, 5);
10956 int rm = extract32(insn, 16, 5);
10957 int rn = extract32(insn, 5, 5);
10958 int rd = extract32(insn, 0, 5);
10959 int pass;
10960 TCGCond cond;
10962 switch (opcode) {
10963 case 0x13: /* MUL, PMUL */
10964 if (u && size != 0) {
10965 unallocated_encoding(s);
10966 return;
10968 /* fall through */
10969 case 0x0: /* SHADD, UHADD */
10970 case 0x2: /* SRHADD, URHADD */
10971 case 0x4: /* SHSUB, UHSUB */
10972 case 0xc: /* SMAX, UMAX */
10973 case 0xd: /* SMIN, UMIN */
10974 case 0xe: /* SABD, UABD */
10975 case 0xf: /* SABA, UABA */
10976 case 0x12: /* MLA, MLS */
10977 if (size == 3) {
10978 unallocated_encoding(s);
10979 return;
10981 break;
10982 case 0x16: /* SQDMULH, SQRDMULH */
10983 if (size == 0 || size == 3) {
10984 unallocated_encoding(s);
10985 return;
10987 break;
10988 default:
10989 if (size == 3 && !is_q) {
10990 unallocated_encoding(s);
10991 return;
10993 break;
10996 if (!fp_access_check(s)) {
10997 return;
11000 switch (opcode) {
11001 case 0x01: /* SQADD, UQADD */
11002 tcg_gen_gvec_4(vec_full_reg_offset(s, rd),
11003 offsetof(CPUARMState, vfp.qc),
11004 vec_full_reg_offset(s, rn),
11005 vec_full_reg_offset(s, rm),
11006 is_q ? 16 : 8, vec_full_reg_size(s),
11007 (u ? uqadd_op : sqadd_op) + size);
11008 return;
11009 case 0x05: /* SQSUB, UQSUB */
11010 tcg_gen_gvec_4(vec_full_reg_offset(s, rd),
11011 offsetof(CPUARMState, vfp.qc),
11012 vec_full_reg_offset(s, rn),
11013 vec_full_reg_offset(s, rm),
11014 is_q ? 16 : 8, vec_full_reg_size(s),
11015 (u ? uqsub_op : sqsub_op) + size);
11016 return;
11017 case 0x0c: /* SMAX, UMAX */
11018 if (u) {
11019 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11020 } else {
11021 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11023 return;
11024 case 0x0d: /* SMIN, UMIN */
11025 if (u) {
11026 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11027 } else {
11028 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11030 return;
11031 case 0x10: /* ADD, SUB */
11032 if (u) {
11033 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11034 } else {
11035 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11037 return;
11038 case 0x13: /* MUL, PMUL */
11039 if (!u) { /* MUL */
11040 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11041 return;
11043 break;
11044 case 0x12: /* MLA, MLS */
11045 if (u) {
11046 gen_gvec_op3(s, is_q, rd, rn, rm, &mls_op[size]);
11047 } else {
11048 gen_gvec_op3(s, is_q, rd, rn, rm, &mla_op[size]);
11050 return;
11051 case 0x11:
11052 if (!u) { /* CMTST */
11053 gen_gvec_op3(s, is_q, rd, rn, rm, &cmtst_op[size]);
11054 return;
11056 /* else CMEQ */
11057 cond = TCG_COND_EQ;
11058 goto do_gvec_cmp;
11059 case 0x06: /* CMGT, CMHI */
11060 cond = u ? TCG_COND_GTU : TCG_COND_GT;
11061 goto do_gvec_cmp;
11062 case 0x07: /* CMGE, CMHS */
11063 cond = u ? TCG_COND_GEU : TCG_COND_GE;
11064 do_gvec_cmp:
11065 tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11066 vec_full_reg_offset(s, rn),
11067 vec_full_reg_offset(s, rm),
11068 is_q ? 16 : 8, vec_full_reg_size(s));
11069 return;
11072 if (size == 3) {
11073 assert(is_q);
11074 for (pass = 0; pass < 2; pass++) {
11075 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11076 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11077 TCGv_i64 tcg_res = tcg_temp_new_i64();
11079 read_vec_element(s, tcg_op1, rn, pass, MO_64);
11080 read_vec_element(s, tcg_op2, rm, pass, MO_64);
11082 handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11084 write_vec_element(s, tcg_res, rd, pass, MO_64);
11086 tcg_temp_free_i64(tcg_res);
11087 tcg_temp_free_i64(tcg_op1);
11088 tcg_temp_free_i64(tcg_op2);
11090 } else {
11091 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11092 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11093 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11094 TCGv_i32 tcg_res = tcg_temp_new_i32();
11095 NeonGenTwoOpFn *genfn = NULL;
11096 NeonGenTwoOpEnvFn *genenvfn = NULL;
11098 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11099 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11101 switch (opcode) {
11102 case 0x0: /* SHADD, UHADD */
11104 static NeonGenTwoOpFn * const fns[3][2] = {
11105 { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11106 { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11107 { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11109 genfn = fns[size][u];
11110 break;
11112 case 0x2: /* SRHADD, URHADD */
11114 static NeonGenTwoOpFn * const fns[3][2] = {
11115 { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11116 { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11117 { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11119 genfn = fns[size][u];
11120 break;
11122 case 0x4: /* SHSUB, UHSUB */
11124 static NeonGenTwoOpFn * const fns[3][2] = {
11125 { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11126 { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11127 { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11129 genfn = fns[size][u];
11130 break;
11132 case 0x8: /* SSHL, USHL */
11134 static NeonGenTwoOpFn * const fns[3][2] = {
11135 { gen_helper_neon_shl_s8, gen_helper_neon_shl_u8 },
11136 { gen_helper_neon_shl_s16, gen_helper_neon_shl_u16 },
11137 { gen_helper_neon_shl_s32, gen_helper_neon_shl_u32 },
11139 genfn = fns[size][u];
11140 break;
11142 case 0x9: /* SQSHL, UQSHL */
11144 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11145 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11146 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11147 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11149 genenvfn = fns[size][u];
11150 break;
11152 case 0xa: /* SRSHL, URSHL */
11154 static NeonGenTwoOpFn * const fns[3][2] = {
11155 { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11156 { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11157 { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11159 genfn = fns[size][u];
11160 break;
11162 case 0xb: /* SQRSHL, UQRSHL */
11164 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11165 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11166 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11167 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11169 genenvfn = fns[size][u];
11170 break;
11172 case 0xe: /* SABD, UABD */
11173 case 0xf: /* SABA, UABA */
11175 static NeonGenTwoOpFn * const fns[3][2] = {
11176 { gen_helper_neon_abd_s8, gen_helper_neon_abd_u8 },
11177 { gen_helper_neon_abd_s16, gen_helper_neon_abd_u16 },
11178 { gen_helper_neon_abd_s32, gen_helper_neon_abd_u32 },
11180 genfn = fns[size][u];
11181 break;
11183 case 0x13: /* MUL, PMUL */
11184 assert(u); /* PMUL */
11185 assert(size == 0);
11186 genfn = gen_helper_neon_mul_p8;
11187 break;
11188 case 0x16: /* SQDMULH, SQRDMULH */
11190 static NeonGenTwoOpEnvFn * const fns[2][2] = {
11191 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
11192 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
11194 assert(size == 1 || size == 2);
11195 genenvfn = fns[size - 1][u];
11196 break;
11198 default:
11199 g_assert_not_reached();
11202 if (genenvfn) {
11203 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11204 } else {
11205 genfn(tcg_res, tcg_op1, tcg_op2);
11208 if (opcode == 0xf) {
11209 /* SABA, UABA: accumulating ops */
11210 static NeonGenTwoOpFn * const fns[3] = {
11211 gen_helper_neon_add_u8,
11212 gen_helper_neon_add_u16,
11213 tcg_gen_add_i32,
11216 read_vec_element_i32(s, tcg_op1, rd, pass, MO_32);
11217 fns[size](tcg_res, tcg_op1, tcg_res);
11220 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11222 tcg_temp_free_i32(tcg_res);
11223 tcg_temp_free_i32(tcg_op1);
11224 tcg_temp_free_i32(tcg_op2);
11227 clear_vec_high(s, is_q, rd);
11230 /* AdvSIMD three same
11231 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
11232 * +---+---+---+-----------+------+---+------+--------+---+------+------+
11233 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
11234 * +---+---+---+-----------+------+---+------+--------+---+------+------+
11236 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11238 int opcode = extract32(insn, 11, 5);
11240 switch (opcode) {
11241 case 0x3: /* logic ops */
11242 disas_simd_3same_logic(s, insn);
11243 break;
11244 case 0x17: /* ADDP */
11245 case 0x14: /* SMAXP, UMAXP */
11246 case 0x15: /* SMINP, UMINP */
11248 /* Pairwise operations */
11249 int is_q = extract32(insn, 30, 1);
11250 int u = extract32(insn, 29, 1);
11251 int size = extract32(insn, 22, 2);
11252 int rm = extract32(insn, 16, 5);
11253 int rn = extract32(insn, 5, 5);
11254 int rd = extract32(insn, 0, 5);
11255 if (opcode == 0x17) {
11256 if (u || (size == 3 && !is_q)) {
11257 unallocated_encoding(s);
11258 return;
11260 } else {
11261 if (size == 3) {
11262 unallocated_encoding(s);
11263 return;
11266 handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11267 break;
11269 case 0x18 ... 0x31:
11270 /* floating point ops, sz[1] and U are part of opcode */
11271 disas_simd_3same_float(s, insn);
11272 break;
11273 default:
11274 disas_simd_3same_int(s, insn);
11275 break;
11280 * Advanced SIMD three same (ARMv8.2 FP16 variants)
11282 * 31 30 29 28 24 23 22 21 20 16 15 14 13 11 10 9 5 4 0
11283 * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11284 * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 | Rm | 0 0 | opcode | 1 | Rn | Rd |
11285 * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11287 * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11288 * (register), FACGE, FABD, FCMGT (register) and FACGT.
11291 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11293 int opcode, fpopcode;
11294 int is_q, u, a, rm, rn, rd;
11295 int datasize, elements;
11296 int pass;
11297 TCGv_ptr fpst;
11298 bool pairwise = false;
11300 if (!dc_isar_feature(aa64_fp16, s)) {
11301 unallocated_encoding(s);
11302 return;
11305 if (!fp_access_check(s)) {
11306 return;
11309 /* For these floating point ops, the U, a and opcode bits
11310 * together indicate the operation.
11312 opcode = extract32(insn, 11, 3);
11313 u = extract32(insn, 29, 1);
11314 a = extract32(insn, 23, 1);
11315 is_q = extract32(insn, 30, 1);
11316 rm = extract32(insn, 16, 5);
11317 rn = extract32(insn, 5, 5);
11318 rd = extract32(insn, 0, 5);
11320 fpopcode = opcode | (a << 3) | (u << 4);
11321 datasize = is_q ? 128 : 64;
11322 elements = datasize / 16;
11324 switch (fpopcode) {
11325 case 0x10: /* FMAXNMP */
11326 case 0x12: /* FADDP */
11327 case 0x16: /* FMAXP */
11328 case 0x18: /* FMINNMP */
11329 case 0x1e: /* FMINP */
11330 pairwise = true;
11331 break;
11334 fpst = get_fpstatus_ptr(true);
11336 if (pairwise) {
11337 int maxpass = is_q ? 8 : 4;
11338 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11339 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11340 TCGv_i32 tcg_res[8];
11342 for (pass = 0; pass < maxpass; pass++) {
11343 int passreg = pass < (maxpass / 2) ? rn : rm;
11344 int passelt = (pass << 1) & (maxpass - 1);
11346 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11347 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11348 tcg_res[pass] = tcg_temp_new_i32();
11350 switch (fpopcode) {
11351 case 0x10: /* FMAXNMP */
11352 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11353 fpst);
11354 break;
11355 case 0x12: /* FADDP */
11356 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11357 break;
11358 case 0x16: /* FMAXP */
11359 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11360 break;
11361 case 0x18: /* FMINNMP */
11362 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11363 fpst);
11364 break;
11365 case 0x1e: /* FMINP */
11366 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11367 break;
11368 default:
11369 g_assert_not_reached();
11373 for (pass = 0; pass < maxpass; pass++) {
11374 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11375 tcg_temp_free_i32(tcg_res[pass]);
11378 tcg_temp_free_i32(tcg_op1);
11379 tcg_temp_free_i32(tcg_op2);
11381 } else {
11382 for (pass = 0; pass < elements; pass++) {
11383 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11384 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11385 TCGv_i32 tcg_res = tcg_temp_new_i32();
11387 read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11388 read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11390 switch (fpopcode) {
11391 case 0x0: /* FMAXNM */
11392 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11393 break;
11394 case 0x1: /* FMLA */
11395 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11396 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11397 fpst);
11398 break;
11399 case 0x2: /* FADD */
11400 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11401 break;
11402 case 0x3: /* FMULX */
11403 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11404 break;
11405 case 0x4: /* FCMEQ */
11406 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11407 break;
11408 case 0x6: /* FMAX */
11409 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11410 break;
11411 case 0x7: /* FRECPS */
11412 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11413 break;
11414 case 0x8: /* FMINNM */
11415 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11416 break;
11417 case 0x9: /* FMLS */
11418 /* As usual for ARM, separate negation for fused multiply-add */
11419 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11420 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11421 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11422 fpst);
11423 break;
11424 case 0xa: /* FSUB */
11425 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11426 break;
11427 case 0xe: /* FMIN */
11428 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11429 break;
11430 case 0xf: /* FRSQRTS */
11431 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11432 break;
11433 case 0x13: /* FMUL */
11434 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11435 break;
11436 case 0x14: /* FCMGE */
11437 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11438 break;
11439 case 0x15: /* FACGE */
11440 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11441 break;
11442 case 0x17: /* FDIV */
11443 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11444 break;
11445 case 0x1a: /* FABD */
11446 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11447 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11448 break;
11449 case 0x1c: /* FCMGT */
11450 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11451 break;
11452 case 0x1d: /* FACGT */
11453 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11454 break;
11455 default:
11456 fprintf(stderr, "%s: insn %#04x, fpop %#2x @ %#" PRIx64 "\n",
11457 __func__, insn, fpopcode, s->pc);
11458 g_assert_not_reached();
11461 write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11462 tcg_temp_free_i32(tcg_res);
11463 tcg_temp_free_i32(tcg_op1);
11464 tcg_temp_free_i32(tcg_op2);
11468 tcg_temp_free_ptr(fpst);
11470 clear_vec_high(s, is_q, rd);
11473 /* AdvSIMD three same extra
11474 * 31 30 29 28 24 23 22 21 20 16 15 14 11 10 9 5 4 0
11475 * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11476 * | 0 | Q | U | 0 1 1 1 0 | size | 0 | Rm | 1 | opcode | 1 | Rn | Rd |
11477 * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11479 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11481 int rd = extract32(insn, 0, 5);
11482 int rn = extract32(insn, 5, 5);
11483 int opcode = extract32(insn, 11, 4);
11484 int rm = extract32(insn, 16, 5);
11485 int size = extract32(insn, 22, 2);
11486 bool u = extract32(insn, 29, 1);
11487 bool is_q = extract32(insn, 30, 1);
11488 bool feature;
11489 int rot;
11491 switch (u * 16 + opcode) {
11492 case 0x10: /* SQRDMLAH (vector) */
11493 case 0x11: /* SQRDMLSH (vector) */
11494 if (size != 1 && size != 2) {
11495 unallocated_encoding(s);
11496 return;
11498 feature = dc_isar_feature(aa64_rdm, s);
11499 break;
11500 case 0x02: /* SDOT (vector) */
11501 case 0x12: /* UDOT (vector) */
11502 if (size != MO_32) {
11503 unallocated_encoding(s);
11504 return;
11506 feature = dc_isar_feature(aa64_dp, s);
11507 break;
11508 case 0x18: /* FCMLA, #0 */
11509 case 0x19: /* FCMLA, #90 */
11510 case 0x1a: /* FCMLA, #180 */
11511 case 0x1b: /* FCMLA, #270 */
11512 case 0x1c: /* FCADD, #90 */
11513 case 0x1e: /* FCADD, #270 */
11514 if (size == 0
11515 || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11516 || (size == 3 && !is_q)) {
11517 unallocated_encoding(s);
11518 return;
11520 feature = dc_isar_feature(aa64_fcma, s);
11521 break;
11522 default:
11523 unallocated_encoding(s);
11524 return;
11526 if (!feature) {
11527 unallocated_encoding(s);
11528 return;
11530 if (!fp_access_check(s)) {
11531 return;
11534 switch (opcode) {
11535 case 0x0: /* SQRDMLAH (vector) */
11536 switch (size) {
11537 case 1:
11538 gen_gvec_op3_env(s, is_q, rd, rn, rm, gen_helper_gvec_qrdmlah_s16);
11539 break;
11540 case 2:
11541 gen_gvec_op3_env(s, is_q, rd, rn, rm, gen_helper_gvec_qrdmlah_s32);
11542 break;
11543 default:
11544 g_assert_not_reached();
11546 return;
11548 case 0x1: /* SQRDMLSH (vector) */
11549 switch (size) {
11550 case 1:
11551 gen_gvec_op3_env(s, is_q, rd, rn, rm, gen_helper_gvec_qrdmlsh_s16);
11552 break;
11553 case 2:
11554 gen_gvec_op3_env(s, is_q, rd, rn, rm, gen_helper_gvec_qrdmlsh_s32);
11555 break;
11556 default:
11557 g_assert_not_reached();
11559 return;
11561 case 0x2: /* SDOT / UDOT */
11562 gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0,
11563 u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11564 return;
11566 case 0x8: /* FCMLA, #0 */
11567 case 0x9: /* FCMLA, #90 */
11568 case 0xa: /* FCMLA, #180 */
11569 case 0xb: /* FCMLA, #270 */
11570 rot = extract32(opcode, 0, 2);
11571 switch (size) {
11572 case 1:
11573 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, true, rot,
11574 gen_helper_gvec_fcmlah);
11575 break;
11576 case 2:
11577 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, false, rot,
11578 gen_helper_gvec_fcmlas);
11579 break;
11580 case 3:
11581 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, false, rot,
11582 gen_helper_gvec_fcmlad);
11583 break;
11584 default:
11585 g_assert_not_reached();
11587 return;
11589 case 0xc: /* FCADD, #90 */
11590 case 0xe: /* FCADD, #270 */
11591 rot = extract32(opcode, 1, 1);
11592 switch (size) {
11593 case 1:
11594 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11595 gen_helper_gvec_fcaddh);
11596 break;
11597 case 2:
11598 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11599 gen_helper_gvec_fcadds);
11600 break;
11601 case 3:
11602 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11603 gen_helper_gvec_fcaddd);
11604 break;
11605 default:
11606 g_assert_not_reached();
11608 return;
11610 default:
11611 g_assert_not_reached();
11615 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11616 int size, int rn, int rd)
11618 /* Handle 2-reg-misc ops which are widening (so each size element
11619 * in the source becomes a 2*size element in the destination.
11620 * The only instruction like this is FCVTL.
11622 int pass;
11624 if (size == 3) {
11625 /* 32 -> 64 bit fp conversion */
11626 TCGv_i64 tcg_res[2];
11627 int srcelt = is_q ? 2 : 0;
11629 for (pass = 0; pass < 2; pass++) {
11630 TCGv_i32 tcg_op = tcg_temp_new_i32();
11631 tcg_res[pass] = tcg_temp_new_i64();
11633 read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11634 gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
11635 tcg_temp_free_i32(tcg_op);
11637 for (pass = 0; pass < 2; pass++) {
11638 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11639 tcg_temp_free_i64(tcg_res[pass]);
11641 } else {
11642 /* 16 -> 32 bit fp conversion */
11643 int srcelt = is_q ? 4 : 0;
11644 TCGv_i32 tcg_res[4];
11645 TCGv_ptr fpst = get_fpstatus_ptr(false);
11646 TCGv_i32 ahp = get_ahp_flag();
11648 for (pass = 0; pass < 4; pass++) {
11649 tcg_res[pass] = tcg_temp_new_i32();
11651 read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11652 gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11653 fpst, ahp);
11655 for (pass = 0; pass < 4; pass++) {
11656 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11657 tcg_temp_free_i32(tcg_res[pass]);
11660 tcg_temp_free_ptr(fpst);
11661 tcg_temp_free_i32(ahp);
11665 static void handle_rev(DisasContext *s, int opcode, bool u,
11666 bool is_q, int size, int rn, int rd)
11668 int op = (opcode << 1) | u;
11669 int opsz = op + size;
11670 int grp_size = 3 - opsz;
11671 int dsize = is_q ? 128 : 64;
11672 int i;
11674 if (opsz >= 3) {
11675 unallocated_encoding(s);
11676 return;
11679 if (!fp_access_check(s)) {
11680 return;
11683 if (size == 0) {
11684 /* Special case bytes, use bswap op on each group of elements */
11685 int groups = dsize / (8 << grp_size);
11687 for (i = 0; i < groups; i++) {
11688 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11690 read_vec_element(s, tcg_tmp, rn, i, grp_size);
11691 switch (grp_size) {
11692 case MO_16:
11693 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
11694 break;
11695 case MO_32:
11696 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
11697 break;
11698 case MO_64:
11699 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11700 break;
11701 default:
11702 g_assert_not_reached();
11704 write_vec_element(s, tcg_tmp, rd, i, grp_size);
11705 tcg_temp_free_i64(tcg_tmp);
11707 clear_vec_high(s, is_q, rd);
11708 } else {
11709 int revmask = (1 << grp_size) - 1;
11710 int esize = 8 << size;
11711 int elements = dsize / esize;
11712 TCGv_i64 tcg_rn = tcg_temp_new_i64();
11713 TCGv_i64 tcg_rd = tcg_const_i64(0);
11714 TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
11716 for (i = 0; i < elements; i++) {
11717 int e_rev = (i & 0xf) ^ revmask;
11718 int off = e_rev * esize;
11719 read_vec_element(s, tcg_rn, rn, i, size);
11720 if (off >= 64) {
11721 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
11722 tcg_rn, off - 64, esize);
11723 } else {
11724 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
11727 write_vec_element(s, tcg_rd, rd, 0, MO_64);
11728 write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
11730 tcg_temp_free_i64(tcg_rd_hi);
11731 tcg_temp_free_i64(tcg_rd);
11732 tcg_temp_free_i64(tcg_rn);
11736 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
11737 bool is_q, int size, int rn, int rd)
11739 /* Implement the pairwise operations from 2-misc:
11740 * SADDLP, UADDLP, SADALP, UADALP.
11741 * These all add pairs of elements in the input to produce a
11742 * double-width result element in the output (possibly accumulating).
11744 bool accum = (opcode == 0x6);
11745 int maxpass = is_q ? 2 : 1;
11746 int pass;
11747 TCGv_i64 tcg_res[2];
11749 if (size == 2) {
11750 /* 32 + 32 -> 64 op */
11751 TCGMemOp memop = size + (u ? 0 : MO_SIGN);
11753 for (pass = 0; pass < maxpass; pass++) {
11754 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11755 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11757 tcg_res[pass] = tcg_temp_new_i64();
11759 read_vec_element(s, tcg_op1, rn, pass * 2, memop);
11760 read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
11761 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11762 if (accum) {
11763 read_vec_element(s, tcg_op1, rd, pass, MO_64);
11764 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
11767 tcg_temp_free_i64(tcg_op1);
11768 tcg_temp_free_i64(tcg_op2);
11770 } else {
11771 for (pass = 0; pass < maxpass; pass++) {
11772 TCGv_i64 tcg_op = tcg_temp_new_i64();
11773 NeonGenOneOpFn *genfn;
11774 static NeonGenOneOpFn * const fns[2][2] = {
11775 { gen_helper_neon_addlp_s8, gen_helper_neon_addlp_u8 },
11776 { gen_helper_neon_addlp_s16, gen_helper_neon_addlp_u16 },
11779 genfn = fns[size][u];
11781 tcg_res[pass] = tcg_temp_new_i64();
11783 read_vec_element(s, tcg_op, rn, pass, MO_64);
11784 genfn(tcg_res[pass], tcg_op);
11786 if (accum) {
11787 read_vec_element(s, tcg_op, rd, pass, MO_64);
11788 if (size == 0) {
11789 gen_helper_neon_addl_u16(tcg_res[pass],
11790 tcg_res[pass], tcg_op);
11791 } else {
11792 gen_helper_neon_addl_u32(tcg_res[pass],
11793 tcg_res[pass], tcg_op);
11796 tcg_temp_free_i64(tcg_op);
11799 if (!is_q) {
11800 tcg_res[1] = tcg_const_i64(0);
11802 for (pass = 0; pass < 2; pass++) {
11803 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11804 tcg_temp_free_i64(tcg_res[pass]);
11808 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
11810 /* Implement SHLL and SHLL2 */
11811 int pass;
11812 int part = is_q ? 2 : 0;
11813 TCGv_i64 tcg_res[2];
11815 for (pass = 0; pass < 2; pass++) {
11816 static NeonGenWidenFn * const widenfns[3] = {
11817 gen_helper_neon_widen_u8,
11818 gen_helper_neon_widen_u16,
11819 tcg_gen_extu_i32_i64,
11821 NeonGenWidenFn *widenfn = widenfns[size];
11822 TCGv_i32 tcg_op = tcg_temp_new_i32();
11824 read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
11825 tcg_res[pass] = tcg_temp_new_i64();
11826 widenfn(tcg_res[pass], tcg_op);
11827 tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
11829 tcg_temp_free_i32(tcg_op);
11832 for (pass = 0; pass < 2; pass++) {
11833 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11834 tcg_temp_free_i64(tcg_res[pass]);
11838 /* AdvSIMD two reg misc
11839 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
11840 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11841 * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
11842 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11844 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
11846 int size = extract32(insn, 22, 2);
11847 int opcode = extract32(insn, 12, 5);
11848 bool u = extract32(insn, 29, 1);
11849 bool is_q = extract32(insn, 30, 1);
11850 int rn = extract32(insn, 5, 5);
11851 int rd = extract32(insn, 0, 5);
11852 bool need_fpstatus = false;
11853 bool need_rmode = false;
11854 int rmode = -1;
11855 TCGv_i32 tcg_rmode;
11856 TCGv_ptr tcg_fpstatus;
11858 switch (opcode) {
11859 case 0x0: /* REV64, REV32 */
11860 case 0x1: /* REV16 */
11861 handle_rev(s, opcode, u, is_q, size, rn, rd);
11862 return;
11863 case 0x5: /* CNT, NOT, RBIT */
11864 if (u && size == 0) {
11865 /* NOT */
11866 break;
11867 } else if (u && size == 1) {
11868 /* RBIT */
11869 break;
11870 } else if (!u && size == 0) {
11871 /* CNT */
11872 break;
11874 unallocated_encoding(s);
11875 return;
11876 case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
11877 case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
11878 if (size == 3) {
11879 unallocated_encoding(s);
11880 return;
11882 if (!fp_access_check(s)) {
11883 return;
11886 handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
11887 return;
11888 case 0x4: /* CLS, CLZ */
11889 if (size == 3) {
11890 unallocated_encoding(s);
11891 return;
11893 break;
11894 case 0x2: /* SADDLP, UADDLP */
11895 case 0x6: /* SADALP, UADALP */
11896 if (size == 3) {
11897 unallocated_encoding(s);
11898 return;
11900 if (!fp_access_check(s)) {
11901 return;
11903 handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
11904 return;
11905 case 0x13: /* SHLL, SHLL2 */
11906 if (u == 0 || size == 3) {
11907 unallocated_encoding(s);
11908 return;
11910 if (!fp_access_check(s)) {
11911 return;
11913 handle_shll(s, is_q, size, rn, rd);
11914 return;
11915 case 0xa: /* CMLT */
11916 if (u == 1) {
11917 unallocated_encoding(s);
11918 return;
11920 /* fall through */
11921 case 0x8: /* CMGT, CMGE */
11922 case 0x9: /* CMEQ, CMLE */
11923 case 0xb: /* ABS, NEG */
11924 if (size == 3 && !is_q) {
11925 unallocated_encoding(s);
11926 return;
11928 break;
11929 case 0x3: /* SUQADD, USQADD */
11930 if (size == 3 && !is_q) {
11931 unallocated_encoding(s);
11932 return;
11934 if (!fp_access_check(s)) {
11935 return;
11937 handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
11938 return;
11939 case 0x7: /* SQABS, SQNEG */
11940 if (size == 3 && !is_q) {
11941 unallocated_encoding(s);
11942 return;
11944 break;
11945 case 0xc ... 0xf:
11946 case 0x16 ... 0x1d:
11947 case 0x1f:
11949 /* Floating point: U, size[1] and opcode indicate operation;
11950 * size[0] indicates single or double precision.
11952 int is_double = extract32(size, 0, 1);
11953 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
11954 size = is_double ? 3 : 2;
11955 switch (opcode) {
11956 case 0x2f: /* FABS */
11957 case 0x6f: /* FNEG */
11958 if (size == 3 && !is_q) {
11959 unallocated_encoding(s);
11960 return;
11962 break;
11963 case 0x1d: /* SCVTF */
11964 case 0x5d: /* UCVTF */
11966 bool is_signed = (opcode == 0x1d) ? true : false;
11967 int elements = is_double ? 2 : is_q ? 4 : 2;
11968 if (is_double && !is_q) {
11969 unallocated_encoding(s);
11970 return;
11972 if (!fp_access_check(s)) {
11973 return;
11975 handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
11976 return;
11978 case 0x2c: /* FCMGT (zero) */
11979 case 0x2d: /* FCMEQ (zero) */
11980 case 0x2e: /* FCMLT (zero) */
11981 case 0x6c: /* FCMGE (zero) */
11982 case 0x6d: /* FCMLE (zero) */
11983 if (size == 3 && !is_q) {
11984 unallocated_encoding(s);
11985 return;
11987 handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
11988 return;
11989 case 0x7f: /* FSQRT */
11990 if (size == 3 && !is_q) {
11991 unallocated_encoding(s);
11992 return;
11994 break;
11995 case 0x1a: /* FCVTNS */
11996 case 0x1b: /* FCVTMS */
11997 case 0x3a: /* FCVTPS */
11998 case 0x3b: /* FCVTZS */
11999 case 0x5a: /* FCVTNU */
12000 case 0x5b: /* FCVTMU */
12001 case 0x7a: /* FCVTPU */
12002 case 0x7b: /* FCVTZU */
12003 need_fpstatus = true;
12004 need_rmode = true;
12005 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12006 if (size == 3 && !is_q) {
12007 unallocated_encoding(s);
12008 return;
12010 break;
12011 case 0x5c: /* FCVTAU */
12012 case 0x1c: /* FCVTAS */
12013 need_fpstatus = true;
12014 need_rmode = true;
12015 rmode = FPROUNDING_TIEAWAY;
12016 if (size == 3 && !is_q) {
12017 unallocated_encoding(s);
12018 return;
12020 break;
12021 case 0x3c: /* URECPE */
12022 if (size == 3) {
12023 unallocated_encoding(s);
12024 return;
12026 /* fall through */
12027 case 0x3d: /* FRECPE */
12028 case 0x7d: /* FRSQRTE */
12029 if (size == 3 && !is_q) {
12030 unallocated_encoding(s);
12031 return;
12033 if (!fp_access_check(s)) {
12034 return;
12036 handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12037 return;
12038 case 0x56: /* FCVTXN, FCVTXN2 */
12039 if (size == 2) {
12040 unallocated_encoding(s);
12041 return;
12043 /* fall through */
12044 case 0x16: /* FCVTN, FCVTN2 */
12045 /* handle_2misc_narrow does a 2*size -> size operation, but these
12046 * instructions encode the source size rather than dest size.
12048 if (!fp_access_check(s)) {
12049 return;
12051 handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12052 return;
12053 case 0x17: /* FCVTL, FCVTL2 */
12054 if (!fp_access_check(s)) {
12055 return;
12057 handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12058 return;
12059 case 0x18: /* FRINTN */
12060 case 0x19: /* FRINTM */
12061 case 0x38: /* FRINTP */
12062 case 0x39: /* FRINTZ */
12063 need_rmode = true;
12064 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12065 /* fall through */
12066 case 0x59: /* FRINTX */
12067 case 0x79: /* FRINTI */
12068 need_fpstatus = true;
12069 if (size == 3 && !is_q) {
12070 unallocated_encoding(s);
12071 return;
12073 break;
12074 case 0x58: /* FRINTA */
12075 need_rmode = true;
12076 rmode = FPROUNDING_TIEAWAY;
12077 need_fpstatus = true;
12078 if (size == 3 && !is_q) {
12079 unallocated_encoding(s);
12080 return;
12082 break;
12083 case 0x7c: /* URSQRTE */
12084 if (size == 3) {
12085 unallocated_encoding(s);
12086 return;
12088 need_fpstatus = true;
12089 break;
12090 default:
12091 unallocated_encoding(s);
12092 return;
12094 break;
12096 default:
12097 unallocated_encoding(s);
12098 return;
12101 if (!fp_access_check(s)) {
12102 return;
12105 if (need_fpstatus || need_rmode) {
12106 tcg_fpstatus = get_fpstatus_ptr(false);
12107 } else {
12108 tcg_fpstatus = NULL;
12110 if (need_rmode) {
12111 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
12112 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12113 } else {
12114 tcg_rmode = NULL;
12117 switch (opcode) {
12118 case 0x5:
12119 if (u && size == 0) { /* NOT */
12120 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12121 return;
12123 break;
12124 case 0xb:
12125 if (u) { /* NEG */
12126 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12127 return;
12129 break;
12132 if (size == 3) {
12133 /* All 64-bit element operations can be shared with scalar 2misc */
12134 int pass;
12136 /* Coverity claims (size == 3 && !is_q) has been eliminated
12137 * from all paths leading to here.
12139 tcg_debug_assert(is_q);
12140 for (pass = 0; pass < 2; pass++) {
12141 TCGv_i64 tcg_op = tcg_temp_new_i64();
12142 TCGv_i64 tcg_res = tcg_temp_new_i64();
12144 read_vec_element(s, tcg_op, rn, pass, MO_64);
12146 handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12147 tcg_rmode, tcg_fpstatus);
12149 write_vec_element(s, tcg_res, rd, pass, MO_64);
12151 tcg_temp_free_i64(tcg_res);
12152 tcg_temp_free_i64(tcg_op);
12154 } else {
12155 int pass;
12157 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12158 TCGv_i32 tcg_op = tcg_temp_new_i32();
12159 TCGv_i32 tcg_res = tcg_temp_new_i32();
12160 TCGCond cond;
12162 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12164 if (size == 2) {
12165 /* Special cases for 32 bit elements */
12166 switch (opcode) {
12167 case 0xa: /* CMLT */
12168 /* 32 bit integer comparison against zero, result is
12169 * test ? (2^32 - 1) : 0. We implement via setcond(test)
12170 * and inverting.
12172 cond = TCG_COND_LT;
12173 do_cmop:
12174 tcg_gen_setcondi_i32(cond, tcg_res, tcg_op, 0);
12175 tcg_gen_neg_i32(tcg_res, tcg_res);
12176 break;
12177 case 0x8: /* CMGT, CMGE */
12178 cond = u ? TCG_COND_GE : TCG_COND_GT;
12179 goto do_cmop;
12180 case 0x9: /* CMEQ, CMLE */
12181 cond = u ? TCG_COND_LE : TCG_COND_EQ;
12182 goto do_cmop;
12183 case 0x4: /* CLS */
12184 if (u) {
12185 tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12186 } else {
12187 tcg_gen_clrsb_i32(tcg_res, tcg_op);
12189 break;
12190 case 0x7: /* SQABS, SQNEG */
12191 if (u) {
12192 gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12193 } else {
12194 gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12196 break;
12197 case 0xb: /* ABS, NEG */
12198 if (u) {
12199 tcg_gen_neg_i32(tcg_res, tcg_op);
12200 } else {
12201 TCGv_i32 tcg_zero = tcg_const_i32(0);
12202 tcg_gen_neg_i32(tcg_res, tcg_op);
12203 tcg_gen_movcond_i32(TCG_COND_GT, tcg_res, tcg_op,
12204 tcg_zero, tcg_op, tcg_res);
12205 tcg_temp_free_i32(tcg_zero);
12207 break;
12208 case 0x2f: /* FABS */
12209 gen_helper_vfp_abss(tcg_res, tcg_op);
12210 break;
12211 case 0x6f: /* FNEG */
12212 gen_helper_vfp_negs(tcg_res, tcg_op);
12213 break;
12214 case 0x7f: /* FSQRT */
12215 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12216 break;
12217 case 0x1a: /* FCVTNS */
12218 case 0x1b: /* FCVTMS */
12219 case 0x1c: /* FCVTAS */
12220 case 0x3a: /* FCVTPS */
12221 case 0x3b: /* FCVTZS */
12223 TCGv_i32 tcg_shift = tcg_const_i32(0);
12224 gen_helper_vfp_tosls(tcg_res, tcg_op,
12225 tcg_shift, tcg_fpstatus);
12226 tcg_temp_free_i32(tcg_shift);
12227 break;
12229 case 0x5a: /* FCVTNU */
12230 case 0x5b: /* FCVTMU */
12231 case 0x5c: /* FCVTAU */
12232 case 0x7a: /* FCVTPU */
12233 case 0x7b: /* FCVTZU */
12235 TCGv_i32 tcg_shift = tcg_const_i32(0);
12236 gen_helper_vfp_touls(tcg_res, tcg_op,
12237 tcg_shift, tcg_fpstatus);
12238 tcg_temp_free_i32(tcg_shift);
12239 break;
12241 case 0x18: /* FRINTN */
12242 case 0x19: /* FRINTM */
12243 case 0x38: /* FRINTP */
12244 case 0x39: /* FRINTZ */
12245 case 0x58: /* FRINTA */
12246 case 0x79: /* FRINTI */
12247 gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12248 break;
12249 case 0x59: /* FRINTX */
12250 gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12251 break;
12252 case 0x7c: /* URSQRTE */
12253 gen_helper_rsqrte_u32(tcg_res, tcg_op, tcg_fpstatus);
12254 break;
12255 default:
12256 g_assert_not_reached();
12258 } else {
12259 /* Use helpers for 8 and 16 bit elements */
12260 switch (opcode) {
12261 case 0x5: /* CNT, RBIT */
12262 /* For these two insns size is part of the opcode specifier
12263 * (handled earlier); they always operate on byte elements.
12265 if (u) {
12266 gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12267 } else {
12268 gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12270 break;
12271 case 0x7: /* SQABS, SQNEG */
12273 NeonGenOneOpEnvFn *genfn;
12274 static NeonGenOneOpEnvFn * const fns[2][2] = {
12275 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12276 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12278 genfn = fns[size][u];
12279 genfn(tcg_res, cpu_env, tcg_op);
12280 break;
12282 case 0x8: /* CMGT, CMGE */
12283 case 0x9: /* CMEQ, CMLE */
12284 case 0xa: /* CMLT */
12286 static NeonGenTwoOpFn * const fns[3][2] = {
12287 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_s16 },
12288 { gen_helper_neon_cge_s8, gen_helper_neon_cge_s16 },
12289 { gen_helper_neon_ceq_u8, gen_helper_neon_ceq_u16 },
12291 NeonGenTwoOpFn *genfn;
12292 int comp;
12293 bool reverse;
12294 TCGv_i32 tcg_zero = tcg_const_i32(0);
12296 /* comp = index into [CMGT, CMGE, CMEQ, CMLE, CMLT] */
12297 comp = (opcode - 0x8) * 2 + u;
12298 /* ...but LE, LT are implemented as reverse GE, GT */
12299 reverse = (comp > 2);
12300 if (reverse) {
12301 comp = 4 - comp;
12303 genfn = fns[comp][size];
12304 if (reverse) {
12305 genfn(tcg_res, tcg_zero, tcg_op);
12306 } else {
12307 genfn(tcg_res, tcg_op, tcg_zero);
12309 tcg_temp_free_i32(tcg_zero);
12310 break;
12312 case 0xb: /* ABS, NEG */
12313 if (u) {
12314 TCGv_i32 tcg_zero = tcg_const_i32(0);
12315 if (size) {
12316 gen_helper_neon_sub_u16(tcg_res, tcg_zero, tcg_op);
12317 } else {
12318 gen_helper_neon_sub_u8(tcg_res, tcg_zero, tcg_op);
12320 tcg_temp_free_i32(tcg_zero);
12321 } else {
12322 if (size) {
12323 gen_helper_neon_abs_s16(tcg_res, tcg_op);
12324 } else {
12325 gen_helper_neon_abs_s8(tcg_res, tcg_op);
12328 break;
12329 case 0x4: /* CLS, CLZ */
12330 if (u) {
12331 if (size == 0) {
12332 gen_helper_neon_clz_u8(tcg_res, tcg_op);
12333 } else {
12334 gen_helper_neon_clz_u16(tcg_res, tcg_op);
12336 } else {
12337 if (size == 0) {
12338 gen_helper_neon_cls_s8(tcg_res, tcg_op);
12339 } else {
12340 gen_helper_neon_cls_s16(tcg_res, tcg_op);
12343 break;
12344 default:
12345 g_assert_not_reached();
12349 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12351 tcg_temp_free_i32(tcg_res);
12352 tcg_temp_free_i32(tcg_op);
12355 clear_vec_high(s, is_q, rd);
12357 if (need_rmode) {
12358 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12359 tcg_temp_free_i32(tcg_rmode);
12361 if (need_fpstatus) {
12362 tcg_temp_free_ptr(tcg_fpstatus);
12366 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12368 * 31 30 29 28 27 24 23 22 21 17 16 12 11 10 9 5 4 0
12369 * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12370 * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 | Rn | Rd |
12371 * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12372 * mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12373 * val: 0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12375 * This actually covers two groups where scalar access is governed by
12376 * bit 28. A bunch of the instructions (float to integral) only exist
12377 * in the vector form and are un-allocated for the scalar decode. Also
12378 * in the scalar decode Q is always 1.
12380 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12382 int fpop, opcode, a, u;
12383 int rn, rd;
12384 bool is_q;
12385 bool is_scalar;
12386 bool only_in_vector = false;
12388 int pass;
12389 TCGv_i32 tcg_rmode = NULL;
12390 TCGv_ptr tcg_fpstatus = NULL;
12391 bool need_rmode = false;
12392 bool need_fpst = true;
12393 int rmode;
12395 if (!dc_isar_feature(aa64_fp16, s)) {
12396 unallocated_encoding(s);
12397 return;
12400 rd = extract32(insn, 0, 5);
12401 rn = extract32(insn, 5, 5);
12403 a = extract32(insn, 23, 1);
12404 u = extract32(insn, 29, 1);
12405 is_scalar = extract32(insn, 28, 1);
12406 is_q = extract32(insn, 30, 1);
12408 opcode = extract32(insn, 12, 5);
12409 fpop = deposit32(opcode, 5, 1, a);
12410 fpop = deposit32(fpop, 6, 1, u);
12412 rd = extract32(insn, 0, 5);
12413 rn = extract32(insn, 5, 5);
12415 switch (fpop) {
12416 case 0x1d: /* SCVTF */
12417 case 0x5d: /* UCVTF */
12419 int elements;
12421 if (is_scalar) {
12422 elements = 1;
12423 } else {
12424 elements = (is_q ? 8 : 4);
12427 if (!fp_access_check(s)) {
12428 return;
12430 handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12431 return;
12433 break;
12434 case 0x2c: /* FCMGT (zero) */
12435 case 0x2d: /* FCMEQ (zero) */
12436 case 0x2e: /* FCMLT (zero) */
12437 case 0x6c: /* FCMGE (zero) */
12438 case 0x6d: /* FCMLE (zero) */
12439 handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12440 return;
12441 case 0x3d: /* FRECPE */
12442 case 0x3f: /* FRECPX */
12443 break;
12444 case 0x18: /* FRINTN */
12445 need_rmode = true;
12446 only_in_vector = true;
12447 rmode = FPROUNDING_TIEEVEN;
12448 break;
12449 case 0x19: /* FRINTM */
12450 need_rmode = true;
12451 only_in_vector = true;
12452 rmode = FPROUNDING_NEGINF;
12453 break;
12454 case 0x38: /* FRINTP */
12455 need_rmode = true;
12456 only_in_vector = true;
12457 rmode = FPROUNDING_POSINF;
12458 break;
12459 case 0x39: /* FRINTZ */
12460 need_rmode = true;
12461 only_in_vector = true;
12462 rmode = FPROUNDING_ZERO;
12463 break;
12464 case 0x58: /* FRINTA */
12465 need_rmode = true;
12466 only_in_vector = true;
12467 rmode = FPROUNDING_TIEAWAY;
12468 break;
12469 case 0x59: /* FRINTX */
12470 case 0x79: /* FRINTI */
12471 only_in_vector = true;
12472 /* current rounding mode */
12473 break;
12474 case 0x1a: /* FCVTNS */
12475 need_rmode = true;
12476 rmode = FPROUNDING_TIEEVEN;
12477 break;
12478 case 0x1b: /* FCVTMS */
12479 need_rmode = true;
12480 rmode = FPROUNDING_NEGINF;
12481 break;
12482 case 0x1c: /* FCVTAS */
12483 need_rmode = true;
12484 rmode = FPROUNDING_TIEAWAY;
12485 break;
12486 case 0x3a: /* FCVTPS */
12487 need_rmode = true;
12488 rmode = FPROUNDING_POSINF;
12489 break;
12490 case 0x3b: /* FCVTZS */
12491 need_rmode = true;
12492 rmode = FPROUNDING_ZERO;
12493 break;
12494 case 0x5a: /* FCVTNU */
12495 need_rmode = true;
12496 rmode = FPROUNDING_TIEEVEN;
12497 break;
12498 case 0x5b: /* FCVTMU */
12499 need_rmode = true;
12500 rmode = FPROUNDING_NEGINF;
12501 break;
12502 case 0x5c: /* FCVTAU */
12503 need_rmode = true;
12504 rmode = FPROUNDING_TIEAWAY;
12505 break;
12506 case 0x7a: /* FCVTPU */
12507 need_rmode = true;
12508 rmode = FPROUNDING_POSINF;
12509 break;
12510 case 0x7b: /* FCVTZU */
12511 need_rmode = true;
12512 rmode = FPROUNDING_ZERO;
12513 break;
12514 case 0x2f: /* FABS */
12515 case 0x6f: /* FNEG */
12516 need_fpst = false;
12517 break;
12518 case 0x7d: /* FRSQRTE */
12519 case 0x7f: /* FSQRT (vector) */
12520 break;
12521 default:
12522 fprintf(stderr, "%s: insn %#04x fpop %#2x\n", __func__, insn, fpop);
12523 g_assert_not_reached();
12527 /* Check additional constraints for the scalar encoding */
12528 if (is_scalar) {
12529 if (!is_q) {
12530 unallocated_encoding(s);
12531 return;
12533 /* FRINTxx is only in the vector form */
12534 if (only_in_vector) {
12535 unallocated_encoding(s);
12536 return;
12540 if (!fp_access_check(s)) {
12541 return;
12544 if (need_rmode || need_fpst) {
12545 tcg_fpstatus = get_fpstatus_ptr(true);
12548 if (need_rmode) {
12549 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
12550 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12553 if (is_scalar) {
12554 TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12555 TCGv_i32 tcg_res = tcg_temp_new_i32();
12557 switch (fpop) {
12558 case 0x1a: /* FCVTNS */
12559 case 0x1b: /* FCVTMS */
12560 case 0x1c: /* FCVTAS */
12561 case 0x3a: /* FCVTPS */
12562 case 0x3b: /* FCVTZS */
12563 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12564 break;
12565 case 0x3d: /* FRECPE */
12566 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12567 break;
12568 case 0x3f: /* FRECPX */
12569 gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12570 break;
12571 case 0x5a: /* FCVTNU */
12572 case 0x5b: /* FCVTMU */
12573 case 0x5c: /* FCVTAU */
12574 case 0x7a: /* FCVTPU */
12575 case 0x7b: /* FCVTZU */
12576 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12577 break;
12578 case 0x6f: /* FNEG */
12579 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12580 break;
12581 case 0x7d: /* FRSQRTE */
12582 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12583 break;
12584 default:
12585 g_assert_not_reached();
12588 /* limit any sign extension going on */
12589 tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12590 write_fp_sreg(s, rd, tcg_res);
12592 tcg_temp_free_i32(tcg_res);
12593 tcg_temp_free_i32(tcg_op);
12594 } else {
12595 for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12596 TCGv_i32 tcg_op = tcg_temp_new_i32();
12597 TCGv_i32 tcg_res = tcg_temp_new_i32();
12599 read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12601 switch (fpop) {
12602 case 0x1a: /* FCVTNS */
12603 case 0x1b: /* FCVTMS */
12604 case 0x1c: /* FCVTAS */
12605 case 0x3a: /* FCVTPS */
12606 case 0x3b: /* FCVTZS */
12607 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12608 break;
12609 case 0x3d: /* FRECPE */
12610 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12611 break;
12612 case 0x5a: /* FCVTNU */
12613 case 0x5b: /* FCVTMU */
12614 case 0x5c: /* FCVTAU */
12615 case 0x7a: /* FCVTPU */
12616 case 0x7b: /* FCVTZU */
12617 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12618 break;
12619 case 0x18: /* FRINTN */
12620 case 0x19: /* FRINTM */
12621 case 0x38: /* FRINTP */
12622 case 0x39: /* FRINTZ */
12623 case 0x58: /* FRINTA */
12624 case 0x79: /* FRINTI */
12625 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12626 break;
12627 case 0x59: /* FRINTX */
12628 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12629 break;
12630 case 0x2f: /* FABS */
12631 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12632 break;
12633 case 0x6f: /* FNEG */
12634 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12635 break;
12636 case 0x7d: /* FRSQRTE */
12637 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12638 break;
12639 case 0x7f: /* FSQRT */
12640 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12641 break;
12642 default:
12643 g_assert_not_reached();
12646 write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12648 tcg_temp_free_i32(tcg_res);
12649 tcg_temp_free_i32(tcg_op);
12652 clear_vec_high(s, is_q, rd);
12655 if (tcg_rmode) {
12656 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12657 tcg_temp_free_i32(tcg_rmode);
12660 if (tcg_fpstatus) {
12661 tcg_temp_free_ptr(tcg_fpstatus);
12665 /* AdvSIMD scalar x indexed element
12666 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
12667 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12668 * | 0 1 | U | 1 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
12669 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12670 * AdvSIMD vector x indexed element
12671 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
12672 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12673 * | 0 | Q | U | 0 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
12674 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12676 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12678 /* This encoding has two kinds of instruction:
12679 * normal, where we perform elt x idxelt => elt for each
12680 * element in the vector
12681 * long, where we perform elt x idxelt and generate a result of
12682 * double the width of the input element
12683 * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12685 bool is_scalar = extract32(insn, 28, 1);
12686 bool is_q = extract32(insn, 30, 1);
12687 bool u = extract32(insn, 29, 1);
12688 int size = extract32(insn, 22, 2);
12689 int l = extract32(insn, 21, 1);
12690 int m = extract32(insn, 20, 1);
12691 /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12692 int rm = extract32(insn, 16, 4);
12693 int opcode = extract32(insn, 12, 4);
12694 int h = extract32(insn, 11, 1);
12695 int rn = extract32(insn, 5, 5);
12696 int rd = extract32(insn, 0, 5);
12697 bool is_long = false;
12698 int is_fp = 0;
12699 bool is_fp16 = false;
12700 int index;
12701 TCGv_ptr fpst;
12703 switch (16 * u + opcode) {
12704 case 0x08: /* MUL */
12705 case 0x10: /* MLA */
12706 case 0x14: /* MLS */
12707 if (is_scalar) {
12708 unallocated_encoding(s);
12709 return;
12711 break;
12712 case 0x02: /* SMLAL, SMLAL2 */
12713 case 0x12: /* UMLAL, UMLAL2 */
12714 case 0x06: /* SMLSL, SMLSL2 */
12715 case 0x16: /* UMLSL, UMLSL2 */
12716 case 0x0a: /* SMULL, SMULL2 */
12717 case 0x1a: /* UMULL, UMULL2 */
12718 if (is_scalar) {
12719 unallocated_encoding(s);
12720 return;
12722 is_long = true;
12723 break;
12724 case 0x03: /* SQDMLAL, SQDMLAL2 */
12725 case 0x07: /* SQDMLSL, SQDMLSL2 */
12726 case 0x0b: /* SQDMULL, SQDMULL2 */
12727 is_long = true;
12728 break;
12729 case 0x0c: /* SQDMULH */
12730 case 0x0d: /* SQRDMULH */
12731 break;
12732 case 0x01: /* FMLA */
12733 case 0x05: /* FMLS */
12734 case 0x09: /* FMUL */
12735 case 0x19: /* FMULX */
12736 is_fp = 1;
12737 break;
12738 case 0x1d: /* SQRDMLAH */
12739 case 0x1f: /* SQRDMLSH */
12740 if (!dc_isar_feature(aa64_rdm, s)) {
12741 unallocated_encoding(s);
12742 return;
12744 break;
12745 case 0x0e: /* SDOT */
12746 case 0x1e: /* UDOT */
12747 if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12748 unallocated_encoding(s);
12749 return;
12751 break;
12752 case 0x11: /* FCMLA #0 */
12753 case 0x13: /* FCMLA #90 */
12754 case 0x15: /* FCMLA #180 */
12755 case 0x17: /* FCMLA #270 */
12756 if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
12757 unallocated_encoding(s);
12758 return;
12760 is_fp = 2;
12761 break;
12762 case 0x00: /* FMLAL */
12763 case 0x04: /* FMLSL */
12764 case 0x18: /* FMLAL2 */
12765 case 0x1c: /* FMLSL2 */
12766 if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
12767 unallocated_encoding(s);
12768 return;
12770 size = MO_16;
12771 /* is_fp, but we pass cpu_env not fp_status. */
12772 break;
12773 default:
12774 unallocated_encoding(s);
12775 return;
12778 switch (is_fp) {
12779 case 1: /* normal fp */
12780 /* convert insn encoded size to TCGMemOp size */
12781 switch (size) {
12782 case 0: /* half-precision */
12783 size = MO_16;
12784 is_fp16 = true;
12785 break;
12786 case MO_32: /* single precision */
12787 case MO_64: /* double precision */
12788 break;
12789 default:
12790 unallocated_encoding(s);
12791 return;
12793 break;
12795 case 2: /* complex fp */
12796 /* Each indexable element is a complex pair. */
12797 size += 1;
12798 switch (size) {
12799 case MO_32:
12800 if (h && !is_q) {
12801 unallocated_encoding(s);
12802 return;
12804 is_fp16 = true;
12805 break;
12806 case MO_64:
12807 break;
12808 default:
12809 unallocated_encoding(s);
12810 return;
12812 break;
12814 default: /* integer */
12815 switch (size) {
12816 case MO_8:
12817 case MO_64:
12818 unallocated_encoding(s);
12819 return;
12821 break;
12823 if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
12824 unallocated_encoding(s);
12825 return;
12828 /* Given TCGMemOp size, adjust register and indexing. */
12829 switch (size) {
12830 case MO_16:
12831 index = h << 2 | l << 1 | m;
12832 break;
12833 case MO_32:
12834 index = h << 1 | l;
12835 rm |= m << 4;
12836 break;
12837 case MO_64:
12838 if (l || !is_q) {
12839 unallocated_encoding(s);
12840 return;
12842 index = h;
12843 rm |= m << 4;
12844 break;
12845 default:
12846 g_assert_not_reached();
12849 if (!fp_access_check(s)) {
12850 return;
12853 if (is_fp) {
12854 fpst = get_fpstatus_ptr(is_fp16);
12855 } else {
12856 fpst = NULL;
12859 switch (16 * u + opcode) {
12860 case 0x0e: /* SDOT */
12861 case 0x1e: /* UDOT */
12862 gen_gvec_op3_ool(s, is_q, rd, rn, rm, index,
12863 u ? gen_helper_gvec_udot_idx_b
12864 : gen_helper_gvec_sdot_idx_b);
12865 return;
12866 case 0x11: /* FCMLA #0 */
12867 case 0x13: /* FCMLA #90 */
12868 case 0x15: /* FCMLA #180 */
12869 case 0x17: /* FCMLA #270 */
12871 int rot = extract32(insn, 13, 2);
12872 int data = (index << 2) | rot;
12873 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
12874 vec_full_reg_offset(s, rn),
12875 vec_full_reg_offset(s, rm), fpst,
12876 is_q ? 16 : 8, vec_full_reg_size(s), data,
12877 size == MO_64
12878 ? gen_helper_gvec_fcmlas_idx
12879 : gen_helper_gvec_fcmlah_idx);
12880 tcg_temp_free_ptr(fpst);
12882 return;
12884 case 0x00: /* FMLAL */
12885 case 0x04: /* FMLSL */
12886 case 0x18: /* FMLAL2 */
12887 case 0x1c: /* FMLSL2 */
12889 int is_s = extract32(opcode, 2, 1);
12890 int is_2 = u;
12891 int data = (index << 2) | (is_2 << 1) | is_s;
12892 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
12893 vec_full_reg_offset(s, rn),
12894 vec_full_reg_offset(s, rm), cpu_env,
12895 is_q ? 16 : 8, vec_full_reg_size(s),
12896 data, gen_helper_gvec_fmlal_idx_a64);
12898 return;
12901 if (size == 3) {
12902 TCGv_i64 tcg_idx = tcg_temp_new_i64();
12903 int pass;
12905 assert(is_fp && is_q && !is_long);
12907 read_vec_element(s, tcg_idx, rm, index, MO_64);
12909 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
12910 TCGv_i64 tcg_op = tcg_temp_new_i64();
12911 TCGv_i64 tcg_res = tcg_temp_new_i64();
12913 read_vec_element(s, tcg_op, rn, pass, MO_64);
12915 switch (16 * u + opcode) {
12916 case 0x05: /* FMLS */
12917 /* As usual for ARM, separate negation for fused multiply-add */
12918 gen_helper_vfp_negd(tcg_op, tcg_op);
12919 /* fall through */
12920 case 0x01: /* FMLA */
12921 read_vec_element(s, tcg_res, rd, pass, MO_64);
12922 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
12923 break;
12924 case 0x09: /* FMUL */
12925 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
12926 break;
12927 case 0x19: /* FMULX */
12928 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
12929 break;
12930 default:
12931 g_assert_not_reached();
12934 write_vec_element(s, tcg_res, rd, pass, MO_64);
12935 tcg_temp_free_i64(tcg_op);
12936 tcg_temp_free_i64(tcg_res);
12939 tcg_temp_free_i64(tcg_idx);
12940 clear_vec_high(s, !is_scalar, rd);
12941 } else if (!is_long) {
12942 /* 32 bit floating point, or 16 or 32 bit integer.
12943 * For the 16 bit scalar case we use the usual Neon helpers and
12944 * rely on the fact that 0 op 0 == 0 with no side effects.
12946 TCGv_i32 tcg_idx = tcg_temp_new_i32();
12947 int pass, maxpasses;
12949 if (is_scalar) {
12950 maxpasses = 1;
12951 } else {
12952 maxpasses = is_q ? 4 : 2;
12955 read_vec_element_i32(s, tcg_idx, rm, index, size);
12957 if (size == 1 && !is_scalar) {
12958 /* The simplest way to handle the 16x16 indexed ops is to duplicate
12959 * the index into both halves of the 32 bit tcg_idx and then use
12960 * the usual Neon helpers.
12962 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
12965 for (pass = 0; pass < maxpasses; pass++) {
12966 TCGv_i32 tcg_op = tcg_temp_new_i32();
12967 TCGv_i32 tcg_res = tcg_temp_new_i32();
12969 read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
12971 switch (16 * u + opcode) {
12972 case 0x08: /* MUL */
12973 case 0x10: /* MLA */
12974 case 0x14: /* MLS */
12976 static NeonGenTwoOpFn * const fns[2][2] = {
12977 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
12978 { tcg_gen_add_i32, tcg_gen_sub_i32 },
12980 NeonGenTwoOpFn *genfn;
12981 bool is_sub = opcode == 0x4;
12983 if (size == 1) {
12984 gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
12985 } else {
12986 tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
12988 if (opcode == 0x8) {
12989 break;
12991 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
12992 genfn = fns[size - 1][is_sub];
12993 genfn(tcg_res, tcg_op, tcg_res);
12994 break;
12996 case 0x05: /* FMLS */
12997 case 0x01: /* FMLA */
12998 read_vec_element_i32(s, tcg_res, rd, pass,
12999 is_scalar ? size : MO_32);
13000 switch (size) {
13001 case 1:
13002 if (opcode == 0x5) {
13003 /* As usual for ARM, separate negation for fused
13004 * multiply-add */
13005 tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13007 if (is_scalar) {
13008 gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13009 tcg_res, fpst);
13010 } else {
13011 gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13012 tcg_res, fpst);
13014 break;
13015 case 2:
13016 if (opcode == 0x5) {
13017 /* As usual for ARM, separate negation for
13018 * fused multiply-add */
13019 tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13021 gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13022 tcg_res, fpst);
13023 break;
13024 default:
13025 g_assert_not_reached();
13027 break;
13028 case 0x09: /* FMUL */
13029 switch (size) {
13030 case 1:
13031 if (is_scalar) {
13032 gen_helper_advsimd_mulh(tcg_res, tcg_op,
13033 tcg_idx, fpst);
13034 } else {
13035 gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13036 tcg_idx, fpst);
13038 break;
13039 case 2:
13040 gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13041 break;
13042 default:
13043 g_assert_not_reached();
13045 break;
13046 case 0x19: /* FMULX */
13047 switch (size) {
13048 case 1:
13049 if (is_scalar) {
13050 gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13051 tcg_idx, fpst);
13052 } else {
13053 gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13054 tcg_idx, fpst);
13056 break;
13057 case 2:
13058 gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13059 break;
13060 default:
13061 g_assert_not_reached();
13063 break;
13064 case 0x0c: /* SQDMULH */
13065 if (size == 1) {
13066 gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13067 tcg_op, tcg_idx);
13068 } else {
13069 gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13070 tcg_op, tcg_idx);
13072 break;
13073 case 0x0d: /* SQRDMULH */
13074 if (size == 1) {
13075 gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13076 tcg_op, tcg_idx);
13077 } else {
13078 gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13079 tcg_op, tcg_idx);
13081 break;
13082 case 0x1d: /* SQRDMLAH */
13083 read_vec_element_i32(s, tcg_res, rd, pass,
13084 is_scalar ? size : MO_32);
13085 if (size == 1) {
13086 gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13087 tcg_op, tcg_idx, tcg_res);
13088 } else {
13089 gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13090 tcg_op, tcg_idx, tcg_res);
13092 break;
13093 case 0x1f: /* SQRDMLSH */
13094 read_vec_element_i32(s, tcg_res, rd, pass,
13095 is_scalar ? size : MO_32);
13096 if (size == 1) {
13097 gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13098 tcg_op, tcg_idx, tcg_res);
13099 } else {
13100 gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13101 tcg_op, tcg_idx, tcg_res);
13103 break;
13104 default:
13105 g_assert_not_reached();
13108 if (is_scalar) {
13109 write_fp_sreg(s, rd, tcg_res);
13110 } else {
13111 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13114 tcg_temp_free_i32(tcg_op);
13115 tcg_temp_free_i32(tcg_res);
13118 tcg_temp_free_i32(tcg_idx);
13119 clear_vec_high(s, is_q, rd);
13120 } else {
13121 /* long ops: 16x16->32 or 32x32->64 */
13122 TCGv_i64 tcg_res[2];
13123 int pass;
13124 bool satop = extract32(opcode, 0, 1);
13125 TCGMemOp memop = MO_32;
13127 if (satop || !u) {
13128 memop |= MO_SIGN;
13131 if (size == 2) {
13132 TCGv_i64 tcg_idx = tcg_temp_new_i64();
13134 read_vec_element(s, tcg_idx, rm, index, memop);
13136 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13137 TCGv_i64 tcg_op = tcg_temp_new_i64();
13138 TCGv_i64 tcg_passres;
13139 int passelt;
13141 if (is_scalar) {
13142 passelt = 0;
13143 } else {
13144 passelt = pass + (is_q * 2);
13147 read_vec_element(s, tcg_op, rn, passelt, memop);
13149 tcg_res[pass] = tcg_temp_new_i64();
13151 if (opcode == 0xa || opcode == 0xb) {
13152 /* Non-accumulating ops */
13153 tcg_passres = tcg_res[pass];
13154 } else {
13155 tcg_passres = tcg_temp_new_i64();
13158 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13159 tcg_temp_free_i64(tcg_op);
13161 if (satop) {
13162 /* saturating, doubling */
13163 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13164 tcg_passres, tcg_passres);
13167 if (opcode == 0xa || opcode == 0xb) {
13168 continue;
13171 /* Accumulating op: handle accumulate step */
13172 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13174 switch (opcode) {
13175 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13176 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13177 break;
13178 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13179 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13180 break;
13181 case 0x7: /* SQDMLSL, SQDMLSL2 */
13182 tcg_gen_neg_i64(tcg_passres, tcg_passres);
13183 /* fall through */
13184 case 0x3: /* SQDMLAL, SQDMLAL2 */
13185 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13186 tcg_res[pass],
13187 tcg_passres);
13188 break;
13189 default:
13190 g_assert_not_reached();
13192 tcg_temp_free_i64(tcg_passres);
13194 tcg_temp_free_i64(tcg_idx);
13196 clear_vec_high(s, !is_scalar, rd);
13197 } else {
13198 TCGv_i32 tcg_idx = tcg_temp_new_i32();
13200 assert(size == 1);
13201 read_vec_element_i32(s, tcg_idx, rm, index, size);
13203 if (!is_scalar) {
13204 /* The simplest way to handle the 16x16 indexed ops is to
13205 * duplicate the index into both halves of the 32 bit tcg_idx
13206 * and then use the usual Neon helpers.
13208 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13211 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13212 TCGv_i32 tcg_op = tcg_temp_new_i32();
13213 TCGv_i64 tcg_passres;
13215 if (is_scalar) {
13216 read_vec_element_i32(s, tcg_op, rn, pass, size);
13217 } else {
13218 read_vec_element_i32(s, tcg_op, rn,
13219 pass + (is_q * 2), MO_32);
13222 tcg_res[pass] = tcg_temp_new_i64();
13224 if (opcode == 0xa || opcode == 0xb) {
13225 /* Non-accumulating ops */
13226 tcg_passres = tcg_res[pass];
13227 } else {
13228 tcg_passres = tcg_temp_new_i64();
13231 if (memop & MO_SIGN) {
13232 gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13233 } else {
13234 gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13236 if (satop) {
13237 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13238 tcg_passres, tcg_passres);
13240 tcg_temp_free_i32(tcg_op);
13242 if (opcode == 0xa || opcode == 0xb) {
13243 continue;
13246 /* Accumulating op: handle accumulate step */
13247 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13249 switch (opcode) {
13250 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13251 gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13252 tcg_passres);
13253 break;
13254 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13255 gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13256 tcg_passres);
13257 break;
13258 case 0x7: /* SQDMLSL, SQDMLSL2 */
13259 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13260 /* fall through */
13261 case 0x3: /* SQDMLAL, SQDMLAL2 */
13262 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13263 tcg_res[pass],
13264 tcg_passres);
13265 break;
13266 default:
13267 g_assert_not_reached();
13269 tcg_temp_free_i64(tcg_passres);
13271 tcg_temp_free_i32(tcg_idx);
13273 if (is_scalar) {
13274 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13278 if (is_scalar) {
13279 tcg_res[1] = tcg_const_i64(0);
13282 for (pass = 0; pass < 2; pass++) {
13283 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13284 tcg_temp_free_i64(tcg_res[pass]);
13288 if (fpst) {
13289 tcg_temp_free_ptr(fpst);
13293 /* Crypto AES
13294 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
13295 * +-----------------+------+-----------+--------+-----+------+------+
13296 * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
13297 * +-----------------+------+-----------+--------+-----+------+------+
13299 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13301 int size = extract32(insn, 22, 2);
13302 int opcode = extract32(insn, 12, 5);
13303 int rn = extract32(insn, 5, 5);
13304 int rd = extract32(insn, 0, 5);
13305 int decrypt;
13306 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr;
13307 TCGv_i32 tcg_decrypt;
13308 CryptoThreeOpIntFn *genfn;
13310 if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13311 unallocated_encoding(s);
13312 return;
13315 switch (opcode) {
13316 case 0x4: /* AESE */
13317 decrypt = 0;
13318 genfn = gen_helper_crypto_aese;
13319 break;
13320 case 0x6: /* AESMC */
13321 decrypt = 0;
13322 genfn = gen_helper_crypto_aesmc;
13323 break;
13324 case 0x5: /* AESD */
13325 decrypt = 1;
13326 genfn = gen_helper_crypto_aese;
13327 break;
13328 case 0x7: /* AESIMC */
13329 decrypt = 1;
13330 genfn = gen_helper_crypto_aesmc;
13331 break;
13332 default:
13333 unallocated_encoding(s);
13334 return;
13337 if (!fp_access_check(s)) {
13338 return;
13341 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13342 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
13343 tcg_decrypt = tcg_const_i32(decrypt);
13345 genfn(tcg_rd_ptr, tcg_rn_ptr, tcg_decrypt);
13347 tcg_temp_free_ptr(tcg_rd_ptr);
13348 tcg_temp_free_ptr(tcg_rn_ptr);
13349 tcg_temp_free_i32(tcg_decrypt);
13352 /* Crypto three-reg SHA
13353 * 31 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
13354 * +-----------------+------+---+------+---+--------+-----+------+------+
13355 * | 0 1 0 1 1 1 1 0 | size | 0 | Rm | 0 | opcode | 0 0 | Rn | Rd |
13356 * +-----------------+------+---+------+---+--------+-----+------+------+
13358 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13360 int size = extract32(insn, 22, 2);
13361 int opcode = extract32(insn, 12, 3);
13362 int rm = extract32(insn, 16, 5);
13363 int rn = extract32(insn, 5, 5);
13364 int rd = extract32(insn, 0, 5);
13365 CryptoThreeOpFn *genfn;
13366 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr;
13367 bool feature;
13369 if (size != 0) {
13370 unallocated_encoding(s);
13371 return;
13374 switch (opcode) {
13375 case 0: /* SHA1C */
13376 case 1: /* SHA1P */
13377 case 2: /* SHA1M */
13378 case 3: /* SHA1SU0 */
13379 genfn = NULL;
13380 feature = dc_isar_feature(aa64_sha1, s);
13381 break;
13382 case 4: /* SHA256H */
13383 genfn = gen_helper_crypto_sha256h;
13384 feature = dc_isar_feature(aa64_sha256, s);
13385 break;
13386 case 5: /* SHA256H2 */
13387 genfn = gen_helper_crypto_sha256h2;
13388 feature = dc_isar_feature(aa64_sha256, s);
13389 break;
13390 case 6: /* SHA256SU1 */
13391 genfn = gen_helper_crypto_sha256su1;
13392 feature = dc_isar_feature(aa64_sha256, s);
13393 break;
13394 default:
13395 unallocated_encoding(s);
13396 return;
13399 if (!feature) {
13400 unallocated_encoding(s);
13401 return;
13404 if (!fp_access_check(s)) {
13405 return;
13408 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13409 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
13410 tcg_rm_ptr = vec_full_reg_ptr(s, rm);
13412 if (genfn) {
13413 genfn(tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr);
13414 } else {
13415 TCGv_i32 tcg_opcode = tcg_const_i32(opcode);
13417 gen_helper_crypto_sha1_3reg(tcg_rd_ptr, tcg_rn_ptr,
13418 tcg_rm_ptr, tcg_opcode);
13419 tcg_temp_free_i32(tcg_opcode);
13422 tcg_temp_free_ptr(tcg_rd_ptr);
13423 tcg_temp_free_ptr(tcg_rn_ptr);
13424 tcg_temp_free_ptr(tcg_rm_ptr);
13427 /* Crypto two-reg SHA
13428 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
13429 * +-----------------+------+-----------+--------+-----+------+------+
13430 * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
13431 * +-----------------+------+-----------+--------+-----+------+------+
13433 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13435 int size = extract32(insn, 22, 2);
13436 int opcode = extract32(insn, 12, 5);
13437 int rn = extract32(insn, 5, 5);
13438 int rd = extract32(insn, 0, 5);
13439 CryptoTwoOpFn *genfn;
13440 bool feature;
13441 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr;
13443 if (size != 0) {
13444 unallocated_encoding(s);
13445 return;
13448 switch (opcode) {
13449 case 0: /* SHA1H */
13450 feature = dc_isar_feature(aa64_sha1, s);
13451 genfn = gen_helper_crypto_sha1h;
13452 break;
13453 case 1: /* SHA1SU1 */
13454 feature = dc_isar_feature(aa64_sha1, s);
13455 genfn = gen_helper_crypto_sha1su1;
13456 break;
13457 case 2: /* SHA256SU0 */
13458 feature = dc_isar_feature(aa64_sha256, s);
13459 genfn = gen_helper_crypto_sha256su0;
13460 break;
13461 default:
13462 unallocated_encoding(s);
13463 return;
13466 if (!feature) {
13467 unallocated_encoding(s);
13468 return;
13471 if (!fp_access_check(s)) {
13472 return;
13475 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13476 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
13478 genfn(tcg_rd_ptr, tcg_rn_ptr);
13480 tcg_temp_free_ptr(tcg_rd_ptr);
13481 tcg_temp_free_ptr(tcg_rn_ptr);
13484 /* Crypto three-reg SHA512
13485 * 31 21 20 16 15 14 13 12 11 10 9 5 4 0
13486 * +-----------------------+------+---+---+-----+--------+------+------+
13487 * | 1 1 0 0 1 1 1 0 0 1 1 | Rm | 1 | O | 0 0 | opcode | Rn | Rd |
13488 * +-----------------------+------+---+---+-----+--------+------+------+
13490 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13492 int opcode = extract32(insn, 10, 2);
13493 int o = extract32(insn, 14, 1);
13494 int rm = extract32(insn, 16, 5);
13495 int rn = extract32(insn, 5, 5);
13496 int rd = extract32(insn, 0, 5);
13497 bool feature;
13498 CryptoThreeOpFn *genfn;
13500 if (o == 0) {
13501 switch (opcode) {
13502 case 0: /* SHA512H */
13503 feature = dc_isar_feature(aa64_sha512, s);
13504 genfn = gen_helper_crypto_sha512h;
13505 break;
13506 case 1: /* SHA512H2 */
13507 feature = dc_isar_feature(aa64_sha512, s);
13508 genfn = gen_helper_crypto_sha512h2;
13509 break;
13510 case 2: /* SHA512SU1 */
13511 feature = dc_isar_feature(aa64_sha512, s);
13512 genfn = gen_helper_crypto_sha512su1;
13513 break;
13514 case 3: /* RAX1 */
13515 feature = dc_isar_feature(aa64_sha3, s);
13516 genfn = NULL;
13517 break;
13519 } else {
13520 switch (opcode) {
13521 case 0: /* SM3PARTW1 */
13522 feature = dc_isar_feature(aa64_sm3, s);
13523 genfn = gen_helper_crypto_sm3partw1;
13524 break;
13525 case 1: /* SM3PARTW2 */
13526 feature = dc_isar_feature(aa64_sm3, s);
13527 genfn = gen_helper_crypto_sm3partw2;
13528 break;
13529 case 2: /* SM4EKEY */
13530 feature = dc_isar_feature(aa64_sm4, s);
13531 genfn = gen_helper_crypto_sm4ekey;
13532 break;
13533 default:
13534 unallocated_encoding(s);
13535 return;
13539 if (!feature) {
13540 unallocated_encoding(s);
13541 return;
13544 if (!fp_access_check(s)) {
13545 return;
13548 if (genfn) {
13549 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr;
13551 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13552 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
13553 tcg_rm_ptr = vec_full_reg_ptr(s, rm);
13555 genfn(tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr);
13557 tcg_temp_free_ptr(tcg_rd_ptr);
13558 tcg_temp_free_ptr(tcg_rn_ptr);
13559 tcg_temp_free_ptr(tcg_rm_ptr);
13560 } else {
13561 TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
13562 int pass;
13564 tcg_op1 = tcg_temp_new_i64();
13565 tcg_op2 = tcg_temp_new_i64();
13566 tcg_res[0] = tcg_temp_new_i64();
13567 tcg_res[1] = tcg_temp_new_i64();
13569 for (pass = 0; pass < 2; pass++) {
13570 read_vec_element(s, tcg_op1, rn, pass, MO_64);
13571 read_vec_element(s, tcg_op2, rm, pass, MO_64);
13573 tcg_gen_rotli_i64(tcg_res[pass], tcg_op2, 1);
13574 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13576 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13577 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13579 tcg_temp_free_i64(tcg_op1);
13580 tcg_temp_free_i64(tcg_op2);
13581 tcg_temp_free_i64(tcg_res[0]);
13582 tcg_temp_free_i64(tcg_res[1]);
13586 /* Crypto two-reg SHA512
13587 * 31 12 11 10 9 5 4 0
13588 * +-----------------------------------------+--------+------+------+
13589 * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode | Rn | Rd |
13590 * +-----------------------------------------+--------+------+------+
13592 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13594 int opcode = extract32(insn, 10, 2);
13595 int rn = extract32(insn, 5, 5);
13596 int rd = extract32(insn, 0, 5);
13597 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr;
13598 bool feature;
13599 CryptoTwoOpFn *genfn;
13601 switch (opcode) {
13602 case 0: /* SHA512SU0 */
13603 feature = dc_isar_feature(aa64_sha512, s);
13604 genfn = gen_helper_crypto_sha512su0;
13605 break;
13606 case 1: /* SM4E */
13607 feature = dc_isar_feature(aa64_sm4, s);
13608 genfn = gen_helper_crypto_sm4e;
13609 break;
13610 default:
13611 unallocated_encoding(s);
13612 return;
13615 if (!feature) {
13616 unallocated_encoding(s);
13617 return;
13620 if (!fp_access_check(s)) {
13621 return;
13624 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13625 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
13627 genfn(tcg_rd_ptr, tcg_rn_ptr);
13629 tcg_temp_free_ptr(tcg_rd_ptr);
13630 tcg_temp_free_ptr(tcg_rn_ptr);
13633 /* Crypto four-register
13634 * 31 23 22 21 20 16 15 14 10 9 5 4 0
13635 * +-------------------+-----+------+---+------+------+------+
13636 * | 1 1 0 0 1 1 1 0 0 | Op0 | Rm | 0 | Ra | Rn | Rd |
13637 * +-------------------+-----+------+---+------+------+------+
13639 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13641 int op0 = extract32(insn, 21, 2);
13642 int rm = extract32(insn, 16, 5);
13643 int ra = extract32(insn, 10, 5);
13644 int rn = extract32(insn, 5, 5);
13645 int rd = extract32(insn, 0, 5);
13646 bool feature;
13648 switch (op0) {
13649 case 0: /* EOR3 */
13650 case 1: /* BCAX */
13651 feature = dc_isar_feature(aa64_sha3, s);
13652 break;
13653 case 2: /* SM3SS1 */
13654 feature = dc_isar_feature(aa64_sm3, s);
13655 break;
13656 default:
13657 unallocated_encoding(s);
13658 return;
13661 if (!feature) {
13662 unallocated_encoding(s);
13663 return;
13666 if (!fp_access_check(s)) {
13667 return;
13670 if (op0 < 2) {
13671 TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13672 int pass;
13674 tcg_op1 = tcg_temp_new_i64();
13675 tcg_op2 = tcg_temp_new_i64();
13676 tcg_op3 = tcg_temp_new_i64();
13677 tcg_res[0] = tcg_temp_new_i64();
13678 tcg_res[1] = tcg_temp_new_i64();
13680 for (pass = 0; pass < 2; pass++) {
13681 read_vec_element(s, tcg_op1, rn, pass, MO_64);
13682 read_vec_element(s, tcg_op2, rm, pass, MO_64);
13683 read_vec_element(s, tcg_op3, ra, pass, MO_64);
13685 if (op0 == 0) {
13686 /* EOR3 */
13687 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13688 } else {
13689 /* BCAX */
13690 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13692 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13694 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13695 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13697 tcg_temp_free_i64(tcg_op1);
13698 tcg_temp_free_i64(tcg_op2);
13699 tcg_temp_free_i64(tcg_op3);
13700 tcg_temp_free_i64(tcg_res[0]);
13701 tcg_temp_free_i64(tcg_res[1]);
13702 } else {
13703 TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13705 tcg_op1 = tcg_temp_new_i32();
13706 tcg_op2 = tcg_temp_new_i32();
13707 tcg_op3 = tcg_temp_new_i32();
13708 tcg_res = tcg_temp_new_i32();
13709 tcg_zero = tcg_const_i32(0);
13711 read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13712 read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13713 read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13715 tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13716 tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13717 tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13718 tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13720 write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13721 write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13722 write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13723 write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13725 tcg_temp_free_i32(tcg_op1);
13726 tcg_temp_free_i32(tcg_op2);
13727 tcg_temp_free_i32(tcg_op3);
13728 tcg_temp_free_i32(tcg_res);
13729 tcg_temp_free_i32(tcg_zero);
13733 /* Crypto XAR
13734 * 31 21 20 16 15 10 9 5 4 0
13735 * +-----------------------+------+--------+------+------+
13736 * | 1 1 0 0 1 1 1 0 1 0 0 | Rm | imm6 | Rn | Rd |
13737 * +-----------------------+------+--------+------+------+
13739 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
13741 int rm = extract32(insn, 16, 5);
13742 int imm6 = extract32(insn, 10, 6);
13743 int rn = extract32(insn, 5, 5);
13744 int rd = extract32(insn, 0, 5);
13745 TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
13746 int pass;
13748 if (!dc_isar_feature(aa64_sha3, s)) {
13749 unallocated_encoding(s);
13750 return;
13753 if (!fp_access_check(s)) {
13754 return;
13757 tcg_op1 = tcg_temp_new_i64();
13758 tcg_op2 = tcg_temp_new_i64();
13759 tcg_res[0] = tcg_temp_new_i64();
13760 tcg_res[1] = tcg_temp_new_i64();
13762 for (pass = 0; pass < 2; pass++) {
13763 read_vec_element(s, tcg_op1, rn, pass, MO_64);
13764 read_vec_element(s, tcg_op2, rm, pass, MO_64);
13766 tcg_gen_xor_i64(tcg_res[pass], tcg_op1, tcg_op2);
13767 tcg_gen_rotri_i64(tcg_res[pass], tcg_res[pass], imm6);
13769 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13770 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13772 tcg_temp_free_i64(tcg_op1);
13773 tcg_temp_free_i64(tcg_op2);
13774 tcg_temp_free_i64(tcg_res[0]);
13775 tcg_temp_free_i64(tcg_res[1]);
13778 /* Crypto three-reg imm2
13779 * 31 21 20 16 15 14 13 12 11 10 9 5 4 0
13780 * +-----------------------+------+-----+------+--------+------+------+
13781 * | 1 1 0 0 1 1 1 0 0 1 0 | Rm | 1 0 | imm2 | opcode | Rn | Rd |
13782 * +-----------------------+------+-----+------+--------+------+------+
13784 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
13786 int opcode = extract32(insn, 10, 2);
13787 int imm2 = extract32(insn, 12, 2);
13788 int rm = extract32(insn, 16, 5);
13789 int rn = extract32(insn, 5, 5);
13790 int rd = extract32(insn, 0, 5);
13791 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr;
13792 TCGv_i32 tcg_imm2, tcg_opcode;
13794 if (!dc_isar_feature(aa64_sm3, s)) {
13795 unallocated_encoding(s);
13796 return;
13799 if (!fp_access_check(s)) {
13800 return;
13803 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13804 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
13805 tcg_rm_ptr = vec_full_reg_ptr(s, rm);
13806 tcg_imm2 = tcg_const_i32(imm2);
13807 tcg_opcode = tcg_const_i32(opcode);
13809 gen_helper_crypto_sm3tt(tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr, tcg_imm2,
13810 tcg_opcode);
13812 tcg_temp_free_ptr(tcg_rd_ptr);
13813 tcg_temp_free_ptr(tcg_rn_ptr);
13814 tcg_temp_free_ptr(tcg_rm_ptr);
13815 tcg_temp_free_i32(tcg_imm2);
13816 tcg_temp_free_i32(tcg_opcode);
13819 /* C3.6 Data processing - SIMD, inc Crypto
13821 * As the decode gets a little complex we are using a table based
13822 * approach for this part of the decode.
13824 static const AArch64DecodeTable data_proc_simd[] = {
13825 /* pattern , mask , fn */
13826 { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
13827 { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
13828 { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
13829 { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
13830 { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
13831 { 0x0e000400, 0x9fe08400, disas_simd_copy },
13832 { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
13833 /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
13834 { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
13835 { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
13836 { 0x0e000000, 0xbf208c00, disas_simd_tb },
13837 { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
13838 { 0x2e000000, 0xbf208400, disas_simd_ext },
13839 { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
13840 { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
13841 { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
13842 { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
13843 { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
13844 { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
13845 { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
13846 { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
13847 { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
13848 { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
13849 { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
13850 { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
13851 { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
13852 { 0xce000000, 0xff808000, disas_crypto_four_reg },
13853 { 0xce800000, 0xffe00000, disas_crypto_xar },
13854 { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
13855 { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
13856 { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
13857 { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
13858 { 0x00000000, 0x00000000, NULL }
13861 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
13863 /* Note that this is called with all non-FP cases from
13864 * table C3-6 so it must UNDEF for entries not specifically
13865 * allocated to instructions in that table.
13867 AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
13868 if (fn) {
13869 fn(s, insn);
13870 } else {
13871 unallocated_encoding(s);
13875 /* C3.6 Data processing - SIMD and floating point */
13876 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
13878 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
13879 disas_data_proc_fp(s, insn);
13880 } else {
13881 /* SIMD, including crypto */
13882 disas_data_proc_simd(s, insn);
13887 * is_guarded_page:
13888 * @env: The cpu environment
13889 * @s: The DisasContext
13891 * Return true if the page is guarded.
13893 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
13895 #ifdef CONFIG_USER_ONLY
13896 return false; /* FIXME */
13897 #else
13898 uint64_t addr = s->base.pc_first;
13899 int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
13900 unsigned int index = tlb_index(env, mmu_idx, addr);
13901 CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr);
13904 * We test this immediately after reading an insn, which means
13905 * that any normal page must be in the TLB. The only exception
13906 * would be for executing from flash or device memory, which
13907 * does not retain the TLB entry.
13909 * FIXME: Assume false for those, for now. We could use
13910 * arm_cpu_get_phys_page_attrs_debug to re-read the page
13911 * table entry even for that case.
13913 return (tlb_hit(entry->addr_code, addr) &&
13914 env->iotlb[mmu_idx][index].attrs.target_tlb_bit0);
13915 #endif
13919 * btype_destination_ok:
13920 * @insn: The instruction at the branch destination
13921 * @bt: SCTLR_ELx.BT
13922 * @btype: PSTATE.BTYPE, and is non-zero
13924 * On a guarded page, there are a limited number of insns
13925 * that may be present at the branch target:
13926 * - branch target identifiers,
13927 * - paciasp, pacibsp,
13928 * - BRK insn
13929 * - HLT insn
13930 * Anything else causes a Branch Target Exception.
13932 * Return true if the branch is compatible, false to raise BTITRAP.
13934 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
13936 if ((insn & 0xfffff01fu) == 0xd503201fu) {
13937 /* HINT space */
13938 switch (extract32(insn, 5, 7)) {
13939 case 0b011001: /* PACIASP */
13940 case 0b011011: /* PACIBSP */
13942 * If SCTLR_ELx.BT, then PACI*SP are not compatible
13943 * with btype == 3. Otherwise all btype are ok.
13945 return !bt || btype != 3;
13946 case 0b100000: /* BTI */
13947 /* Not compatible with any btype. */
13948 return false;
13949 case 0b100010: /* BTI c */
13950 /* Not compatible with btype == 3 */
13951 return btype != 3;
13952 case 0b100100: /* BTI j */
13953 /* Not compatible with btype == 2 */
13954 return btype != 2;
13955 case 0b100110: /* BTI jc */
13956 /* Compatible with any btype. */
13957 return true;
13959 } else {
13960 switch (insn & 0xffe0001fu) {
13961 case 0xd4200000u: /* BRK */
13962 case 0xd4400000u: /* HLT */
13963 /* Give priority to the breakpoint exception. */
13964 return true;
13967 return false;
13970 /* C3.1 A64 instruction index by encoding */
13971 static void disas_a64_insn(CPUARMState *env, DisasContext *s)
13973 uint32_t insn;
13975 insn = arm_ldl_code(env, s->pc, s->sctlr_b);
13976 s->insn = insn;
13977 s->pc += 4;
13979 s->fp_access_checked = false;
13981 if (dc_isar_feature(aa64_bti, s)) {
13982 if (s->base.num_insns == 1) {
13984 * At the first insn of the TB, compute s->guarded_page.
13985 * We delayed computing this until successfully reading
13986 * the first insn of the TB, above. This (mostly) ensures
13987 * that the softmmu tlb entry has been populated, and the
13988 * page table GP bit is available.
13990 * Note that we need to compute this even if btype == 0,
13991 * because this value is used for BR instructions later
13992 * where ENV is not available.
13994 s->guarded_page = is_guarded_page(env, s);
13996 /* First insn can have btype set to non-zero. */
13997 tcg_debug_assert(s->btype >= 0);
14000 * Note that the Branch Target Exception has fairly high
14001 * priority -- below debugging exceptions but above most
14002 * everything else. This allows us to handle this now
14003 * instead of waiting until the insn is otherwise decoded.
14005 if (s->btype != 0
14006 && s->guarded_page
14007 && !btype_destination_ok(insn, s->bt, s->btype)) {
14008 gen_exception_insn(s, 4, EXCP_UDEF, syn_btitrap(s->btype),
14009 default_exception_el(s));
14010 return;
14012 } else {
14013 /* Not the first insn: btype must be 0. */
14014 tcg_debug_assert(s->btype == 0);
14018 switch (extract32(insn, 25, 4)) {
14019 case 0x0: case 0x1: case 0x3: /* UNALLOCATED */
14020 unallocated_encoding(s);
14021 break;
14022 case 0x2:
14023 if (!dc_isar_feature(aa64_sve, s) || !disas_sve(s, insn)) {
14024 unallocated_encoding(s);
14026 break;
14027 case 0x8: case 0x9: /* Data processing - immediate */
14028 disas_data_proc_imm(s, insn);
14029 break;
14030 case 0xa: case 0xb: /* Branch, exception generation and system insns */
14031 disas_b_exc_sys(s, insn);
14032 break;
14033 case 0x4:
14034 case 0x6:
14035 case 0xc:
14036 case 0xe: /* Loads and stores */
14037 disas_ldst(s, insn);
14038 break;
14039 case 0x5:
14040 case 0xd: /* Data processing - register */
14041 disas_data_proc_reg(s, insn);
14042 break;
14043 case 0x7:
14044 case 0xf: /* Data processing - SIMD and floating point */
14045 disas_data_proc_simd_fp(s, insn);
14046 break;
14047 default:
14048 assert(FALSE); /* all 15 cases should be handled above */
14049 break;
14052 /* if we allocated any temporaries, free them here */
14053 free_tmp_a64(s);
14056 * After execution of most insns, btype is reset to 0.
14057 * Note that we set btype == -1 when the insn sets btype.
14059 if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14060 reset_btype(s);
14064 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14065 CPUState *cpu)
14067 DisasContext *dc = container_of(dcbase, DisasContext, base);
14068 CPUARMState *env = cpu->env_ptr;
14069 ARMCPU *arm_cpu = arm_env_get_cpu(env);
14070 uint32_t tb_flags = dc->base.tb->flags;
14071 int bound, core_mmu_idx;
14073 dc->isar = &arm_cpu->isar;
14074 dc->pc = dc->base.pc_first;
14075 dc->condjmp = 0;
14077 dc->aarch64 = 1;
14078 /* If we are coming from secure EL0 in a system with a 32-bit EL3, then
14079 * there is no secure EL1, so we route exceptions to EL3.
14081 dc->secure_routed_to_el3 = arm_feature(env, ARM_FEATURE_EL3) &&
14082 !arm_el_is_aa64(env, 3);
14083 dc->thumb = 0;
14084 dc->sctlr_b = 0;
14085 dc->be_data = FIELD_EX32(tb_flags, TBFLAG_ANY, BE_DATA) ? MO_BE : MO_LE;
14086 dc->condexec_mask = 0;
14087 dc->condexec_cond = 0;
14088 core_mmu_idx = FIELD_EX32(tb_flags, TBFLAG_ANY, MMUIDX);
14089 dc->mmu_idx = core_to_arm_mmu_idx(env, core_mmu_idx);
14090 dc->tbii = FIELD_EX32(tb_flags, TBFLAG_A64, TBII);
14091 dc->tbid = FIELD_EX32(tb_flags, TBFLAG_A64, TBID);
14092 dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14093 #if !defined(CONFIG_USER_ONLY)
14094 dc->user = (dc->current_el == 0);
14095 #endif
14096 dc->fp_excp_el = FIELD_EX32(tb_flags, TBFLAG_ANY, FPEXC_EL);
14097 dc->sve_excp_el = FIELD_EX32(tb_flags, TBFLAG_A64, SVEEXC_EL);
14098 dc->sve_len = (FIELD_EX32(tb_flags, TBFLAG_A64, ZCR_LEN) + 1) * 16;
14099 dc->pauth_active = FIELD_EX32(tb_flags, TBFLAG_A64, PAUTH_ACTIVE);
14100 dc->bt = FIELD_EX32(tb_flags, TBFLAG_A64, BT);
14101 dc->btype = FIELD_EX32(tb_flags, TBFLAG_A64, BTYPE);
14102 dc->vec_len = 0;
14103 dc->vec_stride = 0;
14104 dc->cp_regs = arm_cpu->cp_regs;
14105 dc->features = env->features;
14107 /* Single step state. The code-generation logic here is:
14108 * SS_ACTIVE == 0:
14109 * generate code with no special handling for single-stepping (except
14110 * that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14111 * this happens anyway because those changes are all system register or
14112 * PSTATE writes).
14113 * SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14114 * emit code for one insn
14115 * emit code to clear PSTATE.SS
14116 * emit code to generate software step exception for completed step
14117 * end TB (as usual for having generated an exception)
14118 * SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14119 * emit code to generate a software step exception
14120 * end the TB
14122 dc->ss_active = FIELD_EX32(tb_flags, TBFLAG_ANY, SS_ACTIVE);
14123 dc->pstate_ss = FIELD_EX32(tb_flags, TBFLAG_ANY, PSTATE_SS);
14124 dc->is_ldex = false;
14125 dc->ss_same_el = (arm_debug_target_el(env) == dc->current_el);
14127 /* Bound the number of insns to execute to those left on the page. */
14128 bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14130 /* If architectural single step active, limit to 1. */
14131 if (dc->ss_active) {
14132 bound = 1;
14134 dc->base.max_insns = MIN(dc->base.max_insns, bound);
14136 init_tmp_a64_array(dc);
14139 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14143 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14145 DisasContext *dc = container_of(dcbase, DisasContext, base);
14147 tcg_gen_insn_start(dc->pc, 0, 0);
14148 dc->insn_start = tcg_last_op();
14151 static bool aarch64_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,
14152 const CPUBreakpoint *bp)
14154 DisasContext *dc = container_of(dcbase, DisasContext, base);
14156 if (bp->flags & BP_CPU) {
14157 gen_a64_set_pc_im(dc->pc);
14158 gen_helper_check_breakpoints(cpu_env);
14159 /* End the TB early; it likely won't be executed */
14160 dc->base.is_jmp = DISAS_TOO_MANY;
14161 } else {
14162 gen_exception_internal_insn(dc, 0, EXCP_DEBUG);
14163 /* The address covered by the breakpoint must be
14164 included in [tb->pc, tb->pc + tb->size) in order
14165 to for it to be properly cleared -- thus we
14166 increment the PC here so that the logic setting
14167 tb->size below does the right thing. */
14168 dc->pc += 4;
14169 dc->base.is_jmp = DISAS_NORETURN;
14172 return true;
14175 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14177 DisasContext *dc = container_of(dcbase, DisasContext, base);
14178 CPUARMState *env = cpu->env_ptr;
14180 if (dc->ss_active && !dc->pstate_ss) {
14181 /* Singlestep state is Active-pending.
14182 * If we're in this state at the start of a TB then either
14183 * a) we just took an exception to an EL which is being debugged
14184 * and this is the first insn in the exception handler
14185 * b) debug exceptions were masked and we just unmasked them
14186 * without changing EL (eg by clearing PSTATE.D)
14187 * In either case we're going to take a swstep exception in the
14188 * "did not step an insn" case, and so the syndrome ISV and EX
14189 * bits should be zero.
14191 assert(dc->base.num_insns == 1);
14192 gen_exception(EXCP_UDEF, syn_swstep(dc->ss_same_el, 0, 0),
14193 default_exception_el(dc));
14194 dc->base.is_jmp = DISAS_NORETURN;
14195 } else {
14196 disas_a64_insn(env, dc);
14199 dc->base.pc_next = dc->pc;
14200 translator_loop_temp_check(&dc->base);
14203 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14205 DisasContext *dc = container_of(dcbase, DisasContext, base);
14207 if (unlikely(dc->base.singlestep_enabled || dc->ss_active)) {
14208 /* Note that this means single stepping WFI doesn't halt the CPU.
14209 * For conditional branch insns this is harmless unreachable code as
14210 * gen_goto_tb() has already handled emitting the debug exception
14211 * (and thus a tb-jump is not possible when singlestepping).
14213 switch (dc->base.is_jmp) {
14214 default:
14215 gen_a64_set_pc_im(dc->pc);
14216 /* fall through */
14217 case DISAS_EXIT:
14218 case DISAS_JUMP:
14219 if (dc->base.singlestep_enabled) {
14220 gen_exception_internal(EXCP_DEBUG);
14221 } else {
14222 gen_step_complete_exception(dc);
14224 break;
14225 case DISAS_NORETURN:
14226 break;
14228 } else {
14229 switch (dc->base.is_jmp) {
14230 case DISAS_NEXT:
14231 case DISAS_TOO_MANY:
14232 gen_goto_tb(dc, 1, dc->pc);
14233 break;
14234 default:
14235 case DISAS_UPDATE:
14236 gen_a64_set_pc_im(dc->pc);
14237 /* fall through */
14238 case DISAS_EXIT:
14239 tcg_gen_exit_tb(NULL, 0);
14240 break;
14241 case DISAS_JUMP:
14242 tcg_gen_lookup_and_goto_ptr();
14243 break;
14244 case DISAS_NORETURN:
14245 case DISAS_SWI:
14246 break;
14247 case DISAS_WFE:
14248 gen_a64_set_pc_im(dc->pc);
14249 gen_helper_wfe(cpu_env);
14250 break;
14251 case DISAS_YIELD:
14252 gen_a64_set_pc_im(dc->pc);
14253 gen_helper_yield(cpu_env);
14254 break;
14255 case DISAS_WFI:
14257 /* This is a special case because we don't want to just halt the CPU
14258 * if trying to debug across a WFI.
14260 TCGv_i32 tmp = tcg_const_i32(4);
14262 gen_a64_set_pc_im(dc->pc);
14263 gen_helper_wfi(cpu_env, tmp);
14264 tcg_temp_free_i32(tmp);
14265 /* The helper doesn't necessarily throw an exception, but we
14266 * must go back to the main loop to check for interrupts anyway.
14268 tcg_gen_exit_tb(NULL, 0);
14269 break;
14274 /* Functions above can change dc->pc, so re-align db->pc_next */
14275 dc->base.pc_next = dc->pc;
14278 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14279 CPUState *cpu)
14281 DisasContext *dc = container_of(dcbase, DisasContext, base);
14283 qemu_log("IN: %s\n", lookup_symbol(dc->base.pc_first));
14284 log_target_disas(cpu, dc->base.pc_first, dc->base.tb->size);
14287 const TranslatorOps aarch64_translator_ops = {
14288 .init_disas_context = aarch64_tr_init_disas_context,
14289 .tb_start = aarch64_tr_tb_start,
14290 .insn_start = aarch64_tr_insn_start,
14291 .breakpoint_check = aarch64_tr_breakpoint_check,
14292 .translate_insn = aarch64_tr_translate_insn,
14293 .tb_stop = aarch64_tr_tb_stop,
14294 .disas_log = aarch64_tr_disas_log,