Merge remote-tracking branch 'remotes/pmaydell/tags/pull-target-arm-20190816' into...
[qemu/ar7.git] / target / arm / translate-a64.c
blobfc3e5f5c3894cfbcaeb902049ebff8ebbf6da83e
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 "hw/semihosting/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 gen_a64_set_pc_im(uint64_t val)
156 tcg_gen_movi_i64(cpu_pc, val);
160 * Handle Top Byte Ignore (TBI) bits.
162 * If address tagging is enabled via the TCR TBI bits:
163 * + for EL2 and EL3 there is only one TBI bit, and if it is set
164 * then the address is zero-extended, clearing bits [63:56]
165 * + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
166 * and TBI1 controls addressses with bit 55 == 1.
167 * If the appropriate TBI bit is set for the address then
168 * the address is sign-extended from bit 55 into bits [63:56]
170 * Here We have concatenated TBI{1,0} into tbi.
172 static void gen_top_byte_ignore(DisasContext *s, TCGv_i64 dst,
173 TCGv_i64 src, int tbi)
175 if (tbi == 0) {
176 /* Load unmodified address */
177 tcg_gen_mov_i64(dst, src);
178 } else if (s->current_el >= 2) {
179 /* FIXME: ARMv8.1-VHE S2 translation regime. */
180 /* Force tag byte to all zero */
181 tcg_gen_extract_i64(dst, src, 0, 56);
182 } else {
183 /* Sign-extend from bit 55. */
184 tcg_gen_sextract_i64(dst, src, 0, 56);
186 if (tbi != 3) {
187 TCGv_i64 tcg_zero = tcg_const_i64(0);
190 * The two TBI bits differ.
191 * If tbi0, then !tbi1: only use the extension if positive.
192 * if !tbi0, then tbi1: only use the extension if negative.
194 tcg_gen_movcond_i64(tbi == 1 ? TCG_COND_GE : TCG_COND_LT,
195 dst, dst, tcg_zero, dst, src);
196 tcg_temp_free_i64(tcg_zero);
201 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
204 * If address tagging is enabled for instructions via the TCR TBI bits,
205 * then loading an address into the PC will clear out any tag.
207 gen_top_byte_ignore(s, cpu_pc, src, s->tbii);
211 * Return a "clean" address for ADDR according to TBID.
212 * This is always a fresh temporary, as we need to be able to
213 * increment this independently of a dirty write-back address.
215 static TCGv_i64 clean_data_tbi(DisasContext *s, TCGv_i64 addr)
217 TCGv_i64 clean = new_tmp_a64(s);
218 gen_top_byte_ignore(s, clean, addr, s->tbid);
219 return clean;
222 typedef struct DisasCompare64 {
223 TCGCond cond;
224 TCGv_i64 value;
225 } DisasCompare64;
227 static void a64_test_cc(DisasCompare64 *c64, int cc)
229 DisasCompare c32;
231 arm_test_cc(&c32, cc);
233 /* Sign-extend the 32-bit value so that the GE/LT comparisons work
234 * properly. The NE/EQ comparisons are also fine with this choice. */
235 c64->cond = c32.cond;
236 c64->value = tcg_temp_new_i64();
237 tcg_gen_ext_i32_i64(c64->value, c32.value);
239 arm_free_cc(&c32);
242 static void a64_free_cc(DisasCompare64 *c64)
244 tcg_temp_free_i64(c64->value);
247 static void gen_exception_internal(int excp)
249 TCGv_i32 tcg_excp = tcg_const_i32(excp);
251 assert(excp_is_internal(excp));
252 gen_helper_exception_internal(cpu_env, tcg_excp);
253 tcg_temp_free_i32(tcg_excp);
256 static void gen_exception_internal_insn(DisasContext *s, uint64_t pc, int excp)
258 gen_a64_set_pc_im(pc);
259 gen_exception_internal(excp);
260 s->base.is_jmp = DISAS_NORETURN;
263 static void gen_exception_insn(DisasContext *s, uint64_t pc, int excp,
264 uint32_t syndrome, uint32_t target_el)
266 gen_a64_set_pc_im(pc);
267 gen_exception(excp, syndrome, target_el);
268 s->base.is_jmp = DISAS_NORETURN;
271 static void gen_exception_bkpt_insn(DisasContext *s, uint32_t syndrome)
273 TCGv_i32 tcg_syn;
275 gen_a64_set_pc_im(s->pc_curr);
276 tcg_syn = tcg_const_i32(syndrome);
277 gen_helper_exception_bkpt_insn(cpu_env, tcg_syn);
278 tcg_temp_free_i32(tcg_syn);
279 s->base.is_jmp = DISAS_NORETURN;
282 static void gen_step_complete_exception(DisasContext *s)
284 /* We just completed step of an insn. Move from Active-not-pending
285 * to Active-pending, and then also take the swstep exception.
286 * This corresponds to making the (IMPDEF) choice to prioritize
287 * swstep exceptions over asynchronous exceptions taken to an exception
288 * level where debug is disabled. This choice has the advantage that
289 * we do not need to maintain internal state corresponding to the
290 * ISV/EX syndrome bits between completion of the step and generation
291 * of the exception, and our syndrome information is always correct.
293 gen_ss_advance(s);
294 gen_swstep_exception(s, 1, s->is_ldex);
295 s->base.is_jmp = DISAS_NORETURN;
298 static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
300 /* No direct tb linking with singlestep (either QEMU's or the ARM
301 * debug architecture kind) or deterministic io
303 if (s->base.singlestep_enabled || s->ss_active ||
304 (tb_cflags(s->base.tb) & CF_LAST_IO)) {
305 return false;
308 #ifndef CONFIG_USER_ONLY
309 /* Only link tbs from inside the same guest page */
310 if ((s->base.tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
311 return false;
313 #endif
315 return true;
318 static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
320 TranslationBlock *tb;
322 tb = s->base.tb;
323 if (use_goto_tb(s, n, dest)) {
324 tcg_gen_goto_tb(n);
325 gen_a64_set_pc_im(dest);
326 tcg_gen_exit_tb(tb, n);
327 s->base.is_jmp = DISAS_NORETURN;
328 } else {
329 gen_a64_set_pc_im(dest);
330 if (s->ss_active) {
331 gen_step_complete_exception(s);
332 } else if (s->base.singlestep_enabled) {
333 gen_exception_internal(EXCP_DEBUG);
334 } else {
335 tcg_gen_lookup_and_goto_ptr();
336 s->base.is_jmp = DISAS_NORETURN;
341 static void init_tmp_a64_array(DisasContext *s)
343 #ifdef CONFIG_DEBUG_TCG
344 memset(s->tmp_a64, 0, sizeof(s->tmp_a64));
345 #endif
346 s->tmp_a64_count = 0;
349 static void free_tmp_a64(DisasContext *s)
351 int i;
352 for (i = 0; i < s->tmp_a64_count; i++) {
353 tcg_temp_free_i64(s->tmp_a64[i]);
355 init_tmp_a64_array(s);
358 TCGv_i64 new_tmp_a64(DisasContext *s)
360 assert(s->tmp_a64_count < TMP_A64_MAX);
361 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
364 TCGv_i64 new_tmp_a64_zero(DisasContext *s)
366 TCGv_i64 t = new_tmp_a64(s);
367 tcg_gen_movi_i64(t, 0);
368 return t;
372 * Register access functions
374 * These functions are used for directly accessing a register in where
375 * changes to the final register value are likely to be made. If you
376 * need to use a register for temporary calculation (e.g. index type
377 * operations) use the read_* form.
379 * B1.2.1 Register mappings
381 * In instruction register encoding 31 can refer to ZR (zero register) or
382 * the SP (stack pointer) depending on context. In QEMU's case we map SP
383 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
384 * This is the point of the _sp forms.
386 TCGv_i64 cpu_reg(DisasContext *s, int reg)
388 if (reg == 31) {
389 return new_tmp_a64_zero(s);
390 } else {
391 return cpu_X[reg];
395 /* register access for when 31 == SP */
396 TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
398 return cpu_X[reg];
401 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
402 * representing the register contents. This TCGv is an auto-freed
403 * temporary so it need not be explicitly freed, and may be modified.
405 TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
407 TCGv_i64 v = new_tmp_a64(s);
408 if (reg != 31) {
409 if (sf) {
410 tcg_gen_mov_i64(v, cpu_X[reg]);
411 } else {
412 tcg_gen_ext32u_i64(v, cpu_X[reg]);
414 } else {
415 tcg_gen_movi_i64(v, 0);
417 return v;
420 TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
422 TCGv_i64 v = new_tmp_a64(s);
423 if (sf) {
424 tcg_gen_mov_i64(v, cpu_X[reg]);
425 } else {
426 tcg_gen_ext32u_i64(v, cpu_X[reg]);
428 return v;
431 /* Return the offset into CPUARMState of a slice (from
432 * the least significant end) of FP register Qn (ie
433 * Dn, Sn, Hn or Bn).
434 * (Note that this is not the same mapping as for A32; see cpu.h)
436 static inline int fp_reg_offset(DisasContext *s, int regno, TCGMemOp size)
438 return vec_reg_offset(s, regno, 0, size);
441 /* Offset of the high half of the 128 bit vector Qn */
442 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
444 return vec_reg_offset(s, regno, 1, MO_64);
447 /* Convenience accessors for reading and writing single and double
448 * FP registers. Writing clears the upper parts of the associated
449 * 128 bit vector register, as required by the architecture.
450 * Note that unlike the GP register accessors, the values returned
451 * by the read functions must be manually freed.
453 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
455 TCGv_i64 v = tcg_temp_new_i64();
457 tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
458 return v;
461 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
463 TCGv_i32 v = tcg_temp_new_i32();
465 tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
466 return v;
469 static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
471 TCGv_i32 v = tcg_temp_new_i32();
473 tcg_gen_ld16u_i32(v, cpu_env, fp_reg_offset(s, reg, MO_16));
474 return v;
477 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
478 * If SVE is not enabled, then there are only 128 bits in the vector.
480 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
482 unsigned ofs = fp_reg_offset(s, rd, MO_64);
483 unsigned vsz = vec_full_reg_size(s);
485 if (!is_q) {
486 TCGv_i64 tcg_zero = tcg_const_i64(0);
487 tcg_gen_st_i64(tcg_zero, cpu_env, ofs + 8);
488 tcg_temp_free_i64(tcg_zero);
490 if (vsz > 16) {
491 tcg_gen_gvec_dup8i(ofs + 16, vsz - 16, vsz - 16, 0);
495 void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
497 unsigned ofs = fp_reg_offset(s, reg, MO_64);
499 tcg_gen_st_i64(v, cpu_env, ofs);
500 clear_vec_high(s, false, reg);
503 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
505 TCGv_i64 tmp = tcg_temp_new_i64();
507 tcg_gen_extu_i32_i64(tmp, v);
508 write_fp_dreg(s, reg, tmp);
509 tcg_temp_free_i64(tmp);
512 TCGv_ptr get_fpstatus_ptr(bool is_f16)
514 TCGv_ptr statusptr = tcg_temp_new_ptr();
515 int offset;
517 /* In A64 all instructions (both FP and Neon) use the FPCR; there
518 * is no equivalent of the A32 Neon "standard FPSCR value".
519 * However half-precision operations operate under a different
520 * FZ16 flag and use vfp.fp_status_f16 instead of vfp.fp_status.
522 if (is_f16) {
523 offset = offsetof(CPUARMState, vfp.fp_status_f16);
524 } else {
525 offset = offsetof(CPUARMState, vfp.fp_status);
527 tcg_gen_addi_ptr(statusptr, cpu_env, offset);
528 return statusptr;
531 /* Expand a 2-operand AdvSIMD vector operation using an expander function. */
532 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
533 GVecGen2Fn *gvec_fn, int vece)
535 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
536 is_q ? 16 : 8, vec_full_reg_size(s));
539 /* Expand a 2-operand + immediate AdvSIMD vector operation using
540 * an expander function.
542 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
543 int64_t imm, GVecGen2iFn *gvec_fn, int vece)
545 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
546 imm, is_q ? 16 : 8, vec_full_reg_size(s));
549 /* Expand a 3-operand AdvSIMD vector operation using an expander function. */
550 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
551 GVecGen3Fn *gvec_fn, int vece)
553 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
554 vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
557 /* Expand a 4-operand AdvSIMD vector operation using an expander function. */
558 static void gen_gvec_fn4(DisasContext *s, bool is_q, int rd, int rn, int rm,
559 int rx, GVecGen4Fn *gvec_fn, int vece)
561 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
562 vec_full_reg_offset(s, rm), vec_full_reg_offset(s, rx),
563 is_q ? 16 : 8, vec_full_reg_size(s));
566 /* Expand a 2-operand + immediate AdvSIMD vector operation using
567 * an op descriptor.
569 static void gen_gvec_op2i(DisasContext *s, bool is_q, int rd,
570 int rn, int64_t imm, const GVecGen2i *gvec_op)
572 tcg_gen_gvec_2i(vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
573 is_q ? 16 : 8, vec_full_reg_size(s), imm, gvec_op);
576 /* Expand a 3-operand AdvSIMD vector operation using an op descriptor. */
577 static void gen_gvec_op3(DisasContext *s, bool is_q, int rd,
578 int rn, int rm, const GVecGen3 *gvec_op)
580 tcg_gen_gvec_3(vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
581 vec_full_reg_offset(s, rm), is_q ? 16 : 8,
582 vec_full_reg_size(s), gvec_op);
585 /* Expand a 3-operand operation using an out-of-line helper. */
586 static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
587 int rn, int rm, int data, gen_helper_gvec_3 *fn)
589 tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
590 vec_full_reg_offset(s, rn),
591 vec_full_reg_offset(s, rm),
592 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
595 /* Expand a 3-operand + env pointer operation using
596 * an out-of-line helper.
598 static void gen_gvec_op3_env(DisasContext *s, bool is_q, int rd,
599 int rn, int rm, gen_helper_gvec_3_ptr *fn)
601 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
602 vec_full_reg_offset(s, rn),
603 vec_full_reg_offset(s, rm), cpu_env,
604 is_q ? 16 : 8, vec_full_reg_size(s), 0, fn);
607 /* Expand a 3-operand + fpstatus pointer + simd data value operation using
608 * an out-of-line helper.
610 static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
611 int rm, bool is_fp16, int data,
612 gen_helper_gvec_3_ptr *fn)
614 TCGv_ptr fpst = get_fpstatus_ptr(is_fp16);
615 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
616 vec_full_reg_offset(s, rn),
617 vec_full_reg_offset(s, rm), fpst,
618 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
619 tcg_temp_free_ptr(fpst);
622 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
623 * than the 32 bit equivalent.
625 static inline void gen_set_NZ64(TCGv_i64 result)
627 tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
628 tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
631 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
632 static inline void gen_logic_CC(int sf, TCGv_i64 result)
634 if (sf) {
635 gen_set_NZ64(result);
636 } else {
637 tcg_gen_extrl_i64_i32(cpu_ZF, result);
638 tcg_gen_mov_i32(cpu_NF, cpu_ZF);
640 tcg_gen_movi_i32(cpu_CF, 0);
641 tcg_gen_movi_i32(cpu_VF, 0);
644 /* dest = T0 + T1; compute C, N, V and Z flags */
645 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
647 if (sf) {
648 TCGv_i64 result, flag, tmp;
649 result = tcg_temp_new_i64();
650 flag = tcg_temp_new_i64();
651 tmp = tcg_temp_new_i64();
653 tcg_gen_movi_i64(tmp, 0);
654 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
656 tcg_gen_extrl_i64_i32(cpu_CF, flag);
658 gen_set_NZ64(result);
660 tcg_gen_xor_i64(flag, result, t0);
661 tcg_gen_xor_i64(tmp, t0, t1);
662 tcg_gen_andc_i64(flag, flag, tmp);
663 tcg_temp_free_i64(tmp);
664 tcg_gen_extrh_i64_i32(cpu_VF, flag);
666 tcg_gen_mov_i64(dest, result);
667 tcg_temp_free_i64(result);
668 tcg_temp_free_i64(flag);
669 } else {
670 /* 32 bit arithmetic */
671 TCGv_i32 t0_32 = tcg_temp_new_i32();
672 TCGv_i32 t1_32 = tcg_temp_new_i32();
673 TCGv_i32 tmp = tcg_temp_new_i32();
675 tcg_gen_movi_i32(tmp, 0);
676 tcg_gen_extrl_i64_i32(t0_32, t0);
677 tcg_gen_extrl_i64_i32(t1_32, t1);
678 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
679 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
680 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
681 tcg_gen_xor_i32(tmp, t0_32, t1_32);
682 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
683 tcg_gen_extu_i32_i64(dest, cpu_NF);
685 tcg_temp_free_i32(tmp);
686 tcg_temp_free_i32(t0_32);
687 tcg_temp_free_i32(t1_32);
691 /* dest = T0 - T1; compute C, N, V and Z flags */
692 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
694 if (sf) {
695 /* 64 bit arithmetic */
696 TCGv_i64 result, flag, tmp;
698 result = tcg_temp_new_i64();
699 flag = tcg_temp_new_i64();
700 tcg_gen_sub_i64(result, t0, t1);
702 gen_set_NZ64(result);
704 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
705 tcg_gen_extrl_i64_i32(cpu_CF, flag);
707 tcg_gen_xor_i64(flag, result, t0);
708 tmp = tcg_temp_new_i64();
709 tcg_gen_xor_i64(tmp, t0, t1);
710 tcg_gen_and_i64(flag, flag, tmp);
711 tcg_temp_free_i64(tmp);
712 tcg_gen_extrh_i64_i32(cpu_VF, flag);
713 tcg_gen_mov_i64(dest, result);
714 tcg_temp_free_i64(flag);
715 tcg_temp_free_i64(result);
716 } else {
717 /* 32 bit arithmetic */
718 TCGv_i32 t0_32 = tcg_temp_new_i32();
719 TCGv_i32 t1_32 = tcg_temp_new_i32();
720 TCGv_i32 tmp;
722 tcg_gen_extrl_i64_i32(t0_32, t0);
723 tcg_gen_extrl_i64_i32(t1_32, t1);
724 tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
725 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
726 tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
727 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
728 tmp = tcg_temp_new_i32();
729 tcg_gen_xor_i32(tmp, t0_32, t1_32);
730 tcg_temp_free_i32(t0_32);
731 tcg_temp_free_i32(t1_32);
732 tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
733 tcg_temp_free_i32(tmp);
734 tcg_gen_extu_i32_i64(dest, cpu_NF);
738 /* dest = T0 + T1 + CF; do not compute flags. */
739 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
741 TCGv_i64 flag = tcg_temp_new_i64();
742 tcg_gen_extu_i32_i64(flag, cpu_CF);
743 tcg_gen_add_i64(dest, t0, t1);
744 tcg_gen_add_i64(dest, dest, flag);
745 tcg_temp_free_i64(flag);
747 if (!sf) {
748 tcg_gen_ext32u_i64(dest, dest);
752 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
753 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
755 if (sf) {
756 TCGv_i64 result, cf_64, vf_64, tmp;
757 result = tcg_temp_new_i64();
758 cf_64 = tcg_temp_new_i64();
759 vf_64 = tcg_temp_new_i64();
760 tmp = tcg_const_i64(0);
762 tcg_gen_extu_i32_i64(cf_64, cpu_CF);
763 tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
764 tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
765 tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
766 gen_set_NZ64(result);
768 tcg_gen_xor_i64(vf_64, result, t0);
769 tcg_gen_xor_i64(tmp, t0, t1);
770 tcg_gen_andc_i64(vf_64, vf_64, tmp);
771 tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
773 tcg_gen_mov_i64(dest, result);
775 tcg_temp_free_i64(tmp);
776 tcg_temp_free_i64(vf_64);
777 tcg_temp_free_i64(cf_64);
778 tcg_temp_free_i64(result);
779 } else {
780 TCGv_i32 t0_32, t1_32, tmp;
781 t0_32 = tcg_temp_new_i32();
782 t1_32 = tcg_temp_new_i32();
783 tmp = tcg_const_i32(0);
785 tcg_gen_extrl_i64_i32(t0_32, t0);
786 tcg_gen_extrl_i64_i32(t1_32, t1);
787 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
788 tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
790 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
791 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
792 tcg_gen_xor_i32(tmp, t0_32, t1_32);
793 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
794 tcg_gen_extu_i32_i64(dest, cpu_NF);
796 tcg_temp_free_i32(tmp);
797 tcg_temp_free_i32(t1_32);
798 tcg_temp_free_i32(t0_32);
803 * Load/Store generators
807 * Store from GPR register to memory.
809 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
810 TCGv_i64 tcg_addr, int size, int memidx,
811 bool iss_valid,
812 unsigned int iss_srt,
813 bool iss_sf, bool iss_ar)
815 g_assert(size <= 3);
816 tcg_gen_qemu_st_i64(source, tcg_addr, memidx, s->be_data + size);
818 if (iss_valid) {
819 uint32_t syn;
821 syn = syn_data_abort_with_iss(0,
822 size,
823 false,
824 iss_srt,
825 iss_sf,
826 iss_ar,
827 0, 0, 0, 0, 0, false);
828 disas_set_insn_syndrome(s, syn);
832 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
833 TCGv_i64 tcg_addr, int size,
834 bool iss_valid,
835 unsigned int iss_srt,
836 bool iss_sf, bool iss_ar)
838 do_gpr_st_memidx(s, source, tcg_addr, size, get_mem_index(s),
839 iss_valid, iss_srt, iss_sf, iss_ar);
843 * Load from memory to GPR register
845 static void do_gpr_ld_memidx(DisasContext *s,
846 TCGv_i64 dest, TCGv_i64 tcg_addr,
847 int size, bool is_signed,
848 bool extend, int memidx,
849 bool iss_valid, unsigned int iss_srt,
850 bool iss_sf, bool iss_ar)
852 TCGMemOp memop = s->be_data + size;
854 g_assert(size <= 3);
856 if (is_signed) {
857 memop += MO_SIGN;
860 tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
862 if (extend && is_signed) {
863 g_assert(size < 3);
864 tcg_gen_ext32u_i64(dest, dest);
867 if (iss_valid) {
868 uint32_t syn;
870 syn = syn_data_abort_with_iss(0,
871 size,
872 is_signed,
873 iss_srt,
874 iss_sf,
875 iss_ar,
876 0, 0, 0, 0, 0, false);
877 disas_set_insn_syndrome(s, syn);
881 static void do_gpr_ld(DisasContext *s,
882 TCGv_i64 dest, TCGv_i64 tcg_addr,
883 int size, bool is_signed, bool extend,
884 bool iss_valid, unsigned int iss_srt,
885 bool iss_sf, bool iss_ar)
887 do_gpr_ld_memidx(s, dest, tcg_addr, size, is_signed, extend,
888 get_mem_index(s),
889 iss_valid, iss_srt, iss_sf, iss_ar);
893 * Store from FP register to memory
895 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
897 /* This writes the bottom N bits of a 128 bit wide vector to memory */
898 TCGv_i64 tmp = tcg_temp_new_i64();
899 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_offset(s, srcidx, MO_64));
900 if (size < 4) {
901 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s),
902 s->be_data + size);
903 } else {
904 bool be = s->be_data == MO_BE;
905 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
907 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
908 tcg_gen_qemu_st_i64(tmp, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
909 s->be_data | MO_Q);
910 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_hi_offset(s, srcidx));
911 tcg_gen_qemu_st_i64(tmp, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
912 s->be_data | MO_Q);
913 tcg_temp_free_i64(tcg_hiaddr);
916 tcg_temp_free_i64(tmp);
920 * Load from memory to FP register
922 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
924 /* This always zero-extends and writes to a full 128 bit wide vector */
925 TCGv_i64 tmplo = tcg_temp_new_i64();
926 TCGv_i64 tmphi;
928 if (size < 4) {
929 TCGMemOp memop = s->be_data + size;
930 tmphi = tcg_const_i64(0);
931 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
932 } else {
933 bool be = s->be_data == MO_BE;
934 TCGv_i64 tcg_hiaddr;
936 tmphi = tcg_temp_new_i64();
937 tcg_hiaddr = tcg_temp_new_i64();
939 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
940 tcg_gen_qemu_ld_i64(tmplo, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
941 s->be_data | MO_Q);
942 tcg_gen_qemu_ld_i64(tmphi, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
943 s->be_data | MO_Q);
944 tcg_temp_free_i64(tcg_hiaddr);
947 tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
948 tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
950 tcg_temp_free_i64(tmplo);
951 tcg_temp_free_i64(tmphi);
953 clear_vec_high(s, true, destidx);
957 * Vector load/store helpers.
959 * The principal difference between this and a FP load is that we don't
960 * zero extend as we are filling a partial chunk of the vector register.
961 * These functions don't support 128 bit loads/stores, which would be
962 * normal load/store operations.
964 * The _i32 versions are useful when operating on 32 bit quantities
965 * (eg for floating point single or using Neon helper functions).
968 /* Get value of an element within a vector register */
969 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
970 int element, TCGMemOp memop)
972 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
973 switch (memop) {
974 case MO_8:
975 tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
976 break;
977 case MO_16:
978 tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
979 break;
980 case MO_32:
981 tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
982 break;
983 case MO_8|MO_SIGN:
984 tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
985 break;
986 case MO_16|MO_SIGN:
987 tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
988 break;
989 case MO_32|MO_SIGN:
990 tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
991 break;
992 case MO_64:
993 case MO_64|MO_SIGN:
994 tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
995 break;
996 default:
997 g_assert_not_reached();
1001 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1002 int element, TCGMemOp memop)
1004 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1005 switch (memop) {
1006 case MO_8:
1007 tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1008 break;
1009 case MO_16:
1010 tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1011 break;
1012 case MO_8|MO_SIGN:
1013 tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1014 break;
1015 case MO_16|MO_SIGN:
1016 tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1017 break;
1018 case MO_32:
1019 case MO_32|MO_SIGN:
1020 tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1021 break;
1022 default:
1023 g_assert_not_reached();
1027 /* Set value of an element within a vector register */
1028 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1029 int element, TCGMemOp memop)
1031 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1032 switch (memop) {
1033 case MO_8:
1034 tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1035 break;
1036 case MO_16:
1037 tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1038 break;
1039 case MO_32:
1040 tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1041 break;
1042 case MO_64:
1043 tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1044 break;
1045 default:
1046 g_assert_not_reached();
1050 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1051 int destidx, int element, TCGMemOp memop)
1053 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1054 switch (memop) {
1055 case MO_8:
1056 tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1057 break;
1058 case MO_16:
1059 tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1060 break;
1061 case MO_32:
1062 tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1063 break;
1064 default:
1065 g_assert_not_reached();
1069 /* Store from vector register to memory */
1070 static void do_vec_st(DisasContext *s, int srcidx, int element,
1071 TCGv_i64 tcg_addr, int size, TCGMemOp endian)
1073 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1075 read_vec_element(s, tcg_tmp, srcidx, element, size);
1076 tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), endian | size);
1078 tcg_temp_free_i64(tcg_tmp);
1081 /* Load from memory to vector register */
1082 static void do_vec_ld(DisasContext *s, int destidx, int element,
1083 TCGv_i64 tcg_addr, int size, TCGMemOp endian)
1085 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1087 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), endian | size);
1088 write_vec_element(s, tcg_tmp, destidx, element, size);
1090 tcg_temp_free_i64(tcg_tmp);
1093 /* Check that FP/Neon access is enabled. If it is, return
1094 * true. If not, emit code to generate an appropriate exception,
1095 * and return false; the caller should not emit any code for
1096 * the instruction. Note that this check must happen after all
1097 * unallocated-encoding checks (otherwise the syndrome information
1098 * for the resulting exception will be incorrect).
1100 static inline bool fp_access_check(DisasContext *s)
1102 assert(!s->fp_access_checked);
1103 s->fp_access_checked = true;
1105 if (!s->fp_excp_el) {
1106 return true;
1109 gen_exception_insn(s, s->pc_curr, EXCP_UDEF,
1110 syn_fp_access_trap(1, 0xe, false), s->fp_excp_el);
1111 return false;
1114 /* Check that SVE access is enabled. If it is, return true.
1115 * If not, emit code to generate an appropriate exception and return false.
1117 bool sve_access_check(DisasContext *s)
1119 if (s->sve_excp_el) {
1120 gen_exception_insn(s, s->pc_curr, EXCP_UDEF, syn_sve_access_trap(),
1121 s->sve_excp_el);
1122 return false;
1124 return fp_access_check(s);
1128 * This utility function is for doing register extension with an
1129 * optional shift. You will likely want to pass a temporary for the
1130 * destination register. See DecodeRegExtend() in the ARM ARM.
1132 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1133 int option, unsigned int shift)
1135 int extsize = extract32(option, 0, 2);
1136 bool is_signed = extract32(option, 2, 1);
1138 if (is_signed) {
1139 switch (extsize) {
1140 case 0:
1141 tcg_gen_ext8s_i64(tcg_out, tcg_in);
1142 break;
1143 case 1:
1144 tcg_gen_ext16s_i64(tcg_out, tcg_in);
1145 break;
1146 case 2:
1147 tcg_gen_ext32s_i64(tcg_out, tcg_in);
1148 break;
1149 case 3:
1150 tcg_gen_mov_i64(tcg_out, tcg_in);
1151 break;
1153 } else {
1154 switch (extsize) {
1155 case 0:
1156 tcg_gen_ext8u_i64(tcg_out, tcg_in);
1157 break;
1158 case 1:
1159 tcg_gen_ext16u_i64(tcg_out, tcg_in);
1160 break;
1161 case 2:
1162 tcg_gen_ext32u_i64(tcg_out, tcg_in);
1163 break;
1164 case 3:
1165 tcg_gen_mov_i64(tcg_out, tcg_in);
1166 break;
1170 if (shift) {
1171 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1175 static inline void gen_check_sp_alignment(DisasContext *s)
1177 /* The AArch64 architecture mandates that (if enabled via PSTATE
1178 * or SCTLR bits) there is a check that SP is 16-aligned on every
1179 * SP-relative load or store (with an exception generated if it is not).
1180 * In line with general QEMU practice regarding misaligned accesses,
1181 * we omit these checks for the sake of guest program performance.
1182 * This function is provided as a hook so we can more easily add these
1183 * checks in future (possibly as a "favour catching guest program bugs
1184 * over speed" user selectable option).
1189 * This provides a simple table based table lookup decoder. It is
1190 * intended to be used when the relevant bits for decode are too
1191 * awkwardly placed and switch/if based logic would be confusing and
1192 * deeply nested. Since it's a linear search through the table, tables
1193 * should be kept small.
1195 * It returns the first handler where insn & mask == pattern, or
1196 * NULL if there is no match.
1197 * The table is terminated by an empty mask (i.e. 0)
1199 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1200 uint32_t insn)
1202 const AArch64DecodeTable *tptr = table;
1204 while (tptr->mask) {
1205 if ((insn & tptr->mask) == tptr->pattern) {
1206 return tptr->disas_fn;
1208 tptr++;
1210 return NULL;
1214 * The instruction disassembly implemented here matches
1215 * the instruction encoding classifications in chapter C4
1216 * of the ARM Architecture Reference Manual (DDI0487B_a);
1217 * classification names and decode diagrams here should generally
1218 * match up with those in the manual.
1221 /* Unconditional branch (immediate)
1222 * 31 30 26 25 0
1223 * +----+-----------+-------------------------------------+
1224 * | op | 0 0 1 0 1 | imm26 |
1225 * +----+-----------+-------------------------------------+
1227 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
1229 uint64_t addr = s->pc_curr + sextract32(insn, 0, 26) * 4;
1231 if (insn & (1U << 31)) {
1232 /* BL Branch with link */
1233 tcg_gen_movi_i64(cpu_reg(s, 30), s->base.pc_next);
1236 /* B Branch / BL Branch with link */
1237 reset_btype(s);
1238 gen_goto_tb(s, 0, addr);
1241 /* Compare and branch (immediate)
1242 * 31 30 25 24 23 5 4 0
1243 * +----+-------------+----+---------------------+--------+
1244 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
1245 * +----+-------------+----+---------------------+--------+
1247 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
1249 unsigned int sf, op, rt;
1250 uint64_t addr;
1251 TCGLabel *label_match;
1252 TCGv_i64 tcg_cmp;
1254 sf = extract32(insn, 31, 1);
1255 op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
1256 rt = extract32(insn, 0, 5);
1257 addr = s->pc_curr + sextract32(insn, 5, 19) * 4;
1259 tcg_cmp = read_cpu_reg(s, rt, sf);
1260 label_match = gen_new_label();
1262 reset_btype(s);
1263 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1264 tcg_cmp, 0, label_match);
1266 gen_goto_tb(s, 0, s->base.pc_next);
1267 gen_set_label(label_match);
1268 gen_goto_tb(s, 1, addr);
1271 /* Test and branch (immediate)
1272 * 31 30 25 24 23 19 18 5 4 0
1273 * +----+-------------+----+-------+-------------+------+
1274 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
1275 * +----+-------------+----+-------+-------------+------+
1277 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1279 unsigned int bit_pos, op, rt;
1280 uint64_t addr;
1281 TCGLabel *label_match;
1282 TCGv_i64 tcg_cmp;
1284 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1285 op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1286 addr = s->pc_curr + sextract32(insn, 5, 14) * 4;
1287 rt = extract32(insn, 0, 5);
1289 tcg_cmp = tcg_temp_new_i64();
1290 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1291 label_match = gen_new_label();
1293 reset_btype(s);
1294 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1295 tcg_cmp, 0, label_match);
1296 tcg_temp_free_i64(tcg_cmp);
1297 gen_goto_tb(s, 0, s->base.pc_next);
1298 gen_set_label(label_match);
1299 gen_goto_tb(s, 1, addr);
1302 /* Conditional branch (immediate)
1303 * 31 25 24 23 5 4 3 0
1304 * +---------------+----+---------------------+----+------+
1305 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
1306 * +---------------+----+---------------------+----+------+
1308 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1310 unsigned int cond;
1311 uint64_t addr;
1313 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1314 unallocated_encoding(s);
1315 return;
1317 addr = s->pc_curr + sextract32(insn, 5, 19) * 4;
1318 cond = extract32(insn, 0, 4);
1320 reset_btype(s);
1321 if (cond < 0x0e) {
1322 /* genuinely conditional branches */
1323 TCGLabel *label_match = gen_new_label();
1324 arm_gen_test_cc(cond, label_match);
1325 gen_goto_tb(s, 0, s->base.pc_next);
1326 gen_set_label(label_match);
1327 gen_goto_tb(s, 1, addr);
1328 } else {
1329 /* 0xe and 0xf are both "always" conditions */
1330 gen_goto_tb(s, 0, addr);
1334 /* HINT instruction group, including various allocated HINTs */
1335 static void handle_hint(DisasContext *s, uint32_t insn,
1336 unsigned int op1, unsigned int op2, unsigned int crm)
1338 unsigned int selector = crm << 3 | op2;
1340 if (op1 != 3) {
1341 unallocated_encoding(s);
1342 return;
1345 switch (selector) {
1346 case 0b00000: /* NOP */
1347 break;
1348 case 0b00011: /* WFI */
1349 s->base.is_jmp = DISAS_WFI;
1350 break;
1351 case 0b00001: /* YIELD */
1352 /* When running in MTTCG we don't generate jumps to the yield and
1353 * WFE helpers as it won't affect the scheduling of other vCPUs.
1354 * If we wanted to more completely model WFE/SEV so we don't busy
1355 * spin unnecessarily we would need to do something more involved.
1357 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1358 s->base.is_jmp = DISAS_YIELD;
1360 break;
1361 case 0b00010: /* WFE */
1362 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1363 s->base.is_jmp = DISAS_WFE;
1365 break;
1366 case 0b00100: /* SEV */
1367 case 0b00101: /* SEVL */
1368 /* we treat all as NOP at least for now */
1369 break;
1370 case 0b00111: /* XPACLRI */
1371 if (s->pauth_active) {
1372 gen_helper_xpaci(cpu_X[30], cpu_env, cpu_X[30]);
1374 break;
1375 case 0b01000: /* PACIA1716 */
1376 if (s->pauth_active) {
1377 gen_helper_pacia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1379 break;
1380 case 0b01010: /* PACIB1716 */
1381 if (s->pauth_active) {
1382 gen_helper_pacib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1384 break;
1385 case 0b01100: /* AUTIA1716 */
1386 if (s->pauth_active) {
1387 gen_helper_autia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1389 break;
1390 case 0b01110: /* AUTIB1716 */
1391 if (s->pauth_active) {
1392 gen_helper_autib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1394 break;
1395 case 0b11000: /* PACIAZ */
1396 if (s->pauth_active) {
1397 gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30],
1398 new_tmp_a64_zero(s));
1400 break;
1401 case 0b11001: /* PACIASP */
1402 if (s->pauth_active) {
1403 gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1405 break;
1406 case 0b11010: /* PACIBZ */
1407 if (s->pauth_active) {
1408 gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30],
1409 new_tmp_a64_zero(s));
1411 break;
1412 case 0b11011: /* PACIBSP */
1413 if (s->pauth_active) {
1414 gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1416 break;
1417 case 0b11100: /* AUTIAZ */
1418 if (s->pauth_active) {
1419 gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30],
1420 new_tmp_a64_zero(s));
1422 break;
1423 case 0b11101: /* AUTIASP */
1424 if (s->pauth_active) {
1425 gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1427 break;
1428 case 0b11110: /* AUTIBZ */
1429 if (s->pauth_active) {
1430 gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30],
1431 new_tmp_a64_zero(s));
1433 break;
1434 case 0b11111: /* AUTIBSP */
1435 if (s->pauth_active) {
1436 gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1438 break;
1439 default:
1440 /* default specified as NOP equivalent */
1441 break;
1445 static void gen_clrex(DisasContext *s, uint32_t insn)
1447 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1450 /* CLREX, DSB, DMB, ISB */
1451 static void handle_sync(DisasContext *s, uint32_t insn,
1452 unsigned int op1, unsigned int op2, unsigned int crm)
1454 TCGBar bar;
1456 if (op1 != 3) {
1457 unallocated_encoding(s);
1458 return;
1461 switch (op2) {
1462 case 2: /* CLREX */
1463 gen_clrex(s, insn);
1464 return;
1465 case 4: /* DSB */
1466 case 5: /* DMB */
1467 switch (crm & 3) {
1468 case 1: /* MBReqTypes_Reads */
1469 bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1470 break;
1471 case 2: /* MBReqTypes_Writes */
1472 bar = TCG_BAR_SC | TCG_MO_ST_ST;
1473 break;
1474 default: /* MBReqTypes_All */
1475 bar = TCG_BAR_SC | TCG_MO_ALL;
1476 break;
1478 tcg_gen_mb(bar);
1479 return;
1480 case 6: /* ISB */
1481 /* We need to break the TB after this insn to execute
1482 * a self-modified code correctly and also to take
1483 * any pending interrupts immediately.
1485 reset_btype(s);
1486 gen_goto_tb(s, 0, s->base.pc_next);
1487 return;
1489 case 7: /* SB */
1490 if (crm != 0 || !dc_isar_feature(aa64_sb, s)) {
1491 goto do_unallocated;
1494 * TODO: There is no speculation barrier opcode for TCG;
1495 * MB and end the TB instead.
1497 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1498 gen_goto_tb(s, 0, s->base.pc_next);
1499 return;
1501 default:
1502 do_unallocated:
1503 unallocated_encoding(s);
1504 return;
1508 static void gen_xaflag(void)
1510 TCGv_i32 z = tcg_temp_new_i32();
1512 tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1515 * (!C & !Z) << 31
1516 * (!(C | Z)) << 31
1517 * ~((C | Z) << 31)
1518 * ~-(C | Z)
1519 * (C | Z) - 1
1521 tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1522 tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1524 /* !(Z & C) */
1525 tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1526 tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1528 /* (!C & Z) << 31 -> -(Z & ~C) */
1529 tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1530 tcg_gen_neg_i32(cpu_VF, cpu_VF);
1532 /* C | Z */
1533 tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1535 tcg_temp_free_i32(z);
1538 static void gen_axflag(void)
1540 tcg_gen_sari_i32(cpu_VF, cpu_VF, 31); /* V ? -1 : 0 */
1541 tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF); /* C & !V */
1543 /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1544 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1546 tcg_gen_movi_i32(cpu_NF, 0);
1547 tcg_gen_movi_i32(cpu_VF, 0);
1550 /* MSR (immediate) - move immediate to processor state field */
1551 static void handle_msr_i(DisasContext *s, uint32_t insn,
1552 unsigned int op1, unsigned int op2, unsigned int crm)
1554 TCGv_i32 t1;
1555 int op = op1 << 3 | op2;
1557 /* End the TB by default, chaining is ok. */
1558 s->base.is_jmp = DISAS_TOO_MANY;
1560 switch (op) {
1561 case 0x00: /* CFINV */
1562 if (crm != 0 || !dc_isar_feature(aa64_condm_4, s)) {
1563 goto do_unallocated;
1565 tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1566 s->base.is_jmp = DISAS_NEXT;
1567 break;
1569 case 0x01: /* XAFlag */
1570 if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1571 goto do_unallocated;
1573 gen_xaflag();
1574 s->base.is_jmp = DISAS_NEXT;
1575 break;
1577 case 0x02: /* AXFlag */
1578 if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1579 goto do_unallocated;
1581 gen_axflag();
1582 s->base.is_jmp = DISAS_NEXT;
1583 break;
1585 case 0x05: /* SPSel */
1586 if (s->current_el == 0) {
1587 goto do_unallocated;
1589 t1 = tcg_const_i32(crm & PSTATE_SP);
1590 gen_helper_msr_i_spsel(cpu_env, t1);
1591 tcg_temp_free_i32(t1);
1592 break;
1594 case 0x1e: /* DAIFSet */
1595 t1 = tcg_const_i32(crm);
1596 gen_helper_msr_i_daifset(cpu_env, t1);
1597 tcg_temp_free_i32(t1);
1598 break;
1600 case 0x1f: /* DAIFClear */
1601 t1 = tcg_const_i32(crm);
1602 gen_helper_msr_i_daifclear(cpu_env, t1);
1603 tcg_temp_free_i32(t1);
1604 /* For DAIFClear, exit the cpu loop to re-evaluate pending IRQs. */
1605 s->base.is_jmp = DISAS_UPDATE;
1606 break;
1608 default:
1609 do_unallocated:
1610 unallocated_encoding(s);
1611 return;
1615 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1617 TCGv_i32 tmp = tcg_temp_new_i32();
1618 TCGv_i32 nzcv = tcg_temp_new_i32();
1620 /* build bit 31, N */
1621 tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
1622 /* build bit 30, Z */
1623 tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1624 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1625 /* build bit 29, C */
1626 tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1627 /* build bit 28, V */
1628 tcg_gen_shri_i32(tmp, cpu_VF, 31);
1629 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1630 /* generate result */
1631 tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1633 tcg_temp_free_i32(nzcv);
1634 tcg_temp_free_i32(tmp);
1637 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1639 TCGv_i32 nzcv = tcg_temp_new_i32();
1641 /* take NZCV from R[t] */
1642 tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
1644 /* bit 31, N */
1645 tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
1646 /* bit 30, Z */
1647 tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1648 tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1649 /* bit 29, C */
1650 tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1651 tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1652 /* bit 28, V */
1653 tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1654 tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1655 tcg_temp_free_i32(nzcv);
1658 /* MRS - move from system register
1659 * MSR (register) - move to system register
1660 * SYS
1661 * SYSL
1662 * These are all essentially the same insn in 'read' and 'write'
1663 * versions, with varying op0 fields.
1665 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1666 unsigned int op0, unsigned int op1, unsigned int op2,
1667 unsigned int crn, unsigned int crm, unsigned int rt)
1669 const ARMCPRegInfo *ri;
1670 TCGv_i64 tcg_rt;
1672 ri = get_arm_cp_reginfo(s->cp_regs,
1673 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1674 crn, crm, op0, op1, op2));
1676 if (!ri) {
1677 /* Unknown register; this might be a guest error or a QEMU
1678 * unimplemented feature.
1680 qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1681 "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1682 isread ? "read" : "write", op0, op1, crn, crm, op2);
1683 unallocated_encoding(s);
1684 return;
1687 /* Check access permissions */
1688 if (!cp_access_ok(s->current_el, ri, isread)) {
1689 unallocated_encoding(s);
1690 return;
1693 if (ri->accessfn) {
1694 /* Emit code to perform further access permissions checks at
1695 * runtime; this may result in an exception.
1697 TCGv_ptr tmpptr;
1698 TCGv_i32 tcg_syn, tcg_isread;
1699 uint32_t syndrome;
1701 gen_a64_set_pc_im(s->pc_curr);
1702 tmpptr = tcg_const_ptr(ri);
1703 syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1704 tcg_syn = tcg_const_i32(syndrome);
1705 tcg_isread = tcg_const_i32(isread);
1706 gen_helper_access_check_cp_reg(cpu_env, tmpptr, tcg_syn, tcg_isread);
1707 tcg_temp_free_ptr(tmpptr);
1708 tcg_temp_free_i32(tcg_syn);
1709 tcg_temp_free_i32(tcg_isread);
1712 /* Handle special cases first */
1713 switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
1714 case ARM_CP_NOP:
1715 return;
1716 case ARM_CP_NZCV:
1717 tcg_rt = cpu_reg(s, rt);
1718 if (isread) {
1719 gen_get_nzcv(tcg_rt);
1720 } else {
1721 gen_set_nzcv(tcg_rt);
1723 return;
1724 case ARM_CP_CURRENTEL:
1725 /* Reads as current EL value from pstate, which is
1726 * guaranteed to be constant by the tb flags.
1728 tcg_rt = cpu_reg(s, rt);
1729 tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
1730 return;
1731 case ARM_CP_DC_ZVA:
1732 /* Writes clear the aligned block of memory which rt points into. */
1733 tcg_rt = cpu_reg(s, rt);
1734 gen_helper_dc_zva(cpu_env, tcg_rt);
1735 return;
1736 default:
1737 break;
1739 if ((ri->type & ARM_CP_FPU) && !fp_access_check(s)) {
1740 return;
1741 } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
1742 return;
1745 if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1746 gen_io_start();
1749 tcg_rt = cpu_reg(s, rt);
1751 if (isread) {
1752 if (ri->type & ARM_CP_CONST) {
1753 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1754 } else if (ri->readfn) {
1755 TCGv_ptr tmpptr;
1756 tmpptr = tcg_const_ptr(ri);
1757 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1758 tcg_temp_free_ptr(tmpptr);
1759 } else {
1760 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1762 } else {
1763 if (ri->type & ARM_CP_CONST) {
1764 /* If not forbidden by access permissions, treat as WI */
1765 return;
1766 } else if (ri->writefn) {
1767 TCGv_ptr tmpptr;
1768 tmpptr = tcg_const_ptr(ri);
1769 gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1770 tcg_temp_free_ptr(tmpptr);
1771 } else {
1772 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
1776 if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1777 /* I/O operations must end the TB here (whether read or write) */
1778 gen_io_end();
1779 s->base.is_jmp = DISAS_UPDATE;
1780 } else if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
1781 /* We default to ending the TB on a coprocessor register write,
1782 * but allow this to be suppressed by the register definition
1783 * (usually only necessary to work around guest bugs).
1785 s->base.is_jmp = DISAS_UPDATE;
1789 /* System
1790 * 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0
1791 * +---------------------+---+-----+-----+-------+-------+-----+------+
1792 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 | CRn | CRm | op2 | Rt |
1793 * +---------------------+---+-----+-----+-------+-------+-----+------+
1795 static void disas_system(DisasContext *s, uint32_t insn)
1797 unsigned int l, op0, op1, crn, crm, op2, rt;
1798 l = extract32(insn, 21, 1);
1799 op0 = extract32(insn, 19, 2);
1800 op1 = extract32(insn, 16, 3);
1801 crn = extract32(insn, 12, 4);
1802 crm = extract32(insn, 8, 4);
1803 op2 = extract32(insn, 5, 3);
1804 rt = extract32(insn, 0, 5);
1806 if (op0 == 0) {
1807 if (l || rt != 31) {
1808 unallocated_encoding(s);
1809 return;
1811 switch (crn) {
1812 case 2: /* HINT (including allocated hints like NOP, YIELD, etc) */
1813 handle_hint(s, insn, op1, op2, crm);
1814 break;
1815 case 3: /* CLREX, DSB, DMB, ISB */
1816 handle_sync(s, insn, op1, op2, crm);
1817 break;
1818 case 4: /* MSR (immediate) */
1819 handle_msr_i(s, insn, op1, op2, crm);
1820 break;
1821 default:
1822 unallocated_encoding(s);
1823 break;
1825 return;
1827 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
1830 /* Exception generation
1832 * 31 24 23 21 20 5 4 2 1 0
1833 * +-----------------+-----+------------------------+-----+----+
1834 * | 1 1 0 1 0 1 0 0 | opc | imm16 | op2 | LL |
1835 * +-----------------------+------------------------+----------+
1837 static void disas_exc(DisasContext *s, uint32_t insn)
1839 int opc = extract32(insn, 21, 3);
1840 int op2_ll = extract32(insn, 0, 5);
1841 int imm16 = extract32(insn, 5, 16);
1842 TCGv_i32 tmp;
1844 switch (opc) {
1845 case 0:
1846 /* For SVC, HVC and SMC we advance the single-step state
1847 * machine before taking the exception. This is architecturally
1848 * mandated, to ensure that single-stepping a system call
1849 * instruction works properly.
1851 switch (op2_ll) {
1852 case 1: /* SVC */
1853 gen_ss_advance(s);
1854 gen_exception_insn(s, s->base.pc_next, EXCP_SWI,
1855 syn_aa64_svc(imm16), default_exception_el(s));
1856 break;
1857 case 2: /* HVC */
1858 if (s->current_el == 0) {
1859 unallocated_encoding(s);
1860 break;
1862 /* The pre HVC helper handles cases when HVC gets trapped
1863 * as an undefined insn by runtime configuration.
1865 gen_a64_set_pc_im(s->pc_curr);
1866 gen_helper_pre_hvc(cpu_env);
1867 gen_ss_advance(s);
1868 gen_exception_insn(s, s->base.pc_next, EXCP_HVC,
1869 syn_aa64_hvc(imm16), 2);
1870 break;
1871 case 3: /* SMC */
1872 if (s->current_el == 0) {
1873 unallocated_encoding(s);
1874 break;
1876 gen_a64_set_pc_im(s->pc_curr);
1877 tmp = tcg_const_i32(syn_aa64_smc(imm16));
1878 gen_helper_pre_smc(cpu_env, tmp);
1879 tcg_temp_free_i32(tmp);
1880 gen_ss_advance(s);
1881 gen_exception_insn(s, s->base.pc_next, EXCP_SMC,
1882 syn_aa64_smc(imm16), 3);
1883 break;
1884 default:
1885 unallocated_encoding(s);
1886 break;
1888 break;
1889 case 1:
1890 if (op2_ll != 0) {
1891 unallocated_encoding(s);
1892 break;
1894 /* BRK */
1895 gen_exception_bkpt_insn(s, syn_aa64_bkpt(imm16));
1896 break;
1897 case 2:
1898 if (op2_ll != 0) {
1899 unallocated_encoding(s);
1900 break;
1902 /* HLT. This has two purposes.
1903 * Architecturally, it is an external halting debug instruction.
1904 * Since QEMU doesn't implement external debug, we treat this as
1905 * it is required for halting debug disabled: it will UNDEF.
1906 * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
1908 if (semihosting_enabled() && imm16 == 0xf000) {
1909 #ifndef CONFIG_USER_ONLY
1910 /* In system mode, don't allow userspace access to semihosting,
1911 * to provide some semblance of security (and for consistency
1912 * with our 32-bit semihosting).
1914 if (s->current_el == 0) {
1915 unsupported_encoding(s, insn);
1916 break;
1918 #endif
1919 gen_exception_internal_insn(s, s->base.pc_next, EXCP_SEMIHOST);
1920 } else {
1921 unsupported_encoding(s, insn);
1923 break;
1924 case 5:
1925 if (op2_ll < 1 || op2_ll > 3) {
1926 unallocated_encoding(s);
1927 break;
1929 /* DCPS1, DCPS2, DCPS3 */
1930 unsupported_encoding(s, insn);
1931 break;
1932 default:
1933 unallocated_encoding(s);
1934 break;
1938 /* Unconditional branch (register)
1939 * 31 25 24 21 20 16 15 10 9 5 4 0
1940 * +---------------+-------+-------+-------+------+-------+
1941 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
1942 * +---------------+-------+-------+-------+------+-------+
1944 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
1946 unsigned int opc, op2, op3, rn, op4;
1947 unsigned btype_mod = 2; /* 0: BR, 1: BLR, 2: other */
1948 TCGv_i64 dst;
1949 TCGv_i64 modifier;
1951 opc = extract32(insn, 21, 4);
1952 op2 = extract32(insn, 16, 5);
1953 op3 = extract32(insn, 10, 6);
1954 rn = extract32(insn, 5, 5);
1955 op4 = extract32(insn, 0, 5);
1957 if (op2 != 0x1f) {
1958 goto do_unallocated;
1961 switch (opc) {
1962 case 0: /* BR */
1963 case 1: /* BLR */
1964 case 2: /* RET */
1965 btype_mod = opc;
1966 switch (op3) {
1967 case 0:
1968 /* BR, BLR, RET */
1969 if (op4 != 0) {
1970 goto do_unallocated;
1972 dst = cpu_reg(s, rn);
1973 break;
1975 case 2:
1976 case 3:
1977 if (!dc_isar_feature(aa64_pauth, s)) {
1978 goto do_unallocated;
1980 if (opc == 2) {
1981 /* RETAA, RETAB */
1982 if (rn != 0x1f || op4 != 0x1f) {
1983 goto do_unallocated;
1985 rn = 30;
1986 modifier = cpu_X[31];
1987 } else {
1988 /* BRAAZ, BRABZ, BLRAAZ, BLRABZ */
1989 if (op4 != 0x1f) {
1990 goto do_unallocated;
1992 modifier = new_tmp_a64_zero(s);
1994 if (s->pauth_active) {
1995 dst = new_tmp_a64(s);
1996 if (op3 == 2) {
1997 gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
1998 } else {
1999 gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2001 } else {
2002 dst = cpu_reg(s, rn);
2004 break;
2006 default:
2007 goto do_unallocated;
2009 gen_a64_set_pc(s, dst);
2010 /* BLR also needs to load return address */
2011 if (opc == 1) {
2012 tcg_gen_movi_i64(cpu_reg(s, 30), s->base.pc_next);
2014 break;
2016 case 8: /* BRAA */
2017 case 9: /* BLRAA */
2018 if (!dc_isar_feature(aa64_pauth, s)) {
2019 goto do_unallocated;
2021 if ((op3 & ~1) != 2) {
2022 goto do_unallocated;
2024 btype_mod = opc & 1;
2025 if (s->pauth_active) {
2026 dst = new_tmp_a64(s);
2027 modifier = cpu_reg_sp(s, op4);
2028 if (op3 == 2) {
2029 gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
2030 } else {
2031 gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2033 } else {
2034 dst = cpu_reg(s, rn);
2036 gen_a64_set_pc(s, dst);
2037 /* BLRAA also needs to load return address */
2038 if (opc == 9) {
2039 tcg_gen_movi_i64(cpu_reg(s, 30), s->base.pc_next);
2041 break;
2043 case 4: /* ERET */
2044 if (s->current_el == 0) {
2045 goto do_unallocated;
2047 switch (op3) {
2048 case 0: /* ERET */
2049 if (op4 != 0) {
2050 goto do_unallocated;
2052 dst = tcg_temp_new_i64();
2053 tcg_gen_ld_i64(dst, cpu_env,
2054 offsetof(CPUARMState, elr_el[s->current_el]));
2055 break;
2057 case 2: /* ERETAA */
2058 case 3: /* ERETAB */
2059 if (!dc_isar_feature(aa64_pauth, s)) {
2060 goto do_unallocated;
2062 if (rn != 0x1f || op4 != 0x1f) {
2063 goto do_unallocated;
2065 dst = tcg_temp_new_i64();
2066 tcg_gen_ld_i64(dst, cpu_env,
2067 offsetof(CPUARMState, elr_el[s->current_el]));
2068 if (s->pauth_active) {
2069 modifier = cpu_X[31];
2070 if (op3 == 2) {
2071 gen_helper_autia(dst, cpu_env, dst, modifier);
2072 } else {
2073 gen_helper_autib(dst, cpu_env, dst, modifier);
2076 break;
2078 default:
2079 goto do_unallocated;
2081 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2082 gen_io_start();
2085 gen_helper_exception_return(cpu_env, dst);
2086 tcg_temp_free_i64(dst);
2087 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2088 gen_io_end();
2090 /* Must exit loop to check un-masked IRQs */
2091 s->base.is_jmp = DISAS_EXIT;
2092 return;
2094 case 5: /* DRPS */
2095 if (op3 != 0 || op4 != 0 || rn != 0x1f) {
2096 goto do_unallocated;
2097 } else {
2098 unsupported_encoding(s, insn);
2100 return;
2102 default:
2103 do_unallocated:
2104 unallocated_encoding(s);
2105 return;
2108 switch (btype_mod) {
2109 case 0: /* BR */
2110 if (dc_isar_feature(aa64_bti, s)) {
2111 /* BR to {x16,x17} or !guard -> 1, else 3. */
2112 set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
2114 break;
2116 case 1: /* BLR */
2117 if (dc_isar_feature(aa64_bti, s)) {
2118 /* BLR sets BTYPE to 2, regardless of source guarded page. */
2119 set_btype(s, 2);
2121 break;
2123 default: /* RET or none of the above. */
2124 /* BTYPE will be set to 0 by normal end-of-insn processing. */
2125 break;
2128 s->base.is_jmp = DISAS_JUMP;
2131 /* Branches, exception generating and system instructions */
2132 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
2134 switch (extract32(insn, 25, 7)) {
2135 case 0x0a: case 0x0b:
2136 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
2137 disas_uncond_b_imm(s, insn);
2138 break;
2139 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
2140 disas_comp_b_imm(s, insn);
2141 break;
2142 case 0x1b: case 0x5b: /* Test & branch (immediate) */
2143 disas_test_b_imm(s, insn);
2144 break;
2145 case 0x2a: /* Conditional branch (immediate) */
2146 disas_cond_b_imm(s, insn);
2147 break;
2148 case 0x6a: /* Exception generation / System */
2149 if (insn & (1 << 24)) {
2150 if (extract32(insn, 22, 2) == 0) {
2151 disas_system(s, insn);
2152 } else {
2153 unallocated_encoding(s);
2155 } else {
2156 disas_exc(s, insn);
2158 break;
2159 case 0x6b: /* Unconditional branch (register) */
2160 disas_uncond_b_reg(s, insn);
2161 break;
2162 default:
2163 unallocated_encoding(s);
2164 break;
2169 * Load/Store exclusive instructions are implemented by remembering
2170 * the value/address loaded, and seeing if these are the same
2171 * when the store is performed. This is not actually the architecturally
2172 * mandated semantics, but it works for typical guest code sequences
2173 * and avoids having to monitor regular stores.
2175 * The store exclusive uses the atomic cmpxchg primitives to avoid
2176 * races in multi-threaded linux-user and when MTTCG softmmu is
2177 * enabled.
2179 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
2180 TCGv_i64 addr, int size, bool is_pair)
2182 int idx = get_mem_index(s);
2183 TCGMemOp memop = s->be_data;
2185 g_assert(size <= 3);
2186 if (is_pair) {
2187 g_assert(size >= 2);
2188 if (size == 2) {
2189 /* The pair must be single-copy atomic for the doubleword. */
2190 memop |= MO_64 | MO_ALIGN;
2191 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2192 if (s->be_data == MO_LE) {
2193 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2194 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2195 } else {
2196 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2197 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2199 } else {
2200 /* The pair must be single-copy atomic for *each* doubleword, not
2201 the entire quadword, however it must be quadword aligned. */
2202 memop |= MO_64;
2203 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx,
2204 memop | MO_ALIGN_16);
2206 TCGv_i64 addr2 = tcg_temp_new_i64();
2207 tcg_gen_addi_i64(addr2, addr, 8);
2208 tcg_gen_qemu_ld_i64(cpu_exclusive_high, addr2, idx, memop);
2209 tcg_temp_free_i64(addr2);
2211 tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2212 tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2214 } else {
2215 memop |= size | MO_ALIGN;
2216 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2217 tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2219 tcg_gen_mov_i64(cpu_exclusive_addr, addr);
2222 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2223 TCGv_i64 addr, int size, int is_pair)
2225 /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2226 * && (!is_pair || env->exclusive_high == [addr + datasize])) {
2227 * [addr] = {Rt};
2228 * if (is_pair) {
2229 * [addr + datasize] = {Rt2};
2231 * {Rd} = 0;
2232 * } else {
2233 * {Rd} = 1;
2235 * env->exclusive_addr = -1;
2237 TCGLabel *fail_label = gen_new_label();
2238 TCGLabel *done_label = gen_new_label();
2239 TCGv_i64 tmp;
2241 tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
2243 tmp = tcg_temp_new_i64();
2244 if (is_pair) {
2245 if (size == 2) {
2246 if (s->be_data == MO_LE) {
2247 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2248 } else {
2249 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2251 tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2252 cpu_exclusive_val, tmp,
2253 get_mem_index(s),
2254 MO_64 | MO_ALIGN | s->be_data);
2255 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2256 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2257 if (!HAVE_CMPXCHG128) {
2258 gen_helper_exit_atomic(cpu_env);
2259 s->base.is_jmp = DISAS_NORETURN;
2260 } else if (s->be_data == MO_LE) {
2261 gen_helper_paired_cmpxchg64_le_parallel(tmp, cpu_env,
2262 cpu_exclusive_addr,
2263 cpu_reg(s, rt),
2264 cpu_reg(s, rt2));
2265 } else {
2266 gen_helper_paired_cmpxchg64_be_parallel(tmp, cpu_env,
2267 cpu_exclusive_addr,
2268 cpu_reg(s, rt),
2269 cpu_reg(s, rt2));
2271 } else if (s->be_data == MO_LE) {
2272 gen_helper_paired_cmpxchg64_le(tmp, cpu_env, cpu_exclusive_addr,
2273 cpu_reg(s, rt), cpu_reg(s, rt2));
2274 } else {
2275 gen_helper_paired_cmpxchg64_be(tmp, cpu_env, cpu_exclusive_addr,
2276 cpu_reg(s, rt), cpu_reg(s, rt2));
2278 } else {
2279 tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2280 cpu_reg(s, rt), get_mem_index(s),
2281 size | MO_ALIGN | s->be_data);
2282 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2284 tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2285 tcg_temp_free_i64(tmp);
2286 tcg_gen_br(done_label);
2288 gen_set_label(fail_label);
2289 tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2290 gen_set_label(done_label);
2291 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2294 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2295 int rn, int size)
2297 TCGv_i64 tcg_rs = cpu_reg(s, rs);
2298 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2299 int memidx = get_mem_index(s);
2300 TCGv_i64 clean_addr;
2302 if (rn == 31) {
2303 gen_check_sp_alignment(s);
2305 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2306 tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt, memidx,
2307 size | MO_ALIGN | s->be_data);
2310 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2311 int rn, int size)
2313 TCGv_i64 s1 = cpu_reg(s, rs);
2314 TCGv_i64 s2 = cpu_reg(s, rs + 1);
2315 TCGv_i64 t1 = cpu_reg(s, rt);
2316 TCGv_i64 t2 = cpu_reg(s, rt + 1);
2317 TCGv_i64 clean_addr;
2318 int memidx = get_mem_index(s);
2320 if (rn == 31) {
2321 gen_check_sp_alignment(s);
2323 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2325 if (size == 2) {
2326 TCGv_i64 cmp = tcg_temp_new_i64();
2327 TCGv_i64 val = tcg_temp_new_i64();
2329 if (s->be_data == MO_LE) {
2330 tcg_gen_concat32_i64(val, t1, t2);
2331 tcg_gen_concat32_i64(cmp, s1, s2);
2332 } else {
2333 tcg_gen_concat32_i64(val, t2, t1);
2334 tcg_gen_concat32_i64(cmp, s2, s1);
2337 tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx,
2338 MO_64 | MO_ALIGN | s->be_data);
2339 tcg_temp_free_i64(val);
2341 if (s->be_data == MO_LE) {
2342 tcg_gen_extr32_i64(s1, s2, cmp);
2343 } else {
2344 tcg_gen_extr32_i64(s2, s1, cmp);
2346 tcg_temp_free_i64(cmp);
2347 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2348 if (HAVE_CMPXCHG128) {
2349 TCGv_i32 tcg_rs = tcg_const_i32(rs);
2350 if (s->be_data == MO_LE) {
2351 gen_helper_casp_le_parallel(cpu_env, tcg_rs,
2352 clean_addr, t1, t2);
2353 } else {
2354 gen_helper_casp_be_parallel(cpu_env, tcg_rs,
2355 clean_addr, t1, t2);
2357 tcg_temp_free_i32(tcg_rs);
2358 } else {
2359 gen_helper_exit_atomic(cpu_env);
2360 s->base.is_jmp = DISAS_NORETURN;
2362 } else {
2363 TCGv_i64 d1 = tcg_temp_new_i64();
2364 TCGv_i64 d2 = tcg_temp_new_i64();
2365 TCGv_i64 a2 = tcg_temp_new_i64();
2366 TCGv_i64 c1 = tcg_temp_new_i64();
2367 TCGv_i64 c2 = tcg_temp_new_i64();
2368 TCGv_i64 zero = tcg_const_i64(0);
2370 /* Load the two words, in memory order. */
2371 tcg_gen_qemu_ld_i64(d1, clean_addr, memidx,
2372 MO_64 | MO_ALIGN_16 | s->be_data);
2373 tcg_gen_addi_i64(a2, clean_addr, 8);
2374 tcg_gen_qemu_ld_i64(d2, a2, memidx, MO_64 | s->be_data);
2376 /* Compare the two words, also in memory order. */
2377 tcg_gen_setcond_i64(TCG_COND_EQ, c1, d1, s1);
2378 tcg_gen_setcond_i64(TCG_COND_EQ, c2, d2, s2);
2379 tcg_gen_and_i64(c2, c2, c1);
2381 /* If compare equal, write back new data, else write back old data. */
2382 tcg_gen_movcond_i64(TCG_COND_NE, c1, c2, zero, t1, d1);
2383 tcg_gen_movcond_i64(TCG_COND_NE, c2, c2, zero, t2, d2);
2384 tcg_gen_qemu_st_i64(c1, clean_addr, memidx, MO_64 | s->be_data);
2385 tcg_gen_qemu_st_i64(c2, a2, memidx, MO_64 | s->be_data);
2386 tcg_temp_free_i64(a2);
2387 tcg_temp_free_i64(c1);
2388 tcg_temp_free_i64(c2);
2389 tcg_temp_free_i64(zero);
2391 /* Write back the data from memory to Rs. */
2392 tcg_gen_mov_i64(s1, d1);
2393 tcg_gen_mov_i64(s2, d2);
2394 tcg_temp_free_i64(d1);
2395 tcg_temp_free_i64(d2);
2399 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
2400 * from the ARMv8 specs for LDR (Shared decode for all encodings).
2402 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2404 int opc0 = extract32(opc, 0, 1);
2405 int regsize;
2407 if (is_signed) {
2408 regsize = opc0 ? 32 : 64;
2409 } else {
2410 regsize = size == 3 ? 64 : 32;
2412 return regsize == 64;
2415 /* Load/store exclusive
2417 * 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0
2418 * +-----+-------------+----+---+----+------+----+-------+------+------+
2419 * | sz | 0 0 1 0 0 0 | o2 | L | o1 | Rs | o0 | Rt2 | Rn | Rt |
2420 * +-----+-------------+----+---+----+------+----+-------+------+------+
2422 * sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2423 * L: 0 -> store, 1 -> load
2424 * o2: 0 -> exclusive, 1 -> not
2425 * o1: 0 -> single register, 1 -> register pair
2426 * o0: 1 -> load-acquire/store-release, 0 -> not
2428 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2430 int rt = extract32(insn, 0, 5);
2431 int rn = extract32(insn, 5, 5);
2432 int rt2 = extract32(insn, 10, 5);
2433 int rs = extract32(insn, 16, 5);
2434 int is_lasr = extract32(insn, 15, 1);
2435 int o2_L_o1_o0 = extract32(insn, 21, 3) * 2 | is_lasr;
2436 int size = extract32(insn, 30, 2);
2437 TCGv_i64 clean_addr;
2439 switch (o2_L_o1_o0) {
2440 case 0x0: /* STXR */
2441 case 0x1: /* STLXR */
2442 if (rn == 31) {
2443 gen_check_sp_alignment(s);
2445 if (is_lasr) {
2446 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2448 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2449 gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, false);
2450 return;
2452 case 0x4: /* LDXR */
2453 case 0x5: /* LDAXR */
2454 if (rn == 31) {
2455 gen_check_sp_alignment(s);
2457 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2458 s->is_ldex = true;
2459 gen_load_exclusive(s, rt, rt2, clean_addr, size, false);
2460 if (is_lasr) {
2461 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2463 return;
2465 case 0x8: /* STLLR */
2466 if (!dc_isar_feature(aa64_lor, s)) {
2467 break;
2469 /* StoreLORelease is the same as Store-Release for QEMU. */
2470 /* fall through */
2471 case 0x9: /* STLR */
2472 /* Generate ISS for non-exclusive accesses including LASR. */
2473 if (rn == 31) {
2474 gen_check_sp_alignment(s);
2476 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2477 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2478 do_gpr_st(s, cpu_reg(s, rt), clean_addr, size, true, rt,
2479 disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2480 return;
2482 case 0xc: /* LDLAR */
2483 if (!dc_isar_feature(aa64_lor, s)) {
2484 break;
2486 /* LoadLOAcquire is the same as Load-Acquire for QEMU. */
2487 /* fall through */
2488 case 0xd: /* LDAR */
2489 /* Generate ISS for non-exclusive accesses including LASR. */
2490 if (rn == 31) {
2491 gen_check_sp_alignment(s);
2493 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2494 do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size, false, false, true, rt,
2495 disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2496 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2497 return;
2499 case 0x2: case 0x3: /* CASP / STXP */
2500 if (size & 2) { /* STXP / STLXP */
2501 if (rn == 31) {
2502 gen_check_sp_alignment(s);
2504 if (is_lasr) {
2505 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2507 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2508 gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, true);
2509 return;
2511 if (rt2 == 31
2512 && ((rt | rs) & 1) == 0
2513 && dc_isar_feature(aa64_atomics, s)) {
2514 /* CASP / CASPL */
2515 gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2516 return;
2518 break;
2520 case 0x6: case 0x7: /* CASPA / LDXP */
2521 if (size & 2) { /* LDXP / LDAXP */
2522 if (rn == 31) {
2523 gen_check_sp_alignment(s);
2525 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2526 s->is_ldex = true;
2527 gen_load_exclusive(s, rt, rt2, clean_addr, size, true);
2528 if (is_lasr) {
2529 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2531 return;
2533 if (rt2 == 31
2534 && ((rt | rs) & 1) == 0
2535 && dc_isar_feature(aa64_atomics, s)) {
2536 /* CASPA / CASPAL */
2537 gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2538 return;
2540 break;
2542 case 0xa: /* CAS */
2543 case 0xb: /* CASL */
2544 case 0xe: /* CASA */
2545 case 0xf: /* CASAL */
2546 if (rt2 == 31 && dc_isar_feature(aa64_atomics, s)) {
2547 gen_compare_and_swap(s, rs, rt, rn, size);
2548 return;
2550 break;
2552 unallocated_encoding(s);
2556 * Load register (literal)
2558 * 31 30 29 27 26 25 24 23 5 4 0
2559 * +-----+-------+---+-----+-------------------+-------+
2560 * | opc | 0 1 1 | V | 0 0 | imm19 | Rt |
2561 * +-----+-------+---+-----+-------------------+-------+
2563 * V: 1 -> vector (simd/fp)
2564 * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2565 * 10-> 32 bit signed, 11 -> prefetch
2566 * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2568 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2570 int rt = extract32(insn, 0, 5);
2571 int64_t imm = sextract32(insn, 5, 19) << 2;
2572 bool is_vector = extract32(insn, 26, 1);
2573 int opc = extract32(insn, 30, 2);
2574 bool is_signed = false;
2575 int size = 2;
2576 TCGv_i64 tcg_rt, clean_addr;
2578 if (is_vector) {
2579 if (opc == 3) {
2580 unallocated_encoding(s);
2581 return;
2583 size = 2 + opc;
2584 if (!fp_access_check(s)) {
2585 return;
2587 } else {
2588 if (opc == 3) {
2589 /* PRFM (literal) : prefetch */
2590 return;
2592 size = 2 + extract32(opc, 0, 1);
2593 is_signed = extract32(opc, 1, 1);
2596 tcg_rt = cpu_reg(s, rt);
2598 clean_addr = tcg_const_i64(s->pc_curr + imm);
2599 if (is_vector) {
2600 do_fp_ld(s, rt, clean_addr, size);
2601 } else {
2602 /* Only unsigned 32bit loads target 32bit registers. */
2603 bool iss_sf = opc != 0;
2605 do_gpr_ld(s, tcg_rt, clean_addr, size, is_signed, false,
2606 true, rt, iss_sf, false);
2608 tcg_temp_free_i64(clean_addr);
2612 * LDNP (Load Pair - non-temporal hint)
2613 * LDP (Load Pair - non vector)
2614 * LDPSW (Load Pair Signed Word - non vector)
2615 * STNP (Store Pair - non-temporal hint)
2616 * STP (Store Pair - non vector)
2617 * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2618 * LDP (Load Pair of SIMD&FP)
2619 * STNP (Store Pair of SIMD&FP - non-temporal hint)
2620 * STP (Store Pair of SIMD&FP)
2622 * 31 30 29 27 26 25 24 23 22 21 15 14 10 9 5 4 0
2623 * +-----+-------+---+---+-------+---+-----------------------------+
2624 * | opc | 1 0 1 | V | 0 | index | L | imm7 | Rt2 | Rn | Rt |
2625 * +-----+-------+---+---+-------+---+-------+-------+------+------+
2627 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
2628 * LDPSW 01
2629 * LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2630 * V: 0 -> GPR, 1 -> Vector
2631 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2632 * 10 -> signed offset, 11 -> pre-index
2633 * L: 0 -> Store 1 -> Load
2635 * Rt, Rt2 = GPR or SIMD registers to be stored
2636 * Rn = general purpose register containing address
2637 * imm7 = signed offset (multiple of 4 or 8 depending on size)
2639 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2641 int rt = extract32(insn, 0, 5);
2642 int rn = extract32(insn, 5, 5);
2643 int rt2 = extract32(insn, 10, 5);
2644 uint64_t offset = sextract64(insn, 15, 7);
2645 int index = extract32(insn, 23, 2);
2646 bool is_vector = extract32(insn, 26, 1);
2647 bool is_load = extract32(insn, 22, 1);
2648 int opc = extract32(insn, 30, 2);
2650 bool is_signed = false;
2651 bool postindex = false;
2652 bool wback = false;
2654 TCGv_i64 clean_addr, dirty_addr;
2656 int size;
2658 if (opc == 3) {
2659 unallocated_encoding(s);
2660 return;
2663 if (is_vector) {
2664 size = 2 + opc;
2665 } else {
2666 size = 2 + extract32(opc, 1, 1);
2667 is_signed = extract32(opc, 0, 1);
2668 if (!is_load && is_signed) {
2669 unallocated_encoding(s);
2670 return;
2674 switch (index) {
2675 case 1: /* post-index */
2676 postindex = true;
2677 wback = true;
2678 break;
2679 case 0:
2680 /* signed offset with "non-temporal" hint. Since we don't emulate
2681 * caches we don't care about hints to the cache system about
2682 * data access patterns, and handle this identically to plain
2683 * signed offset.
2685 if (is_signed) {
2686 /* There is no non-temporal-hint version of LDPSW */
2687 unallocated_encoding(s);
2688 return;
2690 postindex = false;
2691 break;
2692 case 2: /* signed offset, rn not updated */
2693 postindex = false;
2694 break;
2695 case 3: /* pre-index */
2696 postindex = false;
2697 wback = true;
2698 break;
2701 if (is_vector && !fp_access_check(s)) {
2702 return;
2705 offset <<= size;
2707 if (rn == 31) {
2708 gen_check_sp_alignment(s);
2711 dirty_addr = read_cpu_reg_sp(s, rn, 1);
2712 if (!postindex) {
2713 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2715 clean_addr = clean_data_tbi(s, dirty_addr);
2717 if (is_vector) {
2718 if (is_load) {
2719 do_fp_ld(s, rt, clean_addr, size);
2720 } else {
2721 do_fp_st(s, rt, clean_addr, size);
2723 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2724 if (is_load) {
2725 do_fp_ld(s, rt2, clean_addr, size);
2726 } else {
2727 do_fp_st(s, rt2, clean_addr, size);
2729 } else {
2730 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2731 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
2733 if (is_load) {
2734 TCGv_i64 tmp = tcg_temp_new_i64();
2736 /* Do not modify tcg_rt before recognizing any exception
2737 * from the second load.
2739 do_gpr_ld(s, tmp, clean_addr, size, is_signed, false,
2740 false, 0, false, false);
2741 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2742 do_gpr_ld(s, tcg_rt2, clean_addr, size, is_signed, false,
2743 false, 0, false, false);
2745 tcg_gen_mov_i64(tcg_rt, tmp);
2746 tcg_temp_free_i64(tmp);
2747 } else {
2748 do_gpr_st(s, tcg_rt, clean_addr, size,
2749 false, 0, false, false);
2750 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2751 do_gpr_st(s, tcg_rt2, clean_addr, size,
2752 false, 0, false, false);
2756 if (wback) {
2757 if (postindex) {
2758 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2760 tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
2765 * Load/store (immediate post-indexed)
2766 * Load/store (immediate pre-indexed)
2767 * Load/store (unscaled immediate)
2769 * 31 30 29 27 26 25 24 23 22 21 20 12 11 10 9 5 4 0
2770 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2771 * |size| 1 1 1 | V | 0 0 | opc | 0 | imm9 | idx | Rn | Rt |
2772 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2774 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
2775 10 -> unprivileged
2776 * V = 0 -> non-vector
2777 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
2778 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2780 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
2781 int opc,
2782 int size,
2783 int rt,
2784 bool is_vector)
2786 int rn = extract32(insn, 5, 5);
2787 int imm9 = sextract32(insn, 12, 9);
2788 int idx = extract32(insn, 10, 2);
2789 bool is_signed = false;
2790 bool is_store = false;
2791 bool is_extended = false;
2792 bool is_unpriv = (idx == 2);
2793 bool iss_valid = !is_vector;
2794 bool post_index;
2795 bool writeback;
2797 TCGv_i64 clean_addr, dirty_addr;
2799 if (is_vector) {
2800 size |= (opc & 2) << 1;
2801 if (size > 4 || is_unpriv) {
2802 unallocated_encoding(s);
2803 return;
2805 is_store = ((opc & 1) == 0);
2806 if (!fp_access_check(s)) {
2807 return;
2809 } else {
2810 if (size == 3 && opc == 2) {
2811 /* PRFM - prefetch */
2812 if (idx != 0) {
2813 unallocated_encoding(s);
2814 return;
2816 return;
2818 if (opc == 3 && size > 1) {
2819 unallocated_encoding(s);
2820 return;
2822 is_store = (opc == 0);
2823 is_signed = extract32(opc, 1, 1);
2824 is_extended = (size < 3) && extract32(opc, 0, 1);
2827 switch (idx) {
2828 case 0:
2829 case 2:
2830 post_index = false;
2831 writeback = false;
2832 break;
2833 case 1:
2834 post_index = true;
2835 writeback = true;
2836 break;
2837 case 3:
2838 post_index = false;
2839 writeback = true;
2840 break;
2841 default:
2842 g_assert_not_reached();
2845 if (rn == 31) {
2846 gen_check_sp_alignment(s);
2849 dirty_addr = read_cpu_reg_sp(s, rn, 1);
2850 if (!post_index) {
2851 tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
2853 clean_addr = clean_data_tbi(s, dirty_addr);
2855 if (is_vector) {
2856 if (is_store) {
2857 do_fp_st(s, rt, clean_addr, size);
2858 } else {
2859 do_fp_ld(s, rt, clean_addr, size);
2861 } else {
2862 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2863 int memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
2864 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2866 if (is_store) {
2867 do_gpr_st_memidx(s, tcg_rt, clean_addr, size, memidx,
2868 iss_valid, rt, iss_sf, false);
2869 } else {
2870 do_gpr_ld_memidx(s, tcg_rt, clean_addr, size,
2871 is_signed, is_extended, memidx,
2872 iss_valid, rt, iss_sf, false);
2876 if (writeback) {
2877 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2878 if (post_index) {
2879 tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
2881 tcg_gen_mov_i64(tcg_rn, dirty_addr);
2886 * Load/store (register offset)
2888 * 31 30 29 27 26 25 24 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2889 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2890 * |size| 1 1 1 | V | 0 0 | opc | 1 | Rm | opt | S| 1 0 | Rn | Rt |
2891 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2893 * For non-vector:
2894 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2895 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2896 * For vector:
2897 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2898 * opc<0>: 0 -> store, 1 -> load
2899 * V: 1 -> vector/simd
2900 * opt: extend encoding (see DecodeRegExtend)
2901 * S: if S=1 then scale (essentially index by sizeof(size))
2902 * Rt: register to transfer into/out of
2903 * Rn: address register or SP for base
2904 * Rm: offset register or ZR for offset
2906 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
2907 int opc,
2908 int size,
2909 int rt,
2910 bool is_vector)
2912 int rn = extract32(insn, 5, 5);
2913 int shift = extract32(insn, 12, 1);
2914 int rm = extract32(insn, 16, 5);
2915 int opt = extract32(insn, 13, 3);
2916 bool is_signed = false;
2917 bool is_store = false;
2918 bool is_extended = false;
2920 TCGv_i64 tcg_rm, clean_addr, dirty_addr;
2922 if (extract32(opt, 1, 1) == 0) {
2923 unallocated_encoding(s);
2924 return;
2927 if (is_vector) {
2928 size |= (opc & 2) << 1;
2929 if (size > 4) {
2930 unallocated_encoding(s);
2931 return;
2933 is_store = !extract32(opc, 0, 1);
2934 if (!fp_access_check(s)) {
2935 return;
2937 } else {
2938 if (size == 3 && opc == 2) {
2939 /* PRFM - prefetch */
2940 return;
2942 if (opc == 3 && size > 1) {
2943 unallocated_encoding(s);
2944 return;
2946 is_store = (opc == 0);
2947 is_signed = extract32(opc, 1, 1);
2948 is_extended = (size < 3) && extract32(opc, 0, 1);
2951 if (rn == 31) {
2952 gen_check_sp_alignment(s);
2954 dirty_addr = read_cpu_reg_sp(s, rn, 1);
2956 tcg_rm = read_cpu_reg(s, rm, 1);
2957 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
2959 tcg_gen_add_i64(dirty_addr, dirty_addr, tcg_rm);
2960 clean_addr = clean_data_tbi(s, dirty_addr);
2962 if (is_vector) {
2963 if (is_store) {
2964 do_fp_st(s, rt, clean_addr, size);
2965 } else {
2966 do_fp_ld(s, rt, clean_addr, size);
2968 } else {
2969 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2970 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2971 if (is_store) {
2972 do_gpr_st(s, tcg_rt, clean_addr, size,
2973 true, rt, iss_sf, false);
2974 } else {
2975 do_gpr_ld(s, tcg_rt, clean_addr, size,
2976 is_signed, is_extended,
2977 true, rt, iss_sf, false);
2983 * Load/store (unsigned immediate)
2985 * 31 30 29 27 26 25 24 23 22 21 10 9 5
2986 * +----+-------+---+-----+-----+------------+-------+------+
2987 * |size| 1 1 1 | V | 0 1 | opc | imm12 | Rn | Rt |
2988 * +----+-------+---+-----+-----+------------+-------+------+
2990 * For non-vector:
2991 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2992 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2993 * For vector:
2994 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2995 * opc<0>: 0 -> store, 1 -> load
2996 * Rn: base address register (inc SP)
2997 * Rt: target register
2999 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
3000 int opc,
3001 int size,
3002 int rt,
3003 bool is_vector)
3005 int rn = extract32(insn, 5, 5);
3006 unsigned int imm12 = extract32(insn, 10, 12);
3007 unsigned int offset;
3009 TCGv_i64 clean_addr, dirty_addr;
3011 bool is_store;
3012 bool is_signed = false;
3013 bool is_extended = false;
3015 if (is_vector) {
3016 size |= (opc & 2) << 1;
3017 if (size > 4) {
3018 unallocated_encoding(s);
3019 return;
3021 is_store = !extract32(opc, 0, 1);
3022 if (!fp_access_check(s)) {
3023 return;
3025 } else {
3026 if (size == 3 && opc == 2) {
3027 /* PRFM - prefetch */
3028 return;
3030 if (opc == 3 && size > 1) {
3031 unallocated_encoding(s);
3032 return;
3034 is_store = (opc == 0);
3035 is_signed = extract32(opc, 1, 1);
3036 is_extended = (size < 3) && extract32(opc, 0, 1);
3039 if (rn == 31) {
3040 gen_check_sp_alignment(s);
3042 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3043 offset = imm12 << size;
3044 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3045 clean_addr = clean_data_tbi(s, dirty_addr);
3047 if (is_vector) {
3048 if (is_store) {
3049 do_fp_st(s, rt, clean_addr, size);
3050 } else {
3051 do_fp_ld(s, rt, clean_addr, size);
3053 } else {
3054 TCGv_i64 tcg_rt = cpu_reg(s, rt);
3055 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3056 if (is_store) {
3057 do_gpr_st(s, tcg_rt, clean_addr, size,
3058 true, rt, iss_sf, false);
3059 } else {
3060 do_gpr_ld(s, tcg_rt, clean_addr, size, is_signed, is_extended,
3061 true, rt, iss_sf, false);
3066 /* Atomic memory operations
3068 * 31 30 27 26 24 22 21 16 15 12 10 5 0
3069 * +------+-------+---+-----+-----+---+----+----+-----+-----+----+-----+
3070 * | size | 1 1 1 | V | 0 0 | A R | 1 | Rs | o3 | opc | 0 0 | Rn | Rt |
3071 * +------+-------+---+-----+-----+--------+----+-----+-----+----+-----+
3073 * Rt: the result register
3074 * Rn: base address or SP
3075 * Rs: the source register for the operation
3076 * V: vector flag (always 0 as of v8.3)
3077 * A: acquire flag
3078 * R: release flag
3080 static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
3081 int size, int rt, bool is_vector)
3083 int rs = extract32(insn, 16, 5);
3084 int rn = extract32(insn, 5, 5);
3085 int o3_opc = extract32(insn, 12, 4);
3086 TCGv_i64 tcg_rs, clean_addr;
3087 AtomicThreeOpFn *fn;
3089 if (is_vector || !dc_isar_feature(aa64_atomics, s)) {
3090 unallocated_encoding(s);
3091 return;
3093 switch (o3_opc) {
3094 case 000: /* LDADD */
3095 fn = tcg_gen_atomic_fetch_add_i64;
3096 break;
3097 case 001: /* LDCLR */
3098 fn = tcg_gen_atomic_fetch_and_i64;
3099 break;
3100 case 002: /* LDEOR */
3101 fn = tcg_gen_atomic_fetch_xor_i64;
3102 break;
3103 case 003: /* LDSET */
3104 fn = tcg_gen_atomic_fetch_or_i64;
3105 break;
3106 case 004: /* LDSMAX */
3107 fn = tcg_gen_atomic_fetch_smax_i64;
3108 break;
3109 case 005: /* LDSMIN */
3110 fn = tcg_gen_atomic_fetch_smin_i64;
3111 break;
3112 case 006: /* LDUMAX */
3113 fn = tcg_gen_atomic_fetch_umax_i64;
3114 break;
3115 case 007: /* LDUMIN */
3116 fn = tcg_gen_atomic_fetch_umin_i64;
3117 break;
3118 case 010: /* SWP */
3119 fn = tcg_gen_atomic_xchg_i64;
3120 break;
3121 default:
3122 unallocated_encoding(s);
3123 return;
3126 if (rn == 31) {
3127 gen_check_sp_alignment(s);
3129 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
3130 tcg_rs = read_cpu_reg(s, rs, true);
3132 if (o3_opc == 1) { /* LDCLR */
3133 tcg_gen_not_i64(tcg_rs, tcg_rs);
3136 /* The tcg atomic primitives are all full barriers. Therefore we
3137 * can ignore the Acquire and Release bits of this instruction.
3139 fn(cpu_reg(s, rt), clean_addr, tcg_rs, get_mem_index(s),
3140 s->be_data | size | MO_ALIGN);
3144 * PAC memory operations
3146 * 31 30 27 26 24 22 21 12 11 10 5 0
3147 * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3148 * | size | 1 1 1 | V | 0 0 | M S | 1 | imm9 | W | 1 | Rn | Rt |
3149 * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3151 * Rt: the result register
3152 * Rn: base address or SP
3153 * V: vector flag (always 0 as of v8.3)
3154 * M: clear for key DA, set for key DB
3155 * W: pre-indexing flag
3156 * S: sign for imm9.
3158 static void disas_ldst_pac(DisasContext *s, uint32_t insn,
3159 int size, int rt, bool is_vector)
3161 int rn = extract32(insn, 5, 5);
3162 bool is_wback = extract32(insn, 11, 1);
3163 bool use_key_a = !extract32(insn, 23, 1);
3164 int offset;
3165 TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3167 if (size != 3 || is_vector || !dc_isar_feature(aa64_pauth, s)) {
3168 unallocated_encoding(s);
3169 return;
3172 if (rn == 31) {
3173 gen_check_sp_alignment(s);
3175 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3177 if (s->pauth_active) {
3178 if (use_key_a) {
3179 gen_helper_autda(dirty_addr, cpu_env, dirty_addr, cpu_X[31]);
3180 } else {
3181 gen_helper_autdb(dirty_addr, cpu_env, dirty_addr, cpu_X[31]);
3185 /* Form the 10-bit signed, scaled offset. */
3186 offset = (extract32(insn, 22, 1) << 9) | extract32(insn, 12, 9);
3187 offset = sextract32(offset << size, 0, 10 + size);
3188 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3190 /* Note that "clean" and "dirty" here refer to TBI not PAC. */
3191 clean_addr = clean_data_tbi(s, dirty_addr);
3193 tcg_rt = cpu_reg(s, rt);
3194 do_gpr_ld(s, tcg_rt, clean_addr, size, /* is_signed */ false,
3195 /* extend */ false, /* iss_valid */ !is_wback,
3196 /* iss_srt */ rt, /* iss_sf */ true, /* iss_ar */ false);
3198 if (is_wback) {
3199 tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3203 /* Load/store register (all forms) */
3204 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
3206 int rt = extract32(insn, 0, 5);
3207 int opc = extract32(insn, 22, 2);
3208 bool is_vector = extract32(insn, 26, 1);
3209 int size = extract32(insn, 30, 2);
3211 switch (extract32(insn, 24, 2)) {
3212 case 0:
3213 if (extract32(insn, 21, 1) == 0) {
3214 /* Load/store register (unscaled immediate)
3215 * Load/store immediate pre/post-indexed
3216 * Load/store register unprivileged
3218 disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
3219 return;
3221 switch (extract32(insn, 10, 2)) {
3222 case 0:
3223 disas_ldst_atomic(s, insn, size, rt, is_vector);
3224 return;
3225 case 2:
3226 disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
3227 return;
3228 default:
3229 disas_ldst_pac(s, insn, size, rt, is_vector);
3230 return;
3232 break;
3233 case 1:
3234 disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
3235 return;
3237 unallocated_encoding(s);
3240 /* AdvSIMD load/store multiple structures
3242 * 31 30 29 23 22 21 16 15 12 11 10 9 5 4 0
3243 * +---+---+---------------+---+-------------+--------+------+------+------+
3244 * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size | Rn | Rt |
3245 * +---+---+---------------+---+-------------+--------+------+------+------+
3247 * AdvSIMD load/store multiple structures (post-indexed)
3249 * 31 30 29 23 22 21 20 16 15 12 11 10 9 5 4 0
3250 * +---+---+---------------+---+---+---------+--------+------+------+------+
3251 * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 | Rm | opcode | size | Rn | Rt |
3252 * +---+---+---------------+---+---+---------+--------+------+------+------+
3254 * Rt: first (or only) SIMD&FP register to be transferred
3255 * Rn: base address or SP
3256 * Rm (post-index only): post-index register (when !31) or size dependent #imm
3258 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
3260 int rt = extract32(insn, 0, 5);
3261 int rn = extract32(insn, 5, 5);
3262 int rm = extract32(insn, 16, 5);
3263 int size = extract32(insn, 10, 2);
3264 int opcode = extract32(insn, 12, 4);
3265 bool is_store = !extract32(insn, 22, 1);
3266 bool is_postidx = extract32(insn, 23, 1);
3267 bool is_q = extract32(insn, 30, 1);
3268 TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3269 TCGMemOp endian = s->be_data;
3271 int ebytes; /* bytes per element */
3272 int elements; /* elements per vector */
3273 int rpt; /* num iterations */
3274 int selem; /* structure elements */
3275 int r;
3277 if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
3278 unallocated_encoding(s);
3279 return;
3282 if (!is_postidx && rm != 0) {
3283 unallocated_encoding(s);
3284 return;
3287 /* From the shared decode logic */
3288 switch (opcode) {
3289 case 0x0:
3290 rpt = 1;
3291 selem = 4;
3292 break;
3293 case 0x2:
3294 rpt = 4;
3295 selem = 1;
3296 break;
3297 case 0x4:
3298 rpt = 1;
3299 selem = 3;
3300 break;
3301 case 0x6:
3302 rpt = 3;
3303 selem = 1;
3304 break;
3305 case 0x7:
3306 rpt = 1;
3307 selem = 1;
3308 break;
3309 case 0x8:
3310 rpt = 1;
3311 selem = 2;
3312 break;
3313 case 0xa:
3314 rpt = 2;
3315 selem = 1;
3316 break;
3317 default:
3318 unallocated_encoding(s);
3319 return;
3322 if (size == 3 && !is_q && selem != 1) {
3323 /* reserved */
3324 unallocated_encoding(s);
3325 return;
3328 if (!fp_access_check(s)) {
3329 return;
3332 if (rn == 31) {
3333 gen_check_sp_alignment(s);
3336 /* For our purposes, bytes are always little-endian. */
3337 if (size == 0) {
3338 endian = MO_LE;
3341 /* Consecutive little-endian elements from a single register
3342 * can be promoted to a larger little-endian operation.
3344 if (selem == 1 && endian == MO_LE) {
3345 size = 3;
3347 ebytes = 1 << size;
3348 elements = (is_q ? 16 : 8) / ebytes;
3350 tcg_rn = cpu_reg_sp(s, rn);
3351 clean_addr = clean_data_tbi(s, tcg_rn);
3352 tcg_ebytes = tcg_const_i64(ebytes);
3354 for (r = 0; r < rpt; r++) {
3355 int e;
3356 for (e = 0; e < elements; e++) {
3357 int xs;
3358 for (xs = 0; xs < selem; xs++) {
3359 int tt = (rt + r + xs) % 32;
3360 if (is_store) {
3361 do_vec_st(s, tt, e, clean_addr, size, endian);
3362 } else {
3363 do_vec_ld(s, tt, e, clean_addr, size, endian);
3365 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3369 tcg_temp_free_i64(tcg_ebytes);
3371 if (!is_store) {
3372 /* For non-quad operations, setting a slice of the low
3373 * 64 bits of the register clears the high 64 bits (in
3374 * the ARM ARM pseudocode this is implicit in the fact
3375 * that 'rval' is a 64 bit wide variable).
3376 * For quad operations, we might still need to zero the
3377 * high bits of SVE.
3379 for (r = 0; r < rpt * selem; r++) {
3380 int tt = (rt + r) % 32;
3381 clear_vec_high(s, is_q, tt);
3385 if (is_postidx) {
3386 if (rm == 31) {
3387 tcg_gen_addi_i64(tcg_rn, tcg_rn, rpt * elements * selem * ebytes);
3388 } else {
3389 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3394 /* AdvSIMD load/store single structure
3396 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3397 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3398 * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size | Rn | Rt |
3399 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3401 * AdvSIMD load/store single structure (post-indexed)
3403 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3404 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3405 * | 0 | Q | 0 0 1 1 0 1 1 | L R | Rm | opc | S | size | Rn | Rt |
3406 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3408 * Rt: first (or only) SIMD&FP register to be transferred
3409 * Rn: base address or SP
3410 * Rm (post-index only): post-index register (when !31) or size dependent #imm
3411 * index = encoded in Q:S:size dependent on size
3413 * lane_size = encoded in R, opc
3414 * transfer width = encoded in opc, S, size
3416 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
3418 int rt = extract32(insn, 0, 5);
3419 int rn = extract32(insn, 5, 5);
3420 int rm = extract32(insn, 16, 5);
3421 int size = extract32(insn, 10, 2);
3422 int S = extract32(insn, 12, 1);
3423 int opc = extract32(insn, 13, 3);
3424 int R = extract32(insn, 21, 1);
3425 int is_load = extract32(insn, 22, 1);
3426 int is_postidx = extract32(insn, 23, 1);
3427 int is_q = extract32(insn, 30, 1);
3429 int scale = extract32(opc, 1, 2);
3430 int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
3431 bool replicate = false;
3432 int index = is_q << 3 | S << 2 | size;
3433 int ebytes, xs;
3434 TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3436 if (extract32(insn, 31, 1)) {
3437 unallocated_encoding(s);
3438 return;
3440 if (!is_postidx && rm != 0) {
3441 unallocated_encoding(s);
3442 return;
3445 switch (scale) {
3446 case 3:
3447 if (!is_load || S) {
3448 unallocated_encoding(s);
3449 return;
3451 scale = size;
3452 replicate = true;
3453 break;
3454 case 0:
3455 break;
3456 case 1:
3457 if (extract32(size, 0, 1)) {
3458 unallocated_encoding(s);
3459 return;
3461 index >>= 1;
3462 break;
3463 case 2:
3464 if (extract32(size, 1, 1)) {
3465 unallocated_encoding(s);
3466 return;
3468 if (!extract32(size, 0, 1)) {
3469 index >>= 2;
3470 } else {
3471 if (S) {
3472 unallocated_encoding(s);
3473 return;
3475 index >>= 3;
3476 scale = 3;
3478 break;
3479 default:
3480 g_assert_not_reached();
3483 if (!fp_access_check(s)) {
3484 return;
3487 ebytes = 1 << scale;
3489 if (rn == 31) {
3490 gen_check_sp_alignment(s);
3493 tcg_rn = cpu_reg_sp(s, rn);
3494 clean_addr = clean_data_tbi(s, tcg_rn);
3495 tcg_ebytes = tcg_const_i64(ebytes);
3497 for (xs = 0; xs < selem; xs++) {
3498 if (replicate) {
3499 /* Load and replicate to all elements */
3500 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3502 tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr,
3503 get_mem_index(s), s->be_data + scale);
3504 tcg_gen_gvec_dup_i64(scale, vec_full_reg_offset(s, rt),
3505 (is_q + 1) * 8, vec_full_reg_size(s),
3506 tcg_tmp);
3507 tcg_temp_free_i64(tcg_tmp);
3508 } else {
3509 /* Load/store one element per register */
3510 if (is_load) {
3511 do_vec_ld(s, rt, index, clean_addr, scale, s->be_data);
3512 } else {
3513 do_vec_st(s, rt, index, clean_addr, scale, s->be_data);
3516 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3517 rt = (rt + 1) % 32;
3519 tcg_temp_free_i64(tcg_ebytes);
3521 if (is_postidx) {
3522 if (rm == 31) {
3523 tcg_gen_addi_i64(tcg_rn, tcg_rn, selem * ebytes);
3524 } else {
3525 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3530 /* Loads and stores */
3531 static void disas_ldst(DisasContext *s, uint32_t insn)
3533 switch (extract32(insn, 24, 6)) {
3534 case 0x08: /* Load/store exclusive */
3535 disas_ldst_excl(s, insn);
3536 break;
3537 case 0x18: case 0x1c: /* Load register (literal) */
3538 disas_ld_lit(s, insn);
3539 break;
3540 case 0x28: case 0x29:
3541 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
3542 disas_ldst_pair(s, insn);
3543 break;
3544 case 0x38: case 0x39:
3545 case 0x3c: case 0x3d: /* Load/store register (all forms) */
3546 disas_ldst_reg(s, insn);
3547 break;
3548 case 0x0c: /* AdvSIMD load/store multiple structures */
3549 disas_ldst_multiple_struct(s, insn);
3550 break;
3551 case 0x0d: /* AdvSIMD load/store single structure */
3552 disas_ldst_single_struct(s, insn);
3553 break;
3554 default:
3555 unallocated_encoding(s);
3556 break;
3560 /* PC-rel. addressing
3561 * 31 30 29 28 24 23 5 4 0
3562 * +----+-------+-----------+-------------------+------+
3563 * | op | immlo | 1 0 0 0 0 | immhi | Rd |
3564 * +----+-------+-----------+-------------------+------+
3566 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
3568 unsigned int page, rd;
3569 uint64_t base;
3570 uint64_t offset;
3572 page = extract32(insn, 31, 1);
3573 /* SignExtend(immhi:immlo) -> offset */
3574 offset = sextract64(insn, 5, 19);
3575 offset = offset << 2 | extract32(insn, 29, 2);
3576 rd = extract32(insn, 0, 5);
3577 base = s->pc_curr;
3579 if (page) {
3580 /* ADRP (page based) */
3581 base &= ~0xfff;
3582 offset <<= 12;
3585 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
3589 * Add/subtract (immediate)
3591 * 31 30 29 28 24 23 22 21 10 9 5 4 0
3592 * +--+--+--+-----------+-----+-------------+-----+-----+
3593 * |sf|op| S| 1 0 0 0 1 |shift| imm12 | Rn | Rd |
3594 * +--+--+--+-----------+-----+-------------+-----+-----+
3596 * sf: 0 -> 32bit, 1 -> 64bit
3597 * op: 0 -> add , 1 -> sub
3598 * S: 1 -> set flags
3599 * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
3601 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
3603 int rd = extract32(insn, 0, 5);
3604 int rn = extract32(insn, 5, 5);
3605 uint64_t imm = extract32(insn, 10, 12);
3606 int shift = extract32(insn, 22, 2);
3607 bool setflags = extract32(insn, 29, 1);
3608 bool sub_op = extract32(insn, 30, 1);
3609 bool is_64bit = extract32(insn, 31, 1);
3611 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3612 TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
3613 TCGv_i64 tcg_result;
3615 switch (shift) {
3616 case 0x0:
3617 break;
3618 case 0x1:
3619 imm <<= 12;
3620 break;
3621 default:
3622 unallocated_encoding(s);
3623 return;
3626 tcg_result = tcg_temp_new_i64();
3627 if (!setflags) {
3628 if (sub_op) {
3629 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
3630 } else {
3631 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
3633 } else {
3634 TCGv_i64 tcg_imm = tcg_const_i64(imm);
3635 if (sub_op) {
3636 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
3637 } else {
3638 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
3640 tcg_temp_free_i64(tcg_imm);
3643 if (is_64bit) {
3644 tcg_gen_mov_i64(tcg_rd, tcg_result);
3645 } else {
3646 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3649 tcg_temp_free_i64(tcg_result);
3652 /* The input should be a value in the bottom e bits (with higher
3653 * bits zero); returns that value replicated into every element
3654 * of size e in a 64 bit integer.
3656 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
3658 assert(e != 0);
3659 while (e < 64) {
3660 mask |= mask << e;
3661 e *= 2;
3663 return mask;
3666 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
3667 static inline uint64_t bitmask64(unsigned int length)
3669 assert(length > 0 && length <= 64);
3670 return ~0ULL >> (64 - length);
3673 /* Simplified variant of pseudocode DecodeBitMasks() for the case where we
3674 * only require the wmask. Returns false if the imms/immr/immn are a reserved
3675 * value (ie should cause a guest UNDEF exception), and true if they are
3676 * valid, in which case the decoded bit pattern is written to result.
3678 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
3679 unsigned int imms, unsigned int immr)
3681 uint64_t mask;
3682 unsigned e, levels, s, r;
3683 int len;
3685 assert(immn < 2 && imms < 64 && immr < 64);
3687 /* The bit patterns we create here are 64 bit patterns which
3688 * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
3689 * 64 bits each. Each element contains the same value: a run
3690 * of between 1 and e-1 non-zero bits, rotated within the
3691 * element by between 0 and e-1 bits.
3693 * The element size and run length are encoded into immn (1 bit)
3694 * and imms (6 bits) as follows:
3695 * 64 bit elements: immn = 1, imms = <length of run - 1>
3696 * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
3697 * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
3698 * 8 bit elements: immn = 0, imms = 110 : <length of run - 1>
3699 * 4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
3700 * 2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
3701 * Notice that immn = 0, imms = 11111x is the only combination
3702 * not covered by one of the above options; this is reserved.
3703 * Further, <length of run - 1> all-ones is a reserved pattern.
3705 * In all cases the rotation is by immr % e (and immr is 6 bits).
3708 /* First determine the element size */
3709 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
3710 if (len < 1) {
3711 /* This is the immn == 0, imms == 0x11111x case */
3712 return false;
3714 e = 1 << len;
3716 levels = e - 1;
3717 s = imms & levels;
3718 r = immr & levels;
3720 if (s == levels) {
3721 /* <length of run - 1> mustn't be all-ones. */
3722 return false;
3725 /* Create the value of one element: s+1 set bits rotated
3726 * by r within the element (which is e bits wide)...
3728 mask = bitmask64(s + 1);
3729 if (r) {
3730 mask = (mask >> r) | (mask << (e - r));
3731 mask &= bitmask64(e);
3733 /* ...then replicate the element over the whole 64 bit value */
3734 mask = bitfield_replicate(mask, e);
3735 *result = mask;
3736 return true;
3739 /* Logical (immediate)
3740 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
3741 * +----+-----+-------------+---+------+------+------+------+
3742 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms | Rn | Rd |
3743 * +----+-----+-------------+---+------+------+------+------+
3745 static void disas_logic_imm(DisasContext *s, uint32_t insn)
3747 unsigned int sf, opc, is_n, immr, imms, rn, rd;
3748 TCGv_i64 tcg_rd, tcg_rn;
3749 uint64_t wmask;
3750 bool is_and = false;
3752 sf = extract32(insn, 31, 1);
3753 opc = extract32(insn, 29, 2);
3754 is_n = extract32(insn, 22, 1);
3755 immr = extract32(insn, 16, 6);
3756 imms = extract32(insn, 10, 6);
3757 rn = extract32(insn, 5, 5);
3758 rd = extract32(insn, 0, 5);
3760 if (!sf && is_n) {
3761 unallocated_encoding(s);
3762 return;
3765 if (opc == 0x3) { /* ANDS */
3766 tcg_rd = cpu_reg(s, rd);
3767 } else {
3768 tcg_rd = cpu_reg_sp(s, rd);
3770 tcg_rn = cpu_reg(s, rn);
3772 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
3773 /* some immediate field values are reserved */
3774 unallocated_encoding(s);
3775 return;
3778 if (!sf) {
3779 wmask &= 0xffffffff;
3782 switch (opc) {
3783 case 0x3: /* ANDS */
3784 case 0x0: /* AND */
3785 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
3786 is_and = true;
3787 break;
3788 case 0x1: /* ORR */
3789 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
3790 break;
3791 case 0x2: /* EOR */
3792 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
3793 break;
3794 default:
3795 assert(FALSE); /* must handle all above */
3796 break;
3799 if (!sf && !is_and) {
3800 /* zero extend final result; we know we can skip this for AND
3801 * since the immediate had the high 32 bits clear.
3803 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3806 if (opc == 3) { /* ANDS */
3807 gen_logic_CC(sf, tcg_rd);
3812 * Move wide (immediate)
3814 * 31 30 29 28 23 22 21 20 5 4 0
3815 * +--+-----+-------------+-----+----------------+------+
3816 * |sf| opc | 1 0 0 1 0 1 | hw | imm16 | Rd |
3817 * +--+-----+-------------+-----+----------------+------+
3819 * sf: 0 -> 32 bit, 1 -> 64 bit
3820 * opc: 00 -> N, 10 -> Z, 11 -> K
3821 * hw: shift/16 (0,16, and sf only 32, 48)
3823 static void disas_movw_imm(DisasContext *s, uint32_t insn)
3825 int rd = extract32(insn, 0, 5);
3826 uint64_t imm = extract32(insn, 5, 16);
3827 int sf = extract32(insn, 31, 1);
3828 int opc = extract32(insn, 29, 2);
3829 int pos = extract32(insn, 21, 2) << 4;
3830 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3831 TCGv_i64 tcg_imm;
3833 if (!sf && (pos >= 32)) {
3834 unallocated_encoding(s);
3835 return;
3838 switch (opc) {
3839 case 0: /* MOVN */
3840 case 2: /* MOVZ */
3841 imm <<= pos;
3842 if (opc == 0) {
3843 imm = ~imm;
3845 if (!sf) {
3846 imm &= 0xffffffffu;
3848 tcg_gen_movi_i64(tcg_rd, imm);
3849 break;
3850 case 3: /* MOVK */
3851 tcg_imm = tcg_const_i64(imm);
3852 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
3853 tcg_temp_free_i64(tcg_imm);
3854 if (!sf) {
3855 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3857 break;
3858 default:
3859 unallocated_encoding(s);
3860 break;
3864 /* Bitfield
3865 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
3866 * +----+-----+-------------+---+------+------+------+------+
3867 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms | Rn | Rd |
3868 * +----+-----+-------------+---+------+------+------+------+
3870 static void disas_bitfield(DisasContext *s, uint32_t insn)
3872 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
3873 TCGv_i64 tcg_rd, tcg_tmp;
3875 sf = extract32(insn, 31, 1);
3876 opc = extract32(insn, 29, 2);
3877 n = extract32(insn, 22, 1);
3878 ri = extract32(insn, 16, 6);
3879 si = extract32(insn, 10, 6);
3880 rn = extract32(insn, 5, 5);
3881 rd = extract32(insn, 0, 5);
3882 bitsize = sf ? 64 : 32;
3884 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
3885 unallocated_encoding(s);
3886 return;
3889 tcg_rd = cpu_reg(s, rd);
3891 /* Suppress the zero-extend for !sf. Since RI and SI are constrained
3892 to be smaller than bitsize, we'll never reference data outside the
3893 low 32-bits anyway. */
3894 tcg_tmp = read_cpu_reg(s, rn, 1);
3896 /* Recognize simple(r) extractions. */
3897 if (si >= ri) {
3898 /* Wd<s-r:0> = Wn<s:r> */
3899 len = (si - ri) + 1;
3900 if (opc == 0) { /* SBFM: ASR, SBFX, SXTB, SXTH, SXTW */
3901 tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
3902 goto done;
3903 } else if (opc == 2) { /* UBFM: UBFX, LSR, UXTB, UXTH */
3904 tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
3905 return;
3907 /* opc == 1, BFXIL fall through to deposit */
3908 tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
3909 pos = 0;
3910 } else {
3911 /* Handle the ri > si case with a deposit
3912 * Wd<32+s-r,32-r> = Wn<s:0>
3914 len = si + 1;
3915 pos = (bitsize - ri) & (bitsize - 1);
3918 if (opc == 0 && len < ri) {
3919 /* SBFM: sign extend the destination field from len to fill
3920 the balance of the word. Let the deposit below insert all
3921 of those sign bits. */
3922 tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
3923 len = ri;
3926 if (opc == 1) { /* BFM, BFXIL */
3927 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
3928 } else {
3929 /* SBFM or UBFM: We start with zero, and we haven't modified
3930 any bits outside bitsize, therefore the zero-extension
3931 below is unneeded. */
3932 tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
3933 return;
3936 done:
3937 if (!sf) { /* zero extend final result */
3938 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3942 /* Extract
3943 * 31 30 29 28 23 22 21 20 16 15 10 9 5 4 0
3944 * +----+------+-------------+---+----+------+--------+------+------+
3945 * | sf | op21 | 1 0 0 1 1 1 | N | o0 | Rm | imms | Rn | Rd |
3946 * +----+------+-------------+---+----+------+--------+------+------+
3948 static void disas_extract(DisasContext *s, uint32_t insn)
3950 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
3952 sf = extract32(insn, 31, 1);
3953 n = extract32(insn, 22, 1);
3954 rm = extract32(insn, 16, 5);
3955 imm = extract32(insn, 10, 6);
3956 rn = extract32(insn, 5, 5);
3957 rd = extract32(insn, 0, 5);
3958 op21 = extract32(insn, 29, 2);
3959 op0 = extract32(insn, 21, 1);
3960 bitsize = sf ? 64 : 32;
3962 if (sf != n || op21 || op0 || imm >= bitsize) {
3963 unallocated_encoding(s);
3964 } else {
3965 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
3967 tcg_rd = cpu_reg(s, rd);
3969 if (unlikely(imm == 0)) {
3970 /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
3971 * so an extract from bit 0 is a special case.
3973 if (sf) {
3974 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
3975 } else {
3976 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
3978 } else {
3979 tcg_rm = cpu_reg(s, rm);
3980 tcg_rn = cpu_reg(s, rn);
3982 if (sf) {
3983 /* Specialization to ROR happens in EXTRACT2. */
3984 tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, imm);
3985 } else {
3986 TCGv_i32 t0 = tcg_temp_new_i32();
3988 tcg_gen_extrl_i64_i32(t0, tcg_rm);
3989 if (rm == rn) {
3990 tcg_gen_rotri_i32(t0, t0, imm);
3991 } else {
3992 TCGv_i32 t1 = tcg_temp_new_i32();
3993 tcg_gen_extrl_i64_i32(t1, tcg_rn);
3994 tcg_gen_extract2_i32(t0, t0, t1, imm);
3995 tcg_temp_free_i32(t1);
3997 tcg_gen_extu_i32_i64(tcg_rd, t0);
3998 tcg_temp_free_i32(t0);
4004 /* Data processing - immediate */
4005 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
4007 switch (extract32(insn, 23, 6)) {
4008 case 0x20: case 0x21: /* PC-rel. addressing */
4009 disas_pc_rel_adr(s, insn);
4010 break;
4011 case 0x22: case 0x23: /* Add/subtract (immediate) */
4012 disas_add_sub_imm(s, insn);
4013 break;
4014 case 0x24: /* Logical (immediate) */
4015 disas_logic_imm(s, insn);
4016 break;
4017 case 0x25: /* Move wide (immediate) */
4018 disas_movw_imm(s, insn);
4019 break;
4020 case 0x26: /* Bitfield */
4021 disas_bitfield(s, insn);
4022 break;
4023 case 0x27: /* Extract */
4024 disas_extract(s, insn);
4025 break;
4026 default:
4027 unallocated_encoding(s);
4028 break;
4032 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4033 * Note that it is the caller's responsibility to ensure that the
4034 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4035 * mandated semantics for out of range shifts.
4037 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4038 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4040 switch (shift_type) {
4041 case A64_SHIFT_TYPE_LSL:
4042 tcg_gen_shl_i64(dst, src, shift_amount);
4043 break;
4044 case A64_SHIFT_TYPE_LSR:
4045 tcg_gen_shr_i64(dst, src, shift_amount);
4046 break;
4047 case A64_SHIFT_TYPE_ASR:
4048 if (!sf) {
4049 tcg_gen_ext32s_i64(dst, src);
4051 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4052 break;
4053 case A64_SHIFT_TYPE_ROR:
4054 if (sf) {
4055 tcg_gen_rotr_i64(dst, src, shift_amount);
4056 } else {
4057 TCGv_i32 t0, t1;
4058 t0 = tcg_temp_new_i32();
4059 t1 = tcg_temp_new_i32();
4060 tcg_gen_extrl_i64_i32(t0, src);
4061 tcg_gen_extrl_i64_i32(t1, shift_amount);
4062 tcg_gen_rotr_i32(t0, t0, t1);
4063 tcg_gen_extu_i32_i64(dst, t0);
4064 tcg_temp_free_i32(t0);
4065 tcg_temp_free_i32(t1);
4067 break;
4068 default:
4069 assert(FALSE); /* all shift types should be handled */
4070 break;
4073 if (!sf) { /* zero extend final result */
4074 tcg_gen_ext32u_i64(dst, dst);
4078 /* Shift a TCGv src by immediate, put result in dst.
4079 * The shift amount must be in range (this should always be true as the
4080 * relevant instructions will UNDEF on bad shift immediates).
4082 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4083 enum a64_shift_type shift_type, unsigned int shift_i)
4085 assert(shift_i < (sf ? 64 : 32));
4087 if (shift_i == 0) {
4088 tcg_gen_mov_i64(dst, src);
4089 } else {
4090 TCGv_i64 shift_const;
4092 shift_const = tcg_const_i64(shift_i);
4093 shift_reg(dst, src, sf, shift_type, shift_const);
4094 tcg_temp_free_i64(shift_const);
4098 /* Logical (shifted register)
4099 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
4100 * +----+-----+-----------+-------+---+------+--------+------+------+
4101 * | sf | opc | 0 1 0 1 0 | shift | N | Rm | imm6 | Rn | Rd |
4102 * +----+-----+-----------+-------+---+------+--------+------+------+
4104 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4106 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4107 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4109 sf = extract32(insn, 31, 1);
4110 opc = extract32(insn, 29, 2);
4111 shift_type = extract32(insn, 22, 2);
4112 invert = extract32(insn, 21, 1);
4113 rm = extract32(insn, 16, 5);
4114 shift_amount = extract32(insn, 10, 6);
4115 rn = extract32(insn, 5, 5);
4116 rd = extract32(insn, 0, 5);
4118 if (!sf && (shift_amount & (1 << 5))) {
4119 unallocated_encoding(s);
4120 return;
4123 tcg_rd = cpu_reg(s, rd);
4125 if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4126 /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4127 * register-register MOV and MVN, so it is worth special casing.
4129 tcg_rm = cpu_reg(s, rm);
4130 if (invert) {
4131 tcg_gen_not_i64(tcg_rd, tcg_rm);
4132 if (!sf) {
4133 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4135 } else {
4136 if (sf) {
4137 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4138 } else {
4139 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4142 return;
4145 tcg_rm = read_cpu_reg(s, rm, sf);
4147 if (shift_amount) {
4148 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4151 tcg_rn = cpu_reg(s, rn);
4153 switch (opc | (invert << 2)) {
4154 case 0: /* AND */
4155 case 3: /* ANDS */
4156 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4157 break;
4158 case 1: /* ORR */
4159 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4160 break;
4161 case 2: /* EOR */
4162 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4163 break;
4164 case 4: /* BIC */
4165 case 7: /* BICS */
4166 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4167 break;
4168 case 5: /* ORN */
4169 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4170 break;
4171 case 6: /* EON */
4172 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4173 break;
4174 default:
4175 assert(FALSE);
4176 break;
4179 if (!sf) {
4180 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4183 if (opc == 3) {
4184 gen_logic_CC(sf, tcg_rd);
4189 * Add/subtract (extended register)
4191 * 31|30|29|28 24|23 22|21|20 16|15 13|12 10|9 5|4 0|
4192 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4193 * |sf|op| S| 0 1 0 1 1 | opt | 1| Rm |option| imm3 | Rn | Rd |
4194 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4196 * sf: 0 -> 32bit, 1 -> 64bit
4197 * op: 0 -> add , 1 -> sub
4198 * S: 1 -> set flags
4199 * opt: 00
4200 * option: extension type (see DecodeRegExtend)
4201 * imm3: optional shift to Rm
4203 * Rd = Rn + LSL(extend(Rm), amount)
4205 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4207 int rd = extract32(insn, 0, 5);
4208 int rn = extract32(insn, 5, 5);
4209 int imm3 = extract32(insn, 10, 3);
4210 int option = extract32(insn, 13, 3);
4211 int rm = extract32(insn, 16, 5);
4212 int opt = extract32(insn, 22, 2);
4213 bool setflags = extract32(insn, 29, 1);
4214 bool sub_op = extract32(insn, 30, 1);
4215 bool sf = extract32(insn, 31, 1);
4217 TCGv_i64 tcg_rm, tcg_rn; /* temps */
4218 TCGv_i64 tcg_rd;
4219 TCGv_i64 tcg_result;
4221 if (imm3 > 4 || opt != 0) {
4222 unallocated_encoding(s);
4223 return;
4226 /* non-flag setting ops may use SP */
4227 if (!setflags) {
4228 tcg_rd = cpu_reg_sp(s, rd);
4229 } else {
4230 tcg_rd = cpu_reg(s, rd);
4232 tcg_rn = read_cpu_reg_sp(s, rn, sf);
4234 tcg_rm = read_cpu_reg(s, rm, sf);
4235 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4237 tcg_result = tcg_temp_new_i64();
4239 if (!setflags) {
4240 if (sub_op) {
4241 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4242 } else {
4243 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4245 } else {
4246 if (sub_op) {
4247 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4248 } else {
4249 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4253 if (sf) {
4254 tcg_gen_mov_i64(tcg_rd, tcg_result);
4255 } else {
4256 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4259 tcg_temp_free_i64(tcg_result);
4263 * Add/subtract (shifted register)
4265 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
4266 * +--+--+--+-----------+-----+--+-------+---------+------+------+
4267 * |sf|op| S| 0 1 0 1 1 |shift| 0| Rm | imm6 | Rn | Rd |
4268 * +--+--+--+-----------+-----+--+-------+---------+------+------+
4270 * sf: 0 -> 32bit, 1 -> 64bit
4271 * op: 0 -> add , 1 -> sub
4272 * S: 1 -> set flags
4273 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4274 * imm6: Shift amount to apply to Rm before the add/sub
4276 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4278 int rd = extract32(insn, 0, 5);
4279 int rn = extract32(insn, 5, 5);
4280 int imm6 = extract32(insn, 10, 6);
4281 int rm = extract32(insn, 16, 5);
4282 int shift_type = extract32(insn, 22, 2);
4283 bool setflags = extract32(insn, 29, 1);
4284 bool sub_op = extract32(insn, 30, 1);
4285 bool sf = extract32(insn, 31, 1);
4287 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4288 TCGv_i64 tcg_rn, tcg_rm;
4289 TCGv_i64 tcg_result;
4291 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4292 unallocated_encoding(s);
4293 return;
4296 tcg_rn = read_cpu_reg(s, rn, sf);
4297 tcg_rm = read_cpu_reg(s, rm, sf);
4299 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4301 tcg_result = tcg_temp_new_i64();
4303 if (!setflags) {
4304 if (sub_op) {
4305 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4306 } else {
4307 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4309 } else {
4310 if (sub_op) {
4311 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4312 } else {
4313 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4317 if (sf) {
4318 tcg_gen_mov_i64(tcg_rd, tcg_result);
4319 } else {
4320 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4323 tcg_temp_free_i64(tcg_result);
4326 /* Data-processing (3 source)
4328 * 31 30 29 28 24 23 21 20 16 15 14 10 9 5 4 0
4329 * +--+------+-----------+------+------+----+------+------+------+
4330 * |sf| op54 | 1 1 0 1 1 | op31 | Rm | o0 | Ra | Rn | Rd |
4331 * +--+------+-----------+------+------+----+------+------+------+
4333 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4335 int rd = extract32(insn, 0, 5);
4336 int rn = extract32(insn, 5, 5);
4337 int ra = extract32(insn, 10, 5);
4338 int rm = extract32(insn, 16, 5);
4339 int op_id = (extract32(insn, 29, 3) << 4) |
4340 (extract32(insn, 21, 3) << 1) |
4341 extract32(insn, 15, 1);
4342 bool sf = extract32(insn, 31, 1);
4343 bool is_sub = extract32(op_id, 0, 1);
4344 bool is_high = extract32(op_id, 2, 1);
4345 bool is_signed = false;
4346 TCGv_i64 tcg_op1;
4347 TCGv_i64 tcg_op2;
4348 TCGv_i64 tcg_tmp;
4350 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
4351 switch (op_id) {
4352 case 0x42: /* SMADDL */
4353 case 0x43: /* SMSUBL */
4354 case 0x44: /* SMULH */
4355 is_signed = true;
4356 break;
4357 case 0x0: /* MADD (32bit) */
4358 case 0x1: /* MSUB (32bit) */
4359 case 0x40: /* MADD (64bit) */
4360 case 0x41: /* MSUB (64bit) */
4361 case 0x4a: /* UMADDL */
4362 case 0x4b: /* UMSUBL */
4363 case 0x4c: /* UMULH */
4364 break;
4365 default:
4366 unallocated_encoding(s);
4367 return;
4370 if (is_high) {
4371 TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
4372 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4373 TCGv_i64 tcg_rn = cpu_reg(s, rn);
4374 TCGv_i64 tcg_rm = cpu_reg(s, rm);
4376 if (is_signed) {
4377 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4378 } else {
4379 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4382 tcg_temp_free_i64(low_bits);
4383 return;
4386 tcg_op1 = tcg_temp_new_i64();
4387 tcg_op2 = tcg_temp_new_i64();
4388 tcg_tmp = tcg_temp_new_i64();
4390 if (op_id < 0x42) {
4391 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
4392 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
4393 } else {
4394 if (is_signed) {
4395 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
4396 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
4397 } else {
4398 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
4399 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
4403 if (ra == 31 && !is_sub) {
4404 /* Special-case MADD with rA == XZR; it is the standard MUL alias */
4405 tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
4406 } else {
4407 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
4408 if (is_sub) {
4409 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4410 } else {
4411 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4415 if (!sf) {
4416 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
4419 tcg_temp_free_i64(tcg_op1);
4420 tcg_temp_free_i64(tcg_op2);
4421 tcg_temp_free_i64(tcg_tmp);
4424 /* Add/subtract (with carry)
4425 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
4426 * +--+--+--+------------------------+------+-------------+------+-----+
4427 * |sf|op| S| 1 1 0 1 0 0 0 0 | rm | 0 0 0 0 0 0 | Rn | Rd |
4428 * +--+--+--+------------------------+------+-------------+------+-----+
4431 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
4433 unsigned int sf, op, setflags, rm, rn, rd;
4434 TCGv_i64 tcg_y, tcg_rn, tcg_rd;
4436 sf = extract32(insn, 31, 1);
4437 op = extract32(insn, 30, 1);
4438 setflags = extract32(insn, 29, 1);
4439 rm = extract32(insn, 16, 5);
4440 rn = extract32(insn, 5, 5);
4441 rd = extract32(insn, 0, 5);
4443 tcg_rd = cpu_reg(s, rd);
4444 tcg_rn = cpu_reg(s, rn);
4446 if (op) {
4447 tcg_y = new_tmp_a64(s);
4448 tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
4449 } else {
4450 tcg_y = cpu_reg(s, rm);
4453 if (setflags) {
4454 gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
4455 } else {
4456 gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
4461 * Rotate right into flags
4462 * 31 30 29 21 15 10 5 4 0
4463 * +--+--+--+-----------------+--------+-----------+------+--+------+
4464 * |sf|op| S| 1 1 0 1 0 0 0 0 | imm6 | 0 0 0 0 1 | Rn |o2| mask |
4465 * +--+--+--+-----------------+--------+-----------+------+--+------+
4467 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
4469 int mask = extract32(insn, 0, 4);
4470 int o2 = extract32(insn, 4, 1);
4471 int rn = extract32(insn, 5, 5);
4472 int imm6 = extract32(insn, 15, 6);
4473 int sf_op_s = extract32(insn, 29, 3);
4474 TCGv_i64 tcg_rn;
4475 TCGv_i32 nzcv;
4477 if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
4478 unallocated_encoding(s);
4479 return;
4482 tcg_rn = read_cpu_reg(s, rn, 1);
4483 tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
4485 nzcv = tcg_temp_new_i32();
4486 tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
4488 if (mask & 8) { /* N */
4489 tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
4491 if (mask & 4) { /* Z */
4492 tcg_gen_not_i32(cpu_ZF, nzcv);
4493 tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
4495 if (mask & 2) { /* C */
4496 tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
4498 if (mask & 1) { /* V */
4499 tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
4502 tcg_temp_free_i32(nzcv);
4506 * Evaluate into flags
4507 * 31 30 29 21 15 14 10 5 4 0
4508 * +--+--+--+-----------------+---------+----+---------+------+--+------+
4509 * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 | Rn |o3| mask |
4510 * +--+--+--+-----------------+---------+----+---------+------+--+------+
4512 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
4514 int o3_mask = extract32(insn, 0, 5);
4515 int rn = extract32(insn, 5, 5);
4516 int o2 = extract32(insn, 15, 6);
4517 int sz = extract32(insn, 14, 1);
4518 int sf_op_s = extract32(insn, 29, 3);
4519 TCGv_i32 tmp;
4520 int shift;
4522 if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
4523 !dc_isar_feature(aa64_condm_4, s)) {
4524 unallocated_encoding(s);
4525 return;
4527 shift = sz ? 16 : 24; /* SETF16 or SETF8 */
4529 tmp = tcg_temp_new_i32();
4530 tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
4531 tcg_gen_shli_i32(cpu_NF, tmp, shift);
4532 tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
4533 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
4534 tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
4535 tcg_temp_free_i32(tmp);
4538 /* Conditional compare (immediate / register)
4539 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
4540 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
4541 * |sf|op| S| 1 1 0 1 0 0 1 0 |imm5/rm | cond |i/r |o2| Rn |o3|nzcv |
4542 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
4543 * [1] y [0] [0]
4545 static void disas_cc(DisasContext *s, uint32_t insn)
4547 unsigned int sf, op, y, cond, rn, nzcv, is_imm;
4548 TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
4549 TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
4550 DisasCompare c;
4552 if (!extract32(insn, 29, 1)) {
4553 unallocated_encoding(s);
4554 return;
4556 if (insn & (1 << 10 | 1 << 4)) {
4557 unallocated_encoding(s);
4558 return;
4560 sf = extract32(insn, 31, 1);
4561 op = extract32(insn, 30, 1);
4562 is_imm = extract32(insn, 11, 1);
4563 y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
4564 cond = extract32(insn, 12, 4);
4565 rn = extract32(insn, 5, 5);
4566 nzcv = extract32(insn, 0, 4);
4568 /* Set T0 = !COND. */
4569 tcg_t0 = tcg_temp_new_i32();
4570 arm_test_cc(&c, cond);
4571 tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
4572 arm_free_cc(&c);
4574 /* Load the arguments for the new comparison. */
4575 if (is_imm) {
4576 tcg_y = new_tmp_a64(s);
4577 tcg_gen_movi_i64(tcg_y, y);
4578 } else {
4579 tcg_y = cpu_reg(s, y);
4581 tcg_rn = cpu_reg(s, rn);
4583 /* Set the flags for the new comparison. */
4584 tcg_tmp = tcg_temp_new_i64();
4585 if (op) {
4586 gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
4587 } else {
4588 gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
4590 tcg_temp_free_i64(tcg_tmp);
4592 /* If COND was false, force the flags to #nzcv. Compute two masks
4593 * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
4594 * For tcg hosts that support ANDC, we can make do with just T1.
4595 * In either case, allow the tcg optimizer to delete any unused mask.
4597 tcg_t1 = tcg_temp_new_i32();
4598 tcg_t2 = tcg_temp_new_i32();
4599 tcg_gen_neg_i32(tcg_t1, tcg_t0);
4600 tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
4602 if (nzcv & 8) { /* N */
4603 tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
4604 } else {
4605 if (TCG_TARGET_HAS_andc_i32) {
4606 tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
4607 } else {
4608 tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
4611 if (nzcv & 4) { /* Z */
4612 if (TCG_TARGET_HAS_andc_i32) {
4613 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
4614 } else {
4615 tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
4617 } else {
4618 tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
4620 if (nzcv & 2) { /* C */
4621 tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
4622 } else {
4623 if (TCG_TARGET_HAS_andc_i32) {
4624 tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
4625 } else {
4626 tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
4629 if (nzcv & 1) { /* V */
4630 tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
4631 } else {
4632 if (TCG_TARGET_HAS_andc_i32) {
4633 tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
4634 } else {
4635 tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
4638 tcg_temp_free_i32(tcg_t0);
4639 tcg_temp_free_i32(tcg_t1);
4640 tcg_temp_free_i32(tcg_t2);
4643 /* Conditional select
4644 * 31 30 29 28 21 20 16 15 12 11 10 9 5 4 0
4645 * +----+----+---+-----------------+------+------+-----+------+------+
4646 * | sf | op | S | 1 1 0 1 0 1 0 0 | Rm | cond | op2 | Rn | Rd |
4647 * +----+----+---+-----------------+------+------+-----+------+------+
4649 static void disas_cond_select(DisasContext *s, uint32_t insn)
4651 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
4652 TCGv_i64 tcg_rd, zero;
4653 DisasCompare64 c;
4655 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
4656 /* S == 1 or op2<1> == 1 */
4657 unallocated_encoding(s);
4658 return;
4660 sf = extract32(insn, 31, 1);
4661 else_inv = extract32(insn, 30, 1);
4662 rm = extract32(insn, 16, 5);
4663 cond = extract32(insn, 12, 4);
4664 else_inc = extract32(insn, 10, 1);
4665 rn = extract32(insn, 5, 5);
4666 rd = extract32(insn, 0, 5);
4668 tcg_rd = cpu_reg(s, rd);
4670 a64_test_cc(&c, cond);
4671 zero = tcg_const_i64(0);
4673 if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
4674 /* CSET & CSETM. */
4675 tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
4676 if (else_inv) {
4677 tcg_gen_neg_i64(tcg_rd, tcg_rd);
4679 } else {
4680 TCGv_i64 t_true = cpu_reg(s, rn);
4681 TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
4682 if (else_inv && else_inc) {
4683 tcg_gen_neg_i64(t_false, t_false);
4684 } else if (else_inv) {
4685 tcg_gen_not_i64(t_false, t_false);
4686 } else if (else_inc) {
4687 tcg_gen_addi_i64(t_false, t_false, 1);
4689 tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
4692 tcg_temp_free_i64(zero);
4693 a64_free_cc(&c);
4695 if (!sf) {
4696 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4700 static void handle_clz(DisasContext *s, unsigned int sf,
4701 unsigned int rn, unsigned int rd)
4703 TCGv_i64 tcg_rd, tcg_rn;
4704 tcg_rd = cpu_reg(s, rd);
4705 tcg_rn = cpu_reg(s, rn);
4707 if (sf) {
4708 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
4709 } else {
4710 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4711 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4712 tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
4713 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4714 tcg_temp_free_i32(tcg_tmp32);
4718 static void handle_cls(DisasContext *s, unsigned int sf,
4719 unsigned int rn, unsigned int rd)
4721 TCGv_i64 tcg_rd, tcg_rn;
4722 tcg_rd = cpu_reg(s, rd);
4723 tcg_rn = cpu_reg(s, rn);
4725 if (sf) {
4726 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
4727 } else {
4728 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4729 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4730 tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
4731 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4732 tcg_temp_free_i32(tcg_tmp32);
4736 static void handle_rbit(DisasContext *s, unsigned int sf,
4737 unsigned int rn, unsigned int rd)
4739 TCGv_i64 tcg_rd, tcg_rn;
4740 tcg_rd = cpu_reg(s, rd);
4741 tcg_rn = cpu_reg(s, rn);
4743 if (sf) {
4744 gen_helper_rbit64(tcg_rd, tcg_rn);
4745 } else {
4746 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4747 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4748 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
4749 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4750 tcg_temp_free_i32(tcg_tmp32);
4754 /* REV with sf==1, opcode==3 ("REV64") */
4755 static void handle_rev64(DisasContext *s, unsigned int sf,
4756 unsigned int rn, unsigned int rd)
4758 if (!sf) {
4759 unallocated_encoding(s);
4760 return;
4762 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
4765 /* REV with sf==0, opcode==2
4766 * REV32 (sf==1, opcode==2)
4768 static void handle_rev32(DisasContext *s, unsigned int sf,
4769 unsigned int rn, unsigned int rd)
4771 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4773 if (sf) {
4774 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4775 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4777 /* bswap32_i64 requires zero high word */
4778 tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
4779 tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
4780 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
4781 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
4782 tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
4784 tcg_temp_free_i64(tcg_tmp);
4785 } else {
4786 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
4787 tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
4791 /* REV16 (opcode==1) */
4792 static void handle_rev16(DisasContext *s, unsigned int sf,
4793 unsigned int rn, unsigned int rd)
4795 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4796 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4797 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4798 TCGv_i64 mask = tcg_const_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
4800 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
4801 tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
4802 tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
4803 tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
4804 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
4806 tcg_temp_free_i64(mask);
4807 tcg_temp_free_i64(tcg_tmp);
4810 /* Data-processing (1 source)
4811 * 31 30 29 28 21 20 16 15 10 9 5 4 0
4812 * +----+---+---+-----------------+---------+--------+------+------+
4813 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode | Rn | Rd |
4814 * +----+---+---+-----------------+---------+--------+------+------+
4816 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
4818 unsigned int sf, opcode, opcode2, rn, rd;
4819 TCGv_i64 tcg_rd;
4821 if (extract32(insn, 29, 1)) {
4822 unallocated_encoding(s);
4823 return;
4826 sf = extract32(insn, 31, 1);
4827 opcode = extract32(insn, 10, 6);
4828 opcode2 = extract32(insn, 16, 5);
4829 rn = extract32(insn, 5, 5);
4830 rd = extract32(insn, 0, 5);
4832 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
4834 switch (MAP(sf, opcode2, opcode)) {
4835 case MAP(0, 0x00, 0x00): /* RBIT */
4836 case MAP(1, 0x00, 0x00):
4837 handle_rbit(s, sf, rn, rd);
4838 break;
4839 case MAP(0, 0x00, 0x01): /* REV16 */
4840 case MAP(1, 0x00, 0x01):
4841 handle_rev16(s, sf, rn, rd);
4842 break;
4843 case MAP(0, 0x00, 0x02): /* REV/REV32 */
4844 case MAP(1, 0x00, 0x02):
4845 handle_rev32(s, sf, rn, rd);
4846 break;
4847 case MAP(1, 0x00, 0x03): /* REV64 */
4848 handle_rev64(s, sf, rn, rd);
4849 break;
4850 case MAP(0, 0x00, 0x04): /* CLZ */
4851 case MAP(1, 0x00, 0x04):
4852 handle_clz(s, sf, rn, rd);
4853 break;
4854 case MAP(0, 0x00, 0x05): /* CLS */
4855 case MAP(1, 0x00, 0x05):
4856 handle_cls(s, sf, rn, rd);
4857 break;
4858 case MAP(1, 0x01, 0x00): /* PACIA */
4859 if (s->pauth_active) {
4860 tcg_rd = cpu_reg(s, rd);
4861 gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4862 } else if (!dc_isar_feature(aa64_pauth, s)) {
4863 goto do_unallocated;
4865 break;
4866 case MAP(1, 0x01, 0x01): /* PACIB */
4867 if (s->pauth_active) {
4868 tcg_rd = cpu_reg(s, rd);
4869 gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4870 } else if (!dc_isar_feature(aa64_pauth, s)) {
4871 goto do_unallocated;
4873 break;
4874 case MAP(1, 0x01, 0x02): /* PACDA */
4875 if (s->pauth_active) {
4876 tcg_rd = cpu_reg(s, rd);
4877 gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4878 } else if (!dc_isar_feature(aa64_pauth, s)) {
4879 goto do_unallocated;
4881 break;
4882 case MAP(1, 0x01, 0x03): /* PACDB */
4883 if (s->pauth_active) {
4884 tcg_rd = cpu_reg(s, rd);
4885 gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4886 } else if (!dc_isar_feature(aa64_pauth, s)) {
4887 goto do_unallocated;
4889 break;
4890 case MAP(1, 0x01, 0x04): /* AUTIA */
4891 if (s->pauth_active) {
4892 tcg_rd = cpu_reg(s, rd);
4893 gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4894 } else if (!dc_isar_feature(aa64_pauth, s)) {
4895 goto do_unallocated;
4897 break;
4898 case MAP(1, 0x01, 0x05): /* AUTIB */
4899 if (s->pauth_active) {
4900 tcg_rd = cpu_reg(s, rd);
4901 gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4902 } else if (!dc_isar_feature(aa64_pauth, s)) {
4903 goto do_unallocated;
4905 break;
4906 case MAP(1, 0x01, 0x06): /* AUTDA */
4907 if (s->pauth_active) {
4908 tcg_rd = cpu_reg(s, rd);
4909 gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4910 } else if (!dc_isar_feature(aa64_pauth, s)) {
4911 goto do_unallocated;
4913 break;
4914 case MAP(1, 0x01, 0x07): /* AUTDB */
4915 if (s->pauth_active) {
4916 tcg_rd = cpu_reg(s, rd);
4917 gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
4918 } else if (!dc_isar_feature(aa64_pauth, s)) {
4919 goto do_unallocated;
4921 break;
4922 case MAP(1, 0x01, 0x08): /* PACIZA */
4923 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
4924 goto do_unallocated;
4925 } else if (s->pauth_active) {
4926 tcg_rd = cpu_reg(s, rd);
4927 gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
4929 break;
4930 case MAP(1, 0x01, 0x09): /* PACIZB */
4931 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
4932 goto do_unallocated;
4933 } else if (s->pauth_active) {
4934 tcg_rd = cpu_reg(s, rd);
4935 gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
4937 break;
4938 case MAP(1, 0x01, 0x0a): /* PACDZA */
4939 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
4940 goto do_unallocated;
4941 } else if (s->pauth_active) {
4942 tcg_rd = cpu_reg(s, rd);
4943 gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
4945 break;
4946 case MAP(1, 0x01, 0x0b): /* PACDZB */
4947 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
4948 goto do_unallocated;
4949 } else if (s->pauth_active) {
4950 tcg_rd = cpu_reg(s, rd);
4951 gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
4953 break;
4954 case MAP(1, 0x01, 0x0c): /* AUTIZA */
4955 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
4956 goto do_unallocated;
4957 } else if (s->pauth_active) {
4958 tcg_rd = cpu_reg(s, rd);
4959 gen_helper_autia(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
4961 break;
4962 case MAP(1, 0x01, 0x0d): /* AUTIZB */
4963 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
4964 goto do_unallocated;
4965 } else if (s->pauth_active) {
4966 tcg_rd = cpu_reg(s, rd);
4967 gen_helper_autib(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
4969 break;
4970 case MAP(1, 0x01, 0x0e): /* AUTDZA */
4971 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
4972 goto do_unallocated;
4973 } else if (s->pauth_active) {
4974 tcg_rd = cpu_reg(s, rd);
4975 gen_helper_autda(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
4977 break;
4978 case MAP(1, 0x01, 0x0f): /* AUTDZB */
4979 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
4980 goto do_unallocated;
4981 } else if (s->pauth_active) {
4982 tcg_rd = cpu_reg(s, rd);
4983 gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
4985 break;
4986 case MAP(1, 0x01, 0x10): /* XPACI */
4987 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
4988 goto do_unallocated;
4989 } else if (s->pauth_active) {
4990 tcg_rd = cpu_reg(s, rd);
4991 gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
4993 break;
4994 case MAP(1, 0x01, 0x11): /* XPACD */
4995 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
4996 goto do_unallocated;
4997 } else if (s->pauth_active) {
4998 tcg_rd = cpu_reg(s, rd);
4999 gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5001 break;
5002 default:
5003 do_unallocated:
5004 unallocated_encoding(s);
5005 break;
5008 #undef MAP
5011 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5012 unsigned int rm, unsigned int rn, unsigned int rd)
5014 TCGv_i64 tcg_n, tcg_m, tcg_rd;
5015 tcg_rd = cpu_reg(s, rd);
5017 if (!sf && is_signed) {
5018 tcg_n = new_tmp_a64(s);
5019 tcg_m = new_tmp_a64(s);
5020 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5021 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5022 } else {
5023 tcg_n = read_cpu_reg(s, rn, sf);
5024 tcg_m = read_cpu_reg(s, rm, sf);
5027 if (is_signed) {
5028 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5029 } else {
5030 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5033 if (!sf) { /* zero extend final result */
5034 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5038 /* LSLV, LSRV, ASRV, RORV */
5039 static void handle_shift_reg(DisasContext *s,
5040 enum a64_shift_type shift_type, unsigned int sf,
5041 unsigned int rm, unsigned int rn, unsigned int rd)
5043 TCGv_i64 tcg_shift = tcg_temp_new_i64();
5044 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5045 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5047 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5048 shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5049 tcg_temp_free_i64(tcg_shift);
5052 /* CRC32[BHWX], CRC32C[BHWX] */
5053 static void handle_crc32(DisasContext *s,
5054 unsigned int sf, unsigned int sz, bool crc32c,
5055 unsigned int rm, unsigned int rn, unsigned int rd)
5057 TCGv_i64 tcg_acc, tcg_val;
5058 TCGv_i32 tcg_bytes;
5060 if (!dc_isar_feature(aa64_crc32, s)
5061 || (sf == 1 && sz != 3)
5062 || (sf == 0 && sz == 3)) {
5063 unallocated_encoding(s);
5064 return;
5067 if (sz == 3) {
5068 tcg_val = cpu_reg(s, rm);
5069 } else {
5070 uint64_t mask;
5071 switch (sz) {
5072 case 0:
5073 mask = 0xFF;
5074 break;
5075 case 1:
5076 mask = 0xFFFF;
5077 break;
5078 case 2:
5079 mask = 0xFFFFFFFF;
5080 break;
5081 default:
5082 g_assert_not_reached();
5084 tcg_val = new_tmp_a64(s);
5085 tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5088 tcg_acc = cpu_reg(s, rn);
5089 tcg_bytes = tcg_const_i32(1 << sz);
5091 if (crc32c) {
5092 gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5093 } else {
5094 gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5097 tcg_temp_free_i32(tcg_bytes);
5100 /* Data-processing (2 source)
5101 * 31 30 29 28 21 20 16 15 10 9 5 4 0
5102 * +----+---+---+-----------------+------+--------+------+------+
5103 * | sf | 0 | S | 1 1 0 1 0 1 1 0 | Rm | opcode | Rn | Rd |
5104 * +----+---+---+-----------------+------+--------+------+------+
5106 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5108 unsigned int sf, rm, opcode, rn, rd;
5109 sf = extract32(insn, 31, 1);
5110 rm = extract32(insn, 16, 5);
5111 opcode = extract32(insn, 10, 6);
5112 rn = extract32(insn, 5, 5);
5113 rd = extract32(insn, 0, 5);
5115 if (extract32(insn, 29, 1)) {
5116 unallocated_encoding(s);
5117 return;
5120 switch (opcode) {
5121 case 2: /* UDIV */
5122 handle_div(s, false, sf, rm, rn, rd);
5123 break;
5124 case 3: /* SDIV */
5125 handle_div(s, true, sf, rm, rn, rd);
5126 break;
5127 case 8: /* LSLV */
5128 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5129 break;
5130 case 9: /* LSRV */
5131 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5132 break;
5133 case 10: /* ASRV */
5134 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5135 break;
5136 case 11: /* RORV */
5137 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5138 break;
5139 case 12: /* PACGA */
5140 if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5141 goto do_unallocated;
5143 gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5144 cpu_reg(s, rn), cpu_reg_sp(s, rm));
5145 break;
5146 case 16:
5147 case 17:
5148 case 18:
5149 case 19:
5150 case 20:
5151 case 21:
5152 case 22:
5153 case 23: /* CRC32 */
5155 int sz = extract32(opcode, 0, 2);
5156 bool crc32c = extract32(opcode, 2, 1);
5157 handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5158 break;
5160 default:
5161 do_unallocated:
5162 unallocated_encoding(s);
5163 break;
5168 * Data processing - register
5169 * 31 30 29 28 25 21 20 16 10 0
5170 * +--+---+--+---+-------+-----+-------+-------+---------+
5171 * | |op0| |op1| 1 0 1 | op2 | | op3 | |
5172 * +--+---+--+---+-------+-----+-------+-------+---------+
5174 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5176 int op0 = extract32(insn, 30, 1);
5177 int op1 = extract32(insn, 28, 1);
5178 int op2 = extract32(insn, 21, 4);
5179 int op3 = extract32(insn, 10, 6);
5181 if (!op1) {
5182 if (op2 & 8) {
5183 if (op2 & 1) {
5184 /* Add/sub (extended register) */
5185 disas_add_sub_ext_reg(s, insn);
5186 } else {
5187 /* Add/sub (shifted register) */
5188 disas_add_sub_reg(s, insn);
5190 } else {
5191 /* Logical (shifted register) */
5192 disas_logic_reg(s, insn);
5194 return;
5197 switch (op2) {
5198 case 0x0:
5199 switch (op3) {
5200 case 0x00: /* Add/subtract (with carry) */
5201 disas_adc_sbc(s, insn);
5202 break;
5204 case 0x01: /* Rotate right into flags */
5205 case 0x21:
5206 disas_rotate_right_into_flags(s, insn);
5207 break;
5209 case 0x02: /* Evaluate into flags */
5210 case 0x12:
5211 case 0x22:
5212 case 0x32:
5213 disas_evaluate_into_flags(s, insn);
5214 break;
5216 default:
5217 goto do_unallocated;
5219 break;
5221 case 0x2: /* Conditional compare */
5222 disas_cc(s, insn); /* both imm and reg forms */
5223 break;
5225 case 0x4: /* Conditional select */
5226 disas_cond_select(s, insn);
5227 break;
5229 case 0x6: /* Data-processing */
5230 if (op0) { /* (1 source) */
5231 disas_data_proc_1src(s, insn);
5232 } else { /* (2 source) */
5233 disas_data_proc_2src(s, insn);
5235 break;
5236 case 0x8 ... 0xf: /* (3 source) */
5237 disas_data_proc_3src(s, insn);
5238 break;
5240 default:
5241 do_unallocated:
5242 unallocated_encoding(s);
5243 break;
5247 static void handle_fp_compare(DisasContext *s, int size,
5248 unsigned int rn, unsigned int rm,
5249 bool cmp_with_zero, bool signal_all_nans)
5251 TCGv_i64 tcg_flags = tcg_temp_new_i64();
5252 TCGv_ptr fpst = get_fpstatus_ptr(size == MO_16);
5254 if (size == MO_64) {
5255 TCGv_i64 tcg_vn, tcg_vm;
5257 tcg_vn = read_fp_dreg(s, rn);
5258 if (cmp_with_zero) {
5259 tcg_vm = tcg_const_i64(0);
5260 } else {
5261 tcg_vm = read_fp_dreg(s, rm);
5263 if (signal_all_nans) {
5264 gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5265 } else {
5266 gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5268 tcg_temp_free_i64(tcg_vn);
5269 tcg_temp_free_i64(tcg_vm);
5270 } else {
5271 TCGv_i32 tcg_vn = tcg_temp_new_i32();
5272 TCGv_i32 tcg_vm = tcg_temp_new_i32();
5274 read_vec_element_i32(s, tcg_vn, rn, 0, size);
5275 if (cmp_with_zero) {
5276 tcg_gen_movi_i32(tcg_vm, 0);
5277 } else {
5278 read_vec_element_i32(s, tcg_vm, rm, 0, size);
5281 switch (size) {
5282 case MO_32:
5283 if (signal_all_nans) {
5284 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5285 } else {
5286 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5288 break;
5289 case MO_16:
5290 if (signal_all_nans) {
5291 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5292 } else {
5293 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5295 break;
5296 default:
5297 g_assert_not_reached();
5300 tcg_temp_free_i32(tcg_vn);
5301 tcg_temp_free_i32(tcg_vm);
5304 tcg_temp_free_ptr(fpst);
5306 gen_set_nzcv(tcg_flags);
5308 tcg_temp_free_i64(tcg_flags);
5311 /* Floating point compare
5312 * 31 30 29 28 24 23 22 21 20 16 15 14 13 10 9 5 4 0
5313 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5314 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | op | 1 0 0 0 | Rn | op2 |
5315 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5317 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5319 unsigned int mos, type, rm, op, rn, opc, op2r;
5320 int size;
5322 mos = extract32(insn, 29, 3);
5323 type = extract32(insn, 22, 2);
5324 rm = extract32(insn, 16, 5);
5325 op = extract32(insn, 14, 2);
5326 rn = extract32(insn, 5, 5);
5327 opc = extract32(insn, 3, 2);
5328 op2r = extract32(insn, 0, 3);
5330 if (mos || op || op2r) {
5331 unallocated_encoding(s);
5332 return;
5335 switch (type) {
5336 case 0:
5337 size = MO_32;
5338 break;
5339 case 1:
5340 size = MO_64;
5341 break;
5342 case 3:
5343 size = MO_16;
5344 if (dc_isar_feature(aa64_fp16, s)) {
5345 break;
5347 /* fallthru */
5348 default:
5349 unallocated_encoding(s);
5350 return;
5353 if (!fp_access_check(s)) {
5354 return;
5357 handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
5360 /* Floating point conditional compare
5361 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
5362 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5363 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 0 1 | Rn | op | nzcv |
5364 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5366 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
5368 unsigned int mos, type, rm, cond, rn, op, nzcv;
5369 TCGv_i64 tcg_flags;
5370 TCGLabel *label_continue = NULL;
5371 int size;
5373 mos = extract32(insn, 29, 3);
5374 type = extract32(insn, 22, 2);
5375 rm = extract32(insn, 16, 5);
5376 cond = extract32(insn, 12, 4);
5377 rn = extract32(insn, 5, 5);
5378 op = extract32(insn, 4, 1);
5379 nzcv = extract32(insn, 0, 4);
5381 if (mos) {
5382 unallocated_encoding(s);
5383 return;
5386 switch (type) {
5387 case 0:
5388 size = MO_32;
5389 break;
5390 case 1:
5391 size = MO_64;
5392 break;
5393 case 3:
5394 size = MO_16;
5395 if (dc_isar_feature(aa64_fp16, s)) {
5396 break;
5398 /* fallthru */
5399 default:
5400 unallocated_encoding(s);
5401 return;
5404 if (!fp_access_check(s)) {
5405 return;
5408 if (cond < 0x0e) { /* not always */
5409 TCGLabel *label_match = gen_new_label();
5410 label_continue = gen_new_label();
5411 arm_gen_test_cc(cond, label_match);
5412 /* nomatch: */
5413 tcg_flags = tcg_const_i64(nzcv << 28);
5414 gen_set_nzcv(tcg_flags);
5415 tcg_temp_free_i64(tcg_flags);
5416 tcg_gen_br(label_continue);
5417 gen_set_label(label_match);
5420 handle_fp_compare(s, size, rn, rm, false, op);
5422 if (cond < 0x0e) {
5423 gen_set_label(label_continue);
5427 /* Floating point conditional select
5428 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
5429 * +---+---+---+-----------+------+---+------+------+-----+------+------+
5430 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 1 1 | Rn | Rd |
5431 * +---+---+---+-----------+------+---+------+------+-----+------+------+
5433 static void disas_fp_csel(DisasContext *s, uint32_t insn)
5435 unsigned int mos, type, rm, cond, rn, rd;
5436 TCGv_i64 t_true, t_false, t_zero;
5437 DisasCompare64 c;
5438 TCGMemOp sz;
5440 mos = extract32(insn, 29, 3);
5441 type = extract32(insn, 22, 2);
5442 rm = extract32(insn, 16, 5);
5443 cond = extract32(insn, 12, 4);
5444 rn = extract32(insn, 5, 5);
5445 rd = extract32(insn, 0, 5);
5447 if (mos) {
5448 unallocated_encoding(s);
5449 return;
5452 switch (type) {
5453 case 0:
5454 sz = MO_32;
5455 break;
5456 case 1:
5457 sz = MO_64;
5458 break;
5459 case 3:
5460 sz = MO_16;
5461 if (dc_isar_feature(aa64_fp16, s)) {
5462 break;
5464 /* fallthru */
5465 default:
5466 unallocated_encoding(s);
5467 return;
5470 if (!fp_access_check(s)) {
5471 return;
5474 /* Zero extend sreg & hreg inputs to 64 bits now. */
5475 t_true = tcg_temp_new_i64();
5476 t_false = tcg_temp_new_i64();
5477 read_vec_element(s, t_true, rn, 0, sz);
5478 read_vec_element(s, t_false, rm, 0, sz);
5480 a64_test_cc(&c, cond);
5481 t_zero = tcg_const_i64(0);
5482 tcg_gen_movcond_i64(c.cond, t_true, c.value, t_zero, t_true, t_false);
5483 tcg_temp_free_i64(t_zero);
5484 tcg_temp_free_i64(t_false);
5485 a64_free_cc(&c);
5487 /* Note that sregs & hregs write back zeros to the high bits,
5488 and we've already done the zero-extension. */
5489 write_fp_dreg(s, rd, t_true);
5490 tcg_temp_free_i64(t_true);
5493 /* Floating-point data-processing (1 source) - half precision */
5494 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
5496 TCGv_ptr fpst = NULL;
5497 TCGv_i32 tcg_op = read_fp_hreg(s, rn);
5498 TCGv_i32 tcg_res = tcg_temp_new_i32();
5500 switch (opcode) {
5501 case 0x0: /* FMOV */
5502 tcg_gen_mov_i32(tcg_res, tcg_op);
5503 break;
5504 case 0x1: /* FABS */
5505 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
5506 break;
5507 case 0x2: /* FNEG */
5508 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
5509 break;
5510 case 0x3: /* FSQRT */
5511 fpst = get_fpstatus_ptr(true);
5512 gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
5513 break;
5514 case 0x8: /* FRINTN */
5515 case 0x9: /* FRINTP */
5516 case 0xa: /* FRINTM */
5517 case 0xb: /* FRINTZ */
5518 case 0xc: /* FRINTA */
5520 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
5521 fpst = get_fpstatus_ptr(true);
5523 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5524 gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
5526 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5527 tcg_temp_free_i32(tcg_rmode);
5528 break;
5530 case 0xe: /* FRINTX */
5531 fpst = get_fpstatus_ptr(true);
5532 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
5533 break;
5534 case 0xf: /* FRINTI */
5535 fpst = get_fpstatus_ptr(true);
5536 gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
5537 break;
5538 default:
5539 abort();
5542 write_fp_sreg(s, rd, tcg_res);
5544 if (fpst) {
5545 tcg_temp_free_ptr(fpst);
5547 tcg_temp_free_i32(tcg_op);
5548 tcg_temp_free_i32(tcg_res);
5551 /* Floating-point data-processing (1 source) - single precision */
5552 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
5554 void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
5555 TCGv_i32 tcg_op, tcg_res;
5556 TCGv_ptr fpst;
5557 int rmode = -1;
5559 tcg_op = read_fp_sreg(s, rn);
5560 tcg_res = tcg_temp_new_i32();
5562 switch (opcode) {
5563 case 0x0: /* FMOV */
5564 tcg_gen_mov_i32(tcg_res, tcg_op);
5565 goto done;
5566 case 0x1: /* FABS */
5567 gen_helper_vfp_abss(tcg_res, tcg_op);
5568 goto done;
5569 case 0x2: /* FNEG */
5570 gen_helper_vfp_negs(tcg_res, tcg_op);
5571 goto done;
5572 case 0x3: /* FSQRT */
5573 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
5574 goto done;
5575 case 0x8: /* FRINTN */
5576 case 0x9: /* FRINTP */
5577 case 0xa: /* FRINTM */
5578 case 0xb: /* FRINTZ */
5579 case 0xc: /* FRINTA */
5580 rmode = arm_rmode_to_sf(opcode & 7);
5581 gen_fpst = gen_helper_rints;
5582 break;
5583 case 0xe: /* FRINTX */
5584 gen_fpst = gen_helper_rints_exact;
5585 break;
5586 case 0xf: /* FRINTI */
5587 gen_fpst = gen_helper_rints;
5588 break;
5589 case 0x10: /* FRINT32Z */
5590 rmode = float_round_to_zero;
5591 gen_fpst = gen_helper_frint32_s;
5592 break;
5593 case 0x11: /* FRINT32X */
5594 gen_fpst = gen_helper_frint32_s;
5595 break;
5596 case 0x12: /* FRINT64Z */
5597 rmode = float_round_to_zero;
5598 gen_fpst = gen_helper_frint64_s;
5599 break;
5600 case 0x13: /* FRINT64X */
5601 gen_fpst = gen_helper_frint64_s;
5602 break;
5603 default:
5604 g_assert_not_reached();
5607 fpst = get_fpstatus_ptr(false);
5608 if (rmode >= 0) {
5609 TCGv_i32 tcg_rmode = tcg_const_i32(rmode);
5610 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5611 gen_fpst(tcg_res, tcg_op, fpst);
5612 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5613 tcg_temp_free_i32(tcg_rmode);
5614 } else {
5615 gen_fpst(tcg_res, tcg_op, fpst);
5617 tcg_temp_free_ptr(fpst);
5619 done:
5620 write_fp_sreg(s, rd, tcg_res);
5621 tcg_temp_free_i32(tcg_op);
5622 tcg_temp_free_i32(tcg_res);
5625 /* Floating-point data-processing (1 source) - double precision */
5626 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
5628 void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
5629 TCGv_i64 tcg_op, tcg_res;
5630 TCGv_ptr fpst;
5631 int rmode = -1;
5633 switch (opcode) {
5634 case 0x0: /* FMOV */
5635 gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
5636 return;
5639 tcg_op = read_fp_dreg(s, rn);
5640 tcg_res = tcg_temp_new_i64();
5642 switch (opcode) {
5643 case 0x1: /* FABS */
5644 gen_helper_vfp_absd(tcg_res, tcg_op);
5645 goto done;
5646 case 0x2: /* FNEG */
5647 gen_helper_vfp_negd(tcg_res, tcg_op);
5648 goto done;
5649 case 0x3: /* FSQRT */
5650 gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
5651 goto done;
5652 case 0x8: /* FRINTN */
5653 case 0x9: /* FRINTP */
5654 case 0xa: /* FRINTM */
5655 case 0xb: /* FRINTZ */
5656 case 0xc: /* FRINTA */
5657 rmode = arm_rmode_to_sf(opcode & 7);
5658 gen_fpst = gen_helper_rintd;
5659 break;
5660 case 0xe: /* FRINTX */
5661 gen_fpst = gen_helper_rintd_exact;
5662 break;
5663 case 0xf: /* FRINTI */
5664 gen_fpst = gen_helper_rintd;
5665 break;
5666 case 0x10: /* FRINT32Z */
5667 rmode = float_round_to_zero;
5668 gen_fpst = gen_helper_frint32_d;
5669 break;
5670 case 0x11: /* FRINT32X */
5671 gen_fpst = gen_helper_frint32_d;
5672 break;
5673 case 0x12: /* FRINT64Z */
5674 rmode = float_round_to_zero;
5675 gen_fpst = gen_helper_frint64_d;
5676 break;
5677 case 0x13: /* FRINT64X */
5678 gen_fpst = gen_helper_frint64_d;
5679 break;
5680 default:
5681 g_assert_not_reached();
5684 fpst = get_fpstatus_ptr(false);
5685 if (rmode >= 0) {
5686 TCGv_i32 tcg_rmode = tcg_const_i32(rmode);
5687 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5688 gen_fpst(tcg_res, tcg_op, fpst);
5689 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5690 tcg_temp_free_i32(tcg_rmode);
5691 } else {
5692 gen_fpst(tcg_res, tcg_op, fpst);
5694 tcg_temp_free_ptr(fpst);
5696 done:
5697 write_fp_dreg(s, rd, tcg_res);
5698 tcg_temp_free_i64(tcg_op);
5699 tcg_temp_free_i64(tcg_res);
5702 static void handle_fp_fcvt(DisasContext *s, int opcode,
5703 int rd, int rn, int dtype, int ntype)
5705 switch (ntype) {
5706 case 0x0:
5708 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
5709 if (dtype == 1) {
5710 /* Single to double */
5711 TCGv_i64 tcg_rd = tcg_temp_new_i64();
5712 gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
5713 write_fp_dreg(s, rd, tcg_rd);
5714 tcg_temp_free_i64(tcg_rd);
5715 } else {
5716 /* Single to half */
5717 TCGv_i32 tcg_rd = tcg_temp_new_i32();
5718 TCGv_i32 ahp = get_ahp_flag();
5719 TCGv_ptr fpst = get_fpstatus_ptr(false);
5721 gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
5722 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
5723 write_fp_sreg(s, rd, tcg_rd);
5724 tcg_temp_free_i32(tcg_rd);
5725 tcg_temp_free_i32(ahp);
5726 tcg_temp_free_ptr(fpst);
5728 tcg_temp_free_i32(tcg_rn);
5729 break;
5731 case 0x1:
5733 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
5734 TCGv_i32 tcg_rd = tcg_temp_new_i32();
5735 if (dtype == 0) {
5736 /* Double to single */
5737 gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
5738 } else {
5739 TCGv_ptr fpst = get_fpstatus_ptr(false);
5740 TCGv_i32 ahp = get_ahp_flag();
5741 /* Double to half */
5742 gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
5743 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
5744 tcg_temp_free_ptr(fpst);
5745 tcg_temp_free_i32(ahp);
5747 write_fp_sreg(s, rd, tcg_rd);
5748 tcg_temp_free_i32(tcg_rd);
5749 tcg_temp_free_i64(tcg_rn);
5750 break;
5752 case 0x3:
5754 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
5755 TCGv_ptr tcg_fpst = get_fpstatus_ptr(false);
5756 TCGv_i32 tcg_ahp = get_ahp_flag();
5757 tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
5758 if (dtype == 0) {
5759 /* Half to single */
5760 TCGv_i32 tcg_rd = tcg_temp_new_i32();
5761 gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
5762 write_fp_sreg(s, rd, tcg_rd);
5763 tcg_temp_free_ptr(tcg_fpst);
5764 tcg_temp_free_i32(tcg_ahp);
5765 tcg_temp_free_i32(tcg_rd);
5766 } else {
5767 /* Half to double */
5768 TCGv_i64 tcg_rd = tcg_temp_new_i64();
5769 gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
5770 write_fp_dreg(s, rd, tcg_rd);
5771 tcg_temp_free_i64(tcg_rd);
5773 tcg_temp_free_i32(tcg_rn);
5774 break;
5776 default:
5777 abort();
5781 /* Floating point data-processing (1 source)
5782 * 31 30 29 28 24 23 22 21 20 15 14 10 9 5 4 0
5783 * +---+---+---+-----------+------+---+--------+-----------+------+------+
5784 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 | Rn | Rd |
5785 * +---+---+---+-----------+------+---+--------+-----------+------+------+
5787 static void disas_fp_1src(DisasContext *s, uint32_t insn)
5789 int mos = extract32(insn, 29, 3);
5790 int type = extract32(insn, 22, 2);
5791 int opcode = extract32(insn, 15, 6);
5792 int rn = extract32(insn, 5, 5);
5793 int rd = extract32(insn, 0, 5);
5795 if (mos) {
5796 unallocated_encoding(s);
5797 return;
5800 switch (opcode) {
5801 case 0x4: case 0x5: case 0x7:
5803 /* FCVT between half, single and double precision */
5804 int dtype = extract32(opcode, 0, 2);
5805 if (type == 2 || dtype == type) {
5806 unallocated_encoding(s);
5807 return;
5809 if (!fp_access_check(s)) {
5810 return;
5813 handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
5814 break;
5817 case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
5818 if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
5819 unallocated_encoding(s);
5820 return;
5822 /* fall through */
5823 case 0x0 ... 0x3:
5824 case 0x8 ... 0xc:
5825 case 0xe ... 0xf:
5826 /* 32-to-32 and 64-to-64 ops */
5827 switch (type) {
5828 case 0:
5829 if (!fp_access_check(s)) {
5830 return;
5832 handle_fp_1src_single(s, opcode, rd, rn);
5833 break;
5834 case 1:
5835 if (!fp_access_check(s)) {
5836 return;
5838 handle_fp_1src_double(s, opcode, rd, rn);
5839 break;
5840 case 3:
5841 if (!dc_isar_feature(aa64_fp16, s)) {
5842 unallocated_encoding(s);
5843 return;
5846 if (!fp_access_check(s)) {
5847 return;
5849 handle_fp_1src_half(s, opcode, rd, rn);
5850 break;
5851 default:
5852 unallocated_encoding(s);
5854 break;
5856 default:
5857 unallocated_encoding(s);
5858 break;
5862 /* Floating-point data-processing (2 source) - single precision */
5863 static void handle_fp_2src_single(DisasContext *s, int opcode,
5864 int rd, int rn, int rm)
5866 TCGv_i32 tcg_op1;
5867 TCGv_i32 tcg_op2;
5868 TCGv_i32 tcg_res;
5869 TCGv_ptr fpst;
5871 tcg_res = tcg_temp_new_i32();
5872 fpst = get_fpstatus_ptr(false);
5873 tcg_op1 = read_fp_sreg(s, rn);
5874 tcg_op2 = read_fp_sreg(s, rm);
5876 switch (opcode) {
5877 case 0x0: /* FMUL */
5878 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
5879 break;
5880 case 0x1: /* FDIV */
5881 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
5882 break;
5883 case 0x2: /* FADD */
5884 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
5885 break;
5886 case 0x3: /* FSUB */
5887 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
5888 break;
5889 case 0x4: /* FMAX */
5890 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
5891 break;
5892 case 0x5: /* FMIN */
5893 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
5894 break;
5895 case 0x6: /* FMAXNM */
5896 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
5897 break;
5898 case 0x7: /* FMINNM */
5899 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
5900 break;
5901 case 0x8: /* FNMUL */
5902 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
5903 gen_helper_vfp_negs(tcg_res, tcg_res);
5904 break;
5907 write_fp_sreg(s, rd, tcg_res);
5909 tcg_temp_free_ptr(fpst);
5910 tcg_temp_free_i32(tcg_op1);
5911 tcg_temp_free_i32(tcg_op2);
5912 tcg_temp_free_i32(tcg_res);
5915 /* Floating-point data-processing (2 source) - double precision */
5916 static void handle_fp_2src_double(DisasContext *s, int opcode,
5917 int rd, int rn, int rm)
5919 TCGv_i64 tcg_op1;
5920 TCGv_i64 tcg_op2;
5921 TCGv_i64 tcg_res;
5922 TCGv_ptr fpst;
5924 tcg_res = tcg_temp_new_i64();
5925 fpst = get_fpstatus_ptr(false);
5926 tcg_op1 = read_fp_dreg(s, rn);
5927 tcg_op2 = read_fp_dreg(s, rm);
5929 switch (opcode) {
5930 case 0x0: /* FMUL */
5931 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
5932 break;
5933 case 0x1: /* FDIV */
5934 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
5935 break;
5936 case 0x2: /* FADD */
5937 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
5938 break;
5939 case 0x3: /* FSUB */
5940 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
5941 break;
5942 case 0x4: /* FMAX */
5943 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
5944 break;
5945 case 0x5: /* FMIN */
5946 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
5947 break;
5948 case 0x6: /* FMAXNM */
5949 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
5950 break;
5951 case 0x7: /* FMINNM */
5952 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
5953 break;
5954 case 0x8: /* FNMUL */
5955 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
5956 gen_helper_vfp_negd(tcg_res, tcg_res);
5957 break;
5960 write_fp_dreg(s, rd, tcg_res);
5962 tcg_temp_free_ptr(fpst);
5963 tcg_temp_free_i64(tcg_op1);
5964 tcg_temp_free_i64(tcg_op2);
5965 tcg_temp_free_i64(tcg_res);
5968 /* Floating-point data-processing (2 source) - half precision */
5969 static void handle_fp_2src_half(DisasContext *s, int opcode,
5970 int rd, int rn, int rm)
5972 TCGv_i32 tcg_op1;
5973 TCGv_i32 tcg_op2;
5974 TCGv_i32 tcg_res;
5975 TCGv_ptr fpst;
5977 tcg_res = tcg_temp_new_i32();
5978 fpst = get_fpstatus_ptr(true);
5979 tcg_op1 = read_fp_hreg(s, rn);
5980 tcg_op2 = read_fp_hreg(s, rm);
5982 switch (opcode) {
5983 case 0x0: /* FMUL */
5984 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
5985 break;
5986 case 0x1: /* FDIV */
5987 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
5988 break;
5989 case 0x2: /* FADD */
5990 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
5991 break;
5992 case 0x3: /* FSUB */
5993 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
5994 break;
5995 case 0x4: /* FMAX */
5996 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
5997 break;
5998 case 0x5: /* FMIN */
5999 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6000 break;
6001 case 0x6: /* FMAXNM */
6002 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6003 break;
6004 case 0x7: /* FMINNM */
6005 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6006 break;
6007 case 0x8: /* FNMUL */
6008 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6009 tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6010 break;
6011 default:
6012 g_assert_not_reached();
6015 write_fp_sreg(s, rd, tcg_res);
6017 tcg_temp_free_ptr(fpst);
6018 tcg_temp_free_i32(tcg_op1);
6019 tcg_temp_free_i32(tcg_op2);
6020 tcg_temp_free_i32(tcg_res);
6023 /* Floating point data-processing (2 source)
6024 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
6025 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6026 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | opcode | 1 0 | Rn | Rd |
6027 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6029 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6031 int mos = extract32(insn, 29, 3);
6032 int type = extract32(insn, 22, 2);
6033 int rd = extract32(insn, 0, 5);
6034 int rn = extract32(insn, 5, 5);
6035 int rm = extract32(insn, 16, 5);
6036 int opcode = extract32(insn, 12, 4);
6038 if (opcode > 8 || mos) {
6039 unallocated_encoding(s);
6040 return;
6043 switch (type) {
6044 case 0:
6045 if (!fp_access_check(s)) {
6046 return;
6048 handle_fp_2src_single(s, opcode, rd, rn, rm);
6049 break;
6050 case 1:
6051 if (!fp_access_check(s)) {
6052 return;
6054 handle_fp_2src_double(s, opcode, rd, rn, rm);
6055 break;
6056 case 3:
6057 if (!dc_isar_feature(aa64_fp16, s)) {
6058 unallocated_encoding(s);
6059 return;
6061 if (!fp_access_check(s)) {
6062 return;
6064 handle_fp_2src_half(s, opcode, rd, rn, rm);
6065 break;
6066 default:
6067 unallocated_encoding(s);
6071 /* Floating-point data-processing (3 source) - single precision */
6072 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6073 int rd, int rn, int rm, int ra)
6075 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6076 TCGv_i32 tcg_res = tcg_temp_new_i32();
6077 TCGv_ptr fpst = get_fpstatus_ptr(false);
6079 tcg_op1 = read_fp_sreg(s, rn);
6080 tcg_op2 = read_fp_sreg(s, rm);
6081 tcg_op3 = read_fp_sreg(s, ra);
6083 /* These are fused multiply-add, and must be done as one
6084 * floating point operation with no rounding between the
6085 * multiplication and addition steps.
6086 * NB that doing the negations here as separate steps is
6087 * correct : an input NaN should come out with its sign bit
6088 * flipped if it is a negated-input.
6090 if (o1 == true) {
6091 gen_helper_vfp_negs(tcg_op3, tcg_op3);
6094 if (o0 != o1) {
6095 gen_helper_vfp_negs(tcg_op1, tcg_op1);
6098 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6100 write_fp_sreg(s, rd, tcg_res);
6102 tcg_temp_free_ptr(fpst);
6103 tcg_temp_free_i32(tcg_op1);
6104 tcg_temp_free_i32(tcg_op2);
6105 tcg_temp_free_i32(tcg_op3);
6106 tcg_temp_free_i32(tcg_res);
6109 /* Floating-point data-processing (3 source) - double precision */
6110 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6111 int rd, int rn, int rm, int ra)
6113 TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6114 TCGv_i64 tcg_res = tcg_temp_new_i64();
6115 TCGv_ptr fpst = get_fpstatus_ptr(false);
6117 tcg_op1 = read_fp_dreg(s, rn);
6118 tcg_op2 = read_fp_dreg(s, rm);
6119 tcg_op3 = read_fp_dreg(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_negd(tcg_op3, tcg_op3);
6132 if (o0 != o1) {
6133 gen_helper_vfp_negd(tcg_op1, tcg_op1);
6136 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6138 write_fp_dreg(s, rd, tcg_res);
6140 tcg_temp_free_ptr(fpst);
6141 tcg_temp_free_i64(tcg_op1);
6142 tcg_temp_free_i64(tcg_op2);
6143 tcg_temp_free_i64(tcg_op3);
6144 tcg_temp_free_i64(tcg_res);
6147 /* Floating-point data-processing (3 source) - half precision */
6148 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6149 int rd, int rn, int rm, int ra)
6151 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6152 TCGv_i32 tcg_res = tcg_temp_new_i32();
6153 TCGv_ptr fpst = get_fpstatus_ptr(true);
6155 tcg_op1 = read_fp_hreg(s, rn);
6156 tcg_op2 = read_fp_hreg(s, rm);
6157 tcg_op3 = read_fp_hreg(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 tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6170 if (o0 != o1) {
6171 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6174 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6176 write_fp_sreg(s, rd, tcg_res);
6178 tcg_temp_free_ptr(fpst);
6179 tcg_temp_free_i32(tcg_op1);
6180 tcg_temp_free_i32(tcg_op2);
6181 tcg_temp_free_i32(tcg_op3);
6182 tcg_temp_free_i32(tcg_res);
6185 /* Floating point data-processing (3 source)
6186 * 31 30 29 28 24 23 22 21 20 16 15 14 10 9 5 4 0
6187 * +---+---+---+-----------+------+----+------+----+------+------+------+
6188 * | M | 0 | S | 1 1 1 1 1 | type | o1 | Rm | o0 | Ra | Rn | Rd |
6189 * +---+---+---+-----------+------+----+------+----+------+------+------+
6191 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6193 int mos = extract32(insn, 29, 3);
6194 int type = extract32(insn, 22, 2);
6195 int rd = extract32(insn, 0, 5);
6196 int rn = extract32(insn, 5, 5);
6197 int ra = extract32(insn, 10, 5);
6198 int rm = extract32(insn, 16, 5);
6199 bool o0 = extract32(insn, 15, 1);
6200 bool o1 = extract32(insn, 21, 1);
6202 if (mos) {
6203 unallocated_encoding(s);
6204 return;
6207 switch (type) {
6208 case 0:
6209 if (!fp_access_check(s)) {
6210 return;
6212 handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6213 break;
6214 case 1:
6215 if (!fp_access_check(s)) {
6216 return;
6218 handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6219 break;
6220 case 3:
6221 if (!dc_isar_feature(aa64_fp16, s)) {
6222 unallocated_encoding(s);
6223 return;
6225 if (!fp_access_check(s)) {
6226 return;
6228 handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6229 break;
6230 default:
6231 unallocated_encoding(s);
6235 /* Floating point immediate
6236 * 31 30 29 28 24 23 22 21 20 13 12 10 9 5 4 0
6237 * +---+---+---+-----------+------+---+------------+-------+------+------+
6238 * | M | 0 | S | 1 1 1 1 0 | type | 1 | imm8 | 1 0 0 | imm5 | Rd |
6239 * +---+---+---+-----------+------+---+------------+-------+------+------+
6241 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6243 int rd = extract32(insn, 0, 5);
6244 int imm5 = extract32(insn, 5, 5);
6245 int imm8 = extract32(insn, 13, 8);
6246 int type = extract32(insn, 22, 2);
6247 int mos = extract32(insn, 29, 3);
6248 uint64_t imm;
6249 TCGv_i64 tcg_res;
6250 TCGMemOp sz;
6252 if (mos || imm5) {
6253 unallocated_encoding(s);
6254 return;
6257 switch (type) {
6258 case 0:
6259 sz = MO_32;
6260 break;
6261 case 1:
6262 sz = MO_64;
6263 break;
6264 case 3:
6265 sz = MO_16;
6266 if (dc_isar_feature(aa64_fp16, s)) {
6267 break;
6269 /* fallthru */
6270 default:
6271 unallocated_encoding(s);
6272 return;
6275 if (!fp_access_check(s)) {
6276 return;
6279 imm = vfp_expand_imm(sz, imm8);
6281 tcg_res = tcg_const_i64(imm);
6282 write_fp_dreg(s, rd, tcg_res);
6283 tcg_temp_free_i64(tcg_res);
6286 /* Handle floating point <=> fixed point conversions. Note that we can
6287 * also deal with fp <=> integer conversions as a special case (scale == 64)
6288 * OPTME: consider handling that special case specially or at least skipping
6289 * the call to scalbn in the helpers for zero shifts.
6291 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6292 bool itof, int rmode, int scale, int sf, int type)
6294 bool is_signed = !(opcode & 1);
6295 TCGv_ptr tcg_fpstatus;
6296 TCGv_i32 tcg_shift, tcg_single;
6297 TCGv_i64 tcg_double;
6299 tcg_fpstatus = get_fpstatus_ptr(type == 3);
6301 tcg_shift = tcg_const_i32(64 - scale);
6303 if (itof) {
6304 TCGv_i64 tcg_int = cpu_reg(s, rn);
6305 if (!sf) {
6306 TCGv_i64 tcg_extend = new_tmp_a64(s);
6308 if (is_signed) {
6309 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6310 } else {
6311 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6314 tcg_int = tcg_extend;
6317 switch (type) {
6318 case 1: /* float64 */
6319 tcg_double = tcg_temp_new_i64();
6320 if (is_signed) {
6321 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6322 tcg_shift, tcg_fpstatus);
6323 } else {
6324 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6325 tcg_shift, tcg_fpstatus);
6327 write_fp_dreg(s, rd, tcg_double);
6328 tcg_temp_free_i64(tcg_double);
6329 break;
6331 case 0: /* float32 */
6332 tcg_single = tcg_temp_new_i32();
6333 if (is_signed) {
6334 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6335 tcg_shift, tcg_fpstatus);
6336 } else {
6337 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6338 tcg_shift, tcg_fpstatus);
6340 write_fp_sreg(s, rd, tcg_single);
6341 tcg_temp_free_i32(tcg_single);
6342 break;
6344 case 3: /* float16 */
6345 tcg_single = tcg_temp_new_i32();
6346 if (is_signed) {
6347 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6348 tcg_shift, tcg_fpstatus);
6349 } else {
6350 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6351 tcg_shift, tcg_fpstatus);
6353 write_fp_sreg(s, rd, tcg_single);
6354 tcg_temp_free_i32(tcg_single);
6355 break;
6357 default:
6358 g_assert_not_reached();
6360 } else {
6361 TCGv_i64 tcg_int = cpu_reg(s, rd);
6362 TCGv_i32 tcg_rmode;
6364 if (extract32(opcode, 2, 1)) {
6365 /* There are too many rounding modes to all fit into rmode,
6366 * so FCVTA[US] is a special case.
6368 rmode = FPROUNDING_TIEAWAY;
6371 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
6373 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
6375 switch (type) {
6376 case 1: /* float64 */
6377 tcg_double = read_fp_dreg(s, rn);
6378 if (is_signed) {
6379 if (!sf) {
6380 gen_helper_vfp_tosld(tcg_int, tcg_double,
6381 tcg_shift, tcg_fpstatus);
6382 } else {
6383 gen_helper_vfp_tosqd(tcg_int, tcg_double,
6384 tcg_shift, tcg_fpstatus);
6386 } else {
6387 if (!sf) {
6388 gen_helper_vfp_tould(tcg_int, tcg_double,
6389 tcg_shift, tcg_fpstatus);
6390 } else {
6391 gen_helper_vfp_touqd(tcg_int, tcg_double,
6392 tcg_shift, tcg_fpstatus);
6395 if (!sf) {
6396 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6398 tcg_temp_free_i64(tcg_double);
6399 break;
6401 case 0: /* float32 */
6402 tcg_single = read_fp_sreg(s, rn);
6403 if (sf) {
6404 if (is_signed) {
6405 gen_helper_vfp_tosqs(tcg_int, tcg_single,
6406 tcg_shift, tcg_fpstatus);
6407 } else {
6408 gen_helper_vfp_touqs(tcg_int, tcg_single,
6409 tcg_shift, tcg_fpstatus);
6411 } else {
6412 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6413 if (is_signed) {
6414 gen_helper_vfp_tosls(tcg_dest, tcg_single,
6415 tcg_shift, tcg_fpstatus);
6416 } else {
6417 gen_helper_vfp_touls(tcg_dest, tcg_single,
6418 tcg_shift, tcg_fpstatus);
6420 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6421 tcg_temp_free_i32(tcg_dest);
6423 tcg_temp_free_i32(tcg_single);
6424 break;
6426 case 3: /* float16 */
6427 tcg_single = read_fp_sreg(s, rn);
6428 if (sf) {
6429 if (is_signed) {
6430 gen_helper_vfp_tosqh(tcg_int, tcg_single,
6431 tcg_shift, tcg_fpstatus);
6432 } else {
6433 gen_helper_vfp_touqh(tcg_int, tcg_single,
6434 tcg_shift, tcg_fpstatus);
6436 } else {
6437 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6438 if (is_signed) {
6439 gen_helper_vfp_toslh(tcg_dest, tcg_single,
6440 tcg_shift, tcg_fpstatus);
6441 } else {
6442 gen_helper_vfp_toulh(tcg_dest, tcg_single,
6443 tcg_shift, tcg_fpstatus);
6445 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6446 tcg_temp_free_i32(tcg_dest);
6448 tcg_temp_free_i32(tcg_single);
6449 break;
6451 default:
6452 g_assert_not_reached();
6455 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
6456 tcg_temp_free_i32(tcg_rmode);
6459 tcg_temp_free_ptr(tcg_fpstatus);
6460 tcg_temp_free_i32(tcg_shift);
6463 /* Floating point <-> fixed point conversions
6464 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
6465 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6466 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale | Rn | Rd |
6467 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6469 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
6471 int rd = extract32(insn, 0, 5);
6472 int rn = extract32(insn, 5, 5);
6473 int scale = extract32(insn, 10, 6);
6474 int opcode = extract32(insn, 16, 3);
6475 int rmode = extract32(insn, 19, 2);
6476 int type = extract32(insn, 22, 2);
6477 bool sbit = extract32(insn, 29, 1);
6478 bool sf = extract32(insn, 31, 1);
6479 bool itof;
6481 if (sbit || (!sf && scale < 32)) {
6482 unallocated_encoding(s);
6483 return;
6486 switch (type) {
6487 case 0: /* float32 */
6488 case 1: /* float64 */
6489 break;
6490 case 3: /* float16 */
6491 if (dc_isar_feature(aa64_fp16, s)) {
6492 break;
6494 /* fallthru */
6495 default:
6496 unallocated_encoding(s);
6497 return;
6500 switch ((rmode << 3) | opcode) {
6501 case 0x2: /* SCVTF */
6502 case 0x3: /* UCVTF */
6503 itof = true;
6504 break;
6505 case 0x18: /* FCVTZS */
6506 case 0x19: /* FCVTZU */
6507 itof = false;
6508 break;
6509 default:
6510 unallocated_encoding(s);
6511 return;
6514 if (!fp_access_check(s)) {
6515 return;
6518 handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
6521 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
6523 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
6524 * without conversion.
6527 if (itof) {
6528 TCGv_i64 tcg_rn = cpu_reg(s, rn);
6529 TCGv_i64 tmp;
6531 switch (type) {
6532 case 0:
6533 /* 32 bit */
6534 tmp = tcg_temp_new_i64();
6535 tcg_gen_ext32u_i64(tmp, tcg_rn);
6536 write_fp_dreg(s, rd, tmp);
6537 tcg_temp_free_i64(tmp);
6538 break;
6539 case 1:
6540 /* 64 bit */
6541 write_fp_dreg(s, rd, tcg_rn);
6542 break;
6543 case 2:
6544 /* 64 bit to top half. */
6545 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
6546 clear_vec_high(s, true, rd);
6547 break;
6548 case 3:
6549 /* 16 bit */
6550 tmp = tcg_temp_new_i64();
6551 tcg_gen_ext16u_i64(tmp, tcg_rn);
6552 write_fp_dreg(s, rd, tmp);
6553 tcg_temp_free_i64(tmp);
6554 break;
6555 default:
6556 g_assert_not_reached();
6558 } else {
6559 TCGv_i64 tcg_rd = cpu_reg(s, rd);
6561 switch (type) {
6562 case 0:
6563 /* 32 bit */
6564 tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
6565 break;
6566 case 1:
6567 /* 64 bit */
6568 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
6569 break;
6570 case 2:
6571 /* 64 bits from top half */
6572 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
6573 break;
6574 case 3:
6575 /* 16 bit */
6576 tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
6577 break;
6578 default:
6579 g_assert_not_reached();
6584 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
6586 TCGv_i64 t = read_fp_dreg(s, rn);
6587 TCGv_ptr fpstatus = get_fpstatus_ptr(false);
6589 gen_helper_fjcvtzs(t, t, fpstatus);
6591 tcg_temp_free_ptr(fpstatus);
6593 tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
6594 tcg_gen_extrh_i64_i32(cpu_ZF, t);
6595 tcg_gen_movi_i32(cpu_CF, 0);
6596 tcg_gen_movi_i32(cpu_NF, 0);
6597 tcg_gen_movi_i32(cpu_VF, 0);
6599 tcg_temp_free_i64(t);
6602 /* Floating point <-> integer conversions
6603 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
6604 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
6605 * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
6606 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
6608 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
6610 int rd = extract32(insn, 0, 5);
6611 int rn = extract32(insn, 5, 5);
6612 int opcode = extract32(insn, 16, 3);
6613 int rmode = extract32(insn, 19, 2);
6614 int type = extract32(insn, 22, 2);
6615 bool sbit = extract32(insn, 29, 1);
6616 bool sf = extract32(insn, 31, 1);
6617 bool itof = false;
6619 if (sbit) {
6620 goto do_unallocated;
6623 switch (opcode) {
6624 case 2: /* SCVTF */
6625 case 3: /* UCVTF */
6626 itof = true;
6627 /* fallthru */
6628 case 4: /* FCVTAS */
6629 case 5: /* FCVTAU */
6630 if (rmode != 0) {
6631 goto do_unallocated;
6633 /* fallthru */
6634 case 0: /* FCVT[NPMZ]S */
6635 case 1: /* FCVT[NPMZ]U */
6636 switch (type) {
6637 case 0: /* float32 */
6638 case 1: /* float64 */
6639 break;
6640 case 3: /* float16 */
6641 if (!dc_isar_feature(aa64_fp16, s)) {
6642 goto do_unallocated;
6644 break;
6645 default:
6646 goto do_unallocated;
6648 if (!fp_access_check(s)) {
6649 return;
6651 handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
6652 break;
6654 default:
6655 switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
6656 case 0b01100110: /* FMOV half <-> 32-bit int */
6657 case 0b01100111:
6658 case 0b11100110: /* FMOV half <-> 64-bit int */
6659 case 0b11100111:
6660 if (!dc_isar_feature(aa64_fp16, s)) {
6661 goto do_unallocated;
6663 /* fallthru */
6664 case 0b00000110: /* FMOV 32-bit */
6665 case 0b00000111:
6666 case 0b10100110: /* FMOV 64-bit */
6667 case 0b10100111:
6668 case 0b11001110: /* FMOV top half of 128-bit */
6669 case 0b11001111:
6670 if (!fp_access_check(s)) {
6671 return;
6673 itof = opcode & 1;
6674 handle_fmov(s, rd, rn, type, itof);
6675 break;
6677 case 0b00111110: /* FJCVTZS */
6678 if (!dc_isar_feature(aa64_jscvt, s)) {
6679 goto do_unallocated;
6680 } else if (fp_access_check(s)) {
6681 handle_fjcvtzs(s, rd, rn);
6683 break;
6685 default:
6686 do_unallocated:
6687 unallocated_encoding(s);
6688 return;
6690 break;
6694 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
6695 * 31 30 29 28 25 24 0
6696 * +---+---+---+---------+-----------------------------+
6697 * | | 0 | | 1 1 1 1 | |
6698 * +---+---+---+---------+-----------------------------+
6700 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
6702 if (extract32(insn, 24, 1)) {
6703 /* Floating point data-processing (3 source) */
6704 disas_fp_3src(s, insn);
6705 } else if (extract32(insn, 21, 1) == 0) {
6706 /* Floating point to fixed point conversions */
6707 disas_fp_fixed_conv(s, insn);
6708 } else {
6709 switch (extract32(insn, 10, 2)) {
6710 case 1:
6711 /* Floating point conditional compare */
6712 disas_fp_ccomp(s, insn);
6713 break;
6714 case 2:
6715 /* Floating point data-processing (2 source) */
6716 disas_fp_2src(s, insn);
6717 break;
6718 case 3:
6719 /* Floating point conditional select */
6720 disas_fp_csel(s, insn);
6721 break;
6722 case 0:
6723 switch (ctz32(extract32(insn, 12, 4))) {
6724 case 0: /* [15:12] == xxx1 */
6725 /* Floating point immediate */
6726 disas_fp_imm(s, insn);
6727 break;
6728 case 1: /* [15:12] == xx10 */
6729 /* Floating point compare */
6730 disas_fp_compare(s, insn);
6731 break;
6732 case 2: /* [15:12] == x100 */
6733 /* Floating point data-processing (1 source) */
6734 disas_fp_1src(s, insn);
6735 break;
6736 case 3: /* [15:12] == 1000 */
6737 unallocated_encoding(s);
6738 break;
6739 default: /* [15:12] == 0000 */
6740 /* Floating point <-> integer conversions */
6741 disas_fp_int_conv(s, insn);
6742 break;
6744 break;
6749 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
6750 int pos)
6752 /* Extract 64 bits from the middle of two concatenated 64 bit
6753 * vector register slices left:right. The extracted bits start
6754 * at 'pos' bits into the right (least significant) side.
6755 * We return the result in tcg_right, and guarantee not to
6756 * trash tcg_left.
6758 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
6759 assert(pos > 0 && pos < 64);
6761 tcg_gen_shri_i64(tcg_right, tcg_right, pos);
6762 tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
6763 tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
6765 tcg_temp_free_i64(tcg_tmp);
6768 /* EXT
6769 * 31 30 29 24 23 22 21 20 16 15 14 11 10 9 5 4 0
6770 * +---+---+-------------+-----+---+------+---+------+---+------+------+
6771 * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 | Rm | 0 | imm4 | 0 | Rn | Rd |
6772 * +---+---+-------------+-----+---+------+---+------+---+------+------+
6774 static void disas_simd_ext(DisasContext *s, uint32_t insn)
6776 int is_q = extract32(insn, 30, 1);
6777 int op2 = extract32(insn, 22, 2);
6778 int imm4 = extract32(insn, 11, 4);
6779 int rm = extract32(insn, 16, 5);
6780 int rn = extract32(insn, 5, 5);
6781 int rd = extract32(insn, 0, 5);
6782 int pos = imm4 << 3;
6783 TCGv_i64 tcg_resl, tcg_resh;
6785 if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
6786 unallocated_encoding(s);
6787 return;
6790 if (!fp_access_check(s)) {
6791 return;
6794 tcg_resh = tcg_temp_new_i64();
6795 tcg_resl = tcg_temp_new_i64();
6797 /* Vd gets bits starting at pos bits into Vm:Vn. This is
6798 * either extracting 128 bits from a 128:128 concatenation, or
6799 * extracting 64 bits from a 64:64 concatenation.
6801 if (!is_q) {
6802 read_vec_element(s, tcg_resl, rn, 0, MO_64);
6803 if (pos != 0) {
6804 read_vec_element(s, tcg_resh, rm, 0, MO_64);
6805 do_ext64(s, tcg_resh, tcg_resl, pos);
6807 tcg_gen_movi_i64(tcg_resh, 0);
6808 } else {
6809 TCGv_i64 tcg_hh;
6810 typedef struct {
6811 int reg;
6812 int elt;
6813 } EltPosns;
6814 EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
6815 EltPosns *elt = eltposns;
6817 if (pos >= 64) {
6818 elt++;
6819 pos -= 64;
6822 read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
6823 elt++;
6824 read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
6825 elt++;
6826 if (pos != 0) {
6827 do_ext64(s, tcg_resh, tcg_resl, pos);
6828 tcg_hh = tcg_temp_new_i64();
6829 read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
6830 do_ext64(s, tcg_hh, tcg_resh, pos);
6831 tcg_temp_free_i64(tcg_hh);
6835 write_vec_element(s, tcg_resl, rd, 0, MO_64);
6836 tcg_temp_free_i64(tcg_resl);
6837 write_vec_element(s, tcg_resh, rd, 1, MO_64);
6838 tcg_temp_free_i64(tcg_resh);
6841 /* TBL/TBX
6842 * 31 30 29 24 23 22 21 20 16 15 14 13 12 11 10 9 5 4 0
6843 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
6844 * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 | Rm | 0 | len | op | 0 0 | Rn | Rd |
6845 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
6847 static void disas_simd_tb(DisasContext *s, uint32_t insn)
6849 int op2 = extract32(insn, 22, 2);
6850 int is_q = extract32(insn, 30, 1);
6851 int rm = extract32(insn, 16, 5);
6852 int rn = extract32(insn, 5, 5);
6853 int rd = extract32(insn, 0, 5);
6854 int is_tblx = extract32(insn, 12, 1);
6855 int len = extract32(insn, 13, 2);
6856 TCGv_i64 tcg_resl, tcg_resh, tcg_idx;
6857 TCGv_i32 tcg_regno, tcg_numregs;
6859 if (op2 != 0) {
6860 unallocated_encoding(s);
6861 return;
6864 if (!fp_access_check(s)) {
6865 return;
6868 /* This does a table lookup: for every byte element in the input
6869 * we index into a table formed from up to four vector registers,
6870 * and then the output is the result of the lookups. Our helper
6871 * function does the lookup operation for a single 64 bit part of
6872 * the input.
6874 tcg_resl = tcg_temp_new_i64();
6875 tcg_resh = tcg_temp_new_i64();
6877 if (is_tblx) {
6878 read_vec_element(s, tcg_resl, rd, 0, MO_64);
6879 } else {
6880 tcg_gen_movi_i64(tcg_resl, 0);
6882 if (is_tblx && is_q) {
6883 read_vec_element(s, tcg_resh, rd, 1, MO_64);
6884 } else {
6885 tcg_gen_movi_i64(tcg_resh, 0);
6888 tcg_idx = tcg_temp_new_i64();
6889 tcg_regno = tcg_const_i32(rn);
6890 tcg_numregs = tcg_const_i32(len + 1);
6891 read_vec_element(s, tcg_idx, rm, 0, MO_64);
6892 gen_helper_simd_tbl(tcg_resl, cpu_env, tcg_resl, tcg_idx,
6893 tcg_regno, tcg_numregs);
6894 if (is_q) {
6895 read_vec_element(s, tcg_idx, rm, 1, MO_64);
6896 gen_helper_simd_tbl(tcg_resh, cpu_env, tcg_resh, tcg_idx,
6897 tcg_regno, tcg_numregs);
6899 tcg_temp_free_i64(tcg_idx);
6900 tcg_temp_free_i32(tcg_regno);
6901 tcg_temp_free_i32(tcg_numregs);
6903 write_vec_element(s, tcg_resl, rd, 0, MO_64);
6904 tcg_temp_free_i64(tcg_resl);
6905 write_vec_element(s, tcg_resh, rd, 1, MO_64);
6906 tcg_temp_free_i64(tcg_resh);
6909 /* ZIP/UZP/TRN
6910 * 31 30 29 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
6911 * +---+---+-------------+------+---+------+---+------------------+------+
6912 * | 0 | Q | 0 0 1 1 1 0 | size | 0 | Rm | 0 | opc | 1 0 | Rn | Rd |
6913 * +---+---+-------------+------+---+------+---+------------------+------+
6915 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
6917 int rd = extract32(insn, 0, 5);
6918 int rn = extract32(insn, 5, 5);
6919 int rm = extract32(insn, 16, 5);
6920 int size = extract32(insn, 22, 2);
6921 /* opc field bits [1:0] indicate ZIP/UZP/TRN;
6922 * bit 2 indicates 1 vs 2 variant of the insn.
6924 int opcode = extract32(insn, 12, 2);
6925 bool part = extract32(insn, 14, 1);
6926 bool is_q = extract32(insn, 30, 1);
6927 int esize = 8 << size;
6928 int i, ofs;
6929 int datasize = is_q ? 128 : 64;
6930 int elements = datasize / esize;
6931 TCGv_i64 tcg_res, tcg_resl, tcg_resh;
6933 if (opcode == 0 || (size == 3 && !is_q)) {
6934 unallocated_encoding(s);
6935 return;
6938 if (!fp_access_check(s)) {
6939 return;
6942 tcg_resl = tcg_const_i64(0);
6943 tcg_resh = tcg_const_i64(0);
6944 tcg_res = tcg_temp_new_i64();
6946 for (i = 0; i < elements; i++) {
6947 switch (opcode) {
6948 case 1: /* UZP1/2 */
6950 int midpoint = elements / 2;
6951 if (i < midpoint) {
6952 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
6953 } else {
6954 read_vec_element(s, tcg_res, rm,
6955 2 * (i - midpoint) + part, size);
6957 break;
6959 case 2: /* TRN1/2 */
6960 if (i & 1) {
6961 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
6962 } else {
6963 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
6965 break;
6966 case 3: /* ZIP1/2 */
6968 int base = part * elements / 2;
6969 if (i & 1) {
6970 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
6971 } else {
6972 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
6974 break;
6976 default:
6977 g_assert_not_reached();
6980 ofs = i * esize;
6981 if (ofs < 64) {
6982 tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
6983 tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
6984 } else {
6985 tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
6986 tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
6990 tcg_temp_free_i64(tcg_res);
6992 write_vec_element(s, tcg_resl, rd, 0, MO_64);
6993 tcg_temp_free_i64(tcg_resl);
6994 write_vec_element(s, tcg_resh, rd, 1, MO_64);
6995 tcg_temp_free_i64(tcg_resh);
6999 * do_reduction_op helper
7001 * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7002 * important for correct NaN propagation that we do these
7003 * operations in exactly the order specified by the pseudocode.
7005 * This is a recursive function, TCG temps should be freed by the
7006 * calling function once it is done with the values.
7008 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7009 int esize, int size, int vmap, TCGv_ptr fpst)
7011 if (esize == size) {
7012 int element;
7013 TCGMemOp msize = esize == 16 ? MO_16 : MO_32;
7014 TCGv_i32 tcg_elem;
7016 /* We should have one register left here */
7017 assert(ctpop8(vmap) == 1);
7018 element = ctz32(vmap);
7019 assert(element < 8);
7021 tcg_elem = tcg_temp_new_i32();
7022 read_vec_element_i32(s, tcg_elem, rn, element, msize);
7023 return tcg_elem;
7024 } else {
7025 int bits = size / 2;
7026 int shift = ctpop8(vmap) / 2;
7027 int vmap_lo = (vmap >> shift) & vmap;
7028 int vmap_hi = (vmap & ~vmap_lo);
7029 TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7031 tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7032 tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7033 tcg_res = tcg_temp_new_i32();
7035 switch (fpopcode) {
7036 case 0x0c: /* fmaxnmv half-precision */
7037 gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7038 break;
7039 case 0x0f: /* fmaxv half-precision */
7040 gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7041 break;
7042 case 0x1c: /* fminnmv half-precision */
7043 gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7044 break;
7045 case 0x1f: /* fminv half-precision */
7046 gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7047 break;
7048 case 0x2c: /* fmaxnmv */
7049 gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7050 break;
7051 case 0x2f: /* fmaxv */
7052 gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7053 break;
7054 case 0x3c: /* fminnmv */
7055 gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7056 break;
7057 case 0x3f: /* fminv */
7058 gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7059 break;
7060 default:
7061 g_assert_not_reached();
7064 tcg_temp_free_i32(tcg_hi);
7065 tcg_temp_free_i32(tcg_lo);
7066 return tcg_res;
7070 /* AdvSIMD across lanes
7071 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
7072 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7073 * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
7074 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7076 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7078 int rd = extract32(insn, 0, 5);
7079 int rn = extract32(insn, 5, 5);
7080 int size = extract32(insn, 22, 2);
7081 int opcode = extract32(insn, 12, 5);
7082 bool is_q = extract32(insn, 30, 1);
7083 bool is_u = extract32(insn, 29, 1);
7084 bool is_fp = false;
7085 bool is_min = false;
7086 int esize;
7087 int elements;
7088 int i;
7089 TCGv_i64 tcg_res, tcg_elt;
7091 switch (opcode) {
7092 case 0x1b: /* ADDV */
7093 if (is_u) {
7094 unallocated_encoding(s);
7095 return;
7097 /* fall through */
7098 case 0x3: /* SADDLV, UADDLV */
7099 case 0xa: /* SMAXV, UMAXV */
7100 case 0x1a: /* SMINV, UMINV */
7101 if (size == 3 || (size == 2 && !is_q)) {
7102 unallocated_encoding(s);
7103 return;
7105 break;
7106 case 0xc: /* FMAXNMV, FMINNMV */
7107 case 0xf: /* FMAXV, FMINV */
7108 /* Bit 1 of size field encodes min vs max and the actual size
7109 * depends on the encoding of the U bit. If not set (and FP16
7110 * enabled) then we do half-precision float instead of single
7111 * precision.
7113 is_min = extract32(size, 1, 1);
7114 is_fp = true;
7115 if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7116 size = 1;
7117 } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7118 unallocated_encoding(s);
7119 return;
7120 } else {
7121 size = 2;
7123 break;
7124 default:
7125 unallocated_encoding(s);
7126 return;
7129 if (!fp_access_check(s)) {
7130 return;
7133 esize = 8 << size;
7134 elements = (is_q ? 128 : 64) / esize;
7136 tcg_res = tcg_temp_new_i64();
7137 tcg_elt = tcg_temp_new_i64();
7139 /* These instructions operate across all lanes of a vector
7140 * to produce a single result. We can guarantee that a 64
7141 * bit intermediate is sufficient:
7142 * + for [US]ADDLV the maximum element size is 32 bits, and
7143 * the result type is 64 bits
7144 * + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7145 * same as the element size, which is 32 bits at most
7146 * For the integer operations we can choose to work at 64
7147 * or 32 bits and truncate at the end; for simplicity
7148 * we use 64 bits always. The floating point
7149 * ops do require 32 bit intermediates, though.
7151 if (!is_fp) {
7152 read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7154 for (i = 1; i < elements; i++) {
7155 read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7157 switch (opcode) {
7158 case 0x03: /* SADDLV / UADDLV */
7159 case 0x1b: /* ADDV */
7160 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7161 break;
7162 case 0x0a: /* SMAXV / UMAXV */
7163 if (is_u) {
7164 tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7165 } else {
7166 tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7168 break;
7169 case 0x1a: /* SMINV / UMINV */
7170 if (is_u) {
7171 tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7172 } else {
7173 tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7175 break;
7176 default:
7177 g_assert_not_reached();
7181 } else {
7182 /* Floating point vector reduction ops which work across 32
7183 * bit (single) or 16 bit (half-precision) intermediates.
7184 * Note that correct NaN propagation requires that we do these
7185 * operations in exactly the order specified by the pseudocode.
7187 TCGv_ptr fpst = get_fpstatus_ptr(size == MO_16);
7188 int fpopcode = opcode | is_min << 4 | is_u << 5;
7189 int vmap = (1 << elements) - 1;
7190 TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7191 (is_q ? 128 : 64), vmap, fpst);
7192 tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7193 tcg_temp_free_i32(tcg_res32);
7194 tcg_temp_free_ptr(fpst);
7197 tcg_temp_free_i64(tcg_elt);
7199 /* Now truncate the result to the width required for the final output */
7200 if (opcode == 0x03) {
7201 /* SADDLV, UADDLV: result is 2*esize */
7202 size++;
7205 switch (size) {
7206 case 0:
7207 tcg_gen_ext8u_i64(tcg_res, tcg_res);
7208 break;
7209 case 1:
7210 tcg_gen_ext16u_i64(tcg_res, tcg_res);
7211 break;
7212 case 2:
7213 tcg_gen_ext32u_i64(tcg_res, tcg_res);
7214 break;
7215 case 3:
7216 break;
7217 default:
7218 g_assert_not_reached();
7221 write_fp_dreg(s, rd, tcg_res);
7222 tcg_temp_free_i64(tcg_res);
7225 /* DUP (Element, Vector)
7227 * 31 30 29 21 20 16 15 10 9 5 4 0
7228 * +---+---+-------------------+--------+-------------+------+------+
7229 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
7230 * +---+---+-------------------+--------+-------------+------+------+
7232 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7234 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7235 int imm5)
7237 int size = ctz32(imm5);
7238 int index = imm5 >> (size + 1);
7240 if (size > 3 || (size == 3 && !is_q)) {
7241 unallocated_encoding(s);
7242 return;
7245 if (!fp_access_check(s)) {
7246 return;
7249 tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7250 vec_reg_offset(s, rn, index, size),
7251 is_q ? 16 : 8, vec_full_reg_size(s));
7254 /* DUP (element, scalar)
7255 * 31 21 20 16 15 10 9 5 4 0
7256 * +-----------------------+--------+-------------+------+------+
7257 * | 0 1 0 1 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
7258 * +-----------------------+--------+-------------+------+------+
7260 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7261 int imm5)
7263 int size = ctz32(imm5);
7264 int index;
7265 TCGv_i64 tmp;
7267 if (size > 3) {
7268 unallocated_encoding(s);
7269 return;
7272 if (!fp_access_check(s)) {
7273 return;
7276 index = imm5 >> (size + 1);
7278 /* This instruction just extracts the specified element and
7279 * zero-extends it into the bottom of the destination register.
7281 tmp = tcg_temp_new_i64();
7282 read_vec_element(s, tmp, rn, index, size);
7283 write_fp_dreg(s, rd, tmp);
7284 tcg_temp_free_i64(tmp);
7287 /* DUP (General)
7289 * 31 30 29 21 20 16 15 10 9 5 4 0
7290 * +---+---+-------------------+--------+-------------+------+------+
7291 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 1 1 | Rn | Rd |
7292 * +---+---+-------------------+--------+-------------+------+------+
7294 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7296 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7297 int imm5)
7299 int size = ctz32(imm5);
7300 uint32_t dofs, oprsz, maxsz;
7302 if (size > 3 || ((size == 3) && !is_q)) {
7303 unallocated_encoding(s);
7304 return;
7307 if (!fp_access_check(s)) {
7308 return;
7311 dofs = vec_full_reg_offset(s, rd);
7312 oprsz = is_q ? 16 : 8;
7313 maxsz = vec_full_reg_size(s);
7315 tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7318 /* INS (Element)
7320 * 31 21 20 16 15 14 11 10 9 5 4 0
7321 * +-----------------------+--------+------------+---+------+------+
7322 * | 0 1 1 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
7323 * +-----------------------+--------+------------+---+------+------+
7325 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7326 * index: encoded in imm5<4:size+1>
7328 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7329 int imm4, int imm5)
7331 int size = ctz32(imm5);
7332 int src_index, dst_index;
7333 TCGv_i64 tmp;
7335 if (size > 3) {
7336 unallocated_encoding(s);
7337 return;
7340 if (!fp_access_check(s)) {
7341 return;
7344 dst_index = extract32(imm5, 1+size, 5);
7345 src_index = extract32(imm4, size, 4);
7347 tmp = tcg_temp_new_i64();
7349 read_vec_element(s, tmp, rn, src_index, size);
7350 write_vec_element(s, tmp, rd, dst_index, size);
7352 tcg_temp_free_i64(tmp);
7356 /* INS (General)
7358 * 31 21 20 16 15 10 9 5 4 0
7359 * +-----------------------+--------+-------------+------+------+
7360 * | 0 1 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 1 1 1 | Rn | Rd |
7361 * +-----------------------+--------+-------------+------+------+
7363 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7364 * index: encoded in imm5<4:size+1>
7366 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7368 int size = ctz32(imm5);
7369 int idx;
7371 if (size > 3) {
7372 unallocated_encoding(s);
7373 return;
7376 if (!fp_access_check(s)) {
7377 return;
7380 idx = extract32(imm5, 1 + size, 4 - size);
7381 write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7385 * UMOV (General)
7386 * SMOV (General)
7388 * 31 30 29 21 20 16 15 12 10 9 5 4 0
7389 * +---+---+-------------------+--------+-------------+------+------+
7390 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 1 U 1 1 | Rn | Rd |
7391 * +---+---+-------------------+--------+-------------+------+------+
7393 * U: unsigned when set
7394 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7396 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7397 int rn, int rd, int imm5)
7399 int size = ctz32(imm5);
7400 int element;
7401 TCGv_i64 tcg_rd;
7403 /* Check for UnallocatedEncodings */
7404 if (is_signed) {
7405 if (size > 2 || (size == 2 && !is_q)) {
7406 unallocated_encoding(s);
7407 return;
7409 } else {
7410 if (size > 3
7411 || (size < 3 && is_q)
7412 || (size == 3 && !is_q)) {
7413 unallocated_encoding(s);
7414 return;
7418 if (!fp_access_check(s)) {
7419 return;
7422 element = extract32(imm5, 1+size, 4);
7424 tcg_rd = cpu_reg(s, rd);
7425 read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7426 if (is_signed && !is_q) {
7427 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7431 /* AdvSIMD copy
7432 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
7433 * +---+---+----+-----------------+------+---+------+---+------+------+
7434 * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
7435 * +---+---+----+-----------------+------+---+------+---+------+------+
7437 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7439 int rd = extract32(insn, 0, 5);
7440 int rn = extract32(insn, 5, 5);
7441 int imm4 = extract32(insn, 11, 4);
7442 int op = extract32(insn, 29, 1);
7443 int is_q = extract32(insn, 30, 1);
7444 int imm5 = extract32(insn, 16, 5);
7446 if (op) {
7447 if (is_q) {
7448 /* INS (element) */
7449 handle_simd_inse(s, rd, rn, imm4, imm5);
7450 } else {
7451 unallocated_encoding(s);
7453 } else {
7454 switch (imm4) {
7455 case 0:
7456 /* DUP (element - vector) */
7457 handle_simd_dupe(s, is_q, rd, rn, imm5);
7458 break;
7459 case 1:
7460 /* DUP (general) */
7461 handle_simd_dupg(s, is_q, rd, rn, imm5);
7462 break;
7463 case 3:
7464 if (is_q) {
7465 /* INS (general) */
7466 handle_simd_insg(s, rd, rn, imm5);
7467 } else {
7468 unallocated_encoding(s);
7470 break;
7471 case 5:
7472 case 7:
7473 /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
7474 handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
7475 break;
7476 default:
7477 unallocated_encoding(s);
7478 break;
7483 /* AdvSIMD modified immediate
7484 * 31 30 29 28 19 18 16 15 12 11 10 9 5 4 0
7485 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7486 * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh | Rd |
7487 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7489 * There are a number of operations that can be carried out here:
7490 * MOVI - move (shifted) imm into register
7491 * MVNI - move inverted (shifted) imm into register
7492 * ORR - bitwise OR of (shifted) imm with register
7493 * BIC - bitwise clear of (shifted) imm with register
7494 * With ARMv8.2 we also have:
7495 * FMOV half-precision
7497 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
7499 int rd = extract32(insn, 0, 5);
7500 int cmode = extract32(insn, 12, 4);
7501 int cmode_3_1 = extract32(cmode, 1, 3);
7502 int cmode_0 = extract32(cmode, 0, 1);
7503 int o2 = extract32(insn, 11, 1);
7504 uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
7505 bool is_neg = extract32(insn, 29, 1);
7506 bool is_q = extract32(insn, 30, 1);
7507 uint64_t imm = 0;
7509 if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
7510 /* Check for FMOV (vector, immediate) - half-precision */
7511 if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
7512 unallocated_encoding(s);
7513 return;
7517 if (!fp_access_check(s)) {
7518 return;
7521 /* See AdvSIMDExpandImm() in ARM ARM */
7522 switch (cmode_3_1) {
7523 case 0: /* Replicate(Zeros(24):imm8, 2) */
7524 case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
7525 case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
7526 case 3: /* Replicate(imm8:Zeros(24), 2) */
7528 int shift = cmode_3_1 * 8;
7529 imm = bitfield_replicate(abcdefgh << shift, 32);
7530 break;
7532 case 4: /* Replicate(Zeros(8):imm8, 4) */
7533 case 5: /* Replicate(imm8:Zeros(8), 4) */
7535 int shift = (cmode_3_1 & 0x1) * 8;
7536 imm = bitfield_replicate(abcdefgh << shift, 16);
7537 break;
7539 case 6:
7540 if (cmode_0) {
7541 /* Replicate(Zeros(8):imm8:Ones(16), 2) */
7542 imm = (abcdefgh << 16) | 0xffff;
7543 } else {
7544 /* Replicate(Zeros(16):imm8:Ones(8), 2) */
7545 imm = (abcdefgh << 8) | 0xff;
7547 imm = bitfield_replicate(imm, 32);
7548 break;
7549 case 7:
7550 if (!cmode_0 && !is_neg) {
7551 imm = bitfield_replicate(abcdefgh, 8);
7552 } else if (!cmode_0 && is_neg) {
7553 int i;
7554 imm = 0;
7555 for (i = 0; i < 8; i++) {
7556 if ((abcdefgh) & (1 << i)) {
7557 imm |= 0xffULL << (i * 8);
7560 } else if (cmode_0) {
7561 if (is_neg) {
7562 imm = (abcdefgh & 0x3f) << 48;
7563 if (abcdefgh & 0x80) {
7564 imm |= 0x8000000000000000ULL;
7566 if (abcdefgh & 0x40) {
7567 imm |= 0x3fc0000000000000ULL;
7568 } else {
7569 imm |= 0x4000000000000000ULL;
7571 } else {
7572 if (o2) {
7573 /* FMOV (vector, immediate) - half-precision */
7574 imm = vfp_expand_imm(MO_16, abcdefgh);
7575 /* now duplicate across the lanes */
7576 imm = bitfield_replicate(imm, 16);
7577 } else {
7578 imm = (abcdefgh & 0x3f) << 19;
7579 if (abcdefgh & 0x80) {
7580 imm |= 0x80000000;
7582 if (abcdefgh & 0x40) {
7583 imm |= 0x3e000000;
7584 } else {
7585 imm |= 0x40000000;
7587 imm |= (imm << 32);
7591 break;
7592 default:
7593 fprintf(stderr, "%s: cmode_3_1: %x\n", __func__, cmode_3_1);
7594 g_assert_not_reached();
7597 if (cmode_3_1 != 7 && is_neg) {
7598 imm = ~imm;
7601 if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
7602 /* MOVI or MVNI, with MVNI negation handled above. */
7603 tcg_gen_gvec_dup64i(vec_full_reg_offset(s, rd), is_q ? 16 : 8,
7604 vec_full_reg_size(s), imm);
7605 } else {
7606 /* ORR or BIC, with BIC negation to AND handled above. */
7607 if (is_neg) {
7608 gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
7609 } else {
7610 gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
7615 /* AdvSIMD scalar copy
7616 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
7617 * +-----+----+-----------------+------+---+------+---+------+------+
7618 * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
7619 * +-----+----+-----------------+------+---+------+---+------+------+
7621 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
7623 int rd = extract32(insn, 0, 5);
7624 int rn = extract32(insn, 5, 5);
7625 int imm4 = extract32(insn, 11, 4);
7626 int imm5 = extract32(insn, 16, 5);
7627 int op = extract32(insn, 29, 1);
7629 if (op != 0 || imm4 != 0) {
7630 unallocated_encoding(s);
7631 return;
7634 /* DUP (element, scalar) */
7635 handle_simd_dupes(s, rd, rn, imm5);
7638 /* AdvSIMD scalar pairwise
7639 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
7640 * +-----+---+-----------+------+-----------+--------+-----+------+------+
7641 * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
7642 * +-----+---+-----------+------+-----------+--------+-----+------+------+
7644 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
7646 int u = extract32(insn, 29, 1);
7647 int size = extract32(insn, 22, 2);
7648 int opcode = extract32(insn, 12, 5);
7649 int rn = extract32(insn, 5, 5);
7650 int rd = extract32(insn, 0, 5);
7651 TCGv_ptr fpst;
7653 /* For some ops (the FP ones), size[1] is part of the encoding.
7654 * For ADDP strictly it is not but size[1] is always 1 for valid
7655 * encodings.
7657 opcode |= (extract32(size, 1, 1) << 5);
7659 switch (opcode) {
7660 case 0x3b: /* ADDP */
7661 if (u || size != 3) {
7662 unallocated_encoding(s);
7663 return;
7665 if (!fp_access_check(s)) {
7666 return;
7669 fpst = NULL;
7670 break;
7671 case 0xc: /* FMAXNMP */
7672 case 0xd: /* FADDP */
7673 case 0xf: /* FMAXP */
7674 case 0x2c: /* FMINNMP */
7675 case 0x2f: /* FMINP */
7676 /* FP op, size[0] is 32 or 64 bit*/
7677 if (!u) {
7678 if (!dc_isar_feature(aa64_fp16, s)) {
7679 unallocated_encoding(s);
7680 return;
7681 } else {
7682 size = MO_16;
7684 } else {
7685 size = extract32(size, 0, 1) ? MO_64 : MO_32;
7688 if (!fp_access_check(s)) {
7689 return;
7692 fpst = get_fpstatus_ptr(size == MO_16);
7693 break;
7694 default:
7695 unallocated_encoding(s);
7696 return;
7699 if (size == MO_64) {
7700 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7701 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7702 TCGv_i64 tcg_res = tcg_temp_new_i64();
7704 read_vec_element(s, tcg_op1, rn, 0, MO_64);
7705 read_vec_element(s, tcg_op2, rn, 1, MO_64);
7707 switch (opcode) {
7708 case 0x3b: /* ADDP */
7709 tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
7710 break;
7711 case 0xc: /* FMAXNMP */
7712 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7713 break;
7714 case 0xd: /* FADDP */
7715 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
7716 break;
7717 case 0xf: /* FMAXP */
7718 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
7719 break;
7720 case 0x2c: /* FMINNMP */
7721 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7722 break;
7723 case 0x2f: /* FMINP */
7724 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
7725 break;
7726 default:
7727 g_assert_not_reached();
7730 write_fp_dreg(s, rd, tcg_res);
7732 tcg_temp_free_i64(tcg_op1);
7733 tcg_temp_free_i64(tcg_op2);
7734 tcg_temp_free_i64(tcg_res);
7735 } else {
7736 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7737 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7738 TCGv_i32 tcg_res = tcg_temp_new_i32();
7740 read_vec_element_i32(s, tcg_op1, rn, 0, size);
7741 read_vec_element_i32(s, tcg_op2, rn, 1, size);
7743 if (size == MO_16) {
7744 switch (opcode) {
7745 case 0xc: /* FMAXNMP */
7746 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
7747 break;
7748 case 0xd: /* FADDP */
7749 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
7750 break;
7751 case 0xf: /* FMAXP */
7752 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
7753 break;
7754 case 0x2c: /* FMINNMP */
7755 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
7756 break;
7757 case 0x2f: /* FMINP */
7758 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
7759 break;
7760 default:
7761 g_assert_not_reached();
7763 } else {
7764 switch (opcode) {
7765 case 0xc: /* FMAXNMP */
7766 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
7767 break;
7768 case 0xd: /* FADDP */
7769 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
7770 break;
7771 case 0xf: /* FMAXP */
7772 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
7773 break;
7774 case 0x2c: /* FMINNMP */
7775 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
7776 break;
7777 case 0x2f: /* FMINP */
7778 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
7779 break;
7780 default:
7781 g_assert_not_reached();
7785 write_fp_sreg(s, rd, tcg_res);
7787 tcg_temp_free_i32(tcg_op1);
7788 tcg_temp_free_i32(tcg_op2);
7789 tcg_temp_free_i32(tcg_res);
7792 if (fpst) {
7793 tcg_temp_free_ptr(fpst);
7798 * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
7800 * This code is handles the common shifting code and is used by both
7801 * the vector and scalar code.
7803 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
7804 TCGv_i64 tcg_rnd, bool accumulate,
7805 bool is_u, int size, int shift)
7807 bool extended_result = false;
7808 bool round = tcg_rnd != NULL;
7809 int ext_lshift = 0;
7810 TCGv_i64 tcg_src_hi;
7812 if (round && size == 3) {
7813 extended_result = true;
7814 ext_lshift = 64 - shift;
7815 tcg_src_hi = tcg_temp_new_i64();
7816 } else if (shift == 64) {
7817 if (!accumulate && is_u) {
7818 /* result is zero */
7819 tcg_gen_movi_i64(tcg_res, 0);
7820 return;
7824 /* Deal with the rounding step */
7825 if (round) {
7826 if (extended_result) {
7827 TCGv_i64 tcg_zero = tcg_const_i64(0);
7828 if (!is_u) {
7829 /* take care of sign extending tcg_res */
7830 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
7831 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
7832 tcg_src, tcg_src_hi,
7833 tcg_rnd, tcg_zero);
7834 } else {
7835 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
7836 tcg_src, tcg_zero,
7837 tcg_rnd, tcg_zero);
7839 tcg_temp_free_i64(tcg_zero);
7840 } else {
7841 tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
7845 /* Now do the shift right */
7846 if (round && extended_result) {
7847 /* extended case, >64 bit precision required */
7848 if (ext_lshift == 0) {
7849 /* special case, only high bits matter */
7850 tcg_gen_mov_i64(tcg_src, tcg_src_hi);
7851 } else {
7852 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
7853 tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
7854 tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
7856 } else {
7857 if (is_u) {
7858 if (shift == 64) {
7859 /* essentially shifting in 64 zeros */
7860 tcg_gen_movi_i64(tcg_src, 0);
7861 } else {
7862 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
7864 } else {
7865 if (shift == 64) {
7866 /* effectively extending the sign-bit */
7867 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
7868 } else {
7869 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
7874 if (accumulate) {
7875 tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
7876 } else {
7877 tcg_gen_mov_i64(tcg_res, tcg_src);
7880 if (extended_result) {
7881 tcg_temp_free_i64(tcg_src_hi);
7885 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
7886 static void handle_scalar_simd_shri(DisasContext *s,
7887 bool is_u, int immh, int immb,
7888 int opcode, int rn, int rd)
7890 const int size = 3;
7891 int immhb = immh << 3 | immb;
7892 int shift = 2 * (8 << size) - immhb;
7893 bool accumulate = false;
7894 bool round = false;
7895 bool insert = false;
7896 TCGv_i64 tcg_rn;
7897 TCGv_i64 tcg_rd;
7898 TCGv_i64 tcg_round;
7900 if (!extract32(immh, 3, 1)) {
7901 unallocated_encoding(s);
7902 return;
7905 if (!fp_access_check(s)) {
7906 return;
7909 switch (opcode) {
7910 case 0x02: /* SSRA / USRA (accumulate) */
7911 accumulate = true;
7912 break;
7913 case 0x04: /* SRSHR / URSHR (rounding) */
7914 round = true;
7915 break;
7916 case 0x06: /* SRSRA / URSRA (accum + rounding) */
7917 accumulate = round = true;
7918 break;
7919 case 0x08: /* SRI */
7920 insert = true;
7921 break;
7924 if (round) {
7925 uint64_t round_const = 1ULL << (shift - 1);
7926 tcg_round = tcg_const_i64(round_const);
7927 } else {
7928 tcg_round = NULL;
7931 tcg_rn = read_fp_dreg(s, rn);
7932 tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
7934 if (insert) {
7935 /* shift count same as element size is valid but does nothing;
7936 * special case to avoid potential shift by 64.
7938 int esize = 8 << size;
7939 if (shift != esize) {
7940 tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
7941 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
7943 } else {
7944 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
7945 accumulate, is_u, size, shift);
7948 write_fp_dreg(s, rd, tcg_rd);
7950 tcg_temp_free_i64(tcg_rn);
7951 tcg_temp_free_i64(tcg_rd);
7952 if (round) {
7953 tcg_temp_free_i64(tcg_round);
7957 /* SHL/SLI - Scalar shift left */
7958 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
7959 int immh, int immb, int opcode,
7960 int rn, int rd)
7962 int size = 32 - clz32(immh) - 1;
7963 int immhb = immh << 3 | immb;
7964 int shift = immhb - (8 << size);
7965 TCGv_i64 tcg_rn = new_tmp_a64(s);
7966 TCGv_i64 tcg_rd = new_tmp_a64(s);
7968 if (!extract32(immh, 3, 1)) {
7969 unallocated_encoding(s);
7970 return;
7973 if (!fp_access_check(s)) {
7974 return;
7977 tcg_rn = read_fp_dreg(s, rn);
7978 tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
7980 if (insert) {
7981 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
7982 } else {
7983 tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
7986 write_fp_dreg(s, rd, tcg_rd);
7988 tcg_temp_free_i64(tcg_rn);
7989 tcg_temp_free_i64(tcg_rd);
7992 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
7993 * (signed/unsigned) narrowing */
7994 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
7995 bool is_u_shift, bool is_u_narrow,
7996 int immh, int immb, int opcode,
7997 int rn, int rd)
7999 int immhb = immh << 3 | immb;
8000 int size = 32 - clz32(immh) - 1;
8001 int esize = 8 << size;
8002 int shift = (2 * esize) - immhb;
8003 int elements = is_scalar ? 1 : (64 / esize);
8004 bool round = extract32(opcode, 0, 1);
8005 TCGMemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8006 TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8007 TCGv_i32 tcg_rd_narrowed;
8008 TCGv_i64 tcg_final;
8010 static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8011 { gen_helper_neon_narrow_sat_s8,
8012 gen_helper_neon_unarrow_sat8 },
8013 { gen_helper_neon_narrow_sat_s16,
8014 gen_helper_neon_unarrow_sat16 },
8015 { gen_helper_neon_narrow_sat_s32,
8016 gen_helper_neon_unarrow_sat32 },
8017 { NULL, NULL },
8019 static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8020 gen_helper_neon_narrow_sat_u8,
8021 gen_helper_neon_narrow_sat_u16,
8022 gen_helper_neon_narrow_sat_u32,
8023 NULL
8025 NeonGenNarrowEnvFn *narrowfn;
8027 int i;
8029 assert(size < 4);
8031 if (extract32(immh, 3, 1)) {
8032 unallocated_encoding(s);
8033 return;
8036 if (!fp_access_check(s)) {
8037 return;
8040 if (is_u_shift) {
8041 narrowfn = unsigned_narrow_fns[size];
8042 } else {
8043 narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8046 tcg_rn = tcg_temp_new_i64();
8047 tcg_rd = tcg_temp_new_i64();
8048 tcg_rd_narrowed = tcg_temp_new_i32();
8049 tcg_final = tcg_const_i64(0);
8051 if (round) {
8052 uint64_t round_const = 1ULL << (shift - 1);
8053 tcg_round = tcg_const_i64(round_const);
8054 } else {
8055 tcg_round = NULL;
8058 for (i = 0; i < elements; i++) {
8059 read_vec_element(s, tcg_rn, rn, i, ldop);
8060 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8061 false, is_u_shift, size+1, shift);
8062 narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8063 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8064 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8067 if (!is_q) {
8068 write_vec_element(s, tcg_final, rd, 0, MO_64);
8069 } else {
8070 write_vec_element(s, tcg_final, rd, 1, MO_64);
8073 if (round) {
8074 tcg_temp_free_i64(tcg_round);
8076 tcg_temp_free_i64(tcg_rn);
8077 tcg_temp_free_i64(tcg_rd);
8078 tcg_temp_free_i32(tcg_rd_narrowed);
8079 tcg_temp_free_i64(tcg_final);
8081 clear_vec_high(s, is_q, rd);
8084 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8085 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8086 bool src_unsigned, bool dst_unsigned,
8087 int immh, int immb, int rn, int rd)
8089 int immhb = immh << 3 | immb;
8090 int size = 32 - clz32(immh) - 1;
8091 int shift = immhb - (8 << size);
8092 int pass;
8094 assert(immh != 0);
8095 assert(!(scalar && is_q));
8097 if (!scalar) {
8098 if (!is_q && extract32(immh, 3, 1)) {
8099 unallocated_encoding(s);
8100 return;
8103 /* Since we use the variable-shift helpers we must
8104 * replicate the shift count into each element of
8105 * the tcg_shift value.
8107 switch (size) {
8108 case 0:
8109 shift |= shift << 8;
8110 /* fall through */
8111 case 1:
8112 shift |= shift << 16;
8113 break;
8114 case 2:
8115 case 3:
8116 break;
8117 default:
8118 g_assert_not_reached();
8122 if (!fp_access_check(s)) {
8123 return;
8126 if (size == 3) {
8127 TCGv_i64 tcg_shift = tcg_const_i64(shift);
8128 static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8129 { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8130 { NULL, gen_helper_neon_qshl_u64 },
8132 NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8133 int maxpass = is_q ? 2 : 1;
8135 for (pass = 0; pass < maxpass; pass++) {
8136 TCGv_i64 tcg_op = tcg_temp_new_i64();
8138 read_vec_element(s, tcg_op, rn, pass, MO_64);
8139 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8140 write_vec_element(s, tcg_op, rd, pass, MO_64);
8142 tcg_temp_free_i64(tcg_op);
8144 tcg_temp_free_i64(tcg_shift);
8145 clear_vec_high(s, is_q, rd);
8146 } else {
8147 TCGv_i32 tcg_shift = tcg_const_i32(shift);
8148 static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8150 { gen_helper_neon_qshl_s8,
8151 gen_helper_neon_qshl_s16,
8152 gen_helper_neon_qshl_s32 },
8153 { gen_helper_neon_qshlu_s8,
8154 gen_helper_neon_qshlu_s16,
8155 gen_helper_neon_qshlu_s32 }
8156 }, {
8157 { NULL, NULL, NULL },
8158 { gen_helper_neon_qshl_u8,
8159 gen_helper_neon_qshl_u16,
8160 gen_helper_neon_qshl_u32 }
8163 NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8164 TCGMemOp memop = scalar ? size : MO_32;
8165 int maxpass = scalar ? 1 : is_q ? 4 : 2;
8167 for (pass = 0; pass < maxpass; pass++) {
8168 TCGv_i32 tcg_op = tcg_temp_new_i32();
8170 read_vec_element_i32(s, tcg_op, rn, pass, memop);
8171 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8172 if (scalar) {
8173 switch (size) {
8174 case 0:
8175 tcg_gen_ext8u_i32(tcg_op, tcg_op);
8176 break;
8177 case 1:
8178 tcg_gen_ext16u_i32(tcg_op, tcg_op);
8179 break;
8180 case 2:
8181 break;
8182 default:
8183 g_assert_not_reached();
8185 write_fp_sreg(s, rd, tcg_op);
8186 } else {
8187 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8190 tcg_temp_free_i32(tcg_op);
8192 tcg_temp_free_i32(tcg_shift);
8194 if (!scalar) {
8195 clear_vec_high(s, is_q, rd);
8200 /* Common vector code for handling integer to FP conversion */
8201 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8202 int elements, int is_signed,
8203 int fracbits, int size)
8205 TCGv_ptr tcg_fpst = get_fpstatus_ptr(size == MO_16);
8206 TCGv_i32 tcg_shift = NULL;
8208 TCGMemOp mop = size | (is_signed ? MO_SIGN : 0);
8209 int pass;
8211 if (fracbits || size == MO_64) {
8212 tcg_shift = tcg_const_i32(fracbits);
8215 if (size == MO_64) {
8216 TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8217 TCGv_i64 tcg_double = tcg_temp_new_i64();
8219 for (pass = 0; pass < elements; pass++) {
8220 read_vec_element(s, tcg_int64, rn, pass, mop);
8222 if (is_signed) {
8223 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8224 tcg_shift, tcg_fpst);
8225 } else {
8226 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8227 tcg_shift, tcg_fpst);
8229 if (elements == 1) {
8230 write_fp_dreg(s, rd, tcg_double);
8231 } else {
8232 write_vec_element(s, tcg_double, rd, pass, MO_64);
8236 tcg_temp_free_i64(tcg_int64);
8237 tcg_temp_free_i64(tcg_double);
8239 } else {
8240 TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8241 TCGv_i32 tcg_float = tcg_temp_new_i32();
8243 for (pass = 0; pass < elements; pass++) {
8244 read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8246 switch (size) {
8247 case MO_32:
8248 if (fracbits) {
8249 if (is_signed) {
8250 gen_helper_vfp_sltos(tcg_float, tcg_int32,
8251 tcg_shift, tcg_fpst);
8252 } else {
8253 gen_helper_vfp_ultos(tcg_float, tcg_int32,
8254 tcg_shift, tcg_fpst);
8256 } else {
8257 if (is_signed) {
8258 gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8259 } else {
8260 gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8263 break;
8264 case MO_16:
8265 if (fracbits) {
8266 if (is_signed) {
8267 gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8268 tcg_shift, tcg_fpst);
8269 } else {
8270 gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8271 tcg_shift, tcg_fpst);
8273 } else {
8274 if (is_signed) {
8275 gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8276 } else {
8277 gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8280 break;
8281 default:
8282 g_assert_not_reached();
8285 if (elements == 1) {
8286 write_fp_sreg(s, rd, tcg_float);
8287 } else {
8288 write_vec_element_i32(s, tcg_float, rd, pass, size);
8292 tcg_temp_free_i32(tcg_int32);
8293 tcg_temp_free_i32(tcg_float);
8296 tcg_temp_free_ptr(tcg_fpst);
8297 if (tcg_shift) {
8298 tcg_temp_free_i32(tcg_shift);
8301 clear_vec_high(s, elements << size == 16, rd);
8304 /* UCVTF/SCVTF - Integer to FP conversion */
8305 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8306 bool is_q, bool is_u,
8307 int immh, int immb, int opcode,
8308 int rn, int rd)
8310 int size, elements, fracbits;
8311 int immhb = immh << 3 | immb;
8313 if (immh & 8) {
8314 size = MO_64;
8315 if (!is_scalar && !is_q) {
8316 unallocated_encoding(s);
8317 return;
8319 } else if (immh & 4) {
8320 size = MO_32;
8321 } else if (immh & 2) {
8322 size = MO_16;
8323 if (!dc_isar_feature(aa64_fp16, s)) {
8324 unallocated_encoding(s);
8325 return;
8327 } else {
8328 /* immh == 0 would be a failure of the decode logic */
8329 g_assert(immh == 1);
8330 unallocated_encoding(s);
8331 return;
8334 if (is_scalar) {
8335 elements = 1;
8336 } else {
8337 elements = (8 << is_q) >> size;
8339 fracbits = (16 << size) - immhb;
8341 if (!fp_access_check(s)) {
8342 return;
8345 handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8348 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8349 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8350 bool is_q, bool is_u,
8351 int immh, int immb, int rn, int rd)
8353 int immhb = immh << 3 | immb;
8354 int pass, size, fracbits;
8355 TCGv_ptr tcg_fpstatus;
8356 TCGv_i32 tcg_rmode, tcg_shift;
8358 if (immh & 0x8) {
8359 size = MO_64;
8360 if (!is_scalar && !is_q) {
8361 unallocated_encoding(s);
8362 return;
8364 } else if (immh & 0x4) {
8365 size = MO_32;
8366 } else if (immh & 0x2) {
8367 size = MO_16;
8368 if (!dc_isar_feature(aa64_fp16, s)) {
8369 unallocated_encoding(s);
8370 return;
8372 } else {
8373 /* Should have split out AdvSIMD modified immediate earlier. */
8374 assert(immh == 1);
8375 unallocated_encoding(s);
8376 return;
8379 if (!fp_access_check(s)) {
8380 return;
8383 assert(!(is_scalar && is_q));
8385 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(FPROUNDING_ZERO));
8386 tcg_fpstatus = get_fpstatus_ptr(size == MO_16);
8387 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
8388 fracbits = (16 << size) - immhb;
8389 tcg_shift = tcg_const_i32(fracbits);
8391 if (size == MO_64) {
8392 int maxpass = is_scalar ? 1 : 2;
8394 for (pass = 0; pass < maxpass; pass++) {
8395 TCGv_i64 tcg_op = tcg_temp_new_i64();
8397 read_vec_element(s, tcg_op, rn, pass, MO_64);
8398 if (is_u) {
8399 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8400 } else {
8401 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8403 write_vec_element(s, tcg_op, rd, pass, MO_64);
8404 tcg_temp_free_i64(tcg_op);
8406 clear_vec_high(s, is_q, rd);
8407 } else {
8408 void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8409 int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8411 switch (size) {
8412 case MO_16:
8413 if (is_u) {
8414 fn = gen_helper_vfp_touhh;
8415 } else {
8416 fn = gen_helper_vfp_toshh;
8418 break;
8419 case MO_32:
8420 if (is_u) {
8421 fn = gen_helper_vfp_touls;
8422 } else {
8423 fn = gen_helper_vfp_tosls;
8425 break;
8426 default:
8427 g_assert_not_reached();
8430 for (pass = 0; pass < maxpass; pass++) {
8431 TCGv_i32 tcg_op = tcg_temp_new_i32();
8433 read_vec_element_i32(s, tcg_op, rn, pass, size);
8434 fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8435 if (is_scalar) {
8436 write_fp_sreg(s, rd, tcg_op);
8437 } else {
8438 write_vec_element_i32(s, tcg_op, rd, pass, size);
8440 tcg_temp_free_i32(tcg_op);
8442 if (!is_scalar) {
8443 clear_vec_high(s, is_q, rd);
8447 tcg_temp_free_ptr(tcg_fpstatus);
8448 tcg_temp_free_i32(tcg_shift);
8449 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
8450 tcg_temp_free_i32(tcg_rmode);
8453 /* AdvSIMD scalar shift by immediate
8454 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
8455 * +-----+---+-------------+------+------+--------+---+------+------+
8456 * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
8457 * +-----+---+-------------+------+------+--------+---+------+------+
8459 * This is the scalar version so it works on a fixed sized registers
8461 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8463 int rd = extract32(insn, 0, 5);
8464 int rn = extract32(insn, 5, 5);
8465 int opcode = extract32(insn, 11, 5);
8466 int immb = extract32(insn, 16, 3);
8467 int immh = extract32(insn, 19, 4);
8468 bool is_u = extract32(insn, 29, 1);
8470 if (immh == 0) {
8471 unallocated_encoding(s);
8472 return;
8475 switch (opcode) {
8476 case 0x08: /* SRI */
8477 if (!is_u) {
8478 unallocated_encoding(s);
8479 return;
8481 /* fall through */
8482 case 0x00: /* SSHR / USHR */
8483 case 0x02: /* SSRA / USRA */
8484 case 0x04: /* SRSHR / URSHR */
8485 case 0x06: /* SRSRA / URSRA */
8486 handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8487 break;
8488 case 0x0a: /* SHL / SLI */
8489 handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8490 break;
8491 case 0x1c: /* SCVTF, UCVTF */
8492 handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8493 opcode, rn, rd);
8494 break;
8495 case 0x10: /* SQSHRUN, SQSHRUN2 */
8496 case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8497 if (!is_u) {
8498 unallocated_encoding(s);
8499 return;
8501 handle_vec_simd_sqshrn(s, true, false, false, true,
8502 immh, immb, opcode, rn, rd);
8503 break;
8504 case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8505 case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8506 handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8507 immh, immb, opcode, rn, rd);
8508 break;
8509 case 0xc: /* SQSHLU */
8510 if (!is_u) {
8511 unallocated_encoding(s);
8512 return;
8514 handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8515 break;
8516 case 0xe: /* SQSHL, UQSHL */
8517 handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8518 break;
8519 case 0x1f: /* FCVTZS, FCVTZU */
8520 handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8521 break;
8522 default:
8523 unallocated_encoding(s);
8524 break;
8528 /* AdvSIMD scalar three different
8529 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
8530 * +-----+---+-----------+------+---+------+--------+-----+------+------+
8531 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
8532 * +-----+---+-----------+------+---+------+--------+-----+------+------+
8534 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8536 bool is_u = extract32(insn, 29, 1);
8537 int size = extract32(insn, 22, 2);
8538 int opcode = extract32(insn, 12, 4);
8539 int rm = extract32(insn, 16, 5);
8540 int rn = extract32(insn, 5, 5);
8541 int rd = extract32(insn, 0, 5);
8543 if (is_u) {
8544 unallocated_encoding(s);
8545 return;
8548 switch (opcode) {
8549 case 0x9: /* SQDMLAL, SQDMLAL2 */
8550 case 0xb: /* SQDMLSL, SQDMLSL2 */
8551 case 0xd: /* SQDMULL, SQDMULL2 */
8552 if (size == 0 || size == 3) {
8553 unallocated_encoding(s);
8554 return;
8556 break;
8557 default:
8558 unallocated_encoding(s);
8559 return;
8562 if (!fp_access_check(s)) {
8563 return;
8566 if (size == 2) {
8567 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8568 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8569 TCGv_i64 tcg_res = tcg_temp_new_i64();
8571 read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8572 read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8574 tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8575 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
8577 switch (opcode) {
8578 case 0xd: /* SQDMULL, SQDMULL2 */
8579 break;
8580 case 0xb: /* SQDMLSL, SQDMLSL2 */
8581 tcg_gen_neg_i64(tcg_res, tcg_res);
8582 /* fall through */
8583 case 0x9: /* SQDMLAL, SQDMLAL2 */
8584 read_vec_element(s, tcg_op1, rd, 0, MO_64);
8585 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
8586 tcg_res, tcg_op1);
8587 break;
8588 default:
8589 g_assert_not_reached();
8592 write_fp_dreg(s, rd, tcg_res);
8594 tcg_temp_free_i64(tcg_op1);
8595 tcg_temp_free_i64(tcg_op2);
8596 tcg_temp_free_i64(tcg_res);
8597 } else {
8598 TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8599 TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8600 TCGv_i64 tcg_res = tcg_temp_new_i64();
8602 gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8603 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
8605 switch (opcode) {
8606 case 0xd: /* SQDMULL, SQDMULL2 */
8607 break;
8608 case 0xb: /* SQDMLSL, SQDMLSL2 */
8609 gen_helper_neon_negl_u32(tcg_res, tcg_res);
8610 /* fall through */
8611 case 0x9: /* SQDMLAL, SQDMLAL2 */
8613 TCGv_i64 tcg_op3 = tcg_temp_new_i64();
8614 read_vec_element(s, tcg_op3, rd, 0, MO_32);
8615 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
8616 tcg_res, tcg_op3);
8617 tcg_temp_free_i64(tcg_op3);
8618 break;
8620 default:
8621 g_assert_not_reached();
8624 tcg_gen_ext32u_i64(tcg_res, tcg_res);
8625 write_fp_dreg(s, rd, tcg_res);
8627 tcg_temp_free_i32(tcg_op1);
8628 tcg_temp_free_i32(tcg_op2);
8629 tcg_temp_free_i64(tcg_res);
8633 static void handle_3same_64(DisasContext *s, int opcode, bool u,
8634 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
8636 /* Handle 64x64->64 opcodes which are shared between the scalar
8637 * and vector 3-same groups. We cover every opcode where size == 3
8638 * is valid in either the three-reg-same (integer, not pairwise)
8639 * or scalar-three-reg-same groups.
8641 TCGCond cond;
8643 switch (opcode) {
8644 case 0x1: /* SQADD */
8645 if (u) {
8646 gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8647 } else {
8648 gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8650 break;
8651 case 0x5: /* SQSUB */
8652 if (u) {
8653 gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8654 } else {
8655 gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8657 break;
8658 case 0x6: /* CMGT, CMHI */
8659 /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
8660 * We implement this using setcond (test) and then negating.
8662 cond = u ? TCG_COND_GTU : TCG_COND_GT;
8663 do_cmop:
8664 tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
8665 tcg_gen_neg_i64(tcg_rd, tcg_rd);
8666 break;
8667 case 0x7: /* CMGE, CMHS */
8668 cond = u ? TCG_COND_GEU : TCG_COND_GE;
8669 goto do_cmop;
8670 case 0x11: /* CMTST, CMEQ */
8671 if (u) {
8672 cond = TCG_COND_EQ;
8673 goto do_cmop;
8675 gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
8676 break;
8677 case 0x8: /* SSHL, USHL */
8678 if (u) {
8679 gen_helper_neon_shl_u64(tcg_rd, tcg_rn, tcg_rm);
8680 } else {
8681 gen_helper_neon_shl_s64(tcg_rd, tcg_rn, tcg_rm);
8683 break;
8684 case 0x9: /* SQSHL, UQSHL */
8685 if (u) {
8686 gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8687 } else {
8688 gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8690 break;
8691 case 0xa: /* SRSHL, URSHL */
8692 if (u) {
8693 gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
8694 } else {
8695 gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
8697 break;
8698 case 0xb: /* SQRSHL, UQRSHL */
8699 if (u) {
8700 gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8701 } else {
8702 gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8704 break;
8705 case 0x10: /* ADD, SUB */
8706 if (u) {
8707 tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
8708 } else {
8709 tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
8711 break;
8712 default:
8713 g_assert_not_reached();
8717 /* Handle the 3-same-operands float operations; shared by the scalar
8718 * and vector encodings. The caller must filter out any encodings
8719 * not allocated for the encoding it is dealing with.
8721 static void handle_3same_float(DisasContext *s, int size, int elements,
8722 int fpopcode, int rd, int rn, int rm)
8724 int pass;
8725 TCGv_ptr fpst = get_fpstatus_ptr(false);
8727 for (pass = 0; pass < elements; pass++) {
8728 if (size) {
8729 /* Double */
8730 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8731 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8732 TCGv_i64 tcg_res = tcg_temp_new_i64();
8734 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8735 read_vec_element(s, tcg_op2, rm, pass, MO_64);
8737 switch (fpopcode) {
8738 case 0x39: /* FMLS */
8739 /* As usual for ARM, separate negation for fused multiply-add */
8740 gen_helper_vfp_negd(tcg_op1, tcg_op1);
8741 /* fall through */
8742 case 0x19: /* FMLA */
8743 read_vec_element(s, tcg_res, rd, pass, MO_64);
8744 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
8745 tcg_res, fpst);
8746 break;
8747 case 0x18: /* FMAXNM */
8748 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8749 break;
8750 case 0x1a: /* FADD */
8751 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8752 break;
8753 case 0x1b: /* FMULX */
8754 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
8755 break;
8756 case 0x1c: /* FCMEQ */
8757 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8758 break;
8759 case 0x1e: /* FMAX */
8760 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8761 break;
8762 case 0x1f: /* FRECPS */
8763 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8764 break;
8765 case 0x38: /* FMINNM */
8766 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8767 break;
8768 case 0x3a: /* FSUB */
8769 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
8770 break;
8771 case 0x3e: /* FMIN */
8772 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8773 break;
8774 case 0x3f: /* FRSQRTS */
8775 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8776 break;
8777 case 0x5b: /* FMUL */
8778 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
8779 break;
8780 case 0x5c: /* FCMGE */
8781 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8782 break;
8783 case 0x5d: /* FACGE */
8784 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8785 break;
8786 case 0x5f: /* FDIV */
8787 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
8788 break;
8789 case 0x7a: /* FABD */
8790 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
8791 gen_helper_vfp_absd(tcg_res, tcg_res);
8792 break;
8793 case 0x7c: /* FCMGT */
8794 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8795 break;
8796 case 0x7d: /* FACGT */
8797 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8798 break;
8799 default:
8800 g_assert_not_reached();
8803 write_vec_element(s, tcg_res, rd, pass, MO_64);
8805 tcg_temp_free_i64(tcg_res);
8806 tcg_temp_free_i64(tcg_op1);
8807 tcg_temp_free_i64(tcg_op2);
8808 } else {
8809 /* Single */
8810 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8811 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8812 TCGv_i32 tcg_res = tcg_temp_new_i32();
8814 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
8815 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
8817 switch (fpopcode) {
8818 case 0x39: /* FMLS */
8819 /* As usual for ARM, separate negation for fused multiply-add */
8820 gen_helper_vfp_negs(tcg_op1, tcg_op1);
8821 /* fall through */
8822 case 0x19: /* FMLA */
8823 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
8824 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
8825 tcg_res, fpst);
8826 break;
8827 case 0x1a: /* FADD */
8828 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8829 break;
8830 case 0x1b: /* FMULX */
8831 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
8832 break;
8833 case 0x1c: /* FCMEQ */
8834 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8835 break;
8836 case 0x1e: /* FMAX */
8837 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8838 break;
8839 case 0x1f: /* FRECPS */
8840 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8841 break;
8842 case 0x18: /* FMAXNM */
8843 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8844 break;
8845 case 0x38: /* FMINNM */
8846 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8847 break;
8848 case 0x3a: /* FSUB */
8849 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
8850 break;
8851 case 0x3e: /* FMIN */
8852 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8853 break;
8854 case 0x3f: /* FRSQRTS */
8855 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8856 break;
8857 case 0x5b: /* FMUL */
8858 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
8859 break;
8860 case 0x5c: /* FCMGE */
8861 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8862 break;
8863 case 0x5d: /* FACGE */
8864 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8865 break;
8866 case 0x5f: /* FDIV */
8867 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
8868 break;
8869 case 0x7a: /* FABD */
8870 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
8871 gen_helper_vfp_abss(tcg_res, tcg_res);
8872 break;
8873 case 0x7c: /* FCMGT */
8874 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8875 break;
8876 case 0x7d: /* FACGT */
8877 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8878 break;
8879 default:
8880 g_assert_not_reached();
8883 if (elements == 1) {
8884 /* scalar single so clear high part */
8885 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
8887 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
8888 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
8889 tcg_temp_free_i64(tcg_tmp);
8890 } else {
8891 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
8894 tcg_temp_free_i32(tcg_res);
8895 tcg_temp_free_i32(tcg_op1);
8896 tcg_temp_free_i32(tcg_op2);
8900 tcg_temp_free_ptr(fpst);
8902 clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
8905 /* AdvSIMD scalar three same
8906 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
8907 * +-----+---+-----------+------+---+------+--------+---+------+------+
8908 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
8909 * +-----+---+-----------+------+---+------+--------+---+------+------+
8911 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
8913 int rd = extract32(insn, 0, 5);
8914 int rn = extract32(insn, 5, 5);
8915 int opcode = extract32(insn, 11, 5);
8916 int rm = extract32(insn, 16, 5);
8917 int size = extract32(insn, 22, 2);
8918 bool u = extract32(insn, 29, 1);
8919 TCGv_i64 tcg_rd;
8921 if (opcode >= 0x18) {
8922 /* Floating point: U, size[1] and opcode indicate operation */
8923 int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
8924 switch (fpopcode) {
8925 case 0x1b: /* FMULX */
8926 case 0x1f: /* FRECPS */
8927 case 0x3f: /* FRSQRTS */
8928 case 0x5d: /* FACGE */
8929 case 0x7d: /* FACGT */
8930 case 0x1c: /* FCMEQ */
8931 case 0x5c: /* FCMGE */
8932 case 0x7c: /* FCMGT */
8933 case 0x7a: /* FABD */
8934 break;
8935 default:
8936 unallocated_encoding(s);
8937 return;
8940 if (!fp_access_check(s)) {
8941 return;
8944 handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
8945 return;
8948 switch (opcode) {
8949 case 0x1: /* SQADD, UQADD */
8950 case 0x5: /* SQSUB, UQSUB */
8951 case 0x9: /* SQSHL, UQSHL */
8952 case 0xb: /* SQRSHL, UQRSHL */
8953 break;
8954 case 0x8: /* SSHL, USHL */
8955 case 0xa: /* SRSHL, URSHL */
8956 case 0x6: /* CMGT, CMHI */
8957 case 0x7: /* CMGE, CMHS */
8958 case 0x11: /* CMTST, CMEQ */
8959 case 0x10: /* ADD, SUB (vector) */
8960 if (size != 3) {
8961 unallocated_encoding(s);
8962 return;
8964 break;
8965 case 0x16: /* SQDMULH, SQRDMULH (vector) */
8966 if (size != 1 && size != 2) {
8967 unallocated_encoding(s);
8968 return;
8970 break;
8971 default:
8972 unallocated_encoding(s);
8973 return;
8976 if (!fp_access_check(s)) {
8977 return;
8980 tcg_rd = tcg_temp_new_i64();
8982 if (size == 3) {
8983 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
8984 TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
8986 handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
8987 tcg_temp_free_i64(tcg_rn);
8988 tcg_temp_free_i64(tcg_rm);
8989 } else {
8990 /* Do a single operation on the lowest element in the vector.
8991 * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
8992 * no side effects for all these operations.
8993 * OPTME: special-purpose helpers would avoid doing some
8994 * unnecessary work in the helper for the 8 and 16 bit cases.
8996 NeonGenTwoOpEnvFn *genenvfn;
8997 TCGv_i32 tcg_rn = tcg_temp_new_i32();
8998 TCGv_i32 tcg_rm = tcg_temp_new_i32();
8999 TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9001 read_vec_element_i32(s, tcg_rn, rn, 0, size);
9002 read_vec_element_i32(s, tcg_rm, rm, 0, size);
9004 switch (opcode) {
9005 case 0x1: /* SQADD, UQADD */
9007 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9008 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9009 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9010 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9012 genenvfn = fns[size][u];
9013 break;
9015 case 0x5: /* SQSUB, UQSUB */
9017 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9018 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9019 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9020 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9022 genenvfn = fns[size][u];
9023 break;
9025 case 0x9: /* SQSHL, UQSHL */
9027 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9028 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9029 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9030 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9032 genenvfn = fns[size][u];
9033 break;
9035 case 0xb: /* SQRSHL, UQRSHL */
9037 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9038 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9039 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9040 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9042 genenvfn = fns[size][u];
9043 break;
9045 case 0x16: /* SQDMULH, SQRDMULH */
9047 static NeonGenTwoOpEnvFn * const fns[2][2] = {
9048 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9049 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9051 assert(size == 1 || size == 2);
9052 genenvfn = fns[size - 1][u];
9053 break;
9055 default:
9056 g_assert_not_reached();
9059 genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9060 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9061 tcg_temp_free_i32(tcg_rd32);
9062 tcg_temp_free_i32(tcg_rn);
9063 tcg_temp_free_i32(tcg_rm);
9066 write_fp_dreg(s, rd, tcg_rd);
9068 tcg_temp_free_i64(tcg_rd);
9071 /* AdvSIMD scalar three same FP16
9072 * 31 30 29 28 24 23 22 21 20 16 15 14 13 11 10 9 5 4 0
9073 * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9074 * | 0 1 | U | 1 1 1 1 0 | a | 1 0 | Rm | 0 0 | opcode | 1 | Rn | Rd |
9075 * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9076 * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9077 * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9079 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9080 uint32_t insn)
9082 int rd = extract32(insn, 0, 5);
9083 int rn = extract32(insn, 5, 5);
9084 int opcode = extract32(insn, 11, 3);
9085 int rm = extract32(insn, 16, 5);
9086 bool u = extract32(insn, 29, 1);
9087 bool a = extract32(insn, 23, 1);
9088 int fpopcode = opcode | (a << 3) | (u << 4);
9089 TCGv_ptr fpst;
9090 TCGv_i32 tcg_op1;
9091 TCGv_i32 tcg_op2;
9092 TCGv_i32 tcg_res;
9094 switch (fpopcode) {
9095 case 0x03: /* FMULX */
9096 case 0x04: /* FCMEQ (reg) */
9097 case 0x07: /* FRECPS */
9098 case 0x0f: /* FRSQRTS */
9099 case 0x14: /* FCMGE (reg) */
9100 case 0x15: /* FACGE */
9101 case 0x1a: /* FABD */
9102 case 0x1c: /* FCMGT (reg) */
9103 case 0x1d: /* FACGT */
9104 break;
9105 default:
9106 unallocated_encoding(s);
9107 return;
9110 if (!dc_isar_feature(aa64_fp16, s)) {
9111 unallocated_encoding(s);
9114 if (!fp_access_check(s)) {
9115 return;
9118 fpst = get_fpstatus_ptr(true);
9120 tcg_op1 = read_fp_hreg(s, rn);
9121 tcg_op2 = read_fp_hreg(s, rm);
9122 tcg_res = tcg_temp_new_i32();
9124 switch (fpopcode) {
9125 case 0x03: /* FMULX */
9126 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9127 break;
9128 case 0x04: /* FCMEQ (reg) */
9129 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9130 break;
9131 case 0x07: /* FRECPS */
9132 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9133 break;
9134 case 0x0f: /* FRSQRTS */
9135 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9136 break;
9137 case 0x14: /* FCMGE (reg) */
9138 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9139 break;
9140 case 0x15: /* FACGE */
9141 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9142 break;
9143 case 0x1a: /* FABD */
9144 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9145 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9146 break;
9147 case 0x1c: /* FCMGT (reg) */
9148 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9149 break;
9150 case 0x1d: /* FACGT */
9151 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9152 break;
9153 default:
9154 g_assert_not_reached();
9157 write_fp_sreg(s, rd, tcg_res);
9160 tcg_temp_free_i32(tcg_res);
9161 tcg_temp_free_i32(tcg_op1);
9162 tcg_temp_free_i32(tcg_op2);
9163 tcg_temp_free_ptr(fpst);
9166 /* AdvSIMD scalar three same extra
9167 * 31 30 29 28 24 23 22 21 20 16 15 14 11 10 9 5 4 0
9168 * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9169 * | 0 1 | U | 1 1 1 1 0 | size | 0 | Rm | 1 | opcode | 1 | Rn | Rd |
9170 * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9172 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9173 uint32_t insn)
9175 int rd = extract32(insn, 0, 5);
9176 int rn = extract32(insn, 5, 5);
9177 int opcode = extract32(insn, 11, 4);
9178 int rm = extract32(insn, 16, 5);
9179 int size = extract32(insn, 22, 2);
9180 bool u = extract32(insn, 29, 1);
9181 TCGv_i32 ele1, ele2, ele3;
9182 TCGv_i64 res;
9183 bool feature;
9185 switch (u * 16 + opcode) {
9186 case 0x10: /* SQRDMLAH (vector) */
9187 case 0x11: /* SQRDMLSH (vector) */
9188 if (size != 1 && size != 2) {
9189 unallocated_encoding(s);
9190 return;
9192 feature = dc_isar_feature(aa64_rdm, s);
9193 break;
9194 default:
9195 unallocated_encoding(s);
9196 return;
9198 if (!feature) {
9199 unallocated_encoding(s);
9200 return;
9202 if (!fp_access_check(s)) {
9203 return;
9206 /* Do a single operation on the lowest element in the vector.
9207 * We use the standard Neon helpers and rely on 0 OP 0 == 0
9208 * with no side effects for all these operations.
9209 * OPTME: special-purpose helpers would avoid doing some
9210 * unnecessary work in the helper for the 16 bit cases.
9212 ele1 = tcg_temp_new_i32();
9213 ele2 = tcg_temp_new_i32();
9214 ele3 = tcg_temp_new_i32();
9216 read_vec_element_i32(s, ele1, rn, 0, size);
9217 read_vec_element_i32(s, ele2, rm, 0, size);
9218 read_vec_element_i32(s, ele3, rd, 0, size);
9220 switch (opcode) {
9221 case 0x0: /* SQRDMLAH */
9222 if (size == 1) {
9223 gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9224 } else {
9225 gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9227 break;
9228 case 0x1: /* SQRDMLSH */
9229 if (size == 1) {
9230 gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9231 } else {
9232 gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9234 break;
9235 default:
9236 g_assert_not_reached();
9238 tcg_temp_free_i32(ele1);
9239 tcg_temp_free_i32(ele2);
9241 res = tcg_temp_new_i64();
9242 tcg_gen_extu_i32_i64(res, ele3);
9243 tcg_temp_free_i32(ele3);
9245 write_fp_dreg(s, rd, res);
9246 tcg_temp_free_i64(res);
9249 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9250 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9251 TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9253 /* Handle 64->64 opcodes which are shared between the scalar and
9254 * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9255 * is valid in either group and also the double-precision fp ops.
9256 * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9257 * requires them.
9259 TCGCond cond;
9261 switch (opcode) {
9262 case 0x4: /* CLS, CLZ */
9263 if (u) {
9264 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9265 } else {
9266 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9268 break;
9269 case 0x5: /* NOT */
9270 /* This opcode is shared with CNT and RBIT but we have earlier
9271 * enforced that size == 3 if and only if this is the NOT insn.
9273 tcg_gen_not_i64(tcg_rd, tcg_rn);
9274 break;
9275 case 0x7: /* SQABS, SQNEG */
9276 if (u) {
9277 gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9278 } else {
9279 gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9281 break;
9282 case 0xa: /* CMLT */
9283 /* 64 bit integer comparison against zero, result is
9284 * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9285 * subtracting 1.
9287 cond = TCG_COND_LT;
9288 do_cmop:
9289 tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9290 tcg_gen_neg_i64(tcg_rd, tcg_rd);
9291 break;
9292 case 0x8: /* CMGT, CMGE */
9293 cond = u ? TCG_COND_GE : TCG_COND_GT;
9294 goto do_cmop;
9295 case 0x9: /* CMEQ, CMLE */
9296 cond = u ? TCG_COND_LE : TCG_COND_EQ;
9297 goto do_cmop;
9298 case 0xb: /* ABS, NEG */
9299 if (u) {
9300 tcg_gen_neg_i64(tcg_rd, tcg_rn);
9301 } else {
9302 tcg_gen_abs_i64(tcg_rd, tcg_rn);
9304 break;
9305 case 0x2f: /* FABS */
9306 gen_helper_vfp_absd(tcg_rd, tcg_rn);
9307 break;
9308 case 0x6f: /* FNEG */
9309 gen_helper_vfp_negd(tcg_rd, tcg_rn);
9310 break;
9311 case 0x7f: /* FSQRT */
9312 gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9313 break;
9314 case 0x1a: /* FCVTNS */
9315 case 0x1b: /* FCVTMS */
9316 case 0x1c: /* FCVTAS */
9317 case 0x3a: /* FCVTPS */
9318 case 0x3b: /* FCVTZS */
9320 TCGv_i32 tcg_shift = tcg_const_i32(0);
9321 gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
9322 tcg_temp_free_i32(tcg_shift);
9323 break;
9325 case 0x5a: /* FCVTNU */
9326 case 0x5b: /* FCVTMU */
9327 case 0x5c: /* FCVTAU */
9328 case 0x7a: /* FCVTPU */
9329 case 0x7b: /* FCVTZU */
9331 TCGv_i32 tcg_shift = tcg_const_i32(0);
9332 gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
9333 tcg_temp_free_i32(tcg_shift);
9334 break;
9336 case 0x18: /* FRINTN */
9337 case 0x19: /* FRINTM */
9338 case 0x38: /* FRINTP */
9339 case 0x39: /* FRINTZ */
9340 case 0x58: /* FRINTA */
9341 case 0x79: /* FRINTI */
9342 gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9343 break;
9344 case 0x59: /* FRINTX */
9345 gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9346 break;
9347 case 0x1e: /* FRINT32Z */
9348 case 0x5e: /* FRINT32X */
9349 gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9350 break;
9351 case 0x1f: /* FRINT64Z */
9352 case 0x5f: /* FRINT64X */
9353 gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9354 break;
9355 default:
9356 g_assert_not_reached();
9360 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9361 bool is_scalar, bool is_u, bool is_q,
9362 int size, int rn, int rd)
9364 bool is_double = (size == MO_64);
9365 TCGv_ptr fpst;
9367 if (!fp_access_check(s)) {
9368 return;
9371 fpst = get_fpstatus_ptr(size == MO_16);
9373 if (is_double) {
9374 TCGv_i64 tcg_op = tcg_temp_new_i64();
9375 TCGv_i64 tcg_zero = tcg_const_i64(0);
9376 TCGv_i64 tcg_res = tcg_temp_new_i64();
9377 NeonGenTwoDoubleOPFn *genfn;
9378 bool swap = false;
9379 int pass;
9381 switch (opcode) {
9382 case 0x2e: /* FCMLT (zero) */
9383 swap = true;
9384 /* fallthrough */
9385 case 0x2c: /* FCMGT (zero) */
9386 genfn = gen_helper_neon_cgt_f64;
9387 break;
9388 case 0x2d: /* FCMEQ (zero) */
9389 genfn = gen_helper_neon_ceq_f64;
9390 break;
9391 case 0x6d: /* FCMLE (zero) */
9392 swap = true;
9393 /* fall through */
9394 case 0x6c: /* FCMGE (zero) */
9395 genfn = gen_helper_neon_cge_f64;
9396 break;
9397 default:
9398 g_assert_not_reached();
9401 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9402 read_vec_element(s, tcg_op, rn, pass, MO_64);
9403 if (swap) {
9404 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9405 } else {
9406 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9408 write_vec_element(s, tcg_res, rd, pass, MO_64);
9410 tcg_temp_free_i64(tcg_res);
9411 tcg_temp_free_i64(tcg_zero);
9412 tcg_temp_free_i64(tcg_op);
9414 clear_vec_high(s, !is_scalar, rd);
9415 } else {
9416 TCGv_i32 tcg_op = tcg_temp_new_i32();
9417 TCGv_i32 tcg_zero = tcg_const_i32(0);
9418 TCGv_i32 tcg_res = tcg_temp_new_i32();
9419 NeonGenTwoSingleOPFn *genfn;
9420 bool swap = false;
9421 int pass, maxpasses;
9423 if (size == MO_16) {
9424 switch (opcode) {
9425 case 0x2e: /* FCMLT (zero) */
9426 swap = true;
9427 /* fall through */
9428 case 0x2c: /* FCMGT (zero) */
9429 genfn = gen_helper_advsimd_cgt_f16;
9430 break;
9431 case 0x2d: /* FCMEQ (zero) */
9432 genfn = gen_helper_advsimd_ceq_f16;
9433 break;
9434 case 0x6d: /* FCMLE (zero) */
9435 swap = true;
9436 /* fall through */
9437 case 0x6c: /* FCMGE (zero) */
9438 genfn = gen_helper_advsimd_cge_f16;
9439 break;
9440 default:
9441 g_assert_not_reached();
9443 } else {
9444 switch (opcode) {
9445 case 0x2e: /* FCMLT (zero) */
9446 swap = true;
9447 /* fall through */
9448 case 0x2c: /* FCMGT (zero) */
9449 genfn = gen_helper_neon_cgt_f32;
9450 break;
9451 case 0x2d: /* FCMEQ (zero) */
9452 genfn = gen_helper_neon_ceq_f32;
9453 break;
9454 case 0x6d: /* FCMLE (zero) */
9455 swap = true;
9456 /* fall through */
9457 case 0x6c: /* FCMGE (zero) */
9458 genfn = gen_helper_neon_cge_f32;
9459 break;
9460 default:
9461 g_assert_not_reached();
9465 if (is_scalar) {
9466 maxpasses = 1;
9467 } else {
9468 int vector_size = 8 << is_q;
9469 maxpasses = vector_size >> size;
9472 for (pass = 0; pass < maxpasses; pass++) {
9473 read_vec_element_i32(s, tcg_op, rn, pass, size);
9474 if (swap) {
9475 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9476 } else {
9477 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9479 if (is_scalar) {
9480 write_fp_sreg(s, rd, tcg_res);
9481 } else {
9482 write_vec_element_i32(s, tcg_res, rd, pass, size);
9485 tcg_temp_free_i32(tcg_res);
9486 tcg_temp_free_i32(tcg_zero);
9487 tcg_temp_free_i32(tcg_op);
9488 if (!is_scalar) {
9489 clear_vec_high(s, is_q, rd);
9493 tcg_temp_free_ptr(fpst);
9496 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9497 bool is_scalar, bool is_u, bool is_q,
9498 int size, int rn, int rd)
9500 bool is_double = (size == 3);
9501 TCGv_ptr fpst = get_fpstatus_ptr(false);
9503 if (is_double) {
9504 TCGv_i64 tcg_op = tcg_temp_new_i64();
9505 TCGv_i64 tcg_res = tcg_temp_new_i64();
9506 int pass;
9508 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9509 read_vec_element(s, tcg_op, rn, pass, MO_64);
9510 switch (opcode) {
9511 case 0x3d: /* FRECPE */
9512 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9513 break;
9514 case 0x3f: /* FRECPX */
9515 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9516 break;
9517 case 0x7d: /* FRSQRTE */
9518 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9519 break;
9520 default:
9521 g_assert_not_reached();
9523 write_vec_element(s, tcg_res, rd, pass, MO_64);
9525 tcg_temp_free_i64(tcg_res);
9526 tcg_temp_free_i64(tcg_op);
9527 clear_vec_high(s, !is_scalar, rd);
9528 } else {
9529 TCGv_i32 tcg_op = tcg_temp_new_i32();
9530 TCGv_i32 tcg_res = tcg_temp_new_i32();
9531 int pass, maxpasses;
9533 if (is_scalar) {
9534 maxpasses = 1;
9535 } else {
9536 maxpasses = is_q ? 4 : 2;
9539 for (pass = 0; pass < maxpasses; pass++) {
9540 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9542 switch (opcode) {
9543 case 0x3c: /* URECPE */
9544 gen_helper_recpe_u32(tcg_res, tcg_op, fpst);
9545 break;
9546 case 0x3d: /* FRECPE */
9547 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9548 break;
9549 case 0x3f: /* FRECPX */
9550 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9551 break;
9552 case 0x7d: /* FRSQRTE */
9553 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9554 break;
9555 default:
9556 g_assert_not_reached();
9559 if (is_scalar) {
9560 write_fp_sreg(s, rd, tcg_res);
9561 } else {
9562 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9565 tcg_temp_free_i32(tcg_res);
9566 tcg_temp_free_i32(tcg_op);
9567 if (!is_scalar) {
9568 clear_vec_high(s, is_q, rd);
9571 tcg_temp_free_ptr(fpst);
9574 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9575 int opcode, bool u, bool is_q,
9576 int size, int rn, int rd)
9578 /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9579 * in the source becomes a size element in the destination).
9581 int pass;
9582 TCGv_i32 tcg_res[2];
9583 int destelt = is_q ? 2 : 0;
9584 int passes = scalar ? 1 : 2;
9586 if (scalar) {
9587 tcg_res[1] = tcg_const_i32(0);
9590 for (pass = 0; pass < passes; pass++) {
9591 TCGv_i64 tcg_op = tcg_temp_new_i64();
9592 NeonGenNarrowFn *genfn = NULL;
9593 NeonGenNarrowEnvFn *genenvfn = NULL;
9595 if (scalar) {
9596 read_vec_element(s, tcg_op, rn, pass, size + 1);
9597 } else {
9598 read_vec_element(s, tcg_op, rn, pass, MO_64);
9600 tcg_res[pass] = tcg_temp_new_i32();
9602 switch (opcode) {
9603 case 0x12: /* XTN, SQXTUN */
9605 static NeonGenNarrowFn * const xtnfns[3] = {
9606 gen_helper_neon_narrow_u8,
9607 gen_helper_neon_narrow_u16,
9608 tcg_gen_extrl_i64_i32,
9610 static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9611 gen_helper_neon_unarrow_sat8,
9612 gen_helper_neon_unarrow_sat16,
9613 gen_helper_neon_unarrow_sat32,
9615 if (u) {
9616 genenvfn = sqxtunfns[size];
9617 } else {
9618 genfn = xtnfns[size];
9620 break;
9622 case 0x14: /* SQXTN, UQXTN */
9624 static NeonGenNarrowEnvFn * const fns[3][2] = {
9625 { gen_helper_neon_narrow_sat_s8,
9626 gen_helper_neon_narrow_sat_u8 },
9627 { gen_helper_neon_narrow_sat_s16,
9628 gen_helper_neon_narrow_sat_u16 },
9629 { gen_helper_neon_narrow_sat_s32,
9630 gen_helper_neon_narrow_sat_u32 },
9632 genenvfn = fns[size][u];
9633 break;
9635 case 0x16: /* FCVTN, FCVTN2 */
9636 /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9637 if (size == 2) {
9638 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
9639 } else {
9640 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9641 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9642 TCGv_ptr fpst = get_fpstatus_ptr(false);
9643 TCGv_i32 ahp = get_ahp_flag();
9645 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9646 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9647 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9648 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9649 tcg_temp_free_i32(tcg_lo);
9650 tcg_temp_free_i32(tcg_hi);
9651 tcg_temp_free_ptr(fpst);
9652 tcg_temp_free_i32(ahp);
9654 break;
9655 case 0x56: /* FCVTXN, FCVTXN2 */
9656 /* 64 bit to 32 bit float conversion
9657 * with von Neumann rounding (round to odd)
9659 assert(size == 2);
9660 gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
9661 break;
9662 default:
9663 g_assert_not_reached();
9666 if (genfn) {
9667 genfn(tcg_res[pass], tcg_op);
9668 } else if (genenvfn) {
9669 genenvfn(tcg_res[pass], cpu_env, tcg_op);
9672 tcg_temp_free_i64(tcg_op);
9675 for (pass = 0; pass < 2; pass++) {
9676 write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
9677 tcg_temp_free_i32(tcg_res[pass]);
9679 clear_vec_high(s, is_q, rd);
9682 /* Remaining saturating accumulating ops */
9683 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
9684 bool is_q, int size, int rn, int rd)
9686 bool is_double = (size == 3);
9688 if (is_double) {
9689 TCGv_i64 tcg_rn = tcg_temp_new_i64();
9690 TCGv_i64 tcg_rd = tcg_temp_new_i64();
9691 int pass;
9693 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9694 read_vec_element(s, tcg_rn, rn, pass, MO_64);
9695 read_vec_element(s, tcg_rd, rd, pass, MO_64);
9697 if (is_u) { /* USQADD */
9698 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9699 } else { /* SUQADD */
9700 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9702 write_vec_element(s, tcg_rd, rd, pass, MO_64);
9704 tcg_temp_free_i64(tcg_rd);
9705 tcg_temp_free_i64(tcg_rn);
9706 clear_vec_high(s, !is_scalar, rd);
9707 } else {
9708 TCGv_i32 tcg_rn = tcg_temp_new_i32();
9709 TCGv_i32 tcg_rd = tcg_temp_new_i32();
9710 int pass, maxpasses;
9712 if (is_scalar) {
9713 maxpasses = 1;
9714 } else {
9715 maxpasses = is_q ? 4 : 2;
9718 for (pass = 0; pass < maxpasses; pass++) {
9719 if (is_scalar) {
9720 read_vec_element_i32(s, tcg_rn, rn, pass, size);
9721 read_vec_element_i32(s, tcg_rd, rd, pass, size);
9722 } else {
9723 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
9724 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9727 if (is_u) { /* USQADD */
9728 switch (size) {
9729 case 0:
9730 gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9731 break;
9732 case 1:
9733 gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9734 break;
9735 case 2:
9736 gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9737 break;
9738 default:
9739 g_assert_not_reached();
9741 } else { /* SUQADD */
9742 switch (size) {
9743 case 0:
9744 gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9745 break;
9746 case 1:
9747 gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9748 break;
9749 case 2:
9750 gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9751 break;
9752 default:
9753 g_assert_not_reached();
9757 if (is_scalar) {
9758 TCGv_i64 tcg_zero = tcg_const_i64(0);
9759 write_vec_element(s, tcg_zero, rd, 0, MO_64);
9760 tcg_temp_free_i64(tcg_zero);
9762 write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9764 tcg_temp_free_i32(tcg_rd);
9765 tcg_temp_free_i32(tcg_rn);
9766 clear_vec_high(s, is_q, rd);
9770 /* AdvSIMD scalar two reg misc
9771 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
9772 * +-----+---+-----------+------+-----------+--------+-----+------+------+
9773 * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
9774 * +-----+---+-----------+------+-----------+--------+-----+------+------+
9776 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
9778 int rd = extract32(insn, 0, 5);
9779 int rn = extract32(insn, 5, 5);
9780 int opcode = extract32(insn, 12, 5);
9781 int size = extract32(insn, 22, 2);
9782 bool u = extract32(insn, 29, 1);
9783 bool is_fcvt = false;
9784 int rmode;
9785 TCGv_i32 tcg_rmode;
9786 TCGv_ptr tcg_fpstatus;
9788 switch (opcode) {
9789 case 0x3: /* USQADD / SUQADD*/
9790 if (!fp_access_check(s)) {
9791 return;
9793 handle_2misc_satacc(s, true, u, false, size, rn, rd);
9794 return;
9795 case 0x7: /* SQABS / SQNEG */
9796 break;
9797 case 0xa: /* CMLT */
9798 if (u) {
9799 unallocated_encoding(s);
9800 return;
9802 /* fall through */
9803 case 0x8: /* CMGT, CMGE */
9804 case 0x9: /* CMEQ, CMLE */
9805 case 0xb: /* ABS, NEG */
9806 if (size != 3) {
9807 unallocated_encoding(s);
9808 return;
9810 break;
9811 case 0x12: /* SQXTUN */
9812 if (!u) {
9813 unallocated_encoding(s);
9814 return;
9816 /* fall through */
9817 case 0x14: /* SQXTN, UQXTN */
9818 if (size == 3) {
9819 unallocated_encoding(s);
9820 return;
9822 if (!fp_access_check(s)) {
9823 return;
9825 handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
9826 return;
9827 case 0xc ... 0xf:
9828 case 0x16 ... 0x1d:
9829 case 0x1f:
9830 /* Floating point: U, size[1] and opcode indicate operation;
9831 * size[0] indicates single or double precision.
9833 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
9834 size = extract32(size, 0, 1) ? 3 : 2;
9835 switch (opcode) {
9836 case 0x2c: /* FCMGT (zero) */
9837 case 0x2d: /* FCMEQ (zero) */
9838 case 0x2e: /* FCMLT (zero) */
9839 case 0x6c: /* FCMGE (zero) */
9840 case 0x6d: /* FCMLE (zero) */
9841 handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
9842 return;
9843 case 0x1d: /* SCVTF */
9844 case 0x5d: /* UCVTF */
9846 bool is_signed = (opcode == 0x1d);
9847 if (!fp_access_check(s)) {
9848 return;
9850 handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
9851 return;
9853 case 0x3d: /* FRECPE */
9854 case 0x3f: /* FRECPX */
9855 case 0x7d: /* FRSQRTE */
9856 if (!fp_access_check(s)) {
9857 return;
9859 handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
9860 return;
9861 case 0x1a: /* FCVTNS */
9862 case 0x1b: /* FCVTMS */
9863 case 0x3a: /* FCVTPS */
9864 case 0x3b: /* FCVTZS */
9865 case 0x5a: /* FCVTNU */
9866 case 0x5b: /* FCVTMU */
9867 case 0x7a: /* FCVTPU */
9868 case 0x7b: /* FCVTZU */
9869 is_fcvt = true;
9870 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
9871 break;
9872 case 0x1c: /* FCVTAS */
9873 case 0x5c: /* FCVTAU */
9874 /* TIEAWAY doesn't fit in the usual rounding mode encoding */
9875 is_fcvt = true;
9876 rmode = FPROUNDING_TIEAWAY;
9877 break;
9878 case 0x56: /* FCVTXN, FCVTXN2 */
9879 if (size == 2) {
9880 unallocated_encoding(s);
9881 return;
9883 if (!fp_access_check(s)) {
9884 return;
9886 handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
9887 return;
9888 default:
9889 unallocated_encoding(s);
9890 return;
9892 break;
9893 default:
9894 unallocated_encoding(s);
9895 return;
9898 if (!fp_access_check(s)) {
9899 return;
9902 if (is_fcvt) {
9903 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
9904 tcg_fpstatus = get_fpstatus_ptr(false);
9905 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
9906 } else {
9907 tcg_rmode = NULL;
9908 tcg_fpstatus = NULL;
9911 if (size == 3) {
9912 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9913 TCGv_i64 tcg_rd = tcg_temp_new_i64();
9915 handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
9916 write_fp_dreg(s, rd, tcg_rd);
9917 tcg_temp_free_i64(tcg_rd);
9918 tcg_temp_free_i64(tcg_rn);
9919 } else {
9920 TCGv_i32 tcg_rn = tcg_temp_new_i32();
9921 TCGv_i32 tcg_rd = tcg_temp_new_i32();
9923 read_vec_element_i32(s, tcg_rn, rn, 0, size);
9925 switch (opcode) {
9926 case 0x7: /* SQABS, SQNEG */
9928 NeonGenOneOpEnvFn *genfn;
9929 static NeonGenOneOpEnvFn * const fns[3][2] = {
9930 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
9931 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
9932 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
9934 genfn = fns[size][u];
9935 genfn(tcg_rd, cpu_env, tcg_rn);
9936 break;
9938 case 0x1a: /* FCVTNS */
9939 case 0x1b: /* FCVTMS */
9940 case 0x1c: /* FCVTAS */
9941 case 0x3a: /* FCVTPS */
9942 case 0x3b: /* FCVTZS */
9944 TCGv_i32 tcg_shift = tcg_const_i32(0);
9945 gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
9946 tcg_temp_free_i32(tcg_shift);
9947 break;
9949 case 0x5a: /* FCVTNU */
9950 case 0x5b: /* FCVTMU */
9951 case 0x5c: /* FCVTAU */
9952 case 0x7a: /* FCVTPU */
9953 case 0x7b: /* FCVTZU */
9955 TCGv_i32 tcg_shift = tcg_const_i32(0);
9956 gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
9957 tcg_temp_free_i32(tcg_shift);
9958 break;
9960 default:
9961 g_assert_not_reached();
9964 write_fp_sreg(s, rd, tcg_rd);
9965 tcg_temp_free_i32(tcg_rd);
9966 tcg_temp_free_i32(tcg_rn);
9969 if (is_fcvt) {
9970 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
9971 tcg_temp_free_i32(tcg_rmode);
9972 tcg_temp_free_ptr(tcg_fpstatus);
9976 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
9977 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
9978 int immh, int immb, int opcode, int rn, int rd)
9980 int size = 32 - clz32(immh) - 1;
9981 int immhb = immh << 3 | immb;
9982 int shift = 2 * (8 << size) - immhb;
9983 bool accumulate = false;
9984 int dsize = is_q ? 128 : 64;
9985 int esize = 8 << size;
9986 int elements = dsize/esize;
9987 TCGMemOp memop = size | (is_u ? 0 : MO_SIGN);
9988 TCGv_i64 tcg_rn = new_tmp_a64(s);
9989 TCGv_i64 tcg_rd = new_tmp_a64(s);
9990 TCGv_i64 tcg_round;
9991 uint64_t round_const;
9992 int i;
9994 if (extract32(immh, 3, 1) && !is_q) {
9995 unallocated_encoding(s);
9996 return;
9998 tcg_debug_assert(size <= 3);
10000 if (!fp_access_check(s)) {
10001 return;
10004 switch (opcode) {
10005 case 0x02: /* SSRA / USRA (accumulate) */
10006 if (is_u) {
10007 /* Shift count same as element size produces zero to add. */
10008 if (shift == 8 << size) {
10009 goto done;
10011 gen_gvec_op2i(s, is_q, rd, rn, shift, &usra_op[size]);
10012 } else {
10013 /* Shift count same as element size produces all sign to add. */
10014 if (shift == 8 << size) {
10015 shift -= 1;
10017 gen_gvec_op2i(s, is_q, rd, rn, shift, &ssra_op[size]);
10019 return;
10020 case 0x08: /* SRI */
10021 /* Shift count same as element size is valid but does nothing. */
10022 if (shift == 8 << size) {
10023 goto done;
10025 gen_gvec_op2i(s, is_q, rd, rn, shift, &sri_op[size]);
10026 return;
10028 case 0x00: /* SSHR / USHR */
10029 if (is_u) {
10030 if (shift == 8 << size) {
10031 /* Shift count the same size as element size produces zero. */
10032 tcg_gen_gvec_dup8i(vec_full_reg_offset(s, rd),
10033 is_q ? 16 : 8, vec_full_reg_size(s), 0);
10034 } else {
10035 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shri, size);
10037 } else {
10038 /* Shift count the same size as element size produces all sign. */
10039 if (shift == 8 << size) {
10040 shift -= 1;
10042 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_sari, size);
10044 return;
10046 case 0x04: /* SRSHR / URSHR (rounding) */
10047 break;
10048 case 0x06: /* SRSRA / URSRA (accum + rounding) */
10049 accumulate = true;
10050 break;
10051 default:
10052 g_assert_not_reached();
10055 round_const = 1ULL << (shift - 1);
10056 tcg_round = tcg_const_i64(round_const);
10058 for (i = 0; i < elements; i++) {
10059 read_vec_element(s, tcg_rn, rn, i, memop);
10060 if (accumulate) {
10061 read_vec_element(s, tcg_rd, rd, i, memop);
10064 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10065 accumulate, is_u, size, shift);
10067 write_vec_element(s, tcg_rd, rd, i, size);
10069 tcg_temp_free_i64(tcg_round);
10071 done:
10072 clear_vec_high(s, is_q, rd);
10075 /* SHL/SLI - Vector shift left */
10076 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10077 int immh, int immb, int opcode, int rn, int rd)
10079 int size = 32 - clz32(immh) - 1;
10080 int immhb = immh << 3 | immb;
10081 int shift = immhb - (8 << size);
10083 /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10084 assert(size >= 0 && size <= 3);
10086 if (extract32(immh, 3, 1) && !is_q) {
10087 unallocated_encoding(s);
10088 return;
10091 if (!fp_access_check(s)) {
10092 return;
10095 if (insert) {
10096 gen_gvec_op2i(s, is_q, rd, rn, shift, &sli_op[size]);
10097 } else {
10098 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10102 /* USHLL/SHLL - Vector shift left with widening */
10103 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10104 int immh, int immb, int opcode, int rn, int rd)
10106 int size = 32 - clz32(immh) - 1;
10107 int immhb = immh << 3 | immb;
10108 int shift = immhb - (8 << size);
10109 int dsize = 64;
10110 int esize = 8 << size;
10111 int elements = dsize/esize;
10112 TCGv_i64 tcg_rn = new_tmp_a64(s);
10113 TCGv_i64 tcg_rd = new_tmp_a64(s);
10114 int i;
10116 if (size >= 3) {
10117 unallocated_encoding(s);
10118 return;
10121 if (!fp_access_check(s)) {
10122 return;
10125 /* For the LL variants the store is larger than the load,
10126 * so if rd == rn we would overwrite parts of our input.
10127 * So load everything right now and use shifts in the main loop.
10129 read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10131 for (i = 0; i < elements; i++) {
10132 tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10133 ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10134 tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10135 write_vec_element(s, tcg_rd, rd, i, size + 1);
10139 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10140 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10141 int immh, int immb, int opcode, int rn, int rd)
10143 int immhb = immh << 3 | immb;
10144 int size = 32 - clz32(immh) - 1;
10145 int dsize = 64;
10146 int esize = 8 << size;
10147 int elements = dsize/esize;
10148 int shift = (2 * esize) - immhb;
10149 bool round = extract32(opcode, 0, 1);
10150 TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10151 TCGv_i64 tcg_round;
10152 int i;
10154 if (extract32(immh, 3, 1)) {
10155 unallocated_encoding(s);
10156 return;
10159 if (!fp_access_check(s)) {
10160 return;
10163 tcg_rn = tcg_temp_new_i64();
10164 tcg_rd = tcg_temp_new_i64();
10165 tcg_final = tcg_temp_new_i64();
10166 read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10168 if (round) {
10169 uint64_t round_const = 1ULL << (shift - 1);
10170 tcg_round = tcg_const_i64(round_const);
10171 } else {
10172 tcg_round = NULL;
10175 for (i = 0; i < elements; i++) {
10176 read_vec_element(s, tcg_rn, rn, i, size+1);
10177 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10178 false, true, size+1, shift);
10180 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10183 if (!is_q) {
10184 write_vec_element(s, tcg_final, rd, 0, MO_64);
10185 } else {
10186 write_vec_element(s, tcg_final, rd, 1, MO_64);
10188 if (round) {
10189 tcg_temp_free_i64(tcg_round);
10191 tcg_temp_free_i64(tcg_rn);
10192 tcg_temp_free_i64(tcg_rd);
10193 tcg_temp_free_i64(tcg_final);
10195 clear_vec_high(s, is_q, rd);
10199 /* AdvSIMD shift by immediate
10200 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
10201 * +---+---+---+-------------+------+------+--------+---+------+------+
10202 * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
10203 * +---+---+---+-------------+------+------+--------+---+------+------+
10205 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10207 int rd = extract32(insn, 0, 5);
10208 int rn = extract32(insn, 5, 5);
10209 int opcode = extract32(insn, 11, 5);
10210 int immb = extract32(insn, 16, 3);
10211 int immh = extract32(insn, 19, 4);
10212 bool is_u = extract32(insn, 29, 1);
10213 bool is_q = extract32(insn, 30, 1);
10215 switch (opcode) {
10216 case 0x08: /* SRI */
10217 if (!is_u) {
10218 unallocated_encoding(s);
10219 return;
10221 /* fall through */
10222 case 0x00: /* SSHR / USHR */
10223 case 0x02: /* SSRA / USRA (accumulate) */
10224 case 0x04: /* SRSHR / URSHR (rounding) */
10225 case 0x06: /* SRSRA / URSRA (accum + rounding) */
10226 handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10227 break;
10228 case 0x0a: /* SHL / SLI */
10229 handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10230 break;
10231 case 0x10: /* SHRN */
10232 case 0x11: /* RSHRN / SQRSHRUN */
10233 if (is_u) {
10234 handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10235 opcode, rn, rd);
10236 } else {
10237 handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10239 break;
10240 case 0x12: /* SQSHRN / UQSHRN */
10241 case 0x13: /* SQRSHRN / UQRSHRN */
10242 handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10243 opcode, rn, rd);
10244 break;
10245 case 0x14: /* SSHLL / USHLL */
10246 handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10247 break;
10248 case 0x1c: /* SCVTF / UCVTF */
10249 handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10250 opcode, rn, rd);
10251 break;
10252 case 0xc: /* SQSHLU */
10253 if (!is_u) {
10254 unallocated_encoding(s);
10255 return;
10257 handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10258 break;
10259 case 0xe: /* SQSHL, UQSHL */
10260 handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10261 break;
10262 case 0x1f: /* FCVTZS/ FCVTZU */
10263 handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10264 return;
10265 default:
10266 unallocated_encoding(s);
10267 return;
10271 /* Generate code to do a "long" addition or subtraction, ie one done in
10272 * TCGv_i64 on vector lanes twice the width specified by size.
10274 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10275 TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10277 static NeonGenTwo64OpFn * const fns[3][2] = {
10278 { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10279 { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10280 { tcg_gen_add_i64, tcg_gen_sub_i64 },
10282 NeonGenTwo64OpFn *genfn;
10283 assert(size < 3);
10285 genfn = fns[size][is_sub];
10286 genfn(tcg_res, tcg_op1, tcg_op2);
10289 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10290 int opcode, int rd, int rn, int rm)
10292 /* 3-reg-different widening insns: 64 x 64 -> 128 */
10293 TCGv_i64 tcg_res[2];
10294 int pass, accop;
10296 tcg_res[0] = tcg_temp_new_i64();
10297 tcg_res[1] = tcg_temp_new_i64();
10299 /* Does this op do an adding accumulate, a subtracting accumulate,
10300 * or no accumulate at all?
10302 switch (opcode) {
10303 case 5:
10304 case 8:
10305 case 9:
10306 accop = 1;
10307 break;
10308 case 10:
10309 case 11:
10310 accop = -1;
10311 break;
10312 default:
10313 accop = 0;
10314 break;
10317 if (accop != 0) {
10318 read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10319 read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10322 /* size == 2 means two 32x32->64 operations; this is worth special
10323 * casing because we can generally handle it inline.
10325 if (size == 2) {
10326 for (pass = 0; pass < 2; pass++) {
10327 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10328 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10329 TCGv_i64 tcg_passres;
10330 TCGMemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10332 int elt = pass + is_q * 2;
10334 read_vec_element(s, tcg_op1, rn, elt, memop);
10335 read_vec_element(s, tcg_op2, rm, elt, memop);
10337 if (accop == 0) {
10338 tcg_passres = tcg_res[pass];
10339 } else {
10340 tcg_passres = tcg_temp_new_i64();
10343 switch (opcode) {
10344 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10345 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10346 break;
10347 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10348 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10349 break;
10350 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10351 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10353 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10354 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10356 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10357 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10358 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10359 tcg_passres,
10360 tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10361 tcg_temp_free_i64(tcg_tmp1);
10362 tcg_temp_free_i64(tcg_tmp2);
10363 break;
10365 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10366 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10367 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10368 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10369 break;
10370 case 9: /* SQDMLAL, SQDMLAL2 */
10371 case 11: /* SQDMLSL, SQDMLSL2 */
10372 case 13: /* SQDMULL, SQDMULL2 */
10373 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10374 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10375 tcg_passres, tcg_passres);
10376 break;
10377 default:
10378 g_assert_not_reached();
10381 if (opcode == 9 || opcode == 11) {
10382 /* saturating accumulate ops */
10383 if (accop < 0) {
10384 tcg_gen_neg_i64(tcg_passres, tcg_passres);
10386 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10387 tcg_res[pass], tcg_passres);
10388 } else if (accop > 0) {
10389 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10390 } else if (accop < 0) {
10391 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10394 if (accop != 0) {
10395 tcg_temp_free_i64(tcg_passres);
10398 tcg_temp_free_i64(tcg_op1);
10399 tcg_temp_free_i64(tcg_op2);
10401 } else {
10402 /* size 0 or 1, generally helper functions */
10403 for (pass = 0; pass < 2; pass++) {
10404 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10405 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10406 TCGv_i64 tcg_passres;
10407 int elt = pass + is_q * 2;
10409 read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10410 read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10412 if (accop == 0) {
10413 tcg_passres = tcg_res[pass];
10414 } else {
10415 tcg_passres = tcg_temp_new_i64();
10418 switch (opcode) {
10419 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10420 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10422 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10423 static NeonGenWidenFn * const widenfns[2][2] = {
10424 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10425 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10427 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10429 widenfn(tcg_op2_64, tcg_op2);
10430 widenfn(tcg_passres, tcg_op1);
10431 gen_neon_addl(size, (opcode == 2), tcg_passres,
10432 tcg_passres, tcg_op2_64);
10433 tcg_temp_free_i64(tcg_op2_64);
10434 break;
10436 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10437 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10438 if (size == 0) {
10439 if (is_u) {
10440 gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10441 } else {
10442 gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10444 } else {
10445 if (is_u) {
10446 gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10447 } else {
10448 gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10451 break;
10452 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10453 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10454 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10455 if (size == 0) {
10456 if (is_u) {
10457 gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10458 } else {
10459 gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10461 } else {
10462 if (is_u) {
10463 gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10464 } else {
10465 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10468 break;
10469 case 9: /* SQDMLAL, SQDMLAL2 */
10470 case 11: /* SQDMLSL, SQDMLSL2 */
10471 case 13: /* SQDMULL, SQDMULL2 */
10472 assert(size == 1);
10473 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10474 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10475 tcg_passres, tcg_passres);
10476 break;
10477 case 14: /* PMULL */
10478 assert(size == 0);
10479 gen_helper_neon_mull_p8(tcg_passres, tcg_op1, tcg_op2);
10480 break;
10481 default:
10482 g_assert_not_reached();
10484 tcg_temp_free_i32(tcg_op1);
10485 tcg_temp_free_i32(tcg_op2);
10487 if (accop != 0) {
10488 if (opcode == 9 || opcode == 11) {
10489 /* saturating accumulate ops */
10490 if (accop < 0) {
10491 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10493 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10494 tcg_res[pass],
10495 tcg_passres);
10496 } else {
10497 gen_neon_addl(size, (accop < 0), tcg_res[pass],
10498 tcg_res[pass], tcg_passres);
10500 tcg_temp_free_i64(tcg_passres);
10505 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10506 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10507 tcg_temp_free_i64(tcg_res[0]);
10508 tcg_temp_free_i64(tcg_res[1]);
10511 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10512 int opcode, int rd, int rn, int rm)
10514 TCGv_i64 tcg_res[2];
10515 int part = is_q ? 2 : 0;
10516 int pass;
10518 for (pass = 0; pass < 2; pass++) {
10519 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10520 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10521 TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10522 static NeonGenWidenFn * const widenfns[3][2] = {
10523 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10524 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10525 { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10527 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10529 read_vec_element(s, tcg_op1, rn, pass, MO_64);
10530 read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10531 widenfn(tcg_op2_wide, tcg_op2);
10532 tcg_temp_free_i32(tcg_op2);
10533 tcg_res[pass] = tcg_temp_new_i64();
10534 gen_neon_addl(size, (opcode == 3),
10535 tcg_res[pass], tcg_op1, tcg_op2_wide);
10536 tcg_temp_free_i64(tcg_op1);
10537 tcg_temp_free_i64(tcg_op2_wide);
10540 for (pass = 0; pass < 2; pass++) {
10541 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10542 tcg_temp_free_i64(tcg_res[pass]);
10546 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10548 tcg_gen_addi_i64(in, in, 1U << 31);
10549 tcg_gen_extrh_i64_i32(res, in);
10552 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10553 int opcode, int rd, int rn, int rm)
10555 TCGv_i32 tcg_res[2];
10556 int part = is_q ? 2 : 0;
10557 int pass;
10559 for (pass = 0; pass < 2; pass++) {
10560 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10561 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10562 TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10563 static NeonGenNarrowFn * const narrowfns[3][2] = {
10564 { gen_helper_neon_narrow_high_u8,
10565 gen_helper_neon_narrow_round_high_u8 },
10566 { gen_helper_neon_narrow_high_u16,
10567 gen_helper_neon_narrow_round_high_u16 },
10568 { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10570 NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10572 read_vec_element(s, tcg_op1, rn, pass, MO_64);
10573 read_vec_element(s, tcg_op2, rm, pass, MO_64);
10575 gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10577 tcg_temp_free_i64(tcg_op1);
10578 tcg_temp_free_i64(tcg_op2);
10580 tcg_res[pass] = tcg_temp_new_i32();
10581 gennarrow(tcg_res[pass], tcg_wideres);
10582 tcg_temp_free_i64(tcg_wideres);
10585 for (pass = 0; pass < 2; pass++) {
10586 write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10587 tcg_temp_free_i32(tcg_res[pass]);
10589 clear_vec_high(s, is_q, rd);
10592 static void handle_pmull_64(DisasContext *s, int is_q, int rd, int rn, int rm)
10594 /* PMULL of 64 x 64 -> 128 is an odd special case because it
10595 * is the only three-reg-diff instruction which produces a
10596 * 128-bit wide result from a single operation. However since
10597 * it's possible to calculate the two halves more or less
10598 * separately we just use two helper calls.
10600 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10601 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10602 TCGv_i64 tcg_res = tcg_temp_new_i64();
10604 read_vec_element(s, tcg_op1, rn, is_q, MO_64);
10605 read_vec_element(s, tcg_op2, rm, is_q, MO_64);
10606 gen_helper_neon_pmull_64_lo(tcg_res, tcg_op1, tcg_op2);
10607 write_vec_element(s, tcg_res, rd, 0, MO_64);
10608 gen_helper_neon_pmull_64_hi(tcg_res, tcg_op1, tcg_op2);
10609 write_vec_element(s, tcg_res, rd, 1, MO_64);
10611 tcg_temp_free_i64(tcg_op1);
10612 tcg_temp_free_i64(tcg_op2);
10613 tcg_temp_free_i64(tcg_res);
10616 /* AdvSIMD three different
10617 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
10618 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10619 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
10620 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10622 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10624 /* Instructions in this group fall into three basic classes
10625 * (in each case with the operation working on each element in
10626 * the input vectors):
10627 * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10628 * 128 bit input)
10629 * (2) wide 64 x 128 -> 128
10630 * (3) narrowing 128 x 128 -> 64
10631 * Here we do initial decode, catch unallocated cases and
10632 * dispatch to separate functions for each class.
10634 int is_q = extract32(insn, 30, 1);
10635 int is_u = extract32(insn, 29, 1);
10636 int size = extract32(insn, 22, 2);
10637 int opcode = extract32(insn, 12, 4);
10638 int rm = extract32(insn, 16, 5);
10639 int rn = extract32(insn, 5, 5);
10640 int rd = extract32(insn, 0, 5);
10642 switch (opcode) {
10643 case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10644 case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10645 /* 64 x 128 -> 128 */
10646 if (size == 3) {
10647 unallocated_encoding(s);
10648 return;
10650 if (!fp_access_check(s)) {
10651 return;
10653 handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10654 break;
10655 case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10656 case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10657 /* 128 x 128 -> 64 */
10658 if (size == 3) {
10659 unallocated_encoding(s);
10660 return;
10662 if (!fp_access_check(s)) {
10663 return;
10665 handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10666 break;
10667 case 14: /* PMULL, PMULL2 */
10668 if (is_u || size == 1 || size == 2) {
10669 unallocated_encoding(s);
10670 return;
10672 if (size == 3) {
10673 if (!dc_isar_feature(aa64_pmull, s)) {
10674 unallocated_encoding(s);
10675 return;
10677 if (!fp_access_check(s)) {
10678 return;
10680 handle_pmull_64(s, is_q, rd, rn, rm);
10681 return;
10683 goto is_widening;
10684 case 9: /* SQDMLAL, SQDMLAL2 */
10685 case 11: /* SQDMLSL, SQDMLSL2 */
10686 case 13: /* SQDMULL, SQDMULL2 */
10687 if (is_u || size == 0) {
10688 unallocated_encoding(s);
10689 return;
10691 /* fall through */
10692 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10693 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10694 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10695 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10696 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10697 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10698 case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10699 /* 64 x 64 -> 128 */
10700 if (size == 3) {
10701 unallocated_encoding(s);
10702 return;
10704 is_widening:
10705 if (!fp_access_check(s)) {
10706 return;
10709 handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10710 break;
10711 default:
10712 /* opcode 15 not allocated */
10713 unallocated_encoding(s);
10714 break;
10718 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10719 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10721 int rd = extract32(insn, 0, 5);
10722 int rn = extract32(insn, 5, 5);
10723 int rm = extract32(insn, 16, 5);
10724 int size = extract32(insn, 22, 2);
10725 bool is_u = extract32(insn, 29, 1);
10726 bool is_q = extract32(insn, 30, 1);
10728 if (!fp_access_check(s)) {
10729 return;
10732 switch (size + 4 * is_u) {
10733 case 0: /* AND */
10734 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10735 return;
10736 case 1: /* BIC */
10737 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10738 return;
10739 case 2: /* ORR */
10740 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10741 return;
10742 case 3: /* ORN */
10743 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10744 return;
10745 case 4: /* EOR */
10746 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10747 return;
10749 case 5: /* BSL bitwise select */
10750 gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
10751 return;
10752 case 6: /* BIT, bitwise insert if true */
10753 gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
10754 return;
10755 case 7: /* BIF, bitwise insert if false */
10756 gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
10757 return;
10759 default:
10760 g_assert_not_reached();
10764 /* Pairwise op subgroup of C3.6.16.
10766 * This is called directly or via the handle_3same_float for float pairwise
10767 * operations where the opcode and size are calculated differently.
10769 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
10770 int size, int rn, int rm, int rd)
10772 TCGv_ptr fpst;
10773 int pass;
10775 /* Floating point operations need fpst */
10776 if (opcode >= 0x58) {
10777 fpst = get_fpstatus_ptr(false);
10778 } else {
10779 fpst = NULL;
10782 if (!fp_access_check(s)) {
10783 return;
10786 /* These operations work on the concatenated rm:rn, with each pair of
10787 * adjacent elements being operated on to produce an element in the result.
10789 if (size == 3) {
10790 TCGv_i64 tcg_res[2];
10792 for (pass = 0; pass < 2; pass++) {
10793 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10794 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10795 int passreg = (pass == 0) ? rn : rm;
10797 read_vec_element(s, tcg_op1, passreg, 0, MO_64);
10798 read_vec_element(s, tcg_op2, passreg, 1, MO_64);
10799 tcg_res[pass] = tcg_temp_new_i64();
10801 switch (opcode) {
10802 case 0x17: /* ADDP */
10803 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
10804 break;
10805 case 0x58: /* FMAXNMP */
10806 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10807 break;
10808 case 0x5a: /* FADDP */
10809 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10810 break;
10811 case 0x5e: /* FMAXP */
10812 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10813 break;
10814 case 0x78: /* FMINNMP */
10815 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10816 break;
10817 case 0x7e: /* FMINP */
10818 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10819 break;
10820 default:
10821 g_assert_not_reached();
10824 tcg_temp_free_i64(tcg_op1);
10825 tcg_temp_free_i64(tcg_op2);
10828 for (pass = 0; pass < 2; pass++) {
10829 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10830 tcg_temp_free_i64(tcg_res[pass]);
10832 } else {
10833 int maxpass = is_q ? 4 : 2;
10834 TCGv_i32 tcg_res[4];
10836 for (pass = 0; pass < maxpass; pass++) {
10837 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10838 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10839 NeonGenTwoOpFn *genfn = NULL;
10840 int passreg = pass < (maxpass / 2) ? rn : rm;
10841 int passelt = (is_q && (pass & 1)) ? 2 : 0;
10843 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
10844 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
10845 tcg_res[pass] = tcg_temp_new_i32();
10847 switch (opcode) {
10848 case 0x17: /* ADDP */
10850 static NeonGenTwoOpFn * const fns[3] = {
10851 gen_helper_neon_padd_u8,
10852 gen_helper_neon_padd_u16,
10853 tcg_gen_add_i32,
10855 genfn = fns[size];
10856 break;
10858 case 0x14: /* SMAXP, UMAXP */
10860 static NeonGenTwoOpFn * const fns[3][2] = {
10861 { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
10862 { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
10863 { tcg_gen_smax_i32, tcg_gen_umax_i32 },
10865 genfn = fns[size][u];
10866 break;
10868 case 0x15: /* SMINP, UMINP */
10870 static NeonGenTwoOpFn * const fns[3][2] = {
10871 { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
10872 { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
10873 { tcg_gen_smin_i32, tcg_gen_umin_i32 },
10875 genfn = fns[size][u];
10876 break;
10878 /* The FP operations are all on single floats (32 bit) */
10879 case 0x58: /* FMAXNMP */
10880 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10881 break;
10882 case 0x5a: /* FADDP */
10883 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10884 break;
10885 case 0x5e: /* FMAXP */
10886 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10887 break;
10888 case 0x78: /* FMINNMP */
10889 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10890 break;
10891 case 0x7e: /* FMINP */
10892 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10893 break;
10894 default:
10895 g_assert_not_reached();
10898 /* FP ops called directly, otherwise call now */
10899 if (genfn) {
10900 genfn(tcg_res[pass], tcg_op1, tcg_op2);
10903 tcg_temp_free_i32(tcg_op1);
10904 tcg_temp_free_i32(tcg_op2);
10907 for (pass = 0; pass < maxpass; pass++) {
10908 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
10909 tcg_temp_free_i32(tcg_res[pass]);
10911 clear_vec_high(s, is_q, rd);
10914 if (fpst) {
10915 tcg_temp_free_ptr(fpst);
10919 /* Floating point op subgroup of C3.6.16. */
10920 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
10922 /* For floating point ops, the U, size[1] and opcode bits
10923 * together indicate the operation. size[0] indicates single
10924 * or double.
10926 int fpopcode = extract32(insn, 11, 5)
10927 | (extract32(insn, 23, 1) << 5)
10928 | (extract32(insn, 29, 1) << 6);
10929 int is_q = extract32(insn, 30, 1);
10930 int size = extract32(insn, 22, 1);
10931 int rm = extract32(insn, 16, 5);
10932 int rn = extract32(insn, 5, 5);
10933 int rd = extract32(insn, 0, 5);
10935 int datasize = is_q ? 128 : 64;
10936 int esize = 32 << size;
10937 int elements = datasize / esize;
10939 if (size == 1 && !is_q) {
10940 unallocated_encoding(s);
10941 return;
10944 switch (fpopcode) {
10945 case 0x58: /* FMAXNMP */
10946 case 0x5a: /* FADDP */
10947 case 0x5e: /* FMAXP */
10948 case 0x78: /* FMINNMP */
10949 case 0x7e: /* FMINP */
10950 if (size && !is_q) {
10951 unallocated_encoding(s);
10952 return;
10954 handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
10955 rn, rm, rd);
10956 return;
10957 case 0x1b: /* FMULX */
10958 case 0x1f: /* FRECPS */
10959 case 0x3f: /* FRSQRTS */
10960 case 0x5d: /* FACGE */
10961 case 0x7d: /* FACGT */
10962 case 0x19: /* FMLA */
10963 case 0x39: /* FMLS */
10964 case 0x18: /* FMAXNM */
10965 case 0x1a: /* FADD */
10966 case 0x1c: /* FCMEQ */
10967 case 0x1e: /* FMAX */
10968 case 0x38: /* FMINNM */
10969 case 0x3a: /* FSUB */
10970 case 0x3e: /* FMIN */
10971 case 0x5b: /* FMUL */
10972 case 0x5c: /* FCMGE */
10973 case 0x5f: /* FDIV */
10974 case 0x7a: /* FABD */
10975 case 0x7c: /* FCMGT */
10976 if (!fp_access_check(s)) {
10977 return;
10979 handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
10980 return;
10982 case 0x1d: /* FMLAL */
10983 case 0x3d: /* FMLSL */
10984 case 0x59: /* FMLAL2 */
10985 case 0x79: /* FMLSL2 */
10986 if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
10987 unallocated_encoding(s);
10988 return;
10990 if (fp_access_check(s)) {
10991 int is_s = extract32(insn, 23, 1);
10992 int is_2 = extract32(insn, 29, 1);
10993 int data = (is_2 << 1) | is_s;
10994 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
10995 vec_full_reg_offset(s, rn),
10996 vec_full_reg_offset(s, rm), cpu_env,
10997 is_q ? 16 : 8, vec_full_reg_size(s),
10998 data, gen_helper_gvec_fmlal_a64);
11000 return;
11002 default:
11003 unallocated_encoding(s);
11004 return;
11008 /* Integer op subgroup of C3.6.16. */
11009 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11011 int is_q = extract32(insn, 30, 1);
11012 int u = extract32(insn, 29, 1);
11013 int size = extract32(insn, 22, 2);
11014 int opcode = extract32(insn, 11, 5);
11015 int rm = extract32(insn, 16, 5);
11016 int rn = extract32(insn, 5, 5);
11017 int rd = extract32(insn, 0, 5);
11018 int pass;
11019 TCGCond cond;
11021 switch (opcode) {
11022 case 0x13: /* MUL, PMUL */
11023 if (u && size != 0) {
11024 unallocated_encoding(s);
11025 return;
11027 /* fall through */
11028 case 0x0: /* SHADD, UHADD */
11029 case 0x2: /* SRHADD, URHADD */
11030 case 0x4: /* SHSUB, UHSUB */
11031 case 0xc: /* SMAX, UMAX */
11032 case 0xd: /* SMIN, UMIN */
11033 case 0xe: /* SABD, UABD */
11034 case 0xf: /* SABA, UABA */
11035 case 0x12: /* MLA, MLS */
11036 if (size == 3) {
11037 unallocated_encoding(s);
11038 return;
11040 break;
11041 case 0x16: /* SQDMULH, SQRDMULH */
11042 if (size == 0 || size == 3) {
11043 unallocated_encoding(s);
11044 return;
11046 break;
11047 default:
11048 if (size == 3 && !is_q) {
11049 unallocated_encoding(s);
11050 return;
11052 break;
11055 if (!fp_access_check(s)) {
11056 return;
11059 switch (opcode) {
11060 case 0x01: /* SQADD, UQADD */
11061 tcg_gen_gvec_4(vec_full_reg_offset(s, rd),
11062 offsetof(CPUARMState, vfp.qc),
11063 vec_full_reg_offset(s, rn),
11064 vec_full_reg_offset(s, rm),
11065 is_q ? 16 : 8, vec_full_reg_size(s),
11066 (u ? uqadd_op : sqadd_op) + size);
11067 return;
11068 case 0x05: /* SQSUB, UQSUB */
11069 tcg_gen_gvec_4(vec_full_reg_offset(s, rd),
11070 offsetof(CPUARMState, vfp.qc),
11071 vec_full_reg_offset(s, rn),
11072 vec_full_reg_offset(s, rm),
11073 is_q ? 16 : 8, vec_full_reg_size(s),
11074 (u ? uqsub_op : sqsub_op) + size);
11075 return;
11076 case 0x0c: /* SMAX, UMAX */
11077 if (u) {
11078 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11079 } else {
11080 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11082 return;
11083 case 0x0d: /* SMIN, UMIN */
11084 if (u) {
11085 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11086 } else {
11087 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11089 return;
11090 case 0x10: /* ADD, SUB */
11091 if (u) {
11092 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11093 } else {
11094 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11096 return;
11097 case 0x13: /* MUL, PMUL */
11098 if (!u) { /* MUL */
11099 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11100 return;
11102 break;
11103 case 0x12: /* MLA, MLS */
11104 if (u) {
11105 gen_gvec_op3(s, is_q, rd, rn, rm, &mls_op[size]);
11106 } else {
11107 gen_gvec_op3(s, is_q, rd, rn, rm, &mla_op[size]);
11109 return;
11110 case 0x11:
11111 if (!u) { /* CMTST */
11112 gen_gvec_op3(s, is_q, rd, rn, rm, &cmtst_op[size]);
11113 return;
11115 /* else CMEQ */
11116 cond = TCG_COND_EQ;
11117 goto do_gvec_cmp;
11118 case 0x06: /* CMGT, CMHI */
11119 cond = u ? TCG_COND_GTU : TCG_COND_GT;
11120 goto do_gvec_cmp;
11121 case 0x07: /* CMGE, CMHS */
11122 cond = u ? TCG_COND_GEU : TCG_COND_GE;
11123 do_gvec_cmp:
11124 tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11125 vec_full_reg_offset(s, rn),
11126 vec_full_reg_offset(s, rm),
11127 is_q ? 16 : 8, vec_full_reg_size(s));
11128 return;
11131 if (size == 3) {
11132 assert(is_q);
11133 for (pass = 0; pass < 2; pass++) {
11134 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11135 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11136 TCGv_i64 tcg_res = tcg_temp_new_i64();
11138 read_vec_element(s, tcg_op1, rn, pass, MO_64);
11139 read_vec_element(s, tcg_op2, rm, pass, MO_64);
11141 handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11143 write_vec_element(s, tcg_res, rd, pass, MO_64);
11145 tcg_temp_free_i64(tcg_res);
11146 tcg_temp_free_i64(tcg_op1);
11147 tcg_temp_free_i64(tcg_op2);
11149 } else {
11150 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11151 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11152 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11153 TCGv_i32 tcg_res = tcg_temp_new_i32();
11154 NeonGenTwoOpFn *genfn = NULL;
11155 NeonGenTwoOpEnvFn *genenvfn = NULL;
11157 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11158 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11160 switch (opcode) {
11161 case 0x0: /* SHADD, UHADD */
11163 static NeonGenTwoOpFn * const fns[3][2] = {
11164 { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11165 { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11166 { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11168 genfn = fns[size][u];
11169 break;
11171 case 0x2: /* SRHADD, URHADD */
11173 static NeonGenTwoOpFn * const fns[3][2] = {
11174 { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11175 { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11176 { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11178 genfn = fns[size][u];
11179 break;
11181 case 0x4: /* SHSUB, UHSUB */
11183 static NeonGenTwoOpFn * const fns[3][2] = {
11184 { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11185 { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11186 { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11188 genfn = fns[size][u];
11189 break;
11191 case 0x8: /* SSHL, USHL */
11193 static NeonGenTwoOpFn * const fns[3][2] = {
11194 { gen_helper_neon_shl_s8, gen_helper_neon_shl_u8 },
11195 { gen_helper_neon_shl_s16, gen_helper_neon_shl_u16 },
11196 { gen_helper_neon_shl_s32, gen_helper_neon_shl_u32 },
11198 genfn = fns[size][u];
11199 break;
11201 case 0x9: /* SQSHL, UQSHL */
11203 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11204 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11205 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11206 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11208 genenvfn = fns[size][u];
11209 break;
11211 case 0xa: /* SRSHL, URSHL */
11213 static NeonGenTwoOpFn * const fns[3][2] = {
11214 { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11215 { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11216 { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11218 genfn = fns[size][u];
11219 break;
11221 case 0xb: /* SQRSHL, UQRSHL */
11223 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11224 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11225 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11226 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11228 genenvfn = fns[size][u];
11229 break;
11231 case 0xe: /* SABD, UABD */
11232 case 0xf: /* SABA, UABA */
11234 static NeonGenTwoOpFn * const fns[3][2] = {
11235 { gen_helper_neon_abd_s8, gen_helper_neon_abd_u8 },
11236 { gen_helper_neon_abd_s16, gen_helper_neon_abd_u16 },
11237 { gen_helper_neon_abd_s32, gen_helper_neon_abd_u32 },
11239 genfn = fns[size][u];
11240 break;
11242 case 0x13: /* MUL, PMUL */
11243 assert(u); /* PMUL */
11244 assert(size == 0);
11245 genfn = gen_helper_neon_mul_p8;
11246 break;
11247 case 0x16: /* SQDMULH, SQRDMULH */
11249 static NeonGenTwoOpEnvFn * const fns[2][2] = {
11250 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
11251 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
11253 assert(size == 1 || size == 2);
11254 genenvfn = fns[size - 1][u];
11255 break;
11257 default:
11258 g_assert_not_reached();
11261 if (genenvfn) {
11262 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11263 } else {
11264 genfn(tcg_res, tcg_op1, tcg_op2);
11267 if (opcode == 0xf) {
11268 /* SABA, UABA: accumulating ops */
11269 static NeonGenTwoOpFn * const fns[3] = {
11270 gen_helper_neon_add_u8,
11271 gen_helper_neon_add_u16,
11272 tcg_gen_add_i32,
11275 read_vec_element_i32(s, tcg_op1, rd, pass, MO_32);
11276 fns[size](tcg_res, tcg_op1, tcg_res);
11279 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11281 tcg_temp_free_i32(tcg_res);
11282 tcg_temp_free_i32(tcg_op1);
11283 tcg_temp_free_i32(tcg_op2);
11286 clear_vec_high(s, is_q, rd);
11289 /* AdvSIMD three same
11290 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
11291 * +---+---+---+-----------+------+---+------+--------+---+------+------+
11292 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
11293 * +---+---+---+-----------+------+---+------+--------+---+------+------+
11295 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11297 int opcode = extract32(insn, 11, 5);
11299 switch (opcode) {
11300 case 0x3: /* logic ops */
11301 disas_simd_3same_logic(s, insn);
11302 break;
11303 case 0x17: /* ADDP */
11304 case 0x14: /* SMAXP, UMAXP */
11305 case 0x15: /* SMINP, UMINP */
11307 /* Pairwise operations */
11308 int is_q = extract32(insn, 30, 1);
11309 int u = extract32(insn, 29, 1);
11310 int size = extract32(insn, 22, 2);
11311 int rm = extract32(insn, 16, 5);
11312 int rn = extract32(insn, 5, 5);
11313 int rd = extract32(insn, 0, 5);
11314 if (opcode == 0x17) {
11315 if (u || (size == 3 && !is_q)) {
11316 unallocated_encoding(s);
11317 return;
11319 } else {
11320 if (size == 3) {
11321 unallocated_encoding(s);
11322 return;
11325 handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11326 break;
11328 case 0x18 ... 0x31:
11329 /* floating point ops, sz[1] and U are part of opcode */
11330 disas_simd_3same_float(s, insn);
11331 break;
11332 default:
11333 disas_simd_3same_int(s, insn);
11334 break;
11339 * Advanced SIMD three same (ARMv8.2 FP16 variants)
11341 * 31 30 29 28 24 23 22 21 20 16 15 14 13 11 10 9 5 4 0
11342 * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11343 * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 | Rm | 0 0 | opcode | 1 | Rn | Rd |
11344 * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11346 * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11347 * (register), FACGE, FABD, FCMGT (register) and FACGT.
11350 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11352 int opcode, fpopcode;
11353 int is_q, u, a, rm, rn, rd;
11354 int datasize, elements;
11355 int pass;
11356 TCGv_ptr fpst;
11357 bool pairwise = false;
11359 if (!dc_isar_feature(aa64_fp16, s)) {
11360 unallocated_encoding(s);
11361 return;
11364 if (!fp_access_check(s)) {
11365 return;
11368 /* For these floating point ops, the U, a and opcode bits
11369 * together indicate the operation.
11371 opcode = extract32(insn, 11, 3);
11372 u = extract32(insn, 29, 1);
11373 a = extract32(insn, 23, 1);
11374 is_q = extract32(insn, 30, 1);
11375 rm = extract32(insn, 16, 5);
11376 rn = extract32(insn, 5, 5);
11377 rd = extract32(insn, 0, 5);
11379 fpopcode = opcode | (a << 3) | (u << 4);
11380 datasize = is_q ? 128 : 64;
11381 elements = datasize / 16;
11383 switch (fpopcode) {
11384 case 0x10: /* FMAXNMP */
11385 case 0x12: /* FADDP */
11386 case 0x16: /* FMAXP */
11387 case 0x18: /* FMINNMP */
11388 case 0x1e: /* FMINP */
11389 pairwise = true;
11390 break;
11393 fpst = get_fpstatus_ptr(true);
11395 if (pairwise) {
11396 int maxpass = is_q ? 8 : 4;
11397 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11398 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11399 TCGv_i32 tcg_res[8];
11401 for (pass = 0; pass < maxpass; pass++) {
11402 int passreg = pass < (maxpass / 2) ? rn : rm;
11403 int passelt = (pass << 1) & (maxpass - 1);
11405 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11406 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11407 tcg_res[pass] = tcg_temp_new_i32();
11409 switch (fpopcode) {
11410 case 0x10: /* FMAXNMP */
11411 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11412 fpst);
11413 break;
11414 case 0x12: /* FADDP */
11415 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11416 break;
11417 case 0x16: /* FMAXP */
11418 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11419 break;
11420 case 0x18: /* FMINNMP */
11421 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11422 fpst);
11423 break;
11424 case 0x1e: /* FMINP */
11425 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11426 break;
11427 default:
11428 g_assert_not_reached();
11432 for (pass = 0; pass < maxpass; pass++) {
11433 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11434 tcg_temp_free_i32(tcg_res[pass]);
11437 tcg_temp_free_i32(tcg_op1);
11438 tcg_temp_free_i32(tcg_op2);
11440 } else {
11441 for (pass = 0; pass < elements; pass++) {
11442 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11443 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11444 TCGv_i32 tcg_res = tcg_temp_new_i32();
11446 read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11447 read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11449 switch (fpopcode) {
11450 case 0x0: /* FMAXNM */
11451 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11452 break;
11453 case 0x1: /* FMLA */
11454 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11455 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11456 fpst);
11457 break;
11458 case 0x2: /* FADD */
11459 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11460 break;
11461 case 0x3: /* FMULX */
11462 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11463 break;
11464 case 0x4: /* FCMEQ */
11465 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11466 break;
11467 case 0x6: /* FMAX */
11468 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11469 break;
11470 case 0x7: /* FRECPS */
11471 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11472 break;
11473 case 0x8: /* FMINNM */
11474 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11475 break;
11476 case 0x9: /* FMLS */
11477 /* As usual for ARM, separate negation for fused multiply-add */
11478 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11479 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11480 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11481 fpst);
11482 break;
11483 case 0xa: /* FSUB */
11484 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11485 break;
11486 case 0xe: /* FMIN */
11487 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11488 break;
11489 case 0xf: /* FRSQRTS */
11490 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11491 break;
11492 case 0x13: /* FMUL */
11493 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11494 break;
11495 case 0x14: /* FCMGE */
11496 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11497 break;
11498 case 0x15: /* FACGE */
11499 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11500 break;
11501 case 0x17: /* FDIV */
11502 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11503 break;
11504 case 0x1a: /* FABD */
11505 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11506 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11507 break;
11508 case 0x1c: /* FCMGT */
11509 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11510 break;
11511 case 0x1d: /* FACGT */
11512 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11513 break;
11514 default:
11515 fprintf(stderr, "%s: insn %#04x, fpop %#2x @ %#" PRIx64 "\n",
11516 __func__, insn, fpopcode, s->pc_curr);
11517 g_assert_not_reached();
11520 write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11521 tcg_temp_free_i32(tcg_res);
11522 tcg_temp_free_i32(tcg_op1);
11523 tcg_temp_free_i32(tcg_op2);
11527 tcg_temp_free_ptr(fpst);
11529 clear_vec_high(s, is_q, rd);
11532 /* AdvSIMD three same extra
11533 * 31 30 29 28 24 23 22 21 20 16 15 14 11 10 9 5 4 0
11534 * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11535 * | 0 | Q | U | 0 1 1 1 0 | size | 0 | Rm | 1 | opcode | 1 | Rn | Rd |
11536 * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11538 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11540 int rd = extract32(insn, 0, 5);
11541 int rn = extract32(insn, 5, 5);
11542 int opcode = extract32(insn, 11, 4);
11543 int rm = extract32(insn, 16, 5);
11544 int size = extract32(insn, 22, 2);
11545 bool u = extract32(insn, 29, 1);
11546 bool is_q = extract32(insn, 30, 1);
11547 bool feature;
11548 int rot;
11550 switch (u * 16 + opcode) {
11551 case 0x10: /* SQRDMLAH (vector) */
11552 case 0x11: /* SQRDMLSH (vector) */
11553 if (size != 1 && size != 2) {
11554 unallocated_encoding(s);
11555 return;
11557 feature = dc_isar_feature(aa64_rdm, s);
11558 break;
11559 case 0x02: /* SDOT (vector) */
11560 case 0x12: /* UDOT (vector) */
11561 if (size != MO_32) {
11562 unallocated_encoding(s);
11563 return;
11565 feature = dc_isar_feature(aa64_dp, s);
11566 break;
11567 case 0x18: /* FCMLA, #0 */
11568 case 0x19: /* FCMLA, #90 */
11569 case 0x1a: /* FCMLA, #180 */
11570 case 0x1b: /* FCMLA, #270 */
11571 case 0x1c: /* FCADD, #90 */
11572 case 0x1e: /* FCADD, #270 */
11573 if (size == 0
11574 || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11575 || (size == 3 && !is_q)) {
11576 unallocated_encoding(s);
11577 return;
11579 feature = dc_isar_feature(aa64_fcma, s);
11580 break;
11581 default:
11582 unallocated_encoding(s);
11583 return;
11585 if (!feature) {
11586 unallocated_encoding(s);
11587 return;
11589 if (!fp_access_check(s)) {
11590 return;
11593 switch (opcode) {
11594 case 0x0: /* SQRDMLAH (vector) */
11595 switch (size) {
11596 case 1:
11597 gen_gvec_op3_env(s, is_q, rd, rn, rm, gen_helper_gvec_qrdmlah_s16);
11598 break;
11599 case 2:
11600 gen_gvec_op3_env(s, is_q, rd, rn, rm, gen_helper_gvec_qrdmlah_s32);
11601 break;
11602 default:
11603 g_assert_not_reached();
11605 return;
11607 case 0x1: /* SQRDMLSH (vector) */
11608 switch (size) {
11609 case 1:
11610 gen_gvec_op3_env(s, is_q, rd, rn, rm, gen_helper_gvec_qrdmlsh_s16);
11611 break;
11612 case 2:
11613 gen_gvec_op3_env(s, is_q, rd, rn, rm, gen_helper_gvec_qrdmlsh_s32);
11614 break;
11615 default:
11616 g_assert_not_reached();
11618 return;
11620 case 0x2: /* SDOT / UDOT */
11621 gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0,
11622 u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11623 return;
11625 case 0x8: /* FCMLA, #0 */
11626 case 0x9: /* FCMLA, #90 */
11627 case 0xa: /* FCMLA, #180 */
11628 case 0xb: /* FCMLA, #270 */
11629 rot = extract32(opcode, 0, 2);
11630 switch (size) {
11631 case 1:
11632 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, true, rot,
11633 gen_helper_gvec_fcmlah);
11634 break;
11635 case 2:
11636 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, false, rot,
11637 gen_helper_gvec_fcmlas);
11638 break;
11639 case 3:
11640 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, false, rot,
11641 gen_helper_gvec_fcmlad);
11642 break;
11643 default:
11644 g_assert_not_reached();
11646 return;
11648 case 0xc: /* FCADD, #90 */
11649 case 0xe: /* FCADD, #270 */
11650 rot = extract32(opcode, 1, 1);
11651 switch (size) {
11652 case 1:
11653 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11654 gen_helper_gvec_fcaddh);
11655 break;
11656 case 2:
11657 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11658 gen_helper_gvec_fcadds);
11659 break;
11660 case 3:
11661 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11662 gen_helper_gvec_fcaddd);
11663 break;
11664 default:
11665 g_assert_not_reached();
11667 return;
11669 default:
11670 g_assert_not_reached();
11674 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11675 int size, int rn, int rd)
11677 /* Handle 2-reg-misc ops which are widening (so each size element
11678 * in the source becomes a 2*size element in the destination.
11679 * The only instruction like this is FCVTL.
11681 int pass;
11683 if (size == 3) {
11684 /* 32 -> 64 bit fp conversion */
11685 TCGv_i64 tcg_res[2];
11686 int srcelt = is_q ? 2 : 0;
11688 for (pass = 0; pass < 2; pass++) {
11689 TCGv_i32 tcg_op = tcg_temp_new_i32();
11690 tcg_res[pass] = tcg_temp_new_i64();
11692 read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11693 gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
11694 tcg_temp_free_i32(tcg_op);
11696 for (pass = 0; pass < 2; pass++) {
11697 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11698 tcg_temp_free_i64(tcg_res[pass]);
11700 } else {
11701 /* 16 -> 32 bit fp conversion */
11702 int srcelt = is_q ? 4 : 0;
11703 TCGv_i32 tcg_res[4];
11704 TCGv_ptr fpst = get_fpstatus_ptr(false);
11705 TCGv_i32 ahp = get_ahp_flag();
11707 for (pass = 0; pass < 4; pass++) {
11708 tcg_res[pass] = tcg_temp_new_i32();
11710 read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11711 gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11712 fpst, ahp);
11714 for (pass = 0; pass < 4; pass++) {
11715 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11716 tcg_temp_free_i32(tcg_res[pass]);
11719 tcg_temp_free_ptr(fpst);
11720 tcg_temp_free_i32(ahp);
11724 static void handle_rev(DisasContext *s, int opcode, bool u,
11725 bool is_q, int size, int rn, int rd)
11727 int op = (opcode << 1) | u;
11728 int opsz = op + size;
11729 int grp_size = 3 - opsz;
11730 int dsize = is_q ? 128 : 64;
11731 int i;
11733 if (opsz >= 3) {
11734 unallocated_encoding(s);
11735 return;
11738 if (!fp_access_check(s)) {
11739 return;
11742 if (size == 0) {
11743 /* Special case bytes, use bswap op on each group of elements */
11744 int groups = dsize / (8 << grp_size);
11746 for (i = 0; i < groups; i++) {
11747 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11749 read_vec_element(s, tcg_tmp, rn, i, grp_size);
11750 switch (grp_size) {
11751 case MO_16:
11752 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
11753 break;
11754 case MO_32:
11755 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
11756 break;
11757 case MO_64:
11758 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11759 break;
11760 default:
11761 g_assert_not_reached();
11763 write_vec_element(s, tcg_tmp, rd, i, grp_size);
11764 tcg_temp_free_i64(tcg_tmp);
11766 clear_vec_high(s, is_q, rd);
11767 } else {
11768 int revmask = (1 << grp_size) - 1;
11769 int esize = 8 << size;
11770 int elements = dsize / esize;
11771 TCGv_i64 tcg_rn = tcg_temp_new_i64();
11772 TCGv_i64 tcg_rd = tcg_const_i64(0);
11773 TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
11775 for (i = 0; i < elements; i++) {
11776 int e_rev = (i & 0xf) ^ revmask;
11777 int off = e_rev * esize;
11778 read_vec_element(s, tcg_rn, rn, i, size);
11779 if (off >= 64) {
11780 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
11781 tcg_rn, off - 64, esize);
11782 } else {
11783 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
11786 write_vec_element(s, tcg_rd, rd, 0, MO_64);
11787 write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
11789 tcg_temp_free_i64(tcg_rd_hi);
11790 tcg_temp_free_i64(tcg_rd);
11791 tcg_temp_free_i64(tcg_rn);
11795 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
11796 bool is_q, int size, int rn, int rd)
11798 /* Implement the pairwise operations from 2-misc:
11799 * SADDLP, UADDLP, SADALP, UADALP.
11800 * These all add pairs of elements in the input to produce a
11801 * double-width result element in the output (possibly accumulating).
11803 bool accum = (opcode == 0x6);
11804 int maxpass = is_q ? 2 : 1;
11805 int pass;
11806 TCGv_i64 tcg_res[2];
11808 if (size == 2) {
11809 /* 32 + 32 -> 64 op */
11810 TCGMemOp memop = size + (u ? 0 : MO_SIGN);
11812 for (pass = 0; pass < maxpass; pass++) {
11813 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11814 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11816 tcg_res[pass] = tcg_temp_new_i64();
11818 read_vec_element(s, tcg_op1, rn, pass * 2, memop);
11819 read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
11820 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11821 if (accum) {
11822 read_vec_element(s, tcg_op1, rd, pass, MO_64);
11823 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
11826 tcg_temp_free_i64(tcg_op1);
11827 tcg_temp_free_i64(tcg_op2);
11829 } else {
11830 for (pass = 0; pass < maxpass; pass++) {
11831 TCGv_i64 tcg_op = tcg_temp_new_i64();
11832 NeonGenOneOpFn *genfn;
11833 static NeonGenOneOpFn * const fns[2][2] = {
11834 { gen_helper_neon_addlp_s8, gen_helper_neon_addlp_u8 },
11835 { gen_helper_neon_addlp_s16, gen_helper_neon_addlp_u16 },
11838 genfn = fns[size][u];
11840 tcg_res[pass] = tcg_temp_new_i64();
11842 read_vec_element(s, tcg_op, rn, pass, MO_64);
11843 genfn(tcg_res[pass], tcg_op);
11845 if (accum) {
11846 read_vec_element(s, tcg_op, rd, pass, MO_64);
11847 if (size == 0) {
11848 gen_helper_neon_addl_u16(tcg_res[pass],
11849 tcg_res[pass], tcg_op);
11850 } else {
11851 gen_helper_neon_addl_u32(tcg_res[pass],
11852 tcg_res[pass], tcg_op);
11855 tcg_temp_free_i64(tcg_op);
11858 if (!is_q) {
11859 tcg_res[1] = tcg_const_i64(0);
11861 for (pass = 0; pass < 2; pass++) {
11862 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11863 tcg_temp_free_i64(tcg_res[pass]);
11867 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
11869 /* Implement SHLL and SHLL2 */
11870 int pass;
11871 int part = is_q ? 2 : 0;
11872 TCGv_i64 tcg_res[2];
11874 for (pass = 0; pass < 2; pass++) {
11875 static NeonGenWidenFn * const widenfns[3] = {
11876 gen_helper_neon_widen_u8,
11877 gen_helper_neon_widen_u16,
11878 tcg_gen_extu_i32_i64,
11880 NeonGenWidenFn *widenfn = widenfns[size];
11881 TCGv_i32 tcg_op = tcg_temp_new_i32();
11883 read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
11884 tcg_res[pass] = tcg_temp_new_i64();
11885 widenfn(tcg_res[pass], tcg_op);
11886 tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
11888 tcg_temp_free_i32(tcg_op);
11891 for (pass = 0; pass < 2; pass++) {
11892 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11893 tcg_temp_free_i64(tcg_res[pass]);
11897 /* AdvSIMD two reg misc
11898 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
11899 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11900 * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
11901 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11903 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
11905 int size = extract32(insn, 22, 2);
11906 int opcode = extract32(insn, 12, 5);
11907 bool u = extract32(insn, 29, 1);
11908 bool is_q = extract32(insn, 30, 1);
11909 int rn = extract32(insn, 5, 5);
11910 int rd = extract32(insn, 0, 5);
11911 bool need_fpstatus = false;
11912 bool need_rmode = false;
11913 int rmode = -1;
11914 TCGv_i32 tcg_rmode;
11915 TCGv_ptr tcg_fpstatus;
11917 switch (opcode) {
11918 case 0x0: /* REV64, REV32 */
11919 case 0x1: /* REV16 */
11920 handle_rev(s, opcode, u, is_q, size, rn, rd);
11921 return;
11922 case 0x5: /* CNT, NOT, RBIT */
11923 if (u && size == 0) {
11924 /* NOT */
11925 break;
11926 } else if (u && size == 1) {
11927 /* RBIT */
11928 break;
11929 } else if (!u && size == 0) {
11930 /* CNT */
11931 break;
11933 unallocated_encoding(s);
11934 return;
11935 case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
11936 case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
11937 if (size == 3) {
11938 unallocated_encoding(s);
11939 return;
11941 if (!fp_access_check(s)) {
11942 return;
11945 handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
11946 return;
11947 case 0x4: /* CLS, CLZ */
11948 if (size == 3) {
11949 unallocated_encoding(s);
11950 return;
11952 break;
11953 case 0x2: /* SADDLP, UADDLP */
11954 case 0x6: /* SADALP, UADALP */
11955 if (size == 3) {
11956 unallocated_encoding(s);
11957 return;
11959 if (!fp_access_check(s)) {
11960 return;
11962 handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
11963 return;
11964 case 0x13: /* SHLL, SHLL2 */
11965 if (u == 0 || size == 3) {
11966 unallocated_encoding(s);
11967 return;
11969 if (!fp_access_check(s)) {
11970 return;
11972 handle_shll(s, is_q, size, rn, rd);
11973 return;
11974 case 0xa: /* CMLT */
11975 if (u == 1) {
11976 unallocated_encoding(s);
11977 return;
11979 /* fall through */
11980 case 0x8: /* CMGT, CMGE */
11981 case 0x9: /* CMEQ, CMLE */
11982 case 0xb: /* ABS, NEG */
11983 if (size == 3 && !is_q) {
11984 unallocated_encoding(s);
11985 return;
11987 break;
11988 case 0x3: /* SUQADD, USQADD */
11989 if (size == 3 && !is_q) {
11990 unallocated_encoding(s);
11991 return;
11993 if (!fp_access_check(s)) {
11994 return;
11996 handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
11997 return;
11998 case 0x7: /* SQABS, SQNEG */
11999 if (size == 3 && !is_q) {
12000 unallocated_encoding(s);
12001 return;
12003 break;
12004 case 0xc ... 0xf:
12005 case 0x16 ... 0x1f:
12007 /* Floating point: U, size[1] and opcode indicate operation;
12008 * size[0] indicates single or double precision.
12010 int is_double = extract32(size, 0, 1);
12011 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12012 size = is_double ? 3 : 2;
12013 switch (opcode) {
12014 case 0x2f: /* FABS */
12015 case 0x6f: /* FNEG */
12016 if (size == 3 && !is_q) {
12017 unallocated_encoding(s);
12018 return;
12020 break;
12021 case 0x1d: /* SCVTF */
12022 case 0x5d: /* UCVTF */
12024 bool is_signed = (opcode == 0x1d) ? true : false;
12025 int elements = is_double ? 2 : is_q ? 4 : 2;
12026 if (is_double && !is_q) {
12027 unallocated_encoding(s);
12028 return;
12030 if (!fp_access_check(s)) {
12031 return;
12033 handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12034 return;
12036 case 0x2c: /* FCMGT (zero) */
12037 case 0x2d: /* FCMEQ (zero) */
12038 case 0x2e: /* FCMLT (zero) */
12039 case 0x6c: /* FCMGE (zero) */
12040 case 0x6d: /* FCMLE (zero) */
12041 if (size == 3 && !is_q) {
12042 unallocated_encoding(s);
12043 return;
12045 handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12046 return;
12047 case 0x7f: /* FSQRT */
12048 if (size == 3 && !is_q) {
12049 unallocated_encoding(s);
12050 return;
12052 break;
12053 case 0x1a: /* FCVTNS */
12054 case 0x1b: /* FCVTMS */
12055 case 0x3a: /* FCVTPS */
12056 case 0x3b: /* FCVTZS */
12057 case 0x5a: /* FCVTNU */
12058 case 0x5b: /* FCVTMU */
12059 case 0x7a: /* FCVTPU */
12060 case 0x7b: /* FCVTZU */
12061 need_fpstatus = true;
12062 need_rmode = true;
12063 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12064 if (size == 3 && !is_q) {
12065 unallocated_encoding(s);
12066 return;
12068 break;
12069 case 0x5c: /* FCVTAU */
12070 case 0x1c: /* FCVTAS */
12071 need_fpstatus = true;
12072 need_rmode = true;
12073 rmode = FPROUNDING_TIEAWAY;
12074 if (size == 3 && !is_q) {
12075 unallocated_encoding(s);
12076 return;
12078 break;
12079 case 0x3c: /* URECPE */
12080 if (size == 3) {
12081 unallocated_encoding(s);
12082 return;
12084 /* fall through */
12085 case 0x3d: /* FRECPE */
12086 case 0x7d: /* FRSQRTE */
12087 if (size == 3 && !is_q) {
12088 unallocated_encoding(s);
12089 return;
12091 if (!fp_access_check(s)) {
12092 return;
12094 handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12095 return;
12096 case 0x56: /* FCVTXN, FCVTXN2 */
12097 if (size == 2) {
12098 unallocated_encoding(s);
12099 return;
12101 /* fall through */
12102 case 0x16: /* FCVTN, FCVTN2 */
12103 /* handle_2misc_narrow does a 2*size -> size operation, but these
12104 * instructions encode the source size rather than dest size.
12106 if (!fp_access_check(s)) {
12107 return;
12109 handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12110 return;
12111 case 0x17: /* FCVTL, FCVTL2 */
12112 if (!fp_access_check(s)) {
12113 return;
12115 handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12116 return;
12117 case 0x18: /* FRINTN */
12118 case 0x19: /* FRINTM */
12119 case 0x38: /* FRINTP */
12120 case 0x39: /* FRINTZ */
12121 need_rmode = true;
12122 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12123 /* fall through */
12124 case 0x59: /* FRINTX */
12125 case 0x79: /* FRINTI */
12126 need_fpstatus = true;
12127 if (size == 3 && !is_q) {
12128 unallocated_encoding(s);
12129 return;
12131 break;
12132 case 0x58: /* FRINTA */
12133 need_rmode = true;
12134 rmode = FPROUNDING_TIEAWAY;
12135 need_fpstatus = true;
12136 if (size == 3 && !is_q) {
12137 unallocated_encoding(s);
12138 return;
12140 break;
12141 case 0x7c: /* URSQRTE */
12142 if (size == 3) {
12143 unallocated_encoding(s);
12144 return;
12146 need_fpstatus = true;
12147 break;
12148 case 0x1e: /* FRINT32Z */
12149 case 0x1f: /* FRINT64Z */
12150 need_rmode = true;
12151 rmode = FPROUNDING_ZERO;
12152 /* fall through */
12153 case 0x5e: /* FRINT32X */
12154 case 0x5f: /* FRINT64X */
12155 need_fpstatus = true;
12156 if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12157 unallocated_encoding(s);
12158 return;
12160 break;
12161 default:
12162 unallocated_encoding(s);
12163 return;
12165 break;
12167 default:
12168 unallocated_encoding(s);
12169 return;
12172 if (!fp_access_check(s)) {
12173 return;
12176 if (need_fpstatus || need_rmode) {
12177 tcg_fpstatus = get_fpstatus_ptr(false);
12178 } else {
12179 tcg_fpstatus = NULL;
12181 if (need_rmode) {
12182 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
12183 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12184 } else {
12185 tcg_rmode = NULL;
12188 switch (opcode) {
12189 case 0x5:
12190 if (u && size == 0) { /* NOT */
12191 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12192 return;
12194 break;
12195 case 0xb:
12196 if (u) { /* ABS, NEG */
12197 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12198 } else {
12199 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12201 return;
12204 if (size == 3) {
12205 /* All 64-bit element operations can be shared with scalar 2misc */
12206 int pass;
12208 /* Coverity claims (size == 3 && !is_q) has been eliminated
12209 * from all paths leading to here.
12211 tcg_debug_assert(is_q);
12212 for (pass = 0; pass < 2; pass++) {
12213 TCGv_i64 tcg_op = tcg_temp_new_i64();
12214 TCGv_i64 tcg_res = tcg_temp_new_i64();
12216 read_vec_element(s, tcg_op, rn, pass, MO_64);
12218 handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12219 tcg_rmode, tcg_fpstatus);
12221 write_vec_element(s, tcg_res, rd, pass, MO_64);
12223 tcg_temp_free_i64(tcg_res);
12224 tcg_temp_free_i64(tcg_op);
12226 } else {
12227 int pass;
12229 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12230 TCGv_i32 tcg_op = tcg_temp_new_i32();
12231 TCGv_i32 tcg_res = tcg_temp_new_i32();
12232 TCGCond cond;
12234 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12236 if (size == 2) {
12237 /* Special cases for 32 bit elements */
12238 switch (opcode) {
12239 case 0xa: /* CMLT */
12240 /* 32 bit integer comparison against zero, result is
12241 * test ? (2^32 - 1) : 0. We implement via setcond(test)
12242 * and inverting.
12244 cond = TCG_COND_LT;
12245 do_cmop:
12246 tcg_gen_setcondi_i32(cond, tcg_res, tcg_op, 0);
12247 tcg_gen_neg_i32(tcg_res, tcg_res);
12248 break;
12249 case 0x8: /* CMGT, CMGE */
12250 cond = u ? TCG_COND_GE : TCG_COND_GT;
12251 goto do_cmop;
12252 case 0x9: /* CMEQ, CMLE */
12253 cond = u ? TCG_COND_LE : TCG_COND_EQ;
12254 goto do_cmop;
12255 case 0x4: /* CLS */
12256 if (u) {
12257 tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12258 } else {
12259 tcg_gen_clrsb_i32(tcg_res, tcg_op);
12261 break;
12262 case 0x7: /* SQABS, SQNEG */
12263 if (u) {
12264 gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12265 } else {
12266 gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12268 break;
12269 case 0x2f: /* FABS */
12270 gen_helper_vfp_abss(tcg_res, tcg_op);
12271 break;
12272 case 0x6f: /* FNEG */
12273 gen_helper_vfp_negs(tcg_res, tcg_op);
12274 break;
12275 case 0x7f: /* FSQRT */
12276 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12277 break;
12278 case 0x1a: /* FCVTNS */
12279 case 0x1b: /* FCVTMS */
12280 case 0x1c: /* FCVTAS */
12281 case 0x3a: /* FCVTPS */
12282 case 0x3b: /* FCVTZS */
12284 TCGv_i32 tcg_shift = tcg_const_i32(0);
12285 gen_helper_vfp_tosls(tcg_res, tcg_op,
12286 tcg_shift, tcg_fpstatus);
12287 tcg_temp_free_i32(tcg_shift);
12288 break;
12290 case 0x5a: /* FCVTNU */
12291 case 0x5b: /* FCVTMU */
12292 case 0x5c: /* FCVTAU */
12293 case 0x7a: /* FCVTPU */
12294 case 0x7b: /* FCVTZU */
12296 TCGv_i32 tcg_shift = tcg_const_i32(0);
12297 gen_helper_vfp_touls(tcg_res, tcg_op,
12298 tcg_shift, tcg_fpstatus);
12299 tcg_temp_free_i32(tcg_shift);
12300 break;
12302 case 0x18: /* FRINTN */
12303 case 0x19: /* FRINTM */
12304 case 0x38: /* FRINTP */
12305 case 0x39: /* FRINTZ */
12306 case 0x58: /* FRINTA */
12307 case 0x79: /* FRINTI */
12308 gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12309 break;
12310 case 0x59: /* FRINTX */
12311 gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12312 break;
12313 case 0x7c: /* URSQRTE */
12314 gen_helper_rsqrte_u32(tcg_res, tcg_op, tcg_fpstatus);
12315 break;
12316 case 0x1e: /* FRINT32Z */
12317 case 0x5e: /* FRINT32X */
12318 gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12319 break;
12320 case 0x1f: /* FRINT64Z */
12321 case 0x5f: /* FRINT64X */
12322 gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12323 break;
12324 default:
12325 g_assert_not_reached();
12327 } else {
12328 /* Use helpers for 8 and 16 bit elements */
12329 switch (opcode) {
12330 case 0x5: /* CNT, RBIT */
12331 /* For these two insns size is part of the opcode specifier
12332 * (handled earlier); they always operate on byte elements.
12334 if (u) {
12335 gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12336 } else {
12337 gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12339 break;
12340 case 0x7: /* SQABS, SQNEG */
12342 NeonGenOneOpEnvFn *genfn;
12343 static NeonGenOneOpEnvFn * const fns[2][2] = {
12344 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12345 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12347 genfn = fns[size][u];
12348 genfn(tcg_res, cpu_env, tcg_op);
12349 break;
12351 case 0x8: /* CMGT, CMGE */
12352 case 0x9: /* CMEQ, CMLE */
12353 case 0xa: /* CMLT */
12355 static NeonGenTwoOpFn * const fns[3][2] = {
12356 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_s16 },
12357 { gen_helper_neon_cge_s8, gen_helper_neon_cge_s16 },
12358 { gen_helper_neon_ceq_u8, gen_helper_neon_ceq_u16 },
12360 NeonGenTwoOpFn *genfn;
12361 int comp;
12362 bool reverse;
12363 TCGv_i32 tcg_zero = tcg_const_i32(0);
12365 /* comp = index into [CMGT, CMGE, CMEQ, CMLE, CMLT] */
12366 comp = (opcode - 0x8) * 2 + u;
12367 /* ...but LE, LT are implemented as reverse GE, GT */
12368 reverse = (comp > 2);
12369 if (reverse) {
12370 comp = 4 - comp;
12372 genfn = fns[comp][size];
12373 if (reverse) {
12374 genfn(tcg_res, tcg_zero, tcg_op);
12375 } else {
12376 genfn(tcg_res, tcg_op, tcg_zero);
12378 tcg_temp_free_i32(tcg_zero);
12379 break;
12381 case 0x4: /* CLS, CLZ */
12382 if (u) {
12383 if (size == 0) {
12384 gen_helper_neon_clz_u8(tcg_res, tcg_op);
12385 } else {
12386 gen_helper_neon_clz_u16(tcg_res, tcg_op);
12388 } else {
12389 if (size == 0) {
12390 gen_helper_neon_cls_s8(tcg_res, tcg_op);
12391 } else {
12392 gen_helper_neon_cls_s16(tcg_res, tcg_op);
12395 break;
12396 default:
12397 g_assert_not_reached();
12401 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12403 tcg_temp_free_i32(tcg_res);
12404 tcg_temp_free_i32(tcg_op);
12407 clear_vec_high(s, is_q, rd);
12409 if (need_rmode) {
12410 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12411 tcg_temp_free_i32(tcg_rmode);
12413 if (need_fpstatus) {
12414 tcg_temp_free_ptr(tcg_fpstatus);
12418 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12420 * 31 30 29 28 27 24 23 22 21 17 16 12 11 10 9 5 4 0
12421 * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12422 * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 | Rn | Rd |
12423 * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12424 * mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12425 * val: 0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12427 * This actually covers two groups where scalar access is governed by
12428 * bit 28. A bunch of the instructions (float to integral) only exist
12429 * in the vector form and are un-allocated for the scalar decode. Also
12430 * in the scalar decode Q is always 1.
12432 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12434 int fpop, opcode, a, u;
12435 int rn, rd;
12436 bool is_q;
12437 bool is_scalar;
12438 bool only_in_vector = false;
12440 int pass;
12441 TCGv_i32 tcg_rmode = NULL;
12442 TCGv_ptr tcg_fpstatus = NULL;
12443 bool need_rmode = false;
12444 bool need_fpst = true;
12445 int rmode;
12447 if (!dc_isar_feature(aa64_fp16, s)) {
12448 unallocated_encoding(s);
12449 return;
12452 rd = extract32(insn, 0, 5);
12453 rn = extract32(insn, 5, 5);
12455 a = extract32(insn, 23, 1);
12456 u = extract32(insn, 29, 1);
12457 is_scalar = extract32(insn, 28, 1);
12458 is_q = extract32(insn, 30, 1);
12460 opcode = extract32(insn, 12, 5);
12461 fpop = deposit32(opcode, 5, 1, a);
12462 fpop = deposit32(fpop, 6, 1, u);
12464 rd = extract32(insn, 0, 5);
12465 rn = extract32(insn, 5, 5);
12467 switch (fpop) {
12468 case 0x1d: /* SCVTF */
12469 case 0x5d: /* UCVTF */
12471 int elements;
12473 if (is_scalar) {
12474 elements = 1;
12475 } else {
12476 elements = (is_q ? 8 : 4);
12479 if (!fp_access_check(s)) {
12480 return;
12482 handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12483 return;
12485 break;
12486 case 0x2c: /* FCMGT (zero) */
12487 case 0x2d: /* FCMEQ (zero) */
12488 case 0x2e: /* FCMLT (zero) */
12489 case 0x6c: /* FCMGE (zero) */
12490 case 0x6d: /* FCMLE (zero) */
12491 handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12492 return;
12493 case 0x3d: /* FRECPE */
12494 case 0x3f: /* FRECPX */
12495 break;
12496 case 0x18: /* FRINTN */
12497 need_rmode = true;
12498 only_in_vector = true;
12499 rmode = FPROUNDING_TIEEVEN;
12500 break;
12501 case 0x19: /* FRINTM */
12502 need_rmode = true;
12503 only_in_vector = true;
12504 rmode = FPROUNDING_NEGINF;
12505 break;
12506 case 0x38: /* FRINTP */
12507 need_rmode = true;
12508 only_in_vector = true;
12509 rmode = FPROUNDING_POSINF;
12510 break;
12511 case 0x39: /* FRINTZ */
12512 need_rmode = true;
12513 only_in_vector = true;
12514 rmode = FPROUNDING_ZERO;
12515 break;
12516 case 0x58: /* FRINTA */
12517 need_rmode = true;
12518 only_in_vector = true;
12519 rmode = FPROUNDING_TIEAWAY;
12520 break;
12521 case 0x59: /* FRINTX */
12522 case 0x79: /* FRINTI */
12523 only_in_vector = true;
12524 /* current rounding mode */
12525 break;
12526 case 0x1a: /* FCVTNS */
12527 need_rmode = true;
12528 rmode = FPROUNDING_TIEEVEN;
12529 break;
12530 case 0x1b: /* FCVTMS */
12531 need_rmode = true;
12532 rmode = FPROUNDING_NEGINF;
12533 break;
12534 case 0x1c: /* FCVTAS */
12535 need_rmode = true;
12536 rmode = FPROUNDING_TIEAWAY;
12537 break;
12538 case 0x3a: /* FCVTPS */
12539 need_rmode = true;
12540 rmode = FPROUNDING_POSINF;
12541 break;
12542 case 0x3b: /* FCVTZS */
12543 need_rmode = true;
12544 rmode = FPROUNDING_ZERO;
12545 break;
12546 case 0x5a: /* FCVTNU */
12547 need_rmode = true;
12548 rmode = FPROUNDING_TIEEVEN;
12549 break;
12550 case 0x5b: /* FCVTMU */
12551 need_rmode = true;
12552 rmode = FPROUNDING_NEGINF;
12553 break;
12554 case 0x5c: /* FCVTAU */
12555 need_rmode = true;
12556 rmode = FPROUNDING_TIEAWAY;
12557 break;
12558 case 0x7a: /* FCVTPU */
12559 need_rmode = true;
12560 rmode = FPROUNDING_POSINF;
12561 break;
12562 case 0x7b: /* FCVTZU */
12563 need_rmode = true;
12564 rmode = FPROUNDING_ZERO;
12565 break;
12566 case 0x2f: /* FABS */
12567 case 0x6f: /* FNEG */
12568 need_fpst = false;
12569 break;
12570 case 0x7d: /* FRSQRTE */
12571 case 0x7f: /* FSQRT (vector) */
12572 break;
12573 default:
12574 fprintf(stderr, "%s: insn %#04x fpop %#2x\n", __func__, insn, fpop);
12575 g_assert_not_reached();
12579 /* Check additional constraints for the scalar encoding */
12580 if (is_scalar) {
12581 if (!is_q) {
12582 unallocated_encoding(s);
12583 return;
12585 /* FRINTxx is only in the vector form */
12586 if (only_in_vector) {
12587 unallocated_encoding(s);
12588 return;
12592 if (!fp_access_check(s)) {
12593 return;
12596 if (need_rmode || need_fpst) {
12597 tcg_fpstatus = get_fpstatus_ptr(true);
12600 if (need_rmode) {
12601 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
12602 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12605 if (is_scalar) {
12606 TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12607 TCGv_i32 tcg_res = tcg_temp_new_i32();
12609 switch (fpop) {
12610 case 0x1a: /* FCVTNS */
12611 case 0x1b: /* FCVTMS */
12612 case 0x1c: /* FCVTAS */
12613 case 0x3a: /* FCVTPS */
12614 case 0x3b: /* FCVTZS */
12615 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12616 break;
12617 case 0x3d: /* FRECPE */
12618 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12619 break;
12620 case 0x3f: /* FRECPX */
12621 gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12622 break;
12623 case 0x5a: /* FCVTNU */
12624 case 0x5b: /* FCVTMU */
12625 case 0x5c: /* FCVTAU */
12626 case 0x7a: /* FCVTPU */
12627 case 0x7b: /* FCVTZU */
12628 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12629 break;
12630 case 0x6f: /* FNEG */
12631 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12632 break;
12633 case 0x7d: /* FRSQRTE */
12634 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12635 break;
12636 default:
12637 g_assert_not_reached();
12640 /* limit any sign extension going on */
12641 tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12642 write_fp_sreg(s, rd, tcg_res);
12644 tcg_temp_free_i32(tcg_res);
12645 tcg_temp_free_i32(tcg_op);
12646 } else {
12647 for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12648 TCGv_i32 tcg_op = tcg_temp_new_i32();
12649 TCGv_i32 tcg_res = tcg_temp_new_i32();
12651 read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12653 switch (fpop) {
12654 case 0x1a: /* FCVTNS */
12655 case 0x1b: /* FCVTMS */
12656 case 0x1c: /* FCVTAS */
12657 case 0x3a: /* FCVTPS */
12658 case 0x3b: /* FCVTZS */
12659 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12660 break;
12661 case 0x3d: /* FRECPE */
12662 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12663 break;
12664 case 0x5a: /* FCVTNU */
12665 case 0x5b: /* FCVTMU */
12666 case 0x5c: /* FCVTAU */
12667 case 0x7a: /* FCVTPU */
12668 case 0x7b: /* FCVTZU */
12669 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12670 break;
12671 case 0x18: /* FRINTN */
12672 case 0x19: /* FRINTM */
12673 case 0x38: /* FRINTP */
12674 case 0x39: /* FRINTZ */
12675 case 0x58: /* FRINTA */
12676 case 0x79: /* FRINTI */
12677 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12678 break;
12679 case 0x59: /* FRINTX */
12680 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12681 break;
12682 case 0x2f: /* FABS */
12683 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12684 break;
12685 case 0x6f: /* FNEG */
12686 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12687 break;
12688 case 0x7d: /* FRSQRTE */
12689 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12690 break;
12691 case 0x7f: /* FSQRT */
12692 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12693 break;
12694 default:
12695 g_assert_not_reached();
12698 write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12700 tcg_temp_free_i32(tcg_res);
12701 tcg_temp_free_i32(tcg_op);
12704 clear_vec_high(s, is_q, rd);
12707 if (tcg_rmode) {
12708 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12709 tcg_temp_free_i32(tcg_rmode);
12712 if (tcg_fpstatus) {
12713 tcg_temp_free_ptr(tcg_fpstatus);
12717 /* AdvSIMD scalar x indexed element
12718 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
12719 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12720 * | 0 1 | U | 1 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
12721 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12722 * AdvSIMD vector x indexed element
12723 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
12724 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12725 * | 0 | Q | U | 0 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
12726 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12728 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12730 /* This encoding has two kinds of instruction:
12731 * normal, where we perform elt x idxelt => elt for each
12732 * element in the vector
12733 * long, where we perform elt x idxelt and generate a result of
12734 * double the width of the input element
12735 * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12737 bool is_scalar = extract32(insn, 28, 1);
12738 bool is_q = extract32(insn, 30, 1);
12739 bool u = extract32(insn, 29, 1);
12740 int size = extract32(insn, 22, 2);
12741 int l = extract32(insn, 21, 1);
12742 int m = extract32(insn, 20, 1);
12743 /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12744 int rm = extract32(insn, 16, 4);
12745 int opcode = extract32(insn, 12, 4);
12746 int h = extract32(insn, 11, 1);
12747 int rn = extract32(insn, 5, 5);
12748 int rd = extract32(insn, 0, 5);
12749 bool is_long = false;
12750 int is_fp = 0;
12751 bool is_fp16 = false;
12752 int index;
12753 TCGv_ptr fpst;
12755 switch (16 * u + opcode) {
12756 case 0x08: /* MUL */
12757 case 0x10: /* MLA */
12758 case 0x14: /* MLS */
12759 if (is_scalar) {
12760 unallocated_encoding(s);
12761 return;
12763 break;
12764 case 0x02: /* SMLAL, SMLAL2 */
12765 case 0x12: /* UMLAL, UMLAL2 */
12766 case 0x06: /* SMLSL, SMLSL2 */
12767 case 0x16: /* UMLSL, UMLSL2 */
12768 case 0x0a: /* SMULL, SMULL2 */
12769 case 0x1a: /* UMULL, UMULL2 */
12770 if (is_scalar) {
12771 unallocated_encoding(s);
12772 return;
12774 is_long = true;
12775 break;
12776 case 0x03: /* SQDMLAL, SQDMLAL2 */
12777 case 0x07: /* SQDMLSL, SQDMLSL2 */
12778 case 0x0b: /* SQDMULL, SQDMULL2 */
12779 is_long = true;
12780 break;
12781 case 0x0c: /* SQDMULH */
12782 case 0x0d: /* SQRDMULH */
12783 break;
12784 case 0x01: /* FMLA */
12785 case 0x05: /* FMLS */
12786 case 0x09: /* FMUL */
12787 case 0x19: /* FMULX */
12788 is_fp = 1;
12789 break;
12790 case 0x1d: /* SQRDMLAH */
12791 case 0x1f: /* SQRDMLSH */
12792 if (!dc_isar_feature(aa64_rdm, s)) {
12793 unallocated_encoding(s);
12794 return;
12796 break;
12797 case 0x0e: /* SDOT */
12798 case 0x1e: /* UDOT */
12799 if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12800 unallocated_encoding(s);
12801 return;
12803 break;
12804 case 0x11: /* FCMLA #0 */
12805 case 0x13: /* FCMLA #90 */
12806 case 0x15: /* FCMLA #180 */
12807 case 0x17: /* FCMLA #270 */
12808 if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
12809 unallocated_encoding(s);
12810 return;
12812 is_fp = 2;
12813 break;
12814 case 0x00: /* FMLAL */
12815 case 0x04: /* FMLSL */
12816 case 0x18: /* FMLAL2 */
12817 case 0x1c: /* FMLSL2 */
12818 if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
12819 unallocated_encoding(s);
12820 return;
12822 size = MO_16;
12823 /* is_fp, but we pass cpu_env not fp_status. */
12824 break;
12825 default:
12826 unallocated_encoding(s);
12827 return;
12830 switch (is_fp) {
12831 case 1: /* normal fp */
12832 /* convert insn encoded size to TCGMemOp size */
12833 switch (size) {
12834 case 0: /* half-precision */
12835 size = MO_16;
12836 is_fp16 = true;
12837 break;
12838 case MO_32: /* single precision */
12839 case MO_64: /* double precision */
12840 break;
12841 default:
12842 unallocated_encoding(s);
12843 return;
12845 break;
12847 case 2: /* complex fp */
12848 /* Each indexable element is a complex pair. */
12849 size += 1;
12850 switch (size) {
12851 case MO_32:
12852 if (h && !is_q) {
12853 unallocated_encoding(s);
12854 return;
12856 is_fp16 = true;
12857 break;
12858 case MO_64:
12859 break;
12860 default:
12861 unallocated_encoding(s);
12862 return;
12864 break;
12866 default: /* integer */
12867 switch (size) {
12868 case MO_8:
12869 case MO_64:
12870 unallocated_encoding(s);
12871 return;
12873 break;
12875 if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
12876 unallocated_encoding(s);
12877 return;
12880 /* Given TCGMemOp size, adjust register and indexing. */
12881 switch (size) {
12882 case MO_16:
12883 index = h << 2 | l << 1 | m;
12884 break;
12885 case MO_32:
12886 index = h << 1 | l;
12887 rm |= m << 4;
12888 break;
12889 case MO_64:
12890 if (l || !is_q) {
12891 unallocated_encoding(s);
12892 return;
12894 index = h;
12895 rm |= m << 4;
12896 break;
12897 default:
12898 g_assert_not_reached();
12901 if (!fp_access_check(s)) {
12902 return;
12905 if (is_fp) {
12906 fpst = get_fpstatus_ptr(is_fp16);
12907 } else {
12908 fpst = NULL;
12911 switch (16 * u + opcode) {
12912 case 0x0e: /* SDOT */
12913 case 0x1e: /* UDOT */
12914 gen_gvec_op3_ool(s, is_q, rd, rn, rm, index,
12915 u ? gen_helper_gvec_udot_idx_b
12916 : gen_helper_gvec_sdot_idx_b);
12917 return;
12918 case 0x11: /* FCMLA #0 */
12919 case 0x13: /* FCMLA #90 */
12920 case 0x15: /* FCMLA #180 */
12921 case 0x17: /* FCMLA #270 */
12923 int rot = extract32(insn, 13, 2);
12924 int data = (index << 2) | rot;
12925 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
12926 vec_full_reg_offset(s, rn),
12927 vec_full_reg_offset(s, rm), fpst,
12928 is_q ? 16 : 8, vec_full_reg_size(s), data,
12929 size == MO_64
12930 ? gen_helper_gvec_fcmlas_idx
12931 : gen_helper_gvec_fcmlah_idx);
12932 tcg_temp_free_ptr(fpst);
12934 return;
12936 case 0x00: /* FMLAL */
12937 case 0x04: /* FMLSL */
12938 case 0x18: /* FMLAL2 */
12939 case 0x1c: /* FMLSL2 */
12941 int is_s = extract32(opcode, 2, 1);
12942 int is_2 = u;
12943 int data = (index << 2) | (is_2 << 1) | is_s;
12944 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
12945 vec_full_reg_offset(s, rn),
12946 vec_full_reg_offset(s, rm), cpu_env,
12947 is_q ? 16 : 8, vec_full_reg_size(s),
12948 data, gen_helper_gvec_fmlal_idx_a64);
12950 return;
12953 if (size == 3) {
12954 TCGv_i64 tcg_idx = tcg_temp_new_i64();
12955 int pass;
12957 assert(is_fp && is_q && !is_long);
12959 read_vec_element(s, tcg_idx, rm, index, MO_64);
12961 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
12962 TCGv_i64 tcg_op = tcg_temp_new_i64();
12963 TCGv_i64 tcg_res = tcg_temp_new_i64();
12965 read_vec_element(s, tcg_op, rn, pass, MO_64);
12967 switch (16 * u + opcode) {
12968 case 0x05: /* FMLS */
12969 /* As usual for ARM, separate negation for fused multiply-add */
12970 gen_helper_vfp_negd(tcg_op, tcg_op);
12971 /* fall through */
12972 case 0x01: /* FMLA */
12973 read_vec_element(s, tcg_res, rd, pass, MO_64);
12974 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
12975 break;
12976 case 0x09: /* FMUL */
12977 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
12978 break;
12979 case 0x19: /* FMULX */
12980 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
12981 break;
12982 default:
12983 g_assert_not_reached();
12986 write_vec_element(s, tcg_res, rd, pass, MO_64);
12987 tcg_temp_free_i64(tcg_op);
12988 tcg_temp_free_i64(tcg_res);
12991 tcg_temp_free_i64(tcg_idx);
12992 clear_vec_high(s, !is_scalar, rd);
12993 } else if (!is_long) {
12994 /* 32 bit floating point, or 16 or 32 bit integer.
12995 * For the 16 bit scalar case we use the usual Neon helpers and
12996 * rely on the fact that 0 op 0 == 0 with no side effects.
12998 TCGv_i32 tcg_idx = tcg_temp_new_i32();
12999 int pass, maxpasses;
13001 if (is_scalar) {
13002 maxpasses = 1;
13003 } else {
13004 maxpasses = is_q ? 4 : 2;
13007 read_vec_element_i32(s, tcg_idx, rm, index, size);
13009 if (size == 1 && !is_scalar) {
13010 /* The simplest way to handle the 16x16 indexed ops is to duplicate
13011 * the index into both halves of the 32 bit tcg_idx and then use
13012 * the usual Neon helpers.
13014 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13017 for (pass = 0; pass < maxpasses; pass++) {
13018 TCGv_i32 tcg_op = tcg_temp_new_i32();
13019 TCGv_i32 tcg_res = tcg_temp_new_i32();
13021 read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13023 switch (16 * u + opcode) {
13024 case 0x08: /* MUL */
13025 case 0x10: /* MLA */
13026 case 0x14: /* MLS */
13028 static NeonGenTwoOpFn * const fns[2][2] = {
13029 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13030 { tcg_gen_add_i32, tcg_gen_sub_i32 },
13032 NeonGenTwoOpFn *genfn;
13033 bool is_sub = opcode == 0x4;
13035 if (size == 1) {
13036 gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13037 } else {
13038 tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13040 if (opcode == 0x8) {
13041 break;
13043 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13044 genfn = fns[size - 1][is_sub];
13045 genfn(tcg_res, tcg_op, tcg_res);
13046 break;
13048 case 0x05: /* FMLS */
13049 case 0x01: /* FMLA */
13050 read_vec_element_i32(s, tcg_res, rd, pass,
13051 is_scalar ? size : MO_32);
13052 switch (size) {
13053 case 1:
13054 if (opcode == 0x5) {
13055 /* As usual for ARM, separate negation for fused
13056 * multiply-add */
13057 tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13059 if (is_scalar) {
13060 gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13061 tcg_res, fpst);
13062 } else {
13063 gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13064 tcg_res, fpst);
13066 break;
13067 case 2:
13068 if (opcode == 0x5) {
13069 /* As usual for ARM, separate negation for
13070 * fused multiply-add */
13071 tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13073 gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13074 tcg_res, fpst);
13075 break;
13076 default:
13077 g_assert_not_reached();
13079 break;
13080 case 0x09: /* FMUL */
13081 switch (size) {
13082 case 1:
13083 if (is_scalar) {
13084 gen_helper_advsimd_mulh(tcg_res, tcg_op,
13085 tcg_idx, fpst);
13086 } else {
13087 gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13088 tcg_idx, fpst);
13090 break;
13091 case 2:
13092 gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13093 break;
13094 default:
13095 g_assert_not_reached();
13097 break;
13098 case 0x19: /* FMULX */
13099 switch (size) {
13100 case 1:
13101 if (is_scalar) {
13102 gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13103 tcg_idx, fpst);
13104 } else {
13105 gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13106 tcg_idx, fpst);
13108 break;
13109 case 2:
13110 gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13111 break;
13112 default:
13113 g_assert_not_reached();
13115 break;
13116 case 0x0c: /* SQDMULH */
13117 if (size == 1) {
13118 gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13119 tcg_op, tcg_idx);
13120 } else {
13121 gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13122 tcg_op, tcg_idx);
13124 break;
13125 case 0x0d: /* SQRDMULH */
13126 if (size == 1) {
13127 gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13128 tcg_op, tcg_idx);
13129 } else {
13130 gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13131 tcg_op, tcg_idx);
13133 break;
13134 case 0x1d: /* SQRDMLAH */
13135 read_vec_element_i32(s, tcg_res, rd, pass,
13136 is_scalar ? size : MO_32);
13137 if (size == 1) {
13138 gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13139 tcg_op, tcg_idx, tcg_res);
13140 } else {
13141 gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13142 tcg_op, tcg_idx, tcg_res);
13144 break;
13145 case 0x1f: /* SQRDMLSH */
13146 read_vec_element_i32(s, tcg_res, rd, pass,
13147 is_scalar ? size : MO_32);
13148 if (size == 1) {
13149 gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13150 tcg_op, tcg_idx, tcg_res);
13151 } else {
13152 gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13153 tcg_op, tcg_idx, tcg_res);
13155 break;
13156 default:
13157 g_assert_not_reached();
13160 if (is_scalar) {
13161 write_fp_sreg(s, rd, tcg_res);
13162 } else {
13163 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13166 tcg_temp_free_i32(tcg_op);
13167 tcg_temp_free_i32(tcg_res);
13170 tcg_temp_free_i32(tcg_idx);
13171 clear_vec_high(s, is_q, rd);
13172 } else {
13173 /* long ops: 16x16->32 or 32x32->64 */
13174 TCGv_i64 tcg_res[2];
13175 int pass;
13176 bool satop = extract32(opcode, 0, 1);
13177 TCGMemOp memop = MO_32;
13179 if (satop || !u) {
13180 memop |= MO_SIGN;
13183 if (size == 2) {
13184 TCGv_i64 tcg_idx = tcg_temp_new_i64();
13186 read_vec_element(s, tcg_idx, rm, index, memop);
13188 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13189 TCGv_i64 tcg_op = tcg_temp_new_i64();
13190 TCGv_i64 tcg_passres;
13191 int passelt;
13193 if (is_scalar) {
13194 passelt = 0;
13195 } else {
13196 passelt = pass + (is_q * 2);
13199 read_vec_element(s, tcg_op, rn, passelt, memop);
13201 tcg_res[pass] = tcg_temp_new_i64();
13203 if (opcode == 0xa || opcode == 0xb) {
13204 /* Non-accumulating ops */
13205 tcg_passres = tcg_res[pass];
13206 } else {
13207 tcg_passres = tcg_temp_new_i64();
13210 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13211 tcg_temp_free_i64(tcg_op);
13213 if (satop) {
13214 /* saturating, doubling */
13215 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13216 tcg_passres, tcg_passres);
13219 if (opcode == 0xa || opcode == 0xb) {
13220 continue;
13223 /* Accumulating op: handle accumulate step */
13224 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13226 switch (opcode) {
13227 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13228 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13229 break;
13230 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13231 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13232 break;
13233 case 0x7: /* SQDMLSL, SQDMLSL2 */
13234 tcg_gen_neg_i64(tcg_passres, tcg_passres);
13235 /* fall through */
13236 case 0x3: /* SQDMLAL, SQDMLAL2 */
13237 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13238 tcg_res[pass],
13239 tcg_passres);
13240 break;
13241 default:
13242 g_assert_not_reached();
13244 tcg_temp_free_i64(tcg_passres);
13246 tcg_temp_free_i64(tcg_idx);
13248 clear_vec_high(s, !is_scalar, rd);
13249 } else {
13250 TCGv_i32 tcg_idx = tcg_temp_new_i32();
13252 assert(size == 1);
13253 read_vec_element_i32(s, tcg_idx, rm, index, size);
13255 if (!is_scalar) {
13256 /* The simplest way to handle the 16x16 indexed ops is to
13257 * duplicate the index into both halves of the 32 bit tcg_idx
13258 * and then use the usual Neon helpers.
13260 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13263 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13264 TCGv_i32 tcg_op = tcg_temp_new_i32();
13265 TCGv_i64 tcg_passres;
13267 if (is_scalar) {
13268 read_vec_element_i32(s, tcg_op, rn, pass, size);
13269 } else {
13270 read_vec_element_i32(s, tcg_op, rn,
13271 pass + (is_q * 2), MO_32);
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 if (memop & MO_SIGN) {
13284 gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13285 } else {
13286 gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13288 if (satop) {
13289 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13290 tcg_passres, tcg_passres);
13292 tcg_temp_free_i32(tcg_op);
13294 if (opcode == 0xa || opcode == 0xb) {
13295 continue;
13298 /* Accumulating op: handle accumulate step */
13299 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13301 switch (opcode) {
13302 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13303 gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13304 tcg_passres);
13305 break;
13306 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13307 gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13308 tcg_passres);
13309 break;
13310 case 0x7: /* SQDMLSL, SQDMLSL2 */
13311 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13312 /* fall through */
13313 case 0x3: /* SQDMLAL, SQDMLAL2 */
13314 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13315 tcg_res[pass],
13316 tcg_passres);
13317 break;
13318 default:
13319 g_assert_not_reached();
13321 tcg_temp_free_i64(tcg_passres);
13323 tcg_temp_free_i32(tcg_idx);
13325 if (is_scalar) {
13326 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13330 if (is_scalar) {
13331 tcg_res[1] = tcg_const_i64(0);
13334 for (pass = 0; pass < 2; pass++) {
13335 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13336 tcg_temp_free_i64(tcg_res[pass]);
13340 if (fpst) {
13341 tcg_temp_free_ptr(fpst);
13345 /* Crypto AES
13346 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
13347 * +-----------------+------+-----------+--------+-----+------+------+
13348 * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
13349 * +-----------------+------+-----------+--------+-----+------+------+
13351 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13353 int size = extract32(insn, 22, 2);
13354 int opcode = extract32(insn, 12, 5);
13355 int rn = extract32(insn, 5, 5);
13356 int rd = extract32(insn, 0, 5);
13357 int decrypt;
13358 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr;
13359 TCGv_i32 tcg_decrypt;
13360 CryptoThreeOpIntFn *genfn;
13362 if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13363 unallocated_encoding(s);
13364 return;
13367 switch (opcode) {
13368 case 0x4: /* AESE */
13369 decrypt = 0;
13370 genfn = gen_helper_crypto_aese;
13371 break;
13372 case 0x6: /* AESMC */
13373 decrypt = 0;
13374 genfn = gen_helper_crypto_aesmc;
13375 break;
13376 case 0x5: /* AESD */
13377 decrypt = 1;
13378 genfn = gen_helper_crypto_aese;
13379 break;
13380 case 0x7: /* AESIMC */
13381 decrypt = 1;
13382 genfn = gen_helper_crypto_aesmc;
13383 break;
13384 default:
13385 unallocated_encoding(s);
13386 return;
13389 if (!fp_access_check(s)) {
13390 return;
13393 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13394 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
13395 tcg_decrypt = tcg_const_i32(decrypt);
13397 genfn(tcg_rd_ptr, tcg_rn_ptr, tcg_decrypt);
13399 tcg_temp_free_ptr(tcg_rd_ptr);
13400 tcg_temp_free_ptr(tcg_rn_ptr);
13401 tcg_temp_free_i32(tcg_decrypt);
13404 /* Crypto three-reg SHA
13405 * 31 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
13406 * +-----------------+------+---+------+---+--------+-----+------+------+
13407 * | 0 1 0 1 1 1 1 0 | size | 0 | Rm | 0 | opcode | 0 0 | Rn | Rd |
13408 * +-----------------+------+---+------+---+--------+-----+------+------+
13410 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13412 int size = extract32(insn, 22, 2);
13413 int opcode = extract32(insn, 12, 3);
13414 int rm = extract32(insn, 16, 5);
13415 int rn = extract32(insn, 5, 5);
13416 int rd = extract32(insn, 0, 5);
13417 CryptoThreeOpFn *genfn;
13418 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr;
13419 bool feature;
13421 if (size != 0) {
13422 unallocated_encoding(s);
13423 return;
13426 switch (opcode) {
13427 case 0: /* SHA1C */
13428 case 1: /* SHA1P */
13429 case 2: /* SHA1M */
13430 case 3: /* SHA1SU0 */
13431 genfn = NULL;
13432 feature = dc_isar_feature(aa64_sha1, s);
13433 break;
13434 case 4: /* SHA256H */
13435 genfn = gen_helper_crypto_sha256h;
13436 feature = dc_isar_feature(aa64_sha256, s);
13437 break;
13438 case 5: /* SHA256H2 */
13439 genfn = gen_helper_crypto_sha256h2;
13440 feature = dc_isar_feature(aa64_sha256, s);
13441 break;
13442 case 6: /* SHA256SU1 */
13443 genfn = gen_helper_crypto_sha256su1;
13444 feature = dc_isar_feature(aa64_sha256, s);
13445 break;
13446 default:
13447 unallocated_encoding(s);
13448 return;
13451 if (!feature) {
13452 unallocated_encoding(s);
13453 return;
13456 if (!fp_access_check(s)) {
13457 return;
13460 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13461 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
13462 tcg_rm_ptr = vec_full_reg_ptr(s, rm);
13464 if (genfn) {
13465 genfn(tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr);
13466 } else {
13467 TCGv_i32 tcg_opcode = tcg_const_i32(opcode);
13469 gen_helper_crypto_sha1_3reg(tcg_rd_ptr, tcg_rn_ptr,
13470 tcg_rm_ptr, tcg_opcode);
13471 tcg_temp_free_i32(tcg_opcode);
13474 tcg_temp_free_ptr(tcg_rd_ptr);
13475 tcg_temp_free_ptr(tcg_rn_ptr);
13476 tcg_temp_free_ptr(tcg_rm_ptr);
13479 /* Crypto two-reg SHA
13480 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
13481 * +-----------------+------+-----------+--------+-----+------+------+
13482 * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
13483 * +-----------------+------+-----------+--------+-----+------+------+
13485 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13487 int size = extract32(insn, 22, 2);
13488 int opcode = extract32(insn, 12, 5);
13489 int rn = extract32(insn, 5, 5);
13490 int rd = extract32(insn, 0, 5);
13491 CryptoTwoOpFn *genfn;
13492 bool feature;
13493 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr;
13495 if (size != 0) {
13496 unallocated_encoding(s);
13497 return;
13500 switch (opcode) {
13501 case 0: /* SHA1H */
13502 feature = dc_isar_feature(aa64_sha1, s);
13503 genfn = gen_helper_crypto_sha1h;
13504 break;
13505 case 1: /* SHA1SU1 */
13506 feature = dc_isar_feature(aa64_sha1, s);
13507 genfn = gen_helper_crypto_sha1su1;
13508 break;
13509 case 2: /* SHA256SU0 */
13510 feature = dc_isar_feature(aa64_sha256, s);
13511 genfn = gen_helper_crypto_sha256su0;
13512 break;
13513 default:
13514 unallocated_encoding(s);
13515 return;
13518 if (!feature) {
13519 unallocated_encoding(s);
13520 return;
13523 if (!fp_access_check(s)) {
13524 return;
13527 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13528 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
13530 genfn(tcg_rd_ptr, tcg_rn_ptr);
13532 tcg_temp_free_ptr(tcg_rd_ptr);
13533 tcg_temp_free_ptr(tcg_rn_ptr);
13536 /* Crypto three-reg SHA512
13537 * 31 21 20 16 15 14 13 12 11 10 9 5 4 0
13538 * +-----------------------+------+---+---+-----+--------+------+------+
13539 * | 1 1 0 0 1 1 1 0 0 1 1 | Rm | 1 | O | 0 0 | opcode | Rn | Rd |
13540 * +-----------------------+------+---+---+-----+--------+------+------+
13542 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13544 int opcode = extract32(insn, 10, 2);
13545 int o = extract32(insn, 14, 1);
13546 int rm = extract32(insn, 16, 5);
13547 int rn = extract32(insn, 5, 5);
13548 int rd = extract32(insn, 0, 5);
13549 bool feature;
13550 CryptoThreeOpFn *genfn;
13552 if (o == 0) {
13553 switch (opcode) {
13554 case 0: /* SHA512H */
13555 feature = dc_isar_feature(aa64_sha512, s);
13556 genfn = gen_helper_crypto_sha512h;
13557 break;
13558 case 1: /* SHA512H2 */
13559 feature = dc_isar_feature(aa64_sha512, s);
13560 genfn = gen_helper_crypto_sha512h2;
13561 break;
13562 case 2: /* SHA512SU1 */
13563 feature = dc_isar_feature(aa64_sha512, s);
13564 genfn = gen_helper_crypto_sha512su1;
13565 break;
13566 case 3: /* RAX1 */
13567 feature = dc_isar_feature(aa64_sha3, s);
13568 genfn = NULL;
13569 break;
13571 } else {
13572 switch (opcode) {
13573 case 0: /* SM3PARTW1 */
13574 feature = dc_isar_feature(aa64_sm3, s);
13575 genfn = gen_helper_crypto_sm3partw1;
13576 break;
13577 case 1: /* SM3PARTW2 */
13578 feature = dc_isar_feature(aa64_sm3, s);
13579 genfn = gen_helper_crypto_sm3partw2;
13580 break;
13581 case 2: /* SM4EKEY */
13582 feature = dc_isar_feature(aa64_sm4, s);
13583 genfn = gen_helper_crypto_sm4ekey;
13584 break;
13585 default:
13586 unallocated_encoding(s);
13587 return;
13591 if (!feature) {
13592 unallocated_encoding(s);
13593 return;
13596 if (!fp_access_check(s)) {
13597 return;
13600 if (genfn) {
13601 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr;
13603 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13604 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
13605 tcg_rm_ptr = vec_full_reg_ptr(s, rm);
13607 genfn(tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr);
13609 tcg_temp_free_ptr(tcg_rd_ptr);
13610 tcg_temp_free_ptr(tcg_rn_ptr);
13611 tcg_temp_free_ptr(tcg_rm_ptr);
13612 } else {
13613 TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
13614 int pass;
13616 tcg_op1 = tcg_temp_new_i64();
13617 tcg_op2 = tcg_temp_new_i64();
13618 tcg_res[0] = tcg_temp_new_i64();
13619 tcg_res[1] = tcg_temp_new_i64();
13621 for (pass = 0; pass < 2; pass++) {
13622 read_vec_element(s, tcg_op1, rn, pass, MO_64);
13623 read_vec_element(s, tcg_op2, rm, pass, MO_64);
13625 tcg_gen_rotli_i64(tcg_res[pass], tcg_op2, 1);
13626 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13628 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13629 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13631 tcg_temp_free_i64(tcg_op1);
13632 tcg_temp_free_i64(tcg_op2);
13633 tcg_temp_free_i64(tcg_res[0]);
13634 tcg_temp_free_i64(tcg_res[1]);
13638 /* Crypto two-reg SHA512
13639 * 31 12 11 10 9 5 4 0
13640 * +-----------------------------------------+--------+------+------+
13641 * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode | Rn | Rd |
13642 * +-----------------------------------------+--------+------+------+
13644 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13646 int opcode = extract32(insn, 10, 2);
13647 int rn = extract32(insn, 5, 5);
13648 int rd = extract32(insn, 0, 5);
13649 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr;
13650 bool feature;
13651 CryptoTwoOpFn *genfn;
13653 switch (opcode) {
13654 case 0: /* SHA512SU0 */
13655 feature = dc_isar_feature(aa64_sha512, s);
13656 genfn = gen_helper_crypto_sha512su0;
13657 break;
13658 case 1: /* SM4E */
13659 feature = dc_isar_feature(aa64_sm4, s);
13660 genfn = gen_helper_crypto_sm4e;
13661 break;
13662 default:
13663 unallocated_encoding(s);
13664 return;
13667 if (!feature) {
13668 unallocated_encoding(s);
13669 return;
13672 if (!fp_access_check(s)) {
13673 return;
13676 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13677 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
13679 genfn(tcg_rd_ptr, tcg_rn_ptr);
13681 tcg_temp_free_ptr(tcg_rd_ptr);
13682 tcg_temp_free_ptr(tcg_rn_ptr);
13685 /* Crypto four-register
13686 * 31 23 22 21 20 16 15 14 10 9 5 4 0
13687 * +-------------------+-----+------+---+------+------+------+
13688 * | 1 1 0 0 1 1 1 0 0 | Op0 | Rm | 0 | Ra | Rn | Rd |
13689 * +-------------------+-----+------+---+------+------+------+
13691 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13693 int op0 = extract32(insn, 21, 2);
13694 int rm = extract32(insn, 16, 5);
13695 int ra = extract32(insn, 10, 5);
13696 int rn = extract32(insn, 5, 5);
13697 int rd = extract32(insn, 0, 5);
13698 bool feature;
13700 switch (op0) {
13701 case 0: /* EOR3 */
13702 case 1: /* BCAX */
13703 feature = dc_isar_feature(aa64_sha3, s);
13704 break;
13705 case 2: /* SM3SS1 */
13706 feature = dc_isar_feature(aa64_sm3, s);
13707 break;
13708 default:
13709 unallocated_encoding(s);
13710 return;
13713 if (!feature) {
13714 unallocated_encoding(s);
13715 return;
13718 if (!fp_access_check(s)) {
13719 return;
13722 if (op0 < 2) {
13723 TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13724 int pass;
13726 tcg_op1 = tcg_temp_new_i64();
13727 tcg_op2 = tcg_temp_new_i64();
13728 tcg_op3 = tcg_temp_new_i64();
13729 tcg_res[0] = tcg_temp_new_i64();
13730 tcg_res[1] = tcg_temp_new_i64();
13732 for (pass = 0; pass < 2; pass++) {
13733 read_vec_element(s, tcg_op1, rn, pass, MO_64);
13734 read_vec_element(s, tcg_op2, rm, pass, MO_64);
13735 read_vec_element(s, tcg_op3, ra, pass, MO_64);
13737 if (op0 == 0) {
13738 /* EOR3 */
13739 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13740 } else {
13741 /* BCAX */
13742 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13744 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13746 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13747 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13749 tcg_temp_free_i64(tcg_op1);
13750 tcg_temp_free_i64(tcg_op2);
13751 tcg_temp_free_i64(tcg_op3);
13752 tcg_temp_free_i64(tcg_res[0]);
13753 tcg_temp_free_i64(tcg_res[1]);
13754 } else {
13755 TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13757 tcg_op1 = tcg_temp_new_i32();
13758 tcg_op2 = tcg_temp_new_i32();
13759 tcg_op3 = tcg_temp_new_i32();
13760 tcg_res = tcg_temp_new_i32();
13761 tcg_zero = tcg_const_i32(0);
13763 read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13764 read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13765 read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13767 tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13768 tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13769 tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13770 tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13772 write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13773 write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13774 write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13775 write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13777 tcg_temp_free_i32(tcg_op1);
13778 tcg_temp_free_i32(tcg_op2);
13779 tcg_temp_free_i32(tcg_op3);
13780 tcg_temp_free_i32(tcg_res);
13781 tcg_temp_free_i32(tcg_zero);
13785 /* Crypto XAR
13786 * 31 21 20 16 15 10 9 5 4 0
13787 * +-----------------------+------+--------+------+------+
13788 * | 1 1 0 0 1 1 1 0 1 0 0 | Rm | imm6 | Rn | Rd |
13789 * +-----------------------+------+--------+------+------+
13791 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
13793 int rm = extract32(insn, 16, 5);
13794 int imm6 = extract32(insn, 10, 6);
13795 int rn = extract32(insn, 5, 5);
13796 int rd = extract32(insn, 0, 5);
13797 TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
13798 int pass;
13800 if (!dc_isar_feature(aa64_sha3, s)) {
13801 unallocated_encoding(s);
13802 return;
13805 if (!fp_access_check(s)) {
13806 return;
13809 tcg_op1 = tcg_temp_new_i64();
13810 tcg_op2 = tcg_temp_new_i64();
13811 tcg_res[0] = tcg_temp_new_i64();
13812 tcg_res[1] = tcg_temp_new_i64();
13814 for (pass = 0; pass < 2; pass++) {
13815 read_vec_element(s, tcg_op1, rn, pass, MO_64);
13816 read_vec_element(s, tcg_op2, rm, pass, MO_64);
13818 tcg_gen_xor_i64(tcg_res[pass], tcg_op1, tcg_op2);
13819 tcg_gen_rotri_i64(tcg_res[pass], tcg_res[pass], imm6);
13821 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13822 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13824 tcg_temp_free_i64(tcg_op1);
13825 tcg_temp_free_i64(tcg_op2);
13826 tcg_temp_free_i64(tcg_res[0]);
13827 tcg_temp_free_i64(tcg_res[1]);
13830 /* Crypto three-reg imm2
13831 * 31 21 20 16 15 14 13 12 11 10 9 5 4 0
13832 * +-----------------------+------+-----+------+--------+------+------+
13833 * | 1 1 0 0 1 1 1 0 0 1 0 | Rm | 1 0 | imm2 | opcode | Rn | Rd |
13834 * +-----------------------+------+-----+------+--------+------+------+
13836 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
13838 int opcode = extract32(insn, 10, 2);
13839 int imm2 = extract32(insn, 12, 2);
13840 int rm = extract32(insn, 16, 5);
13841 int rn = extract32(insn, 5, 5);
13842 int rd = extract32(insn, 0, 5);
13843 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr;
13844 TCGv_i32 tcg_imm2, tcg_opcode;
13846 if (!dc_isar_feature(aa64_sm3, s)) {
13847 unallocated_encoding(s);
13848 return;
13851 if (!fp_access_check(s)) {
13852 return;
13855 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13856 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
13857 tcg_rm_ptr = vec_full_reg_ptr(s, rm);
13858 tcg_imm2 = tcg_const_i32(imm2);
13859 tcg_opcode = tcg_const_i32(opcode);
13861 gen_helper_crypto_sm3tt(tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr, tcg_imm2,
13862 tcg_opcode);
13864 tcg_temp_free_ptr(tcg_rd_ptr);
13865 tcg_temp_free_ptr(tcg_rn_ptr);
13866 tcg_temp_free_ptr(tcg_rm_ptr);
13867 tcg_temp_free_i32(tcg_imm2);
13868 tcg_temp_free_i32(tcg_opcode);
13871 /* C3.6 Data processing - SIMD, inc Crypto
13873 * As the decode gets a little complex we are using a table based
13874 * approach for this part of the decode.
13876 static const AArch64DecodeTable data_proc_simd[] = {
13877 /* pattern , mask , fn */
13878 { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
13879 { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
13880 { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
13881 { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
13882 { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
13883 { 0x0e000400, 0x9fe08400, disas_simd_copy },
13884 { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
13885 /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
13886 { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
13887 { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
13888 { 0x0e000000, 0xbf208c00, disas_simd_tb },
13889 { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
13890 { 0x2e000000, 0xbf208400, disas_simd_ext },
13891 { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
13892 { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
13893 { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
13894 { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
13895 { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
13896 { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
13897 { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
13898 { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
13899 { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
13900 { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
13901 { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
13902 { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
13903 { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
13904 { 0xce000000, 0xff808000, disas_crypto_four_reg },
13905 { 0xce800000, 0xffe00000, disas_crypto_xar },
13906 { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
13907 { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
13908 { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
13909 { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
13910 { 0x00000000, 0x00000000, NULL }
13913 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
13915 /* Note that this is called with all non-FP cases from
13916 * table C3-6 so it must UNDEF for entries not specifically
13917 * allocated to instructions in that table.
13919 AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
13920 if (fn) {
13921 fn(s, insn);
13922 } else {
13923 unallocated_encoding(s);
13927 /* C3.6 Data processing - SIMD and floating point */
13928 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
13930 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
13931 disas_data_proc_fp(s, insn);
13932 } else {
13933 /* SIMD, including crypto */
13934 disas_data_proc_simd(s, insn);
13939 * is_guarded_page:
13940 * @env: The cpu environment
13941 * @s: The DisasContext
13943 * Return true if the page is guarded.
13945 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
13947 #ifdef CONFIG_USER_ONLY
13948 return false; /* FIXME */
13949 #else
13950 uint64_t addr = s->base.pc_first;
13951 int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
13952 unsigned int index = tlb_index(env, mmu_idx, addr);
13953 CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr);
13956 * We test this immediately after reading an insn, which means
13957 * that any normal page must be in the TLB. The only exception
13958 * would be for executing from flash or device memory, which
13959 * does not retain the TLB entry.
13961 * FIXME: Assume false for those, for now. We could use
13962 * arm_cpu_get_phys_page_attrs_debug to re-read the page
13963 * table entry even for that case.
13965 return (tlb_hit(entry->addr_code, addr) &&
13966 env_tlb(env)->d[mmu_idx].iotlb[index].attrs.target_tlb_bit0);
13967 #endif
13971 * btype_destination_ok:
13972 * @insn: The instruction at the branch destination
13973 * @bt: SCTLR_ELx.BT
13974 * @btype: PSTATE.BTYPE, and is non-zero
13976 * On a guarded page, there are a limited number of insns
13977 * that may be present at the branch target:
13978 * - branch target identifiers,
13979 * - paciasp, pacibsp,
13980 * - BRK insn
13981 * - HLT insn
13982 * Anything else causes a Branch Target Exception.
13984 * Return true if the branch is compatible, false to raise BTITRAP.
13986 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
13988 if ((insn & 0xfffff01fu) == 0xd503201fu) {
13989 /* HINT space */
13990 switch (extract32(insn, 5, 7)) {
13991 case 0b011001: /* PACIASP */
13992 case 0b011011: /* PACIBSP */
13994 * If SCTLR_ELx.BT, then PACI*SP are not compatible
13995 * with btype == 3. Otherwise all btype are ok.
13997 return !bt || btype != 3;
13998 case 0b100000: /* BTI */
13999 /* Not compatible with any btype. */
14000 return false;
14001 case 0b100010: /* BTI c */
14002 /* Not compatible with btype == 3 */
14003 return btype != 3;
14004 case 0b100100: /* BTI j */
14005 /* Not compatible with btype == 2 */
14006 return btype != 2;
14007 case 0b100110: /* BTI jc */
14008 /* Compatible with any btype. */
14009 return true;
14011 } else {
14012 switch (insn & 0xffe0001fu) {
14013 case 0xd4200000u: /* BRK */
14014 case 0xd4400000u: /* HLT */
14015 /* Give priority to the breakpoint exception. */
14016 return true;
14019 return false;
14022 /* C3.1 A64 instruction index by encoding */
14023 static void disas_a64_insn(CPUARMState *env, DisasContext *s)
14025 uint32_t insn;
14027 s->pc_curr = s->base.pc_next;
14028 insn = arm_ldl_code(env, s->base.pc_next, s->sctlr_b);
14029 s->insn = insn;
14030 s->base.pc_next += 4;
14032 s->fp_access_checked = false;
14034 if (dc_isar_feature(aa64_bti, s)) {
14035 if (s->base.num_insns == 1) {
14037 * At the first insn of the TB, compute s->guarded_page.
14038 * We delayed computing this until successfully reading
14039 * the first insn of the TB, above. This (mostly) ensures
14040 * that the softmmu tlb entry has been populated, and the
14041 * page table GP bit is available.
14043 * Note that we need to compute this even if btype == 0,
14044 * because this value is used for BR instructions later
14045 * where ENV is not available.
14047 s->guarded_page = is_guarded_page(env, s);
14049 /* First insn can have btype set to non-zero. */
14050 tcg_debug_assert(s->btype >= 0);
14053 * Note that the Branch Target Exception has fairly high
14054 * priority -- below debugging exceptions but above most
14055 * everything else. This allows us to handle this now
14056 * instead of waiting until the insn is otherwise decoded.
14058 if (s->btype != 0
14059 && s->guarded_page
14060 && !btype_destination_ok(insn, s->bt, s->btype)) {
14061 gen_exception_insn(s, s->pc_curr, EXCP_UDEF,
14062 syn_btitrap(s->btype),
14063 default_exception_el(s));
14064 return;
14066 } else {
14067 /* Not the first insn: btype must be 0. */
14068 tcg_debug_assert(s->btype == 0);
14072 switch (extract32(insn, 25, 4)) {
14073 case 0x0: case 0x1: case 0x3: /* UNALLOCATED */
14074 unallocated_encoding(s);
14075 break;
14076 case 0x2:
14077 if (!dc_isar_feature(aa64_sve, s) || !disas_sve(s, insn)) {
14078 unallocated_encoding(s);
14080 break;
14081 case 0x8: case 0x9: /* Data processing - immediate */
14082 disas_data_proc_imm(s, insn);
14083 break;
14084 case 0xa: case 0xb: /* Branch, exception generation and system insns */
14085 disas_b_exc_sys(s, insn);
14086 break;
14087 case 0x4:
14088 case 0x6:
14089 case 0xc:
14090 case 0xe: /* Loads and stores */
14091 disas_ldst(s, insn);
14092 break;
14093 case 0x5:
14094 case 0xd: /* Data processing - register */
14095 disas_data_proc_reg(s, insn);
14096 break;
14097 case 0x7:
14098 case 0xf: /* Data processing - SIMD and floating point */
14099 disas_data_proc_simd_fp(s, insn);
14100 break;
14101 default:
14102 assert(FALSE); /* all 15 cases should be handled above */
14103 break;
14106 /* if we allocated any temporaries, free them here */
14107 free_tmp_a64(s);
14110 * After execution of most insns, btype is reset to 0.
14111 * Note that we set btype == -1 when the insn sets btype.
14113 if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14114 reset_btype(s);
14118 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14119 CPUState *cpu)
14121 DisasContext *dc = container_of(dcbase, DisasContext, base);
14122 CPUARMState *env = cpu->env_ptr;
14123 ARMCPU *arm_cpu = env_archcpu(env);
14124 uint32_t tb_flags = dc->base.tb->flags;
14125 int bound, core_mmu_idx;
14127 dc->isar = &arm_cpu->isar;
14128 dc->condjmp = 0;
14130 dc->aarch64 = 1;
14131 /* If we are coming from secure EL0 in a system with a 32-bit EL3, then
14132 * there is no secure EL1, so we route exceptions to EL3.
14134 dc->secure_routed_to_el3 = arm_feature(env, ARM_FEATURE_EL3) &&
14135 !arm_el_is_aa64(env, 3);
14136 dc->thumb = 0;
14137 dc->sctlr_b = 0;
14138 dc->be_data = FIELD_EX32(tb_flags, TBFLAG_ANY, BE_DATA) ? MO_BE : MO_LE;
14139 dc->condexec_mask = 0;
14140 dc->condexec_cond = 0;
14141 core_mmu_idx = FIELD_EX32(tb_flags, TBFLAG_ANY, MMUIDX);
14142 dc->mmu_idx = core_to_arm_mmu_idx(env, core_mmu_idx);
14143 dc->tbii = FIELD_EX32(tb_flags, TBFLAG_A64, TBII);
14144 dc->tbid = FIELD_EX32(tb_flags, TBFLAG_A64, TBID);
14145 dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14146 #if !defined(CONFIG_USER_ONLY)
14147 dc->user = (dc->current_el == 0);
14148 #endif
14149 dc->fp_excp_el = FIELD_EX32(tb_flags, TBFLAG_ANY, FPEXC_EL);
14150 dc->sve_excp_el = FIELD_EX32(tb_flags, TBFLAG_A64, SVEEXC_EL);
14151 dc->sve_len = (FIELD_EX32(tb_flags, TBFLAG_A64, ZCR_LEN) + 1) * 16;
14152 dc->pauth_active = FIELD_EX32(tb_flags, TBFLAG_A64, PAUTH_ACTIVE);
14153 dc->bt = FIELD_EX32(tb_flags, TBFLAG_A64, BT);
14154 dc->btype = FIELD_EX32(tb_flags, TBFLAG_A64, BTYPE);
14155 dc->vec_len = 0;
14156 dc->vec_stride = 0;
14157 dc->cp_regs = arm_cpu->cp_regs;
14158 dc->features = env->features;
14160 /* Single step state. The code-generation logic here is:
14161 * SS_ACTIVE == 0:
14162 * generate code with no special handling for single-stepping (except
14163 * that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14164 * this happens anyway because those changes are all system register or
14165 * PSTATE writes).
14166 * SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14167 * emit code for one insn
14168 * emit code to clear PSTATE.SS
14169 * emit code to generate software step exception for completed step
14170 * end TB (as usual for having generated an exception)
14171 * SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14172 * emit code to generate a software step exception
14173 * end the TB
14175 dc->ss_active = FIELD_EX32(tb_flags, TBFLAG_ANY, SS_ACTIVE);
14176 dc->pstate_ss = FIELD_EX32(tb_flags, TBFLAG_ANY, PSTATE_SS);
14177 dc->is_ldex = false;
14178 dc->debug_target_el = FIELD_EX32(tb_flags, TBFLAG_ANY, DEBUG_TARGET_EL);
14180 /* Bound the number of insns to execute to those left on the page. */
14181 bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14183 /* If architectural single step active, limit to 1. */
14184 if (dc->ss_active) {
14185 bound = 1;
14187 dc->base.max_insns = MIN(dc->base.max_insns, bound);
14189 init_tmp_a64_array(dc);
14192 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14196 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14198 DisasContext *dc = container_of(dcbase, DisasContext, base);
14200 tcg_gen_insn_start(dc->base.pc_next, 0, 0);
14201 dc->insn_start = tcg_last_op();
14204 static bool aarch64_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,
14205 const CPUBreakpoint *bp)
14207 DisasContext *dc = container_of(dcbase, DisasContext, base);
14209 if (bp->flags & BP_CPU) {
14210 gen_a64_set_pc_im(dc->base.pc_next);
14211 gen_helper_check_breakpoints(cpu_env);
14212 /* End the TB early; it likely won't be executed */
14213 dc->base.is_jmp = DISAS_TOO_MANY;
14214 } else {
14215 gen_exception_internal_insn(dc, dc->base.pc_next, EXCP_DEBUG);
14216 /* The address covered by the breakpoint must be
14217 included in [tb->pc, tb->pc + tb->size) in order
14218 to for it to be properly cleared -- thus we
14219 increment the PC here so that the logic setting
14220 tb->size below does the right thing. */
14221 dc->base.pc_next += 4;
14222 dc->base.is_jmp = DISAS_NORETURN;
14225 return true;
14228 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14230 DisasContext *dc = container_of(dcbase, DisasContext, base);
14231 CPUARMState *env = cpu->env_ptr;
14233 if (dc->ss_active && !dc->pstate_ss) {
14234 /* Singlestep state is Active-pending.
14235 * If we're in this state at the start of a TB then either
14236 * a) we just took an exception to an EL which is being debugged
14237 * and this is the first insn in the exception handler
14238 * b) debug exceptions were masked and we just unmasked them
14239 * without changing EL (eg by clearing PSTATE.D)
14240 * In either case we're going to take a swstep exception in the
14241 * "did not step an insn" case, and so the syndrome ISV and EX
14242 * bits should be zero.
14244 assert(dc->base.num_insns == 1);
14245 gen_swstep_exception(dc, 0, 0);
14246 dc->base.is_jmp = DISAS_NORETURN;
14247 } else {
14248 disas_a64_insn(env, dc);
14251 translator_loop_temp_check(&dc->base);
14254 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14256 DisasContext *dc = container_of(dcbase, DisasContext, base);
14258 if (unlikely(dc->base.singlestep_enabled || dc->ss_active)) {
14259 /* Note that this means single stepping WFI doesn't halt the CPU.
14260 * For conditional branch insns this is harmless unreachable code as
14261 * gen_goto_tb() has already handled emitting the debug exception
14262 * (and thus a tb-jump is not possible when singlestepping).
14264 switch (dc->base.is_jmp) {
14265 default:
14266 gen_a64_set_pc_im(dc->base.pc_next);
14267 /* fall through */
14268 case DISAS_EXIT:
14269 case DISAS_JUMP:
14270 if (dc->base.singlestep_enabled) {
14271 gen_exception_internal(EXCP_DEBUG);
14272 } else {
14273 gen_step_complete_exception(dc);
14275 break;
14276 case DISAS_NORETURN:
14277 break;
14279 } else {
14280 switch (dc->base.is_jmp) {
14281 case DISAS_NEXT:
14282 case DISAS_TOO_MANY:
14283 gen_goto_tb(dc, 1, dc->base.pc_next);
14284 break;
14285 default:
14286 case DISAS_UPDATE:
14287 gen_a64_set_pc_im(dc->base.pc_next);
14288 /* fall through */
14289 case DISAS_EXIT:
14290 tcg_gen_exit_tb(NULL, 0);
14291 break;
14292 case DISAS_JUMP:
14293 tcg_gen_lookup_and_goto_ptr();
14294 break;
14295 case DISAS_NORETURN:
14296 case DISAS_SWI:
14297 break;
14298 case DISAS_WFE:
14299 gen_a64_set_pc_im(dc->base.pc_next);
14300 gen_helper_wfe(cpu_env);
14301 break;
14302 case DISAS_YIELD:
14303 gen_a64_set_pc_im(dc->base.pc_next);
14304 gen_helper_yield(cpu_env);
14305 break;
14306 case DISAS_WFI:
14308 /* This is a special case because we don't want to just halt the CPU
14309 * if trying to debug across a WFI.
14311 TCGv_i32 tmp = tcg_const_i32(4);
14313 gen_a64_set_pc_im(dc->base.pc_next);
14314 gen_helper_wfi(cpu_env, tmp);
14315 tcg_temp_free_i32(tmp);
14316 /* The helper doesn't necessarily throw an exception, but we
14317 * must go back to the main loop to check for interrupts anyway.
14319 tcg_gen_exit_tb(NULL, 0);
14320 break;
14326 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14327 CPUState *cpu)
14329 DisasContext *dc = container_of(dcbase, DisasContext, base);
14331 qemu_log("IN: %s\n", lookup_symbol(dc->base.pc_first));
14332 log_target_disas(cpu, dc->base.pc_first, dc->base.tb->size);
14335 const TranslatorOps aarch64_translator_ops = {
14336 .init_disas_context = aarch64_tr_init_disas_context,
14337 .tb_start = aarch64_tr_tb_start,
14338 .insn_start = aarch64_tr_insn_start,
14339 .breakpoint_check = aarch64_tr_breakpoint_check,
14340 .translate_insn = aarch64_tr_translate_insn,
14341 .tb_stop = aarch64_tr_tb_stop,
14342 .disas_log = aarch64_tr_disas_log,