target/arm: Implement ARMv8.4-CondM
[qemu/ar7.git] / target / arm / translate-a64.c
blob3cc9a99a9cb316ab25b37a6f070a0af9d9f98807
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_step_complete_exception(DisasContext *s)
426 /* We just completed step of an insn. Move from Active-not-pending
427 * to Active-pending, and then also take the swstep exception.
428 * This corresponds to making the (IMPDEF) choice to prioritize
429 * swstep exceptions over asynchronous exceptions taken to an exception
430 * level where debug is disabled. This choice has the advantage that
431 * we do not need to maintain internal state corresponding to the
432 * ISV/EX syndrome bits between completion of the step and generation
433 * of the exception, and our syndrome information is always correct.
435 gen_ss_advance(s);
436 gen_exception(EXCP_UDEF, syn_swstep(s->ss_same_el, 1, s->is_ldex),
437 default_exception_el(s));
438 s->base.is_jmp = DISAS_NORETURN;
441 static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
443 /* No direct tb linking with singlestep (either QEMU's or the ARM
444 * debug architecture kind) or deterministic io
446 if (s->base.singlestep_enabled || s->ss_active ||
447 (tb_cflags(s->base.tb) & CF_LAST_IO)) {
448 return false;
451 #ifndef CONFIG_USER_ONLY
452 /* Only link tbs from inside the same guest page */
453 if ((s->base.tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
454 return false;
456 #endif
458 return true;
461 static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
463 TranslationBlock *tb;
465 tb = s->base.tb;
466 if (use_goto_tb(s, n, dest)) {
467 tcg_gen_goto_tb(n);
468 gen_a64_set_pc_im(dest);
469 tcg_gen_exit_tb(tb, n);
470 s->base.is_jmp = DISAS_NORETURN;
471 } else {
472 gen_a64_set_pc_im(dest);
473 if (s->ss_active) {
474 gen_step_complete_exception(s);
475 } else if (s->base.singlestep_enabled) {
476 gen_exception_internal(EXCP_DEBUG);
477 } else {
478 tcg_gen_lookup_and_goto_ptr();
479 s->base.is_jmp = DISAS_NORETURN;
484 void unallocated_encoding(DisasContext *s)
486 /* Unallocated and reserved encodings are uncategorized */
487 gen_exception_insn(s, 4, EXCP_UDEF, syn_uncategorized(),
488 default_exception_el(s));
491 static void init_tmp_a64_array(DisasContext *s)
493 #ifdef CONFIG_DEBUG_TCG
494 memset(s->tmp_a64, 0, sizeof(s->tmp_a64));
495 #endif
496 s->tmp_a64_count = 0;
499 static void free_tmp_a64(DisasContext *s)
501 int i;
502 for (i = 0; i < s->tmp_a64_count; i++) {
503 tcg_temp_free_i64(s->tmp_a64[i]);
505 init_tmp_a64_array(s);
508 TCGv_i64 new_tmp_a64(DisasContext *s)
510 assert(s->tmp_a64_count < TMP_A64_MAX);
511 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
514 TCGv_i64 new_tmp_a64_zero(DisasContext *s)
516 TCGv_i64 t = new_tmp_a64(s);
517 tcg_gen_movi_i64(t, 0);
518 return t;
522 * Register access functions
524 * These functions are used for directly accessing a register in where
525 * changes to the final register value are likely to be made. If you
526 * need to use a register for temporary calculation (e.g. index type
527 * operations) use the read_* form.
529 * B1.2.1 Register mappings
531 * In instruction register encoding 31 can refer to ZR (zero register) or
532 * the SP (stack pointer) depending on context. In QEMU's case we map SP
533 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
534 * This is the point of the _sp forms.
536 TCGv_i64 cpu_reg(DisasContext *s, int reg)
538 if (reg == 31) {
539 return new_tmp_a64_zero(s);
540 } else {
541 return cpu_X[reg];
545 /* register access for when 31 == SP */
546 TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
548 return cpu_X[reg];
551 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
552 * representing the register contents. This TCGv is an auto-freed
553 * temporary so it need not be explicitly freed, and may be modified.
555 TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
557 TCGv_i64 v = new_tmp_a64(s);
558 if (reg != 31) {
559 if (sf) {
560 tcg_gen_mov_i64(v, cpu_X[reg]);
561 } else {
562 tcg_gen_ext32u_i64(v, cpu_X[reg]);
564 } else {
565 tcg_gen_movi_i64(v, 0);
567 return v;
570 TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
572 TCGv_i64 v = new_tmp_a64(s);
573 if (sf) {
574 tcg_gen_mov_i64(v, cpu_X[reg]);
575 } else {
576 tcg_gen_ext32u_i64(v, cpu_X[reg]);
578 return v;
581 /* Return the offset into CPUARMState of a slice (from
582 * the least significant end) of FP register Qn (ie
583 * Dn, Sn, Hn or Bn).
584 * (Note that this is not the same mapping as for A32; see cpu.h)
586 static inline int fp_reg_offset(DisasContext *s, int regno, TCGMemOp size)
588 return vec_reg_offset(s, regno, 0, size);
591 /* Offset of the high half of the 128 bit vector Qn */
592 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
594 return vec_reg_offset(s, regno, 1, MO_64);
597 /* Convenience accessors for reading and writing single and double
598 * FP registers. Writing clears the upper parts of the associated
599 * 128 bit vector register, as required by the architecture.
600 * Note that unlike the GP register accessors, the values returned
601 * by the read functions must be manually freed.
603 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
605 TCGv_i64 v = tcg_temp_new_i64();
607 tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
608 return v;
611 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
613 TCGv_i32 v = tcg_temp_new_i32();
615 tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
616 return v;
619 static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
621 TCGv_i32 v = tcg_temp_new_i32();
623 tcg_gen_ld16u_i32(v, cpu_env, fp_reg_offset(s, reg, MO_16));
624 return v;
627 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
628 * If SVE is not enabled, then there are only 128 bits in the vector.
630 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
632 unsigned ofs = fp_reg_offset(s, rd, MO_64);
633 unsigned vsz = vec_full_reg_size(s);
635 if (!is_q) {
636 TCGv_i64 tcg_zero = tcg_const_i64(0);
637 tcg_gen_st_i64(tcg_zero, cpu_env, ofs + 8);
638 tcg_temp_free_i64(tcg_zero);
640 if (vsz > 16) {
641 tcg_gen_gvec_dup8i(ofs + 16, vsz - 16, vsz - 16, 0);
645 void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
647 unsigned ofs = fp_reg_offset(s, reg, MO_64);
649 tcg_gen_st_i64(v, cpu_env, ofs);
650 clear_vec_high(s, false, reg);
653 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
655 TCGv_i64 tmp = tcg_temp_new_i64();
657 tcg_gen_extu_i32_i64(tmp, v);
658 write_fp_dreg(s, reg, tmp);
659 tcg_temp_free_i64(tmp);
662 TCGv_ptr get_fpstatus_ptr(bool is_f16)
664 TCGv_ptr statusptr = tcg_temp_new_ptr();
665 int offset;
667 /* In A64 all instructions (both FP and Neon) use the FPCR; there
668 * is no equivalent of the A32 Neon "standard FPSCR value".
669 * However half-precision operations operate under a different
670 * FZ16 flag and use vfp.fp_status_f16 instead of vfp.fp_status.
672 if (is_f16) {
673 offset = offsetof(CPUARMState, vfp.fp_status_f16);
674 } else {
675 offset = offsetof(CPUARMState, vfp.fp_status);
677 tcg_gen_addi_ptr(statusptr, cpu_env, offset);
678 return statusptr;
681 /* Expand a 2-operand AdvSIMD vector operation using an expander function. */
682 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
683 GVecGen2Fn *gvec_fn, int vece)
685 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
686 is_q ? 16 : 8, vec_full_reg_size(s));
689 /* Expand a 2-operand + immediate AdvSIMD vector operation using
690 * an expander function.
692 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
693 int64_t imm, GVecGen2iFn *gvec_fn, int vece)
695 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
696 imm, is_q ? 16 : 8, vec_full_reg_size(s));
699 /* Expand a 3-operand AdvSIMD vector operation using an expander function. */
700 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
701 GVecGen3Fn *gvec_fn, int vece)
703 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
704 vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
707 /* Expand a 2-operand + immediate AdvSIMD vector operation using
708 * an op descriptor.
710 static void gen_gvec_op2i(DisasContext *s, bool is_q, int rd,
711 int rn, int64_t imm, const GVecGen2i *gvec_op)
713 tcg_gen_gvec_2i(vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
714 is_q ? 16 : 8, vec_full_reg_size(s), imm, gvec_op);
717 /* Expand a 3-operand AdvSIMD vector operation using an op descriptor. */
718 static void gen_gvec_op3(DisasContext *s, bool is_q, int rd,
719 int rn, int rm, const GVecGen3 *gvec_op)
721 tcg_gen_gvec_3(vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
722 vec_full_reg_offset(s, rm), is_q ? 16 : 8,
723 vec_full_reg_size(s), gvec_op);
726 /* Expand a 3-operand operation using an out-of-line helper. */
727 static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
728 int rn, int rm, int data, gen_helper_gvec_3 *fn)
730 tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
731 vec_full_reg_offset(s, rn),
732 vec_full_reg_offset(s, rm),
733 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
736 /* Expand a 3-operand + env pointer operation using
737 * an out-of-line helper.
739 static void gen_gvec_op3_env(DisasContext *s, bool is_q, int rd,
740 int rn, int rm, gen_helper_gvec_3_ptr *fn)
742 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
743 vec_full_reg_offset(s, rn),
744 vec_full_reg_offset(s, rm), cpu_env,
745 is_q ? 16 : 8, vec_full_reg_size(s), 0, fn);
748 /* Expand a 3-operand + fpstatus pointer + simd data value operation using
749 * an out-of-line helper.
751 static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
752 int rm, bool is_fp16, int data,
753 gen_helper_gvec_3_ptr *fn)
755 TCGv_ptr fpst = get_fpstatus_ptr(is_fp16);
756 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
757 vec_full_reg_offset(s, rn),
758 vec_full_reg_offset(s, rm), fpst,
759 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
760 tcg_temp_free_ptr(fpst);
763 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
764 * than the 32 bit equivalent.
766 static inline void gen_set_NZ64(TCGv_i64 result)
768 tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
769 tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
772 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
773 static inline void gen_logic_CC(int sf, TCGv_i64 result)
775 if (sf) {
776 gen_set_NZ64(result);
777 } else {
778 tcg_gen_extrl_i64_i32(cpu_ZF, result);
779 tcg_gen_mov_i32(cpu_NF, cpu_ZF);
781 tcg_gen_movi_i32(cpu_CF, 0);
782 tcg_gen_movi_i32(cpu_VF, 0);
785 /* dest = T0 + T1; compute C, N, V and Z flags */
786 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
788 if (sf) {
789 TCGv_i64 result, flag, tmp;
790 result = tcg_temp_new_i64();
791 flag = tcg_temp_new_i64();
792 tmp = tcg_temp_new_i64();
794 tcg_gen_movi_i64(tmp, 0);
795 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
797 tcg_gen_extrl_i64_i32(cpu_CF, flag);
799 gen_set_NZ64(result);
801 tcg_gen_xor_i64(flag, result, t0);
802 tcg_gen_xor_i64(tmp, t0, t1);
803 tcg_gen_andc_i64(flag, flag, tmp);
804 tcg_temp_free_i64(tmp);
805 tcg_gen_extrh_i64_i32(cpu_VF, flag);
807 tcg_gen_mov_i64(dest, result);
808 tcg_temp_free_i64(result);
809 tcg_temp_free_i64(flag);
810 } else {
811 /* 32 bit arithmetic */
812 TCGv_i32 t0_32 = tcg_temp_new_i32();
813 TCGv_i32 t1_32 = tcg_temp_new_i32();
814 TCGv_i32 tmp = tcg_temp_new_i32();
816 tcg_gen_movi_i32(tmp, 0);
817 tcg_gen_extrl_i64_i32(t0_32, t0);
818 tcg_gen_extrl_i64_i32(t1_32, t1);
819 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
820 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
821 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
822 tcg_gen_xor_i32(tmp, t0_32, t1_32);
823 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
824 tcg_gen_extu_i32_i64(dest, cpu_NF);
826 tcg_temp_free_i32(tmp);
827 tcg_temp_free_i32(t0_32);
828 tcg_temp_free_i32(t1_32);
832 /* dest = T0 - T1; compute C, N, V and Z flags */
833 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
835 if (sf) {
836 /* 64 bit arithmetic */
837 TCGv_i64 result, flag, tmp;
839 result = tcg_temp_new_i64();
840 flag = tcg_temp_new_i64();
841 tcg_gen_sub_i64(result, t0, t1);
843 gen_set_NZ64(result);
845 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
846 tcg_gen_extrl_i64_i32(cpu_CF, flag);
848 tcg_gen_xor_i64(flag, result, t0);
849 tmp = tcg_temp_new_i64();
850 tcg_gen_xor_i64(tmp, t0, t1);
851 tcg_gen_and_i64(flag, flag, tmp);
852 tcg_temp_free_i64(tmp);
853 tcg_gen_extrh_i64_i32(cpu_VF, flag);
854 tcg_gen_mov_i64(dest, result);
855 tcg_temp_free_i64(flag);
856 tcg_temp_free_i64(result);
857 } else {
858 /* 32 bit arithmetic */
859 TCGv_i32 t0_32 = tcg_temp_new_i32();
860 TCGv_i32 t1_32 = tcg_temp_new_i32();
861 TCGv_i32 tmp;
863 tcg_gen_extrl_i64_i32(t0_32, t0);
864 tcg_gen_extrl_i64_i32(t1_32, t1);
865 tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
866 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
867 tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
868 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
869 tmp = tcg_temp_new_i32();
870 tcg_gen_xor_i32(tmp, t0_32, t1_32);
871 tcg_temp_free_i32(t0_32);
872 tcg_temp_free_i32(t1_32);
873 tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
874 tcg_temp_free_i32(tmp);
875 tcg_gen_extu_i32_i64(dest, cpu_NF);
879 /* dest = T0 + T1 + CF; do not compute flags. */
880 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
882 TCGv_i64 flag = tcg_temp_new_i64();
883 tcg_gen_extu_i32_i64(flag, cpu_CF);
884 tcg_gen_add_i64(dest, t0, t1);
885 tcg_gen_add_i64(dest, dest, flag);
886 tcg_temp_free_i64(flag);
888 if (!sf) {
889 tcg_gen_ext32u_i64(dest, dest);
893 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
894 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
896 if (sf) {
897 TCGv_i64 result, cf_64, vf_64, tmp;
898 result = tcg_temp_new_i64();
899 cf_64 = tcg_temp_new_i64();
900 vf_64 = tcg_temp_new_i64();
901 tmp = tcg_const_i64(0);
903 tcg_gen_extu_i32_i64(cf_64, cpu_CF);
904 tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
905 tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
906 tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
907 gen_set_NZ64(result);
909 tcg_gen_xor_i64(vf_64, result, t0);
910 tcg_gen_xor_i64(tmp, t0, t1);
911 tcg_gen_andc_i64(vf_64, vf_64, tmp);
912 tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
914 tcg_gen_mov_i64(dest, result);
916 tcg_temp_free_i64(tmp);
917 tcg_temp_free_i64(vf_64);
918 tcg_temp_free_i64(cf_64);
919 tcg_temp_free_i64(result);
920 } else {
921 TCGv_i32 t0_32, t1_32, tmp;
922 t0_32 = tcg_temp_new_i32();
923 t1_32 = tcg_temp_new_i32();
924 tmp = tcg_const_i32(0);
926 tcg_gen_extrl_i64_i32(t0_32, t0);
927 tcg_gen_extrl_i64_i32(t1_32, t1);
928 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
929 tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
931 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
932 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
933 tcg_gen_xor_i32(tmp, t0_32, t1_32);
934 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
935 tcg_gen_extu_i32_i64(dest, cpu_NF);
937 tcg_temp_free_i32(tmp);
938 tcg_temp_free_i32(t1_32);
939 tcg_temp_free_i32(t0_32);
944 * Load/Store generators
948 * Store from GPR register to memory.
950 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
951 TCGv_i64 tcg_addr, int size, int memidx,
952 bool iss_valid,
953 unsigned int iss_srt,
954 bool iss_sf, bool iss_ar)
956 g_assert(size <= 3);
957 tcg_gen_qemu_st_i64(source, tcg_addr, memidx, s->be_data + size);
959 if (iss_valid) {
960 uint32_t syn;
962 syn = syn_data_abort_with_iss(0,
963 size,
964 false,
965 iss_srt,
966 iss_sf,
967 iss_ar,
968 0, 0, 0, 0, 0, false);
969 disas_set_insn_syndrome(s, syn);
973 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
974 TCGv_i64 tcg_addr, int size,
975 bool iss_valid,
976 unsigned int iss_srt,
977 bool iss_sf, bool iss_ar)
979 do_gpr_st_memidx(s, source, tcg_addr, size, get_mem_index(s),
980 iss_valid, iss_srt, iss_sf, iss_ar);
984 * Load from memory to GPR register
986 static void do_gpr_ld_memidx(DisasContext *s,
987 TCGv_i64 dest, TCGv_i64 tcg_addr,
988 int size, bool is_signed,
989 bool extend, int memidx,
990 bool iss_valid, unsigned int iss_srt,
991 bool iss_sf, bool iss_ar)
993 TCGMemOp memop = s->be_data + size;
995 g_assert(size <= 3);
997 if (is_signed) {
998 memop += MO_SIGN;
1001 tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
1003 if (extend && is_signed) {
1004 g_assert(size < 3);
1005 tcg_gen_ext32u_i64(dest, dest);
1008 if (iss_valid) {
1009 uint32_t syn;
1011 syn = syn_data_abort_with_iss(0,
1012 size,
1013 is_signed,
1014 iss_srt,
1015 iss_sf,
1016 iss_ar,
1017 0, 0, 0, 0, 0, false);
1018 disas_set_insn_syndrome(s, syn);
1022 static void do_gpr_ld(DisasContext *s,
1023 TCGv_i64 dest, TCGv_i64 tcg_addr,
1024 int size, bool is_signed, bool extend,
1025 bool iss_valid, unsigned int iss_srt,
1026 bool iss_sf, bool iss_ar)
1028 do_gpr_ld_memidx(s, dest, tcg_addr, size, is_signed, extend,
1029 get_mem_index(s),
1030 iss_valid, iss_srt, iss_sf, iss_ar);
1034 * Store from FP register to memory
1036 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
1038 /* This writes the bottom N bits of a 128 bit wide vector to memory */
1039 TCGv_i64 tmp = tcg_temp_new_i64();
1040 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_offset(s, srcidx, MO_64));
1041 if (size < 4) {
1042 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s),
1043 s->be_data + size);
1044 } else {
1045 bool be = s->be_data == MO_BE;
1046 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
1048 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
1049 tcg_gen_qemu_st_i64(tmp, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
1050 s->be_data | MO_Q);
1051 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_hi_offset(s, srcidx));
1052 tcg_gen_qemu_st_i64(tmp, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
1053 s->be_data | MO_Q);
1054 tcg_temp_free_i64(tcg_hiaddr);
1057 tcg_temp_free_i64(tmp);
1061 * Load from memory to FP register
1063 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
1065 /* This always zero-extends and writes to a full 128 bit wide vector */
1066 TCGv_i64 tmplo = tcg_temp_new_i64();
1067 TCGv_i64 tmphi;
1069 if (size < 4) {
1070 TCGMemOp memop = s->be_data + size;
1071 tmphi = tcg_const_i64(0);
1072 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
1073 } else {
1074 bool be = s->be_data == MO_BE;
1075 TCGv_i64 tcg_hiaddr;
1077 tmphi = tcg_temp_new_i64();
1078 tcg_hiaddr = tcg_temp_new_i64();
1080 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
1081 tcg_gen_qemu_ld_i64(tmplo, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
1082 s->be_data | MO_Q);
1083 tcg_gen_qemu_ld_i64(tmphi, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
1084 s->be_data | MO_Q);
1085 tcg_temp_free_i64(tcg_hiaddr);
1088 tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
1089 tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
1091 tcg_temp_free_i64(tmplo);
1092 tcg_temp_free_i64(tmphi);
1094 clear_vec_high(s, true, destidx);
1098 * Vector load/store helpers.
1100 * The principal difference between this and a FP load is that we don't
1101 * zero extend as we are filling a partial chunk of the vector register.
1102 * These functions don't support 128 bit loads/stores, which would be
1103 * normal load/store operations.
1105 * The _i32 versions are useful when operating on 32 bit quantities
1106 * (eg for floating point single or using Neon helper functions).
1109 /* Get value of an element within a vector register */
1110 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
1111 int element, TCGMemOp memop)
1113 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1114 switch (memop) {
1115 case MO_8:
1116 tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
1117 break;
1118 case MO_16:
1119 tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
1120 break;
1121 case MO_32:
1122 tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
1123 break;
1124 case MO_8|MO_SIGN:
1125 tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
1126 break;
1127 case MO_16|MO_SIGN:
1128 tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
1129 break;
1130 case MO_32|MO_SIGN:
1131 tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
1132 break;
1133 case MO_64:
1134 case MO_64|MO_SIGN:
1135 tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
1136 break;
1137 default:
1138 g_assert_not_reached();
1142 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1143 int element, TCGMemOp memop)
1145 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1146 switch (memop) {
1147 case MO_8:
1148 tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1149 break;
1150 case MO_16:
1151 tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1152 break;
1153 case MO_8|MO_SIGN:
1154 tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1155 break;
1156 case MO_16|MO_SIGN:
1157 tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1158 break;
1159 case MO_32:
1160 case MO_32|MO_SIGN:
1161 tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1162 break;
1163 default:
1164 g_assert_not_reached();
1168 /* Set value of an element within a vector register */
1169 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1170 int element, TCGMemOp memop)
1172 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1173 switch (memop) {
1174 case MO_8:
1175 tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1176 break;
1177 case MO_16:
1178 tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1179 break;
1180 case MO_32:
1181 tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1182 break;
1183 case MO_64:
1184 tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1185 break;
1186 default:
1187 g_assert_not_reached();
1191 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1192 int destidx, int element, TCGMemOp memop)
1194 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1195 switch (memop) {
1196 case MO_8:
1197 tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1198 break;
1199 case MO_16:
1200 tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1201 break;
1202 case MO_32:
1203 tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1204 break;
1205 default:
1206 g_assert_not_reached();
1210 /* Store from vector register to memory */
1211 static void do_vec_st(DisasContext *s, int srcidx, int element,
1212 TCGv_i64 tcg_addr, int size, TCGMemOp endian)
1214 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1216 read_vec_element(s, tcg_tmp, srcidx, element, size);
1217 tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), endian | size);
1219 tcg_temp_free_i64(tcg_tmp);
1222 /* Load from memory to vector register */
1223 static void do_vec_ld(DisasContext *s, int destidx, int element,
1224 TCGv_i64 tcg_addr, int size, TCGMemOp endian)
1226 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1228 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), endian | size);
1229 write_vec_element(s, tcg_tmp, destidx, element, size);
1231 tcg_temp_free_i64(tcg_tmp);
1234 /* Check that FP/Neon access is enabled. If it is, return
1235 * true. If not, emit code to generate an appropriate exception,
1236 * and return false; the caller should not emit any code for
1237 * the instruction. Note that this check must happen after all
1238 * unallocated-encoding checks (otherwise the syndrome information
1239 * for the resulting exception will be incorrect).
1241 static inline bool fp_access_check(DisasContext *s)
1243 assert(!s->fp_access_checked);
1244 s->fp_access_checked = true;
1246 if (!s->fp_excp_el) {
1247 return true;
1250 gen_exception_insn(s, 4, EXCP_UDEF, syn_fp_access_trap(1, 0xe, false),
1251 s->fp_excp_el);
1252 return false;
1255 /* Check that SVE access is enabled. If it is, return true.
1256 * If not, emit code to generate an appropriate exception and return false.
1258 bool sve_access_check(DisasContext *s)
1260 if (s->sve_excp_el) {
1261 gen_exception_insn(s, 4, EXCP_UDEF, syn_sve_access_trap(),
1262 s->sve_excp_el);
1263 return false;
1265 return fp_access_check(s);
1269 * This utility function is for doing register extension with an
1270 * optional shift. You will likely want to pass a temporary for the
1271 * destination register. See DecodeRegExtend() in the ARM ARM.
1273 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1274 int option, unsigned int shift)
1276 int extsize = extract32(option, 0, 2);
1277 bool is_signed = extract32(option, 2, 1);
1279 if (is_signed) {
1280 switch (extsize) {
1281 case 0:
1282 tcg_gen_ext8s_i64(tcg_out, tcg_in);
1283 break;
1284 case 1:
1285 tcg_gen_ext16s_i64(tcg_out, tcg_in);
1286 break;
1287 case 2:
1288 tcg_gen_ext32s_i64(tcg_out, tcg_in);
1289 break;
1290 case 3:
1291 tcg_gen_mov_i64(tcg_out, tcg_in);
1292 break;
1294 } else {
1295 switch (extsize) {
1296 case 0:
1297 tcg_gen_ext8u_i64(tcg_out, tcg_in);
1298 break;
1299 case 1:
1300 tcg_gen_ext16u_i64(tcg_out, tcg_in);
1301 break;
1302 case 2:
1303 tcg_gen_ext32u_i64(tcg_out, tcg_in);
1304 break;
1305 case 3:
1306 tcg_gen_mov_i64(tcg_out, tcg_in);
1307 break;
1311 if (shift) {
1312 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1316 static inline void gen_check_sp_alignment(DisasContext *s)
1318 /* The AArch64 architecture mandates that (if enabled via PSTATE
1319 * or SCTLR bits) there is a check that SP is 16-aligned on every
1320 * SP-relative load or store (with an exception generated if it is not).
1321 * In line with general QEMU practice regarding misaligned accesses,
1322 * we omit these checks for the sake of guest program performance.
1323 * This function is provided as a hook so we can more easily add these
1324 * checks in future (possibly as a "favour catching guest program bugs
1325 * over speed" user selectable option).
1330 * This provides a simple table based table lookup decoder. It is
1331 * intended to be used when the relevant bits for decode are too
1332 * awkwardly placed and switch/if based logic would be confusing and
1333 * deeply nested. Since it's a linear search through the table, tables
1334 * should be kept small.
1336 * It returns the first handler where insn & mask == pattern, or
1337 * NULL if there is no match.
1338 * The table is terminated by an empty mask (i.e. 0)
1340 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1341 uint32_t insn)
1343 const AArch64DecodeTable *tptr = table;
1345 while (tptr->mask) {
1346 if ((insn & tptr->mask) == tptr->pattern) {
1347 return tptr->disas_fn;
1349 tptr++;
1351 return NULL;
1355 * The instruction disassembly implemented here matches
1356 * the instruction encoding classifications in chapter C4
1357 * of the ARM Architecture Reference Manual (DDI0487B_a);
1358 * classification names and decode diagrams here should generally
1359 * match up with those in the manual.
1362 /* Unconditional branch (immediate)
1363 * 31 30 26 25 0
1364 * +----+-----------+-------------------------------------+
1365 * | op | 0 0 1 0 1 | imm26 |
1366 * +----+-----------+-------------------------------------+
1368 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
1370 uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
1372 if (insn & (1U << 31)) {
1373 /* BL Branch with link */
1374 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1377 /* B Branch / BL Branch with link */
1378 reset_btype(s);
1379 gen_goto_tb(s, 0, addr);
1382 /* Compare and branch (immediate)
1383 * 31 30 25 24 23 5 4 0
1384 * +----+-------------+----+---------------------+--------+
1385 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
1386 * +----+-------------+----+---------------------+--------+
1388 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
1390 unsigned int sf, op, rt;
1391 uint64_t addr;
1392 TCGLabel *label_match;
1393 TCGv_i64 tcg_cmp;
1395 sf = extract32(insn, 31, 1);
1396 op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
1397 rt = extract32(insn, 0, 5);
1398 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1400 tcg_cmp = read_cpu_reg(s, rt, sf);
1401 label_match = gen_new_label();
1403 reset_btype(s);
1404 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1405 tcg_cmp, 0, label_match);
1407 gen_goto_tb(s, 0, s->pc);
1408 gen_set_label(label_match);
1409 gen_goto_tb(s, 1, addr);
1412 /* Test and branch (immediate)
1413 * 31 30 25 24 23 19 18 5 4 0
1414 * +----+-------------+----+-------+-------------+------+
1415 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
1416 * +----+-------------+----+-------+-------------+------+
1418 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1420 unsigned int bit_pos, op, rt;
1421 uint64_t addr;
1422 TCGLabel *label_match;
1423 TCGv_i64 tcg_cmp;
1425 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1426 op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1427 addr = s->pc + sextract32(insn, 5, 14) * 4 - 4;
1428 rt = extract32(insn, 0, 5);
1430 tcg_cmp = tcg_temp_new_i64();
1431 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1432 label_match = gen_new_label();
1434 reset_btype(s);
1435 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1436 tcg_cmp, 0, label_match);
1437 tcg_temp_free_i64(tcg_cmp);
1438 gen_goto_tb(s, 0, s->pc);
1439 gen_set_label(label_match);
1440 gen_goto_tb(s, 1, addr);
1443 /* Conditional branch (immediate)
1444 * 31 25 24 23 5 4 3 0
1445 * +---------------+----+---------------------+----+------+
1446 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
1447 * +---------------+----+---------------------+----+------+
1449 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1451 unsigned int cond;
1452 uint64_t addr;
1454 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1455 unallocated_encoding(s);
1456 return;
1458 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1459 cond = extract32(insn, 0, 4);
1461 reset_btype(s);
1462 if (cond < 0x0e) {
1463 /* genuinely conditional branches */
1464 TCGLabel *label_match = gen_new_label();
1465 arm_gen_test_cc(cond, label_match);
1466 gen_goto_tb(s, 0, s->pc);
1467 gen_set_label(label_match);
1468 gen_goto_tb(s, 1, addr);
1469 } else {
1470 /* 0xe and 0xf are both "always" conditions */
1471 gen_goto_tb(s, 0, addr);
1475 /* HINT instruction group, including various allocated HINTs */
1476 static void handle_hint(DisasContext *s, uint32_t insn,
1477 unsigned int op1, unsigned int op2, unsigned int crm)
1479 unsigned int selector = crm << 3 | op2;
1481 if (op1 != 3) {
1482 unallocated_encoding(s);
1483 return;
1486 switch (selector) {
1487 case 0b00000: /* NOP */
1488 break;
1489 case 0b00011: /* WFI */
1490 s->base.is_jmp = DISAS_WFI;
1491 break;
1492 case 0b00001: /* YIELD */
1493 /* When running in MTTCG we don't generate jumps to the yield and
1494 * WFE helpers as it won't affect the scheduling of other vCPUs.
1495 * If we wanted to more completely model WFE/SEV so we don't busy
1496 * spin unnecessarily we would need to do something more involved.
1498 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1499 s->base.is_jmp = DISAS_YIELD;
1501 break;
1502 case 0b00010: /* WFE */
1503 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1504 s->base.is_jmp = DISAS_WFE;
1506 break;
1507 case 0b00100: /* SEV */
1508 case 0b00101: /* SEVL */
1509 /* we treat all as NOP at least for now */
1510 break;
1511 case 0b00111: /* XPACLRI */
1512 if (s->pauth_active) {
1513 gen_helper_xpaci(cpu_X[30], cpu_env, cpu_X[30]);
1515 break;
1516 case 0b01000: /* PACIA1716 */
1517 if (s->pauth_active) {
1518 gen_helper_pacia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1520 break;
1521 case 0b01010: /* PACIB1716 */
1522 if (s->pauth_active) {
1523 gen_helper_pacib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1525 break;
1526 case 0b01100: /* AUTIA1716 */
1527 if (s->pauth_active) {
1528 gen_helper_autia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1530 break;
1531 case 0b01110: /* AUTIB1716 */
1532 if (s->pauth_active) {
1533 gen_helper_autib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1535 break;
1536 case 0b11000: /* PACIAZ */
1537 if (s->pauth_active) {
1538 gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30],
1539 new_tmp_a64_zero(s));
1541 break;
1542 case 0b11001: /* PACIASP */
1543 if (s->pauth_active) {
1544 gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1546 break;
1547 case 0b11010: /* PACIBZ */
1548 if (s->pauth_active) {
1549 gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30],
1550 new_tmp_a64_zero(s));
1552 break;
1553 case 0b11011: /* PACIBSP */
1554 if (s->pauth_active) {
1555 gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1557 break;
1558 case 0b11100: /* AUTIAZ */
1559 if (s->pauth_active) {
1560 gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30],
1561 new_tmp_a64_zero(s));
1563 break;
1564 case 0b11101: /* AUTIASP */
1565 if (s->pauth_active) {
1566 gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1568 break;
1569 case 0b11110: /* AUTIBZ */
1570 if (s->pauth_active) {
1571 gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30],
1572 new_tmp_a64_zero(s));
1574 break;
1575 case 0b11111: /* AUTIBSP */
1576 if (s->pauth_active) {
1577 gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1579 break;
1580 default:
1581 /* default specified as NOP equivalent */
1582 break;
1586 static void gen_clrex(DisasContext *s, uint32_t insn)
1588 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1591 /* CLREX, DSB, DMB, ISB */
1592 static void handle_sync(DisasContext *s, uint32_t insn,
1593 unsigned int op1, unsigned int op2, unsigned int crm)
1595 TCGBar bar;
1597 if (op1 != 3) {
1598 unallocated_encoding(s);
1599 return;
1602 switch (op2) {
1603 case 2: /* CLREX */
1604 gen_clrex(s, insn);
1605 return;
1606 case 4: /* DSB */
1607 case 5: /* DMB */
1608 switch (crm & 3) {
1609 case 1: /* MBReqTypes_Reads */
1610 bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1611 break;
1612 case 2: /* MBReqTypes_Writes */
1613 bar = TCG_BAR_SC | TCG_MO_ST_ST;
1614 break;
1615 default: /* MBReqTypes_All */
1616 bar = TCG_BAR_SC | TCG_MO_ALL;
1617 break;
1619 tcg_gen_mb(bar);
1620 return;
1621 case 6: /* ISB */
1622 /* We need to break the TB after this insn to execute
1623 * a self-modified code correctly and also to take
1624 * any pending interrupts immediately.
1626 reset_btype(s);
1627 gen_goto_tb(s, 0, s->pc);
1628 return;
1630 case 7: /* SB */
1631 if (crm != 0 || !dc_isar_feature(aa64_sb, s)) {
1632 goto do_unallocated;
1635 * TODO: There is no speculation barrier opcode for TCG;
1636 * MB and end the TB instead.
1638 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1639 gen_goto_tb(s, 0, s->pc);
1640 return;
1642 default:
1643 do_unallocated:
1644 unallocated_encoding(s);
1645 return;
1649 /* MSR (immediate) - move immediate to processor state field */
1650 static void handle_msr_i(DisasContext *s, uint32_t insn,
1651 unsigned int op1, unsigned int op2, unsigned int crm)
1653 TCGv_i32 t1;
1654 int op = op1 << 3 | op2;
1656 /* End the TB by default, chaining is ok. */
1657 s->base.is_jmp = DISAS_TOO_MANY;
1659 switch (op) {
1660 case 0x00: /* CFINV */
1661 if (crm != 0 || !dc_isar_feature(aa64_condm_4, s)) {
1662 goto do_unallocated;
1664 tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1665 s->base.is_jmp = DISAS_NEXT;
1666 break;
1668 case 0x05: /* SPSel */
1669 if (s->current_el == 0) {
1670 goto do_unallocated;
1672 t1 = tcg_const_i32(crm & PSTATE_SP);
1673 gen_helper_msr_i_spsel(cpu_env, t1);
1674 tcg_temp_free_i32(t1);
1675 break;
1677 case 0x1e: /* DAIFSet */
1678 t1 = tcg_const_i32(crm);
1679 gen_helper_msr_i_daifset(cpu_env, t1);
1680 tcg_temp_free_i32(t1);
1681 break;
1683 case 0x1f: /* DAIFClear */
1684 t1 = tcg_const_i32(crm);
1685 gen_helper_msr_i_daifclear(cpu_env, t1);
1686 tcg_temp_free_i32(t1);
1687 /* For DAIFClear, exit the cpu loop to re-evaluate pending IRQs. */
1688 s->base.is_jmp = DISAS_UPDATE;
1689 break;
1691 default:
1692 do_unallocated:
1693 unallocated_encoding(s);
1694 return;
1698 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1700 TCGv_i32 tmp = tcg_temp_new_i32();
1701 TCGv_i32 nzcv = tcg_temp_new_i32();
1703 /* build bit 31, N */
1704 tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
1705 /* build bit 30, Z */
1706 tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1707 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1708 /* build bit 29, C */
1709 tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1710 /* build bit 28, V */
1711 tcg_gen_shri_i32(tmp, cpu_VF, 31);
1712 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1713 /* generate result */
1714 tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1716 tcg_temp_free_i32(nzcv);
1717 tcg_temp_free_i32(tmp);
1720 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1722 TCGv_i32 nzcv = tcg_temp_new_i32();
1724 /* take NZCV from R[t] */
1725 tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
1727 /* bit 31, N */
1728 tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
1729 /* bit 30, Z */
1730 tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1731 tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1732 /* bit 29, C */
1733 tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1734 tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1735 /* bit 28, V */
1736 tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1737 tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1738 tcg_temp_free_i32(nzcv);
1741 /* MRS - move from system register
1742 * MSR (register) - move to system register
1743 * SYS
1744 * SYSL
1745 * These are all essentially the same insn in 'read' and 'write'
1746 * versions, with varying op0 fields.
1748 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1749 unsigned int op0, unsigned int op1, unsigned int op2,
1750 unsigned int crn, unsigned int crm, unsigned int rt)
1752 const ARMCPRegInfo *ri;
1753 TCGv_i64 tcg_rt;
1755 ri = get_arm_cp_reginfo(s->cp_regs,
1756 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1757 crn, crm, op0, op1, op2));
1759 if (!ri) {
1760 /* Unknown register; this might be a guest error or a QEMU
1761 * unimplemented feature.
1763 qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1764 "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1765 isread ? "read" : "write", op0, op1, crn, crm, op2);
1766 unallocated_encoding(s);
1767 return;
1770 /* Check access permissions */
1771 if (!cp_access_ok(s->current_el, ri, isread)) {
1772 unallocated_encoding(s);
1773 return;
1776 if (ri->accessfn) {
1777 /* Emit code to perform further access permissions checks at
1778 * runtime; this may result in an exception.
1780 TCGv_ptr tmpptr;
1781 TCGv_i32 tcg_syn, tcg_isread;
1782 uint32_t syndrome;
1784 gen_a64_set_pc_im(s->pc - 4);
1785 tmpptr = tcg_const_ptr(ri);
1786 syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1787 tcg_syn = tcg_const_i32(syndrome);
1788 tcg_isread = tcg_const_i32(isread);
1789 gen_helper_access_check_cp_reg(cpu_env, tmpptr, tcg_syn, tcg_isread);
1790 tcg_temp_free_ptr(tmpptr);
1791 tcg_temp_free_i32(tcg_syn);
1792 tcg_temp_free_i32(tcg_isread);
1795 /* Handle special cases first */
1796 switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
1797 case ARM_CP_NOP:
1798 return;
1799 case ARM_CP_NZCV:
1800 tcg_rt = cpu_reg(s, rt);
1801 if (isread) {
1802 gen_get_nzcv(tcg_rt);
1803 } else {
1804 gen_set_nzcv(tcg_rt);
1806 return;
1807 case ARM_CP_CURRENTEL:
1808 /* Reads as current EL value from pstate, which is
1809 * guaranteed to be constant by the tb flags.
1811 tcg_rt = cpu_reg(s, rt);
1812 tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
1813 return;
1814 case ARM_CP_DC_ZVA:
1815 /* Writes clear the aligned block of memory which rt points into. */
1816 tcg_rt = cpu_reg(s, rt);
1817 gen_helper_dc_zva(cpu_env, tcg_rt);
1818 return;
1819 default:
1820 break;
1822 if ((ri->type & ARM_CP_FPU) && !fp_access_check(s)) {
1823 return;
1824 } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
1825 return;
1828 if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1829 gen_io_start();
1832 tcg_rt = cpu_reg(s, rt);
1834 if (isread) {
1835 if (ri->type & ARM_CP_CONST) {
1836 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1837 } else if (ri->readfn) {
1838 TCGv_ptr tmpptr;
1839 tmpptr = tcg_const_ptr(ri);
1840 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1841 tcg_temp_free_ptr(tmpptr);
1842 } else {
1843 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1845 } else {
1846 if (ri->type & ARM_CP_CONST) {
1847 /* If not forbidden by access permissions, treat as WI */
1848 return;
1849 } else if (ri->writefn) {
1850 TCGv_ptr tmpptr;
1851 tmpptr = tcg_const_ptr(ri);
1852 gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1853 tcg_temp_free_ptr(tmpptr);
1854 } else {
1855 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
1859 if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1860 /* I/O operations must end the TB here (whether read or write) */
1861 gen_io_end();
1862 s->base.is_jmp = DISAS_UPDATE;
1863 } else if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
1864 /* We default to ending the TB on a coprocessor register write,
1865 * but allow this to be suppressed by the register definition
1866 * (usually only necessary to work around guest bugs).
1868 s->base.is_jmp = DISAS_UPDATE;
1872 /* System
1873 * 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0
1874 * +---------------------+---+-----+-----+-------+-------+-----+------+
1875 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 | CRn | CRm | op2 | Rt |
1876 * +---------------------+---+-----+-----+-------+-------+-----+------+
1878 static void disas_system(DisasContext *s, uint32_t insn)
1880 unsigned int l, op0, op1, crn, crm, op2, rt;
1881 l = extract32(insn, 21, 1);
1882 op0 = extract32(insn, 19, 2);
1883 op1 = extract32(insn, 16, 3);
1884 crn = extract32(insn, 12, 4);
1885 crm = extract32(insn, 8, 4);
1886 op2 = extract32(insn, 5, 3);
1887 rt = extract32(insn, 0, 5);
1889 if (op0 == 0) {
1890 if (l || rt != 31) {
1891 unallocated_encoding(s);
1892 return;
1894 switch (crn) {
1895 case 2: /* HINT (including allocated hints like NOP, YIELD, etc) */
1896 handle_hint(s, insn, op1, op2, crm);
1897 break;
1898 case 3: /* CLREX, DSB, DMB, ISB */
1899 handle_sync(s, insn, op1, op2, crm);
1900 break;
1901 case 4: /* MSR (immediate) */
1902 handle_msr_i(s, insn, op1, op2, crm);
1903 break;
1904 default:
1905 unallocated_encoding(s);
1906 break;
1908 return;
1910 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
1913 /* Exception generation
1915 * 31 24 23 21 20 5 4 2 1 0
1916 * +-----------------+-----+------------------------+-----+----+
1917 * | 1 1 0 1 0 1 0 0 | opc | imm16 | op2 | LL |
1918 * +-----------------------+------------------------+----------+
1920 static void disas_exc(DisasContext *s, uint32_t insn)
1922 int opc = extract32(insn, 21, 3);
1923 int op2_ll = extract32(insn, 0, 5);
1924 int imm16 = extract32(insn, 5, 16);
1925 TCGv_i32 tmp;
1927 switch (opc) {
1928 case 0:
1929 /* For SVC, HVC and SMC we advance the single-step state
1930 * machine before taking the exception. This is architecturally
1931 * mandated, to ensure that single-stepping a system call
1932 * instruction works properly.
1934 switch (op2_ll) {
1935 case 1: /* SVC */
1936 gen_ss_advance(s);
1937 gen_exception_insn(s, 0, EXCP_SWI, syn_aa64_svc(imm16),
1938 default_exception_el(s));
1939 break;
1940 case 2: /* HVC */
1941 if (s->current_el == 0) {
1942 unallocated_encoding(s);
1943 break;
1945 /* The pre HVC helper handles cases when HVC gets trapped
1946 * as an undefined insn by runtime configuration.
1948 gen_a64_set_pc_im(s->pc - 4);
1949 gen_helper_pre_hvc(cpu_env);
1950 gen_ss_advance(s);
1951 gen_exception_insn(s, 0, EXCP_HVC, syn_aa64_hvc(imm16), 2);
1952 break;
1953 case 3: /* SMC */
1954 if (s->current_el == 0) {
1955 unallocated_encoding(s);
1956 break;
1958 gen_a64_set_pc_im(s->pc - 4);
1959 tmp = tcg_const_i32(syn_aa64_smc(imm16));
1960 gen_helper_pre_smc(cpu_env, tmp);
1961 tcg_temp_free_i32(tmp);
1962 gen_ss_advance(s);
1963 gen_exception_insn(s, 0, EXCP_SMC, syn_aa64_smc(imm16), 3);
1964 break;
1965 default:
1966 unallocated_encoding(s);
1967 break;
1969 break;
1970 case 1:
1971 if (op2_ll != 0) {
1972 unallocated_encoding(s);
1973 break;
1975 /* BRK */
1976 gen_exception_bkpt_insn(s, 4, syn_aa64_bkpt(imm16));
1977 break;
1978 case 2:
1979 if (op2_ll != 0) {
1980 unallocated_encoding(s);
1981 break;
1983 /* HLT. This has two purposes.
1984 * Architecturally, it is an external halting debug instruction.
1985 * Since QEMU doesn't implement external debug, we treat this as
1986 * it is required for halting debug disabled: it will UNDEF.
1987 * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
1989 if (semihosting_enabled() && imm16 == 0xf000) {
1990 #ifndef CONFIG_USER_ONLY
1991 /* In system mode, don't allow userspace access to semihosting,
1992 * to provide some semblance of security (and for consistency
1993 * with our 32-bit semihosting).
1995 if (s->current_el == 0) {
1996 unsupported_encoding(s, insn);
1997 break;
1999 #endif
2000 gen_exception_internal_insn(s, 0, EXCP_SEMIHOST);
2001 } else {
2002 unsupported_encoding(s, insn);
2004 break;
2005 case 5:
2006 if (op2_ll < 1 || op2_ll > 3) {
2007 unallocated_encoding(s);
2008 break;
2010 /* DCPS1, DCPS2, DCPS3 */
2011 unsupported_encoding(s, insn);
2012 break;
2013 default:
2014 unallocated_encoding(s);
2015 break;
2019 /* Unconditional branch (register)
2020 * 31 25 24 21 20 16 15 10 9 5 4 0
2021 * +---------------+-------+-------+-------+------+-------+
2022 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
2023 * +---------------+-------+-------+-------+------+-------+
2025 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
2027 unsigned int opc, op2, op3, rn, op4;
2028 unsigned btype_mod = 2; /* 0: BR, 1: BLR, 2: other */
2029 TCGv_i64 dst;
2030 TCGv_i64 modifier;
2032 opc = extract32(insn, 21, 4);
2033 op2 = extract32(insn, 16, 5);
2034 op3 = extract32(insn, 10, 6);
2035 rn = extract32(insn, 5, 5);
2036 op4 = extract32(insn, 0, 5);
2038 if (op2 != 0x1f) {
2039 goto do_unallocated;
2042 switch (opc) {
2043 case 0: /* BR */
2044 case 1: /* BLR */
2045 case 2: /* RET */
2046 btype_mod = opc;
2047 switch (op3) {
2048 case 0:
2049 /* BR, BLR, RET */
2050 if (op4 != 0) {
2051 goto do_unallocated;
2053 dst = cpu_reg(s, rn);
2054 break;
2056 case 2:
2057 case 3:
2058 if (!dc_isar_feature(aa64_pauth, s)) {
2059 goto do_unallocated;
2061 if (opc == 2) {
2062 /* RETAA, RETAB */
2063 if (rn != 0x1f || op4 != 0x1f) {
2064 goto do_unallocated;
2066 rn = 30;
2067 modifier = cpu_X[31];
2068 } else {
2069 /* BRAAZ, BRABZ, BLRAAZ, BLRABZ */
2070 if (op4 != 0x1f) {
2071 goto do_unallocated;
2073 modifier = new_tmp_a64_zero(s);
2075 if (s->pauth_active) {
2076 dst = new_tmp_a64(s);
2077 if (op3 == 2) {
2078 gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
2079 } else {
2080 gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2082 } else {
2083 dst = cpu_reg(s, rn);
2085 break;
2087 default:
2088 goto do_unallocated;
2090 gen_a64_set_pc(s, dst);
2091 /* BLR also needs to load return address */
2092 if (opc == 1) {
2093 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
2095 break;
2097 case 8: /* BRAA */
2098 case 9: /* BLRAA */
2099 if (!dc_isar_feature(aa64_pauth, s)) {
2100 goto do_unallocated;
2102 if ((op3 & ~1) != 2) {
2103 goto do_unallocated;
2105 btype_mod = opc & 1;
2106 if (s->pauth_active) {
2107 dst = new_tmp_a64(s);
2108 modifier = cpu_reg_sp(s, op4);
2109 if (op3 == 2) {
2110 gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
2111 } else {
2112 gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2114 } else {
2115 dst = cpu_reg(s, rn);
2117 gen_a64_set_pc(s, dst);
2118 /* BLRAA also needs to load return address */
2119 if (opc == 9) {
2120 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
2122 break;
2124 case 4: /* ERET */
2125 if (s->current_el == 0) {
2126 goto do_unallocated;
2128 switch (op3) {
2129 case 0: /* ERET */
2130 if (op4 != 0) {
2131 goto do_unallocated;
2133 dst = tcg_temp_new_i64();
2134 tcg_gen_ld_i64(dst, cpu_env,
2135 offsetof(CPUARMState, elr_el[s->current_el]));
2136 break;
2138 case 2: /* ERETAA */
2139 case 3: /* ERETAB */
2140 if (!dc_isar_feature(aa64_pauth, s)) {
2141 goto do_unallocated;
2143 if (rn != 0x1f || op4 != 0x1f) {
2144 goto do_unallocated;
2146 dst = tcg_temp_new_i64();
2147 tcg_gen_ld_i64(dst, cpu_env,
2148 offsetof(CPUARMState, elr_el[s->current_el]));
2149 if (s->pauth_active) {
2150 modifier = cpu_X[31];
2151 if (op3 == 2) {
2152 gen_helper_autia(dst, cpu_env, dst, modifier);
2153 } else {
2154 gen_helper_autib(dst, cpu_env, dst, modifier);
2157 break;
2159 default:
2160 goto do_unallocated;
2162 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2163 gen_io_start();
2166 gen_helper_exception_return(cpu_env, dst);
2167 tcg_temp_free_i64(dst);
2168 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2169 gen_io_end();
2171 /* Must exit loop to check un-masked IRQs */
2172 s->base.is_jmp = DISAS_EXIT;
2173 return;
2175 case 5: /* DRPS */
2176 if (op3 != 0 || op4 != 0 || rn != 0x1f) {
2177 goto do_unallocated;
2178 } else {
2179 unsupported_encoding(s, insn);
2181 return;
2183 default:
2184 do_unallocated:
2185 unallocated_encoding(s);
2186 return;
2189 switch (btype_mod) {
2190 case 0: /* BR */
2191 if (dc_isar_feature(aa64_bti, s)) {
2192 /* BR to {x16,x17} or !guard -> 1, else 3. */
2193 set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
2195 break;
2197 case 1: /* BLR */
2198 if (dc_isar_feature(aa64_bti, s)) {
2199 /* BLR sets BTYPE to 2, regardless of source guarded page. */
2200 set_btype(s, 2);
2202 break;
2204 default: /* RET or none of the above. */
2205 /* BTYPE will be set to 0 by normal end-of-insn processing. */
2206 break;
2209 s->base.is_jmp = DISAS_JUMP;
2212 /* Branches, exception generating and system instructions */
2213 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
2215 switch (extract32(insn, 25, 7)) {
2216 case 0x0a: case 0x0b:
2217 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
2218 disas_uncond_b_imm(s, insn);
2219 break;
2220 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
2221 disas_comp_b_imm(s, insn);
2222 break;
2223 case 0x1b: case 0x5b: /* Test & branch (immediate) */
2224 disas_test_b_imm(s, insn);
2225 break;
2226 case 0x2a: /* Conditional branch (immediate) */
2227 disas_cond_b_imm(s, insn);
2228 break;
2229 case 0x6a: /* Exception generation / System */
2230 if (insn & (1 << 24)) {
2231 if (extract32(insn, 22, 2) == 0) {
2232 disas_system(s, insn);
2233 } else {
2234 unallocated_encoding(s);
2236 } else {
2237 disas_exc(s, insn);
2239 break;
2240 case 0x6b: /* Unconditional branch (register) */
2241 disas_uncond_b_reg(s, insn);
2242 break;
2243 default:
2244 unallocated_encoding(s);
2245 break;
2250 * Load/Store exclusive instructions are implemented by remembering
2251 * the value/address loaded, and seeing if these are the same
2252 * when the store is performed. This is not actually the architecturally
2253 * mandated semantics, but it works for typical guest code sequences
2254 * and avoids having to monitor regular stores.
2256 * The store exclusive uses the atomic cmpxchg primitives to avoid
2257 * races in multi-threaded linux-user and when MTTCG softmmu is
2258 * enabled.
2260 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
2261 TCGv_i64 addr, int size, bool is_pair)
2263 int idx = get_mem_index(s);
2264 TCGMemOp memop = s->be_data;
2266 g_assert(size <= 3);
2267 if (is_pair) {
2268 g_assert(size >= 2);
2269 if (size == 2) {
2270 /* The pair must be single-copy atomic for the doubleword. */
2271 memop |= MO_64 | MO_ALIGN;
2272 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2273 if (s->be_data == MO_LE) {
2274 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2275 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2276 } else {
2277 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2278 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2280 } else {
2281 /* The pair must be single-copy atomic for *each* doubleword, not
2282 the entire quadword, however it must be quadword aligned. */
2283 memop |= MO_64;
2284 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx,
2285 memop | MO_ALIGN_16);
2287 TCGv_i64 addr2 = tcg_temp_new_i64();
2288 tcg_gen_addi_i64(addr2, addr, 8);
2289 tcg_gen_qemu_ld_i64(cpu_exclusive_high, addr2, idx, memop);
2290 tcg_temp_free_i64(addr2);
2292 tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2293 tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2295 } else {
2296 memop |= size | MO_ALIGN;
2297 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2298 tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2300 tcg_gen_mov_i64(cpu_exclusive_addr, addr);
2303 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2304 TCGv_i64 addr, int size, int is_pair)
2306 /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2307 * && (!is_pair || env->exclusive_high == [addr + datasize])) {
2308 * [addr] = {Rt};
2309 * if (is_pair) {
2310 * [addr + datasize] = {Rt2};
2312 * {Rd} = 0;
2313 * } else {
2314 * {Rd} = 1;
2316 * env->exclusive_addr = -1;
2318 TCGLabel *fail_label = gen_new_label();
2319 TCGLabel *done_label = gen_new_label();
2320 TCGv_i64 tmp;
2322 tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
2324 tmp = tcg_temp_new_i64();
2325 if (is_pair) {
2326 if (size == 2) {
2327 if (s->be_data == MO_LE) {
2328 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2329 } else {
2330 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2332 tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2333 cpu_exclusive_val, tmp,
2334 get_mem_index(s),
2335 MO_64 | MO_ALIGN | s->be_data);
2336 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2337 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2338 if (!HAVE_CMPXCHG128) {
2339 gen_helper_exit_atomic(cpu_env);
2340 s->base.is_jmp = DISAS_NORETURN;
2341 } else if (s->be_data == MO_LE) {
2342 gen_helper_paired_cmpxchg64_le_parallel(tmp, cpu_env,
2343 cpu_exclusive_addr,
2344 cpu_reg(s, rt),
2345 cpu_reg(s, rt2));
2346 } else {
2347 gen_helper_paired_cmpxchg64_be_parallel(tmp, cpu_env,
2348 cpu_exclusive_addr,
2349 cpu_reg(s, rt),
2350 cpu_reg(s, rt2));
2352 } else if (s->be_data == MO_LE) {
2353 gen_helper_paired_cmpxchg64_le(tmp, cpu_env, cpu_exclusive_addr,
2354 cpu_reg(s, rt), cpu_reg(s, rt2));
2355 } else {
2356 gen_helper_paired_cmpxchg64_be(tmp, cpu_env, cpu_exclusive_addr,
2357 cpu_reg(s, rt), cpu_reg(s, rt2));
2359 } else {
2360 tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2361 cpu_reg(s, rt), get_mem_index(s),
2362 size | MO_ALIGN | s->be_data);
2363 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2365 tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2366 tcg_temp_free_i64(tmp);
2367 tcg_gen_br(done_label);
2369 gen_set_label(fail_label);
2370 tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2371 gen_set_label(done_label);
2372 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2375 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2376 int rn, int size)
2378 TCGv_i64 tcg_rs = cpu_reg(s, rs);
2379 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2380 int memidx = get_mem_index(s);
2381 TCGv_i64 clean_addr;
2383 if (rn == 31) {
2384 gen_check_sp_alignment(s);
2386 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2387 tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt, memidx,
2388 size | MO_ALIGN | s->be_data);
2391 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2392 int rn, int size)
2394 TCGv_i64 s1 = cpu_reg(s, rs);
2395 TCGv_i64 s2 = cpu_reg(s, rs + 1);
2396 TCGv_i64 t1 = cpu_reg(s, rt);
2397 TCGv_i64 t2 = cpu_reg(s, rt + 1);
2398 TCGv_i64 clean_addr;
2399 int memidx = get_mem_index(s);
2401 if (rn == 31) {
2402 gen_check_sp_alignment(s);
2404 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2406 if (size == 2) {
2407 TCGv_i64 cmp = tcg_temp_new_i64();
2408 TCGv_i64 val = tcg_temp_new_i64();
2410 if (s->be_data == MO_LE) {
2411 tcg_gen_concat32_i64(val, t1, t2);
2412 tcg_gen_concat32_i64(cmp, s1, s2);
2413 } else {
2414 tcg_gen_concat32_i64(val, t2, t1);
2415 tcg_gen_concat32_i64(cmp, s2, s1);
2418 tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx,
2419 MO_64 | MO_ALIGN | s->be_data);
2420 tcg_temp_free_i64(val);
2422 if (s->be_data == MO_LE) {
2423 tcg_gen_extr32_i64(s1, s2, cmp);
2424 } else {
2425 tcg_gen_extr32_i64(s2, s1, cmp);
2427 tcg_temp_free_i64(cmp);
2428 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2429 if (HAVE_CMPXCHG128) {
2430 TCGv_i32 tcg_rs = tcg_const_i32(rs);
2431 if (s->be_data == MO_LE) {
2432 gen_helper_casp_le_parallel(cpu_env, tcg_rs,
2433 clean_addr, t1, t2);
2434 } else {
2435 gen_helper_casp_be_parallel(cpu_env, tcg_rs,
2436 clean_addr, t1, t2);
2438 tcg_temp_free_i32(tcg_rs);
2439 } else {
2440 gen_helper_exit_atomic(cpu_env);
2441 s->base.is_jmp = DISAS_NORETURN;
2443 } else {
2444 TCGv_i64 d1 = tcg_temp_new_i64();
2445 TCGv_i64 d2 = tcg_temp_new_i64();
2446 TCGv_i64 a2 = tcg_temp_new_i64();
2447 TCGv_i64 c1 = tcg_temp_new_i64();
2448 TCGv_i64 c2 = tcg_temp_new_i64();
2449 TCGv_i64 zero = tcg_const_i64(0);
2451 /* Load the two words, in memory order. */
2452 tcg_gen_qemu_ld_i64(d1, clean_addr, memidx,
2453 MO_64 | MO_ALIGN_16 | s->be_data);
2454 tcg_gen_addi_i64(a2, clean_addr, 8);
2455 tcg_gen_qemu_ld_i64(d2, clean_addr, memidx, MO_64 | s->be_data);
2457 /* Compare the two words, also in memory order. */
2458 tcg_gen_setcond_i64(TCG_COND_EQ, c1, d1, s1);
2459 tcg_gen_setcond_i64(TCG_COND_EQ, c2, d2, s2);
2460 tcg_gen_and_i64(c2, c2, c1);
2462 /* If compare equal, write back new data, else write back old data. */
2463 tcg_gen_movcond_i64(TCG_COND_NE, c1, c2, zero, t1, d1);
2464 tcg_gen_movcond_i64(TCG_COND_NE, c2, c2, zero, t2, d2);
2465 tcg_gen_qemu_st_i64(c1, clean_addr, memidx, MO_64 | s->be_data);
2466 tcg_gen_qemu_st_i64(c2, a2, memidx, MO_64 | s->be_data);
2467 tcg_temp_free_i64(a2);
2468 tcg_temp_free_i64(c1);
2469 tcg_temp_free_i64(c2);
2470 tcg_temp_free_i64(zero);
2472 /* Write back the data from memory to Rs. */
2473 tcg_gen_mov_i64(s1, d1);
2474 tcg_gen_mov_i64(s2, d2);
2475 tcg_temp_free_i64(d1);
2476 tcg_temp_free_i64(d2);
2480 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
2481 * from the ARMv8 specs for LDR (Shared decode for all encodings).
2483 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2485 int opc0 = extract32(opc, 0, 1);
2486 int regsize;
2488 if (is_signed) {
2489 regsize = opc0 ? 32 : 64;
2490 } else {
2491 regsize = size == 3 ? 64 : 32;
2493 return regsize == 64;
2496 /* Load/store exclusive
2498 * 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0
2499 * +-----+-------------+----+---+----+------+----+-------+------+------+
2500 * | sz | 0 0 1 0 0 0 | o2 | L | o1 | Rs | o0 | Rt2 | Rn | Rt |
2501 * +-----+-------------+----+---+----+------+----+-------+------+------+
2503 * sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2504 * L: 0 -> store, 1 -> load
2505 * o2: 0 -> exclusive, 1 -> not
2506 * o1: 0 -> single register, 1 -> register pair
2507 * o0: 1 -> load-acquire/store-release, 0 -> not
2509 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2511 int rt = extract32(insn, 0, 5);
2512 int rn = extract32(insn, 5, 5);
2513 int rt2 = extract32(insn, 10, 5);
2514 int rs = extract32(insn, 16, 5);
2515 int is_lasr = extract32(insn, 15, 1);
2516 int o2_L_o1_o0 = extract32(insn, 21, 3) * 2 | is_lasr;
2517 int size = extract32(insn, 30, 2);
2518 TCGv_i64 clean_addr;
2520 switch (o2_L_o1_o0) {
2521 case 0x0: /* STXR */
2522 case 0x1: /* STLXR */
2523 if (rn == 31) {
2524 gen_check_sp_alignment(s);
2526 if (is_lasr) {
2527 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2529 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2530 gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, false);
2531 return;
2533 case 0x4: /* LDXR */
2534 case 0x5: /* LDAXR */
2535 if (rn == 31) {
2536 gen_check_sp_alignment(s);
2538 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2539 s->is_ldex = true;
2540 gen_load_exclusive(s, rt, rt2, clean_addr, size, false);
2541 if (is_lasr) {
2542 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2544 return;
2546 case 0x8: /* STLLR */
2547 if (!dc_isar_feature(aa64_lor, s)) {
2548 break;
2550 /* StoreLORelease is the same as Store-Release for QEMU. */
2551 /* fall through */
2552 case 0x9: /* STLR */
2553 /* Generate ISS for non-exclusive accesses including LASR. */
2554 if (rn == 31) {
2555 gen_check_sp_alignment(s);
2557 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2558 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2559 do_gpr_st(s, cpu_reg(s, rt), clean_addr, size, true, rt,
2560 disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2561 return;
2563 case 0xc: /* LDLAR */
2564 if (!dc_isar_feature(aa64_lor, s)) {
2565 break;
2567 /* LoadLOAcquire is the same as Load-Acquire for QEMU. */
2568 /* fall through */
2569 case 0xd: /* LDAR */
2570 /* Generate ISS for non-exclusive accesses including LASR. */
2571 if (rn == 31) {
2572 gen_check_sp_alignment(s);
2574 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2575 do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size, false, false, true, rt,
2576 disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2577 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2578 return;
2580 case 0x2: case 0x3: /* CASP / STXP */
2581 if (size & 2) { /* STXP / STLXP */
2582 if (rn == 31) {
2583 gen_check_sp_alignment(s);
2585 if (is_lasr) {
2586 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2588 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2589 gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, true);
2590 return;
2592 if (rt2 == 31
2593 && ((rt | rs) & 1) == 0
2594 && dc_isar_feature(aa64_atomics, s)) {
2595 /* CASP / CASPL */
2596 gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2597 return;
2599 break;
2601 case 0x6: case 0x7: /* CASPA / LDXP */
2602 if (size & 2) { /* LDXP / LDAXP */
2603 if (rn == 31) {
2604 gen_check_sp_alignment(s);
2606 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2607 s->is_ldex = true;
2608 gen_load_exclusive(s, rt, rt2, clean_addr, size, true);
2609 if (is_lasr) {
2610 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2612 return;
2614 if (rt2 == 31
2615 && ((rt | rs) & 1) == 0
2616 && dc_isar_feature(aa64_atomics, s)) {
2617 /* CASPA / CASPAL */
2618 gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2619 return;
2621 break;
2623 case 0xa: /* CAS */
2624 case 0xb: /* CASL */
2625 case 0xe: /* CASA */
2626 case 0xf: /* CASAL */
2627 if (rt2 == 31 && dc_isar_feature(aa64_atomics, s)) {
2628 gen_compare_and_swap(s, rs, rt, rn, size);
2629 return;
2631 break;
2633 unallocated_encoding(s);
2637 * Load register (literal)
2639 * 31 30 29 27 26 25 24 23 5 4 0
2640 * +-----+-------+---+-----+-------------------+-------+
2641 * | opc | 0 1 1 | V | 0 0 | imm19 | Rt |
2642 * +-----+-------+---+-----+-------------------+-------+
2644 * V: 1 -> vector (simd/fp)
2645 * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2646 * 10-> 32 bit signed, 11 -> prefetch
2647 * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2649 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2651 int rt = extract32(insn, 0, 5);
2652 int64_t imm = sextract32(insn, 5, 19) << 2;
2653 bool is_vector = extract32(insn, 26, 1);
2654 int opc = extract32(insn, 30, 2);
2655 bool is_signed = false;
2656 int size = 2;
2657 TCGv_i64 tcg_rt, clean_addr;
2659 if (is_vector) {
2660 if (opc == 3) {
2661 unallocated_encoding(s);
2662 return;
2664 size = 2 + opc;
2665 if (!fp_access_check(s)) {
2666 return;
2668 } else {
2669 if (opc == 3) {
2670 /* PRFM (literal) : prefetch */
2671 return;
2673 size = 2 + extract32(opc, 0, 1);
2674 is_signed = extract32(opc, 1, 1);
2677 tcg_rt = cpu_reg(s, rt);
2679 clean_addr = tcg_const_i64((s->pc - 4) + imm);
2680 if (is_vector) {
2681 do_fp_ld(s, rt, clean_addr, size);
2682 } else {
2683 /* Only unsigned 32bit loads target 32bit registers. */
2684 bool iss_sf = opc != 0;
2686 do_gpr_ld(s, tcg_rt, clean_addr, size, is_signed, false,
2687 true, rt, iss_sf, false);
2689 tcg_temp_free_i64(clean_addr);
2693 * LDNP (Load Pair - non-temporal hint)
2694 * LDP (Load Pair - non vector)
2695 * LDPSW (Load Pair Signed Word - non vector)
2696 * STNP (Store Pair - non-temporal hint)
2697 * STP (Store Pair - non vector)
2698 * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2699 * LDP (Load Pair of SIMD&FP)
2700 * STNP (Store Pair of SIMD&FP - non-temporal hint)
2701 * STP (Store Pair of SIMD&FP)
2703 * 31 30 29 27 26 25 24 23 22 21 15 14 10 9 5 4 0
2704 * +-----+-------+---+---+-------+---+-----------------------------+
2705 * | opc | 1 0 1 | V | 0 | index | L | imm7 | Rt2 | Rn | Rt |
2706 * +-----+-------+---+---+-------+---+-------+-------+------+------+
2708 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
2709 * LDPSW 01
2710 * LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2711 * V: 0 -> GPR, 1 -> Vector
2712 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2713 * 10 -> signed offset, 11 -> pre-index
2714 * L: 0 -> Store 1 -> Load
2716 * Rt, Rt2 = GPR or SIMD registers to be stored
2717 * Rn = general purpose register containing address
2718 * imm7 = signed offset (multiple of 4 or 8 depending on size)
2720 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2722 int rt = extract32(insn, 0, 5);
2723 int rn = extract32(insn, 5, 5);
2724 int rt2 = extract32(insn, 10, 5);
2725 uint64_t offset = sextract64(insn, 15, 7);
2726 int index = extract32(insn, 23, 2);
2727 bool is_vector = extract32(insn, 26, 1);
2728 bool is_load = extract32(insn, 22, 1);
2729 int opc = extract32(insn, 30, 2);
2731 bool is_signed = false;
2732 bool postindex = false;
2733 bool wback = false;
2735 TCGv_i64 clean_addr, dirty_addr;
2737 int size;
2739 if (opc == 3) {
2740 unallocated_encoding(s);
2741 return;
2744 if (is_vector) {
2745 size = 2 + opc;
2746 } else {
2747 size = 2 + extract32(opc, 1, 1);
2748 is_signed = extract32(opc, 0, 1);
2749 if (!is_load && is_signed) {
2750 unallocated_encoding(s);
2751 return;
2755 switch (index) {
2756 case 1: /* post-index */
2757 postindex = true;
2758 wback = true;
2759 break;
2760 case 0:
2761 /* signed offset with "non-temporal" hint. Since we don't emulate
2762 * caches we don't care about hints to the cache system about
2763 * data access patterns, and handle this identically to plain
2764 * signed offset.
2766 if (is_signed) {
2767 /* There is no non-temporal-hint version of LDPSW */
2768 unallocated_encoding(s);
2769 return;
2771 postindex = false;
2772 break;
2773 case 2: /* signed offset, rn not updated */
2774 postindex = false;
2775 break;
2776 case 3: /* pre-index */
2777 postindex = false;
2778 wback = true;
2779 break;
2782 if (is_vector && !fp_access_check(s)) {
2783 return;
2786 offset <<= size;
2788 if (rn == 31) {
2789 gen_check_sp_alignment(s);
2792 dirty_addr = read_cpu_reg_sp(s, rn, 1);
2793 if (!postindex) {
2794 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2796 clean_addr = clean_data_tbi(s, dirty_addr);
2798 if (is_vector) {
2799 if (is_load) {
2800 do_fp_ld(s, rt, clean_addr, size);
2801 } else {
2802 do_fp_st(s, rt, clean_addr, size);
2804 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2805 if (is_load) {
2806 do_fp_ld(s, rt2, clean_addr, size);
2807 } else {
2808 do_fp_st(s, rt2, clean_addr, size);
2810 } else {
2811 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2812 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
2814 if (is_load) {
2815 TCGv_i64 tmp = tcg_temp_new_i64();
2817 /* Do not modify tcg_rt before recognizing any exception
2818 * from the second load.
2820 do_gpr_ld(s, tmp, clean_addr, size, is_signed, false,
2821 false, 0, false, false);
2822 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2823 do_gpr_ld(s, tcg_rt2, clean_addr, size, is_signed, false,
2824 false, 0, false, false);
2826 tcg_gen_mov_i64(tcg_rt, tmp);
2827 tcg_temp_free_i64(tmp);
2828 } else {
2829 do_gpr_st(s, tcg_rt, clean_addr, size,
2830 false, 0, false, false);
2831 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2832 do_gpr_st(s, tcg_rt2, clean_addr, size,
2833 false, 0, false, false);
2837 if (wback) {
2838 if (postindex) {
2839 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2841 tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
2846 * Load/store (immediate post-indexed)
2847 * Load/store (immediate pre-indexed)
2848 * Load/store (unscaled immediate)
2850 * 31 30 29 27 26 25 24 23 22 21 20 12 11 10 9 5 4 0
2851 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2852 * |size| 1 1 1 | V | 0 0 | opc | 0 | imm9 | idx | Rn | Rt |
2853 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2855 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
2856 10 -> unprivileged
2857 * V = 0 -> non-vector
2858 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
2859 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2861 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
2862 int opc,
2863 int size,
2864 int rt,
2865 bool is_vector)
2867 int rn = extract32(insn, 5, 5);
2868 int imm9 = sextract32(insn, 12, 9);
2869 int idx = extract32(insn, 10, 2);
2870 bool is_signed = false;
2871 bool is_store = false;
2872 bool is_extended = false;
2873 bool is_unpriv = (idx == 2);
2874 bool iss_valid = !is_vector;
2875 bool post_index;
2876 bool writeback;
2878 TCGv_i64 clean_addr, dirty_addr;
2880 if (is_vector) {
2881 size |= (opc & 2) << 1;
2882 if (size > 4 || is_unpriv) {
2883 unallocated_encoding(s);
2884 return;
2886 is_store = ((opc & 1) == 0);
2887 if (!fp_access_check(s)) {
2888 return;
2890 } else {
2891 if (size == 3 && opc == 2) {
2892 /* PRFM - prefetch */
2893 if (idx != 0) {
2894 unallocated_encoding(s);
2895 return;
2897 return;
2899 if (opc == 3 && size > 1) {
2900 unallocated_encoding(s);
2901 return;
2903 is_store = (opc == 0);
2904 is_signed = extract32(opc, 1, 1);
2905 is_extended = (size < 3) && extract32(opc, 0, 1);
2908 switch (idx) {
2909 case 0:
2910 case 2:
2911 post_index = false;
2912 writeback = false;
2913 break;
2914 case 1:
2915 post_index = true;
2916 writeback = true;
2917 break;
2918 case 3:
2919 post_index = false;
2920 writeback = true;
2921 break;
2922 default:
2923 g_assert_not_reached();
2926 if (rn == 31) {
2927 gen_check_sp_alignment(s);
2930 dirty_addr = read_cpu_reg_sp(s, rn, 1);
2931 if (!post_index) {
2932 tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
2934 clean_addr = clean_data_tbi(s, dirty_addr);
2936 if (is_vector) {
2937 if (is_store) {
2938 do_fp_st(s, rt, clean_addr, size);
2939 } else {
2940 do_fp_ld(s, rt, clean_addr, size);
2942 } else {
2943 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2944 int memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
2945 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2947 if (is_store) {
2948 do_gpr_st_memidx(s, tcg_rt, clean_addr, size, memidx,
2949 iss_valid, rt, iss_sf, false);
2950 } else {
2951 do_gpr_ld_memidx(s, tcg_rt, clean_addr, size,
2952 is_signed, is_extended, memidx,
2953 iss_valid, rt, iss_sf, false);
2957 if (writeback) {
2958 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2959 if (post_index) {
2960 tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
2962 tcg_gen_mov_i64(tcg_rn, dirty_addr);
2967 * Load/store (register offset)
2969 * 31 30 29 27 26 25 24 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2970 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2971 * |size| 1 1 1 | V | 0 0 | opc | 1 | Rm | opt | S| 1 0 | Rn | Rt |
2972 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2974 * For non-vector:
2975 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2976 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2977 * For vector:
2978 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2979 * opc<0>: 0 -> store, 1 -> load
2980 * V: 1 -> vector/simd
2981 * opt: extend encoding (see DecodeRegExtend)
2982 * S: if S=1 then scale (essentially index by sizeof(size))
2983 * Rt: register to transfer into/out of
2984 * Rn: address register or SP for base
2985 * Rm: offset register or ZR for offset
2987 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
2988 int opc,
2989 int size,
2990 int rt,
2991 bool is_vector)
2993 int rn = extract32(insn, 5, 5);
2994 int shift = extract32(insn, 12, 1);
2995 int rm = extract32(insn, 16, 5);
2996 int opt = extract32(insn, 13, 3);
2997 bool is_signed = false;
2998 bool is_store = false;
2999 bool is_extended = false;
3001 TCGv_i64 tcg_rm, clean_addr, dirty_addr;
3003 if (extract32(opt, 1, 1) == 0) {
3004 unallocated_encoding(s);
3005 return;
3008 if (is_vector) {
3009 size |= (opc & 2) << 1;
3010 if (size > 4) {
3011 unallocated_encoding(s);
3012 return;
3014 is_store = !extract32(opc, 0, 1);
3015 if (!fp_access_check(s)) {
3016 return;
3018 } else {
3019 if (size == 3 && opc == 2) {
3020 /* PRFM - prefetch */
3021 return;
3023 if (opc == 3 && size > 1) {
3024 unallocated_encoding(s);
3025 return;
3027 is_store = (opc == 0);
3028 is_signed = extract32(opc, 1, 1);
3029 is_extended = (size < 3) && extract32(opc, 0, 1);
3032 if (rn == 31) {
3033 gen_check_sp_alignment(s);
3035 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3037 tcg_rm = read_cpu_reg(s, rm, 1);
3038 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
3040 tcg_gen_add_i64(dirty_addr, dirty_addr, tcg_rm);
3041 clean_addr = clean_data_tbi(s, dirty_addr);
3043 if (is_vector) {
3044 if (is_store) {
3045 do_fp_st(s, rt, clean_addr, size);
3046 } else {
3047 do_fp_ld(s, rt, clean_addr, size);
3049 } else {
3050 TCGv_i64 tcg_rt = cpu_reg(s, rt);
3051 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3052 if (is_store) {
3053 do_gpr_st(s, tcg_rt, clean_addr, size,
3054 true, rt, iss_sf, false);
3055 } else {
3056 do_gpr_ld(s, tcg_rt, clean_addr, size,
3057 is_signed, is_extended,
3058 true, rt, iss_sf, false);
3064 * Load/store (unsigned immediate)
3066 * 31 30 29 27 26 25 24 23 22 21 10 9 5
3067 * +----+-------+---+-----+-----+------------+-------+------+
3068 * |size| 1 1 1 | V | 0 1 | opc | imm12 | Rn | Rt |
3069 * +----+-------+---+-----+-----+------------+-------+------+
3071 * For non-vector:
3072 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3073 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3074 * For vector:
3075 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3076 * opc<0>: 0 -> store, 1 -> load
3077 * Rn: base address register (inc SP)
3078 * Rt: target register
3080 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
3081 int opc,
3082 int size,
3083 int rt,
3084 bool is_vector)
3086 int rn = extract32(insn, 5, 5);
3087 unsigned int imm12 = extract32(insn, 10, 12);
3088 unsigned int offset;
3090 TCGv_i64 clean_addr, dirty_addr;
3092 bool is_store;
3093 bool is_signed = false;
3094 bool is_extended = false;
3096 if (is_vector) {
3097 size |= (opc & 2) << 1;
3098 if (size > 4) {
3099 unallocated_encoding(s);
3100 return;
3102 is_store = !extract32(opc, 0, 1);
3103 if (!fp_access_check(s)) {
3104 return;
3106 } else {
3107 if (size == 3 && opc == 2) {
3108 /* PRFM - prefetch */
3109 return;
3111 if (opc == 3 && size > 1) {
3112 unallocated_encoding(s);
3113 return;
3115 is_store = (opc == 0);
3116 is_signed = extract32(opc, 1, 1);
3117 is_extended = (size < 3) && extract32(opc, 0, 1);
3120 if (rn == 31) {
3121 gen_check_sp_alignment(s);
3123 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3124 offset = imm12 << size;
3125 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3126 clean_addr = clean_data_tbi(s, dirty_addr);
3128 if (is_vector) {
3129 if (is_store) {
3130 do_fp_st(s, rt, clean_addr, size);
3131 } else {
3132 do_fp_ld(s, rt, clean_addr, size);
3134 } else {
3135 TCGv_i64 tcg_rt = cpu_reg(s, rt);
3136 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3137 if (is_store) {
3138 do_gpr_st(s, tcg_rt, clean_addr, size,
3139 true, rt, iss_sf, false);
3140 } else {
3141 do_gpr_ld(s, tcg_rt, clean_addr, size, is_signed, is_extended,
3142 true, rt, iss_sf, false);
3147 /* Atomic memory operations
3149 * 31 30 27 26 24 22 21 16 15 12 10 5 0
3150 * +------+-------+---+-----+-----+---+----+----+-----+-----+----+-----+
3151 * | size | 1 1 1 | V | 0 0 | A R | 1 | Rs | o3 | opc | 0 0 | Rn | Rt |
3152 * +------+-------+---+-----+-----+--------+----+-----+-----+----+-----+
3154 * Rt: the result register
3155 * Rn: base address or SP
3156 * Rs: the source register for the operation
3157 * V: vector flag (always 0 as of v8.3)
3158 * A: acquire flag
3159 * R: release flag
3161 static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
3162 int size, int rt, bool is_vector)
3164 int rs = extract32(insn, 16, 5);
3165 int rn = extract32(insn, 5, 5);
3166 int o3_opc = extract32(insn, 12, 4);
3167 TCGv_i64 tcg_rs, clean_addr;
3168 AtomicThreeOpFn *fn;
3170 if (is_vector || !dc_isar_feature(aa64_atomics, s)) {
3171 unallocated_encoding(s);
3172 return;
3174 switch (o3_opc) {
3175 case 000: /* LDADD */
3176 fn = tcg_gen_atomic_fetch_add_i64;
3177 break;
3178 case 001: /* LDCLR */
3179 fn = tcg_gen_atomic_fetch_and_i64;
3180 break;
3181 case 002: /* LDEOR */
3182 fn = tcg_gen_atomic_fetch_xor_i64;
3183 break;
3184 case 003: /* LDSET */
3185 fn = tcg_gen_atomic_fetch_or_i64;
3186 break;
3187 case 004: /* LDSMAX */
3188 fn = tcg_gen_atomic_fetch_smax_i64;
3189 break;
3190 case 005: /* LDSMIN */
3191 fn = tcg_gen_atomic_fetch_smin_i64;
3192 break;
3193 case 006: /* LDUMAX */
3194 fn = tcg_gen_atomic_fetch_umax_i64;
3195 break;
3196 case 007: /* LDUMIN */
3197 fn = tcg_gen_atomic_fetch_umin_i64;
3198 break;
3199 case 010: /* SWP */
3200 fn = tcg_gen_atomic_xchg_i64;
3201 break;
3202 default:
3203 unallocated_encoding(s);
3204 return;
3207 if (rn == 31) {
3208 gen_check_sp_alignment(s);
3210 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
3211 tcg_rs = read_cpu_reg(s, rs, true);
3213 if (o3_opc == 1) { /* LDCLR */
3214 tcg_gen_not_i64(tcg_rs, tcg_rs);
3217 /* The tcg atomic primitives are all full barriers. Therefore we
3218 * can ignore the Acquire and Release bits of this instruction.
3220 fn(cpu_reg(s, rt), clean_addr, tcg_rs, get_mem_index(s),
3221 s->be_data | size | MO_ALIGN);
3225 * PAC memory operations
3227 * 31 30 27 26 24 22 21 12 11 10 5 0
3228 * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3229 * | size | 1 1 1 | V | 0 0 | M S | 1 | imm9 | W | 1 | Rn | Rt |
3230 * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3232 * Rt: the result register
3233 * Rn: base address or SP
3234 * V: vector flag (always 0 as of v8.3)
3235 * M: clear for key DA, set for key DB
3236 * W: pre-indexing flag
3237 * S: sign for imm9.
3239 static void disas_ldst_pac(DisasContext *s, uint32_t insn,
3240 int size, int rt, bool is_vector)
3242 int rn = extract32(insn, 5, 5);
3243 bool is_wback = extract32(insn, 11, 1);
3244 bool use_key_a = !extract32(insn, 23, 1);
3245 int offset;
3246 TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3248 if (size != 3 || is_vector || !dc_isar_feature(aa64_pauth, s)) {
3249 unallocated_encoding(s);
3250 return;
3253 if (rn == 31) {
3254 gen_check_sp_alignment(s);
3256 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3258 if (s->pauth_active) {
3259 if (use_key_a) {
3260 gen_helper_autda(dirty_addr, cpu_env, dirty_addr, cpu_X[31]);
3261 } else {
3262 gen_helper_autdb(dirty_addr, cpu_env, dirty_addr, cpu_X[31]);
3266 /* Form the 10-bit signed, scaled offset. */
3267 offset = (extract32(insn, 22, 1) << 9) | extract32(insn, 12, 9);
3268 offset = sextract32(offset << size, 0, 10 + size);
3269 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3271 /* Note that "clean" and "dirty" here refer to TBI not PAC. */
3272 clean_addr = clean_data_tbi(s, dirty_addr);
3274 tcg_rt = cpu_reg(s, rt);
3275 do_gpr_ld(s, tcg_rt, clean_addr, size, /* is_signed */ false,
3276 /* extend */ false, /* iss_valid */ !is_wback,
3277 /* iss_srt */ rt, /* iss_sf */ true, /* iss_ar */ false);
3279 if (is_wback) {
3280 tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3284 /* Load/store register (all forms) */
3285 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
3287 int rt = extract32(insn, 0, 5);
3288 int opc = extract32(insn, 22, 2);
3289 bool is_vector = extract32(insn, 26, 1);
3290 int size = extract32(insn, 30, 2);
3292 switch (extract32(insn, 24, 2)) {
3293 case 0:
3294 if (extract32(insn, 21, 1) == 0) {
3295 /* Load/store register (unscaled immediate)
3296 * Load/store immediate pre/post-indexed
3297 * Load/store register unprivileged
3299 disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
3300 return;
3302 switch (extract32(insn, 10, 2)) {
3303 case 0:
3304 disas_ldst_atomic(s, insn, size, rt, is_vector);
3305 return;
3306 case 2:
3307 disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
3308 return;
3309 default:
3310 disas_ldst_pac(s, insn, size, rt, is_vector);
3311 return;
3313 break;
3314 case 1:
3315 disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
3316 return;
3318 unallocated_encoding(s);
3321 /* AdvSIMD load/store multiple structures
3323 * 31 30 29 23 22 21 16 15 12 11 10 9 5 4 0
3324 * +---+---+---------------+---+-------------+--------+------+------+------+
3325 * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size | Rn | Rt |
3326 * +---+---+---------------+---+-------------+--------+------+------+------+
3328 * AdvSIMD load/store multiple structures (post-indexed)
3330 * 31 30 29 23 22 21 20 16 15 12 11 10 9 5 4 0
3331 * +---+---+---------------+---+---+---------+--------+------+------+------+
3332 * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 | Rm | opcode | size | Rn | Rt |
3333 * +---+---+---------------+---+---+---------+--------+------+------+------+
3335 * Rt: first (or only) SIMD&FP register to be transferred
3336 * Rn: base address or SP
3337 * Rm (post-index only): post-index register (when !31) or size dependent #imm
3339 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
3341 int rt = extract32(insn, 0, 5);
3342 int rn = extract32(insn, 5, 5);
3343 int rm = extract32(insn, 16, 5);
3344 int size = extract32(insn, 10, 2);
3345 int opcode = extract32(insn, 12, 4);
3346 bool is_store = !extract32(insn, 22, 1);
3347 bool is_postidx = extract32(insn, 23, 1);
3348 bool is_q = extract32(insn, 30, 1);
3349 TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3350 TCGMemOp endian = s->be_data;
3352 int ebytes; /* bytes per element */
3353 int elements; /* elements per vector */
3354 int rpt; /* num iterations */
3355 int selem; /* structure elements */
3356 int r;
3358 if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
3359 unallocated_encoding(s);
3360 return;
3363 if (!is_postidx && rm != 0) {
3364 unallocated_encoding(s);
3365 return;
3368 /* From the shared decode logic */
3369 switch (opcode) {
3370 case 0x0:
3371 rpt = 1;
3372 selem = 4;
3373 break;
3374 case 0x2:
3375 rpt = 4;
3376 selem = 1;
3377 break;
3378 case 0x4:
3379 rpt = 1;
3380 selem = 3;
3381 break;
3382 case 0x6:
3383 rpt = 3;
3384 selem = 1;
3385 break;
3386 case 0x7:
3387 rpt = 1;
3388 selem = 1;
3389 break;
3390 case 0x8:
3391 rpt = 1;
3392 selem = 2;
3393 break;
3394 case 0xa:
3395 rpt = 2;
3396 selem = 1;
3397 break;
3398 default:
3399 unallocated_encoding(s);
3400 return;
3403 if (size == 3 && !is_q && selem != 1) {
3404 /* reserved */
3405 unallocated_encoding(s);
3406 return;
3409 if (!fp_access_check(s)) {
3410 return;
3413 if (rn == 31) {
3414 gen_check_sp_alignment(s);
3417 /* For our purposes, bytes are always little-endian. */
3418 if (size == 0) {
3419 endian = MO_LE;
3422 /* Consecutive little-endian elements from a single register
3423 * can be promoted to a larger little-endian operation.
3425 if (selem == 1 && endian == MO_LE) {
3426 size = 3;
3428 ebytes = 1 << size;
3429 elements = (is_q ? 16 : 8) / ebytes;
3431 tcg_rn = cpu_reg_sp(s, rn);
3432 clean_addr = clean_data_tbi(s, tcg_rn);
3433 tcg_ebytes = tcg_const_i64(ebytes);
3435 for (r = 0; r < rpt; r++) {
3436 int e;
3437 for (e = 0; e < elements; e++) {
3438 int xs;
3439 for (xs = 0; xs < selem; xs++) {
3440 int tt = (rt + r + xs) % 32;
3441 if (is_store) {
3442 do_vec_st(s, tt, e, clean_addr, size, endian);
3443 } else {
3444 do_vec_ld(s, tt, e, clean_addr, size, endian);
3446 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3450 tcg_temp_free_i64(tcg_ebytes);
3452 if (!is_store) {
3453 /* For non-quad operations, setting a slice of the low
3454 * 64 bits of the register clears the high 64 bits (in
3455 * the ARM ARM pseudocode this is implicit in the fact
3456 * that 'rval' is a 64 bit wide variable).
3457 * For quad operations, we might still need to zero the
3458 * high bits of SVE.
3460 for (r = 0; r < rpt * selem; r++) {
3461 int tt = (rt + r) % 32;
3462 clear_vec_high(s, is_q, tt);
3466 if (is_postidx) {
3467 if (rm == 31) {
3468 tcg_gen_addi_i64(tcg_rn, tcg_rn, rpt * elements * selem * ebytes);
3469 } else {
3470 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3475 /* AdvSIMD load/store single structure
3477 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3478 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3479 * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size | Rn | Rt |
3480 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3482 * AdvSIMD load/store single structure (post-indexed)
3484 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3485 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3486 * | 0 | Q | 0 0 1 1 0 1 1 | L R | Rm | opc | S | size | Rn | Rt |
3487 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3489 * Rt: first (or only) SIMD&FP register to be transferred
3490 * Rn: base address or SP
3491 * Rm (post-index only): post-index register (when !31) or size dependent #imm
3492 * index = encoded in Q:S:size dependent on size
3494 * lane_size = encoded in R, opc
3495 * transfer width = encoded in opc, S, size
3497 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
3499 int rt = extract32(insn, 0, 5);
3500 int rn = extract32(insn, 5, 5);
3501 int rm = extract32(insn, 16, 5);
3502 int size = extract32(insn, 10, 2);
3503 int S = extract32(insn, 12, 1);
3504 int opc = extract32(insn, 13, 3);
3505 int R = extract32(insn, 21, 1);
3506 int is_load = extract32(insn, 22, 1);
3507 int is_postidx = extract32(insn, 23, 1);
3508 int is_q = extract32(insn, 30, 1);
3510 int scale = extract32(opc, 1, 2);
3511 int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
3512 bool replicate = false;
3513 int index = is_q << 3 | S << 2 | size;
3514 int ebytes, xs;
3515 TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3517 if (extract32(insn, 31, 1)) {
3518 unallocated_encoding(s);
3519 return;
3521 if (!is_postidx && rm != 0) {
3522 unallocated_encoding(s);
3523 return;
3526 switch (scale) {
3527 case 3:
3528 if (!is_load || S) {
3529 unallocated_encoding(s);
3530 return;
3532 scale = size;
3533 replicate = true;
3534 break;
3535 case 0:
3536 break;
3537 case 1:
3538 if (extract32(size, 0, 1)) {
3539 unallocated_encoding(s);
3540 return;
3542 index >>= 1;
3543 break;
3544 case 2:
3545 if (extract32(size, 1, 1)) {
3546 unallocated_encoding(s);
3547 return;
3549 if (!extract32(size, 0, 1)) {
3550 index >>= 2;
3551 } else {
3552 if (S) {
3553 unallocated_encoding(s);
3554 return;
3556 index >>= 3;
3557 scale = 3;
3559 break;
3560 default:
3561 g_assert_not_reached();
3564 if (!fp_access_check(s)) {
3565 return;
3568 ebytes = 1 << scale;
3570 if (rn == 31) {
3571 gen_check_sp_alignment(s);
3574 tcg_rn = cpu_reg_sp(s, rn);
3575 clean_addr = clean_data_tbi(s, tcg_rn);
3576 tcg_ebytes = tcg_const_i64(ebytes);
3578 for (xs = 0; xs < selem; xs++) {
3579 if (replicate) {
3580 /* Load and replicate to all elements */
3581 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3583 tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr,
3584 get_mem_index(s), s->be_data + scale);
3585 tcg_gen_gvec_dup_i64(scale, vec_full_reg_offset(s, rt),
3586 (is_q + 1) * 8, vec_full_reg_size(s),
3587 tcg_tmp);
3588 tcg_temp_free_i64(tcg_tmp);
3589 } else {
3590 /* Load/store one element per register */
3591 if (is_load) {
3592 do_vec_ld(s, rt, index, clean_addr, scale, s->be_data);
3593 } else {
3594 do_vec_st(s, rt, index, clean_addr, scale, s->be_data);
3597 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3598 rt = (rt + 1) % 32;
3600 tcg_temp_free_i64(tcg_ebytes);
3602 if (is_postidx) {
3603 if (rm == 31) {
3604 tcg_gen_addi_i64(tcg_rn, tcg_rn, selem * ebytes);
3605 } else {
3606 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3611 /* Loads and stores */
3612 static void disas_ldst(DisasContext *s, uint32_t insn)
3614 switch (extract32(insn, 24, 6)) {
3615 case 0x08: /* Load/store exclusive */
3616 disas_ldst_excl(s, insn);
3617 break;
3618 case 0x18: case 0x1c: /* Load register (literal) */
3619 disas_ld_lit(s, insn);
3620 break;
3621 case 0x28: case 0x29:
3622 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
3623 disas_ldst_pair(s, insn);
3624 break;
3625 case 0x38: case 0x39:
3626 case 0x3c: case 0x3d: /* Load/store register (all forms) */
3627 disas_ldst_reg(s, insn);
3628 break;
3629 case 0x0c: /* AdvSIMD load/store multiple structures */
3630 disas_ldst_multiple_struct(s, insn);
3631 break;
3632 case 0x0d: /* AdvSIMD load/store single structure */
3633 disas_ldst_single_struct(s, insn);
3634 break;
3635 default:
3636 unallocated_encoding(s);
3637 break;
3641 /* PC-rel. addressing
3642 * 31 30 29 28 24 23 5 4 0
3643 * +----+-------+-----------+-------------------+------+
3644 * | op | immlo | 1 0 0 0 0 | immhi | Rd |
3645 * +----+-------+-----------+-------------------+------+
3647 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
3649 unsigned int page, rd;
3650 uint64_t base;
3651 uint64_t offset;
3653 page = extract32(insn, 31, 1);
3654 /* SignExtend(immhi:immlo) -> offset */
3655 offset = sextract64(insn, 5, 19);
3656 offset = offset << 2 | extract32(insn, 29, 2);
3657 rd = extract32(insn, 0, 5);
3658 base = s->pc - 4;
3660 if (page) {
3661 /* ADRP (page based) */
3662 base &= ~0xfff;
3663 offset <<= 12;
3666 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
3670 * Add/subtract (immediate)
3672 * 31 30 29 28 24 23 22 21 10 9 5 4 0
3673 * +--+--+--+-----------+-----+-------------+-----+-----+
3674 * |sf|op| S| 1 0 0 0 1 |shift| imm12 | Rn | Rd |
3675 * +--+--+--+-----------+-----+-------------+-----+-----+
3677 * sf: 0 -> 32bit, 1 -> 64bit
3678 * op: 0 -> add , 1 -> sub
3679 * S: 1 -> set flags
3680 * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
3682 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
3684 int rd = extract32(insn, 0, 5);
3685 int rn = extract32(insn, 5, 5);
3686 uint64_t imm = extract32(insn, 10, 12);
3687 int shift = extract32(insn, 22, 2);
3688 bool setflags = extract32(insn, 29, 1);
3689 bool sub_op = extract32(insn, 30, 1);
3690 bool is_64bit = extract32(insn, 31, 1);
3692 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3693 TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
3694 TCGv_i64 tcg_result;
3696 switch (shift) {
3697 case 0x0:
3698 break;
3699 case 0x1:
3700 imm <<= 12;
3701 break;
3702 default:
3703 unallocated_encoding(s);
3704 return;
3707 tcg_result = tcg_temp_new_i64();
3708 if (!setflags) {
3709 if (sub_op) {
3710 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
3711 } else {
3712 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
3714 } else {
3715 TCGv_i64 tcg_imm = tcg_const_i64(imm);
3716 if (sub_op) {
3717 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
3718 } else {
3719 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
3721 tcg_temp_free_i64(tcg_imm);
3724 if (is_64bit) {
3725 tcg_gen_mov_i64(tcg_rd, tcg_result);
3726 } else {
3727 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3730 tcg_temp_free_i64(tcg_result);
3733 /* The input should be a value in the bottom e bits (with higher
3734 * bits zero); returns that value replicated into every element
3735 * of size e in a 64 bit integer.
3737 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
3739 assert(e != 0);
3740 while (e < 64) {
3741 mask |= mask << e;
3742 e *= 2;
3744 return mask;
3747 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
3748 static inline uint64_t bitmask64(unsigned int length)
3750 assert(length > 0 && length <= 64);
3751 return ~0ULL >> (64 - length);
3754 /* Simplified variant of pseudocode DecodeBitMasks() for the case where we
3755 * only require the wmask. Returns false if the imms/immr/immn are a reserved
3756 * value (ie should cause a guest UNDEF exception), and true if they are
3757 * valid, in which case the decoded bit pattern is written to result.
3759 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
3760 unsigned int imms, unsigned int immr)
3762 uint64_t mask;
3763 unsigned e, levels, s, r;
3764 int len;
3766 assert(immn < 2 && imms < 64 && immr < 64);
3768 /* The bit patterns we create here are 64 bit patterns which
3769 * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
3770 * 64 bits each. Each element contains the same value: a run
3771 * of between 1 and e-1 non-zero bits, rotated within the
3772 * element by between 0 and e-1 bits.
3774 * The element size and run length are encoded into immn (1 bit)
3775 * and imms (6 bits) as follows:
3776 * 64 bit elements: immn = 1, imms = <length of run - 1>
3777 * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
3778 * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
3779 * 8 bit elements: immn = 0, imms = 110 : <length of run - 1>
3780 * 4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
3781 * 2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
3782 * Notice that immn = 0, imms = 11111x is the only combination
3783 * not covered by one of the above options; this is reserved.
3784 * Further, <length of run - 1> all-ones is a reserved pattern.
3786 * In all cases the rotation is by immr % e (and immr is 6 bits).
3789 /* First determine the element size */
3790 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
3791 if (len < 1) {
3792 /* This is the immn == 0, imms == 0x11111x case */
3793 return false;
3795 e = 1 << len;
3797 levels = e - 1;
3798 s = imms & levels;
3799 r = immr & levels;
3801 if (s == levels) {
3802 /* <length of run - 1> mustn't be all-ones. */
3803 return false;
3806 /* Create the value of one element: s+1 set bits rotated
3807 * by r within the element (which is e bits wide)...
3809 mask = bitmask64(s + 1);
3810 if (r) {
3811 mask = (mask >> r) | (mask << (e - r));
3812 mask &= bitmask64(e);
3814 /* ...then replicate the element over the whole 64 bit value */
3815 mask = bitfield_replicate(mask, e);
3816 *result = mask;
3817 return true;
3820 /* Logical (immediate)
3821 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
3822 * +----+-----+-------------+---+------+------+------+------+
3823 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms | Rn | Rd |
3824 * +----+-----+-------------+---+------+------+------+------+
3826 static void disas_logic_imm(DisasContext *s, uint32_t insn)
3828 unsigned int sf, opc, is_n, immr, imms, rn, rd;
3829 TCGv_i64 tcg_rd, tcg_rn;
3830 uint64_t wmask;
3831 bool is_and = false;
3833 sf = extract32(insn, 31, 1);
3834 opc = extract32(insn, 29, 2);
3835 is_n = extract32(insn, 22, 1);
3836 immr = extract32(insn, 16, 6);
3837 imms = extract32(insn, 10, 6);
3838 rn = extract32(insn, 5, 5);
3839 rd = extract32(insn, 0, 5);
3841 if (!sf && is_n) {
3842 unallocated_encoding(s);
3843 return;
3846 if (opc == 0x3) { /* ANDS */
3847 tcg_rd = cpu_reg(s, rd);
3848 } else {
3849 tcg_rd = cpu_reg_sp(s, rd);
3851 tcg_rn = cpu_reg(s, rn);
3853 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
3854 /* some immediate field values are reserved */
3855 unallocated_encoding(s);
3856 return;
3859 if (!sf) {
3860 wmask &= 0xffffffff;
3863 switch (opc) {
3864 case 0x3: /* ANDS */
3865 case 0x0: /* AND */
3866 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
3867 is_and = true;
3868 break;
3869 case 0x1: /* ORR */
3870 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
3871 break;
3872 case 0x2: /* EOR */
3873 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
3874 break;
3875 default:
3876 assert(FALSE); /* must handle all above */
3877 break;
3880 if (!sf && !is_and) {
3881 /* zero extend final result; we know we can skip this for AND
3882 * since the immediate had the high 32 bits clear.
3884 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3887 if (opc == 3) { /* ANDS */
3888 gen_logic_CC(sf, tcg_rd);
3893 * Move wide (immediate)
3895 * 31 30 29 28 23 22 21 20 5 4 0
3896 * +--+-----+-------------+-----+----------------+------+
3897 * |sf| opc | 1 0 0 1 0 1 | hw | imm16 | Rd |
3898 * +--+-----+-------------+-----+----------------+------+
3900 * sf: 0 -> 32 bit, 1 -> 64 bit
3901 * opc: 00 -> N, 10 -> Z, 11 -> K
3902 * hw: shift/16 (0,16, and sf only 32, 48)
3904 static void disas_movw_imm(DisasContext *s, uint32_t insn)
3906 int rd = extract32(insn, 0, 5);
3907 uint64_t imm = extract32(insn, 5, 16);
3908 int sf = extract32(insn, 31, 1);
3909 int opc = extract32(insn, 29, 2);
3910 int pos = extract32(insn, 21, 2) << 4;
3911 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3912 TCGv_i64 tcg_imm;
3914 if (!sf && (pos >= 32)) {
3915 unallocated_encoding(s);
3916 return;
3919 switch (opc) {
3920 case 0: /* MOVN */
3921 case 2: /* MOVZ */
3922 imm <<= pos;
3923 if (opc == 0) {
3924 imm = ~imm;
3926 if (!sf) {
3927 imm &= 0xffffffffu;
3929 tcg_gen_movi_i64(tcg_rd, imm);
3930 break;
3931 case 3: /* MOVK */
3932 tcg_imm = tcg_const_i64(imm);
3933 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
3934 tcg_temp_free_i64(tcg_imm);
3935 if (!sf) {
3936 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3938 break;
3939 default:
3940 unallocated_encoding(s);
3941 break;
3945 /* Bitfield
3946 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
3947 * +----+-----+-------------+---+------+------+------+------+
3948 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms | Rn | Rd |
3949 * +----+-----+-------------+---+------+------+------+------+
3951 static void disas_bitfield(DisasContext *s, uint32_t insn)
3953 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
3954 TCGv_i64 tcg_rd, tcg_tmp;
3956 sf = extract32(insn, 31, 1);
3957 opc = extract32(insn, 29, 2);
3958 n = extract32(insn, 22, 1);
3959 ri = extract32(insn, 16, 6);
3960 si = extract32(insn, 10, 6);
3961 rn = extract32(insn, 5, 5);
3962 rd = extract32(insn, 0, 5);
3963 bitsize = sf ? 64 : 32;
3965 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
3966 unallocated_encoding(s);
3967 return;
3970 tcg_rd = cpu_reg(s, rd);
3972 /* Suppress the zero-extend for !sf. Since RI and SI are constrained
3973 to be smaller than bitsize, we'll never reference data outside the
3974 low 32-bits anyway. */
3975 tcg_tmp = read_cpu_reg(s, rn, 1);
3977 /* Recognize simple(r) extractions. */
3978 if (si >= ri) {
3979 /* Wd<s-r:0> = Wn<s:r> */
3980 len = (si - ri) + 1;
3981 if (opc == 0) { /* SBFM: ASR, SBFX, SXTB, SXTH, SXTW */
3982 tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
3983 goto done;
3984 } else if (opc == 2) { /* UBFM: UBFX, LSR, UXTB, UXTH */
3985 tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
3986 return;
3988 /* opc == 1, BXFIL fall through to deposit */
3989 tcg_gen_extract_i64(tcg_tmp, tcg_tmp, ri, len);
3990 pos = 0;
3991 } else {
3992 /* Handle the ri > si case with a deposit
3993 * Wd<32+s-r,32-r> = Wn<s:0>
3995 len = si + 1;
3996 pos = (bitsize - ri) & (bitsize - 1);
3999 if (opc == 0 && len < ri) {
4000 /* SBFM: sign extend the destination field from len to fill
4001 the balance of the word. Let the deposit below insert all
4002 of those sign bits. */
4003 tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4004 len = ri;
4007 if (opc == 1) { /* BFM, BXFIL */
4008 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4009 } else {
4010 /* SBFM or UBFM: We start with zero, and we haven't modified
4011 any bits outside bitsize, therefore the zero-extension
4012 below is unneeded. */
4013 tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4014 return;
4017 done:
4018 if (!sf) { /* zero extend final result */
4019 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4023 /* Extract
4024 * 31 30 29 28 23 22 21 20 16 15 10 9 5 4 0
4025 * +----+------+-------------+---+----+------+--------+------+------+
4026 * | sf | op21 | 1 0 0 1 1 1 | N | o0 | Rm | imms | Rn | Rd |
4027 * +----+------+-------------+---+----+------+--------+------+------+
4029 static void disas_extract(DisasContext *s, uint32_t insn)
4031 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
4033 sf = extract32(insn, 31, 1);
4034 n = extract32(insn, 22, 1);
4035 rm = extract32(insn, 16, 5);
4036 imm = extract32(insn, 10, 6);
4037 rn = extract32(insn, 5, 5);
4038 rd = extract32(insn, 0, 5);
4039 op21 = extract32(insn, 29, 2);
4040 op0 = extract32(insn, 21, 1);
4041 bitsize = sf ? 64 : 32;
4043 if (sf != n || op21 || op0 || imm >= bitsize) {
4044 unallocated_encoding(s);
4045 } else {
4046 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4048 tcg_rd = cpu_reg(s, rd);
4050 if (unlikely(imm == 0)) {
4051 /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4052 * so an extract from bit 0 is a special case.
4054 if (sf) {
4055 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
4056 } else {
4057 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
4059 } else if (rm == rn) { /* ROR */
4060 tcg_rm = cpu_reg(s, rm);
4061 if (sf) {
4062 tcg_gen_rotri_i64(tcg_rd, tcg_rm, imm);
4063 } else {
4064 TCGv_i32 tmp = tcg_temp_new_i32();
4065 tcg_gen_extrl_i64_i32(tmp, tcg_rm);
4066 tcg_gen_rotri_i32(tmp, tmp, imm);
4067 tcg_gen_extu_i32_i64(tcg_rd, tmp);
4068 tcg_temp_free_i32(tmp);
4070 } else {
4071 tcg_rm = read_cpu_reg(s, rm, sf);
4072 tcg_rn = read_cpu_reg(s, rn, sf);
4073 tcg_gen_shri_i64(tcg_rm, tcg_rm, imm);
4074 tcg_gen_shli_i64(tcg_rn, tcg_rn, bitsize - imm);
4075 tcg_gen_or_i64(tcg_rd, tcg_rm, tcg_rn);
4076 if (!sf) {
4077 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4083 /* Data processing - immediate */
4084 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
4086 switch (extract32(insn, 23, 6)) {
4087 case 0x20: case 0x21: /* PC-rel. addressing */
4088 disas_pc_rel_adr(s, insn);
4089 break;
4090 case 0x22: case 0x23: /* Add/subtract (immediate) */
4091 disas_add_sub_imm(s, insn);
4092 break;
4093 case 0x24: /* Logical (immediate) */
4094 disas_logic_imm(s, insn);
4095 break;
4096 case 0x25: /* Move wide (immediate) */
4097 disas_movw_imm(s, insn);
4098 break;
4099 case 0x26: /* Bitfield */
4100 disas_bitfield(s, insn);
4101 break;
4102 case 0x27: /* Extract */
4103 disas_extract(s, insn);
4104 break;
4105 default:
4106 unallocated_encoding(s);
4107 break;
4111 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4112 * Note that it is the caller's responsibility to ensure that the
4113 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4114 * mandated semantics for out of range shifts.
4116 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4117 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4119 switch (shift_type) {
4120 case A64_SHIFT_TYPE_LSL:
4121 tcg_gen_shl_i64(dst, src, shift_amount);
4122 break;
4123 case A64_SHIFT_TYPE_LSR:
4124 tcg_gen_shr_i64(dst, src, shift_amount);
4125 break;
4126 case A64_SHIFT_TYPE_ASR:
4127 if (!sf) {
4128 tcg_gen_ext32s_i64(dst, src);
4130 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4131 break;
4132 case A64_SHIFT_TYPE_ROR:
4133 if (sf) {
4134 tcg_gen_rotr_i64(dst, src, shift_amount);
4135 } else {
4136 TCGv_i32 t0, t1;
4137 t0 = tcg_temp_new_i32();
4138 t1 = tcg_temp_new_i32();
4139 tcg_gen_extrl_i64_i32(t0, src);
4140 tcg_gen_extrl_i64_i32(t1, shift_amount);
4141 tcg_gen_rotr_i32(t0, t0, t1);
4142 tcg_gen_extu_i32_i64(dst, t0);
4143 tcg_temp_free_i32(t0);
4144 tcg_temp_free_i32(t1);
4146 break;
4147 default:
4148 assert(FALSE); /* all shift types should be handled */
4149 break;
4152 if (!sf) { /* zero extend final result */
4153 tcg_gen_ext32u_i64(dst, dst);
4157 /* Shift a TCGv src by immediate, put result in dst.
4158 * The shift amount must be in range (this should always be true as the
4159 * relevant instructions will UNDEF on bad shift immediates).
4161 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4162 enum a64_shift_type shift_type, unsigned int shift_i)
4164 assert(shift_i < (sf ? 64 : 32));
4166 if (shift_i == 0) {
4167 tcg_gen_mov_i64(dst, src);
4168 } else {
4169 TCGv_i64 shift_const;
4171 shift_const = tcg_const_i64(shift_i);
4172 shift_reg(dst, src, sf, shift_type, shift_const);
4173 tcg_temp_free_i64(shift_const);
4177 /* Logical (shifted register)
4178 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
4179 * +----+-----+-----------+-------+---+------+--------+------+------+
4180 * | sf | opc | 0 1 0 1 0 | shift | N | Rm | imm6 | Rn | Rd |
4181 * +----+-----+-----------+-------+---+------+--------+------+------+
4183 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4185 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4186 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4188 sf = extract32(insn, 31, 1);
4189 opc = extract32(insn, 29, 2);
4190 shift_type = extract32(insn, 22, 2);
4191 invert = extract32(insn, 21, 1);
4192 rm = extract32(insn, 16, 5);
4193 shift_amount = extract32(insn, 10, 6);
4194 rn = extract32(insn, 5, 5);
4195 rd = extract32(insn, 0, 5);
4197 if (!sf && (shift_amount & (1 << 5))) {
4198 unallocated_encoding(s);
4199 return;
4202 tcg_rd = cpu_reg(s, rd);
4204 if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4205 /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4206 * register-register MOV and MVN, so it is worth special casing.
4208 tcg_rm = cpu_reg(s, rm);
4209 if (invert) {
4210 tcg_gen_not_i64(tcg_rd, tcg_rm);
4211 if (!sf) {
4212 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4214 } else {
4215 if (sf) {
4216 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4217 } else {
4218 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4221 return;
4224 tcg_rm = read_cpu_reg(s, rm, sf);
4226 if (shift_amount) {
4227 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4230 tcg_rn = cpu_reg(s, rn);
4232 switch (opc | (invert << 2)) {
4233 case 0: /* AND */
4234 case 3: /* ANDS */
4235 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4236 break;
4237 case 1: /* ORR */
4238 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4239 break;
4240 case 2: /* EOR */
4241 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4242 break;
4243 case 4: /* BIC */
4244 case 7: /* BICS */
4245 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4246 break;
4247 case 5: /* ORN */
4248 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4249 break;
4250 case 6: /* EON */
4251 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4252 break;
4253 default:
4254 assert(FALSE);
4255 break;
4258 if (!sf) {
4259 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4262 if (opc == 3) {
4263 gen_logic_CC(sf, tcg_rd);
4268 * Add/subtract (extended register)
4270 * 31|30|29|28 24|23 22|21|20 16|15 13|12 10|9 5|4 0|
4271 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4272 * |sf|op| S| 0 1 0 1 1 | opt | 1| Rm |option| imm3 | Rn | Rd |
4273 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4275 * sf: 0 -> 32bit, 1 -> 64bit
4276 * op: 0 -> add , 1 -> sub
4277 * S: 1 -> set flags
4278 * opt: 00
4279 * option: extension type (see DecodeRegExtend)
4280 * imm3: optional shift to Rm
4282 * Rd = Rn + LSL(extend(Rm), amount)
4284 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4286 int rd = extract32(insn, 0, 5);
4287 int rn = extract32(insn, 5, 5);
4288 int imm3 = extract32(insn, 10, 3);
4289 int option = extract32(insn, 13, 3);
4290 int rm = extract32(insn, 16, 5);
4291 int opt = extract32(insn, 22, 2);
4292 bool setflags = extract32(insn, 29, 1);
4293 bool sub_op = extract32(insn, 30, 1);
4294 bool sf = extract32(insn, 31, 1);
4296 TCGv_i64 tcg_rm, tcg_rn; /* temps */
4297 TCGv_i64 tcg_rd;
4298 TCGv_i64 tcg_result;
4300 if (imm3 > 4 || opt != 0) {
4301 unallocated_encoding(s);
4302 return;
4305 /* non-flag setting ops may use SP */
4306 if (!setflags) {
4307 tcg_rd = cpu_reg_sp(s, rd);
4308 } else {
4309 tcg_rd = cpu_reg(s, rd);
4311 tcg_rn = read_cpu_reg_sp(s, rn, sf);
4313 tcg_rm = read_cpu_reg(s, rm, sf);
4314 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4316 tcg_result = tcg_temp_new_i64();
4318 if (!setflags) {
4319 if (sub_op) {
4320 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4321 } else {
4322 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4324 } else {
4325 if (sub_op) {
4326 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4327 } else {
4328 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4332 if (sf) {
4333 tcg_gen_mov_i64(tcg_rd, tcg_result);
4334 } else {
4335 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4338 tcg_temp_free_i64(tcg_result);
4342 * Add/subtract (shifted register)
4344 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
4345 * +--+--+--+-----------+-----+--+-------+---------+------+------+
4346 * |sf|op| S| 0 1 0 1 1 |shift| 0| Rm | imm6 | Rn | Rd |
4347 * +--+--+--+-----------+-----+--+-------+---------+------+------+
4349 * sf: 0 -> 32bit, 1 -> 64bit
4350 * op: 0 -> add , 1 -> sub
4351 * S: 1 -> set flags
4352 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4353 * imm6: Shift amount to apply to Rm before the add/sub
4355 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4357 int rd = extract32(insn, 0, 5);
4358 int rn = extract32(insn, 5, 5);
4359 int imm6 = extract32(insn, 10, 6);
4360 int rm = extract32(insn, 16, 5);
4361 int shift_type = extract32(insn, 22, 2);
4362 bool setflags = extract32(insn, 29, 1);
4363 bool sub_op = extract32(insn, 30, 1);
4364 bool sf = extract32(insn, 31, 1);
4366 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4367 TCGv_i64 tcg_rn, tcg_rm;
4368 TCGv_i64 tcg_result;
4370 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4371 unallocated_encoding(s);
4372 return;
4375 tcg_rn = read_cpu_reg(s, rn, sf);
4376 tcg_rm = read_cpu_reg(s, rm, sf);
4378 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4380 tcg_result = tcg_temp_new_i64();
4382 if (!setflags) {
4383 if (sub_op) {
4384 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4385 } else {
4386 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4388 } else {
4389 if (sub_op) {
4390 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4391 } else {
4392 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4396 if (sf) {
4397 tcg_gen_mov_i64(tcg_rd, tcg_result);
4398 } else {
4399 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4402 tcg_temp_free_i64(tcg_result);
4405 /* Data-processing (3 source)
4407 * 31 30 29 28 24 23 21 20 16 15 14 10 9 5 4 0
4408 * +--+------+-----------+------+------+----+------+------+------+
4409 * |sf| op54 | 1 1 0 1 1 | op31 | Rm | o0 | Ra | Rn | Rd |
4410 * +--+------+-----------+------+------+----+------+------+------+
4412 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4414 int rd = extract32(insn, 0, 5);
4415 int rn = extract32(insn, 5, 5);
4416 int ra = extract32(insn, 10, 5);
4417 int rm = extract32(insn, 16, 5);
4418 int op_id = (extract32(insn, 29, 3) << 4) |
4419 (extract32(insn, 21, 3) << 1) |
4420 extract32(insn, 15, 1);
4421 bool sf = extract32(insn, 31, 1);
4422 bool is_sub = extract32(op_id, 0, 1);
4423 bool is_high = extract32(op_id, 2, 1);
4424 bool is_signed = false;
4425 TCGv_i64 tcg_op1;
4426 TCGv_i64 tcg_op2;
4427 TCGv_i64 tcg_tmp;
4429 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
4430 switch (op_id) {
4431 case 0x42: /* SMADDL */
4432 case 0x43: /* SMSUBL */
4433 case 0x44: /* SMULH */
4434 is_signed = true;
4435 break;
4436 case 0x0: /* MADD (32bit) */
4437 case 0x1: /* MSUB (32bit) */
4438 case 0x40: /* MADD (64bit) */
4439 case 0x41: /* MSUB (64bit) */
4440 case 0x4a: /* UMADDL */
4441 case 0x4b: /* UMSUBL */
4442 case 0x4c: /* UMULH */
4443 break;
4444 default:
4445 unallocated_encoding(s);
4446 return;
4449 if (is_high) {
4450 TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
4451 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4452 TCGv_i64 tcg_rn = cpu_reg(s, rn);
4453 TCGv_i64 tcg_rm = cpu_reg(s, rm);
4455 if (is_signed) {
4456 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4457 } else {
4458 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4461 tcg_temp_free_i64(low_bits);
4462 return;
4465 tcg_op1 = tcg_temp_new_i64();
4466 tcg_op2 = tcg_temp_new_i64();
4467 tcg_tmp = tcg_temp_new_i64();
4469 if (op_id < 0x42) {
4470 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
4471 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
4472 } else {
4473 if (is_signed) {
4474 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
4475 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
4476 } else {
4477 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
4478 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
4482 if (ra == 31 && !is_sub) {
4483 /* Special-case MADD with rA == XZR; it is the standard MUL alias */
4484 tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
4485 } else {
4486 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
4487 if (is_sub) {
4488 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4489 } else {
4490 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4494 if (!sf) {
4495 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
4498 tcg_temp_free_i64(tcg_op1);
4499 tcg_temp_free_i64(tcg_op2);
4500 tcg_temp_free_i64(tcg_tmp);
4503 /* Add/subtract (with carry)
4504 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
4505 * +--+--+--+------------------------+------+-------------+------+-----+
4506 * |sf|op| S| 1 1 0 1 0 0 0 0 | rm | 0 0 0 0 0 0 | Rn | Rd |
4507 * +--+--+--+------------------------+------+-------------+------+-----+
4510 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
4512 unsigned int sf, op, setflags, rm, rn, rd;
4513 TCGv_i64 tcg_y, tcg_rn, tcg_rd;
4515 sf = extract32(insn, 31, 1);
4516 op = extract32(insn, 30, 1);
4517 setflags = extract32(insn, 29, 1);
4518 rm = extract32(insn, 16, 5);
4519 rn = extract32(insn, 5, 5);
4520 rd = extract32(insn, 0, 5);
4522 tcg_rd = cpu_reg(s, rd);
4523 tcg_rn = cpu_reg(s, rn);
4525 if (op) {
4526 tcg_y = new_tmp_a64(s);
4527 tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
4528 } else {
4529 tcg_y = cpu_reg(s, rm);
4532 if (setflags) {
4533 gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
4534 } else {
4535 gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
4540 * Rotate right into flags
4541 * 31 30 29 21 15 10 5 4 0
4542 * +--+--+--+-----------------+--------+-----------+------+--+------+
4543 * |sf|op| S| 1 1 0 1 0 0 0 0 | imm6 | 0 0 0 0 1 | Rn |o2| mask |
4544 * +--+--+--+-----------------+--------+-----------+------+--+------+
4546 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
4548 int mask = extract32(insn, 0, 4);
4549 int o2 = extract32(insn, 4, 1);
4550 int rn = extract32(insn, 5, 5);
4551 int imm6 = extract32(insn, 15, 6);
4552 int sf_op_s = extract32(insn, 29, 3);
4553 TCGv_i64 tcg_rn;
4554 TCGv_i32 nzcv;
4556 if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
4557 unallocated_encoding(s);
4558 return;
4561 tcg_rn = read_cpu_reg(s, rn, 1);
4562 tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
4564 nzcv = tcg_temp_new_i32();
4565 tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
4567 if (mask & 8) { /* N */
4568 tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
4570 if (mask & 4) { /* Z */
4571 tcg_gen_not_i32(cpu_ZF, nzcv);
4572 tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
4574 if (mask & 2) { /* C */
4575 tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
4577 if (mask & 1) { /* V */
4578 tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
4581 tcg_temp_free_i32(nzcv);
4585 * Evaluate into flags
4586 * 31 30 29 21 15 14 10 5 4 0
4587 * +--+--+--+-----------------+---------+----+---------+------+--+------+
4588 * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 | Rn |o3| mask |
4589 * +--+--+--+-----------------+---------+----+---------+------+--+------+
4591 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
4593 int o3_mask = extract32(insn, 0, 5);
4594 int rn = extract32(insn, 5, 5);
4595 int o2 = extract32(insn, 15, 6);
4596 int sz = extract32(insn, 14, 1);
4597 int sf_op_s = extract32(insn, 29, 3);
4598 TCGv_i32 tmp;
4599 int shift;
4601 if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
4602 !dc_isar_feature(aa64_condm_4, s)) {
4603 unallocated_encoding(s);
4604 return;
4606 shift = sz ? 16 : 24; /* SETF16 or SETF8 */
4608 tmp = tcg_temp_new_i32();
4609 tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
4610 tcg_gen_shli_i32(cpu_NF, tmp, shift);
4611 tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
4612 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
4613 tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
4614 tcg_temp_free_i32(tmp);
4617 /* Conditional compare (immediate / register)
4618 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
4619 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
4620 * |sf|op| S| 1 1 0 1 0 0 1 0 |imm5/rm | cond |i/r |o2| Rn |o3|nzcv |
4621 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
4622 * [1] y [0] [0]
4624 static void disas_cc(DisasContext *s, uint32_t insn)
4626 unsigned int sf, op, y, cond, rn, nzcv, is_imm;
4627 TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
4628 TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
4629 DisasCompare c;
4631 if (!extract32(insn, 29, 1)) {
4632 unallocated_encoding(s);
4633 return;
4635 if (insn & (1 << 10 | 1 << 4)) {
4636 unallocated_encoding(s);
4637 return;
4639 sf = extract32(insn, 31, 1);
4640 op = extract32(insn, 30, 1);
4641 is_imm = extract32(insn, 11, 1);
4642 y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
4643 cond = extract32(insn, 12, 4);
4644 rn = extract32(insn, 5, 5);
4645 nzcv = extract32(insn, 0, 4);
4647 /* Set T0 = !COND. */
4648 tcg_t0 = tcg_temp_new_i32();
4649 arm_test_cc(&c, cond);
4650 tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
4651 arm_free_cc(&c);
4653 /* Load the arguments for the new comparison. */
4654 if (is_imm) {
4655 tcg_y = new_tmp_a64(s);
4656 tcg_gen_movi_i64(tcg_y, y);
4657 } else {
4658 tcg_y = cpu_reg(s, y);
4660 tcg_rn = cpu_reg(s, rn);
4662 /* Set the flags for the new comparison. */
4663 tcg_tmp = tcg_temp_new_i64();
4664 if (op) {
4665 gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
4666 } else {
4667 gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
4669 tcg_temp_free_i64(tcg_tmp);
4671 /* If COND was false, force the flags to #nzcv. Compute two masks
4672 * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
4673 * For tcg hosts that support ANDC, we can make do with just T1.
4674 * In either case, allow the tcg optimizer to delete any unused mask.
4676 tcg_t1 = tcg_temp_new_i32();
4677 tcg_t2 = tcg_temp_new_i32();
4678 tcg_gen_neg_i32(tcg_t1, tcg_t0);
4679 tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
4681 if (nzcv & 8) { /* N */
4682 tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
4683 } else {
4684 if (TCG_TARGET_HAS_andc_i32) {
4685 tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
4686 } else {
4687 tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
4690 if (nzcv & 4) { /* Z */
4691 if (TCG_TARGET_HAS_andc_i32) {
4692 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
4693 } else {
4694 tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
4696 } else {
4697 tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
4699 if (nzcv & 2) { /* C */
4700 tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
4701 } else {
4702 if (TCG_TARGET_HAS_andc_i32) {
4703 tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
4704 } else {
4705 tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
4708 if (nzcv & 1) { /* V */
4709 tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
4710 } else {
4711 if (TCG_TARGET_HAS_andc_i32) {
4712 tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
4713 } else {
4714 tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
4717 tcg_temp_free_i32(tcg_t0);
4718 tcg_temp_free_i32(tcg_t1);
4719 tcg_temp_free_i32(tcg_t2);
4722 /* Conditional select
4723 * 31 30 29 28 21 20 16 15 12 11 10 9 5 4 0
4724 * +----+----+---+-----------------+------+------+-----+------+------+
4725 * | sf | op | S | 1 1 0 1 0 1 0 0 | Rm | cond | op2 | Rn | Rd |
4726 * +----+----+---+-----------------+------+------+-----+------+------+
4728 static void disas_cond_select(DisasContext *s, uint32_t insn)
4730 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
4731 TCGv_i64 tcg_rd, zero;
4732 DisasCompare64 c;
4734 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
4735 /* S == 1 or op2<1> == 1 */
4736 unallocated_encoding(s);
4737 return;
4739 sf = extract32(insn, 31, 1);
4740 else_inv = extract32(insn, 30, 1);
4741 rm = extract32(insn, 16, 5);
4742 cond = extract32(insn, 12, 4);
4743 else_inc = extract32(insn, 10, 1);
4744 rn = extract32(insn, 5, 5);
4745 rd = extract32(insn, 0, 5);
4747 tcg_rd = cpu_reg(s, rd);
4749 a64_test_cc(&c, cond);
4750 zero = tcg_const_i64(0);
4752 if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
4753 /* CSET & CSETM. */
4754 tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
4755 if (else_inv) {
4756 tcg_gen_neg_i64(tcg_rd, tcg_rd);
4758 } else {
4759 TCGv_i64 t_true = cpu_reg(s, rn);
4760 TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
4761 if (else_inv && else_inc) {
4762 tcg_gen_neg_i64(t_false, t_false);
4763 } else if (else_inv) {
4764 tcg_gen_not_i64(t_false, t_false);
4765 } else if (else_inc) {
4766 tcg_gen_addi_i64(t_false, t_false, 1);
4768 tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
4771 tcg_temp_free_i64(zero);
4772 a64_free_cc(&c);
4774 if (!sf) {
4775 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4779 static void handle_clz(DisasContext *s, unsigned int sf,
4780 unsigned int rn, unsigned int rd)
4782 TCGv_i64 tcg_rd, tcg_rn;
4783 tcg_rd = cpu_reg(s, rd);
4784 tcg_rn = cpu_reg(s, rn);
4786 if (sf) {
4787 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
4788 } else {
4789 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4790 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4791 tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
4792 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4793 tcg_temp_free_i32(tcg_tmp32);
4797 static void handle_cls(DisasContext *s, unsigned int sf,
4798 unsigned int rn, unsigned int rd)
4800 TCGv_i64 tcg_rd, tcg_rn;
4801 tcg_rd = cpu_reg(s, rd);
4802 tcg_rn = cpu_reg(s, rn);
4804 if (sf) {
4805 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
4806 } else {
4807 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4808 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4809 tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
4810 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4811 tcg_temp_free_i32(tcg_tmp32);
4815 static void handle_rbit(DisasContext *s, unsigned int sf,
4816 unsigned int rn, unsigned int rd)
4818 TCGv_i64 tcg_rd, tcg_rn;
4819 tcg_rd = cpu_reg(s, rd);
4820 tcg_rn = cpu_reg(s, rn);
4822 if (sf) {
4823 gen_helper_rbit64(tcg_rd, tcg_rn);
4824 } else {
4825 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4826 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4827 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
4828 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4829 tcg_temp_free_i32(tcg_tmp32);
4833 /* REV with sf==1, opcode==3 ("REV64") */
4834 static void handle_rev64(DisasContext *s, unsigned int sf,
4835 unsigned int rn, unsigned int rd)
4837 if (!sf) {
4838 unallocated_encoding(s);
4839 return;
4841 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
4844 /* REV with sf==0, opcode==2
4845 * REV32 (sf==1, opcode==2)
4847 static void handle_rev32(DisasContext *s, unsigned int sf,
4848 unsigned int rn, unsigned int rd)
4850 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4852 if (sf) {
4853 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4854 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4856 /* bswap32_i64 requires zero high word */
4857 tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
4858 tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
4859 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
4860 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
4861 tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
4863 tcg_temp_free_i64(tcg_tmp);
4864 } else {
4865 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
4866 tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
4870 /* REV16 (opcode==1) */
4871 static void handle_rev16(DisasContext *s, unsigned int sf,
4872 unsigned int rn, unsigned int rd)
4874 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4875 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4876 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4877 TCGv_i64 mask = tcg_const_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
4879 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
4880 tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
4881 tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
4882 tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
4883 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
4885 tcg_temp_free_i64(mask);
4886 tcg_temp_free_i64(tcg_tmp);
4889 /* Data-processing (1 source)
4890 * 31 30 29 28 21 20 16 15 10 9 5 4 0
4891 * +----+---+---+-----------------+---------+--------+------+------+
4892 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode | Rn | Rd |
4893 * +----+---+---+-----------------+---------+--------+------+------+
4895 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
4897 unsigned int sf, opcode, opcode2, rn, rd;
4898 TCGv_i64 tcg_rd;
4900 if (extract32(insn, 29, 1)) {
4901 unallocated_encoding(s);
4902 return;
4905 sf = extract32(insn, 31, 1);
4906 opcode = extract32(insn, 10, 6);
4907 opcode2 = extract32(insn, 16, 5);
4908 rn = extract32(insn, 5, 5);
4909 rd = extract32(insn, 0, 5);
4911 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
4913 switch (MAP(sf, opcode2, opcode)) {
4914 case MAP(0, 0x00, 0x00): /* RBIT */
4915 case MAP(1, 0x00, 0x00):
4916 handle_rbit(s, sf, rn, rd);
4917 break;
4918 case MAP(0, 0x00, 0x01): /* REV16 */
4919 case MAP(1, 0x00, 0x01):
4920 handle_rev16(s, sf, rn, rd);
4921 break;
4922 case MAP(0, 0x00, 0x02): /* REV/REV32 */
4923 case MAP(1, 0x00, 0x02):
4924 handle_rev32(s, sf, rn, rd);
4925 break;
4926 case MAP(1, 0x00, 0x03): /* REV64 */
4927 handle_rev64(s, sf, rn, rd);
4928 break;
4929 case MAP(0, 0x00, 0x04): /* CLZ */
4930 case MAP(1, 0x00, 0x04):
4931 handle_clz(s, sf, rn, rd);
4932 break;
4933 case MAP(0, 0x00, 0x05): /* CLS */
4934 case MAP(1, 0x00, 0x05):
4935 handle_cls(s, sf, rn, rd);
4936 break;
4937 case MAP(1, 0x01, 0x00): /* PACIA */
4938 if (s->pauth_active) {
4939 tcg_rd = cpu_reg(s, rd);
4940 gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4941 } else if (!dc_isar_feature(aa64_pauth, s)) {
4942 goto do_unallocated;
4944 break;
4945 case MAP(1, 0x01, 0x01): /* PACIB */
4946 if (s->pauth_active) {
4947 tcg_rd = cpu_reg(s, rd);
4948 gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4949 } else if (!dc_isar_feature(aa64_pauth, s)) {
4950 goto do_unallocated;
4952 break;
4953 case MAP(1, 0x01, 0x02): /* PACDA */
4954 if (s->pauth_active) {
4955 tcg_rd = cpu_reg(s, rd);
4956 gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4957 } else if (!dc_isar_feature(aa64_pauth, s)) {
4958 goto do_unallocated;
4960 break;
4961 case MAP(1, 0x01, 0x03): /* PACDB */
4962 if (s->pauth_active) {
4963 tcg_rd = cpu_reg(s, rd);
4964 gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4965 } else if (!dc_isar_feature(aa64_pauth, s)) {
4966 goto do_unallocated;
4968 break;
4969 case MAP(1, 0x01, 0x04): /* AUTIA */
4970 if (s->pauth_active) {
4971 tcg_rd = cpu_reg(s, rd);
4972 gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4973 } else if (!dc_isar_feature(aa64_pauth, s)) {
4974 goto do_unallocated;
4976 break;
4977 case MAP(1, 0x01, 0x05): /* AUTIB */
4978 if (s->pauth_active) {
4979 tcg_rd = cpu_reg(s, rd);
4980 gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4981 } else if (!dc_isar_feature(aa64_pauth, s)) {
4982 goto do_unallocated;
4984 break;
4985 case MAP(1, 0x01, 0x06): /* AUTDA */
4986 if (s->pauth_active) {
4987 tcg_rd = cpu_reg(s, rd);
4988 gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4989 } else if (!dc_isar_feature(aa64_pauth, s)) {
4990 goto do_unallocated;
4992 break;
4993 case MAP(1, 0x01, 0x07): /* AUTDB */
4994 if (s->pauth_active) {
4995 tcg_rd = cpu_reg(s, rd);
4996 gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4997 } else if (!dc_isar_feature(aa64_pauth, s)) {
4998 goto do_unallocated;
5000 break;
5001 case MAP(1, 0x01, 0x08): /* PACIZA */
5002 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5003 goto do_unallocated;
5004 } else if (s->pauth_active) {
5005 tcg_rd = cpu_reg(s, rd);
5006 gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5008 break;
5009 case MAP(1, 0x01, 0x09): /* PACIZB */
5010 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5011 goto do_unallocated;
5012 } else if (s->pauth_active) {
5013 tcg_rd = cpu_reg(s, rd);
5014 gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5016 break;
5017 case MAP(1, 0x01, 0x0a): /* PACDZA */
5018 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5019 goto do_unallocated;
5020 } else if (s->pauth_active) {
5021 tcg_rd = cpu_reg(s, rd);
5022 gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5024 break;
5025 case MAP(1, 0x01, 0x0b): /* PACDZB */
5026 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5027 goto do_unallocated;
5028 } else if (s->pauth_active) {
5029 tcg_rd = cpu_reg(s, rd);
5030 gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5032 break;
5033 case MAP(1, 0x01, 0x0c): /* AUTIZA */
5034 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5035 goto do_unallocated;
5036 } else if (s->pauth_active) {
5037 tcg_rd = cpu_reg(s, rd);
5038 gen_helper_autia(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5040 break;
5041 case MAP(1, 0x01, 0x0d): /* AUTIZB */
5042 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5043 goto do_unallocated;
5044 } else if (s->pauth_active) {
5045 tcg_rd = cpu_reg(s, rd);
5046 gen_helper_autib(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5048 break;
5049 case MAP(1, 0x01, 0x0e): /* AUTDZA */
5050 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5051 goto do_unallocated;
5052 } else if (s->pauth_active) {
5053 tcg_rd = cpu_reg(s, rd);
5054 gen_helper_autda(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5056 break;
5057 case MAP(1, 0x01, 0x0f): /* AUTDZB */
5058 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5059 goto do_unallocated;
5060 } else if (s->pauth_active) {
5061 tcg_rd = cpu_reg(s, rd);
5062 gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5064 break;
5065 case MAP(1, 0x01, 0x10): /* XPACI */
5066 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5067 goto do_unallocated;
5068 } else if (s->pauth_active) {
5069 tcg_rd = cpu_reg(s, rd);
5070 gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
5072 break;
5073 case MAP(1, 0x01, 0x11): /* XPACD */
5074 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5075 goto do_unallocated;
5076 } else if (s->pauth_active) {
5077 tcg_rd = cpu_reg(s, rd);
5078 gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5080 break;
5081 default:
5082 do_unallocated:
5083 unallocated_encoding(s);
5084 break;
5087 #undef MAP
5090 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5091 unsigned int rm, unsigned int rn, unsigned int rd)
5093 TCGv_i64 tcg_n, tcg_m, tcg_rd;
5094 tcg_rd = cpu_reg(s, rd);
5096 if (!sf && is_signed) {
5097 tcg_n = new_tmp_a64(s);
5098 tcg_m = new_tmp_a64(s);
5099 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5100 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5101 } else {
5102 tcg_n = read_cpu_reg(s, rn, sf);
5103 tcg_m = read_cpu_reg(s, rm, sf);
5106 if (is_signed) {
5107 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5108 } else {
5109 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5112 if (!sf) { /* zero extend final result */
5113 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5117 /* LSLV, LSRV, ASRV, RORV */
5118 static void handle_shift_reg(DisasContext *s,
5119 enum a64_shift_type shift_type, unsigned int sf,
5120 unsigned int rm, unsigned int rn, unsigned int rd)
5122 TCGv_i64 tcg_shift = tcg_temp_new_i64();
5123 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5124 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5126 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5127 shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5128 tcg_temp_free_i64(tcg_shift);
5131 /* CRC32[BHWX], CRC32C[BHWX] */
5132 static void handle_crc32(DisasContext *s,
5133 unsigned int sf, unsigned int sz, bool crc32c,
5134 unsigned int rm, unsigned int rn, unsigned int rd)
5136 TCGv_i64 tcg_acc, tcg_val;
5137 TCGv_i32 tcg_bytes;
5139 if (!dc_isar_feature(aa64_crc32, s)
5140 || (sf == 1 && sz != 3)
5141 || (sf == 0 && sz == 3)) {
5142 unallocated_encoding(s);
5143 return;
5146 if (sz == 3) {
5147 tcg_val = cpu_reg(s, rm);
5148 } else {
5149 uint64_t mask;
5150 switch (sz) {
5151 case 0:
5152 mask = 0xFF;
5153 break;
5154 case 1:
5155 mask = 0xFFFF;
5156 break;
5157 case 2:
5158 mask = 0xFFFFFFFF;
5159 break;
5160 default:
5161 g_assert_not_reached();
5163 tcg_val = new_tmp_a64(s);
5164 tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5167 tcg_acc = cpu_reg(s, rn);
5168 tcg_bytes = tcg_const_i32(1 << sz);
5170 if (crc32c) {
5171 gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5172 } else {
5173 gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5176 tcg_temp_free_i32(tcg_bytes);
5179 /* Data-processing (2 source)
5180 * 31 30 29 28 21 20 16 15 10 9 5 4 0
5181 * +----+---+---+-----------------+------+--------+------+------+
5182 * | sf | 0 | S | 1 1 0 1 0 1 1 0 | Rm | opcode | Rn | Rd |
5183 * +----+---+---+-----------------+------+--------+------+------+
5185 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5187 unsigned int sf, rm, opcode, rn, rd;
5188 sf = extract32(insn, 31, 1);
5189 rm = extract32(insn, 16, 5);
5190 opcode = extract32(insn, 10, 6);
5191 rn = extract32(insn, 5, 5);
5192 rd = extract32(insn, 0, 5);
5194 if (extract32(insn, 29, 1)) {
5195 unallocated_encoding(s);
5196 return;
5199 switch (opcode) {
5200 case 2: /* UDIV */
5201 handle_div(s, false, sf, rm, rn, rd);
5202 break;
5203 case 3: /* SDIV */
5204 handle_div(s, true, sf, rm, rn, rd);
5205 break;
5206 case 8: /* LSLV */
5207 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5208 break;
5209 case 9: /* LSRV */
5210 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5211 break;
5212 case 10: /* ASRV */
5213 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5214 break;
5215 case 11: /* RORV */
5216 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5217 break;
5218 case 12: /* PACGA */
5219 if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5220 goto do_unallocated;
5222 gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5223 cpu_reg(s, rn), cpu_reg_sp(s, rm));
5224 break;
5225 case 16:
5226 case 17:
5227 case 18:
5228 case 19:
5229 case 20:
5230 case 21:
5231 case 22:
5232 case 23: /* CRC32 */
5234 int sz = extract32(opcode, 0, 2);
5235 bool crc32c = extract32(opcode, 2, 1);
5236 handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5237 break;
5239 default:
5240 do_unallocated:
5241 unallocated_encoding(s);
5242 break;
5247 * Data processing - register
5248 * 31 30 29 28 25 21 20 16 10 0
5249 * +--+---+--+---+-------+-----+-------+-------+---------+
5250 * | |op0| |op1| 1 0 1 | op2 | | op3 | |
5251 * +--+---+--+---+-------+-----+-------+-------+---------+
5253 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5255 int op0 = extract32(insn, 30, 1);
5256 int op1 = extract32(insn, 28, 1);
5257 int op2 = extract32(insn, 21, 4);
5258 int op3 = extract32(insn, 10, 6);
5260 if (!op1) {
5261 if (op2 & 8) {
5262 if (op2 & 1) {
5263 /* Add/sub (extended register) */
5264 disas_add_sub_ext_reg(s, insn);
5265 } else {
5266 /* Add/sub (shifted register) */
5267 disas_add_sub_reg(s, insn);
5269 } else {
5270 /* Logical (shifted register) */
5271 disas_logic_reg(s, insn);
5273 return;
5276 switch (op2) {
5277 case 0x0:
5278 switch (op3) {
5279 case 0x00: /* Add/subtract (with carry) */
5280 disas_adc_sbc(s, insn);
5281 break;
5283 case 0x01: /* Rotate right into flags */
5284 case 0x21:
5285 disas_rotate_right_into_flags(s, insn);
5286 break;
5288 case 0x02: /* Evaluate into flags */
5289 case 0x12:
5290 case 0x22:
5291 case 0x32:
5292 disas_evaluate_into_flags(s, insn);
5293 break;
5295 default:
5296 goto do_unallocated;
5298 break;
5300 case 0x2: /* Conditional compare */
5301 disas_cc(s, insn); /* both imm and reg forms */
5302 break;
5304 case 0x4: /* Conditional select */
5305 disas_cond_select(s, insn);
5306 break;
5308 case 0x6: /* Data-processing */
5309 if (op0) { /* (1 source) */
5310 disas_data_proc_1src(s, insn);
5311 } else { /* (2 source) */
5312 disas_data_proc_2src(s, insn);
5314 break;
5315 case 0x8 ... 0xf: /* (3 source) */
5316 disas_data_proc_3src(s, insn);
5317 break;
5319 default:
5320 do_unallocated:
5321 unallocated_encoding(s);
5322 break;
5326 static void handle_fp_compare(DisasContext *s, int size,
5327 unsigned int rn, unsigned int rm,
5328 bool cmp_with_zero, bool signal_all_nans)
5330 TCGv_i64 tcg_flags = tcg_temp_new_i64();
5331 TCGv_ptr fpst = get_fpstatus_ptr(size == MO_16);
5333 if (size == MO_64) {
5334 TCGv_i64 tcg_vn, tcg_vm;
5336 tcg_vn = read_fp_dreg(s, rn);
5337 if (cmp_with_zero) {
5338 tcg_vm = tcg_const_i64(0);
5339 } else {
5340 tcg_vm = read_fp_dreg(s, rm);
5342 if (signal_all_nans) {
5343 gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5344 } else {
5345 gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5347 tcg_temp_free_i64(tcg_vn);
5348 tcg_temp_free_i64(tcg_vm);
5349 } else {
5350 TCGv_i32 tcg_vn = tcg_temp_new_i32();
5351 TCGv_i32 tcg_vm = tcg_temp_new_i32();
5353 read_vec_element_i32(s, tcg_vn, rn, 0, size);
5354 if (cmp_with_zero) {
5355 tcg_gen_movi_i32(tcg_vm, 0);
5356 } else {
5357 read_vec_element_i32(s, tcg_vm, rm, 0, size);
5360 switch (size) {
5361 case MO_32:
5362 if (signal_all_nans) {
5363 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5364 } else {
5365 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5367 break;
5368 case MO_16:
5369 if (signal_all_nans) {
5370 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5371 } else {
5372 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5374 break;
5375 default:
5376 g_assert_not_reached();
5379 tcg_temp_free_i32(tcg_vn);
5380 tcg_temp_free_i32(tcg_vm);
5383 tcg_temp_free_ptr(fpst);
5385 gen_set_nzcv(tcg_flags);
5387 tcg_temp_free_i64(tcg_flags);
5390 /* Floating point compare
5391 * 31 30 29 28 24 23 22 21 20 16 15 14 13 10 9 5 4 0
5392 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5393 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | op | 1 0 0 0 | Rn | op2 |
5394 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5396 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5398 unsigned int mos, type, rm, op, rn, opc, op2r;
5399 int size;
5401 mos = extract32(insn, 29, 3);
5402 type = extract32(insn, 22, 2);
5403 rm = extract32(insn, 16, 5);
5404 op = extract32(insn, 14, 2);
5405 rn = extract32(insn, 5, 5);
5406 opc = extract32(insn, 3, 2);
5407 op2r = extract32(insn, 0, 3);
5409 if (mos || op || op2r) {
5410 unallocated_encoding(s);
5411 return;
5414 switch (type) {
5415 case 0:
5416 size = MO_32;
5417 break;
5418 case 1:
5419 size = MO_64;
5420 break;
5421 case 3:
5422 size = MO_16;
5423 if (dc_isar_feature(aa64_fp16, s)) {
5424 break;
5426 /* fallthru */
5427 default:
5428 unallocated_encoding(s);
5429 return;
5432 if (!fp_access_check(s)) {
5433 return;
5436 handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
5439 /* Floating point conditional compare
5440 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
5441 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5442 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 0 1 | Rn | op | nzcv |
5443 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5445 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
5447 unsigned int mos, type, rm, cond, rn, op, nzcv;
5448 TCGv_i64 tcg_flags;
5449 TCGLabel *label_continue = NULL;
5450 int size;
5452 mos = extract32(insn, 29, 3);
5453 type = extract32(insn, 22, 2);
5454 rm = extract32(insn, 16, 5);
5455 cond = extract32(insn, 12, 4);
5456 rn = extract32(insn, 5, 5);
5457 op = extract32(insn, 4, 1);
5458 nzcv = extract32(insn, 0, 4);
5460 if (mos) {
5461 unallocated_encoding(s);
5462 return;
5465 switch (type) {
5466 case 0:
5467 size = MO_32;
5468 break;
5469 case 1:
5470 size = MO_64;
5471 break;
5472 case 3:
5473 size = MO_16;
5474 if (dc_isar_feature(aa64_fp16, s)) {
5475 break;
5477 /* fallthru */
5478 default:
5479 unallocated_encoding(s);
5480 return;
5483 if (!fp_access_check(s)) {
5484 return;
5487 if (cond < 0x0e) { /* not always */
5488 TCGLabel *label_match = gen_new_label();
5489 label_continue = gen_new_label();
5490 arm_gen_test_cc(cond, label_match);
5491 /* nomatch: */
5492 tcg_flags = tcg_const_i64(nzcv << 28);
5493 gen_set_nzcv(tcg_flags);
5494 tcg_temp_free_i64(tcg_flags);
5495 tcg_gen_br(label_continue);
5496 gen_set_label(label_match);
5499 handle_fp_compare(s, size, rn, rm, false, op);
5501 if (cond < 0x0e) {
5502 gen_set_label(label_continue);
5506 /* Floating point conditional select
5507 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
5508 * +---+---+---+-----------+------+---+------+------+-----+------+------+
5509 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 1 1 | Rn | Rd |
5510 * +---+---+---+-----------+------+---+------+------+-----+------+------+
5512 static void disas_fp_csel(DisasContext *s, uint32_t insn)
5514 unsigned int mos, type, rm, cond, rn, rd;
5515 TCGv_i64 t_true, t_false, t_zero;
5516 DisasCompare64 c;
5517 TCGMemOp sz;
5519 mos = extract32(insn, 29, 3);
5520 type = extract32(insn, 22, 2);
5521 rm = extract32(insn, 16, 5);
5522 cond = extract32(insn, 12, 4);
5523 rn = extract32(insn, 5, 5);
5524 rd = extract32(insn, 0, 5);
5526 if (mos) {
5527 unallocated_encoding(s);
5528 return;
5531 switch (type) {
5532 case 0:
5533 sz = MO_32;
5534 break;
5535 case 1:
5536 sz = MO_64;
5537 break;
5538 case 3:
5539 sz = MO_16;
5540 if (dc_isar_feature(aa64_fp16, s)) {
5541 break;
5543 /* fallthru */
5544 default:
5545 unallocated_encoding(s);
5546 return;
5549 if (!fp_access_check(s)) {
5550 return;
5553 /* Zero extend sreg & hreg inputs to 64 bits now. */
5554 t_true = tcg_temp_new_i64();
5555 t_false = tcg_temp_new_i64();
5556 read_vec_element(s, t_true, rn, 0, sz);
5557 read_vec_element(s, t_false, rm, 0, sz);
5559 a64_test_cc(&c, cond);
5560 t_zero = tcg_const_i64(0);
5561 tcg_gen_movcond_i64(c.cond, t_true, c.value, t_zero, t_true, t_false);
5562 tcg_temp_free_i64(t_zero);
5563 tcg_temp_free_i64(t_false);
5564 a64_free_cc(&c);
5566 /* Note that sregs & hregs write back zeros to the high bits,
5567 and we've already done the zero-extension. */
5568 write_fp_dreg(s, rd, t_true);
5569 tcg_temp_free_i64(t_true);
5572 /* Floating-point data-processing (1 source) - half precision */
5573 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
5575 TCGv_ptr fpst = NULL;
5576 TCGv_i32 tcg_op = read_fp_hreg(s, rn);
5577 TCGv_i32 tcg_res = tcg_temp_new_i32();
5579 switch (opcode) {
5580 case 0x0: /* FMOV */
5581 tcg_gen_mov_i32(tcg_res, tcg_op);
5582 break;
5583 case 0x1: /* FABS */
5584 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
5585 break;
5586 case 0x2: /* FNEG */
5587 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
5588 break;
5589 case 0x3: /* FSQRT */
5590 fpst = get_fpstatus_ptr(true);
5591 gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
5592 break;
5593 case 0x8: /* FRINTN */
5594 case 0x9: /* FRINTP */
5595 case 0xa: /* FRINTM */
5596 case 0xb: /* FRINTZ */
5597 case 0xc: /* FRINTA */
5599 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
5600 fpst = get_fpstatus_ptr(true);
5602 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5603 gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
5605 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5606 tcg_temp_free_i32(tcg_rmode);
5607 break;
5609 case 0xe: /* FRINTX */
5610 fpst = get_fpstatus_ptr(true);
5611 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
5612 break;
5613 case 0xf: /* FRINTI */
5614 fpst = get_fpstatus_ptr(true);
5615 gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
5616 break;
5617 default:
5618 abort();
5621 write_fp_sreg(s, rd, tcg_res);
5623 if (fpst) {
5624 tcg_temp_free_ptr(fpst);
5626 tcg_temp_free_i32(tcg_op);
5627 tcg_temp_free_i32(tcg_res);
5630 /* Floating-point data-processing (1 source) - single precision */
5631 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
5633 TCGv_ptr fpst;
5634 TCGv_i32 tcg_op;
5635 TCGv_i32 tcg_res;
5637 fpst = get_fpstatus_ptr(false);
5638 tcg_op = read_fp_sreg(s, rn);
5639 tcg_res = tcg_temp_new_i32();
5641 switch (opcode) {
5642 case 0x0: /* FMOV */
5643 tcg_gen_mov_i32(tcg_res, tcg_op);
5644 break;
5645 case 0x1: /* FABS */
5646 gen_helper_vfp_abss(tcg_res, tcg_op);
5647 break;
5648 case 0x2: /* FNEG */
5649 gen_helper_vfp_negs(tcg_res, tcg_op);
5650 break;
5651 case 0x3: /* FSQRT */
5652 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
5653 break;
5654 case 0x8: /* FRINTN */
5655 case 0x9: /* FRINTP */
5656 case 0xa: /* FRINTM */
5657 case 0xb: /* FRINTZ */
5658 case 0xc: /* FRINTA */
5660 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
5662 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5663 gen_helper_rints(tcg_res, tcg_op, fpst);
5665 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5666 tcg_temp_free_i32(tcg_rmode);
5667 break;
5669 case 0xe: /* FRINTX */
5670 gen_helper_rints_exact(tcg_res, tcg_op, fpst);
5671 break;
5672 case 0xf: /* FRINTI */
5673 gen_helper_rints(tcg_res, tcg_op, fpst);
5674 break;
5675 default:
5676 abort();
5679 write_fp_sreg(s, rd, tcg_res);
5681 tcg_temp_free_ptr(fpst);
5682 tcg_temp_free_i32(tcg_op);
5683 tcg_temp_free_i32(tcg_res);
5686 /* Floating-point data-processing (1 source) - double precision */
5687 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
5689 TCGv_ptr fpst;
5690 TCGv_i64 tcg_op;
5691 TCGv_i64 tcg_res;
5693 switch (opcode) {
5694 case 0x0: /* FMOV */
5695 gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
5696 return;
5699 fpst = get_fpstatus_ptr(false);
5700 tcg_op = read_fp_dreg(s, rn);
5701 tcg_res = tcg_temp_new_i64();
5703 switch (opcode) {
5704 case 0x1: /* FABS */
5705 gen_helper_vfp_absd(tcg_res, tcg_op);
5706 break;
5707 case 0x2: /* FNEG */
5708 gen_helper_vfp_negd(tcg_res, tcg_op);
5709 break;
5710 case 0x3: /* FSQRT */
5711 gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
5712 break;
5713 case 0x8: /* FRINTN */
5714 case 0x9: /* FRINTP */
5715 case 0xa: /* FRINTM */
5716 case 0xb: /* FRINTZ */
5717 case 0xc: /* FRINTA */
5719 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
5721 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5722 gen_helper_rintd(tcg_res, tcg_op, fpst);
5724 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5725 tcg_temp_free_i32(tcg_rmode);
5726 break;
5728 case 0xe: /* FRINTX */
5729 gen_helper_rintd_exact(tcg_res, tcg_op, fpst);
5730 break;
5731 case 0xf: /* FRINTI */
5732 gen_helper_rintd(tcg_res, tcg_op, fpst);
5733 break;
5734 default:
5735 abort();
5738 write_fp_dreg(s, rd, tcg_res);
5740 tcg_temp_free_ptr(fpst);
5741 tcg_temp_free_i64(tcg_op);
5742 tcg_temp_free_i64(tcg_res);
5745 static void handle_fp_fcvt(DisasContext *s, int opcode,
5746 int rd, int rn, int dtype, int ntype)
5748 switch (ntype) {
5749 case 0x0:
5751 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
5752 if (dtype == 1) {
5753 /* Single to double */
5754 TCGv_i64 tcg_rd = tcg_temp_new_i64();
5755 gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
5756 write_fp_dreg(s, rd, tcg_rd);
5757 tcg_temp_free_i64(tcg_rd);
5758 } else {
5759 /* Single to half */
5760 TCGv_i32 tcg_rd = tcg_temp_new_i32();
5761 TCGv_i32 ahp = get_ahp_flag();
5762 TCGv_ptr fpst = get_fpstatus_ptr(false);
5764 gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
5765 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
5766 write_fp_sreg(s, rd, tcg_rd);
5767 tcg_temp_free_i32(tcg_rd);
5768 tcg_temp_free_i32(ahp);
5769 tcg_temp_free_ptr(fpst);
5771 tcg_temp_free_i32(tcg_rn);
5772 break;
5774 case 0x1:
5776 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
5777 TCGv_i32 tcg_rd = tcg_temp_new_i32();
5778 if (dtype == 0) {
5779 /* Double to single */
5780 gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
5781 } else {
5782 TCGv_ptr fpst = get_fpstatus_ptr(false);
5783 TCGv_i32 ahp = get_ahp_flag();
5784 /* Double to half */
5785 gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
5786 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
5787 tcg_temp_free_ptr(fpst);
5788 tcg_temp_free_i32(ahp);
5790 write_fp_sreg(s, rd, tcg_rd);
5791 tcg_temp_free_i32(tcg_rd);
5792 tcg_temp_free_i64(tcg_rn);
5793 break;
5795 case 0x3:
5797 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
5798 TCGv_ptr tcg_fpst = get_fpstatus_ptr(false);
5799 TCGv_i32 tcg_ahp = get_ahp_flag();
5800 tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
5801 if (dtype == 0) {
5802 /* Half to single */
5803 TCGv_i32 tcg_rd = tcg_temp_new_i32();
5804 gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
5805 write_fp_sreg(s, rd, tcg_rd);
5806 tcg_temp_free_ptr(tcg_fpst);
5807 tcg_temp_free_i32(tcg_ahp);
5808 tcg_temp_free_i32(tcg_rd);
5809 } else {
5810 /* Half to double */
5811 TCGv_i64 tcg_rd = tcg_temp_new_i64();
5812 gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
5813 write_fp_dreg(s, rd, tcg_rd);
5814 tcg_temp_free_i64(tcg_rd);
5816 tcg_temp_free_i32(tcg_rn);
5817 break;
5819 default:
5820 abort();
5824 /* Floating point data-processing (1 source)
5825 * 31 30 29 28 24 23 22 21 20 15 14 10 9 5 4 0
5826 * +---+---+---+-----------+------+---+--------+-----------+------+------+
5827 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 | Rn | Rd |
5828 * +---+---+---+-----------+------+---+--------+-----------+------+------+
5830 static void disas_fp_1src(DisasContext *s, uint32_t insn)
5832 int mos = extract32(insn, 29, 3);
5833 int type = extract32(insn, 22, 2);
5834 int opcode = extract32(insn, 15, 6);
5835 int rn = extract32(insn, 5, 5);
5836 int rd = extract32(insn, 0, 5);
5838 if (mos) {
5839 unallocated_encoding(s);
5840 return;
5843 switch (opcode) {
5844 case 0x4: case 0x5: case 0x7:
5846 /* FCVT between half, single and double precision */
5847 int dtype = extract32(opcode, 0, 2);
5848 if (type == 2 || dtype == type) {
5849 unallocated_encoding(s);
5850 return;
5852 if (!fp_access_check(s)) {
5853 return;
5856 handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
5857 break;
5859 case 0x0 ... 0x3:
5860 case 0x8 ... 0xc:
5861 case 0xe ... 0xf:
5862 /* 32-to-32 and 64-to-64 ops */
5863 switch (type) {
5864 case 0:
5865 if (!fp_access_check(s)) {
5866 return;
5869 handle_fp_1src_single(s, opcode, rd, rn);
5870 break;
5871 case 1:
5872 if (!fp_access_check(s)) {
5873 return;
5876 handle_fp_1src_double(s, opcode, rd, rn);
5877 break;
5878 case 3:
5879 if (!dc_isar_feature(aa64_fp16, s)) {
5880 unallocated_encoding(s);
5881 return;
5884 if (!fp_access_check(s)) {
5885 return;
5888 handle_fp_1src_half(s, opcode, rd, rn);
5889 break;
5890 default:
5891 unallocated_encoding(s);
5893 break;
5894 default:
5895 unallocated_encoding(s);
5896 break;
5900 /* Floating-point data-processing (2 source) - single precision */
5901 static void handle_fp_2src_single(DisasContext *s, int opcode,
5902 int rd, int rn, int rm)
5904 TCGv_i32 tcg_op1;
5905 TCGv_i32 tcg_op2;
5906 TCGv_i32 tcg_res;
5907 TCGv_ptr fpst;
5909 tcg_res = tcg_temp_new_i32();
5910 fpst = get_fpstatus_ptr(false);
5911 tcg_op1 = read_fp_sreg(s, rn);
5912 tcg_op2 = read_fp_sreg(s, rm);
5914 switch (opcode) {
5915 case 0x0: /* FMUL */
5916 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
5917 break;
5918 case 0x1: /* FDIV */
5919 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
5920 break;
5921 case 0x2: /* FADD */
5922 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
5923 break;
5924 case 0x3: /* FSUB */
5925 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
5926 break;
5927 case 0x4: /* FMAX */
5928 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
5929 break;
5930 case 0x5: /* FMIN */
5931 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
5932 break;
5933 case 0x6: /* FMAXNM */
5934 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
5935 break;
5936 case 0x7: /* FMINNM */
5937 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
5938 break;
5939 case 0x8: /* FNMUL */
5940 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
5941 gen_helper_vfp_negs(tcg_res, tcg_res);
5942 break;
5945 write_fp_sreg(s, rd, tcg_res);
5947 tcg_temp_free_ptr(fpst);
5948 tcg_temp_free_i32(tcg_op1);
5949 tcg_temp_free_i32(tcg_op2);
5950 tcg_temp_free_i32(tcg_res);
5953 /* Floating-point data-processing (2 source) - double precision */
5954 static void handle_fp_2src_double(DisasContext *s, int opcode,
5955 int rd, int rn, int rm)
5957 TCGv_i64 tcg_op1;
5958 TCGv_i64 tcg_op2;
5959 TCGv_i64 tcg_res;
5960 TCGv_ptr fpst;
5962 tcg_res = tcg_temp_new_i64();
5963 fpst = get_fpstatus_ptr(false);
5964 tcg_op1 = read_fp_dreg(s, rn);
5965 tcg_op2 = read_fp_dreg(s, rm);
5967 switch (opcode) {
5968 case 0x0: /* FMUL */
5969 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
5970 break;
5971 case 0x1: /* FDIV */
5972 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
5973 break;
5974 case 0x2: /* FADD */
5975 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
5976 break;
5977 case 0x3: /* FSUB */
5978 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
5979 break;
5980 case 0x4: /* FMAX */
5981 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
5982 break;
5983 case 0x5: /* FMIN */
5984 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
5985 break;
5986 case 0x6: /* FMAXNM */
5987 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
5988 break;
5989 case 0x7: /* FMINNM */
5990 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
5991 break;
5992 case 0x8: /* FNMUL */
5993 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
5994 gen_helper_vfp_negd(tcg_res, tcg_res);
5995 break;
5998 write_fp_dreg(s, rd, tcg_res);
6000 tcg_temp_free_ptr(fpst);
6001 tcg_temp_free_i64(tcg_op1);
6002 tcg_temp_free_i64(tcg_op2);
6003 tcg_temp_free_i64(tcg_res);
6006 /* Floating-point data-processing (2 source) - half precision */
6007 static void handle_fp_2src_half(DisasContext *s, int opcode,
6008 int rd, int rn, int rm)
6010 TCGv_i32 tcg_op1;
6011 TCGv_i32 tcg_op2;
6012 TCGv_i32 tcg_res;
6013 TCGv_ptr fpst;
6015 tcg_res = tcg_temp_new_i32();
6016 fpst = get_fpstatus_ptr(true);
6017 tcg_op1 = read_fp_hreg(s, rn);
6018 tcg_op2 = read_fp_hreg(s, rm);
6020 switch (opcode) {
6021 case 0x0: /* FMUL */
6022 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6023 break;
6024 case 0x1: /* FDIV */
6025 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6026 break;
6027 case 0x2: /* FADD */
6028 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6029 break;
6030 case 0x3: /* FSUB */
6031 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6032 break;
6033 case 0x4: /* FMAX */
6034 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6035 break;
6036 case 0x5: /* FMIN */
6037 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6038 break;
6039 case 0x6: /* FMAXNM */
6040 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6041 break;
6042 case 0x7: /* FMINNM */
6043 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6044 break;
6045 case 0x8: /* FNMUL */
6046 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6047 tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6048 break;
6049 default:
6050 g_assert_not_reached();
6053 write_fp_sreg(s, rd, tcg_res);
6055 tcg_temp_free_ptr(fpst);
6056 tcg_temp_free_i32(tcg_op1);
6057 tcg_temp_free_i32(tcg_op2);
6058 tcg_temp_free_i32(tcg_res);
6061 /* Floating point data-processing (2 source)
6062 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
6063 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6064 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | opcode | 1 0 | Rn | Rd |
6065 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6067 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6069 int mos = extract32(insn, 29, 3);
6070 int type = extract32(insn, 22, 2);
6071 int rd = extract32(insn, 0, 5);
6072 int rn = extract32(insn, 5, 5);
6073 int rm = extract32(insn, 16, 5);
6074 int opcode = extract32(insn, 12, 4);
6076 if (opcode > 8 || mos) {
6077 unallocated_encoding(s);
6078 return;
6081 switch (type) {
6082 case 0:
6083 if (!fp_access_check(s)) {
6084 return;
6086 handle_fp_2src_single(s, opcode, rd, rn, rm);
6087 break;
6088 case 1:
6089 if (!fp_access_check(s)) {
6090 return;
6092 handle_fp_2src_double(s, opcode, rd, rn, rm);
6093 break;
6094 case 3:
6095 if (!dc_isar_feature(aa64_fp16, s)) {
6096 unallocated_encoding(s);
6097 return;
6099 if (!fp_access_check(s)) {
6100 return;
6102 handle_fp_2src_half(s, opcode, rd, rn, rm);
6103 break;
6104 default:
6105 unallocated_encoding(s);
6109 /* Floating-point data-processing (3 source) - single precision */
6110 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6111 int rd, int rn, int rm, int ra)
6113 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6114 TCGv_i32 tcg_res = tcg_temp_new_i32();
6115 TCGv_ptr fpst = get_fpstatus_ptr(false);
6117 tcg_op1 = read_fp_sreg(s, rn);
6118 tcg_op2 = read_fp_sreg(s, rm);
6119 tcg_op3 = read_fp_sreg(s, ra);
6121 /* These are fused multiply-add, and must be done as one
6122 * floating point operation with no rounding between the
6123 * multiplication and addition steps.
6124 * NB that doing the negations here as separate steps is
6125 * correct : an input NaN should come out with its sign bit
6126 * flipped if it is a negated-input.
6128 if (o1 == true) {
6129 gen_helper_vfp_negs(tcg_op3, tcg_op3);
6132 if (o0 != o1) {
6133 gen_helper_vfp_negs(tcg_op1, tcg_op1);
6136 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6138 write_fp_sreg(s, rd, tcg_res);
6140 tcg_temp_free_ptr(fpst);
6141 tcg_temp_free_i32(tcg_op1);
6142 tcg_temp_free_i32(tcg_op2);
6143 tcg_temp_free_i32(tcg_op3);
6144 tcg_temp_free_i32(tcg_res);
6147 /* Floating-point data-processing (3 source) - double precision */
6148 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6149 int rd, int rn, int rm, int ra)
6151 TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6152 TCGv_i64 tcg_res = tcg_temp_new_i64();
6153 TCGv_ptr fpst = get_fpstatus_ptr(false);
6155 tcg_op1 = read_fp_dreg(s, rn);
6156 tcg_op2 = read_fp_dreg(s, rm);
6157 tcg_op3 = read_fp_dreg(s, ra);
6159 /* These are fused multiply-add, and must be done as one
6160 * floating point operation with no rounding between the
6161 * multiplication and addition steps.
6162 * NB that doing the negations here as separate steps is
6163 * correct : an input NaN should come out with its sign bit
6164 * flipped if it is a negated-input.
6166 if (o1 == true) {
6167 gen_helper_vfp_negd(tcg_op3, tcg_op3);
6170 if (o0 != o1) {
6171 gen_helper_vfp_negd(tcg_op1, tcg_op1);
6174 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6176 write_fp_dreg(s, rd, tcg_res);
6178 tcg_temp_free_ptr(fpst);
6179 tcg_temp_free_i64(tcg_op1);
6180 tcg_temp_free_i64(tcg_op2);
6181 tcg_temp_free_i64(tcg_op3);
6182 tcg_temp_free_i64(tcg_res);
6185 /* Floating-point data-processing (3 source) - half precision */
6186 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6187 int rd, int rn, int rm, int ra)
6189 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6190 TCGv_i32 tcg_res = tcg_temp_new_i32();
6191 TCGv_ptr fpst = get_fpstatus_ptr(true);
6193 tcg_op1 = read_fp_hreg(s, rn);
6194 tcg_op2 = read_fp_hreg(s, rm);
6195 tcg_op3 = read_fp_hreg(s, ra);
6197 /* These are fused multiply-add, and must be done as one
6198 * floating point operation with no rounding between the
6199 * multiplication and addition steps.
6200 * NB that doing the negations here as separate steps is
6201 * correct : an input NaN should come out with its sign bit
6202 * flipped if it is a negated-input.
6204 if (o1 == true) {
6205 tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6208 if (o0 != o1) {
6209 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6212 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6214 write_fp_sreg(s, rd, tcg_res);
6216 tcg_temp_free_ptr(fpst);
6217 tcg_temp_free_i32(tcg_op1);
6218 tcg_temp_free_i32(tcg_op2);
6219 tcg_temp_free_i32(tcg_op3);
6220 tcg_temp_free_i32(tcg_res);
6223 /* Floating point data-processing (3 source)
6224 * 31 30 29 28 24 23 22 21 20 16 15 14 10 9 5 4 0
6225 * +---+---+---+-----------+------+----+------+----+------+------+------+
6226 * | M | 0 | S | 1 1 1 1 1 | type | o1 | Rm | o0 | Ra | Rn | Rd |
6227 * +---+---+---+-----------+------+----+------+----+------+------+------+
6229 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6231 int mos = extract32(insn, 29, 3);
6232 int type = extract32(insn, 22, 2);
6233 int rd = extract32(insn, 0, 5);
6234 int rn = extract32(insn, 5, 5);
6235 int ra = extract32(insn, 10, 5);
6236 int rm = extract32(insn, 16, 5);
6237 bool o0 = extract32(insn, 15, 1);
6238 bool o1 = extract32(insn, 21, 1);
6240 if (mos) {
6241 unallocated_encoding(s);
6242 return;
6245 switch (type) {
6246 case 0:
6247 if (!fp_access_check(s)) {
6248 return;
6250 handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6251 break;
6252 case 1:
6253 if (!fp_access_check(s)) {
6254 return;
6256 handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6257 break;
6258 case 3:
6259 if (!dc_isar_feature(aa64_fp16, s)) {
6260 unallocated_encoding(s);
6261 return;
6263 if (!fp_access_check(s)) {
6264 return;
6266 handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6267 break;
6268 default:
6269 unallocated_encoding(s);
6273 /* The imm8 encodes the sign bit, enough bits to represent an exponent in
6274 * the range 01....1xx to 10....0xx, and the most significant 4 bits of
6275 * the mantissa; see VFPExpandImm() in the v8 ARM ARM.
6277 uint64_t vfp_expand_imm(int size, uint8_t imm8)
6279 uint64_t imm;
6281 switch (size) {
6282 case MO_64:
6283 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
6284 (extract32(imm8, 6, 1) ? 0x3fc0 : 0x4000) |
6285 extract32(imm8, 0, 6);
6286 imm <<= 48;
6287 break;
6288 case MO_32:
6289 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
6290 (extract32(imm8, 6, 1) ? 0x3e00 : 0x4000) |
6291 (extract32(imm8, 0, 6) << 3);
6292 imm <<= 16;
6293 break;
6294 case MO_16:
6295 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
6296 (extract32(imm8, 6, 1) ? 0x3000 : 0x4000) |
6297 (extract32(imm8, 0, 6) << 6);
6298 break;
6299 default:
6300 g_assert_not_reached();
6302 return imm;
6305 /* Floating point immediate
6306 * 31 30 29 28 24 23 22 21 20 13 12 10 9 5 4 0
6307 * +---+---+---+-----------+------+---+------------+-------+------+------+
6308 * | M | 0 | S | 1 1 1 1 0 | type | 1 | imm8 | 1 0 0 | imm5 | Rd |
6309 * +---+---+---+-----------+------+---+------------+-------+------+------+
6311 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6313 int rd = extract32(insn, 0, 5);
6314 int imm5 = extract32(insn, 5, 5);
6315 int imm8 = extract32(insn, 13, 8);
6316 int type = extract32(insn, 22, 2);
6317 int mos = extract32(insn, 29, 3);
6318 uint64_t imm;
6319 TCGv_i64 tcg_res;
6320 TCGMemOp sz;
6322 if (mos || imm5) {
6323 unallocated_encoding(s);
6324 return;
6327 switch (type) {
6328 case 0:
6329 sz = MO_32;
6330 break;
6331 case 1:
6332 sz = MO_64;
6333 break;
6334 case 3:
6335 sz = MO_16;
6336 if (dc_isar_feature(aa64_fp16, s)) {
6337 break;
6339 /* fallthru */
6340 default:
6341 unallocated_encoding(s);
6342 return;
6345 if (!fp_access_check(s)) {
6346 return;
6349 imm = vfp_expand_imm(sz, imm8);
6351 tcg_res = tcg_const_i64(imm);
6352 write_fp_dreg(s, rd, tcg_res);
6353 tcg_temp_free_i64(tcg_res);
6356 /* Handle floating point <=> fixed point conversions. Note that we can
6357 * also deal with fp <=> integer conversions as a special case (scale == 64)
6358 * OPTME: consider handling that special case specially or at least skipping
6359 * the call to scalbn in the helpers for zero shifts.
6361 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6362 bool itof, int rmode, int scale, int sf, int type)
6364 bool is_signed = !(opcode & 1);
6365 TCGv_ptr tcg_fpstatus;
6366 TCGv_i32 tcg_shift, tcg_single;
6367 TCGv_i64 tcg_double;
6369 tcg_fpstatus = get_fpstatus_ptr(type == 3);
6371 tcg_shift = tcg_const_i32(64 - scale);
6373 if (itof) {
6374 TCGv_i64 tcg_int = cpu_reg(s, rn);
6375 if (!sf) {
6376 TCGv_i64 tcg_extend = new_tmp_a64(s);
6378 if (is_signed) {
6379 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6380 } else {
6381 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6384 tcg_int = tcg_extend;
6387 switch (type) {
6388 case 1: /* float64 */
6389 tcg_double = tcg_temp_new_i64();
6390 if (is_signed) {
6391 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6392 tcg_shift, tcg_fpstatus);
6393 } else {
6394 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6395 tcg_shift, tcg_fpstatus);
6397 write_fp_dreg(s, rd, tcg_double);
6398 tcg_temp_free_i64(tcg_double);
6399 break;
6401 case 0: /* float32 */
6402 tcg_single = tcg_temp_new_i32();
6403 if (is_signed) {
6404 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6405 tcg_shift, tcg_fpstatus);
6406 } else {
6407 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6408 tcg_shift, tcg_fpstatus);
6410 write_fp_sreg(s, rd, tcg_single);
6411 tcg_temp_free_i32(tcg_single);
6412 break;
6414 case 3: /* float16 */
6415 tcg_single = tcg_temp_new_i32();
6416 if (is_signed) {
6417 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6418 tcg_shift, tcg_fpstatus);
6419 } else {
6420 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6421 tcg_shift, tcg_fpstatus);
6423 write_fp_sreg(s, rd, tcg_single);
6424 tcg_temp_free_i32(tcg_single);
6425 break;
6427 default:
6428 g_assert_not_reached();
6430 } else {
6431 TCGv_i64 tcg_int = cpu_reg(s, rd);
6432 TCGv_i32 tcg_rmode;
6434 if (extract32(opcode, 2, 1)) {
6435 /* There are too many rounding modes to all fit into rmode,
6436 * so FCVTA[US] is a special case.
6438 rmode = FPROUNDING_TIEAWAY;
6441 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
6443 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
6445 switch (type) {
6446 case 1: /* float64 */
6447 tcg_double = read_fp_dreg(s, rn);
6448 if (is_signed) {
6449 if (!sf) {
6450 gen_helper_vfp_tosld(tcg_int, tcg_double,
6451 tcg_shift, tcg_fpstatus);
6452 } else {
6453 gen_helper_vfp_tosqd(tcg_int, tcg_double,
6454 tcg_shift, tcg_fpstatus);
6456 } else {
6457 if (!sf) {
6458 gen_helper_vfp_tould(tcg_int, tcg_double,
6459 tcg_shift, tcg_fpstatus);
6460 } else {
6461 gen_helper_vfp_touqd(tcg_int, tcg_double,
6462 tcg_shift, tcg_fpstatus);
6465 if (!sf) {
6466 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6468 tcg_temp_free_i64(tcg_double);
6469 break;
6471 case 0: /* float32 */
6472 tcg_single = read_fp_sreg(s, rn);
6473 if (sf) {
6474 if (is_signed) {
6475 gen_helper_vfp_tosqs(tcg_int, tcg_single,
6476 tcg_shift, tcg_fpstatus);
6477 } else {
6478 gen_helper_vfp_touqs(tcg_int, tcg_single,
6479 tcg_shift, tcg_fpstatus);
6481 } else {
6482 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6483 if (is_signed) {
6484 gen_helper_vfp_tosls(tcg_dest, tcg_single,
6485 tcg_shift, tcg_fpstatus);
6486 } else {
6487 gen_helper_vfp_touls(tcg_dest, tcg_single,
6488 tcg_shift, tcg_fpstatus);
6490 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6491 tcg_temp_free_i32(tcg_dest);
6493 tcg_temp_free_i32(tcg_single);
6494 break;
6496 case 3: /* float16 */
6497 tcg_single = read_fp_sreg(s, rn);
6498 if (sf) {
6499 if (is_signed) {
6500 gen_helper_vfp_tosqh(tcg_int, tcg_single,
6501 tcg_shift, tcg_fpstatus);
6502 } else {
6503 gen_helper_vfp_touqh(tcg_int, tcg_single,
6504 tcg_shift, tcg_fpstatus);
6506 } else {
6507 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6508 if (is_signed) {
6509 gen_helper_vfp_toslh(tcg_dest, tcg_single,
6510 tcg_shift, tcg_fpstatus);
6511 } else {
6512 gen_helper_vfp_toulh(tcg_dest, tcg_single,
6513 tcg_shift, tcg_fpstatus);
6515 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6516 tcg_temp_free_i32(tcg_dest);
6518 tcg_temp_free_i32(tcg_single);
6519 break;
6521 default:
6522 g_assert_not_reached();
6525 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
6526 tcg_temp_free_i32(tcg_rmode);
6529 tcg_temp_free_ptr(tcg_fpstatus);
6530 tcg_temp_free_i32(tcg_shift);
6533 /* Floating point <-> fixed point conversions
6534 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
6535 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6536 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale | Rn | Rd |
6537 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6539 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
6541 int rd = extract32(insn, 0, 5);
6542 int rn = extract32(insn, 5, 5);
6543 int scale = extract32(insn, 10, 6);
6544 int opcode = extract32(insn, 16, 3);
6545 int rmode = extract32(insn, 19, 2);
6546 int type = extract32(insn, 22, 2);
6547 bool sbit = extract32(insn, 29, 1);
6548 bool sf = extract32(insn, 31, 1);
6549 bool itof;
6551 if (sbit || (!sf && scale < 32)) {
6552 unallocated_encoding(s);
6553 return;
6556 switch (type) {
6557 case 0: /* float32 */
6558 case 1: /* float64 */
6559 break;
6560 case 3: /* float16 */
6561 if (dc_isar_feature(aa64_fp16, s)) {
6562 break;
6564 /* fallthru */
6565 default:
6566 unallocated_encoding(s);
6567 return;
6570 switch ((rmode << 3) | opcode) {
6571 case 0x2: /* SCVTF */
6572 case 0x3: /* UCVTF */
6573 itof = true;
6574 break;
6575 case 0x18: /* FCVTZS */
6576 case 0x19: /* FCVTZU */
6577 itof = false;
6578 break;
6579 default:
6580 unallocated_encoding(s);
6581 return;
6584 if (!fp_access_check(s)) {
6585 return;
6588 handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
6591 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
6593 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
6594 * without conversion.
6597 if (itof) {
6598 TCGv_i64 tcg_rn = cpu_reg(s, rn);
6599 TCGv_i64 tmp;
6601 switch (type) {
6602 case 0:
6603 /* 32 bit */
6604 tmp = tcg_temp_new_i64();
6605 tcg_gen_ext32u_i64(tmp, tcg_rn);
6606 write_fp_dreg(s, rd, tmp);
6607 tcg_temp_free_i64(tmp);
6608 break;
6609 case 1:
6610 /* 64 bit */
6611 write_fp_dreg(s, rd, tcg_rn);
6612 break;
6613 case 2:
6614 /* 64 bit to top half. */
6615 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
6616 clear_vec_high(s, true, rd);
6617 break;
6618 case 3:
6619 /* 16 bit */
6620 tmp = tcg_temp_new_i64();
6621 tcg_gen_ext16u_i64(tmp, tcg_rn);
6622 write_fp_dreg(s, rd, tmp);
6623 tcg_temp_free_i64(tmp);
6624 break;
6625 default:
6626 g_assert_not_reached();
6628 } else {
6629 TCGv_i64 tcg_rd = cpu_reg(s, rd);
6631 switch (type) {
6632 case 0:
6633 /* 32 bit */
6634 tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
6635 break;
6636 case 1:
6637 /* 64 bit */
6638 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
6639 break;
6640 case 2:
6641 /* 64 bits from top half */
6642 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
6643 break;
6644 case 3:
6645 /* 16 bit */
6646 tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
6647 break;
6648 default:
6649 g_assert_not_reached();
6654 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
6656 TCGv_i64 t = read_fp_dreg(s, rn);
6657 TCGv_ptr fpstatus = get_fpstatus_ptr(false);
6659 gen_helper_fjcvtzs(t, t, fpstatus);
6661 tcg_temp_free_ptr(fpstatus);
6663 tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
6664 tcg_gen_extrh_i64_i32(cpu_ZF, t);
6665 tcg_gen_movi_i32(cpu_CF, 0);
6666 tcg_gen_movi_i32(cpu_NF, 0);
6667 tcg_gen_movi_i32(cpu_VF, 0);
6669 tcg_temp_free_i64(t);
6672 /* Floating point <-> integer conversions
6673 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
6674 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
6675 * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
6676 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
6678 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
6680 int rd = extract32(insn, 0, 5);
6681 int rn = extract32(insn, 5, 5);
6682 int opcode = extract32(insn, 16, 3);
6683 int rmode = extract32(insn, 19, 2);
6684 int type = extract32(insn, 22, 2);
6685 bool sbit = extract32(insn, 29, 1);
6686 bool sf = extract32(insn, 31, 1);
6687 bool itof = false;
6689 if (sbit) {
6690 goto do_unallocated;
6693 switch (opcode) {
6694 case 2: /* SCVTF */
6695 case 3: /* UCVTF */
6696 itof = true;
6697 /* fallthru */
6698 case 4: /* FCVTAS */
6699 case 5: /* FCVTAU */
6700 if (rmode != 0) {
6701 goto do_unallocated;
6703 /* fallthru */
6704 case 0: /* FCVT[NPMZ]S */
6705 case 1: /* FCVT[NPMZ]U */
6706 switch (type) {
6707 case 0: /* float32 */
6708 case 1: /* float64 */
6709 break;
6710 case 3: /* float16 */
6711 if (!dc_isar_feature(aa64_fp16, s)) {
6712 goto do_unallocated;
6714 break;
6715 default:
6716 goto do_unallocated;
6718 if (!fp_access_check(s)) {
6719 return;
6721 handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
6722 break;
6724 default:
6725 switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
6726 case 0b01100110: /* FMOV half <-> 32-bit int */
6727 case 0b01100111:
6728 case 0b11100110: /* FMOV half <-> 64-bit int */
6729 case 0b11100111:
6730 if (!dc_isar_feature(aa64_fp16, s)) {
6731 goto do_unallocated;
6733 /* fallthru */
6734 case 0b00000110: /* FMOV 32-bit */
6735 case 0b00000111:
6736 case 0b10100110: /* FMOV 64-bit */
6737 case 0b10100111:
6738 case 0b11001110: /* FMOV top half of 128-bit */
6739 case 0b11001111:
6740 if (!fp_access_check(s)) {
6741 return;
6743 itof = opcode & 1;
6744 handle_fmov(s, rd, rn, type, itof);
6745 break;
6747 case 0b00111110: /* FJCVTZS */
6748 if (!dc_isar_feature(aa64_jscvt, s)) {
6749 goto do_unallocated;
6750 } else if (fp_access_check(s)) {
6751 handle_fjcvtzs(s, rd, rn);
6753 break;
6755 default:
6756 do_unallocated:
6757 unallocated_encoding(s);
6758 return;
6760 break;
6764 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
6765 * 31 30 29 28 25 24 0
6766 * +---+---+---+---------+-----------------------------+
6767 * | | 0 | | 1 1 1 1 | |
6768 * +---+---+---+---------+-----------------------------+
6770 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
6772 if (extract32(insn, 24, 1)) {
6773 /* Floating point data-processing (3 source) */
6774 disas_fp_3src(s, insn);
6775 } else if (extract32(insn, 21, 1) == 0) {
6776 /* Floating point to fixed point conversions */
6777 disas_fp_fixed_conv(s, insn);
6778 } else {
6779 switch (extract32(insn, 10, 2)) {
6780 case 1:
6781 /* Floating point conditional compare */
6782 disas_fp_ccomp(s, insn);
6783 break;
6784 case 2:
6785 /* Floating point data-processing (2 source) */
6786 disas_fp_2src(s, insn);
6787 break;
6788 case 3:
6789 /* Floating point conditional select */
6790 disas_fp_csel(s, insn);
6791 break;
6792 case 0:
6793 switch (ctz32(extract32(insn, 12, 4))) {
6794 case 0: /* [15:12] == xxx1 */
6795 /* Floating point immediate */
6796 disas_fp_imm(s, insn);
6797 break;
6798 case 1: /* [15:12] == xx10 */
6799 /* Floating point compare */
6800 disas_fp_compare(s, insn);
6801 break;
6802 case 2: /* [15:12] == x100 */
6803 /* Floating point data-processing (1 source) */
6804 disas_fp_1src(s, insn);
6805 break;
6806 case 3: /* [15:12] == 1000 */
6807 unallocated_encoding(s);
6808 break;
6809 default: /* [15:12] == 0000 */
6810 /* Floating point <-> integer conversions */
6811 disas_fp_int_conv(s, insn);
6812 break;
6814 break;
6819 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
6820 int pos)
6822 /* Extract 64 bits from the middle of two concatenated 64 bit
6823 * vector register slices left:right. The extracted bits start
6824 * at 'pos' bits into the right (least significant) side.
6825 * We return the result in tcg_right, and guarantee not to
6826 * trash tcg_left.
6828 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
6829 assert(pos > 0 && pos < 64);
6831 tcg_gen_shri_i64(tcg_right, tcg_right, pos);
6832 tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
6833 tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
6835 tcg_temp_free_i64(tcg_tmp);
6838 /* EXT
6839 * 31 30 29 24 23 22 21 20 16 15 14 11 10 9 5 4 0
6840 * +---+---+-------------+-----+---+------+---+------+---+------+------+
6841 * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 | Rm | 0 | imm4 | 0 | Rn | Rd |
6842 * +---+---+-------------+-----+---+------+---+------+---+------+------+
6844 static void disas_simd_ext(DisasContext *s, uint32_t insn)
6846 int is_q = extract32(insn, 30, 1);
6847 int op2 = extract32(insn, 22, 2);
6848 int imm4 = extract32(insn, 11, 4);
6849 int rm = extract32(insn, 16, 5);
6850 int rn = extract32(insn, 5, 5);
6851 int rd = extract32(insn, 0, 5);
6852 int pos = imm4 << 3;
6853 TCGv_i64 tcg_resl, tcg_resh;
6855 if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
6856 unallocated_encoding(s);
6857 return;
6860 if (!fp_access_check(s)) {
6861 return;
6864 tcg_resh = tcg_temp_new_i64();
6865 tcg_resl = tcg_temp_new_i64();
6867 /* Vd gets bits starting at pos bits into Vm:Vn. This is
6868 * either extracting 128 bits from a 128:128 concatenation, or
6869 * extracting 64 bits from a 64:64 concatenation.
6871 if (!is_q) {
6872 read_vec_element(s, tcg_resl, rn, 0, MO_64);
6873 if (pos != 0) {
6874 read_vec_element(s, tcg_resh, rm, 0, MO_64);
6875 do_ext64(s, tcg_resh, tcg_resl, pos);
6877 tcg_gen_movi_i64(tcg_resh, 0);
6878 } else {
6879 TCGv_i64 tcg_hh;
6880 typedef struct {
6881 int reg;
6882 int elt;
6883 } EltPosns;
6884 EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
6885 EltPosns *elt = eltposns;
6887 if (pos >= 64) {
6888 elt++;
6889 pos -= 64;
6892 read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
6893 elt++;
6894 read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
6895 elt++;
6896 if (pos != 0) {
6897 do_ext64(s, tcg_resh, tcg_resl, pos);
6898 tcg_hh = tcg_temp_new_i64();
6899 read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
6900 do_ext64(s, tcg_hh, tcg_resh, pos);
6901 tcg_temp_free_i64(tcg_hh);
6905 write_vec_element(s, tcg_resl, rd, 0, MO_64);
6906 tcg_temp_free_i64(tcg_resl);
6907 write_vec_element(s, tcg_resh, rd, 1, MO_64);
6908 tcg_temp_free_i64(tcg_resh);
6911 /* TBL/TBX
6912 * 31 30 29 24 23 22 21 20 16 15 14 13 12 11 10 9 5 4 0
6913 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
6914 * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 | Rm | 0 | len | op | 0 0 | Rn | Rd |
6915 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
6917 static void disas_simd_tb(DisasContext *s, uint32_t insn)
6919 int op2 = extract32(insn, 22, 2);
6920 int is_q = extract32(insn, 30, 1);
6921 int rm = extract32(insn, 16, 5);
6922 int rn = extract32(insn, 5, 5);
6923 int rd = extract32(insn, 0, 5);
6924 int is_tblx = extract32(insn, 12, 1);
6925 int len = extract32(insn, 13, 2);
6926 TCGv_i64 tcg_resl, tcg_resh, tcg_idx;
6927 TCGv_i32 tcg_regno, tcg_numregs;
6929 if (op2 != 0) {
6930 unallocated_encoding(s);
6931 return;
6934 if (!fp_access_check(s)) {
6935 return;
6938 /* This does a table lookup: for every byte element in the input
6939 * we index into a table formed from up to four vector registers,
6940 * and then the output is the result of the lookups. Our helper
6941 * function does the lookup operation for a single 64 bit part of
6942 * the input.
6944 tcg_resl = tcg_temp_new_i64();
6945 tcg_resh = tcg_temp_new_i64();
6947 if (is_tblx) {
6948 read_vec_element(s, tcg_resl, rd, 0, MO_64);
6949 } else {
6950 tcg_gen_movi_i64(tcg_resl, 0);
6952 if (is_tblx && is_q) {
6953 read_vec_element(s, tcg_resh, rd, 1, MO_64);
6954 } else {
6955 tcg_gen_movi_i64(tcg_resh, 0);
6958 tcg_idx = tcg_temp_new_i64();
6959 tcg_regno = tcg_const_i32(rn);
6960 tcg_numregs = tcg_const_i32(len + 1);
6961 read_vec_element(s, tcg_idx, rm, 0, MO_64);
6962 gen_helper_simd_tbl(tcg_resl, cpu_env, tcg_resl, tcg_idx,
6963 tcg_regno, tcg_numregs);
6964 if (is_q) {
6965 read_vec_element(s, tcg_idx, rm, 1, MO_64);
6966 gen_helper_simd_tbl(tcg_resh, cpu_env, tcg_resh, tcg_idx,
6967 tcg_regno, tcg_numregs);
6969 tcg_temp_free_i64(tcg_idx);
6970 tcg_temp_free_i32(tcg_regno);
6971 tcg_temp_free_i32(tcg_numregs);
6973 write_vec_element(s, tcg_resl, rd, 0, MO_64);
6974 tcg_temp_free_i64(tcg_resl);
6975 write_vec_element(s, tcg_resh, rd, 1, MO_64);
6976 tcg_temp_free_i64(tcg_resh);
6979 /* ZIP/UZP/TRN
6980 * 31 30 29 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
6981 * +---+---+-------------+------+---+------+---+------------------+------+
6982 * | 0 | Q | 0 0 1 1 1 0 | size | 0 | Rm | 0 | opc | 1 0 | Rn | Rd |
6983 * +---+---+-------------+------+---+------+---+------------------+------+
6985 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
6987 int rd = extract32(insn, 0, 5);
6988 int rn = extract32(insn, 5, 5);
6989 int rm = extract32(insn, 16, 5);
6990 int size = extract32(insn, 22, 2);
6991 /* opc field bits [1:0] indicate ZIP/UZP/TRN;
6992 * bit 2 indicates 1 vs 2 variant of the insn.
6994 int opcode = extract32(insn, 12, 2);
6995 bool part = extract32(insn, 14, 1);
6996 bool is_q = extract32(insn, 30, 1);
6997 int esize = 8 << size;
6998 int i, ofs;
6999 int datasize = is_q ? 128 : 64;
7000 int elements = datasize / esize;
7001 TCGv_i64 tcg_res, tcg_resl, tcg_resh;
7003 if (opcode == 0 || (size == 3 && !is_q)) {
7004 unallocated_encoding(s);
7005 return;
7008 if (!fp_access_check(s)) {
7009 return;
7012 tcg_resl = tcg_const_i64(0);
7013 tcg_resh = tcg_const_i64(0);
7014 tcg_res = tcg_temp_new_i64();
7016 for (i = 0; i < elements; i++) {
7017 switch (opcode) {
7018 case 1: /* UZP1/2 */
7020 int midpoint = elements / 2;
7021 if (i < midpoint) {
7022 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
7023 } else {
7024 read_vec_element(s, tcg_res, rm,
7025 2 * (i - midpoint) + part, size);
7027 break;
7029 case 2: /* TRN1/2 */
7030 if (i & 1) {
7031 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
7032 } else {
7033 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
7035 break;
7036 case 3: /* ZIP1/2 */
7038 int base = part * elements / 2;
7039 if (i & 1) {
7040 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
7041 } else {
7042 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
7044 break;
7046 default:
7047 g_assert_not_reached();
7050 ofs = i * esize;
7051 if (ofs < 64) {
7052 tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
7053 tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
7054 } else {
7055 tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
7056 tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
7060 tcg_temp_free_i64(tcg_res);
7062 write_vec_element(s, tcg_resl, rd, 0, MO_64);
7063 tcg_temp_free_i64(tcg_resl);
7064 write_vec_element(s, tcg_resh, rd, 1, MO_64);
7065 tcg_temp_free_i64(tcg_resh);
7069 * do_reduction_op helper
7071 * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7072 * important for correct NaN propagation that we do these
7073 * operations in exactly the order specified by the pseudocode.
7075 * This is a recursive function, TCG temps should be freed by the
7076 * calling function once it is done with the values.
7078 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7079 int esize, int size, int vmap, TCGv_ptr fpst)
7081 if (esize == size) {
7082 int element;
7083 TCGMemOp msize = esize == 16 ? MO_16 : MO_32;
7084 TCGv_i32 tcg_elem;
7086 /* We should have one register left here */
7087 assert(ctpop8(vmap) == 1);
7088 element = ctz32(vmap);
7089 assert(element < 8);
7091 tcg_elem = tcg_temp_new_i32();
7092 read_vec_element_i32(s, tcg_elem, rn, element, msize);
7093 return tcg_elem;
7094 } else {
7095 int bits = size / 2;
7096 int shift = ctpop8(vmap) / 2;
7097 int vmap_lo = (vmap >> shift) & vmap;
7098 int vmap_hi = (vmap & ~vmap_lo);
7099 TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7101 tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7102 tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7103 tcg_res = tcg_temp_new_i32();
7105 switch (fpopcode) {
7106 case 0x0c: /* fmaxnmv half-precision */
7107 gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7108 break;
7109 case 0x0f: /* fmaxv half-precision */
7110 gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7111 break;
7112 case 0x1c: /* fminnmv half-precision */
7113 gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7114 break;
7115 case 0x1f: /* fminv half-precision */
7116 gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7117 break;
7118 case 0x2c: /* fmaxnmv */
7119 gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7120 break;
7121 case 0x2f: /* fmaxv */
7122 gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7123 break;
7124 case 0x3c: /* fminnmv */
7125 gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7126 break;
7127 case 0x3f: /* fminv */
7128 gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7129 break;
7130 default:
7131 g_assert_not_reached();
7134 tcg_temp_free_i32(tcg_hi);
7135 tcg_temp_free_i32(tcg_lo);
7136 return tcg_res;
7140 /* AdvSIMD across lanes
7141 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
7142 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7143 * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
7144 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7146 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7148 int rd = extract32(insn, 0, 5);
7149 int rn = extract32(insn, 5, 5);
7150 int size = extract32(insn, 22, 2);
7151 int opcode = extract32(insn, 12, 5);
7152 bool is_q = extract32(insn, 30, 1);
7153 bool is_u = extract32(insn, 29, 1);
7154 bool is_fp = false;
7155 bool is_min = false;
7156 int esize;
7157 int elements;
7158 int i;
7159 TCGv_i64 tcg_res, tcg_elt;
7161 switch (opcode) {
7162 case 0x1b: /* ADDV */
7163 if (is_u) {
7164 unallocated_encoding(s);
7165 return;
7167 /* fall through */
7168 case 0x3: /* SADDLV, UADDLV */
7169 case 0xa: /* SMAXV, UMAXV */
7170 case 0x1a: /* SMINV, UMINV */
7171 if (size == 3 || (size == 2 && !is_q)) {
7172 unallocated_encoding(s);
7173 return;
7175 break;
7176 case 0xc: /* FMAXNMV, FMINNMV */
7177 case 0xf: /* FMAXV, FMINV */
7178 /* Bit 1 of size field encodes min vs max and the actual size
7179 * depends on the encoding of the U bit. If not set (and FP16
7180 * enabled) then we do half-precision float instead of single
7181 * precision.
7183 is_min = extract32(size, 1, 1);
7184 is_fp = true;
7185 if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7186 size = 1;
7187 } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7188 unallocated_encoding(s);
7189 return;
7190 } else {
7191 size = 2;
7193 break;
7194 default:
7195 unallocated_encoding(s);
7196 return;
7199 if (!fp_access_check(s)) {
7200 return;
7203 esize = 8 << size;
7204 elements = (is_q ? 128 : 64) / esize;
7206 tcg_res = tcg_temp_new_i64();
7207 tcg_elt = tcg_temp_new_i64();
7209 /* These instructions operate across all lanes of a vector
7210 * to produce a single result. We can guarantee that a 64
7211 * bit intermediate is sufficient:
7212 * + for [US]ADDLV the maximum element size is 32 bits, and
7213 * the result type is 64 bits
7214 * + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7215 * same as the element size, which is 32 bits at most
7216 * For the integer operations we can choose to work at 64
7217 * or 32 bits and truncate at the end; for simplicity
7218 * we use 64 bits always. The floating point
7219 * ops do require 32 bit intermediates, though.
7221 if (!is_fp) {
7222 read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7224 for (i = 1; i < elements; i++) {
7225 read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7227 switch (opcode) {
7228 case 0x03: /* SADDLV / UADDLV */
7229 case 0x1b: /* ADDV */
7230 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7231 break;
7232 case 0x0a: /* SMAXV / UMAXV */
7233 if (is_u) {
7234 tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7235 } else {
7236 tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7238 break;
7239 case 0x1a: /* SMINV / UMINV */
7240 if (is_u) {
7241 tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7242 } else {
7243 tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7245 break;
7246 default:
7247 g_assert_not_reached();
7251 } else {
7252 /* Floating point vector reduction ops which work across 32
7253 * bit (single) or 16 bit (half-precision) intermediates.
7254 * Note that correct NaN propagation requires that we do these
7255 * operations in exactly the order specified by the pseudocode.
7257 TCGv_ptr fpst = get_fpstatus_ptr(size == MO_16);
7258 int fpopcode = opcode | is_min << 4 | is_u << 5;
7259 int vmap = (1 << elements) - 1;
7260 TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7261 (is_q ? 128 : 64), vmap, fpst);
7262 tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7263 tcg_temp_free_i32(tcg_res32);
7264 tcg_temp_free_ptr(fpst);
7267 tcg_temp_free_i64(tcg_elt);
7269 /* Now truncate the result to the width required for the final output */
7270 if (opcode == 0x03) {
7271 /* SADDLV, UADDLV: result is 2*esize */
7272 size++;
7275 switch (size) {
7276 case 0:
7277 tcg_gen_ext8u_i64(tcg_res, tcg_res);
7278 break;
7279 case 1:
7280 tcg_gen_ext16u_i64(tcg_res, tcg_res);
7281 break;
7282 case 2:
7283 tcg_gen_ext32u_i64(tcg_res, tcg_res);
7284 break;
7285 case 3:
7286 break;
7287 default:
7288 g_assert_not_reached();
7291 write_fp_dreg(s, rd, tcg_res);
7292 tcg_temp_free_i64(tcg_res);
7295 /* DUP (Element, Vector)
7297 * 31 30 29 21 20 16 15 10 9 5 4 0
7298 * +---+---+-------------------+--------+-------------+------+------+
7299 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
7300 * +---+---+-------------------+--------+-------------+------+------+
7302 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7304 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7305 int imm5)
7307 int size = ctz32(imm5);
7308 int index = imm5 >> (size + 1);
7310 if (size > 3 || (size == 3 && !is_q)) {
7311 unallocated_encoding(s);
7312 return;
7315 if (!fp_access_check(s)) {
7316 return;
7319 tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7320 vec_reg_offset(s, rn, index, size),
7321 is_q ? 16 : 8, vec_full_reg_size(s));
7324 /* DUP (element, scalar)
7325 * 31 21 20 16 15 10 9 5 4 0
7326 * +-----------------------+--------+-------------+------+------+
7327 * | 0 1 0 1 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
7328 * +-----------------------+--------+-------------+------+------+
7330 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7331 int imm5)
7333 int size = ctz32(imm5);
7334 int index;
7335 TCGv_i64 tmp;
7337 if (size > 3) {
7338 unallocated_encoding(s);
7339 return;
7342 if (!fp_access_check(s)) {
7343 return;
7346 index = imm5 >> (size + 1);
7348 /* This instruction just extracts the specified element and
7349 * zero-extends it into the bottom of the destination register.
7351 tmp = tcg_temp_new_i64();
7352 read_vec_element(s, tmp, rn, index, size);
7353 write_fp_dreg(s, rd, tmp);
7354 tcg_temp_free_i64(tmp);
7357 /* DUP (General)
7359 * 31 30 29 21 20 16 15 10 9 5 4 0
7360 * +---+---+-------------------+--------+-------------+------+------+
7361 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 1 1 | Rn | Rd |
7362 * +---+---+-------------------+--------+-------------+------+------+
7364 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7366 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7367 int imm5)
7369 int size = ctz32(imm5);
7370 uint32_t dofs, oprsz, maxsz;
7372 if (size > 3 || ((size == 3) && !is_q)) {
7373 unallocated_encoding(s);
7374 return;
7377 if (!fp_access_check(s)) {
7378 return;
7381 dofs = vec_full_reg_offset(s, rd);
7382 oprsz = is_q ? 16 : 8;
7383 maxsz = vec_full_reg_size(s);
7385 tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7388 /* INS (Element)
7390 * 31 21 20 16 15 14 11 10 9 5 4 0
7391 * +-----------------------+--------+------------+---+------+------+
7392 * | 0 1 1 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
7393 * +-----------------------+--------+------------+---+------+------+
7395 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7396 * index: encoded in imm5<4:size+1>
7398 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7399 int imm4, int imm5)
7401 int size = ctz32(imm5);
7402 int src_index, dst_index;
7403 TCGv_i64 tmp;
7405 if (size > 3) {
7406 unallocated_encoding(s);
7407 return;
7410 if (!fp_access_check(s)) {
7411 return;
7414 dst_index = extract32(imm5, 1+size, 5);
7415 src_index = extract32(imm4, size, 4);
7417 tmp = tcg_temp_new_i64();
7419 read_vec_element(s, tmp, rn, src_index, size);
7420 write_vec_element(s, tmp, rd, dst_index, size);
7422 tcg_temp_free_i64(tmp);
7426 /* INS (General)
7428 * 31 21 20 16 15 10 9 5 4 0
7429 * +-----------------------+--------+-------------+------+------+
7430 * | 0 1 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 1 1 1 | Rn | Rd |
7431 * +-----------------------+--------+-------------+------+------+
7433 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7434 * index: encoded in imm5<4:size+1>
7436 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7438 int size = ctz32(imm5);
7439 int idx;
7441 if (size > 3) {
7442 unallocated_encoding(s);
7443 return;
7446 if (!fp_access_check(s)) {
7447 return;
7450 idx = extract32(imm5, 1 + size, 4 - size);
7451 write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7455 * UMOV (General)
7456 * SMOV (General)
7458 * 31 30 29 21 20 16 15 12 10 9 5 4 0
7459 * +---+---+-------------------+--------+-------------+------+------+
7460 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 1 U 1 1 | Rn | Rd |
7461 * +---+---+-------------------+--------+-------------+------+------+
7463 * U: unsigned when set
7464 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7466 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7467 int rn, int rd, int imm5)
7469 int size = ctz32(imm5);
7470 int element;
7471 TCGv_i64 tcg_rd;
7473 /* Check for UnallocatedEncodings */
7474 if (is_signed) {
7475 if (size > 2 || (size == 2 && !is_q)) {
7476 unallocated_encoding(s);
7477 return;
7479 } else {
7480 if (size > 3
7481 || (size < 3 && is_q)
7482 || (size == 3 && !is_q)) {
7483 unallocated_encoding(s);
7484 return;
7488 if (!fp_access_check(s)) {
7489 return;
7492 element = extract32(imm5, 1+size, 4);
7494 tcg_rd = cpu_reg(s, rd);
7495 read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7496 if (is_signed && !is_q) {
7497 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7501 /* AdvSIMD copy
7502 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
7503 * +---+---+----+-----------------+------+---+------+---+------+------+
7504 * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
7505 * +---+---+----+-----------------+------+---+------+---+------+------+
7507 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7509 int rd = extract32(insn, 0, 5);
7510 int rn = extract32(insn, 5, 5);
7511 int imm4 = extract32(insn, 11, 4);
7512 int op = extract32(insn, 29, 1);
7513 int is_q = extract32(insn, 30, 1);
7514 int imm5 = extract32(insn, 16, 5);
7516 if (op) {
7517 if (is_q) {
7518 /* INS (element) */
7519 handle_simd_inse(s, rd, rn, imm4, imm5);
7520 } else {
7521 unallocated_encoding(s);
7523 } else {
7524 switch (imm4) {
7525 case 0:
7526 /* DUP (element - vector) */
7527 handle_simd_dupe(s, is_q, rd, rn, imm5);
7528 break;
7529 case 1:
7530 /* DUP (general) */
7531 handle_simd_dupg(s, is_q, rd, rn, imm5);
7532 break;
7533 case 3:
7534 if (is_q) {
7535 /* INS (general) */
7536 handle_simd_insg(s, rd, rn, imm5);
7537 } else {
7538 unallocated_encoding(s);
7540 break;
7541 case 5:
7542 case 7:
7543 /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
7544 handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
7545 break;
7546 default:
7547 unallocated_encoding(s);
7548 break;
7553 /* AdvSIMD modified immediate
7554 * 31 30 29 28 19 18 16 15 12 11 10 9 5 4 0
7555 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7556 * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh | Rd |
7557 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7559 * There are a number of operations that can be carried out here:
7560 * MOVI - move (shifted) imm into register
7561 * MVNI - move inverted (shifted) imm into register
7562 * ORR - bitwise OR of (shifted) imm with register
7563 * BIC - bitwise clear of (shifted) imm with register
7564 * With ARMv8.2 we also have:
7565 * FMOV half-precision
7567 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
7569 int rd = extract32(insn, 0, 5);
7570 int cmode = extract32(insn, 12, 4);
7571 int cmode_3_1 = extract32(cmode, 1, 3);
7572 int cmode_0 = extract32(cmode, 0, 1);
7573 int o2 = extract32(insn, 11, 1);
7574 uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
7575 bool is_neg = extract32(insn, 29, 1);
7576 bool is_q = extract32(insn, 30, 1);
7577 uint64_t imm = 0;
7579 if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
7580 /* Check for FMOV (vector, immediate) - half-precision */
7581 if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
7582 unallocated_encoding(s);
7583 return;
7587 if (!fp_access_check(s)) {
7588 return;
7591 /* See AdvSIMDExpandImm() in ARM ARM */
7592 switch (cmode_3_1) {
7593 case 0: /* Replicate(Zeros(24):imm8, 2) */
7594 case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
7595 case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
7596 case 3: /* Replicate(imm8:Zeros(24), 2) */
7598 int shift = cmode_3_1 * 8;
7599 imm = bitfield_replicate(abcdefgh << shift, 32);
7600 break;
7602 case 4: /* Replicate(Zeros(8):imm8, 4) */
7603 case 5: /* Replicate(imm8:Zeros(8), 4) */
7605 int shift = (cmode_3_1 & 0x1) * 8;
7606 imm = bitfield_replicate(abcdefgh << shift, 16);
7607 break;
7609 case 6:
7610 if (cmode_0) {
7611 /* Replicate(Zeros(8):imm8:Ones(16), 2) */
7612 imm = (abcdefgh << 16) | 0xffff;
7613 } else {
7614 /* Replicate(Zeros(16):imm8:Ones(8), 2) */
7615 imm = (abcdefgh << 8) | 0xff;
7617 imm = bitfield_replicate(imm, 32);
7618 break;
7619 case 7:
7620 if (!cmode_0 && !is_neg) {
7621 imm = bitfield_replicate(abcdefgh, 8);
7622 } else if (!cmode_0 && is_neg) {
7623 int i;
7624 imm = 0;
7625 for (i = 0; i < 8; i++) {
7626 if ((abcdefgh) & (1 << i)) {
7627 imm |= 0xffULL << (i * 8);
7630 } else if (cmode_0) {
7631 if (is_neg) {
7632 imm = (abcdefgh & 0x3f) << 48;
7633 if (abcdefgh & 0x80) {
7634 imm |= 0x8000000000000000ULL;
7636 if (abcdefgh & 0x40) {
7637 imm |= 0x3fc0000000000000ULL;
7638 } else {
7639 imm |= 0x4000000000000000ULL;
7641 } else {
7642 if (o2) {
7643 /* FMOV (vector, immediate) - half-precision */
7644 imm = vfp_expand_imm(MO_16, abcdefgh);
7645 /* now duplicate across the lanes */
7646 imm = bitfield_replicate(imm, 16);
7647 } else {
7648 imm = (abcdefgh & 0x3f) << 19;
7649 if (abcdefgh & 0x80) {
7650 imm |= 0x80000000;
7652 if (abcdefgh & 0x40) {
7653 imm |= 0x3e000000;
7654 } else {
7655 imm |= 0x40000000;
7657 imm |= (imm << 32);
7661 break;
7662 default:
7663 fprintf(stderr, "%s: cmode_3_1: %x\n", __func__, cmode_3_1);
7664 g_assert_not_reached();
7667 if (cmode_3_1 != 7 && is_neg) {
7668 imm = ~imm;
7671 if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
7672 /* MOVI or MVNI, with MVNI negation handled above. */
7673 tcg_gen_gvec_dup64i(vec_full_reg_offset(s, rd), is_q ? 16 : 8,
7674 vec_full_reg_size(s), imm);
7675 } else {
7676 /* ORR or BIC, with BIC negation to AND handled above. */
7677 if (is_neg) {
7678 gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
7679 } else {
7680 gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
7685 /* AdvSIMD scalar copy
7686 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
7687 * +-----+----+-----------------+------+---+------+---+------+------+
7688 * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
7689 * +-----+----+-----------------+------+---+------+---+------+------+
7691 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
7693 int rd = extract32(insn, 0, 5);
7694 int rn = extract32(insn, 5, 5);
7695 int imm4 = extract32(insn, 11, 4);
7696 int imm5 = extract32(insn, 16, 5);
7697 int op = extract32(insn, 29, 1);
7699 if (op != 0 || imm4 != 0) {
7700 unallocated_encoding(s);
7701 return;
7704 /* DUP (element, scalar) */
7705 handle_simd_dupes(s, rd, rn, imm5);
7708 /* AdvSIMD scalar pairwise
7709 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
7710 * +-----+---+-----------+------+-----------+--------+-----+------+------+
7711 * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
7712 * +-----+---+-----------+------+-----------+--------+-----+------+------+
7714 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
7716 int u = extract32(insn, 29, 1);
7717 int size = extract32(insn, 22, 2);
7718 int opcode = extract32(insn, 12, 5);
7719 int rn = extract32(insn, 5, 5);
7720 int rd = extract32(insn, 0, 5);
7721 TCGv_ptr fpst;
7723 /* For some ops (the FP ones), size[1] is part of the encoding.
7724 * For ADDP strictly it is not but size[1] is always 1 for valid
7725 * encodings.
7727 opcode |= (extract32(size, 1, 1) << 5);
7729 switch (opcode) {
7730 case 0x3b: /* ADDP */
7731 if (u || size != 3) {
7732 unallocated_encoding(s);
7733 return;
7735 if (!fp_access_check(s)) {
7736 return;
7739 fpst = NULL;
7740 break;
7741 case 0xc: /* FMAXNMP */
7742 case 0xd: /* FADDP */
7743 case 0xf: /* FMAXP */
7744 case 0x2c: /* FMINNMP */
7745 case 0x2f: /* FMINP */
7746 /* FP op, size[0] is 32 or 64 bit*/
7747 if (!u) {
7748 if (!dc_isar_feature(aa64_fp16, s)) {
7749 unallocated_encoding(s);
7750 return;
7751 } else {
7752 size = MO_16;
7754 } else {
7755 size = extract32(size, 0, 1) ? MO_64 : MO_32;
7758 if (!fp_access_check(s)) {
7759 return;
7762 fpst = get_fpstatus_ptr(size == MO_16);
7763 break;
7764 default:
7765 unallocated_encoding(s);
7766 return;
7769 if (size == MO_64) {
7770 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7771 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7772 TCGv_i64 tcg_res = tcg_temp_new_i64();
7774 read_vec_element(s, tcg_op1, rn, 0, MO_64);
7775 read_vec_element(s, tcg_op2, rn, 1, MO_64);
7777 switch (opcode) {
7778 case 0x3b: /* ADDP */
7779 tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
7780 break;
7781 case 0xc: /* FMAXNMP */
7782 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7783 break;
7784 case 0xd: /* FADDP */
7785 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
7786 break;
7787 case 0xf: /* FMAXP */
7788 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
7789 break;
7790 case 0x2c: /* FMINNMP */
7791 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7792 break;
7793 case 0x2f: /* FMINP */
7794 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
7795 break;
7796 default:
7797 g_assert_not_reached();
7800 write_fp_dreg(s, rd, tcg_res);
7802 tcg_temp_free_i64(tcg_op1);
7803 tcg_temp_free_i64(tcg_op2);
7804 tcg_temp_free_i64(tcg_res);
7805 } else {
7806 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7807 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7808 TCGv_i32 tcg_res = tcg_temp_new_i32();
7810 read_vec_element_i32(s, tcg_op1, rn, 0, size);
7811 read_vec_element_i32(s, tcg_op2, rn, 1, size);
7813 if (size == MO_16) {
7814 switch (opcode) {
7815 case 0xc: /* FMAXNMP */
7816 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
7817 break;
7818 case 0xd: /* FADDP */
7819 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
7820 break;
7821 case 0xf: /* FMAXP */
7822 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
7823 break;
7824 case 0x2c: /* FMINNMP */
7825 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
7826 break;
7827 case 0x2f: /* FMINP */
7828 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
7829 break;
7830 default:
7831 g_assert_not_reached();
7833 } else {
7834 switch (opcode) {
7835 case 0xc: /* FMAXNMP */
7836 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
7837 break;
7838 case 0xd: /* FADDP */
7839 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
7840 break;
7841 case 0xf: /* FMAXP */
7842 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
7843 break;
7844 case 0x2c: /* FMINNMP */
7845 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
7846 break;
7847 case 0x2f: /* FMINP */
7848 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
7849 break;
7850 default:
7851 g_assert_not_reached();
7855 write_fp_sreg(s, rd, tcg_res);
7857 tcg_temp_free_i32(tcg_op1);
7858 tcg_temp_free_i32(tcg_op2);
7859 tcg_temp_free_i32(tcg_res);
7862 if (fpst) {
7863 tcg_temp_free_ptr(fpst);
7868 * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
7870 * This code is handles the common shifting code and is used by both
7871 * the vector and scalar code.
7873 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
7874 TCGv_i64 tcg_rnd, bool accumulate,
7875 bool is_u, int size, int shift)
7877 bool extended_result = false;
7878 bool round = tcg_rnd != NULL;
7879 int ext_lshift = 0;
7880 TCGv_i64 tcg_src_hi;
7882 if (round && size == 3) {
7883 extended_result = true;
7884 ext_lshift = 64 - shift;
7885 tcg_src_hi = tcg_temp_new_i64();
7886 } else if (shift == 64) {
7887 if (!accumulate && is_u) {
7888 /* result is zero */
7889 tcg_gen_movi_i64(tcg_res, 0);
7890 return;
7894 /* Deal with the rounding step */
7895 if (round) {
7896 if (extended_result) {
7897 TCGv_i64 tcg_zero = tcg_const_i64(0);
7898 if (!is_u) {
7899 /* take care of sign extending tcg_res */
7900 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
7901 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
7902 tcg_src, tcg_src_hi,
7903 tcg_rnd, tcg_zero);
7904 } else {
7905 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
7906 tcg_src, tcg_zero,
7907 tcg_rnd, tcg_zero);
7909 tcg_temp_free_i64(tcg_zero);
7910 } else {
7911 tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
7915 /* Now do the shift right */
7916 if (round && extended_result) {
7917 /* extended case, >64 bit precision required */
7918 if (ext_lshift == 0) {
7919 /* special case, only high bits matter */
7920 tcg_gen_mov_i64(tcg_src, tcg_src_hi);
7921 } else {
7922 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
7923 tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
7924 tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
7926 } else {
7927 if (is_u) {
7928 if (shift == 64) {
7929 /* essentially shifting in 64 zeros */
7930 tcg_gen_movi_i64(tcg_src, 0);
7931 } else {
7932 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
7934 } else {
7935 if (shift == 64) {
7936 /* effectively extending the sign-bit */
7937 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
7938 } else {
7939 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
7944 if (accumulate) {
7945 tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
7946 } else {
7947 tcg_gen_mov_i64(tcg_res, tcg_src);
7950 if (extended_result) {
7951 tcg_temp_free_i64(tcg_src_hi);
7955 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
7956 static void handle_scalar_simd_shri(DisasContext *s,
7957 bool is_u, int immh, int immb,
7958 int opcode, int rn, int rd)
7960 const int size = 3;
7961 int immhb = immh << 3 | immb;
7962 int shift = 2 * (8 << size) - immhb;
7963 bool accumulate = false;
7964 bool round = false;
7965 bool insert = false;
7966 TCGv_i64 tcg_rn;
7967 TCGv_i64 tcg_rd;
7968 TCGv_i64 tcg_round;
7970 if (!extract32(immh, 3, 1)) {
7971 unallocated_encoding(s);
7972 return;
7975 if (!fp_access_check(s)) {
7976 return;
7979 switch (opcode) {
7980 case 0x02: /* SSRA / USRA (accumulate) */
7981 accumulate = true;
7982 break;
7983 case 0x04: /* SRSHR / URSHR (rounding) */
7984 round = true;
7985 break;
7986 case 0x06: /* SRSRA / URSRA (accum + rounding) */
7987 accumulate = round = true;
7988 break;
7989 case 0x08: /* SRI */
7990 insert = true;
7991 break;
7994 if (round) {
7995 uint64_t round_const = 1ULL << (shift - 1);
7996 tcg_round = tcg_const_i64(round_const);
7997 } else {
7998 tcg_round = NULL;
8001 tcg_rn = read_fp_dreg(s, rn);
8002 tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8004 if (insert) {
8005 /* shift count same as element size is valid but does nothing;
8006 * special case to avoid potential shift by 64.
8008 int esize = 8 << size;
8009 if (shift != esize) {
8010 tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8011 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8013 } else {
8014 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8015 accumulate, is_u, size, shift);
8018 write_fp_dreg(s, rd, tcg_rd);
8020 tcg_temp_free_i64(tcg_rn);
8021 tcg_temp_free_i64(tcg_rd);
8022 if (round) {
8023 tcg_temp_free_i64(tcg_round);
8027 /* SHL/SLI - Scalar shift left */
8028 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8029 int immh, int immb, int opcode,
8030 int rn, int rd)
8032 int size = 32 - clz32(immh) - 1;
8033 int immhb = immh << 3 | immb;
8034 int shift = immhb - (8 << size);
8035 TCGv_i64 tcg_rn = new_tmp_a64(s);
8036 TCGv_i64 tcg_rd = new_tmp_a64(s);
8038 if (!extract32(immh, 3, 1)) {
8039 unallocated_encoding(s);
8040 return;
8043 if (!fp_access_check(s)) {
8044 return;
8047 tcg_rn = read_fp_dreg(s, rn);
8048 tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8050 if (insert) {
8051 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8052 } else {
8053 tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8056 write_fp_dreg(s, rd, tcg_rd);
8058 tcg_temp_free_i64(tcg_rn);
8059 tcg_temp_free_i64(tcg_rd);
8062 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8063 * (signed/unsigned) narrowing */
8064 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8065 bool is_u_shift, bool is_u_narrow,
8066 int immh, int immb, int opcode,
8067 int rn, int rd)
8069 int immhb = immh << 3 | immb;
8070 int size = 32 - clz32(immh) - 1;
8071 int esize = 8 << size;
8072 int shift = (2 * esize) - immhb;
8073 int elements = is_scalar ? 1 : (64 / esize);
8074 bool round = extract32(opcode, 0, 1);
8075 TCGMemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8076 TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8077 TCGv_i32 tcg_rd_narrowed;
8078 TCGv_i64 tcg_final;
8080 static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8081 { gen_helper_neon_narrow_sat_s8,
8082 gen_helper_neon_unarrow_sat8 },
8083 { gen_helper_neon_narrow_sat_s16,
8084 gen_helper_neon_unarrow_sat16 },
8085 { gen_helper_neon_narrow_sat_s32,
8086 gen_helper_neon_unarrow_sat32 },
8087 { NULL, NULL },
8089 static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8090 gen_helper_neon_narrow_sat_u8,
8091 gen_helper_neon_narrow_sat_u16,
8092 gen_helper_neon_narrow_sat_u32,
8093 NULL
8095 NeonGenNarrowEnvFn *narrowfn;
8097 int i;
8099 assert(size < 4);
8101 if (extract32(immh, 3, 1)) {
8102 unallocated_encoding(s);
8103 return;
8106 if (!fp_access_check(s)) {
8107 return;
8110 if (is_u_shift) {
8111 narrowfn = unsigned_narrow_fns[size];
8112 } else {
8113 narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8116 tcg_rn = tcg_temp_new_i64();
8117 tcg_rd = tcg_temp_new_i64();
8118 tcg_rd_narrowed = tcg_temp_new_i32();
8119 tcg_final = tcg_const_i64(0);
8121 if (round) {
8122 uint64_t round_const = 1ULL << (shift - 1);
8123 tcg_round = tcg_const_i64(round_const);
8124 } else {
8125 tcg_round = NULL;
8128 for (i = 0; i < elements; i++) {
8129 read_vec_element(s, tcg_rn, rn, i, ldop);
8130 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8131 false, is_u_shift, size+1, shift);
8132 narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8133 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8134 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8137 if (!is_q) {
8138 write_vec_element(s, tcg_final, rd, 0, MO_64);
8139 } else {
8140 write_vec_element(s, tcg_final, rd, 1, MO_64);
8143 if (round) {
8144 tcg_temp_free_i64(tcg_round);
8146 tcg_temp_free_i64(tcg_rn);
8147 tcg_temp_free_i64(tcg_rd);
8148 tcg_temp_free_i32(tcg_rd_narrowed);
8149 tcg_temp_free_i64(tcg_final);
8151 clear_vec_high(s, is_q, rd);
8154 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8155 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8156 bool src_unsigned, bool dst_unsigned,
8157 int immh, int immb, int rn, int rd)
8159 int immhb = immh << 3 | immb;
8160 int size = 32 - clz32(immh) - 1;
8161 int shift = immhb - (8 << size);
8162 int pass;
8164 assert(immh != 0);
8165 assert(!(scalar && is_q));
8167 if (!scalar) {
8168 if (!is_q && extract32(immh, 3, 1)) {
8169 unallocated_encoding(s);
8170 return;
8173 /* Since we use the variable-shift helpers we must
8174 * replicate the shift count into each element of
8175 * the tcg_shift value.
8177 switch (size) {
8178 case 0:
8179 shift |= shift << 8;
8180 /* fall through */
8181 case 1:
8182 shift |= shift << 16;
8183 break;
8184 case 2:
8185 case 3:
8186 break;
8187 default:
8188 g_assert_not_reached();
8192 if (!fp_access_check(s)) {
8193 return;
8196 if (size == 3) {
8197 TCGv_i64 tcg_shift = tcg_const_i64(shift);
8198 static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8199 { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8200 { NULL, gen_helper_neon_qshl_u64 },
8202 NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8203 int maxpass = is_q ? 2 : 1;
8205 for (pass = 0; pass < maxpass; pass++) {
8206 TCGv_i64 tcg_op = tcg_temp_new_i64();
8208 read_vec_element(s, tcg_op, rn, pass, MO_64);
8209 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8210 write_vec_element(s, tcg_op, rd, pass, MO_64);
8212 tcg_temp_free_i64(tcg_op);
8214 tcg_temp_free_i64(tcg_shift);
8215 clear_vec_high(s, is_q, rd);
8216 } else {
8217 TCGv_i32 tcg_shift = tcg_const_i32(shift);
8218 static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8220 { gen_helper_neon_qshl_s8,
8221 gen_helper_neon_qshl_s16,
8222 gen_helper_neon_qshl_s32 },
8223 { gen_helper_neon_qshlu_s8,
8224 gen_helper_neon_qshlu_s16,
8225 gen_helper_neon_qshlu_s32 }
8226 }, {
8227 { NULL, NULL, NULL },
8228 { gen_helper_neon_qshl_u8,
8229 gen_helper_neon_qshl_u16,
8230 gen_helper_neon_qshl_u32 }
8233 NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8234 TCGMemOp memop = scalar ? size : MO_32;
8235 int maxpass = scalar ? 1 : is_q ? 4 : 2;
8237 for (pass = 0; pass < maxpass; pass++) {
8238 TCGv_i32 tcg_op = tcg_temp_new_i32();
8240 read_vec_element_i32(s, tcg_op, rn, pass, memop);
8241 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8242 if (scalar) {
8243 switch (size) {
8244 case 0:
8245 tcg_gen_ext8u_i32(tcg_op, tcg_op);
8246 break;
8247 case 1:
8248 tcg_gen_ext16u_i32(tcg_op, tcg_op);
8249 break;
8250 case 2:
8251 break;
8252 default:
8253 g_assert_not_reached();
8255 write_fp_sreg(s, rd, tcg_op);
8256 } else {
8257 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8260 tcg_temp_free_i32(tcg_op);
8262 tcg_temp_free_i32(tcg_shift);
8264 if (!scalar) {
8265 clear_vec_high(s, is_q, rd);
8270 /* Common vector code for handling integer to FP conversion */
8271 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8272 int elements, int is_signed,
8273 int fracbits, int size)
8275 TCGv_ptr tcg_fpst = get_fpstatus_ptr(size == MO_16);
8276 TCGv_i32 tcg_shift = NULL;
8278 TCGMemOp mop = size | (is_signed ? MO_SIGN : 0);
8279 int pass;
8281 if (fracbits || size == MO_64) {
8282 tcg_shift = tcg_const_i32(fracbits);
8285 if (size == MO_64) {
8286 TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8287 TCGv_i64 tcg_double = tcg_temp_new_i64();
8289 for (pass = 0; pass < elements; pass++) {
8290 read_vec_element(s, tcg_int64, rn, pass, mop);
8292 if (is_signed) {
8293 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8294 tcg_shift, tcg_fpst);
8295 } else {
8296 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8297 tcg_shift, tcg_fpst);
8299 if (elements == 1) {
8300 write_fp_dreg(s, rd, tcg_double);
8301 } else {
8302 write_vec_element(s, tcg_double, rd, pass, MO_64);
8306 tcg_temp_free_i64(tcg_int64);
8307 tcg_temp_free_i64(tcg_double);
8309 } else {
8310 TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8311 TCGv_i32 tcg_float = tcg_temp_new_i32();
8313 for (pass = 0; pass < elements; pass++) {
8314 read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8316 switch (size) {
8317 case MO_32:
8318 if (fracbits) {
8319 if (is_signed) {
8320 gen_helper_vfp_sltos(tcg_float, tcg_int32,
8321 tcg_shift, tcg_fpst);
8322 } else {
8323 gen_helper_vfp_ultos(tcg_float, tcg_int32,
8324 tcg_shift, tcg_fpst);
8326 } else {
8327 if (is_signed) {
8328 gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8329 } else {
8330 gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8333 break;
8334 case MO_16:
8335 if (fracbits) {
8336 if (is_signed) {
8337 gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8338 tcg_shift, tcg_fpst);
8339 } else {
8340 gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8341 tcg_shift, tcg_fpst);
8343 } else {
8344 if (is_signed) {
8345 gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8346 } else {
8347 gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8350 break;
8351 default:
8352 g_assert_not_reached();
8355 if (elements == 1) {
8356 write_fp_sreg(s, rd, tcg_float);
8357 } else {
8358 write_vec_element_i32(s, tcg_float, rd, pass, size);
8362 tcg_temp_free_i32(tcg_int32);
8363 tcg_temp_free_i32(tcg_float);
8366 tcg_temp_free_ptr(tcg_fpst);
8367 if (tcg_shift) {
8368 tcg_temp_free_i32(tcg_shift);
8371 clear_vec_high(s, elements << size == 16, rd);
8374 /* UCVTF/SCVTF - Integer to FP conversion */
8375 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8376 bool is_q, bool is_u,
8377 int immh, int immb, int opcode,
8378 int rn, int rd)
8380 int size, elements, fracbits;
8381 int immhb = immh << 3 | immb;
8383 if (immh & 8) {
8384 size = MO_64;
8385 if (!is_scalar && !is_q) {
8386 unallocated_encoding(s);
8387 return;
8389 } else if (immh & 4) {
8390 size = MO_32;
8391 } else if (immh & 2) {
8392 size = MO_16;
8393 if (!dc_isar_feature(aa64_fp16, s)) {
8394 unallocated_encoding(s);
8395 return;
8397 } else {
8398 /* immh == 0 would be a failure of the decode logic */
8399 g_assert(immh == 1);
8400 unallocated_encoding(s);
8401 return;
8404 if (is_scalar) {
8405 elements = 1;
8406 } else {
8407 elements = (8 << is_q) >> size;
8409 fracbits = (16 << size) - immhb;
8411 if (!fp_access_check(s)) {
8412 return;
8415 handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8418 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8419 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8420 bool is_q, bool is_u,
8421 int immh, int immb, int rn, int rd)
8423 int immhb = immh << 3 | immb;
8424 int pass, size, fracbits;
8425 TCGv_ptr tcg_fpstatus;
8426 TCGv_i32 tcg_rmode, tcg_shift;
8428 if (immh & 0x8) {
8429 size = MO_64;
8430 if (!is_scalar && !is_q) {
8431 unallocated_encoding(s);
8432 return;
8434 } else if (immh & 0x4) {
8435 size = MO_32;
8436 } else if (immh & 0x2) {
8437 size = MO_16;
8438 if (!dc_isar_feature(aa64_fp16, s)) {
8439 unallocated_encoding(s);
8440 return;
8442 } else {
8443 /* Should have split out AdvSIMD modified immediate earlier. */
8444 assert(immh == 1);
8445 unallocated_encoding(s);
8446 return;
8449 if (!fp_access_check(s)) {
8450 return;
8453 assert(!(is_scalar && is_q));
8455 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(FPROUNDING_ZERO));
8456 tcg_fpstatus = get_fpstatus_ptr(size == MO_16);
8457 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
8458 fracbits = (16 << size) - immhb;
8459 tcg_shift = tcg_const_i32(fracbits);
8461 if (size == MO_64) {
8462 int maxpass = is_scalar ? 1 : 2;
8464 for (pass = 0; pass < maxpass; pass++) {
8465 TCGv_i64 tcg_op = tcg_temp_new_i64();
8467 read_vec_element(s, tcg_op, rn, pass, MO_64);
8468 if (is_u) {
8469 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8470 } else {
8471 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8473 write_vec_element(s, tcg_op, rd, pass, MO_64);
8474 tcg_temp_free_i64(tcg_op);
8476 clear_vec_high(s, is_q, rd);
8477 } else {
8478 void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8479 int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8481 switch (size) {
8482 case MO_16:
8483 if (is_u) {
8484 fn = gen_helper_vfp_touhh;
8485 } else {
8486 fn = gen_helper_vfp_toshh;
8488 break;
8489 case MO_32:
8490 if (is_u) {
8491 fn = gen_helper_vfp_touls;
8492 } else {
8493 fn = gen_helper_vfp_tosls;
8495 break;
8496 default:
8497 g_assert_not_reached();
8500 for (pass = 0; pass < maxpass; pass++) {
8501 TCGv_i32 tcg_op = tcg_temp_new_i32();
8503 read_vec_element_i32(s, tcg_op, rn, pass, size);
8504 fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8505 if (is_scalar) {
8506 write_fp_sreg(s, rd, tcg_op);
8507 } else {
8508 write_vec_element_i32(s, tcg_op, rd, pass, size);
8510 tcg_temp_free_i32(tcg_op);
8512 if (!is_scalar) {
8513 clear_vec_high(s, is_q, rd);
8517 tcg_temp_free_ptr(tcg_fpstatus);
8518 tcg_temp_free_i32(tcg_shift);
8519 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
8520 tcg_temp_free_i32(tcg_rmode);
8523 /* AdvSIMD scalar shift by immediate
8524 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
8525 * +-----+---+-------------+------+------+--------+---+------+------+
8526 * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
8527 * +-----+---+-------------+------+------+--------+---+------+------+
8529 * This is the scalar version so it works on a fixed sized registers
8531 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8533 int rd = extract32(insn, 0, 5);
8534 int rn = extract32(insn, 5, 5);
8535 int opcode = extract32(insn, 11, 5);
8536 int immb = extract32(insn, 16, 3);
8537 int immh = extract32(insn, 19, 4);
8538 bool is_u = extract32(insn, 29, 1);
8540 if (immh == 0) {
8541 unallocated_encoding(s);
8542 return;
8545 switch (opcode) {
8546 case 0x08: /* SRI */
8547 if (!is_u) {
8548 unallocated_encoding(s);
8549 return;
8551 /* fall through */
8552 case 0x00: /* SSHR / USHR */
8553 case 0x02: /* SSRA / USRA */
8554 case 0x04: /* SRSHR / URSHR */
8555 case 0x06: /* SRSRA / URSRA */
8556 handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8557 break;
8558 case 0x0a: /* SHL / SLI */
8559 handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8560 break;
8561 case 0x1c: /* SCVTF, UCVTF */
8562 handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8563 opcode, rn, rd);
8564 break;
8565 case 0x10: /* SQSHRUN, SQSHRUN2 */
8566 case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8567 if (!is_u) {
8568 unallocated_encoding(s);
8569 return;
8571 handle_vec_simd_sqshrn(s, true, false, false, true,
8572 immh, immb, opcode, rn, rd);
8573 break;
8574 case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8575 case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8576 handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8577 immh, immb, opcode, rn, rd);
8578 break;
8579 case 0xc: /* SQSHLU */
8580 if (!is_u) {
8581 unallocated_encoding(s);
8582 return;
8584 handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8585 break;
8586 case 0xe: /* SQSHL, UQSHL */
8587 handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8588 break;
8589 case 0x1f: /* FCVTZS, FCVTZU */
8590 handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8591 break;
8592 default:
8593 unallocated_encoding(s);
8594 break;
8598 /* AdvSIMD scalar three different
8599 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
8600 * +-----+---+-----------+------+---+------+--------+-----+------+------+
8601 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
8602 * +-----+---+-----------+------+---+------+--------+-----+------+------+
8604 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8606 bool is_u = extract32(insn, 29, 1);
8607 int size = extract32(insn, 22, 2);
8608 int opcode = extract32(insn, 12, 4);
8609 int rm = extract32(insn, 16, 5);
8610 int rn = extract32(insn, 5, 5);
8611 int rd = extract32(insn, 0, 5);
8613 if (is_u) {
8614 unallocated_encoding(s);
8615 return;
8618 switch (opcode) {
8619 case 0x9: /* SQDMLAL, SQDMLAL2 */
8620 case 0xb: /* SQDMLSL, SQDMLSL2 */
8621 case 0xd: /* SQDMULL, SQDMULL2 */
8622 if (size == 0 || size == 3) {
8623 unallocated_encoding(s);
8624 return;
8626 break;
8627 default:
8628 unallocated_encoding(s);
8629 return;
8632 if (!fp_access_check(s)) {
8633 return;
8636 if (size == 2) {
8637 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8638 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8639 TCGv_i64 tcg_res = tcg_temp_new_i64();
8641 read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8642 read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8644 tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8645 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
8647 switch (opcode) {
8648 case 0xd: /* SQDMULL, SQDMULL2 */
8649 break;
8650 case 0xb: /* SQDMLSL, SQDMLSL2 */
8651 tcg_gen_neg_i64(tcg_res, tcg_res);
8652 /* fall through */
8653 case 0x9: /* SQDMLAL, SQDMLAL2 */
8654 read_vec_element(s, tcg_op1, rd, 0, MO_64);
8655 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
8656 tcg_res, tcg_op1);
8657 break;
8658 default:
8659 g_assert_not_reached();
8662 write_fp_dreg(s, rd, tcg_res);
8664 tcg_temp_free_i64(tcg_op1);
8665 tcg_temp_free_i64(tcg_op2);
8666 tcg_temp_free_i64(tcg_res);
8667 } else {
8668 TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8669 TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8670 TCGv_i64 tcg_res = tcg_temp_new_i64();
8672 gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8673 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
8675 switch (opcode) {
8676 case 0xd: /* SQDMULL, SQDMULL2 */
8677 break;
8678 case 0xb: /* SQDMLSL, SQDMLSL2 */
8679 gen_helper_neon_negl_u32(tcg_res, tcg_res);
8680 /* fall through */
8681 case 0x9: /* SQDMLAL, SQDMLAL2 */
8683 TCGv_i64 tcg_op3 = tcg_temp_new_i64();
8684 read_vec_element(s, tcg_op3, rd, 0, MO_32);
8685 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
8686 tcg_res, tcg_op3);
8687 tcg_temp_free_i64(tcg_op3);
8688 break;
8690 default:
8691 g_assert_not_reached();
8694 tcg_gen_ext32u_i64(tcg_res, tcg_res);
8695 write_fp_dreg(s, rd, tcg_res);
8697 tcg_temp_free_i32(tcg_op1);
8698 tcg_temp_free_i32(tcg_op2);
8699 tcg_temp_free_i64(tcg_res);
8703 static void handle_3same_64(DisasContext *s, int opcode, bool u,
8704 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
8706 /* Handle 64x64->64 opcodes which are shared between the scalar
8707 * and vector 3-same groups. We cover every opcode where size == 3
8708 * is valid in either the three-reg-same (integer, not pairwise)
8709 * or scalar-three-reg-same groups.
8711 TCGCond cond;
8713 switch (opcode) {
8714 case 0x1: /* SQADD */
8715 if (u) {
8716 gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8717 } else {
8718 gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8720 break;
8721 case 0x5: /* SQSUB */
8722 if (u) {
8723 gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8724 } else {
8725 gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8727 break;
8728 case 0x6: /* CMGT, CMHI */
8729 /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
8730 * We implement this using setcond (test) and then negating.
8732 cond = u ? TCG_COND_GTU : TCG_COND_GT;
8733 do_cmop:
8734 tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
8735 tcg_gen_neg_i64(tcg_rd, tcg_rd);
8736 break;
8737 case 0x7: /* CMGE, CMHS */
8738 cond = u ? TCG_COND_GEU : TCG_COND_GE;
8739 goto do_cmop;
8740 case 0x11: /* CMTST, CMEQ */
8741 if (u) {
8742 cond = TCG_COND_EQ;
8743 goto do_cmop;
8745 gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
8746 break;
8747 case 0x8: /* SSHL, USHL */
8748 if (u) {
8749 gen_helper_neon_shl_u64(tcg_rd, tcg_rn, tcg_rm);
8750 } else {
8751 gen_helper_neon_shl_s64(tcg_rd, tcg_rn, tcg_rm);
8753 break;
8754 case 0x9: /* SQSHL, UQSHL */
8755 if (u) {
8756 gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8757 } else {
8758 gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8760 break;
8761 case 0xa: /* SRSHL, URSHL */
8762 if (u) {
8763 gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
8764 } else {
8765 gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
8767 break;
8768 case 0xb: /* SQRSHL, UQRSHL */
8769 if (u) {
8770 gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8771 } else {
8772 gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8774 break;
8775 case 0x10: /* ADD, SUB */
8776 if (u) {
8777 tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
8778 } else {
8779 tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
8781 break;
8782 default:
8783 g_assert_not_reached();
8787 /* Handle the 3-same-operands float operations; shared by the scalar
8788 * and vector encodings. The caller must filter out any encodings
8789 * not allocated for the encoding it is dealing with.
8791 static void handle_3same_float(DisasContext *s, int size, int elements,
8792 int fpopcode, int rd, int rn, int rm)
8794 int pass;
8795 TCGv_ptr fpst = get_fpstatus_ptr(false);
8797 for (pass = 0; pass < elements; pass++) {
8798 if (size) {
8799 /* Double */
8800 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8801 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8802 TCGv_i64 tcg_res = tcg_temp_new_i64();
8804 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8805 read_vec_element(s, tcg_op2, rm, pass, MO_64);
8807 switch (fpopcode) {
8808 case 0x39: /* FMLS */
8809 /* As usual for ARM, separate negation for fused multiply-add */
8810 gen_helper_vfp_negd(tcg_op1, tcg_op1);
8811 /* fall through */
8812 case 0x19: /* FMLA */
8813 read_vec_element(s, tcg_res, rd, pass, MO_64);
8814 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
8815 tcg_res, fpst);
8816 break;
8817 case 0x18: /* FMAXNM */
8818 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8819 break;
8820 case 0x1a: /* FADD */
8821 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8822 break;
8823 case 0x1b: /* FMULX */
8824 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
8825 break;
8826 case 0x1c: /* FCMEQ */
8827 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8828 break;
8829 case 0x1e: /* FMAX */
8830 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8831 break;
8832 case 0x1f: /* FRECPS */
8833 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8834 break;
8835 case 0x38: /* FMINNM */
8836 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8837 break;
8838 case 0x3a: /* FSUB */
8839 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
8840 break;
8841 case 0x3e: /* FMIN */
8842 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8843 break;
8844 case 0x3f: /* FRSQRTS */
8845 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8846 break;
8847 case 0x5b: /* FMUL */
8848 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
8849 break;
8850 case 0x5c: /* FCMGE */
8851 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8852 break;
8853 case 0x5d: /* FACGE */
8854 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8855 break;
8856 case 0x5f: /* FDIV */
8857 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
8858 break;
8859 case 0x7a: /* FABD */
8860 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
8861 gen_helper_vfp_absd(tcg_res, tcg_res);
8862 break;
8863 case 0x7c: /* FCMGT */
8864 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8865 break;
8866 case 0x7d: /* FACGT */
8867 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8868 break;
8869 default:
8870 g_assert_not_reached();
8873 write_vec_element(s, tcg_res, rd, pass, MO_64);
8875 tcg_temp_free_i64(tcg_res);
8876 tcg_temp_free_i64(tcg_op1);
8877 tcg_temp_free_i64(tcg_op2);
8878 } else {
8879 /* Single */
8880 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8881 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8882 TCGv_i32 tcg_res = tcg_temp_new_i32();
8884 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
8885 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
8887 switch (fpopcode) {
8888 case 0x39: /* FMLS */
8889 /* As usual for ARM, separate negation for fused multiply-add */
8890 gen_helper_vfp_negs(tcg_op1, tcg_op1);
8891 /* fall through */
8892 case 0x19: /* FMLA */
8893 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
8894 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
8895 tcg_res, fpst);
8896 break;
8897 case 0x1a: /* FADD */
8898 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8899 break;
8900 case 0x1b: /* FMULX */
8901 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
8902 break;
8903 case 0x1c: /* FCMEQ */
8904 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8905 break;
8906 case 0x1e: /* FMAX */
8907 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8908 break;
8909 case 0x1f: /* FRECPS */
8910 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8911 break;
8912 case 0x18: /* FMAXNM */
8913 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8914 break;
8915 case 0x38: /* FMINNM */
8916 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8917 break;
8918 case 0x3a: /* FSUB */
8919 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
8920 break;
8921 case 0x3e: /* FMIN */
8922 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8923 break;
8924 case 0x3f: /* FRSQRTS */
8925 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8926 break;
8927 case 0x5b: /* FMUL */
8928 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
8929 break;
8930 case 0x5c: /* FCMGE */
8931 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8932 break;
8933 case 0x5d: /* FACGE */
8934 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8935 break;
8936 case 0x5f: /* FDIV */
8937 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
8938 break;
8939 case 0x7a: /* FABD */
8940 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
8941 gen_helper_vfp_abss(tcg_res, tcg_res);
8942 break;
8943 case 0x7c: /* FCMGT */
8944 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8945 break;
8946 case 0x7d: /* FACGT */
8947 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8948 break;
8949 default:
8950 g_assert_not_reached();
8953 if (elements == 1) {
8954 /* scalar single so clear high part */
8955 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
8957 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
8958 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
8959 tcg_temp_free_i64(tcg_tmp);
8960 } else {
8961 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
8964 tcg_temp_free_i32(tcg_res);
8965 tcg_temp_free_i32(tcg_op1);
8966 tcg_temp_free_i32(tcg_op2);
8970 tcg_temp_free_ptr(fpst);
8972 clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
8975 /* AdvSIMD scalar three same
8976 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
8977 * +-----+---+-----------+------+---+------+--------+---+------+------+
8978 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
8979 * +-----+---+-----------+------+---+------+--------+---+------+------+
8981 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
8983 int rd = extract32(insn, 0, 5);
8984 int rn = extract32(insn, 5, 5);
8985 int opcode = extract32(insn, 11, 5);
8986 int rm = extract32(insn, 16, 5);
8987 int size = extract32(insn, 22, 2);
8988 bool u = extract32(insn, 29, 1);
8989 TCGv_i64 tcg_rd;
8991 if (opcode >= 0x18) {
8992 /* Floating point: U, size[1] and opcode indicate operation */
8993 int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
8994 switch (fpopcode) {
8995 case 0x1b: /* FMULX */
8996 case 0x1f: /* FRECPS */
8997 case 0x3f: /* FRSQRTS */
8998 case 0x5d: /* FACGE */
8999 case 0x7d: /* FACGT */
9000 case 0x1c: /* FCMEQ */
9001 case 0x5c: /* FCMGE */
9002 case 0x7c: /* FCMGT */
9003 case 0x7a: /* FABD */
9004 break;
9005 default:
9006 unallocated_encoding(s);
9007 return;
9010 if (!fp_access_check(s)) {
9011 return;
9014 handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9015 return;
9018 switch (opcode) {
9019 case 0x1: /* SQADD, UQADD */
9020 case 0x5: /* SQSUB, UQSUB */
9021 case 0x9: /* SQSHL, UQSHL */
9022 case 0xb: /* SQRSHL, UQRSHL */
9023 break;
9024 case 0x8: /* SSHL, USHL */
9025 case 0xa: /* SRSHL, URSHL */
9026 case 0x6: /* CMGT, CMHI */
9027 case 0x7: /* CMGE, CMHS */
9028 case 0x11: /* CMTST, CMEQ */
9029 case 0x10: /* ADD, SUB (vector) */
9030 if (size != 3) {
9031 unallocated_encoding(s);
9032 return;
9034 break;
9035 case 0x16: /* SQDMULH, SQRDMULH (vector) */
9036 if (size != 1 && size != 2) {
9037 unallocated_encoding(s);
9038 return;
9040 break;
9041 default:
9042 unallocated_encoding(s);
9043 return;
9046 if (!fp_access_check(s)) {
9047 return;
9050 tcg_rd = tcg_temp_new_i64();
9052 if (size == 3) {
9053 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9054 TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9056 handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9057 tcg_temp_free_i64(tcg_rn);
9058 tcg_temp_free_i64(tcg_rm);
9059 } else {
9060 /* Do a single operation on the lowest element in the vector.
9061 * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9062 * no side effects for all these operations.
9063 * OPTME: special-purpose helpers would avoid doing some
9064 * unnecessary work in the helper for the 8 and 16 bit cases.
9066 NeonGenTwoOpEnvFn *genenvfn;
9067 TCGv_i32 tcg_rn = tcg_temp_new_i32();
9068 TCGv_i32 tcg_rm = tcg_temp_new_i32();
9069 TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9071 read_vec_element_i32(s, tcg_rn, rn, 0, size);
9072 read_vec_element_i32(s, tcg_rm, rm, 0, size);
9074 switch (opcode) {
9075 case 0x1: /* SQADD, UQADD */
9077 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9078 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9079 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9080 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9082 genenvfn = fns[size][u];
9083 break;
9085 case 0x5: /* SQSUB, UQSUB */
9087 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9088 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9089 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9090 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9092 genenvfn = fns[size][u];
9093 break;
9095 case 0x9: /* SQSHL, UQSHL */
9097 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9098 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9099 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9100 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9102 genenvfn = fns[size][u];
9103 break;
9105 case 0xb: /* SQRSHL, UQRSHL */
9107 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9108 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9109 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9110 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9112 genenvfn = fns[size][u];
9113 break;
9115 case 0x16: /* SQDMULH, SQRDMULH */
9117 static NeonGenTwoOpEnvFn * const fns[2][2] = {
9118 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9119 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9121 assert(size == 1 || size == 2);
9122 genenvfn = fns[size - 1][u];
9123 break;
9125 default:
9126 g_assert_not_reached();
9129 genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9130 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9131 tcg_temp_free_i32(tcg_rd32);
9132 tcg_temp_free_i32(tcg_rn);
9133 tcg_temp_free_i32(tcg_rm);
9136 write_fp_dreg(s, rd, tcg_rd);
9138 tcg_temp_free_i64(tcg_rd);
9141 /* AdvSIMD scalar three same FP16
9142 * 31 30 29 28 24 23 22 21 20 16 15 14 13 11 10 9 5 4 0
9143 * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9144 * | 0 1 | U | 1 1 1 1 0 | a | 1 0 | Rm | 0 0 | opcode | 1 | Rn | Rd |
9145 * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9146 * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9147 * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9149 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9150 uint32_t insn)
9152 int rd = extract32(insn, 0, 5);
9153 int rn = extract32(insn, 5, 5);
9154 int opcode = extract32(insn, 11, 3);
9155 int rm = extract32(insn, 16, 5);
9156 bool u = extract32(insn, 29, 1);
9157 bool a = extract32(insn, 23, 1);
9158 int fpopcode = opcode | (a << 3) | (u << 4);
9159 TCGv_ptr fpst;
9160 TCGv_i32 tcg_op1;
9161 TCGv_i32 tcg_op2;
9162 TCGv_i32 tcg_res;
9164 switch (fpopcode) {
9165 case 0x03: /* FMULX */
9166 case 0x04: /* FCMEQ (reg) */
9167 case 0x07: /* FRECPS */
9168 case 0x0f: /* FRSQRTS */
9169 case 0x14: /* FCMGE (reg) */
9170 case 0x15: /* FACGE */
9171 case 0x1a: /* FABD */
9172 case 0x1c: /* FCMGT (reg) */
9173 case 0x1d: /* FACGT */
9174 break;
9175 default:
9176 unallocated_encoding(s);
9177 return;
9180 if (!dc_isar_feature(aa64_fp16, s)) {
9181 unallocated_encoding(s);
9184 if (!fp_access_check(s)) {
9185 return;
9188 fpst = get_fpstatus_ptr(true);
9190 tcg_op1 = read_fp_hreg(s, rn);
9191 tcg_op2 = read_fp_hreg(s, rm);
9192 tcg_res = tcg_temp_new_i32();
9194 switch (fpopcode) {
9195 case 0x03: /* FMULX */
9196 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9197 break;
9198 case 0x04: /* FCMEQ (reg) */
9199 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9200 break;
9201 case 0x07: /* FRECPS */
9202 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9203 break;
9204 case 0x0f: /* FRSQRTS */
9205 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9206 break;
9207 case 0x14: /* FCMGE (reg) */
9208 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9209 break;
9210 case 0x15: /* FACGE */
9211 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9212 break;
9213 case 0x1a: /* FABD */
9214 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9215 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9216 break;
9217 case 0x1c: /* FCMGT (reg) */
9218 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9219 break;
9220 case 0x1d: /* FACGT */
9221 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9222 break;
9223 default:
9224 g_assert_not_reached();
9227 write_fp_sreg(s, rd, tcg_res);
9230 tcg_temp_free_i32(tcg_res);
9231 tcg_temp_free_i32(tcg_op1);
9232 tcg_temp_free_i32(tcg_op2);
9233 tcg_temp_free_ptr(fpst);
9236 /* AdvSIMD scalar three same extra
9237 * 31 30 29 28 24 23 22 21 20 16 15 14 11 10 9 5 4 0
9238 * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9239 * | 0 1 | U | 1 1 1 1 0 | size | 0 | Rm | 1 | opcode | 1 | Rn | Rd |
9240 * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9242 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9243 uint32_t insn)
9245 int rd = extract32(insn, 0, 5);
9246 int rn = extract32(insn, 5, 5);
9247 int opcode = extract32(insn, 11, 4);
9248 int rm = extract32(insn, 16, 5);
9249 int size = extract32(insn, 22, 2);
9250 bool u = extract32(insn, 29, 1);
9251 TCGv_i32 ele1, ele2, ele3;
9252 TCGv_i64 res;
9253 bool feature;
9255 switch (u * 16 + opcode) {
9256 case 0x10: /* SQRDMLAH (vector) */
9257 case 0x11: /* SQRDMLSH (vector) */
9258 if (size != 1 && size != 2) {
9259 unallocated_encoding(s);
9260 return;
9262 feature = dc_isar_feature(aa64_rdm, s);
9263 break;
9264 default:
9265 unallocated_encoding(s);
9266 return;
9268 if (!feature) {
9269 unallocated_encoding(s);
9270 return;
9272 if (!fp_access_check(s)) {
9273 return;
9276 /* Do a single operation on the lowest element in the vector.
9277 * We use the standard Neon helpers and rely on 0 OP 0 == 0
9278 * with no side effects for all these operations.
9279 * OPTME: special-purpose helpers would avoid doing some
9280 * unnecessary work in the helper for the 16 bit cases.
9282 ele1 = tcg_temp_new_i32();
9283 ele2 = tcg_temp_new_i32();
9284 ele3 = tcg_temp_new_i32();
9286 read_vec_element_i32(s, ele1, rn, 0, size);
9287 read_vec_element_i32(s, ele2, rm, 0, size);
9288 read_vec_element_i32(s, ele3, rd, 0, size);
9290 switch (opcode) {
9291 case 0x0: /* SQRDMLAH */
9292 if (size == 1) {
9293 gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9294 } else {
9295 gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9297 break;
9298 case 0x1: /* SQRDMLSH */
9299 if (size == 1) {
9300 gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9301 } else {
9302 gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9304 break;
9305 default:
9306 g_assert_not_reached();
9308 tcg_temp_free_i32(ele1);
9309 tcg_temp_free_i32(ele2);
9311 res = tcg_temp_new_i64();
9312 tcg_gen_extu_i32_i64(res, ele3);
9313 tcg_temp_free_i32(ele3);
9315 write_fp_dreg(s, rd, res);
9316 tcg_temp_free_i64(res);
9319 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9320 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9321 TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9323 /* Handle 64->64 opcodes which are shared between the scalar and
9324 * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9325 * is valid in either group and also the double-precision fp ops.
9326 * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9327 * requires them.
9329 TCGCond cond;
9331 switch (opcode) {
9332 case 0x4: /* CLS, CLZ */
9333 if (u) {
9334 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9335 } else {
9336 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9338 break;
9339 case 0x5: /* NOT */
9340 /* This opcode is shared with CNT and RBIT but we have earlier
9341 * enforced that size == 3 if and only if this is the NOT insn.
9343 tcg_gen_not_i64(tcg_rd, tcg_rn);
9344 break;
9345 case 0x7: /* SQABS, SQNEG */
9346 if (u) {
9347 gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9348 } else {
9349 gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9351 break;
9352 case 0xa: /* CMLT */
9353 /* 64 bit integer comparison against zero, result is
9354 * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9355 * subtracting 1.
9357 cond = TCG_COND_LT;
9358 do_cmop:
9359 tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9360 tcg_gen_neg_i64(tcg_rd, tcg_rd);
9361 break;
9362 case 0x8: /* CMGT, CMGE */
9363 cond = u ? TCG_COND_GE : TCG_COND_GT;
9364 goto do_cmop;
9365 case 0x9: /* CMEQ, CMLE */
9366 cond = u ? TCG_COND_LE : TCG_COND_EQ;
9367 goto do_cmop;
9368 case 0xb: /* ABS, NEG */
9369 if (u) {
9370 tcg_gen_neg_i64(tcg_rd, tcg_rn);
9371 } else {
9372 TCGv_i64 tcg_zero = tcg_const_i64(0);
9373 tcg_gen_neg_i64(tcg_rd, tcg_rn);
9374 tcg_gen_movcond_i64(TCG_COND_GT, tcg_rd, tcg_rn, tcg_zero,
9375 tcg_rn, tcg_rd);
9376 tcg_temp_free_i64(tcg_zero);
9378 break;
9379 case 0x2f: /* FABS */
9380 gen_helper_vfp_absd(tcg_rd, tcg_rn);
9381 break;
9382 case 0x6f: /* FNEG */
9383 gen_helper_vfp_negd(tcg_rd, tcg_rn);
9384 break;
9385 case 0x7f: /* FSQRT */
9386 gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9387 break;
9388 case 0x1a: /* FCVTNS */
9389 case 0x1b: /* FCVTMS */
9390 case 0x1c: /* FCVTAS */
9391 case 0x3a: /* FCVTPS */
9392 case 0x3b: /* FCVTZS */
9394 TCGv_i32 tcg_shift = tcg_const_i32(0);
9395 gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
9396 tcg_temp_free_i32(tcg_shift);
9397 break;
9399 case 0x5a: /* FCVTNU */
9400 case 0x5b: /* FCVTMU */
9401 case 0x5c: /* FCVTAU */
9402 case 0x7a: /* FCVTPU */
9403 case 0x7b: /* FCVTZU */
9405 TCGv_i32 tcg_shift = tcg_const_i32(0);
9406 gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
9407 tcg_temp_free_i32(tcg_shift);
9408 break;
9410 case 0x18: /* FRINTN */
9411 case 0x19: /* FRINTM */
9412 case 0x38: /* FRINTP */
9413 case 0x39: /* FRINTZ */
9414 case 0x58: /* FRINTA */
9415 case 0x79: /* FRINTI */
9416 gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9417 break;
9418 case 0x59: /* FRINTX */
9419 gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9420 break;
9421 default:
9422 g_assert_not_reached();
9426 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9427 bool is_scalar, bool is_u, bool is_q,
9428 int size, int rn, int rd)
9430 bool is_double = (size == MO_64);
9431 TCGv_ptr fpst;
9433 if (!fp_access_check(s)) {
9434 return;
9437 fpst = get_fpstatus_ptr(size == MO_16);
9439 if (is_double) {
9440 TCGv_i64 tcg_op = tcg_temp_new_i64();
9441 TCGv_i64 tcg_zero = tcg_const_i64(0);
9442 TCGv_i64 tcg_res = tcg_temp_new_i64();
9443 NeonGenTwoDoubleOPFn *genfn;
9444 bool swap = false;
9445 int pass;
9447 switch (opcode) {
9448 case 0x2e: /* FCMLT (zero) */
9449 swap = true;
9450 /* fallthrough */
9451 case 0x2c: /* FCMGT (zero) */
9452 genfn = gen_helper_neon_cgt_f64;
9453 break;
9454 case 0x2d: /* FCMEQ (zero) */
9455 genfn = gen_helper_neon_ceq_f64;
9456 break;
9457 case 0x6d: /* FCMLE (zero) */
9458 swap = true;
9459 /* fall through */
9460 case 0x6c: /* FCMGE (zero) */
9461 genfn = gen_helper_neon_cge_f64;
9462 break;
9463 default:
9464 g_assert_not_reached();
9467 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9468 read_vec_element(s, tcg_op, rn, pass, MO_64);
9469 if (swap) {
9470 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9471 } else {
9472 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9474 write_vec_element(s, tcg_res, rd, pass, MO_64);
9476 tcg_temp_free_i64(tcg_res);
9477 tcg_temp_free_i64(tcg_zero);
9478 tcg_temp_free_i64(tcg_op);
9480 clear_vec_high(s, !is_scalar, rd);
9481 } else {
9482 TCGv_i32 tcg_op = tcg_temp_new_i32();
9483 TCGv_i32 tcg_zero = tcg_const_i32(0);
9484 TCGv_i32 tcg_res = tcg_temp_new_i32();
9485 NeonGenTwoSingleOPFn *genfn;
9486 bool swap = false;
9487 int pass, maxpasses;
9489 if (size == MO_16) {
9490 switch (opcode) {
9491 case 0x2e: /* FCMLT (zero) */
9492 swap = true;
9493 /* fall through */
9494 case 0x2c: /* FCMGT (zero) */
9495 genfn = gen_helper_advsimd_cgt_f16;
9496 break;
9497 case 0x2d: /* FCMEQ (zero) */
9498 genfn = gen_helper_advsimd_ceq_f16;
9499 break;
9500 case 0x6d: /* FCMLE (zero) */
9501 swap = true;
9502 /* fall through */
9503 case 0x6c: /* FCMGE (zero) */
9504 genfn = gen_helper_advsimd_cge_f16;
9505 break;
9506 default:
9507 g_assert_not_reached();
9509 } else {
9510 switch (opcode) {
9511 case 0x2e: /* FCMLT (zero) */
9512 swap = true;
9513 /* fall through */
9514 case 0x2c: /* FCMGT (zero) */
9515 genfn = gen_helper_neon_cgt_f32;
9516 break;
9517 case 0x2d: /* FCMEQ (zero) */
9518 genfn = gen_helper_neon_ceq_f32;
9519 break;
9520 case 0x6d: /* FCMLE (zero) */
9521 swap = true;
9522 /* fall through */
9523 case 0x6c: /* FCMGE (zero) */
9524 genfn = gen_helper_neon_cge_f32;
9525 break;
9526 default:
9527 g_assert_not_reached();
9531 if (is_scalar) {
9532 maxpasses = 1;
9533 } else {
9534 int vector_size = 8 << is_q;
9535 maxpasses = vector_size >> size;
9538 for (pass = 0; pass < maxpasses; pass++) {
9539 read_vec_element_i32(s, tcg_op, rn, pass, size);
9540 if (swap) {
9541 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9542 } else {
9543 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9545 if (is_scalar) {
9546 write_fp_sreg(s, rd, tcg_res);
9547 } else {
9548 write_vec_element_i32(s, tcg_res, rd, pass, size);
9551 tcg_temp_free_i32(tcg_res);
9552 tcg_temp_free_i32(tcg_zero);
9553 tcg_temp_free_i32(tcg_op);
9554 if (!is_scalar) {
9555 clear_vec_high(s, is_q, rd);
9559 tcg_temp_free_ptr(fpst);
9562 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9563 bool is_scalar, bool is_u, bool is_q,
9564 int size, int rn, int rd)
9566 bool is_double = (size == 3);
9567 TCGv_ptr fpst = get_fpstatus_ptr(false);
9569 if (is_double) {
9570 TCGv_i64 tcg_op = tcg_temp_new_i64();
9571 TCGv_i64 tcg_res = tcg_temp_new_i64();
9572 int pass;
9574 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9575 read_vec_element(s, tcg_op, rn, pass, MO_64);
9576 switch (opcode) {
9577 case 0x3d: /* FRECPE */
9578 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9579 break;
9580 case 0x3f: /* FRECPX */
9581 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9582 break;
9583 case 0x7d: /* FRSQRTE */
9584 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9585 break;
9586 default:
9587 g_assert_not_reached();
9589 write_vec_element(s, tcg_res, rd, pass, MO_64);
9591 tcg_temp_free_i64(tcg_res);
9592 tcg_temp_free_i64(tcg_op);
9593 clear_vec_high(s, !is_scalar, rd);
9594 } else {
9595 TCGv_i32 tcg_op = tcg_temp_new_i32();
9596 TCGv_i32 tcg_res = tcg_temp_new_i32();
9597 int pass, maxpasses;
9599 if (is_scalar) {
9600 maxpasses = 1;
9601 } else {
9602 maxpasses = is_q ? 4 : 2;
9605 for (pass = 0; pass < maxpasses; pass++) {
9606 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9608 switch (opcode) {
9609 case 0x3c: /* URECPE */
9610 gen_helper_recpe_u32(tcg_res, tcg_op, fpst);
9611 break;
9612 case 0x3d: /* FRECPE */
9613 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9614 break;
9615 case 0x3f: /* FRECPX */
9616 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9617 break;
9618 case 0x7d: /* FRSQRTE */
9619 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9620 break;
9621 default:
9622 g_assert_not_reached();
9625 if (is_scalar) {
9626 write_fp_sreg(s, rd, tcg_res);
9627 } else {
9628 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9631 tcg_temp_free_i32(tcg_res);
9632 tcg_temp_free_i32(tcg_op);
9633 if (!is_scalar) {
9634 clear_vec_high(s, is_q, rd);
9637 tcg_temp_free_ptr(fpst);
9640 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9641 int opcode, bool u, bool is_q,
9642 int size, int rn, int rd)
9644 /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9645 * in the source becomes a size element in the destination).
9647 int pass;
9648 TCGv_i32 tcg_res[2];
9649 int destelt = is_q ? 2 : 0;
9650 int passes = scalar ? 1 : 2;
9652 if (scalar) {
9653 tcg_res[1] = tcg_const_i32(0);
9656 for (pass = 0; pass < passes; pass++) {
9657 TCGv_i64 tcg_op = tcg_temp_new_i64();
9658 NeonGenNarrowFn *genfn = NULL;
9659 NeonGenNarrowEnvFn *genenvfn = NULL;
9661 if (scalar) {
9662 read_vec_element(s, tcg_op, rn, pass, size + 1);
9663 } else {
9664 read_vec_element(s, tcg_op, rn, pass, MO_64);
9666 tcg_res[pass] = tcg_temp_new_i32();
9668 switch (opcode) {
9669 case 0x12: /* XTN, SQXTUN */
9671 static NeonGenNarrowFn * const xtnfns[3] = {
9672 gen_helper_neon_narrow_u8,
9673 gen_helper_neon_narrow_u16,
9674 tcg_gen_extrl_i64_i32,
9676 static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9677 gen_helper_neon_unarrow_sat8,
9678 gen_helper_neon_unarrow_sat16,
9679 gen_helper_neon_unarrow_sat32,
9681 if (u) {
9682 genenvfn = sqxtunfns[size];
9683 } else {
9684 genfn = xtnfns[size];
9686 break;
9688 case 0x14: /* SQXTN, UQXTN */
9690 static NeonGenNarrowEnvFn * const fns[3][2] = {
9691 { gen_helper_neon_narrow_sat_s8,
9692 gen_helper_neon_narrow_sat_u8 },
9693 { gen_helper_neon_narrow_sat_s16,
9694 gen_helper_neon_narrow_sat_u16 },
9695 { gen_helper_neon_narrow_sat_s32,
9696 gen_helper_neon_narrow_sat_u32 },
9698 genenvfn = fns[size][u];
9699 break;
9701 case 0x16: /* FCVTN, FCVTN2 */
9702 /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9703 if (size == 2) {
9704 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
9705 } else {
9706 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9707 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9708 TCGv_ptr fpst = get_fpstatus_ptr(false);
9709 TCGv_i32 ahp = get_ahp_flag();
9711 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9712 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9713 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9714 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9715 tcg_temp_free_i32(tcg_lo);
9716 tcg_temp_free_i32(tcg_hi);
9717 tcg_temp_free_ptr(fpst);
9718 tcg_temp_free_i32(ahp);
9720 break;
9721 case 0x56: /* FCVTXN, FCVTXN2 */
9722 /* 64 bit to 32 bit float conversion
9723 * with von Neumann rounding (round to odd)
9725 assert(size == 2);
9726 gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
9727 break;
9728 default:
9729 g_assert_not_reached();
9732 if (genfn) {
9733 genfn(tcg_res[pass], tcg_op);
9734 } else if (genenvfn) {
9735 genenvfn(tcg_res[pass], cpu_env, tcg_op);
9738 tcg_temp_free_i64(tcg_op);
9741 for (pass = 0; pass < 2; pass++) {
9742 write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
9743 tcg_temp_free_i32(tcg_res[pass]);
9745 clear_vec_high(s, is_q, rd);
9748 /* Remaining saturating accumulating ops */
9749 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
9750 bool is_q, int size, int rn, int rd)
9752 bool is_double = (size == 3);
9754 if (is_double) {
9755 TCGv_i64 tcg_rn = tcg_temp_new_i64();
9756 TCGv_i64 tcg_rd = tcg_temp_new_i64();
9757 int pass;
9759 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9760 read_vec_element(s, tcg_rn, rn, pass, MO_64);
9761 read_vec_element(s, tcg_rd, rd, pass, MO_64);
9763 if (is_u) { /* USQADD */
9764 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9765 } else { /* SUQADD */
9766 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9768 write_vec_element(s, tcg_rd, rd, pass, MO_64);
9770 tcg_temp_free_i64(tcg_rd);
9771 tcg_temp_free_i64(tcg_rn);
9772 clear_vec_high(s, !is_scalar, rd);
9773 } else {
9774 TCGv_i32 tcg_rn = tcg_temp_new_i32();
9775 TCGv_i32 tcg_rd = tcg_temp_new_i32();
9776 int pass, maxpasses;
9778 if (is_scalar) {
9779 maxpasses = 1;
9780 } else {
9781 maxpasses = is_q ? 4 : 2;
9784 for (pass = 0; pass < maxpasses; pass++) {
9785 if (is_scalar) {
9786 read_vec_element_i32(s, tcg_rn, rn, pass, size);
9787 read_vec_element_i32(s, tcg_rd, rd, pass, size);
9788 } else {
9789 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
9790 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9793 if (is_u) { /* USQADD */
9794 switch (size) {
9795 case 0:
9796 gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9797 break;
9798 case 1:
9799 gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9800 break;
9801 case 2:
9802 gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9803 break;
9804 default:
9805 g_assert_not_reached();
9807 } else { /* SUQADD */
9808 switch (size) {
9809 case 0:
9810 gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9811 break;
9812 case 1:
9813 gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9814 break;
9815 case 2:
9816 gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9817 break;
9818 default:
9819 g_assert_not_reached();
9823 if (is_scalar) {
9824 TCGv_i64 tcg_zero = tcg_const_i64(0);
9825 write_vec_element(s, tcg_zero, rd, 0, MO_64);
9826 tcg_temp_free_i64(tcg_zero);
9828 write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9830 tcg_temp_free_i32(tcg_rd);
9831 tcg_temp_free_i32(tcg_rn);
9832 clear_vec_high(s, is_q, rd);
9836 /* AdvSIMD scalar two reg misc
9837 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
9838 * +-----+---+-----------+------+-----------+--------+-----+------+------+
9839 * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
9840 * +-----+---+-----------+------+-----------+--------+-----+------+------+
9842 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
9844 int rd = extract32(insn, 0, 5);
9845 int rn = extract32(insn, 5, 5);
9846 int opcode = extract32(insn, 12, 5);
9847 int size = extract32(insn, 22, 2);
9848 bool u = extract32(insn, 29, 1);
9849 bool is_fcvt = false;
9850 int rmode;
9851 TCGv_i32 tcg_rmode;
9852 TCGv_ptr tcg_fpstatus;
9854 switch (opcode) {
9855 case 0x3: /* USQADD / SUQADD*/
9856 if (!fp_access_check(s)) {
9857 return;
9859 handle_2misc_satacc(s, true, u, false, size, rn, rd);
9860 return;
9861 case 0x7: /* SQABS / SQNEG */
9862 break;
9863 case 0xa: /* CMLT */
9864 if (u) {
9865 unallocated_encoding(s);
9866 return;
9868 /* fall through */
9869 case 0x8: /* CMGT, CMGE */
9870 case 0x9: /* CMEQ, CMLE */
9871 case 0xb: /* ABS, NEG */
9872 if (size != 3) {
9873 unallocated_encoding(s);
9874 return;
9876 break;
9877 case 0x12: /* SQXTUN */
9878 if (!u) {
9879 unallocated_encoding(s);
9880 return;
9882 /* fall through */
9883 case 0x14: /* SQXTN, UQXTN */
9884 if (size == 3) {
9885 unallocated_encoding(s);
9886 return;
9888 if (!fp_access_check(s)) {
9889 return;
9891 handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
9892 return;
9893 case 0xc ... 0xf:
9894 case 0x16 ... 0x1d:
9895 case 0x1f:
9896 /* Floating point: U, size[1] and opcode indicate operation;
9897 * size[0] indicates single or double precision.
9899 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
9900 size = extract32(size, 0, 1) ? 3 : 2;
9901 switch (opcode) {
9902 case 0x2c: /* FCMGT (zero) */
9903 case 0x2d: /* FCMEQ (zero) */
9904 case 0x2e: /* FCMLT (zero) */
9905 case 0x6c: /* FCMGE (zero) */
9906 case 0x6d: /* FCMLE (zero) */
9907 handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
9908 return;
9909 case 0x1d: /* SCVTF */
9910 case 0x5d: /* UCVTF */
9912 bool is_signed = (opcode == 0x1d);
9913 if (!fp_access_check(s)) {
9914 return;
9916 handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
9917 return;
9919 case 0x3d: /* FRECPE */
9920 case 0x3f: /* FRECPX */
9921 case 0x7d: /* FRSQRTE */
9922 if (!fp_access_check(s)) {
9923 return;
9925 handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
9926 return;
9927 case 0x1a: /* FCVTNS */
9928 case 0x1b: /* FCVTMS */
9929 case 0x3a: /* FCVTPS */
9930 case 0x3b: /* FCVTZS */
9931 case 0x5a: /* FCVTNU */
9932 case 0x5b: /* FCVTMU */
9933 case 0x7a: /* FCVTPU */
9934 case 0x7b: /* FCVTZU */
9935 is_fcvt = true;
9936 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
9937 break;
9938 case 0x1c: /* FCVTAS */
9939 case 0x5c: /* FCVTAU */
9940 /* TIEAWAY doesn't fit in the usual rounding mode encoding */
9941 is_fcvt = true;
9942 rmode = FPROUNDING_TIEAWAY;
9943 break;
9944 case 0x56: /* FCVTXN, FCVTXN2 */
9945 if (size == 2) {
9946 unallocated_encoding(s);
9947 return;
9949 if (!fp_access_check(s)) {
9950 return;
9952 handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
9953 return;
9954 default:
9955 unallocated_encoding(s);
9956 return;
9958 break;
9959 default:
9960 unallocated_encoding(s);
9961 return;
9964 if (!fp_access_check(s)) {
9965 return;
9968 if (is_fcvt) {
9969 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
9970 tcg_fpstatus = get_fpstatus_ptr(false);
9971 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
9972 } else {
9973 tcg_rmode = NULL;
9974 tcg_fpstatus = NULL;
9977 if (size == 3) {
9978 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9979 TCGv_i64 tcg_rd = tcg_temp_new_i64();
9981 handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
9982 write_fp_dreg(s, rd, tcg_rd);
9983 tcg_temp_free_i64(tcg_rd);
9984 tcg_temp_free_i64(tcg_rn);
9985 } else {
9986 TCGv_i32 tcg_rn = tcg_temp_new_i32();
9987 TCGv_i32 tcg_rd = tcg_temp_new_i32();
9989 read_vec_element_i32(s, tcg_rn, rn, 0, size);
9991 switch (opcode) {
9992 case 0x7: /* SQABS, SQNEG */
9994 NeonGenOneOpEnvFn *genfn;
9995 static NeonGenOneOpEnvFn * const fns[3][2] = {
9996 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
9997 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
9998 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10000 genfn = fns[size][u];
10001 genfn(tcg_rd, cpu_env, tcg_rn);
10002 break;
10004 case 0x1a: /* FCVTNS */
10005 case 0x1b: /* FCVTMS */
10006 case 0x1c: /* FCVTAS */
10007 case 0x3a: /* FCVTPS */
10008 case 0x3b: /* FCVTZS */
10010 TCGv_i32 tcg_shift = tcg_const_i32(0);
10011 gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
10012 tcg_temp_free_i32(tcg_shift);
10013 break;
10015 case 0x5a: /* FCVTNU */
10016 case 0x5b: /* FCVTMU */
10017 case 0x5c: /* FCVTAU */
10018 case 0x7a: /* FCVTPU */
10019 case 0x7b: /* FCVTZU */
10021 TCGv_i32 tcg_shift = tcg_const_i32(0);
10022 gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
10023 tcg_temp_free_i32(tcg_shift);
10024 break;
10026 default:
10027 g_assert_not_reached();
10030 write_fp_sreg(s, rd, tcg_rd);
10031 tcg_temp_free_i32(tcg_rd);
10032 tcg_temp_free_i32(tcg_rn);
10035 if (is_fcvt) {
10036 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
10037 tcg_temp_free_i32(tcg_rmode);
10038 tcg_temp_free_ptr(tcg_fpstatus);
10042 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10043 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10044 int immh, int immb, int opcode, int rn, int rd)
10046 int size = 32 - clz32(immh) - 1;
10047 int immhb = immh << 3 | immb;
10048 int shift = 2 * (8 << size) - immhb;
10049 bool accumulate = false;
10050 int dsize = is_q ? 128 : 64;
10051 int esize = 8 << size;
10052 int elements = dsize/esize;
10053 TCGMemOp memop = size | (is_u ? 0 : MO_SIGN);
10054 TCGv_i64 tcg_rn = new_tmp_a64(s);
10055 TCGv_i64 tcg_rd = new_tmp_a64(s);
10056 TCGv_i64 tcg_round;
10057 uint64_t round_const;
10058 int i;
10060 if (extract32(immh, 3, 1) && !is_q) {
10061 unallocated_encoding(s);
10062 return;
10064 tcg_debug_assert(size <= 3);
10066 if (!fp_access_check(s)) {
10067 return;
10070 switch (opcode) {
10071 case 0x02: /* SSRA / USRA (accumulate) */
10072 if (is_u) {
10073 /* Shift count same as element size produces zero to add. */
10074 if (shift == 8 << size) {
10075 goto done;
10077 gen_gvec_op2i(s, is_q, rd, rn, shift, &usra_op[size]);
10078 } else {
10079 /* Shift count same as element size produces all sign to add. */
10080 if (shift == 8 << size) {
10081 shift -= 1;
10083 gen_gvec_op2i(s, is_q, rd, rn, shift, &ssra_op[size]);
10085 return;
10086 case 0x08: /* SRI */
10087 /* Shift count same as element size is valid but does nothing. */
10088 if (shift == 8 << size) {
10089 goto done;
10091 gen_gvec_op2i(s, is_q, rd, rn, shift, &sri_op[size]);
10092 return;
10094 case 0x00: /* SSHR / USHR */
10095 if (is_u) {
10096 if (shift == 8 << size) {
10097 /* Shift count the same size as element size produces zero. */
10098 tcg_gen_gvec_dup8i(vec_full_reg_offset(s, rd),
10099 is_q ? 16 : 8, vec_full_reg_size(s), 0);
10100 } else {
10101 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shri, size);
10103 } else {
10104 /* Shift count the same size as element size produces all sign. */
10105 if (shift == 8 << size) {
10106 shift -= 1;
10108 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_sari, size);
10110 return;
10112 case 0x04: /* SRSHR / URSHR (rounding) */
10113 break;
10114 case 0x06: /* SRSRA / URSRA (accum + rounding) */
10115 accumulate = true;
10116 break;
10117 default:
10118 g_assert_not_reached();
10121 round_const = 1ULL << (shift - 1);
10122 tcg_round = tcg_const_i64(round_const);
10124 for (i = 0; i < elements; i++) {
10125 read_vec_element(s, tcg_rn, rn, i, memop);
10126 if (accumulate) {
10127 read_vec_element(s, tcg_rd, rd, i, memop);
10130 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10131 accumulate, is_u, size, shift);
10133 write_vec_element(s, tcg_rd, rd, i, size);
10135 tcg_temp_free_i64(tcg_round);
10137 done:
10138 clear_vec_high(s, is_q, rd);
10141 /* SHL/SLI - Vector shift left */
10142 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10143 int immh, int immb, int opcode, int rn, int rd)
10145 int size = 32 - clz32(immh) - 1;
10146 int immhb = immh << 3 | immb;
10147 int shift = immhb - (8 << size);
10149 /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10150 assert(size >= 0 && size <= 3);
10152 if (extract32(immh, 3, 1) && !is_q) {
10153 unallocated_encoding(s);
10154 return;
10157 if (!fp_access_check(s)) {
10158 return;
10161 if (insert) {
10162 gen_gvec_op2i(s, is_q, rd, rn, shift, &sli_op[size]);
10163 } else {
10164 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10168 /* USHLL/SHLL - Vector shift left with widening */
10169 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10170 int immh, int immb, int opcode, int rn, int rd)
10172 int size = 32 - clz32(immh) - 1;
10173 int immhb = immh << 3 | immb;
10174 int shift = immhb - (8 << size);
10175 int dsize = 64;
10176 int esize = 8 << size;
10177 int elements = dsize/esize;
10178 TCGv_i64 tcg_rn = new_tmp_a64(s);
10179 TCGv_i64 tcg_rd = new_tmp_a64(s);
10180 int i;
10182 if (size >= 3) {
10183 unallocated_encoding(s);
10184 return;
10187 if (!fp_access_check(s)) {
10188 return;
10191 /* For the LL variants the store is larger than the load,
10192 * so if rd == rn we would overwrite parts of our input.
10193 * So load everything right now and use shifts in the main loop.
10195 read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10197 for (i = 0; i < elements; i++) {
10198 tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10199 ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10200 tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10201 write_vec_element(s, tcg_rd, rd, i, size + 1);
10205 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10206 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10207 int immh, int immb, int opcode, int rn, int rd)
10209 int immhb = immh << 3 | immb;
10210 int size = 32 - clz32(immh) - 1;
10211 int dsize = 64;
10212 int esize = 8 << size;
10213 int elements = dsize/esize;
10214 int shift = (2 * esize) - immhb;
10215 bool round = extract32(opcode, 0, 1);
10216 TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10217 TCGv_i64 tcg_round;
10218 int i;
10220 if (extract32(immh, 3, 1)) {
10221 unallocated_encoding(s);
10222 return;
10225 if (!fp_access_check(s)) {
10226 return;
10229 tcg_rn = tcg_temp_new_i64();
10230 tcg_rd = tcg_temp_new_i64();
10231 tcg_final = tcg_temp_new_i64();
10232 read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10234 if (round) {
10235 uint64_t round_const = 1ULL << (shift - 1);
10236 tcg_round = tcg_const_i64(round_const);
10237 } else {
10238 tcg_round = NULL;
10241 for (i = 0; i < elements; i++) {
10242 read_vec_element(s, tcg_rn, rn, i, size+1);
10243 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10244 false, true, size+1, shift);
10246 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10249 if (!is_q) {
10250 write_vec_element(s, tcg_final, rd, 0, MO_64);
10251 } else {
10252 write_vec_element(s, tcg_final, rd, 1, MO_64);
10254 if (round) {
10255 tcg_temp_free_i64(tcg_round);
10257 tcg_temp_free_i64(tcg_rn);
10258 tcg_temp_free_i64(tcg_rd);
10259 tcg_temp_free_i64(tcg_final);
10261 clear_vec_high(s, is_q, rd);
10265 /* AdvSIMD shift by immediate
10266 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
10267 * +---+---+---+-------------+------+------+--------+---+------+------+
10268 * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
10269 * +---+---+---+-------------+------+------+--------+---+------+------+
10271 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10273 int rd = extract32(insn, 0, 5);
10274 int rn = extract32(insn, 5, 5);
10275 int opcode = extract32(insn, 11, 5);
10276 int immb = extract32(insn, 16, 3);
10277 int immh = extract32(insn, 19, 4);
10278 bool is_u = extract32(insn, 29, 1);
10279 bool is_q = extract32(insn, 30, 1);
10281 switch (opcode) {
10282 case 0x08: /* SRI */
10283 if (!is_u) {
10284 unallocated_encoding(s);
10285 return;
10287 /* fall through */
10288 case 0x00: /* SSHR / USHR */
10289 case 0x02: /* SSRA / USRA (accumulate) */
10290 case 0x04: /* SRSHR / URSHR (rounding) */
10291 case 0x06: /* SRSRA / URSRA (accum + rounding) */
10292 handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10293 break;
10294 case 0x0a: /* SHL / SLI */
10295 handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10296 break;
10297 case 0x10: /* SHRN */
10298 case 0x11: /* RSHRN / SQRSHRUN */
10299 if (is_u) {
10300 handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10301 opcode, rn, rd);
10302 } else {
10303 handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10305 break;
10306 case 0x12: /* SQSHRN / UQSHRN */
10307 case 0x13: /* SQRSHRN / UQRSHRN */
10308 handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10309 opcode, rn, rd);
10310 break;
10311 case 0x14: /* SSHLL / USHLL */
10312 handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10313 break;
10314 case 0x1c: /* SCVTF / UCVTF */
10315 handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10316 opcode, rn, rd);
10317 break;
10318 case 0xc: /* SQSHLU */
10319 if (!is_u) {
10320 unallocated_encoding(s);
10321 return;
10323 handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10324 break;
10325 case 0xe: /* SQSHL, UQSHL */
10326 handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10327 break;
10328 case 0x1f: /* FCVTZS/ FCVTZU */
10329 handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10330 return;
10331 default:
10332 unallocated_encoding(s);
10333 return;
10337 /* Generate code to do a "long" addition or subtraction, ie one done in
10338 * TCGv_i64 on vector lanes twice the width specified by size.
10340 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10341 TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10343 static NeonGenTwo64OpFn * const fns[3][2] = {
10344 { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10345 { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10346 { tcg_gen_add_i64, tcg_gen_sub_i64 },
10348 NeonGenTwo64OpFn *genfn;
10349 assert(size < 3);
10351 genfn = fns[size][is_sub];
10352 genfn(tcg_res, tcg_op1, tcg_op2);
10355 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10356 int opcode, int rd, int rn, int rm)
10358 /* 3-reg-different widening insns: 64 x 64 -> 128 */
10359 TCGv_i64 tcg_res[2];
10360 int pass, accop;
10362 tcg_res[0] = tcg_temp_new_i64();
10363 tcg_res[1] = tcg_temp_new_i64();
10365 /* Does this op do an adding accumulate, a subtracting accumulate,
10366 * or no accumulate at all?
10368 switch (opcode) {
10369 case 5:
10370 case 8:
10371 case 9:
10372 accop = 1;
10373 break;
10374 case 10:
10375 case 11:
10376 accop = -1;
10377 break;
10378 default:
10379 accop = 0;
10380 break;
10383 if (accop != 0) {
10384 read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10385 read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10388 /* size == 2 means two 32x32->64 operations; this is worth special
10389 * casing because we can generally handle it inline.
10391 if (size == 2) {
10392 for (pass = 0; pass < 2; pass++) {
10393 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10394 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10395 TCGv_i64 tcg_passres;
10396 TCGMemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10398 int elt = pass + is_q * 2;
10400 read_vec_element(s, tcg_op1, rn, elt, memop);
10401 read_vec_element(s, tcg_op2, rm, elt, memop);
10403 if (accop == 0) {
10404 tcg_passres = tcg_res[pass];
10405 } else {
10406 tcg_passres = tcg_temp_new_i64();
10409 switch (opcode) {
10410 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10411 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10412 break;
10413 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10414 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10415 break;
10416 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10417 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10419 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10420 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10422 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10423 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10424 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10425 tcg_passres,
10426 tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10427 tcg_temp_free_i64(tcg_tmp1);
10428 tcg_temp_free_i64(tcg_tmp2);
10429 break;
10431 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10432 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10433 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10434 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10435 break;
10436 case 9: /* SQDMLAL, SQDMLAL2 */
10437 case 11: /* SQDMLSL, SQDMLSL2 */
10438 case 13: /* SQDMULL, SQDMULL2 */
10439 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10440 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10441 tcg_passres, tcg_passres);
10442 break;
10443 default:
10444 g_assert_not_reached();
10447 if (opcode == 9 || opcode == 11) {
10448 /* saturating accumulate ops */
10449 if (accop < 0) {
10450 tcg_gen_neg_i64(tcg_passres, tcg_passres);
10452 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10453 tcg_res[pass], tcg_passres);
10454 } else if (accop > 0) {
10455 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10456 } else if (accop < 0) {
10457 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10460 if (accop != 0) {
10461 tcg_temp_free_i64(tcg_passres);
10464 tcg_temp_free_i64(tcg_op1);
10465 tcg_temp_free_i64(tcg_op2);
10467 } else {
10468 /* size 0 or 1, generally helper functions */
10469 for (pass = 0; pass < 2; pass++) {
10470 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10471 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10472 TCGv_i64 tcg_passres;
10473 int elt = pass + is_q * 2;
10475 read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10476 read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10478 if (accop == 0) {
10479 tcg_passres = tcg_res[pass];
10480 } else {
10481 tcg_passres = tcg_temp_new_i64();
10484 switch (opcode) {
10485 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10486 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10488 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10489 static NeonGenWidenFn * const widenfns[2][2] = {
10490 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10491 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10493 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10495 widenfn(tcg_op2_64, tcg_op2);
10496 widenfn(tcg_passres, tcg_op1);
10497 gen_neon_addl(size, (opcode == 2), tcg_passres,
10498 tcg_passres, tcg_op2_64);
10499 tcg_temp_free_i64(tcg_op2_64);
10500 break;
10502 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10503 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10504 if (size == 0) {
10505 if (is_u) {
10506 gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10507 } else {
10508 gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10510 } else {
10511 if (is_u) {
10512 gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10513 } else {
10514 gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10517 break;
10518 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10519 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10520 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10521 if (size == 0) {
10522 if (is_u) {
10523 gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10524 } else {
10525 gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10527 } else {
10528 if (is_u) {
10529 gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10530 } else {
10531 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10534 break;
10535 case 9: /* SQDMLAL, SQDMLAL2 */
10536 case 11: /* SQDMLSL, SQDMLSL2 */
10537 case 13: /* SQDMULL, SQDMULL2 */
10538 assert(size == 1);
10539 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10540 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10541 tcg_passres, tcg_passres);
10542 break;
10543 case 14: /* PMULL */
10544 assert(size == 0);
10545 gen_helper_neon_mull_p8(tcg_passres, tcg_op1, tcg_op2);
10546 break;
10547 default:
10548 g_assert_not_reached();
10550 tcg_temp_free_i32(tcg_op1);
10551 tcg_temp_free_i32(tcg_op2);
10553 if (accop != 0) {
10554 if (opcode == 9 || opcode == 11) {
10555 /* saturating accumulate ops */
10556 if (accop < 0) {
10557 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10559 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10560 tcg_res[pass],
10561 tcg_passres);
10562 } else {
10563 gen_neon_addl(size, (accop < 0), tcg_res[pass],
10564 tcg_res[pass], tcg_passres);
10566 tcg_temp_free_i64(tcg_passres);
10571 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10572 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10573 tcg_temp_free_i64(tcg_res[0]);
10574 tcg_temp_free_i64(tcg_res[1]);
10577 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10578 int opcode, int rd, int rn, int rm)
10580 TCGv_i64 tcg_res[2];
10581 int part = is_q ? 2 : 0;
10582 int pass;
10584 for (pass = 0; pass < 2; pass++) {
10585 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10586 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10587 TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10588 static NeonGenWidenFn * const widenfns[3][2] = {
10589 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10590 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10591 { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10593 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10595 read_vec_element(s, tcg_op1, rn, pass, MO_64);
10596 read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10597 widenfn(tcg_op2_wide, tcg_op2);
10598 tcg_temp_free_i32(tcg_op2);
10599 tcg_res[pass] = tcg_temp_new_i64();
10600 gen_neon_addl(size, (opcode == 3),
10601 tcg_res[pass], tcg_op1, tcg_op2_wide);
10602 tcg_temp_free_i64(tcg_op1);
10603 tcg_temp_free_i64(tcg_op2_wide);
10606 for (pass = 0; pass < 2; pass++) {
10607 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10608 tcg_temp_free_i64(tcg_res[pass]);
10612 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10614 tcg_gen_addi_i64(in, in, 1U << 31);
10615 tcg_gen_extrh_i64_i32(res, in);
10618 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10619 int opcode, int rd, int rn, int rm)
10621 TCGv_i32 tcg_res[2];
10622 int part = is_q ? 2 : 0;
10623 int pass;
10625 for (pass = 0; pass < 2; pass++) {
10626 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10627 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10628 TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10629 static NeonGenNarrowFn * const narrowfns[3][2] = {
10630 { gen_helper_neon_narrow_high_u8,
10631 gen_helper_neon_narrow_round_high_u8 },
10632 { gen_helper_neon_narrow_high_u16,
10633 gen_helper_neon_narrow_round_high_u16 },
10634 { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10636 NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10638 read_vec_element(s, tcg_op1, rn, pass, MO_64);
10639 read_vec_element(s, tcg_op2, rm, pass, MO_64);
10641 gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10643 tcg_temp_free_i64(tcg_op1);
10644 tcg_temp_free_i64(tcg_op2);
10646 tcg_res[pass] = tcg_temp_new_i32();
10647 gennarrow(tcg_res[pass], tcg_wideres);
10648 tcg_temp_free_i64(tcg_wideres);
10651 for (pass = 0; pass < 2; pass++) {
10652 write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10653 tcg_temp_free_i32(tcg_res[pass]);
10655 clear_vec_high(s, is_q, rd);
10658 static void handle_pmull_64(DisasContext *s, int is_q, int rd, int rn, int rm)
10660 /* PMULL of 64 x 64 -> 128 is an odd special case because it
10661 * is the only three-reg-diff instruction which produces a
10662 * 128-bit wide result from a single operation. However since
10663 * it's possible to calculate the two halves more or less
10664 * separately we just use two helper calls.
10666 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10667 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10668 TCGv_i64 tcg_res = tcg_temp_new_i64();
10670 read_vec_element(s, tcg_op1, rn, is_q, MO_64);
10671 read_vec_element(s, tcg_op2, rm, is_q, MO_64);
10672 gen_helper_neon_pmull_64_lo(tcg_res, tcg_op1, tcg_op2);
10673 write_vec_element(s, tcg_res, rd, 0, MO_64);
10674 gen_helper_neon_pmull_64_hi(tcg_res, tcg_op1, tcg_op2);
10675 write_vec_element(s, tcg_res, rd, 1, MO_64);
10677 tcg_temp_free_i64(tcg_op1);
10678 tcg_temp_free_i64(tcg_op2);
10679 tcg_temp_free_i64(tcg_res);
10682 /* AdvSIMD three different
10683 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
10684 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10685 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
10686 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10688 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10690 /* Instructions in this group fall into three basic classes
10691 * (in each case with the operation working on each element in
10692 * the input vectors):
10693 * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10694 * 128 bit input)
10695 * (2) wide 64 x 128 -> 128
10696 * (3) narrowing 128 x 128 -> 64
10697 * Here we do initial decode, catch unallocated cases and
10698 * dispatch to separate functions for each class.
10700 int is_q = extract32(insn, 30, 1);
10701 int is_u = extract32(insn, 29, 1);
10702 int size = extract32(insn, 22, 2);
10703 int opcode = extract32(insn, 12, 4);
10704 int rm = extract32(insn, 16, 5);
10705 int rn = extract32(insn, 5, 5);
10706 int rd = extract32(insn, 0, 5);
10708 switch (opcode) {
10709 case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10710 case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10711 /* 64 x 128 -> 128 */
10712 if (size == 3) {
10713 unallocated_encoding(s);
10714 return;
10716 if (!fp_access_check(s)) {
10717 return;
10719 handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10720 break;
10721 case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10722 case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10723 /* 128 x 128 -> 64 */
10724 if (size == 3) {
10725 unallocated_encoding(s);
10726 return;
10728 if (!fp_access_check(s)) {
10729 return;
10731 handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10732 break;
10733 case 14: /* PMULL, PMULL2 */
10734 if (is_u || size == 1 || size == 2) {
10735 unallocated_encoding(s);
10736 return;
10738 if (size == 3) {
10739 if (!dc_isar_feature(aa64_pmull, s)) {
10740 unallocated_encoding(s);
10741 return;
10743 if (!fp_access_check(s)) {
10744 return;
10746 handle_pmull_64(s, is_q, rd, rn, rm);
10747 return;
10749 goto is_widening;
10750 case 9: /* SQDMLAL, SQDMLAL2 */
10751 case 11: /* SQDMLSL, SQDMLSL2 */
10752 case 13: /* SQDMULL, SQDMULL2 */
10753 if (is_u || size == 0) {
10754 unallocated_encoding(s);
10755 return;
10757 /* fall through */
10758 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10759 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10760 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10761 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10762 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10763 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10764 case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10765 /* 64 x 64 -> 128 */
10766 if (size == 3) {
10767 unallocated_encoding(s);
10768 return;
10770 is_widening:
10771 if (!fp_access_check(s)) {
10772 return;
10775 handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10776 break;
10777 default:
10778 /* opcode 15 not allocated */
10779 unallocated_encoding(s);
10780 break;
10784 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10785 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10787 int rd = extract32(insn, 0, 5);
10788 int rn = extract32(insn, 5, 5);
10789 int rm = extract32(insn, 16, 5);
10790 int size = extract32(insn, 22, 2);
10791 bool is_u = extract32(insn, 29, 1);
10792 bool is_q = extract32(insn, 30, 1);
10794 if (!fp_access_check(s)) {
10795 return;
10798 switch (size + 4 * is_u) {
10799 case 0: /* AND */
10800 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10801 return;
10802 case 1: /* BIC */
10803 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10804 return;
10805 case 2: /* ORR */
10806 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10807 return;
10808 case 3: /* ORN */
10809 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10810 return;
10811 case 4: /* EOR */
10812 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10813 return;
10815 case 5: /* BSL bitwise select */
10816 gen_gvec_op3(s, is_q, rd, rn, rm, &bsl_op);
10817 return;
10818 case 6: /* BIT, bitwise insert if true */
10819 gen_gvec_op3(s, is_q, rd, rn, rm, &bit_op);
10820 return;
10821 case 7: /* BIF, bitwise insert if false */
10822 gen_gvec_op3(s, is_q, rd, rn, rm, &bif_op);
10823 return;
10825 default:
10826 g_assert_not_reached();
10830 /* Pairwise op subgroup of C3.6.16.
10832 * This is called directly or via the handle_3same_float for float pairwise
10833 * operations where the opcode and size are calculated differently.
10835 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
10836 int size, int rn, int rm, int rd)
10838 TCGv_ptr fpst;
10839 int pass;
10841 /* Floating point operations need fpst */
10842 if (opcode >= 0x58) {
10843 fpst = get_fpstatus_ptr(false);
10844 } else {
10845 fpst = NULL;
10848 if (!fp_access_check(s)) {
10849 return;
10852 /* These operations work on the concatenated rm:rn, with each pair of
10853 * adjacent elements being operated on to produce an element in the result.
10855 if (size == 3) {
10856 TCGv_i64 tcg_res[2];
10858 for (pass = 0; pass < 2; pass++) {
10859 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10860 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10861 int passreg = (pass == 0) ? rn : rm;
10863 read_vec_element(s, tcg_op1, passreg, 0, MO_64);
10864 read_vec_element(s, tcg_op2, passreg, 1, MO_64);
10865 tcg_res[pass] = tcg_temp_new_i64();
10867 switch (opcode) {
10868 case 0x17: /* ADDP */
10869 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
10870 break;
10871 case 0x58: /* FMAXNMP */
10872 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10873 break;
10874 case 0x5a: /* FADDP */
10875 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10876 break;
10877 case 0x5e: /* FMAXP */
10878 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10879 break;
10880 case 0x78: /* FMINNMP */
10881 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10882 break;
10883 case 0x7e: /* FMINP */
10884 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10885 break;
10886 default:
10887 g_assert_not_reached();
10890 tcg_temp_free_i64(tcg_op1);
10891 tcg_temp_free_i64(tcg_op2);
10894 for (pass = 0; pass < 2; pass++) {
10895 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10896 tcg_temp_free_i64(tcg_res[pass]);
10898 } else {
10899 int maxpass = is_q ? 4 : 2;
10900 TCGv_i32 tcg_res[4];
10902 for (pass = 0; pass < maxpass; pass++) {
10903 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10904 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10905 NeonGenTwoOpFn *genfn = NULL;
10906 int passreg = pass < (maxpass / 2) ? rn : rm;
10907 int passelt = (is_q && (pass & 1)) ? 2 : 0;
10909 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
10910 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
10911 tcg_res[pass] = tcg_temp_new_i32();
10913 switch (opcode) {
10914 case 0x17: /* ADDP */
10916 static NeonGenTwoOpFn * const fns[3] = {
10917 gen_helper_neon_padd_u8,
10918 gen_helper_neon_padd_u16,
10919 tcg_gen_add_i32,
10921 genfn = fns[size];
10922 break;
10924 case 0x14: /* SMAXP, UMAXP */
10926 static NeonGenTwoOpFn * const fns[3][2] = {
10927 { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
10928 { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
10929 { tcg_gen_smax_i32, tcg_gen_umax_i32 },
10931 genfn = fns[size][u];
10932 break;
10934 case 0x15: /* SMINP, UMINP */
10936 static NeonGenTwoOpFn * const fns[3][2] = {
10937 { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
10938 { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
10939 { tcg_gen_smin_i32, tcg_gen_umin_i32 },
10941 genfn = fns[size][u];
10942 break;
10944 /* The FP operations are all on single floats (32 bit) */
10945 case 0x58: /* FMAXNMP */
10946 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10947 break;
10948 case 0x5a: /* FADDP */
10949 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10950 break;
10951 case 0x5e: /* FMAXP */
10952 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10953 break;
10954 case 0x78: /* FMINNMP */
10955 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10956 break;
10957 case 0x7e: /* FMINP */
10958 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10959 break;
10960 default:
10961 g_assert_not_reached();
10964 /* FP ops called directly, otherwise call now */
10965 if (genfn) {
10966 genfn(tcg_res[pass], tcg_op1, tcg_op2);
10969 tcg_temp_free_i32(tcg_op1);
10970 tcg_temp_free_i32(tcg_op2);
10973 for (pass = 0; pass < maxpass; pass++) {
10974 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
10975 tcg_temp_free_i32(tcg_res[pass]);
10977 clear_vec_high(s, is_q, rd);
10980 if (fpst) {
10981 tcg_temp_free_ptr(fpst);
10985 /* Floating point op subgroup of C3.6.16. */
10986 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
10988 /* For floating point ops, the U, size[1] and opcode bits
10989 * together indicate the operation. size[0] indicates single
10990 * or double.
10992 int fpopcode = extract32(insn, 11, 5)
10993 | (extract32(insn, 23, 1) << 5)
10994 | (extract32(insn, 29, 1) << 6);
10995 int is_q = extract32(insn, 30, 1);
10996 int size = extract32(insn, 22, 1);
10997 int rm = extract32(insn, 16, 5);
10998 int rn = extract32(insn, 5, 5);
10999 int rd = extract32(insn, 0, 5);
11001 int datasize = is_q ? 128 : 64;
11002 int esize = 32 << size;
11003 int elements = datasize / esize;
11005 if (size == 1 && !is_q) {
11006 unallocated_encoding(s);
11007 return;
11010 switch (fpopcode) {
11011 case 0x58: /* FMAXNMP */
11012 case 0x5a: /* FADDP */
11013 case 0x5e: /* FMAXP */
11014 case 0x78: /* FMINNMP */
11015 case 0x7e: /* FMINP */
11016 if (size && !is_q) {
11017 unallocated_encoding(s);
11018 return;
11020 handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11021 rn, rm, rd);
11022 return;
11023 case 0x1b: /* FMULX */
11024 case 0x1f: /* FRECPS */
11025 case 0x3f: /* FRSQRTS */
11026 case 0x5d: /* FACGE */
11027 case 0x7d: /* FACGT */
11028 case 0x19: /* FMLA */
11029 case 0x39: /* FMLS */
11030 case 0x18: /* FMAXNM */
11031 case 0x1a: /* FADD */
11032 case 0x1c: /* FCMEQ */
11033 case 0x1e: /* FMAX */
11034 case 0x38: /* FMINNM */
11035 case 0x3a: /* FSUB */
11036 case 0x3e: /* FMIN */
11037 case 0x5b: /* FMUL */
11038 case 0x5c: /* FCMGE */
11039 case 0x5f: /* FDIV */
11040 case 0x7a: /* FABD */
11041 case 0x7c: /* FCMGT */
11042 if (!fp_access_check(s)) {
11043 return;
11045 handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11046 return;
11048 case 0x1d: /* FMLAL */
11049 case 0x3d: /* FMLSL */
11050 case 0x59: /* FMLAL2 */
11051 case 0x79: /* FMLSL2 */
11052 if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11053 unallocated_encoding(s);
11054 return;
11056 if (fp_access_check(s)) {
11057 int is_s = extract32(insn, 23, 1);
11058 int is_2 = extract32(insn, 29, 1);
11059 int data = (is_2 << 1) | is_s;
11060 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11061 vec_full_reg_offset(s, rn),
11062 vec_full_reg_offset(s, rm), cpu_env,
11063 is_q ? 16 : 8, vec_full_reg_size(s),
11064 data, gen_helper_gvec_fmlal_a64);
11066 return;
11068 default:
11069 unallocated_encoding(s);
11070 return;
11074 /* Integer op subgroup of C3.6.16. */
11075 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11077 int is_q = extract32(insn, 30, 1);
11078 int u = extract32(insn, 29, 1);
11079 int size = extract32(insn, 22, 2);
11080 int opcode = extract32(insn, 11, 5);
11081 int rm = extract32(insn, 16, 5);
11082 int rn = extract32(insn, 5, 5);
11083 int rd = extract32(insn, 0, 5);
11084 int pass;
11085 TCGCond cond;
11087 switch (opcode) {
11088 case 0x13: /* MUL, PMUL */
11089 if (u && size != 0) {
11090 unallocated_encoding(s);
11091 return;
11093 /* fall through */
11094 case 0x0: /* SHADD, UHADD */
11095 case 0x2: /* SRHADD, URHADD */
11096 case 0x4: /* SHSUB, UHSUB */
11097 case 0xc: /* SMAX, UMAX */
11098 case 0xd: /* SMIN, UMIN */
11099 case 0xe: /* SABD, UABD */
11100 case 0xf: /* SABA, UABA */
11101 case 0x12: /* MLA, MLS */
11102 if (size == 3) {
11103 unallocated_encoding(s);
11104 return;
11106 break;
11107 case 0x16: /* SQDMULH, SQRDMULH */
11108 if (size == 0 || size == 3) {
11109 unallocated_encoding(s);
11110 return;
11112 break;
11113 default:
11114 if (size == 3 && !is_q) {
11115 unallocated_encoding(s);
11116 return;
11118 break;
11121 if (!fp_access_check(s)) {
11122 return;
11125 switch (opcode) {
11126 case 0x01: /* SQADD, UQADD */
11127 tcg_gen_gvec_4(vec_full_reg_offset(s, rd),
11128 offsetof(CPUARMState, vfp.qc),
11129 vec_full_reg_offset(s, rn),
11130 vec_full_reg_offset(s, rm),
11131 is_q ? 16 : 8, vec_full_reg_size(s),
11132 (u ? uqadd_op : sqadd_op) + size);
11133 return;
11134 case 0x05: /* SQSUB, UQSUB */
11135 tcg_gen_gvec_4(vec_full_reg_offset(s, rd),
11136 offsetof(CPUARMState, vfp.qc),
11137 vec_full_reg_offset(s, rn),
11138 vec_full_reg_offset(s, rm),
11139 is_q ? 16 : 8, vec_full_reg_size(s),
11140 (u ? uqsub_op : sqsub_op) + size);
11141 return;
11142 case 0x0c: /* SMAX, UMAX */
11143 if (u) {
11144 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11145 } else {
11146 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11148 return;
11149 case 0x0d: /* SMIN, UMIN */
11150 if (u) {
11151 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11152 } else {
11153 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11155 return;
11156 case 0x10: /* ADD, SUB */
11157 if (u) {
11158 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11159 } else {
11160 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11162 return;
11163 case 0x13: /* MUL, PMUL */
11164 if (!u) { /* MUL */
11165 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11166 return;
11168 break;
11169 case 0x12: /* MLA, MLS */
11170 if (u) {
11171 gen_gvec_op3(s, is_q, rd, rn, rm, &mls_op[size]);
11172 } else {
11173 gen_gvec_op3(s, is_q, rd, rn, rm, &mla_op[size]);
11175 return;
11176 case 0x11:
11177 if (!u) { /* CMTST */
11178 gen_gvec_op3(s, is_q, rd, rn, rm, &cmtst_op[size]);
11179 return;
11181 /* else CMEQ */
11182 cond = TCG_COND_EQ;
11183 goto do_gvec_cmp;
11184 case 0x06: /* CMGT, CMHI */
11185 cond = u ? TCG_COND_GTU : TCG_COND_GT;
11186 goto do_gvec_cmp;
11187 case 0x07: /* CMGE, CMHS */
11188 cond = u ? TCG_COND_GEU : TCG_COND_GE;
11189 do_gvec_cmp:
11190 tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11191 vec_full_reg_offset(s, rn),
11192 vec_full_reg_offset(s, rm),
11193 is_q ? 16 : 8, vec_full_reg_size(s));
11194 return;
11197 if (size == 3) {
11198 assert(is_q);
11199 for (pass = 0; pass < 2; pass++) {
11200 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11201 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11202 TCGv_i64 tcg_res = tcg_temp_new_i64();
11204 read_vec_element(s, tcg_op1, rn, pass, MO_64);
11205 read_vec_element(s, tcg_op2, rm, pass, MO_64);
11207 handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11209 write_vec_element(s, tcg_res, rd, pass, MO_64);
11211 tcg_temp_free_i64(tcg_res);
11212 tcg_temp_free_i64(tcg_op1);
11213 tcg_temp_free_i64(tcg_op2);
11215 } else {
11216 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11217 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11218 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11219 TCGv_i32 tcg_res = tcg_temp_new_i32();
11220 NeonGenTwoOpFn *genfn = NULL;
11221 NeonGenTwoOpEnvFn *genenvfn = NULL;
11223 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11224 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11226 switch (opcode) {
11227 case 0x0: /* SHADD, UHADD */
11229 static NeonGenTwoOpFn * const fns[3][2] = {
11230 { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11231 { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11232 { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11234 genfn = fns[size][u];
11235 break;
11237 case 0x2: /* SRHADD, URHADD */
11239 static NeonGenTwoOpFn * const fns[3][2] = {
11240 { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11241 { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11242 { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11244 genfn = fns[size][u];
11245 break;
11247 case 0x4: /* SHSUB, UHSUB */
11249 static NeonGenTwoOpFn * const fns[3][2] = {
11250 { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11251 { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11252 { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11254 genfn = fns[size][u];
11255 break;
11257 case 0x8: /* SSHL, USHL */
11259 static NeonGenTwoOpFn * const fns[3][2] = {
11260 { gen_helper_neon_shl_s8, gen_helper_neon_shl_u8 },
11261 { gen_helper_neon_shl_s16, gen_helper_neon_shl_u16 },
11262 { gen_helper_neon_shl_s32, gen_helper_neon_shl_u32 },
11264 genfn = fns[size][u];
11265 break;
11267 case 0x9: /* SQSHL, UQSHL */
11269 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11270 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11271 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11272 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11274 genenvfn = fns[size][u];
11275 break;
11277 case 0xa: /* SRSHL, URSHL */
11279 static NeonGenTwoOpFn * const fns[3][2] = {
11280 { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11281 { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11282 { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11284 genfn = fns[size][u];
11285 break;
11287 case 0xb: /* SQRSHL, UQRSHL */
11289 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11290 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11291 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11292 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11294 genenvfn = fns[size][u];
11295 break;
11297 case 0xe: /* SABD, UABD */
11298 case 0xf: /* SABA, UABA */
11300 static NeonGenTwoOpFn * const fns[3][2] = {
11301 { gen_helper_neon_abd_s8, gen_helper_neon_abd_u8 },
11302 { gen_helper_neon_abd_s16, gen_helper_neon_abd_u16 },
11303 { gen_helper_neon_abd_s32, gen_helper_neon_abd_u32 },
11305 genfn = fns[size][u];
11306 break;
11308 case 0x13: /* MUL, PMUL */
11309 assert(u); /* PMUL */
11310 assert(size == 0);
11311 genfn = gen_helper_neon_mul_p8;
11312 break;
11313 case 0x16: /* SQDMULH, SQRDMULH */
11315 static NeonGenTwoOpEnvFn * const fns[2][2] = {
11316 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
11317 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
11319 assert(size == 1 || size == 2);
11320 genenvfn = fns[size - 1][u];
11321 break;
11323 default:
11324 g_assert_not_reached();
11327 if (genenvfn) {
11328 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11329 } else {
11330 genfn(tcg_res, tcg_op1, tcg_op2);
11333 if (opcode == 0xf) {
11334 /* SABA, UABA: accumulating ops */
11335 static NeonGenTwoOpFn * const fns[3] = {
11336 gen_helper_neon_add_u8,
11337 gen_helper_neon_add_u16,
11338 tcg_gen_add_i32,
11341 read_vec_element_i32(s, tcg_op1, rd, pass, MO_32);
11342 fns[size](tcg_res, tcg_op1, tcg_res);
11345 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11347 tcg_temp_free_i32(tcg_res);
11348 tcg_temp_free_i32(tcg_op1);
11349 tcg_temp_free_i32(tcg_op2);
11352 clear_vec_high(s, is_q, rd);
11355 /* AdvSIMD three same
11356 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
11357 * +---+---+---+-----------+------+---+------+--------+---+------+------+
11358 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
11359 * +---+---+---+-----------+------+---+------+--------+---+------+------+
11361 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11363 int opcode = extract32(insn, 11, 5);
11365 switch (opcode) {
11366 case 0x3: /* logic ops */
11367 disas_simd_3same_logic(s, insn);
11368 break;
11369 case 0x17: /* ADDP */
11370 case 0x14: /* SMAXP, UMAXP */
11371 case 0x15: /* SMINP, UMINP */
11373 /* Pairwise operations */
11374 int is_q = extract32(insn, 30, 1);
11375 int u = extract32(insn, 29, 1);
11376 int size = extract32(insn, 22, 2);
11377 int rm = extract32(insn, 16, 5);
11378 int rn = extract32(insn, 5, 5);
11379 int rd = extract32(insn, 0, 5);
11380 if (opcode == 0x17) {
11381 if (u || (size == 3 && !is_q)) {
11382 unallocated_encoding(s);
11383 return;
11385 } else {
11386 if (size == 3) {
11387 unallocated_encoding(s);
11388 return;
11391 handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11392 break;
11394 case 0x18 ... 0x31:
11395 /* floating point ops, sz[1] and U are part of opcode */
11396 disas_simd_3same_float(s, insn);
11397 break;
11398 default:
11399 disas_simd_3same_int(s, insn);
11400 break;
11405 * Advanced SIMD three same (ARMv8.2 FP16 variants)
11407 * 31 30 29 28 24 23 22 21 20 16 15 14 13 11 10 9 5 4 0
11408 * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11409 * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 | Rm | 0 0 | opcode | 1 | Rn | Rd |
11410 * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11412 * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11413 * (register), FACGE, FABD, FCMGT (register) and FACGT.
11416 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11418 int opcode, fpopcode;
11419 int is_q, u, a, rm, rn, rd;
11420 int datasize, elements;
11421 int pass;
11422 TCGv_ptr fpst;
11423 bool pairwise = false;
11425 if (!dc_isar_feature(aa64_fp16, s)) {
11426 unallocated_encoding(s);
11427 return;
11430 if (!fp_access_check(s)) {
11431 return;
11434 /* For these floating point ops, the U, a and opcode bits
11435 * together indicate the operation.
11437 opcode = extract32(insn, 11, 3);
11438 u = extract32(insn, 29, 1);
11439 a = extract32(insn, 23, 1);
11440 is_q = extract32(insn, 30, 1);
11441 rm = extract32(insn, 16, 5);
11442 rn = extract32(insn, 5, 5);
11443 rd = extract32(insn, 0, 5);
11445 fpopcode = opcode | (a << 3) | (u << 4);
11446 datasize = is_q ? 128 : 64;
11447 elements = datasize / 16;
11449 switch (fpopcode) {
11450 case 0x10: /* FMAXNMP */
11451 case 0x12: /* FADDP */
11452 case 0x16: /* FMAXP */
11453 case 0x18: /* FMINNMP */
11454 case 0x1e: /* FMINP */
11455 pairwise = true;
11456 break;
11459 fpst = get_fpstatus_ptr(true);
11461 if (pairwise) {
11462 int maxpass = is_q ? 8 : 4;
11463 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11464 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11465 TCGv_i32 tcg_res[8];
11467 for (pass = 0; pass < maxpass; pass++) {
11468 int passreg = pass < (maxpass / 2) ? rn : rm;
11469 int passelt = (pass << 1) & (maxpass - 1);
11471 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11472 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11473 tcg_res[pass] = tcg_temp_new_i32();
11475 switch (fpopcode) {
11476 case 0x10: /* FMAXNMP */
11477 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11478 fpst);
11479 break;
11480 case 0x12: /* FADDP */
11481 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11482 break;
11483 case 0x16: /* FMAXP */
11484 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11485 break;
11486 case 0x18: /* FMINNMP */
11487 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11488 fpst);
11489 break;
11490 case 0x1e: /* FMINP */
11491 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11492 break;
11493 default:
11494 g_assert_not_reached();
11498 for (pass = 0; pass < maxpass; pass++) {
11499 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11500 tcg_temp_free_i32(tcg_res[pass]);
11503 tcg_temp_free_i32(tcg_op1);
11504 tcg_temp_free_i32(tcg_op2);
11506 } else {
11507 for (pass = 0; pass < elements; pass++) {
11508 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11509 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11510 TCGv_i32 tcg_res = tcg_temp_new_i32();
11512 read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11513 read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11515 switch (fpopcode) {
11516 case 0x0: /* FMAXNM */
11517 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11518 break;
11519 case 0x1: /* FMLA */
11520 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11521 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11522 fpst);
11523 break;
11524 case 0x2: /* FADD */
11525 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11526 break;
11527 case 0x3: /* FMULX */
11528 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11529 break;
11530 case 0x4: /* FCMEQ */
11531 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11532 break;
11533 case 0x6: /* FMAX */
11534 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11535 break;
11536 case 0x7: /* FRECPS */
11537 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11538 break;
11539 case 0x8: /* FMINNM */
11540 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11541 break;
11542 case 0x9: /* FMLS */
11543 /* As usual for ARM, separate negation for fused multiply-add */
11544 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11545 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11546 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11547 fpst);
11548 break;
11549 case 0xa: /* FSUB */
11550 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11551 break;
11552 case 0xe: /* FMIN */
11553 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11554 break;
11555 case 0xf: /* FRSQRTS */
11556 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11557 break;
11558 case 0x13: /* FMUL */
11559 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11560 break;
11561 case 0x14: /* FCMGE */
11562 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11563 break;
11564 case 0x15: /* FACGE */
11565 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11566 break;
11567 case 0x17: /* FDIV */
11568 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11569 break;
11570 case 0x1a: /* FABD */
11571 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11572 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11573 break;
11574 case 0x1c: /* FCMGT */
11575 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11576 break;
11577 case 0x1d: /* FACGT */
11578 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11579 break;
11580 default:
11581 fprintf(stderr, "%s: insn %#04x, fpop %#2x @ %#" PRIx64 "\n",
11582 __func__, insn, fpopcode, s->pc);
11583 g_assert_not_reached();
11586 write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11587 tcg_temp_free_i32(tcg_res);
11588 tcg_temp_free_i32(tcg_op1);
11589 tcg_temp_free_i32(tcg_op2);
11593 tcg_temp_free_ptr(fpst);
11595 clear_vec_high(s, is_q, rd);
11598 /* AdvSIMD three same extra
11599 * 31 30 29 28 24 23 22 21 20 16 15 14 11 10 9 5 4 0
11600 * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11601 * | 0 | Q | U | 0 1 1 1 0 | size | 0 | Rm | 1 | opcode | 1 | Rn | Rd |
11602 * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11604 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11606 int rd = extract32(insn, 0, 5);
11607 int rn = extract32(insn, 5, 5);
11608 int opcode = extract32(insn, 11, 4);
11609 int rm = extract32(insn, 16, 5);
11610 int size = extract32(insn, 22, 2);
11611 bool u = extract32(insn, 29, 1);
11612 bool is_q = extract32(insn, 30, 1);
11613 bool feature;
11614 int rot;
11616 switch (u * 16 + opcode) {
11617 case 0x10: /* SQRDMLAH (vector) */
11618 case 0x11: /* SQRDMLSH (vector) */
11619 if (size != 1 && size != 2) {
11620 unallocated_encoding(s);
11621 return;
11623 feature = dc_isar_feature(aa64_rdm, s);
11624 break;
11625 case 0x02: /* SDOT (vector) */
11626 case 0x12: /* UDOT (vector) */
11627 if (size != MO_32) {
11628 unallocated_encoding(s);
11629 return;
11631 feature = dc_isar_feature(aa64_dp, s);
11632 break;
11633 case 0x18: /* FCMLA, #0 */
11634 case 0x19: /* FCMLA, #90 */
11635 case 0x1a: /* FCMLA, #180 */
11636 case 0x1b: /* FCMLA, #270 */
11637 case 0x1c: /* FCADD, #90 */
11638 case 0x1e: /* FCADD, #270 */
11639 if (size == 0
11640 || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11641 || (size == 3 && !is_q)) {
11642 unallocated_encoding(s);
11643 return;
11645 feature = dc_isar_feature(aa64_fcma, s);
11646 break;
11647 default:
11648 unallocated_encoding(s);
11649 return;
11651 if (!feature) {
11652 unallocated_encoding(s);
11653 return;
11655 if (!fp_access_check(s)) {
11656 return;
11659 switch (opcode) {
11660 case 0x0: /* SQRDMLAH (vector) */
11661 switch (size) {
11662 case 1:
11663 gen_gvec_op3_env(s, is_q, rd, rn, rm, gen_helper_gvec_qrdmlah_s16);
11664 break;
11665 case 2:
11666 gen_gvec_op3_env(s, is_q, rd, rn, rm, gen_helper_gvec_qrdmlah_s32);
11667 break;
11668 default:
11669 g_assert_not_reached();
11671 return;
11673 case 0x1: /* SQRDMLSH (vector) */
11674 switch (size) {
11675 case 1:
11676 gen_gvec_op3_env(s, is_q, rd, rn, rm, gen_helper_gvec_qrdmlsh_s16);
11677 break;
11678 case 2:
11679 gen_gvec_op3_env(s, is_q, rd, rn, rm, gen_helper_gvec_qrdmlsh_s32);
11680 break;
11681 default:
11682 g_assert_not_reached();
11684 return;
11686 case 0x2: /* SDOT / UDOT */
11687 gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0,
11688 u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11689 return;
11691 case 0x8: /* FCMLA, #0 */
11692 case 0x9: /* FCMLA, #90 */
11693 case 0xa: /* FCMLA, #180 */
11694 case 0xb: /* FCMLA, #270 */
11695 rot = extract32(opcode, 0, 2);
11696 switch (size) {
11697 case 1:
11698 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, true, rot,
11699 gen_helper_gvec_fcmlah);
11700 break;
11701 case 2:
11702 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, false, rot,
11703 gen_helper_gvec_fcmlas);
11704 break;
11705 case 3:
11706 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, false, rot,
11707 gen_helper_gvec_fcmlad);
11708 break;
11709 default:
11710 g_assert_not_reached();
11712 return;
11714 case 0xc: /* FCADD, #90 */
11715 case 0xe: /* FCADD, #270 */
11716 rot = extract32(opcode, 1, 1);
11717 switch (size) {
11718 case 1:
11719 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11720 gen_helper_gvec_fcaddh);
11721 break;
11722 case 2:
11723 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11724 gen_helper_gvec_fcadds);
11725 break;
11726 case 3:
11727 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11728 gen_helper_gvec_fcaddd);
11729 break;
11730 default:
11731 g_assert_not_reached();
11733 return;
11735 default:
11736 g_assert_not_reached();
11740 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11741 int size, int rn, int rd)
11743 /* Handle 2-reg-misc ops which are widening (so each size element
11744 * in the source becomes a 2*size element in the destination.
11745 * The only instruction like this is FCVTL.
11747 int pass;
11749 if (size == 3) {
11750 /* 32 -> 64 bit fp conversion */
11751 TCGv_i64 tcg_res[2];
11752 int srcelt = is_q ? 2 : 0;
11754 for (pass = 0; pass < 2; pass++) {
11755 TCGv_i32 tcg_op = tcg_temp_new_i32();
11756 tcg_res[pass] = tcg_temp_new_i64();
11758 read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11759 gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
11760 tcg_temp_free_i32(tcg_op);
11762 for (pass = 0; pass < 2; pass++) {
11763 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11764 tcg_temp_free_i64(tcg_res[pass]);
11766 } else {
11767 /* 16 -> 32 bit fp conversion */
11768 int srcelt = is_q ? 4 : 0;
11769 TCGv_i32 tcg_res[4];
11770 TCGv_ptr fpst = get_fpstatus_ptr(false);
11771 TCGv_i32 ahp = get_ahp_flag();
11773 for (pass = 0; pass < 4; pass++) {
11774 tcg_res[pass] = tcg_temp_new_i32();
11776 read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11777 gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11778 fpst, ahp);
11780 for (pass = 0; pass < 4; pass++) {
11781 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11782 tcg_temp_free_i32(tcg_res[pass]);
11785 tcg_temp_free_ptr(fpst);
11786 tcg_temp_free_i32(ahp);
11790 static void handle_rev(DisasContext *s, int opcode, bool u,
11791 bool is_q, int size, int rn, int rd)
11793 int op = (opcode << 1) | u;
11794 int opsz = op + size;
11795 int grp_size = 3 - opsz;
11796 int dsize = is_q ? 128 : 64;
11797 int i;
11799 if (opsz >= 3) {
11800 unallocated_encoding(s);
11801 return;
11804 if (!fp_access_check(s)) {
11805 return;
11808 if (size == 0) {
11809 /* Special case bytes, use bswap op on each group of elements */
11810 int groups = dsize / (8 << grp_size);
11812 for (i = 0; i < groups; i++) {
11813 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11815 read_vec_element(s, tcg_tmp, rn, i, grp_size);
11816 switch (grp_size) {
11817 case MO_16:
11818 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
11819 break;
11820 case MO_32:
11821 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
11822 break;
11823 case MO_64:
11824 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11825 break;
11826 default:
11827 g_assert_not_reached();
11829 write_vec_element(s, tcg_tmp, rd, i, grp_size);
11830 tcg_temp_free_i64(tcg_tmp);
11832 clear_vec_high(s, is_q, rd);
11833 } else {
11834 int revmask = (1 << grp_size) - 1;
11835 int esize = 8 << size;
11836 int elements = dsize / esize;
11837 TCGv_i64 tcg_rn = tcg_temp_new_i64();
11838 TCGv_i64 tcg_rd = tcg_const_i64(0);
11839 TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
11841 for (i = 0; i < elements; i++) {
11842 int e_rev = (i & 0xf) ^ revmask;
11843 int off = e_rev * esize;
11844 read_vec_element(s, tcg_rn, rn, i, size);
11845 if (off >= 64) {
11846 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
11847 tcg_rn, off - 64, esize);
11848 } else {
11849 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
11852 write_vec_element(s, tcg_rd, rd, 0, MO_64);
11853 write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
11855 tcg_temp_free_i64(tcg_rd_hi);
11856 tcg_temp_free_i64(tcg_rd);
11857 tcg_temp_free_i64(tcg_rn);
11861 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
11862 bool is_q, int size, int rn, int rd)
11864 /* Implement the pairwise operations from 2-misc:
11865 * SADDLP, UADDLP, SADALP, UADALP.
11866 * These all add pairs of elements in the input to produce a
11867 * double-width result element in the output (possibly accumulating).
11869 bool accum = (opcode == 0x6);
11870 int maxpass = is_q ? 2 : 1;
11871 int pass;
11872 TCGv_i64 tcg_res[2];
11874 if (size == 2) {
11875 /* 32 + 32 -> 64 op */
11876 TCGMemOp memop = size + (u ? 0 : MO_SIGN);
11878 for (pass = 0; pass < maxpass; pass++) {
11879 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11880 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11882 tcg_res[pass] = tcg_temp_new_i64();
11884 read_vec_element(s, tcg_op1, rn, pass * 2, memop);
11885 read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
11886 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11887 if (accum) {
11888 read_vec_element(s, tcg_op1, rd, pass, MO_64);
11889 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
11892 tcg_temp_free_i64(tcg_op1);
11893 tcg_temp_free_i64(tcg_op2);
11895 } else {
11896 for (pass = 0; pass < maxpass; pass++) {
11897 TCGv_i64 tcg_op = tcg_temp_new_i64();
11898 NeonGenOneOpFn *genfn;
11899 static NeonGenOneOpFn * const fns[2][2] = {
11900 { gen_helper_neon_addlp_s8, gen_helper_neon_addlp_u8 },
11901 { gen_helper_neon_addlp_s16, gen_helper_neon_addlp_u16 },
11904 genfn = fns[size][u];
11906 tcg_res[pass] = tcg_temp_new_i64();
11908 read_vec_element(s, tcg_op, rn, pass, MO_64);
11909 genfn(tcg_res[pass], tcg_op);
11911 if (accum) {
11912 read_vec_element(s, tcg_op, rd, pass, MO_64);
11913 if (size == 0) {
11914 gen_helper_neon_addl_u16(tcg_res[pass],
11915 tcg_res[pass], tcg_op);
11916 } else {
11917 gen_helper_neon_addl_u32(tcg_res[pass],
11918 tcg_res[pass], tcg_op);
11921 tcg_temp_free_i64(tcg_op);
11924 if (!is_q) {
11925 tcg_res[1] = tcg_const_i64(0);
11927 for (pass = 0; pass < 2; pass++) {
11928 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11929 tcg_temp_free_i64(tcg_res[pass]);
11933 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
11935 /* Implement SHLL and SHLL2 */
11936 int pass;
11937 int part = is_q ? 2 : 0;
11938 TCGv_i64 tcg_res[2];
11940 for (pass = 0; pass < 2; pass++) {
11941 static NeonGenWidenFn * const widenfns[3] = {
11942 gen_helper_neon_widen_u8,
11943 gen_helper_neon_widen_u16,
11944 tcg_gen_extu_i32_i64,
11946 NeonGenWidenFn *widenfn = widenfns[size];
11947 TCGv_i32 tcg_op = tcg_temp_new_i32();
11949 read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
11950 tcg_res[pass] = tcg_temp_new_i64();
11951 widenfn(tcg_res[pass], tcg_op);
11952 tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
11954 tcg_temp_free_i32(tcg_op);
11957 for (pass = 0; pass < 2; pass++) {
11958 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11959 tcg_temp_free_i64(tcg_res[pass]);
11963 /* AdvSIMD two reg misc
11964 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
11965 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11966 * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
11967 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11969 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
11971 int size = extract32(insn, 22, 2);
11972 int opcode = extract32(insn, 12, 5);
11973 bool u = extract32(insn, 29, 1);
11974 bool is_q = extract32(insn, 30, 1);
11975 int rn = extract32(insn, 5, 5);
11976 int rd = extract32(insn, 0, 5);
11977 bool need_fpstatus = false;
11978 bool need_rmode = false;
11979 int rmode = -1;
11980 TCGv_i32 tcg_rmode;
11981 TCGv_ptr tcg_fpstatus;
11983 switch (opcode) {
11984 case 0x0: /* REV64, REV32 */
11985 case 0x1: /* REV16 */
11986 handle_rev(s, opcode, u, is_q, size, rn, rd);
11987 return;
11988 case 0x5: /* CNT, NOT, RBIT */
11989 if (u && size == 0) {
11990 /* NOT */
11991 break;
11992 } else if (u && size == 1) {
11993 /* RBIT */
11994 break;
11995 } else if (!u && size == 0) {
11996 /* CNT */
11997 break;
11999 unallocated_encoding(s);
12000 return;
12001 case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12002 case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12003 if (size == 3) {
12004 unallocated_encoding(s);
12005 return;
12007 if (!fp_access_check(s)) {
12008 return;
12011 handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
12012 return;
12013 case 0x4: /* CLS, CLZ */
12014 if (size == 3) {
12015 unallocated_encoding(s);
12016 return;
12018 break;
12019 case 0x2: /* SADDLP, UADDLP */
12020 case 0x6: /* SADALP, UADALP */
12021 if (size == 3) {
12022 unallocated_encoding(s);
12023 return;
12025 if (!fp_access_check(s)) {
12026 return;
12028 handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
12029 return;
12030 case 0x13: /* SHLL, SHLL2 */
12031 if (u == 0 || size == 3) {
12032 unallocated_encoding(s);
12033 return;
12035 if (!fp_access_check(s)) {
12036 return;
12038 handle_shll(s, is_q, size, rn, rd);
12039 return;
12040 case 0xa: /* CMLT */
12041 if (u == 1) {
12042 unallocated_encoding(s);
12043 return;
12045 /* fall through */
12046 case 0x8: /* CMGT, CMGE */
12047 case 0x9: /* CMEQ, CMLE */
12048 case 0xb: /* ABS, NEG */
12049 if (size == 3 && !is_q) {
12050 unallocated_encoding(s);
12051 return;
12053 break;
12054 case 0x3: /* SUQADD, USQADD */
12055 if (size == 3 && !is_q) {
12056 unallocated_encoding(s);
12057 return;
12059 if (!fp_access_check(s)) {
12060 return;
12062 handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12063 return;
12064 case 0x7: /* SQABS, SQNEG */
12065 if (size == 3 && !is_q) {
12066 unallocated_encoding(s);
12067 return;
12069 break;
12070 case 0xc ... 0xf:
12071 case 0x16 ... 0x1d:
12072 case 0x1f:
12074 /* Floating point: U, size[1] and opcode indicate operation;
12075 * size[0] indicates single or double precision.
12077 int is_double = extract32(size, 0, 1);
12078 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12079 size = is_double ? 3 : 2;
12080 switch (opcode) {
12081 case 0x2f: /* FABS */
12082 case 0x6f: /* FNEG */
12083 if (size == 3 && !is_q) {
12084 unallocated_encoding(s);
12085 return;
12087 break;
12088 case 0x1d: /* SCVTF */
12089 case 0x5d: /* UCVTF */
12091 bool is_signed = (opcode == 0x1d) ? true : false;
12092 int elements = is_double ? 2 : is_q ? 4 : 2;
12093 if (is_double && !is_q) {
12094 unallocated_encoding(s);
12095 return;
12097 if (!fp_access_check(s)) {
12098 return;
12100 handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12101 return;
12103 case 0x2c: /* FCMGT (zero) */
12104 case 0x2d: /* FCMEQ (zero) */
12105 case 0x2e: /* FCMLT (zero) */
12106 case 0x6c: /* FCMGE (zero) */
12107 case 0x6d: /* FCMLE (zero) */
12108 if (size == 3 && !is_q) {
12109 unallocated_encoding(s);
12110 return;
12112 handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12113 return;
12114 case 0x7f: /* FSQRT */
12115 if (size == 3 && !is_q) {
12116 unallocated_encoding(s);
12117 return;
12119 break;
12120 case 0x1a: /* FCVTNS */
12121 case 0x1b: /* FCVTMS */
12122 case 0x3a: /* FCVTPS */
12123 case 0x3b: /* FCVTZS */
12124 case 0x5a: /* FCVTNU */
12125 case 0x5b: /* FCVTMU */
12126 case 0x7a: /* FCVTPU */
12127 case 0x7b: /* FCVTZU */
12128 need_fpstatus = true;
12129 need_rmode = true;
12130 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12131 if (size == 3 && !is_q) {
12132 unallocated_encoding(s);
12133 return;
12135 break;
12136 case 0x5c: /* FCVTAU */
12137 case 0x1c: /* FCVTAS */
12138 need_fpstatus = true;
12139 need_rmode = true;
12140 rmode = FPROUNDING_TIEAWAY;
12141 if (size == 3 && !is_q) {
12142 unallocated_encoding(s);
12143 return;
12145 break;
12146 case 0x3c: /* URECPE */
12147 if (size == 3) {
12148 unallocated_encoding(s);
12149 return;
12151 /* fall through */
12152 case 0x3d: /* FRECPE */
12153 case 0x7d: /* FRSQRTE */
12154 if (size == 3 && !is_q) {
12155 unallocated_encoding(s);
12156 return;
12158 if (!fp_access_check(s)) {
12159 return;
12161 handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12162 return;
12163 case 0x56: /* FCVTXN, FCVTXN2 */
12164 if (size == 2) {
12165 unallocated_encoding(s);
12166 return;
12168 /* fall through */
12169 case 0x16: /* FCVTN, FCVTN2 */
12170 /* handle_2misc_narrow does a 2*size -> size operation, but these
12171 * instructions encode the source size rather than dest size.
12173 if (!fp_access_check(s)) {
12174 return;
12176 handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12177 return;
12178 case 0x17: /* FCVTL, FCVTL2 */
12179 if (!fp_access_check(s)) {
12180 return;
12182 handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12183 return;
12184 case 0x18: /* FRINTN */
12185 case 0x19: /* FRINTM */
12186 case 0x38: /* FRINTP */
12187 case 0x39: /* FRINTZ */
12188 need_rmode = true;
12189 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12190 /* fall through */
12191 case 0x59: /* FRINTX */
12192 case 0x79: /* FRINTI */
12193 need_fpstatus = true;
12194 if (size == 3 && !is_q) {
12195 unallocated_encoding(s);
12196 return;
12198 break;
12199 case 0x58: /* FRINTA */
12200 need_rmode = true;
12201 rmode = FPROUNDING_TIEAWAY;
12202 need_fpstatus = true;
12203 if (size == 3 && !is_q) {
12204 unallocated_encoding(s);
12205 return;
12207 break;
12208 case 0x7c: /* URSQRTE */
12209 if (size == 3) {
12210 unallocated_encoding(s);
12211 return;
12213 need_fpstatus = true;
12214 break;
12215 default:
12216 unallocated_encoding(s);
12217 return;
12219 break;
12221 default:
12222 unallocated_encoding(s);
12223 return;
12226 if (!fp_access_check(s)) {
12227 return;
12230 if (need_fpstatus || need_rmode) {
12231 tcg_fpstatus = get_fpstatus_ptr(false);
12232 } else {
12233 tcg_fpstatus = NULL;
12235 if (need_rmode) {
12236 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
12237 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12238 } else {
12239 tcg_rmode = NULL;
12242 switch (opcode) {
12243 case 0x5:
12244 if (u && size == 0) { /* NOT */
12245 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12246 return;
12248 break;
12249 case 0xb:
12250 if (u) { /* NEG */
12251 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12252 return;
12254 break;
12257 if (size == 3) {
12258 /* All 64-bit element operations can be shared with scalar 2misc */
12259 int pass;
12261 /* Coverity claims (size == 3 && !is_q) has been eliminated
12262 * from all paths leading to here.
12264 tcg_debug_assert(is_q);
12265 for (pass = 0; pass < 2; pass++) {
12266 TCGv_i64 tcg_op = tcg_temp_new_i64();
12267 TCGv_i64 tcg_res = tcg_temp_new_i64();
12269 read_vec_element(s, tcg_op, rn, pass, MO_64);
12271 handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12272 tcg_rmode, tcg_fpstatus);
12274 write_vec_element(s, tcg_res, rd, pass, MO_64);
12276 tcg_temp_free_i64(tcg_res);
12277 tcg_temp_free_i64(tcg_op);
12279 } else {
12280 int pass;
12282 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12283 TCGv_i32 tcg_op = tcg_temp_new_i32();
12284 TCGv_i32 tcg_res = tcg_temp_new_i32();
12285 TCGCond cond;
12287 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12289 if (size == 2) {
12290 /* Special cases for 32 bit elements */
12291 switch (opcode) {
12292 case 0xa: /* CMLT */
12293 /* 32 bit integer comparison against zero, result is
12294 * test ? (2^32 - 1) : 0. We implement via setcond(test)
12295 * and inverting.
12297 cond = TCG_COND_LT;
12298 do_cmop:
12299 tcg_gen_setcondi_i32(cond, tcg_res, tcg_op, 0);
12300 tcg_gen_neg_i32(tcg_res, tcg_res);
12301 break;
12302 case 0x8: /* CMGT, CMGE */
12303 cond = u ? TCG_COND_GE : TCG_COND_GT;
12304 goto do_cmop;
12305 case 0x9: /* CMEQ, CMLE */
12306 cond = u ? TCG_COND_LE : TCG_COND_EQ;
12307 goto do_cmop;
12308 case 0x4: /* CLS */
12309 if (u) {
12310 tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12311 } else {
12312 tcg_gen_clrsb_i32(tcg_res, tcg_op);
12314 break;
12315 case 0x7: /* SQABS, SQNEG */
12316 if (u) {
12317 gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12318 } else {
12319 gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12321 break;
12322 case 0xb: /* ABS, NEG */
12323 if (u) {
12324 tcg_gen_neg_i32(tcg_res, tcg_op);
12325 } else {
12326 TCGv_i32 tcg_zero = tcg_const_i32(0);
12327 tcg_gen_neg_i32(tcg_res, tcg_op);
12328 tcg_gen_movcond_i32(TCG_COND_GT, tcg_res, tcg_op,
12329 tcg_zero, tcg_op, tcg_res);
12330 tcg_temp_free_i32(tcg_zero);
12332 break;
12333 case 0x2f: /* FABS */
12334 gen_helper_vfp_abss(tcg_res, tcg_op);
12335 break;
12336 case 0x6f: /* FNEG */
12337 gen_helper_vfp_negs(tcg_res, tcg_op);
12338 break;
12339 case 0x7f: /* FSQRT */
12340 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12341 break;
12342 case 0x1a: /* FCVTNS */
12343 case 0x1b: /* FCVTMS */
12344 case 0x1c: /* FCVTAS */
12345 case 0x3a: /* FCVTPS */
12346 case 0x3b: /* FCVTZS */
12348 TCGv_i32 tcg_shift = tcg_const_i32(0);
12349 gen_helper_vfp_tosls(tcg_res, tcg_op,
12350 tcg_shift, tcg_fpstatus);
12351 tcg_temp_free_i32(tcg_shift);
12352 break;
12354 case 0x5a: /* FCVTNU */
12355 case 0x5b: /* FCVTMU */
12356 case 0x5c: /* FCVTAU */
12357 case 0x7a: /* FCVTPU */
12358 case 0x7b: /* FCVTZU */
12360 TCGv_i32 tcg_shift = tcg_const_i32(0);
12361 gen_helper_vfp_touls(tcg_res, tcg_op,
12362 tcg_shift, tcg_fpstatus);
12363 tcg_temp_free_i32(tcg_shift);
12364 break;
12366 case 0x18: /* FRINTN */
12367 case 0x19: /* FRINTM */
12368 case 0x38: /* FRINTP */
12369 case 0x39: /* FRINTZ */
12370 case 0x58: /* FRINTA */
12371 case 0x79: /* FRINTI */
12372 gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12373 break;
12374 case 0x59: /* FRINTX */
12375 gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12376 break;
12377 case 0x7c: /* URSQRTE */
12378 gen_helper_rsqrte_u32(tcg_res, tcg_op, tcg_fpstatus);
12379 break;
12380 default:
12381 g_assert_not_reached();
12383 } else {
12384 /* Use helpers for 8 and 16 bit elements */
12385 switch (opcode) {
12386 case 0x5: /* CNT, RBIT */
12387 /* For these two insns size is part of the opcode specifier
12388 * (handled earlier); they always operate on byte elements.
12390 if (u) {
12391 gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12392 } else {
12393 gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12395 break;
12396 case 0x7: /* SQABS, SQNEG */
12398 NeonGenOneOpEnvFn *genfn;
12399 static NeonGenOneOpEnvFn * const fns[2][2] = {
12400 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12401 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12403 genfn = fns[size][u];
12404 genfn(tcg_res, cpu_env, tcg_op);
12405 break;
12407 case 0x8: /* CMGT, CMGE */
12408 case 0x9: /* CMEQ, CMLE */
12409 case 0xa: /* CMLT */
12411 static NeonGenTwoOpFn * const fns[3][2] = {
12412 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_s16 },
12413 { gen_helper_neon_cge_s8, gen_helper_neon_cge_s16 },
12414 { gen_helper_neon_ceq_u8, gen_helper_neon_ceq_u16 },
12416 NeonGenTwoOpFn *genfn;
12417 int comp;
12418 bool reverse;
12419 TCGv_i32 tcg_zero = tcg_const_i32(0);
12421 /* comp = index into [CMGT, CMGE, CMEQ, CMLE, CMLT] */
12422 comp = (opcode - 0x8) * 2 + u;
12423 /* ...but LE, LT are implemented as reverse GE, GT */
12424 reverse = (comp > 2);
12425 if (reverse) {
12426 comp = 4 - comp;
12428 genfn = fns[comp][size];
12429 if (reverse) {
12430 genfn(tcg_res, tcg_zero, tcg_op);
12431 } else {
12432 genfn(tcg_res, tcg_op, tcg_zero);
12434 tcg_temp_free_i32(tcg_zero);
12435 break;
12437 case 0xb: /* ABS, NEG */
12438 if (u) {
12439 TCGv_i32 tcg_zero = tcg_const_i32(0);
12440 if (size) {
12441 gen_helper_neon_sub_u16(tcg_res, tcg_zero, tcg_op);
12442 } else {
12443 gen_helper_neon_sub_u8(tcg_res, tcg_zero, tcg_op);
12445 tcg_temp_free_i32(tcg_zero);
12446 } else {
12447 if (size) {
12448 gen_helper_neon_abs_s16(tcg_res, tcg_op);
12449 } else {
12450 gen_helper_neon_abs_s8(tcg_res, tcg_op);
12453 break;
12454 case 0x4: /* CLS, CLZ */
12455 if (u) {
12456 if (size == 0) {
12457 gen_helper_neon_clz_u8(tcg_res, tcg_op);
12458 } else {
12459 gen_helper_neon_clz_u16(tcg_res, tcg_op);
12461 } else {
12462 if (size == 0) {
12463 gen_helper_neon_cls_s8(tcg_res, tcg_op);
12464 } else {
12465 gen_helper_neon_cls_s16(tcg_res, tcg_op);
12468 break;
12469 default:
12470 g_assert_not_reached();
12474 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12476 tcg_temp_free_i32(tcg_res);
12477 tcg_temp_free_i32(tcg_op);
12480 clear_vec_high(s, is_q, rd);
12482 if (need_rmode) {
12483 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12484 tcg_temp_free_i32(tcg_rmode);
12486 if (need_fpstatus) {
12487 tcg_temp_free_ptr(tcg_fpstatus);
12491 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12493 * 31 30 29 28 27 24 23 22 21 17 16 12 11 10 9 5 4 0
12494 * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12495 * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 | Rn | Rd |
12496 * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12497 * mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12498 * val: 0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12500 * This actually covers two groups where scalar access is governed by
12501 * bit 28. A bunch of the instructions (float to integral) only exist
12502 * in the vector form and are un-allocated for the scalar decode. Also
12503 * in the scalar decode Q is always 1.
12505 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12507 int fpop, opcode, a, u;
12508 int rn, rd;
12509 bool is_q;
12510 bool is_scalar;
12511 bool only_in_vector = false;
12513 int pass;
12514 TCGv_i32 tcg_rmode = NULL;
12515 TCGv_ptr tcg_fpstatus = NULL;
12516 bool need_rmode = false;
12517 bool need_fpst = true;
12518 int rmode;
12520 if (!dc_isar_feature(aa64_fp16, s)) {
12521 unallocated_encoding(s);
12522 return;
12525 rd = extract32(insn, 0, 5);
12526 rn = extract32(insn, 5, 5);
12528 a = extract32(insn, 23, 1);
12529 u = extract32(insn, 29, 1);
12530 is_scalar = extract32(insn, 28, 1);
12531 is_q = extract32(insn, 30, 1);
12533 opcode = extract32(insn, 12, 5);
12534 fpop = deposit32(opcode, 5, 1, a);
12535 fpop = deposit32(fpop, 6, 1, u);
12537 rd = extract32(insn, 0, 5);
12538 rn = extract32(insn, 5, 5);
12540 switch (fpop) {
12541 case 0x1d: /* SCVTF */
12542 case 0x5d: /* UCVTF */
12544 int elements;
12546 if (is_scalar) {
12547 elements = 1;
12548 } else {
12549 elements = (is_q ? 8 : 4);
12552 if (!fp_access_check(s)) {
12553 return;
12555 handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12556 return;
12558 break;
12559 case 0x2c: /* FCMGT (zero) */
12560 case 0x2d: /* FCMEQ (zero) */
12561 case 0x2e: /* FCMLT (zero) */
12562 case 0x6c: /* FCMGE (zero) */
12563 case 0x6d: /* FCMLE (zero) */
12564 handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12565 return;
12566 case 0x3d: /* FRECPE */
12567 case 0x3f: /* FRECPX */
12568 break;
12569 case 0x18: /* FRINTN */
12570 need_rmode = true;
12571 only_in_vector = true;
12572 rmode = FPROUNDING_TIEEVEN;
12573 break;
12574 case 0x19: /* FRINTM */
12575 need_rmode = true;
12576 only_in_vector = true;
12577 rmode = FPROUNDING_NEGINF;
12578 break;
12579 case 0x38: /* FRINTP */
12580 need_rmode = true;
12581 only_in_vector = true;
12582 rmode = FPROUNDING_POSINF;
12583 break;
12584 case 0x39: /* FRINTZ */
12585 need_rmode = true;
12586 only_in_vector = true;
12587 rmode = FPROUNDING_ZERO;
12588 break;
12589 case 0x58: /* FRINTA */
12590 need_rmode = true;
12591 only_in_vector = true;
12592 rmode = FPROUNDING_TIEAWAY;
12593 break;
12594 case 0x59: /* FRINTX */
12595 case 0x79: /* FRINTI */
12596 only_in_vector = true;
12597 /* current rounding mode */
12598 break;
12599 case 0x1a: /* FCVTNS */
12600 need_rmode = true;
12601 rmode = FPROUNDING_TIEEVEN;
12602 break;
12603 case 0x1b: /* FCVTMS */
12604 need_rmode = true;
12605 rmode = FPROUNDING_NEGINF;
12606 break;
12607 case 0x1c: /* FCVTAS */
12608 need_rmode = true;
12609 rmode = FPROUNDING_TIEAWAY;
12610 break;
12611 case 0x3a: /* FCVTPS */
12612 need_rmode = true;
12613 rmode = FPROUNDING_POSINF;
12614 break;
12615 case 0x3b: /* FCVTZS */
12616 need_rmode = true;
12617 rmode = FPROUNDING_ZERO;
12618 break;
12619 case 0x5a: /* FCVTNU */
12620 need_rmode = true;
12621 rmode = FPROUNDING_TIEEVEN;
12622 break;
12623 case 0x5b: /* FCVTMU */
12624 need_rmode = true;
12625 rmode = FPROUNDING_NEGINF;
12626 break;
12627 case 0x5c: /* FCVTAU */
12628 need_rmode = true;
12629 rmode = FPROUNDING_TIEAWAY;
12630 break;
12631 case 0x7a: /* FCVTPU */
12632 need_rmode = true;
12633 rmode = FPROUNDING_POSINF;
12634 break;
12635 case 0x7b: /* FCVTZU */
12636 need_rmode = true;
12637 rmode = FPROUNDING_ZERO;
12638 break;
12639 case 0x2f: /* FABS */
12640 case 0x6f: /* FNEG */
12641 need_fpst = false;
12642 break;
12643 case 0x7d: /* FRSQRTE */
12644 case 0x7f: /* FSQRT (vector) */
12645 break;
12646 default:
12647 fprintf(stderr, "%s: insn %#04x fpop %#2x\n", __func__, insn, fpop);
12648 g_assert_not_reached();
12652 /* Check additional constraints for the scalar encoding */
12653 if (is_scalar) {
12654 if (!is_q) {
12655 unallocated_encoding(s);
12656 return;
12658 /* FRINTxx is only in the vector form */
12659 if (only_in_vector) {
12660 unallocated_encoding(s);
12661 return;
12665 if (!fp_access_check(s)) {
12666 return;
12669 if (need_rmode || need_fpst) {
12670 tcg_fpstatus = get_fpstatus_ptr(true);
12673 if (need_rmode) {
12674 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
12675 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12678 if (is_scalar) {
12679 TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12680 TCGv_i32 tcg_res = tcg_temp_new_i32();
12682 switch (fpop) {
12683 case 0x1a: /* FCVTNS */
12684 case 0x1b: /* FCVTMS */
12685 case 0x1c: /* FCVTAS */
12686 case 0x3a: /* FCVTPS */
12687 case 0x3b: /* FCVTZS */
12688 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12689 break;
12690 case 0x3d: /* FRECPE */
12691 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12692 break;
12693 case 0x3f: /* FRECPX */
12694 gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12695 break;
12696 case 0x5a: /* FCVTNU */
12697 case 0x5b: /* FCVTMU */
12698 case 0x5c: /* FCVTAU */
12699 case 0x7a: /* FCVTPU */
12700 case 0x7b: /* FCVTZU */
12701 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12702 break;
12703 case 0x6f: /* FNEG */
12704 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12705 break;
12706 case 0x7d: /* FRSQRTE */
12707 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12708 break;
12709 default:
12710 g_assert_not_reached();
12713 /* limit any sign extension going on */
12714 tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12715 write_fp_sreg(s, rd, tcg_res);
12717 tcg_temp_free_i32(tcg_res);
12718 tcg_temp_free_i32(tcg_op);
12719 } else {
12720 for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12721 TCGv_i32 tcg_op = tcg_temp_new_i32();
12722 TCGv_i32 tcg_res = tcg_temp_new_i32();
12724 read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12726 switch (fpop) {
12727 case 0x1a: /* FCVTNS */
12728 case 0x1b: /* FCVTMS */
12729 case 0x1c: /* FCVTAS */
12730 case 0x3a: /* FCVTPS */
12731 case 0x3b: /* FCVTZS */
12732 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12733 break;
12734 case 0x3d: /* FRECPE */
12735 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12736 break;
12737 case 0x5a: /* FCVTNU */
12738 case 0x5b: /* FCVTMU */
12739 case 0x5c: /* FCVTAU */
12740 case 0x7a: /* FCVTPU */
12741 case 0x7b: /* FCVTZU */
12742 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12743 break;
12744 case 0x18: /* FRINTN */
12745 case 0x19: /* FRINTM */
12746 case 0x38: /* FRINTP */
12747 case 0x39: /* FRINTZ */
12748 case 0x58: /* FRINTA */
12749 case 0x79: /* FRINTI */
12750 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12751 break;
12752 case 0x59: /* FRINTX */
12753 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12754 break;
12755 case 0x2f: /* FABS */
12756 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12757 break;
12758 case 0x6f: /* FNEG */
12759 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12760 break;
12761 case 0x7d: /* FRSQRTE */
12762 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12763 break;
12764 case 0x7f: /* FSQRT */
12765 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12766 break;
12767 default:
12768 g_assert_not_reached();
12771 write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12773 tcg_temp_free_i32(tcg_res);
12774 tcg_temp_free_i32(tcg_op);
12777 clear_vec_high(s, is_q, rd);
12780 if (tcg_rmode) {
12781 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12782 tcg_temp_free_i32(tcg_rmode);
12785 if (tcg_fpstatus) {
12786 tcg_temp_free_ptr(tcg_fpstatus);
12790 /* AdvSIMD scalar x indexed element
12791 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
12792 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12793 * | 0 1 | U | 1 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
12794 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12795 * AdvSIMD vector x indexed element
12796 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
12797 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12798 * | 0 | Q | U | 0 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
12799 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12801 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12803 /* This encoding has two kinds of instruction:
12804 * normal, where we perform elt x idxelt => elt for each
12805 * element in the vector
12806 * long, where we perform elt x idxelt and generate a result of
12807 * double the width of the input element
12808 * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12810 bool is_scalar = extract32(insn, 28, 1);
12811 bool is_q = extract32(insn, 30, 1);
12812 bool u = extract32(insn, 29, 1);
12813 int size = extract32(insn, 22, 2);
12814 int l = extract32(insn, 21, 1);
12815 int m = extract32(insn, 20, 1);
12816 /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12817 int rm = extract32(insn, 16, 4);
12818 int opcode = extract32(insn, 12, 4);
12819 int h = extract32(insn, 11, 1);
12820 int rn = extract32(insn, 5, 5);
12821 int rd = extract32(insn, 0, 5);
12822 bool is_long = false;
12823 int is_fp = 0;
12824 bool is_fp16 = false;
12825 int index;
12826 TCGv_ptr fpst;
12828 switch (16 * u + opcode) {
12829 case 0x08: /* MUL */
12830 case 0x10: /* MLA */
12831 case 0x14: /* MLS */
12832 if (is_scalar) {
12833 unallocated_encoding(s);
12834 return;
12836 break;
12837 case 0x02: /* SMLAL, SMLAL2 */
12838 case 0x12: /* UMLAL, UMLAL2 */
12839 case 0x06: /* SMLSL, SMLSL2 */
12840 case 0x16: /* UMLSL, UMLSL2 */
12841 case 0x0a: /* SMULL, SMULL2 */
12842 case 0x1a: /* UMULL, UMULL2 */
12843 if (is_scalar) {
12844 unallocated_encoding(s);
12845 return;
12847 is_long = true;
12848 break;
12849 case 0x03: /* SQDMLAL, SQDMLAL2 */
12850 case 0x07: /* SQDMLSL, SQDMLSL2 */
12851 case 0x0b: /* SQDMULL, SQDMULL2 */
12852 is_long = true;
12853 break;
12854 case 0x0c: /* SQDMULH */
12855 case 0x0d: /* SQRDMULH */
12856 break;
12857 case 0x01: /* FMLA */
12858 case 0x05: /* FMLS */
12859 case 0x09: /* FMUL */
12860 case 0x19: /* FMULX */
12861 is_fp = 1;
12862 break;
12863 case 0x1d: /* SQRDMLAH */
12864 case 0x1f: /* SQRDMLSH */
12865 if (!dc_isar_feature(aa64_rdm, s)) {
12866 unallocated_encoding(s);
12867 return;
12869 break;
12870 case 0x0e: /* SDOT */
12871 case 0x1e: /* UDOT */
12872 if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12873 unallocated_encoding(s);
12874 return;
12876 break;
12877 case 0x11: /* FCMLA #0 */
12878 case 0x13: /* FCMLA #90 */
12879 case 0x15: /* FCMLA #180 */
12880 case 0x17: /* FCMLA #270 */
12881 if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
12882 unallocated_encoding(s);
12883 return;
12885 is_fp = 2;
12886 break;
12887 case 0x00: /* FMLAL */
12888 case 0x04: /* FMLSL */
12889 case 0x18: /* FMLAL2 */
12890 case 0x1c: /* FMLSL2 */
12891 if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
12892 unallocated_encoding(s);
12893 return;
12895 size = MO_16;
12896 /* is_fp, but we pass cpu_env not fp_status. */
12897 break;
12898 default:
12899 unallocated_encoding(s);
12900 return;
12903 switch (is_fp) {
12904 case 1: /* normal fp */
12905 /* convert insn encoded size to TCGMemOp size */
12906 switch (size) {
12907 case 0: /* half-precision */
12908 size = MO_16;
12909 is_fp16 = true;
12910 break;
12911 case MO_32: /* single precision */
12912 case MO_64: /* double precision */
12913 break;
12914 default:
12915 unallocated_encoding(s);
12916 return;
12918 break;
12920 case 2: /* complex fp */
12921 /* Each indexable element is a complex pair. */
12922 size += 1;
12923 switch (size) {
12924 case MO_32:
12925 if (h && !is_q) {
12926 unallocated_encoding(s);
12927 return;
12929 is_fp16 = true;
12930 break;
12931 case MO_64:
12932 break;
12933 default:
12934 unallocated_encoding(s);
12935 return;
12937 break;
12939 default: /* integer */
12940 switch (size) {
12941 case MO_8:
12942 case MO_64:
12943 unallocated_encoding(s);
12944 return;
12946 break;
12948 if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
12949 unallocated_encoding(s);
12950 return;
12953 /* Given TCGMemOp size, adjust register and indexing. */
12954 switch (size) {
12955 case MO_16:
12956 index = h << 2 | l << 1 | m;
12957 break;
12958 case MO_32:
12959 index = h << 1 | l;
12960 rm |= m << 4;
12961 break;
12962 case MO_64:
12963 if (l || !is_q) {
12964 unallocated_encoding(s);
12965 return;
12967 index = h;
12968 rm |= m << 4;
12969 break;
12970 default:
12971 g_assert_not_reached();
12974 if (!fp_access_check(s)) {
12975 return;
12978 if (is_fp) {
12979 fpst = get_fpstatus_ptr(is_fp16);
12980 } else {
12981 fpst = NULL;
12984 switch (16 * u + opcode) {
12985 case 0x0e: /* SDOT */
12986 case 0x1e: /* UDOT */
12987 gen_gvec_op3_ool(s, is_q, rd, rn, rm, index,
12988 u ? gen_helper_gvec_udot_idx_b
12989 : gen_helper_gvec_sdot_idx_b);
12990 return;
12991 case 0x11: /* FCMLA #0 */
12992 case 0x13: /* FCMLA #90 */
12993 case 0x15: /* FCMLA #180 */
12994 case 0x17: /* FCMLA #270 */
12996 int rot = extract32(insn, 13, 2);
12997 int data = (index << 2) | rot;
12998 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
12999 vec_full_reg_offset(s, rn),
13000 vec_full_reg_offset(s, rm), fpst,
13001 is_q ? 16 : 8, vec_full_reg_size(s), data,
13002 size == MO_64
13003 ? gen_helper_gvec_fcmlas_idx
13004 : gen_helper_gvec_fcmlah_idx);
13005 tcg_temp_free_ptr(fpst);
13007 return;
13009 case 0x00: /* FMLAL */
13010 case 0x04: /* FMLSL */
13011 case 0x18: /* FMLAL2 */
13012 case 0x1c: /* FMLSL2 */
13014 int is_s = extract32(opcode, 2, 1);
13015 int is_2 = u;
13016 int data = (index << 2) | (is_2 << 1) | is_s;
13017 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13018 vec_full_reg_offset(s, rn),
13019 vec_full_reg_offset(s, rm), cpu_env,
13020 is_q ? 16 : 8, vec_full_reg_size(s),
13021 data, gen_helper_gvec_fmlal_idx_a64);
13023 return;
13026 if (size == 3) {
13027 TCGv_i64 tcg_idx = tcg_temp_new_i64();
13028 int pass;
13030 assert(is_fp && is_q && !is_long);
13032 read_vec_element(s, tcg_idx, rm, index, MO_64);
13034 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13035 TCGv_i64 tcg_op = tcg_temp_new_i64();
13036 TCGv_i64 tcg_res = tcg_temp_new_i64();
13038 read_vec_element(s, tcg_op, rn, pass, MO_64);
13040 switch (16 * u + opcode) {
13041 case 0x05: /* FMLS */
13042 /* As usual for ARM, separate negation for fused multiply-add */
13043 gen_helper_vfp_negd(tcg_op, tcg_op);
13044 /* fall through */
13045 case 0x01: /* FMLA */
13046 read_vec_element(s, tcg_res, rd, pass, MO_64);
13047 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13048 break;
13049 case 0x09: /* FMUL */
13050 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13051 break;
13052 case 0x19: /* FMULX */
13053 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13054 break;
13055 default:
13056 g_assert_not_reached();
13059 write_vec_element(s, tcg_res, rd, pass, MO_64);
13060 tcg_temp_free_i64(tcg_op);
13061 tcg_temp_free_i64(tcg_res);
13064 tcg_temp_free_i64(tcg_idx);
13065 clear_vec_high(s, !is_scalar, rd);
13066 } else if (!is_long) {
13067 /* 32 bit floating point, or 16 or 32 bit integer.
13068 * For the 16 bit scalar case we use the usual Neon helpers and
13069 * rely on the fact that 0 op 0 == 0 with no side effects.
13071 TCGv_i32 tcg_idx = tcg_temp_new_i32();
13072 int pass, maxpasses;
13074 if (is_scalar) {
13075 maxpasses = 1;
13076 } else {
13077 maxpasses = is_q ? 4 : 2;
13080 read_vec_element_i32(s, tcg_idx, rm, index, size);
13082 if (size == 1 && !is_scalar) {
13083 /* The simplest way to handle the 16x16 indexed ops is to duplicate
13084 * the index into both halves of the 32 bit tcg_idx and then use
13085 * the usual Neon helpers.
13087 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13090 for (pass = 0; pass < maxpasses; pass++) {
13091 TCGv_i32 tcg_op = tcg_temp_new_i32();
13092 TCGv_i32 tcg_res = tcg_temp_new_i32();
13094 read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13096 switch (16 * u + opcode) {
13097 case 0x08: /* MUL */
13098 case 0x10: /* MLA */
13099 case 0x14: /* MLS */
13101 static NeonGenTwoOpFn * const fns[2][2] = {
13102 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13103 { tcg_gen_add_i32, tcg_gen_sub_i32 },
13105 NeonGenTwoOpFn *genfn;
13106 bool is_sub = opcode == 0x4;
13108 if (size == 1) {
13109 gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13110 } else {
13111 tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13113 if (opcode == 0x8) {
13114 break;
13116 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13117 genfn = fns[size - 1][is_sub];
13118 genfn(tcg_res, tcg_op, tcg_res);
13119 break;
13121 case 0x05: /* FMLS */
13122 case 0x01: /* FMLA */
13123 read_vec_element_i32(s, tcg_res, rd, pass,
13124 is_scalar ? size : MO_32);
13125 switch (size) {
13126 case 1:
13127 if (opcode == 0x5) {
13128 /* As usual for ARM, separate negation for fused
13129 * multiply-add */
13130 tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13132 if (is_scalar) {
13133 gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13134 tcg_res, fpst);
13135 } else {
13136 gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13137 tcg_res, fpst);
13139 break;
13140 case 2:
13141 if (opcode == 0x5) {
13142 /* As usual for ARM, separate negation for
13143 * fused multiply-add */
13144 tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13146 gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13147 tcg_res, fpst);
13148 break;
13149 default:
13150 g_assert_not_reached();
13152 break;
13153 case 0x09: /* FMUL */
13154 switch (size) {
13155 case 1:
13156 if (is_scalar) {
13157 gen_helper_advsimd_mulh(tcg_res, tcg_op,
13158 tcg_idx, fpst);
13159 } else {
13160 gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13161 tcg_idx, fpst);
13163 break;
13164 case 2:
13165 gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13166 break;
13167 default:
13168 g_assert_not_reached();
13170 break;
13171 case 0x19: /* FMULX */
13172 switch (size) {
13173 case 1:
13174 if (is_scalar) {
13175 gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13176 tcg_idx, fpst);
13177 } else {
13178 gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13179 tcg_idx, fpst);
13181 break;
13182 case 2:
13183 gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13184 break;
13185 default:
13186 g_assert_not_reached();
13188 break;
13189 case 0x0c: /* SQDMULH */
13190 if (size == 1) {
13191 gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13192 tcg_op, tcg_idx);
13193 } else {
13194 gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13195 tcg_op, tcg_idx);
13197 break;
13198 case 0x0d: /* SQRDMULH */
13199 if (size == 1) {
13200 gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13201 tcg_op, tcg_idx);
13202 } else {
13203 gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13204 tcg_op, tcg_idx);
13206 break;
13207 case 0x1d: /* SQRDMLAH */
13208 read_vec_element_i32(s, tcg_res, rd, pass,
13209 is_scalar ? size : MO_32);
13210 if (size == 1) {
13211 gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13212 tcg_op, tcg_idx, tcg_res);
13213 } else {
13214 gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13215 tcg_op, tcg_idx, tcg_res);
13217 break;
13218 case 0x1f: /* SQRDMLSH */
13219 read_vec_element_i32(s, tcg_res, rd, pass,
13220 is_scalar ? size : MO_32);
13221 if (size == 1) {
13222 gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13223 tcg_op, tcg_idx, tcg_res);
13224 } else {
13225 gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13226 tcg_op, tcg_idx, tcg_res);
13228 break;
13229 default:
13230 g_assert_not_reached();
13233 if (is_scalar) {
13234 write_fp_sreg(s, rd, tcg_res);
13235 } else {
13236 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13239 tcg_temp_free_i32(tcg_op);
13240 tcg_temp_free_i32(tcg_res);
13243 tcg_temp_free_i32(tcg_idx);
13244 clear_vec_high(s, is_q, rd);
13245 } else {
13246 /* long ops: 16x16->32 or 32x32->64 */
13247 TCGv_i64 tcg_res[2];
13248 int pass;
13249 bool satop = extract32(opcode, 0, 1);
13250 TCGMemOp memop = MO_32;
13252 if (satop || !u) {
13253 memop |= MO_SIGN;
13256 if (size == 2) {
13257 TCGv_i64 tcg_idx = tcg_temp_new_i64();
13259 read_vec_element(s, tcg_idx, rm, index, memop);
13261 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13262 TCGv_i64 tcg_op = tcg_temp_new_i64();
13263 TCGv_i64 tcg_passres;
13264 int passelt;
13266 if (is_scalar) {
13267 passelt = 0;
13268 } else {
13269 passelt = pass + (is_q * 2);
13272 read_vec_element(s, tcg_op, rn, passelt, memop);
13274 tcg_res[pass] = tcg_temp_new_i64();
13276 if (opcode == 0xa || opcode == 0xb) {
13277 /* Non-accumulating ops */
13278 tcg_passres = tcg_res[pass];
13279 } else {
13280 tcg_passres = tcg_temp_new_i64();
13283 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13284 tcg_temp_free_i64(tcg_op);
13286 if (satop) {
13287 /* saturating, doubling */
13288 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13289 tcg_passres, tcg_passres);
13292 if (opcode == 0xa || opcode == 0xb) {
13293 continue;
13296 /* Accumulating op: handle accumulate step */
13297 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13299 switch (opcode) {
13300 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13301 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13302 break;
13303 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13304 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13305 break;
13306 case 0x7: /* SQDMLSL, SQDMLSL2 */
13307 tcg_gen_neg_i64(tcg_passres, tcg_passres);
13308 /* fall through */
13309 case 0x3: /* SQDMLAL, SQDMLAL2 */
13310 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13311 tcg_res[pass],
13312 tcg_passres);
13313 break;
13314 default:
13315 g_assert_not_reached();
13317 tcg_temp_free_i64(tcg_passres);
13319 tcg_temp_free_i64(tcg_idx);
13321 clear_vec_high(s, !is_scalar, rd);
13322 } else {
13323 TCGv_i32 tcg_idx = tcg_temp_new_i32();
13325 assert(size == 1);
13326 read_vec_element_i32(s, tcg_idx, rm, index, size);
13328 if (!is_scalar) {
13329 /* The simplest way to handle the 16x16 indexed ops is to
13330 * duplicate the index into both halves of the 32 bit tcg_idx
13331 * and then use the usual Neon helpers.
13333 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13336 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13337 TCGv_i32 tcg_op = tcg_temp_new_i32();
13338 TCGv_i64 tcg_passres;
13340 if (is_scalar) {
13341 read_vec_element_i32(s, tcg_op, rn, pass, size);
13342 } else {
13343 read_vec_element_i32(s, tcg_op, rn,
13344 pass + (is_q * 2), MO_32);
13347 tcg_res[pass] = tcg_temp_new_i64();
13349 if (opcode == 0xa || opcode == 0xb) {
13350 /* Non-accumulating ops */
13351 tcg_passres = tcg_res[pass];
13352 } else {
13353 tcg_passres = tcg_temp_new_i64();
13356 if (memop & MO_SIGN) {
13357 gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13358 } else {
13359 gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13361 if (satop) {
13362 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13363 tcg_passres, tcg_passres);
13365 tcg_temp_free_i32(tcg_op);
13367 if (opcode == 0xa || opcode == 0xb) {
13368 continue;
13371 /* Accumulating op: handle accumulate step */
13372 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13374 switch (opcode) {
13375 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13376 gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13377 tcg_passres);
13378 break;
13379 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13380 gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13381 tcg_passres);
13382 break;
13383 case 0x7: /* SQDMLSL, SQDMLSL2 */
13384 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13385 /* fall through */
13386 case 0x3: /* SQDMLAL, SQDMLAL2 */
13387 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13388 tcg_res[pass],
13389 tcg_passres);
13390 break;
13391 default:
13392 g_assert_not_reached();
13394 tcg_temp_free_i64(tcg_passres);
13396 tcg_temp_free_i32(tcg_idx);
13398 if (is_scalar) {
13399 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13403 if (is_scalar) {
13404 tcg_res[1] = tcg_const_i64(0);
13407 for (pass = 0; pass < 2; pass++) {
13408 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13409 tcg_temp_free_i64(tcg_res[pass]);
13413 if (fpst) {
13414 tcg_temp_free_ptr(fpst);
13418 /* Crypto AES
13419 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
13420 * +-----------------+------+-----------+--------+-----+------+------+
13421 * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
13422 * +-----------------+------+-----------+--------+-----+------+------+
13424 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13426 int size = extract32(insn, 22, 2);
13427 int opcode = extract32(insn, 12, 5);
13428 int rn = extract32(insn, 5, 5);
13429 int rd = extract32(insn, 0, 5);
13430 int decrypt;
13431 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr;
13432 TCGv_i32 tcg_decrypt;
13433 CryptoThreeOpIntFn *genfn;
13435 if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13436 unallocated_encoding(s);
13437 return;
13440 switch (opcode) {
13441 case 0x4: /* AESE */
13442 decrypt = 0;
13443 genfn = gen_helper_crypto_aese;
13444 break;
13445 case 0x6: /* AESMC */
13446 decrypt = 0;
13447 genfn = gen_helper_crypto_aesmc;
13448 break;
13449 case 0x5: /* AESD */
13450 decrypt = 1;
13451 genfn = gen_helper_crypto_aese;
13452 break;
13453 case 0x7: /* AESIMC */
13454 decrypt = 1;
13455 genfn = gen_helper_crypto_aesmc;
13456 break;
13457 default:
13458 unallocated_encoding(s);
13459 return;
13462 if (!fp_access_check(s)) {
13463 return;
13466 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13467 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
13468 tcg_decrypt = tcg_const_i32(decrypt);
13470 genfn(tcg_rd_ptr, tcg_rn_ptr, tcg_decrypt);
13472 tcg_temp_free_ptr(tcg_rd_ptr);
13473 tcg_temp_free_ptr(tcg_rn_ptr);
13474 tcg_temp_free_i32(tcg_decrypt);
13477 /* Crypto three-reg SHA
13478 * 31 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
13479 * +-----------------+------+---+------+---+--------+-----+------+------+
13480 * | 0 1 0 1 1 1 1 0 | size | 0 | Rm | 0 | opcode | 0 0 | Rn | Rd |
13481 * +-----------------+------+---+------+---+--------+-----+------+------+
13483 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13485 int size = extract32(insn, 22, 2);
13486 int opcode = extract32(insn, 12, 3);
13487 int rm = extract32(insn, 16, 5);
13488 int rn = extract32(insn, 5, 5);
13489 int rd = extract32(insn, 0, 5);
13490 CryptoThreeOpFn *genfn;
13491 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr;
13492 bool feature;
13494 if (size != 0) {
13495 unallocated_encoding(s);
13496 return;
13499 switch (opcode) {
13500 case 0: /* SHA1C */
13501 case 1: /* SHA1P */
13502 case 2: /* SHA1M */
13503 case 3: /* SHA1SU0 */
13504 genfn = NULL;
13505 feature = dc_isar_feature(aa64_sha1, s);
13506 break;
13507 case 4: /* SHA256H */
13508 genfn = gen_helper_crypto_sha256h;
13509 feature = dc_isar_feature(aa64_sha256, s);
13510 break;
13511 case 5: /* SHA256H2 */
13512 genfn = gen_helper_crypto_sha256h2;
13513 feature = dc_isar_feature(aa64_sha256, s);
13514 break;
13515 case 6: /* SHA256SU1 */
13516 genfn = gen_helper_crypto_sha256su1;
13517 feature = dc_isar_feature(aa64_sha256, s);
13518 break;
13519 default:
13520 unallocated_encoding(s);
13521 return;
13524 if (!feature) {
13525 unallocated_encoding(s);
13526 return;
13529 if (!fp_access_check(s)) {
13530 return;
13533 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13534 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
13535 tcg_rm_ptr = vec_full_reg_ptr(s, rm);
13537 if (genfn) {
13538 genfn(tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr);
13539 } else {
13540 TCGv_i32 tcg_opcode = tcg_const_i32(opcode);
13542 gen_helper_crypto_sha1_3reg(tcg_rd_ptr, tcg_rn_ptr,
13543 tcg_rm_ptr, tcg_opcode);
13544 tcg_temp_free_i32(tcg_opcode);
13547 tcg_temp_free_ptr(tcg_rd_ptr);
13548 tcg_temp_free_ptr(tcg_rn_ptr);
13549 tcg_temp_free_ptr(tcg_rm_ptr);
13552 /* Crypto two-reg SHA
13553 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
13554 * +-----------------+------+-----------+--------+-----+------+------+
13555 * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
13556 * +-----------------+------+-----------+--------+-----+------+------+
13558 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13560 int size = extract32(insn, 22, 2);
13561 int opcode = extract32(insn, 12, 5);
13562 int rn = extract32(insn, 5, 5);
13563 int rd = extract32(insn, 0, 5);
13564 CryptoTwoOpFn *genfn;
13565 bool feature;
13566 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr;
13568 if (size != 0) {
13569 unallocated_encoding(s);
13570 return;
13573 switch (opcode) {
13574 case 0: /* SHA1H */
13575 feature = dc_isar_feature(aa64_sha1, s);
13576 genfn = gen_helper_crypto_sha1h;
13577 break;
13578 case 1: /* SHA1SU1 */
13579 feature = dc_isar_feature(aa64_sha1, s);
13580 genfn = gen_helper_crypto_sha1su1;
13581 break;
13582 case 2: /* SHA256SU0 */
13583 feature = dc_isar_feature(aa64_sha256, s);
13584 genfn = gen_helper_crypto_sha256su0;
13585 break;
13586 default:
13587 unallocated_encoding(s);
13588 return;
13591 if (!feature) {
13592 unallocated_encoding(s);
13593 return;
13596 if (!fp_access_check(s)) {
13597 return;
13600 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13601 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
13603 genfn(tcg_rd_ptr, tcg_rn_ptr);
13605 tcg_temp_free_ptr(tcg_rd_ptr);
13606 tcg_temp_free_ptr(tcg_rn_ptr);
13609 /* Crypto three-reg SHA512
13610 * 31 21 20 16 15 14 13 12 11 10 9 5 4 0
13611 * +-----------------------+------+---+---+-----+--------+------+------+
13612 * | 1 1 0 0 1 1 1 0 0 1 1 | Rm | 1 | O | 0 0 | opcode | Rn | Rd |
13613 * +-----------------------+------+---+---+-----+--------+------+------+
13615 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13617 int opcode = extract32(insn, 10, 2);
13618 int o = extract32(insn, 14, 1);
13619 int rm = extract32(insn, 16, 5);
13620 int rn = extract32(insn, 5, 5);
13621 int rd = extract32(insn, 0, 5);
13622 bool feature;
13623 CryptoThreeOpFn *genfn;
13625 if (o == 0) {
13626 switch (opcode) {
13627 case 0: /* SHA512H */
13628 feature = dc_isar_feature(aa64_sha512, s);
13629 genfn = gen_helper_crypto_sha512h;
13630 break;
13631 case 1: /* SHA512H2 */
13632 feature = dc_isar_feature(aa64_sha512, s);
13633 genfn = gen_helper_crypto_sha512h2;
13634 break;
13635 case 2: /* SHA512SU1 */
13636 feature = dc_isar_feature(aa64_sha512, s);
13637 genfn = gen_helper_crypto_sha512su1;
13638 break;
13639 case 3: /* RAX1 */
13640 feature = dc_isar_feature(aa64_sha3, s);
13641 genfn = NULL;
13642 break;
13644 } else {
13645 switch (opcode) {
13646 case 0: /* SM3PARTW1 */
13647 feature = dc_isar_feature(aa64_sm3, s);
13648 genfn = gen_helper_crypto_sm3partw1;
13649 break;
13650 case 1: /* SM3PARTW2 */
13651 feature = dc_isar_feature(aa64_sm3, s);
13652 genfn = gen_helper_crypto_sm3partw2;
13653 break;
13654 case 2: /* SM4EKEY */
13655 feature = dc_isar_feature(aa64_sm4, s);
13656 genfn = gen_helper_crypto_sm4ekey;
13657 break;
13658 default:
13659 unallocated_encoding(s);
13660 return;
13664 if (!feature) {
13665 unallocated_encoding(s);
13666 return;
13669 if (!fp_access_check(s)) {
13670 return;
13673 if (genfn) {
13674 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr;
13676 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13677 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
13678 tcg_rm_ptr = vec_full_reg_ptr(s, rm);
13680 genfn(tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr);
13682 tcg_temp_free_ptr(tcg_rd_ptr);
13683 tcg_temp_free_ptr(tcg_rn_ptr);
13684 tcg_temp_free_ptr(tcg_rm_ptr);
13685 } else {
13686 TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
13687 int pass;
13689 tcg_op1 = tcg_temp_new_i64();
13690 tcg_op2 = tcg_temp_new_i64();
13691 tcg_res[0] = tcg_temp_new_i64();
13692 tcg_res[1] = tcg_temp_new_i64();
13694 for (pass = 0; pass < 2; pass++) {
13695 read_vec_element(s, tcg_op1, rn, pass, MO_64);
13696 read_vec_element(s, tcg_op2, rm, pass, MO_64);
13698 tcg_gen_rotli_i64(tcg_res[pass], tcg_op2, 1);
13699 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13701 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13702 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13704 tcg_temp_free_i64(tcg_op1);
13705 tcg_temp_free_i64(tcg_op2);
13706 tcg_temp_free_i64(tcg_res[0]);
13707 tcg_temp_free_i64(tcg_res[1]);
13711 /* Crypto two-reg SHA512
13712 * 31 12 11 10 9 5 4 0
13713 * +-----------------------------------------+--------+------+------+
13714 * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode | Rn | Rd |
13715 * +-----------------------------------------+--------+------+------+
13717 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13719 int opcode = extract32(insn, 10, 2);
13720 int rn = extract32(insn, 5, 5);
13721 int rd = extract32(insn, 0, 5);
13722 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr;
13723 bool feature;
13724 CryptoTwoOpFn *genfn;
13726 switch (opcode) {
13727 case 0: /* SHA512SU0 */
13728 feature = dc_isar_feature(aa64_sha512, s);
13729 genfn = gen_helper_crypto_sha512su0;
13730 break;
13731 case 1: /* SM4E */
13732 feature = dc_isar_feature(aa64_sm4, s);
13733 genfn = gen_helper_crypto_sm4e;
13734 break;
13735 default:
13736 unallocated_encoding(s);
13737 return;
13740 if (!feature) {
13741 unallocated_encoding(s);
13742 return;
13745 if (!fp_access_check(s)) {
13746 return;
13749 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13750 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
13752 genfn(tcg_rd_ptr, tcg_rn_ptr);
13754 tcg_temp_free_ptr(tcg_rd_ptr);
13755 tcg_temp_free_ptr(tcg_rn_ptr);
13758 /* Crypto four-register
13759 * 31 23 22 21 20 16 15 14 10 9 5 4 0
13760 * +-------------------+-----+------+---+------+------+------+
13761 * | 1 1 0 0 1 1 1 0 0 | Op0 | Rm | 0 | Ra | Rn | Rd |
13762 * +-------------------+-----+------+---+------+------+------+
13764 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13766 int op0 = extract32(insn, 21, 2);
13767 int rm = extract32(insn, 16, 5);
13768 int ra = extract32(insn, 10, 5);
13769 int rn = extract32(insn, 5, 5);
13770 int rd = extract32(insn, 0, 5);
13771 bool feature;
13773 switch (op0) {
13774 case 0: /* EOR3 */
13775 case 1: /* BCAX */
13776 feature = dc_isar_feature(aa64_sha3, s);
13777 break;
13778 case 2: /* SM3SS1 */
13779 feature = dc_isar_feature(aa64_sm3, s);
13780 break;
13781 default:
13782 unallocated_encoding(s);
13783 return;
13786 if (!feature) {
13787 unallocated_encoding(s);
13788 return;
13791 if (!fp_access_check(s)) {
13792 return;
13795 if (op0 < 2) {
13796 TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13797 int pass;
13799 tcg_op1 = tcg_temp_new_i64();
13800 tcg_op2 = tcg_temp_new_i64();
13801 tcg_op3 = tcg_temp_new_i64();
13802 tcg_res[0] = tcg_temp_new_i64();
13803 tcg_res[1] = tcg_temp_new_i64();
13805 for (pass = 0; pass < 2; pass++) {
13806 read_vec_element(s, tcg_op1, rn, pass, MO_64);
13807 read_vec_element(s, tcg_op2, rm, pass, MO_64);
13808 read_vec_element(s, tcg_op3, ra, pass, MO_64);
13810 if (op0 == 0) {
13811 /* EOR3 */
13812 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13813 } else {
13814 /* BCAX */
13815 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13817 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13819 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13820 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13822 tcg_temp_free_i64(tcg_op1);
13823 tcg_temp_free_i64(tcg_op2);
13824 tcg_temp_free_i64(tcg_op3);
13825 tcg_temp_free_i64(tcg_res[0]);
13826 tcg_temp_free_i64(tcg_res[1]);
13827 } else {
13828 TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13830 tcg_op1 = tcg_temp_new_i32();
13831 tcg_op2 = tcg_temp_new_i32();
13832 tcg_op3 = tcg_temp_new_i32();
13833 tcg_res = tcg_temp_new_i32();
13834 tcg_zero = tcg_const_i32(0);
13836 read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13837 read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13838 read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13840 tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13841 tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13842 tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13843 tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13845 write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13846 write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13847 write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13848 write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13850 tcg_temp_free_i32(tcg_op1);
13851 tcg_temp_free_i32(tcg_op2);
13852 tcg_temp_free_i32(tcg_op3);
13853 tcg_temp_free_i32(tcg_res);
13854 tcg_temp_free_i32(tcg_zero);
13858 /* Crypto XAR
13859 * 31 21 20 16 15 10 9 5 4 0
13860 * +-----------------------+------+--------+------+------+
13861 * | 1 1 0 0 1 1 1 0 1 0 0 | Rm | imm6 | Rn | Rd |
13862 * +-----------------------+------+--------+------+------+
13864 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
13866 int rm = extract32(insn, 16, 5);
13867 int imm6 = extract32(insn, 10, 6);
13868 int rn = extract32(insn, 5, 5);
13869 int rd = extract32(insn, 0, 5);
13870 TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
13871 int pass;
13873 if (!dc_isar_feature(aa64_sha3, s)) {
13874 unallocated_encoding(s);
13875 return;
13878 if (!fp_access_check(s)) {
13879 return;
13882 tcg_op1 = tcg_temp_new_i64();
13883 tcg_op2 = tcg_temp_new_i64();
13884 tcg_res[0] = tcg_temp_new_i64();
13885 tcg_res[1] = tcg_temp_new_i64();
13887 for (pass = 0; pass < 2; pass++) {
13888 read_vec_element(s, tcg_op1, rn, pass, MO_64);
13889 read_vec_element(s, tcg_op2, rm, pass, MO_64);
13891 tcg_gen_xor_i64(tcg_res[pass], tcg_op1, tcg_op2);
13892 tcg_gen_rotri_i64(tcg_res[pass], tcg_res[pass], imm6);
13894 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13895 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13897 tcg_temp_free_i64(tcg_op1);
13898 tcg_temp_free_i64(tcg_op2);
13899 tcg_temp_free_i64(tcg_res[0]);
13900 tcg_temp_free_i64(tcg_res[1]);
13903 /* Crypto three-reg imm2
13904 * 31 21 20 16 15 14 13 12 11 10 9 5 4 0
13905 * +-----------------------+------+-----+------+--------+------+------+
13906 * | 1 1 0 0 1 1 1 0 0 1 0 | Rm | 1 0 | imm2 | opcode | Rn | Rd |
13907 * +-----------------------+------+-----+------+--------+------+------+
13909 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
13911 int opcode = extract32(insn, 10, 2);
13912 int imm2 = extract32(insn, 12, 2);
13913 int rm = extract32(insn, 16, 5);
13914 int rn = extract32(insn, 5, 5);
13915 int rd = extract32(insn, 0, 5);
13916 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr;
13917 TCGv_i32 tcg_imm2, tcg_opcode;
13919 if (!dc_isar_feature(aa64_sm3, s)) {
13920 unallocated_encoding(s);
13921 return;
13924 if (!fp_access_check(s)) {
13925 return;
13928 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13929 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
13930 tcg_rm_ptr = vec_full_reg_ptr(s, rm);
13931 tcg_imm2 = tcg_const_i32(imm2);
13932 tcg_opcode = tcg_const_i32(opcode);
13934 gen_helper_crypto_sm3tt(tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr, tcg_imm2,
13935 tcg_opcode);
13937 tcg_temp_free_ptr(tcg_rd_ptr);
13938 tcg_temp_free_ptr(tcg_rn_ptr);
13939 tcg_temp_free_ptr(tcg_rm_ptr);
13940 tcg_temp_free_i32(tcg_imm2);
13941 tcg_temp_free_i32(tcg_opcode);
13944 /* C3.6 Data processing - SIMD, inc Crypto
13946 * As the decode gets a little complex we are using a table based
13947 * approach for this part of the decode.
13949 static const AArch64DecodeTable data_proc_simd[] = {
13950 /* pattern , mask , fn */
13951 { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
13952 { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
13953 { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
13954 { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
13955 { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
13956 { 0x0e000400, 0x9fe08400, disas_simd_copy },
13957 { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
13958 /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
13959 { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
13960 { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
13961 { 0x0e000000, 0xbf208c00, disas_simd_tb },
13962 { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
13963 { 0x2e000000, 0xbf208400, disas_simd_ext },
13964 { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
13965 { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
13966 { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
13967 { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
13968 { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
13969 { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
13970 { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
13971 { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
13972 { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
13973 { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
13974 { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
13975 { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
13976 { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
13977 { 0xce000000, 0xff808000, disas_crypto_four_reg },
13978 { 0xce800000, 0xffe00000, disas_crypto_xar },
13979 { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
13980 { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
13981 { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
13982 { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
13983 { 0x00000000, 0x00000000, NULL }
13986 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
13988 /* Note that this is called with all non-FP cases from
13989 * table C3-6 so it must UNDEF for entries not specifically
13990 * allocated to instructions in that table.
13992 AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
13993 if (fn) {
13994 fn(s, insn);
13995 } else {
13996 unallocated_encoding(s);
14000 /* C3.6 Data processing - SIMD and floating point */
14001 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
14003 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
14004 disas_data_proc_fp(s, insn);
14005 } else {
14006 /* SIMD, including crypto */
14007 disas_data_proc_simd(s, insn);
14012 * is_guarded_page:
14013 * @env: The cpu environment
14014 * @s: The DisasContext
14016 * Return true if the page is guarded.
14018 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
14020 #ifdef CONFIG_USER_ONLY
14021 return false; /* FIXME */
14022 #else
14023 uint64_t addr = s->base.pc_first;
14024 int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
14025 unsigned int index = tlb_index(env, mmu_idx, addr);
14026 CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr);
14029 * We test this immediately after reading an insn, which means
14030 * that any normal page must be in the TLB. The only exception
14031 * would be for executing from flash or device memory, which
14032 * does not retain the TLB entry.
14034 * FIXME: Assume false for those, for now. We could use
14035 * arm_cpu_get_phys_page_attrs_debug to re-read the page
14036 * table entry even for that case.
14038 return (tlb_hit(entry->addr_code, addr) &&
14039 env->iotlb[mmu_idx][index].attrs.target_tlb_bit0);
14040 #endif
14044 * btype_destination_ok:
14045 * @insn: The instruction at the branch destination
14046 * @bt: SCTLR_ELx.BT
14047 * @btype: PSTATE.BTYPE, and is non-zero
14049 * On a guarded page, there are a limited number of insns
14050 * that may be present at the branch target:
14051 * - branch target identifiers,
14052 * - paciasp, pacibsp,
14053 * - BRK insn
14054 * - HLT insn
14055 * Anything else causes a Branch Target Exception.
14057 * Return true if the branch is compatible, false to raise BTITRAP.
14059 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
14061 if ((insn & 0xfffff01fu) == 0xd503201fu) {
14062 /* HINT space */
14063 switch (extract32(insn, 5, 7)) {
14064 case 0b011001: /* PACIASP */
14065 case 0b011011: /* PACIBSP */
14067 * If SCTLR_ELx.BT, then PACI*SP are not compatible
14068 * with btype == 3. Otherwise all btype are ok.
14070 return !bt || btype != 3;
14071 case 0b100000: /* BTI */
14072 /* Not compatible with any btype. */
14073 return false;
14074 case 0b100010: /* BTI c */
14075 /* Not compatible with btype == 3 */
14076 return btype != 3;
14077 case 0b100100: /* BTI j */
14078 /* Not compatible with btype == 2 */
14079 return btype != 2;
14080 case 0b100110: /* BTI jc */
14081 /* Compatible with any btype. */
14082 return true;
14084 } else {
14085 switch (insn & 0xffe0001fu) {
14086 case 0xd4200000u: /* BRK */
14087 case 0xd4400000u: /* HLT */
14088 /* Give priority to the breakpoint exception. */
14089 return true;
14092 return false;
14095 /* C3.1 A64 instruction index by encoding */
14096 static void disas_a64_insn(CPUARMState *env, DisasContext *s)
14098 uint32_t insn;
14100 insn = arm_ldl_code(env, s->pc, s->sctlr_b);
14101 s->insn = insn;
14102 s->pc += 4;
14104 s->fp_access_checked = false;
14106 if (dc_isar_feature(aa64_bti, s)) {
14107 if (s->base.num_insns == 1) {
14109 * At the first insn of the TB, compute s->guarded_page.
14110 * We delayed computing this until successfully reading
14111 * the first insn of the TB, above. This (mostly) ensures
14112 * that the softmmu tlb entry has been populated, and the
14113 * page table GP bit is available.
14115 * Note that we need to compute this even if btype == 0,
14116 * because this value is used for BR instructions later
14117 * where ENV is not available.
14119 s->guarded_page = is_guarded_page(env, s);
14121 /* First insn can have btype set to non-zero. */
14122 tcg_debug_assert(s->btype >= 0);
14125 * Note that the Branch Target Exception has fairly high
14126 * priority -- below debugging exceptions but above most
14127 * everything else. This allows us to handle this now
14128 * instead of waiting until the insn is otherwise decoded.
14130 if (s->btype != 0
14131 && s->guarded_page
14132 && !btype_destination_ok(insn, s->bt, s->btype)) {
14133 gen_exception_insn(s, 4, EXCP_UDEF, syn_btitrap(s->btype),
14134 default_exception_el(s));
14135 return;
14137 } else {
14138 /* Not the first insn: btype must be 0. */
14139 tcg_debug_assert(s->btype == 0);
14143 switch (extract32(insn, 25, 4)) {
14144 case 0x0: case 0x1: case 0x3: /* UNALLOCATED */
14145 unallocated_encoding(s);
14146 break;
14147 case 0x2:
14148 if (!dc_isar_feature(aa64_sve, s) || !disas_sve(s, insn)) {
14149 unallocated_encoding(s);
14151 break;
14152 case 0x8: case 0x9: /* Data processing - immediate */
14153 disas_data_proc_imm(s, insn);
14154 break;
14155 case 0xa: case 0xb: /* Branch, exception generation and system insns */
14156 disas_b_exc_sys(s, insn);
14157 break;
14158 case 0x4:
14159 case 0x6:
14160 case 0xc:
14161 case 0xe: /* Loads and stores */
14162 disas_ldst(s, insn);
14163 break;
14164 case 0x5:
14165 case 0xd: /* Data processing - register */
14166 disas_data_proc_reg(s, insn);
14167 break;
14168 case 0x7:
14169 case 0xf: /* Data processing - SIMD and floating point */
14170 disas_data_proc_simd_fp(s, insn);
14171 break;
14172 default:
14173 assert(FALSE); /* all 15 cases should be handled above */
14174 break;
14177 /* if we allocated any temporaries, free them here */
14178 free_tmp_a64(s);
14181 * After execution of most insns, btype is reset to 0.
14182 * Note that we set btype == -1 when the insn sets btype.
14184 if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14185 reset_btype(s);
14189 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14190 CPUState *cpu)
14192 DisasContext *dc = container_of(dcbase, DisasContext, base);
14193 CPUARMState *env = cpu->env_ptr;
14194 ARMCPU *arm_cpu = arm_env_get_cpu(env);
14195 uint32_t tb_flags = dc->base.tb->flags;
14196 int bound, core_mmu_idx;
14198 dc->isar = &arm_cpu->isar;
14199 dc->pc = dc->base.pc_first;
14200 dc->condjmp = 0;
14202 dc->aarch64 = 1;
14203 /* If we are coming from secure EL0 in a system with a 32-bit EL3, then
14204 * there is no secure EL1, so we route exceptions to EL3.
14206 dc->secure_routed_to_el3 = arm_feature(env, ARM_FEATURE_EL3) &&
14207 !arm_el_is_aa64(env, 3);
14208 dc->thumb = 0;
14209 dc->sctlr_b = 0;
14210 dc->be_data = FIELD_EX32(tb_flags, TBFLAG_ANY, BE_DATA) ? MO_BE : MO_LE;
14211 dc->condexec_mask = 0;
14212 dc->condexec_cond = 0;
14213 core_mmu_idx = FIELD_EX32(tb_flags, TBFLAG_ANY, MMUIDX);
14214 dc->mmu_idx = core_to_arm_mmu_idx(env, core_mmu_idx);
14215 dc->tbii = FIELD_EX32(tb_flags, TBFLAG_A64, TBII);
14216 dc->tbid = FIELD_EX32(tb_flags, TBFLAG_A64, TBID);
14217 dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14218 #if !defined(CONFIG_USER_ONLY)
14219 dc->user = (dc->current_el == 0);
14220 #endif
14221 dc->fp_excp_el = FIELD_EX32(tb_flags, TBFLAG_ANY, FPEXC_EL);
14222 dc->sve_excp_el = FIELD_EX32(tb_flags, TBFLAG_A64, SVEEXC_EL);
14223 dc->sve_len = (FIELD_EX32(tb_flags, TBFLAG_A64, ZCR_LEN) + 1) * 16;
14224 dc->pauth_active = FIELD_EX32(tb_flags, TBFLAG_A64, PAUTH_ACTIVE);
14225 dc->bt = FIELD_EX32(tb_flags, TBFLAG_A64, BT);
14226 dc->btype = FIELD_EX32(tb_flags, TBFLAG_A64, BTYPE);
14227 dc->vec_len = 0;
14228 dc->vec_stride = 0;
14229 dc->cp_regs = arm_cpu->cp_regs;
14230 dc->features = env->features;
14232 /* Single step state. The code-generation logic here is:
14233 * SS_ACTIVE == 0:
14234 * generate code with no special handling for single-stepping (except
14235 * that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14236 * this happens anyway because those changes are all system register or
14237 * PSTATE writes).
14238 * SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14239 * emit code for one insn
14240 * emit code to clear PSTATE.SS
14241 * emit code to generate software step exception for completed step
14242 * end TB (as usual for having generated an exception)
14243 * SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14244 * emit code to generate a software step exception
14245 * end the TB
14247 dc->ss_active = FIELD_EX32(tb_flags, TBFLAG_ANY, SS_ACTIVE);
14248 dc->pstate_ss = FIELD_EX32(tb_flags, TBFLAG_ANY, PSTATE_SS);
14249 dc->is_ldex = false;
14250 dc->ss_same_el = (arm_debug_target_el(env) == dc->current_el);
14252 /* Bound the number of insns to execute to those left on the page. */
14253 bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14255 /* If architectural single step active, limit to 1. */
14256 if (dc->ss_active) {
14257 bound = 1;
14259 dc->base.max_insns = MIN(dc->base.max_insns, bound);
14261 init_tmp_a64_array(dc);
14264 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14268 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14270 DisasContext *dc = container_of(dcbase, DisasContext, base);
14272 tcg_gen_insn_start(dc->pc, 0, 0);
14273 dc->insn_start = tcg_last_op();
14276 static bool aarch64_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,
14277 const CPUBreakpoint *bp)
14279 DisasContext *dc = container_of(dcbase, DisasContext, base);
14281 if (bp->flags & BP_CPU) {
14282 gen_a64_set_pc_im(dc->pc);
14283 gen_helper_check_breakpoints(cpu_env);
14284 /* End the TB early; it likely won't be executed */
14285 dc->base.is_jmp = DISAS_TOO_MANY;
14286 } else {
14287 gen_exception_internal_insn(dc, 0, EXCP_DEBUG);
14288 /* The address covered by the breakpoint must be
14289 included in [tb->pc, tb->pc + tb->size) in order
14290 to for it to be properly cleared -- thus we
14291 increment the PC here so that the logic setting
14292 tb->size below does the right thing. */
14293 dc->pc += 4;
14294 dc->base.is_jmp = DISAS_NORETURN;
14297 return true;
14300 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14302 DisasContext *dc = container_of(dcbase, DisasContext, base);
14303 CPUARMState *env = cpu->env_ptr;
14305 if (dc->ss_active && !dc->pstate_ss) {
14306 /* Singlestep state is Active-pending.
14307 * If we're in this state at the start of a TB then either
14308 * a) we just took an exception to an EL which is being debugged
14309 * and this is the first insn in the exception handler
14310 * b) debug exceptions were masked and we just unmasked them
14311 * without changing EL (eg by clearing PSTATE.D)
14312 * In either case we're going to take a swstep exception in the
14313 * "did not step an insn" case, and so the syndrome ISV and EX
14314 * bits should be zero.
14316 assert(dc->base.num_insns == 1);
14317 gen_exception(EXCP_UDEF, syn_swstep(dc->ss_same_el, 0, 0),
14318 default_exception_el(dc));
14319 dc->base.is_jmp = DISAS_NORETURN;
14320 } else {
14321 disas_a64_insn(env, dc);
14324 dc->base.pc_next = dc->pc;
14325 translator_loop_temp_check(&dc->base);
14328 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14330 DisasContext *dc = container_of(dcbase, DisasContext, base);
14332 if (unlikely(dc->base.singlestep_enabled || dc->ss_active)) {
14333 /* Note that this means single stepping WFI doesn't halt the CPU.
14334 * For conditional branch insns this is harmless unreachable code as
14335 * gen_goto_tb() has already handled emitting the debug exception
14336 * (and thus a tb-jump is not possible when singlestepping).
14338 switch (dc->base.is_jmp) {
14339 default:
14340 gen_a64_set_pc_im(dc->pc);
14341 /* fall through */
14342 case DISAS_EXIT:
14343 case DISAS_JUMP:
14344 if (dc->base.singlestep_enabled) {
14345 gen_exception_internal(EXCP_DEBUG);
14346 } else {
14347 gen_step_complete_exception(dc);
14349 break;
14350 case DISAS_NORETURN:
14351 break;
14353 } else {
14354 switch (dc->base.is_jmp) {
14355 case DISAS_NEXT:
14356 case DISAS_TOO_MANY:
14357 gen_goto_tb(dc, 1, dc->pc);
14358 break;
14359 default:
14360 case DISAS_UPDATE:
14361 gen_a64_set_pc_im(dc->pc);
14362 /* fall through */
14363 case DISAS_EXIT:
14364 tcg_gen_exit_tb(NULL, 0);
14365 break;
14366 case DISAS_JUMP:
14367 tcg_gen_lookup_and_goto_ptr();
14368 break;
14369 case DISAS_NORETURN:
14370 case DISAS_SWI:
14371 break;
14372 case DISAS_WFE:
14373 gen_a64_set_pc_im(dc->pc);
14374 gen_helper_wfe(cpu_env);
14375 break;
14376 case DISAS_YIELD:
14377 gen_a64_set_pc_im(dc->pc);
14378 gen_helper_yield(cpu_env);
14379 break;
14380 case DISAS_WFI:
14382 /* This is a special case because we don't want to just halt the CPU
14383 * if trying to debug across a WFI.
14385 TCGv_i32 tmp = tcg_const_i32(4);
14387 gen_a64_set_pc_im(dc->pc);
14388 gen_helper_wfi(cpu_env, tmp);
14389 tcg_temp_free_i32(tmp);
14390 /* The helper doesn't necessarily throw an exception, but we
14391 * must go back to the main loop to check for interrupts anyway.
14393 tcg_gen_exit_tb(NULL, 0);
14394 break;
14399 /* Functions above can change dc->pc, so re-align db->pc_next */
14400 dc->base.pc_next = dc->pc;
14403 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14404 CPUState *cpu)
14406 DisasContext *dc = container_of(dcbase, DisasContext, base);
14408 qemu_log("IN: %s\n", lookup_symbol(dc->base.pc_first));
14409 log_target_disas(cpu, dc->base.pc_first, dc->base.tb->size);
14412 const TranslatorOps aarch64_translator_ops = {
14413 .init_disas_context = aarch64_tr_init_disas_context,
14414 .tb_start = aarch64_tr_tb_start,
14415 .insn_start = aarch64_tr_insn_start,
14416 .breakpoint_check = aarch64_tr_breakpoint_check,
14417 .translate_insn = aarch64_tr_translate_insn,
14418 .tb_stop = aarch64_tr_tb_stop,
14419 .disas_log = aarch64_tr_disas_log,